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