1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27 /* This file parses java source code and issues a tree node image
28 suitable for code generation (byte code and targeted CPU assembly
31 The grammar conforms to the Java grammar described in "The Java(TM)
32 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
33 1996, ISBN 0-201-63451-1"
35 The following modifications were brought to the original grammar:
37 method_body: added the rule '| block SC_TK'
38 static_initializer: added the rule 'static block SC_TK'.
40 Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
43 statement_nsi: 'nsi' should be read no_short_if.
45 Some rules have been modified to support JDK1.1 inner classes
46 definitions and other extensions. */
57 #include "java-tree.h"
66 #define DIR_SEPARATOR '/'
69 /* Local function prototypes */
70 static char *java_accstring_lookup PROTO ((int));
71 static void classitf_redefinition_error PROTO ((char *,tree, tree, tree));
72 static void variable_redefinition_error PROTO ((tree, tree, tree, int));
73 static void check_modifiers PROTO ((char *, int, int));
74 static tree create_class PROTO ((int, tree, tree, tree));
75 static tree create_interface PROTO ((int, tree, tree));
76 static tree find_field PROTO ((tree, tree));
77 static tree lookup_field_wrapper PROTO ((tree, tree));
78 static int duplicate_declaration_error_p PROTO ((tree, tree, tree));
79 static void register_fields PROTO ((int, tree, tree));
80 static tree parser_qualified_classname PROTO ((tree));
81 static int parser_check_super PROTO ((tree, tree, tree));
82 static int parser_check_super_interface PROTO ((tree, tree, tree));
83 static void check_modifiers_consistency PROTO ((int));
84 static tree lookup_cl PROTO ((tree));
85 static tree lookup_java_method2 PROTO ((tree, tree, int));
86 static tree method_header PROTO ((int, tree, tree, tree));
87 static void fix_method_argument_names PROTO ((tree ,tree));
88 static tree method_declarator PROTO ((tree, tree));
89 static void parse_warning_context PVPROTO ((tree cl, const char *msg, ...))
91 static void issue_warning_error_from_context PROTO ((tree, const char *msg, va_list));
92 static tree parse_jdk1_1_error PROTO ((char *));
93 static void complete_class_report_errors PROTO ((jdep *));
94 static int process_imports PROTO ((void));
95 static void read_import_dir PROTO ((tree));
96 static int find_in_imports_on_demand PROTO ((tree));
97 static int find_in_imports PROTO ((tree));
98 static int check_pkg_class_access PROTO ((tree, tree));
99 static tree resolve_package PROTO ((tree, tree *));
100 static tree lookup_package_type PROTO ((char *, int));
101 static tree resolve_class PROTO ((tree, tree, tree));
102 static tree do_resolve_class PROTO ((tree, tree, tree));
103 static void declare_local_variables PROTO ((int, tree, tree));
104 static void source_start_java_method PROTO ((tree));
105 static void source_end_java_method PROTO ((void));
106 static void expand_start_java_method PROTO ((tree));
107 static tree find_name_in_single_imports PROTO ((tree));
108 static void check_abstract_method_header PROTO ((tree));
109 static tree lookup_java_interface_method2 PROTO ((tree, tree));
110 static tree resolve_expression_name PROTO ((tree, tree *));
111 static tree maybe_create_class_interface_decl PROTO ((tree, tree, tree));
112 static int check_class_interface_creation PROTO ((int, int, tree,
114 static tree patch_method_invocation PROTO ((tree, tree, tree,
116 static int breakdown_qualified PROTO ((tree *, tree *, tree));
117 static tree resolve_and_layout PROTO ((tree, tree));
118 static tree resolve_no_layout PROTO ((tree, tree));
119 static int invocation_mode PROTO ((tree, int));
120 static tree find_applicable_accessible_methods_list PROTO ((int, tree,
122 static tree find_most_specific_methods_list PROTO ((tree));
123 static int argument_types_convertible PROTO ((tree, tree));
124 static tree patch_invoke PROTO ((tree, tree, tree));
125 static tree lookup_method_invoke PROTO ((int, tree, tree, tree, tree));
126 static tree register_incomplete_type PROTO ((int, tree, tree, tree));
127 static tree obtain_incomplete_type PROTO ((tree));
128 static tree java_complete_lhs PROTO ((tree));
129 static tree java_complete_tree PROTO ((tree));
130 static void java_complete_expand_method PROTO ((tree));
131 static int unresolved_type_p PROTO ((tree, tree *));
132 static void create_jdep_list PROTO ((struct parser_ctxt *));
133 static tree build_expr_block PROTO ((tree, tree));
134 static tree enter_block PROTO ((void));
135 static tree enter_a_block PROTO ((tree));
136 static tree exit_block PROTO ((void));
137 static tree lookup_name_in_blocks PROTO ((tree));
138 static void maybe_absorb_scoping_blocks PROTO ((void));
139 static tree build_method_invocation PROTO ((tree, tree));
140 static tree build_new_invocation PROTO ((tree, tree));
141 static tree build_assignment PROTO ((int, int, tree, tree));
142 static tree build_binop PROTO ((enum tree_code, int, tree, tree));
143 static int check_final_assignment PROTO ((tree ,tree));
144 static tree patch_assignment PROTO ((tree, tree, tree ));
145 static tree patch_binop PROTO ((tree, tree, tree));
146 static tree build_unaryop PROTO ((int, int, tree));
147 static tree build_incdec PROTO ((int, int, tree, int));
148 static tree patch_unaryop PROTO ((tree, tree));
149 static tree build_cast PROTO ((int, tree, tree));
150 static tree build_null_of_type PROTO ((tree));
151 static tree patch_cast PROTO ((tree, tree));
152 static int valid_ref_assignconv_cast_p PROTO ((tree, tree, int));
153 static int valid_builtin_assignconv_identity_widening_p PROTO ((tree, tree));
154 static int valid_cast_to_p PROTO ((tree, tree));
155 static int valid_method_invocation_conversion_p PROTO ((tree, tree));
156 static tree try_builtin_assignconv PROTO ((tree, tree, tree));
157 static tree try_reference_assignconv PROTO ((tree, tree));
158 static tree build_unresolved_array_type PROTO ((tree));
159 static tree build_array_from_name PROTO ((tree, tree, tree, tree *));
160 static tree build_array_ref PROTO ((int, tree, tree));
161 static tree patch_array_ref PROTO ((tree));
162 static tree make_qualified_name PROTO ((tree, tree, int));
163 static tree merge_qualified_name PROTO ((tree, tree));
164 static tree make_qualified_primary PROTO ((tree, tree, int));
165 static int resolve_qualified_expression_name PROTO ((tree, tree *,
167 static void qualify_ambiguous_name PROTO ((tree));
168 static void maybe_generate_clinit PROTO ((void));
169 static tree resolve_field_access PROTO ((tree, tree *, tree *));
170 static tree build_newarray_node PROTO ((tree, tree, int));
171 static tree patch_newarray PROTO ((tree));
172 static tree resolve_type_during_patch PROTO ((tree));
173 static tree build_this PROTO ((int));
174 static tree build_return PROTO ((int, tree));
175 static tree patch_return PROTO ((tree));
176 static tree maybe_access_field PROTO ((tree, tree, tree));
177 static int complete_function_arguments PROTO ((tree));
178 static int check_for_static_method_reference PROTO ((tree, tree, tree, tree, tree));
179 static int not_accessible_p PROTO ((tree, tree, int));
180 static void check_deprecation PROTO ((tree, tree));
181 static int class_in_current_package PROTO ((tree));
182 static tree build_if_else_statement PROTO ((int, tree, tree, tree));
183 static tree patch_if_else_statement PROTO ((tree));
184 static tree add_stmt_to_compound PROTO ((tree, tree, tree));
185 static tree add_stmt_to_block PROTO ((tree, tree, tree));
186 static tree patch_exit_expr PROTO ((tree));
187 static tree build_labeled_block PROTO ((int, tree));
188 static tree generate_labeled_block PROTO (());
189 static tree complete_labeled_statement PROTO ((tree, tree));
190 static tree build_bc_statement PROTO ((int, int, tree));
191 static tree patch_bc_statement PROTO ((tree));
192 static tree patch_loop_statement PROTO ((tree));
193 static tree build_new_loop PROTO ((tree));
194 static tree build_loop_body PROTO ((int, tree, int));
195 static tree complete_loop_body PROTO ((int, tree, tree, int));
196 static tree build_debugable_stmt PROTO ((int, tree));
197 static tree complete_for_loop PROTO ((int, tree, tree, tree));
198 static tree patch_switch_statement PROTO ((tree));
199 static tree string_constant_concatenation PROTO ((tree, tree));
200 static tree build_string_concatenation PROTO ((tree, tree));
201 static tree patch_string_cst PROTO ((tree));
202 static tree patch_string PROTO ((tree));
203 static tree build_jump_to_finally PROTO ((tree, tree, tree, tree));
204 static tree build_try_statement PROTO ((int, tree, tree));
205 static tree build_try_finally_statement PROTO ((int, tree, tree));
206 static tree patch_try_statement PROTO ((tree));
207 static tree patch_synchronized_statement PROTO ((tree, tree));
208 static tree patch_throw_statement PROTO ((tree, tree));
209 static void check_thrown_exceptions PROTO ((int, tree));
210 static int check_thrown_exceptions_do PROTO ((tree));
211 static void purge_unchecked_exceptions PROTO ((tree));
212 static void check_throws_clauses PROTO ((tree, tree, tree));
213 static void complete_method_declaration PROTO ((tree));
214 static tree build_super_invocation PROTO (());
215 static int verify_constructor_circularity PROTO ((tree, tree));
216 static char *constructor_circularity_msg PROTO ((tree, tree));
217 static tree build_this_super_qualified_invocation PROTO ((int, tree, tree,
219 static char *get_printable_method_name PROTO ((tree));
220 static tree patch_conditional_expr PROTO ((tree, tree, tree));
221 static void maybe_generate_finit PROTO (());
222 static void fix_constructors PROTO ((tree));
223 static int verify_constructor_super PROTO (());
224 static tree create_artificial_method PROTO ((tree, int, tree, tree, tree));
225 static void start_artificial_method_body PROTO ((tree));
226 static void end_artificial_method_body PROTO ((tree));
227 static int check_method_redefinition PROTO ((tree, tree));
228 static int reset_method_name PROTO ((tree));
229 static void java_check_regular_methods PROTO ((tree));
230 static void java_check_abstract_methods PROTO ((tree));
231 static tree maybe_build_primttype_type_ref PROTO ((tree, tree));
232 static void unreachable_stmt_error PROTO ((tree));
233 static tree find_expr_with_wfl PROTO ((tree));
234 static void missing_return_error PROTO ((tree));
235 static tree build_new_array_init PROTO ((int, tree));
236 static tree patch_new_array_init PROTO ((tree, tree));
237 static tree maybe_build_array_element_wfl PROTO ((tree));
238 static int array_constructor_check_entry PROTO ((tree, tree));
239 static char *purify_type_name PROTO ((char *));
240 static tree patch_initialized_static_field PROTO ((tree));
241 static tree fold_constant_for_init PROTO ((tree, tree));
242 static tree strip_out_static_field_access_decl PROTO ((tree));
244 /* Number of error found so far. */
245 int java_error_count;
246 /* Number of warning found so far. */
247 int java_warning_count;
249 /* The current parser context */
250 static struct parser_ctxt *ctxp;
252 /* List of things that were anlyzed for which code will be generated */
253 static struct parser_ctxt *ctxp_for_generation = NULL;
255 /* binop_lookup maps token to tree_code. It is used where binary
256 operations are involved and required by the parser. RDIV_EXPR
257 covers both integral/floating point division. The code is changed
258 once the type of both operator is worked out. */
260 static enum tree_code binop_lookup[19] =
262 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
263 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
264 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
265 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
266 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
268 #define BINOP_LOOKUP(VALUE) \
269 binop_lookup [((VALUE) - PLUS_TK)% \
270 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
272 /* Fake WFL used to report error message. It is initialized once if
273 needed and reused with it's location information is overriden. */
274 tree wfl_operator = NULL_TREE;
276 /* The "$L" identifier we use to create labels. */
277 static tree label_id = NULL_TREE;
279 /* The "StringBuffer" identifier used for the String `+' operator. */
280 static tree wfl_string_buffer = NULL_TREE;
282 /* The "append" identifier used for String `+' operator. */
283 static tree wfl_append = NULL_TREE;
285 /* The "toString" identifier used for String `+' operator. */
286 static tree wfl_to_string = NULL_TREE;
288 /* The "java.lang" import qualified name. */
289 static tree java_lang_id = NULL_TREE;
291 /* The "java.lang.Cloneable" qualified name. */
292 static tree java_lang_cloneable = NULL_TREE;
307 /* Things defined here have to match the order of what's in the
308 binop_lookup table. */
310 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
311 %token LS_TK SRS_TK ZRS_TK
312 %token AND_TK XOR_TK OR_TK
313 %token BOOL_AND_TK BOOL_OR_TK
314 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
316 /* This maps to the same binop_lookup entry than the token above */
318 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
320 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
321 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
324 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
326 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
327 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
328 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
329 %token PAD_TK ABSTRACT_TK MODIFIER_TK
331 /* Keep those two in order, too */
332 %token DECR_TK INCR_TK
334 /* From now one, things can be in any order */
336 %token DEFAULT_TK IF_TK THROW_TK
337 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
338 %token THROWS_TK BREAK_TK IMPORT_TK
339 %token ELSE_TK INSTANCEOF_TK RETURN_TK
340 %token VOID_TK CATCH_TK INTERFACE_TK
341 %token CASE_TK EXTENDS_TK FINALLY_TK
342 %token SUPER_TK WHILE_TK CLASS_TK
343 %token SWITCH_TK CONST_TK TRY_TK
344 %token FOR_TK NEW_TK CONTINUE_TK
345 %token GOTO_TK PACKAGE_TK THIS_TK
347 %token BYTE_TK SHORT_TK INT_TK LONG_TK
348 %token CHAR_TK INTEGRAL_TK
350 %token FLOAT_TK DOUBLE_TK FP_TK
354 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
356 %token ASSIGN_ANY_TK ASSIGN_TK
357 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
359 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
360 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
362 %type <value> modifiers MODIFIER_TK
364 %type <node> super ID_TK identifier
365 %type <node> name simple_name qualified_name
366 %type <node> class_declaration type_declaration compilation_unit
367 field_declaration method_declaration extends_interfaces
368 interfaces interface_type_list
369 interface_declaration class_member_declaration
370 import_declarations package_declaration
371 type_declarations interface_body
372 interface_member_declaration constant_declaration
373 interface_member_declarations interface_type
374 abstract_method_declaration interface_type_list
375 %type <node> class_body_declaration class_member_declaration
376 static_initializer constructor_declaration block
377 %type <node> class_body_declarations constructor_header
378 %type <node> class_or_interface_type class_type class_type_list
379 constructor_declarator explicit_constructor_invocation
380 %type <node> dim_expr dim_exprs this_or_super throws
382 %type <node> variable_declarator_id variable_declarator
383 variable_declarators variable_initializer
384 variable_initializers constructor_body
387 %type <node> class_body block_end
388 %type <node> statement statement_without_trailing_substatement
389 labeled_statement if_then_statement label_decl
390 if_then_else_statement while_statement for_statement
391 statement_nsi labeled_statement_nsi do_statement
392 if_then_else_statement_nsi while_statement_nsi
393 for_statement_nsi statement_expression_list for_init
394 for_update statement_expression expression_statement
395 primary_no_new_array expression primary
396 array_creation_expression array_type
397 class_instance_creation_expression field_access
398 method_invocation array_access something_dot_new
399 argument_list postfix_expression while_expression
400 post_increment_expression post_decrement_expression
401 unary_expression_not_plus_minus unary_expression
402 pre_increment_expression pre_decrement_expression
403 unary_expression_not_plus_minus cast_expression
404 multiplicative_expression additive_expression
405 shift_expression relational_expression
406 equality_expression and_expression
407 exclusive_or_expression inclusive_or_expression
408 conditional_and_expression conditional_or_expression
409 conditional_expression assignment_expression
410 left_hand_side assignment for_header for_begin
411 constant_expression do_statement_begin empty_statement
412 switch_statement synchronized_statement throw_statement
413 try_statement switch_expression switch_block
414 catches catch_clause catch_clause_parameter finally
415 %type <node> return_statement break_statement continue_statement
417 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
418 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
419 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
420 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
421 %type <operator> ASSIGN_ANY_TK assignment_operator
422 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
423 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
424 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
425 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK
426 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
427 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
428 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
430 %type <node> method_body
432 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
433 STRING_LIT_TK NULL_TK VOID_TK
435 %type <node> IF_TK WHILE_TK FOR_TK
437 %type <node> formal_parameter_list formal_parameter
438 method_declarator method_header
440 %type <node> primitive_type reference_type type
441 BOOLEAN_TK INTEGRAL_TK FP_TK
444 /* 19.2 Production from 2.3: The Syntactic Grammar */
450 /* 19.3 Productions from 3: Lexical structure */
460 /* 19.4 Productions from 4: Types, Values and Variables */
473 class_or_interface_type
477 class_or_interface_type:
482 class_or_interface_type /* Default rule */
486 class_or_interface_type
490 primitive_type OSB_TK CSB_TK
492 $$ = build_java_array_type ($1, -1);
493 CLASS_LOADED_P ($$) = 1;
496 { $$ = build_unresolved_array_type ($1); }
497 | array_type OSB_TK CSB_TK
498 { $$ = build_unresolved_array_type ($1); }
499 | primitive_type OSB_TK error
500 {RULE ("']' expected"); RECOVER;}
501 | array_type OSB_TK error
502 {RULE ("']' expected"); RECOVER;}
505 /* 19.5 Productions from 6: Names */
507 simple_name /* Default rule */
508 | qualified_name /* Default rule */
512 identifier /* Default rule */
516 name DOT_TK identifier
517 { $$ = make_qualified_name ($1, $3, $2.location); }
524 /* 19.6: Production from 7: Packages */
527 | package_declaration
528 | import_declarations
530 | package_declaration import_declarations
531 | package_declaration type_declarations
532 | import_declarations type_declarations
533 | package_declaration import_declarations type_declarations
541 | import_declarations import_declaration
549 | type_declarations type_declaration
553 PACKAGE_TK name SC_TK
554 { ctxp->package = EXPR_WFL_NODE ($2); }
556 {yyerror ("Missing name"); RECOVER;}
557 | PACKAGE_TK name error
558 {yyerror ("';' expected"); RECOVER;}
562 single_type_import_declaration
563 | type_import_on_demand_declaration
566 single_type_import_declaration:
569 tree name = EXPR_WFL_NODE ($2), node, last_name;
570 int i = IDENTIFIER_LENGTH (name)-1;
571 char *last = &IDENTIFIER_POINTER (name)[i];
572 while (last != IDENTIFIER_POINTER (name))
578 last_name = get_identifier (++last);
579 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
581 tree err = find_name_in_single_imports (last_name);
582 if (err && err != name)
584 ($2, "Ambiguous class: `%s' and `%s'",
585 IDENTIFIER_POINTER (name),
586 IDENTIFIER_POINTER (err));
588 REGISTER_IMPORT ($2, last_name)
591 REGISTER_IMPORT ($2, last_name);
594 {yyerror ("Missing name"); RECOVER;}
595 | IMPORT_TK name error
596 {yyerror ("';' expected"); RECOVER;}
599 type_import_on_demand_declaration:
600 IMPORT_TK name DOT_TK MULT_TK SC_TK
602 tree name = EXPR_WFL_NODE ($2);
603 /* Don't import java.lang.* twice. */
604 if (name != java_lang_id)
606 tree node = build_tree_list ($2, NULL_TREE);
607 read_import_dir ($2);
608 TREE_CHAIN (node) = ctxp->import_demand_list;
609 ctxp->import_demand_list = node;
612 | IMPORT_TK name DOT_TK error
613 {yyerror ("'*' expected"); RECOVER;}
614 | IMPORT_TK name DOT_TK MULT_TK error
615 {yyerror ("';' expected"); RECOVER;}
621 maybe_generate_finit ();
622 maybe_generate_clinit ();
625 | interface_declaration
631 yyerror ("Class or interface declaration expected");
635 /* 19.7 Shortened from the original:
636 modifiers: modifier | modifiers modifier
637 modifier: any of public... */
643 | modifiers MODIFIER_TK
648 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
649 java_accstring_lookup (acc));
657 /* 19.8.1 Production from $8.1: Class Declaration */
659 modifiers CLASS_TK identifier super interfaces
660 { create_class ($1, $3, $4, $5); }
665 | CLASS_TK identifier super interfaces
666 { create_class (0, $2, $3, $4); }
671 | modifiers CLASS_TK error
672 {yyerror ("Missing class name"); RECOVER;}
674 {yyerror ("Missing class name"); RECOVER;}
675 | CLASS_TK identifier error
677 if (!ctxp->class_err) yyerror ("'{' expected");
680 | modifiers CLASS_TK identifier error
681 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
686 | EXTENDS_TK class_type
688 | EXTENDS_TK class_type error
689 {yyerror ("'{' expected"); ctxp->class_err=1;}
691 {yyerror ("Missing super class name"); ctxp->class_err=1;}
696 | IMPLEMENTS_TK interface_type_list
698 | IMPLEMENTS_TK error
701 yyerror ("Missing interface name");
708 ctxp->interface_number = 1;
709 $$ = build_tree_list ($1, NULL_TREE);
711 | interface_type_list C_TK interface_type
713 ctxp->interface_number++;
714 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
716 | interface_type_list C_TK error
717 {yyerror ("Missing interface name"); RECOVER;}
722 { $$ = ctxp->current_parsed_class; }
723 | OCB_TK class_body_declarations CCB_TK
724 { $$ = ctxp->current_parsed_class; }
727 class_body_declarations:
728 class_body_declaration
729 | class_body_declarations class_body_declaration
732 class_body_declaration:
733 class_member_declaration
735 | constructor_declaration
736 | block /* Added, JDK1.1, instance initializer */
737 { $$ = parse_jdk1_1_error ("instance initializer"); }
740 class_member_declaration:
742 | field_declaration SC_TK
745 | class_declaration /* Added, JDK1.1 inner classes */
746 { $$ = parse_jdk1_1_error ("inner classe declaration"); }
747 | interface_declaration /* Added, JDK1.1 inner classes */
748 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
751 /* 19.8.2 Productions from 8.3: Field Declarations */
753 type variable_declarators SC_TK
754 { register_fields (0, $1, $2); }
755 | modifiers type variable_declarators SC_TK
758 ("Illegal modifier `%s' for field declaration",
759 $1, FIELD_MODIFIERS);
760 check_modifiers_consistency ($1);
761 register_fields ($1, $2, $3);
765 variable_declarators:
766 /* Should we use build_decl_list () instead ? FIXME */
767 variable_declarator /* Default rule */
768 | variable_declarators C_TK variable_declarator
769 { $$ = chainon ($1, $3); }
770 | variable_declarators C_TK error
771 {yyerror ("Missing term"); RECOVER;}
775 variable_declarator_id
776 { $$ = build_tree_list ($1, NULL_TREE); }
777 | variable_declarator_id ASSIGN_TK variable_initializer
779 if (java_error_count)
782 ($1, build_assignment ($2.token, $2.location, $1, $3));
784 | variable_declarator_id ASSIGN_TK error
786 yyerror ("Missing variable initializer");
787 $$ = build_tree_list ($1, NULL_TREE);
790 | variable_declarator_id ASSIGN_TK variable_initializer error
792 yyerror ("';' expected");
793 $$ = build_tree_list ($1, NULL_TREE);
798 variable_declarator_id:
800 | variable_declarator_id OSB_TK CSB_TK
801 { $$ = build_unresolved_array_type ($1); }
803 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
804 | variable_declarator_id OSB_TK error
805 {yyerror ("']' expected"); DRECOVER(vdi);}
806 | variable_declarator_id CSB_TK error
807 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
810 variable_initializer:
815 /* 19.8.3 Productions from 8.4: Method Declarations */
819 current_function_decl = $1;
820 source_start_java_method (current_function_decl);
823 { complete_method_declaration ($3); }
824 | method_header error
825 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
829 type method_declarator throws
830 { $$ = method_header (0, $1, $2, $3); }
831 | VOID_TK method_declarator throws
832 { $$ = method_header (0, void_type_node, $2, $3); }
833 | modifiers type method_declarator throws
834 { $$ = method_header ($1, $2, $3, $4); }
835 | modifiers VOID_TK method_declarator throws
836 { $$ = method_header ($1, void_type_node, $3, $4); }
839 | modifiers type error
842 {yyerror ("Identifier expected"); RECOVER;}
843 | modifiers VOID_TK error
844 {yyerror ("Identifier expected"); RECOVER;}
847 yyerror ("Invalid method declaration, return type required");
853 identifier OP_TK CP_TK
854 { $$ = method_declarator ($1, NULL_TREE); }
855 | identifier OP_TK formal_parameter_list CP_TK
856 { $$ = method_declarator ($1, $3); }
857 | method_declarator OSB_TK CSB_TK
859 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
861 build_unresolved_array_type (TREE_PURPOSE ($1));
862 parse_warning_context
864 "Discouraged form of returned type specification");
866 | identifier OP_TK error
867 {yyerror ("')' expected"); DRECOVER(method_declarator);}
868 | method_declarator OSB_TK error
869 {yyerror ("']' expected"); RECOVER;}
872 formal_parameter_list:
875 ctxp->formal_parameter_number = 1;
877 | formal_parameter_list C_TK formal_parameter
879 ctxp->formal_parameter_number += 1;
880 $$ = chainon ($1, $3);
882 | formal_parameter_list C_TK error
883 {yyerror ("Missing formal parameter term"); RECOVER;}
887 type variable_declarator_id
889 $$ = build_tree_list ($2, $1);
891 | modifiers type variable_declarator_id /* Added, JDK1.1 final parms */
892 { $$ = parse_jdk1_1_error ("final parameters"); }
894 {yyerror ("Missing identifier"); RECOVER;}
895 | modifiers type error
897 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
898 yyerror ("Missing identifier"); RECOVER;
904 | THROWS_TK class_type_list
907 {yyerror ("Missing class type term"); RECOVER;}
912 { $$ = build_tree_list ($1, $1); }
913 | class_type_list C_TK class_type
914 { $$ = tree_cons ($3, $3, $1); }
915 | class_type_list C_TK error
916 {yyerror ("Missing class type term"); RECOVER;}
923 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
926 /* 19.8.4 Productions from 8.5: Static Initializers */
930 RULE ("STATIC_INITIALIZER");
932 | static block SC_TK /* Shouldn't be here. FIXME */
934 RULE ("STATIC_INITIALIZER");
938 static: /* Test lval.sub_token here */
941 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
945 /* 19.8.5 Productions from 8.6: Constructor Declarations */
946 constructor_declaration:
949 current_function_decl = $1;
950 source_start_java_method (current_function_decl);
953 { complete_method_declaration ($3); }
957 constructor_declarator throws
958 { $$ = method_header (0, NULL_TREE, $1, $2); }
959 | modifiers constructor_declarator throws
960 { $$ = method_header ($1, NULL_TREE, $2, $3); }
963 constructor_declarator:
964 simple_name OP_TK CP_TK
965 { $$ = method_declarator ($1, NULL_TREE); }
966 | simple_name OP_TK formal_parameter_list CP_TK
967 { $$ = method_declarator ($1, $3); }
971 /* Unlike regular method, we always need a complete (empty)
972 body so we can safely perform all the required code
973 addition (super invocation and field initialization) */
974 block_begin block_end
976 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
979 | block_begin explicit_constructor_invocation block_end
981 | block_begin block_statements block_end
983 | block_begin explicit_constructor_invocation block_statements block_end
987 /* Error recovery for that rule moved down expression_statement: rule. */
988 explicit_constructor_invocation:
989 this_or_super OP_TK CP_TK SC_TK
991 $$ = build_method_invocation ($1, NULL_TREE);
992 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
993 $$ = java_method_add_stmt (current_function_decl, $$);
995 | this_or_super OP_TK argument_list CP_TK SC_TK
997 $$ = build_method_invocation ($1, $3);
998 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
999 $$ = java_method_add_stmt (current_function_decl, $$);
1001 /* Added, JDK1.1 inner classes. Modified because the rule
1002 'primary' couldn't work. */
1003 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1004 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1005 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1006 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1009 this_or_super: /* Added, simplifies error diagnostics */
1012 tree wfl = build_wfl_node (this_identifier_node,
1013 input_filename, 0, 0);
1014 EXPR_WFL_LINECOL (wfl) = $1.location;
1019 tree wfl = build_wfl_node (super_identifier_node,
1020 input_filename, 0, 0);
1021 EXPR_WFL_LINECOL (wfl) = $1.location;
1026 /* 19.9 Productions from 9: Interfaces */
1027 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1028 interface_declaration:
1029 INTERFACE_TK identifier
1030 { create_interface (0, $2, NULL_TREE); }
1035 | modifiers INTERFACE_TK identifier
1036 { create_interface ($1, $3, NULL_TREE); }
1041 | INTERFACE_TK identifier extends_interfaces
1042 { create_interface (0, $2, $3); }
1047 | modifiers INTERFACE_TK identifier extends_interfaces
1048 { create_interface ($1, $3, $4); }
1053 | INTERFACE_TK identifier error
1054 {yyerror ("'{' expected"); RECOVER;}
1055 | modifiers INTERFACE_TK identifier error
1056 {yyerror ("'{' expected"); RECOVER;}
1060 EXTENDS_TK interface_type
1062 ctxp->interface_number = 1;
1063 $$ = build_tree_list ($2, NULL_TREE);
1065 | extends_interfaces C_TK interface_type
1067 ctxp->interface_number++;
1068 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1071 {yyerror ("Invalid interface type"); RECOVER;}
1072 | extends_interfaces C_TK error
1073 {yyerror ("Missing term"); RECOVER;}
1079 | OCB_TK interface_member_declarations CCB_TK
1083 interface_member_declarations:
1084 interface_member_declaration
1085 | interface_member_declarations interface_member_declaration
1088 interface_member_declaration:
1089 constant_declaration
1090 | abstract_method_declaration
1091 | class_declaration /* Added, JDK1.1 inner classes */
1092 { $$ = parse_jdk1_1_error ("inner class declaration"); }
1093 | interface_declaration /* Added, JDK1.1 inner classes */
1094 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
1097 constant_declaration:
1101 abstract_method_declaration:
1104 check_abstract_method_header ($1);
1105 current_function_decl = NULL_TREE; /* FIXME ? */
1107 | method_header error
1108 {yyerror ("';' expected"); RECOVER;}
1111 /* 19.10 Productions from 10: Arrays */
1114 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1115 | OCB_TK variable_initializers CCB_TK
1116 { $$ = build_new_array_init ($1.location, $2); }
1117 | OCB_TK variable_initializers C_TK CCB_TK
1118 { $$ = build_new_array_init ($1.location, $2); }
1121 variable_initializers:
1122 variable_initializer
1124 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1127 | variable_initializers C_TK variable_initializer
1129 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1131 | variable_initializers C_TK error
1132 {yyerror ("Missing term"); RECOVER;}
1135 /* 19.11 Production from 14: Blocks and Statements */
1138 { $$ = empty_stmt_node; }
1139 | block_begin block_statements block_end
1151 maybe_absorb_scoping_blocks ();
1158 | block_statements block_statement
1162 local_variable_declaration_statement
1164 { java_method_add_stmt (current_function_decl, $1); }
1165 | class_declaration /* Added, JDK1.1 inner classes */
1166 { parse_jdk1_1_error ("inner class declaration"); }
1169 local_variable_declaration_statement:
1170 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1173 local_variable_declaration:
1174 type variable_declarators
1175 { declare_local_variables (0, $1, $2); }
1176 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
1177 { declare_local_variables ($1, $2, $3); }
1181 statement_without_trailing_substatement
1184 | if_then_else_statement
1188 /* If the for loop is unlabeled, we must return the
1189 block it was defined it. It our last chance to
1190 get a hold on it. */
1191 if (!LOOP_HAS_LABEL_P ($$))
1197 statement_without_trailing_substatement
1198 | labeled_statement_nsi
1199 | if_then_else_statement_nsi
1200 | while_statement_nsi
1204 statement_without_trailing_substatement:
1207 | expression_statement
1211 | continue_statement
1213 | synchronized_statement
1220 { $$ = empty_stmt_node; }
1224 identifier REL_CL_TK
1226 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1227 EXPR_WFL_NODE ($1));
1229 push_labeled_block ($$);
1230 PUSH_LABELED_BLOCK ($$);
1235 label_decl statement
1237 $$ = complete_labeled_statement ($1, $2);
1238 pop_labeled_block ();
1239 POP_LABELED_BLOCK ();
1242 {yyerror ("':' expected"); RECOVER;}
1245 labeled_statement_nsi:
1246 label_decl statement_nsi
1248 $$ = complete_labeled_statement ($1, $2);
1249 pop_labeled_block ();
1250 POP_LABELED_BLOCK ();
1254 /* We concentrate here a bunch of error handling rules that we couldn't write
1255 earlier, because expression_statement catches a missing ';'. */
1256 expression_statement:
1257 statement_expression SC_TK
1259 /* We have a statement. Generate a WFL around it so
1261 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1262 /* We know we have a statement, so set the debug
1263 info to be eventually generate here. */
1264 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1268 if (ctxp->prevent_ese != lineno)
1269 yyerror ("Invalid expression statement");
1270 DRECOVER (expr_stmt);
1274 if (ctxp->prevent_ese != lineno)
1275 yyerror ("Invalid expression statement");
1276 DRECOVER (expr_stmt);
1280 if (ctxp->prevent_ese != lineno)
1281 yyerror ("Invalid expression statement");
1282 DRECOVER (expr_stmt);
1284 | this_or_super OP_TK error
1285 {yyerror ("')' expected"); RECOVER;}
1286 | this_or_super OP_TK CP_TK error
1288 yyerror ("Constructor invocation must be first "
1289 "thing in a constructor");
1292 | this_or_super OP_TK argument_list error
1293 {yyerror ("')' expected"); RECOVER;}
1294 | this_or_super OP_TK argument_list CP_TK error
1296 yyerror ("Constructor invocation must be first "
1297 "thing in a constructor");
1300 | name DOT_TK SUPER_TK error
1301 {yyerror ("'(' expected"); RECOVER;}
1302 | name DOT_TK SUPER_TK OP_TK error
1303 {yyerror ("')' expected"); RECOVER;}
1304 | name DOT_TK SUPER_TK OP_TK argument_list error
1305 {yyerror ("')' expected"); RECOVER;}
1306 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1307 {yyerror ("';' expected"); RECOVER;}
1308 | name DOT_TK SUPER_TK OP_TK CP_TK error
1309 {yyerror ("';' expected"); RECOVER;}
1312 statement_expression:
1314 | pre_increment_expression
1315 | pre_decrement_expression
1316 | post_increment_expression
1317 | post_decrement_expression
1319 | class_instance_creation_expression
1323 IF_TK OP_TK expression CP_TK statement
1324 { $$ = build_if_else_statement ($2.location, $3, $5, NULL_TREE); }
1326 {yyerror ("'(' expected"); RECOVER;}
1328 {yyerror ("Missing term"); RECOVER;}
1329 | IF_TK OP_TK expression error
1330 {yyerror ("')' expected"); RECOVER;}
1333 if_then_else_statement:
1334 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1335 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1338 if_then_else_statement_nsi:
1339 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1340 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1350 /* Make into "proper list" of COMPOUND_EXPRs.
1351 I.e. make the last statment also have its own
1353 maybe_absorb_scoping_blocks ();
1354 TREE_OPERAND ($1, 1) = exit_block ();
1355 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1360 SWITCH_TK OP_TK expression CP_TK
1362 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1363 EXPR_WFL_LINECOL ($$) = $2.location;
1366 {yyerror ("'(' expected"); RECOVER;}
1367 | SWITCH_TK OP_TK error
1368 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1369 | SWITCH_TK OP_TK expression CP_TK error
1370 {yyerror ("'{' expected"); RECOVER;}
1373 /* Default assignment is there to avoid type node on switch_block
1379 | OCB_TK switch_labels CCB_TK
1381 | OCB_TK switch_block_statement_groups CCB_TK
1383 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1387 switch_block_statement_groups:
1388 switch_block_statement_group
1389 | switch_block_statement_groups switch_block_statement_group
1392 switch_block_statement_group:
1393 switch_labels block_statements
1398 | switch_labels switch_label
1402 CASE_TK constant_expression REL_CL_TK
1404 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1405 EXPR_WFL_LINECOL (lab) = $1.location;
1406 java_method_add_stmt (current_function_decl, lab);
1408 | DEFAULT_TK REL_CL_TK
1410 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1411 EXPR_WFL_LINECOL (lab) = $1.location;
1412 java_method_add_stmt (current_function_decl, lab);
1415 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1416 | CASE_TK constant_expression error
1417 {yyerror ("':' expected"); RECOVER;}
1419 {yyerror ("':' expected"); RECOVER;}
1423 WHILE_TK OP_TK expression CP_TK
1425 tree body = build_loop_body ($2.location, $3, 0);
1426 $$ = build_new_loop (body);
1431 while_expression statement
1432 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1434 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1435 | WHILE_TK OP_TK error
1436 {yyerror ("Missing term and ')' expected"); RECOVER;}
1437 | WHILE_TK OP_TK expression error
1438 {yyerror ("')' expected"); RECOVER;}
1441 while_statement_nsi:
1442 while_expression statement_nsi
1443 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1449 tree body = build_loop_body (0, NULL_TREE, 1);
1450 $$ = build_new_loop (body);
1452 /* Need error handing here. FIXME */
1456 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1457 { $$ = complete_loop_body ($4.location, $5, $2, 1); }
1461 for_begin SC_TK expression SC_TK for_update CP_TK statement
1462 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1463 | for_begin SC_TK SC_TK for_update CP_TK statement
1465 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1466 /* We have not condition, so we get rid of the EXIT_EXPR */
1467 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1470 | for_begin SC_TK error
1471 {yyerror ("Invalid control expression"); RECOVER;}
1472 | for_begin SC_TK expression SC_TK error
1473 {yyerror ("Invalid update expression"); RECOVER;}
1474 | for_begin SC_TK SC_TK error
1475 {yyerror ("Invalid update expression"); RECOVER;}
1479 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1480 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1481 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1483 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1484 /* We have not condition, so we get rid of the EXIT_EXPR */
1485 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1493 /* This scope defined for local variable that may be
1494 defined within the scope of the for loop */
1498 {yyerror ("'(' expected"); DRECOVER(for_1);}
1499 | FOR_TK OP_TK error
1500 {yyerror ("Invalid init statement"); RECOVER;}
1506 /* We now declare the loop body. The loop is
1507 declared as a for loop. */
1508 tree body = build_loop_body (0, NULL_TREE, 0);
1509 $$ = build_new_loop (body);
1510 IS_FOR_LOOP_P ($$) = 1;
1511 /* The loop is added to the current block the for
1512 statement is defined within */
1513 java_method_add_stmt (current_function_decl, $$);
1516 for_init: /* Can be empty */
1517 { $$ = empty_stmt_node; }
1518 | statement_expression_list
1520 /* Init statement recorded within the previously
1521 defined block scope */
1522 $$ = java_method_add_stmt (current_function_decl, $1);
1524 | local_variable_declaration
1526 /* Local variable are recorded within the previously
1527 defined block scope */
1530 | statement_expression_list error
1531 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1534 for_update: /* Can be empty */
1535 {$$ = empty_stmt_node;}
1536 | statement_expression_list
1537 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1540 statement_expression_list:
1541 statement_expression
1542 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1543 | statement_expression_list C_TK statement_expression
1544 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1545 | statement_expression_list C_TK error
1546 {yyerror ("Missing term"); RECOVER;}
1551 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1552 | BREAK_TK identifier SC_TK
1553 { $$ = build_bc_statement ($1.location, 1, $2); }
1555 {yyerror ("Missing term"); RECOVER;}
1556 | BREAK_TK identifier error
1557 {yyerror ("';' expected"); RECOVER;}
1562 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1563 | CONTINUE_TK identifier SC_TK
1564 { $$ = build_bc_statement ($1.location, 0, $2); }
1566 {yyerror ("Missing term"); RECOVER;}
1567 | CONTINUE_TK identifier error
1568 {yyerror ("';' expected"); RECOVER;}
1573 { $$ = build_return ($1.location, NULL_TREE); }
1574 | RETURN_TK expression SC_TK
1575 { $$ = build_return ($1.location, $2); }
1577 {yyerror ("Missing term"); RECOVER;}
1578 | RETURN_TK expression error
1579 {yyerror ("';' expected"); RECOVER;}
1583 THROW_TK expression SC_TK
1585 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1586 EXPR_WFL_LINECOL ($$) = $1.location;
1589 {yyerror ("Missing term"); RECOVER;}
1590 | THROW_TK expression error
1591 {yyerror ("';' expected"); RECOVER;}
1594 synchronized_statement:
1595 synchronized OP_TK expression CP_TK block
1597 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1598 EXPR_WFL_LINECOL ($$) =
1599 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1601 | synchronized OP_TK expression CP_TK error
1602 {yyerror ("'{' expected"); RECOVER;}
1603 | synchronized error
1604 {yyerror ("'(' expected"); RECOVER;}
1605 | synchronized OP_TK error CP_TK
1606 {yyerror ("Missing term"); RECOVER;}
1607 | synchronized OP_TK error
1608 {yyerror ("Missing term"); RECOVER;}
1614 if ((1 << $1) != ACC_SYNCHRONIZED)
1615 fatal ("synchronized was '%d' - yyparse", (1 << $1));
1620 TRY_TK block catches
1621 { $$ = build_try_statement ($1.location, $2, $3); }
1622 | TRY_TK block finally
1623 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1624 | TRY_TK block catches finally
1625 { $$ = build_try_finally_statement ($1.location,
1626 build_try_statement ($1.location,
1630 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1635 | catches catch_clause
1637 TREE_CHAIN ($2) = $1;
1643 catch_clause_parameter block
1645 java_method_add_stmt (current_function_decl, $2);
1650 catch_clause_parameter:
1651 CATCH_TK OP_TK formal_parameter CP_TK
1653 /* We add a block to define a scope for
1654 formal_parameter (CCBP). The formal parameter is
1655 declared initialized by the appropriate function
1657 tree ccpb = enter_block ();
1658 tree init = build_assignment (ASSIGN_TK, $2.location,
1660 soft_exceptioninfo_call_node);
1661 declare_local_variables (0, TREE_VALUE ($3),
1662 build_tree_list (TREE_PURPOSE ($3),
1664 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1665 EXPR_WFL_LINECOL ($$) = $1.location;
1668 {yyerror ("'(' expected"); RECOVER;}
1669 | CATCH_TK OP_TK error
1670 {yyerror ("Missing term or ')' expected"); DRECOVER (2);}
1671 | CATCH_TK OP_TK error CP_TK /* That's for () */
1672 {yyerror ("')' expected"); DRECOVER (1);}
1679 {yyerror ("'{' expected"); RECOVER; }
1682 /* 19.12 Production from 15: Expressions */
1684 primary_no_new_array
1685 | array_creation_expression
1688 primary_no_new_array:
1691 { $$ = build_this ($1.location); }
1692 | OP_TK expression CP_TK
1694 | class_instance_creation_expression
1698 /* type DOT_TK CLASS_TK doens't work. So we split the rule
1699 'type' into its components. Missing is something for array,
1700 which will complete the reference_type part. FIXME */
1701 | name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1702 { $$ = parse_jdk1_1_error ("named class literals"); }
1703 | primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1704 { $$ = build_class_ref ($1); }
1705 | VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1706 { $$ = build_class_ref (void_type_node); }
1707 /* Added, JDK1.1 inner classes. Documentation is wrong
1708 refering to a 'ClassName' (class_name) rule that doesn't
1709 exist. Used name instead. */
1710 | name DOT_TK THIS_TK
1711 { $$ = parse_jdk1_1_error ("class literals"); }
1712 | OP_TK expression error
1713 {yyerror ("')' expected"); RECOVER;}
1715 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1716 | primitive_type DOT_TK error
1717 {yyerror ("'class' expected" ); RECOVER;}
1718 | VOID_TK DOT_TK error
1719 {yyerror ("'class' expected" ); RECOVER;}
1722 class_instance_creation_expression:
1723 NEW_TK class_type OP_TK argument_list CP_TK
1724 { $$ = build_new_invocation ($2, $4); }
1725 | NEW_TK class_type OP_TK CP_TK
1726 { $$ = build_new_invocation ($2, NULL_TREE); }
1727 /* Added, JDK1.1 inner classes but modified to use
1728 'class_type' instead of 'TypeName' (type_name) mentionned
1729 in the documentation but doesn't exist. */
1730 | NEW_TK class_type OP_TK argument_list CP_TK class_body
1731 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1732 | NEW_TK class_type OP_TK CP_TK class_body
1733 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1734 /* Added, JDK1.1 inner classes, modified to use name or
1735 primary instead of primary solely which couldn't work in
1737 | something_dot_new identifier OP_TK CP_TK
1738 | something_dot_new identifier OP_TK CP_TK class_body
1739 | something_dot_new identifier OP_TK argument_list CP_TK
1740 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1741 | NEW_TK error SC_TK
1742 {yyerror ("'(' expected"); DRECOVER(new_1);}
1743 | NEW_TK class_type error
1744 {yyerror ("'(' expected"); RECOVER;}
1745 | NEW_TK class_type OP_TK error
1746 {yyerror ("')' or term expected"); RECOVER;}
1747 | NEW_TK class_type OP_TK argument_list error
1748 {yyerror ("')' expected"); RECOVER;}
1749 | something_dot_new error
1750 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1751 | something_dot_new identifier error
1752 {yyerror ("'(' expected"); RECOVER;}
1755 something_dot_new: /* Added, not part of the specs. */
1757 | primary DOT_TK NEW_TK
1763 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
1764 ctxp->formal_parameter_number = 1;
1766 | argument_list C_TK expression
1768 ctxp->formal_parameter_number += 1;
1769 $$ = tree_cons (NULL_TREE, $3, $1);
1771 | argument_list C_TK error
1772 {yyerror ("Missing term"); RECOVER;}
1775 array_creation_expression:
1776 NEW_TK primitive_type dim_exprs
1777 { $$ = build_newarray_node ($2, $3, 0); }
1778 | NEW_TK class_or_interface_type dim_exprs
1779 { $$ = build_newarray_node ($2, $3, 0); }
1780 | NEW_TK primitive_type dim_exprs dims
1781 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
1782 | NEW_TK class_or_interface_type dim_exprs dims
1783 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
1784 /* Added, JDK1.1 anonymous array. Initial documentation rule
1786 | NEW_TK class_or_interface_type dims array_initializer
1787 { $$ = parse_jdk1_1_error ("anonymous array"); }
1788 | NEW_TK primitive_type dims array_initializer
1789 { $$ = parse_jdk1_1_error ("anonymous array"); }
1790 | NEW_TK error CSB_TK
1791 {yyerror ("'[' expected"); DRECOVER ("]");}
1792 | NEW_TK error OSB_TK
1793 {yyerror ("']' expected"); RECOVER;}
1798 { $$ = build_tree_list (NULL_TREE, $1); }
1799 | dim_exprs dim_expr
1800 { $$ = tree_cons (NULL_TREE, $2, $$); }
1804 OSB_TK expression CSB_TK
1806 EXPR_WFL_LINECOL ($2) = $1.location;
1809 | OSB_TK expression error
1810 {yyerror ("']' expected"); RECOVER;}
1813 yyerror ("Missing term");
1814 yyerror ("']' expected");
1823 /* If not initialized, allocate memory for the osb
1825 if (!ctxp->osb_limit)
1827 allocate = ctxp->osb_limit = 32;
1828 ctxp->osb_depth = -1;
1830 /* If capacity overflown, reallocate a bigger chuck */
1831 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
1832 allocate = ctxp->osb_limit << 1;
1836 allocate *= sizeof (int);
1837 if (ctxp->osb_number)
1838 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
1841 ctxp->osb_number = (int *)xmalloc (allocate);
1844 CURRENT_OSB (ctxp) = 1;
1846 | dims OSB_TK CSB_TK
1847 { CURRENT_OSB (ctxp)++; }
1849 { yyerror ("']' expected"); RECOVER;}
1853 primary DOT_TK identifier
1854 { $$ = make_qualified_primary ($1, $3, $2.location); }
1855 /* FIXME - REWRITE TO:
1856 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
1857 | SUPER_TK DOT_TK identifier
1860 build_wfl_node (super_identifier_node,
1861 input_filename, 0, 0);
1862 EXPR_WFL_LINECOL (super_wfl) = $1.location;
1863 $$ = make_qualified_name (super_wfl, $3, $2.location);
1866 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
1871 { $$ = build_method_invocation ($1, NULL_TREE); }
1872 | name OP_TK argument_list CP_TK
1873 { $$ = build_method_invocation ($1, $3); }
1874 | primary DOT_TK identifier OP_TK CP_TK
1876 if (TREE_CODE ($1) == THIS_EXPR)
1877 $$ = build_this_super_qualified_invocation
1878 (1, $3, NULL_TREE, 0, $2.location);
1881 tree invok = build_method_invocation ($3, NULL_TREE);
1882 $$ = make_qualified_primary ($1, invok, $2.location);
1885 | primary DOT_TK identifier OP_TK argument_list CP_TK
1887 if (TREE_CODE ($1) == THIS_EXPR)
1888 $$ = build_this_super_qualified_invocation
1889 (1, $3, $5, 0, $2.location);
1892 tree invok = build_method_invocation ($3, $5);
1893 $$ = make_qualified_primary ($1, invok, $2.location);
1896 | SUPER_TK DOT_TK identifier OP_TK CP_TK
1898 $$ = build_this_super_qualified_invocation
1899 (0, $3, NULL_TREE, $1.location, $2.location);
1901 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
1903 $$ = build_this_super_qualified_invocation
1904 (0, $3, $5, $1.location, $2.location);
1906 /* Screws up thing. I let it here until I'm convinced it can
1908 | primary DOT_TK error
1909 {yyerror ("'(' expected"); DRECOVER(bad);} */
1910 | SUPER_TK DOT_TK error CP_TK
1911 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1912 | SUPER_TK DOT_TK error DOT_TK
1913 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1917 name OSB_TK expression CSB_TK
1918 { $$ = build_array_ref ($2.location, $1, $3); }
1919 | primary_no_new_array OSB_TK expression CSB_TK
1920 { $$ = build_array_ref ($2.location, $1, $3); }
1923 yyerror ("Missing term and ']' expected");
1924 DRECOVER(array_access);
1926 | name OSB_TK expression error
1928 yyerror ("']' expected");
1929 DRECOVER(array_access);
1931 | primary_no_new_array OSB_TK error
1933 yyerror ("Missing term and ']' expected");
1934 DRECOVER(array_access);
1936 | primary_no_new_array OSB_TK expression error
1938 yyerror ("']' expected");
1939 DRECOVER(array_access);
1946 | post_increment_expression
1947 | post_decrement_expression
1950 post_increment_expression:
1951 postfix_expression INCR_TK
1952 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1955 post_decrement_expression:
1956 postfix_expression DECR_TK
1957 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1961 pre_increment_expression
1962 | pre_decrement_expression
1963 | PLUS_TK unary_expression
1964 {$$ = build_unaryop ($1.token, $1.location, $2); }
1965 | MINUS_TK unary_expression
1966 {$$ = build_unaryop ($1.token, $1.location, $2); }
1967 | unary_expression_not_plus_minus
1969 {yyerror ("Missing term"); RECOVER}
1971 {yyerror ("Missing term"); RECOVER}
1974 pre_increment_expression:
1975 INCR_TK unary_expression
1976 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1978 {yyerror ("Missing term"); RECOVER}
1981 pre_decrement_expression:
1982 DECR_TK unary_expression
1983 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1985 {yyerror ("Missing term"); RECOVER}
1988 unary_expression_not_plus_minus:
1990 | NOT_TK unary_expression
1991 {$$ = build_unaryop ($1.token, $1.location, $2); }
1992 | NEG_TK unary_expression
1993 {$$ = build_unaryop ($1.token, $1.location, $2); }
1996 {yyerror ("Missing term"); RECOVER}
1998 {yyerror ("Missing term"); RECOVER}
2001 cast_expression: /* Error handling here is potentially weak */
2002 OP_TK primitive_type dims CP_TK unary_expression
2005 while (CURRENT_OSB (ctxp)--)
2006 type = build_java_array_type (type, -1);
2008 $$ = build_cast ($1.location, type, $5);
2010 | OP_TK primitive_type CP_TK unary_expression
2011 { $$ = build_cast ($1.location, $2, $4); }
2012 | OP_TK expression CP_TK unary_expression_not_plus_minus
2013 { $$ = build_cast ($1.location, $2, $4); }
2014 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2017 while (CURRENT_OSB (ctxp)--)
2018 obstack_1grow (&temporary_obstack, '[');
2020 obstack_grow0 (&temporary_obstack,
2021 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2022 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2023 ptr = obstack_finish (&temporary_obstack);
2024 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2025 $$ = build_cast ($1.location, $2, $5);
2027 | OP_TK primitive_type OSB_TK error
2028 {yyerror ("']' expected, invalid type expression");}
2031 if (ctxp->prevent_ese != lineno)
2032 yyerror ("Invalid type expression"); RECOVER;
2035 | OP_TK primitive_type dims CP_TK error
2036 {yyerror ("Missing term"); RECOVER;}
2037 | OP_TK primitive_type CP_TK error
2038 {yyerror ("Missing term"); RECOVER;}
2039 | OP_TK name dims CP_TK error
2040 {yyerror ("Missing term"); RECOVER;}
2043 multiplicative_expression:
2045 | multiplicative_expression MULT_TK unary_expression
2047 $$ = build_binop (BINOP_LOOKUP ($2.token),
2048 $2.location, $1, $3);
2050 | multiplicative_expression DIV_TK unary_expression
2052 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2055 | multiplicative_expression REM_TK unary_expression
2057 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2060 | multiplicative_expression MULT_TK error
2061 {yyerror ("Missing term"); RECOVER;}
2062 | multiplicative_expression DIV_TK error
2063 {yyerror ("Missing term"); RECOVER;}
2064 | multiplicative_expression REM_TK error
2065 {yyerror ("Missing term"); RECOVER;}
2068 additive_expression:
2069 multiplicative_expression
2070 | additive_expression PLUS_TK multiplicative_expression
2072 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2075 | additive_expression MINUS_TK multiplicative_expression
2077 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2080 | additive_expression PLUS_TK error
2081 {yyerror ("Missing term"); RECOVER;}
2082 | additive_expression MINUS_TK error
2083 {yyerror ("Missing term"); RECOVER;}
2088 | shift_expression LS_TK additive_expression
2090 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2093 | shift_expression SRS_TK additive_expression
2095 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2098 | shift_expression ZRS_TK additive_expression
2100 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2103 | shift_expression LS_TK error
2104 {yyerror ("Missing term"); RECOVER;}
2105 | shift_expression SRS_TK error
2106 {yyerror ("Missing term"); RECOVER;}
2107 | shift_expression ZRS_TK error
2108 {yyerror ("Missing term"); RECOVER;}
2111 relational_expression:
2113 | relational_expression LT_TK shift_expression
2115 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2118 | relational_expression GT_TK shift_expression
2120 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2123 | relational_expression LTE_TK shift_expression
2125 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2128 | relational_expression GTE_TK shift_expression
2130 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2133 | relational_expression INSTANCEOF_TK reference_type
2134 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2135 | relational_expression LT_TK error
2136 {yyerror ("Missing term"); RECOVER;}
2137 | relational_expression GT_TK error
2138 {yyerror ("Missing term"); RECOVER;}
2139 | relational_expression LTE_TK error
2140 {yyerror ("Missing term"); RECOVER;}
2141 | relational_expression GTE_TK error
2142 {yyerror ("Missing term"); RECOVER;}
2143 | relational_expression INSTANCEOF_TK error
2144 {yyerror ("Invalid reference type"); RECOVER;}
2147 equality_expression:
2148 relational_expression
2149 | equality_expression EQ_TK relational_expression
2151 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2154 | equality_expression NEQ_TK relational_expression
2156 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2159 | equality_expression EQ_TK error
2160 {yyerror ("Missing term"); RECOVER;}
2161 | equality_expression NEQ_TK error
2162 {yyerror ("Missing term"); RECOVER;}
2167 | and_expression AND_TK equality_expression
2169 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2172 | and_expression AND_TK error
2173 {yyerror ("Missing term"); RECOVER;}
2176 exclusive_or_expression:
2178 | exclusive_or_expression XOR_TK and_expression
2180 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2183 | exclusive_or_expression XOR_TK error
2184 {yyerror ("Missing term"); RECOVER;}
2187 inclusive_or_expression:
2188 exclusive_or_expression
2189 | inclusive_or_expression OR_TK exclusive_or_expression
2191 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2194 | inclusive_or_expression OR_TK error
2195 {yyerror ("Missing term"); RECOVER;}
2198 conditional_and_expression:
2199 inclusive_or_expression
2200 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2202 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2205 | conditional_and_expression BOOL_AND_TK error
2206 {yyerror ("Missing term"); RECOVER;}
2209 conditional_or_expression:
2210 conditional_and_expression
2211 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2213 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2216 | conditional_or_expression BOOL_OR_TK error
2217 {yyerror ("Missing term"); RECOVER;}
2220 conditional_expression: /* Error handling here is weak */
2221 conditional_or_expression
2222 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2224 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2225 EXPR_WFL_LINECOL ($$) = $2.location;
2227 | conditional_or_expression REL_QM_TK REL_CL_TK error
2230 yyerror ("Missing term");
2233 | conditional_or_expression REL_QM_TK error
2234 {yyerror ("Missing term"); DRECOVER (2);}
2235 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2236 {yyerror ("Missing term"); DRECOVER (3);}
2239 assignment_expression:
2240 conditional_expression
2245 left_hand_side assignment_operator assignment_expression
2246 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2247 | left_hand_side assignment_operator error
2249 if (ctxp->prevent_ese != lineno)
2250 yyerror ("Missing term");
2261 assignment_operator:
2267 assignment_expression
2270 constant_expression:
2279 /* Flag for the error report routine to issue the error the first time
2280 it's called (overriding the default behavior which is to drop the
2281 first invocation and honor the second one, taking advantage of a
2283 static int force_error = 0;
2285 /* Create a new parser context and make it the current one. */
2288 java_push_parser_context ()
2290 struct parser_ctxt *new =
2291 (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2293 bzero (new, sizeof (struct parser_ctxt));
2298 ctxp->incomplete_class = ctxp->next->incomplete_class;
2299 ctxp->gclass_list = ctxp->next->gclass_list;
2303 /* If the first file of a file list was a class file, no context
2304 exists for a source file to be parsed. This boolean remembers that
2305 java_parser_context_save_global might have created a dummy one, so
2306 that java_parser_context_restore_global can pop it. */
2307 static int extra_ctxp_pushed_p = 0;
2310 java_parser_context_save_global ()
2314 java_push_parser_context ();
2315 extra_ctxp_pushed_p = 1;
2317 ctxp->finput = finput;
2318 ctxp->lineno = lineno;
2319 ctxp->current_class = current_class;
2320 ctxp->filename = input_filename;
2321 ctxp->current_function_decl = current_function_decl;
2325 java_parser_context_restore_global ()
2327 finput = ctxp->finput;
2328 lineno = ctxp->lineno;
2329 current_class = ctxp->current_class;
2330 input_filename = ctxp->filename;
2331 current_function_decl = ctxp->current_function_decl;
2332 if (!ctxp->next && extra_ctxp_pushed_p)
2334 java_pop_parser_context (0);
2335 extra_ctxp_pushed_p = 0;
2340 java_pop_parser_context (generate)
2344 struct parser_ctxt *toFree, *next;
2353 next->incomplete_class = ctxp->incomplete_class;
2354 next->gclass_list = ctxp->gclass_list;
2355 lineno = ctxp->lineno;
2356 finput = ctxp->finput;
2357 current_class = ctxp->current_class;
2360 /* Set the single import class file flag to 0 for the current list
2361 of imported things */
2362 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2363 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2365 /* And restore those of the previous context */
2366 if ((ctxp = next)) /* Assignment is really meant here */
2367 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2368 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2372 toFree->next = ctxp_for_generation;
2373 ctxp_for_generation = toFree;
2379 /* Reporting JDK1.1 features not implemented */
2382 parse_jdk1_1_error (msg)
2385 sorry (": `%s' JDK1.1(TM) feature", msg);
2387 return empty_stmt_node;
2390 static int do_warning = 0;
2397 static int prev_lineno;
2398 static char *prev_msg;
2401 char *remainder, *code_from_source;
2402 extern struct obstack temporary_obstack;
2404 if (!force_error && prev_lineno == lineno)
2407 /* Save current error location but report latter, when the context is
2409 if (ctxp->java_error_flag == 0)
2411 ctxp->java_error_flag = 1;
2413 /* Do something to use the previous line if we're reaching the
2414 end of the file... */
2415 #ifdef VERBOSE_SKELETON
2416 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2421 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2422 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2425 ctxp->java_error_flag = 0;
2427 java_warning_count++;
2431 if (elc.col == 0 && msg[1] == ';')
2433 elc.col = ctxp->p_line->char_col-1;
2434 elc.line = ctxp->p_line->lineno;
2437 save_lineno = lineno;
2438 prev_lineno = lineno = elc.line;
2441 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2442 obstack_grow0 (&temporary_obstack,
2443 code_from_source, strlen (code_from_source));
2444 remainder = obstack_finish (&temporary_obstack);
2446 warning ("%s.\n%s", msg, remainder);
2448 error ("%s.\n%s", msg, remainder);
2450 /* This allow us to cheaply avoid an extra 'Invalid expression
2451 statement' error report when errors have been already reported on
2452 the same line. This occurs when we report an error but don't have
2453 a synchronization point other than ';', which
2454 expression_statement is the only one to take care of. */
2455 ctxp->prevent_ese = lineno = save_lineno;
2459 issue_warning_error_from_context (cl, msg, ap)
2464 char *saved, *saved_input_filename;
2466 vsprintf (buffer, msg, ap);
2469 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2470 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2471 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
2473 /* We have a CL, that's a good reason for using it if it contains data */
2474 saved = ctxp->filename;
2475 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2476 ctxp->filename = EXPR_WFL_FILENAME (cl);
2477 saved_input_filename = input_filename;
2478 input_filename = ctxp->filename;
2480 java_error (buffer);
2481 ctxp->filename = saved;
2482 input_filename = saved_input_filename;
2486 /* Issue an error message at a current source line CL */
2489 parse_error_context VPROTO ((tree cl, const char *msg, ...))
2491 #ifndef ANSI_PROTOTYPES
2498 #ifndef ANSI_PROTOTYPES
2499 cl = va_arg (ap, tree);
2500 msg = va_arg (ap, const char *);
2502 issue_warning_error_from_context (cl, msg, ap);
2506 /* Issue a warning at a current source line CL */
2509 parse_warning_context VPROTO ((tree cl, const char *msg, ...))
2511 #ifndef ANSI_PROTOTYPES
2518 #ifndef ANSI_PROTOTYPES
2519 cl = va_arg (ap, tree);
2520 msg = va_arg (ap, const char *);
2523 force_error = do_warning = 1;
2524 issue_warning_error_from_context (cl, msg, ap);
2525 do_warning = force_error = 0;
2530 find_expr_with_wfl (node)
2538 switch (TREE_CODE (node))
2541 return find_expr_with_wfl (BLOCK_EXPR_BODY (node));
2544 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
2547 to_return = find_expr_with_wfl (TREE_OPERAND (node, 1));
2551 return find_expr_with_wfl (TREE_OPERAND (node, 0));
2553 case LABELED_BLOCK_EXPR:
2554 return find_expr_with_wfl (TREE_OPERAND (node, 1));
2556 code = TREE_CODE_CLASS (TREE_CODE (node));
2557 if (((code == '1') || (code == '2') || (code == 'e'))
2558 && EXPR_WFL_LINECOL (node))
2566 /* Issue a missing return statement error. Uses METHOD to figure the
2567 last line of the method the error occurs in. */
2570 missing_return_error (method)
2573 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
2574 parse_error_context (wfl_operator, "Missing return statement");
2577 /* Issue an unreachable statement error. From NODE, find the next
2578 statement to report appropriately. */
2580 unreachable_stmt_error (node)
2583 /* Browse node to find the next expression node that has a WFL. Use
2584 the location to report the error */
2585 if (TREE_CODE (node) == COMPOUND_EXPR)
2586 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
2588 node = find_expr_with_wfl (node);
2592 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
2593 parse_error_context (wfl_operator, "Unreachable statement");
2596 fatal ("Can't get valid statement - unreachable_stmt_error");
2600 java_report_errors ()
2602 if (java_error_count)
2603 fprintf (stderr, "%d error%s",
2604 java_error_count, (java_error_count == 1 ? "" : "s"));
2605 if (java_warning_count)
2606 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
2607 java_warning_count, (java_warning_count == 1 ? "" : "s"));
2608 if (java_error_count || java_warning_count)
2609 putc ('\n', stderr);
2610 return java_error_count;
2614 java_accstring_lookup (flags)
2617 static char buffer [80];
2618 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
2620 /* Access modifier looked-up first for easier report on forbidden
2622 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
2623 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
2624 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
2625 if (flags & ACC_STATIC) COPY_RETURN ("static");
2626 if (flags & ACC_FINAL) COPY_RETURN ("final");
2627 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
2628 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
2629 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
2630 if (flags & ACC_NATIVE) COPY_RETURN ("native");
2631 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
2632 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
2639 /* Issuing error messages upon redefinition of classes, interfaces or
2643 classitf_redefinition_error (context, id, decl, cl)
2647 parse_error_context (cl, "%s `%s' already defined in %s:%d",
2648 context, IDENTIFIER_POINTER (id),
2649 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
2650 /* Here we should point out where its redefined. It's a unicode. FIXME */
2654 variable_redefinition_error (context, name, type, line)
2655 tree context, name, type;
2660 /* Figure a proper name for type. We might haven't resolved it */
2661 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
2662 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
2664 type_name = lang_printable_name (type, 0);
2666 parse_error_context (context,
2667 "Variable `%s' is already defined in this method and "
2668 "was declared `%s %s' at line %d",
2669 IDENTIFIER_POINTER (name),
2670 type_name, IDENTIFIER_POINTER (name), line);
2674 build_array_from_name (type, type_wfl, name, ret_name)
2675 tree type, type_wfl, name, *ret_name;
2680 /* Eventually get more dims */
2681 string = IDENTIFIER_POINTER (name);
2682 while (string [more_dims] == '[')
2685 /* If we have, then craft a new type for this variable */
2688 name = get_identifier (&more_dims [string]);
2690 /* If we have a pointer, use its type */
2691 if (TREE_CODE (type) == POINTER_TYPE)
2692 type = TREE_TYPE (type);
2694 /* Building the first dimension of a primitive type uses this
2696 if (JPRIMITIVE_TYPE_P (type))
2698 type = build_java_array_type (type, -1);
2699 CLASS_LOADED_P (type) = 1;
2702 /* Otherwise, if we have a WFL for this type, use it (the type
2703 is already an array on an unresolved type, and we just keep
2704 on adding dimensions) */
2708 /* Add all the dimensions */
2710 type = build_unresolved_array_type (type);
2712 /* The type may have been incomplete in the first place */
2714 type = obtain_incomplete_type (type);
2721 /* Build something that the type identifier resolver will identify as
2722 being an array to an unresolved type. TYPE_WFL is a WFL on a
2726 build_unresolved_array_type (type_or_wfl)
2731 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
2732 just create a array type */
2733 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
2735 tree type = build_java_array_type (type_or_wfl, -1);
2736 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
2740 obstack_1grow (&temporary_obstack, '[');
2741 obstack_grow0 (&temporary_obstack,
2742 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
2743 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
2744 ptr = obstack_finish (&temporary_obstack);
2745 return build_expr_wfl (get_identifier (ptr),
2746 EXPR_WFL_FILENAME (type_or_wfl),
2747 EXPR_WFL_LINENO (type_or_wfl),
2748 EXPR_WFL_COLNO (type_or_wfl));
2751 /* Check modifiers. If one doesn't fit, retrieve it in its declaration line
2752 and point it out. */
2755 check_modifiers (message, value, mask)
2760 /* Should point out the one that don't fit. ASCII/unicode,
2761 going backward. FIXME */
2764 int i, remainder = value & ~mask;
2765 for (i = 0; i <= 10; i++)
2766 if ((1 << i) & remainder)
2767 parse_error_context (ctxp->modifier_ctx [i], message,
2768 java_accstring_lookup (1 << i));
2773 parser_add_interface (class_decl, interface_decl, wfl)
2774 tree class_decl, interface_decl, wfl;
2776 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
2777 parse_error_context (wfl, "Interface `%s' repeated",
2778 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
2781 /* Bulk of common class/interface checks. Return 1 if an error was
2782 encountered. TAG is 0 for a class, 1 for an interface. */
2785 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
2786 int is_interface, flags;
2787 tree raw_name, qualified_name, decl, cl;
2792 fprintf (stderr, " %s %s", (is_interface ? "interface" : "class"),
2793 IDENTIFIER_POINTER (qualified_name));
2795 /* Scope of an interface/class type name:
2796 - Can't be imported by a single type import
2797 - Can't already exists in the package */
2798 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
2799 && (node = find_name_in_single_imports (raw_name)))
2802 (cl, "%s name `%s' clashes with imported type `%s'",
2803 (is_interface ? "Interface" : "Class"),
2804 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
2807 if (decl && CLASS_COMPLETE_P (decl))
2809 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
2810 qualified_name, decl, cl);
2814 /* If public, file name should match class/interface name */
2815 if (flags & ACC_PUBLIC)
2819 /* Contains OS dependent assumption on path separator. FIXME */
2820 for (f = &input_filename [strlen (input_filename)];
2821 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
2824 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
2826 if (strncmp (IDENTIFIER_POINTER (raw_name),
2827 f , IDENTIFIER_LENGTH (raw_name)) ||
2828 f [IDENTIFIER_LENGTH (raw_name)] != '.')
2829 parse_error_context (cl, "Public %s `%s' must be defined in a file "
2831 (is_interface ? "interface" : "class"),
2832 IDENTIFIER_POINTER (qualified_name),
2833 IDENTIFIER_POINTER (raw_name));
2836 check_modifiers ((is_interface ?
2837 "Illegal modifier `%s' for interface declaration" :
2838 "Illegal modifier `%s' for class declaration"), flags,
2839 (is_interface ? INTERFACE_MODIFIERS : CLASS_MODIFIERS));
2843 /* If DECL is NULL, create and push a new DECL, record the current
2844 line CL and do other maintenance things. */
2847 maybe_create_class_interface_decl (decl, qualified_name, cl)
2848 tree decl, qualified_name, cl;
2851 decl = push_class (make_class (), qualified_name);
2853 /* Take care of the file and line business */
2854 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
2855 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
2856 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
2857 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
2858 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
2860 ctxp->current_parsed_class = decl;
2862 /* Link the declaration to the already seen ones */
2863 TREE_CHAIN (decl) = ctxp->class_list;
2864 ctxp->class_list = decl;
2866 /* Create a new nodes in the global lists */
2867 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
2868 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
2870 /* Install a new dependency list element */
2871 create_jdep_list (ctxp);
2873 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
2874 IDENTIFIER_POINTER (qualified_name)));
2879 add_superinterfaces (decl, interface_list)
2880 tree decl, interface_list;
2883 /* Superinterface(s): if present and defined, parser_check_super_interface ()
2884 takes care of ensuring that:
2885 - This is an accessible interface type,
2886 - Circularity detection.
2887 parser_add_interface is then called. If present but not defined,
2888 the check operation is delayed until the super interface gets
2890 for (node = interface_list; node; node = TREE_CHAIN (node))
2892 tree current = TREE_PURPOSE (node);
2893 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
2894 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
2896 if (!parser_check_super_interface (idecl, decl, current))
2897 parser_add_interface (decl, idecl, current);
2900 register_incomplete_type (JDEP_INTERFACE,
2901 current, decl, NULL_TREE);
2905 /* Create an interface in pass1 and return its decl. Return the
2906 interface's decl in pass 2. */
2909 create_interface (flags, id, super)
2913 tree raw_name = EXPR_WFL_NODE (id);
2914 tree q_name = parser_qualified_classname (id);
2915 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
2917 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
2919 /* Basic checks: scope, redefinition, modifiers */
2920 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
2923 /* Interface modifiers check
2924 - public/abstract allowed (already done at that point)
2925 - abstract is obsolete (comes first, it's a warning, or should be)
2926 - Can't use twice the same (checked in the modifier rule) */
2927 if ((flags & ACC_ABSTRACT) && flag_redundant)
2928 parse_warning_context
2929 (MODIFIER_WFL (ABSTRACT_TK),
2930 "Redundant use of `abstract' modifier. Interface `%s' is implicitely "
2931 "abstract", IDENTIFIER_POINTER (raw_name));
2933 /* Create a new decl if DECL is NULL, otherwise fix it */
2934 decl = maybe_create_class_interface_decl (decl, q_name, id);
2936 /* Set super info and mark the class a complete */
2937 set_super_info (ACC_ABSTRACT | ACC_INTERFACE | flags, TREE_TYPE (decl),
2938 object_type_node, ctxp->interface_number);
2939 ctxp->interface_number = 0;
2940 CLASS_COMPLETE_P (decl) = 1;
2941 add_superinterfaces (decl, super);
2946 /* Create an class in pass1 and return its decl. Return class
2947 interface's decl in pass 2. */
2950 create_class (flags, id, super, interfaces)
2952 tree id, super, interfaces;
2954 tree raw_name = EXPR_WFL_NODE (id);
2955 tree class_id, decl;
2956 tree super_decl = NULL, super_decl_type;
2958 class_id = parser_qualified_classname (id);
2959 decl = IDENTIFIER_CLASS_VALUE (class_id);
2960 ctxp->current_parsed_class_un = EXPR_WFL_NODE (id);
2961 EXPR_WFL_NODE (id) = class_id;
2963 /* Basic check: scope, redefinition, modifiers */
2964 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
2967 /* Class modifier check:
2968 - Allowed modifier (already done at that point)
2969 - abstract AND final forbidden
2970 - Public classes defined in the correct file */
2971 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
2972 parse_error_context (id, "Class `%s' can't be declared both abstract "
2973 "and final", IDENTIFIER_POINTER (raw_name));
2975 /* Create a new decl if DECL is NULL, otherwise fix it */
2976 decl = maybe_create_class_interface_decl (decl, class_id, id);
2978 /* If SUPER exists, use it, otherwise use Object */
2981 /* Can't extend java.lang.Object */
2982 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
2984 parse_error_context (id, "Can't extend `java.lang.Object'");
2988 /* The class is known and exists if there is a decl. Otherwise,
2989 postpone the operation and do it later. */
2990 super_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (super));
2993 parser_check_super (super_decl, decl, id);
2994 super_decl_type = TREE_TYPE (super_decl);
2998 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3000 else if (TREE_TYPE (decl) != object_type_node)
3001 super_decl_type = object_type_node;
3002 /* We're defining java.lang.Object */
3004 super_decl_type = NULL_TREE;
3006 /* Set super info and mark the class a complete */
3007 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3008 ctxp->interface_number);
3009 ctxp->interface_number = 0;
3010 CLASS_COMPLETE_P (decl) = 1;
3011 add_superinterfaces (decl, interfaces);
3013 /* Eventually sets the @deprecated tag flag */
3014 CHECK_DEPRECATED (decl);
3019 /* Can't use lookup_field () since we don't want to load the class and
3020 can't set the CLASS_LOADED_P flag */
3023 find_field (class, name)
3028 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3030 if (DECL_NAME (decl) == name)
3036 /* Wrap around lookup_field that doesn't potentially upset the value
3040 lookup_field_wrapper (class, name)
3045 java_parser_context_save_global ();
3046 decl = lookup_field (&type, name);
3047 java_parser_context_restore_global ();
3051 /* Find duplicate field within the same class declarations and report
3052 the error. Returns 1 if a duplicated field was found, 0
3056 duplicate_declaration_error_p (new_field_name, new_type, cl)
3057 tree new_field_name, new_type, cl;
3059 /* This might be modified to work with method decl as well */
3060 tree decl = find_field (TREE_TYPE (ctxp->current_parsed_class),
3064 char *t1 = strdup (purify_type_name
3065 ((TREE_CODE (new_type) == POINTER_TYPE
3066 && TREE_TYPE (new_type) == NULL_TREE) ?
3067 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
3068 lang_printable_name (new_type, 1)));
3069 /* The type may not have been completed by the time we report
3071 char *t2 = strdup (purify_type_name
3072 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
3073 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
3074 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
3075 lang_printable_name (TREE_TYPE (decl), 1)));
3077 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
3078 t1, IDENTIFIER_POINTER (new_field_name),
3079 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
3080 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3088 /* Field registration routine. If TYPE doesn't exist, field
3089 declarations are linked to the undefined TYPE dependency list, to
3090 be later resolved in java_complete_class () */
3093 register_fields (flags, type, variable_list)
3095 tree type, variable_list;
3097 tree current, saved_type;
3098 tree class_type = TREE_TYPE (ctxp->current_parsed_class);
3099 int saved_lineno = lineno;
3101 tree wfl = NULL_TREE;
3103 /* If we're adding fields to interfaces, those fields are public,
3105 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
3107 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
3109 "%s", "interface field(s)");
3110 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
3112 "%s", "interface field(s)");
3113 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
3114 flags, ACC_FINAL, "%s", "interface field(s)");
3115 check_modifiers ("Illegal interface member modifier `%s'", flags,
3116 INTERFACE_FIELD_MODIFIERS);
3117 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
3120 /* Obtain a suitable type for resolution, if necessary */
3121 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
3123 /* If TYPE is fully resolved and we don't have a reference, make one */
3124 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3126 for (current = variable_list, saved_type = type; current;
3127 current = TREE_CHAIN (current), type = saved_type)
3131 tree cl = TREE_PURPOSE (current);
3132 tree init = TREE_VALUE (current);
3133 tree current_name = EXPR_WFL_NODE (cl);
3135 /* Process NAME, as it may specify extra dimension(s) for it */
3136 type = build_array_from_name (type, wfl, current_name, ¤t_name);
3138 /* Type adjustment. We may have just readjusted TYPE because
3139 the variable specified more dimensions. Make sure we have
3140 a reference if we can and don't have one already. Also
3141 change the name if we have an init. */
3142 if (type != saved_type)
3144 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3146 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
3149 real_type = GET_REAL_TYPE (type);
3150 /* Check for redeclarations */
3151 if (duplicate_declaration_error_p (current_name, real_type, cl))
3154 /* Set lineno to the line the field was found and create a
3155 declaration for it. Eventually sets the @deprecated tag flag. */
3156 lineno = EXPR_WFL_LINENO (cl);
3157 field_decl = add_field (class_type, current_name, real_type, flags);
3158 CHECK_DEPRECATED (field_decl);
3160 /* Check if we must chain. */
3162 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
3164 /* Default value of a static field is 0 and it is considered
3166 if (flags & ACC_STATIC)
3167 INITIALIZED_P (field_decl) = 1;
3169 /* If we have an initialization value tied to the field */
3172 /* The field is declared static */
3173 if (flags & ACC_STATIC)
3175 /* We include the field and its initialization part into
3176 a list used to generate <clinit>. After <clinit> is
3177 walked, field initializations will be processed and
3178 fields initialized with known constants will be taken
3179 out of <clinit> and have their DECL_INITIAL set
3181 TREE_CHAIN (init) = ctxp->static_initialized;
3182 ctxp->static_initialized = init;
3183 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
3184 if (TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
3185 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
3187 /* A non-static field declared with an immediate initialization is
3188 to be initialized in <init>, if any. This field is remembered
3189 to be processed at the time of the generation of <init>. */
3192 TREE_CHAIN (init) = ctxp->non_static_initialized;
3193 ctxp->non_static_initialized = init;
3195 INITIALIZED_P (field_decl) = 1;
3196 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
3199 lineno = saved_lineno;
3202 /* Generate the method $finit$ that initializes fields initialized
3203 upon declaration. */
3206 maybe_generate_finit ()
3208 tree mdecl, current;
3210 if (!ctxp->non_static_initialized || java_error_count)
3213 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3214 ACC_PRIVATE, void_type_node,
3215 finit_identifier_node, end_params_node);
3216 start_artificial_method_body (mdecl);
3218 ctxp->non_static_initialized = nreverse (ctxp->non_static_initialized);
3219 for (current = ctxp->non_static_initialized; current;
3220 current = TREE_CHAIN (current))
3221 java_method_add_stmt (mdecl,
3222 build_debugable_stmt (EXPR_WFL_LINECOL (current),
3225 end_artificial_method_body (mdecl);
3226 CLASS_HAS_FINIT_P (TREE_TYPE (ctxp->current_parsed_class)) = 1;
3227 ctxp->non_static_initialized = NULL_TREE;
3230 /* Check whether it is necessary to generate a <clinit> for the class
3234 maybe_generate_clinit ()
3238 if (!ctxp->static_initialized || java_error_count)
3241 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3242 ACC_STATIC, void_type_node,
3243 clinit_identifier_node, end_params_node);
3244 start_artificial_method_body (mdecl);
3246 /* Keep initialization in order to enforce 8.5 */
3247 ctxp->static_initialized = nreverse (ctxp->static_initialized);
3249 /* We process the list of assignment we produced as the result of
3250 the declaration of initialized static field and add them as
3251 statement to the <clinit> method. */
3252 for (c = ctxp->static_initialized; c; c = TREE_CHAIN (c))
3254 /* We build the assignment expression that will initialize the
3255 field to its value. There are strict rules on static
3256 initializers (8.5). FIXME */
3257 java_method_add_stmt (mdecl,
3258 build_debugable_stmt (EXPR_WFL_LINECOL (c), c));
3261 end_artificial_method_body (mdecl);
3262 ctxp->static_initialized = NULL_TREE;
3265 /* Shared accros method_declarator and method_header to remember the
3266 patch stage that was reached during the declaration of the method.
3267 A method DECL is built differently is there is no patch
3268 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
3269 pending on the currently defined method. */
3271 static int patch_stage;
3273 /* Check the method declaration and add the method to its current
3274 class. If the argument list is known to contain incomplete types,
3275 the method is partially added and the registration will be resume
3276 once the method arguments resolved. If TYPE is NULL, we're dealing
3277 with a constructor. */
3280 method_header (flags, type, mdecl, throws)
3282 tree type, mdecl, throws;
3284 tree meth = TREE_VALUE (mdecl);
3285 tree id = TREE_PURPOSE (mdecl);
3286 tree this_class = TREE_TYPE (ctxp->current_parsed_class);
3287 tree type_wfl = NULL_TREE;
3288 tree meth_name = NULL_TREE, current, orig_arg;
3290 int constructor_ok = 0, must_chain;
3292 check_modifiers_consistency (flags);
3294 /* There are some forbidden modifiers for an abstract method and its
3295 class must be abstract as well. */
3296 if (type && (flags & ACC_ABSTRACT))
3298 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
3299 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
3300 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
3301 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
3302 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
3303 if (!CLASS_ABSTRACT (TYPE_NAME (this_class)))
3305 (id, "Class `%s' must be declared abstract to define abstract "
3307 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
3308 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3310 /* Things to be checked when declaring a constructor */
3313 int ec = java_error_count;
3314 /* 8.6: Constructor declarations: we might be trying to define a
3315 method without specifying a return type. */
3316 if (EXPR_WFL_NODE (id) != ctxp->current_parsed_class_un)
3318 (id, "Invalid method declaration, return type required");
3319 /* 8.6.3: Constructor modifiers */
3322 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
3323 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
3324 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
3325 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
3326 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
3328 /* If we found error here, we don't consider it's OK to tread
3329 the method definition as a constructor, for the rest of this
3331 if (ec == java_error_count)
3335 /* Method declared within the scope of an interface are implicitly
3336 abstract and public. Conflicts with other erroneously provided
3337 modifiers are check right after. */
3339 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
3341 /* If FLAGS isn't set because of a modifier, turn the
3342 corresponding modifier WFL to NULL so we issue a warning on
3343 the obsolete use of the modifier */
3344 if (!(flags & ACC_PUBLIC))
3345 MODIFIER_WFL (PUBLIC_TK) = NULL;
3346 if (!(flags & ACC_ABSTRACT))
3347 MODIFIER_WFL (ABSTRACT_TK) = NULL;
3348 flags |= ACC_PUBLIC;
3349 flags |= ACC_ABSTRACT;
3352 /* Modifiers context reset moved up, so abstract method declaration
3353 modifiers can be later checked. */
3355 /* Set constructor returned type to void and method name to <init>,
3356 unless we found an error identifier the constructor (in which
3357 case we retain the original name) */
3360 type = void_type_node;
3362 meth_name = init_identifier_node;
3365 meth_name = EXPR_WFL_NODE (id);
3367 /* Do the returned type resolution and registration if necessary */
3368 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3371 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
3372 EXPR_WFL_NODE (id) = meth_name;
3373 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3377 patch_stage = JDEP_METHOD_RETURN;
3378 register_incomplete_type (patch_stage, type_wfl, id, type);
3379 TREE_TYPE (meth) = GET_REAL_TYPE (type);
3382 TREE_TYPE (meth) = type;
3384 saved_lineno = lineno;
3385 /* When defining an abstract or interface method, the curly
3386 bracket at level 1 doesn't exist because there is no function
3388 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
3389 EXPR_WFL_LINENO (id));
3391 /* Remember the original argument list */
3392 orig_arg = TYPE_ARG_TYPES (meth);
3394 if (patch_stage) /* includes ret type and/or all args */
3397 meth = add_method_1 (this_class, flags, meth_name, meth);
3398 /* Patch for the return type */
3399 if (patch_stage == JDEP_METHOD_RETURN)
3401 jdep = CLASSD_LAST (ctxp->classd_list);
3402 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
3404 /* This is the stop JDEP. METH allows the function's signature
3406 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
3409 meth = add_method (this_class, flags, meth_name,
3410 build_java_signature (meth));
3412 /* Fix the method argument list so we have the argument name
3414 fix_method_argument_names (orig_arg, meth);
3416 /* Register the parameter number and re-install the current line
3418 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
3419 lineno = saved_lineno;
3421 /* Register exception specified by the `throws' keyword for
3422 resolution and set the method decl appropriate field to the list.
3423 Note: the grammar ensures that what we get here are class
3427 throws = nreverse (throws);
3428 for (current = throws; current; current = TREE_CHAIN (current))
3430 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
3431 NULL_TREE, NULL_TREE);
3432 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
3433 &TREE_VALUE (current);
3435 DECL_FUNCTION_THROWS (meth) = throws;
3438 /* We set the DECL_NAME to ID so we can track the location where
3439 the function was declared. This allow us to report
3440 redefinition error accurately. When method are verified,
3441 DECL_NAME is reinstalled properly (using the content of the
3442 WFL node ID) (see check_method_redefinition). We don't do that
3443 when Object is being defined. Constructor <init> names will be
3444 reinstalled the same way. */
3445 if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node)
3446 DECL_NAME (meth) = id;
3448 /* Set the flag if we correctly processed a constructor */
3450 DECL_CONSTRUCTOR_P (meth) = 1;
3452 /* Eventually set the @deprecated tag flag */
3453 CHECK_DEPRECATED (meth);
3459 fix_method_argument_names (orig_arg, meth)
3460 tree orig_arg, meth;
3462 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
3463 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
3465 TREE_PURPOSE (arg) = this_identifier_node;
3466 arg = TREE_CHAIN (arg);
3468 while (orig_arg != end_params_node)
3470 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
3471 orig_arg = TREE_CHAIN (orig_arg);
3472 arg = TREE_CHAIN (arg);
3476 /* Complete the method declaration with METHOD_BODY. */
3479 complete_method_declaration (method_body)
3482 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
3483 maybe_absorb_scoping_blocks ();
3484 /* Exit function's body */
3486 /* Merge last line of the function with first line, directly in the
3487 function decl. It will be used to emit correct debug info. */
3488 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
3491 /* Build a an error message for constructor circularity errors. */
3494 constructor_circularity_msg (from, to)
3497 static char string [4096];
3498 char *t = strdup (lang_printable_name (from, 0));
3499 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
3504 /* Verify a circular call to METH. Return 1 if an error is found, 0
3508 verify_constructor_circularity (meth, current)
3511 static tree list = NULL_TREE;
3513 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3515 if (TREE_VALUE (c) == meth)
3521 list = nreverse (list);
3522 for (liste = list; liste; liste = TREE_CHAIN (liste))
3525 (TREE_PURPOSE (TREE_PURPOSE (liste)),
3526 constructor_circularity_msg
3527 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
3531 t = strdup (lang_printable_name (meth, 0));
3532 parse_error_context (TREE_PURPOSE (c),
3533 "%s: recursive invocation of constructor `%s'",
3534 constructor_circularity_msg (current, meth), t);
3540 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3542 list = tree_cons (c, current, list);
3543 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
3545 list = TREE_CHAIN (list);
3550 /* Check modifiers that can be declared but exclusively */
3553 check_modifiers_consistency (flags)
3557 tree cl = NULL_TREE;
3559 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, 0, acc_count, cl);
3560 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, 1, acc_count, cl);
3561 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, 2, acc_count, cl);
3564 (cl, "Inconsistent member declaration. At most one of `public', "
3565 "`private', or `protected' may be specified");
3568 /* Check the methode header METH for abstract specifics features */
3571 check_abstract_method_header (meth)
3574 int flags = get_access_flags_from_decl (meth);
3575 /* DECL_NAME might still be a WFL node */
3576 tree name = GET_METHOD_NAME (meth);
3578 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (ABSTRACT_TK), flags,
3579 ACC_ABSTRACT, "abstract method `%s'",
3580 IDENTIFIER_POINTER (name));
3581 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), flags,
3582 ACC_PUBLIC, "abstract method `%s'",
3583 IDENTIFIER_POINTER (name));
3585 check_modifiers ("Illegal modifier `%s' for interface method",
3586 flags, INTERFACE_METHOD_MODIFIERS);
3589 /* Create a FUNCTION_TYPE node and start augmenting it with the
3590 declared function arguments. Arguments type that can't be resolved
3591 are left as they are, but the returned node is marked as containing
3592 incomplete types. */
3595 method_declarator (id, list)
3598 tree arg_types = NULL_TREE, current, node;
3599 tree meth = make_node (FUNCTION_TYPE);
3602 patch_stage = JDEP_NO_PATCH;
3604 for (current = list; current; current = TREE_CHAIN (current))
3607 tree wfl_name = TREE_PURPOSE (current);
3608 tree type = TREE_VALUE (current);
3609 tree name = EXPR_WFL_NODE (wfl_name);
3610 tree already, arg_node;
3611 tree type_wfl = NULL_TREE;
3614 /* Obtain a suitable type for resolution, if necessary */
3615 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3617 /* Process NAME, as it may specify extra dimension(s) for it */
3618 type = build_array_from_name (type, type_wfl, name, &name);
3619 EXPR_WFL_NODE (wfl_name) = name;
3621 real_type = GET_REAL_TYPE (type);
3622 if (TREE_CODE (real_type) == RECORD_TYPE)
3624 real_type = promote_type (real_type);
3625 if (TREE_CODE (type) == TREE_LIST)
3626 TREE_PURPOSE (type) = real_type;
3629 /* Check redefinition */
3630 for (already = arg_types; already; already = TREE_CHAIN (already))
3631 if (TREE_PURPOSE (already) == name)
3634 (wfl_name, "Variable `%s' is used more than once in the "
3635 "argument list of method `%s'", IDENTIFIER_POINTER (name),
3636 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3640 /* If we've an incomplete argument type, we know there is a location
3641 to patch when the type get resolved, later. */
3645 patch_stage = JDEP_METHOD;
3646 type = register_incomplete_type (patch_stage,
3647 type_wfl, wfl_name, type);
3648 jdep = CLASSD_LAST (ctxp->classd_list);
3649 JDEP_MISC (jdep) = id;
3652 /* The argument node: a name and a (possibly) incomplete type */
3653 arg_node = build_tree_list (name, real_type);
3655 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
3656 TREE_CHAIN (arg_node) = arg_types;
3657 arg_types = arg_node;
3659 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
3660 node = build_tree_list (id, meth);
3665 unresolved_type_p (wfl, returned)
3670 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
3672 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
3674 *returned = (decl ? TREE_TYPE (decl) : NULL_TREE);
3682 /* From NAME, build a qualified identifier node using the
3683 qualification from the current package definition. */
3686 parser_qualified_classname (name)
3690 return merge_qualified_name (ctxp->package, EXPR_WFL_NODE (name));
3692 return EXPR_WFL_NODE (name);
3695 /* Called once the type a interface extends is resolved. Returns 0 if
3696 everything is OK. */
3699 parser_check_super_interface (super_decl, this_decl, this_wfl)
3700 tree super_decl, this_decl, this_wfl;
3702 tree super_type = TREE_TYPE (super_decl);
3704 /* Has to be an interface */
3705 if (!CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (super_decl))))
3708 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
3709 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
3710 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
3711 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
3712 "interface" : "class"),
3713 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
3717 /* Check scope: same package OK, other package: OK if public */
3718 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
3721 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
3722 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3723 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3727 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
3728 0 if everthing is OK. */
3731 parser_check_super (super_decl, this_decl, wfl)
3732 tree super_decl, this_decl, wfl;
3734 tree super_type = TREE_TYPE (super_decl);
3736 /* SUPER should be a CLASS (neither an array nor an interface) */
3737 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
3740 (wfl, "Class `%s' can't subclass %s `%s'",
3741 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3742 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
3743 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3747 if (CLASS_FINAL (TYPE_NAME (super_type)))
3749 parse_error_context (wfl, "Can't subclass final classes: %s",
3750 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3754 /* Check scope: same package OK, other package: OK if public */
3755 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
3758 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
3759 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3760 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3764 /* Create a new dependency list and link it (in a LIFO manner) to the
3765 CTXP list of type dependency list. */
3768 create_jdep_list (ctxp)
3769 struct parser_ctxt *ctxp;
3771 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
3772 new->first = new->last = NULL;
3773 new->next = ctxp->classd_list;
3774 ctxp->classd_list = new;
3778 reverse_jdep_list (ctxp)
3779 struct parser_ctxt *ctxp;
3781 register jdeplist *prev = NULL, *current, *next;
3782 for (current = ctxp->classd_list; current; current = next)
3784 next = current->next;
3785 current->next = prev;
3791 /* Create a fake pointer based on the ID stored in
3792 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
3793 registered again. */
3796 obtain_incomplete_type (type_name)
3801 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
3802 name = EXPR_WFL_NODE (type_name);
3803 else if (INCOMPLETE_TYPE_P (type_name))
3804 name = TYPE_NAME (type_name);
3806 fatal ("invalid type name - obtain_incomplete_type");
3808 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
3809 if (TYPE_NAME (TREE_PURPOSE (ptr)) == name)
3815 push_obstacks (&permanent_obstack, &permanent_obstack);
3816 BUILD_PTR_FROM_NAME (core, name);
3818 ptr = build_tree_list (core, NULL_TREE);
3820 TREE_CHAIN (ptr) = ctxp->incomplete_class;
3821 ctxp->incomplete_class = ptr;
3827 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
3828 non NULL instead of computing a new fake type based on WFL. The new
3829 dependency is inserted in the current type dependency list, in FIFO
3833 register_incomplete_type (kind, wfl, decl, ptr)
3835 tree wfl, decl, ptr;
3837 jdep *new = (jdep *)xmalloc (sizeof (jdep));
3839 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
3840 ptr = obtain_incomplete_type (wfl);
3842 JDEP_KIND (new) = kind;
3843 JDEP_DECL (new) = decl;
3844 JDEP_SOLV (new) = ptr;
3845 JDEP_WFL (new) = wfl;
3846 JDEP_CHAIN (new) = NULL;
3847 JDEP_MISC (new) = NULL_TREE;
3848 JDEP_GET_PATCH (new) = (tree *)NULL;
3850 JDEP_INSERT (ctxp->classd_list, new);
3856 java_check_circular_reference ()
3859 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
3861 tree type = TREE_TYPE (current);
3862 if (CLASS_INTERFACE (TYPE_NAME (type)))
3864 /* Check all interfaces this class extends */
3865 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
3870 n = TREE_VEC_LENGTH (basetype_vec);
3871 for (i = 0; i < n; i++)
3873 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
3874 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
3875 && interface_of_p (type, BINFO_TYPE (vec_elt)))
3876 parse_error_context (lookup_cl (current),
3877 "Cyclic interface inheritance");
3881 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
3882 parse_error_context (lookup_cl (current),
3883 "Cyclic class inheritance");
3887 /* safe_layout_class just makes sure that we can load a class without
3888 disrupting the current_class, input_file, lineno, etc, information
3889 about the class processed currently. */
3892 safe_layout_class (class)
3895 tree save_current_class = current_class;
3896 char *save_input_filename = input_filename;
3897 int save_lineno = lineno;
3899 push_obstacks (&permanent_obstack, &permanent_obstack);
3901 layout_class (class);
3904 current_class = save_current_class;
3905 input_filename = save_input_filename;
3906 lineno = save_lineno;
3907 CLASS_LOADED_P (class) = 1;
3911 jdep_resolve_class (dep)
3916 if (JDEP_RESOLVED_P (dep))
3917 decl = JDEP_RESOLVED_DECL (dep);
3920 decl = resolve_class (JDEP_TO_RESOLVE (dep),
3921 JDEP_DECL (dep), JDEP_WFL (dep));
3922 JDEP_RESOLVED (dep, decl);
3926 complete_class_report_errors (dep);
3931 /* Complete unsatisfied class declaration and their dependencies */
3934 java_complete_class ()
3941 push_obstacks (&permanent_obstack, &permanent_obstack);
3943 /* Process imports and reverse the import on demand list */
3945 if (ctxp->import_demand_list)
3946 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
3948 /* Rever things so we have the right order */
3949 ctxp->class_list = nreverse (ctxp->class_list);
3950 ctxp->classd_list = reverse_jdep_list (ctxp);
3952 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
3954 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
3957 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
3960 if (!(decl = jdep_resolve_class (dep)))
3963 /* Now it's time to patch */
3964 switch (JDEP_KIND (dep))
3967 /* Simply patch super */
3968 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
3970 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
3971 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
3976 /* We do part of the job done in add_field */
3977 tree field_decl = JDEP_DECL (dep);
3978 tree field_type = TREE_TYPE (decl);
3979 push_obstacks (&permanent_obstack, &permanent_obstack);
3980 if (TREE_CODE (field_type) == RECORD_TYPE)
3981 field_type = promote_type (field_type);
3983 TREE_TYPE (field_decl) = field_type;
3984 DECL_ALIGN (field_decl) = 0;
3985 layout_decl (field_decl, 0);
3986 SOURCE_FRONTEND_DEBUG
3987 (("Completed field/var decl `%s' with `%s'",
3988 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
3989 IDENTIFIER_POINTER (DECL_NAME (decl))));
3992 case JDEP_METHOD: /* We start patching a method */
3993 case JDEP_METHOD_RETURN:
3999 type = TREE_TYPE(decl);
4000 if (TREE_CODE (type) == RECORD_TYPE)
4001 type = promote_type (type);
4002 JDEP_APPLY_PATCH (dep, type);
4003 SOURCE_FRONTEND_DEBUG
4004 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
4005 "Completing fct `%s' with ret type `%s'":
4006 "Completing arg `%s' with type `%s'"),
4007 IDENTIFIER_POINTER (EXPR_WFL_NODE
4008 (JDEP_DECL_WFL (dep))),
4009 IDENTIFIER_POINTER (DECL_NAME (decl))));
4013 dep = JDEP_CHAIN (dep);
4014 if (JDEP_KIND (dep) == JDEP_METHOD_END)
4017 decl = jdep_resolve_class (dep);
4021 tree mdecl = JDEP_DECL (dep), signature;
4022 push_obstacks (&permanent_obstack, &permanent_obstack);
4023 /* Recompute and reset the signature */
4024 signature = build_java_signature (TREE_TYPE (mdecl));
4025 set_java_signature (TREE_TYPE (mdecl), signature);
4032 case JDEP_INTERFACE:
4033 if (parser_check_super_interface (decl, JDEP_DECL (dep),
4036 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
4041 type = TREE_TYPE(decl);
4042 if (TREE_CODE (type) == RECORD_TYPE)
4043 type = promote_type (type);
4044 JDEP_APPLY_PATCH (dep, type);
4048 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4049 SOURCE_FRONTEND_DEBUG
4050 (("Completing a random type dependency on a '%s' node",
4051 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
4054 case JDEP_EXCEPTION:
4055 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4056 SOURCE_FRONTEND_DEBUG
4057 (("Completing `%s' `throws' argument node",
4058 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
4062 fatal ("Can't handle patch code %d - java_complete_class",
4071 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
4075 resolve_class (class_type, decl, cl)
4076 tree class_type, decl, cl;
4078 char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
4080 tree resolved_type, resolved_type_decl;
4082 /* 1- Check to see if we have an array. If true, find what we really
4084 while (name[0] == '[')
4087 TYPE_NAME (class_type) = get_identifier (name);
4089 /* 2- Resolve the bare type */
4090 if (!(resolved_type_decl = do_resolve_class (class_type, decl, cl)))
4092 resolved_type = TREE_TYPE (resolved_type_decl);
4094 /* 3- If we have and array, reconstruct the array down to its nesting */
4097 while (base != name)
4099 if (TREE_CODE (resolved_type) == RECORD_TYPE)
4100 resolved_type = promote_type (resolved_type);
4101 resolved_type = build_java_array_type (resolved_type, -1);
4102 CLASS_LOADED_P (resolved_type) = 1;
4105 /* Build a fake decl for this, since this is what is expected to
4107 resolved_type_decl =
4108 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
4109 /* Figure how those two things are important for error report. FIXME */
4110 DECL_SOURCE_LINE (resolved_type_decl) = 0;
4111 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
4113 return resolved_type_decl;
4116 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
4117 are used to report error messages. */
4120 do_resolve_class (class_type, decl, cl)
4125 tree new_class_decl;
4126 tree original_name = NULL_TREE;
4128 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
4129 its is changed by find_in_imports{_on_demand} */
4131 /* 1- Check for the type in single imports */
4132 if (find_in_imports (class_type))
4135 /* 2- And check for the type in the current compilation unit. If it fails,
4136 try with a name qualified with the package name if appropriate. */
4137 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4139 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4140 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4141 load_class (TYPE_NAME (class_type), 0);
4142 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4145 original_name = TYPE_NAME (class_type);
4146 if (!QUALIFIED_P (TYPE_NAME (class_type)) && ctxp->package)
4147 TYPE_NAME (class_type) = merge_qualified_name (ctxp->package,
4148 TYPE_NAME (class_type));
4150 if (!(new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4151 load_class (TYPE_NAME (class_type), 0);
4152 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4154 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4155 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4156 load_class (TYPE_NAME (class_type), 0);
4157 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4160 new_name = TYPE_NAME (class_type);
4161 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_name)) != NULL_TREE)
4163 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4164 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4165 load_class (new_name, 0);
4166 return IDENTIFIER_CLASS_VALUE (new_name);
4170 tree class = read_class (new_name);
4171 if (class != NULL_TREE)
4173 tree decl = IDENTIFIER_CLASS_VALUE (new_name);
4174 if (decl == NULL_TREE)
4175 decl = push_class (class, new_name);
4180 TYPE_NAME (class_type) = original_name;
4182 /* 3- Check an other compilation unit that bears the name of type */
4183 load_class (TYPE_NAME (class_type), 0);
4184 if (check_pkg_class_access (TYPE_NAME (class_type),
4185 (cl ? cl : lookup_cl (decl))))
4188 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4189 return new_class_decl;
4191 /* 4- Check the import on demands. Don't allow bar.baz to be
4192 imported from foo.* */
4193 if (!QUALIFIED_P (TYPE_NAME (class_type)))
4194 if (find_in_imports_on_demand (class_type))
4197 /* 5- Last call for a resolution */
4198 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4201 /* Resolve NAME and lay it out (if not done and if not the current
4202 parsed class). Return a decl node. This function is meant to be
4203 called when type resolution is necessary during the walk pass. */
4206 resolve_and_layout (something, cl)
4212 /* Don't do that on the current class */
4213 if (something == current_class)
4214 return TYPE_NAME (current_class);
4216 /* Don't do anything for void and other primitive types */
4217 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4220 /* Pointer types can be reall pointer types or fake pointers. When
4221 finding a real pointer, recheck for primitive types */
4222 if (TREE_CODE (something) == POINTER_TYPE)
4224 if (TREE_TYPE (something))
4226 something = TREE_TYPE (something);
4227 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4231 something = TYPE_NAME (something);
4234 /* Don't do anything for arrays of primitive types */
4235 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
4236 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
4239 /* If something is not and IDENTIFIER_NODE, it can be a a TYPE_DECL
4241 if (TREE_CODE (something) != IDENTIFIER_NODE)
4242 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
4243 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
4245 if (!(decl = resolve_no_layout (something, cl)))
4248 /* Resolve and layout if necessary */
4249 layout_class_methods (TREE_TYPE (decl));
4250 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl)))
4251 CHECK_METHODS (decl);
4252 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
4253 safe_layout_class (TREE_TYPE (decl));
4258 /* Resolve a class, returns its decl but doesn't perform any
4259 layout. The current parsing context is saved and restored */
4262 resolve_no_layout (name, cl)
4266 BUILD_PTR_FROM_NAME (ptr, name);
4267 java_parser_context_save_global ();
4268 decl = resolve_class (ptr, NULL_TREE, cl);
4269 java_parser_context_restore_global ();
4274 /* Called when reporting errors. Skip leader '[' in a complex array
4275 type description that failed to be resolved. */
4278 purify_type_name (name)
4281 while (*name && *name == '[')
4286 /* The type CURRENT refers to can't be found. We print error messages. */
4289 complete_class_report_errors (dep)
4294 if (!JDEP_WFL (dep))
4297 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
4298 switch (JDEP_KIND (dep))
4302 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
4303 purify_type_name (name),
4304 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4308 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
4309 purify_type_name (name),
4310 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4312 case JDEP_METHOD: /* Covers arguments */
4314 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4315 "argument `%s' of method `%s'",
4316 purify_type_name (name),
4317 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
4318 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
4320 case JDEP_METHOD_RETURN: /* Covers return type */
4322 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4323 "return type of method `%s'",
4324 purify_type_name (name),
4325 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
4327 case JDEP_INTERFACE:
4329 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
4330 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
4331 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
4332 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4336 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4337 "local variable `%s'",
4338 purify_type_name (IDENTIFIER_POINTER
4339 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
4340 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4342 case JDEP_EXCEPTION: /* As specified by `throws' */
4344 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
4345 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
4348 /* Fix for -Wall. Just break doing nothing. The error will be
4354 /* Check uninitialized final. */
4361 /* Return a static string containing the DECL prototype string. If
4362 DECL is a constructor, use the class name instead of the form
4366 get_printable_method_name (decl)
4372 if (DECL_CONSTRUCTOR_P (decl))
4374 name = DECL_NAME (decl);
4375 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
4378 to_return = lang_printable_name (decl, 0);
4379 if (DECL_CONSTRUCTOR_P (decl))
4380 DECL_NAME (decl) = name;
4385 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
4386 nevertheless needs to be verfied, 1 otherwise. */
4389 reset_method_name (method)
4392 if (DECL_NAME (method) != clinit_identifier_node
4393 && DECL_NAME (method) != finit_identifier_node)
4395 /* NAME is just the plain name when Object is being defined */
4396 if (DECL_CONTEXT (method) != object_type_node)
4397 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
4398 init_identifier_node : GET_METHOD_NAME (method));
4405 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
4408 java_get_real_method_name (method_decl)
4411 tree method_name = DECL_NAME (method_decl);
4412 if (DECL_CONSTRUCTOR_P (method_decl))
4413 return init_identifier_node;
4415 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
4416 and still can be a constructor. FIXME */
4418 /* Don't confuse method only bearing the name of their class as
4420 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
4422 && ctxp->current_parsed_class_un == EXPR_WFL_NODE (method_name)
4423 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
4424 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
4425 return init_identifier_node;
4427 return EXPR_WFL_NODE (method_name);
4430 /* Track method being redefined inside the same class. As a side
4431 effect, set DECL_NAME to an IDENTIFIER (prior entering this
4432 function it's a FWL, so we can track errors more accurately */
4435 check_method_redefinition (class, method)
4439 tree cl = DECL_NAME (method);
4440 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
4441 /* decl name of artificial <clinit> and $finit$ doesn't need to be
4442 fixed and checked */
4444 /* Reset the method name before running the check. If it returns 1,
4445 the method doesn't need to be verified with respect to method
4446 redeclaration and we return 0 */
4447 if (reset_method_name (method))
4450 name = DECL_NAME (method);
4451 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
4453 if (redef == method)
4455 if (DECL_NAME (redef) == name
4456 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
4459 (cl, "Duplicate %s declaration `%s'",
4460 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
4461 get_printable_method_name (redef));
4468 /* Check all the methods of CLASS. Methods are first completed then
4469 checked according to regular method existance rules.
4470 If no constructor were encountered, then build its declaration. */
4473 java_check_regular_methods (class_decl)
4476 int saw_constructor = 0;
4478 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
4479 tree super_class = CLASSTYPE_SUPER (class);
4480 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
4483 /* It is not necessary to check methods defined in java.lang.Object */
4484 if (class == object_type_node)
4487 if (!TYPE_NVIRTUALS (class))
4488 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4490 /* Should take interfaces into account. FIXME */
4491 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
4494 tree method_wfl = DECL_NAME (method);
4497 /* If we previously found something and its name was saved,
4499 if (found && saved_found_wfl)
4501 DECL_NAME (found) = saved_found_wfl;
4502 saved_found_wfl = NULL_TREE;
4505 /* Check for redefinitions */
4506 if (check_method_redefinition (class, method))
4509 /* If we see one constructor a mark so we don't generate the
4510 default one. Also skip other verifications: constructors
4511 can't be inherited hence hiden or overriden */
4512 if (DECL_CONSTRUCTOR_P (method))
4514 saw_constructor = 1;
4518 /* We verify things thrown by the method. They must inherits from
4519 java.lang.Throwable */
4520 for (mthrows = DECL_FUNCTION_THROWS (method);
4521 mthrows; mthrows = TREE_CHAIN (mthrows))
4523 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
4525 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be "
4526 "a subclass of class `java.lang.Throwable'",
4528 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
4531 sig = build_java_argument_signature (TREE_TYPE (method));
4532 found = lookup_argument_method (super_class, DECL_NAME (method), sig);
4534 /* Nothing overrides or it's a private method. */
4537 if (METHOD_PRIVATE (found))
4543 /* If found wasn't verified, it's DECL_NAME won't be set properly.
4544 We set it temporarily for the sake of the error report. */
4545 saved_found_wfl = DECL_NAME (found);
4546 reset_method_name (found);
4548 /* Can't override a method with the same name and different return
4550 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
4552 char *t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)),
4556 "Method `%s' was defined with return type `%s' in class `%s'",
4557 lang_printable_name (found, 0), t,
4559 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4563 /* Can't override final. Can't override static. */
4564 if (METHOD_FINAL (found) || METHOD_STATIC (found))
4566 /* Static *can* override static */
4567 if (METHOD_STATIC (found) && METHOD_STATIC (method))
4571 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
4572 (METHOD_FINAL (found) ? "Final" : "Static"),
4573 lang_printable_name (found, 0),
4574 (METHOD_FINAL (found) ? "final" : "static"),
4576 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4579 /* Static method can't override instance method. */
4580 if (METHOD_STATIC (method))
4584 "Instance methods can't be overriden by a static method. Method "
4585 "`%s' is an instance method in class `%s'",
4586 lang_printable_name (found, 0),
4588 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4592 aflags = get_access_flags_from_decl (found);
4593 /* - Overriding/hiding public must be public
4594 - Overriding/hiding protected must be protected or public
4595 - If the overriden or hidden method has default (package)
4596 access, then the overriding or hiding method must not be
4597 private; otherwise, a compile-time error occurs */
4598 if ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
4599 || (METHOD_PROTECTED (found)
4600 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
4601 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
4602 && METHOD_PRIVATE (method)))
4606 "Methods can't be overridden to be more private. Method `%s' is "
4607 "not %s in class `%s'", lang_printable_name (method, 0),
4608 (METHOD_PUBLIC (method) ? "public" :
4609 (METHOD_PRIVATE (method) ? "private" : "protected")),
4610 IDENTIFIER_POINTER (DECL_NAME
4611 (TYPE_NAME (DECL_CONTEXT (found)))));
4615 /* Overriding methods must have compatible `throws' clauses on checked
4616 exceptions, if any */
4617 check_throws_clauses (method, method_wfl, found);
4619 /* If the method has default access in an other package, then
4620 issue a warning that the current method doesn't override the
4621 one that was found elsewhere. Do not issue this warning when
4622 the match was found in java.lang.Object. */
4623 if (DECL_CONTEXT (found) != object_type_node
4624 && (!aflags || (aflags > ACC_PROTECTED))
4625 && !class_in_current_package (DECL_CONTEXT (found))
4626 && flag_not_overriding)
4627 parse_warning_context
4628 (method_wfl, "Method `%s' in class `%s' does not "
4629 "override the corresponding method in class `%s', which is "
4630 "private to a different package",
4631 lang_printable_name (found, 0),
4632 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
4633 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4635 /* Inheriting multiple methods with the same signature. FIXME */
4638 /* Don't forget eventual pending found and saved_found_wfl. Take
4639 into account that we might have exited because we saw an
4640 aritifical method as the last entry. */
4642 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
4643 DECL_NAME (found) = saved_found_wfl;
4645 if (!TYPE_NVIRTUALS (class))
4646 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4648 if (!saw_constructor)
4650 /* No constructor seen, we craft one, at line 0. Since this
4651 operation takes place after we laid methods out
4652 (layout_class_methods), we prepare the its DECL
4657 /* If the class is declared PUBLIC, the default constructor is
4658 PUBLIC otherwise it has default access implied by no access
4660 flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
4662 decl = create_artificial_method (class, flags, void_type_node,
4663 init_identifier_node, end_params_node);
4664 DECL_CONSTRUCTOR_P (decl) = 1;
4665 layout_class_method (TREE_TYPE (class_decl), NULL_TREE, decl, NULL_TREE);
4669 /* Return a non zero value if the `throws' clause of METHOD (if any)
4670 is incompatible with the `throws' clause of FOUND (if any). */
4673 check_throws_clauses (method, method_wfl, found)
4674 tree method, method_wfl, found;
4676 tree mthrows, fthrows;
4678 /* Can't check these things with class loaded from bytecode. FIXME */
4679 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
4682 for (mthrows = DECL_FUNCTION_THROWS (method);
4683 mthrows; mthrows = TREE_CHAIN (mthrows))
4685 /* We don't verify unchecked expressions */
4686 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
4688 /* Checked expression must be compatible */
4689 for (fthrows = DECL_FUNCTION_THROWS (found);
4690 fthrows; fthrows = TREE_CHAIN (fthrows))
4691 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
4696 (method_wfl, "Invalid checked exception class `%s' in "
4697 "`throws' clause. The exception must be a subclass of an "
4698 "exception thrown by `%s' from class `%s'",
4699 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
4700 lang_printable_name (found, 0),
4702 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4707 /* Check abstract method of interface INTERFACE */
4710 java_check_abstract_methods (interface_decl)
4711 tree interface_decl;
4714 tree method, basetype_vec, found;
4715 tree interface = TREE_TYPE (interface_decl);
4717 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
4719 tree method_wfl = DECL_NAME (method);
4721 /* 2- Check for double definition inside the defining interface */
4722 if (check_method_redefinition (interface, method))
4725 /* 3- Overriding is OK as far as we preserve the return type and
4726 the thrown exceptions (FIXME) */
4727 found = lookup_java_interface_method2 (interface, method);
4731 tree saved_found_wfl = DECL_NAME (found);
4732 reset_method_name (found);
4733 t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
4736 "Method `%s' was defined with return type `%s' in class `%s'",
4737 lang_printable_name (found, 0), t,
4739 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4743 DECL_NAME (found) = saved_found_wfl;
4747 /* 4- Inherited methods can't differ by their returned types */
4748 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
4750 n = TREE_VEC_LENGTH (basetype_vec);
4751 for (i = 0; i < n; i++)
4753 tree sub_interface_method, sub_interface;
4754 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4757 sub_interface = BINFO_TYPE (vec_elt);
4758 for (sub_interface_method = TYPE_METHODS (sub_interface);
4759 sub_interface_method;
4760 sub_interface_method = TREE_CHAIN (sub_interface_method))
4762 found = lookup_java_interface_method2 (interface,
4763 sub_interface_method);
4764 if (found && (found != sub_interface_method))
4766 tree saved_found_wfl = DECL_NAME (found);
4767 reset_method_name (found);
4769 (lookup_cl (sub_interface_method),
4770 "Interface `%s' inherits method `%s' from interface `%s'. "
4771 "This method is redefined with a different return type in "
4773 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
4774 lang_printable_name (found, 0),
4776 (DECL_NAME (TYPE_NAME
4777 (DECL_CONTEXT (sub_interface_method)))),
4779 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4780 DECL_NAME (found) = saved_found_wfl;
4786 /* Lookup methods in interfaces using their name and partial
4787 signature. Return a matching method only if their types differ. */
4790 lookup_java_interface_method2 (class, method_decl)
4791 tree class, method_decl;
4794 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
4799 n = TREE_VEC_LENGTH (basetype_vec);
4800 for (i = 0; i < n; i++)
4802 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
4803 if ((BINFO_TYPE (vec_elt) != object_type_node)
4805 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
4808 for (i = 0; i < n; i++)
4810 to_return = lookup_java_interface_method2
4811 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
4819 /* Lookup method using their name and partial signature. Return a
4820 matching method only if their types differ. */
4823 lookup_java_method2 (clas, method_decl, do_interface)
4824 tree clas, method_decl;
4827 tree method, method_signature, method_name, method_type, name;
4829 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
4830 name = DECL_NAME (method_decl);
4831 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4832 EXPR_WFL_NODE (name) : name);
4833 method_type = TREE_TYPE (TREE_TYPE (method_decl));
4835 while (clas != NULL_TREE)
4837 for (method = TYPE_METHODS (clas);
4838 method != NULL_TREE; method = TREE_CHAIN (method))
4840 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
4841 tree name = DECL_NAME (method);
4842 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4843 EXPR_WFL_NODE (name) : name) == method_name
4844 && method_sig == method_signature
4845 && TREE_TYPE (TREE_TYPE (method)) != method_type)
4848 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
4853 /* Return the line that matches DECL line number. Used during error
4860 static tree cl = NULL_TREE;
4865 if (cl == NULL_TREE)
4866 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
4868 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
4869 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
4874 /* Look for a simple name in the single-type import list */
4877 find_name_in_single_imports (name)
4882 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
4883 if (TREE_VALUE (node) == name)
4884 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
4889 /* Process all single-type import. */
4897 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4899 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
4901 /* Don't load twice something already defined. */
4902 if (IDENTIFIER_CLASS_VALUE (to_be_found))
4904 QUALIFIED_P (to_be_found) = 1;
4905 load_class (to_be_found, 0);
4907 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
4908 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
4910 parse_error_context (TREE_PURPOSE (import),
4911 "Class or interface `%s' not found in import",
4912 IDENTIFIER_POINTER (to_be_found));
4921 /* Possibly find a class imported by a single-type import statement. Return
4922 1 if an error occured, 0 otherwise. */
4925 find_in_imports (class_type)
4930 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4931 if (TREE_VALUE (import) == TYPE_NAME (class_type))
4933 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
4934 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
4940 note_possible_classname (name, len)
4945 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
4947 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
4951 node = ident_subst (name, len, "", '/', '.', "");
4952 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
4953 QUALIFIED_P (node) = 1; /* As soon as we turn / into . */
4957 /* Read a import directory, gathering potential match for further type
4958 references. Indifferently reads a filesystem or a ZIP archive
4962 read_import_dir (wfl)
4965 tree package_id = EXPR_WFL_NODE (wfl);
4966 char *package_name = IDENTIFIER_POINTER (package_id);
4967 int package_length = IDENTIFIER_LENGTH (package_id);
4969 JCF *saved_jcf = current_jcf;
4974 struct buffer filename[1];
4977 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
4979 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
4981 BUFFER_INIT (filename);
4982 buffer_grow (filename, package_length + 100);
4984 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
4986 char *entry_name = jcf_path_name (entry);
4987 int entry_length = strlen (entry_name);
4988 if (jcf_path_is_zipfile (entry))
4991 buffer_grow (filename, entry_length);
4992 memcpy (filename->data, entry_name, entry_length - 1);
4993 filename->data[entry_length-1] = '\0';
4994 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
4996 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
4999 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
5000 BUFFER_RESET (filename);
5001 for (k = 0; k < package_length; k++)
5003 char ch = package_name[k];
5004 *filename->ptr++ = ch == '.' ? '/' : ch;
5006 *filename->ptr++ = '/';
5008 for (; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
5010 char *current_entry = ZIPDIR_FILENAME (zipd);
5011 int current_entry_len = zipd->filename_length;
5013 if (strncmp (filename->data, current_entry,
5014 BUFFER_LENGTH (filename)) != 0)
5016 found += note_possible_classname (current_entry,
5023 BUFFER_RESET (filename);
5024 buffer_grow (filename, entry_length + package_length + 4);
5025 strcpy (filename->data, entry_name);
5026 filename->ptr = filename->data + entry_length;
5027 for (k = 0; k < package_length; k++)
5029 char ch = package_name[k];
5030 *filename->ptr++ = ch == '.' ? '/' : ch;
5032 *filename->ptr = '\0';
5034 dirp = opendir (filename->data);
5037 *filename->ptr++ = '/';
5042 struct dirent *direntp = readdir (dirp);
5045 d_name = direntp->d_name;
5046 len = strlen (direntp->d_name);
5047 buffer_grow (filename, len+1);
5048 strcpy (filename->ptr, d_name);
5049 found += note_possible_classname (filename->data + entry_length,
5050 package_length+len+1);
5057 free (filename->data);
5059 /* Here we should have a unified way of retrieving an entry, to be
5063 static int first = 1;
5067 sprintf (buffer, "Can't find default package `%s'. Check "
5068 "the CLASSPATH environment variable and the access to the "
5069 "archives.", package_name);
5075 parse_error_context (wfl, "Package `%s' not found in import",
5077 current_jcf = saved_jcf;
5080 current_jcf = saved_jcf;
5083 /* Possibly find a type in the import on demands specified
5084 types. Returns 1 if an error occured, 0 otherwise. Run throught the
5085 entire list, to detected potential double definitions. */
5088 find_in_imports_on_demand (class_type)
5091 tree node, import, node_to_use;
5095 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
5098 obstack_grow (&temporary_obstack,
5099 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
5100 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5101 obstack_1grow (&temporary_obstack, '.');
5102 obstack_grow0 (&temporary_obstack,
5103 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5104 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
5105 id_name = obstack_finish (&temporary_obstack);
5107 node = maybe_get_identifier (id_name);
5108 if (node && IS_A_CLASSFILE_NAME (node))
5112 cl = TREE_PURPOSE (import);
5120 (import, "Type `%s' also potentially defined in package `%s'",
5121 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5122 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5129 /* Setup lineno so that it refers to the line of the import (in
5130 case we parse a class file and encounter errors */
5132 int saved_lineno = lineno;
5133 lineno = EXPR_WFL_LINENO (cl);
5134 TYPE_NAME (class_type) = node_to_use;
5135 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
5136 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5137 /* If there is no DECL set for the class or if the class isn't
5138 loaded and not seen in source yet, the load */
5139 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
5140 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
5141 load_class (node_to_use, 0);
5142 lineno = saved_lineno;
5143 return check_pkg_class_access (TYPE_NAME (class_type), cl);
5146 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
5150 resolve_package (pkg, next)
5153 tree type_name = NULL_TREE;
5154 char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5156 /* The trick is to determine when the package name stops and were
5157 the name of something contained in the package starts. Then we
5158 return a fully qualified name of what we want to get. */
5160 /* Do a quick search on well known package names */
5161 if (!strncmp (name, "java.lang.reflect", 17))
5164 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
5165 type_name = lookup_package_type (name, 17);
5167 else if (!strncmp (name, "java.lang", 9))
5169 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
5170 type_name = lookup_package_type (name, 9);
5173 return NULL_TREE; /* FIXME, search all imported packages. */
5179 lookup_package_type (name, from)
5184 char *sub = &name[from+1];
5185 while (*sub != '.' && *sub)
5187 strncpy (subname, name, sub-name);
5188 subname [sub-name] = '\0';
5189 return get_identifier (subname);
5192 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
5193 access violations were found, 1 otherwise. */
5196 check_pkg_class_access (class_name, cl)
5202 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
5205 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
5208 if (!CLASS_PUBLIC (TYPE_NAME (type)))
5210 /* Access to a private class within the same package is
5213 breakdown_qualified (&l, &r, class_name);
5214 if (l == ctxp->package)
5218 (cl, "Can't access %s `%s'. Only public classes and interfaces in "
5219 "other packages can be accessed",
5220 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
5221 IDENTIFIER_POINTER (class_name));
5227 /* Local variable declaration. */
5230 declare_local_variables (modifier, type, vlist)
5235 tree decl, current, saved_type;
5236 tree type_wfl = NULL_TREE;
5239 /* Push a new block if statement were seen between the last time we
5240 pushed a block and now. Keep a cound of block to close */
5241 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)))
5243 tree body = DECL_FUNCTION_BODY (current_function_decl);
5244 tree b = enter_block ();
5245 BLOCK_EXPR_ORIGIN(b) = body;
5251 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
5252 if (modifier == ACC_FINAL)
5254 if (flag_static_local_jdk1_1)
5255 parse_warning_context (ctxp->modifier_ctx [i],
5256 "Unsupported JDK1.1 `final' local variable "
5257 "(treated as non final)");
5262 (ctxp->modifier_ctx [i],
5263 "Only `final' is allowed as a local variables modifier");
5268 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
5269 hold the TYPE value if a new incomplete has to be created (as
5270 opposed to being found already existing and reused). */
5271 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5273 /* If TYPE is fully resolved and we don't have a reference, make one */
5274 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5276 /* Go through all the declared variables */
5277 for (current = vlist, saved_type = type; current;
5278 current = TREE_CHAIN (current), type = saved_type)
5280 tree other, real_type;
5281 tree wfl = TREE_PURPOSE (current);
5282 tree name = EXPR_WFL_NODE (wfl);
5283 tree init = TREE_VALUE (current);
5285 /* Process NAME, as it may specify extra dimension(s) for it */
5286 type = build_array_from_name (type, type_wfl, name, &name);
5288 /* Variable redefinition check */
5289 if ((other = lookup_name_in_blocks (name)))
5291 variable_redefinition_error (wfl, name, TREE_TYPE (other),
5292 DECL_SOURCE_LINE (other));
5296 /* Type adjustment. We may have just readjusted TYPE because
5297 the variable specified more dimensions. Make sure we have
5298 a reference if we can and don't have one already. */
5299 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5301 real_type = GET_REAL_TYPE (type);
5302 /* Never layout this decl. This will be done when its scope
5304 decl = build_decl (VAR_DECL, name, real_type);
5305 BLOCK_CHAIN_DECL (decl);
5307 /* Don't try to use an INIT statement when an error was found */
5308 if (init && java_error_count)
5311 /* Add the initialization function to the current function's code */
5314 /* Name might have been readjusted */
5315 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
5316 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
5317 java_method_add_stmt (current_function_decl,
5318 build_debugable_stmt (EXPR_WFL_LINECOL (init),
5322 /* Setup dependency the type of the decl */
5326 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
5327 dep = CLASSD_LAST (ctxp->classd_list);
5328 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
5331 SOURCE_FRONTEND_DEBUG (("Defined locals"));
5334 /* Called during parsing. Build decls from argument list. */
5337 source_start_java_method (fndecl)
5344 current_function_decl = fndecl;
5346 /* New scope for the function */
5348 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
5349 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
5351 tree type = TREE_VALUE (tem);
5352 tree name = TREE_PURPOSE (tem);
5354 /* If type is incomplete. Create an incomplete decl and ask for
5355 the decl to be patched later */
5356 if (INCOMPLETE_TYPE_P (type))
5359 tree real_type = GET_REAL_TYPE (type);
5360 parm_decl = build_decl (PARM_DECL, name, real_type);
5361 type = obtain_incomplete_type (type);
5362 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
5363 jdep = CLASSD_LAST (ctxp->classd_list);
5364 JDEP_MISC (jdep) = name;
5365 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
5368 parm_decl = build_decl (PARM_DECL, name, type);
5370 BLOCK_CHAIN_DECL (parm_decl);
5372 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5373 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
5375 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
5378 /* Called during parsing. Creates an artificial method declaration. */
5381 create_artificial_method (class, flags, type, name, args)
5384 tree type, name, args;
5386 int saved_lineno = lineno;
5390 mdecl = make_node (FUNCTION_TYPE);
5391 TREE_TYPE (mdecl) = type;
5392 TYPE_ARG_TYPES (mdecl) = args;
5393 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
5394 lineno = saved_lineno;
5395 DECL_ARTIFICIAL (mdecl) = 1;
5399 /* Starts the body if an artifical method. */
5402 start_artificial_method_body (mdecl)
5405 DECL_SOURCE_LINE (mdecl) = 1;
5406 DECL_SOURCE_LINE_MERGE (mdecl, 1);
5407 source_start_java_method (mdecl);
5412 end_artificial_method_body (mdecl)
5415 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
5419 /* Called during expansion. Push decls formerly built from argument
5420 list so they're usable during expansion. */
5423 expand_start_java_method (fndecl)
5428 current_function_decl = fndecl;
5430 announce_function (fndecl);
5431 pushlevel (1); /* Push parameters */
5432 ptr = &DECL_ARGUMENTS (fndecl);
5433 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5436 tree next = TREE_CHAIN (tem);
5437 tree type = TREE_TYPE (tem);
5438 #ifdef PROMOTE_PROTOTYPES
5439 if (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
5440 && INTEGRAL_TYPE_P (type))
5441 type = integer_type_node;
5443 DECL_ARG_TYPE (tem) = type;
5444 layout_decl (tem, 0);
5446 INITIALIZED_P (tem) = 1; /* Parms are initialized */
5448 ptr = &TREE_CHAIN (tem);
5452 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5453 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
5454 complete_start_java_method (fndecl);
5457 /* Terminate a function and expand its body. */
5460 source_end_java_method ()
5462 tree fndecl = current_function_decl;
5464 java_parser_context_save_global ();
5465 lineno = ctxp->last_ccb_indent1;
5467 /* Set EH language codes */
5468 java_set_exception_lang_code ();
5470 /* Generate function's code */
5471 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
5472 && ! flag_emit_class_files)
5473 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
5475 /* pop out of its parameters */
5476 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5478 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
5480 /* Generate rtl for function exit. */
5481 if (! flag_emit_class_files)
5483 lineno = DECL_SOURCE_LINE_LAST (fndecl);
5484 /* Emit catch-finally clauses */
5486 expand_function_end (input_filename, lineno, 0);
5488 /* Run the optimizers and output assembler code for this function. */
5489 rest_of_compilation (fndecl);
5492 current_function_decl = NULL_TREE;
5493 /* permanent_allocation (1); */
5494 java_parser_context_restore_global ();
5497 /* Record EXPR in the current function block. Complements compound
5498 expression second operand if necessary. */
5501 java_method_add_stmt (fndecl, expr)
5504 return add_stmt_to_block (DECL_FUNCTION_BODY (fndecl), NULL_TREE, expr);
5508 add_stmt_to_block (b, type, stmt)
5511 tree body = BLOCK_EXPR_BODY (b), c;
5513 if (java_error_count)
5516 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
5519 BLOCK_EXPR_BODY (b) = c;
5520 TREE_SIDE_EFFECTS (c) = 1;
5524 /* Add STMT to EXISTING if possible, otherwise create a new
5525 COMPOUND_EXPR and add STMT to it. */
5528 add_stmt_to_compound (existing, type, stmt)
5529 tree existing, type, stmt;
5532 return build (COMPOUND_EXPR, type, existing, stmt);
5537 /* Hold THIS for the scope of the current public method decl. */
5538 static tree current_this;
5540 void java_layout_seen_class_methods ()
5542 tree previous_list = all_class_list;
5543 tree end = NULL_TREE;
5548 for (current = previous_list;
5549 current != end; current = TREE_CHAIN (current))
5550 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
5552 if (previous_list != all_class_list)
5554 end = previous_list;
5555 previous_list = all_class_list;
5562 /* Layout the methods of all classes loaded in one way on an
5563 other. Check methods of source parsed classes. Then reorder the
5564 fields and layout the classes or the type of all source parsed
5568 java_layout_classes ()
5571 int save_error_count = java_error_count;
5573 /* Layout the methods of all classes seen so far */
5574 java_layout_seen_class_methods ();
5575 java_parse_abort_on_error ();
5576 all_class_list = NULL_TREE;
5578 /* Then check the methods of all parsed classes */
5579 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5580 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
5581 CHECK_METHODS (TREE_VALUE (current));
5582 java_parse_abort_on_error ();
5584 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5586 current_class = TREE_TYPE (TREE_VALUE (current));
5588 /* Reverse the fields, but leave the dummy field in front.
5589 Fields are already ordered for Object and Class */
5590 if (TYPE_FIELDS (current_class) && current_class != object_type_node
5591 && current_class != class_type_node)
5593 /* If the dummy field is there, reverse the right fields and
5594 just layout the type for proper fields offset */
5595 if (!DECL_NAME (TYPE_FIELDS (current_class)))
5597 tree fields = TYPE_FIELDS (current_class);
5598 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
5599 TYPE_SIZE (current_class) = NULL_TREE;
5600 layout_type (current_class);
5602 /* We don't have a dummy field, we need to layout the class,
5603 after having reversed the fields */
5606 TYPE_FIELDS (current_class) =
5607 nreverse (TYPE_FIELDS (current_class));
5608 TYPE_SIZE (current_class) = NULL_TREE;
5609 layout_class (current_class);
5613 layout_class (current_class);
5615 /* From now on, the class is considered completely loaded */
5616 CLASS_LOADED_P (current_class) = 1;
5618 /* Error reported by the caller */
5619 if (java_error_count)
5623 /* We might have reloaded classes durign the process of laying out
5624 classes for code generation. We must layout the methods of those
5625 late additions, as constructor checks might use them */
5626 java_layout_seen_class_methods ();
5627 java_parse_abort_on_error ();
5630 /* Expand all methods in all registered classes. */
5633 java_complete_expand_methods ()
5637 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5639 tree class_type = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current));
5642 current_class = TREE_TYPE (current);
5644 /* Initialize a new constant pool */
5645 init_outgoing_cpool ();
5647 /* We want <clinit> (if any) to be processed first. */
5648 decl = tree_last (TYPE_METHODS (class_type));
5649 if (decl && DECL_NAME (decl) == clinit_identifier_node)
5651 tree list = nreverse (TYPE_METHODS (class_type));
5652 list = TREE_CHAIN (list);
5653 TREE_CHAIN (decl) = NULL_TREE;
5654 TYPE_METHODS (class_type) = chainon (decl, nreverse (list));
5657 /* Don't process function bodies in interfaces */
5658 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5659 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5661 current_function_decl = decl;
5662 /* Don't generate debug info on line zero when expanding a
5663 generated constructor. */
5664 if (DECL_CONSTRUCTOR_P (decl) && !DECL_FUNCTION_BODY (decl))
5666 /* If we found errors, it's too dangerous to try to generate
5667 and expand a constructor */
5668 if (!java_error_count)
5670 restore_line_number_status (1);
5671 java_complete_expand_method (decl);
5672 restore_line_number_status (0);
5675 else if (METHOD_ABSTRACT (decl) || METHOD_NATIVE (decl))
5678 java_complete_expand_method (decl);
5681 /* Now verify constructor circularity (stop after the first one
5683 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5684 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5685 if (DECL_CONSTRUCTOR_P (decl) &&
5686 verify_constructor_circularity (decl, decl))
5689 /* Make the class data, register it and run the rest of decl
5690 compilation on it */
5691 if (!java_error_count)
5693 if (flag_emit_class_files)
5694 write_classfile (current_class);
5695 else if (! flag_syntax_only)
5696 finish_class (current_class);
5701 /* Hold a list of catch clauses list. The first element of this list is
5702 the list of the catch clauses of the currently analysed try block. */
5703 static tree currently_caught_type_list;
5705 /* Complete and expand a method. */
5708 java_complete_expand_method (mdecl)
5711 /* Fix constructors before expanding them */
5712 if (DECL_CONSTRUCTOR_P (mdecl))
5713 fix_constructors (mdecl);
5715 /* Expand functions that have a body */
5716 if (DECL_FUNCTION_BODY (mdecl))
5718 tree fbody = DECL_FUNCTION_BODY (mdecl);
5719 tree block_body = BLOCK_EXPR_BODY (fbody);
5720 expand_start_java_method (mdecl);
5723 = (!METHOD_STATIC (mdecl) ?
5724 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
5726 /* Purge the `throws' list of unchecked exceptions */
5727 purge_unchecked_exceptions (mdecl);
5729 /* Install exceptions thrown with `throws' */
5730 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
5732 if (block_body != NULL_TREE)
5734 block_body = java_complete_tree (block_body);
5735 check_for_initialization (block_body);
5737 BLOCK_EXPR_BODY (fbody) = block_body;
5739 if ((block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
5740 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
5741 missing_return_error (current_function_decl);
5743 /* Don't go any further if we've found error(s) during the
5745 if (!java_error_count)
5746 source_end_java_method ();
5749 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
5753 /* Pop the exceptions and sanity check */
5755 if (currently_caught_type_list)
5756 fatal ("Exception list non empty - java_complete_expand_method");
5760 /* Craft a body for default constructor. Patch existing constructor
5761 bodies with call to super() and field initialization statements if
5765 fix_constructors (mdecl)
5768 tree body = DECL_FUNCTION_BODY (mdecl);
5772 /* The constructor body must be crafted by hand. It's the
5773 constructor we defined when we realize we didn't have the
5774 CLASSNAME() constructor */
5778 /* It is an error for the compiler to generate a default
5779 constructor if the superclass doesn't have a constructor that
5780 takes no argument */
5781 if (verify_constructor_super ())
5783 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (current_class));
5784 char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
5785 parse_error_context (lookup_cl (TYPE_NAME (current_class)),
5786 "No constructor matching `%s()' found in "
5787 "class `%s'", n, n);
5790 start_artificial_method_body (mdecl);
5792 /* We don't generate a super constructor invocation if we're
5793 compiling java.lang.Object. build_super_invocation takes care
5795 compound = java_method_add_stmt (mdecl, build_super_invocation ());
5797 end_artificial_method_body (mdecl);
5799 /* Search for an explicit constructor invocation */
5803 tree main_block = BLOCK_EXPR_BODY (body);
5804 tree compound = NULL_TREE;
5807 switch (TREE_CODE (body))
5810 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
5814 case EXPR_WITH_FILE_LOCATION:
5815 body = TREE_OPERAND (body, 0);
5818 body = BLOCK_EXPR_BODY (body);
5824 /* The constructor is missing an invocation of super() */
5826 compound = add_stmt_to_compound (compound, NULL_TREE,
5827 build_super_invocation ());
5829 /* Fix the constructor main block if we're adding extra stmts */
5832 compound = add_stmt_to_compound (compound, NULL_TREE,
5833 BLOCK_EXPR_BODY (main_block));
5834 BLOCK_EXPR_BODY (main_block) = compound;
5839 /* Browse constructors in the super class, searching for a constructor
5840 that doesn't take any argument. Return 0 if one is found, 1
5844 verify_constructor_super ()
5846 tree class = CLASSTYPE_SUPER (current_class);
5853 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
5855 if (DECL_CONSTRUCTOR_P (mdecl)
5856 && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (mdecl))) == end_params_node)
5863 /* Expand finals. */
5866 java_expand_finals ()
5870 /* Generate code for all context remembered for code generation. */
5873 java_expand_classes ()
5875 int save_error_count = java_error_count;
5876 java_parse_abort_on_error ();
5877 if (!(ctxp = ctxp_for_generation))
5879 java_layout_classes ();
5880 java_parse_abort_on_error ();
5882 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
5884 ctxp = ctxp_for_generation;
5885 lang_init_source (2); /* Error msgs have method prototypes */
5886 java_complete_expand_methods (); /* Complete and expand method bodies */
5887 java_parse_abort_on_error ();
5888 java_expand_finals (); /* Expand and check the finals */
5889 java_parse_abort_on_error ();
5890 java_check_final (); /* Check unitialized final */
5891 java_parse_abort_on_error ();
5895 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
5896 a tree list node containing RIGHT. Fore coming RIGHTs will be
5897 chained to this hook. LOCATION contains the location of the
5898 separating `.' operator. */
5901 make_qualified_primary (primary, right, location)
5902 tree primary, right;
5907 /* We want to process THIS . xxx symbolicaly, to keep it consistent
5908 with the way we're processing SUPER. A THIS from a primary as a
5909 different form than a SUPER. Turn THIS into something symbolic */
5910 if (TREE_CODE (primary) == THIS_EXPR)
5912 wfl = build_wfl_node (this_identifier_node, input_filename, 0, 0);
5913 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5914 wfl = make_qualified_name (wfl, right, location);
5915 PRIMARY_P (wfl) = 1;
5918 /* Other non WFL node are wrapped around a WFL */
5919 else if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
5921 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
5922 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5923 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (primary, NULL_TREE);
5928 if (!EXPR_WFL_QUALIFICATION (primary))
5929 EXPR_WFL_QUALIFICATION (primary) =
5930 build_tree_list (primary, NULL_TREE);
5933 EXPR_WFL_LINECOL (right) = location;
5934 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
5935 PRIMARY_P (wfl) = 1;
5939 /* Simple merge of two name separated by a `.' */
5942 merge_qualified_name (left, right)
5946 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
5947 IDENTIFIER_LENGTH (left));
5948 obstack_1grow (&temporary_obstack, '.');
5949 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
5950 IDENTIFIER_LENGTH (right));
5951 node = get_identifier (obstack_base (&temporary_obstack));
5952 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
5953 QUALIFIED_P (node) = 1;
5957 /* Merge the two parts of a qualified name into LEFT. Set the
5958 location information of the resulting node to LOCATION, usually
5959 inherited from the location information of the `.' operator. */
5962 make_qualified_name (left, right, location)
5966 #ifdef USE_COMPONENT_REF
5967 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
5968 EXPR_WFL_LINECOL (node) = location;
5971 tree left_id = EXPR_WFL_NODE (left);
5972 tree right_id = EXPR_WFL_NODE (right);
5975 merge = merge_qualified_name (left_id, right_id);
5977 /* Left wasn't qualified and is now qualified */
5978 if (!QUALIFIED_P (left_id))
5980 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
5981 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
5982 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
5985 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
5986 EXPR_WFL_LINECOL (wfl) = location;
5987 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
5989 EXPR_WFL_NODE (left) = merge;
5994 /* Extract the last identifier component of the qualified in WFL. The
5995 last identifier is removed from the linked list */
5998 cut_identifier_in_qualified (wfl)
6002 tree previous = NULL_TREE;
6003 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
6004 if (!TREE_CHAIN (q))
6007 fatal ("Operating on a non qualified qualified WFL - "
6008 "cut_identifier_in_qualified");
6009 TREE_CHAIN (previous) = NULL_TREE;
6010 return TREE_PURPOSE (q);
6014 /* Resolve the expression name NAME. Return its decl. */
6017 resolve_expression_name (id, orig)
6021 tree name = EXPR_WFL_NODE (id);
6024 /* 6.5.5.1: Simple expression names */
6025 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
6027 /* 15.13.1: NAME can appear within the scope of a local variable
6029 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
6032 /* 15.13.1: NAME can appear within a class declaration */
6035 decl = lookup_field_wrapper (current_class, name);
6038 int fs = FIELD_STATIC (decl);
6039 /* Instance variable (8.3.1.1) can't appear within
6040 static method, static initializer or initializer for
6041 a static variable. */
6042 if (!fs && METHOD_STATIC (current_function_decl))
6045 (id, "Can't make a static reference to nonstatic variable "
6046 "`%s' in class `%s'",
6047 IDENTIFIER_POINTER (name),
6048 IDENTIFIER_POINTER (DECL_NAME
6049 (TYPE_NAME (current_class))));
6050 return error_mark_node;
6052 /* Instance variables can't appear as an argument of
6053 an explicit constructor invocation */
6054 if (!fs && ctxp->explicit_constructor_p)
6057 (id, "Can't reference `%s' before the superclass "
6058 "constructor has been called", IDENTIFIER_POINTER (name));
6059 return error_mark_node;
6062 /* Otherwise build what it takes to access the field */
6063 decl = build_field_ref ((fs ? NULL_TREE : current_this),
6064 current_class, name);
6065 if (fs && !flag_emit_class_files)
6066 decl = build_class_init (current_class, decl);
6067 /* We may be asked to save the real field access node */
6070 /* And we return what we got */
6073 /* Fall down to error report on undefined variable */
6076 /* 6.5.5.2 Qualified Expression Names */
6081 qualify_ambiguous_name (id);
6082 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
6083 /* 15.10.2: Accessing Superclass Members using super */
6084 return resolve_field_access (id, NULL, NULL);
6087 /* We've got an error here */
6088 parse_error_context (id, "Undefined variable `%s'",
6089 IDENTIFIER_POINTER (name));
6091 return error_mark_node;
6094 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
6095 We return something suitable to generate the field access. We also
6096 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
6097 recipient's address can be null. */
6100 resolve_field_access (qual_wfl, field_decl, field_type)
6102 tree *field_decl, *field_type;
6106 tree decl, where_found, type_found;
6108 if (resolve_qualified_expression_name (qual_wfl, &decl,
6109 &where_found, &type_found))
6110 return error_mark_node;
6112 /* Resolve the LENGTH field of an array here */
6113 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
6114 && ! flag_emit_class_files)
6116 tree length = build_java_array_length_access (where_found);
6118 build_java_arraynull_check (type_found, length, int_type_node);
6120 /* We might have been trying to resolve field.method(). In which
6121 case, the resolution is over and decl is the answer */
6122 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
6124 else if (JDECL_P (decl))
6126 int static_final_found = 0;
6128 type_found = DECL_CONTEXT (decl);
6129 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
6130 if (FIELD_FINAL (decl)
6131 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
6132 && DECL_LANG_SPECIFIC (decl)
6133 && DECL_INITIAL (decl))
6135 field_ref = DECL_INITIAL (decl);
6136 static_final_found = 1;
6139 field_ref = build_field_ref ((is_static ? NULL_TREE : where_found),
6140 type_found, DECL_NAME (decl));
6141 if (field_ref == error_mark_node)
6142 return error_mark_node;
6143 if (is_static && !static_final_found && !flag_emit_class_files)
6145 field_ref = build_class_init (type_found, field_ref);
6146 /* If the static field was identified by an expression that
6147 needs to be generated, make the field access a compound
6148 expression whose first part of the evaluation of the
6149 field selector part. */
6150 if (where_found && TREE_CODE (where_found) != TYPE_DECL
6151 && TREE_CODE (where_found) != RECORD_TYPE)
6153 tree type = QUAL_DECL_TYPE (field_ref);
6154 field_ref = build (COMPOUND_EXPR, type, where_found, field_ref);
6164 *field_type = (QUAL_DECL_TYPE (decl) ?
6165 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
6169 /* If NODE is an access to f static field, strip out the class
6170 initialization part and return the field decl, otherwise, return
6174 strip_out_static_field_access_decl (node)
6177 if (TREE_CODE (node) == COMPOUND_EXPR)
6179 tree op1 = TREE_OPERAND (node, 1);
6180 if (TREE_CODE (op1) == COMPOUND_EXPR)
6182 tree call = TREE_OPERAND (op1, 0);
6183 if (TREE_CODE (call) == CALL_EXPR
6184 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
6185 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
6186 == soft_initclass_node)
6187 return TREE_OPERAND (op1, 1);
6193 /* 6.5.5.2: Qualified Expression Names */
6196 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
6198 tree *found_decl, *type_found, *where_found;
6200 int from_type = 0; /* Field search initiated from a type */
6201 int from_super = 0, from_cast = 0;
6202 int previous_call_static = 0;
6204 tree decl = NULL_TREE, type = NULL_TREE, q;
6205 *type_found = *where_found = NULL_TREE;
6207 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
6209 tree qual_wfl = QUAL_WFL (q);
6211 /* 15.10.1 Field Access Using a Primary */
6212 switch (TREE_CODE (qual_wfl))
6215 case NEW_CLASS_EXPR:
6216 /* If the access to the function call is a non static field,
6217 build the code to access it. */
6218 if (JDECL_P (decl) && !FIELD_STATIC (decl))
6220 decl = maybe_access_field (decl, *where_found,
6221 DECL_CONTEXT (decl));
6222 if (decl == error_mark_node)
6225 /* And code for the function call */
6226 if (complete_function_arguments (qual_wfl))
6228 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
6229 CALL_USING_SUPER (qual_wfl) = 1;
6231 patch_method_invocation (qual_wfl, decl, type, &is_static, NULL);
6232 if (*where_found == error_mark_node)
6234 *type_found = type = QUAL_DECL_TYPE (*where_found);
6236 /* If the previous call was static and this one is too,
6237 build a compound expression to hold the two (because in
6238 that case, previous function calls aren't transported as
6239 forcoming function's argument. */
6240 if (previous_call_static && is_static)
6242 decl = build (COMPOUND_EXPR, type, decl, *where_found);
6243 TREE_SIDE_EFFECTS (decl) = 1;
6247 previous_call_static = is_static;
6248 decl = *where_found;
6252 case NEW_ARRAY_EXPR:
6253 *where_found = decl = java_complete_tree (qual_wfl);
6254 if (decl == error_mark_node)
6256 *type_found = type = QUAL_DECL_TYPE (decl);
6257 CLASS_LOADED_P (type) = 1;
6261 *where_found = decl = java_complete_tree (qual_wfl);
6262 if (decl == error_mark_node)
6264 *type_found = type = QUAL_DECL_TYPE (decl);
6268 case CONDITIONAL_EXPR:
6270 *where_found = decl = java_complete_tree (qual_wfl);
6271 if (decl == error_mark_node)
6273 *type_found = type = QUAL_DECL_TYPE (decl);
6277 /* If the access to the function call is a non static field,
6278 build the code to access it. */
6279 if (JDECL_P (decl) && !FIELD_STATIC (decl))
6281 decl = maybe_access_field (decl, *where_found, type);
6282 if (decl == error_mark_node)
6285 /* And code for the array reference expression */
6286 decl = java_complete_tree (qual_wfl);
6287 if (decl == error_mark_node)
6289 type = QUAL_DECL_TYPE (decl);
6293 /* Fix for -Wall Just go to the next statement. Don't
6297 /* If we fall here, we weren't processing a (static) function call. */
6298 previous_call_static = 0;
6300 /* It can be the keyword THIS */
6301 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
6306 (wfl, "Keyword `this' used outside allowed context");
6309 /* We have to generate code for intermediate acess */
6310 *where_found = decl = current_this;
6311 *type_found = type = QUAL_DECL_TYPE (decl);
6315 /* 15.10.2 Accessing Superclass Members using SUPER */
6316 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
6319 /* Check on the restricted use of SUPER */
6320 if (METHOD_STATIC (current_function_decl)
6321 || current_class == object_type_node)
6324 (wfl, "Keyword `super' used outside allowed context");
6327 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
6328 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
6329 CLASSTYPE_SUPER (current_class),
6330 build_this (EXPR_WFL_LINECOL (qual_wfl)));
6331 *where_found = decl = java_complete_tree (node);
6332 if (decl == error_mark_node)
6334 *type_found = type = QUAL_DECL_TYPE (decl);
6335 from_super = from_type = 1;
6339 /* 15.13.1: Can't search for field name in packages, so we
6340 assume a variable/class name was meant. */
6341 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
6343 tree name = resolve_package (wfl, &q);
6346 *where_found = decl = resolve_no_layout (name, qual_wfl);
6347 /* We wan't to be absolutely that the class is laid
6348 out. We're going to search something inside it. */
6349 *type_found = type = TREE_TYPE (decl);
6350 layout_class (type);
6352 /* Should be a list, really. FIXME */
6353 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 1;
6354 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 0;
6358 if (from_super || from_cast)
6360 ((from_cast ? qual_wfl : wfl),
6361 "No variable `%s' defined in class `%s'",
6362 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6363 lang_printable_name (type, 0));
6366 (qual_wfl, "Undefined variable or class name: `%s'",
6367 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
6372 /* We have a type name. It's been already resolved when the
6373 expression was qualified. */
6374 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
6376 if (!(decl = QUAL_RESOLUTION (q)))
6377 return 1; /* Error reported already */
6379 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
6382 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
6383 java_accstring_lookup (get_access_flags_from_decl (decl)),
6384 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6385 IDENTIFIER_POINTER (DECL_NAME (decl)),
6386 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6389 check_deprecation (qual_wfl, decl);
6391 type = TREE_TYPE (decl);
6394 /* We resolve and expression name */
6399 /* If there exists an early resolution, use it. That occurs
6400 only once and we know that there are more things to
6401 come. Don't do that when processing something after SUPER
6402 (we need more thing to be put in place below */
6403 if (!from_super && QUAL_RESOLUTION (q))
6405 decl = QUAL_RESOLUTION (q);
6408 if (!FIELD_STATIC (decl))
6409 *where_found = current_this;
6412 *where_found = TREE_TYPE (decl);
6413 if (TREE_CODE (*where_found) == POINTER_TYPE)
6414 *where_found = TREE_TYPE (*where_found);
6419 /* We have to search for a field, knowing the type of its
6420 container. The flag FROM_TYPE indicates that we resolved
6421 the last member of the expression as a type name, which
6422 means that for the resolution of this field, we'll look
6423 for other errors than if it was resolved as a member of
6428 tree field_decl_type; /* For layout */
6430 if (!from_type && !JREFERENCE_TYPE_P (type))
6433 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
6434 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6435 lang_printable_name (type, 0),
6436 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6441 lookup_field_wrapper (type, EXPR_WFL_NODE (qual_wfl))))
6444 (qual_wfl, "No variable `%s' defined in class `%s'",
6445 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6446 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6450 /* Layout the type of field_decl, since we may need
6451 it. Don't do primitive types or loaded classes. The
6452 situation of non primitive arrays may not handled
6453 properly here. FIXME */
6454 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
6455 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
6457 field_decl_type = TREE_TYPE (field_decl);
6458 if (!JPRIMITIVE_TYPE_P (field_decl_type)
6459 && !CLASS_LOADED_P (field_decl_type)
6460 && !TYPE_ARRAY_P (field_decl_type))
6461 resolve_and_layout (field_decl_type, NULL_TREE);
6462 if (TYPE_ARRAY_P (field_decl_type))
6463 CLASS_LOADED_P (field_decl_type) = 1;
6465 /* Check on accessibility here */
6466 if (not_accessible_p (type, field_decl, from_super))
6470 "Can't access %s field `%s.%s' from `%s'",
6471 java_accstring_lookup
6472 (get_access_flags_from_decl (field_decl)),
6473 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6474 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
6476 (DECL_NAME (TYPE_NAME (current_class))));
6479 check_deprecation (qual_wfl, field_decl);
6481 /* There are things to check when fields are accessed
6482 from type. There are no restrictions on a static
6483 declaration of the field when it is accessed from an
6485 is_static = FIELD_STATIC (field_decl);
6486 if (!from_super && from_type
6487 && !TYPE_INTERFACE_P (type) && !is_static)
6490 (qual_wfl, "Can't make a static reference to nonstatic "
6491 "variable `%s' in class `%s'",
6492 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6493 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6496 from_cast = from_super = 0;
6498 /* If we need to generate something to get a proper
6499 handle on what this field is accessed from, do it
6503 decl = maybe_access_field (decl, *where_found, *type_found);
6504 if (decl == error_mark_node)
6508 /* We want to keep the location were found it, and the type
6510 *where_found = decl;
6513 /* This is the decl found and eventually the next one to
6518 type = QUAL_DECL_TYPE (decl);
6525 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
6526 can't be accessed from REFERENCE (a record type). */
6528 int not_accessible_p (reference, member, from_super)
6529 tree reference, member;
6532 int access_flag = get_access_flags_from_decl (member);
6534 /* Access always granted for members declared public */
6535 if (access_flag & ACC_PUBLIC)
6538 /* Check access on protected members */
6539 if (access_flag & ACC_PROTECTED)
6541 /* Access granted if it occurs from within the package
6542 containing the class in which the protected member is
6544 if (class_in_current_package (DECL_CONTEXT (member)))
6547 /* If accessed with the form `super.member', then access is granted */
6551 /* Otherwise, access is granted if occuring from the class where
6552 member is declared or a subclass of it */
6553 if (inherits_from_p (reference, current_class))
6558 /* Check access on private members. Access is granted only if it
6559 occurs from within the class in witch it is declared */
6560 if (access_flag & ACC_PRIVATE)
6561 return (current_class == DECL_CONTEXT (member) ? 0 : 1);
6563 /* Default access are permitted only when occuring within the
6564 package in which the type (REFERENCE) is declared. In other words,
6565 REFERENCE is defined in the current package */
6567 return !class_in_current_package (reference);
6569 /* Otherwise, access is granted */
6573 /* Test deprecated decl access. */
6575 check_deprecation (wfl, decl)
6578 char *file = DECL_SOURCE_FILE (decl);
6579 /* Complain if the field is deprecated and the file it was defined
6580 in isn't compiled at the same time the file which contains its
6582 if (DECL_DEPRECATED (decl)
6583 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
6586 switch (TREE_CODE (decl))
6589 strcpy (the, "method");
6592 strcpy (the, "field");
6595 strcpy (the, "class");
6598 fatal ("unexpected DECL code - check_deprecation");
6600 parse_warning_context
6601 (wfl, "The %s `%s' in class `%s' has been deprecated",
6602 the, lang_printable_name (decl, 0),
6603 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
6607 /* Returns 1 if class was declared in the current package, 0 otherwise */
6610 class_in_current_package (class)
6613 static tree cache = NULL_TREE;
6620 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
6622 /* If the current package is empty and the name of CLASS is
6623 qualified, class isn't in the current package. If there is a
6624 current package and the name of the CLASS is not qualified, class
6625 isn't in the current package */
6626 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
6629 /* If there is not package and the name of CLASS isn't qualified,
6630 they belong to the same unnamed package */
6631 if (!ctxp->package && !qualified_flag)
6634 /* Compare the left part of the name of CLASS with the package name */
6635 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
6636 if (ctxp->package == left)
6644 /* This function may generate code to access DECL from WHERE. This is
6645 done only if certain conditions meet. */
6648 maybe_access_field (decl, where, type)
6649 tree decl, where, type;
6651 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
6652 && !FIELD_STATIC (decl))
6653 decl = build_field_ref (where ? where : current_this,
6654 (type ? type : DECL_CONTEXT (decl)),
6659 /* Build a method invocation, by patching PATCH. If non NULL
6660 and according to the situation, PRIMARY and WHERE may be
6661 used. IS_STATIC is set to 1 if the invoked function is static. */
6664 patch_method_invocation (patch, primary, where, is_static, ret_decl)
6665 tree patch, primary, where;
6669 tree wfl = TREE_OPERAND (patch, 0);
6670 tree args = TREE_OPERAND (patch, 1);
6671 tree name = EXPR_WFL_NODE (wfl);
6673 int is_static_flag = 0;
6674 int is_super_init = 0;
6675 tree this_arg = NULL_TREE;
6677 /* Should be overriden if everything goes well. Otherwise, if
6678 something fails, it should keep this value. It stop the
6679 evaluation of a bogus assignment. See java_complete_tree,
6680 MODIFY_EXPR: for the reasons why we sometimes want to keep on
6681 evaluating an assignment */
6682 TREE_TYPE (patch) = error_mark_node;
6684 /* Since lookup functions are messing with line numbers, save the
6686 java_parser_context_save_global ();
6688 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
6690 /* Resolution of qualified name, excluding constructors */
6691 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
6693 tree class_decl, identifier, identifier_wfl;
6694 /* Extract the last IDENTIFIER of the qualified
6695 expression. This is a wfl and we will use it's location
6696 data during error report. */
6697 identifier_wfl = cut_identifier_in_qualified (wfl);
6698 identifier = EXPR_WFL_NODE (identifier_wfl);
6700 /* Given the context, IDENTIFIER is syntactically qualified
6701 as a MethodName. We need to qualify what's before */
6702 qualify_ambiguous_name (wfl);
6704 /* Package resolution are erroneous */
6705 if (RESOLVE_PACKAGE_NAME_P (wfl))
6708 breakdown_qualified (&remainder, NULL, EXPR_WFL_NODE (wfl));
6709 parse_error_context (wfl, "Can't search method `%s' in package "
6710 "`%s'",IDENTIFIER_POINTER (identifier),
6711 IDENTIFIER_POINTER (remainder));
6712 PATCH_METHOD_RETURN_ERROR ();
6714 /* We're resolving a call from a type */
6715 else if (RESOLVE_TYPE_NAME_P (wfl))
6717 tree decl = QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl));
6718 tree name = DECL_NAME (decl);
6721 class_decl = resolve_and_layout (name, wfl);
6722 if (CLASS_INTERFACE (decl))
6725 (identifier_wfl, "Can't make static reference to method "
6726 "`%s' in interface `%s'", IDENTIFIER_POINTER (identifier),
6727 IDENTIFIER_POINTER (name));
6728 PATCH_METHOD_RETURN_ERROR ();
6730 /* Look the method up in the type selector. The method ought
6732 type = TREE_TYPE (class_decl);
6733 list = lookup_method_invoke (0, wfl, type, identifier, args);
6734 if (list && !METHOD_STATIC (list))
6736 char *fct_name = strdup (lang_printable_name (list, 0));
6739 "Can't make static reference to method `%s %s' in class `%s'",
6740 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6741 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6743 PATCH_METHOD_RETURN_ERROR ();
6745 args = nreverse (args);
6747 /* We're resolving an expression name */
6752 /* 1- Find the field to which the call applies */
6753 field = resolve_field_access (wfl, NULL, &type);
6754 if (field == error_mark_node)
6755 PATCH_METHOD_RETURN_ERROR ();
6756 /* field is used in lieu of a primary. It alows us not to
6757 report errors on erroneous use of `this' in
6761 /* 2- Do the layout of the class where the last field
6762 was found, so we can search it. */
6763 class_decl = resolve_and_layout (type, NULL_TREE);
6764 if (class_decl != NULL_TREE)
6765 type = TREE_TYPE (class_decl);
6767 /* 3- Retrieve a filtered list of method matches, Refine
6768 if necessary. In any cases, point out errors. */
6769 list = lookup_method_invoke (0, identifier_wfl, type,
6772 /* 4- Add the field as an argument */
6773 args = nreverse (args);
6777 /* IDENTIFIER_WFL will be used to report any problem further */
6778 wfl = identifier_wfl;
6780 /* Resolution of simple names, names generated after a primary: or
6784 tree class_to_search;
6785 int lc; /* Looking for Constructor */
6787 /* We search constructor in their target class */
6788 if (CALL_CONSTRUCTOR_P (patch))
6790 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6791 class_to_search = EXPR_WFL_NODE (wfl);
6792 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6793 this_identifier_node)
6794 class_to_search = NULL_TREE;
6795 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6796 super_identifier_node)
6799 if (CLASSTYPE_SUPER (current_class))
6801 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
6804 parse_error_context (wfl, "Can't invoke super constructor "
6805 "on java.lang.Object");
6806 PATCH_METHOD_RETURN_ERROR ();
6810 /* Class to search is NULL if we're searching the current one */
6811 if (class_to_search)
6813 class_to_search = resolve_and_layout (class_to_search,
6815 if (!class_to_search)
6818 (wfl, "Class `%s' not found in type declaration",
6819 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6820 PATCH_METHOD_RETURN_ERROR ();
6823 /* Can't instantiate an abstract class, but we can
6824 invoke it's constructor. It's use within the `new'
6825 context is denied here. */
6826 if (CLASS_ABSTRACT (class_to_search)
6827 && TREE_CODE (patch) == NEW_CLASS_EXPR)
6830 (wfl, "Class `%s' is an abstract class. It can't be "
6831 "instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6832 PATCH_METHOD_RETURN_ERROR ();
6834 class_to_search = TREE_TYPE (class_to_search);
6837 class_to_search = current_class;
6840 /* This is a regular search in the local class, unless an
6841 alternate class is specified. */
6844 class_to_search = (where ? where : current_class);
6848 /* NAME is a simple identifier or comes from a primary. Search
6849 in the class whose declaration contain the method being
6851 resolve_and_layout (class_to_search, NULL_TREE);
6852 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
6854 /* Don't continue if no method were found, as the next statement
6855 can't be executed then. */
6857 PATCH_METHOD_RETURN_ERROR ();
6859 /* Check for static reference if non static methods */
6860 if (check_for_static_method_reference (wfl, patch, list,
6861 class_to_search, primary))
6862 PATCH_METHOD_RETURN_ERROR ();
6864 /* Non static methods are called with the current object extra
6865 argument. If patch a `new TYPE()', the argument is the value
6866 returned by the object allocator. If method is resolved as a
6867 primary, use the primary otherwise use the current THIS. */
6868 args = nreverse (args);
6869 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
6870 this_arg = primary ? primary : current_this;
6873 /* Merge point of all resolution schemes. If we have nothing, this
6874 is an error, already signaled */
6876 PATCH_METHOD_RETURN_ERROR ();
6878 /* Check accessibility, position the is_static flag, build and
6880 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
6882 char *fct_name = strdup (lang_printable_name (list, 0));
6884 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
6885 java_accstring_lookup (get_access_flags_from_decl (list)),
6886 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6887 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
6888 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6890 PATCH_METHOD_RETURN_ERROR ();
6892 check_deprecation (wfl, list);
6894 is_static_flag = METHOD_STATIC (list);
6895 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
6896 args = tree_cons (NULL_TREE, this_arg, args);
6898 /* In the context of an explicit constructor invocation, we can't
6899 invoke any method relying on `this'. Exceptions are: we're
6900 invoking a static function, primary exists and is not the current
6901 this, we're creating a new object. */
6902 if (ctxp->explicit_constructor_p
6904 && (!primary || primary == current_this)
6905 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
6908 (wfl, "Can't reference `this' before the superclass constructor has "
6910 PATCH_METHOD_RETURN_ERROR ();
6912 java_parser_context_restore_global ();
6914 *is_static = is_static_flag;
6915 /* Sometimes, we want the decl of the selected method. Such as for
6919 patch = patch_invoke (patch, list, args);
6920 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
6922 /* Generate the code used to initialize fields declared with an
6923 initialization statement. For now, it returns a call the the
6924 artificial function $finit$, if required. */
6927 build_method_invocation (build_expr_wfl (finit_identifier_node,
6928 input_filename, 0, 0),
6930 patch = build (COMPOUND_EXPR, void_type_node, patch,
6931 java_complete_tree (finit_call));
6932 CAN_COMPLETE_NORMALLY (patch) = 1;
6937 /* Check that we're not trying to do a static reference to a method in
6938 non static method. Return 1 if it's the case, 0 otherwise. */
6941 check_for_static_method_reference (wfl, node, method, where, primary)
6942 tree wfl, node, method, where, primary;
6944 if (METHOD_STATIC (current_function_decl)
6945 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
6947 char *fct_name = strdup (lang_printable_name (method, 0));
6949 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
6950 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
6951 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
6958 /* Patch an invoke expression METHOD and ARGS, based on its invocation
6962 patch_invoke (patch, method, args)
6963 tree patch, method, args;
6966 tree original_call, t, ta;
6968 /* Last step for args: convert build-in types. If we're dealing with
6969 a new TYPE() type call, the first argument to the constructor
6970 isn't found in the incomming argument list, but delivered by
6972 t = TYPE_ARG_TYPES (TREE_TYPE (method));
6973 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6975 for (ta = args; t != end_params_node && ta;
6976 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
6977 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
6978 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
6979 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
6981 if (flag_emit_class_files)
6985 tree signature = build_java_signature (TREE_TYPE (method));
6986 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
6988 case INVOKE_VIRTUAL:
6989 dtable = invoke_build_dtable (0, args);
6990 func = build_invokevirtual (dtable, method);
6995 func = build_known_method_ref (method, TREE_TYPE (method),
6996 DECL_CONTEXT (method),
7000 case INVOKE_INTERFACE:
7001 dtable = invoke_build_dtable (1, args);
7002 func = build_invokeinterface (dtable, DECL_NAME (method), signature);
7006 fatal ("internal error - unknown invocation_mode result");
7009 /* Ensure self_type is initialized, (invokestatic). FIXME */
7010 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
7013 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
7014 TREE_OPERAND (patch, 0) = func;
7015 TREE_OPERAND (patch, 1) = args;
7016 original_call = patch;
7018 /* We're processing a `new TYPE ()' form. New is called an its
7019 returned value is the first argument to the constructor. We build
7020 a COMPOUND_EXPR and use saved expression so that the overall NEW
7021 expression value is a pointer to a newly created and initialized
7023 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
7025 tree class = DECL_CONTEXT (method);
7026 tree c1, saved_new, size, new;
7027 if (flag_emit_class_files)
7029 TREE_TYPE (patch) = build_pointer_type (class);
7032 if (!TYPE_SIZE (class))
7033 safe_layout_class (class);
7034 size = size_in_bytes (class);
7035 new = build (CALL_EXPR, promote_type (class),
7036 build_address_of (alloc_object_node),
7037 tree_cons (NULL_TREE, build_class_ref (class),
7038 build_tree_list (NULL_TREE,
7039 size_in_bytes (class))),
7041 saved_new = save_expr (new);
7042 c1 = build_tree_list (NULL_TREE, saved_new);
7043 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
7044 TREE_OPERAND (original_call, 1) = c1;
7045 TREE_SET_CODE (original_call, CALL_EXPR);
7046 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
7052 invocation_mode (method, super)
7056 int access = get_access_flags_from_decl (method);
7059 return INVOKE_SUPER;
7061 if (access & ACC_STATIC || access & ACC_FINAL || access & ACC_PRIVATE)
7062 return INVOKE_STATIC;
7064 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
7065 return INVOKE_STATIC;
7067 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
7068 return INVOKE_INTERFACE;
7070 if (DECL_CONSTRUCTOR_P (method))
7071 return INVOKE_STATIC;
7073 return INVOKE_VIRTUAL;
7076 /* Retrieve a refined list of matching methods. It covers the step
7077 15.11.2 (Compile-Time Step 2) */
7080 lookup_method_invoke (lc, cl, class, name, arg_list)
7083 tree class, name, arg_list;
7085 tree atl = end_params_node; /* Arg Type List */
7086 tree method, signature, list, node;
7087 char *candidates; /* Used for error report */
7089 /* Fix the arguments */
7090 for (node = arg_list; node; node = TREE_CHAIN (node))
7092 tree current_arg = TREE_TYPE (TREE_VALUE (node));
7093 /* Non primitive type may have to be resolved */
7094 if (!JPRIMITIVE_TYPE_P (current_arg))
7095 resolve_and_layout (current_arg, NULL_TREE);
7097 if (TREE_CODE (current_arg) == RECORD_TYPE)
7098 current_arg = promote_type (current_arg);
7099 atl = tree_cons (NULL_TREE, current_arg, atl);
7102 /* Find all candidates and then refine the list, searching for the
7103 most specific method. */
7104 list = find_applicable_accessible_methods_list (lc, class, name, atl);
7105 list = find_most_specific_methods_list (list);
7106 if (list && !TREE_CHAIN (list))
7107 return TREE_VALUE (list);
7109 /* Issue an error. List candidates if any. Candidates are listed
7110 only if accessible (non accessible methods may end-up here for
7111 the sake of a better error report). */
7116 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
7117 for (current = list; current; current = TREE_CHAIN (current))
7119 tree cm = TREE_VALUE (current);
7121 if (!cm || not_accessible_p (class, cm, 0))
7124 (string, " `%s' in `%s'%s",
7125 get_printable_method_name (cm),
7126 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
7127 (TREE_CHAIN (current) ? "\n" : ""));
7128 obstack_grow (&temporary_obstack, string, strlen (string));
7130 obstack_1grow (&temporary_obstack, '\0');
7131 candidates = obstack_finish (&temporary_obstack);
7133 /* Issue the error message */
7134 method = make_node (FUNCTION_TYPE);
7135 TYPE_ARG_TYPES (method) = atl;
7136 signature = build_java_argument_signature (method);
7137 parse_error_context (cl, "Can't find %s `%s(%s)' in class `%s'%s",
7138 (lc ? "constructor" : "method"),
7140 IDENTIFIER_POINTER(DECL_NAME (TYPE_NAME (class))) :
7141 IDENTIFIER_POINTER (name)),
7142 IDENTIFIER_POINTER (signature),
7143 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))),
7144 (candidates ? candidates : ""));
7148 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
7149 when we're looking for a constructor. */
7152 find_applicable_accessible_methods_list (lc, class, name, arglist)
7154 tree class, name, arglist;
7157 tree list = NULL_TREE, all_list = NULL_TREE;
7159 while (class != NULL_TREE)
7161 for (method = TYPE_METHODS (class);
7162 method != NULL_TREE; method = TREE_CHAIN (method))
7164 if (lc && !DECL_CONSTRUCTOR_P (method))
7166 else if (!lc && (DECL_CONSTRUCTOR_P (method)
7167 || (GET_METHOD_NAME (method) != name)))
7170 if (argument_types_convertible (method, arglist))
7172 /* Retain accessible methods only */
7173 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
7175 list = tree_cons (NULL_TREE, method, list);
7177 /* Also retain all selected method here */
7178 all_list = tree_cons (NULL_TREE, method, list);
7181 /* When dealing with constructor, stop here, otherwise search
7183 class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
7185 /* Either return the list obtained or all selected (but
7186 inaccessible) methods for better error report. */
7187 return (!list ? all_list : list);
7190 /* 15.11.2.2 Choose the Most Specific Method */
7193 find_most_specific_methods_list (list)
7197 tree current, new_list = NULL_TREE;
7198 for (current = list; current; current = TREE_CHAIN (current))
7201 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
7203 for (method = list; method; method = TREE_CHAIN (method))
7205 /* Don't test a method against itself */
7206 if (method == current)
7209 /* Compare arguments and location where method where declared */
7210 if (argument_types_convertible (TREE_VALUE (method),
7211 TREE_VALUE (current))
7212 && valid_method_invocation_conversion_p
7213 (DECL_CONTEXT (TREE_VALUE (method)),
7214 DECL_CONTEXT (TREE_VALUE (current))))
7216 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
7217 max = (v > max ? v : max);
7222 /* Review the list and select the maximally specific methods */
7223 for (current = list; current; current = TREE_CHAIN (current))
7224 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7225 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7227 /* If we can't find one, lower expectations and try to gather multiple
7228 maximally specific methods */
7233 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7234 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7242 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
7243 converted by method invocation conversion (5.3) to the type of the
7244 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
7245 to change less often than M1. */
7248 argument_types_convertible (m1, m2_or_arglist)
7249 tree m1, m2_or_arglist;
7251 static tree m2_arg_value = NULL_TREE;
7252 static tree m2_arg_cache = NULL_TREE;
7254 register tree m1_arg, m2_arg;
7256 m1_arg = TYPE_ARG_TYPES (TREE_TYPE (m1));
7257 if (!METHOD_STATIC (m1))
7258 m1_arg = TREE_CHAIN (m1_arg);
7260 if (m2_arg_value == m2_or_arglist)
7261 m2_arg = m2_arg_cache;
7264 /* M2_OR_ARGLIST can be a function DECL or a raw list of
7266 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
7268 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
7269 if (!METHOD_STATIC (m2_or_arglist))
7270 m2_arg = TREE_CHAIN (m2_arg);
7273 m2_arg = m2_or_arglist;
7275 m2_arg_value = m2_or_arglist;
7276 m2_arg_cache = m2_arg;
7279 while (m1_arg != end_params_node && m2_arg != end_params_node)
7281 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
7282 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
7283 TREE_VALUE (m2_arg)))
7285 m1_arg = TREE_CHAIN (m1_arg);
7286 m2_arg = TREE_CHAIN (m2_arg);
7288 return m1_arg == end_params_node && m2_arg == end_params_node;
7291 /* Qualification routines */
7294 qualify_ambiguous_name (id)
7297 tree qual, qual_wfl, name, decl, ptr_type, saved_current_class;
7298 int again, super_found = 0, this_found = 0, new_array_found = 0;
7300 /* We first qualify the first element, then derive qualification of
7301 others based on the first one. If the first element is qualified
7302 by a resolution (field or type), this resolution is stored in the
7303 QUAL_RESOLUTION of the qual element being examined. We need to
7304 save the current_class since the use of SUPER might change the
7306 saved_current_class = current_class;
7307 qual = EXPR_WFL_QUALIFICATION (id);
7310 /* Simple qualified expression feature a qual_wfl that is a
7311 WFL. Expression derived from a primary feature more complicated
7312 things like a CALL_EXPR. Expression from primary need to be
7313 worked out to extract the part on which the qualification will
7315 qual_wfl = QUAL_WFL (qual);
7316 switch (TREE_CODE (qual_wfl))
7319 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7320 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
7322 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
7323 qual_wfl = QUAL_WFL (qual);
7326 case NEW_ARRAY_EXPR:
7327 qual = TREE_CHAIN (qual);
7328 new_array_found = again = 1;
7330 case NEW_CLASS_EXPR:
7332 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7335 while (TREE_CODE (qual_wfl) == ARRAY_REF)
7336 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7339 /* Fix for -Wall. Just break doing nothing */
7342 name = EXPR_WFL_NODE (qual_wfl);
7343 ptr_type = current_class;
7345 /* If we have a THIS (from a primary), we set the context accordingly */
7346 if (name == this_identifier_node)
7348 qual = TREE_CHAIN (qual);
7349 qual_wfl = QUAL_WFL (qual);
7350 if (TREE_CODE (qual_wfl) == CALL_EXPR)
7353 name = EXPR_WFL_NODE (qual_wfl);
7356 /* If we have a SUPER, we set the context accordingly */
7357 if (name == super_identifier_node)
7359 current_class = CLASSTYPE_SUPER (ptr_type);
7360 /* Check that there is such a thing as a super class. If not,
7361 return. The error will be caught later on, during the
7365 current_class = saved_current_class;
7368 qual = TREE_CHAIN (qual);
7369 /* Do one more interation to set things up */
7370 super_found = again = 1;
7372 /* Loop one more time if we're dealing with ?: or a string
7373 constant, or a convert expression */
7374 if (TREE_CODE (qual_wfl) == CONDITIONAL_EXPR
7375 || TREE_CODE (qual_wfl) == STRING_CST
7376 || TREE_CODE (qual_wfl) == CONVERT_EXPR)
7378 qual = TREE_CHAIN (qual);
7379 qual_wfl = QUAL_WFL (qual);
7384 /* If name appears within the scope of a location variable
7385 declaration or parameter declaration, then it is an expression
7386 name. We don't carry this test out if we're in the context of the
7387 use of SUPER or THIS */
7388 if (!this_found && !super_found && (decl = IDENTIFIER_LOCAL_VALUE (name)))
7390 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7391 QUAL_RESOLUTION (qual) = decl;
7394 /* If within the class/interface NAME was found to be used there
7395 exists a (possibly inherited) field named NAME, then this is an
7396 expression name. If we saw a NEW_ARRAY_EXPR before and want to
7397 address length, it is OK. */
7398 else if ((decl = lookup_field_wrapper (ptr_type, name))
7399 || (new_array_found && name == length_identifier_node))
7401 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7402 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
7405 /* We reclassify NAME as a type name if:
7406 - NAME is a class/interface declared within the compilation
7407 unit containing NAME,
7408 - NAME is imported via a single-type-import declaration,
7409 - NAME is declared in an another compilation unit of the package
7410 of the compilation unit containing NAME,
7411 - NAME is declared by exactly on type-import-on-demand declaration
7412 of the compilation unit containing NAME. */
7413 else if ((decl = resolve_and_layout (name, NULL_TREE)))
7415 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
7416 QUAL_RESOLUTION (qual) = decl;
7419 /* Method call are expression name */
7420 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
7421 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF)
7422 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7424 /* Check here that NAME isn't declared by more than one
7425 type-import-on-demand declaration of the compilation unit
7426 containing NAME. FIXME */
7428 /* Otherwise, NAME is reclassified as a package name */
7430 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
7432 /* Propagate the qualification accross other components of the
7434 for (qual = TREE_CHAIN (qual); qual;
7435 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
7437 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7438 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
7440 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
7443 /* Store the global qualification for the ambiguous part of ID back
7445 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
7446 RESOLVE_EXPRESSION_NAME_P (id) = 1;
7447 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
7448 RESOLVE_TYPE_NAME_P (id) = 1;
7449 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7450 RESOLVE_PACKAGE_NAME_P (id) = 1;
7452 /* Restore the current class */
7453 current_class = saved_current_class;
7457 breakdown_qualified (left, right, source)
7458 tree *left, *right, source;
7460 char *p = IDENTIFIER_POINTER (source), *base;
7461 int l = IDENTIFIER_LENGTH (source);
7463 /* Breakdown NAME into REMAINDER . IDENTIFIER */
7466 while (*p != '.' && p != base)
7469 /* We didn't find a '.'. Return an error */
7475 *right = get_identifier (p+1);
7476 *left = get_identifier (IDENTIFIER_POINTER (source));
7482 /* Patch tree nodes in a function body. When a BLOCK is found, push
7483 local variable decls if present.
7484 Same as java_complete_lhs, but does resolve static finals to values. */
7487 java_complete_tree (node)
7490 node = java_complete_lhs (node);
7491 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7492 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE)
7494 tree value = DECL_INITIAL (node);
7495 DECL_INITIAL (node) = NULL_TREE;
7496 value = fold_constant_for_init (value, node);
7497 DECL_INITIAL (node) = value;
7498 if (value != NULL_TREE)
7504 /* Patch tree nodes in a function body. When a BLOCK is found, push
7505 local variable decls if present.
7506 Same as java_complete_tree, but does not resolve static finals to values. */
7509 java_complete_lhs (node)
7512 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
7515 /* CONVERT_EXPR always has its type set, even though it needs to be
7517 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
7520 /* The switch block implements cases processing container nodes
7521 first. Contained nodes are always written back. Leaves come
7522 next and return a value. */
7523 switch (TREE_CODE (node))
7527 /* 1- Block section.
7528 Set the local values on decl names so we can identify them
7529 faster when they're referenced. At that stage, identifiers
7530 are legal so we don't check for declaration errors. */
7531 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7533 DECL_CONTEXT (cn) = current_function_decl;
7534 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
7535 INITIALIZED_P (cn) = 0;
7537 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
7538 CAN_COMPLETE_NORMALLY (node) = 1;
7541 tree stmt = BLOCK_EXPR_BODY (node);
7544 if (TREE_CODE (stmt) == COMPOUND_EXPR)
7546 /* Re-order from (((A; B); C); ...; Z) to
7547 (A; (B; (C ; (...; Z)))).
7548 This makes it easier to scan the statements left-to-right
7549 without using recursion (which might overflow the stack
7550 if the block has many statements. */
7553 tree left = TREE_OPERAND (stmt, 0);
7554 if (TREE_CODE (left) != COMPOUND_EXPR)
7556 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
7557 TREE_OPERAND (left, 1) = stmt;
7560 BLOCK_EXPR_BODY (node) = stmt;
7563 /* Now do the actual complete, without deep recursion for
7565 ptr = &BLOCK_EXPR_BODY (node);
7566 while (TREE_CODE (*ptr) == COMPOUND_EXPR)
7568 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
7569 tree *next = &TREE_OPERAND (*ptr, 1);
7570 TREE_OPERAND (*ptr, 0) = cur;
7571 if (TREE_CODE (cur) == ERROR_MARK)
7573 else if (! CAN_COMPLETE_NORMALLY (cur))
7578 if (TREE_CODE (wfl_op2) == BLOCK)
7579 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
7580 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
7581 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
7585 if (TREE_CODE (wfl_op2) != CASE_EXPR
7586 && TREE_CODE (wfl_op2) != DEFAULT_EXPR
7587 && wfl_op2 != empty_stmt_node)
7588 unreachable_stmt_error (*ptr);
7592 *ptr = java_complete_tree (*ptr);
7594 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
7595 return error_mark_node;
7596 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
7598 /* Turn local bindings to null */
7599 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7600 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
7602 TREE_TYPE (node) = void_type_node;
7605 /* 2- They are expressions but ultimately deal with statements */
7608 wfl_op1 = TREE_OPERAND (node, 0);
7609 COMPLETE_CHECK_OP_0 (node);
7610 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7611 return patch_throw_statement (node, wfl_op1);
7613 case SYNCHRONIZED_EXPR:
7614 wfl_op1 = TREE_OPERAND (node, 0);
7615 return patch_synchronized_statement (node, wfl_op1);
7618 return patch_try_statement (node);
7620 case TRY_FINALLY_EXPR:
7621 COMPLETE_CHECK_OP_0 (node);
7622 COMPLETE_CHECK_OP_1 (node);
7623 CAN_COMPLETE_NORMALLY (node)
7624 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
7625 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
7626 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
7629 case CLEANUP_POINT_EXPR:
7630 COMPLETE_CHECK_OP_0 (node);
7631 TREE_TYPE (node) = void_type_node;
7632 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
7635 case WITH_CLEANUP_EXPR:
7636 COMPLETE_CHECK_OP_0 (node);
7637 COMPLETE_CHECK_OP_2 (node);
7638 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
7639 TREE_TYPE (node) = void_type_node;
7642 case LABELED_BLOCK_EXPR:
7643 PUSH_LABELED_BLOCK (node);
7644 if (LABELED_BLOCK_BODY (node))
7645 COMPLETE_CHECK_OP_1 (node);
7646 TREE_TYPE (node) = void_type_node;
7647 POP_LABELED_BLOCK ();
7648 if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
7649 CAN_COMPLETE_NORMALLY (node) = 1;
7652 case EXIT_BLOCK_EXPR:
7653 /* We don't complete operand 1, because it's the return value of
7654 the EXIT_BLOCK_EXPR which doesn't exist it Java */
7655 return patch_bc_statement (node);
7658 cn = java_complete_tree (TREE_OPERAND (node, 0));
7659 if (cn == error_mark_node)
7662 /* First, the case expression must be constant */
7665 if (!TREE_CONSTANT (cn))
7667 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7668 parse_error_context (node, "Constant expression required");
7669 return error_mark_node;
7672 nn = ctxp->current_loop;
7674 /* It must be assignable to the type of the switch expression. */
7675 if (!try_builtin_assignconv (NULL_TREE,
7676 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
7678 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7681 "Incompatible type for case. Can't convert `%s' to `int'",
7682 lang_printable_name (TREE_TYPE (cn), 0));
7683 return error_mark_node;
7686 cn = fold (convert (int_type_node, cn));
7688 /* Multiple instance of a case label bearing the same
7689 value is checked during code generation. The case
7690 expression is allright so far. */
7691 TREE_OPERAND (node, 0) = cn;
7692 TREE_TYPE (node) = void_type_node;
7693 CAN_COMPLETE_NORMALLY (node) = 1;
7694 TREE_SIDE_EFFECTS (node) = 1;
7698 nn = ctxp->current_loop;
7699 /* Only one default label is allowed per switch statement */
7700 if (SWITCH_HAS_DEFAULT (nn))
7702 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7703 parse_error_context (wfl_operator,
7704 "Duplicate case label: `default'");
7705 return error_mark_node;
7708 SWITCH_HAS_DEFAULT (nn) = 1;
7709 TREE_TYPE (node) = void_type_node;
7710 TREE_SIDE_EFFECTS (node) = 1;
7711 CAN_COMPLETE_NORMALLY (node) = 1;
7717 /* Check whether the loop was enclosed in a labeled
7718 statement. If not, create one, insert the loop in it and
7720 nn = patch_loop_statement (node);
7722 /* Anyways, walk the body of the loop */
7723 if (TREE_CODE (node) == LOOP_EXPR)
7724 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7725 /* Switch statement: walk the switch expression and the cases */
7727 node = patch_switch_statement (node);
7729 if (TREE_OPERAND (node, 0) == error_mark_node)
7730 return error_mark_node;
7731 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
7732 /* If we returned something different, that's because we
7733 inserted a label. Pop the label too. */
7736 if (CAN_COMPLETE_NORMALLY (node))
7737 CAN_COMPLETE_NORMALLY (nn) = 1;
7738 POP_LABELED_BLOCK ();
7744 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7745 return patch_exit_expr (node);
7749 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7750 if (TREE_OPERAND (node, 0) == error_mark_node)
7751 return error_mark_node;
7752 /* then-else branches */
7753 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7754 if (TREE_OPERAND (node, 1) == error_mark_node)
7755 return error_mark_node;
7756 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
7757 if (TREE_OPERAND (node, 2) == error_mark_node)
7758 return error_mark_node;
7759 return patch_if_else_statement (node);
7762 case CONDITIONAL_EXPR:
7764 wfl_op1 = TREE_OPERAND (node, 0);
7765 COMPLETE_CHECK_OP_0 (node);
7766 wfl_op2 = TREE_OPERAND (node, 1);
7767 COMPLETE_CHECK_OP_1 (node);
7768 wfl_op3 = TREE_OPERAND (node, 2);
7769 COMPLETE_CHECK_OP_2 (node);
7770 return patch_conditional_expr (node, wfl_op1, wfl_op2);
7772 /* 3- Expression section */
7774 wfl_op2 = TREE_OPERAND (node, 1);
7775 TREE_OPERAND (node, 0) = nn =
7776 java_complete_tree (TREE_OPERAND (node, 0));
7777 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK
7778 && wfl_op2 != empty_stmt_node)
7780 /* An unreachable condition in a do-while statement
7781 is *not* (technically) an unreachable statement. */
7783 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
7784 nn = EXPR_WFL_NODE (nn);
7785 if (TREE_CODE (nn) != EXIT_EXPR)
7787 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
7788 parse_error_context (wfl_operator, "Unreachable statement");
7791 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7792 if (TREE_OPERAND (node, 1) == error_mark_node)
7793 return error_mark_node;
7794 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
7795 CAN_COMPLETE_NORMALLY (node)
7796 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
7800 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7801 return patch_return (node);
7803 case EXPR_WITH_FILE_LOCATION:
7804 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
7805 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
7807 node = resolve_expression_name (node, NULL);
7808 CAN_COMPLETE_NORMALLY (node) = 1;
7813 int save_lineno = lineno;
7814 lineno = EXPR_WFL_LINENO (node);
7815 body = java_complete_tree (EXPR_WFL_NODE (node));
7816 lineno = save_lineno;
7817 EXPR_WFL_NODE (node) = body;
7818 TREE_SIDE_EFFECTS (node) = 1;
7819 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
7820 if (EXPR_WFL_NODE (node) == error_mark_node)
7822 /* Its important for the evaluation of assignment that
7823 this mark on the TREE_TYPE is propagated. */
7824 TREE_TYPE (node) = error_mark_node;
7825 return error_mark_node;
7828 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
7833 case NEW_ARRAY_EXPR:
7834 /* Patch all the dimensions */
7836 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
7838 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
7839 tree dim = java_complete_tree (TREE_VALUE (cn));
7840 if (dim == error_mark_node)
7847 TREE_VALUE (cn) = dim;
7848 /* Setup the location of the current dimension, for
7849 later error report. */
7851 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
7852 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
7855 /* They complete the array creation expression, if no errors
7857 CAN_COMPLETE_NORMALLY (node) = 1;
7858 return (flag ? error_mark_node : patch_newarray (node));
7860 case NEW_CLASS_EXPR:
7862 /* Complete function's argument(s) first */
7863 if (complete_function_arguments (node))
7864 return error_mark_node;
7867 tree decl, wfl = TREE_OPERAND (node, 0);
7868 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
7870 node = patch_method_invocation (node, NULL_TREE,
7871 NULL_TREE, 0, &decl);
7872 if (node == error_mark_node)
7873 return error_mark_node;
7875 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
7876 /* If we call this(...), register signature and positions */
7878 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
7879 tree_cons (wfl, decl,
7880 DECL_CONSTRUCTOR_CALLS (current_function_decl));
7881 CAN_COMPLETE_NORMALLY (node) = 1;
7886 /* Save potential wfls */
7887 wfl_op1 = TREE_OPERAND (node, 0);
7888 wfl_op2 = TREE_OPERAND (node, 1);
7889 TREE_OPERAND (node, 0) = java_complete_lhs (wfl_op1);
7890 if (TREE_OPERAND (node, 0) == error_mark_node)
7891 return error_mark_node;
7893 if (COMPOUND_ASSIGN_P (wfl_op2))
7897 java_complete_tree (TREE_OPERAND (wfl_op2, 0));
7899 /* Hand stablize the lhs on both places */
7900 lvalue = stabilize_reference (other);
7901 TREE_OPERAND (node, 0) = lvalue;
7902 TREE_OPERAND (TREE_OPERAND (node, 1), 0) = lvalue;
7905 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
7906 function to complete this RHS */
7907 if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT)
7908 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
7909 TREE_OPERAND (node, 1));
7911 nn = java_complete_tree (TREE_OPERAND (node, 1));
7913 /* There are cases where the type of RHS is fixed. In those
7914 cases, if the evaluation of the RHS fails, we further the
7915 evaluation of the assignment to detect more errors. */
7916 if (nn == error_mark_node)
7918 /* It's hopeless, but we can further things on to discover
7919 an error during the assignment. In any cases, the
7920 assignment operation fails. */
7921 if (TREE_CODE (TREE_OPERAND (node, 1)) != EXPR_WITH_FILE_LOCATION
7922 && TREE_CODE (TREE_OPERAND (node, 1)) != NEW_ARRAY_INIT
7923 && TREE_TYPE (TREE_OPERAND (node, 1)) != error_mark_node)
7924 patch_assignment (node, wfl_op1, wfl_op2);
7926 /* Now, we still mark the lhs as initialized */
7927 if (JDECL_P (TREE_OPERAND (node, 0)))
7928 INITIALIZED_P (TREE_OPERAND (node, 0)) = 1;
7930 return error_mark_node;
7932 TREE_OPERAND (node, 1) = nn;
7934 /* In case we're handling = with a String as a RHS, we need to
7935 produce a String out of the RHS (it might still be a
7936 STRING_CST or a StringBuffer at this stage */
7937 if ((nn = patch_string (TREE_OPERAND (node, 1))))
7938 TREE_OPERAND (node, 1) = nn;
7939 node = patch_assignment (node, wfl_op1, wfl_op2);
7940 CAN_COMPLETE_NORMALLY (node) = 1;
7942 /* Before returning the node, in the context of a static field
7943 assignment in <clinit>, we may want to carray further
7944 optimizations. (VAR_DECL means it's a static field. See
7946 if (DECL_NAME (current_function_decl) == clinit_identifier_node
7947 && MODIFY_EXPR_FROM_INITIALIZATION_P (node)
7948 && TREE_CODE (TREE_OPERAND (node, 0)) == VAR_DECL)
7949 node = patch_initialized_static_field (node);
7962 case TRUNC_MOD_EXPR:
7964 case TRUTH_ANDIF_EXPR:
7965 case TRUTH_ORIF_EXPR:
7972 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
7973 knows how to handle those cases. */
7974 wfl_op1 = TREE_OPERAND (node, 0);
7975 wfl_op2 = TREE_OPERAND (node, 1);
7977 CAN_COMPLETE_NORMALLY (node) = 1;
7978 /* Don't complete string nodes if dealing with the PLUS operand. */
7979 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
7981 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7982 if (TREE_OPERAND (node, 0) == error_mark_node)
7983 return error_mark_node;
7985 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
7987 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
7988 if (TREE_OPERAND (node, 1) == error_mark_node)
7989 return error_mark_node;
7991 return patch_binop (node, wfl_op1, wfl_op2);
7993 case INSTANCEOF_EXPR:
7994 wfl_op1 = TREE_OPERAND (node, 0);
7995 COMPLETE_CHECK_OP_0 (node);
7996 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
7998 case UNARY_PLUS_EXPR:
8000 case TRUTH_NOT_EXPR:
8002 case PREDECREMENT_EXPR:
8003 case PREINCREMENT_EXPR:
8004 case POSTDECREMENT_EXPR:
8005 case POSTINCREMENT_EXPR:
8007 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
8008 how to handle those cases. */
8009 wfl_op1 = TREE_OPERAND (node, 0);
8010 CAN_COMPLETE_NORMALLY (node) = 1;
8011 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
8012 if (TREE_OPERAND (node, 0) == error_mark_node)
8013 return error_mark_node;
8014 node = patch_unaryop (node, wfl_op1);
8015 CAN_COMPLETE_NORMALLY (node) = 1;
8019 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
8020 how to handle those cases. */
8021 wfl_op1 = TREE_OPERAND (node, 0);
8022 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
8023 if (TREE_OPERAND (node, 0) == error_mark_node)
8024 return error_mark_node;
8025 if (!flag_emit_class_files)
8026 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
8027 /* The same applies to wfl_op2 */
8028 wfl_op2 = TREE_OPERAND (node, 1);
8029 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
8030 if (TREE_OPERAND (node, 1) == error_mark_node)
8031 return error_mark_node;
8032 if (!flag_emit_class_files)
8033 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
8034 return patch_array_ref (node);
8040 /* The first step in the re-write of qualified name handling. FIXME.
8041 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
8042 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
8043 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
8045 tree name = TREE_OPERAND (node, 1);
8046 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
8047 if (field == NULL_TREE)
8049 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
8050 return error_mark_node;
8052 if (! FIELD_STATIC (field))
8054 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
8055 return error_mark_node;
8060 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
8064 /* Can't use THIS in a static environment */
8067 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8068 parse_error_context (wfl_operator, "Keyword `this' used outside "
8070 TREE_TYPE (node) = error_mark_node;
8071 return error_mark_node;
8073 if (ctxp->explicit_constructor_p)
8075 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8077 (wfl_operator, "Can't reference `this' or `super' before the "
8078 "superclass constructor has been called");
8079 TREE_TYPE (node) = error_mark_node;
8080 return error_mark_node;
8082 return current_this;
8085 CAN_COMPLETE_NORMALLY (node) = 1;
8086 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
8087 and it's time to turn it into the appropriate String object
8089 if ((node = patch_string (node)))
8091 fatal ("No case for tree code `%s' - java_complete_tree\n",
8092 tree_code_name [TREE_CODE (node)]);
8097 /* Complete function call's argument. Return a non zero value is an
8101 complete_function_arguments (node)
8107 ctxp->explicit_constructor_p += (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
8108 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
8110 tree wfl = TREE_VALUE (cn), parm, temp;
8111 parm = java_complete_tree (wfl);
8112 if (parm == error_mark_node)
8117 /* If have a string literal that we haven't transformed yet or a
8118 crafted string buffer, as a result of use of the the String
8119 `+' operator. Build `parm.toString()' and expand it. */
8120 if ((temp = patch_string (parm)))
8122 /* Inline PRIMTYPE.TYPE read access */
8123 parm = maybe_build_primttype_type_ref (parm, wfl);
8125 TREE_VALUE (cn) = parm;
8127 ctxp->explicit_constructor_p -= (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
8131 /* Sometimes (for loops and variable initialized during their
8132 declaration), we want to wrap a statement around a WFL and turn it
8136 build_debugable_stmt (location, stmt)
8140 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
8142 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
8143 EXPR_WFL_LINECOL (stmt) = location;
8145 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
8150 build_expr_block (body, decls)
8153 tree node = make_node (BLOCK);
8154 BLOCK_EXPR_DECLS (node) = decls;
8155 BLOCK_EXPR_BODY (node) = body;
8157 TREE_TYPE (node) = TREE_TYPE (body);
8158 TREE_SIDE_EFFECTS (node) = 1;
8162 /* Create a new function block and link it approriately to current
8163 function block chain */
8168 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
8171 /* Link block B supercontext to the previous block. The current
8172 function DECL is used as supercontext when enter_a_block is called
8173 for the first time for a given function. The current function body
8174 (DECL_FUNCTION_BODY) is set to be block B. */
8180 tree fndecl = current_function_decl;
8182 if (!DECL_FUNCTION_BODY (fndecl))
8184 BLOCK_SUPERCONTEXT (b) = fndecl;
8185 DECL_FUNCTION_BODY (fndecl) = b;
8189 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
8190 DECL_FUNCTION_BODY (fndecl) = b;
8195 /* Exit a block by changing the current function body
8196 (DECL_FUNCTION_BODY) to the current block super context, only if
8197 the block being exited isn't the method's top level one. */
8202 tree b = DECL_FUNCTION_BODY (current_function_decl);
8204 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
8205 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
8210 /* Lookup for NAME in the nested function's blocks, all the way up to
8211 the current toplevel one. It complies with Java's local variable
8215 lookup_name_in_blocks (name)
8218 tree b = DECL_FUNCTION_BODY (current_function_decl);
8220 while (b != current_function_decl)
8224 /* Paranoid sanity check. To be removed */
8225 if (TREE_CODE (b) != BLOCK)
8226 fatal ("non block expr function body - lookup_name_in_blocks");
8228 for (current = BLOCK_EXPR_DECLS (b); current;
8229 current = TREE_CHAIN (current))
8230 if (DECL_NAME (current) == name)
8232 b = BLOCK_SUPERCONTEXT (b);
8238 maybe_absorb_scoping_blocks ()
8240 while (BLOCK_EXPR_ORIGIN (DECL_FUNCTION_BODY (current_function_decl)))
8242 tree b = exit_block ();
8243 java_method_add_stmt (current_function_decl, b);
8244 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
8249 /* This section of the source is reserved to build_* functions that
8250 are building incomplete tree nodes and the patch_* functions that
8251 are completing them. */
8253 /* Build a super() constructor invocation. Returns empty_stmt_node if
8254 we're currently dealing with the class java.lang.Object. */
8257 build_super_invocation ()
8259 if (current_class == object_type_node)
8260 return empty_stmt_node;
8263 tree super_wfl = build_wfl_node (super_identifier_node,
8264 input_filename, 0, 0);
8265 return build_method_invocation (super_wfl, NULL_TREE);
8269 /* Build a SUPER/THIS qualified method invocation. */
8272 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
8280 build_wfl_node ((use_this ? this_identifier_node : super_identifier_node),
8281 input_filename, 0, 0);
8282 EXPR_WFL_LINECOL (wfl) = lloc;
8283 invok = build_method_invocation (name, args);
8284 return make_qualified_primary (wfl, invok, rloc);
8287 /* Build an incomplete CALL_EXPR node. */
8290 build_method_invocation (name, args)
8294 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
8295 TREE_SIDE_EFFECTS (call) = 1;
8296 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8300 /* Build an incomplete new xxx(...) node. */
8303 build_new_invocation (name, args)
8306 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
8307 TREE_SIDE_EFFECTS (call) = 1;
8308 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8312 /* Build an incomplete assignment expression. */
8315 build_assignment (op, op_location, lhs, rhs)
8316 int op, op_location;
8320 /* Build the corresponding binop if we deal with a Compound
8321 Assignment operator. Mark the binop sub-tree as part of a
8322 Compound Assignment expression */
8323 if (op != ASSIGN_TK)
8325 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
8326 COMPOUND_ASSIGN_P (rhs) = 1;
8328 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
8329 TREE_SIDE_EFFECTS (assignment) = 1;
8330 EXPR_WFL_LINECOL (assignment) = op_location;
8334 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
8337 print_int_node (node)
8340 static char buffer [80];
8341 if (TREE_CONSTANT_OVERFLOW (node))
8342 sprintf (buffer, "<overflow>");
8344 if (TREE_INT_CST_HIGH (node) == 0)
8345 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
8346 TREE_INT_CST_LOW (node));
8347 else if (TREE_INT_CST_HIGH (node) == -1
8348 && TREE_INT_CST_LOW (node) != 0)
8351 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
8352 -TREE_INT_CST_LOW (node));
8355 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
8356 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
8361 /* Return 1 if you an assignment of a FINAL is attempted */
8364 check_final_assignment (lvalue, wfl)
8367 if (JDECL_P (lvalue) && FIELD_FINAL (lvalue) &&
8368 DECL_NAME (current_function_decl) != clinit_identifier_node)
8371 (wfl, "Can't assign a value to the final variable `%s'",
8372 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
8378 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
8379 read. This is needed to avoid circularities in the implementation
8380 of these fields in libjava. */
8383 maybe_build_primttype_type_ref (rhs, wfl)
8386 tree to_return = NULL_TREE;
8387 tree rhs_type = TREE_TYPE (rhs);
8388 if (TREE_CODE (rhs) == COMPOUND_EXPR)
8390 tree n = TREE_OPERAND (rhs, 1);
8391 if (TREE_CODE (n) == VAR_DECL
8392 && DECL_NAME (n) == TYPE_identifier_node
8393 && rhs_type == class_ptr_type)
8395 char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
8396 if (!strncmp (self_name, "java.lang.", 10))
8397 to_return = build_primtype_type_ref (self_name);
8400 return (to_return ? to_return : rhs );
8403 /* 15.25 Assignment operators. */
8406 patch_assignment (node, wfl_op1, wfl_op2)
8411 tree rhs = TREE_OPERAND (node, 1);
8412 tree lvalue = TREE_OPERAND (node, 0), llvalue;
8413 tree lhs_type, rhs_type, new_rhs = NULL_TREE;
8414 int error_found = 0;
8415 int lvalue_from_array = 0;
8417 /* Can't assign to a final. */
8418 if (check_final_assignment (lvalue, wfl_op1))
8421 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8423 /* Lhs can be a named variable */
8424 if (JDECL_P (lvalue))
8426 INITIALIZED_P (lvalue) = 1;
8427 lhs_type = TREE_TYPE (lvalue);
8429 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
8430 comment on reason why */
8431 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
8433 lhs_type = TREE_TYPE (lvalue);
8434 lvalue_from_array = 1;
8436 /* Or a field access */
8437 else if (TREE_CODE (lvalue) == COMPONENT_REF)
8438 lhs_type = TREE_TYPE (lvalue);
8439 /* Or a function return slot */
8440 else if (TREE_CODE (lvalue) == RESULT_DECL)
8441 lhs_type = TREE_TYPE (lvalue);
8442 /* Otherwise, we might want to try to write into an optimized static
8443 final, this is an of a different nature, reported further on. */
8444 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
8445 && resolve_expression_name (wfl_op1, &llvalue)
8446 && check_final_assignment (llvalue, wfl_op1))
8449 /* What we should do instead is resetting the all the flags
8450 previously set, exchange lvalue for llvalue and continue. */
8451 return error_mark_node;
8455 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
8459 rhs_type = TREE_TYPE (rhs);
8460 /* 5.1 Try the assignment conversion for builtin type. */
8461 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
8463 /* 5.2 If it failed, try a reference conversion */
8464 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
8465 lhs_type = promote_type (rhs_type);
8467 /* 15.25.2 If we have a compound assignment, convert RHS into the
8469 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8470 new_rhs = convert (lhs_type, rhs);
8472 /* Explicit cast required. This is an error */
8475 char *t1 = strdup (lang_printable_name (TREE_TYPE (rhs), 0));
8476 char *t2 = strdup (lang_printable_name (lhs_type, 0));
8478 char operation [32]; /* Max size known */
8480 /* If the assignment is part of a declaration, we use the WFL of
8481 the declared variable to point out the error and call it a
8482 declaration problem. If the assignment is a genuine =
8483 operator, we call is a operator `=' problem, otherwise we
8484 call it an assignment problem. In both of these last cases,
8485 we use the WFL of the operator to indicate the error. */
8487 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
8490 strcpy (operation, "declaration");
8495 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8496 strcpy (operation, "assignment");
8497 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
8498 strcpy (operation, "`return'");
8500 strcpy (operation, "`='");
8504 (wfl, (!valid_cast_to_p (rhs_type, lhs_type) ?
8505 "Incompatible type for %s. Can't convert `%s' to `%s'" :
8506 "Incompatible type for %s. Explicit cast "
8507 "needed to convert `%s' to `%s'"), operation, t1, t2);
8508 free (t1); free (t2);
8512 /* Inline read access to java.lang.PRIMTYPE.TYPE */
8514 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
8517 return error_mark_node;
8519 /* If we built a compound expression as the result of a reference
8520 assignment into an array element, return it here. */
8521 if (TREE_CODE (node) == COMPOUND_EXPR)
8524 TREE_OPERAND (node, 0) = lvalue;
8525 TREE_OPERAND (node, 1) = new_rhs;
8526 TREE_TYPE (node) = lhs_type;
8530 /* Optimize static (final) field initialized upon declaration.
8531 - If the field is static final and is assigned to a primitive
8532 constant type, then set its DECL_INITIAL to the value.
8536 patch_initialized_static_field (node)
8539 tree field = TREE_OPERAND (node, 0);
8540 tree value = TREE_OPERAND (node, 1);
8542 if (DECL_INITIAL (field) != NULL_TREE)
8544 tree type = TREE_TYPE (value);
8545 if (FIELD_FINAL (field) && TREE_CONSTANT (value)
8546 && (JPRIMITIVE_TYPE_P (type)
8547 || (flag_emit_class_files
8548 && TREE_CODE (type) == POINTER_TYPE
8549 && TREE_TYPE (type) == string_type_node)))
8551 DECL_INITIAL (field) = value;
8552 return empty_stmt_node;
8554 DECL_INITIAL (field) = NULL_TREE;
8559 /* Check that type SOURCE can be cast into type DEST. If the cast
8560 can't occur at all, return 0 otherwise 1. This function is used to
8561 produce accurate error messages on the reasons why an assignment
8565 try_reference_assignconv (lhs_type, rhs)
8568 tree new_rhs = NULL_TREE;
8569 tree rhs_type = TREE_TYPE (rhs);
8571 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
8573 /* `null' may be assigned to any reference type */
8574 if (rhs == null_pointer_node)
8575 new_rhs = null_pointer_node;
8576 /* Try the reference assignment conversion */
8577 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
8579 /* This is a magic assignment that we process differently */
8580 else if (rhs == soft_exceptioninfo_call_node)
8586 /* Check that RHS can be converted into LHS_TYPE by the assignment
8587 conversion (5.2), for the cases of RHS being a builtin type. Return
8588 NULL_TREE if the conversion fails or if because RHS isn't of a
8589 builtin type. Return a converted RHS if the conversion is possible. */
8592 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
8593 tree wfl_op1, lhs_type, rhs;
8595 tree new_rhs = NULL_TREE;
8596 tree rhs_type = TREE_TYPE (rhs);
8598 /* Zero accepted everywhere */
8599 if (TREE_CODE (rhs) == INTEGER_CST
8600 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
8601 && JPRIMITIVE_TYPE_P (rhs_type))
8602 new_rhs = convert (lhs_type, rhs);
8604 /* 5.1.1 Try Identity Conversion,
8605 5.1.2 Try Widening Primitive Conversion */
8606 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
8607 new_rhs = convert (lhs_type, rhs);
8609 /* Try a narrowing primitive conversion (5.1.3):
8610 - expression is a constant expression of type int AND
8611 - variable is byte, short or char AND
8612 - The value of the expression is representable in the type of the
8614 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
8615 && (lhs_type == byte_type_node || lhs_type == char_type_node
8616 || lhs_type == short_type_node))
8618 if (int_fits_type_p (rhs, lhs_type))
8619 new_rhs = convert (lhs_type, rhs);
8620 else if (wfl_op1) /* Might be called with a NULL */
8621 parse_warning_context
8622 (wfl_op1, "Constant expression `%s' to wide for narrowing "
8623 "primitive conversion to `%s'",
8624 print_int_node (rhs), lang_printable_name (lhs_type, 0));
8625 /* Reported a warning that will turn into an error further
8626 down, so we don't return */
8632 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
8633 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
8634 0 is the conversion test fails. This implements parts the method
8635 invocation convertion (5.3). */
8638 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
8639 tree lhs_type, rhs_type;
8641 /* 5.1.1: This is the identity conversion part. */
8642 if (lhs_type == rhs_type)
8645 /* Reject non primitive types */
8646 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
8649 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
8650 than a char can't be converted into a char. Short can't too, but
8651 the < test below takes care of that */
8652 if (lhs_type == char_type_node && rhs_type == byte_type_node)
8655 /* Accept all promoted type here. Note, we can't use <= in the test
8656 below, because we still need to bounce out assignments of short
8657 to char and the likes */
8658 if (lhs_type == int_type_node
8659 && (rhs_type == promoted_byte_type_node
8660 || rhs_type == promoted_short_type_node
8661 || rhs_type == promoted_char_type_node
8662 || rhs_type == promoted_boolean_type_node))
8665 /* From here, an integral is widened if its precision is smaller
8666 than the precision of the LHS or if the LHS is a floating point
8667 type, or the RHS is a float and the RHS a double. */
8668 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
8669 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
8670 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
8671 || (rhs_type == float_type_node && lhs_type == double_type_node))
8677 /* Check that something of SOURCE type can be assigned or cast to
8678 something of DEST type at runtime. Return 1 if the operation is
8679 valid, 0 otherwise. If CAST is set to 1, we're treating the case
8680 were SOURCE is cast into DEST, which borrows a lot of the
8681 assignment check. */
8684 valid_ref_assignconv_cast_p (source, dest, cast)
8689 /* SOURCE or DEST might be null if not from a declared entity. */
8690 if (!source || !dest)
8692 if (JNULLP_TYPE_P (source))
8694 if (TREE_CODE (source) == POINTER_TYPE)
8695 source = TREE_TYPE (source);
8696 if (TREE_CODE (dest) == POINTER_TYPE)
8697 dest = TREE_TYPE (dest);
8698 /* Case where SOURCE is a class type */
8699 if (TYPE_CLASS_P (source))
8701 if (TYPE_CLASS_P (dest))
8702 return source == dest || inherits_from_p (source, dest)
8703 || (cast && inherits_from_p (dest, source));
8704 if (TYPE_INTERFACE_P (dest))
8706 /* If doing a cast and SOURCE is final, the operation is
8707 always correct a compile time (because even if SOURCE
8708 does not implement DEST, a subclass of SOURCE might). */
8709 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
8711 /* Otherwise, SOURCE must implement DEST */
8712 return interface_of_p (dest, source);
8714 /* DEST is an array, cast permited if SOURCE is of Object type */
8715 return (cast && source == object_type_node ? 1 : 0);
8717 if (TYPE_INTERFACE_P (source))
8719 if (TYPE_CLASS_P (dest))
8721 /* If not casting, DEST must be the Object type */
8723 return dest == object_type_node;
8724 /* We're doing a cast. The cast is always valid is class
8725 DEST is not final, otherwise, DEST must implement SOURCE */
8726 else if (!CLASS_FINAL (TYPE_NAME (dest)))
8729 return interface_of_p (source, dest);
8731 if (TYPE_INTERFACE_P (dest))
8733 /* If doing a cast, then if SOURCE and DEST contain method
8734 with the same signature but different return type, then
8735 this is a (compile time) error */
8738 tree method_source, method_dest;
8742 for (method_source = TYPE_METHODS (source); method_source;
8743 method_source = TREE_CHAIN (method_source))
8746 build_java_argument_signature (TREE_TYPE (method_source));
8747 source_type = TREE_TYPE (TREE_TYPE (method_source));
8748 source_name = DECL_NAME (method_source);
8749 for (method_dest = TYPE_METHODS (dest);
8750 method_dest; method_dest = TREE_CHAIN (method_dest))
8752 build_java_argument_signature (TREE_TYPE (method_dest))
8753 && source_name == DECL_NAME (method_dest)
8754 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
8760 return source == dest || interface_of_p (dest, source);
8765 if (TYPE_ARRAY_P (source))
8767 if (TYPE_CLASS_P (dest))
8768 return dest == object_type_node;
8769 /* Can't cast an array to an interface unless the interface is
8770 java.lang.Cloneable */
8771 if (TYPE_INTERFACE_P (dest))
8772 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
8775 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
8776 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
8778 /* In case of severe errors, they turn out null */
8779 if (!dest_element_type || !source_element_type)
8781 if (source_element_type == dest_element_type)
8783 return valid_ref_assignconv_cast_p (source_element_type,
8784 dest_element_type, cast);
8792 valid_cast_to_p (source, dest)
8796 if (TREE_CODE (source) == POINTER_TYPE)
8797 source = TREE_TYPE (source);
8798 if (TREE_CODE (dest) == POINTER_TYPE)
8799 dest = TREE_TYPE (dest);
8801 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
8802 return valid_ref_assignconv_cast_p (source, dest, 1);
8804 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
8810 /* Method invocation conversion test. Return 1 if type SOURCE can be
8811 converted to type DEST through the methond invocation conversion
8815 do_unary_numeric_promotion (arg)
8818 tree type = TREE_TYPE (arg);
8819 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
8820 : TREE_CODE (type) == CHAR_TYPE)
8821 arg = convert (int_type_node, arg);
8825 /* Return a non zero value if SOURCE can be converted into DEST using
8826 the method invocation conversion rule (5.3). */
8828 valid_method_invocation_conversion_p (dest, source)
8831 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
8832 && valid_builtin_assignconv_identity_widening_p (dest, source))
8833 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
8834 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
8835 && valid_ref_assignconv_cast_p (source, dest, 0)));
8838 /* Build an incomplete binop expression. */
8841 build_binop (op, op_location, op1, op2)
8846 tree binop = build (op, NULL_TREE, op1, op2);
8847 TREE_SIDE_EFFECTS (binop) = 1;
8848 /* Store the location of the operator, for better error report. The
8849 string of the operator will be rebuild based on the OP value. */
8850 EXPR_WFL_LINECOL (binop) = op_location;
8854 /* Build the string of the operator retained by NODE. If NODE is part
8855 of a compound expression, add an '=' at the end of the string. This
8856 function is called when an error needs to be reported on an
8857 operator. The string is returned as a pointer to a static character
8861 operator_string (node)
8864 #define BUILD_OPERATOR_STRING(S) \
8866 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
8870 static char buffer [10];
8871 switch (TREE_CODE (node))
8873 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
8874 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
8875 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
8876 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8877 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
8878 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
8879 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
8880 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
8881 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
8882 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
8883 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
8884 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
8885 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
8886 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
8887 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
8888 case GT_EXPR: BUILD_OPERATOR_STRING (">");
8889 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
8890 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
8891 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
8892 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8893 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
8894 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
8895 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
8896 case PREINCREMENT_EXPR: /* Fall through */
8897 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
8898 case PREDECREMENT_EXPR: /* Fall through */
8899 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
8901 fatal ("unregistered operator %s - operator_string",
8902 tree_code_name [TREE_CODE (node)]);
8905 #undef BUILD_OPERATOR_STRING
8908 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
8909 errors but we modify NODE so that it contains the type computed
8910 according to the expression, when it's fixed. Otherwise, we write
8911 error_mark_node as the type. It allows us to further the analysis
8912 of remaining nodes and detects more errors in certain cases. */
8915 patch_binop (node, wfl_op1, wfl_op2)
8920 tree op1 = TREE_OPERAND (node, 0);
8921 tree op2 = TREE_OPERAND (node, 1);
8922 tree op1_type = TREE_TYPE (op1);
8923 tree op2_type = TREE_TYPE (op2);
8925 int code = TREE_CODE (node);
8927 /* If 1, tell the routine that we have to return error_mark_node
8928 after checking for the initialization of the RHS */
8929 int error_found = 0;
8931 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8935 /* 15.16 Multiplicative operators */
8936 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
8937 case RDIV_EXPR: /* 15.16.2 Division Operator / */
8938 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
8939 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
8941 if (!JPRIMITIVE_TYPE_P (op1_type))
8942 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
8943 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
8944 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
8945 TREE_TYPE (node) = error_mark_node;
8949 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8950 /* Change the division operator if necessary */
8951 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
8952 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
8954 /* This one is more complicated. FLOATs are processed by a
8955 function call to soft_fmod. Duplicate the value of the
8956 COMPOUND_ASSIGN_P flag. */
8957 if (code == TRUNC_MOD_EXPR)
8959 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
8960 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
8965 /* 15.17 Additive Operators */
8966 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
8968 /* Operation is valid if either one argument is a string
8969 constant, a String object or a StringBuffer crafted for the
8970 purpose of the a previous usage of the String concatenation
8973 if (TREE_CODE (op1) == STRING_CST
8974 || TREE_CODE (op2) == STRING_CST
8975 || JSTRING_TYPE_P (op1_type)
8976 || JSTRING_TYPE_P (op2_type)
8977 || IS_CRAFTED_STRING_BUFFER_P (op1)
8978 || IS_CRAFTED_STRING_BUFFER_P (op2))
8979 return build_string_concatenation (op1, op2);
8981 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
8983 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
8985 if (!JPRIMITIVE_TYPE_P (op1_type))
8986 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
8987 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
8988 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
8989 TREE_TYPE (node) = error_mark_node;
8993 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8996 /* 15.18 Shift Operators */
9000 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
9002 if (!JINTEGRAL_TYPE_P (op1_type))
9003 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
9006 (wfl_operator, (JPRIMITIVE_TYPE_P (op2_type) ?
9007 "Incompatible type for `%s'. Explicit cast needed to convert "
9008 "shift distance from `%s' to integral" :
9009 "Incompatible type for `%s'. Can't convert shift distance from "
9010 "`%s' to integral"),
9011 operator_string (node), lang_printable_name (op2_type, 0));
9012 TREE_TYPE (node) = error_mark_node;
9017 /* Unary numeric promotion (5.6.1) is performed on each operand
9019 op1 = do_unary_numeric_promotion (op1);
9020 op2 = do_unary_numeric_promotion (op2);
9022 /* The type of the shift expression is the type of the promoted
9023 type of the left-hand operand */
9024 prom_type = TREE_TYPE (op1);
9026 /* Shift int only up to 0x1f and long up to 0x3f */
9027 if (prom_type == int_type_node)
9028 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
9029 build_int_2 (0x1f, 0)));
9031 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
9032 build_int_2 (0x3f, 0)));
9034 /* The >>> operator is a >> operating on unsigned quantities */
9035 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
9038 tree utype = unsigned_type (prom_type);
9039 op1 = convert (utype, op1);
9040 TREE_SET_CODE (node, RSHIFT_EXPR);
9041 TREE_OPERAND (node, 0) = op1;
9042 TREE_OPERAND (node, 1) = op2;
9043 TREE_TYPE (node) = utype;
9044 to_return = convert (prom_type, node);
9045 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
9046 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
9051 /* 15.19.1 Type Comparison Operator instaceof */
9052 case INSTANCEOF_EXPR:
9054 TREE_TYPE (node) = boolean_type_node;
9056 if (!(op2_type = resolve_type_during_patch (op2)))
9057 return error_mark_node;
9059 /* The first operand must be a reference type or the null type */
9060 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
9061 error_found = 1; /* Error reported further below */
9063 /* The second operand must be a reference type */
9064 if (!JREFERENCE_TYPE_P (op2_type))
9066 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
9068 (wfl_operator, "Invalid argument `%s' for `instanceof'",
9069 lang_printable_name (op2_type, 0));
9073 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
9075 /* If the first operand is null, the result is always false */
9076 if (op1 == null_pointer_node)
9077 return boolean_false_node;
9078 else if (flag_emit_class_files)
9080 TREE_OPERAND (node, 1) = op2_type;
9083 /* Otherwise we have to invoke instance of to figure it out */
9087 build (CALL_EXPR, boolean_type_node,
9088 build_address_of (soft_instanceof_node),
9091 build_tree_list (NULL_TREE,
9092 build_class_ref (op2_type))),
9094 TREE_SIDE_EFFECTS (call) = 1;
9098 /* There is no way the expression operand can be an instance of
9099 the type operand. This is a compile time error. */
9102 char *t1 = strdup (lang_printable_name (op1_type, 0));
9103 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
9105 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
9106 t1, lang_printable_name (op2_type, 0));
9113 /* 15.21 Bitwise and Logical Operators */
9117 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
9118 /* Binary numeric promotion is performed on both operand and the
9119 expression retain that type */
9120 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9122 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
9123 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
9124 /* The type of the bitwise operator expression is BOOLEAN */
9125 prom_type = boolean_type_node;
9128 if (!JINTEGRAL_TYPE_P (op1_type))
9129 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
9130 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
9131 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
9132 TREE_TYPE (node) = error_mark_node;
9134 /* Insert a break here if adding thing before the switch's
9135 break for this case */
9139 /* 15.22 Conditional-And Operator */
9140 case TRUTH_ANDIF_EXPR:
9141 /* 15.23 Conditional-Or Operator */
9142 case TRUTH_ORIF_EXPR:
9143 /* Operands must be of BOOLEAN type */
9144 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
9145 TREE_CODE (op2_type) != BOOLEAN_TYPE)
9147 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
9148 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
9149 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
9150 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
9151 TREE_TYPE (node) = boolean_type_node;
9155 /* The type of the conditional operators is BOOLEAN */
9156 prom_type = boolean_type_node;
9159 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
9164 /* The type of each of the operands must be a primitive numeric
9166 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
9168 if (!JNUMERIC_TYPE_P (op1_type))
9169 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
9170 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
9171 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
9172 TREE_TYPE (node) = boolean_type_node;
9176 /* Binary numeric promotion is performed on the operands */
9177 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9178 /* The type of the relation expression is always BOOLEAN */
9179 prom_type = boolean_type_node;
9182 /* 15.20 Equality Operator */
9185 /* 15.20.1 Numerical Equality Operators == and != */
9186 /* Binary numeric promotion is performed on the operands */
9187 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
9188 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9190 /* 15.20.2 Boolean Equality Operators == and != */
9191 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
9192 TREE_CODE (op2_type) == BOOLEAN_TYPE)
9193 ; /* Nothing to do here */
9195 /* 15.20.3 Reference Equality Operators == and != */
9196 /* Types have to be either references or the null type. If
9197 they're references, it must be possible to convert either
9198 type to the other by casting conversion. */
9199 else if (op1 == null_pointer_node || op2 == null_pointer_node
9200 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
9201 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
9202 || valid_ref_assignconv_cast_p (op2_type,
9204 ; /* Nothing to do here */
9206 /* Else we have an error figure what can't be converted into
9207 what and report the error */
9211 t1 = strdup (lang_printable_name (op1_type, 0));
9213 (wfl_operator, "Incompatible type for `%s'. Can't convert `%s' "
9214 "to `%s'", operator_string (node), t1,
9215 lang_printable_name (op2_type, 0));
9217 TREE_TYPE (node) = boolean_type_node;
9221 prom_type = boolean_type_node;
9226 return error_mark_node;
9228 TREE_OPERAND (node, 0) = op1;
9229 TREE_OPERAND (node, 1) = op2;
9230 TREE_TYPE (node) = prom_type;
9234 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
9235 zero value, the value of CSTE comes after the valude of STRING */
9238 do_merge_string_cste (cste, string, string_len, after)
9241 int string_len, after;
9243 int len = TREE_STRING_LENGTH (cste) + string_len;
9244 char *old = TREE_STRING_POINTER (cste);
9245 TREE_STRING_LENGTH (cste) = len;
9246 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
9249 strcpy (TREE_STRING_POINTER (cste), string);
9250 strcat (TREE_STRING_POINTER (cste), old);
9254 strcpy (TREE_STRING_POINTER (cste), old);
9255 strcat (TREE_STRING_POINTER (cste), string);
9260 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
9261 new STRING_CST on success, NULL_TREE on failure */
9264 merge_string_cste (op1, op2, after)
9268 /* Handle two string constants right away */
9269 if (TREE_CODE (op2) == STRING_CST)
9270 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
9271 TREE_STRING_LENGTH (op2), after);
9273 /* Reasonable integer constant can be treated right away */
9274 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
9276 static char *boolean_true = "true";
9277 static char *boolean_false = "false";
9278 static char *null_pointer = "null";
9282 if (op2 == boolean_true_node)
9283 string = boolean_true;
9284 else if (op2 == boolean_false_node)
9285 string = boolean_false;
9286 else if (op2 == null_pointer_node)
9287 string = null_pointer;
9288 else if (TREE_TYPE (op2) == char_type_node)
9290 ch[0] = (char )TREE_INT_CST_LOW (op2);
9295 string = print_int_node (op2);
9297 return do_merge_string_cste (op1, string, strlen (string), after);
9302 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
9303 has to be a STRING_CST and the other part must be a STRING_CST or a
9304 INTEGRAL constant. Return a new STRING_CST if the operation
9305 succeed, NULL_TREE otherwise.
9307 If the case we want to optimize for space, we might want to return
9308 NULL_TREE for each invocation of this routine. FIXME */
9311 string_constant_concatenation (op1, op2)
9314 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
9319 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
9320 rest = (string == op1 ? op2 : op1);
9321 invert = (string == op1 ? 0 : 1 );
9323 /* Walk REST, only if it looks reasonable */
9324 if (TREE_CODE (rest) != STRING_CST
9325 && !IS_CRAFTED_STRING_BUFFER_P (rest)
9326 && !JSTRING_TYPE_P (TREE_TYPE (rest))
9327 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
9329 rest = java_complete_tree (rest);
9330 if (rest == error_mark_node)
9331 return error_mark_node;
9334 return merge_string_cste (string, rest, invert);
9339 /* Implement the `+' operator. Does static optimization if possible,
9340 otherwise create (if necessary) and append elements to a
9341 StringBuffer. The StringBuffer will be carried around until it is
9342 used for a function call or an assignment. Then toString() will be
9343 called on it to turn it into a String object. */
9346 build_string_concatenation (op1, op2)
9351 /* Try to do some static optimization */
9352 if ((result = string_constant_concatenation (op1, op2)))
9355 /* Discard null constants on either sides of the expression */
9356 if (TREE_CODE (op1) == STRING_CST && !TREE_STRING_LENGTH (op1))
9361 else if (TREE_CODE (op2) == STRING_CST && !TREE_STRING_LENGTH (op2))
9364 /* If operands are string constant, turn then into object references */
9365 if (TREE_CODE (op1) == STRING_CST)
9366 op1 = patch_string_cst (op1);
9367 if (op2 && TREE_CODE (op2) == STRING_CST)
9368 op2 = patch_string_cst (op2);
9370 /* If either one of the constant is null and the other non null
9371 operand is a String object, return it. */
9372 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
9375 /* If OP1 isn't already a StringBuffer, create and
9376 initialize a new one */
9377 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
9379 /* Two solutions here:
9380 1) OP1 is a string reference, we call new StringBuffer(OP1)
9381 2) OP1 is something else, we call new StringBuffer().append(OP1). */
9382 if (JSTRING_TYPE_P (TREE_TYPE (op1)))
9383 op1 = BUILD_STRING_BUFFER (op1);
9386 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
9387 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
9393 /* OP1 is no longer the last node holding a crafted StringBuffer */
9394 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
9395 /* Create a node for `{new...,xxx}.append (op2)' */
9397 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
9400 /* Mark the last node holding a crafted StringBuffer */
9401 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
9406 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
9407 StringBuffer. If no string were found to be patched, return
9414 if (node == error_mark_node)
9415 return error_mark_node;
9416 if (TREE_CODE (node) == STRING_CST)
9417 return patch_string_cst (node);
9418 else if (IS_CRAFTED_STRING_BUFFER_P (node))
9420 int saved = ctxp->explicit_constructor_p;
9421 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
9423 /* Temporary disable forbid the use of `this'. */
9424 ctxp->explicit_constructor_p = 0;
9425 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
9426 /* Restore it at its previous value */
9427 ctxp->explicit_constructor_p = saved;
9433 /* Build the internal representation of a string constant. */
9436 patch_string_cst (node)
9440 if (! flag_emit_class_files)
9442 push_obstacks (&permanent_obstack, &permanent_obstack);
9443 node = get_identifier (TREE_STRING_POINTER (node));
9444 location = alloc_name_constant (CONSTANT_String, node);
9445 node = build_ref_from_constant_pool (location);
9447 TREE_TYPE (node) = promote_type (string_type_node);
9448 TREE_CONSTANT (node) = 1;
9452 /* Build an incomplete unary operator expression. */
9455 build_unaryop (op_token, op_location, op1)
9456 int op_token, op_location;
9463 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
9464 case MINUS_TK: op = NEGATE_EXPR; break;
9465 case NEG_TK: op = TRUTH_NOT_EXPR; break;
9466 case NOT_TK: op = BIT_NOT_EXPR; break;
9467 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
9471 unaryop = build1 (op, NULL_TREE, op1);
9472 TREE_SIDE_EFFECTS (unaryop) = 1;
9473 /* Store the location of the operator, for better error report. The
9474 string of the operator will be rebuild based on the OP value. */
9475 EXPR_WFL_LINECOL (unaryop) = op_location;
9479 /* Special case for the ++/-- operators, since they require an extra
9480 argument to build, which is set to NULL and patched
9481 later. IS_POST_P is 1 if the operator, 0 otherwise. */
9484 build_incdec (op_token, op_location, op1, is_post_p)
9485 int op_token, op_location;
9489 static enum tree_code lookup [2][2] =
9491 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
9492 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
9494 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
9495 NULL_TREE, op1, NULL_TREE);
9496 TREE_SIDE_EFFECTS (node) = 1;
9497 /* Store the location of the operator, for better error report. The
9498 string of the operator will be rebuild based on the OP value. */
9499 EXPR_WFL_LINECOL (node) = op_location;
9503 /* Build an incomplete cast operator, based on the use of the
9504 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
9505 set. java_complete_tree is trained to walk a CONVERT_EXPR even
9506 though its type is already set. */
9509 build_cast (location, type, exp)
9513 tree node = build1 (CONVERT_EXPR, type, exp);
9514 EXPR_WFL_LINECOL (node) = location;
9518 /* 15.14 Unary operators. We return error_mark_node in case of error,
9519 but preserve the type of NODE if the type is fixed. */
9522 patch_unaryop (node, wfl_op)
9526 tree op = TREE_OPERAND (node, 0);
9527 tree op_type = TREE_TYPE (op);
9528 tree prom_type, value, decl;
9529 int code = TREE_CODE (node);
9530 int error_found = 0;
9532 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9536 /* 15.13.2 Postfix Increment Operator ++ */
9537 case POSTINCREMENT_EXPR:
9538 /* 15.13.3 Postfix Increment Operator -- */
9539 case POSTDECREMENT_EXPR:
9540 /* 15.14.1 Prefix Increment Operator ++ */
9541 case PREINCREMENT_EXPR:
9542 /* 15.14.2 Prefix Decrement Operator -- */
9543 case PREDECREMENT_EXPR:
9544 decl = strip_out_static_field_access_decl (op);
9546 && !((TREE_CODE (decl) == INDIRECT_REF
9547 || TREE_CODE (decl) == COMPONENT_REF)
9548 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))))
9551 /* Before screaming, check that we're not in fact trying to
9552 increment a optimized static final access, in which case
9553 we issue an different error message. */
9554 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
9555 && resolve_expression_name (wfl_op, &lvalue)
9556 && check_final_assignment (lvalue, wfl_op)))
9557 parse_error_context (wfl_operator, "Invalid argument to `%s'",
9558 operator_string (node));
9559 TREE_TYPE (node) = error_mark_node;
9562 else if (check_final_assignment (op, wfl_op))
9565 /* From now on, we know that op if a variable and that it has a
9566 valid wfl. We use wfl_op to locate errors related to the
9568 else if (!JNUMERIC_TYPE_P (op_type))
9571 (wfl_op, "Invalid argument type `%s' to `%s'",
9572 lang_printable_name (op_type, 0), operator_string (node));
9573 TREE_TYPE (node) = error_mark_node;
9578 /* Before the addition, binary numeric promotion is performed on
9580 value = build_int_2 (1, 0);
9582 binary_numeric_promotion (op_type, TREE_TYPE (value), &op, &value);
9583 /* And write the promoted incremented and increment */
9584 TREE_OPERAND (node, 0) = op;
9585 TREE_OPERAND (node, 1) = value;
9586 /* Convert the overall back into its original type. */
9587 return fold (convert (op_type, node));
9591 /* 15.14.3 Unary Plus Operator + */
9592 case UNARY_PLUS_EXPR:
9593 /* 15.14.4 Unary Minus Operator - */
9595 if (!JNUMERIC_TYPE_P (op_type))
9597 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
9598 TREE_TYPE (node) = error_mark_node;
9601 /* Unary numeric promotion is performed on operand */
9604 op = do_unary_numeric_promotion (op);
9605 prom_type = TREE_TYPE (op);
9606 if (code == UNARY_PLUS_EXPR)
9611 /* 15.14.5 Bitwise Complement Operator ~ */
9613 if (!JINTEGRAL_TYPE_P (op_type))
9615 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
9616 TREE_TYPE (node) = error_mark_node;
9621 op = do_unary_numeric_promotion (op);
9622 prom_type = TREE_TYPE (op);
9626 /* 15.14.6 Logical Complement Operator ! */
9627 case TRUTH_NOT_EXPR:
9628 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
9630 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
9631 /* But the type is known. We will report an error if further
9632 attempt of a assignment is made with this rhs */
9633 TREE_TYPE (node) = boolean_type_node;
9637 prom_type = boolean_type_node;
9640 /* 15.15 Cast Expression */
9642 value = patch_cast (node, wfl_operator);
9643 if (value == error_mark_node)
9645 /* If this cast is part of an assignment, we tell the code
9646 that deals with it not to complain about a mismatch,
9647 because things have been cast, anyways */
9648 TREE_TYPE (node) = error_mark_node;
9652 return fold (value);
9657 return error_mark_node;
9659 /* There are cases where node has been replaced by something else
9660 and we don't end up returning here: UNARY_PLUS_EXPR,
9661 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
9662 TREE_OPERAND (node, 0) = fold (op);
9663 TREE_TYPE (node) = prom_type;
9667 /* Generic type resolution that sometimes takes place during node
9668 patching. Returned the resolved type or generate an error
9669 message. Return the resolved type or NULL_TREE. */
9672 resolve_type_during_patch (type)
9675 if (unresolved_type_p (type, NULL))
9677 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
9680 parse_error_context (type,
9681 "Class `%s' not found in type declaration",
9682 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
9687 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
9688 return TREE_TYPE (type_decl);
9693 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
9694 found. Otherwise NODE or something meant to replace it is returned. */
9697 patch_cast (node, wfl_operator)
9701 tree op = TREE_OPERAND (node, 0);
9702 tree op_type = TREE_TYPE (op);
9703 tree cast_type = TREE_TYPE (node);
9706 /* First resolve OP_TYPE if unresolved */
9707 if (!(cast_type = resolve_type_during_patch (cast_type)))
9708 return error_mark_node;
9710 /* Check on cast that are proven correct at compile time */
9711 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
9713 static tree convert_narrow ();
9715 if (cast_type == op_type)
9718 /* float and double type are converted to the original type main
9719 variant and then to the target type. */
9720 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
9721 op = convert (integer_type_node, op);
9723 /* Try widening/narowwing convertion. Potentially, things need
9724 to be worked out in gcc so we implement the extreme cases
9725 correctly. fold_convert() needs to be fixed. */
9726 return convert (cast_type, op);
9729 /* It's also valid to cast a boolean into a boolean */
9730 if (op_type == boolean_type_node && cast_type == boolean_type_node)
9733 /* null can be casted to references */
9734 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
9735 return build_null_of_type (cast_type);
9737 /* The remaining legal casts involve conversion between reference
9738 types. Check for their compile time correctness. */
9739 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
9740 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
9742 TREE_TYPE (node) = promote_type (cast_type);
9743 /* Now, the case can be determined correct at compile time if
9744 OP_TYPE can be converted into CAST_TYPE by assignment
9747 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
9749 TREE_SET_CODE (node, NOP_EXPR);
9753 if (flag_emit_class_files)
9755 TREE_SET_CODE (node, CONVERT_EXPR);
9759 /* The cast requires a run-time check */
9760 return build (CALL_EXPR, promote_type (cast_type),
9761 build_address_of (soft_checkcast_node),
9762 tree_cons (NULL_TREE, build_class_ref (cast_type),
9763 build_tree_list (NULL_TREE, op)),
9767 /* Any other casts are proven incorrect at compile time */
9768 t1 = strdup (lang_printable_name (op_type, 0));
9769 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
9770 t1, lang_printable_name (cast_type, 0));
9772 return error_mark_node;
9775 /* Build a null constant and give it the type TYPE. */
9778 build_null_of_type (type)
9781 tree node = build_int_2 (0, 0);
9782 TREE_TYPE (node) = promote_type (type);
9786 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
9787 a list of indices. */
9789 build_array_ref (location, array, index)
9793 tree node = build (ARRAY_REF, NULL_TREE, array, index);
9794 EXPR_WFL_LINECOL (node) = location;
9798 /* 15.12 Array Access Expression */
9801 patch_array_ref (node)
9804 tree array = TREE_OPERAND (node, 0);
9805 tree array_type = TREE_TYPE (array);
9806 tree index = TREE_OPERAND (node, 1);
9807 tree index_type = TREE_TYPE (index);
9808 int error_found = 0;
9810 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9812 if (TREE_CODE (array_type) == POINTER_TYPE)
9813 array_type = TREE_TYPE (array_type);
9815 /* The array reference must be an array */
9816 if (!TYPE_ARRAY_P (array_type))
9819 (wfl_operator, "`[]' can only be applied to arrays. It can't be "
9820 "applied to `%s'", lang_printable_name (array_type, 0));
9821 TREE_TYPE (node) = error_mark_node;
9825 /* The array index underdoes unary numeric promotion. The promoted
9827 index = do_unary_numeric_promotion (index);
9828 if (TREE_TYPE (index) != int_type_node)
9830 int could_cast = valid_cast_to_p (index_type, int_type_node);
9833 (could_cast ? "Incompatible type for `[]'. Explicit cast needed to "
9834 "convert `%s' to `int'" : "Incompatible type for `[]'. "
9835 "Can't convert `%s' to `int'"),
9836 lang_printable_name (index_type, 0));
9837 TREE_TYPE (node) = error_mark_node;
9842 return error_mark_node;
9844 array_type = TYPE_ARRAY_ELEMENT (array_type);
9846 if (flag_emit_class_files)
9848 TREE_OPERAND (node, 0) = array;
9849 TREE_OPERAND (node, 1) = index;
9852 node = build_java_arrayaccess (array, array_type, index);
9853 TREE_TYPE (node) = array_type;
9857 /* 15.9 Array Creation Expressions */
9860 build_newarray_node (type, dims, extra_dims)
9866 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
9867 build_int_2 (extra_dims, 0));
9872 patch_newarray (node)
9875 tree type = TREE_OPERAND (node, 0);
9876 tree dims = TREE_OPERAND (node, 1);
9877 tree cdim, array_type;
9878 int error_found = 0;
9880 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
9882 /* Dimension types are verified. It's better for the types to be
9883 verified in order. */
9884 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
9887 tree dim = TREE_VALUE (cdim);
9889 /* Dim might have been saved during its evaluation */
9890 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
9892 /* The type of each specified dimension must be an integral type. */
9893 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
9896 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
9897 promoted type must be int. */
9900 dim = do_unary_numeric_promotion (dim);
9901 if (TREE_TYPE (dim) != int_type_node)
9905 /* Report errors on types here */
9909 (TREE_PURPOSE (cdim),
9910 "Incompatible type for dimension in array creation expression. "
9911 "%s convert `%s' to `int'",
9912 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
9913 "Explicit cast needed to" : "Can't"),
9914 lang_printable_name (TREE_TYPE (dim), 0));
9918 TREE_PURPOSE (cdim) = NULL_TREE;
9921 /* Resolve array base type if unresolved */
9922 if (!(type = resolve_type_during_patch (type)))
9927 /* We don't want further evaluation of this bogus array creation
9929 TREE_TYPE (node) = error_mark_node;
9930 return error_mark_node;
9933 /* Set array_type to the actual (promoted) array type of the result. */
9934 if (TREE_CODE (type) == RECORD_TYPE)
9935 type = build_pointer_type (type);
9936 while (--xdims >= 0)
9938 type = promote_type (build_java_array_type (type, -1));
9940 dims = nreverse (dims);
9942 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
9945 array_type = build_java_array_type (type,
9946 TREE_CODE (cdim) == INTEGER_CST ?
9947 TREE_INT_CST_LOW (cdim) : -1);
9948 array_type = promote_type (array_type);
9950 dims = nreverse (dims);
9952 /* The node is transformed into a function call. Things are done
9953 differently according to the number of dimensions. If the number
9954 of dimension is equal to 1, then the nature of the base type
9955 (primitive or not) matters. */
9957 return build_new_array (type, TREE_VALUE (dims));
9959 /* Can't reuse what's already written in expr.c because it uses the
9960 JVM stack representation. Provide a build_multianewarray. FIXME */
9961 return build (CALL_EXPR, array_type,
9962 build_address_of (soft_multianewarray_node),
9963 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
9964 tree_cons (NULL_TREE,
9965 build_int_2 (ndims, 0), dims )),
9969 /* 10.6 Array initializer. */
9971 /* Build a wfl for array element that don't have one, so we can
9972 pin-point errors. */
9975 maybe_build_array_element_wfl (node)
9978 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
9979 return build_expr_wfl (NULL_TREE, ctxp->filename,
9980 ctxp->elc.line, ctxp->elc.prev_col);
9985 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
9986 identification of initialized arrays easier to detect during walk
9990 build_new_array_init (location, values)
9994 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
9995 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
9996 EXPR_WFL_LINECOL (to_return) = location;
10000 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
10001 occurred. Otherwise return NODE after having set its type
10005 patch_new_array_init (type, node)
10008 int error_seen = 0;
10009 tree current, element_type;
10010 HOST_WIDE_INT length;
10011 int all_constant = 1;
10012 tree init = TREE_OPERAND (node, 0);
10014 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
10016 parse_error_context (node,
10017 "Invalid array initializer for non-array type `%s'",
10018 lang_printable_name (type, 1));
10019 return error_mark_node;
10021 type = TREE_TYPE (type);
10022 element_type = TYPE_ARRAY_ELEMENT (type);
10024 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
10026 for (length = 0, current = CONSTRUCTOR_ELTS (init);
10027 current; length++, current = TREE_CHAIN (current))
10029 tree elt = TREE_VALUE (current);
10030 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
10032 error_seen |= array_constructor_check_entry (element_type, current);
10033 elt = TREE_VALUE (current);
10034 /* When compiling to native code, STRING_CST is converted to
10035 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
10036 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
10041 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
10042 TREE_PURPOSE (current) = NULL_TREE;
10045 if (elt && TREE_VALUE (elt) == error_mark_node)
10050 return error_mark_node;
10052 /* Create a new type. We can't reuse the one we have here by
10053 patching its dimension because it originally is of dimension -1
10054 hence reused by gcc. This would prevent triangular arrays. */
10055 type = build_java_array_type (element_type, length);
10056 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
10057 TREE_TYPE (node) = promote_type (type);
10058 TREE_CONSTANT (init) = all_constant;
10059 TREE_CONSTANT (node) = all_constant;
10063 /* Verify that one entry of the initializer element list can be
10064 assigned to the array base type. Report 1 if an error occurred, 0
10068 array_constructor_check_entry (type, entry)
10071 char *array_type_string = NULL; /* For error reports */
10072 tree value, type_value, new_value, wfl_value, patched;
10073 int error_seen = 0;
10075 new_value = NULL_TREE;
10076 wfl_value = TREE_VALUE (entry);
10078 value = java_complete_tree (TREE_VALUE (entry));
10079 /* patch_string return error_mark_node if arg is error_mark_node */
10080 if ((patched = patch_string (value)))
10082 if (value == error_mark_node)
10085 type_value = TREE_TYPE (value);
10087 /* At anytime, try_builtin_assignconv can report a warning on
10088 constant overflow during narrowing. */
10089 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
10090 new_value = try_builtin_assignconv (wfl_operator, type, value);
10091 if (!new_value && (new_value = try_reference_assignconv (type, value)))
10092 type_value = promote_type (type);
10094 /* Check and report errors */
10097 char *msg = (!valid_cast_to_p (type_value, type) ?
10098 "Can't" : "Explicit cast needed to");
10099 if (!array_type_string)
10100 array_type_string = strdup (lang_printable_name (type, 1));
10101 parse_error_context
10102 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
10103 msg, lang_printable_name (type_value, 1), array_type_string);
10109 new_value = maybe_build_primttype_type_ref (new_value, wfl_operator);
10110 TREE_VALUE (entry) = new_value;
10113 if (array_type_string)
10114 free (array_type_string);
10116 TREE_PURPOSE (entry) = NULL_TREE;
10121 build_this (location)
10124 tree node = build_wfl_node (this_identifier_node, input_filename, 0, 0);
10125 TREE_SET_CODE (node, THIS_EXPR);
10126 EXPR_WFL_LINECOL (node) = location;
10130 /* 14.15 The return statement. It builds a modify expression that
10131 assigns the returned value to the RESULT_DECL that hold the value
10135 build_return (location, op)
10139 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
10140 EXPR_WFL_LINECOL (node) = location;
10141 node = build_debugable_stmt (location, node);
10146 patch_return (node)
10149 tree return_exp = TREE_OPERAND (node, 0);
10150 tree meth = current_function_decl;
10151 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
10152 int error_found = 0;
10154 TREE_TYPE (node) = error_mark_node;
10155 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10157 /* It's invalid to have a return value within a function that is
10158 declared with the keyword void or that is a constructor */
10159 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
10162 /* It's invalid to have a no return value within a function that
10163 isn't declared with the keyword `void' */
10164 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
10169 if (!DECL_CONSTRUCTOR_P (meth))
10171 char *t = strdup (lang_printable_name (mtype, 0));
10172 parse_error_context (wfl_operator,
10173 "`return' with%s value from `%s %s'",
10174 (error_found == 1 ? "" : "out"),
10175 t, lang_printable_name (meth, 0));
10179 parse_error_context (wfl_operator,
10180 "`return' with value from constructor `%s'",
10181 lang_printable_name (meth, 0));
10182 return error_mark_node;
10185 /* If we have a return_exp, build a modify expression and expand
10186 it. Note: at that point, the assignment is declared valid, but we
10187 may want to carry some more hacks */
10190 tree exp = java_complete_tree (return_exp);
10191 tree modify, patched;
10193 /* If the function returned value and EXP are booleans, EXP has
10194 to be converted into the type of DECL_RESULT, which is integer
10195 (see complete_start_java_method) */
10196 if (TREE_TYPE (exp) == boolean_type_node &&
10197 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
10198 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
10200 /* `null' can be assigned to a function returning a reference */
10201 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
10202 exp == null_pointer_node)
10203 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
10205 if ((patched = patch_string (exp)))
10208 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
10209 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
10210 modify = java_complete_tree (modify);
10212 if (modify != error_mark_node)
10214 TREE_SIDE_EFFECTS (modify) = 1;
10215 TREE_OPERAND (node, 0) = modify;
10218 return error_mark_node;
10220 TREE_TYPE (node) = void_type_node;
10221 TREE_SIDE_EFFECTS (node) = 1;
10225 /* 14.8 The if Statement */
10228 build_if_else_statement (location, expression, if_body, else_body)
10230 tree expression, if_body, else_body;
10234 else_body = empty_stmt_node;
10235 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
10236 EXPR_WFL_LINECOL (node) = location;
10237 node = build_debugable_stmt (location, node);
10242 patch_if_else_statement (node)
10245 tree expression = TREE_OPERAND (node, 0);
10247 TREE_TYPE (node) = error_mark_node;
10248 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10250 /* The type of expression must be boolean */
10251 if (TREE_TYPE (expression) != boolean_type_node
10252 && TREE_TYPE (expression) != promoted_boolean_type_node)
10254 parse_error_context
10256 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
10257 lang_printable_name (TREE_TYPE (expression), 0));
10258 return error_mark_node;
10261 TREE_TYPE (node) = void_type_node;
10262 TREE_SIDE_EFFECTS (node) = 1;
10263 CAN_COMPLETE_NORMALLY (node)
10264 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10265 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
10269 /* 14.6 Labeled Statements */
10271 /* Action taken when a lableled statement is parsed. a new
10272 LABELED_BLOCK_EXPR is created. No statement is attached to the
10276 build_labeled_block (location, label)
10280 tree label_name = merge_qualified_name (label_id, label);
10281 tree label_decl, node;
10283 /* Issue an error if we try to reuse a label that was previously
10285 if (IDENTIFIER_LOCAL_VALUE (label_name))
10287 EXPR_WFL_LINECOL (wfl_operator) = location;
10288 parse_error_context (wfl_operator, "Declaration of `%s' shadows "
10289 "a previous label declaration",
10290 IDENTIFIER_POINTER (label));
10291 EXPR_WFL_LINECOL (wfl_operator) =
10292 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
10293 parse_error_context (wfl_operator, "This is the location of the "
10294 "previous declaration of label `%s'",
10295 IDENTIFIER_POINTER (label));
10296 java_error_count--;
10299 label_decl = create_label_decl (label_name);
10300 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
10301 EXPR_WFL_LINECOL (node) = location;
10302 TREE_SIDE_EFFECTS (node) = 1;
10306 /* Generate a label crafting a unique name for it. This is used to
10307 implicitely label loops that aren't the body part of labeled
10311 generate_labeled_block ()
10313 return build_labeled_block (0, generate_name ());
10316 /* A labeled statement LBE is attached a statement. */
10319 complete_labeled_statement (lbe, statement)
10320 tree lbe; /* Labeled block expr */
10323 /* In anyways, tie the loop to its statement */
10324 LABELED_BLOCK_BODY (lbe) = statement;
10326 /* Ok, if statement is a for loop, we have to attach the labeled
10327 statement to the block the for loop belongs to and return the
10329 if (TREE_CODE (statement) == LOOP_EXPR && IS_FOR_LOOP_P (statement))
10331 java_method_add_stmt (current_function_decl, lbe);
10332 return exit_block ();
10338 /* 14.10, 14.11, 14.12 Loop Statements */
10340 /* Create an empty LOOP_EXPR and make it the last in the nested loop
10344 build_new_loop (loop_body)
10347 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
10348 TREE_SIDE_EFFECTS (loop) = 1;
10353 /* Create a loop body according to the following structure:
10355 COMPOUND_EXPR (loop main body)
10356 EXIT_EXPR (this order is for while/for loops.
10357 LABELED_BLOCK_EXPR the order is reversed for do loops)
10358 LABEL_DECL (a continue occuring here branches at the
10359 BODY end of this labeled block)
10362 REVERSED, if non zero, tells that the loop condition expr comes
10363 after the body, like in the do-while loop.
10365 To obtain a loop, the loop body structure described above is
10366 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
10369 LABEL_DECL (use this label to exit the loop)
10371 <structure described above> */
10374 build_loop_body (location, condition, reversed)
10379 tree first, second, body;
10381 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
10382 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
10383 condition = build_debugable_stmt (location, condition);
10384 TREE_SIDE_EFFECTS (condition) = 1;
10386 body = generate_labeled_block ();
10387 first = (reversed ? body : condition);
10388 second = (reversed ? condition : body);
10390 build (COMPOUND_EXPR, NULL_TREE,
10391 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
10394 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
10395 their order) on the current loop. Unlink the current loop from the
10399 complete_loop_body (location, condition, body, reversed)
10401 tree condition, body;
10404 tree to_return = ctxp->current_loop;
10405 tree loop_body = LOOP_EXPR_BODY (to_return);
10408 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
10409 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
10410 The real EXIT_EXPR is one operand further. */
10411 EXPR_WFL_LINECOL (cnode) = location;
10412 /* This one is for accurate error reports */
10413 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
10414 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
10416 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
10421 /* Tailored version of complete_loop_body for FOR loops, when FOR
10422 loops feature the condition part */
10425 complete_for_loop (location, condition, update, body)
10427 tree condition, update, body;
10429 /* Put the condition and the loop body in place */
10430 tree loop = complete_loop_body (location, condition, body, 0);
10431 /* LOOP is the current loop which has been now popped of the loop
10432 stack. Install the update block */
10433 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
10437 /* If the loop isn't surrounded by a labeled statement, create one and
10438 insert LOOP as it's body. */
10441 patch_loop_statement (loop)
10444 tree loop_label, to_return_as_loop;
10446 if (LOOP_HAS_LABEL_P (loop))
10448 loop_label = ctxp->current_labeled_block;
10449 to_return_as_loop = loop;
10453 loop_label = generate_labeled_block ();
10454 LABELED_BLOCK_BODY (loop_label) = loop;
10455 PUSH_LABELED_BLOCK (loop_label);
10456 to_return_as_loop = loop_label;
10458 TREE_TYPE (to_return_as_loop) = void_type_node;
10459 return to_return_as_loop;
10462 /* 14.13, 14.14: break and continue Statements */
10464 /* Build a break or a continue statement. a null NAME indicates an
10465 unlabeled break/continue statement. */
10468 build_bc_statement (location, is_break, name)
10469 int location, is_break;
10472 tree break_continue, label_block_expr = NULL_TREE;
10476 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
10477 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
10478 /* Null means that we don't have a target for this named
10479 break/continue. In this case, we make the target to be the
10480 label name, so that the error can be reported accuratly in
10481 patch_bc_statement. */
10482 label_block_expr = EXPR_WFL_NODE (name);
10484 /* Unlabeled break/continue will be handled during the
10485 break/continue patch operation */
10487 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
10489 IS_BREAK_STMT_P (break_continue) = is_break;
10490 TREE_SIDE_EFFECTS (break_continue) = 1;
10491 EXPR_WFL_LINECOL (break_continue) = location;
10492 break_continue = build_debugable_stmt (location, break_continue);
10493 return break_continue;
10496 /* Verification of a break/continue statement. */
10499 patch_bc_statement (node)
10502 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
10503 int is_unlabeled = 0;
10504 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10506 /* Not having a target means that the break/continue statement is
10507 unlabeled. We try to find a decent label for it */
10511 /* There should be a loop/switch to branch to */
10512 if (ctxp->current_loop)
10514 if (TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10516 /* At that stage, we're in the loop body, which is
10517 encapsulated around a LABELED_BLOCK_EXPR. So searching
10518 the current loop label requires us to consider the
10519 labeled block before the current one. */
10520 if (!LOOP_HAS_LABEL_SKIP_P (ctxp->current_loop))
10521 fatal ("unlabeled loop has no installed label -- "
10522 "patch_bc_statement");
10523 bc_label = TREE_CHAIN (ctxp->current_labeled_block);
10525 /* For a SWITCH statement, this is the current one */
10527 bc_label = ctxp->current_labeled_block;
10529 /* Not having a loop to break/continue to is an error */
10532 parse_error_context (wfl_operator, "`%s' must be in loop%s",
10533 (IS_BREAK_STMT_P (node) ? "break" : "continue"),
10534 (IS_BREAK_STMT_P (node) ? " or switch" : ""));
10535 return error_mark_node;
10538 /* Having an identifier here means that the target is unknown. */
10539 else if (TREE_CODE (bc_label) == IDENTIFIER_NODE)
10541 parse_error_context (wfl_operator, "No label definition found for `%s'",
10542 IDENTIFIER_POINTER (bc_label));
10543 return error_mark_node;
10546 /* Find the statement we're targeting. */
10547 target_stmt = LABELED_BLOCK_BODY (bc_label);
10549 /* Target loop is slightly burrowed in the case of a for loop, it
10550 appears at the first sight to be a block. */
10551 if (TREE_CODE (target_stmt) == BLOCK)
10553 tree sub = BLOCK_SUBBLOCKS (target_stmt);
10554 if (sub && TREE_CODE (sub) == COMPOUND_EXPR && TREE_OPERAND (sub, 1)
10555 && TREE_CODE (TREE_OPERAND (sub, 1)) == LOOP_EXPR)
10556 target_stmt = TREE_OPERAND (sub, 1);
10559 /* 14.13 The break Statement */
10560 if (IS_BREAK_STMT_P (node))
10562 /* Named break are always fine, as far as they have a target
10563 (already verified). Anonymous break need to target
10564 while/do/for/switch */
10565 if (is_unlabeled &&
10566 !(TREE_CODE (target_stmt) == LOOP_EXPR /* do/while/for */
10567 || TREE_CODE (target_stmt) == SWITCH_EXPR)) /* switch */
10569 parse_error_context (wfl_operator,
10570 "`break' must be in loop or switch");
10571 return error_mark_node;
10573 /* If previously unlabeled, install the new found label */
10575 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
10577 /* 14.14 The continue Statement */
10578 /* The continue statement must always target a loop, unnamed or not. */
10581 if (TREE_CODE (target_stmt) != LOOP_EXPR) /* do/while/for */
10583 parse_error_context (wfl_operator, "`continue' must be in loop");
10584 return error_mark_node;
10586 /* Everything looks good. We can fix the `continue' jump to go
10587 at the place in the loop were the continue is. For unlabeled
10588 continue, the continuation point is the current labeled
10589 block, by construction. */
10591 EXIT_BLOCK_LABELED_BLOCK (node) =
10592 bc_label = ctxp->current_labeled_block;
10595 CAN_COMPLETE_NORMALLY (bc_label) = 1;
10597 /* Our break/continue don't return values. */
10598 TREE_TYPE (node) = void_type_node;
10599 /* Encapsulate the break within a compound statement so that it's
10600 expanded all the times by expand_expr (and not clobered
10601 sometimes, like after a if statement) */
10602 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
10603 TREE_SIDE_EFFECTS (node) = 1;
10607 /* Process the exit expression belonging to a loop. Its type must be
10611 patch_exit_expr (node)
10614 tree expression = TREE_OPERAND (node, 0);
10615 TREE_TYPE (node) = error_mark_node;
10616 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10618 /* The type of expression must be boolean */
10619 if (TREE_TYPE (expression) != boolean_type_node)
10621 parse_error_context
10623 "Incompatible type for loop conditional. Can't convert `%s' to "
10625 lang_printable_name (TREE_TYPE (expression), 0));
10626 return error_mark_node;
10628 /* Now we know things are allright, invert the condition, fold and
10630 TREE_OPERAND (node, 0) =
10631 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
10633 if (! integer_zerop (TREE_OPERAND (node, 0))
10634 && ctxp->current_loop != NULL_TREE
10635 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10636 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
10637 if (! integer_onep (TREE_OPERAND (node, 0)))
10638 CAN_COMPLETE_NORMALLY (node) = 1;
10641 TREE_TYPE (node) = void_type_node;
10645 /* 14.9 Switch statement */
10648 patch_switch_statement (node)
10651 tree se = TREE_OPERAND (node, 0), se_type;
10653 /* Complete the switch expression */
10654 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
10655 se_type = TREE_TYPE (se);
10656 /* The type of the switch expression must be char, byte, short or
10658 if (!JINTEGRAL_TYPE_P (se_type))
10660 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10661 parse_error_context (wfl_operator, "Incompatible type for `switch'. "
10662 "Can't convert `%s' to `int'",
10663 lang_printable_name (se_type, 0));
10664 /* This is what java_complete_tree will check */
10665 TREE_OPERAND (node, 0) = error_mark_node;
10666 return error_mark_node;
10669 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10671 /* Ready to return */
10672 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
10674 TREE_TYPE (node) = error_mark_node;
10675 return error_mark_node;
10677 TREE_TYPE (node) = void_type_node;
10678 TREE_SIDE_EFFECTS (node) = 1;
10679 CAN_COMPLETE_NORMALLY (node)
10680 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10681 || ! SWITCH_HAS_DEFAULT (node);
10685 /* 14.18 The try statement */
10687 /* Wrap BLOCK around a LABELED_BLOCK, set DECL to the newly generated
10688 exit labeld and issue a jump to FINALLY_LABEL:
10692 <orignal_statments>
10699 build_jump_to_finally (block, decl, finally_label, type)
10700 tree block, decl, finally_label, type;
10703 tree new_block = build (LABELED_BLOCK_EXPR, type,
10704 create_label_decl (generate_name ()), block);
10706 stmt = build (MODIFY_EXPR, void_type_node, decl,
10707 build_address_of (LABELED_BLOCK_LABEL (new_block)));
10708 TREE_SIDE_EFFECTS (stmt) = 1;
10709 CAN_COMPLETE_NORMALLY (stmt) = 1;
10710 add_stmt_to_block (block, type, stmt);
10711 stmt = build (GOTO_EXPR, void_type_node, finally_label);
10712 TREE_SIDE_EFFECTS (stmt) = 1;
10713 add_stmt_to_block (block, type, stmt);
10718 build_try_statement (location, try_block, catches)
10720 tree try_block, catches;
10722 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
10723 EXPR_WFL_LINECOL (node) = location;
10728 build_try_finally_statement (location, try_block, finally)
10730 tree try_block, finally;
10732 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
10733 EXPR_WFL_LINECOL (node) = location;
10738 patch_try_statement (node)
10741 int error_found = 0;
10742 tree try = TREE_OPERAND (node, 0);
10743 /* Exception handlers are considered in left to right order */
10744 tree catch = nreverse (TREE_OPERAND (node, 1));
10745 tree current, caught_type_list = NULL_TREE;
10747 /* Check catch clauses, if any. Every time we find an error, we try
10748 to process the next catch clause. We process the catch clause before
10749 the try block so that when processing the try block we can check thrown
10750 exceptions againts the caught type list. */
10751 for (current = catch; current; current = TREE_CHAIN (current))
10753 tree carg_decl, carg_type;
10754 tree sub_current, catch_block, catch_clause;
10757 /* At this point, the structure of the catch clause is
10758 CATCH_EXPR (catch node)
10759 BLOCK (with the decl of the parameter)
10761 MODIFY_EXPR (assignment of the catch parameter)
10762 BLOCK (catch clause block)
10764 catch_clause = TREE_OPERAND (current, 0);
10765 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
10766 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
10768 /* Catch clauses can't have more than one parameter declared,
10769 but it's already enforced by the grammar. Make sure that the
10770 only parameter of the clause statement in of class Throwable
10771 or a subclass of Throwable, but that was done earlier. The
10772 catch clause parameter type has also been resolved. */
10774 /* Just make sure that the catch clause parameter type inherits
10775 from java.lang.Throwable */
10776 if (!inherits_from_p (carg_type, throwable_type_node))
10778 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10779 parse_error_context (wfl_operator,
10780 "Can't catch class `%s'. Catch clause "
10781 "parameter type must be a subclass of "
10782 "class `java.lang.Throwable'",
10783 lang_printable_name (carg_type, 0));
10788 /* Partial check for unreachable catch statement: The catch
10789 clause is reachable iff is no earlier catch block A in
10790 the try statement such that the type of the catch
10791 clause's parameter is the same as or a subclass of the
10792 type of A's parameter */
10794 for (sub_current = catch;
10795 sub_current != current; sub_current = TREE_CHAIN (sub_current))
10797 tree sub_catch_clause, decl;
10798 sub_catch_clause = TREE_OPERAND (sub_current, 0);
10799 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
10801 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
10803 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10804 parse_error_context
10805 (wfl_operator, "`catch' not reached because of the catch "
10806 "clause at line %d", EXPR_WFL_LINENO (sub_current));
10807 unreachable = error_found = 1;
10811 /* Complete the catch clause block */
10812 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
10813 if (catch_block == error_mark_node)
10818 if (CAN_COMPLETE_NORMALLY (catch_block))
10819 CAN_COMPLETE_NORMALLY (node) = 1;
10820 TREE_OPERAND (current, 0) = catch_block;
10825 /* Things to do here: the exception must be thrown */
10827 /* Link this type to the caught type list */
10828 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
10831 PUSH_EXCEPTIONS (caught_type_list);
10832 if ((try = java_complete_tree (try)) == error_mark_node)
10834 if (CAN_COMPLETE_NORMALLY (try))
10835 CAN_COMPLETE_NORMALLY (node) = 1;
10838 /* Verification ends here */
10840 return error_mark_node;
10842 TREE_OPERAND (node, 0) = try;
10843 TREE_OPERAND (node, 1) = catch;
10844 TREE_TYPE (node) = void_type_node;
10848 /* 14.17 The synchronized Statement */
10851 patch_synchronized_statement (node, wfl_op1)
10852 tree node, wfl_op1;
10854 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
10855 tree block = TREE_OPERAND (node, 1);
10857 tree enter, exit, expr_decl, assignment;
10859 if (expr == error_mark_node)
10861 block = java_complete_tree (block);
10865 /* The TYPE of expr must be a reference type */
10866 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
10868 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10869 parse_error_context (wfl_operator, "Incompatible type for `synchronized'"
10870 ". Can't convert `%s' to `java.lang.Object'",
10871 lang_printable_name (TREE_TYPE (expr), 0));
10872 return error_mark_node;
10875 /* Generate a try-finally for the synchronized statement, except
10876 that the handler that catches all throw exception calls
10877 _Jv_MonitorExit and then rethrow the exception.
10878 The synchronized statement is then implemented as:
10881 _Jv_MonitorEnter (expression)
10883 _Jv_MonitorExit (expression)
10887 e = _Jv_exception_info ();
10888 _Jv_MonitorExit (expression)
10892 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
10893 BUILD_MONITOR_ENTER (enter, expr_decl);
10894 BUILD_MONITOR_EXIT (exit, expr_decl);
10895 CAN_COMPLETE_NORMALLY (enter) = 1;
10896 CAN_COMPLETE_NORMALLY (exit) = 1;
10897 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
10898 TREE_SIDE_EFFECTS (assignment) = 1;
10899 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
10900 build (COMPOUND_EXPR, NULL_TREE,
10901 build (WITH_CLEANUP_EXPR, NULL_TREE,
10902 build (COMPOUND_EXPR, NULL_TREE,
10903 assignment, enter),
10906 node = build_expr_block (node, expr_decl);
10908 return java_complete_tree (node);
10911 /* 14.16 The throw Statement */
10914 patch_throw_statement (node, wfl_op1)
10915 tree node, wfl_op1;
10917 tree expr = TREE_OPERAND (node, 0);
10918 tree type = TREE_TYPE (expr);
10919 int unchecked_ok = 0, tryblock_throws_ok = 0;
10921 /* Thrown expression must be assignable to java.lang.Throwable */
10922 if (!try_reference_assignconv (throwable_type_node, expr))
10924 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10925 parse_error_context (wfl_operator, "Can't throw `%s'; it must be a "
10926 "subclass of class `java.lang.Throwable'",
10927 lang_printable_name (type, 0));
10928 /* If the thrown expression was a reference, we further the
10929 compile-time check. */
10930 if (!JREFERENCE_TYPE_P (type))
10931 return error_mark_node;
10934 /* At least one of the following must be true */
10936 /* The type of the throw expression is a not checked exception,
10937 i.e. is a unchecked expression. */
10938 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
10940 /* Throw is contained in a try statement and at least one catch
10941 clause can receive the thrown expression or the current method is
10942 declared to throw such an exception. Or, the throw statement is
10943 contained in a method or constructor declaration and the type of
10944 the Expression is assignable to at least one type listed in the
10945 throws clause the declaration. */
10946 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10948 tryblock_throws_ok =
10949 check_thrown_exceptions_do (TREE_TYPE (expr));
10950 if (!(unchecked_ok || tryblock_throws_ok))
10952 /* If there is a surrounding try block that has no matching
10953 clatch clause, report it first. A surrounding try block exits
10954 only if there is something after the list of checked
10955 exception thrown by the current function (if any). */
10956 if (IN_TRY_BLOCK_P ())
10957 parse_error_context (wfl_operator, "Checked exception `%s' can't be "
10958 "caught by any of the catch clause(s) "
10959 "of the surrounding `try' block",
10960 lang_printable_name (type, 0));
10961 /* If we have no surrounding try statement and the method doesn't have
10962 any throws, report it now. FIXME */
10963 else if (!EXCEPTIONS_P (currently_caught_type_list)
10964 && !tryblock_throws_ok)
10965 parse_error_context (wfl_operator, "Checked exception `%s' isn't "
10966 "thrown from a `try' block",
10967 lang_printable_name (type, 0));
10968 /* Otherwise, the current method doesn't have the appropriate
10969 throws declaration */
10971 parse_error_context (wfl_operator, "Checked exception `%s' doesn't "
10972 "match any of current method's `throws' "
10974 lang_printable_name (type, 0));
10975 return error_mark_node;
10978 /* If a throw statement is contained in a static initializer, then a
10979 compile-time check ensures that either its value is always an
10980 unchecked exception or its value is always caught by some try
10981 statement that contains it. FIXME, static initializer. */
10983 if (! flag_emit_class_files)
10984 BUILD_THROW (node, expr);
10988 /* Check that exception said to be thrown by method DECL can be
10989 effectively caught from where DECL is invoked. */
10992 check_thrown_exceptions (location, decl)
10997 /* For all the unchecked exceptions thrown by DECL */
10998 for (throws = DECL_FUNCTION_THROWS (decl); throws;
10999 throws = TREE_CHAIN (throws))
11000 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
11003 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
11004 if (DECL_NAME (decl) == get_identifier ("clone"))
11007 EXPR_WFL_LINECOL (wfl_operator) = location;
11008 parse_error_context
11009 (wfl_operator, "Exception `%s' must be caught, or it must be "
11010 "declared in the `throws' clause of `%s'",
11011 lang_printable_name (TREE_VALUE (throws), 0),
11012 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
11016 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
11017 try-catch blocks, OR is listed in the `throws' clause of the
11021 check_thrown_exceptions_do (exception)
11024 tree list = currently_caught_type_list;
11025 resolve_and_layout (exception, NULL_TREE);
11026 /* First, all the nested try-catch-finally at that stage. The
11027 last element contains `throws' clause exceptions, if any. */
11028 if (IS_UNCHECKED_EXCEPTION_P (exception))
11033 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
11034 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
11036 list = TREE_CHAIN (list);
11042 purge_unchecked_exceptions (mdecl)
11045 tree throws = DECL_FUNCTION_THROWS (mdecl);
11046 tree new = NULL_TREE;
11050 tree next = TREE_CHAIN (throws);
11051 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
11053 TREE_CHAIN (throws) = new;
11058 /* List is inverted here, but it doesn't matter */
11059 DECL_FUNCTION_THROWS (mdecl) = new;
11062 /* 15.24 Conditional Operator ?: */
11065 patch_conditional_expr (node, wfl_cond, wfl_op1)
11066 tree node, wfl_cond, wfl_op1;
11068 tree cond = TREE_OPERAND (node, 0);
11069 tree op1 = TREE_OPERAND (node, 1);
11070 tree op2 = TREE_OPERAND (node, 2);
11071 tree resulting_type = NULL_TREE;
11072 tree t1, t2, patched;
11073 int error_found = 0;
11075 /* Operands of ?: might be StringBuffers crafted as a result of a
11076 string concatenation. Obtain a descent operand here. */
11077 if ((patched = patch_string (op1)))
11078 TREE_OPERAND (node, 1) = op1 = patched;
11079 if ((patched = patch_string (op2)))
11080 TREE_OPERAND (node, 2) = op2 = patched;
11082 t1 = TREE_TYPE (op1);
11083 t2 = TREE_TYPE (op2);
11085 /* The first expression must be a boolean */
11086 if (TREE_TYPE (cond) != boolean_type_node)
11088 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
11089 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11090 "convert `%s' to `boolean'",
11091 lang_printable_name (TREE_TYPE (cond), 0));
11095 /* Second and third can be numeric, boolean (i.e. primitive),
11096 references or null. Anything else results in an error */
11097 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
11098 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
11099 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
11100 || (t1 == boolean_type_node && t2 == boolean_type_node)))
11103 /* Determine the type of the conditional expression. Same types are
11104 easy to deal with */
11106 resulting_type = t1;
11108 /* There are different rules for numeric types */
11109 else if (JNUMERIC_TYPE_P (t1))
11111 /* if byte/short found, the resulting type is short */
11112 if ((t1 == byte_type_node && t2 == short_type_node)
11113 || (t1 == short_type_node && t2 == byte_type_node))
11114 resulting_type = short_type_node;
11116 /* If t1 is a constant int and t2 is of type byte, short or char
11117 and t1's value fits in t2, then the resulting type is t2 */
11118 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
11119 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
11120 resulting_type = t2;
11122 /* If t2 is a constant int and t1 is of type byte, short or char
11123 and t2's value fits in t1, then the resulting type is t1 */
11124 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
11125 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
11126 resulting_type = t1;
11128 /* Otherwise, binary numeric promotion is applied and the
11129 resulting type is the promoted type of operand 1 and 2 */
11131 resulting_type = binary_numeric_promotion (t2, t2,
11132 &TREE_OPERAND (node, 1),
11133 &TREE_OPERAND (node, 2));
11136 /* Cases of a reference and a null type */
11137 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
11138 resulting_type = t1;
11140 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
11141 resulting_type = t2;
11143 /* Last case: different reference types. If a type can be converted
11144 into the other one by assignment conversion, the latter
11145 determines the type of the expression */
11146 else if ((resulting_type = try_reference_assignconv (t1, op2)))
11147 resulting_type = promote_type (t1);
11149 else if ((resulting_type = try_reference_assignconv (t2, op1)))
11150 resulting_type = promote_type (t2);
11152 /* If we don't have any resulting type, we're in trouble */
11153 if (!resulting_type)
11155 char *t = strdup (lang_printable_name (t1, 0));
11156 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
11157 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11158 "convert `%s' to `%s'", t,
11159 lang_printable_name (t2, 0));
11166 TREE_TYPE (node) = error_mark_node;
11167 return error_mark_node;
11170 TREE_TYPE (node) = resulting_type;
11171 TREE_SET_CODE (node, COND_EXPR);
11172 CAN_COMPLETE_NORMALLY (node) = 1;
11176 /* Try to constant fold NODE.
11177 If NODE is not a constant expression, return NULL_EXPR.
11178 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
11181 fold_constant_for_init (node, context)
11185 tree op0, op1, val;
11186 enum tree_code code = TREE_CODE (node);
11188 if (code == INTEGER_CST || code == REAL_CST || code == STRING_CST)
11190 if (TREE_TYPE (node) != NULL_TREE)
11198 case TRUNC_MOD_EXPR:
11206 case TRUTH_ANDIF_EXPR:
11207 case TRUTH_ORIF_EXPR:
11214 op0 = TREE_OPERAND (node, 0);
11215 op1 = TREE_OPERAND (node, 1);
11216 val = fold_constant_for_init (op0, context);
11217 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11219 TREE_OPERAND (node, 0) = val;
11220 val = fold_constant_for_init (op1, context);
11221 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11223 TREE_OPERAND (node, 1) = val;
11224 return patch_binop (node, op0, op1);
11226 case UNARY_PLUS_EXPR:
11228 case TRUTH_NOT_EXPR:
11231 op0 = TREE_OPERAND (node, 0);
11232 val = fold_constant_for_init (op0, context);
11233 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11235 TREE_OPERAND (node, 0) = val;
11236 return patch_unaryop (node, op0);
11240 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
11241 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11243 TREE_OPERAND (node, 0) = val;
11244 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
11245 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11247 TREE_OPERAND (node, 1) = val;
11248 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
11249 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11251 TREE_OPERAND (node, 2) = val;
11252 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
11253 : TREE_OPERAND (node, 2);
11256 if (! FIELD_STATIC (node) || ! FIELD_FINAL (node)
11257 || DECL_INITIAL (node) == NULL_TREE)
11259 val = DECL_INITIAL (node);
11260 /* Guard against infinite recursion. */
11261 DECL_INITIAL (node) = NULL_TREE;
11262 val = fold_constant_for_init (val, DECL_CONTEXT (node));
11263 DECL_INITIAL (node) = val;
11266 case EXPR_WITH_FILE_LOCATION:
11267 /* Compare java_complete_tree and resolve_expression_name. */
11268 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11269 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11271 tree name = EXPR_WFL_NODE (node);
11273 if (PRIMARY_P (node))
11275 else if (! QUALIFIED_P (name))
11277 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
11278 if (! FIELD_STATIC (decl))
11280 return fold_constant_for_init (decl, decl);
11285 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
11286 qualify_ambiguous_name (node);
11287 if (resolve_field_access (node, &decl, NULL)
11288 && decl != NULL_TREE)
11289 return fold_constant_for_init (decl, decl);
11296 op0 = TREE_OPERAND (node, 0);
11297 val = fold_constant_for_init (op0, context);
11298 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11300 TREE_OPERAND (node, 0) = val;
11304 #ifdef USE_COMPONENT_REF
11306 case COMPONENT_REF:
11315 #ifdef USE_COMPONENT_REF
11316 /* Context is 'T' for TypeName, 'P' for PackageName,
11317 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
11320 resolve_simple_name (name, context)
11327 resolve_qualified_name (name, context)