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