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, tree));
205 static tree patch_try_statement PROTO ((tree));
206 static tree patch_synchronized_statement PROTO ((tree, tree));
207 static tree patch_throw_statement PROTO ((tree, tree));
208 static void check_thrown_exceptions PROTO ((int, tree));
209 static int check_thrown_exceptions_do PROTO ((tree));
210 static void purge_unchecked_exceptions PROTO ((tree));
211 static void check_throws_clauses PROTO ((tree, tree, tree));
212 static void complete_method_declaration PROTO ((tree));
213 static tree build_super_invocation PROTO (());
214 static int verify_constructor_circularity PROTO ((tree, tree));
215 static char *constructor_circularity_msg PROTO ((tree, tree));
216 static tree build_this_super_qualified_invocation PROTO ((int, tree, tree,
218 static char *get_printable_method_name PROTO ((tree));
219 static tree patch_conditional_expr PROTO ((tree, tree, tree));
220 static void maybe_generate_finit PROTO (());
221 static void fix_constructors PROTO ((tree));
222 static int verify_constructor_super PROTO (());
223 static tree create_artificial_method PROTO ((tree, int, tree, tree, tree));
224 static void start_artificial_method_body PROTO ((tree));
225 static void end_artificial_method_body PROTO ((tree));
226 static int check_method_redefinition PROTO ((tree, tree));
227 static int reset_method_name PROTO ((tree));
228 static void java_check_regular_methods PROTO ((tree));
229 static void java_check_abstract_methods PROTO ((tree));
230 static tree maybe_build_primttype_type_ref PROTO ((tree, tree));
231 static void unreachable_stmt_error PROTO ((tree));
232 static tree find_expr_with_wfl PROTO ((tree));
233 static void missing_return_error PROTO ((tree));
234 static tree build_new_array_init PROTO ((int, tree));
235 static tree patch_new_array_init PROTO ((tree, tree));
236 static tree maybe_build_array_element_wfl PROTO ((tree));
237 static int array_constructor_check_entry PROTO ((tree, tree));
238 static char *purify_type_name PROTO ((char *));
239 static tree patch_initialized_static_field PROTO ((tree));
240 static tree fold_constant_for_init PROTO ((tree, tree));
242 /* Number of error found so far. */
243 int java_error_count;
244 /* Number of warning found so far. */
245 int java_warning_count;
247 /* The current parser context */
248 static struct parser_ctxt *ctxp;
250 /* List of things that were anlyzed for which code will be generated */
251 static struct parser_ctxt *ctxp_for_generation = NULL;
253 /* binop_lookup maps token to tree_code. It is used where binary
254 operations are involved and required by the parser. RDIV_EXPR
255 covers both integral/floating point division. The code is changed
256 once the type of both operator is worked out. */
258 static enum tree_code binop_lookup[19] =
260 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
261 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
262 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
263 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
264 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
266 #define BINOP_LOOKUP(VALUE) \
267 binop_lookup [((VALUE) - PLUS_TK)% \
268 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
270 /* Fake WFL used to report error message. It is initialized once if
271 needed and reused with it's location information is overriden. */
272 tree wfl_operator = NULL_TREE;
274 /* The "$L" identifier we use to create labels. */
275 static tree label_id = NULL_TREE;
277 /* The "StringBuffer" identifier used for the String `+' operator. */
278 static tree wfl_string_buffer = NULL_TREE;
280 /* The "append" identifier used for String `+' operator. */
281 static tree wfl_append = NULL_TREE;
283 /* The "toString" identifier used for String `+' operator. */
284 static tree wfl_to_string = NULL_TREE;
286 /* The "java.lang" import qualified name. */
287 static tree java_lang_id = NULL_TREE;
302 /* Things defined here have to match the order of what's in the
303 binop_lookup table. */
305 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
306 %token LS_TK SRS_TK ZRS_TK
307 %token AND_TK XOR_TK OR_TK
308 %token BOOL_AND_TK BOOL_OR_TK
309 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
311 /* This maps to the same binop_lookup entry than the token above */
313 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
315 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
316 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
319 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
321 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
322 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
323 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
324 %token PAD_TK ABSTRACT_TK MODIFIER_TK
326 /* Keep those two in order, too */
327 %token DECR_TK INCR_TK
329 /* From now one, things can be in any order */
331 %token DEFAULT_TK IF_TK THROW_TK
332 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
333 %token THROWS_TK BREAK_TK IMPORT_TK
334 %token ELSE_TK INSTANCEOF_TK RETURN_TK
335 %token VOID_TK CATCH_TK INTERFACE_TK
336 %token CASE_TK EXTENDS_TK FINALLY_TK
337 %token SUPER_TK WHILE_TK CLASS_TK
338 %token SWITCH_TK CONST_TK TRY_TK
339 %token FOR_TK NEW_TK CONTINUE_TK
340 %token GOTO_TK PACKAGE_TK THIS_TK
342 %token BYTE_TK SHORT_TK INT_TK LONG_TK
343 %token CHAR_TK INTEGRAL_TK
345 %token FLOAT_TK DOUBLE_TK FP_TK
349 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
351 %token ASSIGN_ANY_TK ASSIGN_TK
352 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
354 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
355 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
357 %type <value> modifiers MODIFIER_TK
359 %type <node> super ID_TK identifier
360 %type <node> name simple_name qualified_name
361 %type <node> class_declaration type_declaration compilation_unit
362 field_declaration method_declaration extends_interfaces
363 interfaces interface_type_list
364 interface_declaration class_member_declaration
365 import_declarations package_declaration
366 type_declarations interface_body
367 interface_member_declaration constant_declaration
368 interface_member_declarations interface_type
369 abstract_method_declaration interface_type_list
370 %type <node> class_body_declaration class_member_declaration
371 static_initializer constructor_declaration block
372 %type <node> class_body_declarations constructor_header
373 %type <node> class_or_interface_type class_type class_type_list
374 constructor_declarator explicit_constructor_invocation
375 %type <node> dim_expr dim_exprs this_or_super throws
377 %type <node> variable_declarator_id variable_declarator
378 variable_declarators variable_initializer
379 variable_initializers constructor_body
382 %type <node> class_body block_end
383 %type <node> statement statement_without_trailing_substatement
384 labeled_statement if_then_statement label_decl
385 if_then_else_statement while_statement for_statement
386 statement_nsi labeled_statement_nsi do_statement
387 if_then_else_statement_nsi while_statement_nsi
388 for_statement_nsi statement_expression_list for_init
389 for_update statement_expression expression_statement
390 primary_no_new_array expression primary
391 array_creation_expression array_type
392 class_instance_creation_expression field_access
393 method_invocation array_access something_dot_new
394 argument_list postfix_expression while_expression
395 post_increment_expression post_decrement_expression
396 unary_expression_not_plus_minus unary_expression
397 pre_increment_expression pre_decrement_expression
398 unary_expression_not_plus_minus cast_expression
399 multiplicative_expression additive_expression
400 shift_expression relational_expression
401 equality_expression and_expression
402 exclusive_or_expression inclusive_or_expression
403 conditional_and_expression conditional_or_expression
404 conditional_expression assignment_expression
405 left_hand_side assignment for_header for_begin
406 constant_expression do_statement_begin empty_statement
407 switch_statement synchronized_statement throw_statement
408 try_statement switch_expression switch_block
409 catches catch_clause catch_clause_parameter finally
410 %type <node> return_statement break_statement continue_statement
412 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
413 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
414 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
415 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
416 %type <operator> ASSIGN_ANY_TK assignment_operator
417 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
418 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
419 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
420 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK
421 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
422 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
423 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
425 %type <node> method_body
427 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
428 STRING_LIT_TK NULL_TK VOID_TK
430 %type <node> IF_TK WHILE_TK FOR_TK
432 %type <node> formal_parameter_list formal_parameter
433 method_declarator method_header
435 %type <node> primitive_type reference_type type
436 BOOLEAN_TK INTEGRAL_TK FP_TK
439 /* 19.2 Production from 2.3: The Syntactic Grammar */
445 /* 19.3 Productions from 3: Lexical structure */
455 /* 19.4 Productions from 4: Types, Values and Variables */
468 class_or_interface_type
472 class_or_interface_type:
477 class_or_interface_type /* Default rule */
481 class_or_interface_type
485 primitive_type OSB_TK CSB_TK
487 $$ = build_java_array_type ($1, -1);
488 CLASS_LOADED_P ($$) = 1;
491 { $$ = build_unresolved_array_type ($1); }
492 | array_type OSB_TK CSB_TK
493 { $$ = build_unresolved_array_type ($1); }
494 | primitive_type OSB_TK error
495 {RULE ("']' expected"); RECOVER;}
496 | array_type OSB_TK error
497 {RULE ("']' expected"); RECOVER;}
500 /* 19.5 Productions from 6: Names */
502 simple_name /* Default rule */
503 | qualified_name /* Default rule */
507 identifier /* Default rule */
511 name DOT_TK identifier
512 { $$ = make_qualified_name ($1, $3, $2.location); }
519 /* 19.6: Production from 7: Packages */
522 | package_declaration
523 | import_declarations
525 | package_declaration import_declarations
526 | package_declaration type_declarations
527 | import_declarations type_declarations
528 | package_declaration import_declarations type_declarations
536 | import_declarations import_declaration
544 | type_declarations type_declaration
548 PACKAGE_TK name SC_TK
549 { ctxp->package = EXPR_WFL_NODE ($2); }
551 {yyerror ("Missing name"); RECOVER;}
552 | PACKAGE_TK name error
553 {yyerror ("';' expected"); RECOVER;}
557 single_type_import_declaration
558 | type_import_on_demand_declaration
561 single_type_import_declaration:
564 tree name = EXPR_WFL_NODE ($2), node, last_name;
565 int i = IDENTIFIER_LENGTH (name)-1;
566 char *last = &IDENTIFIER_POINTER (name)[i];
567 while (last != IDENTIFIER_POINTER (name))
573 last_name = get_identifier (++last);
574 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
576 tree err = find_name_in_single_imports (last_name);
577 if (err && err != name)
579 ($2, "Ambiguous class: `%s' and `%s'",
580 IDENTIFIER_POINTER (name),
581 IDENTIFIER_POINTER (err));
583 REGISTER_IMPORT ($2, last_name)
586 REGISTER_IMPORT ($2, last_name);
589 {yyerror ("Missing name"); RECOVER;}
590 | IMPORT_TK name error
591 {yyerror ("';' expected"); RECOVER;}
594 type_import_on_demand_declaration:
595 IMPORT_TK name DOT_TK MULT_TK SC_TK
597 tree name = EXPR_WFL_NODE ($2);
598 /* Don't import java.lang.* twice. */
599 if (name != java_lang_id)
601 tree node = build_tree_list ($2, NULL_TREE);
602 read_import_dir ($2);
603 TREE_CHAIN (node) = ctxp->import_demand_list;
604 ctxp->import_demand_list = node;
607 | IMPORT_TK name DOT_TK error
608 {yyerror ("'*' expected"); RECOVER;}
609 | IMPORT_TK name DOT_TK MULT_TK error
610 {yyerror ("';' expected"); RECOVER;}
616 maybe_generate_finit ();
617 maybe_generate_clinit ();
620 | interface_declaration
626 yyerror ("Class or interface declaration expected");
630 /* 19.7 Shortened from the original:
631 modifiers: modifier | modifiers modifier
632 modifier: any of public... */
638 | modifiers MODIFIER_TK
643 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
644 java_accstring_lookup (acc));
652 /* 19.8.1 Production from $8.1: Class Declaration */
654 modifiers CLASS_TK identifier super interfaces
655 { create_class ($1, $3, $4, $5); }
660 | CLASS_TK identifier super interfaces
661 { create_class (0, $2, $3, $4); }
666 | modifiers CLASS_TK error
667 {yyerror ("Missing class name"); RECOVER;}
669 {yyerror ("Missing class name"); RECOVER;}
670 | CLASS_TK identifier error
671 {if (!ctxp->class_err) yyerror ("'{' expected"); DRECOVER(class1);}
672 | modifiers CLASS_TK identifier error
673 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
678 | EXTENDS_TK class_type
680 | EXTENDS_TK class_type error
681 {yyerror ("'{' expected"); ctxp->class_err=1;}
683 {yyerror ("Missing super class name"); ctxp->class_err=1;}
688 | IMPLEMENTS_TK interface_type_list
690 | IMPLEMENTS_TK error
693 yyerror ("Missing interface name");
700 ctxp->interface_number = 1;
701 $$ = build_tree_list ($1, NULL_TREE);
703 | interface_type_list C_TK interface_type
705 ctxp->interface_number++;
706 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
708 | interface_type_list C_TK error
709 {yyerror ("Missing interface name"); RECOVER;}
714 { $$ = ctxp->current_parsed_class; }
715 | OCB_TK class_body_declarations CCB_TK
716 { $$ = ctxp->current_parsed_class; }
719 class_body_declarations:
720 class_body_declaration
721 | class_body_declarations class_body_declaration
724 class_body_declaration:
725 class_member_declaration
727 | constructor_declaration
728 | block /* Added, JDK1.1, instance initializer */
729 { $$ = parse_jdk1_1_error ("instance initializer"); }
732 class_member_declaration:
735 | class_declaration /* Added, JDK1.1 inner classes */
736 { $$ = parse_jdk1_1_error ("inner classe declaration"); }
737 | interface_declaration /* Added, JDK1.1 inner classes */
738 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
741 /* 19.8.2 Productions from 8.3: Field Declarations */
743 type variable_declarators SC_TK
744 { register_fields (0, $1, $2); }
745 | modifiers type variable_declarators SC_TK
748 ("Illegal modifier `%s' for field declaration",
749 $1, FIELD_MODIFIERS);
750 check_modifiers_consistency ($1);
751 register_fields ($1, $2, $3);
755 variable_declarators:
756 /* Should we use build_decl_list () instead ? FIXME */
757 variable_declarator /* Default rule */
758 | variable_declarators C_TK variable_declarator
759 { $$ = chainon ($1, $3); }
760 | variable_declarators C_TK error
761 {yyerror ("Missing term"); RECOVER;}
765 variable_declarator_id
766 { $$ = build_tree_list ($1, NULL_TREE); }
767 | variable_declarator_id ASSIGN_TK variable_initializer
769 if (java_error_count)
772 ($1, build_assignment ($2.token, $2.location, $1, $3));
774 | variable_declarator_id ASSIGN_TK error
776 yyerror ("Missing variable initializer");
777 $$ = build_tree_list ($1, NULL_TREE);
780 | variable_declarator_id ASSIGN_TK variable_initializer error
782 yyerror ("';' expected");
783 $$ = build_tree_list ($1, NULL_TREE);
788 variable_declarator_id:
790 | variable_declarator_id OSB_TK CSB_TK
791 { $$ = build_unresolved_array_type ($1); }
793 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
794 | variable_declarator_id OSB_TK error
795 {yyerror ("']' expected"); DRECOVER(vdi);}
796 | variable_declarator_id CSB_TK error
797 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
800 variable_initializer:
805 /* 19.8.3 Productions from 8.4: Method Declarations */
809 current_function_decl = $1;
810 source_start_java_method (current_function_decl);
813 { complete_method_declaration ($3); }
814 | method_header error
815 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
819 type method_declarator throws
820 { $$ = method_header (0, $1, $2, $3); }
821 | VOID_TK method_declarator throws
822 { $$ = method_header (0, void_type_node, $2, $3); }
823 | modifiers type method_declarator throws
824 { $$ = method_header ($1, $2, $3, $4); }
825 | modifiers VOID_TK method_declarator throws
826 { $$ = method_header ($1, void_type_node, $3, $4); }
829 | modifiers type error
832 {yyerror ("Identifier expected"); RECOVER;}
833 | modifiers VOID_TK error
834 {yyerror ("Identifier expected"); RECOVER;}
837 yyerror ("Invalid method declaration, return type required");
843 identifier OP_TK CP_TK
844 { $$ = method_declarator ($1, NULL_TREE); }
845 | identifier OP_TK formal_parameter_list CP_TK
846 { $$ = method_declarator ($1, $3); }
847 | method_declarator OSB_TK CSB_TK
849 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
851 build_unresolved_array_type (TREE_PURPOSE ($1));
852 parse_warning_context
854 "Discouraged form of returned type specification");
856 | identifier OP_TK error
857 {yyerror ("')' expected"); DRECOVER(method_declarator);}
858 | method_declarator OSB_TK error
859 {yyerror ("']' expected"); RECOVER;}
862 formal_parameter_list:
865 ctxp->formal_parameter_number = 1;
867 | formal_parameter_list C_TK formal_parameter
869 ctxp->formal_parameter_number += 1;
870 $$ = chainon ($1, $3);
872 | formal_parameter_list C_TK error
873 {yyerror ("Missing formal parameter term"); RECOVER;}
877 type variable_declarator_id
879 $$ = build_tree_list ($2, $1);
881 | modifiers type variable_declarator_id /* Added, JDK1.1 final parms */
882 { $$ = parse_jdk1_1_error ("final parameters"); }
884 {yyerror ("Missing identifier"); RECOVER;}
885 | modifiers type error
887 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
888 yyerror ("Missing identifier"); RECOVER;
894 | THROWS_TK class_type_list
897 {yyerror ("Missing class type term"); RECOVER;}
902 { $$ = build_tree_list ($1, $1); }
903 | class_type_list C_TK class_type
904 { $$ = tree_cons ($3, $3, $1); }
905 | class_type_list C_TK error
906 {yyerror ("Missing class type term"); RECOVER;}
913 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
916 /* 19.8.4 Productions from 8.5: Static Initializers */
920 RULE ("STATIC_INITIALIZER");
922 | static block SC_TK /* Shouldn't be here. FIXME */
924 RULE ("STATIC_INITIALIZER");
928 static: /* Test lval.sub_token here */
931 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
935 /* 19.8.5 Productions from 8.6: Constructor Declarations */
936 constructor_declaration:
939 current_function_decl = $1;
940 source_start_java_method (current_function_decl);
943 { complete_method_declaration ($3); }
947 constructor_declarator throws
948 { $$ = method_header (0, NULL_TREE, $1, $2); }
949 | modifiers constructor_declarator throws
950 { $$ = method_header ($1, NULL_TREE, $2, $3); }
953 constructor_declarator:
954 simple_name OP_TK CP_TK
955 { $$ = method_declarator ($1, NULL_TREE); }
956 | simple_name OP_TK formal_parameter_list CP_TK
957 { $$ = method_declarator ($1, $3); }
961 /* Unlike regular method, we always need a complete (empty)
962 body so we can safely perform all the required code
963 addition (super invocation and field initialization) */
964 block_begin block_end
966 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
969 | block_begin explicit_constructor_invocation block_end
971 | block_begin block_statements block_end
973 | block_begin explicit_constructor_invocation block_statements block_end
977 /* Error recovery for that rule moved down expression_statement: rule. */
978 explicit_constructor_invocation:
979 this_or_super OP_TK CP_TK SC_TK
981 $$ = build_method_invocation ($1, NULL_TREE);
982 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
983 $$ = java_method_add_stmt (current_function_decl, $$);
985 | this_or_super OP_TK argument_list CP_TK SC_TK
987 $$ = build_method_invocation ($1, $3);
988 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
989 $$ = java_method_add_stmt (current_function_decl, $$);
991 /* Added, JDK1.1 inner classes. Modified because the rule
992 'primary' couldn't work. */
993 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
994 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
995 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
996 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
999 this_or_super: /* Added, simplifies error diagnostics */
1002 tree wfl = build_wfl_node (this_identifier_node,
1003 input_filename, 0, 0);
1004 EXPR_WFL_LINECOL (wfl) = $1.location;
1009 tree wfl = build_wfl_node (super_identifier_node,
1010 input_filename, 0, 0);
1011 EXPR_WFL_LINECOL (wfl) = $1.location;
1016 /* 19.9 Productions from 9: Interfaces */
1017 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1018 interface_declaration:
1019 INTERFACE_TK identifier
1020 { create_interface (0, $2, NULL_TREE); }
1025 | modifiers INTERFACE_TK identifier
1026 { create_interface ($1, $3, NULL_TREE); }
1031 | INTERFACE_TK identifier extends_interfaces
1032 { create_interface (0, $2, $3); }
1037 | modifiers INTERFACE_TK identifier extends_interfaces
1038 { create_interface ($1, $3, $4); }
1043 | INTERFACE_TK identifier error
1044 {yyerror ("(here)'{' expected"); RECOVER;}
1045 | modifiers INTERFACE_TK identifier error
1046 {yyerror ("(there)'{' expected"); RECOVER;}
1050 EXTENDS_TK interface_type
1052 ctxp->interface_number = 1;
1053 $$ = build_tree_list ($2, NULL_TREE);
1055 | extends_interfaces C_TK interface_type
1057 ctxp->interface_number++;
1058 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1061 {yyerror ("Invalid interface type"); RECOVER;}
1062 | extends_interfaces C_TK error
1063 {yyerror ("Missing term"); RECOVER;}
1069 | OCB_TK interface_member_declarations CCB_TK
1073 interface_member_declarations:
1074 interface_member_declaration
1075 | interface_member_declarations interface_member_declaration
1078 interface_member_declaration:
1079 constant_declaration
1080 | abstract_method_declaration
1081 | class_declaration /* Added, JDK1.1 inner classes */
1082 { $$ = parse_jdk1_1_error ("inner class declaration"); }
1083 | interface_declaration /* Added, JDK1.1 inner classes */
1084 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
1087 constant_declaration:
1091 abstract_method_declaration:
1094 check_abstract_method_header ($1);
1095 current_function_decl = NULL_TREE; /* FIXME ? */
1097 | method_header error
1098 {yyerror ("';' expected"); RECOVER;}
1101 /* 19.10 Productions from 10: Arrays */
1104 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1105 | OCB_TK variable_initializers CCB_TK
1106 { $$ = build_new_array_init ($1.location, $2); }
1107 | OCB_TK variable_initializers C_TK CCB_TK
1108 { $$ = build_new_array_init ($1.location, $2); }
1111 variable_initializers:
1112 variable_initializer
1114 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1117 | variable_initializers C_TK variable_initializer
1119 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1121 | variable_initializers C_TK error
1122 {yyerror ("Missing term"); RECOVER;}
1125 /* 19.11 Production from 14: Blocks and Statements */
1128 { $$ = empty_stmt_node; }
1129 | block_begin block_statements block_end
1141 maybe_absorb_scoping_blocks ();
1148 | block_statements block_statement
1152 local_variable_declaration_statement
1154 { java_method_add_stmt (current_function_decl, $1); }
1155 | class_declaration /* Added, JDK1.1 inner classes */
1156 { parse_jdk1_1_error ("inner class declaration"); }
1159 local_variable_declaration_statement:
1160 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1163 local_variable_declaration:
1164 type variable_declarators
1165 { declare_local_variables (0, $1, $2); }
1166 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
1167 { declare_local_variables ($1, $2, $3); }
1171 statement_without_trailing_substatement
1174 | if_then_else_statement
1178 /* If the for loop is unlabeled, we must return the
1179 block it was defined it. It our last chance to
1180 get a hold on it. */
1181 if (!LOOP_HAS_LABEL_P ($$))
1187 statement_without_trailing_substatement
1188 | labeled_statement_nsi
1189 | if_then_else_statement_nsi
1190 | while_statement_nsi
1194 statement_without_trailing_substatement:
1197 | expression_statement
1201 | continue_statement
1203 | synchronized_statement
1210 { $$ = empty_stmt_node; }
1214 identifier REL_CL_TK
1216 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1217 EXPR_WFL_NODE ($1));
1219 push_labeled_block ($$);
1220 PUSH_LABELED_BLOCK ($$);
1225 label_decl statement
1227 $$ = complete_labeled_statement ($1, $2);
1228 pop_labeled_block ();
1229 POP_LABELED_BLOCK ();
1232 {yyerror ("':' expected"); RECOVER;}
1235 labeled_statement_nsi:
1236 label_decl statement_nsi
1238 $$ = complete_labeled_statement ($1, $2);
1239 pop_labeled_block ();
1240 POP_LABELED_BLOCK ();
1244 /* We concentrate here a bunch of error handling rules that we couldn't write
1245 earlier, because expression_statement catches a missing ';'. */
1246 expression_statement:
1247 statement_expression SC_TK
1249 /* We have a statement. Generate a WFL around it so
1251 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1252 /* We know we have a statement, so set the debug
1253 info to be eventually generate here. */
1254 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1258 if (ctxp->prevent_ese != lineno)
1259 yyerror ("Invalid expression statement");
1260 DRECOVER (expr_stmt);
1264 if (ctxp->prevent_ese != lineno)
1265 yyerror ("Invalid expression statement");
1266 DRECOVER (expr_stmt);
1270 if (ctxp->prevent_ese != lineno)
1271 yyerror ("Invalid expression statement");
1272 DRECOVER (expr_stmt);
1274 | this_or_super OP_TK error
1275 {yyerror ("')' expected"); RECOVER;}
1276 | this_or_super OP_TK CP_TK error
1278 yyerror ("Constructor invocation must be first "
1279 "thing in a constructor");
1282 | this_or_super OP_TK argument_list error
1283 {yyerror ("')' expected"); RECOVER;}
1284 | this_or_super OP_TK argument_list CP_TK error
1286 yyerror ("Constructor invocation must be first "
1287 "thing in a constructor");
1290 | name DOT_TK SUPER_TK error
1291 {yyerror ("'(' expected"); RECOVER;}
1292 | name DOT_TK SUPER_TK OP_TK error
1293 {yyerror ("')' expected"); RECOVER;}
1294 | name DOT_TK SUPER_TK OP_TK argument_list error
1295 {yyerror ("')' expected"); RECOVER;}
1296 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1297 {yyerror ("';' expected"); RECOVER;}
1298 | name DOT_TK SUPER_TK OP_TK CP_TK error
1299 {yyerror ("';' expected"); RECOVER;}
1302 statement_expression:
1304 | pre_increment_expression
1305 | pre_decrement_expression
1306 | post_increment_expression
1307 | post_decrement_expression
1309 | class_instance_creation_expression
1313 IF_TK OP_TK expression CP_TK statement
1314 { $$ = build_if_else_statement ($2.location, $3, $5, NULL_TREE); }
1316 {yyerror ("'(' expected"); RECOVER;}
1318 {yyerror ("Missing term"); RECOVER;}
1319 | IF_TK OP_TK expression error
1320 {yyerror ("')' expected"); RECOVER;}
1323 if_then_else_statement:
1324 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1325 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1328 if_then_else_statement_nsi:
1329 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1330 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1340 /* Make into "proper list" of COMPOUND_EXPRs.
1341 I.e. make the last statment also have its own
1343 maybe_absorb_scoping_blocks ();
1344 TREE_OPERAND ($1, 1) = exit_block ();
1345 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1350 SWITCH_TK OP_TK expression CP_TK
1352 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1353 EXPR_WFL_LINECOL ($$) = $2.location;
1356 {yyerror ("'(' expected"); RECOVER;}
1357 | SWITCH_TK OP_TK error
1358 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1359 | SWITCH_TK OP_TK expression CP_TK error
1360 {yyerror ("'{' expected"); RECOVER;}
1363 /* Default assignment is there to avoid type node on switch_block
1369 | OCB_TK switch_labels CCB_TK
1371 | OCB_TK switch_block_statement_groups CCB_TK
1373 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1377 switch_block_statement_groups:
1378 switch_block_statement_group
1379 | switch_block_statement_groups switch_block_statement_group
1382 switch_block_statement_group:
1383 switch_labels block_statements
1388 | switch_labels switch_label
1392 CASE_TK constant_expression REL_CL_TK
1394 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1395 EXPR_WFL_LINECOL (lab) = $1.location;
1396 java_method_add_stmt (current_function_decl, lab);
1398 | DEFAULT_TK REL_CL_TK
1400 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1401 EXPR_WFL_LINECOL (lab) = $1.location;
1402 java_method_add_stmt (current_function_decl, lab);
1405 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1406 | CASE_TK constant_expression error
1407 {yyerror ("':' expected"); RECOVER;}
1409 {yyerror ("':' expected"); RECOVER;}
1413 WHILE_TK OP_TK expression CP_TK
1415 tree body = build_loop_body ($2.location, $3, 0);
1416 $$ = build_new_loop (body);
1421 while_expression statement
1422 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1424 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1425 | WHILE_TK OP_TK error
1426 {yyerror ("Missing term and ')' expected"); RECOVER;}
1427 | WHILE_TK OP_TK expression error
1428 {yyerror ("')' expected"); RECOVER;}
1431 while_statement_nsi:
1432 while_expression statement_nsi
1433 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1439 tree body = build_loop_body (0, NULL_TREE, 1);
1440 $$ = build_new_loop (body);
1442 /* Need error handing here. FIXME */
1446 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1447 { $$ = complete_loop_body ($4.location, $5, $2, 1); }
1451 for_begin SC_TK expression SC_TK for_update CP_TK statement
1452 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1453 | for_begin SC_TK SC_TK for_update CP_TK statement
1455 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1456 /* We have not condition, so we get rid of the EXIT_EXPR */
1457 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1460 | for_begin SC_TK error
1461 {yyerror ("Invalid control expression"); RECOVER;}
1462 | for_begin SC_TK expression SC_TK error
1463 {yyerror ("Invalid update expression"); RECOVER;}
1464 | for_begin SC_TK SC_TK error
1465 {yyerror ("Invalid update expression"); RECOVER;}
1469 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1470 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1471 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1473 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1474 /* We have not condition, so we get rid of the EXIT_EXPR */
1475 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1483 /* This scope defined for local variable that may be
1484 defined within the scope of the for loop */
1488 {yyerror ("'(' expected"); DRECOVER(for_1);}
1489 | FOR_TK OP_TK error
1490 {yyerror ("Invalid init statement"); RECOVER;}
1496 /* We now declare the loop body. The loop is
1497 declared as a for loop. */
1498 tree body = build_loop_body (0, NULL_TREE, 0);
1499 $$ = build_new_loop (body);
1500 IS_FOR_LOOP_P ($$) = 1;
1501 /* The loop is added to the current block the for
1502 statement is defined within */
1503 java_method_add_stmt (current_function_decl, $$);
1506 for_init: /* Can be empty */
1507 { $$ = empty_stmt_node; }
1508 | statement_expression_list
1510 /* Init statement recorded within the previously
1511 defined block scope */
1512 $$ = java_method_add_stmt (current_function_decl, $1);
1514 | local_variable_declaration
1516 /* Local variable are recorded within the previously
1517 defined block scope */
1520 | statement_expression_list error
1521 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1524 for_update: /* Can be empty */
1525 {$$ = empty_stmt_node;}
1526 | statement_expression_list
1527 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1530 statement_expression_list:
1531 statement_expression
1532 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1533 | statement_expression_list C_TK statement_expression
1534 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1535 | statement_expression_list C_TK error
1536 {yyerror ("Missing term"); RECOVER;}
1541 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1542 | BREAK_TK identifier SC_TK
1543 { $$ = build_bc_statement ($1.location, 1, $2); }
1545 {yyerror ("Missing term"); RECOVER;}
1546 | BREAK_TK identifier error
1547 {yyerror ("';' expected"); RECOVER;}
1552 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1553 | CONTINUE_TK identifier SC_TK
1554 { $$ = build_bc_statement ($1.location, 0, $2); }
1556 {yyerror ("Missing term"); RECOVER;}
1557 | CONTINUE_TK identifier error
1558 {yyerror ("';' expected"); RECOVER;}
1563 { $$ = build_return ($1.location, NULL_TREE); }
1564 | RETURN_TK expression SC_TK
1565 { $$ = build_return ($1.location, $2); }
1567 {yyerror ("Missing term"); RECOVER;}
1568 | RETURN_TK expression error
1569 {yyerror ("';' expected"); RECOVER;}
1573 THROW_TK expression SC_TK
1575 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1576 EXPR_WFL_LINECOL ($$) = $1.location;
1579 {yyerror ("Missing term"); RECOVER;}
1580 | THROW_TK expression error
1581 {yyerror ("';' expected"); RECOVER;}
1584 synchronized_statement:
1585 synchronized OP_TK expression CP_TK block
1587 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1588 EXPR_WFL_LINECOL ($$) =
1589 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1591 | synchronized OP_TK expression CP_TK error
1592 {yyerror ("'{' expected"); RECOVER;}
1593 | synchronized error
1594 {yyerror ("'(' expected"); RECOVER;}
1595 | synchronized OP_TK error CP_TK
1596 {yyerror ("Missing term"); RECOVER;}
1597 | synchronized OP_TK error
1598 {yyerror ("Missing term"); RECOVER;}
1604 if ((1 << $1) != ACC_SYNCHRONIZED)
1605 fatal ("synchronized was '%d' - yyparse", (1 << $1));
1610 TRY_TK block catches
1611 { $$ = build_try_statement ($1.location, $2, $3, NULL_TREE); }
1612 | TRY_TK block finally
1613 { $$ = build_try_statement ($1.location, $2, NULL_TREE, $3); }
1614 | TRY_TK block catches finally
1615 { $$ = build_try_statement ($1.location, $2, $3, $4); }
1617 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1622 | catches catch_clause
1624 TREE_CHAIN ($2) = $1;
1630 catch_clause_parameter block
1632 java_method_add_stmt (current_function_decl, $2);
1637 catch_clause_parameter:
1638 CATCH_TK OP_TK formal_parameter CP_TK
1640 /* We add a block to define a scope for
1641 formal_parameter (CCBP). The formal parameter is
1642 declared initialized by the appropriate function
1644 tree ccpb = enter_block ();
1645 tree init = build_assignment (ASSIGN_TK, $2.location,
1647 soft_exceptioninfo_call_node);
1648 declare_local_variables (0, TREE_VALUE ($3),
1649 build_tree_list (TREE_PURPOSE ($3),
1651 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1652 EXPR_WFL_LINECOL ($$) = $1.location;
1655 {yyerror ("'(' expected"); RECOVER;}
1656 | CATCH_TK OP_TK error
1657 {yyerror ("Missing term or ')' expected"); DRECOVER (2);}
1658 | CATCH_TK OP_TK error CP_TK /* That's for () */
1659 {yyerror ("')' expected"); DRECOVER (1);}
1665 $$ = build (FINALLY_EXPR, NULL_TREE,
1666 create_label_decl (generate_name ()), $2);
1669 {yyerror ("'{' expected"); RECOVER; }
1672 /* 19.12 Production from 15: Expressions */
1674 primary_no_new_array
1675 | array_creation_expression
1678 primary_no_new_array:
1681 { $$ = build_this ($1.location); }
1682 | OP_TK expression CP_TK
1684 | class_instance_creation_expression
1688 /* type DOT_TK CLASS_TK doens't work. So we split the rule
1689 'type' into its components. Missing is something for array,
1690 which will complete the reference_type part. FIXME */
1691 | name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1692 { $$ = parse_jdk1_1_error ("named class literals"); }
1693 | primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1694 { $$ = build_class_ref ($1); }
1695 | VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1696 { $$ = build_class_ref (void_type_node); }
1697 /* Added, JDK1.1 inner classes. Documentation is wrong
1698 refering to a 'ClassName' (class_name) rule that doesn't
1699 exist. Used name instead. */
1700 | name DOT_TK THIS_TK
1701 { $$ = parse_jdk1_1_error ("class literals"); }
1702 | OP_TK expression error
1703 {yyerror ("')' expected"); RECOVER;}
1705 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1706 | primitive_type DOT_TK error
1707 {yyerror ("'class' expected" ); RECOVER;}
1708 | VOID_TK DOT_TK error
1709 {yyerror ("'class' expected" ); RECOVER;}
1712 class_instance_creation_expression:
1713 NEW_TK class_type OP_TK argument_list CP_TK
1714 { $$ = build_new_invocation ($2, $4); }
1715 | NEW_TK class_type OP_TK CP_TK
1716 { $$ = build_new_invocation ($2, NULL_TREE); }
1717 /* Added, JDK1.1 inner classes but modified to use
1718 'class_type' instead of 'TypeName' (type_name) mentionned
1719 in the documentation but doesn't exist. */
1720 | NEW_TK class_type OP_TK argument_list CP_TK class_body
1721 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1722 | NEW_TK class_type OP_TK CP_TK class_body
1723 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1724 /* Added, JDK1.1 inner classes, modified to use name or
1725 primary instead of primary solely which couldn't work in
1727 | something_dot_new identifier OP_TK CP_TK
1728 | something_dot_new identifier OP_TK CP_TK class_body
1729 | something_dot_new identifier OP_TK argument_list CP_TK
1730 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1731 | NEW_TK error SC_TK
1732 {yyerror ("'(' expected"); DRECOVER(new_1);}
1733 | NEW_TK class_type error
1734 {yyerror ("'(' expected"); RECOVER;}
1735 | NEW_TK class_type OP_TK error
1736 {yyerror ("')' or term expected"); RECOVER;}
1737 | NEW_TK class_type OP_TK argument_list error
1738 {yyerror ("')' expected"); RECOVER;}
1739 | something_dot_new error
1740 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1741 | something_dot_new identifier error
1742 {yyerror ("'(' expected"); RECOVER;}
1745 something_dot_new: /* Added, not part of the specs. */
1747 | primary DOT_TK NEW_TK
1753 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
1754 ctxp->formal_parameter_number = 1;
1756 | argument_list C_TK expression
1758 ctxp->formal_parameter_number += 1;
1759 $$ = tree_cons (NULL_TREE, $3, $1);
1761 | argument_list C_TK error
1762 {yyerror ("Missing term"); RECOVER;}
1765 array_creation_expression:
1766 NEW_TK primitive_type dim_exprs
1767 { $$ = build_newarray_node ($2, $3, 0); }
1768 | NEW_TK class_or_interface_type dim_exprs
1769 { $$ = build_newarray_node ($2, $3, 0); }
1770 | NEW_TK primitive_type dim_exprs dims
1771 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
1772 | NEW_TK class_or_interface_type dim_exprs dims
1773 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
1774 /* Added, JDK1.1 anonymous array. Initial documentation rule
1776 | NEW_TK class_or_interface_type dims array_initializer
1777 { $$ = parse_jdk1_1_error ("anonymous array"); }
1778 | NEW_TK primitive_type dims array_initializer
1779 { $$ = parse_jdk1_1_error ("anonymous array"); }
1780 | NEW_TK error CSB_TK
1781 {yyerror ("'[' expected"); DRECOVER ("]");}
1782 | NEW_TK error OSB_TK
1783 {yyerror ("']' expected"); RECOVER;}
1788 { $$ = build_tree_list (NULL_TREE, $1); }
1789 | dim_exprs dim_expr
1790 { $$ = tree_cons (NULL_TREE, $2, $$); }
1794 OSB_TK expression CSB_TK
1796 EXPR_WFL_LINECOL ($2) = $1.location;
1799 | OSB_TK expression error
1800 {yyerror ("']' expected"); RECOVER;}
1803 yyerror ("Missing term");
1804 yyerror ("']' expected");
1813 /* If not initialized, allocate memory for the osb
1815 if (!ctxp->osb_limit)
1817 allocate = ctxp->osb_limit = 32;
1818 ctxp->osb_depth = -1;
1820 /* If capacity overflown, reallocate a bigger chuck */
1821 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
1822 allocate = ctxp->osb_limit << 1;
1826 allocate *= sizeof (int);
1827 if (ctxp->osb_number)
1828 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
1831 ctxp->osb_number = (int *)xmalloc (allocate);
1834 CURRENT_OSB (ctxp) = 1;
1836 | dims OSB_TK CSB_TK
1837 { CURRENT_OSB (ctxp)++; }
1839 { yyerror ("']' expected"); RECOVER;}
1843 primary DOT_TK identifier
1844 { $$ = make_qualified_primary ($1, $3, $2.location); }
1845 /* FIXME - REWRITE TO:
1846 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
1847 | SUPER_TK DOT_TK identifier
1850 build_wfl_node (super_identifier_node,
1851 input_filename, 0, 0);
1852 EXPR_WFL_LINECOL (super_wfl) = $1.location;
1853 $$ = make_qualified_name (super_wfl, $3, $2.location);
1856 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
1861 { $$ = build_method_invocation ($1, NULL_TREE); }
1862 | name OP_TK argument_list CP_TK
1863 { $$ = build_method_invocation ($1, $3); }
1864 | primary DOT_TK identifier OP_TK CP_TK
1866 if (TREE_CODE ($1) == THIS_EXPR)
1867 $$ = build_this_super_qualified_invocation
1868 (1, $3, NULL_TREE, 0, $2.location);
1871 tree invok = build_method_invocation ($3, NULL_TREE);
1872 $$ = make_qualified_primary ($1, invok, $2.location);
1875 | primary DOT_TK identifier OP_TK argument_list CP_TK
1877 if (TREE_CODE ($1) == THIS_EXPR)
1878 $$ = build_this_super_qualified_invocation
1879 (1, $3, $5, 0, $2.location);
1882 tree invok = build_method_invocation ($3, $5);
1883 $$ = make_qualified_primary ($1, invok, $2.location);
1886 | SUPER_TK DOT_TK identifier OP_TK CP_TK
1888 $$ = build_this_super_qualified_invocation
1889 (0, $3, NULL_TREE, $1.location, $2.location);
1891 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
1893 $$ = build_this_super_qualified_invocation
1894 (0, $3, $5, $1.location, $2.location);
1896 /* Screws up thing. I let it here until I'm convinced it can
1898 | primary DOT_TK error
1899 {yyerror ("'(' expected"); DRECOVER(bad);} */
1900 | SUPER_TK DOT_TK error CP_TK
1901 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1902 | SUPER_TK DOT_TK error DOT_TK
1903 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1907 name OSB_TK expression CSB_TK
1908 { $$ = build_array_ref ($2.location, $1, $3); }
1909 | primary_no_new_array OSB_TK expression CSB_TK
1910 { $$ = build_array_ref ($2.location, $1, $3); }
1913 yyerror ("Missing term and ']' expected");
1914 DRECOVER(array_access);
1916 | name OSB_TK expression error
1918 yyerror ("']' expected");
1919 DRECOVER(array_access);
1921 | primary_no_new_array OSB_TK error
1923 yyerror ("Missing term and ']' expected");
1924 DRECOVER(array_access);
1926 | primary_no_new_array OSB_TK expression error
1928 yyerror ("']' expected");
1929 DRECOVER(array_access);
1936 | post_increment_expression
1937 | post_decrement_expression
1940 post_increment_expression:
1941 postfix_expression INCR_TK
1942 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1945 post_decrement_expression:
1946 postfix_expression DECR_TK
1947 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1951 pre_increment_expression
1952 | pre_decrement_expression
1953 | PLUS_TK unary_expression
1954 {$$ = build_unaryop ($1.token, $1.location, $2); }
1955 | MINUS_TK unary_expression
1956 {$$ = build_unaryop ($1.token, $1.location, $2); }
1957 | unary_expression_not_plus_minus
1959 {yyerror ("Missing term"); RECOVER}
1961 {yyerror ("Missing term"); RECOVER}
1964 pre_increment_expression:
1965 INCR_TK unary_expression
1966 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1968 {yyerror ("Missing term"); RECOVER}
1971 pre_decrement_expression:
1972 DECR_TK unary_expression
1973 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1975 {yyerror ("Missing term"); RECOVER}
1978 unary_expression_not_plus_minus:
1980 | NOT_TK unary_expression
1981 {$$ = build_unaryop ($1.token, $1.location, $2); }
1982 | NEG_TK unary_expression
1983 {$$ = build_unaryop ($1.token, $1.location, $2); }
1986 {yyerror ("Missing term"); RECOVER}
1988 {yyerror ("Missing term"); RECOVER}
1991 cast_expression: /* Error handling here is potentially weak */
1992 OP_TK primitive_type dims CP_TK unary_expression
1995 while (CURRENT_OSB (ctxp)--)
1996 type = build_java_array_type (type, -1);
1998 $$ = build_cast ($1.location, type, $5);
2000 | OP_TK primitive_type CP_TK unary_expression
2001 { $$ = build_cast ($1.location, $2, $4); }
2002 | OP_TK expression CP_TK unary_expression_not_plus_minus
2003 { $$ = build_cast ($1.location, $2, $4); }
2004 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2007 while (CURRENT_OSB (ctxp)--)
2008 obstack_1grow (&temporary_obstack, '[');
2010 obstack_grow0 (&temporary_obstack,
2011 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2012 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2013 ptr = obstack_finish (&temporary_obstack);
2014 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2015 $$ = build_cast ($1.location, $2, $5);
2017 | OP_TK primitive_type OSB_TK error
2018 {yyerror ("']' expected, invalid type expression");}
2021 if (ctxp->prevent_ese != lineno)
2022 yyerror ("Invalid type expression"); RECOVER;
2025 | OP_TK primitive_type dims CP_TK error
2026 {yyerror ("Missing term"); RECOVER;}
2027 | OP_TK primitive_type CP_TK error
2028 {yyerror ("Missing term"); RECOVER;}
2029 | OP_TK name dims CP_TK error
2030 {yyerror ("Missing term"); RECOVER;}
2033 multiplicative_expression:
2035 | multiplicative_expression MULT_TK unary_expression
2037 $$ = build_binop (BINOP_LOOKUP ($2.token),
2038 $2.location, $1, $3);
2040 | multiplicative_expression DIV_TK unary_expression
2042 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2045 | multiplicative_expression REM_TK unary_expression
2047 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2050 | multiplicative_expression MULT_TK error
2051 {yyerror ("Missing term"); RECOVER;}
2052 | multiplicative_expression DIV_TK error
2053 {yyerror ("Missing term"); RECOVER;}
2054 | multiplicative_expression REM_TK error
2055 {yyerror ("Missing term"); RECOVER;}
2058 additive_expression:
2059 multiplicative_expression
2060 | additive_expression PLUS_TK multiplicative_expression
2062 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2065 | additive_expression MINUS_TK multiplicative_expression
2067 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2070 | additive_expression PLUS_TK error
2071 {yyerror ("Missing term"); RECOVER;}
2072 | additive_expression MINUS_TK error
2073 {yyerror ("Missing term"); RECOVER;}
2078 | shift_expression LS_TK additive_expression
2080 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2083 | shift_expression SRS_TK additive_expression
2085 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2088 | shift_expression ZRS_TK additive_expression
2090 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2093 | shift_expression LS_TK error
2094 {yyerror ("Missing term"); RECOVER;}
2095 | shift_expression SRS_TK error
2096 {yyerror ("Missing term"); RECOVER;}
2097 | shift_expression ZRS_TK error
2098 {yyerror ("Missing term"); RECOVER;}
2101 relational_expression:
2103 | relational_expression LT_TK shift_expression
2105 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2108 | relational_expression GT_TK shift_expression
2110 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2113 | relational_expression LTE_TK shift_expression
2115 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2118 | relational_expression GTE_TK shift_expression
2120 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2123 | relational_expression INSTANCEOF_TK reference_type
2124 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2125 | relational_expression LT_TK error
2126 {yyerror ("Missing term"); RECOVER;}
2127 | relational_expression GT_TK error
2128 {yyerror ("Missing term"); RECOVER;}
2129 | relational_expression LTE_TK error
2130 {yyerror ("Missing term"); RECOVER;}
2131 | relational_expression GTE_TK error
2132 {yyerror ("Missing term"); RECOVER;}
2133 | relational_expression INSTANCEOF_TK error
2134 {yyerror ("Invalid reference type"); RECOVER;}
2137 equality_expression:
2138 relational_expression
2139 | equality_expression EQ_TK relational_expression
2141 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2144 | equality_expression NEQ_TK relational_expression
2146 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2149 | equality_expression EQ_TK error
2150 {yyerror ("Missing term"); RECOVER;}
2151 | equality_expression NEQ_TK error
2152 {yyerror ("Missing term"); RECOVER;}
2157 | and_expression AND_TK equality_expression
2159 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2162 | and_expression AND_TK error
2163 {yyerror ("Missing term"); RECOVER;}
2166 exclusive_or_expression:
2168 | exclusive_or_expression XOR_TK and_expression
2170 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2173 | exclusive_or_expression XOR_TK error
2174 {yyerror ("Missing term"); RECOVER;}
2177 inclusive_or_expression:
2178 exclusive_or_expression
2179 | inclusive_or_expression OR_TK exclusive_or_expression
2181 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2184 | inclusive_or_expression OR_TK error
2185 {yyerror ("Missing term"); RECOVER;}
2188 conditional_and_expression:
2189 inclusive_or_expression
2190 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2192 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2195 | conditional_and_expression BOOL_AND_TK error
2196 {yyerror ("Missing term"); RECOVER;}
2199 conditional_or_expression:
2200 conditional_and_expression
2201 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2203 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2206 | conditional_or_expression BOOL_OR_TK error
2207 {yyerror ("Missing term"); RECOVER;}
2210 conditional_expression: /* Error handling here is weak */
2211 conditional_or_expression
2212 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2214 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2215 EXPR_WFL_LINECOL ($$) = $2.location;
2217 | conditional_or_expression REL_QM_TK REL_CL_TK error
2220 yyerror ("Missing term");
2223 | conditional_or_expression REL_QM_TK error
2224 {yyerror ("Missing term"); DRECOVER (2);}
2225 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2226 {yyerror ("Missing term"); DRECOVER (3);}
2229 assignment_expression:
2230 conditional_expression
2235 left_hand_side assignment_operator assignment_expression
2236 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2237 | left_hand_side assignment_operator error
2239 if (ctxp->prevent_ese != lineno)
2240 yyerror ("Missing term");
2251 assignment_operator:
2257 assignment_expression
2260 constant_expression:
2269 /* Flag for the error report routine to issue the error the first time
2270 it's called (overriding the default behavior which is to drop the
2271 first invocation and honor the second one, taking advantage of a
2273 static int force_error = 0;
2275 /* Create a new parser context and make it the current one. */
2278 java_push_parser_context ()
2280 struct parser_ctxt *new =
2281 (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2283 bzero (new, sizeof (struct parser_ctxt));
2288 ctxp->incomplete_class = ctxp->next->incomplete_class;
2289 ctxp->gclass_list = ctxp->next->gclass_list;
2293 /* If the first file of a file list was a class file, no context
2294 exists for a source file to be parsed. This boolean remembers that
2295 java_parser_context_save_global might have created a dummy one, so
2296 that java_parser_context_restore_global can pop it. */
2297 static int extra_ctxp_pushed_p = 0;
2300 java_parser_context_save_global ()
2304 java_push_parser_context ();
2305 extra_ctxp_pushed_p = 1;
2307 ctxp->finput = finput;
2308 ctxp->lineno = lineno;
2309 ctxp->current_class = current_class;
2310 ctxp->filename = input_filename;
2311 ctxp->current_function_decl = current_function_decl;
2315 java_parser_context_restore_global ()
2317 finput = ctxp->finput;
2318 lineno = ctxp->lineno;
2319 current_class = ctxp->current_class;
2320 input_filename = ctxp->filename;
2321 current_function_decl = ctxp->current_function_decl;
2322 if (!ctxp->next && extra_ctxp_pushed_p)
2324 java_pop_parser_context (0);
2325 extra_ctxp_pushed_p = 0;
2330 java_pop_parser_context (generate)
2334 struct parser_ctxt *toFree, *next;
2343 next->incomplete_class = ctxp->incomplete_class;
2344 next->gclass_list = ctxp->gclass_list;
2345 lineno = ctxp->lineno;
2346 finput = ctxp->finput;
2347 current_class = ctxp->current_class;
2350 /* Set the single import class file flag to 0 for the current list
2351 of imported things */
2352 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2353 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2355 /* And restore those of the previous context */
2356 if ((ctxp = next)) /* Assignment is really meant here */
2357 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2358 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2362 toFree->next = ctxp_for_generation;
2363 ctxp_for_generation = toFree;
2369 /* Reporting JDK1.1 features not implemented */
2372 parse_jdk1_1_error (msg)
2375 sorry (": `%s' JDK1.1(TM) feature", msg);
2377 return empty_stmt_node;
2380 static int do_warning = 0;
2387 static int prev_lineno;
2388 static char *prev_msg;
2391 char *remainder, *code_from_source;
2392 extern struct obstack temporary_obstack;
2394 if (!force_error && prev_lineno == lineno)
2397 /* Save current error location but report latter, when the context is
2399 if (ctxp->java_error_flag == 0)
2401 ctxp->java_error_flag = 1;
2403 /* Do something to use the previous line if we're reaching the
2404 end of the file... */
2405 #ifdef VERBOSE_SKELETON
2406 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2411 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2412 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2415 ctxp->java_error_flag = 0;
2417 java_warning_count++;
2421 if (elc.col == 0 && msg[1] == ';')
2423 elc.col = ctxp->p_line->char_col-1;
2424 elc.line = ctxp->p_line->lineno;
2427 save_lineno = lineno;
2428 prev_lineno = lineno = elc.line;
2431 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2432 obstack_grow0 (&temporary_obstack,
2433 code_from_source, strlen (code_from_source));
2434 remainder = obstack_finish (&temporary_obstack);
2436 warning ("%s.\n%s", msg, remainder);
2438 error ("%s.\n%s", msg, remainder);
2440 /* This allow us to cheaply avoid an extra 'Invalid expression
2441 statement' error report when errors have been already reported on
2442 the same line. This occurs when we report an error but don't have
2443 a synchronization point other than ';', which
2444 expression_statement is the only one to take care of. */
2445 ctxp->prevent_ese = lineno = save_lineno;
2449 issue_warning_error_from_context (cl, msg, ap)
2454 char *saved, *saved_input_filename;
2456 vsprintf (buffer, msg, ap);
2459 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2460 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2461 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
2463 /* We have a CL, that's a good reason for using it if it contains data */
2464 saved = ctxp->filename;
2465 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2466 ctxp->filename = EXPR_WFL_FILENAME (cl);
2467 saved_input_filename = input_filename;
2468 input_filename = ctxp->filename;
2470 java_error (buffer);
2471 ctxp->filename = saved;
2472 input_filename = saved_input_filename;
2476 /* Issue an error message at a current source line CL */
2479 parse_error_context VPROTO ((tree cl, const char *msg, ...))
2481 #ifndef ANSI_PROTOTYPES
2488 #ifndef ANSI_PROTOTYPES
2489 cl = va_arg (ap, tree);
2490 msg = va_arg (ap, const char *);
2492 issue_warning_error_from_context (cl, msg, ap);
2496 /* Issue a warning at a current source line CL */
2499 parse_warning_context VPROTO ((tree cl, const char *msg, ...))
2501 #ifndef ANSI_PROTOTYPES
2508 #ifndef ANSI_PROTOTYPES
2509 cl = va_arg (ap, tree);
2510 msg = va_arg (ap, const char *);
2513 force_error = do_warning = 1;
2514 issue_warning_error_from_context (cl, msg, ap);
2515 do_warning = force_error = 0;
2520 find_expr_with_wfl (node)
2528 switch (TREE_CODE (node))
2531 return find_expr_with_wfl (BLOCK_EXPR_BODY (node));
2534 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
2537 to_return = find_expr_with_wfl (TREE_OPERAND (node, 1));
2541 return find_expr_with_wfl (TREE_OPERAND (node, 0));
2543 case LABELED_BLOCK_EXPR:
2544 return find_expr_with_wfl (TREE_OPERAND (node, 1));
2546 code = TREE_CODE_CLASS (TREE_CODE (node));
2547 if (((code == '1') || (code == '2') || (code == 'e'))
2548 && EXPR_WFL_LINECOL (node))
2556 /* Issue a missing return statement error. Uses METHOD to figure the
2557 last line of the method the error occurs in. */
2560 missing_return_error (method)
2563 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
2564 parse_error_context (wfl_operator, "Missing return statement");
2567 /* Issue an unreachable statement error. From NODE, find the next
2568 statement to report appropriately. */
2570 unreachable_stmt_error (node)
2573 /* Browse node to find the next expression node that has a WFL. Use
2574 the location to report the error */
2575 if (TREE_CODE (node) == COMPOUND_EXPR)
2576 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
2578 node = find_expr_with_wfl (node);
2582 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
2583 parse_error_context (wfl_operator, "Unreachable statement");
2586 fatal ("Can't get valid statement - unreachable_stmt_error");
2590 java_report_errors ()
2592 if (java_error_count)
2593 fprintf (stderr, "%d error%s",
2594 java_error_count, (java_error_count == 1 ? "" : "s"));
2595 if (java_warning_count)
2596 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
2597 java_warning_count, (java_warning_count == 1 ? "" : "s"));
2598 if (java_error_count || java_warning_count)
2599 putc ('\n', stderr);
2600 return java_error_count;
2604 java_accstring_lookup (flags)
2607 static char buffer [80];
2608 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
2610 /* Access modifier looked-up first for easier report on forbidden
2612 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
2613 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
2614 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
2615 if (flags & ACC_STATIC) COPY_RETURN ("static");
2616 if (flags & ACC_FINAL) COPY_RETURN ("final");
2617 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
2618 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
2619 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
2620 if (flags & ACC_NATIVE) COPY_RETURN ("native");
2621 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
2622 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
2629 /* Issuing error messages upon redefinition of classes, interfaces or
2633 classitf_redefinition_error (context, id, decl, cl)
2637 parse_error_context (cl, "%s `%s' already defined in %s:%d",
2638 context, IDENTIFIER_POINTER (id),
2639 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
2640 /* Here we should point out where its redefined. It's a unicode. FIXME */
2644 variable_redefinition_error (context, name, type, line)
2645 tree context, name, type;
2650 /* Figure a proper name for type. We might haven't resolved it */
2651 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
2652 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
2654 type_name = lang_printable_name (type, 0);
2656 parse_error_context (context,
2657 "Variable `%s' is already defined in this method and "
2658 "was declared `%s %s' at line %d",
2659 IDENTIFIER_POINTER (name),
2660 type_name, IDENTIFIER_POINTER (name), line);
2664 build_array_from_name (type, type_wfl, name, ret_name)
2665 tree type, type_wfl, name, *ret_name;
2670 /* Eventually get more dims */
2671 string = IDENTIFIER_POINTER (name);
2672 while (string [more_dims] == '[')
2675 /* If we have, then craft a new type for this variable */
2678 name = get_identifier (&more_dims [string]);
2680 /* If type already is a reference on an array, get the base type */
2681 if ((TREE_CODE (type) == POINTER_TYPE) &&
2682 TYPE_ARRAY_P (TREE_TYPE (type)))
2683 type = TREE_TYPE (type);
2685 /* Building the first dimension of a primitive type uses this
2687 if (JPRIMITIVE_TYPE_P (type))
2689 type = build_java_array_type (type, -1);
2690 CLASS_LOADED_P (type) = 1;
2693 /* Otherwise, if we have a WFL for this type, use it (the type
2694 is already an array on an unresolved type, and we just keep
2695 on adding dimensions) */
2699 /* Add all the dimensions */
2701 type = build_unresolved_array_type (type);
2703 /* The type may have been incomplete in the first place */
2705 type = obtain_incomplete_type (type);
2712 /* Build something that the type identifier resolver will identify as
2713 being an array to an unresolved type. TYPE_WFL is a WFL on a
2717 build_unresolved_array_type (type_or_wfl)
2722 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
2723 just create a array type */
2724 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
2726 tree type = build_java_array_type (type_or_wfl, -1);
2727 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
2731 obstack_1grow (&temporary_obstack, '[');
2732 obstack_grow0 (&temporary_obstack,
2733 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
2734 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
2735 ptr = obstack_finish (&temporary_obstack);
2736 return build_expr_wfl (get_identifier (ptr),
2737 EXPR_WFL_FILENAME (type_or_wfl),
2738 EXPR_WFL_LINENO (type_or_wfl),
2739 EXPR_WFL_COLNO (type_or_wfl));
2742 /* Check modifiers. If one doesn't fit, retrieve it in its declaration line
2743 and point it out. */
2746 check_modifiers (message, value, mask)
2751 /* Should point out the one that don't fit. ASCII/unicode,
2752 going backward. FIXME */
2755 int i, remainder = value & ~mask;
2756 for (i = 0; i <= 10; i++)
2757 if ((1 << i) & remainder)
2758 parse_error_context (ctxp->modifier_ctx [i], message,
2759 java_accstring_lookup (1 << i));
2764 parser_add_interface (class_decl, interface_decl, wfl)
2765 tree class_decl, interface_decl, wfl;
2767 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
2768 parse_error_context (wfl, "Interface `%s' repeated",
2769 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
2772 /* Bulk of common class/interface checks. Return 1 if an error was
2773 encountered. TAG is 0 for a class, 1 for an interface. */
2776 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
2777 int is_interface, flags;
2778 tree raw_name, qualified_name, decl, cl;
2783 fprintf (stderr, " %s %s", (is_interface ? "interface" : "class"),
2784 IDENTIFIER_POINTER (qualified_name));
2786 /* Scope of an interface/class type name:
2787 - Can't be imported by a single type import
2788 - Can't already exists in the package */
2789 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
2790 && (node = find_name_in_single_imports (raw_name)))
2793 (cl, "%s name `%s' clashes with imported type `%s'",
2794 (is_interface ? "Interface" : "Class"),
2795 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
2798 if (decl && CLASS_COMPLETE_P (decl))
2800 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
2801 qualified_name, decl, cl);
2805 /* If public, file name should match class/interface name */
2806 if (flags & ACC_PUBLIC)
2810 /* Contains OS dependent assumption on path separator. FIXME */
2811 for (f = &input_filename [strlen (input_filename)];
2812 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
2815 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
2817 if (strncmp (IDENTIFIER_POINTER (raw_name),
2818 f , IDENTIFIER_LENGTH (raw_name)) ||
2819 f [IDENTIFIER_LENGTH (raw_name)] != '.')
2820 parse_error_context (cl, "Public %s `%s' must be defined in a file "
2822 (is_interface ? "interface" : "class"),
2823 IDENTIFIER_POINTER (qualified_name),
2824 IDENTIFIER_POINTER (raw_name));
2827 check_modifiers ((is_interface ?
2828 "Illegal modifier `%s' for interface declaration" :
2829 "Illegal modifier `%s' for class declaration"), flags,
2830 (is_interface ? INTERFACE_MODIFIERS : CLASS_MODIFIERS));
2834 /* If DECL is NULL, create and push a new DECL, record the current
2835 line CL and do other maintenance things. */
2838 maybe_create_class_interface_decl (decl, qualified_name, cl)
2839 tree decl, qualified_name, cl;
2842 decl = push_class (make_class (), qualified_name);
2844 /* Take care of the file and line business */
2845 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
2846 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
2847 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
2848 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
2849 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
2851 ctxp->current_parsed_class = decl;
2853 /* Link the declaration to the already seen ones */
2854 TREE_CHAIN (decl) = ctxp->class_list;
2855 ctxp->class_list = decl;
2857 /* Create a new nodes in the global lists */
2858 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
2859 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
2861 /* Install a new dependency list element */
2862 create_jdep_list (ctxp);
2864 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
2865 IDENTIFIER_POINTER (qualified_name)));
2870 add_superinterfaces (decl, interface_list)
2871 tree decl, interface_list;
2874 /* Superinterface(s): if present and defined, parser_check_super_interface ()
2875 takes care of ensuring that:
2876 - This is an accessible interface type,
2877 - Circularity detection.
2878 parser_add_interface is then called. If present but not defined,
2879 the check operation is delayed until the super interface gets
2881 for (node = interface_list; node; node = TREE_CHAIN (node))
2883 tree current = TREE_PURPOSE (node);
2884 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
2885 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
2887 if (!parser_check_super_interface (idecl, decl, current))
2888 parser_add_interface (decl, idecl, current);
2891 register_incomplete_type (JDEP_INTERFACE,
2892 current, decl, NULL_TREE);
2896 /* Create an interface in pass1 and return its decl. Return the
2897 interface's decl in pass 2. */
2900 create_interface (flags, id, super)
2904 tree raw_name = EXPR_WFL_NODE (id);
2905 tree q_name = parser_qualified_classname (id);
2906 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
2908 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
2910 /* Basic checks: scope, redefinition, modifiers */
2911 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
2914 /* Interface modifiers check
2915 - public/abstract allowed (already done at that point)
2916 - abstract is obsolete (comes first, it's a warning, or should be)
2917 - Can't use twice the same (checked in the modifier rule) */
2918 if ((flags & ACC_ABSTRACT) && flag_redundant)
2919 parse_warning_context
2920 (MODIFIER_WFL (ABSTRACT_TK),
2921 "Redundant use of `abstract' modifier. Interface `%s' is implicitely "
2922 "abstract", IDENTIFIER_POINTER (raw_name));
2924 /* Create a new decl if DECL is NULL, otherwise fix it */
2925 decl = maybe_create_class_interface_decl (decl, q_name, id);
2927 /* Set super info and mark the class a complete */
2928 set_super_info (ACC_ABSTRACT | ACC_INTERFACE | flags, TREE_TYPE (decl),
2929 object_type_node, ctxp->interface_number);
2930 ctxp->interface_number = 0;
2931 CLASS_COMPLETE_P (decl) = 1;
2932 add_superinterfaces (decl, super);
2937 /* Create an class in pass1 and return its decl. Return class
2938 interface's decl in pass 2. */
2941 create_class (flags, id, super, interfaces)
2943 tree id, super, interfaces;
2945 tree raw_name = EXPR_WFL_NODE (id);
2946 tree class_id, decl;
2947 tree super_decl = NULL, super_decl_type;
2949 class_id = parser_qualified_classname (id);
2950 decl = IDENTIFIER_CLASS_VALUE (class_id);
2951 ctxp->current_parsed_class_un = EXPR_WFL_NODE (id);
2952 EXPR_WFL_NODE (id) = class_id;
2954 /* Basic check: scope, redefinition, modifiers */
2955 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
2958 /* Class modifier check:
2959 - Allowed modifier (already done at that point)
2960 - abstract AND final forbidden
2961 - Public classes defined in the correct file */
2962 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
2963 parse_error_context (id, "Class `%s' can't be declared both abstract "
2964 "and final", IDENTIFIER_POINTER (raw_name));
2966 /* Create a new decl if DECL is NULL, otherwise fix it */
2967 decl = maybe_create_class_interface_decl (decl, class_id, id);
2969 /* If SUPER exists, use it, otherwise use Object */
2972 /* Can't extend java.lang.Object */
2973 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
2975 parse_error_context (id, "Can't extend `java.lang.Object'");
2979 /* The class is known and exists if there is a decl. Otherwise,
2980 postpone the operation and do it later. */
2981 super_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (super));
2984 parser_check_super (super_decl, decl, id);
2985 super_decl_type = TREE_TYPE (super_decl);
2989 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
2991 else if (TREE_TYPE (decl) != object_type_node)
2992 super_decl_type = object_type_node;
2993 /* We're defining java.lang.Object */
2995 super_decl_type = NULL_TREE;
2997 /* Set super info and mark the class a complete */
2998 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
2999 ctxp->interface_number);
3000 ctxp->interface_number = 0;
3001 CLASS_COMPLETE_P (decl) = 1;
3002 add_superinterfaces (decl, interfaces);
3004 /* Eventually sets the @deprecated tag flag */
3005 CHECK_DEPRECATED (decl);
3010 /* Can't use lookup_field () since we don't want to load the class and
3011 can't set the CLASS_LOADED_P flag */
3014 find_field (class, name)
3019 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3021 if (DECL_NAME (decl) == name)
3027 /* Wrap around lookup_field that doesn't potentially upset the value
3031 lookup_field_wrapper (class, name)
3036 java_parser_context_save_global ();
3037 decl = lookup_field (&type, name);
3038 java_parser_context_restore_global ();
3042 /* Find duplicate field within the same class declarations and report
3043 the error. Returns 1 if a duplicated field was found, 0
3047 duplicate_declaration_error_p (new_field_name, new_type, cl)
3048 tree new_field_name, new_type, cl;
3050 /* This might be modified to work with method decl as well */
3051 tree decl = find_field (TREE_TYPE (ctxp->current_parsed_class),
3055 char *t1 = strdup (purify_type_name
3056 ((TREE_CODE (new_type) == POINTER_TYPE
3057 && TREE_TYPE (new_type) == NULL_TREE) ?
3058 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
3059 lang_printable_name (new_type, 1)));
3060 /* The type may not have been completed by the time we report
3062 char *t2 = strdup (purify_type_name
3063 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
3064 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
3065 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
3066 lang_printable_name (TREE_TYPE (decl), 1)));
3068 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
3069 t1, IDENTIFIER_POINTER (new_field_name),
3070 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
3071 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3079 /* Field registration routine. If TYPE doesn't exist, field
3080 declarations are linked to the undefined TYPE dependency list, to
3081 be later resolved in java_complete_class () */
3084 register_fields (flags, type, variable_list)
3086 tree type, variable_list;
3088 tree current, saved_type;
3089 tree class_type = TREE_TYPE (ctxp->current_parsed_class);
3090 int saved_lineno = lineno;
3092 tree wfl = NULL_TREE;
3094 /* If we're adding fields to interfaces, those fields are public,
3096 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
3098 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
3100 "%s", "interface field(s)");
3101 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
3103 "%s", "interface field(s)");
3104 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
3105 flags, ACC_FINAL, "%s", "interface field(s)");
3106 check_modifiers ("Illegal interface member modifier `%s'", flags,
3107 INTERFACE_FIELD_MODIFIERS);
3108 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
3111 /* Obtain a suitable type for resolution, if necessary */
3112 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
3114 /* If TYPE is fully resolved and we don't have a reference, make one */
3115 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3117 for (current = variable_list, saved_type = type; current;
3118 current = TREE_CHAIN (current), type = saved_type)
3122 tree cl = TREE_PURPOSE (current);
3123 tree init = TREE_VALUE (current);
3124 tree current_name = EXPR_WFL_NODE (cl);
3126 /* Process NAME, as it may specify extra dimension(s) for it */
3127 type = build_array_from_name (type, wfl, current_name, ¤t_name);
3129 /* Type adjustment. We may have just readjusted TYPE because
3130 the variable specified more dimensions. Make sure we have
3131 a reference if we can and don't have one already. Also
3132 change the name if we have an init. */
3133 if (type != saved_type)
3135 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3137 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
3140 real_type = GET_REAL_TYPE (type);
3141 /* Check for redeclarations */
3142 if (duplicate_declaration_error_p (current_name, real_type, cl))
3145 /* Set lineno to the line the field was found and create a
3146 declaration for it. Eventually sets the @deprecated tag flag. */
3147 lineno = EXPR_WFL_LINENO (cl);
3148 field_decl = add_field (class_type, current_name, real_type, flags);
3149 CHECK_DEPRECATED (field_decl);
3151 /* Check if we must chain. */
3153 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
3155 /* Default value of a static field is 0 and it is considered
3157 if (flags & ACC_STATIC)
3158 INITIALIZED_P (field_decl) = 1;
3160 /* If we have an initialization value tied to the field */
3163 /* The field is declared static */
3164 if (flags & ACC_STATIC)
3166 /* We include the field and its initialization part into
3167 a list used to generate <clinit>. After <clinit> is
3168 walked, field initializations will be processed and
3169 fields initialized with known constants will be taken
3170 out of <clinit> and have their DECL_INITIAL set
3172 TREE_CHAIN (init) = ctxp->static_initialized;
3173 ctxp->static_initialized = init;
3174 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
3175 if (TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
3176 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
3178 /* A non-static field declared with an immediate initialization is
3179 to be initialized in <init>, if any. This field is remembered
3180 to be processed at the time of the generation of <init>. */
3183 TREE_CHAIN (init) = ctxp->non_static_initialized;
3184 ctxp->non_static_initialized = init;
3186 INITIALIZED_P (field_decl) = 1;
3187 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
3190 lineno = saved_lineno;
3193 /* Generate the method $finit$ that initializes fields initialized
3194 upon declaration. */
3197 maybe_generate_finit ()
3199 tree mdecl, current;
3201 if (!ctxp->non_static_initialized || java_error_count)
3204 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3205 ACC_PRIVATE, void_type_node,
3206 finit_identifier_node, end_params_node);
3207 start_artificial_method_body (mdecl);
3209 ctxp->non_static_initialized = nreverse (ctxp->non_static_initialized);
3210 for (current = ctxp->non_static_initialized; current;
3211 current = TREE_CHAIN (current))
3212 java_method_add_stmt (mdecl,
3213 build_debugable_stmt (EXPR_WFL_LINECOL (current),
3216 end_artificial_method_body (mdecl);
3217 CLASS_HAS_FINIT_P (TREE_TYPE (ctxp->current_parsed_class)) = 1;
3218 ctxp->non_static_initialized = NULL_TREE;
3221 /* Check whether it is necessary to generate a <clinit> for the class
3225 maybe_generate_clinit ()
3229 if (!ctxp->static_initialized || java_error_count)
3232 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3233 ACC_STATIC, void_type_node,
3234 clinit_identifier_node, end_params_node);
3235 start_artificial_method_body (mdecl);
3237 /* Keep initialization in order to enforce 8.5 */
3238 ctxp->static_initialized = nreverse (ctxp->static_initialized);
3240 /* We process the list of assignment we produced as the result of
3241 the declaration of initialized static field and add them as
3242 statement to the <clinit> method. */
3243 for (c = ctxp->static_initialized; c; c = TREE_CHAIN (c))
3245 /* We build the assignment expression that will initialize the
3246 field to its value. There are strict rules on static
3247 initializers (8.5). FIXME */
3248 java_method_add_stmt (mdecl,
3249 build_debugable_stmt (EXPR_WFL_LINECOL (c), c));
3252 end_artificial_method_body (mdecl);
3253 ctxp->static_initialized = NULL_TREE;
3256 /* Shared accros method_declarator and method_header to remember the
3257 patch stage that was reached during the declaration of the method.
3258 A method DECL is built differently is there is no patch
3259 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
3260 pending on the currently defined method. */
3262 static int patch_stage;
3264 /* Check the method declaration and add the method to its current
3265 class. If the argument list is known to contain incomplete types,
3266 the method is partially added and the registration will be resume
3267 once the method arguments resolved. If TYPE is NULL, we're dealing
3268 with a constructor. */
3271 method_header (flags, type, mdecl, throws)
3273 tree type, mdecl, throws;
3275 tree meth = TREE_VALUE (mdecl);
3276 tree id = TREE_PURPOSE (mdecl);
3277 tree this_class = TREE_TYPE (ctxp->current_parsed_class);
3278 tree type_wfl = NULL_TREE;
3279 tree meth_name = NULL_TREE, current, orig_arg;
3281 int constructor_ok = 0, must_chain;
3283 check_modifiers_consistency (flags);
3285 /* There are some forbidden modifiers for an abstract method and its
3286 class must be abstract as well. */
3287 if (type && (flags & ACC_ABSTRACT))
3289 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
3290 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
3291 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
3292 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
3293 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
3294 if (!CLASS_ABSTRACT (TYPE_NAME (this_class)))
3296 (id, "Class `%s' must be declared abstract to define abstract "
3298 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
3299 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3301 /* Things to be checked when declaring a constructor */
3304 int ec = java_error_count;
3305 /* 8.6: Constructor declarations: we might be trying to define a
3306 method without specifying a return type. */
3307 if (EXPR_WFL_NODE (id) != ctxp->current_parsed_class_un)
3309 (id, "Invalid method declaration, return type required");
3310 /* 8.6.3: Constructor modifiers */
3313 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
3314 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
3315 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
3316 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
3317 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
3319 /* If we found error here, we don't consider it's OK to tread
3320 the method definition as a constructor, for the rest of this
3322 if (ec == java_error_count)
3326 /* Method declared within the scope of an interface are implicitly
3327 abstract and public. Conflicts with other erroneously provided
3328 modifiers are check right after. */
3330 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
3332 /* If FLAGS isn't set because of a modifier, turn the
3333 corresponding modifier WFL to NULL so we issue a warning on
3334 the obsolete use of the modifier */
3335 if (!(flags & ACC_PUBLIC))
3336 MODIFIER_WFL (PUBLIC_TK) = NULL;
3337 if (!(flags & ACC_ABSTRACT))
3338 MODIFIER_WFL (ABSTRACT_TK) = NULL;
3339 flags |= ACC_PUBLIC;
3340 flags |= ACC_ABSTRACT;
3343 /* Modifiers context reset moved up, so abstract method declaration
3344 modifiers can be later checked. */
3346 /* Set constructor returned type to void and method name to <init>,
3347 unless we found an error identifier the constructor (in which
3348 case we retain the original name) */
3351 type = void_type_node;
3353 meth_name = init_identifier_node;
3356 meth_name = EXPR_WFL_NODE (id);
3358 /* Do the returned type resolution and registration if necessary */
3359 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3362 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
3363 EXPR_WFL_NODE (id) = meth_name;
3364 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3368 patch_stage = JDEP_METHOD_RETURN;
3369 register_incomplete_type (patch_stage, type_wfl, id, type);
3370 TREE_TYPE (meth) = GET_REAL_TYPE (type);
3373 TREE_TYPE (meth) = type;
3375 saved_lineno = lineno;
3376 /* When defining an abstract or interface method, the curly
3377 bracket at level 1 doesn't exist because there is no function
3379 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
3380 EXPR_WFL_LINENO (id));
3382 /* Remember the original argument list */
3383 orig_arg = TYPE_ARG_TYPES (meth);
3385 if (patch_stage) /* includes ret type and/or all args */
3388 meth = add_method_1 (this_class, flags, meth_name, meth);
3389 /* Patch for the return type */
3390 if (patch_stage == JDEP_METHOD_RETURN)
3392 jdep = CLASSD_LAST (ctxp->classd_list);
3393 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
3395 /* This is the stop JDEP. METH allows the function's signature
3397 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
3400 meth = add_method (this_class, flags, meth_name,
3401 build_java_signature (meth));
3403 /* Fix the method argument list so we have the argument name
3405 fix_method_argument_names (orig_arg, meth);
3407 /* Register the parameter number and re-install the current line
3409 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
3410 lineno = saved_lineno;
3412 /* Register exception specified by the `throws' keyword for
3413 resolution and set the method decl appropriate field to the list.
3414 Note: the grammar ensures that what we get here are class
3418 throws = nreverse (throws);
3419 for (current = throws; current; current = TREE_CHAIN (current))
3421 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
3422 NULL_TREE, NULL_TREE);
3423 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
3424 &TREE_VALUE (current);
3426 DECL_FUNCTION_THROWS (meth) = throws;
3429 /* We set the DECL_NAME to ID so we can track the location where
3430 the function was declared. This allow us to report
3431 redefinition error accurately. When method are verified,
3432 DECL_NAME is reinstalled properly (using the content of the
3433 WFL node ID) (see check_method_redefinition). We don't do that
3434 when Object is being defined. Constructor <init> names will be
3435 reinstalled the same way. */
3436 if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node)
3437 DECL_NAME (meth) = id;
3439 /* Set the flag if we correctly processed a constructor */
3441 DECL_CONSTRUCTOR_P (meth) = 1;
3443 /* Eventually set the @deprecated tag flag */
3444 CHECK_DEPRECATED (meth);
3450 fix_method_argument_names (orig_arg, meth)
3451 tree orig_arg, meth;
3453 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
3454 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
3456 TREE_PURPOSE (arg) = this_identifier_node;
3457 arg = TREE_CHAIN (arg);
3459 while (orig_arg != end_params_node)
3461 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
3462 orig_arg = TREE_CHAIN (orig_arg);
3463 arg = TREE_CHAIN (arg);
3467 /* Complete the method declaration with METHOD_BODY. */
3470 complete_method_declaration (method_body)
3473 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
3474 maybe_absorb_scoping_blocks ();
3475 /* Exit function's body */
3477 /* Merge last line of the function with first line, directly in the
3478 function decl. It will be used to emit correct debug info. */
3479 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
3482 /* Build a an error message for constructor circularity errors. */
3485 constructor_circularity_msg (from, to)
3488 static char string [4096];
3489 char *t = strdup (lang_printable_name (from, 0));
3490 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
3495 /* Verify a circular call to METH. Return 1 if an error is found, 0
3499 verify_constructor_circularity (meth, current)
3502 static tree list = NULL_TREE;
3504 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3506 if (TREE_VALUE (c) == meth)
3512 list = nreverse (list);
3513 for (liste = list; liste; liste = TREE_CHAIN (liste))
3516 (TREE_PURPOSE (TREE_PURPOSE (liste)),
3517 constructor_circularity_msg
3518 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
3522 t = strdup (lang_printable_name (meth, 0));
3523 parse_error_context (TREE_PURPOSE (c),
3524 "%s: recursive invocation of constructor `%s'",
3525 constructor_circularity_msg (current, meth), t);
3531 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3533 list = tree_cons (c, current, list);
3534 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
3536 list = TREE_CHAIN (list);
3541 /* Check modifiers that can be declared but exclusively */
3544 check_modifiers_consistency (flags)
3548 tree cl = NULL_TREE;
3550 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, 0, acc_count, cl);
3551 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, 1, acc_count, cl);
3552 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, 2, acc_count, cl);
3555 (cl, "Inconsistent member declaration. At most one of `public', "
3556 "`private', or `protected' may be specified");
3559 /* Check the methode header METH for abstract specifics features */
3562 check_abstract_method_header (meth)
3565 int flags = get_access_flags_from_decl (meth);
3566 /* DECL_NAME might still be a WFL node */
3567 tree name = GET_METHOD_NAME (meth);
3569 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (ABSTRACT_TK), flags,
3570 ACC_ABSTRACT, "abstract method `%s'",
3571 IDENTIFIER_POINTER (name));
3572 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), flags,
3573 ACC_PUBLIC, "abstract method `%s'",
3574 IDENTIFIER_POINTER (name));
3576 check_modifiers ("Illegal modifier `%s' for interface method",
3577 flags, INTERFACE_METHOD_MODIFIERS);
3580 /* Create a FUNCTION_TYPE node and start augmenting it with the
3581 declared function arguments. Arguments type that can't be resolved
3582 are left as they are, but the returned node is marked as containing
3583 incomplete types. */
3586 method_declarator (id, list)
3589 tree arg_types = NULL_TREE, current, node;
3590 tree meth = make_node (FUNCTION_TYPE);
3593 patch_stage = JDEP_NO_PATCH;
3595 for (current = list; current; current = TREE_CHAIN (current))
3598 tree wfl_name = TREE_PURPOSE (current);
3599 tree type = TREE_VALUE (current);
3600 tree name = EXPR_WFL_NODE (wfl_name);
3601 tree already, arg_node;
3602 tree type_wfl = NULL_TREE;
3605 /* Obtain a suitable type for resolution, if necessary */
3606 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3608 /* Process NAME, as it may specify extra dimension(s) for it */
3609 type = build_array_from_name (type, type_wfl, name, &name);
3610 EXPR_WFL_NODE (wfl_name) = name;
3612 real_type = GET_REAL_TYPE (type);
3613 if (TREE_CODE (real_type) == RECORD_TYPE)
3615 real_type = promote_type (real_type);
3616 if (TREE_CODE (type) == TREE_LIST)
3617 TREE_PURPOSE (type) = real_type;
3620 /* Check redefinition */
3621 for (already = arg_types; already; already = TREE_CHAIN (already))
3622 if (TREE_PURPOSE (already) == name)
3625 (wfl_name, "Variable `%s' is used more than once in the "
3626 "argument list of method `%s'", IDENTIFIER_POINTER (name),
3627 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3631 /* If we've an incomplete argument type, we know there is a location
3632 to patch when the type get resolved, later. */
3636 patch_stage = JDEP_METHOD;
3637 type = register_incomplete_type (patch_stage,
3638 type_wfl, wfl_name, type);
3639 jdep = CLASSD_LAST (ctxp->classd_list);
3640 JDEP_MISC (jdep) = id;
3643 /* The argument node: a name and a (possibly) incomplete type */
3644 arg_node = build_tree_list (name, real_type);
3646 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
3647 TREE_CHAIN (arg_node) = arg_types;
3648 arg_types = arg_node;
3650 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
3651 node = build_tree_list (id, meth);
3656 unresolved_type_p (wfl, returned)
3661 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
3663 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
3665 *returned = (decl ? TREE_TYPE (decl) : NULL_TREE);
3673 /* From NAME, build a qualified identifier node using the
3674 qualification from the current package definition. */
3677 parser_qualified_classname (name)
3681 return merge_qualified_name (ctxp->package, EXPR_WFL_NODE (name));
3683 return EXPR_WFL_NODE (name);
3686 /* Called once the type a interface extends is resolved. Returns 0 if
3687 everything is OK. */
3690 parser_check_super_interface (super_decl, this_decl, this_wfl)
3691 tree super_decl, this_decl, this_wfl;
3693 tree super_type = TREE_TYPE (super_decl);
3695 /* Has to be an interface */
3696 if (!CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (super_decl))))
3699 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
3700 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
3701 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
3702 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
3703 "interface" : "class"),
3704 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
3708 /* Check scope: same package OK, other package: OK if public */
3709 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
3712 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
3713 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3714 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3718 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
3719 0 if everthing is OK. */
3722 parser_check_super (super_decl, this_decl, wfl)
3723 tree super_decl, this_decl, wfl;
3725 tree super_type = TREE_TYPE (super_decl);
3727 /* SUPER should be a CLASS (neither an array nor an interface) */
3728 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
3731 (wfl, "Class `%s' can't subclass %s `%s'",
3732 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3733 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
3734 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3738 if (CLASS_FINAL (TYPE_NAME (super_type)))
3740 parse_error_context (wfl, "Can't subclass final classes: %s",
3741 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3745 /* Check scope: same package OK, other package: OK if public */
3746 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
3749 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
3750 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3751 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3755 /* Create a new dependency list and link it (in a LIFO manner) to the
3756 CTXP list of type dependency list. */
3759 create_jdep_list (ctxp)
3760 struct parser_ctxt *ctxp;
3762 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
3763 new->first = new->last = NULL;
3764 new->next = ctxp->classd_list;
3765 ctxp->classd_list = new;
3769 reverse_jdep_list (ctxp)
3770 struct parser_ctxt *ctxp;
3772 register jdeplist *prev = NULL, *current, *next;
3773 for (current = ctxp->classd_list; current; current = next)
3775 next = current->next;
3776 current->next = prev;
3782 /* Create a fake pointer based on the ID stored in
3783 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
3784 registered again. */
3787 obtain_incomplete_type (type_name)
3792 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
3793 name = EXPR_WFL_NODE (type_name);
3794 else if (INCOMPLETE_TYPE_P (type_name))
3795 name = TYPE_NAME (type_name);
3797 fatal ("invalid type name - obtain_incomplete_type");
3799 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
3800 if (TYPE_NAME (TREE_PURPOSE (ptr)) == name)
3806 push_obstacks (&permanent_obstack, &permanent_obstack);
3807 BUILD_PTR_FROM_NAME (core, name);
3809 ptr = build_tree_list (core, NULL_TREE);
3811 TREE_CHAIN (ptr) = ctxp->incomplete_class;
3812 ctxp->incomplete_class = ptr;
3818 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
3819 non NULL instead of computing a new fake type based on WFL. The new
3820 dependency is inserted in the current type dependency list, in FIFO
3824 register_incomplete_type (kind, wfl, decl, ptr)
3826 tree wfl, decl, ptr;
3828 jdep *new = (jdep *)xmalloc (sizeof (jdep));
3830 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
3831 ptr = obtain_incomplete_type (wfl);
3833 JDEP_KIND (new) = kind;
3834 JDEP_DECL (new) = decl;
3835 JDEP_SOLV (new) = ptr;
3836 JDEP_WFL (new) = wfl;
3837 JDEP_CHAIN (new) = NULL;
3838 JDEP_MISC (new) = NULL_TREE;
3839 JDEP_GET_PATCH (new) = (tree *)NULL;
3841 JDEP_INSERT (ctxp->classd_list, new);
3847 java_check_circular_reference ()
3850 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
3852 tree type = TREE_TYPE (current);
3853 if (CLASS_INTERFACE (TYPE_NAME (type)))
3855 /* Check all interfaces this class extends */
3856 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
3861 n = TREE_VEC_LENGTH (basetype_vec);
3862 for (i = 0; i < n; i++)
3864 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
3865 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
3866 && interface_of_p (type, BINFO_TYPE (vec_elt)))
3867 parse_error_context (lookup_cl (current),
3868 "Cyclic interface inheritance");
3872 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
3873 parse_error_context (lookup_cl (current),
3874 "Cyclic class inheritance");
3878 /* safe_layout_class just makes sure that we can load a class without
3879 disrupting the current_class, input_file, lineno, etc, information
3880 about the class processed currently. */
3883 safe_layout_class (class)
3886 tree save_current_class = current_class;
3887 char *save_input_filename = input_filename;
3888 int save_lineno = lineno;
3890 push_obstacks (&permanent_obstack, &permanent_obstack);
3892 layout_class (class);
3895 current_class = save_current_class;
3896 input_filename = save_input_filename;
3897 lineno = save_lineno;
3898 CLASS_LOADED_P (class) = 1;
3902 jdep_resolve_class (dep)
3907 if (JDEP_RESOLVED_P (dep))
3908 decl = JDEP_RESOLVED_DECL (dep);
3911 decl = resolve_class (JDEP_TO_RESOLVE (dep),
3912 JDEP_DECL (dep), JDEP_WFL (dep));
3913 JDEP_RESOLVED (dep, decl);
3917 complete_class_report_errors (dep);
3922 /* Complete unsatisfied class declaration and their dependencies */
3925 java_complete_class ()
3932 push_obstacks (&permanent_obstack, &permanent_obstack);
3934 /* Process imports and reverse the import on demand list */
3936 if (ctxp->import_demand_list)
3937 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
3939 /* Rever things so we have the right order */
3940 ctxp->class_list = nreverse (ctxp->class_list);
3941 ctxp->classd_list = reverse_jdep_list (ctxp);
3943 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
3945 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
3948 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
3951 if (!(decl = jdep_resolve_class (dep)))
3954 /* Now it's time to patch */
3955 switch (JDEP_KIND (dep))
3958 /* Simply patch super */
3959 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
3961 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
3962 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
3967 /* We do part of the job done in add_field */
3968 tree field_decl = JDEP_DECL (dep);
3969 tree field_type = TREE_TYPE (decl);
3970 push_obstacks (&permanent_obstack, &permanent_obstack);
3971 if (TREE_CODE (field_type) == RECORD_TYPE)
3972 field_type = promote_type (field_type);
3974 TREE_TYPE (field_decl) = field_type;
3975 DECL_ALIGN (field_decl) = 0;
3976 layout_decl (field_decl, 0);
3977 SOURCE_FRONTEND_DEBUG
3978 (("Completed field/var decl `%s' with `%s'",
3979 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
3980 IDENTIFIER_POINTER (DECL_NAME (decl))));
3983 case JDEP_METHOD: /* We start patching a method */
3984 case JDEP_METHOD_RETURN:
3990 type = TREE_TYPE(decl);
3991 if (TREE_CODE (type) == RECORD_TYPE)
3992 type = promote_type (type);
3993 JDEP_APPLY_PATCH (dep, type);
3994 SOURCE_FRONTEND_DEBUG
3995 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
3996 "Completing fct `%s' with ret type `%s'":
3997 "Completing arg `%s' with type `%s'"),
3998 IDENTIFIER_POINTER (EXPR_WFL_NODE
3999 (JDEP_DECL_WFL (dep))),
4000 IDENTIFIER_POINTER (DECL_NAME (decl))));
4004 dep = JDEP_CHAIN (dep);
4005 if (JDEP_KIND (dep) == JDEP_METHOD_END)
4008 decl = jdep_resolve_class (dep);
4012 tree mdecl = JDEP_DECL (dep), signature;
4013 push_obstacks (&permanent_obstack, &permanent_obstack);
4014 /* Recompute and reset the signature */
4015 signature = build_java_signature (TREE_TYPE (mdecl));
4016 set_java_signature (TREE_TYPE (mdecl), signature);
4023 case JDEP_INTERFACE:
4024 if (parser_check_super_interface (decl, JDEP_DECL (dep),
4027 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
4032 type = TREE_TYPE(decl);
4033 if (TREE_CODE (type) == RECORD_TYPE)
4034 type = promote_type (type);
4035 JDEP_APPLY_PATCH (dep, type);
4039 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4040 SOURCE_FRONTEND_DEBUG
4041 (("Completing a random type dependency on a '%s' node",
4042 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
4045 case JDEP_EXCEPTION:
4046 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4047 SOURCE_FRONTEND_DEBUG
4048 (("Completing `%s' `throws' argument node",
4049 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
4053 fatal ("Can't handle patch code %d - java_complete_class",
4062 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
4066 resolve_class (class_type, decl, cl)
4067 tree class_type, decl, cl;
4069 char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
4071 tree resolved_type, resolved_type_decl;
4073 /* 1- Check to see if we have an array. If true, find what we really
4075 while (name[0] == '[')
4078 TYPE_NAME (class_type) = get_identifier (name);
4080 /* 2- Resolve the bare type */
4081 if (!(resolved_type_decl = do_resolve_class (class_type, decl, cl)))
4083 resolved_type = TREE_TYPE (resolved_type_decl);
4085 /* 3- If we have and array, reconstruct the array down to its nesting */
4088 while (base != name)
4090 if (TREE_CODE (resolved_type) == RECORD_TYPE)
4091 resolved_type = promote_type (resolved_type);
4092 resolved_type = build_java_array_type (resolved_type, -1);
4093 CLASS_LOADED_P (resolved_type) = 1;
4096 /* Build a fake decl for this, since this is what is expected to
4098 resolved_type_decl =
4099 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
4100 /* Figure how those two things are important for error report. FIXME */
4101 DECL_SOURCE_LINE (resolved_type_decl) = 0;
4102 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
4104 return resolved_type_decl;
4107 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
4108 are used to report error messages. */
4111 do_resolve_class (class_type, decl, cl)
4116 tree new_class_decl;
4117 tree original_name = NULL_TREE;
4119 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
4120 its is changed by find_in_imports{_on_demand} */
4122 /* 1- Check for the type in single imports */
4123 if (find_in_imports (class_type))
4126 /* 2- And check for the type in the current compilation unit. If it fails,
4127 try with a name qualified with the package name if appropriate. */
4128 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4130 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4131 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4132 load_class (TYPE_NAME (class_type), 0);
4133 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4136 original_name = TYPE_NAME (class_type);
4137 if (!QUALIFIED_P (TYPE_NAME (class_type)) && ctxp->package)
4138 TYPE_NAME (class_type) = merge_qualified_name (ctxp->package,
4139 TYPE_NAME (class_type));
4141 if (!(new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4142 load_class (TYPE_NAME (class_type), 0);
4143 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4145 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4146 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4147 load_class (TYPE_NAME (class_type), 0);
4148 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4151 new_name = TYPE_NAME (class_type);
4152 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_name)) != NULL_TREE)
4154 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4155 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4156 load_class (new_name, 0);
4157 return IDENTIFIER_CLASS_VALUE (new_name);
4161 tree class = read_class (new_name);
4162 if (class != NULL_TREE)
4164 tree decl = IDENTIFIER_CLASS_VALUE (new_name);
4165 if (decl == NULL_TREE)
4166 decl = push_class (class, new_name);
4171 TYPE_NAME (class_type) = original_name;
4173 /* 3- Check an other compilation unit that bears the name of type */
4174 load_class (TYPE_NAME (class_type), 0);
4175 if (check_pkg_class_access (TYPE_NAME (class_type),
4176 (cl ? cl : lookup_cl (decl))))
4179 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4180 return new_class_decl;
4182 /* 4- Check the import on demands. Don't allow bar.baz to be
4183 imported from foo.* */
4184 if (!QUALIFIED_P (TYPE_NAME (class_type)))
4185 if (find_in_imports_on_demand (class_type))
4188 /* 5- Last call for a resolution */
4189 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4192 /* Resolve NAME and lay it out (if not done and if not the current
4193 parsed class). Return a decl node. This function is meant to be
4194 called when type resolution is necessary during the walk pass. */
4197 resolve_and_layout (something, cl)
4203 /* Don't do that on the current class */
4204 if (something == current_class)
4205 return TYPE_NAME (current_class);
4207 /* Don't do anything for void and other primitive types */
4208 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4211 /* Pointer types can be reall pointer types or fake pointers. When
4212 finding a real pointer, recheck for primitive types */
4213 if (TREE_CODE (something) == POINTER_TYPE)
4215 if (TREE_TYPE (something))
4217 something = TREE_TYPE (something);
4218 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4222 something = TYPE_NAME (something);
4225 /* Don't do anything for arrays of primitive types */
4226 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
4227 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
4230 /* If something is not and IDENTIFIER_NODE, it can be a a TYPE_DECL
4232 if (TREE_CODE (something) != IDENTIFIER_NODE)
4233 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
4234 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
4236 if (!(decl = resolve_no_layout (something, cl)))
4239 /* Resolve and layout if necessary */
4240 layout_class_methods (TREE_TYPE (decl));
4241 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl)))
4242 CHECK_METHODS (decl);
4243 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
4244 safe_layout_class (TREE_TYPE (decl));
4249 /* Resolve a class, returns its decl but doesn't perform any
4250 layout. The current parsing context is saved and restored */
4253 resolve_no_layout (name, cl)
4257 BUILD_PTR_FROM_NAME (ptr, name);
4258 java_parser_context_save_global ();
4259 decl = resolve_class (ptr, NULL_TREE, cl);
4260 java_parser_context_restore_global ();
4265 /* Called when reporting errors. Skip leader '[' in a complex array
4266 type description that failed to be resolved. */
4269 purify_type_name (name)
4272 while (*name && *name == '[')
4277 /* The type CURRENT refers to can't be found. We print error messages. */
4280 complete_class_report_errors (dep)
4285 if (!JDEP_WFL (dep))
4288 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
4289 switch (JDEP_KIND (dep))
4293 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
4294 purify_type_name (name),
4295 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4299 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
4300 purify_type_name (name),
4301 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4303 case JDEP_METHOD: /* Covers arguments */
4305 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4306 "argument `%s' of method `%s'",
4307 purify_type_name (name),
4308 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
4309 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
4311 case JDEP_METHOD_RETURN: /* Covers return type */
4313 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4314 "return type of method `%s'",
4315 purify_type_name (name),
4316 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
4318 case JDEP_INTERFACE:
4320 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
4321 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
4322 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
4323 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4327 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4328 "local variable `%s'",
4329 purify_type_name (IDENTIFIER_POINTER
4330 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
4331 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4333 case JDEP_EXCEPTION: /* As specified by `throws' */
4335 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
4336 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
4339 /* Fix for -Wall. Just break doing nothing. The error will be
4345 /* Check uninitialized final. */
4352 /* Return a static string containing the DECL prototype string. If
4353 DECL is a constructor, use the class name instead of the form
4357 get_printable_method_name (decl)
4363 if (DECL_CONSTRUCTOR_P (decl))
4365 name = DECL_NAME (decl);
4366 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
4369 to_return = lang_printable_name (decl, 0);
4370 if (DECL_CONSTRUCTOR_P (decl))
4371 DECL_NAME (decl) = name;
4376 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
4377 nevertheless needs to be verfied, 1 otherwise. */
4380 reset_method_name (method)
4383 if (DECL_NAME (method) != clinit_identifier_node
4384 && DECL_NAME (method) != finit_identifier_node)
4386 /* NAME is just the plain name when Object is being defined */
4387 if (DECL_CONTEXT (method) != object_type_node)
4388 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
4389 init_identifier_node : GET_METHOD_NAME (method));
4396 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
4399 java_get_real_method_name (method_decl)
4402 tree method_name = DECL_NAME (method_decl);
4403 if (DECL_CONSTRUCTOR_P (method_decl))
4404 return init_identifier_node;
4406 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
4407 and still can be a constructor. FIXME */
4409 /* Don't confuse method only bearing the name of their class as
4411 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
4413 && ctxp->current_parsed_class_un == EXPR_WFL_NODE (method_name)
4414 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
4415 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
4416 return init_identifier_node;
4418 return EXPR_WFL_NODE (method_name);
4421 /* Track method being redefined inside the same class. As a side
4422 effect, set DECL_NAME to an IDENTIFIER (prior entering this
4423 function it's a FWL, so we can track errors more accurately */
4426 check_method_redefinition (class, method)
4430 tree cl = DECL_NAME (method);
4431 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
4432 /* decl name of artificial <clinit> and $finit$ doesn't need to be
4433 fixed and checked */
4435 /* Reset the method name before running the check. If it returns 1,
4436 the method doesn't need to be verified with respect to method
4437 redeclaration and we return 0 */
4438 if (reset_method_name (method))
4441 name = DECL_NAME (method);
4442 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
4444 if (redef == method)
4446 if (DECL_NAME (redef) == name
4447 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
4450 (cl, "Duplicate %s declaration `%s'",
4451 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
4452 get_printable_method_name (redef));
4459 /* Check all the methods of CLASS. Methods are first completed then
4460 checked according to regular method existance rules.
4461 If no constructor were encountered, then build its declaration. */
4464 java_check_regular_methods (class_decl)
4467 int saw_constructor = 0;
4469 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
4470 tree super_class = CLASSTYPE_SUPER (class);
4471 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
4474 /* It is not necessary to check methods defined in java.lang.Object */
4475 if (class == object_type_node)
4478 if (!TYPE_NVIRTUALS (class))
4479 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4481 /* Should take interfaces into account. FIXME */
4482 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
4485 tree method_wfl = DECL_NAME (method);
4488 /* If we previously found something and its name was saved,
4490 if (found && saved_found_wfl)
4492 DECL_NAME (found) = saved_found_wfl;
4493 saved_found_wfl = NULL_TREE;
4496 /* Check for redefinitions */
4497 if (check_method_redefinition (class, method))
4500 /* If we see one constructor a mark so we don't generate the
4501 default one. Also skip other verifications: constructors
4502 can't be inherited hence hiden or overriden */
4503 if (DECL_CONSTRUCTOR_P (method))
4505 saw_constructor = 1;
4509 /* We verify things thrown by the method. They must inherits from
4510 java.lang.Throwable */
4511 for (mthrows = DECL_FUNCTION_THROWS (method);
4512 mthrows; mthrows = TREE_CHAIN (mthrows))
4514 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
4516 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be "
4517 "a subclass of class `java.lang.Throwable'",
4519 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
4522 sig = build_java_argument_signature (TREE_TYPE (method));
4523 found = lookup_argument_method (super_class, DECL_NAME (method), sig);
4525 /* Nothing overrides or it's a private method. */
4528 if (METHOD_PRIVATE (found))
4534 /* If found wasn't verified, it's DECL_NAME won't be set properly.
4535 We set it temporarily for the sake of the error report. */
4536 saved_found_wfl = DECL_NAME (found);
4537 reset_method_name (found);
4539 /* Can't override a method with the same name and different return
4541 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
4543 char *t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)),
4547 "Method `%s' was defined with return type `%s' in class `%s'",
4548 lang_printable_name (found, 0), t,
4550 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4554 /* Can't override final. Can't override static. */
4555 if (METHOD_FINAL (found) || METHOD_STATIC (found))
4557 /* Static *can* override static */
4558 if (METHOD_STATIC (found) && METHOD_STATIC (method))
4562 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
4563 (METHOD_FINAL (found) ? "Final" : "Static"),
4564 lang_printable_name (found, 0),
4565 (METHOD_FINAL (found) ? "final" : "static"),
4567 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4570 /* Static method can't override instance method. */
4571 if (METHOD_STATIC (method))
4575 "Instance methods can't be overriden by a static method. Method "
4576 "`%s' is an instance method in class `%s'",
4577 lang_printable_name (found, 0),
4579 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4583 aflags = get_access_flags_from_decl (found);
4584 /* - Overriding/hiding public must be public
4585 - Overriding/hiding protected must be protected or public
4586 - If the overriden or hidden method has default (package)
4587 access, then the overriding or hiding method must not be
4588 private; otherwise, a compile-time error occurs */
4589 if ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
4590 || (METHOD_PROTECTED (found)
4591 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
4592 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
4593 && METHOD_PRIVATE (method)))
4597 "Methods can't be overridden to be more private. Method `%s' is "
4598 "not %s in class `%s'", lang_printable_name (method, 0),
4599 (METHOD_PUBLIC (method) ? "public" :
4600 (METHOD_PRIVATE (method) ? "private" : "protected")),
4601 IDENTIFIER_POINTER (DECL_NAME
4602 (TYPE_NAME (DECL_CONTEXT (found)))));
4606 /* Overriding methods must have compatible `throws' clauses on checked
4607 exceptions, if any */
4608 check_throws_clauses (method, method_wfl, found);
4610 /* If the method has default access in an other package, then
4611 issue a warning that the current method doesn't override the
4612 one that was found elsewhere. Do not issue this warning when
4613 the match was found in java.lang.Object. */
4614 if (DECL_CONTEXT (found) != object_type_node
4615 && (!aflags || (aflags > ACC_PROTECTED))
4616 && !class_in_current_package (DECL_CONTEXT (found))
4617 && flag_not_overriding)
4618 parse_warning_context
4619 (method_wfl, "Method `%s' in class `%s' does not "
4620 "override the corresponding method in class `%s', which is "
4621 "private to a different package",
4622 lang_printable_name (found, 0),
4623 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
4624 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4626 /* Inheriting multiple methods with the same signature. FIXME */
4629 /* Don't forget eventual pending found and saved_found_wfl. Take
4630 into account that we might have exited because we saw an
4631 aritifical method as the last entry. */
4633 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
4634 DECL_NAME (found) = saved_found_wfl;
4636 if (!TYPE_NVIRTUALS (class))
4637 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4639 if (!saw_constructor)
4641 /* No constructor seen, we craft one, at line 0. Since this
4642 operation takes place after we laid methods out
4643 (layout_class_methods), we prepare the its DECL
4648 /* If the class is declared PUBLIC, the default constructor is
4649 PUBLIC otherwise it has default access implied by no access
4651 flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
4653 decl = create_artificial_method (class, flags, void_type_node,
4654 init_identifier_node, end_params_node);
4655 DECL_CONSTRUCTOR_P (decl) = 1;
4656 layout_class_method (TREE_TYPE (class_decl), NULL_TREE, decl, NULL_TREE);
4660 /* Return a non zero value if the `throws' clause of METHOD (if any)
4661 is incompatible with the `throws' clause of FOUND (if any). */
4664 check_throws_clauses (method, method_wfl, found)
4665 tree method, method_wfl, found;
4667 tree mthrows, fthrows;
4669 /* Can't check these things with class loaded from bytecode. FIXME */
4670 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
4673 for (mthrows = DECL_FUNCTION_THROWS (method);
4674 mthrows; mthrows = TREE_CHAIN (mthrows))
4676 /* We don't verify unchecked expressions */
4677 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
4679 /* Checked expression must be compatible */
4680 for (fthrows = DECL_FUNCTION_THROWS (found);
4681 fthrows; fthrows = TREE_CHAIN (fthrows))
4682 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
4687 (method_wfl, "Invalid checked exception class `%s' in "
4688 "`throws' clause. The exception must be a subclass of an "
4689 "exception thrown by `%s' from class `%s'",
4690 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
4691 lang_printable_name (found, 0),
4693 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4698 /* Check abstract method of interface INTERFACE */
4701 java_check_abstract_methods (interface_decl)
4702 tree interface_decl;
4705 tree method, basetype_vec, found;
4706 tree interface = TREE_TYPE (interface_decl);
4708 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
4710 tree method_wfl = DECL_NAME (method);
4712 /* 2- Check for double definition inside the defining interface */
4713 if (check_method_redefinition (interface, method))
4716 /* 3- Overriding is OK as far as we preserve the return type and
4717 the thrown exceptions (FIXME) */
4718 found = lookup_java_interface_method2 (interface, method);
4722 tree saved_found_wfl = DECL_NAME (found);
4723 reset_method_name (found);
4724 t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
4727 "Method `%s' was defined with return type `%s' in class `%s'",
4728 lang_printable_name (found, 0), t,
4730 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4734 DECL_NAME (found) = saved_found_wfl;
4738 /* 4- Inherited methods can't differ by their returned types */
4739 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
4741 n = TREE_VEC_LENGTH (basetype_vec);
4742 for (i = 0; i < n; i++)
4744 tree sub_interface_method, sub_interface;
4745 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4748 sub_interface = BINFO_TYPE (vec_elt);
4749 for (sub_interface_method = TYPE_METHODS (sub_interface);
4750 sub_interface_method;
4751 sub_interface_method = TREE_CHAIN (sub_interface_method))
4753 found = lookup_java_interface_method2 (interface,
4754 sub_interface_method);
4755 if (found && (found != sub_interface_method))
4757 tree saved_found_wfl = DECL_NAME (found);
4758 reset_method_name (found);
4760 (lookup_cl (sub_interface_method),
4761 "Interface `%s' inherits method `%s' from interface `%s'. "
4762 "This method is redefined with a different return type in "
4764 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
4765 lang_printable_name (found, 0),
4767 (DECL_NAME (TYPE_NAME
4768 (DECL_CONTEXT (sub_interface_method)))),
4770 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4771 DECL_NAME (found) = saved_found_wfl;
4777 /* Lookup methods in interfaces using their name and partial
4778 signature. Return a matching method only if their types differ. */
4781 lookup_java_interface_method2 (class, method_decl)
4782 tree class, method_decl;
4785 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
4790 n = TREE_VEC_LENGTH (basetype_vec);
4791 for (i = 0; i < n; i++)
4793 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
4794 if ((BINFO_TYPE (vec_elt) != object_type_node)
4796 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
4799 for (i = 0; i < n; i++)
4801 to_return = lookup_java_interface_method2
4802 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
4810 /* Lookup method using their name and partial signature. Return a
4811 matching method only if their types differ. */
4814 lookup_java_method2 (clas, method_decl, do_interface)
4815 tree clas, method_decl;
4818 tree method, method_signature, method_name, method_type, name;
4820 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
4821 name = DECL_NAME (method_decl);
4822 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4823 EXPR_WFL_NODE (name) : name);
4824 method_type = TREE_TYPE (TREE_TYPE (method_decl));
4826 while (clas != NULL_TREE)
4828 for (method = TYPE_METHODS (clas);
4829 method != NULL_TREE; method = TREE_CHAIN (method))
4831 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
4832 tree name = DECL_NAME (method);
4833 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4834 EXPR_WFL_NODE (name) : name) == method_name
4835 && method_sig == method_signature
4836 && TREE_TYPE (TREE_TYPE (method)) != method_type)
4839 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
4844 /* Return the line that matches DECL line number. Used during error
4851 static tree cl = NULL_TREE;
4856 if (cl == NULL_TREE)
4857 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
4859 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
4860 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
4865 /* Look for a simple name in the single-type import list */
4868 find_name_in_single_imports (name)
4873 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
4874 if (TREE_VALUE (node) == name)
4875 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
4880 /* Process all single-type import. */
4888 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4890 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
4892 /* Don't load twice something already defined. */
4893 if (IDENTIFIER_CLASS_VALUE (to_be_found))
4895 QUALIFIED_P (to_be_found) = 1;
4896 load_class (to_be_found, 0);
4898 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
4899 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
4901 parse_error_context (TREE_PURPOSE (import),
4902 "Class or interface `%s' not found in import",
4903 IDENTIFIER_POINTER (to_be_found));
4912 /* Possibly find a class imported by a single-type import statement. Return
4913 1 if an error occured, 0 otherwise. */
4916 find_in_imports (class_type)
4921 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4922 if (TREE_VALUE (import) == TYPE_NAME (class_type))
4924 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
4925 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
4931 note_possible_classname (name, len)
4936 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
4938 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
4942 node = ident_subst (name, len, "", '/', '.', "");
4943 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
4944 QUALIFIED_P (node) = 1; /* As soon as we turn / into . */
4948 /* Read a import directory, gathering potential match for further type
4949 references. Indifferently reads a filesystem or a ZIP archive
4953 read_import_dir (wfl)
4956 tree package_id = EXPR_WFL_NODE (wfl);
4957 char *package_name = IDENTIFIER_POINTER (package_id);
4958 int package_length = IDENTIFIER_LENGTH (package_id);
4960 JCF *saved_jcf = current_jcf;
4965 struct buffer filename[1];
4968 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
4970 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
4972 BUFFER_INIT (filename);
4973 buffer_grow (filename, package_length + 100);
4975 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
4977 char *entry_name = jcf_path_name (entry);
4978 int entry_length = strlen (entry_name);
4979 if (jcf_path_is_zipfile (entry))
4982 buffer_grow (filename, entry_length);
4983 memcpy (filename->data, entry_name, entry_length - 1);
4984 filename->data[entry_length-1] = '\0';
4985 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
4987 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
4990 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
4991 BUFFER_RESET (filename);
4992 for (k = 0; k < package_length; k++)
4994 char ch = package_name[k];
4995 *filename->ptr++ = ch == '.' ? '/' : ch;
4997 *filename->ptr++ = '/';
4999 for (; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
5001 char *current_entry = ZIPDIR_FILENAME (zipd);
5002 int current_entry_len = zipd->filename_length;
5004 if (strncmp (filename->data, current_entry,
5005 BUFFER_LENGTH (filename)) != 0)
5007 found += note_possible_classname (current_entry,
5014 BUFFER_RESET (filename);
5015 buffer_grow (filename, entry_length + package_length + 4);
5016 strcpy (filename->data, entry_name);
5017 filename->ptr = filename->data + entry_length;
5018 for (k = 0; k < package_length; k++)
5020 char ch = package_name[k];
5021 *filename->ptr++ = ch == '.' ? '/' : ch;
5023 *filename->ptr = '\0';
5025 dirp = opendir (filename->data);
5028 *filename->ptr++ = '/';
5033 struct dirent *direntp = readdir (dirp);
5036 d_name = direntp->d_name;
5037 len = strlen (direntp->d_name);
5038 buffer_grow (filename, len+1);
5039 strcpy (filename->ptr, d_name);
5040 found += note_possible_classname (filename->data + entry_length,
5041 package_length+len+1);
5048 free (filename->data);
5050 /* Here we should have a unified way of retrieving an entry, to be
5054 static int first = 1;
5058 sprintf (buffer, "Can't find default package `%s'. Check "
5059 "the CLASSPATH environment variable and the access to the "
5060 "archives.", package_name);
5066 parse_error_context (wfl, "Package `%s' not found in import",
5068 current_jcf = saved_jcf;
5071 current_jcf = saved_jcf;
5074 /* Possibly find a type in the import on demands specified
5075 types. Returns 1 if an error occured, 0 otherwise. Run throught the
5076 entire list, to detected potential double definitions. */
5079 find_in_imports_on_demand (class_type)
5082 tree node, import, node_to_use;
5086 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
5089 obstack_grow (&temporary_obstack,
5090 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
5091 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5092 obstack_1grow (&temporary_obstack, '.');
5093 obstack_grow0 (&temporary_obstack,
5094 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5095 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
5096 id_name = obstack_finish (&temporary_obstack);
5098 node = maybe_get_identifier (id_name);
5099 if (node && IS_A_CLASSFILE_NAME (node))
5103 cl = TREE_PURPOSE (import);
5111 (import, "Type `%s' also potentially defined in package `%s'",
5112 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5113 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5120 /* Setup lineno so that it refers to the line of the import (in
5121 case we parse a class file and encounter errors */
5123 int saved_lineno = lineno;
5124 lineno = EXPR_WFL_LINENO (cl);
5125 TYPE_NAME (class_type) = node_to_use;
5126 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
5127 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5128 /* If there is no DECL set for the class or if the class isn't
5129 loaded and not seen in source yet, the load */
5130 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
5131 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
5132 load_class (node_to_use, 0);
5133 lineno = saved_lineno;
5134 return check_pkg_class_access (TYPE_NAME (class_type), cl);
5137 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
5141 resolve_package (pkg, next)
5144 tree type_name = NULL_TREE;
5145 char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5147 /* The trick is to determine when the package name stops and were
5148 the name of something contained in the package starts. Then we
5149 return a fully qualified name of what we want to get. */
5151 /* Do a quick search on well known package names */
5152 if (!strncmp (name, "java.lang.reflect", 17))
5155 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
5156 type_name = lookup_package_type (name, 17);
5158 else if (!strncmp (name, "java.lang", 9))
5160 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
5161 type_name = lookup_package_type (name, 9);
5164 return NULL_TREE; /* FIXME, search all imported packages. */
5170 lookup_package_type (name, from)
5175 char *sub = &name[from+1];
5176 while (*sub != '.' && *sub)
5178 strncpy (subname, name, sub-name);
5179 subname [sub-name] = '\0';
5180 return get_identifier (subname);
5183 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
5184 access violations were found, 1 otherwise. */
5187 check_pkg_class_access (class_name, cl)
5193 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
5196 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
5199 if (!CLASS_PUBLIC (TYPE_NAME (type)))
5202 (cl, "Can't access %s `%s'. Only public classes and interfaces in "
5203 "other packages can be accessed",
5204 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
5205 IDENTIFIER_POINTER (class_name));
5211 /* Local variable declaration. */
5214 declare_local_variables (modifier, type, vlist)
5219 tree decl, current, saved_type;
5220 tree type_wfl = NULL_TREE;
5223 /* Push a new block if statement were seen between the last time we
5224 pushed a block and now. Keep a cound of block to close */
5225 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)))
5227 tree body = DECL_FUNCTION_BODY (current_function_decl);
5228 tree b = enter_block ();
5229 BLOCK_EXPR_ORIGIN(b) = body;
5235 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
5236 if (modifier == ACC_FINAL)
5238 if (flag_static_local_jdk1_1)
5239 parse_warning_context (ctxp->modifier_ctx [i],
5240 "Unsupported JDK1.1 `final' local variable "
5241 "(treated as non final)");
5246 (ctxp->modifier_ctx [i],
5247 "Only `final' is allowed as a local variables modifier");
5252 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
5253 hold the TYPE value if a new incomplete has to be created (as
5254 opposed to being found already existing and reused). */
5255 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5257 /* If TYPE is fully resolved and we don't have a reference, make one */
5258 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5260 /* Go through all the declared variables */
5261 for (current = vlist, saved_type = type; current;
5262 current = TREE_CHAIN (current), type = saved_type)
5264 tree other, real_type;
5265 tree wfl = TREE_PURPOSE (current);
5266 tree name = EXPR_WFL_NODE (wfl);
5267 tree init = TREE_VALUE (current);
5269 /* Process NAME, as it may specify extra dimension(s) for it */
5270 type = build_array_from_name (type, type_wfl, name, &name);
5272 /* Variable redefinition check */
5273 if ((other = lookup_name_in_blocks (name)))
5275 variable_redefinition_error (wfl, name, TREE_TYPE (other),
5276 DECL_SOURCE_LINE (other));
5280 /* Type adjustment. We may have just readjusted TYPE because
5281 the variable specified more dimensions. Make sure we have
5282 a reference if we can and don't have one already. */
5283 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5285 real_type = GET_REAL_TYPE (type);
5286 /* Never layout this decl. This will be done when its scope
5288 decl = build_decl (VAR_DECL, name, real_type);
5289 BLOCK_CHAIN_DECL (decl);
5291 /* Don't try to use an INIT statement when an error was found */
5292 if (init && java_error_count)
5295 /* Add the initialization function to the current function's code */
5298 /* Name might have been readjusted */
5299 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
5300 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
5301 java_method_add_stmt (current_function_decl,
5302 build_debugable_stmt (EXPR_WFL_LINECOL (init),
5306 /* Setup dependency the type of the decl */
5310 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
5311 dep = CLASSD_LAST (ctxp->classd_list);
5312 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
5315 SOURCE_FRONTEND_DEBUG (("Defined locals"));
5318 /* Called during parsing. Build decls from argument list. */
5321 source_start_java_method (fndecl)
5328 current_function_decl = fndecl;
5330 /* New scope for the function */
5332 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
5333 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
5335 tree type = TREE_VALUE (tem);
5336 tree name = TREE_PURPOSE (tem);
5338 /* If type is incomplete. Create an incomplete decl and ask for
5339 the decl to be patched later */
5340 if (INCOMPLETE_TYPE_P (type))
5343 tree real_type = GET_REAL_TYPE (type);
5344 parm_decl = build_decl (PARM_DECL, name, real_type);
5345 type = obtain_incomplete_type (type);
5346 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
5347 jdep = CLASSD_LAST (ctxp->classd_list);
5348 JDEP_MISC (jdep) = name;
5349 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
5352 parm_decl = build_decl (PARM_DECL, name, type);
5354 BLOCK_CHAIN_DECL (parm_decl);
5356 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5357 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
5359 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
5362 /* Called during parsing. Creates an artificial method declaration. */
5365 create_artificial_method (class, flags, type, name, args)
5368 tree type, name, args;
5370 int saved_lineno = lineno;
5374 mdecl = make_node (FUNCTION_TYPE);
5375 TREE_TYPE (mdecl) = type;
5376 TYPE_ARG_TYPES (mdecl) = args;
5377 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
5378 lineno = saved_lineno;
5379 DECL_ARTIFICIAL (mdecl) = 1;
5383 /* Starts the body if an artifical method. */
5386 start_artificial_method_body (mdecl)
5389 DECL_SOURCE_LINE (mdecl) = 1;
5390 DECL_SOURCE_LINE_MERGE (mdecl, 1);
5391 source_start_java_method (mdecl);
5396 end_artificial_method_body (mdecl)
5399 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
5403 /* Called during expansion. Push decls formerly built from argument
5404 list so they're usable during expansion. */
5407 expand_start_java_method (fndecl)
5412 current_function_decl = fndecl;
5414 announce_function (fndecl);
5415 pushlevel (1); /* Push parameters */
5416 ptr = &DECL_ARGUMENTS (fndecl);
5417 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5420 tree next = TREE_CHAIN (tem);
5421 tree type = TREE_TYPE (tem);
5422 #ifdef PROMOTE_PROTOTYPES
5423 if (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
5424 && INTEGRAL_TYPE_P (type))
5425 type = integer_type_node;
5427 DECL_ARG_TYPE (tem) = type;
5428 layout_decl (tem, 0);
5430 INITIALIZED_P (tem) = 1; /* Parms are initialized */
5432 ptr = &TREE_CHAIN (tem);
5436 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5437 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
5438 complete_start_java_method (fndecl);
5441 /* Terminate a function and expand its body. */
5444 source_end_java_method ()
5446 tree fndecl = current_function_decl;
5448 java_parser_context_save_global ();
5449 lineno = ctxp->last_ccb_indent1;
5451 /* Set EH language codes */
5452 java_set_exception_lang_code ();
5454 /* Generate function's code */
5455 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
5456 && ! flag_emit_class_files)
5457 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
5459 /* pop out of its parameters */
5460 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5462 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
5464 /* Generate rtl for function exit. */
5465 if (! flag_emit_class_files)
5467 lineno = DECL_SOURCE_LINE_LAST (fndecl);
5468 /* Emit catch-finally clauses */
5470 expand_function_end (input_filename, lineno, 0);
5472 /* Run the optimizers and output assembler code for this function. */
5473 rest_of_compilation (fndecl);
5476 current_function_decl = NULL_TREE;
5477 /* permanent_allocation (1); */
5478 java_parser_context_restore_global ();
5481 /* Record EXPR in the current function block. Complements compound
5482 expression second operand if necessary. */
5485 java_method_add_stmt (fndecl, expr)
5488 return add_stmt_to_block (DECL_FUNCTION_BODY (fndecl), NULL_TREE, expr);
5492 add_stmt_to_block (b, type, stmt)
5495 tree body = BLOCK_EXPR_BODY (b), c;
5497 if (java_error_count)
5500 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
5503 BLOCK_EXPR_BODY (b) = c;
5504 TREE_SIDE_EFFECTS (c) = 1;
5508 /* Add STMT to EXISTING if possible, otherwise create a new
5509 COMPOUND_EXPR and add STMT to it. */
5512 add_stmt_to_compound (existing, type, stmt)
5513 tree existing, type, stmt;
5516 return build (COMPOUND_EXPR, type, existing, stmt);
5521 /* Hold THIS for the scope of the current public method decl. */
5522 static tree current_this;
5524 void java_layout_seen_class_methods ()
5526 tree previous_list = all_class_list;
5527 tree end = NULL_TREE;
5532 for (current = previous_list;
5533 current != end; current = TREE_CHAIN (current))
5534 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
5536 if (previous_list != all_class_list)
5538 end = previous_list;
5539 previous_list = all_class_list;
5546 /* Layout the methods of all classes loaded in one way on an
5547 other. Check methods of source parsed classes. Then reorder the
5548 fields and layout the classes or the type of all source parsed
5552 java_layout_classes ()
5555 int save_error_count = java_error_count;
5557 /* Layout the methods of all classes seen so far */
5558 java_layout_seen_class_methods ();
5559 java_parse_abort_on_error ();
5560 all_class_list = NULL_TREE;
5562 /* Then check the methods of all parsed classes */
5563 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5564 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
5565 CHECK_METHODS (TREE_VALUE (current));
5566 java_parse_abort_on_error ();
5568 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5570 current_class = TREE_TYPE (TREE_VALUE (current));
5572 /* Reverse the fields, but leave the dummy field in front.
5573 Fields are already ordered for Object and Class */
5574 if (TYPE_FIELDS (current_class) && current_class != object_type_node
5575 && current_class != class_type_node)
5577 /* If the dummy field is there, reverse the right fields and
5578 just layout the type for proper fields offset */
5579 if (!DECL_NAME (TYPE_FIELDS (current_class)))
5581 tree fields = TYPE_FIELDS (current_class);
5582 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
5583 TYPE_SIZE (current_class) = NULL_TREE;
5584 layout_type (current_class);
5586 /* We don't have a dummy field, we need to layout the class,
5587 after having reversed the fields */
5590 TYPE_FIELDS (current_class) =
5591 nreverse (TYPE_FIELDS (current_class));
5592 TYPE_SIZE (current_class) = NULL_TREE;
5593 layout_class (current_class);
5597 layout_class (current_class);
5599 /* From now on, the class is considered completely loaded */
5600 CLASS_LOADED_P (current_class) = 1;
5602 /* Error reported by the caller */
5603 if (java_error_count)
5607 /* We might have reloaded classes durign the process of laying out
5608 classes for code generation. We must layout the methods of those
5609 late additions, as constructor checks might use them */
5610 java_layout_seen_class_methods ();
5611 java_parse_abort_on_error ();
5614 /* Expand all methods in all registered classes. */
5617 java_complete_expand_methods ()
5621 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5623 tree class_type = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current));
5626 current_class = TREE_TYPE (current);
5628 /* Initialize a new constant pool */
5629 init_outgoing_cpool ();
5631 /* We want <clinit> (if any) to be processed first. */
5632 decl = tree_last (TYPE_METHODS (class_type));
5633 if (decl && DECL_NAME (decl) == clinit_identifier_node)
5635 tree list = nreverse (TYPE_METHODS (class_type));
5636 list = TREE_CHAIN (list);
5637 TREE_CHAIN (decl) = NULL_TREE;
5638 TYPE_METHODS (class_type) = chainon (decl, nreverse (list));
5641 /* Don't process function bodies in interfaces */
5642 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5643 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5645 current_function_decl = decl;
5646 /* Don't generate debug info on line zero when expanding a
5647 generated constructor. */
5648 if (DECL_CONSTRUCTOR_P (decl) && !DECL_FUNCTION_BODY (decl))
5650 /* If we found errors, it's too dangerous to try to generate
5651 and expand a constructor */
5652 if (!java_error_count)
5654 restore_line_number_status (1);
5655 java_complete_expand_method (decl);
5656 restore_line_number_status (0);
5659 else if (METHOD_ABSTRACT (decl) || METHOD_NATIVE (decl))
5662 java_complete_expand_method (decl);
5665 /* Now verify constructor circularity (stop after the first one
5667 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5668 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5669 if (DECL_CONSTRUCTOR_P (decl) &&
5670 verify_constructor_circularity (decl, decl))
5673 /* Make the class data, register it and run the rest of decl
5674 compilation on it */
5675 if (!java_error_count)
5677 if (flag_emit_class_files)
5678 write_classfile (current_class);
5679 else if (! flag_syntax_only)
5680 finish_class (current_class);
5685 /* Hold a list of catch clauses list. The first element of this list is
5686 the list of the catch clauses of the currently analysed try block. */
5687 static tree currently_caught_type_list;
5689 /* Complete and expand a method. */
5692 java_complete_expand_method (mdecl)
5695 /* Fix constructors before expanding them */
5696 if (DECL_CONSTRUCTOR_P (mdecl))
5697 fix_constructors (mdecl);
5699 /* Expand functions that have a body */
5700 if (DECL_FUNCTION_BODY (mdecl))
5702 tree fbody = DECL_FUNCTION_BODY (mdecl);
5703 tree block_body = BLOCK_EXPR_BODY (fbody);
5704 expand_start_java_method (mdecl);
5707 = (!METHOD_STATIC (mdecl) ?
5708 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
5710 /* Purge the `throws' list of unchecked exceptions */
5711 purge_unchecked_exceptions (mdecl);
5713 /* Install exceptions thrown with `throws' */
5714 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
5716 if (block_body != NULL_TREE)
5718 block_body = java_complete_tree (block_body);
5719 check_for_initialization (block_body);
5721 BLOCK_EXPR_BODY (fbody) = block_body;
5723 if ((block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
5724 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
5725 missing_return_error (current_function_decl);
5727 /* Don't go any further if we've found error(s) during the
5729 if (!java_error_count)
5730 source_end_java_method ();
5733 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
5737 /* Pop the exceptions and sanity check */
5739 if (currently_caught_type_list)
5740 fatal ("Exception list non empty - java_complete_expand_method");
5744 /* Craft a body for default constructor. Patch existing constructor
5745 bodies with call to super() and field initialization statements if
5749 fix_constructors (mdecl)
5752 tree body = DECL_FUNCTION_BODY (mdecl);
5756 /* The constructor body must be crafted by hand. It's the
5757 constructor we defined when we realize we didn't have the
5758 CLASSNAME() constructor */
5762 /* It is an error for the compiler to generate a default
5763 constructor if the superclass doesn't have a constructor that
5764 takes no argument */
5765 if (verify_constructor_super ())
5767 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (current_class));
5768 char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
5769 parse_error_context (lookup_cl (TYPE_NAME (current_class)),
5770 "No constructor matching `%s()' found in "
5771 "class `%s'", n, n);
5774 start_artificial_method_body (mdecl);
5776 /* We don't generate a super constructor invocation if we're
5777 compiling java.lang.Object. build_super_invocation takes care
5779 compound = java_method_add_stmt (mdecl, build_super_invocation ());
5781 end_artificial_method_body (mdecl);
5783 /* Search for an explicit constructor invocation */
5787 tree main_block = BLOCK_EXPR_BODY (body);
5788 tree compound = NULL_TREE;
5791 switch (TREE_CODE (body))
5794 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
5798 case EXPR_WITH_FILE_LOCATION:
5799 body = TREE_OPERAND (body, 0);
5802 body = BLOCK_EXPR_BODY (body);
5808 /* The constructor is missing an invocation of super() */
5810 compound = add_stmt_to_compound (compound, NULL_TREE,
5811 build_super_invocation ());
5813 /* Fix the constructor main block if we're adding extra stmts */
5816 compound = add_stmt_to_compound (compound, NULL_TREE,
5817 BLOCK_EXPR_BODY (main_block));
5818 BLOCK_EXPR_BODY (main_block) = compound;
5823 /* Browse constructors in the super class, searching for a constructor
5824 that doesn't take any argument. Return 0 if one is found, 1
5828 verify_constructor_super ()
5830 tree class = CLASSTYPE_SUPER (current_class);
5837 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
5839 if (DECL_CONSTRUCTOR_P (mdecl)
5840 && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (mdecl))) == end_params_node)
5847 /* Expand finals. */
5850 java_expand_finals ()
5854 /* Generate code for all context remembered for code generation. */
5857 java_expand_classes ()
5859 int save_error_count = java_error_count;
5860 java_parse_abort_on_error ();
5861 if (!(ctxp = ctxp_for_generation))
5863 java_layout_classes ();
5864 java_parse_abort_on_error ();
5866 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
5868 ctxp = ctxp_for_generation;
5869 lang_init_source (2); /* Error msgs have method prototypes */
5870 java_complete_expand_methods (); /* Complete and expand method bodies */
5871 java_parse_abort_on_error ();
5872 java_expand_finals (); /* Expand and check the finals */
5873 java_parse_abort_on_error ();
5874 java_check_final (); /* Check unitialized final */
5875 java_parse_abort_on_error ();
5879 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
5880 a tree list node containing RIGHT. Fore coming RIGHTs will be
5881 chained to this hook. LOCATION contains the location of the
5882 separating `.' operator. */
5885 make_qualified_primary (primary, right, location)
5886 tree primary, right;
5891 /* We want to process THIS . xxx symbolicaly, to keep it consistent
5892 with the way we're processing SUPER. A THIS from a primary as a
5893 different form than a SUPER. Turn THIS into something symbolic */
5894 if (TREE_CODE (primary) == THIS_EXPR)
5896 wfl = build_wfl_node (this_identifier_node, input_filename, 0, 0);
5897 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5898 wfl = make_qualified_name (wfl, right, location);
5899 PRIMARY_P (wfl) = 1;
5902 /* Other non WFL node are wrapped around a WFL */
5903 else if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
5905 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
5906 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5907 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (primary, NULL_TREE);
5912 if (!EXPR_WFL_QUALIFICATION (primary))
5913 EXPR_WFL_QUALIFICATION (primary) =
5914 build_tree_list (primary, NULL_TREE);
5917 EXPR_WFL_LINECOL (right) = location;
5918 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
5919 PRIMARY_P (wfl) = 1;
5923 /* Simple merge of two name separated by a `.' */
5926 merge_qualified_name (left, right)
5930 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
5931 IDENTIFIER_LENGTH (left));
5932 obstack_1grow (&temporary_obstack, '.');
5933 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
5934 IDENTIFIER_LENGTH (right));
5935 node = get_identifier (obstack_base (&temporary_obstack));
5936 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
5937 QUALIFIED_P (node) = 1;
5941 /* Merge the two parts of a qualified name into LEFT. Set the
5942 location information of the resulting node to LOCATION, usually
5943 inherited from the location information of the `.' operator. */
5946 make_qualified_name (left, right, location)
5950 #ifdef USE_COMPONENT_REF
5951 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
5952 EXPR_WFL_LINECOL (node) = location;
5955 tree left_id = EXPR_WFL_NODE (left);
5956 tree right_id = EXPR_WFL_NODE (right);
5959 merge = merge_qualified_name (left_id, right_id);
5961 /* Left wasn't qualified and is now qualified */
5962 if (!QUALIFIED_P (left_id))
5964 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
5965 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
5966 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
5969 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
5970 EXPR_WFL_LINECOL (wfl) = location;
5971 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
5973 EXPR_WFL_NODE (left) = merge;
5978 /* Extract the last identifier component of the qualified in WFL. The
5979 last identifier is removed from the linked list */
5982 cut_identifier_in_qualified (wfl)
5986 tree previous = NULL_TREE;
5987 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
5988 if (!TREE_CHAIN (q))
5991 fatal ("Operating on a non qualified qualified WFL - "
5992 "cut_identifier_in_qualified");
5993 TREE_CHAIN (previous) = NULL_TREE;
5994 return TREE_PURPOSE (q);
5998 /* Resolve the expression name NAME. Return its decl. */
6001 resolve_expression_name (id, orig)
6005 tree name = EXPR_WFL_NODE (id);
6008 /* 6.5.5.1: Simple expression names */
6009 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
6011 /* 15.13.1: NAME can appear within the scope of a local variable
6013 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
6016 /* 15.13.1: NAME can appear within a class declaration */
6019 decl = lookup_field_wrapper (current_class, name);
6022 int fs = FIELD_STATIC (decl);
6023 /* Instance variable (8.3.1.1) can't appear within
6024 static method, static initializer or initializer for
6025 a static variable. */
6026 if (!fs && METHOD_STATIC (current_function_decl))
6029 (id, "Can't make a static reference to nonstatic variable "
6030 "`%s' in class `%s'",
6031 IDENTIFIER_POINTER (name),
6032 IDENTIFIER_POINTER (DECL_NAME
6033 (TYPE_NAME (current_class))));
6034 return error_mark_node;
6036 /* Instance variables can't appear as an argument of
6037 an explicit constructor invocation */
6038 if (!fs && ctxp->explicit_constructor_p)
6041 (id, "Can't reference `%s' before the superclass "
6042 "constructor has been called", IDENTIFIER_POINTER (name));
6043 return error_mark_node;
6046 /* Otherwise build what it takes to access the field */
6047 decl = build_field_ref ((fs ? NULL_TREE : current_this),
6048 current_class, name);
6049 if (fs && !flag_emit_class_files)
6050 decl = build_class_init (current_class, decl);
6051 /* We may be asked to save the real field access node */
6054 /* And we return what we got */
6057 /* Fall down to error report on undefined variable */
6060 /* 6.5.5.2 Qualified Expression Names */
6065 qualify_ambiguous_name (id);
6066 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
6067 /* 15.10.2: Accessing Superclass Members using super */
6068 return resolve_field_access (id, NULL, NULL);
6071 /* We've got an error here */
6072 parse_error_context (id, "Undefined variable `%s'",
6073 IDENTIFIER_POINTER (name));
6075 return error_mark_node;
6078 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
6079 We return something suitable to generate the field access. We also
6080 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
6081 recipient's address can be null. */
6084 resolve_field_access (qual_wfl, field_decl, field_type)
6086 tree *field_decl, *field_type;
6090 tree decl, where_found, type_found;
6092 if (resolve_qualified_expression_name (qual_wfl, &decl,
6093 &where_found, &type_found))
6094 return error_mark_node;
6096 /* Resolve the LENGTH field of an array here */
6097 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
6098 && ! flag_emit_class_files)
6100 tree length = build_java_array_length_access (where_found);
6102 build_java_arraynull_check (type_found, length, int_type_node);
6104 /* We might have been trying to resolve field.method(). In which
6105 case, the resolution is over and decl is the answer */
6106 else if (DECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
6108 else if (DECL_P (decl))
6110 int static_final_found = 0;
6112 type_found = DECL_CONTEXT (decl);
6113 is_static = DECL_P (decl) && FIELD_STATIC (decl);
6114 if (FIELD_FINAL (decl)
6115 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
6116 && DECL_LANG_SPECIFIC (decl)
6117 && DECL_INITIAL (decl))
6119 field_ref = DECL_INITIAL (decl);
6120 static_final_found = 1;
6123 field_ref = build_field_ref ((is_static ? NULL_TREE : where_found),
6124 type_found, DECL_NAME (decl));
6125 if (field_ref == error_mark_node)
6126 return error_mark_node;
6127 if (is_static && !static_final_found && !flag_emit_class_files)
6129 field_ref = build_class_init (type_found, field_ref);
6130 /* If the static field was identified by an expression that
6131 needs to be generated, make the field access a compound
6132 expression whose first part of the evaluation of the
6133 field selector part. */
6134 if (where_found && TREE_CODE (where_found) != TYPE_DECL
6135 && TREE_CODE (where_found) != RECORD_TYPE)
6137 tree type = QUAL_DECL_TYPE (field_ref);
6138 field_ref = build (COMPOUND_EXPR, type, where_found, field_ref);
6148 *field_type = (QUAL_DECL_TYPE (decl) ?
6149 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
6153 /* 6.5.5.2: Qualified Expression Names */
6156 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
6158 tree *found_decl, *type_found, *where_found;
6160 int from_type = 0; /* Field search initiated from a type */
6161 int from_super = 0, from_cast = 0;
6162 int previous_call_static = 0;
6164 tree decl = NULL_TREE, type = NULL_TREE, q;
6165 *type_found = *where_found = NULL_TREE;
6167 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
6169 tree qual_wfl = QUAL_WFL (q);
6171 /* 15.10.1 Field Access Using a Primary */
6172 switch (TREE_CODE (qual_wfl))
6175 case NEW_CLASS_EXPR:
6176 /* If the access to the function call is a non static field,
6177 build the code to access it. */
6178 if (DECL_P (decl) && !FIELD_STATIC (decl))
6180 decl = maybe_access_field (decl, *where_found,
6181 DECL_CONTEXT (decl));
6182 if (decl == error_mark_node)
6185 /* And code for the function call */
6186 if (complete_function_arguments (qual_wfl))
6188 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
6189 CALL_USING_SUPER (qual_wfl) = 1;
6191 patch_method_invocation (qual_wfl, decl, type, &is_static, NULL);
6192 if (*where_found == error_mark_node)
6194 *type_found = type = QUAL_DECL_TYPE (*where_found);
6196 /* If the previous call was static and this one is too,
6197 build a compound expression to hold the two (because in
6198 that case, previous function calls aren't transported as
6199 forcoming function's argument. */
6200 if (previous_call_static && is_static)
6202 decl = build (COMPOUND_EXPR, type, decl, *where_found);
6203 TREE_SIDE_EFFECTS (decl) = 1;
6207 previous_call_static = is_static;
6208 decl = *where_found;
6212 case NEW_ARRAY_EXPR:
6213 *where_found = decl = java_complete_tree (qual_wfl);
6214 if (decl == error_mark_node)
6216 *type_found = type = QUAL_DECL_TYPE (decl);
6217 CLASS_LOADED_P (type) = 1;
6221 *where_found = decl = java_complete_tree (qual_wfl);
6222 if (decl == error_mark_node)
6224 *type_found = type = QUAL_DECL_TYPE (decl);
6228 case CONDITIONAL_EXPR:
6230 *where_found = decl = java_complete_tree (qual_wfl);
6231 if (decl == error_mark_node)
6233 *type_found = type = QUAL_DECL_TYPE (decl);
6237 /* If the access to the function call is a non static field,
6238 build the code to access it. */
6239 if (DECL_P (decl) && !FIELD_STATIC (decl))
6241 decl = maybe_access_field (decl, *where_found, type);
6242 if (decl == error_mark_node)
6245 /* And code for the array reference expression */
6246 decl = java_complete_tree (qual_wfl);
6247 if (decl == error_mark_node)
6249 type = QUAL_DECL_TYPE (decl);
6253 /* Fix for -Wall Just go to the next statement. Don't
6257 /* If we fall here, we weren't processing a (static) function call. */
6258 previous_call_static = 0;
6260 /* It can be the keyword THIS */
6261 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
6266 (wfl, "Keyword `this' used outside allowed context");
6269 /* We have to generate code for intermediate acess */
6270 *where_found = decl = current_this;
6271 *type_found = type = QUAL_DECL_TYPE (decl);
6275 /* 15.10.2 Accessing Superclass Members using SUPER */
6276 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
6279 /* Check on the restricted use of SUPER */
6280 if (METHOD_STATIC (current_function_decl)
6281 || current_class == object_type_node)
6284 (wfl, "Keyword `super' used outside allowed context");
6287 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
6288 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
6289 CLASSTYPE_SUPER (current_class),
6290 build_this (EXPR_WFL_LINECOL (qual_wfl)));
6291 *where_found = decl = java_complete_tree (node);
6292 if (decl == error_mark_node)
6294 *type_found = type = QUAL_DECL_TYPE (decl);
6295 from_super = from_type = 1;
6299 /* 15.13.1: Can't search for field name in packages, so we
6300 assume a variable/class name was meant. */
6301 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
6303 tree name = resolve_package (wfl, &q);
6306 *where_found = decl = resolve_no_layout (name, qual_wfl);
6307 /* We wan't to be absolutely that the class is laid
6308 out. We're going to search something inside it. */
6309 *type_found = type = TREE_TYPE (decl);
6310 layout_class (type);
6312 /* Should be a list, really. FIXME */
6313 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 1;
6314 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 0;
6318 if (from_super || from_cast)
6320 ((from_cast ? qual_wfl : wfl),
6321 "No variable `%s' defined in class `%s'",
6322 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6323 lang_printable_name (type, 0));
6326 (qual_wfl, "Undefined variable or class name: `%s'",
6327 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
6332 /* We have a type name. It's been already resolved when the
6333 expression was qualified. */
6334 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
6336 if (!(decl = QUAL_RESOLUTION (q)))
6337 return 1; /* Error reported already */
6339 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
6342 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
6343 java_accstring_lookup (get_access_flags_from_decl (decl)),
6344 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6345 IDENTIFIER_POINTER (DECL_NAME (decl)),
6346 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6349 check_deprecation (qual_wfl, decl);
6351 type = TREE_TYPE (decl);
6354 /* We resolve and expression name */
6359 /* If there exists an early resolution, use it. That occurs
6360 only once and we know that there are more things to
6361 come. Don't do that when processing something after SUPER
6362 (we need more thing to be put in place below */
6363 if (!from_super && QUAL_RESOLUTION (q))
6365 decl = QUAL_RESOLUTION (q);
6368 if (!FIELD_STATIC (decl))
6369 *where_found = current_this;
6372 *where_found = TREE_TYPE (decl);
6373 if (TREE_CODE (*where_found) == POINTER_TYPE)
6374 *where_found = TREE_TYPE (*where_found);
6379 /* We have to search for a field, knowing the type of its
6380 container. The flag FROM_TYPE indicates that we resolved
6381 the last member of the expression as a type name, which
6382 means that for the resolution of this field, we'll look
6383 for other errors than if it was resolved as a member of
6388 tree field_decl_type; /* For layout */
6390 if (!from_type && !JREFERENCE_TYPE_P (type))
6393 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
6394 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6395 lang_printable_name (type, 0),
6396 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6401 lookup_field_wrapper (type, EXPR_WFL_NODE (qual_wfl))))
6404 (qual_wfl, "No variable `%s' defined in class `%s'",
6405 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6406 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6410 /* Layout the type of field_decl, since we may need
6411 it. Don't do primitive types or loaded classes. The
6412 situation of non primitive arrays may not handled
6413 properly here. FIXME */
6414 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
6415 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
6417 field_decl_type = TREE_TYPE (field_decl);
6418 if (!JPRIMITIVE_TYPE_P (field_decl_type)
6419 && !CLASS_LOADED_P (field_decl_type)
6420 && !TYPE_ARRAY_P (field_decl_type))
6421 resolve_and_layout (field_decl_type, NULL_TREE);
6422 if (TYPE_ARRAY_P (field_decl_type))
6423 CLASS_LOADED_P (field_decl_type) = 1;
6425 /* Check on accessibility here */
6426 if (not_accessible_p (type, field_decl, from_super))
6430 "Can't access %s field `%s.%s' from `%s'",
6431 java_accstring_lookup
6432 (get_access_flags_from_decl (field_decl)),
6433 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6434 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
6436 (DECL_NAME (TYPE_NAME (current_class))));
6439 check_deprecation (qual_wfl, field_decl);
6441 /* There are things to check when fields are accessed
6442 from type. There are no restrictions on a static
6443 declaration of the field when it is accessed from an
6445 is_static = FIELD_STATIC (field_decl);
6446 if (!from_super && from_type
6447 && !TYPE_INTERFACE_P (type) && !is_static)
6450 (qual_wfl, "Can't make a static reference to nonstatic "
6451 "variable `%s' in class `%s'",
6452 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6453 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6456 from_cast = from_super = 0;
6458 /* If we need to generate something to get a proper
6459 handle on what this field is accessed from, do it
6463 decl = maybe_access_field (decl, *where_found, *type_found);
6464 if (decl == error_mark_node)
6468 /* We want to keep the location were found it, and the type
6470 *where_found = decl;
6473 /* This is the decl found and eventually the next one to
6478 type = QUAL_DECL_TYPE (decl);
6485 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
6486 can't be accessed from REFERENCE (a record type). */
6488 int not_accessible_p (reference, member, from_super)
6489 tree reference, member;
6492 int access_flag = get_access_flags_from_decl (member);
6494 /* Access always granted for members declared public */
6495 if (access_flag & ACC_PUBLIC)
6498 /* Check access on protected members */
6499 if (access_flag & ACC_PROTECTED)
6501 /* Access granted if it occurs from within the package
6502 containing the class in which the protected member is
6504 if (class_in_current_package (DECL_CONTEXT (member)))
6507 /* If accessed with the form `super.member', then access is granted */
6511 /* Otherwise, access is granted if occuring from the class where
6512 member is declared or a subclass of it */
6513 if (inherits_from_p (reference, current_class))
6518 /* Check access on private members. Access is granted only if it
6519 occurs from within the class in witch it is declared */
6520 if (access_flag & ACC_PRIVATE)
6521 return (current_class == DECL_CONTEXT (member) ? 0 : 1);
6523 /* Default access are permitted only when occuring within the
6524 package in which the type (REFERENCE) is declared. In other words,
6525 REFERENCE is defined in the current package */
6527 return !class_in_current_package (reference);
6529 /* Otherwise, access is granted */
6533 /* Test deprecated decl access. */
6535 check_deprecation (wfl, decl)
6538 char *file = DECL_SOURCE_FILE (decl);
6539 /* Complain if the field is deprecated and the file it was defined
6540 in isn't compiled at the same time the file which contains its
6542 if (DECL_DEPRECATED (decl)
6543 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
6546 switch (TREE_CODE (decl))
6549 strcpy (the, "method");
6552 strcpy (the, "field");
6555 strcpy (the, "class");
6558 fatal ("unexpected DECL code - check_deprecation");
6560 parse_warning_context
6561 (wfl, "The %s `%s' in class `%s' has been deprecated",
6562 the, lang_printable_name (decl, 0),
6563 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
6567 /* Returns 1 if class was declared in the current package, 0 otherwise */
6570 class_in_current_package (class)
6573 static tree cache = NULL_TREE;
6580 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
6582 /* If the current package is empty and the name of CLASS is
6583 qualified, class isn't in the current package. If there is a
6584 current package and the name of the CLASS is not qualified, class
6585 isn't in the current package */
6586 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
6589 /* If there is not package and the name of CLASS isn't qualified,
6590 they belong to the same unnamed package */
6591 if (!ctxp->package && !qualified_flag)
6594 /* Compare the left part of the name of CLASS with the package name */
6595 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
6596 if (ctxp->package == left)
6604 /* This function may generate code to access DECL from WHERE. This is
6605 done only if certain conditions meet. */
6608 maybe_access_field (decl, where, type)
6609 tree decl, where, type;
6611 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
6612 && !FIELD_STATIC (decl))
6613 decl = build_field_ref (where ? where : current_this,
6614 (type ? type : DECL_CONTEXT (decl)),
6619 /* Build a method invocation, by patching PATCH. If non NULL
6620 and according to the situation, PRIMARY and WHERE may be
6621 used. IS_STATIC is set to 1 if the invoked function is static. */
6624 patch_method_invocation (patch, primary, where, is_static, ret_decl)
6625 tree patch, primary, where;
6629 tree wfl = TREE_OPERAND (patch, 0);
6630 tree args = TREE_OPERAND (patch, 1);
6631 tree name = EXPR_WFL_NODE (wfl);
6633 int is_static_flag = 0;
6634 int is_super_init = 0;
6635 tree this_arg = NULL_TREE;
6637 /* Should be overriden if everything goes well. Otherwise, if
6638 something fails, it should keep this value. It stop the
6639 evaluation of a bogus assignment. See java_complete_tree,
6640 MODIFY_EXPR: for the reasons why we sometimes want to keep on
6641 evaluating an assignment */
6642 TREE_TYPE (patch) = error_mark_node;
6644 /* Since lookup functions are messing with line numbers, save the
6646 java_parser_context_save_global ();
6648 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
6650 /* Resolution of qualified name, excluding constructors */
6651 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
6653 tree class_decl, identifier, identifier_wfl;
6654 /* Extract the last IDENTIFIER of the qualified
6655 expression. This is a wfl and we will use it's location
6656 data during error report. */
6657 identifier_wfl = cut_identifier_in_qualified (wfl);
6658 identifier = EXPR_WFL_NODE (identifier_wfl);
6660 /* Given the context, IDENTIFIER is syntactically qualified
6661 as a MethodName. We need to qualify what's before */
6662 qualify_ambiguous_name (wfl);
6664 /* Package resolution are erroneous */
6665 if (RESOLVE_PACKAGE_NAME_P (wfl))
6668 breakdown_qualified (&remainder, NULL, EXPR_WFL_NODE (wfl));
6669 parse_error_context (wfl, "Can't search method `%s' in package "
6670 "`%s'",IDENTIFIER_POINTER (identifier),
6671 IDENTIFIER_POINTER (remainder));
6672 PATCH_METHOD_RETURN_ERROR ();
6674 /* We're resolving a call from a type */
6675 else if (RESOLVE_TYPE_NAME_P (wfl))
6677 tree decl = QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl));
6678 tree name = DECL_NAME (decl);
6681 class_decl = resolve_and_layout (name, wfl);
6682 if (CLASS_INTERFACE (decl))
6685 (identifier_wfl, "Can't make static reference to method "
6686 "`%s' in interface `%s'", IDENTIFIER_POINTER (identifier),
6687 IDENTIFIER_POINTER (name));
6688 PATCH_METHOD_RETURN_ERROR ();
6690 /* Look the method up in the type selector. The method ought
6692 type = TREE_TYPE (class_decl);
6693 list = lookup_method_invoke (0, wfl, type, identifier, args);
6694 if (list && !METHOD_STATIC (list))
6696 char *fct_name = strdup (lang_printable_name (list, 0));
6699 "Can't make static reference to method `%s %s' in class `%s'",
6700 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6701 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6703 PATCH_METHOD_RETURN_ERROR ();
6705 args = nreverse (args);
6707 /* We're resolving an expression name */
6712 /* 1- Find the field to which the call applies */
6713 field = resolve_field_access (wfl, NULL, &type);
6714 if (field == error_mark_node)
6715 PATCH_METHOD_RETURN_ERROR ();
6716 /* field is used in lieu of a primary. It alows us not to
6717 report errors on erroneous use of `this' in
6721 /* 2- Do the layout of the class where the last field
6722 was found, so we can search it. */
6723 class_decl = resolve_and_layout (type, NULL_TREE);
6724 if (class_decl != NULL_TREE)
6725 type = TREE_TYPE (class_decl);
6727 /* 3- Retrieve a filtered list of method matches, Refine
6728 if necessary. In any cases, point out errors. */
6729 list = lookup_method_invoke (0, identifier_wfl, type,
6732 /* 4- Add the field as an argument */
6733 args = nreverse (args);
6737 /* IDENTIFIER_WFL will be used to report any problem further */
6738 wfl = identifier_wfl;
6740 /* Resolution of simple names, names generated after a primary: or
6744 tree class_to_search;
6745 int lc; /* Looking for Constructor */
6747 /* We search constructor in their target class */
6748 if (CALL_CONSTRUCTOR_P (patch))
6750 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6751 class_to_search = EXPR_WFL_NODE (wfl);
6752 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6753 this_identifier_node)
6754 class_to_search = NULL_TREE;
6755 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6756 super_identifier_node)
6759 if (CLASSTYPE_SUPER (current_class))
6761 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
6764 parse_error_context (wfl, "Can't invoke super constructor "
6765 "on java.lang.Object");
6766 PATCH_METHOD_RETURN_ERROR ();
6770 /* Class to search is NULL if we're searching the current one */
6771 if (class_to_search)
6773 class_to_search = resolve_and_layout (class_to_search,
6775 if (!class_to_search)
6778 (wfl, "Class `%s' not found in type declaration",
6779 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6780 PATCH_METHOD_RETURN_ERROR ();
6783 /* Can't instantiate an abstract class, but we can
6784 invoke it's constructor. It's use within the `new'
6785 context is denied here. */
6786 if (CLASS_ABSTRACT (class_to_search)
6787 && TREE_CODE (patch) == NEW_CLASS_EXPR)
6790 (wfl, "Class `%s' is an abstract class. It can't be "
6791 "instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6792 PATCH_METHOD_RETURN_ERROR ();
6794 class_to_search = TREE_TYPE (class_to_search);
6797 class_to_search = current_class;
6800 /* This is a regular search in the local class, unless an
6801 alternate class is specified. */
6804 class_to_search = (where ? where : current_class);
6808 /* NAME is a simple identifier or comes from a primary. Search
6809 in the class whose declaration contain the method being
6811 resolve_and_layout (class_to_search, NULL_TREE);
6812 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
6814 /* Don't continue if no method were found, as the next statement
6815 can't be executed then. */
6817 PATCH_METHOD_RETURN_ERROR ();
6819 /* Check for static reference if non static methods */
6820 if (check_for_static_method_reference (wfl, patch, list,
6821 class_to_search, primary))
6822 PATCH_METHOD_RETURN_ERROR ();
6824 /* Non static methods are called with the current object extra
6825 argument. If patch a `new TYPE()', the argument is the value
6826 returned by the object allocator. If method is resolved as a
6827 primary, use the primary otherwise use the current THIS. */
6828 args = nreverse (args);
6829 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
6830 this_arg = primary ? primary : current_this;
6833 /* Merge point of all resolution schemes. If we have nothing, this
6834 is an error, already signaled */
6836 PATCH_METHOD_RETURN_ERROR ();
6838 /* Check accessibility, position the is_static flag, build and
6840 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
6842 char *fct_name = strdup (lang_printable_name (list, 0));
6844 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
6845 java_accstring_lookup (get_access_flags_from_decl (list)),
6846 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6847 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
6848 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6850 PATCH_METHOD_RETURN_ERROR ();
6852 check_deprecation (wfl, list);
6854 is_static_flag = METHOD_STATIC (list);
6855 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
6856 args = tree_cons (NULL_TREE, this_arg, args);
6858 /* In the context of an explicit constructor invocation, we can't
6859 invoke any method relying on `this'. Exceptions are: we're
6860 invoking a static function, primary exists and is not the current
6861 this, we're creating a new object. */
6862 if (ctxp->explicit_constructor_p
6864 && (!primary || primary == current_this)
6865 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
6868 (wfl, "Can't reference `this' before the superclass constructor has "
6870 PATCH_METHOD_RETURN_ERROR ();
6872 java_parser_context_restore_global ();
6874 *is_static = is_static_flag;
6875 /* Sometimes, we want the decl of the selected method. Such as for
6879 patch = patch_invoke (patch, list, args);
6880 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
6882 /* Generate the code used to initialize fields declared with an
6883 initialization statement. For now, it returns a call the the
6884 artificial function $finit$, if required. */
6887 build_method_invocation (build_expr_wfl (finit_identifier_node,
6888 input_filename, 0, 0),
6890 patch = build (COMPOUND_EXPR, void_type_node, patch,
6891 java_complete_tree (finit_call));
6892 CAN_COMPLETE_NORMALLY (patch) = 1;
6897 /* Check that we're not trying to do a static reference to a method in
6898 non static method. Return 1 if it's the case, 0 otherwise. */
6901 check_for_static_method_reference (wfl, node, method, where, primary)
6902 tree wfl, node, method, where, primary;
6904 if (METHOD_STATIC (current_function_decl)
6905 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
6907 char *fct_name = strdup (lang_printable_name (method, 0));
6909 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
6910 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
6911 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
6918 /* Patch an invoke expression METHOD and ARGS, based on its invocation
6922 patch_invoke (patch, method, args)
6923 tree patch, method, args;
6926 tree original_call, t, ta;
6928 /* Last step for args: convert build-in types. If we're dealing with
6929 a new TYPE() type call, the first argument to the constructor
6930 isn't found in the incomming argument list, but delivered by
6932 t = TYPE_ARG_TYPES (TREE_TYPE (method));
6933 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6935 for (ta = args; t != end_params_node && ta;
6936 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
6937 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
6938 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
6939 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
6941 if (flag_emit_class_files)
6945 tree signature = build_java_signature (TREE_TYPE (method));
6946 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
6948 case INVOKE_VIRTUAL:
6949 dtable = invoke_build_dtable (0, args);
6950 func = build_invokevirtual (dtable, method);
6955 func = build_known_method_ref (method, TREE_TYPE (method),
6956 DECL_CONTEXT (method),
6960 case INVOKE_INTERFACE:
6961 dtable = invoke_build_dtable (1, args);
6962 func = build_invokeinterface (dtable, DECL_NAME (method), signature);
6966 fatal ("internal error - unknown invocation_mode result");
6969 /* Ensure self_type is initialized, (invokestatic). FIXME */
6970 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
6973 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
6974 TREE_OPERAND (patch, 0) = func;
6975 TREE_OPERAND (patch, 1) = args;
6976 original_call = patch;
6978 /* We're processing a `new TYPE ()' form. New is called an its
6979 returned value is the first argument to the constructor. We build
6980 a COMPOUND_EXPR and use saved expression so that the overall NEW
6981 expression value is a pointer to a newly created and initialized
6983 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
6985 tree class = DECL_CONTEXT (method);
6986 tree c1, saved_new, size, new;
6987 if (flag_emit_class_files)
6989 TREE_TYPE (patch) = build_pointer_type (class);
6992 if (!TYPE_SIZE (class))
6993 safe_layout_class (class);
6994 size = size_in_bytes (class);
6995 new = build (CALL_EXPR, promote_type (class),
6996 build_address_of (alloc_object_node),
6997 tree_cons (NULL_TREE, build_class_ref (class),
6998 build_tree_list (NULL_TREE,
6999 size_in_bytes (class))),
7001 saved_new = save_expr (new);
7002 c1 = build_tree_list (NULL_TREE, saved_new);
7003 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
7004 TREE_OPERAND (original_call, 1) = c1;
7005 TREE_SET_CODE (original_call, CALL_EXPR);
7006 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
7012 invocation_mode (method, super)
7016 int access = get_access_flags_from_decl (method);
7019 return INVOKE_SUPER;
7021 if (access & ACC_STATIC || access & ACC_FINAL || access & ACC_PRIVATE)
7022 return INVOKE_STATIC;
7024 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
7025 return INVOKE_STATIC;
7027 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
7028 return INVOKE_INTERFACE;
7030 if (DECL_CONSTRUCTOR_P (method))
7031 return INVOKE_STATIC;
7033 return INVOKE_VIRTUAL;
7036 /* Retrieve a refined list of matching methods. It covers the step
7037 15.11.2 (Compile-Time Step 2) */
7040 lookup_method_invoke (lc, cl, class, name, arg_list)
7043 tree class, name, arg_list;
7045 tree atl = end_params_node; /* Arg Type List */
7046 tree method, signature, list, node;
7047 char *candidates; /* Used for error report */
7049 /* Fix the arguments */
7050 for (node = arg_list; node; node = TREE_CHAIN (node))
7052 tree current_arg = TREE_VALUE (node);
7053 /* Integer constant 0 passed as itself, not as a type */
7054 if (current_arg != integer_zero_node)
7055 current_arg = TREE_TYPE (TREE_VALUE (node));
7056 /* Non primitive type may have to be resolved */
7057 if (current_arg != integer_zero_node
7058 && !JPRIMITIVE_TYPE_P (current_arg))
7059 resolve_and_layout (current_arg, NULL_TREE);
7061 if (TREE_CODE (current_arg) == RECORD_TYPE)
7062 current_arg = promote_type (current_arg);
7063 atl = tree_cons (NULL_TREE, current_arg, atl);
7066 /* Find all candidates and then refine the list, searching for the
7067 most specific method. */
7068 list = find_applicable_accessible_methods_list (lc, class, name, atl);
7069 list = find_most_specific_methods_list (list);
7070 if (list && !TREE_CHAIN (list))
7071 return TREE_VALUE (list);
7073 /* Issue an error. List candidates if any. Candidates are listed
7074 only if accessible (non accessible methods may end-up here for
7075 the sake of a better error report). */
7080 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
7081 for (current = list; current; current = TREE_CHAIN (current))
7083 tree cm = TREE_VALUE (current);
7085 if (!cm || not_accessible_p (class, cm, 0))
7088 (string, " `%s' in `%s'%s",
7089 get_printable_method_name (cm),
7090 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
7091 (TREE_CHAIN (current) ? "\n" : ""));
7092 obstack_grow (&temporary_obstack, string, strlen (string));
7094 obstack_1grow (&temporary_obstack, '\0');
7095 candidates = obstack_finish (&temporary_obstack);
7097 /* Issue the error message */
7098 for (node = atl; node; node = TREE_CHAIN (node))
7099 if (TREE_VALUE (node) == integer_zero_node)
7100 TREE_VALUE (node) = long_type_node;
7101 method = make_node (FUNCTION_TYPE);
7102 TYPE_ARG_TYPES (method) = atl;
7103 signature = build_java_argument_signature (method);
7104 parse_error_context (cl, "Can't find %s `%s(%s)' in class `%s'%s",
7105 (lc ? "constructor" : "method"),
7107 IDENTIFIER_POINTER(DECL_NAME (TYPE_NAME (class))) :
7108 IDENTIFIER_POINTER (name)),
7109 IDENTIFIER_POINTER (signature),
7110 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))),
7111 (candidates ? candidates : ""));
7115 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
7116 when we're looking for a constructor. */
7119 find_applicable_accessible_methods_list (lc, class, name, arglist)
7121 tree class, name, arglist;
7124 tree list = NULL_TREE, all_list = NULL_TREE;
7126 while (class != NULL_TREE)
7128 for (method = TYPE_METHODS (class);
7129 method != NULL_TREE; method = TREE_CHAIN (method))
7131 if (lc && !DECL_CONSTRUCTOR_P (method))
7133 else if (!lc && (DECL_CONSTRUCTOR_P (method)
7134 || (GET_METHOD_NAME (method) != name)))
7137 if (argument_types_convertible (method, arglist))
7139 /* Retain accessible methods only */
7140 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
7142 list = tree_cons (NULL_TREE, method, list);
7144 /* Also retain all selected method here */
7145 all_list = tree_cons (NULL_TREE, method, list);
7148 /* When dealing with constructor, stop here, otherwise search
7150 class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
7152 /* Either return the list obtained or all selected (but
7153 inaccessible) methods for better error report. */
7154 return (!list ? all_list : list);
7157 /* 15.11.2.2 Choose the Most Specific Method */
7160 find_most_specific_methods_list (list)
7164 tree current, new_list = NULL_TREE;
7165 for (current = list; current; current = TREE_CHAIN (current))
7168 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
7170 for (method = list; method; method = TREE_CHAIN (method))
7172 /* Don't test a method against itself */
7173 if (method == current)
7176 /* Compare arguments and location where method where declared */
7177 if (argument_types_convertible (TREE_VALUE (method),
7178 TREE_VALUE (current))
7179 && valid_method_invocation_conversion_p
7180 (DECL_CONTEXT (TREE_VALUE (method)),
7181 DECL_CONTEXT (TREE_VALUE (current))))
7183 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
7184 max = (v > max ? v : max);
7189 /* Review the list and select the maximally specific methods */
7190 for (current = list; current; current = TREE_CHAIN (current))
7191 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7192 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7194 /* If we can't find one, lower expectations and try to gather multiple
7195 maximally specific methods */
7200 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7201 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7209 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
7210 converted by method invocation conversion (5.3) to the type of the
7211 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
7212 to change less often than M1. */
7215 argument_types_convertible (m1, m2_or_arglist)
7216 tree m1, m2_or_arglist;
7218 static tree m2_arg_value = NULL_TREE;
7219 static tree m2_arg_cache = NULL_TREE;
7221 register tree m1_arg, m2_arg;
7223 m1_arg = TYPE_ARG_TYPES (TREE_TYPE (m1));
7224 if (!METHOD_STATIC (m1))
7225 m1_arg = TREE_CHAIN (m1_arg);
7227 if (m2_arg_value == m2_or_arglist)
7228 m2_arg = m2_arg_cache;
7231 /* M2_OR_ARGLIST can be a function DECL or a raw list of
7233 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
7235 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
7236 if (!METHOD_STATIC (m2_or_arglist))
7237 m2_arg = TREE_CHAIN (m2_arg);
7240 m2_arg = m2_or_arglist;
7242 m2_arg_value = m2_or_arglist;
7243 m2_arg_cache = m2_arg;
7246 while (m1_arg != end_params_node && m2_arg != end_params_node)
7248 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
7249 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
7250 TREE_VALUE (m2_arg)))
7252 m1_arg = TREE_CHAIN (m1_arg);
7253 m2_arg = TREE_CHAIN (m2_arg);
7255 return m1_arg == end_params_node && m2_arg == end_params_node;
7258 /* Qualification routines */
7261 qualify_ambiguous_name (id)
7264 tree qual, qual_wfl, name, decl, ptr_type, saved_current_class;
7265 int again, super_found = 0, this_found = 0, new_array_found = 0;
7267 /* We first qualify the first element, then derive qualification of
7268 others based on the first one. If the first element is qualified
7269 by a resolution (field or type), this resolution is stored in the
7270 QUAL_RESOLUTION of the qual element being examined. We need to
7271 save the current_class since the use of SUPER might change the
7273 saved_current_class = current_class;
7274 qual = EXPR_WFL_QUALIFICATION (id);
7277 /* Simple qualified expression feature a qual_wfl that is a
7278 WFL. Expression derived from a primary feature more complicated
7279 things like a CALL_EXPR. Expression from primary need to be
7280 worked out to extract the part on which the qualification will
7282 qual_wfl = QUAL_WFL (qual);
7283 switch (TREE_CODE (qual_wfl))
7286 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7287 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
7289 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
7290 qual_wfl = QUAL_WFL (qual);
7293 case NEW_ARRAY_EXPR:
7294 qual = TREE_CHAIN (qual);
7295 new_array_found = again = 1;
7297 case NEW_CLASS_EXPR:
7299 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7302 while (TREE_CODE (qual_wfl) == ARRAY_REF)
7303 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7306 /* Fix for -Wall. Just break doing nothing */
7309 name = EXPR_WFL_NODE (qual_wfl);
7310 ptr_type = current_class;
7312 /* If we have a THIS (from a primary), we set the context accordingly */
7313 if (name == this_identifier_node)
7315 qual = TREE_CHAIN (qual);
7316 qual_wfl = QUAL_WFL (qual);
7317 if (TREE_CODE (qual_wfl) == CALL_EXPR)
7320 name = EXPR_WFL_NODE (qual_wfl);
7323 /* If we have a SUPER, we set the context accordingly */
7324 if (name == super_identifier_node)
7326 current_class = CLASSTYPE_SUPER (ptr_type);
7327 /* Check that there is such a thing as a super class. If not,
7328 return. The error will be caught later on, during the
7332 current_class = saved_current_class;
7335 qual = TREE_CHAIN (qual);
7336 /* Do one more interation to set things up */
7337 super_found = again = 1;
7339 /* Loop one more time if we're dealing with ?: or a string constant */
7340 if (TREE_CODE (qual_wfl) == CONDITIONAL_EXPR
7341 || TREE_CODE (qual_wfl) == STRING_CST)
7343 qual = TREE_CHAIN (qual);
7344 qual_wfl = QUAL_WFL (qual);
7349 /* If name appears within the scope of a location variable
7350 declaration or parameter declaration, then it is an expression
7351 name. We don't carry this test out if we're in the context of the
7352 use of SUPER or THIS */
7353 if (!this_found && !super_found && (decl = IDENTIFIER_LOCAL_VALUE (name)))
7355 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7356 QUAL_RESOLUTION (qual) = decl;
7359 /* If within the class/interface NAME was found to be used there
7360 exists a (possibly inherited) field named NAME, then this is an
7361 expression name. If we saw a NEW_ARRAY_EXPR before and want to
7362 address length, it is OK. */
7363 else if ((decl = lookup_field_wrapper (ptr_type, name))
7364 || (new_array_found && name == length_identifier_node))
7366 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7367 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
7370 /* We reclassify NAME as a type name if:
7371 - NAME is a class/interface declared within the compilation
7372 unit containing NAME,
7373 - NAME is imported via a single-type-import declaration,
7374 - NAME is declared in an another compilation unit of the package
7375 of the compilation unit containing NAME,
7376 - NAME is declared by exactly on type-import-on-demand declaration
7377 of the compilation unit containing NAME. */
7378 else if ((decl = resolve_and_layout (name, NULL_TREE)))
7380 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
7381 QUAL_RESOLUTION (qual) = decl;
7384 /* Method call are expression name */
7385 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
7386 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF)
7387 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7389 /* Check here that NAME isn't declared by more than one
7390 type-import-on-demand declaration of the compilation unit
7391 containing NAME. FIXME */
7393 /* Otherwise, NAME is reclassified as a package name */
7395 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
7397 /* Propagate the qualification accross other components of the
7399 for (qual = TREE_CHAIN (qual); qual;
7400 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
7402 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7403 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
7405 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
7408 /* Store the global qualification for the ambiguous part of ID back
7410 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
7411 RESOLVE_EXPRESSION_NAME_P (id) = 1;
7412 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
7413 RESOLVE_TYPE_NAME_P (id) = 1;
7414 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7415 RESOLVE_PACKAGE_NAME_P (id) = 1;
7417 /* Restore the current class */
7418 current_class = saved_current_class;
7422 breakdown_qualified (left, right, source)
7423 tree *left, *right, source;
7425 char *p = IDENTIFIER_POINTER (source), *base;
7426 int l = IDENTIFIER_LENGTH (source);
7428 /* Breakdown NAME into REMAINDER . IDENTIFIER */
7431 while (*p != '.' && p != base)
7434 /* We didn't find a '.'. Return an error */
7440 *right = get_identifier (p+1);
7441 *left = get_identifier (IDENTIFIER_POINTER (source));
7447 /* Patch tree nodes in a function body. When a BLOCK is found, push
7448 local variable decls if present.
7449 Same as java_complete_lhs, but does resolve static finals to values. */
7452 java_complete_tree (node)
7455 node = java_complete_lhs (node);
7456 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7457 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE)
7459 tree value = DECL_INITIAL (node);
7460 DECL_INITIAL (node) = NULL_TREE;
7461 value = fold_constant_for_init (value, node);
7462 DECL_INITIAL (node) = value;
7463 if (value != NULL_TREE)
7469 /* Patch tree nodes in a function body. When a BLOCK is found, push
7470 local variable decls if present.
7471 Same as java_complete_tree, but does not resolve static finals to values. */
7474 java_complete_lhs (node)
7477 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
7480 /* CONVERT_EXPR always has its type set, even though it needs to be
7482 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
7485 /* The switch block implements cases processing container nodes
7486 first. Contained nodes are always written back. Leaves come
7487 next and return a value. */
7488 switch (TREE_CODE (node))
7492 /* 1- Block section.
7493 Set the local values on decl names so we can identify them
7494 faster when they're referenced. At that stage, identifiers
7495 are legal so we don't check for declaration errors. */
7496 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7498 DECL_CONTEXT (cn) = current_function_decl;
7499 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
7500 INITIALIZED_P (cn) = 0;
7502 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
7503 CAN_COMPLETE_NORMALLY (node) = 1;
7506 tree stmt = BLOCK_EXPR_BODY (node);
7509 if (TREE_CODE (stmt) == COMPOUND_EXPR)
7511 /* Re-order from (((A; B); C); ...; Z) to
7512 (A; (B; (C ; (...; Z)))).
7513 This makes it easier to scan the statements left-to-right
7514 without using recursion (which might overflow the stack
7515 if the block has many statements. */
7518 tree left = TREE_OPERAND (stmt, 0);
7519 if (TREE_CODE (left) != COMPOUND_EXPR)
7521 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
7522 TREE_OPERAND (left, 1) = stmt;
7525 BLOCK_EXPR_BODY (node) = stmt;
7528 /* Now do the actual complete, without deep recursion for
7530 ptr = &BLOCK_EXPR_BODY (node);
7531 while (TREE_CODE (*ptr) == COMPOUND_EXPR)
7533 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
7534 tree *next = &TREE_OPERAND (*ptr, 1);
7535 TREE_OPERAND (*ptr, 0) = cur;
7536 if (TREE_CODE (cur) == ERROR_MARK)
7538 else if (! CAN_COMPLETE_NORMALLY (cur))
7543 if (TREE_CODE (wfl_op2) == BLOCK)
7544 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
7545 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
7546 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
7550 if (TREE_CODE (wfl_op2) != CASE_EXPR
7551 && TREE_CODE (wfl_op2) != DEFAULT_EXPR
7552 && wfl_op2 != empty_stmt_node)
7553 unreachable_stmt_error (*ptr);
7557 *ptr = java_complete_tree (*ptr);
7559 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
7560 return error_mark_node;
7561 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
7563 /* Turn local bindings to null */
7564 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7565 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
7567 TREE_TYPE (node) = void_type_node;
7570 /* 2- They are expressions but ultimately deal with statements */
7573 wfl_op1 = TREE_OPERAND (node, 0);
7574 COMPLETE_CHECK_OP_0 (node);
7575 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7576 return patch_throw_statement (node, wfl_op1);
7578 case SYNCHRONIZED_EXPR:
7579 wfl_op1 = TREE_OPERAND (node, 0);
7580 return patch_synchronized_statement (node, wfl_op1);
7583 return patch_try_statement (node);
7585 case CLEANUP_POINT_EXPR:
7586 COMPLETE_CHECK_OP_0 (node);
7587 TREE_TYPE (node) = void_type_node;
7588 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
7591 case WITH_CLEANUP_EXPR:
7592 COMPLETE_CHECK_OP_0 (node);
7593 COMPLETE_CHECK_OP_2 (node);
7594 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
7595 TREE_TYPE (node) = void_type_node;
7598 case LABELED_BLOCK_EXPR:
7599 PUSH_LABELED_BLOCK (node);
7600 if (LABELED_BLOCK_BODY (node))
7601 COMPLETE_CHECK_OP_1 (node);
7602 TREE_TYPE (node) = void_type_node;
7603 POP_LABELED_BLOCK ();
7604 if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
7605 CAN_COMPLETE_NORMALLY (node) = 1;
7608 case EXIT_BLOCK_EXPR:
7609 /* We don't complete operand 1, because it's the return value of
7610 the EXIT_BLOCK_EXPR which doesn't exist it Java */
7611 return patch_bc_statement (node);
7614 cn = java_complete_tree (TREE_OPERAND (node, 0));
7615 if (cn == error_mark_node)
7618 /* First, the case expression must be constant */
7621 if (!TREE_CONSTANT (cn))
7623 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7624 parse_error_context (node, "Constant expression required");
7625 return error_mark_node;
7628 nn = ctxp->current_loop;
7630 /* It must be assignable to the type of the switch expression. */
7631 if (!try_builtin_assignconv (NULL_TREE,
7632 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
7634 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7637 "Incompatible type for case. Can't convert `%s' to `int'",
7638 lang_printable_name (TREE_TYPE (cn), 0));
7639 return error_mark_node;
7642 cn = fold (convert (int_type_node, cn));
7644 /* Multiple instance of a case label bearing the same
7645 value is checked during code generation. The case
7646 expression is allright so far. */
7647 TREE_OPERAND (node, 0) = cn;
7648 TREE_TYPE (node) = void_type_node;
7649 CAN_COMPLETE_NORMALLY (node) = 1;
7650 TREE_SIDE_EFFECTS (node) = 1;
7654 nn = ctxp->current_loop;
7655 /* Only one default label is allowed per switch statement */
7656 if (SWITCH_HAS_DEFAULT (nn))
7658 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7659 parse_error_context (wfl_operator,
7660 "Duplicate case label: `default'");
7661 return error_mark_node;
7664 SWITCH_HAS_DEFAULT (nn) = 1;
7665 TREE_TYPE (node) = void_type_node;
7666 TREE_SIDE_EFFECTS (node) = 1;
7667 CAN_COMPLETE_NORMALLY (node) = 1;
7673 /* Check whether the loop was enclosed in a labeled
7674 statement. If not, create one, insert the loop in it and
7676 nn = patch_loop_statement (node);
7678 /* Anyways, walk the body of the loop */
7679 if (TREE_CODE (node) == LOOP_EXPR)
7680 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7681 /* Switch statement: walk the switch expression and the cases */
7683 node = patch_switch_statement (node);
7685 if (TREE_OPERAND (node, 0) == error_mark_node)
7686 return error_mark_node;
7687 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
7688 /* If we returned something different, that's because we
7689 inserted a label. Pop the label too. */
7692 if (CAN_COMPLETE_NORMALLY (node))
7693 CAN_COMPLETE_NORMALLY (nn) = 1;
7694 POP_LABELED_BLOCK ();
7700 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7701 return patch_exit_expr (node);
7705 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7706 if (TREE_OPERAND (node, 0) == error_mark_node)
7707 return error_mark_node;
7708 /* then-else branches */
7709 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7710 if (TREE_OPERAND (node, 1) == error_mark_node)
7711 return error_mark_node;
7712 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
7713 if (TREE_OPERAND (node, 2) == error_mark_node)
7714 return error_mark_node;
7715 return patch_if_else_statement (node);
7718 case CONDITIONAL_EXPR:
7720 wfl_op1 = TREE_OPERAND (node, 0);
7721 COMPLETE_CHECK_OP_0 (node);
7722 wfl_op2 = TREE_OPERAND (node, 1);
7723 COMPLETE_CHECK_OP_1 (node);
7724 wfl_op3 = TREE_OPERAND (node, 2);
7725 COMPLETE_CHECK_OP_2 (node);
7726 return patch_conditional_expr (node, wfl_op1, wfl_op2);
7728 /* 3- Expression section */
7730 wfl_op2 = TREE_OPERAND (node, 1);
7731 TREE_OPERAND (node, 0) = nn =
7732 java_complete_tree (TREE_OPERAND (node, 0));
7733 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK
7734 && wfl_op2 != empty_stmt_node)
7736 /* An unreachable condition in a do-while statement
7737 is *not* (technically) an unreachable statement. */
7739 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
7740 nn = EXPR_WFL_NODE (nn);
7741 if (TREE_CODE (nn) != EXIT_EXPR)
7743 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
7744 parse_error_context (wfl_operator, "Unreachable statement");
7747 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7748 if (TREE_OPERAND (node, 1) == error_mark_node)
7749 return error_mark_node;
7750 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
7751 CAN_COMPLETE_NORMALLY (node)
7752 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
7756 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7757 return patch_return (node);
7759 case EXPR_WITH_FILE_LOCATION:
7760 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
7761 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
7763 node = resolve_expression_name (node, NULL);
7764 CAN_COMPLETE_NORMALLY (node) = 1;
7769 int save_lineno = lineno;
7770 lineno = EXPR_WFL_LINENO (node);
7771 body = java_complete_tree (EXPR_WFL_NODE (node));
7772 lineno = save_lineno;
7773 EXPR_WFL_NODE (node) = body;
7774 TREE_SIDE_EFFECTS (node) = 1;
7775 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
7776 if (EXPR_WFL_NODE (node) == error_mark_node)
7778 /* Its important for the evaluation of assignment that
7779 this mark on the TREE_TYPE is propagated. */
7780 TREE_TYPE (node) = error_mark_node;
7781 return error_mark_node;
7784 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
7789 case NEW_ARRAY_EXPR:
7790 /* Patch all the dimensions */
7792 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
7794 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
7795 tree dim = java_complete_tree (TREE_VALUE (cn));
7796 if (dim == error_mark_node)
7803 TREE_VALUE (cn) = dim;
7804 /* Setup the location of the current dimension, for
7805 later error report. */
7807 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
7808 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
7811 /* They complete the array creation expression, if no errors
7813 CAN_COMPLETE_NORMALLY (node) = 1;
7814 return (flag ? error_mark_node : patch_newarray (node));
7816 case NEW_CLASS_EXPR:
7818 /* Complete function's argument(s) first */
7819 if (complete_function_arguments (node))
7820 return error_mark_node;
7823 tree decl, wfl = TREE_OPERAND (node, 0);
7824 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
7826 node = patch_method_invocation (node, NULL_TREE,
7827 NULL_TREE, 0, &decl);
7828 if (node == error_mark_node)
7829 return error_mark_node;
7831 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
7832 /* If we call this(...), register signature and positions */
7834 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
7835 tree_cons (wfl, decl,
7836 DECL_CONSTRUCTOR_CALLS (current_function_decl));
7837 CAN_COMPLETE_NORMALLY (node) = 1;
7842 /* Save potential wfls */
7843 wfl_op1 = TREE_OPERAND (node, 0);
7844 wfl_op2 = TREE_OPERAND (node, 1);
7845 TREE_OPERAND (node, 0) = java_complete_lhs (wfl_op1);
7846 if (TREE_OPERAND (node, 0) == error_mark_node)
7847 return error_mark_node;
7849 if (COMPOUND_ASSIGN_P (wfl_op2))
7853 java_complete_tree (TREE_OPERAND (wfl_op2, 0));
7855 /* Hand stablize the lhs on both places */
7856 lvalue = stabilize_reference (other);
7857 TREE_OPERAND (node, 0) = lvalue;
7858 TREE_OPERAND (TREE_OPERAND (node, 1), 0) = lvalue;
7861 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
7862 function to complete this RHS */
7863 if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT)
7864 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
7865 TREE_OPERAND (node, 1));
7867 nn = java_complete_tree (TREE_OPERAND (node, 1));
7869 /* There are cases where the type of RHS is fixed. In those
7870 cases, if the evaluation of the RHS fails, we further the
7871 evaluation of the assignment to detect more errors. */
7872 if (nn == error_mark_node)
7874 /* It's hopeless, but we can further things on to discover
7875 an error during the assignment. In any cases, the
7876 assignment operation fails. */
7877 if (TREE_CODE (TREE_OPERAND (node, 1)) != EXPR_WITH_FILE_LOCATION
7878 && TREE_CODE (TREE_OPERAND (node, 1)) != NEW_ARRAY_INIT
7879 && TREE_TYPE (TREE_OPERAND (node, 1)) != error_mark_node)
7880 patch_assignment (node, wfl_op1, wfl_op2);
7882 /* Now, we still mark the lhs as initialized */
7883 if (DECL_P (TREE_OPERAND (node, 0)))
7884 INITIALIZED_P (TREE_OPERAND (node, 0)) = 1;
7886 return error_mark_node;
7888 TREE_OPERAND (node, 1) = nn;
7890 /* In case we're handling = with a String as a RHS, we need to
7891 produce a String out of the RHS (it might still be a
7892 STRING_CST or a StringBuffer at this stage */
7893 if ((nn = patch_string (TREE_OPERAND (node, 1))))
7894 TREE_OPERAND (node, 1) = nn;
7895 node = patch_assignment (node, wfl_op1, wfl_op2);
7896 CAN_COMPLETE_NORMALLY (node) = 1;
7898 /* Before returning the node, in the context of a static field
7899 assignment in <clinit>, we may want to carray further
7900 optimizations. (VAR_DECL means it's a static field. See
7902 if (DECL_NAME (current_function_decl) == clinit_identifier_node
7903 && MODIFY_EXPR_FROM_INITIALIZATION_P (node)
7904 && TREE_CODE (TREE_OPERAND (node, 0)) == VAR_DECL)
7905 node = patch_initialized_static_field (node);
7918 case TRUNC_MOD_EXPR:
7920 case TRUTH_ANDIF_EXPR:
7921 case TRUTH_ORIF_EXPR:
7928 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
7929 knows how to handle those cases. */
7930 wfl_op1 = TREE_OPERAND (node, 0);
7931 wfl_op2 = TREE_OPERAND (node, 1);
7933 CAN_COMPLETE_NORMALLY (node) = 1;
7934 /* Don't complete string nodes if dealing with the PLUS operand. */
7935 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
7937 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7938 if (TREE_OPERAND (node, 0) == error_mark_node)
7939 return error_mark_node;
7941 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
7943 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
7944 if (TREE_OPERAND (node, 1) == error_mark_node)
7945 return error_mark_node;
7947 return patch_binop (node, wfl_op1, wfl_op2);
7949 case INSTANCEOF_EXPR:
7950 wfl_op1 = TREE_OPERAND (node, 0);
7951 COMPLETE_CHECK_OP_0 (node);
7952 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
7954 case UNARY_PLUS_EXPR:
7956 case TRUTH_NOT_EXPR:
7958 case PREDECREMENT_EXPR:
7959 case PREINCREMENT_EXPR:
7960 case POSTDECREMENT_EXPR:
7961 case POSTINCREMENT_EXPR:
7963 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
7964 how to handle those cases. */
7965 wfl_op1 = TREE_OPERAND (node, 0);
7966 CAN_COMPLETE_NORMALLY (node) = 1;
7967 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7968 if (TREE_OPERAND (node, 0) == error_mark_node)
7969 return error_mark_node;
7970 node = patch_unaryop (node, wfl_op1);
7971 CAN_COMPLETE_NORMALLY (node) = 1;
7975 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
7976 how to handle those cases. */
7977 wfl_op1 = TREE_OPERAND (node, 0);
7978 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7979 if (TREE_OPERAND (node, 0) == error_mark_node)
7980 return error_mark_node;
7981 if (!flag_emit_class_files)
7982 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
7983 /* The same applies to wfl_op2 */
7984 wfl_op2 = TREE_OPERAND (node, 1);
7985 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
7986 if (TREE_OPERAND (node, 1) == error_mark_node)
7987 return error_mark_node;
7988 if (!flag_emit_class_files)
7989 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
7990 return patch_array_ref (node);
7996 /* The first step in the re-write of qualified name handling. FIXME.
7997 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
7998 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7999 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
8001 tree name = TREE_OPERAND (node, 1);
8002 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
8003 if (field == NULL_TREE)
8005 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
8006 return error_mark_node;
8008 if (! FIELD_STATIC (field))
8010 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
8011 return error_mark_node;
8016 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
8020 /* Can't use THIS in a static environment */
8023 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8024 parse_error_context (wfl_operator, "Keyword `this' used outside "
8026 TREE_TYPE (node) = error_mark_node;
8027 return error_mark_node;
8029 if (ctxp->explicit_constructor_p)
8031 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8033 (wfl_operator, "Can't reference `this' or `super' before the "
8034 "superclass constructor has been called");
8035 TREE_TYPE (node) = error_mark_node;
8036 return error_mark_node;
8038 return current_this;
8041 CAN_COMPLETE_NORMALLY (node) = 1;
8042 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
8043 and it's time to turn it into the appropriate String object
8045 if ((node = patch_string (node)))
8047 fatal ("No case for tree code `%s' - java_complete_tree\n",
8048 tree_code_name [TREE_CODE (node)]);
8053 /* Complete function call's argument. Return a non zero value is an
8057 complete_function_arguments (node)
8063 ctxp->explicit_constructor_p += (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
8064 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
8066 tree wfl = TREE_VALUE (cn), parm, temp;
8067 parm = java_complete_tree (wfl);
8068 if (parm == error_mark_node)
8073 /* If have a string literal that we haven't transformed yet or a
8074 crafted string buffer, as a result of use of the the String
8075 `+' operator. Build `parm.toString()' and expand it. */
8076 if ((temp = patch_string (parm)))
8078 /* Inline PRIMTYPE.TYPE read access */
8079 parm = maybe_build_primttype_type_ref (parm, wfl);
8081 TREE_VALUE (cn) = parm;
8083 ctxp->explicit_constructor_p -= (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
8087 /* Sometimes (for loops and variable initialized during their
8088 declaration), we want to wrap a statement around a WFL and turn it
8092 build_debugable_stmt (location, stmt)
8096 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
8098 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
8099 EXPR_WFL_LINECOL (stmt) = location;
8101 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
8106 build_expr_block (body, decls)
8109 tree node = make_node (BLOCK);
8110 BLOCK_EXPR_DECLS (node) = decls;
8111 BLOCK_EXPR_BODY (node) = body;
8113 TREE_TYPE (node) = TREE_TYPE (body);
8114 TREE_SIDE_EFFECTS (node) = 1;
8118 /* Create a new function block and link it approriately to current
8119 function block chain */
8124 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
8127 /* Link block B supercontext to the previous block. The current
8128 function DECL is used as supercontext when enter_a_block is called
8129 for the first time for a given function. The current function body
8130 (DECL_FUNCTION_BODY) is set to be block B. */
8136 tree fndecl = current_function_decl;
8138 if (!DECL_FUNCTION_BODY (fndecl))
8140 BLOCK_SUPERCONTEXT (b) = fndecl;
8141 DECL_FUNCTION_BODY (fndecl) = b;
8145 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
8146 DECL_FUNCTION_BODY (fndecl) = b;
8151 /* Exit a block by changing the current function body
8152 (DECL_FUNCTION_BODY) to the current block super context, only if
8153 the block being exited isn't the method's top level one. */
8158 tree b = DECL_FUNCTION_BODY (current_function_decl);
8160 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
8161 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
8166 /* Lookup for NAME in the nested function's blocks, all the way up to
8167 the current toplevel one. It complies with Java's local variable
8171 lookup_name_in_blocks (name)
8174 tree b = DECL_FUNCTION_BODY (current_function_decl);
8176 while (b != current_function_decl)
8180 /* Paranoid sanity check. To be removed */
8181 if (TREE_CODE (b) != BLOCK)
8182 fatal ("non block expr function body - lookup_name_in_blocks");
8184 for (current = BLOCK_EXPR_DECLS (b); current;
8185 current = TREE_CHAIN (current))
8186 if (DECL_NAME (current) == name)
8188 b = BLOCK_SUPERCONTEXT (b);
8194 maybe_absorb_scoping_blocks ()
8196 while (BLOCK_EXPR_ORIGIN (DECL_FUNCTION_BODY (current_function_decl)))
8198 tree b = exit_block ();
8199 java_method_add_stmt (current_function_decl, b);
8200 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
8205 /* This section of the source is reserved to build_* functions that
8206 are building incomplete tree nodes and the patch_* functions that
8207 are completing them. */
8209 /* Build a super() constructor invocation. Returns empty_stmt_node if
8210 we're currently dealing with the class java.lang.Object. */
8213 build_super_invocation ()
8215 if (current_class == object_type_node)
8216 return empty_stmt_node;
8219 tree super_wfl = build_wfl_node (super_identifier_node,
8220 input_filename, 0, 0);
8221 return build_method_invocation (super_wfl, NULL_TREE);
8225 /* Build a SUPER/THIS qualified method invocation. */
8228 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
8236 build_wfl_node ((use_this ? this_identifier_node : super_identifier_node),
8237 input_filename, 0, 0);
8238 EXPR_WFL_LINECOL (wfl) = lloc;
8239 invok = build_method_invocation (name, args);
8240 return make_qualified_primary (wfl, invok, rloc);
8243 /* Build an incomplete CALL_EXPR node. */
8246 build_method_invocation (name, args)
8250 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
8251 TREE_SIDE_EFFECTS (call) = 1;
8252 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8256 /* Build an incomplete new xxx(...) node. */
8259 build_new_invocation (name, args)
8262 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
8263 TREE_SIDE_EFFECTS (call) = 1;
8264 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8268 /* Build an incomplete assignment expression. */
8271 build_assignment (op, op_location, lhs, rhs)
8272 int op, op_location;
8276 /* Build the corresponding binop if we deal with a Compound
8277 Assignment operator. Mark the binop sub-tree as part of a
8278 Compound Assignment expression */
8279 if (op != ASSIGN_TK)
8281 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
8282 COMPOUND_ASSIGN_P (rhs) = 1;
8284 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
8285 TREE_SIDE_EFFECTS (assignment) = 1;
8286 EXPR_WFL_LINECOL (assignment) = op_location;
8290 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
8293 print_int_node (node)
8296 static char buffer [80];
8297 if (TREE_CONSTANT_OVERFLOW (node))
8298 sprintf (buffer, "<overflow>");
8300 if (TREE_INT_CST_HIGH (node) == 0)
8301 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
8302 TREE_INT_CST_LOW (node));
8303 else if (TREE_INT_CST_HIGH (node) == -1
8304 && TREE_INT_CST_LOW (node) != 0)
8307 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
8308 -TREE_INT_CST_LOW (node));
8311 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
8312 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
8317 /* Return 1 if you an assignment of a FINAL is attempted */
8320 check_final_assignment (lvalue, wfl)
8323 if (DECL_P (lvalue) && FIELD_FINAL (lvalue) &&
8324 DECL_NAME (current_function_decl) != clinit_identifier_node)
8327 (wfl, "Can't assign a value to the final variable `%s'",
8328 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
8334 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
8335 read. This is needed to avoid circularities in the implementation
8336 of these fields in libjava. */
8339 maybe_build_primttype_type_ref (rhs, wfl)
8342 tree to_return = NULL_TREE;
8343 tree rhs_type = TREE_TYPE (rhs);
8344 if (TREE_CODE (rhs) == COMPOUND_EXPR)
8346 tree n = TREE_OPERAND (rhs, 1);
8347 if (TREE_CODE (n) == VAR_DECL
8348 && DECL_NAME (n) == TYPE_identifier_node
8349 && rhs_type == class_ptr_type)
8351 char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
8352 if (!strncmp (self_name, "java.lang.", 10))
8353 to_return = build_primtype_type_ref (self_name);
8356 return (to_return ? to_return : rhs );
8359 /* 15.25 Assignment operators. */
8362 patch_assignment (node, wfl_op1, wfl_op2)
8367 tree rhs = TREE_OPERAND (node, 1);
8368 tree lvalue = TREE_OPERAND (node, 0), llvalue;
8369 tree lhs_type, rhs_type, new_rhs = NULL_TREE;
8370 int error_found = 0;
8371 int lvalue_from_array = 0;
8373 /* Can't assign to a final. */
8374 if (check_final_assignment (lvalue, wfl_op1))
8377 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8379 /* Lhs can be a named variable */
8380 if (DECL_P (lvalue))
8382 INITIALIZED_P (lvalue) = 1;
8383 lhs_type = TREE_TYPE (lvalue);
8385 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
8386 comment on reason why */
8387 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
8389 lhs_type = TREE_TYPE (lvalue);
8390 lvalue_from_array = 1;
8392 /* Or a field access */
8393 else if (TREE_CODE (lvalue) == COMPONENT_REF)
8394 lhs_type = TREE_TYPE (lvalue);
8395 /* Or a function return slot */
8396 else if (TREE_CODE (lvalue) == RESULT_DECL)
8397 lhs_type = TREE_TYPE (lvalue);
8398 /* Otherwise, we might want to try to write into an optimized static
8399 final, this is an of a different nature, reported further on. */
8400 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
8401 && resolve_expression_name (wfl_op1, &llvalue)
8402 && check_final_assignment (llvalue, wfl_op1))
8405 /* What we should do instead is resetting the all the flags
8406 previously set, exchange lvalue for llvalue and continue. */
8407 return error_mark_node;
8411 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
8415 rhs_type = TREE_TYPE (rhs);
8416 /* 5.1 Try the assignment conversion for builtin type. */
8417 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
8419 /* 5.2 If it failed, try a reference conversion */
8420 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
8421 lhs_type = promote_type (rhs_type);
8423 /* 15.25.2 If we have a compound assignment, convert RHS into the
8425 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8426 new_rhs = convert (lhs_type, rhs);
8428 /* Explicit cast required. This is an error */
8431 char *t1 = strdup (lang_printable_name (TREE_TYPE (rhs), 0));
8432 char *t2 = strdup (lang_printable_name (lhs_type, 0));
8434 char operation [32]; /* Max size known */
8436 /* If the assignment is part of a declaration, we use the WFL of
8437 the declared variable to point out the error and call it a
8438 declaration problem. If the assignment is a genuine =
8439 operator, we call is a operator `=' problem, otherwise we
8440 call it an assignment problem. In both of these last cases,
8441 we use the WFL of the operator to indicate the error. */
8443 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
8446 strcpy (operation, "declaration");
8451 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8452 strcpy (operation, "assignment");
8453 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
8454 strcpy (operation, "`return'");
8456 strcpy (operation, "`='");
8460 (wfl, (!valid_cast_to_p (rhs_type, lhs_type) ?
8461 "Incompatible type for %s. Can't convert `%s' to `%s'" :
8462 "Incompatible type for %s. Explicit cast "
8463 "needed to convert `%s' to `%s'"), operation, t1, t2);
8464 free (t1); free (t2);
8468 /* Inline read access to java.lang.PRIMTYPE.TYPE */
8470 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
8473 return error_mark_node;
8475 /* If we built a compound expression as the result of a reference
8476 assignment into an array element, return it here. */
8477 if (TREE_CODE (node) == COMPOUND_EXPR)
8480 TREE_OPERAND (node, 0) = lvalue;
8481 TREE_OPERAND (node, 1) = new_rhs;
8482 TREE_TYPE (node) = lhs_type;
8486 /* Optimize static (final) field initialized upon declaration.
8487 - If the field is static final and is assigned to a primitive
8488 constant type, then set its DECL_INITIAL to the value.
8492 patch_initialized_static_field (node)
8495 tree field = TREE_OPERAND (node, 0);
8496 tree value = TREE_OPERAND (node, 1);
8498 if (DECL_INITIAL (field) != NULL_TREE)
8500 tree type = TREE_TYPE (value);
8501 if (FIELD_FINAL (field) && TREE_CONSTANT (value)
8502 && (JPRIMITIVE_TYPE_P (type)
8503 || (flag_emit_class_files
8504 && TREE_CODE (type) == POINTER_TYPE
8505 && TREE_TYPE (type) == string_type_node)))
8507 DECL_INITIAL (field) = value;
8508 return empty_stmt_node;
8510 DECL_INITIAL (field) = NULL_TREE;
8515 /* Check that type SOURCE can be cast into type DEST. If the cast
8516 can't occur at all, return 0 otherwise 1. This function is used to
8517 produce accurate error messages on the reasons why an assignment
8521 try_reference_assignconv (lhs_type, rhs)
8524 tree new_rhs = NULL_TREE;
8525 tree rhs_type = TREE_TYPE (rhs);
8527 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
8529 /* `null' may be assigned to any reference type */
8530 if (rhs == null_pointer_node)
8531 new_rhs = null_pointer_node;
8532 /* Try the reference assignment conversion */
8533 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
8535 /* This is a magic assignment that we process differently */
8536 else if (rhs == soft_exceptioninfo_call_node)
8542 /* Check that RHS can be converted into LHS_TYPE by the assignment
8543 conversion (5.2), for the cases of RHS being a builtin type. Return
8544 NULL_TREE if the conversion fails or if because RHS isn't of a
8545 builtin type. Return a converted RHS if the conversion is possible. */
8548 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
8549 tree wfl_op1, lhs_type, rhs;
8551 tree new_rhs = NULL_TREE;
8552 tree rhs_type = TREE_TYPE (rhs);
8554 /* Zero accepted everywhere */
8555 if (TREE_CODE (rhs) == INTEGER_CST
8556 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
8557 && JPRIMITIVE_TYPE_P (rhs_type))
8558 new_rhs = convert (lhs_type, rhs);
8560 /* 5.1.1 Try Identity Conversion,
8561 5.1.2 Try Widening Primitive Conversion */
8562 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
8563 new_rhs = convert (lhs_type, rhs);
8565 /* Try a narrowing primitive conversion (5.1.3):
8566 - expression is a constant expression of type int AND
8567 - variable is byte, short or char AND
8568 - The value of the expression is representable in the type of the
8570 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
8571 && (lhs_type == byte_type_node || lhs_type == char_type_node
8572 || lhs_type == short_type_node))
8574 if (int_fits_type_p (rhs, lhs_type))
8575 new_rhs = convert (lhs_type, rhs);
8576 else if (wfl_op1) /* Might be called with a NULL */
8577 parse_warning_context
8578 (wfl_op1, "Constant expression `%s' to wide for narrowing "
8579 "primitive conversion to `%s'",
8580 print_int_node (rhs), lang_printable_name (lhs_type, 0));
8581 /* Reported a warning that will turn into an error further
8582 down, so we don't return */
8588 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
8589 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
8590 0 is the conversion test fails. This implements parts the method
8591 invocation convertion (5.3). */
8594 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
8595 tree lhs_type, rhs_type;
8597 /* 5.1.1: This is the identity conversion part. */
8598 if (lhs_type == rhs_type)
8601 /* Sometimes, instead of passing a type, we pass integer_zero_node
8602 so we know that a numeric type can accomodate it */
8603 if (JNUMERIC_TYPE_P (lhs_type) && (rhs_type == integer_zero_node))
8606 /* Reject non primitive types */
8607 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
8610 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
8611 than a char can't be converted into a char. Short can't too, but
8612 the < test below takes care of that */
8613 if (lhs_type == char_type_node && rhs_type == byte_type_node)
8616 /* Accept all promoted type here. Note, we can't use <= in the test
8617 below, because we still need to bounce out assignments of short
8618 to char and the likes */
8619 if (lhs_type == int_type_node
8620 && (rhs_type == promoted_byte_type_node
8621 || rhs_type == promoted_short_type_node
8622 || rhs_type == promoted_char_type_node
8623 || rhs_type == promoted_boolean_type_node))
8626 /* From here, an integral is widened if its precision is smaller
8627 than the precision of the LHS or if the LHS is a floating point
8628 type, or the RHS is a float and the RHS a double. */
8629 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
8630 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
8631 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
8632 || (rhs_type == float_type_node && lhs_type == double_type_node))
8638 /* Check that something of SOURCE type can be assigned or cast to
8639 something of DEST type at runtime. Return 1 if the operation is
8640 valid, 0 otherwise. If CAST is set to 1, we're treating the case
8641 were SOURCE is cast into DEST, which borrows a lot of the
8642 assignment check. */
8645 valid_ref_assignconv_cast_p (source, dest, cast)
8650 if (JNULLP_TYPE_P (source))
8652 if (TREE_CODE (source) == POINTER_TYPE)
8653 source = TREE_TYPE (source);
8654 if (TREE_CODE (dest) == POINTER_TYPE)
8655 dest = TREE_TYPE (dest);
8656 /* Case where SOURCE is a class type */
8657 if (TYPE_CLASS_P (source))
8659 if (TYPE_CLASS_P (dest))
8660 return source == dest || inherits_from_p (source, dest)
8661 || (cast && inherits_from_p (dest, source));
8662 if (TYPE_INTERFACE_P (dest))
8664 /* If doing a cast and SOURCE is final, the operation is
8665 always correct a compile time (because even if SOURCE
8666 does not implement DEST, a subclass of SOURCE might). */
8667 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
8669 /* Otherwise, SOURCE must implement DEST */
8670 return interface_of_p (dest, source);
8672 /* DEST is an array, cast permited if SOURCE is of Object type */
8673 return (cast && source == object_type_node ? 1 : 0);
8675 if (TYPE_INTERFACE_P (source))
8677 if (TYPE_CLASS_P (dest))
8679 /* If not casting, DEST must be the Object type */
8681 return dest == object_type_node;
8682 /* We're doing a cast. The cast is always valid is class
8683 DEST is not final, otherwise, DEST must implement SOURCE */
8684 else if (!CLASS_FINAL (TYPE_NAME (dest)))
8687 return interface_of_p (source, dest);
8689 if (TYPE_INTERFACE_P (dest))
8691 /* If doing a cast, then if SOURCE and DEST contain method
8692 with the same signature but different return type, then
8693 this is a (compile time) error */
8696 tree method_source, method_dest;
8700 for (method_source = TYPE_METHODS (source); method_source;
8701 method_source = TREE_CHAIN (method_source))
8704 build_java_argument_signature (TREE_TYPE (method_source));
8705 source_type = TREE_TYPE (TREE_TYPE (method_source));
8706 source_name = DECL_NAME (method_source);
8707 for (method_dest = TYPE_METHODS (dest);
8708 method_dest; method_dest = TREE_CHAIN (method_dest))
8710 build_java_argument_signature (TREE_TYPE (method_dest))
8711 && source_name == DECL_NAME (method_dest)
8712 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
8718 return source == dest || interface_of_p (dest, source);
8723 if (TYPE_ARRAY_P (source))
8725 if (TYPE_CLASS_P (dest))
8726 return dest == object_type_node;
8727 if (TYPE_INTERFACE_P (dest))
8728 return 0; /* Install test on Clonable. FIXME */
8731 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
8732 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
8734 /* In case of severe errors, they turn out null */
8735 if (!dest_element_type || !source_element_type)
8737 if (source_element_type == dest_element_type)
8739 return valid_ref_assignconv_cast_p (source_element_type,
8740 dest_element_type, cast);
8748 valid_cast_to_p (source, dest)
8752 if (TREE_CODE (source) == POINTER_TYPE)
8753 source = TREE_TYPE (source);
8754 if (TREE_CODE (dest) == POINTER_TYPE)
8755 dest = TREE_TYPE (dest);
8757 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
8758 return valid_ref_assignconv_cast_p (source, dest, 1);
8760 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
8766 /* Method invocation conversion test. Return 1 if type SOURCE can be
8767 converted to type DEST through the methond invocation conversion
8771 do_unary_numeric_promotion (arg)
8774 tree type = TREE_TYPE (arg);
8775 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
8776 : TREE_CODE (type) == CHAR_TYPE)
8777 arg = convert (int_type_node, arg);
8781 /* Return a non zero value if SOURCE can be converted into DEST using
8782 the method invocation conversion rule (5.3). */
8784 valid_method_invocation_conversion_p (dest, source)
8787 return (((JPRIMITIVE_TYPE_P (source) || (source == integer_zero_node))
8788 && JPRIMITIVE_TYPE_P (dest)
8789 && valid_builtin_assignconv_identity_widening_p (dest, source))
8790 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
8791 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
8792 && valid_ref_assignconv_cast_p (source, dest, 0)));
8795 /* Build an incomplete binop expression. */
8798 build_binop (op, op_location, op1, op2)
8803 tree binop = build (op, NULL_TREE, op1, op2);
8804 TREE_SIDE_EFFECTS (binop) = 1;
8805 /* Store the location of the operator, for better error report. The
8806 string of the operator will be rebuild based on the OP value. */
8807 EXPR_WFL_LINECOL (binop) = op_location;
8811 /* Build the string of the operator retained by NODE. If NODE is part
8812 of a compound expression, add an '=' at the end of the string. This
8813 function is called when an error needs to be reported on an
8814 operator. The string is returned as a pointer to a static character
8818 operator_string (node)
8821 #define BUILD_OPERATOR_STRING(S) \
8823 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
8827 static char buffer [10];
8828 switch (TREE_CODE (node))
8830 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
8831 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
8832 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
8833 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8834 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
8835 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
8836 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
8837 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
8838 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
8839 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
8840 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
8841 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
8842 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
8843 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
8844 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
8845 case GT_EXPR: BUILD_OPERATOR_STRING (">");
8846 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
8847 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
8848 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
8849 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8850 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
8851 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
8852 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
8853 case PREINCREMENT_EXPR: /* Fall through */
8854 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
8855 case PREDECREMENT_EXPR: /* Fall through */
8856 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
8858 fatal ("unregistered operator %s - operator_string",
8859 tree_code_name [TREE_CODE (node)]);
8862 #undef BUILD_OPERATOR_STRING
8865 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
8866 errors but we modify NODE so that it contains the type computed
8867 according to the expression, when it's fixed. Otherwise, we write
8868 error_mark_node as the type. It allows us to further the analysis
8869 of remaining nodes and detects more errors in certain cases. */
8872 patch_binop (node, wfl_op1, wfl_op2)
8877 tree op1 = TREE_OPERAND (node, 0);
8878 tree op2 = TREE_OPERAND (node, 1);
8879 tree op1_type = TREE_TYPE (op1);
8880 tree op2_type = TREE_TYPE (op2);
8882 int code = TREE_CODE (node);
8884 /* If 1, tell the routine that we have to return error_mark_node
8885 after checking for the initialization of the RHS */
8886 int error_found = 0;
8888 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8892 /* 15.16 Multiplicative operators */
8893 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
8894 case RDIV_EXPR: /* 15.16.2 Division Operator / */
8895 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
8896 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
8898 if (!JPRIMITIVE_TYPE_P (op1_type))
8899 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
8900 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
8901 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
8902 TREE_TYPE (node) = error_mark_node;
8906 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8907 /* Change the division operator if necessary */
8908 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
8909 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
8910 /* This one is more complicated. FLOATs are processed by a function
8911 call to soft_fmod. */
8912 if (code == TRUNC_MOD_EXPR)
8913 return build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
8916 /* 15.17 Additive Operators */
8917 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
8919 /* Operation is valid if either one argument is a string
8920 constant, a String object or a StringBuffer crafted for the
8921 purpose of the a previous usage of the String concatenation
8924 if (TREE_CODE (op1) == STRING_CST
8925 || TREE_CODE (op2) == STRING_CST
8926 || JSTRING_TYPE_P (op1_type)
8927 || JSTRING_TYPE_P (op2_type)
8928 || IS_CRAFTED_STRING_BUFFER_P (op1)
8929 || IS_CRAFTED_STRING_BUFFER_P (op2))
8930 return build_string_concatenation (op1, op2);
8932 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
8934 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
8936 if (!JPRIMITIVE_TYPE_P (op1_type))
8937 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
8938 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
8939 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
8940 TREE_TYPE (node) = error_mark_node;
8944 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8947 /* 15.18 Shift Operators */
8951 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
8953 if (!JINTEGRAL_TYPE_P (op1_type))
8954 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
8957 (wfl_operator, (JPRIMITIVE_TYPE_P (op2_type) ?
8958 "Incompatible type for `%s'. Explicit cast needed to convert "
8959 "shift distance from `%s' to integral" :
8960 "Incompatible type for `%s'. Can't convert shift distance from "
8961 "`%s' to integral"),
8962 operator_string (node), lang_printable_name (op2_type, 0));
8963 TREE_TYPE (node) = error_mark_node;
8968 /* Unary numeric promotion (5.6.1) is performed on each operand
8970 op1 = do_unary_numeric_promotion (op1);
8971 op2 = do_unary_numeric_promotion (op2);
8973 /* The type of the shift expression is the type of the promoted
8974 type of the left-hand operand */
8975 prom_type = TREE_TYPE (op1);
8977 /* Shift int only up to 0x1f and long up to 0x3f */
8978 if (prom_type == int_type_node)
8979 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
8980 build_int_2 (0x1f, 0)));
8982 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
8983 build_int_2 (0x3f, 0)));
8985 /* The >>> operator is a >> operating on unsigned quantities */
8986 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
8988 tree utype = unsigned_type (prom_type);
8989 op1 = convert (utype, op1);
8990 TREE_SET_CODE (node, RSHIFT_EXPR);
8991 TREE_OPERAND (node, 0) = op1;
8992 TREE_OPERAND (node, 1) = op2;
8993 TREE_TYPE (node) = utype;
8994 return convert (prom_type, node);
8998 /* 15.19.1 Type Comparison Operator instaceof */
8999 case INSTANCEOF_EXPR:
9001 TREE_TYPE (node) = boolean_type_node;
9003 if (!(op2_type = resolve_type_during_patch (op2)))
9004 return error_mark_node;
9006 /* The first operand must be a reference type or the null type */
9007 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
9008 error_found = 1; /* Error reported further below */
9010 /* The second operand must be a reference type */
9011 if (!JREFERENCE_TYPE_P (op2_type))
9013 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
9015 (wfl_operator, "Invalid argument `%s' for `instanceof'",
9016 lang_printable_name (op2_type, 0));
9020 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
9022 /* If the first operand is null, the result is always false */
9023 if (op1 == null_pointer_node)
9024 return boolean_false_node;
9025 else if (flag_emit_class_files)
9027 TREE_OPERAND (node, 1) = op2_type;
9030 /* Otherwise we have to invoke instance of to figure it out */
9034 build (CALL_EXPR, boolean_type_node,
9035 build_address_of (soft_instanceof_node),
9038 build_tree_list (NULL_TREE,
9039 build_class_ref (op2_type))),
9041 TREE_SIDE_EFFECTS (call) = 1;
9045 /* There is no way the expression operand can be an instance of
9046 the type operand. This is a compile time error. */
9049 char *t1 = strdup (lang_printable_name (op1_type, 0));
9050 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
9052 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
9053 t1, lang_printable_name (op2_type, 0));
9060 /* 15.21 Bitwise and Logical Operators */
9064 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
9065 /* Binary numeric promotion is performed on both operand and the
9066 expression retain that type */
9067 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9069 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
9070 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
9071 /* The type of the bitwise operator expression is BOOLEAN */
9072 prom_type = boolean_type_node;
9075 if (!JINTEGRAL_TYPE_P (op1_type))
9076 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
9077 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
9078 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
9079 TREE_TYPE (node) = error_mark_node;
9081 /* Insert a break here if adding thing before the switch's
9082 break for this case */
9086 /* 15.22 Conditional-And Operator */
9087 case TRUTH_ANDIF_EXPR:
9088 /* 15.23 Conditional-Or Operator */
9089 case TRUTH_ORIF_EXPR:
9090 /* Operands must be of BOOLEAN type */
9091 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
9092 TREE_CODE (op2_type) != BOOLEAN_TYPE)
9094 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
9095 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
9096 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
9097 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
9098 TREE_TYPE (node) = boolean_type_node;
9102 /* The type of the conditional operators is BOOLEAN */
9103 prom_type = boolean_type_node;
9106 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
9111 /* The type of each of the operands must be a primitive numeric
9113 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
9115 if (!JNUMERIC_TYPE_P (op1_type))
9116 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
9117 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
9118 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
9119 TREE_TYPE (node) = boolean_type_node;
9123 /* Binary numeric promotion is performed on the operands */
9124 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9125 /* The type of the relation expression is always BOOLEAN */
9126 prom_type = boolean_type_node;
9129 /* 15.20 Equality Operator */
9132 /* 15.20.1 Numerical Equality Operators == and != */
9133 /* Binary numeric promotion is performed on the operands */
9134 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
9135 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9137 /* 15.20.2 Boolean Equality Operators == and != */
9138 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
9139 TREE_CODE (op2_type) == BOOLEAN_TYPE)
9140 ; /* Nothing to do here */
9142 /* 15.20.3 Reference Equality Operators == and != */
9143 /* Types have to be either references or the null type. If
9144 they're references, it must be possible to convert either
9145 type to the other by casting conversion. */
9146 else if (op1 == null_pointer_node || op2 == null_pointer_node
9147 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
9148 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
9149 || valid_ref_assignconv_cast_p (op2_type,
9151 ; /* Nothing to do here */
9153 /* Else we have an error figure what can't be converted into
9154 what and report the error */
9158 t1 = strdup (lang_printable_name (op1_type, 0));
9160 (wfl_operator, "Incompatible type for `%s'. Can't convert `%s' "
9161 "to `%s'", operator_string (node), t1,
9162 lang_printable_name (op2_type, 0));
9164 TREE_TYPE (node) = boolean_type_node;
9168 prom_type = boolean_type_node;
9173 return error_mark_node;
9175 TREE_OPERAND (node, 0) = op1;
9176 TREE_OPERAND (node, 1) = op2;
9177 TREE_TYPE (node) = prom_type;
9181 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
9182 zero value, the value of CSTE comes after the valude of STRING */
9185 do_merge_string_cste (cste, string, string_len, after)
9188 int string_len, after;
9190 int len = TREE_STRING_LENGTH (cste) + string_len;
9191 char *old = TREE_STRING_POINTER (cste);
9192 TREE_STRING_LENGTH (cste) = len;
9193 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
9196 strcpy (TREE_STRING_POINTER (cste), string);
9197 strcat (TREE_STRING_POINTER (cste), old);
9201 strcpy (TREE_STRING_POINTER (cste), old);
9202 strcat (TREE_STRING_POINTER (cste), string);
9207 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
9208 new STRING_CST on success, NULL_TREE on failure */
9211 merge_string_cste (op1, op2, after)
9215 /* Handle two string constants right away */
9216 if (TREE_CODE (op2) == STRING_CST)
9217 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
9218 TREE_STRING_LENGTH (op2), after);
9220 /* Reasonable integer constant can be treated right away */
9221 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
9223 static char *boolean_true = "true";
9224 static char *boolean_false = "false";
9225 static char *null_pointer = "null";
9229 if (op2 == boolean_true_node)
9230 string = boolean_true;
9231 else if (op2 == boolean_false_node)
9232 string = boolean_false;
9233 else if (op2 == null_pointer_node)
9234 string = null_pointer;
9235 else if (TREE_TYPE (op2) == char_type_node)
9237 ch[0] = (char )TREE_INT_CST_LOW (op2);
9242 string = print_int_node (op2);
9244 return do_merge_string_cste (op1, string, strlen (string), after);
9249 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
9250 has to be a STRING_CST and the other part must be a STRING_CST or a
9251 INTEGRAL constant. Return a new STRING_CST if the operation
9252 succeed, NULL_TREE otherwise.
9254 If the case we want to optimize for space, we might want to return
9255 NULL_TREE for each invocation of this routine. FIXME */
9258 string_constant_concatenation (op1, op2)
9261 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
9266 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
9267 rest = (string == op1 ? op2 : op1);
9268 invert = (string == op1 ? 0 : 1 );
9270 /* Walk REST, only if it looks reasonable */
9271 if (TREE_CODE (rest) != STRING_CST
9272 && !IS_CRAFTED_STRING_BUFFER_P (rest)
9273 && !JSTRING_TYPE_P (TREE_TYPE (rest))
9274 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
9276 rest = java_complete_tree (rest);
9277 if (rest == error_mark_node)
9278 return error_mark_node;
9281 return merge_string_cste (string, rest, invert);
9286 /* Implement the `+' operator. Does static optimization if possible,
9287 otherwise create (if necessary) and append elements to a
9288 StringBuffer. The StringBuffer will be carried around until it is
9289 used for a function call or an assignment. Then toString() will be
9290 called on it to turn it into a String object. */
9293 build_string_concatenation (op1, op2)
9298 /* Try to do some static optimization */
9299 if ((result = string_constant_concatenation (op1, op2)))
9302 /* Discard null constants on either sides of the expression */
9303 if (TREE_CODE (op1) == STRING_CST && !TREE_STRING_LENGTH (op1))
9308 else if (TREE_CODE (op2) == STRING_CST && !TREE_STRING_LENGTH (op2))
9311 /* If operands are string constant, turn then into object references */
9312 if (TREE_CODE (op1) == STRING_CST)
9313 op1 = patch_string_cst (op1);
9314 if (op2 && TREE_CODE (op2) == STRING_CST)
9315 op2 = patch_string_cst (op2);
9317 /* If either one of the constant is null and the other non null
9318 operand is a String object, return it. */
9319 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
9322 /* If OP1 isn't already a StringBuffer, create and
9323 initialize a new one */
9324 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
9326 /* Two solutions here:
9327 1) OP1 is a string reference, we call new StringBuffer(OP1)
9328 2) OP1 is something else, we call new StringBuffer().append(OP1). */
9329 if (JSTRING_TYPE_P (TREE_TYPE (op1)))
9330 op1 = BUILD_STRING_BUFFER (op1);
9333 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
9334 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
9340 /* OP1 is no longer the last node holding a crafted StringBuffer */
9341 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
9342 /* Create a node for `{new...,xxx}.append (op2)' */
9344 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
9347 /* Mark the last node holding a crafted StringBuffer */
9348 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
9353 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
9354 StringBuffer. If no string were found to be patched, return
9361 if (node == error_mark_node)
9362 return error_mark_node;
9363 if (TREE_CODE (node) == STRING_CST)
9364 return patch_string_cst (node);
9365 else if (IS_CRAFTED_STRING_BUFFER_P (node))
9367 int saved = ctxp->explicit_constructor_p;
9368 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
9370 /* Temporary disable forbid the use of `this'. */
9371 ctxp->explicit_constructor_p = 0;
9372 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
9373 /* Restore it at its previous value */
9374 ctxp->explicit_constructor_p = saved;
9380 /* Build the internal representation of a string constant. */
9383 patch_string_cst (node)
9387 if (! flag_emit_class_files)
9389 push_obstacks (&permanent_obstack, &permanent_obstack);
9390 node = get_identifier (TREE_STRING_POINTER (node));
9391 location = alloc_name_constant (CONSTANT_String, node);
9392 node = build_ref_from_constant_pool (location);
9394 TREE_TYPE (node) = promote_type (string_type_node);
9395 TREE_CONSTANT (node) = 1;
9399 /* Build an incomplete unary operator expression. */
9402 build_unaryop (op_token, op_location, op1)
9403 int op_token, op_location;
9410 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
9411 case MINUS_TK: op = NEGATE_EXPR; break;
9412 case NEG_TK: op = TRUTH_NOT_EXPR; break;
9413 case NOT_TK: op = BIT_NOT_EXPR; break;
9414 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
9418 unaryop = build1 (op, NULL_TREE, op1);
9419 TREE_SIDE_EFFECTS (unaryop) = 1;
9420 /* Store the location of the operator, for better error report. The
9421 string of the operator will be rebuild based on the OP value. */
9422 EXPR_WFL_LINECOL (unaryop) = op_location;
9426 /* Special case for the ++/-- operators, since they require an extra
9427 argument to build, which is set to NULL and patched
9428 later. IS_POST_P is 1 if the operator, 0 otherwise. */
9431 build_incdec (op_token, op_location, op1, is_post_p)
9432 int op_token, op_location;
9436 static enum tree_code lookup [2][2] =
9438 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
9439 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
9441 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
9442 NULL_TREE, op1, NULL_TREE);
9443 TREE_SIDE_EFFECTS (node) = 1;
9444 /* Store the location of the operator, for better error report. The
9445 string of the operator will be rebuild based on the OP value. */
9446 EXPR_WFL_LINECOL (node) = op_location;
9450 /* Build an incomplete cast operator, based on the use of the
9451 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
9452 set. java_complete_tree is trained to walk a CONVERT_EXPR even
9453 though its type is already set. */
9456 build_cast (location, type, exp)
9460 tree node = build1 (CONVERT_EXPR, type, exp);
9461 EXPR_WFL_LINECOL (node) = location;
9465 /* 15.14 Unary operators. We return error_mark_node in case of error,
9466 but preserve the type of NODE if the type is fixed. */
9469 patch_unaryop (node, wfl_op)
9473 tree op = TREE_OPERAND (node, 0);
9474 tree op_type = TREE_TYPE (op);
9475 tree prom_type, value;
9476 int code = TREE_CODE (node);
9477 int error_found = 0;
9479 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9483 /* 15.13.2 Postfix Increment Operator ++ */
9484 case POSTINCREMENT_EXPR:
9485 /* 15.13.3 Postfix Increment Operator -- */
9486 case POSTDECREMENT_EXPR:
9487 /* 15.14.1 Prefix Increment Operator ++ */
9488 case PREINCREMENT_EXPR:
9489 /* 15.14.2 Prefix Decrement Operator -- */
9490 case PREDECREMENT_EXPR:
9491 if (!DECL_P (op) && !((TREE_CODE (op) == INDIRECT_REF
9492 || TREE_CODE (op) == COMPONENT_REF)
9493 && JPRIMITIVE_TYPE_P (TREE_TYPE (op))))
9496 /* Before screaming, check that we're not in fact trying to
9497 increment a optimized static final access, in which case
9498 we issue an different error message. */
9499 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
9500 && resolve_expression_name (wfl_op, &lvalue)
9501 && check_final_assignment (lvalue, wfl_op)))
9502 parse_error_context (wfl_operator, "Invalid argument to `%s'",
9503 operator_string (node));
9504 TREE_TYPE (node) = error_mark_node;
9507 else if (check_final_assignment (op, wfl_op))
9510 /* From now on, we know that op if a variable and that it has a
9511 valid wfl. We use wfl_op to locate errors related to the
9513 else if (!JNUMERIC_TYPE_P (op_type))
9516 (wfl_op, "Invalid argument type `%s' to `%s'",
9517 lang_printable_name (op_type, 0), operator_string (node));
9518 TREE_TYPE (node) = error_mark_node;
9523 /* Before the addition, binary numeric promotion is performed on
9525 value = build_int_2 (1, 0);
9527 binary_numeric_promotion (op_type, TREE_TYPE (value), &op, &value);
9528 /* And write the promoted incremented and increment */
9529 TREE_OPERAND (node, 0) = op;
9530 TREE_OPERAND (node, 1) = value;
9531 /* Convert the overall back into its original type. */
9532 return fold (convert (op_type, node));
9536 /* 15.14.3 Unary Plus Operator + */
9537 case UNARY_PLUS_EXPR:
9538 /* 15.14.4 Unary Minus Operator - */
9540 if (!JNUMERIC_TYPE_P (op_type))
9542 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
9543 TREE_TYPE (node) = error_mark_node;
9546 /* Unary numeric promotion is performed on operand */
9549 op = do_unary_numeric_promotion (op);
9550 prom_type = TREE_TYPE (op);
9551 if (code == UNARY_PLUS_EXPR)
9556 /* 15.14.5 Bitwise Complement Operator ~ */
9558 if (!JINTEGRAL_TYPE_P (op_type))
9560 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
9561 TREE_TYPE (node) = error_mark_node;
9566 op = do_unary_numeric_promotion (op);
9567 prom_type = TREE_TYPE (op);
9571 /* 15.14.6 Logical Complement Operator ! */
9572 case TRUTH_NOT_EXPR:
9573 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
9575 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
9576 /* But the type is known. We will report an error if further
9577 attempt of a assignment is made with this rhs */
9578 TREE_TYPE (node) = boolean_type_node;
9582 prom_type = boolean_type_node;
9585 /* 15.15 Cast Expression */
9587 value = patch_cast (node, wfl_operator);
9588 if (value == error_mark_node)
9590 /* If this cast is part of an assignment, we tell the code
9591 that deals with it not to complain about a mismatch,
9592 because things have been cast, anyways */
9593 TREE_TYPE (node) = error_mark_node;
9597 return fold (value);
9602 return error_mark_node;
9604 /* There are cases where node has been replaced by something else
9605 and we don't end up returning here: UNARY_PLUS_EXPR,
9606 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
9607 TREE_OPERAND (node, 0) = fold (op);
9608 TREE_TYPE (node) = prom_type;
9612 /* Generic type resolution that sometimes takes place during node
9613 patching. Returned the resolved type or generate an error
9614 message. Return the resolved type or NULL_TREE. */
9617 resolve_type_during_patch (type)
9620 if (unresolved_type_p (type, NULL))
9622 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
9625 parse_error_context (type,
9626 "Class `%s' not found in type declaration",
9627 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
9632 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
9633 return TREE_TYPE (type_decl);
9638 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
9639 found. Otherwise NODE or something meant to replace it is returned. */
9642 patch_cast (node, wfl_operator)
9646 tree op = TREE_OPERAND (node, 0);
9647 tree op_type = TREE_TYPE (op);
9648 tree cast_type = TREE_TYPE (node);
9651 /* First resolve OP_TYPE if unresolved */
9652 if (!(cast_type = resolve_type_during_patch (cast_type)))
9653 return error_mark_node;
9655 /* Check on cast that are proven correct at compile time */
9656 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
9658 static tree convert_narrow ();
9660 if (cast_type == op_type)
9663 /* Try widening/narowwing convertion. Potentially, things need
9664 to be worked out in gcc so we implement the extreme cases
9665 correctly. fold_convert() needs to be fixed. */
9666 return convert (cast_type, op);
9669 /* null can be casted to references */
9670 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
9671 return build_null_of_type (cast_type);
9673 /* The remaining legal casts involve conversion between reference
9674 types. Check for their compile time correctness. */
9675 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
9676 && valid_ref_assignconv_cast_p (cast_type, op_type, 1))
9678 TREE_TYPE (node) = promote_type (cast_type);
9679 /* Now, the case can be determined correct at compile time if
9680 OP_TYPE can be converted into CAST_TYPE by assignment
9683 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
9685 TREE_SET_CODE (node, NOP_EXPR);
9689 if (flag_emit_class_files)
9691 TREE_SET_CODE (node, CONVERT_EXPR);
9695 /* The cast requires a run-time check */
9696 return build (CALL_EXPR, promote_type (cast_type),
9697 build_address_of (soft_checkcast_node),
9698 tree_cons (NULL_TREE, build_class_ref (cast_type),
9699 build_tree_list (NULL_TREE, op)),
9703 /* Any other casts are proven incorrect at compile time */
9704 t1 = strdup (lang_printable_name (op_type, 0));
9705 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
9706 t1, lang_printable_name (cast_type, 0));
9708 return error_mark_node;
9711 /* Build a null constant and give it the type TYPE. */
9714 build_null_of_type (type)
9717 tree node = build_int_2 (0, 0);
9718 TREE_TYPE (node) = promote_type (type);
9722 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
9723 a list of indices. */
9725 build_array_ref (location, array, index)
9729 tree node = build (ARRAY_REF, NULL_TREE, array, index);
9730 EXPR_WFL_LINECOL (node) = location;
9734 /* 15.12 Array Access Expression */
9737 patch_array_ref (node)
9740 tree array = TREE_OPERAND (node, 0);
9741 tree array_type = TREE_TYPE (array);
9742 tree index = TREE_OPERAND (node, 1);
9743 tree index_type = TREE_TYPE (index);
9744 int error_found = 0;
9746 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9748 if (TREE_CODE (array_type) == POINTER_TYPE)
9749 array_type = TREE_TYPE (array_type);
9751 /* The array reference must be an array */
9752 if (!TYPE_ARRAY_P (array_type))
9755 (wfl_operator, "`[]' can only be applied to arrays. It can't be "
9756 "applied to `%s'", lang_printable_name (array_type, 0));
9757 TREE_TYPE (node) = error_mark_node;
9761 /* The array index underdoes unary numeric promotion. The promoted
9763 index = do_unary_numeric_promotion (index);
9764 if (TREE_TYPE (index) != int_type_node)
9766 int could_cast = valid_cast_to_p (index_type, int_type_node);
9769 (could_cast ? "Incompatible type for `[]'. Explicit cast needed to "
9770 "convert `%s' to `int'" : "Incompatible type for `[]'. "
9771 "Can't convert `%s' to `int'"),
9772 lang_printable_name (index_type, 0));
9773 TREE_TYPE (node) = error_mark_node;
9778 return error_mark_node;
9780 array_type = TYPE_ARRAY_ELEMENT (array_type);
9782 if (flag_emit_class_files)
9784 TREE_OPERAND (node, 0) = array;
9785 TREE_OPERAND (node, 1) = index;
9788 node = build_java_arrayaccess (array, array_type, index);
9789 TREE_TYPE (node) = array_type;
9793 /* 15.9 Array Creation Expressions */
9796 build_newarray_node (type, dims, extra_dims)
9802 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
9803 build_int_2 (extra_dims, 0));
9808 patch_newarray (node)
9811 tree type = TREE_OPERAND (node, 0);
9812 tree dims = TREE_OPERAND (node, 1);
9813 tree cdim, array_type;
9814 int error_found = 0;
9816 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
9818 /* Dimension types are verified. It's better for the types to be
9819 verified in order. */
9820 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
9823 tree dim = TREE_VALUE (cdim);
9825 /* Dim might have been saved during its evaluation */
9826 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
9828 /* The type of each specified dimension must be an integral type. */
9829 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
9832 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
9833 promoted type must be int. */
9836 dim = do_unary_numeric_promotion (dim);
9837 if (TREE_TYPE (dim) != int_type_node)
9841 /* Report errors on types here */
9845 (TREE_PURPOSE (cdim),
9846 "Incompatible type for dimension in array creation expression. "
9847 "%s convert `%s' to `int'",
9848 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
9849 "Explicit cast needed to" : "Can't"),
9850 lang_printable_name (TREE_TYPE (dim), 0));
9854 TREE_PURPOSE (cdim) = NULL_TREE;
9857 /* Resolve array base type if unresolved */
9858 if (!(type = resolve_type_during_patch (type)))
9863 /* We don't want further evaluation of this bogus array creation
9865 TREE_TYPE (node) = error_mark_node;
9866 return error_mark_node;
9869 /* Set array_type to the actual (promoted) array type of the result. */
9870 if (TREE_CODE (type) == RECORD_TYPE)
9871 type = build_pointer_type (type);
9872 while (--xdims >= 0)
9874 type = promote_type (build_java_array_type (type, -1));
9876 dims = nreverse (dims);
9878 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
9881 array_type = build_java_array_type (type,
9882 TREE_CODE (cdim) == INTEGER_CST ?
9883 TREE_INT_CST_LOW (cdim) : -1);
9884 array_type = promote_type (array_type);
9886 dims = nreverse (dims);
9888 /* The node is transformed into a function call. Things are done
9889 differently according to the number of dimensions. If the number
9890 of dimension is equal to 1, then the nature of the base type
9891 (primitive or not) matters. */
9893 return build_new_array (type, TREE_VALUE (dims));
9895 /* Can't reuse what's already written in expr.c because it uses the
9896 JVM stack representation. Provide a build_multianewarray. FIXME */
9897 return build (CALL_EXPR, array_type,
9898 build_address_of (soft_multianewarray_node),
9899 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
9900 tree_cons (NULL_TREE,
9901 build_int_2 (ndims, 0), dims )),
9905 /* 10.6 Array initializer. */
9907 /* Build a wfl for array element that don't have one, so we can
9908 pin-point errors. */
9911 maybe_build_array_element_wfl (node)
9914 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
9915 return build_expr_wfl (NULL_TREE, ctxp->filename,
9916 ctxp->elc.line, ctxp->elc.prev_col);
9921 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
9922 identification of initialized arrays easier to detect during walk
9926 build_new_array_init (location, values)
9930 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
9931 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
9932 EXPR_WFL_LINECOL (to_return) = location;
9936 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
9937 occurred. Otherwise return NODE after having set its type
9941 patch_new_array_init (type, node)
9945 tree current, element_type;
9946 HOST_WIDE_INT length;
9947 int all_constant = 1;
9948 tree init = TREE_OPERAND (node, 0);
9950 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
9952 parse_error_context (node,
9953 "Invalid array initializer for non-array type `%s'",
9954 lang_printable_name (type, 1));
9955 return error_mark_node;
9957 type = TREE_TYPE (type);
9958 element_type = TYPE_ARRAY_ELEMENT (type);
9960 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
9962 for (length = 0, current = CONSTRUCTOR_ELTS (init);
9963 current; length++, current = TREE_CHAIN (current))
9965 tree elt = TREE_VALUE (current);
9966 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
9968 error_seen |= array_constructor_check_entry (element_type, current);
9969 elt = TREE_VALUE (current);
9970 /* When compiling to native code, STRING_CST is converted to
9971 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
9972 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
9977 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
9978 TREE_PURPOSE (current) = NULL_TREE;
9981 if (elt && TREE_VALUE (elt) == error_mark_node)
9986 return error_mark_node;
9988 /* Create a new type. We can't reuse the one we have here by
9989 patching its dimension because it originally is of dimension -1
9990 hence reused by gcc. This would prevent triangular arrays. */
9991 type = build_java_array_type (element_type, length);
9992 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
9993 TREE_TYPE (node) = promote_type (type);
9994 TREE_CONSTANT (init) = all_constant;
9995 TREE_CONSTANT (node) = all_constant;
9999 /* Verify that one entry of the initializer element list can be
10000 assigned to the array base type. Report 1 if an error occurred, 0
10004 array_constructor_check_entry (type, entry)
10007 char *array_type_string = NULL; /* For error reports */
10008 tree value, type_value, new_value, wfl_value, patched;
10009 int error_seen = 0;
10011 new_value = NULL_TREE;
10012 wfl_value = TREE_VALUE (entry);
10014 value = java_complete_tree (TREE_VALUE (entry));
10015 /* patch_string return error_mark_node if arg is error_mark_node */
10016 if ((patched = patch_string (value)))
10018 if (value == error_mark_node)
10021 type_value = TREE_TYPE (value);
10023 /* At anytime, try_builtin_assignconv can report a warning on
10024 constant overflow during narrowing. */
10025 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
10026 new_value = try_builtin_assignconv (wfl_operator, type, value);
10027 if (!new_value && (new_value = try_reference_assignconv (type, value)))
10028 type_value = promote_type (type);
10030 /* Check and report errors */
10033 char *msg = (!valid_cast_to_p (type_value, type) ?
10034 "Can't" : "Explicit cast needed to");
10035 if (!array_type_string)
10036 array_type_string = strdup (lang_printable_name (type, 1));
10037 parse_error_context
10038 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
10039 msg, lang_printable_name (type_value, 1), array_type_string);
10045 new_value = maybe_build_primttype_type_ref (new_value, wfl_operator);
10046 TREE_VALUE (entry) = new_value;
10049 if (array_type_string)
10050 free (array_type_string);
10052 TREE_PURPOSE (entry) = NULL_TREE;
10057 build_this (location)
10060 tree node = build_wfl_node (this_identifier_node, input_filename, 0, 0);
10061 TREE_SET_CODE (node, THIS_EXPR);
10062 EXPR_WFL_LINECOL (node) = location;
10066 /* 14.15 The return statement. It builds a modify expression that
10067 assigns the returned value to the RESULT_DECL that hold the value
10071 build_return (location, op)
10075 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
10076 EXPR_WFL_LINECOL (node) = location;
10077 node = build_debugable_stmt (location, node);
10082 patch_return (node)
10085 tree return_exp = TREE_OPERAND (node, 0);
10086 tree meth = current_function_decl;
10087 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
10088 int error_found = 0;
10090 TREE_TYPE (node) = error_mark_node;
10091 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10093 /* It's invalid to have a return value within a function that is
10094 declared with the keyword void or that is a constructor */
10095 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
10098 /* It's invalid to have a no return value within a function that
10099 isn't declared with the keyword `void' */
10100 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
10105 if (!DECL_CONSTRUCTOR_P (meth))
10107 char *t = strdup (lang_printable_name (mtype, 0));
10108 parse_error_context (wfl_operator,
10109 "`return' with%s value from `%s %s'",
10110 (error_found == 1 ? "" : "out"),
10111 t, lang_printable_name (meth, 0));
10115 parse_error_context (wfl_operator,
10116 "`return' with value from constructor `%s'",
10117 lang_printable_name (meth, 0));
10118 return error_mark_node;
10121 /* If we have a return_exp, build a modify expression and expand
10122 it. Note: at that point, the assignment is declared valid, but we
10123 may want to carry some more hacks */
10126 tree exp = java_complete_tree (return_exp);
10127 tree modify, patched;
10129 /* If the function returned value and EXP are booleans, EXP has
10130 to be converted into the type of DECL_RESULT, which is integer
10131 (see complete_start_java_method) */
10132 if (TREE_TYPE (exp) == boolean_type_node &&
10133 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
10134 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
10136 /* `null' can be assigned to a function returning a reference */
10137 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
10138 exp == null_pointer_node)
10139 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
10141 if ((patched = patch_string (exp)))
10144 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
10145 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
10146 modify = java_complete_tree (modify);
10148 if (modify != error_mark_node)
10150 TREE_SIDE_EFFECTS (modify) = 1;
10151 TREE_OPERAND (node, 0) = modify;
10154 return error_mark_node;
10156 TREE_TYPE (node) = void_type_node;
10157 TREE_SIDE_EFFECTS (node) = 1;
10161 /* 14.8 The if Statement */
10164 build_if_else_statement (location, expression, if_body, else_body)
10166 tree expression, if_body, else_body;
10170 else_body = empty_stmt_node;
10171 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
10172 EXPR_WFL_LINECOL (node) = location;
10173 node = build_debugable_stmt (location, node);
10178 patch_if_else_statement (node)
10181 tree expression = TREE_OPERAND (node, 0);
10183 TREE_TYPE (node) = error_mark_node;
10184 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10186 /* The type of expression must be boolean */
10187 if (TREE_TYPE (expression) != boolean_type_node
10188 && TREE_TYPE (expression) != promoted_boolean_type_node)
10190 parse_error_context
10192 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
10193 lang_printable_name (TREE_TYPE (expression), 0));
10194 return error_mark_node;
10197 TREE_TYPE (node) = void_type_node;
10198 TREE_SIDE_EFFECTS (node) = 1;
10199 CAN_COMPLETE_NORMALLY (node)
10200 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10201 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
10205 /* 14.6 Labeled Statements */
10207 /* Action taken when a lableled statement is parsed. a new
10208 LABELED_BLOCK_EXPR is created. No statement is attached to the
10212 build_labeled_block (location, label)
10216 tree label_name = merge_qualified_name (label_id, label);
10217 tree label_decl, node;
10219 /* Issue an error if we try to reuse a label that was previously
10221 if (IDENTIFIER_LOCAL_VALUE (label_name))
10223 EXPR_WFL_LINECOL (wfl_operator) = location;
10224 parse_error_context (wfl_operator, "Declaration of `%s' shadows "
10225 "a previous label declaration",
10226 IDENTIFIER_POINTER (label));
10227 EXPR_WFL_LINECOL (wfl_operator) =
10228 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
10229 parse_error_context (wfl_operator, "This is the location of the "
10230 "previous declaration of label `%s'",
10231 IDENTIFIER_POINTER (label));
10232 java_error_count--;
10235 label_decl = create_label_decl (label_name);
10236 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
10237 EXPR_WFL_LINECOL (node) = location;
10238 TREE_SIDE_EFFECTS (node) = 1;
10242 /* Generate a label crafting a unique name for it. This is used to
10243 implicitely label loops that aren't the body part of labeled
10247 generate_labeled_block ()
10249 return build_labeled_block (0, generate_name ());
10252 /* A labeled statement LBE is attached a statement. */
10255 complete_labeled_statement (lbe, statement)
10256 tree lbe; /* Labeled block expr */
10259 /* In anyways, tie the loop to its statement */
10260 LABELED_BLOCK_BODY (lbe) = statement;
10262 /* Ok, if statement is a for loop, we have to attach the labeled
10263 statement to the block the for loop belongs to and return the
10265 if (TREE_CODE (statement) == LOOP_EXPR && IS_FOR_LOOP_P (statement))
10267 java_method_add_stmt (current_function_decl, lbe);
10268 return exit_block ();
10274 /* 14.10, 14.11, 14.12 Loop Statements */
10276 /* Create an empty LOOP_EXPR and make it the last in the nested loop
10280 build_new_loop (loop_body)
10283 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
10284 TREE_SIDE_EFFECTS (loop) = 1;
10289 /* Create a loop body according to the following structure:
10291 COMPOUND_EXPR (loop main body)
10292 EXIT_EXPR (this order is for while/for loops.
10293 LABELED_BLOCK_EXPR the order is reversed for do loops)
10294 LABEL_DECL (continue occurding here branche at the
10295 BODY end of this labeled block)
10298 REVERSED, if non zero, tells that the loop condition expr comes
10299 after the body, like in the do-while loop.
10301 To obtain a loop, the loop body structure described above is
10302 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
10305 LABEL_DECL (use this label to exit the loop)
10307 <structure described above> */
10310 build_loop_body (location, condition, reversed)
10315 tree first, second, body;
10317 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
10318 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
10319 condition = build_debugable_stmt (location, condition);
10320 TREE_SIDE_EFFECTS (condition) = 1;
10322 body = generate_labeled_block ();
10323 first = (reversed ? body : condition);
10324 second = (reversed ? condition : body);
10326 build (COMPOUND_EXPR, NULL_TREE,
10327 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
10330 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
10331 their order) on the current loop. Unlink the current loop from the
10335 complete_loop_body (location, condition, body, reversed)
10337 tree condition, body;
10340 tree to_return = ctxp->current_loop;
10341 tree loop_body = LOOP_EXPR_BODY (to_return);
10344 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
10345 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
10346 The real EXIT_EXPR is one operand further. */
10347 EXPR_WFL_LINECOL (cnode) = location;
10348 /* This one is for accurate error reports */
10349 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
10350 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
10352 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
10357 /* Tailored version of complete_loop_body for FOR loops, when FOR
10358 loops feature the condition part */
10361 complete_for_loop (location, condition, update, body)
10363 tree condition, update, body;
10365 /* Put the condition and the loop body in place */
10366 tree loop = complete_loop_body (location, condition, body, 0);
10367 /* LOOP is the current loop which has been now popped of the loop
10368 stack. Install the update block */
10369 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
10373 /* If the loop isn't surrounded by a labeled statement, create one and
10374 insert LOOP as it's body. */
10377 patch_loop_statement (loop)
10380 tree loop_label, to_return_as_loop;
10382 if (LOOP_HAS_LABEL_P (loop))
10384 loop_label = ctxp->current_labeled_block;
10385 to_return_as_loop = loop;
10389 loop_label = generate_labeled_block ();
10390 LABELED_BLOCK_BODY (loop_label) = loop;
10391 PUSH_LABELED_BLOCK (loop_label);
10392 to_return_as_loop = loop_label;
10394 TREE_TYPE (to_return_as_loop) = void_type_node;
10395 return to_return_as_loop;
10398 /* 14.13, 14.14: break and continue Statements */
10400 /* Build a break or a continue statement. a null NAME indicates an
10401 unlabeled break/continue statement. */
10404 build_bc_statement (location, is_break, name)
10405 int location, is_break;
10408 tree break_continue, label_block_expr = NULL_TREE;
10412 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
10413 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
10414 /* Null means that we don't have a target for this named
10415 break/continue. In this case, we make the target to be the
10416 label name, so that the error can be reported accuratly in
10417 patch_bc_statement. */
10418 label_block_expr = EXPR_WFL_NODE (name);
10420 /* Unlabeled break/continue will be handled during the
10421 break/continue patch operation */
10423 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
10425 IS_BREAK_STMT_P (break_continue) = is_break;
10426 TREE_SIDE_EFFECTS (break_continue) = 1;
10427 EXPR_WFL_LINECOL (break_continue) = location;
10428 break_continue = build_debugable_stmt (location, break_continue);
10429 return break_continue;
10432 /* Verification of a break/continue statement. */
10435 patch_bc_statement (node)
10438 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
10439 int is_unlabeled = 0;
10440 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10442 /* Not having a target means that the break/continue statement is
10443 unlabeled. We try to find a decent label for it */
10447 /* There should be a loop/switch to branch to */
10448 if (ctxp->current_loop)
10450 if (TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10452 /* At that stage, we're in the loop body, which is
10453 encapsulated around a LABELED_BLOCK_EXPR. So searching
10454 the current loop label requires us to consider the
10455 labeled block before the current one. */
10456 if (!LOOP_HAS_LABEL_SKIP_P (ctxp->current_loop))
10457 fatal ("unlabeled loop has no installed label -- "
10458 "patch_bc_statement");
10459 bc_label = TREE_CHAIN (ctxp->current_labeled_block);
10461 /* For a SWITCH statement, this is the current one */
10463 bc_label = ctxp->current_labeled_block;
10465 /* Not having a loop to break/continue to is an error */
10468 parse_error_context (wfl_operator, "`%s' must be in loop%s",
10469 (IS_BREAK_STMT_P (node) ? "break" : "continue"),
10470 (IS_BREAK_STMT_P (node) ? " or switch" : ""));
10471 return error_mark_node;
10474 /* Having an identifier here means that the target is unknown. */
10475 else if (TREE_CODE (bc_label) == IDENTIFIER_NODE)
10477 parse_error_context (wfl_operator, "No label definition found for `%s'",
10478 IDENTIFIER_POINTER (bc_label));
10479 return error_mark_node;
10482 /* Find the statement we're targeting. */
10483 target_stmt = LABELED_BLOCK_BODY (bc_label);
10485 /* 14.13 The break Statement */
10486 if (IS_BREAK_STMT_P (node))
10488 /* Named break are always fine, as far as they have a target
10489 (already verified). Anonymous break need to target
10490 while/do/for/switch */
10491 if (is_unlabeled &&
10492 !(TREE_CODE (target_stmt) == LOOP_EXPR /* do/while/for */
10493 || TREE_CODE (target_stmt) == SWITCH_EXPR)) /* switch FIXME */
10495 parse_error_context (wfl_operator,
10496 "`break' must be in loop or switch");
10497 return error_mark_node;
10499 /* If previously unlabeled, install the new found label */
10501 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
10503 /* 14.14 The continue Statement */
10504 /* The continue statement must always target a loop */
10507 if (TREE_CODE (target_stmt) != LOOP_EXPR) /* do/while/for */
10509 parse_error_context (wfl_operator, "`continue' must be in loop");
10510 return error_mark_node;
10512 /* Everything looks good. We can fix the `continue' jump to go
10513 at the place in the loop were the continue is. The continue
10514 is the current labeled block, by construction. */
10515 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label = ctxp->current_labeled_block;
10518 CAN_COMPLETE_NORMALLY (bc_label) = 1;
10520 /* Our break/continue don't return values. */
10521 TREE_TYPE (node) = void_type_node;
10522 /* Encapsulate the break within a compound statement so that it's
10523 expanded all the times by expand_expr (and not clobered
10524 sometimes, like after a if statement) */
10525 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
10526 TREE_SIDE_EFFECTS (node) = 1;
10530 /* Process the exit expression belonging to a loop. Its type must be
10534 patch_exit_expr (node)
10537 tree expression = TREE_OPERAND (node, 0);
10538 TREE_TYPE (node) = error_mark_node;
10539 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10541 /* The type of expression must be boolean */
10542 if (TREE_TYPE (expression) != boolean_type_node)
10544 parse_error_context
10546 "Incompatible type for loop conditional. Can't convert `%s' to "
10548 lang_printable_name (TREE_TYPE (expression), 0));
10549 return error_mark_node;
10551 /* Now we know things are allright, invert the condition, fold and
10553 TREE_OPERAND (node, 0) =
10554 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
10556 if (! integer_zerop (TREE_OPERAND (node, 0))
10557 && ctxp->current_loop != NULL_TREE
10558 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10559 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
10560 if (! integer_onep (TREE_OPERAND (node, 0)))
10561 CAN_COMPLETE_NORMALLY (node) = 1;
10564 TREE_TYPE (node) = void_type_node;
10568 /* 14.9 Switch statement */
10571 patch_switch_statement (node)
10574 tree se = TREE_OPERAND (node, 0), se_type;
10576 /* Complete the switch expression */
10577 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
10578 se_type = TREE_TYPE (se);
10579 /* The type of the switch expression must be char, byte, short or
10581 if (!JINTEGRAL_TYPE_P (se_type))
10583 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10584 parse_error_context (wfl_operator, "Incompatible type for `switch'. "
10585 "Can't convert `%s' to `int'",
10586 lang_printable_name (se_type, 0));
10587 /* This is what java_complete_tree will check */
10588 TREE_OPERAND (node, 0) = error_mark_node;
10589 return error_mark_node;
10592 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10594 /* Ready to return */
10595 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
10597 TREE_TYPE (node) = error_mark_node;
10598 return error_mark_node;
10600 TREE_TYPE (node) = void_type_node;
10601 TREE_SIDE_EFFECTS (node) = 1;
10602 CAN_COMPLETE_NORMALLY (node)
10603 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10604 || ! SWITCH_HAS_DEFAULT (node);
10608 /* 14.18 The try statement */
10610 /* Wrap BLOCK around a LABELED_BLOCK, set DECL to the newly generated
10611 exit labeld and issue a jump to FINALLY_LABEL:
10615 <orignal_statments>
10622 build_jump_to_finally (block, decl, finally_label, type)
10623 tree block, decl, finally_label, type;
10626 tree new_block = build (LABELED_BLOCK_EXPR, type,
10627 create_label_decl (generate_name ()), block);
10629 stmt = build (MODIFY_EXPR, void_type_node, decl,
10630 build_address_of (LABELED_BLOCK_LABEL (new_block)));
10631 TREE_SIDE_EFFECTS (stmt) = 1;
10632 CAN_COMPLETE_NORMALLY (stmt) = 1;
10633 add_stmt_to_block (block, type, stmt);
10634 stmt = build (GOTO_EXPR, void_type_node, finally_label);
10635 TREE_SIDE_EFFECTS (stmt) = 1;
10636 add_stmt_to_block (block, type, stmt);
10641 build_try_statement (location, try_block, catches, finally)
10643 tree try_block, catches, finally;
10647 if (finally && ! flag_emit_class_files)
10649 /* This block defines a scope for the entire try[-catch]-finally
10650 sequence. It hold a local variable used to return from the
10651 finally using a computed goto. We call it
10652 return_from_finally (RFF). */
10653 rff = build_decl (VAR_DECL, generate_name (), return_address_type_node);
10655 /* Modification of the try block. */
10656 try_block = build_jump_to_finally (try_block, rff,
10657 FINALLY_EXPR_LABEL (finally),
10660 /* To the finally block: add the computed goto */
10661 add_stmt_to_block (FINALLY_EXPR_BLOCK (finally), NULL_TREE,
10662 build (GOTO_EXPR, void_type_node, rff));
10664 /* Modification of each catch blocks, if any */
10667 tree catch, catch_decl, catch_block, stmt;
10669 for (catch = catches; catch; catch = TREE_CHAIN (catch))
10670 TREE_OPERAND (catch, 0) =
10671 build_jump_to_finally (TREE_OPERAND (catch, 0), rff,
10672 FINALLY_EXPR_LABEL (finally),
10675 /* Plus, at the end of the list, we add the catch clause that
10676 will catch an uncaught exception, call finally and rethrow it:
10678 void *exception_parameter; (catch_decl)
10681 exception_parameter = _Jv_exception_info ();
10687 exception_parameter */
10688 catch_decl = build_decl (VAR_DECL, generate_name (), ptr_type_node);
10689 BUILD_ASSIGN_EXCEPTION_INFO (stmt, catch_decl);
10690 catch_block = build_expr_block (stmt, NULL_TREE);
10691 catch_block = build_jump_to_finally (catch_block, rff,
10692 FINALLY_EXPR_LABEL (finally),
10694 BUILD_THROW (stmt, catch_decl);
10695 catch_block = build_expr_block (catch_block, catch_decl);
10696 add_stmt_to_block (catch_block, void_type_node, stmt);
10698 /* Link the new handler to the existing list as the first
10699 entry. It will be the last one to be generated. */
10700 catch = build1 (CATCH_EXPR, void_type_node, catch_block);
10701 TREE_CHAIN (catch) = catches;
10706 node = build (TRY_EXPR, NULL_TREE, try_block, catches, finally);
10707 EXPR_WFL_LINECOL (node) = location;
10709 /* If we have a finally, surround this whole thing by a block where
10710 the RFF local variable is defined. */
10712 return (finally && ! flag_emit_class_files ? build_expr_block (node, rff)
10716 /* Get the catch clause block from an element of the catch clause
10717 list. If depends on whether a finally clause exists or node (in
10718 which case the original catch clause was surrounded by a
10719 LABELED_BLOCK_EXPR. */
10722 java_get_catch_block (node, finally_present_p)
10724 int finally_present_p;
10726 return (CATCH_EXPR_GET_EXPR (TREE_OPERAND (node, 0), finally_present_p));
10730 patch_try_statement (node)
10733 int error_found = 0;
10734 tree try = TREE_OPERAND (node, 0);
10735 /* Exception handlers are considered in left to right order */
10736 tree catch = nreverse (TREE_OPERAND (node, 1));
10737 tree finally = TREE_OPERAND (node, 2);
10738 int finally_p = (finally ? 1 : 0);
10739 tree current, caught_type_list = NULL_TREE;
10741 /* Check catch clauses, if any. Every time we find an error, we try
10742 to process the next catch clause. We process the catch clause before
10743 the try block so that when processing the try block we can check thrown
10744 exceptions againts the caught type list. */
10745 for (current = catch; current; current = TREE_CHAIN (current))
10747 tree carg_decl, carg_type;
10748 tree sub_current, catch_block, catch_clause;
10751 /* Always detect the last catch clause if a finally is
10752 present. This is the catch-all handler and it just needs to
10754 if (!TREE_CHAIN (current) && finally)
10756 TREE_OPERAND (current, 0) =
10757 java_complete_tree (TREE_OPERAND (current, 0));
10761 /* At this point, the structure of the catch clause is
10762 LABELED_BLOCK_EXPR (if we have a finally)
10763 CATCH_EXPR (catch node)
10764 BLOCK (with the decl of the parameter)
10766 MODIFY_EXPR (assignment of the catch parameter)
10767 BLOCK (catch clause block)
10768 LABEL_DECL (where to return after finally (if any))
10770 Since the structure of the catch clause depends on the
10771 presence of a finally, we use a function call to get to the
10773 catch_clause = java_get_catch_block (current, finally_p);
10774 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
10775 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
10777 /* Catch clauses can't have more than one parameter declared,
10778 but it's already enforced by the grammar. Make sure that the
10779 only parameter of the clause statement in of class Throwable
10780 or a subclass of Throwable, but that was done earlier. The
10781 catch clause parameter type has also been resolved. */
10783 /* Just make sure that the catch clause parameter type inherits
10784 from java.lang.Throwable */
10785 if (!inherits_from_p (carg_type, throwable_type_node))
10787 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10788 parse_error_context (wfl_operator,
10789 "Can't catch class `%s'. Catch clause "
10790 "parameter type must be a subclass of "
10791 "class `java.lang.Throwable'",
10792 lang_printable_name (carg_type, 0));
10797 /* Partial check for unreachable catch statement: The catch
10798 clause is reachable iff is no earlier catch block A in
10799 the try statement such that the type of the catch
10800 clause's parameter is the same as or a subclass of the
10801 type of A's parameter */
10803 for (sub_current = catch;
10804 sub_current != current; sub_current = TREE_CHAIN (sub_current))
10806 tree sub_catch_clause, decl;
10807 sub_catch_clause = java_get_catch_block (sub_current, finally_p);
10808 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
10810 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
10812 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10813 parse_error_context
10814 (wfl_operator, "`catch' not reached because of the catch "
10815 "clause at line %d", EXPR_WFL_LINENO (sub_current));
10816 unreachable = error_found = 1;
10820 /* Complete the catch clause block */
10821 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
10822 if (catch_block == error_mark_node)
10827 if (CAN_COMPLETE_NORMALLY (catch_block))
10828 CAN_COMPLETE_NORMALLY (node) = 1;
10829 TREE_OPERAND (current, 0) = catch_block;
10834 /* Things to do here: the exception must be thrown */
10836 /* Link this type to the caught type list */
10837 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
10840 PUSH_EXCEPTIONS (caught_type_list);
10841 if ((try = java_complete_tree (try)) == error_mark_node)
10843 if (CAN_COMPLETE_NORMALLY (try))
10844 CAN_COMPLETE_NORMALLY (node) = 1;
10847 /* Process finally */
10850 current = java_complete_tree (FINALLY_EXPR_BLOCK (finally));
10851 FINALLY_EXPR_BLOCK (finally) = current;
10852 if (current == error_mark_node)
10854 if (! CAN_COMPLETE_NORMALLY (current))
10855 CAN_COMPLETE_NORMALLY (node) = 0;
10858 /* Verification ends here */
10860 return error_mark_node;
10862 TREE_OPERAND (node, 0) = try;
10863 TREE_OPERAND (node, 1) = catch;
10864 TREE_OPERAND (node, 2) = finally;
10865 TREE_TYPE (node) = void_type_node;
10869 /* 14.17 The synchronized Statement */
10872 patch_synchronized_statement (node, wfl_op1)
10873 tree node, wfl_op1;
10875 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
10876 tree block = TREE_OPERAND (node, 1);
10878 tree enter, exit, expr_decl, assignment;
10880 if (expr == error_mark_node)
10882 block = java_complete_tree (block);
10886 /* The TYPE of expr must be a reference type */
10887 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
10889 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10890 parse_error_context (wfl_operator, "Incompatible type for `synchronized'"
10891 ". Can't convert `%s' to `java.lang.Object'",
10892 lang_printable_name (TREE_TYPE (expr), 0));
10893 return error_mark_node;
10896 /* Generate a try-finally for the synchronized statement, except
10897 that the handler that catches all throw exception calls
10898 _Jv_MonitorExit and then rethrow the exception.
10899 The synchronized statement is then implemented as:
10902 _Jv_MonitorEnter (expression)
10904 _Jv_MonitorExit (expression)
10908 e = _Jv_exception_info ();
10909 _Jv_MonitorExit (expression)
10913 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
10914 BUILD_MONITOR_ENTER (enter, expr_decl);
10915 BUILD_MONITOR_EXIT (exit, expr_decl);
10916 CAN_COMPLETE_NORMALLY (enter) = 1;
10917 CAN_COMPLETE_NORMALLY (exit) = 1;
10918 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
10919 TREE_SIDE_EFFECTS (assignment) = 1;
10920 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
10921 build (COMPOUND_EXPR, NULL_TREE,
10922 build (WITH_CLEANUP_EXPR, NULL_TREE,
10923 build (COMPOUND_EXPR, NULL_TREE,
10924 assignment, enter),
10927 node = build_expr_block (node, expr_decl);
10929 return java_complete_tree (node);
10932 /* 14.16 The throw Statement */
10935 patch_throw_statement (node, wfl_op1)
10936 tree node, wfl_op1;
10938 tree expr = TREE_OPERAND (node, 0);
10939 tree type = TREE_TYPE (expr);
10940 int unchecked_ok = 0, tryblock_throws_ok = 0;
10942 /* Thrown expression must be assignable to java.lang.Throwable */
10943 if (!try_reference_assignconv (throwable_type_node, expr))
10945 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10946 parse_error_context (wfl_operator, "Can't throw `%s'; it must be a "
10947 "subclass of class `java.lang.Throwable'",
10948 lang_printable_name (type, 0));
10949 /* If the thrown expression was a reference, we further the
10950 compile-time check. */
10951 if (!JREFERENCE_TYPE_P (type))
10952 return error_mark_node;
10955 /* At least one of the following must be true */
10957 /* The type of the throw expression is a not checked exception,
10958 i.e. is a unchecked expression. */
10959 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
10961 /* Throw is contained in a try statement and at least one catch
10962 clause can receive the thrown expression or the current method is
10963 declared to throw such an exception. Or, the throw statement is
10964 contained in a method or constructor declaration and the type of
10965 the Expression is assignable to at least one type listed in the
10966 throws clause the declaration. */
10967 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10969 tryblock_throws_ok =
10970 check_thrown_exceptions_do (TREE_TYPE (expr));
10971 if (!(unchecked_ok || tryblock_throws_ok))
10973 /* If there is a surrounding try block that has no matching
10974 clatch clause, report it first. A surrounding try block exits
10975 only if there is something after the list of checked
10976 exception thrown by the current function (if any). */
10977 if (IN_TRY_BLOCK_P ())
10978 parse_error_context (wfl_operator, "Checked exception `%s' can't be "
10979 "caught by any of the catch clause(s) "
10980 "of the surrounding `try' block",
10981 lang_printable_name (type, 0));
10982 /* If we have no surrounding try statement and the method doesn't have
10983 any throws, report it now. FIXME */
10984 else if (!EXCEPTIONS_P (currently_caught_type_list)
10985 && !tryblock_throws_ok)
10986 parse_error_context (wfl_operator, "Checked exception `%s' isn't "
10987 "thrown from a `try' block",
10988 lang_printable_name (type, 0));
10989 /* Otherwise, the current method doesn't have the appropriate
10990 throws declaration */
10992 parse_error_context (wfl_operator, "Checked exception `%s' doesn't "
10993 "match any of current method's `throws' "
10995 lang_printable_name (type, 0));
10996 return error_mark_node;
10999 /* If a throw statement is contained in a static initializer, then a
11000 compile-time check ensures that either its value is always an
11001 unchecked exception or its value is always caught by some try
11002 statement that contains it. FIXME, static initializer. */
11004 if (! flag_emit_class_files)
11005 BUILD_THROW (node, expr);
11009 /* Check that exception said to be thrown by method DECL can be
11010 effectively caught from where DECL is invoked. */
11013 check_thrown_exceptions (location, decl)
11018 /* For all the unchecked exceptions thrown by DECL */
11019 for (throws = DECL_FUNCTION_THROWS (decl); throws;
11020 throws = TREE_CHAIN (throws))
11021 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
11024 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
11025 if (DECL_NAME (decl) == get_identifier ("clone"))
11028 EXPR_WFL_LINECOL (wfl_operator) = location;
11029 parse_error_context
11030 (wfl_operator, "Exception `%s' must be caught, or it must be "
11031 "declared in the `throws' clause of `%s'",
11032 lang_printable_name (TREE_VALUE (throws), 0),
11033 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
11037 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
11038 try-catch blocks, OR is listed in the `throws' clause of the
11042 check_thrown_exceptions_do (exception)
11045 tree list = currently_caught_type_list;
11046 resolve_and_layout (exception, NULL_TREE);
11047 /* First, all the nested try-catch-finally at that stage. The
11048 last element contains `throws' clause exceptions, if any. */
11049 if (IS_UNCHECKED_EXCEPTION_P (exception))
11054 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
11055 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
11057 list = TREE_CHAIN (list);
11063 purge_unchecked_exceptions (mdecl)
11066 tree throws = DECL_FUNCTION_THROWS (mdecl);
11067 tree new = NULL_TREE;
11071 tree next = TREE_CHAIN (throws);
11072 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
11074 TREE_CHAIN (throws) = new;
11079 /* List is inverted here, but it doesn't matter */
11080 DECL_FUNCTION_THROWS (mdecl) = new;
11083 /* 15.24 Conditional Operator ?: */
11086 patch_conditional_expr (node, wfl_cond, wfl_op1)
11087 tree node, wfl_cond, wfl_op1;
11089 tree cond = TREE_OPERAND (node, 0);
11090 tree op1 = TREE_OPERAND (node, 1);
11091 tree op2 = TREE_OPERAND (node, 2);
11092 tree resulting_type = NULL_TREE;
11093 tree t1, t2, patched;
11094 int error_found = 0;
11096 /* Operands of ?: might be StringBuffers crafted as a result of a
11097 string concatenation. Obtain a descent operand here. */
11098 if ((patched = patch_string (op1)))
11099 TREE_OPERAND (node, 1) = op1 = patched;
11100 if ((patched = patch_string (op2)))
11101 TREE_OPERAND (node, 2) = op2 = patched;
11103 t1 = TREE_TYPE (op1);
11104 t2 = TREE_TYPE (op2);
11106 /* The first expression must be a boolean */
11107 if (TREE_TYPE (cond) != boolean_type_node)
11109 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
11110 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11111 "convert `%s' to `boolean'",
11112 lang_printable_name (TREE_TYPE (cond), 0));
11116 /* Second and third can be numeric, boolean (i.e. primitive),
11117 references or null. Anything else results in an error */
11118 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
11119 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
11120 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
11121 || (t1 == boolean_type_node && t2 == boolean_type_node)))
11124 /* Determine the type of the conditional expression. Same types are
11125 easy to deal with */
11127 resulting_type = t1;
11129 /* There are different rules for numeric types */
11130 else if (JNUMERIC_TYPE_P (t1))
11132 /* if byte/short found, the resulting type is short */
11133 if ((t1 == byte_type_node && t2 == short_type_node)
11134 || (t1 == short_type_node && t2 == byte_type_node))
11135 resulting_type = short_type_node;
11137 /* If t1 is a constant int and t2 is of type byte, short or char
11138 and t1's value fits in t2, then the resulting type is t2 */
11139 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
11140 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
11141 resulting_type = t2;
11143 /* If t2 is a constant int and t1 is of type byte, short or char
11144 and t2's value fits in t1, then the resulting type is t1 */
11145 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
11146 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
11147 resulting_type = t1;
11149 /* Otherwise, binary numeric promotion is applied and the
11150 resulting type is the promoted type of operand 1 and 2 */
11152 resulting_type = binary_numeric_promotion (t2, t2,
11153 &TREE_OPERAND (node, 1),
11154 &TREE_OPERAND (node, 2));
11157 /* Cases of a reference and a null type */
11158 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
11159 resulting_type = t1;
11161 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
11162 resulting_type = t2;
11164 /* Last case: different reference types. If a type can be converted
11165 into the other one by assignment conversion, the latter
11166 determines the type of the expression */
11167 else if ((resulting_type = try_reference_assignconv (t1, op2)))
11168 resulting_type = promote_type (t1);
11170 else if ((resulting_type = try_reference_assignconv (t2, op1)))
11171 resulting_type = promote_type (t2);
11173 /* If we don't have any resulting type, we're in trouble */
11174 if (!resulting_type)
11176 char *t = strdup (lang_printable_name (t1, 0));
11177 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
11178 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11179 "convert `%s' to `%s'", t,
11180 lang_printable_name (t2, 0));
11187 TREE_TYPE (node) = error_mark_node;
11188 return error_mark_node;
11191 TREE_TYPE (node) = resulting_type;
11192 TREE_SET_CODE (node, COND_EXPR);
11193 CAN_COMPLETE_NORMALLY (node) = 1;
11197 /* Try to constant fold NODE.
11198 If NODE is not a constant expression, return NULL_EXPR.
11199 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
11202 fold_constant_for_init (node, context)
11206 tree op0, op1, val;
11207 enum tree_code code = TREE_CODE (node);
11209 if (code == INTEGER_CST || code == REAL_CST || code == STRING_CST)
11211 if (TREE_TYPE (node) != NULL_TREE)
11219 case TRUNC_MOD_EXPR:
11227 case TRUTH_ANDIF_EXPR:
11228 case TRUTH_ORIF_EXPR:
11235 op0 = TREE_OPERAND (node, 0);
11236 op1 = TREE_OPERAND (node, 1);
11237 val = fold_constant_for_init (op0, context);
11238 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11240 TREE_OPERAND (node, 0) = val;
11241 val = fold_constant_for_init (op1, context);
11242 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11244 TREE_OPERAND (node, 1) = val;
11245 return patch_binop (node, op0, op1);
11247 case UNARY_PLUS_EXPR:
11249 case TRUTH_NOT_EXPR:
11252 op0 = TREE_OPERAND (node, 0);
11253 val = fold_constant_for_init (op0, context);
11254 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11256 TREE_OPERAND (node, 0) = val;
11257 return patch_unaryop (node, op0);
11261 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
11262 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11264 TREE_OPERAND (node, 0) = val;
11265 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
11266 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11268 TREE_OPERAND (node, 1) = val;
11269 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
11270 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11272 TREE_OPERAND (node, 2) = val;
11273 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
11274 : TREE_OPERAND (node, 2);
11277 if (! FIELD_STATIC (node) || ! FIELD_FINAL (node)
11278 || DECL_INITIAL (node) == NULL_TREE)
11280 val = DECL_INITIAL (node);
11281 /* Guard against infinite recursion. */
11282 DECL_INITIAL (node) = NULL_TREE;
11283 val = fold_constant_for_init (val, DECL_CONTEXT (node));
11284 DECL_INITIAL (node) = val;
11287 case EXPR_WITH_FILE_LOCATION:
11288 /* Compare java_complete_tree and resolve_expression_name. */
11289 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11290 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11292 tree name = EXPR_WFL_NODE (node);
11294 if (PRIMARY_P (node))
11296 else if (! QUALIFIED_P (name))
11298 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
11299 if (! FIELD_STATIC (decl))
11301 return fold_constant_for_init (decl, decl);
11306 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
11307 qualify_ambiguous_name (node);
11308 if (resolve_field_access (node, &decl, NULL)
11309 && decl != NULL_TREE)
11310 return fold_constant_for_init (decl, decl);
11317 op0 = TREE_OPERAND (node, 0);
11318 val = fold_constant_for_init (op0, context);
11319 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11321 TREE_OPERAND (node, 0) = val;
11325 #ifdef USE_COMPONENT_REF
11327 case COMPONENT_REF:
11336 #ifdef USE_COMPONENT_REF
11337 /* Context is 'T' for TypeName, 'P' for PackageName,
11338 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
11341 resolve_simple_name (name, context)
11348 resolve_qualified_name (name, context)