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