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