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