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