]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
lex.c (java_read_unicode): Reset bs_count when finished with `\u' sequence.
[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"
19e223db 68#include "ggc.h"
e04a16fb 69
c2952b01
APB
70#ifndef DIR_SEPARATOR
71#define DIR_SEPARATOR '/'
72#endif
73
82371d41 74/* Local function prototypes */
df32d2ce
KG
75static char *java_accstring_lookup PARAMS ((int));
76static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
77static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
df32d2ce
KG
78static tree create_class PARAMS ((int, tree, tree, tree));
79static tree create_interface PARAMS ((int, tree, tree));
c2952b01 80static void end_class_declaration PARAMS ((int));
df32d2ce
KG
81static tree find_field PARAMS ((tree, tree));
82static tree lookup_field_wrapper PARAMS ((tree, tree));
83static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
84static void register_fields PARAMS ((int, tree, tree));
98a52c2c 85static tree parser_qualified_classname PARAMS ((tree));
df32d2ce
KG
86static int parser_check_super PARAMS ((tree, tree, tree));
87static int parser_check_super_interface PARAMS ((tree, tree, tree));
88static void check_modifiers_consistency PARAMS ((int));
89static tree lookup_cl PARAMS ((tree));
90static tree lookup_java_method2 PARAMS ((tree, tree, int));
91static tree method_header PARAMS ((int, tree, tree, tree));
92static void fix_method_argument_names PARAMS ((tree ,tree));
93static tree method_declarator PARAMS ((tree, tree));
94static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
d4476be2 95 ATTRIBUTE_PRINTF_2;
df32d2ce
KG
96static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list));
97static void parse_ctor_invocation_error PARAMS ((void));
98static tree parse_jdk1_1_error PARAMS ((const char *));
99static void complete_class_report_errors PARAMS ((jdep *));
100static int process_imports PARAMS ((void));
101static void read_import_dir PARAMS ((tree));
102static int find_in_imports_on_demand PARAMS ((tree));
9a7ab4b3 103static void find_in_imports PARAMS ((tree));
cf1748bf 104static void check_inner_class_access PARAMS ((tree, tree, tree));
df32d2ce 105static int check_pkg_class_access PARAMS ((tree, tree));
9a7ab4b3 106static void register_package PARAMS ((tree));
df32d2ce
KG
107static tree resolve_package PARAMS ((tree, tree *));
108static tree lookup_package_type PARAMS ((const char *, int));
109static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
c2952b01 110static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
111static void declare_local_variables PARAMS ((int, tree, tree));
112static void source_start_java_method PARAMS ((tree));
113static void source_end_java_method PARAMS ((void));
114static void expand_start_java_method PARAMS ((tree));
115static tree find_name_in_single_imports PARAMS ((tree));
116static void check_abstract_method_header PARAMS ((tree));
117static tree lookup_java_interface_method2 PARAMS ((tree, tree));
118static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 119static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 120static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 121 tree, tree, tree));
df32d2ce 122static tree patch_method_invocation PARAMS ((tree, tree, tree,
89e09b9a 123 int *, tree *));
df32d2ce
KG
124static int breakdown_qualified PARAMS ((tree *, tree *, tree));
125static tree resolve_and_layout PARAMS ((tree, tree));
9a7ab4b3 126static tree qualify_and_find PARAMS ((tree, tree, tree));
df32d2ce
KG
127static tree resolve_no_layout PARAMS ((tree, tree));
128static int invocation_mode PARAMS ((tree, int));
129static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 130 tree, tree));
df32d2ce 131static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 132 tree *, tree *));
df32d2ce
KG
133static tree find_most_specific_methods_list PARAMS ((tree));
134static int argument_types_convertible PARAMS ((tree, tree));
135static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 136static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
137static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
138static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
139static tree obtain_incomplete_type PARAMS ((tree));
140static tree java_complete_lhs PARAMS ((tree));
141static tree java_complete_tree PARAMS ((tree));
c2952b01 142static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
92d83515 143static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
144static void java_complete_expand_method PARAMS ((tree));
145static int unresolved_type_p PARAMS ((tree, tree *));
146static void create_jdep_list PARAMS ((struct parser_ctxt *));
147static tree build_expr_block PARAMS ((tree, tree));
148static tree enter_block PARAMS ((void));
149static tree enter_a_block PARAMS ((tree));
150static tree exit_block PARAMS ((void));
151static tree lookup_name_in_blocks PARAMS ((tree));
152static void maybe_absorb_scoping_blocks PARAMS ((void));
153static tree build_method_invocation PARAMS ((tree, tree));
154static tree build_new_invocation PARAMS ((tree, tree));
155static tree build_assignment PARAMS ((int, int, tree, tree));
156static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
157static int check_final_assignment PARAMS ((tree ,tree));
158static tree patch_assignment PARAMS ((tree, tree, tree ));
159static tree patch_binop PARAMS ((tree, tree, tree));
160static tree build_unaryop PARAMS ((int, int, tree));
161static tree build_incdec PARAMS ((int, int, tree, int));
162static tree patch_unaryop PARAMS ((tree, tree));
163static tree build_cast PARAMS ((int, tree, tree));
164static tree build_null_of_type PARAMS ((tree));
165static tree patch_cast PARAMS ((tree, tree));
166static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
167static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
168static int valid_cast_to_p PARAMS ((tree, tree));
169static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
170static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
171static tree try_reference_assignconv PARAMS ((tree, tree));
172static tree build_unresolved_array_type PARAMS ((tree));
173static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
174static tree build_array_ref PARAMS ((int, tree, tree));
175static tree patch_array_ref PARAMS ((tree));
176static tree make_qualified_name PARAMS ((tree, tree, int));
177static tree merge_qualified_name PARAMS ((tree, tree));
178static tree make_qualified_primary PARAMS ((tree, tree, int));
179static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 180 tree *, tree *));
df32d2ce 181static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
182static tree resolve_field_access PARAMS ((tree, tree *, tree *));
183static tree build_newarray_node PARAMS ((tree, tree, int));
184static tree patch_newarray PARAMS ((tree));
185static tree resolve_type_during_patch PARAMS ((tree));
186static tree build_this PARAMS ((int));
9a7ab4b3 187static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
188static tree build_return PARAMS ((int, tree));
189static tree patch_return PARAMS ((tree));
190static tree maybe_access_field PARAMS ((tree, tree, tree));
191static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
192static int check_for_static_method_reference PARAMS ((tree, tree, tree,
193 tree, tree));
df32d2ce
KG
194static int not_accessible_p PARAMS ((tree, tree, int));
195static void check_deprecation PARAMS ((tree, tree));
196static int class_in_current_package PARAMS ((tree));
197static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
198static tree patch_if_else_statement PARAMS ((tree));
199static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
200static tree add_stmt_to_block PARAMS ((tree, tree, tree));
201static tree patch_exit_expr PARAMS ((tree));
202static tree build_labeled_block PARAMS ((int, tree));
203static tree finish_labeled_statement PARAMS ((tree, tree));
204static tree build_bc_statement PARAMS ((int, int, tree));
205static tree patch_bc_statement PARAMS ((tree));
206static tree patch_loop_statement PARAMS ((tree));
207static tree build_new_loop PARAMS ((tree));
208static tree build_loop_body PARAMS ((int, tree, int));
209static tree finish_loop_body PARAMS ((int, tree, tree, int));
210static tree build_debugable_stmt PARAMS ((int, tree));
211static tree finish_for_loop PARAMS ((int, tree, tree, tree));
212static tree patch_switch_statement PARAMS ((tree));
213static tree string_constant_concatenation PARAMS ((tree, tree));
214static tree build_string_concatenation PARAMS ((tree, tree));
215static tree patch_string_cst PARAMS ((tree));
216static tree patch_string PARAMS ((tree));
217static tree build_try_statement PARAMS ((int, tree, tree));
218static tree build_try_finally_statement PARAMS ((int, tree, tree));
219static tree patch_try_statement PARAMS ((tree));
220static tree patch_synchronized_statement PARAMS ((tree, tree));
221static tree patch_throw_statement PARAMS ((tree, tree));
222static void check_thrown_exceptions PARAMS ((int, tree));
223static int check_thrown_exceptions_do PARAMS ((tree));
224static void purge_unchecked_exceptions PARAMS ((tree));
225static void check_throws_clauses PARAMS ((tree, tree, tree));
226static void finish_method_declaration PARAMS ((tree));
227static tree build_super_invocation PARAMS ((tree));
228static int verify_constructor_circularity PARAMS ((tree, tree));
229static char *constructor_circularity_msg PARAMS ((tree, tree));
230static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 231 int, int));
df32d2ce
KG
232static const char *get_printable_method_name PARAMS ((tree));
233static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
234static tree generate_finit PARAMS ((tree));
235static void add_instance_initializer PARAMS ((tree));
df32d2ce 236static void fix_constructors PARAMS ((tree));
c2952b01
APB
237static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
238 tree, int *));
239static void craft_constructor PARAMS ((tree, tree));
240static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
241static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
242static void start_artificial_method_body PARAMS ((tree));
243static void end_artificial_method_body PARAMS ((tree));
244static int check_method_redefinition PARAMS ((tree, tree));
245static int reset_method_name PARAMS ((tree));
165f37bc 246static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
247static void java_check_regular_methods PARAMS ((tree));
248static void java_check_abstract_methods PARAMS ((tree));
249static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
250static void unreachable_stmt_error PARAMS ((tree));
251static tree find_expr_with_wfl PARAMS ((tree));
252static void missing_return_error PARAMS ((tree));
253static tree build_new_array_init PARAMS ((int, tree));
254static tree patch_new_array_init PARAMS ((tree, tree));
255static tree maybe_build_array_element_wfl PARAMS ((tree));
256static int array_constructor_check_entry PARAMS ((tree, tree));
257static const char *purify_type_name PARAMS ((const char *));
258static tree fold_constant_for_init PARAMS ((tree, tree));
259static tree strip_out_static_field_access_decl PARAMS ((tree));
260static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
261static void static_ref_err PARAMS ((tree, tree, tree));
262static void parser_add_interface PARAMS ((tree, tree, tree));
263static void add_superinterfaces PARAMS ((tree, tree));
264static tree jdep_resolve_class PARAMS ((jdep *));
265static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
266static void java_complete_expand_classes PARAMS ((void));
267static void java_complete_expand_class PARAMS ((tree));
268static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
269static tree cut_identifier_in_qualified PARAMS ((tree));
270static tree java_stabilize_reference PARAMS ((tree));
271static tree do_unary_numeric_promotion PARAMS ((tree));
272static char * operator_string PARAMS ((tree));
273static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
274static tree merge_string_cste PARAMS ((tree, tree, int));
275static tree java_refold PARAMS ((tree));
276static int java_decl_equiv PARAMS ((tree, tree));
277static int binop_compound_p PARAMS ((enum tree_code));
278static tree search_loop PARAMS ((tree));
279static int labeled_block_contains_loop_p PARAMS ((tree, tree));
1175b9b4 280static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
281static void java_check_abstract_method_definitions PARAMS ((tree));
282static void java_debug_context_do PARAMS ((int));
c2952b01
APB
283static void java_parser_context_push_initialized_field PARAMS ((void));
284static void java_parser_context_pop_initialized_field PARAMS ((void));
285static tree reorder_static_initialized PARAMS ((tree));
286static void java_parser_context_suspend PARAMS ((void));
287static void java_parser_context_resume PARAMS ((void));
288
289/* JDK 1.1 work. FIXME */
290
291static tree maybe_make_nested_class_name PARAMS ((tree));
292static void make_nested_class_name PARAMS ((tree));
293static void set_nested_class_simple_name_value PARAMS ((tree, int));
294static void link_nested_class_to_enclosing PARAMS ((void));
295static tree find_as_inner_class PARAMS ((tree, tree, tree));
296static tree find_as_inner_class_do PARAMS ((tree, tree));
297static int check_inner_class_redefinition PARAMS ((tree, tree));
298
299static tree build_thisn_assign PARAMS ((void));
300static tree build_current_thisn PARAMS ((tree));
301static tree build_access_to_thisn PARAMS ((tree, tree, int));
302static tree maybe_build_thisn_access_method PARAMS ((tree));
303
304static tree build_outer_field_access PARAMS ((tree, tree));
305static tree build_outer_field_access_methods PARAMS ((tree));
306static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
307 tree, tree));
308static tree build_outer_method_access_method PARAMS ((tree));
309static tree build_new_access_id PARAMS ((void));
310static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
311 tree, tree));
312
313static int outer_field_access_p PARAMS ((tree, tree));
314static int outer_field_expanded_access_p PARAMS ((tree, tree *,
315 tree *, tree *));
316static tree outer_field_access_fix PARAMS ((tree, tree, tree));
317static tree build_incomplete_class_ref PARAMS ((int, tree));
318static tree patch_incomplete_class_ref PARAMS ((tree));
319static tree create_anonymous_class PARAMS ((int, tree));
320static void patch_anonymous_class PARAMS ((tree, tree, tree));
321static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 322
165f37bc
APB
323static tree build_dot_class_method PARAMS ((tree));
324static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 325static void create_new_parser_context PARAMS ((int));
f15b9af9 326static void mark_parser_ctxt PARAMS ((void *));
165f37bc 327
e04a16fb
AG
328/* Number of error found so far. */
329int java_error_count;
330/* Number of warning found so far. */
331int java_warning_count;
ce6e9147
APB
332/* Tell when not to fold, when doing xrefs */
333int do_not_fold;
c2952b01
APB
334/* Cyclic inheritance report, as it can be set by layout_class */
335char *cyclic_inheritance_report;
f15b9af9 336
c2952b01
APB
337/* Tell when we're within an instance initializer */
338static int in_instance_initializer;
e04a16fb
AG
339
340/* The current parser context */
d4370213 341struct parser_ctxt *ctxp;
e04a16fb 342
d4370213 343/* List of things that were analyzed for which code will be generated */
b351b287
APB
344static struct parser_ctxt *ctxp_for_generation = NULL;
345
e04a16fb
AG
346/* binop_lookup maps token to tree_code. It is used where binary
347 operations are involved and required by the parser. RDIV_EXPR
348 covers both integral/floating point division. The code is changed
349 once the type of both operator is worked out. */
350
351static enum tree_code binop_lookup[19] =
352 {
353 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
354 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
355 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
356 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
357 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
358 };
359#define BINOP_LOOKUP(VALUE) \
6e2aa220 360 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 361
5cbdba64
APB
362/* This is the end index for binary operators that can also be used
363 in compound assignements. */
364#define BINOP_COMPOUND_CANDIDATES 11
365
e04a16fb 366/* The "$L" identifier we use to create labels. */
b67d701b
PB
367static tree label_id = NULL_TREE;
368
369/* The "StringBuffer" identifier used for the String `+' operator. */
370static tree wfl_string_buffer = NULL_TREE;
371
372/* The "append" identifier used for String `+' operator. */
373static tree wfl_append = NULL_TREE;
374
375/* The "toString" identifier used for String `+' operator. */
376static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
377
378/* The "java.lang" import qualified name. */
379static tree java_lang_id = NULL_TREE;
09ed0f70 380
c2952b01
APB
381/* The generated `inst$' identifier used for generated enclosing
382 instance/field access functions. */
383static tree inst_id = NULL_TREE;
384
09ed0f70
APB
385/* The "java.lang.Cloneable" qualified name. */
386static tree java_lang_cloneable = NULL_TREE;
f099f336 387
ee17a290
TT
388/* The "java.io.Serializable" qualified name. */
389static tree java_io_serializable = NULL_TREE;
390
f099f336
APB
391/* Context and flag for static blocks */
392static tree current_static_block = NULL_TREE;
393
c2952b01
APB
394/* The generated `write_parm_value$' identifier. */
395static tree wpv_id;
396
ee07f4f4
APB
397/* The list of all packages we've seen so far */
398static tree package_list = NULL_TREE;
2884c41e 399
19e223db
MM
400/* Hold THIS for the scope of the current public method decl. */
401static tree current_this;
402
403/* Hold a list of catch clauses list. The first element of this list is
404 the list of the catch clauses of the currently analysed try block. */
405static tree currently_caught_type_list;
406
2884c41e
KG
407/* Check modifiers. If one doesn't fit, retrieve it in its declaration
408 line and point it out. */
409/* Should point out the one that don't fit. ASCII/unicode, going
410 backward. FIXME */
411
412#define check_modifiers(__message, __value, __mask) do { \
413 if ((__value) & ~(__mask)) \
414 { \
415 int i, remainder = (__value) & ~(__mask); \
416 for (i = 0; i <= 10; i++) \
417 if ((1 << i) & remainder) \
418 parse_error_context (ctxp->modifier_ctx [i], (__message), \
419 java_accstring_lookup (1 << i)); \
420 } \
421} while (0)
ee07f4f4 422
e04a16fb
AG
423%}
424
425%union {
426 tree node;
427 int sub_token;
428 struct {
429 int token;
430 int location;
431 } operator;
432 int value;
433}
434
9ee9b555
KG
435%{
436#include "lex.c"
437%}
438
e04a16fb
AG
439%pure_parser
440
441/* Things defined here have to match the order of what's in the
442 binop_lookup table. */
443
444%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
445%token LS_TK SRS_TK ZRS_TK
446%token AND_TK XOR_TK OR_TK
447%token BOOL_AND_TK BOOL_OR_TK
448%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
449
450/* This maps to the same binop_lookup entry than the token above */
451
452%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
453%token REM_ASSIGN_TK
454%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
455%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
456
457
458/* Modifier TOKEN have to be kept in this order. Don't scramble it */
459
460%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
461%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
462%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
463%token PAD_TK ABSTRACT_TK MODIFIER_TK
464
465/* Keep those two in order, too */
466%token DECR_TK INCR_TK
467
468/* From now one, things can be in any order */
469
470%token DEFAULT_TK IF_TK THROW_TK
471%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
472%token THROWS_TK BREAK_TK IMPORT_TK
473%token ELSE_TK INSTANCEOF_TK RETURN_TK
474%token VOID_TK CATCH_TK INTERFACE_TK
475%token CASE_TK EXTENDS_TK FINALLY_TK
476%token SUPER_TK WHILE_TK CLASS_TK
477%token SWITCH_TK CONST_TK TRY_TK
478%token FOR_TK NEW_TK CONTINUE_TK
479%token GOTO_TK PACKAGE_TK THIS_TK
480
481%token BYTE_TK SHORT_TK INT_TK LONG_TK
482%token CHAR_TK INTEGRAL_TK
483
484%token FLOAT_TK DOUBLE_TK FP_TK
485
486%token ID_TK
487
488%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
489
490%token ASSIGN_ANY_TK ASSIGN_TK
491%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
492
493%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
494%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
495
c2952b01 496%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
497
498%type <node> super ID_TK identifier
499%type <node> name simple_name qualified_name
c2952b01 500%type <node> type_declaration compilation_unit
e04a16fb
AG
501 field_declaration method_declaration extends_interfaces
502 interfaces interface_type_list
c2952b01 503 class_member_declaration
e04a16fb
AG
504 import_declarations package_declaration
505 type_declarations interface_body
506 interface_member_declaration constant_declaration
507 interface_member_declarations interface_type
508 abstract_method_declaration interface_type_list
509%type <node> class_body_declaration class_member_declaration
510 static_initializer constructor_declaration block
22eed1e6 511%type <node> class_body_declarations constructor_header
e04a16fb
AG
512%type <node> class_or_interface_type class_type class_type_list
513 constructor_declarator explicit_constructor_invocation
b9f7e36c 514%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
515
516%type <node> variable_declarator_id variable_declarator
517 variable_declarators variable_initializer
22eed1e6 518 variable_initializers constructor_body
ac825856 519 array_initializer
e04a16fb 520
2e5eb5c5 521%type <node> class_body block_end constructor_block_end
e04a16fb
AG
522%type <node> statement statement_without_trailing_substatement
523 labeled_statement if_then_statement label_decl
524 if_then_else_statement while_statement for_statement
525 statement_nsi labeled_statement_nsi do_statement
526 if_then_else_statement_nsi while_statement_nsi
527 for_statement_nsi statement_expression_list for_init
528 for_update statement_expression expression_statement
529 primary_no_new_array expression primary
530 array_creation_expression array_type
531 class_instance_creation_expression field_access
532 method_invocation array_access something_dot_new
533 argument_list postfix_expression while_expression
534 post_increment_expression post_decrement_expression
535 unary_expression_not_plus_minus unary_expression
536 pre_increment_expression pre_decrement_expression
537 unary_expression_not_plus_minus cast_expression
538 multiplicative_expression additive_expression
539 shift_expression relational_expression
540 equality_expression and_expression
541 exclusive_or_expression inclusive_or_expression
542 conditional_and_expression conditional_or_expression
543 conditional_expression assignment_expression
544 left_hand_side assignment for_header for_begin
545 constant_expression do_statement_begin empty_statement
b67d701b 546 switch_statement synchronized_statement throw_statement
f8976021 547 try_statement switch_expression switch_block
15fdcfe9 548 catches catch_clause catch_clause_parameter finally
c2952b01 549 anonymous_class_creation
e04a16fb
AG
550%type <node> return_statement break_statement continue_statement
551
552%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
553%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
554%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
555%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
556%type <operator> ASSIGN_ANY_TK assignment_operator
557%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
558%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
559%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 560%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 561%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
562%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
563%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 564%type <operator> NEW_TK
e04a16fb
AG
565
566%type <node> method_body
567
568%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
569 STRING_LIT_TK NULL_TK VOID_TK
570
571%type <node> IF_TK WHILE_TK FOR_TK
572
573%type <node> formal_parameter_list formal_parameter
574 method_declarator method_header
575
c2952b01 576%type <node> primitive_type reference_type type
e04a16fb
AG
577 BOOLEAN_TK INTEGRAL_TK FP_TK
578
c2952b01
APB
579/* Added or modified JDK 1.1 rule types */
580%type <node> type_literals array_type_literal
581
e04a16fb
AG
582%%
583/* 19.2 Production from 2.3: The Syntactic Grammar */
584goal:
19e223db
MM
585 {
586 /* Register static variables with the garbage
587 collector. */
588 ggc_add_tree_root (&label_id, 1);
589 ggc_add_tree_root (&wfl_string_buffer, 1);
590 ggc_add_tree_root (&wfl_append, 1);
591 ggc_add_tree_root (&wfl_to_string, 1);
592 ggc_add_tree_root (&java_lang_id, 1);
593 ggc_add_tree_root (&inst_id, 1);
594 ggc_add_tree_root (&java_lang_cloneable, 1);
595 ggc_add_tree_root (&java_io_serializable, 1);
596 ggc_add_tree_root (&current_static_block, 1);
597 ggc_add_tree_root (&wpv_id, 1);
598 ggc_add_tree_root (&package_list, 1);
599 ggc_add_tree_root (&current_this, 1);
600 ggc_add_tree_root (&currently_caught_type_list, 1);
f15b9af9
MM
601 ggc_add_root (&ctxp, 1,
602 sizeof (struct parser_ctxt *),
603 mark_parser_ctxt);
604 ggc_add_root (&ctxp_for_generation, 1,
605 sizeof (struct parser_ctxt *),
606 mark_parser_ctxt);
19e223db 607 }
e04a16fb
AG
608 compilation_unit
609 {}
610;
611
612/* 19.3 Productions from 3: Lexical structure */
613literal:
614 INT_LIT_TK
615| FP_LIT_TK
616| BOOL_LIT_TK
617| CHAR_LIT_TK
618| STRING_LIT_TK
619| NULL_TK
620;
621
622/* 19.4 Productions from 4: Types, Values and Variables */
623type:
624 primitive_type
625| reference_type
626;
627
628primitive_type:
629 INTEGRAL_TK
630| FP_TK
631| BOOLEAN_TK
632;
633
634reference_type:
635 class_or_interface_type
636| array_type
637;
638
639class_or_interface_type:
640 name
641;
642
643class_type:
644 class_or_interface_type /* Default rule */
645;
646
647interface_type:
648 class_or_interface_type
649;
650
651array_type:
652 primitive_type OSB_TK CSB_TK
653 {
654 $$ = build_java_array_type ($1, -1);
655 CLASS_LOADED_P ($$) = 1;
656 }
657| name OSB_TK CSB_TK
658 { $$ = build_unresolved_array_type ($1); }
659| array_type OSB_TK CSB_TK
660 { $$ = build_unresolved_array_type ($1); }
661| primitive_type OSB_TK error
662 {RULE ("']' expected"); RECOVER;}
663| array_type OSB_TK error
664 {RULE ("']' expected"); RECOVER;}
665;
666
667/* 19.5 Productions from 6: Names */
668name:
669 simple_name /* Default rule */
670| qualified_name /* Default rule */
671;
672
673simple_name:
674 identifier /* Default rule */
675;
676
677qualified_name:
678 name DOT_TK identifier
679 { $$ = make_qualified_name ($1, $3, $2.location); }
680;
681
682identifier:
683 ID_TK
684;
685
686/* 19.6: Production from 7: Packages */
687compilation_unit:
688 {$$ = NULL;}
689| package_declaration
690| import_declarations
691| type_declarations
692| package_declaration import_declarations
693| package_declaration type_declarations
694| import_declarations type_declarations
695| package_declaration import_declarations type_declarations
696;
697
698import_declarations:
699 import_declaration
700 {
701 $$ = NULL;
702 }
703| import_declarations import_declaration
704 {
705 $$ = NULL;
706 }
707;
708
709type_declarations:
710 type_declaration
711| type_declarations type_declaration
712;
713
714package_declaration:
715 PACKAGE_TK name SC_TK
ee07f4f4
APB
716 {
717 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 718 register_package (ctxp->package);
ee07f4f4 719 }
e04a16fb
AG
720| PACKAGE_TK error
721 {yyerror ("Missing name"); RECOVER;}
722| PACKAGE_TK name error
723 {yyerror ("';' expected"); RECOVER;}
724;
725
726import_declaration:
727 single_type_import_declaration
728| type_import_on_demand_declaration
729;
730
731single_type_import_declaration:
732 IMPORT_TK name SC_TK
733 {
9a7ab4b3 734 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 735 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 736 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
737 while (last != IDENTIFIER_POINTER (name))
738 {
739 if (last [0] == '.')
740 break;
741 last--;
742 }
743 last_name = get_identifier (++last);
744 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
745 {
746 tree err = find_name_in_single_imports (last_name);
747 if (err && err != name)
748 parse_error_context
749 ($2, "Ambiguous class: `%s' and `%s'",
750 IDENTIFIER_POINTER (name),
751 IDENTIFIER_POINTER (err));
5e942c50 752 else
9a7ab4b3 753 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
754 }
755 else
5e942c50 756 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
757 }
758| IMPORT_TK error
759 {yyerror ("Missing name"); RECOVER;}
760| IMPORT_TK name error
761 {yyerror ("';' expected"); RECOVER;}
762;
763
764type_import_on_demand_declaration:
765 IMPORT_TK name DOT_TK MULT_TK SC_TK
766 {
767 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
768 /* Don't import java.lang.* twice. */
769 if (name != java_lang_id)
770 {
ba179f9f 771 read_import_dir ($2);
9a7ab4b3
APB
772 ctxp->import_demand_list =
773 chainon (ctxp->import_demand_list,
774 build_tree_list ($2, NULL_TREE));
ba179f9f 775 }
e04a16fb
AG
776 }
777| IMPORT_TK name DOT_TK error
778 {yyerror ("'*' expected"); RECOVER;}
779| IMPORT_TK name DOT_TK MULT_TK error
780 {yyerror ("';' expected"); RECOVER;}
781;
782
783type_declaration:
784 class_declaration
c2952b01 785 { end_class_declaration (0); }
e04a16fb 786| interface_declaration
c2952b01 787 { end_class_declaration (0); }
5f1c312a 788| empty_statement
e04a16fb
AG
789| error
790 {
791 YYERROR_NOW;
792 yyerror ("Class or interface declaration expected");
793 }
794;
795
796/* 19.7 Shortened from the original:
797 modifiers: modifier | modifiers modifier
798 modifier: any of public... */
799modifiers:
800 MODIFIER_TK
801 {
802 $$ = (1 << $1);
803 }
804| modifiers MODIFIER_TK
805 {
806 int acc = (1 << $2);
807 if ($$ & acc)
808 parse_error_context
809 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
810 java_accstring_lookup (acc));
811 else
812 {
813 $$ |= acc;
814 }
815 }
816;
817
818/* 19.8.1 Production from $8.1: Class Declaration */
819class_declaration:
820 modifiers CLASS_TK identifier super interfaces
821 { create_class ($1, $3, $4, $5); }
822 class_body
e04a16fb
AG
823| CLASS_TK identifier super interfaces
824 { create_class (0, $2, $3, $4); }
825 class_body
e04a16fb
AG
826| modifiers CLASS_TK error
827 {yyerror ("Missing class name"); RECOVER;}
828| CLASS_TK error
829 {yyerror ("Missing class name"); RECOVER;}
830| CLASS_TK identifier error
0b4d333e
APB
831 {
832 if (!ctxp->class_err) yyerror ("'{' expected");
833 DRECOVER(class1);
834 }
e04a16fb
AG
835| modifiers CLASS_TK identifier error
836 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
837;
838
839super:
840 { $$ = NULL; }
841| EXTENDS_TK class_type
842 { $$ = $2; }
843| EXTENDS_TK class_type error
844 {yyerror ("'{' expected"); ctxp->class_err=1;}
845| EXTENDS_TK error
846 {yyerror ("Missing super class name"); ctxp->class_err=1;}
847;
848
849interfaces:
850 { $$ = NULL_TREE; }
851| IMPLEMENTS_TK interface_type_list
852 { $$ = $2; }
853| IMPLEMENTS_TK error
854 {
855 ctxp->class_err=1;
856 yyerror ("Missing interface name");
857 }
858;
859
860interface_type_list:
861 interface_type
862 {
863 ctxp->interface_number = 1;
864 $$ = build_tree_list ($1, NULL_TREE);
865 }
866| interface_type_list C_TK interface_type
867 {
868 ctxp->interface_number++;
869 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
870 }
871| interface_type_list C_TK error
872 {yyerror ("Missing interface name"); RECOVER;}
873;
874
875class_body:
876 OCB_TK CCB_TK
7f10c2e2
APB
877 {
878 /* Store the location of the `}' when doing xrefs */
879 if (flag_emit_xref)
c2952b01 880 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 881 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 882 $$ = GET_CPC ();
7f10c2e2 883 }
e04a16fb 884| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
885 {
886 /* Store the location of the `}' when doing xrefs */
887 if (flag_emit_xref)
c2952b01 888 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 889 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 890 $$ = GET_CPC ();
7f10c2e2 891 }
e04a16fb
AG
892;
893
894class_body_declarations:
895 class_body_declaration
896| class_body_declarations class_body_declaration
897;
898
899class_body_declaration:
900 class_member_declaration
901| static_initializer
902| constructor_declaration
903| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
904 {
905 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
906 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
907 }
e04a16fb
AG
908;
909
910class_member_declaration:
911 field_declaration
912| method_declaration
913| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
914 { end_class_declaration (1); }
915| interface_declaration /* Added, JDK1.1 inner interfaces */
916 { end_class_declaration (1); }
5f1c312a 917| empty_statement
e04a16fb
AG
918;
919
920/* 19.8.2 Productions from 8.3: Field Declarations */
921field_declaration:
922 type variable_declarators SC_TK
923 { register_fields (0, $1, $2); }
924| modifiers type variable_declarators SC_TK
925 {
e04a16fb
AG
926 check_modifiers
927 ("Illegal modifier `%s' for field declaration",
928 $1, FIELD_MODIFIERS);
929 check_modifiers_consistency ($1);
930 register_fields ($1, $2, $3);
931 }
932;
933
934variable_declarators:
935 /* Should we use build_decl_list () instead ? FIXME */
936 variable_declarator /* Default rule */
937| variable_declarators C_TK variable_declarator
938 { $$ = chainon ($1, $3); }
939| variable_declarators C_TK error
940 {yyerror ("Missing term"); RECOVER;}
941;
942
943variable_declarator:
944 variable_declarator_id
945 { $$ = build_tree_list ($1, NULL_TREE); }
946| variable_declarator_id ASSIGN_TK variable_initializer
947 {
948 if (java_error_count)
949 $3 = NULL_TREE;
950 $$ = build_tree_list
951 ($1, build_assignment ($2.token, $2.location, $1, $3));
952 }
953| variable_declarator_id ASSIGN_TK error
954 {
955 yyerror ("Missing variable initializer");
956 $$ = build_tree_list ($1, NULL_TREE);
957 RECOVER;
958 }
959| variable_declarator_id ASSIGN_TK variable_initializer error
960 {
961 yyerror ("';' expected");
962 $$ = build_tree_list ($1, NULL_TREE);
963 RECOVER;
964 }
965;
966
967variable_declarator_id:
968 identifier
969| variable_declarator_id OSB_TK CSB_TK
c583dd46 970 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
971| identifier error
972 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
973| variable_declarator_id OSB_TK error
974 {yyerror ("']' expected"); DRECOVER(vdi);}
975| variable_declarator_id CSB_TK error
976 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
977;
978
979variable_initializer:
980 expression
981| array_initializer
e04a16fb
AG
982;
983
984/* 19.8.3 Productions from 8.4: Method Declarations */
985method_declaration:
986 method_header
987 {
988 current_function_decl = $1;
c2952b01
APB
989 if (current_function_decl
990 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
991 source_start_java_method (current_function_decl);
992 else
993 current_function_decl = NULL_TREE;
e04a16fb
AG
994 }
995 method_body
b635eb2f 996 { finish_method_declaration ($3); }
e04a16fb
AG
997| method_header error
998 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
999;
1000
1001method_header:
1002 type method_declarator throws
b9f7e36c 1003 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 1004| VOID_TK method_declarator throws
b9f7e36c 1005 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 1006| modifiers type method_declarator throws
b9f7e36c 1007 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1008| modifiers VOID_TK method_declarator throws
b9f7e36c 1009 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1010| type error
efa0a23f
APB
1011 {
1012 yyerror ("Invalid method declaration, method name required");
1013 RECOVER;
1014 }
e04a16fb
AG
1015| modifiers type error
1016 {RECOVER;}
1017| VOID_TK error
1018 {yyerror ("Identifier expected"); RECOVER;}
1019| modifiers VOID_TK error
1020 {yyerror ("Identifier expected"); RECOVER;}
1021| modifiers error
1022 {
1023 yyerror ("Invalid method declaration, return type required");
1024 RECOVER;
1025 }
1026;
1027
1028method_declarator:
1029 identifier OP_TK CP_TK
c2952b01
APB
1030 {
1031 ctxp->formal_parameter_number = 0;
1032 $$ = method_declarator ($1, NULL_TREE);
1033 }
e04a16fb
AG
1034| identifier OP_TK formal_parameter_list CP_TK
1035 { $$ = method_declarator ($1, $3); }
1036| method_declarator OSB_TK CSB_TK
1037 {
1886c9d8
APB
1038 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1039 TREE_PURPOSE ($1) =
1040 build_unresolved_array_type (TREE_PURPOSE ($1));
1041 parse_warning_context
1042 (wfl_operator,
1043 "Discouraged form of returned type specification");
e04a16fb
AG
1044 }
1045| identifier OP_TK error
1046 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1047| method_declarator OSB_TK error
1048 {yyerror ("']' expected"); RECOVER;}
1049;
1050
1051formal_parameter_list:
1052 formal_parameter
1053 {
1054 ctxp->formal_parameter_number = 1;
1055 }
1056| formal_parameter_list C_TK formal_parameter
1057 {
1058 ctxp->formal_parameter_number += 1;
1059 $$ = chainon ($1, $3);
1060 }
1061| formal_parameter_list C_TK error
c2952b01 1062 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1063;
1064
1065formal_parameter:
1066 type variable_declarator_id
1067 {
1068 $$ = build_tree_list ($2, $1);
1069 }
18990de5 1070| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1071 {
5256aa37 1072 $$ = build_tree_list ($3, $2);
c2952b01 1073 ARG_FINAL_P ($$) = 1;
5256aa37 1074 }
e04a16fb 1075| type error
f8989a66
APB
1076 {
1077 yyerror ("Missing identifier"); RECOVER;
1078 $$ = NULL_TREE;
1079 }
18990de5 1080| final type error
e04a16fb 1081 {
e04a16fb 1082 yyerror ("Missing identifier"); RECOVER;
f8989a66 1083 $$ = NULL_TREE;
e04a16fb
AG
1084 }
1085;
1086
18990de5
JB
1087final:
1088 modifiers
1089 {
1090 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1091 $1, ACC_FINAL);
1092 if ($1 != ACC_FINAL)
1093 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1094 }
1095;
1096
e04a16fb 1097throws:
b9f7e36c 1098 { $$ = NULL_TREE; }
e04a16fb 1099| THROWS_TK class_type_list
b9f7e36c 1100 { $$ = $2; }
e04a16fb
AG
1101| THROWS_TK error
1102 {yyerror ("Missing class type term"); RECOVER;}
1103;
1104
1105class_type_list:
1106 class_type
c877974e 1107 { $$ = build_tree_list ($1, $1); }
e04a16fb 1108| class_type_list C_TK class_type
c877974e 1109 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1110| class_type_list C_TK error
1111 {yyerror ("Missing class type term"); RECOVER;}
1112;
1113
1114method_body:
1115 block
5f1c312a 1116| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1117;
1118
1119/* 19.8.4 Productions from 8.5: Static Initializers */
1120static_initializer:
1121 static block
1122 {
c2952b01
APB
1123 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1124 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb 1125 }
e04a16fb
AG
1126;
1127
1128static: /* Test lval.sub_token here */
c2952b01 1129 modifiers
e04a16fb 1130 {
c2952b01
APB
1131 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1132 /* Can't have a static initializer in an innerclass */
1133 if ($1 | ACC_STATIC &&
1134 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1135 parse_error_context
1136 (MODIFIER_WFL (STATIC_TK),
1137 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1138 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1139 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1140 }
1141;
1142
1143/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1144constructor_declaration:
22eed1e6 1145 constructor_header
e04a16fb 1146 {
22eed1e6
APB
1147 current_function_decl = $1;
1148 source_start_java_method (current_function_decl);
e04a16fb 1149 }
22eed1e6 1150 constructor_body
b635eb2f 1151 { finish_method_declaration ($3); }
22eed1e6
APB
1152;
1153
1154constructor_header:
1155 constructor_declarator throws
1156 { $$ = method_header (0, NULL_TREE, $1, $2); }
1157| modifiers constructor_declarator throws
1158 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1159;
1160
1161constructor_declarator:
1162 simple_name OP_TK CP_TK
c2952b01
APB
1163 {
1164 ctxp->formal_parameter_number = 0;
1165 $$ = method_declarator ($1, NULL_TREE);
1166 }
e04a16fb 1167| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1168 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1169;
1170
1171constructor_body:
22eed1e6
APB
1172 /* Unlike regular method, we always need a complete (empty)
1173 body so we can safely perform all the required code
1174 addition (super invocation and field initialization) */
2e5eb5c5 1175 block_begin constructor_block_end
22eed1e6 1176 {
9bbc7d9f 1177 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1178 $$ = $2;
1179 }
2e5eb5c5 1180| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1181 { $$ = $3; }
2e5eb5c5 1182| block_begin block_statements constructor_block_end
22eed1e6 1183 { $$ = $3; }
2e5eb5c5 1184| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1185 { $$ = $4; }
e04a16fb
AG
1186;
1187
2e5eb5c5
APB
1188constructor_block_end:
1189 block_end
5f1c312a 1190;
2e5eb5c5 1191
e04a16fb
AG
1192/* Error recovery for that rule moved down expression_statement: rule. */
1193explicit_constructor_invocation:
1194 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1195 {
1196 $$ = build_method_invocation ($1, NULL_TREE);
1197 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1198 $$ = java_method_add_stmt (current_function_decl, $$);
1199 }
e04a16fb 1200| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1201 {
1202 $$ = build_method_invocation ($1, $3);
1203 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1204 $$ = java_method_add_stmt (current_function_decl, $$);
1205 }
e04a16fb
AG
1206 /* Added, JDK1.1 inner classes. Modified because the rule
1207 'primary' couldn't work. */
1208| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1209 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1210| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1211 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1212;
1213
1214this_or_super: /* Added, simplifies error diagnostics */
1215 THIS_TK
1216 {
9ee9b555 1217 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1218 EXPR_WFL_LINECOL (wfl) = $1.location;
1219 $$ = wfl;
1220 }
1221| SUPER_TK
1222 {
9ee9b555 1223 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1224 EXPR_WFL_LINECOL (wfl) = $1.location;
1225 $$ = wfl;
1226 }
1227;
1228
1229/* 19.9 Productions from 9: Interfaces */
1230/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1231interface_declaration:
1232 INTERFACE_TK identifier
1233 { create_interface (0, $2, NULL_TREE); }
1234 interface_body
e04a16fb
AG
1235| modifiers INTERFACE_TK identifier
1236 { create_interface ($1, $3, NULL_TREE); }
1237 interface_body
e04a16fb
AG
1238| INTERFACE_TK identifier extends_interfaces
1239 { create_interface (0, $2, $3); }
1240 interface_body
e04a16fb
AG
1241| modifiers INTERFACE_TK identifier extends_interfaces
1242 { create_interface ($1, $3, $4); }
1243 interface_body
e04a16fb 1244| INTERFACE_TK identifier error
0b4d333e 1245 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1246| modifiers INTERFACE_TK identifier error
0b4d333e 1247 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1248;
1249
1250extends_interfaces:
1251 EXTENDS_TK interface_type
1252 {
1253 ctxp->interface_number = 1;
1254 $$ = build_tree_list ($2, NULL_TREE);
1255 }
1256| extends_interfaces C_TK interface_type
1257 {
1258 ctxp->interface_number++;
1259 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1260 }
1261| EXTENDS_TK error
1262 {yyerror ("Invalid interface type"); RECOVER;}
1263| extends_interfaces C_TK error
1264 {yyerror ("Missing term"); RECOVER;}
1265;
1266
1267interface_body:
1268 OCB_TK CCB_TK
1269 { $$ = NULL_TREE; }
1270| OCB_TK interface_member_declarations CCB_TK
1271 { $$ = NULL_TREE; }
1272;
1273
1274interface_member_declarations:
1275 interface_member_declaration
1276| interface_member_declarations interface_member_declaration
1277;
1278
1279interface_member_declaration:
1280 constant_declaration
1281| abstract_method_declaration
1282| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1283 { end_class_declaration (1); }
1284| interface_declaration /* Added, JDK1.1 inner interfaces */
1285 { end_class_declaration (1); }
e04a16fb
AG
1286;
1287
1288constant_declaration:
1289 field_declaration
1290;
1291
1292abstract_method_declaration:
1293 method_header SC_TK
1294 {
1295 check_abstract_method_header ($1);
1296 current_function_decl = NULL_TREE; /* FIXME ? */
1297 }
1298| method_header error
1299 {yyerror ("';' expected"); RECOVER;}
1300;
1301
1302/* 19.10 Productions from 10: Arrays */
1303array_initializer:
1304 OCB_TK CCB_TK
1179ebc2 1305 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1306| OCB_TK variable_initializers CCB_TK
f8976021 1307 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1308| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1309 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1310;
1311
1312variable_initializers:
1313 variable_initializer
f8976021
APB
1314 {
1315 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1316 $1, NULL_TREE);
1317 }
e04a16fb 1318| variable_initializers C_TK variable_initializer
1179ebc2
APB
1319 {
1320 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1321 }
e04a16fb
AG
1322| variable_initializers C_TK error
1323 {yyerror ("Missing term"); RECOVER;}
1324;
1325
1326/* 19.11 Production from 14: Blocks and Statements */
1327block:
1328 OCB_TK CCB_TK
7f10c2e2
APB
1329 {
1330 /* Store the location of the `}' when doing xrefs */
1331 if (current_function_decl && flag_emit_xref)
1332 DECL_END_SOURCE_LINE (current_function_decl) =
1333 EXPR_WFL_ADD_COL ($2.location, 1);
1334 $$ = empty_stmt_node;
1335 }
22eed1e6
APB
1336| block_begin block_statements block_end
1337 { $$ = $3; }
1338;
1339
1340block_begin:
1341 OCB_TK
e04a16fb 1342 { enter_block (); }
22eed1e6
APB
1343;
1344
1345block_end:
e04a16fb
AG
1346 CCB_TK
1347 {
1348 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1349 /* Store the location of the `}' when doing xrefs */
1350 if (current_function_decl && flag_emit_xref)
1351 DECL_END_SOURCE_LINE (current_function_decl) =
1352 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1353 $$ = exit_block ();
c280e37a
APB
1354 if (!BLOCK_SUBBLOCKS ($$))
1355 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1356 }
1357;
1358
1359block_statements:
1360 block_statement
1361| block_statements block_statement
1362;
1363
1364block_statement:
1365 local_variable_declaration_statement
1366| statement
15fdcfe9 1367 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1368| class_declaration /* Added, JDK1.1 local classes */
1369 {
1370 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1371 end_class_declaration (1);
1372 }
e04a16fb
AG
1373;
1374
1375local_variable_declaration_statement:
1376 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1377;
1378
1379local_variable_declaration:
1380 type variable_declarators
1381 { declare_local_variables (0, $1, $2); }
a003f638 1382| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1383 { declare_local_variables ($1, $2, $3); }
1384;
1385
1386statement:
1387 statement_without_trailing_substatement
1388| labeled_statement
e04a16fb 1389| if_then_statement
e04a16fb 1390| if_then_else_statement
e04a16fb 1391| while_statement
e04a16fb 1392| for_statement
cd9643f7 1393 { $$ = exit_block (); }
e04a16fb
AG
1394;
1395
1396statement_nsi:
1397 statement_without_trailing_substatement
1398| labeled_statement_nsi
e04a16fb 1399| if_then_else_statement_nsi
e04a16fb 1400| while_statement_nsi
e04a16fb 1401| for_statement_nsi
9dd939b2 1402 { $$ = exit_block (); }
e04a16fb
AG
1403;
1404
1405statement_without_trailing_substatement:
1406 block
e04a16fb 1407| empty_statement
e04a16fb 1408| expression_statement
e04a16fb 1409| switch_statement
e04a16fb 1410| do_statement
e04a16fb 1411| break_statement
e04a16fb 1412| continue_statement
e04a16fb
AG
1413| return_statement
1414| synchronized_statement
e04a16fb 1415| throw_statement
e04a16fb 1416| try_statement
e04a16fb
AG
1417;
1418
1419empty_statement:
1420 SC_TK
5f1c312a
APB
1421 {
1422 if (flag_extraneous_semicolon)
1423 {
1424 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1425 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1426 }
1427 $$ = empty_stmt_node;
1428 }
e04a16fb
AG
1429;
1430
1431label_decl:
1432 identifier REL_CL_TK
1433 {
1434 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1435 EXPR_WFL_NODE ($1));
e04a16fb
AG
1436 pushlevel (2);
1437 push_labeled_block ($$);
1438 PUSH_LABELED_BLOCK ($$);
1439 }
1440;
1441
1442labeled_statement:
1443 label_decl statement
b635eb2f 1444 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1445| identifier error
1446 {yyerror ("':' expected"); RECOVER;}
1447;
1448
1449labeled_statement_nsi:
1450 label_decl statement_nsi
b635eb2f 1451 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1452;
1453
1454/* We concentrate here a bunch of error handling rules that we couldn't write
1455 earlier, because expression_statement catches a missing ';'. */
1456expression_statement:
1457 statement_expression SC_TK
1458 {
1459 /* We have a statement. Generate a WFL around it so
1460 we can debug it */
1461 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1462 /* We know we have a statement, so set the debug
1463 info to be eventually generate here. */
1464 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1465 }
1466| error SC_TK
1467 {
1468 if (ctxp->prevent_ese != lineno)
1469 yyerror ("Invalid expression statement");
1470 DRECOVER (expr_stmt);
1471 }
1472| error OCB_TK
1473 {
1474 if (ctxp->prevent_ese != lineno)
1475 yyerror ("Invalid expression statement");
1476 DRECOVER (expr_stmt);
1477 }
1478| error CCB_TK
1479 {
1480 if (ctxp->prevent_ese != lineno)
1481 yyerror ("Invalid expression statement");
1482 DRECOVER (expr_stmt);
1483 }
1484| this_or_super OP_TK error
1485 {yyerror ("')' expected"); RECOVER;}
1486| this_or_super OP_TK CP_TK error
22eed1e6 1487 {
8119c720 1488 parse_ctor_invocation_error ();
22eed1e6
APB
1489 RECOVER;
1490 }
e04a16fb
AG
1491| this_or_super OP_TK argument_list error
1492 {yyerror ("')' expected"); RECOVER;}
1493| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1494 {
8119c720 1495 parse_ctor_invocation_error ();
22eed1e6
APB
1496 RECOVER;
1497 }
e04a16fb
AG
1498| name DOT_TK SUPER_TK error
1499 {yyerror ("'(' expected"); RECOVER;}
1500| name DOT_TK SUPER_TK OP_TK error
1501 {yyerror ("')' expected"); RECOVER;}
1502| name DOT_TK SUPER_TK OP_TK argument_list error
1503 {yyerror ("')' expected"); RECOVER;}
1504| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1505 {yyerror ("';' expected"); RECOVER;}
1506| name DOT_TK SUPER_TK OP_TK CP_TK error
1507 {yyerror ("';' expected"); RECOVER;}
1508;
1509
1510statement_expression:
1511 assignment
1512| pre_increment_expression
e04a16fb 1513| pre_decrement_expression
e04a16fb 1514| post_increment_expression
e04a16fb 1515| post_decrement_expression
e04a16fb
AG
1516| method_invocation
1517| class_instance_creation_expression
e04a16fb
AG
1518;
1519
1520if_then_statement:
1521 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1522 {
1523 $$ = build_if_else_statement ($2.location, $3,
1524 $5, NULL_TREE);
1525 }
e04a16fb
AG
1526| IF_TK error
1527 {yyerror ("'(' expected"); RECOVER;}
1528| IF_TK OP_TK error
1529 {yyerror ("Missing term"); RECOVER;}
1530| IF_TK OP_TK expression error
1531 {yyerror ("')' expected"); RECOVER;}
1532;
1533
1534if_then_else_statement:
1535 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1536 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1537;
1538
1539if_then_else_statement_nsi:
1540 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1541 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1542;
1543
1544switch_statement:
15fdcfe9
PB
1545 switch_expression
1546 {
1547 enter_block ();
1548 }
1549 switch_block
b67d701b 1550 {
15fdcfe9 1551 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1552 I.e. make the last statment also have its own
1553 COMPOUND_EXPR. */
15fdcfe9
PB
1554 maybe_absorb_scoping_blocks ();
1555 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1556 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1557 }
1558;
1559
1560switch_expression:
1561 SWITCH_TK OP_TK expression CP_TK
1562 {
1563 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1564 EXPR_WFL_LINECOL ($$) = $2.location;
1565 }
e04a16fb
AG
1566| SWITCH_TK error
1567 {yyerror ("'(' expected"); RECOVER;}
1568| SWITCH_TK OP_TK error
1569 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1570| SWITCH_TK OP_TK expression CP_TK error
1571 {yyerror ("'{' expected"); RECOVER;}
1572;
1573
f8976021
APB
1574/* Default assignment is there to avoid type node on switch_block
1575 node. */
1576
e04a16fb
AG
1577switch_block:
1578 OCB_TK CCB_TK
f8976021 1579 { $$ = NULL_TREE; }
e04a16fb 1580| OCB_TK switch_labels CCB_TK
f8976021 1581 { $$ = NULL_TREE; }
e04a16fb 1582| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1583 { $$ = NULL_TREE; }
e04a16fb 1584| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1585 { $$ = NULL_TREE; }
e04a16fb
AG
1586;
1587
1588switch_block_statement_groups:
1589 switch_block_statement_group
1590| switch_block_statement_groups switch_block_statement_group
1591;
1592
1593switch_block_statement_group:
15fdcfe9 1594 switch_labels block_statements
e04a16fb
AG
1595;
1596
e04a16fb
AG
1597switch_labels:
1598 switch_label
1599| switch_labels switch_label
1600;
1601
1602switch_label:
1603 CASE_TK constant_expression REL_CL_TK
b67d701b 1604 {
15fdcfe9
PB
1605 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1606 EXPR_WFL_LINECOL (lab) = $1.location;
1607 java_method_add_stmt (current_function_decl, lab);
b67d701b 1608 }
e04a16fb 1609| DEFAULT_TK REL_CL_TK
b67d701b 1610 {
15fdcfe9
PB
1611 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1612 EXPR_WFL_LINECOL (lab) = $1.location;
1613 java_method_add_stmt (current_function_decl, lab);
b67d701b 1614 }
e04a16fb
AG
1615| CASE_TK error
1616 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1617| CASE_TK constant_expression error
1618 {yyerror ("':' expected"); RECOVER;}
1619| DEFAULT_TK error
1620 {yyerror ("':' expected"); RECOVER;}
1621;
1622
1623while_expression:
1624 WHILE_TK OP_TK expression CP_TK
1625 {
1626 tree body = build_loop_body ($2.location, $3, 0);
1627 $$ = build_new_loop (body);
1628 }
1629;
1630
1631while_statement:
1632 while_expression statement
b635eb2f 1633 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1634| WHILE_TK error
1635 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1636| WHILE_TK OP_TK error
1637 {yyerror ("Missing term and ')' expected"); RECOVER;}
1638| WHILE_TK OP_TK expression error
1639 {yyerror ("')' expected"); RECOVER;}
1640;
1641
1642while_statement_nsi:
1643 while_expression statement_nsi
b635eb2f 1644 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1645;
1646
1647do_statement_begin:
1648 DO_TK
1649 {
1650 tree body = build_loop_body (0, NULL_TREE, 1);
1651 $$ = build_new_loop (body);
1652 }
1653 /* Need error handing here. FIXME */
1654;
1655
1656do_statement:
1657 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1658 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1659;
1660
1661for_statement:
1662 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1663 {
1664 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1665 $3 = build_wfl_node ($3);
1666 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1667 }
e04a16fb
AG
1668| for_begin SC_TK SC_TK for_update CP_TK statement
1669 {
b635eb2f 1670 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1671 /* We have not condition, so we get rid of the EXIT_EXPR */
1672 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1673 empty_stmt_node;
e04a16fb
AG
1674 }
1675| for_begin SC_TK error
1676 {yyerror ("Invalid control expression"); RECOVER;}
1677| for_begin SC_TK expression SC_TK error
1678 {yyerror ("Invalid update expression"); RECOVER;}
1679| for_begin SC_TK SC_TK error
1680 {yyerror ("Invalid update expression"); RECOVER;}
1681;
1682
1683for_statement_nsi:
1684 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1685 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1686| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1687 {
b635eb2f 1688 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1689 /* We have not condition, so we get rid of the EXIT_EXPR */
1690 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1691 empty_stmt_node;
e04a16fb
AG
1692 }
1693;
1694
1695for_header:
1696 FOR_TK OP_TK
1697 {
1698 /* This scope defined for local variable that may be
1699 defined within the scope of the for loop */
1700 enter_block ();
1701 }
1702| FOR_TK error
1703 {yyerror ("'(' expected"); DRECOVER(for_1);}
1704| FOR_TK OP_TK error
1705 {yyerror ("Invalid init statement"); RECOVER;}
1706;
1707
1708for_begin:
1709 for_header for_init
1710 {
1711 /* We now declare the loop body. The loop is
1712 declared as a for loop. */
1713 tree body = build_loop_body (0, NULL_TREE, 0);
1714 $$ = build_new_loop (body);
c2952b01 1715 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1716 /* The loop is added to the current block the for
1717 statement is defined within */
1718 java_method_add_stmt (current_function_decl, $$);
1719 }
1720;
1721for_init: /* Can be empty */
9bbc7d9f 1722 { $$ = empty_stmt_node; }
e04a16fb
AG
1723| statement_expression_list
1724 {
1725 /* Init statement recorded within the previously
1726 defined block scope */
1727 $$ = java_method_add_stmt (current_function_decl, $1);
1728 }
1729| local_variable_declaration
1730 {
1731 /* Local variable are recorded within the previously
1732 defined block scope */
1733 $$ = NULL_TREE;
1734 }
1735| statement_expression_list error
1736 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1737;
1738
1739for_update: /* Can be empty */
9bbc7d9f 1740 {$$ = empty_stmt_node;}
e04a16fb
AG
1741| statement_expression_list
1742 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1743;
1744
1745statement_expression_list:
1746 statement_expression
1747 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1748| statement_expression_list C_TK statement_expression
1749 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1750| statement_expression_list C_TK error
1751 {yyerror ("Missing term"); RECOVER;}
1752;
1753
1754break_statement:
1755 BREAK_TK SC_TK
1756 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1757| BREAK_TK identifier SC_TK
1758 { $$ = build_bc_statement ($1.location, 1, $2); }
1759| BREAK_TK error
1760 {yyerror ("Missing term"); RECOVER;}
1761| BREAK_TK identifier error
1762 {yyerror ("';' expected"); RECOVER;}
1763;
1764
1765continue_statement:
1766 CONTINUE_TK SC_TK
1767 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1768| CONTINUE_TK identifier SC_TK
1769 { $$ = build_bc_statement ($1.location, 0, $2); }
1770| CONTINUE_TK error
1771 {yyerror ("Missing term"); RECOVER;}
1772| CONTINUE_TK identifier error
1773 {yyerror ("';' expected"); RECOVER;}
1774;
1775
1776return_statement:
1777 RETURN_TK SC_TK
1778 { $$ = build_return ($1.location, NULL_TREE); }
1779| RETURN_TK expression SC_TK
1780 { $$ = build_return ($1.location, $2); }
1781| RETURN_TK error
1782 {yyerror ("Missing term"); RECOVER;}
1783| RETURN_TK expression error
1784 {yyerror ("';' expected"); RECOVER;}
1785;
1786
1787throw_statement:
1788 THROW_TK expression SC_TK
b9f7e36c
APB
1789 {
1790 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1791 EXPR_WFL_LINECOL ($$) = $1.location;
1792 }
e04a16fb
AG
1793| THROW_TK error
1794 {yyerror ("Missing term"); RECOVER;}
1795| THROW_TK expression error
1796 {yyerror ("';' expected"); RECOVER;}
1797;
1798
1799synchronized_statement:
1800 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1801 {
1802 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1803 EXPR_WFL_LINECOL ($$) =
1804 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1805 }
e04a16fb
AG
1806| synchronized OP_TK expression CP_TK error
1807 {yyerror ("'{' expected"); RECOVER;}
1808| synchronized error
1809 {yyerror ("'(' expected"); RECOVER;}
1810| synchronized OP_TK error CP_TK
1811 {yyerror ("Missing term"); RECOVER;}
1812| synchronized OP_TK error
1813 {yyerror ("Missing term"); RECOVER;}
1814;
1815
b9f7e36c 1816synchronized:
efa0a23f 1817 modifiers
e04a16fb 1818 {
781b0558
KG
1819 check_modifiers (
1820 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1821 $1, ACC_SYNCHRONIZED);
1822 if ($1 != ACC_SYNCHRONIZED)
1823 MODIFIER_WFL (SYNCHRONIZED_TK) =
1824 build_wfl_node (NULL_TREE);
e04a16fb
AG
1825 }
1826;
1827
1828try_statement:
1829 TRY_TK block catches
a7d8d81f 1830 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1831| TRY_TK block finally
a7d8d81f 1832 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1833| TRY_TK block catches finally
2aa11e97
APB
1834 { $$ = build_try_finally_statement
1835 ($1.location, build_try_statement ($1.location,
1836 $2, $3), $4);
1837 }
e04a16fb
AG
1838| TRY_TK error
1839 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1840;
1841
1842catches:
1843 catch_clause
1844| catches catch_clause
b67d701b
PB
1845 {
1846 TREE_CHAIN ($2) = $1;
1847 $$ = $2;
1848 }
e04a16fb
AG
1849;
1850
1851catch_clause:
b67d701b
PB
1852 catch_clause_parameter block
1853 {
1854 java_method_add_stmt (current_function_decl, $2);
1855 exit_block ();
1856 $$ = $1;
1857 }
1858
1859catch_clause_parameter:
1860 CATCH_TK OP_TK formal_parameter CP_TK
1861 {
1862 /* We add a block to define a scope for
1863 formal_parameter (CCBP). The formal parameter is
1864 declared initialized by the appropriate function
1865 call */
1866 tree ccpb = enter_block ();
1867 tree init = build_assignment (ASSIGN_TK, $2.location,
1868 TREE_PURPOSE ($3),
1869 soft_exceptioninfo_call_node);
1870 declare_local_variables (0, TREE_VALUE ($3),
1871 build_tree_list (TREE_PURPOSE ($3),
1872 init));
1873 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1874 EXPR_WFL_LINECOL ($$) = $1.location;
1875 }
e04a16fb 1876| CATCH_TK error
97f30284 1877 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1878| CATCH_TK OP_TK error
97f30284
APB
1879 {
1880 yyerror ("Missing term or ')' expected");
1881 RECOVER; $$ = NULL_TREE;
1882 }
b67d701b 1883| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1884 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1885;
1886
1887finally:
1888 FINALLY_TK block
a7d8d81f 1889 { $$ = $2; }
e04a16fb
AG
1890| FINALLY_TK error
1891 {yyerror ("'{' expected"); RECOVER; }
1892;
1893
1894/* 19.12 Production from 15: Expressions */
1895primary:
1896 primary_no_new_array
1897| array_creation_expression
1898;
1899
1900primary_no_new_array:
1901 literal
1902| THIS_TK
1903 { $$ = build_this ($1.location); }
1904| OP_TK expression CP_TK
1905 {$$ = $2;}
1906| class_instance_creation_expression
1907| field_access
1908| method_invocation
1909| array_access
c2952b01 1910| type_literals
e04a16fb
AG
1911 /* Added, JDK1.1 inner classes. Documentation is wrong
1912 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1913 exist. Used name: instead. */
e04a16fb 1914| name DOT_TK THIS_TK
c2952b01
APB
1915 {
1916 tree wfl = build_wfl_node (this_identifier_node);
1917 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1918 }
e04a16fb
AG
1919| OP_TK expression error
1920 {yyerror ("')' expected"); RECOVER;}
1921| name DOT_TK error
1922 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1923| primitive_type DOT_TK error
1924 {yyerror ("'class' expected" ); RECOVER;}
1925| VOID_TK DOT_TK error
1926 {yyerror ("'class' expected" ); RECOVER;}
1927;
1928
c2952b01
APB
1929/* Added, JDK1.1 type literals. We can't use `type' directly, so we
1930 broke the rule down a bit. */
1931
1932array_type_literal:
1933 primitive_type OSB_TK CSB_TK
1934 {
1935 $$ = build_java_array_type ($1, -1);
1936 CLASS_LOADED_P ($$) = 1;
1937 }
1938| name OSB_TK CSB_TK
1939 { $$ = build_unresolved_array_type ($1); }
1940/* This triggers two reduce/reduce conflict between array_type_literal and
1941 dims. FIXME.
1942| array_type OSB_TK CSB_TK
1943 { $$ = build_unresolved_array_type ($1); }
1944*/
1945;
1946
1947type_literals:
1948 name DOT_TK CLASS_TK
1949 { $$ = build_incomplete_class_ref ($2.location, $1); }
1950| array_type_literal DOT_TK CLASS_TK
1951 { $$ = build_incomplete_class_ref ($2.location, $1); }
1952| primitive_type DOT_TK CLASS_TK
1953 { $$ = build_class_ref ($1); }
1954| VOID_TK DOT_TK CLASS_TK
1955 { $$ = build_class_ref (void_type_node); }
1956;
1957
e04a16fb
AG
1958class_instance_creation_expression:
1959 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1960 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1961| NEW_TK class_type OP_TK CP_TK
b67d701b 1962 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1963| anonymous_class_creation
e04a16fb
AG
1964 /* Added, JDK1.1 inner classes, modified to use name or
1965 primary instead of primary solely which couldn't work in
1966 all situations. */
1967| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1968 {
1969 tree ctor = build_new_invocation ($2, NULL_TREE);
1970 $$ = make_qualified_primary ($1, ctor,
1971 EXPR_WFL_LINECOL ($1));
1972 }
e04a16fb
AG
1973| something_dot_new identifier OP_TK CP_TK class_body
1974| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1975 {
1976 tree ctor = build_new_invocation ($2, $4);
1977 $$ = make_qualified_primary ($1, ctor,
1978 EXPR_WFL_LINECOL ($1));
1979 }
e04a16fb
AG
1980| something_dot_new identifier OP_TK argument_list CP_TK class_body
1981| NEW_TK error SC_TK
1982 {yyerror ("'(' expected"); DRECOVER(new_1);}
1983| NEW_TK class_type error
1984 {yyerror ("'(' expected"); RECOVER;}
1985| NEW_TK class_type OP_TK error
1986 {yyerror ("')' or term expected"); RECOVER;}
1987| NEW_TK class_type OP_TK argument_list error
1988 {yyerror ("')' expected"); RECOVER;}
1989| something_dot_new error
1990 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1991| something_dot_new identifier error
1992 {yyerror ("'(' expected"); RECOVER;}
1993;
1994
c2952b01
APB
1995/* Created after JDK1.1 rules originally added to
1996 class_instance_creation_expression, but modified to use
1997 'class_type' instead of 'TypeName' (type_name) which is mentionned
1998 in the documentation but doesn't exist. */
1999
2000anonymous_class_creation:
2001 NEW_TK class_type OP_TK argument_list CP_TK
2002 { create_anonymous_class ($1.location, $2); }
2003 class_body
2004 {
2005 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2006 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2007
2008 end_class_declaration (1);
2009
2010 /* Now we can craft the new expression */
2011 $$ = build_new_invocation (id, $4);
2012
2013 /* Note that we can't possibly be here if
2014 `class_type' is an interface (in which case the
2015 anonymous class extends Object and implements
2016 `class_type', hence its constructor can't have
2017 arguments.) */
2018
2019 /* Otherwise, the innerclass must feature a
2020 constructor matching `argument_list'. Anonymous
2021 classes are a bit special: it's impossible to
2022 define constructor for them, hence constructors
2023 must be generated following the hints provided by
2024 the `new' expression. Whether a super constructor
2025 of that nature exists or not is to be verified
2026 later on in verify_constructor_super.
2027
2028 It's during the expansion of a `new' statement
2029 refering to an anonymous class that a ctor will
2030 be generated for the anonymous class, with the
2031 right arguments. */
2032
2033 }
2034| NEW_TK class_type OP_TK CP_TK
2035 { create_anonymous_class ($1.location, $2); }
2036 class_body
2037 {
2038 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2039 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2040
2041 end_class_declaration (1);
2042
2043 /* Now we can craft the new expression. The
2044 statement doesn't need to be remember so that a
2045 constructor can be generated, since its signature
2046 is already known. */
2047 $$ = build_new_invocation (id, NULL_TREE);
2048 }
2049;
2050
e04a16fb
AG
2051something_dot_new: /* Added, not part of the specs. */
2052 name DOT_TK NEW_TK
c2952b01 2053 { $$ = $1; }
e04a16fb 2054| primary DOT_TK NEW_TK
c2952b01 2055 { $$ = $1; }
e04a16fb
AG
2056;
2057
2058argument_list:
2059 expression
2060 {
2061 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2062 ctxp->formal_parameter_number = 1;
2063 }
2064| argument_list C_TK expression
2065 {
2066 ctxp->formal_parameter_number += 1;
2067 $$ = tree_cons (NULL_TREE, $3, $1);
2068 }
2069| argument_list C_TK error
2070 {yyerror ("Missing term"); RECOVER;}
2071;
2072
2073array_creation_expression:
2074 NEW_TK primitive_type dim_exprs
2075 { $$ = build_newarray_node ($2, $3, 0); }
2076| NEW_TK class_or_interface_type dim_exprs
2077 { $$ = build_newarray_node ($2, $3, 0); }
2078| NEW_TK primitive_type dim_exprs dims
ba179f9f 2079 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb 2080| NEW_TK class_or_interface_type dim_exprs dims
ba179f9f 2081 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb
AG
2082 /* Added, JDK1.1 anonymous array. Initial documentation rule
2083 modified */
2084| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2085 {
2086 char *sig;
2087 while (CURRENT_OSB (ctxp)--)
2088 obstack_1grow (&temporary_obstack, '[');
2089 sig = obstack_finish (&temporary_obstack);
2090 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2091 $2, get_identifier (sig), $4);
2092 }
e04a16fb 2093| NEW_TK primitive_type dims array_initializer
c2952b01
APB
2094 {
2095 tree type = $2;
2096 while (CURRENT_OSB (ctxp)--)
2097 type = build_java_array_type (type, -1);
2098 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2099 build_pointer_type (type), NULL_TREE, $4);
2100 }
e04a16fb
AG
2101| NEW_TK error CSB_TK
2102 {yyerror ("'[' expected"); DRECOVER ("]");}
2103| NEW_TK error OSB_TK
2104 {yyerror ("']' expected"); RECOVER;}
2105;
2106
2107dim_exprs:
2108 dim_expr
2109 { $$ = build_tree_list (NULL_TREE, $1); }
2110| dim_exprs dim_expr
2111 { $$ = tree_cons (NULL_TREE, $2, $$); }
2112;
2113
2114dim_expr:
2115 OSB_TK expression CSB_TK
2116 {
9a7ab4b3
APB
2117 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2118 {
2119 $2 = build_wfl_node ($2);
2120 TREE_TYPE ($2) = NULL_TREE;
2121 }
e04a16fb
AG
2122 EXPR_WFL_LINECOL ($2) = $1.location;
2123 $$ = $2;
2124 }
2125| OSB_TK expression error
2126 {yyerror ("']' expected"); RECOVER;}
2127| OSB_TK error
2128 {
2129 yyerror ("Missing term");
2130 yyerror ("']' expected");
2131 RECOVER;
2132 }
2133;
2134
2135dims:
2136 OSB_TK CSB_TK
ba179f9f
APB
2137 {
2138 int allocate = 0;
2139 /* If not initialized, allocate memory for the osb
2140 numbers stack */
2141 if (!ctxp->osb_limit)
2142 {
2143 allocate = ctxp->osb_limit = 32;
2144 ctxp->osb_depth = -1;
2145 }
c2952b01 2146 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2147 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2148 allocate = ctxp->osb_limit << 1;
2149
2150 if (allocate)
2151 {
2152 allocate *= sizeof (int);
2153 if (ctxp->osb_number)
2154 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2155 allocate);
2156 else
2157 ctxp->osb_number = (int *)xmalloc (allocate);
2158 }
2159 ctxp->osb_depth++;
2160 CURRENT_OSB (ctxp) = 1;
2161 }
e04a16fb 2162| dims OSB_TK CSB_TK
ba179f9f 2163 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2164| dims OSB_TK error
2165 { yyerror ("']' expected"); RECOVER;}
2166;
2167
2168field_access:
2169 primary DOT_TK identifier
2170 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2171 /* FIXME - REWRITE TO:
2172 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2173| SUPER_TK DOT_TK identifier
2174 {
2175 tree super_wfl =
9ee9b555 2176 build_wfl_node (super_identifier_node);
e04a16fb
AG
2177 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2178 $$ = make_qualified_name (super_wfl, $3, $2.location);
2179 }
2180| SUPER_TK error
2181 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2182;
2183
2184method_invocation:
2185 name OP_TK CP_TK
2186 { $$ = build_method_invocation ($1, NULL_TREE); }
2187| name OP_TK argument_list CP_TK
2188 { $$ = build_method_invocation ($1, $3); }
2189| primary DOT_TK identifier OP_TK CP_TK
2190 {
22eed1e6
APB
2191 if (TREE_CODE ($1) == THIS_EXPR)
2192 $$ = build_this_super_qualified_invocation
2193 (1, $3, NULL_TREE, 0, $2.location);
2194 else
2195 {
2196 tree invok = build_method_invocation ($3, NULL_TREE);
2197 $$ = make_qualified_primary ($1, invok, $2.location);
2198 }
e04a16fb
AG
2199 }
2200| primary DOT_TK identifier OP_TK argument_list CP_TK
2201 {
22eed1e6
APB
2202 if (TREE_CODE ($1) == THIS_EXPR)
2203 $$ = build_this_super_qualified_invocation
2204 (1, $3, $5, 0, $2.location);
2205 else
2206 {
2207 tree invok = build_method_invocation ($3, $5);
2208 $$ = make_qualified_primary ($1, invok, $2.location);
2209 }
e04a16fb
AG
2210 }
2211| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2212 {
2213 $$ = build_this_super_qualified_invocation
2214 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2215 }
2216| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2217 {
22eed1e6
APB
2218 $$ = build_this_super_qualified_invocation
2219 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2220 }
2221 /* Screws up thing. I let it here until I'm convinced it can
2222 be removed. FIXME
2223| primary DOT_TK error
2224 {yyerror ("'(' expected"); DRECOVER(bad);} */
2225| SUPER_TK DOT_TK error CP_TK
2226 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2227| SUPER_TK DOT_TK error DOT_TK
2228 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2229;
2230
2231array_access:
2232 name OSB_TK expression CSB_TK
2233 { $$ = build_array_ref ($2.location, $1, $3); }
2234| primary_no_new_array OSB_TK expression CSB_TK
2235 { $$ = build_array_ref ($2.location, $1, $3); }
2236| name OSB_TK error
2237 {
2238 yyerror ("Missing term and ']' expected");
2239 DRECOVER(array_access);
2240 }
2241| name OSB_TK expression error
2242 {
2243 yyerror ("']' expected");
2244 DRECOVER(array_access);
2245 }
2246| primary_no_new_array OSB_TK error
2247 {
2248 yyerror ("Missing term and ']' expected");
2249 DRECOVER(array_access);
2250 }
2251| primary_no_new_array OSB_TK expression error
2252 {
2253 yyerror ("']' expected");
2254 DRECOVER(array_access);
2255 }
2256;
2257
2258postfix_expression:
2259 primary
2260| name
2261| post_increment_expression
2262| post_decrement_expression
2263;
2264
2265post_increment_expression:
2266 postfix_expression INCR_TK
2267 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2268;
2269
2270post_decrement_expression:
2271 postfix_expression DECR_TK
2272 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2273;
2274
2275unary_expression:
2276 pre_increment_expression
2277| pre_decrement_expression
2278| PLUS_TK unary_expression
2279 {$$ = build_unaryop ($1.token, $1.location, $2); }
2280| MINUS_TK unary_expression
2281 {$$ = build_unaryop ($1.token, $1.location, $2); }
2282| unary_expression_not_plus_minus
2283| PLUS_TK error
2284 {yyerror ("Missing term"); RECOVER}
2285| MINUS_TK error
2286 {yyerror ("Missing term"); RECOVER}
2287;
2288
2289pre_increment_expression:
2290 INCR_TK unary_expression
2291 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2292| INCR_TK error
2293 {yyerror ("Missing term"); RECOVER}
2294;
2295
2296pre_decrement_expression:
2297 DECR_TK unary_expression
2298 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2299| DECR_TK error
2300 {yyerror ("Missing term"); RECOVER}
2301;
2302
2303unary_expression_not_plus_minus:
2304 postfix_expression
2305| NOT_TK unary_expression
2306 {$$ = build_unaryop ($1.token, $1.location, $2); }
2307| NEG_TK unary_expression
2308 {$$ = build_unaryop ($1.token, $1.location, $2); }
2309| cast_expression
2310| NOT_TK error
2311 {yyerror ("Missing term"); RECOVER}
2312| NEG_TK error
2313 {yyerror ("Missing term"); RECOVER}
2314;
2315
2316cast_expression: /* Error handling here is potentially weak */
2317 OP_TK primitive_type dims CP_TK unary_expression
2318 {
2319 tree type = $2;
ba179f9f 2320 while (CURRENT_OSB (ctxp)--)
e04a16fb 2321 type = build_java_array_type (type, -1);
ba179f9f 2322 ctxp->osb_depth--;
e04a16fb
AG
2323 $$ = build_cast ($1.location, type, $5);
2324 }
2325| OP_TK primitive_type CP_TK unary_expression
2326 { $$ = build_cast ($1.location, $2, $4); }
2327| OP_TK expression CP_TK unary_expression_not_plus_minus
2328 { $$ = build_cast ($1.location, $2, $4); }
2329| OP_TK name dims CP_TK unary_expression_not_plus_minus
2330 {
49f48c71 2331 const char *ptr;
ba179f9f 2332 while (CURRENT_OSB (ctxp)--)
e04a16fb 2333 obstack_1grow (&temporary_obstack, '[');
ba179f9f 2334 ctxp->osb_depth--;
e04a16fb
AG
2335 obstack_grow0 (&temporary_obstack,
2336 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2337 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2338 ptr = obstack_finish (&temporary_obstack);
2339 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2340 $$ = build_cast ($1.location, $2, $5);
2341 }
2342| OP_TK primitive_type OSB_TK error
2343 {yyerror ("']' expected, invalid type expression");}
2344| OP_TK error
2345 {
2346 if (ctxp->prevent_ese != lineno)
2347 yyerror ("Invalid type expression"); RECOVER;
2348 RECOVER;
2349 }
2350| OP_TK primitive_type dims CP_TK error
2351 {yyerror ("Missing term"); RECOVER;}
2352| OP_TK primitive_type CP_TK error
2353 {yyerror ("Missing term"); RECOVER;}
2354| OP_TK name dims CP_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356;
2357
2358multiplicative_expression:
2359 unary_expression
2360| multiplicative_expression MULT_TK unary_expression
2361 {
2362 $$ = build_binop (BINOP_LOOKUP ($2.token),
2363 $2.location, $1, $3);
2364 }
2365| multiplicative_expression DIV_TK unary_expression
2366 {
2367 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2368 $1, $3);
2369 }
2370| multiplicative_expression REM_TK unary_expression
2371 {
2372 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2373 $1, $3);
2374 }
2375| multiplicative_expression MULT_TK error
2376 {yyerror ("Missing term"); RECOVER;}
2377| multiplicative_expression DIV_TK error
2378 {yyerror ("Missing term"); RECOVER;}
2379| multiplicative_expression REM_TK error
2380 {yyerror ("Missing term"); RECOVER;}
2381;
2382
2383additive_expression:
2384 multiplicative_expression
2385| additive_expression PLUS_TK multiplicative_expression
2386 {
2387 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2388 $1, $3);
2389 }
2390| additive_expression MINUS_TK multiplicative_expression
2391 {
2392 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2393 $1, $3);
2394 }
2395| additive_expression PLUS_TK error
2396 {yyerror ("Missing term"); RECOVER;}
2397| additive_expression MINUS_TK error
2398 {yyerror ("Missing term"); RECOVER;}
2399;
2400
2401shift_expression:
2402 additive_expression
2403| shift_expression LS_TK additive_expression
2404 {
2405 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2406 $1, $3);
2407 }
2408| shift_expression SRS_TK additive_expression
2409 {
2410 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2411 $1, $3);
2412 }
2413| shift_expression ZRS_TK additive_expression
2414 {
2415 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2416 $1, $3);
2417 }
2418| shift_expression LS_TK error
2419 {yyerror ("Missing term"); RECOVER;}
2420| shift_expression SRS_TK error
2421 {yyerror ("Missing term"); RECOVER;}
2422| shift_expression ZRS_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424;
2425
2426relational_expression:
2427 shift_expression
2428| relational_expression LT_TK shift_expression
2429 {
2430 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2431 $1, $3);
2432 }
2433| relational_expression GT_TK shift_expression
2434 {
2435 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2436 $1, $3);
2437 }
2438| relational_expression LTE_TK shift_expression
2439 {
2440 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2441 $1, $3);
2442 }
2443| relational_expression GTE_TK shift_expression
2444 {
2445 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2446 $1, $3);
2447 }
2448| relational_expression INSTANCEOF_TK reference_type
5e942c50 2449 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2450| relational_expression LT_TK error
2451 {yyerror ("Missing term"); RECOVER;}
2452| relational_expression GT_TK error
2453 {yyerror ("Missing term"); RECOVER;}
2454| relational_expression LTE_TK error
2455 {yyerror ("Missing term"); RECOVER;}
2456| relational_expression GTE_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458| relational_expression INSTANCEOF_TK error
2459 {yyerror ("Invalid reference type"); RECOVER;}
2460;
2461
2462equality_expression:
2463 relational_expression
2464| equality_expression EQ_TK relational_expression
2465 {
2466 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2467 $1, $3);
2468 }
2469| equality_expression NEQ_TK relational_expression
2470 {
2471 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2472 $1, $3);
2473 }
2474| equality_expression EQ_TK error
2475 {yyerror ("Missing term"); RECOVER;}
2476| equality_expression NEQ_TK error
2477 {yyerror ("Missing term"); RECOVER;}
2478;
2479
2480and_expression:
2481 equality_expression
2482| and_expression AND_TK equality_expression
2483 {
2484 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2485 $1, $3);
2486 }
2487| and_expression AND_TK error
2488 {yyerror ("Missing term"); RECOVER;}
2489;
2490
2491exclusive_or_expression:
2492 and_expression
2493| exclusive_or_expression XOR_TK and_expression
2494 {
2495 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2496 $1, $3);
2497 }
2498| exclusive_or_expression XOR_TK error
2499 {yyerror ("Missing term"); RECOVER;}
2500;
2501
2502inclusive_or_expression:
2503 exclusive_or_expression
2504| inclusive_or_expression OR_TK exclusive_or_expression
2505 {
2506 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2507 $1, $3);
2508 }
2509| inclusive_or_expression OR_TK error
2510 {yyerror ("Missing term"); RECOVER;}
2511;
2512
2513conditional_and_expression:
2514 inclusive_or_expression
2515| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2516 {
2517 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2518 $1, $3);
2519 }
2520| conditional_and_expression BOOL_AND_TK error
2521 {yyerror ("Missing term"); RECOVER;}
2522;
2523
2524conditional_or_expression:
2525 conditional_and_expression
2526| conditional_or_expression BOOL_OR_TK conditional_and_expression
2527 {
2528 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2529 $1, $3);
2530 }
2531| conditional_or_expression BOOL_OR_TK error
2532 {yyerror ("Missing term"); RECOVER;}
2533;
2534
2535conditional_expression: /* Error handling here is weak */
2536 conditional_or_expression
2537| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2538 {
2539 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2540 EXPR_WFL_LINECOL ($$) = $2.location;
2541 }
e04a16fb
AG
2542| conditional_or_expression REL_QM_TK REL_CL_TK error
2543 {
2544 YYERROR_NOW;
2545 yyerror ("Missing term");
2546 DRECOVER (1);
2547 }
2548| conditional_or_expression REL_QM_TK error
2549 {yyerror ("Missing term"); DRECOVER (2);}
2550| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2551 {yyerror ("Missing term"); DRECOVER (3);}
2552;
2553
2554assignment_expression:
2555 conditional_expression
2556| assignment
2557;
2558
2559assignment:
2560 left_hand_side assignment_operator assignment_expression
2561 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2562| left_hand_side assignment_operator error
2563 {
2564 if (ctxp->prevent_ese != lineno)
2565 yyerror ("Missing term");
2566 DRECOVER (assign);
2567 }
2568;
2569
2570left_hand_side:
2571 name
2572| field_access
2573| array_access
2574;
2575
2576assignment_operator:
2577 ASSIGN_ANY_TK
2578| ASSIGN_TK
2579;
2580
2581expression:
2582 assignment_expression
2583;
2584
2585constant_expression:
2586 expression
2587;
2588
2589%%
2590\f
2591
c2952b01
APB
2592/* This section of the code deal with save/restoring parser contexts.
2593 Add mode documentation here. FIXME */
e04a16fb 2594
c2952b01
APB
2595/* Helper function. Create a new parser context. With
2596 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2597 context is copied, otherwise, the new context is zeroed. The newly
2598 created context becomes the current one. */
e04a16fb 2599
c2952b01
APB
2600static void
2601create_new_parser_context (copy_from_previous)
2602 int copy_from_previous;
e04a16fb 2603{
c2952b01 2604 struct parser_ctxt *new;
e04a16fb 2605
c2952b01
APB
2606 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2607 if (copy_from_previous)
2608 {
2609 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2610 new->saved_data_ctx = 1;
2611 }
2612 else
2613 bzero ((PTR) new, sizeof (struct parser_ctxt));
2614
e04a16fb
AG
2615 new->next = ctxp;
2616 ctxp = new;
c2952b01
APB
2617}
2618
2619/* Create a new parser context and make it the current one. */
2620
2621void
2622java_push_parser_context ()
2623{
2624 create_new_parser_context (0);
e04a16fb 2625 if (ctxp->next)
5e942c50
APB
2626 {
2627 ctxp->incomplete_class = ctxp->next->incomplete_class;
2628 ctxp->gclass_list = ctxp->next->gclass_list;
2629 }
e04a16fb
AG
2630}
2631
c2952b01
APB
2632void
2633java_pop_parser_context (generate)
2634 int generate;
2635{
2636 tree current;
2637 struct parser_ctxt *toFree, *next;
2638
2639 if (!ctxp)
2640 return;
2641
2642 toFree = ctxp;
2643 next = ctxp->next;
2644 if (next)
2645 {
2646 next->incomplete_class = ctxp->incomplete_class;
2647 next->gclass_list = ctxp->gclass_list;
2648 lineno = ctxp->lineno;
19e223db 2649 current_class = ctxp->class_type;
c2952b01
APB
2650 }
2651
d19cbcb5
TT
2652 /* If the old and new lexers differ, then free the old one. */
2653 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2654 java_destroy_lexer (ctxp->lexer);
2655
c2952b01
APB
2656 /* Set the single import class file flag to 0 for the current list
2657 of imported things */
2658 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2659 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2660
2661 /* And restore those of the previous context */
2662 if ((ctxp = next)) /* Assignment is really meant here */
2663 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2664 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2665
2666 /* If we pushed a context to parse a class intended to be generated,
2667 we keep it so we can remember the class. What we could actually
2668 do is to just update a list of class names. */
2669 if (generate)
2670 {
2671 toFree->next = ctxp_for_generation;
2672 ctxp_for_generation = toFree;
2673 }
2674 else
2675 free (toFree);
2676}
2677
2678/* Create a parser context for the use of saving some global
2679 variables. */
2680
e04a16fb
AG
2681void
2682java_parser_context_save_global ()
2683{
22eed1e6
APB
2684 if (!ctxp)
2685 {
2686 java_push_parser_context ();
ee07f4f4
APB
2687 ctxp->saved_data_ctx = 1;
2688 }
c2952b01
APB
2689
2690 /* If this context already stores data, create a new one suitable
2691 for data storage. */
ee07f4f4 2692 else if (ctxp->saved_data)
c2952b01
APB
2693 create_new_parser_context (1);
2694
e04a16fb 2695 ctxp->lineno = lineno;
19e223db 2696 ctxp->class_type = current_class;
e04a16fb 2697 ctxp->filename = input_filename;
19e223db 2698 ctxp->function_decl = current_function_decl;
ee07f4f4 2699 ctxp->saved_data = 1;
e04a16fb
AG
2700}
2701
c2952b01
APB
2702/* Restore some global variables from the previous context. Make the
2703 previous context the current one. */
2704
e04a16fb
AG
2705void
2706java_parser_context_restore_global ()
2707{
e04a16fb 2708 lineno = ctxp->lineno;
19e223db 2709 current_class = ctxp->class_type;
e04a16fb 2710 input_filename = ctxp->filename;
19e223db 2711 current_function_decl = ctxp->function_decl;
c2952b01 2712 ctxp->saved_data = 0;
ee07f4f4
APB
2713 if (ctxp->saved_data_ctx)
2714 java_pop_parser_context (0);
e04a16fb
AG
2715}
2716
c2952b01
APB
2717/* Suspend vital data for the current class/function being parsed so
2718 that an other class can be parsed. Used to let local/anonymous
2719 classes be parsed. */
2720
2721static void
2722java_parser_context_suspend ()
e04a16fb 2723{
c2952b01 2724 /* This makes debugging through java_debug_context easier */
3b304f5b 2725 static const char *name = "<inner buffer context>";
e04a16fb 2726
c2952b01
APB
2727 /* Duplicate the previous context, use it to save the globals we're
2728 interested in */
2729 create_new_parser_context (1);
19e223db
MM
2730 ctxp->function_decl = current_function_decl;
2731 ctxp->class_type = current_class;
5e942c50 2732
c2952b01
APB
2733 /* Then create a new context which inherits all data from the
2734 previous one. This will be the new current context */
2735 create_new_parser_context (1);
2736
2737 /* Help debugging */
2738 ctxp->next->filename = name;
2739}
2740
2741/* Resume vital data for the current class/function being parsed so
2742 that an other class can be parsed. Used to let local/anonymous
2743 classes be parsed. The trick is the data storing file position
2744 informations must be restored to their current value, so parsing
2745 can resume as if no context was ever saved. */
2746
2747static void
2748java_parser_context_resume ()
2749{
2750 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2751 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2752 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2753
2754 /* We need to inherit the list of classes to complete/generate */
2755 restored->incomplete_class = old->incomplete_class;
2756 restored->gclass_list = old->gclass_list;
2757 restored->classd_list = old->classd_list;
2758 restored->class_list = old->class_list;
2759
2760 /* Restore the current class and function from the saver */
19e223db
MM
2761 current_class = saver->class_type;
2762 current_function_decl = saver->function_decl;
c2952b01
APB
2763
2764 /* Retrive the restored context */
2765 ctxp = restored;
2766
2767 /* Re-installed the data for the parsing to carry on */
2768 bcopy (&old->marker_begining, &ctxp->marker_begining,
2769 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2770
2771 /* Buffer context can now be discarded */
2772 free (saver);
2773 free (old);
2774}
2775
2776/* Add a new anchor node to which all statement(s) initializing static
2777 and non static initialized upon declaration field(s) will be
2778 linked. */
2779
2780static void
2781java_parser_context_push_initialized_field ()
2782{
2783 tree node;
2784
2785 node = build_tree_list (NULL_TREE, NULL_TREE);
2786 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2787 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2788
2789 node = build_tree_list (NULL_TREE, NULL_TREE);
2790 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2791 CPC_INITIALIZER_LIST (ctxp) = node;
2792
2793 node = build_tree_list (NULL_TREE, NULL_TREE);
2794 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2795 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2796}
2797
2798/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2799 remember them so we can use it to create and populate the finit$
c2952b01
APB
2800 or <clinit> functions. */
2801
2802static void
2803java_parser_context_pop_initialized_field ()
2804{
2805 tree stmts;
2806 tree class_type = TREE_TYPE (GET_CPC ());
2807
2808 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2809 {
c2952b01
APB
2810 stmts = CPC_INITIALIZER_STMT (ctxp);
2811 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2812 if (stmts && !java_error_count)
2813 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2814 }
2815
c2952b01
APB
2816 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2817 {
2818 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2819 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2820 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2821 /* Keep initialization in order to enforce 8.5 */
2822 if (stmts && !java_error_count)
2823 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2824 }
e04a16fb 2825
c2952b01
APB
2826 /* JDK 1.1 instance initializers */
2827 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2828 {
c2952b01
APB
2829 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2830 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2831 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2832 if (stmts && !java_error_count)
2833 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2834 }
c2952b01
APB
2835}
2836
2837static tree
2838reorder_static_initialized (list)
2839 tree list;
2840{
2841 /* We have to keep things in order. The alias initializer have to
2842 come first, then the initialized regular field, in reverse to
2843 keep them in lexical order. */
2844 tree marker, previous = NULL_TREE;
2845 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2846 if (TREE_CODE (marker) == TREE_LIST
2847 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2848 break;
2849
2850 /* No static initialized, the list is fine as is */
2851 if (!previous)
2852 list = TREE_CHAIN (marker);
2853
2854 /* No marker? reverse the whole list */
2855 else if (!marker)
2856 list = nreverse (list);
2857
2858 /* Otherwise, reverse what's after the marker and the new reordered
2859 sublist will replace the marker. */
b351b287 2860 else
c2952b01
APB
2861 {
2862 TREE_CHAIN (previous) = NULL_TREE;
2863 list = nreverse (list);
2864 list = chainon (TREE_CHAIN (marker), list);
2865 }
2866 return list;
e04a16fb
AG
2867}
2868
c2952b01
APB
2869/* Helper functions to dump the parser context stack. */
2870
2871#define TAB_CONTEXT(C) \
2872 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2873
2874static void
2875java_debug_context_do (tab)
2876 int tab;
2877{
ee07f4f4
APB
2878 struct parser_ctxt *copy = ctxp;
2879 while (copy)
2880 {
c2952b01 2881 TAB_CONTEXT (tab);
ee07f4f4 2882 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2883 TAB_CONTEXT (tab);
ee07f4f4 2884 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2885 TAB_CONTEXT (tab);
2886 fprintf (stderr, "lineno: %d\n", copy->lineno);
2887 TAB_CONTEXT (tab);
ee07f4f4
APB
2888 fprintf (stderr, "package: %s\n",
2889 (copy->package ?
2890 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2891 TAB_CONTEXT (tab);
ee07f4f4 2892 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2893 TAB_CONTEXT (tab);
ee07f4f4
APB
2894 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2895 copy = copy->next;
2896 tab += 2;
2897 }
ee07f4f4
APB
2898}
2899
c2952b01
APB
2900/* Dump the stacked up parser contexts. Intended to be called from a
2901 debugger. */
2902
ee07f4f4
APB
2903void
2904java_debug_context ()
2905{
2906 java_debug_context_do (0);
2907}
2908
c2952b01
APB
2909\f
2910
2911/* Flag for the error report routine to issue the error the first time
2912 it's called (overriding the default behavior which is to drop the
2913 first invocation and honor the second one, taking advantage of a
2914 richer context. */
2915static int force_error = 0;
ee07f4f4 2916
8119c720
APB
2917/* Reporting an constructor invocation error. */
2918static void
2919parse_ctor_invocation_error ()
2920{
2921 if (DECL_CONSTRUCTOR_P (current_function_decl))
2922 yyerror ("Constructor invocation must be first thing in a constructor");
2923 else
2924 yyerror ("Only constructors can invoke constructors");
2925}
2926
2927/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2928
2929static tree
2930parse_jdk1_1_error (msg)
49f48c71 2931 const char *msg;
b67d701b
PB
2932{
2933 sorry (": `%s' JDK1.1(TM) feature", msg);
2934 java_error_count++;
9bbc7d9f 2935 return empty_stmt_node;
b67d701b
PB
2936}
2937
e04a16fb
AG
2938static int do_warning = 0;
2939
2940void
2941yyerror (msg)
49f48c71 2942 const char *msg;
e04a16fb
AG
2943{
2944 static java_lc elc;
2945 static int prev_lineno;
49f48c71 2946 static const char *prev_msg;
e04a16fb 2947
0a2138e2 2948 int save_lineno;
e04a16fb
AG
2949 char *remainder, *code_from_source;
2950 extern struct obstack temporary_obstack;
2951
2952 if (!force_error && prev_lineno == lineno)
2953 return;
2954
2955 /* Save current error location but report latter, when the context is
2956 richer. */
2957 if (ctxp->java_error_flag == 0)
2958 {
2959 ctxp->java_error_flag = 1;
2960 elc = ctxp->elc;
2961 /* Do something to use the previous line if we're reaching the
2962 end of the file... */
2963#ifdef VERBOSE_SKELETON
2964 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2965#endif
2966 return;
2967 }
2968
2969 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2970 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2971 return;
2972
2973 ctxp->java_error_flag = 0;
2974 if (do_warning)
2975 java_warning_count++;
2976 else
2977 java_error_count++;
2978
807bc1db 2979 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
2980 {
2981 elc.col = ctxp->p_line->char_col-1;
2982 elc.line = ctxp->p_line->lineno;
2983 }
2984
2985 save_lineno = lineno;
2986 prev_lineno = lineno = elc.line;
2987 prev_msg = msg;
2988
2989 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2990 obstack_grow0 (&temporary_obstack,
2991 code_from_source, strlen (code_from_source));
2992 remainder = obstack_finish (&temporary_obstack);
2993 if (do_warning)
2994 warning ("%s.\n%s", msg, remainder);
2995 else
2996 error ("%s.\n%s", msg, remainder);
2997
2998 /* This allow us to cheaply avoid an extra 'Invalid expression
2999 statement' error report when errors have been already reported on
3000 the same line. This occurs when we report an error but don't have
3001 a synchronization point other than ';', which
3002 expression_statement is the only one to take care of. */
3003 ctxp->prevent_ese = lineno = save_lineno;
3004}
3005
3006static void
15fdcfe9 3007issue_warning_error_from_context (cl, msg, ap)
5e942c50 3008 tree cl;
d4476be2 3009 const char *msg;
15fdcfe9 3010 va_list ap;
5e942c50 3011{
3b304f5b 3012 const char *saved, *saved_input_filename;
15fdcfe9
PB
3013 char buffer [4096];
3014 vsprintf (buffer, msg, ap);
3015 force_error = 1;
5e942c50
APB
3016
3017 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3018 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3019 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3020
3021 /* We have a CL, that's a good reason for using it if it contains data */
3022 saved = ctxp->filename;
3023 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3024 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3025 saved_input_filename = input_filename;
3026 input_filename = ctxp->filename;
15fdcfe9
PB
3027 java_error (NULL);
3028 java_error (buffer);
5e942c50 3029 ctxp->filename = saved;
1886c9d8 3030 input_filename = saved_input_filename;
15fdcfe9 3031 force_error = 0;
5e942c50
APB
3032}
3033
e04a16fb
AG
3034/* Issue an error message at a current source line CL */
3035
15fdcfe9 3036void
df32d2ce 3037parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3038{
d4476be2 3039#ifndef ANSI_PROTOTYPES
e04a16fb 3040 tree cl;
d4476be2 3041 const char *msg;
e04a16fb 3042#endif
e04a16fb
AG
3043 va_list ap;
3044
3045 VA_START (ap, msg);
d4476be2 3046#ifndef ANSI_PROTOTYPES
e04a16fb 3047 cl = va_arg (ap, tree);
d4476be2 3048 msg = va_arg (ap, const char *);
e04a16fb 3049#endif
15fdcfe9
PB
3050 issue_warning_error_from_context (cl, msg, ap);
3051 va_end (ap);
e04a16fb
AG
3052}
3053
3054/* Issue a warning at a current source line CL */
3055
3056static void
df32d2ce 3057parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3058{
d4476be2 3059#ifndef ANSI_PROTOTYPES
e04a16fb 3060 tree cl;
d4476be2 3061 const char *msg;
e04a16fb 3062#endif
e04a16fb
AG
3063 va_list ap;
3064
3065 VA_START (ap, msg);
d4476be2 3066#ifndef ANSI_PROTOTYPES
e04a16fb 3067 cl = va_arg (ap, tree);
d4476be2 3068 msg = va_arg (ap, const char *);
e04a16fb 3069#endif
e04a16fb 3070
c877974e 3071 force_error = do_warning = 1;
15fdcfe9 3072 issue_warning_error_from_context (cl, msg, ap);
c877974e 3073 do_warning = force_error = 0;
15fdcfe9 3074 va_end (ap);
e04a16fb
AG
3075}
3076
82371d41
APB
3077static tree
3078find_expr_with_wfl (node)
3079 tree node;
3080{
3081 while (node)
3082 {
3083 char code;
3084 tree to_return;
3085
3086 switch (TREE_CODE (node))
3087 {
3088 case BLOCK:
c0d87ff6
PB
3089 node = BLOCK_EXPR_BODY (node);
3090 continue;
82371d41
APB
3091
3092 case COMPOUND_EXPR:
3093 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3094 if (to_return)
3095 return to_return;
c0d87ff6
PB
3096 node = TREE_OPERAND (node, 1);
3097 continue;
82371d41
APB
3098
3099 case LOOP_EXPR:
c0d87ff6
PB
3100 node = TREE_OPERAND (node, 0);
3101 continue;
82371d41
APB
3102
3103 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3104 node = TREE_OPERAND (node, 1);
3105 continue;
3106
82371d41
APB
3107 default:
3108 code = TREE_CODE_CLASS (TREE_CODE (node));
3109 if (((code == '1') || (code == '2') || (code == 'e'))
3110 && EXPR_WFL_LINECOL (node))
3111 return node;
ba179f9f 3112 return NULL_TREE;
82371d41
APB
3113 }
3114 }
3115 return NULL_TREE;
3116}
3117
3118/* Issue a missing return statement error. Uses METHOD to figure the
3119 last line of the method the error occurs in. */
3120
3121static void
3122missing_return_error (method)
3123 tree method;
3124{
3125 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3126 parse_error_context (wfl_operator, "Missing return statement");
3127}
3128
3129/* Issue an unreachable statement error. From NODE, find the next
3130 statement to report appropriately. */
3131static void
3132unreachable_stmt_error (node)
3133 tree node;
3134{
3135 /* Browse node to find the next expression node that has a WFL. Use
3136 the location to report the error */
3137 if (TREE_CODE (node) == COMPOUND_EXPR)
3138 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3139 else
3140 node = find_expr_with_wfl (node);
3141
3142 if (node)
3143 {
3144 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3145 parse_error_context (wfl_operator, "Unreachable statement");
3146 }
3147 else
3148 fatal ("Can't get valid statement - unreachable_stmt_error");
3149}
3150
c877974e 3151int
e04a16fb
AG
3152java_report_errors ()
3153{
3154 if (java_error_count)
3155 fprintf (stderr, "%d error%s",
3156 java_error_count, (java_error_count == 1 ? "" : "s"));
3157 if (java_warning_count)
3158 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3159 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3160 if (java_error_count || java_warning_count)
3161 putc ('\n', stderr);
c877974e 3162 return java_error_count;
e04a16fb
AG
3163}
3164
3165static char *
3166java_accstring_lookup (flags)
3167 int flags;
3168{
3169 static char buffer [80];
3170#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3171
3172 /* Access modifier looked-up first for easier report on forbidden
3173 access. */
3174 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3175 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3176 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3177 if (flags & ACC_STATIC) COPY_RETURN ("static");
3178 if (flags & ACC_FINAL) COPY_RETURN ("final");
3179 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3180 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3181 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3182 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3183 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3184 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3185
3186 buffer [0] = '\0';
3187 return buffer;
3188#undef COPY_RETURN
3189}
3190
b67d701b
PB
3191/* Issuing error messages upon redefinition of classes, interfaces or
3192 variables. */
3193
e04a16fb 3194static void
b67d701b 3195classitf_redefinition_error (context, id, decl, cl)
49f48c71 3196 const char *context;
e04a16fb
AG
3197 tree id, decl, cl;
3198{
3199 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3200 context, IDENTIFIER_POINTER (id),
3201 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3202 /* Here we should point out where its redefined. It's a unicode. FIXME */
3203}
3204
b67d701b
PB
3205static void
3206variable_redefinition_error (context, name, type, line)
3207 tree context, name, type;
3208 int line;
3209{
49f48c71 3210 const char *type_name;
b67d701b
PB
3211
3212 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3213 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3214 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3215 else
0a2138e2 3216 type_name = lang_printable_name (type, 0);
b67d701b
PB
3217
3218 parse_error_context (context,
781b0558 3219 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3220 IDENTIFIER_POINTER (name),
3221 type_name, IDENTIFIER_POINTER (name), line);
3222}
3223
c583dd46
APB
3224static tree
3225build_array_from_name (type, type_wfl, name, ret_name)
3226 tree type, type_wfl, name, *ret_name;
3227{
3228 int more_dims = 0;
49f48c71 3229 const char *string;
c583dd46
APB
3230
3231 /* Eventually get more dims */
3232 string = IDENTIFIER_POINTER (name);
3233 while (string [more_dims] == '[')
3234 more_dims++;
3235
3236 /* If we have, then craft a new type for this variable */
3237 if (more_dims)
3238 {
c0d87ff6 3239 name = get_identifier (&string [more_dims]);
c583dd46 3240
34f4db93
APB
3241 /* If we have a pointer, use its type */
3242 if (TREE_CODE (type) == POINTER_TYPE)
3243 type = TREE_TYPE (type);
c583dd46
APB
3244
3245 /* Building the first dimension of a primitive type uses this
3246 function */
3247 if (JPRIMITIVE_TYPE_P (type))
3248 {
3249 type = build_java_array_type (type, -1);
22eed1e6 3250 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3251 more_dims--;
3252 }
3253 /* Otherwise, if we have a WFL for this type, use it (the type
3254 is already an array on an unresolved type, and we just keep
3255 on adding dimensions) */
3256 else if (type_wfl)
3257 type = type_wfl;
3258
3259 /* Add all the dimensions */
3260 while (more_dims--)
3261 type = build_unresolved_array_type (type);
3262
3263 /* The type may have been incomplete in the first place */
3264 if (type_wfl)
3265 type = obtain_incomplete_type (type);
3266 }
3267
c2952b01
APB
3268 if (ret_name)
3269 *ret_name = name;
c583dd46
APB
3270 return type;
3271}
3272
e04a16fb
AG
3273/* Build something that the type identifier resolver will identify as
3274 being an array to an unresolved type. TYPE_WFL is a WFL on a
3275 identifier. */
3276
3277static tree
3278build_unresolved_array_type (type_or_wfl)
3279 tree type_or_wfl;
3280{
49f48c71 3281 const char *ptr;
e04a16fb 3282
1886c9d8 3283 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3284 just create a array type */
3285 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3286 {
3287 tree type = build_java_array_type (type_or_wfl, -1);
3288 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3289 return type;
3290 }
3291
3292 obstack_1grow (&temporary_obstack, '[');
3293 obstack_grow0 (&temporary_obstack,
3294 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3295 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3296 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3297 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3298 return type_or_wfl;
e04a16fb
AG
3299}
3300
e04a16fb
AG
3301static void
3302parser_add_interface (class_decl, interface_decl, wfl)
3303 tree class_decl, interface_decl, wfl;
3304{
3305 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3306 parse_error_context (wfl, "Interface `%s' repeated",
3307 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3308}
3309
3310/* Bulk of common class/interface checks. Return 1 if an error was
3311 encountered. TAG is 0 for a class, 1 for an interface. */
3312
3313static int
3314check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3315 int is_interface, flags;
3316 tree raw_name, qualified_name, decl, cl;
3317{
3318 tree node;
c2952b01
APB
3319 int sca = 0; /* Static class allowed */
3320 int icaf = 0; /* Inner class allowed flags */
3321 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3322
3323 if (!quiet_flag)
c2952b01
APB
3324 fprintf (stderr, " %s%s %s",
3325 (CPC_INNER_P () ? "inner" : ""),
3326 (is_interface ? "interface" : "class"),
e04a16fb
AG
3327 IDENTIFIER_POINTER (qualified_name));
3328
3329 /* Scope of an interface/class type name:
3330 - Can't be imported by a single type import
3331 - Can't already exists in the package */
3332 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3333 && (node = find_name_in_single_imports (raw_name))
3334 && !CPC_INNER_P ())
e04a16fb
AG
3335 {
3336 parse_error_context
3337 (cl, "%s name `%s' clashes with imported type `%s'",
3338 (is_interface ? "Interface" : "Class"),
3339 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3340 return 1;
3341 }
3342 if (decl && CLASS_COMPLETE_P (decl))
3343 {
b67d701b
PB
3344 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3345 qualified_name, decl, cl);
e04a16fb
AG
3346 return 1;
3347 }
3348
c2952b01
APB
3349 if (check_inner_class_redefinition (raw_name, cl))
3350 return 1;
3351
3352 /* If public, file name should match class/interface name, except
3353 when dealing with an inner class */
3354 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3355 {
49f48c71 3356 const char *f;
e04a16fb
AG
3357
3358 /* Contains OS dependent assumption on path separator. FIXME */
3359 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3360 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3361 f--)
3362 ;
847fe791 3363 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3364 f++;
3365 if (strncmp (IDENTIFIER_POINTER (raw_name),
3366 f , IDENTIFIER_LENGTH (raw_name)) ||
3367 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3368 parse_error_context
3369 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3370 (is_interface ? "interface" : "class"),
3371 IDENTIFIER_POINTER (qualified_name),
3372 IDENTIFIER_POINTER (raw_name));
3373 }
3374
c2952b01
APB
3375 /* Static classes can be declared only in top level classes. Note:
3376 once static, a inner class is a top level class. */
3377 if (flags & ACC_STATIC)
3378 {
3379 /* Catch the specific error of declaring an class inner class
3380 with no toplevel enclosing class. Prevent check_modifiers from
3381 complaining a second time */
3382 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3383 {
3384 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3385 IDENTIFIER_POINTER (qualified_name));
3386 sca = ACC_STATIC;
3387 }
3388 /* Else, in the context of a top-level class declaration, let
3389 `check_modifiers' do its job, otherwise, give it a go */
3390 else
3391 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3392 }
3393
a40d21da 3394 /* Inner classes can be declared private or protected
c2952b01
APB
3395 within their enclosing classes. */
3396 if (CPC_INNER_P ())
3397 {
3398 /* A class which is local to a block can't be public, private,
3399 protected or static. But it is created final, so allow this
3400 one. */
3401 if (current_function_decl)
3402 icaf = sca = uaaf = ACC_FINAL;
3403 else
3404 {
3405 check_modifiers_consistency (flags);
3406 icaf = ACC_PRIVATE|ACC_PROTECTED;
3407 }
3408 }
3409
a40d21da
APB
3410 if (is_interface)
3411 {
3412 if (CPC_INNER_P ())
3413 uaaf = INTERFACE_INNER_MODIFIERS;
3414 else
3415 uaaf = INTERFACE_MODIFIERS;
3416
3417 check_modifiers ("Illegal modifier `%s' for interface declaration",
3418 flags, uaaf);
3419 }
2884c41e 3420 else
a40d21da
APB
3421 check_modifiers ((current_function_decl ?
3422 "Illegal modifier `%s' for local class declaration" :
3423 "Illegal modifier `%s' for class declaration"),
c2952b01 3424 flags, uaaf|sca|icaf);
e04a16fb
AG
3425 return 0;
3426}
3427
c2952b01
APB
3428static void
3429make_nested_class_name (cpc_list)
3430 tree cpc_list;
3431{
3432 tree name;
3433
3434 if (!cpc_list)
3435 return;
3436 else
3437 make_nested_class_name (TREE_CHAIN (cpc_list));
3438
3439 /* Pick the qualified name when dealing with the first upmost
3440 enclosing class */
3441 name = (TREE_CHAIN (cpc_list) ?
3442 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3443 obstack_grow (&temporary_obstack,
3444 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3445 /* Why is NO_DOLLAR_IN_LABEL defined? */
3446#if 0
3447#ifdef NO_DOLLAR_IN_LABEL
3448 fatal ("make_nested_class_name: Can't use '$' as a separator "
3449 "for inner classes");
3450#endif
3451#endif
3452 obstack_1grow (&temporary_obstack, '$');
3453}
3454
3455/* Can't redefine a class already defined in an earlier scope. */
3456
3457static int
3458check_inner_class_redefinition (raw_name, cl)
3459 tree raw_name, cl;
3460{
3461 tree scope_list;
3462
3463 for (scope_list = GET_CPC_LIST (); scope_list;
3464 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3465 if (raw_name == GET_CPC_UN_NODE (scope_list))
3466 {
3467 parse_error_context
3468 (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",
3469 IDENTIFIER_POINTER (raw_name));
3470 return 1;
3471 }
3472 return 0;
3473}
3474
3475static tree
3476find_as_inner_class (enclosing, name, cl)
3477 tree enclosing, name, cl;
3478{
3479 tree qual, to_return;
3480 if (!enclosing)
3481 return NULL_TREE;
3482
3483 name = TYPE_NAME (name);
3484
3485 /* First search: within the scope of `enclosing', search for name */
3486 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3487 qual = EXPR_WFL_QUALIFICATION (cl);
3488 else if (cl)
3489 qual = build_tree_list (cl, NULL_TREE);
3490 else
3491 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3492
3493 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3494 return to_return;
3495
3496 /* We're dealing with a qualified name. Try to resolve thing until
3497 we get something that is an enclosing class. */
3498 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3499 {
3500 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3501
0c2b8145
APB
3502 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3503 qual = TREE_CHAIN (qual))
c2952b01
APB
3504 {
3505 acc = merge_qualified_name (acc,
3506 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3507 BUILD_PTR_FROM_NAME (ptr, acc);
3508 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3509 }
3510
3511 /* A NULL qual and a decl means that the search ended
3512 successfully?!? We have to do something then. FIXME */
3513
3514 if (decl)
3515 enclosing = decl;
3516 else
3517 qual = EXPR_WFL_QUALIFICATION (cl);
3518 }
3519 /* Otherwise, create a qual for the other part of the resolution. */
3520 else
3521 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3522
1e12ab9b 3523 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3524}
3525
3526/* We go inside the list of sub classes and try to find a way
3527 through. */
3528
3529static tree
3530find_as_inner_class_do (qual, enclosing)
3531 tree qual, enclosing;
3532{
3533 if (!qual)
3534 return NULL_TREE;
3535
3536 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3537 {
3538 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3539 tree next_enclosing = NULL_TREE;
3540 tree inner_list;
3541
3542 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3543 inner_list; inner_list = TREE_CHAIN (inner_list))
3544 {
3545 if (TREE_VALUE (inner_list) == name_to_match)
3546 {
3547 next_enclosing = TREE_PURPOSE (inner_list);
3548 break;
3549 }
3550 }
3551 enclosing = next_enclosing;
3552 }
3553
3554 return (!qual && enclosing ? enclosing : NULL_TREE);
3555}
3556
3557/* Reach all inner classes and tie their unqualified name to a
3558 DECL. */
3559
3560static void
3561set_nested_class_simple_name_value (outer, set)
3562 tree outer;
3563 int set;
3564{
3565 tree l;
3566
3567 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3568 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3569 TREE_PURPOSE (l) : NULL_TREE);
3570}
3571
3572static void
3573link_nested_class_to_enclosing ()
3574{
3575 if (GET_ENCLOSING_CPC ())
3576 {
3577 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3578 DECL_INNER_CLASS_LIST (enclosing) =
3579 tree_cons (GET_CPC (), GET_CPC_UN (),
3580 DECL_INNER_CLASS_LIST (enclosing));
3581 enclosing = enclosing;
3582 }
3583}
3584
3585static tree
3586maybe_make_nested_class_name (name)
3587 tree name;
3588{
3589 tree id = NULL_TREE;
3590
3591 if (CPC_INNER_P ())
3592 {
3593 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3594 obstack_grow0 (&temporary_obstack,
3595 IDENTIFIER_POINTER (name),
3596 IDENTIFIER_LENGTH (name));
c2952b01
APB
3597 id = get_identifier (obstack_finish (&temporary_obstack));
3598 if (ctxp->package)
3599 QUALIFIED_P (id) = 1;
3600 }
3601 return id;
3602}
3603
3604/* If DECL is NULL, create and push a new DECL, record the current
3605 line CL and do other maintenance things. */
3606
e04a16fb 3607static tree
c2952b01
APB
3608maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3609 tree decl, raw_name, qualified_name, cl;
e04a16fb 3610{
5e942c50 3611 if (!decl)
e04a16fb 3612 decl = push_class (make_class (), qualified_name);
c2952b01 3613
e04a16fb
AG
3614 /* Take care of the file and line business */
3615 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3616 /* If we're emiting xrefs, store the line/col number information */
3617 if (flag_emit_xref)
3618 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3619 else
3620 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3621 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3622 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3623 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3624
c2952b01
APB
3625 PUSH_CPC (decl, raw_name);
3626 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3627
e04a16fb
AG
3628 /* Link the declaration to the already seen ones */
3629 TREE_CHAIN (decl) = ctxp->class_list;
3630 ctxp->class_list = decl;
5e942c50 3631
23a79c61 3632 /* Create a new nodes in the global lists */
5e942c50 3633 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3634 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3635
e04a16fb
AG
3636 /* Install a new dependency list element */
3637 create_jdep_list (ctxp);
3638
3639 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3640 IDENTIFIER_POINTER (qualified_name)));
3641 return decl;
3642}
3643
3644static void
3645add_superinterfaces (decl, interface_list)
3646 tree decl, interface_list;
3647{
3648 tree node;
3649 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3650 takes care of ensuring that:
3651 - This is an accessible interface type,
3652 - Circularity detection.
3653 parser_add_interface is then called. If present but not defined,
3654 the check operation is delayed until the super interface gets
3655 defined. */
3656 for (node = interface_list; node; node = TREE_CHAIN (node))
3657 {
15fdcfe9 3658 tree current = TREE_PURPOSE (node);
5e942c50
APB
3659 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3660 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3661 {
5e942c50
APB
3662 if (!parser_check_super_interface (idecl, decl, current))
3663 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3664 }
3665 else
3666 register_incomplete_type (JDEP_INTERFACE,
3667 current, decl, NULL_TREE);
3668 }
3669}
3670
3671/* Create an interface in pass1 and return its decl. Return the
3672 interface's decl in pass 2. */
3673
3674static tree
3675create_interface (flags, id, super)
3676 int flags;
3677 tree id, super;
3678{
e04a16fb 3679 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3680 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3681 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3682
3683 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3684
3685 /* Basic checks: scope, redefinition, modifiers */
3686 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3687 {
3688 PUSH_ERROR ();
3689 return NULL_TREE;
3690 }
3691
3692 /* Suspend the current parsing context if we're parsing an inner
3693 interface */
3694 if (CPC_INNER_P ())
3695 java_parser_context_suspend ();
3696
3697 /* Push a new context for (static) initialized upon declaration fields */
3698 java_parser_context_push_initialized_field ();
e04a16fb
AG
3699
3700 /* Interface modifiers check
3701 - public/abstract allowed (already done at that point)
3702 - abstract is obsolete (comes first, it's a warning, or should be)
3703 - Can't use twice the same (checked in the modifier rule) */
c877974e 3704 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3705 parse_warning_context
3706 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3707 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3708
3709 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3710 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3711
3712 /* Set super info and mark the class a complete */
2aa11e97 3713 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3714 object_type_node, ctxp->interface_number);
3715 ctxp->interface_number = 0;
3716 CLASS_COMPLETE_P (decl) = 1;
3717 add_superinterfaces (decl, super);
3718
3719 return decl;
3720}
3721
c2952b01
APB
3722/* Anonymous class counter. Will be reset to 1 every time a non
3723 anonymous class gets created. */
3724static int anonymous_class_counter = 1;
3725
3726/* Patch anonymous class CLASS, by either extending or implementing
3727 DEP. */
3728
3729static void
3730patch_anonymous_class (type_decl, class_decl, wfl)
3731 tree type_decl, class_decl, wfl;
3732{
3733 tree class = TREE_TYPE (class_decl);
3734 tree type = TREE_TYPE (type_decl);
3735 tree binfo = TYPE_BINFO (class);
3736
3737 /* If it's an interface, implement it */
3738 if (CLASS_INTERFACE (type_decl))
3739 {
3740 tree s_binfo;
3741 int length;
3742
3743 if (parser_check_super_interface (type_decl, class_decl, wfl))
3744 return;
3745
3746 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3747 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3748 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3749 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3750 /* And add the interface */
3751 parser_add_interface (class_decl, type_decl, wfl);
3752 }
3753 /* Otherwise, it's a type we want to extend */
3754 else
3755 {
3756 if (parser_check_super (type_decl, class_decl, wfl))
3757 return;
3758 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3759 }
3760}
3761
3762static tree
3763create_anonymous_class (location, type_name)
3764 int location;
3765 tree type_name;
3766{
3767 char buffer [80];
3768 tree super = NULL_TREE, itf = NULL_TREE;
3769 tree id, type_decl, class;
3770
3771 /* The unqualified name of the anonymous class. It's just a number. */
3772 sprintf (buffer, "%d", anonymous_class_counter++);
3773 id = build_wfl_node (get_identifier (buffer));
3774 EXPR_WFL_LINECOL (id) = location;
3775
3776 /* We know about the type to extend/implement. We go ahead */
3777 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3778 {
3779 /* Create a class which either implements on extends the designated
3780 class. The class bears an innacessible name. */
3781 if (CLASS_INTERFACE (type_decl))
3782 {
3783 /* It's OK to modify it here. It's been already used and
3784 shouldn't be reused */
3785 ctxp->interface_number = 1;
3786 /* Interfaces should presented as a list of WFLs */
3787 itf = build_tree_list (type_name, NULL_TREE);
3788 }
3789 else
3790 super = type_name;
3791 }
3792
3793 class = create_class (ACC_FINAL, id, super, itf);
3794
3795 /* We didn't know anything about the stuff. We register a dependence. */
3796 if (!type_decl)
3797 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3798
3799 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3800 return class;
3801}
3802
a40d21da 3803/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3804 interface's decl in pass 2. */
3805
3806static tree
3807create_class (flags, id, super, interfaces)
3808 int flags;
3809 tree id, super, interfaces;
3810{
e04a16fb
AG
3811 tree raw_name = EXPR_WFL_NODE (id);
3812 tree class_id, decl;
9ee9b555 3813 tree super_decl_type;
e04a16fb 3814
98a52c2c 3815 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3816 decl = IDENTIFIER_CLASS_VALUE (class_id);
3817 EXPR_WFL_NODE (id) = class_id;
3818
3819 /* Basic check: scope, redefinition, modifiers */
3820 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3821 {
3822 PUSH_ERROR ();
3823 return NULL_TREE;
3824 }
3825
3826 /* Suspend the current parsing context if we're parsing an inner
3827 class or an anonymous class. */
3828 if (CPC_INNER_P ())
3829 java_parser_context_suspend ();
3830 /* Push a new context for (static) initialized upon declaration fields */
3831 java_parser_context_push_initialized_field ();
e04a16fb
AG
3832
3833 /* Class modifier check:
3834 - Allowed modifier (already done at that point)
3835 - abstract AND final forbidden
3836 - Public classes defined in the correct file */
3837 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3838 parse_error_context
3839 (id, "Class `%s' can't be declared both abstract and final",
3840 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3841
3842 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3843 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3844
3845 /* If SUPER exists, use it, otherwise use Object */
3846 if (super)
3847 {
3848 /* Can't extend java.lang.Object */
3849 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3850 {
3851 parse_error_context (id, "Can't extend `java.lang.Object'");
3852 return NULL_TREE;
3853 }
3854
2c3199bc
PB
3855 super_decl_type =
3856 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3857 }
3858 else if (TREE_TYPE (decl) != object_type_node)
3859 super_decl_type = object_type_node;
3860 /* We're defining java.lang.Object */
3861 else
3862 super_decl_type = NULL_TREE;
3863
3864 /* Set super info and mark the class a complete */
3865 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3866 ctxp->interface_number);
3867 ctxp->interface_number = 0;
3868 CLASS_COMPLETE_P (decl) = 1;
3869 add_superinterfaces (decl, interfaces);
3870
c2952b01
APB
3871 /* Add the private this$<n> field, Replicate final locals still in
3872 scope as private final fields mangled like val$<local_name>.
3873 This doesn't not occur for top level (static) inner classes. */
3874 if (PURE_INNER_CLASS_DECL_P (decl))
3875 add_inner_class_fields (decl, current_function_decl);
3876
7f10c2e2
APB
3877 /* If doing xref, store the location at which the inherited class
3878 (if any) was seen. */
3879 if (flag_emit_xref && super)
3880 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3881
5e942c50
APB
3882 /* Eventually sets the @deprecated tag flag */
3883 CHECK_DEPRECATED (decl);
3884
165f37bc
APB
3885 /* Reset the anonymous class counter when declaring non inner classes */
3886 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3887 anonymous_class_counter = 1;
3888
e04a16fb
AG
3889 return decl;
3890}
3891
c2952b01 3892/* End a class declaration: register the statements used to create
c00f0fb2 3893 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3894 parser context if necessary. */
3895
3896static void
3897end_class_declaration (resume)
3898 int resume;
3899{
3900 /* If an error occured, context weren't pushed and won't need to be
3901 popped by a resume. */
3902 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3903
3904 java_parser_context_pop_initialized_field ();
3905 POP_CPC ();
3906 if (resume && no_error_occured)
3907 java_parser_context_resume ();
93220702
APB
3908
3909 /* We're ending a class declaration, this is a good time to reset
3910 the interface cout. Note that might have been already done in
3911 create_interface, but if at that time an inner class was being
3912 dealt with, the interface count was reset in a context created
3913 for the sake of handling inner classes declaration. */
3914 ctxp->interface_number = 0;
c2952b01
APB
3915}
3916
3917static void
3918add_inner_class_fields (class_decl, fct_decl)
3919 tree class_decl;
3920 tree fct_decl;
3921{
3922 tree block, marker, f;
3923
3924 f = add_field (TREE_TYPE (class_decl),
3925 build_current_thisn (TREE_TYPE (class_decl)),
3926 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3927 ACC_PRIVATE);
3928 FIELD_THISN (f) = 1;
3929
3930 if (!fct_decl)
3931 return;
3932
3933 for (block = GET_CURRENT_BLOCK (fct_decl);
3934 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3935 {
3936 tree decl;
3937 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3938 {
3939 char *name, *pname;
3940 tree wfl, init, list;
3941
3942 /* Avoid non final arguments. */
3943 if (!LOCAL_FINAL (decl))
3944 continue;
3945
3946 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3947 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3948 wfl = build_wfl_node (get_identifier (name));
3949 init = build_wfl_node (get_identifier (pname));
3950 /* Build an initialization for the field: it will be
c00f0fb2 3951 initialized by a parameter added to finit$, bearing a
c2952b01 3952 mangled name of the field itself (param$<n>.) The
c00f0fb2 3953 parameter is provided to finit$ by the constructor
c2952b01
APB
3954 invoking it (hence the constructor will also feature a
3955 hidden parameter, set to the value of the outer context
3956 local at the time the inner class is created.)
3957
3958 Note: we take into account all possible locals that can
3959 be accessed by the inner class. It's actually not trivial
3960 to minimize these aliases down to the ones really
3961 used. One way to do that would be to expand all regular
c00f0fb2 3962 methods first, then finit$ to get a picture of what's
c2952b01
APB
3963 used. It works with the exception that we would have to
3964 go back on all constructor invoked in regular methods to
3965 have their invokation reworked (to include the right amount
3966 of alias initializer parameters.)
3967
3968 The only real way around, I think, is a first pass to
3969 identify locals really used in the inner class. We leave
3970 the flag FIELD_LOCAL_ALIAS_USED around for that future
3971 use.
3972
3973 On the other hand, it only affect local inner classes,
c00f0fb2 3974 whose constructors (and finit$ call) will be featuring
c2952b01
APB
3975 unecessary arguments. It's easy for a developper to keep
3976 this number of parameter down by using the `final'
3977 keyword only when necessary. For the time being, we can
3978 issue a warning on unecessary finals. FIXME */
3979 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3980 wfl, init);
3981
3982 /* Register the field. The TREE_LIST holding the part
3983 initialized/initializer will be marked ARG_FINAL_P so
3984 that the created field can be marked
3985 FIELD_LOCAL_ALIAS. */
3986 list = build_tree_list (wfl, init);
3987 ARG_FINAL_P (list) = 1;
3988 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3989 }
3990 }
3991
3992 if (!CPC_INITIALIZER_STMT (ctxp))
3993 return;
3994
3995 /* If we ever registered an alias field, insert and marker to
3996 remeber where the list ends. The second part of the list (the one
3997 featuring initialized fields) so it can be later reversed to
3998 enforce 8.5. The marker will be removed during that operation. */
3999 marker = build_tree_list (NULL_TREE, NULL_TREE);
4000 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4001 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4002}
4003
e04a16fb
AG
4004/* Can't use lookup_field () since we don't want to load the class and
4005 can't set the CLASS_LOADED_P flag */
4006
4007static tree
4008find_field (class, name)
4009 tree class;
4010 tree name;
4011{
4012 tree decl;
4013 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4014 {
4015 if (DECL_NAME (decl) == name)
4016 return decl;
4017 }
4018 return NULL_TREE;
4019}
4020
4021/* Wrap around lookup_field that doesn't potentially upset the value
4022 of CLASS */
4023
4024static tree
4025lookup_field_wrapper (class, name)
4026 tree class, name;
4027{
4028 tree type = class;
9a7ab4b3 4029 tree decl = NULL_TREE;
c877974e 4030 java_parser_context_save_global ();
f2760b27
APB
4031
4032 /* Last chance: if we're within the context of an inner class, we
4033 might be trying to access a local variable defined in an outer
4034 context. We try to look for it now. */
9a7ab4b3 4035 if (INNER_CLASS_TYPE_P (class))
f2760b27
APB
4036 {
4037 char *alias_buffer;
9a7ab4b3 4038 tree new_name;
f2760b27 4039 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
9a7ab4b3
APB
4040 new_name = get_identifier (alias_buffer);
4041 decl = lookup_field (&type, new_name);
f2760b27
APB
4042 if (decl && decl != error_mark_node)
4043 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4044 }
9a7ab4b3
APB
4045 if (!decl || decl == error_mark_node)
4046 {
4047 type = class;
4048 decl = lookup_field (&type, name);
4049 }
f2760b27 4050
c877974e 4051 java_parser_context_restore_global ();
93024893 4052 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4053}
4054
4055/* Find duplicate field within the same class declarations and report
c583dd46
APB
4056 the error. Returns 1 if a duplicated field was found, 0
4057 otherwise. */
e04a16fb
AG
4058
4059static int
c583dd46 4060duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4061 tree new_field_name, new_type, cl;
e04a16fb
AG
4062{
4063 /* This might be modified to work with method decl as well */
c2952b01 4064 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4065 if (decl)
4066 {
c2e3db92 4067 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4068 ((TREE_CODE (new_type) == POINTER_TYPE
4069 && TREE_TYPE (new_type) == NULL_TREE) ?
4070 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4071 lang_printable_name (new_type, 1)));
c877974e
APB
4072 /* The type may not have been completed by the time we report
4073 the error */
c2e3db92 4074 char *t2 = xstrdup (purify_type_name
4a5f66c3 4075 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4076 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4077 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4078 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4079 parse_error_context
4080 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4081 t1, IDENTIFIER_POINTER (new_field_name),
4082 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4083 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4084 free (t1);
4085 free (t2);
c583dd46 4086 return 1;
e04a16fb 4087 }
c583dd46 4088 return 0;
e04a16fb
AG
4089}
4090
4091/* Field registration routine. If TYPE doesn't exist, field
4092 declarations are linked to the undefined TYPE dependency list, to
4093 be later resolved in java_complete_class () */
4094
4095static void
4096register_fields (flags, type, variable_list)
4097 int flags;
4098 tree type, variable_list;
4099{
c583dd46 4100 tree current, saved_type;
c2952b01 4101 tree class_type = NULL_TREE;
e04a16fb
AG
4102 int saved_lineno = lineno;
4103 int must_chain = 0;
4104 tree wfl = NULL_TREE;
4105
c2952b01
APB
4106 if (GET_CPC ())
4107 class_type = TREE_TYPE (GET_CPC ());
4108
4109 if (!class_type || class_type == error_mark_node)
4110 return;
4111
e04a16fb
AG
4112 /* If we're adding fields to interfaces, those fields are public,
4113 static, final */
4114 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4115 {
4116 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4117 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4118 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4119 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4120 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4121 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4122 check_modifiers ("Illegal interface member modifier `%s'", flags,
4123 INTERFACE_FIELD_MODIFIERS);
4124 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4125 }
4126
c583dd46
APB
4127 /* Obtain a suitable type for resolution, if necessary */
4128 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4129
4130 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4131 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4132
c583dd46
APB
4133 for (current = variable_list, saved_type = type; current;
4134 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4135 {
c877974e 4136 tree real_type;
c583dd46 4137 tree field_decl;
e04a16fb
AG
4138 tree cl = TREE_PURPOSE (current);
4139 tree init = TREE_VALUE (current);
4140 tree current_name = EXPR_WFL_NODE (cl);
4141
cf1748bf 4142 /* Can't declare non-final static fields in inner classes */
c2952b01 4143 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4144 && !(flags & ACC_FINAL))
c2952b01 4145 parse_error_context
cf1748bf 4146 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4147 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4148 lang_printable_name (class_type, 0));
4149
c583dd46
APB
4150 /* Process NAME, as it may specify extra dimension(s) for it */
4151 type = build_array_from_name (type, wfl, current_name, &current_name);
4152
c583dd46
APB
4153 /* Type adjustment. We may have just readjusted TYPE because
4154 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4155 a reference if we can and don't have one already. Also
4156 change the name if we have an init. */
4157 if (type != saved_type)
4158 {
1886c9d8 4159 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4160 if (init)
4161 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4162 }
e04a16fb 4163
c877974e
APB
4164 real_type = GET_REAL_TYPE (type);
4165 /* Check for redeclarations */
4166 if (duplicate_declaration_error_p (current_name, real_type, cl))
4167 continue;
4168
c583dd46 4169 /* Set lineno to the line the field was found and create a
5e942c50 4170 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4171 if (flag_emit_xref)
4172 lineno = EXPR_WFL_LINECOL (cl);
4173 else
4174 lineno = EXPR_WFL_LINENO (cl);
c877974e 4175 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4176 CHECK_DEPRECATED (field_decl);
c2952b01
APB
4177
4178 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4179 mark the created field FIELD_LOCAL_ALIAS, so that we can
c00f0fb2 4180 hide parameters to this inner class finit$ and constructors. */
c2952b01
APB
4181 if (ARG_FINAL_P (current))
4182 FIELD_LOCAL_ALIAS (field_decl) = 1;
c583dd46
APB
4183
4184 /* Check if we must chain. */
4185 if (must_chain)
4186 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4187
c583dd46
APB
4188 /* If we have an initialization value tied to the field */
4189 if (init)
4190 {
4191 /* The field is declared static */
e04a16fb 4192 if (flags & ACC_STATIC)
e04a16fb 4193 {
7525cc04
APB
4194 /* We include the field and its initialization part into
4195 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4196 walked, field initializations will be processed and
4197 fields initialized with known constants will be taken
4198 out of <clinit> and have their DECL_INITIAL set
7525cc04 4199 appropriately. */
c2952b01
APB
4200 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4201 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4202 if (TREE_OPERAND (init, 1)
4203 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4204 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4205 }
5e942c50
APB
4206 /* A non-static field declared with an immediate initialization is
4207 to be initialized in <init>, if any. This field is remembered
4208 to be processed at the time of the generation of <init>. */
c583dd46
APB
4209 else
4210 {
c2952b01
APB
4211 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4212 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4213 }
5b09b33e 4214 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4215 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4216 }
4217 }
4218 lineno = saved_lineno;
4219}
4220
c00f0fb2
APB
4221/* Generate finit$, using the list of initialized fields to populate
4222 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4223 one(s) used to initialized the field(s) caching outer context
4224 local(s). */
22eed1e6 4225
c2952b01
APB
4226static tree
4227generate_finit (class_type)
4228 tree class_type;
22eed1e6 4229{
c2952b01
APB
4230 int count = 0;
4231 tree list = TYPE_FINIT_STMT_LIST (class_type);
4232 tree mdecl, current, parms;
4233
4234 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4235 class_type, NULL_TREE,
4236 &count);
4237 CRAFTED_PARAM_LIST_FIXUP (parms);
4238 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4239 finit_identifier_node, parms);
4240 fix_method_argument_names (parms, mdecl);
4241 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4242 mdecl, NULL_TREE);
4243 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4244 start_artificial_method_body (mdecl);
4245
c2952b01 4246 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4247 java_method_add_stmt (mdecl,
4248 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4249 current));
22eed1e6 4250 end_artificial_method_body (mdecl);
c2952b01 4251 return mdecl;
22eed1e6
APB
4252}
4253
e04a16fb 4254static void
c2952b01
APB
4255add_instance_initializer (mdecl)
4256 tree mdecl;
e04a16fb 4257{
c2952b01
APB
4258 tree current;
4259 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4260 tree compound = NULL_TREE;
e04a16fb 4261
c2952b01 4262 if (stmt_list)
e04a16fb 4263 {
c2952b01
APB
4264 for (current = stmt_list; current; current = TREE_CHAIN (current))
4265 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4266
c2952b01
APB
4267 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4268 NULL_TREE, compound));
4269 }
e04a16fb
AG
4270}
4271
4272/* Shared accros method_declarator and method_header to remember the
4273 patch stage that was reached during the declaration of the method.
4274 A method DECL is built differently is there is no patch
4275 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4276 pending on the currently defined method. */
4277
4278static int patch_stage;
4279
4280/* Check the method declaration and add the method to its current
4281 class. If the argument list is known to contain incomplete types,
4282 the method is partially added and the registration will be resume
22eed1e6
APB
4283 once the method arguments resolved. If TYPE is NULL, we're dealing
4284 with a constructor. */
e04a16fb
AG
4285
4286static tree
4287method_header (flags, type, mdecl, throws)
4288 int flags;
4289 tree type, mdecl, throws;
4290{
1886c9d8 4291 tree type_wfl = NULL_TREE;
79d13333 4292 tree meth_name = NULL_TREE;
c2952b01 4293 tree current, orig_arg, this_class = NULL;
34d4df06 4294 tree id, meth;
e04a16fb 4295 int saved_lineno;
1886c9d8 4296 int constructor_ok = 0, must_chain;
c2952b01 4297 int count;
34d4df06
APB
4298
4299 if (mdecl == error_mark_node)
4300 return error_mark_node;
4301 meth = TREE_VALUE (mdecl);
4302 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4303
4304 check_modifiers_consistency (flags);
79d13333 4305
c2952b01
APB
4306 if (GET_CPC ())
4307 this_class = TREE_TYPE (GET_CPC ());
4308
4309 if (!this_class || this_class == error_mark_node)
79d13333 4310 return NULL_TREE;
e04a16fb
AG
4311
4312 /* There are some forbidden modifiers for an abstract method and its
4313 class must be abstract as well. */
22eed1e6 4314 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4315 {
4316 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4317 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4318 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4319 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4320 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4321 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4322 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4323 parse_error_context
781b0558 4324 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4325 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4326 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4327 }
c2952b01 4328
22eed1e6
APB
4329 /* Things to be checked when declaring a constructor */
4330 if (!type)
4331 {
4332 int ec = java_error_count;
4333 /* 8.6: Constructor declarations: we might be trying to define a
4334 method without specifying a return type. */
c2952b01 4335 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4336 parse_error_context
4337 (id, "Invalid method declaration, return type required");
4338 /* 8.6.3: Constructor modifiers */
4339 else
4340 {
4341 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4342 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4343 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4344 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4345 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4346 }
4347 /* If we found error here, we don't consider it's OK to tread
4348 the method definition as a constructor, for the rest of this
4349 function */
4350 if (ec == java_error_count)
4351 constructor_ok = 1;
4352 }
e04a16fb
AG
4353
4354 /* Method declared within the scope of an interface are implicitly
4355 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4356 modifiers are checked right after. */
e04a16fb
AG
4357
4358 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4359 {
4360 /* If FLAGS isn't set because of a modifier, turn the
4361 corresponding modifier WFL to NULL so we issue a warning on
4362 the obsolete use of the modifier */
4363 if (!(flags & ACC_PUBLIC))
4364 MODIFIER_WFL (PUBLIC_TK) = NULL;
4365 if (!(flags & ACC_ABSTRACT))
4366 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4367 flags |= ACC_PUBLIC;
4368 flags |= ACC_ABSTRACT;
4369 }
4370
c2952b01
APB
4371 /* Inner class can't declare static methods */
4372 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4373 {
4374 parse_error_context
4375 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4376 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4377 lang_printable_name (this_class, 0));
4378 }
4379
e04a16fb
AG
4380 /* Modifiers context reset moved up, so abstract method declaration
4381 modifiers can be later checked. */
4382
22eed1e6
APB
4383 /* Set constructor returned type to void and method name to <init>,
4384 unless we found an error identifier the constructor (in which
4385 case we retain the original name) */
4386 if (!type)
4387 {
4388 type = void_type_node;
4389 if (constructor_ok)
4390 meth_name = init_identifier_node;
4391 }
4392 else
4393 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4394
1886c9d8
APB
4395 /* Do the returned type resolution and registration if necessary */
4396 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4397
4a5f66c3
APB
4398 if (meth_name)
4399 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4400 EXPR_WFL_NODE (id) = meth_name;
4401 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4402
4403 if (must_chain)
e04a16fb 4404 {
1886c9d8
APB
4405 patch_stage = JDEP_METHOD_RETURN;
4406 register_incomplete_type (patch_stage, type_wfl, id, type);
4407 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4408 }
4409 else
1886c9d8 4410 TREE_TYPE (meth) = type;
e04a16fb
AG
4411
4412 saved_lineno = lineno;
4413 /* When defining an abstract or interface method, the curly
4414 bracket at level 1 doesn't exist because there is no function
4415 body */
4416 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4417 EXPR_WFL_LINENO (id));
4418
5e942c50
APB
4419 /* Remember the original argument list */
4420 orig_arg = TYPE_ARG_TYPES (meth);
4421
e04a16fb
AG
4422 if (patch_stage) /* includes ret type and/or all args */
4423 {
4424 jdep *jdep;
4425 meth = add_method_1 (this_class, flags, meth_name, meth);
4426 /* Patch for the return type */
4427 if (patch_stage == JDEP_METHOD_RETURN)
4428 {
4429 jdep = CLASSD_LAST (ctxp->classd_list);
4430 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4431 }
4432 /* This is the stop JDEP. METH allows the function's signature
4433 to be computed. */
4434 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4435 }
4436 else
5e942c50
APB
4437 meth = add_method (this_class, flags, meth_name,
4438 build_java_signature (meth));
4439
c2952b01
APB
4440 /* Remember final parameters */
4441 MARK_FINAL_PARMS (meth, orig_arg);
4442
5e942c50
APB
4443 /* Fix the method argument list so we have the argument name
4444 information */
4445 fix_method_argument_names (orig_arg, meth);
4446
4447 /* Register the parameter number and re-install the current line
4448 number */
e04a16fb
AG
4449 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4450 lineno = saved_lineno;
b9f7e36c
APB
4451
4452 /* Register exception specified by the `throws' keyword for
4453 resolution and set the method decl appropriate field to the list.
4454 Note: the grammar ensures that what we get here are class
4455 types. */
4456 if (throws)
4457 {
4458 throws = nreverse (throws);
4459 for (current = throws; current; current = TREE_CHAIN (current))
4460 {
4461 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4462 NULL_TREE, NULL_TREE);
4463 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4464 &TREE_VALUE (current);
4465 }
4466 DECL_FUNCTION_THROWS (meth) = throws;
4467 }
4468
e04a16fb
AG
4469 /* We set the DECL_NAME to ID so we can track the location where
4470 the function was declared. This allow us to report
4471 redefinition error accurately. When method are verified,
4472 DECL_NAME is reinstalled properly (using the content of the
4473 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4474 when Object is being defined. Constructor <init> names will be
4475 reinstalled the same way. */
c2952b01 4476 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4477 DECL_NAME (meth) = id;
22eed1e6
APB
4478
4479 /* Set the flag if we correctly processed a constructor */
4480 if (constructor_ok)
c2952b01
APB
4481 {
4482 DECL_CONSTRUCTOR_P (meth) = 1;
4483 /* Compute and store the number of artificial parameters declared
4484 for this constructor */
4485 for (count = 0, current = TYPE_FIELDS (this_class); current;
4486 current = TREE_CHAIN (current))
4487 if (FIELD_LOCAL_ALIAS (current))
4488 count++;
4489 DECL_FUNCTION_NAP (meth) = count;
4490 }
22eed1e6 4491
5e942c50
APB
4492 /* Eventually set the @deprecated tag flag */
4493 CHECK_DEPRECATED (meth);
4494
7f10c2e2
APB
4495 /* If doing xref, store column and line number information instead
4496 of the line number only. */
4497 if (flag_emit_xref)
4498 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4499
e04a16fb
AG
4500 return meth;
4501}
4502
5e942c50
APB
4503static void
4504fix_method_argument_names (orig_arg, meth)
4505 tree orig_arg, meth;
4506{
4507 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4508 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4509 {
4510 TREE_PURPOSE (arg) = this_identifier_node;
4511 arg = TREE_CHAIN (arg);
4512 }
de4c7b02 4513 while (orig_arg != end_params_node)
5e942c50
APB
4514 {
4515 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4516 orig_arg = TREE_CHAIN (orig_arg);
4517 arg = TREE_CHAIN (arg);
4518 }
4519}
4520
22eed1e6
APB
4521/* Complete the method declaration with METHOD_BODY. */
4522
4523static void
b635eb2f 4524finish_method_declaration (method_body)
22eed1e6
APB
4525 tree method_body;
4526{
79d13333
APB
4527 int flags;
4528
4529 if (!current_function_decl)
4530 return;
4531
4532 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4533
4534 /* 8.4.5 Method Body */
4535 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4536 {
4537 tree wfl = DECL_NAME (current_function_decl);
4538 parse_error_context (wfl,
4539 "%s method `%s' can't have a body defined",
4540 (METHOD_NATIVE (current_function_decl) ?
4541 "Native" : "Abstract"),
4542 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4543 method_body = NULL_TREE;
4544 }
4545 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4546 {
4547 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4548 parse_error_context
4549 (wfl,
4550 "Non native and non abstract method `%s' must have a body defined",
4551 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4552 method_body = NULL_TREE;
4553 }
4554
2c56429a
APB
4555 if (flag_emit_class_files && method_body
4556 && TREE_CODE (method_body) == NOP_EXPR
4557 && TREE_TYPE (current_function_decl)
4558 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4559 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4560
22eed1e6
APB
4561 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4562 maybe_absorb_scoping_blocks ();
4563 /* Exit function's body */
4564 exit_block ();
4565 /* Merge last line of the function with first line, directly in the
4566 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4567 if (!flag_emit_xref)
4568 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4569
4570 /* Since function's argument's list are shared, reset the
4571 ARG_FINAL_P parameter that might have been set on some of this
4572 function parameters. */
4573 UNMARK_FINAL_PARMS (current_function_decl);
4574
f099f336
APB
4575 /* So we don't have an irrelevant function declaration context for
4576 the next static block we'll see. */
4577 current_function_decl = NULL_TREE;
22eed1e6
APB
4578}
4579
4580/* Build a an error message for constructor circularity errors. */
4581
4582static char *
4583constructor_circularity_msg (from, to)
4584 tree from, to;
4585{
4586 static char string [4096];
c2e3db92 4587 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4588 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4589 free (t);
4590 return string;
4591}
4592
4593/* Verify a circular call to METH. Return 1 if an error is found, 0
4594 otherwise. */
4595
4596static int
4597verify_constructor_circularity (meth, current)
4598 tree meth, current;
4599{
4600 static tree list = NULL_TREE;
19e223db 4601 static int initialized_p;
22eed1e6 4602 tree c;
19e223db
MM
4603
4604 /* If we haven't already registered LIST with the garbage collector,
4605 do so now. */
4606 if (!initialized_p)
4607 {
4608 ggc_add_tree_root (&list, 1);
4609 initialized_p = 1;
4610 }
4611
22eed1e6
APB
4612 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4613 {
4614 if (TREE_VALUE (c) == meth)
4615 {
4616 char *t;
4617 if (list)
4618 {
4619 tree liste;
4620 list = nreverse (list);
4621 for (liste = list; liste; liste = TREE_CHAIN (liste))
4622 {
4623 parse_error_context
c63b98cd 4624 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4625 constructor_circularity_msg
4626 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4627 java_error_count--;
4628 }
4629 }
c2e3db92 4630 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4631 parse_error_context (TREE_PURPOSE (c),
4632 "%s: recursive invocation of constructor `%s'",
4633 constructor_circularity_msg (current, meth), t);
4634 free (t);
4635 list = NULL_TREE;
4636 return 1;
4637 }
4638 }
4639 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4640 {
4641 list = tree_cons (c, current, list);
4642 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4643 return 1;
4644 list = TREE_CHAIN (list);
4645 }
4646 return 0;
4647}
4648
e04a16fb
AG
4649/* Check modifiers that can be declared but exclusively */
4650
4651static void
4652check_modifiers_consistency (flags)
4653 int flags;
4654{
4655 int acc_count = 0;
4656 tree cl = NULL_TREE;
4657
e0fc4118
TT
4658 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4659 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4660 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4661 if (acc_count > 1)
4662 parse_error_context
e0fc4118
TT
4663 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4664
4665 acc_count = 0;
4666 cl = NULL_TREE;
14d075d8
TT
4667 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4668 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4669 if (acc_count > 1)
4670 parse_error_context (cl,
4671 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4672}
4673
4674/* Check the methode header METH for abstract specifics features */
4675
4676static void
4677check_abstract_method_header (meth)
4678 tree meth;
4679{
4680 int flags = get_access_flags_from_decl (meth);
4681 /* DECL_NAME might still be a WFL node */
c877974e 4682 tree name = GET_METHOD_NAME (meth);
e04a16fb 4683
2884c41e
KG
4684 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4685 ACC_ABSTRACT, "abstract method",
4686 IDENTIFIER_POINTER (name));
4687 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4688 ACC_PUBLIC, "abstract method",
4689 IDENTIFIER_POINTER (name));
e04a16fb
AG
4690
4691 check_modifiers ("Illegal modifier `%s' for interface method",
4692 flags, INTERFACE_METHOD_MODIFIERS);
4693}
4694
4695/* Create a FUNCTION_TYPE node and start augmenting it with the
4696 declared function arguments. Arguments type that can't be resolved
4697 are left as they are, but the returned node is marked as containing
4698 incomplete types. */
4699
4700static tree
4701method_declarator (id, list)
4702 tree id, list;
4703{
4704 tree arg_types = NULL_TREE, current, node;
4705 tree meth = make_node (FUNCTION_TYPE);
4706 jdep *jdep;
e04a16fb
AG
4707
4708 patch_stage = JDEP_NO_PATCH;
c2952b01 4709
34d4df06
APB
4710 if (GET_CPC () == error_mark_node)
4711 return error_mark_node;
4712
c2952b01
APB
4713 /* If we're dealing with an inner class constructor, we hide the
4714 this$<n> decl in the name field of its parameter declaration. We
4715 also might have to hide the outer context local alias
4716 initializers. Not done when the class is a toplevel class. */
4717 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4718 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4719 {
4720 tree aliases_list, type, thisn;
4721 /* First the aliases, linked to the regular parameters */
4722 aliases_list =
4723 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4724 TREE_TYPE (GET_CPC ()),
4725 NULL_TREE, NULL);
4726 list = chainon (nreverse (aliases_list), list);
4727
4728 /* Then this$<n> */
4729 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4730 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4731 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4732 list);
4733 }
e04a16fb
AG
4734
4735 for (current = list; current; current = TREE_CHAIN (current))
4736 {
c583dd46 4737 int must_chain = 0;
e04a16fb
AG
4738 tree wfl_name = TREE_PURPOSE (current);
4739 tree type = TREE_VALUE (current);
4740 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4741 tree already, arg_node;
4742 tree type_wfl = NULL_TREE;
23a79c61 4743 tree real_type;
c583dd46
APB
4744
4745 /* Obtain a suitable type for resolution, if necessary */
4746 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4747
4748 /* Process NAME, as it may specify extra dimension(s) for it */
4749 type = build_array_from_name (type, type_wfl, name, &name);
4750 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4751
23a79c61
APB
4752 real_type = GET_REAL_TYPE (type);
4753 if (TREE_CODE (real_type) == RECORD_TYPE)
4754 {
4755 real_type = promote_type (real_type);
4756 if (TREE_CODE (type) == TREE_LIST)
4757 TREE_PURPOSE (type) = real_type;
4758 }
5e942c50 4759
e04a16fb
AG
4760 /* Check redefinition */
4761 for (already = arg_types; already; already = TREE_CHAIN (already))
4762 if (TREE_PURPOSE (already) == name)
4763 {
781b0558
KG
4764 parse_error_context
4765 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4766 IDENTIFIER_POINTER (name),
e04a16fb
AG
4767 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4768 break;
4769 }
4770
4771 /* If we've an incomplete argument type, we know there is a location
4772 to patch when the type get resolved, later. */
4773 jdep = NULL;
c583dd46 4774 if (must_chain)
e04a16fb 4775 {
c583dd46
APB
4776 patch_stage = JDEP_METHOD;
4777 type = register_incomplete_type (patch_stage,
4778 type_wfl, wfl_name, type);
4779 jdep = CLASSD_LAST (ctxp->classd_list);
4780 JDEP_MISC (jdep) = id;
e04a16fb 4781 }
c583dd46 4782
c2952b01 4783 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4784 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4785 /* Remeber arguments declared final. */
4786 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4787
e04a16fb
AG
4788 if (jdep)
4789 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4790 TREE_CHAIN (arg_node) = arg_types;
4791 arg_types = arg_node;
4792 }
de4c7b02 4793 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4794 node = build_tree_list (id, meth);
4795 return node;
4796}
4797
4798static int
4799unresolved_type_p (wfl, returned)
4800 tree wfl;
4801 tree *returned;
4802
4803{
4804 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4805 {
e04a16fb 4806 if (returned)
165f37bc
APB
4807 {
4808 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4809 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4810 *returned = TREE_TYPE (decl);
4811 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4812 *returned = TREE_TYPE (GET_CPC ());
4813 else
4814 *returned = NULL_TREE;
4815 }
e04a16fb
AG
4816 return 1;
4817 }
4818 if (returned)
4819 *returned = wfl;
4820 return 0;
4821}
4822
4823/* From NAME, build a qualified identifier node using the
4824 qualification from the current package definition. */
4825
4826static tree
98a52c2c 4827parser_qualified_classname (name)
e04a16fb
AG
4828 tree name;
4829{
c2952b01
APB
4830 tree nested_class_name;
4831
98a52c2c 4832 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4833 return nested_class_name;
4834
e04a16fb 4835 if (ctxp->package)
c2952b01 4836 return merge_qualified_name (ctxp->package, name);
e04a16fb 4837 else
c2952b01 4838 return name;
e04a16fb
AG
4839}
4840
4841/* Called once the type a interface extends is resolved. Returns 0 if
4842 everything is OK. */
4843
4844static int
4845parser_check_super_interface (super_decl, this_decl, this_wfl)
4846 tree super_decl, this_decl, this_wfl;
4847{
4848 tree super_type = TREE_TYPE (super_decl);
4849
4850 /* Has to be an interface */
c2952b01 4851 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4852 {
4853 parse_error_context
4854 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4855 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4856 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4857 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4858 "interface" : "class"),
4859 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4860 return 1;
4861 }
4862
4863 /* Check scope: same package OK, other package: OK if public */
4864 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4865 return 1;
4866
4867 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4868 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4869 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4870 return 0;
4871}
4872
4873/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4874 0 if everthing is OK. */
4875
4876static int
4877parser_check_super (super_decl, this_decl, wfl)
4878 tree super_decl, this_decl, wfl;
4879{
e04a16fb
AG
4880 tree super_type = TREE_TYPE (super_decl);
4881
4882 /* SUPER should be a CLASS (neither an array nor an interface) */
4883 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4884 {
4885 parse_error_context
4886 (wfl, "Class `%s' can't subclass %s `%s'",
4887 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4888 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4889 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4890 return 1;
4891 }
4892
4893 if (CLASS_FINAL (TYPE_NAME (super_type)))
4894 {
4895 parse_error_context (wfl, "Can't subclass final classes: %s",
4896 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4897 return 1;
4898 }
4899
4900 /* Check scope: same package OK, other package: OK if public */
4901 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4902 return 1;
4903
4904 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4905 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4906 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4907 return 0;
4908}
4909
4910/* Create a new dependency list and link it (in a LIFO manner) to the
4911 CTXP list of type dependency list. */
4912
4913static void
4914create_jdep_list (ctxp)
4915 struct parser_ctxt *ctxp;
4916{
23a79c61 4917 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4918 new->first = new->last = NULL;
4919 new->next = ctxp->classd_list;
4920 ctxp->classd_list = new;
4921}
4922
4923static jdeplist *
4924reverse_jdep_list (ctxp)
4925 struct parser_ctxt *ctxp;
4926{
4927 register jdeplist *prev = NULL, *current, *next;
4928 for (current = ctxp->classd_list; current; current = next)
4929 {
4930 next = current->next;
4931 current->next = prev;
4932 prev = current;
4933 }
4934 return prev;
4935}
4936
23a79c61
APB
4937/* Create a fake pointer based on the ID stored in
4938 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4939 registered again. */
e04a16fb
AG
4940
4941static tree
23a79c61
APB
4942obtain_incomplete_type (type_name)
4943 tree type_name;
e04a16fb 4944{
23a79c61
APB
4945 tree ptr, name;
4946
4947 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4948 name = EXPR_WFL_NODE (type_name);
4949 else if (INCOMPLETE_TYPE_P (type_name))
4950 name = TYPE_NAME (type_name);
4951 else
4952 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4953
4954 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4955 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4956 break;
4957
4958 if (!ptr)
4959 {
e04a16fb 4960 push_obstacks (&permanent_obstack, &permanent_obstack);
78d21f92
PB
4961 BUILD_PTR_FROM_NAME (ptr, name);
4962 layout_type (ptr);
e04a16fb
AG
4963 pop_obstacks ();
4964 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4965 ctxp->incomplete_class = ptr;
4966 }
4967
4968 return ptr;
4969}
4970
4971/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4972 non NULL instead of computing a new fake type based on WFL. The new
4973 dependency is inserted in the current type dependency list, in FIFO
4974 manner. */
4975
4976static tree
4977register_incomplete_type (kind, wfl, decl, ptr)
4978 int kind;
4979 tree wfl, decl, ptr;
4980{
23a79c61 4981 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4982
e04a16fb
AG
4983 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4984 ptr = obtain_incomplete_type (wfl);
4985
4986 JDEP_KIND (new) = kind;
4987 JDEP_DECL (new) = decl;
4988 JDEP_SOLV (new) = ptr;
4989 JDEP_WFL (new) = wfl;
4990 JDEP_CHAIN (new) = NULL;
4991 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
4992 /* For some dependencies, set the enclosing class of the current
4993 class to be the enclosing context */
4994 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
165f37bc
APB
4995 && GET_ENCLOSING_CPC ())
4996 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4997 else
324ed8fd 4998 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
4999 JDEP_GET_PATCH (new) = (tree *)NULL;
5000
5001 JDEP_INSERT (ctxp->classd_list, new);
5002
5003 return ptr;
5004}
5005
5006void
5007java_check_circular_reference ()
5008{
5009 tree current;
5010 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5011 {
5012 tree type = TREE_TYPE (current);
e920ebc9 5013 if (CLASS_INTERFACE (current))
e04a16fb
AG
5014 {
5015 /* Check all interfaces this class extends */
5016 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5017 int n, i;
5018
5019 if (!basetype_vec)
5020 return;
5021 n = TREE_VEC_LENGTH (basetype_vec);
5022 for (i = 0; i < n; i++)
5023 {
5024 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5025 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5026 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5027 parse_error_context (lookup_cl (current),
5028 "Cyclic interface inheritance");
5029 }
5030 }
5031 else
5032 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5033 parse_error_context (lookup_cl (current),
c2952b01
APB
5034 "Cyclic class inheritance%s",
5035 (cyclic_inheritance_report ?
5036 cyclic_inheritance_report : ""));
5037 }
5038}
5039
5040/* Augment the parameter list PARM with parameters crafted to
5041 initialize outer context locals aliases. Through ARTIFICIAL, a
5042 count is kept of the number of crafted parameters. MODE governs
5043 what eventually gets created: something suitable for a function
5044 creation or a function invocation, either the constructor or
c00f0fb2 5045 finit$. */
c2952b01
APB
5046
5047static tree
5048build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5049 int mode;
5050 tree class_type, parm;
5051 int *artificial;
5052{
5053 tree field;
da632f2c
APB
5054 tree additional_parms = NULL_TREE;
5055
c2952b01
APB
5056 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5057 if (FIELD_LOCAL_ALIAS (field))
5058 {
63ad61ed 5059 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01
APB
5060 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5061
5062 switch (mode)
5063 {
5064 case AIPL_FUNCTION_DECLARATION:
5065 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5066 purpose = build_wfl_node (get_identifier (buffer));
5067 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5068 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5069 else
5070 value = TREE_TYPE (field);
5071 break;
5072
5073 case AIPL_FUNCTION_CREATION:
5074 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5075 purpose = get_identifier (buffer);
5076 value = TREE_TYPE (field);
5077 break;
5078
5079 case AIPL_FUNCTION_FINIT_INVOCATION:
5080 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5081 /* Now, this is wrong. purpose should always be the NAME
5082 of something and value its matching value (decl, type,
5083 etc...) FIXME -- but there is a lot to fix. */
5084
5085 /* When invoked for this kind of operation, we already
5086 know whether a field is used or not. */
5087 purpose = TREE_TYPE (field);
5088 value = build_wfl_node (get_identifier (buffer));
5089 break;
5090
5091 case AIPL_FUNCTION_CTOR_INVOCATION:
5092 /* There are two case: the constructor invokation happends
5093 outside the local inner, in which case, locales from the outer
5094 context are directly used.
5095
5096 Otherwise, we fold to using the alias directly. */
5097 if (class_type == current_class)
5098 value = field;
5099 else
5100 {
5101 name = get_identifier (&buffer[4]);
5102 value = IDENTIFIER_LOCAL_VALUE (name);
5103 }
5104 break;
5105 }
da632f2c 5106 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5107 if (artificial)
5108 *artificial +=1;
5109 }
da632f2c
APB
5110 if (additional_parms)
5111 {
5112 if (ANONYMOUS_CLASS_P (class_type)
5113 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5114 additional_parms = nreverse (additional_parms);
5115 parm = chainon (additional_parms, parm);
5116 }
5117
5118 return parm;
c2952b01
APB
5119}
5120
5121/* Craft a constructor for CLASS_DECL -- what we should do when none
5122 where found. ARGS is non NULL when a special signature must be
5123 enforced. This is the case for anonymous classes. */
5124
5125static void
5126craft_constructor (class_decl, args)
5127 tree class_decl, args;
5128{
5129 tree class_type = TREE_TYPE (class_decl);
5130 tree parm = NULL_TREE;
5131 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5132 ACC_PUBLIC : 0);
5133 int i = 0, artificial = 0;
5134 tree decl, ctor_name;
5135 char buffer [80];
5136
5137 push_obstacks (&permanent_obstack, &permanent_obstack);
5138
5139 /* The constructor name is <init> unless we're dealing with an
5140 anonymous class, in which case the name will be fixed after having
5141 be expanded. */
5142 if (ANONYMOUS_CLASS_P (class_type))
5143 ctor_name = DECL_NAME (class_decl);
5144 else
5145 ctor_name = init_identifier_node;
5146
5147 /* If we're dealing with an inner class constructor, we hide the
5148 this$<n> decl in the name field of its parameter declaration. */
5149 if (PURE_INNER_CLASS_TYPE_P (class_type))
5150 {
5151 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5152 parm = tree_cons (build_current_thisn (class_type),
5153 build_pointer_type (type), parm);
5154
5155 /* Some more arguments to be hidden here. The values of the local
5156 variables of the outer context that the inner class needs to see. */
5157 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5158 class_type, parm,
5159 &artificial);
5160 }
5161
5162 /* Then if there are any args to be enforced, enforce them now */
5163 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5164 {
5165 sprintf (buffer, "parm%d", i++);
5166 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5167 }
c2952b01
APB
5168
5169 CRAFTED_PARAM_LIST_FIXUP (parm);
5170 decl = create_artificial_method (class_type, flags, void_type_node,
5171 ctor_name, parm);
5172 fix_method_argument_names (parm, decl);
5173 /* Now, mark the artificial parameters. */
5174 DECL_FUNCTION_NAP (decl) = artificial;
5175
5176 pop_obstacks ();
5177 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5178}
5179
c2952b01 5180
e920ebc9
APB
5181/* Fix the constructors. This will be called right after circular
5182 references have been checked. It is necessary to fix constructors
5183 early even if no code generation will take place for that class:
5184 some generated constructor might be required by the class whose
5185 compilation triggered this one to be simply loaded. */
5186
5187void
5188java_fix_constructors ()
5189{
5190 tree current;
5191
5192 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5193 {
e920ebc9
APB
5194 tree class_type = TREE_TYPE (current);
5195 int saw_ctor = 0;
c2952b01
APB
5196 tree decl;
5197
5198 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5199 continue;
e920ebc9
APB
5200
5201 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5202 {
5203 if (DECL_CONSTRUCTOR_P (decl))
5204 {
5205 fix_constructors (decl);
5206 saw_ctor = 1;
5207 }
5208 }
5209
c2952b01
APB
5210 /* Anonymous class constructor can't be generated that early. */
5211 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5212 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5213 }
5214}
5215
23a79c61
APB
5216/* safe_layout_class just makes sure that we can load a class without
5217 disrupting the current_class, input_file, lineno, etc, information
5218 about the class processed currently. */
5219
e04a16fb
AG
5220void
5221safe_layout_class (class)
5222 tree class;
5223{
5224 tree save_current_class = current_class;
3b304f5b 5225 const char *save_input_filename = input_filename;
e04a16fb 5226 int save_lineno = lineno;
5e942c50 5227
e04a16fb 5228 push_obstacks (&permanent_obstack, &permanent_obstack);
5e942c50 5229
e04a16fb
AG
5230 layout_class (class);
5231 pop_obstacks ();
5e942c50 5232
e04a16fb
AG
5233 current_class = save_current_class;
5234 input_filename = save_input_filename;
5235 lineno = save_lineno;
5236 CLASS_LOADED_P (class) = 1;
5237}
5238
5239static tree
5240jdep_resolve_class (dep)
5241 jdep *dep;
5242{
5243 tree decl;
5244
23a79c61
APB
5245 if (JDEP_RESOLVED_P (dep))
5246 decl = JDEP_RESOLVED_DECL (dep);
5247 else
e04a16fb 5248 {
c2952b01 5249 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5250 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5251 JDEP_RESOLVED (dep, decl);
5252 }
23a79c61 5253
e04a16fb 5254 if (!decl)
23a79c61 5255 complete_class_report_errors (dep);
1e12ab9b 5256 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5257 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5258 return decl;
5259}
5260
5261/* Complete unsatisfied class declaration and their dependencies */
5262
5263void
5264java_complete_class ()
5265{
e04a16fb
AG
5266 tree cclass;
5267 jdeplist *cclassd;
5268 int error_found;
b67d701b 5269 tree type;
e04a16fb
AG
5270
5271 push_obstacks (&permanent_obstack, &permanent_obstack);
5272
9a7ab4b3 5273 /* Process imports */
e04a16fb 5274 process_imports ();
e04a16fb
AG
5275
5276 /* Rever things so we have the right order */
5277 ctxp->class_list = nreverse (ctxp->class_list);
5278 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5279
e04a16fb
AG
5280 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5281 cclass && cclassd;
5282 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5283 {
5284 jdep *dep;
5285 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5286 {
5287 tree decl;
e04a16fb
AG
5288 if (!(decl = jdep_resolve_class (dep)))
5289 continue;
5290
5291 /* Now it's time to patch */
5292 switch (JDEP_KIND (dep))
5293 {
5294 case JDEP_SUPER:
5295 /* Simply patch super */
5296 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5297 continue;
5298 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5299 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5300 break;
5301
5302 case JDEP_FIELD:
5303 {
5304 /* We do part of the job done in add_field */
5305 tree field_decl = JDEP_DECL (dep);
5306 tree field_type = TREE_TYPE (decl);
5307 push_obstacks (&permanent_obstack, &permanent_obstack);
e04a16fb 5308 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb
AG
5309 field_type = promote_type (field_type);
5310 pop_obstacks ();
5311 TREE_TYPE (field_decl) = field_type;
5e942c50 5312 DECL_ALIGN (field_decl) = 0;
11cf4d18 5313 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5314 layout_decl (field_decl, 0);
e04a16fb
AG
5315 SOURCE_FRONTEND_DEBUG
5316 (("Completed field/var decl `%s' with `%s'",
5317 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5318 IDENTIFIER_POINTER (DECL_NAME (decl))));
5319 break;
5320 }
5321 case JDEP_METHOD: /* We start patching a method */
5322 case JDEP_METHOD_RETURN:
5323 error_found = 0;
5324 while (1)
5325 {
5326 if (decl)
5327 {
b67d701b
PB
5328 type = TREE_TYPE(decl);
5329 if (TREE_CODE (type) == RECORD_TYPE)
5330 type = promote_type (type);
e04a16fb
AG
5331 JDEP_APPLY_PATCH (dep, type);
5332 SOURCE_FRONTEND_DEBUG
5333 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5334 "Completing fct `%s' with ret type `%s'":
5335 "Completing arg `%s' with type `%s'"),
5336 IDENTIFIER_POINTER (EXPR_WFL_NODE
5337 (JDEP_DECL_WFL (dep))),
5338 IDENTIFIER_POINTER (DECL_NAME (decl))));
5339 }
5340 else
5341 error_found = 1;
5342 dep = JDEP_CHAIN (dep);
5343 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5344 break;
5345 else
5346 decl = jdep_resolve_class (dep);
5347 }
5348 if (!error_found)
5349 {
5350 tree mdecl = JDEP_DECL (dep), signature;
5351 push_obstacks (&permanent_obstack, &permanent_obstack);
165f37bc
APB
5352 /* Recompute and reset the signature, check first that
5353 all types are now defined. If they're not,
5354 dont build the signature. */
5355 if (check_method_types_complete (mdecl))
5356 {
5357 signature = build_java_signature (TREE_TYPE (mdecl));
5358 set_java_signature (TREE_TYPE (mdecl), signature);
5359 }
e04a16fb
AG
5360 pop_obstacks ();
5361 }
5362 else
5363 continue;
5364 break;
5365
5366 case JDEP_INTERFACE:
5367 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5368 JDEP_WFL (dep)))
5369 continue;
5370 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5371 break;
5372
b67d701b 5373 case JDEP_PARM:
e04a16fb 5374 case JDEP_VARIABLE:
b67d701b
PB
5375 type = TREE_TYPE(decl);
5376 if (TREE_CODE (type) == RECORD_TYPE)
5377 type = promote_type (type);
5378 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5379 break;
5380
5381 case JDEP_TYPE:
5382 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5383 SOURCE_FRONTEND_DEBUG
5384 (("Completing a random type dependency on a '%s' node",
5385 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5386 break;
5387
b9f7e36c 5388 case JDEP_EXCEPTION:
c877974e
APB
5389 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5390 SOURCE_FRONTEND_DEBUG
5391 (("Completing `%s' `throws' argument node",
5392 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5393 break;
5394
c2952b01
APB
5395 case JDEP_ANONYMOUS:
5396 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5397 break;
5398
e04a16fb 5399 default:
0a2138e2
APB
5400 fatal ("Can't handle patch code %d - java_complete_class",
5401 JDEP_KIND (dep));
e04a16fb
AG
5402 }
5403 }
5404 }
5405 pop_obstacks ();
5406 return;
5407}
5408
5409/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5410 array. */
5411
5412static tree
c2952b01
APB
5413resolve_class (enclosing, class_type, decl, cl)
5414 tree enclosing, class_type, decl, cl;
e04a16fb 5415{
49f48c71
KG
5416 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5417 const char *base = name;
78d21f92
PB
5418 tree resolved_type = TREE_TYPE (class_type);
5419 tree resolved_type_decl;
e04a16fb 5420
78d21f92
PB
5421 if (resolved_type != NULL_TREE)
5422 {
5423 tree resolved_type_decl = TYPE_NAME (resolved_type);
5424 if (resolved_type_decl == NULL_TREE
5425 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5426 {
5427 resolved_type_decl = build_decl (TYPE_DECL,
5428 TYPE_NAME (class_type),
5429 resolved_type);
5430 }
5431 return resolved_type_decl;
5432 }
5433
e04a16fb
AG
5434 /* 1- Check to see if we have an array. If true, find what we really
5435 want to resolve */
5436 while (name[0] == '[')
5437 name++;
5438 if (base != name)
34d4df06
APB
5439 {
5440 TYPE_NAME (class_type) = get_identifier (name);
5441 WFL_STRIP_BRACKET (cl, cl);
5442 }
e04a16fb
AG
5443
5444 /* 2- Resolve the bare type */
c2952b01
APB
5445 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5446 decl, cl)))
e04a16fb
AG
5447 return NULL_TREE;
5448 resolved_type = TREE_TYPE (resolved_type_decl);
5449
5450 /* 3- If we have and array, reconstruct the array down to its nesting */
5451 if (base != name)
5452 {
5453 while (base != name)
5454 {
5455 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5456 resolved_type = promote_type (resolved_type);
5457 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5458 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5459 name--;
5460 }
5461 /* Build a fake decl for this, since this is what is expected to
5462 be returned. */
5463 resolved_type_decl =
5464 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5465 /* Figure how those two things are important for error report. FIXME */
5466 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5467 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5468 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5469 }
78d21f92 5470 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5471 return resolved_type_decl;
5472}
5473
5474/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5475 are used to report error messages. */
5476
78d21f92 5477tree
c2952b01
APB
5478do_resolve_class (enclosing, class_type, decl, cl)
5479 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5480{
5481 tree new_class_decl;
e04a16fb
AG
5482
5483 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5484 it is changed by find_in_imports{_on_demand} and (but it doesn't
5485 really matter) qualify_and_find */
e04a16fb 5486
c2952b01
APB
5487 /* 0- Search in the current class as an inner class */
5488
5489 /* Maybe some code here should be added to load the class or
5490 something, at least if the class isn't an inner class and ended
5491 being loaded from class file. FIXME. */
a40d21da
APB
5492 while (enclosing)
5493 {
5494 tree name;
5495
5496 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5497 return new_class_decl;
5498
0c2b8145
APB
5499 /* Explore enclosing contexts. */
5500 while (INNER_CLASS_DECL_P (enclosing))
5501 {
5502 enclosing = DECL_CONTEXT (enclosing);
5503 if ((new_class_decl = find_as_inner_class (enclosing,
5504 class_type, cl)))
5505 return new_class_decl;
5506 }
5507
a40d21da
APB
5508 /* Now go to the upper classes, bail out if necessary. */
5509 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5510 if (!enclosing || enclosing == object_type_node)
5511 break;
5512
5513 if (TREE_CODE (enclosing) == RECORD_TYPE)
5514 {
5515 enclosing = TYPE_NAME (enclosing);
5516 continue;
5517 }
5518
5519 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5520 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5521 else
5522 name = enclosing;
5523 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5524 }
c2952b01 5525
9a7ab4b3
APB
5526 /* 1- Check for the type in single imports. This will change
5527 TYPE_NAME() if something relevant is found */
5528 find_in_imports (class_type);
e04a16fb 5529
9a7ab4b3 5530 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5531 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5532 {
5533 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5534 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5535 load_class (TYPE_NAME (class_type), 0);
5536 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5537 }
5538
9a7ab4b3
APB
5539 /* 3- Search according to the current package definition */
5540 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5541 {
5542 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5543 TYPE_NAME (class_type))))
5544 return new_class_decl;
5545 }
5546
5547 /* 4- Check the import on demands. Don't allow bar.baz to be
5548 imported from foo.* */
5549 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5550 if (find_in_imports_on_demand (class_type))
5551 return NULL_TREE;
5552
5553 /* If found in find_in_imports_on_demant, the type has already been
5554 loaded. */
5555 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5556 return new_class_decl;
5557
5558 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5559 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5560 {
ee07f4f4 5561 tree package;
d6baf6f5
APB
5562
5563 /* If there is a current package (ctxp->package), it's the first
5564 element of package_list and we can skip it. */
5565 for (package = (ctxp->package ?
5566 TREE_CHAIN (package_list) : package_list);
5567 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5568 if ((new_class_decl = qualify_and_find (class_type,
5569 TREE_PURPOSE (package),
5570 TYPE_NAME (class_type))))
5571 return new_class_decl;
5572 }
5573
5574 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5575 load_class (TYPE_NAME (class_type), 0);
5576 if (check_pkg_class_access (TYPE_NAME (class_type),
5577 (cl ? cl : lookup_cl (decl))))
5578 return NULL_TREE;
5579
9a7ab4b3 5580 /* 6- Last call for a resolution */
e04a16fb
AG
5581 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5582}
5583
9a7ab4b3
APB
5584static tree
5585qualify_and_find (class_type, package, name)
5586 tree class_type, package, name;
5587{
5588 tree new_qualified = merge_qualified_name (package, name);
5589 tree new_class_decl;
5590
5591 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5592 load_class (new_qualified, 0);
5593 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5594 {
5595 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5596 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5597 load_class (new_qualified, 0);
5598 TYPE_NAME (class_type) = new_qualified;
5599 return IDENTIFIER_CLASS_VALUE (new_qualified);
5600 }
5601 return NULL_TREE;
5602}
5603
e04a16fb 5604/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5605 parsed class). Return a decl node. This function is meant to be
5606 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5607
5608static tree
c877974e
APB
5609resolve_and_layout (something, cl)
5610 tree something;
e04a16fb
AG
5611 tree cl;
5612{
34d4df06 5613 tree decl, decl_type;
c877974e 5614
23a79c61
APB
5615 /* Don't do that on the current class */
5616 if (something == current_class)
5617 return TYPE_NAME (current_class);
c877974e 5618
23a79c61 5619 /* Don't do anything for void and other primitive types */
c877974e
APB
5620 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5621 return NULL_TREE;
5622
23a79c61
APB
5623 /* Pointer types can be reall pointer types or fake pointers. When
5624 finding a real pointer, recheck for primitive types */
5625 if (TREE_CODE (something) == POINTER_TYPE)
5626 {
5627 if (TREE_TYPE (something))
5628 {
5629 something = TREE_TYPE (something);
5630 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5631 return NULL_TREE;
5632 }
5633 else
5634 something = TYPE_NAME (something);
5635 }
5636
5637 /* Don't do anything for arrays of primitive types */
5638 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5639 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5640 return NULL_TREE;
5641
c2952b01
APB
5642 /* Something might be a WFL */
5643 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5644 something = EXPR_WFL_NODE (something);
5645
5646 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5647 TYPE_DECL or a real TYPE */
5648 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5649 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5650 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5651
23a79c61
APB
5652 if (!(decl = resolve_no_layout (something, cl)))
5653 return NULL_TREE;
5654
5655 /* Resolve and layout if necessary */
34d4df06
APB
5656 decl_type = TREE_TYPE (decl);
5657 layout_class_methods (decl_type);
5658 /* Check methods */
5659 if (CLASS_FROM_SOURCE_P (decl_type))
5660 java_check_methods (decl);
5661 /* Layout the type if necessary */
5662 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5663 safe_layout_class (decl_type);
23a79c61 5664
e04a16fb
AG
5665 return decl;
5666}
5667
5668/* Resolve a class, returns its decl but doesn't perform any
5669 layout. The current parsing context is saved and restored */
5670
5671static tree
5672resolve_no_layout (name, cl)
5673 tree name, cl;
5674{
5675 tree ptr, decl;
5676 BUILD_PTR_FROM_NAME (ptr, name);
5677 java_parser_context_save_global ();
c2952b01 5678 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5679 java_parser_context_restore_global ();
5680
5681 return decl;
5682}
5683
23a79c61
APB
5684/* Called when reporting errors. Skip leader '[' in a complex array
5685 type description that failed to be resolved. */
e04a16fb 5686
49f48c71 5687static const char *
e04a16fb 5688purify_type_name (name)
49f48c71 5689 const char *name;
e04a16fb
AG
5690{
5691 while (*name && *name == '[')
5692 name++;
5693 return name;
5694}
5695
5696/* The type CURRENT refers to can't be found. We print error messages. */
5697
5698static void
5699complete_class_report_errors (dep)
5700 jdep *dep;
5701{
49f48c71 5702 const char *name;
23a79c61
APB
5703
5704 if (!JDEP_WFL (dep))
5705 return;
5706
5707 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5708 switch (JDEP_KIND (dep))
5709 {
5710 case JDEP_SUPER:
5711 parse_error_context
5712 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5713 purify_type_name (name),
e04a16fb
AG
5714 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5715 break;
5716 case JDEP_FIELD:
5717 parse_error_context
5718 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5719 purify_type_name (name),
e04a16fb
AG
5720 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5721 break;
5722 case JDEP_METHOD: /* Covers arguments */
5723 parse_error_context
781b0558 5724 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5725 purify_type_name (name),
e04a16fb
AG
5726 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5727 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5728 break;
5729 case JDEP_METHOD_RETURN: /* Covers return type */
5730 parse_error_context
781b0558 5731 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5732 purify_type_name (name),
e04a16fb
AG
5733 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5734 break;
5735 case JDEP_INTERFACE:
5736 parse_error_context
5737 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5738 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5739 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5740 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5741 break;
5742 case JDEP_VARIABLE:
5743 parse_error_context
781b0558 5744 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5745 purify_type_name (IDENTIFIER_POINTER
5746 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5747 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5748 break;
b9f7e36c
APB
5749 case JDEP_EXCEPTION: /* As specified by `throws' */
5750 parse_error_context
5751 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5752 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5753 break;
0a2138e2
APB
5754 default:
5755 /* Fix for -Wall. Just break doing nothing. The error will be
5756 caught later */
5757 break;
e04a16fb
AG
5758 }
5759}
5760
22eed1e6
APB
5761/* Return a static string containing the DECL prototype string. If
5762 DECL is a constructor, use the class name instead of the form
5763 <init> */
5764
49f48c71 5765static const char *
22eed1e6
APB
5766get_printable_method_name (decl)
5767 tree decl;
5768{
49f48c71 5769 const char *to_return;
9ee9b555 5770 tree name = NULL_TREE;
22eed1e6
APB
5771
5772 if (DECL_CONSTRUCTOR_P (decl))
5773 {
5774 name = DECL_NAME (decl);
5e942c50 5775 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5776 }
5777
5778 to_return = lang_printable_name (decl, 0);
5779 if (DECL_CONSTRUCTOR_P (decl))
5780 DECL_NAME (decl) = name;
5781
5782 return to_return;
5783}
5784
5e942c50
APB
5785/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5786 nevertheless needs to be verfied, 1 otherwise. */
5787
5788static int
5789reset_method_name (method)
5790 tree method;
5791{
c2952b01 5792 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5793 {
5794 /* NAME is just the plain name when Object is being defined */
5795 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5796 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5797 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5798 return 0;
5799 }
5800 else
5801 return 1;
5802}
5803
c877974e
APB
5804/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5805
5806tree
5807java_get_real_method_name (method_decl)
5808 tree method_decl;
5809{
5810 tree method_name = DECL_NAME (method_decl);
5811 if (DECL_CONSTRUCTOR_P (method_decl))
5812 return init_identifier_node;
82371d41
APB
5813
5814 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5815 and still can be a constructor. FIXME */
5816
23a79c61
APB
5817 /* Don't confuse method only bearing the name of their class as
5818 constructors */
82371d41
APB
5819 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5820 && ctxp
c2952b01 5821 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5822 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5823 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5824 return init_identifier_node;
5825 else
5826 return EXPR_WFL_NODE (method_name);
5827}
5828
22eed1e6
APB
5829/* Track method being redefined inside the same class. As a side
5830 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5831 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5832
e04a16fb
AG
5833static int
5834check_method_redefinition (class, method)
5835 tree class, method;
5836{
5837 tree redef, name;
5838 tree cl = DECL_NAME (method);
c3f2a476 5839 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
c00f0fb2 5840 /* decl name of artificial <clinit> and finit$ doesn't need to be
ba179f9f 5841 fixed and checked */
5e942c50
APB
5842
5843 /* Reset the method name before running the check. If it returns 1,
5844 the method doesn't need to be verified with respect to method
5845 redeclaration and we return 0 */
5846 if (reset_method_name (method))
e04a16fb 5847 return 0;
5e942c50
APB
5848
5849 name = DECL_NAME (method);
e04a16fb
AG
5850 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5851 {
c3f2a476 5852 if (redef == method)
e04a16fb 5853 break;
c3f2a476
APB
5854 if (DECL_NAME (redef) == name
5855 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5856 {
22eed1e6
APB
5857 parse_error_context
5858 (cl, "Duplicate %s declaration `%s'",
5859 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5860 get_printable_method_name (redef));
e04a16fb
AG
5861 return 1;
5862 }
5863 }
5864 return 0;
5865}
5866
1175b9b4
TT
5867/* Return 1 if check went ok, 0 otherwise. */
5868static int
d77613be
APB
5869check_abstract_method_definitions (do_interface, class_decl, type)
5870 int do_interface;
5871 tree class_decl, type;
5872{
5873 tree class = TREE_TYPE (class_decl);
5874 tree method, end_type;
1175b9b4 5875 int ok = 1;
d77613be
APB
5876
5877 end_type = (do_interface ? object_type_node : type);
5878 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5879 {
5880 tree other_super, other_method, method_sig, method_name;
5881 int found = 0;
165f37bc 5882 int end_type_reached = 0;
d77613be
APB
5883
5884 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5885 continue;
5886
5887 /* Now verify that somewhere in between TYPE and CLASS,
5888 abstract method METHOD gets a non abstract definition
5889 that is inherited by CLASS. */
5890
5891 method_sig = build_java_signature (TREE_TYPE (method));
5892 method_name = DECL_NAME (method);
5893 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5894 method_name = EXPR_WFL_NODE (method_name);
5895
165f37bc
APB
5896 other_super = class;
5897 do {
5898 if (other_super == end_type)
5899 end_type_reached = 1;
5900
5901 /* Method search */
5902 for (other_method = TYPE_METHODS (other_super); other_method;
5903 other_method = TREE_CHAIN (other_method))
5904 {
5905 tree s = build_java_signature (TREE_TYPE (other_method));
5906 tree other_name = DECL_NAME (other_method);
5907
5908 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5909 other_name = EXPR_WFL_NODE (other_name);
5910 if (!DECL_CLINIT_P (other_method)
5911 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5912 && method_name == other_name
5913 && method_sig == s
5914 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5915 {
5916 found = 1;
5917 break;
5918 }
5919 }
5920 other_super = CLASSTYPE_SUPER (other_super);
5921 } while (!end_type_reached);
5922
d77613be
APB
5923 /* Report that abstract METHOD didn't find an implementation
5924 that CLASS can use. */
5925 if (!found)
5926 {
c2e3db92 5927 char *t = xstrdup (lang_printable_name
d77613be
APB
5928 (TREE_TYPE (TREE_TYPE (method)), 0));
5929 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5930 tree saved_wfl = NULL_TREE;
5931
5932 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5933 {
5934 saved_wfl = DECL_NAME (method);
5935 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5936 }
5937
5938 parse_error_context
5939 (lookup_cl (class_decl),
781b0558 5940 "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
5941 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5942 t, lang_printable_name (method, 0),
5943 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5944 "interface" : "class"),
5945 IDENTIFIER_POINTER (ccn),
5946 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5947 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5948 ok = 0;
d77613be 5949 free (t);
1175b9b4 5950
d77613be
APB
5951 if (saved_wfl)
5952 DECL_NAME (method) = saved_wfl;
5953 }
5954 }
1175b9b4
TT
5955
5956 if (ok && do_interface)
5957 {
5958 /* Check for implemented interfaces. */
5959 int i;
5960 tree vector = TYPE_BINFO_BASETYPES (type);
5961 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5962 {
5963 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5964 ok = check_abstract_method_definitions (1, class_decl, super);
5965 }
5966 }
5967
5968 return ok;
d77613be
APB
5969}
5970
614eaae0 5971/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5972 methods. */
5973
5974static void
5975java_check_abstract_method_definitions (class_decl)
5976 tree class_decl;
5977{
5978 tree class = TREE_TYPE (class_decl);
5979 tree super, vector;
5980 int i;
5981
5982 if (CLASS_ABSTRACT (class_decl))
5983 return;
5984
5985 /* Check for inherited types */
165f37bc
APB
5986 super = class;
5987 do {
5988 super = CLASSTYPE_SUPER (super);
5989 check_abstract_method_definitions (0, class_decl, super);
5990 } while (super != object_type_node);
d77613be
APB
5991
5992 /* Check for implemented interfaces. */
5993 vector = TYPE_BINFO_BASETYPES (class);
5994 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5995 {
5996 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5997 check_abstract_method_definitions (1, class_decl, super);
5998 }
5999}
6000
165f37bc
APB
6001/* Check all the types method DECL uses and return 1 if all of them
6002 are now complete, 0 otherwise. This is used to check whether its
6003 safe to build a method signature or not. */
6004
6005static int
6006check_method_types_complete (decl)
6007 tree decl;
6008{
6009 tree type = TREE_TYPE (decl);
6010 tree args;
6011
6012 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6013 return 0;
6014
6015 args = TYPE_ARG_TYPES (type);
6016 if (TREE_CODE (type) == METHOD_TYPE)
6017 args = TREE_CHAIN (args);
6018 for (; args != end_params_node; args = TREE_CHAIN (args))
6019 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6020 return 0;
6021
6022 return 1;
6023}
6024
34d4df06
APB
6025/* Visible interface to check methods contained in CLASS_DECL */
6026
6027void
6028java_check_methods (class_decl)
6029 tree class_decl;
6030{
6031 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6032 return;
6033
6034 if (CLASS_INTERFACE (class_decl))
6035 java_check_abstract_methods (class_decl);
6036 else
6037 java_check_regular_methods (class_decl);
6038
6039 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6040}
6041
d77613be
APB
6042/* Check all the methods of CLASS_DECL. Methods are first completed
6043 then checked according to regular method existance rules. If no
6044 constructor for CLASS_DECL were encountered, then build its
6045 declaration. */
e04a16fb
AG
6046
6047static void
6048java_check_regular_methods (class_decl)
6049 tree class_decl;
6050{
c2952b01 6051 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6052 tree method;
6053 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 6054 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
6055 tree mthrows;
6056
6057 /* It is not necessary to check methods defined in java.lang.Object */
6058 if (class == object_type_node)
6059 return;
e04a16fb 6060
23a79c61
APB
6061 if (!TYPE_NVIRTUALS (class))
6062 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6063
6064 /* Should take interfaces into account. FIXME */
6065 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6066 {
5e942c50 6067 tree sig;
e04a16fb
AG
6068 tree method_wfl = DECL_NAME (method);
6069 int aflags;
6070
5e942c50
APB
6071 /* If we previously found something and its name was saved,
6072 reinstall it now */
6073 if (found && saved_found_wfl)
ba179f9f
APB
6074 {
6075 DECL_NAME (found) = saved_found_wfl;
6076 saved_found_wfl = NULL_TREE;
6077 }
5e942c50 6078
e04a16fb
AG
6079 /* Check for redefinitions */
6080 if (check_method_redefinition (class, method))
6081 continue;
6082
22eed1e6
APB
6083 /* If we see one constructor a mark so we don't generate the
6084 default one. Also skip other verifications: constructors
6085 can't be inherited hence hiden or overriden */
6086 if (DECL_CONSTRUCTOR_P (method))
6087 {
6088 saw_constructor = 1;
6089 continue;
6090 }
6091
c877974e
APB
6092 /* We verify things thrown by the method. They must inherits from
6093 java.lang.Throwable */
6094 for (mthrows = DECL_FUNCTION_THROWS (method);
6095 mthrows; mthrows = TREE_CHAIN (mthrows))
6096 {
6097 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6098 parse_error_context
781b0558 6099 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6100 IDENTIFIER_POINTER
6101 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6102 }
6103
e04a16fb 6104 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6105 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6106
c2952b01
APB
6107 /* Inner class can't declare static methods */
6108 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6109 {
6110 char *t = xstrdup (lang_printable_name (class, 0));
6111 parse_error_context
6112 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6113 lang_printable_name (method, 0), t);
6114 free (t);
6115 }
6116
5e942c50 6117 /* Nothing overrides or it's a private method. */
aabd7048 6118 if (!found)
5e942c50 6119 continue;
aabd7048
PB
6120 if (METHOD_PRIVATE (found))
6121 {
6122 found = NULL_TREE;
6123 continue;
6124 }
5e942c50
APB
6125
6126 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6127 We set it temporarily for the sake of the error report. */
6128 saved_found_wfl = DECL_NAME (found);
6129 reset_method_name (found);
6130
614eaae0
APB
6131 /* If `found' is declared in an interface, make sure the
6132 modifier matches. */
6133 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6134 && clinit_identifier_node != DECL_NAME (found)
6135 && !METHOD_PUBLIC (method))
6136 {
6137 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6138 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6139 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6140 lang_printable_name (method, 0),
6141 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6142 }
6143
e04a16fb
AG
6144 /* Can't override a method with the same name and different return
6145 types. */
6146 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6147 {
614eaae0
APB
6148 char *t = xstrdup
6149 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6150 parse_error_context
7f10c2e2 6151 (method_wfl,
b9f7e36c 6152 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6153 lang_printable_name (found, 0), t,
b9f7e36c
APB
6154 IDENTIFIER_POINTER
6155 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6156 free (t);
6157 }
e04a16fb 6158
7f10c2e2
APB
6159 aflags = get_access_flags_from_decl (found);
6160 /* If the method has default, access in an other package, then
6161 issue a warning that the current method doesn't override the
6162 one that was found elsewhere. Do not issue this warning when
6163 the match was found in java.lang.Object. */
6164 if (DECL_CONTEXT (found) != object_type_node
a003f638 6165 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6166 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6167 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6168 && flag_not_overriding)
6169 {
6170 parse_warning_context
781b0558 6171 (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
6172 lang_printable_name (found, 0),
6173 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6174 IDENTIFIER_POINTER (DECL_NAME
6175 (TYPE_NAME (DECL_CONTEXT (found)))));
6176 continue;
6177 }
6178
e04a16fb
AG
6179 /* Can't override final. Can't override static. */
6180 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6181 {
6182 /* Static *can* override static */
6183 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6184 continue;
6185 parse_error_context
6186 (method_wfl,
6187 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6188 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6189 lang_printable_name (found, 0),
e04a16fb
AG
6190 (METHOD_FINAL (found) ? "final" : "static"),
6191 IDENTIFIER_POINTER
6192 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6193 continue;
6194 }
7f10c2e2 6195
e04a16fb
AG
6196 /* Static method can't override instance method. */
6197 if (METHOD_STATIC (method))
6198 {
6199 parse_error_context
6200 (method_wfl,
781b0558 6201 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6202 lang_printable_name (found, 0),
e04a16fb
AG
6203 IDENTIFIER_POINTER
6204 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6205 continue;
6206 }
5e942c50 6207
5e942c50
APB
6208 /* - Overriding/hiding public must be public
6209 - Overriding/hiding protected must be protected or public
6210 - If the overriden or hidden method has default (package)
6211 access, then the overriding or hiding method must not be
614eaae0
APB
6212 private; otherwise, a compile-time error occurs. If
6213 `found' belongs to an interface, things have been already
6214 taken care of. */
6215 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6216 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6217 || (METHOD_PROTECTED (found)
6218 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6219 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6220 && METHOD_PRIVATE (method))))
e04a16fb
AG
6221 {
6222 parse_error_context
6223 (method_wfl,
781b0558 6224 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6225 (METHOD_PUBLIC (method) ? "public" :
6226 (METHOD_PRIVATE (method) ? "private" : "protected")),
6227 IDENTIFIER_POINTER (DECL_NAME
6228 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6229 continue;
6230 }
6231
b9f7e36c
APB
6232 /* Overriding methods must have compatible `throws' clauses on checked
6233 exceptions, if any */
6234 check_throws_clauses (method, method_wfl, found);
6235
e04a16fb
AG
6236 /* Inheriting multiple methods with the same signature. FIXME */
6237 }
6238
5e942c50
APB
6239 /* Don't forget eventual pending found and saved_found_wfl. Take
6240 into account that we might have exited because we saw an
d77613be 6241 artificial method as the last entry. */
5e942c50
APB
6242
6243 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6244 DECL_NAME (found) = saved_found_wfl;
6245
23a79c61
APB
6246 if (!TYPE_NVIRTUALS (class))
6247 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6248
d77613be
APB
6249 /* Search for inherited abstract method not yet implemented in this
6250 class. */
6251 java_check_abstract_method_definitions (class_decl);
6252
22eed1e6 6253 if (!saw_constructor)
e920ebc9 6254 fatal ("No constructor found");
e04a16fb
AG
6255}
6256
b9f7e36c
APB
6257/* Return a non zero value if the `throws' clause of METHOD (if any)
6258 is incompatible with the `throws' clause of FOUND (if any). */
6259
6260static void
6261check_throws_clauses (method, method_wfl, found)
6262 tree method, method_wfl, found;
6263{
6264 tree mthrows, fthrows;
6265
c877974e
APB
6266 /* Can't check these things with class loaded from bytecode. FIXME */
6267 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6268 return;
6269
b9f7e36c
APB
6270 for (mthrows = DECL_FUNCTION_THROWS (method);
6271 mthrows; mthrows = TREE_CHAIN (mthrows))
6272 {
6273 /* We don't verify unchecked expressions */
c877974e 6274 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6275 continue;
6276 /* Checked expression must be compatible */
6277 for (fthrows = DECL_FUNCTION_THROWS (found);
6278 fthrows; fthrows = TREE_CHAIN (fthrows))
6279 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6280 break;
6281 if (!fthrows)
6282 {
6283 parse_error_context
781b0558 6284 (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 6285 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6286 lang_printable_name (found, 0),
b9f7e36c
APB
6287 IDENTIFIER_POINTER
6288 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6289 }
6290 }
6291}
6292
e04a16fb
AG
6293/* Check abstract method of interface INTERFACE */
6294
6295static void
5e942c50
APB
6296java_check_abstract_methods (interface_decl)
6297 tree interface_decl;
e04a16fb
AG
6298{
6299 int i, n;
6300 tree method, basetype_vec, found;
5e942c50 6301 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6302
6303 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6304 {
b9f7e36c 6305 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6306
6307 /* 2- Check for double definition inside the defining interface */
6308 if (check_method_redefinition (interface, method))
6309 continue;
6310
6311 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6312 the thrown exceptions (FIXME) */
e04a16fb
AG
6313 found = lookup_java_interface_method2 (interface, method);
6314 if (found)
6315 {
5e942c50
APB
6316 char *t;
6317 tree saved_found_wfl = DECL_NAME (found);
6318 reset_method_name (found);
c2e3db92 6319 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6320 parse_error_context
b9f7e36c 6321 (method_wfl,
5e942c50 6322 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6323 lang_printable_name (found, 0), t,
b9f7e36c
APB
6324 IDENTIFIER_POINTER
6325 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6326 free (t);
5e942c50 6327 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6328 continue;
e04a16fb
AG
6329 }
6330 }
6331
6332 /* 4- Inherited methods can't differ by their returned types */
6333 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6334 return;
6335 n = TREE_VEC_LENGTH (basetype_vec);
6336 for (i = 0; i < n; i++)
6337 {
6338 tree sub_interface_method, sub_interface;
6339 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6340 if (!vec_elt)
6341 continue;
6342 sub_interface = BINFO_TYPE (vec_elt);
6343 for (sub_interface_method = TYPE_METHODS (sub_interface);
6344 sub_interface_method;
6345 sub_interface_method = TREE_CHAIN (sub_interface_method))
6346 {
6347 found = lookup_java_interface_method2 (interface,
6348 sub_interface_method);
6349 if (found && (found != sub_interface_method))
5e942c50
APB
6350 {
6351 tree saved_found_wfl = DECL_NAME (found);
6352 reset_method_name (found);
6353 parse_error_context
6354 (lookup_cl (sub_interface_method),
781b0558 6355 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6356 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6357 lang_printable_name (found, 0),
6358 IDENTIFIER_POINTER
6359 (DECL_NAME (TYPE_NAME
6360 (DECL_CONTEXT (sub_interface_method)))),
6361 IDENTIFIER_POINTER
6362 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6363 DECL_NAME (found) = saved_found_wfl;
6364 }
e04a16fb
AG
6365 }
6366 }
6367}
6368
e04a16fb
AG
6369/* Lookup methods in interfaces using their name and partial
6370 signature. Return a matching method only if their types differ. */
6371
6372static tree
6373lookup_java_interface_method2 (class, method_decl)
6374 tree class, method_decl;
6375{
6376 int i, n;
6377 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6378
6379 if (!basetype_vec)
6380 return NULL_TREE;
6381
6382 n = TREE_VEC_LENGTH (basetype_vec);
6383 for (i = 0; i < n; i++)
6384 {
6385 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6386 if ((BINFO_TYPE (vec_elt) != object_type_node)
6387 && (to_return =
6388 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6389 return to_return;
6390 }
6391 for (i = 0; i < n; i++)
6392 {
6393 to_return = lookup_java_interface_method2
6394 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6395 if (to_return)
6396 return to_return;
6397 }
6398
6399 return NULL_TREE;
6400}
6401
6402/* Lookup method using their name and partial signature. Return a
6403 matching method only if their types differ. */
6404
6405static tree
6406lookup_java_method2 (clas, method_decl, do_interface)
6407 tree clas, method_decl;
6408 int do_interface;
6409{
5e942c50
APB
6410 tree method, method_signature, method_name, method_type, name;
6411
e04a16fb 6412 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6413 name = DECL_NAME (method_decl);
6414 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6415 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6416 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6417
6418 while (clas != NULL_TREE)
6419 {
6420 for (method = TYPE_METHODS (clas);
6421 method != NULL_TREE; method = TREE_CHAIN (method))
6422 {
6423 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6424 tree name = DECL_NAME (method);
6425 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6426 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6427 && method_sig == method_signature
6428 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6429 return method;
e04a16fb
AG
6430 }
6431 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6432 }
6433 return NULL_TREE;
6434}
6435
f441f671
APB
6436/* Return the line that matches DECL line number, and try its best to
6437 position the column number. Used during error reports. */
e04a16fb
AG
6438
6439static tree
6440lookup_cl (decl)
6441 tree decl;
6442{
6443 static tree cl = NULL_TREE;
f441f671 6444 char *line, *found;
e04a16fb
AG
6445
6446 if (!decl)
6447 return NULL_TREE;
6448
6449 if (cl == NULL_TREE)
19e223db
MM
6450 {
6451 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6452 ggc_add_tree_root (&cl, 1);
6453 }
e04a16fb
AG
6454
6455 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6456 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6457
f441f671
APB
6458 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6459 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6460
6461 found = strstr ((const char *)line,
6462 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6463 if (found)
6464 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6465
e04a16fb
AG
6466 return cl;
6467}
6468
6469/* Look for a simple name in the single-type import list */
6470
6471static tree
6472find_name_in_single_imports (name)
6473 tree name;
6474{
6475 tree node;
6476
6477 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6478 if (TREE_VALUE (node) == name)
6479 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6480
6481 return NULL_TREE;
6482}
6483
6484/* Process all single-type import. */
6485
6486static int
6487process_imports ()
6488{
6489 tree import;
6490 int error_found;
6491
6492 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6493 {
6494 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6495
6496 /* Don't load twice something already defined. */
6497 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6498 continue;
6499 QUALIFIED_P (to_be_found) = 1;
6500 load_class (to_be_found, 0);
6501 error_found =
6502 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6503 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6504 {
6505 parse_error_context (TREE_PURPOSE (import),
6506 "Class or interface `%s' not found in import",
6507 IDENTIFIER_POINTER (to_be_found));
6508 return 1;
6509 }
6510 if (error_found)
6511 return 1;
6512 }
6513 return 0;
6514}
6515
9a7ab4b3
APB
6516/* Possibly find and mark a class imported by a single-type import
6517 statement. */
e04a16fb 6518
9a7ab4b3 6519static void
e04a16fb
AG
6520find_in_imports (class_type)
6521 tree class_type;
6522{
6523 tree import;
6524
6525 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6526 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6527 {
6528 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6529 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6530 }
e04a16fb
AG
6531}
6532
e04a16fb 6533static int
63a212ed 6534note_possible_classname (name, len)
49f48c71 6535 const char *name;
63a212ed 6536 int len;
e04a16fb 6537{
63a212ed
PB
6538 tree node;
6539 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6540 len = len - 5;
6541 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6542 len = len - 6;
e04a16fb 6543 else
63a212ed
PB
6544 return 0;
6545 node = ident_subst (name, len, "", '/', '.', "");
6546 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6547 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6548 return 1;
e04a16fb
AG
6549}
6550
6551/* Read a import directory, gathering potential match for further type
6552 references. Indifferently reads a filesystem or a ZIP archive
6553 directory. */
6554
6555static void
6556read_import_dir (wfl)
6557 tree wfl;
6558{
63a212ed 6559 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6560 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6561 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6562 DIR *dirp = NULL;
d8fccff5 6563 JCF *saved_jcf = current_jcf;
e04a16fb 6564
63a212ed
PB
6565 int found = 0;
6566 int k;
6567 void *entry;
6568 struct buffer filename[1];
6569
6570
6571 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6572 return;
6573 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6574
6575 BUFFER_INIT (filename);
6576 buffer_grow (filename, package_length + 100);
6577
6578 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6579 {
49f48c71 6580 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6581 int entry_length = strlen (entry_name);
6582 if (jcf_path_is_zipfile (entry))
6583 {
6584 ZipFile *zipf;
6585 buffer_grow (filename, entry_length);
6586 memcpy (filename->data, entry_name, entry_length - 1);
6587 filename->data[entry_length-1] = '\0';
6588 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6589 if (zipf == NULL)
6590 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6591 else
6592 {
6593 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6594 BUFFER_RESET (filename);
6595 for (k = 0; k < package_length; k++)
6596 {
6597 char ch = package_name[k];
6598 *filename->ptr++ = ch == '.' ? '/' : ch;
6599 }
6600 *filename->ptr++ = '/';
6601
345137c7 6602 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6603 {
49f48c71 6604 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6605 int current_entry_len = zipd->filename_length;
6606
345137c7
TT
6607 if (current_entry_len >= BUFFER_LENGTH (filename)
6608 && strncmp (filename->data, current_entry,
6609 BUFFER_LENGTH (filename)) != 0)
63a212ed 6610 continue;
345137c7 6611 found |= note_possible_classname (current_entry,
63a212ed
PB
6612 current_entry_len);
6613 }
6614 }
6615 }
6616 else
6617 {
6618 BUFFER_RESET (filename);
6619 buffer_grow (filename, entry_length + package_length + 4);
6620 strcpy (filename->data, entry_name);
6621 filename->ptr = filename->data + entry_length;
6622 for (k = 0; k < package_length; k++)
6623 {
6624 char ch = package_name[k];
6625 *filename->ptr++ = ch == '.' ? '/' : ch;
6626 }
6627 *filename->ptr = '\0';
6628
6629 dirp = opendir (filename->data);
6630 if (dirp == NULL)
6631 continue;
6632 *filename->ptr++ = '/';
6633 for (;;)
6634 {
63a212ed 6635 int len;
49f48c71 6636 const char *d_name;
63a212ed
PB
6637 struct dirent *direntp = readdir (dirp);
6638 if (!direntp)
6639 break;
6640 d_name = direntp->d_name;
6641 len = strlen (direntp->d_name);
6642 buffer_grow (filename, len+1);
6643 strcpy (filename->ptr, d_name);
345137c7 6644 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6645 package_length+len+1);
6646 }
6647 if (dirp)
6648 closedir (dirp);
6649 }
6650 }
e04a16fb 6651
63a212ed 6652 free (filename->data);
e04a16fb 6653
63a212ed
PB
6654 /* Here we should have a unified way of retrieving an entry, to be
6655 indexed. */
6656 if (!found)
e04a16fb
AG
6657 {
6658 static int first = 1;
6659 if (first)
6660 {
781b0558 6661 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6662 java_error_count++;
6663 first = 0;
6664 }
6665 else
63a212ed
PB
6666 parse_error_context (wfl, "Package `%s' not found in import",
6667 package_name);
e04a16fb
AG
6668 current_jcf = saved_jcf;
6669 return;
6670 }
e04a16fb
AG
6671 current_jcf = saved_jcf;
6672}
6673
6674/* Possibly find a type in the import on demands specified
6675 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6676 entire list, to detected potential double definitions. */
6677
6678static int
6679find_in_imports_on_demand (class_type)
6680 tree class_type;
6681{
ab3a6dd6 6682 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6683 int seen_once = -1;
ab3a6dd6 6684 tree cl = NULL_TREE;
e04a16fb
AG
6685
6686 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6687 {
49f48c71 6688 const char *id_name;
e04a16fb
AG
6689 obstack_grow (&temporary_obstack,
6690 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6691 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6692 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6693 obstack_grow0 (&temporary_obstack,
6694 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6695 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6696 id_name = obstack_finish (&temporary_obstack);
6697
6698 node = maybe_get_identifier (id_name);
6699 if (node && IS_A_CLASSFILE_NAME (node))
6700 {
6701 if (seen_once < 0)
6702 {
6703 cl = TREE_PURPOSE (import);
6704 seen_once = 1;
6705 node_to_use = node;
6706 }
6707 else
6708 {
6709 seen_once++;
6710 parse_error_context
1e12ab9b
APB
6711 (TREE_PURPOSE (import),
6712 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6713 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6714 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6715 }
6716 }
6717 }
6718
6719 if (seen_once == 1)
6720 {
6721 /* Setup lineno so that it refers to the line of the import (in
6722 case we parse a class file and encounter errors */
6723 tree decl;
6724 int saved_lineno = lineno;
6725 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6726 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6727 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6728 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6729 /* If there is no DECL set for the class or if the class isn't
6730 loaded and not seen in source yet, the load */
6731 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6732 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6733 load_class (node_to_use, 0);
6734 lineno = saved_lineno;
6735 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6736 }
6737 else
6738 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6739}
6740
9a7ab4b3
APB
6741/* Add package NAME to the list of package encountered so far. To
6742 speed up class lookup in do_resolve_class, we make sure a
6743 particular package is added only once. */
6744
6745static void
6746register_package (name)
6747 tree name;
6748{
6749 static struct hash_table _pht, *pht = NULL;
6750
6751 if (!pht)
6752 {
6753 hash_table_init (&_pht, hash_newfunc,
6754 java_hash_hash_tree_node, java_hash_compare_tree_node);
6755 pht = &_pht;
6756 }
6757
6758 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6759 {
6760 package_list = chainon (package_list, build_tree_list (name, NULL));
6761 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6762 }
6763}
6764
5e942c50
APB
6765static tree
6766resolve_package (pkg, next)
6767 tree pkg, *next;
6768{
c2952b01 6769 tree current, acc;
5e942c50 6770 tree type_name = NULL_TREE;
49f48c71 6771 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6772
6773 /* The trick is to determine when the package name stops and were
6774 the name of something contained in the package starts. Then we
6775 return a fully qualified name of what we want to get. */
6776
6777 /* Do a quick search on well known package names */
6778 if (!strncmp (name, "java.lang.reflect", 17))
6779 {
6780 *next =
6781 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6782 type_name = lookup_package_type (name, 17);
6783 }
6784 else if (!strncmp (name, "java.lang", 9))
6785 {
6786 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6787 type_name = lookup_package_type (name, 9);
6788 }
5e942c50 6789
2c56429a
APB
6790 /* If we found something here, return */
6791 if (type_name)
6792 return type_name;
6793
6794 *next = EXPR_WFL_QUALIFICATION (pkg);
6795
6796 /* Try the current package. */
6797 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6798 IDENTIFIER_LENGTH (ctxp->package)))
6799 {
6800 type_name =
6801 lookup_package_type_and_set_next (name,
6802 IDENTIFIER_LENGTH (ctxp->package),
6803 next );
6804 if (type_name)
6805 return type_name;
6806 }
6807
6808 /* Search in imported package */
6809 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6810 {
6811 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6812 int len = IDENTIFIER_LENGTH (current_pkg_name);
6813 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6814 {
6815 tree left, dummy;
6816
6817 breakdown_qualified (&left, &dummy, current_pkg_name);
6818 len = IDENTIFIER_LENGTH (left);
6819 type_name = lookup_package_type_and_set_next (name, len, next);
6820 if (type_name)
6821 break;
6822 }
6823 }
6824
c2952b01
APB
6825 /* Try to progressively construct a type name */
6826 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6827 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6828 current; current = TREE_CHAIN (current))
6829 {
6830 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6831 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6832 {
6833 type_name = acc;
6b48deee
APB
6834 /* resolve_package should be used in a loop, hence we
6835 point at this one to naturally process the next one at
6836 the next iteration. */
6837 *next = current;
c2952b01
APB
6838 break;
6839 }
6840 }
2c56429a
APB
6841 return type_name;
6842}
6843
6844static tree
6845lookup_package_type_and_set_next (name, len, next)
49f48c71 6846 const char *name;
2c56429a
APB
6847 int len;
6848 tree *next;
6849{
49f48c71 6850 const char *ptr;
2c56429a
APB
6851 tree type_name = lookup_package_type (name, len);
6852
6853 if (!type_name)
6854 return NULL;
6855
6856 ptr = IDENTIFIER_POINTER (type_name);
6857 while (ptr && (ptr = strchr (ptr, '.')))
6858 {
6859 *next = TREE_CHAIN (*next);
6860 ptr++;
6861 }
5e942c50
APB
6862 return type_name;
6863}
6864
6865static tree
6866lookup_package_type (name, from)
49f48c71 6867 const char *name;
5e942c50
APB
6868 int from;
6869{
6870 char subname [128];
49f48c71 6871 const char *sub = &name[from+1];
5e942c50
APB
6872 while (*sub != '.' && *sub)
6873 sub++;
6874 strncpy (subname, name, sub-name);
6875 subname [sub-name] = '\0';
6876 return get_identifier (subname);
6877}
6878
cf1748bf 6879static void
4dbf4496
APB
6880check_inner_class_access (decl, enclosing_decl, cl)
6881 tree decl, enclosing_decl, cl;
cf1748bf 6882{
4dbf4496
APB
6883 int access = 0;
6884
cf1748bf 6885 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6886 as a result of processing a JDEP crafted by source_start_java_method
6887 for the purpose of patching its parm decl. But the error would
6888 have been already trapped when fixing the method's signature.
6889 DECL can also be NULL in case of earlier errors. */
6890 if (!decl || !cl)
cf1748bf
APB
6891 return;
6892
4dbf4496
APB
6893 /* We grant access to private and protected inner classes if the
6894 location from where we're trying to access DECL is an enclosing
6895 context for DECL or if both have a common enclosing context. */
6896 if (CLASS_PRIVATE (decl))
6897 access = 1;
6898 if (CLASS_PROTECTED (decl))
6899 access = 2;
6900 if (!access)
6901 return;
6902
6903 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6904 TREE_TYPE (decl))
6905 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6906 TREE_TYPE (decl)))
6907 return;
6908
6909 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6910 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6911 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6912 lang_printable_name (decl, 0));
6913}
6914
e04a16fb
AG
6915/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6916 access violations were found, 1 otherwise. */
6917
6918static int
6919check_pkg_class_access (class_name, cl)
6920 tree class_name;
6921 tree cl;
6922{
6923 tree type;
e04a16fb
AG
6924
6925 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6926 return 0;
6927
6928 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6929 return 0;
6930
6931 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6932 {
e28cd97b
APB
6933 /* Access to a private class within the same package is
6934 allowed. */
6935 tree l, r;
6936 breakdown_qualified (&l, &r, class_name);
6937 if (l == ctxp->package)
6938 return 0;
6939
e04a16fb 6940 parse_error_context
781b0558 6941 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6942 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6943 IDENTIFIER_POINTER (class_name));
6944 return 1;
6945 }
6946 return 0;
6947}
6948
6949/* Local variable declaration. */
6950
6951static void
6952declare_local_variables (modifier, type, vlist)
6953 int modifier;
6954 tree type;
6955 tree vlist;
6956{
c583dd46
APB
6957 tree decl, current, saved_type;
6958 tree type_wfl = NULL_TREE;
e04a16fb 6959 int must_chain = 0;
c2952b01 6960 int final_p = 0;
e04a16fb 6961
2aa11e97 6962 /* Push a new block if statements were seen between the last time we
e04a16fb 6963 pushed a block and now. Keep a cound of block to close */
f099f336 6964 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6965 {
f099f336 6966 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6967 tree b = enter_block ();
f099f336 6968 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6969 }
6970
6971 if (modifier)
6972 {
6973 int i;
6974 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6975 if (modifier == ACC_FINAL)
c2952b01 6976 final_p = 1;
c877974e
APB
6977 else
6978 {
6979 parse_error_context
6980 (ctxp->modifier_ctx [i],
6981 "Only `final' is allowed as a local variables modifier");
6982 return;
6983 }
e04a16fb
AG
6984 }
6985
c583dd46
APB
6986 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6987 hold the TYPE value if a new incomplete has to be created (as
6988 opposed to being found already existing and reused). */
6989 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6990
6991 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6992 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6993
6994 /* Go through all the declared variables */
6995 for (current = vlist, saved_type = type; current;
6996 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6997 {
c877974e 6998 tree other, real_type;
e04a16fb
AG
6999 tree wfl = TREE_PURPOSE (current);
7000 tree name = EXPR_WFL_NODE (wfl);
7001 tree init = TREE_VALUE (current);
e04a16fb 7002
c583dd46
APB
7003 /* Process NAME, as it may specify extra dimension(s) for it */
7004 type = build_array_from_name (type, type_wfl, name, &name);
7005
7006 /* Variable redefinition check */
7007 if ((other = lookup_name_in_blocks (name)))
7008 {
7009 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7010 DECL_SOURCE_LINE (other));
7011 continue;
7012 }
7013
7014 /* Type adjustment. We may have just readjusted TYPE because
7015 the variable specified more dimensions. Make sure we have
7016 a reference if we can and don't have one already. */
1886c9d8 7017 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
7018
7019 real_type = GET_REAL_TYPE (type);
c583dd46
APB
7020 /* Never layout this decl. This will be done when its scope
7021 will be entered */
c877974e 7022 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 7023 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
7024 BLOCK_CHAIN_DECL (decl);
7025
d4370213
APB
7026 /* If doing xreferencing, replace the line number with the WFL
7027 compound value */
7028 if (flag_emit_xref)
7029 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7030
e04a16fb
AG
7031 /* Don't try to use an INIT statement when an error was found */
7032 if (init && java_error_count)
7033 init = NULL_TREE;
c583dd46
APB
7034
7035 /* Add the initialization function to the current function's code */
7036 if (init)
e04a16fb 7037 {
c583dd46
APB
7038 /* Name might have been readjusted */
7039 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7040 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7041 java_method_add_stmt (current_function_decl,
7042 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7043 init));
7044 }
7045
7046 /* Setup dependency the type of the decl */
7047 if (must_chain)
7048 {
7049 jdep *dep;
7050 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7051 dep = CLASSD_LAST (ctxp->classd_list);
7052 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7053 }
7054 }
7055 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7056}
7057
7058/* Called during parsing. Build decls from argument list. */
7059
7060static void
7061source_start_java_method (fndecl)
7062 tree fndecl;
7063{
7064 tree tem;
7065 tree parm_decl;
7066 int i;
7067
79d13333
APB
7068 if (!fndecl)
7069 return;
7070
e04a16fb
AG
7071 current_function_decl = fndecl;
7072
7073 /* New scope for the function */
7074 enter_block ();
7075 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7076 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7077 {
7078 tree type = TREE_VALUE (tem);
7079 tree name = TREE_PURPOSE (tem);
7080
23a79c61
APB
7081 /* If type is incomplete. Create an incomplete decl and ask for
7082 the decl to be patched later */
e04a16fb
AG
7083 if (INCOMPLETE_TYPE_P (type))
7084 {
7085 jdep *jdep;
c877974e
APB
7086 tree real_type = GET_REAL_TYPE (type);
7087 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7088 type = obtain_incomplete_type (type);
e04a16fb
AG
7089 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7090 jdep = CLASSD_LAST (ctxp->classd_list);
7091 JDEP_MISC (jdep) = name;
7092 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7093 }
7094 else
7095 parm_decl = build_decl (PARM_DECL, name, type);
7096
c2952b01
APB
7097 /* Remember if a local variable was declared final (via its
7098 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7099 if (ARG_FINAL_P (tem))
7100 LOCAL_FINAL (parm_decl) = 1;
7101
e04a16fb
AG
7102 BLOCK_CHAIN_DECL (parm_decl);
7103 }
7104 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7105 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7106 nreverse (tem);
7107 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7108 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7109}
7110
22eed1e6
APB
7111/* Called during parsing. Creates an artificial method declaration. */
7112
7113static tree
7114create_artificial_method (class, flags, type, name, args)
7115 tree class;
7116 int flags;
7117 tree type, name, args;
7118{
22eed1e6
APB
7119 tree mdecl;
7120
c2952b01 7121 java_parser_context_save_global ();
22eed1e6
APB
7122 lineno = 0;
7123 mdecl = make_node (FUNCTION_TYPE);
7124 TREE_TYPE (mdecl) = type;
7125 TYPE_ARG_TYPES (mdecl) = args;
7126 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7127 java_parser_context_restore_global ();
22eed1e6
APB
7128 DECL_ARTIFICIAL (mdecl) = 1;
7129 return mdecl;
7130}
7131
7132/* Starts the body if an artifical method. */
7133
7134static void
7135start_artificial_method_body (mdecl)
7136 tree mdecl;
7137{
7138 DECL_SOURCE_LINE (mdecl) = 1;
7139 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7140 source_start_java_method (mdecl);
7141 enter_block ();
7142}
7143
7144static void
7145end_artificial_method_body (mdecl)
7146 tree mdecl;
7147{
7148 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7149 exit_block ();
7150}
7151
e04a16fb
AG
7152/* Called during expansion. Push decls formerly built from argument
7153 list so they're usable during expansion. */
7154
7155static void
7156expand_start_java_method (fndecl)
7157 tree fndecl;
7158{
7159 tree tem, *ptr;
e04a16fb 7160
e04a16fb
AG
7161 current_function_decl = fndecl;
7162
c2952b01
APB
7163 if (! quiet_flag)
7164 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7165 announce_function (fndecl);
c2952b01
APB
7166 if (! quiet_flag)
7167 fprintf (stderr, "]");
7168
7169 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7170 ptr = &DECL_ARGUMENTS (fndecl);
7171 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7172 while (tem)
7173 {
7174 tree next = TREE_CHAIN (tem);
b67d701b 7175 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7176 if (PROMOTE_PROTOTYPES
7177 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7178 && INTEGRAL_TYPE_P (type))
7179 type = integer_type_node;
b67d701b 7180 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7181 layout_decl (tem, 0);
7182 pushdecl (tem);
e04a16fb
AG
7183 *ptr = tem;
7184 ptr = &TREE_CHAIN (tem);
7185 tem = next;
7186 }
7187 *ptr = NULL_TREE;
7188 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7189 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7190}
7191
7192/* Terminate a function and expand its body. */
7193
7194static void
7195source_end_java_method ()
7196{
7197 tree fndecl = current_function_decl;
138657ec 7198 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7199
79d13333
APB
7200 if (!fndecl)
7201 return;
7202
e04a16fb
AG
7203 java_parser_context_save_global ();
7204 lineno = ctxp->last_ccb_indent1;
7205
b67d701b
PB
7206 /* Set EH language codes */
7207 java_set_exception_lang_code ();
7208
5423609c
APB
7209 /* Turn function bodies with only a NOP expr null, so they don't get
7210 generated at all and we won't get warnings when using the -W
7211 -Wall flags. */
7212 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7213 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7214
e04a16fb
AG
7215 /* Generate function's code */
7216 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7217 && ! flag_emit_class_files
7218 && ! flag_emit_xref)
e04a16fb
AG
7219 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7220
7221 /* pop out of its parameters */
7222 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7223 poplevel (1, 0, 1);
7224 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7225
7226 /* Generate rtl for function exit. */
e8fc7396 7227 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7228 {
7229 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7230 /* Emit catch-finally clauses */
7231 emit_handlers ();
e04a16fb
AG
7232 expand_function_end (input_filename, lineno, 0);
7233
138657ec
AH
7234 /* FIXME: If the current method contains any exception handlers,
7235 force asynchronous_exceptions: this is necessary because signal
7236 handlers in libjava may throw exceptions. This is far from being
7237 a perfect solution, but it's better than doing nothing at all.*/
7238 if (catch_clauses)
7239 asynchronous_exceptions = 1;
7240
e04a16fb
AG
7241 /* Run the optimizers and output assembler code for this function. */
7242 rest_of_compilation (fndecl);
7243 }
7244
7245 current_function_decl = NULL_TREE;
8226320b 7246 permanent_allocation (1);
e04a16fb 7247 java_parser_context_restore_global ();
138657ec 7248 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7249}
7250
7251/* Record EXPR in the current function block. Complements compound
7252 expression second operand if necessary. */
7253
7254tree
7255java_method_add_stmt (fndecl, expr)
7256 tree fndecl, expr;
7257{
b771925e
APB
7258 if (!GET_CURRENT_BLOCK (fndecl))
7259 return NULL_TREE;
f099f336 7260 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7261}
e04a16fb 7262
b67d701b
PB
7263static tree
7264add_stmt_to_block (b, type, stmt)
7265 tree b, type, stmt;
7266{
7267 tree body = BLOCK_EXPR_BODY (b), c;
7268
e04a16fb
AG
7269 if (java_error_count)
7270 return body;
b67d701b
PB
7271
7272 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7273 return body;
7274
b67d701b
PB
7275 BLOCK_EXPR_BODY (b) = c;
7276 TREE_SIDE_EFFECTS (c) = 1;
7277 return c;
e04a16fb
AG
7278}
7279
7280/* Add STMT to EXISTING if possible, otherwise create a new
7281 COMPOUND_EXPR and add STMT to it. */
7282
7283static tree
7284add_stmt_to_compound (existing, type, stmt)
7285 tree existing, type, stmt;
7286{
15fdcfe9
PB
7287 if (existing)
7288 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7289 else
15fdcfe9 7290 return stmt;
e04a16fb
AG
7291}
7292
1886c9d8
APB
7293void java_layout_seen_class_methods ()
7294{
7295 tree previous_list = all_class_list;
7296 tree end = NULL_TREE;
7297 tree current;
7298
7299 while (1)
7300 {
7301 for (current = previous_list;
7302 current != end; current = TREE_CHAIN (current))
7303 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7304
7305 if (previous_list != all_class_list)
7306 {
7307 end = previous_list;
7308 previous_list = all_class_list;
7309 }
7310 else
7311 break;
7312 }
7313}
7314
e04a16fb 7315void
c2952b01 7316java_reorder_fields ()
e04a16fb 7317{
c2952b01 7318 static tree stop_reordering = NULL_TREE;
19e223db 7319 static int initialized_p;
c2952b01 7320 tree current;
19e223db
MM
7321
7322 /* Register STOP_REORDERING with the garbage collector. */
7323 if (!initialized_p)
7324 {
7325 ggc_add_tree_root (&stop_reordering, 1);
7326 initialized_p = 1;
7327 }
7328
5e942c50 7329 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7330 {
5e942c50 7331 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7332
c2952b01
APB
7333 if (current_class == stop_reordering)
7334 break;
7335
c877974e
APB
7336 /* Reverse the fields, but leave the dummy field in front.
7337 Fields are already ordered for Object and Class */
7338 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7339 && current_class != class_type_node)
7340 {
23a79c61
APB
7341 /* If the dummy field is there, reverse the right fields and
7342 just layout the type for proper fields offset */
c877974e
APB
7343 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7344 {
7345 tree fields = TYPE_FIELDS (current_class);
7346 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7347 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7348 }
23a79c61
APB
7349 /* We don't have a dummy field, we need to layout the class,
7350 after having reversed the fields */
c877974e
APB
7351 else
7352 {
7353 TYPE_FIELDS (current_class) =
7354 nreverse (TYPE_FIELDS (current_class));
7355 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7356 }
7357 }
c2952b01
APB
7358 }
7359 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7360}
7361
7362/* Layout the methods of all classes loaded in one way on an
7363 other. Check methods of source parsed classes. Then reorder the
7364 fields and layout the classes or the type of all source parsed
7365 classes */
7366
7367void
7368java_layout_classes ()
7369{
7370 tree current;
7371 int save_error_count = java_error_count;
7372
7373 /* Layout the methods of all classes seen so far */
7374 java_layout_seen_class_methods ();
7375 java_parse_abort_on_error ();
7376 all_class_list = NULL_TREE;
7377
7378 /* Then check the methods of all parsed classes */
7379 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7380 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7381 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7382 java_parse_abort_on_error ();
7383
7384 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7385 {
7386 current_class = TREE_TYPE (TREE_VALUE (current));
7387 layout_class (current_class);
5e942c50 7388
c877974e
APB
7389 /* From now on, the class is considered completely loaded */
7390 CLASS_LOADED_P (current_class) = 1;
7391
5e942c50
APB
7392 /* Error reported by the caller */
7393 if (java_error_count)
7394 return;
e04a16fb 7395 }
23a79c61
APB
7396
7397 /* We might have reloaded classes durign the process of laying out
7398 classes for code generation. We must layout the methods of those
7399 late additions, as constructor checks might use them */
1886c9d8 7400 java_layout_seen_class_methods ();
23a79c61 7401 java_parse_abort_on_error ();
e04a16fb
AG
7402}
7403
c2952b01
APB
7404/* Expand methods in the current set of classes rememebered for
7405 generation. */
e04a16fb 7406
49f48c71 7407static void
c2952b01 7408java_complete_expand_classes ()
e04a16fb
AG
7409{
7410 tree current;
ce6e9147
APB
7411
7412 do_not_fold = flag_emit_xref;
c2952b01 7413
e04a16fb 7414 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7415 if (!INNER_CLASS_DECL_P (current))
7416 java_complete_expand_class (current);
7417}
e04a16fb 7418
c2952b01
APB
7419/* Expand the methods found in OUTER, starting first by OUTER's inner
7420 classes, if any. */
e04a16fb 7421
c2952b01
APB
7422static void
7423java_complete_expand_class (outer)
7424 tree outer;
7425{
7426 tree inner_list;
e04a16fb 7427
c2952b01 7428 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7429
c2952b01
APB
7430 /* We need to go after all inner classes and start expanding them,
7431 starting with most nested ones. We have to do that because nested
7432 classes might add functions to outer classes */
e04a16fb 7433
c2952b01
APB
7434 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7435 inner_list; inner_list = TREE_CHAIN (inner_list))
7436 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7437
c2952b01
APB
7438 java_complete_expand_methods (outer);
7439 set_nested_class_simple_name_value (outer, 0); /* Reset */
7440}
7441
7442/* Expand methods registered in CLASS_DECL. The general idea is that
7443 we expand regular methods first. This allows us get an estimate on
7444 how outer context local alias fields are really used so we can add
7445 to the constructor just enough code to initialize them properly (it
c00f0fb2 7446 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7447 constructors and then <clinit>. */
7448
7449static void
7450java_complete_expand_methods (class_decl)
7451 tree class_decl;
7452{
7453 tree clinit, finit, decl, first_decl;
7454
7455 current_class = TREE_TYPE (class_decl);
7456
7457 /* Initialize a new constant pool */
7458 init_outgoing_cpool ();
7459
7460 /* Pre-expand <clinit> to figure whether we really need it or
7461 not. If we do need it, we pre-expand the static fields so they're
7462 ready to be used somewhere else. <clinit> will be fully expanded
7463 after we processed the constructors. */
7464 first_decl = TYPE_METHODS (current_class);
7465 clinit = maybe_generate_pre_expand_clinit (current_class);
7466
c00f0fb2 7467 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7468 try to use it.*/
7469 if (TYPE_FINIT_STMT_LIST (current_class))
7470 {
7471 finit = generate_finit (current_class);
7472 java_complete_expand_method (finit);
7473 }
7474
7475 /* Now do the constructors */
7476 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7477 {
7478 int no_body;
7479
7480 if (!DECL_CONSTRUCTOR_P (decl))
7481 continue;
7482
7483 no_body = !DECL_FUNCTION_BODY (decl);
7484 /* Don't generate debug info on line zero when expanding a
7485 generated constructor. */
7486 if (no_body)
7487 restore_line_number_status (1);
7488
7489 java_complete_expand_method (decl);
7490
7491 if (no_body)
7492 restore_line_number_status (0);
7493 }
7494
7495 /* First, do the ordinary methods. */
7496 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7497 {
7145d9fe
TT
7498 /* Skip abstract or native methods -- but do handle native
7499 methods when generating JNI stubs. */
7500 if (METHOD_ABSTRACT (decl)
7501 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7502 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7503 continue;
7145d9fe
TT
7504
7505 if (METHOD_NATIVE (decl))
7506 {
7507 tree body = build_jni_stub (decl);
7508 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7509 }
7510
c2952b01
APB
7511 java_complete_expand_method (decl);
7512 }
7513
7514 /* If there is indeed a <clinit>, fully expand it now */
7515 if (clinit)
7516 {
7517 /* Prevent the use of `this' inside <clinit> */
7518 ctxp->explicit_constructor_p = 1;
7519 java_complete_expand_method (clinit);
7520 ctxp->explicit_constructor_p = 0;
e04a16fb 7521 }
c2952b01 7522
165f37bc
APB
7523 /* We might have generated a class$ that we now want to expand */
7524 if (TYPE_DOT_CLASS (current_class))
7525 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7526
c2952b01
APB
7527 /* Now verify constructor circularity (stop after the first one we
7528 prove wrong.) */
7529 if (!CLASS_INTERFACE (class_decl))
7530 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7531 if (DECL_CONSTRUCTOR_P (decl)
7532 && verify_constructor_circularity (decl, decl))
7533 break;
7534
7535 /* Save the constant pool. We'll need to restore it later. */
7536 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7537}
7538
c2952b01
APB
7539/* Attempt to create <clinit>. Pre-expand static fields so they can be
7540 safely used in some other methods/constructors. */
e920ebc9 7541
c2952b01
APB
7542static tree
7543maybe_generate_pre_expand_clinit (class_type)
7544 tree class_type;
e920ebc9 7545{
c2952b01
APB
7546 tree current, mdecl;
7547
7548 if (!TYPE_CLINIT_STMT_LIST (class_type))
7549 return NULL_TREE;
e920ebc9 7550
c2952b01
APB
7551 /* Go through all static fields and pre expand them */
7552 for (current = TYPE_FIELDS (class_type); current;
7553 current = TREE_CHAIN (current))
7554 if (FIELD_STATIC (current))
7555 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7556
7557 /* Then build the <clinit> method */
7558 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7559 clinit_identifier_node, end_params_node);
7560 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7561 mdecl, NULL_TREE);
7562 start_artificial_method_body (mdecl);
7563
7564 /* We process the list of assignment we produced as the result of
7565 the declaration of initialized static field and add them as
7566 statement to the <clinit> method. */
7567 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7568 current = TREE_CHAIN (current))
e920ebc9 7569 {
9a7ab4b3 7570 tree stmt = current;
c2952b01
APB
7571 /* We build the assignment expression that will initialize the
7572 field to its value. There are strict rules on static
7573 initializers (8.5). FIXME */
98a52c2c 7574 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7575 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7576 java_method_add_stmt (mdecl, stmt);
7577 }
e920ebc9 7578
c2952b01
APB
7579 end_artificial_method_body (mdecl);
7580
92d83515
APB
7581 /* Now we want to place <clinit> as the last method (because we need
7582 it at least for interface so that it doesn't interfere with the
7583 dispatch table based lookup. */
7584 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7585 {
92d83515
APB
7586 current = TREE_CHAIN (TYPE_METHODS (class_type));
7587 TYPE_METHODS (class_type) = current;
c2952b01
APB
7588
7589 while (TREE_CHAIN (current))
7590 current = TREE_CHAIN (current);
92d83515 7591
c2952b01
APB
7592 TREE_CHAIN (current) = mdecl;
7593 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7594 }
c2952b01
APB
7595
7596 return mdecl;
e920ebc9
APB
7597}
7598
92d83515
APB
7599/* See whether we could get rid of <clinit>. Criteria are: all static
7600 final fields have constant initial values and the body of <clinit>
7601 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7602
7603static int
7604maybe_yank_clinit (mdecl)
7605 tree mdecl;
7606{
7607 tree type, current;
7608 tree fbody, bbody;
99eaf8d4 7609 int found = 0;
92d83515
APB
7610
7611 if (!DECL_CLINIT_P (mdecl))
7612 return 0;
f0f3a777
APB
7613
7614 /* If the body isn't empty, then we keep <clinit>. Note that if
7615 we're emitting classfiles, this isn't enough not to rule it
7616 out. */
92d83515
APB
7617 fbody = DECL_FUNCTION_BODY (mdecl);
7618 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7619 bbody = BLOCK_EXPR_BODY (bbody);
f0f3a777 7620 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7621 return 0;
7622
7623 type = DECL_CONTEXT (mdecl);
7624 current = TYPE_FIELDS (type);
7625
7626 for (current = (current ? TREE_CHAIN (current) : current);
7627 current; current = TREE_CHAIN (current))
f0f3a777
APB
7628 {
7629 tree f_init;
7630
7631 /* We're not interested in non static field */
7632 if (!FIELD_STATIC (current))
7633 continue;
7634
7635 /* Anything that isn't String or a basic type is ruled out -- or
7636 if we now how to deal with it (when doing things natively) we
7637 should generated an empty <clinit> so that SUID are computed
7638 correctly. */
7639 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7640 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7641 break;
7642
7643 f_init = DECL_INITIAL (current);
7644 /* If we're emitting native code, we want static final fields to
7645 have constant initializers. If we don't meet these
7646 conditions, we keep <clinit> */
7647 if (!flag_emit_class_files
7648 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7649 break;
7650 /* If we're emitting bytecode, we want static fields to have
7651 constant initializers or no initializer. If we don't meet
7652 these conditions, we keep <clinit> */
7653 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7654 break;
7655 }
92d83515 7656
99eaf8d4
APB
7657 /* Now we analyze the method body and look for something that
7658 isn't a MODIFY_EXPR */
7659 if (bbody == empty_stmt_node)
7660 bbody = NULL_TREE;
7661 while (bbody)
7662 switch (TREE_CODE (bbody))
7663 {
7664 case BLOCK:
7665 bbody = BLOCK_EXPR_BODY (bbody);
7666 break;
7667
7668 case EXPR_WITH_FILE_LOCATION:
7669 bbody = EXPR_WFL_NODE (bbody);
7670 break;
7671
7672 case COMPOUND_EXPR:
7673 bbody = TREE_OPERAND (bbody, 0);
7674 break;
7675
7676 case MODIFY_EXPR:
7677 bbody = NULL_TREE;
7678 break;
7679
7680 default:
7681 bbody = NULL_TREE;
7682 found = 1;
7683 }
7684
7685 if (current || found)
92d83515
APB
7686 return 0;
7687
7688 /* Get rid of <clinit> in the class' list of methods */
7689 if (TYPE_METHODS (type) == mdecl)
7690 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7691 else
7692 for (current = TYPE_METHODS (type); current;
7693 current = TREE_CHAIN (current))
7694 if (TREE_CHAIN (current) == mdecl)
7695 {
7696 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7697 break;
7698 }
7699
7700 return 1;
7701}
7702
7703
e04a16fb
AG
7704/* Complete and expand a method. */
7705
7706static void
7707java_complete_expand_method (mdecl)
7708 tree mdecl;
7709{
92d83515
APB
7710 int yank_clinit = 0;
7711
c2952b01 7712 current_function_decl = mdecl;
22eed1e6
APB
7713 /* Fix constructors before expanding them */
7714 if (DECL_CONSTRUCTOR_P (mdecl))
7715 fix_constructors (mdecl);
e04a16fb 7716
22eed1e6 7717 /* Expand functions that have a body */
e04a16fb
AG
7718 if (DECL_FUNCTION_BODY (mdecl))
7719 {
9bbc7d9f
PB
7720 tree fbody = DECL_FUNCTION_BODY (mdecl);
7721 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7722 tree exception_copy = NULL_TREE;
e04a16fb 7723 expand_start_java_method (mdecl);
939d7216 7724 build_result_decl (mdecl);
e04a16fb
AG
7725
7726 current_this
7727 = (!METHOD_STATIC (mdecl) ?
7728 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7729
ce6e9147
APB
7730 /* Purge the `throws' list of unchecked exceptions. If we're
7731 doing xref, save a copy of the list and re-install it
7732 later. */
7733 if (flag_emit_xref)
7734 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7735
b9f7e36c
APB
7736 purge_unchecked_exceptions (mdecl);
7737
7738 /* Install exceptions thrown with `throws' */
7739 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7740
9bbc7d9f 7741 if (block_body != NULL_TREE)
bc3ca41b
PB
7742 {
7743 block_body = java_complete_tree (block_body);
c2952b01 7744
7145d9fe 7745 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7746 check_for_initialization (block_body);
f099f336 7747 ctxp->explicit_constructor_p = 0;
bc3ca41b 7748 }
e803d3b2 7749
9bbc7d9f 7750 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7751
c2952b01
APB
7752 /* If we saw a return but couldn't evaluate it properly, we'll
7753 have an error_mark_node here. */
7754 if (block_body != error_mark_node
7755 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7756 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7757 && !flag_emit_xref)
82371d41 7758 missing_return_error (current_function_decl);
7525cc04 7759
92d83515
APB
7760 /* Check wether we could just get rid of clinit, now the picture
7761 is complete. */
7762 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7763 complete_start_java_method (mdecl);
7764
e04a16fb 7765 /* Don't go any further if we've found error(s) during the
92d83515
APB
7766 expansion */
7767 if (!java_error_count && !yank_clinit)
e04a16fb 7768 source_end_java_method ();
22eed1e6
APB
7769 else
7770 {
92d83515
APB
7771 if (java_error_count)
7772 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7773 poplevel (1, 0, 1);
7774 }
b9f7e36c
APB
7775
7776 /* Pop the exceptions and sanity check */
7777 POP_EXCEPTIONS();
7778 if (currently_caught_type_list)
7779 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7780
7781 if (flag_emit_xref)
7782 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7783 }
7784}
7785
c2952b01
APB
7786\f
7787
7788/* This section of the code deals with accessing enclosing context
7789 fields either directly by using the relevant access to this$<n> or
7790 by invoking an access method crafted for that purpose. */
7791
7792/* Build the necessary access from an inner class to an outer
7793 class. This routine could be optimized to cache previous result
7794 (decl, current_class and returned access). When an access method
7795 needs to be generated, it always takes the form of a read. It might
7796 be later turned into a write by calling outer_field_access_fix. */
7797
7798static tree
7799build_outer_field_access (id, decl)
7800 tree id, decl;
7801{
7802 tree access = NULL_TREE;
7803 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7804
7805 /* If decl's class is the direct outer class of the current_class,
f0f3a777 7806 build the access as `this$<n>.<field>'. Note that we will break
c2952b01
APB
7807 the `private' barrier if we're not emitting bytecodes. */
7808 if (ctx == DECL_CONTEXT (decl)
7809 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7810 {
7811 tree thisn = build_current_thisn (current_class);
7812 access = make_qualified_primary (build_wfl_node (thisn),
7813 id, EXPR_WFL_LINECOL (id));
7814 }
7815 /* Otherwise, generate access methods to outer this and access the
7816 field (either using an access method or by direct access.) */
7817 else
7818 {
7819 int lc = EXPR_WFL_LINECOL (id);
7820
7821 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7822 get a hold to the enclosing instance we need, and then we
c2952b01
APB
7823 build the field access. */
7824 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7825
7826 /* If the field is private and we're generating bytecode, then
7827 we generate an access method */
7828 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7829 {
7830 tree name = build_outer_field_access_methods (decl);
7831 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7832 name, access, NULL_TREE);
7833 }
7834 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7835 Once again we break the `private' access rule from a foreign
7836 class. */
7837 else
7838 access = make_qualified_primary (access, id, lc);
7839 }
7840 return resolve_expression_name (access, NULL);
7841}
7842
7843/* Return a non zero value if NODE describes an outer field inner
7844 access. */
7845
7846static int
7847outer_field_access_p (type, decl)
7848 tree type, decl;
7849{
7850 if (!INNER_CLASS_TYPE_P (type)
7851 || TREE_CODE (decl) != FIELD_DECL
7852 || DECL_CONTEXT (decl) == type)
7853 return 0;
7854
7855 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7856 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7857 {
7858 if (type == DECL_CONTEXT (decl))
7859 return 1;
7860 if (!DECL_CONTEXT (TYPE_NAME (type)))
7861 break;
7862 }
7863
7864 return 0;
7865}
7866
7867/* Return a non zero value if NODE represents an outer field inner
7868 access that was been already expanded. As a side effect, it returns
7869 the name of the field being accessed and the argument passed to the
7870 access function, suitable for a regeneration of the access method
7871 call if necessary. */
7872
7873static int
7874outer_field_expanded_access_p (node, name, arg_type, arg)
7875 tree node, *name, *arg_type, *arg;
7876{
7877 int identified = 0;
7878
7879 if (TREE_CODE (node) != CALL_EXPR)
7880 return 0;
7881
7882 /* Well, gcj generates slightly different tree nodes when compiling
7883 to native or bytecodes. It's the case for function calls. */
7884
7885 if (flag_emit_class_files
7886 && TREE_CODE (node) == CALL_EXPR
7887 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7888 identified = 1;
7889 else if (!flag_emit_class_files)
7890 {
7891 node = TREE_OPERAND (node, 0);
7892
7893 if (node && TREE_OPERAND (node, 0)
7894 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7895 {
7896 node = TREE_OPERAND (node, 0);
7897 if (TREE_OPERAND (node, 0)
7898 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7899 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7900 (DECL_NAME (TREE_OPERAND (node, 0)))))
7901 identified = 1;
7902 }
7903 }
7904
7905 if (identified && name && arg_type && arg)
7906 {
7907 tree argument = TREE_OPERAND (node, 1);
7908 *name = DECL_NAME (TREE_OPERAND (node, 0));
7909 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7910 *arg = TREE_VALUE (argument);
7911 }
7912 return identified;
7913}
7914
7915/* Detect in NODE an outer field read access from an inner class and
7916 transform it into a write with RHS as an argument. This function is
7917 called from the java_complete_lhs when an assignment to a LHS can
7918 be identified. */
7919
7920static tree
7921outer_field_access_fix (wfl, node, rhs)
7922 tree wfl, node, rhs;
7923{
7924 tree name, arg_type, arg;
7925
7926 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7927 {
7928 /* At any rate, check whether we're trying to assign a value to
7929 a final. */
7930 tree accessed = (JDECL_P (node) ? node :
7931 (TREE_CODE (node) == COMPONENT_REF ?
7932 TREE_OPERAND (node, 1) : node));
7933 if (check_final_assignment (accessed, wfl))
7934 return error_mark_node;
7935
7936 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7937 arg_type, name, arg, rhs);
7938 return java_complete_tree (node);
7939 }
7940 return NULL_TREE;
7941}
7942
7943/* Construct the expression that calls an access method:
7944 <type>.access$<n>(<arg1> [, <arg2>]);
7945
7946 ARG2 can be NULL and will be omitted in that case. It will denote a
7947 read access. */
7948
7949static tree
7950build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7951 int lc;
7952 tree type, access_method_name, arg1, arg2;
7953{
7954 tree args, cn, access;
7955
7956 args = arg1 ? arg1 :
7957 build_wfl_node (build_current_thisn (current_class));
7958 args = build_tree_list (NULL_TREE, args);
7959
7960 if (arg2)
7961 args = tree_cons (NULL_TREE, arg2, args);
7962
7963 access = build_method_invocation (build_wfl_node (access_method_name), args);
7964 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7965 return make_qualified_primary (cn, access, lc);
7966}
7967
7968static tree
7969build_new_access_id ()
7970{
7971 static int access_n_counter = 1;
7972 char buffer [128];
7973
7974 sprintf (buffer, "access$%d", access_n_counter++);
7975 return get_identifier (buffer);
7976}
7977
7978/* Create the static access functions for the outer field DECL. We define a
7979 read:
7980 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7981 return inst$.field;
7982 }
7983 and a write access:
7984 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7985 TREE_TYPE (<field>) value$) {
7986 return inst$.field = value$;
7987 }
7988 We should have a usage flags on the DECL so we can lazily turn the ones
7989 we're using for code generation. FIXME.
7990*/
7991
7992static tree
7993build_outer_field_access_methods (decl)
7994 tree decl;
7995{
7996 tree id, args, stmt, mdecl;
7997
7998 /* Check point, to be removed. FIXME */
7999 if (FIELD_INNER_ACCESS (decl)
8000 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
8001 abort ();
8002
8003 if (FIELD_INNER_ACCESS (decl))
8004 return FIELD_INNER_ACCESS (decl);
8005
8006 push_obstacks (&permanent_obstack, &permanent_obstack);
8007
8008 /* Create the identifier and a function named after it. */
8009 id = build_new_access_id ();
8010
8011 /* The identifier is marked as bearing the name of a generated write
8012 access function for outer field accessed from inner classes. */
8013 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8014
8015 /* Create the read access */
8016 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8017 TREE_CHAIN (args) = end_params_node;
8018 stmt = make_qualified_primary (build_wfl_node (inst_id),
8019 build_wfl_node (DECL_NAME (decl)), 0);
8020 stmt = build_return (0, stmt);
8021 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8022 TREE_TYPE (decl), id, args, stmt);
8023 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8024
8025 /* Create the write access method */
8026 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8027 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8028 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8029 stmt = make_qualified_primary (build_wfl_node (inst_id),
8030 build_wfl_node (DECL_NAME (decl)), 0);
8031 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8032 build_wfl_node (wpv_id)));
8033
8034 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8035 TREE_TYPE (decl), id, args, stmt);
8036 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8037 pop_obstacks ();
8038
8039 /* Return the access name */
8040 return FIELD_INNER_ACCESS (decl) = id;
8041}
8042
8043/* Build an field access method NAME. */
8044
8045static tree
8046build_outer_field_access_method (class, type, name, args, body)
8047 tree class, type, name, args, body;
8048{
8049 tree saved_current_function_decl, mdecl;
8050
8051 /* Create the method */
8052 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8053 fix_method_argument_names (args, mdecl);
8054 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8055
8056 /* Attach the method body. */
8057 saved_current_function_decl = current_function_decl;
8058 start_artificial_method_body (mdecl);
8059 java_method_add_stmt (mdecl, body);
8060 end_artificial_method_body (mdecl);
8061 current_function_decl = saved_current_function_decl;
8062
8063 return mdecl;
8064}
8065
8066\f
8067/* This section deals with building access function necessary for
8068 certain kinds of method invocation from inner classes. */
8069
8070static tree
8071build_outer_method_access_method (decl)
8072 tree decl;
8073{
8074 tree saved_current_function_decl, mdecl;
8075 tree args = NULL_TREE, call_args = NULL_TREE;
8076 tree carg, id, body, class;
8077 char buffer [80];
8078 int parm_id_count = 0;
8079
8080 /* Test this abort with an access to a private field */
8081 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8082 abort ();
8083
8084 /* Check the cache first */
8085 if (DECL_FUNCTION_INNER_ACCESS (decl))
8086 return DECL_FUNCTION_INNER_ACCESS (decl);
8087
8088 class = DECL_CONTEXT (decl);
8089
8090 /* Obtain an access identifier and mark it */
8091 id = build_new_access_id ();
8092 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8093
8094 push_obstacks (&permanent_obstack, &permanent_obstack);
8095
8096 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8097 /* Create the arguments, as much as the original */
8098 for (; carg && carg != end_params_node;
8099 carg = TREE_CHAIN (carg))
8100 {
8101 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8102 args = chainon (args, build_tree_list (get_identifier (buffer),
8103 TREE_VALUE (carg)));
8104 }
8105 args = chainon (args, end_params_node);
8106
8107 /* Create the method */
8108 mdecl = create_artificial_method (class, ACC_STATIC,
8109 TREE_TYPE (TREE_TYPE (decl)), id, args);
8110 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8111 /* There is a potential bug here. We should be able to use
8112 fix_method_argument_names, but then arg names get mixed up and
8113 eventually a constructor will have its this$0 altered and the
8114 outer context won't be assignment properly. The test case is
8115 stub.java FIXME */
8116 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8117
8118 /* Attach the method body. */
8119 saved_current_function_decl = current_function_decl;
8120 start_artificial_method_body (mdecl);
8121
8122 /* The actual method invocation uses the same args. When invoking a
8123 static methods that way, we don't want to skip the first
8124 argument. */
8125 carg = args;
8126 if (!METHOD_STATIC (decl))
8127 carg = TREE_CHAIN (carg);
8128 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8129 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8130 call_args);
8131
8132 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8133 call_args);
8134 if (!METHOD_STATIC (decl))
8135 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8136 body, 0);
8137 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8138 body = build_return (0, body);
8139 java_method_add_stmt (mdecl,body);
8140 end_artificial_method_body (mdecl);
8141 current_function_decl = saved_current_function_decl;
8142 pop_obstacks ();
8143
8144 /* Back tag the access function so it know what it accesses */
8145 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8146
8147 /* Tag the current method so it knows it has an access generated */
8148 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8149}
8150
8151\f
8152/* This section of the code deals with building expressions to access
8153 the enclosing instance of an inner class. The enclosing instance is
8154 kept in a generated field called this$<n>, with <n> being the
8155 inner class nesting level (starting from 0.) */
8156
8157/* Build an access to a given this$<n>, possibly by chaining access
8158 call to others. Access methods to this$<n> are build on the fly if
8159 necessary */
8160
8161static tree
8162build_access_to_thisn (from, to, lc)
8163 tree from, to;
8164 int lc;
8165{
8166 tree access = NULL_TREE;
8167
8168 while (from != to)
8169 {
8170 tree access0_wfl, cn;
8171
8172 maybe_build_thisn_access_method (from);
8173 access0_wfl = build_wfl_node (access0_identifier_node);
8174 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8175 EXPR_WFL_LINECOL (access0_wfl) = lc;
8176
8177 if (!access)
8178 {
8179 access = build_current_thisn (current_class);
8180 access = build_wfl_node (access);
8181 }
8182 access = build_tree_list (NULL_TREE, access);
8183 access = build_method_invocation (access0_wfl, access);
8184 access = make_qualified_primary (cn, access, lc);
8185
8186 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8187 }
8188 return access;
8189}
8190
8191/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8192 is returned if nothing needs to be generated. Otherwise, the method
152de068 8193 generated and a method decl is returned.
c2952b01
APB
8194
8195 NOTE: These generated methods should be declared in a class file
8196 attribute so that they can't be referred to directly. */
8197
8198static tree
8199maybe_build_thisn_access_method (type)
8200 tree type;
8201{
8202 tree mdecl, args, stmt, rtype;
8203 tree saved_current_function_decl;
8204
8205 /* If TYPE is a top-level class, no access method is required.
8206 If there already is such an access method, bail out. */
8207 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8208 return NULL_TREE;
8209
8210 /* We generate the method. The method looks like:
8211 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8212 */
8213 push_obstacks (&permanent_obstack, &permanent_obstack);
8214 args = build_tree_list (inst_id, build_pointer_type (type));
8215 TREE_CHAIN (args) = end_params_node;
8216 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8217 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8218 access0_identifier_node, args);
8219 fix_method_argument_names (args, mdecl);
8220 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8221 stmt = build_current_thisn (type);
8222 stmt = make_qualified_primary (build_wfl_node (inst_id),
8223 build_wfl_node (stmt), 0);
8224 stmt = build_return (0, stmt);
8225
8226 saved_current_function_decl = current_function_decl;
8227 start_artificial_method_body (mdecl);
8228 java_method_add_stmt (mdecl, stmt);
8229 end_artificial_method_body (mdecl);
8230 current_function_decl = saved_current_function_decl;
8231 pop_obstacks ();
8232
8233 CLASS_ACCESS0_GENERATED_P (type) = 1;
8234
8235 return mdecl;
8236}
8237
8238/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8239 the first level of innerclassing. this$1 for the next one, etc...
8240 This function can be invoked with TYPE to NULL, available and then
8241 has to count the parser context. */
8242
8243static tree
8244build_current_thisn (type)
8245 tree type;
8246{
8247 static int saved_i = -1;
8248 static tree saved_thisn = NULL_TREE;
19e223db
MM
8249 static tree saved_type = NULL_TREE;
8250 static int saved_type_i = 0;
8251 static int initialized_p;
c2952b01
APB
8252 tree decl;
8253 char buffer [80];
8254 int i = 0;
8255
19e223db
MM
8256 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8257 if (!initialized_p)
c2952b01 8258 {
19e223db
MM
8259 ggc_add_tree_root (&saved_thisn, 1);
8260 ggc_add_tree_root (&saved_type, 1);
8261 initialized_p = 1;
8262 }
c2952b01 8263
19e223db
MM
8264 if (type)
8265 {
c2952b01
APB
8266 if (type == saved_type)
8267 i = saved_type_i;
8268 else
8269 {
8270 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8271 decl; decl = DECL_CONTEXT (decl), i++)
8272 ;
8273
8274 saved_type = type;
8275 saved_type_i = i;
8276 }
8277 }
8278 else
8279 i = list_length (GET_CPC_LIST ())-2;
8280
8281 if (i == saved_i)
8282 return saved_thisn;
8283
8284 sprintf (buffer, "this$%d", i);
8285 saved_i = i;
8286 saved_thisn = get_identifier (buffer);
8287 return saved_thisn;
8288}
8289
8290/* Return the assignement to the hidden enclosing context `this$<n>'
8291 by the second incoming parameter to the innerclass constructor. The
8292 form used is `this.this$<n> = this$<n>;'. */
8293
8294static tree
8295build_thisn_assign ()
8296{
8297 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8298 {
8299 tree thisn = build_current_thisn (current_class);
8300 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8301 build_wfl_node (thisn), 0);
8302 tree rhs = build_wfl_node (thisn);
8303 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8304 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8305 }
8306 return NULL_TREE;
8307}
8308
8309\f
165f37bc
APB
8310/* Building the synthetic `class$' used to implement the `.class' 1.1
8311 extension for non primitive types. This method looks like:
8312
8313 static Class class$(String type) throws NoClassDefFoundError
8314 {
8315 try {return (java.lang.Class.forName (String));}
8316 catch (ClassNotFoundException e) {
8317 throw new NoClassDefFoundError(e.getMessage());}
8318 } */
8319
8320static tree
8321build_dot_class_method (class)
8322 tree class;
8323{
8324#define BWF(S) build_wfl_node (get_identifier ((S)))
8325#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8326 tree args, tmp, saved_current_function_decl, mdecl;
8327 tree stmt, throw_stmt, catch, catch_block, try_block;
8328 tree catch_clause_param;
8329 tree class_not_found_exception, no_class_def_found_error;
8330
8331 static tree get_message_wfl, type_parm_wfl;
8332
8333 if (!get_message_wfl)
8334 {
8335 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8336 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8337 ggc_add_tree_root (&get_message_wfl, 1);
8338 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8339 }
8340
8341 /* Build the arguments */
8342 args = build_tree_list (get_identifier ("type$"),
8343 build_pointer_type (string_type_node));
8344 TREE_CHAIN (args) = end_params_node;
8345
8346 /* Build the qualified name java.lang.Class.forName */
8347 tmp = MQN (MQN (MQN (BWF ("java"),
8348 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8349
8350 /* For things we have to catch and throw */
8351 class_not_found_exception =
8352 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8353 no_class_def_found_error =
8354 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8355 load_class (class_not_found_exception, 1);
8356 load_class (no_class_def_found_error, 1);
8357
8358 /* Create the "class$" function */
8359 mdecl = create_artificial_method (class, ACC_STATIC,
8360 build_pointer_type (class_type_node),
8361 get_identifier ("class$"), args);
8362 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8363 no_class_def_found_error);
8364
8365 /* We start by building the try block. We need to build:
8366 return (java.lang.Class.forName (type)); */
8367 stmt = build_method_invocation (tmp,
8368 build_tree_list (NULL_TREE, type_parm_wfl));
8369 stmt = build_return (0, stmt);
8370 /* Put it in a block. That's the try block */
8371 try_block = build_expr_block (stmt, NULL_TREE);
8372
8373 /* Now onto the catch block. We start by building the expression
8374 throwing a new exception:
8375 throw new NoClassDefFoundError (_.getMessage); */
8376 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8377 get_message_wfl, 0);
8378 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8379
8380 /* Build new NoClassDefFoundError (_.getMessage) */
8381 throw_stmt = build_new_invocation
8382 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8383 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8384
8385 /* Build the throw, (it's too early to use BUILD_THROW) */
8386 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8387
8388 /* Build the catch block to encapsulate all this. We begin by
8389 building an decl for the catch clause parameter and link it to
8390 newly created block, the catch block. */
8391 catch_clause_param =
8392 build_decl (VAR_DECL, wpv_id,
8393 build_pointer_type (class_not_found_exception));
8394 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8395
8396 /* We initialize the variable with the exception handler. */
8397 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8398 soft_exceptioninfo_call_node);
8399 add_stmt_to_block (catch_block, NULL_TREE, catch);
8400
8401 /* We add the statement throwing the new exception */
8402 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8403
8404 /* Build a catch expression for all this */
8405 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8406
8407 /* Build the try/catch sequence */
8408 stmt = build_try_statement (0, try_block, catch_block);
8409
8410 fix_method_argument_names (args, mdecl);
8411 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8412 saved_current_function_decl = current_function_decl;
8413 start_artificial_method_body (mdecl);
8414 java_method_add_stmt (mdecl, stmt);
8415 end_artificial_method_body (mdecl);
8416 current_function_decl = saved_current_function_decl;
8417 TYPE_DOT_CLASS (class) = mdecl;
8418
8419 return mdecl;
8420}
8421
8422static tree
f0f3a777
APB
8423build_dot_class_method_invocation (type)
8424 tree type;
165f37bc 8425{
f0f3a777
APB
8426 tree sig_id, s;
8427
8428 if (TYPE_ARRAY_P (type))
8429 sig_id = build_java_signature (type);
8430 else
8431 sig_id = DECL_NAME (TYPE_NAME (type));
8432
8433 s = make_node (STRING_CST);
8434 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (sig_id);
165f37bc
APB
8435 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8436 TREE_STRING_LENGTH (s)+1);
f0f3a777 8437 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8438 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8439 build_tree_list (NULL_TREE, s));
8440}
8441
c2952b01
APB
8442/* This section of the code deals with constructor. */
8443
22eed1e6
APB
8444/* Craft a body for default constructor. Patch existing constructor
8445 bodies with call to super() and field initialization statements if
8446 necessary. */
8447
8448static void
8449fix_constructors (mdecl)
8450 tree mdecl;
8451{
8452 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8453 tree thisn_assign, compound = NULL_TREE;
8454 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8455
22eed1e6
APB
8456 if (!body)
8457 {
22eed1e6
APB
8458 /* It is an error for the compiler to generate a default
8459 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8460 takes no argument, or the same args for an anonymous class */
8461 if (verify_constructor_super (mdecl))
22eed1e6 8462 {
c2952b01
APB
8463 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8464 tree save = DECL_NAME (mdecl);
49f48c71 8465 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8466 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8467 parse_error_context
c2952b01
APB
8468 (lookup_cl (TYPE_NAME (class_type)),
8469 "No constructor matching `%s' found in class `%s'",
8470 lang_printable_name (mdecl, 0), n);
8471 DECL_NAME (mdecl) = save;
22eed1e6
APB
8472 }
8473
c2952b01
APB
8474 /* The constructor body must be crafted by hand. It's the
8475 constructor we defined when we realize we didn't have the
8476 CLASSNAME() constructor */
22eed1e6
APB
8477 start_artificial_method_body (mdecl);
8478
f0f3a777
APB
8479 /* Insert an assignment to the this$<n> hidden field, if
8480 necessary */
8481 if ((thisn_assign = build_thisn_assign ()))
8482 java_method_add_stmt (mdecl, thisn_assign);
8483
22eed1e6
APB
8484 /* We don't generate a super constructor invocation if we're
8485 compiling java.lang.Object. build_super_invocation takes care
8486 of that. */
e920ebc9 8487 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8488
c2952b01
APB
8489 /* Insert the instance initializer block right here, after the
8490 super invocation. */
8491 add_instance_initializer (mdecl);
8492
22eed1e6
APB
8493 end_artificial_method_body (mdecl);
8494 }
8495 /* Search for an explicit constructor invocation */
8496 else
8497 {
8498 int found = 0;
8499 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8500
8501 while (body)
8502 switch (TREE_CODE (body))
8503 {
8504 case CALL_EXPR:
8505 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8506 body = NULL_TREE;
8507 break;
8508 case COMPOUND_EXPR:
8509 case EXPR_WITH_FILE_LOCATION:
8510 body = TREE_OPERAND (body, 0);
8511 break;
8512 case BLOCK:
8513 body = BLOCK_EXPR_BODY (body);
8514 break;
8515 default:
8516 found = 0;
8517 body = NULL_TREE;
8518 }
8519 /* The constructor is missing an invocation of super() */
8520 if (!found)
8521 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8522 build_super_invocation (mdecl));
22eed1e6 8523
c2952b01
APB
8524 /* Generate the assignment to this$<n>, if necessary */
8525 if ((thisn_assign = build_thisn_assign ()))
8526 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8527
f0f3a777
APB
8528 /* Insert the instance initializer block right here, after the
8529 super invocation. */
8530 add_instance_initializer (mdecl);
8531
22eed1e6
APB
8532 /* Fix the constructor main block if we're adding extra stmts */
8533 if (compound)
8534 {
8535 compound = add_stmt_to_compound (compound, NULL_TREE,
8536 BLOCK_EXPR_BODY (main_block));
8537 BLOCK_EXPR_BODY (main_block) = compound;
8538 }
8539 }
8540}
8541
8542/* Browse constructors in the super class, searching for a constructor
8543 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8544 otherwise. If the current class is an anonymous inner class, look
8545 for something that has the same signature. */
22eed1e6
APB
8546
8547static int
c2952b01
APB
8548verify_constructor_super (mdecl)
8549 tree mdecl;
22eed1e6
APB
8550{
8551 tree class = CLASSTYPE_SUPER (current_class);
152de068 8552 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8553 tree sdecl;
8554
22eed1e6
APB
8555 if (!class)
8556 return 0;
8557
c2952b01 8558 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8559 {
c2952b01
APB
8560 tree mdecl_arg_type;
8561 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8562 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8563 if (DECL_CONSTRUCTOR_P (sdecl))
8564 {
cf1b2274 8565 tree m_arg_type;
152de068
APB
8566 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8567 if (super_inner)
8568 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8569 for (m_arg_type = mdecl_arg_type;
8570 (arg_type != end_params_node
8571 && m_arg_type != end_params_node);
c2952b01 8572 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8573 m_arg_type = TREE_CHAIN (m_arg_type))
8574 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8575 break;
8576
cf1b2274 8577 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8578 return 0;
8579 }
8580 }
8581 else
8582 {
8583 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8584 {
152de068
APB
8585 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8586 if (super_inner)
8587 arg = TREE_CHAIN (arg);
8588 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8589 return 0;
8590 }
8591 }
8592 return 1;
8593}
8594
22eed1e6 8595/* Generate code for all context remembered for code generation. */
b351b287
APB
8596
8597void
8598java_expand_classes ()
8599{
5423609c 8600 int save_error_count = 0;
c2952b01
APB
8601 static struct parser_ctxt *saved_ctxp = NULL;
8602
23a79c61
APB
8603 java_parse_abort_on_error ();
8604 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8605 return;
8606 java_layout_classes ();
8607 java_parse_abort_on_error ();
8608
c2952b01 8609 saved_ctxp = ctxp_for_generation;
b351b287
APB
8610 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8611 {
8612 ctxp = ctxp_for_generation;
8613 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8614 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8615 java_parse_abort_on_error ();
8616 }
c2952b01
APB
8617
8618 /* Find anonymous classes and expand their constructor, now they
8619 have been fixed. */
8620 for (ctxp_for_generation = saved_ctxp;
8621 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8622 {
8623 tree current;
8624 ctxp = ctxp_for_generation;
8625 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8626 {
8627 current_class = TREE_TYPE (current);
8628 if (ANONYMOUS_CLASS_P (current_class))
8629 {
8630 tree d;
8631 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8632 {
8633 if (DECL_CONSTRUCTOR_P (d))
8634 {
8635 restore_line_number_status (1);
8636 reset_method_name (d);
8637 java_complete_expand_method (d);
8638 restore_line_number_status (0);
8639 break; /* We now there are no other ones */
8640 }
8641 }
8642 }
8643 }
8644 }
8645
8646 /* If we've found error at that stage, don't try to generate
8647 anything, unless we're emitting xrefs or checking the syntax only
8648 (but not using -fsyntax-only for the purpose of generating
8649 bytecode. */
8650 if (java_error_count && !flag_emit_xref
8651 && (!flag_syntax_only && !flag_emit_class_files))
8652 return;
8653
8654 /* Now things are stable, go for generation of the class data. */
8655 for (ctxp_for_generation = saved_ctxp;
8656 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8657 {
8658 tree current;
8659 ctxp = ctxp_for_generation;
8660 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8661 {
8662 current_class = TREE_TYPE (current);
8663 outgoing_cpool = TYPE_CPOOL (current_class);
8664 if (flag_emit_class_files)
8665 write_classfile (current_class);
8666 if (flag_emit_xref)
8667 expand_xref (current_class);
8668 else if (! flag_syntax_only)
8669 finish_class ();
8670 }
8671 }
b351b287
APB
8672}
8673
e04a16fb
AG
8674/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8675 a tree list node containing RIGHT. Fore coming RIGHTs will be
8676 chained to this hook. LOCATION contains the location of the
8677 separating `.' operator. */
8678
8679static tree
8680make_qualified_primary (primary, right, location)
8681 tree primary, right;
8682 int location;
8683{
8684 tree wfl;
8685
c2952b01 8686 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8687 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8688 else
8689 {
8690 wfl = primary;
c2952b01
APB
8691 /* If wfl wasn't qualified, we build a first anchor */
8692 if (!EXPR_WFL_QUALIFICATION (wfl))
8693 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8694 }
8695
c2952b01 8696 /* And chain them */
e04a16fb
AG
8697 EXPR_WFL_LINECOL (right) = location;
8698 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8699 PRIMARY_P (wfl) = 1;
8700 return wfl;
8701}
8702
8703/* Simple merge of two name separated by a `.' */
8704
8705static tree
8706merge_qualified_name (left, right)
8707 tree left, right;
8708{
8709 tree node;
c2952b01
APB
8710 if (!left && !right)
8711 return NULL_TREE;
8712
8713 if (!left)
8714 return right;
8715
8716 if (!right)
8717 return left;
8718
e04a16fb
AG
8719 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8720 IDENTIFIER_LENGTH (left));
8721 obstack_1grow (&temporary_obstack, '.');
8722 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8723 IDENTIFIER_LENGTH (right));
8724 node = get_identifier (obstack_base (&temporary_obstack));
8725 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8726 QUALIFIED_P (node) = 1;
8727 return node;
8728}
8729
8730/* Merge the two parts of a qualified name into LEFT. Set the
8731 location information of the resulting node to LOCATION, usually
8732 inherited from the location information of the `.' operator. */
8733
8734static tree
8735make_qualified_name (left, right, location)
8736 tree left, right;
8737 int location;
8738{
bc3ca41b
PB
8739#ifdef USE_COMPONENT_REF
8740 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8741 EXPR_WFL_LINECOL (node) = location;
8742 return node;
8743#else
e04a16fb
AG
8744 tree left_id = EXPR_WFL_NODE (left);
8745 tree right_id = EXPR_WFL_NODE (right);
8746 tree wfl, merge;
8747
8748 merge = merge_qualified_name (left_id, right_id);
8749
8750 /* Left wasn't qualified and is now qualified */
8751 if (!QUALIFIED_P (left_id))
8752 {
8753 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8754 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8755 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8756 }
8757
8758 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8759 EXPR_WFL_LINECOL (wfl) = location;
8760 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8761
8762 EXPR_WFL_NODE (left) = merge;
8763 return left;
bc3ca41b 8764#endif
e04a16fb
AG
8765}
8766
8767/* Extract the last identifier component of the qualified in WFL. The
8768 last identifier is removed from the linked list */
8769
8770static tree
8771cut_identifier_in_qualified (wfl)
8772 tree wfl;
8773{
8774 tree q;
8775 tree previous = NULL_TREE;
8776 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8777 if (!TREE_CHAIN (q))
8778 {
8779 if (!previous)
781b0558 8780 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8781 TREE_CHAIN (previous) = NULL_TREE;
8782 return TREE_PURPOSE (q);
8783 }
8784}
8785
8786/* Resolve the expression name NAME. Return its decl. */
8787
8788static tree
5e942c50 8789resolve_expression_name (id, orig)
e04a16fb 8790 tree id;
5e942c50 8791 tree *orig;
e04a16fb
AG
8792{
8793 tree name = EXPR_WFL_NODE (id);
8794 tree decl;
8795
8796 /* 6.5.5.1: Simple expression names */
8797 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8798 {
8799 /* 15.13.1: NAME can appear within the scope of a local variable
8800 declaration */
8801 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8802 return decl;
8803
8804 /* 15.13.1: NAME can appear within a class declaration */
8805 else
8806 {
8807 decl = lookup_field_wrapper (current_class, name);
8808 if (decl)
8809 {
c2952b01 8810 tree access = NULL_TREE;
e04a16fb 8811 int fs = FIELD_STATIC (decl);
f2760b27
APB
8812
8813 /* If we're accessing an outer scope local alias, make
8814 sure we change the name of the field we're going to
8815 build access to. */
8816 if (FIELD_LOCAL_ALIAS_USED (decl))
8817 name = DECL_NAME (decl);
8818
e04a16fb
AG
8819 /* Instance variable (8.3.1.1) can't appear within
8820 static method, static initializer or initializer for
8821 a static variable. */
8822 if (!fs && METHOD_STATIC (current_function_decl))
8823 {
7f10c2e2 8824 static_ref_err (id, name, current_class);
e04a16fb
AG
8825 return error_mark_node;
8826 }
22eed1e6
APB
8827 /* Instance variables can't appear as an argument of
8828 an explicit constructor invocation */
8829 if (!fs && ctxp->explicit_constructor_p)
8830 {
8831 parse_error_context
781b0558 8832 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8833 return error_mark_node;
8834 }
5e942c50 8835
c2952b01
APB
8836 /* If we're processing an inner class and we're trying
8837 to access a field belonging to an outer class, build
8838 the access to the field */
8839 if (!fs && outer_field_access_p (current_class, decl))
8840 return build_outer_field_access (id, decl);
8841
5e942c50 8842 /* Otherwise build what it takes to access the field */
c2952b01
APB
8843 access = build_field_ref ((fs ? NULL_TREE : current_this),
8844 DECL_CONTEXT (decl), name);
e8fc7396 8845 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8846 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8847 /* We may be asked to save the real field access node */
8848 if (orig)
c2952b01 8849 *orig = access;
5e942c50 8850 /* And we return what we got */
c2952b01 8851 return access;
e04a16fb
AG
8852 }
8853 /* Fall down to error report on undefined variable */
8854 }
8855 }
8856 /* 6.5.5.2 Qualified Expression Names */
8857 else
8858 {
5e942c50
APB
8859 if (orig)
8860 *orig = NULL_TREE;
e04a16fb
AG
8861 qualify_ambiguous_name (id);
8862 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8863 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8864 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8865 }
8866
8867 /* We've got an error here */
8868 parse_error_context (id, "Undefined variable `%s'",
8869 IDENTIFIER_POINTER (name));
8870
8871 return error_mark_node;
8872}
8873
7f10c2e2
APB
8874static void
8875static_ref_err (wfl, field_id, class_type)
8876 tree wfl, field_id, class_type;
8877{
8878 parse_error_context
8879 (wfl,
8880 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8881 IDENTIFIER_POINTER (field_id),
8882 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8883}
8884
e04a16fb
AG
8885/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8886 We return something suitable to generate the field access. We also
8887 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8888 recipient's address can be null. */
8889
8890static tree
8891resolve_field_access (qual_wfl, field_decl, field_type)
8892 tree qual_wfl;
8893 tree *field_decl, *field_type;
8894{
8895 int is_static = 0;
8896 tree field_ref;
8897 tree decl, where_found, type_found;
8898
8899 if (resolve_qualified_expression_name (qual_wfl, &decl,
8900 &where_found, &type_found))
8901 return error_mark_node;
8902
8903 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8904 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8905 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8906 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8907 {
8908 tree length = build_java_array_length_access (where_found);
8909 field_ref =
8910 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8911
8912 /* In case we're dealing with a static array, we need to
8913 initialize its class before the array length can be fetched.
8914 It's also a good time to create a DECL_RTL for the field if
8915 none already exists, otherwise if the field was declared in a
8916 class found in an external file and hasn't been (and won't
8917 be) accessed for its value, none will be created. */
8918 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8919 {
8920 build_static_field_ref (where_found);
8921 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8922 }
e04a16fb
AG
8923 }
8924 /* We might have been trying to resolve field.method(). In which
8925 case, the resolution is over and decl is the answer */
34f4db93 8926 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8927 field_ref = decl;
34f4db93 8928 else if (JDECL_P (decl))
e04a16fb 8929 {
5e942c50
APB
8930 int static_final_found = 0;
8931 if (!type_found)
8932 type_found = DECL_CONTEXT (decl);
34f4db93 8933 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
0c2b8145 8934 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
5e942c50 8935 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8936 && DECL_INITIAL (decl))
5e942c50 8937 {
0c2b8145
APB
8938 /* When called on a FIELD_DECL of the right (primitive)
8939 type, java_complete_tree will try to substitue the decl
8940 for it's initial value. */
70541f45 8941 field_ref = java_complete_tree (decl);
5e942c50
APB
8942 static_final_found = 1;
8943 }
8944 else
7f10c2e2
APB
8945 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8946 NULL_TREE : where_found),
5e942c50 8947 type_found, DECL_NAME (decl));
e04a16fb
AG
8948 if (field_ref == error_mark_node)
8949 return error_mark_node;
e8fc7396
APB
8950 if (is_static && !static_final_found
8951 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8952 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8953 }
8954 else
8955 field_ref = decl;
8956
8957 if (field_decl)
8958 *field_decl = decl;
8959 if (field_type)
c877974e
APB
8960 *field_type = (QUAL_DECL_TYPE (decl) ?
8961 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8962 return field_ref;
8963}
8964
e28cd97b
APB
8965/* If NODE is an access to f static field, strip out the class
8966 initialization part and return the field decl, otherwise, return
8967 NODE. */
8968
8969static tree
8970strip_out_static_field_access_decl (node)
8971 tree node;
8972{
8973 if (TREE_CODE (node) == COMPOUND_EXPR)
8974 {
8975 tree op1 = TREE_OPERAND (node, 1);
8976 if (TREE_CODE (op1) == COMPOUND_EXPR)
8977 {
8978 tree call = TREE_OPERAND (op1, 0);
8979 if (TREE_CODE (call) == CALL_EXPR
8980 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8981 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8982 == soft_initclass_node)
8983 return TREE_OPERAND (op1, 1);
8984 }
2f11d407
TT
8985 else if (JDECL_P (op1))
8986 return op1;
e28cd97b
APB
8987 }
8988 return node;
8989}
8990
e04a16fb
AG
8991/* 6.5.5.2: Qualified Expression Names */
8992
8993static int
8994resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8995 tree wfl;
8996 tree *found_decl, *type_found, *where_found;
8997{
8998 int from_type = 0; /* Field search initiated from a type */
c2952b01 8999 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
9000 int previous_call_static = 0;
9001 int is_static;
9002 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
9003 /* For certain for of inner class instantiation */
9004 tree saved_current, saved_this;
9005#define RESTORE_THIS_AND_CURRENT_CLASS \
9006 { current_class = saved_current; current_this = saved_this;}
9007
c877974e 9008 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
9009
9010 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9011 {
9012 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
9013 tree ret_decl; /* for EH checking */
9014 int location; /* for EH checking */
e04a16fb
AG
9015
9016 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
9017 switch (TREE_CODE (qual_wfl))
9018 {
9019 case CALL_EXPR:
b67d701b 9020 case NEW_CLASS_EXPR:
e04a16fb
AG
9021 /* If the access to the function call is a non static field,
9022 build the code to access it. */
34f4db93 9023 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9024 {
ac825856
APB
9025 decl = maybe_access_field (decl, *where_found,
9026 DECL_CONTEXT (decl));
e04a16fb
AG
9027 if (decl == error_mark_node)
9028 return 1;
9029 }
c2952b01 9030
e04a16fb
AG
9031 /* And code for the function call */
9032 if (complete_function_arguments (qual_wfl))
9033 return 1;
c2952b01
APB
9034
9035 /* We might have to setup a new current class and a new this
9036 for the search of an inner class, relative to the type of
9037 a expression resolved as `decl'. The current values are
9038 saved and restored shortly after */
9039 saved_current = current_class;
9040 saved_this = current_this;
9041 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
9042 {
9043 current_class = type;
9044 current_this = decl;
9045 }
9046
89e09b9a
PB
9047 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9048 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9049 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9050 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9051 *where_found = patch_method_invocation (qual_wfl, decl, type,
9052 &is_static, &ret_decl);
e04a16fb 9053 if (*where_found == error_mark_node)
c2952b01
APB
9054 {
9055 RESTORE_THIS_AND_CURRENT_CLASS;
9056 return 1;
9057 }
e04a16fb
AG
9058 *type_found = type = QUAL_DECL_TYPE (*where_found);
9059
c2952b01
APB
9060 /* If we're creating an inner class instance, check for that
9061 an enclosing instance is in scope */
9062 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9063 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9064 {
9065 parse_error_context
165f37bc
APB
9066 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9067 lang_printable_name (type, 0),
9068 (!current_this ? "" :
9069 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9070 RESTORE_THIS_AND_CURRENT_CLASS;
9071 return 1;
9072 }
9073
9074 /* In case we had to change then to resolve a inner class
9075 instantiation using a primary qualified by a `new' */
9076 RESTORE_THIS_AND_CURRENT_CLASS;
9077
34d4df06
APB
9078 /* EH check. No check on access$<n> functions */
9079 if (location
9080 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9081 (DECL_NAME (current_function_decl)))
7705e9db
APB
9082 check_thrown_exceptions (location, ret_decl);
9083
e04a16fb
AG
9084 /* If the previous call was static and this one is too,
9085 build a compound expression to hold the two (because in
9086 that case, previous function calls aren't transported as
9087 forcoming function's argument. */
9088 if (previous_call_static && is_static)
9089 {
9090 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9091 TREE_SIDE_EFFECTS (decl) = 1;
9092 }
9093 else
9094 {
9095 previous_call_static = is_static;
9096 decl = *where_found;
9097 }
c2952b01 9098 from_type = 0;
e04a16fb
AG
9099 continue;
9100
d8fccff5 9101 case NEW_ARRAY_EXPR:
c2952b01 9102 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9103 *where_found = decl = java_complete_tree (qual_wfl);
9104 if (decl == error_mark_node)
9105 return 1;
9106 *type_found = type = QUAL_DECL_TYPE (decl);
9107 CLASS_LOADED_P (type) = 1;
9108 continue;
9109
e04a16fb
AG
9110 case CONVERT_EXPR:
9111 *where_found = decl = java_complete_tree (qual_wfl);
9112 if (decl == error_mark_node)
9113 return 1;
9114 *type_found = type = QUAL_DECL_TYPE (decl);
9115 from_cast = 1;
9116 continue;
9117
22eed1e6 9118 case CONDITIONAL_EXPR:
5e942c50 9119 case STRING_CST:
ac22f9cb 9120 case MODIFY_EXPR:
22eed1e6
APB
9121 *where_found = decl = java_complete_tree (qual_wfl);
9122 if (decl == error_mark_node)
9123 return 1;
9124 *type_found = type = QUAL_DECL_TYPE (decl);
9125 continue;
9126
e04a16fb
AG
9127 case ARRAY_REF:
9128 /* If the access to the function call is a non static field,
9129 build the code to access it. */
34f4db93 9130 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9131 {
9132 decl = maybe_access_field (decl, *where_found, type);
9133 if (decl == error_mark_node)
9134 return 1;
9135 }
9136 /* And code for the array reference expression */
9137 decl = java_complete_tree (qual_wfl);
9138 if (decl == error_mark_node)
9139 return 1;
9140 type = QUAL_DECL_TYPE (decl);
9141 continue;
0a2138e2 9142
37feda7d
APB
9143 case PLUS_EXPR:
9144 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9145 return 1;
9146 if ((type = patch_string (decl)))
9147 decl = type;
9148 *where_found = QUAL_RESOLUTION (q) = decl;
9149 *type_found = type = TREE_TYPE (decl);
9150 break;
9151
165f37bc
APB
9152 case CLASS_LITERAL:
9153 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9154 return 1;
9155 *where_found = QUAL_RESOLUTION (q) = decl;
9156 *type_found = type = TREE_TYPE (decl);
9157 break;
9158
0a2138e2
APB
9159 default:
9160 /* Fix for -Wall Just go to the next statement. Don't
9161 continue */
a3f406ce 9162 break;
e04a16fb
AG
9163 }
9164
9165 /* If we fall here, we weren't processing a (static) function call. */
9166 previous_call_static = 0;
9167
9168 /* It can be the keyword THIS */
9169 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9170 {
9171 if (!current_this)
9172 {
9173 parse_error_context
9174 (wfl, "Keyword `this' used outside allowed context");
9175 return 1;
9176 }
f63991a8
APB
9177 if (ctxp->explicit_constructor_p)
9178 {
781b0558 9179 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9180 return 1;
9181 }
e04a16fb 9182 /* We have to generate code for intermediate acess */
c2952b01
APB
9183 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9184 {
9185 *where_found = decl = current_this;
9186 *type_found = type = QUAL_DECL_TYPE (decl);
9187 }
4dbf4496
APB
9188 /* We're trying to access the this from somewhere else. Make sure
9189 it's allowed before doing so. */
c2952b01
APB
9190 else
9191 {
4dbf4496
APB
9192 if (!enclosing_context_p (type, current_class))
9193 {
9194 char *p = xstrdup (lang_printable_name (type, 0));
9195 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9196 p, p,
9197 lang_printable_name (current_class, 0));
9198 free (p);
9199 return 1;
9200 }
c2952b01
APB
9201 *where_found = decl = build_current_thisn (type);
9202 from_qualified_this = 1;
9203 }
9204
9205 from_type = 0;
e04a16fb
AG
9206 continue;
9207 }
9208
9209 /* 15.10.2 Accessing Superclass Members using SUPER */
9210 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9211 {
9212 tree node;
9213 /* Check on the restricted use of SUPER */
9214 if (METHOD_STATIC (current_function_decl)
9215 || current_class == object_type_node)
9216 {
9217 parse_error_context
9218 (wfl, "Keyword `super' used outside allowed context");
9219 return 1;
9220 }
9221 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9222 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9223 CLASSTYPE_SUPER (current_class),
9224 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9225 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9226 if (decl == error_mark_node)
9227 return 1;
e04a16fb
AG
9228 *type_found = type = QUAL_DECL_TYPE (decl);
9229 from_super = from_type = 1;
9230 continue;
9231 }
9232
9233 /* 15.13.1: Can't search for field name in packages, so we
9234 assume a variable/class name was meant. */
9235 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9236 {
5e942c50
APB
9237 tree name = resolve_package (wfl, &q);
9238 if (name)
9239 {
c2952b01 9240 tree list;
5e942c50 9241 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9242 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9243 out. We're going to search something inside it. */
9244 *type_found = type = TREE_TYPE (decl);
9245 layout_class (type);
9246 from_type = 1;
c2952b01 9247
dde1da72
APB
9248 /* Fix them all the way down, if any are left. */
9249 if (q)
c2952b01 9250 {
dde1da72
APB
9251 list = TREE_CHAIN (q);
9252 while (list)
9253 {
9254 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9255 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9256 list = TREE_CHAIN (list);
9257 }
c2952b01 9258 }
5e942c50 9259 }
e04a16fb 9260 else
5e942c50
APB
9261 {
9262 if (from_super || from_cast)
9263 parse_error_context
9264 ((from_cast ? qual_wfl : wfl),
9265 "No variable `%s' defined in class `%s'",
9266 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9267 lang_printable_name (type, 0));
9268 else
9269 parse_error_context
9270 (qual_wfl, "Undefined variable or class name: `%s'",
9271 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9272 return 1;
9273 }
e04a16fb
AG
9274 }
9275
9276 /* We have a type name. It's been already resolved when the
9277 expression was qualified. */
9278 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9279 {
9280 if (!(decl = QUAL_RESOLUTION (q)))
9281 return 1; /* Error reported already */
9282
c2952b01
APB
9283 /* Sneak preview. If next we see a `new', we're facing a
9284 qualification with resulted in a type being selected
9285 instead of a field. Report the error */
9286 if(TREE_CHAIN (q)
9287 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9288 {
9289 parse_error_context (qual_wfl, "Undefined variable `%s'",
9290 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9291 return 1;
9292 }
9293
e04a16fb
AG
9294 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9295 {
9296 parse_error_context
9297 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9298 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9299 GET_TYPE_NAME (type),
e04a16fb
AG
9300 IDENTIFIER_POINTER (DECL_NAME (decl)),
9301 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9302 return 1;
9303 }
5e942c50 9304 check_deprecation (qual_wfl, decl);
c2952b01 9305
e04a16fb
AG
9306 type = TREE_TYPE (decl);
9307 from_type = 1;
9308 }
9309 /* We resolve and expression name */
9310 else
9311 {
cd531a2e 9312 tree field_decl = NULL_TREE;
e04a16fb
AG
9313
9314 /* If there exists an early resolution, use it. That occurs
9315 only once and we know that there are more things to
9316 come. Don't do that when processing something after SUPER
9317 (we need more thing to be put in place below */
9318 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9319 {
9320 decl = QUAL_RESOLUTION (q);
c877974e 9321 if (!type)
5e942c50 9322 {
7f10c2e2
APB
9323 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9324 {
9325 if (current_this)
9326 *where_found = current_this;
9327 else
9328 {
9329 static_ref_err (qual_wfl, DECL_NAME (decl),
9330 current_class);
9331 return 1;
9332 }
f0f3a777
APB
9333 if (outer_field_access_p (current_class, decl))
9334 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9335 }
c877974e
APB
9336 else
9337 {
9338 *where_found = TREE_TYPE (decl);
9339 if (TREE_CODE (*where_found) == POINTER_TYPE)
9340 *where_found = TREE_TYPE (*where_found);
9341 }
5e942c50 9342 }
b67d701b 9343 }
e04a16fb
AG
9344
9345 /* We have to search for a field, knowing the type of its
9346 container. The flag FROM_TYPE indicates that we resolved
9347 the last member of the expression as a type name, which
5e942c50
APB
9348 means that for the resolution of this field, we'll look
9349 for other errors than if it was resolved as a member of
9350 an other field. */
e04a16fb
AG
9351 else
9352 {
9353 int is_static;
5e942c50
APB
9354 tree field_decl_type; /* For layout */
9355
e04a16fb
AG
9356 if (!from_type && !JREFERENCE_TYPE_P (type))
9357 {
9358 parse_error_context
9359 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9360 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9361 lang_printable_name (type, 0),
e04a16fb
AG
9362 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9363 return 1;
9364 }
9365
dc0b3eff
PB
9366 field_decl = lookup_field_wrapper (type,
9367 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9368
9369 /* Maybe what we're trying to access an inner class. */
9370 if (!field_decl)
9371 {
9372 tree ptr, inner_decl;
9373
9374 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9375 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9376 if (inner_decl)
9377 {
9378 check_inner_class_access (inner_decl, decl, qual_wfl);
9379 type = TREE_TYPE (inner_decl);
9380 decl = inner_decl;
9381 from_type = 1;
9382 continue;
9383 }
9384 }
9385
dc0b3eff 9386 if (field_decl == NULL_TREE)
e04a16fb
AG
9387 {
9388 parse_error_context
2aa11e97 9389 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9390 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9391 GET_TYPE_NAME (type));
e04a16fb
AG
9392 return 1;
9393 }
dc0b3eff
PB
9394 if (field_decl == error_mark_node)
9395 return 1;
5e942c50
APB
9396
9397 /* Layout the type of field_decl, since we may need
c877974e
APB
9398 it. Don't do primitive types or loaded classes. The
9399 situation of non primitive arrays may not handled
9400 properly here. FIXME */
5e942c50
APB
9401 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9402 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9403 else
9404 field_decl_type = TREE_TYPE (field_decl);
9405 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9406 && !CLASS_LOADED_P (field_decl_type)
9407 && !TYPE_ARRAY_P (field_decl_type))
9408 resolve_and_layout (field_decl_type, NULL_TREE);
9409 if (TYPE_ARRAY_P (field_decl_type))
9410 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9411
9412 /* Check on accessibility here */
9413 if (not_accessible_p (type, field_decl, from_super))
9414 {
9415 parse_error_context
9416 (qual_wfl,
9417 "Can't access %s field `%s.%s' from `%s'",
9418 java_accstring_lookup
9419 (get_access_flags_from_decl (field_decl)),
2aa11e97 9420 GET_TYPE_NAME (type),
e04a16fb
AG
9421 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9422 IDENTIFIER_POINTER
9423 (DECL_NAME (TYPE_NAME (current_class))));
9424 return 1;
9425 }
5e942c50 9426 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9427
9428 /* There are things to check when fields are accessed
9429 from type. There are no restrictions on a static
9430 declaration of the field when it is accessed from an
9431 interface */
9432 is_static = FIELD_STATIC (field_decl);
9433 if (!from_super && from_type
c2952b01
APB
9434 && !TYPE_INTERFACE_P (type)
9435 && !is_static
9436 && (current_function_decl
9437 && METHOD_STATIC (current_function_decl)))
e04a16fb 9438 {
7f10c2e2 9439 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9440 return 1;
9441 }
9442 from_cast = from_super = 0;
9443
c2952b01
APB
9444 /* It's an access from a type but it isn't static, we
9445 make it relative to `this'. */
9446 if (!is_static && from_type)
9447 decl = current_this;
9448
5e942c50
APB
9449 /* If we need to generate something to get a proper
9450 handle on what this field is accessed from, do it
9451 now. */
e04a16fb
AG
9452 if (!is_static)
9453 {
c583dd46 9454 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9455 if (decl == error_mark_node)
9456 return 1;
9457 }
9458
9459 /* We want to keep the location were found it, and the type
9460 we found. */
9461 *where_found = decl;
9462 *type_found = type;
9463
c2952b01
APB
9464 /* Generate the correct expression for field access from
9465 qualified this */
9466 if (from_qualified_this)
9467 {
9468 field_decl = build_outer_field_access (qual_wfl, field_decl);
9469 from_qualified_this = 0;
9470 }
9471
e04a16fb
AG
9472 /* This is the decl found and eventually the next one to
9473 search from */
9474 decl = field_decl;
9475 }
e04a16fb
AG
9476 from_type = 0;
9477 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9478
9479 /* Sneak preview. If decl is qualified by a `new', report
9480 the error here to be accurate on the peculiar construct */
9481 if (TREE_CHAIN (q)
9482 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9483 && !JREFERENCE_TYPE_P (type))
9484 {
9485 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9486 lang_printable_name (type, 0));
9487 return 1;
9488 }
e04a16fb 9489 }
dde1da72
APB
9490 /* `q' might have changed due to a after package resolution
9491 re-qualification */
9492 if (!q)
9493 break;
e04a16fb
AG
9494 }
9495 *found_decl = decl;
9496 return 0;
9497}
9498
9499/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9500 can't be accessed from REFERENCE (a record type). This should be
9501 used when decl is a field or a method.*/
e04a16fb 9502
be245ac0
KG
9503static int
9504not_accessible_p (reference, member, from_super)
e04a16fb
AG
9505 tree reference, member;
9506 int from_super;
9507{
9508 int access_flag = get_access_flags_from_decl (member);
9509
4dbf4496
APB
9510 /* Inner classes are processed by check_inner_class_access */
9511 if (INNER_CLASS_TYPE_P (reference))
9512 return 0;
9513
e04a16fb
AG
9514 /* Access always granted for members declared public */
9515 if (access_flag & ACC_PUBLIC)
9516 return 0;
9517
9518 /* Check access on protected members */
9519 if (access_flag & ACC_PROTECTED)
9520 {
9521 /* Access granted if it occurs from within the package
9522 containing the class in which the protected member is
9523 declared */
9524 if (class_in_current_package (DECL_CONTEXT (member)))
9525 return 0;
9526
9bbc7d9f
PB
9527 /* If accessed with the form `super.member', then access is granted */
9528 if (from_super)
9529 return 0;
e04a16fb 9530
9bbc7d9f 9531 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9532 member is declared or a subclass of it. Find the right
9533 context to perform the check */
9534 if (PURE_INNER_CLASS_TYPE_P (reference))
9535 {
9536 while (INNER_CLASS_TYPE_P (reference))
9537 {
9538 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9539 return 0;
9540 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9541 }
9542 }
473e7b07 9543 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9544 return 0;
e04a16fb
AG
9545 return 1;
9546 }
9547
9548 /* Check access on private members. Access is granted only if it
473e7b07 9549 occurs from within the class in which it is declared. Exceptions
4dbf4496 9550 are accesses from inner-classes. */
e04a16fb 9551 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9552 return (current_class == DECL_CONTEXT (member) ? 0 :
9553 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9554
9555 /* Default access are permitted only when occuring within the
9556 package in which the type (REFERENCE) is declared. In other words,
9557 REFERENCE is defined in the current package */
9558 if (ctxp->package)
9559 return !class_in_current_package (reference);
473e7b07 9560
e04a16fb
AG
9561 /* Otherwise, access is granted */
9562 return 0;
9563}
9564
5e942c50
APB
9565/* Test deprecated decl access. */
9566static void
9567check_deprecation (wfl, decl)
9568 tree wfl, decl;
9569{
49f48c71 9570 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9571 /* Complain if the field is deprecated and the file it was defined
9572 in isn't compiled at the same time the file which contains its
9573 use is */
9574 if (DECL_DEPRECATED (decl)
9575 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9576 {
9577 char the [20];
9578 switch (TREE_CODE (decl))
9579 {
9580 case FUNCTION_DECL:
9581 strcpy (the, "method");
9582 break;
9583 case FIELD_DECL:
9584 strcpy (the, "field");
9585 break;
9586 case TYPE_DECL:
9587 strcpy (the, "class");
9588 break;
15fdcfe9
PB
9589 default:
9590 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9591 }
9592 parse_warning_context
9593 (wfl, "The %s `%s' in class `%s' has been deprecated",
9594 the, lang_printable_name (decl, 0),
9595 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9596 }
9597}
9598
e04a16fb
AG
9599/* Returns 1 if class was declared in the current package, 0 otherwise */
9600
9601static int
9602class_in_current_package (class)
9603 tree class;
9604{
9605 static tree cache = NULL_TREE;
9606 int qualified_flag;
9607 tree left;
9608
9609 if (cache == class)
9610 return 1;
9611
9612 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9613
9614 /* If the current package is empty and the name of CLASS is
9615 qualified, class isn't in the current package. If there is a
9616 current package and the name of the CLASS is not qualified, class
9617 isn't in the current package */
0a2138e2 9618 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9619 return 0;
9620
9621 /* If there is not package and the name of CLASS isn't qualified,
9622 they belong to the same unnamed package */
9623 if (!ctxp->package && !qualified_flag)
9624 return 1;
9625
9626 /* Compare the left part of the name of CLASS with the package name */
9627 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9628 if (ctxp->package == left)
9629 {
19e223db
MM
9630 static int initialized_p;
9631 /* Register CACHE with the garbage collector. */
9632 if (!initialized_p)
9633 {
9634 ggc_add_tree_root (&cache, 1);
9635 initialized_p = 1;
9636 }
9637
e04a16fb
AG
9638 cache = class;
9639 return 1;
9640 }
9641 return 0;
9642}
9643
9644/* This function may generate code to access DECL from WHERE. This is
9645 done only if certain conditions meet. */
9646
9647static tree
9648maybe_access_field (decl, where, type)
9649 tree decl, where, type;
9650{
5e942c50
APB
9651 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9652 && !FIELD_STATIC (decl))
e04a16fb 9653 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9654 (type ? type : DECL_CONTEXT (decl)),
9655 DECL_NAME (decl));
e04a16fb
AG
9656 return decl;
9657}
9658
15fdcfe9 9659/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9660 and according to the situation, PRIMARY and WHERE may be
9661 used. IS_STATIC is set to 1 if the invoked function is static. */
9662
9663static tree
89e09b9a 9664patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9665 tree patch, primary, where;
9666 int *is_static;
b9f7e36c 9667 tree *ret_decl;
e04a16fb
AG
9668{
9669 tree wfl = TREE_OPERAND (patch, 0);
9670 tree args = TREE_OPERAND (patch, 1);
9671 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9672 tree list;
22eed1e6 9673 int is_static_flag = 0;
89e09b9a 9674 int is_super_init = 0;
bccaf73a 9675 tree this_arg = NULL_TREE;
e04a16fb
AG
9676
9677 /* Should be overriden if everything goes well. Otherwise, if
9678 something fails, it should keep this value. It stop the
9679 evaluation of a bogus assignment. See java_complete_tree,
9680 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9681 evaluating an assignment */
9682 TREE_TYPE (patch) = error_mark_node;
9683
9684 /* Since lookup functions are messing with line numbers, save the
9685 context now. */
9686 java_parser_context_save_global ();
9687
9688 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9689
9690 /* Resolution of qualified name, excluding constructors */
9691 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9692 {
dde1da72 9693 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9694 /* Extract the last IDENTIFIER of the qualified
9695 expression. This is a wfl and we will use it's location
9696 data during error report. */
9697 identifier_wfl = cut_identifier_in_qualified (wfl);
9698 identifier = EXPR_WFL_NODE (identifier_wfl);
9699
9700 /* Given the context, IDENTIFIER is syntactically qualified
9701 as a MethodName. We need to qualify what's before */
9702 qualify_ambiguous_name (wfl);
dde1da72 9703 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9704
dde1da72
APB
9705 if (resolved == error_mark_node)
9706 PATCH_METHOD_RETURN_ERROR ();
9707
9708 type = GET_SKIP_TYPE (resolved);
9709 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9710
9711 if (JPRIMITIVE_TYPE_P (type))
9712 {
9713 parse_error_context
9714 (identifier_wfl,
9715 "Can't invoke a method on primitive type `%s'",
9716 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9717 PATCH_METHOD_RETURN_ERROR ();
9718 }
9719
dde1da72
APB
9720 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9721 args = nreverse (args);
2c56429a 9722
e04a16fb 9723 /* We're resolving a call from a type */
dde1da72 9724 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9725 {
dde1da72 9726 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9727 {
9728 parse_error_context
781b0558
KG
9729 (identifier_wfl,
9730 "Can't make static reference to method `%s' in interface `%s'",
9731 IDENTIFIER_POINTER (identifier),
e04a16fb 9732 IDENTIFIER_POINTER (name));
b9f7e36c 9733 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9734 }
e04a16fb
AG
9735 if (list && !METHOD_STATIC (list))
9736 {
c2e3db92 9737 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9738 parse_error_context
9739 (identifier_wfl,
9740 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9741 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9742 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9743 free (fct_name);
b9f7e36c 9744 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9745 }
9746 }
e04a16fb 9747 else
dde1da72
APB
9748 this_arg = primary = resolved;
9749
5e942c50 9750 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9751 wfl = identifier_wfl;
9752 }
9753 /* Resolution of simple names, names generated after a primary: or
9754 constructors */
9755 else
9756 {
cd531a2e 9757 tree class_to_search = NULL_TREE;
c2952b01 9758 int lc; /* Looking for Constructor */
e04a16fb
AG
9759
9760 /* We search constructor in their target class */
9761 if (CALL_CONSTRUCTOR_P (patch))
9762 {
22eed1e6
APB
9763 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9764 class_to_search = EXPR_WFL_NODE (wfl);
9765 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9766 this_identifier_node)
9767 class_to_search = NULL_TREE;
9768 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9769 super_identifier_node)
e04a16fb 9770 {
89e09b9a 9771 is_super_init = 1;
22eed1e6
APB
9772 if (CLASSTYPE_SUPER (current_class))
9773 class_to_search =
9774 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9775 else
9776 {
781b0558 9777 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9778 PATCH_METHOD_RETURN_ERROR ();
9779 }
e04a16fb 9780 }
22eed1e6
APB
9781
9782 /* Class to search is NULL if we're searching the current one */
9783 if (class_to_search)
e04a16fb 9784 {
c2952b01
APB
9785 class_to_search = resolve_and_layout (class_to_search, wfl);
9786
22eed1e6
APB
9787 if (!class_to_search)
9788 {
9789 parse_error_context
9790 (wfl, "Class `%s' not found in type declaration",
9791 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9792 PATCH_METHOD_RETURN_ERROR ();
9793 }
9794
5e942c50
APB
9795 /* Can't instantiate an abstract class, but we can
9796 invoke it's constructor. It's use within the `new'
9797 context is denied here. */
9798 if (CLASS_ABSTRACT (class_to_search)
9799 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9800 {
9801 parse_error_context
781b0558
KG
9802 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9803 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9804 PATCH_METHOD_RETURN_ERROR ();
9805 }
c2952b01 9806
22eed1e6 9807 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9808 }
22eed1e6
APB
9809 else
9810 class_to_search = current_class;
e04a16fb
AG
9811 lc = 1;
9812 }
9813 /* This is a regular search in the local class, unless an
9814 alternate class is specified. */
9815 else
9816 {
9817 class_to_search = (where ? where : current_class);
9818 lc = 0;
9819 }
c2952b01 9820
e04a16fb
AG
9821 /* NAME is a simple identifier or comes from a primary. Search
9822 in the class whose declaration contain the method being
9823 invoked. */
c877974e 9824 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9825
c2952b01 9826 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9827 /* Don't continue if no method were found, as the next statement
9828 can't be executed then. */
b9f7e36c
APB
9829 if (!list)
9830 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9831
9832 /* Check for static reference if non static methods */
9833 if (check_for_static_method_reference (wfl, patch, list,
9834 class_to_search, primary))
b9f7e36c 9835 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9836
165f37bc
APB
9837 /* Check for inner classes creation from illegal contexts */
9838 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9839 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9840 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9841 {
9842 parse_error_context
9843 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9844 lang_printable_name (class_to_search, 0),
9845 (!current_this ? "" :
9846 "; an explicit one must be provided when creating this inner class"));
9847 PATCH_METHOD_RETURN_ERROR ();
9848 }
9849
22eed1e6
APB
9850 /* Non static methods are called with the current object extra
9851 argument. If patch a `new TYPE()', the argument is the value
9852 returned by the object allocator. If method is resolved as a
9853 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9854 args = nreverse (args);
bccaf73a 9855 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9856 {
9857 this_arg = primary ? primary : current_this;
9858
9859 /* If we're using an access method, things are different.
9860 There are two familly of cases:
9861
9862 1) We're not generating bytecodes:
9863
9864 - LIST is non static. It's invocation is transformed from
9865 x(a1,...,an) into this$<n>.x(a1,....an).
9866 - LIST is static. It's invocation is transformed from
9867 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9868
9869 2) We're generating bytecodes:
9870
9871 - LIST is non static. It's invocation is transformed from
9872 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9873 - LIST is static. It's invocation is transformed from
9874 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9875
9876 Of course, this$<n> can be abitrary complex, ranging from
9877 this$0 (the immediate outer context) to
9878 access$0(access$0(...(this$0))).
9879
9880 maybe_use_access_method returns a non zero value if the
dfb99c83 9881 this_arg has to be moved into the (then generated) stub
4dbf4496 9882 argument list. In the meantime, the selected function
dfb99c83 9883 might have be replaced by a generated stub. */
c2952b01
APB
9884 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9885 args = tree_cons (NULL_TREE, this_arg, args);
9886 }
e04a16fb 9887 }
b67d701b 9888
e04a16fb
AG
9889 /* Merge point of all resolution schemes. If we have nothing, this
9890 is an error, already signaled */
b9f7e36c
APB
9891 if (!list)
9892 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9893
e04a16fb
AG
9894 /* Check accessibility, position the is_static flag, build and
9895 return the call */
9bbc7d9f 9896 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9897 {
c2e3db92 9898 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9899 parse_error_context
9900 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9901 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9902 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9903 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9904 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9905 free (fct_name);
b9f7e36c 9906 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9907 }
5e942c50 9908 check_deprecation (wfl, list);
22eed1e6 9909
c2952b01
APB
9910 /* If invoking a innerclass constructor, there are hidden parameters
9911 to pass */
9912 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9913 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9914 {
9915 /* And make sure we add the accessed local variables to be saved
9916 in field aliases. */
9917 args = build_alias_initializer_parameter_list
9918 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9919
da632f2c 9920 /* Secretly pass the current_this/primary as a second argument */
165f37bc
APB
9921 if (primary || current_this)
9922 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9923 else
9924 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9925 }
9926
152de068
APB
9927 /* This handles the situation where a constructor invocation needs
9928 to have an enclosing context passed as a second parameter (the
9929 constructor is one of an inner class. We extract it from the
9930 current function. */
9931 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9932 {
9933 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9934 tree extra_arg;
9935
9936 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9937 {
9938 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9939 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9940 }
9941 else
9942 {
9943 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9944 extra_arg =
9945 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9946 extra_arg = java_complete_tree (extra_arg);
9947 }
9948 args = tree_cons (NULL_TREE, extra_arg, args);
9949 }
9950
22eed1e6 9951 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
9952 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9953 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9954
c3f2a476
APB
9955 /* In the context of an explicit constructor invocation, we can't
9956 invoke any method relying on `this'. Exceptions are: we're
9957 invoking a static function, primary exists and is not the current
9958 this, we're creating a new object. */
22eed1e6 9959 if (ctxp->explicit_constructor_p
c3f2a476
APB
9960 && !is_static_flag
9961 && (!primary || primary == current_this)
9962 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9963 {
781b0558 9964 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9965 PATCH_METHOD_RETURN_ERROR ();
9966 }
e04a16fb 9967 java_parser_context_restore_global ();
22eed1e6
APB
9968 if (is_static)
9969 *is_static = is_static_flag;
b9f7e36c
APB
9970 /* Sometimes, we want the decl of the selected method. Such as for
9971 EH checking */
9972 if (ret_decl)
9973 *ret_decl = list;
89e09b9a
PB
9974 patch = patch_invoke (patch, list, args);
9975 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9976 {
c2952b01
APB
9977 tree finit_parms, finit_call;
9978
c00f0fb2 9979 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
9980 finit_parms = build_alias_initializer_parameter_list
9981 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 9982
c2952b01
APB
9983 finit_call =
9984 build_method_invocation (build_wfl_node (finit_identifier_node),
9985 finit_parms);
9986
9987 /* Generate the code used to initialize fields declared with an
9988 initialization statement and build a compound statement along
9989 with the super constructor invocation. */
89e09b9a
PB
9990 patch = build (COMPOUND_EXPR, void_type_node, patch,
9991 java_complete_tree (finit_call));
9992 CAN_COMPLETE_NORMALLY (patch) = 1;
9993 }
9994 return patch;
e04a16fb
AG
9995}
9996
9997/* Check that we're not trying to do a static reference to a method in
9998 non static method. Return 1 if it's the case, 0 otherwise. */
9999
10000static int
10001check_for_static_method_reference (wfl, node, method, where, primary)
10002 tree wfl, node, method, where, primary;
10003{
10004 if (METHOD_STATIC (current_function_decl)
10005 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10006 {
c2e3db92 10007 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10008 parse_error_context
10009 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10010 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10011 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10012 free (fct_name);
10013 return 1;
10014 }
10015 return 0;
10016}
10017
c2952b01
APB
10018/* Fix the invocation of *MDECL if necessary in the case of a
10019 invocation from an inner class. *THIS_ARG might be modified
10020 appropriately and an alternative access to *MDECL might be
10021 returned. */
10022
10023static int
10024maybe_use_access_method (is_super_init, mdecl, this_arg)
10025 int is_super_init;
10026 tree *mdecl, *this_arg;
10027{
10028 tree ctx;
10029 tree md = *mdecl, ta = *this_arg;
10030 int to_return = 0;
10031 int non_static_context = !METHOD_STATIC (md);
10032
10033 if (is_super_init
165f37bc
APB
10034 || DECL_CONTEXT (md) == current_class
10035 || !PURE_INNER_CLASS_TYPE_P (current_class)
10036 || DECL_FINIT_P (md))
c2952b01
APB
10037 return 0;
10038
10039 /* If we're calling a method found in an enclosing class, generate
10040 what it takes to retrieve the right this. Don't do that if we're
10041 invoking a static method. */
10042
10043 if (non_static_context)
10044 {
10045 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10046 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10047 {
10048 ta = build_current_thisn (current_class);
10049 ta = build_wfl_node (ta);
10050 }
10051 else
10052 {
10053 tree type = ctx;
10054 while (type)
10055 {
10056 maybe_build_thisn_access_method (type);
4dbf4496 10057 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10058 {
10059 ta = build_access_to_thisn (ctx, type, 0);
10060 break;
10061 }
10062 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10063 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10064 }
10065 }
10066 ta = java_complete_tree (ta);
10067 }
10068
10069 /* We might have to use an access method to get to MD. We can
10070 break the method access rule as far as we're not generating
10071 bytecode */
10072 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10073 {
10074 md = build_outer_method_access_method (md);
10075 to_return = 1;
10076 }
10077
10078 *mdecl = md;
10079 *this_arg = ta;
10080
10081 /* Returnin a non zero value indicates we were doing a non static
10082 method invokation that is now a static invocation. It will have
10083 callee displace `this' to insert it in the regular argument
10084 list. */
10085 return (non_static_context && to_return);
10086}
10087
e04a16fb
AG
10088/* Patch an invoke expression METHOD and ARGS, based on its invocation
10089 mode. */
10090
10091static tree
89e09b9a 10092patch_invoke (patch, method, args)
e04a16fb 10093 tree patch, method, args;
e04a16fb
AG
10094{
10095 tree dtable, func;
0a2138e2 10096 tree original_call, t, ta;
e815887f 10097 tree cond = NULL_TREE;
e04a16fb 10098
5e942c50
APB
10099 /* Last step for args: convert build-in types. If we're dealing with
10100 a new TYPE() type call, the first argument to the constructor
e815887f 10101 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10102 `new' */
10103 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10104 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10105 t = TREE_CHAIN (t);
ac825856
APB
10106 for (ta = args; t != end_params_node && ta;
10107 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10108 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10109 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10110 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10111
10112 /* Resolve unresolved returned type isses */
10113 t = TREE_TYPE (TREE_TYPE (method));
10114 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10115 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10116
e8fc7396 10117 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10118 func = method;
10119 else
e04a16fb 10120 {
15fdcfe9 10121 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10122 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10123 {
10124 case INVOKE_VIRTUAL:
10125 dtable = invoke_build_dtable (0, args);
10126 func = build_invokevirtual (dtable, method);
10127 break;
b9f7e36c 10128
e815887f
TT
10129 case INVOKE_NONVIRTUAL:
10130 /* If the object for the method call is null, we throw an
10131 exception. We don't do this if the object is the current
10132 method's `this'. In other cases we just rely on an
10133 optimization pass to eliminate redundant checks. */
10134 if (TREE_VALUE (args) != current_this)
10135 {
10136 /* We use a SAVE_EXPR here to make sure we only evaluate
10137 the new `self' expression once. */
10138 tree save_arg = save_expr (TREE_VALUE (args));
10139 TREE_VALUE (args) = save_arg;
10140 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10141 null_pointer_node);
10142 }
10143 /* Fall through. */
10144
15fdcfe9
PB
10145 case INVOKE_SUPER:
10146 case INVOKE_STATIC:
10147 func = build_known_method_ref (method, TREE_TYPE (method),
10148 DECL_CONTEXT (method),
10149 signature, args);
10150 break;
e04a16fb 10151
15fdcfe9
PB
10152 case INVOKE_INTERFACE:
10153 dtable = invoke_build_dtable (1, args);
173f556c 10154 func = build_invokeinterface (dtable, method);
15fdcfe9 10155 break;
5e942c50 10156
15fdcfe9 10157 default:
89e09b9a 10158 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10159 }
10160
10161 /* Ensure self_type is initialized, (invokestatic). FIXME */
10162 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10163 }
10164
e04a16fb
AG
10165 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10166 TREE_OPERAND (patch, 0) = func;
10167 TREE_OPERAND (patch, 1) = args;
10168 original_call = patch;
10169
e815887f 10170 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10171 returned value is the first argument to the constructor. We build
10172 a COMPOUND_EXPR and use saved expression so that the overall NEW
10173 expression value is a pointer to a newly created and initialized
10174 class. */
10175 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10176 {
10177 tree class = DECL_CONTEXT (method);
10178 tree c1, saved_new, size, new;
e8fc7396 10179 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10180 {
10181 TREE_TYPE (patch) = build_pointer_type (class);
10182 return patch;
10183 }
e04a16fb
AG
10184 if (!TYPE_SIZE (class))
10185 safe_layout_class (class);
10186 size = size_in_bytes (class);
10187 new = build (CALL_EXPR, promote_type (class),
10188 build_address_of (alloc_object_node),
10189 tree_cons (NULL_TREE, build_class_ref (class),
10190 build_tree_list (NULL_TREE,
10191 size_in_bytes (class))),
10192 NULL_TREE);
10193 saved_new = save_expr (new);
10194 c1 = build_tree_list (NULL_TREE, saved_new);
10195 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10196 TREE_OPERAND (original_call, 1) = c1;
10197 TREE_SET_CODE (original_call, CALL_EXPR);
10198 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10199 }
e815887f
TT
10200
10201 /* If COND is set, then we are building a check to see if the object
10202 is NULL. */
10203 if (cond != NULL_TREE)
10204 {
10205 /* We have to make the `then' branch a compound expression to
10206 make the types turn out right. This seems bizarre. */
10207 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10208 build (COMPOUND_EXPR, TREE_TYPE (patch),
10209 build (CALL_EXPR, void_type_node,
10210 build_address_of (soft_nullpointer_node),
10211 NULL_TREE, NULL_TREE),
10212 (FLOAT_TYPE_P (TREE_TYPE (patch))
10213 ? build_real (TREE_TYPE (patch), dconst0)
10214 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10215 integer_zero_node))),
10216 patch);
10217 TREE_SIDE_EFFECTS (patch) = 1;
10218 }
10219
e04a16fb
AG
10220 return patch;
10221}
10222
10223static int
10224invocation_mode (method, super)
10225 tree method;
10226 int super;
10227{
10228 int access = get_access_flags_from_decl (method);
10229
22eed1e6
APB
10230 if (super)
10231 return INVOKE_SUPER;
10232
e815887f 10233 if (access & ACC_STATIC)
e04a16fb
AG
10234 return INVOKE_STATIC;
10235
e815887f
TT
10236 /* We have to look for a constructor before we handle nonvirtual
10237 calls; otherwise the constructor will look nonvirtual. */
10238 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10239 return INVOKE_STATIC;
e815887f
TT
10240
10241 if (access & ACC_FINAL || access & ACC_PRIVATE)
10242 return INVOKE_NONVIRTUAL;
10243
10244 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10245 return INVOKE_NONVIRTUAL;
10246
e04a16fb
AG
10247 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10248 return INVOKE_INTERFACE;
22eed1e6 10249
e04a16fb
AG
10250 return INVOKE_VIRTUAL;
10251}
10252
b67d701b
PB
10253/* Retrieve a refined list of matching methods. It covers the step
10254 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10255
10256static tree
10257lookup_method_invoke (lc, cl, class, name, arg_list)
10258 int lc;
10259 tree cl;
10260 tree class, name, arg_list;
10261{
de4c7b02 10262 tree atl = end_params_node; /* Arg Type List */
c877974e 10263 tree method, signature, list, node;
49f48c71 10264 const char *candidates; /* Used for error report */
b5b8a0e7 10265 char *dup;
e04a16fb 10266
5e942c50 10267 /* Fix the arguments */
e04a16fb
AG
10268 for (node = arg_list; node; node = TREE_CHAIN (node))
10269 {
e3884b71 10270 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10271 /* Non primitive type may have to be resolved */
e3884b71 10272 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10273 resolve_and_layout (current_arg, NULL_TREE);
10274 /* And promoted */
b67d701b 10275 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10276 current_arg = promote_type (current_arg);
5e942c50 10277 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10278 }
e04a16fb 10279
c2952b01
APB
10280 /* Presto. If we're dealing with an anonymous class and a
10281 constructor call, generate the right constructor now, since we
10282 know the arguments' types. */
10283
10284 if (lc && ANONYMOUS_CLASS_P (class))
10285 craft_constructor (TYPE_NAME (class), atl);
10286
5e942c50
APB
10287 /* Find all candidates and then refine the list, searching for the
10288 most specific method. */
10289 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10290 list = find_most_specific_methods_list (list);
b67d701b
PB
10291 if (list && !TREE_CHAIN (list))
10292 return TREE_VALUE (list);
e04a16fb 10293
b67d701b
PB
10294 /* Issue an error. List candidates if any. Candidates are listed
10295 only if accessible (non accessible methods may end-up here for
10296 the sake of a better error report). */
10297 candidates = NULL;
10298 if (list)
e04a16fb 10299 {
e04a16fb 10300 tree current;
b67d701b 10301 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10302 for (current = list; current; current = TREE_CHAIN (current))
10303 {
b67d701b
PB
10304 tree cm = TREE_VALUE (current);
10305 char string [4096];
10306 if (!cm || not_accessible_p (class, cm, 0))
10307 continue;
b67d701b 10308 sprintf
22eed1e6
APB
10309 (string, " `%s' in `%s'%s",
10310 get_printable_method_name (cm),
b67d701b
PB
10311 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10312 (TREE_CHAIN (current) ? "\n" : ""));
10313 obstack_grow (&temporary_obstack, string, strlen (string));
10314 }
10315 obstack_1grow (&temporary_obstack, '\0');
10316 candidates = obstack_finish (&temporary_obstack);
10317 }
10318 /* Issue the error message */
c877974e
APB
10319 method = make_node (FUNCTION_TYPE);
10320 TYPE_ARG_TYPES (method) = atl;
b67d701b 10321 signature = build_java_argument_signature (method);
c63b98cd 10322 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10323 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10324 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10325 (lc ? dup : IDENTIFIER_POINTER (name)),
10326 IDENTIFIER_POINTER (signature), dup,
b67d701b 10327 (candidates ? candidates : ""));
b5b8a0e7 10328 free (dup);
b67d701b
PB
10329 return NULL_TREE;
10330}
10331
5e942c50
APB
10332/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10333 when we're looking for a constructor. */
b67d701b
PB
10334
10335static tree
5e942c50
APB
10336find_applicable_accessible_methods_list (lc, class, name, arglist)
10337 int lc;
b67d701b
PB
10338 tree class, name, arglist;
10339{
ad69b5b6
BM
10340 static struct hash_table t, *searched_classes = NULL;
10341 static int search_not_done = 0;
b67d701b
PB
10342 tree list = NULL_TREE, all_list = NULL_TREE;
10343
ad69b5b6
BM
10344 /* Check the hash table to determine if this class has been searched
10345 already. */
10346 if (searched_classes)
10347 {
10348 if (hash_lookup (searched_classes,
10349 (const hash_table_key) class, FALSE, NULL))
10350 return NULL;
10351 }
10352 else
10353 {
10354 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10355 java_hash_compare_tree_node);
10356 searched_classes = &t;
10357 }
10358
10359 search_not_done++;
10360 hash_lookup (searched_classes,
0c2b8145 10361 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10362
c2952b01
APB
10363 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10364 {
10365 load_class (class, 1);
10366 safe_layout_class (class);
10367 }
10368
1982388a 10369 /* Search interfaces */
9a7ab4b3
APB
10370 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10371 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10372 {
1982388a
APB
10373 int i, n;
10374 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10375 search_applicable_methods_list (lc, TYPE_METHODS (class),
10376 name, arglist, &list, &all_list);
1982388a 10377 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10378 for (i = 1; i < n; i++)
b67d701b 10379 {
de0b553f
APB
10380 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10381 tree rlist;
10382
de0b553f
APB
10383 rlist = find_applicable_accessible_methods_list (lc, t, name,
10384 arglist);
165f37bc 10385 list = chainon (rlist, list);
e04a16fb 10386 }
e04a16fb 10387 }
1982388a
APB
10388 /* Search classes */
10389 else
c2952b01 10390 {
165f37bc
APB
10391 tree sc = class;
10392 int seen_inner_class = 0;
c2952b01
APB
10393 search_applicable_methods_list (lc, TYPE_METHODS (class),
10394 name, arglist, &list, &all_list);
10395
165f37bc
APB
10396 /* We must search all interfaces of this class */
10397 if (!lc)
10398 {
10399 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10400 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10401 for (i = 1; i < n; i++)
10402 {
10403 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10404 if (t != object_type_node)
30a3caef
ZW
10405 {
10406 tree rlist
10407 = find_applicable_accessible_methods_list (lc, t,
10408 name, arglist);
10409 list = chainon (rlist, list);
10410 }
165f37bc 10411 }
165f37bc
APB
10412 }
10413
c2952b01
APB
10414 /* Search enclosing context of inner classes before looking
10415 ancestors up. */
10416 while (!lc && INNER_CLASS_TYPE_P (class))
10417 {
165f37bc
APB
10418 tree rlist;
10419 seen_inner_class = 1;
c2952b01 10420 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10421 rlist = find_applicable_accessible_methods_list (lc, class,
10422 name, arglist);
10423 list = chainon (rlist, list);
c2952b01 10424 }
165f37bc
APB
10425
10426 if (!lc && seen_inner_class
10427 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10428 class = CLASSTYPE_SUPER (sc);
10429 else
10430 class = sc;
10431
ad69b5b6
BM
10432 /* Search superclass */
10433 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10434 {
10435 tree rlist;
10436 class = CLASSTYPE_SUPER (class);
10437 rlist = find_applicable_accessible_methods_list (lc, class,
10438 name, arglist);
10439 list = chainon (rlist, list);
10440 }
10441 }
10442
10443 search_not_done--;
10444
10445 /* We're done. Reset the searched classes list and finally search
10446 java.lang.Object if it wasn't searched already. */
10447 if (!search_not_done)
10448 {
10449 if (!lc
10450 && TYPE_METHODS (object_type_node)
10451 && !hash_lookup (searched_classes,
10452 (const hash_table_key) object_type_node,
10453 FALSE, NULL))
10454 {
10455 search_applicable_methods_list (lc,
10456 TYPE_METHODS (object_type_node),
10457 name, arglist, &list, &all_list);
10458 }
10459 hash_table_free (searched_classes);
10460 searched_classes = NULL;
c2952b01 10461 }
1982388a 10462
b67d701b
PB
10463 /* Either return the list obtained or all selected (but
10464 inaccessible) methods for better error report. */
10465 return (!list ? all_list : list);
10466}
e04a16fb 10467
ad69b5b6 10468/* Effectively search for the appropriate method in method */
1982388a
APB
10469
10470static void
c2952b01 10471search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10472 int lc;
10473 tree method, name, arglist;
10474 tree *list, *all_list;
10475{
10476 for (; method; method = TREE_CHAIN (method))
10477 {
10478 /* When dealing with constructor, stop here, otherwise search
10479 other classes */
10480 if (lc && !DECL_CONSTRUCTOR_P (method))
10481 continue;
10482 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10483 || (GET_METHOD_NAME (method) != name)))
10484 continue;
10485
10486 if (argument_types_convertible (method, arglist))
10487 {
10488 /* Retain accessible methods only */
10489 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10490 method, 0))
10491 *list = tree_cons (NULL_TREE, method, *list);
10492 else
10493 /* Also retain all selected method here */
10494 *all_list = tree_cons (NULL_TREE, method, *list);
10495 }
10496 }
ad69b5b6 10497}
1982388a 10498
b67d701b
PB
10499/* 15.11.2.2 Choose the Most Specific Method */
10500
10501static tree
10502find_most_specific_methods_list (list)
10503 tree list;
10504{
10505 int max = 0;
9a7ab4b3 10506 int abstract, candidates;
b67d701b
PB
10507 tree current, new_list = NULL_TREE;
10508 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10509 {
b67d701b
PB
10510 tree method;
10511 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10512
10513 for (method = list; method; method = TREE_CHAIN (method))
10514 {
0c2b8145 10515 tree method_v, current_v;
b67d701b
PB
10516 /* Don't test a method against itself */
10517 if (method == current)
10518 continue;
10519
0c2b8145
APB
10520 method_v = TREE_VALUE (method);
10521 current_v = TREE_VALUE (current);
10522
10523 /* Compare arguments and location where methods where declared */
10524 if (argument_types_convertible (method_v, current_v))
b67d701b 10525 {
0c2b8145
APB
10526 if (valid_method_invocation_conversion_p
10527 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10528 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10529 && enclosing_context_p (DECL_CONTEXT (method_v),
10530 DECL_CONTEXT (current_v))))
10531 {
10532 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10533 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10534 max = (v > max ? v : max);
10535 }
b67d701b
PB
10536 }
10537 }
e04a16fb
AG
10538 }
10539
b67d701b 10540 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10541 for (current = list, abstract = -1, candidates = -1;
10542 current; current = TREE_CHAIN (current))
b67d701b 10543 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10544 {
10545 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10546 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10547 candidates++;
10548 }
b67d701b 10549
165f37bc
APB
10550 /* If we have several and they're all abstract, just pick the
10551 closest one. */
9a7ab4b3
APB
10552 if (candidates > 0 && (candidates == abstract))
10553 {
10554 new_list = nreverse (new_list);
10555 TREE_CHAIN (new_list) = NULL_TREE;
10556 }
165f37bc 10557
9a7ab4b3
APB
10558 /* We have several, we couldn't find a most specific, all but one are
10559 abstract, we pick the only non abstract one. */
10560 if (candidates > 0 && !max && (candidates == abstract+1))
165f37bc 10561 {
9a7ab4b3
APB
10562 for (current = new_list; current; current = TREE_CHAIN (current))
10563 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10564 {
10565 TREE_CHAIN (current) = NULL_TREE;
10566 new_list = current;
10567 }
165f37bc
APB
10568 }
10569
b67d701b
PB
10570 /* If we can't find one, lower expectations and try to gather multiple
10571 maximally specific methods */
165f37bc 10572 while (!new_list && max)
b67d701b
PB
10573 {
10574 while (--max > 0)
10575 {
10576 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10577 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10578 }
b67d701b
PB
10579 }
10580
10581 return new_list;
e04a16fb
AG
10582}
10583
b67d701b
PB
10584/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10585 converted by method invocation conversion (5.3) to the type of the
10586 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10587 to change less often than M1. */
e04a16fb 10588
b67d701b
PB
10589static int
10590argument_types_convertible (m1, m2_or_arglist)
10591 tree m1, m2_or_arglist;
e04a16fb 10592{
b67d701b
PB
10593 static tree m2_arg_value = NULL_TREE;
10594 static tree m2_arg_cache = NULL_TREE;
19e223db 10595 static int initialized_p;
e04a16fb 10596
b67d701b 10597 register tree m1_arg, m2_arg;
e04a16fb 10598
19e223db
MM
10599 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10600 collector. */
10601 if (!initialized_p)
10602 {
10603 ggc_add_tree_root (&m2_arg_value, 1);
10604 ggc_add_tree_root (&m2_arg_cache, 1);
10605 initialized_p = 1;
10606 }
10607
c2952b01 10608 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10609
b67d701b
PB
10610 if (m2_arg_value == m2_or_arglist)
10611 m2_arg = m2_arg_cache;
10612 else
10613 {
10614 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10615 argument types */
10616 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10617 {
10618 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10619 if (!METHOD_STATIC (m2_or_arglist))
10620 m2_arg = TREE_CHAIN (m2_arg);
10621 }
10622 else
10623 m2_arg = m2_or_arglist;
e04a16fb 10624
b67d701b
PB
10625 m2_arg_value = m2_or_arglist;
10626 m2_arg_cache = m2_arg;
10627 }
e04a16fb 10628
de4c7b02 10629 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10630 {
c877974e 10631 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10632 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10633 TREE_VALUE (m2_arg)))
10634 break;
10635 m1_arg = TREE_CHAIN (m1_arg);
10636 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10637 }
de4c7b02 10638 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10639}
10640
10641/* Qualification routines */
10642
10643static void
10644qualify_ambiguous_name (id)
10645 tree id;
10646{
cd531a2e
KG
10647 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10648 saved_current_class;
d8fccff5 10649 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10650 int code;
e04a16fb
AG
10651
10652 /* We first qualify the first element, then derive qualification of
10653 others based on the first one. If the first element is qualified
10654 by a resolution (field or type), this resolution is stored in the
10655 QUAL_RESOLUTION of the qual element being examined. We need to
10656 save the current_class since the use of SUPER might change the
10657 its value. */
10658 saved_current_class = current_class;
10659 qual = EXPR_WFL_QUALIFICATION (id);
10660 do {
10661
10662 /* Simple qualified expression feature a qual_wfl that is a
10663 WFL. Expression derived from a primary feature more complicated
10664 things like a CALL_EXPR. Expression from primary need to be
10665 worked out to extract the part on which the qualification will
10666 take place. */
10667 qual_wfl = QUAL_WFL (qual);
10668 switch (TREE_CODE (qual_wfl))
10669 {
10670 case CALL_EXPR:
10671 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10672 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10673 {
10674 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10675 qual_wfl = QUAL_WFL (qual);
10676 }
10677 break;
d8fccff5 10678 case NEW_ARRAY_EXPR:
c2952b01 10679 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10680 qual = TREE_CHAIN (qual);
1a6d4fb7 10681 again = new_array_found = 1;
d8fccff5 10682 continue;
e04a16fb 10683 case CONVERT_EXPR:
f2760b27
APB
10684 break;
10685 case NEW_CLASS_EXPR:
e04a16fb
AG
10686 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10687 break;
c583dd46
APB
10688 case ARRAY_REF:
10689 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10690 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10691 break;
8576f094
APB
10692 case STRING_CST:
10693 qual = TREE_CHAIN (qual);
10694 qual_wfl = QUAL_WFL (qual);
10695 break;
165f37bc
APB
10696 case CLASS_LITERAL:
10697 qual = TREE_CHAIN (qual);
10698 qual_wfl = QUAL_WFL (qual);
10699 break;
0a2138e2
APB
10700 default:
10701 /* Fix for -Wall. Just break doing nothing */
10702 break;
e04a16fb 10703 }
8576f094 10704
e04a16fb
AG
10705 ptr_type = current_class;
10706 again = 0;
8576f094
APB
10707 code = TREE_CODE (qual_wfl);
10708
10709 /* Pos evaluation: non WFL leading expression nodes */
10710 if (code == CONVERT_EXPR
10711 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10712 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10713
cd7c5840
APB
10714 else if (code == INTEGER_CST)
10715 name = qual_wfl;
10716
ac22f9cb 10717 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10718 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10719 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10720
c2952b01
APB
10721 else if (code == TREE_LIST)
10722 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10723
37feda7d
APB
10724 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10725 || code == PLUS_EXPR)
8576f094
APB
10726 {
10727 qual = TREE_CHAIN (qual);
10728 qual_wfl = QUAL_WFL (qual);
10729 again = 1;
10730 }
10731 else
f441f671
APB
10732 {
10733 name = EXPR_WFL_NODE (qual_wfl);
10734 if (!name)
10735 {
10736 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10737 again = 1;
10738 }
10739 }
10740
e04a16fb
AG
10741 /* If we have a THIS (from a primary), we set the context accordingly */
10742 if (name == this_identifier_node)
10743 {
10744 qual = TREE_CHAIN (qual);
10745 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10746 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10747 again = 1;
10748 else
10749 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10750 this_found = 1;
10751 }
10752 /* If we have a SUPER, we set the context accordingly */
10753 if (name == super_identifier_node)
10754 {
10755 current_class = CLASSTYPE_SUPER (ptr_type);
10756 /* Check that there is such a thing as a super class. If not,
10757 return. The error will be caught later on, during the
10758 resolution */
10759 if (!current_class)
10760 {
10761 current_class = saved_current_class;
10762 return;
10763 }
10764 qual = TREE_CHAIN (qual);
10765 /* Do one more interation to set things up */
10766 super_found = again = 1;
10767 }
10768 } while (again);
10769
f2760b27
APB
10770 /* If name appears within the scope of a local variable declaration
10771 or parameter declaration, then it is an expression name. We don't
10772 carry this test out if we're in the context of the use of SUPER
10773 or THIS */
cd7c5840
APB
10774 if (!this_found && !super_found
10775 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10776 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10777 {
10778 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10779 QUAL_RESOLUTION (qual) = decl;
10780 }
10781
10782 /* If within the class/interface NAME was found to be used there
10783 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10784 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10785 address length, it is OK. */
10786 else if ((decl = lookup_field_wrapper (ptr_type, name))
10787 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10788 {
10789 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10790 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10791 }
10792
1a6d4fb7 10793 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10794 - NAME is a class/interface declared within the compilation
10795 unit containing NAME,
10796 - NAME is imported via a single-type-import declaration,
10797 - NAME is declared in an another compilation unit of the package
10798 of the compilation unit containing NAME,
10799 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10800 of the compilation unit containing NAME.
10801 - NAME is actually a STRING_CST. */
cd7c5840
APB
10802 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10803 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10804 {
10805 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10806 QUAL_RESOLUTION (qual) = decl;
10807 }
10808
f2760b27 10809 /* Method call, array references and cast are expression name */
9bbc7d9f 10810 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10811 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10812 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10813 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10814
10815 /* Check here that NAME isn't declared by more than one
10816 type-import-on-demand declaration of the compilation unit
10817 containing NAME. FIXME */
10818
10819 /* Otherwise, NAME is reclassified as a package name */
10820 else
10821 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10822
10823 /* Propagate the qualification accross other components of the
10824 qualified name */
10825 for (qual = TREE_CHAIN (qual); qual;
10826 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10827 {
10828 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10829 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10830 else
10831 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10832 }
10833
10834 /* Store the global qualification for the ambiguous part of ID back
10835 into ID fields */
10836 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10837 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10838 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10839 RESOLVE_TYPE_NAME_P (id) = 1;
10840 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10841 RESOLVE_PACKAGE_NAME_P (id) = 1;
10842
10843 /* Restore the current class */
10844 current_class = saved_current_class;
10845}
10846
10847static int
10848breakdown_qualified (left, right, source)
10849 tree *left, *right, source;
10850{
63ad61ed 10851 char *p, *base;
e04a16fb
AG
10852 int l = IDENTIFIER_LENGTH (source);
10853
63ad61ed
ZW
10854 base = alloca (l + 1);
10855 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10856
e04a16fb 10857 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10858 p = base + l - 1;
e04a16fb
AG
10859 while (*p != '.' && p != base)
10860 p--;
10861
10862 /* We didn't find a '.'. Return an error */
10863 if (p == base)
10864 return 1;
10865
10866 *p = '\0';
10867 if (right)
10868 *right = get_identifier (p+1);
63ad61ed 10869 *left = get_identifier (base);
e04a16fb
AG
10870
10871 return 0;
10872}
10873
e04a16fb 10874/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10875 local variable decls if present.
10876 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10877
10878static tree
10879java_complete_tree (node)
10880 tree node;
5b09b33e
PB
10881{
10882 node = java_complete_lhs (node);
0c2b8145
APB
10883 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10884 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 10885 && !flag_emit_xref)
5b09b33e
PB
10886 {
10887 tree value = DECL_INITIAL (node);
10888 DECL_INITIAL (node) = NULL_TREE;
100f7cd8 10889 push_obstacks (&permanent_obstack, &permanent_obstack);
5b09b33e 10890 value = fold_constant_for_init (value, node);
100f7cd8 10891 pop_obstacks ();
5b09b33e
PB
10892 DECL_INITIAL (node) = value;
10893 if (value != NULL_TREE)
c2952b01
APB
10894 {
10895 /* fold_constant_for_init sometimes widen the original type
10896 of the constant (i.e. byte to int.) It's not desirable,
10897 especially if NODE is a function argument. */
10898 if (TREE_CODE (value) == INTEGER_CST
10899 && TREE_TYPE (node) != TREE_TYPE (value))
10900 return convert (TREE_TYPE (node), value);
10901 else
10902 return value;
10903 }
5b09b33e
PB
10904 }
10905 return node;
10906}
10907
2aa11e97
APB
10908static tree
10909java_stabilize_reference (node)
10910 tree node;
10911{
10912 if (TREE_CODE (node) == COMPOUND_EXPR)
10913 {
10914 tree op0 = TREE_OPERAND (node, 0);
10915 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10916 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10917 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10918 return node;
10919 }
5cbdba64 10920 return stabilize_reference (node);
2aa11e97
APB
10921}
10922
5b09b33e
PB
10923/* Patch tree nodes in a function body. When a BLOCK is found, push
10924 local variable decls if present.
10925 Same as java_complete_tree, but does not resolve static finals to values. */
10926
10927static tree
10928java_complete_lhs (node)
10929 tree node;
e04a16fb 10930{
22eed1e6 10931 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10932 int flag;
e04a16fb
AG
10933
10934 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10935 worked out. */
e04a16fb
AG
10936 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10937 return node;
10938
10939 /* The switch block implements cases processing container nodes
10940 first. Contained nodes are always written back. Leaves come
10941 next and return a value. */
10942 switch (TREE_CODE (node))
10943 {
10944 case BLOCK:
10945
10946 /* 1- Block section.
10947 Set the local values on decl names so we can identify them
10948 faster when they're referenced. At that stage, identifiers
10949 are legal so we don't check for declaration errors. */
10950 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10951 {
10952 DECL_CONTEXT (cn) = current_function_decl;
10953 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 10954 }
15fdcfe9
PB
10955 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10956 CAN_COMPLETE_NORMALLY (node) = 1;
10957 else
e04a16fb 10958 {
15fdcfe9
PB
10959 tree stmt = BLOCK_EXPR_BODY (node);
10960 tree *ptr;
10961 int error_seen = 0;
10962 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10963 {
c877974e
APB
10964 /* Re-order from (((A; B); C); ...; Z) to
10965 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
10966 This makes it easier to scan the statements left-to-right
10967 without using recursion (which might overflow the stack
10968 if the block has many statements. */
10969 for (;;)
10970 {
10971 tree left = TREE_OPERAND (stmt, 0);
10972 if (TREE_CODE (left) != COMPOUND_EXPR)
10973 break;
10974 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10975 TREE_OPERAND (left, 1) = stmt;
10976 stmt = left;
10977 }
10978 BLOCK_EXPR_BODY (node) = stmt;
10979 }
10980
c877974e
APB
10981 /* Now do the actual complete, without deep recursion for
10982 long blocks. */
15fdcfe9 10983 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
10984 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10985 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
10986 {
10987 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10988 tree *next = &TREE_OPERAND (*ptr, 1);
10989 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
10990 if (cur == empty_stmt_node)
10991 {
10992 /* Optimization; makes it easier to detect empty bodies.
10993 Most useful for <clinit> with all-constant initializer. */
10994 *ptr = *next;
10995 continue;
10996 }
15fdcfe9
PB
10997 if (TREE_CODE (cur) == ERROR_MARK)
10998 error_seen++;
10999 else if (! CAN_COMPLETE_NORMALLY (cur))
11000 {
11001 wfl_op2 = *next;
11002 for (;;)
11003 {
11004 if (TREE_CODE (wfl_op2) == BLOCK)
11005 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11006 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11007 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11008 else
11009 break;
11010 }
11011 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11012 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11013 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11014 }
11015 ptr = next;
11016 }
11017 *ptr = java_complete_tree (*ptr);
11018
11019 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11020 return error_mark_node;
15fdcfe9 11021 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11022 }
11023 /* Turn local bindings to null */
11024 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11025 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11026
11027 TREE_TYPE (node) = void_type_node;
11028 break;
11029
11030 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11031
b9f7e36c
APB
11032 case THROW_EXPR:
11033 wfl_op1 = TREE_OPERAND (node, 0);
11034 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11035 /* 14.19 A throw statement cannot complete normally. */
11036 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11037 return patch_throw_statement (node, wfl_op1);
11038
11039 case SYNCHRONIZED_EXPR:
11040 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11041 return patch_synchronized_statement (node, wfl_op1);
11042
b67d701b
PB
11043 case TRY_EXPR:
11044 return patch_try_statement (node);
11045
a7d8d81f
PB
11046 case TRY_FINALLY_EXPR:
11047 COMPLETE_CHECK_OP_0 (node);
11048 COMPLETE_CHECK_OP_1 (node);
11049 CAN_COMPLETE_NORMALLY (node)
11050 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11051 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11052 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11053 return node;
11054
5a005d9e
PB
11055 case CLEANUP_POINT_EXPR:
11056 COMPLETE_CHECK_OP_0 (node);
11057 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11058 CAN_COMPLETE_NORMALLY (node) =
11059 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11060 return node;
11061
11062 case WITH_CLEANUP_EXPR:
11063 COMPLETE_CHECK_OP_0 (node);
11064 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11065 CAN_COMPLETE_NORMALLY (node) =
11066 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11067 TREE_TYPE (node) = void_type_node;
11068 return node;
11069
e04a16fb
AG
11070 case LABELED_BLOCK_EXPR:
11071 PUSH_LABELED_BLOCK (node);
11072 if (LABELED_BLOCK_BODY (node))
11073 COMPLETE_CHECK_OP_1 (node);
11074 TREE_TYPE (node) = void_type_node;
11075 POP_LABELED_BLOCK ();
1fb89a4d
APB
11076
11077 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11078 {
11079 LABELED_BLOCK_BODY (node) = NULL_TREE;
11080 CAN_COMPLETE_NORMALLY (node) = 1;
11081 }
1fb89a4d 11082 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11083 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11084 return node;
11085
11086 case EXIT_BLOCK_EXPR:
11087 /* We don't complete operand 1, because it's the return value of
11088 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11089 return patch_bc_statement (node);
11090
15fdcfe9
PB
11091 case CASE_EXPR:
11092 cn = java_complete_tree (TREE_OPERAND (node, 0));
11093 if (cn == error_mark_node)
11094 return cn;
11095
8576f094
APB
11096 /* First, the case expression must be constant. Values of final
11097 fields are accepted. */
15fdcfe9 11098 cn = fold (cn);
8576f094
APB
11099 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11100 && JDECL_P (TREE_OPERAND (cn, 1))
11101 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11102 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8
APB
11103 {
11104 push_obstacks (&permanent_obstack, &permanent_obstack);
11105 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11106 TREE_OPERAND (cn, 1));
11107 pop_obstacks ();
11108 }
15fdcfe9 11109
ce6e9147 11110 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11111 {
11112 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11113 parse_error_context (node, "Constant expression required");
11114 return error_mark_node;
11115 }
11116
11117 nn = ctxp->current_loop;
11118
11119 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11120 if (!try_builtin_assignconv (NULL_TREE,
11121 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11122 {
11123 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11124 parse_error_context
11125 (wfl_operator,
11126 "Incompatible type for case. Can't convert `%s' to `int'",
11127 lang_printable_name (TREE_TYPE (cn), 0));
11128 return error_mark_node;
11129 }
11130
11131 cn = fold (convert (int_type_node, cn));
11132
11133 /* Multiple instance of a case label bearing the same
11134 value is checked during code generation. The case
11135 expression is allright so far. */
34d4df06
APB
11136 if (TREE_CODE (cn) == VAR_DECL)
11137 cn = DECL_INITIAL (cn);
15fdcfe9 11138 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11139 TREE_TYPE (node) = void_type_node;
15fdcfe9 11140 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11141 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11142 break;
11143
11144 case DEFAULT_EXPR:
11145 nn = ctxp->current_loop;
11146 /* Only one default label is allowed per switch statement */
11147 if (SWITCH_HAS_DEFAULT (nn))
11148 {
11149 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11150 parse_error_context (wfl_operator,
11151 "Duplicate case label: `default'");
11152 return error_mark_node;
11153 }
11154 else
11155 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11156 TREE_TYPE (node) = void_type_node;
10100cc7 11157 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11158 CAN_COMPLETE_NORMALLY (node) = 1;
11159 break;
11160
b67d701b 11161 case SWITCH_EXPR:
e04a16fb
AG
11162 case LOOP_EXPR:
11163 PUSH_LOOP (node);
11164 /* Check whether the loop was enclosed in a labeled
11165 statement. If not, create one, insert the loop in it and
11166 return the node */
11167 nn = patch_loop_statement (node);
b67d701b 11168
e04a16fb 11169 /* Anyways, walk the body of the loop */
b67d701b
PB
11170 if (TREE_CODE (node) == LOOP_EXPR)
11171 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11172 /* Switch statement: walk the switch expression and the cases */
11173 else
11174 node = patch_switch_statement (node);
11175
e7c7bcef 11176 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11177 nn = error_mark_node;
11178 else
15fdcfe9 11179 {
b635eb2f
PB
11180 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11181 /* If we returned something different, that's because we
11182 inserted a label. Pop the label too. */
11183 if (nn != node)
11184 {
11185 if (CAN_COMPLETE_NORMALLY (node))
11186 CAN_COMPLETE_NORMALLY (nn) = 1;
11187 POP_LABELED_BLOCK ();
11188 }
15fdcfe9 11189 }
e04a16fb
AG
11190 POP_LOOP ();
11191 return nn;
11192
11193 case EXIT_EXPR:
11194 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11195 return patch_exit_expr (node);
11196
11197 case COND_EXPR:
11198 /* Condition */
11199 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11200 if (TREE_OPERAND (node, 0) == error_mark_node)
11201 return error_mark_node;
11202 /* then-else branches */
11203 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11204 if (TREE_OPERAND (node, 1) == error_mark_node)
11205 return error_mark_node;
11206 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11207 if (TREE_OPERAND (node, 2) == error_mark_node)
11208 return error_mark_node;
11209 return patch_if_else_statement (node);
11210 break;
11211
22eed1e6
APB
11212 case CONDITIONAL_EXPR:
11213 /* Condition */
11214 wfl_op1 = TREE_OPERAND (node, 0);
11215 COMPLETE_CHECK_OP_0 (node);
11216 wfl_op2 = TREE_OPERAND (node, 1);
11217 COMPLETE_CHECK_OP_1 (node);
11218 wfl_op3 = TREE_OPERAND (node, 2);
11219 COMPLETE_CHECK_OP_2 (node);
11220 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11221
e04a16fb
AG
11222 /* 3- Expression section */
11223 case COMPOUND_EXPR:
15fdcfe9 11224 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11225 TREE_OPERAND (node, 0) = nn =
11226 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11227 if (wfl_op2 == empty_stmt_node)
11228 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11229 else
15fdcfe9 11230 {
dc0b3eff 11231 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11232 {
dc0b3eff
PB
11233 /* An unreachable condition in a do-while statement
11234 is *not* (technically) an unreachable statement. */
11235 nn = wfl_op2;
11236 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11237 nn = EXPR_WFL_NODE (nn);
11238 if (TREE_CODE (nn) != EXIT_EXPR)
11239 {
11240 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11241 parse_error_context (wfl_operator, "Unreachable statement");
11242 }
bccaf73a 11243 }
dc0b3eff
PB
11244 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11245 if (TREE_OPERAND (node, 1) == error_mark_node)
11246 return error_mark_node;
11247 CAN_COMPLETE_NORMALLY (node)
11248 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11249 }
e04a16fb
AG
11250 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11251 break;
11252
11253 case RETURN_EXPR:
15fdcfe9 11254 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11255 return patch_return (node);
11256
11257 case EXPR_WITH_FILE_LOCATION:
11258 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11259 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11260 {
5423609c 11261 tree wfl = node;
15fdcfe9 11262 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11263 if (node == error_mark_node)
11264 return node;
5423609c
APB
11265 /* Keep line number information somewhere were it doesn't
11266 disrupt the completion process. */
2c56429a 11267 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11268 {
11269 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11270 TREE_OPERAND (node, 1) = wfl;
11271 }
15fdcfe9
PB
11272 CAN_COMPLETE_NORMALLY (node) = 1;
11273 }
e04a16fb
AG
11274 else
11275 {
5b09b33e
PB
11276 tree body;
11277 int save_lineno = lineno;
11278 lineno = EXPR_WFL_LINENO (node);
11279 body = java_complete_tree (EXPR_WFL_NODE (node));
11280 lineno = save_lineno;
15fdcfe9 11281 EXPR_WFL_NODE (node) = body;
dc0b3eff 11282 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11283 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11284 if (body == empty_stmt_node)
11285 {
11286 /* Optimization; makes it easier to detect empty bodies. */
11287 return body;
11288 }
dc0b3eff 11289 if (body == error_mark_node)
e04a16fb
AG
11290 {
11291 /* Its important for the evaluation of assignment that
11292 this mark on the TREE_TYPE is propagated. */
11293 TREE_TYPE (node) = error_mark_node;
11294 return error_mark_node;
11295 }
11296 else
11297 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11298
e04a16fb
AG
11299 }
11300 break;
11301
b67d701b 11302 case NEW_ARRAY_EXPR:
e04a16fb
AG
11303 /* Patch all the dimensions */
11304 flag = 0;
11305 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11306 {
11307 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11308 tree dim = convert (int_type_node,
11309 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11310 if (dim == error_mark_node)
11311 {
11312 flag = 1;
11313 continue;
11314 }
11315 else
11316 {
b9f7e36c 11317 TREE_VALUE (cn) = dim;
e04a16fb
AG
11318 /* Setup the location of the current dimension, for
11319 later error report. */
11320 TREE_PURPOSE (cn) =
11321 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11322 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11323 }
11324 }
11325 /* They complete the array creation expression, if no errors
11326 were found. */
15fdcfe9 11327 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11328 return (flag ? error_mark_node
11329 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11330
c2952b01
APB
11331 case NEW_ANONYMOUS_ARRAY_EXPR:
11332 /* Create the array type if necessary. */
11333 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11334 {
11335 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11336 if (!(type = resolve_type_during_patch (type)))
11337 return error_mark_node;
11338 type = build_array_from_name (type, NULL_TREE,
11339 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11340 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11341 }
11342 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11343 ANONYMOUS_ARRAY_INITIALIZER (node));
11344 if (node == error_mark_node)
11345 return error_mark_node;
11346 CAN_COMPLETE_NORMALLY (node) = 1;
11347 return node;
11348
b67d701b 11349 case NEW_CLASS_EXPR:
e04a16fb 11350 case CALL_EXPR:
b67d701b 11351 /* Complete function's argument(s) first */
e04a16fb
AG
11352 if (complete_function_arguments (node))
11353 return error_mark_node;
11354 else
b9f7e36c 11355 {
22eed1e6
APB
11356 tree decl, wfl = TREE_OPERAND (node, 0);
11357 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11358
c877974e 11359 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11360 NULL_TREE, 0, &decl);
c877974e
APB
11361 if (node == error_mark_node)
11362 return error_mark_node;
11363
11364 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11365 /* If we call this(...), register signature and positions */
11366 if (in_this)
11367 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11368 tree_cons (wfl, decl,
11369 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11370 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11371 return force_evaluation_order (node);
b9f7e36c 11372 }
e04a16fb
AG
11373
11374 case MODIFY_EXPR:
11375 /* Save potential wfls */
11376 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11377 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11378
cd9643f7
PB
11379 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11380 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11381 && DECL_INITIAL (nn) != NULL_TREE)
11382 {
100f7cd8
APB
11383 tree value;
11384
11385 push_obstacks (&permanent_obstack, &permanent_obstack);
11386 value = fold_constant_for_init (nn, nn);
11387 pop_obstacks ();
c2952b01 11388
cd9643f7
PB
11389 if (value != NULL_TREE)
11390 {
11391 tree type = TREE_TYPE (value);
c2952b01
APB
11392 if (JPRIMITIVE_TYPE_P (type) ||
11393 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11394 return empty_stmt_node;
11395 }
629d4b4d
APB
11396 if (! flag_emit_class_files)
11397 DECL_INITIAL (nn) = NULL_TREE;
cd9643f7 11398 }
e04a16fb 11399 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11400
e04a16fb
AG
11401 if (TREE_OPERAND (node, 0) == error_mark_node)
11402 return error_mark_node;
11403
5cbdba64
APB
11404 flag = COMPOUND_ASSIGN_P (wfl_op2);
11405 if (flag)
e04a16fb 11406 {
c2952b01
APB
11407 /* This might break when accessing outer field from inner
11408 class. TESTME, FIXME */
2aa11e97 11409 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11410
11411 /* Hand stablize the lhs on both places */
e04a16fb 11412 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11413 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11414 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11415
5cbdba64 11416 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11417 /* Now complete the RHS. We write it back later on. */
11418 nn = java_complete_tree (TREE_OPERAND (node, 1));
11419
642f15d1
APB
11420 if ((cn = patch_string (nn)))
11421 nn = cn;
11422
2aa11e97
APB
11423 /* The last part of the rewrite for E1 op= E2 is to have
11424 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11425 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11426 TREE_TYPE (lvalue), nn));
5cbdba64
APB
11427
11428 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11429 }
11430
f8976021 11431 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11432 function to complete this RHS. Note that a NEW_ARRAY_INIT
11433 might have been already fully expanded if created as a result
11434 of processing an anonymous array initializer. We avoid doing
11435 the operation twice by testing whether the node already bears
11436 a type. */
11437 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11438 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11439 TREE_OPERAND (node, 1));
2aa11e97 11440 /* Otherwise we simply complete the RHS */
f8976021
APB
11441 else
11442 nn = java_complete_tree (TREE_OPERAND (node, 1));
11443
e04a16fb 11444 if (nn == error_mark_node)
c0d87ff6 11445 return error_mark_node;
2aa11e97
APB
11446
11447 /* Write back the RHS as we evaluated it. */
e04a16fb 11448 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11449
11450 /* In case we're handling = with a String as a RHS, we need to
11451 produce a String out of the RHS (it might still be a
11452 STRING_CST or a StringBuffer at this stage */
11453 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11454 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11455
11456 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11457 TREE_OPERAND (node, 1))))
11458 {
11459 /* We return error_mark_node if outer_field_access_fix
11460 detects we write into a final. */
11461 if (nn == error_mark_node)
11462 return error_mark_node;
11463 node = nn;
11464 }
11465 else
11466 {
11467 node = patch_assignment (node, wfl_op1, wfl_op2);
11468 /* Reorganize the tree if necessary. */
11469 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11470 || JSTRING_P (TREE_TYPE (node))))
11471 node = java_refold (node);
11472 }
11473
15fdcfe9
PB
11474 CAN_COMPLETE_NORMALLY (node) = 1;
11475 return node;
e04a16fb
AG
11476
11477 case MULT_EXPR:
11478 case PLUS_EXPR:
11479 case MINUS_EXPR:
11480 case LSHIFT_EXPR:
11481 case RSHIFT_EXPR:
11482 case URSHIFT_EXPR:
11483 case BIT_AND_EXPR:
11484 case BIT_XOR_EXPR:
11485 case BIT_IOR_EXPR:
11486 case TRUNC_MOD_EXPR:
c2952b01 11487 case TRUNC_DIV_EXPR:
e04a16fb
AG
11488 case RDIV_EXPR:
11489 case TRUTH_ANDIF_EXPR:
11490 case TRUTH_ORIF_EXPR:
11491 case EQ_EXPR:
11492 case NE_EXPR:
11493 case GT_EXPR:
11494 case GE_EXPR:
11495 case LT_EXPR:
11496 case LE_EXPR:
11497 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11498 knows how to handle those cases. */
11499 wfl_op1 = TREE_OPERAND (node, 0);
11500 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11501
15fdcfe9 11502 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11503 /* Don't complete string nodes if dealing with the PLUS operand. */
11504 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11505 {
11506 nn = java_complete_tree (wfl_op1);
11507 if (nn == error_mark_node)
11508 return error_mark_node;
48a840d9 11509
2aa11e97
APB
11510 TREE_OPERAND (node, 0) = nn;
11511 }
b67d701b 11512 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11513 {
11514 nn = java_complete_tree (wfl_op2);
11515 if (nn == error_mark_node)
11516 return error_mark_node;
48a840d9 11517
2aa11e97
APB
11518 TREE_OPERAND (node, 1) = nn;
11519 }
dc0b3eff 11520 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11521
5e942c50
APB
11522 case INSTANCEOF_EXPR:
11523 wfl_op1 = TREE_OPERAND (node, 0);
11524 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11525 if (flag_emit_xref)
11526 {
11527 TREE_TYPE (node) = boolean_type_node;
11528 return node;
11529 }
5e942c50
APB
11530 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11531
b67d701b 11532 case UNARY_PLUS_EXPR:
e04a16fb
AG
11533 case NEGATE_EXPR:
11534 case TRUTH_NOT_EXPR:
11535 case BIT_NOT_EXPR:
11536 case PREDECREMENT_EXPR:
11537 case PREINCREMENT_EXPR:
11538 case POSTDECREMENT_EXPR:
11539 case POSTINCREMENT_EXPR:
11540 case CONVERT_EXPR:
11541 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11542 how to handle those cases. */
11543 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11544 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11545 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11546 if (TREE_OPERAND (node, 0) == error_mark_node)
11547 return error_mark_node;
4a5f66c3
APB
11548 node = patch_unaryop (node, wfl_op1);
11549 CAN_COMPLETE_NORMALLY (node) = 1;
11550 break;
e04a16fb
AG
11551
11552 case ARRAY_REF:
11553 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11554 how to handle those cases. */
11555 wfl_op1 = TREE_OPERAND (node, 0);
11556 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11557 if (TREE_OPERAND (node, 0) == error_mark_node)
11558 return error_mark_node;
7f1d4866 11559 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11560 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11561 /* The same applies to wfl_op2 */
11562 wfl_op2 = TREE_OPERAND (node, 1);
11563 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11564 if (TREE_OPERAND (node, 1) == error_mark_node)
11565 return error_mark_node;
7f1d4866 11566 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11567 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11568 return patch_array_ref (node);
e04a16fb 11569
63a212ed
PB
11570 case RECORD_TYPE:
11571 return node;;
11572
11573 case COMPONENT_REF:
11574 /* The first step in the re-write of qualified name handling. FIXME.
11575 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11576 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11577 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11578 {
11579 tree name = TREE_OPERAND (node, 1);
11580 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11581 if (field == NULL_TREE)
11582 {
11583 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11584 return error_mark_node;
11585 }
11586 if (! FIELD_STATIC (field))
11587 {
11588 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11589 return error_mark_node;
11590 }
11591 return field;
11592 }
11593 else
11594 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11595 break;
9bbc7d9f 11596
b67d701b 11597 case THIS_EXPR:
e04a16fb
AG
11598 /* Can't use THIS in a static environment */
11599 if (!current_this)
11600 {
11601 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11602 parse_error_context (wfl_operator,
11603 "Keyword `this' used outside allowed context");
e04a16fb
AG
11604 TREE_TYPE (node) = error_mark_node;
11605 return error_mark_node;
11606 }
22eed1e6
APB
11607 if (ctxp->explicit_constructor_p)
11608 {
11609 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11610 parse_error_context
781b0558 11611 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11612 TREE_TYPE (node) = error_mark_node;
11613 return error_mark_node;
11614 }
e04a16fb 11615 return current_this;
c2952b01
APB
11616
11617 case CLASS_LITERAL:
11618 CAN_COMPLETE_NORMALLY (node) = 1;
11619 node = patch_incomplete_class_ref (node);
11620 if (node == error_mark_node)
11621 return error_mark_node;
11622 break;
11623
11624 case INSTANCE_INITIALIZERS_EXPR:
11625 in_instance_initializer++;
11626 node = java_complete_tree (TREE_OPERAND (node, 0));
11627 in_instance_initializer--;
11628 if (node != error_mark_node)
11629 TREE_TYPE (node) = void_type_node;
11630 else
11631 return error_mark_node;
11632 break;
e04a16fb 11633
e04a16fb 11634 default:
15fdcfe9 11635 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11636 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11637 and it's time to turn it into the appropriate String object */
11638 if ((nn = patch_string (node)))
11639 node = nn;
11640 else
11641 fatal ("No case for tree code `%s' - java_complete_tree\n",
11642 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11643 }
11644 return node;
11645}
11646
11647/* Complete function call's argument. Return a non zero value is an
11648 error was found. */
11649
11650static int
11651complete_function_arguments (node)
11652 tree node;
11653{
11654 int flag = 0;
11655 tree cn;
11656
f63991a8 11657 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11658 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11659 {
b67d701b 11660 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11661 parm = java_complete_tree (wfl);
c2952b01 11662
e04a16fb
AG
11663 if (parm == error_mark_node)
11664 {
11665 flag = 1;
11666 continue;
11667 }
b67d701b
PB
11668 /* If have a string literal that we haven't transformed yet or a
11669 crafted string buffer, as a result of use of the the String
11670 `+' operator. Build `parm.toString()' and expand it. */
11671 if ((temp = patch_string (parm)))
b9f7e36c 11672 parm = temp;
5e942c50
APB
11673 /* Inline PRIMTYPE.TYPE read access */
11674 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11675
5e942c50 11676 TREE_VALUE (cn) = parm;
e04a16fb 11677 }
f63991a8 11678 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11679 return flag;
11680}
11681
11682/* Sometimes (for loops and variable initialized during their
11683 declaration), we want to wrap a statement around a WFL and turn it
11684 debugable. */
11685
11686static tree
11687build_debugable_stmt (location, stmt)
11688 int location;
11689 tree stmt;
11690{
11691 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11692 {
11693 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11694 EXPR_WFL_LINECOL (stmt) = location;
11695 }
11696 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11697 return stmt;
11698}
11699
11700static tree
11701build_expr_block (body, decls)
11702 tree body, decls;
11703{
11704 tree node = make_node (BLOCK);
11705 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11706 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11707 if (body)
11708 TREE_TYPE (node) = TREE_TYPE (body);
11709 TREE_SIDE_EFFECTS (node) = 1;
11710 return node;
11711}
11712
b67d701b
PB
11713/* Create a new function block and link it approriately to current
11714 function block chain */
e04a16fb
AG
11715
11716static tree
11717enter_block ()
11718{
b67d701b
PB
11719 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11720}
11721
11722/* Link block B supercontext to the previous block. The current
11723 function DECL is used as supercontext when enter_a_block is called
11724 for the first time for a given function. The current function body
11725 (DECL_FUNCTION_BODY) is set to be block B. */
11726
11727static tree
11728enter_a_block (b)
11729 tree b;
11730{
e04a16fb
AG
11731 tree fndecl = current_function_decl;
11732
f099f336
APB
11733 if (!fndecl) {
11734 BLOCK_SUPERCONTEXT (b) = current_static_block;
11735 current_static_block = b;
11736 }
11737
11738 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11739 {
11740 BLOCK_SUPERCONTEXT (b) = fndecl;
11741 DECL_FUNCTION_BODY (fndecl) = b;
11742 }
11743 else
11744 {
11745 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11746 DECL_FUNCTION_BODY (fndecl) = b;
11747 }
11748 return b;
11749}
11750
11751/* Exit a block by changing the current function body
11752 (DECL_FUNCTION_BODY) to the current block super context, only if
11753 the block being exited isn't the method's top level one. */
11754
11755static tree
11756exit_block ()
11757{
f099f336
APB
11758 tree b;
11759 if (current_function_decl)
11760 {
11761 b = DECL_FUNCTION_BODY (current_function_decl);
11762 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11763 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11764 }
11765 else
11766 {
11767 b = current_static_block;
e04a16fb 11768
f099f336
APB
11769 if (BLOCK_SUPERCONTEXT (b))
11770 current_static_block = BLOCK_SUPERCONTEXT (b);
11771 }
e04a16fb
AG
11772 return b;
11773}
11774
11775/* Lookup for NAME in the nested function's blocks, all the way up to
11776 the current toplevel one. It complies with Java's local variable
11777 scoping rules. */
11778
11779static tree
11780lookup_name_in_blocks (name)
11781 tree name;
11782{
f099f336 11783 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11784
11785 while (b != current_function_decl)
11786 {
11787 tree current;
11788
11789 /* Paranoid sanity check. To be removed */
11790 if (TREE_CODE (b) != BLOCK)
11791 fatal ("non block expr function body - lookup_name_in_blocks");
11792
11793 for (current = BLOCK_EXPR_DECLS (b); current;
11794 current = TREE_CHAIN (current))
11795 if (DECL_NAME (current) == name)
11796 return current;
11797 b = BLOCK_SUPERCONTEXT (b);
11798 }
11799 return NULL_TREE;
11800}
11801
11802static void
11803maybe_absorb_scoping_blocks ()
11804{
f099f336 11805 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11806 {
11807 tree b = exit_block ();
11808 java_method_add_stmt (current_function_decl, b);
11809 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11810 }
11811}
11812
11813\f
11814/* This section of the source is reserved to build_* functions that
11815 are building incomplete tree nodes and the patch_* functions that
11816 are completing them. */
11817
c2952b01
APB
11818/* Wrap a non WFL node around a WFL. */
11819static tree
9a7ab4b3 11820build_wfl_wrap (node, location)
c2952b01 11821 tree node;
9a7ab4b3 11822 int location;
c2952b01
APB
11823{
11824 tree wfl, node_to_insert = node;
11825
11826 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11827 with the way we're processing SUPER. A THIS from a primary as a
11828 different form than a SUPER. Turn THIS into something symbolic */
11829 if (TREE_CODE (node) == THIS_EXPR)
11830 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11831 else
11832 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11833
9a7ab4b3 11834 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11835 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11836 return wfl;
11837}
11838
11839
9bbc7d9f 11840/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11841 we're currently dealing with the class java.lang.Object. */
11842
11843static tree
e920ebc9
APB
11844build_super_invocation (mdecl)
11845 tree mdecl;
22eed1e6 11846{
e920ebc9 11847 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11848 return empty_stmt_node;
22eed1e6
APB
11849 else
11850 {
9ee9b555 11851 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11852 tree a = NULL_TREE, t;
11853 /* If we're dealing with an anonymous class, pass the arguments
11854 of the crafted constructor along. */
11855 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11856 {
11857 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11858 for (; t != end_params_node; t = TREE_CHAIN (t))
11859 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11860 }
11861 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11862 }
11863}
11864
11865/* Build a SUPER/THIS qualified method invocation. */
11866
11867static tree
11868build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11869 int use_this;
11870 tree name, args;
11871 int lloc, rloc;
22eed1e6
APB
11872{
11873 tree invok;
11874 tree wfl =
9ee9b555 11875 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11876 EXPR_WFL_LINECOL (wfl) = lloc;
11877 invok = build_method_invocation (name, args);
11878 return make_qualified_primary (wfl, invok, rloc);
11879}
11880
b67d701b 11881/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11882
11883static tree
11884build_method_invocation (name, args)
11885 tree name;
11886 tree args;
11887{
11888 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11889 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11890 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11891 return call;
11892}
11893
11894/* Build an incomplete new xxx(...) node. */
11895
11896static tree
11897build_new_invocation (name, args)
11898 tree name, args;
11899{
11900 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11901 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11902 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11903 return call;
11904}
11905
11906/* Build an incomplete assignment expression. */
11907
11908static tree
11909build_assignment (op, op_location, lhs, rhs)
11910 int op, op_location;
11911 tree lhs, rhs;
11912{
11913 tree assignment;
11914 /* Build the corresponding binop if we deal with a Compound
11915 Assignment operator. Mark the binop sub-tree as part of a
11916 Compound Assignment expression */
11917 if (op != ASSIGN_TK)
11918 {
11919 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11920 COMPOUND_ASSIGN_P (rhs) = 1;
11921 }
11922 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11923 TREE_SIDE_EFFECTS (assignment) = 1;
11924 EXPR_WFL_LINECOL (assignment) = op_location;
11925 return assignment;
11926}
11927
11928/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11929
15fdcfe9 11930char *
e04a16fb
AG
11931print_int_node (node)
11932 tree node;
11933{
11934 static char buffer [80];
11935 if (TREE_CONSTANT_OVERFLOW (node))
11936 sprintf (buffer, "<overflow>");
11937
11938 if (TREE_INT_CST_HIGH (node) == 0)
11939 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11940 TREE_INT_CST_LOW (node));
11941 else if (TREE_INT_CST_HIGH (node) == -1
11942 && TREE_INT_CST_LOW (node) != 0)
11943 {
11944 buffer [0] = '-';
11945 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11946 -TREE_INT_CST_LOW (node));
11947 }
11948 else
11949 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11950 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11951
11952 return buffer;
11953}
11954
7f1d4866
APB
11955/* Return 1 if an assignment to a FINAL is attempted in a non suitable
11956 context. */
5e942c50
APB
11957
11958static int
11959check_final_assignment (lvalue, wfl)
11960 tree lvalue, wfl;
11961{
6632dcdd
APB
11962 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11963 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11964 lvalue = TREE_OPERAND (lvalue, 1);
11965
bc2874c9
TT
11966 /* When generating class files, references to the `length' field
11967 look a bit different. */
11968 if ((flag_emit_class_files
11969 && TREE_CODE (lvalue) == COMPONENT_REF
11970 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11971 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11972 || (TREE_CODE (lvalue) == FIELD_DECL
11973 && FIELD_FINAL (lvalue)
11974 && !DECL_CLINIT_P (current_function_decl)
11975 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
11976 {
11977 parse_error_context
11978 (wfl, "Can't assign a value to the final variable `%s'",
11979 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11980 return 1;
11981 }
11982 return 0;
11983}
11984
11985/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11986 read. This is needed to avoid circularities in the implementation
11987 of these fields in libjava. */
11988
11989static tree
11990maybe_build_primttype_type_ref (rhs, wfl)
11991 tree rhs, wfl;
11992{
11993 tree to_return = NULL_TREE;
11994 tree rhs_type = TREE_TYPE (rhs);
11995 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11996 {
11997 tree n = TREE_OPERAND (rhs, 1);
11998 if (TREE_CODE (n) == VAR_DECL
11999 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12000 && rhs_type == class_ptr_type
12001 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12002 {
49f48c71 12003 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12004 if (!strncmp (self_name, "java.lang.", 10))
12005 to_return = build_primtype_type_ref (self_name);
12006 }
12007 }
12008 return (to_return ? to_return : rhs );
12009}
12010
e04a16fb
AG
12011/* 15.25 Assignment operators. */
12012
12013static tree
12014patch_assignment (node, wfl_op1, wfl_op2)
12015 tree node;
12016 tree wfl_op1;
12017 tree wfl_op2;
12018{
0a2138e2 12019 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12020 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12021 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12022 int error_found = 0;
12023 int lvalue_from_array = 0;
12024
c2952b01 12025 /* Can't assign to a (blank) final. */
5e942c50
APB
12026 if (check_final_assignment (lvalue, wfl_op1))
12027 error_found = 1;
e04a16fb
AG
12028
12029 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12030
12031 /* Lhs can be a named variable */
34f4db93 12032 if (JDECL_P (lvalue))
e04a16fb 12033 {
e04a16fb
AG
12034 lhs_type = TREE_TYPE (lvalue);
12035 }
12036 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12037 comment on reason why */
12038 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12039 {
12040 lhs_type = TREE_TYPE (lvalue);
12041 lvalue_from_array = 1;
12042 }
12043 /* Or a field access */
12044 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12045 lhs_type = TREE_TYPE (lvalue);
12046 /* Or a function return slot */
12047 else if (TREE_CODE (lvalue) == RESULT_DECL)
12048 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12049 /* Otherwise, we might want to try to write into an optimized static
12050 final, this is an of a different nature, reported further on. */
12051 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12052 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12053 {
6632dcdd 12054 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12055 {
12056 /* What we should do instead is resetting the all the flags
12057 previously set, exchange lvalue for llvalue and continue. */
12058 error_found = 1;
12059 return error_mark_node;
12060 }
12061 else
12062 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12063 }
12064 else
e04a16fb
AG
12065 {
12066 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12067 error_found = 1;
12068 }
12069
12070 rhs_type = TREE_TYPE (rhs);
b67d701b 12071 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12072 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12073
b67d701b 12074 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12075 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12076 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12077
12078 /* 15.25.2 If we have a compound assignment, convert RHS into the
12079 type of the LHS */
12080 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12081 new_rhs = convert (lhs_type, rhs);
12082
12083 /* Explicit cast required. This is an error */
12084 if (!new_rhs)
12085 {
c2e3db92
KG
12086 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12087 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12088 tree wfl;
12089 char operation [32]; /* Max size known */
12090
12091 /* If the assignment is part of a declaration, we use the WFL of
12092 the declared variable to point out the error and call it a
12093 declaration problem. If the assignment is a genuine =
12094 operator, we call is a operator `=' problem, otherwise we
12095 call it an assignment problem. In both of these last cases,
12096 we use the WFL of the operator to indicate the error. */
12097
12098 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12099 {
12100 wfl = wfl_op1;
12101 strcpy (operation, "declaration");
12102 }
12103 else
12104 {
12105 wfl = wfl_operator;
12106 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12107 strcpy (operation, "assignment");
12108 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12109 strcpy (operation, "`return'");
12110 else
12111 strcpy (operation, "`='");
12112 }
12113
1ebadc60 12114 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12115 parse_error_context
12116 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12117 operation, t1, t2);
1ebadc60 12118 else
781b0558 12119 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12120 operation, t1, t2);
e04a16fb
AG
12121 free (t1); free (t2);
12122 error_found = 1;
12123 }
12124
c877974e
APB
12125 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12126 if (new_rhs)
12127 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12128
e04a16fb
AG
12129 if (error_found)
12130 return error_mark_node;
12131
2622b947
APB
12132 /* 10.10: Array Store Exception runtime check */
12133 if (!flag_emit_class_files
e8fc7396 12134 && !flag_emit_xref
2622b947 12135 && lvalue_from_array
afc390b1 12136 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12137 {
12138 tree check;
12139 tree base = lvalue;
12140
12141 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12142 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12143 base = TREE_OPERAND (lvalue, 0);
12144 else
12145 {
12146 if (flag_bounds_check)
12147 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12148 else
12149 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12150 }
12151
12152 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12153 new_rhs = save_expr (new_rhs);
2622b947
APB
12154 check = build (CALL_EXPR, void_type_node,
12155 build_address_of (soft_checkarraystore_node),
12156 tree_cons (NULL_TREE, base,
12157 build_tree_list (NULL_TREE, new_rhs)),
12158 NULL_TREE);
12159 TREE_SIDE_EFFECTS (check) = 1;
12160
12161 /* We have to decide on an insertion point */
12162 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12163 {
12164 tree t;
12165 if (flag_bounds_check)
12166 {
12167 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12168 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12169 build (COMPOUND_EXPR, void_type_node, t, check);
12170 }
12171 else
12172 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12173 check, TREE_OPERAND (lvalue, 1));
12174 }
12175 else
12176 {
12177 /* Make sure the bound check will happen before the store check */
12178 if (flag_bounds_check)
12179 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12180 build (COMPOUND_EXPR, void_type_node,
12181 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12182 else
12183 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12184 }
12185 }
22eed1e6 12186
34d4df06
APB
12187 /* Final locals can be used as case values in switch
12188 statement. Prepare them for this eventuality. */
12189 if (TREE_CODE (lvalue) == VAR_DECL
12190 && LOCAL_FINAL (lvalue)
12191 && TREE_CONSTANT (new_rhs)
12192 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12193 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12194 )
12195 {
12196 TREE_CONSTANT (lvalue) = 1;
12197 DECL_INITIAL (lvalue) = new_rhs;
12198 }
12199
e04a16fb
AG
12200 TREE_OPERAND (node, 0) = lvalue;
12201 TREE_OPERAND (node, 1) = new_rhs;
12202 TREE_TYPE (node) = lhs_type;
12203 return node;
12204}
12205
b67d701b
PB
12206/* Check that type SOURCE can be cast into type DEST. If the cast
12207 can't occur at all, return 0 otherwise 1. This function is used to
12208 produce accurate error messages on the reasons why an assignment
12209 failed. */
e04a16fb 12210
b67d701b
PB
12211static tree
12212try_reference_assignconv (lhs_type, rhs)
12213 tree lhs_type, rhs;
e04a16fb 12214{
b67d701b
PB
12215 tree new_rhs = NULL_TREE;
12216 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12217
b67d701b
PB
12218 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12219 {
12220 /* `null' may be assigned to any reference type */
12221 if (rhs == null_pointer_node)
12222 new_rhs = null_pointer_node;
12223 /* Try the reference assignment conversion */
12224 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12225 new_rhs = rhs;
12226 /* This is a magic assignment that we process differently */
12227 else if (rhs == soft_exceptioninfo_call_node)
12228 new_rhs = rhs;
12229 }
12230 return new_rhs;
12231}
12232
12233/* Check that RHS can be converted into LHS_TYPE by the assignment
12234 conversion (5.2), for the cases of RHS being a builtin type. Return
12235 NULL_TREE if the conversion fails or if because RHS isn't of a
12236 builtin type. Return a converted RHS if the conversion is possible. */
12237
12238static tree
12239try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12240 tree wfl_op1, lhs_type, rhs;
12241{
12242 tree new_rhs = NULL_TREE;
12243 tree rhs_type = TREE_TYPE (rhs);
12244
5e942c50
APB
12245 /* Zero accepted everywhere */
12246 if (TREE_CODE (rhs) == INTEGER_CST
12247 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12248 && JPRIMITIVE_TYPE_P (rhs_type))
12249 new_rhs = convert (lhs_type, rhs);
12250
b67d701b
PB
12251 /* 5.1.1 Try Identity Conversion,
12252 5.1.2 Try Widening Primitive Conversion */
5e942c50 12253 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12254 new_rhs = convert (lhs_type, rhs);
12255
12256 /* Try a narrowing primitive conversion (5.1.3):
12257 - expression is a constant expression of type int AND
12258 - variable is byte, short or char AND
12259 - The value of the expression is representable in the type of the
12260 variable */
12261 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12262 && (lhs_type == byte_type_node || lhs_type == char_type_node
12263 || lhs_type == short_type_node))
12264 {
12265 if (int_fits_type_p (rhs, lhs_type))
12266 new_rhs = convert (lhs_type, rhs);
12267 else if (wfl_op1) /* Might be called with a NULL */
12268 parse_warning_context
781b0558 12269 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
0a2138e2 12270 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12271 /* Reported a warning that will turn into an error further
12272 down, so we don't return */
12273 }
12274
12275 return new_rhs;
12276}
12277
12278/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12279 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12280 0 is the conversion test fails. This implements parts the method
12281 invocation convertion (5.3). */
12282
12283static int
12284valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12285 tree lhs_type, rhs_type;
12286{
acd663ee 12287 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12288 if (lhs_type == rhs_type)
12289 return 1;
12290
acd663ee
APB
12291 /* Reject non primitive types */
12292 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
b67d701b
PB
12293 return 0;
12294
acd663ee
APB
12295 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12296 than a char can't be converted into a char. Short can't too, but
12297 the < test below takes care of that */
b67d701b
PB
12298 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12299 return 0;
12300
5e942c50
APB
12301 /* Accept all promoted type here. Note, we can't use <= in the test
12302 below, because we still need to bounce out assignments of short
12303 to char and the likes */
12304 if (lhs_type == int_type_node
12305 && (rhs_type == promoted_byte_type_node
12306 || rhs_type == promoted_short_type_node
12307 || rhs_type == promoted_char_type_node
12308 || rhs_type == promoted_boolean_type_node))
12309 return 1;
12310
acd663ee
APB
12311 /* From here, an integral is widened if its precision is smaller
12312 than the precision of the LHS or if the LHS is a floating point
12313 type, or the RHS is a float and the RHS a double. */
12314 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12315 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12316 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12317 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12318 return 1;
12319
12320 return 0;
e04a16fb
AG
12321}
12322
12323/* Check that something of SOURCE type can be assigned or cast to
12324 something of DEST type at runtime. Return 1 if the operation is
12325 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12326 were SOURCE is cast into DEST, which borrows a lot of the
12327 assignment check. */
12328
12329static int
12330valid_ref_assignconv_cast_p (source, dest, cast)
12331 tree source;
12332 tree dest;
12333 int cast;
12334{
09ed0f70
APB
12335 /* SOURCE or DEST might be null if not from a declared entity. */
12336 if (!source || !dest)
12337 return 0;
5e942c50
APB
12338 if (JNULLP_TYPE_P (source))
12339 return 1;
e04a16fb
AG
12340 if (TREE_CODE (source) == POINTER_TYPE)
12341 source = TREE_TYPE (source);
12342 if (TREE_CODE (dest) == POINTER_TYPE)
12343 dest = TREE_TYPE (dest);
12344 /* Case where SOURCE is a class type */
12345 if (TYPE_CLASS_P (source))
12346 {
12347 if (TYPE_CLASS_P (dest))
c2952b01
APB
12348 return (source == dest
12349 || inherits_from_p (source, dest)
c2952b01 12350 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12351 if (TYPE_INTERFACE_P (dest))
12352 {
12353 /* If doing a cast and SOURCE is final, the operation is
12354 always correct a compile time (because even if SOURCE
12355 does not implement DEST, a subclass of SOURCE might). */
12356 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12357 return 1;
12358 /* Otherwise, SOURCE must implement DEST */
12359 return interface_of_p (dest, source);
12360 }
12361 /* DEST is an array, cast permited if SOURCE is of Object type */
12362 return (cast && source == object_type_node ? 1 : 0);
12363 }
12364 if (TYPE_INTERFACE_P (source))
12365 {
12366 if (TYPE_CLASS_P (dest))
12367 {
12368 /* If not casting, DEST must be the Object type */
12369 if (!cast)
12370 return dest == object_type_node;
12371 /* We're doing a cast. The cast is always valid is class
12372 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12373 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12374 return 1;
12375 else
12376 return interface_of_p (source, dest);
12377 }
12378 if (TYPE_INTERFACE_P (dest))
12379 {
12380 /* If doing a cast, then if SOURCE and DEST contain method
12381 with the same signature but different return type, then
12382 this is a (compile time) error */
12383 if (cast)
12384 {
12385 tree method_source, method_dest;
12386 tree source_type;
0a2138e2 12387 tree source_sig;
e04a16fb
AG
12388 tree source_name;
12389 for (method_source = TYPE_METHODS (source); method_source;
12390 method_source = TREE_CHAIN (method_source))
12391 {
12392 source_sig =
12393 build_java_argument_signature (TREE_TYPE (method_source));
12394 source_type = TREE_TYPE (TREE_TYPE (method_source));
12395 source_name = DECL_NAME (method_source);
12396 for (method_dest = TYPE_METHODS (dest);
12397 method_dest; method_dest = TREE_CHAIN (method_dest))
12398 if (source_sig ==
12399 build_java_argument_signature (TREE_TYPE (method_dest))
12400 && source_name == DECL_NAME (method_dest)
12401 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12402 return 0;
12403 }
12404 return 1;
12405 }
12406 else
12407 return source == dest || interface_of_p (dest, source);
12408 }
ee17a290
TT
12409 else
12410 {
12411 /* Array */
12412 return (cast
12413 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12414 || (DECL_NAME (TYPE_NAME (source))
12415 == java_io_serializable)));
12416 }
e04a16fb
AG
12417 }
12418 if (TYPE_ARRAY_P (source))
12419 {
12420 if (TYPE_CLASS_P (dest))
12421 return dest == object_type_node;
09ed0f70 12422 /* Can't cast an array to an interface unless the interface is
ee17a290 12423 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12424 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12425 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12426 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12427 else /* Arrays */
12428 {
12429 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12430 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12431
b9f7e36c
APB
12432 /* In case of severe errors, they turn out null */
12433 if (!dest_element_type || !source_element_type)
12434 return 0;
e04a16fb
AG
12435 if (source_element_type == dest_element_type)
12436 return 1;
12437 return valid_ref_assignconv_cast_p (source_element_type,
12438 dest_element_type, cast);
12439 }
12440 return 0;
12441 }
12442 return 0;
12443}
12444
b67d701b
PB
12445static int
12446valid_cast_to_p (source, dest)
12447 tree source;
12448 tree dest;
12449{
12450 if (TREE_CODE (source) == POINTER_TYPE)
12451 source = TREE_TYPE (source);
12452 if (TREE_CODE (dest) == POINTER_TYPE)
12453 dest = TREE_TYPE (dest);
12454
12455 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12456 return valid_ref_assignconv_cast_p (source, dest, 1);
12457
12458 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12459 return 1;
12460
12461 return 0;
12462}
12463
12464/* Method invocation conversion test. Return 1 if type SOURCE can be
12465 converted to type DEST through the methond invocation conversion
12466 process (5.3) */
12467
15fdcfe9
PB
12468static tree
12469do_unary_numeric_promotion (arg)
12470 tree arg;
12471{
12472 tree type = TREE_TYPE (arg);
12473 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
12474 : TREE_CODE (type) == CHAR_TYPE)
12475 arg = convert (int_type_node, arg);
12476 return arg;
12477}
12478
acd663ee
APB
12479/* Return a non zero value if SOURCE can be converted into DEST using
12480 the method invocation conversion rule (5.3). */
b67d701b
PB
12481static int
12482valid_method_invocation_conversion_p (dest, source)
12483 tree dest, source;
12484{
e3884b71 12485 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12486 && valid_builtin_assignconv_identity_widening_p (dest, source))
12487 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12488 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12489 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12490}
12491
e04a16fb
AG
12492/* Build an incomplete binop expression. */
12493
12494static tree
12495build_binop (op, op_location, op1, op2)
12496 enum tree_code op;
12497 int op_location;
12498 tree op1, op2;
12499{
5e942c50 12500 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12501 TREE_SIDE_EFFECTS (binop) = 1;
12502 /* Store the location of the operator, for better error report. The
12503 string of the operator will be rebuild based on the OP value. */
12504 EXPR_WFL_LINECOL (binop) = op_location;
12505 return binop;
12506}
12507
12508/* Build the string of the operator retained by NODE. If NODE is part
12509 of a compound expression, add an '=' at the end of the string. This
12510 function is called when an error needs to be reported on an
12511 operator. The string is returned as a pointer to a static character
12512 buffer. */
12513
12514static char *
12515operator_string (node)
12516 tree node;
12517{
12518#define BUILD_OPERATOR_STRING(S) \
12519 { \
12520 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12521 return buffer; \
12522 }
12523
12524 static char buffer [10];
12525 switch (TREE_CODE (node))
12526 {
12527 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12528 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12529 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12530 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12531 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12532 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12533 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12534 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12535 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12536 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12537 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12538 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12539 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12540 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12541 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12542 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12543 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12544 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12545 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12546 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12547 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12548 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12549 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12550 case PREINCREMENT_EXPR: /* Fall through */
12551 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12552 case PREDECREMENT_EXPR: /* Fall through */
12553 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12554 default:
12555 fatal ("unregistered operator %s - operator_string",
12556 tree_code_name [TREE_CODE (node)]);
12557 }
12558 return NULL;
12559#undef BUILD_OPERATOR_STRING
12560}
12561
5cbdba64
APB
12562/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12563
12564static int
12565java_decl_equiv (var_acc1, var_acc2)
12566 tree var_acc1, var_acc2;
12567{
12568 if (JDECL_P (var_acc1))
12569 return (var_acc1 == var_acc2);
12570
12571 return (TREE_CODE (var_acc1) == COMPONENT_REF
12572 && TREE_CODE (var_acc2) == COMPONENT_REF
12573 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12574 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12575 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12576}
12577
12578/* Return a non zero value if CODE is one of the operators that can be
12579 used in conjunction with the `=' operator in a compound assignment. */
12580
12581static int
12582binop_compound_p (code)
12583 enum tree_code code;
12584{
12585 int i;
12586 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12587 if (binop_lookup [i] == code)
12588 break;
12589
12590 return i < BINOP_COMPOUND_CANDIDATES;
12591}
12592
12593/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12594
12595static tree
12596java_refold (t)
12597 tree t;
12598{
12599 tree c, b, ns, decl;
12600
12601 if (TREE_CODE (t) != MODIFY_EXPR)
12602 return t;
12603
12604 c = TREE_OPERAND (t, 1);
12605 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12606 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12607 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12608 return t;
12609
12610 /* Now the left branch of the binary operator. */
12611 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12612 if (! (b && TREE_CODE (b) == NOP_EXPR
12613 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12614 return t;
12615
12616 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12617 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12618 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12619 return t;
12620
12621 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12622 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12623 /* It's got to be the an equivalent decl */
12624 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12625 {
12626 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12627 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12628 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12629 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12630 /* Change the right part of the BINOP_EXPR */
12631 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12632 }
12633
12634 return t;
12635}
12636
e04a16fb
AG
12637/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12638 errors but we modify NODE so that it contains the type computed
12639 according to the expression, when it's fixed. Otherwise, we write
12640 error_mark_node as the type. It allows us to further the analysis
12641 of remaining nodes and detects more errors in certain cases. */
12642
12643static tree
12644patch_binop (node, wfl_op1, wfl_op2)
12645 tree node;
12646 tree wfl_op1;
12647 tree wfl_op2;
12648{
12649 tree op1 = TREE_OPERAND (node, 0);
12650 tree op2 = TREE_OPERAND (node, 1);
12651 tree op1_type = TREE_TYPE (op1);
12652 tree op2_type = TREE_TYPE (op2);
48a840d9 12653 tree prom_type = NULL_TREE, cn;
e04a16fb 12654 int code = TREE_CODE (node);
b67d701b 12655
e04a16fb
AG
12656 /* If 1, tell the routine that we have to return error_mark_node
12657 after checking for the initialization of the RHS */
12658 int error_found = 0;
12659
e04a16fb
AG
12660 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12661
e04a16fb
AG
12662 switch (code)
12663 {
12664 /* 15.16 Multiplicative operators */
12665 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12666 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12667 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb
AG
12668 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12669 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12670 {
12671 if (!JPRIMITIVE_TYPE_P (op1_type))
12672 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12673 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12674 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12675 TREE_TYPE (node) = error_mark_node;
12676 error_found = 1;
12677 break;
12678 }
12679 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12680 /* Change the division operator if necessary */
12681 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12682 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12683
aa4759c1
AH
12684 if (TREE_CODE (prom_type) == INTEGER_TYPE
12685 && flag_use_divide_subroutine
12686 && ! flag_emit_class_files
12687 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12688 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12689
0b4d333e
APB
12690 /* This one is more complicated. FLOATs are processed by a
12691 function call to soft_fmod. Duplicate the value of the
12692 COMPOUND_ASSIGN_P flag. */
e04a16fb 12693 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12694 {
12695 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12696 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12697 TREE_SIDE_EFFECTS (mod)
12698 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12699 return mod;
12700 }
e04a16fb
AG
12701 break;
12702
12703 /* 15.17 Additive Operators */
12704 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12705
12706 /* Operation is valid if either one argument is a string
12707 constant, a String object or a StringBuffer crafted for the
12708 purpose of the a previous usage of the String concatenation
12709 operator */
12710
12711 if (TREE_CODE (op1) == STRING_CST
12712 || TREE_CODE (op2) == STRING_CST
12713 || JSTRING_TYPE_P (op1_type)
12714 || JSTRING_TYPE_P (op2_type)
12715 || IS_CRAFTED_STRING_BUFFER_P (op1)
12716 || IS_CRAFTED_STRING_BUFFER_P (op2))
12717 return build_string_concatenation (op1, op2);
12718
e04a16fb
AG
12719 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12720 Numeric Types */
12721 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12722 {
12723 if (!JPRIMITIVE_TYPE_P (op1_type))
12724 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12725 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12726 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12727 TREE_TYPE (node) = error_mark_node;
12728 error_found = 1;
12729 break;
12730 }
12731 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12732 break;
12733
12734 /* 15.18 Shift Operators */
12735 case LSHIFT_EXPR:
12736 case RSHIFT_EXPR:
12737 case URSHIFT_EXPR:
12738 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12739 {
12740 if (!JINTEGRAL_TYPE_P (op1_type))
12741 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12742 else
1ebadc60
KG
12743 {
12744 if (JPRIMITIVE_TYPE_P (op2_type))
12745 parse_error_context (wfl_operator,
781b0558 12746 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12747 operator_string (node),
12748 lang_printable_name (op2_type, 0));
12749 else
781b0558
KG
12750 parse_error_context (wfl_operator,
12751 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12752 operator_string (node),
12753 lang_printable_name (op2_type, 0));
12754 }
e04a16fb
AG
12755 TREE_TYPE (node) = error_mark_node;
12756 error_found = 1;
12757 break;
12758 }
12759
12760 /* Unary numeric promotion (5.6.1) is performed on each operand
12761 separatly */
15fdcfe9
PB
12762 op1 = do_unary_numeric_promotion (op1);
12763 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12764
12765 /* The type of the shift expression is the type of the promoted
12766 type of the left-hand operand */
12767 prom_type = TREE_TYPE (op1);
12768
c2952b01
APB
12769 /* Shift int only up to 0x1f and long up to 0x3f */
12770 if (prom_type == int_type_node)
12771 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12772 build_int_2 (0x1f, 0)));
12773 else
12774 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12775 build_int_2 (0x3f, 0)));
e04a16fb
AG
12776
12777 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12778 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12779 {
0b4d333e 12780 tree to_return;
73333a87
AH
12781 tree utype = unsigned_type (prom_type);
12782 op1 = convert (utype, op1);
e04a16fb 12783 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12784 TREE_OPERAND (node, 0) = op1;
12785 TREE_OPERAND (node, 1) = op2;
12786 TREE_TYPE (node) = utype;
0b4d333e
APB
12787 to_return = convert (prom_type, node);
12788 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12789 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12790 TREE_SIDE_EFFECTS (to_return)
12791 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12792 return to_return;
e04a16fb
AG
12793 }
12794 break;
5e942c50
APB
12795
12796 /* 15.19.1 Type Comparison Operator instaceof */
12797 case INSTANCEOF_EXPR:
12798
12799 TREE_TYPE (node) = boolean_type_node;
12800
12801 if (!(op2_type = resolve_type_during_patch (op2)))
12802 return error_mark_node;
12803
12804 /* The first operand must be a reference type or the null type */
12805 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12806 error_found = 1; /* Error reported further below */
12807
12808 /* The second operand must be a reference type */
12809 if (!JREFERENCE_TYPE_P (op2_type))
12810 {
12811 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12812 parse_error_context
12813 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12814 lang_printable_name (op2_type, 0));
12815 error_found = 1;
12816 }
12817
12818 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12819 {
12820 /* If the first operand is null, the result is always false */
12821 if (op1 == null_pointer_node)
12822 return boolean_false_node;
15fdcfe9
PB
12823 else if (flag_emit_class_files)
12824 {
12825 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12826 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12827 return node;
12828 }
5e942c50
APB
12829 /* Otherwise we have to invoke instance of to figure it out */
12830 else
67db0ce7 12831 return build_instanceof (op1, op2_type);
5e942c50
APB
12832 }
12833 /* There is no way the expression operand can be an instance of
12834 the type operand. This is a compile time error. */
12835 else
12836 {
c2e3db92 12837 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12838 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12839 parse_error_context
12840 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12841 t1, lang_printable_name (op2_type, 0));
12842 free (t1);
12843 error_found = 1;
12844 }
e04a16fb 12845
5e942c50 12846 break;
e04a16fb
AG
12847
12848 /* 15.21 Bitwise and Logical Operators */
12849 case BIT_AND_EXPR:
12850 case BIT_XOR_EXPR:
12851 case BIT_IOR_EXPR:
12852 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12853 /* Binary numeric promotion is performed on both operand and the
12854 expression retain that type */
12855 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12856
12857 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12858 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12859 /* The type of the bitwise operator expression is BOOLEAN */
12860 prom_type = boolean_type_node;
12861 else
12862 {
12863 if (!JINTEGRAL_TYPE_P (op1_type))
12864 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12865 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12866 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12867 TREE_TYPE (node) = error_mark_node;
12868 error_found = 1;
12869 /* Insert a break here if adding thing before the switch's
12870 break for this case */
12871 }
12872 break;
12873
12874 /* 15.22 Conditional-And Operator */
12875 case TRUTH_ANDIF_EXPR:
12876 /* 15.23 Conditional-Or Operator */
12877 case TRUTH_ORIF_EXPR:
12878 /* Operands must be of BOOLEAN type */
12879 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12880 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12881 {
12882 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12883 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12884 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12885 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12886 TREE_TYPE (node) = boolean_type_node;
12887 error_found = 1;
12888 break;
12889 }
12890 /* The type of the conditional operators is BOOLEAN */
12891 prom_type = boolean_type_node;
12892 break;
12893
12894 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12895 case LT_EXPR:
12896 case GT_EXPR:
12897 case LE_EXPR:
12898 case GE_EXPR:
12899 /* The type of each of the operands must be a primitive numeric
12900 type */
12901 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12902 {
12903 if (!JNUMERIC_TYPE_P (op1_type))
12904 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12905 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12906 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12907 TREE_TYPE (node) = boolean_type_node;
12908 error_found = 1;
12909 break;
12910 }
12911 /* Binary numeric promotion is performed on the operands */
12912 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12913 /* The type of the relation expression is always BOOLEAN */
12914 prom_type = boolean_type_node;
12915 break;
12916
12917 /* 15.20 Equality Operator */
12918 case EQ_EXPR:
12919 case NE_EXPR:
48a840d9
APB
12920 /* It's time for us to patch the strings. */
12921 if ((cn = patch_string (op1)))
12922 {
12923 op1 = cn;
12924 op1_type = TREE_TYPE (op1);
12925 }
12926 if ((cn = patch_string (op2)))
12927 {
12928 op2 = cn;
12929 op2_type = TREE_TYPE (op2);
12930 }
12931
e04a16fb
AG
12932 /* 15.20.1 Numerical Equality Operators == and != */
12933 /* Binary numeric promotion is performed on the operands */
5e942c50 12934 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
12935 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12936
12937 /* 15.20.2 Boolean Equality Operators == and != */
12938 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12939 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12940 ; /* Nothing to do here */
12941
12942 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
12943 /* Types have to be either references or the null type. If
12944 they're references, it must be possible to convert either
12945 type to the other by casting conversion. */
b9f7e36c
APB
12946 else if (op1 == null_pointer_node || op2 == null_pointer_node
12947 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
12948 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12949 || valid_ref_assignconv_cast_p (op2_type,
12950 op1_type, 1))))
e04a16fb
AG
12951 ; /* Nothing to do here */
12952
12953 /* Else we have an error figure what can't be converted into
12954 what and report the error */
12955 else
12956 {
12957 char *t1;
c2e3db92 12958 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 12959 parse_error_context
781b0558
KG
12960 (wfl_operator,
12961 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12962 operator_string (node), t1,
0a2138e2 12963 lang_printable_name (op2_type, 0));
e04a16fb
AG
12964 free (t1);
12965 TREE_TYPE (node) = boolean_type_node;
12966 error_found = 1;
12967 break;
12968 }
12969 prom_type = boolean_type_node;
12970 break;
12971 }
12972
e04a16fb
AG
12973 if (error_found)
12974 return error_mark_node;
12975
12976 TREE_OPERAND (node, 0) = op1;
12977 TREE_OPERAND (node, 1) = op2;
12978 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
12979 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12980
ce6e9147
APB
12981 if (flag_emit_xref)
12982 return node;
12983
d1472141
PB
12984 /* fold does not respect side-effect order as required for Java but not C.
12985 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12986 * bytecode.
12987 */
12988 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12989 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
12990 node = fold (node);
12991 return node;
e04a16fb
AG
12992}
12993
b67d701b
PB
12994/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12995 zero value, the value of CSTE comes after the valude of STRING */
12996
12997static tree
12998do_merge_string_cste (cste, string, string_len, after)
12999 tree cste;
49f48c71 13000 const char *string;
b67d701b
PB
13001 int string_len, after;
13002{
49f48c71 13003 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13004 int old_len = TREE_STRING_LENGTH (cste);
13005 int len = old_len + string_len;
13006 char *new;
13007
13008 cste = make_node (STRING_CST);
b67d701b 13009 TREE_STRING_LENGTH (cste) = len;
354e99ce
APB
13010 new = TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
13011
b67d701b
PB
13012 if (after)
13013 {
354e99ce
APB
13014 memcpy (new, string, string_len);
13015 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13016 }
13017 else
13018 {
354e99ce
APB
13019 memcpy (new, old, old_len);
13020 memcpy (&new [old_len], string, string_len);
b67d701b 13021 }
354e99ce 13022 new [len] = '\0';
b67d701b
PB
13023 return cste;
13024}
13025
13026/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13027 new STRING_CST on success, NULL_TREE on failure */
13028
13029static tree
13030merge_string_cste (op1, op2, after)
13031 tree op1, op2;
13032 int after;
13033{
13034 /* Handle two string constants right away */
13035 if (TREE_CODE (op2) == STRING_CST)
13036 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13037 TREE_STRING_LENGTH (op2), after);
13038
13039 /* Reasonable integer constant can be treated right away */
13040 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13041 {
49f48c71
KG
13042 static const char *boolean_true = "true";
13043 static const char *boolean_false = "false";
13044 static const char *null_pointer = "null";
b67d701b 13045 char ch[3];
49f48c71 13046 const char *string;
b67d701b
PB
13047
13048 if (op2 == boolean_true_node)
13049 string = boolean_true;
13050 else if (op2 == boolean_false_node)
13051 string = boolean_false;
13052 else if (op2 == null_pointer_node)
13053 string = null_pointer;
13054 else if (TREE_TYPE (op2) == char_type_node)
13055 {
13056 ch[0] = (char )TREE_INT_CST_LOW (op2);
13057 ch[1] = '\0';
13058 string = ch;
13059 }
13060 else
13061 string = print_int_node (op2);
13062
13063 return do_merge_string_cste (op1, string, strlen (string), after);
13064 }
13065 return NULL_TREE;
13066}
13067
13068/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13069 has to be a STRING_CST and the other part must be a STRING_CST or a
13070 INTEGRAL constant. Return a new STRING_CST if the operation
13071 succeed, NULL_TREE otherwise.
13072
13073 If the case we want to optimize for space, we might want to return
13074 NULL_TREE for each invocation of this routine. FIXME */
13075
13076static tree
13077string_constant_concatenation (op1, op2)
13078 tree op1, op2;
13079{
13080 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13081 {
0a2138e2 13082 tree string, rest;
b67d701b
PB
13083 int invert;
13084
13085 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13086 rest = (string == op1 ? op2 : op1);
13087 invert = (string == op1 ? 0 : 1 );
13088
13089 /* Walk REST, only if it looks reasonable */
13090 if (TREE_CODE (rest) != STRING_CST
13091 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13092 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13093 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13094 {
13095 rest = java_complete_tree (rest);
13096 if (rest == error_mark_node)
13097 return error_mark_node;
13098 rest = fold (rest);
13099 }
13100 return merge_string_cste (string, rest, invert);
13101 }
13102 return NULL_TREE;
13103}
13104
13105/* Implement the `+' operator. Does static optimization if possible,
13106 otherwise create (if necessary) and append elements to a
13107 StringBuffer. The StringBuffer will be carried around until it is
13108 used for a function call or an assignment. Then toString() will be
13109 called on it to turn it into a String object. */
13110
13111static tree
13112build_string_concatenation (op1, op2)
13113 tree op1, op2;
13114{
13115 tree result;
dc0b3eff 13116 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13117
13118 if (flag_emit_xref)
13119 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13120
13121 /* Try to do some static optimization */
13122 if ((result = string_constant_concatenation (op1, op2)))
13123 return result;
13124
c0d87ff6
PB
13125 /* Discard empty strings on either side of the expression */
13126 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13127 {
13128 op1 = op2;
13129 op2 = NULL_TREE;
13130 }
c0d87ff6 13131 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13132 op2 = NULL_TREE;
b67d701b 13133
acd663ee 13134 /* If operands are string constant, turn then into object references */
b67d701b
PB
13135 if (TREE_CODE (op1) == STRING_CST)
13136 op1 = patch_string_cst (op1);
acd663ee 13137 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13138 op2 = patch_string_cst (op2);
13139
acd663ee
APB
13140 /* If either one of the constant is null and the other non null
13141 operand is a String object, return it. */
13142 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13143 return op1;
13144
b67d701b
PB
13145 /* If OP1 isn't already a StringBuffer, create and
13146 initialize a new one */
13147 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13148 {
13149 /* Two solutions here:
c52b5771
AG
13150 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13151 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13152 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13153 op1 = BUILD_STRING_BUFFER (op1);
13154 else
13155 {
13156 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13157 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13158 }
13159 }
13160
acd663ee
APB
13161 if (op2)
13162 {
13163 /* OP1 is no longer the last node holding a crafted StringBuffer */
13164 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13165 /* Create a node for `{new...,xxx}.append (op2)' */
13166 if (op2)
13167 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13168 }
13169
b67d701b
PB
13170 /* Mark the last node holding a crafted StringBuffer */
13171 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13172
13173 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13174 return op1;
13175}
13176
13177/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13178 StringBuffer. If no string were found to be patched, return
13179 NULL. */
13180
13181static tree
13182patch_string (node)
13183 tree node;
13184{
1179ebc2
APB
13185 if (node == error_mark_node)
13186 return error_mark_node;
b67d701b
PB
13187 if (TREE_CODE (node) == STRING_CST)
13188 return patch_string_cst (node);
13189 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13190 {
c877974e 13191 int saved = ctxp->explicit_constructor_p;
b67d701b 13192 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13193 tree ret;
13194 /* Temporary disable forbid the use of `this'. */
13195 ctxp->explicit_constructor_p = 0;
13196 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13197 /* String concatenation arguments must be evaluated in order too. */
13198 ret = force_evaluation_order (ret);
c877974e
APB
13199 /* Restore it at its previous value */
13200 ctxp->explicit_constructor_p = saved;
13201 return ret;
b67d701b
PB
13202 }
13203 return NULL_TREE;
13204}
13205
13206/* Build the internal representation of a string constant. */
13207
13208static tree
13209patch_string_cst (node)
13210 tree node;
13211{
13212 int location;
15fdcfe9
PB
13213 if (! flag_emit_class_files)
13214 {
13215 push_obstacks (&permanent_obstack, &permanent_obstack);
13216 node = get_identifier (TREE_STRING_POINTER (node));
13217 location = alloc_name_constant (CONSTANT_String, node);
13218 node = build_ref_from_constant_pool (location);
8226320b 13219 pop_obstacks ();
15fdcfe9 13220 }
cd9643f7 13221 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13222 TREE_CONSTANT (node) = 1;
13223 return node;
13224}
13225
13226/* Build an incomplete unary operator expression. */
e04a16fb
AG
13227
13228static tree
13229build_unaryop (op_token, op_location, op1)
13230 int op_token, op_location;
13231 tree op1;
13232{
13233 enum tree_code op;
13234 tree unaryop;
13235 switch (op_token)
13236 {
b67d701b 13237 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13238 case MINUS_TK: op = NEGATE_EXPR; break;
13239 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13240 case NOT_TK: op = BIT_NOT_EXPR; break;
13241 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13242 op_token);
13243 }
13244
13245 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13246 TREE_SIDE_EFFECTS (unaryop) = 1;
13247 /* Store the location of the operator, for better error report. The
13248 string of the operator will be rebuild based on the OP value. */
13249 EXPR_WFL_LINECOL (unaryop) = op_location;
13250 return unaryop;
13251}
13252
13253/* Special case for the ++/-- operators, since they require an extra
13254 argument to build, which is set to NULL and patched
13255 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13256
13257static tree
13258build_incdec (op_token, op_location, op1, is_post_p)
13259 int op_token, op_location;
13260 tree op1;
13261 int is_post_p;
13262{
13263 static enum tree_code lookup [2][2] =
13264 {
13265 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13266 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13267 };
13268 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13269 NULL_TREE, op1, NULL_TREE);
13270 TREE_SIDE_EFFECTS (node) = 1;
13271 /* Store the location of the operator, for better error report. The
13272 string of the operator will be rebuild based on the OP value. */
13273 EXPR_WFL_LINECOL (node) = op_location;
13274 return node;
13275}
13276
13277/* Build an incomplete cast operator, based on the use of the
13278 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13279 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13280 though its type is already set. */
13281
13282static tree
13283build_cast (location, type, exp)
13284 int location;
13285 tree type, exp;
13286{
13287 tree node = build1 (CONVERT_EXPR, type, exp);
13288 EXPR_WFL_LINECOL (node) = location;
13289 return node;
13290}
13291
c2952b01
APB
13292/* Build an incomplete class reference operator. */
13293static tree
13294build_incomplete_class_ref (location, class_name)
13295 int location;
13296 tree class_name;
13297{
13298 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13299 EXPR_WFL_LINECOL (node) = location;
13300 return node;
13301}
13302
13303/* Complete an incomplete class reference operator. */
13304static tree
13305patch_incomplete_class_ref (node)
13306 tree node;
13307{
13308 tree type = TREE_OPERAND (node, 0);
13309 tree ref_type;
13310
13311 if (!(ref_type = resolve_type_during_patch (type)))
13312 return error_mark_node;
13313
165f37bc 13314 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13315 {
13316 /* A class referenced by `foo.class' is initialized. */
13317 return build_class_init (ref_type, build_class_ref (ref_type));
13318 }
165f37bc
APB
13319
13320 /* If we're emitting class files and we have to deal with non
13321 primitive types, we invoke (and consider generating) the
13322 synthetic static method `class$'. */
13323 if (!TYPE_DOT_CLASS (current_class))
13324 build_dot_class_method (current_class);
f0f3a777 13325 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13326 return java_complete_tree (ref_type);
c2952b01
APB
13327}
13328
e04a16fb
AG
13329/* 15.14 Unary operators. We return error_mark_node in case of error,
13330 but preserve the type of NODE if the type is fixed. */
13331
13332static tree
13333patch_unaryop (node, wfl_op)
13334 tree node;
13335 tree wfl_op;
13336{
13337 tree op = TREE_OPERAND (node, 0);
13338 tree op_type = TREE_TYPE (op);
ab3a6dd6 13339 tree prom_type = NULL_TREE, value, decl;
c2952b01 13340 int outer_field_flag = 0;
e04a16fb
AG
13341 int code = TREE_CODE (node);
13342 int error_found = 0;
13343
13344 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13345
13346 switch (code)
13347 {
13348 /* 15.13.2 Postfix Increment Operator ++ */
13349 case POSTINCREMENT_EXPR:
13350 /* 15.13.3 Postfix Increment Operator -- */
13351 case POSTDECREMENT_EXPR:
13352 /* 15.14.1 Prefix Increment Operator ++ */
13353 case PREINCREMENT_EXPR:
13354 /* 15.14.2 Prefix Decrement Operator -- */
13355 case PREDECREMENT_EXPR:
5cbdba64 13356 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13357 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13358 /* We might be trying to change an outer field accessed using
13359 access method. */
13360 if (outer_field_flag)
13361 {
13362 /* Retrieve the decl of the field we're trying to access. We
13363 do that by first retrieving the function we would call to
13364 access the field. It has been already verified that this
13365 field isn't final */
13366 if (flag_emit_class_files)
13367 decl = TREE_OPERAND (op, 0);
13368 else
13369 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13370 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13371 }
b3edebcf 13372 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13373 else if (!JDECL_P (decl)
b3edebcf
APB
13374 && TREE_CODE (decl) != COMPONENT_REF
13375 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13376 && TREE_CODE (decl) != INDIRECT_REF
13377 && !(TREE_CODE (decl) == COMPOUND_EXPR
13378 && TREE_OPERAND (decl, 1)
13379 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13380 {
5e942c50
APB
13381 tree lvalue;
13382 /* Before screaming, check that we're not in fact trying to
13383 increment a optimized static final access, in which case
13384 we issue an different error message. */
13385 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13386 && resolve_expression_name (wfl_op, &lvalue)
13387 && check_final_assignment (lvalue, wfl_op)))
13388 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13389 operator_string (node));
e04a16fb
AG
13390 TREE_TYPE (node) = error_mark_node;
13391 error_found = 1;
13392 }
c2952b01
APB
13393
13394 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13395 error_found = 1;
13396
e04a16fb
AG
13397 /* From now on, we know that op if a variable and that it has a
13398 valid wfl. We use wfl_op to locate errors related to the
13399 ++/-- operand. */
13400 else if (!JNUMERIC_TYPE_P (op_type))
13401 {
13402 parse_error_context
13403 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13404 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13405 TREE_TYPE (node) = error_mark_node;
13406 error_found = 1;
13407 }
13408 else
13409 {
4a5f66c3 13410 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13411 both operands, if really necessary */
13412 if (JINTEGRAL_TYPE_P (op_type))
13413 {
13414 value = build_int_2 (1, 0);
13415 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13416 }
13417 else
13418 {
13419 value = build_int_2 (1, 0);
13420 TREE_TYPE (node) =
13421 binary_numeric_promotion (op_type,
13422 TREE_TYPE (value), &op, &value);
13423 }
c2952b01
APB
13424
13425 /* We remember we might be accessing an outer field */
13426 if (outer_field_flag)
13427 {
13428 /* We re-generate an access to the field */
13429 value = build (PLUS_EXPR, TREE_TYPE (op),
13430 build_outer_field_access (wfl_op, decl), value);
13431
13432 /* And we patch the original access$() into a write
13433 with plus_op as a rhs */
13434 return outer_field_access_fix (node, op, value);
13435 }
13436
5cbdba64 13437 /* And write back into the node. */
4a5f66c3 13438 TREE_OPERAND (node, 0) = op;
e04a16fb 13439 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13440 /* Convert the overall back into its original type, if
13441 necessary, and return */
13442 if (JINTEGRAL_TYPE_P (op_type))
13443 return fold (node);
13444 else
13445 return fold (convert (op_type, node));
e04a16fb
AG
13446 }
13447 break;
13448
13449 /* 15.14.3 Unary Plus Operator + */
b67d701b 13450 case UNARY_PLUS_EXPR:
e04a16fb
AG
13451 /* 15.14.4 Unary Minus Operator - */
13452 case NEGATE_EXPR:
13453 if (!JNUMERIC_TYPE_P (op_type))
13454 {
13455 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13456 TREE_TYPE (node) = error_mark_node;
13457 error_found = 1;
13458 }
13459 /* Unary numeric promotion is performed on operand */
13460 else
13461 {
15fdcfe9
PB
13462 op = do_unary_numeric_promotion (op);
13463 prom_type = TREE_TYPE (op);
b67d701b 13464 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13465 return fold (op);
e04a16fb
AG
13466 }
13467 break;
13468
13469 /* 15.14.5 Bitwise Complement Operator ~ */
13470 case BIT_NOT_EXPR:
13471 if (!JINTEGRAL_TYPE_P (op_type))
13472 {
13473 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13474 TREE_TYPE (node) = error_mark_node;
13475 error_found = 1;
13476 }
13477 else
13478 {
15fdcfe9
PB
13479 op = do_unary_numeric_promotion (op);
13480 prom_type = TREE_TYPE (op);
e04a16fb
AG
13481 }
13482 break;
13483
13484 /* 15.14.6 Logical Complement Operator ! */
13485 case TRUTH_NOT_EXPR:
13486 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13487 {
13488 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13489 /* But the type is known. We will report an error if further
13490 attempt of a assignment is made with this rhs */
e04a16fb
AG
13491 TREE_TYPE (node) = boolean_type_node;
13492 error_found = 1;
13493 }
13494 else
13495 prom_type = boolean_type_node;
13496 break;
13497
13498 /* 15.15 Cast Expression */
13499 case CONVERT_EXPR:
0a2138e2 13500 value = patch_cast (node, wfl_operator);
e04a16fb 13501 if (value == error_mark_node)
c877974e
APB
13502 {
13503 /* If this cast is part of an assignment, we tell the code
13504 that deals with it not to complain about a mismatch,
13505 because things have been cast, anyways */
13506 TREE_TYPE (node) = error_mark_node;
13507 error_found = 1;
13508 }
13509 else
dc0b3eff
PB
13510 {
13511 value = fold (value);
13512 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13513 return value;
13514 }
e04a16fb
AG
13515 break;
13516 }
13517
e04a16fb
AG
13518 if (error_found)
13519 return error_mark_node;
4a5f66c3
APB
13520
13521 /* There are cases where node has been replaced by something else
13522 and we don't end up returning here: UNARY_PLUS_EXPR,
13523 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13524 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13525 TREE_TYPE (node) = prom_type;
dc0b3eff 13526 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13527 return fold (node);
13528}
13529
13530/* Generic type resolution that sometimes takes place during node
13531 patching. Returned the resolved type or generate an error
13532 message. Return the resolved type or NULL_TREE. */
13533
13534static tree
13535resolve_type_during_patch (type)
13536 tree type;
13537{
13538 if (unresolved_type_p (type, NULL))
13539 {
4142b247 13540 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13541 if (!type_decl)
13542 {
13543 parse_error_context (type,
13544 "Class `%s' not found in type declaration",
13545 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13546 return NULL_TREE;
13547 }
13548 else
5e942c50
APB
13549 {
13550 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13551 return TREE_TYPE (type_decl);
13552 }
e04a16fb
AG
13553 }
13554 return type;
13555}
13556/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13557 found. Otherwise NODE or something meant to replace it is returned. */
13558
13559static tree
19e223db 13560patch_cast (node, wfl_op)
e04a16fb 13561 tree node;
19e223db 13562 tree wfl_op;
e04a16fb
AG
13563{
13564 tree op = TREE_OPERAND (node, 0);
13565 tree op_type = TREE_TYPE (op);
13566 tree cast_type = TREE_TYPE (node);
13567 char *t1;
13568
13569 /* First resolve OP_TYPE if unresolved */
13570 if (!(cast_type = resolve_type_during_patch (cast_type)))
13571 return error_mark_node;
13572
13573 /* Check on cast that are proven correct at compile time */
13574 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13575 {
e04a16fb
AG
13576 /* Same type */
13577 if (cast_type == op_type)
13578 return node;
13579
0b4d333e
APB
13580 /* float and double type are converted to the original type main
13581 variant and then to the target type. */
13582 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13583 op = convert (integer_type_node, op);
13584
e04a16fb
AG
13585 /* Try widening/narowwing convertion. Potentially, things need
13586 to be worked out in gcc so we implement the extreme cases
13587 correctly. fold_convert() needs to be fixed. */
13588 return convert (cast_type, op);
13589 }
13590
0b4d333e
APB
13591 /* It's also valid to cast a boolean into a boolean */
13592 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13593 return node;
13594
5e942c50
APB
13595 /* null can be casted to references */
13596 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13597 return build_null_of_type (cast_type);
13598
e04a16fb
AG
13599 /* The remaining legal casts involve conversion between reference
13600 types. Check for their compile time correctness. */
13601 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13602 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13603 {
13604 TREE_TYPE (node) = promote_type (cast_type);
13605 /* Now, the case can be determined correct at compile time if
13606 OP_TYPE can be converted into CAST_TYPE by assignment
13607 conversion (5.2) */
13608
13609 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13610 {
13611 TREE_SET_CODE (node, NOP_EXPR);
13612 return node;
13613 }
13614
13615 if (flag_emit_class_files)
13616 {
13617 TREE_SET_CODE (node, CONVERT_EXPR);
13618 return node;
13619 }
e04a16fb
AG
13620
13621 /* The cast requires a run-time check */
13622 return build (CALL_EXPR, promote_type (cast_type),
13623 build_address_of (soft_checkcast_node),
13624 tree_cons (NULL_TREE, build_class_ref (cast_type),
13625 build_tree_list (NULL_TREE, op)),
13626 NULL_TREE);
13627 }
13628
13629 /* Any other casts are proven incorrect at compile time */
c2e3db92 13630 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 13631 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 13632 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13633 free (t1);
13634 return error_mark_node;
13635}
13636
5e942c50
APB
13637/* Build a null constant and give it the type TYPE. */
13638
13639static tree
13640build_null_of_type (type)
13641 tree type;
13642{
13643 tree node = build_int_2 (0, 0);
13644 TREE_TYPE (node) = promote_type (type);
13645 return node;
13646}
13647
e04a16fb
AG
13648/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13649 a list of indices. */
13650static tree
13651build_array_ref (location, array, index)
13652 int location;
13653 tree array, index;
13654{
13655 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13656 EXPR_WFL_LINECOL (node) = location;
13657 return node;
13658}
13659
13660/* 15.12 Array Access Expression */
13661
13662static tree
c877974e
APB
13663patch_array_ref (node)
13664 tree node;
e04a16fb
AG
13665{
13666 tree array = TREE_OPERAND (node, 0);
13667 tree array_type = TREE_TYPE (array);
13668 tree index = TREE_OPERAND (node, 1);
13669 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13670 int error_found = 0;
13671
13672 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13673
e04a16fb
AG
13674 if (TREE_CODE (array_type) == POINTER_TYPE)
13675 array_type = TREE_TYPE (array_type);
13676
13677 /* The array reference must be an array */
13678 if (!TYPE_ARRAY_P (array_type))
13679 {
13680 parse_error_context
781b0558
KG
13681 (wfl_operator,
13682 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13683 lang_printable_name (array_type, 0));
e04a16fb
AG
13684 TREE_TYPE (node) = error_mark_node;
13685 error_found = 1;
13686 }
13687
c2952b01 13688 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13689 type must be int */
15fdcfe9
PB
13690 index = do_unary_numeric_promotion (index);
13691 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13692 {
1ebadc60 13693 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13694 parse_error_context (wfl_operator,
13695 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13696 lang_printable_name (index_type, 0));
13697 else
781b0558
KG
13698 parse_error_context (wfl_operator,
13699 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13700 lang_printable_name (index_type, 0));
e04a16fb
AG
13701 TREE_TYPE (node) = error_mark_node;
13702 error_found = 1;
13703 }
13704
e04a16fb
AG
13705 if (error_found)
13706 return error_mark_node;
e04a16fb 13707
5e942c50 13708 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13709
7f1d4866 13710 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13711 {
15fdcfe9
PB
13712 TREE_OPERAND (node, 0) = array;
13713 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13714 }
13715 else
939d7216
PB
13716 {
13717 /* The save_expr is for correct evaluation order. It would be cleaner
13718 to use force_evaluation_order (see comment there), but that is
13719 difficult when we also have to deal with bounds checking. */
13720 if (TREE_SIDE_EFFECTS (index))
13721 array = save_expr (array);
13722 node = build_java_arrayaccess (array, array_type, index);
13723 if (TREE_SIDE_EFFECTS (index))
13724 node = build (COMPOUND_EXPR, array_type, array, node);
13725 }
e04a16fb
AG
13726 TREE_TYPE (node) = array_type;
13727 return node;
13728}
13729
13730/* 15.9 Array Creation Expressions */
13731
13732static tree
13733build_newarray_node (type, dims, extra_dims)
13734 tree type;
13735 tree dims;
13736 int extra_dims;
13737{
13738 tree node =
b67d701b 13739 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13740 build_int_2 (extra_dims, 0));
e04a16fb
AG
13741 return node;
13742}
13743
13744static tree
13745patch_newarray (node)
13746 tree node;
13747{
13748 tree type = TREE_OPERAND (node, 0);
13749 tree dims = TREE_OPERAND (node, 1);
13750 tree cdim, array_type;
13751 int error_found = 0;
13752 int ndims = 0;
13753 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13754
13755 /* Dimension types are verified. It's better for the types to be
13756 verified in order. */
13757 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13758 {
13759 int dim_error = 0;
13760 tree dim = TREE_VALUE (cdim);
13761
13762 /* Dim might have been saved during its evaluation */
13763 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13764
13765 /* The type of each specified dimension must be an integral type. */
13766 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13767 dim_error = 1;
13768
13769 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13770 promoted type must be int. */
13771 else
13772 {
15fdcfe9 13773 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13774 if (TREE_TYPE (dim) != int_type_node)
13775 dim_error = 1;
13776 }
13777
13778 /* Report errors on types here */
13779 if (dim_error)
13780 {
13781 parse_error_context
13782 (TREE_PURPOSE (cdim),
781b0558 13783 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13784 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13785 "Explicit cast needed to" : "Can't"),
0a2138e2 13786 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13787 error_found = 1;
13788 }
13789
e04a16fb
AG
13790 TREE_PURPOSE (cdim) = NULL_TREE;
13791 }
13792
13793 /* Resolve array base type if unresolved */
13794 if (!(type = resolve_type_during_patch (type)))
13795 error_found = 1;
13796
13797 if (error_found)
13798 {
13799 /* We don't want further evaluation of this bogus array creation
13800 operation */
13801 TREE_TYPE (node) = error_mark_node;
13802 return error_mark_node;
13803 }
13804
15fdcfe9
PB
13805 /* Set array_type to the actual (promoted) array type of the result. */
13806 if (TREE_CODE (type) == RECORD_TYPE)
13807 type = build_pointer_type (type);
13808 while (--xdims >= 0)
13809 {
13810 type = promote_type (build_java_array_type (type, -1));
13811 }
13812 dims = nreverse (dims);
13813 array_type = type;
13814 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13815 {
13816 type = array_type;
05bccae2
RK
13817 array_type
13818 = build_java_array_type (type,
13819 TREE_CODE (cdim) == INTEGER_CST
13820 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13821 : -1);
15fdcfe9
PB
13822 array_type = promote_type (array_type);
13823 }
13824 dims = nreverse (dims);
13825
e04a16fb
AG
13826 /* The node is transformed into a function call. Things are done
13827 differently according to the number of dimensions. If the number
13828 of dimension is equal to 1, then the nature of the base type
13829 (primitive or not) matters. */
15fdcfe9 13830 if (ndims == 1)
fdec99c6 13831 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13832
e04a16fb
AG
13833 /* Can't reuse what's already written in expr.c because it uses the
13834 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13835 return build (CALL_EXPR, array_type,
e04a16fb 13836 build_address_of (soft_multianewarray_node),
15fdcfe9 13837 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13838 tree_cons (NULL_TREE,
15fdcfe9 13839 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13840 NULL_TREE);
13841}
13842
f8976021
APB
13843/* 10.6 Array initializer. */
13844
13845/* Build a wfl for array element that don't have one, so we can
13846 pin-point errors. */
13847
13848static tree
13849maybe_build_array_element_wfl (node)
13850 tree node;
13851{
13852 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13853 return build_expr_wfl (NULL_TREE, ctxp->filename,
13854 ctxp->elc.line, ctxp->elc.prev_col);
13855 else
13856 return NULL_TREE;
13857}
13858
13859/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13860 identification of initialized arrays easier to detect during walk
13861 and expansion. */
13862
13863static tree
13864build_new_array_init (location, values)
13865 int location;
13866 tree values;
13867{
13868 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13869 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13870 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13871 return to_return;
13872}
13873
13874/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13875 occurred. Otherwise return NODE after having set its type
13876 appropriately. */
13877
13878static tree
13879patch_new_array_init (type, node)
13880 tree type, node;
f8976021
APB
13881{
13882 int error_seen = 0;
fdec99c6 13883 tree current, element_type;
f8976021 13884 HOST_WIDE_INT length;
fdec99c6
PB
13885 int all_constant = 1;
13886 tree init = TREE_OPERAND (node, 0);
f8976021 13887
fdec99c6
PB
13888 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13889 {
13890 parse_error_context (node,
13891 "Invalid array initializer for non-array type `%s'",
13892 lang_printable_name (type, 1));
13893 return error_mark_node;
13894 }
13895 type = TREE_TYPE (type);
13896 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13897
fdec99c6
PB
13898 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13899
13900 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13901 current; length++, current = TREE_CHAIN (current))
f8976021 13902 {
fdec99c6
PB
13903 tree elt = TREE_VALUE (current);
13904 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13905 {
fdec99c6 13906 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13907 elt = TREE_VALUE (current);
13908 /* When compiling to native code, STRING_CST is converted to
13909 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13910 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13911 all_constant = 0;
f8976021 13912 }
fdec99c6
PB
13913 else
13914 {
13915 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13916 TREE_PURPOSE (current) = NULL_TREE;
13917 all_constant = 0;
13918 }
9a7ab4b3
APB
13919 if (elt && TREE_CODE (elt) == TREE_LIST
13920 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 13921 error_seen = 1;
f8976021
APB
13922 }
13923
13924 if (error_seen)
13925 return error_mark_node;
13926
13927 /* Create a new type. We can't reuse the one we have here by
13928 patching its dimension because it originally is of dimension -1
13929 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13930 type = build_java_array_type (element_type, length);
13931 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13932 TREE_TYPE (node) = promote_type (type);
13933 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13934 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13935 return node;
13936}
13937
13938/* Verify that one entry of the initializer element list can be
13939 assigned to the array base type. Report 1 if an error occurred, 0
13940 otherwise. */
13941
13942static int
13943array_constructor_check_entry (type, entry)
13944 tree type, entry;
13945{
13946 char *array_type_string = NULL; /* For error reports */
13947 tree value, type_value, new_value, wfl_value, patched;
13948 int error_seen = 0;
13949
13950 new_value = NULL_TREE;
13951 wfl_value = TREE_VALUE (entry);
13952
100f7cd8 13953 push_obstacks (&permanent_obstack, &permanent_obstack);
f8976021 13954 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 13955 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
13956 if ((patched = patch_string (value)))
13957 value = patched;
1179ebc2
APB
13958 if (value == error_mark_node)
13959 return 1;
f8976021 13960
f8976021
APB
13961 type_value = TREE_TYPE (value);
13962
1179ebc2 13963 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
13964 constant overflow during narrowing. */
13965 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13966 new_value = try_builtin_assignconv (wfl_operator, type, value);
13967 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13968 type_value = promote_type (type);
100f7cd8
APB
13969
13970 pop_obstacks ();
f8976021
APB
13971 /* Check and report errors */
13972 if (!new_value)
13973 {
49f48c71 13974 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
13975 "Can't" : "Explicit cast needed to");
13976 if (!array_type_string)
c2e3db92 13977 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
13978 parse_error_context
13979 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13980 msg, lang_printable_name (type_value, 1), array_type_string);
13981 error_seen = 1;
13982 }
13983
13984 if (new_value)
13985 {
b8c5b1c6 13986 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
13987 TREE_VALUE (entry) = new_value;
13988 }
13989
13990 if (array_type_string)
13991 free (array_type_string);
13992
13993 TREE_PURPOSE (entry) = NULL_TREE;
13994 return error_seen;
13995}
13996
e04a16fb
AG
13997static tree
13998build_this (location)
13999 int location;
14000{
9ee9b555 14001 tree node = build_wfl_node (this_identifier_node);
b67d701b 14002 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14003 EXPR_WFL_LINECOL (node) = location;
14004 return node;
14005}
14006
14007/* 14.15 The return statement. It builds a modify expression that
14008 assigns the returned value to the RESULT_DECL that hold the value
14009 to be returned. */
14010
14011static tree
14012build_return (location, op)
14013 int location;
14014 tree op;
14015{
14016 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14017 EXPR_WFL_LINECOL (node) = location;
b67d701b 14018 node = build_debugable_stmt (location, node);
e04a16fb
AG
14019 return node;
14020}
14021
14022static tree
14023patch_return (node)
14024 tree node;
14025{
14026 tree return_exp = TREE_OPERAND (node, 0);
14027 tree meth = current_function_decl;
14028 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14029 int error_found = 0;
14030
14031 TREE_TYPE (node) = error_mark_node;
14032 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14033
14034 /* It's invalid to have a return value within a function that is
14035 declared with the keyword void or that is a constructor */
14036 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14037 error_found = 1;
14038
f099f336 14039 /* It's invalid to use a return statement in a static block */
c2952b01 14040 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14041 error_found = 1;
14042
e04a16fb
AG
14043 /* It's invalid to have a no return value within a function that
14044 isn't declared with the keyword `void' */
14045 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14046 error_found = 2;
c2952b01
APB
14047
14048 if (in_instance_initializer)
14049 error_found = 1;
e04a16fb
AG
14050
14051 if (error_found)
14052 {
c2952b01 14053 if (in_instance_initializer)
f099f336 14054 parse_error_context (wfl_operator,
c2952b01
APB
14055 "`return' inside instance initializer");
14056
14057 else if (DECL_CLINIT_P (current_function_decl))
14058 parse_error_context (wfl_operator,
14059 "`return' inside static initializer");
f099f336
APB
14060
14061 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14062 {
c2e3db92 14063 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14064 parse_error_context (wfl_operator,
14065 "`return' with%s value from `%s %s'",
14066 (error_found == 1 ? "" : "out"),
14067 t, lang_printable_name (meth, 0));
14068 free (t);
14069 }
14070 else
14071 parse_error_context (wfl_operator,
14072 "`return' with value from constructor `%s'",
14073 lang_printable_name (meth, 0));
e04a16fb
AG
14074 return error_mark_node;
14075 }
14076
5e942c50
APB
14077 /* If we have a return_exp, build a modify expression and expand
14078 it. Note: at that point, the assignment is declared valid, but we
14079 may want to carry some more hacks */
e04a16fb
AG
14080 if (return_exp)
14081 {
5e942c50
APB
14082 tree exp = java_complete_tree (return_exp);
14083 tree modify, patched;
14084
14085 /* If the function returned value and EXP are booleans, EXP has
14086 to be converted into the type of DECL_RESULT, which is integer
14087 (see complete_start_java_method) */
14088 if (TREE_TYPE (exp) == boolean_type_node &&
14089 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14090 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14091
14092 /* `null' can be assigned to a function returning a reference */
14093 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14094 exp == null_pointer_node)
14095 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14096
14097 if ((patched = patch_string (exp)))
14098 exp = patched;
14099
14100 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14101 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14102 modify = java_complete_tree (modify);
5e942c50 14103
e04a16fb
AG
14104 if (modify != error_mark_node)
14105 {
14106 TREE_SIDE_EFFECTS (modify) = 1;
14107 TREE_OPERAND (node, 0) = modify;
14108 }
14109 else
14110 return error_mark_node;
14111 }
14112 TREE_TYPE (node) = void_type_node;
14113 TREE_SIDE_EFFECTS (node) = 1;
14114 return node;
14115}
14116
14117/* 14.8 The if Statement */
14118
14119static tree
14120build_if_else_statement (location, expression, if_body, else_body)
14121 int location;
14122 tree expression, if_body, else_body;
14123{
14124 tree node;
e04a16fb 14125 if (!else_body)
9bbc7d9f 14126 else_body = empty_stmt_node;
e04a16fb
AG
14127 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14128 EXPR_WFL_LINECOL (node) = location;
b67d701b 14129 node = build_debugable_stmt (location, node);
e04a16fb
AG
14130 return node;
14131}
14132
14133static tree
14134patch_if_else_statement (node)
14135 tree node;
14136{
14137 tree expression = TREE_OPERAND (node, 0);
14138
14139 TREE_TYPE (node) = error_mark_node;
14140 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14141
14142 /* The type of expression must be boolean */
b67d701b
PB
14143 if (TREE_TYPE (expression) != boolean_type_node
14144 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14145 {
14146 parse_error_context
14147 (wfl_operator,
14148 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14149 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14150 return error_mark_node;
14151 }
14152
14153 TREE_TYPE (node) = void_type_node;
14154 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14155 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14156 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14157 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14158 return node;
14159}
14160
14161/* 14.6 Labeled Statements */
14162
14163/* Action taken when a lableled statement is parsed. a new
14164 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14165 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14166
14167static tree
0a2138e2 14168build_labeled_block (location, label)
e04a16fb 14169 int location;
0a2138e2 14170 tree label;
e04a16fb 14171{
b635eb2f 14172 tree label_name ;
e04a16fb 14173 tree label_decl, node;
b635eb2f
PB
14174 if (label == NULL_TREE || label == continue_identifier_node)
14175 label_name = label;
14176 else
e04a16fb 14177 {
b635eb2f
PB
14178 label_name = merge_qualified_name (label_id, label);
14179 /* Issue an error if we try to reuse a label that was previously
14180 declared */
14181 if (IDENTIFIER_LOCAL_VALUE (label_name))
14182 {
14183 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14184 parse_error_context (wfl_operator,
14185 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14186 IDENTIFIER_POINTER (label));
14187 EXPR_WFL_LINECOL (wfl_operator) =
14188 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14189 parse_error_context (wfl_operator,
14190 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14191 IDENTIFIER_POINTER (label));
14192 java_error_count--;
14193 }
e04a16fb
AG
14194 }
14195
14196 label_decl = create_label_decl (label_name);
14197 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14198 EXPR_WFL_LINECOL (node) = location;
14199 TREE_SIDE_EFFECTS (node) = 1;
14200 return node;
14201}
14202
b67d701b 14203/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14204
14205static tree
b635eb2f 14206finish_labeled_statement (lbe, statement)
e04a16fb
AG
14207 tree lbe; /* Labeled block expr */
14208 tree statement;
14209{
14210 /* In anyways, tie the loop to its statement */
14211 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14212 pop_labeled_block ();
14213 POP_LABELED_BLOCK ();
e04a16fb
AG
14214 return lbe;
14215}
14216
14217/* 14.10, 14.11, 14.12 Loop Statements */
14218
14219/* Create an empty LOOP_EXPR and make it the last in the nested loop
14220 list. */
14221
14222static tree
14223build_new_loop (loop_body)
14224 tree loop_body;
14225{
14226 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14227 TREE_SIDE_EFFECTS (loop) = 1;
14228 PUSH_LOOP (loop);
14229 return loop;
14230}
14231
14232/* Create a loop body according to the following structure:
14233 COMPOUND_EXPR
14234 COMPOUND_EXPR (loop main body)
14235 EXIT_EXPR (this order is for while/for loops.
14236 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14237 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14238 BODY end of this labeled block)
14239 INCREMENT (if any)
14240
14241 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14242 after the body, like in the do-while loop.
14243
14244 To obtain a loop, the loop body structure described above is
14245 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14246
14247 LABELED_BLOCK_EXPR
14248 LABEL_DECL (use this label to exit the loop)
14249 LOOP_EXPR
14250 <structure described above> */
e04a16fb
AG
14251
14252static tree
14253build_loop_body (location, condition, reversed)
14254 int location;
14255 tree condition;
14256 int reversed;
14257{
0a2138e2 14258 tree first, second, body;
e04a16fb
AG
14259
14260 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14261 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14262 condition = build_debugable_stmt (location, condition);
14263 TREE_SIDE_EFFECTS (condition) = 1;
14264
b635eb2f 14265 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14266 first = (reversed ? body : condition);
14267 second = (reversed ? condition : body);
14268 return
14269 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14270 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14271}
14272
14273/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14274 their order) on the current loop. Unlink the current loop from the
14275 loop list. */
14276
14277static tree
b635eb2f 14278finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14279 int location;
14280 tree condition, body;
14281 int reversed;
14282{
14283 tree to_return = ctxp->current_loop;
14284 tree loop_body = LOOP_EXPR_BODY (to_return);
14285 if (condition)
14286 {
14287 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14288 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14289 The real EXIT_EXPR is one operand further. */
14290 EXPR_WFL_LINECOL (cnode) = location;
14291 /* This one is for accurate error reports */
14292 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14293 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14294 }
14295 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14296 POP_LOOP ();
14297 return to_return;
14298}
14299
b635eb2f 14300/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14301 loops feature the condition part */
14302
14303static tree
b635eb2f 14304finish_for_loop (location, condition, update, body)
e04a16fb
AG
14305 int location;
14306 tree condition, update, body;
14307{
14308 /* Put the condition and the loop body in place */
b635eb2f 14309 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14310 /* LOOP is the current loop which has been now popped of the loop
14311 stack. Install the update block */
14312 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14313 return loop;
14314}
14315
5cbdba64
APB
14316/* Try to find the loop a block might be related to. This comprises
14317 the case where the LOOP_EXPR is found as the second operand of a
14318 COMPOUND_EXPR, because the loop happens to have an initialization
14319 part, then expressed as the first operand of the COMPOUND_EXPR. If
14320 the search finds something, 1 is returned. Otherwise, 0 is
14321 returned. The search is assumed to start from a
14322 LABELED_BLOCK_EXPR's block. */
14323
14324static tree
14325search_loop (statement)
14326 tree statement;
14327{
14328 if (TREE_CODE (statement) == LOOP_EXPR)
14329 return statement;
14330
14331 if (TREE_CODE (statement) == BLOCK)
14332 statement = BLOCK_SUBBLOCKS (statement);
14333 else
14334 return NULL_TREE;
14335
14336 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14337 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14338 statement = TREE_OPERAND (statement, 1);
14339
14340 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14341 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14342}
14343
14344/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14345 returned otherwise. */
14346
14347static int
14348labeled_block_contains_loop_p (block, loop)
14349 tree block, loop;
14350{
14351 if (!block)
14352 return 0;
14353
14354 if (LABELED_BLOCK_BODY (block) == loop)
14355 return 1;
14356
c2952b01 14357 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14358 return 1;
14359
14360 return 0;
14361}
14362
e04a16fb 14363/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14364 insert LOOP as its body. */
e04a16fb
AG
14365
14366static tree
14367patch_loop_statement (loop)
14368 tree loop;
14369{
cd9643f7 14370 tree loop_label;
5cbdba64 14371
cd9643f7 14372 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14373 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14374 return loop;
14375
cd9643f7 14376 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14377 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14378 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14379 LABELED_BLOCK_BODY (loop_label) = loop;
14380 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14381 return loop_label;
e04a16fb
AG
14382}
14383
14384/* 14.13, 14.14: break and continue Statements */
14385
14386/* Build a break or a continue statement. a null NAME indicates an
14387 unlabeled break/continue statement. */
14388
14389static tree
14390build_bc_statement (location, is_break, name)
14391 int location, is_break;
14392 tree name;
14393{
14394 tree break_continue, label_block_expr = NULL_TREE;
14395
14396 if (name)
14397 {
14398 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14399 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14400 /* Null means that we don't have a target for this named
14401 break/continue. In this case, we make the target to be the
14402 label name, so that the error can be reported accuratly in
14403 patch_bc_statement. */
14404 label_block_expr = EXPR_WFL_NODE (name);
14405 }
14406 /* Unlabeled break/continue will be handled during the
14407 break/continue patch operation */
14408 break_continue
14409 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14410
14411 IS_BREAK_STMT_P (break_continue) = is_break;
14412 TREE_SIDE_EFFECTS (break_continue) = 1;
14413 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14414 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14415 return break_continue;
14416}
14417
14418/* Verification of a break/continue statement. */
14419
14420static tree
14421patch_bc_statement (node)
14422 tree node;
14423{
14424 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14425 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14426 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14427
e04a16fb 14428 /* Having an identifier here means that the target is unknown. */
b635eb2f 14429 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14430 {
14431 parse_error_context (wfl_operator, "No label definition found for `%s'",
14432 IDENTIFIER_POINTER (bc_label));
14433 return error_mark_node;
14434 }
b635eb2f 14435 if (! IS_BREAK_STMT_P (node))
e04a16fb 14436 {
b635eb2f
PB
14437 /* It's a continue statement. */
14438 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14439 {
b635eb2f
PB
14440 if (labeled_block == NULL_TREE)
14441 {
14442 if (bc_label == NULL_TREE)
14443 parse_error_context (wfl_operator,
14444 "`continue' must be in loop");
14445 else
1504b2b4
APB
14446 parse_error_context
14447 (wfl_operator, "continue label `%s' does not name a loop",
14448 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14449 return error_mark_node;
14450 }
14451 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14452 == continue_identifier_node)
14453 && (bc_label == NULL_TREE
14454 || TREE_CHAIN (labeled_block) == bc_label))
14455 {
14456 bc_label = labeled_block;
14457 break;
14458 }
e04a16fb 14459 }
e04a16fb 14460 }
b635eb2f 14461 else if (!bc_label)
34f4db93 14462 {
b635eb2f 14463 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14464 {
b635eb2f
PB
14465 if (labeled_block == NULL_TREE)
14466 {
14467 parse_error_context (wfl_operator,
14468 "`break' must be in loop or switch");
14469 return error_mark_node;
14470 }
14471 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14472 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14473 || search_loop (target_stmt))
b635eb2f
PB
14474 {
14475 bc_label = labeled_block;
14476 break;
14477 }
e04a16fb 14478 }
e04a16fb
AG
14479 }
14480
b635eb2f 14481 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14482 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14483
e04a16fb
AG
14484 /* Our break/continue don't return values. */
14485 TREE_TYPE (node) = void_type_node;
14486 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14487 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14488 sometimes, like after a if statement) */
14489 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14490 TREE_SIDE_EFFECTS (node) = 1;
14491 return node;
14492}
14493
14494/* Process the exit expression belonging to a loop. Its type must be
14495 boolean. */
14496
14497static tree
14498patch_exit_expr (node)
14499 tree node;
14500{
14501 tree expression = TREE_OPERAND (node, 0);
14502 TREE_TYPE (node) = error_mark_node;
14503 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14504
14505 /* The type of expression must be boolean */
14506 if (TREE_TYPE (expression) != boolean_type_node)
14507 {
14508 parse_error_context
14509 (wfl_operator,
781b0558 14510 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14511 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14512 return error_mark_node;
14513 }
14514 /* Now we know things are allright, invert the condition, fold and
14515 return */
14516 TREE_OPERAND (node, 0) =
14517 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14518
14519 if (! integer_zerop (TREE_OPERAND (node, 0))
14520 && ctxp->current_loop != NULL_TREE
14521 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14522 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14523 if (! integer_onep (TREE_OPERAND (node, 0)))
14524 CAN_COMPLETE_NORMALLY (node) = 1;
14525
14526
e04a16fb
AG
14527 TREE_TYPE (node) = void_type_node;
14528 return node;
14529}
b67d701b
PB
14530
14531/* 14.9 Switch statement */
14532
14533static tree
14534patch_switch_statement (node)
14535 tree node;
14536{
c877974e 14537 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14538
14539 /* Complete the switch expression */
14540 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14541 se_type = TREE_TYPE (se);
14542 /* The type of the switch expression must be char, byte, short or
14543 int */
2e0f0aff 14544 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14545 {
14546 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14547 parse_error_context (wfl_operator,
14548 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14549 lang_printable_name (se_type, 0));
b67d701b
PB
14550 /* This is what java_complete_tree will check */
14551 TREE_OPERAND (node, 0) = error_mark_node;
14552 return error_mark_node;
14553 }
14554
15fdcfe9 14555 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14556
14557 /* Ready to return */
15fdcfe9 14558 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14559 {
14560 TREE_TYPE (node) = error_mark_node;
14561 return error_mark_node;
14562 }
14563 TREE_TYPE (node) = void_type_node;
14564 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14565 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14566 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14567 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14568 return node;
14569}
14570
165f37bc 14571/* 14.18 The try/catch statements */
b67d701b 14572
b67d701b 14573static tree
a7d8d81f 14574build_try_statement (location, try_block, catches)
b67d701b 14575 int location;
a7d8d81f
PB
14576 tree try_block, catches;
14577{
14578 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14579 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14580 return node;
b67d701b
PB
14581}
14582
a7d8d81f
PB
14583static tree
14584build_try_finally_statement (location, try_block, finally)
14585 int location;
14586 tree try_block, finally;
b67d701b 14587{
a7d8d81f
PB
14588 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14589 EXPR_WFL_LINECOL (node) = location;
14590 return node;
b67d701b
PB
14591}
14592
14593static tree
14594patch_try_statement (node)
14595 tree node;
14596{
14597 int error_found = 0;
14598 tree try = TREE_OPERAND (node, 0);
14599 /* Exception handlers are considered in left to right order */
14600 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14601 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14602
14603 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14604 to process the next catch clause. We process the catch clause before
14605 the try block so that when processing the try block we can check thrown
14606 exceptions againts the caught type list. */
b67d701b
PB
14607 for (current = catch; current; current = TREE_CHAIN (current))
14608 {
14609 tree carg_decl, carg_type;
14610 tree sub_current, catch_block, catch_clause;
14611 int unreachable;
14612
b67d701b 14613 /* At this point, the structure of the catch clause is
b67d701b
PB
14614 CATCH_EXPR (catch node)
14615 BLOCK (with the decl of the parameter)
14616 COMPOUND_EXPR
7525cc04 14617 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14618 BLOCK (catch clause block)
a7d8d81f
PB
14619 */
14620 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14621 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14622 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14623
14624 /* Catch clauses can't have more than one parameter declared,
14625 but it's already enforced by the grammar. Make sure that the
14626 only parameter of the clause statement in of class Throwable
14627 or a subclass of Throwable, but that was done earlier. The
14628 catch clause parameter type has also been resolved. */
14629
14630 /* Just make sure that the catch clause parameter type inherits
14631 from java.lang.Throwable */
14632 if (!inherits_from_p (carg_type, throwable_type_node))
14633 {
14634 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14635 parse_error_context (wfl_operator,
781b0558 14636 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14637 lang_printable_name (carg_type, 0));
b67d701b
PB
14638 error_found = 1;
14639 continue;
14640 }
14641
14642 /* Partial check for unreachable catch statement: The catch
14643 clause is reachable iff is no earlier catch block A in
14644 the try statement such that the type of the catch
14645 clause's parameter is the same as or a subclass of the
14646 type of A's parameter */
14647 unreachable = 0;
14648 for (sub_current = catch;
14649 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14650 {
14651 tree sub_catch_clause, decl;
a7d8d81f 14652 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14653 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14654
14655 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14656 {
14657 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14658 parse_error_context
781b0558
KG
14659 (wfl_operator,
14660 "`catch' not reached because of the catch clause at line %d",
14661 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14662 unreachable = error_found = 1;
14663 break;
14664 }
14665 }
b67d701b
PB
14666 /* Complete the catch clause block */
14667 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14668 if (catch_block == error_mark_node)
14669 {
14670 error_found = 1;
14671 continue;
14672 }
15fdcfe9
PB
14673 if (CAN_COMPLETE_NORMALLY (catch_block))
14674 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14675 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14676
14677 if (unreachable)
14678 continue;
14679
14680 /* Things to do here: the exception must be thrown */
14681
14682 /* Link this type to the caught type list */
14683 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14684 }
14685
b9f7e36c
APB
14686 PUSH_EXCEPTIONS (caught_type_list);
14687 if ((try = java_complete_tree (try)) == error_mark_node)
14688 error_found = 1;
15fdcfe9
PB
14689 if (CAN_COMPLETE_NORMALLY (try))
14690 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14691 POP_EXCEPTIONS ();
14692
b67d701b
PB
14693 /* Verification ends here */
14694 if (error_found)
14695 return error_mark_node;
14696
14697 TREE_OPERAND (node, 0) = try;
14698 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14699 TREE_TYPE (node) = void_type_node;
14700 return node;
14701}
b9f7e36c
APB
14702
14703/* 14.17 The synchronized Statement */
14704
14705static tree
14706patch_synchronized_statement (node, wfl_op1)
14707 tree node, wfl_op1;
14708{
5a005d9e 14709 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14710 tree block = TREE_OPERAND (node, 1);
5a005d9e 14711
d8fccff5 14712 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14713
14714 if (expr == error_mark_node)
14715 {
14716 block = java_complete_tree (block);
14717 return expr;
14718 }
b9f7e36c
APB
14719
14720 /* The TYPE of expr must be a reference type */
5a005d9e 14721 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14722 {
14723 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14724 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14725 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14726 return error_mark_node;
14727 }
14728
ce6e9147
APB
14729 if (flag_emit_xref)
14730 {
14731 TREE_OPERAND (node, 0) = expr;
14732 TREE_OPERAND (node, 1) = java_complete_tree (block);
14733 CAN_COMPLETE_NORMALLY (node) = 1;
14734 return node;
14735 }
14736
b9f7e36c
APB
14737 /* Generate a try-finally for the synchronized statement, except
14738 that the handler that catches all throw exception calls
14739 _Jv_MonitorExit and then rethrow the exception.
14740 The synchronized statement is then implemented as:
14741 TRY
14742 {
14743 _Jv_MonitorEnter (expression)
14744 synchronized_block
14745 _Jv_MonitorExit (expression)
14746 }
14747 CATCH_ALL
14748 {
14749 e = _Jv_exception_info ();
14750 _Jv_MonitorExit (expression)
14751 Throw (e);
14752 } */
14753
5a005d9e
PB
14754 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14755 BUILD_MONITOR_ENTER (enter, expr_decl);
14756 BUILD_MONITOR_EXIT (exit, expr_decl);
14757 CAN_COMPLETE_NORMALLY (enter) = 1;
14758 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14759 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14760 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14761 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14762 build (COMPOUND_EXPR, NULL_TREE,
14763 build (WITH_CLEANUP_EXPR, NULL_TREE,
14764 build (COMPOUND_EXPR, NULL_TREE,
96847892 14765 assignment, enter),
5a005d9e
PB
14766 NULL_TREE, exit),
14767 block));
14768 node = build_expr_block (node, expr_decl);
14769
14770 return java_complete_tree (node);
b9f7e36c
APB
14771}
14772
14773/* 14.16 The throw Statement */
14774
14775static tree
14776patch_throw_statement (node, wfl_op1)
14777 tree node, wfl_op1;
14778{
14779 tree expr = TREE_OPERAND (node, 0);
14780 tree type = TREE_TYPE (expr);
14781 int unchecked_ok = 0, tryblock_throws_ok = 0;
14782
14783 /* Thrown expression must be assignable to java.lang.Throwable */
14784 if (!try_reference_assignconv (throwable_type_node, expr))
14785 {
14786 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14787 parse_error_context (wfl_operator,
14788 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14789 lang_printable_name (type, 0));
b9f7e36c
APB
14790 /* If the thrown expression was a reference, we further the
14791 compile-time check. */
14792 if (!JREFERENCE_TYPE_P (type))
14793 return error_mark_node;
14794 }
14795
14796 /* At least one of the following must be true */
14797
14798 /* The type of the throw expression is a not checked exception,
14799 i.e. is a unchecked expression. */
c877974e 14800 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14801
c2952b01
APB
14802 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14803 /* An instance can't throw a checked excetion unless that exception
14804 is explicitely declared in the `throws' clause of each
14805 constructor. This doesn't apply to anonymous classes, since they
14806 don't have declared constructors. */
14807 if (!unchecked_ok
14808 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14809 {
14810 tree current;
14811 for (current = TYPE_METHODS (current_class); current;
14812 current = TREE_CHAIN (current))
14813 if (DECL_CONSTRUCTOR_P (current)
14814 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14815 {
14816 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)",
14817 lang_printable_name (TREE_TYPE (expr), 0));
14818 return error_mark_node;
14819 }
14820 }
14821
b9f7e36c
APB
14822 /* Throw is contained in a try statement and at least one catch
14823 clause can receive the thrown expression or the current method is
14824 declared to throw such an exception. Or, the throw statement is
14825 contained in a method or constructor declaration and the type of
14826 the Expression is assignable to at least one type listed in the
14827 throws clause the declaration. */
b9f7e36c 14828 if (!unchecked_ok)
f099f336 14829 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14830 if (!(unchecked_ok || tryblock_throws_ok))
14831 {
14832 /* If there is a surrounding try block that has no matching
14833 clatch clause, report it first. A surrounding try block exits
14834 only if there is something after the list of checked
14835 exception thrown by the current function (if any). */
14836 if (IN_TRY_BLOCK_P ())
781b0558 14837 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 14838 lang_printable_name (type, 0));
b9f7e36c
APB
14839 /* If we have no surrounding try statement and the method doesn't have
14840 any throws, report it now. FIXME */
f099f336
APB
14841
14842 /* We report that the exception can't be throw from a try block
14843 in all circumstances but when the `throw' is inside a static
14844 block. */
b9f7e36c
APB
14845 else if (!EXCEPTIONS_P (currently_caught_type_list)
14846 && !tryblock_throws_ok)
f099f336 14847 {
c2952b01 14848 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14849 parse_error_context (wfl_operator,
14850 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14851 lang_printable_name (type, 0));
14852 else
781b0558
KG
14853 parse_error_context (wfl_operator,
14854 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14855 lang_printable_name (type, 0));
14856 }
b9f7e36c
APB
14857 /* Otherwise, the current method doesn't have the appropriate
14858 throws declaration */
14859 else
781b0558 14860 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14861 lang_printable_name (type, 0));
b9f7e36c
APB
14862 return error_mark_node;
14863 }
14864
ce6e9147 14865 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14866 BUILD_THROW (node, expr);
ce6e9147
APB
14867
14868 /* If doing xrefs, keep the location where the `throw' was seen. */
14869 if (flag_emit_xref)
14870 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14871 return node;
14872}
14873
14874/* Check that exception said to be thrown by method DECL can be
14875 effectively caught from where DECL is invoked. */
14876
14877static void
14878check_thrown_exceptions (location, decl)
14879 int location;
14880 tree decl;
14881{
14882 tree throws;
14883 /* For all the unchecked exceptions thrown by DECL */
14884 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14885 throws = TREE_CHAIN (throws))
0a2138e2 14886 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14887 {
3e78f871
PB
14888#if 1
14889 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14890 if (DECL_NAME (decl) == get_identifier ("clone"))
14891 continue;
14892#endif
b9f7e36c 14893 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14894 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14895 parse_error_context
14896 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14897 lang_printable_name (TREE_VALUE (throws), 0));
14898 else
14899 {
14900 parse_error_context
781b0558 14901 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14902 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14903 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14904 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14905 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14906 }
b9f7e36c
APB
14907 }
14908}
14909
c877974e 14910/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14911 try-catch blocks, OR is listed in the `throws' clause of the
14912 current method. */
14913
14914static int
0a2138e2 14915check_thrown_exceptions_do (exception)
b9f7e36c
APB
14916 tree exception;
14917{
14918 tree list = currently_caught_type_list;
c877974e 14919 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14920 /* First, all the nested try-catch-finally at that stage. The
14921 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14922 if (IS_UNCHECKED_EXCEPTION_P (exception))
14923 return 1;
b9f7e36c
APB
14924 while (list)
14925 {
14926 tree caught;
14927 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14928 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14929 return 1;
14930 list = TREE_CHAIN (list);
14931 }
14932 return 0;
14933}
14934
14935static void
14936purge_unchecked_exceptions (mdecl)
14937 tree mdecl;
14938{
14939 tree throws = DECL_FUNCTION_THROWS (mdecl);
14940 tree new = NULL_TREE;
14941
14942 while (throws)
14943 {
14944 tree next = TREE_CHAIN (throws);
c877974e 14945 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
14946 {
14947 TREE_CHAIN (throws) = new;
14948 new = throws;
14949 }
14950 throws = next;
14951 }
14952 /* List is inverted here, but it doesn't matter */
14953 DECL_FUNCTION_THROWS (mdecl) = new;
14954}
22eed1e6
APB
14955
14956/* 15.24 Conditional Operator ?: */
14957
14958static tree
14959patch_conditional_expr (node, wfl_cond, wfl_op1)
14960 tree node, wfl_cond, wfl_op1;
14961{
14962 tree cond = TREE_OPERAND (node, 0);
14963 tree op1 = TREE_OPERAND (node, 1);
14964 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 14965 tree resulting_type = NULL_TREE;
ac825856 14966 tree t1, t2, patched;
22eed1e6
APB
14967 int error_found = 0;
14968
ac825856
APB
14969 /* Operands of ?: might be StringBuffers crafted as a result of a
14970 string concatenation. Obtain a descent operand here. */
14971 if ((patched = patch_string (op1)))
14972 TREE_OPERAND (node, 1) = op1 = patched;
14973 if ((patched = patch_string (op2)))
14974 TREE_OPERAND (node, 2) = op2 = patched;
14975
14976 t1 = TREE_TYPE (op1);
14977 t2 = TREE_TYPE (op2);
14978
22eed1e6
APB
14979 /* The first expression must be a boolean */
14980 if (TREE_TYPE (cond) != boolean_type_node)
14981 {
14982 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
14983 parse_error_context (wfl_operator,
14984 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
14985 lang_printable_name (TREE_TYPE (cond), 0));
14986 error_found = 1;
14987 }
14988
14989 /* Second and third can be numeric, boolean (i.e. primitive),
14990 references or null. Anything else results in an error */
14991 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14992 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14993 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14994 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14995 error_found = 1;
14996
14997 /* Determine the type of the conditional expression. Same types are
14998 easy to deal with */
14999 else if (t1 == t2)
15000 resulting_type = t1;
15001
15002 /* There are different rules for numeric types */
15003 else if (JNUMERIC_TYPE_P (t1))
15004 {
15005 /* if byte/short found, the resulting type is short */
15006 if ((t1 == byte_type_node && t2 == short_type_node)
15007 || (t1 == short_type_node && t2 == byte_type_node))
15008 resulting_type = short_type_node;
15009
15010 /* If t1 is a constant int and t2 is of type byte, short or char
15011 and t1's value fits in t2, then the resulting type is t2 */
15012 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15013 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15014 resulting_type = t2;
15015
15016 /* If t2 is a constant int and t1 is of type byte, short or char
15017 and t2's value fits in t1, then the resulting type is t1 */
15018 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15019 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15020 resulting_type = t1;
15021
15022 /* Otherwise, binary numeric promotion is applied and the
15023 resulting type is the promoted type of operand 1 and 2 */
15024 else
93024893 15025 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15026 &TREE_OPERAND (node, 1),
15027 &TREE_OPERAND (node, 2));
15028 }
15029
15030 /* Cases of a reference and a null type */
15031 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15032 resulting_type = t1;
15033
15034 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15035 resulting_type = t2;
15036
15037 /* Last case: different reference types. If a type can be converted
15038 into the other one by assignment conversion, the latter
15039 determines the type of the expression */
15040 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15041 resulting_type = promote_type (t1);
15042
15043 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15044 resulting_type = promote_type (t2);
15045
15046 /* If we don't have any resulting type, we're in trouble */
15047 if (!resulting_type)
15048 {
c2e3db92 15049 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15050 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15051 parse_error_context (wfl_operator,
15052 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15053 t, lang_printable_name (t2, 0));
22eed1e6
APB
15054 free (t);
15055 error_found = 1;
15056 }
15057
15058 if (error_found)
15059 {
15060 TREE_TYPE (node) = error_mark_node;
15061 return error_mark_node;
15062 }
15063
15064 TREE_TYPE (node) = resulting_type;
15065 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15066 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15067 return node;
15068}
ac825856 15069
5b09b33e
PB
15070/* Try to constant fold NODE.
15071 If NODE is not a constant expression, return NULL_EXPR.
15072 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15073
15074static tree
15075fold_constant_for_init (node, context)
15076 tree node;
15077 tree context;
15078{
15079 tree op0, op1, val;
15080 enum tree_code code = TREE_CODE (node);
15081
ee97d354 15082 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15083 return node;
93024893 15084
5b09b33e
PB
15085 switch (code)
15086 {
5b09b33e
PB
15087 case PLUS_EXPR:
15088 case MINUS_EXPR:
bc3ca41b
PB
15089 case MULT_EXPR:
15090 case TRUNC_MOD_EXPR:
15091 case RDIV_EXPR:
5b09b33e
PB
15092 case LSHIFT_EXPR:
15093 case RSHIFT_EXPR:
15094 case URSHIFT_EXPR:
15095 case BIT_AND_EXPR:
15096 case BIT_XOR_EXPR:
15097 case BIT_IOR_EXPR:
5b09b33e
PB
15098 case TRUTH_ANDIF_EXPR:
15099 case TRUTH_ORIF_EXPR:
15100 case EQ_EXPR:
15101 case NE_EXPR:
15102 case GT_EXPR:
15103 case GE_EXPR:
15104 case LT_EXPR:
15105 case LE_EXPR:
15106 op0 = TREE_OPERAND (node, 0);
15107 op1 = TREE_OPERAND (node, 1);
15108 val = fold_constant_for_init (op0, context);
15109 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15110 return NULL_TREE;
15111 TREE_OPERAND (node, 0) = val;
15112 val = fold_constant_for_init (op1, context);
15113 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15114 return NULL_TREE;
15115 TREE_OPERAND (node, 1) = val;
15116 return patch_binop (node, op0, op1);
15117
15118 case UNARY_PLUS_EXPR:
15119 case NEGATE_EXPR:
15120 case TRUTH_NOT_EXPR:
15121 case BIT_NOT_EXPR:
15122 case CONVERT_EXPR:
15123 op0 = TREE_OPERAND (node, 0);
15124 val = fold_constant_for_init (op0, context);
15125 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15126 return NULL_TREE;
15127 TREE_OPERAND (node, 0) = val;
5a005d9e 15128 return patch_unaryop (node, op0);
5b09b33e
PB
15129 break;
15130
15131 case COND_EXPR:
15132 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15133 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15134 return NULL_TREE;
15135 TREE_OPERAND (node, 0) = val;
15136 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15137 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15138 return NULL_TREE;
15139 TREE_OPERAND (node, 1) = val;
15140 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15141 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15142 return NULL_TREE;
15143 TREE_OPERAND (node, 2) = val;
15144 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15145 : TREE_OPERAND (node, 2);
15146
15147 case VAR_DECL:
8576f094
APB
15148 case FIELD_DECL:
15149 if (! FIELD_FINAL (node)
5b09b33e
PB
15150 || DECL_INITIAL (node) == NULL_TREE)
15151 return NULL_TREE;
15152 val = DECL_INITIAL (node);
15153 /* Guard against infinite recursion. */
15154 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15155 val = fold_constant_for_init (val, node);
5b09b33e
PB
15156 DECL_INITIAL (node) = val;
15157 return val;
15158
15159 case EXPR_WITH_FILE_LOCATION:
15160 /* Compare java_complete_tree and resolve_expression_name. */
15161 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15162 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15163 {
15164 tree name = EXPR_WFL_NODE (node);
15165 tree decl;
15166 if (PRIMARY_P (node))
15167 return NULL_TREE;
15168 else if (! QUALIFIED_P (name))
15169 {
15170 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15171 if (decl == NULL_TREE
15172 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15173 return NULL_TREE;
15174 return fold_constant_for_init (decl, decl);
15175 }
15176 else
15177 {
5b09b33e
PB
15178 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15179 qualify_ambiguous_name (node);
15180 if (resolve_field_access (node, &decl, NULL)
15181 && decl != NULL_TREE)
15182 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15183 return NULL_TREE;
15184 }
15185 }
15186 else
15187 {
15188 op0 = TREE_OPERAND (node, 0);
15189 val = fold_constant_for_init (op0, context);
15190 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15191 return NULL_TREE;
15192 TREE_OPERAND (node, 0) = val;
15193 return val;
15194 }
15195
bc3ca41b
PB
15196#ifdef USE_COMPONENT_REF
15197 case IDENTIFIER:
15198 case COMPONENT_REF:
15199 ?;
15200#endif
15201
5b09b33e
PB
15202 default:
15203 return NULL_TREE;
15204 }
15205}
bc3ca41b
PB
15206
15207#ifdef USE_COMPONENT_REF
15208/* Context is 'T' for TypeName, 'P' for PackageName,
15209 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15210
15211tree
15212resolve_simple_name (name, context)
15213 tree name;
15214 int context;
15215{
15216}
15217
15218tree
15219resolve_qualified_name (name, context)
15220 tree name;
15221 int context;
15222{
15223}
15224#endif
f15b9af9
MM
15225
15226/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15227
15228static void
15229mark_parser_ctxt (p)
15230 void *p;
15231{
15232 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15233 int i;
15234
15235 if (!pc)
15236 return;
15237
15238#ifndef JC1_LITE
15239 for (i = 0; i < 11; ++i)
15240 ggc_mark_tree (pc->modifier_ctx[i]);
15241 ggc_mark_tree (pc->class_type);
15242 ggc_mark_tree (pc->function_decl);
15243 ggc_mark_tree (pc->package);
15244 ggc_mark_tree (pc->incomplete_class);
15245 ggc_mark_tree (pc->gclass_list);
15246 ggc_mark_tree (pc->class_list);
15247 ggc_mark_tree (pc->current_parsed_class);
15248 ggc_mark_tree (pc->current_parsed_class_un);
15249 ggc_mark_tree (pc->non_static_initialized);
15250 ggc_mark_tree (pc->static_initialized);
15251 ggc_mark_tree (pc->instance_initializers);
15252 ggc_mark_tree (pc->import_list);
15253 ggc_mark_tree (pc->import_demand_list);
15254 ggc_mark_tree (pc->current_loop);
15255 ggc_mark_tree (pc->current_labeled_block);
15256#endif /* JC1_LITE */
15257
15258 if (pc->next)
15259 mark_parser_ctxt (&pc->next);
15260}
This page took 2.646758 seconds and 5 git commands to generate.