]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
m68kelf.h: Set USE_GAS...
[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.
e511adc0 3 Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
e04a16fb
AG
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA.
22
23Java and all Java-based marks are trademarks or registered trademarks
24of Sun Microsystems, Inc. in the United States and other countries.
25The Free Software Foundation is independent of Sun Microsystems, Inc. */
26
27/* This file parses java source code and issues a tree node image
28suitable for code generation (byte code and targeted CPU assembly
29language).
30
31The grammar conforms to the Java grammar described in "The Java(TM)
32Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
331996, ISBN 0-201-63451-1"
34
35The following modifications were brought to the original grammar:
36
37method_body: added the rule '| block SC_TK'
e04a16fb
AG
38static_initializer: added the rule 'static block SC_TK'.
39
40Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
42
43statement_nsi: 'nsi' should be read no_short_if.
44
45Some rules have been modified to support JDK1.1 inner classes
46definitions and other extensions. */
47
48%{
e04a16fb 49#include "config.h"
36635152
GS
50#include "system.h"
51#include <dirent.h>
e04a16fb
AG
52#include "tree.h"
53#include "rtl.h"
54#include "obstack.h"
0a2138e2 55#include "toplev.h"
e04a16fb
AG
56#include "flags.h"
57#include "java-tree.h"
58#include "jcf.h"
59#include "lex.h"
60#include "parse.h"
61#include "zipfile.h"
5e942c50 62#include "convert.h"
63a212ed 63#include "buffer.h"
f099f336 64#include "xref.h"
b384405b 65#include "function.h"
138657ec 66#include "except.h"
0ae70c6a 67#include "defaults.h"
e04a16fb 68
fa322ab5
TT
69#ifndef DIR_SEPARATOR
70#define DIR_SEPARATOR '/'
71#endif
72
82371d41
APB
73/* Local function prototypes */
74static char *java_accstring_lookup PROTO ((int));
49f48c71 75static void classitf_redefinition_error PROTO ((const char *,tree, tree, tree));
82371d41 76static void variable_redefinition_error PROTO ((tree, tree, tree, int));
49f48c71 77static void check_modifiers PROTO ((const char *, int, int));
82371d41
APB
78static tree create_class PROTO ((int, tree, tree, tree));
79static tree create_interface PROTO ((int, tree, tree));
80static tree find_field PROTO ((tree, tree));
81static tree lookup_field_wrapper PROTO ((tree, tree));
82static int duplicate_declaration_error_p PROTO ((tree, tree, tree));
83static void register_fields PROTO ((int, tree, tree));
84static tree parser_qualified_classname PROTO ((tree));
85static int parser_check_super PROTO ((tree, tree, tree));
86static int parser_check_super_interface PROTO ((tree, tree, tree));
87static void check_modifiers_consistency PROTO ((int));
88static tree lookup_cl PROTO ((tree));
89static tree lookup_java_method2 PROTO ((tree, tree, int));
90static tree method_header PROTO ((int, tree, tree, tree));
91static void fix_method_argument_names PROTO ((tree ,tree));
92static tree method_declarator PROTO ((tree, tree));
d4476be2
KG
93static void parse_warning_context PVPROTO ((tree cl, const char *msg, ...))
94 ATTRIBUTE_PRINTF_2;
95static void issue_warning_error_from_context PROTO ((tree, const char *msg, va_list));
49f48c71 96static tree parse_jdk1_1_error PROTO ((const char *));
82371d41
APB
97static void complete_class_report_errors PROTO ((jdep *));
98static int process_imports PROTO ((void));
99static void read_import_dir PROTO ((tree));
100static int find_in_imports_on_demand PROTO ((tree));
101static int find_in_imports PROTO ((tree));
102static int check_pkg_class_access PROTO ((tree, tree));
103static tree resolve_package PROTO ((tree, tree *));
49f48c71
KG
104static tree lookup_package_type PROTO ((const char *, int));
105static tree lookup_package_type_and_set_next PROTO ((const char *, int, tree *));
82371d41 106static tree resolve_class PROTO ((tree, tree, tree));
82371d41
APB
107static void declare_local_variables PROTO ((int, tree, tree));
108static void source_start_java_method PROTO ((tree));
109static void source_end_java_method PROTO ((void));
110static void expand_start_java_method PROTO ((tree));
111static tree find_name_in_single_imports PROTO ((tree));
112static void check_abstract_method_header PROTO ((tree));
113static tree lookup_java_interface_method2 PROTO ((tree, tree));
114static tree resolve_expression_name PROTO ((tree, tree *));
115static tree maybe_create_class_interface_decl PROTO ((tree, tree, tree));
116static int check_class_interface_creation PROTO ((int, int, tree,
117 tree, tree, tree));
118static tree patch_method_invocation PROTO ((tree, tree, tree,
89e09b9a 119 int *, tree *));
82371d41
APB
120static int breakdown_qualified PROTO ((tree *, tree *, tree));
121static tree resolve_and_layout PROTO ((tree, tree));
122static tree resolve_no_layout PROTO ((tree, tree));
123static int invocation_mode PROTO ((tree, int));
124static tree find_applicable_accessible_methods_list PROTO ((int, tree,
125 tree, tree));
1982388a
APB
126static void search_applicable_methods_list PROTO ((int, tree, tree, tree,
127 tree *, tree *));
82371d41
APB
128static tree find_most_specific_methods_list PROTO ((tree));
129static int argument_types_convertible PROTO ((tree, tree));
89e09b9a 130static tree patch_invoke PROTO ((tree, tree, tree));
82371d41
APB
131static tree lookup_method_invoke PROTO ((int, tree, tree, tree, tree));
132static tree register_incomplete_type PROTO ((int, tree, tree, tree));
133static tree obtain_incomplete_type PROTO ((tree));
5b09b33e 134static tree java_complete_lhs PROTO ((tree));
82371d41
APB
135static tree java_complete_tree PROTO ((tree));
136static void java_complete_expand_method PROTO ((tree));
137static int unresolved_type_p PROTO ((tree, tree *));
138static void create_jdep_list PROTO ((struct parser_ctxt *));
139static tree build_expr_block PROTO ((tree, tree));
140static tree enter_block PROTO ((void));
141static tree enter_a_block PROTO ((tree));
142static tree exit_block PROTO ((void));
143static tree lookup_name_in_blocks PROTO ((tree));
144static void maybe_absorb_scoping_blocks PROTO ((void));
145static tree build_method_invocation PROTO ((tree, tree));
146static tree build_new_invocation PROTO ((tree, tree));
147static tree build_assignment PROTO ((int, int, tree, tree));
148static tree build_binop PROTO ((enum tree_code, int, tree, tree));
149static int check_final_assignment PROTO ((tree ,tree));
150static tree patch_assignment PROTO ((tree, tree, tree ));
151static tree patch_binop PROTO ((tree, tree, tree));
152static tree build_unaryop PROTO ((int, int, tree));
153static tree build_incdec PROTO ((int, int, tree, int));
154static tree patch_unaryop PROTO ((tree, tree));
155static tree build_cast PROTO ((int, tree, tree));
156static tree build_null_of_type PROTO ((tree));
157static tree patch_cast PROTO ((tree, tree));
158static int valid_ref_assignconv_cast_p PROTO ((tree, tree, int));
159static int valid_builtin_assignconv_identity_widening_p PROTO ((tree, tree));
160static int valid_cast_to_p PROTO ((tree, tree));
161static int valid_method_invocation_conversion_p PROTO ((tree, tree));
162static tree try_builtin_assignconv PROTO ((tree, tree, tree));
163static tree try_reference_assignconv PROTO ((tree, tree));
164static tree build_unresolved_array_type PROTO ((tree));
165static tree build_array_from_name PROTO ((tree, tree, tree, tree *));
166static tree build_array_ref PROTO ((int, tree, tree));
167static tree patch_array_ref PROTO ((tree));
168static tree make_qualified_name PROTO ((tree, tree, int));
169static tree merge_qualified_name PROTO ((tree, tree));
170static tree make_qualified_primary PROTO ((tree, tree, int));
171static int resolve_qualified_expression_name PROTO ((tree, tree *,
172 tree *, tree *));
173static void qualify_ambiguous_name PROTO ((tree));
174static void maybe_generate_clinit PROTO ((void));
175static tree resolve_field_access PROTO ((tree, tree *, tree *));
176static tree build_newarray_node PROTO ((tree, tree, int));
177static tree patch_newarray PROTO ((tree));
178static tree resolve_type_during_patch PROTO ((tree));
179static tree build_this PROTO ((int));
180static tree build_return PROTO ((int, tree));
181static tree patch_return PROTO ((tree));
182static tree maybe_access_field PROTO ((tree, tree, tree));
183static int complete_function_arguments PROTO ((tree));
184static int check_for_static_method_reference PROTO ((tree, tree, tree, tree, tree));
185static int not_accessible_p PROTO ((tree, tree, int));
186static void check_deprecation PROTO ((tree, tree));
187static int class_in_current_package PROTO ((tree));
188static tree build_if_else_statement PROTO ((int, tree, tree, tree));
189static tree patch_if_else_statement PROTO ((tree));
190static tree add_stmt_to_compound PROTO ((tree, tree, tree));
191static tree add_stmt_to_block PROTO ((tree, tree, tree));
192static tree patch_exit_expr PROTO ((tree));
193static tree build_labeled_block PROTO ((int, tree));
b635eb2f 194static tree finish_labeled_statement PROTO ((tree, tree));
82371d41
APB
195static tree build_bc_statement PROTO ((int, int, tree));
196static tree patch_bc_statement PROTO ((tree));
197static tree patch_loop_statement PROTO ((tree));
198static tree build_new_loop PROTO ((tree));
199static tree build_loop_body PROTO ((int, tree, int));
b635eb2f 200static tree finish_loop_body PROTO ((int, tree, tree, int));
82371d41 201static tree build_debugable_stmt PROTO ((int, tree));
b635eb2f 202static tree finish_for_loop PROTO ((int, tree, tree, tree));
82371d41
APB
203static tree patch_switch_statement PROTO ((tree));
204static tree string_constant_concatenation PROTO ((tree, tree));
205static tree build_string_concatenation PROTO ((tree, tree));
206static tree patch_string_cst PROTO ((tree));
207static tree patch_string PROTO ((tree));
a7d8d81f
PB
208static tree build_try_statement PROTO ((int, tree, tree));
209static tree build_try_finally_statement PROTO ((int, tree, tree));
82371d41
APB
210static tree patch_try_statement PROTO ((tree));
211static tree patch_synchronized_statement PROTO ((tree, tree));
212static tree patch_throw_statement PROTO ((tree, tree));
213static void check_thrown_exceptions PROTO ((int, tree));
214static int check_thrown_exceptions_do PROTO ((tree));
215static void purge_unchecked_exceptions PROTO ((tree));
216static void check_throws_clauses PROTO ((tree, tree, tree));
b635eb2f 217static void finish_method_declaration PROTO ((tree));
49f48c71 218static tree build_super_invocation PROTO ((void));
82371d41
APB
219static int verify_constructor_circularity PROTO ((tree, tree));
220static char *constructor_circularity_msg PROTO ((tree, tree));
221static tree build_this_super_qualified_invocation PROTO ((int, tree, tree,
222 int, int));
49f48c71 223static const char *get_printable_method_name PROTO ((tree));
82371d41 224static tree patch_conditional_expr PROTO ((tree, tree, tree));
49f48c71 225static void maybe_generate_finit PROTO ((void));
82371d41 226static void fix_constructors PROTO ((tree));
49f48c71 227static int verify_constructor_super PROTO ((void));
82371d41
APB
228static tree create_artificial_method PROTO ((tree, int, tree, tree, tree));
229static void start_artificial_method_body PROTO ((tree));
230static void end_artificial_method_body PROTO ((tree));
82371d41
APB
231static int check_method_redefinition PROTO ((tree, tree));
232static int reset_method_name PROTO ((tree));
233static void java_check_regular_methods PROTO ((tree));
234static void java_check_abstract_methods PROTO ((tree));
235static tree maybe_build_primttype_type_ref PROTO ((tree, tree));
236static void unreachable_stmt_error PROTO ((tree));
237static tree find_expr_with_wfl PROTO ((tree));
238static void missing_return_error PROTO ((tree));
f8976021
APB
239static tree build_new_array_init PROTO ((int, tree));
240static tree patch_new_array_init PROTO ((tree, tree));
f8976021
APB
241static tree maybe_build_array_element_wfl PROTO ((tree));
242static int array_constructor_check_entry PROTO ((tree, tree));
49f48c71 243static const char *purify_type_name PROTO ((const char *));
5b09b33e 244static tree fold_constant_for_init PROTO ((tree, tree));
e28cd97b 245static tree strip_out_static_field_access_decl PROTO ((tree));
7e21fe59 246static jdeplist *reverse_jdep_list PROTO ((struct parser_ctxt *));
7f10c2e2 247static void static_ref_err PROTO ((tree, tree, tree));
49f48c71
KG
248static void parser_add_interface PROTO ((tree, tree, tree));
249static void add_superinterfaces PROTO ((tree, tree));
250static tree jdep_resolve_class PROTO ((jdep *));
251static int note_possible_classname PROTO ((const char *, int));
252static void java_complete_expand_methods PROTO ((void));
253static void java_expand_finals PROTO ((void));
254static tree cut_identifier_in_qualified PROTO ((tree));
255static tree java_stabilize_reference PROTO ((tree));
256static tree do_unary_numeric_promotion PROTO ((tree));
257static char * operator_string PROTO ((tree));
258static tree do_merge_string_cste PROTO ((tree, const char *, int, int));
259static tree merge_string_cste PROTO ((tree, tree, int));
5cbdba64 260static tree java_refold PROTO ((tree));
be245ac0
KG
261static int java_decl_equiv PROTO ((tree, tree));
262static int binop_compound_p PROTO ((enum tree_code));
263static tree search_loop PROTO ((tree));
264static int labeled_block_contains_loop_p PROTO ((tree, tree));
d593dd8c
KG
265static void check_abstract_method_definitions PROTO ((int, tree, tree));
266static void java_check_abstract_method_definitions PROTO ((tree));
82371d41 267
e04a16fb
AG
268/* Number of error found so far. */
269int java_error_count;
270/* Number of warning found so far. */
271int java_warning_count;
ce6e9147
APB
272/* Tell when not to fold, when doing xrefs */
273int do_not_fold;
e04a16fb
AG
274
275/* The current parser context */
d4370213 276struct parser_ctxt *ctxp;
e04a16fb 277
d4370213 278/* List of things that were analyzed for which code will be generated */
b351b287
APB
279static struct parser_ctxt *ctxp_for_generation = NULL;
280
e04a16fb
AG
281/* binop_lookup maps token to tree_code. It is used where binary
282 operations are involved and required by the parser. RDIV_EXPR
283 covers both integral/floating point division. The code is changed
284 once the type of both operator is worked out. */
285
286static enum tree_code binop_lookup[19] =
287 {
288 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
289 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
290 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
291 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
292 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
293 };
294#define BINOP_LOOKUP(VALUE) \
295 binop_lookup [((VALUE) - PLUS_TK)% \
296 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
297
5cbdba64
APB
298/* This is the end index for binary operators that can also be used
299 in compound assignements. */
300#define BINOP_COMPOUND_CANDIDATES 11
301
e04a16fb
AG
302/* Fake WFL used to report error message. It is initialized once if
303 needed and reused with it's location information is overriden. */
15fdcfe9 304tree wfl_operator = NULL_TREE;
e04a16fb
AG
305
306/* The "$L" identifier we use to create labels. */
b67d701b
PB
307static tree label_id = NULL_TREE;
308
309/* The "StringBuffer" identifier used for the String `+' operator. */
310static tree wfl_string_buffer = NULL_TREE;
311
312/* The "append" identifier used for String `+' operator. */
313static tree wfl_append = NULL_TREE;
314
315/* The "toString" identifier used for String `+' operator. */
316static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
317
318/* The "java.lang" import qualified name. */
319static tree java_lang_id = NULL_TREE;
09ed0f70
APB
320
321/* The "java.lang.Cloneable" qualified name. */
322static tree java_lang_cloneable = NULL_TREE;
f099f336
APB
323
324/* Context and flag for static blocks */
325static tree current_static_block = NULL_TREE;
326
e04a16fb
AG
327%}
328
329%union {
330 tree node;
331 int sub_token;
332 struct {
333 int token;
334 int location;
335 } operator;
336 int value;
337}
338
9ee9b555
KG
339%{
340#include "lex.c"
341%}
342
e04a16fb
AG
343%pure_parser
344
345/* Things defined here have to match the order of what's in the
346 binop_lookup table. */
347
348%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
349%token LS_TK SRS_TK ZRS_TK
350%token AND_TK XOR_TK OR_TK
351%token BOOL_AND_TK BOOL_OR_TK
352%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
353
354/* This maps to the same binop_lookup entry than the token above */
355
356%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
357%token REM_ASSIGN_TK
358%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
359%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
360
361
362/* Modifier TOKEN have to be kept in this order. Don't scramble it */
363
364%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
365%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
366%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
367%token PAD_TK ABSTRACT_TK MODIFIER_TK
368
369/* Keep those two in order, too */
370%token DECR_TK INCR_TK
371
372/* From now one, things can be in any order */
373
374%token DEFAULT_TK IF_TK THROW_TK
375%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
376%token THROWS_TK BREAK_TK IMPORT_TK
377%token ELSE_TK INSTANCEOF_TK RETURN_TK
378%token VOID_TK CATCH_TK INTERFACE_TK
379%token CASE_TK EXTENDS_TK FINALLY_TK
380%token SUPER_TK WHILE_TK CLASS_TK
381%token SWITCH_TK CONST_TK TRY_TK
382%token FOR_TK NEW_TK CONTINUE_TK
383%token GOTO_TK PACKAGE_TK THIS_TK
384
385%token BYTE_TK SHORT_TK INT_TK LONG_TK
386%token CHAR_TK INTEGRAL_TK
387
388%token FLOAT_TK DOUBLE_TK FP_TK
389
390%token ID_TK
391
392%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
393
394%token ASSIGN_ANY_TK ASSIGN_TK
395%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
396
397%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
398%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
399
400%type <value> modifiers MODIFIER_TK
401
402%type <node> super ID_TK identifier
403%type <node> name simple_name qualified_name
404%type <node> class_declaration type_declaration compilation_unit
405 field_declaration method_declaration extends_interfaces
406 interfaces interface_type_list
407 interface_declaration class_member_declaration
408 import_declarations package_declaration
409 type_declarations interface_body
410 interface_member_declaration constant_declaration
411 interface_member_declarations interface_type
412 abstract_method_declaration interface_type_list
413%type <node> class_body_declaration class_member_declaration
414 static_initializer constructor_declaration block
22eed1e6 415%type <node> class_body_declarations constructor_header
e04a16fb
AG
416%type <node> class_or_interface_type class_type class_type_list
417 constructor_declarator explicit_constructor_invocation
b9f7e36c 418%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
419
420%type <node> variable_declarator_id variable_declarator
421 variable_declarators variable_initializer
22eed1e6 422 variable_initializers constructor_body
ac825856 423 array_initializer
e04a16fb 424
2e5eb5c5 425%type <node> class_body block_end constructor_block_end
e04a16fb
AG
426%type <node> statement statement_without_trailing_substatement
427 labeled_statement if_then_statement label_decl
428 if_then_else_statement while_statement for_statement
429 statement_nsi labeled_statement_nsi do_statement
430 if_then_else_statement_nsi while_statement_nsi
431 for_statement_nsi statement_expression_list for_init
432 for_update statement_expression expression_statement
433 primary_no_new_array expression primary
434 array_creation_expression array_type
435 class_instance_creation_expression field_access
436 method_invocation array_access something_dot_new
437 argument_list postfix_expression while_expression
438 post_increment_expression post_decrement_expression
439 unary_expression_not_plus_minus unary_expression
440 pre_increment_expression pre_decrement_expression
441 unary_expression_not_plus_minus cast_expression
442 multiplicative_expression additive_expression
443 shift_expression relational_expression
444 equality_expression and_expression
445 exclusive_or_expression inclusive_or_expression
446 conditional_and_expression conditional_or_expression
447 conditional_expression assignment_expression
448 left_hand_side assignment for_header for_begin
449 constant_expression do_statement_begin empty_statement
b67d701b 450 switch_statement synchronized_statement throw_statement
f8976021 451 try_statement switch_expression switch_block
15fdcfe9 452 catches catch_clause catch_clause_parameter finally
e04a16fb
AG
453%type <node> return_statement break_statement continue_statement
454
455%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
456%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
457%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
458%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
459%type <operator> ASSIGN_ANY_TK assignment_operator
460%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
461%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
462%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 463%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 464%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
465%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
466%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
e04a16fb
AG
467
468%type <node> method_body
469
470%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
471 STRING_LIT_TK NULL_TK VOID_TK
472
473%type <node> IF_TK WHILE_TK FOR_TK
474
475%type <node> formal_parameter_list formal_parameter
476 method_declarator method_header
477
478%type <node> primitive_type reference_type type
479 BOOLEAN_TK INTEGRAL_TK FP_TK
480
481%%
482/* 19.2 Production from 2.3: The Syntactic Grammar */
483goal:
484 compilation_unit
485 {}
486;
487
488/* 19.3 Productions from 3: Lexical structure */
489literal:
490 INT_LIT_TK
491| FP_LIT_TK
492| BOOL_LIT_TK
493| CHAR_LIT_TK
494| STRING_LIT_TK
495| NULL_TK
496;
497
498/* 19.4 Productions from 4: Types, Values and Variables */
499type:
500 primitive_type
501| reference_type
502;
503
504primitive_type:
505 INTEGRAL_TK
506| FP_TK
507| BOOLEAN_TK
508;
509
510reference_type:
511 class_or_interface_type
512| array_type
513;
514
515class_or_interface_type:
516 name
517;
518
519class_type:
520 class_or_interface_type /* Default rule */
521;
522
523interface_type:
524 class_or_interface_type
525;
526
527array_type:
528 primitive_type OSB_TK CSB_TK
529 {
530 $$ = build_java_array_type ($1, -1);
531 CLASS_LOADED_P ($$) = 1;
532 }
533| name OSB_TK CSB_TK
534 { $$ = build_unresolved_array_type ($1); }
535| array_type OSB_TK CSB_TK
536 { $$ = build_unresolved_array_type ($1); }
537| primitive_type OSB_TK error
538 {RULE ("']' expected"); RECOVER;}
539| array_type OSB_TK error
540 {RULE ("']' expected"); RECOVER;}
541;
542
543/* 19.5 Productions from 6: Names */
544name:
545 simple_name /* Default rule */
546| qualified_name /* Default rule */
547;
548
549simple_name:
550 identifier /* Default rule */
551;
552
553qualified_name:
554 name DOT_TK identifier
555 { $$ = make_qualified_name ($1, $3, $2.location); }
556;
557
558identifier:
559 ID_TK
560;
561
562/* 19.6: Production from 7: Packages */
563compilation_unit:
564 {$$ = NULL;}
565| package_declaration
566| import_declarations
567| type_declarations
568| package_declaration import_declarations
569| package_declaration type_declarations
570| import_declarations type_declarations
571| package_declaration import_declarations type_declarations
572;
573
574import_declarations:
575 import_declaration
576 {
577 $$ = NULL;
578 }
579| import_declarations import_declaration
580 {
581 $$ = NULL;
582 }
583;
584
585type_declarations:
586 type_declaration
587| type_declarations type_declaration
588;
589
590package_declaration:
591 PACKAGE_TK name SC_TK
592 { ctxp->package = EXPR_WFL_NODE ($2); }
593| PACKAGE_TK error
594 {yyerror ("Missing name"); RECOVER;}
595| PACKAGE_TK name error
596 {yyerror ("';' expected"); RECOVER;}
597;
598
599import_declaration:
600 single_type_import_declaration
601| type_import_on_demand_declaration
602;
603
604single_type_import_declaration:
605 IMPORT_TK name SC_TK
606 {
607 tree name = EXPR_WFL_NODE ($2), node, last_name;
608 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 609 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
610 while (last != IDENTIFIER_POINTER (name))
611 {
612 if (last [0] == '.')
613 break;
614 last--;
615 }
616 last_name = get_identifier (++last);
617 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
618 {
619 tree err = find_name_in_single_imports (last_name);
620 if (err && err != name)
621 parse_error_context
622 ($2, "Ambiguous class: `%s' and `%s'",
623 IDENTIFIER_POINTER (name),
624 IDENTIFIER_POINTER (err));
5e942c50
APB
625 else
626 REGISTER_IMPORT ($2, last_name)
e04a16fb
AG
627 }
628 else
5e942c50 629 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
630 }
631| IMPORT_TK error
632 {yyerror ("Missing name"); RECOVER;}
633| IMPORT_TK name error
634 {yyerror ("';' expected"); RECOVER;}
635;
636
637type_import_on_demand_declaration:
638 IMPORT_TK name DOT_TK MULT_TK SC_TK
639 {
640 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
641 /* Don't import java.lang.* twice. */
642 if (name != java_lang_id)
643 {
644 tree node = build_tree_list ($2, NULL_TREE);
645 read_import_dir ($2);
646 TREE_CHAIN (node) = ctxp->import_demand_list;
647 ctxp->import_demand_list = node;
648 }
e04a16fb
AG
649 }
650| IMPORT_TK name DOT_TK error
651 {yyerror ("'*' expected"); RECOVER;}
652| IMPORT_TK name DOT_TK MULT_TK error
653 {yyerror ("';' expected"); RECOVER;}
654;
655
656type_declaration:
657 class_declaration
658 {
22eed1e6 659 maybe_generate_finit ();
7525cc04 660 maybe_generate_clinit ();
e04a16fb
AG
661 $$ = $1;
662 }
663| interface_declaration
7f1d4866
APB
664 {
665 maybe_generate_clinit ();
666 $$ = $1;
667 }
e04a16fb
AG
668| SC_TK
669 { $$ = NULL; }
670| error
671 {
672 YYERROR_NOW;
673 yyerror ("Class or interface declaration expected");
674 }
675;
676
677/* 19.7 Shortened from the original:
678 modifiers: modifier | modifiers modifier
679 modifier: any of public... */
680modifiers:
681 MODIFIER_TK
682 {
683 $$ = (1 << $1);
684 }
685| modifiers MODIFIER_TK
686 {
687 int acc = (1 << $2);
688 if ($$ & acc)
689 parse_error_context
690 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
691 java_accstring_lookup (acc));
692 else
693 {
694 $$ |= acc;
695 }
696 }
697;
698
699/* 19.8.1 Production from $8.1: Class Declaration */
700class_declaration:
701 modifiers CLASS_TK identifier super interfaces
702 { create_class ($1, $3, $4, $5); }
703 class_body
704 {
705 $$ = $7;
706 }
707| CLASS_TK identifier super interfaces
708 { create_class (0, $2, $3, $4); }
709 class_body
710 {
711 $$ = $6;
712 }
713| modifiers CLASS_TK error
714 {yyerror ("Missing class name"); RECOVER;}
715| CLASS_TK error
716 {yyerror ("Missing class name"); RECOVER;}
717| CLASS_TK identifier error
0b4d333e
APB
718 {
719 if (!ctxp->class_err) yyerror ("'{' expected");
720 DRECOVER(class1);
721 }
e04a16fb
AG
722| modifiers CLASS_TK identifier error
723 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
724;
725
726super:
727 { $$ = NULL; }
728| EXTENDS_TK class_type
729 { $$ = $2; }
730| EXTENDS_TK class_type error
731 {yyerror ("'{' expected"); ctxp->class_err=1;}
732| EXTENDS_TK error
733 {yyerror ("Missing super class name"); ctxp->class_err=1;}
734;
735
736interfaces:
737 { $$ = NULL_TREE; }
738| IMPLEMENTS_TK interface_type_list
739 { $$ = $2; }
740| IMPLEMENTS_TK error
741 {
742 ctxp->class_err=1;
743 yyerror ("Missing interface name");
744 }
745;
746
747interface_type_list:
748 interface_type
749 {
750 ctxp->interface_number = 1;
751 $$ = build_tree_list ($1, NULL_TREE);
752 }
753| interface_type_list C_TK interface_type
754 {
755 ctxp->interface_number++;
756 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
757 }
758| interface_type_list C_TK error
759 {yyerror ("Missing interface name"); RECOVER;}
760;
761
762class_body:
763 OCB_TK CCB_TK
7f10c2e2
APB
764 {
765 /* Store the location of the `}' when doing xrefs */
766 if (flag_emit_xref)
767 DECL_END_SOURCE_LINE (ctxp->current_parsed_class) =
768 EXPR_WFL_ADD_COL ($2.location, 1);
769 $$ = ctxp->current_parsed_class;
770 }
e04a16fb 771| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
772 {
773 /* Store the location of the `}' when doing xrefs */
774 if (flag_emit_xref)
775 DECL_END_SOURCE_LINE (ctxp->current_parsed_class) =
776 EXPR_WFL_ADD_COL ($3.location, 1);
777 $$ = ctxp->current_parsed_class;
778 }
e04a16fb
AG
779;
780
781class_body_declarations:
782 class_body_declaration
783| class_body_declarations class_body_declaration
784;
785
786class_body_declaration:
787 class_member_declaration
788| static_initializer
789| constructor_declaration
790| block /* Added, JDK1.1, instance initializer */
b67d701b 791 { $$ = parse_jdk1_1_error ("instance initializer"); }
e04a16fb
AG
792;
793
794class_member_declaration:
795 field_declaration
0b4d333e
APB
796| field_declaration SC_TK
797 { $$ = $1; }
e04a16fb
AG
798| method_declaration
799| class_declaration /* Added, JDK1.1 inner classes */
b67d701b 800 { $$ = parse_jdk1_1_error ("inner classe declaration"); }
e04a16fb 801| interface_declaration /* Added, JDK1.1 inner classes */
b67d701b 802 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
e04a16fb
AG
803;
804
805/* 19.8.2 Productions from 8.3: Field Declarations */
806field_declaration:
807 type variable_declarators SC_TK
808 { register_fields (0, $1, $2); }
809| modifiers type variable_declarators SC_TK
810 {
e04a16fb
AG
811 check_modifiers
812 ("Illegal modifier `%s' for field declaration",
813 $1, FIELD_MODIFIERS);
814 check_modifiers_consistency ($1);
815 register_fields ($1, $2, $3);
816 }
817;
818
819variable_declarators:
820 /* Should we use build_decl_list () instead ? FIXME */
821 variable_declarator /* Default rule */
822| variable_declarators C_TK variable_declarator
823 { $$ = chainon ($1, $3); }
824| variable_declarators C_TK error
825 {yyerror ("Missing term"); RECOVER;}
826;
827
828variable_declarator:
829 variable_declarator_id
830 { $$ = build_tree_list ($1, NULL_TREE); }
831| variable_declarator_id ASSIGN_TK variable_initializer
832 {
833 if (java_error_count)
834 $3 = NULL_TREE;
835 $$ = build_tree_list
836 ($1, build_assignment ($2.token, $2.location, $1, $3));
837 }
838| variable_declarator_id ASSIGN_TK error
839 {
840 yyerror ("Missing variable initializer");
841 $$ = build_tree_list ($1, NULL_TREE);
842 RECOVER;
843 }
844| variable_declarator_id ASSIGN_TK variable_initializer error
845 {
846 yyerror ("';' expected");
847 $$ = build_tree_list ($1, NULL_TREE);
848 RECOVER;
849 }
850;
851
852variable_declarator_id:
853 identifier
854| variable_declarator_id OSB_TK CSB_TK
c583dd46 855 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
856| identifier error
857 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
858| variable_declarator_id OSB_TK error
859 {yyerror ("']' expected"); DRECOVER(vdi);}
860| variable_declarator_id CSB_TK error
861 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
862;
863
864variable_initializer:
865 expression
866| array_initializer
e04a16fb
AG
867;
868
869/* 19.8.3 Productions from 8.4: Method Declarations */
870method_declaration:
871 method_header
872 {
873 current_function_decl = $1;
874 source_start_java_method (current_function_decl);
875 }
876 method_body
b635eb2f 877 { finish_method_declaration ($3); }
e04a16fb
AG
878| method_header error
879 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
880;
881
882method_header:
883 type method_declarator throws
b9f7e36c 884 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 885| VOID_TK method_declarator throws
b9f7e36c 886 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 887| modifiers type method_declarator throws
b9f7e36c 888 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 889| modifiers VOID_TK method_declarator throws
b9f7e36c 890 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb
AG
891| type error
892 {RECOVER;}
893| modifiers type error
894 {RECOVER;}
895| VOID_TK error
896 {yyerror ("Identifier expected"); RECOVER;}
897| modifiers VOID_TK error
898 {yyerror ("Identifier expected"); RECOVER;}
899| modifiers error
900 {
901 yyerror ("Invalid method declaration, return type required");
902 RECOVER;
903 }
904;
905
906method_declarator:
907 identifier OP_TK CP_TK
908 { $$ = method_declarator ($1, NULL_TREE); }
909| identifier OP_TK formal_parameter_list CP_TK
910 { $$ = method_declarator ($1, $3); }
911| method_declarator OSB_TK CSB_TK
912 {
1886c9d8
APB
913 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
914 TREE_PURPOSE ($1) =
915 build_unresolved_array_type (TREE_PURPOSE ($1));
916 parse_warning_context
917 (wfl_operator,
918 "Discouraged form of returned type specification");
e04a16fb
AG
919 }
920| identifier OP_TK error
921 {yyerror ("')' expected"); DRECOVER(method_declarator);}
922| method_declarator OSB_TK error
923 {yyerror ("']' expected"); RECOVER;}
924;
925
926formal_parameter_list:
927 formal_parameter
928 {
929 ctxp->formal_parameter_number = 1;
930 }
931| formal_parameter_list C_TK formal_parameter
932 {
933 ctxp->formal_parameter_number += 1;
934 $$ = chainon ($1, $3);
935 }
936| formal_parameter_list C_TK error
937 {yyerror ("Missing formal parameter term"); RECOVER;}
938;
939
940formal_parameter:
941 type variable_declarator_id
942 {
943 $$ = build_tree_list ($2, $1);
944 }
c877974e 945| modifiers type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37
APB
946 {
947 parse_jdk1_1_error ("final parameters");
948 $$ = build_tree_list ($3, $2);
949 }
e04a16fb
AG
950| type error
951 {yyerror ("Missing identifier"); RECOVER;}
952| modifiers type error
953 {
954 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
955 yyerror ("Missing identifier"); RECOVER;
956 }
957;
958
959throws:
b9f7e36c 960 { $$ = NULL_TREE; }
e04a16fb 961| THROWS_TK class_type_list
b9f7e36c 962 { $$ = $2; }
e04a16fb
AG
963| THROWS_TK error
964 {yyerror ("Missing class type term"); RECOVER;}
965;
966
967class_type_list:
968 class_type
c877974e 969 { $$ = build_tree_list ($1, $1); }
e04a16fb 970| class_type_list C_TK class_type
c877974e 971 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
972| class_type_list C_TK error
973 {yyerror ("Missing class type term"); RECOVER;}
974;
975
976method_body:
977 block
978| block SC_TK
979| SC_TK
980 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
981;
982
983/* 19.8.4 Productions from 8.5: Static Initializers */
984static_initializer:
985 static block
986 {
f099f336
APB
987 TREE_CHAIN ($2) = ctxp->static_initialized;
988 ctxp->static_initialized = $2;
e04a16fb
AG
989 }
990| static block SC_TK /* Shouldn't be here. FIXME */
991 {
f099f336
APB
992 TREE_CHAIN ($2) = ctxp->static_initialized;
993 ctxp->static_initialized = $2;
e04a16fb
AG
994 }
995;
996
997static: /* Test lval.sub_token here */
998 MODIFIER_TK
999 {
1000 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1001 }
1002;
1003
1004/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1005constructor_declaration:
22eed1e6 1006 constructor_header
e04a16fb 1007 {
22eed1e6
APB
1008 current_function_decl = $1;
1009 source_start_java_method (current_function_decl);
e04a16fb 1010 }
22eed1e6 1011 constructor_body
b635eb2f 1012 { finish_method_declaration ($3); }
22eed1e6
APB
1013;
1014
1015constructor_header:
1016 constructor_declarator throws
1017 { $$ = method_header (0, NULL_TREE, $1, $2); }
1018| modifiers constructor_declarator throws
1019 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1020;
1021
1022constructor_declarator:
1023 simple_name OP_TK CP_TK
22eed1e6 1024 { $$ = method_declarator ($1, NULL_TREE); }
e04a16fb 1025| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1026 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1027;
1028
1029constructor_body:
22eed1e6
APB
1030 /* Unlike regular method, we always need a complete (empty)
1031 body so we can safely perform all the required code
1032 addition (super invocation and field initialization) */
2e5eb5c5 1033 block_begin constructor_block_end
22eed1e6 1034 {
9bbc7d9f 1035 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1036 $$ = $2;
1037 }
2e5eb5c5 1038| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1039 { $$ = $3; }
2e5eb5c5 1040| block_begin block_statements constructor_block_end
22eed1e6 1041 { $$ = $3; }
2e5eb5c5 1042| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1043 { $$ = $4; }
e04a16fb
AG
1044;
1045
2e5eb5c5
APB
1046constructor_block_end:
1047 block_end
1048| block_end SC_TK
1049
e04a16fb
AG
1050/* Error recovery for that rule moved down expression_statement: rule. */
1051explicit_constructor_invocation:
1052 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1053 {
1054 $$ = build_method_invocation ($1, NULL_TREE);
1055 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1056 $$ = java_method_add_stmt (current_function_decl, $$);
1057 }
e04a16fb 1058| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1059 {
1060 $$ = build_method_invocation ($1, $3);
1061 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1062 $$ = java_method_add_stmt (current_function_decl, $$);
1063 }
e04a16fb
AG
1064 /* Added, JDK1.1 inner classes. Modified because the rule
1065 'primary' couldn't work. */
1066| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1067 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1068| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1069 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1070;
1071
1072this_or_super: /* Added, simplifies error diagnostics */
1073 THIS_TK
1074 {
9ee9b555 1075 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1076 EXPR_WFL_LINECOL (wfl) = $1.location;
1077 $$ = wfl;
1078 }
1079| SUPER_TK
1080 {
9ee9b555 1081 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1082 EXPR_WFL_LINECOL (wfl) = $1.location;
1083 $$ = wfl;
1084 }
1085;
1086
1087/* 19.9 Productions from 9: Interfaces */
1088/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1089interface_declaration:
1090 INTERFACE_TK identifier
1091 { create_interface (0, $2, NULL_TREE); }
1092 interface_body
1093 {
1094 $$ = $4;
1095 }
1096| modifiers INTERFACE_TK identifier
1097 { create_interface ($1, $3, NULL_TREE); }
1098 interface_body
1099 {
1100 $$ = $5;
1101 }
1102| INTERFACE_TK identifier extends_interfaces
1103 { create_interface (0, $2, $3); }
1104 interface_body
1105 {
1106 $$ = $5;
1107 }
1108| modifiers INTERFACE_TK identifier extends_interfaces
1109 { create_interface ($1, $3, $4); }
1110 interface_body
1111 {
1112 $$ = $6;
1113 }
1114| INTERFACE_TK identifier error
0b4d333e 1115 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1116| modifiers INTERFACE_TK identifier error
0b4d333e 1117 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1118;
1119
1120extends_interfaces:
1121 EXTENDS_TK interface_type
1122 {
1123 ctxp->interface_number = 1;
1124 $$ = build_tree_list ($2, NULL_TREE);
1125 }
1126| extends_interfaces C_TK interface_type
1127 {
1128 ctxp->interface_number++;
1129 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1130 }
1131| EXTENDS_TK error
1132 {yyerror ("Invalid interface type"); RECOVER;}
1133| extends_interfaces C_TK error
1134 {yyerror ("Missing term"); RECOVER;}
1135;
1136
1137interface_body:
1138 OCB_TK CCB_TK
1139 { $$ = NULL_TREE; }
1140| OCB_TK interface_member_declarations CCB_TK
1141 { $$ = NULL_TREE; }
1142;
1143
1144interface_member_declarations:
1145 interface_member_declaration
1146| interface_member_declarations interface_member_declaration
1147;
1148
1149interface_member_declaration:
1150 constant_declaration
1151| abstract_method_declaration
1152| class_declaration /* Added, JDK1.1 inner classes */
b67d701b 1153 { $$ = parse_jdk1_1_error ("inner class declaration"); }
e04a16fb 1154| interface_declaration /* Added, JDK1.1 inner classes */
b67d701b 1155 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
e04a16fb
AG
1156;
1157
1158constant_declaration:
1159 field_declaration
1160;
1161
1162abstract_method_declaration:
1163 method_header SC_TK
1164 {
1165 check_abstract_method_header ($1);
1166 current_function_decl = NULL_TREE; /* FIXME ? */
1167 }
1168| method_header error
1169 {yyerror ("';' expected"); RECOVER;}
1170;
1171
1172/* 19.10 Productions from 10: Arrays */
1173array_initializer:
1174 OCB_TK CCB_TK
1179ebc2 1175 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1176| OCB_TK variable_initializers CCB_TK
f8976021 1177 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1178| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1179 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1180;
1181
1182variable_initializers:
1183 variable_initializer
f8976021
APB
1184 {
1185 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1186 $1, NULL_TREE);
1187 }
e04a16fb 1188| variable_initializers C_TK variable_initializer
1179ebc2
APB
1189 {
1190 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1191 }
e04a16fb
AG
1192| variable_initializers C_TK error
1193 {yyerror ("Missing term"); RECOVER;}
1194;
1195
1196/* 19.11 Production from 14: Blocks and Statements */
1197block:
1198 OCB_TK CCB_TK
7f10c2e2
APB
1199 {
1200 /* Store the location of the `}' when doing xrefs */
1201 if (current_function_decl && flag_emit_xref)
1202 DECL_END_SOURCE_LINE (current_function_decl) =
1203 EXPR_WFL_ADD_COL ($2.location, 1);
1204 $$ = empty_stmt_node;
1205 }
22eed1e6
APB
1206| block_begin block_statements block_end
1207 { $$ = $3; }
1208;
1209
1210block_begin:
1211 OCB_TK
e04a16fb 1212 { enter_block (); }
22eed1e6
APB
1213;
1214
1215block_end:
e04a16fb
AG
1216 CCB_TK
1217 {
1218 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1219 /* Store the location of the `}' when doing xrefs */
1220 if (current_function_decl && flag_emit_xref)
1221 DECL_END_SOURCE_LINE (current_function_decl) =
1222 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb
AG
1223 $$ = exit_block ();
1224 }
1225;
1226
1227block_statements:
1228 block_statement
1229| block_statements block_statement
1230;
1231
1232block_statement:
1233 local_variable_declaration_statement
1234| statement
15fdcfe9 1235 { java_method_add_stmt (current_function_decl, $1); }
e04a16fb 1236| class_declaration /* Added, JDK1.1 inner classes */
15fdcfe9 1237 { parse_jdk1_1_error ("inner class declaration"); }
e04a16fb
AG
1238;
1239
1240local_variable_declaration_statement:
1241 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1242;
1243
1244local_variable_declaration:
1245 type variable_declarators
1246 { declare_local_variables (0, $1, $2); }
1247| modifiers type variable_declarators /* Added, JDK1.1 final locals */
1248 { declare_local_variables ($1, $2, $3); }
1249;
1250
1251statement:
1252 statement_without_trailing_substatement
1253| labeled_statement
e04a16fb 1254| if_then_statement
e04a16fb 1255| if_then_else_statement
e04a16fb 1256| while_statement
e04a16fb 1257| for_statement
cd9643f7 1258 { $$ = exit_block (); }
e04a16fb
AG
1259;
1260
1261statement_nsi:
1262 statement_without_trailing_substatement
1263| labeled_statement_nsi
e04a16fb 1264| if_then_else_statement_nsi
e04a16fb 1265| while_statement_nsi
e04a16fb 1266| for_statement_nsi
9dd939b2 1267 { $$ = exit_block (); }
e04a16fb
AG
1268;
1269
1270statement_without_trailing_substatement:
1271 block
e04a16fb 1272| empty_statement
e04a16fb 1273| expression_statement
e04a16fb 1274| switch_statement
e04a16fb 1275| do_statement
e04a16fb 1276| break_statement
e04a16fb 1277| continue_statement
e04a16fb
AG
1278| return_statement
1279| synchronized_statement
e04a16fb 1280| throw_statement
e04a16fb 1281| try_statement
e04a16fb
AG
1282;
1283
1284empty_statement:
1285 SC_TK
9bbc7d9f 1286 { $$ = empty_stmt_node; }
e04a16fb
AG
1287;
1288
1289label_decl:
1290 identifier REL_CL_TK
1291 {
1292 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1293 EXPR_WFL_NODE ($1));
e04a16fb
AG
1294 pushlevel (2);
1295 push_labeled_block ($$);
1296 PUSH_LABELED_BLOCK ($$);
1297 }
1298;
1299
1300labeled_statement:
1301 label_decl statement
b635eb2f 1302 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1303| identifier error
1304 {yyerror ("':' expected"); RECOVER;}
1305;
1306
1307labeled_statement_nsi:
1308 label_decl statement_nsi
b635eb2f 1309 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1310;
1311
1312/* We concentrate here a bunch of error handling rules that we couldn't write
1313 earlier, because expression_statement catches a missing ';'. */
1314expression_statement:
1315 statement_expression SC_TK
1316 {
1317 /* We have a statement. Generate a WFL around it so
1318 we can debug it */
1319 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1320 /* We know we have a statement, so set the debug
1321 info to be eventually generate here. */
1322 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1323 }
1324| error SC_TK
1325 {
1326 if (ctxp->prevent_ese != lineno)
1327 yyerror ("Invalid expression statement");
1328 DRECOVER (expr_stmt);
1329 }
1330| error OCB_TK
1331 {
1332 if (ctxp->prevent_ese != lineno)
1333 yyerror ("Invalid expression statement");
1334 DRECOVER (expr_stmt);
1335 }
1336| error CCB_TK
1337 {
1338 if (ctxp->prevent_ese != lineno)
1339 yyerror ("Invalid expression statement");
1340 DRECOVER (expr_stmt);
1341 }
1342| this_or_super OP_TK error
1343 {yyerror ("')' expected"); RECOVER;}
1344| this_or_super OP_TK CP_TK error
22eed1e6
APB
1345 {
1346 yyerror ("Constructor invocation must be first "
1347 "thing in a constructor");
1348 RECOVER;
1349 }
e04a16fb
AG
1350| this_or_super OP_TK argument_list error
1351 {yyerror ("')' expected"); RECOVER;}
1352| this_or_super OP_TK argument_list CP_TK error
22eed1e6
APB
1353 {
1354 yyerror ("Constructor invocation must be first "
1355 "thing in a constructor");
1356 RECOVER;
1357 }
e04a16fb
AG
1358| name DOT_TK SUPER_TK error
1359 {yyerror ("'(' expected"); RECOVER;}
1360| name DOT_TK SUPER_TK OP_TK error
1361 {yyerror ("')' expected"); RECOVER;}
1362| name DOT_TK SUPER_TK OP_TK argument_list error
1363 {yyerror ("')' expected"); RECOVER;}
1364| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1365 {yyerror ("';' expected"); RECOVER;}
1366| name DOT_TK SUPER_TK OP_TK CP_TK error
1367 {yyerror ("';' expected"); RECOVER;}
1368;
1369
1370statement_expression:
1371 assignment
1372| pre_increment_expression
e04a16fb 1373| pre_decrement_expression
e04a16fb 1374| post_increment_expression
e04a16fb 1375| post_decrement_expression
e04a16fb
AG
1376| method_invocation
1377| class_instance_creation_expression
e04a16fb
AG
1378;
1379
1380if_then_statement:
1381 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1382 {
1383 $$ = build_if_else_statement ($2.location, $3,
1384 $5, NULL_TREE);
1385 }
e04a16fb
AG
1386| IF_TK error
1387 {yyerror ("'(' expected"); RECOVER;}
1388| IF_TK OP_TK error
1389 {yyerror ("Missing term"); RECOVER;}
1390| IF_TK OP_TK expression error
1391 {yyerror ("')' expected"); RECOVER;}
1392;
1393
1394if_then_else_statement:
1395 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1396 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1397;
1398
1399if_then_else_statement_nsi:
1400 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1401 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1402;
1403
1404switch_statement:
15fdcfe9
PB
1405 switch_expression
1406 {
1407 enter_block ();
1408 }
1409 switch_block
b67d701b 1410 {
15fdcfe9 1411 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1412 I.e. make the last statment also have its own
1413 COMPOUND_EXPR. */
15fdcfe9
PB
1414 maybe_absorb_scoping_blocks ();
1415 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1416 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1417 }
1418;
1419
1420switch_expression:
1421 SWITCH_TK OP_TK expression CP_TK
1422 {
1423 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1424 EXPR_WFL_LINECOL ($$) = $2.location;
1425 }
e04a16fb
AG
1426| SWITCH_TK error
1427 {yyerror ("'(' expected"); RECOVER;}
1428| SWITCH_TK OP_TK error
1429 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1430| SWITCH_TK OP_TK expression CP_TK error
1431 {yyerror ("'{' expected"); RECOVER;}
1432;
1433
f8976021
APB
1434/* Default assignment is there to avoid type node on switch_block
1435 node. */
1436
e04a16fb
AG
1437switch_block:
1438 OCB_TK CCB_TK
f8976021 1439 { $$ = NULL_TREE; }
e04a16fb 1440| OCB_TK switch_labels CCB_TK
f8976021 1441 { $$ = NULL_TREE; }
e04a16fb 1442| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1443 { $$ = NULL_TREE; }
e04a16fb 1444| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1445 { $$ = NULL_TREE; }
e04a16fb
AG
1446;
1447
1448switch_block_statement_groups:
1449 switch_block_statement_group
1450| switch_block_statement_groups switch_block_statement_group
1451;
1452
1453switch_block_statement_group:
15fdcfe9 1454 switch_labels block_statements
e04a16fb
AG
1455;
1456
e04a16fb
AG
1457switch_labels:
1458 switch_label
1459| switch_labels switch_label
1460;
1461
1462switch_label:
1463 CASE_TK constant_expression REL_CL_TK
b67d701b 1464 {
15fdcfe9
PB
1465 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1466 EXPR_WFL_LINECOL (lab) = $1.location;
1467 java_method_add_stmt (current_function_decl, lab);
b67d701b 1468 }
e04a16fb 1469| DEFAULT_TK REL_CL_TK
b67d701b 1470 {
15fdcfe9
PB
1471 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1472 EXPR_WFL_LINECOL (lab) = $1.location;
1473 java_method_add_stmt (current_function_decl, lab);
b67d701b 1474 }
e04a16fb
AG
1475| CASE_TK error
1476 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1477| CASE_TK constant_expression error
1478 {yyerror ("':' expected"); RECOVER;}
1479| DEFAULT_TK error
1480 {yyerror ("':' expected"); RECOVER;}
1481;
1482
1483while_expression:
1484 WHILE_TK OP_TK expression CP_TK
1485 {
1486 tree body = build_loop_body ($2.location, $3, 0);
1487 $$ = build_new_loop (body);
1488 }
1489;
1490
1491while_statement:
1492 while_expression statement
b635eb2f 1493 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1494| WHILE_TK error
1495 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1496| WHILE_TK OP_TK error
1497 {yyerror ("Missing term and ')' expected"); RECOVER;}
1498| WHILE_TK OP_TK expression error
1499 {yyerror ("')' expected"); RECOVER;}
1500;
1501
1502while_statement_nsi:
1503 while_expression statement_nsi
b635eb2f 1504 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1505;
1506
1507do_statement_begin:
1508 DO_TK
1509 {
1510 tree body = build_loop_body (0, NULL_TREE, 1);
1511 $$ = build_new_loop (body);
1512 }
1513 /* Need error handing here. FIXME */
1514;
1515
1516do_statement:
1517 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1518 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1519;
1520
1521for_statement:
1522 for_begin SC_TK expression SC_TK for_update CP_TK statement
b635eb2f 1523 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7); }
e04a16fb
AG
1524| for_begin SC_TK SC_TK for_update CP_TK statement
1525 {
b635eb2f 1526 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1527 /* We have not condition, so we get rid of the EXIT_EXPR */
1528 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1529 empty_stmt_node;
e04a16fb
AG
1530 }
1531| for_begin SC_TK error
1532 {yyerror ("Invalid control expression"); RECOVER;}
1533| for_begin SC_TK expression SC_TK error
1534 {yyerror ("Invalid update expression"); RECOVER;}
1535| for_begin SC_TK SC_TK error
1536 {yyerror ("Invalid update expression"); RECOVER;}
1537;
1538
1539for_statement_nsi:
1540 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1541 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1542| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1543 {
b635eb2f 1544 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1545 /* We have not condition, so we get rid of the EXIT_EXPR */
1546 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1547 empty_stmt_node;
e04a16fb
AG
1548 }
1549;
1550
1551for_header:
1552 FOR_TK OP_TK
1553 {
1554 /* This scope defined for local variable that may be
1555 defined within the scope of the for loop */
1556 enter_block ();
1557 }
1558| FOR_TK error
1559 {yyerror ("'(' expected"); DRECOVER(for_1);}
1560| FOR_TK OP_TK error
1561 {yyerror ("Invalid init statement"); RECOVER;}
1562;
1563
1564for_begin:
1565 for_header for_init
1566 {
1567 /* We now declare the loop body. The loop is
1568 declared as a for loop. */
1569 tree body = build_loop_body (0, NULL_TREE, 0);
1570 $$ = build_new_loop (body);
1571 IS_FOR_LOOP_P ($$) = 1;
1572 /* The loop is added to the current block the for
1573 statement is defined within */
1574 java_method_add_stmt (current_function_decl, $$);
1575 }
1576;
1577for_init: /* Can be empty */
9bbc7d9f 1578 { $$ = empty_stmt_node; }
e04a16fb
AG
1579| statement_expression_list
1580 {
1581 /* Init statement recorded within the previously
1582 defined block scope */
1583 $$ = java_method_add_stmt (current_function_decl, $1);
1584 }
1585| local_variable_declaration
1586 {
1587 /* Local variable are recorded within the previously
1588 defined block scope */
1589 $$ = NULL_TREE;
1590 }
1591| statement_expression_list error
1592 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1593;
1594
1595for_update: /* Can be empty */
9bbc7d9f 1596 {$$ = empty_stmt_node;}
e04a16fb
AG
1597| statement_expression_list
1598 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1599;
1600
1601statement_expression_list:
1602 statement_expression
1603 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1604| statement_expression_list C_TK statement_expression
1605 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1606| statement_expression_list C_TK error
1607 {yyerror ("Missing term"); RECOVER;}
1608;
1609
1610break_statement:
1611 BREAK_TK SC_TK
1612 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1613| BREAK_TK identifier SC_TK
1614 { $$ = build_bc_statement ($1.location, 1, $2); }
1615| BREAK_TK error
1616 {yyerror ("Missing term"); RECOVER;}
1617| BREAK_TK identifier error
1618 {yyerror ("';' expected"); RECOVER;}
1619;
1620
1621continue_statement:
1622 CONTINUE_TK SC_TK
1623 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1624| CONTINUE_TK identifier SC_TK
1625 { $$ = build_bc_statement ($1.location, 0, $2); }
1626| CONTINUE_TK error
1627 {yyerror ("Missing term"); RECOVER;}
1628| CONTINUE_TK identifier error
1629 {yyerror ("';' expected"); RECOVER;}
1630;
1631
1632return_statement:
1633 RETURN_TK SC_TK
1634 { $$ = build_return ($1.location, NULL_TREE); }
1635| RETURN_TK expression SC_TK
1636 { $$ = build_return ($1.location, $2); }
1637| RETURN_TK error
1638 {yyerror ("Missing term"); RECOVER;}
1639| RETURN_TK expression error
1640 {yyerror ("';' expected"); RECOVER;}
1641;
1642
1643throw_statement:
1644 THROW_TK expression SC_TK
b9f7e36c
APB
1645 {
1646 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1647 EXPR_WFL_LINECOL ($$) = $1.location;
1648 }
e04a16fb
AG
1649| THROW_TK error
1650 {yyerror ("Missing term"); RECOVER;}
1651| THROW_TK expression error
1652 {yyerror ("';' expected"); RECOVER;}
1653;
1654
1655synchronized_statement:
1656 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1657 {
1658 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1659 EXPR_WFL_LINECOL ($$) =
1660 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1661 }
e04a16fb
AG
1662| synchronized OP_TK expression CP_TK error
1663 {yyerror ("'{' expected"); RECOVER;}
1664| synchronized error
1665 {yyerror ("'(' expected"); RECOVER;}
1666| synchronized OP_TK error CP_TK
1667 {yyerror ("Missing term"); RECOVER;}
1668| synchronized OP_TK error
1669 {yyerror ("Missing term"); RECOVER;}
1670;
1671
b9f7e36c 1672synchronized:
e04a16fb
AG
1673 MODIFIER_TK
1674 {
b9f7e36c
APB
1675 if ((1 << $1) != ACC_SYNCHRONIZED)
1676 fatal ("synchronized was '%d' - yyparse", (1 << $1));
e04a16fb
AG
1677 }
1678;
1679
1680try_statement:
1681 TRY_TK block catches
a7d8d81f 1682 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1683| TRY_TK block finally
a7d8d81f 1684 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1685| TRY_TK block catches finally
2aa11e97
APB
1686 { $$ = build_try_finally_statement
1687 ($1.location, build_try_statement ($1.location,
1688 $2, $3), $4);
1689 }
e04a16fb
AG
1690| TRY_TK error
1691 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1692;
1693
1694catches:
1695 catch_clause
1696| catches catch_clause
b67d701b
PB
1697 {
1698 TREE_CHAIN ($2) = $1;
1699 $$ = $2;
1700 }
e04a16fb
AG
1701;
1702
1703catch_clause:
b67d701b
PB
1704 catch_clause_parameter block
1705 {
1706 java_method_add_stmt (current_function_decl, $2);
1707 exit_block ();
1708 $$ = $1;
1709 }
1710
1711catch_clause_parameter:
1712 CATCH_TK OP_TK formal_parameter CP_TK
1713 {
1714 /* We add a block to define a scope for
1715 formal_parameter (CCBP). The formal parameter is
1716 declared initialized by the appropriate function
1717 call */
1718 tree ccpb = enter_block ();
1719 tree init = build_assignment (ASSIGN_TK, $2.location,
1720 TREE_PURPOSE ($3),
1721 soft_exceptioninfo_call_node);
1722 declare_local_variables (0, TREE_VALUE ($3),
1723 build_tree_list (TREE_PURPOSE ($3),
1724 init));
1725 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1726 EXPR_WFL_LINECOL ($$) = $1.location;
1727 }
e04a16fb
AG
1728| CATCH_TK error
1729 {yyerror ("'(' expected"); RECOVER;}
e04a16fb 1730| CATCH_TK OP_TK error
b67d701b
PB
1731 {yyerror ("Missing term or ')' expected"); DRECOVER (2);}
1732| CATCH_TK OP_TK error CP_TK /* That's for () */
1733 {yyerror ("')' expected"); DRECOVER (1);}
e04a16fb
AG
1734;
1735
1736finally:
1737 FINALLY_TK block
a7d8d81f 1738 { $$ = $2; }
e04a16fb
AG
1739| FINALLY_TK error
1740 {yyerror ("'{' expected"); RECOVER; }
1741;
1742
1743/* 19.12 Production from 15: Expressions */
1744primary:
1745 primary_no_new_array
1746| array_creation_expression
1747;
1748
1749primary_no_new_array:
1750 literal
1751| THIS_TK
1752 { $$ = build_this ($1.location); }
1753| OP_TK expression CP_TK
1754 {$$ = $2;}
1755| class_instance_creation_expression
1756| field_access
1757| method_invocation
1758| array_access
1759 /* type DOT_TK CLASS_TK doens't work. So we split the rule
1760 'type' into its components. Missing is something for array,
1761 which will complete the reference_type part. FIXME */
1762| name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
c877974e 1763 { $$ = parse_jdk1_1_error ("named class literals"); }
e04a16fb 1764| primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
c877974e 1765 { $$ = build_class_ref ($1); }
e04a16fb 1766| VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
c877974e 1767 { $$ = build_class_ref (void_type_node); }
e04a16fb
AG
1768 /* Added, JDK1.1 inner classes. Documentation is wrong
1769 refering to a 'ClassName' (class_name) rule that doesn't
1770 exist. Used name instead. */
1771| name DOT_TK THIS_TK
b67d701b 1772 { $$ = parse_jdk1_1_error ("class literals"); }
e04a16fb
AG
1773| OP_TK expression error
1774 {yyerror ("')' expected"); RECOVER;}
1775| name DOT_TK error
1776 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1777| primitive_type DOT_TK error
1778 {yyerror ("'class' expected" ); RECOVER;}
1779| VOID_TK DOT_TK error
1780 {yyerror ("'class' expected" ); RECOVER;}
1781;
1782
1783class_instance_creation_expression:
1784 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1785 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1786| NEW_TK class_type OP_TK CP_TK
b67d701b 1787 { $$ = build_new_invocation ($2, NULL_TREE); }
e04a16fb
AG
1788 /* Added, JDK1.1 inner classes but modified to use
1789 'class_type' instead of 'TypeName' (type_name) mentionned
1790 in the documentation but doesn't exist. */
1791| NEW_TK class_type OP_TK argument_list CP_TK class_body
b67d701b 1792 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
e04a16fb 1793| NEW_TK class_type OP_TK CP_TK class_body
b67d701b 1794 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
e04a16fb
AG
1795 /* Added, JDK1.1 inner classes, modified to use name or
1796 primary instead of primary solely which couldn't work in
1797 all situations. */
1798| something_dot_new identifier OP_TK CP_TK
1799| something_dot_new identifier OP_TK CP_TK class_body
1800| something_dot_new identifier OP_TK argument_list CP_TK
1801| something_dot_new identifier OP_TK argument_list CP_TK class_body
1802| NEW_TK error SC_TK
1803 {yyerror ("'(' expected"); DRECOVER(new_1);}
1804| NEW_TK class_type error
1805 {yyerror ("'(' expected"); RECOVER;}
1806| NEW_TK class_type OP_TK error
1807 {yyerror ("')' or term expected"); RECOVER;}
1808| NEW_TK class_type OP_TK argument_list error
1809 {yyerror ("')' expected"); RECOVER;}
1810| something_dot_new error
1811 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1812| something_dot_new identifier error
1813 {yyerror ("'(' expected"); RECOVER;}
1814;
1815
1816something_dot_new: /* Added, not part of the specs. */
1817 name DOT_TK NEW_TK
1818| primary DOT_TK NEW_TK
1819;
1820
1821argument_list:
1822 expression
1823 {
1824 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
1825 ctxp->formal_parameter_number = 1;
1826 }
1827| argument_list C_TK expression
1828 {
1829 ctxp->formal_parameter_number += 1;
1830 $$ = tree_cons (NULL_TREE, $3, $1);
1831 }
1832| argument_list C_TK error
1833 {yyerror ("Missing term"); RECOVER;}
1834;
1835
1836array_creation_expression:
1837 NEW_TK primitive_type dim_exprs
1838 { $$ = build_newarray_node ($2, $3, 0); }
1839| NEW_TK class_or_interface_type dim_exprs
1840 { $$ = build_newarray_node ($2, $3, 0); }
1841| NEW_TK primitive_type dim_exprs dims
ba179f9f 1842 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb 1843| NEW_TK class_or_interface_type dim_exprs dims
ba179f9f 1844 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb
AG
1845 /* Added, JDK1.1 anonymous array. Initial documentation rule
1846 modified */
1847| NEW_TK class_or_interface_type dims array_initializer
b67d701b 1848 { $$ = parse_jdk1_1_error ("anonymous array"); }
e04a16fb 1849| NEW_TK primitive_type dims array_initializer
b67d701b 1850 { $$ = parse_jdk1_1_error ("anonymous array"); }
e04a16fb
AG
1851| NEW_TK error CSB_TK
1852 {yyerror ("'[' expected"); DRECOVER ("]");}
1853| NEW_TK error OSB_TK
1854 {yyerror ("']' expected"); RECOVER;}
1855;
1856
1857dim_exprs:
1858 dim_expr
1859 { $$ = build_tree_list (NULL_TREE, $1); }
1860| dim_exprs dim_expr
1861 { $$ = tree_cons (NULL_TREE, $2, $$); }
1862;
1863
1864dim_expr:
1865 OSB_TK expression CSB_TK
1866 {
1867 EXPR_WFL_LINECOL ($2) = $1.location;
1868 $$ = $2;
1869 }
1870| OSB_TK expression error
1871 {yyerror ("']' expected"); RECOVER;}
1872| OSB_TK error
1873 {
1874 yyerror ("Missing term");
1875 yyerror ("']' expected");
1876 RECOVER;
1877 }
1878;
1879
1880dims:
1881 OSB_TK CSB_TK
ba179f9f
APB
1882 {
1883 int allocate = 0;
1884 /* If not initialized, allocate memory for the osb
1885 numbers stack */
1886 if (!ctxp->osb_limit)
1887 {
1888 allocate = ctxp->osb_limit = 32;
1889 ctxp->osb_depth = -1;
1890 }
1891 /* If capacity overflown, reallocate a bigger chuck */
1892 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
1893 allocate = ctxp->osb_limit << 1;
1894
1895 if (allocate)
1896 {
1897 allocate *= sizeof (int);
1898 if (ctxp->osb_number)
1899 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
1900 allocate);
1901 else
1902 ctxp->osb_number = (int *)xmalloc (allocate);
1903 }
1904 ctxp->osb_depth++;
1905 CURRENT_OSB (ctxp) = 1;
1906 }
e04a16fb 1907| dims OSB_TK CSB_TK
ba179f9f 1908 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
1909| dims OSB_TK error
1910 { yyerror ("']' expected"); RECOVER;}
1911;
1912
1913field_access:
1914 primary DOT_TK identifier
1915 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
1916 /* FIXME - REWRITE TO:
1917 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
1918| SUPER_TK DOT_TK identifier
1919 {
1920 tree super_wfl =
9ee9b555 1921 build_wfl_node (super_identifier_node);
e04a16fb
AG
1922 EXPR_WFL_LINECOL (super_wfl) = $1.location;
1923 $$ = make_qualified_name (super_wfl, $3, $2.location);
1924 }
1925| SUPER_TK error
1926 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
1927;
1928
1929method_invocation:
1930 name OP_TK CP_TK
1931 { $$ = build_method_invocation ($1, NULL_TREE); }
1932| name OP_TK argument_list CP_TK
1933 { $$ = build_method_invocation ($1, $3); }
1934| primary DOT_TK identifier OP_TK CP_TK
1935 {
22eed1e6
APB
1936 if (TREE_CODE ($1) == THIS_EXPR)
1937 $$ = build_this_super_qualified_invocation
1938 (1, $3, NULL_TREE, 0, $2.location);
1939 else
1940 {
1941 tree invok = build_method_invocation ($3, NULL_TREE);
1942 $$ = make_qualified_primary ($1, invok, $2.location);
1943 }
e04a16fb
AG
1944 }
1945| primary DOT_TK identifier OP_TK argument_list CP_TK
1946 {
22eed1e6
APB
1947 if (TREE_CODE ($1) == THIS_EXPR)
1948 $$ = build_this_super_qualified_invocation
1949 (1, $3, $5, 0, $2.location);
1950 else
1951 {
1952 tree invok = build_method_invocation ($3, $5);
1953 $$ = make_qualified_primary ($1, invok, $2.location);
1954 }
e04a16fb
AG
1955 }
1956| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
1957 {
1958 $$ = build_this_super_qualified_invocation
1959 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
1960 }
1961| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
1962 {
22eed1e6
APB
1963 $$ = build_this_super_qualified_invocation
1964 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
1965 }
1966 /* Screws up thing. I let it here until I'm convinced it can
1967 be removed. FIXME
1968| primary DOT_TK error
1969 {yyerror ("'(' expected"); DRECOVER(bad);} */
1970| SUPER_TK DOT_TK error CP_TK
1971 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1972| SUPER_TK DOT_TK error DOT_TK
1973 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1974;
1975
1976array_access:
1977 name OSB_TK expression CSB_TK
1978 { $$ = build_array_ref ($2.location, $1, $3); }
1979| primary_no_new_array OSB_TK expression CSB_TK
1980 { $$ = build_array_ref ($2.location, $1, $3); }
1981| name OSB_TK error
1982 {
1983 yyerror ("Missing term and ']' expected");
1984 DRECOVER(array_access);
1985 }
1986| name OSB_TK expression error
1987 {
1988 yyerror ("']' expected");
1989 DRECOVER(array_access);
1990 }
1991| primary_no_new_array OSB_TK error
1992 {
1993 yyerror ("Missing term and ']' expected");
1994 DRECOVER(array_access);
1995 }
1996| primary_no_new_array OSB_TK expression error
1997 {
1998 yyerror ("']' expected");
1999 DRECOVER(array_access);
2000 }
2001;
2002
2003postfix_expression:
2004 primary
2005| name
2006| post_increment_expression
2007| post_decrement_expression
2008;
2009
2010post_increment_expression:
2011 postfix_expression INCR_TK
2012 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2013;
2014
2015post_decrement_expression:
2016 postfix_expression DECR_TK
2017 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2018;
2019
2020unary_expression:
2021 pre_increment_expression
2022| pre_decrement_expression
2023| PLUS_TK unary_expression
2024 {$$ = build_unaryop ($1.token, $1.location, $2); }
2025| MINUS_TK unary_expression
2026 {$$ = build_unaryop ($1.token, $1.location, $2); }
2027| unary_expression_not_plus_minus
2028| PLUS_TK error
2029 {yyerror ("Missing term"); RECOVER}
2030| MINUS_TK error
2031 {yyerror ("Missing term"); RECOVER}
2032;
2033
2034pre_increment_expression:
2035 INCR_TK unary_expression
2036 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2037| INCR_TK error
2038 {yyerror ("Missing term"); RECOVER}
2039;
2040
2041pre_decrement_expression:
2042 DECR_TK unary_expression
2043 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2044| DECR_TK error
2045 {yyerror ("Missing term"); RECOVER}
2046;
2047
2048unary_expression_not_plus_minus:
2049 postfix_expression
2050| NOT_TK unary_expression
2051 {$$ = build_unaryop ($1.token, $1.location, $2); }
2052| NEG_TK unary_expression
2053 {$$ = build_unaryop ($1.token, $1.location, $2); }
2054| cast_expression
2055| NOT_TK error
2056 {yyerror ("Missing term"); RECOVER}
2057| NEG_TK error
2058 {yyerror ("Missing term"); RECOVER}
2059;
2060
2061cast_expression: /* Error handling here is potentially weak */
2062 OP_TK primitive_type dims CP_TK unary_expression
2063 {
2064 tree type = $2;
ba179f9f 2065 while (CURRENT_OSB (ctxp)--)
e04a16fb 2066 type = build_java_array_type (type, -1);
ba179f9f 2067 ctxp->osb_depth--;
e04a16fb
AG
2068 $$ = build_cast ($1.location, type, $5);
2069 }
2070| OP_TK primitive_type CP_TK unary_expression
2071 { $$ = build_cast ($1.location, $2, $4); }
2072| OP_TK expression CP_TK unary_expression_not_plus_minus
2073 { $$ = build_cast ($1.location, $2, $4); }
2074| OP_TK name dims CP_TK unary_expression_not_plus_minus
2075 {
49f48c71 2076 const char *ptr;
ba179f9f 2077 while (CURRENT_OSB (ctxp)--)
e04a16fb 2078 obstack_1grow (&temporary_obstack, '[');
ba179f9f 2079 ctxp->osb_depth--;
e04a16fb
AG
2080 obstack_grow0 (&temporary_obstack,
2081 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2082 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2083 ptr = obstack_finish (&temporary_obstack);
2084 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2085 $$ = build_cast ($1.location, $2, $5);
2086 }
2087| OP_TK primitive_type OSB_TK error
2088 {yyerror ("']' expected, invalid type expression");}
2089| OP_TK error
2090 {
2091 if (ctxp->prevent_ese != lineno)
2092 yyerror ("Invalid type expression"); RECOVER;
2093 RECOVER;
2094 }
2095| OP_TK primitive_type dims CP_TK error
2096 {yyerror ("Missing term"); RECOVER;}
2097| OP_TK primitive_type CP_TK error
2098 {yyerror ("Missing term"); RECOVER;}
2099| OP_TK name dims CP_TK error
2100 {yyerror ("Missing term"); RECOVER;}
2101;
2102
2103multiplicative_expression:
2104 unary_expression
2105| multiplicative_expression MULT_TK unary_expression
2106 {
2107 $$ = build_binop (BINOP_LOOKUP ($2.token),
2108 $2.location, $1, $3);
2109 }
2110| multiplicative_expression DIV_TK unary_expression
2111 {
2112 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2113 $1, $3);
2114 }
2115| multiplicative_expression REM_TK unary_expression
2116 {
2117 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2118 $1, $3);
2119 }
2120| multiplicative_expression MULT_TK error
2121 {yyerror ("Missing term"); RECOVER;}
2122| multiplicative_expression DIV_TK error
2123 {yyerror ("Missing term"); RECOVER;}
2124| multiplicative_expression REM_TK error
2125 {yyerror ("Missing term"); RECOVER;}
2126;
2127
2128additive_expression:
2129 multiplicative_expression
2130| additive_expression PLUS_TK multiplicative_expression
2131 {
2132 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2133 $1, $3);
2134 }
2135| additive_expression MINUS_TK multiplicative_expression
2136 {
2137 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2138 $1, $3);
2139 }
2140| additive_expression PLUS_TK error
2141 {yyerror ("Missing term"); RECOVER;}
2142| additive_expression MINUS_TK error
2143 {yyerror ("Missing term"); RECOVER;}
2144;
2145
2146shift_expression:
2147 additive_expression
2148| shift_expression LS_TK additive_expression
2149 {
2150 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2151 $1, $3);
2152 }
2153| shift_expression SRS_TK additive_expression
2154 {
2155 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2156 $1, $3);
2157 }
2158| shift_expression ZRS_TK additive_expression
2159 {
2160 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2161 $1, $3);
2162 }
2163| shift_expression LS_TK error
2164 {yyerror ("Missing term"); RECOVER;}
2165| shift_expression SRS_TK error
2166 {yyerror ("Missing term"); RECOVER;}
2167| shift_expression ZRS_TK error
2168 {yyerror ("Missing term"); RECOVER;}
2169;
2170
2171relational_expression:
2172 shift_expression
2173| relational_expression LT_TK shift_expression
2174 {
2175 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2176 $1, $3);
2177 }
2178| relational_expression GT_TK shift_expression
2179 {
2180 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2181 $1, $3);
2182 }
2183| relational_expression LTE_TK shift_expression
2184 {
2185 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2186 $1, $3);
2187 }
2188| relational_expression GTE_TK shift_expression
2189 {
2190 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2191 $1, $3);
2192 }
2193| relational_expression INSTANCEOF_TK reference_type
5e942c50 2194 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2195| relational_expression LT_TK error
2196 {yyerror ("Missing term"); RECOVER;}
2197| relational_expression GT_TK error
2198 {yyerror ("Missing term"); RECOVER;}
2199| relational_expression LTE_TK error
2200 {yyerror ("Missing term"); RECOVER;}
2201| relational_expression GTE_TK error
2202 {yyerror ("Missing term"); RECOVER;}
2203| relational_expression INSTANCEOF_TK error
2204 {yyerror ("Invalid reference type"); RECOVER;}
2205;
2206
2207equality_expression:
2208 relational_expression
2209| equality_expression EQ_TK relational_expression
2210 {
2211 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2212 $1, $3);
2213 }
2214| equality_expression NEQ_TK relational_expression
2215 {
2216 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2217 $1, $3);
2218 }
2219| equality_expression EQ_TK error
2220 {yyerror ("Missing term"); RECOVER;}
2221| equality_expression NEQ_TK error
2222 {yyerror ("Missing term"); RECOVER;}
2223;
2224
2225and_expression:
2226 equality_expression
2227| and_expression AND_TK equality_expression
2228 {
2229 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2230 $1, $3);
2231 }
2232| and_expression AND_TK error
2233 {yyerror ("Missing term"); RECOVER;}
2234;
2235
2236exclusive_or_expression:
2237 and_expression
2238| exclusive_or_expression XOR_TK and_expression
2239 {
2240 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2241 $1, $3);
2242 }
2243| exclusive_or_expression XOR_TK error
2244 {yyerror ("Missing term"); RECOVER;}
2245;
2246
2247inclusive_or_expression:
2248 exclusive_or_expression
2249| inclusive_or_expression OR_TK exclusive_or_expression
2250 {
2251 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2252 $1, $3);
2253 }
2254| inclusive_or_expression OR_TK error
2255 {yyerror ("Missing term"); RECOVER;}
2256;
2257
2258conditional_and_expression:
2259 inclusive_or_expression
2260| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2261 {
2262 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2263 $1, $3);
2264 }
2265| conditional_and_expression BOOL_AND_TK error
2266 {yyerror ("Missing term"); RECOVER;}
2267;
2268
2269conditional_or_expression:
2270 conditional_and_expression
2271| conditional_or_expression BOOL_OR_TK conditional_and_expression
2272 {
2273 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2274 $1, $3);
2275 }
2276| conditional_or_expression BOOL_OR_TK error
2277 {yyerror ("Missing term"); RECOVER;}
2278;
2279
2280conditional_expression: /* Error handling here is weak */
2281 conditional_or_expression
2282| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2283 {
2284 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2285 EXPR_WFL_LINECOL ($$) = $2.location;
2286 }
e04a16fb
AG
2287| conditional_or_expression REL_QM_TK REL_CL_TK error
2288 {
2289 YYERROR_NOW;
2290 yyerror ("Missing term");
2291 DRECOVER (1);
2292 }
2293| conditional_or_expression REL_QM_TK error
2294 {yyerror ("Missing term"); DRECOVER (2);}
2295| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2296 {yyerror ("Missing term"); DRECOVER (3);}
2297;
2298
2299assignment_expression:
2300 conditional_expression
2301| assignment
2302;
2303
2304assignment:
2305 left_hand_side assignment_operator assignment_expression
2306 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2307| left_hand_side assignment_operator error
2308 {
2309 if (ctxp->prevent_ese != lineno)
2310 yyerror ("Missing term");
2311 DRECOVER (assign);
2312 }
2313;
2314
2315left_hand_side:
2316 name
2317| field_access
2318| array_access
2319;
2320
2321assignment_operator:
2322 ASSIGN_ANY_TK
2323| ASSIGN_TK
2324;
2325
2326expression:
2327 assignment_expression
2328;
2329
2330constant_expression:
2331 expression
2332;
2333
2334%%
2335\f
2336
e04a16fb
AG
2337/* Flag for the error report routine to issue the error the first time
2338 it's called (overriding the default behavior which is to drop the
2339 first invocation and honor the second one, taking advantage of a
2340 richer context. */
2341static int force_error = 0;
2342
2343/* Create a new parser context and make it the current one. */
2344
2345void
2346java_push_parser_context ()
2347{
2348 struct parser_ctxt *new =
23a79c61 2349 (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
e04a16fb 2350
4504ead1 2351 bzero ((PTR) new, sizeof (struct parser_ctxt));
e04a16fb
AG
2352 new->next = ctxp;
2353 ctxp = new;
2354 if (ctxp->next)
5e942c50
APB
2355 {
2356 ctxp->incomplete_class = ctxp->next->incomplete_class;
2357 ctxp->gclass_list = ctxp->next->gclass_list;
2358 }
e04a16fb
AG
2359}
2360
22eed1e6
APB
2361/* If the first file of a file list was a class file, no context
2362 exists for a source file to be parsed. This boolean remembers that
2363 java_parser_context_save_global might have created a dummy one, so
2364 that java_parser_context_restore_global can pop it. */
2365static int extra_ctxp_pushed_p = 0;
2366
e04a16fb
AG
2367void
2368java_parser_context_save_global ()
2369{
22eed1e6
APB
2370 if (!ctxp)
2371 {
2372 java_push_parser_context ();
2373 extra_ctxp_pushed_p = 1;
2374 }
e04a16fb
AG
2375 ctxp->finput = finput;
2376 ctxp->lineno = lineno;
2377 ctxp->current_class = current_class;
2378 ctxp->filename = input_filename;
2379 ctxp->current_function_decl = current_function_decl;
2380}
2381
2382void
2383java_parser_context_restore_global ()
2384{
2385 finput = ctxp->finput;
2386 lineno = ctxp->lineno;
2387 current_class = ctxp->current_class;
2388 input_filename = ctxp->filename;
2389 current_function_decl = ctxp->current_function_decl;
23a79c61 2390 if (!ctxp->next && extra_ctxp_pushed_p)
22eed1e6
APB
2391 {
2392 java_pop_parser_context (0);
2393 extra_ctxp_pushed_p = 0;
2394 }
e04a16fb
AG
2395}
2396
2397void
b351b287
APB
2398java_pop_parser_context (generate)
2399 int generate;
e04a16fb
AG
2400{
2401 tree current;
5e942c50 2402 struct parser_ctxt *toFree, *next;
e04a16fb 2403
5e942c50
APB
2404 if (!ctxp)
2405 return;
2406
2407 toFree = ctxp;
2408 next = ctxp->next;
e04a16fb
AG
2409 if (next)
2410 {
2411 next->incomplete_class = ctxp->incomplete_class;
5e942c50 2412 next->gclass_list = ctxp->gclass_list;
e04a16fb
AG
2413 lineno = ctxp->lineno;
2414 finput = ctxp->finput;
2415 current_class = ctxp->current_class;
2416 }
2417
2418 /* Set the single import class file flag to 0 for the current list
2419 of imported things */
2420 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2421 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2422
2423 /* And restore those of the previous context */
0a2138e2 2424 if ((ctxp = next)) /* Assignment is really meant here */
e04a16fb
AG
2425 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2426 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2427
b351b287
APB
2428 if (generate)
2429 {
2430 toFree->next = ctxp_for_generation;
2431 ctxp_for_generation = toFree;
2432 }
2433 else
2434 free (toFree);
e04a16fb
AG
2435}
2436
b67d701b
PB
2437/* Reporting JDK1.1 features not implemented */
2438
2439static tree
2440parse_jdk1_1_error (msg)
49f48c71 2441 const char *msg;
b67d701b
PB
2442{
2443 sorry (": `%s' JDK1.1(TM) feature", msg);
2444 java_error_count++;
9bbc7d9f 2445 return empty_stmt_node;
b67d701b
PB
2446}
2447
e04a16fb
AG
2448static int do_warning = 0;
2449
2450void
2451yyerror (msg)
49f48c71 2452 const char *msg;
e04a16fb
AG
2453{
2454 static java_lc elc;
2455 static int prev_lineno;
49f48c71 2456 static const char *prev_msg;
e04a16fb 2457
0a2138e2 2458 int save_lineno;
e04a16fb
AG
2459 char *remainder, *code_from_source;
2460 extern struct obstack temporary_obstack;
2461
2462 if (!force_error && prev_lineno == lineno)
2463 return;
2464
2465 /* Save current error location but report latter, when the context is
2466 richer. */
2467 if (ctxp->java_error_flag == 0)
2468 {
2469 ctxp->java_error_flag = 1;
2470 elc = ctxp->elc;
2471 /* Do something to use the previous line if we're reaching the
2472 end of the file... */
2473#ifdef VERBOSE_SKELETON
2474 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2475#endif
2476 return;
2477 }
2478
2479 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2480 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2481 return;
2482
2483 ctxp->java_error_flag = 0;
2484 if (do_warning)
2485 java_warning_count++;
2486 else
2487 java_error_count++;
2488
2489 if (elc.col == 0 && msg[1] == ';')
2490 {
2491 elc.col = ctxp->p_line->char_col-1;
2492 elc.line = ctxp->p_line->lineno;
2493 }
2494
2495 save_lineno = lineno;
2496 prev_lineno = lineno = elc.line;
2497 prev_msg = msg;
2498
2499 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2500 obstack_grow0 (&temporary_obstack,
2501 code_from_source, strlen (code_from_source));
2502 remainder = obstack_finish (&temporary_obstack);
2503 if (do_warning)
2504 warning ("%s.\n%s", msg, remainder);
2505 else
2506 error ("%s.\n%s", msg, remainder);
2507
2508 /* This allow us to cheaply avoid an extra 'Invalid expression
2509 statement' error report when errors have been already reported on
2510 the same line. This occurs when we report an error but don't have
2511 a synchronization point other than ';', which
2512 expression_statement is the only one to take care of. */
2513 ctxp->prevent_ese = lineno = save_lineno;
2514}
2515
2516static void
15fdcfe9 2517issue_warning_error_from_context (cl, msg, ap)
5e942c50 2518 tree cl;
d4476be2 2519 const char *msg;
15fdcfe9 2520 va_list ap;
5e942c50 2521{
1886c9d8 2522 char *saved, *saved_input_filename;
15fdcfe9
PB
2523 char buffer [4096];
2524 vsprintf (buffer, msg, ap);
2525 force_error = 1;
5e942c50
APB
2526
2527 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
2528 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2529 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
2530
2531 /* We have a CL, that's a good reason for using it if it contains data */
2532 saved = ctxp->filename;
2533 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2534 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
2535 saved_input_filename = input_filename;
2536 input_filename = ctxp->filename;
15fdcfe9
PB
2537 java_error (NULL);
2538 java_error (buffer);
5e942c50 2539 ctxp->filename = saved;
1886c9d8 2540 input_filename = saved_input_filename;
15fdcfe9 2541 force_error = 0;
5e942c50
APB
2542}
2543
e04a16fb
AG
2544/* Issue an error message at a current source line CL */
2545
15fdcfe9 2546void
d4476be2 2547parse_error_context VPROTO ((tree cl, const char *msg, ...))
e04a16fb 2548{
d4476be2 2549#ifndef ANSI_PROTOTYPES
e04a16fb 2550 tree cl;
d4476be2 2551 const char *msg;
e04a16fb 2552#endif
e04a16fb
AG
2553 va_list ap;
2554
2555 VA_START (ap, msg);
d4476be2 2556#ifndef ANSI_PROTOTYPES
e04a16fb 2557 cl = va_arg (ap, tree);
d4476be2 2558 msg = va_arg (ap, const char *);
e04a16fb 2559#endif
15fdcfe9
PB
2560 issue_warning_error_from_context (cl, msg, ap);
2561 va_end (ap);
e04a16fb
AG
2562}
2563
2564/* Issue a warning at a current source line CL */
2565
2566static void
d4476be2 2567parse_warning_context VPROTO ((tree cl, const char *msg, ...))
e04a16fb 2568{
d4476be2 2569#ifndef ANSI_PROTOTYPES
e04a16fb 2570 tree cl;
d4476be2 2571 const char *msg;
e04a16fb 2572#endif
e04a16fb
AG
2573 va_list ap;
2574
2575 VA_START (ap, msg);
d4476be2 2576#ifndef ANSI_PROTOTYPES
e04a16fb 2577 cl = va_arg (ap, tree);
d4476be2 2578 msg = va_arg (ap, const char *);
e04a16fb 2579#endif
e04a16fb 2580
c877974e 2581 force_error = do_warning = 1;
15fdcfe9 2582 issue_warning_error_from_context (cl, msg, ap);
c877974e 2583 do_warning = force_error = 0;
15fdcfe9 2584 va_end (ap);
e04a16fb
AG
2585}
2586
82371d41
APB
2587static tree
2588find_expr_with_wfl (node)
2589 tree node;
2590{
2591 while (node)
2592 {
2593 char code;
2594 tree to_return;
2595
2596 switch (TREE_CODE (node))
2597 {
2598 case BLOCK:
c0d87ff6
PB
2599 node = BLOCK_EXPR_BODY (node);
2600 continue;
82371d41
APB
2601
2602 case COMPOUND_EXPR:
2603 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
2604 if (to_return)
2605 return to_return;
c0d87ff6
PB
2606 node = TREE_OPERAND (node, 1);
2607 continue;
82371d41
APB
2608
2609 case LOOP_EXPR:
c0d87ff6
PB
2610 node = TREE_OPERAND (node, 0);
2611 continue;
82371d41
APB
2612
2613 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
2614 node = TREE_OPERAND (node, 1);
2615 continue;
2616
82371d41
APB
2617 default:
2618 code = TREE_CODE_CLASS (TREE_CODE (node));
2619 if (((code == '1') || (code == '2') || (code == 'e'))
2620 && EXPR_WFL_LINECOL (node))
2621 return node;
ba179f9f 2622 return NULL_TREE;
82371d41
APB
2623 }
2624 }
2625 return NULL_TREE;
2626}
2627
2628/* Issue a missing return statement error. Uses METHOD to figure the
2629 last line of the method the error occurs in. */
2630
2631static void
2632missing_return_error (method)
2633 tree method;
2634{
2635 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
2636 parse_error_context (wfl_operator, "Missing return statement");
2637}
2638
2639/* Issue an unreachable statement error. From NODE, find the next
2640 statement to report appropriately. */
2641static void
2642unreachable_stmt_error (node)
2643 tree node;
2644{
2645 /* Browse node to find the next expression node that has a WFL. Use
2646 the location to report the error */
2647 if (TREE_CODE (node) == COMPOUND_EXPR)
2648 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
2649 else
2650 node = find_expr_with_wfl (node);
2651
2652 if (node)
2653 {
2654 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
2655 parse_error_context (wfl_operator, "Unreachable statement");
2656 }
2657 else
2658 fatal ("Can't get valid statement - unreachable_stmt_error");
2659}
2660
c877974e 2661int
e04a16fb
AG
2662java_report_errors ()
2663{
2664 if (java_error_count)
2665 fprintf (stderr, "%d error%s",
2666 java_error_count, (java_error_count == 1 ? "" : "s"));
2667 if (java_warning_count)
2668 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
2669 java_warning_count, (java_warning_count == 1 ? "" : "s"));
2670 if (java_error_count || java_warning_count)
2671 putc ('\n', stderr);
c877974e 2672 return java_error_count;
e04a16fb
AG
2673}
2674
2675static char *
2676java_accstring_lookup (flags)
2677 int flags;
2678{
2679 static char buffer [80];
2680#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
2681
2682 /* Access modifier looked-up first for easier report on forbidden
2683 access. */
2684 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
2685 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
2686 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
2687 if (flags & ACC_STATIC) COPY_RETURN ("static");
2688 if (flags & ACC_FINAL) COPY_RETURN ("final");
2689 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
2690 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
2691 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
2692 if (flags & ACC_NATIVE) COPY_RETURN ("native");
2693 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
2694 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
2695
2696 buffer [0] = '\0';
2697 return buffer;
2698#undef COPY_RETURN
2699}
2700
b67d701b
PB
2701/* Issuing error messages upon redefinition of classes, interfaces or
2702 variables. */
2703
e04a16fb 2704static void
b67d701b 2705classitf_redefinition_error (context, id, decl, cl)
49f48c71 2706 const char *context;
e04a16fb
AG
2707 tree id, decl, cl;
2708{
2709 parse_error_context (cl, "%s `%s' already defined in %s:%d",
2710 context, IDENTIFIER_POINTER (id),
2711 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
2712 /* Here we should point out where its redefined. It's a unicode. FIXME */
2713}
2714
b67d701b
PB
2715static void
2716variable_redefinition_error (context, name, type, line)
2717 tree context, name, type;
2718 int line;
2719{
49f48c71 2720 const char *type_name;
b67d701b
PB
2721
2722 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
2723 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
2724 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 2725 else
0a2138e2 2726 type_name = lang_printable_name (type, 0);
b67d701b
PB
2727
2728 parse_error_context (context,
2729 "Variable `%s' is already defined in this method and "
2730 "was declared `%s %s' at line %d",
2731 IDENTIFIER_POINTER (name),
2732 type_name, IDENTIFIER_POINTER (name), line);
2733}
2734
c583dd46
APB
2735static tree
2736build_array_from_name (type, type_wfl, name, ret_name)
2737 tree type, type_wfl, name, *ret_name;
2738{
2739 int more_dims = 0;
49f48c71 2740 const char *string;
c583dd46
APB
2741
2742 /* Eventually get more dims */
2743 string = IDENTIFIER_POINTER (name);
2744 while (string [more_dims] == '[')
2745 more_dims++;
2746
2747 /* If we have, then craft a new type for this variable */
2748 if (more_dims)
2749 {
c0d87ff6 2750 name = get_identifier (&string [more_dims]);
c583dd46 2751
34f4db93
APB
2752 /* If we have a pointer, use its type */
2753 if (TREE_CODE (type) == POINTER_TYPE)
2754 type = TREE_TYPE (type);
c583dd46
APB
2755
2756 /* Building the first dimension of a primitive type uses this
2757 function */
2758 if (JPRIMITIVE_TYPE_P (type))
2759 {
2760 type = build_java_array_type (type, -1);
22eed1e6 2761 CLASS_LOADED_P (type) = 1;
c583dd46
APB
2762 more_dims--;
2763 }
2764 /* Otherwise, if we have a WFL for this type, use it (the type
2765 is already an array on an unresolved type, and we just keep
2766 on adding dimensions) */
2767 else if (type_wfl)
2768 type = type_wfl;
2769
2770 /* Add all the dimensions */
2771 while (more_dims--)
2772 type = build_unresolved_array_type (type);
2773
2774 /* The type may have been incomplete in the first place */
2775 if (type_wfl)
2776 type = obtain_incomplete_type (type);
2777 }
2778
2779 *ret_name = name;
2780 return type;
2781}
2782
e04a16fb
AG
2783/* Build something that the type identifier resolver will identify as
2784 being an array to an unresolved type. TYPE_WFL is a WFL on a
2785 identifier. */
2786
2787static tree
2788build_unresolved_array_type (type_or_wfl)
2789 tree type_or_wfl;
2790{
49f48c71 2791 const char *ptr;
e04a16fb 2792
1886c9d8 2793 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
2794 just create a array type */
2795 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
2796 {
2797 tree type = build_java_array_type (type_or_wfl, -1);
2798 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
2799 return type;
2800 }
2801
2802 obstack_1grow (&temporary_obstack, '[');
2803 obstack_grow0 (&temporary_obstack,
2804 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
2805 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
2806 ptr = obstack_finish (&temporary_obstack);
2807 return build_expr_wfl (get_identifier (ptr),
2808 EXPR_WFL_FILENAME (type_or_wfl),
2809 EXPR_WFL_LINENO (type_or_wfl),
2810 EXPR_WFL_COLNO (type_or_wfl));
2811}
2812
2813/* Check modifiers. If one doesn't fit, retrieve it in its declaration line
2814 and point it out. */
2815
2816static void
2817check_modifiers (message, value, mask)
49f48c71 2818 const char *message;
e04a16fb
AG
2819 int value;
2820 int mask;
2821{
2822 /* Should point out the one that don't fit. ASCII/unicode,
2823 going backward. FIXME */
2824 if (value & ~mask)
2825 {
2826 int i, remainder = value & ~mask;
2827 for (i = 0; i <= 10; i++)
2828 if ((1 << i) & remainder)
2829 parse_error_context (ctxp->modifier_ctx [i], message,
2830 java_accstring_lookup (1 << i));
2831 }
2832}
2833
2834static void
2835parser_add_interface (class_decl, interface_decl, wfl)
2836 tree class_decl, interface_decl, wfl;
2837{
2838 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
2839 parse_error_context (wfl, "Interface `%s' repeated",
2840 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
2841}
2842
2843/* Bulk of common class/interface checks. Return 1 if an error was
2844 encountered. TAG is 0 for a class, 1 for an interface. */
2845
2846static int
2847check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
2848 int is_interface, flags;
2849 tree raw_name, qualified_name, decl, cl;
2850{
2851 tree node;
2852
2853 if (!quiet_flag)
2854 fprintf (stderr, " %s %s", (is_interface ? "interface" : "class"),
2855 IDENTIFIER_POINTER (qualified_name));
2856
2857 /* Scope of an interface/class type name:
2858 - Can't be imported by a single type import
2859 - Can't already exists in the package */
2860 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
2861 && (node = find_name_in_single_imports (raw_name)))
2862 {
2863 parse_error_context
2864 (cl, "%s name `%s' clashes with imported type `%s'",
2865 (is_interface ? "Interface" : "Class"),
2866 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
2867 return 1;
2868 }
2869 if (decl && CLASS_COMPLETE_P (decl))
2870 {
b67d701b
PB
2871 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
2872 qualified_name, decl, cl);
e04a16fb
AG
2873 return 1;
2874 }
2875
2876 /* If public, file name should match class/interface name */
2877 if (flags & ACC_PUBLIC)
2878 {
49f48c71 2879 const char *f;
e04a16fb
AG
2880
2881 /* Contains OS dependent assumption on path separator. FIXME */
2882 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
2883 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
2884 f--)
2885 ;
847fe791 2886 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
2887 f++;
2888 if (strncmp (IDENTIFIER_POINTER (raw_name),
2889 f , IDENTIFIER_LENGTH (raw_name)) ||
2890 f [IDENTIFIER_LENGTH (raw_name)] != '.')
2891 parse_error_context (cl, "Public %s `%s' must be defined in a file "
2892 "called `%s.java'",
2893 (is_interface ? "interface" : "class"),
2894 IDENTIFIER_POINTER (qualified_name),
2895 IDENTIFIER_POINTER (raw_name));
2896 }
2897
2898 check_modifiers ((is_interface ?
2899 "Illegal modifier `%s' for interface declaration" :
2900 "Illegal modifier `%s' for class declaration"), flags,
2901 (is_interface ? INTERFACE_MODIFIERS : CLASS_MODIFIERS));
2902 return 0;
2903}
2904
2905/* If DECL is NULL, create and push a new DECL, record the current
2906 line CL and do other maintenance things. */
2907
2908static tree
2909maybe_create_class_interface_decl (decl, qualified_name, cl)
2910 tree decl, qualified_name, cl;
2911{
5e942c50 2912 if (!decl)
e04a16fb
AG
2913 decl = push_class (make_class (), qualified_name);
2914
2915 /* Take care of the file and line business */
2916 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
2917 /* If we're emiting xrefs, store the line/col number information */
2918 if (flag_emit_xref)
2919 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
2920 else
2921 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 2922 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
2923 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
2924 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb
AG
2925
2926 ctxp->current_parsed_class = decl;
2927
2928 /* Link the declaration to the already seen ones */
2929 TREE_CHAIN (decl) = ctxp->class_list;
2930 ctxp->class_list = decl;
5e942c50 2931
23a79c61 2932 /* Create a new nodes in the global lists */
5e942c50 2933 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 2934 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 2935
e04a16fb
AG
2936 /* Install a new dependency list element */
2937 create_jdep_list (ctxp);
2938
2939 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
2940 IDENTIFIER_POINTER (qualified_name)));
2941 return decl;
2942}
2943
2944static void
2945add_superinterfaces (decl, interface_list)
2946 tree decl, interface_list;
2947{
2948 tree node;
2949 /* Superinterface(s): if present and defined, parser_check_super_interface ()
2950 takes care of ensuring that:
2951 - This is an accessible interface type,
2952 - Circularity detection.
2953 parser_add_interface is then called. If present but not defined,
2954 the check operation is delayed until the super interface gets
2955 defined. */
2956 for (node = interface_list; node; node = TREE_CHAIN (node))
2957 {
15fdcfe9 2958 tree current = TREE_PURPOSE (node);
5e942c50
APB
2959 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
2960 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 2961 {
5e942c50
APB
2962 if (!parser_check_super_interface (idecl, decl, current))
2963 parser_add_interface (decl, idecl, current);
e04a16fb
AG
2964 }
2965 else
2966 register_incomplete_type (JDEP_INTERFACE,
2967 current, decl, NULL_TREE);
2968 }
2969}
2970
2971/* Create an interface in pass1 and return its decl. Return the
2972 interface's decl in pass 2. */
2973
2974static tree
2975create_interface (flags, id, super)
2976 int flags;
2977 tree id, super;
2978{
e04a16fb
AG
2979 tree raw_name = EXPR_WFL_NODE (id);
2980 tree q_name = parser_qualified_classname (id);
2981 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
2982
2983 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
2984
2985 /* Basic checks: scope, redefinition, modifiers */
2986 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
2987 return NULL_TREE;
2988
2989 /* Interface modifiers check
2990 - public/abstract allowed (already done at that point)
2991 - abstract is obsolete (comes first, it's a warning, or should be)
2992 - Can't use twice the same (checked in the modifier rule) */
c877974e 2993 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
2994 parse_warning_context
2995 (MODIFIER_WFL (ABSTRACT_TK),
c877974e 2996 "Redundant use of `abstract' modifier. Interface `%s' is implicitely "
e04a16fb 2997 "abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
2998
2999 /* Create a new decl if DECL is NULL, otherwise fix it */
3000 decl = maybe_create_class_interface_decl (decl, q_name, id);
3001
3002 /* Set super info and mark the class a complete */
2aa11e97 3003 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3004 object_type_node, ctxp->interface_number);
3005 ctxp->interface_number = 0;
3006 CLASS_COMPLETE_P (decl) = 1;
3007 add_superinterfaces (decl, super);
3008
3009 return decl;
3010}
3011
3012/* Create an class in pass1 and return its decl. Return class
3013 interface's decl in pass 2. */
3014
3015static tree
3016create_class (flags, id, super, interfaces)
3017 int flags;
3018 tree id, super, interfaces;
3019{
e04a16fb
AG
3020 tree raw_name = EXPR_WFL_NODE (id);
3021 tree class_id, decl;
9ee9b555 3022 tree super_decl_type;
e04a16fb
AG
3023
3024 class_id = parser_qualified_classname (id);
3025 decl = IDENTIFIER_CLASS_VALUE (class_id);
5e942c50 3026 ctxp->current_parsed_class_un = EXPR_WFL_NODE (id);
e04a16fb
AG
3027 EXPR_WFL_NODE (id) = class_id;
3028
3029 /* Basic check: scope, redefinition, modifiers */
3030 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3031 return NULL_TREE;
3032
3033 /* Class modifier check:
3034 - Allowed modifier (already done at that point)
3035 - abstract AND final forbidden
3036 - Public classes defined in the correct file */
3037 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3038 parse_error_context (id, "Class `%s' can't be declared both abstract "
3039 "and final", IDENTIFIER_POINTER (raw_name));
3040
3041 /* Create a new decl if DECL is NULL, otherwise fix it */
3042 decl = maybe_create_class_interface_decl (decl, class_id, id);
3043
3044 /* If SUPER exists, use it, otherwise use Object */
3045 if (super)
3046 {
3047 /* Can't extend java.lang.Object */
3048 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3049 {
3050 parse_error_context (id, "Can't extend `java.lang.Object'");
3051 return NULL_TREE;
3052 }
3053
2c3199bc
PB
3054 super_decl_type =
3055 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3056 }
3057 else if (TREE_TYPE (decl) != object_type_node)
3058 super_decl_type = object_type_node;
3059 /* We're defining java.lang.Object */
3060 else
3061 super_decl_type = NULL_TREE;
3062
3063 /* Set super info and mark the class a complete */
3064 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3065 ctxp->interface_number);
3066 ctxp->interface_number = 0;
3067 CLASS_COMPLETE_P (decl) = 1;
3068 add_superinterfaces (decl, interfaces);
3069
7f10c2e2
APB
3070 /* If doing xref, store the location at which the inherited class
3071 (if any) was seen. */
3072 if (flag_emit_xref && super)
3073 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3074
5e942c50
APB
3075 /* Eventually sets the @deprecated tag flag */
3076 CHECK_DEPRECATED (decl);
3077
e04a16fb
AG
3078 return decl;
3079}
3080
3081/* Can't use lookup_field () since we don't want to load the class and
3082 can't set the CLASS_LOADED_P flag */
3083
3084static tree
3085find_field (class, name)
3086 tree class;
3087 tree name;
3088{
3089 tree decl;
3090 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3091 {
3092 if (DECL_NAME (decl) == name)
3093 return decl;
3094 }
3095 return NULL_TREE;
3096}
3097
3098/* Wrap around lookup_field that doesn't potentially upset the value
3099 of CLASS */
3100
3101static tree
3102lookup_field_wrapper (class, name)
3103 tree class, name;
3104{
3105 tree type = class;
5b09b33e 3106 tree decl;
c877974e 3107 java_parser_context_save_global ();
5b09b33e 3108 decl = lookup_field (&type, name);
c877974e 3109 java_parser_context_restore_global ();
93024893 3110 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
3111}
3112
3113/* Find duplicate field within the same class declarations and report
c583dd46
APB
3114 the error. Returns 1 if a duplicated field was found, 0
3115 otherwise. */
e04a16fb
AG
3116
3117static int
c583dd46 3118duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 3119 tree new_field_name, new_type, cl;
e04a16fb
AG
3120{
3121 /* This might be modified to work with method decl as well */
3122 tree decl = find_field (TREE_TYPE (ctxp->current_parsed_class),
3123 new_field_name);
3124 if (decl)
3125 {
4a5f66c3
APB
3126 char *t1 = strdup (purify_type_name
3127 ((TREE_CODE (new_type) == POINTER_TYPE
3128 && TREE_TYPE (new_type) == NULL_TREE) ?
3129 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
3130 lang_printable_name (new_type, 1)));
c877974e
APB
3131 /* The type may not have been completed by the time we report
3132 the error */
4a5f66c3
APB
3133 char *t2 = strdup (purify_type_name
3134 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
3135 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
3136 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
3137 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
3138 parse_error_context
3139 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
3140 t1, IDENTIFIER_POINTER (new_field_name),
3141 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
3142 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3143 free (t1);
3144 free (t2);
c583dd46 3145 return 1;
e04a16fb 3146 }
c583dd46 3147 return 0;
e04a16fb
AG
3148}
3149
3150/* Field registration routine. If TYPE doesn't exist, field
3151 declarations are linked to the undefined TYPE dependency list, to
3152 be later resolved in java_complete_class () */
3153
3154static void
3155register_fields (flags, type, variable_list)
3156 int flags;
3157 tree type, variable_list;
3158{
c583dd46 3159 tree current, saved_type;
e04a16fb
AG
3160 tree class_type = TREE_TYPE (ctxp->current_parsed_class);
3161 int saved_lineno = lineno;
3162 int must_chain = 0;
3163 tree wfl = NULL_TREE;
3164
3165 /* If we're adding fields to interfaces, those fields are public,
3166 static, final */
3167 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
3168 {
3169 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
3170 flags, ACC_PUBLIC,
3171 "%s", "interface field(s)");
3172 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
3173 flags, ACC_STATIC,
3174 "%s", "interface field(s)");
3175 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
3176 flags, ACC_FINAL, "%s", "interface field(s)");
3177 check_modifiers ("Illegal interface member modifier `%s'", flags,
3178 INTERFACE_FIELD_MODIFIERS);
3179 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
3180 }
3181
c583dd46
APB
3182 /* Obtain a suitable type for resolution, if necessary */
3183 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
3184
3185 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 3186 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 3187
c583dd46
APB
3188 for (current = variable_list, saved_type = type; current;
3189 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 3190 {
c877974e 3191 tree real_type;
c583dd46 3192 tree field_decl;
e04a16fb
AG
3193 tree cl = TREE_PURPOSE (current);
3194 tree init = TREE_VALUE (current);
3195 tree current_name = EXPR_WFL_NODE (cl);
3196
c583dd46
APB
3197 /* Process NAME, as it may specify extra dimension(s) for it */
3198 type = build_array_from_name (type, wfl, current_name, &current_name);
3199
c583dd46
APB
3200 /* Type adjustment. We may have just readjusted TYPE because
3201 the variable specified more dimensions. Make sure we have
22eed1e6
APB
3202 a reference if we can and don't have one already. Also
3203 change the name if we have an init. */
3204 if (type != saved_type)
3205 {
1886c9d8 3206 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
3207 if (init)
3208 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
3209 }
e04a16fb 3210
c877974e
APB
3211 real_type = GET_REAL_TYPE (type);
3212 /* Check for redeclarations */
3213 if (duplicate_declaration_error_p (current_name, real_type, cl))
3214 continue;
3215
c583dd46 3216 /* Set lineno to the line the field was found and create a
5e942c50 3217 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
3218 if (flag_emit_xref)
3219 lineno = EXPR_WFL_LINECOL (cl);
3220 else
3221 lineno = EXPR_WFL_LINENO (cl);
c877974e 3222 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 3223 CHECK_DEPRECATED (field_decl);
c583dd46
APB
3224
3225 /* Check if we must chain. */
3226 if (must_chain)
3227 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 3228
c583dd46
APB
3229 /* If we have an initialization value tied to the field */
3230 if (init)
3231 {
3232 /* The field is declared static */
e04a16fb 3233 if (flags & ACC_STATIC)
e04a16fb 3234 {
7525cc04
APB
3235 /* We include the field and its initialization part into
3236 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
3237 walked, field initializations will be processed and
3238 fields initialized with known constants will be taken
3239 out of <clinit> and have their DECL_INITIAL set
7525cc04
APB
3240 appropriately. */
3241 TREE_CHAIN (init) = ctxp->static_initialized;
3242 ctxp->static_initialized = init;
7f10c2e2
APB
3243 if (TREE_OPERAND (init, 1)
3244 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 3245 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 3246 }
5e942c50
APB
3247 /* A non-static field declared with an immediate initialization is
3248 to be initialized in <init>, if any. This field is remembered
3249 to be processed at the time of the generation of <init>. */
c583dd46
APB
3250 else
3251 {
3252 TREE_CHAIN (init) = ctxp->non_static_initialized;
3253 ctxp->non_static_initialized = init;
3254 }
5b09b33e 3255 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 3256 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
3257 }
3258 }
3259 lineno = saved_lineno;
3260}
3261
89e09b9a 3262/* Generate the method $finit$ that initializes fields initialized
22eed1e6
APB
3263 upon declaration. */
3264
3265static void
3266maybe_generate_finit ()
3267{
3268 tree mdecl, current;
3269
3270 if (!ctxp->non_static_initialized || java_error_count)
3271 return;
3272
3273 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
89e09b9a 3274 ACC_PRIVATE, void_type_node,
de4c7b02 3275 finit_identifier_node, end_params_node);
22eed1e6
APB
3276 start_artificial_method_body (mdecl);
3277
3278 ctxp->non_static_initialized = nreverse (ctxp->non_static_initialized);
3279 for (current = ctxp->non_static_initialized; current;
3280 current = TREE_CHAIN (current))
3281 java_method_add_stmt (mdecl,
3282 build_debugable_stmt (EXPR_WFL_LINECOL (current),
3283 current));
3284
3285 end_artificial_method_body (mdecl);
3286 CLASS_HAS_FINIT_P (TREE_TYPE (ctxp->current_parsed_class)) = 1;
3287 ctxp->non_static_initialized = NULL_TREE;
3288}
3289
e04a16fb
AG
3290/* Check whether it is necessary to generate a <clinit> for the class
3291 we just parsed. */
3292
3293static void
3294maybe_generate_clinit ()
3295{
22eed1e6 3296 tree mdecl, c;
e04a16fb
AG
3297
3298 if (!ctxp->static_initialized || java_error_count)
3299 return;
3300
22eed1e6
APB
3301 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3302 ACC_STATIC, void_type_node,
de4c7b02 3303 clinit_identifier_node, end_params_node);
22eed1e6 3304 start_artificial_method_body (mdecl);
e04a16fb
AG
3305
3306 /* Keep initialization in order to enforce 8.5 */
3307 ctxp->static_initialized = nreverse (ctxp->static_initialized);
3308
3309 /* We process the list of assignment we produced as the result of
3310 the declaration of initialized static field and add them as
3311 statement to the <clinit> method. */
3312 for (c = ctxp->static_initialized; c; c = TREE_CHAIN (c))
3313 {
3314 /* We build the assignment expression that will initialize the
3315 field to its value. There are strict rules on static
3316 initializers (8.5). FIXME */
22eed1e6
APB
3317 java_method_add_stmt (mdecl,
3318 build_debugable_stmt (EXPR_WFL_LINECOL (c), c));
e04a16fb
AG
3319 }
3320
22eed1e6 3321 end_artificial_method_body (mdecl);
e04a16fb
AG
3322 ctxp->static_initialized = NULL_TREE;
3323}
3324
3325/* Shared accros method_declarator and method_header to remember the
3326 patch stage that was reached during the declaration of the method.
3327 A method DECL is built differently is there is no patch
3328 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
3329 pending on the currently defined method. */
3330
3331static int patch_stage;
3332
3333/* Check the method declaration and add the method to its current
3334 class. If the argument list is known to contain incomplete types,
3335 the method is partially added and the registration will be resume
22eed1e6
APB
3336 once the method arguments resolved. If TYPE is NULL, we're dealing
3337 with a constructor. */
e04a16fb
AG
3338
3339static tree
3340method_header (flags, type, mdecl, throws)
3341 int flags;
3342 tree type, mdecl, throws;
3343{
3344 tree meth = TREE_VALUE (mdecl);
3345 tree id = TREE_PURPOSE (mdecl);
1886c9d8 3346 tree type_wfl = NULL_TREE;
79d13333
APB
3347 tree meth_name = NULL_TREE;
3348 tree current, orig_arg, this_class;
e04a16fb 3349 int saved_lineno;
1886c9d8 3350 int constructor_ok = 0, must_chain;
e04a16fb
AG
3351
3352 check_modifiers_consistency (flags);
79d13333
APB
3353
3354 if (ctxp->current_parsed_class)
3355 this_class = TREE_TYPE (ctxp->current_parsed_class);
3356 else
3357 return NULL_TREE;
e04a16fb
AG
3358
3359 /* There are some forbidden modifiers for an abstract method and its
3360 class must be abstract as well. */
22eed1e6 3361 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
3362 {
3363 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
3364 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
3365 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
3366 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
3367 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
3368 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
3369 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb
AG
3370 parse_error_context
3371 (id, "Class `%s' must be declared abstract to define abstract "
3372 "method `%s'",
3373 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
3374 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3375 }
22eed1e6
APB
3376 /* Things to be checked when declaring a constructor */
3377 if (!type)
3378 {
3379 int ec = java_error_count;
3380 /* 8.6: Constructor declarations: we might be trying to define a
3381 method without specifying a return type. */
5e942c50 3382 if (EXPR_WFL_NODE (id) != ctxp->current_parsed_class_un)
22eed1e6
APB
3383 parse_error_context
3384 (id, "Invalid method declaration, return type required");
3385 /* 8.6.3: Constructor modifiers */
3386 else
3387 {
3388 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
3389 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
3390 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
3391 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
3392 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
3393 }
3394 /* If we found error here, we don't consider it's OK to tread
3395 the method definition as a constructor, for the rest of this
3396 function */
3397 if (ec == java_error_count)
3398 constructor_ok = 1;
3399 }
e04a16fb
AG
3400
3401 /* Method declared within the scope of an interface are implicitly
3402 abstract and public. Conflicts with other erroneously provided
c0d87ff6 3403 modifiers are checked right after. */
e04a16fb
AG
3404
3405 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
3406 {
3407 /* If FLAGS isn't set because of a modifier, turn the
3408 corresponding modifier WFL to NULL so we issue a warning on
3409 the obsolete use of the modifier */
3410 if (!(flags & ACC_PUBLIC))
3411 MODIFIER_WFL (PUBLIC_TK) = NULL;
3412 if (!(flags & ACC_ABSTRACT))
3413 MODIFIER_WFL (ABSTRACT_TK) = NULL;
3414 flags |= ACC_PUBLIC;
3415 flags |= ACC_ABSTRACT;
3416 }
3417
3418 /* Modifiers context reset moved up, so abstract method declaration
3419 modifiers can be later checked. */
3420
22eed1e6
APB
3421 /* Set constructor returned type to void and method name to <init>,
3422 unless we found an error identifier the constructor (in which
3423 case we retain the original name) */
3424 if (!type)
3425 {
3426 type = void_type_node;
3427 if (constructor_ok)
3428 meth_name = init_identifier_node;
3429 }
3430 else
3431 meth_name = EXPR_WFL_NODE (id);
e04a16fb 3432
1886c9d8
APB
3433 /* Do the returned type resolution and registration if necessary */
3434 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3435
4a5f66c3
APB
3436 if (meth_name)
3437 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
3438 EXPR_WFL_NODE (id) = meth_name;
3439 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3440
3441 if (must_chain)
e04a16fb 3442 {
1886c9d8
APB
3443 patch_stage = JDEP_METHOD_RETURN;
3444 register_incomplete_type (patch_stage, type_wfl, id, type);
3445 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
3446 }
3447 else
1886c9d8 3448 TREE_TYPE (meth) = type;
e04a16fb
AG
3449
3450 saved_lineno = lineno;
3451 /* When defining an abstract or interface method, the curly
3452 bracket at level 1 doesn't exist because there is no function
3453 body */
3454 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
3455 EXPR_WFL_LINENO (id));
3456
5e942c50
APB
3457 /* Remember the original argument list */
3458 orig_arg = TYPE_ARG_TYPES (meth);
3459
e04a16fb
AG
3460 if (patch_stage) /* includes ret type and/or all args */
3461 {
3462 jdep *jdep;
3463 meth = add_method_1 (this_class, flags, meth_name, meth);
3464 /* Patch for the return type */
3465 if (patch_stage == JDEP_METHOD_RETURN)
3466 {
3467 jdep = CLASSD_LAST (ctxp->classd_list);
3468 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
3469 }
3470 /* This is the stop JDEP. METH allows the function's signature
3471 to be computed. */
3472 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
3473 }
3474 else
5e942c50
APB
3475 meth = add_method (this_class, flags, meth_name,
3476 build_java_signature (meth));
3477
3478 /* Fix the method argument list so we have the argument name
3479 information */
3480 fix_method_argument_names (orig_arg, meth);
3481
3482 /* Register the parameter number and re-install the current line
3483 number */
e04a16fb
AG
3484 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
3485 lineno = saved_lineno;
b9f7e36c
APB
3486
3487 /* Register exception specified by the `throws' keyword for
3488 resolution and set the method decl appropriate field to the list.
3489 Note: the grammar ensures that what we get here are class
3490 types. */
3491 if (throws)
3492 {
3493 throws = nreverse (throws);
3494 for (current = throws; current; current = TREE_CHAIN (current))
3495 {
3496 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
3497 NULL_TREE, NULL_TREE);
3498 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
3499 &TREE_VALUE (current);
3500 }
3501 DECL_FUNCTION_THROWS (meth) = throws;
3502 }
3503
e04a16fb
AG
3504 /* We set the DECL_NAME to ID so we can track the location where
3505 the function was declared. This allow us to report
3506 redefinition error accurately. When method are verified,
3507 DECL_NAME is reinstalled properly (using the content of the
3508 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
3509 when Object is being defined. Constructor <init> names will be
3510 reinstalled the same way. */
e04a16fb
AG
3511 if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node)
3512 DECL_NAME (meth) = id;
22eed1e6
APB
3513
3514 /* Set the flag if we correctly processed a constructor */
3515 if (constructor_ok)
3516 DECL_CONSTRUCTOR_P (meth) = 1;
3517
5e942c50
APB
3518 /* Eventually set the @deprecated tag flag */
3519 CHECK_DEPRECATED (meth);
3520
7f10c2e2
APB
3521 /* If doing xref, store column and line number information instead
3522 of the line number only. */
3523 if (flag_emit_xref)
3524 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
3525
e04a16fb
AG
3526 return meth;
3527}
3528
5e942c50
APB
3529static void
3530fix_method_argument_names (orig_arg, meth)
3531 tree orig_arg, meth;
3532{
3533 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
3534 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
3535 {
3536 TREE_PURPOSE (arg) = this_identifier_node;
3537 arg = TREE_CHAIN (arg);
3538 }
de4c7b02 3539 while (orig_arg != end_params_node)
5e942c50
APB
3540 {
3541 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
3542 orig_arg = TREE_CHAIN (orig_arg);
3543 arg = TREE_CHAIN (arg);
3544 }
3545}
3546
22eed1e6
APB
3547/* Complete the method declaration with METHOD_BODY. */
3548
3549static void
b635eb2f 3550finish_method_declaration (method_body)
22eed1e6
APB
3551 tree method_body;
3552{
79d13333
APB
3553 int flags;
3554
3555 if (!current_function_decl)
3556 return;
3557
3558 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
3559
3560 /* 8.4.5 Method Body */
3561 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
3562 {
3563 tree wfl = DECL_NAME (current_function_decl);
3564 parse_error_context (wfl,
3565 "%s method `%s' can't have a body defined",
3566 (METHOD_NATIVE (current_function_decl) ?
3567 "Native" : "Abstract"),
3568 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
3569 method_body = NULL_TREE;
3570 }
3571 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
3572 {
3573 tree wfl = DECL_NAME (current_function_decl);
3574 parse_error_context (wfl,
3575 "Non native and non abstract method `%s' must "
3576 "have a body defined",
3577 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
3578 method_body = NULL_TREE;
3579 }
3580
2c56429a
APB
3581 if (flag_emit_class_files && method_body
3582 && TREE_CODE (method_body) == NOP_EXPR
3583 && TREE_TYPE (current_function_decl)
3584 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
3585 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
3586
22eed1e6
APB
3587 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
3588 maybe_absorb_scoping_blocks ();
3589 /* Exit function's body */
3590 exit_block ();
3591 /* Merge last line of the function with first line, directly in the
3592 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
3593 if (!flag_emit_xref)
3594 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
f099f336
APB
3595 /* So we don't have an irrelevant function declaration context for
3596 the next static block we'll see. */
3597 current_function_decl = NULL_TREE;
22eed1e6
APB
3598}
3599
3600/* Build a an error message for constructor circularity errors. */
3601
3602static char *
3603constructor_circularity_msg (from, to)
3604 tree from, to;
3605{
3606 static char string [4096];
3607 char *t = strdup (lang_printable_name (from, 0));
3608 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
3609 free (t);
3610 return string;
3611}
3612
3613/* Verify a circular call to METH. Return 1 if an error is found, 0
3614 otherwise. */
3615
3616static int
3617verify_constructor_circularity (meth, current)
3618 tree meth, current;
3619{
3620 static tree list = NULL_TREE;
3621 tree c;
3622 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3623 {
3624 if (TREE_VALUE (c) == meth)
3625 {
3626 char *t;
3627 if (list)
3628 {
3629 tree liste;
3630 list = nreverse (list);
3631 for (liste = list; liste; liste = TREE_CHAIN (liste))
3632 {
3633 parse_error_context
3634 (TREE_PURPOSE (TREE_PURPOSE (liste)),
3635 constructor_circularity_msg
3636 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
3637 java_error_count--;
3638 }
3639 }
3640 t = strdup (lang_printable_name (meth, 0));
3641 parse_error_context (TREE_PURPOSE (c),
3642 "%s: recursive invocation of constructor `%s'",
3643 constructor_circularity_msg (current, meth), t);
3644 free (t);
3645 list = NULL_TREE;
3646 return 1;
3647 }
3648 }
3649 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3650 {
3651 list = tree_cons (c, current, list);
3652 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
3653 return 1;
3654 list = TREE_CHAIN (list);
3655 }
3656 return 0;
3657}
3658
e04a16fb
AG
3659/* Check modifiers that can be declared but exclusively */
3660
3661static void
3662check_modifiers_consistency (flags)
3663 int flags;
3664{
3665 int acc_count = 0;
3666 tree cl = NULL_TREE;
3667
3668 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, 0, acc_count, cl);
3669 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, 1, acc_count, cl);
3670 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, 2, acc_count, cl);
3671 if (acc_count > 1)
3672 parse_error_context
3673 (cl, "Inconsistent member declaration. At most one of `public', "
3674 "`private', or `protected' may be specified");
3675}
3676
3677/* Check the methode header METH for abstract specifics features */
3678
3679static void
3680check_abstract_method_header (meth)
3681 tree meth;
3682{
3683 int flags = get_access_flags_from_decl (meth);
3684 /* DECL_NAME might still be a WFL node */
c877974e 3685 tree name = GET_METHOD_NAME (meth);
e04a16fb
AG
3686
3687 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (ABSTRACT_TK), flags,
3688 ACC_ABSTRACT, "abstract method `%s'",
3689 IDENTIFIER_POINTER (name));
3690 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), flags,
3691 ACC_PUBLIC, "abstract method `%s'",
3692 IDENTIFIER_POINTER (name));
3693
3694 check_modifiers ("Illegal modifier `%s' for interface method",
3695 flags, INTERFACE_METHOD_MODIFIERS);
3696}
3697
3698/* Create a FUNCTION_TYPE node and start augmenting it with the
3699 declared function arguments. Arguments type that can't be resolved
3700 are left as they are, but the returned node is marked as containing
3701 incomplete types. */
3702
3703static tree
3704method_declarator (id, list)
3705 tree id, list;
3706{
3707 tree arg_types = NULL_TREE, current, node;
3708 tree meth = make_node (FUNCTION_TYPE);
3709 jdep *jdep;
e04a16fb
AG
3710
3711 patch_stage = JDEP_NO_PATCH;
3712
3713 for (current = list; current; current = TREE_CHAIN (current))
3714 {
c583dd46 3715 int must_chain = 0;
e04a16fb
AG
3716 tree wfl_name = TREE_PURPOSE (current);
3717 tree type = TREE_VALUE (current);
3718 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
3719 tree already, arg_node;
3720 tree type_wfl = NULL_TREE;
23a79c61 3721 tree real_type;
c583dd46
APB
3722
3723 /* Obtain a suitable type for resolution, if necessary */
3724 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3725
3726 /* Process NAME, as it may specify extra dimension(s) for it */
3727 type = build_array_from_name (type, type_wfl, name, &name);
3728 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 3729
23a79c61
APB
3730 real_type = GET_REAL_TYPE (type);
3731 if (TREE_CODE (real_type) == RECORD_TYPE)
3732 {
3733 real_type = promote_type (real_type);
3734 if (TREE_CODE (type) == TREE_LIST)
3735 TREE_PURPOSE (type) = real_type;
3736 }
5e942c50 3737
e04a16fb
AG
3738 /* Check redefinition */
3739 for (already = arg_types; already; already = TREE_CHAIN (already))
3740 if (TREE_PURPOSE (already) == name)
3741 {
3742 parse_error_context
3743 (wfl_name, "Variable `%s' is used more than once in the "
3744 "argument list of method `%s'", IDENTIFIER_POINTER (name),
3745 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3746 break;
3747 }
3748
3749 /* If we've an incomplete argument type, we know there is a location
3750 to patch when the type get resolved, later. */
3751 jdep = NULL;
c583dd46 3752 if (must_chain)
e04a16fb 3753 {
c583dd46
APB
3754 patch_stage = JDEP_METHOD;
3755 type = register_incomplete_type (patch_stage,
3756 type_wfl, wfl_name, type);
3757 jdep = CLASSD_LAST (ctxp->classd_list);
3758 JDEP_MISC (jdep) = id;
e04a16fb 3759 }
c583dd46 3760
e04a16fb 3761 /* The argument node: a name and a (possibly) incomplete type */
23a79c61 3762 arg_node = build_tree_list (name, real_type);
e04a16fb
AG
3763 if (jdep)
3764 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
3765 TREE_CHAIN (arg_node) = arg_types;
3766 arg_types = arg_node;
3767 }
de4c7b02 3768 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
3769 node = build_tree_list (id, meth);
3770 return node;
3771}
3772
3773static int
3774unresolved_type_p (wfl, returned)
3775 tree wfl;
3776 tree *returned;
3777
3778{
3779 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
3780 {
3781 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
3782 if (returned)
3783 *returned = (decl ? TREE_TYPE (decl) : NULL_TREE);
3784 return 1;
3785 }
3786 if (returned)
3787 *returned = wfl;
3788 return 0;
3789}
3790
3791/* From NAME, build a qualified identifier node using the
3792 qualification from the current package definition. */
3793
3794static tree
3795parser_qualified_classname (name)
3796 tree name;
3797{
3798 if (ctxp->package)
3799 return merge_qualified_name (ctxp->package, EXPR_WFL_NODE (name));
3800 else
3801 return EXPR_WFL_NODE (name);
3802}
3803
3804/* Called once the type a interface extends is resolved. Returns 0 if
3805 everything is OK. */
3806
3807static int
3808parser_check_super_interface (super_decl, this_decl, this_wfl)
3809 tree super_decl, this_decl, this_wfl;
3810{
3811 tree super_type = TREE_TYPE (super_decl);
3812
3813 /* Has to be an interface */
3814 if (!CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (super_decl))))
3815 {
3816 parse_error_context
3817 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
3818 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
3819 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
3820 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
3821 "interface" : "class"),
3822 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
3823 return 1;
3824 }
3825
3826 /* Check scope: same package OK, other package: OK if public */
3827 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
3828 return 1;
3829
3830 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
3831 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3832 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3833 return 0;
3834}
3835
3836/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
3837 0 if everthing is OK. */
3838
3839static int
3840parser_check_super (super_decl, this_decl, wfl)
3841 tree super_decl, this_decl, wfl;
3842{
e04a16fb
AG
3843 tree super_type = TREE_TYPE (super_decl);
3844
3845 /* SUPER should be a CLASS (neither an array nor an interface) */
3846 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
3847 {
3848 parse_error_context
3849 (wfl, "Class `%s' can't subclass %s `%s'",
3850 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3851 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
3852 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3853 return 1;
3854 }
3855
3856 if (CLASS_FINAL (TYPE_NAME (super_type)))
3857 {
3858 parse_error_context (wfl, "Can't subclass final classes: %s",
3859 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3860 return 1;
3861 }
3862
3863 /* Check scope: same package OK, other package: OK if public */
3864 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
3865 return 1;
3866
3867 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
3868 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3869 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3870 return 0;
3871}
3872
3873/* Create a new dependency list and link it (in a LIFO manner) to the
3874 CTXP list of type dependency list. */
3875
3876static void
3877create_jdep_list (ctxp)
3878 struct parser_ctxt *ctxp;
3879{
23a79c61 3880 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
3881 new->first = new->last = NULL;
3882 new->next = ctxp->classd_list;
3883 ctxp->classd_list = new;
3884}
3885
3886static jdeplist *
3887reverse_jdep_list (ctxp)
3888 struct parser_ctxt *ctxp;
3889{
3890 register jdeplist *prev = NULL, *current, *next;
3891 for (current = ctxp->classd_list; current; current = next)
3892 {
3893 next = current->next;
3894 current->next = prev;
3895 prev = current;
3896 }
3897 return prev;
3898}
3899
23a79c61
APB
3900/* Create a fake pointer based on the ID stored in
3901 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
3902 registered again. */
e04a16fb
AG
3903
3904static tree
23a79c61
APB
3905obtain_incomplete_type (type_name)
3906 tree type_name;
e04a16fb 3907{
23a79c61
APB
3908 tree ptr, name;
3909
3910 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
3911 name = EXPR_WFL_NODE (type_name);
3912 else if (INCOMPLETE_TYPE_P (type_name))
3913 name = TYPE_NAME (type_name);
3914 else
3915 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
3916
3917 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 3918 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
3919 break;
3920
3921 if (!ptr)
3922 {
e04a16fb 3923 push_obstacks (&permanent_obstack, &permanent_obstack);
78d21f92
PB
3924 BUILD_PTR_FROM_NAME (ptr, name);
3925 layout_type (ptr);
e04a16fb
AG
3926 pop_obstacks ();
3927 TREE_CHAIN (ptr) = ctxp->incomplete_class;
3928 ctxp->incomplete_class = ptr;
3929 }
3930
3931 return ptr;
3932}
3933
3934/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
3935 non NULL instead of computing a new fake type based on WFL. The new
3936 dependency is inserted in the current type dependency list, in FIFO
3937 manner. */
3938
3939static tree
3940register_incomplete_type (kind, wfl, decl, ptr)
3941 int kind;
3942 tree wfl, decl, ptr;
3943{
23a79c61 3944 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 3945
e04a16fb
AG
3946 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
3947 ptr = obtain_incomplete_type (wfl);
3948
3949 JDEP_KIND (new) = kind;
3950 JDEP_DECL (new) = decl;
3951 JDEP_SOLV (new) = ptr;
3952 JDEP_WFL (new) = wfl;
3953 JDEP_CHAIN (new) = NULL;
3954 JDEP_MISC (new) = NULL_TREE;
3955 JDEP_GET_PATCH (new) = (tree *)NULL;
3956
3957 JDEP_INSERT (ctxp->classd_list, new);
3958
3959 return ptr;
3960}
3961
3962void
3963java_check_circular_reference ()
3964{
3965 tree current;
3966 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
3967 {
3968 tree type = TREE_TYPE (current);
3969 if (CLASS_INTERFACE (TYPE_NAME (type)))
3970 {
3971 /* Check all interfaces this class extends */
3972 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
3973 int n, i;
3974
3975 if (!basetype_vec)
3976 return;
3977 n = TREE_VEC_LENGTH (basetype_vec);
3978 for (i = 0; i < n; i++)
3979 {
3980 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
3981 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
3982 && interface_of_p (type, BINFO_TYPE (vec_elt)))
3983 parse_error_context (lookup_cl (current),
3984 "Cyclic interface inheritance");
3985 }
3986 }
3987 else
3988 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
3989 parse_error_context (lookup_cl (current),
3990 "Cyclic class inheritance");
3991 }
3992}
3993
23a79c61
APB
3994/* safe_layout_class just makes sure that we can load a class without
3995 disrupting the current_class, input_file, lineno, etc, information
3996 about the class processed currently. */
3997
e04a16fb
AG
3998void
3999safe_layout_class (class)
4000 tree class;
4001{
4002 tree save_current_class = current_class;
4003 char *save_input_filename = input_filename;
4004 int save_lineno = lineno;
5e942c50 4005
e04a16fb 4006 push_obstacks (&permanent_obstack, &permanent_obstack);
5e942c50 4007
e04a16fb
AG
4008 layout_class (class);
4009 pop_obstacks ();
5e942c50 4010
e04a16fb
AG
4011 current_class = save_current_class;
4012 input_filename = save_input_filename;
4013 lineno = save_lineno;
4014 CLASS_LOADED_P (class) = 1;
4015}
4016
4017static tree
4018jdep_resolve_class (dep)
4019 jdep *dep;
4020{
4021 tree decl;
4022
23a79c61
APB
4023 if (JDEP_RESOLVED_P (dep))
4024 decl = JDEP_RESOLVED_DECL (dep);
4025 else
e04a16fb 4026 {
23a79c61
APB
4027 decl = resolve_class (JDEP_TO_RESOLVE (dep),
4028 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
4029 JDEP_RESOLVED (dep, decl);
4030 }
23a79c61 4031
e04a16fb 4032 if (!decl)
23a79c61
APB
4033 complete_class_report_errors (dep);
4034
e04a16fb
AG
4035 return decl;
4036}
4037
4038/* Complete unsatisfied class declaration and their dependencies */
4039
4040void
4041java_complete_class ()
4042{
e04a16fb
AG
4043 tree cclass;
4044 jdeplist *cclassd;
4045 int error_found;
b67d701b 4046 tree type;
e04a16fb
AG
4047
4048 push_obstacks (&permanent_obstack, &permanent_obstack);
4049
4050 /* Process imports and reverse the import on demand list */
4051 process_imports ();
4052 if (ctxp->import_demand_list)
4053 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
4054
4055 /* Rever things so we have the right order */
4056 ctxp->class_list = nreverse (ctxp->class_list);
4057 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 4058
e04a16fb
AG
4059 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
4060 cclass && cclassd;
4061 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
4062 {
4063 jdep *dep;
4064 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
4065 {
4066 tree decl;
e04a16fb
AG
4067 if (!(decl = jdep_resolve_class (dep)))
4068 continue;
4069
4070 /* Now it's time to patch */
4071 switch (JDEP_KIND (dep))
4072 {
4073 case JDEP_SUPER:
4074 /* Simply patch super */
4075 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
4076 continue;
4077 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
4078 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
4079 break;
4080
4081 case JDEP_FIELD:
4082 {
4083 /* We do part of the job done in add_field */
4084 tree field_decl = JDEP_DECL (dep);
4085 tree field_type = TREE_TYPE (decl);
4086 push_obstacks (&permanent_obstack, &permanent_obstack);
e04a16fb 4087 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb
AG
4088 field_type = promote_type (field_type);
4089 pop_obstacks ();
4090 TREE_TYPE (field_decl) = field_type;
5e942c50
APB
4091 DECL_ALIGN (field_decl) = 0;
4092 layout_decl (field_decl, 0);
e04a16fb
AG
4093 SOURCE_FRONTEND_DEBUG
4094 (("Completed field/var decl `%s' with `%s'",
4095 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
4096 IDENTIFIER_POINTER (DECL_NAME (decl))));
4097 break;
4098 }
4099 case JDEP_METHOD: /* We start patching a method */
4100 case JDEP_METHOD_RETURN:
4101 error_found = 0;
4102 while (1)
4103 {
4104 if (decl)
4105 {
b67d701b
PB
4106 type = TREE_TYPE(decl);
4107 if (TREE_CODE (type) == RECORD_TYPE)
4108 type = promote_type (type);
e04a16fb
AG
4109 JDEP_APPLY_PATCH (dep, type);
4110 SOURCE_FRONTEND_DEBUG
4111 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
4112 "Completing fct `%s' with ret type `%s'":
4113 "Completing arg `%s' with type `%s'"),
4114 IDENTIFIER_POINTER (EXPR_WFL_NODE
4115 (JDEP_DECL_WFL (dep))),
4116 IDENTIFIER_POINTER (DECL_NAME (decl))));
4117 }
4118 else
4119 error_found = 1;
4120 dep = JDEP_CHAIN (dep);
4121 if (JDEP_KIND (dep) == JDEP_METHOD_END)
4122 break;
4123 else
4124 decl = jdep_resolve_class (dep);
4125 }
4126 if (!error_found)
4127 {
4128 tree mdecl = JDEP_DECL (dep), signature;
4129 push_obstacks (&permanent_obstack, &permanent_obstack);
4130 /* Recompute and reset the signature */
4131 signature = build_java_signature (TREE_TYPE (mdecl));
4132 set_java_signature (TREE_TYPE (mdecl), signature);
4133 pop_obstacks ();
4134 }
4135 else
4136 continue;
4137 break;
4138
4139 case JDEP_INTERFACE:
4140 if (parser_check_super_interface (decl, JDEP_DECL (dep),
4141 JDEP_WFL (dep)))
4142 continue;
4143 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
4144 break;
4145
b67d701b 4146 case JDEP_PARM:
e04a16fb 4147 case JDEP_VARIABLE:
b67d701b
PB
4148 type = TREE_TYPE(decl);
4149 if (TREE_CODE (type) == RECORD_TYPE)
4150 type = promote_type (type);
4151 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
4152 break;
4153
4154 case JDEP_TYPE:
4155 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4156 SOURCE_FRONTEND_DEBUG
4157 (("Completing a random type dependency on a '%s' node",
4158 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
4159 break;
4160
b9f7e36c 4161 case JDEP_EXCEPTION:
c877974e
APB
4162 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4163 SOURCE_FRONTEND_DEBUG
4164 (("Completing `%s' `throws' argument node",
4165 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
4166 break;
4167
e04a16fb 4168 default:
0a2138e2
APB
4169 fatal ("Can't handle patch code %d - java_complete_class",
4170 JDEP_KIND (dep));
e04a16fb
AG
4171 }
4172 }
4173 }
4174 pop_obstacks ();
4175 return;
4176}
4177
4178/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
4179 array. */
4180
4181static tree
4182resolve_class (class_type, decl, cl)
4183 tree class_type, decl, cl;
4184{
49f48c71
KG
4185 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
4186 const char *base = name;
78d21f92
PB
4187 tree resolved_type = TREE_TYPE (class_type);
4188 tree resolved_type_decl;
e04a16fb 4189
78d21f92
PB
4190 if (resolved_type != NULL_TREE)
4191 {
4192 tree resolved_type_decl = TYPE_NAME (resolved_type);
4193 if (resolved_type_decl == NULL_TREE
4194 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
4195 {
4196 resolved_type_decl = build_decl (TYPE_DECL,
4197 TYPE_NAME (class_type),
4198 resolved_type);
4199 }
4200 return resolved_type_decl;
4201 }
4202
e04a16fb
AG
4203 /* 1- Check to see if we have an array. If true, find what we really
4204 want to resolve */
4205 while (name[0] == '[')
4206 name++;
4207 if (base != name)
4208 TYPE_NAME (class_type) = get_identifier (name);
4209
4210 /* 2- Resolve the bare type */
4211 if (!(resolved_type_decl = do_resolve_class (class_type, decl, cl)))
4212 return NULL_TREE;
4213 resolved_type = TREE_TYPE (resolved_type_decl);
4214
4215 /* 3- If we have and array, reconstruct the array down to its nesting */
4216 if (base != name)
4217 {
4218 while (base != name)
4219 {
4220 if (TREE_CODE (resolved_type) == RECORD_TYPE)
4221 resolved_type = promote_type (resolved_type);
4222 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 4223 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
4224 name--;
4225 }
4226 /* Build a fake decl for this, since this is what is expected to
4227 be returned. */
4228 resolved_type_decl =
4229 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
4230 /* Figure how those two things are important for error report. FIXME */
4231 DECL_SOURCE_LINE (resolved_type_decl) = 0;
4232 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 4233 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 4234 }
78d21f92 4235 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
4236 return resolved_type_decl;
4237}
4238
4239/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
4240 are used to report error messages. */
4241
78d21f92 4242tree
e04a16fb
AG
4243do_resolve_class (class_type, decl, cl)
4244 tree class_type;
4245 tree decl;
4246 tree cl;
4247{
4248 tree new_class_decl;
4249 tree original_name = NULL_TREE;
4250
4251 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
4252 its is changed by find_in_imports{_on_demand} */
4253
4254 /* 1- Check for the type in single imports */
4255 if (find_in_imports (class_type))
4256 return NULL_TREE;
4257
4258 /* 2- And check for the type in the current compilation unit. If it fails,
4259 try with a name qualified with the package name if appropriate. */
e04a16fb
AG
4260 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4261 {
4262 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4263 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4264 load_class (TYPE_NAME (class_type), 0);
4265 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4266 }
4267
4268 original_name = TYPE_NAME (class_type);
4269 if (!QUALIFIED_P (TYPE_NAME (class_type)) && ctxp->package)
4270 TYPE_NAME (class_type) = merge_qualified_name (ctxp->package,
4271 TYPE_NAME (class_type));
bc3ca41b 4272#if 1
5e942c50
APB
4273 if (!(new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4274 load_class (TYPE_NAME (class_type), 0);
e04a16fb
AG
4275 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4276 {
4277 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4278 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4279 load_class (TYPE_NAME (class_type), 0);
4280 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4281 }
bc3ca41b
PB
4282#else
4283 new_name = TYPE_NAME (class_type);
4284 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_name)) != NULL_TREE)
4285 {
4286 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4287 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4288 load_class (new_name, 0);
4289 return IDENTIFIER_CLASS_VALUE (new_name);
4290 }
4291 else
4292 {
4293 tree class = read_class (new_name);
4294 if (class != NULL_TREE)
4295 {
4296 tree decl = IDENTIFIER_CLASS_VALUE (new_name);
4297 if (decl == NULL_TREE)
4298 decl = push_class (class, new_name);
4299 return decl;
4300 }
4301 }
4302#endif
e04a16fb
AG
4303 TYPE_NAME (class_type) = original_name;
4304
4305 /* 3- Check an other compilation unit that bears the name of type */
4306 load_class (TYPE_NAME (class_type), 0);
4307 if (check_pkg_class_access (TYPE_NAME (class_type),
4308 (cl ? cl : lookup_cl (decl))))
4309 return NULL_TREE;
4310
4311 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4312 return new_class_decl;
4313
4314 /* 4- Check the import on demands. Don't allow bar.baz to be
4315 imported from foo.* */
4316 if (!QUALIFIED_P (TYPE_NAME (class_type)))
4317 if (find_in_imports_on_demand (class_type))
4318 return NULL_TREE;
4319
4320 /* 5- Last call for a resolution */
4321 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4322}
4323
4324/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
4325 parsed class). Return a decl node. This function is meant to be
4326 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
4327
4328static tree
c877974e
APB
4329resolve_and_layout (something, cl)
4330 tree something;
e04a16fb
AG
4331 tree cl;
4332{
c877974e
APB
4333 tree decl;
4334
23a79c61
APB
4335 /* Don't do that on the current class */
4336 if (something == current_class)
4337 return TYPE_NAME (current_class);
c877974e 4338
23a79c61 4339 /* Don't do anything for void and other primitive types */
c877974e
APB
4340 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4341 return NULL_TREE;
4342
23a79c61
APB
4343 /* Pointer types can be reall pointer types or fake pointers. When
4344 finding a real pointer, recheck for primitive types */
4345 if (TREE_CODE (something) == POINTER_TYPE)
4346 {
4347 if (TREE_TYPE (something))
4348 {
4349 something = TREE_TYPE (something);
4350 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4351 return NULL_TREE;
4352 }
4353 else
4354 something = TYPE_NAME (something);
4355 }
4356
4357 /* Don't do anything for arrays of primitive types */
4358 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
4359 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
4360 return NULL_TREE;
4361
4362 /* If something is not and IDENTIFIER_NODE, it can be a a TYPE_DECL
4363 or a real TYPE */
c877974e
APB
4364 if (TREE_CODE (something) != IDENTIFIER_NODE)
4365 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
4366 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
4367
23a79c61
APB
4368 if (!(decl = resolve_no_layout (something, cl)))
4369 return NULL_TREE;
4370
4371 /* Resolve and layout if necessary */
4372 layout_class_methods (TREE_TYPE (decl));
7705e9db
APB
4373 /* Check methods, but only once */
4374 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl))
4375 && !CLASS_LOADED_P (TREE_TYPE (decl)))
23a79c61
APB
4376 CHECK_METHODS (decl);
4377 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
e04a16fb 4378 safe_layout_class (TREE_TYPE (decl));
23a79c61 4379
e04a16fb
AG
4380 return decl;
4381}
4382
4383/* Resolve a class, returns its decl but doesn't perform any
4384 layout. The current parsing context is saved and restored */
4385
4386static tree
4387resolve_no_layout (name, cl)
4388 tree name, cl;
4389{
4390 tree ptr, decl;
4391 BUILD_PTR_FROM_NAME (ptr, name);
4392 java_parser_context_save_global ();
4393 decl = resolve_class (ptr, NULL_TREE, cl);
4394 java_parser_context_restore_global ();
4395
4396 return decl;
4397}
4398
23a79c61
APB
4399/* Called when reporting errors. Skip leader '[' in a complex array
4400 type description that failed to be resolved. */
e04a16fb 4401
49f48c71 4402static const char *
e04a16fb 4403purify_type_name (name)
49f48c71 4404 const char *name;
e04a16fb
AG
4405{
4406 while (*name && *name == '[')
4407 name++;
4408 return name;
4409}
4410
4411/* The type CURRENT refers to can't be found. We print error messages. */
4412
4413static void
4414complete_class_report_errors (dep)
4415 jdep *dep;
4416{
49f48c71 4417 const char *name;
23a79c61
APB
4418
4419 if (!JDEP_WFL (dep))
4420 return;
4421
4422 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
4423 switch (JDEP_KIND (dep))
4424 {
4425 case JDEP_SUPER:
4426 parse_error_context
4427 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 4428 purify_type_name (name),
e04a16fb
AG
4429 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4430 break;
4431 case JDEP_FIELD:
4432 parse_error_context
4433 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 4434 purify_type_name (name),
e04a16fb
AG
4435 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4436 break;
4437 case JDEP_METHOD: /* Covers arguments */
4438 parse_error_context
4439 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4440 "argument `%s' of method `%s'",
23a79c61 4441 purify_type_name (name),
e04a16fb
AG
4442 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
4443 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
4444 break;
4445 case JDEP_METHOD_RETURN: /* Covers return type */
4446 parse_error_context
4447 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4448 "return type of method `%s'",
23a79c61 4449 purify_type_name (name),
e04a16fb
AG
4450 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
4451 break;
4452 case JDEP_INTERFACE:
4453 parse_error_context
4454 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
4455 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
4456 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
4457 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4458 break;
4459 case JDEP_VARIABLE:
4460 parse_error_context
4461 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4462 "local variable `%s'",
b67d701b
PB
4463 purify_type_name (IDENTIFIER_POINTER
4464 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
4465 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4466 break;
b9f7e36c
APB
4467 case JDEP_EXCEPTION: /* As specified by `throws' */
4468 parse_error_context
4469 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
4470 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
4471 break;
0a2138e2
APB
4472 default:
4473 /* Fix for -Wall. Just break doing nothing. The error will be
4474 caught later */
4475 break;
e04a16fb
AG
4476 }
4477}
4478
4479/* Check uninitialized final. */
4480
4481void
4482java_check_final ()
4483{
4484}
4485
22eed1e6
APB
4486/* Return a static string containing the DECL prototype string. If
4487 DECL is a constructor, use the class name instead of the form
4488 <init> */
4489
49f48c71 4490static const char *
22eed1e6
APB
4491get_printable_method_name (decl)
4492 tree decl;
4493{
49f48c71 4494 const char *to_return;
9ee9b555 4495 tree name = NULL_TREE;
22eed1e6
APB
4496
4497 if (DECL_CONSTRUCTOR_P (decl))
4498 {
4499 name = DECL_NAME (decl);
5e942c50 4500 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
4501 }
4502
4503 to_return = lang_printable_name (decl, 0);
4504 if (DECL_CONSTRUCTOR_P (decl))
4505 DECL_NAME (decl) = name;
4506
4507 return to_return;
4508}
4509
5e942c50
APB
4510/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
4511 nevertheless needs to be verfied, 1 otherwise. */
4512
4513static int
4514reset_method_name (method)
4515 tree method;
4516{
7f1d4866 4517 if (!IS_CLINIT (method) && DECL_NAME (method) != finit_identifier_node)
5e942c50
APB
4518 {
4519 /* NAME is just the plain name when Object is being defined */
4520 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
4521 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
4522 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
4523 return 0;
4524 }
4525 else
4526 return 1;
4527}
4528
c877974e
APB
4529/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
4530
4531tree
4532java_get_real_method_name (method_decl)
4533 tree method_decl;
4534{
4535 tree method_name = DECL_NAME (method_decl);
4536 if (DECL_CONSTRUCTOR_P (method_decl))
4537 return init_identifier_node;
82371d41
APB
4538
4539 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
4540 and still can be a constructor. FIXME */
4541
23a79c61
APB
4542 /* Don't confuse method only bearing the name of their class as
4543 constructors */
82371d41
APB
4544 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
4545 && ctxp
4546 && ctxp->current_parsed_class_un == EXPR_WFL_NODE (method_name)
23a79c61
APB
4547 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
4548 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
4549 return init_identifier_node;
4550 else
4551 return EXPR_WFL_NODE (method_name);
4552}
4553
22eed1e6
APB
4554/* Track method being redefined inside the same class. As a side
4555 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 4556 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 4557
e04a16fb
AG
4558static int
4559check_method_redefinition (class, method)
4560 tree class, method;
4561{
4562 tree redef, name;
4563 tree cl = DECL_NAME (method);
c3f2a476 4564 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
ba179f9f
APB
4565 /* decl name of artificial <clinit> and $finit$ doesn't need to be
4566 fixed and checked */
5e942c50
APB
4567
4568 /* Reset the method name before running the check. If it returns 1,
4569 the method doesn't need to be verified with respect to method
4570 redeclaration and we return 0 */
4571 if (reset_method_name (method))
e04a16fb 4572 return 0;
5e942c50
APB
4573
4574 name = DECL_NAME (method);
e04a16fb
AG
4575 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
4576 {
c3f2a476 4577 if (redef == method)
e04a16fb 4578 break;
c3f2a476
APB
4579 if (DECL_NAME (redef) == name
4580 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 4581 {
22eed1e6
APB
4582 parse_error_context
4583 (cl, "Duplicate %s declaration `%s'",
4584 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
4585 get_printable_method_name (redef));
e04a16fb
AG
4586 return 1;
4587 }
4588 }
4589 return 0;
4590}
4591
d77613be
APB
4592static void
4593check_abstract_method_definitions (do_interface, class_decl, type)
4594 int do_interface;
4595 tree class_decl, type;
4596{
4597 tree class = TREE_TYPE (class_decl);
4598 tree method, end_type;
4599
4600 end_type = (do_interface ? object_type_node : type);
4601 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
4602 {
4603 tree other_super, other_method, method_sig, method_name;
4604 int found = 0;
4605
4606 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
4607 continue;
4608
4609 /* Now verify that somewhere in between TYPE and CLASS,
4610 abstract method METHOD gets a non abstract definition
4611 that is inherited by CLASS. */
4612
4613 method_sig = build_java_signature (TREE_TYPE (method));
4614 method_name = DECL_NAME (method);
4615 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
4616 method_name = EXPR_WFL_NODE (method_name);
4617
4618 for (other_super = class; other_super != end_type;
4619 other_super = CLASSTYPE_SUPER (other_super))
4620 {
4621 for (other_method = TYPE_METHODS (other_super); other_method;
4622 other_method = TREE_CHAIN (other_method))
4623 {
4624 tree s = build_java_signature (TREE_TYPE (other_method));
4625 tree other_name = DECL_NAME (other_method);
4626
4627 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
4628 other_name = EXPR_WFL_NODE (other_name);
4629 if (!IS_CLINIT (other_method)
4630 && !DECL_CONSTRUCTOR_P (other_method)
4631 && method_name == other_name && method_sig == s)
4632 {
4633 found = 1;
4634 break;
4635 }
4636 }
4637 }
4638
4639 /* Report that abstract METHOD didn't find an implementation
4640 that CLASS can use. */
4641 if (!found)
4642 {
4643 char *t = strdup (lang_printable_name
4644 (TREE_TYPE (TREE_TYPE (method)), 0));
4645 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
4646 tree saved_wfl = NULL_TREE;
4647
4648 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
4649 {
4650 saved_wfl = DECL_NAME (method);
4651 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
4652 }
4653
4654 parse_error_context
4655 (lookup_cl (class_decl),
4656 "Class `%s' doesn't define the abstract method `%s %s' from "
4657 "%s `%s'. This method must be defined or %s `%s' must be "
4658 "declared abstract",
4659 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
4660 t, lang_printable_name (method, 0),
4661 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
4662 "interface" : "class"),
4663 IDENTIFIER_POINTER (ccn),
4664 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
4665 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
4666
4667 free (t);
4668
4669 if (saved_wfl)
4670 DECL_NAME (method) = saved_wfl;
4671 }
4672 }
4673}
4674
4675/* Check that CLASS_DECL somehoow implements all inherited abstract
4676 methods. */
4677
4678static void
4679java_check_abstract_method_definitions (class_decl)
4680 tree class_decl;
4681{
4682 tree class = TREE_TYPE (class_decl);
4683 tree super, vector;
4684 int i;
4685
4686 if (CLASS_ABSTRACT (class_decl))
4687 return;
4688
4689 /* Check for inherited types */
4690 for (super = CLASSTYPE_SUPER (class); super != object_type_node;
4691 super = CLASSTYPE_SUPER (super))
4692 {
4693 if (!CLASS_ABSTRACT (TYPE_NAME (super)))
4694 continue;
4695
4696 check_abstract_method_definitions (0, class_decl, super);
4697 }
4698
4699 /* Check for implemented interfaces. */
4700 vector = TYPE_BINFO_BASETYPES (class);
4701 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
4702 {
4703 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
4704 check_abstract_method_definitions (1, class_decl, super);
4705 }
4706}
4707
4708/* Check all the methods of CLASS_DECL. Methods are first completed
4709 then checked according to regular method existance rules. If no
4710 constructor for CLASS_DECL were encountered, then build its
4711 declaration. */
e04a16fb
AG
4712
4713static void
4714java_check_regular_methods (class_decl)
4715 tree class_decl;
4716{
5e942c50 4717 int saw_constructor = 0;
e04a16fb
AG
4718 tree method;
4719 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
4720 tree super_class = CLASSTYPE_SUPER (class);
5e942c50 4721 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
4722 tree mthrows;
4723
4724 /* It is not necessary to check methods defined in java.lang.Object */
4725 if (class == object_type_node)
4726 return;
e04a16fb 4727
23a79c61
APB
4728 if (!TYPE_NVIRTUALS (class))
4729 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
4730
4731 /* Should take interfaces into account. FIXME */
4732 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
4733 {
5e942c50 4734 tree sig;
e04a16fb
AG
4735 tree method_wfl = DECL_NAME (method);
4736 int aflags;
4737
5e942c50
APB
4738 /* If we previously found something and its name was saved,
4739 reinstall it now */
4740 if (found && saved_found_wfl)
ba179f9f
APB
4741 {
4742 DECL_NAME (found) = saved_found_wfl;
4743 saved_found_wfl = NULL_TREE;
4744 }
5e942c50 4745
e04a16fb
AG
4746 /* Check for redefinitions */
4747 if (check_method_redefinition (class, method))
4748 continue;
4749
22eed1e6
APB
4750 /* If we see one constructor a mark so we don't generate the
4751 default one. Also skip other verifications: constructors
4752 can't be inherited hence hiden or overriden */
4753 if (DECL_CONSTRUCTOR_P (method))
4754 {
4755 saw_constructor = 1;
4756 continue;
4757 }
4758
c877974e
APB
4759 /* We verify things thrown by the method. They must inherits from
4760 java.lang.Throwable */
4761 for (mthrows = DECL_FUNCTION_THROWS (method);
4762 mthrows; mthrows = TREE_CHAIN (mthrows))
4763 {
4764 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
4765 parse_error_context
4766 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be "
4767 "a subclass of class `java.lang.Throwable'",
4768 IDENTIFIER_POINTER
4769 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
4770 }
4771
e04a16fb 4772 sig = build_java_argument_signature (TREE_TYPE (method));
e04a16fb 4773 found = lookup_argument_method (super_class, DECL_NAME (method), sig);
b9f7e36c 4774
5e942c50 4775 /* Nothing overrides or it's a private method. */
aabd7048 4776 if (!found)
5e942c50 4777 continue;
aabd7048
PB
4778 if (METHOD_PRIVATE (found))
4779 {
4780 found = NULL_TREE;
4781 continue;
4782 }
5e942c50
APB
4783
4784 /* If found wasn't verified, it's DECL_NAME won't be set properly.
4785 We set it temporarily for the sake of the error report. */
4786 saved_found_wfl = DECL_NAME (found);
4787 reset_method_name (found);
4788
e04a16fb
AG
4789 /* Can't override a method with the same name and different return
4790 types. */
4791 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 4792 {
0a2138e2
APB
4793 char *t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)),
4794 0));
b9f7e36c 4795 parse_error_context
7f10c2e2 4796 (method_wfl,
b9f7e36c 4797 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 4798 lang_printable_name (found, 0), t,
b9f7e36c
APB
4799 IDENTIFIER_POINTER
4800 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4801 free (t);
4802 }
e04a16fb 4803
7f10c2e2
APB
4804 aflags = get_access_flags_from_decl (found);
4805 /* If the method has default, access in an other package, then
4806 issue a warning that the current method doesn't override the
4807 one that was found elsewhere. Do not issue this warning when
4808 the match was found in java.lang.Object. */
4809 if (DECL_CONTEXT (found) != object_type_node
4810 && ((aflags & 0x7) == 0)
4811 && !class_in_current_package (DECL_CONTEXT (found))
1fb89a4d 4812 && DECL_NAME (found) != clinit_identifier_node
7f10c2e2
APB
4813 && flag_not_overriding)
4814 {
4815 parse_warning_context
4816 (method_wfl, "Method `%s' in class `%s' does not "
4817 "override the corresponding method in class `%s', which is "
4818 "private to a different package",
4819 lang_printable_name (found, 0),
4820 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
4821 IDENTIFIER_POINTER (DECL_NAME
4822 (TYPE_NAME (DECL_CONTEXT (found)))));
4823 continue;
4824 }
4825
e04a16fb
AG
4826 /* Can't override final. Can't override static. */
4827 if (METHOD_FINAL (found) || METHOD_STATIC (found))
4828 {
4829 /* Static *can* override static */
4830 if (METHOD_STATIC (found) && METHOD_STATIC (method))
4831 continue;
4832 parse_error_context
4833 (method_wfl,
4834 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
4835 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 4836 lang_printable_name (found, 0),
e04a16fb
AG
4837 (METHOD_FINAL (found) ? "final" : "static"),
4838 IDENTIFIER_POINTER
4839 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4840 continue;
4841 }
7f10c2e2 4842
e04a16fb
AG
4843 /* Static method can't override instance method. */
4844 if (METHOD_STATIC (method))
4845 {
4846 parse_error_context
4847 (method_wfl,
4848 "Instance methods can't be overriden by a static method. Method "
4849 "`%s' is an instance method in class `%s'",
0a2138e2 4850 lang_printable_name (found, 0),
e04a16fb
AG
4851 IDENTIFIER_POINTER
4852 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4853 continue;
4854 }
5e942c50 4855
5e942c50
APB
4856 /* - Overriding/hiding public must be public
4857 - Overriding/hiding protected must be protected or public
4858 - If the overriden or hidden method has default (package)
4859 access, then the overriding or hiding method must not be
4860 private; otherwise, a compile-time error occurs */
4861 if ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
4862 || (METHOD_PROTECTED (found)
4863 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
4864 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
4865 && METHOD_PRIVATE (method)))
e04a16fb
AG
4866 {
4867 parse_error_context
4868 (method_wfl,
4869 "Methods can't be overridden to be more private. Method `%s' is "
5e942c50
APB
4870 "not %s in class `%s'", lang_printable_name (method, 0),
4871 (METHOD_PUBLIC (method) ? "public" :
4872 (METHOD_PRIVATE (method) ? "private" : "protected")),
4873 IDENTIFIER_POINTER (DECL_NAME
4874 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
4875 continue;
4876 }
4877
b9f7e36c
APB
4878 /* Overriding methods must have compatible `throws' clauses on checked
4879 exceptions, if any */
4880 check_throws_clauses (method, method_wfl, found);
4881
e04a16fb
AG
4882 /* Inheriting multiple methods with the same signature. FIXME */
4883 }
4884
5e942c50
APB
4885 /* Don't forget eventual pending found and saved_found_wfl. Take
4886 into account that we might have exited because we saw an
d77613be 4887 artificial method as the last entry. */
5e942c50
APB
4888
4889 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
4890 DECL_NAME (found) = saved_found_wfl;
4891
23a79c61
APB
4892 if (!TYPE_NVIRTUALS (class))
4893 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 4894
d77613be
APB
4895 /* Search for inherited abstract method not yet implemented in this
4896 class. */
4897 java_check_abstract_method_definitions (class_decl);
4898
22eed1e6 4899 if (!saw_constructor)
e04a16fb 4900 {
23a79c61
APB
4901 /* No constructor seen, we craft one, at line 0. Since this
4902 operation takes place after we laid methods out
4903 (layout_class_methods), we prepare the its DECL
4904 appropriately. */
22eed1e6
APB
4905 int flags;
4906 tree decl;
4907
4908 /* If the class is declared PUBLIC, the default constructor is
4909 PUBLIC otherwise it has default access implied by no access
4910 modifiers. */
4911 flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
4912 ACC_PUBLIC : 0);
4913 decl = create_artificial_method (class, flags, void_type_node,
de4c7b02 4914 init_identifier_node, end_params_node);
e04a16fb 4915 DECL_CONSTRUCTOR_P (decl) = 1;
23a79c61 4916 layout_class_method (TREE_TYPE (class_decl), NULL_TREE, decl, NULL_TREE);
e04a16fb
AG
4917 }
4918}
4919
b9f7e36c
APB
4920/* Return a non zero value if the `throws' clause of METHOD (if any)
4921 is incompatible with the `throws' clause of FOUND (if any). */
4922
4923static void
4924check_throws_clauses (method, method_wfl, found)
4925 tree method, method_wfl, found;
4926{
4927 tree mthrows, fthrows;
4928
c877974e
APB
4929 /* Can't check these things with class loaded from bytecode. FIXME */
4930 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
4931 return;
4932
b9f7e36c
APB
4933 for (mthrows = DECL_FUNCTION_THROWS (method);
4934 mthrows; mthrows = TREE_CHAIN (mthrows))
4935 {
4936 /* We don't verify unchecked expressions */
c877974e 4937 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
4938 continue;
4939 /* Checked expression must be compatible */
4940 for (fthrows = DECL_FUNCTION_THROWS (found);
4941 fthrows; fthrows = TREE_CHAIN (fthrows))
4942 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
4943 break;
4944 if (!fthrows)
4945 {
4946 parse_error_context
4947 (method_wfl, "Invalid checked exception class `%s' in "
4948 "`throws' clause. The exception must be a subclass of an "
4949 "exception thrown by `%s' from class `%s'",
4950 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 4951 lang_printable_name (found, 0),
b9f7e36c
APB
4952 IDENTIFIER_POINTER
4953 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4954 }
4955 }
4956}
4957
e04a16fb
AG
4958/* Check abstract method of interface INTERFACE */
4959
4960static void
5e942c50
APB
4961java_check_abstract_methods (interface_decl)
4962 tree interface_decl;
e04a16fb
AG
4963{
4964 int i, n;
4965 tree method, basetype_vec, found;
5e942c50 4966 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
4967
4968 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
4969 {
b9f7e36c 4970 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
4971
4972 /* 2- Check for double definition inside the defining interface */
4973 if (check_method_redefinition (interface, method))
4974 continue;
4975
4976 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 4977 the thrown exceptions (FIXME) */
e04a16fb
AG
4978 found = lookup_java_interface_method2 (interface, method);
4979 if (found)
4980 {
5e942c50
APB
4981 char *t;
4982 tree saved_found_wfl = DECL_NAME (found);
4983 reset_method_name (found);
4984 t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 4985 parse_error_context
b9f7e36c 4986 (method_wfl,
5e942c50 4987 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 4988 lang_printable_name (found, 0), t,
b9f7e36c
APB
4989 IDENTIFIER_POINTER
4990 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4991 free (t);
e04a16fb 4992 continue;
5e942c50
APB
4993
4994 DECL_NAME (found) = saved_found_wfl;
e04a16fb
AG
4995 }
4996 }
4997
4998 /* 4- Inherited methods can't differ by their returned types */
4999 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
5000 return;
5001 n = TREE_VEC_LENGTH (basetype_vec);
5002 for (i = 0; i < n; i++)
5003 {
5004 tree sub_interface_method, sub_interface;
5005 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5006 if (!vec_elt)
5007 continue;
5008 sub_interface = BINFO_TYPE (vec_elt);
5009 for (sub_interface_method = TYPE_METHODS (sub_interface);
5010 sub_interface_method;
5011 sub_interface_method = TREE_CHAIN (sub_interface_method))
5012 {
5013 found = lookup_java_interface_method2 (interface,
5014 sub_interface_method);
5015 if (found && (found != sub_interface_method))
5e942c50
APB
5016 {
5017 tree saved_found_wfl = DECL_NAME (found);
5018 reset_method_name (found);
5019 parse_error_context
5020 (lookup_cl (sub_interface_method),
5021 "Interface `%s' inherits method `%s' from interface `%s'. "
5022 "This method is redefined with a different return type in "
5023 "interface `%s'",
5024 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
5025 lang_printable_name (found, 0),
5026 IDENTIFIER_POINTER
5027 (DECL_NAME (TYPE_NAME
5028 (DECL_CONTEXT (sub_interface_method)))),
5029 IDENTIFIER_POINTER
5030 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
5031 DECL_NAME (found) = saved_found_wfl;
5032 }
e04a16fb
AG
5033 }
5034 }
5035}
5036
e04a16fb
AG
5037/* Lookup methods in interfaces using their name and partial
5038 signature. Return a matching method only if their types differ. */
5039
5040static tree
5041lookup_java_interface_method2 (class, method_decl)
5042 tree class, method_decl;
5043{
5044 int i, n;
5045 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
5046
5047 if (!basetype_vec)
5048 return NULL_TREE;
5049
5050 n = TREE_VEC_LENGTH (basetype_vec);
5051 for (i = 0; i < n; i++)
5052 {
5053 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
5054 if ((BINFO_TYPE (vec_elt) != object_type_node)
5055 && (to_return =
5056 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
5057 return to_return;
5058 }
5059 for (i = 0; i < n; i++)
5060 {
5061 to_return = lookup_java_interface_method2
5062 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
5063 if (to_return)
5064 return to_return;
5065 }
5066
5067 return NULL_TREE;
5068}
5069
5070/* Lookup method using their name and partial signature. Return a
5071 matching method only if their types differ. */
5072
5073static tree
5074lookup_java_method2 (clas, method_decl, do_interface)
5075 tree clas, method_decl;
5076 int do_interface;
5077{
5e942c50
APB
5078 tree method, method_signature, method_name, method_type, name;
5079
e04a16fb 5080 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
5081 name = DECL_NAME (method_decl);
5082 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
5083 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
5084 method_type = TREE_TYPE (TREE_TYPE (method_decl));
5085
5086 while (clas != NULL_TREE)
5087 {
5088 for (method = TYPE_METHODS (clas);
5089 method != NULL_TREE; method = TREE_CHAIN (method))
5090 {
5091 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
5092 tree name = DECL_NAME (method);
5093 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
5094 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
5095 && method_sig == method_signature
5096 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 5097 return method;
e04a16fb
AG
5098 }
5099 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
5100 }
5101 return NULL_TREE;
5102}
5103
f441f671
APB
5104/* Return the line that matches DECL line number, and try its best to
5105 position the column number. Used during error reports. */
e04a16fb
AG
5106
5107static tree
5108lookup_cl (decl)
5109 tree decl;
5110{
5111 static tree cl = NULL_TREE;
f441f671 5112 char *line, *found;
e04a16fb
AG
5113
5114 if (!decl)
5115 return NULL_TREE;
5116
5117 if (cl == NULL_TREE)
5118 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
5119
5120 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
5121 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
5122
f441f671
APB
5123 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
5124 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
5125
5126 found = strstr ((const char *)line,
5127 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
5128 if (found)
5129 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
5130
e04a16fb
AG
5131 return cl;
5132}
5133
5134/* Look for a simple name in the single-type import list */
5135
5136static tree
5137find_name_in_single_imports (name)
5138 tree name;
5139{
5140 tree node;
5141
5142 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
5143 if (TREE_VALUE (node) == name)
5144 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
5145
5146 return NULL_TREE;
5147}
5148
5149/* Process all single-type import. */
5150
5151static int
5152process_imports ()
5153{
5154 tree import;
5155 int error_found;
5156
5157 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
5158 {
5159 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
5160
5161 /* Don't load twice something already defined. */
5162 if (IDENTIFIER_CLASS_VALUE (to_be_found))
5163 continue;
5164 QUALIFIED_P (to_be_found) = 1;
5165 load_class (to_be_found, 0);
5166 error_found =
5167 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
5168 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
5169 {
5170 parse_error_context (TREE_PURPOSE (import),
5171 "Class or interface `%s' not found in import",
5172 IDENTIFIER_POINTER (to_be_found));
5173 return 1;
5174 }
5175 if (error_found)
5176 return 1;
5177 }
5178 return 0;
5179}
5180
5181/* Possibly find a class imported by a single-type import statement. Return
5182 1 if an error occured, 0 otherwise. */
5183
5184static int
5185find_in_imports (class_type)
5186 tree class_type;
5187{
5188 tree import;
5189
5190 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
5191 if (TREE_VALUE (import) == TYPE_NAME (class_type))
5192 {
5193 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
5194 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb
AG
5195 }
5196 return 0;
5197}
5198
e04a16fb 5199static int
63a212ed 5200note_possible_classname (name, len)
49f48c71 5201 const char *name;
63a212ed 5202 int len;
e04a16fb 5203{
63a212ed
PB
5204 tree node;
5205 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
5206 len = len - 5;
5207 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
5208 len = len - 6;
e04a16fb 5209 else
63a212ed
PB
5210 return 0;
5211 node = ident_subst (name, len, "", '/', '.', "");
5212 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 5213 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 5214 return 1;
e04a16fb
AG
5215}
5216
5217/* Read a import directory, gathering potential match for further type
5218 references. Indifferently reads a filesystem or a ZIP archive
5219 directory. */
5220
5221static void
5222read_import_dir (wfl)
5223 tree wfl;
5224{
63a212ed 5225 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 5226 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 5227 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 5228 DIR *dirp = NULL;
d8fccff5 5229 JCF *saved_jcf = current_jcf;
e04a16fb 5230
63a212ed
PB
5231 int found = 0;
5232 int k;
5233 void *entry;
5234 struct buffer filename[1];
5235
5236
5237 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
5238 return;
5239 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
5240
5241 BUFFER_INIT (filename);
5242 buffer_grow (filename, package_length + 100);
5243
5244 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
5245 {
49f48c71 5246 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
5247 int entry_length = strlen (entry_name);
5248 if (jcf_path_is_zipfile (entry))
5249 {
5250 ZipFile *zipf;
5251 buffer_grow (filename, entry_length);
5252 memcpy (filename->data, entry_name, entry_length - 1);
5253 filename->data[entry_length-1] = '\0';
5254 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
5255 if (zipf == NULL)
5256 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
5257 else
5258 {
5259 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
5260 BUFFER_RESET (filename);
5261 for (k = 0; k < package_length; k++)
5262 {
5263 char ch = package_name[k];
5264 *filename->ptr++ = ch == '.' ? '/' : ch;
5265 }
5266 *filename->ptr++ = '/';
5267
345137c7 5268 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 5269 {
49f48c71 5270 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
5271 int current_entry_len = zipd->filename_length;
5272
345137c7
TT
5273 if (current_entry_len >= BUFFER_LENGTH (filename)
5274 && strncmp (filename->data, current_entry,
5275 BUFFER_LENGTH (filename)) != 0)
63a212ed 5276 continue;
345137c7 5277 found |= note_possible_classname (current_entry,
63a212ed
PB
5278 current_entry_len);
5279 }
5280 }
5281 }
5282 else
5283 {
5284 BUFFER_RESET (filename);
5285 buffer_grow (filename, entry_length + package_length + 4);
5286 strcpy (filename->data, entry_name);
5287 filename->ptr = filename->data + entry_length;
5288 for (k = 0; k < package_length; k++)
5289 {
5290 char ch = package_name[k];
5291 *filename->ptr++ = ch == '.' ? '/' : ch;
5292 }
5293 *filename->ptr = '\0';
5294
5295 dirp = opendir (filename->data);
5296 if (dirp == NULL)
5297 continue;
5298 *filename->ptr++ = '/';
5299 for (;;)
5300 {
63a212ed 5301 int len;
49f48c71 5302 const char *d_name;
63a212ed
PB
5303 struct dirent *direntp = readdir (dirp);
5304 if (!direntp)
5305 break;
5306 d_name = direntp->d_name;
5307 len = strlen (direntp->d_name);
5308 buffer_grow (filename, len+1);
5309 strcpy (filename->ptr, d_name);
345137c7 5310 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
5311 package_length+len+1);
5312 }
5313 if (dirp)
5314 closedir (dirp);
5315 }
5316 }
e04a16fb 5317
63a212ed 5318 free (filename->data);
e04a16fb 5319
63a212ed
PB
5320 /* Here we should have a unified way of retrieving an entry, to be
5321 indexed. */
5322 if (!found)
e04a16fb
AG
5323 {
5324 static int first = 1;
5325 if (first)
5326 {
5327 char buffer [256];
5328 sprintf (buffer, "Can't find default package `%s'. Check "
5329 "the CLASSPATH environment variable and the access to the "
63a212ed 5330 "archives.", package_name);
e04a16fb
AG
5331 error (buffer);
5332 java_error_count++;
5333 first = 0;
5334 }
5335 else
63a212ed
PB
5336 parse_error_context (wfl, "Package `%s' not found in import",
5337 package_name);
e04a16fb
AG
5338 current_jcf = saved_jcf;
5339 return;
5340 }
e04a16fb
AG
5341 current_jcf = saved_jcf;
5342}
5343
5344/* Possibly find a type in the import on demands specified
5345 types. Returns 1 if an error occured, 0 otherwise. Run throught the
5346 entire list, to detected potential double definitions. */
5347
5348static int
5349find_in_imports_on_demand (class_type)
5350 tree class_type;
5351{
ab3a6dd6 5352 tree node, import, node_to_use = NULL_TREE;
e04a16fb 5353 int seen_once = -1;
ab3a6dd6 5354 tree cl = NULL_TREE;
e04a16fb
AG
5355
5356 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
5357 {
49f48c71 5358 const char *id_name;
e04a16fb
AG
5359 obstack_grow (&temporary_obstack,
5360 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
5361 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 5362 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
5363 obstack_grow0 (&temporary_obstack,
5364 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5365 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
5366 id_name = obstack_finish (&temporary_obstack);
5367
5368 node = maybe_get_identifier (id_name);
5369 if (node && IS_A_CLASSFILE_NAME (node))
5370 {
5371 if (seen_once < 0)
5372 {
5373 cl = TREE_PURPOSE (import);
5374 seen_once = 1;
5375 node_to_use = node;
5376 }
5377 else
5378 {
5379 seen_once++;
5380 parse_error_context
5381 (import, "Type `%s' also potentially defined in package `%s'",
5382 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5383 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5384 }
5385 }
5386 }
5387
5388 if (seen_once == 1)
5389 {
5390 /* Setup lineno so that it refers to the line of the import (in
5391 case we parse a class file and encounter errors */
5392 tree decl;
5393 int saved_lineno = lineno;
5394 lineno = EXPR_WFL_LINENO (cl);
63a212ed 5395 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
5396 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
5397 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5398 /* If there is no DECL set for the class or if the class isn't
5399 loaded and not seen in source yet, the load */
5400 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
5401 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
5402 load_class (node_to_use, 0);
5403 lineno = saved_lineno;
5404 return check_pkg_class_access (TYPE_NAME (class_type), cl);
5405 }
5406 else
5407 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
5408}
5409
5e942c50
APB
5410static tree
5411resolve_package (pkg, next)
5412 tree pkg, *next;
5413{
2c56429a 5414 tree current;
5e942c50 5415 tree type_name = NULL_TREE;
49f48c71 5416 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
5417
5418 /* The trick is to determine when the package name stops and were
5419 the name of something contained in the package starts. Then we
5420 return a fully qualified name of what we want to get. */
5421
5422 /* Do a quick search on well known package names */
5423 if (!strncmp (name, "java.lang.reflect", 17))
5424 {
5425 *next =
5426 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
5427 type_name = lookup_package_type (name, 17);
5428 }
5429 else if (!strncmp (name, "java.lang", 9))
5430 {
5431 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
5432 type_name = lookup_package_type (name, 9);
5433 }
5e942c50 5434
2c56429a
APB
5435 /* If we found something here, return */
5436 if (type_name)
5437 return type_name;
5438
5439 *next = EXPR_WFL_QUALIFICATION (pkg);
5440
5441 /* Try the current package. */
5442 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
5443 IDENTIFIER_LENGTH (ctxp->package)))
5444 {
5445 type_name =
5446 lookup_package_type_and_set_next (name,
5447 IDENTIFIER_LENGTH (ctxp->package),
5448 next );
5449 if (type_name)
5450 return type_name;
5451 }
5452
5453 /* Search in imported package */
5454 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
5455 {
5456 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
5457 int len = IDENTIFIER_LENGTH (current_pkg_name);
5458 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
5459 {
5460 tree left, dummy;
5461
5462 breakdown_qualified (&left, &dummy, current_pkg_name);
5463 len = IDENTIFIER_LENGTH (left);
5464 type_name = lookup_package_type_and_set_next (name, len, next);
5465 if (type_name)
5466 break;
5467 }
5468 }
5469
5470 return type_name;
5471}
5472
5473static tree
5474lookup_package_type_and_set_next (name, len, next)
49f48c71 5475 const char *name;
2c56429a
APB
5476 int len;
5477 tree *next;
5478{
49f48c71 5479 const char *ptr;
2c56429a
APB
5480 tree type_name = lookup_package_type (name, len);
5481
5482 if (!type_name)
5483 return NULL;
5484
5485 ptr = IDENTIFIER_POINTER (type_name);
5486 while (ptr && (ptr = strchr (ptr, '.')))
5487 {
5488 *next = TREE_CHAIN (*next);
5489 ptr++;
5490 }
5e942c50
APB
5491 return type_name;
5492}
5493
5494static tree
5495lookup_package_type (name, from)
49f48c71 5496 const char *name;
5e942c50
APB
5497 int from;
5498{
5499 char subname [128];
49f48c71 5500 const char *sub = &name[from+1];
5e942c50
APB
5501 while (*sub != '.' && *sub)
5502 sub++;
5503 strncpy (subname, name, sub-name);
5504 subname [sub-name] = '\0';
5505 return get_identifier (subname);
5506}
5507
e04a16fb
AG
5508/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
5509 access violations were found, 1 otherwise. */
5510
5511static int
5512check_pkg_class_access (class_name, cl)
5513 tree class_name;
5514 tree cl;
5515{
5516 tree type;
e04a16fb
AG
5517
5518 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
5519 return 0;
5520
5521 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
5522 return 0;
5523
5524 if (!CLASS_PUBLIC (TYPE_NAME (type)))
5525 {
e28cd97b
APB
5526 /* Access to a private class within the same package is
5527 allowed. */
5528 tree l, r;
5529 breakdown_qualified (&l, &r, class_name);
5530 if (l == ctxp->package)
5531 return 0;
5532
e04a16fb
AG
5533 parse_error_context
5534 (cl, "Can't access %s `%s'. Only public classes and interfaces in "
5535 "other packages can be accessed",
5536 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
5537 IDENTIFIER_POINTER (class_name));
5538 return 1;
5539 }
5540 return 0;
5541}
5542
5543/* Local variable declaration. */
5544
5545static void
5546declare_local_variables (modifier, type, vlist)
5547 int modifier;
5548 tree type;
5549 tree vlist;
5550{
c583dd46
APB
5551 tree decl, current, saved_type;
5552 tree type_wfl = NULL_TREE;
e04a16fb
AG
5553 int must_chain = 0;
5554
2aa11e97 5555 /* Push a new block if statements were seen between the last time we
e04a16fb 5556 pushed a block and now. Keep a cound of block to close */
f099f336 5557 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 5558 {
f099f336 5559 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 5560 tree b = enter_block ();
f099f336 5561 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
5562 }
5563
5564 if (modifier)
5565 {
5566 int i;
5567 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e
APB
5568 if (modifier == ACC_FINAL)
5569 {
5570 if (flag_static_local_jdk1_1)
5571 parse_warning_context (ctxp->modifier_ctx [i],
5572 "Unsupported JDK1.1 `final' local variable "
5573 "(treated as non final)");
5574 }
5575 else
5576 {
5577 parse_error_context
5578 (ctxp->modifier_ctx [i],
5579 "Only `final' is allowed as a local variables modifier");
5580 return;
5581 }
e04a16fb
AG
5582 }
5583
c583dd46
APB
5584 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
5585 hold the TYPE value if a new incomplete has to be created (as
5586 opposed to being found already existing and reused). */
5587 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5588
5589 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 5590 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
5591
5592 /* Go through all the declared variables */
5593 for (current = vlist, saved_type = type; current;
5594 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 5595 {
c877974e 5596 tree other, real_type;
e04a16fb
AG
5597 tree wfl = TREE_PURPOSE (current);
5598 tree name = EXPR_WFL_NODE (wfl);
5599 tree init = TREE_VALUE (current);
e04a16fb 5600
c583dd46
APB
5601 /* Process NAME, as it may specify extra dimension(s) for it */
5602 type = build_array_from_name (type, type_wfl, name, &name);
5603
5604 /* Variable redefinition check */
5605 if ((other = lookup_name_in_blocks (name)))
5606 {
5607 variable_redefinition_error (wfl, name, TREE_TYPE (other),
5608 DECL_SOURCE_LINE (other));
5609 continue;
5610 }
5611
5612 /* Type adjustment. We may have just readjusted TYPE because
5613 the variable specified more dimensions. Make sure we have
5614 a reference if we can and don't have one already. */
1886c9d8 5615 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
5616
5617 real_type = GET_REAL_TYPE (type);
c583dd46
APB
5618 /* Never layout this decl. This will be done when its scope
5619 will be entered */
c877974e 5620 decl = build_decl (VAR_DECL, name, real_type);
c583dd46
APB
5621 BLOCK_CHAIN_DECL (decl);
5622
d4370213
APB
5623 /* If doing xreferencing, replace the line number with the WFL
5624 compound value */
5625 if (flag_emit_xref)
5626 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
5627
e04a16fb
AG
5628 /* Don't try to use an INIT statement when an error was found */
5629 if (init && java_error_count)
5630 init = NULL_TREE;
c583dd46
APB
5631
5632 /* Add the initialization function to the current function's code */
5633 if (init)
e04a16fb 5634 {
c583dd46
APB
5635 /* Name might have been readjusted */
5636 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
5637 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
5638 java_method_add_stmt (current_function_decl,
5639 build_debugable_stmt (EXPR_WFL_LINECOL (init),
5640 init));
5641 }
5642
5643 /* Setup dependency the type of the decl */
5644 if (must_chain)
5645 {
5646 jdep *dep;
5647 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
5648 dep = CLASSD_LAST (ctxp->classd_list);
5649 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
5650 }
5651 }
5652 SOURCE_FRONTEND_DEBUG (("Defined locals"));
5653}
5654
5655/* Called during parsing. Build decls from argument list. */
5656
5657static void
5658source_start_java_method (fndecl)
5659 tree fndecl;
5660{
5661 tree tem;
5662 tree parm_decl;
5663 int i;
5664
79d13333
APB
5665 if (!fndecl)
5666 return;
5667
e04a16fb
AG
5668 current_function_decl = fndecl;
5669
5670 /* New scope for the function */
5671 enter_block ();
5672 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 5673 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
5674 {
5675 tree type = TREE_VALUE (tem);
5676 tree name = TREE_PURPOSE (tem);
5677
23a79c61
APB
5678 /* If type is incomplete. Create an incomplete decl and ask for
5679 the decl to be patched later */
e04a16fb
AG
5680 if (INCOMPLETE_TYPE_P (type))
5681 {
5682 jdep *jdep;
c877974e
APB
5683 tree real_type = GET_REAL_TYPE (type);
5684 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 5685 type = obtain_incomplete_type (type);
e04a16fb
AG
5686 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
5687 jdep = CLASSD_LAST (ctxp->classd_list);
5688 JDEP_MISC (jdep) = name;
5689 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
5690 }
5691 else
5692 parm_decl = build_decl (PARM_DECL, name, type);
5693
5694 BLOCK_CHAIN_DECL (parm_decl);
5695 }
5696 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5697 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
5698 nreverse (tem);
5699 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
5700}
5701
22eed1e6
APB
5702/* Called during parsing. Creates an artificial method declaration. */
5703
5704static tree
5705create_artificial_method (class, flags, type, name, args)
5706 tree class;
5707 int flags;
5708 tree type, name, args;
5709{
5710 int saved_lineno = lineno;
5711 tree mdecl;
5712
5713 lineno = 0;
5714 mdecl = make_node (FUNCTION_TYPE);
5715 TREE_TYPE (mdecl) = type;
5716 TYPE_ARG_TYPES (mdecl) = args;
5717 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
5718 lineno = saved_lineno;
5719 DECL_ARTIFICIAL (mdecl) = 1;
5720 return mdecl;
5721}
5722
5723/* Starts the body if an artifical method. */
5724
5725static void
5726start_artificial_method_body (mdecl)
5727 tree mdecl;
5728{
5729 DECL_SOURCE_LINE (mdecl) = 1;
5730 DECL_SOURCE_LINE_MERGE (mdecl, 1);
5731 source_start_java_method (mdecl);
5732 enter_block ();
5733}
5734
5735static void
5736end_artificial_method_body (mdecl)
5737 tree mdecl;
5738{
5739 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
5740 exit_block ();
5741}
5742
e04a16fb
AG
5743/* Called during expansion. Push decls formerly built from argument
5744 list so they're usable during expansion. */
5745
5746static void
5747expand_start_java_method (fndecl)
5748 tree fndecl;
5749{
5750 tree tem, *ptr;
e04a16fb 5751
e04a16fb
AG
5752 current_function_decl = fndecl;
5753
5754 announce_function (fndecl);
5755 pushlevel (1); /* Push parameters */
5756 ptr = &DECL_ARGUMENTS (fndecl);
5757 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5758 while (tem)
5759 {
5760 tree next = TREE_CHAIN (tem);
b67d701b 5761 tree type = TREE_TYPE (tem);
e438e1b7
JJ
5762 if (PROMOTE_PROTOTYPES
5763 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
5764 && INTEGRAL_TYPE_P (type))
5765 type = integer_type_node;
b67d701b 5766 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
5767 layout_decl (tem, 0);
5768 pushdecl (tem);
e04a16fb
AG
5769 *ptr = tem;
5770 ptr = &TREE_CHAIN (tem);
5771 tem = next;
5772 }
5773 *ptr = NULL_TREE;
5774 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5775 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
5776}
5777
5778/* Terminate a function and expand its body. */
5779
5780static void
5781source_end_java_method ()
5782{
5783 tree fndecl = current_function_decl;
138657ec 5784 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 5785
79d13333
APB
5786 if (!fndecl)
5787 return;
5788
e04a16fb
AG
5789 java_parser_context_save_global ();
5790 lineno = ctxp->last_ccb_indent1;
5791
b67d701b
PB
5792 /* Set EH language codes */
5793 java_set_exception_lang_code ();
5794
5423609c
APB
5795 /* Turn function bodies with only a NOP expr null, so they don't get
5796 generated at all and we won't get warnings when using the -W
5797 -Wall flags. */
5798 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
5799 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
5800
e04a16fb
AG
5801 /* Generate function's code */
5802 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
5803 && ! flag_emit_class_files
5804 && ! flag_emit_xref)
e04a16fb
AG
5805 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
5806
5807 /* pop out of its parameters */
5808 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5809 poplevel (1, 0, 1);
5810 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
5811
5812 /* Generate rtl for function exit. */
e8fc7396 5813 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
5814 {
5815 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
5816 /* Emit catch-finally clauses */
5817 emit_handlers ();
e04a16fb
AG
5818 expand_function_end (input_filename, lineno, 0);
5819
138657ec
AH
5820 /* FIXME: If the current method contains any exception handlers,
5821 force asynchronous_exceptions: this is necessary because signal
5822 handlers in libjava may throw exceptions. This is far from being
5823 a perfect solution, but it's better than doing nothing at all.*/
5824 if (catch_clauses)
5825 asynchronous_exceptions = 1;
5826
e04a16fb
AG
5827 /* Run the optimizers and output assembler code for this function. */
5828 rest_of_compilation (fndecl);
5829 }
5830
5831 current_function_decl = NULL_TREE;
5832 /* permanent_allocation (1); */
5833 java_parser_context_restore_global ();
138657ec 5834 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
5835}
5836
5837/* Record EXPR in the current function block. Complements compound
5838 expression second operand if necessary. */
5839
5840tree
5841java_method_add_stmt (fndecl, expr)
5842 tree fndecl, expr;
5843{
79d13333
APB
5844 if (!fndecl)
5845 return NULL;
f099f336 5846 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 5847}
e04a16fb 5848
b67d701b
PB
5849static tree
5850add_stmt_to_block (b, type, stmt)
5851 tree b, type, stmt;
5852{
5853 tree body = BLOCK_EXPR_BODY (b), c;
5854
e04a16fb
AG
5855 if (java_error_count)
5856 return body;
b67d701b
PB
5857
5858 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
5859 return body;
5860
b67d701b
PB
5861 BLOCK_EXPR_BODY (b) = c;
5862 TREE_SIDE_EFFECTS (c) = 1;
5863 return c;
e04a16fb
AG
5864}
5865
5866/* Add STMT to EXISTING if possible, otherwise create a new
5867 COMPOUND_EXPR and add STMT to it. */
5868
5869static tree
5870add_stmt_to_compound (existing, type, stmt)
5871 tree existing, type, stmt;
5872{
15fdcfe9
PB
5873 if (existing)
5874 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 5875 else
15fdcfe9 5876 return stmt;
e04a16fb
AG
5877}
5878
5879/* Hold THIS for the scope of the current public method decl. */
5880static tree current_this;
5881
1886c9d8
APB
5882void java_layout_seen_class_methods ()
5883{
5884 tree previous_list = all_class_list;
5885 tree end = NULL_TREE;
5886 tree current;
5887
5888 while (1)
5889 {
5890 for (current = previous_list;
5891 current != end; current = TREE_CHAIN (current))
5892 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
5893
5894 if (previous_list != all_class_list)
5895 {
5896 end = previous_list;
5897 previous_list = all_class_list;
5898 }
5899 else
5900 break;
5901 }
5902}
5903
23a79c61
APB
5904/* Layout the methods of all classes loaded in one way on an
5905 other. Check methods of source parsed classes. Then reorder the
5906 fields and layout the classes or the type of all source parsed
5907 classes */
e04a16fb
AG
5908
5909void
5910java_layout_classes ()
5911{
5912 tree current;
bc3ca41b 5913 int save_error_count = java_error_count;
5e942c50 5914
23a79c61 5915 /* Layout the methods of all classes seen so far */
1886c9d8 5916 java_layout_seen_class_methods ();
23a79c61
APB
5917 java_parse_abort_on_error ();
5918 all_class_list = NULL_TREE;
5919
5920 /* Then check the methods of all parsed classes */
5921 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5922 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
5923 CHECK_METHODS (TREE_VALUE (current));
5924 java_parse_abort_on_error ();
5925
5e942c50 5926 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 5927 {
5e942c50 5928 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 5929
c877974e
APB
5930 /* Reverse the fields, but leave the dummy field in front.
5931 Fields are already ordered for Object and Class */
5932 if (TYPE_FIELDS (current_class) && current_class != object_type_node
5933 && current_class != class_type_node)
5934 {
23a79c61
APB
5935 /* If the dummy field is there, reverse the right fields and
5936 just layout the type for proper fields offset */
c877974e
APB
5937 if (!DECL_NAME (TYPE_FIELDS (current_class)))
5938 {
5939 tree fields = TYPE_FIELDS (current_class);
5940 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
5941 TYPE_SIZE (current_class) = NULL_TREE;
5942 layout_type (current_class);
5943 }
23a79c61
APB
5944 /* We don't have a dummy field, we need to layout the class,
5945 after having reversed the fields */
c877974e
APB
5946 else
5947 {
5948 TYPE_FIELDS (current_class) =
5949 nreverse (TYPE_FIELDS (current_class));
5950 TYPE_SIZE (current_class) = NULL_TREE;
5951 layout_class (current_class);
5952 }
5953 }
23a79c61
APB
5954 else
5955 layout_class (current_class);
5e942c50 5956
c877974e
APB
5957 /* From now on, the class is considered completely loaded */
5958 CLASS_LOADED_P (current_class) = 1;
5959
5e942c50
APB
5960 /* Error reported by the caller */
5961 if (java_error_count)
5962 return;
e04a16fb 5963 }
23a79c61
APB
5964
5965 /* We might have reloaded classes durign the process of laying out
5966 classes for code generation. We must layout the methods of those
5967 late additions, as constructor checks might use them */
1886c9d8 5968 java_layout_seen_class_methods ();
23a79c61 5969 java_parse_abort_on_error ();
e04a16fb
AG
5970}
5971
5972/* Expand all methods in all registered classes. */
5973
49f48c71 5974static void
e04a16fb
AG
5975java_complete_expand_methods ()
5976{
5977 tree current;
ce6e9147
APB
5978
5979 do_not_fold = flag_emit_xref;
e04a16fb
AG
5980
5981 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5982 {
7f1d4866 5983 int is_interface;
e04a16fb
AG
5984 tree class_type = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current));
5985 tree decl;
e04a16fb
AG
5986
5987 current_class = TREE_TYPE (current);
7f1d4866 5988 is_interface = CLASS_INTERFACE (TYPE_NAME (current_class));
e04a16fb
AG
5989
5990 /* Initialize a new constant pool */
5991 init_outgoing_cpool ();
5992
7525cc04
APB
5993 /* We want <clinit> (if any) to be processed first. */
5994 decl = tree_last (TYPE_METHODS (class_type));
7f1d4866 5995 if (IS_CLINIT (decl))
7525cc04 5996 {
cd9643f7
PB
5997 tree fbody = DECL_FUNCTION_BODY (decl);
5998 tree list;
5999 if (fbody != NULL_TREE)
6000 {
6001 /* First check if we can ignore empty <clinit> */
6002 tree block_body = BLOCK_EXPR_BODY (fbody);
6003
6004 current_this = NULL_TREE;
6005 current_function_decl = decl;
6006 if (block_body != NULL_TREE)
6007 {
6008 /* Prevent the use of `this' inside <clinit> */
6009 ctxp->explicit_constructor_p = 1;
6010
6011 block_body = java_complete_tree (block_body);
6012 ctxp->explicit_constructor_p = 0;
6013 BLOCK_EXPR_BODY (fbody) = block_body;
6014 if (block_body != NULL_TREE
6015 && TREE_CODE (block_body) == BLOCK
6016 && BLOCK_EXPR_BODY (block_body) == empty_stmt_node)
6017 decl = NULL_TREE;
6018 }
6019 }
6020 list = nreverse (TREE_CHAIN (nreverse (TYPE_METHODS (class_type))));
6021 if (decl != NULL_TREE)
6022 {
6023 TREE_CHAIN (decl) = list;
6024 TYPE_METHODS (class_type) = decl;
6025 }
6026 else
6027 TYPE_METHODS (class_type) = list;
7525cc04 6028 }
7f1d4866
APB
6029
6030 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
6031 {
7f1d4866
APB
6032 current_function_decl = decl;
6033 /* Don't generate debug info on line zero when expanding a
6034 generated constructor. */
6035 if (DECL_CONSTRUCTOR_P (decl) && !DECL_FUNCTION_BODY (decl))
6036 {
6037 /* If we found errors, it's too dangerous to try to
6038 generate and expand a constructor */
6039 if (!java_error_count)
6040 {
6041 restore_line_number_status (1);
6042 java_complete_expand_method (decl);
6043 restore_line_number_status (0);
e04a16fb 6044 }
7f1d4866
APB
6045 }
6046 else if (METHOD_ABSTRACT (decl) || METHOD_NATIVE (decl))
6047 continue;
6048 else
6049 java_complete_expand_method (decl);
6050 }
e04a16fb 6051
22eed1e6
APB
6052 /* Now verify constructor circularity (stop after the first one
6053 we find) */
7f1d4866 6054 if (!is_interface)
22eed1e6
APB
6055 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
6056 if (DECL_CONSTRUCTOR_P (decl) &&
6057 verify_constructor_circularity (decl, decl))
6058 break;
6059
e04a16fb
AG
6060 /* Make the class data, register it and run the rest of decl
6061 compilation on it */
63a212ed
PB
6062 if (!java_error_count)
6063 {
6064 if (flag_emit_class_files)
6065 write_classfile (current_class);
f099f336
APB
6066 if (flag_emit_xref)
6067 expand_xref (current_class);
aabd7048 6068 else if (! flag_syntax_only)
d593dd8c 6069 finish_class ();
63a212ed 6070 }
e04a16fb
AG
6071 }
6072}
6073
b9f7e36c
APB
6074/* Hold a list of catch clauses list. The first element of this list is
6075 the list of the catch clauses of the currently analysed try block. */
6076static tree currently_caught_type_list;
6077
e04a16fb
AG
6078/* Complete and expand a method. */
6079
6080static void
6081java_complete_expand_method (mdecl)
6082 tree mdecl;
6083{
22eed1e6
APB
6084 /* Fix constructors before expanding them */
6085 if (DECL_CONSTRUCTOR_P (mdecl))
6086 fix_constructors (mdecl);
e04a16fb 6087
22eed1e6 6088 /* Expand functions that have a body */
e04a16fb
AG
6089 if (DECL_FUNCTION_BODY (mdecl))
6090 {
9bbc7d9f
PB
6091 tree fbody = DECL_FUNCTION_BODY (mdecl);
6092 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 6093 tree exception_copy = NULL_TREE;
e04a16fb 6094 expand_start_java_method (mdecl);
939d7216 6095 build_result_decl (mdecl);
e04a16fb
AG
6096
6097 current_this
6098 = (!METHOD_STATIC (mdecl) ?
6099 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
6100
ce6e9147
APB
6101 /* Purge the `throws' list of unchecked exceptions. If we're
6102 doing xref, save a copy of the list and re-install it
6103 later. */
6104 if (flag_emit_xref)
6105 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
6106
b9f7e36c
APB
6107 purge_unchecked_exceptions (mdecl);
6108
6109 /* Install exceptions thrown with `throws' */
6110 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
6111
9bbc7d9f 6112 if (block_body != NULL_TREE)
bc3ca41b
PB
6113 {
6114 block_body = java_complete_tree (block_body);
ce6e9147
APB
6115 if (!flag_emit_xref)
6116 check_for_initialization (block_body);
f099f336 6117 ctxp->explicit_constructor_p = 0;
bc3ca41b 6118 }
9bbc7d9f 6119 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 6120
9bbc7d9f 6121 if ((block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
6122 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
6123 && !flag_emit_xref)
82371d41 6124 missing_return_error (current_function_decl);
7525cc04 6125
939d7216
PB
6126 complete_start_java_method (mdecl);
6127
e04a16fb
AG
6128 /* Don't go any further if we've found error(s) during the
6129 expansion */
6130 if (!java_error_count)
6131 source_end_java_method ();
22eed1e6
APB
6132 else
6133 {
6134 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
6135 poplevel (1, 0, 1);
6136 }
b9f7e36c
APB
6137
6138 /* Pop the exceptions and sanity check */
6139 POP_EXCEPTIONS();
6140 if (currently_caught_type_list)
6141 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
6142
6143 if (flag_emit_xref)
6144 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
6145 }
6146}
6147
22eed1e6
APB
6148/* Craft a body for default constructor. Patch existing constructor
6149 bodies with call to super() and field initialization statements if
6150 necessary. */
6151
6152static void
6153fix_constructors (mdecl)
6154 tree mdecl;
6155{
6156 tree body = DECL_FUNCTION_BODY (mdecl);
22eed1e6 6157
22eed1e6
APB
6158 if (!body)
6159 {
89e09b9a
PB
6160 /* The constructor body must be crafted by hand. It's the
6161 constructor we defined when we realize we didn't have the
6162 CLASSNAME() constructor */
6163
22eed1e6
APB
6164 tree compound;
6165
6166 /* It is an error for the compiler to generate a default
6167 constructor if the superclass doesn't have a constructor that
6168 takes no argument */
6169 if (verify_constructor_super ())
6170 {
6171 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (current_class));
49f48c71 6172 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
22eed1e6
APB
6173 parse_error_context (lookup_cl (TYPE_NAME (current_class)),
6174 "No constructor matching `%s()' found in "
6175 "class `%s'", n, n);
6176 }
6177
6178 start_artificial_method_body (mdecl);
6179
6180 /* We don't generate a super constructor invocation if we're
6181 compiling java.lang.Object. build_super_invocation takes care
6182 of that. */
6183 compound = java_method_add_stmt (mdecl, build_super_invocation ());
22eed1e6
APB
6184
6185 end_artificial_method_body (mdecl);
6186 }
6187 /* Search for an explicit constructor invocation */
6188 else
6189 {
6190 int found = 0;
6191 tree main_block = BLOCK_EXPR_BODY (body);
6192 tree compound = NULL_TREE;
6193
6194 while (body)
6195 switch (TREE_CODE (body))
6196 {
6197 case CALL_EXPR:
6198 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
6199 body = NULL_TREE;
6200 break;
6201 case COMPOUND_EXPR:
6202 case EXPR_WITH_FILE_LOCATION:
6203 body = TREE_OPERAND (body, 0);
6204 break;
6205 case BLOCK:
6206 body = BLOCK_EXPR_BODY (body);
6207 break;
6208 default:
6209 found = 0;
6210 body = NULL_TREE;
6211 }
6212 /* The constructor is missing an invocation of super() */
6213 if (!found)
6214 compound = add_stmt_to_compound (compound, NULL_TREE,
6215 build_super_invocation ());
6216
22eed1e6
APB
6217 /* Fix the constructor main block if we're adding extra stmts */
6218 if (compound)
6219 {
6220 compound = add_stmt_to_compound (compound, NULL_TREE,
6221 BLOCK_EXPR_BODY (main_block));
6222 BLOCK_EXPR_BODY (main_block) = compound;
6223 }
6224 }
6225}
6226
6227/* Browse constructors in the super class, searching for a constructor
6228 that doesn't take any argument. Return 0 if one is found, 1
6229 otherwise. */
6230
6231static int
6232verify_constructor_super ()
6233{
6234 tree class = CLASSTYPE_SUPER (current_class);
6235 if (!class)
6236 return 0;
6237
6238 if (class)
6239 {
6240 tree mdecl;
6241 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
6242 {
6243 if (DECL_CONSTRUCTOR_P (mdecl)
d77613be
APB
6244 && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (mdecl)))
6245 == end_params_node)
22eed1e6
APB
6246 return 0;
6247 }
6248 }
6249 return 1;
6250}
6251
e04a16fb
AG
6252/* Expand finals. */
6253
49f48c71 6254static void
e04a16fb
AG
6255java_expand_finals ()
6256{
6257}
6258
22eed1e6 6259/* Generate code for all context remembered for code generation. */
b351b287
APB
6260
6261void
6262java_expand_classes ()
6263{
5423609c 6264 int save_error_count = 0;
23a79c61
APB
6265 java_parse_abort_on_error ();
6266 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
6267 return;
6268 java_layout_classes ();
6269 java_parse_abort_on_error ();
6270
b351b287
APB
6271 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
6272 {
6273 ctxp = ctxp_for_generation;
6274 lang_init_source (2); /* Error msgs have method prototypes */
6275 java_complete_expand_methods (); /* Complete and expand method bodies */
6276 java_parse_abort_on_error ();
6277 java_expand_finals (); /* Expand and check the finals */
6278 java_parse_abort_on_error ();
6279 java_check_final (); /* Check unitialized final */
6280 java_parse_abort_on_error ();
6281 }
b351b287
APB
6282}
6283
e04a16fb
AG
6284/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
6285 a tree list node containing RIGHT. Fore coming RIGHTs will be
6286 chained to this hook. LOCATION contains the location of the
6287 separating `.' operator. */
6288
6289static tree
6290make_qualified_primary (primary, right, location)
6291 tree primary, right;
6292 int location;
6293{
6294 tree wfl;
6295
6296 /* We want to process THIS . xxx symbolicaly, to keep it consistent
6297 with the way we're processing SUPER. A THIS from a primary as a
6298 different form than a SUPER. Turn THIS into something symbolic */
b67d701b 6299 if (TREE_CODE (primary) == THIS_EXPR)
e04a16fb 6300 {
9ee9b555 6301 wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
6302 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
6303 wfl = make_qualified_name (wfl, right, location);
6304 PRIMARY_P (wfl) = 1;
6305 return wfl;
6306 }
6307 /* Other non WFL node are wrapped around a WFL */
6308 else if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
6309 {
6310 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
6311 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
6312 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (primary, NULL_TREE);
6313 }
6314 else
6315 {
6316 wfl = primary;
6317 if (!EXPR_WFL_QUALIFICATION (primary))
6318 EXPR_WFL_QUALIFICATION (primary) =
6319 build_tree_list (primary, NULL_TREE);
6320 }
6321
6322 EXPR_WFL_LINECOL (right) = location;
6323 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
6324 PRIMARY_P (wfl) = 1;
6325 return wfl;
6326}
6327
6328/* Simple merge of two name separated by a `.' */
6329
6330static tree
6331merge_qualified_name (left, right)
6332 tree left, right;
6333{
6334 tree node;
6335 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
6336 IDENTIFIER_LENGTH (left));
6337 obstack_1grow (&temporary_obstack, '.');
6338 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
6339 IDENTIFIER_LENGTH (right));
6340 node = get_identifier (obstack_base (&temporary_obstack));
6341 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
6342 QUALIFIED_P (node) = 1;
6343 return node;
6344}
6345
6346/* Merge the two parts of a qualified name into LEFT. Set the
6347 location information of the resulting node to LOCATION, usually
6348 inherited from the location information of the `.' operator. */
6349
6350static tree
6351make_qualified_name (left, right, location)
6352 tree left, right;
6353 int location;
6354{
bc3ca41b
PB
6355#ifdef USE_COMPONENT_REF
6356 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
6357 EXPR_WFL_LINECOL (node) = location;
6358 return node;
6359#else
e04a16fb
AG
6360 tree left_id = EXPR_WFL_NODE (left);
6361 tree right_id = EXPR_WFL_NODE (right);
6362 tree wfl, merge;
6363
6364 merge = merge_qualified_name (left_id, right_id);
6365
6366 /* Left wasn't qualified and is now qualified */
6367 if (!QUALIFIED_P (left_id))
6368 {
6369 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
6370 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
6371 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
6372 }
6373
6374 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
6375 EXPR_WFL_LINECOL (wfl) = location;
6376 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
6377
6378 EXPR_WFL_NODE (left) = merge;
6379 return left;
bc3ca41b 6380#endif
e04a16fb
AG
6381}
6382
6383/* Extract the last identifier component of the qualified in WFL. The
6384 last identifier is removed from the linked list */
6385
6386static tree
6387cut_identifier_in_qualified (wfl)
6388 tree wfl;
6389{
6390 tree q;
6391 tree previous = NULL_TREE;
6392 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
6393 if (!TREE_CHAIN (q))
6394 {
6395 if (!previous)
6396 fatal ("Operating on a non qualified qualified WFL - "
6397 "cut_identifier_in_qualified");
6398 TREE_CHAIN (previous) = NULL_TREE;
6399 return TREE_PURPOSE (q);
6400 }
6401}
6402
6403/* Resolve the expression name NAME. Return its decl. */
6404
6405static tree
5e942c50 6406resolve_expression_name (id, orig)
e04a16fb 6407 tree id;
5e942c50 6408 tree *orig;
e04a16fb
AG
6409{
6410 tree name = EXPR_WFL_NODE (id);
6411 tree decl;
6412
6413 /* 6.5.5.1: Simple expression names */
6414 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
6415 {
6416 /* 15.13.1: NAME can appear within the scope of a local variable
6417 declaration */
6418 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
6419 return decl;
6420
6421 /* 15.13.1: NAME can appear within a class declaration */
6422 else
6423 {
6424 decl = lookup_field_wrapper (current_class, name);
6425 if (decl)
6426 {
6427 int fs = FIELD_STATIC (decl);
6428 /* Instance variable (8.3.1.1) can't appear within
6429 static method, static initializer or initializer for
6430 a static variable. */
6431 if (!fs && METHOD_STATIC (current_function_decl))
6432 {
7f10c2e2 6433 static_ref_err (id, name, current_class);
e04a16fb
AG
6434 return error_mark_node;
6435 }
22eed1e6
APB
6436 /* Instance variables can't appear as an argument of
6437 an explicit constructor invocation */
6438 if (!fs && ctxp->explicit_constructor_p)
6439 {
6440 parse_error_context
6441 (id, "Can't reference `%s' before the superclass "
6442 "constructor has been called", IDENTIFIER_POINTER (name));
6443 return error_mark_node;
6444 }
5e942c50
APB
6445
6446 /* Otherwise build what it takes to access the field */
e04a16fb 6447 decl = build_field_ref ((fs ? NULL_TREE : current_this),
7f1d4866 6448 DECL_CONTEXT (decl), name);
e8fc7396 6449 if (fs && !flag_emit_class_files && !flag_emit_xref)
7f1d4866 6450 decl = build_class_init (DECL_CONTEXT (decl), decl);
5e942c50
APB
6451 /* We may be asked to save the real field access node */
6452 if (orig)
6453 *orig = decl;
6454 /* And we return what we got */
5b09b33e 6455 return decl;
e04a16fb
AG
6456 }
6457 /* Fall down to error report on undefined variable */
6458 }
6459 }
6460 /* 6.5.5.2 Qualified Expression Names */
6461 else
6462 {
5e942c50
APB
6463 if (orig)
6464 *orig = NULL_TREE;
e04a16fb
AG
6465 qualify_ambiguous_name (id);
6466 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
6467 /* 15.10.2: Accessing Superclass Members using super */
6468 return resolve_field_access (id, NULL, NULL);
6469 }
6470
6471 /* We've got an error here */
6472 parse_error_context (id, "Undefined variable `%s'",
6473 IDENTIFIER_POINTER (name));
6474
6475 return error_mark_node;
6476}
6477
7f10c2e2
APB
6478static void
6479static_ref_err (wfl, field_id, class_type)
6480 tree wfl, field_id, class_type;
6481{
6482 parse_error_context
6483 (wfl,
6484 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
6485 IDENTIFIER_POINTER (field_id),
6486 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
6487}
6488
e04a16fb
AG
6489/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
6490 We return something suitable to generate the field access. We also
6491 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
6492 recipient's address can be null. */
6493
6494static tree
6495resolve_field_access (qual_wfl, field_decl, field_type)
6496 tree qual_wfl;
6497 tree *field_decl, *field_type;
6498{
6499 int is_static = 0;
6500 tree field_ref;
6501 tree decl, where_found, type_found;
6502
6503 if (resolve_qualified_expression_name (qual_wfl, &decl,
6504 &where_found, &type_found))
6505 return error_mark_node;
6506
6507 /* Resolve the LENGTH field of an array here */
6508 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
e8fc7396 6509 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
6510 {
6511 tree length = build_java_array_length_access (where_found);
6512 field_ref =
6513 build_java_arraynull_check (type_found, length, int_type_node);
6514 }
6515 /* We might have been trying to resolve field.method(). In which
6516 case, the resolution is over and decl is the answer */
34f4db93 6517 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 6518 field_ref = decl;
34f4db93 6519 else if (JDECL_P (decl))
e04a16fb 6520 {
5e942c50
APB
6521 int static_final_found = 0;
6522 if (!type_found)
6523 type_found = DECL_CONTEXT (decl);
34f4db93 6524 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
5e942c50
APB
6525 if (FIELD_FINAL (decl)
6526 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
6527 && DECL_LANG_SPECIFIC (decl)
7525cc04 6528 && DECL_INITIAL (decl))
5e942c50 6529 {
7525cc04 6530 field_ref = DECL_INITIAL (decl);
5e942c50
APB
6531 static_final_found = 1;
6532 }
6533 else
7f10c2e2
APB
6534 field_ref = build_field_ref ((is_static && !flag_emit_xref?
6535 NULL_TREE : where_found),
5e942c50 6536 type_found, DECL_NAME (decl));
e04a16fb
AG
6537 if (field_ref == error_mark_node)
6538 return error_mark_node;
e8fc7396
APB
6539 if (is_static && !static_final_found
6540 && !flag_emit_class_files && !flag_emit_xref)
e04a16fb
AG
6541 {
6542 field_ref = build_class_init (type_found, field_ref);
6543 /* If the static field was identified by an expression that
6544 needs to be generated, make the field access a compound
7f10c2e2 6545 expression whose first part is the evaluation of the
e04a16fb 6546 field selector part. */
c877974e
APB
6547 if (where_found && TREE_CODE (where_found) != TYPE_DECL
6548 && TREE_CODE (where_found) != RECORD_TYPE)
e04a16fb
AG
6549 {
6550 tree type = QUAL_DECL_TYPE (field_ref);
8576f094
APB
6551 if (TREE_CODE (type) == RECORD_TYPE)
6552 type = build_pointer_type (type);
e04a16fb
AG
6553 field_ref = build (COMPOUND_EXPR, type, where_found, field_ref);
6554 }
6555 }
6556 }
6557 else
6558 field_ref = decl;
6559
6560 if (field_decl)
6561 *field_decl = decl;
6562 if (field_type)
c877974e
APB
6563 *field_type = (QUAL_DECL_TYPE (decl) ?
6564 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
6565 return field_ref;
6566}
6567
e28cd97b
APB
6568/* If NODE is an access to f static field, strip out the class
6569 initialization part and return the field decl, otherwise, return
6570 NODE. */
6571
6572static tree
6573strip_out_static_field_access_decl (node)
6574 tree node;
6575{
6576 if (TREE_CODE (node) == COMPOUND_EXPR)
6577 {
6578 tree op1 = TREE_OPERAND (node, 1);
6579 if (TREE_CODE (op1) == COMPOUND_EXPR)
6580 {
6581 tree call = TREE_OPERAND (op1, 0);
6582 if (TREE_CODE (call) == CALL_EXPR
6583 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
6584 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
6585 == soft_initclass_node)
6586 return TREE_OPERAND (op1, 1);
6587 }
6588 }
6589 return node;
6590}
6591
e04a16fb
AG
6592/* 6.5.5.2: Qualified Expression Names */
6593
6594static int
6595resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
6596 tree wfl;
6597 tree *found_decl, *type_found, *where_found;
6598{
6599 int from_type = 0; /* Field search initiated from a type */
6600 int from_super = 0, from_cast = 0;
6601 int previous_call_static = 0;
6602 int is_static;
6603 tree decl = NULL_TREE, type = NULL_TREE, q;
c877974e 6604 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
6605
6606 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
6607 {
6608 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
6609 tree ret_decl; /* for EH checking */
6610 int location; /* for EH checking */
e04a16fb
AG
6611
6612 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
6613 switch (TREE_CODE (qual_wfl))
6614 {
6615 case CALL_EXPR:
b67d701b 6616 case NEW_CLASS_EXPR:
e04a16fb
AG
6617 /* If the access to the function call is a non static field,
6618 build the code to access it. */
34f4db93 6619 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 6620 {
ac825856
APB
6621 decl = maybe_access_field (decl, *where_found,
6622 DECL_CONTEXT (decl));
e04a16fb
AG
6623 if (decl == error_mark_node)
6624 return 1;
6625 }
6626 /* And code for the function call */
6627 if (complete_function_arguments (qual_wfl))
6628 return 1;
7705e9db 6629
89e09b9a
PB
6630 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
6631 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
6632 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
6633 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
6634 *where_found = patch_method_invocation (qual_wfl, decl, type,
6635 &is_static, &ret_decl);
e04a16fb
AG
6636 if (*where_found == error_mark_node)
6637 return 1;
6638 *type_found = type = QUAL_DECL_TYPE (*where_found);
6639
7705e9db
APB
6640 /* EH check */
6641 if (location)
6642 check_thrown_exceptions (location, ret_decl);
6643
e04a16fb
AG
6644 /* If the previous call was static and this one is too,
6645 build a compound expression to hold the two (because in
6646 that case, previous function calls aren't transported as
6647 forcoming function's argument. */
6648 if (previous_call_static && is_static)
6649 {
6650 decl = build (COMPOUND_EXPR, type, decl, *where_found);
6651 TREE_SIDE_EFFECTS (decl) = 1;
6652 }
6653 else
6654 {
6655 previous_call_static = is_static;
6656 decl = *where_found;
6657 }
6658 continue;
6659
d8fccff5
APB
6660 case NEW_ARRAY_EXPR:
6661 *where_found = decl = java_complete_tree (qual_wfl);
6662 if (decl == error_mark_node)
6663 return 1;
6664 *type_found = type = QUAL_DECL_TYPE (decl);
6665 CLASS_LOADED_P (type) = 1;
6666 continue;
6667
e04a16fb
AG
6668 case CONVERT_EXPR:
6669 *where_found = decl = java_complete_tree (qual_wfl);
6670 if (decl == error_mark_node)
6671 return 1;
6672 *type_found = type = QUAL_DECL_TYPE (decl);
6673 from_cast = 1;
6674 continue;
6675
22eed1e6 6676 case CONDITIONAL_EXPR:
5e942c50 6677 case STRING_CST:
22eed1e6
APB
6678 *where_found = decl = java_complete_tree (qual_wfl);
6679 if (decl == error_mark_node)
6680 return 1;
6681 *type_found = type = QUAL_DECL_TYPE (decl);
6682 continue;
6683
e04a16fb
AG
6684 case ARRAY_REF:
6685 /* If the access to the function call is a non static field,
6686 build the code to access it. */
34f4db93 6687 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
6688 {
6689 decl = maybe_access_field (decl, *where_found, type);
6690 if (decl == error_mark_node)
6691 return 1;
6692 }
6693 /* And code for the array reference expression */
6694 decl = java_complete_tree (qual_wfl);
6695 if (decl == error_mark_node)
6696 return 1;
6697 type = QUAL_DECL_TYPE (decl);
6698 continue;
0a2138e2
APB
6699
6700 default:
6701 /* Fix for -Wall Just go to the next statement. Don't
6702 continue */
a3f406ce 6703 break;
e04a16fb
AG
6704 }
6705
6706 /* If we fall here, we weren't processing a (static) function call. */
6707 previous_call_static = 0;
6708
6709 /* It can be the keyword THIS */
6710 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
6711 {
6712 if (!current_this)
6713 {
6714 parse_error_context
6715 (wfl, "Keyword `this' used outside allowed context");
6716 return 1;
6717 }
6718 /* We have to generate code for intermediate acess */
6719 *where_found = decl = current_this;
5e942c50 6720 *type_found = type = QUAL_DECL_TYPE (decl);
e04a16fb
AG
6721 continue;
6722 }
6723
6724 /* 15.10.2 Accessing Superclass Members using SUPER */
6725 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
6726 {
6727 tree node;
6728 /* Check on the restricted use of SUPER */
6729 if (METHOD_STATIC (current_function_decl)
6730 || current_class == object_type_node)
6731 {
6732 parse_error_context
6733 (wfl, "Keyword `super' used outside allowed context");
6734 return 1;
6735 }
6736 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
6737 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
6738 CLASSTYPE_SUPER (current_class),
6739 build_this (EXPR_WFL_LINECOL (qual_wfl)));
6740 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
6741 if (decl == error_mark_node)
6742 return 1;
e04a16fb
AG
6743 *type_found = type = QUAL_DECL_TYPE (decl);
6744 from_super = from_type = 1;
6745 continue;
6746 }
6747
6748 /* 15.13.1: Can't search for field name in packages, so we
6749 assume a variable/class name was meant. */
6750 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
6751 {
5e942c50
APB
6752 tree name = resolve_package (wfl, &q);
6753 if (name)
6754 {
6755 *where_found = decl = resolve_no_layout (name, qual_wfl);
6756 /* We wan't to be absolutely that the class is laid
6757 out. We're going to search something inside it. */
6758 *type_found = type = TREE_TYPE (decl);
6759 layout_class (type);
6760 from_type = 1;
6761 /* Should be a list, really. FIXME */
6762 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 1;
6763 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 0;
6764 }
e04a16fb 6765 else
5e942c50
APB
6766 {
6767 if (from_super || from_cast)
6768 parse_error_context
6769 ((from_cast ? qual_wfl : wfl),
6770 "No variable `%s' defined in class `%s'",
6771 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6772 lang_printable_name (type, 0));
6773 else
6774 parse_error_context
6775 (qual_wfl, "Undefined variable or class name: `%s'",
6776 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
6777 return 1;
6778 }
e04a16fb
AG
6779 }
6780
6781 /* We have a type name. It's been already resolved when the
6782 expression was qualified. */
6783 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
6784 {
6785 if (!(decl = QUAL_RESOLUTION (q)))
6786 return 1; /* Error reported already */
6787
6788 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
6789 {
6790 parse_error_context
6791 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
6792 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 6793 GET_TYPE_NAME (type),
e04a16fb
AG
6794 IDENTIFIER_POINTER (DECL_NAME (decl)),
6795 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6796 return 1;
6797 }
5e942c50 6798 check_deprecation (qual_wfl, decl);
e04a16fb
AG
6799
6800 type = TREE_TYPE (decl);
6801 from_type = 1;
6802 }
6803 /* We resolve and expression name */
6804 else
6805 {
cd531a2e 6806 tree field_decl = NULL_TREE;
e04a16fb
AG
6807
6808 /* If there exists an early resolution, use it. That occurs
6809 only once and we know that there are more things to
6810 come. Don't do that when processing something after SUPER
6811 (we need more thing to be put in place below */
6812 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
6813 {
6814 decl = QUAL_RESOLUTION (q);
c877974e 6815 if (!type)
5e942c50 6816 {
7f10c2e2
APB
6817 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
6818 {
6819 if (current_this)
6820 *where_found = current_this;
6821 else
6822 {
6823 static_ref_err (qual_wfl, DECL_NAME (decl),
6824 current_class);
6825 return 1;
6826 }
6827 }
c877974e
APB
6828 else
6829 {
6830 *where_found = TREE_TYPE (decl);
6831 if (TREE_CODE (*where_found) == POINTER_TYPE)
6832 *where_found = TREE_TYPE (*where_found);
6833 }
5e942c50 6834 }
b67d701b 6835 }
e04a16fb
AG
6836
6837 /* We have to search for a field, knowing the type of its
6838 container. The flag FROM_TYPE indicates that we resolved
6839 the last member of the expression as a type name, which
5e942c50
APB
6840 means that for the resolution of this field, we'll look
6841 for other errors than if it was resolved as a member of
6842 an other field. */
e04a16fb
AG
6843 else
6844 {
6845 int is_static;
5e942c50
APB
6846 tree field_decl_type; /* For layout */
6847
e04a16fb
AG
6848 if (!from_type && !JREFERENCE_TYPE_P (type))
6849 {
6850 parse_error_context
6851 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
6852 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 6853 lang_printable_name (type, 0),
e04a16fb
AG
6854 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6855 return 1;
6856 }
6857
dc0b3eff
PB
6858 field_decl = lookup_field_wrapper (type,
6859 EXPR_WFL_NODE (qual_wfl));
6860 if (field_decl == NULL_TREE)
e04a16fb
AG
6861 {
6862 parse_error_context
2aa11e97 6863 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 6864 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 6865 GET_TYPE_NAME (type));
e04a16fb
AG
6866 return 1;
6867 }
dc0b3eff
PB
6868 if (field_decl == error_mark_node)
6869 return 1;
5e942c50
APB
6870
6871 /* Layout the type of field_decl, since we may need
c877974e
APB
6872 it. Don't do primitive types or loaded classes. The
6873 situation of non primitive arrays may not handled
6874 properly here. FIXME */
5e942c50
APB
6875 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
6876 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
6877 else
6878 field_decl_type = TREE_TYPE (field_decl);
6879 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
6880 && !CLASS_LOADED_P (field_decl_type)
6881 && !TYPE_ARRAY_P (field_decl_type))
6882 resolve_and_layout (field_decl_type, NULL_TREE);
6883 if (TYPE_ARRAY_P (field_decl_type))
6884 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
6885
6886 /* Check on accessibility here */
6887 if (not_accessible_p (type, field_decl, from_super))
6888 {
6889 parse_error_context
6890 (qual_wfl,
6891 "Can't access %s field `%s.%s' from `%s'",
6892 java_accstring_lookup
6893 (get_access_flags_from_decl (field_decl)),
2aa11e97 6894 GET_TYPE_NAME (type),
e04a16fb
AG
6895 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
6896 IDENTIFIER_POINTER
6897 (DECL_NAME (TYPE_NAME (current_class))));
6898 return 1;
6899 }
5e942c50 6900 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
6901
6902 /* There are things to check when fields are accessed
6903 from type. There are no restrictions on a static
6904 declaration of the field when it is accessed from an
6905 interface */
6906 is_static = FIELD_STATIC (field_decl);
6907 if (!from_super && from_type
6908 && !TYPE_INTERFACE_P (type) && !is_static)
6909 {
7f10c2e2 6910 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
6911 return 1;
6912 }
6913 from_cast = from_super = 0;
6914
5e942c50
APB
6915 /* If we need to generate something to get a proper
6916 handle on what this field is accessed from, do it
6917 now. */
e04a16fb
AG
6918 if (!is_static)
6919 {
c583dd46 6920 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
6921 if (decl == error_mark_node)
6922 return 1;
6923 }
6924
6925 /* We want to keep the location were found it, and the type
6926 we found. */
6927 *where_found = decl;
6928 *type_found = type;
6929
6930 /* This is the decl found and eventually the next one to
6931 search from */
6932 decl = field_decl;
6933 }
e04a16fb
AG
6934 from_type = 0;
6935 type = QUAL_DECL_TYPE (decl);
6936 }
6937 }
6938 *found_decl = decl;
6939 return 0;
6940}
6941
6942/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
6943 can't be accessed from REFERENCE (a record type). */
6944
be245ac0
KG
6945static int
6946not_accessible_p (reference, member, from_super)
e04a16fb
AG
6947 tree reference, member;
6948 int from_super;
6949{
6950 int access_flag = get_access_flags_from_decl (member);
6951
6952 /* Access always granted for members declared public */
6953 if (access_flag & ACC_PUBLIC)
6954 return 0;
6955
6956 /* Check access on protected members */
6957 if (access_flag & ACC_PROTECTED)
6958 {
6959 /* Access granted if it occurs from within the package
6960 containing the class in which the protected member is
6961 declared */
6962 if (class_in_current_package (DECL_CONTEXT (member)))
6963 return 0;
6964
9bbc7d9f
PB
6965 /* If accessed with the form `super.member', then access is granted */
6966 if (from_super)
6967 return 0;
e04a16fb 6968
9bbc7d9f
PB
6969 /* Otherwise, access is granted if occuring from the class where
6970 member is declared or a subclass of it */
6971 if (inherits_from_p (reference, current_class))
6972 return 0;
e04a16fb
AG
6973 return 1;
6974 }
6975
6976 /* Check access on private members. Access is granted only if it
c877974e 6977 occurs from within the class in witch it is declared */
e04a16fb
AG
6978 if (access_flag & ACC_PRIVATE)
6979 return (current_class == DECL_CONTEXT (member) ? 0 : 1);
6980
6981 /* Default access are permitted only when occuring within the
6982 package in which the type (REFERENCE) is declared. In other words,
6983 REFERENCE is defined in the current package */
6984 if (ctxp->package)
6985 return !class_in_current_package (reference);
6986
6987 /* Otherwise, access is granted */
6988 return 0;
6989}
6990
5e942c50
APB
6991/* Test deprecated decl access. */
6992static void
6993check_deprecation (wfl, decl)
6994 tree wfl, decl;
6995{
49f48c71 6996 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
6997 /* Complain if the field is deprecated and the file it was defined
6998 in isn't compiled at the same time the file which contains its
6999 use is */
7000 if (DECL_DEPRECATED (decl)
7001 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
7002 {
7003 char the [20];
7004 switch (TREE_CODE (decl))
7005 {
7006 case FUNCTION_DECL:
7007 strcpy (the, "method");
7008 break;
7009 case FIELD_DECL:
7010 strcpy (the, "field");
7011 break;
7012 case TYPE_DECL:
7013 strcpy (the, "class");
7014 break;
15fdcfe9
PB
7015 default:
7016 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
7017 }
7018 parse_warning_context
7019 (wfl, "The %s `%s' in class `%s' has been deprecated",
7020 the, lang_printable_name (decl, 0),
7021 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
7022 }
7023}
7024
e04a16fb
AG
7025/* Returns 1 if class was declared in the current package, 0 otherwise */
7026
7027static int
7028class_in_current_package (class)
7029 tree class;
7030{
7031 static tree cache = NULL_TREE;
7032 int qualified_flag;
7033 tree left;
7034
7035 if (cache == class)
7036 return 1;
7037
7038 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
7039
7040 /* If the current package is empty and the name of CLASS is
7041 qualified, class isn't in the current package. If there is a
7042 current package and the name of the CLASS is not qualified, class
7043 isn't in the current package */
0a2138e2 7044 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
7045 return 0;
7046
7047 /* If there is not package and the name of CLASS isn't qualified,
7048 they belong to the same unnamed package */
7049 if (!ctxp->package && !qualified_flag)
7050 return 1;
7051
7052 /* Compare the left part of the name of CLASS with the package name */
7053 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
7054 if (ctxp->package == left)
7055 {
7056 cache = class;
7057 return 1;
7058 }
7059 return 0;
7060}
7061
7062/* This function may generate code to access DECL from WHERE. This is
7063 done only if certain conditions meet. */
7064
7065static tree
7066maybe_access_field (decl, where, type)
7067 tree decl, where, type;
7068{
5e942c50
APB
7069 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
7070 && !FIELD_STATIC (decl))
e04a16fb 7071 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
7072 (type ? type : DECL_CONTEXT (decl)),
7073 DECL_NAME (decl));
e04a16fb
AG
7074 return decl;
7075}
7076
15fdcfe9 7077/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
7078 and according to the situation, PRIMARY and WHERE may be
7079 used. IS_STATIC is set to 1 if the invoked function is static. */
7080
7081static tree
89e09b9a 7082patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
7083 tree patch, primary, where;
7084 int *is_static;
b9f7e36c 7085 tree *ret_decl;
e04a16fb
AG
7086{
7087 tree wfl = TREE_OPERAND (patch, 0);
7088 tree args = TREE_OPERAND (patch, 1);
7089 tree name = EXPR_WFL_NODE (wfl);
5e942c50 7090 tree list;
22eed1e6 7091 int is_static_flag = 0;
89e09b9a 7092 int is_super_init = 0;
bccaf73a 7093 tree this_arg = NULL_TREE;
e04a16fb
AG
7094
7095 /* Should be overriden if everything goes well. Otherwise, if
7096 something fails, it should keep this value. It stop the
7097 evaluation of a bogus assignment. See java_complete_tree,
7098 MODIFY_EXPR: for the reasons why we sometimes want to keep on
7099 evaluating an assignment */
7100 TREE_TYPE (patch) = error_mark_node;
7101
7102 /* Since lookup functions are messing with line numbers, save the
7103 context now. */
7104 java_parser_context_save_global ();
7105
7106 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
7107
7108 /* Resolution of qualified name, excluding constructors */
7109 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
7110 {
7111 tree class_decl, identifier, identifier_wfl;
7112 /* Extract the last IDENTIFIER of the qualified
7113 expression. This is a wfl and we will use it's location
7114 data during error report. */
7115 identifier_wfl = cut_identifier_in_qualified (wfl);
7116 identifier = EXPR_WFL_NODE (identifier_wfl);
7117
7118 /* Given the context, IDENTIFIER is syntactically qualified
7119 as a MethodName. We need to qualify what's before */
7120 qualify_ambiguous_name (wfl);
7121
2c56429a 7122 /* Package resolution */
e04a16fb
AG
7123 if (RESOLVE_PACKAGE_NAME_P (wfl))
7124 {
2c56429a
APB
7125 tree next, decl, name = resolve_package (wfl, &next);
7126
7127 if (!name)
7128 {
7129 tree remainder;
7130 breakdown_qualified (&remainder, NULL, EXPR_WFL_NODE (wfl));
7131 parse_error_context (wfl, "Can't search method `%s' in package "
7132 "`%s'",IDENTIFIER_POINTER (identifier),
7133 IDENTIFIER_POINTER (remainder));
7134 PATCH_METHOD_RETURN_ERROR ();
7135 }
7136 RESOLVE_PACKAGE_NAME_P (wfl) = 0;
7137 if ((decl = resolve_no_layout (name, QUAL_WFL (next))))
7138 {
7139 QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl)) = decl;
7140 RESOLVE_EXPRESSION_NAME_P (wfl) = 0;
7141 RESOLVE_TYPE_NAME_P (wfl) = 1;
7142 }
7143 else
7144 {
7145 RESOLVE_EXPRESSION_NAME_P (wfl) = 1;
7146 RESOLVE_TYPE_NAME_P (wfl) = 0;
7147 }
e04a16fb 7148 }
2c56429a 7149
e04a16fb 7150 /* We're resolving a call from a type */
2c56429a 7151 if (RESOLVE_TYPE_NAME_P (wfl))
e04a16fb
AG
7152 {
7153 tree decl = QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl));
7154 tree name = DECL_NAME (decl);
7155 tree type;
7156
7157 class_decl = resolve_and_layout (name, wfl);
7158 if (CLASS_INTERFACE (decl))
7159 {
7160 parse_error_context
7161 (identifier_wfl, "Can't make static reference to method "
7162 "`%s' in interface `%s'", IDENTIFIER_POINTER (identifier),
7163 IDENTIFIER_POINTER (name));
b9f7e36c 7164 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
7165 }
7166 /* Look the method up in the type selector. The method ought
7167 to be static. */
7168 type = TREE_TYPE (class_decl);
7169 list = lookup_method_invoke (0, wfl, type, identifier, args);
7170 if (list && !METHOD_STATIC (list))
7171 {
0a2138e2 7172 char *fct_name = strdup (lang_printable_name (list, 0));
e04a16fb
AG
7173 parse_error_context
7174 (identifier_wfl,
7175 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
7176 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
7177 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 7178 free (fct_name);
b9f7e36c 7179 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 7180 }
5e942c50 7181 args = nreverse (args);
e04a16fb
AG
7182 }
7183 /* We're resolving an expression name */
7184 else
7185 {
7186 tree field, type;
7187
7188 /* 1- Find the field to which the call applies */
7189 field = resolve_field_access (wfl, NULL, &type);
7190 if (field == error_mark_node)
b9f7e36c 7191 PATCH_METHOD_RETURN_ERROR ();
c3f2a476
APB
7192 /* field is used in lieu of a primary. It alows us not to
7193 report errors on erroneous use of `this' in
7194 constructors. */
7195 primary = field;
e04a16fb
AG
7196
7197 /* 2- Do the layout of the class where the last field
7198 was found, so we can search it. */
c877974e 7199 class_decl = resolve_and_layout (type, NULL_TREE);
3e78f871 7200 if (class_decl != NULL_TREE)
c877974e
APB
7201 type = TREE_TYPE (class_decl);
7202
e04a16fb
AG
7203 /* 3- Retrieve a filtered list of method matches, Refine
7204 if necessary. In any cases, point out errors. */
7205 list = lookup_method_invoke (0, identifier_wfl, type,
7206 identifier, args);
7207
7208 /* 4- Add the field as an argument */
bccaf73a
PB
7209 args = nreverse (args);
7210 this_arg = field;
e04a16fb
AG
7211 }
7212
5e942c50 7213 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
7214 wfl = identifier_wfl;
7215 }
7216 /* Resolution of simple names, names generated after a primary: or
7217 constructors */
7218 else
7219 {
cd531a2e 7220 tree class_to_search = NULL_TREE;
e04a16fb
AG
7221 int lc; /* Looking for Constructor */
7222
7223 /* We search constructor in their target class */
7224 if (CALL_CONSTRUCTOR_P (patch))
7225 {
22eed1e6
APB
7226 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
7227 class_to_search = EXPR_WFL_NODE (wfl);
7228 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
7229 this_identifier_node)
7230 class_to_search = NULL_TREE;
7231 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
7232 super_identifier_node)
e04a16fb 7233 {
89e09b9a 7234 is_super_init = 1;
22eed1e6
APB
7235 if (CLASSTYPE_SUPER (current_class))
7236 class_to_search =
7237 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
7238 else
7239 {
7240 parse_error_context (wfl, "Can't invoke super constructor "
7241 "on java.lang.Object");
7242 PATCH_METHOD_RETURN_ERROR ();
7243 }
e04a16fb 7244 }
22eed1e6
APB
7245
7246 /* Class to search is NULL if we're searching the current one */
7247 if (class_to_search)
e04a16fb 7248 {
23a79c61
APB
7249 class_to_search = resolve_and_layout (class_to_search,
7250 NULL_TREE);
22eed1e6
APB
7251 if (!class_to_search)
7252 {
7253 parse_error_context
7254 (wfl, "Class `%s' not found in type declaration",
7255 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
7256 PATCH_METHOD_RETURN_ERROR ();
7257 }
7258
5e942c50
APB
7259 /* Can't instantiate an abstract class, but we can
7260 invoke it's constructor. It's use within the `new'
7261 context is denied here. */
7262 if (CLASS_ABSTRACT (class_to_search)
7263 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
7264 {
7265 parse_error_context
7266 (wfl, "Class `%s' is an abstract class. It can't be "
7267 "instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
7268 PATCH_METHOD_RETURN_ERROR ();
7269 }
7270 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 7271 }
22eed1e6
APB
7272 else
7273 class_to_search = current_class;
e04a16fb
AG
7274 lc = 1;
7275 }
7276 /* This is a regular search in the local class, unless an
7277 alternate class is specified. */
7278 else
7279 {
7280 class_to_search = (where ? where : current_class);
7281 lc = 0;
7282 }
7283
7284 /* NAME is a simple identifier or comes from a primary. Search
7285 in the class whose declaration contain the method being
7286 invoked. */
c877974e 7287 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb
AG
7288 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
7289
7290 /* Don't continue if no method were found, as the next statement
7291 can't be executed then. */
b9f7e36c
APB
7292 if (!list)
7293 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
7294
7295 /* Check for static reference if non static methods */
7296 if (check_for_static_method_reference (wfl, patch, list,
7297 class_to_search, primary))
b9f7e36c 7298 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 7299
22eed1e6
APB
7300 /* Non static methods are called with the current object extra
7301 argument. If patch a `new TYPE()', the argument is the value
7302 returned by the object allocator. If method is resolved as a
7303 primary, use the primary otherwise use the current THIS. */
b9f7e36c 7304 args = nreverse (args);
bccaf73a
PB
7305 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
7306 this_arg = primary ? primary : current_this;
e04a16fb 7307 }
b67d701b 7308
e04a16fb
AG
7309 /* Merge point of all resolution schemes. If we have nothing, this
7310 is an error, already signaled */
b9f7e36c
APB
7311 if (!list)
7312 PATCH_METHOD_RETURN_ERROR ();
b67d701b 7313
e04a16fb
AG
7314 /* Check accessibility, position the is_static flag, build and
7315 return the call */
9bbc7d9f 7316 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 7317 {
0a2138e2 7318 char *fct_name = strdup (lang_printable_name (list, 0));
e04a16fb
AG
7319 parse_error_context
7320 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
7321 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 7322 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
7323 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
7324 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 7325 free (fct_name);
b9f7e36c 7326 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 7327 }
5e942c50 7328 check_deprecation (wfl, list);
22eed1e6
APB
7329
7330 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
7331 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
7332 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 7333
c3f2a476
APB
7334 /* In the context of an explicit constructor invocation, we can't
7335 invoke any method relying on `this'. Exceptions are: we're
7336 invoking a static function, primary exists and is not the current
7337 this, we're creating a new object. */
22eed1e6 7338 if (ctxp->explicit_constructor_p
c3f2a476
APB
7339 && !is_static_flag
7340 && (!primary || primary == current_this)
7341 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6
APB
7342 {
7343 parse_error_context
7344 (wfl, "Can't reference `this' before the superclass constructor has "
7345 "been called");
7346 PATCH_METHOD_RETURN_ERROR ();
7347 }
e04a16fb 7348 java_parser_context_restore_global ();
22eed1e6
APB
7349 if (is_static)
7350 *is_static = is_static_flag;
b9f7e36c
APB
7351 /* Sometimes, we want the decl of the selected method. Such as for
7352 EH checking */
7353 if (ret_decl)
7354 *ret_decl = list;
89e09b9a
PB
7355 patch = patch_invoke (patch, list, args);
7356 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
7357 {
7358 /* Generate the code used to initialize fields declared with an
7359 initialization statement. For now, it returns a call the the
7360 artificial function $finit$, if required. */
7361
7362 tree finit_call =
7363 build_method_invocation (build_expr_wfl (finit_identifier_node,
7364 input_filename, 0, 0),
7365 NULL_TREE);
7366 patch = build (COMPOUND_EXPR, void_type_node, patch,
7367 java_complete_tree (finit_call));
7368 CAN_COMPLETE_NORMALLY (patch) = 1;
7369 }
7370 return patch;
e04a16fb
AG
7371}
7372
7373/* Check that we're not trying to do a static reference to a method in
7374 non static method. Return 1 if it's the case, 0 otherwise. */
7375
7376static int
7377check_for_static_method_reference (wfl, node, method, where, primary)
7378 tree wfl, node, method, where, primary;
7379{
7380 if (METHOD_STATIC (current_function_decl)
7381 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
7382 {
0a2138e2 7383 char *fct_name = strdup (lang_printable_name (method, 0));
e04a16fb
AG
7384 parse_error_context
7385 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 7386 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
7387 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
7388 free (fct_name);
7389 return 1;
7390 }
7391 return 0;
7392}
7393
7394/* Patch an invoke expression METHOD and ARGS, based on its invocation
7395 mode. */
7396
7397static tree
89e09b9a 7398patch_invoke (patch, method, args)
e04a16fb 7399 tree patch, method, args;
e04a16fb
AG
7400{
7401 tree dtable, func;
0a2138e2 7402 tree original_call, t, ta;
e04a16fb 7403
5e942c50
APB
7404 /* Last step for args: convert build-in types. If we're dealing with
7405 a new TYPE() type call, the first argument to the constructor
7406 isn't found in the incomming argument list, but delivered by
7407 `new' */
7408 t = TYPE_ARG_TYPES (TREE_TYPE (method));
7409 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
7410 t = TREE_CHAIN (t);
ac825856
APB
7411 for (ta = args; t != end_params_node && ta;
7412 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
7413 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
7414 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
7415 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
7416
7417 /* Resolve unresolved returned type isses */
7418 t = TREE_TYPE (TREE_TYPE (method));
7419 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
7420 resolve_and_layout (TREE_TYPE (t), NULL);
22eed1e6 7421
e8fc7396 7422 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
7423 func = method;
7424 else
e04a16fb 7425 {
15fdcfe9 7426 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 7427 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
7428 {
7429 case INVOKE_VIRTUAL:
7430 dtable = invoke_build_dtable (0, args);
7431 func = build_invokevirtual (dtable, method);
7432 break;
b9f7e36c 7433
15fdcfe9
PB
7434 case INVOKE_SUPER:
7435 case INVOKE_STATIC:
7436 func = build_known_method_ref (method, TREE_TYPE (method),
7437 DECL_CONTEXT (method),
7438 signature, args);
7439 break;
e04a16fb 7440
15fdcfe9
PB
7441 case INVOKE_INTERFACE:
7442 dtable = invoke_build_dtable (1, args);
7443 func = build_invokeinterface (dtable, DECL_NAME (method), signature);
7444 break;
5e942c50 7445
15fdcfe9 7446 default:
89e09b9a 7447 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
7448 }
7449
7450 /* Ensure self_type is initialized, (invokestatic). FIXME */
7451 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
7452 }
7453
e04a16fb
AG
7454 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
7455 TREE_OPERAND (patch, 0) = func;
7456 TREE_OPERAND (patch, 1) = args;
7457 original_call = patch;
7458
22eed1e6
APB
7459 /* We're processing a `new TYPE ()' form. New is called an its
7460 returned value is the first argument to the constructor. We build
7461 a COMPOUND_EXPR and use saved expression so that the overall NEW
7462 expression value is a pointer to a newly created and initialized
7463 class. */
7464 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
7465 {
7466 tree class = DECL_CONTEXT (method);
7467 tree c1, saved_new, size, new;
e8fc7396 7468 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
7469 {
7470 TREE_TYPE (patch) = build_pointer_type (class);
7471 return patch;
7472 }
e04a16fb
AG
7473 if (!TYPE_SIZE (class))
7474 safe_layout_class (class);
7475 size = size_in_bytes (class);
7476 new = build (CALL_EXPR, promote_type (class),
7477 build_address_of (alloc_object_node),
7478 tree_cons (NULL_TREE, build_class_ref (class),
7479 build_tree_list (NULL_TREE,
7480 size_in_bytes (class))),
7481 NULL_TREE);
7482 saved_new = save_expr (new);
7483 c1 = build_tree_list (NULL_TREE, saved_new);
7484 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
7485 TREE_OPERAND (original_call, 1) = c1;
7486 TREE_SET_CODE (original_call, CALL_EXPR);
7487 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
7488 }
7489 return patch;
7490}
7491
7492static int
7493invocation_mode (method, super)
7494 tree method;
7495 int super;
7496{
7497 int access = get_access_flags_from_decl (method);
7498
22eed1e6
APB
7499 if (super)
7500 return INVOKE_SUPER;
7501
82371d41 7502 if (access & ACC_STATIC || access & ACC_FINAL || access & ACC_PRIVATE)
e04a16fb
AG
7503 return INVOKE_STATIC;
7504
7505 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
7506 return INVOKE_STATIC;
7507
e04a16fb
AG
7508 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
7509 return INVOKE_INTERFACE;
7510
7511 if (DECL_CONSTRUCTOR_P (method))
7512 return INVOKE_STATIC;
22eed1e6 7513
e04a16fb
AG
7514 return INVOKE_VIRTUAL;
7515}
7516
b67d701b
PB
7517/* Retrieve a refined list of matching methods. It covers the step
7518 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
7519
7520static tree
7521lookup_method_invoke (lc, cl, class, name, arg_list)
7522 int lc;
7523 tree cl;
7524 tree class, name, arg_list;
7525{
de4c7b02 7526 tree atl = end_params_node; /* Arg Type List */
c877974e 7527 tree method, signature, list, node;
49f48c71 7528 const char *candidates; /* Used for error report */
e04a16fb 7529
5e942c50 7530 /* Fix the arguments */
e04a16fb
AG
7531 for (node = arg_list; node; node = TREE_CHAIN (node))
7532 {
e3884b71 7533 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 7534 /* Non primitive type may have to be resolved */
e3884b71 7535 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
7536 resolve_and_layout (current_arg, NULL_TREE);
7537 /* And promoted */
b67d701b 7538 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 7539 current_arg = promote_type (current_arg);
5e942c50 7540 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 7541 }
e04a16fb 7542
5e942c50
APB
7543 /* Find all candidates and then refine the list, searching for the
7544 most specific method. */
7545 list = find_applicable_accessible_methods_list (lc, class, name, atl);
7546 list = find_most_specific_methods_list (list);
b67d701b
PB
7547 if (list && !TREE_CHAIN (list))
7548 return TREE_VALUE (list);
e04a16fb 7549
b67d701b
PB
7550 /* Issue an error. List candidates if any. Candidates are listed
7551 only if accessible (non accessible methods may end-up here for
7552 the sake of a better error report). */
7553 candidates = NULL;
7554 if (list)
e04a16fb 7555 {
e04a16fb 7556 tree current;
b67d701b 7557 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
7558 for (current = list; current; current = TREE_CHAIN (current))
7559 {
b67d701b
PB
7560 tree cm = TREE_VALUE (current);
7561 char string [4096];
7562 if (!cm || not_accessible_p (class, cm, 0))
7563 continue;
b67d701b 7564 sprintf
22eed1e6
APB
7565 (string, " `%s' in `%s'%s",
7566 get_printable_method_name (cm),
b67d701b
PB
7567 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
7568 (TREE_CHAIN (current) ? "\n" : ""));
7569 obstack_grow (&temporary_obstack, string, strlen (string));
7570 }
7571 obstack_1grow (&temporary_obstack, '\0');
7572 candidates = obstack_finish (&temporary_obstack);
7573 }
7574 /* Issue the error message */
c877974e
APB
7575 method = make_node (FUNCTION_TYPE);
7576 TYPE_ARG_TYPES (method) = atl;
b67d701b 7577 signature = build_java_argument_signature (method);
22eed1e6
APB
7578 parse_error_context (cl, "Can't find %s `%s(%s)' in class `%s'%s",
7579 (lc ? "constructor" : "method"),
7580 (lc ?
d77613be 7581 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))) :
22eed1e6 7582 IDENTIFIER_POINTER (name)),
b67d701b
PB
7583 IDENTIFIER_POINTER (signature),
7584 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))),
7585 (candidates ? candidates : ""));
7586 return NULL_TREE;
7587}
7588
5e942c50
APB
7589/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
7590 when we're looking for a constructor. */
b67d701b
PB
7591
7592static tree
5e942c50
APB
7593find_applicable_accessible_methods_list (lc, class, name, arglist)
7594 int lc;
b67d701b
PB
7595 tree class, name, arglist;
7596{
b67d701b
PB
7597 tree list = NULL_TREE, all_list = NULL_TREE;
7598
1982388a
APB
7599 /* Search interfaces */
7600 if (CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 7601 {
de0b553f
APB
7602 static tree searched_interfaces = NULL_TREE;
7603 static int search_not_done = 0;
1982388a
APB
7604 int i, n;
7605 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
7606
de0b553f
APB
7607 /* Have we searched this interface already? */
7608 if (searched_interfaces)
7609 {
7610 tree current;
7611 for (current = searched_interfaces;
7612 current; current = TREE_CHAIN (current))
7613 if (TREE_VALUE (current) == class)
7614 return NULL;
7615 }
7616 searched_interfaces = tree_cons (NULL_TREE, class, searched_interfaces);
7617
1982388a
APB
7618 search_applicable_methods_list
7619 (lc, TYPE_METHODS (class), name, arglist, &list, &all_list);
7620
7621 n = TREE_VEC_LENGTH (basetype_vec);
7622 for (i = 0; i < n; i++)
b67d701b 7623 {
de0b553f
APB
7624 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
7625 tree rlist;
7626
7627 /* Skip java.lang.Object (we'll search it once later.) */
7628 if (t == object_type_node)
7629 continue;
7630
7631 search_not_done++;
7632 rlist = find_applicable_accessible_methods_list (lc, t, name,
7633 arglist);
1982388a 7634 all_list = chainon (rlist, (list ? list : all_list));
de0b553f
APB
7635 search_not_done--;
7636 }
7637
7638 /* We're done. Reset the searched interfaces list and finally search
7639 java.lang.Object */
7640 if (!search_not_done)
7641 {
7642 searched_interfaces = NULL_TREE;
7643 search_applicable_methods_list (lc, TYPE_METHODS (object_type_node),
7644 name, arglist, &list, &all_list);
e04a16fb 7645 }
e04a16fb 7646 }
1982388a
APB
7647 /* Search classes */
7648 else
7649 while (class != NULL_TREE)
7650 {
7651 search_applicable_methods_list
7652 (lc, TYPE_METHODS (class), name, arglist, &list, &all_list);
7653 class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
7654 }
7655
b67d701b
PB
7656 /* Either return the list obtained or all selected (but
7657 inaccessible) methods for better error report. */
7658 return (!list ? all_list : list);
7659}
e04a16fb 7660
1982388a
APB
7661/* Effectively search for the approriate method in method */
7662
7663static void
7664search_applicable_methods_list(lc, method, name, arglist, list, all_list)
7665 int lc;
7666 tree method, name, arglist;
7667 tree *list, *all_list;
7668{
7669 for (; method; method = TREE_CHAIN (method))
7670 {
7671 /* When dealing with constructor, stop here, otherwise search
7672 other classes */
7673 if (lc && !DECL_CONSTRUCTOR_P (method))
7674 continue;
7675 else if (!lc && (DECL_CONSTRUCTOR_P (method)
7676 || (GET_METHOD_NAME (method) != name)))
7677 continue;
7678
7679 if (argument_types_convertible (method, arglist))
7680 {
7681 /* Retain accessible methods only */
7682 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
7683 method, 0))
7684 *list = tree_cons (NULL_TREE, method, *list);
7685 else
7686 /* Also retain all selected method here */
7687 *all_list = tree_cons (NULL_TREE, method, *list);
7688 }
7689 }
7690}
7691
b67d701b
PB
7692/* 15.11.2.2 Choose the Most Specific Method */
7693
7694static tree
7695find_most_specific_methods_list (list)
7696 tree list;
7697{
7698 int max = 0;
7699 tree current, new_list = NULL_TREE;
7700 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 7701 {
b67d701b
PB
7702 tree method;
7703 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
7704
7705 for (method = list; method; method = TREE_CHAIN (method))
7706 {
7707 /* Don't test a method against itself */
7708 if (method == current)
7709 continue;
7710
7711 /* Compare arguments and location where method where declared */
7712 if (argument_types_convertible (TREE_VALUE (method),
7713 TREE_VALUE (current))
7714 && valid_method_invocation_conversion_p
7715 (DECL_CONTEXT (TREE_VALUE (method)),
7716 DECL_CONTEXT (TREE_VALUE (current))))
7717 {
7718 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
7719 max = (v > max ? v : max);
7720 }
7721 }
e04a16fb
AG
7722 }
7723
b67d701b
PB
7724 /* Review the list and select the maximally specific methods */
7725 for (current = list; current; current = TREE_CHAIN (current))
7726 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7727 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7728
7729 /* If we can't find one, lower expectations and try to gather multiple
7730 maximally specific methods */
7731 while (!new_list)
7732 {
7733 while (--max > 0)
7734 {
7735 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7736 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7737 }
7738 return new_list;
7739 }
7740
7741 return new_list;
e04a16fb
AG
7742}
7743
b67d701b
PB
7744/* Make sure that the type of each M2_OR_ARGLIST arguments can be
7745 converted by method invocation conversion (5.3) to the type of the
7746 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
7747 to change less often than M1. */
e04a16fb 7748
b67d701b
PB
7749static int
7750argument_types_convertible (m1, m2_or_arglist)
7751 tree m1, m2_or_arglist;
e04a16fb 7752{
b67d701b
PB
7753 static tree m2_arg_value = NULL_TREE;
7754 static tree m2_arg_cache = NULL_TREE;
e04a16fb 7755
b67d701b 7756 register tree m1_arg, m2_arg;
e04a16fb 7757
b67d701b
PB
7758 m1_arg = TYPE_ARG_TYPES (TREE_TYPE (m1));
7759 if (!METHOD_STATIC (m1))
7760 m1_arg = TREE_CHAIN (m1_arg);
e04a16fb 7761
b67d701b
PB
7762 if (m2_arg_value == m2_or_arglist)
7763 m2_arg = m2_arg_cache;
7764 else
7765 {
7766 /* M2_OR_ARGLIST can be a function DECL or a raw list of
7767 argument types */
7768 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
7769 {
7770 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
7771 if (!METHOD_STATIC (m2_or_arglist))
7772 m2_arg = TREE_CHAIN (m2_arg);
7773 }
7774 else
7775 m2_arg = m2_or_arglist;
e04a16fb 7776
b67d701b
PB
7777 m2_arg_value = m2_or_arglist;
7778 m2_arg_cache = m2_arg;
7779 }
e04a16fb 7780
de4c7b02 7781 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 7782 {
c877974e 7783 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
7784 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
7785 TREE_VALUE (m2_arg)))
7786 break;
7787 m1_arg = TREE_CHAIN (m1_arg);
7788 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 7789 }
de4c7b02 7790 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
7791}
7792
7793/* Qualification routines */
7794
7795static void
7796qualify_ambiguous_name (id)
7797 tree id;
7798{
cd531a2e
KG
7799 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
7800 saved_current_class;
d8fccff5 7801 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 7802 int code;
e04a16fb
AG
7803
7804 /* We first qualify the first element, then derive qualification of
7805 others based on the first one. If the first element is qualified
7806 by a resolution (field or type), this resolution is stored in the
7807 QUAL_RESOLUTION of the qual element being examined. We need to
7808 save the current_class since the use of SUPER might change the
7809 its value. */
7810 saved_current_class = current_class;
7811 qual = EXPR_WFL_QUALIFICATION (id);
7812 do {
7813
7814 /* Simple qualified expression feature a qual_wfl that is a
7815 WFL. Expression derived from a primary feature more complicated
7816 things like a CALL_EXPR. Expression from primary need to be
7817 worked out to extract the part on which the qualification will
7818 take place. */
7819 qual_wfl = QUAL_WFL (qual);
7820 switch (TREE_CODE (qual_wfl))
7821 {
7822 case CALL_EXPR:
7823 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7824 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
7825 {
7826 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
7827 qual_wfl = QUAL_WFL (qual);
7828 }
7829 break;
d8fccff5
APB
7830 case NEW_ARRAY_EXPR:
7831 qual = TREE_CHAIN (qual);
1a6d4fb7 7832 again = new_array_found = 1;
d8fccff5 7833 continue;
b67d701b 7834 case NEW_CLASS_EXPR:
e04a16fb 7835 case CONVERT_EXPR:
e04a16fb
AG
7836 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7837 break;
c583dd46
APB
7838 case ARRAY_REF:
7839 while (TREE_CODE (qual_wfl) == ARRAY_REF)
7840 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7841 break;
8576f094
APB
7842 case STRING_CST:
7843 qual = TREE_CHAIN (qual);
7844 qual_wfl = QUAL_WFL (qual);
7845 break;
0a2138e2
APB
7846 default:
7847 /* Fix for -Wall. Just break doing nothing */
7848 break;
e04a16fb 7849 }
8576f094 7850
e04a16fb
AG
7851 ptr_type = current_class;
7852 again = 0;
8576f094
APB
7853 code = TREE_CODE (qual_wfl);
7854
7855 /* Pos evaluation: non WFL leading expression nodes */
7856 if (code == CONVERT_EXPR
7857 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
7858 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
7859
7860 else if (code == ARRAY_REF &&
7861 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
7862 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
7863
7864 else if (code == CALL_EXPR &&
7865 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
7866 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
7867
7868 else if (code == STRING_CST || code == CONDITIONAL_EXPR)
7869 {
7870 qual = TREE_CHAIN (qual);
7871 qual_wfl = QUAL_WFL (qual);
7872 again = 1;
7873 }
7874 else
f441f671
APB
7875 {
7876 name = EXPR_WFL_NODE (qual_wfl);
7877 if (!name)
7878 {
7879 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
7880 again = 1;
7881 }
7882 }
7883
e04a16fb
AG
7884 /* If we have a THIS (from a primary), we set the context accordingly */
7885 if (name == this_identifier_node)
7886 {
7887 qual = TREE_CHAIN (qual);
7888 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
7889 if (TREE_CODE (qual_wfl) == CALL_EXPR)
7890 again = 1;
7891 else
7892 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
7893 this_found = 1;
7894 }
7895 /* If we have a SUPER, we set the context accordingly */
7896 if (name == super_identifier_node)
7897 {
7898 current_class = CLASSTYPE_SUPER (ptr_type);
7899 /* Check that there is such a thing as a super class. If not,
7900 return. The error will be caught later on, during the
7901 resolution */
7902 if (!current_class)
7903 {
7904 current_class = saved_current_class;
7905 return;
7906 }
7907 qual = TREE_CHAIN (qual);
7908 /* Do one more interation to set things up */
7909 super_found = again = 1;
7910 }
7911 } while (again);
7912
7913 /* If name appears within the scope of a location variable
7914 declaration or parameter declaration, then it is an expression
7915 name. We don't carry this test out if we're in the context of the
7916 use of SUPER or THIS */
1a6d4fb7
APB
7917 if (!this_found && !super_found &&
7918 TREE_CODE (name) != STRING_CST && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
7919 {
7920 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7921 QUAL_RESOLUTION (qual) = decl;
7922 }
7923
7924 /* If within the class/interface NAME was found to be used there
7925 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
7926 expression name. If we saw a NEW_ARRAY_EXPR before and want to
7927 address length, it is OK. */
7928 else if ((decl = lookup_field_wrapper (ptr_type, name))
7929 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
7930 {
7931 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 7932 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
7933 }
7934
1a6d4fb7 7935 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
7936 - NAME is a class/interface declared within the compilation
7937 unit containing NAME,
7938 - NAME is imported via a single-type-import declaration,
7939 - NAME is declared in an another compilation unit of the package
7940 of the compilation unit containing NAME,
7941 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
7942 of the compilation unit containing NAME.
7943 - NAME is actually a STRING_CST. */
7944 else if (TREE_CODE (name) == STRING_CST ||
7945 (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
7946 {
7947 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
7948 QUAL_RESOLUTION (qual) = decl;
7949 }
7950
7951 /* Method call are expression name */
9bbc7d9f 7952 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
7953 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
7954 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
7955 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7956
7957 /* Check here that NAME isn't declared by more than one
7958 type-import-on-demand declaration of the compilation unit
7959 containing NAME. FIXME */
7960
7961 /* Otherwise, NAME is reclassified as a package name */
7962 else
7963 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
7964
7965 /* Propagate the qualification accross other components of the
7966 qualified name */
7967 for (qual = TREE_CHAIN (qual); qual;
7968 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
7969 {
7970 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7971 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
7972 else
7973 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
7974 }
7975
7976 /* Store the global qualification for the ambiguous part of ID back
7977 into ID fields */
7978 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
7979 RESOLVE_EXPRESSION_NAME_P (id) = 1;
7980 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
7981 RESOLVE_TYPE_NAME_P (id) = 1;
7982 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7983 RESOLVE_PACKAGE_NAME_P (id) = 1;
7984
7985 /* Restore the current class */
7986 current_class = saved_current_class;
7987}
7988
7989static int
7990breakdown_qualified (left, right, source)
7991 tree *left, *right, source;
7992{
7993 char *p = IDENTIFIER_POINTER (source), *base;
7994 int l = IDENTIFIER_LENGTH (source);
7995
7996 /* Breakdown NAME into REMAINDER . IDENTIFIER */
7997 base = p;
7998 p += (l-1);
7999 while (*p != '.' && p != base)
8000 p--;
8001
8002 /* We didn't find a '.'. Return an error */
8003 if (p == base)
8004 return 1;
8005
8006 *p = '\0';
8007 if (right)
8008 *right = get_identifier (p+1);
8009 *left = get_identifier (IDENTIFIER_POINTER (source));
8010 *p = '.';
8011
8012 return 0;
8013}
8014
e04a16fb 8015/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
8016 local variable decls if present.
8017 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
8018
8019static tree
8020java_complete_tree (node)
8021 tree node;
5b09b33e
PB
8022{
8023 node = java_complete_lhs (node);
8024 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7f10c2e2
APB
8025 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE
8026 && !flag_emit_xref)
5b09b33e
PB
8027 {
8028 tree value = DECL_INITIAL (node);
8029 DECL_INITIAL (node) = NULL_TREE;
8030 value = fold_constant_for_init (value, node);
8031 DECL_INITIAL (node) = value;
8032 if (value != NULL_TREE)
8033 return value;
8034 }
8035 return node;
8036}
8037
2aa11e97
APB
8038static tree
8039java_stabilize_reference (node)
8040 tree node;
8041{
8042 if (TREE_CODE (node) == COMPOUND_EXPR)
8043 {
8044 tree op0 = TREE_OPERAND (node, 0);
8045 tree op1 = TREE_OPERAND (node, 1);
642f15d1 8046 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
8047 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
8048 return node;
8049 }
5cbdba64 8050 return stabilize_reference (node);
2aa11e97
APB
8051}
8052
5b09b33e
PB
8053/* Patch tree nodes in a function body. When a BLOCK is found, push
8054 local variable decls if present.
8055 Same as java_complete_tree, but does not resolve static finals to values. */
8056
8057static tree
8058java_complete_lhs (node)
8059 tree node;
e04a16fb 8060{
22eed1e6 8061 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 8062 int flag;
e04a16fb
AG
8063
8064 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 8065 worked out. */
e04a16fb
AG
8066 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
8067 return node;
8068
8069 /* The switch block implements cases processing container nodes
8070 first. Contained nodes are always written back. Leaves come
8071 next and return a value. */
8072 switch (TREE_CODE (node))
8073 {
8074 case BLOCK:
8075
8076 /* 1- Block section.
8077 Set the local values on decl names so we can identify them
8078 faster when they're referenced. At that stage, identifiers
8079 are legal so we don't check for declaration errors. */
8080 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
8081 {
8082 DECL_CONTEXT (cn) = current_function_decl;
8083 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 8084 }
15fdcfe9
PB
8085 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
8086 CAN_COMPLETE_NORMALLY (node) = 1;
8087 else
e04a16fb 8088 {
15fdcfe9
PB
8089 tree stmt = BLOCK_EXPR_BODY (node);
8090 tree *ptr;
8091 int error_seen = 0;
8092 if (TREE_CODE (stmt) == COMPOUND_EXPR)
8093 {
c877974e
APB
8094 /* Re-order from (((A; B); C); ...; Z) to
8095 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
8096 This makes it easier to scan the statements left-to-right
8097 without using recursion (which might overflow the stack
8098 if the block has many statements. */
8099 for (;;)
8100 {
8101 tree left = TREE_OPERAND (stmt, 0);
8102 if (TREE_CODE (left) != COMPOUND_EXPR)
8103 break;
8104 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
8105 TREE_OPERAND (left, 1) = stmt;
8106 stmt = left;
8107 }
8108 BLOCK_EXPR_BODY (node) = stmt;
8109 }
8110
c877974e
APB
8111 /* Now do the actual complete, without deep recursion for
8112 long blocks. */
15fdcfe9 8113 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
8114 while (TREE_CODE (*ptr) == COMPOUND_EXPR
8115 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
8116 {
8117 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
8118 tree *next = &TREE_OPERAND (*ptr, 1);
8119 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
8120 if (cur == empty_stmt_node)
8121 {
8122 /* Optimization; makes it easier to detect empty bodies.
8123 Most useful for <clinit> with all-constant initializer. */
8124 *ptr = *next;
8125 continue;
8126 }
15fdcfe9
PB
8127 if (TREE_CODE (cur) == ERROR_MARK)
8128 error_seen++;
8129 else if (! CAN_COMPLETE_NORMALLY (cur))
8130 {
8131 wfl_op2 = *next;
8132 for (;;)
8133 {
8134 if (TREE_CODE (wfl_op2) == BLOCK)
8135 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
8136 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
8137 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
8138 else
8139 break;
8140 }
8141 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 8142 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 8143 unreachable_stmt_error (*ptr);
15fdcfe9
PB
8144 }
8145 ptr = next;
8146 }
8147 *ptr = java_complete_tree (*ptr);
8148
8149 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 8150 return error_mark_node;
15fdcfe9 8151 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
8152 }
8153 /* Turn local bindings to null */
8154 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
8155 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
8156
8157 TREE_TYPE (node) = void_type_node;
8158 break;
8159
8160 /* 2- They are expressions but ultimately deal with statements */
b67d701b 8161
b9f7e36c
APB
8162 case THROW_EXPR:
8163 wfl_op1 = TREE_OPERAND (node, 0);
8164 COMPLETE_CHECK_OP_0 (node);
15fdcfe9 8165 /* CAN_COMPLETE_NORMALLY (node) = 0; */
b9f7e36c
APB
8166 return patch_throw_statement (node, wfl_op1);
8167
8168 case SYNCHRONIZED_EXPR:
8169 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
8170 return patch_synchronized_statement (node, wfl_op1);
8171
b67d701b
PB
8172 case TRY_EXPR:
8173 return patch_try_statement (node);
8174
a7d8d81f
PB
8175 case TRY_FINALLY_EXPR:
8176 COMPLETE_CHECK_OP_0 (node);
8177 COMPLETE_CHECK_OP_1 (node);
8178 CAN_COMPLETE_NORMALLY (node)
8179 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
8180 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
8181 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
8182 return node;
8183
5a005d9e
PB
8184 case CLEANUP_POINT_EXPR:
8185 COMPLETE_CHECK_OP_0 (node);
8186 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
8187 CAN_COMPLETE_NORMALLY (node) =
8188 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
8189 return node;
8190
8191 case WITH_CLEANUP_EXPR:
8192 COMPLETE_CHECK_OP_0 (node);
8193 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
8194 CAN_COMPLETE_NORMALLY (node) =
8195 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
8196 TREE_TYPE (node) = void_type_node;
8197 return node;
8198
e04a16fb
AG
8199 case LABELED_BLOCK_EXPR:
8200 PUSH_LABELED_BLOCK (node);
8201 if (LABELED_BLOCK_BODY (node))
8202 COMPLETE_CHECK_OP_1 (node);
8203 TREE_TYPE (node) = void_type_node;
8204 POP_LABELED_BLOCK ();
1fb89a4d
APB
8205
8206 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
8207 {
8208 LABELED_BLOCK_BODY (node) = NULL_TREE;
8209 CAN_COMPLETE_NORMALLY (node) = 1;
8210 }
1fb89a4d 8211 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 8212 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
8213 return node;
8214
8215 case EXIT_BLOCK_EXPR:
8216 /* We don't complete operand 1, because it's the return value of
8217 the EXIT_BLOCK_EXPR which doesn't exist it Java */
8218 return patch_bc_statement (node);
8219
15fdcfe9
PB
8220 case CASE_EXPR:
8221 cn = java_complete_tree (TREE_OPERAND (node, 0));
8222 if (cn == error_mark_node)
8223 return cn;
8224
8576f094
APB
8225 /* First, the case expression must be constant. Values of final
8226 fields are accepted. */
15fdcfe9 8227 cn = fold (cn);
8576f094
APB
8228 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
8229 && JDECL_P (TREE_OPERAND (cn, 1))
8230 && FIELD_FINAL (TREE_OPERAND (cn, 1))
8231 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
8232 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
8233 TREE_OPERAND (cn, 1));
15fdcfe9 8234
ce6e9147 8235 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
8236 {
8237 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8238 parse_error_context (node, "Constant expression required");
8239 return error_mark_node;
8240 }
8241
8242 nn = ctxp->current_loop;
8243
8244 /* It must be assignable to the type of the switch expression. */
c877974e
APB
8245 if (!try_builtin_assignconv (NULL_TREE,
8246 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
8247 {
8248 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8249 parse_error_context
8250 (wfl_operator,
8251 "Incompatible type for case. Can't convert `%s' to `int'",
8252 lang_printable_name (TREE_TYPE (cn), 0));
8253 return error_mark_node;
8254 }
8255
8256 cn = fold (convert (int_type_node, cn));
8257
8258 /* Multiple instance of a case label bearing the same
8259 value is checked during code generation. The case
8260 expression is allright so far. */
8261 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 8262 TREE_TYPE (node) = void_type_node;
15fdcfe9 8263 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 8264 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
8265 break;
8266
8267 case DEFAULT_EXPR:
8268 nn = ctxp->current_loop;
8269 /* Only one default label is allowed per switch statement */
8270 if (SWITCH_HAS_DEFAULT (nn))
8271 {
8272 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8273 parse_error_context (wfl_operator,
8274 "Duplicate case label: `default'");
8275 return error_mark_node;
8276 }
8277 else
8278 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 8279 TREE_TYPE (node) = void_type_node;
10100cc7 8280 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
8281 CAN_COMPLETE_NORMALLY (node) = 1;
8282 break;
8283
b67d701b 8284 case SWITCH_EXPR:
e04a16fb
AG
8285 case LOOP_EXPR:
8286 PUSH_LOOP (node);
8287 /* Check whether the loop was enclosed in a labeled
8288 statement. If not, create one, insert the loop in it and
8289 return the node */
8290 nn = patch_loop_statement (node);
b67d701b 8291
e04a16fb 8292 /* Anyways, walk the body of the loop */
b67d701b
PB
8293 if (TREE_CODE (node) == LOOP_EXPR)
8294 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
8295 /* Switch statement: walk the switch expression and the cases */
8296 else
8297 node = patch_switch_statement (node);
8298
e04a16fb 8299 if (TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
8300 nn = error_mark_node;
8301 else
15fdcfe9 8302 {
b635eb2f
PB
8303 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
8304 /* If we returned something different, that's because we
8305 inserted a label. Pop the label too. */
8306 if (nn != node)
8307 {
8308 if (CAN_COMPLETE_NORMALLY (node))
8309 CAN_COMPLETE_NORMALLY (nn) = 1;
8310 POP_LABELED_BLOCK ();
8311 }
15fdcfe9 8312 }
e04a16fb
AG
8313 POP_LOOP ();
8314 return nn;
8315
8316 case EXIT_EXPR:
8317 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
8318 return patch_exit_expr (node);
8319
8320 case COND_EXPR:
8321 /* Condition */
8322 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
8323 if (TREE_OPERAND (node, 0) == error_mark_node)
8324 return error_mark_node;
8325 /* then-else branches */
8326 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
8327 if (TREE_OPERAND (node, 1) == error_mark_node)
8328 return error_mark_node;
8329 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
8330 if (TREE_OPERAND (node, 2) == error_mark_node)
8331 return error_mark_node;
8332 return patch_if_else_statement (node);
8333 break;
8334
22eed1e6
APB
8335 case CONDITIONAL_EXPR:
8336 /* Condition */
8337 wfl_op1 = TREE_OPERAND (node, 0);
8338 COMPLETE_CHECK_OP_0 (node);
8339 wfl_op2 = TREE_OPERAND (node, 1);
8340 COMPLETE_CHECK_OP_1 (node);
8341 wfl_op3 = TREE_OPERAND (node, 2);
8342 COMPLETE_CHECK_OP_2 (node);
8343 return patch_conditional_expr (node, wfl_op1, wfl_op2);
8344
e04a16fb
AG
8345 /* 3- Expression section */
8346 case COMPOUND_EXPR:
15fdcfe9 8347 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
8348 TREE_OPERAND (node, 0) = nn =
8349 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
8350 if (wfl_op2 == empty_stmt_node)
8351 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
8352 else
15fdcfe9 8353 {
dc0b3eff 8354 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 8355 {
dc0b3eff
PB
8356 /* An unreachable condition in a do-while statement
8357 is *not* (technically) an unreachable statement. */
8358 nn = wfl_op2;
8359 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
8360 nn = EXPR_WFL_NODE (nn);
8361 if (TREE_CODE (nn) != EXIT_EXPR)
8362 {
8363 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
8364 parse_error_context (wfl_operator, "Unreachable statement");
8365 }
bccaf73a 8366 }
dc0b3eff
PB
8367 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
8368 if (TREE_OPERAND (node, 1) == error_mark_node)
8369 return error_mark_node;
8370 CAN_COMPLETE_NORMALLY (node)
8371 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 8372 }
e04a16fb
AG
8373 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
8374 break;
8375
8376 case RETURN_EXPR:
15fdcfe9 8377 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
8378 return patch_return (node);
8379
8380 case EXPR_WITH_FILE_LOCATION:
8381 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
8382 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 8383 {
5423609c 8384 tree wfl = node;
15fdcfe9 8385 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
8386 if (node == error_mark_node)
8387 return node;
5423609c
APB
8388 /* Keep line number information somewhere were it doesn't
8389 disrupt the completion process. */
2c56429a 8390 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
8391 {
8392 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
8393 TREE_OPERAND (node, 1) = wfl;
8394 }
15fdcfe9
PB
8395 CAN_COMPLETE_NORMALLY (node) = 1;
8396 }
e04a16fb
AG
8397 else
8398 {
5b09b33e
PB
8399 tree body;
8400 int save_lineno = lineno;
8401 lineno = EXPR_WFL_LINENO (node);
8402 body = java_complete_tree (EXPR_WFL_NODE (node));
8403 lineno = save_lineno;
15fdcfe9 8404 EXPR_WFL_NODE (node) = body;
dc0b3eff 8405 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 8406 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
8407 if (body == empty_stmt_node)
8408 {
8409 /* Optimization; makes it easier to detect empty bodies. */
8410 return body;
8411 }
dc0b3eff 8412 if (body == error_mark_node)
e04a16fb
AG
8413 {
8414 /* Its important for the evaluation of assignment that
8415 this mark on the TREE_TYPE is propagated. */
8416 TREE_TYPE (node) = error_mark_node;
8417 return error_mark_node;
8418 }
8419 else
8420 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 8421
e04a16fb
AG
8422 }
8423 break;
8424
b67d701b 8425 case NEW_ARRAY_EXPR:
e04a16fb
AG
8426 /* Patch all the dimensions */
8427 flag = 0;
8428 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
8429 {
8430 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
8431 tree dim = java_complete_tree (TREE_VALUE (cn));
8432 if (dim == error_mark_node)
8433 {
8434 flag = 1;
8435 continue;
8436 }
8437 else
8438 {
b9f7e36c 8439 TREE_VALUE (cn) = dim;
e04a16fb
AG
8440 /* Setup the location of the current dimension, for
8441 later error report. */
8442 TREE_PURPOSE (cn) =
8443 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
8444 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
8445 }
8446 }
8447 /* They complete the array creation expression, if no errors
8448 were found. */
15fdcfe9 8449 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
8450 return (flag ? error_mark_node
8451 : force_evaluation_order (patch_newarray (node)));
e04a16fb 8452
b67d701b 8453 case NEW_CLASS_EXPR:
e04a16fb 8454 case CALL_EXPR:
b67d701b 8455 /* Complete function's argument(s) first */
e04a16fb
AG
8456 if (complete_function_arguments (node))
8457 return error_mark_node;
8458 else
b9f7e36c 8459 {
22eed1e6
APB
8460 tree decl, wfl = TREE_OPERAND (node, 0);
8461 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
8462
c877974e 8463 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 8464 NULL_TREE, 0, &decl);
c877974e
APB
8465 if (node == error_mark_node)
8466 return error_mark_node;
8467
8468 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
8469 /* If we call this(...), register signature and positions */
8470 if (in_this)
8471 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
8472 tree_cons (wfl, decl,
8473 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 8474 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 8475 return force_evaluation_order (node);
b9f7e36c 8476 }
e04a16fb
AG
8477
8478 case MODIFY_EXPR:
8479 /* Save potential wfls */
8480 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7
PB
8481 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
8482 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
8483 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
8484 && DECL_INITIAL (nn) != NULL_TREE)
8485 {
8486 tree value = fold_constant_for_init (nn, nn);
8487 if (value != NULL_TREE)
8488 {
8489 tree type = TREE_TYPE (value);
8490 if (JPRIMITIVE_TYPE_P (type) || type == string_ptr_type_node)
8491 return empty_stmt_node;
8492 }
8493 DECL_INITIAL (nn) = NULL_TREE;
8494 }
e04a16fb 8495 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 8496
e04a16fb
AG
8497 if (TREE_OPERAND (node, 0) == error_mark_node)
8498 return error_mark_node;
8499
5cbdba64
APB
8500 flag = COMPOUND_ASSIGN_P (wfl_op2);
8501 if (flag)
e04a16fb 8502 {
2aa11e97 8503 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
8504
8505 /* Hand stablize the lhs on both places */
e04a16fb 8506 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
8507 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
8508 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 8509
5cbdba64 8510 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
8511 /* Now complete the RHS. We write it back later on. */
8512 nn = java_complete_tree (TREE_OPERAND (node, 1));
8513
642f15d1
APB
8514 if ((cn = patch_string (nn)))
8515 nn = cn;
8516
2aa11e97
APB
8517 /* The last part of the rewrite for E1 op= E2 is to have
8518 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
8519 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
8520 TREE_TYPE (lvalue), nn));
5cbdba64
APB
8521
8522 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
8523 }
8524
f8976021
APB
8525 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
8526 function to complete this RHS */
2aa11e97 8527 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT)
fdec99c6 8528 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 8529 TREE_OPERAND (node, 1));
2aa11e97 8530 /* Otherwise we simply complete the RHS */
f8976021
APB
8531 else
8532 nn = java_complete_tree (TREE_OPERAND (node, 1));
8533
e04a16fb 8534 if (nn == error_mark_node)
c0d87ff6 8535 return error_mark_node;
2aa11e97
APB
8536
8537 /* Write back the RHS as we evaluated it. */
e04a16fb 8538 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
8539
8540 /* In case we're handling = with a String as a RHS, we need to
8541 produce a String out of the RHS (it might still be a
8542 STRING_CST or a StringBuffer at this stage */
8543 if ((nn = patch_string (TREE_OPERAND (node, 1))))
8544 TREE_OPERAND (node, 1) = nn;
15fdcfe9 8545 node = patch_assignment (node, wfl_op1, wfl_op2);
5cbdba64
APB
8546 /* Reorganize the tree if necessary. */
8547 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
8548 || JSTRING_P (TREE_TYPE (node))))
8549 node = java_refold (node);
15fdcfe9
PB
8550 CAN_COMPLETE_NORMALLY (node) = 1;
8551 return node;
e04a16fb
AG
8552
8553 case MULT_EXPR:
8554 case PLUS_EXPR:
8555 case MINUS_EXPR:
8556 case LSHIFT_EXPR:
8557 case RSHIFT_EXPR:
8558 case URSHIFT_EXPR:
8559 case BIT_AND_EXPR:
8560 case BIT_XOR_EXPR:
8561 case BIT_IOR_EXPR:
8562 case TRUNC_MOD_EXPR:
8563 case RDIV_EXPR:
8564 case TRUTH_ANDIF_EXPR:
8565 case TRUTH_ORIF_EXPR:
8566 case EQ_EXPR:
8567 case NE_EXPR:
8568 case GT_EXPR:
8569 case GE_EXPR:
8570 case LT_EXPR:
8571 case LE_EXPR:
8572 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
8573 knows how to handle those cases. */
8574 wfl_op1 = TREE_OPERAND (node, 0);
8575 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 8576
15fdcfe9 8577 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
8578 /* Don't complete string nodes if dealing with the PLUS operand. */
8579 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
8580 {
8581 nn = java_complete_tree (wfl_op1);
8582 if (nn == error_mark_node)
8583 return error_mark_node;
8584 if ((cn = patch_string (nn)))
8585 nn = cn;
8586 TREE_OPERAND (node, 0) = nn;
8587 }
b67d701b 8588 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
8589 {
8590 nn = java_complete_tree (wfl_op2);
8591 if (nn == error_mark_node)
8592 return error_mark_node;
8593 if ((cn = patch_string (nn)))
8594 nn = cn;
8595 TREE_OPERAND (node, 1) = nn;
8596 }
dc0b3eff 8597 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 8598
5e942c50
APB
8599 case INSTANCEOF_EXPR:
8600 wfl_op1 = TREE_OPERAND (node, 0);
8601 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
8602 if (flag_emit_xref)
8603 {
8604 TREE_TYPE (node) = boolean_type_node;
8605 return node;
8606 }
5e942c50
APB
8607 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
8608
b67d701b 8609 case UNARY_PLUS_EXPR:
e04a16fb
AG
8610 case NEGATE_EXPR:
8611 case TRUTH_NOT_EXPR:
8612 case BIT_NOT_EXPR:
8613 case PREDECREMENT_EXPR:
8614 case PREINCREMENT_EXPR:
8615 case POSTDECREMENT_EXPR:
8616 case POSTINCREMENT_EXPR:
8617 case CONVERT_EXPR:
8618 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
8619 how to handle those cases. */
8620 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 8621 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
8622 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
8623 if (TREE_OPERAND (node, 0) == error_mark_node)
8624 return error_mark_node;
4a5f66c3
APB
8625 node = patch_unaryop (node, wfl_op1);
8626 CAN_COMPLETE_NORMALLY (node) = 1;
8627 break;
e04a16fb
AG
8628
8629 case ARRAY_REF:
8630 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
8631 how to handle those cases. */
8632 wfl_op1 = TREE_OPERAND (node, 0);
8633 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
8634 if (TREE_OPERAND (node, 0) == error_mark_node)
8635 return error_mark_node;
7f1d4866 8636 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 8637 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
8638 /* The same applies to wfl_op2 */
8639 wfl_op2 = TREE_OPERAND (node, 1);
8640 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
8641 if (TREE_OPERAND (node, 1) == error_mark_node)
8642 return error_mark_node;
7f1d4866 8643 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 8644 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 8645 return patch_array_ref (node);
e04a16fb 8646
63a212ed
PB
8647 case RECORD_TYPE:
8648 return node;;
8649
8650 case COMPONENT_REF:
8651 /* The first step in the re-write of qualified name handling. FIXME.
8652 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 8653 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
8654 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
8655 {
8656 tree name = TREE_OPERAND (node, 1);
8657 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
8658 if (field == NULL_TREE)
8659 {
8660 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
8661 return error_mark_node;
8662 }
8663 if (! FIELD_STATIC (field))
8664 {
8665 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
8666 return error_mark_node;
8667 }
8668 return field;
8669 }
8670 else
8671 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 8672 break;
9bbc7d9f 8673
b67d701b 8674 case THIS_EXPR:
e04a16fb
AG
8675 /* Can't use THIS in a static environment */
8676 if (!current_this)
8677 {
8678 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8679 parse_error_context (wfl_operator, "Keyword `this' used outside "
8680 "allowed context");
8681 TREE_TYPE (node) = error_mark_node;
8682 return error_mark_node;
8683 }
22eed1e6
APB
8684 if (ctxp->explicit_constructor_p)
8685 {
8686 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8687 parse_error_context
8688 (wfl_operator, "Can't reference `this' or `super' before the "
8689 "superclass constructor has been called");
8690 TREE_TYPE (node) = error_mark_node;
8691 return error_mark_node;
8692 }
e04a16fb
AG
8693 return current_this;
8694
e04a16fb 8695 default:
15fdcfe9 8696 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
8697 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
8698 and it's time to turn it into the appropriate String object
8699 */
8700 if ((node = patch_string (node)))
8701 return node;
e04a16fb
AG
8702 fatal ("No case for tree code `%s' - java_complete_tree\n",
8703 tree_code_name [TREE_CODE (node)]);
8704 }
8705 return node;
8706}
8707
8708/* Complete function call's argument. Return a non zero value is an
8709 error was found. */
8710
8711static int
8712complete_function_arguments (node)
8713 tree node;
8714{
8715 int flag = 0;
8716 tree cn;
8717
22eed1e6 8718 ctxp->explicit_constructor_p += (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
8719 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
8720 {
b67d701b 8721 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb
AG
8722 parm = java_complete_tree (wfl);
8723 if (parm == error_mark_node)
8724 {
8725 flag = 1;
8726 continue;
8727 }
b67d701b
PB
8728 /* If have a string literal that we haven't transformed yet or a
8729 crafted string buffer, as a result of use of the the String
8730 `+' operator. Build `parm.toString()' and expand it. */
8731 if ((temp = patch_string (parm)))
b9f7e36c 8732 parm = temp;
5e942c50
APB
8733 /* Inline PRIMTYPE.TYPE read access */
8734 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 8735
5e942c50 8736 TREE_VALUE (cn) = parm;
e04a16fb 8737 }
22eed1e6 8738 ctxp->explicit_constructor_p -= (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
8739 return flag;
8740}
8741
8742/* Sometimes (for loops and variable initialized during their
8743 declaration), we want to wrap a statement around a WFL and turn it
8744 debugable. */
8745
8746static tree
8747build_debugable_stmt (location, stmt)
8748 int location;
8749 tree stmt;
8750{
8751 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
8752 {
8753 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
8754 EXPR_WFL_LINECOL (stmt) = location;
8755 }
8756 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
8757 return stmt;
8758}
8759
8760static tree
8761build_expr_block (body, decls)
8762 tree body, decls;
8763{
8764 tree node = make_node (BLOCK);
8765 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 8766 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
8767 if (body)
8768 TREE_TYPE (node) = TREE_TYPE (body);
8769 TREE_SIDE_EFFECTS (node) = 1;
8770 return node;
8771}
8772
b67d701b
PB
8773/* Create a new function block and link it approriately to current
8774 function block chain */
e04a16fb
AG
8775
8776static tree
8777enter_block ()
8778{
b67d701b
PB
8779 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
8780}
8781
8782/* Link block B supercontext to the previous block. The current
8783 function DECL is used as supercontext when enter_a_block is called
8784 for the first time for a given function. The current function body
8785 (DECL_FUNCTION_BODY) is set to be block B. */
8786
8787static tree
8788enter_a_block (b)
8789 tree b;
8790{
e04a16fb
AG
8791 tree fndecl = current_function_decl;
8792
f099f336
APB
8793 if (!fndecl) {
8794 BLOCK_SUPERCONTEXT (b) = current_static_block;
8795 current_static_block = b;
8796 }
8797
8798 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
8799 {
8800 BLOCK_SUPERCONTEXT (b) = fndecl;
8801 DECL_FUNCTION_BODY (fndecl) = b;
8802 }
8803 else
8804 {
8805 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
8806 DECL_FUNCTION_BODY (fndecl) = b;
8807 }
8808 return b;
8809}
8810
8811/* Exit a block by changing the current function body
8812 (DECL_FUNCTION_BODY) to the current block super context, only if
8813 the block being exited isn't the method's top level one. */
8814
8815static tree
8816exit_block ()
8817{
f099f336
APB
8818 tree b;
8819 if (current_function_decl)
8820 {
8821 b = DECL_FUNCTION_BODY (current_function_decl);
8822 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
8823 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
8824 }
8825 else
8826 {
8827 b = current_static_block;
e04a16fb 8828
f099f336
APB
8829 if (BLOCK_SUPERCONTEXT (b))
8830 current_static_block = BLOCK_SUPERCONTEXT (b);
8831 }
e04a16fb
AG
8832 return b;
8833}
8834
8835/* Lookup for NAME in the nested function's blocks, all the way up to
8836 the current toplevel one. It complies with Java's local variable
8837 scoping rules. */
8838
8839static tree
8840lookup_name_in_blocks (name)
8841 tree name;
8842{
f099f336 8843 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
8844
8845 while (b != current_function_decl)
8846 {
8847 tree current;
8848
8849 /* Paranoid sanity check. To be removed */
8850 if (TREE_CODE (b) != BLOCK)
8851 fatal ("non block expr function body - lookup_name_in_blocks");
8852
8853 for (current = BLOCK_EXPR_DECLS (b); current;
8854 current = TREE_CHAIN (current))
8855 if (DECL_NAME (current) == name)
8856 return current;
8857 b = BLOCK_SUPERCONTEXT (b);
8858 }
8859 return NULL_TREE;
8860}
8861
8862static void
8863maybe_absorb_scoping_blocks ()
8864{
f099f336 8865 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
8866 {
8867 tree b = exit_block ();
8868 java_method_add_stmt (current_function_decl, b);
8869 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
8870 }
8871}
8872
8873\f
8874/* This section of the source is reserved to build_* functions that
8875 are building incomplete tree nodes and the patch_* functions that
8876 are completing them. */
8877
9bbc7d9f 8878/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
8879 we're currently dealing with the class java.lang.Object. */
8880
8881static tree
8882build_super_invocation ()
8883{
8884 if (current_class == object_type_node)
9bbc7d9f 8885 return empty_stmt_node;
22eed1e6
APB
8886 else
8887 {
9ee9b555 8888 tree super_wfl = build_wfl_node (super_identifier_node);
22eed1e6
APB
8889 return build_method_invocation (super_wfl, NULL_TREE);
8890 }
8891}
8892
8893/* Build a SUPER/THIS qualified method invocation. */
8894
8895static tree
8896build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
8897 int use_this;
8898 tree name, args;
8899 int lloc, rloc;
8900
8901{
8902 tree invok;
8903 tree wfl =
9ee9b555 8904 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
8905 EXPR_WFL_LINECOL (wfl) = lloc;
8906 invok = build_method_invocation (name, args);
8907 return make_qualified_primary (wfl, invok, rloc);
8908}
8909
b67d701b 8910/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
8911
8912static tree
8913build_method_invocation (name, args)
8914 tree name;
8915 tree args;
8916{
8917 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
8918 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
8919 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8920 return call;
8921}
8922
8923/* Build an incomplete new xxx(...) node. */
8924
8925static tree
8926build_new_invocation (name, args)
8927 tree name, args;
8928{
8929 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
8930 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
8931 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8932 return call;
8933}
8934
8935/* Build an incomplete assignment expression. */
8936
8937static tree
8938build_assignment (op, op_location, lhs, rhs)
8939 int op, op_location;
8940 tree lhs, rhs;
8941{
8942 tree assignment;
8943 /* Build the corresponding binop if we deal with a Compound
8944 Assignment operator. Mark the binop sub-tree as part of a
8945 Compound Assignment expression */
8946 if (op != ASSIGN_TK)
8947 {
8948 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
8949 COMPOUND_ASSIGN_P (rhs) = 1;
8950 }
8951 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
8952 TREE_SIDE_EFFECTS (assignment) = 1;
8953 EXPR_WFL_LINECOL (assignment) = op_location;
8954 return assignment;
8955}
8956
8957/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
8958
15fdcfe9 8959char *
e04a16fb
AG
8960print_int_node (node)
8961 tree node;
8962{
8963 static char buffer [80];
8964 if (TREE_CONSTANT_OVERFLOW (node))
8965 sprintf (buffer, "<overflow>");
8966
8967 if (TREE_INT_CST_HIGH (node) == 0)
8968 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
8969 TREE_INT_CST_LOW (node));
8970 else if (TREE_INT_CST_HIGH (node) == -1
8971 && TREE_INT_CST_LOW (node) != 0)
8972 {
8973 buffer [0] = '-';
8974 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
8975 -TREE_INT_CST_LOW (node));
8976 }
8977 else
8978 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
8979 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
8980
8981 return buffer;
8982}
8983
7f1d4866
APB
8984/* Return 1 if an assignment to a FINAL is attempted in a non suitable
8985 context. */
5e942c50
APB
8986
8987static int
8988check_final_assignment (lvalue, wfl)
8989 tree lvalue, wfl;
8990{
7f1d4866
APB
8991 if (JDECL_P (lvalue)
8992 && FIELD_FINAL (lvalue) && !IS_CLINIT (current_function_decl))
5e942c50
APB
8993 {
8994 parse_error_context
8995 (wfl, "Can't assign a value to the final variable `%s'",
8996 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
8997 return 1;
8998 }
8999 return 0;
9000}
9001
9002/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
9003 read. This is needed to avoid circularities in the implementation
9004 of these fields in libjava. */
9005
9006static tree
9007maybe_build_primttype_type_ref (rhs, wfl)
9008 tree rhs, wfl;
9009{
9010 tree to_return = NULL_TREE;
9011 tree rhs_type = TREE_TYPE (rhs);
9012 if (TREE_CODE (rhs) == COMPOUND_EXPR)
9013 {
9014 tree n = TREE_OPERAND (rhs, 1);
9015 if (TREE_CODE (n) == VAR_DECL
9016 && DECL_NAME (n) == TYPE_identifier_node
9017 && rhs_type == class_ptr_type)
9018 {
49f48c71 9019 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
9020 if (!strncmp (self_name, "java.lang.", 10))
9021 to_return = build_primtype_type_ref (self_name);
9022 }
9023 }
9024 return (to_return ? to_return : rhs );
9025}
9026
e04a16fb
AG
9027/* 15.25 Assignment operators. */
9028
9029static tree
9030patch_assignment (node, wfl_op1, wfl_op2)
9031 tree node;
9032 tree wfl_op1;
9033 tree wfl_op2;
9034{
0a2138e2 9035 tree rhs = TREE_OPERAND (node, 1);
5e942c50 9036 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 9037 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
9038 int error_found = 0;
9039 int lvalue_from_array = 0;
9040
9041 /* Can't assign to a final. */
5e942c50
APB
9042 if (check_final_assignment (lvalue, wfl_op1))
9043 error_found = 1;
e04a16fb
AG
9044
9045 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9046
9047 /* Lhs can be a named variable */
34f4db93 9048 if (JDECL_P (lvalue))
e04a16fb 9049 {
e04a16fb
AG
9050 lhs_type = TREE_TYPE (lvalue);
9051 }
9052 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
9053 comment on reason why */
9054 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
9055 {
9056 lhs_type = TREE_TYPE (lvalue);
9057 lvalue_from_array = 1;
9058 }
9059 /* Or a field access */
9060 else if (TREE_CODE (lvalue) == COMPONENT_REF)
9061 lhs_type = TREE_TYPE (lvalue);
9062 /* Or a function return slot */
9063 else if (TREE_CODE (lvalue) == RESULT_DECL)
9064 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
9065 /* Otherwise, we might want to try to write into an optimized static
9066 final, this is an of a different nature, reported further on. */
9067 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 9068 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 9069 {
1504b2b4
APB
9070 if (check_final_assignment (llvalue, wfl_op1))
9071 {
9072 /* What we should do instead is resetting the all the flags
9073 previously set, exchange lvalue for llvalue and continue. */
9074 error_found = 1;
9075 return error_mark_node;
9076 }
9077 else
9078 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
9079 }
9080 else
e04a16fb
AG
9081 {
9082 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
9083 error_found = 1;
9084 }
9085
9086 rhs_type = TREE_TYPE (rhs);
b67d701b 9087 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 9088 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 9089
b67d701b 9090 /* 5.2 If it failed, try a reference conversion */
0a2138e2 9091 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 9092 lhs_type = promote_type (rhs_type);
e04a16fb
AG
9093
9094 /* 15.25.2 If we have a compound assignment, convert RHS into the
9095 type of the LHS */
9096 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
9097 new_rhs = convert (lhs_type, rhs);
9098
9099 /* Explicit cast required. This is an error */
9100 if (!new_rhs)
9101 {
0a2138e2
APB
9102 char *t1 = strdup (lang_printable_name (TREE_TYPE (rhs), 0));
9103 char *t2 = strdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
9104 tree wfl;
9105 char operation [32]; /* Max size known */
9106
9107 /* If the assignment is part of a declaration, we use the WFL of
9108 the declared variable to point out the error and call it a
9109 declaration problem. If the assignment is a genuine =
9110 operator, we call is a operator `=' problem, otherwise we
9111 call it an assignment problem. In both of these last cases,
9112 we use the WFL of the operator to indicate the error. */
9113
9114 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
9115 {
9116 wfl = wfl_op1;
9117 strcpy (operation, "declaration");
9118 }
9119 else
9120 {
9121 wfl = wfl_operator;
9122 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
9123 strcpy (operation, "assignment");
9124 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
9125 strcpy (operation, "`return'");
9126 else
9127 strcpy (operation, "`='");
9128 }
9129
9130 parse_error_context
b67d701b 9131 (wfl, (!valid_cast_to_p (rhs_type, lhs_type) ?
e04a16fb
AG
9132 "Incompatible type for %s. Can't convert `%s' to `%s'" :
9133 "Incompatible type for %s. Explicit cast "
9134 "needed to convert `%s' to `%s'"), operation, t1, t2);
9135 free (t1); free (t2);
9136 error_found = 1;
9137 }
9138
c877974e
APB
9139 /* Inline read access to java.lang.PRIMTYPE.TYPE */
9140 if (new_rhs)
9141 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 9142
e04a16fb
AG
9143 if (error_found)
9144 return error_mark_node;
9145
2622b947
APB
9146 /* 10.10: Array Store Exception runtime check */
9147 if (!flag_emit_class_files
e8fc7396 9148 && !flag_emit_xref
2622b947
APB
9149 && lvalue_from_array
9150 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type))
9151 && !CLASS_FINAL (TYPE_NAME (GET_SKIP_TYPE (rhs_type))))
9152 {
9153 tree check;
9154 tree base = lvalue;
9155
9156 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
9157 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
9158 base = TREE_OPERAND (lvalue, 0);
9159 else
9160 {
9161 if (flag_bounds_check)
9162 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
9163 else
9164 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
9165 }
9166
9167 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 9168 new_rhs = save_expr (new_rhs);
2622b947
APB
9169 check = build (CALL_EXPR, void_type_node,
9170 build_address_of (soft_checkarraystore_node),
9171 tree_cons (NULL_TREE, base,
9172 build_tree_list (NULL_TREE, new_rhs)),
9173 NULL_TREE);
9174 TREE_SIDE_EFFECTS (check) = 1;
9175
9176 /* We have to decide on an insertion point */
9177 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
9178 {
9179 tree t;
9180 if (flag_bounds_check)
9181 {
9182 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
9183 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
9184 build (COMPOUND_EXPR, void_type_node, t, check);
9185 }
9186 else
9187 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
9188 check, TREE_OPERAND (lvalue, 1));
9189 }
9190 else
9191 {
9192 /* Make sure the bound check will happen before the store check */
9193 if (flag_bounds_check)
9194 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
9195 build (COMPOUND_EXPR, void_type_node,
9196 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
9197 else
9198 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
9199 }
9200 }
22eed1e6 9201
e04a16fb
AG
9202 TREE_OPERAND (node, 0) = lvalue;
9203 TREE_OPERAND (node, 1) = new_rhs;
9204 TREE_TYPE (node) = lhs_type;
9205 return node;
9206}
9207
b67d701b
PB
9208/* Check that type SOURCE can be cast into type DEST. If the cast
9209 can't occur at all, return 0 otherwise 1. This function is used to
9210 produce accurate error messages on the reasons why an assignment
9211 failed. */
e04a16fb 9212
b67d701b
PB
9213static tree
9214try_reference_assignconv (lhs_type, rhs)
9215 tree lhs_type, rhs;
e04a16fb 9216{
b67d701b
PB
9217 tree new_rhs = NULL_TREE;
9218 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 9219
b67d701b
PB
9220 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
9221 {
9222 /* `null' may be assigned to any reference type */
9223 if (rhs == null_pointer_node)
9224 new_rhs = null_pointer_node;
9225 /* Try the reference assignment conversion */
9226 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
9227 new_rhs = rhs;
9228 /* This is a magic assignment that we process differently */
9229 else if (rhs == soft_exceptioninfo_call_node)
9230 new_rhs = rhs;
9231 }
9232 return new_rhs;
9233}
9234
9235/* Check that RHS can be converted into LHS_TYPE by the assignment
9236 conversion (5.2), for the cases of RHS being a builtin type. Return
9237 NULL_TREE if the conversion fails or if because RHS isn't of a
9238 builtin type. Return a converted RHS if the conversion is possible. */
9239
9240static tree
9241try_builtin_assignconv (wfl_op1, lhs_type, rhs)
9242 tree wfl_op1, lhs_type, rhs;
9243{
9244 tree new_rhs = NULL_TREE;
9245 tree rhs_type = TREE_TYPE (rhs);
9246
5e942c50
APB
9247 /* Zero accepted everywhere */
9248 if (TREE_CODE (rhs) == INTEGER_CST
9249 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
9250 && JPRIMITIVE_TYPE_P (rhs_type))
9251 new_rhs = convert (lhs_type, rhs);
9252
b67d701b
PB
9253 /* 5.1.1 Try Identity Conversion,
9254 5.1.2 Try Widening Primitive Conversion */
5e942c50 9255 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
9256 new_rhs = convert (lhs_type, rhs);
9257
9258 /* Try a narrowing primitive conversion (5.1.3):
9259 - expression is a constant expression of type int AND
9260 - variable is byte, short or char AND
9261 - The value of the expression is representable in the type of the
9262 variable */
9263 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
9264 && (lhs_type == byte_type_node || lhs_type == char_type_node
9265 || lhs_type == short_type_node))
9266 {
9267 if (int_fits_type_p (rhs, lhs_type))
9268 new_rhs = convert (lhs_type, rhs);
9269 else if (wfl_op1) /* Might be called with a NULL */
9270 parse_warning_context
9271 (wfl_op1, "Constant expression `%s' to wide for narrowing "
9272 "primitive conversion to `%s'",
0a2138e2 9273 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
9274 /* Reported a warning that will turn into an error further
9275 down, so we don't return */
9276 }
9277
9278 return new_rhs;
9279}
9280
9281/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
9282 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
9283 0 is the conversion test fails. This implements parts the method
9284 invocation convertion (5.3). */
9285
9286static int
9287valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
9288 tree lhs_type, rhs_type;
9289{
acd663ee 9290 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
9291 if (lhs_type == rhs_type)
9292 return 1;
9293
acd663ee
APB
9294 /* Reject non primitive types */
9295 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
b67d701b
PB
9296 return 0;
9297
acd663ee
APB
9298 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
9299 than a char can't be converted into a char. Short can't too, but
9300 the < test below takes care of that */
b67d701b
PB
9301 if (lhs_type == char_type_node && rhs_type == byte_type_node)
9302 return 0;
9303
5e942c50
APB
9304 /* Accept all promoted type here. Note, we can't use <= in the test
9305 below, because we still need to bounce out assignments of short
9306 to char and the likes */
9307 if (lhs_type == int_type_node
9308 && (rhs_type == promoted_byte_type_node
9309 || rhs_type == promoted_short_type_node
9310 || rhs_type == promoted_char_type_node
9311 || rhs_type == promoted_boolean_type_node))
9312 return 1;
9313
acd663ee
APB
9314 /* From here, an integral is widened if its precision is smaller
9315 than the precision of the LHS or if the LHS is a floating point
9316 type, or the RHS is a float and the RHS a double. */
9317 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
9318 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
9319 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
9320 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
9321 return 1;
9322
9323 return 0;
e04a16fb
AG
9324}
9325
9326/* Check that something of SOURCE type can be assigned or cast to
9327 something of DEST type at runtime. Return 1 if the operation is
9328 valid, 0 otherwise. If CAST is set to 1, we're treating the case
9329 were SOURCE is cast into DEST, which borrows a lot of the
9330 assignment check. */
9331
9332static int
9333valid_ref_assignconv_cast_p (source, dest, cast)
9334 tree source;
9335 tree dest;
9336 int cast;
9337{
09ed0f70
APB
9338 /* SOURCE or DEST might be null if not from a declared entity. */
9339 if (!source || !dest)
9340 return 0;
5e942c50
APB
9341 if (JNULLP_TYPE_P (source))
9342 return 1;
e04a16fb
AG
9343 if (TREE_CODE (source) == POINTER_TYPE)
9344 source = TREE_TYPE (source);
9345 if (TREE_CODE (dest) == POINTER_TYPE)
9346 dest = TREE_TYPE (dest);
9347 /* Case where SOURCE is a class type */
9348 if (TYPE_CLASS_P (source))
9349 {
9350 if (TYPE_CLASS_P (dest))
9351 return source == dest || inherits_from_p (source, dest)
0a2138e2 9352 || (cast && inherits_from_p (dest, source));
e04a16fb
AG
9353 if (TYPE_INTERFACE_P (dest))
9354 {
9355 /* If doing a cast and SOURCE is final, the operation is
9356 always correct a compile time (because even if SOURCE
9357 does not implement DEST, a subclass of SOURCE might). */
9358 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
9359 return 1;
9360 /* Otherwise, SOURCE must implement DEST */
9361 return interface_of_p (dest, source);
9362 }
9363 /* DEST is an array, cast permited if SOURCE is of Object type */
9364 return (cast && source == object_type_node ? 1 : 0);
9365 }
9366 if (TYPE_INTERFACE_P (source))
9367 {
9368 if (TYPE_CLASS_P (dest))
9369 {
9370 /* If not casting, DEST must be the Object type */
9371 if (!cast)
9372 return dest == object_type_node;
9373 /* We're doing a cast. The cast is always valid is class
9374 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 9375 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
9376 return 1;
9377 else
9378 return interface_of_p (source, dest);
9379 }
9380 if (TYPE_INTERFACE_P (dest))
9381 {
9382 /* If doing a cast, then if SOURCE and DEST contain method
9383 with the same signature but different return type, then
9384 this is a (compile time) error */
9385 if (cast)
9386 {
9387 tree method_source, method_dest;
9388 tree source_type;
0a2138e2 9389 tree source_sig;
e04a16fb
AG
9390 tree source_name;
9391 for (method_source = TYPE_METHODS (source); method_source;
9392 method_source = TREE_CHAIN (method_source))
9393 {
9394 source_sig =
9395 build_java_argument_signature (TREE_TYPE (method_source));
9396 source_type = TREE_TYPE (TREE_TYPE (method_source));
9397 source_name = DECL_NAME (method_source);
9398 for (method_dest = TYPE_METHODS (dest);
9399 method_dest; method_dest = TREE_CHAIN (method_dest))
9400 if (source_sig ==
9401 build_java_argument_signature (TREE_TYPE (method_dest))
9402 && source_name == DECL_NAME (method_dest)
9403 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
9404 return 0;
9405 }
9406 return 1;
9407 }
9408 else
9409 return source == dest || interface_of_p (dest, source);
9410 }
9411 else /* Array */
93024893
APB
9412 return (cast ?
9413 (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0);
e04a16fb
AG
9414 }
9415 if (TYPE_ARRAY_P (source))
9416 {
9417 if (TYPE_CLASS_P (dest))
9418 return dest == object_type_node;
09ed0f70
APB
9419 /* Can't cast an array to an interface unless the interface is
9420 java.lang.Cloneable */
e04a16fb 9421 if (TYPE_INTERFACE_P (dest))
09ed0f70 9422 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
e04a16fb
AG
9423 else /* Arrays */
9424 {
9425 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
9426 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
9427
b9f7e36c
APB
9428 /* In case of severe errors, they turn out null */
9429 if (!dest_element_type || !source_element_type)
9430 return 0;
e04a16fb
AG
9431 if (source_element_type == dest_element_type)
9432 return 1;
9433 return valid_ref_assignconv_cast_p (source_element_type,
9434 dest_element_type, cast);
9435 }
9436 return 0;
9437 }
9438 return 0;
9439}
9440
b67d701b
PB
9441static int
9442valid_cast_to_p (source, dest)
9443 tree source;
9444 tree dest;
9445{
9446 if (TREE_CODE (source) == POINTER_TYPE)
9447 source = TREE_TYPE (source);
9448 if (TREE_CODE (dest) == POINTER_TYPE)
9449 dest = TREE_TYPE (dest);
9450
9451 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
9452 return valid_ref_assignconv_cast_p (source, dest, 1);
9453
9454 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
9455 return 1;
9456
9457 return 0;
9458}
9459
9460/* Method invocation conversion test. Return 1 if type SOURCE can be
9461 converted to type DEST through the methond invocation conversion
9462 process (5.3) */
9463
15fdcfe9
PB
9464static tree
9465do_unary_numeric_promotion (arg)
9466 tree arg;
9467{
9468 tree type = TREE_TYPE (arg);
9469 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
9470 : TREE_CODE (type) == CHAR_TYPE)
9471 arg = convert (int_type_node, arg);
9472 return arg;
9473}
9474
acd663ee
APB
9475/* Return a non zero value if SOURCE can be converted into DEST using
9476 the method invocation conversion rule (5.3). */
b67d701b
PB
9477static int
9478valid_method_invocation_conversion_p (dest, source)
9479 tree dest, source;
9480{
e3884b71 9481 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
9482 && valid_builtin_assignconv_identity_widening_p (dest, source))
9483 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
9484 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
9485 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
9486}
9487
e04a16fb
AG
9488/* Build an incomplete binop expression. */
9489
9490static tree
9491build_binop (op, op_location, op1, op2)
9492 enum tree_code op;
9493 int op_location;
9494 tree op1, op2;
9495{
5e942c50 9496 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
9497 TREE_SIDE_EFFECTS (binop) = 1;
9498 /* Store the location of the operator, for better error report. The
9499 string of the operator will be rebuild based on the OP value. */
9500 EXPR_WFL_LINECOL (binop) = op_location;
9501 return binop;
9502}
9503
9504/* Build the string of the operator retained by NODE. If NODE is part
9505 of a compound expression, add an '=' at the end of the string. This
9506 function is called when an error needs to be reported on an
9507 operator. The string is returned as a pointer to a static character
9508 buffer. */
9509
9510static char *
9511operator_string (node)
9512 tree node;
9513{
9514#define BUILD_OPERATOR_STRING(S) \
9515 { \
9516 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
9517 return buffer; \
9518 }
9519
9520 static char buffer [10];
9521 switch (TREE_CODE (node))
9522 {
9523 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
9524 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
9525 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
9526 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
9527 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
9528 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
9529 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
9530 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
9531 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
9532 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
9533 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
9534 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
9535 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
9536 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
9537 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
9538 case GT_EXPR: BUILD_OPERATOR_STRING (">");
9539 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
9540 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
9541 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 9542 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
9543 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
9544 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
9545 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
9546 case PREINCREMENT_EXPR: /* Fall through */
9547 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
9548 case PREDECREMENT_EXPR: /* Fall through */
9549 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
9550 default:
9551 fatal ("unregistered operator %s - operator_string",
9552 tree_code_name [TREE_CODE (node)]);
9553 }
9554 return NULL;
9555#undef BUILD_OPERATOR_STRING
9556}
9557
5cbdba64
APB
9558/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
9559
9560static int
9561java_decl_equiv (var_acc1, var_acc2)
9562 tree var_acc1, var_acc2;
9563{
9564 if (JDECL_P (var_acc1))
9565 return (var_acc1 == var_acc2);
9566
9567 return (TREE_CODE (var_acc1) == COMPONENT_REF
9568 && TREE_CODE (var_acc2) == COMPONENT_REF
9569 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
9570 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
9571 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
9572}
9573
9574/* Return a non zero value if CODE is one of the operators that can be
9575 used in conjunction with the `=' operator in a compound assignment. */
9576
9577static int
9578binop_compound_p (code)
9579 enum tree_code code;
9580{
9581 int i;
9582 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
9583 if (binop_lookup [i] == code)
9584 break;
9585
9586 return i < BINOP_COMPOUND_CANDIDATES;
9587}
9588
9589/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
9590
9591static tree
9592java_refold (t)
9593 tree t;
9594{
9595 tree c, b, ns, decl;
9596
9597 if (TREE_CODE (t) != MODIFY_EXPR)
9598 return t;
9599
9600 c = TREE_OPERAND (t, 1);
9601 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
9602 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
9603 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
9604 return t;
9605
9606 /* Now the left branch of the binary operator. */
9607 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
9608 if (! (b && TREE_CODE (b) == NOP_EXPR
9609 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
9610 return t;
9611
9612 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
9613 if (! (ns && TREE_CODE (ns) == NOP_EXPR
9614 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
9615 return t;
9616
9617 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
9618 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
9619 /* It's got to be the an equivalent decl */
9620 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
9621 {
9622 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
9623 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
9624 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
9625 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
9626 /* Change the right part of the BINOP_EXPR */
9627 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
9628 }
9629
9630 return t;
9631}
9632
e04a16fb
AG
9633/* Binary operators (15.16 up to 15.18). We return error_mark_node on
9634 errors but we modify NODE so that it contains the type computed
9635 according to the expression, when it's fixed. Otherwise, we write
9636 error_mark_node as the type. It allows us to further the analysis
9637 of remaining nodes and detects more errors in certain cases. */
9638
9639static tree
9640patch_binop (node, wfl_op1, wfl_op2)
9641 tree node;
9642 tree wfl_op1;
9643 tree wfl_op2;
9644{
9645 tree op1 = TREE_OPERAND (node, 0);
9646 tree op2 = TREE_OPERAND (node, 1);
9647 tree op1_type = TREE_TYPE (op1);
9648 tree op2_type = TREE_TYPE (op2);
ab3a6dd6 9649 tree prom_type = NULL_TREE;
e04a16fb 9650 int code = TREE_CODE (node);
b67d701b 9651
e04a16fb
AG
9652 /* If 1, tell the routine that we have to return error_mark_node
9653 after checking for the initialization of the RHS */
9654 int error_found = 0;
9655
e04a16fb
AG
9656 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9657
e04a16fb
AG
9658 switch (code)
9659 {
9660 /* 15.16 Multiplicative operators */
9661 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
9662 case RDIV_EXPR: /* 15.16.2 Division Operator / */
9663 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
9664 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
9665 {
9666 if (!JPRIMITIVE_TYPE_P (op1_type))
9667 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
9668 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
9669 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
9670 TREE_TYPE (node) = error_mark_node;
9671 error_found = 1;
9672 break;
9673 }
9674 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9675 /* Change the division operator if necessary */
9676 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
9677 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 9678
aa4759c1
AH
9679 if (TREE_CODE (prom_type) == INTEGER_TYPE
9680 && flag_use_divide_subroutine
9681 && ! flag_emit_class_files
9682 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
9683 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
9684
0b4d333e
APB
9685 /* This one is more complicated. FLOATs are processed by a
9686 function call to soft_fmod. Duplicate the value of the
9687 COMPOUND_ASSIGN_P flag. */
e04a16fb 9688 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
9689 {
9690 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
9691 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
9692 TREE_SIDE_EFFECTS (mod)
9693 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
9694 return mod;
9695 }
e04a16fb
AG
9696 break;
9697
9698 /* 15.17 Additive Operators */
9699 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
9700
9701 /* Operation is valid if either one argument is a string
9702 constant, a String object or a StringBuffer crafted for the
9703 purpose of the a previous usage of the String concatenation
9704 operator */
9705
9706 if (TREE_CODE (op1) == STRING_CST
9707 || TREE_CODE (op2) == STRING_CST
9708 || JSTRING_TYPE_P (op1_type)
9709 || JSTRING_TYPE_P (op2_type)
9710 || IS_CRAFTED_STRING_BUFFER_P (op1)
9711 || IS_CRAFTED_STRING_BUFFER_P (op2))
9712 return build_string_concatenation (op1, op2);
9713
e04a16fb
AG
9714 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
9715 Numeric Types */
9716 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
9717 {
9718 if (!JPRIMITIVE_TYPE_P (op1_type))
9719 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
9720 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
9721 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
9722 TREE_TYPE (node) = error_mark_node;
9723 error_found = 1;
9724 break;
9725 }
9726 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9727 break;
9728
9729 /* 15.18 Shift Operators */
9730 case LSHIFT_EXPR:
9731 case RSHIFT_EXPR:
9732 case URSHIFT_EXPR:
9733 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
9734 {
9735 if (!JINTEGRAL_TYPE_P (op1_type))
9736 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
9737 else
9738 parse_error_context
9739 (wfl_operator, (JPRIMITIVE_TYPE_P (op2_type) ?
9740 "Incompatible type for `%s'. Explicit cast needed to convert "
9741 "shift distance from `%s' to integral" :
9742 "Incompatible type for `%s'. Can't convert shift distance from "
9743 "`%s' to integral"),
0a2138e2 9744 operator_string (node), lang_printable_name (op2_type, 0));
e04a16fb
AG
9745 TREE_TYPE (node) = error_mark_node;
9746 error_found = 1;
9747 break;
9748 }
9749
9750 /* Unary numeric promotion (5.6.1) is performed on each operand
9751 separatly */
15fdcfe9
PB
9752 op1 = do_unary_numeric_promotion (op1);
9753 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
9754
9755 /* The type of the shift expression is the type of the promoted
9756 type of the left-hand operand */
9757 prom_type = TREE_TYPE (op1);
9758
9759 /* Shift int only up to 0x1f and long up to 0x3f */
9760 if (prom_type == int_type_node)
9761 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
9762 build_int_2 (0x1f, 0)));
9763 else
9764 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
9765 build_int_2 (0x3f, 0)));
9766
9767 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 9768 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 9769 {
0b4d333e 9770 tree to_return;
73333a87
AH
9771 tree utype = unsigned_type (prom_type);
9772 op1 = convert (utype, op1);
e04a16fb 9773 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
9774 TREE_OPERAND (node, 0) = op1;
9775 TREE_OPERAND (node, 1) = op2;
9776 TREE_TYPE (node) = utype;
0b4d333e
APB
9777 to_return = convert (prom_type, node);
9778 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
9779 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
9780 TREE_SIDE_EFFECTS (to_return)
9781 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 9782 return to_return;
e04a16fb
AG
9783 }
9784 break;
5e942c50
APB
9785
9786 /* 15.19.1 Type Comparison Operator instaceof */
9787 case INSTANCEOF_EXPR:
9788
9789 TREE_TYPE (node) = boolean_type_node;
9790
9791 if (!(op2_type = resolve_type_during_patch (op2)))
9792 return error_mark_node;
9793
9794 /* The first operand must be a reference type or the null type */
9795 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
9796 error_found = 1; /* Error reported further below */
9797
9798 /* The second operand must be a reference type */
9799 if (!JREFERENCE_TYPE_P (op2_type))
9800 {
9801 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
9802 parse_error_context
9803 (wfl_operator, "Invalid argument `%s' for `instanceof'",
9804 lang_printable_name (op2_type, 0));
9805 error_found = 1;
9806 }
9807
9808 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
9809 {
9810 /* If the first operand is null, the result is always false */
9811 if (op1 == null_pointer_node)
9812 return boolean_false_node;
15fdcfe9
PB
9813 else if (flag_emit_class_files)
9814 {
9815 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 9816 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
9817 return node;
9818 }
5e942c50
APB
9819 /* Otherwise we have to invoke instance of to figure it out */
9820 else
9821 {
9822 tree call =
9823 build (CALL_EXPR, boolean_type_node,
9824 build_address_of (soft_instanceof_node),
9825 tree_cons
9826 (NULL_TREE, op1,
9827 build_tree_list (NULL_TREE,
9828 build_class_ref (op2_type))),
9829 NULL_TREE);
dc0b3eff 9830 TREE_SIDE_EFFECTS (call) = TREE_SIDE_EFFECTS (op1);
5e942c50
APB
9831 return call;
9832 }
9833 }
9834 /* There is no way the expression operand can be an instance of
9835 the type operand. This is a compile time error. */
9836 else
9837 {
9838 char *t1 = strdup (lang_printable_name (op1_type, 0));
9839 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
9840 parse_error_context
9841 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
9842 t1, lang_printable_name (op2_type, 0));
9843 free (t1);
9844 error_found = 1;
9845 }
e04a16fb 9846
5e942c50 9847 break;
e04a16fb
AG
9848
9849 /* 15.21 Bitwise and Logical Operators */
9850 case BIT_AND_EXPR:
9851 case BIT_XOR_EXPR:
9852 case BIT_IOR_EXPR:
9853 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
9854 /* Binary numeric promotion is performed on both operand and the
9855 expression retain that type */
9856 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9857
9858 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
9859 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
9860 /* The type of the bitwise operator expression is BOOLEAN */
9861 prom_type = boolean_type_node;
9862 else
9863 {
9864 if (!JINTEGRAL_TYPE_P (op1_type))
9865 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
9866 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
9867 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
9868 TREE_TYPE (node) = error_mark_node;
9869 error_found = 1;
9870 /* Insert a break here if adding thing before the switch's
9871 break for this case */
9872 }
9873 break;
9874
9875 /* 15.22 Conditional-And Operator */
9876 case TRUTH_ANDIF_EXPR:
9877 /* 15.23 Conditional-Or Operator */
9878 case TRUTH_ORIF_EXPR:
9879 /* Operands must be of BOOLEAN type */
9880 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
9881 TREE_CODE (op2_type) != BOOLEAN_TYPE)
9882 {
9883 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
9884 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
9885 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
9886 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
9887 TREE_TYPE (node) = boolean_type_node;
9888 error_found = 1;
9889 break;
9890 }
9891 /* The type of the conditional operators is BOOLEAN */
9892 prom_type = boolean_type_node;
9893 break;
9894
9895 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
9896 case LT_EXPR:
9897 case GT_EXPR:
9898 case LE_EXPR:
9899 case GE_EXPR:
9900 /* The type of each of the operands must be a primitive numeric
9901 type */
9902 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
9903 {
9904 if (!JNUMERIC_TYPE_P (op1_type))
9905 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
9906 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
9907 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
9908 TREE_TYPE (node) = boolean_type_node;
9909 error_found = 1;
9910 break;
9911 }
9912 /* Binary numeric promotion is performed on the operands */
9913 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9914 /* The type of the relation expression is always BOOLEAN */
9915 prom_type = boolean_type_node;
9916 break;
9917
9918 /* 15.20 Equality Operator */
9919 case EQ_EXPR:
9920 case NE_EXPR:
9921 /* 15.20.1 Numerical Equality Operators == and != */
9922 /* Binary numeric promotion is performed on the operands */
5e942c50 9923 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
9924 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9925
9926 /* 15.20.2 Boolean Equality Operators == and != */
9927 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
9928 TREE_CODE (op2_type) == BOOLEAN_TYPE)
9929 ; /* Nothing to do here */
9930
9931 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
9932 /* Types have to be either references or the null type. If
9933 they're references, it must be possible to convert either
9934 type to the other by casting conversion. */
b9f7e36c
APB
9935 else if (op1 == null_pointer_node || op2 == null_pointer_node
9936 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
9937 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
9938 || valid_ref_assignconv_cast_p (op2_type,
9939 op1_type, 1))))
e04a16fb
AG
9940 ; /* Nothing to do here */
9941
9942 /* Else we have an error figure what can't be converted into
9943 what and report the error */
9944 else
9945 {
9946 char *t1;
0a2138e2 9947 t1 = strdup (lang_printable_name (op1_type, 0));
e04a16fb
AG
9948 parse_error_context
9949 (wfl_operator, "Incompatible type for `%s'. Can't convert `%s' "
9950 "to `%s'", operator_string (node), t1,
0a2138e2 9951 lang_printable_name (op2_type, 0));
e04a16fb
AG
9952 free (t1);
9953 TREE_TYPE (node) = boolean_type_node;
9954 error_found = 1;
9955 break;
9956 }
9957 prom_type = boolean_type_node;
9958 break;
9959 }
9960
e04a16fb
AG
9961 if (error_found)
9962 return error_mark_node;
9963
9964 TREE_OPERAND (node, 0) = op1;
9965 TREE_OPERAND (node, 1) = op2;
9966 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
9967 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
9968
ce6e9147
APB
9969 if (flag_emit_xref)
9970 return node;
9971
d1472141
PB
9972 /* fold does not respect side-effect order as required for Java but not C.
9973 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
9974 * bytecode.
9975 */
9976 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
9977 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
9978 node = fold (node);
9979 return node;
e04a16fb
AG
9980}
9981
b67d701b
PB
9982/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
9983 zero value, the value of CSTE comes after the valude of STRING */
9984
9985static tree
9986do_merge_string_cste (cste, string, string_len, after)
9987 tree cste;
49f48c71 9988 const char *string;
b67d701b
PB
9989 int string_len, after;
9990{
9991 int len = TREE_STRING_LENGTH (cste) + string_len;
49f48c71 9992 const char *old = TREE_STRING_POINTER (cste);
b67d701b
PB
9993 TREE_STRING_LENGTH (cste) = len;
9994 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
9995 if (after)
9996 {
9997 strcpy (TREE_STRING_POINTER (cste), string);
9998 strcat (TREE_STRING_POINTER (cste), old);
9999 }
10000 else
10001 {
10002 strcpy (TREE_STRING_POINTER (cste), old);
10003 strcat (TREE_STRING_POINTER (cste), string);
10004 }
10005 return cste;
10006}
10007
10008/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
10009 new STRING_CST on success, NULL_TREE on failure */
10010
10011static tree
10012merge_string_cste (op1, op2, after)
10013 tree op1, op2;
10014 int after;
10015{
10016 /* Handle two string constants right away */
10017 if (TREE_CODE (op2) == STRING_CST)
10018 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
10019 TREE_STRING_LENGTH (op2), after);
10020
10021 /* Reasonable integer constant can be treated right away */
10022 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
10023 {
49f48c71
KG
10024 static const char *boolean_true = "true";
10025 static const char *boolean_false = "false";
10026 static const char *null_pointer = "null";
b67d701b 10027 char ch[3];
49f48c71 10028 const char *string;
b67d701b
PB
10029
10030 if (op2 == boolean_true_node)
10031 string = boolean_true;
10032 else if (op2 == boolean_false_node)
10033 string = boolean_false;
10034 else if (op2 == null_pointer_node)
10035 string = null_pointer;
10036 else if (TREE_TYPE (op2) == char_type_node)
10037 {
10038 ch[0] = (char )TREE_INT_CST_LOW (op2);
10039 ch[1] = '\0';
10040 string = ch;
10041 }
10042 else
10043 string = print_int_node (op2);
10044
10045 return do_merge_string_cste (op1, string, strlen (string), after);
10046 }
10047 return NULL_TREE;
10048}
10049
10050/* Tries to statically concatenate OP1 and OP2 if possible. Either one
10051 has to be a STRING_CST and the other part must be a STRING_CST or a
10052 INTEGRAL constant. Return a new STRING_CST if the operation
10053 succeed, NULL_TREE otherwise.
10054
10055 If the case we want to optimize for space, we might want to return
10056 NULL_TREE for each invocation of this routine. FIXME */
10057
10058static tree
10059string_constant_concatenation (op1, op2)
10060 tree op1, op2;
10061{
10062 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
10063 {
0a2138e2 10064 tree string, rest;
b67d701b
PB
10065 int invert;
10066
10067 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
10068 rest = (string == op1 ? op2 : op1);
10069 invert = (string == op1 ? 0 : 1 );
10070
10071 /* Walk REST, only if it looks reasonable */
10072 if (TREE_CODE (rest) != STRING_CST
10073 && !IS_CRAFTED_STRING_BUFFER_P (rest)
10074 && !JSTRING_TYPE_P (TREE_TYPE (rest))
10075 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
10076 {
10077 rest = java_complete_tree (rest);
10078 if (rest == error_mark_node)
10079 return error_mark_node;
10080 rest = fold (rest);
10081 }
10082 return merge_string_cste (string, rest, invert);
10083 }
10084 return NULL_TREE;
10085}
10086
10087/* Implement the `+' operator. Does static optimization if possible,
10088 otherwise create (if necessary) and append elements to a
10089 StringBuffer. The StringBuffer will be carried around until it is
10090 used for a function call or an assignment. Then toString() will be
10091 called on it to turn it into a String object. */
10092
10093static tree
10094build_string_concatenation (op1, op2)
10095 tree op1, op2;
10096{
10097 tree result;
dc0b3eff 10098 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
10099
10100 if (flag_emit_xref)
10101 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
10102
10103 /* Try to do some static optimization */
10104 if ((result = string_constant_concatenation (op1, op2)))
10105 return result;
10106
c0d87ff6
PB
10107 /* Discard empty strings on either side of the expression */
10108 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
10109 {
10110 op1 = op2;
10111 op2 = NULL_TREE;
10112 }
c0d87ff6 10113 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 10114 op2 = NULL_TREE;
b67d701b 10115
acd663ee 10116 /* If operands are string constant, turn then into object references */
b67d701b
PB
10117 if (TREE_CODE (op1) == STRING_CST)
10118 op1 = patch_string_cst (op1);
acd663ee 10119 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
10120 op2 = patch_string_cst (op2);
10121
acd663ee
APB
10122 /* If either one of the constant is null and the other non null
10123 operand is a String object, return it. */
10124 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
10125 return op1;
10126
b67d701b
PB
10127 /* If OP1 isn't already a StringBuffer, create and
10128 initialize a new one */
10129 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
10130 {
10131 /* Two solutions here:
10132 1) OP1 is a string reference, we call new StringBuffer(OP1)
acd663ee 10133 2) OP1 is something else, we call new StringBuffer().append(OP1). */
b67d701b
PB
10134 if (JSTRING_TYPE_P (TREE_TYPE (op1)))
10135 op1 = BUILD_STRING_BUFFER (op1);
10136 else
10137 {
10138 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
10139 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
10140 }
10141 }
10142
acd663ee
APB
10143 if (op2)
10144 {
10145 /* OP1 is no longer the last node holding a crafted StringBuffer */
10146 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
10147 /* Create a node for `{new...,xxx}.append (op2)' */
10148 if (op2)
10149 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
10150 }
10151
b67d701b
PB
10152 /* Mark the last node holding a crafted StringBuffer */
10153 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
10154
10155 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
10156 return op1;
10157}
10158
10159/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
10160 StringBuffer. If no string were found to be patched, return
10161 NULL. */
10162
10163static tree
10164patch_string (node)
10165 tree node;
10166{
1179ebc2
APB
10167 if (node == error_mark_node)
10168 return error_mark_node;
b67d701b
PB
10169 if (TREE_CODE (node) == STRING_CST)
10170 return patch_string_cst (node);
10171 else if (IS_CRAFTED_STRING_BUFFER_P (node))
10172 {
c877974e 10173 int saved = ctxp->explicit_constructor_p;
b67d701b 10174 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
10175 tree ret;
10176 /* Temporary disable forbid the use of `this'. */
10177 ctxp->explicit_constructor_p = 0;
10178 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
10179 /* Restore it at its previous value */
10180 ctxp->explicit_constructor_p = saved;
10181 return ret;
b67d701b
PB
10182 }
10183 return NULL_TREE;
10184}
10185
10186/* Build the internal representation of a string constant. */
10187
10188static tree
10189patch_string_cst (node)
10190 tree node;
10191{
10192 int location;
15fdcfe9
PB
10193 if (! flag_emit_class_files)
10194 {
10195 push_obstacks (&permanent_obstack, &permanent_obstack);
10196 node = get_identifier (TREE_STRING_POINTER (node));
10197 location = alloc_name_constant (CONSTANT_String, node);
10198 node = build_ref_from_constant_pool (location);
10199 }
cd9643f7 10200 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
10201 TREE_CONSTANT (node) = 1;
10202 return node;
10203}
10204
10205/* Build an incomplete unary operator expression. */
e04a16fb
AG
10206
10207static tree
10208build_unaryop (op_token, op_location, op1)
10209 int op_token, op_location;
10210 tree op1;
10211{
10212 enum tree_code op;
10213 tree unaryop;
10214 switch (op_token)
10215 {
b67d701b 10216 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
10217 case MINUS_TK: op = NEGATE_EXPR; break;
10218 case NEG_TK: op = TRUTH_NOT_EXPR; break;
10219 case NOT_TK: op = BIT_NOT_EXPR; break;
10220 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
10221 op_token);
10222 }
10223
10224 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
10225 TREE_SIDE_EFFECTS (unaryop) = 1;
10226 /* Store the location of the operator, for better error report. The
10227 string of the operator will be rebuild based on the OP value. */
10228 EXPR_WFL_LINECOL (unaryop) = op_location;
10229 return unaryop;
10230}
10231
10232/* Special case for the ++/-- operators, since they require an extra
10233 argument to build, which is set to NULL and patched
10234 later. IS_POST_P is 1 if the operator, 0 otherwise. */
10235
10236static tree
10237build_incdec (op_token, op_location, op1, is_post_p)
10238 int op_token, op_location;
10239 tree op1;
10240 int is_post_p;
10241{
10242 static enum tree_code lookup [2][2] =
10243 {
10244 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
10245 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
10246 };
10247 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
10248 NULL_TREE, op1, NULL_TREE);
10249 TREE_SIDE_EFFECTS (node) = 1;
10250 /* Store the location of the operator, for better error report. The
10251 string of the operator will be rebuild based on the OP value. */
10252 EXPR_WFL_LINECOL (node) = op_location;
10253 return node;
10254}
10255
10256/* Build an incomplete cast operator, based on the use of the
10257 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
10258 set. java_complete_tree is trained to walk a CONVERT_EXPR even
10259 though its type is already set. */
10260
10261static tree
10262build_cast (location, type, exp)
10263 int location;
10264 tree type, exp;
10265{
10266 tree node = build1 (CONVERT_EXPR, type, exp);
10267 EXPR_WFL_LINECOL (node) = location;
10268 return node;
10269}
10270
10271/* 15.14 Unary operators. We return error_mark_node in case of error,
10272 but preserve the type of NODE if the type is fixed. */
10273
10274static tree
10275patch_unaryop (node, wfl_op)
10276 tree node;
10277 tree wfl_op;
10278{
10279 tree op = TREE_OPERAND (node, 0);
10280 tree op_type = TREE_TYPE (op);
ab3a6dd6 10281 tree prom_type = NULL_TREE, value, decl;
e04a16fb
AG
10282 int code = TREE_CODE (node);
10283 int error_found = 0;
10284
10285 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10286
10287 switch (code)
10288 {
10289 /* 15.13.2 Postfix Increment Operator ++ */
10290 case POSTINCREMENT_EXPR:
10291 /* 15.13.3 Postfix Increment Operator -- */
10292 case POSTDECREMENT_EXPR:
10293 /* 15.14.1 Prefix Increment Operator ++ */
10294 case PREINCREMENT_EXPR:
10295 /* 15.14.2 Prefix Decrement Operator -- */
10296 case PREDECREMENT_EXPR:
5cbdba64 10297 op = decl = strip_out_static_field_access_decl (op);
b3edebcf 10298 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
e28cd97b 10299 if (!JDECL_P (decl)
b3edebcf
APB
10300 && TREE_CODE (decl) != COMPONENT_REF
10301 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
10302 && TREE_CODE (decl) != INDIRECT_REF
10303 && !(TREE_CODE (decl) == COMPOUND_EXPR
10304 && TREE_OPERAND (decl, 1)
10305 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 10306 {
5e942c50
APB
10307 tree lvalue;
10308 /* Before screaming, check that we're not in fact trying to
10309 increment a optimized static final access, in which case
10310 we issue an different error message. */
10311 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
10312 && resolve_expression_name (wfl_op, &lvalue)
10313 && check_final_assignment (lvalue, wfl_op)))
10314 parse_error_context (wfl_operator, "Invalid argument to `%s'",
10315 operator_string (node));
e04a16fb
AG
10316 TREE_TYPE (node) = error_mark_node;
10317 error_found = 1;
10318 }
5e942c50
APB
10319 else if (check_final_assignment (op, wfl_op))
10320 error_found = 1;
10321
e04a16fb
AG
10322 /* From now on, we know that op if a variable and that it has a
10323 valid wfl. We use wfl_op to locate errors related to the
10324 ++/-- operand. */
10325 else if (!JNUMERIC_TYPE_P (op_type))
10326 {
10327 parse_error_context
10328 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 10329 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
10330 TREE_TYPE (node) = error_mark_node;
10331 error_found = 1;
10332 }
10333 else
10334 {
4a5f66c3 10335 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
10336 both operands, if really necessary */
10337 if (JINTEGRAL_TYPE_P (op_type))
10338 {
10339 value = build_int_2 (1, 0);
10340 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
10341 }
10342 else
10343 {
10344 value = build_int_2 (1, 0);
10345 TREE_TYPE (node) =
10346 binary_numeric_promotion (op_type,
10347 TREE_TYPE (value), &op, &value);
10348 }
10349 /* And write back into the node. */
4a5f66c3 10350 TREE_OPERAND (node, 0) = op;
e04a16fb 10351 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
10352 /* Convert the overall back into its original type, if
10353 necessary, and return */
10354 if (JINTEGRAL_TYPE_P (op_type))
10355 return fold (node);
10356 else
10357 return fold (convert (op_type, node));
e04a16fb
AG
10358 }
10359 break;
10360
10361 /* 15.14.3 Unary Plus Operator + */
b67d701b 10362 case UNARY_PLUS_EXPR:
e04a16fb
AG
10363 /* 15.14.4 Unary Minus Operator - */
10364 case NEGATE_EXPR:
10365 if (!JNUMERIC_TYPE_P (op_type))
10366 {
10367 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
10368 TREE_TYPE (node) = error_mark_node;
10369 error_found = 1;
10370 }
10371 /* Unary numeric promotion is performed on operand */
10372 else
10373 {
15fdcfe9
PB
10374 op = do_unary_numeric_promotion (op);
10375 prom_type = TREE_TYPE (op);
b67d701b 10376 if (code == UNARY_PLUS_EXPR)
4a5f66c3 10377 return fold (op);
e04a16fb
AG
10378 }
10379 break;
10380
10381 /* 15.14.5 Bitwise Complement Operator ~ */
10382 case BIT_NOT_EXPR:
10383 if (!JINTEGRAL_TYPE_P (op_type))
10384 {
10385 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
10386 TREE_TYPE (node) = error_mark_node;
10387 error_found = 1;
10388 }
10389 else
10390 {
15fdcfe9
PB
10391 op = do_unary_numeric_promotion (op);
10392 prom_type = TREE_TYPE (op);
e04a16fb
AG
10393 }
10394 break;
10395
10396 /* 15.14.6 Logical Complement Operator ! */
10397 case TRUTH_NOT_EXPR:
10398 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
10399 {
10400 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
10401 /* But the type is known. We will report an error if further
10402 attempt of a assignment is made with this rhs */
e04a16fb
AG
10403 TREE_TYPE (node) = boolean_type_node;
10404 error_found = 1;
10405 }
10406 else
10407 prom_type = boolean_type_node;
10408 break;
10409
10410 /* 15.15 Cast Expression */
10411 case CONVERT_EXPR:
0a2138e2 10412 value = patch_cast (node, wfl_operator);
e04a16fb 10413 if (value == error_mark_node)
c877974e
APB
10414 {
10415 /* If this cast is part of an assignment, we tell the code
10416 that deals with it not to complain about a mismatch,
10417 because things have been cast, anyways */
10418 TREE_TYPE (node) = error_mark_node;
10419 error_found = 1;
10420 }
10421 else
dc0b3eff
PB
10422 {
10423 value = fold (value);
10424 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
10425 return value;
10426 }
e04a16fb
AG
10427 break;
10428 }
10429
e04a16fb
AG
10430 if (error_found)
10431 return error_mark_node;
4a5f66c3
APB
10432
10433 /* There are cases where node has been replaced by something else
10434 and we don't end up returning here: UNARY_PLUS_EXPR,
10435 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 10436 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 10437 TREE_TYPE (node) = prom_type;
dc0b3eff 10438 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
10439 return fold (node);
10440}
10441
10442/* Generic type resolution that sometimes takes place during node
10443 patching. Returned the resolved type or generate an error
10444 message. Return the resolved type or NULL_TREE. */
10445
10446static tree
10447resolve_type_during_patch (type)
10448 tree type;
10449{
10450 if (unresolved_type_p (type, NULL))
10451 {
10452 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
10453 if (!type_decl)
10454 {
10455 parse_error_context (type,
10456 "Class `%s' not found in type declaration",
10457 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
10458 return NULL_TREE;
10459 }
10460 else
5e942c50
APB
10461 {
10462 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
10463 return TREE_TYPE (type_decl);
10464 }
e04a16fb
AG
10465 }
10466 return type;
10467}
10468/* 5.5 Casting Conversion. error_mark_node is returned if an error is
10469 found. Otherwise NODE or something meant to replace it is returned. */
10470
10471static tree
0a2138e2 10472patch_cast (node, wfl_operator)
e04a16fb 10473 tree node;
e04a16fb
AG
10474 tree wfl_operator;
10475{
10476 tree op = TREE_OPERAND (node, 0);
10477 tree op_type = TREE_TYPE (op);
10478 tree cast_type = TREE_TYPE (node);
10479 char *t1;
10480
10481 /* First resolve OP_TYPE if unresolved */
10482 if (!(cast_type = resolve_type_during_patch (cast_type)))
10483 return error_mark_node;
10484
10485 /* Check on cast that are proven correct at compile time */
10486 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
10487 {
e04a16fb
AG
10488 /* Same type */
10489 if (cast_type == op_type)
10490 return node;
10491
0b4d333e
APB
10492 /* float and double type are converted to the original type main
10493 variant and then to the target type. */
10494 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
10495 op = convert (integer_type_node, op);
10496
e04a16fb
AG
10497 /* Try widening/narowwing convertion. Potentially, things need
10498 to be worked out in gcc so we implement the extreme cases
10499 correctly. fold_convert() needs to be fixed. */
10500 return convert (cast_type, op);
10501 }
10502
0b4d333e
APB
10503 /* It's also valid to cast a boolean into a boolean */
10504 if (op_type == boolean_type_node && cast_type == boolean_type_node)
10505 return node;
10506
5e942c50
APB
10507 /* null can be casted to references */
10508 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
10509 return build_null_of_type (cast_type);
10510
e04a16fb
AG
10511 /* The remaining legal casts involve conversion between reference
10512 types. Check for their compile time correctness. */
10513 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 10514 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
10515 {
10516 TREE_TYPE (node) = promote_type (cast_type);
10517 /* Now, the case can be determined correct at compile time if
10518 OP_TYPE can be converted into CAST_TYPE by assignment
10519 conversion (5.2) */
10520
10521 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
10522 {
10523 TREE_SET_CODE (node, NOP_EXPR);
10524 return node;
10525 }
10526
10527 if (flag_emit_class_files)
10528 {
10529 TREE_SET_CODE (node, CONVERT_EXPR);
10530 return node;
10531 }
e04a16fb
AG
10532
10533 /* The cast requires a run-time check */
10534 return build (CALL_EXPR, promote_type (cast_type),
10535 build_address_of (soft_checkcast_node),
10536 tree_cons (NULL_TREE, build_class_ref (cast_type),
10537 build_tree_list (NULL_TREE, op)),
10538 NULL_TREE);
10539 }
10540
10541 /* Any other casts are proven incorrect at compile time */
0a2138e2 10542 t1 = strdup (lang_printable_name (op_type, 0));
e04a16fb 10543 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
0a2138e2 10544 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
10545 free (t1);
10546 return error_mark_node;
10547}
10548
5e942c50
APB
10549/* Build a null constant and give it the type TYPE. */
10550
10551static tree
10552build_null_of_type (type)
10553 tree type;
10554{
10555 tree node = build_int_2 (0, 0);
10556 TREE_TYPE (node) = promote_type (type);
10557 return node;
10558}
10559
e04a16fb
AG
10560/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
10561 a list of indices. */
10562static tree
10563build_array_ref (location, array, index)
10564 int location;
10565 tree array, index;
10566{
10567 tree node = build (ARRAY_REF, NULL_TREE, array, index);
10568 EXPR_WFL_LINECOL (node) = location;
10569 return node;
10570}
10571
10572/* 15.12 Array Access Expression */
10573
10574static tree
c877974e
APB
10575patch_array_ref (node)
10576 tree node;
e04a16fb
AG
10577{
10578 tree array = TREE_OPERAND (node, 0);
10579 tree array_type = TREE_TYPE (array);
10580 tree index = TREE_OPERAND (node, 1);
10581 tree index_type = TREE_TYPE (index);
e04a16fb
AG
10582 int error_found = 0;
10583
10584 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10585
e04a16fb
AG
10586 if (TREE_CODE (array_type) == POINTER_TYPE)
10587 array_type = TREE_TYPE (array_type);
10588
10589 /* The array reference must be an array */
10590 if (!TYPE_ARRAY_P (array_type))
10591 {
10592 parse_error_context
10593 (wfl_operator, "`[]' can only be applied to arrays. It can't be "
0a2138e2 10594 "applied to `%s'", lang_printable_name (array_type, 0));
e04a16fb
AG
10595 TREE_TYPE (node) = error_mark_node;
10596 error_found = 1;
10597 }
10598
10599 /* The array index underdoes unary numeric promotion. The promoted
10600 type must be int */
15fdcfe9
PB
10601 index = do_unary_numeric_promotion (index);
10602 if (TREE_TYPE (index) != int_type_node)
e04a16fb 10603 {
b67d701b 10604 int could_cast = valid_cast_to_p (index_type, int_type_node);
e04a16fb
AG
10605 parse_error_context
10606 (wfl_operator,
10607 (could_cast ? "Incompatible type for `[]'. Explicit cast needed to "
10608 "convert `%s' to `int'" : "Incompatible type for `[]'. "
10609 "Can't convert `%s' to `int'"),
0a2138e2 10610 lang_printable_name (index_type, 0));
e04a16fb
AG
10611 TREE_TYPE (node) = error_mark_node;
10612 error_found = 1;
10613 }
10614
e04a16fb
AG
10615 if (error_found)
10616 return error_mark_node;
e04a16fb 10617
5e942c50 10618 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 10619
7f1d4866 10620 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 10621 {
15fdcfe9
PB
10622 TREE_OPERAND (node, 0) = array;
10623 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
10624 }
10625 else
939d7216
PB
10626 {
10627 /* The save_expr is for correct evaluation order. It would be cleaner
10628 to use force_evaluation_order (see comment there), but that is
10629 difficult when we also have to deal with bounds checking. */
10630 if (TREE_SIDE_EFFECTS (index))
10631 array = save_expr (array);
10632 node = build_java_arrayaccess (array, array_type, index);
10633 if (TREE_SIDE_EFFECTS (index))
10634 node = build (COMPOUND_EXPR, array_type, array, node);
10635 }
e04a16fb
AG
10636 TREE_TYPE (node) = array_type;
10637 return node;
10638}
10639
10640/* 15.9 Array Creation Expressions */
10641
10642static tree
10643build_newarray_node (type, dims, extra_dims)
10644 tree type;
10645 tree dims;
10646 int extra_dims;
10647{
10648 tree node =
b67d701b 10649 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 10650 build_int_2 (extra_dims, 0));
e04a16fb
AG
10651 return node;
10652}
10653
10654static tree
10655patch_newarray (node)
10656 tree node;
10657{
10658 tree type = TREE_OPERAND (node, 0);
10659 tree dims = TREE_OPERAND (node, 1);
10660 tree cdim, array_type;
10661 int error_found = 0;
10662 int ndims = 0;
10663 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
10664
10665 /* Dimension types are verified. It's better for the types to be
10666 verified in order. */
10667 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
10668 {
10669 int dim_error = 0;
10670 tree dim = TREE_VALUE (cdim);
10671
10672 /* Dim might have been saved during its evaluation */
10673 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
10674
10675 /* The type of each specified dimension must be an integral type. */
10676 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
10677 dim_error = 1;
10678
10679 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
10680 promoted type must be int. */
10681 else
10682 {
15fdcfe9 10683 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
10684 if (TREE_TYPE (dim) != int_type_node)
10685 dim_error = 1;
10686 }
10687
10688 /* Report errors on types here */
10689 if (dim_error)
10690 {
10691 parse_error_context
10692 (TREE_PURPOSE (cdim),
10693 "Incompatible type for dimension in array creation expression. "
10694 "%s convert `%s' to `int'",
b67d701b 10695 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 10696 "Explicit cast needed to" : "Can't"),
0a2138e2 10697 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
10698 error_found = 1;
10699 }
10700
e04a16fb
AG
10701 TREE_PURPOSE (cdim) = NULL_TREE;
10702 }
10703
10704 /* Resolve array base type if unresolved */
10705 if (!(type = resolve_type_during_patch (type)))
10706 error_found = 1;
10707
10708 if (error_found)
10709 {
10710 /* We don't want further evaluation of this bogus array creation
10711 operation */
10712 TREE_TYPE (node) = error_mark_node;
10713 return error_mark_node;
10714 }
10715
15fdcfe9
PB
10716 /* Set array_type to the actual (promoted) array type of the result. */
10717 if (TREE_CODE (type) == RECORD_TYPE)
10718 type = build_pointer_type (type);
10719 while (--xdims >= 0)
10720 {
10721 type = promote_type (build_java_array_type (type, -1));
10722 }
10723 dims = nreverse (dims);
10724 array_type = type;
10725 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
10726 {
10727 type = array_type;
10728 array_type = build_java_array_type (type,
10729 TREE_CODE (cdim) == INTEGER_CST ?
10730 TREE_INT_CST_LOW (cdim) : -1);
10731 array_type = promote_type (array_type);
10732 }
10733 dims = nreverse (dims);
10734
e04a16fb
AG
10735 /* The node is transformed into a function call. Things are done
10736 differently according to the number of dimensions. If the number
10737 of dimension is equal to 1, then the nature of the base type
10738 (primitive or not) matters. */
15fdcfe9 10739 if (ndims == 1)
fdec99c6 10740 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 10741
e04a16fb
AG
10742 /* Can't reuse what's already written in expr.c because it uses the
10743 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 10744 return build (CALL_EXPR, array_type,
e04a16fb 10745 build_address_of (soft_multianewarray_node),
15fdcfe9 10746 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 10747 tree_cons (NULL_TREE,
15fdcfe9 10748 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
10749 NULL_TREE);
10750}
10751
f8976021
APB
10752/* 10.6 Array initializer. */
10753
10754/* Build a wfl for array element that don't have one, so we can
10755 pin-point errors. */
10756
10757static tree
10758maybe_build_array_element_wfl (node)
10759 tree node;
10760{
10761 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
10762 return build_expr_wfl (NULL_TREE, ctxp->filename,
10763 ctxp->elc.line, ctxp->elc.prev_col);
10764 else
10765 return NULL_TREE;
10766}
10767
10768/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
10769 identification of initialized arrays easier to detect during walk
10770 and expansion. */
10771
10772static tree
10773build_new_array_init (location, values)
10774 int location;
10775 tree values;
10776{
10777 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
10778 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 10779 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
10780 return to_return;
10781}
10782
10783/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
10784 occurred. Otherwise return NODE after having set its type
10785 appropriately. */
10786
10787static tree
10788patch_new_array_init (type, node)
10789 tree type, node;
f8976021
APB
10790{
10791 int error_seen = 0;
fdec99c6 10792 tree current, element_type;
f8976021 10793 HOST_WIDE_INT length;
fdec99c6
PB
10794 int all_constant = 1;
10795 tree init = TREE_OPERAND (node, 0);
f8976021 10796
fdec99c6
PB
10797 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
10798 {
10799 parse_error_context (node,
10800 "Invalid array initializer for non-array type `%s'",
10801 lang_printable_name (type, 1));
10802 return error_mark_node;
10803 }
10804 type = TREE_TYPE (type);
10805 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 10806
fdec99c6
PB
10807 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
10808
10809 for (length = 0, current = CONSTRUCTOR_ELTS (init);
10810 current; length++, current = TREE_CHAIN (current))
f8976021 10811 {
fdec99c6
PB
10812 tree elt = TREE_VALUE (current);
10813 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 10814 {
fdec99c6 10815 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
10816 elt = TREE_VALUE (current);
10817 /* When compiling to native code, STRING_CST is converted to
10818 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
10819 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 10820 all_constant = 0;
f8976021 10821 }
fdec99c6
PB
10822 else
10823 {
10824 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
10825 TREE_PURPOSE (current) = NULL_TREE;
10826 all_constant = 0;
10827 }
10828 if (elt && TREE_VALUE (elt) == error_mark_node)
10829 error_seen = 1;
f8976021
APB
10830 }
10831
10832 if (error_seen)
10833 return error_mark_node;
10834
10835 /* Create a new type. We can't reuse the one we have here by
10836 patching its dimension because it originally is of dimension -1
10837 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
10838 type = build_java_array_type (element_type, length);
10839 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
10840 TREE_TYPE (node) = promote_type (type);
10841 TREE_CONSTANT (init) = all_constant;
bc3ca41b 10842 TREE_CONSTANT (node) = all_constant;
f8976021
APB
10843 return node;
10844}
10845
10846/* Verify that one entry of the initializer element list can be
10847 assigned to the array base type. Report 1 if an error occurred, 0
10848 otherwise. */
10849
10850static int
10851array_constructor_check_entry (type, entry)
10852 tree type, entry;
10853{
10854 char *array_type_string = NULL; /* For error reports */
10855 tree value, type_value, new_value, wfl_value, patched;
10856 int error_seen = 0;
10857
10858 new_value = NULL_TREE;
10859 wfl_value = TREE_VALUE (entry);
10860
f8976021 10861 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 10862 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
10863 if ((patched = patch_string (value)))
10864 value = patched;
1179ebc2
APB
10865 if (value == error_mark_node)
10866 return 1;
f8976021 10867
f8976021
APB
10868 type_value = TREE_TYPE (value);
10869
1179ebc2 10870 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
10871 constant overflow during narrowing. */
10872 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
10873 new_value = try_builtin_assignconv (wfl_operator, type, value);
10874 if (!new_value && (new_value = try_reference_assignconv (type, value)))
10875 type_value = promote_type (type);
10876
10877 /* Check and report errors */
10878 if (!new_value)
10879 {
49f48c71 10880 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
10881 "Can't" : "Explicit cast needed to");
10882 if (!array_type_string)
10883 array_type_string = strdup (lang_printable_name (type, 1));
10884 parse_error_context
10885 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
10886 msg, lang_printable_name (type_value, 1), array_type_string);
10887 error_seen = 1;
10888 }
10889
10890 if (new_value)
10891 {
10892 new_value = maybe_build_primttype_type_ref (new_value, wfl_operator);
10893 TREE_VALUE (entry) = new_value;
10894 }
10895
10896 if (array_type_string)
10897 free (array_type_string);
10898
10899 TREE_PURPOSE (entry) = NULL_TREE;
10900 return error_seen;
10901}
10902
e04a16fb
AG
10903static tree
10904build_this (location)
10905 int location;
10906{
9ee9b555 10907 tree node = build_wfl_node (this_identifier_node);
b67d701b 10908 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
10909 EXPR_WFL_LINECOL (node) = location;
10910 return node;
10911}
10912
10913/* 14.15 The return statement. It builds a modify expression that
10914 assigns the returned value to the RESULT_DECL that hold the value
10915 to be returned. */
10916
10917static tree
10918build_return (location, op)
10919 int location;
10920 tree op;
10921{
10922 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
10923 EXPR_WFL_LINECOL (node) = location;
b67d701b 10924 node = build_debugable_stmt (location, node);
e04a16fb
AG
10925 return node;
10926}
10927
10928static tree
10929patch_return (node)
10930 tree node;
10931{
10932 tree return_exp = TREE_OPERAND (node, 0);
10933 tree meth = current_function_decl;
10934 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
10935 int error_found = 0;
10936
10937 TREE_TYPE (node) = error_mark_node;
10938 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10939
10940 /* It's invalid to have a return value within a function that is
10941 declared with the keyword void or that is a constructor */
10942 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
10943 error_found = 1;
10944
f099f336 10945 /* It's invalid to use a return statement in a static block */
7f1d4866 10946 if (IS_CLINIT (current_function_decl))
f099f336
APB
10947 error_found = 1;
10948
e04a16fb
AG
10949 /* It's invalid to have a no return value within a function that
10950 isn't declared with the keyword `void' */
10951 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
10952 error_found = 2;
10953
10954 if (error_found)
10955 {
7f1d4866 10956 if (IS_CLINIT (current_function_decl))
f099f336
APB
10957 parse_error_context (wfl_operator,
10958 "`return' inside static initializer.");
10959
10960 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6
APB
10961 {
10962 char *t = strdup (lang_printable_name (mtype, 0));
10963 parse_error_context (wfl_operator,
10964 "`return' with%s value from `%s %s'",
10965 (error_found == 1 ? "" : "out"),
10966 t, lang_printable_name (meth, 0));
10967 free (t);
10968 }
10969 else
10970 parse_error_context (wfl_operator,
10971 "`return' with value from constructor `%s'",
10972 lang_printable_name (meth, 0));
e04a16fb
AG
10973 return error_mark_node;
10974 }
10975
5e942c50
APB
10976 /* If we have a return_exp, build a modify expression and expand
10977 it. Note: at that point, the assignment is declared valid, but we
10978 may want to carry some more hacks */
e04a16fb
AG
10979 if (return_exp)
10980 {
5e942c50
APB
10981 tree exp = java_complete_tree (return_exp);
10982 tree modify, patched;
10983
10984 /* If the function returned value and EXP are booleans, EXP has
10985 to be converted into the type of DECL_RESULT, which is integer
10986 (see complete_start_java_method) */
10987 if (TREE_TYPE (exp) == boolean_type_node &&
10988 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
10989 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
10990
10991 /* `null' can be assigned to a function returning a reference */
10992 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
10993 exp == null_pointer_node)
10994 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
10995
10996 if ((patched = patch_string (exp)))
10997 exp = patched;
10998
10999 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
11000 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
11001 modify = java_complete_tree (modify);
5e942c50 11002
e04a16fb
AG
11003 if (modify != error_mark_node)
11004 {
11005 TREE_SIDE_EFFECTS (modify) = 1;
11006 TREE_OPERAND (node, 0) = modify;
11007 }
11008 else
11009 return error_mark_node;
11010 }
11011 TREE_TYPE (node) = void_type_node;
11012 TREE_SIDE_EFFECTS (node) = 1;
11013 return node;
11014}
11015
11016/* 14.8 The if Statement */
11017
11018static tree
11019build_if_else_statement (location, expression, if_body, else_body)
11020 int location;
11021 tree expression, if_body, else_body;
11022{
11023 tree node;
e04a16fb 11024 if (!else_body)
9bbc7d9f 11025 else_body = empty_stmt_node;
e04a16fb
AG
11026 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
11027 EXPR_WFL_LINECOL (node) = location;
b67d701b 11028 node = build_debugable_stmt (location, node);
e04a16fb
AG
11029 return node;
11030}
11031
11032static tree
11033patch_if_else_statement (node)
11034 tree node;
11035{
11036 tree expression = TREE_OPERAND (node, 0);
11037
11038 TREE_TYPE (node) = error_mark_node;
11039 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11040
11041 /* The type of expression must be boolean */
b67d701b
PB
11042 if (TREE_TYPE (expression) != boolean_type_node
11043 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
11044 {
11045 parse_error_context
11046 (wfl_operator,
11047 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 11048 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
11049 return error_mark_node;
11050 }
11051
11052 TREE_TYPE (node) = void_type_node;
11053 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 11054 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
11055 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
11056 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
11057 return node;
11058}
11059
11060/* 14.6 Labeled Statements */
11061
11062/* Action taken when a lableled statement is parsed. a new
11063 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 11064 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
11065
11066static tree
0a2138e2 11067build_labeled_block (location, label)
e04a16fb 11068 int location;
0a2138e2 11069 tree label;
e04a16fb 11070{
b635eb2f 11071 tree label_name ;
e04a16fb 11072 tree label_decl, node;
b635eb2f
PB
11073 if (label == NULL_TREE || label == continue_identifier_node)
11074 label_name = label;
11075 else
e04a16fb 11076 {
b635eb2f
PB
11077 label_name = merge_qualified_name (label_id, label);
11078 /* Issue an error if we try to reuse a label that was previously
11079 declared */
11080 if (IDENTIFIER_LOCAL_VALUE (label_name))
11081 {
11082 EXPR_WFL_LINECOL (wfl_operator) = location;
11083 parse_error_context (wfl_operator, "Declaration of `%s' shadows "
11084 "a previous label declaration",
11085 IDENTIFIER_POINTER (label));
11086 EXPR_WFL_LINECOL (wfl_operator) =
11087 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
11088 parse_error_context (wfl_operator, "This is the location of the "
11089 "previous declaration of label `%s'",
11090 IDENTIFIER_POINTER (label));
11091 java_error_count--;
11092 }
e04a16fb
AG
11093 }
11094
11095 label_decl = create_label_decl (label_name);
11096 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
11097 EXPR_WFL_LINECOL (node) = location;
11098 TREE_SIDE_EFFECTS (node) = 1;
11099 return node;
11100}
11101
b67d701b 11102/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
11103
11104static tree
b635eb2f 11105finish_labeled_statement (lbe, statement)
e04a16fb
AG
11106 tree lbe; /* Labeled block expr */
11107 tree statement;
11108{
11109 /* In anyways, tie the loop to its statement */
11110 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
11111 pop_labeled_block ();
11112 POP_LABELED_BLOCK ();
e04a16fb
AG
11113 return lbe;
11114}
11115
11116/* 14.10, 14.11, 14.12 Loop Statements */
11117
11118/* Create an empty LOOP_EXPR and make it the last in the nested loop
11119 list. */
11120
11121static tree
11122build_new_loop (loop_body)
11123 tree loop_body;
11124{
11125 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
11126 TREE_SIDE_EFFECTS (loop) = 1;
11127 PUSH_LOOP (loop);
11128 return loop;
11129}
11130
11131/* Create a loop body according to the following structure:
11132 COMPOUND_EXPR
11133 COMPOUND_EXPR (loop main body)
11134 EXIT_EXPR (this order is for while/for loops.
11135 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 11136 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
11137 BODY end of this labeled block)
11138 INCREMENT (if any)
11139
11140 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
11141 after the body, like in the do-while loop.
11142
11143 To obtain a loop, the loop body structure described above is
11144 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
11145
11146 LABELED_BLOCK_EXPR
11147 LABEL_DECL (use this label to exit the loop)
11148 LOOP_EXPR
11149 <structure described above> */
e04a16fb
AG
11150
11151static tree
11152build_loop_body (location, condition, reversed)
11153 int location;
11154 tree condition;
11155 int reversed;
11156{
0a2138e2 11157 tree first, second, body;
e04a16fb
AG
11158
11159 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
11160 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
11161 condition = build_debugable_stmt (location, condition);
11162 TREE_SIDE_EFFECTS (condition) = 1;
11163
b635eb2f 11164 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
11165 first = (reversed ? body : condition);
11166 second = (reversed ? condition : body);
11167 return
11168 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 11169 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
11170}
11171
11172/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
11173 their order) on the current loop. Unlink the current loop from the
11174 loop list. */
11175
11176static tree
b635eb2f 11177finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
11178 int location;
11179 tree condition, body;
11180 int reversed;
11181{
11182 tree to_return = ctxp->current_loop;
11183 tree loop_body = LOOP_EXPR_BODY (to_return);
11184 if (condition)
11185 {
11186 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
11187 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
11188 The real EXIT_EXPR is one operand further. */
11189 EXPR_WFL_LINECOL (cnode) = location;
11190 /* This one is for accurate error reports */
11191 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
11192 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
11193 }
11194 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
11195 POP_LOOP ();
11196 return to_return;
11197}
11198
b635eb2f 11199/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
11200 loops feature the condition part */
11201
11202static tree
b635eb2f 11203finish_for_loop (location, condition, update, body)
e04a16fb
AG
11204 int location;
11205 tree condition, update, body;
11206{
11207 /* Put the condition and the loop body in place */
b635eb2f 11208 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
11209 /* LOOP is the current loop which has been now popped of the loop
11210 stack. Install the update block */
11211 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
11212 return loop;
11213}
11214
5cbdba64
APB
11215/* Try to find the loop a block might be related to. This comprises
11216 the case where the LOOP_EXPR is found as the second operand of a
11217 COMPOUND_EXPR, because the loop happens to have an initialization
11218 part, then expressed as the first operand of the COMPOUND_EXPR. If
11219 the search finds something, 1 is returned. Otherwise, 0 is
11220 returned. The search is assumed to start from a
11221 LABELED_BLOCK_EXPR's block. */
11222
11223static tree
11224search_loop (statement)
11225 tree statement;
11226{
11227 if (TREE_CODE (statement) == LOOP_EXPR)
11228 return statement;
11229
11230 if (TREE_CODE (statement) == BLOCK)
11231 statement = BLOCK_SUBBLOCKS (statement);
11232 else
11233 return NULL_TREE;
11234
11235 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
11236 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
11237 statement = TREE_OPERAND (statement, 1);
11238
11239 return (TREE_CODE (statement) == LOOP_EXPR
11240 && IS_FOR_LOOP_P (statement) ? statement : NULL_TREE);
11241}
11242
11243/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
11244 returned otherwise. */
11245
11246static int
11247labeled_block_contains_loop_p (block, loop)
11248 tree block, loop;
11249{
11250 if (!block)
11251 return 0;
11252
11253 if (LABELED_BLOCK_BODY (block) == loop)
11254 return 1;
11255
11256 if (IS_FOR_LOOP_P (loop)
11257 && search_loop (LABELED_BLOCK_BODY (block)) == loop)
11258 return 1;
11259
11260 return 0;
11261}
11262
e04a16fb 11263/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 11264 insert LOOP as its body. */
e04a16fb
AG
11265
11266static tree
11267patch_loop_statement (loop)
11268 tree loop;
11269{
cd9643f7 11270 tree loop_label;
5cbdba64 11271
cd9643f7 11272 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
11273 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
11274 return loop;
11275
cd9643f7 11276 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
11277 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
11278 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
11279 LABELED_BLOCK_BODY (loop_label) = loop;
11280 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 11281 return loop_label;
e04a16fb
AG
11282}
11283
11284/* 14.13, 14.14: break and continue Statements */
11285
11286/* Build a break or a continue statement. a null NAME indicates an
11287 unlabeled break/continue statement. */
11288
11289static tree
11290build_bc_statement (location, is_break, name)
11291 int location, is_break;
11292 tree name;
11293{
11294 tree break_continue, label_block_expr = NULL_TREE;
11295
11296 if (name)
11297 {
11298 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
11299 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
11300 /* Null means that we don't have a target for this named
11301 break/continue. In this case, we make the target to be the
11302 label name, so that the error can be reported accuratly in
11303 patch_bc_statement. */
11304 label_block_expr = EXPR_WFL_NODE (name);
11305 }
11306 /* Unlabeled break/continue will be handled during the
11307 break/continue patch operation */
11308 break_continue
11309 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
11310
11311 IS_BREAK_STMT_P (break_continue) = is_break;
11312 TREE_SIDE_EFFECTS (break_continue) = 1;
11313 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 11314 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
11315 return break_continue;
11316}
11317
11318/* Verification of a break/continue statement. */
11319
11320static tree
11321patch_bc_statement (node)
11322 tree node;
11323{
11324 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 11325 tree labeled_block = ctxp->current_labeled_block;
b67d701b 11326 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 11327
e04a16fb 11328 /* Having an identifier here means that the target is unknown. */
b635eb2f 11329 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
11330 {
11331 parse_error_context (wfl_operator, "No label definition found for `%s'",
11332 IDENTIFIER_POINTER (bc_label));
11333 return error_mark_node;
11334 }
b635eb2f 11335 if (! IS_BREAK_STMT_P (node))
e04a16fb 11336 {
b635eb2f
PB
11337 /* It's a continue statement. */
11338 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 11339 {
b635eb2f
PB
11340 if (labeled_block == NULL_TREE)
11341 {
11342 if (bc_label == NULL_TREE)
11343 parse_error_context (wfl_operator,
11344 "`continue' must be in loop");
11345 else
1504b2b4
APB
11346 parse_error_context
11347 (wfl_operator, "continue label `%s' does not name a loop",
11348 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
11349 return error_mark_node;
11350 }
11351 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
11352 == continue_identifier_node)
11353 && (bc_label == NULL_TREE
11354 || TREE_CHAIN (labeled_block) == bc_label))
11355 {
11356 bc_label = labeled_block;
11357 break;
11358 }
e04a16fb 11359 }
e04a16fb 11360 }
b635eb2f 11361 else if (!bc_label)
34f4db93 11362 {
b635eb2f 11363 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 11364 {
b635eb2f
PB
11365 if (labeled_block == NULL_TREE)
11366 {
11367 parse_error_context (wfl_operator,
11368 "`break' must be in loop or switch");
11369 return error_mark_node;
11370 }
11371 target_stmt = LABELED_BLOCK_BODY (labeled_block);
11372 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 11373 || search_loop (target_stmt))
b635eb2f
PB
11374 {
11375 bc_label = labeled_block;
11376 break;
11377 }
e04a16fb 11378 }
e04a16fb
AG
11379 }
11380
b635eb2f 11381 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
11382 CAN_COMPLETE_NORMALLY (bc_label) = 1;
11383
e04a16fb
AG
11384 /* Our break/continue don't return values. */
11385 TREE_TYPE (node) = void_type_node;
11386 /* Encapsulate the break within a compound statement so that it's
5cbdba64 11387 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
11388 sometimes, like after a if statement) */
11389 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
11390 TREE_SIDE_EFFECTS (node) = 1;
11391 return node;
11392}
11393
11394/* Process the exit expression belonging to a loop. Its type must be
11395 boolean. */
11396
11397static tree
11398patch_exit_expr (node)
11399 tree node;
11400{
11401 tree expression = TREE_OPERAND (node, 0);
11402 TREE_TYPE (node) = error_mark_node;
11403 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11404
11405 /* The type of expression must be boolean */
11406 if (TREE_TYPE (expression) != boolean_type_node)
11407 {
11408 parse_error_context
11409 (wfl_operator,
11410 "Incompatible type for loop conditional. Can't convert `%s' to "
11411 "`boolean'",
0a2138e2 11412 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
11413 return error_mark_node;
11414 }
11415 /* Now we know things are allright, invert the condition, fold and
11416 return */
11417 TREE_OPERAND (node, 0) =
11418 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
11419
11420 if (! integer_zerop (TREE_OPERAND (node, 0))
11421 && ctxp->current_loop != NULL_TREE
11422 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
11423 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
11424 if (! integer_onep (TREE_OPERAND (node, 0)))
11425 CAN_COMPLETE_NORMALLY (node) = 1;
11426
11427
e04a16fb
AG
11428 TREE_TYPE (node) = void_type_node;
11429 return node;
11430}
b67d701b
PB
11431
11432/* 14.9 Switch statement */
11433
11434static tree
11435patch_switch_statement (node)
11436 tree node;
11437{
c877974e 11438 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
11439
11440 /* Complete the switch expression */
11441 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
11442 se_type = TREE_TYPE (se);
11443 /* The type of the switch expression must be char, byte, short or
11444 int */
11445 if (!JINTEGRAL_TYPE_P (se_type))
11446 {
11447 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11448 parse_error_context (wfl_operator, "Incompatible type for `switch'. "
11449 "Can't convert `%s' to `int'",
0a2138e2 11450 lang_printable_name (se_type, 0));
b67d701b
PB
11451 /* This is what java_complete_tree will check */
11452 TREE_OPERAND (node, 0) = error_mark_node;
11453 return error_mark_node;
11454 }
11455
15fdcfe9 11456 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
11457
11458 /* Ready to return */
15fdcfe9 11459 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
11460 {
11461 TREE_TYPE (node) = error_mark_node;
11462 return error_mark_node;
11463 }
11464 TREE_TYPE (node) = void_type_node;
11465 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 11466 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
11467 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
11468 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
11469 return node;
11470}
11471
b67d701b
PB
11472/* 14.18 The try statement */
11473
b67d701b 11474static tree
a7d8d81f 11475build_try_statement (location, try_block, catches)
b67d701b 11476 int location;
a7d8d81f
PB
11477 tree try_block, catches;
11478{
11479 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 11480 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 11481 return node;
b67d701b
PB
11482}
11483
a7d8d81f
PB
11484static tree
11485build_try_finally_statement (location, try_block, finally)
11486 int location;
11487 tree try_block, finally;
b67d701b 11488{
a7d8d81f
PB
11489 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
11490 EXPR_WFL_LINECOL (node) = location;
11491 return node;
b67d701b
PB
11492}
11493
11494static tree
11495patch_try_statement (node)
11496 tree node;
11497{
11498 int error_found = 0;
11499 tree try = TREE_OPERAND (node, 0);
11500 /* Exception handlers are considered in left to right order */
11501 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 11502 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
11503
11504 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
11505 to process the next catch clause. We process the catch clause before
11506 the try block so that when processing the try block we can check thrown
11507 exceptions againts the caught type list. */
b67d701b
PB
11508 for (current = catch; current; current = TREE_CHAIN (current))
11509 {
11510 tree carg_decl, carg_type;
11511 tree sub_current, catch_block, catch_clause;
11512 int unreachable;
11513
b67d701b 11514 /* At this point, the structure of the catch clause is
b67d701b
PB
11515 CATCH_EXPR (catch node)
11516 BLOCK (with the decl of the parameter)
11517 COMPOUND_EXPR
7525cc04 11518 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 11519 BLOCK (catch clause block)
a7d8d81f
PB
11520 */
11521 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
11522 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
11523 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
11524
11525 /* Catch clauses can't have more than one parameter declared,
11526 but it's already enforced by the grammar. Make sure that the
11527 only parameter of the clause statement in of class Throwable
11528 or a subclass of Throwable, but that was done earlier. The
11529 catch clause parameter type has also been resolved. */
11530
11531 /* Just make sure that the catch clause parameter type inherits
11532 from java.lang.Throwable */
11533 if (!inherits_from_p (carg_type, throwable_type_node))
11534 {
11535 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
11536 parse_error_context (wfl_operator,
11537 "Can't catch class `%s'. Catch clause "
11538 "parameter type must be a subclass of "
11539 "class `java.lang.Throwable'",
0a2138e2 11540 lang_printable_name (carg_type, 0));
b67d701b
PB
11541 error_found = 1;
11542 continue;
11543 }
11544
11545 /* Partial check for unreachable catch statement: The catch
11546 clause is reachable iff is no earlier catch block A in
11547 the try statement such that the type of the catch
11548 clause's parameter is the same as or a subclass of the
11549 type of A's parameter */
11550 unreachable = 0;
11551 for (sub_current = catch;
11552 sub_current != current; sub_current = TREE_CHAIN (sub_current))
11553 {
11554 tree sub_catch_clause, decl;
a7d8d81f 11555 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
11556 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
11557
11558 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
11559 {
11560 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
11561 parse_error_context
11562 (wfl_operator, "`catch' not reached because of the catch "
11563 "clause at line %d", EXPR_WFL_LINENO (sub_current));
11564 unreachable = error_found = 1;
11565 break;
11566 }
11567 }
b67d701b
PB
11568 /* Complete the catch clause block */
11569 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
11570 if (catch_block == error_mark_node)
11571 {
11572 error_found = 1;
11573 continue;
11574 }
15fdcfe9
PB
11575 if (CAN_COMPLETE_NORMALLY (catch_block))
11576 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11577 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
11578
11579 if (unreachable)
11580 continue;
11581
11582 /* Things to do here: the exception must be thrown */
11583
11584 /* Link this type to the caught type list */
11585 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
11586 }
11587
b9f7e36c
APB
11588 PUSH_EXCEPTIONS (caught_type_list);
11589 if ((try = java_complete_tree (try)) == error_mark_node)
11590 error_found = 1;
15fdcfe9
PB
11591 if (CAN_COMPLETE_NORMALLY (try))
11592 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
11593 POP_EXCEPTIONS ();
11594
b67d701b
PB
11595 /* Verification ends here */
11596 if (error_found)
11597 return error_mark_node;
11598
11599 TREE_OPERAND (node, 0) = try;
11600 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
11601 TREE_TYPE (node) = void_type_node;
11602 return node;
11603}
b9f7e36c
APB
11604
11605/* 14.17 The synchronized Statement */
11606
11607static tree
11608patch_synchronized_statement (node, wfl_op1)
11609 tree node, wfl_op1;
11610{
5a005d9e 11611 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 11612 tree block = TREE_OPERAND (node, 1);
5a005d9e 11613
d8fccff5 11614 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
11615
11616 if (expr == error_mark_node)
11617 {
11618 block = java_complete_tree (block);
11619 return expr;
11620 }
b9f7e36c
APB
11621
11622 /* The TYPE of expr must be a reference type */
5a005d9e 11623 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
11624 {
11625 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
11626 parse_error_context (wfl_operator, "Incompatible type for `synchronized'"
11627 ". Can't convert `%s' to `java.lang.Object'",
0a2138e2 11628 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
11629 return error_mark_node;
11630 }
11631
ce6e9147
APB
11632 if (flag_emit_xref)
11633 {
11634 TREE_OPERAND (node, 0) = expr;
11635 TREE_OPERAND (node, 1) = java_complete_tree (block);
11636 CAN_COMPLETE_NORMALLY (node) = 1;
11637 return node;
11638 }
11639
b9f7e36c
APB
11640 /* Generate a try-finally for the synchronized statement, except
11641 that the handler that catches all throw exception calls
11642 _Jv_MonitorExit and then rethrow the exception.
11643 The synchronized statement is then implemented as:
11644 TRY
11645 {
11646 _Jv_MonitorEnter (expression)
11647 synchronized_block
11648 _Jv_MonitorExit (expression)
11649 }
11650 CATCH_ALL
11651 {
11652 e = _Jv_exception_info ();
11653 _Jv_MonitorExit (expression)
11654 Throw (e);
11655 } */
11656
5a005d9e
PB
11657 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
11658 BUILD_MONITOR_ENTER (enter, expr_decl);
11659 BUILD_MONITOR_EXIT (exit, expr_decl);
11660 CAN_COMPLETE_NORMALLY (enter) = 1;
11661 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
11662 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
11663 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
11664 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
11665 build (COMPOUND_EXPR, NULL_TREE,
11666 build (WITH_CLEANUP_EXPR, NULL_TREE,
11667 build (COMPOUND_EXPR, NULL_TREE,
96847892 11668 assignment, enter),
5a005d9e
PB
11669 NULL_TREE, exit),
11670 block));
11671 node = build_expr_block (node, expr_decl);
11672
11673 return java_complete_tree (node);
b9f7e36c
APB
11674}
11675
11676/* 14.16 The throw Statement */
11677
11678static tree
11679patch_throw_statement (node, wfl_op1)
11680 tree node, wfl_op1;
11681{
11682 tree expr = TREE_OPERAND (node, 0);
11683 tree type = TREE_TYPE (expr);
11684 int unchecked_ok = 0, tryblock_throws_ok = 0;
11685
11686 /* Thrown expression must be assignable to java.lang.Throwable */
11687 if (!try_reference_assignconv (throwable_type_node, expr))
11688 {
11689 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
11690 parse_error_context (wfl_operator, "Can't throw `%s'; it must be a "
11691 "subclass of class `java.lang.Throwable'",
0a2138e2 11692 lang_printable_name (type, 0));
b9f7e36c
APB
11693 /* If the thrown expression was a reference, we further the
11694 compile-time check. */
11695 if (!JREFERENCE_TYPE_P (type))
11696 return error_mark_node;
11697 }
11698
11699 /* At least one of the following must be true */
11700
11701 /* The type of the throw expression is a not checked exception,
11702 i.e. is a unchecked expression. */
c877974e 11703 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c
APB
11704
11705 /* Throw is contained in a try statement and at least one catch
11706 clause can receive the thrown expression or the current method is
11707 declared to throw such an exception. Or, the throw statement is
11708 contained in a method or constructor declaration and the type of
11709 the Expression is assignable to at least one type listed in the
11710 throws clause the declaration. */
11711 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
11712 if (!unchecked_ok)
f099f336 11713 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
11714 if (!(unchecked_ok || tryblock_throws_ok))
11715 {
11716 /* If there is a surrounding try block that has no matching
11717 clatch clause, report it first. A surrounding try block exits
11718 only if there is something after the list of checked
11719 exception thrown by the current function (if any). */
11720 if (IN_TRY_BLOCK_P ())
11721 parse_error_context (wfl_operator, "Checked exception `%s' can't be "
11722 "caught by any of the catch clause(s) "
11723 "of the surrounding `try' block",
0a2138e2 11724 lang_printable_name (type, 0));
b9f7e36c
APB
11725 /* If we have no surrounding try statement and the method doesn't have
11726 any throws, report it now. FIXME */
f099f336
APB
11727
11728 /* We report that the exception can't be throw from a try block
11729 in all circumstances but when the `throw' is inside a static
11730 block. */
b9f7e36c
APB
11731 else if (!EXCEPTIONS_P (currently_caught_type_list)
11732 && !tryblock_throws_ok)
f099f336 11733 {
7f1d4866 11734 if (IS_CLINIT (current_function_decl))
f099f336
APB
11735 parse_error_context (wfl_operator, "Checked exception `%s' can't "
11736 "be thrown in initializer",
11737 lang_printable_name (type, 0));
11738 else
11739 parse_error_context (wfl_operator, "Checked exception `%s' isn't "
11740 "thrown from a `try' block",
11741 lang_printable_name (type, 0));
11742 }
b9f7e36c
APB
11743 /* Otherwise, the current method doesn't have the appropriate
11744 throws declaration */
11745 else
11746 parse_error_context (wfl_operator, "Checked exception `%s' doesn't "
11747 "match any of current method's `throws' "
11748 "declaration(s)",
0a2138e2 11749 lang_printable_name (type, 0));
b9f7e36c
APB
11750 return error_mark_node;
11751 }
11752
ce6e9147 11753 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 11754 BUILD_THROW (node, expr);
ce6e9147
APB
11755
11756 /* If doing xrefs, keep the location where the `throw' was seen. */
11757 if (flag_emit_xref)
11758 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
11759 return node;
11760}
11761
11762/* Check that exception said to be thrown by method DECL can be
11763 effectively caught from where DECL is invoked. */
11764
11765static void
11766check_thrown_exceptions (location, decl)
11767 int location;
11768 tree decl;
11769{
11770 tree throws;
11771 /* For all the unchecked exceptions thrown by DECL */
11772 for (throws = DECL_FUNCTION_THROWS (decl); throws;
11773 throws = TREE_CHAIN (throws))
0a2138e2 11774 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 11775 {
3e78f871
PB
11776#if 1
11777 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
11778 if (DECL_NAME (decl) == get_identifier ("clone"))
11779 continue;
11780#endif
b9f7e36c 11781 EXPR_WFL_LINECOL (wfl_operator) = location;
7705e9db
APB
11782 if (DECL_NAME (current_function_decl) == finit_identifier_node)
11783 parse_error_context
11784 (wfl_operator, "Exception `%s' can't be thrown in initializer",
11785 lang_printable_name (TREE_VALUE (throws), 0));
11786 else
11787 {
11788 parse_error_context
11789 (wfl_operator, "Exception `%s' must be caught, or it must be "
11790 "declared in the `throws' clause of `%s'",
11791 lang_printable_name (TREE_VALUE (throws), 0),
11792 (DECL_NAME (current_function_decl) == init_identifier_node ?
11793 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
11794 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
11795 }
b9f7e36c
APB
11796 }
11797}
11798
c877974e 11799/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
11800 try-catch blocks, OR is listed in the `throws' clause of the
11801 current method. */
11802
11803static int
0a2138e2 11804check_thrown_exceptions_do (exception)
b9f7e36c
APB
11805 tree exception;
11806{
11807 tree list = currently_caught_type_list;
c877974e 11808 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
11809 /* First, all the nested try-catch-finally at that stage. The
11810 last element contains `throws' clause exceptions, if any. */
c877974e
APB
11811 if (IS_UNCHECKED_EXCEPTION_P (exception))
11812 return 1;
b9f7e36c
APB
11813 while (list)
11814 {
11815 tree caught;
11816 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
11817 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
11818 return 1;
11819 list = TREE_CHAIN (list);
11820 }
11821 return 0;
11822}
11823
11824static void
11825purge_unchecked_exceptions (mdecl)
11826 tree mdecl;
11827{
11828 tree throws = DECL_FUNCTION_THROWS (mdecl);
11829 tree new = NULL_TREE;
11830
11831 while (throws)
11832 {
11833 tree next = TREE_CHAIN (throws);
c877974e 11834 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
11835 {
11836 TREE_CHAIN (throws) = new;
11837 new = throws;
11838 }
11839 throws = next;
11840 }
11841 /* List is inverted here, but it doesn't matter */
11842 DECL_FUNCTION_THROWS (mdecl) = new;
11843}
22eed1e6
APB
11844
11845/* 15.24 Conditional Operator ?: */
11846
11847static tree
11848patch_conditional_expr (node, wfl_cond, wfl_op1)
11849 tree node, wfl_cond, wfl_op1;
11850{
11851 tree cond = TREE_OPERAND (node, 0);
11852 tree op1 = TREE_OPERAND (node, 1);
11853 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 11854 tree resulting_type = NULL_TREE;
ac825856 11855 tree t1, t2, patched;
22eed1e6
APB
11856 int error_found = 0;
11857
ac825856
APB
11858 /* Operands of ?: might be StringBuffers crafted as a result of a
11859 string concatenation. Obtain a descent operand here. */
11860 if ((patched = patch_string (op1)))
11861 TREE_OPERAND (node, 1) = op1 = patched;
11862 if ((patched = patch_string (op2)))
11863 TREE_OPERAND (node, 2) = op2 = patched;
11864
11865 t1 = TREE_TYPE (op1);
11866 t2 = TREE_TYPE (op2);
11867
22eed1e6
APB
11868 /* The first expression must be a boolean */
11869 if (TREE_TYPE (cond) != boolean_type_node)
11870 {
11871 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
11872 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11873 "convert `%s' to `boolean'",
11874 lang_printable_name (TREE_TYPE (cond), 0));
11875 error_found = 1;
11876 }
11877
11878 /* Second and third can be numeric, boolean (i.e. primitive),
11879 references or null. Anything else results in an error */
11880 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
11881 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
11882 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
11883 || (t1 == boolean_type_node && t2 == boolean_type_node)))
11884 error_found = 1;
11885
11886 /* Determine the type of the conditional expression. Same types are
11887 easy to deal with */
11888 else if (t1 == t2)
11889 resulting_type = t1;
11890
11891 /* There are different rules for numeric types */
11892 else if (JNUMERIC_TYPE_P (t1))
11893 {
11894 /* if byte/short found, the resulting type is short */
11895 if ((t1 == byte_type_node && t2 == short_type_node)
11896 || (t1 == short_type_node && t2 == byte_type_node))
11897 resulting_type = short_type_node;
11898
11899 /* If t1 is a constant int and t2 is of type byte, short or char
11900 and t1's value fits in t2, then the resulting type is t2 */
11901 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
11902 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
11903 resulting_type = t2;
11904
11905 /* If t2 is a constant int and t1 is of type byte, short or char
11906 and t2's value fits in t1, then the resulting type is t1 */
11907 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
11908 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
11909 resulting_type = t1;
11910
11911 /* Otherwise, binary numeric promotion is applied and the
11912 resulting type is the promoted type of operand 1 and 2 */
11913 else
93024893 11914 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
11915 &TREE_OPERAND (node, 1),
11916 &TREE_OPERAND (node, 2));
11917 }
11918
11919 /* Cases of a reference and a null type */
11920 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
11921 resulting_type = t1;
11922
11923 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
11924 resulting_type = t2;
11925
11926 /* Last case: different reference types. If a type can be converted
11927 into the other one by assignment conversion, the latter
11928 determines the type of the expression */
11929 else if ((resulting_type = try_reference_assignconv (t1, op2)))
11930 resulting_type = promote_type (t1);
11931
11932 else if ((resulting_type = try_reference_assignconv (t2, op1)))
11933 resulting_type = promote_type (t2);
11934
11935 /* If we don't have any resulting type, we're in trouble */
11936 if (!resulting_type)
11937 {
11938 char *t = strdup (lang_printable_name (t1, 0));
11939 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
11940 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11941 "convert `%s' to `%s'", t,
11942 lang_printable_name (t2, 0));
11943 free (t);
11944 error_found = 1;
11945 }
11946
11947 if (error_found)
11948 {
11949 TREE_TYPE (node) = error_mark_node;
11950 return error_mark_node;
11951 }
11952
11953 TREE_TYPE (node) = resulting_type;
11954 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 11955 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
11956 return node;
11957}
ac825856 11958
5b09b33e
PB
11959/* Try to constant fold NODE.
11960 If NODE is not a constant expression, return NULL_EXPR.
11961 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
11962
11963static tree
11964fold_constant_for_init (node, context)
11965 tree node;
11966 tree context;
11967{
11968 tree op0, op1, val;
11969 enum tree_code code = TREE_CODE (node);
11970
93024893 11971 if (code == STRING_CST)
5b09b33e 11972 return node;
93024893
APB
11973
11974 if (code == INTEGER_CST || code == REAL_CST)
11975 return convert (TREE_TYPE (context), node);
8576f094 11976 if (TREE_TYPE (node) != NULL_TREE && code != VAR_DECL && code != FIELD_DECL)
5b09b33e
PB
11977 return NULL_TREE;
11978
11979 switch (code)
11980 {
5b09b33e
PB
11981 case PLUS_EXPR:
11982 case MINUS_EXPR:
bc3ca41b
PB
11983 case MULT_EXPR:
11984 case TRUNC_MOD_EXPR:
11985 case RDIV_EXPR:
5b09b33e
PB
11986 case LSHIFT_EXPR:
11987 case RSHIFT_EXPR:
11988 case URSHIFT_EXPR:
11989 case BIT_AND_EXPR:
11990 case BIT_XOR_EXPR:
11991 case BIT_IOR_EXPR:
5b09b33e
PB
11992 case TRUTH_ANDIF_EXPR:
11993 case TRUTH_ORIF_EXPR:
11994 case EQ_EXPR:
11995 case NE_EXPR:
11996 case GT_EXPR:
11997 case GE_EXPR:
11998 case LT_EXPR:
11999 case LE_EXPR:
12000 op0 = TREE_OPERAND (node, 0);
12001 op1 = TREE_OPERAND (node, 1);
12002 val = fold_constant_for_init (op0, context);
12003 if (val == NULL_TREE || ! TREE_CONSTANT (val))
12004 return NULL_TREE;
12005 TREE_OPERAND (node, 0) = val;
12006 val = fold_constant_for_init (op1, context);
12007 if (val == NULL_TREE || ! TREE_CONSTANT (val))
12008 return NULL_TREE;
12009 TREE_OPERAND (node, 1) = val;
12010 return patch_binop (node, op0, op1);
12011
12012 case UNARY_PLUS_EXPR:
12013 case NEGATE_EXPR:
12014 case TRUTH_NOT_EXPR:
12015 case BIT_NOT_EXPR:
12016 case CONVERT_EXPR:
12017 op0 = TREE_OPERAND (node, 0);
12018 val = fold_constant_for_init (op0, context);
12019 if (val == NULL_TREE || ! TREE_CONSTANT (val))
12020 return NULL_TREE;
12021 TREE_OPERAND (node, 0) = val;
5a005d9e 12022 return patch_unaryop (node, op0);
5b09b33e
PB
12023 break;
12024
12025 case COND_EXPR:
12026 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
12027 if (val == NULL_TREE || ! TREE_CONSTANT (val))
12028 return NULL_TREE;
12029 TREE_OPERAND (node, 0) = val;
12030 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
12031 if (val == NULL_TREE || ! TREE_CONSTANT (val))
12032 return NULL_TREE;
12033 TREE_OPERAND (node, 1) = val;
12034 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
12035 if (val == NULL_TREE || ! TREE_CONSTANT (val))
12036 return NULL_TREE;
12037 TREE_OPERAND (node, 2) = val;
12038 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
12039 : TREE_OPERAND (node, 2);
12040
12041 case VAR_DECL:
8576f094
APB
12042 case FIELD_DECL:
12043 if (! FIELD_FINAL (node)
5b09b33e
PB
12044 || DECL_INITIAL (node) == NULL_TREE)
12045 return NULL_TREE;
12046 val = DECL_INITIAL (node);
12047 /* Guard against infinite recursion. */
12048 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 12049 val = fold_constant_for_init (val, node);
5b09b33e
PB
12050 DECL_INITIAL (node) = val;
12051 return val;
12052
12053 case EXPR_WITH_FILE_LOCATION:
12054 /* Compare java_complete_tree and resolve_expression_name. */
12055 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
12056 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
12057 {
12058 tree name = EXPR_WFL_NODE (node);
12059 tree decl;
12060 if (PRIMARY_P (node))
12061 return NULL_TREE;
12062 else if (! QUALIFIED_P (name))
12063 {
12064 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
12065 if (decl == NULL_TREE
12066 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
12067 return NULL_TREE;
12068 return fold_constant_for_init (decl, decl);
12069 }
12070 else
12071 {
5b09b33e
PB
12072 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
12073 qualify_ambiguous_name (node);
12074 if (resolve_field_access (node, &decl, NULL)
12075 && decl != NULL_TREE)
12076 return fold_constant_for_init (decl, decl);
5b09b33e
PB
12077 return NULL_TREE;
12078 }
12079 }
12080 else
12081 {
12082 op0 = TREE_OPERAND (node, 0);
12083 val = fold_constant_for_init (op0, context);
12084 if (val == NULL_TREE || ! TREE_CONSTANT (val))
12085 return NULL_TREE;
12086 TREE_OPERAND (node, 0) = val;
12087 return val;
12088 }
12089
bc3ca41b
PB
12090#ifdef USE_COMPONENT_REF
12091 case IDENTIFIER:
12092 case COMPONENT_REF:
12093 ?;
12094#endif
12095
5b09b33e
PB
12096 default:
12097 return NULL_TREE;
12098 }
12099}
bc3ca41b
PB
12100
12101#ifdef USE_COMPONENT_REF
12102/* Context is 'T' for TypeName, 'P' for PackageName,
12103 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
12104
12105tree
12106resolve_simple_name (name, context)
12107 tree name;
12108 int context;
12109{
12110}
12111
12112tree
12113resolve_qualified_name (name, context)
12114 tree name;
12115 int context;
12116{
12117}
12118#endif
This page took 1.650824 seconds and 5 git commands to generate.