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