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 void declare_local_variables PROTO ((int, tree, tree));
103 static void source_start_java_method PROTO ((tree));
104 static void source_end_java_method PROTO ((void));
105 static void expand_start_java_method PROTO ((tree));
106 static tree find_name_in_single_imports PROTO ((tree));
107 static void check_abstract_method_header PROTO ((tree));
108 static tree lookup_java_interface_method2 PROTO ((tree, tree));
109 static tree resolve_expression_name PROTO ((tree, tree *));
110 static tree maybe_create_class_interface_decl PROTO ((tree, tree, tree));
111 static int check_class_interface_creation PROTO ((int, int, tree,
113 static tree patch_method_invocation PROTO ((tree, tree, tree,
115 static int breakdown_qualified PROTO ((tree *, tree *, tree));
116 static tree resolve_and_layout PROTO ((tree, tree));
117 static tree resolve_no_layout PROTO ((tree, tree));
118 static int invocation_mode PROTO ((tree, int));
119 static tree find_applicable_accessible_methods_list PROTO ((int, tree,
121 static tree find_most_specific_methods_list PROTO ((tree));
122 static int argument_types_convertible PROTO ((tree, tree));
123 static tree patch_invoke PROTO ((tree, tree, tree));
124 static tree lookup_method_invoke PROTO ((int, tree, tree, tree, tree));
125 static tree register_incomplete_type PROTO ((int, tree, tree, tree));
126 static tree obtain_incomplete_type PROTO ((tree));
127 static tree java_complete_lhs PROTO ((tree));
128 static tree java_complete_tree PROTO ((tree));
129 static void java_complete_expand_method PROTO ((tree));
130 static int unresolved_type_p PROTO ((tree, tree *));
131 static void create_jdep_list PROTO ((struct parser_ctxt *));
132 static tree build_expr_block PROTO ((tree, tree));
133 static tree enter_block PROTO ((void));
134 static tree enter_a_block PROTO ((tree));
135 static tree exit_block PROTO ((void));
136 static tree lookup_name_in_blocks PROTO ((tree));
137 static void maybe_absorb_scoping_blocks PROTO ((void));
138 static tree build_method_invocation PROTO ((tree, tree));
139 static tree build_new_invocation PROTO ((tree, tree));
140 static tree build_assignment PROTO ((int, int, tree, tree));
141 static tree build_binop PROTO ((enum tree_code, int, tree, tree));
142 static int check_final_assignment PROTO ((tree ,tree));
143 static tree patch_assignment PROTO ((tree, tree, tree ));
144 static tree patch_binop PROTO ((tree, tree, tree));
145 static tree build_unaryop PROTO ((int, int, tree));
146 static tree build_incdec PROTO ((int, int, tree, int));
147 static tree patch_unaryop PROTO ((tree, tree));
148 static tree build_cast PROTO ((int, tree, tree));
149 static tree build_null_of_type PROTO ((tree));
150 static tree patch_cast PROTO ((tree, tree));
151 static int valid_ref_assignconv_cast_p PROTO ((tree, tree, int));
152 static int valid_builtin_assignconv_identity_widening_p PROTO ((tree, tree));
153 static int valid_cast_to_p PROTO ((tree, tree));
154 static int valid_method_invocation_conversion_p PROTO ((tree, tree));
155 static tree try_builtin_assignconv PROTO ((tree, tree, tree));
156 static tree try_reference_assignconv PROTO ((tree, tree));
157 static tree build_unresolved_array_type PROTO ((tree));
158 static tree build_array_from_name PROTO ((tree, tree, tree, tree *));
159 static tree build_array_ref PROTO ((int, tree, tree));
160 static tree patch_array_ref PROTO ((tree));
161 static tree make_qualified_name PROTO ((tree, tree, int));
162 static tree merge_qualified_name PROTO ((tree, tree));
163 static tree make_qualified_primary PROTO ((tree, tree, int));
164 static int resolve_qualified_expression_name PROTO ((tree, tree *,
166 static void qualify_ambiguous_name PROTO ((tree));
167 static void maybe_generate_clinit PROTO ((void));
168 static tree resolve_field_access PROTO ((tree, tree *, tree *));
169 static tree build_newarray_node PROTO ((tree, tree, int));
170 static tree patch_newarray PROTO ((tree));
171 static tree resolve_type_during_patch PROTO ((tree));
172 static tree build_this PROTO ((int));
173 static tree build_return PROTO ((int, tree));
174 static tree patch_return PROTO ((tree));
175 static tree maybe_access_field PROTO ((tree, tree, tree));
176 static int complete_function_arguments PROTO ((tree));
177 static int check_for_static_method_reference PROTO ((tree, tree, tree, tree, tree));
178 static int not_accessible_p PROTO ((tree, tree, int));
179 static void check_deprecation PROTO ((tree, tree));
180 static int class_in_current_package PROTO ((tree));
181 static tree build_if_else_statement PROTO ((int, tree, tree, tree));
182 static tree patch_if_else_statement PROTO ((tree));
183 static tree add_stmt_to_compound PROTO ((tree, tree, tree));
184 static tree add_stmt_to_block PROTO ((tree, tree, tree));
185 static tree patch_exit_expr PROTO ((tree));
186 static tree build_labeled_block PROTO ((int, tree));
187 static tree generate_labeled_block PROTO (());
188 static tree complete_labeled_statement PROTO ((tree, tree));
189 static tree build_bc_statement PROTO ((int, int, tree));
190 static tree patch_bc_statement PROTO ((tree));
191 static tree patch_loop_statement PROTO ((tree));
192 static tree build_new_loop PROTO ((tree));
193 static tree build_loop_body PROTO ((int, tree, int));
194 static tree complete_loop_body PROTO ((int, tree, tree, int));
195 static tree build_debugable_stmt PROTO ((int, tree));
196 static tree complete_for_loop PROTO ((int, tree, tree, tree));
197 static tree patch_switch_statement PROTO ((tree));
198 static tree string_constant_concatenation PROTO ((tree, tree));
199 static tree build_string_concatenation PROTO ((tree, tree));
200 static tree patch_string_cst PROTO ((tree));
201 static tree patch_string PROTO ((tree));
202 static tree build_jump_to_finally PROTO ((tree, tree, tree, tree));
203 static tree build_try_statement PROTO ((int, tree, tree));
204 static tree build_try_finally_statement PROTO ((int, 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));
241 static tree strip_out_static_field_access_decl PROTO ((tree));
243 /* Number of error found so far. */
244 int java_error_count;
245 /* Number of warning found so far. */
246 int java_warning_count;
248 /* The current parser context */
249 static struct parser_ctxt *ctxp;
251 /* List of things that were anlyzed for which code will be generated */
252 static struct parser_ctxt *ctxp_for_generation = NULL;
254 /* binop_lookup maps token to tree_code. It is used where binary
255 operations are involved and required by the parser. RDIV_EXPR
256 covers both integral/floating point division. The code is changed
257 once the type of both operator is worked out. */
259 static enum tree_code binop_lookup[19] =
261 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
262 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
263 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
264 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
265 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
267 #define BINOP_LOOKUP(VALUE) \
268 binop_lookup [((VALUE) - PLUS_TK)% \
269 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
271 /* Fake WFL used to report error message. It is initialized once if
272 needed and reused with it's location information is overriden. */
273 tree wfl_operator = NULL_TREE;
275 /* The "$L" identifier we use to create labels. */
276 static tree label_id = NULL_TREE;
278 /* The "StringBuffer" identifier used for the String `+' operator. */
279 static tree wfl_string_buffer = NULL_TREE;
281 /* The "append" identifier used for String `+' operator. */
282 static tree wfl_append = NULL_TREE;
284 /* The "toString" identifier used for String `+' operator. */
285 static tree wfl_to_string = NULL_TREE;
287 /* The "java.lang" import qualified name. */
288 static tree java_lang_id = NULL_TREE;
290 /* The "java.lang.Cloneable" qualified name. */
291 static tree java_lang_cloneable = NULL_TREE;
306 /* Things defined here have to match the order of what's in the
307 binop_lookup table. */
309 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
310 %token LS_TK SRS_TK ZRS_TK
311 %token AND_TK XOR_TK OR_TK
312 %token BOOL_AND_TK BOOL_OR_TK
313 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
315 /* This maps to the same binop_lookup entry than the token above */
317 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
319 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
320 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
323 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
325 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
326 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
327 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
328 %token PAD_TK ABSTRACT_TK MODIFIER_TK
330 /* Keep those two in order, too */
331 %token DECR_TK INCR_TK
333 /* From now one, things can be in any order */
335 %token DEFAULT_TK IF_TK THROW_TK
336 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
337 %token THROWS_TK BREAK_TK IMPORT_TK
338 %token ELSE_TK INSTANCEOF_TK RETURN_TK
339 %token VOID_TK CATCH_TK INTERFACE_TK
340 %token CASE_TK EXTENDS_TK FINALLY_TK
341 %token SUPER_TK WHILE_TK CLASS_TK
342 %token SWITCH_TK CONST_TK TRY_TK
343 %token FOR_TK NEW_TK CONTINUE_TK
344 %token GOTO_TK PACKAGE_TK THIS_TK
346 %token BYTE_TK SHORT_TK INT_TK LONG_TK
347 %token CHAR_TK INTEGRAL_TK
349 %token FLOAT_TK DOUBLE_TK FP_TK
353 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
355 %token ASSIGN_ANY_TK ASSIGN_TK
356 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
358 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
359 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
361 %type <value> modifiers MODIFIER_TK
363 %type <node> super ID_TK identifier
364 %type <node> name simple_name qualified_name
365 %type <node> class_declaration type_declaration compilation_unit
366 field_declaration method_declaration extends_interfaces
367 interfaces interface_type_list
368 interface_declaration class_member_declaration
369 import_declarations package_declaration
370 type_declarations interface_body
371 interface_member_declaration constant_declaration
372 interface_member_declarations interface_type
373 abstract_method_declaration interface_type_list
374 %type <node> class_body_declaration class_member_declaration
375 static_initializer constructor_declaration block
376 %type <node> class_body_declarations constructor_header
377 %type <node> class_or_interface_type class_type class_type_list
378 constructor_declarator explicit_constructor_invocation
379 %type <node> dim_expr dim_exprs this_or_super throws
381 %type <node> variable_declarator_id variable_declarator
382 variable_declarators variable_initializer
383 variable_initializers constructor_body
386 %type <node> class_body block_end
387 %type <node> statement statement_without_trailing_substatement
388 labeled_statement if_then_statement label_decl
389 if_then_else_statement while_statement for_statement
390 statement_nsi labeled_statement_nsi do_statement
391 if_then_else_statement_nsi while_statement_nsi
392 for_statement_nsi statement_expression_list for_init
393 for_update statement_expression expression_statement
394 primary_no_new_array expression primary
395 array_creation_expression array_type
396 class_instance_creation_expression field_access
397 method_invocation array_access something_dot_new
398 argument_list postfix_expression while_expression
399 post_increment_expression post_decrement_expression
400 unary_expression_not_plus_minus unary_expression
401 pre_increment_expression pre_decrement_expression
402 unary_expression_not_plus_minus cast_expression
403 multiplicative_expression additive_expression
404 shift_expression relational_expression
405 equality_expression and_expression
406 exclusive_or_expression inclusive_or_expression
407 conditional_and_expression conditional_or_expression
408 conditional_expression assignment_expression
409 left_hand_side assignment for_header for_begin
410 constant_expression do_statement_begin empty_statement
411 switch_statement synchronized_statement throw_statement
412 try_statement switch_expression switch_block
413 catches catch_clause catch_clause_parameter finally
414 %type <node> return_statement break_statement continue_statement
416 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
417 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
418 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
419 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
420 %type <operator> ASSIGN_ANY_TK assignment_operator
421 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
422 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
423 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
424 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK
425 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
426 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
427 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
429 %type <node> method_body
431 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
432 STRING_LIT_TK NULL_TK VOID_TK
434 %type <node> IF_TK WHILE_TK FOR_TK
436 %type <node> formal_parameter_list formal_parameter
437 method_declarator method_header
439 %type <node> primitive_type reference_type type
440 BOOLEAN_TK INTEGRAL_TK FP_TK
443 /* 19.2 Production from 2.3: The Syntactic Grammar */
449 /* 19.3 Productions from 3: Lexical structure */
459 /* 19.4 Productions from 4: Types, Values and Variables */
472 class_or_interface_type
476 class_or_interface_type:
481 class_or_interface_type /* Default rule */
485 class_or_interface_type
489 primitive_type OSB_TK CSB_TK
491 $$ = build_java_array_type ($1, -1);
492 CLASS_LOADED_P ($$) = 1;
495 { $$ = build_unresolved_array_type ($1); }
496 | array_type OSB_TK CSB_TK
497 { $$ = build_unresolved_array_type ($1); }
498 | primitive_type OSB_TK error
499 {RULE ("']' expected"); RECOVER;}
500 | array_type OSB_TK error
501 {RULE ("']' expected"); RECOVER;}
504 /* 19.5 Productions from 6: Names */
506 simple_name /* Default rule */
507 | qualified_name /* Default rule */
511 identifier /* Default rule */
515 name DOT_TK identifier
516 { $$ = make_qualified_name ($1, $3, $2.location); }
523 /* 19.6: Production from 7: Packages */
526 | package_declaration
527 | import_declarations
529 | package_declaration import_declarations
530 | package_declaration type_declarations
531 | import_declarations type_declarations
532 | package_declaration import_declarations type_declarations
540 | import_declarations import_declaration
548 | type_declarations type_declaration
552 PACKAGE_TK name SC_TK
553 { ctxp->package = EXPR_WFL_NODE ($2); }
555 {yyerror ("Missing name"); RECOVER;}
556 | PACKAGE_TK name error
557 {yyerror ("';' expected"); RECOVER;}
561 single_type_import_declaration
562 | type_import_on_demand_declaration
565 single_type_import_declaration:
568 tree name = EXPR_WFL_NODE ($2), node, last_name;
569 int i = IDENTIFIER_LENGTH (name)-1;
570 char *last = &IDENTIFIER_POINTER (name)[i];
571 while (last != IDENTIFIER_POINTER (name))
577 last_name = get_identifier (++last);
578 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
580 tree err = find_name_in_single_imports (last_name);
581 if (err && err != name)
583 ($2, "Ambiguous class: `%s' and `%s'",
584 IDENTIFIER_POINTER (name),
585 IDENTIFIER_POINTER (err));
587 REGISTER_IMPORT ($2, last_name)
590 REGISTER_IMPORT ($2, last_name);
593 {yyerror ("Missing name"); RECOVER;}
594 | IMPORT_TK name error
595 {yyerror ("';' expected"); RECOVER;}
598 type_import_on_demand_declaration:
599 IMPORT_TK name DOT_TK MULT_TK SC_TK
601 tree name = EXPR_WFL_NODE ($2);
602 /* Don't import java.lang.* twice. */
603 if (name != java_lang_id)
605 tree node = build_tree_list ($2, NULL_TREE);
606 read_import_dir ($2);
607 TREE_CHAIN (node) = ctxp->import_demand_list;
608 ctxp->import_demand_list = node;
611 | IMPORT_TK name DOT_TK error
612 {yyerror ("'*' expected"); RECOVER;}
613 | IMPORT_TK name DOT_TK MULT_TK error
614 {yyerror ("';' expected"); RECOVER;}
620 maybe_generate_finit ();
621 maybe_generate_clinit ();
624 | interface_declaration
630 yyerror ("Class or interface declaration expected");
634 /* 19.7 Shortened from the original:
635 modifiers: modifier | modifiers modifier
636 modifier: any of public... */
642 | modifiers MODIFIER_TK
647 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
648 java_accstring_lookup (acc));
656 /* 19.8.1 Production from $8.1: Class Declaration */
658 modifiers CLASS_TK identifier super interfaces
659 { create_class ($1, $3, $4, $5); }
664 | CLASS_TK identifier super interfaces
665 { create_class (0, $2, $3, $4); }
670 | modifiers CLASS_TK error
671 {yyerror ("Missing class name"); RECOVER;}
673 {yyerror ("Missing class name"); RECOVER;}
674 | CLASS_TK identifier error
676 if (!ctxp->class_err) yyerror ("'{' expected");
679 | modifiers CLASS_TK identifier error
680 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
685 | EXTENDS_TK class_type
687 | EXTENDS_TK class_type error
688 {yyerror ("'{' expected"); ctxp->class_err=1;}
690 {yyerror ("Missing super class name"); ctxp->class_err=1;}
695 | IMPLEMENTS_TK interface_type_list
697 | IMPLEMENTS_TK error
700 yyerror ("Missing interface name");
707 ctxp->interface_number = 1;
708 $$ = build_tree_list ($1, NULL_TREE);
710 | interface_type_list C_TK interface_type
712 ctxp->interface_number++;
713 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
715 | interface_type_list C_TK error
716 {yyerror ("Missing interface name"); RECOVER;}
721 { $$ = ctxp->current_parsed_class; }
722 | OCB_TK class_body_declarations CCB_TK
723 { $$ = ctxp->current_parsed_class; }
726 class_body_declarations:
727 class_body_declaration
728 | class_body_declarations class_body_declaration
731 class_body_declaration:
732 class_member_declaration
734 | constructor_declaration
735 | block /* Added, JDK1.1, instance initializer */
736 { $$ = parse_jdk1_1_error ("instance initializer"); }
739 class_member_declaration:
741 | field_declaration SC_TK
744 | class_declaration /* Added, JDK1.1 inner classes */
745 { $$ = parse_jdk1_1_error ("inner classe declaration"); }
746 | interface_declaration /* Added, JDK1.1 inner classes */
747 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
750 /* 19.8.2 Productions from 8.3: Field Declarations */
752 type variable_declarators SC_TK
753 { register_fields (0, $1, $2); }
754 | modifiers type variable_declarators SC_TK
757 ("Illegal modifier `%s' for field declaration",
758 $1, FIELD_MODIFIERS);
759 check_modifiers_consistency ($1);
760 register_fields ($1, $2, $3);
764 variable_declarators:
765 /* Should we use build_decl_list () instead ? FIXME */
766 variable_declarator /* Default rule */
767 | variable_declarators C_TK variable_declarator
768 { $$ = chainon ($1, $3); }
769 | variable_declarators C_TK error
770 {yyerror ("Missing term"); RECOVER;}
774 variable_declarator_id
775 { $$ = build_tree_list ($1, NULL_TREE); }
776 | variable_declarator_id ASSIGN_TK variable_initializer
778 if (java_error_count)
781 ($1, build_assignment ($2.token, $2.location, $1, $3));
783 | variable_declarator_id ASSIGN_TK error
785 yyerror ("Missing variable initializer");
786 $$ = build_tree_list ($1, NULL_TREE);
789 | variable_declarator_id ASSIGN_TK variable_initializer error
791 yyerror ("';' expected");
792 $$ = build_tree_list ($1, NULL_TREE);
797 variable_declarator_id:
799 | variable_declarator_id OSB_TK CSB_TK
800 { $$ = build_unresolved_array_type ($1); }
802 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
803 | variable_declarator_id OSB_TK error
804 {yyerror ("']' expected"); DRECOVER(vdi);}
805 | variable_declarator_id CSB_TK error
806 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
809 variable_initializer:
814 /* 19.8.3 Productions from 8.4: Method Declarations */
818 current_function_decl = $1;
819 source_start_java_method (current_function_decl);
822 { complete_method_declaration ($3); }
823 | method_header error
824 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
828 type method_declarator throws
829 { $$ = method_header (0, $1, $2, $3); }
830 | VOID_TK method_declarator throws
831 { $$ = method_header (0, void_type_node, $2, $3); }
832 | modifiers type method_declarator throws
833 { $$ = method_header ($1, $2, $3, $4); }
834 | modifiers VOID_TK method_declarator throws
835 { $$ = method_header ($1, void_type_node, $3, $4); }
838 | modifiers type error
841 {yyerror ("Identifier expected"); RECOVER;}
842 | modifiers VOID_TK error
843 {yyerror ("Identifier expected"); RECOVER;}
846 yyerror ("Invalid method declaration, return type required");
852 identifier OP_TK CP_TK
853 { $$ = method_declarator ($1, NULL_TREE); }
854 | identifier OP_TK formal_parameter_list CP_TK
855 { $$ = method_declarator ($1, $3); }
856 | method_declarator OSB_TK CSB_TK
858 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
860 build_unresolved_array_type (TREE_PURPOSE ($1));
861 parse_warning_context
863 "Discouraged form of returned type specification");
865 | identifier OP_TK error
866 {yyerror ("')' expected"); DRECOVER(method_declarator);}
867 | method_declarator OSB_TK error
868 {yyerror ("']' expected"); RECOVER;}
871 formal_parameter_list:
874 ctxp->formal_parameter_number = 1;
876 | formal_parameter_list C_TK formal_parameter
878 ctxp->formal_parameter_number += 1;
879 $$ = chainon ($1, $3);
881 | formal_parameter_list C_TK error
882 {yyerror ("Missing formal parameter term"); RECOVER;}
886 type variable_declarator_id
888 $$ = build_tree_list ($2, $1);
890 | modifiers type variable_declarator_id /* Added, JDK1.1 final parms */
891 { $$ = parse_jdk1_1_error ("final parameters"); }
893 {yyerror ("Missing identifier"); RECOVER;}
894 | modifiers type error
896 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
897 yyerror ("Missing identifier"); RECOVER;
903 | THROWS_TK class_type_list
906 {yyerror ("Missing class type term"); RECOVER;}
911 { $$ = build_tree_list ($1, $1); }
912 | class_type_list C_TK class_type
913 { $$ = tree_cons ($3, $3, $1); }
914 | class_type_list C_TK error
915 {yyerror ("Missing class type term"); RECOVER;}
922 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
925 /* 19.8.4 Productions from 8.5: Static Initializers */
929 RULE ("STATIC_INITIALIZER");
931 | static block SC_TK /* Shouldn't be here. FIXME */
933 RULE ("STATIC_INITIALIZER");
937 static: /* Test lval.sub_token here */
940 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
944 /* 19.8.5 Productions from 8.6: Constructor Declarations */
945 constructor_declaration:
948 current_function_decl = $1;
949 source_start_java_method (current_function_decl);
952 { complete_method_declaration ($3); }
956 constructor_declarator throws
957 { $$ = method_header (0, NULL_TREE, $1, $2); }
958 | modifiers constructor_declarator throws
959 { $$ = method_header ($1, NULL_TREE, $2, $3); }
962 constructor_declarator:
963 simple_name OP_TK CP_TK
964 { $$ = method_declarator ($1, NULL_TREE); }
965 | simple_name OP_TK formal_parameter_list CP_TK
966 { $$ = method_declarator ($1, $3); }
970 /* Unlike regular method, we always need a complete (empty)
971 body so we can safely perform all the required code
972 addition (super invocation and field initialization) */
973 block_begin block_end
975 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
978 | block_begin explicit_constructor_invocation block_end
980 | block_begin block_statements block_end
982 | block_begin explicit_constructor_invocation block_statements block_end
986 /* Error recovery for that rule moved down expression_statement: rule. */
987 explicit_constructor_invocation:
988 this_or_super OP_TK CP_TK SC_TK
990 $$ = build_method_invocation ($1, NULL_TREE);
991 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
992 $$ = java_method_add_stmt (current_function_decl, $$);
994 | this_or_super OP_TK argument_list CP_TK SC_TK
996 $$ = build_method_invocation ($1, $3);
997 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
998 $$ = java_method_add_stmt (current_function_decl, $$);
1000 /* Added, JDK1.1 inner classes. Modified because the rule
1001 'primary' couldn't work. */
1002 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1003 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1004 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1005 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1008 this_or_super: /* Added, simplifies error diagnostics */
1011 tree wfl = build_wfl_node (this_identifier_node,
1012 input_filename, 0, 0);
1013 EXPR_WFL_LINECOL (wfl) = $1.location;
1018 tree wfl = build_wfl_node (super_identifier_node,
1019 input_filename, 0, 0);
1020 EXPR_WFL_LINECOL (wfl) = $1.location;
1025 /* 19.9 Productions from 9: Interfaces */
1026 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1027 interface_declaration:
1028 INTERFACE_TK identifier
1029 { create_interface (0, $2, NULL_TREE); }
1034 | modifiers INTERFACE_TK identifier
1035 { create_interface ($1, $3, NULL_TREE); }
1040 | INTERFACE_TK identifier extends_interfaces
1041 { create_interface (0, $2, $3); }
1046 | modifiers INTERFACE_TK identifier extends_interfaces
1047 { create_interface ($1, $3, $4); }
1052 | INTERFACE_TK identifier error
1053 {yyerror ("'{' expected"); RECOVER;}
1054 | modifiers INTERFACE_TK identifier error
1055 {yyerror ("'{' expected"); RECOVER;}
1059 EXTENDS_TK interface_type
1061 ctxp->interface_number = 1;
1062 $$ = build_tree_list ($2, NULL_TREE);
1064 | extends_interfaces C_TK interface_type
1066 ctxp->interface_number++;
1067 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1070 {yyerror ("Invalid interface type"); RECOVER;}
1071 | extends_interfaces C_TK error
1072 {yyerror ("Missing term"); RECOVER;}
1078 | OCB_TK interface_member_declarations CCB_TK
1082 interface_member_declarations:
1083 interface_member_declaration
1084 | interface_member_declarations interface_member_declaration
1087 interface_member_declaration:
1088 constant_declaration
1089 | abstract_method_declaration
1090 | class_declaration /* Added, JDK1.1 inner classes */
1091 { $$ = parse_jdk1_1_error ("inner class declaration"); }
1092 | interface_declaration /* Added, JDK1.1 inner classes */
1093 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
1096 constant_declaration:
1100 abstract_method_declaration:
1103 check_abstract_method_header ($1);
1104 current_function_decl = NULL_TREE; /* FIXME ? */
1106 | method_header error
1107 {yyerror ("';' expected"); RECOVER;}
1110 /* 19.10 Productions from 10: Arrays */
1113 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1114 | OCB_TK variable_initializers CCB_TK
1115 { $$ = build_new_array_init ($1.location, $2); }
1116 | OCB_TK variable_initializers C_TK CCB_TK
1117 { $$ = build_new_array_init ($1.location, $2); }
1120 variable_initializers:
1121 variable_initializer
1123 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1126 | variable_initializers C_TK variable_initializer
1128 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1130 | variable_initializers C_TK error
1131 {yyerror ("Missing term"); RECOVER;}
1134 /* 19.11 Production from 14: Blocks and Statements */
1137 { $$ = empty_stmt_node; }
1138 | block_begin block_statements block_end
1150 maybe_absorb_scoping_blocks ();
1157 | block_statements block_statement
1161 local_variable_declaration_statement
1163 { java_method_add_stmt (current_function_decl, $1); }
1164 | class_declaration /* Added, JDK1.1 inner classes */
1165 { parse_jdk1_1_error ("inner class declaration"); }
1168 local_variable_declaration_statement:
1169 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1172 local_variable_declaration:
1173 type variable_declarators
1174 { declare_local_variables (0, $1, $2); }
1175 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
1176 { declare_local_variables ($1, $2, $3); }
1180 statement_without_trailing_substatement
1183 | if_then_else_statement
1187 /* If the for loop is unlabeled, we must return the
1188 block it was defined it. It our last chance to
1189 get a hold on it. */
1190 if (!LOOP_HAS_LABEL_P ($$))
1196 statement_without_trailing_substatement
1197 | labeled_statement_nsi
1198 | if_then_else_statement_nsi
1199 | while_statement_nsi
1203 statement_without_trailing_substatement:
1206 | expression_statement
1210 | continue_statement
1212 | synchronized_statement
1219 { $$ = empty_stmt_node; }
1223 identifier REL_CL_TK
1225 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1226 EXPR_WFL_NODE ($1));
1228 push_labeled_block ($$);
1229 PUSH_LABELED_BLOCK ($$);
1234 label_decl statement
1236 $$ = complete_labeled_statement ($1, $2);
1237 pop_labeled_block ();
1238 POP_LABELED_BLOCK ();
1241 {yyerror ("':' expected"); RECOVER;}
1244 labeled_statement_nsi:
1245 label_decl statement_nsi
1247 $$ = complete_labeled_statement ($1, $2);
1248 pop_labeled_block ();
1249 POP_LABELED_BLOCK ();
1253 /* We concentrate here a bunch of error handling rules that we couldn't write
1254 earlier, because expression_statement catches a missing ';'. */
1255 expression_statement:
1256 statement_expression SC_TK
1258 /* We have a statement. Generate a WFL around it so
1260 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1261 /* We know we have a statement, so set the debug
1262 info to be eventually generate here. */
1263 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1267 if (ctxp->prevent_ese != lineno)
1268 yyerror ("Invalid expression statement");
1269 DRECOVER (expr_stmt);
1273 if (ctxp->prevent_ese != lineno)
1274 yyerror ("Invalid expression statement");
1275 DRECOVER (expr_stmt);
1279 if (ctxp->prevent_ese != lineno)
1280 yyerror ("Invalid expression statement");
1281 DRECOVER (expr_stmt);
1283 | this_or_super OP_TK error
1284 {yyerror ("')' expected"); RECOVER;}
1285 | this_or_super OP_TK CP_TK error
1287 yyerror ("Constructor invocation must be first "
1288 "thing in a constructor");
1291 | this_or_super OP_TK argument_list error
1292 {yyerror ("')' expected"); RECOVER;}
1293 | this_or_super OP_TK argument_list CP_TK error
1295 yyerror ("Constructor invocation must be first "
1296 "thing in a constructor");
1299 | name DOT_TK SUPER_TK error
1300 {yyerror ("'(' expected"); RECOVER;}
1301 | name DOT_TK SUPER_TK OP_TK error
1302 {yyerror ("')' expected"); RECOVER;}
1303 | name DOT_TK SUPER_TK OP_TK argument_list error
1304 {yyerror ("')' expected"); RECOVER;}
1305 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1306 {yyerror ("';' expected"); RECOVER;}
1307 | name DOT_TK SUPER_TK OP_TK CP_TK error
1308 {yyerror ("';' expected"); RECOVER;}
1311 statement_expression:
1313 | pre_increment_expression
1314 | pre_decrement_expression
1315 | post_increment_expression
1316 | post_decrement_expression
1318 | class_instance_creation_expression
1322 IF_TK OP_TK expression CP_TK statement
1323 { $$ = build_if_else_statement ($2.location, $3, $5, NULL_TREE); }
1325 {yyerror ("'(' expected"); RECOVER;}
1327 {yyerror ("Missing term"); RECOVER;}
1328 | IF_TK OP_TK expression error
1329 {yyerror ("')' expected"); RECOVER;}
1332 if_then_else_statement:
1333 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1334 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1337 if_then_else_statement_nsi:
1338 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1339 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1349 /* Make into "proper list" of COMPOUND_EXPRs.
1350 I.e. make the last statment also have its own
1352 maybe_absorb_scoping_blocks ();
1353 TREE_OPERAND ($1, 1) = exit_block ();
1354 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1359 SWITCH_TK OP_TK expression CP_TK
1361 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1362 EXPR_WFL_LINECOL ($$) = $2.location;
1365 {yyerror ("'(' expected"); RECOVER;}
1366 | SWITCH_TK OP_TK error
1367 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1368 | SWITCH_TK OP_TK expression CP_TK error
1369 {yyerror ("'{' expected"); RECOVER;}
1372 /* Default assignment is there to avoid type node on switch_block
1378 | OCB_TK switch_labels CCB_TK
1380 | OCB_TK switch_block_statement_groups CCB_TK
1382 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1386 switch_block_statement_groups:
1387 switch_block_statement_group
1388 | switch_block_statement_groups switch_block_statement_group
1391 switch_block_statement_group:
1392 switch_labels block_statements
1397 | switch_labels switch_label
1401 CASE_TK constant_expression REL_CL_TK
1403 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1404 EXPR_WFL_LINECOL (lab) = $1.location;
1405 java_method_add_stmt (current_function_decl, lab);
1407 | DEFAULT_TK REL_CL_TK
1409 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1410 EXPR_WFL_LINECOL (lab) = $1.location;
1411 java_method_add_stmt (current_function_decl, lab);
1414 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1415 | CASE_TK constant_expression error
1416 {yyerror ("':' expected"); RECOVER;}
1418 {yyerror ("':' expected"); RECOVER;}
1422 WHILE_TK OP_TK expression CP_TK
1424 tree body = build_loop_body ($2.location, $3, 0);
1425 $$ = build_new_loop (body);
1430 while_expression statement
1431 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1433 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1434 | WHILE_TK OP_TK error
1435 {yyerror ("Missing term and ')' expected"); RECOVER;}
1436 | WHILE_TK OP_TK expression error
1437 {yyerror ("')' expected"); RECOVER;}
1440 while_statement_nsi:
1441 while_expression statement_nsi
1442 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1448 tree body = build_loop_body (0, NULL_TREE, 1);
1449 $$ = build_new_loop (body);
1451 /* Need error handing here. FIXME */
1455 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1456 { $$ = complete_loop_body ($4.location, $5, $2, 1); }
1460 for_begin SC_TK expression SC_TK for_update CP_TK statement
1461 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1462 | for_begin SC_TK SC_TK for_update CP_TK statement
1464 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1465 /* We have not condition, so we get rid of the EXIT_EXPR */
1466 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1469 | for_begin SC_TK error
1470 {yyerror ("Invalid control expression"); RECOVER;}
1471 | for_begin SC_TK expression SC_TK error
1472 {yyerror ("Invalid update expression"); RECOVER;}
1473 | for_begin SC_TK SC_TK error
1474 {yyerror ("Invalid update expression"); RECOVER;}
1478 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1479 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1480 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1482 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1483 /* We have not condition, so we get rid of the EXIT_EXPR */
1484 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1492 /* This scope defined for local variable that may be
1493 defined within the scope of the for loop */
1497 {yyerror ("'(' expected"); DRECOVER(for_1);}
1498 | FOR_TK OP_TK error
1499 {yyerror ("Invalid init statement"); RECOVER;}
1505 /* We now declare the loop body. The loop is
1506 declared as a for loop. */
1507 tree body = build_loop_body (0, NULL_TREE, 0);
1508 $$ = build_new_loop (body);
1509 IS_FOR_LOOP_P ($$) = 1;
1510 /* The loop is added to the current block the for
1511 statement is defined within */
1512 java_method_add_stmt (current_function_decl, $$);
1515 for_init: /* Can be empty */
1516 { $$ = empty_stmt_node; }
1517 | statement_expression_list
1519 /* Init statement recorded within the previously
1520 defined block scope */
1521 $$ = java_method_add_stmt (current_function_decl, $1);
1523 | local_variable_declaration
1525 /* Local variable are recorded within the previously
1526 defined block scope */
1529 | statement_expression_list error
1530 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1533 for_update: /* Can be empty */
1534 {$$ = empty_stmt_node;}
1535 | statement_expression_list
1536 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1539 statement_expression_list:
1540 statement_expression
1541 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1542 | statement_expression_list C_TK statement_expression
1543 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1544 | statement_expression_list C_TK error
1545 {yyerror ("Missing term"); RECOVER;}
1550 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1551 | BREAK_TK identifier SC_TK
1552 { $$ = build_bc_statement ($1.location, 1, $2); }
1554 {yyerror ("Missing term"); RECOVER;}
1555 | BREAK_TK identifier error
1556 {yyerror ("';' expected"); RECOVER;}
1561 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1562 | CONTINUE_TK identifier SC_TK
1563 { $$ = build_bc_statement ($1.location, 0, $2); }
1565 {yyerror ("Missing term"); RECOVER;}
1566 | CONTINUE_TK identifier error
1567 {yyerror ("';' expected"); RECOVER;}
1572 { $$ = build_return ($1.location, NULL_TREE); }
1573 | RETURN_TK expression SC_TK
1574 { $$ = build_return ($1.location, $2); }
1576 {yyerror ("Missing term"); RECOVER;}
1577 | RETURN_TK expression error
1578 {yyerror ("';' expected"); RECOVER;}
1582 THROW_TK expression SC_TK
1584 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1585 EXPR_WFL_LINECOL ($$) = $1.location;
1588 {yyerror ("Missing term"); RECOVER;}
1589 | THROW_TK expression error
1590 {yyerror ("';' expected"); RECOVER;}
1593 synchronized_statement:
1594 synchronized OP_TK expression CP_TK block
1596 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1597 EXPR_WFL_LINECOL ($$) =
1598 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1600 | synchronized OP_TK expression CP_TK error
1601 {yyerror ("'{' expected"); RECOVER;}
1602 | synchronized error
1603 {yyerror ("'(' expected"); RECOVER;}
1604 | synchronized OP_TK error CP_TK
1605 {yyerror ("Missing term"); RECOVER;}
1606 | synchronized OP_TK error
1607 {yyerror ("Missing term"); RECOVER;}
1613 if ((1 << $1) != ACC_SYNCHRONIZED)
1614 fatal ("synchronized was '%d' - yyparse", (1 << $1));
1619 TRY_TK block catches
1620 { $$ = build_try_statement ($1.location, $2, $3); }
1621 | TRY_TK block finally
1622 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1623 | TRY_TK block catches finally
1624 { $$ = build_try_finally_statement ($1.location,
1625 build_try_statement ($1.location,
1629 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1634 | catches catch_clause
1636 TREE_CHAIN ($2) = $1;
1642 catch_clause_parameter block
1644 java_method_add_stmt (current_function_decl, $2);
1649 catch_clause_parameter:
1650 CATCH_TK OP_TK formal_parameter CP_TK
1652 /* We add a block to define a scope for
1653 formal_parameter (CCBP). The formal parameter is
1654 declared initialized by the appropriate function
1656 tree ccpb = enter_block ();
1657 tree init = build_assignment (ASSIGN_TK, $2.location,
1659 soft_exceptioninfo_call_node);
1660 declare_local_variables (0, TREE_VALUE ($3),
1661 build_tree_list (TREE_PURPOSE ($3),
1663 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1664 EXPR_WFL_LINECOL ($$) = $1.location;
1667 {yyerror ("'(' expected"); RECOVER;}
1668 | CATCH_TK OP_TK error
1669 {yyerror ("Missing term or ')' expected"); DRECOVER (2);}
1670 | CATCH_TK OP_TK error CP_TK /* That's for () */
1671 {yyerror ("')' expected"); DRECOVER (1);}
1678 {yyerror ("'{' expected"); RECOVER; }
1681 /* 19.12 Production from 15: Expressions */
1683 primary_no_new_array
1684 | array_creation_expression
1687 primary_no_new_array:
1690 { $$ = build_this ($1.location); }
1691 | OP_TK expression CP_TK
1693 | class_instance_creation_expression
1697 /* type DOT_TK CLASS_TK doens't work. So we split the rule
1698 'type' into its components. Missing is something for array,
1699 which will complete the reference_type part. FIXME */
1700 | name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1701 { $$ = parse_jdk1_1_error ("named class literals"); }
1702 | primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1703 { $$ = build_class_ref ($1); }
1704 | VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1705 { $$ = build_class_ref (void_type_node); }
1706 /* Added, JDK1.1 inner classes. Documentation is wrong
1707 refering to a 'ClassName' (class_name) rule that doesn't
1708 exist. Used name instead. */
1709 | name DOT_TK THIS_TK
1710 { $$ = parse_jdk1_1_error ("class literals"); }
1711 | OP_TK expression error
1712 {yyerror ("')' expected"); RECOVER;}
1714 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1715 | primitive_type DOT_TK error
1716 {yyerror ("'class' expected" ); RECOVER;}
1717 | VOID_TK DOT_TK error
1718 {yyerror ("'class' expected" ); RECOVER;}
1721 class_instance_creation_expression:
1722 NEW_TK class_type OP_TK argument_list CP_TK
1723 { $$ = build_new_invocation ($2, $4); }
1724 | NEW_TK class_type OP_TK CP_TK
1725 { $$ = build_new_invocation ($2, NULL_TREE); }
1726 /* Added, JDK1.1 inner classes but modified to use
1727 'class_type' instead of 'TypeName' (type_name) mentionned
1728 in the documentation but doesn't exist. */
1729 | NEW_TK class_type OP_TK argument_list CP_TK class_body
1730 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1731 | NEW_TK class_type OP_TK CP_TK class_body
1732 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1733 /* Added, JDK1.1 inner classes, modified to use name or
1734 primary instead of primary solely which couldn't work in
1736 | something_dot_new identifier OP_TK CP_TK
1737 | something_dot_new identifier OP_TK CP_TK class_body
1738 | something_dot_new identifier OP_TK argument_list CP_TK
1739 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1740 | NEW_TK error SC_TK
1741 {yyerror ("'(' expected"); DRECOVER(new_1);}
1742 | NEW_TK class_type error
1743 {yyerror ("'(' expected"); RECOVER;}
1744 | NEW_TK class_type OP_TK error
1745 {yyerror ("')' or term expected"); RECOVER;}
1746 | NEW_TK class_type OP_TK argument_list error
1747 {yyerror ("')' expected"); RECOVER;}
1748 | something_dot_new error
1749 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1750 | something_dot_new identifier error
1751 {yyerror ("'(' expected"); RECOVER;}
1754 something_dot_new: /* Added, not part of the specs. */
1756 | primary DOT_TK NEW_TK
1762 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
1763 ctxp->formal_parameter_number = 1;
1765 | argument_list C_TK expression
1767 ctxp->formal_parameter_number += 1;
1768 $$ = tree_cons (NULL_TREE, $3, $1);
1770 | argument_list C_TK error
1771 {yyerror ("Missing term"); RECOVER;}
1774 array_creation_expression:
1775 NEW_TK primitive_type dim_exprs
1776 { $$ = build_newarray_node ($2, $3, 0); }
1777 | NEW_TK class_or_interface_type dim_exprs
1778 { $$ = build_newarray_node ($2, $3, 0); }
1779 | NEW_TK primitive_type dim_exprs dims
1780 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
1781 | NEW_TK class_or_interface_type dim_exprs dims
1782 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
1783 /* Added, JDK1.1 anonymous array. Initial documentation rule
1785 | NEW_TK class_or_interface_type dims array_initializer
1786 { $$ = parse_jdk1_1_error ("anonymous array"); }
1787 | NEW_TK primitive_type dims array_initializer
1788 { $$ = parse_jdk1_1_error ("anonymous array"); }
1789 | NEW_TK error CSB_TK
1790 {yyerror ("'[' expected"); DRECOVER ("]");}
1791 | NEW_TK error OSB_TK
1792 {yyerror ("']' expected"); RECOVER;}
1797 { $$ = build_tree_list (NULL_TREE, $1); }
1798 | dim_exprs dim_expr
1799 { $$ = tree_cons (NULL_TREE, $2, $$); }
1803 OSB_TK expression CSB_TK
1805 EXPR_WFL_LINECOL ($2) = $1.location;
1808 | OSB_TK expression error
1809 {yyerror ("']' expected"); RECOVER;}
1812 yyerror ("Missing term");
1813 yyerror ("']' expected");
1822 /* If not initialized, allocate memory for the osb
1824 if (!ctxp->osb_limit)
1826 allocate = ctxp->osb_limit = 32;
1827 ctxp->osb_depth = -1;
1829 /* If capacity overflown, reallocate a bigger chuck */
1830 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
1831 allocate = ctxp->osb_limit << 1;
1835 allocate *= sizeof (int);
1836 if (ctxp->osb_number)
1837 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
1840 ctxp->osb_number = (int *)xmalloc (allocate);
1843 CURRENT_OSB (ctxp) = 1;
1845 | dims OSB_TK CSB_TK
1846 { CURRENT_OSB (ctxp)++; }
1848 { yyerror ("']' expected"); RECOVER;}
1852 primary DOT_TK identifier
1853 { $$ = make_qualified_primary ($1, $3, $2.location); }
1854 /* FIXME - REWRITE TO:
1855 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
1856 | SUPER_TK DOT_TK identifier
1859 build_wfl_node (super_identifier_node,
1860 input_filename, 0, 0);
1861 EXPR_WFL_LINECOL (super_wfl) = $1.location;
1862 $$ = make_qualified_name (super_wfl, $3, $2.location);
1865 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
1870 { $$ = build_method_invocation ($1, NULL_TREE); }
1871 | name OP_TK argument_list CP_TK
1872 { $$ = build_method_invocation ($1, $3); }
1873 | primary DOT_TK identifier OP_TK CP_TK
1875 if (TREE_CODE ($1) == THIS_EXPR)
1876 $$ = build_this_super_qualified_invocation
1877 (1, $3, NULL_TREE, 0, $2.location);
1880 tree invok = build_method_invocation ($3, NULL_TREE);
1881 $$ = make_qualified_primary ($1, invok, $2.location);
1884 | primary DOT_TK identifier OP_TK argument_list CP_TK
1886 if (TREE_CODE ($1) == THIS_EXPR)
1887 $$ = build_this_super_qualified_invocation
1888 (1, $3, $5, 0, $2.location);
1891 tree invok = build_method_invocation ($3, $5);
1892 $$ = make_qualified_primary ($1, invok, $2.location);
1895 | SUPER_TK DOT_TK identifier OP_TK CP_TK
1897 $$ = build_this_super_qualified_invocation
1898 (0, $3, NULL_TREE, $1.location, $2.location);
1900 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
1902 $$ = build_this_super_qualified_invocation
1903 (0, $3, $5, $1.location, $2.location);
1905 /* Screws up thing. I let it here until I'm convinced it can
1907 | primary DOT_TK error
1908 {yyerror ("'(' expected"); DRECOVER(bad);} */
1909 | SUPER_TK DOT_TK error CP_TK
1910 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1911 | SUPER_TK DOT_TK error DOT_TK
1912 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1916 name OSB_TK expression CSB_TK
1917 { $$ = build_array_ref ($2.location, $1, $3); }
1918 | primary_no_new_array OSB_TK expression CSB_TK
1919 { $$ = build_array_ref ($2.location, $1, $3); }
1922 yyerror ("Missing term and ']' expected");
1923 DRECOVER(array_access);
1925 | name OSB_TK expression error
1927 yyerror ("']' expected");
1928 DRECOVER(array_access);
1930 | primary_no_new_array OSB_TK error
1932 yyerror ("Missing term and ']' expected");
1933 DRECOVER(array_access);
1935 | primary_no_new_array OSB_TK expression error
1937 yyerror ("']' expected");
1938 DRECOVER(array_access);
1945 | post_increment_expression
1946 | post_decrement_expression
1949 post_increment_expression:
1950 postfix_expression INCR_TK
1951 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1954 post_decrement_expression:
1955 postfix_expression DECR_TK
1956 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1960 pre_increment_expression
1961 | pre_decrement_expression
1962 | PLUS_TK unary_expression
1963 {$$ = build_unaryop ($1.token, $1.location, $2); }
1964 | MINUS_TK unary_expression
1965 {$$ = build_unaryop ($1.token, $1.location, $2); }
1966 | unary_expression_not_plus_minus
1968 {yyerror ("Missing term"); RECOVER}
1970 {yyerror ("Missing term"); RECOVER}
1973 pre_increment_expression:
1974 INCR_TK unary_expression
1975 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1977 {yyerror ("Missing term"); RECOVER}
1980 pre_decrement_expression:
1981 DECR_TK unary_expression
1982 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1984 {yyerror ("Missing term"); RECOVER}
1987 unary_expression_not_plus_minus:
1989 | NOT_TK unary_expression
1990 {$$ = build_unaryop ($1.token, $1.location, $2); }
1991 | NEG_TK unary_expression
1992 {$$ = build_unaryop ($1.token, $1.location, $2); }
1995 {yyerror ("Missing term"); RECOVER}
1997 {yyerror ("Missing term"); RECOVER}
2000 cast_expression: /* Error handling here is potentially weak */
2001 OP_TK primitive_type dims CP_TK unary_expression
2004 while (CURRENT_OSB (ctxp)--)
2005 type = build_java_array_type (type, -1);
2007 $$ = build_cast ($1.location, type, $5);
2009 | OP_TK primitive_type CP_TK unary_expression
2010 { $$ = build_cast ($1.location, $2, $4); }
2011 | OP_TK expression CP_TK unary_expression_not_plus_minus
2012 { $$ = build_cast ($1.location, $2, $4); }
2013 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2016 while (CURRENT_OSB (ctxp)--)
2017 obstack_1grow (&temporary_obstack, '[');
2019 obstack_grow0 (&temporary_obstack,
2020 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2021 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2022 ptr = obstack_finish (&temporary_obstack);
2023 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2024 $$ = build_cast ($1.location, $2, $5);
2026 | OP_TK primitive_type OSB_TK error
2027 {yyerror ("']' expected, invalid type expression");}
2030 if (ctxp->prevent_ese != lineno)
2031 yyerror ("Invalid type expression"); RECOVER;
2034 | OP_TK primitive_type dims CP_TK error
2035 {yyerror ("Missing term"); RECOVER;}
2036 | OP_TK primitive_type CP_TK error
2037 {yyerror ("Missing term"); RECOVER;}
2038 | OP_TK name dims CP_TK error
2039 {yyerror ("Missing term"); RECOVER;}
2042 multiplicative_expression:
2044 | multiplicative_expression MULT_TK unary_expression
2046 $$ = build_binop (BINOP_LOOKUP ($2.token),
2047 $2.location, $1, $3);
2049 | multiplicative_expression DIV_TK unary_expression
2051 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2054 | multiplicative_expression REM_TK unary_expression
2056 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2059 | multiplicative_expression MULT_TK error
2060 {yyerror ("Missing term"); RECOVER;}
2061 | multiplicative_expression DIV_TK error
2062 {yyerror ("Missing term"); RECOVER;}
2063 | multiplicative_expression REM_TK error
2064 {yyerror ("Missing term"); RECOVER;}
2067 additive_expression:
2068 multiplicative_expression
2069 | additive_expression PLUS_TK multiplicative_expression
2071 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2074 | additive_expression MINUS_TK multiplicative_expression
2076 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2079 | additive_expression PLUS_TK error
2080 {yyerror ("Missing term"); RECOVER;}
2081 | additive_expression MINUS_TK error
2082 {yyerror ("Missing term"); RECOVER;}
2087 | shift_expression LS_TK additive_expression
2089 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2092 | shift_expression SRS_TK additive_expression
2094 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2097 | shift_expression ZRS_TK additive_expression
2099 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2102 | shift_expression LS_TK error
2103 {yyerror ("Missing term"); RECOVER;}
2104 | shift_expression SRS_TK error
2105 {yyerror ("Missing term"); RECOVER;}
2106 | shift_expression ZRS_TK error
2107 {yyerror ("Missing term"); RECOVER;}
2110 relational_expression:
2112 | relational_expression LT_TK shift_expression
2114 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2117 | relational_expression GT_TK shift_expression
2119 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2122 | relational_expression LTE_TK shift_expression
2124 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2127 | relational_expression GTE_TK shift_expression
2129 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2132 | relational_expression INSTANCEOF_TK reference_type
2133 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2134 | relational_expression LT_TK error
2135 {yyerror ("Missing term"); RECOVER;}
2136 | relational_expression GT_TK error
2137 {yyerror ("Missing term"); RECOVER;}
2138 | relational_expression LTE_TK error
2139 {yyerror ("Missing term"); RECOVER;}
2140 | relational_expression GTE_TK error
2141 {yyerror ("Missing term"); RECOVER;}
2142 | relational_expression INSTANCEOF_TK error
2143 {yyerror ("Invalid reference type"); RECOVER;}
2146 equality_expression:
2147 relational_expression
2148 | equality_expression EQ_TK relational_expression
2150 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2153 | equality_expression NEQ_TK relational_expression
2155 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2158 | equality_expression EQ_TK error
2159 {yyerror ("Missing term"); RECOVER;}
2160 | equality_expression NEQ_TK error
2161 {yyerror ("Missing term"); RECOVER;}
2166 | and_expression AND_TK equality_expression
2168 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2171 | and_expression AND_TK error
2172 {yyerror ("Missing term"); RECOVER;}
2175 exclusive_or_expression:
2177 | exclusive_or_expression XOR_TK and_expression
2179 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2182 | exclusive_or_expression XOR_TK error
2183 {yyerror ("Missing term"); RECOVER;}
2186 inclusive_or_expression:
2187 exclusive_or_expression
2188 | inclusive_or_expression OR_TK exclusive_or_expression
2190 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2193 | inclusive_or_expression OR_TK error
2194 {yyerror ("Missing term"); RECOVER;}
2197 conditional_and_expression:
2198 inclusive_or_expression
2199 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2201 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2204 | conditional_and_expression BOOL_AND_TK error
2205 {yyerror ("Missing term"); RECOVER;}
2208 conditional_or_expression:
2209 conditional_and_expression
2210 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2212 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2215 | conditional_or_expression BOOL_OR_TK error
2216 {yyerror ("Missing term"); RECOVER;}
2219 conditional_expression: /* Error handling here is weak */
2220 conditional_or_expression
2221 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2223 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2224 EXPR_WFL_LINECOL ($$) = $2.location;
2226 | conditional_or_expression REL_QM_TK REL_CL_TK error
2229 yyerror ("Missing term");
2232 | conditional_or_expression REL_QM_TK error
2233 {yyerror ("Missing term"); DRECOVER (2);}
2234 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2235 {yyerror ("Missing term"); DRECOVER (3);}
2238 assignment_expression:
2239 conditional_expression
2244 left_hand_side assignment_operator assignment_expression
2245 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2246 | left_hand_side assignment_operator error
2248 if (ctxp->prevent_ese != lineno)
2249 yyerror ("Missing term");
2260 assignment_operator:
2266 assignment_expression
2269 constant_expression:
2278 /* Flag for the error report routine to issue the error the first time
2279 it's called (overriding the default behavior which is to drop the
2280 first invocation and honor the second one, taking advantage of a
2282 static int force_error = 0;
2284 /* Create a new parser context and make it the current one. */
2287 java_push_parser_context ()
2289 struct parser_ctxt *new =
2290 (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2292 bzero (new, sizeof (struct parser_ctxt));
2297 ctxp->incomplete_class = ctxp->next->incomplete_class;
2298 ctxp->gclass_list = ctxp->next->gclass_list;
2302 /* If the first file of a file list was a class file, no context
2303 exists for a source file to be parsed. This boolean remembers that
2304 java_parser_context_save_global might have created a dummy one, so
2305 that java_parser_context_restore_global can pop it. */
2306 static int extra_ctxp_pushed_p = 0;
2309 java_parser_context_save_global ()
2313 java_push_parser_context ();
2314 extra_ctxp_pushed_p = 1;
2316 ctxp->finput = finput;
2317 ctxp->lineno = lineno;
2318 ctxp->current_class = current_class;
2319 ctxp->filename = input_filename;
2320 ctxp->current_function_decl = current_function_decl;
2324 java_parser_context_restore_global ()
2326 finput = ctxp->finput;
2327 lineno = ctxp->lineno;
2328 current_class = ctxp->current_class;
2329 input_filename = ctxp->filename;
2330 current_function_decl = ctxp->current_function_decl;
2331 if (!ctxp->next && extra_ctxp_pushed_p)
2333 java_pop_parser_context (0);
2334 extra_ctxp_pushed_p = 0;
2339 java_pop_parser_context (generate)
2343 struct parser_ctxt *toFree, *next;
2352 next->incomplete_class = ctxp->incomplete_class;
2353 next->gclass_list = ctxp->gclass_list;
2354 lineno = ctxp->lineno;
2355 finput = ctxp->finput;
2356 current_class = ctxp->current_class;
2359 /* Set the single import class file flag to 0 for the current list
2360 of imported things */
2361 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2362 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2364 /* And restore those of the previous context */
2365 if ((ctxp = next)) /* Assignment is really meant here */
2366 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2367 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2371 toFree->next = ctxp_for_generation;
2372 ctxp_for_generation = toFree;
2378 /* Reporting JDK1.1 features not implemented */
2381 parse_jdk1_1_error (msg)
2384 sorry (": `%s' JDK1.1(TM) feature", msg);
2386 return empty_stmt_node;
2389 static int do_warning = 0;
2396 static int prev_lineno;
2397 static char *prev_msg;
2400 char *remainder, *code_from_source;
2401 extern struct obstack temporary_obstack;
2403 if (!force_error && prev_lineno == lineno)
2406 /* Save current error location but report latter, when the context is
2408 if (ctxp->java_error_flag == 0)
2410 ctxp->java_error_flag = 1;
2412 /* Do something to use the previous line if we're reaching the
2413 end of the file... */
2414 #ifdef VERBOSE_SKELETON
2415 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2420 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2421 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2424 ctxp->java_error_flag = 0;
2426 java_warning_count++;
2430 if (elc.col == 0 && msg[1] == ';')
2432 elc.col = ctxp->p_line->char_col-1;
2433 elc.line = ctxp->p_line->lineno;
2436 save_lineno = lineno;
2437 prev_lineno = lineno = elc.line;
2440 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2441 obstack_grow0 (&temporary_obstack,
2442 code_from_source, strlen (code_from_source));
2443 remainder = obstack_finish (&temporary_obstack);
2445 warning ("%s.\n%s", msg, remainder);
2447 error ("%s.\n%s", msg, remainder);
2449 /* This allow us to cheaply avoid an extra 'Invalid expression
2450 statement' error report when errors have been already reported on
2451 the same line. This occurs when we report an error but don't have
2452 a synchronization point other than ';', which
2453 expression_statement is the only one to take care of. */
2454 ctxp->prevent_ese = lineno = save_lineno;
2458 issue_warning_error_from_context (cl, msg, ap)
2463 char *saved, *saved_input_filename;
2465 vsprintf (buffer, msg, ap);
2468 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2469 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2470 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
2472 /* We have a CL, that's a good reason for using it if it contains data */
2473 saved = ctxp->filename;
2474 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2475 ctxp->filename = EXPR_WFL_FILENAME (cl);
2476 saved_input_filename = input_filename;
2477 input_filename = ctxp->filename;
2479 java_error (buffer);
2480 ctxp->filename = saved;
2481 input_filename = saved_input_filename;
2485 /* Issue an error message at a current source line CL */
2488 parse_error_context VPROTO ((tree cl, const char *msg, ...))
2490 #ifndef ANSI_PROTOTYPES
2497 #ifndef ANSI_PROTOTYPES
2498 cl = va_arg (ap, tree);
2499 msg = va_arg (ap, const char *);
2501 issue_warning_error_from_context (cl, msg, ap);
2505 /* Issue a warning at a current source line CL */
2508 parse_warning_context VPROTO ((tree cl, const char *msg, ...))
2510 #ifndef ANSI_PROTOTYPES
2517 #ifndef ANSI_PROTOTYPES
2518 cl = va_arg (ap, tree);
2519 msg = va_arg (ap, const char *);
2522 force_error = do_warning = 1;
2523 issue_warning_error_from_context (cl, msg, ap);
2524 do_warning = force_error = 0;
2529 find_expr_with_wfl (node)
2537 switch (TREE_CODE (node))
2540 return find_expr_with_wfl (BLOCK_EXPR_BODY (node));
2543 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
2546 to_return = find_expr_with_wfl (TREE_OPERAND (node, 1));
2550 return find_expr_with_wfl (TREE_OPERAND (node, 0));
2552 case LABELED_BLOCK_EXPR:
2553 return find_expr_with_wfl (TREE_OPERAND (node, 1));
2555 code = TREE_CODE_CLASS (TREE_CODE (node));
2556 if (((code == '1') || (code == '2') || (code == 'e'))
2557 && EXPR_WFL_LINECOL (node))
2565 /* Issue a missing return statement error. Uses METHOD to figure the
2566 last line of the method the error occurs in. */
2569 missing_return_error (method)
2572 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
2573 parse_error_context (wfl_operator, "Missing return statement");
2576 /* Issue an unreachable statement error. From NODE, find the next
2577 statement to report appropriately. */
2579 unreachable_stmt_error (node)
2582 /* Browse node to find the next expression node that has a WFL. Use
2583 the location to report the error */
2584 if (TREE_CODE (node) == COMPOUND_EXPR)
2585 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
2587 node = find_expr_with_wfl (node);
2591 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
2592 parse_error_context (wfl_operator, "Unreachable statement");
2595 fatal ("Can't get valid statement - unreachable_stmt_error");
2599 java_report_errors ()
2601 if (java_error_count)
2602 fprintf (stderr, "%d error%s",
2603 java_error_count, (java_error_count == 1 ? "" : "s"));
2604 if (java_warning_count)
2605 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
2606 java_warning_count, (java_warning_count == 1 ? "" : "s"));
2607 if (java_error_count || java_warning_count)
2608 putc ('\n', stderr);
2609 return java_error_count;
2613 java_accstring_lookup (flags)
2616 static char buffer [80];
2617 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
2619 /* Access modifier looked-up first for easier report on forbidden
2621 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
2622 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
2623 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
2624 if (flags & ACC_STATIC) COPY_RETURN ("static");
2625 if (flags & ACC_FINAL) COPY_RETURN ("final");
2626 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
2627 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
2628 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
2629 if (flags & ACC_NATIVE) COPY_RETURN ("native");
2630 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
2631 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
2638 /* Issuing error messages upon redefinition of classes, interfaces or
2642 classitf_redefinition_error (context, id, decl, cl)
2646 parse_error_context (cl, "%s `%s' already defined in %s:%d",
2647 context, IDENTIFIER_POINTER (id),
2648 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
2649 /* Here we should point out where its redefined. It's a unicode. FIXME */
2653 variable_redefinition_error (context, name, type, line)
2654 tree context, name, type;
2659 /* Figure a proper name for type. We might haven't resolved it */
2660 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
2661 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
2663 type_name = lang_printable_name (type, 0);
2665 parse_error_context (context,
2666 "Variable `%s' is already defined in this method and "
2667 "was declared `%s %s' at line %d",
2668 IDENTIFIER_POINTER (name),
2669 type_name, IDENTIFIER_POINTER (name), line);
2673 build_array_from_name (type, type_wfl, name, ret_name)
2674 tree type, type_wfl, name, *ret_name;
2679 /* Eventually get more dims */
2680 string = IDENTIFIER_POINTER (name);
2681 while (string [more_dims] == '[')
2684 /* If we have, then craft a new type for this variable */
2687 name = get_identifier (&more_dims [string]);
2689 /* If we have a pointer, use its type */
2690 if (TREE_CODE (type) == POINTER_TYPE)
2691 type = TREE_TYPE (type);
2693 /* Building the first dimension of a primitive type uses this
2695 if (JPRIMITIVE_TYPE_P (type))
2697 type = build_java_array_type (type, -1);
2698 CLASS_LOADED_P (type) = 1;
2701 /* Otherwise, if we have a WFL for this type, use it (the type
2702 is already an array on an unresolved type, and we just keep
2703 on adding dimensions) */
2707 /* Add all the dimensions */
2709 type = build_unresolved_array_type (type);
2711 /* The type may have been incomplete in the first place */
2713 type = obtain_incomplete_type (type);
2720 /* Build something that the type identifier resolver will identify as
2721 being an array to an unresolved type. TYPE_WFL is a WFL on a
2725 build_unresolved_array_type (type_or_wfl)
2730 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
2731 just create a array type */
2732 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
2734 tree type = build_java_array_type (type_or_wfl, -1);
2735 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
2739 obstack_1grow (&temporary_obstack, '[');
2740 obstack_grow0 (&temporary_obstack,
2741 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
2742 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
2743 ptr = obstack_finish (&temporary_obstack);
2744 return build_expr_wfl (get_identifier (ptr),
2745 EXPR_WFL_FILENAME (type_or_wfl),
2746 EXPR_WFL_LINENO (type_or_wfl),
2747 EXPR_WFL_COLNO (type_or_wfl));
2750 /* Check modifiers. If one doesn't fit, retrieve it in its declaration line
2751 and point it out. */
2754 check_modifiers (message, value, mask)
2759 /* Should point out the one that don't fit. ASCII/unicode,
2760 going backward. FIXME */
2763 int i, remainder = value & ~mask;
2764 for (i = 0; i <= 10; i++)
2765 if ((1 << i) & remainder)
2766 parse_error_context (ctxp->modifier_ctx [i], message,
2767 java_accstring_lookup (1 << i));
2772 parser_add_interface (class_decl, interface_decl, wfl)
2773 tree class_decl, interface_decl, wfl;
2775 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
2776 parse_error_context (wfl, "Interface `%s' repeated",
2777 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
2780 /* Bulk of common class/interface checks. Return 1 if an error was
2781 encountered. TAG is 0 for a class, 1 for an interface. */
2784 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
2785 int is_interface, flags;
2786 tree raw_name, qualified_name, decl, cl;
2791 fprintf (stderr, " %s %s", (is_interface ? "interface" : "class"),
2792 IDENTIFIER_POINTER (qualified_name));
2794 /* Scope of an interface/class type name:
2795 - Can't be imported by a single type import
2796 - Can't already exists in the package */
2797 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
2798 && (node = find_name_in_single_imports (raw_name)))
2801 (cl, "%s name `%s' clashes with imported type `%s'",
2802 (is_interface ? "Interface" : "Class"),
2803 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
2806 if (decl && CLASS_COMPLETE_P (decl))
2808 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
2809 qualified_name, decl, cl);
2813 /* If public, file name should match class/interface name */
2814 if (flags & ACC_PUBLIC)
2818 /* Contains OS dependent assumption on path separator. FIXME */
2819 for (f = &input_filename [strlen (input_filename)];
2820 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
2823 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
2825 if (strncmp (IDENTIFIER_POINTER (raw_name),
2826 f , IDENTIFIER_LENGTH (raw_name)) ||
2827 f [IDENTIFIER_LENGTH (raw_name)] != '.')
2828 parse_error_context (cl, "Public %s `%s' must be defined in a file "
2830 (is_interface ? "interface" : "class"),
2831 IDENTIFIER_POINTER (qualified_name),
2832 IDENTIFIER_POINTER (raw_name));
2835 check_modifiers ((is_interface ?
2836 "Illegal modifier `%s' for interface declaration" :
2837 "Illegal modifier `%s' for class declaration"), flags,
2838 (is_interface ? INTERFACE_MODIFIERS : CLASS_MODIFIERS));
2842 /* If DECL is NULL, create and push a new DECL, record the current
2843 line CL and do other maintenance things. */
2846 maybe_create_class_interface_decl (decl, qualified_name, cl)
2847 tree decl, qualified_name, cl;
2850 decl = push_class (make_class (), qualified_name);
2852 /* Take care of the file and line business */
2853 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
2854 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
2855 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
2856 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
2857 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
2859 ctxp->current_parsed_class = decl;
2861 /* Link the declaration to the already seen ones */
2862 TREE_CHAIN (decl) = ctxp->class_list;
2863 ctxp->class_list = decl;
2865 /* Create a new nodes in the global lists */
2866 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
2867 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
2869 /* Install a new dependency list element */
2870 create_jdep_list (ctxp);
2872 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
2873 IDENTIFIER_POINTER (qualified_name)));
2878 add_superinterfaces (decl, interface_list)
2879 tree decl, interface_list;
2882 /* Superinterface(s): if present and defined, parser_check_super_interface ()
2883 takes care of ensuring that:
2884 - This is an accessible interface type,
2885 - Circularity detection.
2886 parser_add_interface is then called. If present but not defined,
2887 the check operation is delayed until the super interface gets
2889 for (node = interface_list; node; node = TREE_CHAIN (node))
2891 tree current = TREE_PURPOSE (node);
2892 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
2893 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
2895 if (!parser_check_super_interface (idecl, decl, current))
2896 parser_add_interface (decl, idecl, current);
2899 register_incomplete_type (JDEP_INTERFACE,
2900 current, decl, NULL_TREE);
2904 /* Create an interface in pass1 and return its decl. Return the
2905 interface's decl in pass 2. */
2908 create_interface (flags, id, super)
2912 tree raw_name = EXPR_WFL_NODE (id);
2913 tree q_name = parser_qualified_classname (id);
2914 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
2916 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
2918 /* Basic checks: scope, redefinition, modifiers */
2919 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
2922 /* Interface modifiers check
2923 - public/abstract allowed (already done at that point)
2924 - abstract is obsolete (comes first, it's a warning, or should be)
2925 - Can't use twice the same (checked in the modifier rule) */
2926 if ((flags & ACC_ABSTRACT) && flag_redundant)
2927 parse_warning_context
2928 (MODIFIER_WFL (ABSTRACT_TK),
2929 "Redundant use of `abstract' modifier. Interface `%s' is implicitely "
2930 "abstract", IDENTIFIER_POINTER (raw_name));
2932 /* Create a new decl if DECL is NULL, otherwise fix it */
2933 decl = maybe_create_class_interface_decl (decl, q_name, id);
2935 /* Set super info and mark the class a complete */
2936 set_super_info (ACC_ABSTRACT | ACC_INTERFACE | flags, TREE_TYPE (decl),
2937 object_type_node, ctxp->interface_number);
2938 ctxp->interface_number = 0;
2939 CLASS_COMPLETE_P (decl) = 1;
2940 add_superinterfaces (decl, super);
2945 /* Create an class in pass1 and return its decl. Return class
2946 interface's decl in pass 2. */
2949 create_class (flags, id, super, interfaces)
2951 tree id, super, interfaces;
2953 tree raw_name = EXPR_WFL_NODE (id);
2954 tree class_id, decl;
2955 tree super_decl = NULL, super_decl_type;
2957 class_id = parser_qualified_classname (id);
2958 decl = IDENTIFIER_CLASS_VALUE (class_id);
2959 ctxp->current_parsed_class_un = EXPR_WFL_NODE (id);
2960 EXPR_WFL_NODE (id) = class_id;
2962 /* Basic check: scope, redefinition, modifiers */
2963 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
2966 /* Class modifier check:
2967 - Allowed modifier (already done at that point)
2968 - abstract AND final forbidden
2969 - Public classes defined in the correct file */
2970 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
2971 parse_error_context (id, "Class `%s' can't be declared both abstract "
2972 "and final", IDENTIFIER_POINTER (raw_name));
2974 /* Create a new decl if DECL is NULL, otherwise fix it */
2975 decl = maybe_create_class_interface_decl (decl, class_id, id);
2977 /* If SUPER exists, use it, otherwise use Object */
2980 /* Can't extend java.lang.Object */
2981 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
2983 parse_error_context (id, "Can't extend `java.lang.Object'");
2987 /* The class is known and exists if there is a decl. Otherwise,
2988 postpone the operation and do it later. */
2989 super_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (super));
2992 parser_check_super (super_decl, decl, id);
2993 super_decl_type = TREE_TYPE (super_decl);
2997 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
2999 else if (TREE_TYPE (decl) != object_type_node)
3000 super_decl_type = object_type_node;
3001 /* We're defining java.lang.Object */
3003 super_decl_type = NULL_TREE;
3005 /* Set super info and mark the class a complete */
3006 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3007 ctxp->interface_number);
3008 ctxp->interface_number = 0;
3009 CLASS_COMPLETE_P (decl) = 1;
3010 add_superinterfaces (decl, interfaces);
3012 /* Eventually sets the @deprecated tag flag */
3013 CHECK_DEPRECATED (decl);
3018 /* Can't use lookup_field () since we don't want to load the class and
3019 can't set the CLASS_LOADED_P flag */
3022 find_field (class, name)
3027 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3029 if (DECL_NAME (decl) == name)
3035 /* Wrap around lookup_field that doesn't potentially upset the value
3039 lookup_field_wrapper (class, name)
3044 java_parser_context_save_global ();
3045 decl = lookup_field (&type, name);
3046 java_parser_context_restore_global ();
3050 /* Find duplicate field within the same class declarations and report
3051 the error. Returns 1 if a duplicated field was found, 0
3055 duplicate_declaration_error_p (new_field_name, new_type, cl)
3056 tree new_field_name, new_type, cl;
3058 /* This might be modified to work with method decl as well */
3059 tree decl = find_field (TREE_TYPE (ctxp->current_parsed_class),
3063 char *t1 = strdup (purify_type_name
3064 ((TREE_CODE (new_type) == POINTER_TYPE
3065 && TREE_TYPE (new_type) == NULL_TREE) ?
3066 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
3067 lang_printable_name (new_type, 1)));
3068 /* The type may not have been completed by the time we report
3070 char *t2 = strdup (purify_type_name
3071 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
3072 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
3073 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
3074 lang_printable_name (TREE_TYPE (decl), 1)));
3076 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
3077 t1, IDENTIFIER_POINTER (new_field_name),
3078 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
3079 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3087 /* Field registration routine. If TYPE doesn't exist, field
3088 declarations are linked to the undefined TYPE dependency list, to
3089 be later resolved in java_complete_class () */
3092 register_fields (flags, type, variable_list)
3094 tree type, variable_list;
3096 tree current, saved_type;
3097 tree class_type = TREE_TYPE (ctxp->current_parsed_class);
3098 int saved_lineno = lineno;
3100 tree wfl = NULL_TREE;
3102 /* If we're adding fields to interfaces, those fields are public,
3104 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
3106 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
3108 "%s", "interface field(s)");
3109 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
3111 "%s", "interface field(s)");
3112 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
3113 flags, ACC_FINAL, "%s", "interface field(s)");
3114 check_modifiers ("Illegal interface member modifier `%s'", flags,
3115 INTERFACE_FIELD_MODIFIERS);
3116 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
3119 /* Obtain a suitable type for resolution, if necessary */
3120 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
3122 /* If TYPE is fully resolved and we don't have a reference, make one */
3123 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3125 for (current = variable_list, saved_type = type; current;
3126 current = TREE_CHAIN (current), type = saved_type)
3130 tree cl = TREE_PURPOSE (current);
3131 tree init = TREE_VALUE (current);
3132 tree current_name = EXPR_WFL_NODE (cl);
3134 /* Process NAME, as it may specify extra dimension(s) for it */
3135 type = build_array_from_name (type, wfl, current_name, ¤t_name);
3137 /* Type adjustment. We may have just readjusted TYPE because
3138 the variable specified more dimensions. Make sure we have
3139 a reference if we can and don't have one already. Also
3140 change the name if we have an init. */
3141 if (type != saved_type)
3143 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3145 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
3148 real_type = GET_REAL_TYPE (type);
3149 /* Check for redeclarations */
3150 if (duplicate_declaration_error_p (current_name, real_type, cl))
3153 /* Set lineno to the line the field was found and create a
3154 declaration for it. Eventually sets the @deprecated tag flag. */
3155 lineno = EXPR_WFL_LINENO (cl);
3156 field_decl = add_field (class_type, current_name, real_type, flags);
3157 CHECK_DEPRECATED (field_decl);
3159 /* Check if we must chain. */
3161 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
3163 /* Default value of a static field is 0 and it is considered
3165 if (flags & ACC_STATIC)
3166 INITIALIZED_P (field_decl) = 1;
3168 /* If we have an initialization value tied to the field */
3171 /* The field is declared static */
3172 if (flags & ACC_STATIC)
3174 /* We include the field and its initialization part into
3175 a list used to generate <clinit>. After <clinit> is
3176 walked, field initializations will be processed and
3177 fields initialized with known constants will be taken
3178 out of <clinit> and have their DECL_INITIAL set
3180 TREE_CHAIN (init) = ctxp->static_initialized;
3181 ctxp->static_initialized = init;
3182 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
3183 if (TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
3184 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
3186 /* A non-static field declared with an immediate initialization is
3187 to be initialized in <init>, if any. This field is remembered
3188 to be processed at the time of the generation of <init>. */
3191 TREE_CHAIN (init) = ctxp->non_static_initialized;
3192 ctxp->non_static_initialized = init;
3194 INITIALIZED_P (field_decl) = 1;
3195 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
3198 lineno = saved_lineno;
3201 /* Generate the method $finit$ that initializes fields initialized
3202 upon declaration. */
3205 maybe_generate_finit ()
3207 tree mdecl, current;
3209 if (!ctxp->non_static_initialized || java_error_count)
3212 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3213 ACC_PRIVATE, void_type_node,
3214 finit_identifier_node, end_params_node);
3215 start_artificial_method_body (mdecl);
3217 ctxp->non_static_initialized = nreverse (ctxp->non_static_initialized);
3218 for (current = ctxp->non_static_initialized; current;
3219 current = TREE_CHAIN (current))
3220 java_method_add_stmt (mdecl,
3221 build_debugable_stmt (EXPR_WFL_LINECOL (current),
3224 end_artificial_method_body (mdecl);
3225 CLASS_HAS_FINIT_P (TREE_TYPE (ctxp->current_parsed_class)) = 1;
3226 ctxp->non_static_initialized = NULL_TREE;
3229 /* Check whether it is necessary to generate a <clinit> for the class
3233 maybe_generate_clinit ()
3237 if (!ctxp->static_initialized || java_error_count)
3240 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3241 ACC_STATIC, void_type_node,
3242 clinit_identifier_node, end_params_node);
3243 start_artificial_method_body (mdecl);
3245 /* Keep initialization in order to enforce 8.5 */
3246 ctxp->static_initialized = nreverse (ctxp->static_initialized);
3248 /* We process the list of assignment we produced as the result of
3249 the declaration of initialized static field and add them as
3250 statement to the <clinit> method. */
3251 for (c = ctxp->static_initialized; c; c = TREE_CHAIN (c))
3253 /* We build the assignment expression that will initialize the
3254 field to its value. There are strict rules on static
3255 initializers (8.5). FIXME */
3256 java_method_add_stmt (mdecl,
3257 build_debugable_stmt (EXPR_WFL_LINECOL (c), c));
3260 end_artificial_method_body (mdecl);
3261 ctxp->static_initialized = NULL_TREE;
3264 /* Shared accros method_declarator and method_header to remember the
3265 patch stage that was reached during the declaration of the method.
3266 A method DECL is built differently is there is no patch
3267 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
3268 pending on the currently defined method. */
3270 static int patch_stage;
3272 /* Check the method declaration and add the method to its current
3273 class. If the argument list is known to contain incomplete types,
3274 the method is partially added and the registration will be resume
3275 once the method arguments resolved. If TYPE is NULL, we're dealing
3276 with a constructor. */
3279 method_header (flags, type, mdecl, throws)
3281 tree type, mdecl, throws;
3283 tree meth = TREE_VALUE (mdecl);
3284 tree id = TREE_PURPOSE (mdecl);
3285 tree this_class = TREE_TYPE (ctxp->current_parsed_class);
3286 tree type_wfl = NULL_TREE;
3287 tree meth_name = NULL_TREE, current, orig_arg;
3289 int constructor_ok = 0, must_chain;
3291 check_modifiers_consistency (flags);
3293 /* There are some forbidden modifiers for an abstract method and its
3294 class must be abstract as well. */
3295 if (type && (flags & ACC_ABSTRACT))
3297 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
3298 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
3299 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
3300 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
3301 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
3302 if (!CLASS_ABSTRACT (TYPE_NAME (this_class)))
3304 (id, "Class `%s' must be declared abstract to define abstract "
3306 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
3307 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3309 /* Things to be checked when declaring a constructor */
3312 int ec = java_error_count;
3313 /* 8.6: Constructor declarations: we might be trying to define a
3314 method without specifying a return type. */
3315 if (EXPR_WFL_NODE (id) != ctxp->current_parsed_class_un)
3317 (id, "Invalid method declaration, return type required");
3318 /* 8.6.3: Constructor modifiers */
3321 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
3322 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
3323 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
3324 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
3325 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
3327 /* If we found error here, we don't consider it's OK to tread
3328 the method definition as a constructor, for the rest of this
3330 if (ec == java_error_count)
3334 /* Method declared within the scope of an interface are implicitly
3335 abstract and public. Conflicts with other erroneously provided
3336 modifiers are check right after. */
3338 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
3340 /* If FLAGS isn't set because of a modifier, turn the
3341 corresponding modifier WFL to NULL so we issue a warning on
3342 the obsolete use of the modifier */
3343 if (!(flags & ACC_PUBLIC))
3344 MODIFIER_WFL (PUBLIC_TK) = NULL;
3345 if (!(flags & ACC_ABSTRACT))
3346 MODIFIER_WFL (ABSTRACT_TK) = NULL;
3347 flags |= ACC_PUBLIC;
3348 flags |= ACC_ABSTRACT;
3351 /* Modifiers context reset moved up, so abstract method declaration
3352 modifiers can be later checked. */
3354 /* Set constructor returned type to void and method name to <init>,
3355 unless we found an error identifier the constructor (in which
3356 case we retain the original name) */
3359 type = void_type_node;
3361 meth_name = init_identifier_node;
3364 meth_name = EXPR_WFL_NODE (id);
3366 /* Do the returned type resolution and registration if necessary */
3367 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3370 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
3371 EXPR_WFL_NODE (id) = meth_name;
3372 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3376 patch_stage = JDEP_METHOD_RETURN;
3377 register_incomplete_type (patch_stage, type_wfl, id, type);
3378 TREE_TYPE (meth) = GET_REAL_TYPE (type);
3381 TREE_TYPE (meth) = type;
3383 saved_lineno = lineno;
3384 /* When defining an abstract or interface method, the curly
3385 bracket at level 1 doesn't exist because there is no function
3387 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
3388 EXPR_WFL_LINENO (id));
3390 /* Remember the original argument list */
3391 orig_arg = TYPE_ARG_TYPES (meth);
3393 if (patch_stage) /* includes ret type and/or all args */
3396 meth = add_method_1 (this_class, flags, meth_name, meth);
3397 /* Patch for the return type */
3398 if (patch_stage == JDEP_METHOD_RETURN)
3400 jdep = CLASSD_LAST (ctxp->classd_list);
3401 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
3403 /* This is the stop JDEP. METH allows the function's signature
3405 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
3408 meth = add_method (this_class, flags, meth_name,
3409 build_java_signature (meth));
3411 /* Fix the method argument list so we have the argument name
3413 fix_method_argument_names (orig_arg, meth);
3415 /* Register the parameter number and re-install the current line
3417 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
3418 lineno = saved_lineno;
3420 /* Register exception specified by the `throws' keyword for
3421 resolution and set the method decl appropriate field to the list.
3422 Note: the grammar ensures that what we get here are class
3426 throws = nreverse (throws);
3427 for (current = throws; current; current = TREE_CHAIN (current))
3429 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
3430 NULL_TREE, NULL_TREE);
3431 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
3432 &TREE_VALUE (current);
3434 DECL_FUNCTION_THROWS (meth) = throws;
3437 /* We set the DECL_NAME to ID so we can track the location where
3438 the function was declared. This allow us to report
3439 redefinition error accurately. When method are verified,
3440 DECL_NAME is reinstalled properly (using the content of the
3441 WFL node ID) (see check_method_redefinition). We don't do that
3442 when Object is being defined. Constructor <init> names will be
3443 reinstalled the same way. */
3444 if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node)
3445 DECL_NAME (meth) = id;
3447 /* Set the flag if we correctly processed a constructor */
3449 DECL_CONSTRUCTOR_P (meth) = 1;
3451 /* Eventually set the @deprecated tag flag */
3452 CHECK_DEPRECATED (meth);
3458 fix_method_argument_names (orig_arg, meth)
3459 tree orig_arg, meth;
3461 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
3462 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
3464 TREE_PURPOSE (arg) = this_identifier_node;
3465 arg = TREE_CHAIN (arg);
3467 while (orig_arg != end_params_node)
3469 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
3470 orig_arg = TREE_CHAIN (orig_arg);
3471 arg = TREE_CHAIN (arg);
3475 /* Complete the method declaration with METHOD_BODY. */
3478 complete_method_declaration (method_body)
3481 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
3482 maybe_absorb_scoping_blocks ();
3483 /* Exit function's body */
3485 /* Merge last line of the function with first line, directly in the
3486 function decl. It will be used to emit correct debug info. */
3487 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
3490 /* Build a an error message for constructor circularity errors. */
3493 constructor_circularity_msg (from, to)
3496 static char string [4096];
3497 char *t = strdup (lang_printable_name (from, 0));
3498 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
3503 /* Verify a circular call to METH. Return 1 if an error is found, 0
3507 verify_constructor_circularity (meth, current)
3510 static tree list = NULL_TREE;
3512 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3514 if (TREE_VALUE (c) == meth)
3520 list = nreverse (list);
3521 for (liste = list; liste; liste = TREE_CHAIN (liste))
3524 (TREE_PURPOSE (TREE_PURPOSE (liste)),
3525 constructor_circularity_msg
3526 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
3530 t = strdup (lang_printable_name (meth, 0));
3531 parse_error_context (TREE_PURPOSE (c),
3532 "%s: recursive invocation of constructor `%s'",
3533 constructor_circularity_msg (current, meth), t);
3539 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3541 list = tree_cons (c, current, list);
3542 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
3544 list = TREE_CHAIN (list);
3549 /* Check modifiers that can be declared but exclusively */
3552 check_modifiers_consistency (flags)
3556 tree cl = NULL_TREE;
3558 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, 0, acc_count, cl);
3559 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, 1, acc_count, cl);
3560 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, 2, acc_count, cl);
3563 (cl, "Inconsistent member declaration. At most one of `public', "
3564 "`private', or `protected' may be specified");
3567 /* Check the methode header METH for abstract specifics features */
3570 check_abstract_method_header (meth)
3573 int flags = get_access_flags_from_decl (meth);
3574 /* DECL_NAME might still be a WFL node */
3575 tree name = GET_METHOD_NAME (meth);
3577 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (ABSTRACT_TK), flags,
3578 ACC_ABSTRACT, "abstract method `%s'",
3579 IDENTIFIER_POINTER (name));
3580 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), flags,
3581 ACC_PUBLIC, "abstract method `%s'",
3582 IDENTIFIER_POINTER (name));
3584 check_modifiers ("Illegal modifier `%s' for interface method",
3585 flags, INTERFACE_METHOD_MODIFIERS);
3588 /* Create a FUNCTION_TYPE node and start augmenting it with the
3589 declared function arguments. Arguments type that can't be resolved
3590 are left as they are, but the returned node is marked as containing
3591 incomplete types. */
3594 method_declarator (id, list)
3597 tree arg_types = NULL_TREE, current, node;
3598 tree meth = make_node (FUNCTION_TYPE);
3601 patch_stage = JDEP_NO_PATCH;
3603 for (current = list; current; current = TREE_CHAIN (current))
3606 tree wfl_name = TREE_PURPOSE (current);
3607 tree type = TREE_VALUE (current);
3608 tree name = EXPR_WFL_NODE (wfl_name);
3609 tree already, arg_node;
3610 tree type_wfl = NULL_TREE;
3613 /* Obtain a suitable type for resolution, if necessary */
3614 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3616 /* Process NAME, as it may specify extra dimension(s) for it */
3617 type = build_array_from_name (type, type_wfl, name, &name);
3618 EXPR_WFL_NODE (wfl_name) = name;
3620 real_type = GET_REAL_TYPE (type);
3621 if (TREE_CODE (real_type) == RECORD_TYPE)
3623 real_type = promote_type (real_type);
3624 if (TREE_CODE (type) == TREE_LIST)
3625 TREE_PURPOSE (type) = real_type;
3628 /* Check redefinition */
3629 for (already = arg_types; already; already = TREE_CHAIN (already))
3630 if (TREE_PURPOSE (already) == name)
3633 (wfl_name, "Variable `%s' is used more than once in the "
3634 "argument list of method `%s'", IDENTIFIER_POINTER (name),
3635 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3639 /* If we've an incomplete argument type, we know there is a location
3640 to patch when the type get resolved, later. */
3644 patch_stage = JDEP_METHOD;
3645 type = register_incomplete_type (patch_stage,
3646 type_wfl, wfl_name, type);
3647 jdep = CLASSD_LAST (ctxp->classd_list);
3648 JDEP_MISC (jdep) = id;
3651 /* The argument node: a name and a (possibly) incomplete type */
3652 arg_node = build_tree_list (name, real_type);
3654 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
3655 TREE_CHAIN (arg_node) = arg_types;
3656 arg_types = arg_node;
3658 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
3659 node = build_tree_list (id, meth);
3664 unresolved_type_p (wfl, returned)
3669 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
3671 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
3673 *returned = (decl ? TREE_TYPE (decl) : NULL_TREE);
3681 /* From NAME, build a qualified identifier node using the
3682 qualification from the current package definition. */
3685 parser_qualified_classname (name)
3689 return merge_qualified_name (ctxp->package, EXPR_WFL_NODE (name));
3691 return EXPR_WFL_NODE (name);
3694 /* Called once the type a interface extends is resolved. Returns 0 if
3695 everything is OK. */
3698 parser_check_super_interface (super_decl, this_decl, this_wfl)
3699 tree super_decl, this_decl, this_wfl;
3701 tree super_type = TREE_TYPE (super_decl);
3703 /* Has to be an interface */
3704 if (!CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (super_decl))))
3707 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
3708 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
3709 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
3710 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
3711 "interface" : "class"),
3712 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
3716 /* Check scope: same package OK, other package: OK if public */
3717 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
3720 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
3721 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3722 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3726 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
3727 0 if everthing is OK. */
3730 parser_check_super (super_decl, this_decl, wfl)
3731 tree super_decl, this_decl, wfl;
3733 tree super_type = TREE_TYPE (super_decl);
3735 /* SUPER should be a CLASS (neither an array nor an interface) */
3736 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
3739 (wfl, "Class `%s' can't subclass %s `%s'",
3740 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3741 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
3742 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3746 if (CLASS_FINAL (TYPE_NAME (super_type)))
3748 parse_error_context (wfl, "Can't subclass final classes: %s",
3749 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3753 /* Check scope: same package OK, other package: OK if public */
3754 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
3757 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
3758 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3759 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3763 /* Create a new dependency list and link it (in a LIFO manner) to the
3764 CTXP list of type dependency list. */
3767 create_jdep_list (ctxp)
3768 struct parser_ctxt *ctxp;
3770 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
3771 new->first = new->last = NULL;
3772 new->next = ctxp->classd_list;
3773 ctxp->classd_list = new;
3777 reverse_jdep_list (ctxp)
3778 struct parser_ctxt *ctxp;
3780 register jdeplist *prev = NULL, *current, *next;
3781 for (current = ctxp->classd_list; current; current = next)
3783 next = current->next;
3784 current->next = prev;
3790 /* Create a fake pointer based on the ID stored in
3791 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
3792 registered again. */
3795 obtain_incomplete_type (type_name)
3800 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
3801 name = EXPR_WFL_NODE (type_name);
3802 else if (INCOMPLETE_TYPE_P (type_name))
3803 name = TYPE_NAME (type_name);
3805 fatal ("invalid type name - obtain_incomplete_type");
3807 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
3808 if (TYPE_NAME (ptr) == name)
3813 push_obstacks (&permanent_obstack, &permanent_obstack);
3814 BUILD_PTR_FROM_NAME (ptr, name);
3817 TREE_CHAIN (ptr) = ctxp->incomplete_class;
3818 ctxp->incomplete_class = ptr;
3824 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
3825 non NULL instead of computing a new fake type based on WFL. The new
3826 dependency is inserted in the current type dependency list, in FIFO
3830 register_incomplete_type (kind, wfl, decl, ptr)
3832 tree wfl, decl, ptr;
3834 jdep *new = (jdep *)xmalloc (sizeof (jdep));
3836 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
3837 ptr = obtain_incomplete_type (wfl);
3839 JDEP_KIND (new) = kind;
3840 JDEP_DECL (new) = decl;
3841 JDEP_SOLV (new) = ptr;
3842 JDEP_WFL (new) = wfl;
3843 JDEP_CHAIN (new) = NULL;
3844 JDEP_MISC (new) = NULL_TREE;
3845 JDEP_GET_PATCH (new) = (tree *)NULL;
3847 JDEP_INSERT (ctxp->classd_list, new);
3853 java_check_circular_reference ()
3856 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
3858 tree type = TREE_TYPE (current);
3859 if (CLASS_INTERFACE (TYPE_NAME (type)))
3861 /* Check all interfaces this class extends */
3862 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
3867 n = TREE_VEC_LENGTH (basetype_vec);
3868 for (i = 0; i < n; i++)
3870 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
3871 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
3872 && interface_of_p (type, BINFO_TYPE (vec_elt)))
3873 parse_error_context (lookup_cl (current),
3874 "Cyclic interface inheritance");
3878 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
3879 parse_error_context (lookup_cl (current),
3880 "Cyclic class inheritance");
3884 /* safe_layout_class just makes sure that we can load a class without
3885 disrupting the current_class, input_file, lineno, etc, information
3886 about the class processed currently. */
3889 safe_layout_class (class)
3892 tree save_current_class = current_class;
3893 char *save_input_filename = input_filename;
3894 int save_lineno = lineno;
3896 push_obstacks (&permanent_obstack, &permanent_obstack);
3898 layout_class (class);
3901 current_class = save_current_class;
3902 input_filename = save_input_filename;
3903 lineno = save_lineno;
3904 CLASS_LOADED_P (class) = 1;
3908 jdep_resolve_class (dep)
3913 if (JDEP_RESOLVED_P (dep))
3914 decl = JDEP_RESOLVED_DECL (dep);
3917 decl = resolve_class (JDEP_TO_RESOLVE (dep),
3918 JDEP_DECL (dep), JDEP_WFL (dep));
3919 JDEP_RESOLVED (dep, decl);
3923 complete_class_report_errors (dep);
3928 /* Complete unsatisfied class declaration and their dependencies */
3931 java_complete_class ()
3938 push_obstacks (&permanent_obstack, &permanent_obstack);
3940 /* Process imports and reverse the import on demand list */
3942 if (ctxp->import_demand_list)
3943 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
3945 /* Rever things so we have the right order */
3946 ctxp->class_list = nreverse (ctxp->class_list);
3947 ctxp->classd_list = reverse_jdep_list (ctxp);
3949 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
3951 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
3954 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
3957 if (!(decl = jdep_resolve_class (dep)))
3960 /* Now it's time to patch */
3961 switch (JDEP_KIND (dep))
3964 /* Simply patch super */
3965 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
3967 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
3968 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
3973 /* We do part of the job done in add_field */
3974 tree field_decl = JDEP_DECL (dep);
3975 tree field_type = TREE_TYPE (decl);
3976 push_obstacks (&permanent_obstack, &permanent_obstack);
3977 if (TREE_CODE (field_type) == RECORD_TYPE)
3978 field_type = promote_type (field_type);
3980 TREE_TYPE (field_decl) = field_type;
3981 DECL_ALIGN (field_decl) = 0;
3982 layout_decl (field_decl, 0);
3983 SOURCE_FRONTEND_DEBUG
3984 (("Completed field/var decl `%s' with `%s'",
3985 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
3986 IDENTIFIER_POINTER (DECL_NAME (decl))));
3989 case JDEP_METHOD: /* We start patching a method */
3990 case JDEP_METHOD_RETURN:
3996 type = TREE_TYPE(decl);
3997 if (TREE_CODE (type) == RECORD_TYPE)
3998 type = promote_type (type);
3999 JDEP_APPLY_PATCH (dep, type);
4000 SOURCE_FRONTEND_DEBUG
4001 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
4002 "Completing fct `%s' with ret type `%s'":
4003 "Completing arg `%s' with type `%s'"),
4004 IDENTIFIER_POINTER (EXPR_WFL_NODE
4005 (JDEP_DECL_WFL (dep))),
4006 IDENTIFIER_POINTER (DECL_NAME (decl))));
4010 dep = JDEP_CHAIN (dep);
4011 if (JDEP_KIND (dep) == JDEP_METHOD_END)
4014 decl = jdep_resolve_class (dep);
4018 tree mdecl = JDEP_DECL (dep), signature;
4019 push_obstacks (&permanent_obstack, &permanent_obstack);
4020 /* Recompute and reset the signature */
4021 signature = build_java_signature (TREE_TYPE (mdecl));
4022 set_java_signature (TREE_TYPE (mdecl), signature);
4029 case JDEP_INTERFACE:
4030 if (parser_check_super_interface (decl, JDEP_DECL (dep),
4033 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
4038 type = TREE_TYPE(decl);
4039 if (TREE_CODE (type) == RECORD_TYPE)
4040 type = promote_type (type);
4041 JDEP_APPLY_PATCH (dep, type);
4045 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4046 SOURCE_FRONTEND_DEBUG
4047 (("Completing a random type dependency on a '%s' node",
4048 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
4051 case JDEP_EXCEPTION:
4052 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4053 SOURCE_FRONTEND_DEBUG
4054 (("Completing `%s' `throws' argument node",
4055 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
4059 fatal ("Can't handle patch code %d - java_complete_class",
4068 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
4072 resolve_class (class_type, decl, cl)
4073 tree class_type, decl, cl;
4075 char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
4077 tree resolved_type = TREE_TYPE (class_type);
4078 tree resolved_type_decl;
4080 if (resolved_type != NULL_TREE)
4082 tree resolved_type_decl = TYPE_NAME (resolved_type);
4083 if (resolved_type_decl == NULL_TREE
4084 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
4086 resolved_type_decl = build_decl (TYPE_DECL,
4087 TYPE_NAME (class_type),
4090 return resolved_type_decl;
4093 /* 1- Check to see if we have an array. If true, find what we really
4095 while (name[0] == '[')
4098 TYPE_NAME (class_type) = get_identifier (name);
4100 /* 2- Resolve the bare type */
4101 if (!(resolved_type_decl = do_resolve_class (class_type, decl, cl)))
4103 resolved_type = TREE_TYPE (resolved_type_decl);
4105 /* 3- If we have and array, reconstruct the array down to its nesting */
4108 while (base != name)
4110 if (TREE_CODE (resolved_type) == RECORD_TYPE)
4111 resolved_type = promote_type (resolved_type);
4112 resolved_type = build_java_array_type (resolved_type, -1);
4113 CLASS_LOADED_P (resolved_type) = 1;
4116 /* Build a fake decl for this, since this is what is expected to
4118 resolved_type_decl =
4119 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
4120 /* Figure how those two things are important for error report. FIXME */
4121 DECL_SOURCE_LINE (resolved_type_decl) = 0;
4122 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
4123 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
4125 TREE_TYPE (class_type) = resolved_type;
4126 return resolved_type_decl;
4129 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
4130 are used to report error messages. */
4133 do_resolve_class (class_type, decl, cl)
4138 tree new_class_decl;
4139 tree original_name = NULL_TREE;
4141 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
4142 its is changed by find_in_imports{_on_demand} */
4144 /* 1- Check for the type in single imports */
4145 if (find_in_imports (class_type))
4148 /* 2- And check for the type in the current compilation unit. If it fails,
4149 try with a name qualified with the package name if appropriate. */
4150 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4152 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4153 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4154 load_class (TYPE_NAME (class_type), 0);
4155 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4158 original_name = TYPE_NAME (class_type);
4159 if (!QUALIFIED_P (TYPE_NAME (class_type)) && ctxp->package)
4160 TYPE_NAME (class_type) = merge_qualified_name (ctxp->package,
4161 TYPE_NAME (class_type));
4163 if (!(new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4164 load_class (TYPE_NAME (class_type), 0);
4165 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4167 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4168 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4169 load_class (TYPE_NAME (class_type), 0);
4170 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4173 new_name = TYPE_NAME (class_type);
4174 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_name)) != NULL_TREE)
4176 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4177 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4178 load_class (new_name, 0);
4179 return IDENTIFIER_CLASS_VALUE (new_name);
4183 tree class = read_class (new_name);
4184 if (class != NULL_TREE)
4186 tree decl = IDENTIFIER_CLASS_VALUE (new_name);
4187 if (decl == NULL_TREE)
4188 decl = push_class (class, new_name);
4193 TYPE_NAME (class_type) = original_name;
4195 /* 3- Check an other compilation unit that bears the name of type */
4196 load_class (TYPE_NAME (class_type), 0);
4197 if (check_pkg_class_access (TYPE_NAME (class_type),
4198 (cl ? cl : lookup_cl (decl))))
4201 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4202 return new_class_decl;
4204 /* 4- Check the import on demands. Don't allow bar.baz to be
4205 imported from foo.* */
4206 if (!QUALIFIED_P (TYPE_NAME (class_type)))
4207 if (find_in_imports_on_demand (class_type))
4210 /* 5- Last call for a resolution */
4211 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4214 /* Resolve NAME and lay it out (if not done and if not the current
4215 parsed class). Return a decl node. This function is meant to be
4216 called when type resolution is necessary during the walk pass. */
4219 resolve_and_layout (something, cl)
4225 /* Don't do that on the current class */
4226 if (something == current_class)
4227 return TYPE_NAME (current_class);
4229 /* Don't do anything for void and other primitive types */
4230 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4233 /* Pointer types can be reall pointer types or fake pointers. When
4234 finding a real pointer, recheck for primitive types */
4235 if (TREE_CODE (something) == POINTER_TYPE)
4237 if (TREE_TYPE (something))
4239 something = TREE_TYPE (something);
4240 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4244 something = TYPE_NAME (something);
4247 /* Don't do anything for arrays of primitive types */
4248 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
4249 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
4252 /* If something is not and IDENTIFIER_NODE, it can be a a TYPE_DECL
4254 if (TREE_CODE (something) != IDENTIFIER_NODE)
4255 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
4256 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
4258 if (!(decl = resolve_no_layout (something, cl)))
4261 /* Resolve and layout if necessary */
4262 layout_class_methods (TREE_TYPE (decl));
4263 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl)))
4264 CHECK_METHODS (decl);
4265 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
4266 safe_layout_class (TREE_TYPE (decl));
4271 /* Resolve a class, returns its decl but doesn't perform any
4272 layout. The current parsing context is saved and restored */
4275 resolve_no_layout (name, cl)
4279 BUILD_PTR_FROM_NAME (ptr, name);
4280 java_parser_context_save_global ();
4281 decl = resolve_class (ptr, NULL_TREE, cl);
4282 java_parser_context_restore_global ();
4287 /* Called when reporting errors. Skip leader '[' in a complex array
4288 type description that failed to be resolved. */
4291 purify_type_name (name)
4294 while (*name && *name == '[')
4299 /* The type CURRENT refers to can't be found. We print error messages. */
4302 complete_class_report_errors (dep)
4307 if (!JDEP_WFL (dep))
4310 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
4311 switch (JDEP_KIND (dep))
4315 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
4316 purify_type_name (name),
4317 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4321 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
4322 purify_type_name (name),
4323 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4325 case JDEP_METHOD: /* Covers arguments */
4327 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4328 "argument `%s' of method `%s'",
4329 purify_type_name (name),
4330 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
4331 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
4333 case JDEP_METHOD_RETURN: /* Covers return type */
4335 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4336 "return type of method `%s'",
4337 purify_type_name (name),
4338 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
4340 case JDEP_INTERFACE:
4342 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
4343 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
4344 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
4345 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4349 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4350 "local variable `%s'",
4351 purify_type_name (IDENTIFIER_POINTER
4352 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
4353 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4355 case JDEP_EXCEPTION: /* As specified by `throws' */
4357 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
4358 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
4361 /* Fix for -Wall. Just break doing nothing. The error will be
4367 /* Check uninitialized final. */
4374 /* Return a static string containing the DECL prototype string. If
4375 DECL is a constructor, use the class name instead of the form
4379 get_printable_method_name (decl)
4385 if (DECL_CONSTRUCTOR_P (decl))
4387 name = DECL_NAME (decl);
4388 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
4391 to_return = lang_printable_name (decl, 0);
4392 if (DECL_CONSTRUCTOR_P (decl))
4393 DECL_NAME (decl) = name;
4398 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
4399 nevertheless needs to be verfied, 1 otherwise. */
4402 reset_method_name (method)
4405 if (DECL_NAME (method) != clinit_identifier_node
4406 && DECL_NAME (method) != finit_identifier_node)
4408 /* NAME is just the plain name when Object is being defined */
4409 if (DECL_CONTEXT (method) != object_type_node)
4410 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
4411 init_identifier_node : GET_METHOD_NAME (method));
4418 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
4421 java_get_real_method_name (method_decl)
4424 tree method_name = DECL_NAME (method_decl);
4425 if (DECL_CONSTRUCTOR_P (method_decl))
4426 return init_identifier_node;
4428 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
4429 and still can be a constructor. FIXME */
4431 /* Don't confuse method only bearing the name of their class as
4433 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
4435 && ctxp->current_parsed_class_un == EXPR_WFL_NODE (method_name)
4436 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
4437 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
4438 return init_identifier_node;
4440 return EXPR_WFL_NODE (method_name);
4443 /* Track method being redefined inside the same class. As a side
4444 effect, set DECL_NAME to an IDENTIFIER (prior entering this
4445 function it's a FWL, so we can track errors more accurately */
4448 check_method_redefinition (class, method)
4452 tree cl = DECL_NAME (method);
4453 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
4454 /* decl name of artificial <clinit> and $finit$ doesn't need to be
4455 fixed and checked */
4457 /* Reset the method name before running the check. If it returns 1,
4458 the method doesn't need to be verified with respect to method
4459 redeclaration and we return 0 */
4460 if (reset_method_name (method))
4463 name = DECL_NAME (method);
4464 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
4466 if (redef == method)
4468 if (DECL_NAME (redef) == name
4469 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
4472 (cl, "Duplicate %s declaration `%s'",
4473 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
4474 get_printable_method_name (redef));
4481 /* Check all the methods of CLASS. Methods are first completed then
4482 checked according to regular method existance rules.
4483 If no constructor were encountered, then build its declaration. */
4486 java_check_regular_methods (class_decl)
4489 int saw_constructor = 0;
4491 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
4492 tree super_class = CLASSTYPE_SUPER (class);
4493 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
4496 /* It is not necessary to check methods defined in java.lang.Object */
4497 if (class == object_type_node)
4500 if (!TYPE_NVIRTUALS (class))
4501 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4503 /* Should take interfaces into account. FIXME */
4504 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
4507 tree method_wfl = DECL_NAME (method);
4510 /* If we previously found something and its name was saved,
4512 if (found && saved_found_wfl)
4514 DECL_NAME (found) = saved_found_wfl;
4515 saved_found_wfl = NULL_TREE;
4518 /* Check for redefinitions */
4519 if (check_method_redefinition (class, method))
4522 /* If we see one constructor a mark so we don't generate the
4523 default one. Also skip other verifications: constructors
4524 can't be inherited hence hiden or overriden */
4525 if (DECL_CONSTRUCTOR_P (method))
4527 saw_constructor = 1;
4531 /* We verify things thrown by the method. They must inherits from
4532 java.lang.Throwable */
4533 for (mthrows = DECL_FUNCTION_THROWS (method);
4534 mthrows; mthrows = TREE_CHAIN (mthrows))
4536 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
4538 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be "
4539 "a subclass of class `java.lang.Throwable'",
4541 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
4544 sig = build_java_argument_signature (TREE_TYPE (method));
4545 found = lookup_argument_method (super_class, DECL_NAME (method), sig);
4547 /* Nothing overrides or it's a private method. */
4550 if (METHOD_PRIVATE (found))
4556 /* If found wasn't verified, it's DECL_NAME won't be set properly.
4557 We set it temporarily for the sake of the error report. */
4558 saved_found_wfl = DECL_NAME (found);
4559 reset_method_name (found);
4561 /* Can't override a method with the same name and different return
4563 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
4565 char *t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)),
4569 "Method `%s' was defined with return type `%s' in class `%s'",
4570 lang_printable_name (found, 0), t,
4572 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4576 /* Can't override final. Can't override static. */
4577 if (METHOD_FINAL (found) || METHOD_STATIC (found))
4579 /* Static *can* override static */
4580 if (METHOD_STATIC (found) && METHOD_STATIC (method))
4584 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
4585 (METHOD_FINAL (found) ? "Final" : "Static"),
4586 lang_printable_name (found, 0),
4587 (METHOD_FINAL (found) ? "final" : "static"),
4589 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4592 /* Static method can't override instance method. */
4593 if (METHOD_STATIC (method))
4597 "Instance methods can't be overriden by a static method. Method "
4598 "`%s' is an instance method in class `%s'",
4599 lang_printable_name (found, 0),
4601 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4605 aflags = get_access_flags_from_decl (found);
4606 /* - Overriding/hiding public must be public
4607 - Overriding/hiding protected must be protected or public
4608 - If the overriden or hidden method has default (package)
4609 access, then the overriding or hiding method must not be
4610 private; otherwise, a compile-time error occurs */
4611 if ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
4612 || (METHOD_PROTECTED (found)
4613 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
4614 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
4615 && METHOD_PRIVATE (method)))
4619 "Methods can't be overridden to be more private. Method `%s' is "
4620 "not %s in class `%s'", lang_printable_name (method, 0),
4621 (METHOD_PUBLIC (method) ? "public" :
4622 (METHOD_PRIVATE (method) ? "private" : "protected")),
4623 IDENTIFIER_POINTER (DECL_NAME
4624 (TYPE_NAME (DECL_CONTEXT (found)))));
4628 /* Overriding methods must have compatible `throws' clauses on checked
4629 exceptions, if any */
4630 check_throws_clauses (method, method_wfl, found);
4632 /* If the method has default access in an other package, then
4633 issue a warning that the current method doesn't override the
4634 one that was found elsewhere. Do not issue this warning when
4635 the match was found in java.lang.Object. */
4636 if (DECL_CONTEXT (found) != object_type_node
4637 && (!aflags || (aflags > ACC_PROTECTED))
4638 && !class_in_current_package (DECL_CONTEXT (found))
4639 && flag_not_overriding)
4640 parse_warning_context
4641 (method_wfl, "Method `%s' in class `%s' does not "
4642 "override the corresponding method in class `%s', which is "
4643 "private to a different package",
4644 lang_printable_name (found, 0),
4645 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
4646 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4648 /* Inheriting multiple methods with the same signature. FIXME */
4651 /* Don't forget eventual pending found and saved_found_wfl. Take
4652 into account that we might have exited because we saw an
4653 aritifical method as the last entry. */
4655 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
4656 DECL_NAME (found) = saved_found_wfl;
4658 if (!TYPE_NVIRTUALS (class))
4659 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4661 if (!saw_constructor)
4663 /* No constructor seen, we craft one, at line 0. Since this
4664 operation takes place after we laid methods out
4665 (layout_class_methods), we prepare the its DECL
4670 /* If the class is declared PUBLIC, the default constructor is
4671 PUBLIC otherwise it has default access implied by no access
4673 flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
4675 decl = create_artificial_method (class, flags, void_type_node,
4676 init_identifier_node, end_params_node);
4677 DECL_CONSTRUCTOR_P (decl) = 1;
4678 layout_class_method (TREE_TYPE (class_decl), NULL_TREE, decl, NULL_TREE);
4682 /* Return a non zero value if the `throws' clause of METHOD (if any)
4683 is incompatible with the `throws' clause of FOUND (if any). */
4686 check_throws_clauses (method, method_wfl, found)
4687 tree method, method_wfl, found;
4689 tree mthrows, fthrows;
4691 /* Can't check these things with class loaded from bytecode. FIXME */
4692 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
4695 for (mthrows = DECL_FUNCTION_THROWS (method);
4696 mthrows; mthrows = TREE_CHAIN (mthrows))
4698 /* We don't verify unchecked expressions */
4699 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
4701 /* Checked expression must be compatible */
4702 for (fthrows = DECL_FUNCTION_THROWS (found);
4703 fthrows; fthrows = TREE_CHAIN (fthrows))
4704 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
4709 (method_wfl, "Invalid checked exception class `%s' in "
4710 "`throws' clause. The exception must be a subclass of an "
4711 "exception thrown by `%s' from class `%s'",
4712 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
4713 lang_printable_name (found, 0),
4715 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4720 /* Check abstract method of interface INTERFACE */
4723 java_check_abstract_methods (interface_decl)
4724 tree interface_decl;
4727 tree method, basetype_vec, found;
4728 tree interface = TREE_TYPE (interface_decl);
4730 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
4732 tree method_wfl = DECL_NAME (method);
4734 /* 2- Check for double definition inside the defining interface */
4735 if (check_method_redefinition (interface, method))
4738 /* 3- Overriding is OK as far as we preserve the return type and
4739 the thrown exceptions (FIXME) */
4740 found = lookup_java_interface_method2 (interface, method);
4744 tree saved_found_wfl = DECL_NAME (found);
4745 reset_method_name (found);
4746 t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
4749 "Method `%s' was defined with return type `%s' in class `%s'",
4750 lang_printable_name (found, 0), t,
4752 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4756 DECL_NAME (found) = saved_found_wfl;
4760 /* 4- Inherited methods can't differ by their returned types */
4761 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
4763 n = TREE_VEC_LENGTH (basetype_vec);
4764 for (i = 0; i < n; i++)
4766 tree sub_interface_method, sub_interface;
4767 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4770 sub_interface = BINFO_TYPE (vec_elt);
4771 for (sub_interface_method = TYPE_METHODS (sub_interface);
4772 sub_interface_method;
4773 sub_interface_method = TREE_CHAIN (sub_interface_method))
4775 found = lookup_java_interface_method2 (interface,
4776 sub_interface_method);
4777 if (found && (found != sub_interface_method))
4779 tree saved_found_wfl = DECL_NAME (found);
4780 reset_method_name (found);
4782 (lookup_cl (sub_interface_method),
4783 "Interface `%s' inherits method `%s' from interface `%s'. "
4784 "This method is redefined with a different return type in "
4786 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
4787 lang_printable_name (found, 0),
4789 (DECL_NAME (TYPE_NAME
4790 (DECL_CONTEXT (sub_interface_method)))),
4792 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4793 DECL_NAME (found) = saved_found_wfl;
4799 /* Lookup methods in interfaces using their name and partial
4800 signature. Return a matching method only if their types differ. */
4803 lookup_java_interface_method2 (class, method_decl)
4804 tree class, method_decl;
4807 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
4812 n = TREE_VEC_LENGTH (basetype_vec);
4813 for (i = 0; i < n; i++)
4815 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
4816 if ((BINFO_TYPE (vec_elt) != object_type_node)
4818 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
4821 for (i = 0; i < n; i++)
4823 to_return = lookup_java_interface_method2
4824 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
4832 /* Lookup method using their name and partial signature. Return a
4833 matching method only if their types differ. */
4836 lookup_java_method2 (clas, method_decl, do_interface)
4837 tree clas, method_decl;
4840 tree method, method_signature, method_name, method_type, name;
4842 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
4843 name = DECL_NAME (method_decl);
4844 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4845 EXPR_WFL_NODE (name) : name);
4846 method_type = TREE_TYPE (TREE_TYPE (method_decl));
4848 while (clas != NULL_TREE)
4850 for (method = TYPE_METHODS (clas);
4851 method != NULL_TREE; method = TREE_CHAIN (method))
4853 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
4854 tree name = DECL_NAME (method);
4855 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4856 EXPR_WFL_NODE (name) : name) == method_name
4857 && method_sig == method_signature
4858 && TREE_TYPE (TREE_TYPE (method)) != method_type)
4861 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
4866 /* Return the line that matches DECL line number. Used during error
4873 static tree cl = NULL_TREE;
4878 if (cl == NULL_TREE)
4879 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
4881 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
4882 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
4887 /* Look for a simple name in the single-type import list */
4890 find_name_in_single_imports (name)
4895 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
4896 if (TREE_VALUE (node) == name)
4897 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
4902 /* Process all single-type import. */
4910 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4912 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
4914 /* Don't load twice something already defined. */
4915 if (IDENTIFIER_CLASS_VALUE (to_be_found))
4917 QUALIFIED_P (to_be_found) = 1;
4918 load_class (to_be_found, 0);
4920 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
4921 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
4923 parse_error_context (TREE_PURPOSE (import),
4924 "Class or interface `%s' not found in import",
4925 IDENTIFIER_POINTER (to_be_found));
4934 /* Possibly find a class imported by a single-type import statement. Return
4935 1 if an error occured, 0 otherwise. */
4938 find_in_imports (class_type)
4943 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4944 if (TREE_VALUE (import) == TYPE_NAME (class_type))
4946 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
4947 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
4953 note_possible_classname (name, len)
4958 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
4960 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
4964 node = ident_subst (name, len, "", '/', '.', "");
4965 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
4966 QUALIFIED_P (node) = 1; /* As soon as we turn / into . */
4970 /* Read a import directory, gathering potential match for further type
4971 references. Indifferently reads a filesystem or a ZIP archive
4975 read_import_dir (wfl)
4978 tree package_id = EXPR_WFL_NODE (wfl);
4979 char *package_name = IDENTIFIER_POINTER (package_id);
4980 int package_length = IDENTIFIER_LENGTH (package_id);
4982 JCF *saved_jcf = current_jcf;
4987 struct buffer filename[1];
4990 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
4992 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
4994 BUFFER_INIT (filename);
4995 buffer_grow (filename, package_length + 100);
4997 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
4999 char *entry_name = jcf_path_name (entry);
5000 int entry_length = strlen (entry_name);
5001 if (jcf_path_is_zipfile (entry))
5004 buffer_grow (filename, entry_length);
5005 memcpy (filename->data, entry_name, entry_length - 1);
5006 filename->data[entry_length-1] = '\0';
5007 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
5009 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
5012 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
5013 BUFFER_RESET (filename);
5014 for (k = 0; k < package_length; k++)
5016 char ch = package_name[k];
5017 *filename->ptr++ = ch == '.' ? '/' : ch;
5019 *filename->ptr++ = '/';
5021 for (; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
5023 char *current_entry = ZIPDIR_FILENAME (zipd);
5024 int current_entry_len = zipd->filename_length;
5026 if (strncmp (filename->data, current_entry,
5027 BUFFER_LENGTH (filename)) != 0)
5029 found += note_possible_classname (current_entry,
5036 BUFFER_RESET (filename);
5037 buffer_grow (filename, entry_length + package_length + 4);
5038 strcpy (filename->data, entry_name);
5039 filename->ptr = filename->data + entry_length;
5040 for (k = 0; k < package_length; k++)
5042 char ch = package_name[k];
5043 *filename->ptr++ = ch == '.' ? '/' : ch;
5045 *filename->ptr = '\0';
5047 dirp = opendir (filename->data);
5050 *filename->ptr++ = '/';
5055 struct dirent *direntp = readdir (dirp);
5058 d_name = direntp->d_name;
5059 len = strlen (direntp->d_name);
5060 buffer_grow (filename, len+1);
5061 strcpy (filename->ptr, d_name);
5062 found += note_possible_classname (filename->data + entry_length,
5063 package_length+len+1);
5070 free (filename->data);
5072 /* Here we should have a unified way of retrieving an entry, to be
5076 static int first = 1;
5080 sprintf (buffer, "Can't find default package `%s'. Check "
5081 "the CLASSPATH environment variable and the access to the "
5082 "archives.", package_name);
5088 parse_error_context (wfl, "Package `%s' not found in import",
5090 current_jcf = saved_jcf;
5093 current_jcf = saved_jcf;
5096 /* Possibly find a type in the import on demands specified
5097 types. Returns 1 if an error occured, 0 otherwise. Run throught the
5098 entire list, to detected potential double definitions. */
5101 find_in_imports_on_demand (class_type)
5104 tree node, import, node_to_use;
5108 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
5111 obstack_grow (&temporary_obstack,
5112 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
5113 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5114 obstack_1grow (&temporary_obstack, '.');
5115 obstack_grow0 (&temporary_obstack,
5116 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5117 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
5118 id_name = obstack_finish (&temporary_obstack);
5120 node = maybe_get_identifier (id_name);
5121 if (node && IS_A_CLASSFILE_NAME (node))
5125 cl = TREE_PURPOSE (import);
5133 (import, "Type `%s' also potentially defined in package `%s'",
5134 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5135 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5142 /* Setup lineno so that it refers to the line of the import (in
5143 case we parse a class file and encounter errors */
5145 int saved_lineno = lineno;
5146 lineno = EXPR_WFL_LINENO (cl);
5147 TYPE_NAME (class_type) = node_to_use;
5148 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
5149 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5150 /* If there is no DECL set for the class or if the class isn't
5151 loaded and not seen in source yet, the load */
5152 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
5153 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
5154 load_class (node_to_use, 0);
5155 lineno = saved_lineno;
5156 return check_pkg_class_access (TYPE_NAME (class_type), cl);
5159 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
5163 resolve_package (pkg, next)
5166 tree type_name = NULL_TREE;
5167 char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5169 /* The trick is to determine when the package name stops and were
5170 the name of something contained in the package starts. Then we
5171 return a fully qualified name of what we want to get. */
5173 /* Do a quick search on well known package names */
5174 if (!strncmp (name, "java.lang.reflect", 17))
5177 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
5178 type_name = lookup_package_type (name, 17);
5180 else if (!strncmp (name, "java.lang", 9))
5182 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
5183 type_name = lookup_package_type (name, 9);
5186 return NULL_TREE; /* FIXME, search all imported packages. */
5192 lookup_package_type (name, from)
5197 char *sub = &name[from+1];
5198 while (*sub != '.' && *sub)
5200 strncpy (subname, name, sub-name);
5201 subname [sub-name] = '\0';
5202 return get_identifier (subname);
5205 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
5206 access violations were found, 1 otherwise. */
5209 check_pkg_class_access (class_name, cl)
5215 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
5218 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
5221 if (!CLASS_PUBLIC (TYPE_NAME (type)))
5223 /* Access to a private class within the same package is
5226 breakdown_qualified (&l, &r, class_name);
5227 if (l == ctxp->package)
5231 (cl, "Can't access %s `%s'. Only public classes and interfaces in "
5232 "other packages can be accessed",
5233 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
5234 IDENTIFIER_POINTER (class_name));
5240 /* Local variable declaration. */
5243 declare_local_variables (modifier, type, vlist)
5248 tree decl, current, saved_type;
5249 tree type_wfl = NULL_TREE;
5252 /* Push a new block if statement were seen between the last time we
5253 pushed a block and now. Keep a cound of block to close */
5254 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)))
5256 tree body = DECL_FUNCTION_BODY (current_function_decl);
5257 tree b = enter_block ();
5258 BLOCK_EXPR_ORIGIN(b) = body;
5264 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
5265 if (modifier == ACC_FINAL)
5267 if (flag_static_local_jdk1_1)
5268 parse_warning_context (ctxp->modifier_ctx [i],
5269 "Unsupported JDK1.1 `final' local variable "
5270 "(treated as non final)");
5275 (ctxp->modifier_ctx [i],
5276 "Only `final' is allowed as a local variables modifier");
5281 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
5282 hold the TYPE value if a new incomplete has to be created (as
5283 opposed to being found already existing and reused). */
5284 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5286 /* If TYPE is fully resolved and we don't have a reference, make one */
5287 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5289 /* Go through all the declared variables */
5290 for (current = vlist, saved_type = type; current;
5291 current = TREE_CHAIN (current), type = saved_type)
5293 tree other, real_type;
5294 tree wfl = TREE_PURPOSE (current);
5295 tree name = EXPR_WFL_NODE (wfl);
5296 tree init = TREE_VALUE (current);
5298 /* Process NAME, as it may specify extra dimension(s) for it */
5299 type = build_array_from_name (type, type_wfl, name, &name);
5301 /* Variable redefinition check */
5302 if ((other = lookup_name_in_blocks (name)))
5304 variable_redefinition_error (wfl, name, TREE_TYPE (other),
5305 DECL_SOURCE_LINE (other));
5309 /* Type adjustment. We may have just readjusted TYPE because
5310 the variable specified more dimensions. Make sure we have
5311 a reference if we can and don't have one already. */
5312 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5314 real_type = GET_REAL_TYPE (type);
5315 /* Never layout this decl. This will be done when its scope
5317 decl = build_decl (VAR_DECL, name, real_type);
5318 BLOCK_CHAIN_DECL (decl);
5320 /* Don't try to use an INIT statement when an error was found */
5321 if (init && java_error_count)
5324 /* Add the initialization function to the current function's code */
5327 /* Name might have been readjusted */
5328 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
5329 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
5330 java_method_add_stmt (current_function_decl,
5331 build_debugable_stmt (EXPR_WFL_LINECOL (init),
5335 /* Setup dependency the type of the decl */
5339 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
5340 dep = CLASSD_LAST (ctxp->classd_list);
5341 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
5344 SOURCE_FRONTEND_DEBUG (("Defined locals"));
5347 /* Called during parsing. Build decls from argument list. */
5350 source_start_java_method (fndecl)
5357 current_function_decl = fndecl;
5359 /* New scope for the function */
5361 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
5362 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
5364 tree type = TREE_VALUE (tem);
5365 tree name = TREE_PURPOSE (tem);
5367 /* If type is incomplete. Create an incomplete decl and ask for
5368 the decl to be patched later */
5369 if (INCOMPLETE_TYPE_P (type))
5372 tree real_type = GET_REAL_TYPE (type);
5373 parm_decl = build_decl (PARM_DECL, name, real_type);
5374 type = obtain_incomplete_type (type);
5375 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
5376 jdep = CLASSD_LAST (ctxp->classd_list);
5377 JDEP_MISC (jdep) = name;
5378 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
5381 parm_decl = build_decl (PARM_DECL, name, type);
5383 BLOCK_CHAIN_DECL (parm_decl);
5385 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5386 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
5388 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
5391 /* Called during parsing. Creates an artificial method declaration. */
5394 create_artificial_method (class, flags, type, name, args)
5397 tree type, name, args;
5399 int saved_lineno = lineno;
5403 mdecl = make_node (FUNCTION_TYPE);
5404 TREE_TYPE (mdecl) = type;
5405 TYPE_ARG_TYPES (mdecl) = args;
5406 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
5407 lineno = saved_lineno;
5408 DECL_ARTIFICIAL (mdecl) = 1;
5412 /* Starts the body if an artifical method. */
5415 start_artificial_method_body (mdecl)
5418 DECL_SOURCE_LINE (mdecl) = 1;
5419 DECL_SOURCE_LINE_MERGE (mdecl, 1);
5420 source_start_java_method (mdecl);
5425 end_artificial_method_body (mdecl)
5428 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
5432 /* Called during expansion. Push decls formerly built from argument
5433 list so they're usable during expansion. */
5436 expand_start_java_method (fndecl)
5441 current_function_decl = fndecl;
5443 announce_function (fndecl);
5444 pushlevel (1); /* Push parameters */
5445 ptr = &DECL_ARGUMENTS (fndecl);
5446 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5449 tree next = TREE_CHAIN (tem);
5450 tree type = TREE_TYPE (tem);
5451 #ifdef PROMOTE_PROTOTYPES
5452 if (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
5453 && INTEGRAL_TYPE_P (type))
5454 type = integer_type_node;
5456 DECL_ARG_TYPE (tem) = type;
5457 layout_decl (tem, 0);
5459 INITIALIZED_P (tem) = 1; /* Parms are initialized */
5461 ptr = &TREE_CHAIN (tem);
5465 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5466 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
5469 /* Terminate a function and expand its body. */
5472 source_end_java_method ()
5474 tree fndecl = current_function_decl;
5476 java_parser_context_save_global ();
5477 lineno = ctxp->last_ccb_indent1;
5479 /* Set EH language codes */
5480 java_set_exception_lang_code ();
5482 /* Generate function's code */
5483 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
5484 && ! flag_emit_class_files)
5485 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
5487 /* pop out of its parameters */
5488 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5490 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
5492 /* Generate rtl for function exit. */
5493 if (! flag_emit_class_files)
5495 lineno = DECL_SOURCE_LINE_LAST (fndecl);
5496 /* Emit catch-finally clauses */
5498 expand_function_end (input_filename, lineno, 0);
5500 /* Run the optimizers and output assembler code for this function. */
5501 rest_of_compilation (fndecl);
5504 current_function_decl = NULL_TREE;
5505 /* permanent_allocation (1); */
5506 java_parser_context_restore_global ();
5509 /* Record EXPR in the current function block. Complements compound
5510 expression second operand if necessary. */
5513 java_method_add_stmt (fndecl, expr)
5516 return add_stmt_to_block (DECL_FUNCTION_BODY (fndecl), NULL_TREE, expr);
5520 add_stmt_to_block (b, type, stmt)
5523 tree body = BLOCK_EXPR_BODY (b), c;
5525 if (java_error_count)
5528 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
5531 BLOCK_EXPR_BODY (b) = c;
5532 TREE_SIDE_EFFECTS (c) = 1;
5536 /* Add STMT to EXISTING if possible, otherwise create a new
5537 COMPOUND_EXPR and add STMT to it. */
5540 add_stmt_to_compound (existing, type, stmt)
5541 tree existing, type, stmt;
5544 return build (COMPOUND_EXPR, type, existing, stmt);
5549 /* Hold THIS for the scope of the current public method decl. */
5550 static tree current_this;
5552 void java_layout_seen_class_methods ()
5554 tree previous_list = all_class_list;
5555 tree end = NULL_TREE;
5560 for (current = previous_list;
5561 current != end; current = TREE_CHAIN (current))
5562 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
5564 if (previous_list != all_class_list)
5566 end = previous_list;
5567 previous_list = all_class_list;
5574 /* Layout the methods of all classes loaded in one way on an
5575 other. Check methods of source parsed classes. Then reorder the
5576 fields and layout the classes or the type of all source parsed
5580 java_layout_classes ()
5583 int save_error_count = java_error_count;
5585 /* Layout the methods of all classes seen so far */
5586 java_layout_seen_class_methods ();
5587 java_parse_abort_on_error ();
5588 all_class_list = NULL_TREE;
5590 /* Then check the methods of all parsed classes */
5591 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5592 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
5593 CHECK_METHODS (TREE_VALUE (current));
5594 java_parse_abort_on_error ();
5596 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5598 current_class = TREE_TYPE (TREE_VALUE (current));
5600 /* Reverse the fields, but leave the dummy field in front.
5601 Fields are already ordered for Object and Class */
5602 if (TYPE_FIELDS (current_class) && current_class != object_type_node
5603 && current_class != class_type_node)
5605 /* If the dummy field is there, reverse the right fields and
5606 just layout the type for proper fields offset */
5607 if (!DECL_NAME (TYPE_FIELDS (current_class)))
5609 tree fields = TYPE_FIELDS (current_class);
5610 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
5611 TYPE_SIZE (current_class) = NULL_TREE;
5612 layout_type (current_class);
5614 /* We don't have a dummy field, we need to layout the class,
5615 after having reversed the fields */
5618 TYPE_FIELDS (current_class) =
5619 nreverse (TYPE_FIELDS (current_class));
5620 TYPE_SIZE (current_class) = NULL_TREE;
5621 layout_class (current_class);
5625 layout_class (current_class);
5627 /* From now on, the class is considered completely loaded */
5628 CLASS_LOADED_P (current_class) = 1;
5630 /* Error reported by the caller */
5631 if (java_error_count)
5635 /* We might have reloaded classes durign the process of laying out
5636 classes for code generation. We must layout the methods of those
5637 late additions, as constructor checks might use them */
5638 java_layout_seen_class_methods ();
5639 java_parse_abort_on_error ();
5642 /* Expand all methods in all registered classes. */
5645 java_complete_expand_methods ()
5649 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5651 tree class_type = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current));
5654 current_class = TREE_TYPE (current);
5656 /* Initialize a new constant pool */
5657 init_outgoing_cpool ();
5659 /* We want <clinit> (if any) to be processed first. */
5660 decl = tree_last (TYPE_METHODS (class_type));
5661 if (decl && DECL_NAME (decl) == clinit_identifier_node)
5663 tree list = nreverse (TYPE_METHODS (class_type));
5664 list = TREE_CHAIN (list);
5665 TREE_CHAIN (decl) = NULL_TREE;
5666 TYPE_METHODS (class_type) = chainon (decl, nreverse (list));
5669 /* Don't process function bodies in interfaces */
5670 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5671 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5673 current_function_decl = decl;
5674 /* Don't generate debug info on line zero when expanding a
5675 generated constructor. */
5676 if (DECL_CONSTRUCTOR_P (decl) && !DECL_FUNCTION_BODY (decl))
5678 /* If we found errors, it's too dangerous to try to generate
5679 and expand a constructor */
5680 if (!java_error_count)
5682 restore_line_number_status (1);
5683 java_complete_expand_method (decl);
5684 restore_line_number_status (0);
5687 else if (METHOD_ABSTRACT (decl) || METHOD_NATIVE (decl))
5690 java_complete_expand_method (decl);
5693 /* Now verify constructor circularity (stop after the first one
5695 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5696 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5697 if (DECL_CONSTRUCTOR_P (decl) &&
5698 verify_constructor_circularity (decl, decl))
5701 /* Make the class data, register it and run the rest of decl
5702 compilation on it */
5703 if (!java_error_count)
5705 if (flag_emit_class_files)
5706 write_classfile (current_class);
5707 else if (! flag_syntax_only)
5708 finish_class (current_class);
5713 /* Hold a list of catch clauses list. The first element of this list is
5714 the list of the catch clauses of the currently analysed try block. */
5715 static tree currently_caught_type_list;
5717 /* Complete and expand a method. */
5720 java_complete_expand_method (mdecl)
5723 /* Fix constructors before expanding them */
5724 if (DECL_CONSTRUCTOR_P (mdecl))
5725 fix_constructors (mdecl);
5727 /* Expand functions that have a body */
5728 if (DECL_FUNCTION_BODY (mdecl))
5730 tree fbody = DECL_FUNCTION_BODY (mdecl);
5731 tree block_body = BLOCK_EXPR_BODY (fbody);
5732 expand_start_java_method (mdecl);
5733 build_result_decl (mdecl);
5736 = (!METHOD_STATIC (mdecl) ?
5737 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
5739 /* Purge the `throws' list of unchecked exceptions */
5740 purge_unchecked_exceptions (mdecl);
5742 /* Install exceptions thrown with `throws' */
5743 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
5745 if (block_body != NULL_TREE)
5747 block_body = java_complete_tree (block_body);
5748 check_for_initialization (block_body);
5750 BLOCK_EXPR_BODY (fbody) = block_body;
5752 if ((block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
5753 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
5754 missing_return_error (current_function_decl);
5756 complete_start_java_method (mdecl);
5758 /* Don't go any further if we've found error(s) during the
5760 if (!java_error_count)
5761 source_end_java_method ();
5764 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
5768 /* Pop the exceptions and sanity check */
5770 if (currently_caught_type_list)
5771 fatal ("Exception list non empty - java_complete_expand_method");
5775 /* Craft a body for default constructor. Patch existing constructor
5776 bodies with call to super() and field initialization statements if
5780 fix_constructors (mdecl)
5783 tree body = DECL_FUNCTION_BODY (mdecl);
5787 /* The constructor body must be crafted by hand. It's the
5788 constructor we defined when we realize we didn't have the
5789 CLASSNAME() constructor */
5793 /* It is an error for the compiler to generate a default
5794 constructor if the superclass doesn't have a constructor that
5795 takes no argument */
5796 if (verify_constructor_super ())
5798 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (current_class));
5799 char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
5800 parse_error_context (lookup_cl (TYPE_NAME (current_class)),
5801 "No constructor matching `%s()' found in "
5802 "class `%s'", n, n);
5805 start_artificial_method_body (mdecl);
5807 /* We don't generate a super constructor invocation if we're
5808 compiling java.lang.Object. build_super_invocation takes care
5810 compound = java_method_add_stmt (mdecl, build_super_invocation ());
5812 end_artificial_method_body (mdecl);
5814 /* Search for an explicit constructor invocation */
5818 tree main_block = BLOCK_EXPR_BODY (body);
5819 tree compound = NULL_TREE;
5822 switch (TREE_CODE (body))
5825 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
5829 case EXPR_WITH_FILE_LOCATION:
5830 body = TREE_OPERAND (body, 0);
5833 body = BLOCK_EXPR_BODY (body);
5839 /* The constructor is missing an invocation of super() */
5841 compound = add_stmt_to_compound (compound, NULL_TREE,
5842 build_super_invocation ());
5844 /* Fix the constructor main block if we're adding extra stmts */
5847 compound = add_stmt_to_compound (compound, NULL_TREE,
5848 BLOCK_EXPR_BODY (main_block));
5849 BLOCK_EXPR_BODY (main_block) = compound;
5854 /* Browse constructors in the super class, searching for a constructor
5855 that doesn't take any argument. Return 0 if one is found, 1
5859 verify_constructor_super ()
5861 tree class = CLASSTYPE_SUPER (current_class);
5868 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
5870 if (DECL_CONSTRUCTOR_P (mdecl)
5871 && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (mdecl))) == end_params_node)
5878 /* Expand finals. */
5881 java_expand_finals ()
5885 /* Generate code for all context remembered for code generation. */
5888 java_expand_classes ()
5890 int save_error_count = java_error_count;
5891 java_parse_abort_on_error ();
5892 if (!(ctxp = ctxp_for_generation))
5894 java_layout_classes ();
5895 java_parse_abort_on_error ();
5897 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
5899 ctxp = ctxp_for_generation;
5900 lang_init_source (2); /* Error msgs have method prototypes */
5901 java_complete_expand_methods (); /* Complete and expand method bodies */
5902 java_parse_abort_on_error ();
5903 java_expand_finals (); /* Expand and check the finals */
5904 java_parse_abort_on_error ();
5905 java_check_final (); /* Check unitialized final */
5906 java_parse_abort_on_error ();
5910 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
5911 a tree list node containing RIGHT. Fore coming RIGHTs will be
5912 chained to this hook. LOCATION contains the location of the
5913 separating `.' operator. */
5916 make_qualified_primary (primary, right, location)
5917 tree primary, right;
5922 /* We want to process THIS . xxx symbolicaly, to keep it consistent
5923 with the way we're processing SUPER. A THIS from a primary as a
5924 different form than a SUPER. Turn THIS into something symbolic */
5925 if (TREE_CODE (primary) == THIS_EXPR)
5927 wfl = build_wfl_node (this_identifier_node, input_filename, 0, 0);
5928 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5929 wfl = make_qualified_name (wfl, right, location);
5930 PRIMARY_P (wfl) = 1;
5933 /* Other non WFL node are wrapped around a WFL */
5934 else if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
5936 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
5937 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5938 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (primary, NULL_TREE);
5943 if (!EXPR_WFL_QUALIFICATION (primary))
5944 EXPR_WFL_QUALIFICATION (primary) =
5945 build_tree_list (primary, NULL_TREE);
5948 EXPR_WFL_LINECOL (right) = location;
5949 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
5950 PRIMARY_P (wfl) = 1;
5954 /* Simple merge of two name separated by a `.' */
5957 merge_qualified_name (left, right)
5961 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
5962 IDENTIFIER_LENGTH (left));
5963 obstack_1grow (&temporary_obstack, '.');
5964 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
5965 IDENTIFIER_LENGTH (right));
5966 node = get_identifier (obstack_base (&temporary_obstack));
5967 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
5968 QUALIFIED_P (node) = 1;
5972 /* Merge the two parts of a qualified name into LEFT. Set the
5973 location information of the resulting node to LOCATION, usually
5974 inherited from the location information of the `.' operator. */
5977 make_qualified_name (left, right, location)
5981 #ifdef USE_COMPONENT_REF
5982 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
5983 EXPR_WFL_LINECOL (node) = location;
5986 tree left_id = EXPR_WFL_NODE (left);
5987 tree right_id = EXPR_WFL_NODE (right);
5990 merge = merge_qualified_name (left_id, right_id);
5992 /* Left wasn't qualified and is now qualified */
5993 if (!QUALIFIED_P (left_id))
5995 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
5996 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
5997 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
6000 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
6001 EXPR_WFL_LINECOL (wfl) = location;
6002 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
6004 EXPR_WFL_NODE (left) = merge;
6009 /* Extract the last identifier component of the qualified in WFL. The
6010 last identifier is removed from the linked list */
6013 cut_identifier_in_qualified (wfl)
6017 tree previous = NULL_TREE;
6018 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
6019 if (!TREE_CHAIN (q))
6022 fatal ("Operating on a non qualified qualified WFL - "
6023 "cut_identifier_in_qualified");
6024 TREE_CHAIN (previous) = NULL_TREE;
6025 return TREE_PURPOSE (q);
6029 /* Resolve the expression name NAME. Return its decl. */
6032 resolve_expression_name (id, orig)
6036 tree name = EXPR_WFL_NODE (id);
6039 /* 6.5.5.1: Simple expression names */
6040 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
6042 /* 15.13.1: NAME can appear within the scope of a local variable
6044 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
6047 /* 15.13.1: NAME can appear within a class declaration */
6050 decl = lookup_field_wrapper (current_class, name);
6053 int fs = FIELD_STATIC (decl);
6054 /* Instance variable (8.3.1.1) can't appear within
6055 static method, static initializer or initializer for
6056 a static variable. */
6057 if (!fs && METHOD_STATIC (current_function_decl))
6060 (id, "Can't make a static reference to nonstatic variable "
6061 "`%s' in class `%s'",
6062 IDENTIFIER_POINTER (name),
6063 IDENTIFIER_POINTER (DECL_NAME
6064 (TYPE_NAME (current_class))));
6065 return error_mark_node;
6067 /* Instance variables can't appear as an argument of
6068 an explicit constructor invocation */
6069 if (!fs && ctxp->explicit_constructor_p)
6072 (id, "Can't reference `%s' before the superclass "
6073 "constructor has been called", IDENTIFIER_POINTER (name));
6074 return error_mark_node;
6077 /* Otherwise build what it takes to access the field */
6078 decl = build_field_ref ((fs ? NULL_TREE : current_this),
6079 current_class, name);
6080 if (fs && !flag_emit_class_files)
6081 decl = build_class_init (current_class, decl);
6082 /* We may be asked to save the real field access node */
6085 /* And we return what we got */
6088 /* Fall down to error report on undefined variable */
6091 /* 6.5.5.2 Qualified Expression Names */
6096 qualify_ambiguous_name (id);
6097 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
6098 /* 15.10.2: Accessing Superclass Members using super */
6099 return resolve_field_access (id, NULL, NULL);
6102 /* We've got an error here */
6103 parse_error_context (id, "Undefined variable `%s'",
6104 IDENTIFIER_POINTER (name));
6106 return error_mark_node;
6109 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
6110 We return something suitable to generate the field access. We also
6111 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
6112 recipient's address can be null. */
6115 resolve_field_access (qual_wfl, field_decl, field_type)
6117 tree *field_decl, *field_type;
6121 tree decl, where_found, type_found;
6123 if (resolve_qualified_expression_name (qual_wfl, &decl,
6124 &where_found, &type_found))
6125 return error_mark_node;
6127 /* Resolve the LENGTH field of an array here */
6128 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
6129 && ! flag_emit_class_files)
6131 tree length = build_java_array_length_access (where_found);
6133 build_java_arraynull_check (type_found, length, int_type_node);
6135 /* We might have been trying to resolve field.method(). In which
6136 case, the resolution is over and decl is the answer */
6137 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
6139 else if (JDECL_P (decl))
6141 int static_final_found = 0;
6143 type_found = DECL_CONTEXT (decl);
6144 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
6145 if (FIELD_FINAL (decl)
6146 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
6147 && DECL_LANG_SPECIFIC (decl)
6148 && DECL_INITIAL (decl))
6150 field_ref = DECL_INITIAL (decl);
6151 static_final_found = 1;
6154 field_ref = build_field_ref ((is_static ? NULL_TREE : where_found),
6155 type_found, DECL_NAME (decl));
6156 if (field_ref == error_mark_node)
6157 return error_mark_node;
6158 if (is_static && !static_final_found && !flag_emit_class_files)
6160 field_ref = build_class_init (type_found, field_ref);
6161 /* If the static field was identified by an expression that
6162 needs to be generated, make the field access a compound
6163 expression whose first part of the evaluation of the
6164 field selector part. */
6165 if (where_found && TREE_CODE (where_found) != TYPE_DECL
6166 && TREE_CODE (where_found) != RECORD_TYPE)
6168 tree type = QUAL_DECL_TYPE (field_ref);
6169 field_ref = build (COMPOUND_EXPR, type, where_found, field_ref);
6179 *field_type = (QUAL_DECL_TYPE (decl) ?
6180 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
6184 /* If NODE is an access to f static field, strip out the class
6185 initialization part and return the field decl, otherwise, return
6189 strip_out_static_field_access_decl (node)
6192 if (TREE_CODE (node) == COMPOUND_EXPR)
6194 tree op1 = TREE_OPERAND (node, 1);
6195 if (TREE_CODE (op1) == COMPOUND_EXPR)
6197 tree call = TREE_OPERAND (op1, 0);
6198 if (TREE_CODE (call) == CALL_EXPR
6199 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
6200 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
6201 == soft_initclass_node)
6202 return TREE_OPERAND (op1, 1);
6208 /* 6.5.5.2: Qualified Expression Names */
6211 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
6213 tree *found_decl, *type_found, *where_found;
6215 int from_type = 0; /* Field search initiated from a type */
6216 int from_super = 0, from_cast = 0;
6217 int previous_call_static = 0;
6219 tree decl = NULL_TREE, type = NULL_TREE, q;
6220 *type_found = *where_found = NULL_TREE;
6222 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
6224 tree qual_wfl = QUAL_WFL (q);
6226 /* 15.10.1 Field Access Using a Primary */
6227 switch (TREE_CODE (qual_wfl))
6230 case NEW_CLASS_EXPR:
6231 /* If the access to the function call is a non static field,
6232 build the code to access it. */
6233 if (JDECL_P (decl) && !FIELD_STATIC (decl))
6235 decl = maybe_access_field (decl, *where_found,
6236 DECL_CONTEXT (decl));
6237 if (decl == error_mark_node)
6240 /* And code for the function call */
6241 if (complete_function_arguments (qual_wfl))
6243 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
6244 CALL_USING_SUPER (qual_wfl) = 1;
6246 patch_method_invocation (qual_wfl, decl, type, &is_static, NULL);
6247 if (*where_found == error_mark_node)
6249 *type_found = type = QUAL_DECL_TYPE (*where_found);
6251 /* If the previous call was static and this one is too,
6252 build a compound expression to hold the two (because in
6253 that case, previous function calls aren't transported as
6254 forcoming function's argument. */
6255 if (previous_call_static && is_static)
6257 decl = build (COMPOUND_EXPR, type, decl, *where_found);
6258 TREE_SIDE_EFFECTS (decl) = 1;
6262 previous_call_static = is_static;
6263 decl = *where_found;
6267 case NEW_ARRAY_EXPR:
6268 *where_found = decl = java_complete_tree (qual_wfl);
6269 if (decl == error_mark_node)
6271 *type_found = type = QUAL_DECL_TYPE (decl);
6272 CLASS_LOADED_P (type) = 1;
6276 *where_found = decl = java_complete_tree (qual_wfl);
6277 if (decl == error_mark_node)
6279 *type_found = type = QUAL_DECL_TYPE (decl);
6283 case CONDITIONAL_EXPR:
6285 *where_found = decl = java_complete_tree (qual_wfl);
6286 if (decl == error_mark_node)
6288 *type_found = type = QUAL_DECL_TYPE (decl);
6292 /* If the access to the function call is a non static field,
6293 build the code to access it. */
6294 if (JDECL_P (decl) && !FIELD_STATIC (decl))
6296 decl = maybe_access_field (decl, *where_found, type);
6297 if (decl == error_mark_node)
6300 /* And code for the array reference expression */
6301 decl = java_complete_tree (qual_wfl);
6302 if (decl == error_mark_node)
6304 type = QUAL_DECL_TYPE (decl);
6308 /* Fix for -Wall Just go to the next statement. Don't
6312 /* If we fall here, we weren't processing a (static) function call. */
6313 previous_call_static = 0;
6315 /* It can be the keyword THIS */
6316 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
6321 (wfl, "Keyword `this' used outside allowed context");
6324 /* We have to generate code for intermediate acess */
6325 *where_found = decl = current_this;
6326 *type_found = type = QUAL_DECL_TYPE (decl);
6330 /* 15.10.2 Accessing Superclass Members using SUPER */
6331 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
6334 /* Check on the restricted use of SUPER */
6335 if (METHOD_STATIC (current_function_decl)
6336 || current_class == object_type_node)
6339 (wfl, "Keyword `super' used outside allowed context");
6342 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
6343 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
6344 CLASSTYPE_SUPER (current_class),
6345 build_this (EXPR_WFL_LINECOL (qual_wfl)));
6346 *where_found = decl = java_complete_tree (node);
6347 if (decl == error_mark_node)
6349 *type_found = type = QUAL_DECL_TYPE (decl);
6350 from_super = from_type = 1;
6354 /* 15.13.1: Can't search for field name in packages, so we
6355 assume a variable/class name was meant. */
6356 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
6358 tree name = resolve_package (wfl, &q);
6361 *where_found = decl = resolve_no_layout (name, qual_wfl);
6362 /* We wan't to be absolutely that the class is laid
6363 out. We're going to search something inside it. */
6364 *type_found = type = TREE_TYPE (decl);
6365 layout_class (type);
6367 /* Should be a list, really. FIXME */
6368 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 1;
6369 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 0;
6373 if (from_super || from_cast)
6375 ((from_cast ? qual_wfl : wfl),
6376 "No variable `%s' defined in class `%s'",
6377 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6378 lang_printable_name (type, 0));
6381 (qual_wfl, "Undefined variable or class name: `%s'",
6382 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
6387 /* We have a type name. It's been already resolved when the
6388 expression was qualified. */
6389 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
6391 if (!(decl = QUAL_RESOLUTION (q)))
6392 return 1; /* Error reported already */
6394 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
6397 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
6398 java_accstring_lookup (get_access_flags_from_decl (decl)),
6399 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6400 IDENTIFIER_POINTER (DECL_NAME (decl)),
6401 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6404 check_deprecation (qual_wfl, decl);
6406 type = TREE_TYPE (decl);
6409 /* We resolve and expression name */
6414 /* If there exists an early resolution, use it. That occurs
6415 only once and we know that there are more things to
6416 come. Don't do that when processing something after SUPER
6417 (we need more thing to be put in place below */
6418 if (!from_super && QUAL_RESOLUTION (q))
6420 decl = QUAL_RESOLUTION (q);
6423 if (!FIELD_STATIC (decl))
6424 *where_found = current_this;
6427 *where_found = TREE_TYPE (decl);
6428 if (TREE_CODE (*where_found) == POINTER_TYPE)
6429 *where_found = TREE_TYPE (*where_found);
6434 /* We have to search for a field, knowing the type of its
6435 container. The flag FROM_TYPE indicates that we resolved
6436 the last member of the expression as a type name, which
6437 means that for the resolution of this field, we'll look
6438 for other errors than if it was resolved as a member of
6443 tree field_decl_type; /* For layout */
6445 if (!from_type && !JREFERENCE_TYPE_P (type))
6448 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
6449 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6450 lang_printable_name (type, 0),
6451 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6455 field_decl = lookup_field_wrapper (type,
6456 EXPR_WFL_NODE (qual_wfl));
6457 if (field_decl == NULL_TREE)
6460 (qual_wfl, "No variable `%s' defined in class `%s'",
6461 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6462 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6465 if (field_decl == error_mark_node)
6468 /* Layout the type of field_decl, since we may need
6469 it. Don't do primitive types or loaded classes. The
6470 situation of non primitive arrays may not handled
6471 properly here. FIXME */
6472 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
6473 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
6475 field_decl_type = TREE_TYPE (field_decl);
6476 if (!JPRIMITIVE_TYPE_P (field_decl_type)
6477 && !CLASS_LOADED_P (field_decl_type)
6478 && !TYPE_ARRAY_P (field_decl_type))
6479 resolve_and_layout (field_decl_type, NULL_TREE);
6480 if (TYPE_ARRAY_P (field_decl_type))
6481 CLASS_LOADED_P (field_decl_type) = 1;
6483 /* Check on accessibility here */
6484 if (not_accessible_p (type, field_decl, from_super))
6488 "Can't access %s field `%s.%s' from `%s'",
6489 java_accstring_lookup
6490 (get_access_flags_from_decl (field_decl)),
6491 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6492 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
6494 (DECL_NAME (TYPE_NAME (current_class))));
6497 check_deprecation (qual_wfl, field_decl);
6499 /* There are things to check when fields are accessed
6500 from type. There are no restrictions on a static
6501 declaration of the field when it is accessed from an
6503 is_static = FIELD_STATIC (field_decl);
6504 if (!from_super && from_type
6505 && !TYPE_INTERFACE_P (type) && !is_static)
6508 (qual_wfl, "Can't make a static reference to nonstatic "
6509 "variable `%s' in class `%s'",
6510 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6511 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6514 from_cast = from_super = 0;
6516 /* If we need to generate something to get a proper
6517 handle on what this field is accessed from, do it
6521 decl = maybe_access_field (decl, *where_found, *type_found);
6522 if (decl == error_mark_node)
6526 /* We want to keep the location were found it, and the type
6528 *where_found = decl;
6531 /* This is the decl found and eventually the next one to
6536 type = QUAL_DECL_TYPE (decl);
6543 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
6544 can't be accessed from REFERENCE (a record type). */
6546 int not_accessible_p (reference, member, from_super)
6547 tree reference, member;
6550 int access_flag = get_access_flags_from_decl (member);
6552 /* Access always granted for members declared public */
6553 if (access_flag & ACC_PUBLIC)
6556 /* Check access on protected members */
6557 if (access_flag & ACC_PROTECTED)
6559 /* Access granted if it occurs from within the package
6560 containing the class in which the protected member is
6562 if (class_in_current_package (DECL_CONTEXT (member)))
6565 /* If accessed with the form `super.member', then access is granted */
6569 /* Otherwise, access is granted if occuring from the class where
6570 member is declared or a subclass of it */
6571 if (inherits_from_p (reference, current_class))
6576 /* Check access on private members. Access is granted only if it
6577 occurs from within the class in witch it is declared */
6578 if (access_flag & ACC_PRIVATE)
6579 return (current_class == DECL_CONTEXT (member) ? 0 : 1);
6581 /* Default access are permitted only when occuring within the
6582 package in which the type (REFERENCE) is declared. In other words,
6583 REFERENCE is defined in the current package */
6585 return !class_in_current_package (reference);
6587 /* Otherwise, access is granted */
6591 /* Test deprecated decl access. */
6593 check_deprecation (wfl, decl)
6596 char *file = DECL_SOURCE_FILE (decl);
6597 /* Complain if the field is deprecated and the file it was defined
6598 in isn't compiled at the same time the file which contains its
6600 if (DECL_DEPRECATED (decl)
6601 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
6604 switch (TREE_CODE (decl))
6607 strcpy (the, "method");
6610 strcpy (the, "field");
6613 strcpy (the, "class");
6616 fatal ("unexpected DECL code - check_deprecation");
6618 parse_warning_context
6619 (wfl, "The %s `%s' in class `%s' has been deprecated",
6620 the, lang_printable_name (decl, 0),
6621 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
6625 /* Returns 1 if class was declared in the current package, 0 otherwise */
6628 class_in_current_package (class)
6631 static tree cache = NULL_TREE;
6638 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
6640 /* If the current package is empty and the name of CLASS is
6641 qualified, class isn't in the current package. If there is a
6642 current package and the name of the CLASS is not qualified, class
6643 isn't in the current package */
6644 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
6647 /* If there is not package and the name of CLASS isn't qualified,
6648 they belong to the same unnamed package */
6649 if (!ctxp->package && !qualified_flag)
6652 /* Compare the left part of the name of CLASS with the package name */
6653 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
6654 if (ctxp->package == left)
6662 /* This function may generate code to access DECL from WHERE. This is
6663 done only if certain conditions meet. */
6666 maybe_access_field (decl, where, type)
6667 tree decl, where, type;
6669 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
6670 && !FIELD_STATIC (decl))
6671 decl = build_field_ref (where ? where : current_this,
6672 (type ? type : DECL_CONTEXT (decl)),
6677 /* Build a method invocation, by patching PATCH. If non NULL
6678 and according to the situation, PRIMARY and WHERE may be
6679 used. IS_STATIC is set to 1 if the invoked function is static. */
6682 patch_method_invocation (patch, primary, where, is_static, ret_decl)
6683 tree patch, primary, where;
6687 tree wfl = TREE_OPERAND (patch, 0);
6688 tree args = TREE_OPERAND (patch, 1);
6689 tree name = EXPR_WFL_NODE (wfl);
6691 int is_static_flag = 0;
6692 int is_super_init = 0;
6693 tree this_arg = NULL_TREE;
6695 /* Should be overriden if everything goes well. Otherwise, if
6696 something fails, it should keep this value. It stop the
6697 evaluation of a bogus assignment. See java_complete_tree,
6698 MODIFY_EXPR: for the reasons why we sometimes want to keep on
6699 evaluating an assignment */
6700 TREE_TYPE (patch) = error_mark_node;
6702 /* Since lookup functions are messing with line numbers, save the
6704 java_parser_context_save_global ();
6706 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
6708 /* Resolution of qualified name, excluding constructors */
6709 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
6711 tree class_decl, identifier, identifier_wfl;
6712 /* Extract the last IDENTIFIER of the qualified
6713 expression. This is a wfl and we will use it's location
6714 data during error report. */
6715 identifier_wfl = cut_identifier_in_qualified (wfl);
6716 identifier = EXPR_WFL_NODE (identifier_wfl);
6718 /* Given the context, IDENTIFIER is syntactically qualified
6719 as a MethodName. We need to qualify what's before */
6720 qualify_ambiguous_name (wfl);
6722 /* Package resolution are erroneous */
6723 if (RESOLVE_PACKAGE_NAME_P (wfl))
6726 breakdown_qualified (&remainder, NULL, EXPR_WFL_NODE (wfl));
6727 parse_error_context (wfl, "Can't search method `%s' in package "
6728 "`%s'",IDENTIFIER_POINTER (identifier),
6729 IDENTIFIER_POINTER (remainder));
6730 PATCH_METHOD_RETURN_ERROR ();
6732 /* We're resolving a call from a type */
6733 else if (RESOLVE_TYPE_NAME_P (wfl))
6735 tree decl = QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl));
6736 tree name = DECL_NAME (decl);
6739 class_decl = resolve_and_layout (name, wfl);
6740 if (CLASS_INTERFACE (decl))
6743 (identifier_wfl, "Can't make static reference to method "
6744 "`%s' in interface `%s'", IDENTIFIER_POINTER (identifier),
6745 IDENTIFIER_POINTER (name));
6746 PATCH_METHOD_RETURN_ERROR ();
6748 /* Look the method up in the type selector. The method ought
6750 type = TREE_TYPE (class_decl);
6751 list = lookup_method_invoke (0, wfl, type, identifier, args);
6752 if (list && !METHOD_STATIC (list))
6754 char *fct_name = strdup (lang_printable_name (list, 0));
6757 "Can't make static reference to method `%s %s' in class `%s'",
6758 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6759 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6761 PATCH_METHOD_RETURN_ERROR ();
6763 args = nreverse (args);
6765 /* We're resolving an expression name */
6770 /* 1- Find the field to which the call applies */
6771 field = resolve_field_access (wfl, NULL, &type);
6772 if (field == error_mark_node)
6773 PATCH_METHOD_RETURN_ERROR ();
6774 /* field is used in lieu of a primary. It alows us not to
6775 report errors on erroneous use of `this' in
6779 /* 2- Do the layout of the class where the last field
6780 was found, so we can search it. */
6781 class_decl = resolve_and_layout (type, NULL_TREE);
6782 if (class_decl != NULL_TREE)
6783 type = TREE_TYPE (class_decl);
6785 /* 3- Retrieve a filtered list of method matches, Refine
6786 if necessary. In any cases, point out errors. */
6787 list = lookup_method_invoke (0, identifier_wfl, type,
6790 /* 4- Add the field as an argument */
6791 args = nreverse (args);
6795 /* IDENTIFIER_WFL will be used to report any problem further */
6796 wfl = identifier_wfl;
6798 /* Resolution of simple names, names generated after a primary: or
6802 tree class_to_search;
6803 int lc; /* Looking for Constructor */
6805 /* We search constructor in their target class */
6806 if (CALL_CONSTRUCTOR_P (patch))
6808 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6809 class_to_search = EXPR_WFL_NODE (wfl);
6810 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6811 this_identifier_node)
6812 class_to_search = NULL_TREE;
6813 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6814 super_identifier_node)
6817 if (CLASSTYPE_SUPER (current_class))
6819 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
6822 parse_error_context (wfl, "Can't invoke super constructor "
6823 "on java.lang.Object");
6824 PATCH_METHOD_RETURN_ERROR ();
6828 /* Class to search is NULL if we're searching the current one */
6829 if (class_to_search)
6831 class_to_search = resolve_and_layout (class_to_search,
6833 if (!class_to_search)
6836 (wfl, "Class `%s' not found in type declaration",
6837 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6838 PATCH_METHOD_RETURN_ERROR ();
6841 /* Can't instantiate an abstract class, but we can
6842 invoke it's constructor. It's use within the `new'
6843 context is denied here. */
6844 if (CLASS_ABSTRACT (class_to_search)
6845 && TREE_CODE (patch) == NEW_CLASS_EXPR)
6848 (wfl, "Class `%s' is an abstract class. It can't be "
6849 "instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6850 PATCH_METHOD_RETURN_ERROR ();
6852 class_to_search = TREE_TYPE (class_to_search);
6855 class_to_search = current_class;
6858 /* This is a regular search in the local class, unless an
6859 alternate class is specified. */
6862 class_to_search = (where ? where : current_class);
6866 /* NAME is a simple identifier or comes from a primary. Search
6867 in the class whose declaration contain the method being
6869 resolve_and_layout (class_to_search, NULL_TREE);
6870 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
6872 /* Don't continue if no method were found, as the next statement
6873 can't be executed then. */
6875 PATCH_METHOD_RETURN_ERROR ();
6877 /* Check for static reference if non static methods */
6878 if (check_for_static_method_reference (wfl, patch, list,
6879 class_to_search, primary))
6880 PATCH_METHOD_RETURN_ERROR ();
6882 /* Non static methods are called with the current object extra
6883 argument. If patch a `new TYPE()', the argument is the value
6884 returned by the object allocator. If method is resolved as a
6885 primary, use the primary otherwise use the current THIS. */
6886 args = nreverse (args);
6887 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
6888 this_arg = primary ? primary : current_this;
6891 /* Merge point of all resolution schemes. If we have nothing, this
6892 is an error, already signaled */
6894 PATCH_METHOD_RETURN_ERROR ();
6896 /* Check accessibility, position the is_static flag, build and
6898 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
6900 char *fct_name = strdup (lang_printable_name (list, 0));
6902 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
6903 java_accstring_lookup (get_access_flags_from_decl (list)),
6904 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6905 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
6906 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6908 PATCH_METHOD_RETURN_ERROR ();
6910 check_deprecation (wfl, list);
6912 is_static_flag = METHOD_STATIC (list);
6913 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
6914 args = tree_cons (NULL_TREE, this_arg, args);
6916 /* In the context of an explicit constructor invocation, we can't
6917 invoke any method relying on `this'. Exceptions are: we're
6918 invoking a static function, primary exists and is not the current
6919 this, we're creating a new object. */
6920 if (ctxp->explicit_constructor_p
6922 && (!primary || primary == current_this)
6923 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
6926 (wfl, "Can't reference `this' before the superclass constructor has "
6928 PATCH_METHOD_RETURN_ERROR ();
6930 java_parser_context_restore_global ();
6932 *is_static = is_static_flag;
6933 /* Sometimes, we want the decl of the selected method. Such as for
6937 patch = patch_invoke (patch, list, args);
6938 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
6940 /* Generate the code used to initialize fields declared with an
6941 initialization statement. For now, it returns a call the the
6942 artificial function $finit$, if required. */
6945 build_method_invocation (build_expr_wfl (finit_identifier_node,
6946 input_filename, 0, 0),
6948 patch = build (COMPOUND_EXPR, void_type_node, patch,
6949 java_complete_tree (finit_call));
6950 CAN_COMPLETE_NORMALLY (patch) = 1;
6955 /* Check that we're not trying to do a static reference to a method in
6956 non static method. Return 1 if it's the case, 0 otherwise. */
6959 check_for_static_method_reference (wfl, node, method, where, primary)
6960 tree wfl, node, method, where, primary;
6962 if (METHOD_STATIC (current_function_decl)
6963 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
6965 char *fct_name = strdup (lang_printable_name (method, 0));
6967 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
6968 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
6969 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
6976 /* Patch an invoke expression METHOD and ARGS, based on its invocation
6980 patch_invoke (patch, method, args)
6981 tree patch, method, args;
6984 tree original_call, t, ta;
6986 /* Last step for args: convert build-in types. If we're dealing with
6987 a new TYPE() type call, the first argument to the constructor
6988 isn't found in the incomming argument list, but delivered by
6990 t = TYPE_ARG_TYPES (TREE_TYPE (method));
6991 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6993 for (ta = args; t != end_params_node && ta;
6994 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
6995 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
6996 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
6997 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
6999 if (flag_emit_class_files)
7003 tree signature = build_java_signature (TREE_TYPE (method));
7004 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
7006 case INVOKE_VIRTUAL:
7007 dtable = invoke_build_dtable (0, args);
7008 func = build_invokevirtual (dtable, method);
7013 func = build_known_method_ref (method, TREE_TYPE (method),
7014 DECL_CONTEXT (method),
7018 case INVOKE_INTERFACE:
7019 dtable = invoke_build_dtable (1, args);
7020 func = build_invokeinterface (dtable, DECL_NAME (method), signature);
7024 fatal ("internal error - unknown invocation_mode result");
7027 /* Ensure self_type is initialized, (invokestatic). FIXME */
7028 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
7031 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
7032 TREE_OPERAND (patch, 0) = func;
7033 TREE_OPERAND (patch, 1) = args;
7034 original_call = patch;
7036 /* We're processing a `new TYPE ()' form. New is called an its
7037 returned value is the first argument to the constructor. We build
7038 a COMPOUND_EXPR and use saved expression so that the overall NEW
7039 expression value is a pointer to a newly created and initialized
7041 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
7043 tree class = DECL_CONTEXT (method);
7044 tree c1, saved_new, size, new;
7045 if (flag_emit_class_files)
7047 TREE_TYPE (patch) = build_pointer_type (class);
7050 if (!TYPE_SIZE (class))
7051 safe_layout_class (class);
7052 size = size_in_bytes (class);
7053 new = build (CALL_EXPR, promote_type (class),
7054 build_address_of (alloc_object_node),
7055 tree_cons (NULL_TREE, build_class_ref (class),
7056 build_tree_list (NULL_TREE,
7057 size_in_bytes (class))),
7059 saved_new = save_expr (new);
7060 c1 = build_tree_list (NULL_TREE, saved_new);
7061 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
7062 TREE_OPERAND (original_call, 1) = c1;
7063 TREE_SET_CODE (original_call, CALL_EXPR);
7064 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
7070 invocation_mode (method, super)
7074 int access = get_access_flags_from_decl (method);
7077 return INVOKE_SUPER;
7079 if (access & ACC_STATIC || access & ACC_FINAL || access & ACC_PRIVATE)
7080 return INVOKE_STATIC;
7082 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
7083 return INVOKE_STATIC;
7085 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
7086 return INVOKE_INTERFACE;
7088 if (DECL_CONSTRUCTOR_P (method))
7089 return INVOKE_STATIC;
7091 return INVOKE_VIRTUAL;
7094 /* Retrieve a refined list of matching methods. It covers the step
7095 15.11.2 (Compile-Time Step 2) */
7098 lookup_method_invoke (lc, cl, class, name, arg_list)
7101 tree class, name, arg_list;
7103 tree atl = end_params_node; /* Arg Type List */
7104 tree method, signature, list, node;
7105 char *candidates; /* Used for error report */
7107 /* Fix the arguments */
7108 for (node = arg_list; node; node = TREE_CHAIN (node))
7110 tree current_arg = TREE_TYPE (TREE_VALUE (node));
7111 /* Non primitive type may have to be resolved */
7112 if (!JPRIMITIVE_TYPE_P (current_arg))
7113 resolve_and_layout (current_arg, NULL_TREE);
7115 if (TREE_CODE (current_arg) == RECORD_TYPE)
7116 current_arg = promote_type (current_arg);
7117 atl = tree_cons (NULL_TREE, current_arg, atl);
7120 /* Find all candidates and then refine the list, searching for the
7121 most specific method. */
7122 list = find_applicable_accessible_methods_list (lc, class, name, atl);
7123 list = find_most_specific_methods_list (list);
7124 if (list && !TREE_CHAIN (list))
7125 return TREE_VALUE (list);
7127 /* Issue an error. List candidates if any. Candidates are listed
7128 only if accessible (non accessible methods may end-up here for
7129 the sake of a better error report). */
7134 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
7135 for (current = list; current; current = TREE_CHAIN (current))
7137 tree cm = TREE_VALUE (current);
7139 if (!cm || not_accessible_p (class, cm, 0))
7142 (string, " `%s' in `%s'%s",
7143 get_printable_method_name (cm),
7144 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
7145 (TREE_CHAIN (current) ? "\n" : ""));
7146 obstack_grow (&temporary_obstack, string, strlen (string));
7148 obstack_1grow (&temporary_obstack, '\0');
7149 candidates = obstack_finish (&temporary_obstack);
7151 /* Issue the error message */
7152 method = make_node (FUNCTION_TYPE);
7153 TYPE_ARG_TYPES (method) = atl;
7154 signature = build_java_argument_signature (method);
7155 parse_error_context (cl, "Can't find %s `%s(%s)' in class `%s'%s",
7156 (lc ? "constructor" : "method"),
7158 IDENTIFIER_POINTER(DECL_NAME (TYPE_NAME (class))) :
7159 IDENTIFIER_POINTER (name)),
7160 IDENTIFIER_POINTER (signature),
7161 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))),
7162 (candidates ? candidates : ""));
7166 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
7167 when we're looking for a constructor. */
7170 find_applicable_accessible_methods_list (lc, class, name, arglist)
7172 tree class, name, arglist;
7175 tree list = NULL_TREE, all_list = NULL_TREE;
7177 while (class != NULL_TREE)
7179 for (method = TYPE_METHODS (class);
7180 method != NULL_TREE; method = TREE_CHAIN (method))
7182 if (lc && !DECL_CONSTRUCTOR_P (method))
7184 else if (!lc && (DECL_CONSTRUCTOR_P (method)
7185 || (GET_METHOD_NAME (method) != name)))
7188 if (argument_types_convertible (method, arglist))
7190 /* Retain accessible methods only */
7191 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
7193 list = tree_cons (NULL_TREE, method, list);
7195 /* Also retain all selected method here */
7196 all_list = tree_cons (NULL_TREE, method, list);
7199 /* When dealing with constructor, stop here, otherwise search
7201 class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
7203 /* Either return the list obtained or all selected (but
7204 inaccessible) methods for better error report. */
7205 return (!list ? all_list : list);
7208 /* 15.11.2.2 Choose the Most Specific Method */
7211 find_most_specific_methods_list (list)
7215 tree current, new_list = NULL_TREE;
7216 for (current = list; current; current = TREE_CHAIN (current))
7219 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
7221 for (method = list; method; method = TREE_CHAIN (method))
7223 /* Don't test a method against itself */
7224 if (method == current)
7227 /* Compare arguments and location where method where declared */
7228 if (argument_types_convertible (TREE_VALUE (method),
7229 TREE_VALUE (current))
7230 && valid_method_invocation_conversion_p
7231 (DECL_CONTEXT (TREE_VALUE (method)),
7232 DECL_CONTEXT (TREE_VALUE (current))))
7234 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
7235 max = (v > max ? v : max);
7240 /* Review the list and select the maximally specific methods */
7241 for (current = list; current; current = TREE_CHAIN (current))
7242 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7243 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7245 /* If we can't find one, lower expectations and try to gather multiple
7246 maximally specific methods */
7251 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7252 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7260 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
7261 converted by method invocation conversion (5.3) to the type of the
7262 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
7263 to change less often than M1. */
7266 argument_types_convertible (m1, m2_or_arglist)
7267 tree m1, m2_or_arglist;
7269 static tree m2_arg_value = NULL_TREE;
7270 static tree m2_arg_cache = NULL_TREE;
7272 register tree m1_arg, m2_arg;
7274 m1_arg = TYPE_ARG_TYPES (TREE_TYPE (m1));
7275 if (!METHOD_STATIC (m1))
7276 m1_arg = TREE_CHAIN (m1_arg);
7278 if (m2_arg_value == m2_or_arglist)
7279 m2_arg = m2_arg_cache;
7282 /* M2_OR_ARGLIST can be a function DECL or a raw list of
7284 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
7286 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
7287 if (!METHOD_STATIC (m2_or_arglist))
7288 m2_arg = TREE_CHAIN (m2_arg);
7291 m2_arg = m2_or_arglist;
7293 m2_arg_value = m2_or_arglist;
7294 m2_arg_cache = m2_arg;
7297 while (m1_arg != end_params_node && m2_arg != end_params_node)
7299 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
7300 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
7301 TREE_VALUE (m2_arg)))
7303 m1_arg = TREE_CHAIN (m1_arg);
7304 m2_arg = TREE_CHAIN (m2_arg);
7306 return m1_arg == end_params_node && m2_arg == end_params_node;
7309 /* Qualification routines */
7312 qualify_ambiguous_name (id)
7315 tree qual, qual_wfl, name, decl, ptr_type, saved_current_class;
7316 int again, super_found = 0, this_found = 0, new_array_found = 0;
7318 /* We first qualify the first element, then derive qualification of
7319 others based on the first one. If the first element is qualified
7320 by a resolution (field or type), this resolution is stored in the
7321 QUAL_RESOLUTION of the qual element being examined. We need to
7322 save the current_class since the use of SUPER might change the
7324 saved_current_class = current_class;
7325 qual = EXPR_WFL_QUALIFICATION (id);
7328 /* Simple qualified expression feature a qual_wfl that is a
7329 WFL. Expression derived from a primary feature more complicated
7330 things like a CALL_EXPR. Expression from primary need to be
7331 worked out to extract the part on which the qualification will
7333 qual_wfl = QUAL_WFL (qual);
7334 switch (TREE_CODE (qual_wfl))
7337 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7338 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
7340 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
7341 qual_wfl = QUAL_WFL (qual);
7344 case NEW_ARRAY_EXPR:
7345 qual = TREE_CHAIN (qual);
7346 new_array_found = again = 1;
7348 case NEW_CLASS_EXPR:
7350 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7353 while (TREE_CODE (qual_wfl) == ARRAY_REF)
7354 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7357 /* Fix for -Wall. Just break doing nothing */
7360 name = EXPR_WFL_NODE (qual_wfl);
7361 ptr_type = current_class;
7363 /* If we have a THIS (from a primary), we set the context accordingly */
7364 if (name == this_identifier_node)
7366 qual = TREE_CHAIN (qual);
7367 qual_wfl = QUAL_WFL (qual);
7368 if (TREE_CODE (qual_wfl) == CALL_EXPR)
7371 name = EXPR_WFL_NODE (qual_wfl);
7374 /* If we have a SUPER, we set the context accordingly */
7375 if (name == super_identifier_node)
7377 current_class = CLASSTYPE_SUPER (ptr_type);
7378 /* Check that there is such a thing as a super class. If not,
7379 return. The error will be caught later on, during the
7383 current_class = saved_current_class;
7386 qual = TREE_CHAIN (qual);
7387 /* Do one more interation to set things up */
7388 super_found = again = 1;
7390 /* Loop one more time if we're dealing with ?: or a string
7391 constant, or a convert expression */
7392 if (TREE_CODE (qual_wfl) == CONDITIONAL_EXPR
7393 || TREE_CODE (qual_wfl) == STRING_CST
7394 || TREE_CODE (qual_wfl) == CONVERT_EXPR)
7396 qual = TREE_CHAIN (qual);
7397 qual_wfl = QUAL_WFL (qual);
7402 /* If name appears within the scope of a location variable
7403 declaration or parameter declaration, then it is an expression
7404 name. We don't carry this test out if we're in the context of the
7405 use of SUPER or THIS */
7406 if (!this_found && !super_found && (decl = IDENTIFIER_LOCAL_VALUE (name)))
7408 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7409 QUAL_RESOLUTION (qual) = decl;
7412 /* If within the class/interface NAME was found to be used there
7413 exists a (possibly inherited) field named NAME, then this is an
7414 expression name. If we saw a NEW_ARRAY_EXPR before and want to
7415 address length, it is OK. */
7416 else if ((decl = lookup_field_wrapper (ptr_type, name))
7417 || (new_array_found && name == length_identifier_node))
7419 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7420 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
7423 /* We reclassify NAME as a type name if:
7424 - NAME is a class/interface declared within the compilation
7425 unit containing NAME,
7426 - NAME is imported via a single-type-import declaration,
7427 - NAME is declared in an another compilation unit of the package
7428 of the compilation unit containing NAME,
7429 - NAME is declared by exactly on type-import-on-demand declaration
7430 of the compilation unit containing NAME. */
7431 else if ((decl = resolve_and_layout (name, NULL_TREE)))
7433 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
7434 QUAL_RESOLUTION (qual) = decl;
7437 /* Method call are expression name */
7438 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
7439 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF)
7440 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7442 /* Check here that NAME isn't declared by more than one
7443 type-import-on-demand declaration of the compilation unit
7444 containing NAME. FIXME */
7446 /* Otherwise, NAME is reclassified as a package name */
7448 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
7450 /* Propagate the qualification accross other components of the
7452 for (qual = TREE_CHAIN (qual); qual;
7453 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
7455 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7456 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
7458 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
7461 /* Store the global qualification for the ambiguous part of ID back
7463 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
7464 RESOLVE_EXPRESSION_NAME_P (id) = 1;
7465 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
7466 RESOLVE_TYPE_NAME_P (id) = 1;
7467 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7468 RESOLVE_PACKAGE_NAME_P (id) = 1;
7470 /* Restore the current class */
7471 current_class = saved_current_class;
7475 breakdown_qualified (left, right, source)
7476 tree *left, *right, source;
7478 char *p = IDENTIFIER_POINTER (source), *base;
7479 int l = IDENTIFIER_LENGTH (source);
7481 /* Breakdown NAME into REMAINDER . IDENTIFIER */
7484 while (*p != '.' && p != base)
7487 /* We didn't find a '.'. Return an error */
7493 *right = get_identifier (p+1);
7494 *left = get_identifier (IDENTIFIER_POINTER (source));
7500 /* Patch tree nodes in a function body. When a BLOCK is found, push
7501 local variable decls if present.
7502 Same as java_complete_lhs, but does resolve static finals to values. */
7505 java_complete_tree (node)
7508 node = java_complete_lhs (node);
7509 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7510 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE)
7512 tree value = DECL_INITIAL (node);
7513 DECL_INITIAL (node) = NULL_TREE;
7514 value = fold_constant_for_init (value, node);
7515 DECL_INITIAL (node) = value;
7516 if (value != NULL_TREE)
7522 /* Patch tree nodes in a function body. When a BLOCK is found, push
7523 local variable decls if present.
7524 Same as java_complete_tree, but does not resolve static finals to values. */
7527 java_complete_lhs (node)
7530 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
7533 /* CONVERT_EXPR always has its type set, even though it needs to be
7535 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
7538 /* The switch block implements cases processing container nodes
7539 first. Contained nodes are always written back. Leaves come
7540 next and return a value. */
7541 switch (TREE_CODE (node))
7545 /* 1- Block section.
7546 Set the local values on decl names so we can identify them
7547 faster when they're referenced. At that stage, identifiers
7548 are legal so we don't check for declaration errors. */
7549 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7551 DECL_CONTEXT (cn) = current_function_decl;
7552 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
7553 INITIALIZED_P (cn) = 0;
7555 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
7556 CAN_COMPLETE_NORMALLY (node) = 1;
7559 tree stmt = BLOCK_EXPR_BODY (node);
7562 if (TREE_CODE (stmt) == COMPOUND_EXPR)
7564 /* Re-order from (((A; B); C); ...; Z) to
7565 (A; (B; (C ; (...; Z)))).
7566 This makes it easier to scan the statements left-to-right
7567 without using recursion (which might overflow the stack
7568 if the block has many statements. */
7571 tree left = TREE_OPERAND (stmt, 0);
7572 if (TREE_CODE (left) != COMPOUND_EXPR)
7574 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
7575 TREE_OPERAND (left, 1) = stmt;
7578 BLOCK_EXPR_BODY (node) = stmt;
7581 /* Now do the actual complete, without deep recursion for
7583 ptr = &BLOCK_EXPR_BODY (node);
7584 while (TREE_CODE (*ptr) == COMPOUND_EXPR
7585 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
7587 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
7588 tree *next = &TREE_OPERAND (*ptr, 1);
7589 TREE_OPERAND (*ptr, 0) = cur;
7590 if (TREE_CODE (cur) == ERROR_MARK)
7592 else if (! CAN_COMPLETE_NORMALLY (cur))
7597 if (TREE_CODE (wfl_op2) == BLOCK)
7598 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
7599 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
7600 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
7604 if (TREE_CODE (wfl_op2) != CASE_EXPR
7605 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
7606 unreachable_stmt_error (*ptr);
7610 *ptr = java_complete_tree (*ptr);
7612 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
7613 return error_mark_node;
7614 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
7616 /* Turn local bindings to null */
7617 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7618 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
7620 TREE_TYPE (node) = void_type_node;
7623 /* 2- They are expressions but ultimately deal with statements */
7626 wfl_op1 = TREE_OPERAND (node, 0);
7627 COMPLETE_CHECK_OP_0 (node);
7628 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7629 return patch_throw_statement (node, wfl_op1);
7631 case SYNCHRONIZED_EXPR:
7632 wfl_op1 = TREE_OPERAND (node, 0);
7633 return patch_synchronized_statement (node, wfl_op1);
7636 return patch_try_statement (node);
7638 case TRY_FINALLY_EXPR:
7639 COMPLETE_CHECK_OP_0 (node);
7640 COMPLETE_CHECK_OP_1 (node);
7641 CAN_COMPLETE_NORMALLY (node)
7642 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
7643 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
7644 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
7647 case CLEANUP_POINT_EXPR:
7648 COMPLETE_CHECK_OP_0 (node);
7649 TREE_TYPE (node) = void_type_node;
7650 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
7653 case WITH_CLEANUP_EXPR:
7654 COMPLETE_CHECK_OP_0 (node);
7655 COMPLETE_CHECK_OP_2 (node);
7656 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
7657 TREE_TYPE (node) = void_type_node;
7660 case LABELED_BLOCK_EXPR:
7661 PUSH_LABELED_BLOCK (node);
7662 if (LABELED_BLOCK_BODY (node))
7663 COMPLETE_CHECK_OP_1 (node);
7664 TREE_TYPE (node) = void_type_node;
7665 POP_LABELED_BLOCK ();
7666 if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
7667 CAN_COMPLETE_NORMALLY (node) = 1;
7670 case EXIT_BLOCK_EXPR:
7671 /* We don't complete operand 1, because it's the return value of
7672 the EXIT_BLOCK_EXPR which doesn't exist it Java */
7673 return patch_bc_statement (node);
7676 cn = java_complete_tree (TREE_OPERAND (node, 0));
7677 if (cn == error_mark_node)
7680 /* First, the case expression must be constant */
7683 if (!TREE_CONSTANT (cn))
7685 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7686 parse_error_context (node, "Constant expression required");
7687 return error_mark_node;
7690 nn = ctxp->current_loop;
7692 /* It must be assignable to the type of the switch expression. */
7693 if (!try_builtin_assignconv (NULL_TREE,
7694 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
7696 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7699 "Incompatible type for case. Can't convert `%s' to `int'",
7700 lang_printable_name (TREE_TYPE (cn), 0));
7701 return error_mark_node;
7704 cn = fold (convert (int_type_node, cn));
7706 /* Multiple instance of a case label bearing the same
7707 value is checked during code generation. The case
7708 expression is allright so far. */
7709 TREE_OPERAND (node, 0) = cn;
7710 TREE_TYPE (node) = void_type_node;
7711 CAN_COMPLETE_NORMALLY (node) = 1;
7712 TREE_SIDE_EFFECTS (node) = 1;
7716 nn = ctxp->current_loop;
7717 /* Only one default label is allowed per switch statement */
7718 if (SWITCH_HAS_DEFAULT (nn))
7720 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7721 parse_error_context (wfl_operator,
7722 "Duplicate case label: `default'");
7723 return error_mark_node;
7726 SWITCH_HAS_DEFAULT (nn) = 1;
7727 TREE_TYPE (node) = void_type_node;
7728 TREE_SIDE_EFFECTS (node) = 1;
7729 CAN_COMPLETE_NORMALLY (node) = 1;
7735 /* Check whether the loop was enclosed in a labeled
7736 statement. If not, create one, insert the loop in it and
7738 nn = patch_loop_statement (node);
7740 /* Anyways, walk the body of the loop */
7741 if (TREE_CODE (node) == LOOP_EXPR)
7742 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7743 /* Switch statement: walk the switch expression and the cases */
7745 node = patch_switch_statement (node);
7747 if (TREE_OPERAND (node, 0) == error_mark_node)
7748 return error_mark_node;
7749 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
7750 /* If we returned something different, that's because we
7751 inserted a label. Pop the label too. */
7754 if (CAN_COMPLETE_NORMALLY (node))
7755 CAN_COMPLETE_NORMALLY (nn) = 1;
7756 POP_LABELED_BLOCK ();
7762 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7763 return patch_exit_expr (node);
7767 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7768 if (TREE_OPERAND (node, 0) == error_mark_node)
7769 return error_mark_node;
7770 /* then-else branches */
7771 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7772 if (TREE_OPERAND (node, 1) == error_mark_node)
7773 return error_mark_node;
7774 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
7775 if (TREE_OPERAND (node, 2) == error_mark_node)
7776 return error_mark_node;
7777 return patch_if_else_statement (node);
7780 case CONDITIONAL_EXPR:
7782 wfl_op1 = TREE_OPERAND (node, 0);
7783 COMPLETE_CHECK_OP_0 (node);
7784 wfl_op2 = TREE_OPERAND (node, 1);
7785 COMPLETE_CHECK_OP_1 (node);
7786 wfl_op3 = TREE_OPERAND (node, 2);
7787 COMPLETE_CHECK_OP_2 (node);
7788 return patch_conditional_expr (node, wfl_op1, wfl_op2);
7790 /* 3- Expression section */
7792 wfl_op2 = TREE_OPERAND (node, 1);
7793 TREE_OPERAND (node, 0) = nn =
7794 java_complete_tree (TREE_OPERAND (node, 0));
7795 if (wfl_op2 == empty_stmt_node)
7796 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
7799 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
7801 /* An unreachable condition in a do-while statement
7802 is *not* (technically) an unreachable statement. */
7804 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
7805 nn = EXPR_WFL_NODE (nn);
7806 if (TREE_CODE (nn) != EXIT_EXPR)
7808 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
7809 parse_error_context (wfl_operator, "Unreachable statement");
7812 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7813 if (TREE_OPERAND (node, 1) == error_mark_node)
7814 return error_mark_node;
7815 CAN_COMPLETE_NORMALLY (node)
7816 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
7818 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
7822 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7823 return patch_return (node);
7825 case EXPR_WITH_FILE_LOCATION:
7826 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
7827 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
7829 node = resolve_expression_name (node, NULL);
7830 if (node == error_mark_node)
7832 CAN_COMPLETE_NORMALLY (node) = 1;
7837 int save_lineno = lineno;
7838 lineno = EXPR_WFL_LINENO (node);
7839 body = java_complete_tree (EXPR_WFL_NODE (node));
7840 lineno = save_lineno;
7841 EXPR_WFL_NODE (node) = body;
7842 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
7843 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
7844 if (body == error_mark_node)
7846 /* Its important for the evaluation of assignment that
7847 this mark on the TREE_TYPE is propagated. */
7848 TREE_TYPE (node) = error_mark_node;
7849 return error_mark_node;
7852 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
7857 case NEW_ARRAY_EXPR:
7858 /* Patch all the dimensions */
7860 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
7862 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
7863 tree dim = java_complete_tree (TREE_VALUE (cn));
7864 if (dim == error_mark_node)
7871 TREE_VALUE (cn) = dim;
7872 /* Setup the location of the current dimension, for
7873 later error report. */
7875 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
7876 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
7879 /* They complete the array creation expression, if no errors
7881 CAN_COMPLETE_NORMALLY (node) = 1;
7882 return (flag ? error_mark_node : patch_newarray (node));
7884 case NEW_CLASS_EXPR:
7886 /* Complete function's argument(s) first */
7887 if (complete_function_arguments (node))
7888 return error_mark_node;
7891 tree decl, wfl = TREE_OPERAND (node, 0);
7892 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
7894 node = patch_method_invocation (node, NULL_TREE,
7895 NULL_TREE, 0, &decl);
7896 if (node == error_mark_node)
7897 return error_mark_node;
7899 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
7900 /* If we call this(...), register signature and positions */
7902 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
7903 tree_cons (wfl, decl,
7904 DECL_CONSTRUCTOR_CALLS (current_function_decl));
7905 CAN_COMPLETE_NORMALLY (node) = 1;
7906 return force_evaluation_order (node);
7910 /* Save potential wfls */
7911 wfl_op1 = TREE_OPERAND (node, 0);
7912 wfl_op2 = TREE_OPERAND (node, 1);
7913 TREE_OPERAND (node, 0) = java_complete_lhs (wfl_op1);
7914 if (TREE_OPERAND (node, 0) == error_mark_node)
7915 return error_mark_node;
7917 if (COMPOUND_ASSIGN_P (wfl_op2))
7921 java_complete_tree (TREE_OPERAND (wfl_op2, 0));
7923 /* Hand stablize the lhs on both places */
7924 lvalue = stabilize_reference (other);
7925 TREE_OPERAND (node, 0) = lvalue;
7926 TREE_OPERAND (TREE_OPERAND (node, 1), 0) = lvalue;
7929 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
7930 function to complete this RHS */
7931 if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT)
7932 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
7933 TREE_OPERAND (node, 1));
7935 nn = java_complete_tree (TREE_OPERAND (node, 1));
7937 /* There are cases where the type of RHS is fixed. In those
7938 cases, if the evaluation of the RHS fails, we further the
7939 evaluation of the assignment to detect more errors. */
7940 if (nn == error_mark_node)
7942 /* It's hopeless, but we can further things on to discover
7943 an error during the assignment. In any cases, the
7944 assignment operation fails. */
7945 if (TREE_CODE (TREE_OPERAND (node, 1)) != EXPR_WITH_FILE_LOCATION
7946 && TREE_CODE (TREE_OPERAND (node, 1)) != NEW_ARRAY_INIT
7947 && TREE_TYPE (TREE_OPERAND (node, 1)) != error_mark_node)
7948 patch_assignment (node, wfl_op1, wfl_op2);
7950 /* Now, we still mark the lhs as initialized */
7951 if (JDECL_P (TREE_OPERAND (node, 0)))
7952 INITIALIZED_P (TREE_OPERAND (node, 0)) = 1;
7954 return error_mark_node;
7956 TREE_OPERAND (node, 1) = nn;
7958 /* In case we're handling = with a String as a RHS, we need to
7959 produce a String out of the RHS (it might still be a
7960 STRING_CST or a StringBuffer at this stage */
7961 if ((nn = patch_string (TREE_OPERAND (node, 1))))
7962 TREE_OPERAND (node, 1) = nn;
7963 node = patch_assignment (node, wfl_op1, wfl_op2);
7964 CAN_COMPLETE_NORMALLY (node) = 1;
7966 /* Before returning the node, in the context of a static field
7967 assignment in <clinit>, we may want to carray further
7968 optimizations. (VAR_DECL means it's a static field. See
7970 if (DECL_NAME (current_function_decl) == clinit_identifier_node
7971 && MODIFY_EXPR_FROM_INITIALIZATION_P (node)
7972 && TREE_CODE (TREE_OPERAND (node, 0)) == VAR_DECL)
7973 node = patch_initialized_static_field (node);
7986 case TRUNC_MOD_EXPR:
7988 case TRUTH_ANDIF_EXPR:
7989 case TRUTH_ORIF_EXPR:
7996 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
7997 knows how to handle those cases. */
7998 wfl_op1 = TREE_OPERAND (node, 0);
7999 wfl_op2 = TREE_OPERAND (node, 1);
8001 CAN_COMPLETE_NORMALLY (node) = 1;
8002 /* Don't complete string nodes if dealing with the PLUS operand. */
8003 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
8005 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
8006 if (TREE_OPERAND (node, 0) == error_mark_node)
8007 return error_mark_node;
8009 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
8011 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
8012 if (TREE_OPERAND (node, 1) == error_mark_node)
8013 return error_mark_node;
8015 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
8017 case INSTANCEOF_EXPR:
8018 wfl_op1 = TREE_OPERAND (node, 0);
8019 COMPLETE_CHECK_OP_0 (node);
8020 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
8022 case UNARY_PLUS_EXPR:
8024 case TRUTH_NOT_EXPR:
8026 case PREDECREMENT_EXPR:
8027 case PREINCREMENT_EXPR:
8028 case POSTDECREMENT_EXPR:
8029 case POSTINCREMENT_EXPR:
8031 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
8032 how to handle those cases. */
8033 wfl_op1 = TREE_OPERAND (node, 0);
8034 CAN_COMPLETE_NORMALLY (node) = 1;
8035 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
8036 if (TREE_OPERAND (node, 0) == error_mark_node)
8037 return error_mark_node;
8038 node = patch_unaryop (node, wfl_op1);
8039 CAN_COMPLETE_NORMALLY (node) = 1;
8043 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
8044 how to handle those cases. */
8045 wfl_op1 = TREE_OPERAND (node, 0);
8046 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
8047 if (TREE_OPERAND (node, 0) == error_mark_node)
8048 return error_mark_node;
8049 if (!flag_emit_class_files)
8050 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
8051 /* The same applies to wfl_op2 */
8052 wfl_op2 = TREE_OPERAND (node, 1);
8053 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
8054 if (TREE_OPERAND (node, 1) == error_mark_node)
8055 return error_mark_node;
8056 if (!flag_emit_class_files)
8057 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
8058 return patch_array_ref (node);
8064 /* The first step in the re-write of qualified name handling. FIXME.
8065 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
8066 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
8067 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
8069 tree name = TREE_OPERAND (node, 1);
8070 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
8071 if (field == NULL_TREE)
8073 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
8074 return error_mark_node;
8076 if (! FIELD_STATIC (field))
8078 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
8079 return error_mark_node;
8084 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
8088 /* Can't use THIS in a static environment */
8091 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8092 parse_error_context (wfl_operator, "Keyword `this' used outside "
8094 TREE_TYPE (node) = error_mark_node;
8095 return error_mark_node;
8097 if (ctxp->explicit_constructor_p)
8099 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8101 (wfl_operator, "Can't reference `this' or `super' before the "
8102 "superclass constructor has been called");
8103 TREE_TYPE (node) = error_mark_node;
8104 return error_mark_node;
8106 return current_this;
8109 CAN_COMPLETE_NORMALLY (node) = 1;
8110 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
8111 and it's time to turn it into the appropriate String object
8113 if ((node = patch_string (node)))
8115 fatal ("No case for tree code `%s' - java_complete_tree\n",
8116 tree_code_name [TREE_CODE (node)]);
8121 /* Complete function call's argument. Return a non zero value is an
8125 complete_function_arguments (node)
8131 ctxp->explicit_constructor_p += (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
8132 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
8134 tree wfl = TREE_VALUE (cn), parm, temp;
8135 parm = java_complete_tree (wfl);
8136 if (parm == error_mark_node)
8141 /* If have a string literal that we haven't transformed yet or a
8142 crafted string buffer, as a result of use of the the String
8143 `+' operator. Build `parm.toString()' and expand it. */
8144 if ((temp = patch_string (parm)))
8146 /* Inline PRIMTYPE.TYPE read access */
8147 parm = maybe_build_primttype_type_ref (parm, wfl);
8149 TREE_VALUE (cn) = parm;
8151 ctxp->explicit_constructor_p -= (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
8155 /* Sometimes (for loops and variable initialized during their
8156 declaration), we want to wrap a statement around a WFL and turn it
8160 build_debugable_stmt (location, stmt)
8164 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
8166 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
8167 EXPR_WFL_LINECOL (stmt) = location;
8169 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
8174 build_expr_block (body, decls)
8177 tree node = make_node (BLOCK);
8178 BLOCK_EXPR_DECLS (node) = decls;
8179 BLOCK_EXPR_BODY (node) = body;
8181 TREE_TYPE (node) = TREE_TYPE (body);
8182 TREE_SIDE_EFFECTS (node) = 1;
8186 /* Create a new function block and link it approriately to current
8187 function block chain */
8192 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
8195 /* Link block B supercontext to the previous block. The current
8196 function DECL is used as supercontext when enter_a_block is called
8197 for the first time for a given function. The current function body
8198 (DECL_FUNCTION_BODY) is set to be block B. */
8204 tree fndecl = current_function_decl;
8206 if (!DECL_FUNCTION_BODY (fndecl))
8208 BLOCK_SUPERCONTEXT (b) = fndecl;
8209 DECL_FUNCTION_BODY (fndecl) = b;
8213 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
8214 DECL_FUNCTION_BODY (fndecl) = b;
8219 /* Exit a block by changing the current function body
8220 (DECL_FUNCTION_BODY) to the current block super context, only if
8221 the block being exited isn't the method's top level one. */
8226 tree b = DECL_FUNCTION_BODY (current_function_decl);
8228 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
8229 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
8234 /* Lookup for NAME in the nested function's blocks, all the way up to
8235 the current toplevel one. It complies with Java's local variable
8239 lookup_name_in_blocks (name)
8242 tree b = DECL_FUNCTION_BODY (current_function_decl);
8244 while (b != current_function_decl)
8248 /* Paranoid sanity check. To be removed */
8249 if (TREE_CODE (b) != BLOCK)
8250 fatal ("non block expr function body - lookup_name_in_blocks");
8252 for (current = BLOCK_EXPR_DECLS (b); current;
8253 current = TREE_CHAIN (current))
8254 if (DECL_NAME (current) == name)
8256 b = BLOCK_SUPERCONTEXT (b);
8262 maybe_absorb_scoping_blocks ()
8264 while (BLOCK_EXPR_ORIGIN (DECL_FUNCTION_BODY (current_function_decl)))
8266 tree b = exit_block ();
8267 java_method_add_stmt (current_function_decl, b);
8268 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
8273 /* This section of the source is reserved to build_* functions that
8274 are building incomplete tree nodes and the patch_* functions that
8275 are completing them. */
8277 /* Build a super() constructor invocation. Returns empty_stmt_node if
8278 we're currently dealing with the class java.lang.Object. */
8281 build_super_invocation ()
8283 if (current_class == object_type_node)
8284 return empty_stmt_node;
8287 tree super_wfl = build_wfl_node (super_identifier_node,
8288 input_filename, 0, 0);
8289 return build_method_invocation (super_wfl, NULL_TREE);
8293 /* Build a SUPER/THIS qualified method invocation. */
8296 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
8304 build_wfl_node ((use_this ? this_identifier_node : super_identifier_node),
8305 input_filename, 0, 0);
8306 EXPR_WFL_LINECOL (wfl) = lloc;
8307 invok = build_method_invocation (name, args);
8308 return make_qualified_primary (wfl, invok, rloc);
8311 /* Build an incomplete CALL_EXPR node. */
8314 build_method_invocation (name, args)
8318 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
8319 TREE_SIDE_EFFECTS (call) = 1;
8320 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8324 /* Build an incomplete new xxx(...) node. */
8327 build_new_invocation (name, args)
8330 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
8331 TREE_SIDE_EFFECTS (call) = 1;
8332 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8336 /* Build an incomplete assignment expression. */
8339 build_assignment (op, op_location, lhs, rhs)
8340 int op, op_location;
8344 /* Build the corresponding binop if we deal with a Compound
8345 Assignment operator. Mark the binop sub-tree as part of a
8346 Compound Assignment expression */
8347 if (op != ASSIGN_TK)
8349 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
8350 COMPOUND_ASSIGN_P (rhs) = 1;
8352 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
8353 TREE_SIDE_EFFECTS (assignment) = 1;
8354 EXPR_WFL_LINECOL (assignment) = op_location;
8358 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
8361 print_int_node (node)
8364 static char buffer [80];
8365 if (TREE_CONSTANT_OVERFLOW (node))
8366 sprintf (buffer, "<overflow>");
8368 if (TREE_INT_CST_HIGH (node) == 0)
8369 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
8370 TREE_INT_CST_LOW (node));
8371 else if (TREE_INT_CST_HIGH (node) == -1
8372 && TREE_INT_CST_LOW (node) != 0)
8375 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
8376 -TREE_INT_CST_LOW (node));
8379 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
8380 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
8385 /* Return 1 if you an assignment of a FINAL is attempted */
8388 check_final_assignment (lvalue, wfl)
8391 if (JDECL_P (lvalue) && FIELD_FINAL (lvalue) &&
8392 DECL_NAME (current_function_decl) != clinit_identifier_node)
8395 (wfl, "Can't assign a value to the final variable `%s'",
8396 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
8402 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
8403 read. This is needed to avoid circularities in the implementation
8404 of these fields in libjava. */
8407 maybe_build_primttype_type_ref (rhs, wfl)
8410 tree to_return = NULL_TREE;
8411 tree rhs_type = TREE_TYPE (rhs);
8412 if (TREE_CODE (rhs) == COMPOUND_EXPR)
8414 tree n = TREE_OPERAND (rhs, 1);
8415 if (TREE_CODE (n) == VAR_DECL
8416 && DECL_NAME (n) == TYPE_identifier_node
8417 && rhs_type == class_ptr_type)
8419 char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
8420 if (!strncmp (self_name, "java.lang.", 10))
8421 to_return = build_primtype_type_ref (self_name);
8424 return (to_return ? to_return : rhs );
8427 /* 15.25 Assignment operators. */
8430 patch_assignment (node, wfl_op1, wfl_op2)
8435 tree rhs = TREE_OPERAND (node, 1);
8436 tree lvalue = TREE_OPERAND (node, 0), llvalue;
8437 tree lhs_type, rhs_type, new_rhs = NULL_TREE;
8438 int error_found = 0;
8439 int lvalue_from_array = 0;
8441 /* Can't assign to a final. */
8442 if (check_final_assignment (lvalue, wfl_op1))
8445 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8447 /* Lhs can be a named variable */
8448 if (JDECL_P (lvalue))
8450 INITIALIZED_P (lvalue) = 1;
8451 lhs_type = TREE_TYPE (lvalue);
8453 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
8454 comment on reason why */
8455 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
8457 lhs_type = TREE_TYPE (lvalue);
8458 lvalue_from_array = 1;
8460 /* Or a field access */
8461 else if (TREE_CODE (lvalue) == COMPONENT_REF)
8462 lhs_type = TREE_TYPE (lvalue);
8463 /* Or a function return slot */
8464 else if (TREE_CODE (lvalue) == RESULT_DECL)
8465 lhs_type = TREE_TYPE (lvalue);
8466 /* Otherwise, we might want to try to write into an optimized static
8467 final, this is an of a different nature, reported further on. */
8468 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
8469 && resolve_expression_name (wfl_op1, &llvalue)
8470 && check_final_assignment (llvalue, wfl_op1))
8473 /* What we should do instead is resetting the all the flags
8474 previously set, exchange lvalue for llvalue and continue. */
8475 return error_mark_node;
8479 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
8483 rhs_type = TREE_TYPE (rhs);
8484 /* 5.1 Try the assignment conversion for builtin type. */
8485 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
8487 /* 5.2 If it failed, try a reference conversion */
8488 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
8489 lhs_type = promote_type (rhs_type);
8491 /* 15.25.2 If we have a compound assignment, convert RHS into the
8493 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8494 new_rhs = convert (lhs_type, rhs);
8496 /* Explicit cast required. This is an error */
8499 char *t1 = strdup (lang_printable_name (TREE_TYPE (rhs), 0));
8500 char *t2 = strdup (lang_printable_name (lhs_type, 0));
8502 char operation [32]; /* Max size known */
8504 /* If the assignment is part of a declaration, we use the WFL of
8505 the declared variable to point out the error and call it a
8506 declaration problem. If the assignment is a genuine =
8507 operator, we call is a operator `=' problem, otherwise we
8508 call it an assignment problem. In both of these last cases,
8509 we use the WFL of the operator to indicate the error. */
8511 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
8514 strcpy (operation, "declaration");
8519 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8520 strcpy (operation, "assignment");
8521 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
8522 strcpy (operation, "`return'");
8524 strcpy (operation, "`='");
8528 (wfl, (!valid_cast_to_p (rhs_type, lhs_type) ?
8529 "Incompatible type for %s. Can't convert `%s' to `%s'" :
8530 "Incompatible type for %s. Explicit cast "
8531 "needed to convert `%s' to `%s'"), operation, t1, t2);
8532 free (t1); free (t2);
8536 /* Inline read access to java.lang.PRIMTYPE.TYPE */
8538 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
8541 return error_mark_node;
8543 /* If we built a compound expression as the result of a reference
8544 assignment into an array element, return it here. */
8545 if (TREE_CODE (node) == COMPOUND_EXPR)
8548 TREE_OPERAND (node, 0) = lvalue;
8549 TREE_OPERAND (node, 1) = new_rhs;
8550 TREE_TYPE (node) = lhs_type;
8554 /* Optimize static (final) field initialized upon declaration.
8555 - If the field is static final and is assigned to a primitive
8556 constant type, then set its DECL_INITIAL to the value.
8560 patch_initialized_static_field (node)
8563 tree field = TREE_OPERAND (node, 0);
8564 tree value = TREE_OPERAND (node, 1);
8566 if (DECL_INITIAL (field) != NULL_TREE)
8568 tree type = TREE_TYPE (value);
8569 if (FIELD_FINAL (field) && TREE_CONSTANT (value)
8570 && (JPRIMITIVE_TYPE_P (type)
8571 || (flag_emit_class_files
8572 && TREE_CODE (type) == POINTER_TYPE
8573 && TREE_TYPE (type) == string_type_node)))
8575 DECL_INITIAL (field) = value;
8576 return empty_stmt_node;
8578 DECL_INITIAL (field) = NULL_TREE;
8583 /* Check that type SOURCE can be cast into type DEST. If the cast
8584 can't occur at all, return 0 otherwise 1. This function is used to
8585 produce accurate error messages on the reasons why an assignment
8589 try_reference_assignconv (lhs_type, rhs)
8592 tree new_rhs = NULL_TREE;
8593 tree rhs_type = TREE_TYPE (rhs);
8595 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
8597 /* `null' may be assigned to any reference type */
8598 if (rhs == null_pointer_node)
8599 new_rhs = null_pointer_node;
8600 /* Try the reference assignment conversion */
8601 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
8603 /* This is a magic assignment that we process differently */
8604 else if (rhs == soft_exceptioninfo_call_node)
8610 /* Check that RHS can be converted into LHS_TYPE by the assignment
8611 conversion (5.2), for the cases of RHS being a builtin type. Return
8612 NULL_TREE if the conversion fails or if because RHS isn't of a
8613 builtin type. Return a converted RHS if the conversion is possible. */
8616 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
8617 tree wfl_op1, lhs_type, rhs;
8619 tree new_rhs = NULL_TREE;
8620 tree rhs_type = TREE_TYPE (rhs);
8622 /* Zero accepted everywhere */
8623 if (TREE_CODE (rhs) == INTEGER_CST
8624 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
8625 && JPRIMITIVE_TYPE_P (rhs_type))
8626 new_rhs = convert (lhs_type, rhs);
8628 /* 5.1.1 Try Identity Conversion,
8629 5.1.2 Try Widening Primitive Conversion */
8630 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
8631 new_rhs = convert (lhs_type, rhs);
8633 /* Try a narrowing primitive conversion (5.1.3):
8634 - expression is a constant expression of type int AND
8635 - variable is byte, short or char AND
8636 - The value of the expression is representable in the type of the
8638 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
8639 && (lhs_type == byte_type_node || lhs_type == char_type_node
8640 || lhs_type == short_type_node))
8642 if (int_fits_type_p (rhs, lhs_type))
8643 new_rhs = convert (lhs_type, rhs);
8644 else if (wfl_op1) /* Might be called with a NULL */
8645 parse_warning_context
8646 (wfl_op1, "Constant expression `%s' to wide for narrowing "
8647 "primitive conversion to `%s'",
8648 print_int_node (rhs), lang_printable_name (lhs_type, 0));
8649 /* Reported a warning that will turn into an error further
8650 down, so we don't return */
8656 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
8657 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
8658 0 is the conversion test fails. This implements parts the method
8659 invocation convertion (5.3). */
8662 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
8663 tree lhs_type, rhs_type;
8665 /* 5.1.1: This is the identity conversion part. */
8666 if (lhs_type == rhs_type)
8669 /* Reject non primitive types */
8670 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
8673 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
8674 than a char can't be converted into a char. Short can't too, but
8675 the < test below takes care of that */
8676 if (lhs_type == char_type_node && rhs_type == byte_type_node)
8679 /* Accept all promoted type here. Note, we can't use <= in the test
8680 below, because we still need to bounce out assignments of short
8681 to char and the likes */
8682 if (lhs_type == int_type_node
8683 && (rhs_type == promoted_byte_type_node
8684 || rhs_type == promoted_short_type_node
8685 || rhs_type == promoted_char_type_node
8686 || rhs_type == promoted_boolean_type_node))
8689 /* From here, an integral is widened if its precision is smaller
8690 than the precision of the LHS or if the LHS is a floating point
8691 type, or the RHS is a float and the RHS a double. */
8692 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
8693 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
8694 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
8695 || (rhs_type == float_type_node && lhs_type == double_type_node))
8701 /* Check that something of SOURCE type can be assigned or cast to
8702 something of DEST type at runtime. Return 1 if the operation is
8703 valid, 0 otherwise. If CAST is set to 1, we're treating the case
8704 were SOURCE is cast into DEST, which borrows a lot of the
8705 assignment check. */
8708 valid_ref_assignconv_cast_p (source, dest, cast)
8713 /* SOURCE or DEST might be null if not from a declared entity. */
8714 if (!source || !dest)
8716 if (JNULLP_TYPE_P (source))
8718 if (TREE_CODE (source) == POINTER_TYPE)
8719 source = TREE_TYPE (source);
8720 if (TREE_CODE (dest) == POINTER_TYPE)
8721 dest = TREE_TYPE (dest);
8722 /* Case where SOURCE is a class type */
8723 if (TYPE_CLASS_P (source))
8725 if (TYPE_CLASS_P (dest))
8726 return source == dest || inherits_from_p (source, dest)
8727 || (cast && inherits_from_p (dest, source));
8728 if (TYPE_INTERFACE_P (dest))
8730 /* If doing a cast and SOURCE is final, the operation is
8731 always correct a compile time (because even if SOURCE
8732 does not implement DEST, a subclass of SOURCE might). */
8733 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
8735 /* Otherwise, SOURCE must implement DEST */
8736 return interface_of_p (dest, source);
8738 /* DEST is an array, cast permited if SOURCE is of Object type */
8739 return (cast && source == object_type_node ? 1 : 0);
8741 if (TYPE_INTERFACE_P (source))
8743 if (TYPE_CLASS_P (dest))
8745 /* If not casting, DEST must be the Object type */
8747 return dest == object_type_node;
8748 /* We're doing a cast. The cast is always valid is class
8749 DEST is not final, otherwise, DEST must implement SOURCE */
8750 else if (!CLASS_FINAL (TYPE_NAME (dest)))
8753 return interface_of_p (source, dest);
8755 if (TYPE_INTERFACE_P (dest))
8757 /* If doing a cast, then if SOURCE and DEST contain method
8758 with the same signature but different return type, then
8759 this is a (compile time) error */
8762 tree method_source, method_dest;
8766 for (method_source = TYPE_METHODS (source); method_source;
8767 method_source = TREE_CHAIN (method_source))
8770 build_java_argument_signature (TREE_TYPE (method_source));
8771 source_type = TREE_TYPE (TREE_TYPE (method_source));
8772 source_name = DECL_NAME (method_source);
8773 for (method_dest = TYPE_METHODS (dest);
8774 method_dest; method_dest = TREE_CHAIN (method_dest))
8776 build_java_argument_signature (TREE_TYPE (method_dest))
8777 && source_name == DECL_NAME (method_dest)
8778 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
8784 return source == dest || interface_of_p (dest, source);
8789 if (TYPE_ARRAY_P (source))
8791 if (TYPE_CLASS_P (dest))
8792 return dest == object_type_node;
8793 /* Can't cast an array to an interface unless the interface is
8794 java.lang.Cloneable */
8795 if (TYPE_INTERFACE_P (dest))
8796 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
8799 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
8800 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
8802 /* In case of severe errors, they turn out null */
8803 if (!dest_element_type || !source_element_type)
8805 if (source_element_type == dest_element_type)
8807 return valid_ref_assignconv_cast_p (source_element_type,
8808 dest_element_type, cast);
8816 valid_cast_to_p (source, dest)
8820 if (TREE_CODE (source) == POINTER_TYPE)
8821 source = TREE_TYPE (source);
8822 if (TREE_CODE (dest) == POINTER_TYPE)
8823 dest = TREE_TYPE (dest);
8825 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
8826 return valid_ref_assignconv_cast_p (source, dest, 1);
8828 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
8834 /* Method invocation conversion test. Return 1 if type SOURCE can be
8835 converted to type DEST through the methond invocation conversion
8839 do_unary_numeric_promotion (arg)
8842 tree type = TREE_TYPE (arg);
8843 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
8844 : TREE_CODE (type) == CHAR_TYPE)
8845 arg = convert (int_type_node, arg);
8849 /* Return a non zero value if SOURCE can be converted into DEST using
8850 the method invocation conversion rule (5.3). */
8852 valid_method_invocation_conversion_p (dest, source)
8855 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
8856 && valid_builtin_assignconv_identity_widening_p (dest, source))
8857 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
8858 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
8859 && valid_ref_assignconv_cast_p (source, dest, 0)));
8862 /* Build an incomplete binop expression. */
8865 build_binop (op, op_location, op1, op2)
8870 tree binop = build (op, NULL_TREE, op1, op2);
8871 TREE_SIDE_EFFECTS (binop) = 1;
8872 /* Store the location of the operator, for better error report. The
8873 string of the operator will be rebuild based on the OP value. */
8874 EXPR_WFL_LINECOL (binop) = op_location;
8878 /* Build the string of the operator retained by NODE. If NODE is part
8879 of a compound expression, add an '=' at the end of the string. This
8880 function is called when an error needs to be reported on an
8881 operator. The string is returned as a pointer to a static character
8885 operator_string (node)
8888 #define BUILD_OPERATOR_STRING(S) \
8890 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
8894 static char buffer [10];
8895 switch (TREE_CODE (node))
8897 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
8898 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
8899 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
8900 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8901 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
8902 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
8903 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
8904 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
8905 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
8906 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
8907 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
8908 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
8909 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
8910 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
8911 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
8912 case GT_EXPR: BUILD_OPERATOR_STRING (">");
8913 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
8914 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
8915 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
8916 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8917 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
8918 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
8919 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
8920 case PREINCREMENT_EXPR: /* Fall through */
8921 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
8922 case PREDECREMENT_EXPR: /* Fall through */
8923 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
8925 fatal ("unregistered operator %s - operator_string",
8926 tree_code_name [TREE_CODE (node)]);
8929 #undef BUILD_OPERATOR_STRING
8932 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
8933 errors but we modify NODE so that it contains the type computed
8934 according to the expression, when it's fixed. Otherwise, we write
8935 error_mark_node as the type. It allows us to further the analysis
8936 of remaining nodes and detects more errors in certain cases. */
8939 patch_binop (node, wfl_op1, wfl_op2)
8944 tree op1 = TREE_OPERAND (node, 0);
8945 tree op2 = TREE_OPERAND (node, 1);
8946 tree op1_type = TREE_TYPE (op1);
8947 tree op2_type = TREE_TYPE (op2);
8949 int code = TREE_CODE (node);
8951 /* If 1, tell the routine that we have to return error_mark_node
8952 after checking for the initialization of the RHS */
8953 int error_found = 0;
8955 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8959 /* 15.16 Multiplicative operators */
8960 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
8961 case RDIV_EXPR: /* 15.16.2 Division Operator / */
8962 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
8963 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
8965 if (!JPRIMITIVE_TYPE_P (op1_type))
8966 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
8967 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
8968 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
8969 TREE_TYPE (node) = error_mark_node;
8973 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8974 /* Change the division operator if necessary */
8975 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
8976 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
8978 /* This one is more complicated. FLOATs are processed by a
8979 function call to soft_fmod. Duplicate the value of the
8980 COMPOUND_ASSIGN_P flag. */
8981 if (code == TRUNC_MOD_EXPR)
8983 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
8984 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
8985 TREE_SIDE_EFFECTS (mod)
8986 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
8991 /* 15.17 Additive Operators */
8992 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
8994 /* Operation is valid if either one argument is a string
8995 constant, a String object or a StringBuffer crafted for the
8996 purpose of the a previous usage of the String concatenation
8999 if (TREE_CODE (op1) == STRING_CST
9000 || TREE_CODE (op2) == STRING_CST
9001 || JSTRING_TYPE_P (op1_type)
9002 || JSTRING_TYPE_P (op2_type)
9003 || IS_CRAFTED_STRING_BUFFER_P (op1)
9004 || IS_CRAFTED_STRING_BUFFER_P (op2))
9005 return build_string_concatenation (op1, op2);
9007 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
9009 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
9011 if (!JPRIMITIVE_TYPE_P (op1_type))
9012 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
9013 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
9014 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
9015 TREE_TYPE (node) = error_mark_node;
9019 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9022 /* 15.18 Shift Operators */
9026 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
9028 if (!JINTEGRAL_TYPE_P (op1_type))
9029 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
9032 (wfl_operator, (JPRIMITIVE_TYPE_P (op2_type) ?
9033 "Incompatible type for `%s'. Explicit cast needed to convert "
9034 "shift distance from `%s' to integral" :
9035 "Incompatible type for `%s'. Can't convert shift distance from "
9036 "`%s' to integral"),
9037 operator_string (node), lang_printable_name (op2_type, 0));
9038 TREE_TYPE (node) = error_mark_node;
9043 /* Unary numeric promotion (5.6.1) is performed on each operand
9045 op1 = do_unary_numeric_promotion (op1);
9046 op2 = do_unary_numeric_promotion (op2);
9048 /* The type of the shift expression is the type of the promoted
9049 type of the left-hand operand */
9050 prom_type = TREE_TYPE (op1);
9052 /* Shift int only up to 0x1f and long up to 0x3f */
9053 if (prom_type == int_type_node)
9054 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
9055 build_int_2 (0x1f, 0)));
9057 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
9058 build_int_2 (0x3f, 0)));
9060 /* The >>> operator is a >> operating on unsigned quantities */
9061 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
9064 tree utype = unsigned_type (prom_type);
9065 op1 = convert (utype, op1);
9066 TREE_SET_CODE (node, RSHIFT_EXPR);
9067 TREE_OPERAND (node, 0) = op1;
9068 TREE_OPERAND (node, 1) = op2;
9069 TREE_TYPE (node) = utype;
9070 to_return = convert (prom_type, node);
9071 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
9072 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
9073 TREE_SIDE_EFFECTS (to_return)
9074 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
9079 /* 15.19.1 Type Comparison Operator instaceof */
9080 case INSTANCEOF_EXPR:
9082 TREE_TYPE (node) = boolean_type_node;
9084 if (!(op2_type = resolve_type_during_patch (op2)))
9085 return error_mark_node;
9087 /* The first operand must be a reference type or the null type */
9088 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
9089 error_found = 1; /* Error reported further below */
9091 /* The second operand must be a reference type */
9092 if (!JREFERENCE_TYPE_P (op2_type))
9094 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
9096 (wfl_operator, "Invalid argument `%s' for `instanceof'",
9097 lang_printable_name (op2_type, 0));
9101 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
9103 /* If the first operand is null, the result is always false */
9104 if (op1 == null_pointer_node)
9105 return boolean_false_node;
9106 else if (flag_emit_class_files)
9108 TREE_OPERAND (node, 1) = op2_type;
9109 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
9112 /* Otherwise we have to invoke instance of to figure it out */
9116 build (CALL_EXPR, boolean_type_node,
9117 build_address_of (soft_instanceof_node),
9120 build_tree_list (NULL_TREE,
9121 build_class_ref (op2_type))),
9123 TREE_SIDE_EFFECTS (call) = TREE_SIDE_EFFECTS (op1);
9127 /* There is no way the expression operand can be an instance of
9128 the type operand. This is a compile time error. */
9131 char *t1 = strdup (lang_printable_name (op1_type, 0));
9132 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
9134 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
9135 t1, lang_printable_name (op2_type, 0));
9142 /* 15.21 Bitwise and Logical Operators */
9146 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
9147 /* Binary numeric promotion is performed on both operand and the
9148 expression retain that type */
9149 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9151 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
9152 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
9153 /* The type of the bitwise operator expression is BOOLEAN */
9154 prom_type = boolean_type_node;
9157 if (!JINTEGRAL_TYPE_P (op1_type))
9158 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
9159 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
9160 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
9161 TREE_TYPE (node) = error_mark_node;
9163 /* Insert a break here if adding thing before the switch's
9164 break for this case */
9168 /* 15.22 Conditional-And Operator */
9169 case TRUTH_ANDIF_EXPR:
9170 /* 15.23 Conditional-Or Operator */
9171 case TRUTH_ORIF_EXPR:
9172 /* Operands must be of BOOLEAN type */
9173 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
9174 TREE_CODE (op2_type) != BOOLEAN_TYPE)
9176 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
9177 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
9178 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
9179 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
9180 TREE_TYPE (node) = boolean_type_node;
9184 /* The type of the conditional operators is BOOLEAN */
9185 prom_type = boolean_type_node;
9188 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
9193 /* The type of each of the operands must be a primitive numeric
9195 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
9197 if (!JNUMERIC_TYPE_P (op1_type))
9198 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
9199 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
9200 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
9201 TREE_TYPE (node) = boolean_type_node;
9205 /* Binary numeric promotion is performed on the operands */
9206 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9207 /* The type of the relation expression is always BOOLEAN */
9208 prom_type = boolean_type_node;
9211 /* 15.20 Equality Operator */
9214 /* 15.20.1 Numerical Equality Operators == and != */
9215 /* Binary numeric promotion is performed on the operands */
9216 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
9217 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9219 /* 15.20.2 Boolean Equality Operators == and != */
9220 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
9221 TREE_CODE (op2_type) == BOOLEAN_TYPE)
9222 ; /* Nothing to do here */
9224 /* 15.20.3 Reference Equality Operators == and != */
9225 /* Types have to be either references or the null type. If
9226 they're references, it must be possible to convert either
9227 type to the other by casting conversion. */
9228 else if (op1 == null_pointer_node || op2 == null_pointer_node
9229 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
9230 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
9231 || valid_ref_assignconv_cast_p (op2_type,
9233 ; /* Nothing to do here */
9235 /* Else we have an error figure what can't be converted into
9236 what and report the error */
9240 t1 = strdup (lang_printable_name (op1_type, 0));
9242 (wfl_operator, "Incompatible type for `%s'. Can't convert `%s' "
9243 "to `%s'", operator_string (node), t1,
9244 lang_printable_name (op2_type, 0));
9246 TREE_TYPE (node) = boolean_type_node;
9250 prom_type = boolean_type_node;
9255 return error_mark_node;
9257 TREE_OPERAND (node, 0) = op1;
9258 TREE_OPERAND (node, 1) = op2;
9259 TREE_TYPE (node) = prom_type;
9260 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
9265 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
9266 zero value, the value of CSTE comes after the valude of STRING */
9269 do_merge_string_cste (cste, string, string_len, after)
9272 int string_len, after;
9274 int len = TREE_STRING_LENGTH (cste) + string_len;
9275 char *old = TREE_STRING_POINTER (cste);
9276 TREE_STRING_LENGTH (cste) = len;
9277 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
9280 strcpy (TREE_STRING_POINTER (cste), string);
9281 strcat (TREE_STRING_POINTER (cste), old);
9285 strcpy (TREE_STRING_POINTER (cste), old);
9286 strcat (TREE_STRING_POINTER (cste), string);
9291 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
9292 new STRING_CST on success, NULL_TREE on failure */
9295 merge_string_cste (op1, op2, after)
9299 /* Handle two string constants right away */
9300 if (TREE_CODE (op2) == STRING_CST)
9301 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
9302 TREE_STRING_LENGTH (op2), after);
9304 /* Reasonable integer constant can be treated right away */
9305 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
9307 static char *boolean_true = "true";
9308 static char *boolean_false = "false";
9309 static char *null_pointer = "null";
9313 if (op2 == boolean_true_node)
9314 string = boolean_true;
9315 else if (op2 == boolean_false_node)
9316 string = boolean_false;
9317 else if (op2 == null_pointer_node)
9318 string = null_pointer;
9319 else if (TREE_TYPE (op2) == char_type_node)
9321 ch[0] = (char )TREE_INT_CST_LOW (op2);
9326 string = print_int_node (op2);
9328 return do_merge_string_cste (op1, string, strlen (string), after);
9333 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
9334 has to be a STRING_CST and the other part must be a STRING_CST or a
9335 INTEGRAL constant. Return a new STRING_CST if the operation
9336 succeed, NULL_TREE otherwise.
9338 If the case we want to optimize for space, we might want to return
9339 NULL_TREE for each invocation of this routine. FIXME */
9342 string_constant_concatenation (op1, op2)
9345 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
9350 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
9351 rest = (string == op1 ? op2 : op1);
9352 invert = (string == op1 ? 0 : 1 );
9354 /* Walk REST, only if it looks reasonable */
9355 if (TREE_CODE (rest) != STRING_CST
9356 && !IS_CRAFTED_STRING_BUFFER_P (rest)
9357 && !JSTRING_TYPE_P (TREE_TYPE (rest))
9358 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
9360 rest = java_complete_tree (rest);
9361 if (rest == error_mark_node)
9362 return error_mark_node;
9365 return merge_string_cste (string, rest, invert);
9370 /* Implement the `+' operator. Does static optimization if possible,
9371 otherwise create (if necessary) and append elements to a
9372 StringBuffer. The StringBuffer will be carried around until it is
9373 used for a function call or an assignment. Then toString() will be
9374 called on it to turn it into a String object. */
9377 build_string_concatenation (op1, op2)
9381 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
9384 /* Try to do some static optimization */
9385 if ((result = string_constant_concatenation (op1, op2)))
9388 /* Discard null constants on either sides of the expression */
9389 if (TREE_CODE (op1) == STRING_CST && !TREE_STRING_LENGTH (op1))
9394 else if (TREE_CODE (op2) == STRING_CST && !TREE_STRING_LENGTH (op2))
9397 /* If operands are string constant, turn then into object references */
9398 if (TREE_CODE (op1) == STRING_CST)
9399 op1 = patch_string_cst (op1);
9400 if (op2 && TREE_CODE (op2) == STRING_CST)
9401 op2 = patch_string_cst (op2);
9403 /* If either one of the constant is null and the other non null
9404 operand is a String object, return it. */
9405 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
9408 /* If OP1 isn't already a StringBuffer, create and
9409 initialize a new one */
9410 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
9412 /* Two solutions here:
9413 1) OP1 is a string reference, we call new StringBuffer(OP1)
9414 2) OP1 is something else, we call new StringBuffer().append(OP1). */
9415 if (JSTRING_TYPE_P (TREE_TYPE (op1)))
9416 op1 = BUILD_STRING_BUFFER (op1);
9419 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
9420 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
9426 /* OP1 is no longer the last node holding a crafted StringBuffer */
9427 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
9428 /* Create a node for `{new...,xxx}.append (op2)' */
9430 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
9433 /* Mark the last node holding a crafted StringBuffer */
9434 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
9436 TREE_SIDE_EFFECTS (op1) = side_effects;
9440 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
9441 StringBuffer. If no string were found to be patched, return
9448 if (node == error_mark_node)
9449 return error_mark_node;
9450 if (TREE_CODE (node) == STRING_CST)
9451 return patch_string_cst (node);
9452 else if (IS_CRAFTED_STRING_BUFFER_P (node))
9454 int saved = ctxp->explicit_constructor_p;
9455 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
9457 /* Temporary disable forbid the use of `this'. */
9458 ctxp->explicit_constructor_p = 0;
9459 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
9460 /* Restore it at its previous value */
9461 ctxp->explicit_constructor_p = saved;
9467 /* Build the internal representation of a string constant. */
9470 patch_string_cst (node)
9474 if (! flag_emit_class_files)
9476 push_obstacks (&permanent_obstack, &permanent_obstack);
9477 node = get_identifier (TREE_STRING_POINTER (node));
9478 location = alloc_name_constant (CONSTANT_String, node);
9479 node = build_ref_from_constant_pool (location);
9481 TREE_TYPE (node) = promote_type (string_type_node);
9482 TREE_CONSTANT (node) = 1;
9486 /* Build an incomplete unary operator expression. */
9489 build_unaryop (op_token, op_location, op1)
9490 int op_token, op_location;
9497 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
9498 case MINUS_TK: op = NEGATE_EXPR; break;
9499 case NEG_TK: op = TRUTH_NOT_EXPR; break;
9500 case NOT_TK: op = BIT_NOT_EXPR; break;
9501 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
9505 unaryop = build1 (op, NULL_TREE, op1);
9506 TREE_SIDE_EFFECTS (unaryop) = 1;
9507 /* Store the location of the operator, for better error report. The
9508 string of the operator will be rebuild based on the OP value. */
9509 EXPR_WFL_LINECOL (unaryop) = op_location;
9513 /* Special case for the ++/-- operators, since they require an extra
9514 argument to build, which is set to NULL and patched
9515 later. IS_POST_P is 1 if the operator, 0 otherwise. */
9518 build_incdec (op_token, op_location, op1, is_post_p)
9519 int op_token, op_location;
9523 static enum tree_code lookup [2][2] =
9525 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
9526 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
9528 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
9529 NULL_TREE, op1, NULL_TREE);
9530 TREE_SIDE_EFFECTS (node) = 1;
9531 /* Store the location of the operator, for better error report. The
9532 string of the operator will be rebuild based on the OP value. */
9533 EXPR_WFL_LINECOL (node) = op_location;
9537 /* Build an incomplete cast operator, based on the use of the
9538 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
9539 set. java_complete_tree is trained to walk a CONVERT_EXPR even
9540 though its type is already set. */
9543 build_cast (location, type, exp)
9547 tree node = build1 (CONVERT_EXPR, type, exp);
9548 EXPR_WFL_LINECOL (node) = location;
9552 /* 15.14 Unary operators. We return error_mark_node in case of error,
9553 but preserve the type of NODE if the type is fixed. */
9556 patch_unaryop (node, wfl_op)
9560 tree op = TREE_OPERAND (node, 0);
9561 tree op_type = TREE_TYPE (op);
9562 tree prom_type, value, decl;
9563 int code = TREE_CODE (node);
9564 int error_found = 0;
9566 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9570 /* 15.13.2 Postfix Increment Operator ++ */
9571 case POSTINCREMENT_EXPR:
9572 /* 15.13.3 Postfix Increment Operator -- */
9573 case POSTDECREMENT_EXPR:
9574 /* 15.14.1 Prefix Increment Operator ++ */
9575 case PREINCREMENT_EXPR:
9576 /* 15.14.2 Prefix Decrement Operator -- */
9577 case PREDECREMENT_EXPR:
9578 decl = strip_out_static_field_access_decl (op);
9580 && !((TREE_CODE (decl) == INDIRECT_REF
9581 || TREE_CODE (decl) == COMPONENT_REF)
9582 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))))
9585 /* Before screaming, check that we're not in fact trying to
9586 increment a optimized static final access, in which case
9587 we issue an different error message. */
9588 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
9589 && resolve_expression_name (wfl_op, &lvalue)
9590 && check_final_assignment (lvalue, wfl_op)))
9591 parse_error_context (wfl_operator, "Invalid argument to `%s'",
9592 operator_string (node));
9593 TREE_TYPE (node) = error_mark_node;
9596 else if (check_final_assignment (op, wfl_op))
9599 /* From now on, we know that op if a variable and that it has a
9600 valid wfl. We use wfl_op to locate errors related to the
9602 else if (!JNUMERIC_TYPE_P (op_type))
9605 (wfl_op, "Invalid argument type `%s' to `%s'",
9606 lang_printable_name (op_type, 0), operator_string (node));
9607 TREE_TYPE (node) = error_mark_node;
9612 /* Before the addition, binary numeric promotion is performed on
9614 value = build_int_2 (1, 0);
9616 binary_numeric_promotion (op_type, TREE_TYPE (value), &op, &value);
9617 /* And write the promoted incremented and increment */
9618 TREE_OPERAND (node, 0) = op;
9619 TREE_OPERAND (node, 1) = value;
9620 /* Convert the overall back into its original type. */
9621 return fold (convert (op_type, node));
9625 /* 15.14.3 Unary Plus Operator + */
9626 case UNARY_PLUS_EXPR:
9627 /* 15.14.4 Unary Minus Operator - */
9629 if (!JNUMERIC_TYPE_P (op_type))
9631 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
9632 TREE_TYPE (node) = error_mark_node;
9635 /* Unary numeric promotion is performed on operand */
9638 op = do_unary_numeric_promotion (op);
9639 prom_type = TREE_TYPE (op);
9640 if (code == UNARY_PLUS_EXPR)
9645 /* 15.14.5 Bitwise Complement Operator ~ */
9647 if (!JINTEGRAL_TYPE_P (op_type))
9649 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
9650 TREE_TYPE (node) = error_mark_node;
9655 op = do_unary_numeric_promotion (op);
9656 prom_type = TREE_TYPE (op);
9660 /* 15.14.6 Logical Complement Operator ! */
9661 case TRUTH_NOT_EXPR:
9662 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
9664 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
9665 /* But the type is known. We will report an error if further
9666 attempt of a assignment is made with this rhs */
9667 TREE_TYPE (node) = boolean_type_node;
9671 prom_type = boolean_type_node;
9674 /* 15.15 Cast Expression */
9676 value = patch_cast (node, wfl_operator);
9677 if (value == error_mark_node)
9679 /* If this cast is part of an assignment, we tell the code
9680 that deals with it not to complain about a mismatch,
9681 because things have been cast, anyways */
9682 TREE_TYPE (node) = error_mark_node;
9687 value = fold (value);
9688 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
9695 return error_mark_node;
9697 /* There are cases where node has been replaced by something else
9698 and we don't end up returning here: UNARY_PLUS_EXPR,
9699 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
9700 TREE_OPERAND (node, 0) = fold (op);
9701 TREE_TYPE (node) = prom_type;
9702 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
9706 /* Generic type resolution that sometimes takes place during node
9707 patching. Returned the resolved type or generate an error
9708 message. Return the resolved type or NULL_TREE. */
9711 resolve_type_during_patch (type)
9714 if (unresolved_type_p (type, NULL))
9716 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
9719 parse_error_context (type,
9720 "Class `%s' not found in type declaration",
9721 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
9726 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
9727 return TREE_TYPE (type_decl);
9732 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
9733 found. Otherwise NODE or something meant to replace it is returned. */
9736 patch_cast (node, wfl_operator)
9740 tree op = TREE_OPERAND (node, 0);
9741 tree op_type = TREE_TYPE (op);
9742 tree cast_type = TREE_TYPE (node);
9745 /* First resolve OP_TYPE if unresolved */
9746 if (!(cast_type = resolve_type_during_patch (cast_type)))
9747 return error_mark_node;
9749 /* Check on cast that are proven correct at compile time */
9750 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
9752 static tree convert_narrow ();
9754 if (cast_type == op_type)
9757 /* float and double type are converted to the original type main
9758 variant and then to the target type. */
9759 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
9760 op = convert (integer_type_node, op);
9762 /* Try widening/narowwing convertion. Potentially, things need
9763 to be worked out in gcc so we implement the extreme cases
9764 correctly. fold_convert() needs to be fixed. */
9765 return convert (cast_type, op);
9768 /* It's also valid to cast a boolean into a boolean */
9769 if (op_type == boolean_type_node && cast_type == boolean_type_node)
9772 /* null can be casted to references */
9773 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
9774 return build_null_of_type (cast_type);
9776 /* The remaining legal casts involve conversion between reference
9777 types. Check for their compile time correctness. */
9778 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
9779 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
9781 TREE_TYPE (node) = promote_type (cast_type);
9782 /* Now, the case can be determined correct at compile time if
9783 OP_TYPE can be converted into CAST_TYPE by assignment
9786 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
9788 TREE_SET_CODE (node, NOP_EXPR);
9792 if (flag_emit_class_files)
9794 TREE_SET_CODE (node, CONVERT_EXPR);
9798 /* The cast requires a run-time check */
9799 return build (CALL_EXPR, promote_type (cast_type),
9800 build_address_of (soft_checkcast_node),
9801 tree_cons (NULL_TREE, build_class_ref (cast_type),
9802 build_tree_list (NULL_TREE, op)),
9806 /* Any other casts are proven incorrect at compile time */
9807 t1 = strdup (lang_printable_name (op_type, 0));
9808 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
9809 t1, lang_printable_name (cast_type, 0));
9811 return error_mark_node;
9814 /* Build a null constant and give it the type TYPE. */
9817 build_null_of_type (type)
9820 tree node = build_int_2 (0, 0);
9821 TREE_TYPE (node) = promote_type (type);
9825 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
9826 a list of indices. */
9828 build_array_ref (location, array, index)
9832 tree node = build (ARRAY_REF, NULL_TREE, array, index);
9833 EXPR_WFL_LINECOL (node) = location;
9837 /* 15.12 Array Access Expression */
9840 patch_array_ref (node)
9843 tree array = TREE_OPERAND (node, 0);
9844 tree array_type = TREE_TYPE (array);
9845 tree index = TREE_OPERAND (node, 1);
9846 tree index_type = TREE_TYPE (index);
9847 int error_found = 0;
9849 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9851 if (TREE_CODE (array_type) == POINTER_TYPE)
9852 array_type = TREE_TYPE (array_type);
9854 /* The array reference must be an array */
9855 if (!TYPE_ARRAY_P (array_type))
9858 (wfl_operator, "`[]' can only be applied to arrays. It can't be "
9859 "applied to `%s'", lang_printable_name (array_type, 0));
9860 TREE_TYPE (node) = error_mark_node;
9864 /* The array index underdoes unary numeric promotion. The promoted
9866 index = do_unary_numeric_promotion (index);
9867 if (TREE_TYPE (index) != int_type_node)
9869 int could_cast = valid_cast_to_p (index_type, int_type_node);
9872 (could_cast ? "Incompatible type for `[]'. Explicit cast needed to "
9873 "convert `%s' to `int'" : "Incompatible type for `[]'. "
9874 "Can't convert `%s' to `int'"),
9875 lang_printable_name (index_type, 0));
9876 TREE_TYPE (node) = error_mark_node;
9881 return error_mark_node;
9883 array_type = TYPE_ARRAY_ELEMENT (array_type);
9885 if (flag_emit_class_files)
9887 TREE_OPERAND (node, 0) = array;
9888 TREE_OPERAND (node, 1) = index;
9892 /* The save_expr is for correct evaluation order. It would be cleaner
9893 to use force_evaluation_order (see comment there), but that is
9894 difficult when we also have to deal with bounds checking. */
9895 if (TREE_SIDE_EFFECTS (index))
9896 array = save_expr (array);
9897 node = build_java_arrayaccess (array, array_type, index);
9898 if (TREE_SIDE_EFFECTS (index))
9899 node = build (COMPOUND_EXPR, array_type, array, node);
9901 TREE_TYPE (node) = array_type;
9905 /* 15.9 Array Creation Expressions */
9908 build_newarray_node (type, dims, extra_dims)
9914 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
9915 build_int_2 (extra_dims, 0));
9920 patch_newarray (node)
9923 tree type = TREE_OPERAND (node, 0);
9924 tree dims = TREE_OPERAND (node, 1);
9925 tree cdim, array_type;
9926 int error_found = 0;
9928 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
9930 /* Dimension types are verified. It's better for the types to be
9931 verified in order. */
9932 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
9935 tree dim = TREE_VALUE (cdim);
9937 /* Dim might have been saved during its evaluation */
9938 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
9940 /* The type of each specified dimension must be an integral type. */
9941 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
9944 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
9945 promoted type must be int. */
9948 dim = do_unary_numeric_promotion (dim);
9949 if (TREE_TYPE (dim) != int_type_node)
9953 /* Report errors on types here */
9957 (TREE_PURPOSE (cdim),
9958 "Incompatible type for dimension in array creation expression. "
9959 "%s convert `%s' to `int'",
9960 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
9961 "Explicit cast needed to" : "Can't"),
9962 lang_printable_name (TREE_TYPE (dim), 0));
9966 TREE_PURPOSE (cdim) = NULL_TREE;
9969 /* Resolve array base type if unresolved */
9970 if (!(type = resolve_type_during_patch (type)))
9975 /* We don't want further evaluation of this bogus array creation
9977 TREE_TYPE (node) = error_mark_node;
9978 return error_mark_node;
9981 /* Set array_type to the actual (promoted) array type of the result. */
9982 if (TREE_CODE (type) == RECORD_TYPE)
9983 type = build_pointer_type (type);
9984 while (--xdims >= 0)
9986 type = promote_type (build_java_array_type (type, -1));
9988 dims = nreverse (dims);
9990 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
9993 array_type = build_java_array_type (type,
9994 TREE_CODE (cdim) == INTEGER_CST ?
9995 TREE_INT_CST_LOW (cdim) : -1);
9996 array_type = promote_type (array_type);
9998 dims = nreverse (dims);
10000 /* The node is transformed into a function call. Things are done
10001 differently according to the number of dimensions. If the number
10002 of dimension is equal to 1, then the nature of the base type
10003 (primitive or not) matters. */
10005 return build_new_array (type, TREE_VALUE (dims));
10007 /* Can't reuse what's already written in expr.c because it uses the
10008 JVM stack representation. Provide a build_multianewarray. FIXME */
10009 return build (CALL_EXPR, array_type,
10010 build_address_of (soft_multianewarray_node),
10011 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
10012 tree_cons (NULL_TREE,
10013 build_int_2 (ndims, 0), dims )),
10017 /* 10.6 Array initializer. */
10019 /* Build a wfl for array element that don't have one, so we can
10020 pin-point errors. */
10023 maybe_build_array_element_wfl (node)
10026 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
10027 return build_expr_wfl (NULL_TREE, ctxp->filename,
10028 ctxp->elc.line, ctxp->elc.prev_col);
10033 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
10034 identification of initialized arrays easier to detect during walk
10038 build_new_array_init (location, values)
10042 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
10043 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
10044 EXPR_WFL_LINECOL (to_return) = location;
10048 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
10049 occurred. Otherwise return NODE after having set its type
10053 patch_new_array_init (type, node)
10056 int error_seen = 0;
10057 tree current, element_type;
10058 HOST_WIDE_INT length;
10059 int all_constant = 1;
10060 tree init = TREE_OPERAND (node, 0);
10062 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
10064 parse_error_context (node,
10065 "Invalid array initializer for non-array type `%s'",
10066 lang_printable_name (type, 1));
10067 return error_mark_node;
10069 type = TREE_TYPE (type);
10070 element_type = TYPE_ARRAY_ELEMENT (type);
10072 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
10074 for (length = 0, current = CONSTRUCTOR_ELTS (init);
10075 current; length++, current = TREE_CHAIN (current))
10077 tree elt = TREE_VALUE (current);
10078 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
10080 error_seen |= array_constructor_check_entry (element_type, current);
10081 elt = TREE_VALUE (current);
10082 /* When compiling to native code, STRING_CST is converted to
10083 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
10084 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
10089 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
10090 TREE_PURPOSE (current) = NULL_TREE;
10093 if (elt && TREE_VALUE (elt) == error_mark_node)
10098 return error_mark_node;
10100 /* Create a new type. We can't reuse the one we have here by
10101 patching its dimension because it originally is of dimension -1
10102 hence reused by gcc. This would prevent triangular arrays. */
10103 type = build_java_array_type (element_type, length);
10104 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
10105 TREE_TYPE (node) = promote_type (type);
10106 TREE_CONSTANT (init) = all_constant;
10107 TREE_CONSTANT (node) = all_constant;
10111 /* Verify that one entry of the initializer element list can be
10112 assigned to the array base type. Report 1 if an error occurred, 0
10116 array_constructor_check_entry (type, entry)
10119 char *array_type_string = NULL; /* For error reports */
10120 tree value, type_value, new_value, wfl_value, patched;
10121 int error_seen = 0;
10123 new_value = NULL_TREE;
10124 wfl_value = TREE_VALUE (entry);
10126 value = java_complete_tree (TREE_VALUE (entry));
10127 /* patch_string return error_mark_node if arg is error_mark_node */
10128 if ((patched = patch_string (value)))
10130 if (value == error_mark_node)
10133 type_value = TREE_TYPE (value);
10135 /* At anytime, try_builtin_assignconv can report a warning on
10136 constant overflow during narrowing. */
10137 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
10138 new_value = try_builtin_assignconv (wfl_operator, type, value);
10139 if (!new_value && (new_value = try_reference_assignconv (type, value)))
10140 type_value = promote_type (type);
10142 /* Check and report errors */
10145 char *msg = (!valid_cast_to_p (type_value, type) ?
10146 "Can't" : "Explicit cast needed to");
10147 if (!array_type_string)
10148 array_type_string = strdup (lang_printable_name (type, 1));
10149 parse_error_context
10150 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
10151 msg, lang_printable_name (type_value, 1), array_type_string);
10157 new_value = maybe_build_primttype_type_ref (new_value, wfl_operator);
10158 TREE_VALUE (entry) = new_value;
10161 if (array_type_string)
10162 free (array_type_string);
10164 TREE_PURPOSE (entry) = NULL_TREE;
10169 build_this (location)
10172 tree node = build_wfl_node (this_identifier_node, input_filename, 0, 0);
10173 TREE_SET_CODE (node, THIS_EXPR);
10174 EXPR_WFL_LINECOL (node) = location;
10178 /* 14.15 The return statement. It builds a modify expression that
10179 assigns the returned value to the RESULT_DECL that hold the value
10183 build_return (location, op)
10187 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
10188 EXPR_WFL_LINECOL (node) = location;
10189 node = build_debugable_stmt (location, node);
10194 patch_return (node)
10197 tree return_exp = TREE_OPERAND (node, 0);
10198 tree meth = current_function_decl;
10199 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
10200 int error_found = 0;
10202 TREE_TYPE (node) = error_mark_node;
10203 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10205 /* It's invalid to have a return value within a function that is
10206 declared with the keyword void or that is a constructor */
10207 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
10210 /* It's invalid to have a no return value within a function that
10211 isn't declared with the keyword `void' */
10212 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
10217 if (!DECL_CONSTRUCTOR_P (meth))
10219 char *t = strdup (lang_printable_name (mtype, 0));
10220 parse_error_context (wfl_operator,
10221 "`return' with%s value from `%s %s'",
10222 (error_found == 1 ? "" : "out"),
10223 t, lang_printable_name (meth, 0));
10227 parse_error_context (wfl_operator,
10228 "`return' with value from constructor `%s'",
10229 lang_printable_name (meth, 0));
10230 return error_mark_node;
10233 /* If we have a return_exp, build a modify expression and expand
10234 it. Note: at that point, the assignment is declared valid, but we
10235 may want to carry some more hacks */
10238 tree exp = java_complete_tree (return_exp);
10239 tree modify, patched;
10241 /* If the function returned value and EXP are booleans, EXP has
10242 to be converted into the type of DECL_RESULT, which is integer
10243 (see complete_start_java_method) */
10244 if (TREE_TYPE (exp) == boolean_type_node &&
10245 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
10246 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
10248 /* `null' can be assigned to a function returning a reference */
10249 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
10250 exp == null_pointer_node)
10251 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
10253 if ((patched = patch_string (exp)))
10256 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
10257 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
10258 modify = java_complete_tree (modify);
10260 if (modify != error_mark_node)
10262 TREE_SIDE_EFFECTS (modify) = 1;
10263 TREE_OPERAND (node, 0) = modify;
10266 return error_mark_node;
10268 TREE_TYPE (node) = void_type_node;
10269 TREE_SIDE_EFFECTS (node) = 1;
10273 /* 14.8 The if Statement */
10276 build_if_else_statement (location, expression, if_body, else_body)
10278 tree expression, if_body, else_body;
10282 else_body = empty_stmt_node;
10283 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
10284 EXPR_WFL_LINECOL (node) = location;
10285 node = build_debugable_stmt (location, node);
10290 patch_if_else_statement (node)
10293 tree expression = TREE_OPERAND (node, 0);
10295 TREE_TYPE (node) = error_mark_node;
10296 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10298 /* The type of expression must be boolean */
10299 if (TREE_TYPE (expression) != boolean_type_node
10300 && TREE_TYPE (expression) != promoted_boolean_type_node)
10302 parse_error_context
10304 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
10305 lang_printable_name (TREE_TYPE (expression), 0));
10306 return error_mark_node;
10309 TREE_TYPE (node) = void_type_node;
10310 TREE_SIDE_EFFECTS (node) = 1;
10311 CAN_COMPLETE_NORMALLY (node)
10312 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10313 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
10317 /* 14.6 Labeled Statements */
10319 /* Action taken when a lableled statement is parsed. a new
10320 LABELED_BLOCK_EXPR is created. No statement is attached to the
10324 build_labeled_block (location, label)
10328 tree label_name = merge_qualified_name (label_id, label);
10329 tree label_decl, node;
10331 /* Issue an error if we try to reuse a label that was previously
10333 if (IDENTIFIER_LOCAL_VALUE (label_name))
10335 EXPR_WFL_LINECOL (wfl_operator) = location;
10336 parse_error_context (wfl_operator, "Declaration of `%s' shadows "
10337 "a previous label declaration",
10338 IDENTIFIER_POINTER (label));
10339 EXPR_WFL_LINECOL (wfl_operator) =
10340 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
10341 parse_error_context (wfl_operator, "This is the location of the "
10342 "previous declaration of label `%s'",
10343 IDENTIFIER_POINTER (label));
10344 java_error_count--;
10347 label_decl = create_label_decl (label_name);
10348 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
10349 EXPR_WFL_LINECOL (node) = location;
10350 TREE_SIDE_EFFECTS (node) = 1;
10354 /* Generate a label crafting a unique name for it. This is used to
10355 implicitely label loops that aren't the body part of labeled
10359 generate_labeled_block ()
10361 return build_labeled_block (0, generate_name ());
10364 /* A labeled statement LBE is attached a statement. */
10367 complete_labeled_statement (lbe, statement)
10368 tree lbe; /* Labeled block expr */
10371 /* In anyways, tie the loop to its statement */
10372 LABELED_BLOCK_BODY (lbe) = statement;
10374 /* Ok, if statement is a for loop, we have to attach the labeled
10375 statement to the block the for loop belongs to and return the
10377 if (TREE_CODE (statement) == LOOP_EXPR && IS_FOR_LOOP_P (statement))
10379 java_method_add_stmt (current_function_decl, lbe);
10380 return exit_block ();
10386 /* 14.10, 14.11, 14.12 Loop Statements */
10388 /* Create an empty LOOP_EXPR and make it the last in the nested loop
10392 build_new_loop (loop_body)
10395 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
10396 TREE_SIDE_EFFECTS (loop) = 1;
10401 /* Create a loop body according to the following structure:
10403 COMPOUND_EXPR (loop main body)
10404 EXIT_EXPR (this order is for while/for loops.
10405 LABELED_BLOCK_EXPR the order is reversed for do loops)
10406 LABEL_DECL (a continue occuring here branches at the
10407 BODY end of this labeled block)
10410 REVERSED, if non zero, tells that the loop condition expr comes
10411 after the body, like in the do-while loop.
10413 To obtain a loop, the loop body structure described above is
10414 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
10417 LABEL_DECL (use this label to exit the loop)
10419 <structure described above> */
10422 build_loop_body (location, condition, reversed)
10427 tree first, second, body;
10429 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
10430 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
10431 condition = build_debugable_stmt (location, condition);
10432 TREE_SIDE_EFFECTS (condition) = 1;
10434 body = generate_labeled_block ();
10435 first = (reversed ? body : condition);
10436 second = (reversed ? condition : body);
10438 build (COMPOUND_EXPR, NULL_TREE,
10439 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
10442 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
10443 their order) on the current loop. Unlink the current loop from the
10447 complete_loop_body (location, condition, body, reversed)
10449 tree condition, body;
10452 tree to_return = ctxp->current_loop;
10453 tree loop_body = LOOP_EXPR_BODY (to_return);
10456 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
10457 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
10458 The real EXIT_EXPR is one operand further. */
10459 EXPR_WFL_LINECOL (cnode) = location;
10460 /* This one is for accurate error reports */
10461 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
10462 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
10464 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
10469 /* Tailored version of complete_loop_body for FOR loops, when FOR
10470 loops feature the condition part */
10473 complete_for_loop (location, condition, update, body)
10475 tree condition, update, body;
10477 /* Put the condition and the loop body in place */
10478 tree loop = complete_loop_body (location, condition, body, 0);
10479 /* LOOP is the current loop which has been now popped of the loop
10480 stack. Install the update block */
10481 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
10485 /* If the loop isn't surrounded by a labeled statement, create one and
10486 insert LOOP as it's body. */
10489 patch_loop_statement (loop)
10492 tree loop_label, to_return_as_loop;
10494 if (LOOP_HAS_LABEL_P (loop))
10496 loop_label = ctxp->current_labeled_block;
10497 to_return_as_loop = loop;
10501 loop_label = generate_labeled_block ();
10502 LABELED_BLOCK_BODY (loop_label) = loop;
10503 PUSH_LABELED_BLOCK (loop_label);
10504 to_return_as_loop = loop_label;
10506 TREE_TYPE (to_return_as_loop) = void_type_node;
10507 return to_return_as_loop;
10510 /* 14.13, 14.14: break and continue Statements */
10512 /* Build a break or a continue statement. a null NAME indicates an
10513 unlabeled break/continue statement. */
10516 build_bc_statement (location, is_break, name)
10517 int location, is_break;
10520 tree break_continue, label_block_expr = NULL_TREE;
10524 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
10525 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
10526 /* Null means that we don't have a target for this named
10527 break/continue. In this case, we make the target to be the
10528 label name, so that the error can be reported accuratly in
10529 patch_bc_statement. */
10530 label_block_expr = EXPR_WFL_NODE (name);
10532 /* Unlabeled break/continue will be handled during the
10533 break/continue patch operation */
10535 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
10537 IS_BREAK_STMT_P (break_continue) = is_break;
10538 TREE_SIDE_EFFECTS (break_continue) = 1;
10539 EXPR_WFL_LINECOL (break_continue) = location;
10540 break_continue = build_debugable_stmt (location, break_continue);
10541 return break_continue;
10544 /* Verification of a break/continue statement. */
10547 patch_bc_statement (node)
10550 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
10551 int is_unlabeled = 0;
10552 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10554 /* Not having a target means that the break/continue statement is
10555 unlabeled. We try to find a decent label for it */
10559 /* There should be a loop/switch to branch to */
10560 if (ctxp->current_loop)
10562 if (TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10564 /* At that stage, we're in the loop body, which is
10565 encapsulated around a LABELED_BLOCK_EXPR. So searching
10566 the current loop label requires us to consider the
10567 labeled block before the current one. */
10568 if (!LOOP_HAS_LABEL_SKIP_P (ctxp->current_loop))
10569 fatal ("unlabeled loop has no installed label -- "
10570 "patch_bc_statement");
10571 bc_label = TREE_CHAIN (ctxp->current_labeled_block);
10573 /* For a SWITCH statement, this is the current one */
10575 bc_label = ctxp->current_labeled_block;
10577 /* Not having a loop to break/continue to is an error */
10580 parse_error_context (wfl_operator, "`%s' must be in loop%s",
10581 (IS_BREAK_STMT_P (node) ? "break" : "continue"),
10582 (IS_BREAK_STMT_P (node) ? " or switch" : ""));
10583 return error_mark_node;
10586 /* Having an identifier here means that the target is unknown. */
10587 else if (TREE_CODE (bc_label) == IDENTIFIER_NODE)
10589 parse_error_context (wfl_operator, "No label definition found for `%s'",
10590 IDENTIFIER_POINTER (bc_label));
10591 return error_mark_node;
10594 /* Find the statement we're targeting. */
10595 target_stmt = LABELED_BLOCK_BODY (bc_label);
10597 /* Target loop is slightly burrowed in the case of a for loop, it
10598 appears at the first sight to be a block. */
10599 if (TREE_CODE (target_stmt) == BLOCK)
10601 tree sub = BLOCK_SUBBLOCKS (target_stmt);
10602 if (sub && TREE_CODE (sub) == COMPOUND_EXPR && TREE_OPERAND (sub, 1)
10603 && TREE_CODE (TREE_OPERAND (sub, 1)) == LOOP_EXPR)
10604 target_stmt = TREE_OPERAND (sub, 1);
10607 /* 14.13 The break Statement */
10608 if (IS_BREAK_STMT_P (node))
10610 /* Named break are always fine, as far as they have a target
10611 (already verified). Anonymous break need to target
10612 while/do/for/switch */
10613 if (is_unlabeled &&
10614 !(TREE_CODE (target_stmt) == LOOP_EXPR /* do/while/for */
10615 || TREE_CODE (target_stmt) == SWITCH_EXPR)) /* switch */
10617 parse_error_context (wfl_operator,
10618 "`break' must be in loop or switch");
10619 return error_mark_node;
10621 /* If previously unlabeled, install the new found label */
10623 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
10625 /* 14.14 The continue Statement */
10626 /* The continue statement must always target a loop, unnamed or not. */
10629 if (TREE_CODE (target_stmt) != LOOP_EXPR) /* do/while/for */
10631 parse_error_context (wfl_operator, "`continue' must be in loop");
10632 return error_mark_node;
10634 /* Everything looks good. We can fix the `continue' jump to go
10635 at the place in the loop were the continue is. For unlabeled
10636 continue, the continuation point is the current labeled
10637 block, by construction. */
10639 EXIT_BLOCK_LABELED_BLOCK (node) =
10640 bc_label = ctxp->current_labeled_block;
10643 CAN_COMPLETE_NORMALLY (bc_label) = 1;
10645 /* Our break/continue don't return values. */
10646 TREE_TYPE (node) = void_type_node;
10647 /* Encapsulate the break within a compound statement so that it's
10648 expanded all the times by expand_expr (and not clobered
10649 sometimes, like after a if statement) */
10650 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
10651 TREE_SIDE_EFFECTS (node) = 1;
10655 /* Process the exit expression belonging to a loop. Its type must be
10659 patch_exit_expr (node)
10662 tree expression = TREE_OPERAND (node, 0);
10663 TREE_TYPE (node) = error_mark_node;
10664 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10666 /* The type of expression must be boolean */
10667 if (TREE_TYPE (expression) != boolean_type_node)
10669 parse_error_context
10671 "Incompatible type for loop conditional. Can't convert `%s' to "
10673 lang_printable_name (TREE_TYPE (expression), 0));
10674 return error_mark_node;
10676 /* Now we know things are allright, invert the condition, fold and
10678 TREE_OPERAND (node, 0) =
10679 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
10681 if (! integer_zerop (TREE_OPERAND (node, 0))
10682 && ctxp->current_loop != NULL_TREE
10683 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10684 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
10685 if (! integer_onep (TREE_OPERAND (node, 0)))
10686 CAN_COMPLETE_NORMALLY (node) = 1;
10689 TREE_TYPE (node) = void_type_node;
10693 /* 14.9 Switch statement */
10696 patch_switch_statement (node)
10699 tree se = TREE_OPERAND (node, 0), se_type;
10701 /* Complete the switch expression */
10702 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
10703 se_type = TREE_TYPE (se);
10704 /* The type of the switch expression must be char, byte, short or
10706 if (!JINTEGRAL_TYPE_P (se_type))
10708 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10709 parse_error_context (wfl_operator, "Incompatible type for `switch'. "
10710 "Can't convert `%s' to `int'",
10711 lang_printable_name (se_type, 0));
10712 /* This is what java_complete_tree will check */
10713 TREE_OPERAND (node, 0) = error_mark_node;
10714 return error_mark_node;
10717 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10719 /* Ready to return */
10720 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
10722 TREE_TYPE (node) = error_mark_node;
10723 return error_mark_node;
10725 TREE_TYPE (node) = void_type_node;
10726 TREE_SIDE_EFFECTS (node) = 1;
10727 CAN_COMPLETE_NORMALLY (node)
10728 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10729 || ! SWITCH_HAS_DEFAULT (node);
10733 /* 14.18 The try statement */
10735 /* Wrap BLOCK around a LABELED_BLOCK, set DECL to the newly generated
10736 exit labeld and issue a jump to FINALLY_LABEL:
10740 <orignal_statments>
10747 build_jump_to_finally (block, decl, finally_label, type)
10748 tree block, decl, finally_label, type;
10751 tree new_block = build (LABELED_BLOCK_EXPR, type,
10752 create_label_decl (generate_name ()), block);
10754 stmt = build (MODIFY_EXPR, void_type_node, decl,
10755 build_address_of (LABELED_BLOCK_LABEL (new_block)));
10756 TREE_SIDE_EFFECTS (stmt) = 1;
10757 CAN_COMPLETE_NORMALLY (stmt) = 1;
10758 add_stmt_to_block (block, type, stmt);
10759 stmt = build (GOTO_EXPR, void_type_node, finally_label);
10760 TREE_SIDE_EFFECTS (stmt) = 1;
10761 add_stmt_to_block (block, type, stmt);
10766 build_try_statement (location, try_block, catches)
10768 tree try_block, catches;
10770 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
10771 EXPR_WFL_LINECOL (node) = location;
10776 build_try_finally_statement (location, try_block, finally)
10778 tree try_block, finally;
10780 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
10781 EXPR_WFL_LINECOL (node) = location;
10786 patch_try_statement (node)
10789 int error_found = 0;
10790 tree try = TREE_OPERAND (node, 0);
10791 /* Exception handlers are considered in left to right order */
10792 tree catch = nreverse (TREE_OPERAND (node, 1));
10793 tree current, caught_type_list = NULL_TREE;
10795 /* Check catch clauses, if any. Every time we find an error, we try
10796 to process the next catch clause. We process the catch clause before
10797 the try block so that when processing the try block we can check thrown
10798 exceptions againts the caught type list. */
10799 for (current = catch; current; current = TREE_CHAIN (current))
10801 tree carg_decl, carg_type;
10802 tree sub_current, catch_block, catch_clause;
10805 /* At this point, the structure of the catch clause is
10806 CATCH_EXPR (catch node)
10807 BLOCK (with the decl of the parameter)
10809 MODIFY_EXPR (assignment of the catch parameter)
10810 BLOCK (catch clause block)
10812 catch_clause = TREE_OPERAND (current, 0);
10813 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
10814 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
10816 /* Catch clauses can't have more than one parameter declared,
10817 but it's already enforced by the grammar. Make sure that the
10818 only parameter of the clause statement in of class Throwable
10819 or a subclass of Throwable, but that was done earlier. The
10820 catch clause parameter type has also been resolved. */
10822 /* Just make sure that the catch clause parameter type inherits
10823 from java.lang.Throwable */
10824 if (!inherits_from_p (carg_type, throwable_type_node))
10826 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10827 parse_error_context (wfl_operator,
10828 "Can't catch class `%s'. Catch clause "
10829 "parameter type must be a subclass of "
10830 "class `java.lang.Throwable'",
10831 lang_printable_name (carg_type, 0));
10836 /* Partial check for unreachable catch statement: The catch
10837 clause is reachable iff is no earlier catch block A in
10838 the try statement such that the type of the catch
10839 clause's parameter is the same as or a subclass of the
10840 type of A's parameter */
10842 for (sub_current = catch;
10843 sub_current != current; sub_current = TREE_CHAIN (sub_current))
10845 tree sub_catch_clause, decl;
10846 sub_catch_clause = TREE_OPERAND (sub_current, 0);
10847 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
10849 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
10851 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10852 parse_error_context
10853 (wfl_operator, "`catch' not reached because of the catch "
10854 "clause at line %d", EXPR_WFL_LINENO (sub_current));
10855 unreachable = error_found = 1;
10859 /* Complete the catch clause block */
10860 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
10861 if (catch_block == error_mark_node)
10866 if (CAN_COMPLETE_NORMALLY (catch_block))
10867 CAN_COMPLETE_NORMALLY (node) = 1;
10868 TREE_OPERAND (current, 0) = catch_block;
10873 /* Things to do here: the exception must be thrown */
10875 /* Link this type to the caught type list */
10876 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
10879 PUSH_EXCEPTIONS (caught_type_list);
10880 if ((try = java_complete_tree (try)) == error_mark_node)
10882 if (CAN_COMPLETE_NORMALLY (try))
10883 CAN_COMPLETE_NORMALLY (node) = 1;
10886 /* Verification ends here */
10888 return error_mark_node;
10890 TREE_OPERAND (node, 0) = try;
10891 TREE_OPERAND (node, 1) = catch;
10892 TREE_TYPE (node) = void_type_node;
10896 /* 14.17 The synchronized Statement */
10899 patch_synchronized_statement (node, wfl_op1)
10900 tree node, wfl_op1;
10902 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
10903 tree block = TREE_OPERAND (node, 1);
10905 tree enter, exit, expr_decl, assignment;
10907 if (expr == error_mark_node)
10909 block = java_complete_tree (block);
10913 /* The TYPE of expr must be a reference type */
10914 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
10916 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10917 parse_error_context (wfl_operator, "Incompatible type for `synchronized'"
10918 ". Can't convert `%s' to `java.lang.Object'",
10919 lang_printable_name (TREE_TYPE (expr), 0));
10920 return error_mark_node;
10923 /* Generate a try-finally for the synchronized statement, except
10924 that the handler that catches all throw exception calls
10925 _Jv_MonitorExit and then rethrow the exception.
10926 The synchronized statement is then implemented as:
10929 _Jv_MonitorEnter (expression)
10931 _Jv_MonitorExit (expression)
10935 e = _Jv_exception_info ();
10936 _Jv_MonitorExit (expression)
10940 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
10941 BUILD_MONITOR_ENTER (enter, expr_decl);
10942 BUILD_MONITOR_EXIT (exit, expr_decl);
10943 CAN_COMPLETE_NORMALLY (enter) = 1;
10944 CAN_COMPLETE_NORMALLY (exit) = 1;
10945 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
10946 TREE_SIDE_EFFECTS (assignment) = 1;
10947 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
10948 build (COMPOUND_EXPR, NULL_TREE,
10949 build (WITH_CLEANUP_EXPR, NULL_TREE,
10950 build (COMPOUND_EXPR, NULL_TREE,
10951 assignment, enter),
10954 node = build_expr_block (node, expr_decl);
10956 return java_complete_tree (node);
10959 /* 14.16 The throw Statement */
10962 patch_throw_statement (node, wfl_op1)
10963 tree node, wfl_op1;
10965 tree expr = TREE_OPERAND (node, 0);
10966 tree type = TREE_TYPE (expr);
10967 int unchecked_ok = 0, tryblock_throws_ok = 0;
10969 /* Thrown expression must be assignable to java.lang.Throwable */
10970 if (!try_reference_assignconv (throwable_type_node, expr))
10972 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10973 parse_error_context (wfl_operator, "Can't throw `%s'; it must be a "
10974 "subclass of class `java.lang.Throwable'",
10975 lang_printable_name (type, 0));
10976 /* If the thrown expression was a reference, we further the
10977 compile-time check. */
10978 if (!JREFERENCE_TYPE_P (type))
10979 return error_mark_node;
10982 /* At least one of the following must be true */
10984 /* The type of the throw expression is a not checked exception,
10985 i.e. is a unchecked expression. */
10986 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
10988 /* Throw is contained in a try statement and at least one catch
10989 clause can receive the thrown expression or the current method is
10990 declared to throw such an exception. Or, the throw statement is
10991 contained in a method or constructor declaration and the type of
10992 the Expression is assignable to at least one type listed in the
10993 throws clause the declaration. */
10994 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10996 tryblock_throws_ok =
10997 check_thrown_exceptions_do (TREE_TYPE (expr));
10998 if (!(unchecked_ok || tryblock_throws_ok))
11000 /* If there is a surrounding try block that has no matching
11001 clatch clause, report it first. A surrounding try block exits
11002 only if there is something after the list of checked
11003 exception thrown by the current function (if any). */
11004 if (IN_TRY_BLOCK_P ())
11005 parse_error_context (wfl_operator, "Checked exception `%s' can't be "
11006 "caught by any of the catch clause(s) "
11007 "of the surrounding `try' block",
11008 lang_printable_name (type, 0));
11009 /* If we have no surrounding try statement and the method doesn't have
11010 any throws, report it now. FIXME */
11011 else if (!EXCEPTIONS_P (currently_caught_type_list)
11012 && !tryblock_throws_ok)
11013 parse_error_context (wfl_operator, "Checked exception `%s' isn't "
11014 "thrown from a `try' block",
11015 lang_printable_name (type, 0));
11016 /* Otherwise, the current method doesn't have the appropriate
11017 throws declaration */
11019 parse_error_context (wfl_operator, "Checked exception `%s' doesn't "
11020 "match any of current method's `throws' "
11022 lang_printable_name (type, 0));
11023 return error_mark_node;
11026 /* If a throw statement is contained in a static initializer, then a
11027 compile-time check ensures that either its value is always an
11028 unchecked exception or its value is always caught by some try
11029 statement that contains it. FIXME, static initializer. */
11031 if (! flag_emit_class_files)
11032 BUILD_THROW (node, expr);
11036 /* Check that exception said to be thrown by method DECL can be
11037 effectively caught from where DECL is invoked. */
11040 check_thrown_exceptions (location, decl)
11045 /* For all the unchecked exceptions thrown by DECL */
11046 for (throws = DECL_FUNCTION_THROWS (decl); throws;
11047 throws = TREE_CHAIN (throws))
11048 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
11051 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
11052 if (DECL_NAME (decl) == get_identifier ("clone"))
11055 EXPR_WFL_LINECOL (wfl_operator) = location;
11056 parse_error_context
11057 (wfl_operator, "Exception `%s' must be caught, or it must be "
11058 "declared in the `throws' clause of `%s'",
11059 lang_printable_name (TREE_VALUE (throws), 0),
11060 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
11064 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
11065 try-catch blocks, OR is listed in the `throws' clause of the
11069 check_thrown_exceptions_do (exception)
11072 tree list = currently_caught_type_list;
11073 resolve_and_layout (exception, NULL_TREE);
11074 /* First, all the nested try-catch-finally at that stage. The
11075 last element contains `throws' clause exceptions, if any. */
11076 if (IS_UNCHECKED_EXCEPTION_P (exception))
11081 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
11082 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
11084 list = TREE_CHAIN (list);
11090 purge_unchecked_exceptions (mdecl)
11093 tree throws = DECL_FUNCTION_THROWS (mdecl);
11094 tree new = NULL_TREE;
11098 tree next = TREE_CHAIN (throws);
11099 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
11101 TREE_CHAIN (throws) = new;
11106 /* List is inverted here, but it doesn't matter */
11107 DECL_FUNCTION_THROWS (mdecl) = new;
11110 /* 15.24 Conditional Operator ?: */
11113 patch_conditional_expr (node, wfl_cond, wfl_op1)
11114 tree node, wfl_cond, wfl_op1;
11116 tree cond = TREE_OPERAND (node, 0);
11117 tree op1 = TREE_OPERAND (node, 1);
11118 tree op2 = TREE_OPERAND (node, 2);
11119 tree resulting_type = NULL_TREE;
11120 tree t1, t2, patched;
11121 int error_found = 0;
11123 /* Operands of ?: might be StringBuffers crafted as a result of a
11124 string concatenation. Obtain a descent operand here. */
11125 if ((patched = patch_string (op1)))
11126 TREE_OPERAND (node, 1) = op1 = patched;
11127 if ((patched = patch_string (op2)))
11128 TREE_OPERAND (node, 2) = op2 = patched;
11130 t1 = TREE_TYPE (op1);
11131 t2 = TREE_TYPE (op2);
11133 /* The first expression must be a boolean */
11134 if (TREE_TYPE (cond) != boolean_type_node)
11136 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
11137 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11138 "convert `%s' to `boolean'",
11139 lang_printable_name (TREE_TYPE (cond), 0));
11143 /* Second and third can be numeric, boolean (i.e. primitive),
11144 references or null. Anything else results in an error */
11145 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
11146 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
11147 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
11148 || (t1 == boolean_type_node && t2 == boolean_type_node)))
11151 /* Determine the type of the conditional expression. Same types are
11152 easy to deal with */
11154 resulting_type = t1;
11156 /* There are different rules for numeric types */
11157 else if (JNUMERIC_TYPE_P (t1))
11159 /* if byte/short found, the resulting type is short */
11160 if ((t1 == byte_type_node && t2 == short_type_node)
11161 || (t1 == short_type_node && t2 == byte_type_node))
11162 resulting_type = short_type_node;
11164 /* If t1 is a constant int and t2 is of type byte, short or char
11165 and t1's value fits in t2, then the resulting type is t2 */
11166 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
11167 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
11168 resulting_type = t2;
11170 /* If t2 is a constant int and t1 is of type byte, short or char
11171 and t2's value fits in t1, then the resulting type is t1 */
11172 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
11173 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
11174 resulting_type = t1;
11176 /* Otherwise, binary numeric promotion is applied and the
11177 resulting type is the promoted type of operand 1 and 2 */
11179 resulting_type = binary_numeric_promotion (t2, t2,
11180 &TREE_OPERAND (node, 1),
11181 &TREE_OPERAND (node, 2));
11184 /* Cases of a reference and a null type */
11185 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
11186 resulting_type = t1;
11188 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
11189 resulting_type = t2;
11191 /* Last case: different reference types. If a type can be converted
11192 into the other one by assignment conversion, the latter
11193 determines the type of the expression */
11194 else if ((resulting_type = try_reference_assignconv (t1, op2)))
11195 resulting_type = promote_type (t1);
11197 else if ((resulting_type = try_reference_assignconv (t2, op1)))
11198 resulting_type = promote_type (t2);
11200 /* If we don't have any resulting type, we're in trouble */
11201 if (!resulting_type)
11203 char *t = strdup (lang_printable_name (t1, 0));
11204 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
11205 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11206 "convert `%s' to `%s'", t,
11207 lang_printable_name (t2, 0));
11214 TREE_TYPE (node) = error_mark_node;
11215 return error_mark_node;
11218 TREE_TYPE (node) = resulting_type;
11219 TREE_SET_CODE (node, COND_EXPR);
11220 CAN_COMPLETE_NORMALLY (node) = 1;
11224 /* Try to constant fold NODE.
11225 If NODE is not a constant expression, return NULL_EXPR.
11226 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
11229 fold_constant_for_init (node, context)
11233 tree op0, op1, val;
11234 enum tree_code code = TREE_CODE (node);
11236 if (code == INTEGER_CST || code == REAL_CST || code == STRING_CST)
11238 if (TREE_TYPE (node) != NULL_TREE)
11246 case TRUNC_MOD_EXPR:
11254 case TRUTH_ANDIF_EXPR:
11255 case TRUTH_ORIF_EXPR:
11262 op0 = TREE_OPERAND (node, 0);
11263 op1 = TREE_OPERAND (node, 1);
11264 val = fold_constant_for_init (op0, context);
11265 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11267 TREE_OPERAND (node, 0) = val;
11268 val = fold_constant_for_init (op1, context);
11269 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11271 TREE_OPERAND (node, 1) = val;
11272 return patch_binop (node, op0, op1);
11274 case UNARY_PLUS_EXPR:
11276 case TRUTH_NOT_EXPR:
11279 op0 = TREE_OPERAND (node, 0);
11280 val = fold_constant_for_init (op0, context);
11281 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11283 TREE_OPERAND (node, 0) = val;
11284 return patch_unaryop (node, op0);
11288 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
11289 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11291 TREE_OPERAND (node, 0) = val;
11292 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
11293 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11295 TREE_OPERAND (node, 1) = val;
11296 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
11297 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11299 TREE_OPERAND (node, 2) = val;
11300 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
11301 : TREE_OPERAND (node, 2);
11304 if (! FIELD_STATIC (node) || ! FIELD_FINAL (node)
11305 || DECL_INITIAL (node) == NULL_TREE)
11307 val = DECL_INITIAL (node);
11308 /* Guard against infinite recursion. */
11309 DECL_INITIAL (node) = NULL_TREE;
11310 val = fold_constant_for_init (val, DECL_CONTEXT (node));
11311 DECL_INITIAL (node) = val;
11314 case EXPR_WITH_FILE_LOCATION:
11315 /* Compare java_complete_tree and resolve_expression_name. */
11316 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11317 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11319 tree name = EXPR_WFL_NODE (node);
11321 if (PRIMARY_P (node))
11323 else if (! QUALIFIED_P (name))
11325 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
11326 if (! FIELD_STATIC (decl))
11328 return fold_constant_for_init (decl, decl);
11333 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
11334 qualify_ambiguous_name (node);
11335 if (resolve_field_access (node, &decl, NULL)
11336 && decl != NULL_TREE)
11337 return fold_constant_for_init (decl, decl);
11344 op0 = TREE_OPERAND (node, 0);
11345 val = fold_constant_for_init (op0, context);
11346 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11348 TREE_OPERAND (node, 0) = val;
11352 #ifdef USE_COMPONENT_REF
11354 case COMPONENT_REF:
11363 #ifdef USE_COMPONENT_REF
11364 /* Context is 'T' for TypeName, 'P' for PackageName,
11365 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
11368 resolve_simple_name (name, context)
11375 resolve_qualified_name (name, context)