]>
Commit | Line | Data |
---|---|---|
e04a16fb AG |
1 | /* Source code parsing and tree node generation for the GNU compiler |
2 | for the Java(TM) language. | |
df32d2ce | 3 | Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
e04a16fb AG |
4 | Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com) |
5 | ||
6 | This file is part of GNU CC. | |
7 | ||
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) | |
11 | any later version. | |
12 | ||
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. | |
17 | ||
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. | |
22 | ||
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. */ | |
26 | ||
27 | /* This file parses java source code and issues a tree node image | |
28 | suitable for code generation (byte code and targeted CPU assembly | |
29 | language). | |
30 | ||
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" | |
34 | ||
35 | The following modifications were brought to the original grammar: | |
36 | ||
37 | method_body: added the rule '| block SC_TK' | |
e04a16fb AG |
38 | static_initializer: added the rule 'static block SC_TK'. |
39 | ||
40 | Note: All the extra rules described above should go away when the | |
41 | empty_statement rule will work. | |
42 | ||
43 | statement_nsi: 'nsi' should be read no_short_if. | |
44 | ||
45 | Some rules have been modified to support JDK1.1 inner classes | |
46 | definitions and other extensions. */ | |
47 | ||
48 | %{ | |
e04a16fb | 49 | #include "config.h" |
36635152 GS |
50 | #include "system.h" |
51 | #include <dirent.h> | |
e04a16fb AG |
52 | #include "tree.h" |
53 | #include "rtl.h" | |
54 | #include "obstack.h" | |
0a2138e2 | 55 | #include "toplev.h" |
e04a16fb AG |
56 | #include "flags.h" |
57 | #include "java-tree.h" | |
58 | #include "jcf.h" | |
59 | #include "lex.h" | |
60 | #include "parse.h" | |
61 | #include "zipfile.h" | |
5e942c50 | 62 | #include "convert.h" |
63a212ed | 63 | #include "buffer.h" |
f099f336 | 64 | #include "xref.h" |
b384405b | 65 | #include "function.h" |
138657ec | 66 | #include "except.h" |
0ae70c6a | 67 | #include "defaults.h" |
e04a16fb | 68 | |
82371d41 | 69 | /* Local function prototypes */ |
df32d2ce KG |
70 | static char *java_accstring_lookup PARAMS ((int)); |
71 | static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree)); | |
72 | static void variable_redefinition_error PARAMS ((tree, tree, tree, int)); | |
73 | static void check_modifiers PARAMS ((const char *, int, int)); | |
74 | static tree create_class PARAMS ((int, tree, tree, tree)); | |
75 | static tree create_interface PARAMS ((int, tree, tree)); | |
76 | static tree find_field PARAMS ((tree, tree)); | |
77 | static tree lookup_field_wrapper PARAMS ((tree, tree)); | |
78 | static int duplicate_declaration_error_p PARAMS ((tree, tree, tree)); | |
79 | static void register_fields PARAMS ((int, tree, tree)); | |
80 | static tree parser_qualified_classname PARAMS ((tree)); | |
81 | static int parser_check_super PARAMS ((tree, tree, tree)); | |
82 | static int parser_check_super_interface PARAMS ((tree, tree, tree)); | |
83 | static void check_modifiers_consistency PARAMS ((int)); | |
84 | static tree lookup_cl PARAMS ((tree)); | |
85 | static tree lookup_java_method2 PARAMS ((tree, tree, int)); | |
86 | static tree method_header PARAMS ((int, tree, tree, tree)); | |
87 | static void fix_method_argument_names PARAMS ((tree ,tree)); | |
88 | static tree method_declarator PARAMS ((tree, tree)); | |
89 | static void parse_warning_context PARAMS ((tree cl, const char *msg, ...)) | |
d4476be2 | 90 | ATTRIBUTE_PRINTF_2; |
df32d2ce KG |
91 | static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list)); |
92 | static void parse_ctor_invocation_error PARAMS ((void)); | |
93 | static tree parse_jdk1_1_error PARAMS ((const char *)); | |
94 | static void complete_class_report_errors PARAMS ((jdep *)); | |
95 | static int process_imports PARAMS ((void)); | |
96 | static void read_import_dir PARAMS ((tree)); | |
97 | static int find_in_imports_on_demand PARAMS ((tree)); | |
98 | static int find_in_imports PARAMS ((tree)); | |
99 | static int check_pkg_class_access PARAMS ((tree, tree)); | |
100 | static tree resolve_package PARAMS ((tree, tree *)); | |
101 | static tree lookup_package_type PARAMS ((const char *, int)); | |
102 | static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *)); | |
103 | static tree resolve_class PARAMS ((tree, tree, tree)); | |
104 | static void declare_local_variables PARAMS ((int, tree, tree)); | |
105 | static void source_start_java_method PARAMS ((tree)); | |
106 | static void source_end_java_method PARAMS ((void)); | |
107 | static void expand_start_java_method PARAMS ((tree)); | |
108 | static tree find_name_in_single_imports PARAMS ((tree)); | |
109 | static void check_abstract_method_header PARAMS ((tree)); | |
110 | static tree lookup_java_interface_method2 PARAMS ((tree, tree)); | |
111 | static tree resolve_expression_name PARAMS ((tree, tree *)); | |
112 | static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree)); | |
113 | static int check_class_interface_creation PARAMS ((int, int, tree, | |
82371d41 | 114 | tree, tree, tree)); |
df32d2ce | 115 | static tree patch_method_invocation PARAMS ((tree, tree, tree, |
89e09b9a | 116 | int *, tree *)); |
df32d2ce KG |
117 | static int breakdown_qualified PARAMS ((tree *, tree *, tree)); |
118 | static tree resolve_and_layout PARAMS ((tree, tree)); | |
119 | static tree resolve_no_layout PARAMS ((tree, tree)); | |
120 | static int invocation_mode PARAMS ((tree, int)); | |
121 | static tree find_applicable_accessible_methods_list PARAMS ((int, tree, | |
82371d41 | 122 | tree, tree)); |
df32d2ce | 123 | static void search_applicable_methods_list PARAMS ((int, tree, tree, tree, |
1982388a | 124 | tree *, tree *)); |
df32d2ce KG |
125 | static tree find_most_specific_methods_list PARAMS ((tree)); |
126 | static int argument_types_convertible PARAMS ((tree, tree)); | |
127 | static tree patch_invoke PARAMS ((tree, tree, tree)); | |
128 | static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree)); | |
129 | static tree register_incomplete_type PARAMS ((int, tree, tree, tree)); | |
130 | static tree obtain_incomplete_type PARAMS ((tree)); | |
131 | static tree java_complete_lhs PARAMS ((tree)); | |
132 | static tree java_complete_tree PARAMS ((tree)); | |
133 | static int java_pre_expand_clinit PARAMS ((tree)); | |
134 | static void java_complete_expand_method PARAMS ((tree)); | |
135 | static int unresolved_type_p PARAMS ((tree, tree *)); | |
136 | static void create_jdep_list PARAMS ((struct parser_ctxt *)); | |
137 | static tree build_expr_block PARAMS ((tree, tree)); | |
138 | static tree enter_block PARAMS ((void)); | |
139 | static tree enter_a_block PARAMS ((tree)); | |
140 | static tree exit_block PARAMS ((void)); | |
141 | static tree lookup_name_in_blocks PARAMS ((tree)); | |
142 | static void maybe_absorb_scoping_blocks PARAMS ((void)); | |
143 | static tree build_method_invocation PARAMS ((tree, tree)); | |
144 | static tree build_new_invocation PARAMS ((tree, tree)); | |
145 | static tree build_assignment PARAMS ((int, int, tree, tree)); | |
146 | static tree build_binop PARAMS ((enum tree_code, int, tree, tree)); | |
147 | static int check_final_assignment PARAMS ((tree ,tree)); | |
148 | static tree patch_assignment PARAMS ((tree, tree, tree )); | |
149 | static tree patch_binop PARAMS ((tree, tree, tree)); | |
150 | static tree build_unaryop PARAMS ((int, int, tree)); | |
151 | static tree build_incdec PARAMS ((int, int, tree, int)); | |
152 | static tree patch_unaryop PARAMS ((tree, tree)); | |
153 | static tree build_cast PARAMS ((int, tree, tree)); | |
154 | static tree build_null_of_type PARAMS ((tree)); | |
155 | static tree patch_cast PARAMS ((tree, tree)); | |
156 | static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int)); | |
157 | static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree)); | |
158 | static int valid_cast_to_p PARAMS ((tree, tree)); | |
159 | static int valid_method_invocation_conversion_p PARAMS ((tree, tree)); | |
160 | static tree try_builtin_assignconv PARAMS ((tree, tree, tree)); | |
161 | static tree try_reference_assignconv PARAMS ((tree, tree)); | |
162 | static tree build_unresolved_array_type PARAMS ((tree)); | |
163 | static tree build_array_from_name PARAMS ((tree, tree, tree, tree *)); | |
164 | static tree build_array_ref PARAMS ((int, tree, tree)); | |
165 | static tree patch_array_ref PARAMS ((tree)); | |
166 | static tree make_qualified_name PARAMS ((tree, tree, int)); | |
167 | static tree merge_qualified_name PARAMS ((tree, tree)); | |
168 | static tree make_qualified_primary PARAMS ((tree, tree, int)); | |
169 | static int resolve_qualified_expression_name PARAMS ((tree, tree *, | |
82371d41 | 170 | tree *, tree *)); |
df32d2ce KG |
171 | static void qualify_ambiguous_name PARAMS ((tree)); |
172 | static void maybe_generate_clinit PARAMS ((void)); | |
173 | static tree resolve_field_access PARAMS ((tree, tree *, tree *)); | |
174 | static tree build_newarray_node PARAMS ((tree, tree, int)); | |
175 | static tree patch_newarray PARAMS ((tree)); | |
176 | static tree resolve_type_during_patch PARAMS ((tree)); | |
177 | static tree build_this PARAMS ((int)); | |
178 | static tree build_return PARAMS ((int, tree)); | |
179 | static tree patch_return PARAMS ((tree)); | |
180 | static tree maybe_access_field PARAMS ((tree, tree, tree)); | |
181 | static int complete_function_arguments PARAMS ((tree)); | |
182 | static int check_for_static_method_reference PARAMS ((tree, tree, tree, tree, tree)); | |
183 | static int not_accessible_p PARAMS ((tree, tree, int)); | |
184 | static void check_deprecation PARAMS ((tree, tree)); | |
185 | static int class_in_current_package PARAMS ((tree)); | |
186 | static tree build_if_else_statement PARAMS ((int, tree, tree, tree)); | |
187 | static tree patch_if_else_statement PARAMS ((tree)); | |
188 | static tree add_stmt_to_compound PARAMS ((tree, tree, tree)); | |
189 | static tree add_stmt_to_block PARAMS ((tree, tree, tree)); | |
190 | static tree patch_exit_expr PARAMS ((tree)); | |
191 | static tree build_labeled_block PARAMS ((int, tree)); | |
192 | static tree finish_labeled_statement PARAMS ((tree, tree)); | |
193 | static tree build_bc_statement PARAMS ((int, int, tree)); | |
194 | static tree patch_bc_statement PARAMS ((tree)); | |
195 | static tree patch_loop_statement PARAMS ((tree)); | |
196 | static tree build_new_loop PARAMS ((tree)); | |
197 | static tree build_loop_body PARAMS ((int, tree, int)); | |
198 | static tree finish_loop_body PARAMS ((int, tree, tree, int)); | |
199 | static tree build_debugable_stmt PARAMS ((int, tree)); | |
200 | static tree finish_for_loop PARAMS ((int, tree, tree, tree)); | |
201 | static tree patch_switch_statement PARAMS ((tree)); | |
202 | static tree string_constant_concatenation PARAMS ((tree, tree)); | |
203 | static tree build_string_concatenation PARAMS ((tree, tree)); | |
204 | static tree patch_string_cst PARAMS ((tree)); | |
205 | static tree patch_string PARAMS ((tree)); | |
206 | static tree build_try_statement PARAMS ((int, tree, tree)); | |
207 | static tree build_try_finally_statement PARAMS ((int, tree, tree)); | |
208 | static tree patch_try_statement PARAMS ((tree)); | |
209 | static tree patch_synchronized_statement PARAMS ((tree, tree)); | |
210 | static tree patch_throw_statement PARAMS ((tree, tree)); | |
211 | static void check_thrown_exceptions PARAMS ((int, tree)); | |
212 | static int check_thrown_exceptions_do PARAMS ((tree)); | |
213 | static void purge_unchecked_exceptions PARAMS ((tree)); | |
214 | static void check_throws_clauses PARAMS ((tree, tree, tree)); | |
215 | static void finish_method_declaration PARAMS ((tree)); | |
216 | static tree build_super_invocation PARAMS ((tree)); | |
217 | static int verify_constructor_circularity PARAMS ((tree, tree)); | |
218 | static char *constructor_circularity_msg PARAMS ((tree, tree)); | |
219 | static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree, | |
82371d41 | 220 | int, int)); |
df32d2ce KG |
221 | static const char *get_printable_method_name PARAMS ((tree)); |
222 | static tree patch_conditional_expr PARAMS ((tree, tree, tree)); | |
223 | static void maybe_generate_finit PARAMS ((void)); | |
224 | static void fix_constructors PARAMS ((tree)); | |
225 | static int verify_constructor_super PARAMS ((void)); | |
226 | static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree)); | |
227 | static void start_artificial_method_body PARAMS ((tree)); | |
228 | static void end_artificial_method_body PARAMS ((tree)); | |
229 | static int check_method_redefinition PARAMS ((tree, tree)); | |
230 | static int reset_method_name PARAMS ((tree)); | |
231 | static void java_check_regular_methods PARAMS ((tree)); | |
232 | static void java_check_abstract_methods PARAMS ((tree)); | |
233 | static tree maybe_build_primttype_type_ref PARAMS ((tree, tree)); | |
234 | static void unreachable_stmt_error PARAMS ((tree)); | |
235 | static tree find_expr_with_wfl PARAMS ((tree)); | |
236 | static void missing_return_error PARAMS ((tree)); | |
237 | static tree build_new_array_init PARAMS ((int, tree)); | |
238 | static tree patch_new_array_init PARAMS ((tree, tree)); | |
239 | static tree maybe_build_array_element_wfl PARAMS ((tree)); | |
240 | static int array_constructor_check_entry PARAMS ((tree, tree)); | |
241 | static const char *purify_type_name PARAMS ((const char *)); | |
242 | static tree fold_constant_for_init PARAMS ((tree, tree)); | |
243 | static tree strip_out_static_field_access_decl PARAMS ((tree)); | |
244 | static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *)); | |
245 | static void static_ref_err PARAMS ((tree, tree, tree)); | |
246 | static void parser_add_interface PARAMS ((tree, tree, tree)); | |
247 | static void add_superinterfaces PARAMS ((tree, tree)); | |
248 | static tree jdep_resolve_class PARAMS ((jdep *)); | |
249 | static int note_possible_classname PARAMS ((const char *, int)); | |
250 | static void java_complete_expand_methods PARAMS ((void)); | |
251 | static void java_expand_finals PARAMS ((void)); | |
252 | static tree cut_identifier_in_qualified PARAMS ((tree)); | |
253 | static tree java_stabilize_reference PARAMS ((tree)); | |
254 | static tree do_unary_numeric_promotion PARAMS ((tree)); | |
255 | static char * operator_string PARAMS ((tree)); | |
256 | static tree do_merge_string_cste PARAMS ((tree, const char *, int, int)); | |
257 | static tree merge_string_cste PARAMS ((tree, tree, int)); | |
258 | static tree java_refold PARAMS ((tree)); | |
259 | static int java_decl_equiv PARAMS ((tree, tree)); | |
260 | static int binop_compound_p PARAMS ((enum tree_code)); | |
261 | static tree search_loop PARAMS ((tree)); | |
262 | static int labeled_block_contains_loop_p PARAMS ((tree, tree)); | |
263 | static void check_abstract_method_definitions PARAMS ((int, tree, tree)); | |
264 | static void java_check_abstract_method_definitions PARAMS ((tree)); | |
265 | static void java_debug_context_do PARAMS ((int)); | |
82371d41 | 266 | |
e04a16fb AG |
267 | /* Number of error found so far. */ |
268 | int java_error_count; | |
269 | /* Number of warning found so far. */ | |
270 | int java_warning_count; | |
ce6e9147 APB |
271 | /* Tell when not to fold, when doing xrefs */ |
272 | int do_not_fold; | |
e04a16fb AG |
273 | |
274 | /* The current parser context */ | |
d4370213 | 275 | struct parser_ctxt *ctxp; |
e04a16fb | 276 | |
d4370213 | 277 | /* List of things that were analyzed for which code will be generated */ |
b351b287 APB |
278 | static struct parser_ctxt *ctxp_for_generation = NULL; |
279 | ||
e04a16fb AG |
280 | /* binop_lookup maps token to tree_code. It is used where binary |
281 | operations are involved and required by the parser. RDIV_EXPR | |
282 | covers both integral/floating point division. The code is changed | |
283 | once the type of both operator is worked out. */ | |
284 | ||
285 | static enum tree_code binop_lookup[19] = | |
286 | { | |
287 | PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR, | |
288 | LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR, | |
289 | BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR, | |
290 | TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR, | |
291 | EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR, | |
292 | }; | |
293 | #define BINOP_LOOKUP(VALUE) \ | |
294 | binop_lookup [((VALUE) - PLUS_TK)% \ | |
295 | (sizeof (binop_lookup) / sizeof (binop_lookup[0]))] | |
296 | ||
5cbdba64 APB |
297 | /* This is the end index for binary operators that can also be used |
298 | in compound assignements. */ | |
299 | #define BINOP_COMPOUND_CANDIDATES 11 | |
300 | ||
e04a16fb AG |
301 | /* Fake WFL used to report error message. It is initialized once if |
302 | needed and reused with it's location information is overriden. */ | |
15fdcfe9 | 303 | tree wfl_operator = NULL_TREE; |
e04a16fb AG |
304 | |
305 | /* The "$L" identifier we use to create labels. */ | |
b67d701b PB |
306 | static tree label_id = NULL_TREE; |
307 | ||
308 | /* The "StringBuffer" identifier used for the String `+' operator. */ | |
309 | static tree wfl_string_buffer = NULL_TREE; | |
310 | ||
311 | /* The "append" identifier used for String `+' operator. */ | |
312 | static tree wfl_append = NULL_TREE; | |
313 | ||
314 | /* The "toString" identifier used for String `+' operator. */ | |
315 | static tree wfl_to_string = NULL_TREE; | |
ba179f9f APB |
316 | |
317 | /* The "java.lang" import qualified name. */ | |
318 | static tree java_lang_id = NULL_TREE; | |
09ed0f70 APB |
319 | |
320 | /* The "java.lang.Cloneable" qualified name. */ | |
321 | static tree java_lang_cloneable = NULL_TREE; | |
f099f336 APB |
322 | |
323 | /* Context and flag for static blocks */ | |
324 | static tree current_static_block = NULL_TREE; | |
325 | ||
ee07f4f4 APB |
326 | /* The list of all packages we've seen so far */ |
327 | static tree package_list = NULL_TREE; | |
328 | ||
e04a16fb AG |
329 | %} |
330 | ||
331 | %union { | |
332 | tree node; | |
333 | int sub_token; | |
334 | struct { | |
335 | int token; | |
336 | int location; | |
337 | } operator; | |
338 | int value; | |
339 | } | |
340 | ||
9ee9b555 KG |
341 | %{ |
342 | #include "lex.c" | |
343 | %} | |
344 | ||
e04a16fb AG |
345 | %pure_parser |
346 | ||
347 | /* Things defined here have to match the order of what's in the | |
348 | binop_lookup table. */ | |
349 | ||
350 | %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK | |
351 | %token LS_TK SRS_TK ZRS_TK | |
352 | %token AND_TK XOR_TK OR_TK | |
353 | %token BOOL_AND_TK BOOL_OR_TK | |
354 | %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK | |
355 | ||
356 | /* This maps to the same binop_lookup entry than the token above */ | |
357 | ||
358 | %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK | |
359 | %token REM_ASSIGN_TK | |
360 | %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK | |
361 | %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK | |
362 | ||
363 | ||
364 | /* Modifier TOKEN have to be kept in this order. Don't scramble it */ | |
365 | ||
366 | %token PUBLIC_TK PRIVATE_TK PROTECTED_TK | |
367 | %token STATIC_TK FINAL_TK SYNCHRONIZED_TK | |
368 | %token VOLATILE_TK TRANSIENT_TK NATIVE_TK | |
369 | %token PAD_TK ABSTRACT_TK MODIFIER_TK | |
370 | ||
371 | /* Keep those two in order, too */ | |
372 | %token DECR_TK INCR_TK | |
373 | ||
374 | /* From now one, things can be in any order */ | |
375 | ||
376 | %token DEFAULT_TK IF_TK THROW_TK | |
377 | %token BOOLEAN_TK DO_TK IMPLEMENTS_TK | |
378 | %token THROWS_TK BREAK_TK IMPORT_TK | |
379 | %token ELSE_TK INSTANCEOF_TK RETURN_TK | |
380 | %token VOID_TK CATCH_TK INTERFACE_TK | |
381 | %token CASE_TK EXTENDS_TK FINALLY_TK | |
382 | %token SUPER_TK WHILE_TK CLASS_TK | |
383 | %token SWITCH_TK CONST_TK TRY_TK | |
384 | %token FOR_TK NEW_TK CONTINUE_TK | |
385 | %token GOTO_TK PACKAGE_TK THIS_TK | |
386 | ||
387 | %token BYTE_TK SHORT_TK INT_TK LONG_TK | |
388 | %token CHAR_TK INTEGRAL_TK | |
389 | ||
390 | %token FLOAT_TK DOUBLE_TK FP_TK | |
391 | ||
392 | %token ID_TK | |
393 | ||
394 | %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK | |
395 | ||
396 | %token ASSIGN_ANY_TK ASSIGN_TK | |
397 | %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK | |
398 | ||
399 | %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK | |
400 | %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK | |
401 | ||
402 | %type <value> modifiers MODIFIER_TK | |
403 | ||
404 | %type <node> super ID_TK identifier | |
405 | %type <node> name simple_name qualified_name | |
406 | %type <node> class_declaration type_declaration compilation_unit | |
407 | field_declaration method_declaration extends_interfaces | |
408 | interfaces interface_type_list | |
409 | interface_declaration class_member_declaration | |
410 | import_declarations package_declaration | |
411 | type_declarations interface_body | |
412 | interface_member_declaration constant_declaration | |
413 | interface_member_declarations interface_type | |
414 | abstract_method_declaration interface_type_list | |
415 | %type <node> class_body_declaration class_member_declaration | |
416 | static_initializer constructor_declaration block | |
22eed1e6 | 417 | %type <node> class_body_declarations constructor_header |
e04a16fb AG |
418 | %type <node> class_or_interface_type class_type class_type_list |
419 | constructor_declarator explicit_constructor_invocation | |
b9f7e36c | 420 | %type <node> dim_expr dim_exprs this_or_super throws |
e04a16fb AG |
421 | |
422 | %type <node> variable_declarator_id variable_declarator | |
423 | variable_declarators variable_initializer | |
22eed1e6 | 424 | variable_initializers constructor_body |
ac825856 | 425 | array_initializer |
e04a16fb | 426 | |
2e5eb5c5 | 427 | %type <node> class_body block_end constructor_block_end |
e04a16fb AG |
428 | %type <node> statement statement_without_trailing_substatement |
429 | labeled_statement if_then_statement label_decl | |
430 | if_then_else_statement while_statement for_statement | |
431 | statement_nsi labeled_statement_nsi do_statement | |
432 | if_then_else_statement_nsi while_statement_nsi | |
433 | for_statement_nsi statement_expression_list for_init | |
434 | for_update statement_expression expression_statement | |
435 | primary_no_new_array expression primary | |
436 | array_creation_expression array_type | |
437 | class_instance_creation_expression field_access | |
438 | method_invocation array_access something_dot_new | |
439 | argument_list postfix_expression while_expression | |
440 | post_increment_expression post_decrement_expression | |
441 | unary_expression_not_plus_minus unary_expression | |
442 | pre_increment_expression pre_decrement_expression | |
443 | unary_expression_not_plus_minus cast_expression | |
444 | multiplicative_expression additive_expression | |
445 | shift_expression relational_expression | |
446 | equality_expression and_expression | |
447 | exclusive_or_expression inclusive_or_expression | |
448 | conditional_and_expression conditional_or_expression | |
449 | conditional_expression assignment_expression | |
450 | left_hand_side assignment for_header for_begin | |
451 | constant_expression do_statement_begin empty_statement | |
b67d701b | 452 | switch_statement synchronized_statement throw_statement |
f8976021 | 453 | try_statement switch_expression switch_block |
15fdcfe9 | 454 | catches catch_clause catch_clause_parameter finally |
e04a16fb AG |
455 | %type <node> return_statement break_statement continue_statement |
456 | ||
457 | %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK | |
458 | %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK | |
459 | %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK | |
460 | %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK | |
461 | %type <operator> ASSIGN_ANY_TK assignment_operator | |
462 | %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK | |
463 | %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK | |
464 | %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK | |
7f10c2e2 | 465 | %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK |
5e942c50 | 466 | %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK |
b9f7e36c APB |
467 | %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK |
468 | %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK | |
e04a16fb AG |
469 | |
470 | %type <node> method_body | |
471 | ||
472 | %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK | |
473 | STRING_LIT_TK NULL_TK VOID_TK | |
474 | ||
475 | %type <node> IF_TK WHILE_TK FOR_TK | |
476 | ||
477 | %type <node> formal_parameter_list formal_parameter | |
478 | method_declarator method_header | |
479 | ||
480 | %type <node> primitive_type reference_type type | |
481 | BOOLEAN_TK INTEGRAL_TK FP_TK | |
482 | ||
483 | %% | |
484 | /* 19.2 Production from 2.3: The Syntactic Grammar */ | |
485 | goal: | |
486 | compilation_unit | |
487 | {} | |
488 | ; | |
489 | ||
490 | /* 19.3 Productions from 3: Lexical structure */ | |
491 | literal: | |
492 | INT_LIT_TK | |
493 | | FP_LIT_TK | |
494 | | BOOL_LIT_TK | |
495 | | CHAR_LIT_TK | |
496 | | STRING_LIT_TK | |
497 | | NULL_TK | |
498 | ; | |
499 | ||
500 | /* 19.4 Productions from 4: Types, Values and Variables */ | |
501 | type: | |
502 | primitive_type | |
503 | | reference_type | |
504 | ; | |
505 | ||
506 | primitive_type: | |
507 | INTEGRAL_TK | |
508 | | FP_TK | |
509 | | BOOLEAN_TK | |
510 | ; | |
511 | ||
512 | reference_type: | |
513 | class_or_interface_type | |
514 | | array_type | |
515 | ; | |
516 | ||
517 | class_or_interface_type: | |
518 | name | |
519 | ; | |
520 | ||
521 | class_type: | |
522 | class_or_interface_type /* Default rule */ | |
523 | ; | |
524 | ||
525 | interface_type: | |
526 | class_or_interface_type | |
527 | ; | |
528 | ||
529 | array_type: | |
530 | primitive_type OSB_TK CSB_TK | |
531 | { | |
532 | $$ = build_java_array_type ($1, -1); | |
533 | CLASS_LOADED_P ($$) = 1; | |
534 | } | |
535 | | name OSB_TK CSB_TK | |
536 | { $$ = build_unresolved_array_type ($1); } | |
537 | | array_type OSB_TK CSB_TK | |
538 | { $$ = build_unresolved_array_type ($1); } | |
539 | | primitive_type OSB_TK error | |
540 | {RULE ("']' expected"); RECOVER;} | |
541 | | array_type OSB_TK error | |
542 | {RULE ("']' expected"); RECOVER;} | |
543 | ; | |
544 | ||
545 | /* 19.5 Productions from 6: Names */ | |
546 | name: | |
547 | simple_name /* Default rule */ | |
548 | | qualified_name /* Default rule */ | |
549 | ; | |
550 | ||
551 | simple_name: | |
552 | identifier /* Default rule */ | |
553 | ; | |
554 | ||
555 | qualified_name: | |
556 | name DOT_TK identifier | |
557 | { $$ = make_qualified_name ($1, $3, $2.location); } | |
558 | ; | |
559 | ||
560 | identifier: | |
561 | ID_TK | |
562 | ; | |
563 | ||
564 | /* 19.6: Production from 7: Packages */ | |
565 | compilation_unit: | |
566 | {$$ = NULL;} | |
567 | | package_declaration | |
568 | | import_declarations | |
569 | | type_declarations | |
570 | | package_declaration import_declarations | |
571 | | package_declaration type_declarations | |
572 | | import_declarations type_declarations | |
573 | | package_declaration import_declarations type_declarations | |
574 | ; | |
575 | ||
576 | import_declarations: | |
577 | import_declaration | |
578 | { | |
579 | $$ = NULL; | |
580 | } | |
581 | | import_declarations import_declaration | |
582 | { | |
583 | $$ = NULL; | |
584 | } | |
585 | ; | |
586 | ||
587 | type_declarations: | |
588 | type_declaration | |
589 | | type_declarations type_declaration | |
590 | ; | |
591 | ||
592 | package_declaration: | |
593 | PACKAGE_TK name SC_TK | |
ee07f4f4 APB |
594 | { |
595 | ctxp->package = EXPR_WFL_NODE ($2); | |
596 | package_list = tree_cons (ctxp->package, NULL, package_list); | |
597 | } | |
e04a16fb AG |
598 | | PACKAGE_TK error |
599 | {yyerror ("Missing name"); RECOVER;} | |
600 | | PACKAGE_TK name error | |
601 | {yyerror ("';' expected"); RECOVER;} | |
602 | ; | |
603 | ||
604 | import_declaration: | |
605 | single_type_import_declaration | |
606 | | type_import_on_demand_declaration | |
607 | ; | |
608 | ||
609 | single_type_import_declaration: | |
610 | IMPORT_TK name SC_TK | |
611 | { | |
612 | tree name = EXPR_WFL_NODE ($2), node, last_name; | |
613 | int i = IDENTIFIER_LENGTH (name)-1; | |
49f48c71 | 614 | const char *last = &IDENTIFIER_POINTER (name)[i]; |
e04a16fb AG |
615 | while (last != IDENTIFIER_POINTER (name)) |
616 | { | |
617 | if (last [0] == '.') | |
618 | break; | |
619 | last--; | |
620 | } | |
621 | last_name = get_identifier (++last); | |
622 | if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name)) | |
623 | { | |
624 | tree err = find_name_in_single_imports (last_name); | |
625 | if (err && err != name) | |
626 | parse_error_context | |
627 | ($2, "Ambiguous class: `%s' and `%s'", | |
628 | IDENTIFIER_POINTER (name), | |
629 | IDENTIFIER_POINTER (err)); | |
5e942c50 APB |
630 | else |
631 | REGISTER_IMPORT ($2, last_name) | |
e04a16fb AG |
632 | } |
633 | else | |
5e942c50 | 634 | REGISTER_IMPORT ($2, last_name); |
e04a16fb AG |
635 | } |
636 | | IMPORT_TK error | |
637 | {yyerror ("Missing name"); RECOVER;} | |
638 | | IMPORT_TK name error | |
639 | {yyerror ("';' expected"); RECOVER;} | |
640 | ; | |
641 | ||
642 | type_import_on_demand_declaration: | |
643 | IMPORT_TK name DOT_TK MULT_TK SC_TK | |
644 | { | |
645 | tree name = EXPR_WFL_NODE ($2); | |
ba179f9f APB |
646 | /* Don't import java.lang.* twice. */ |
647 | if (name != java_lang_id) | |
648 | { | |
649 | tree node = build_tree_list ($2, NULL_TREE); | |
650 | read_import_dir ($2); | |
651 | TREE_CHAIN (node) = ctxp->import_demand_list; | |
652 | ctxp->import_demand_list = node; | |
653 | } | |
e04a16fb AG |
654 | } |
655 | | IMPORT_TK name DOT_TK error | |
656 | {yyerror ("'*' expected"); RECOVER;} | |
657 | | IMPORT_TK name DOT_TK MULT_TK error | |
658 | {yyerror ("';' expected"); RECOVER;} | |
659 | ; | |
660 | ||
661 | type_declaration: | |
662 | class_declaration | |
663 | { | |
22eed1e6 | 664 | maybe_generate_finit (); |
7525cc04 | 665 | maybe_generate_clinit (); |
e04a16fb AG |
666 | $$ = $1; |
667 | } | |
668 | | interface_declaration | |
7f1d4866 APB |
669 | { |
670 | maybe_generate_clinit (); | |
671 | $$ = $1; | |
672 | } | |
e04a16fb AG |
673 | | SC_TK |
674 | { $$ = NULL; } | |
675 | | error | |
676 | { | |
677 | YYERROR_NOW; | |
678 | yyerror ("Class or interface declaration expected"); | |
679 | } | |
680 | ; | |
681 | ||
682 | /* 19.7 Shortened from the original: | |
683 | modifiers: modifier | modifiers modifier | |
684 | modifier: any of public... */ | |
685 | modifiers: | |
686 | MODIFIER_TK | |
687 | { | |
688 | $$ = (1 << $1); | |
689 | } | |
690 | | modifiers MODIFIER_TK | |
691 | { | |
692 | int acc = (1 << $2); | |
693 | if ($$ & acc) | |
694 | parse_error_context | |
695 | (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice", | |
696 | java_accstring_lookup (acc)); | |
697 | else | |
698 | { | |
699 | $$ |= acc; | |
700 | } | |
701 | } | |
702 | ; | |
703 | ||
704 | /* 19.8.1 Production from $8.1: Class Declaration */ | |
705 | class_declaration: | |
706 | modifiers CLASS_TK identifier super interfaces | |
707 | { create_class ($1, $3, $4, $5); } | |
708 | class_body | |
709 | { | |
710 | $$ = $7; | |
711 | } | |
712 | | CLASS_TK identifier super interfaces | |
713 | { create_class (0, $2, $3, $4); } | |
714 | class_body | |
715 | { | |
716 | $$ = $6; | |
717 | } | |
718 | | modifiers CLASS_TK error | |
719 | {yyerror ("Missing class name"); RECOVER;} | |
720 | | CLASS_TK error | |
721 | {yyerror ("Missing class name"); RECOVER;} | |
722 | | CLASS_TK identifier error | |
0b4d333e APB |
723 | { |
724 | if (!ctxp->class_err) yyerror ("'{' expected"); | |
725 | DRECOVER(class1); | |
726 | } | |
e04a16fb AG |
727 | | modifiers CLASS_TK identifier error |
728 | {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;} | |
729 | ; | |
730 | ||
731 | super: | |
732 | { $$ = NULL; } | |
733 | | EXTENDS_TK class_type | |
734 | { $$ = $2; } | |
735 | | EXTENDS_TK class_type error | |
736 | {yyerror ("'{' expected"); ctxp->class_err=1;} | |
737 | | EXTENDS_TK error | |
738 | {yyerror ("Missing super class name"); ctxp->class_err=1;} | |
739 | ; | |
740 | ||
741 | interfaces: | |
742 | { $$ = NULL_TREE; } | |
743 | | IMPLEMENTS_TK interface_type_list | |
744 | { $$ = $2; } | |
745 | | IMPLEMENTS_TK error | |
746 | { | |
747 | ctxp->class_err=1; | |
748 | yyerror ("Missing interface name"); | |
749 | } | |
750 | ; | |
751 | ||
752 | interface_type_list: | |
753 | interface_type | |
754 | { | |
755 | ctxp->interface_number = 1; | |
756 | $$ = build_tree_list ($1, NULL_TREE); | |
757 | } | |
758 | | interface_type_list C_TK interface_type | |
759 | { | |
760 | ctxp->interface_number++; | |
761 | $$ = chainon ($1, build_tree_list ($3, NULL_TREE)); | |
762 | } | |
763 | | interface_type_list C_TK error | |
764 | {yyerror ("Missing interface name"); RECOVER;} | |
765 | ; | |
766 | ||
767 | class_body: | |
768 | OCB_TK CCB_TK | |
7f10c2e2 APB |
769 | { |
770 | /* Store the location of the `}' when doing xrefs */ | |
771 | if (flag_emit_xref) | |
772 | DECL_END_SOURCE_LINE (ctxp->current_parsed_class) = | |
773 | EXPR_WFL_ADD_COL ($2.location, 1); | |
774 | $$ = ctxp->current_parsed_class; | |
775 | } | |
e04a16fb | 776 | | OCB_TK class_body_declarations CCB_TK |
7f10c2e2 APB |
777 | { |
778 | /* Store the location of the `}' when doing xrefs */ | |
779 | if (flag_emit_xref) | |
780 | DECL_END_SOURCE_LINE (ctxp->current_parsed_class) = | |
781 | EXPR_WFL_ADD_COL ($3.location, 1); | |
782 | $$ = ctxp->current_parsed_class; | |
783 | } | |
e04a16fb AG |
784 | ; |
785 | ||
786 | class_body_declarations: | |
787 | class_body_declaration | |
788 | | class_body_declarations class_body_declaration | |
789 | ; | |
790 | ||
791 | class_body_declaration: | |
792 | class_member_declaration | |
793 | | static_initializer | |
794 | | constructor_declaration | |
795 | | block /* Added, JDK1.1, instance initializer */ | |
b67d701b | 796 | { $$ = parse_jdk1_1_error ("instance initializer"); } |
e04a16fb AG |
797 | ; |
798 | ||
799 | class_member_declaration: | |
800 | field_declaration | |
0b4d333e APB |
801 | | field_declaration SC_TK |
802 | { $$ = $1; } | |
e04a16fb AG |
803 | | method_declaration |
804 | | class_declaration /* Added, JDK1.1 inner classes */ | |
b67d701b | 805 | { $$ = parse_jdk1_1_error ("inner classe declaration"); } |
e04a16fb | 806 | | interface_declaration /* Added, JDK1.1 inner classes */ |
b67d701b | 807 | { $$ = parse_jdk1_1_error ("inner interface declaration"); } |
e04a16fb AG |
808 | ; |
809 | ||
810 | /* 19.8.2 Productions from 8.3: Field Declarations */ | |
811 | field_declaration: | |
812 | type variable_declarators SC_TK | |
813 | { register_fields (0, $1, $2); } | |
814 | | modifiers type variable_declarators SC_TK | |
815 | { | |
e04a16fb AG |
816 | check_modifiers |
817 | ("Illegal modifier `%s' for field declaration", | |
818 | $1, FIELD_MODIFIERS); | |
819 | check_modifiers_consistency ($1); | |
820 | register_fields ($1, $2, $3); | |
821 | } | |
822 | ; | |
823 | ||
824 | variable_declarators: | |
825 | /* Should we use build_decl_list () instead ? FIXME */ | |
826 | variable_declarator /* Default rule */ | |
827 | | variable_declarators C_TK variable_declarator | |
828 | { $$ = chainon ($1, $3); } | |
829 | | variable_declarators C_TK error | |
830 | {yyerror ("Missing term"); RECOVER;} | |
831 | ; | |
832 | ||
833 | variable_declarator: | |
834 | variable_declarator_id | |
835 | { $$ = build_tree_list ($1, NULL_TREE); } | |
836 | | variable_declarator_id ASSIGN_TK variable_initializer | |
837 | { | |
838 | if (java_error_count) | |
839 | $3 = NULL_TREE; | |
840 | $$ = build_tree_list | |
841 | ($1, build_assignment ($2.token, $2.location, $1, $3)); | |
842 | } | |
843 | | variable_declarator_id ASSIGN_TK error | |
844 | { | |
845 | yyerror ("Missing variable initializer"); | |
846 | $$ = build_tree_list ($1, NULL_TREE); | |
847 | RECOVER; | |
848 | } | |
849 | | variable_declarator_id ASSIGN_TK variable_initializer error | |
850 | { | |
851 | yyerror ("';' expected"); | |
852 | $$ = build_tree_list ($1, NULL_TREE); | |
853 | RECOVER; | |
854 | } | |
855 | ; | |
856 | ||
857 | variable_declarator_id: | |
858 | identifier | |
859 | | variable_declarator_id OSB_TK CSB_TK | |
c583dd46 | 860 | { $$ = build_unresolved_array_type ($1); } |
e04a16fb AG |
861 | | identifier error |
862 | {yyerror ("Invalid declaration"); DRECOVER(vdi);} | |
863 | | variable_declarator_id OSB_TK error | |
864 | {yyerror ("']' expected"); DRECOVER(vdi);} | |
865 | | variable_declarator_id CSB_TK error | |
866 | {yyerror ("Unbalanced ']'"); DRECOVER(vdi);} | |
867 | ; | |
868 | ||
869 | variable_initializer: | |
870 | expression | |
871 | | array_initializer | |
e04a16fb AG |
872 | ; |
873 | ||
874 | /* 19.8.3 Productions from 8.4: Method Declarations */ | |
875 | method_declaration: | |
876 | method_header | |
877 | { | |
878 | current_function_decl = $1; | |
879 | source_start_java_method (current_function_decl); | |
880 | } | |
881 | method_body | |
b635eb2f | 882 | { finish_method_declaration ($3); } |
e04a16fb AG |
883 | | method_header error |
884 | {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;} | |
885 | ; | |
886 | ||
887 | method_header: | |
888 | type method_declarator throws | |
b9f7e36c | 889 | { $$ = method_header (0, $1, $2, $3); } |
e04a16fb | 890 | | VOID_TK method_declarator throws |
b9f7e36c | 891 | { $$ = method_header (0, void_type_node, $2, $3); } |
e04a16fb | 892 | | modifiers type method_declarator throws |
b9f7e36c | 893 | { $$ = method_header ($1, $2, $3, $4); } |
e04a16fb | 894 | | modifiers VOID_TK method_declarator throws |
b9f7e36c | 895 | { $$ = method_header ($1, void_type_node, $3, $4); } |
e04a16fb | 896 | | type error |
efa0a23f APB |
897 | { |
898 | yyerror ("Invalid method declaration, method name required"); | |
899 | RECOVER; | |
900 | } | |
e04a16fb AG |
901 | | modifiers type error |
902 | {RECOVER;} | |
903 | | VOID_TK error | |
904 | {yyerror ("Identifier expected"); RECOVER;} | |
905 | | modifiers VOID_TK error | |
906 | {yyerror ("Identifier expected"); RECOVER;} | |
907 | | modifiers error | |
908 | { | |
909 | yyerror ("Invalid method declaration, return type required"); | |
910 | RECOVER; | |
911 | } | |
912 | ; | |
913 | ||
914 | method_declarator: | |
915 | identifier OP_TK CP_TK | |
916 | { $$ = method_declarator ($1, NULL_TREE); } | |
917 | | identifier OP_TK formal_parameter_list CP_TK | |
918 | { $$ = method_declarator ($1, $3); } | |
919 | | method_declarator OSB_TK CSB_TK | |
920 | { | |
1886c9d8 APB |
921 | EXPR_WFL_LINECOL (wfl_operator) = $2.location; |
922 | TREE_PURPOSE ($1) = | |
923 | build_unresolved_array_type (TREE_PURPOSE ($1)); | |
924 | parse_warning_context | |
925 | (wfl_operator, | |
926 | "Discouraged form of returned type specification"); | |
e04a16fb AG |
927 | } |
928 | | identifier OP_TK error | |
929 | {yyerror ("')' expected"); DRECOVER(method_declarator);} | |
930 | | method_declarator OSB_TK error | |
931 | {yyerror ("']' expected"); RECOVER;} | |
932 | ; | |
933 | ||
934 | formal_parameter_list: | |
935 | formal_parameter | |
936 | { | |
937 | ctxp->formal_parameter_number = 1; | |
938 | } | |
939 | | formal_parameter_list C_TK formal_parameter | |
940 | { | |
941 | ctxp->formal_parameter_number += 1; | |
942 | $$ = chainon ($1, $3); | |
943 | } | |
944 | | formal_parameter_list C_TK error | |
945 | {yyerror ("Missing formal parameter term"); RECOVER;} | |
946 | ; | |
947 | ||
948 | formal_parameter: | |
949 | type variable_declarator_id | |
950 | { | |
951 | $$ = build_tree_list ($2, $1); | |
952 | } | |
18990de5 | 953 | | final type variable_declarator_id /* Added, JDK1.1 final parms */ |
5256aa37 APB |
954 | { |
955 | parse_jdk1_1_error ("final parameters"); | |
956 | $$ = build_tree_list ($3, $2); | |
957 | } | |
e04a16fb AG |
958 | | type error |
959 | {yyerror ("Missing identifier"); RECOVER;} | |
18990de5 | 960 | | final type error |
e04a16fb | 961 | { |
e04a16fb AG |
962 | yyerror ("Missing identifier"); RECOVER; |
963 | } | |
964 | ; | |
965 | ||
18990de5 JB |
966 | final: |
967 | modifiers | |
968 | { | |
969 | check_modifiers ("Illegal modifier `%s'. Only `final' was expected here", | |
970 | $1, ACC_FINAL); | |
971 | if ($1 != ACC_FINAL) | |
972 | MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE); | |
973 | } | |
974 | ; | |
975 | ||
e04a16fb | 976 | throws: |
b9f7e36c | 977 | { $$ = NULL_TREE; } |
e04a16fb | 978 | | THROWS_TK class_type_list |
b9f7e36c | 979 | { $$ = $2; } |
e04a16fb AG |
980 | | THROWS_TK error |
981 | {yyerror ("Missing class type term"); RECOVER;} | |
982 | ; | |
983 | ||
984 | class_type_list: | |
985 | class_type | |
c877974e | 986 | { $$ = build_tree_list ($1, $1); } |
e04a16fb | 987 | | class_type_list C_TK class_type |
c877974e | 988 | { $$ = tree_cons ($3, $3, $1); } |
e04a16fb AG |
989 | | class_type_list C_TK error |
990 | {yyerror ("Missing class type term"); RECOVER;} | |
991 | ; | |
992 | ||
993 | method_body: | |
994 | block | |
995 | | block SC_TK | |
996 | | SC_TK | |
997 | { $$ = NULL_TREE; } /* Probably not the right thing to do. */ | |
998 | ; | |
999 | ||
1000 | /* 19.8.4 Productions from 8.5: Static Initializers */ | |
1001 | static_initializer: | |
1002 | static block | |
1003 | { | |
f099f336 APB |
1004 | TREE_CHAIN ($2) = ctxp->static_initialized; |
1005 | ctxp->static_initialized = $2; | |
e04a16fb AG |
1006 | } |
1007 | | static block SC_TK /* Shouldn't be here. FIXME */ | |
1008 | { | |
f099f336 APB |
1009 | TREE_CHAIN ($2) = ctxp->static_initialized; |
1010 | ctxp->static_initialized = $2; | |
e04a16fb AG |
1011 | } |
1012 | ; | |
1013 | ||
1014 | static: /* Test lval.sub_token here */ | |
1015 | MODIFIER_TK | |
1016 | { | |
1017 | SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1)); | |
1018 | } | |
1019 | ; | |
1020 | ||
1021 | /* 19.8.5 Productions from 8.6: Constructor Declarations */ | |
e04a16fb | 1022 | constructor_declaration: |
22eed1e6 | 1023 | constructor_header |
e04a16fb | 1024 | { |
22eed1e6 APB |
1025 | current_function_decl = $1; |
1026 | source_start_java_method (current_function_decl); | |
e04a16fb | 1027 | } |
22eed1e6 | 1028 | constructor_body |
b635eb2f | 1029 | { finish_method_declaration ($3); } |
22eed1e6 APB |
1030 | ; |
1031 | ||
1032 | constructor_header: | |
1033 | constructor_declarator throws | |
1034 | { $$ = method_header (0, NULL_TREE, $1, $2); } | |
1035 | | modifiers constructor_declarator throws | |
1036 | { $$ = method_header ($1, NULL_TREE, $2, $3); } | |
e04a16fb AG |
1037 | ; |
1038 | ||
1039 | constructor_declarator: | |
1040 | simple_name OP_TK CP_TK | |
22eed1e6 | 1041 | { $$ = method_declarator ($1, NULL_TREE); } |
e04a16fb | 1042 | | simple_name OP_TK formal_parameter_list CP_TK |
22eed1e6 | 1043 | { $$ = method_declarator ($1, $3); } |
e04a16fb AG |
1044 | ; |
1045 | ||
1046 | constructor_body: | |
22eed1e6 APB |
1047 | /* Unlike regular method, we always need a complete (empty) |
1048 | body so we can safely perform all the required code | |
1049 | addition (super invocation and field initialization) */ | |
2e5eb5c5 | 1050 | block_begin constructor_block_end |
22eed1e6 | 1051 | { |
9bbc7d9f | 1052 | BLOCK_EXPR_BODY ($2) = empty_stmt_node; |
22eed1e6 APB |
1053 | $$ = $2; |
1054 | } | |
2e5eb5c5 | 1055 | | block_begin explicit_constructor_invocation constructor_block_end |
22eed1e6 | 1056 | { $$ = $3; } |
2e5eb5c5 | 1057 | | block_begin block_statements constructor_block_end |
22eed1e6 | 1058 | { $$ = $3; } |
2e5eb5c5 | 1059 | | block_begin explicit_constructor_invocation block_statements constructor_block_end |
22eed1e6 | 1060 | { $$ = $4; } |
e04a16fb AG |
1061 | ; |
1062 | ||
2e5eb5c5 APB |
1063 | constructor_block_end: |
1064 | block_end | |
1065 | | block_end SC_TK | |
1066 | ||
e04a16fb AG |
1067 | /* Error recovery for that rule moved down expression_statement: rule. */ |
1068 | explicit_constructor_invocation: | |
1069 | this_or_super OP_TK CP_TK SC_TK | |
22eed1e6 APB |
1070 | { |
1071 | $$ = build_method_invocation ($1, NULL_TREE); | |
1072 | $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$); | |
1073 | $$ = java_method_add_stmt (current_function_decl, $$); | |
1074 | } | |
e04a16fb | 1075 | | this_or_super OP_TK argument_list CP_TK SC_TK |
22eed1e6 APB |
1076 | { |
1077 | $$ = build_method_invocation ($1, $3); | |
1078 | $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$); | |
1079 | $$ = java_method_add_stmt (current_function_decl, $$); | |
1080 | } | |
e04a16fb AG |
1081 | /* Added, JDK1.1 inner classes. Modified because the rule |
1082 | 'primary' couldn't work. */ | |
1083 | | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK | |
b67d701b | 1084 | {$$ = parse_jdk1_1_error ("explicit constructor invocation"); } |
e04a16fb | 1085 | | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK |
b67d701b | 1086 | {$$ = parse_jdk1_1_error ("explicit constructor invocation"); } |
e04a16fb AG |
1087 | ; |
1088 | ||
1089 | this_or_super: /* Added, simplifies error diagnostics */ | |
1090 | THIS_TK | |
1091 | { | |
9ee9b555 | 1092 | tree wfl = build_wfl_node (this_identifier_node); |
e04a16fb AG |
1093 | EXPR_WFL_LINECOL (wfl) = $1.location; |
1094 | $$ = wfl; | |
1095 | } | |
1096 | | SUPER_TK | |
1097 | { | |
9ee9b555 | 1098 | tree wfl = build_wfl_node (super_identifier_node); |
e04a16fb AG |
1099 | EXPR_WFL_LINECOL (wfl) = $1.location; |
1100 | $$ = wfl; | |
1101 | } | |
1102 | ; | |
1103 | ||
1104 | /* 19.9 Productions from 9: Interfaces */ | |
1105 | /* 19.9.1 Productions from 9.1: Interfaces Declarations */ | |
1106 | interface_declaration: | |
1107 | INTERFACE_TK identifier | |
1108 | { create_interface (0, $2, NULL_TREE); } | |
1109 | interface_body | |
1110 | { | |
1111 | $$ = $4; | |
1112 | } | |
1113 | | modifiers INTERFACE_TK identifier | |
1114 | { create_interface ($1, $3, NULL_TREE); } | |
1115 | interface_body | |
1116 | { | |
1117 | $$ = $5; | |
1118 | } | |
1119 | | INTERFACE_TK identifier extends_interfaces | |
1120 | { create_interface (0, $2, $3); } | |
1121 | interface_body | |
1122 | { | |
1123 | $$ = $5; | |
1124 | } | |
1125 | | modifiers INTERFACE_TK identifier extends_interfaces | |
1126 | { create_interface ($1, $3, $4); } | |
1127 | interface_body | |
1128 | { | |
1129 | $$ = $6; | |
1130 | } | |
1131 | | INTERFACE_TK identifier error | |
0b4d333e | 1132 | {yyerror ("'{' expected"); RECOVER;} |
e04a16fb | 1133 | | modifiers INTERFACE_TK identifier error |
0b4d333e | 1134 | {yyerror ("'{' expected"); RECOVER;} |
e04a16fb AG |
1135 | ; |
1136 | ||
1137 | extends_interfaces: | |
1138 | EXTENDS_TK interface_type | |
1139 | { | |
1140 | ctxp->interface_number = 1; | |
1141 | $$ = build_tree_list ($2, NULL_TREE); | |
1142 | } | |
1143 | | extends_interfaces C_TK interface_type | |
1144 | { | |
1145 | ctxp->interface_number++; | |
1146 | $$ = chainon ($1, build_tree_list ($3, NULL_TREE)); | |
1147 | } | |
1148 | | EXTENDS_TK error | |
1149 | {yyerror ("Invalid interface type"); RECOVER;} | |
1150 | | extends_interfaces C_TK error | |
1151 | {yyerror ("Missing term"); RECOVER;} | |
1152 | ; | |
1153 | ||
1154 | interface_body: | |
1155 | OCB_TK CCB_TK | |
1156 | { $$ = NULL_TREE; } | |
1157 | | OCB_TK interface_member_declarations CCB_TK | |
1158 | { $$ = NULL_TREE; } | |
1159 | ; | |
1160 | ||
1161 | interface_member_declarations: | |
1162 | interface_member_declaration | |
1163 | | interface_member_declarations interface_member_declaration | |
1164 | ; | |
1165 | ||
1166 | interface_member_declaration: | |
1167 | constant_declaration | |
1168 | | abstract_method_declaration | |
1169 | | class_declaration /* Added, JDK1.1 inner classes */ | |
b67d701b | 1170 | { $$ = parse_jdk1_1_error ("inner class declaration"); } |
e04a16fb | 1171 | | interface_declaration /* Added, JDK1.1 inner classes */ |
b67d701b | 1172 | { $$ = parse_jdk1_1_error ("inner interface declaration"); } |
e04a16fb AG |
1173 | ; |
1174 | ||
1175 | constant_declaration: | |
1176 | field_declaration | |
1177 | ; | |
1178 | ||
1179 | abstract_method_declaration: | |
1180 | method_header SC_TK | |
1181 | { | |
1182 | check_abstract_method_header ($1); | |
1183 | current_function_decl = NULL_TREE; /* FIXME ? */ | |
1184 | } | |
1185 | | method_header error | |
1186 | {yyerror ("';' expected"); RECOVER;} | |
1187 | ; | |
1188 | ||
1189 | /* 19.10 Productions from 10: Arrays */ | |
1190 | array_initializer: | |
1191 | OCB_TK CCB_TK | |
1179ebc2 | 1192 | { $$ = build_new_array_init ($1.location, NULL_TREE); } |
e04a16fb | 1193 | | OCB_TK variable_initializers CCB_TK |
f8976021 | 1194 | { $$ = build_new_array_init ($1.location, $2); } |
e04a16fb | 1195 | | OCB_TK variable_initializers C_TK CCB_TK |
f8976021 | 1196 | { $$ = build_new_array_init ($1.location, $2); } |
e04a16fb AG |
1197 | ; |
1198 | ||
1199 | variable_initializers: | |
1200 | variable_initializer | |
f8976021 APB |
1201 | { |
1202 | $$ = tree_cons (maybe_build_array_element_wfl ($1), | |
1203 | $1, NULL_TREE); | |
1204 | } | |
e04a16fb | 1205 | | variable_initializers C_TK variable_initializer |
1179ebc2 APB |
1206 | { |
1207 | $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1); | |
1208 | } | |
e04a16fb AG |
1209 | | variable_initializers C_TK error |
1210 | {yyerror ("Missing term"); RECOVER;} | |
1211 | ; | |
1212 | ||
1213 | /* 19.11 Production from 14: Blocks and Statements */ | |
1214 | block: | |
1215 | OCB_TK CCB_TK | |
7f10c2e2 APB |
1216 | { |
1217 | /* Store the location of the `}' when doing xrefs */ | |
1218 | if (current_function_decl && flag_emit_xref) | |
1219 | DECL_END_SOURCE_LINE (current_function_decl) = | |
1220 | EXPR_WFL_ADD_COL ($2.location, 1); | |
1221 | $$ = empty_stmt_node; | |
1222 | } | |
22eed1e6 APB |
1223 | | block_begin block_statements block_end |
1224 | { $$ = $3; } | |
1225 | ; | |
1226 | ||
1227 | block_begin: | |
1228 | OCB_TK | |
e04a16fb | 1229 | { enter_block (); } |
22eed1e6 APB |
1230 | ; |
1231 | ||
1232 | block_end: | |
e04a16fb AG |
1233 | CCB_TK |
1234 | { | |
1235 | maybe_absorb_scoping_blocks (); | |
7f10c2e2 APB |
1236 | /* Store the location of the `}' when doing xrefs */ |
1237 | if (current_function_decl && flag_emit_xref) | |
1238 | DECL_END_SOURCE_LINE (current_function_decl) = | |
1239 | EXPR_WFL_ADD_COL ($1.location, 1); | |
e04a16fb AG |
1240 | $$ = exit_block (); |
1241 | } | |
1242 | ; | |
1243 | ||
1244 | block_statements: | |
1245 | block_statement | |
1246 | | block_statements block_statement | |
1247 | ; | |
1248 | ||
1249 | block_statement: | |
1250 | local_variable_declaration_statement | |
1251 | | statement | |
15fdcfe9 | 1252 | { java_method_add_stmt (current_function_decl, $1); } |
e04a16fb | 1253 | | class_declaration /* Added, JDK1.1 inner classes */ |
15fdcfe9 | 1254 | { parse_jdk1_1_error ("inner class declaration"); } |
e04a16fb AG |
1255 | ; |
1256 | ||
1257 | local_variable_declaration_statement: | |
1258 | local_variable_declaration SC_TK /* Can't catch missing ';' here */ | |
1259 | ; | |
1260 | ||
1261 | local_variable_declaration: | |
1262 | type variable_declarators | |
1263 | { declare_local_variables (0, $1, $2); } | |
1264 | | modifiers type variable_declarators /* Added, JDK1.1 final locals */ | |
1265 | { declare_local_variables ($1, $2, $3); } | |
1266 | ; | |
1267 | ||
1268 | statement: | |
1269 | statement_without_trailing_substatement | |
1270 | | labeled_statement | |
e04a16fb | 1271 | | if_then_statement |
e04a16fb | 1272 | | if_then_else_statement |
e04a16fb | 1273 | | while_statement |
e04a16fb | 1274 | | for_statement |
cd9643f7 | 1275 | { $$ = exit_block (); } |
e04a16fb AG |
1276 | ; |
1277 | ||
1278 | statement_nsi: | |
1279 | statement_without_trailing_substatement | |
1280 | | labeled_statement_nsi | |
e04a16fb | 1281 | | if_then_else_statement_nsi |
e04a16fb | 1282 | | while_statement_nsi |
e04a16fb | 1283 | | for_statement_nsi |
9dd939b2 | 1284 | { $$ = exit_block (); } |
e04a16fb AG |
1285 | ; |
1286 | ||
1287 | statement_without_trailing_substatement: | |
1288 | block | |
e04a16fb | 1289 | | empty_statement |
e04a16fb | 1290 | | expression_statement |
e04a16fb | 1291 | | switch_statement |
e04a16fb | 1292 | | do_statement |
e04a16fb | 1293 | | break_statement |
e04a16fb | 1294 | | continue_statement |
e04a16fb AG |
1295 | | return_statement |
1296 | | synchronized_statement | |
e04a16fb | 1297 | | throw_statement |
e04a16fb | 1298 | | try_statement |
e04a16fb AG |
1299 | ; |
1300 | ||
1301 | empty_statement: | |
1302 | SC_TK | |
9bbc7d9f | 1303 | { $$ = empty_stmt_node; } |
e04a16fb AG |
1304 | ; |
1305 | ||
1306 | label_decl: | |
1307 | identifier REL_CL_TK | |
1308 | { | |
1309 | $$ = build_labeled_block (EXPR_WFL_LINECOL ($1), | |
0a2138e2 | 1310 | EXPR_WFL_NODE ($1)); |
e04a16fb AG |
1311 | pushlevel (2); |
1312 | push_labeled_block ($$); | |
1313 | PUSH_LABELED_BLOCK ($$); | |
1314 | } | |
1315 | ; | |
1316 | ||
1317 | labeled_statement: | |
1318 | label_decl statement | |
b635eb2f | 1319 | { $$ = finish_labeled_statement ($1, $2); } |
e04a16fb AG |
1320 | | identifier error |
1321 | {yyerror ("':' expected"); RECOVER;} | |
1322 | ; | |
1323 | ||
1324 | labeled_statement_nsi: | |
1325 | label_decl statement_nsi | |
b635eb2f | 1326 | { $$ = finish_labeled_statement ($1, $2); } |
e04a16fb AG |
1327 | ; |
1328 | ||
1329 | /* We concentrate here a bunch of error handling rules that we couldn't write | |
1330 | earlier, because expression_statement catches a missing ';'. */ | |
1331 | expression_statement: | |
1332 | statement_expression SC_TK | |
1333 | { | |
1334 | /* We have a statement. Generate a WFL around it so | |
1335 | we can debug it */ | |
1336 | $$ = build_expr_wfl ($1, input_filename, lineno, 0); | |
1337 | /* We know we have a statement, so set the debug | |
1338 | info to be eventually generate here. */ | |
1339 | $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$); | |
1340 | } | |
1341 | | error SC_TK | |
1342 | { | |
1343 | if (ctxp->prevent_ese != lineno) | |
1344 | yyerror ("Invalid expression statement"); | |
1345 | DRECOVER (expr_stmt); | |
1346 | } | |
1347 | | error OCB_TK | |
1348 | { | |
1349 | if (ctxp->prevent_ese != lineno) | |
1350 | yyerror ("Invalid expression statement"); | |
1351 | DRECOVER (expr_stmt); | |
1352 | } | |
1353 | | error CCB_TK | |
1354 | { | |
1355 | if (ctxp->prevent_ese != lineno) | |
1356 | yyerror ("Invalid expression statement"); | |
1357 | DRECOVER (expr_stmt); | |
1358 | } | |
1359 | | this_or_super OP_TK error | |
1360 | {yyerror ("')' expected"); RECOVER;} | |
1361 | | this_or_super OP_TK CP_TK error | |
22eed1e6 | 1362 | { |
8119c720 | 1363 | parse_ctor_invocation_error (); |
22eed1e6 APB |
1364 | RECOVER; |
1365 | } | |
e04a16fb AG |
1366 | | this_or_super OP_TK argument_list error |
1367 | {yyerror ("')' expected"); RECOVER;} | |
1368 | | this_or_super OP_TK argument_list CP_TK error | |
22eed1e6 | 1369 | { |
8119c720 | 1370 | parse_ctor_invocation_error (); |
22eed1e6 APB |
1371 | RECOVER; |
1372 | } | |
e04a16fb AG |
1373 | | name DOT_TK SUPER_TK error |
1374 | {yyerror ("'(' expected"); RECOVER;} | |
1375 | | name DOT_TK SUPER_TK OP_TK error | |
1376 | {yyerror ("')' expected"); RECOVER;} | |
1377 | | name DOT_TK SUPER_TK OP_TK argument_list error | |
1378 | {yyerror ("')' expected"); RECOVER;} | |
1379 | | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error | |
1380 | {yyerror ("';' expected"); RECOVER;} | |
1381 | | name DOT_TK SUPER_TK OP_TK CP_TK error | |
1382 | {yyerror ("';' expected"); RECOVER;} | |
1383 | ; | |
1384 | ||
1385 | statement_expression: | |
1386 | assignment | |
1387 | | pre_increment_expression | |
e04a16fb | 1388 | | pre_decrement_expression |
e04a16fb | 1389 | | post_increment_expression |
e04a16fb | 1390 | | post_decrement_expression |
e04a16fb AG |
1391 | | method_invocation |
1392 | | class_instance_creation_expression | |
e04a16fb AG |
1393 | ; |
1394 | ||
1395 | if_then_statement: | |
1396 | IF_TK OP_TK expression CP_TK statement | |
2aa11e97 APB |
1397 | { |
1398 | $$ = build_if_else_statement ($2.location, $3, | |
1399 | $5, NULL_TREE); | |
1400 | } | |
e04a16fb AG |
1401 | | IF_TK error |
1402 | {yyerror ("'(' expected"); RECOVER;} | |
1403 | | IF_TK OP_TK error | |
1404 | {yyerror ("Missing term"); RECOVER;} | |
1405 | | IF_TK OP_TK expression error | |
1406 | {yyerror ("')' expected"); RECOVER;} | |
1407 | ; | |
1408 | ||
1409 | if_then_else_statement: | |
1410 | IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement | |
2aa11e97 | 1411 | { $$ = build_if_else_statement ($2.location, $3, $5, $7); } |
e04a16fb AG |
1412 | ; |
1413 | ||
1414 | if_then_else_statement_nsi: | |
1415 | IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi | |
2aa11e97 | 1416 | { $$ = build_if_else_statement ($2.location, $3, $5, $7); } |
e04a16fb AG |
1417 | ; |
1418 | ||
1419 | switch_statement: | |
15fdcfe9 PB |
1420 | switch_expression |
1421 | { | |
1422 | enter_block (); | |
1423 | } | |
1424 | switch_block | |
b67d701b | 1425 | { |
15fdcfe9 | 1426 | /* Make into "proper list" of COMPOUND_EXPRs. |
f8976021 APB |
1427 | I.e. make the last statment also have its own |
1428 | COMPOUND_EXPR. */ | |
15fdcfe9 PB |
1429 | maybe_absorb_scoping_blocks (); |
1430 | TREE_OPERAND ($1, 1) = exit_block (); | |
b67d701b PB |
1431 | $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1); |
1432 | } | |
1433 | ; | |
1434 | ||
1435 | switch_expression: | |
1436 | SWITCH_TK OP_TK expression CP_TK | |
1437 | { | |
1438 | $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE); | |
1439 | EXPR_WFL_LINECOL ($$) = $2.location; | |
1440 | } | |
e04a16fb AG |
1441 | | SWITCH_TK error |
1442 | {yyerror ("'(' expected"); RECOVER;} | |
1443 | | SWITCH_TK OP_TK error | |
1444 | {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);} | |
1445 | | SWITCH_TK OP_TK expression CP_TK error | |
1446 | {yyerror ("'{' expected"); RECOVER;} | |
1447 | ; | |
1448 | ||
f8976021 APB |
1449 | /* Default assignment is there to avoid type node on switch_block |
1450 | node. */ | |
1451 | ||
e04a16fb AG |
1452 | switch_block: |
1453 | OCB_TK CCB_TK | |
f8976021 | 1454 | { $$ = NULL_TREE; } |
e04a16fb | 1455 | | OCB_TK switch_labels CCB_TK |
f8976021 | 1456 | { $$ = NULL_TREE; } |
e04a16fb | 1457 | | OCB_TK switch_block_statement_groups CCB_TK |
f8976021 | 1458 | { $$ = NULL_TREE; } |
e04a16fb | 1459 | | OCB_TK switch_block_statement_groups switch_labels CCB_TK |
f8976021 | 1460 | { $$ = NULL_TREE; } |
e04a16fb AG |
1461 | ; |
1462 | ||
1463 | switch_block_statement_groups: | |
1464 | switch_block_statement_group | |
1465 | | switch_block_statement_groups switch_block_statement_group | |
1466 | ; | |
1467 | ||
1468 | switch_block_statement_group: | |
15fdcfe9 | 1469 | switch_labels block_statements |
e04a16fb AG |
1470 | ; |
1471 | ||
e04a16fb AG |
1472 | switch_labels: |
1473 | switch_label | |
1474 | | switch_labels switch_label | |
1475 | ; | |
1476 | ||
1477 | switch_label: | |
1478 | CASE_TK constant_expression REL_CL_TK | |
b67d701b | 1479 | { |
15fdcfe9 PB |
1480 | tree lab = build1 (CASE_EXPR, NULL_TREE, $2); |
1481 | EXPR_WFL_LINECOL (lab) = $1.location; | |
1482 | java_method_add_stmt (current_function_decl, lab); | |
b67d701b | 1483 | } |
e04a16fb | 1484 | | DEFAULT_TK REL_CL_TK |
b67d701b | 1485 | { |
15fdcfe9 PB |
1486 | tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE); |
1487 | EXPR_WFL_LINECOL (lab) = $1.location; | |
1488 | java_method_add_stmt (current_function_decl, lab); | |
b67d701b | 1489 | } |
e04a16fb AG |
1490 | | CASE_TK error |
1491 | {yyerror ("Missing or invalid constant expression"); RECOVER;} | |
1492 | | CASE_TK constant_expression error | |
1493 | {yyerror ("':' expected"); RECOVER;} | |
1494 | | DEFAULT_TK error | |
1495 | {yyerror ("':' expected"); RECOVER;} | |
1496 | ; | |
1497 | ||
1498 | while_expression: | |
1499 | WHILE_TK OP_TK expression CP_TK | |
1500 | { | |
1501 | tree body = build_loop_body ($2.location, $3, 0); | |
1502 | $$ = build_new_loop (body); | |
1503 | } | |
1504 | ; | |
1505 | ||
1506 | while_statement: | |
1507 | while_expression statement | |
b635eb2f | 1508 | { $$ = finish_loop_body (0, NULL_TREE, $2, 0); } |
e04a16fb AG |
1509 | | WHILE_TK error |
1510 | {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;} | |
1511 | | WHILE_TK OP_TK error | |
1512 | {yyerror ("Missing term and ')' expected"); RECOVER;} | |
1513 | | WHILE_TK OP_TK expression error | |
1514 | {yyerror ("')' expected"); RECOVER;} | |
1515 | ; | |
1516 | ||
1517 | while_statement_nsi: | |
1518 | while_expression statement_nsi | |
b635eb2f | 1519 | { $$ = finish_loop_body (0, NULL_TREE, $2, 0); } |
e04a16fb AG |
1520 | ; |
1521 | ||
1522 | do_statement_begin: | |
1523 | DO_TK | |
1524 | { | |
1525 | tree body = build_loop_body (0, NULL_TREE, 1); | |
1526 | $$ = build_new_loop (body); | |
1527 | } | |
1528 | /* Need error handing here. FIXME */ | |
1529 | ; | |
1530 | ||
1531 | do_statement: | |
1532 | do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK | |
b635eb2f | 1533 | { $$ = finish_loop_body ($4.location, $5, $2, 1); } |
e04a16fb AG |
1534 | ; |
1535 | ||
1536 | for_statement: | |
1537 | for_begin SC_TK expression SC_TK for_update CP_TK statement | |
b635eb2f | 1538 | { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7); } |
e04a16fb AG |
1539 | | for_begin SC_TK SC_TK for_update CP_TK statement |
1540 | { | |
b635eb2f | 1541 | $$ = finish_for_loop (0, NULL_TREE, $4, $6); |
e04a16fb AG |
1542 | /* We have not condition, so we get rid of the EXIT_EXPR */ |
1543 | LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = | |
9bbc7d9f | 1544 | empty_stmt_node; |
e04a16fb AG |
1545 | } |
1546 | | for_begin SC_TK error | |
1547 | {yyerror ("Invalid control expression"); RECOVER;} | |
1548 | | for_begin SC_TK expression SC_TK error | |
1549 | {yyerror ("Invalid update expression"); RECOVER;} | |
1550 | | for_begin SC_TK SC_TK error | |
1551 | {yyerror ("Invalid update expression"); RECOVER;} | |
1552 | ; | |
1553 | ||
1554 | for_statement_nsi: | |
1555 | for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi | |
b635eb2f | 1556 | { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);} |
e04a16fb AG |
1557 | | for_begin SC_TK SC_TK for_update CP_TK statement_nsi |
1558 | { | |
b635eb2f | 1559 | $$ = finish_for_loop (0, NULL_TREE, $4, $6); |
e04a16fb AG |
1560 | /* We have not condition, so we get rid of the EXIT_EXPR */ |
1561 | LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = | |
9bbc7d9f | 1562 | empty_stmt_node; |
e04a16fb AG |
1563 | } |
1564 | ; | |
1565 | ||
1566 | for_header: | |
1567 | FOR_TK OP_TK | |
1568 | { | |
1569 | /* This scope defined for local variable that may be | |
1570 | defined within the scope of the for loop */ | |
1571 | enter_block (); | |
1572 | } | |
1573 | | FOR_TK error | |
1574 | {yyerror ("'(' expected"); DRECOVER(for_1);} | |
1575 | | FOR_TK OP_TK error | |
1576 | {yyerror ("Invalid init statement"); RECOVER;} | |
1577 | ; | |
1578 | ||
1579 | for_begin: | |
1580 | for_header for_init | |
1581 | { | |
1582 | /* We now declare the loop body. The loop is | |
1583 | declared as a for loop. */ | |
1584 | tree body = build_loop_body (0, NULL_TREE, 0); | |
1585 | $$ = build_new_loop (body); | |
1586 | IS_FOR_LOOP_P ($$) = 1; | |
1587 | /* The loop is added to the current block the for | |
1588 | statement is defined within */ | |
1589 | java_method_add_stmt (current_function_decl, $$); | |
1590 | } | |
1591 | ; | |
1592 | for_init: /* Can be empty */ | |
9bbc7d9f | 1593 | { $$ = empty_stmt_node; } |
e04a16fb AG |
1594 | | statement_expression_list |
1595 | { | |
1596 | /* Init statement recorded within the previously | |
1597 | defined block scope */ | |
1598 | $$ = java_method_add_stmt (current_function_decl, $1); | |
1599 | } | |
1600 | | local_variable_declaration | |
1601 | { | |
1602 | /* Local variable are recorded within the previously | |
1603 | defined block scope */ | |
1604 | $$ = NULL_TREE; | |
1605 | } | |
1606 | | statement_expression_list error | |
1607 | {yyerror ("';' expected"); DRECOVER(for_init_1);} | |
1608 | ; | |
1609 | ||
1610 | for_update: /* Can be empty */ | |
9bbc7d9f | 1611 | {$$ = empty_stmt_node;} |
e04a16fb AG |
1612 | | statement_expression_list |
1613 | { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); } | |
1614 | ; | |
1615 | ||
1616 | statement_expression_list: | |
1617 | statement_expression | |
1618 | { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); } | |
1619 | | statement_expression_list C_TK statement_expression | |
1620 | { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); } | |
1621 | | statement_expression_list C_TK error | |
1622 | {yyerror ("Missing term"); RECOVER;} | |
1623 | ; | |
1624 | ||
1625 | break_statement: | |
1626 | BREAK_TK SC_TK | |
1627 | { $$ = build_bc_statement ($1.location, 1, NULL_TREE); } | |
1628 | | BREAK_TK identifier SC_TK | |
1629 | { $$ = build_bc_statement ($1.location, 1, $2); } | |
1630 | | BREAK_TK error | |
1631 | {yyerror ("Missing term"); RECOVER;} | |
1632 | | BREAK_TK identifier error | |
1633 | {yyerror ("';' expected"); RECOVER;} | |
1634 | ; | |
1635 | ||
1636 | continue_statement: | |
1637 | CONTINUE_TK SC_TK | |
1638 | { $$ = build_bc_statement ($1.location, 0, NULL_TREE); } | |
1639 | | CONTINUE_TK identifier SC_TK | |
1640 | { $$ = build_bc_statement ($1.location, 0, $2); } | |
1641 | | CONTINUE_TK error | |
1642 | {yyerror ("Missing term"); RECOVER;} | |
1643 | | CONTINUE_TK identifier error | |
1644 | {yyerror ("';' expected"); RECOVER;} | |
1645 | ; | |
1646 | ||
1647 | return_statement: | |
1648 | RETURN_TK SC_TK | |
1649 | { $$ = build_return ($1.location, NULL_TREE); } | |
1650 | | RETURN_TK expression SC_TK | |
1651 | { $$ = build_return ($1.location, $2); } | |
1652 | | RETURN_TK error | |
1653 | {yyerror ("Missing term"); RECOVER;} | |
1654 | | RETURN_TK expression error | |
1655 | {yyerror ("';' expected"); RECOVER;} | |
1656 | ; | |
1657 | ||
1658 | throw_statement: | |
1659 | THROW_TK expression SC_TK | |
b9f7e36c APB |
1660 | { |
1661 | $$ = build1 (THROW_EXPR, NULL_TREE, $2); | |
1662 | EXPR_WFL_LINECOL ($$) = $1.location; | |
1663 | } | |
e04a16fb AG |
1664 | | THROW_TK error |
1665 | {yyerror ("Missing term"); RECOVER;} | |
1666 | | THROW_TK expression error | |
1667 | {yyerror ("';' expected"); RECOVER;} | |
1668 | ; | |
1669 | ||
1670 | synchronized_statement: | |
1671 | synchronized OP_TK expression CP_TK block | |
b9f7e36c APB |
1672 | { |
1673 | $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5); | |
1674 | EXPR_WFL_LINECOL ($$) = | |
1675 | EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK)); | |
1676 | } | |
e04a16fb AG |
1677 | | synchronized OP_TK expression CP_TK error |
1678 | {yyerror ("'{' expected"); RECOVER;} | |
1679 | | synchronized error | |
1680 | {yyerror ("'(' expected"); RECOVER;} | |
1681 | | synchronized OP_TK error CP_TK | |
1682 | {yyerror ("Missing term"); RECOVER;} | |
1683 | | synchronized OP_TK error | |
1684 | {yyerror ("Missing term"); RECOVER;} | |
1685 | ; | |
1686 | ||
b9f7e36c | 1687 | synchronized: |
efa0a23f | 1688 | modifiers |
e04a16fb | 1689 | { |
781b0558 KG |
1690 | check_modifiers ( |
1691 | "Illegal modifier `%s'. Only `synchronized' was expected here", | |
efa0a23f APB |
1692 | $1, ACC_SYNCHRONIZED); |
1693 | if ($1 != ACC_SYNCHRONIZED) | |
1694 | MODIFIER_WFL (SYNCHRONIZED_TK) = | |
1695 | build_wfl_node (NULL_TREE); | |
e04a16fb AG |
1696 | } |
1697 | ; | |
1698 | ||
1699 | try_statement: | |
1700 | TRY_TK block catches | |
a7d8d81f | 1701 | { $$ = build_try_statement ($1.location, $2, $3); } |
e04a16fb | 1702 | | TRY_TK block finally |
a7d8d81f | 1703 | { $$ = build_try_finally_statement ($1.location, $2, $3); } |
e04a16fb | 1704 | | TRY_TK block catches finally |
2aa11e97 APB |
1705 | { $$ = build_try_finally_statement |
1706 | ($1.location, build_try_statement ($1.location, | |
1707 | $2, $3), $4); | |
1708 | } | |
e04a16fb AG |
1709 | | TRY_TK error |
1710 | {yyerror ("'{' expected"); DRECOVER (try_statement);} | |
1711 | ; | |
1712 | ||
1713 | catches: | |
1714 | catch_clause | |
1715 | | catches catch_clause | |
b67d701b PB |
1716 | { |
1717 | TREE_CHAIN ($2) = $1; | |
1718 | $$ = $2; | |
1719 | } | |
e04a16fb AG |
1720 | ; |
1721 | ||
1722 | catch_clause: | |
b67d701b PB |
1723 | catch_clause_parameter block |
1724 | { | |
1725 | java_method_add_stmt (current_function_decl, $2); | |
1726 | exit_block (); | |
1727 | $$ = $1; | |
1728 | } | |
1729 | ||
1730 | catch_clause_parameter: | |
1731 | CATCH_TK OP_TK formal_parameter CP_TK | |
1732 | { | |
1733 | /* We add a block to define a scope for | |
1734 | formal_parameter (CCBP). The formal parameter is | |
1735 | declared initialized by the appropriate function | |
1736 | call */ | |
1737 | tree ccpb = enter_block (); | |
1738 | tree init = build_assignment (ASSIGN_TK, $2.location, | |
1739 | TREE_PURPOSE ($3), | |
1740 | soft_exceptioninfo_call_node); | |
1741 | declare_local_variables (0, TREE_VALUE ($3), | |
1742 | build_tree_list (TREE_PURPOSE ($3), | |
1743 | init)); | |
1744 | $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb); | |
1745 | EXPR_WFL_LINECOL ($$) = $1.location; | |
1746 | } | |
e04a16fb | 1747 | | CATCH_TK error |
97f30284 | 1748 | {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;} |
e04a16fb | 1749 | | CATCH_TK OP_TK error |
97f30284 APB |
1750 | { |
1751 | yyerror ("Missing term or ')' expected"); | |
1752 | RECOVER; $$ = NULL_TREE; | |
1753 | } | |
b67d701b | 1754 | | CATCH_TK OP_TK error CP_TK /* That's for () */ |
97f30284 | 1755 | {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;} |
e04a16fb AG |
1756 | ; |
1757 | ||
1758 | finally: | |
1759 | FINALLY_TK block | |
a7d8d81f | 1760 | { $$ = $2; } |
e04a16fb AG |
1761 | | FINALLY_TK error |
1762 | {yyerror ("'{' expected"); RECOVER; } | |
1763 | ; | |
1764 | ||
1765 | /* 19.12 Production from 15: Expressions */ | |
1766 | primary: | |
1767 | primary_no_new_array | |
1768 | | array_creation_expression | |
1769 | ; | |
1770 | ||
1771 | primary_no_new_array: | |
1772 | literal | |
1773 | | THIS_TK | |
1774 | { $$ = build_this ($1.location); } | |
1775 | | OP_TK expression CP_TK | |
1776 | {$$ = $2;} | |
1777 | | class_instance_creation_expression | |
1778 | | field_access | |
1779 | | method_invocation | |
1780 | | array_access | |
1781 | /* type DOT_TK CLASS_TK doens't work. So we split the rule | |
1782 | 'type' into its components. Missing is something for array, | |
1783 | which will complete the reference_type part. FIXME */ | |
1784 | | name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */ | |
c877974e | 1785 | { $$ = parse_jdk1_1_error ("named class literals"); } |
e04a16fb | 1786 | | primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */ |
c877974e | 1787 | { $$ = build_class_ref ($1); } |
e04a16fb | 1788 | | VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */ |
c877974e | 1789 | { $$ = build_class_ref (void_type_node); } |
e04a16fb AG |
1790 | /* Added, JDK1.1 inner classes. Documentation is wrong |
1791 | refering to a 'ClassName' (class_name) rule that doesn't | |
1792 | exist. Used name instead. */ | |
1793 | | name DOT_TK THIS_TK | |
b67d701b | 1794 | { $$ = parse_jdk1_1_error ("class literals"); } |
e04a16fb AG |
1795 | | OP_TK expression error |
1796 | {yyerror ("')' expected"); RECOVER;} | |
1797 | | name DOT_TK error | |
1798 | {yyerror ("'class' or 'this' expected" ); RECOVER;} | |
1799 | | primitive_type DOT_TK error | |
1800 | {yyerror ("'class' expected" ); RECOVER;} | |
1801 | | VOID_TK DOT_TK error | |
1802 | {yyerror ("'class' expected" ); RECOVER;} | |
1803 | ; | |
1804 | ||
1805 | class_instance_creation_expression: | |
1806 | NEW_TK class_type OP_TK argument_list CP_TK | |
b67d701b | 1807 | { $$ = build_new_invocation ($2, $4); } |
e04a16fb | 1808 | | NEW_TK class_type OP_TK CP_TK |
b67d701b | 1809 | { $$ = build_new_invocation ($2, NULL_TREE); } |
e04a16fb AG |
1810 | /* Added, JDK1.1 inner classes but modified to use |
1811 | 'class_type' instead of 'TypeName' (type_name) mentionned | |
1812 | in the documentation but doesn't exist. */ | |
1813 | | NEW_TK class_type OP_TK argument_list CP_TK class_body | |
b67d701b | 1814 | { $$ = parse_jdk1_1_error ("inner class instance creation"); } |
e04a16fb | 1815 | | NEW_TK class_type OP_TK CP_TK class_body |
b67d701b | 1816 | { $$ = parse_jdk1_1_error ("inner class instance creation"); } |
e04a16fb AG |
1817 | /* Added, JDK1.1 inner classes, modified to use name or |
1818 | primary instead of primary solely which couldn't work in | |
1819 | all situations. */ | |
1820 | | something_dot_new identifier OP_TK CP_TK | |
1821 | | something_dot_new identifier OP_TK CP_TK class_body | |
1822 | | something_dot_new identifier OP_TK argument_list CP_TK | |
1823 | | something_dot_new identifier OP_TK argument_list CP_TK class_body | |
1824 | | NEW_TK error SC_TK | |
1825 | {yyerror ("'(' expected"); DRECOVER(new_1);} | |
1826 | | NEW_TK class_type error | |
1827 | {yyerror ("'(' expected"); RECOVER;} | |
1828 | | NEW_TK class_type OP_TK error | |
1829 | {yyerror ("')' or term expected"); RECOVER;} | |
1830 | | NEW_TK class_type OP_TK argument_list error | |
1831 | {yyerror ("')' expected"); RECOVER;} | |
1832 | | something_dot_new error | |
1833 | {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;} | |
1834 | | something_dot_new identifier error | |
1835 | {yyerror ("'(' expected"); RECOVER;} | |
1836 | ; | |
1837 | ||
1838 | something_dot_new: /* Added, not part of the specs. */ | |
1839 | name DOT_TK NEW_TK | |
1840 | | primary DOT_TK NEW_TK | |
1841 | ; | |
1842 | ||
1843 | argument_list: | |
1844 | expression | |
1845 | { | |
1846 | $$ = tree_cons (NULL_TREE, $1, NULL_TREE); | |
1847 | ctxp->formal_parameter_number = 1; | |
1848 | } | |
1849 | | argument_list C_TK expression | |
1850 | { | |
1851 | ctxp->formal_parameter_number += 1; | |
1852 | $$ = tree_cons (NULL_TREE, $3, $1); | |
1853 | } | |
1854 | | argument_list C_TK error | |
1855 | {yyerror ("Missing term"); RECOVER;} | |
1856 | ; | |
1857 | ||
1858 | array_creation_expression: | |
1859 | NEW_TK primitive_type dim_exprs | |
1860 | { $$ = build_newarray_node ($2, $3, 0); } | |
1861 | | NEW_TK class_or_interface_type dim_exprs | |
1862 | { $$ = build_newarray_node ($2, $3, 0); } | |
1863 | | NEW_TK primitive_type dim_exprs dims | |
ba179f9f | 1864 | { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));} |
e04a16fb | 1865 | | NEW_TK class_or_interface_type dim_exprs dims |
ba179f9f | 1866 | { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));} |
e04a16fb AG |
1867 | /* Added, JDK1.1 anonymous array. Initial documentation rule |
1868 | modified */ | |
1869 | | NEW_TK class_or_interface_type dims array_initializer | |
b67d701b | 1870 | { $$ = parse_jdk1_1_error ("anonymous array"); } |
e04a16fb | 1871 | | NEW_TK primitive_type dims array_initializer |
b67d701b | 1872 | { $$ = parse_jdk1_1_error ("anonymous array"); } |
e04a16fb AG |
1873 | | NEW_TK error CSB_TK |
1874 | {yyerror ("'[' expected"); DRECOVER ("]");} | |
1875 | | NEW_TK error OSB_TK | |
1876 | {yyerror ("']' expected"); RECOVER;} | |
1877 | ; | |
1878 | ||
1879 | dim_exprs: | |
1880 | dim_expr | |
1881 | { $$ = build_tree_list (NULL_TREE, $1); } | |
1882 | | dim_exprs dim_expr | |
1883 | { $$ = tree_cons (NULL_TREE, $2, $$); } | |
1884 | ; | |
1885 | ||
1886 | dim_expr: | |
1887 | OSB_TK expression CSB_TK | |
1888 | { | |
1889 | EXPR_WFL_LINECOL ($2) = $1.location; | |
1890 | $$ = $2; | |
1891 | } | |
1892 | | OSB_TK expression error | |
1893 | {yyerror ("']' expected"); RECOVER;} | |
1894 | | OSB_TK error | |
1895 | { | |
1896 | yyerror ("Missing term"); | |
1897 | yyerror ("']' expected"); | |
1898 | RECOVER; | |
1899 | } | |
1900 | ; | |
1901 | ||
1902 | dims: | |
1903 | OSB_TK CSB_TK | |
ba179f9f APB |
1904 | { |
1905 | int allocate = 0; | |
1906 | /* If not initialized, allocate memory for the osb | |
1907 | numbers stack */ | |
1908 | if (!ctxp->osb_limit) | |
1909 | { | |
1910 | allocate = ctxp->osb_limit = 32; | |
1911 | ctxp->osb_depth = -1; | |
1912 | } | |
1913 | /* If capacity overflown, reallocate a bigger chuck */ | |
1914 | else if (ctxp->osb_depth+1 == ctxp->osb_limit) | |
1915 | allocate = ctxp->osb_limit << 1; | |
1916 | ||
1917 | if (allocate) | |
1918 | { | |
1919 | allocate *= sizeof (int); | |
1920 | if (ctxp->osb_number) | |
1921 | ctxp->osb_number = (int *)xrealloc (ctxp->osb_number, | |
1922 | allocate); | |
1923 | else | |
1924 | ctxp->osb_number = (int *)xmalloc (allocate); | |
1925 | } | |
1926 | ctxp->osb_depth++; | |
1927 | CURRENT_OSB (ctxp) = 1; | |
1928 | } | |
e04a16fb | 1929 | | dims OSB_TK CSB_TK |
ba179f9f | 1930 | { CURRENT_OSB (ctxp)++; } |
e04a16fb AG |
1931 | | dims OSB_TK error |
1932 | { yyerror ("']' expected"); RECOVER;} | |
1933 | ; | |
1934 | ||
1935 | field_access: | |
1936 | primary DOT_TK identifier | |
1937 | { $$ = make_qualified_primary ($1, $3, $2.location); } | |
9bbc7d9f PB |
1938 | /* FIXME - REWRITE TO: |
1939 | { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */ | |
e04a16fb AG |
1940 | | SUPER_TK DOT_TK identifier |
1941 | { | |
1942 | tree super_wfl = | |
9ee9b555 | 1943 | build_wfl_node (super_identifier_node); |
e04a16fb AG |
1944 | EXPR_WFL_LINECOL (super_wfl) = $1.location; |
1945 | $$ = make_qualified_name (super_wfl, $3, $2.location); | |
1946 | } | |
1947 | | SUPER_TK error | |
1948 | {yyerror ("Field expected"); DRECOVER (super_field_acces);} | |
1949 | ; | |
1950 | ||
1951 | method_invocation: | |
1952 | name OP_TK CP_TK | |
1953 | { $$ = build_method_invocation ($1, NULL_TREE); } | |
1954 | | name OP_TK argument_list CP_TK | |
1955 | { $$ = build_method_invocation ($1, $3); } | |
1956 | | primary DOT_TK identifier OP_TK CP_TK | |
1957 | { | |
22eed1e6 APB |
1958 | if (TREE_CODE ($1) == THIS_EXPR) |
1959 | $$ = build_this_super_qualified_invocation | |
1960 | (1, $3, NULL_TREE, 0, $2.location); | |
1961 | else | |
1962 | { | |
1963 | tree invok = build_method_invocation ($3, NULL_TREE); | |
1964 | $$ = make_qualified_primary ($1, invok, $2.location); | |
1965 | } | |
e04a16fb AG |
1966 | } |
1967 | | primary DOT_TK identifier OP_TK argument_list CP_TK | |
1968 | { | |
22eed1e6 APB |
1969 | if (TREE_CODE ($1) == THIS_EXPR) |
1970 | $$ = build_this_super_qualified_invocation | |
1971 | (1, $3, $5, 0, $2.location); | |
1972 | else | |
1973 | { | |
1974 | tree invok = build_method_invocation ($3, $5); | |
1975 | $$ = make_qualified_primary ($1, invok, $2.location); | |
1976 | } | |
e04a16fb AG |
1977 | } |
1978 | | SUPER_TK DOT_TK identifier OP_TK CP_TK | |
22eed1e6 APB |
1979 | { |
1980 | $$ = build_this_super_qualified_invocation | |
1981 | (0, $3, NULL_TREE, $1.location, $2.location); | |
e04a16fb AG |
1982 | } |
1983 | | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK | |
1984 | { | |
22eed1e6 APB |
1985 | $$ = build_this_super_qualified_invocation |
1986 | (0, $3, $5, $1.location, $2.location); | |
e04a16fb AG |
1987 | } |
1988 | /* Screws up thing. I let it here until I'm convinced it can | |
1989 | be removed. FIXME | |
1990 | | primary DOT_TK error | |
1991 | {yyerror ("'(' expected"); DRECOVER(bad);} */ | |
1992 | | SUPER_TK DOT_TK error CP_TK | |
1993 | { yyerror ("'(' expected"); DRECOVER (method_invocation); } | |
1994 | | SUPER_TK DOT_TK error DOT_TK | |
1995 | { yyerror ("'(' expected"); DRECOVER (method_invocation); } | |
1996 | ; | |
1997 | ||
1998 | array_access: | |
1999 | name OSB_TK expression CSB_TK | |
2000 | { $$ = build_array_ref ($2.location, $1, $3); } | |
2001 | | primary_no_new_array OSB_TK expression CSB_TK | |
2002 | { $$ = build_array_ref ($2.location, $1, $3); } | |
2003 | | name OSB_TK error | |
2004 | { | |
2005 | yyerror ("Missing term and ']' expected"); | |
2006 | DRECOVER(array_access); | |
2007 | } | |
2008 | | name OSB_TK expression error | |
2009 | { | |
2010 | yyerror ("']' expected"); | |
2011 | DRECOVER(array_access); | |
2012 | } | |
2013 | | primary_no_new_array OSB_TK error | |
2014 | { | |
2015 | yyerror ("Missing term and ']' expected"); | |
2016 | DRECOVER(array_access); | |
2017 | } | |
2018 | | primary_no_new_array OSB_TK expression error | |
2019 | { | |
2020 | yyerror ("']' expected"); | |
2021 | DRECOVER(array_access); | |
2022 | } | |
2023 | ; | |
2024 | ||
2025 | postfix_expression: | |
2026 | primary | |
2027 | | name | |
2028 | | post_increment_expression | |
2029 | | post_decrement_expression | |
2030 | ; | |
2031 | ||
2032 | post_increment_expression: | |
2033 | postfix_expression INCR_TK | |
2034 | { $$ = build_incdec ($2.token, $2.location, $1, 1); } | |
2035 | ; | |
2036 | ||
2037 | post_decrement_expression: | |
2038 | postfix_expression DECR_TK | |
2039 | { $$ = build_incdec ($2.token, $2.location, $1, 1); } | |
2040 | ; | |
2041 | ||
2042 | unary_expression: | |
2043 | pre_increment_expression | |
2044 | | pre_decrement_expression | |
2045 | | PLUS_TK unary_expression | |
2046 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2047 | | MINUS_TK unary_expression | |
2048 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2049 | | unary_expression_not_plus_minus | |
2050 | | PLUS_TK error | |
2051 | {yyerror ("Missing term"); RECOVER} | |
2052 | | MINUS_TK error | |
2053 | {yyerror ("Missing term"); RECOVER} | |
2054 | ; | |
2055 | ||
2056 | pre_increment_expression: | |
2057 | INCR_TK unary_expression | |
2058 | {$$ = build_incdec ($1.token, $1.location, $2, 0); } | |
2059 | | INCR_TK error | |
2060 | {yyerror ("Missing term"); RECOVER} | |
2061 | ; | |
2062 | ||
2063 | pre_decrement_expression: | |
2064 | DECR_TK unary_expression | |
2065 | {$$ = build_incdec ($1.token, $1.location, $2, 0); } | |
2066 | | DECR_TK error | |
2067 | {yyerror ("Missing term"); RECOVER} | |
2068 | ; | |
2069 | ||
2070 | unary_expression_not_plus_minus: | |
2071 | postfix_expression | |
2072 | | NOT_TK unary_expression | |
2073 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2074 | | NEG_TK unary_expression | |
2075 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2076 | | cast_expression | |
2077 | | NOT_TK error | |
2078 | {yyerror ("Missing term"); RECOVER} | |
2079 | | NEG_TK error | |
2080 | {yyerror ("Missing term"); RECOVER} | |
2081 | ; | |
2082 | ||
2083 | cast_expression: /* Error handling here is potentially weak */ | |
2084 | OP_TK primitive_type dims CP_TK unary_expression | |
2085 | { | |
2086 | tree type = $2; | |
ba179f9f | 2087 | while (CURRENT_OSB (ctxp)--) |
e04a16fb | 2088 | type = build_java_array_type (type, -1); |
ba179f9f | 2089 | ctxp->osb_depth--; |
e04a16fb AG |
2090 | $$ = build_cast ($1.location, type, $5); |
2091 | } | |
2092 | | OP_TK primitive_type CP_TK unary_expression | |
2093 | { $$ = build_cast ($1.location, $2, $4); } | |
2094 | | OP_TK expression CP_TK unary_expression_not_plus_minus | |
2095 | { $$ = build_cast ($1.location, $2, $4); } | |
2096 | | OP_TK name dims CP_TK unary_expression_not_plus_minus | |
2097 | { | |
49f48c71 | 2098 | const char *ptr; |
ba179f9f | 2099 | while (CURRENT_OSB (ctxp)--) |
e04a16fb | 2100 | obstack_1grow (&temporary_obstack, '['); |
ba179f9f | 2101 | ctxp->osb_depth--; |
e04a16fb AG |
2102 | obstack_grow0 (&temporary_obstack, |
2103 | IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)), | |
2104 | IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2))); | |
2105 | ptr = obstack_finish (&temporary_obstack); | |
2106 | EXPR_WFL_NODE ($2) = get_identifier (ptr); | |
2107 | $$ = build_cast ($1.location, $2, $5); | |
2108 | } | |
2109 | | OP_TK primitive_type OSB_TK error | |
2110 | {yyerror ("']' expected, invalid type expression");} | |
2111 | | OP_TK error | |
2112 | { | |
2113 | if (ctxp->prevent_ese != lineno) | |
2114 | yyerror ("Invalid type expression"); RECOVER; | |
2115 | RECOVER; | |
2116 | } | |
2117 | | OP_TK primitive_type dims CP_TK error | |
2118 | {yyerror ("Missing term"); RECOVER;} | |
2119 | | OP_TK primitive_type CP_TK error | |
2120 | {yyerror ("Missing term"); RECOVER;} | |
2121 | | OP_TK name dims CP_TK error | |
2122 | {yyerror ("Missing term"); RECOVER;} | |
2123 | ; | |
2124 | ||
2125 | multiplicative_expression: | |
2126 | unary_expression | |
2127 | | multiplicative_expression MULT_TK unary_expression | |
2128 | { | |
2129 | $$ = build_binop (BINOP_LOOKUP ($2.token), | |
2130 | $2.location, $1, $3); | |
2131 | } | |
2132 | | multiplicative_expression DIV_TK unary_expression | |
2133 | { | |
2134 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2135 | $1, $3); | |
2136 | } | |
2137 | | multiplicative_expression REM_TK unary_expression | |
2138 | { | |
2139 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2140 | $1, $3); | |
2141 | } | |
2142 | | multiplicative_expression MULT_TK error | |
2143 | {yyerror ("Missing term"); RECOVER;} | |
2144 | | multiplicative_expression DIV_TK error | |
2145 | {yyerror ("Missing term"); RECOVER;} | |
2146 | | multiplicative_expression REM_TK error | |
2147 | {yyerror ("Missing term"); RECOVER;} | |
2148 | ; | |
2149 | ||
2150 | additive_expression: | |
2151 | multiplicative_expression | |
2152 | | additive_expression PLUS_TK multiplicative_expression | |
2153 | { | |
2154 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2155 | $1, $3); | |
2156 | } | |
2157 | | additive_expression MINUS_TK multiplicative_expression | |
2158 | { | |
2159 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2160 | $1, $3); | |
2161 | } | |
2162 | | additive_expression PLUS_TK error | |
2163 | {yyerror ("Missing term"); RECOVER;} | |
2164 | | additive_expression MINUS_TK error | |
2165 | {yyerror ("Missing term"); RECOVER;} | |
2166 | ; | |
2167 | ||
2168 | shift_expression: | |
2169 | additive_expression | |
2170 | | shift_expression LS_TK additive_expression | |
2171 | { | |
2172 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2173 | $1, $3); | |
2174 | } | |
2175 | | shift_expression SRS_TK additive_expression | |
2176 | { | |
2177 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2178 | $1, $3); | |
2179 | } | |
2180 | | shift_expression ZRS_TK additive_expression | |
2181 | { | |
2182 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2183 | $1, $3); | |
2184 | } | |
2185 | | shift_expression LS_TK error | |
2186 | {yyerror ("Missing term"); RECOVER;} | |
2187 | | shift_expression SRS_TK error | |
2188 | {yyerror ("Missing term"); RECOVER;} | |
2189 | | shift_expression ZRS_TK error | |
2190 | {yyerror ("Missing term"); RECOVER;} | |
2191 | ; | |
2192 | ||
2193 | relational_expression: | |
2194 | shift_expression | |
2195 | | relational_expression LT_TK shift_expression | |
2196 | { | |
2197 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2198 | $1, $3); | |
2199 | } | |
2200 | | relational_expression GT_TK shift_expression | |
2201 | { | |
2202 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2203 | $1, $3); | |
2204 | } | |
2205 | | relational_expression LTE_TK shift_expression | |
2206 | { | |
2207 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2208 | $1, $3); | |
2209 | } | |
2210 | | relational_expression GTE_TK shift_expression | |
2211 | { | |
2212 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2213 | $1, $3); | |
2214 | } | |
2215 | | relational_expression INSTANCEOF_TK reference_type | |
5e942c50 | 2216 | { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); } |
e04a16fb AG |
2217 | | relational_expression LT_TK error |
2218 | {yyerror ("Missing term"); RECOVER;} | |
2219 | | relational_expression GT_TK error | |
2220 | {yyerror ("Missing term"); RECOVER;} | |
2221 | | relational_expression LTE_TK error | |
2222 | {yyerror ("Missing term"); RECOVER;} | |
2223 | | relational_expression GTE_TK error | |
2224 | {yyerror ("Missing term"); RECOVER;} | |
2225 | | relational_expression INSTANCEOF_TK error | |
2226 | {yyerror ("Invalid reference type"); RECOVER;} | |
2227 | ; | |
2228 | ||
2229 | equality_expression: | |
2230 | relational_expression | |
2231 | | equality_expression EQ_TK relational_expression | |
2232 | { | |
2233 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2234 | $1, $3); | |
2235 | } | |
2236 | | equality_expression NEQ_TK relational_expression | |
2237 | { | |
2238 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2239 | $1, $3); | |
2240 | } | |
2241 | | equality_expression EQ_TK error | |
2242 | {yyerror ("Missing term"); RECOVER;} | |
2243 | | equality_expression NEQ_TK error | |
2244 | {yyerror ("Missing term"); RECOVER;} | |
2245 | ; | |
2246 | ||
2247 | and_expression: | |
2248 | equality_expression | |
2249 | | and_expression AND_TK equality_expression | |
2250 | { | |
2251 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2252 | $1, $3); | |
2253 | } | |
2254 | | and_expression AND_TK error | |
2255 | {yyerror ("Missing term"); RECOVER;} | |
2256 | ; | |
2257 | ||
2258 | exclusive_or_expression: | |
2259 | and_expression | |
2260 | | exclusive_or_expression XOR_TK and_expression | |
2261 | { | |
2262 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2263 | $1, $3); | |
2264 | } | |
2265 | | exclusive_or_expression XOR_TK error | |
2266 | {yyerror ("Missing term"); RECOVER;} | |
2267 | ; | |
2268 | ||
2269 | inclusive_or_expression: | |
2270 | exclusive_or_expression | |
2271 | | inclusive_or_expression OR_TK exclusive_or_expression | |
2272 | { | |
2273 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2274 | $1, $3); | |
2275 | } | |
2276 | | inclusive_or_expression OR_TK error | |
2277 | {yyerror ("Missing term"); RECOVER;} | |
2278 | ; | |
2279 | ||
2280 | conditional_and_expression: | |
2281 | inclusive_or_expression | |
2282 | | conditional_and_expression BOOL_AND_TK inclusive_or_expression | |
2283 | { | |
2284 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2285 | $1, $3); | |
2286 | } | |
2287 | | conditional_and_expression BOOL_AND_TK error | |
2288 | {yyerror ("Missing term"); RECOVER;} | |
2289 | ; | |
2290 | ||
2291 | conditional_or_expression: | |
2292 | conditional_and_expression | |
2293 | | conditional_or_expression BOOL_OR_TK conditional_and_expression | |
2294 | { | |
2295 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2296 | $1, $3); | |
2297 | } | |
2298 | | conditional_or_expression BOOL_OR_TK error | |
2299 | {yyerror ("Missing term"); RECOVER;} | |
2300 | ; | |
2301 | ||
2302 | conditional_expression: /* Error handling here is weak */ | |
2303 | conditional_or_expression | |
2304 | | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression | |
22eed1e6 APB |
2305 | { |
2306 | $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5); | |
2307 | EXPR_WFL_LINECOL ($$) = $2.location; | |
2308 | } | |
e04a16fb AG |
2309 | | conditional_or_expression REL_QM_TK REL_CL_TK error |
2310 | { | |
2311 | YYERROR_NOW; | |
2312 | yyerror ("Missing term"); | |
2313 | DRECOVER (1); | |
2314 | } | |
2315 | | conditional_or_expression REL_QM_TK error | |
2316 | {yyerror ("Missing term"); DRECOVER (2);} | |
2317 | | conditional_or_expression REL_QM_TK expression REL_CL_TK error | |
2318 | {yyerror ("Missing term"); DRECOVER (3);} | |
2319 | ; | |
2320 | ||
2321 | assignment_expression: | |
2322 | conditional_expression | |
2323 | | assignment | |
2324 | ; | |
2325 | ||
2326 | assignment: | |
2327 | left_hand_side assignment_operator assignment_expression | |
2328 | { $$ = build_assignment ($2.token, $2.location, $1, $3); } | |
2329 | | left_hand_side assignment_operator error | |
2330 | { | |
2331 | if (ctxp->prevent_ese != lineno) | |
2332 | yyerror ("Missing term"); | |
2333 | DRECOVER (assign); | |
2334 | } | |
2335 | ; | |
2336 | ||
2337 | left_hand_side: | |
2338 | name | |
2339 | | field_access | |
2340 | | array_access | |
2341 | ; | |
2342 | ||
2343 | assignment_operator: | |
2344 | ASSIGN_ANY_TK | |
2345 | | ASSIGN_TK | |
2346 | ; | |
2347 | ||
2348 | expression: | |
2349 | assignment_expression | |
2350 | ; | |
2351 | ||
2352 | constant_expression: | |
2353 | expression | |
2354 | ; | |
2355 | ||
2356 | %% | |
2357 | \f | |
2358 | ||
e04a16fb AG |
2359 | /* Flag for the error report routine to issue the error the first time |
2360 | it's called (overriding the default behavior which is to drop the | |
2361 | first invocation and honor the second one, taking advantage of a | |
2362 | richer context. */ | |
2363 | static int force_error = 0; | |
2364 | ||
2365 | /* Create a new parser context and make it the current one. */ | |
2366 | ||
2367 | void | |
2368 | java_push_parser_context () | |
2369 | { | |
2370 | struct parser_ctxt *new = | |
23a79c61 | 2371 | (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt)); |
e04a16fb | 2372 | |
4504ead1 | 2373 | bzero ((PTR) new, sizeof (struct parser_ctxt)); |
e04a16fb AG |
2374 | new->next = ctxp; |
2375 | ctxp = new; | |
2376 | if (ctxp->next) | |
5e942c50 APB |
2377 | { |
2378 | ctxp->incomplete_class = ctxp->next->incomplete_class; | |
2379 | ctxp->gclass_list = ctxp->next->gclass_list; | |
2380 | } | |
e04a16fb AG |
2381 | } |
2382 | ||
2383 | void | |
2384 | java_parser_context_save_global () | |
2385 | { | |
22eed1e6 APB |
2386 | if (!ctxp) |
2387 | { | |
2388 | java_push_parser_context (); | |
ee07f4f4 APB |
2389 | ctxp->saved_data_ctx = 1; |
2390 | } | |
2391 | else if (ctxp->saved_data) | |
2392 | { | |
2393 | struct parser_ctxt *new = | |
2394 | (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt)); | |
2395 | bzero ((PTR)new, sizeof (struct parser_ctxt)); | |
2396 | memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt)); | |
2397 | new->next = ctxp; | |
2398 | ctxp = new; | |
2399 | ctxp->saved_data_ctx = 1; | |
22eed1e6 | 2400 | } |
e04a16fb AG |
2401 | ctxp->finput = finput; |
2402 | ctxp->lineno = lineno; | |
2403 | ctxp->current_class = current_class; | |
2404 | ctxp->filename = input_filename; | |
2405 | ctxp->current_function_decl = current_function_decl; | |
ee07f4f4 | 2406 | ctxp->saved_data = 1; |
e04a16fb AG |
2407 | } |
2408 | ||
2409 | void | |
2410 | java_parser_context_restore_global () | |
2411 | { | |
2412 | finput = ctxp->finput; | |
2413 | lineno = ctxp->lineno; | |
2414 | current_class = ctxp->current_class; | |
2415 | input_filename = ctxp->filename; | |
ee07f4f4 | 2416 | ctxp->saved_data = 0; |
e04a16fb | 2417 | current_function_decl = ctxp->current_function_decl; |
ee07f4f4 APB |
2418 | if (ctxp->saved_data_ctx) |
2419 | java_pop_parser_context (0); | |
e04a16fb AG |
2420 | } |
2421 | ||
2422 | void | |
b351b287 APB |
2423 | java_pop_parser_context (generate) |
2424 | int generate; | |
e04a16fb AG |
2425 | { |
2426 | tree current; | |
5e942c50 | 2427 | struct parser_ctxt *toFree, *next; |
e04a16fb | 2428 | |
5e942c50 APB |
2429 | if (!ctxp) |
2430 | return; | |
2431 | ||
2432 | toFree = ctxp; | |
2433 | next = ctxp->next; | |
e04a16fb AG |
2434 | if (next) |
2435 | { | |
2436 | next->incomplete_class = ctxp->incomplete_class; | |
5e942c50 | 2437 | next->gclass_list = ctxp->gclass_list; |
e04a16fb AG |
2438 | lineno = ctxp->lineno; |
2439 | finput = ctxp->finput; | |
2440 | current_class = ctxp->current_class; | |
2441 | } | |
2442 | ||
2443 | /* Set the single import class file flag to 0 for the current list | |
2444 | of imported things */ | |
2445 | for (current = ctxp->import_list; current; current = TREE_CHAIN (current)) | |
2446 | IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0; | |
2447 | ||
2448 | /* And restore those of the previous context */ | |
0a2138e2 | 2449 | if ((ctxp = next)) /* Assignment is really meant here */ |
e04a16fb AG |
2450 | for (current = ctxp->import_list; current; current = TREE_CHAIN (current)) |
2451 | IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1; | |
2452 | ||
b351b287 APB |
2453 | if (generate) |
2454 | { | |
2455 | toFree->next = ctxp_for_generation; | |
2456 | ctxp_for_generation = toFree; | |
2457 | } | |
2458 | else | |
2459 | free (toFree); | |
e04a16fb AG |
2460 | } |
2461 | ||
ee07f4f4 APB |
2462 | /* Dump the stacked up parser contexts. Intended to be called from a |
2463 | debugger. */ | |
2464 | ||
2465 | static void | |
2466 | java_debug_context_do (tab) | |
2467 | int tab; | |
2468 | { | |
2469 | #define JAVA_TAB_CONTEXT(C) \ | |
2470 | {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);} | |
2471 | ||
2472 | struct parser_ctxt *copy = ctxp; | |
2473 | while (copy) | |
2474 | { | |
2475 | JAVA_TAB_CONTEXT (tab); | |
2476 | fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy); | |
2477 | JAVA_TAB_CONTEXT (tab); | |
2478 | fprintf (stderr, "filename: %s\n", copy->filename); | |
2479 | JAVA_TAB_CONTEXT (tab); | |
2480 | fprintf (stderr, "package: %s\n", | |
2481 | (copy->package ? | |
2482 | IDENTIFIER_POINTER (copy->package) : "<none>")); | |
2483 | JAVA_TAB_CONTEXT (tab); | |
2484 | fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx); | |
2485 | JAVA_TAB_CONTEXT (tab); | |
2486 | fprintf (stderr, "saved data: %d\n", copy->saved_data); | |
2487 | copy = copy->next; | |
2488 | tab += 2; | |
2489 | } | |
2490 | #undef JAVA_TAB_CONTEXT | |
2491 | } | |
2492 | ||
2493 | void | |
2494 | java_debug_context () | |
2495 | { | |
2496 | java_debug_context_do (0); | |
2497 | } | |
2498 | ||
2499 | ||
8119c720 APB |
2500 | /* Reporting an constructor invocation error. */ |
2501 | static void | |
2502 | parse_ctor_invocation_error () | |
2503 | { | |
2504 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
2505 | yyerror ("Constructor invocation must be first thing in a constructor"); | |
2506 | else | |
2507 | yyerror ("Only constructors can invoke constructors"); | |
2508 | } | |
2509 | ||
2510 | /* Reporting JDK1.1 features not implemented. */ | |
b67d701b PB |
2511 | |
2512 | static tree | |
2513 | parse_jdk1_1_error (msg) | |
49f48c71 | 2514 | const char *msg; |
b67d701b PB |
2515 | { |
2516 | sorry (": `%s' JDK1.1(TM) feature", msg); | |
2517 | java_error_count++; | |
9bbc7d9f | 2518 | return empty_stmt_node; |
b67d701b PB |
2519 | } |
2520 | ||
e04a16fb AG |
2521 | static int do_warning = 0; |
2522 | ||
2523 | void | |
2524 | yyerror (msg) | |
49f48c71 | 2525 | const char *msg; |
e04a16fb AG |
2526 | { |
2527 | static java_lc elc; | |
2528 | static int prev_lineno; | |
49f48c71 | 2529 | static const char *prev_msg; |
e04a16fb | 2530 | |
0a2138e2 | 2531 | int save_lineno; |
e04a16fb AG |
2532 | char *remainder, *code_from_source; |
2533 | extern struct obstack temporary_obstack; | |
2534 | ||
2535 | if (!force_error && prev_lineno == lineno) | |
2536 | return; | |
2537 | ||
2538 | /* Save current error location but report latter, when the context is | |
2539 | richer. */ | |
2540 | if (ctxp->java_error_flag == 0) | |
2541 | { | |
2542 | ctxp->java_error_flag = 1; | |
2543 | elc = ctxp->elc; | |
2544 | /* Do something to use the previous line if we're reaching the | |
2545 | end of the file... */ | |
2546 | #ifdef VERBOSE_SKELETON | |
2547 | printf ("* Error detected (%s)\n", (msg ? msg : "(null)")); | |
2548 | #endif | |
2549 | return; | |
2550 | } | |
2551 | ||
2552 | /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */ | |
2553 | if (!force_error && msg == prev_msg && prev_lineno == elc.line) | |
2554 | return; | |
2555 | ||
2556 | ctxp->java_error_flag = 0; | |
2557 | if (do_warning) | |
2558 | java_warning_count++; | |
2559 | else | |
2560 | java_error_count++; | |
2561 | ||
2562 | if (elc.col == 0 && msg[1] == ';') | |
2563 | { | |
2564 | elc.col = ctxp->p_line->char_col-1; | |
2565 | elc.line = ctxp->p_line->lineno; | |
2566 | } | |
2567 | ||
2568 | save_lineno = lineno; | |
2569 | prev_lineno = lineno = elc.line; | |
2570 | prev_msg = msg; | |
2571 | ||
2572 | code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col); | |
2573 | obstack_grow0 (&temporary_obstack, | |
2574 | code_from_source, strlen (code_from_source)); | |
2575 | remainder = obstack_finish (&temporary_obstack); | |
2576 | if (do_warning) | |
2577 | warning ("%s.\n%s", msg, remainder); | |
2578 | else | |
2579 | error ("%s.\n%s", msg, remainder); | |
2580 | ||
2581 | /* This allow us to cheaply avoid an extra 'Invalid expression | |
2582 | statement' error report when errors have been already reported on | |
2583 | the same line. This occurs when we report an error but don't have | |
2584 | a synchronization point other than ';', which | |
2585 | expression_statement is the only one to take care of. */ | |
2586 | ctxp->prevent_ese = lineno = save_lineno; | |
2587 | } | |
2588 | ||
2589 | static void | |
15fdcfe9 | 2590 | issue_warning_error_from_context (cl, msg, ap) |
5e942c50 | 2591 | tree cl; |
d4476be2 | 2592 | const char *msg; |
15fdcfe9 | 2593 | va_list ap; |
5e942c50 | 2594 | { |
1886c9d8 | 2595 | char *saved, *saved_input_filename; |
15fdcfe9 PB |
2596 | char buffer [4096]; |
2597 | vsprintf (buffer, msg, ap); | |
2598 | force_error = 1; | |
5e942c50 APB |
2599 | |
2600 | ctxp->elc.line = EXPR_WFL_LINENO (cl); | |
82371d41 APB |
2601 | ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 : |
2602 | (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl))); | |
5e942c50 APB |
2603 | |
2604 | /* We have a CL, that's a good reason for using it if it contains data */ | |
2605 | saved = ctxp->filename; | |
2606 | if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl)) | |
2607 | ctxp->filename = EXPR_WFL_FILENAME (cl); | |
1886c9d8 APB |
2608 | saved_input_filename = input_filename; |
2609 | input_filename = ctxp->filename; | |
15fdcfe9 PB |
2610 | java_error (NULL); |
2611 | java_error (buffer); | |
5e942c50 | 2612 | ctxp->filename = saved; |
1886c9d8 | 2613 | input_filename = saved_input_filename; |
15fdcfe9 | 2614 | force_error = 0; |
5e942c50 APB |
2615 | } |
2616 | ||
e04a16fb AG |
2617 | /* Issue an error message at a current source line CL */ |
2618 | ||
15fdcfe9 | 2619 | void |
df32d2ce | 2620 | parse_error_context VPARAMS ((tree cl, const char *msg, ...)) |
e04a16fb | 2621 | { |
d4476be2 | 2622 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 2623 | tree cl; |
d4476be2 | 2624 | const char *msg; |
e04a16fb | 2625 | #endif |
e04a16fb AG |
2626 | va_list ap; |
2627 | ||
2628 | VA_START (ap, msg); | |
d4476be2 | 2629 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 2630 | cl = va_arg (ap, tree); |
d4476be2 | 2631 | msg = va_arg (ap, const char *); |
e04a16fb | 2632 | #endif |
15fdcfe9 PB |
2633 | issue_warning_error_from_context (cl, msg, ap); |
2634 | va_end (ap); | |
e04a16fb AG |
2635 | } |
2636 | ||
2637 | /* Issue a warning at a current source line CL */ | |
2638 | ||
2639 | static void | |
df32d2ce | 2640 | parse_warning_context VPARAMS ((tree cl, const char *msg, ...)) |
e04a16fb | 2641 | { |
d4476be2 | 2642 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 2643 | tree cl; |
d4476be2 | 2644 | const char *msg; |
e04a16fb | 2645 | #endif |
e04a16fb AG |
2646 | va_list ap; |
2647 | ||
2648 | VA_START (ap, msg); | |
d4476be2 | 2649 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 2650 | cl = va_arg (ap, tree); |
d4476be2 | 2651 | msg = va_arg (ap, const char *); |
e04a16fb | 2652 | #endif |
e04a16fb | 2653 | |
c877974e | 2654 | force_error = do_warning = 1; |
15fdcfe9 | 2655 | issue_warning_error_from_context (cl, msg, ap); |
c877974e | 2656 | do_warning = force_error = 0; |
15fdcfe9 | 2657 | va_end (ap); |
e04a16fb AG |
2658 | } |
2659 | ||
82371d41 APB |
2660 | static tree |
2661 | find_expr_with_wfl (node) | |
2662 | tree node; | |
2663 | { | |
2664 | while (node) | |
2665 | { | |
2666 | char code; | |
2667 | tree to_return; | |
2668 | ||
2669 | switch (TREE_CODE (node)) | |
2670 | { | |
2671 | case BLOCK: | |
c0d87ff6 PB |
2672 | node = BLOCK_EXPR_BODY (node); |
2673 | continue; | |
82371d41 APB |
2674 | |
2675 | case COMPOUND_EXPR: | |
2676 | to_return = find_expr_with_wfl (TREE_OPERAND (node, 0)); | |
2677 | if (to_return) | |
2678 | return to_return; | |
c0d87ff6 PB |
2679 | node = TREE_OPERAND (node, 1); |
2680 | continue; | |
82371d41 APB |
2681 | |
2682 | case LOOP_EXPR: | |
c0d87ff6 PB |
2683 | node = TREE_OPERAND (node, 0); |
2684 | continue; | |
82371d41 APB |
2685 | |
2686 | case LABELED_BLOCK_EXPR: | |
c0d87ff6 PB |
2687 | node = TREE_OPERAND (node, 1); |
2688 | continue; | |
2689 | ||
82371d41 APB |
2690 | default: |
2691 | code = TREE_CODE_CLASS (TREE_CODE (node)); | |
2692 | if (((code == '1') || (code == '2') || (code == 'e')) | |
2693 | && EXPR_WFL_LINECOL (node)) | |
2694 | return node; | |
ba179f9f | 2695 | return NULL_TREE; |
82371d41 APB |
2696 | } |
2697 | } | |
2698 | return NULL_TREE; | |
2699 | } | |
2700 | ||
2701 | /* Issue a missing return statement error. Uses METHOD to figure the | |
2702 | last line of the method the error occurs in. */ | |
2703 | ||
2704 | static void | |
2705 | missing_return_error (method) | |
2706 | tree method; | |
2707 | { | |
2708 | EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2); | |
2709 | parse_error_context (wfl_operator, "Missing return statement"); | |
2710 | } | |
2711 | ||
2712 | /* Issue an unreachable statement error. From NODE, find the next | |
2713 | statement to report appropriately. */ | |
2714 | static void | |
2715 | unreachable_stmt_error (node) | |
2716 | tree node; | |
2717 | { | |
2718 | /* Browse node to find the next expression node that has a WFL. Use | |
2719 | the location to report the error */ | |
2720 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
2721 | node = find_expr_with_wfl (TREE_OPERAND (node, 1)); | |
2722 | else | |
2723 | node = find_expr_with_wfl (node); | |
2724 | ||
2725 | if (node) | |
2726 | { | |
2727 | EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2); | |
2728 | parse_error_context (wfl_operator, "Unreachable statement"); | |
2729 | } | |
2730 | else | |
2731 | fatal ("Can't get valid statement - unreachable_stmt_error"); | |
2732 | } | |
2733 | ||
c877974e | 2734 | int |
e04a16fb AG |
2735 | java_report_errors () |
2736 | { | |
2737 | if (java_error_count) | |
2738 | fprintf (stderr, "%d error%s", | |
2739 | java_error_count, (java_error_count == 1 ? "" : "s")); | |
2740 | if (java_warning_count) | |
2741 | fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""), | |
2742 | java_warning_count, (java_warning_count == 1 ? "" : "s")); | |
2743 | if (java_error_count || java_warning_count) | |
2744 | putc ('\n', stderr); | |
c877974e | 2745 | return java_error_count; |
e04a16fb AG |
2746 | } |
2747 | ||
2748 | static char * | |
2749 | java_accstring_lookup (flags) | |
2750 | int flags; | |
2751 | { | |
2752 | static char buffer [80]; | |
2753 | #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;} | |
2754 | ||
2755 | /* Access modifier looked-up first for easier report on forbidden | |
2756 | access. */ | |
2757 | if (flags & ACC_PUBLIC) COPY_RETURN ("public"); | |
2758 | if (flags & ACC_PRIVATE) COPY_RETURN ("private"); | |
2759 | if (flags & ACC_PROTECTED) COPY_RETURN ("protected"); | |
2760 | if (flags & ACC_STATIC) COPY_RETURN ("static"); | |
2761 | if (flags & ACC_FINAL) COPY_RETURN ("final"); | |
2762 | if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized"); | |
2763 | if (flags & ACC_VOLATILE) COPY_RETURN ("volatile"); | |
2764 | if (flags & ACC_TRANSIENT) COPY_RETURN ("transient"); | |
2765 | if (flags & ACC_NATIVE) COPY_RETURN ("native"); | |
2766 | if (flags & ACC_INTERFACE) COPY_RETURN ("interface"); | |
2767 | if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract"); | |
2768 | ||
2769 | buffer [0] = '\0'; | |
2770 | return buffer; | |
2771 | #undef COPY_RETURN | |
2772 | } | |
2773 | ||
b67d701b PB |
2774 | /* Issuing error messages upon redefinition of classes, interfaces or |
2775 | variables. */ | |
2776 | ||
e04a16fb | 2777 | static void |
b67d701b | 2778 | classitf_redefinition_error (context, id, decl, cl) |
49f48c71 | 2779 | const char *context; |
e04a16fb AG |
2780 | tree id, decl, cl; |
2781 | { | |
2782 | parse_error_context (cl, "%s `%s' already defined in %s:%d", | |
2783 | context, IDENTIFIER_POINTER (id), | |
2784 | DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl)); | |
2785 | /* Here we should point out where its redefined. It's a unicode. FIXME */ | |
2786 | } | |
2787 | ||
b67d701b PB |
2788 | static void |
2789 | variable_redefinition_error (context, name, type, line) | |
2790 | tree context, name, type; | |
2791 | int line; | |
2792 | { | |
49f48c71 | 2793 | const char *type_name; |
b67d701b PB |
2794 | |
2795 | /* Figure a proper name for type. We might haven't resolved it */ | |
c877974e APB |
2796 | if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type)) |
2797 | type_name = IDENTIFIER_POINTER (TYPE_NAME (type)); | |
b67d701b | 2798 | else |
0a2138e2 | 2799 | type_name = lang_printable_name (type, 0); |
b67d701b PB |
2800 | |
2801 | parse_error_context (context, | |
781b0558 | 2802 | "Variable `%s' is already defined in this method and was declared `%s %s' at line %d", |
b67d701b PB |
2803 | IDENTIFIER_POINTER (name), |
2804 | type_name, IDENTIFIER_POINTER (name), line); | |
2805 | } | |
2806 | ||
c583dd46 APB |
2807 | static tree |
2808 | build_array_from_name (type, type_wfl, name, ret_name) | |
2809 | tree type, type_wfl, name, *ret_name; | |
2810 | { | |
2811 | int more_dims = 0; | |
49f48c71 | 2812 | const char *string; |
c583dd46 APB |
2813 | |
2814 | /* Eventually get more dims */ | |
2815 | string = IDENTIFIER_POINTER (name); | |
2816 | while (string [more_dims] == '[') | |
2817 | more_dims++; | |
2818 | ||
2819 | /* If we have, then craft a new type for this variable */ | |
2820 | if (more_dims) | |
2821 | { | |
c0d87ff6 | 2822 | name = get_identifier (&string [more_dims]); |
c583dd46 | 2823 | |
34f4db93 APB |
2824 | /* If we have a pointer, use its type */ |
2825 | if (TREE_CODE (type) == POINTER_TYPE) | |
2826 | type = TREE_TYPE (type); | |
c583dd46 APB |
2827 | |
2828 | /* Building the first dimension of a primitive type uses this | |
2829 | function */ | |
2830 | if (JPRIMITIVE_TYPE_P (type)) | |
2831 | { | |
2832 | type = build_java_array_type (type, -1); | |
22eed1e6 | 2833 | CLASS_LOADED_P (type) = 1; |
c583dd46 APB |
2834 | more_dims--; |
2835 | } | |
2836 | /* Otherwise, if we have a WFL for this type, use it (the type | |
2837 | is already an array on an unresolved type, and we just keep | |
2838 | on adding dimensions) */ | |
2839 | else if (type_wfl) | |
2840 | type = type_wfl; | |
2841 | ||
2842 | /* Add all the dimensions */ | |
2843 | while (more_dims--) | |
2844 | type = build_unresolved_array_type (type); | |
2845 | ||
2846 | /* The type may have been incomplete in the first place */ | |
2847 | if (type_wfl) | |
2848 | type = obtain_incomplete_type (type); | |
2849 | } | |
2850 | ||
2851 | *ret_name = name; | |
2852 | return type; | |
2853 | } | |
2854 | ||
e04a16fb AG |
2855 | /* Build something that the type identifier resolver will identify as |
2856 | being an array to an unresolved type. TYPE_WFL is a WFL on a | |
2857 | identifier. */ | |
2858 | ||
2859 | static tree | |
2860 | build_unresolved_array_type (type_or_wfl) | |
2861 | tree type_or_wfl; | |
2862 | { | |
49f48c71 | 2863 | const char *ptr; |
e04a16fb | 2864 | |
1886c9d8 | 2865 | /* TYPE_OR_WFL might be an array on a resolved type. In this case, |
e04a16fb AG |
2866 | just create a array type */ |
2867 | if (TREE_CODE (type_or_wfl) == RECORD_TYPE) | |
2868 | { | |
2869 | tree type = build_java_array_type (type_or_wfl, -1); | |
2870 | CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl); | |
2871 | return type; | |
2872 | } | |
2873 | ||
2874 | obstack_1grow (&temporary_obstack, '['); | |
2875 | obstack_grow0 (&temporary_obstack, | |
2876 | IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)), | |
2877 | IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl))); | |
2878 | ptr = obstack_finish (&temporary_obstack); | |
2879 | return build_expr_wfl (get_identifier (ptr), | |
2880 | EXPR_WFL_FILENAME (type_or_wfl), | |
2881 | EXPR_WFL_LINENO (type_or_wfl), | |
2882 | EXPR_WFL_COLNO (type_or_wfl)); | |
2883 | } | |
2884 | ||
2885 | /* Check modifiers. If one doesn't fit, retrieve it in its declaration line | |
2886 | and point it out. */ | |
2887 | ||
2888 | static void | |
2889 | check_modifiers (message, value, mask) | |
49f48c71 | 2890 | const char *message; |
e04a16fb AG |
2891 | int value; |
2892 | int mask; | |
2893 | { | |
2894 | /* Should point out the one that don't fit. ASCII/unicode, | |
2895 | going backward. FIXME */ | |
2896 | if (value & ~mask) | |
2897 | { | |
2898 | int i, remainder = value & ~mask; | |
2899 | for (i = 0; i <= 10; i++) | |
2900 | if ((1 << i) & remainder) | |
2901 | parse_error_context (ctxp->modifier_ctx [i], message, | |
2902 | java_accstring_lookup (1 << i)); | |
2903 | } | |
2904 | } | |
2905 | ||
2906 | static void | |
2907 | parser_add_interface (class_decl, interface_decl, wfl) | |
2908 | tree class_decl, interface_decl, wfl; | |
2909 | { | |
2910 | if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl))) | |
2911 | parse_error_context (wfl, "Interface `%s' repeated", | |
2912 | IDENTIFIER_POINTER (DECL_NAME (interface_decl))); | |
2913 | } | |
2914 | ||
2915 | /* Bulk of common class/interface checks. Return 1 if an error was | |
2916 | encountered. TAG is 0 for a class, 1 for an interface. */ | |
2917 | ||
2918 | static int | |
2919 | check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl) | |
2920 | int is_interface, flags; | |
2921 | tree raw_name, qualified_name, decl, cl; | |
2922 | { | |
2923 | tree node; | |
2924 | ||
2925 | if (!quiet_flag) | |
2926 | fprintf (stderr, " %s %s", (is_interface ? "interface" : "class"), | |
2927 | IDENTIFIER_POINTER (qualified_name)); | |
2928 | ||
2929 | /* Scope of an interface/class type name: | |
2930 | - Can't be imported by a single type import | |
2931 | - Can't already exists in the package */ | |
2932 | if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name) | |
2933 | && (node = find_name_in_single_imports (raw_name))) | |
2934 | { | |
2935 | parse_error_context | |
2936 | (cl, "%s name `%s' clashes with imported type `%s'", | |
2937 | (is_interface ? "Interface" : "Class"), | |
2938 | IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node)); | |
2939 | return 1; | |
2940 | } | |
2941 | if (decl && CLASS_COMPLETE_P (decl)) | |
2942 | { | |
b67d701b PB |
2943 | classitf_redefinition_error ((is_interface ? "Interface" : "Class"), |
2944 | qualified_name, decl, cl); | |
e04a16fb AG |
2945 | return 1; |
2946 | } | |
2947 | ||
2948 | /* If public, file name should match class/interface name */ | |
2949 | if (flags & ACC_PUBLIC) | |
2950 | { | |
49f48c71 | 2951 | const char *f; |
e04a16fb AG |
2952 | |
2953 | /* Contains OS dependent assumption on path separator. FIXME */ | |
2954 | for (f = &input_filename [strlen (input_filename)]; | |
fa322ab5 TT |
2955 | f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR; |
2956 | f--) | |
2957 | ; | |
847fe791 | 2958 | if (f[0] == '/' || f[0] == DIR_SEPARATOR) |
e04a16fb AG |
2959 | f++; |
2960 | if (strncmp (IDENTIFIER_POINTER (raw_name), | |
2961 | f , IDENTIFIER_LENGTH (raw_name)) || | |
2962 | f [IDENTIFIER_LENGTH (raw_name)] != '.') | |
781b0558 KG |
2963 | parse_error_context |
2964 | (cl, "Public %s `%s' must be defined in a file called `%s.java'", | |
e04a16fb AG |
2965 | (is_interface ? "interface" : "class"), |
2966 | IDENTIFIER_POINTER (qualified_name), | |
2967 | IDENTIFIER_POINTER (raw_name)); | |
2968 | } | |
2969 | ||
2970 | check_modifiers ((is_interface ? | |
2971 | "Illegal modifier `%s' for interface declaration" : | |
2972 | "Illegal modifier `%s' for class declaration"), flags, | |
2973 | (is_interface ? INTERFACE_MODIFIERS : CLASS_MODIFIERS)); | |
2974 | return 0; | |
2975 | } | |
2976 | ||
2977 | /* If DECL is NULL, create and push a new DECL, record the current | |
2978 | line CL and do other maintenance things. */ | |
2979 | ||
2980 | static tree | |
2981 | maybe_create_class_interface_decl (decl, qualified_name, cl) | |
2982 | tree decl, qualified_name, cl; | |
2983 | { | |
5e942c50 | 2984 | if (!decl) |
e04a16fb AG |
2985 | decl = push_class (make_class (), qualified_name); |
2986 | ||
2987 | /* Take care of the file and line business */ | |
2988 | DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl); | |
f099f336 APB |
2989 | /* If we're emiting xrefs, store the line/col number information */ |
2990 | if (flag_emit_xref) | |
2991 | DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl); | |
2992 | else | |
2993 | DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl); | |
e04a16fb | 2994 | CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1; |
b351b287 APB |
2995 | CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) = |
2996 | IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl)); | |
e04a16fb AG |
2997 | |
2998 | ctxp->current_parsed_class = decl; | |
2999 | ||
3000 | /* Link the declaration to the already seen ones */ | |
3001 | TREE_CHAIN (decl) = ctxp->class_list; | |
3002 | ctxp->class_list = decl; | |
5e942c50 | 3003 | |
23a79c61 | 3004 | /* Create a new nodes in the global lists */ |
5e942c50 | 3005 | ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list); |
23a79c61 | 3006 | all_class_list = tree_cons (NULL_TREE, decl, all_class_list); |
5e942c50 | 3007 | |
e04a16fb AG |
3008 | /* Install a new dependency list element */ |
3009 | create_jdep_list (ctxp); | |
3010 | ||
3011 | SOURCE_FRONTEND_DEBUG (("Defining class/interface %s", | |
3012 | IDENTIFIER_POINTER (qualified_name))); | |
3013 | return decl; | |
3014 | } | |
3015 | ||
3016 | static void | |
3017 | add_superinterfaces (decl, interface_list) | |
3018 | tree decl, interface_list; | |
3019 | { | |
3020 | tree node; | |
3021 | /* Superinterface(s): if present and defined, parser_check_super_interface () | |
3022 | takes care of ensuring that: | |
3023 | - This is an accessible interface type, | |
3024 | - Circularity detection. | |
3025 | parser_add_interface is then called. If present but not defined, | |
3026 | the check operation is delayed until the super interface gets | |
3027 | defined. */ | |
3028 | for (node = interface_list; node; node = TREE_CHAIN (node)) | |
3029 | { | |
15fdcfe9 | 3030 | tree current = TREE_PURPOSE (node); |
5e942c50 APB |
3031 | tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current)); |
3032 | if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl))) | |
e04a16fb | 3033 | { |
5e942c50 APB |
3034 | if (!parser_check_super_interface (idecl, decl, current)) |
3035 | parser_add_interface (decl, idecl, current); | |
e04a16fb AG |
3036 | } |
3037 | else | |
3038 | register_incomplete_type (JDEP_INTERFACE, | |
3039 | current, decl, NULL_TREE); | |
3040 | } | |
3041 | } | |
3042 | ||
3043 | /* Create an interface in pass1 and return its decl. Return the | |
3044 | interface's decl in pass 2. */ | |
3045 | ||
3046 | static tree | |
3047 | create_interface (flags, id, super) | |
3048 | int flags; | |
3049 | tree id, super; | |
3050 | { | |
e04a16fb AG |
3051 | tree raw_name = EXPR_WFL_NODE (id); |
3052 | tree q_name = parser_qualified_classname (id); | |
3053 | tree decl = IDENTIFIER_CLASS_VALUE (q_name); | |
3054 | ||
3055 | EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */ | |
3056 | ||
3057 | /* Basic checks: scope, redefinition, modifiers */ | |
3058 | if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id)) | |
3059 | return NULL_TREE; | |
3060 | ||
3061 | /* Interface modifiers check | |
3062 | - public/abstract allowed (already done at that point) | |
3063 | - abstract is obsolete (comes first, it's a warning, or should be) | |
3064 | - Can't use twice the same (checked in the modifier rule) */ | |
c877974e | 3065 | if ((flags & ACC_ABSTRACT) && flag_redundant) |
e04a16fb AG |
3066 | parse_warning_context |
3067 | (MODIFIER_WFL (ABSTRACT_TK), | |
781b0558 | 3068 | "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name)); |
e04a16fb AG |
3069 | |
3070 | /* Create a new decl if DECL is NULL, otherwise fix it */ | |
3071 | decl = maybe_create_class_interface_decl (decl, q_name, id); | |
3072 | ||
3073 | /* Set super info and mark the class a complete */ | |
2aa11e97 | 3074 | set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl), |
e04a16fb AG |
3075 | object_type_node, ctxp->interface_number); |
3076 | ctxp->interface_number = 0; | |
3077 | CLASS_COMPLETE_P (decl) = 1; | |
3078 | add_superinterfaces (decl, super); | |
3079 | ||
3080 | return decl; | |
3081 | } | |
3082 | ||
3083 | /* Create an class in pass1 and return its decl. Return class | |
3084 | interface's decl in pass 2. */ | |
3085 | ||
3086 | static tree | |
3087 | create_class (flags, id, super, interfaces) | |
3088 | int flags; | |
3089 | tree id, super, interfaces; | |
3090 | { | |
e04a16fb AG |
3091 | tree raw_name = EXPR_WFL_NODE (id); |
3092 | tree class_id, decl; | |
9ee9b555 | 3093 | tree super_decl_type; |
e04a16fb AG |
3094 | |
3095 | class_id = parser_qualified_classname (id); | |
3096 | decl = IDENTIFIER_CLASS_VALUE (class_id); | |
5e942c50 | 3097 | ctxp->current_parsed_class_un = EXPR_WFL_NODE (id); |
e04a16fb AG |
3098 | EXPR_WFL_NODE (id) = class_id; |
3099 | ||
3100 | /* Basic check: scope, redefinition, modifiers */ | |
3101 | if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id)) | |
3102 | return NULL_TREE; | |
3103 | ||
3104 | /* Class modifier check: | |
3105 | - Allowed modifier (already done at that point) | |
3106 | - abstract AND final forbidden | |
3107 | - Public classes defined in the correct file */ | |
3108 | if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL)) | |
781b0558 KG |
3109 | parse_error_context |
3110 | (id, "Class `%s' can't be declared both abstract and final", | |
3111 | IDENTIFIER_POINTER (raw_name)); | |
e04a16fb AG |
3112 | |
3113 | /* Create a new decl if DECL is NULL, otherwise fix it */ | |
3114 | decl = maybe_create_class_interface_decl (decl, class_id, id); | |
3115 | ||
3116 | /* If SUPER exists, use it, otherwise use Object */ | |
3117 | if (super) | |
3118 | { | |
3119 | /* Can't extend java.lang.Object */ | |
3120 | if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node) | |
3121 | { | |
3122 | parse_error_context (id, "Can't extend `java.lang.Object'"); | |
3123 | return NULL_TREE; | |
3124 | } | |
3125 | ||
2c3199bc PB |
3126 | super_decl_type = |
3127 | register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE); | |
e04a16fb AG |
3128 | } |
3129 | else if (TREE_TYPE (decl) != object_type_node) | |
3130 | super_decl_type = object_type_node; | |
3131 | /* We're defining java.lang.Object */ | |
3132 | else | |
3133 | super_decl_type = NULL_TREE; | |
3134 | ||
3135 | /* Set super info and mark the class a complete */ | |
3136 | set_super_info (flags, TREE_TYPE (decl), super_decl_type, | |
3137 | ctxp->interface_number); | |
3138 | ctxp->interface_number = 0; | |
3139 | CLASS_COMPLETE_P (decl) = 1; | |
3140 | add_superinterfaces (decl, interfaces); | |
3141 | ||
7f10c2e2 APB |
3142 | /* If doing xref, store the location at which the inherited class |
3143 | (if any) was seen. */ | |
3144 | if (flag_emit_xref && super) | |
3145 | DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super); | |
3146 | ||
5e942c50 APB |
3147 | /* Eventually sets the @deprecated tag flag */ |
3148 | CHECK_DEPRECATED (decl); | |
3149 | ||
e04a16fb AG |
3150 | return decl; |
3151 | } | |
3152 | ||
3153 | /* Can't use lookup_field () since we don't want to load the class and | |
3154 | can't set the CLASS_LOADED_P flag */ | |
3155 | ||
3156 | static tree | |
3157 | find_field (class, name) | |
3158 | tree class; | |
3159 | tree name; | |
3160 | { | |
3161 | tree decl; | |
3162 | for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl)) | |
3163 | { | |
3164 | if (DECL_NAME (decl) == name) | |
3165 | return decl; | |
3166 | } | |
3167 | return NULL_TREE; | |
3168 | } | |
3169 | ||
3170 | /* Wrap around lookup_field that doesn't potentially upset the value | |
3171 | of CLASS */ | |
3172 | ||
3173 | static tree | |
3174 | lookup_field_wrapper (class, name) | |
3175 | tree class, name; | |
3176 | { | |
3177 | tree type = class; | |
5b09b33e | 3178 | tree decl; |
c877974e | 3179 | java_parser_context_save_global (); |
5b09b33e | 3180 | decl = lookup_field (&type, name); |
c877974e | 3181 | java_parser_context_restore_global (); |
93024893 | 3182 | return decl == error_mark_node ? NULL : decl; |
e04a16fb AG |
3183 | } |
3184 | ||
3185 | /* Find duplicate field within the same class declarations and report | |
c583dd46 APB |
3186 | the error. Returns 1 if a duplicated field was found, 0 |
3187 | otherwise. */ | |
e04a16fb AG |
3188 | |
3189 | static int | |
c583dd46 | 3190 | duplicate_declaration_error_p (new_field_name, new_type, cl) |
0a2138e2 | 3191 | tree new_field_name, new_type, cl; |
e04a16fb AG |
3192 | { |
3193 | /* This might be modified to work with method decl as well */ | |
3194 | tree decl = find_field (TREE_TYPE (ctxp->current_parsed_class), | |
3195 | new_field_name); | |
3196 | if (decl) | |
3197 | { | |
c2e3db92 | 3198 | char *t1 = xstrdup (purify_type_name |
4a5f66c3 APB |
3199 | ((TREE_CODE (new_type) == POINTER_TYPE |
3200 | && TREE_TYPE (new_type) == NULL_TREE) ? | |
3201 | IDENTIFIER_POINTER (TYPE_NAME (new_type)) : | |
3202 | lang_printable_name (new_type, 1))); | |
c877974e APB |
3203 | /* The type may not have been completed by the time we report |
3204 | the error */ | |
c2e3db92 | 3205 | char *t2 = xstrdup (purify_type_name |
4a5f66c3 | 3206 | ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE |
c877974e APB |
3207 | && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ? |
3208 | IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) : | |
3209 | lang_printable_name (TREE_TYPE (decl), 1))); | |
e04a16fb AG |
3210 | parse_error_context |
3211 | (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)", | |
3212 | t1, IDENTIFIER_POINTER (new_field_name), | |
3213 | t2, IDENTIFIER_POINTER (DECL_NAME (decl)), | |
3214 | DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl)); | |
3215 | free (t1); | |
3216 | free (t2); | |
c583dd46 | 3217 | return 1; |
e04a16fb | 3218 | } |
c583dd46 | 3219 | return 0; |
e04a16fb AG |
3220 | } |
3221 | ||
3222 | /* Field registration routine. If TYPE doesn't exist, field | |
3223 | declarations are linked to the undefined TYPE dependency list, to | |
3224 | be later resolved in java_complete_class () */ | |
3225 | ||
3226 | static void | |
3227 | register_fields (flags, type, variable_list) | |
3228 | int flags; | |
3229 | tree type, variable_list; | |
3230 | { | |
c583dd46 | 3231 | tree current, saved_type; |
e04a16fb AG |
3232 | tree class_type = TREE_TYPE (ctxp->current_parsed_class); |
3233 | int saved_lineno = lineno; | |
3234 | int must_chain = 0; | |
3235 | tree wfl = NULL_TREE; | |
3236 | ||
3237 | /* If we're adding fields to interfaces, those fields are public, | |
3238 | static, final */ | |
3239 | if (CLASS_INTERFACE (TYPE_NAME (class_type))) | |
3240 | { | |
3241 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), | |
3242 | flags, ACC_PUBLIC, | |
3243 | "%s", "interface field(s)"); | |
3244 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK), | |
3245 | flags, ACC_STATIC, | |
3246 | "%s", "interface field(s)"); | |
3247 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK), | |
3248 | flags, ACC_FINAL, "%s", "interface field(s)"); | |
3249 | check_modifiers ("Illegal interface member modifier `%s'", flags, | |
3250 | INTERFACE_FIELD_MODIFIERS); | |
3251 | flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL); | |
3252 | } | |
3253 | ||
c583dd46 APB |
3254 | /* Obtain a suitable type for resolution, if necessary */ |
3255 | SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain); | |
3256 | ||
3257 | /* If TYPE is fully resolved and we don't have a reference, make one */ | |
1886c9d8 | 3258 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
e04a16fb | 3259 | |
c583dd46 APB |
3260 | for (current = variable_list, saved_type = type; current; |
3261 | current = TREE_CHAIN (current), type = saved_type) | |
e04a16fb | 3262 | { |
c877974e | 3263 | tree real_type; |
c583dd46 | 3264 | tree field_decl; |
e04a16fb AG |
3265 | tree cl = TREE_PURPOSE (current); |
3266 | tree init = TREE_VALUE (current); | |
3267 | tree current_name = EXPR_WFL_NODE (cl); | |
3268 | ||
c583dd46 APB |
3269 | /* Process NAME, as it may specify extra dimension(s) for it */ |
3270 | type = build_array_from_name (type, wfl, current_name, ¤t_name); | |
3271 | ||
c583dd46 APB |
3272 | /* Type adjustment. We may have just readjusted TYPE because |
3273 | the variable specified more dimensions. Make sure we have | |
22eed1e6 APB |
3274 | a reference if we can and don't have one already. Also |
3275 | change the name if we have an init. */ | |
3276 | if (type != saved_type) | |
3277 | { | |
1886c9d8 | 3278 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
22eed1e6 APB |
3279 | if (init) |
3280 | EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name; | |
3281 | } | |
e04a16fb | 3282 | |
c877974e APB |
3283 | real_type = GET_REAL_TYPE (type); |
3284 | /* Check for redeclarations */ | |
3285 | if (duplicate_declaration_error_p (current_name, real_type, cl)) | |
3286 | continue; | |
3287 | ||
c583dd46 | 3288 | /* Set lineno to the line the field was found and create a |
5e942c50 | 3289 | declaration for it. Eventually sets the @deprecated tag flag. */ |
f099f336 APB |
3290 | if (flag_emit_xref) |
3291 | lineno = EXPR_WFL_LINECOL (cl); | |
3292 | else | |
3293 | lineno = EXPR_WFL_LINENO (cl); | |
c877974e | 3294 | field_decl = add_field (class_type, current_name, real_type, flags); |
5e942c50 | 3295 | CHECK_DEPRECATED (field_decl); |
c583dd46 APB |
3296 | |
3297 | /* Check if we must chain. */ | |
3298 | if (must_chain) | |
3299 | register_incomplete_type (JDEP_FIELD, wfl, field_decl, type); | |
e04a16fb | 3300 | |
c583dd46 APB |
3301 | /* If we have an initialization value tied to the field */ |
3302 | if (init) | |
3303 | { | |
3304 | /* The field is declared static */ | |
e04a16fb | 3305 | if (flags & ACC_STATIC) |
e04a16fb | 3306 | { |
7525cc04 APB |
3307 | /* We include the field and its initialization part into |
3308 | a list used to generate <clinit>. After <clinit> is | |
ba179f9f APB |
3309 | walked, field initializations will be processed and |
3310 | fields initialized with known constants will be taken | |
3311 | out of <clinit> and have their DECL_INITIAL set | |
7525cc04 APB |
3312 | appropriately. */ |
3313 | TREE_CHAIN (init) = ctxp->static_initialized; | |
3314 | ctxp->static_initialized = init; | |
7f10c2e2 APB |
3315 | if (TREE_OPERAND (init, 1) |
3316 | && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT) | |
5bba4807 | 3317 | TREE_STATIC (TREE_OPERAND (init, 1)) = 1; |
e04a16fb | 3318 | } |
5e942c50 APB |
3319 | /* A non-static field declared with an immediate initialization is |
3320 | to be initialized in <init>, if any. This field is remembered | |
3321 | to be processed at the time of the generation of <init>. */ | |
c583dd46 APB |
3322 | else |
3323 | { | |
3324 | TREE_CHAIN (init) = ctxp->non_static_initialized; | |
3325 | ctxp->non_static_initialized = init; | |
3326 | } | |
5b09b33e | 3327 | MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1; |
8576f094 | 3328 | DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1); |
e04a16fb AG |
3329 | } |
3330 | } | |
3331 | lineno = saved_lineno; | |
3332 | } | |
3333 | ||
89e09b9a | 3334 | /* Generate the method $finit$ that initializes fields initialized |
22eed1e6 APB |
3335 | upon declaration. */ |
3336 | ||
3337 | static void | |
3338 | maybe_generate_finit () | |
3339 | { | |
3340 | tree mdecl, current; | |
3341 | ||
3342 | if (!ctxp->non_static_initialized || java_error_count) | |
3343 | return; | |
3344 | ||
3345 | mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class), | |
89e09b9a | 3346 | ACC_PRIVATE, void_type_node, |
de4c7b02 | 3347 | finit_identifier_node, end_params_node); |
22eed1e6 APB |
3348 | start_artificial_method_body (mdecl); |
3349 | ||
3350 | ctxp->non_static_initialized = nreverse (ctxp->non_static_initialized); | |
3351 | for (current = ctxp->non_static_initialized; current; | |
3352 | current = TREE_CHAIN (current)) | |
3353 | java_method_add_stmt (mdecl, | |
3354 | build_debugable_stmt (EXPR_WFL_LINECOL (current), | |
3355 | current)); | |
3356 | ||
3357 | end_artificial_method_body (mdecl); | |
3358 | CLASS_HAS_FINIT_P (TREE_TYPE (ctxp->current_parsed_class)) = 1; | |
3359 | ctxp->non_static_initialized = NULL_TREE; | |
3360 | } | |
3361 | ||
e04a16fb AG |
3362 | /* Check whether it is necessary to generate a <clinit> for the class |
3363 | we just parsed. */ | |
3364 | ||
3365 | static void | |
3366 | maybe_generate_clinit () | |
3367 | { | |
22eed1e6 | 3368 | tree mdecl, c; |
e04a16fb AG |
3369 | |
3370 | if (!ctxp->static_initialized || java_error_count) | |
3371 | return; | |
3372 | ||
22eed1e6 APB |
3373 | mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class), |
3374 | ACC_STATIC, void_type_node, | |
de4c7b02 | 3375 | clinit_identifier_node, end_params_node); |
22eed1e6 | 3376 | start_artificial_method_body (mdecl); |
e04a16fb AG |
3377 | |
3378 | /* Keep initialization in order to enforce 8.5 */ | |
3379 | ctxp->static_initialized = nreverse (ctxp->static_initialized); | |
3380 | ||
3381 | /* We process the list of assignment we produced as the result of | |
3382 | the declaration of initialized static field and add them as | |
3383 | statement to the <clinit> method. */ | |
3384 | for (c = ctxp->static_initialized; c; c = TREE_CHAIN (c)) | |
3385 | { | |
3386 | /* We build the assignment expression that will initialize the | |
3387 | field to its value. There are strict rules on static | |
3388 | initializers (8.5). FIXME */ | |
22eed1e6 APB |
3389 | java_method_add_stmt (mdecl, |
3390 | build_debugable_stmt (EXPR_WFL_LINECOL (c), c)); | |
e04a16fb AG |
3391 | } |
3392 | ||
22eed1e6 | 3393 | end_artificial_method_body (mdecl); |
e04a16fb AG |
3394 | ctxp->static_initialized = NULL_TREE; |
3395 | } | |
3396 | ||
3397 | /* Shared accros method_declarator and method_header to remember the | |
3398 | patch stage that was reached during the declaration of the method. | |
3399 | A method DECL is built differently is there is no patch | |
3400 | (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN) | |
3401 | pending on the currently defined method. */ | |
3402 | ||
3403 | static int patch_stage; | |
3404 | ||
3405 | /* Check the method declaration and add the method to its current | |
3406 | class. If the argument list is known to contain incomplete types, | |
3407 | the method is partially added and the registration will be resume | |
22eed1e6 APB |
3408 | once the method arguments resolved. If TYPE is NULL, we're dealing |
3409 | with a constructor. */ | |
e04a16fb AG |
3410 | |
3411 | static tree | |
3412 | method_header (flags, type, mdecl, throws) | |
3413 | int flags; | |
3414 | tree type, mdecl, throws; | |
3415 | { | |
3416 | tree meth = TREE_VALUE (mdecl); | |
3417 | tree id = TREE_PURPOSE (mdecl); | |
1886c9d8 | 3418 | tree type_wfl = NULL_TREE; |
79d13333 APB |
3419 | tree meth_name = NULL_TREE; |
3420 | tree current, orig_arg, this_class; | |
e04a16fb | 3421 | int saved_lineno; |
1886c9d8 | 3422 | int constructor_ok = 0, must_chain; |
e04a16fb AG |
3423 | |
3424 | check_modifiers_consistency (flags); | |
79d13333 APB |
3425 | |
3426 | if (ctxp->current_parsed_class) | |
3427 | this_class = TREE_TYPE (ctxp->current_parsed_class); | |
3428 | else | |
3429 | return NULL_TREE; | |
e04a16fb AG |
3430 | |
3431 | /* There are some forbidden modifiers for an abstract method and its | |
3432 | class must be abstract as well. */ | |
22eed1e6 | 3433 | if (type && (flags & ACC_ABSTRACT)) |
e04a16fb AG |
3434 | { |
3435 | ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private"); | |
3436 | ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static"); | |
3437 | ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final"); | |
3438 | ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native"); | |
3439 | ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized"); | |
2aa11e97 APB |
3440 | if (!CLASS_ABSTRACT (TYPE_NAME (this_class)) |
3441 | && !CLASS_INTERFACE (TYPE_NAME (this_class))) | |
e04a16fb | 3442 | parse_error_context |
781b0558 | 3443 | (id, "Class `%s' must be declared abstract to define abstract method `%s'", |
e04a16fb AG |
3444 | IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)), |
3445 | IDENTIFIER_POINTER (EXPR_WFL_NODE (id))); | |
3446 | } | |
22eed1e6 APB |
3447 | /* Things to be checked when declaring a constructor */ |
3448 | if (!type) | |
3449 | { | |
3450 | int ec = java_error_count; | |
3451 | /* 8.6: Constructor declarations: we might be trying to define a | |
3452 | method without specifying a return type. */ | |
5e942c50 | 3453 | if (EXPR_WFL_NODE (id) != ctxp->current_parsed_class_un) |
22eed1e6 APB |
3454 | parse_error_context |
3455 | (id, "Invalid method declaration, return type required"); | |
3456 | /* 8.6.3: Constructor modifiers */ | |
3457 | else | |
3458 | { | |
3459 | JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract"); | |
3460 | JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static"); | |
3461 | JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final"); | |
3462 | JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native"); | |
3463 | JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized"); | |
3464 | } | |
3465 | /* If we found error here, we don't consider it's OK to tread | |
3466 | the method definition as a constructor, for the rest of this | |
3467 | function */ | |
3468 | if (ec == java_error_count) | |
3469 | constructor_ok = 1; | |
3470 | } | |
e04a16fb AG |
3471 | |
3472 | /* Method declared within the scope of an interface are implicitly | |
3473 | abstract and public. Conflicts with other erroneously provided | |
c0d87ff6 | 3474 | modifiers are checked right after. */ |
e04a16fb AG |
3475 | |
3476 | if (CLASS_INTERFACE (TYPE_NAME (this_class))) | |
3477 | { | |
3478 | /* If FLAGS isn't set because of a modifier, turn the | |
3479 | corresponding modifier WFL to NULL so we issue a warning on | |
3480 | the obsolete use of the modifier */ | |
3481 | if (!(flags & ACC_PUBLIC)) | |
3482 | MODIFIER_WFL (PUBLIC_TK) = NULL; | |
3483 | if (!(flags & ACC_ABSTRACT)) | |
3484 | MODIFIER_WFL (ABSTRACT_TK) = NULL; | |
3485 | flags |= ACC_PUBLIC; | |
3486 | flags |= ACC_ABSTRACT; | |
3487 | } | |
3488 | ||
3489 | /* Modifiers context reset moved up, so abstract method declaration | |
3490 | modifiers can be later checked. */ | |
3491 | ||
22eed1e6 APB |
3492 | /* Set constructor returned type to void and method name to <init>, |
3493 | unless we found an error identifier the constructor (in which | |
3494 | case we retain the original name) */ | |
3495 | if (!type) | |
3496 | { | |
3497 | type = void_type_node; | |
3498 | if (constructor_ok) | |
3499 | meth_name = init_identifier_node; | |
3500 | } | |
3501 | else | |
3502 | meth_name = EXPR_WFL_NODE (id); | |
e04a16fb | 3503 | |
1886c9d8 APB |
3504 | /* Do the returned type resolution and registration if necessary */ |
3505 | SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain); | |
3506 | ||
4a5f66c3 APB |
3507 | if (meth_name) |
3508 | type = build_array_from_name (type, type_wfl, meth_name, &meth_name); | |
1886c9d8 APB |
3509 | EXPR_WFL_NODE (id) = meth_name; |
3510 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); | |
3511 | ||
3512 | if (must_chain) | |
e04a16fb | 3513 | { |
1886c9d8 APB |
3514 | patch_stage = JDEP_METHOD_RETURN; |
3515 | register_incomplete_type (patch_stage, type_wfl, id, type); | |
3516 | TREE_TYPE (meth) = GET_REAL_TYPE (type); | |
e04a16fb AG |
3517 | } |
3518 | else | |
1886c9d8 | 3519 | TREE_TYPE (meth) = type; |
e04a16fb AG |
3520 | |
3521 | saved_lineno = lineno; | |
3522 | /* When defining an abstract or interface method, the curly | |
3523 | bracket at level 1 doesn't exist because there is no function | |
3524 | body */ | |
3525 | lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 : | |
3526 | EXPR_WFL_LINENO (id)); | |
3527 | ||
5e942c50 APB |
3528 | /* Remember the original argument list */ |
3529 | orig_arg = TYPE_ARG_TYPES (meth); | |
3530 | ||
e04a16fb AG |
3531 | if (patch_stage) /* includes ret type and/or all args */ |
3532 | { | |
3533 | jdep *jdep; | |
3534 | meth = add_method_1 (this_class, flags, meth_name, meth); | |
3535 | /* Patch for the return type */ | |
3536 | if (patch_stage == JDEP_METHOD_RETURN) | |
3537 | { | |
3538 | jdep = CLASSD_LAST (ctxp->classd_list); | |
3539 | JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth)); | |
3540 | } | |
3541 | /* This is the stop JDEP. METH allows the function's signature | |
3542 | to be computed. */ | |
3543 | register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE); | |
3544 | } | |
3545 | else | |
5e942c50 APB |
3546 | meth = add_method (this_class, flags, meth_name, |
3547 | build_java_signature (meth)); | |
3548 | ||
3549 | /* Fix the method argument list so we have the argument name | |
3550 | information */ | |
3551 | fix_method_argument_names (orig_arg, meth); | |
3552 | ||
3553 | /* Register the parameter number and re-install the current line | |
3554 | number */ | |
e04a16fb AG |
3555 | DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1; |
3556 | lineno = saved_lineno; | |
b9f7e36c APB |
3557 | |
3558 | /* Register exception specified by the `throws' keyword for | |
3559 | resolution and set the method decl appropriate field to the list. | |
3560 | Note: the grammar ensures that what we get here are class | |
3561 | types. */ | |
3562 | if (throws) | |
3563 | { | |
3564 | throws = nreverse (throws); | |
3565 | for (current = throws; current; current = TREE_CHAIN (current)) | |
3566 | { | |
3567 | register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current), | |
3568 | NULL_TREE, NULL_TREE); | |
3569 | JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) = | |
3570 | &TREE_VALUE (current); | |
3571 | } | |
3572 | DECL_FUNCTION_THROWS (meth) = throws; | |
3573 | } | |
3574 | ||
e04a16fb AG |
3575 | /* We set the DECL_NAME to ID so we can track the location where |
3576 | the function was declared. This allow us to report | |
3577 | redefinition error accurately. When method are verified, | |
3578 | DECL_NAME is reinstalled properly (using the content of the | |
3579 | WFL node ID) (see check_method_redefinition). We don't do that | |
22eed1e6 APB |
3580 | when Object is being defined. Constructor <init> names will be |
3581 | reinstalled the same way. */ | |
e04a16fb AG |
3582 | if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node) |
3583 | DECL_NAME (meth) = id; | |
22eed1e6 APB |
3584 | |
3585 | /* Set the flag if we correctly processed a constructor */ | |
3586 | if (constructor_ok) | |
3587 | DECL_CONSTRUCTOR_P (meth) = 1; | |
3588 | ||
5e942c50 APB |
3589 | /* Eventually set the @deprecated tag flag */ |
3590 | CHECK_DEPRECATED (meth); | |
3591 | ||
7f10c2e2 APB |
3592 | /* If doing xref, store column and line number information instead |
3593 | of the line number only. */ | |
3594 | if (flag_emit_xref) | |
3595 | DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id); | |
3596 | ||
e04a16fb AG |
3597 | return meth; |
3598 | } | |
3599 | ||
5e942c50 APB |
3600 | static void |
3601 | fix_method_argument_names (orig_arg, meth) | |
3602 | tree orig_arg, meth; | |
3603 | { | |
3604 | tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth)); | |
3605 | if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE) | |
3606 | { | |
3607 | TREE_PURPOSE (arg) = this_identifier_node; | |
3608 | arg = TREE_CHAIN (arg); | |
3609 | } | |
de4c7b02 | 3610 | while (orig_arg != end_params_node) |
5e942c50 APB |
3611 | { |
3612 | TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg); | |
3613 | orig_arg = TREE_CHAIN (orig_arg); | |
3614 | arg = TREE_CHAIN (arg); | |
3615 | } | |
3616 | } | |
3617 | ||
22eed1e6 APB |
3618 | /* Complete the method declaration with METHOD_BODY. */ |
3619 | ||
3620 | static void | |
b635eb2f | 3621 | finish_method_declaration (method_body) |
22eed1e6 APB |
3622 | tree method_body; |
3623 | { | |
79d13333 APB |
3624 | int flags; |
3625 | ||
3626 | if (!current_function_decl) | |
3627 | return; | |
3628 | ||
3629 | flags = get_access_flags_from_decl (current_function_decl); | |
5256aa37 APB |
3630 | |
3631 | /* 8.4.5 Method Body */ | |
3632 | if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body) | |
3633 | { | |
3634 | tree wfl = DECL_NAME (current_function_decl); | |
3635 | parse_error_context (wfl, | |
3636 | "%s method `%s' can't have a body defined", | |
3637 | (METHOD_NATIVE (current_function_decl) ? | |
3638 | "Native" : "Abstract"), | |
3639 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
3640 | method_body = NULL_TREE; | |
3641 | } | |
3642 | else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body) | |
3643 | { | |
3644 | tree wfl = DECL_NAME (current_function_decl); | |
781b0558 KG |
3645 | parse_error_context |
3646 | (wfl, | |
3647 | "Non native and non abstract method `%s' must have a body defined", | |
3648 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
5256aa37 APB |
3649 | method_body = NULL_TREE; |
3650 | } | |
3651 | ||
2c56429a APB |
3652 | if (flag_emit_class_files && method_body |
3653 | && TREE_CODE (method_body) == NOP_EXPR | |
3654 | && TREE_TYPE (current_function_decl) | |
3655 | && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node) | |
3656 | method_body = build1 (RETURN_EXPR, void_type_node, NULL); | |
3657 | ||
22eed1e6 APB |
3658 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body; |
3659 | maybe_absorb_scoping_blocks (); | |
3660 | /* Exit function's body */ | |
3661 | exit_block (); | |
3662 | /* Merge last line of the function with first line, directly in the | |
3663 | function decl. It will be used to emit correct debug info. */ | |
7f10c2e2 APB |
3664 | if (!flag_emit_xref) |
3665 | DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1); | |
f099f336 APB |
3666 | /* So we don't have an irrelevant function declaration context for |
3667 | the next static block we'll see. */ | |
3668 | current_function_decl = NULL_TREE; | |
22eed1e6 APB |
3669 | } |
3670 | ||
3671 | /* Build a an error message for constructor circularity errors. */ | |
3672 | ||
3673 | static char * | |
3674 | constructor_circularity_msg (from, to) | |
3675 | tree from, to; | |
3676 | { | |
3677 | static char string [4096]; | |
c2e3db92 | 3678 | char *t = xstrdup (lang_printable_name (from, 0)); |
22eed1e6 APB |
3679 | sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0)); |
3680 | free (t); | |
3681 | return string; | |
3682 | } | |
3683 | ||
3684 | /* Verify a circular call to METH. Return 1 if an error is found, 0 | |
3685 | otherwise. */ | |
3686 | ||
3687 | static int | |
3688 | verify_constructor_circularity (meth, current) | |
3689 | tree meth, current; | |
3690 | { | |
3691 | static tree list = NULL_TREE; | |
3692 | tree c; | |
3693 | for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c)) | |
3694 | { | |
3695 | if (TREE_VALUE (c) == meth) | |
3696 | { | |
3697 | char *t; | |
3698 | if (list) | |
3699 | { | |
3700 | tree liste; | |
3701 | list = nreverse (list); | |
3702 | for (liste = list; liste; liste = TREE_CHAIN (liste)) | |
3703 | { | |
3704 | parse_error_context | |
c63b98cd | 3705 | (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s", |
22eed1e6 APB |
3706 | constructor_circularity_msg |
3707 | (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste)))); | |
3708 | java_error_count--; | |
3709 | } | |
3710 | } | |
c2e3db92 | 3711 | t = xstrdup (lang_printable_name (meth, 0)); |
22eed1e6 APB |
3712 | parse_error_context (TREE_PURPOSE (c), |
3713 | "%s: recursive invocation of constructor `%s'", | |
3714 | constructor_circularity_msg (current, meth), t); | |
3715 | free (t); | |
3716 | list = NULL_TREE; | |
3717 | return 1; | |
3718 | } | |
3719 | } | |
3720 | for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c)) | |
3721 | { | |
3722 | list = tree_cons (c, current, list); | |
3723 | if (verify_constructor_circularity (meth, TREE_VALUE (c))) | |
3724 | return 1; | |
3725 | list = TREE_CHAIN (list); | |
3726 | } | |
3727 | return 0; | |
3728 | } | |
3729 | ||
e04a16fb AG |
3730 | /* Check modifiers that can be declared but exclusively */ |
3731 | ||
3732 | static void | |
3733 | check_modifiers_consistency (flags) | |
3734 | int flags; | |
3735 | { | |
3736 | int acc_count = 0; | |
3737 | tree cl = NULL_TREE; | |
3738 | ||
3739 | THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, 0, acc_count, cl); | |
3740 | THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, 1, acc_count, cl); | |
3741 | THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, 2, acc_count, cl); | |
3742 | if (acc_count > 1) | |
3743 | parse_error_context | |
781b0558 | 3744 | (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified"); |
e04a16fb AG |
3745 | } |
3746 | ||
3747 | /* Check the methode header METH for abstract specifics features */ | |
3748 | ||
3749 | static void | |
3750 | check_abstract_method_header (meth) | |
3751 | tree meth; | |
3752 | { | |
3753 | int flags = get_access_flags_from_decl (meth); | |
3754 | /* DECL_NAME might still be a WFL node */ | |
c877974e | 3755 | tree name = GET_METHOD_NAME (meth); |
e04a16fb AG |
3756 | |
3757 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (ABSTRACT_TK), flags, | |
3758 | ACC_ABSTRACT, "abstract method `%s'", | |
3759 | IDENTIFIER_POINTER (name)); | |
3760 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), flags, | |
3761 | ACC_PUBLIC, "abstract method `%s'", | |
3762 | IDENTIFIER_POINTER (name)); | |
3763 | ||
3764 | check_modifiers ("Illegal modifier `%s' for interface method", | |
3765 | flags, INTERFACE_METHOD_MODIFIERS); | |
3766 | } | |
3767 | ||
3768 | /* Create a FUNCTION_TYPE node and start augmenting it with the | |
3769 | declared function arguments. Arguments type that can't be resolved | |
3770 | are left as they are, but the returned node is marked as containing | |
3771 | incomplete types. */ | |
3772 | ||
3773 | static tree | |
3774 | method_declarator (id, list) | |
3775 | tree id, list; | |
3776 | { | |
3777 | tree arg_types = NULL_TREE, current, node; | |
3778 | tree meth = make_node (FUNCTION_TYPE); | |
3779 | jdep *jdep; | |
e04a16fb AG |
3780 | |
3781 | patch_stage = JDEP_NO_PATCH; | |
3782 | ||
3783 | for (current = list; current; current = TREE_CHAIN (current)) | |
3784 | { | |
c583dd46 | 3785 | int must_chain = 0; |
e04a16fb AG |
3786 | tree wfl_name = TREE_PURPOSE (current); |
3787 | tree type = TREE_VALUE (current); | |
3788 | tree name = EXPR_WFL_NODE (wfl_name); | |
c583dd46 APB |
3789 | tree already, arg_node; |
3790 | tree type_wfl = NULL_TREE; | |
23a79c61 | 3791 | tree real_type; |
c583dd46 APB |
3792 | |
3793 | /* Obtain a suitable type for resolution, if necessary */ | |
3794 | SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain); | |
3795 | ||
3796 | /* Process NAME, as it may specify extra dimension(s) for it */ | |
3797 | type = build_array_from_name (type, type_wfl, name, &name); | |
3798 | EXPR_WFL_NODE (wfl_name) = name; | |
e04a16fb | 3799 | |
23a79c61 APB |
3800 | real_type = GET_REAL_TYPE (type); |
3801 | if (TREE_CODE (real_type) == RECORD_TYPE) | |
3802 | { | |
3803 | real_type = promote_type (real_type); | |
3804 | if (TREE_CODE (type) == TREE_LIST) | |
3805 | TREE_PURPOSE (type) = real_type; | |
3806 | } | |
5e942c50 | 3807 | |
e04a16fb AG |
3808 | /* Check redefinition */ |
3809 | for (already = arg_types; already; already = TREE_CHAIN (already)) | |
3810 | if (TREE_PURPOSE (already) == name) | |
3811 | { | |
781b0558 KG |
3812 | parse_error_context |
3813 | (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'", | |
3814 | IDENTIFIER_POINTER (name), | |
e04a16fb AG |
3815 | IDENTIFIER_POINTER (EXPR_WFL_NODE (id))); |
3816 | break; | |
3817 | } | |
3818 | ||
3819 | /* If we've an incomplete argument type, we know there is a location | |
3820 | to patch when the type get resolved, later. */ | |
3821 | jdep = NULL; | |
c583dd46 | 3822 | if (must_chain) |
e04a16fb | 3823 | { |
c583dd46 APB |
3824 | patch_stage = JDEP_METHOD; |
3825 | type = register_incomplete_type (patch_stage, | |
3826 | type_wfl, wfl_name, type); | |
3827 | jdep = CLASSD_LAST (ctxp->classd_list); | |
3828 | JDEP_MISC (jdep) = id; | |
e04a16fb | 3829 | } |
c583dd46 | 3830 | |
e04a16fb | 3831 | /* The argument node: a name and a (possibly) incomplete type */ |
23a79c61 | 3832 | arg_node = build_tree_list (name, real_type); |
e04a16fb AG |
3833 | if (jdep) |
3834 | JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node); | |
3835 | TREE_CHAIN (arg_node) = arg_types; | |
3836 | arg_types = arg_node; | |
3837 | } | |
de4c7b02 | 3838 | TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node); |
e04a16fb AG |
3839 | node = build_tree_list (id, meth); |
3840 | return node; | |
3841 | } | |
3842 | ||
3843 | static int | |
3844 | unresolved_type_p (wfl, returned) | |
3845 | tree wfl; | |
3846 | tree *returned; | |
3847 | ||
3848 | { | |
3849 | if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION) | |
3850 | { | |
3851 | tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl)); | |
3852 | if (returned) | |
3853 | *returned = (decl ? TREE_TYPE (decl) : NULL_TREE); | |
3854 | return 1; | |
3855 | } | |
3856 | if (returned) | |
3857 | *returned = wfl; | |
3858 | return 0; | |
3859 | } | |
3860 | ||
3861 | /* From NAME, build a qualified identifier node using the | |
3862 | qualification from the current package definition. */ | |
3863 | ||
3864 | static tree | |
3865 | parser_qualified_classname (name) | |
3866 | tree name; | |
3867 | { | |
3868 | if (ctxp->package) | |
3869 | return merge_qualified_name (ctxp->package, EXPR_WFL_NODE (name)); | |
3870 | else | |
3871 | return EXPR_WFL_NODE (name); | |
3872 | } | |
3873 | ||
3874 | /* Called once the type a interface extends is resolved. Returns 0 if | |
3875 | everything is OK. */ | |
3876 | ||
3877 | static int | |
3878 | parser_check_super_interface (super_decl, this_decl, this_wfl) | |
3879 | tree super_decl, this_decl, this_wfl; | |
3880 | { | |
3881 | tree super_type = TREE_TYPE (super_decl); | |
3882 | ||
3883 | /* Has to be an interface */ | |
3884 | if (!CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (super_decl)))) | |
3885 | { | |
3886 | parse_error_context | |
3887 | (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'", | |
3888 | (TYPE_ARRAY_P (super_type) ? "array" : "class"), | |
3889 | IDENTIFIER_POINTER (DECL_NAME (super_decl)), | |
3890 | (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ? | |
3891 | "interface" : "class"), | |
3892 | IDENTIFIER_POINTER (DECL_NAME (this_decl))); | |
3893 | return 1; | |
3894 | } | |
3895 | ||
3896 | /* Check scope: same package OK, other package: OK if public */ | |
3897 | if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl))) | |
3898 | return 1; | |
3899 | ||
3900 | SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s", | |
3901 | IDENTIFIER_POINTER (DECL_NAME (this_decl)), | |
3902 | IDENTIFIER_POINTER (DECL_NAME (super_decl)))); | |
3903 | return 0; | |
3904 | } | |
3905 | ||
3906 | /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns | |
3907 | 0 if everthing is OK. */ | |
3908 | ||
3909 | static int | |
3910 | parser_check_super (super_decl, this_decl, wfl) | |
3911 | tree super_decl, this_decl, wfl; | |
3912 | { | |
e04a16fb AG |
3913 | tree super_type = TREE_TYPE (super_decl); |
3914 | ||
3915 | /* SUPER should be a CLASS (neither an array nor an interface) */ | |
3916 | if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type))) | |
3917 | { | |
3918 | parse_error_context | |
3919 | (wfl, "Class `%s' can't subclass %s `%s'", | |
3920 | IDENTIFIER_POINTER (DECL_NAME (this_decl)), | |
3921 | (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"), | |
3922 | IDENTIFIER_POINTER (DECL_NAME (super_decl))); | |
3923 | return 1; | |
3924 | } | |
3925 | ||
3926 | if (CLASS_FINAL (TYPE_NAME (super_type))) | |
3927 | { | |
3928 | parse_error_context (wfl, "Can't subclass final classes: %s", | |
3929 | IDENTIFIER_POINTER (DECL_NAME (super_decl))); | |
3930 | return 1; | |
3931 | } | |
3932 | ||
3933 | /* Check scope: same package OK, other package: OK if public */ | |
3934 | if (check_pkg_class_access (DECL_NAME (super_decl), wfl)) | |
3935 | return 1; | |
3936 | ||
3937 | SOURCE_FRONTEND_DEBUG (("Completing class %s with %s", | |
3938 | IDENTIFIER_POINTER (DECL_NAME (this_decl)), | |
3939 | IDENTIFIER_POINTER (DECL_NAME (super_decl)))); | |
3940 | return 0; | |
3941 | } | |
3942 | ||
3943 | /* Create a new dependency list and link it (in a LIFO manner) to the | |
3944 | CTXP list of type dependency list. */ | |
3945 | ||
3946 | static void | |
3947 | create_jdep_list (ctxp) | |
3948 | struct parser_ctxt *ctxp; | |
3949 | { | |
23a79c61 | 3950 | jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist)); |
e04a16fb AG |
3951 | new->first = new->last = NULL; |
3952 | new->next = ctxp->classd_list; | |
3953 | ctxp->classd_list = new; | |
3954 | } | |
3955 | ||
3956 | static jdeplist * | |
3957 | reverse_jdep_list (ctxp) | |
3958 | struct parser_ctxt *ctxp; | |
3959 | { | |
3960 | register jdeplist *prev = NULL, *current, *next; | |
3961 | for (current = ctxp->classd_list; current; current = next) | |
3962 | { | |
3963 | next = current->next; | |
3964 | current->next = prev; | |
3965 | prev = current; | |
3966 | } | |
3967 | return prev; | |
3968 | } | |
3969 | ||
23a79c61 APB |
3970 | /* Create a fake pointer based on the ID stored in |
3971 | TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be | |
3972 | registered again. */ | |
e04a16fb AG |
3973 | |
3974 | static tree | |
23a79c61 APB |
3975 | obtain_incomplete_type (type_name) |
3976 | tree type_name; | |
e04a16fb | 3977 | { |
23a79c61 APB |
3978 | tree ptr, name; |
3979 | ||
3980 | if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION) | |
3981 | name = EXPR_WFL_NODE (type_name); | |
3982 | else if (INCOMPLETE_TYPE_P (type_name)) | |
3983 | name = TYPE_NAME (type_name); | |
3984 | else | |
3985 | fatal ("invalid type name - obtain_incomplete_type"); | |
e04a16fb AG |
3986 | |
3987 | for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr)) | |
78d21f92 | 3988 | if (TYPE_NAME (ptr) == name) |
e04a16fb AG |
3989 | break; |
3990 | ||
3991 | if (!ptr) | |
3992 | { | |
e04a16fb | 3993 | push_obstacks (&permanent_obstack, &permanent_obstack); |
78d21f92 PB |
3994 | BUILD_PTR_FROM_NAME (ptr, name); |
3995 | layout_type (ptr); | |
e04a16fb AG |
3996 | pop_obstacks (); |
3997 | TREE_CHAIN (ptr) = ctxp->incomplete_class; | |
3998 | ctxp->incomplete_class = ptr; | |
3999 | } | |
4000 | ||
4001 | return ptr; | |
4002 | } | |
4003 | ||
4004 | /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is | |
4005 | non NULL instead of computing a new fake type based on WFL. The new | |
4006 | dependency is inserted in the current type dependency list, in FIFO | |
4007 | manner. */ | |
4008 | ||
4009 | static tree | |
4010 | register_incomplete_type (kind, wfl, decl, ptr) | |
4011 | int kind; | |
4012 | tree wfl, decl, ptr; | |
4013 | { | |
23a79c61 | 4014 | jdep *new = (jdep *)xmalloc (sizeof (jdep)); |
e04a16fb | 4015 | |
e04a16fb AG |
4016 | if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */ |
4017 | ptr = obtain_incomplete_type (wfl); | |
4018 | ||
4019 | JDEP_KIND (new) = kind; | |
4020 | JDEP_DECL (new) = decl; | |
4021 | JDEP_SOLV (new) = ptr; | |
4022 | JDEP_WFL (new) = wfl; | |
4023 | JDEP_CHAIN (new) = NULL; | |
4024 | JDEP_MISC (new) = NULL_TREE; | |
4025 | JDEP_GET_PATCH (new) = (tree *)NULL; | |
4026 | ||
4027 | JDEP_INSERT (ctxp->classd_list, new); | |
4028 | ||
4029 | return ptr; | |
4030 | } | |
4031 | ||
4032 | void | |
4033 | java_check_circular_reference () | |
4034 | { | |
4035 | tree current; | |
4036 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) | |
4037 | { | |
4038 | tree type = TREE_TYPE (current); | |
e920ebc9 | 4039 | if (CLASS_INTERFACE (current)) |
e04a16fb AG |
4040 | { |
4041 | /* Check all interfaces this class extends */ | |
4042 | tree basetype_vec = TYPE_BINFO_BASETYPES (type); | |
4043 | int n, i; | |
4044 | ||
4045 | if (!basetype_vec) | |
4046 | return; | |
4047 | n = TREE_VEC_LENGTH (basetype_vec); | |
4048 | for (i = 0; i < n; i++) | |
4049 | { | |
4050 | tree vec_elt = TREE_VEC_ELT (basetype_vec, i); | |
4051 | if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node | |
4052 | && interface_of_p (type, BINFO_TYPE (vec_elt))) | |
4053 | parse_error_context (lookup_cl (current), | |
4054 | "Cyclic interface inheritance"); | |
4055 | } | |
4056 | } | |
4057 | else | |
4058 | if (inherits_from_p (CLASSTYPE_SUPER (type), type)) | |
4059 | parse_error_context (lookup_cl (current), | |
4060 | "Cyclic class inheritance"); | |
4061 | } | |
4062 | } | |
4063 | ||
e920ebc9 APB |
4064 | /* Fix the constructors. This will be called right after circular |
4065 | references have been checked. It is necessary to fix constructors | |
4066 | early even if no code generation will take place for that class: | |
4067 | some generated constructor might be required by the class whose | |
4068 | compilation triggered this one to be simply loaded. */ | |
4069 | ||
4070 | void | |
4071 | java_fix_constructors () | |
4072 | { | |
4073 | tree current; | |
4074 | ||
4075 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) | |
4076 | { | |
4077 | tree decl; | |
4078 | tree class_type = TREE_TYPE (current); | |
4079 | int saw_ctor = 0; | |
4080 | ||
4081 | for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl)) | |
4082 | { | |
4083 | if (DECL_CONSTRUCTOR_P (decl)) | |
4084 | { | |
4085 | fix_constructors (decl); | |
4086 | saw_ctor = 1; | |
4087 | } | |
4088 | } | |
4089 | ||
4090 | if (!saw_ctor) | |
4091 | { | |
4092 | int flags = (get_access_flags_from_decl (current) & ACC_PUBLIC ? | |
4093 | ACC_PUBLIC : 0); | |
4094 | decl = create_artificial_method (class_type, flags, void_type_node, | |
4095 | init_identifier_node, | |
4096 | end_params_node); | |
4097 | DECL_CONSTRUCTOR_P (decl) = 1; | |
4098 | } | |
4099 | } | |
4100 | } | |
4101 | ||
23a79c61 APB |
4102 | /* safe_layout_class just makes sure that we can load a class without |
4103 | disrupting the current_class, input_file, lineno, etc, information | |
4104 | about the class processed currently. */ | |
4105 | ||
e04a16fb AG |
4106 | void |
4107 | safe_layout_class (class) | |
4108 | tree class; | |
4109 | { | |
4110 | tree save_current_class = current_class; | |
4111 | char *save_input_filename = input_filename; | |
4112 | int save_lineno = lineno; | |
5e942c50 | 4113 | |
e04a16fb | 4114 | push_obstacks (&permanent_obstack, &permanent_obstack); |
5e942c50 | 4115 | |
e04a16fb AG |
4116 | layout_class (class); |
4117 | pop_obstacks (); | |
5e942c50 | 4118 | |
e04a16fb AG |
4119 | current_class = save_current_class; |
4120 | input_filename = save_input_filename; | |
4121 | lineno = save_lineno; | |
4122 | CLASS_LOADED_P (class) = 1; | |
4123 | } | |
4124 | ||
4125 | static tree | |
4126 | jdep_resolve_class (dep) | |
4127 | jdep *dep; | |
4128 | { | |
4129 | tree decl; | |
4130 | ||
23a79c61 APB |
4131 | if (JDEP_RESOLVED_P (dep)) |
4132 | decl = JDEP_RESOLVED_DECL (dep); | |
4133 | else | |
e04a16fb | 4134 | { |
23a79c61 APB |
4135 | decl = resolve_class (JDEP_TO_RESOLVE (dep), |
4136 | JDEP_DECL (dep), JDEP_WFL (dep)); | |
e04a16fb AG |
4137 | JDEP_RESOLVED (dep, decl); |
4138 | } | |
23a79c61 | 4139 | |
e04a16fb | 4140 | if (!decl) |
23a79c61 APB |
4141 | complete_class_report_errors (dep); |
4142 | ||
e04a16fb AG |
4143 | return decl; |
4144 | } | |
4145 | ||
4146 | /* Complete unsatisfied class declaration and their dependencies */ | |
4147 | ||
4148 | void | |
4149 | java_complete_class () | |
4150 | { | |
e04a16fb AG |
4151 | tree cclass; |
4152 | jdeplist *cclassd; | |
4153 | int error_found; | |
b67d701b | 4154 | tree type; |
e04a16fb AG |
4155 | |
4156 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
4157 | ||
4158 | /* Process imports and reverse the import on demand list */ | |
4159 | process_imports (); | |
4160 | if (ctxp->import_demand_list) | |
4161 | ctxp->import_demand_list = nreverse (ctxp->import_demand_list); | |
4162 | ||
4163 | /* Rever things so we have the right order */ | |
4164 | ctxp->class_list = nreverse (ctxp->class_list); | |
4165 | ctxp->classd_list = reverse_jdep_list (ctxp); | |
c877974e | 4166 | |
e04a16fb AG |
4167 | for (cclassd = ctxp->classd_list, cclass = ctxp->class_list; |
4168 | cclass && cclassd; | |
4169 | cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd)) | |
4170 | { | |
4171 | jdep *dep; | |
4172 | for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep)) | |
4173 | { | |
4174 | tree decl; | |
e04a16fb AG |
4175 | if (!(decl = jdep_resolve_class (dep))) |
4176 | continue; | |
4177 | ||
4178 | /* Now it's time to patch */ | |
4179 | switch (JDEP_KIND (dep)) | |
4180 | { | |
4181 | case JDEP_SUPER: | |
4182 | /* Simply patch super */ | |
4183 | if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep))) | |
4184 | continue; | |
4185 | BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO | |
4186 | (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl); | |
4187 | break; | |
4188 | ||
4189 | case JDEP_FIELD: | |
4190 | { | |
4191 | /* We do part of the job done in add_field */ | |
4192 | tree field_decl = JDEP_DECL (dep); | |
4193 | tree field_type = TREE_TYPE (decl); | |
4194 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
e04a16fb | 4195 | if (TREE_CODE (field_type) == RECORD_TYPE) |
e04a16fb AG |
4196 | field_type = promote_type (field_type); |
4197 | pop_obstacks (); | |
4198 | TREE_TYPE (field_decl) = field_type; | |
5e942c50 APB |
4199 | DECL_ALIGN (field_decl) = 0; |
4200 | layout_decl (field_decl, 0); | |
e04a16fb AG |
4201 | SOURCE_FRONTEND_DEBUG |
4202 | (("Completed field/var decl `%s' with `%s'", | |
4203 | IDENTIFIER_POINTER (DECL_NAME (field_decl)), | |
4204 | IDENTIFIER_POINTER (DECL_NAME (decl)))); | |
4205 | break; | |
4206 | } | |
4207 | case JDEP_METHOD: /* We start patching a method */ | |
4208 | case JDEP_METHOD_RETURN: | |
4209 | error_found = 0; | |
4210 | while (1) | |
4211 | { | |
4212 | if (decl) | |
4213 | { | |
b67d701b PB |
4214 | type = TREE_TYPE(decl); |
4215 | if (TREE_CODE (type) == RECORD_TYPE) | |
4216 | type = promote_type (type); | |
e04a16fb AG |
4217 | JDEP_APPLY_PATCH (dep, type); |
4218 | SOURCE_FRONTEND_DEBUG | |
4219 | (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ? | |
4220 | "Completing fct `%s' with ret type `%s'": | |
4221 | "Completing arg `%s' with type `%s'"), | |
4222 | IDENTIFIER_POINTER (EXPR_WFL_NODE | |
4223 | (JDEP_DECL_WFL (dep))), | |
4224 | IDENTIFIER_POINTER (DECL_NAME (decl)))); | |
4225 | } | |
4226 | else | |
4227 | error_found = 1; | |
4228 | dep = JDEP_CHAIN (dep); | |
4229 | if (JDEP_KIND (dep) == JDEP_METHOD_END) | |
4230 | break; | |
4231 | else | |
4232 | decl = jdep_resolve_class (dep); | |
4233 | } | |
4234 | if (!error_found) | |
4235 | { | |
4236 | tree mdecl = JDEP_DECL (dep), signature; | |
4237 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
4238 | /* Recompute and reset the signature */ | |
4239 | signature = build_java_signature (TREE_TYPE (mdecl)); | |
4240 | set_java_signature (TREE_TYPE (mdecl), signature); | |
4241 | pop_obstacks (); | |
4242 | } | |
4243 | else | |
4244 | continue; | |
4245 | break; | |
4246 | ||
4247 | case JDEP_INTERFACE: | |
4248 | if (parser_check_super_interface (decl, JDEP_DECL (dep), | |
4249 | JDEP_WFL (dep))) | |
4250 | continue; | |
4251 | parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep)); | |
4252 | break; | |
4253 | ||
b67d701b | 4254 | case JDEP_PARM: |
e04a16fb | 4255 | case JDEP_VARIABLE: |
b67d701b PB |
4256 | type = TREE_TYPE(decl); |
4257 | if (TREE_CODE (type) == RECORD_TYPE) | |
4258 | type = promote_type (type); | |
4259 | JDEP_APPLY_PATCH (dep, type); | |
e04a16fb AG |
4260 | break; |
4261 | ||
4262 | case JDEP_TYPE: | |
4263 | JDEP_APPLY_PATCH (dep, TREE_TYPE (decl)); | |
4264 | SOURCE_FRONTEND_DEBUG | |
4265 | (("Completing a random type dependency on a '%s' node", | |
4266 | tree_code_name [TREE_CODE (JDEP_DECL (dep))])); | |
4267 | break; | |
4268 | ||
b9f7e36c | 4269 | case JDEP_EXCEPTION: |
c877974e APB |
4270 | JDEP_APPLY_PATCH (dep, TREE_TYPE (decl)); |
4271 | SOURCE_FRONTEND_DEBUG | |
4272 | (("Completing `%s' `throws' argument node", | |
4273 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))))); | |
b9f7e36c APB |
4274 | break; |
4275 | ||
e04a16fb | 4276 | default: |
0a2138e2 APB |
4277 | fatal ("Can't handle patch code %d - java_complete_class", |
4278 | JDEP_KIND (dep)); | |
e04a16fb AG |
4279 | } |
4280 | } | |
4281 | } | |
4282 | pop_obstacks (); | |
4283 | return; | |
4284 | } | |
4285 | ||
4286 | /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an | |
4287 | array. */ | |
4288 | ||
4289 | static tree | |
4290 | resolve_class (class_type, decl, cl) | |
4291 | tree class_type, decl, cl; | |
4292 | { | |
49f48c71 KG |
4293 | const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type)); |
4294 | const char *base = name; | |
78d21f92 PB |
4295 | tree resolved_type = TREE_TYPE (class_type); |
4296 | tree resolved_type_decl; | |
e04a16fb | 4297 | |
78d21f92 PB |
4298 | if (resolved_type != NULL_TREE) |
4299 | { | |
4300 | tree resolved_type_decl = TYPE_NAME (resolved_type); | |
4301 | if (resolved_type_decl == NULL_TREE | |
4302 | || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE) | |
4303 | { | |
4304 | resolved_type_decl = build_decl (TYPE_DECL, | |
4305 | TYPE_NAME (class_type), | |
4306 | resolved_type); | |
4307 | } | |
4308 | return resolved_type_decl; | |
4309 | } | |
4310 | ||
e04a16fb AG |
4311 | /* 1- Check to see if we have an array. If true, find what we really |
4312 | want to resolve */ | |
4313 | while (name[0] == '[') | |
4314 | name++; | |
4315 | if (base != name) | |
4316 | TYPE_NAME (class_type) = get_identifier (name); | |
4317 | ||
4318 | /* 2- Resolve the bare type */ | |
4319 | if (!(resolved_type_decl = do_resolve_class (class_type, decl, cl))) | |
4320 | return NULL_TREE; | |
4321 | resolved_type = TREE_TYPE (resolved_type_decl); | |
4322 | ||
4323 | /* 3- If we have and array, reconstruct the array down to its nesting */ | |
4324 | if (base != name) | |
4325 | { | |
4326 | while (base != name) | |
4327 | { | |
4328 | if (TREE_CODE (resolved_type) == RECORD_TYPE) | |
4329 | resolved_type = promote_type (resolved_type); | |
4330 | resolved_type = build_java_array_type (resolved_type, -1); | |
c583dd46 | 4331 | CLASS_LOADED_P (resolved_type) = 1; |
e04a16fb AG |
4332 | name--; |
4333 | } | |
4334 | /* Build a fake decl for this, since this is what is expected to | |
4335 | be returned. */ | |
4336 | resolved_type_decl = | |
4337 | build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type); | |
4338 | /* Figure how those two things are important for error report. FIXME */ | |
4339 | DECL_SOURCE_LINE (resolved_type_decl) = 0; | |
4340 | DECL_SOURCE_FILE (resolved_type_decl) = input_filename; | |
78d21f92 | 4341 | TYPE_NAME (class_type) = TYPE_NAME (resolved_type); |
e04a16fb | 4342 | } |
78d21f92 | 4343 | TREE_TYPE (class_type) = resolved_type; |
e04a16fb AG |
4344 | return resolved_type_decl; |
4345 | } | |
4346 | ||
4347 | /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL | |
4348 | are used to report error messages. */ | |
4349 | ||
78d21f92 | 4350 | tree |
e04a16fb AG |
4351 | do_resolve_class (class_type, decl, cl) |
4352 | tree class_type; | |
4353 | tree decl; | |
4354 | tree cl; | |
4355 | { | |
4356 | tree new_class_decl; | |
4357 | tree original_name = NULL_TREE; | |
4358 | ||
4359 | /* Do not try to replace TYPE_NAME (class_type) by a variable, since | |
4360 | its is changed by find_in_imports{_on_demand} */ | |
4361 | ||
4362 | /* 1- Check for the type in single imports */ | |
4363 | if (find_in_imports (class_type)) | |
4364 | return NULL_TREE; | |
4365 | ||
4366 | /* 2- And check for the type in the current compilation unit. If it fails, | |
ee07f4f4 | 4367 | try with a name qualified with the package name we've seen so far */ |
e04a16fb AG |
4368 | if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)))) |
4369 | { | |
4370 | if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) && | |
4371 | !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl))) | |
4372 | load_class (TYPE_NAME (class_type), 0); | |
4373 | return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
4374 | } | |
4375 | ||
4376 | original_name = TYPE_NAME (class_type); | |
ee07f4f4 | 4377 | if (!QUALIFIED_P (TYPE_NAME (class_type))) |
bc3ca41b | 4378 | { |
ee07f4f4 APB |
4379 | tree package; |
4380 | for (package = package_list; package; package = TREE_CHAIN (package)) | |
bc3ca41b | 4381 | { |
ee07f4f4 APB |
4382 | tree new_qualified; |
4383 | ||
4384 | new_qualified = merge_qualified_name (TREE_PURPOSE (package), | |
4385 | original_name); | |
4386 | TYPE_NAME (class_type) = new_qualified; | |
4387 | new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
4388 | if (!new_class_decl) | |
4389 | load_class (TYPE_NAME (class_type), 0); | |
4390 | new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
4391 | if (new_class_decl) | |
4392 | { | |
4393 | if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) && | |
4394 | !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl))) | |
4395 | load_class (TYPE_NAME (class_type), 0); | |
4396 | return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
4397 | } | |
bc3ca41b PB |
4398 | } |
4399 | } | |
e04a16fb AG |
4400 | TYPE_NAME (class_type) = original_name; |
4401 | ||
4402 | /* 3- Check an other compilation unit that bears the name of type */ | |
4403 | load_class (TYPE_NAME (class_type), 0); | |
4404 | if (check_pkg_class_access (TYPE_NAME (class_type), | |
4405 | (cl ? cl : lookup_cl (decl)))) | |
4406 | return NULL_TREE; | |
4407 | ||
4408 | if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)))) | |
4409 | return new_class_decl; | |
4410 | ||
4411 | /* 4- Check the import on demands. Don't allow bar.baz to be | |
4412 | imported from foo.* */ | |
4413 | if (!QUALIFIED_P (TYPE_NAME (class_type))) | |
4414 | if (find_in_imports_on_demand (class_type)) | |
4415 | return NULL_TREE; | |
4416 | ||
4417 | /* 5- Last call for a resolution */ | |
4418 | return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
4419 | } | |
4420 | ||
4421 | /* Resolve NAME and lay it out (if not done and if not the current | |
23a79c61 APB |
4422 | parsed class). Return a decl node. This function is meant to be |
4423 | called when type resolution is necessary during the walk pass. */ | |
e04a16fb AG |
4424 | |
4425 | static tree | |
c877974e APB |
4426 | resolve_and_layout (something, cl) |
4427 | tree something; | |
e04a16fb AG |
4428 | tree cl; |
4429 | { | |
c877974e APB |
4430 | tree decl; |
4431 | ||
23a79c61 APB |
4432 | /* Don't do that on the current class */ |
4433 | if (something == current_class) | |
4434 | return TYPE_NAME (current_class); | |
c877974e | 4435 | |
23a79c61 | 4436 | /* Don't do anything for void and other primitive types */ |
c877974e APB |
4437 | if (JPRIMITIVE_TYPE_P (something) || something == void_type_node) |
4438 | return NULL_TREE; | |
4439 | ||
23a79c61 APB |
4440 | /* Pointer types can be reall pointer types or fake pointers. When |
4441 | finding a real pointer, recheck for primitive types */ | |
4442 | if (TREE_CODE (something) == POINTER_TYPE) | |
4443 | { | |
4444 | if (TREE_TYPE (something)) | |
4445 | { | |
4446 | something = TREE_TYPE (something); | |
4447 | if (JPRIMITIVE_TYPE_P (something) || something == void_type_node) | |
4448 | return NULL_TREE; | |
4449 | } | |
4450 | else | |
4451 | something = TYPE_NAME (something); | |
4452 | } | |
4453 | ||
4454 | /* Don't do anything for arrays of primitive types */ | |
4455 | if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something) | |
4456 | && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something))) | |
4457 | return NULL_TREE; | |
4458 | ||
4459 | /* If something is not and IDENTIFIER_NODE, it can be a a TYPE_DECL | |
4460 | or a real TYPE */ | |
c877974e APB |
4461 | if (TREE_CODE (something) != IDENTIFIER_NODE) |
4462 | something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ? | |
4463 | DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something)); | |
4464 | ||
23a79c61 APB |
4465 | if (!(decl = resolve_no_layout (something, cl))) |
4466 | return NULL_TREE; | |
4467 | ||
4468 | /* Resolve and layout if necessary */ | |
4469 | layout_class_methods (TREE_TYPE (decl)); | |
7705e9db APB |
4470 | /* Check methods, but only once */ |
4471 | if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) | |
4472 | && !CLASS_LOADED_P (TREE_TYPE (decl))) | |
23a79c61 APB |
4473 | CHECK_METHODS (decl); |
4474 | if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl))) | |
e04a16fb | 4475 | safe_layout_class (TREE_TYPE (decl)); |
23a79c61 | 4476 | |
e04a16fb AG |
4477 | return decl; |
4478 | } | |
4479 | ||
4480 | /* Resolve a class, returns its decl but doesn't perform any | |
4481 | layout. The current parsing context is saved and restored */ | |
4482 | ||
4483 | static tree | |
4484 | resolve_no_layout (name, cl) | |
4485 | tree name, cl; | |
4486 | { | |
4487 | tree ptr, decl; | |
4488 | BUILD_PTR_FROM_NAME (ptr, name); | |
4489 | java_parser_context_save_global (); | |
4490 | decl = resolve_class (ptr, NULL_TREE, cl); | |
4491 | java_parser_context_restore_global (); | |
4492 | ||
4493 | return decl; | |
4494 | } | |
4495 | ||
23a79c61 APB |
4496 | /* Called when reporting errors. Skip leader '[' in a complex array |
4497 | type description that failed to be resolved. */ | |
e04a16fb | 4498 | |
49f48c71 | 4499 | static const char * |
e04a16fb | 4500 | purify_type_name (name) |
49f48c71 | 4501 | const char *name; |
e04a16fb AG |
4502 | { |
4503 | while (*name && *name == '[') | |
4504 | name++; | |
4505 | return name; | |
4506 | } | |
4507 | ||
4508 | /* The type CURRENT refers to can't be found. We print error messages. */ | |
4509 | ||
4510 | static void | |
4511 | complete_class_report_errors (dep) | |
4512 | jdep *dep; | |
4513 | { | |
49f48c71 | 4514 | const char *name; |
23a79c61 APB |
4515 | |
4516 | if (!JDEP_WFL (dep)) | |
4517 | return; | |
4518 | ||
4519 | name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))); | |
e04a16fb AG |
4520 | switch (JDEP_KIND (dep)) |
4521 | { | |
4522 | case JDEP_SUPER: | |
4523 | parse_error_context | |
4524 | (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found", | |
23a79c61 | 4525 | purify_type_name (name), |
e04a16fb AG |
4526 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); |
4527 | break; | |
4528 | case JDEP_FIELD: | |
4529 | parse_error_context | |
4530 | (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'", | |
23a79c61 | 4531 | purify_type_name (name), |
e04a16fb AG |
4532 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); |
4533 | break; | |
4534 | case JDEP_METHOD: /* Covers arguments */ | |
4535 | parse_error_context | |
781b0558 | 4536 | (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'", |
23a79c61 | 4537 | purify_type_name (name), |
e04a16fb AG |
4538 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))), |
4539 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep)))); | |
4540 | break; | |
4541 | case JDEP_METHOD_RETURN: /* Covers return type */ | |
4542 | parse_error_context | |
781b0558 | 4543 | (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'", |
23a79c61 | 4544 | purify_type_name (name), |
e04a16fb AG |
4545 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep)))); |
4546 | break; | |
4547 | case JDEP_INTERFACE: | |
4548 | parse_error_context | |
4549 | (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found", | |
4550 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))), | |
4551 | (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")), | |
4552 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); | |
4553 | break; | |
4554 | case JDEP_VARIABLE: | |
4555 | parse_error_context | |
781b0558 | 4556 | (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'", |
b67d701b PB |
4557 | purify_type_name (IDENTIFIER_POINTER |
4558 | (EXPR_WFL_NODE (JDEP_WFL (dep)))), | |
e04a16fb AG |
4559 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); |
4560 | break; | |
b9f7e36c APB |
4561 | case JDEP_EXCEPTION: /* As specified by `throws' */ |
4562 | parse_error_context | |
4563 | (JDEP_WFL (dep), "Class `%s' not found in `throws'", | |
4564 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))); | |
4565 | break; | |
0a2138e2 APB |
4566 | default: |
4567 | /* Fix for -Wall. Just break doing nothing. The error will be | |
4568 | caught later */ | |
4569 | break; | |
e04a16fb AG |
4570 | } |
4571 | } | |
4572 | ||
4573 | /* Check uninitialized final. */ | |
4574 | ||
4575 | void | |
4576 | java_check_final () | |
4577 | { | |
4578 | } | |
4579 | ||
22eed1e6 APB |
4580 | /* Return a static string containing the DECL prototype string. If |
4581 | DECL is a constructor, use the class name instead of the form | |
4582 | <init> */ | |
4583 | ||
49f48c71 | 4584 | static const char * |
22eed1e6 APB |
4585 | get_printable_method_name (decl) |
4586 | tree decl; | |
4587 | { | |
49f48c71 | 4588 | const char *to_return; |
9ee9b555 | 4589 | tree name = NULL_TREE; |
22eed1e6 APB |
4590 | |
4591 | if (DECL_CONSTRUCTOR_P (decl)) | |
4592 | { | |
4593 | name = DECL_NAME (decl); | |
5e942c50 | 4594 | DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))); |
22eed1e6 APB |
4595 | } |
4596 | ||
4597 | to_return = lang_printable_name (decl, 0); | |
4598 | if (DECL_CONSTRUCTOR_P (decl)) | |
4599 | DECL_NAME (decl) = name; | |
4600 | ||
4601 | return to_return; | |
4602 | } | |
4603 | ||
5e942c50 APB |
4604 | /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method |
4605 | nevertheless needs to be verfied, 1 otherwise. */ | |
4606 | ||
4607 | static int | |
4608 | reset_method_name (method) | |
4609 | tree method; | |
4610 | { | |
7f1d4866 | 4611 | if (!IS_CLINIT (method) && DECL_NAME (method) != finit_identifier_node) |
5e942c50 APB |
4612 | { |
4613 | /* NAME is just the plain name when Object is being defined */ | |
4614 | if (DECL_CONTEXT (method) != object_type_node) | |
c877974e APB |
4615 | DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ? |
4616 | init_identifier_node : GET_METHOD_NAME (method)); | |
5e942c50 APB |
4617 | return 0; |
4618 | } | |
4619 | else | |
4620 | return 1; | |
4621 | } | |
4622 | ||
c877974e APB |
4623 | /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */ |
4624 | ||
4625 | tree | |
4626 | java_get_real_method_name (method_decl) | |
4627 | tree method_decl; | |
4628 | { | |
4629 | tree method_name = DECL_NAME (method_decl); | |
4630 | if (DECL_CONSTRUCTOR_P (method_decl)) | |
4631 | return init_identifier_node; | |
82371d41 APB |
4632 | |
4633 | /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P | |
4634 | and still can be a constructor. FIXME */ | |
4635 | ||
23a79c61 APB |
4636 | /* Don't confuse method only bearing the name of their class as |
4637 | constructors */ | |
82371d41 APB |
4638 | else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl)) |
4639 | && ctxp | |
4640 | && ctxp->current_parsed_class_un == EXPR_WFL_NODE (method_name) | |
23a79c61 APB |
4641 | && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED |
4642 | && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node) | |
c877974e APB |
4643 | return init_identifier_node; |
4644 | else | |
4645 | return EXPR_WFL_NODE (method_name); | |
4646 | } | |
4647 | ||
22eed1e6 APB |
4648 | /* Track method being redefined inside the same class. As a side |
4649 | effect, set DECL_NAME to an IDENTIFIER (prior entering this | |
d77613be | 4650 | function it's a FWL, so we can track errors more accurately.) */ |
22eed1e6 | 4651 | |
e04a16fb AG |
4652 | static int |
4653 | check_method_redefinition (class, method) | |
4654 | tree class, method; | |
4655 | { | |
4656 | tree redef, name; | |
4657 | tree cl = DECL_NAME (method); | |
c3f2a476 | 4658 | tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method)); |
ba179f9f APB |
4659 | /* decl name of artificial <clinit> and $finit$ doesn't need to be |
4660 | fixed and checked */ | |
5e942c50 APB |
4661 | |
4662 | /* Reset the method name before running the check. If it returns 1, | |
4663 | the method doesn't need to be verified with respect to method | |
4664 | redeclaration and we return 0 */ | |
4665 | if (reset_method_name (method)) | |
e04a16fb | 4666 | return 0; |
5e942c50 APB |
4667 | |
4668 | name = DECL_NAME (method); | |
e04a16fb AG |
4669 | for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef)) |
4670 | { | |
c3f2a476 | 4671 | if (redef == method) |
e04a16fb | 4672 | break; |
c3f2a476 APB |
4673 | if (DECL_NAME (redef) == name |
4674 | && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))) | |
e04a16fb | 4675 | { |
22eed1e6 APB |
4676 | parse_error_context |
4677 | (cl, "Duplicate %s declaration `%s'", | |
4678 | (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"), | |
4679 | get_printable_method_name (redef)); | |
e04a16fb AG |
4680 | return 1; |
4681 | } | |
4682 | } | |
4683 | return 0; | |
4684 | } | |
4685 | ||
d77613be APB |
4686 | static void |
4687 | check_abstract_method_definitions (do_interface, class_decl, type) | |
4688 | int do_interface; | |
4689 | tree class_decl, type; | |
4690 | { | |
4691 | tree class = TREE_TYPE (class_decl); | |
4692 | tree method, end_type; | |
4693 | ||
4694 | end_type = (do_interface ? object_type_node : type); | |
4695 | for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method)) | |
4696 | { | |
4697 | tree other_super, other_method, method_sig, method_name; | |
4698 | int found = 0; | |
4699 | ||
4700 | if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method)) | |
4701 | continue; | |
4702 | ||
4703 | /* Now verify that somewhere in between TYPE and CLASS, | |
4704 | abstract method METHOD gets a non abstract definition | |
4705 | that is inherited by CLASS. */ | |
4706 | ||
4707 | method_sig = build_java_signature (TREE_TYPE (method)); | |
4708 | method_name = DECL_NAME (method); | |
4709 | if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION) | |
4710 | method_name = EXPR_WFL_NODE (method_name); | |
4711 | ||
4712 | for (other_super = class; other_super != end_type; | |
4713 | other_super = CLASSTYPE_SUPER (other_super)) | |
4714 | { | |
4715 | for (other_method = TYPE_METHODS (other_super); other_method; | |
4716 | other_method = TREE_CHAIN (other_method)) | |
4717 | { | |
4718 | tree s = build_java_signature (TREE_TYPE (other_method)); | |
4719 | tree other_name = DECL_NAME (other_method); | |
4720 | ||
4721 | if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION) | |
4722 | other_name = EXPR_WFL_NODE (other_name); | |
4723 | if (!IS_CLINIT (other_method) | |
4724 | && !DECL_CONSTRUCTOR_P (other_method) | |
4725 | && method_name == other_name && method_sig == s) | |
4726 | { | |
4727 | found = 1; | |
4728 | break; | |
4729 | } | |
4730 | } | |
4731 | } | |
4732 | ||
4733 | /* Report that abstract METHOD didn't find an implementation | |
4734 | that CLASS can use. */ | |
4735 | if (!found) | |
4736 | { | |
c2e3db92 | 4737 | char *t = xstrdup (lang_printable_name |
d77613be APB |
4738 | (TREE_TYPE (TREE_TYPE (method)), 0)); |
4739 | tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method))); | |
4740 | tree saved_wfl = NULL_TREE; | |
4741 | ||
4742 | if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION) | |
4743 | { | |
4744 | saved_wfl = DECL_NAME (method); | |
4745 | DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method)); | |
4746 | } | |
4747 | ||
4748 | parse_error_context | |
4749 | (lookup_cl (class_decl), | |
781b0558 | 4750 | "Class `%s' doesn't define the abstract method `%s %s' from %s `%s'. This method must be defined or %s `%s' must be declared abstract", |
d77613be APB |
4751 | IDENTIFIER_POINTER (DECL_NAME (class_decl)), |
4752 | t, lang_printable_name (method, 0), | |
4753 | (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ? | |
4754 | "interface" : "class"), | |
4755 | IDENTIFIER_POINTER (ccn), | |
4756 | (CLASS_INTERFACE (class_decl) ? "interface" : "class"), | |
4757 | IDENTIFIER_POINTER (DECL_NAME (class_decl))); | |
4758 | ||
4759 | free (t); | |
4760 | ||
4761 | if (saved_wfl) | |
4762 | DECL_NAME (method) = saved_wfl; | |
4763 | } | |
4764 | } | |
4765 | } | |
4766 | ||
4767 | /* Check that CLASS_DECL somehoow implements all inherited abstract | |
4768 | methods. */ | |
4769 | ||
4770 | static void | |
4771 | java_check_abstract_method_definitions (class_decl) | |
4772 | tree class_decl; | |
4773 | { | |
4774 | tree class = TREE_TYPE (class_decl); | |
4775 | tree super, vector; | |
4776 | int i; | |
4777 | ||
4778 | if (CLASS_ABSTRACT (class_decl)) | |
4779 | return; | |
4780 | ||
4781 | /* Check for inherited types */ | |
4782 | for (super = CLASSTYPE_SUPER (class); super != object_type_node; | |
4783 | super = CLASSTYPE_SUPER (super)) | |
4784 | { | |
4785 | if (!CLASS_ABSTRACT (TYPE_NAME (super))) | |
4786 | continue; | |
4787 | ||
4788 | check_abstract_method_definitions (0, class_decl, super); | |
4789 | } | |
4790 | ||
4791 | /* Check for implemented interfaces. */ | |
4792 | vector = TYPE_BINFO_BASETYPES (class); | |
4793 | for (i = 1; i < TREE_VEC_LENGTH (vector); i++) | |
4794 | { | |
4795 | super = BINFO_TYPE (TREE_VEC_ELT (vector, i)); | |
4796 | check_abstract_method_definitions (1, class_decl, super); | |
4797 | } | |
4798 | } | |
4799 | ||
4800 | /* Check all the methods of CLASS_DECL. Methods are first completed | |
4801 | then checked according to regular method existance rules. If no | |
4802 | constructor for CLASS_DECL were encountered, then build its | |
4803 | declaration. */ | |
e04a16fb AG |
4804 | |
4805 | static void | |
4806 | java_check_regular_methods (class_decl) | |
4807 | tree class_decl; | |
4808 | { | |
5e942c50 | 4809 | int saw_constructor = 0; |
e04a16fb AG |
4810 | tree method; |
4811 | tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl)); | |
4812 | tree super_class = CLASSTYPE_SUPER (class); | |
5e942c50 | 4813 | tree saved_found_wfl = NULL_TREE, found = NULL_TREE; |
c877974e APB |
4814 | tree mthrows; |
4815 | ||
4816 | /* It is not necessary to check methods defined in java.lang.Object */ | |
4817 | if (class == object_type_node) | |
4818 | return; | |
e04a16fb | 4819 | |
23a79c61 APB |
4820 | if (!TYPE_NVIRTUALS (class)) |
4821 | TYPE_METHODS (class) = nreverse (TYPE_METHODS (class)); | |
e04a16fb AG |
4822 | |
4823 | /* Should take interfaces into account. FIXME */ | |
4824 | for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method)) | |
4825 | { | |
5e942c50 | 4826 | tree sig; |
e04a16fb AG |
4827 | tree method_wfl = DECL_NAME (method); |
4828 | int aflags; | |
4829 | ||
5e942c50 APB |
4830 | /* If we previously found something and its name was saved, |
4831 | reinstall it now */ | |
4832 | if (found && saved_found_wfl) | |
ba179f9f APB |
4833 | { |
4834 | DECL_NAME (found) = saved_found_wfl; | |
4835 | saved_found_wfl = NULL_TREE; | |
4836 | } | |
5e942c50 | 4837 | |
e04a16fb AG |
4838 | /* Check for redefinitions */ |
4839 | if (check_method_redefinition (class, method)) | |
4840 | continue; | |
4841 | ||
22eed1e6 APB |
4842 | /* If we see one constructor a mark so we don't generate the |
4843 | default one. Also skip other verifications: constructors | |
4844 | can't be inherited hence hiden or overriden */ | |
4845 | if (DECL_CONSTRUCTOR_P (method)) | |
4846 | { | |
4847 | saw_constructor = 1; | |
4848 | continue; | |
4849 | } | |
4850 | ||
c877974e APB |
4851 | /* We verify things thrown by the method. They must inherits from |
4852 | java.lang.Throwable */ | |
4853 | for (mthrows = DECL_FUNCTION_THROWS (method); | |
4854 | mthrows; mthrows = TREE_CHAIN (mthrows)) | |
4855 | { | |
4856 | if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node)) | |
4857 | parse_error_context | |
781b0558 | 4858 | (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'", |
c877974e APB |
4859 | IDENTIFIER_POINTER |
4860 | (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows))))); | |
4861 | } | |
4862 | ||
e04a16fb | 4863 | sig = build_java_argument_signature (TREE_TYPE (method)); |
e04a16fb | 4864 | found = lookup_argument_method (super_class, DECL_NAME (method), sig); |
b9f7e36c | 4865 | |
5e942c50 | 4866 | /* Nothing overrides or it's a private method. */ |
aabd7048 | 4867 | if (!found) |
5e942c50 | 4868 | continue; |
aabd7048 PB |
4869 | if (METHOD_PRIVATE (found)) |
4870 | { | |
4871 | found = NULL_TREE; | |
4872 | continue; | |
4873 | } | |
5e942c50 APB |
4874 | |
4875 | /* If found wasn't verified, it's DECL_NAME won't be set properly. | |
4876 | We set it temporarily for the sake of the error report. */ | |
4877 | saved_found_wfl = DECL_NAME (found); | |
4878 | reset_method_name (found); | |
4879 | ||
e04a16fb AG |
4880 | /* Can't override a method with the same name and different return |
4881 | types. */ | |
4882 | if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method))) | |
b9f7e36c | 4883 | { |
c2e3db92 | 4884 | char *t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), |
0a2138e2 | 4885 | 0)); |
b9f7e36c | 4886 | parse_error_context |
7f10c2e2 | 4887 | (method_wfl, |
b9f7e36c | 4888 | "Method `%s' was defined with return type `%s' in class `%s'", |
0a2138e2 | 4889 | lang_printable_name (found, 0), t, |
b9f7e36c APB |
4890 | IDENTIFIER_POINTER |
4891 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
4892 | free (t); | |
4893 | } | |
e04a16fb | 4894 | |
7f10c2e2 APB |
4895 | aflags = get_access_flags_from_decl (found); |
4896 | /* If the method has default, access in an other package, then | |
4897 | issue a warning that the current method doesn't override the | |
4898 | one that was found elsewhere. Do not issue this warning when | |
4899 | the match was found in java.lang.Object. */ | |
4900 | if (DECL_CONTEXT (found) != object_type_node | |
4901 | && ((aflags & 0x7) == 0) | |
4902 | && !class_in_current_package (DECL_CONTEXT (found)) | |
1fb89a4d | 4903 | && DECL_NAME (found) != clinit_identifier_node |
7f10c2e2 APB |
4904 | && flag_not_overriding) |
4905 | { | |
4906 | parse_warning_context | |
781b0558 | 4907 | (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package", |
7f10c2e2 APB |
4908 | lang_printable_name (found, 0), |
4909 | IDENTIFIER_POINTER (DECL_NAME (class_decl)), | |
4910 | IDENTIFIER_POINTER (DECL_NAME | |
4911 | (TYPE_NAME (DECL_CONTEXT (found))))); | |
4912 | continue; | |
4913 | } | |
4914 | ||
e04a16fb AG |
4915 | /* Can't override final. Can't override static. */ |
4916 | if (METHOD_FINAL (found) || METHOD_STATIC (found)) | |
4917 | { | |
4918 | /* Static *can* override static */ | |
4919 | if (METHOD_STATIC (found) && METHOD_STATIC (method)) | |
4920 | continue; | |
4921 | parse_error_context | |
4922 | (method_wfl, | |
4923 | "%s methods can't be overriden. Method `%s' is %s in class `%s'", | |
4924 | (METHOD_FINAL (found) ? "Final" : "Static"), | |
0a2138e2 | 4925 | lang_printable_name (found, 0), |
e04a16fb AG |
4926 | (METHOD_FINAL (found) ? "final" : "static"), |
4927 | IDENTIFIER_POINTER | |
4928 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
4929 | continue; | |
4930 | } | |
7f10c2e2 | 4931 | |
e04a16fb AG |
4932 | /* Static method can't override instance method. */ |
4933 | if (METHOD_STATIC (method)) | |
4934 | { | |
4935 | parse_error_context | |
4936 | (method_wfl, | |
781b0558 | 4937 | "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'", |
0a2138e2 | 4938 | lang_printable_name (found, 0), |
e04a16fb AG |
4939 | IDENTIFIER_POINTER |
4940 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
4941 | continue; | |
4942 | } | |
5e942c50 | 4943 | |
5e942c50 APB |
4944 | /* - Overriding/hiding public must be public |
4945 | - Overriding/hiding protected must be protected or public | |
4946 | - If the overriden or hidden method has default (package) | |
4947 | access, then the overriding or hiding method must not be | |
4948 | private; otherwise, a compile-time error occurs */ | |
4949 | if ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method)) | |
4950 | || (METHOD_PROTECTED (found) | |
4951 | && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method))) | |
4952 | || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC)) | |
4953 | && METHOD_PRIVATE (method))) | |
e04a16fb AG |
4954 | { |
4955 | parse_error_context | |
4956 | (method_wfl, | |
781b0558 | 4957 | "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0), |
5e942c50 APB |
4958 | (METHOD_PUBLIC (method) ? "public" : |
4959 | (METHOD_PRIVATE (method) ? "private" : "protected")), | |
4960 | IDENTIFIER_POINTER (DECL_NAME | |
4961 | (TYPE_NAME (DECL_CONTEXT (found))))); | |
e04a16fb AG |
4962 | continue; |
4963 | } | |
4964 | ||
b9f7e36c APB |
4965 | /* Overriding methods must have compatible `throws' clauses on checked |
4966 | exceptions, if any */ | |
4967 | check_throws_clauses (method, method_wfl, found); | |
4968 | ||
e04a16fb AG |
4969 | /* Inheriting multiple methods with the same signature. FIXME */ |
4970 | } | |
4971 | ||
5e942c50 APB |
4972 | /* Don't forget eventual pending found and saved_found_wfl. Take |
4973 | into account that we might have exited because we saw an | |
d77613be | 4974 | artificial method as the last entry. */ |
5e942c50 APB |
4975 | |
4976 | if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl) | |
4977 | DECL_NAME (found) = saved_found_wfl; | |
4978 | ||
23a79c61 APB |
4979 | if (!TYPE_NVIRTUALS (class)) |
4980 | TYPE_METHODS (class) = nreverse (TYPE_METHODS (class)); | |
e04a16fb | 4981 | |
d77613be APB |
4982 | /* Search for inherited abstract method not yet implemented in this |
4983 | class. */ | |
4984 | java_check_abstract_method_definitions (class_decl); | |
4985 | ||
22eed1e6 | 4986 | if (!saw_constructor) |
e920ebc9 | 4987 | fatal ("No constructor found"); |
e04a16fb AG |
4988 | } |
4989 | ||
b9f7e36c APB |
4990 | /* Return a non zero value if the `throws' clause of METHOD (if any) |
4991 | is incompatible with the `throws' clause of FOUND (if any). */ | |
4992 | ||
4993 | static void | |
4994 | check_throws_clauses (method, method_wfl, found) | |
4995 | tree method, method_wfl, found; | |
4996 | { | |
4997 | tree mthrows, fthrows; | |
4998 | ||
c877974e APB |
4999 | /* Can't check these things with class loaded from bytecode. FIXME */ |
5000 | if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found))) | |
5001 | return; | |
5002 | ||
b9f7e36c APB |
5003 | for (mthrows = DECL_FUNCTION_THROWS (method); |
5004 | mthrows; mthrows = TREE_CHAIN (mthrows)) | |
5005 | { | |
5006 | /* We don't verify unchecked expressions */ | |
c877974e | 5007 | if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows))) |
b9f7e36c APB |
5008 | continue; |
5009 | /* Checked expression must be compatible */ | |
5010 | for (fthrows = DECL_FUNCTION_THROWS (found); | |
5011 | fthrows; fthrows = TREE_CHAIN (fthrows)) | |
5012 | if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows))) | |
5013 | break; | |
5014 | if (!fthrows) | |
5015 | { | |
5016 | parse_error_context | |
781b0558 | 5017 | (method_wfl, "Invalid checked exception class `%s' in `throws' clause. The exception must be a subclass of an exception thrown by `%s' from class `%s'", |
b9f7e36c | 5018 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))), |
0a2138e2 | 5019 | lang_printable_name (found, 0), |
b9f7e36c APB |
5020 | IDENTIFIER_POINTER |
5021 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
5022 | } | |
5023 | } | |
5024 | } | |
5025 | ||
e04a16fb AG |
5026 | /* Check abstract method of interface INTERFACE */ |
5027 | ||
5028 | static void | |
5e942c50 APB |
5029 | java_check_abstract_methods (interface_decl) |
5030 | tree interface_decl; | |
e04a16fb AG |
5031 | { |
5032 | int i, n; | |
5033 | tree method, basetype_vec, found; | |
5e942c50 | 5034 | tree interface = TREE_TYPE (interface_decl); |
e04a16fb AG |
5035 | |
5036 | for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method)) | |
5037 | { | |
b9f7e36c | 5038 | tree method_wfl = DECL_NAME (method); |
e04a16fb AG |
5039 | |
5040 | /* 2- Check for double definition inside the defining interface */ | |
5041 | if (check_method_redefinition (interface, method)) | |
5042 | continue; | |
5043 | ||
5044 | /* 3- Overriding is OK as far as we preserve the return type and | |
b9f7e36c | 5045 | the thrown exceptions (FIXME) */ |
e04a16fb AG |
5046 | found = lookup_java_interface_method2 (interface, method); |
5047 | if (found) | |
5048 | { | |
5e942c50 APB |
5049 | char *t; |
5050 | tree saved_found_wfl = DECL_NAME (found); | |
5051 | reset_method_name (found); | |
c2e3db92 | 5052 | t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0)); |
e04a16fb | 5053 | parse_error_context |
b9f7e36c | 5054 | (method_wfl, |
5e942c50 | 5055 | "Method `%s' was defined with return type `%s' in class `%s'", |
0a2138e2 | 5056 | lang_printable_name (found, 0), t, |
b9f7e36c APB |
5057 | IDENTIFIER_POINTER |
5058 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
5059 | free (t); | |
5e942c50 | 5060 | DECL_NAME (found) = saved_found_wfl; |
c63b98cd | 5061 | continue; |
e04a16fb AG |
5062 | } |
5063 | } | |
5064 | ||
5065 | /* 4- Inherited methods can't differ by their returned types */ | |
5066 | if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface))) | |
5067 | return; | |
5068 | n = TREE_VEC_LENGTH (basetype_vec); | |
5069 | for (i = 0; i < n; i++) | |
5070 | { | |
5071 | tree sub_interface_method, sub_interface; | |
5072 | tree vec_elt = TREE_VEC_ELT (basetype_vec, i); | |
5073 | if (!vec_elt) | |
5074 | continue; | |
5075 | sub_interface = BINFO_TYPE (vec_elt); | |
5076 | for (sub_interface_method = TYPE_METHODS (sub_interface); | |
5077 | sub_interface_method; | |
5078 | sub_interface_method = TREE_CHAIN (sub_interface_method)) | |
5079 | { | |
5080 | found = lookup_java_interface_method2 (interface, | |
5081 | sub_interface_method); | |
5082 | if (found && (found != sub_interface_method)) | |
5e942c50 APB |
5083 | { |
5084 | tree saved_found_wfl = DECL_NAME (found); | |
5085 | reset_method_name (found); | |
5086 | parse_error_context | |
5087 | (lookup_cl (sub_interface_method), | |
781b0558 | 5088 | "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'", |
5e942c50 APB |
5089 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))), |
5090 | lang_printable_name (found, 0), | |
5091 | IDENTIFIER_POINTER | |
5092 | (DECL_NAME (TYPE_NAME | |
5093 | (DECL_CONTEXT (sub_interface_method)))), | |
5094 | IDENTIFIER_POINTER | |
5095 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
5096 | DECL_NAME (found) = saved_found_wfl; | |
5097 | } | |
e04a16fb AG |
5098 | } |
5099 | } | |
5100 | } | |
5101 | ||
e04a16fb AG |
5102 | /* Lookup methods in interfaces using their name and partial |
5103 | signature. Return a matching method only if their types differ. */ | |
5104 | ||
5105 | static tree | |
5106 | lookup_java_interface_method2 (class, method_decl) | |
5107 | tree class, method_decl; | |
5108 | { | |
5109 | int i, n; | |
5110 | tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return; | |
5111 | ||
5112 | if (!basetype_vec) | |
5113 | return NULL_TREE; | |
5114 | ||
5115 | n = TREE_VEC_LENGTH (basetype_vec); | |
5116 | for (i = 0; i < n; i++) | |
5117 | { | |
5118 | tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return; | |
5119 | if ((BINFO_TYPE (vec_elt) != object_type_node) | |
5120 | && (to_return = | |
5121 | lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1))) | |
5122 | return to_return; | |
5123 | } | |
5124 | for (i = 0; i < n; i++) | |
5125 | { | |
5126 | to_return = lookup_java_interface_method2 | |
5127 | (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl); | |
5128 | if (to_return) | |
5129 | return to_return; | |
5130 | } | |
5131 | ||
5132 | return NULL_TREE; | |
5133 | } | |
5134 | ||
5135 | /* Lookup method using their name and partial signature. Return a | |
5136 | matching method only if their types differ. */ | |
5137 | ||
5138 | static tree | |
5139 | lookup_java_method2 (clas, method_decl, do_interface) | |
5140 | tree clas, method_decl; | |
5141 | int do_interface; | |
5142 | { | |
5e942c50 APB |
5143 | tree method, method_signature, method_name, method_type, name; |
5144 | ||
e04a16fb | 5145 | method_signature = build_java_argument_signature (TREE_TYPE (method_decl)); |
5e942c50 APB |
5146 | name = DECL_NAME (method_decl); |
5147 | method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ? | |
5148 | EXPR_WFL_NODE (name) : name); | |
e04a16fb AG |
5149 | method_type = TREE_TYPE (TREE_TYPE (method_decl)); |
5150 | ||
5151 | while (clas != NULL_TREE) | |
5152 | { | |
5153 | for (method = TYPE_METHODS (clas); | |
5154 | method != NULL_TREE; method = TREE_CHAIN (method)) | |
5155 | { | |
5156 | tree method_sig = build_java_argument_signature (TREE_TYPE (method)); | |
5e942c50 APB |
5157 | tree name = DECL_NAME (method); |
5158 | if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ? | |
5159 | EXPR_WFL_NODE (name) : name) == method_name | |
e04a16fb AG |
5160 | && method_sig == method_signature |
5161 | && TREE_TYPE (TREE_TYPE (method)) != method_type) | |
5e942c50 | 5162 | return method; |
e04a16fb AG |
5163 | } |
5164 | clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas)); | |
5165 | } | |
5166 | return NULL_TREE; | |
5167 | } | |
5168 | ||
f441f671 APB |
5169 | /* Return the line that matches DECL line number, and try its best to |
5170 | position the column number. Used during error reports. */ | |
e04a16fb AG |
5171 | |
5172 | static tree | |
5173 | lookup_cl (decl) | |
5174 | tree decl; | |
5175 | { | |
5176 | static tree cl = NULL_TREE; | |
f441f671 | 5177 | char *line, *found; |
e04a16fb AG |
5178 | |
5179 | if (!decl) | |
5180 | return NULL_TREE; | |
5181 | ||
5182 | if (cl == NULL_TREE) | |
5183 | cl = build_expr_wfl (NULL_TREE, NULL, 0, 0); | |
5184 | ||
5185 | EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl)); | |
5186 | EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1); | |
5187 | ||
f441f671 APB |
5188 | line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)), |
5189 | EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl)); | |
5190 | ||
5191 | found = strstr ((const char *)line, | |
5192 | (const char *)IDENTIFIER_POINTER (DECL_NAME (decl))); | |
5193 | if (found) | |
5194 | EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line); | |
5195 | ||
e04a16fb AG |
5196 | return cl; |
5197 | } | |
5198 | ||
5199 | /* Look for a simple name in the single-type import list */ | |
5200 | ||
5201 | static tree | |
5202 | find_name_in_single_imports (name) | |
5203 | tree name; | |
5204 | { | |
5205 | tree node; | |
5206 | ||
5207 | for (node = ctxp->import_list; node; node = TREE_CHAIN (node)) | |
5208 | if (TREE_VALUE (node) == name) | |
5209 | return (EXPR_WFL_NODE (TREE_PURPOSE (node))); | |
5210 | ||
5211 | return NULL_TREE; | |
5212 | } | |
5213 | ||
5214 | /* Process all single-type import. */ | |
5215 | ||
5216 | static int | |
5217 | process_imports () | |
5218 | { | |
5219 | tree import; | |
5220 | int error_found; | |
5221 | ||
5222 | for (import = ctxp->import_list; import; import = TREE_CHAIN (import)) | |
5223 | { | |
5224 | tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import)); | |
5225 | ||
5226 | /* Don't load twice something already defined. */ | |
5227 | if (IDENTIFIER_CLASS_VALUE (to_be_found)) | |
5228 | continue; | |
5229 | QUALIFIED_P (to_be_found) = 1; | |
5230 | load_class (to_be_found, 0); | |
5231 | error_found = | |
5232 | check_pkg_class_access (to_be_found, TREE_PURPOSE (import)); | |
5233 | if (!IDENTIFIER_CLASS_VALUE (to_be_found)) | |
5234 | { | |
5235 | parse_error_context (TREE_PURPOSE (import), | |
5236 | "Class or interface `%s' not found in import", | |
5237 | IDENTIFIER_POINTER (to_be_found)); | |
5238 | return 1; | |
5239 | } | |
5240 | if (error_found) | |
5241 | return 1; | |
5242 | } | |
5243 | return 0; | |
5244 | } | |
5245 | ||
5246 | /* Possibly find a class imported by a single-type import statement. Return | |
5247 | 1 if an error occured, 0 otherwise. */ | |
5248 | ||
5249 | static int | |
5250 | find_in_imports (class_type) | |
5251 | tree class_type; | |
5252 | { | |
5253 | tree import; | |
5254 | ||
5255 | for (import = ctxp->import_list; import; import = TREE_CHAIN (import)) | |
5256 | if (TREE_VALUE (import) == TYPE_NAME (class_type)) | |
5257 | { | |
5258 | TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import)); | |
5259 | QUALIFIED_P (TYPE_NAME (class_type)) = 1; | |
e04a16fb AG |
5260 | } |
5261 | return 0; | |
5262 | } | |
5263 | ||
e04a16fb | 5264 | static int |
63a212ed | 5265 | note_possible_classname (name, len) |
49f48c71 | 5266 | const char *name; |
63a212ed | 5267 | int len; |
e04a16fb | 5268 | { |
63a212ed PB |
5269 | tree node; |
5270 | if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0) | |
5271 | len = len - 5; | |
5272 | else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0) | |
5273 | len = len - 6; | |
e04a16fb | 5274 | else |
63a212ed PB |
5275 | return 0; |
5276 | node = ident_subst (name, len, "", '/', '.', ""); | |
5277 | IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */ | |
fe0e4d76 | 5278 | QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0; |
63a212ed | 5279 | return 1; |
e04a16fb AG |
5280 | } |
5281 | ||
5282 | /* Read a import directory, gathering potential match for further type | |
5283 | references. Indifferently reads a filesystem or a ZIP archive | |
5284 | directory. */ | |
5285 | ||
5286 | static void | |
5287 | read_import_dir (wfl) | |
5288 | tree wfl; | |
5289 | { | |
63a212ed | 5290 | tree package_id = EXPR_WFL_NODE (wfl); |
49f48c71 | 5291 | const char *package_name = IDENTIFIER_POINTER (package_id); |
63a212ed | 5292 | int package_length = IDENTIFIER_LENGTH (package_id); |
e04a16fb | 5293 | DIR *dirp = NULL; |
d8fccff5 | 5294 | JCF *saved_jcf = current_jcf; |
e04a16fb | 5295 | |
63a212ed PB |
5296 | int found = 0; |
5297 | int k; | |
5298 | void *entry; | |
5299 | struct buffer filename[1]; | |
5300 | ||
5301 | ||
5302 | if (IS_AN_IMPORT_ON_DEMAND_P (package_id)) | |
5303 | return; | |
5304 | IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1; | |
5305 | ||
5306 | BUFFER_INIT (filename); | |
5307 | buffer_grow (filename, package_length + 100); | |
5308 | ||
5309 | for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry)) | |
5310 | { | |
49f48c71 | 5311 | const char *entry_name = jcf_path_name (entry); |
63a212ed PB |
5312 | int entry_length = strlen (entry_name); |
5313 | if (jcf_path_is_zipfile (entry)) | |
5314 | { | |
5315 | ZipFile *zipf; | |
5316 | buffer_grow (filename, entry_length); | |
5317 | memcpy (filename->data, entry_name, entry_length - 1); | |
5318 | filename->data[entry_length-1] = '\0'; | |
5319 | zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry)); | |
5320 | if (zipf == NULL) | |
5321 | error ("malformed .zip archive in CLASSPATH: %s", entry_name); | |
5322 | else | |
5323 | { | |
5324 | ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory; | |
5325 | BUFFER_RESET (filename); | |
5326 | for (k = 0; k < package_length; k++) | |
5327 | { | |
5328 | char ch = package_name[k]; | |
5329 | *filename->ptr++ = ch == '.' ? '/' : ch; | |
5330 | } | |
5331 | *filename->ptr++ = '/'; | |
5332 | ||
345137c7 | 5333 | for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd)) |
63a212ed | 5334 | { |
49f48c71 | 5335 | const char *current_entry = ZIPDIR_FILENAME (zipd); |
63a212ed PB |
5336 | int current_entry_len = zipd->filename_length; |
5337 | ||
345137c7 TT |
5338 | if (current_entry_len >= BUFFER_LENGTH (filename) |
5339 | && strncmp (filename->data, current_entry, | |
5340 | BUFFER_LENGTH (filename)) != 0) | |
63a212ed | 5341 | continue; |
345137c7 | 5342 | found |= note_possible_classname (current_entry, |
63a212ed PB |
5343 | current_entry_len); |
5344 | } | |
5345 | } | |
5346 | } | |
5347 | else | |
5348 | { | |
5349 | BUFFER_RESET (filename); | |
5350 | buffer_grow (filename, entry_length + package_length + 4); | |
5351 | strcpy (filename->data, entry_name); | |
5352 | filename->ptr = filename->data + entry_length; | |
5353 | for (k = 0; k < package_length; k++) | |
5354 | { | |
5355 | char ch = package_name[k]; | |
5356 | *filename->ptr++ = ch == '.' ? '/' : ch; | |
5357 | } | |
5358 | *filename->ptr = '\0'; | |
5359 | ||
5360 | dirp = opendir (filename->data); | |
5361 | if (dirp == NULL) | |
5362 | continue; | |
5363 | *filename->ptr++ = '/'; | |
5364 | for (;;) | |
5365 | { | |
63a212ed | 5366 | int len; |
49f48c71 | 5367 | const char *d_name; |
63a212ed PB |
5368 | struct dirent *direntp = readdir (dirp); |
5369 | if (!direntp) | |
5370 | break; | |
5371 | d_name = direntp->d_name; | |
5372 | len = strlen (direntp->d_name); | |
5373 | buffer_grow (filename, len+1); | |
5374 | strcpy (filename->ptr, d_name); | |
345137c7 | 5375 | found |= note_possible_classname (filename->data + entry_length, |
63a212ed PB |
5376 | package_length+len+1); |
5377 | } | |
5378 | if (dirp) | |
5379 | closedir (dirp); | |
5380 | } | |
5381 | } | |
e04a16fb | 5382 | |
63a212ed | 5383 | free (filename->data); |
e04a16fb | 5384 | |
63a212ed PB |
5385 | /* Here we should have a unified way of retrieving an entry, to be |
5386 | indexed. */ | |
5387 | if (!found) | |
e04a16fb AG |
5388 | { |
5389 | static int first = 1; | |
5390 | if (first) | |
5391 | { | |
781b0558 | 5392 | error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name); |
e04a16fb AG |
5393 | java_error_count++; |
5394 | first = 0; | |
5395 | } | |
5396 | else | |
63a212ed PB |
5397 | parse_error_context (wfl, "Package `%s' not found in import", |
5398 | package_name); | |
e04a16fb AG |
5399 | current_jcf = saved_jcf; |
5400 | return; | |
5401 | } | |
e04a16fb AG |
5402 | current_jcf = saved_jcf; |
5403 | } | |
5404 | ||
5405 | /* Possibly find a type in the import on demands specified | |
5406 | types. Returns 1 if an error occured, 0 otherwise. Run throught the | |
5407 | entire list, to detected potential double definitions. */ | |
5408 | ||
5409 | static int | |
5410 | find_in_imports_on_demand (class_type) | |
5411 | tree class_type; | |
5412 | { | |
ab3a6dd6 | 5413 | tree node, import, node_to_use = NULL_TREE; |
e04a16fb | 5414 | int seen_once = -1; |
ab3a6dd6 | 5415 | tree cl = NULL_TREE; |
e04a16fb AG |
5416 | |
5417 | for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import)) | |
5418 | { | |
49f48c71 | 5419 | const char *id_name; |
e04a16fb AG |
5420 | obstack_grow (&temporary_obstack, |
5421 | IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))), | |
5422 | IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import)))); | |
63a212ed | 5423 | obstack_1grow (&temporary_obstack, '.'); |
e04a16fb AG |
5424 | obstack_grow0 (&temporary_obstack, |
5425 | IDENTIFIER_POINTER (TYPE_NAME (class_type)), | |
5426 | IDENTIFIER_LENGTH (TYPE_NAME (class_type))); | |
5427 | id_name = obstack_finish (&temporary_obstack); | |
5428 | ||
5429 | node = maybe_get_identifier (id_name); | |
5430 | if (node && IS_A_CLASSFILE_NAME (node)) | |
5431 | { | |
5432 | if (seen_once < 0) | |
5433 | { | |
5434 | cl = TREE_PURPOSE (import); | |
5435 | seen_once = 1; | |
5436 | node_to_use = node; | |
5437 | } | |
5438 | else | |
5439 | { | |
5440 | seen_once++; | |
5441 | parse_error_context | |
5442 | (import, "Type `%s' also potentially defined in package `%s'", | |
5443 | IDENTIFIER_POINTER (TYPE_NAME (class_type)), | |
5444 | IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import)))); | |
5445 | } | |
5446 | } | |
5447 | } | |
5448 | ||
5449 | if (seen_once == 1) | |
5450 | { | |
5451 | /* Setup lineno so that it refers to the line of the import (in | |
5452 | case we parse a class file and encounter errors */ | |
5453 | tree decl; | |
5454 | int saved_lineno = lineno; | |
5455 | lineno = EXPR_WFL_LINENO (cl); | |
63a212ed | 5456 | TYPE_NAME (class_type) = node_to_use; |
e04a16fb AG |
5457 | QUALIFIED_P (TYPE_NAME (class_type)) = 1; |
5458 | decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
5459 | /* If there is no DECL set for the class or if the class isn't | |
5460 | loaded and not seen in source yet, the load */ | |
5461 | if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl)) | |
5462 | && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl)))) | |
5463 | load_class (node_to_use, 0); | |
5464 | lineno = saved_lineno; | |
5465 | return check_pkg_class_access (TYPE_NAME (class_type), cl); | |
5466 | } | |
5467 | else | |
5468 | return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */ | |
5469 | } | |
5470 | ||
5e942c50 APB |
5471 | static tree |
5472 | resolve_package (pkg, next) | |
5473 | tree pkg, *next; | |
5474 | { | |
2c56429a | 5475 | tree current; |
5e942c50 | 5476 | tree type_name = NULL_TREE; |
49f48c71 | 5477 | const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg)); |
5e942c50 APB |
5478 | |
5479 | /* The trick is to determine when the package name stops and were | |
5480 | the name of something contained in the package starts. Then we | |
5481 | return a fully qualified name of what we want to get. */ | |
5482 | ||
5483 | /* Do a quick search on well known package names */ | |
5484 | if (!strncmp (name, "java.lang.reflect", 17)) | |
5485 | { | |
5486 | *next = | |
5487 | TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)))); | |
5488 | type_name = lookup_package_type (name, 17); | |
5489 | } | |
5490 | else if (!strncmp (name, "java.lang", 9)) | |
5491 | { | |
5492 | *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))); | |
5493 | type_name = lookup_package_type (name, 9); | |
5494 | } | |
5e942c50 | 5495 | |
2c56429a APB |
5496 | /* If we found something here, return */ |
5497 | if (type_name) | |
5498 | return type_name; | |
5499 | ||
5500 | *next = EXPR_WFL_QUALIFICATION (pkg); | |
5501 | ||
5502 | /* Try the current package. */ | |
5503 | if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package), | |
5504 | IDENTIFIER_LENGTH (ctxp->package))) | |
5505 | { | |
5506 | type_name = | |
5507 | lookup_package_type_and_set_next (name, | |
5508 | IDENTIFIER_LENGTH (ctxp->package), | |
5509 | next ); | |
5510 | if (type_name) | |
5511 | return type_name; | |
5512 | } | |
5513 | ||
5514 | /* Search in imported package */ | |
5515 | for (current = ctxp->import_list; current; current = TREE_CHAIN (current)) | |
5516 | { | |
5517 | tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current)); | |
5518 | int len = IDENTIFIER_LENGTH (current_pkg_name); | |
5519 | if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len)) | |
5520 | { | |
5521 | tree left, dummy; | |
5522 | ||
5523 | breakdown_qualified (&left, &dummy, current_pkg_name); | |
5524 | len = IDENTIFIER_LENGTH (left); | |
5525 | type_name = lookup_package_type_and_set_next (name, len, next); | |
5526 | if (type_name) | |
5527 | break; | |
5528 | } | |
5529 | } | |
5530 | ||
5531 | return type_name; | |
5532 | } | |
5533 | ||
5534 | static tree | |
5535 | lookup_package_type_and_set_next (name, len, next) | |
49f48c71 | 5536 | const char *name; |
2c56429a APB |
5537 | int len; |
5538 | tree *next; | |
5539 | { | |
49f48c71 | 5540 | const char *ptr; |
2c56429a APB |
5541 | tree type_name = lookup_package_type (name, len); |
5542 | ||
5543 | if (!type_name) | |
5544 | return NULL; | |
5545 | ||
5546 | ptr = IDENTIFIER_POINTER (type_name); | |
5547 | while (ptr && (ptr = strchr (ptr, '.'))) | |
5548 | { | |
5549 | *next = TREE_CHAIN (*next); | |
5550 | ptr++; | |
5551 | } | |
5e942c50 APB |
5552 | return type_name; |
5553 | } | |
5554 | ||
5555 | static tree | |
5556 | lookup_package_type (name, from) | |
49f48c71 | 5557 | const char *name; |
5e942c50 APB |
5558 | int from; |
5559 | { | |
5560 | char subname [128]; | |
49f48c71 | 5561 | const char *sub = &name[from+1]; |
5e942c50 APB |
5562 | while (*sub != '.' && *sub) |
5563 | sub++; | |
5564 | strncpy (subname, name, sub-name); | |
5565 | subname [sub-name] = '\0'; | |
5566 | return get_identifier (subname); | |
5567 | } | |
5568 | ||
e04a16fb AG |
5569 | /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no |
5570 | access violations were found, 1 otherwise. */ | |
5571 | ||
5572 | static int | |
5573 | check_pkg_class_access (class_name, cl) | |
5574 | tree class_name; | |
5575 | tree cl; | |
5576 | { | |
5577 | tree type; | |
e04a16fb AG |
5578 | |
5579 | if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name)) | |
5580 | return 0; | |
5581 | ||
5582 | if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name)))) | |
5583 | return 0; | |
5584 | ||
5585 | if (!CLASS_PUBLIC (TYPE_NAME (type))) | |
5586 | { | |
e28cd97b APB |
5587 | /* Access to a private class within the same package is |
5588 | allowed. */ | |
5589 | tree l, r; | |
5590 | breakdown_qualified (&l, &r, class_name); | |
5591 | if (l == ctxp->package) | |
5592 | return 0; | |
5593 | ||
e04a16fb | 5594 | parse_error_context |
781b0558 | 5595 | (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed", |
e04a16fb AG |
5596 | (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"), |
5597 | IDENTIFIER_POINTER (class_name)); | |
5598 | return 1; | |
5599 | } | |
5600 | return 0; | |
5601 | } | |
5602 | ||
5603 | /* Local variable declaration. */ | |
5604 | ||
5605 | static void | |
5606 | declare_local_variables (modifier, type, vlist) | |
5607 | int modifier; | |
5608 | tree type; | |
5609 | tree vlist; | |
5610 | { | |
c583dd46 APB |
5611 | tree decl, current, saved_type; |
5612 | tree type_wfl = NULL_TREE; | |
e04a16fb AG |
5613 | int must_chain = 0; |
5614 | ||
2aa11e97 | 5615 | /* Push a new block if statements were seen between the last time we |
e04a16fb | 5616 | pushed a block and now. Keep a cound of block to close */ |
f099f336 | 5617 | if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl))) |
e04a16fb | 5618 | { |
f099f336 | 5619 | tree body = GET_CURRENT_BLOCK (current_function_decl); |
e04a16fb | 5620 | tree b = enter_block (); |
f099f336 | 5621 | BLOCK_EXPR_ORIGIN (b) = body; |
e04a16fb AG |
5622 | } |
5623 | ||
5624 | if (modifier) | |
5625 | { | |
5626 | int i; | |
5627 | for (i = 0; i <= 10; i++) if (1 << i & modifier) break; | |
c877974e APB |
5628 | if (modifier == ACC_FINAL) |
5629 | { | |
5630 | if (flag_static_local_jdk1_1) | |
5631 | parse_warning_context (ctxp->modifier_ctx [i], | |
781b0558 | 5632 | "Unsupported JDK1.1 `final' local variable (treated as non final)"); |
c877974e APB |
5633 | } |
5634 | else | |
5635 | { | |
5636 | parse_error_context | |
5637 | (ctxp->modifier_ctx [i], | |
5638 | "Only `final' is allowed as a local variables modifier"); | |
5639 | return; | |
5640 | } | |
e04a16fb AG |
5641 | } |
5642 | ||
c583dd46 APB |
5643 | /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will |
5644 | hold the TYPE value if a new incomplete has to be created (as | |
5645 | opposed to being found already existing and reused). */ | |
5646 | SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain); | |
5647 | ||
5648 | /* If TYPE is fully resolved and we don't have a reference, make one */ | |
1886c9d8 | 5649 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
c583dd46 APB |
5650 | |
5651 | /* Go through all the declared variables */ | |
5652 | for (current = vlist, saved_type = type; current; | |
5653 | current = TREE_CHAIN (current), type = saved_type) | |
e04a16fb | 5654 | { |
c877974e | 5655 | tree other, real_type; |
e04a16fb AG |
5656 | tree wfl = TREE_PURPOSE (current); |
5657 | tree name = EXPR_WFL_NODE (wfl); | |
5658 | tree init = TREE_VALUE (current); | |
e04a16fb | 5659 | |
c583dd46 APB |
5660 | /* Process NAME, as it may specify extra dimension(s) for it */ |
5661 | type = build_array_from_name (type, type_wfl, name, &name); | |
5662 | ||
5663 | /* Variable redefinition check */ | |
5664 | if ((other = lookup_name_in_blocks (name))) | |
5665 | { | |
5666 | variable_redefinition_error (wfl, name, TREE_TYPE (other), | |
5667 | DECL_SOURCE_LINE (other)); | |
5668 | continue; | |
5669 | } | |
5670 | ||
5671 | /* Type adjustment. We may have just readjusted TYPE because | |
5672 | the variable specified more dimensions. Make sure we have | |
5673 | a reference if we can and don't have one already. */ | |
1886c9d8 | 5674 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
c877974e APB |
5675 | |
5676 | real_type = GET_REAL_TYPE (type); | |
c583dd46 APB |
5677 | /* Never layout this decl. This will be done when its scope |
5678 | will be entered */ | |
c877974e | 5679 | decl = build_decl (VAR_DECL, name, real_type); |
c583dd46 APB |
5680 | BLOCK_CHAIN_DECL (decl); |
5681 | ||
d4370213 APB |
5682 | /* If doing xreferencing, replace the line number with the WFL |
5683 | compound value */ | |
5684 | if (flag_emit_xref) | |
5685 | DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl); | |
5686 | ||
e04a16fb AG |
5687 | /* Don't try to use an INIT statement when an error was found */ |
5688 | if (init && java_error_count) | |
5689 | init = NULL_TREE; | |
c583dd46 APB |
5690 | |
5691 | /* Add the initialization function to the current function's code */ | |
5692 | if (init) | |
e04a16fb | 5693 | { |
c583dd46 APB |
5694 | /* Name might have been readjusted */ |
5695 | EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name; | |
5696 | MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1; | |
5697 | java_method_add_stmt (current_function_decl, | |
5698 | build_debugable_stmt (EXPR_WFL_LINECOL (init), | |
5699 | init)); | |
5700 | } | |
5701 | ||
5702 | /* Setup dependency the type of the decl */ | |
5703 | if (must_chain) | |
5704 | { | |
5705 | jdep *dep; | |
5706 | register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type); | |
5707 | dep = CLASSD_LAST (ctxp->classd_list); | |
5708 | JDEP_GET_PATCH (dep) = &TREE_TYPE (decl); | |
e04a16fb AG |
5709 | } |
5710 | } | |
5711 | SOURCE_FRONTEND_DEBUG (("Defined locals")); | |
5712 | } | |
5713 | ||
5714 | /* Called during parsing. Build decls from argument list. */ | |
5715 | ||
5716 | static void | |
5717 | source_start_java_method (fndecl) | |
5718 | tree fndecl; | |
5719 | { | |
5720 | tree tem; | |
5721 | tree parm_decl; | |
5722 | int i; | |
5723 | ||
79d13333 APB |
5724 | if (!fndecl) |
5725 | return; | |
5726 | ||
e04a16fb AG |
5727 | current_function_decl = fndecl; |
5728 | ||
5729 | /* New scope for the function */ | |
5730 | enter_block (); | |
5731 | for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0; | |
de4c7b02 | 5732 | tem != end_params_node; tem = TREE_CHAIN (tem), i++) |
e04a16fb AG |
5733 | { |
5734 | tree type = TREE_VALUE (tem); | |
5735 | tree name = TREE_PURPOSE (tem); | |
5736 | ||
23a79c61 APB |
5737 | /* If type is incomplete. Create an incomplete decl and ask for |
5738 | the decl to be patched later */ | |
e04a16fb AG |
5739 | if (INCOMPLETE_TYPE_P (type)) |
5740 | { | |
5741 | jdep *jdep; | |
c877974e APB |
5742 | tree real_type = GET_REAL_TYPE (type); |
5743 | parm_decl = build_decl (PARM_DECL, name, real_type); | |
23a79c61 | 5744 | type = obtain_incomplete_type (type); |
e04a16fb AG |
5745 | register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type); |
5746 | jdep = CLASSD_LAST (ctxp->classd_list); | |
5747 | JDEP_MISC (jdep) = name; | |
5748 | JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl); | |
5749 | } | |
5750 | else | |
5751 | parm_decl = build_decl (PARM_DECL, name, type); | |
5752 | ||
5753 | BLOCK_CHAIN_DECL (parm_decl); | |
5754 | } | |
5755 | tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)); | |
5756 | BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) = | |
5757 | nreverse (tem); | |
5758 | DECL_ARG_SLOT_COUNT (current_function_decl) = i; | |
5759 | } | |
5760 | ||
22eed1e6 APB |
5761 | /* Called during parsing. Creates an artificial method declaration. */ |
5762 | ||
5763 | static tree | |
5764 | create_artificial_method (class, flags, type, name, args) | |
5765 | tree class; | |
5766 | int flags; | |
5767 | tree type, name, args; | |
5768 | { | |
5769 | int saved_lineno = lineno; | |
5770 | tree mdecl; | |
5771 | ||
5772 | lineno = 0; | |
5773 | mdecl = make_node (FUNCTION_TYPE); | |
5774 | TREE_TYPE (mdecl) = type; | |
5775 | TYPE_ARG_TYPES (mdecl) = args; | |
5776 | mdecl = add_method (class, flags, name, build_java_signature (mdecl)); | |
5777 | lineno = saved_lineno; | |
5778 | DECL_ARTIFICIAL (mdecl) = 1; | |
5779 | return mdecl; | |
5780 | } | |
5781 | ||
5782 | /* Starts the body if an artifical method. */ | |
5783 | ||
5784 | static void | |
5785 | start_artificial_method_body (mdecl) | |
5786 | tree mdecl; | |
5787 | { | |
5788 | DECL_SOURCE_LINE (mdecl) = 1; | |
5789 | DECL_SOURCE_LINE_MERGE (mdecl, 1); | |
5790 | source_start_java_method (mdecl); | |
5791 | enter_block (); | |
5792 | } | |
5793 | ||
5794 | static void | |
5795 | end_artificial_method_body (mdecl) | |
5796 | tree mdecl; | |
5797 | { | |
5798 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block (); | |
5799 | exit_block (); | |
5800 | } | |
5801 | ||
e04a16fb AG |
5802 | /* Called during expansion. Push decls formerly built from argument |
5803 | list so they're usable during expansion. */ | |
5804 | ||
5805 | static void | |
5806 | expand_start_java_method (fndecl) | |
5807 | tree fndecl; | |
5808 | { | |
5809 | tree tem, *ptr; | |
e04a16fb | 5810 | |
e04a16fb AG |
5811 | current_function_decl = fndecl; |
5812 | ||
5813 | announce_function (fndecl); | |
5814 | pushlevel (1); /* Push parameters */ | |
5815 | ptr = &DECL_ARGUMENTS (fndecl); | |
5816 | tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)); | |
5817 | while (tem) | |
5818 | { | |
5819 | tree next = TREE_CHAIN (tem); | |
b67d701b | 5820 | tree type = TREE_TYPE (tem); |
e438e1b7 JJ |
5821 | if (PROMOTE_PROTOTYPES |
5822 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node) | |
b67d701b PB |
5823 | && INTEGRAL_TYPE_P (type)) |
5824 | type = integer_type_node; | |
b67d701b | 5825 | DECL_ARG_TYPE (tem) = type; |
e04a16fb AG |
5826 | layout_decl (tem, 0); |
5827 | pushdecl (tem); | |
e04a16fb AG |
5828 | *ptr = tem; |
5829 | ptr = &TREE_CHAIN (tem); | |
5830 | tem = next; | |
5831 | } | |
5832 | *ptr = NULL_TREE; | |
5833 | pushdecl_force_head (DECL_ARGUMENTS (fndecl)); | |
5834 | lineno = DECL_SOURCE_LINE_FIRST (fndecl); | |
e04a16fb AG |
5835 | } |
5836 | ||
5837 | /* Terminate a function and expand its body. */ | |
5838 | ||
5839 | static void | |
5840 | source_end_java_method () | |
5841 | { | |
5842 | tree fndecl = current_function_decl; | |
138657ec | 5843 | int flag_asynchronous_exceptions = asynchronous_exceptions; |
e04a16fb | 5844 | |
79d13333 APB |
5845 | if (!fndecl) |
5846 | return; | |
5847 | ||
e04a16fb AG |
5848 | java_parser_context_save_global (); |
5849 | lineno = ctxp->last_ccb_indent1; | |
5850 | ||
b67d701b PB |
5851 | /* Set EH language codes */ |
5852 | java_set_exception_lang_code (); | |
5853 | ||
5423609c APB |
5854 | /* Turn function bodies with only a NOP expr null, so they don't get |
5855 | generated at all and we won't get warnings when using the -W | |
5856 | -Wall flags. */ | |
5857 | if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node) | |
5858 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE; | |
5859 | ||
e04a16fb AG |
5860 | /* Generate function's code */ |
5861 | if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) | |
e8fc7396 APB |
5862 | && ! flag_emit_class_files |
5863 | && ! flag_emit_xref) | |
e04a16fb AG |
5864 | expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))); |
5865 | ||
5866 | /* pop out of its parameters */ | |
5867 | pushdecl_force_head (DECL_ARGUMENTS (fndecl)); | |
5868 | poplevel (1, 0, 1); | |
5869 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
5870 | ||
5871 | /* Generate rtl for function exit. */ | |
e8fc7396 | 5872 | if (! flag_emit_class_files && ! flag_emit_xref) |
e04a16fb AG |
5873 | { |
5874 | lineno = DECL_SOURCE_LINE_LAST (fndecl); | |
b67d701b PB |
5875 | /* Emit catch-finally clauses */ |
5876 | emit_handlers (); | |
e04a16fb AG |
5877 | expand_function_end (input_filename, lineno, 0); |
5878 | ||
138657ec AH |
5879 | /* FIXME: If the current method contains any exception handlers, |
5880 | force asynchronous_exceptions: this is necessary because signal | |
5881 | handlers in libjava may throw exceptions. This is far from being | |
5882 | a perfect solution, but it's better than doing nothing at all.*/ | |
5883 | if (catch_clauses) | |
5884 | asynchronous_exceptions = 1; | |
5885 | ||
e04a16fb AG |
5886 | /* Run the optimizers and output assembler code for this function. */ |
5887 | rest_of_compilation (fndecl); | |
5888 | } | |
5889 | ||
5890 | current_function_decl = NULL_TREE; | |
8226320b | 5891 | permanent_allocation (1); |
e04a16fb | 5892 | java_parser_context_restore_global (); |
138657ec | 5893 | asynchronous_exceptions = flag_asynchronous_exceptions; |
e04a16fb AG |
5894 | } |
5895 | ||
5896 | /* Record EXPR in the current function block. Complements compound | |
5897 | expression second operand if necessary. */ | |
5898 | ||
5899 | tree | |
5900 | java_method_add_stmt (fndecl, expr) | |
5901 | tree fndecl, expr; | |
5902 | { | |
b771925e APB |
5903 | if (!GET_CURRENT_BLOCK (fndecl)) |
5904 | return NULL_TREE; | |
f099f336 | 5905 | return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr); |
b67d701b | 5906 | } |
e04a16fb | 5907 | |
b67d701b PB |
5908 | static tree |
5909 | add_stmt_to_block (b, type, stmt) | |
5910 | tree b, type, stmt; | |
5911 | { | |
5912 | tree body = BLOCK_EXPR_BODY (b), c; | |
5913 | ||
e04a16fb AG |
5914 | if (java_error_count) |
5915 | return body; | |
b67d701b PB |
5916 | |
5917 | if ((c = add_stmt_to_compound (body, type, stmt)) == body) | |
e04a16fb AG |
5918 | return body; |
5919 | ||
b67d701b PB |
5920 | BLOCK_EXPR_BODY (b) = c; |
5921 | TREE_SIDE_EFFECTS (c) = 1; | |
5922 | return c; | |
e04a16fb AG |
5923 | } |
5924 | ||
5925 | /* Add STMT to EXISTING if possible, otherwise create a new | |
5926 | COMPOUND_EXPR and add STMT to it. */ | |
5927 | ||
5928 | static tree | |
5929 | add_stmt_to_compound (existing, type, stmt) | |
5930 | tree existing, type, stmt; | |
5931 | { | |
15fdcfe9 PB |
5932 | if (existing) |
5933 | return build (COMPOUND_EXPR, type, existing, stmt); | |
e04a16fb | 5934 | else |
15fdcfe9 | 5935 | return stmt; |
e04a16fb AG |
5936 | } |
5937 | ||
5938 | /* Hold THIS for the scope of the current public method decl. */ | |
5939 | static tree current_this; | |
5940 | ||
1886c9d8 APB |
5941 | void java_layout_seen_class_methods () |
5942 | { | |
5943 | tree previous_list = all_class_list; | |
5944 | tree end = NULL_TREE; | |
5945 | tree current; | |
5946 | ||
5947 | while (1) | |
5948 | { | |
5949 | for (current = previous_list; | |
5950 | current != end; current = TREE_CHAIN (current)) | |
5951 | layout_class_methods (TREE_TYPE (TREE_VALUE (current))); | |
5952 | ||
5953 | if (previous_list != all_class_list) | |
5954 | { | |
5955 | end = previous_list; | |
5956 | previous_list = all_class_list; | |
5957 | } | |
5958 | else | |
5959 | break; | |
5960 | } | |
5961 | } | |
5962 | ||
23a79c61 APB |
5963 | /* Layout the methods of all classes loaded in one way on an |
5964 | other. Check methods of source parsed classes. Then reorder the | |
5965 | fields and layout the classes or the type of all source parsed | |
5966 | classes */ | |
e04a16fb AG |
5967 | |
5968 | void | |
5969 | java_layout_classes () | |
5970 | { | |
5971 | tree current; | |
bc3ca41b | 5972 | int save_error_count = java_error_count; |
5e942c50 | 5973 | |
23a79c61 | 5974 | /* Layout the methods of all classes seen so far */ |
1886c9d8 | 5975 | java_layout_seen_class_methods (); |
23a79c61 APB |
5976 | java_parse_abort_on_error (); |
5977 | all_class_list = NULL_TREE; | |
5978 | ||
5979 | /* Then check the methods of all parsed classes */ | |
5980 | for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current)) | |
5981 | if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current)))) | |
5982 | CHECK_METHODS (TREE_VALUE (current)); | |
5983 | java_parse_abort_on_error (); | |
5984 | ||
5e942c50 | 5985 | for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current)) |
e04a16fb | 5986 | { |
5e942c50 | 5987 | current_class = TREE_TYPE (TREE_VALUE (current)); |
22eed1e6 | 5988 | |
c877974e APB |
5989 | /* Reverse the fields, but leave the dummy field in front. |
5990 | Fields are already ordered for Object and Class */ | |
5991 | if (TYPE_FIELDS (current_class) && current_class != object_type_node | |
5992 | && current_class != class_type_node) | |
5993 | { | |
23a79c61 APB |
5994 | /* If the dummy field is there, reverse the right fields and |
5995 | just layout the type for proper fields offset */ | |
c877974e APB |
5996 | if (!DECL_NAME (TYPE_FIELDS (current_class))) |
5997 | { | |
5998 | tree fields = TYPE_FIELDS (current_class); | |
5999 | TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields)); | |
6000 | TYPE_SIZE (current_class) = NULL_TREE; | |
6001 | layout_type (current_class); | |
6002 | } | |
23a79c61 APB |
6003 | /* We don't have a dummy field, we need to layout the class, |
6004 | after having reversed the fields */ | |
c877974e APB |
6005 | else |
6006 | { | |
6007 | TYPE_FIELDS (current_class) = | |
6008 | nreverse (TYPE_FIELDS (current_class)); | |
6009 | TYPE_SIZE (current_class) = NULL_TREE; | |
6010 | layout_class (current_class); | |
6011 | } | |
6012 | } | |
23a79c61 APB |
6013 | else |
6014 | layout_class (current_class); | |
5e942c50 | 6015 | |
c877974e APB |
6016 | /* From now on, the class is considered completely loaded */ |
6017 | CLASS_LOADED_P (current_class) = 1; | |
6018 | ||
5e942c50 APB |
6019 | /* Error reported by the caller */ |
6020 | if (java_error_count) | |
6021 | return; | |
e04a16fb | 6022 | } |
23a79c61 APB |
6023 | |
6024 | /* We might have reloaded classes durign the process of laying out | |
6025 | classes for code generation. We must layout the methods of those | |
6026 | late additions, as constructor checks might use them */ | |
1886c9d8 | 6027 | java_layout_seen_class_methods (); |
23a79c61 | 6028 | java_parse_abort_on_error (); |
e04a16fb AG |
6029 | } |
6030 | ||
6031 | /* Expand all methods in all registered classes. */ | |
6032 | ||
49f48c71 | 6033 | static void |
e04a16fb AG |
6034 | java_complete_expand_methods () |
6035 | { | |
6036 | tree current; | |
ce6e9147 APB |
6037 | |
6038 | do_not_fold = flag_emit_xref; | |
e04a16fb AG |
6039 | |
6040 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) | |
6041 | { | |
7f1d4866 | 6042 | int is_interface; |
e04a16fb | 6043 | tree class_type = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current)); |
e920ebc9 | 6044 | tree decl, prev_decl; |
e04a16fb AG |
6045 | |
6046 | current_class = TREE_TYPE (current); | |
7f1d4866 | 6047 | is_interface = CLASS_INTERFACE (TYPE_NAME (current_class)); |
e04a16fb AG |
6048 | |
6049 | /* Initialize a new constant pool */ | |
6050 | init_outgoing_cpool (); | |
6051 | ||
7525cc04 | 6052 | /* We want <clinit> (if any) to be processed first. */ |
e920ebc9 APB |
6053 | for (prev_decl = NULL_TREE, decl = TYPE_METHODS (class_type); |
6054 | decl; prev_decl= decl, decl = TREE_CHAIN (decl)) | |
6055 | if (IS_CLINIT (decl)) | |
6056 | { | |
6057 | if (!java_pre_expand_clinit (decl)) | |
6058 | { | |
6059 | if (prev_decl) | |
6060 | TREE_CHAIN (prev_decl) = TREE_CHAIN (decl); | |
6061 | else | |
6062 | TYPE_METHODS (class_type) = TREE_CHAIN (decl); | |
6063 | } | |
6064 | break; | |
6065 | } | |
cd9643f7 | 6066 | |
e920ebc9 | 6067 | /* Now go on for regular business. */ |
7f1d4866 APB |
6068 | for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl)) |
6069 | { | |
7f1d4866 APB |
6070 | current_function_decl = decl; |
6071 | /* Don't generate debug info on line zero when expanding a | |
6072 | generated constructor. */ | |
6073 | if (DECL_CONSTRUCTOR_P (decl) && !DECL_FUNCTION_BODY (decl)) | |
6074 | { | |
6075 | /* If we found errors, it's too dangerous to try to | |
6076 | generate and expand a constructor */ | |
6077 | if (!java_error_count) | |
6078 | { | |
6079 | restore_line_number_status (1); | |
6080 | java_complete_expand_method (decl); | |
6081 | restore_line_number_status (0); | |
e04a16fb | 6082 | } |
7f1d4866 APB |
6083 | } |
6084 | else if (METHOD_ABSTRACT (decl) || METHOD_NATIVE (decl)) | |
6085 | continue; | |
6086 | else | |
6087 | java_complete_expand_method (decl); | |
6088 | } | |
e04a16fb | 6089 | |
22eed1e6 APB |
6090 | /* Now verify constructor circularity (stop after the first one |
6091 | we find) */ | |
7f1d4866 | 6092 | if (!is_interface) |
22eed1e6 APB |
6093 | for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl)) |
6094 | if (DECL_CONSTRUCTOR_P (decl) && | |
6095 | verify_constructor_circularity (decl, decl)) | |
6096 | break; | |
6097 | ||
e04a16fb AG |
6098 | /* Make the class data, register it and run the rest of decl |
6099 | compilation on it */ | |
63a212ed PB |
6100 | if (!java_error_count) |
6101 | { | |
6102 | if (flag_emit_class_files) | |
6103 | write_classfile (current_class); | |
f099f336 APB |
6104 | if (flag_emit_xref) |
6105 | expand_xref (current_class); | |
aabd7048 | 6106 | else if (! flag_syntax_only) |
d593dd8c | 6107 | finish_class (); |
63a212ed | 6108 | } |
e04a16fb AG |
6109 | } |
6110 | } | |
6111 | ||
b9f7e36c APB |
6112 | /* Hold a list of catch clauses list. The first element of this list is |
6113 | the list of the catch clauses of the currently analysed try block. */ | |
6114 | static tree currently_caught_type_list; | |
6115 | ||
e920ebc9 APB |
6116 | /* Complete and expand <clinit>. Return a non zero value if <clinit> |
6117 | is worth keeping. */ | |
6118 | ||
6119 | static int | |
6120 | java_pre_expand_clinit (decl) | |
6121 | tree decl; | |
6122 | { | |
6123 | tree fbody = DECL_FUNCTION_BODY (decl); | |
e920ebc9 APB |
6124 | int to_return = 1; |
6125 | ||
6126 | if (fbody != NULL_TREE) | |
6127 | { | |
6128 | /* First check if we can ignore empty <clinit> */ | |
6129 | tree block_body = BLOCK_EXPR_BODY (fbody); | |
6130 | ||
6131 | current_this = NULL_TREE; | |
6132 | current_function_decl = decl; | |
6133 | if (block_body != NULL_TREE) | |
6134 | { | |
6135 | /* Prevent the use of `this' inside <clinit> */ | |
6136 | ctxp->explicit_constructor_p = 1; | |
6137 | block_body = java_complete_tree (block_body); | |
6138 | ctxp->explicit_constructor_p = 0; | |
6139 | ||
6140 | BLOCK_EXPR_BODY (fbody) = block_body; | |
6141 | if (block_body != NULL_TREE && TREE_CODE (block_body) == BLOCK | |
6142 | && BLOCK_EXPR_BODY (block_body) == empty_stmt_node) | |
6143 | to_return = 0; | |
6144 | } | |
6145 | } | |
6146 | return to_return; | |
6147 | } | |
6148 | ||
e04a16fb AG |
6149 | /* Complete and expand a method. */ |
6150 | ||
6151 | static void | |
6152 | java_complete_expand_method (mdecl) | |
6153 | tree mdecl; | |
6154 | { | |
22eed1e6 APB |
6155 | /* Fix constructors before expanding them */ |
6156 | if (DECL_CONSTRUCTOR_P (mdecl)) | |
6157 | fix_constructors (mdecl); | |
e04a16fb | 6158 | |
22eed1e6 | 6159 | /* Expand functions that have a body */ |
e04a16fb AG |
6160 | if (DECL_FUNCTION_BODY (mdecl)) |
6161 | { | |
9bbc7d9f PB |
6162 | tree fbody = DECL_FUNCTION_BODY (mdecl); |
6163 | tree block_body = BLOCK_EXPR_BODY (fbody); | |
cd531a2e | 6164 | tree exception_copy = NULL_TREE; |
e04a16fb | 6165 | expand_start_java_method (mdecl); |
939d7216 | 6166 | build_result_decl (mdecl); |
e04a16fb AG |
6167 | |
6168 | current_this | |
6169 | = (!METHOD_STATIC (mdecl) ? | |
6170 | BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE); | |
6171 | ||
ce6e9147 APB |
6172 | /* Purge the `throws' list of unchecked exceptions. If we're |
6173 | doing xref, save a copy of the list and re-install it | |
6174 | later. */ | |
6175 | if (flag_emit_xref) | |
6176 | exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl)); | |
6177 | ||
b9f7e36c APB |
6178 | purge_unchecked_exceptions (mdecl); |
6179 | ||
6180 | /* Install exceptions thrown with `throws' */ | |
6181 | PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl)); | |
6182 | ||
9bbc7d9f | 6183 | if (block_body != NULL_TREE) |
bc3ca41b PB |
6184 | { |
6185 | block_body = java_complete_tree (block_body); | |
ce6e9147 APB |
6186 | if (!flag_emit_xref) |
6187 | check_for_initialization (block_body); | |
f099f336 | 6188 | ctxp->explicit_constructor_p = 0; |
bc3ca41b | 6189 | } |
9bbc7d9f | 6190 | BLOCK_EXPR_BODY (fbody) = block_body; |
5e942c50 | 6191 | |
9bbc7d9f | 6192 | if ((block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body)) |
ce6e9147 APB |
6193 | && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE |
6194 | && !flag_emit_xref) | |
82371d41 | 6195 | missing_return_error (current_function_decl); |
7525cc04 | 6196 | |
939d7216 PB |
6197 | complete_start_java_method (mdecl); |
6198 | ||
e04a16fb AG |
6199 | /* Don't go any further if we've found error(s) during the |
6200 | expansion */ | |
6201 | if (!java_error_count) | |
6202 | source_end_java_method (); | |
22eed1e6 APB |
6203 | else |
6204 | { | |
6205 | pushdecl_force_head (DECL_ARGUMENTS (mdecl)); | |
6206 | poplevel (1, 0, 1); | |
6207 | } | |
b9f7e36c APB |
6208 | |
6209 | /* Pop the exceptions and sanity check */ | |
6210 | POP_EXCEPTIONS(); | |
6211 | if (currently_caught_type_list) | |
6212 | fatal ("Exception list non empty - java_complete_expand_method"); | |
ce6e9147 APB |
6213 | |
6214 | if (flag_emit_xref) | |
6215 | DECL_FUNCTION_THROWS (mdecl) = exception_copy; | |
e04a16fb AG |
6216 | } |
6217 | } | |
6218 | ||
22eed1e6 APB |
6219 | /* Craft a body for default constructor. Patch existing constructor |
6220 | bodies with call to super() and field initialization statements if | |
6221 | necessary. */ | |
6222 | ||
6223 | static void | |
6224 | fix_constructors (mdecl) | |
6225 | tree mdecl; | |
6226 | { | |
6227 | tree body = DECL_FUNCTION_BODY (mdecl); | |
22eed1e6 | 6228 | |
22eed1e6 APB |
6229 | if (!body) |
6230 | { | |
89e09b9a PB |
6231 | /* The constructor body must be crafted by hand. It's the |
6232 | constructor we defined when we realize we didn't have the | |
6233 | CLASSNAME() constructor */ | |
6234 | ||
22eed1e6 APB |
6235 | tree compound; |
6236 | ||
6237 | /* It is an error for the compiler to generate a default | |
6238 | constructor if the superclass doesn't have a constructor that | |
6239 | takes no argument */ | |
6240 | if (verify_constructor_super ()) | |
6241 | { | |
6242 | tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (current_class)); | |
49f48c71 | 6243 | const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl)); |
781b0558 KG |
6244 | parse_error_context |
6245 | (lookup_cl (TYPE_NAME (current_class)), | |
6246 | "No constructor matching `%s()' found in class `%s'", n, n); | |
22eed1e6 APB |
6247 | } |
6248 | ||
6249 | start_artificial_method_body (mdecl); | |
6250 | ||
6251 | /* We don't generate a super constructor invocation if we're | |
6252 | compiling java.lang.Object. build_super_invocation takes care | |
6253 | of that. */ | |
e920ebc9 | 6254 | compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl)); |
22eed1e6 APB |
6255 | |
6256 | end_artificial_method_body (mdecl); | |
6257 | } | |
6258 | /* Search for an explicit constructor invocation */ | |
6259 | else | |
6260 | { | |
6261 | int found = 0; | |
6262 | tree main_block = BLOCK_EXPR_BODY (body); | |
6263 | tree compound = NULL_TREE; | |
6264 | ||
6265 | while (body) | |
6266 | switch (TREE_CODE (body)) | |
6267 | { | |
6268 | case CALL_EXPR: | |
6269 | found = CALL_EXPLICIT_CONSTRUCTOR_P (body); | |
6270 | body = NULL_TREE; | |
6271 | break; | |
6272 | case COMPOUND_EXPR: | |
6273 | case EXPR_WITH_FILE_LOCATION: | |
6274 | body = TREE_OPERAND (body, 0); | |
6275 | break; | |
6276 | case BLOCK: | |
6277 | body = BLOCK_EXPR_BODY (body); | |
6278 | break; | |
6279 | default: | |
6280 | found = 0; | |
6281 | body = NULL_TREE; | |
6282 | } | |
6283 | /* The constructor is missing an invocation of super() */ | |
6284 | if (!found) | |
6285 | compound = add_stmt_to_compound (compound, NULL_TREE, | |
e920ebc9 | 6286 | build_super_invocation (mdecl)); |
22eed1e6 | 6287 | |
22eed1e6 APB |
6288 | /* Fix the constructor main block if we're adding extra stmts */ |
6289 | if (compound) | |
6290 | { | |
6291 | compound = add_stmt_to_compound (compound, NULL_TREE, | |
6292 | BLOCK_EXPR_BODY (main_block)); | |
6293 | BLOCK_EXPR_BODY (main_block) = compound; | |
6294 | } | |
6295 | } | |
6296 | } | |
6297 | ||
6298 | /* Browse constructors in the super class, searching for a constructor | |
6299 | that doesn't take any argument. Return 0 if one is found, 1 | |
6300 | otherwise. */ | |
6301 | ||
6302 | static int | |
6303 | verify_constructor_super () | |
6304 | { | |
6305 | tree class = CLASSTYPE_SUPER (current_class); | |
6306 | if (!class) | |
6307 | return 0; | |
6308 | ||
6309 | if (class) | |
6310 | { | |
6311 | tree mdecl; | |
6312 | for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl)) | |
6313 | { | |
6314 | if (DECL_CONSTRUCTOR_P (mdecl) | |
d77613be APB |
6315 | && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (mdecl))) |
6316 | == end_params_node) | |
22eed1e6 APB |
6317 | return 0; |
6318 | } | |
6319 | } | |
6320 | return 1; | |
6321 | } | |
6322 | ||
e04a16fb AG |
6323 | /* Expand finals. */ |
6324 | ||
49f48c71 | 6325 | static void |
e04a16fb AG |
6326 | java_expand_finals () |
6327 | { | |
6328 | } | |
6329 | ||
22eed1e6 | 6330 | /* Generate code for all context remembered for code generation. */ |
b351b287 APB |
6331 | |
6332 | void | |
6333 | java_expand_classes () | |
6334 | { | |
5423609c | 6335 | int save_error_count = 0; |
23a79c61 APB |
6336 | java_parse_abort_on_error (); |
6337 | if (!(ctxp = ctxp_for_generation)) | |
5e942c50 APB |
6338 | return; |
6339 | java_layout_classes (); | |
6340 | java_parse_abort_on_error (); | |
6341 | ||
b351b287 APB |
6342 | for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next) |
6343 | { | |
6344 | ctxp = ctxp_for_generation; | |
6345 | lang_init_source (2); /* Error msgs have method prototypes */ | |
6346 | java_complete_expand_methods (); /* Complete and expand method bodies */ | |
6347 | java_parse_abort_on_error (); | |
6348 | java_expand_finals (); /* Expand and check the finals */ | |
6349 | java_parse_abort_on_error (); | |
6350 | java_check_final (); /* Check unitialized final */ | |
6351 | java_parse_abort_on_error (); | |
6352 | } | |
b351b287 APB |
6353 | } |
6354 | ||
e04a16fb AG |
6355 | /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to |
6356 | a tree list node containing RIGHT. Fore coming RIGHTs will be | |
6357 | chained to this hook. LOCATION contains the location of the | |
6358 | separating `.' operator. */ | |
6359 | ||
6360 | static tree | |
6361 | make_qualified_primary (primary, right, location) | |
6362 | tree primary, right; | |
6363 | int location; | |
6364 | { | |
6365 | tree wfl; | |
6366 | ||
6367 | /* We want to process THIS . xxx symbolicaly, to keep it consistent | |
6368 | with the way we're processing SUPER. A THIS from a primary as a | |
6369 | different form than a SUPER. Turn THIS into something symbolic */ | |
b67d701b | 6370 | if (TREE_CODE (primary) == THIS_EXPR) |
e04a16fb | 6371 | { |
9ee9b555 | 6372 | wfl = build_wfl_node (this_identifier_node); |
e04a16fb AG |
6373 | EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary); |
6374 | wfl = make_qualified_name (wfl, right, location); | |
6375 | PRIMARY_P (wfl) = 1; | |
6376 | return wfl; | |
6377 | } | |
6378 | /* Other non WFL node are wrapped around a WFL */ | |
6379 | else if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION) | |
6380 | { | |
6381 | wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0); | |
6382 | EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary); | |
6383 | EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (primary, NULL_TREE); | |
6384 | } | |
6385 | else | |
6386 | { | |
6387 | wfl = primary; | |
6388 | if (!EXPR_WFL_QUALIFICATION (primary)) | |
6389 | EXPR_WFL_QUALIFICATION (primary) = | |
6390 | build_tree_list (primary, NULL_TREE); | |
6391 | } | |
6392 | ||
6393 | EXPR_WFL_LINECOL (right) = location; | |
6394 | chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE)); | |
6395 | PRIMARY_P (wfl) = 1; | |
6396 | return wfl; | |
6397 | } | |
6398 | ||
6399 | /* Simple merge of two name separated by a `.' */ | |
6400 | ||
6401 | static tree | |
6402 | merge_qualified_name (left, right) | |
6403 | tree left, right; | |
6404 | { | |
6405 | tree node; | |
6406 | obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left), | |
6407 | IDENTIFIER_LENGTH (left)); | |
6408 | obstack_1grow (&temporary_obstack, '.'); | |
6409 | obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right), | |
6410 | IDENTIFIER_LENGTH (right)); | |
6411 | node = get_identifier (obstack_base (&temporary_obstack)); | |
6412 | obstack_free (&temporary_obstack, obstack_base (&temporary_obstack)); | |
6413 | QUALIFIED_P (node) = 1; | |
6414 | return node; | |
6415 | } | |
6416 | ||
6417 | /* Merge the two parts of a qualified name into LEFT. Set the | |
6418 | location information of the resulting node to LOCATION, usually | |
6419 | inherited from the location information of the `.' operator. */ | |
6420 | ||
6421 | static tree | |
6422 | make_qualified_name (left, right, location) | |
6423 | tree left, right; | |
6424 | int location; | |
6425 | { | |
bc3ca41b PB |
6426 | #ifdef USE_COMPONENT_REF |
6427 | tree node = build (COMPONENT_REF, NULL_TREE, left, right); | |
6428 | EXPR_WFL_LINECOL (node) = location; | |
6429 | return node; | |
6430 | #else | |
e04a16fb AG |
6431 | tree left_id = EXPR_WFL_NODE (left); |
6432 | tree right_id = EXPR_WFL_NODE (right); | |
6433 | tree wfl, merge; | |
6434 | ||
6435 | merge = merge_qualified_name (left_id, right_id); | |
6436 | ||
6437 | /* Left wasn't qualified and is now qualified */ | |
6438 | if (!QUALIFIED_P (left_id)) | |
6439 | { | |
6440 | tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0); | |
6441 | EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left); | |
6442 | EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE); | |
6443 | } | |
6444 | ||
6445 | wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0); | |
6446 | EXPR_WFL_LINECOL (wfl) = location; | |
6447 | chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE)); | |
6448 | ||
6449 | EXPR_WFL_NODE (left) = merge; | |
6450 | return left; | |
bc3ca41b | 6451 | #endif |
e04a16fb AG |
6452 | } |
6453 | ||
6454 | /* Extract the last identifier component of the qualified in WFL. The | |
6455 | last identifier is removed from the linked list */ | |
6456 | ||
6457 | static tree | |
6458 | cut_identifier_in_qualified (wfl) | |
6459 | tree wfl; | |
6460 | { | |
6461 | tree q; | |
6462 | tree previous = NULL_TREE; | |
6463 | for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q)) | |
6464 | if (!TREE_CHAIN (q)) | |
6465 | { | |
6466 | if (!previous) | |
781b0558 | 6467 | fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified"); |
e04a16fb AG |
6468 | TREE_CHAIN (previous) = NULL_TREE; |
6469 | return TREE_PURPOSE (q); | |
6470 | } | |
6471 | } | |
6472 | ||
6473 | /* Resolve the expression name NAME. Return its decl. */ | |
6474 | ||
6475 | static tree | |
5e942c50 | 6476 | resolve_expression_name (id, orig) |
e04a16fb | 6477 | tree id; |
5e942c50 | 6478 | tree *orig; |
e04a16fb AG |
6479 | { |
6480 | tree name = EXPR_WFL_NODE (id); | |
6481 | tree decl; | |
6482 | ||
6483 | /* 6.5.5.1: Simple expression names */ | |
6484 | if (!PRIMARY_P (id) && !QUALIFIED_P (name)) | |
6485 | { | |
6486 | /* 15.13.1: NAME can appear within the scope of a local variable | |
6487 | declaration */ | |
6488 | if ((decl = IDENTIFIER_LOCAL_VALUE (name))) | |
6489 | return decl; | |
6490 | ||
6491 | /* 15.13.1: NAME can appear within a class declaration */ | |
6492 | else | |
6493 | { | |
6494 | decl = lookup_field_wrapper (current_class, name); | |
6495 | if (decl) | |
6496 | { | |
6497 | int fs = FIELD_STATIC (decl); | |
6498 | /* Instance variable (8.3.1.1) can't appear within | |
6499 | static method, static initializer or initializer for | |
6500 | a static variable. */ | |
6501 | if (!fs && METHOD_STATIC (current_function_decl)) | |
6502 | { | |
7f10c2e2 | 6503 | static_ref_err (id, name, current_class); |
e04a16fb AG |
6504 | return error_mark_node; |
6505 | } | |
22eed1e6 APB |
6506 | /* Instance variables can't appear as an argument of |
6507 | an explicit constructor invocation */ | |
6508 | if (!fs && ctxp->explicit_constructor_p) | |
6509 | { | |
6510 | parse_error_context | |
781b0558 | 6511 | (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name)); |
22eed1e6 APB |
6512 | return error_mark_node; |
6513 | } | |
5e942c50 APB |
6514 | |
6515 | /* Otherwise build what it takes to access the field */ | |
e04a16fb | 6516 | decl = build_field_ref ((fs ? NULL_TREE : current_this), |
7f1d4866 | 6517 | DECL_CONTEXT (decl), name); |
e8fc7396 | 6518 | if (fs && !flag_emit_class_files && !flag_emit_xref) |
7f1d4866 | 6519 | decl = build_class_init (DECL_CONTEXT (decl), decl); |
5e942c50 APB |
6520 | /* We may be asked to save the real field access node */ |
6521 | if (orig) | |
6522 | *orig = decl; | |
6523 | /* And we return what we got */ | |
5b09b33e | 6524 | return decl; |
e04a16fb AG |
6525 | } |
6526 | /* Fall down to error report on undefined variable */ | |
6527 | } | |
6528 | } | |
6529 | /* 6.5.5.2 Qualified Expression Names */ | |
6530 | else | |
6531 | { | |
5e942c50 APB |
6532 | if (orig) |
6533 | *orig = NULL_TREE; | |
e04a16fb AG |
6534 | qualify_ambiguous_name (id); |
6535 | /* 15.10.1 Field Access Using a Primary and/or Expression Name */ | |
6536 | /* 15.10.2: Accessing Superclass Members using super */ | |
6537 | return resolve_field_access (id, NULL, NULL); | |
6538 | } | |
6539 | ||
6540 | /* We've got an error here */ | |
6541 | parse_error_context (id, "Undefined variable `%s'", | |
6542 | IDENTIFIER_POINTER (name)); | |
6543 | ||
6544 | return error_mark_node; | |
6545 | } | |
6546 | ||
7f10c2e2 APB |
6547 | static void |
6548 | static_ref_err (wfl, field_id, class_type) | |
6549 | tree wfl, field_id, class_type; | |
6550 | { | |
6551 | parse_error_context | |
6552 | (wfl, | |
6553 | "Can't make a static reference to nonstatic variable `%s' in class `%s'", | |
6554 | IDENTIFIER_POINTER (field_id), | |
6555 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type)))); | |
6556 | } | |
6557 | ||
e04a16fb AG |
6558 | /* 15.10.1 Field Acess Using a Primary and/or Expression Name. |
6559 | We return something suitable to generate the field access. We also | |
6560 | return the field decl in FIELD_DECL and its type in FIELD_TYPE. If | |
6561 | recipient's address can be null. */ | |
6562 | ||
6563 | static tree | |
6564 | resolve_field_access (qual_wfl, field_decl, field_type) | |
6565 | tree qual_wfl; | |
6566 | tree *field_decl, *field_type; | |
6567 | { | |
6568 | int is_static = 0; | |
6569 | tree field_ref; | |
6570 | tree decl, where_found, type_found; | |
6571 | ||
6572 | if (resolve_qualified_expression_name (qual_wfl, &decl, | |
6573 | &where_found, &type_found)) | |
6574 | return error_mark_node; | |
6575 | ||
6576 | /* Resolve the LENGTH field of an array here */ | |
6577 | if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found) | |
e8fc7396 | 6578 | && ! flag_emit_class_files && ! flag_emit_xref) |
e04a16fb AG |
6579 | { |
6580 | tree length = build_java_array_length_access (where_found); | |
6581 | field_ref = | |
6582 | build_java_arraynull_check (type_found, length, int_type_node); | |
6583 | } | |
6584 | /* We might have been trying to resolve field.method(). In which | |
6585 | case, the resolution is over and decl is the answer */ | |
34f4db93 | 6586 | else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl) |
e04a16fb | 6587 | field_ref = decl; |
34f4db93 | 6588 | else if (JDECL_P (decl)) |
e04a16fb | 6589 | { |
5e942c50 APB |
6590 | int static_final_found = 0; |
6591 | if (!type_found) | |
6592 | type_found = DECL_CONTEXT (decl); | |
34f4db93 | 6593 | is_static = JDECL_P (decl) && FIELD_STATIC (decl); |
5e942c50 APB |
6594 | if (FIELD_FINAL (decl) |
6595 | && JPRIMITIVE_TYPE_P (TREE_TYPE (decl)) | |
6596 | && DECL_LANG_SPECIFIC (decl) | |
7525cc04 | 6597 | && DECL_INITIAL (decl)) |
5e942c50 | 6598 | { |
7525cc04 | 6599 | field_ref = DECL_INITIAL (decl); |
5e942c50 APB |
6600 | static_final_found = 1; |
6601 | } | |
6602 | else | |
7f10c2e2 APB |
6603 | field_ref = build_field_ref ((is_static && !flag_emit_xref? |
6604 | NULL_TREE : where_found), | |
5e942c50 | 6605 | type_found, DECL_NAME (decl)); |
e04a16fb AG |
6606 | if (field_ref == error_mark_node) |
6607 | return error_mark_node; | |
e8fc7396 APB |
6608 | if (is_static && !static_final_found |
6609 | && !flag_emit_class_files && !flag_emit_xref) | |
e04a16fb AG |
6610 | { |
6611 | field_ref = build_class_init (type_found, field_ref); | |
6612 | /* If the static field was identified by an expression that | |
6613 | needs to be generated, make the field access a compound | |
7f10c2e2 | 6614 | expression whose first part is the evaluation of the |
e04a16fb | 6615 | field selector part. */ |
c877974e APB |
6616 | if (where_found && TREE_CODE (where_found) != TYPE_DECL |
6617 | && TREE_CODE (where_found) != RECORD_TYPE) | |
e04a16fb AG |
6618 | { |
6619 | tree type = QUAL_DECL_TYPE (field_ref); | |
8576f094 APB |
6620 | if (TREE_CODE (type) == RECORD_TYPE) |
6621 | type = build_pointer_type (type); | |
e04a16fb AG |
6622 | field_ref = build (COMPOUND_EXPR, type, where_found, field_ref); |
6623 | } | |
6624 | } | |
6625 | } | |
6626 | else | |
6627 | field_ref = decl; | |
6628 | ||
6629 | if (field_decl) | |
6630 | *field_decl = decl; | |
6631 | if (field_type) | |
c877974e APB |
6632 | *field_type = (QUAL_DECL_TYPE (decl) ? |
6633 | QUAL_DECL_TYPE (decl) : TREE_TYPE (decl)); | |
e04a16fb AG |
6634 | return field_ref; |
6635 | } | |
6636 | ||
e28cd97b APB |
6637 | /* If NODE is an access to f static field, strip out the class |
6638 | initialization part and return the field decl, otherwise, return | |
6639 | NODE. */ | |
6640 | ||
6641 | static tree | |
6642 | strip_out_static_field_access_decl (node) | |
6643 | tree node; | |
6644 | { | |
6645 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
6646 | { | |
6647 | tree op1 = TREE_OPERAND (node, 1); | |
6648 | if (TREE_CODE (op1) == COMPOUND_EXPR) | |
6649 | { | |
6650 | tree call = TREE_OPERAND (op1, 0); | |
6651 | if (TREE_CODE (call) == CALL_EXPR | |
6652 | && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR | |
6653 | && TREE_OPERAND (TREE_OPERAND (call, 0), 0) | |
6654 | == soft_initclass_node) | |
6655 | return TREE_OPERAND (op1, 1); | |
6656 | } | |
2f11d407 TT |
6657 | else if (JDECL_P (op1)) |
6658 | return op1; | |
e28cd97b APB |
6659 | } |
6660 | return node; | |
6661 | } | |
6662 | ||
e04a16fb AG |
6663 | /* 6.5.5.2: Qualified Expression Names */ |
6664 | ||
6665 | static int | |
6666 | resolve_qualified_expression_name (wfl, found_decl, where_found, type_found) | |
6667 | tree wfl; | |
6668 | tree *found_decl, *type_found, *where_found; | |
6669 | { | |
6670 | int from_type = 0; /* Field search initiated from a type */ | |
6671 | int from_super = 0, from_cast = 0; | |
6672 | int previous_call_static = 0; | |
6673 | int is_static; | |
6674 | tree decl = NULL_TREE, type = NULL_TREE, q; | |
c877974e | 6675 | *type_found = *where_found = NULL_TREE; |
e04a16fb AG |
6676 | |
6677 | for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q)) | |
6678 | { | |
6679 | tree qual_wfl = QUAL_WFL (q); | |
7705e9db APB |
6680 | tree ret_decl; /* for EH checking */ |
6681 | int location; /* for EH checking */ | |
e04a16fb AG |
6682 | |
6683 | /* 15.10.1 Field Access Using a Primary */ | |
e04a16fb AG |
6684 | switch (TREE_CODE (qual_wfl)) |
6685 | { | |
6686 | case CALL_EXPR: | |
b67d701b | 6687 | case NEW_CLASS_EXPR: |
e04a16fb AG |
6688 | /* If the access to the function call is a non static field, |
6689 | build the code to access it. */ | |
34f4db93 | 6690 | if (JDECL_P (decl) && !FIELD_STATIC (decl)) |
e04a16fb | 6691 | { |
ac825856 APB |
6692 | decl = maybe_access_field (decl, *where_found, |
6693 | DECL_CONTEXT (decl)); | |
e04a16fb AG |
6694 | if (decl == error_mark_node) |
6695 | return 1; | |
6696 | } | |
6697 | /* And code for the function call */ | |
6698 | if (complete_function_arguments (qual_wfl)) | |
6699 | return 1; | |
7705e9db | 6700 | |
89e09b9a PB |
6701 | if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR) |
6702 | CALL_USING_SUPER (qual_wfl) = 1; | |
7705e9db APB |
6703 | location = (TREE_CODE (qual_wfl) == CALL_EXPR ? |
6704 | EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0); | |
6705 | *where_found = patch_method_invocation (qual_wfl, decl, type, | |
6706 | &is_static, &ret_decl); | |
e04a16fb AG |
6707 | if (*where_found == error_mark_node) |
6708 | return 1; | |
6709 | *type_found = type = QUAL_DECL_TYPE (*where_found); | |
6710 | ||
7705e9db APB |
6711 | /* EH check */ |
6712 | if (location) | |
6713 | check_thrown_exceptions (location, ret_decl); | |
6714 | ||
e04a16fb AG |
6715 | /* If the previous call was static and this one is too, |
6716 | build a compound expression to hold the two (because in | |
6717 | that case, previous function calls aren't transported as | |
6718 | forcoming function's argument. */ | |
6719 | if (previous_call_static && is_static) | |
6720 | { | |
6721 | decl = build (COMPOUND_EXPR, type, decl, *where_found); | |
6722 | TREE_SIDE_EFFECTS (decl) = 1; | |
6723 | } | |
6724 | else | |
6725 | { | |
6726 | previous_call_static = is_static; | |
6727 | decl = *where_found; | |
6728 | } | |
6729 | continue; | |
6730 | ||
d8fccff5 APB |
6731 | case NEW_ARRAY_EXPR: |
6732 | *where_found = decl = java_complete_tree (qual_wfl); | |
6733 | if (decl == error_mark_node) | |
6734 | return 1; | |
6735 | *type_found = type = QUAL_DECL_TYPE (decl); | |
6736 | CLASS_LOADED_P (type) = 1; | |
6737 | continue; | |
6738 | ||
e04a16fb AG |
6739 | case CONVERT_EXPR: |
6740 | *where_found = decl = java_complete_tree (qual_wfl); | |
6741 | if (decl == error_mark_node) | |
6742 | return 1; | |
6743 | *type_found = type = QUAL_DECL_TYPE (decl); | |
6744 | from_cast = 1; | |
6745 | continue; | |
6746 | ||
22eed1e6 | 6747 | case CONDITIONAL_EXPR: |
5e942c50 | 6748 | case STRING_CST: |
ac22f9cb | 6749 | case MODIFY_EXPR: |
22eed1e6 APB |
6750 | *where_found = decl = java_complete_tree (qual_wfl); |
6751 | if (decl == error_mark_node) | |
6752 | return 1; | |
6753 | *type_found = type = QUAL_DECL_TYPE (decl); | |
6754 | continue; | |
6755 | ||
e04a16fb AG |
6756 | case ARRAY_REF: |
6757 | /* If the access to the function call is a non static field, | |
6758 | build the code to access it. */ | |
34f4db93 | 6759 | if (JDECL_P (decl) && !FIELD_STATIC (decl)) |
e04a16fb AG |
6760 | { |
6761 | decl = maybe_access_field (decl, *where_found, type); | |
6762 | if (decl == error_mark_node) | |
6763 | return 1; | |
6764 | } | |
6765 | /* And code for the array reference expression */ | |
6766 | decl = java_complete_tree (qual_wfl); | |
6767 | if (decl == error_mark_node) | |
6768 | return 1; | |
6769 | type = QUAL_DECL_TYPE (decl); | |
6770 | continue; | |
0a2138e2 | 6771 | |
37feda7d APB |
6772 | case PLUS_EXPR: |
6773 | if ((decl = java_complete_tree (qual_wfl)) == error_mark_node) | |
6774 | return 1; | |
6775 | if ((type = patch_string (decl))) | |
6776 | decl = type; | |
6777 | *where_found = QUAL_RESOLUTION (q) = decl; | |
6778 | *type_found = type = TREE_TYPE (decl); | |
6779 | break; | |
6780 | ||
0a2138e2 APB |
6781 | default: |
6782 | /* Fix for -Wall Just go to the next statement. Don't | |
6783 | continue */ | |
a3f406ce | 6784 | break; |
e04a16fb AG |
6785 | } |
6786 | ||
6787 | /* If we fall here, we weren't processing a (static) function call. */ | |
6788 | previous_call_static = 0; | |
6789 | ||
6790 | /* It can be the keyword THIS */ | |
6791 | if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node) | |
6792 | { | |
6793 | if (!current_this) | |
6794 | { | |
6795 | parse_error_context | |
6796 | (wfl, "Keyword `this' used outside allowed context"); | |
6797 | return 1; | |
6798 | } | |
f63991a8 APB |
6799 | if (ctxp->explicit_constructor_p) |
6800 | { | |
781b0558 | 6801 | parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called"); |
f63991a8 APB |
6802 | return 1; |
6803 | } | |
e04a16fb AG |
6804 | /* We have to generate code for intermediate acess */ |
6805 | *where_found = decl = current_this; | |
5e942c50 | 6806 | *type_found = type = QUAL_DECL_TYPE (decl); |
e04a16fb AG |
6807 | continue; |
6808 | } | |
6809 | ||
6810 | /* 15.10.2 Accessing Superclass Members using SUPER */ | |
6811 | if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node) | |
6812 | { | |
6813 | tree node; | |
6814 | /* Check on the restricted use of SUPER */ | |
6815 | if (METHOD_STATIC (current_function_decl) | |
6816 | || current_class == object_type_node) | |
6817 | { | |
6818 | parse_error_context | |
6819 | (wfl, "Keyword `super' used outside allowed context"); | |
6820 | return 1; | |
6821 | } | |
6822 | /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */ | |
6823 | node = build_cast (EXPR_WFL_LINECOL (qual_wfl), | |
6824 | CLASSTYPE_SUPER (current_class), | |
6825 | build_this (EXPR_WFL_LINECOL (qual_wfl))); | |
6826 | *where_found = decl = java_complete_tree (node); | |
22eed1e6 APB |
6827 | if (decl == error_mark_node) |
6828 | return 1; | |
e04a16fb AG |
6829 | *type_found = type = QUAL_DECL_TYPE (decl); |
6830 | from_super = from_type = 1; | |
6831 | continue; | |
6832 | } | |
6833 | ||
6834 | /* 15.13.1: Can't search for field name in packages, so we | |
6835 | assume a variable/class name was meant. */ | |
6836 | if (RESOLVE_PACKAGE_NAME_P (qual_wfl)) | |
6837 | { | |
5e942c50 APB |
6838 | tree name = resolve_package (wfl, &q); |
6839 | if (name) | |
6840 | { | |
6841 | *where_found = decl = resolve_no_layout (name, qual_wfl); | |
6842 | /* We wan't to be absolutely that the class is laid | |
6843 | out. We're going to search something inside it. */ | |
6844 | *type_found = type = TREE_TYPE (decl); | |
6845 | layout_class (type); | |
6846 | from_type = 1; | |
6847 | /* Should be a list, really. FIXME */ | |
6848 | RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 1; | |
6849 | RESOLVE_PACKAGE_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 0; | |
6850 | } | |
e04a16fb | 6851 | else |
5e942c50 APB |
6852 | { |
6853 | if (from_super || from_cast) | |
6854 | parse_error_context | |
6855 | ((from_cast ? qual_wfl : wfl), | |
6856 | "No variable `%s' defined in class `%s'", | |
6857 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), | |
6858 | lang_printable_name (type, 0)); | |
6859 | else | |
6860 | parse_error_context | |
6861 | (qual_wfl, "Undefined variable or class name: `%s'", | |
6862 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl))); | |
6863 | return 1; | |
6864 | } | |
e04a16fb AG |
6865 | } |
6866 | ||
6867 | /* We have a type name. It's been already resolved when the | |
6868 | expression was qualified. */ | |
6869 | else if (RESOLVE_TYPE_NAME_P (qual_wfl)) | |
6870 | { | |
6871 | if (!(decl = QUAL_RESOLUTION (q))) | |
6872 | return 1; /* Error reported already */ | |
6873 | ||
6874 | if (not_accessible_p (TREE_TYPE (decl), decl, 0)) | |
6875 | { | |
6876 | parse_error_context | |
6877 | (qual_wfl, "Can't access %s field `%s.%s' from `%s'", | |
6878 | java_accstring_lookup (get_access_flags_from_decl (decl)), | |
2aa11e97 | 6879 | GET_TYPE_NAME (type), |
e04a16fb AG |
6880 | IDENTIFIER_POINTER (DECL_NAME (decl)), |
6881 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)))); | |
6882 | return 1; | |
6883 | } | |
5e942c50 | 6884 | check_deprecation (qual_wfl, decl); |
e04a16fb AG |
6885 | |
6886 | type = TREE_TYPE (decl); | |
6887 | from_type = 1; | |
6888 | } | |
6889 | /* We resolve and expression name */ | |
6890 | else | |
6891 | { | |
cd531a2e | 6892 | tree field_decl = NULL_TREE; |
e04a16fb AG |
6893 | |
6894 | /* If there exists an early resolution, use it. That occurs | |
6895 | only once and we know that there are more things to | |
6896 | come. Don't do that when processing something after SUPER | |
6897 | (we need more thing to be put in place below */ | |
6898 | if (!from_super && QUAL_RESOLUTION (q)) | |
b67d701b PB |
6899 | { |
6900 | decl = QUAL_RESOLUTION (q); | |
c877974e | 6901 | if (!type) |
5e942c50 | 6902 | { |
7f10c2e2 APB |
6903 | if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl)) |
6904 | { | |
6905 | if (current_this) | |
6906 | *where_found = current_this; | |
6907 | else | |
6908 | { | |
6909 | static_ref_err (qual_wfl, DECL_NAME (decl), | |
6910 | current_class); | |
6911 | return 1; | |
6912 | } | |
6913 | } | |
c877974e APB |
6914 | else |
6915 | { | |
6916 | *where_found = TREE_TYPE (decl); | |
6917 | if (TREE_CODE (*where_found) == POINTER_TYPE) | |
6918 | *where_found = TREE_TYPE (*where_found); | |
6919 | } | |
5e942c50 | 6920 | } |
b67d701b | 6921 | } |
e04a16fb AG |
6922 | |
6923 | /* We have to search for a field, knowing the type of its | |
6924 | container. The flag FROM_TYPE indicates that we resolved | |
6925 | the last member of the expression as a type name, which | |
5e942c50 APB |
6926 | means that for the resolution of this field, we'll look |
6927 | for other errors than if it was resolved as a member of | |
6928 | an other field. */ | |
e04a16fb AG |
6929 | else |
6930 | { | |
6931 | int is_static; | |
5e942c50 APB |
6932 | tree field_decl_type; /* For layout */ |
6933 | ||
e04a16fb AG |
6934 | if (!from_type && !JREFERENCE_TYPE_P (type)) |
6935 | { | |
6936 | parse_error_context | |
6937 | (qual_wfl, "Attempt to reference field `%s' in `%s %s'", | |
6938 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), | |
0a2138e2 | 6939 | lang_printable_name (type, 0), |
e04a16fb AG |
6940 | IDENTIFIER_POINTER (DECL_NAME (field_decl))); |
6941 | return 1; | |
6942 | } | |
6943 | ||
dc0b3eff PB |
6944 | field_decl = lookup_field_wrapper (type, |
6945 | EXPR_WFL_NODE (qual_wfl)); | |
6946 | if (field_decl == NULL_TREE) | |
e04a16fb AG |
6947 | { |
6948 | parse_error_context | |
2aa11e97 | 6949 | (qual_wfl, "No variable `%s' defined in type `%s'", |
e04a16fb | 6950 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), |
2aa11e97 | 6951 | GET_TYPE_NAME (type)); |
e04a16fb AG |
6952 | return 1; |
6953 | } | |
dc0b3eff PB |
6954 | if (field_decl == error_mark_node) |
6955 | return 1; | |
5e942c50 APB |
6956 | |
6957 | /* Layout the type of field_decl, since we may need | |
c877974e APB |
6958 | it. Don't do primitive types or loaded classes. The |
6959 | situation of non primitive arrays may not handled | |
6960 | properly here. FIXME */ | |
5e942c50 APB |
6961 | if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE) |
6962 | field_decl_type = TREE_TYPE (TREE_TYPE (field_decl)); | |
6963 | else | |
6964 | field_decl_type = TREE_TYPE (field_decl); | |
6965 | if (!JPRIMITIVE_TYPE_P (field_decl_type) | |
c877974e APB |
6966 | && !CLASS_LOADED_P (field_decl_type) |
6967 | && !TYPE_ARRAY_P (field_decl_type)) | |
6968 | resolve_and_layout (field_decl_type, NULL_TREE); | |
6969 | if (TYPE_ARRAY_P (field_decl_type)) | |
6970 | CLASS_LOADED_P (field_decl_type) = 1; | |
e04a16fb AG |
6971 | |
6972 | /* Check on accessibility here */ | |
6973 | if (not_accessible_p (type, field_decl, from_super)) | |
6974 | { | |
6975 | parse_error_context | |
6976 | (qual_wfl, | |
6977 | "Can't access %s field `%s.%s' from `%s'", | |
6978 | java_accstring_lookup | |
6979 | (get_access_flags_from_decl (field_decl)), | |
2aa11e97 | 6980 | GET_TYPE_NAME (type), |
e04a16fb AG |
6981 | IDENTIFIER_POINTER (DECL_NAME (field_decl)), |
6982 | IDENTIFIER_POINTER | |
6983 | (DECL_NAME (TYPE_NAME (current_class)))); | |
6984 | return 1; | |
6985 | } | |
5e942c50 | 6986 | check_deprecation (qual_wfl, field_decl); |
e04a16fb AG |
6987 | |
6988 | /* There are things to check when fields are accessed | |
6989 | from type. There are no restrictions on a static | |
6990 | declaration of the field when it is accessed from an | |
6991 | interface */ | |
6992 | is_static = FIELD_STATIC (field_decl); | |
6993 | if (!from_super && from_type | |
6994 | && !TYPE_INTERFACE_P (type) && !is_static) | |
6995 | { | |
7f10c2e2 | 6996 | static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type); |
e04a16fb AG |
6997 | return 1; |
6998 | } | |
6999 | from_cast = from_super = 0; | |
7000 | ||
5e942c50 APB |
7001 | /* If we need to generate something to get a proper |
7002 | handle on what this field is accessed from, do it | |
7003 | now. */ | |
e04a16fb AG |
7004 | if (!is_static) |
7005 | { | |
c583dd46 | 7006 | decl = maybe_access_field (decl, *where_found, *type_found); |
e04a16fb AG |
7007 | if (decl == error_mark_node) |
7008 | return 1; | |
7009 | } | |
7010 | ||
7011 | /* We want to keep the location were found it, and the type | |
7012 | we found. */ | |
7013 | *where_found = decl; | |
7014 | *type_found = type; | |
7015 | ||
7016 | /* This is the decl found and eventually the next one to | |
7017 | search from */ | |
7018 | decl = field_decl; | |
7019 | } | |
e04a16fb AG |
7020 | from_type = 0; |
7021 | type = QUAL_DECL_TYPE (decl); | |
7022 | } | |
7023 | } | |
7024 | *found_decl = decl; | |
7025 | return 0; | |
7026 | } | |
7027 | ||
7028 | /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl) | |
7029 | can't be accessed from REFERENCE (a record type). */ | |
7030 | ||
be245ac0 KG |
7031 | static int |
7032 | not_accessible_p (reference, member, from_super) | |
e04a16fb AG |
7033 | tree reference, member; |
7034 | int from_super; | |
7035 | { | |
7036 | int access_flag = get_access_flags_from_decl (member); | |
7037 | ||
7038 | /* Access always granted for members declared public */ | |
7039 | if (access_flag & ACC_PUBLIC) | |
7040 | return 0; | |
7041 | ||
7042 | /* Check access on protected members */ | |
7043 | if (access_flag & ACC_PROTECTED) | |
7044 | { | |
7045 | /* Access granted if it occurs from within the package | |
7046 | containing the class in which the protected member is | |
7047 | declared */ | |
7048 | if (class_in_current_package (DECL_CONTEXT (member))) | |
7049 | return 0; | |
7050 | ||
9bbc7d9f PB |
7051 | /* If accessed with the form `super.member', then access is granted */ |
7052 | if (from_super) | |
7053 | return 0; | |
e04a16fb | 7054 | |
9bbc7d9f PB |
7055 | /* Otherwise, access is granted if occuring from the class where |
7056 | member is declared or a subclass of it */ | |
7057 | if (inherits_from_p (reference, current_class)) | |
7058 | return 0; | |
e04a16fb AG |
7059 | return 1; |
7060 | } | |
7061 | ||
7062 | /* Check access on private members. Access is granted only if it | |
c877974e | 7063 | occurs from within the class in witch it is declared */ |
e04a16fb AG |
7064 | if (access_flag & ACC_PRIVATE) |
7065 | return (current_class == DECL_CONTEXT (member) ? 0 : 1); | |
7066 | ||
7067 | /* Default access are permitted only when occuring within the | |
7068 | package in which the type (REFERENCE) is declared. In other words, | |
7069 | REFERENCE is defined in the current package */ | |
7070 | if (ctxp->package) | |
7071 | return !class_in_current_package (reference); | |
7072 | ||
7073 | /* Otherwise, access is granted */ | |
7074 | return 0; | |
7075 | } | |
7076 | ||
5e942c50 APB |
7077 | /* Test deprecated decl access. */ |
7078 | static void | |
7079 | check_deprecation (wfl, decl) | |
7080 | tree wfl, decl; | |
7081 | { | |
49f48c71 | 7082 | const char *file = DECL_SOURCE_FILE (decl); |
5e942c50 APB |
7083 | /* Complain if the field is deprecated and the file it was defined |
7084 | in isn't compiled at the same time the file which contains its | |
7085 | use is */ | |
7086 | if (DECL_DEPRECATED (decl) | |
7087 | && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file))) | |
7088 | { | |
7089 | char the [20]; | |
7090 | switch (TREE_CODE (decl)) | |
7091 | { | |
7092 | case FUNCTION_DECL: | |
7093 | strcpy (the, "method"); | |
7094 | break; | |
7095 | case FIELD_DECL: | |
7096 | strcpy (the, "field"); | |
7097 | break; | |
7098 | case TYPE_DECL: | |
7099 | strcpy (the, "class"); | |
7100 | break; | |
15fdcfe9 PB |
7101 | default: |
7102 | fatal ("unexpected DECL code - check_deprecation"); | |
5e942c50 APB |
7103 | } |
7104 | parse_warning_context | |
7105 | (wfl, "The %s `%s' in class `%s' has been deprecated", | |
7106 | the, lang_printable_name (decl, 0), | |
7107 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))); | |
7108 | } | |
7109 | } | |
7110 | ||
e04a16fb AG |
7111 | /* Returns 1 if class was declared in the current package, 0 otherwise */ |
7112 | ||
7113 | static int | |
7114 | class_in_current_package (class) | |
7115 | tree class; | |
7116 | { | |
7117 | static tree cache = NULL_TREE; | |
7118 | int qualified_flag; | |
7119 | tree left; | |
7120 | ||
7121 | if (cache == class) | |
7122 | return 1; | |
7123 | ||
7124 | qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class))); | |
7125 | ||
7126 | /* If the current package is empty and the name of CLASS is | |
7127 | qualified, class isn't in the current package. If there is a | |
7128 | current package and the name of the CLASS is not qualified, class | |
7129 | isn't in the current package */ | |
0a2138e2 | 7130 | if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag)) |
e04a16fb AG |
7131 | return 0; |
7132 | ||
7133 | /* If there is not package and the name of CLASS isn't qualified, | |
7134 | they belong to the same unnamed package */ | |
7135 | if (!ctxp->package && !qualified_flag) | |
7136 | return 1; | |
7137 | ||
7138 | /* Compare the left part of the name of CLASS with the package name */ | |
7139 | breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class))); | |
7140 | if (ctxp->package == left) | |
7141 | { | |
7142 | cache = class; | |
7143 | return 1; | |
7144 | } | |
7145 | return 0; | |
7146 | } | |
7147 | ||
7148 | /* This function may generate code to access DECL from WHERE. This is | |
7149 | done only if certain conditions meet. */ | |
7150 | ||
7151 | static tree | |
7152 | maybe_access_field (decl, where, type) | |
7153 | tree decl, where, type; | |
7154 | { | |
5e942c50 APB |
7155 | if (TREE_CODE (decl) == FIELD_DECL && decl != current_this |
7156 | && !FIELD_STATIC (decl)) | |
e04a16fb | 7157 | decl = build_field_ref (where ? where : current_this, |
c583dd46 APB |
7158 | (type ? type : DECL_CONTEXT (decl)), |
7159 | DECL_NAME (decl)); | |
e04a16fb AG |
7160 | return decl; |
7161 | } | |
7162 | ||
15fdcfe9 | 7163 | /* Build a method invocation, by patching PATCH. If non NULL |
e04a16fb AG |
7164 | and according to the situation, PRIMARY and WHERE may be |
7165 | used. IS_STATIC is set to 1 if the invoked function is static. */ | |
7166 | ||
7167 | static tree | |
89e09b9a | 7168 | patch_method_invocation (patch, primary, where, is_static, ret_decl) |
e04a16fb AG |
7169 | tree patch, primary, where; |
7170 | int *is_static; | |
b9f7e36c | 7171 | tree *ret_decl; |
e04a16fb AG |
7172 | { |
7173 | tree wfl = TREE_OPERAND (patch, 0); | |
7174 | tree args = TREE_OPERAND (patch, 1); | |
7175 | tree name = EXPR_WFL_NODE (wfl); | |
5e942c50 | 7176 | tree list; |
22eed1e6 | 7177 | int is_static_flag = 0; |
89e09b9a | 7178 | int is_super_init = 0; |
bccaf73a | 7179 | tree this_arg = NULL_TREE; |
e04a16fb AG |
7180 | |
7181 | /* Should be overriden if everything goes well. Otherwise, if | |
7182 | something fails, it should keep this value. It stop the | |
7183 | evaluation of a bogus assignment. See java_complete_tree, | |
7184 | MODIFY_EXPR: for the reasons why we sometimes want to keep on | |
7185 | evaluating an assignment */ | |
7186 | TREE_TYPE (patch) = error_mark_node; | |
7187 | ||
7188 | /* Since lookup functions are messing with line numbers, save the | |
7189 | context now. */ | |
7190 | java_parser_context_save_global (); | |
7191 | ||
7192 | /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */ | |
7193 | ||
7194 | /* Resolution of qualified name, excluding constructors */ | |
7195 | if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch)) | |
7196 | { | |
7197 | tree class_decl, identifier, identifier_wfl; | |
7198 | /* Extract the last IDENTIFIER of the qualified | |
7199 | expression. This is a wfl and we will use it's location | |
7200 | data during error report. */ | |
7201 | identifier_wfl = cut_identifier_in_qualified (wfl); | |
7202 | identifier = EXPR_WFL_NODE (identifier_wfl); | |
7203 | ||
7204 | /* Given the context, IDENTIFIER is syntactically qualified | |
7205 | as a MethodName. We need to qualify what's before */ | |
7206 | qualify_ambiguous_name (wfl); | |
7207 | ||
2c56429a | 7208 | /* Package resolution */ |
e04a16fb AG |
7209 | if (RESOLVE_PACKAGE_NAME_P (wfl)) |
7210 | { | |
2c56429a APB |
7211 | tree next, decl, name = resolve_package (wfl, &next); |
7212 | ||
7213 | if (!name) | |
7214 | { | |
7215 | tree remainder; | |
7216 | breakdown_qualified (&remainder, NULL, EXPR_WFL_NODE (wfl)); | |
781b0558 KG |
7217 | parse_error_context |
7218 | (wfl, "Can't search method `%s' in package `%s'", | |
7219 | IDENTIFIER_POINTER (identifier), | |
7220 | IDENTIFIER_POINTER (remainder)); | |
2c56429a APB |
7221 | PATCH_METHOD_RETURN_ERROR (); |
7222 | } | |
7223 | RESOLVE_PACKAGE_NAME_P (wfl) = 0; | |
7224 | if ((decl = resolve_no_layout (name, QUAL_WFL (next)))) | |
7225 | { | |
7226 | QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl)) = decl; | |
7227 | RESOLVE_EXPRESSION_NAME_P (wfl) = 0; | |
7228 | RESOLVE_TYPE_NAME_P (wfl) = 1; | |
7229 | } | |
7230 | else | |
7231 | { | |
7232 | RESOLVE_EXPRESSION_NAME_P (wfl) = 1; | |
7233 | RESOLVE_TYPE_NAME_P (wfl) = 0; | |
7234 | } | |
e04a16fb | 7235 | } |
2c56429a | 7236 | |
e04a16fb | 7237 | /* We're resolving a call from a type */ |
2c56429a | 7238 | if (RESOLVE_TYPE_NAME_P (wfl)) |
e04a16fb AG |
7239 | { |
7240 | tree decl = QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl)); | |
7241 | tree name = DECL_NAME (decl); | |
7242 | tree type; | |
7243 | ||
7244 | class_decl = resolve_and_layout (name, wfl); | |
7245 | if (CLASS_INTERFACE (decl)) | |
7246 | { | |
7247 | parse_error_context | |
781b0558 KG |
7248 | (identifier_wfl, |
7249 | "Can't make static reference to method `%s' in interface `%s'", | |
7250 | IDENTIFIER_POINTER (identifier), | |
e04a16fb | 7251 | IDENTIFIER_POINTER (name)); |
b9f7e36c | 7252 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb AG |
7253 | } |
7254 | /* Look the method up in the type selector. The method ought | |
7255 | to be static. */ | |
7256 | type = TREE_TYPE (class_decl); | |
7257 | list = lookup_method_invoke (0, wfl, type, identifier, args); | |
7258 | if (list && !METHOD_STATIC (list)) | |
7259 | { | |
c2e3db92 | 7260 | char *fct_name = xstrdup (lang_printable_name (list, 0)); |
e04a16fb AG |
7261 | parse_error_context |
7262 | (identifier_wfl, | |
7263 | "Can't make static reference to method `%s %s' in class `%s'", | |
0a2138e2 APB |
7264 | lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0), |
7265 | fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); | |
e04a16fb | 7266 | free (fct_name); |
b9f7e36c | 7267 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb | 7268 | } |
5e942c50 | 7269 | args = nreverse (args); |
e04a16fb AG |
7270 | } |
7271 | /* We're resolving an expression name */ | |
7272 | else | |
7273 | { | |
7274 | tree field, type; | |
7275 | ||
7276 | /* 1- Find the field to which the call applies */ | |
7277 | field = resolve_field_access (wfl, NULL, &type); | |
7278 | if (field == error_mark_node) | |
b9f7e36c | 7279 | PATCH_METHOD_RETURN_ERROR (); |
c3f2a476 APB |
7280 | /* field is used in lieu of a primary. It alows us not to |
7281 | report errors on erroneous use of `this' in | |
7282 | constructors. */ | |
7283 | primary = field; | |
e04a16fb AG |
7284 | |
7285 | /* 2- Do the layout of the class where the last field | |
7286 | was found, so we can search it. */ | |
c877974e | 7287 | class_decl = resolve_and_layout (type, NULL_TREE); |
3e78f871 | 7288 | if (class_decl != NULL_TREE) |
c877974e APB |
7289 | type = TREE_TYPE (class_decl); |
7290 | ||
e04a16fb AG |
7291 | /* 3- Retrieve a filtered list of method matches, Refine |
7292 | if necessary. In any cases, point out errors. */ | |
7293 | list = lookup_method_invoke (0, identifier_wfl, type, | |
7294 | identifier, args); | |
7295 | ||
7296 | /* 4- Add the field as an argument */ | |
bccaf73a PB |
7297 | args = nreverse (args); |
7298 | this_arg = field; | |
e04a16fb AG |
7299 | } |
7300 | ||
5e942c50 | 7301 | /* IDENTIFIER_WFL will be used to report any problem further */ |
e04a16fb AG |
7302 | wfl = identifier_wfl; |
7303 | } | |
7304 | /* Resolution of simple names, names generated after a primary: or | |
7305 | constructors */ | |
7306 | else | |
7307 | { | |
cd531a2e | 7308 | tree class_to_search = NULL_TREE; |
e04a16fb AG |
7309 | int lc; /* Looking for Constructor */ |
7310 | ||
7311 | /* We search constructor in their target class */ | |
7312 | if (CALL_CONSTRUCTOR_P (patch)) | |
7313 | { | |
22eed1e6 APB |
7314 | if (TREE_CODE (patch) == NEW_CLASS_EXPR) |
7315 | class_to_search = EXPR_WFL_NODE (wfl); | |
7316 | else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) == | |
7317 | this_identifier_node) | |
7318 | class_to_search = NULL_TREE; | |
7319 | else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) == | |
7320 | super_identifier_node) | |
e04a16fb | 7321 | { |
89e09b9a | 7322 | is_super_init = 1; |
22eed1e6 APB |
7323 | if (CLASSTYPE_SUPER (current_class)) |
7324 | class_to_search = | |
7325 | DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class))); | |
7326 | else | |
7327 | { | |
781b0558 | 7328 | parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object"); |
22eed1e6 APB |
7329 | PATCH_METHOD_RETURN_ERROR (); |
7330 | } | |
e04a16fb | 7331 | } |
22eed1e6 APB |
7332 | |
7333 | /* Class to search is NULL if we're searching the current one */ | |
7334 | if (class_to_search) | |
e04a16fb | 7335 | { |
23a79c61 APB |
7336 | class_to_search = resolve_and_layout (class_to_search, |
7337 | NULL_TREE); | |
22eed1e6 APB |
7338 | if (!class_to_search) |
7339 | { | |
7340 | parse_error_context | |
7341 | (wfl, "Class `%s' not found in type declaration", | |
7342 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
7343 | PATCH_METHOD_RETURN_ERROR (); | |
7344 | } | |
7345 | ||
5e942c50 APB |
7346 | /* Can't instantiate an abstract class, but we can |
7347 | invoke it's constructor. It's use within the `new' | |
7348 | context is denied here. */ | |
7349 | if (CLASS_ABSTRACT (class_to_search) | |
7350 | && TREE_CODE (patch) == NEW_CLASS_EXPR) | |
22eed1e6 APB |
7351 | { |
7352 | parse_error_context | |
781b0558 KG |
7353 | (wfl, "Class `%s' is an abstract class. It can't be instantiated", |
7354 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
22eed1e6 APB |
7355 | PATCH_METHOD_RETURN_ERROR (); |
7356 | } | |
7357 | class_to_search = TREE_TYPE (class_to_search); | |
e04a16fb | 7358 | } |
22eed1e6 APB |
7359 | else |
7360 | class_to_search = current_class; | |
e04a16fb AG |
7361 | lc = 1; |
7362 | } | |
7363 | /* This is a regular search in the local class, unless an | |
7364 | alternate class is specified. */ | |
7365 | else | |
7366 | { | |
7367 | class_to_search = (where ? where : current_class); | |
7368 | lc = 0; | |
7369 | } | |
7370 | ||
7371 | /* NAME is a simple identifier or comes from a primary. Search | |
7372 | in the class whose declaration contain the method being | |
7373 | invoked. */ | |
c877974e | 7374 | resolve_and_layout (class_to_search, NULL_TREE); |
e04a16fb AG |
7375 | list = lookup_method_invoke (lc, wfl, class_to_search, name, args); |
7376 | ||
7377 | /* Don't continue if no method were found, as the next statement | |
7378 | can't be executed then. */ | |
b9f7e36c APB |
7379 | if (!list) |
7380 | PATCH_METHOD_RETURN_ERROR (); | |
e04a16fb AG |
7381 | |
7382 | /* Check for static reference if non static methods */ | |
7383 | if (check_for_static_method_reference (wfl, patch, list, | |
7384 | class_to_search, primary)) | |
b9f7e36c | 7385 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb | 7386 | |
22eed1e6 APB |
7387 | /* Non static methods are called with the current object extra |
7388 | argument. If patch a `new TYPE()', the argument is the value | |
7389 | returned by the object allocator. If method is resolved as a | |
7390 | primary, use the primary otherwise use the current THIS. */ | |
b9f7e36c | 7391 | args = nreverse (args); |
bccaf73a PB |
7392 | if (TREE_CODE (patch) != NEW_CLASS_EXPR) |
7393 | this_arg = primary ? primary : current_this; | |
e04a16fb | 7394 | } |
b67d701b | 7395 | |
e04a16fb AG |
7396 | /* Merge point of all resolution schemes. If we have nothing, this |
7397 | is an error, already signaled */ | |
b9f7e36c APB |
7398 | if (!list) |
7399 | PATCH_METHOD_RETURN_ERROR (); | |
b67d701b | 7400 | |
e04a16fb AG |
7401 | /* Check accessibility, position the is_static flag, build and |
7402 | return the call */ | |
9bbc7d9f | 7403 | if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0)) |
e04a16fb | 7404 | { |
c2e3db92 | 7405 | char *fct_name = xstrdup (lang_printable_name (list, 0)); |
e04a16fb AG |
7406 | parse_error_context |
7407 | (wfl, "Can't access %s method `%s %s.%s' from `%s'", | |
7408 | java_accstring_lookup (get_access_flags_from_decl (list)), | |
0a2138e2 | 7409 | lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0), |
5e942c50 APB |
7410 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))), |
7411 | fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)))); | |
e04a16fb | 7412 | free (fct_name); |
b9f7e36c | 7413 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb | 7414 | } |
5e942c50 | 7415 | check_deprecation (wfl, list); |
22eed1e6 APB |
7416 | |
7417 | is_static_flag = METHOD_STATIC (list); | |
bccaf73a PB |
7418 | if (! METHOD_STATIC (list) && this_arg != NULL_TREE) |
7419 | args = tree_cons (NULL_TREE, this_arg, args); | |
22eed1e6 | 7420 | |
c3f2a476 APB |
7421 | /* In the context of an explicit constructor invocation, we can't |
7422 | invoke any method relying on `this'. Exceptions are: we're | |
7423 | invoking a static function, primary exists and is not the current | |
7424 | this, we're creating a new object. */ | |
22eed1e6 | 7425 | if (ctxp->explicit_constructor_p |
c3f2a476 APB |
7426 | && !is_static_flag |
7427 | && (!primary || primary == current_this) | |
7428 | && (TREE_CODE (patch) != NEW_CLASS_EXPR)) | |
22eed1e6 | 7429 | { |
781b0558 | 7430 | parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called"); |
22eed1e6 APB |
7431 | PATCH_METHOD_RETURN_ERROR (); |
7432 | } | |
e04a16fb | 7433 | java_parser_context_restore_global (); |
22eed1e6 APB |
7434 | if (is_static) |
7435 | *is_static = is_static_flag; | |
b9f7e36c APB |
7436 | /* Sometimes, we want the decl of the selected method. Such as for |
7437 | EH checking */ | |
7438 | if (ret_decl) | |
7439 | *ret_decl = list; | |
89e09b9a PB |
7440 | patch = patch_invoke (patch, list, args); |
7441 | if (is_super_init && CLASS_HAS_FINIT_P (current_class)) | |
7442 | { | |
7443 | /* Generate the code used to initialize fields declared with an | |
7444 | initialization statement. For now, it returns a call the the | |
7445 | artificial function $finit$, if required. */ | |
7446 | ||
7447 | tree finit_call = | |
7448 | build_method_invocation (build_expr_wfl (finit_identifier_node, | |
7449 | input_filename, 0, 0), | |
7450 | NULL_TREE); | |
7451 | patch = build (COMPOUND_EXPR, void_type_node, patch, | |
7452 | java_complete_tree (finit_call)); | |
7453 | CAN_COMPLETE_NORMALLY (patch) = 1; | |
7454 | } | |
7455 | return patch; | |
e04a16fb AG |
7456 | } |
7457 | ||
7458 | /* Check that we're not trying to do a static reference to a method in | |
7459 | non static method. Return 1 if it's the case, 0 otherwise. */ | |
7460 | ||
7461 | static int | |
7462 | check_for_static_method_reference (wfl, node, method, where, primary) | |
7463 | tree wfl, node, method, where, primary; | |
7464 | { | |
7465 | if (METHOD_STATIC (current_function_decl) | |
7466 | && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node)) | |
7467 | { | |
c2e3db92 | 7468 | char *fct_name = xstrdup (lang_printable_name (method, 0)); |
e04a16fb AG |
7469 | parse_error_context |
7470 | (wfl, "Can't make static reference to method `%s %s' in class `%s'", | |
0a2138e2 | 7471 | lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name, |
e04a16fb AG |
7472 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where)))); |
7473 | free (fct_name); | |
7474 | return 1; | |
7475 | } | |
7476 | return 0; | |
7477 | } | |
7478 | ||
7479 | /* Patch an invoke expression METHOD and ARGS, based on its invocation | |
7480 | mode. */ | |
7481 | ||
7482 | static tree | |
89e09b9a | 7483 | patch_invoke (patch, method, args) |
e04a16fb | 7484 | tree patch, method, args; |
e04a16fb AG |
7485 | { |
7486 | tree dtable, func; | |
0a2138e2 | 7487 | tree original_call, t, ta; |
e04a16fb | 7488 | |
5e942c50 APB |
7489 | /* Last step for args: convert build-in types. If we're dealing with |
7490 | a new TYPE() type call, the first argument to the constructor | |
7491 | isn't found in the incomming argument list, but delivered by | |
7492 | `new' */ | |
7493 | t = TYPE_ARG_TYPES (TREE_TYPE (method)); | |
7494 | if (TREE_CODE (patch) == NEW_CLASS_EXPR) | |
7495 | t = TREE_CHAIN (t); | |
ac825856 APB |
7496 | for (ta = args; t != end_params_node && ta; |
7497 | t = TREE_CHAIN (t), ta = TREE_CHAIN (ta)) | |
b9f7e36c APB |
7498 | if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) && |
7499 | TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t)) | |
7500 | TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta)); | |
1a6d4fb7 APB |
7501 | |
7502 | /* Resolve unresolved returned type isses */ | |
7503 | t = TREE_TYPE (TREE_TYPE (method)); | |
7504 | if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t))) | |
7505 | resolve_and_layout (TREE_TYPE (t), NULL); | |
22eed1e6 | 7506 | |
e8fc7396 | 7507 | if (flag_emit_class_files || flag_emit_xref) |
15fdcfe9 PB |
7508 | func = method; |
7509 | else | |
e04a16fb | 7510 | { |
15fdcfe9 | 7511 | tree signature = build_java_signature (TREE_TYPE (method)); |
89e09b9a | 7512 | switch (invocation_mode (method, CALL_USING_SUPER (patch))) |
15fdcfe9 PB |
7513 | { |
7514 | case INVOKE_VIRTUAL: | |
7515 | dtable = invoke_build_dtable (0, args); | |
7516 | func = build_invokevirtual (dtable, method); | |
7517 | break; | |
b9f7e36c | 7518 | |
15fdcfe9 PB |
7519 | case INVOKE_SUPER: |
7520 | case INVOKE_STATIC: | |
7521 | func = build_known_method_ref (method, TREE_TYPE (method), | |
7522 | DECL_CONTEXT (method), | |
7523 | signature, args); | |
7524 | break; | |
e04a16fb | 7525 | |
15fdcfe9 PB |
7526 | case INVOKE_INTERFACE: |
7527 | dtable = invoke_build_dtable (1, args); | |
7528 | func = build_invokeinterface (dtable, DECL_NAME (method), signature); | |
7529 | break; | |
5e942c50 | 7530 | |
15fdcfe9 | 7531 | default: |
89e09b9a | 7532 | fatal ("internal error - unknown invocation_mode result"); |
15fdcfe9 PB |
7533 | } |
7534 | ||
7535 | /* Ensure self_type is initialized, (invokestatic). FIXME */ | |
7536 | func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func); | |
e04a16fb AG |
7537 | } |
7538 | ||
e04a16fb AG |
7539 | TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method)); |
7540 | TREE_OPERAND (patch, 0) = func; | |
7541 | TREE_OPERAND (patch, 1) = args; | |
7542 | original_call = patch; | |
7543 | ||
22eed1e6 APB |
7544 | /* We're processing a `new TYPE ()' form. New is called an its |
7545 | returned value is the first argument to the constructor. We build | |
7546 | a COMPOUND_EXPR and use saved expression so that the overall NEW | |
7547 | expression value is a pointer to a newly created and initialized | |
7548 | class. */ | |
7549 | if (TREE_CODE (original_call) == NEW_CLASS_EXPR) | |
e04a16fb AG |
7550 | { |
7551 | tree class = DECL_CONTEXT (method); | |
7552 | tree c1, saved_new, size, new; | |
e8fc7396 | 7553 | if (flag_emit_class_files || flag_emit_xref) |
15fdcfe9 PB |
7554 | { |
7555 | TREE_TYPE (patch) = build_pointer_type (class); | |
7556 | return patch; | |
7557 | } | |
e04a16fb AG |
7558 | if (!TYPE_SIZE (class)) |
7559 | safe_layout_class (class); | |
7560 | size = size_in_bytes (class); | |
7561 | new = build (CALL_EXPR, promote_type (class), | |
7562 | build_address_of (alloc_object_node), | |
7563 | tree_cons (NULL_TREE, build_class_ref (class), | |
7564 | build_tree_list (NULL_TREE, | |
7565 | size_in_bytes (class))), | |
7566 | NULL_TREE); | |
7567 | saved_new = save_expr (new); | |
7568 | c1 = build_tree_list (NULL_TREE, saved_new); | |
7569 | TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1); | |
7570 | TREE_OPERAND (original_call, 1) = c1; | |
7571 | TREE_SET_CODE (original_call, CALL_EXPR); | |
7572 | patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new); | |
7573 | } | |
7574 | return patch; | |
7575 | } | |
7576 | ||
7577 | static int | |
7578 | invocation_mode (method, super) | |
7579 | tree method; | |
7580 | int super; | |
7581 | { | |
7582 | int access = get_access_flags_from_decl (method); | |
7583 | ||
22eed1e6 APB |
7584 | if (super) |
7585 | return INVOKE_SUPER; | |
7586 | ||
82371d41 | 7587 | if (access & ACC_STATIC || access & ACC_FINAL || access & ACC_PRIVATE) |
e04a16fb AG |
7588 | return INVOKE_STATIC; |
7589 | ||
7590 | if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method)))) | |
7591 | return INVOKE_STATIC; | |
7592 | ||
e04a16fb AG |
7593 | if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))) |
7594 | return INVOKE_INTERFACE; | |
7595 | ||
7596 | if (DECL_CONSTRUCTOR_P (method)) | |
7597 | return INVOKE_STATIC; | |
22eed1e6 | 7598 | |
e04a16fb AG |
7599 | return INVOKE_VIRTUAL; |
7600 | } | |
7601 | ||
b67d701b PB |
7602 | /* Retrieve a refined list of matching methods. It covers the step |
7603 | 15.11.2 (Compile-Time Step 2) */ | |
e04a16fb AG |
7604 | |
7605 | static tree | |
7606 | lookup_method_invoke (lc, cl, class, name, arg_list) | |
7607 | int lc; | |
7608 | tree cl; | |
7609 | tree class, name, arg_list; | |
7610 | { | |
de4c7b02 | 7611 | tree atl = end_params_node; /* Arg Type List */ |
c877974e | 7612 | tree method, signature, list, node; |
49f48c71 | 7613 | const char *candidates; /* Used for error report */ |
b5b8a0e7 | 7614 | char *dup; |
e04a16fb | 7615 | |
5e942c50 | 7616 | /* Fix the arguments */ |
e04a16fb AG |
7617 | for (node = arg_list; node; node = TREE_CHAIN (node)) |
7618 | { | |
e3884b71 | 7619 | tree current_arg = TREE_TYPE (TREE_VALUE (node)); |
c877974e | 7620 | /* Non primitive type may have to be resolved */ |
e3884b71 | 7621 | if (!JPRIMITIVE_TYPE_P (current_arg)) |
c877974e APB |
7622 | resolve_and_layout (current_arg, NULL_TREE); |
7623 | /* And promoted */ | |
b67d701b | 7624 | if (TREE_CODE (current_arg) == RECORD_TYPE) |
c877974e | 7625 | current_arg = promote_type (current_arg); |
5e942c50 | 7626 | atl = tree_cons (NULL_TREE, current_arg, atl); |
e04a16fb | 7627 | } |
e04a16fb | 7628 | |
5e942c50 APB |
7629 | /* Find all candidates and then refine the list, searching for the |
7630 | most specific method. */ | |
7631 | list = find_applicable_accessible_methods_list (lc, class, name, atl); | |
7632 | list = find_most_specific_methods_list (list); | |
b67d701b PB |
7633 | if (list && !TREE_CHAIN (list)) |
7634 | return TREE_VALUE (list); | |
e04a16fb | 7635 | |
b67d701b PB |
7636 | /* Issue an error. List candidates if any. Candidates are listed |
7637 | only if accessible (non accessible methods may end-up here for | |
7638 | the sake of a better error report). */ | |
7639 | candidates = NULL; | |
7640 | if (list) | |
e04a16fb | 7641 | { |
e04a16fb | 7642 | tree current; |
b67d701b | 7643 | obstack_grow (&temporary_obstack, ". Candidates are:\n", 18); |
e04a16fb AG |
7644 | for (current = list; current; current = TREE_CHAIN (current)) |
7645 | { | |
b67d701b PB |
7646 | tree cm = TREE_VALUE (current); |
7647 | char string [4096]; | |
7648 | if (!cm || not_accessible_p (class, cm, 0)) | |
7649 | continue; | |
b67d701b | 7650 | sprintf |
22eed1e6 APB |
7651 | (string, " `%s' in `%s'%s", |
7652 | get_printable_method_name (cm), | |
b67d701b PB |
7653 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))), |
7654 | (TREE_CHAIN (current) ? "\n" : "")); | |
7655 | obstack_grow (&temporary_obstack, string, strlen (string)); | |
7656 | } | |
7657 | obstack_1grow (&temporary_obstack, '\0'); | |
7658 | candidates = obstack_finish (&temporary_obstack); | |
7659 | } | |
7660 | /* Issue the error message */ | |
c877974e APB |
7661 | method = make_node (FUNCTION_TYPE); |
7662 | TYPE_ARG_TYPES (method) = atl; | |
b67d701b | 7663 | signature = build_java_argument_signature (method); |
c63b98cd | 7664 | dup = xstrdup (lang_printable_name (class, 0)); |
b5b8a0e7 | 7665 | parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s", |
22eed1e6 | 7666 | (lc ? "constructor" : "method"), |
b5b8a0e7 APB |
7667 | (lc ? dup : IDENTIFIER_POINTER (name)), |
7668 | IDENTIFIER_POINTER (signature), dup, | |
b67d701b | 7669 | (candidates ? candidates : "")); |
b5b8a0e7 | 7670 | free (dup); |
b67d701b PB |
7671 | return NULL_TREE; |
7672 | } | |
7673 | ||
5e942c50 APB |
7674 | /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1 |
7675 | when we're looking for a constructor. */ | |
b67d701b PB |
7676 | |
7677 | static tree | |
5e942c50 APB |
7678 | find_applicable_accessible_methods_list (lc, class, name, arglist) |
7679 | int lc; | |
b67d701b PB |
7680 | tree class, name, arglist; |
7681 | { | |
b67d701b PB |
7682 | tree list = NULL_TREE, all_list = NULL_TREE; |
7683 | ||
1982388a | 7684 | /* Search interfaces */ |
f4e16489 APB |
7685 | if (CLASS_INTERFACE (TYPE_NAME (class)) |
7686 | || CLASS_ABSTRACT (TYPE_NAME (class))) | |
b67d701b | 7687 | { |
de0b553f APB |
7688 | static tree searched_interfaces = NULL_TREE; |
7689 | static int search_not_done = 0; | |
1982388a APB |
7690 | int i, n; |
7691 | tree basetype_vec = TYPE_BINFO_BASETYPES (class); | |
7692 | ||
de0b553f APB |
7693 | /* Have we searched this interface already? */ |
7694 | if (searched_interfaces) | |
7695 | { | |
7696 | tree current; | |
7697 | for (current = searched_interfaces; | |
7698 | current; current = TREE_CHAIN (current)) | |
7699 | if (TREE_VALUE (current) == class) | |
7700 | return NULL; | |
7701 | } | |
7702 | searched_interfaces = tree_cons (NULL_TREE, class, searched_interfaces); | |
7703 | ||
1982388a APB |
7704 | search_applicable_methods_list |
7705 | (lc, TYPE_METHODS (class), name, arglist, &list, &all_list); | |
7706 | ||
7707 | n = TREE_VEC_LENGTH (basetype_vec); | |
7708 | for (i = 0; i < n; i++) | |
b67d701b | 7709 | { |
de0b553f APB |
7710 | tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)); |
7711 | tree rlist; | |
7712 | ||
7713 | /* Skip java.lang.Object (we'll search it once later.) */ | |
7714 | if (t == object_type_node) | |
7715 | continue; | |
7716 | ||
7717 | search_not_done++; | |
7718 | rlist = find_applicable_accessible_methods_list (lc, t, name, | |
7719 | arglist); | |
1982388a | 7720 | all_list = chainon (rlist, (list ? list : all_list)); |
de0b553f APB |
7721 | search_not_done--; |
7722 | } | |
7723 | ||
7724 | /* We're done. Reset the searched interfaces list and finally search | |
7725 | java.lang.Object */ | |
7726 | if (!search_not_done) | |
7727 | { | |
7728 | searched_interfaces = NULL_TREE; | |
7729 | search_applicable_methods_list (lc, TYPE_METHODS (object_type_node), | |
7730 | name, arglist, &list, &all_list); | |
e04a16fb | 7731 | } |
e04a16fb | 7732 | } |
1982388a APB |
7733 | /* Search classes */ |
7734 | else | |
7735 | while (class != NULL_TREE) | |
7736 | { | |
7737 | search_applicable_methods_list | |
7738 | (lc, TYPE_METHODS (class), name, arglist, &list, &all_list); | |
7739 | class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class)); | |
7740 | } | |
7741 | ||
b67d701b PB |
7742 | /* Either return the list obtained or all selected (but |
7743 | inaccessible) methods for better error report. */ | |
7744 | return (!list ? all_list : list); | |
7745 | } | |
e04a16fb | 7746 | |
1982388a APB |
7747 | /* Effectively search for the approriate method in method */ |
7748 | ||
7749 | static void | |
7750 | search_applicable_methods_list(lc, method, name, arglist, list, all_list) | |
7751 | int lc; | |
7752 | tree method, name, arglist; | |
7753 | tree *list, *all_list; | |
7754 | { | |
7755 | for (; method; method = TREE_CHAIN (method)) | |
7756 | { | |
7757 | /* When dealing with constructor, stop here, otherwise search | |
7758 | other classes */ | |
7759 | if (lc && !DECL_CONSTRUCTOR_P (method)) | |
7760 | continue; | |
7761 | else if (!lc && (DECL_CONSTRUCTOR_P (method) | |
7762 | || (GET_METHOD_NAME (method) != name))) | |
7763 | continue; | |
7764 | ||
7765 | if (argument_types_convertible (method, arglist)) | |
7766 | { | |
7767 | /* Retain accessible methods only */ | |
7768 | if (!not_accessible_p (DECL_CONTEXT (current_function_decl), | |
7769 | method, 0)) | |
7770 | *list = tree_cons (NULL_TREE, method, *list); | |
7771 | else | |
7772 | /* Also retain all selected method here */ | |
7773 | *all_list = tree_cons (NULL_TREE, method, *list); | |
7774 | } | |
7775 | } | |
7776 | } | |
7777 | ||
b67d701b PB |
7778 | /* 15.11.2.2 Choose the Most Specific Method */ |
7779 | ||
7780 | static tree | |
7781 | find_most_specific_methods_list (list) | |
7782 | tree list; | |
7783 | { | |
7784 | int max = 0; | |
7785 | tree current, new_list = NULL_TREE; | |
7786 | for (current = list; current; current = TREE_CHAIN (current)) | |
e04a16fb | 7787 | { |
b67d701b PB |
7788 | tree method; |
7789 | DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0; | |
7790 | ||
7791 | for (method = list; method; method = TREE_CHAIN (method)) | |
7792 | { | |
7793 | /* Don't test a method against itself */ | |
7794 | if (method == current) | |
7795 | continue; | |
7796 | ||
7797 | /* Compare arguments and location where method where declared */ | |
7798 | if (argument_types_convertible (TREE_VALUE (method), | |
7799 | TREE_VALUE (current)) | |
7800 | && valid_method_invocation_conversion_p | |
7801 | (DECL_CONTEXT (TREE_VALUE (method)), | |
7802 | DECL_CONTEXT (TREE_VALUE (current)))) | |
7803 | { | |
7804 | int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current)); | |
7805 | max = (v > max ? v : max); | |
7806 | } | |
7807 | } | |
e04a16fb AG |
7808 | } |
7809 | ||
b67d701b PB |
7810 | /* Review the list and select the maximally specific methods */ |
7811 | for (current = list; current; current = TREE_CHAIN (current)) | |
7812 | if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max) | |
7813 | new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list); | |
7814 | ||
7815 | /* If we can't find one, lower expectations and try to gather multiple | |
7816 | maximally specific methods */ | |
7817 | while (!new_list) | |
7818 | { | |
7819 | while (--max > 0) | |
7820 | { | |
7821 | if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max) | |
7822 | new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list); | |
7823 | } | |
7824 | return new_list; | |
7825 | } | |
7826 | ||
7827 | return new_list; | |
e04a16fb AG |
7828 | } |
7829 | ||
b67d701b PB |
7830 | /* Make sure that the type of each M2_OR_ARGLIST arguments can be |
7831 | converted by method invocation conversion (5.3) to the type of the | |
7832 | corresponding parameter of M1. Implementation expects M2_OR_ARGLIST | |
7833 | to change less often than M1. */ | |
e04a16fb | 7834 | |
b67d701b PB |
7835 | static int |
7836 | argument_types_convertible (m1, m2_or_arglist) | |
7837 | tree m1, m2_or_arglist; | |
e04a16fb | 7838 | { |
b67d701b PB |
7839 | static tree m2_arg_value = NULL_TREE; |
7840 | static tree m2_arg_cache = NULL_TREE; | |
e04a16fb | 7841 | |
b67d701b | 7842 | register tree m1_arg, m2_arg; |
e04a16fb | 7843 | |
b67d701b PB |
7844 | m1_arg = TYPE_ARG_TYPES (TREE_TYPE (m1)); |
7845 | if (!METHOD_STATIC (m1)) | |
7846 | m1_arg = TREE_CHAIN (m1_arg); | |
e04a16fb | 7847 | |
b67d701b PB |
7848 | if (m2_arg_value == m2_or_arglist) |
7849 | m2_arg = m2_arg_cache; | |
7850 | else | |
7851 | { | |
7852 | /* M2_OR_ARGLIST can be a function DECL or a raw list of | |
7853 | argument types */ | |
7854 | if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL) | |
7855 | { | |
7856 | m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist)); | |
7857 | if (!METHOD_STATIC (m2_or_arglist)) | |
7858 | m2_arg = TREE_CHAIN (m2_arg); | |
7859 | } | |
7860 | else | |
7861 | m2_arg = m2_or_arglist; | |
e04a16fb | 7862 | |
b67d701b PB |
7863 | m2_arg_value = m2_or_arglist; |
7864 | m2_arg_cache = m2_arg; | |
7865 | } | |
e04a16fb | 7866 | |
de4c7b02 | 7867 | while (m1_arg != end_params_node && m2_arg != end_params_node) |
b67d701b | 7868 | { |
c877974e | 7869 | resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE); |
b67d701b PB |
7870 | if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg), |
7871 | TREE_VALUE (m2_arg))) | |
7872 | break; | |
7873 | m1_arg = TREE_CHAIN (m1_arg); | |
7874 | m2_arg = TREE_CHAIN (m2_arg); | |
e04a16fb | 7875 | } |
de4c7b02 | 7876 | return m1_arg == end_params_node && m2_arg == end_params_node; |
e04a16fb AG |
7877 | } |
7878 | ||
7879 | /* Qualification routines */ | |
7880 | ||
7881 | static void | |
7882 | qualify_ambiguous_name (id) | |
7883 | tree id; | |
7884 | { | |
cd531a2e KG |
7885 | tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE, |
7886 | saved_current_class; | |
d8fccff5 | 7887 | int again, super_found = 0, this_found = 0, new_array_found = 0; |
8576f094 | 7888 | int code; |
e04a16fb AG |
7889 | |
7890 | /* We first qualify the first element, then derive qualification of | |
7891 | others based on the first one. If the first element is qualified | |
7892 | by a resolution (field or type), this resolution is stored in the | |
7893 | QUAL_RESOLUTION of the qual element being examined. We need to | |
7894 | save the current_class since the use of SUPER might change the | |
7895 | its value. */ | |
7896 | saved_current_class = current_class; | |
7897 | qual = EXPR_WFL_QUALIFICATION (id); | |
7898 | do { | |
7899 | ||
7900 | /* Simple qualified expression feature a qual_wfl that is a | |
7901 | WFL. Expression derived from a primary feature more complicated | |
7902 | things like a CALL_EXPR. Expression from primary need to be | |
7903 | worked out to extract the part on which the qualification will | |
7904 | take place. */ | |
7905 | qual_wfl = QUAL_WFL (qual); | |
7906 | switch (TREE_CODE (qual_wfl)) | |
7907 | { | |
7908 | case CALL_EXPR: | |
7909 | qual_wfl = TREE_OPERAND (qual_wfl, 0); | |
7910 | if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION) | |
7911 | { | |
7912 | qual = EXPR_WFL_QUALIFICATION (qual_wfl); | |
7913 | qual_wfl = QUAL_WFL (qual); | |
7914 | } | |
7915 | break; | |
d8fccff5 APB |
7916 | case NEW_ARRAY_EXPR: |
7917 | qual = TREE_CHAIN (qual); | |
1a6d4fb7 | 7918 | again = new_array_found = 1; |
d8fccff5 | 7919 | continue; |
b67d701b | 7920 | case NEW_CLASS_EXPR: |
e04a16fb | 7921 | case CONVERT_EXPR: |
e04a16fb AG |
7922 | qual_wfl = TREE_OPERAND (qual_wfl, 0); |
7923 | break; | |
c583dd46 APB |
7924 | case ARRAY_REF: |
7925 | while (TREE_CODE (qual_wfl) == ARRAY_REF) | |
7926 | qual_wfl = TREE_OPERAND (qual_wfl, 0); | |
7927 | break; | |
8576f094 APB |
7928 | case STRING_CST: |
7929 | qual = TREE_CHAIN (qual); | |
7930 | qual_wfl = QUAL_WFL (qual); | |
7931 | break; | |
0a2138e2 APB |
7932 | default: |
7933 | /* Fix for -Wall. Just break doing nothing */ | |
7934 | break; | |
e04a16fb | 7935 | } |
8576f094 | 7936 | |
e04a16fb AG |
7937 | ptr_type = current_class; |
7938 | again = 0; | |
8576f094 APB |
7939 | code = TREE_CODE (qual_wfl); |
7940 | ||
7941 | /* Pos evaluation: non WFL leading expression nodes */ | |
7942 | if (code == CONVERT_EXPR | |
7943 | && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION) | |
7944 | name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl)); | |
7945 | ||
ac22f9cb | 7946 | else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) && |
8576f094 APB |
7947 | TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION) |
7948 | name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0)); | |
7949 | ||
37feda7d APB |
7950 | else if (code == STRING_CST || code == CONDITIONAL_EXPR |
7951 | || code == PLUS_EXPR) | |
8576f094 APB |
7952 | { |
7953 | qual = TREE_CHAIN (qual); | |
7954 | qual_wfl = QUAL_WFL (qual); | |
7955 | again = 1; | |
7956 | } | |
7957 | else | |
f441f671 APB |
7958 | { |
7959 | name = EXPR_WFL_NODE (qual_wfl); | |
7960 | if (!name) | |
7961 | { | |
7962 | qual = EXPR_WFL_QUALIFICATION (qual_wfl); | |
7963 | again = 1; | |
7964 | } | |
7965 | } | |
7966 | ||
e04a16fb AG |
7967 | /* If we have a THIS (from a primary), we set the context accordingly */ |
7968 | if (name == this_identifier_node) | |
7969 | { | |
7970 | qual = TREE_CHAIN (qual); | |
7971 | qual_wfl = QUAL_WFL (qual); | |
22eed1e6 APB |
7972 | if (TREE_CODE (qual_wfl) == CALL_EXPR) |
7973 | again = 1; | |
7974 | else | |
7975 | name = EXPR_WFL_NODE (qual_wfl); | |
e04a16fb AG |
7976 | this_found = 1; |
7977 | } | |
7978 | /* If we have a SUPER, we set the context accordingly */ | |
7979 | if (name == super_identifier_node) | |
7980 | { | |
7981 | current_class = CLASSTYPE_SUPER (ptr_type); | |
7982 | /* Check that there is such a thing as a super class. If not, | |
7983 | return. The error will be caught later on, during the | |
7984 | resolution */ | |
7985 | if (!current_class) | |
7986 | { | |
7987 | current_class = saved_current_class; | |
7988 | return; | |
7989 | } | |
7990 | qual = TREE_CHAIN (qual); | |
7991 | /* Do one more interation to set things up */ | |
7992 | super_found = again = 1; | |
7993 | } | |
7994 | } while (again); | |
7995 | ||
7996 | /* If name appears within the scope of a location variable | |
7997 | declaration or parameter declaration, then it is an expression | |
7998 | name. We don't carry this test out if we're in the context of the | |
7999 | use of SUPER or THIS */ | |
1a6d4fb7 APB |
8000 | if (!this_found && !super_found && |
8001 | TREE_CODE (name) != STRING_CST && (decl = IDENTIFIER_LOCAL_VALUE (name))) | |
e04a16fb AG |
8002 | { |
8003 | RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1; | |
8004 | QUAL_RESOLUTION (qual) = decl; | |
8005 | } | |
8006 | ||
8007 | /* If within the class/interface NAME was found to be used there | |
8008 | exists a (possibly inherited) field named NAME, then this is an | |
d8fccff5 APB |
8009 | expression name. If we saw a NEW_ARRAY_EXPR before and want to |
8010 | address length, it is OK. */ | |
8011 | else if ((decl = lookup_field_wrapper (ptr_type, name)) | |
8012 | || (new_array_found && name == length_identifier_node)) | |
e04a16fb AG |
8013 | { |
8014 | RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1; | |
d8fccff5 | 8015 | QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl); |
e04a16fb AG |
8016 | } |
8017 | ||
1a6d4fb7 | 8018 | /* We reclassify NAME as yielding to a type name resolution if: |
e04a16fb AG |
8019 | - NAME is a class/interface declared within the compilation |
8020 | unit containing NAME, | |
8021 | - NAME is imported via a single-type-import declaration, | |
8022 | - NAME is declared in an another compilation unit of the package | |
8023 | of the compilation unit containing NAME, | |
8024 | - NAME is declared by exactly on type-import-on-demand declaration | |
1a6d4fb7 APB |
8025 | of the compilation unit containing NAME. |
8026 | - NAME is actually a STRING_CST. */ | |
8027 | else if (TREE_CODE (name) == STRING_CST || | |
8028 | (decl = resolve_and_layout (name, NULL_TREE))) | |
e04a16fb AG |
8029 | { |
8030 | RESOLVE_TYPE_NAME_P (qual_wfl) = 1; | |
8031 | QUAL_RESOLUTION (qual) = decl; | |
8032 | } | |
8033 | ||
8034 | /* Method call are expression name */ | |
9bbc7d9f | 8035 | else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR |
8576f094 APB |
8036 | || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF |
8037 | || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR) | |
e04a16fb AG |
8038 | RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1; |
8039 | ||
8040 | /* Check here that NAME isn't declared by more than one | |
8041 | type-import-on-demand declaration of the compilation unit | |
8042 | containing NAME. FIXME */ | |
8043 | ||
8044 | /* Otherwise, NAME is reclassified as a package name */ | |
8045 | else | |
8046 | RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1; | |
8047 | ||
8048 | /* Propagate the qualification accross other components of the | |
8049 | qualified name */ | |
8050 | for (qual = TREE_CHAIN (qual); qual; | |
8051 | qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual)) | |
8052 | { | |
8053 | if (RESOLVE_PACKAGE_NAME_P (qual_wfl)) | |
8054 | RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1; | |
8055 | else | |
8056 | RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1; | |
8057 | } | |
8058 | ||
8059 | /* Store the global qualification for the ambiguous part of ID back | |
8060 | into ID fields */ | |
8061 | if (RESOLVE_EXPRESSION_NAME_P (qual_wfl)) | |
8062 | RESOLVE_EXPRESSION_NAME_P (id) = 1; | |
8063 | else if (RESOLVE_TYPE_NAME_P (qual_wfl)) | |
8064 | RESOLVE_TYPE_NAME_P (id) = 1; | |
8065 | else if (RESOLVE_PACKAGE_NAME_P (qual_wfl)) | |
8066 | RESOLVE_PACKAGE_NAME_P (id) = 1; | |
8067 | ||
8068 | /* Restore the current class */ | |
8069 | current_class = saved_current_class; | |
8070 | } | |
8071 | ||
8072 | static int | |
8073 | breakdown_qualified (left, right, source) | |
8074 | tree *left, *right, source; | |
8075 | { | |
8076 | char *p = IDENTIFIER_POINTER (source), *base; | |
8077 | int l = IDENTIFIER_LENGTH (source); | |
8078 | ||
8079 | /* Breakdown NAME into REMAINDER . IDENTIFIER */ | |
8080 | base = p; | |
8081 | p += (l-1); | |
8082 | while (*p != '.' && p != base) | |
8083 | p--; | |
8084 | ||
8085 | /* We didn't find a '.'. Return an error */ | |
8086 | if (p == base) | |
8087 | return 1; | |
8088 | ||
8089 | *p = '\0'; | |
8090 | if (right) | |
8091 | *right = get_identifier (p+1); | |
8092 | *left = get_identifier (IDENTIFIER_POINTER (source)); | |
8093 | *p = '.'; | |
8094 | ||
8095 | return 0; | |
8096 | } | |
8097 | ||
e04a16fb | 8098 | /* Patch tree nodes in a function body. When a BLOCK is found, push |
5b09b33e PB |
8099 | local variable decls if present. |
8100 | Same as java_complete_lhs, but does resolve static finals to values. */ | |
e04a16fb AG |
8101 | |
8102 | static tree | |
8103 | java_complete_tree (node) | |
8104 | tree node; | |
5b09b33e PB |
8105 | { |
8106 | node = java_complete_lhs (node); | |
8107 | if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node) | |
7f10c2e2 APB |
8108 | && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE |
8109 | && !flag_emit_xref) | |
5b09b33e PB |
8110 | { |
8111 | tree value = DECL_INITIAL (node); | |
8112 | DECL_INITIAL (node) = NULL_TREE; | |
100f7cd8 | 8113 | push_obstacks (&permanent_obstack, &permanent_obstack); |
5b09b33e | 8114 | value = fold_constant_for_init (value, node); |
100f7cd8 | 8115 | pop_obstacks (); |
5b09b33e PB |
8116 | DECL_INITIAL (node) = value; |
8117 | if (value != NULL_TREE) | |
8118 | return value; | |
8119 | } | |
8120 | return node; | |
8121 | } | |
8122 | ||
2aa11e97 APB |
8123 | static tree |
8124 | java_stabilize_reference (node) | |
8125 | tree node; | |
8126 | { | |
8127 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
8128 | { | |
8129 | tree op0 = TREE_OPERAND (node, 0); | |
8130 | tree op1 = TREE_OPERAND (node, 1); | |
642f15d1 | 8131 | TREE_OPERAND (node, 0) = save_expr (op0); |
2aa11e97 APB |
8132 | TREE_OPERAND (node, 1) = java_stabilize_reference (op1); |
8133 | return node; | |
8134 | } | |
5cbdba64 | 8135 | return stabilize_reference (node); |
2aa11e97 APB |
8136 | } |
8137 | ||
5b09b33e PB |
8138 | /* Patch tree nodes in a function body. When a BLOCK is found, push |
8139 | local variable decls if present. | |
8140 | Same as java_complete_tree, but does not resolve static finals to values. */ | |
8141 | ||
8142 | static tree | |
8143 | java_complete_lhs (node) | |
8144 | tree node; | |
e04a16fb | 8145 | { |
22eed1e6 | 8146 | tree nn, cn, wfl_op1, wfl_op2, wfl_op3; |
b67d701b | 8147 | int flag; |
e04a16fb AG |
8148 | |
8149 | /* CONVERT_EXPR always has its type set, even though it needs to be | |
b67d701b | 8150 | worked out. */ |
e04a16fb AG |
8151 | if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR) |
8152 | return node; | |
8153 | ||
8154 | /* The switch block implements cases processing container nodes | |
8155 | first. Contained nodes are always written back. Leaves come | |
8156 | next and return a value. */ | |
8157 | switch (TREE_CODE (node)) | |
8158 | { | |
8159 | case BLOCK: | |
8160 | ||
8161 | /* 1- Block section. | |
8162 | Set the local values on decl names so we can identify them | |
8163 | faster when they're referenced. At that stage, identifiers | |
8164 | are legal so we don't check for declaration errors. */ | |
8165 | for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn)) | |
8166 | { | |
8167 | DECL_CONTEXT (cn) = current_function_decl; | |
8168 | IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn; | |
e04a16fb | 8169 | } |
15fdcfe9 PB |
8170 | if (BLOCK_EXPR_BODY (node) == NULL_TREE) |
8171 | CAN_COMPLETE_NORMALLY (node) = 1; | |
8172 | else | |
e04a16fb | 8173 | { |
15fdcfe9 PB |
8174 | tree stmt = BLOCK_EXPR_BODY (node); |
8175 | tree *ptr; | |
8176 | int error_seen = 0; | |
8177 | if (TREE_CODE (stmt) == COMPOUND_EXPR) | |
8178 | { | |
c877974e APB |
8179 | /* Re-order from (((A; B); C); ...; Z) to |
8180 | (A; (B; (C ; (...; Z)))). | |
15fdcfe9 PB |
8181 | This makes it easier to scan the statements left-to-right |
8182 | without using recursion (which might overflow the stack | |
8183 | if the block has many statements. */ | |
8184 | for (;;) | |
8185 | { | |
8186 | tree left = TREE_OPERAND (stmt, 0); | |
8187 | if (TREE_CODE (left) != COMPOUND_EXPR) | |
8188 | break; | |
8189 | TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1); | |
8190 | TREE_OPERAND (left, 1) = stmt; | |
8191 | stmt = left; | |
8192 | } | |
8193 | BLOCK_EXPR_BODY (node) = stmt; | |
8194 | } | |
8195 | ||
c877974e APB |
8196 | /* Now do the actual complete, without deep recursion for |
8197 | long blocks. */ | |
15fdcfe9 | 8198 | ptr = &BLOCK_EXPR_BODY (node); |
dc0b3eff PB |
8199 | while (TREE_CODE (*ptr) == COMPOUND_EXPR |
8200 | && TREE_OPERAND (*ptr, 1) != empty_stmt_node) | |
15fdcfe9 PB |
8201 | { |
8202 | tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0)); | |
8203 | tree *next = &TREE_OPERAND (*ptr, 1); | |
8204 | TREE_OPERAND (*ptr, 0) = cur; | |
cd9643f7 PB |
8205 | if (cur == empty_stmt_node) |
8206 | { | |
8207 | /* Optimization; makes it easier to detect empty bodies. | |
8208 | Most useful for <clinit> with all-constant initializer. */ | |
8209 | *ptr = *next; | |
8210 | continue; | |
8211 | } | |
15fdcfe9 PB |
8212 | if (TREE_CODE (cur) == ERROR_MARK) |
8213 | error_seen++; | |
8214 | else if (! CAN_COMPLETE_NORMALLY (cur)) | |
8215 | { | |
8216 | wfl_op2 = *next; | |
8217 | for (;;) | |
8218 | { | |
8219 | if (TREE_CODE (wfl_op2) == BLOCK) | |
8220 | wfl_op2 = BLOCK_EXPR_BODY (wfl_op2); | |
8221 | else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR) | |
8222 | wfl_op2 = TREE_OPERAND (wfl_op2, 0); | |
8223 | else | |
8224 | break; | |
8225 | } | |
8226 | if (TREE_CODE (wfl_op2) != CASE_EXPR | |
dc0b3eff | 8227 | && TREE_CODE (wfl_op2) != DEFAULT_EXPR) |
82371d41 | 8228 | unreachable_stmt_error (*ptr); |
15fdcfe9 PB |
8229 | } |
8230 | ptr = next; | |
8231 | } | |
8232 | *ptr = java_complete_tree (*ptr); | |
8233 | ||
8234 | if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0) | |
e04a16fb | 8235 | return error_mark_node; |
15fdcfe9 | 8236 | CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr); |
e04a16fb AG |
8237 | } |
8238 | /* Turn local bindings to null */ | |
8239 | for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn)) | |
8240 | IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE; | |
8241 | ||
8242 | TREE_TYPE (node) = void_type_node; | |
8243 | break; | |
8244 | ||
8245 | /* 2- They are expressions but ultimately deal with statements */ | |
b67d701b | 8246 | |
b9f7e36c APB |
8247 | case THROW_EXPR: |
8248 | wfl_op1 = TREE_OPERAND (node, 0); | |
8249 | COMPLETE_CHECK_OP_0 (node); | |
15fdcfe9 | 8250 | /* CAN_COMPLETE_NORMALLY (node) = 0; */ |
b9f7e36c APB |
8251 | return patch_throw_statement (node, wfl_op1); |
8252 | ||
8253 | case SYNCHRONIZED_EXPR: | |
8254 | wfl_op1 = TREE_OPERAND (node, 0); | |
b9f7e36c APB |
8255 | return patch_synchronized_statement (node, wfl_op1); |
8256 | ||
b67d701b PB |
8257 | case TRY_EXPR: |
8258 | return patch_try_statement (node); | |
8259 | ||
a7d8d81f PB |
8260 | case TRY_FINALLY_EXPR: |
8261 | COMPLETE_CHECK_OP_0 (node); | |
8262 | COMPLETE_CHECK_OP_1 (node); | |
8263 | CAN_COMPLETE_NORMALLY (node) | |
8264 | = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0)) | |
8265 | && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))); | |
8266 | TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0)); | |
8267 | return node; | |
8268 | ||
5a005d9e PB |
8269 | case CLEANUP_POINT_EXPR: |
8270 | COMPLETE_CHECK_OP_0 (node); | |
8271 | TREE_TYPE (node) = void_type_node; | |
2aa11e97 APB |
8272 | CAN_COMPLETE_NORMALLY (node) = |
8273 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0)); | |
5a005d9e PB |
8274 | return node; |
8275 | ||
8276 | case WITH_CLEANUP_EXPR: | |
8277 | COMPLETE_CHECK_OP_0 (node); | |
8278 | COMPLETE_CHECK_OP_2 (node); | |
2aa11e97 APB |
8279 | CAN_COMPLETE_NORMALLY (node) = |
8280 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0)); | |
5a005d9e PB |
8281 | TREE_TYPE (node) = void_type_node; |
8282 | return node; | |
8283 | ||
e04a16fb AG |
8284 | case LABELED_BLOCK_EXPR: |
8285 | PUSH_LABELED_BLOCK (node); | |
8286 | if (LABELED_BLOCK_BODY (node)) | |
8287 | COMPLETE_CHECK_OP_1 (node); | |
8288 | TREE_TYPE (node) = void_type_node; | |
8289 | POP_LABELED_BLOCK (); | |
1fb89a4d APB |
8290 | |
8291 | if (LABELED_BLOCK_BODY (node) == empty_stmt_node) | |
9dd939b2 APB |
8292 | { |
8293 | LABELED_BLOCK_BODY (node) = NULL_TREE; | |
8294 | CAN_COMPLETE_NORMALLY (node) = 1; | |
8295 | } | |
1fb89a4d | 8296 | else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node))) |
15fdcfe9 | 8297 | CAN_COMPLETE_NORMALLY (node) = 1; |
e04a16fb AG |
8298 | return node; |
8299 | ||
8300 | case EXIT_BLOCK_EXPR: | |
8301 | /* We don't complete operand 1, because it's the return value of | |
8302 | the EXIT_BLOCK_EXPR which doesn't exist it Java */ | |
8303 | return patch_bc_statement (node); | |
8304 | ||
15fdcfe9 PB |
8305 | case CASE_EXPR: |
8306 | cn = java_complete_tree (TREE_OPERAND (node, 0)); | |
8307 | if (cn == error_mark_node) | |
8308 | return cn; | |
8309 | ||
8576f094 APB |
8310 | /* First, the case expression must be constant. Values of final |
8311 | fields are accepted. */ | |
15fdcfe9 | 8312 | cn = fold (cn); |
8576f094 APB |
8313 | if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF) |
8314 | && JDECL_P (TREE_OPERAND (cn, 1)) | |
8315 | && FIELD_FINAL (TREE_OPERAND (cn, 1)) | |
8316 | && DECL_INITIAL (TREE_OPERAND (cn, 1))) | |
100f7cd8 APB |
8317 | { |
8318 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
8319 | cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)), | |
8320 | TREE_OPERAND (cn, 1)); | |
8321 | pop_obstacks (); | |
8322 | } | |
15fdcfe9 | 8323 | |
ce6e9147 | 8324 | if (!TREE_CONSTANT (cn) && !flag_emit_xref) |
15fdcfe9 PB |
8325 | { |
8326 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
8327 | parse_error_context (node, "Constant expression required"); | |
8328 | return error_mark_node; | |
8329 | } | |
8330 | ||
8331 | nn = ctxp->current_loop; | |
8332 | ||
8333 | /* It must be assignable to the type of the switch expression. */ | |
c877974e APB |
8334 | if (!try_builtin_assignconv (NULL_TREE, |
8335 | TREE_TYPE (TREE_OPERAND (nn, 0)), cn)) | |
15fdcfe9 PB |
8336 | { |
8337 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
8338 | parse_error_context | |
8339 | (wfl_operator, | |
8340 | "Incompatible type for case. Can't convert `%s' to `int'", | |
8341 | lang_printable_name (TREE_TYPE (cn), 0)); | |
8342 | return error_mark_node; | |
8343 | } | |
8344 | ||
8345 | cn = fold (convert (int_type_node, cn)); | |
8346 | ||
8347 | /* Multiple instance of a case label bearing the same | |
8348 | value is checked during code generation. The case | |
8349 | expression is allright so far. */ | |
8350 | TREE_OPERAND (node, 0) = cn; | |
9bbc7d9f | 8351 | TREE_TYPE (node) = void_type_node; |
15fdcfe9 | 8352 | CAN_COMPLETE_NORMALLY (node) = 1; |
10100cc7 | 8353 | TREE_SIDE_EFFECTS (node) = 1; |
15fdcfe9 PB |
8354 | break; |
8355 | ||
8356 | case DEFAULT_EXPR: | |
8357 | nn = ctxp->current_loop; | |
8358 | /* Only one default label is allowed per switch statement */ | |
8359 | if (SWITCH_HAS_DEFAULT (nn)) | |
8360 | { | |
8361 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
8362 | parse_error_context (wfl_operator, | |
8363 | "Duplicate case label: `default'"); | |
8364 | return error_mark_node; | |
8365 | } | |
8366 | else | |
8367 | SWITCH_HAS_DEFAULT (nn) = 1; | |
9bbc7d9f | 8368 | TREE_TYPE (node) = void_type_node; |
10100cc7 | 8369 | TREE_SIDE_EFFECTS (node) = 1; |
15fdcfe9 PB |
8370 | CAN_COMPLETE_NORMALLY (node) = 1; |
8371 | break; | |
8372 | ||
b67d701b | 8373 | case SWITCH_EXPR: |
e04a16fb AG |
8374 | case LOOP_EXPR: |
8375 | PUSH_LOOP (node); | |
8376 | /* Check whether the loop was enclosed in a labeled | |
8377 | statement. If not, create one, insert the loop in it and | |
8378 | return the node */ | |
8379 | nn = patch_loop_statement (node); | |
b67d701b | 8380 | |
e04a16fb | 8381 | /* Anyways, walk the body of the loop */ |
b67d701b PB |
8382 | if (TREE_CODE (node) == LOOP_EXPR) |
8383 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); | |
8384 | /* Switch statement: walk the switch expression and the cases */ | |
8385 | else | |
8386 | node = patch_switch_statement (node); | |
8387 | ||
e04a16fb | 8388 | if (TREE_OPERAND (node, 0) == error_mark_node) |
b635eb2f PB |
8389 | nn = error_mark_node; |
8390 | else | |
15fdcfe9 | 8391 | { |
b635eb2f PB |
8392 | TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node; |
8393 | /* If we returned something different, that's because we | |
8394 | inserted a label. Pop the label too. */ | |
8395 | if (nn != node) | |
8396 | { | |
8397 | if (CAN_COMPLETE_NORMALLY (node)) | |
8398 | CAN_COMPLETE_NORMALLY (nn) = 1; | |
8399 | POP_LABELED_BLOCK (); | |
8400 | } | |
15fdcfe9 | 8401 | } |
e04a16fb AG |
8402 | POP_LOOP (); |
8403 | return nn; | |
8404 | ||
8405 | case EXIT_EXPR: | |
8406 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); | |
8407 | return patch_exit_expr (node); | |
8408 | ||
8409 | case COND_EXPR: | |
8410 | /* Condition */ | |
8411 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); | |
8412 | if (TREE_OPERAND (node, 0) == error_mark_node) | |
8413 | return error_mark_node; | |
8414 | /* then-else branches */ | |
8415 | TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1)); | |
8416 | if (TREE_OPERAND (node, 1) == error_mark_node) | |
8417 | return error_mark_node; | |
8418 | TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2)); | |
8419 | if (TREE_OPERAND (node, 2) == error_mark_node) | |
8420 | return error_mark_node; | |
8421 | return patch_if_else_statement (node); | |
8422 | break; | |
8423 | ||
22eed1e6 APB |
8424 | case CONDITIONAL_EXPR: |
8425 | /* Condition */ | |
8426 | wfl_op1 = TREE_OPERAND (node, 0); | |
8427 | COMPLETE_CHECK_OP_0 (node); | |
8428 | wfl_op2 = TREE_OPERAND (node, 1); | |
8429 | COMPLETE_CHECK_OP_1 (node); | |
8430 | wfl_op3 = TREE_OPERAND (node, 2); | |
8431 | COMPLETE_CHECK_OP_2 (node); | |
8432 | return patch_conditional_expr (node, wfl_op1, wfl_op2); | |
8433 | ||
e04a16fb AG |
8434 | /* 3- Expression section */ |
8435 | case COMPOUND_EXPR: | |
15fdcfe9 | 8436 | wfl_op2 = TREE_OPERAND (node, 1); |
ac825856 APB |
8437 | TREE_OPERAND (node, 0) = nn = |
8438 | java_complete_tree (TREE_OPERAND (node, 0)); | |
dc0b3eff PB |
8439 | if (wfl_op2 == empty_stmt_node) |
8440 | CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn); | |
8441 | else | |
15fdcfe9 | 8442 | { |
dc0b3eff | 8443 | if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK) |
bccaf73a | 8444 | { |
dc0b3eff PB |
8445 | /* An unreachable condition in a do-while statement |
8446 | is *not* (technically) an unreachable statement. */ | |
8447 | nn = wfl_op2; | |
8448 | if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION) | |
8449 | nn = EXPR_WFL_NODE (nn); | |
8450 | if (TREE_CODE (nn) != EXIT_EXPR) | |
8451 | { | |
8452 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op2); | |
8453 | parse_error_context (wfl_operator, "Unreachable statement"); | |
8454 | } | |
bccaf73a | 8455 | } |
dc0b3eff PB |
8456 | TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1)); |
8457 | if (TREE_OPERAND (node, 1) == error_mark_node) | |
8458 | return error_mark_node; | |
8459 | CAN_COMPLETE_NORMALLY (node) | |
8460 | = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)); | |
15fdcfe9 | 8461 | } |
e04a16fb AG |
8462 | TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1)); |
8463 | break; | |
8464 | ||
8465 | case RETURN_EXPR: | |
15fdcfe9 | 8466 | /* CAN_COMPLETE_NORMALLY (node) = 0; */ |
e04a16fb AG |
8467 | return patch_return (node); |
8468 | ||
8469 | case EXPR_WITH_FILE_LOCATION: | |
8470 | if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */ | |
8471 | || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE) | |
15fdcfe9 | 8472 | { |
5423609c | 8473 | tree wfl = node; |
15fdcfe9 | 8474 | node = resolve_expression_name (node, NULL); |
dc0b3eff PB |
8475 | if (node == error_mark_node) |
8476 | return node; | |
5423609c APB |
8477 | /* Keep line number information somewhere were it doesn't |
8478 | disrupt the completion process. */ | |
2c56429a | 8479 | if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR) |
5423609c APB |
8480 | { |
8481 | EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1); | |
8482 | TREE_OPERAND (node, 1) = wfl; | |
8483 | } | |
15fdcfe9 PB |
8484 | CAN_COMPLETE_NORMALLY (node) = 1; |
8485 | } | |
e04a16fb AG |
8486 | else |
8487 | { | |
5b09b33e PB |
8488 | tree body; |
8489 | int save_lineno = lineno; | |
8490 | lineno = EXPR_WFL_LINENO (node); | |
8491 | body = java_complete_tree (EXPR_WFL_NODE (node)); | |
8492 | lineno = save_lineno; | |
15fdcfe9 | 8493 | EXPR_WFL_NODE (node) = body; |
dc0b3eff | 8494 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body); |
15fdcfe9 | 8495 | CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body); |
cd9643f7 PB |
8496 | if (body == empty_stmt_node) |
8497 | { | |
8498 | /* Optimization; makes it easier to detect empty bodies. */ | |
8499 | return body; | |
8500 | } | |
dc0b3eff | 8501 | if (body == error_mark_node) |
e04a16fb AG |
8502 | { |
8503 | /* Its important for the evaluation of assignment that | |
8504 | this mark on the TREE_TYPE is propagated. */ | |
8505 | TREE_TYPE (node) = error_mark_node; | |
8506 | return error_mark_node; | |
8507 | } | |
8508 | else | |
8509 | TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node)); | |
15fdcfe9 | 8510 | |
e04a16fb AG |
8511 | } |
8512 | break; | |
8513 | ||
b67d701b | 8514 | case NEW_ARRAY_EXPR: |
e04a16fb AG |
8515 | /* Patch all the dimensions */ |
8516 | flag = 0; | |
8517 | for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn)) | |
8518 | { | |
8519 | int location = EXPR_WFL_LINECOL (TREE_VALUE (cn)); | |
3a1760ac APB |
8520 | tree dim = convert (int_type_node, |
8521 | java_complete_tree (TREE_VALUE (cn))); | |
e04a16fb AG |
8522 | if (dim == error_mark_node) |
8523 | { | |
8524 | flag = 1; | |
8525 | continue; | |
8526 | } | |
8527 | else | |
8528 | { | |
b9f7e36c | 8529 | TREE_VALUE (cn) = dim; |
e04a16fb AG |
8530 | /* Setup the location of the current dimension, for |
8531 | later error report. */ | |
8532 | TREE_PURPOSE (cn) = | |
8533 | build_expr_wfl (NULL_TREE, input_filename, 0, 0); | |
8534 | EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location; | |
8535 | } | |
8536 | } | |
8537 | /* They complete the array creation expression, if no errors | |
8538 | were found. */ | |
15fdcfe9 | 8539 | CAN_COMPLETE_NORMALLY (node) = 1; |
aee48ef8 PB |
8540 | return (flag ? error_mark_node |
8541 | : force_evaluation_order (patch_newarray (node))); | |
e04a16fb | 8542 | |
b67d701b | 8543 | case NEW_CLASS_EXPR: |
e04a16fb | 8544 | case CALL_EXPR: |
b67d701b | 8545 | /* Complete function's argument(s) first */ |
e04a16fb AG |
8546 | if (complete_function_arguments (node)) |
8547 | return error_mark_node; | |
8548 | else | |
b9f7e36c | 8549 | { |
22eed1e6 APB |
8550 | tree decl, wfl = TREE_OPERAND (node, 0); |
8551 | int in_this = CALL_THIS_CONSTRUCTOR_P (node); | |
8552 | ||
c877974e | 8553 | node = patch_method_invocation (node, NULL_TREE, |
89e09b9a | 8554 | NULL_TREE, 0, &decl); |
c877974e APB |
8555 | if (node == error_mark_node) |
8556 | return error_mark_node; | |
8557 | ||
8558 | check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl); | |
8559 | /* If we call this(...), register signature and positions */ | |
8560 | if (in_this) | |
8561 | DECL_CONSTRUCTOR_CALLS (current_function_decl) = | |
8562 | tree_cons (wfl, decl, | |
8563 | DECL_CONSTRUCTOR_CALLS (current_function_decl)); | |
de4c7b02 | 8564 | CAN_COMPLETE_NORMALLY (node) = 1; |
dc0b3eff | 8565 | return force_evaluation_order (node); |
b9f7e36c | 8566 | } |
e04a16fb AG |
8567 | |
8568 | case MODIFY_EXPR: | |
8569 | /* Save potential wfls */ | |
8570 | wfl_op1 = TREE_OPERAND (node, 0); | |
cd9643f7 PB |
8571 | TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1); |
8572 | if (MODIFY_EXPR_FROM_INITIALIZATION_P (node) | |
8573 | && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn) | |
8574 | && DECL_INITIAL (nn) != NULL_TREE) | |
8575 | { | |
100f7cd8 APB |
8576 | tree value; |
8577 | ||
8578 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
8579 | value = fold_constant_for_init (nn, nn); | |
8580 | pop_obstacks (); | |
cd9643f7 PB |
8581 | if (value != NULL_TREE) |
8582 | { | |
8583 | tree type = TREE_TYPE (value); | |
8584 | if (JPRIMITIVE_TYPE_P (type) || type == string_ptr_type_node) | |
8585 | return empty_stmt_node; | |
8586 | } | |
8587 | DECL_INITIAL (nn) = NULL_TREE; | |
8588 | } | |
e04a16fb | 8589 | wfl_op2 = TREE_OPERAND (node, 1); |
cd9643f7 | 8590 | |
e04a16fb AG |
8591 | if (TREE_OPERAND (node, 0) == error_mark_node) |
8592 | return error_mark_node; | |
8593 | ||
5cbdba64 APB |
8594 | flag = COMPOUND_ASSIGN_P (wfl_op2); |
8595 | if (flag) | |
e04a16fb | 8596 | { |
2aa11e97 | 8597 | tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0)); |
e04a16fb AG |
8598 | |
8599 | /* Hand stablize the lhs on both places */ | |
e04a16fb | 8600 | TREE_OPERAND (node, 0) = lvalue; |
5cbdba64 APB |
8601 | TREE_OPERAND (TREE_OPERAND (node, 1), 0) = |
8602 | (flag_emit_class_files ? lvalue : save_expr (lvalue)); | |
2aa11e97 | 8603 | |
5cbdba64 | 8604 | /* 15.25.2.a: Left hand is not an array access. FIXME */ |
2aa11e97 APB |
8605 | /* Now complete the RHS. We write it back later on. */ |
8606 | nn = java_complete_tree (TREE_OPERAND (node, 1)); | |
8607 | ||
642f15d1 APB |
8608 | if ((cn = patch_string (nn))) |
8609 | nn = cn; | |
8610 | ||
2aa11e97 APB |
8611 | /* The last part of the rewrite for E1 op= E2 is to have |
8612 | E1 = (T)(E1 op E2), with T being the type of E1. */ | |
642f15d1 APB |
8613 | nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2), |
8614 | TREE_TYPE (lvalue), nn)); | |
5cbdba64 APB |
8615 | |
8616 | /* 15.25.2.b: Left hand is an array access. FIXME */ | |
e04a16fb AG |
8617 | } |
8618 | ||
f8976021 APB |
8619 | /* If we're about to patch a NEW_ARRAY_INIT, we call a special |
8620 | function to complete this RHS */ | |
2aa11e97 | 8621 | else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT) |
fdec99c6 | 8622 | nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)), |
f8976021 | 8623 | TREE_OPERAND (node, 1)); |
2aa11e97 | 8624 | /* Otherwise we simply complete the RHS */ |
f8976021 APB |
8625 | else |
8626 | nn = java_complete_tree (TREE_OPERAND (node, 1)); | |
8627 | ||
e04a16fb | 8628 | if (nn == error_mark_node) |
c0d87ff6 | 8629 | return error_mark_node; |
2aa11e97 APB |
8630 | |
8631 | /* Write back the RHS as we evaluated it. */ | |
e04a16fb | 8632 | TREE_OPERAND (node, 1) = nn; |
b67d701b PB |
8633 | |
8634 | /* In case we're handling = with a String as a RHS, we need to | |
8635 | produce a String out of the RHS (it might still be a | |
8636 | STRING_CST or a StringBuffer at this stage */ | |
8637 | if ((nn = patch_string (TREE_OPERAND (node, 1)))) | |
8638 | TREE_OPERAND (node, 1) = nn; | |
15fdcfe9 | 8639 | node = patch_assignment (node, wfl_op1, wfl_op2); |
5cbdba64 APB |
8640 | /* Reorganize the tree if necessary. */ |
8641 | if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node)) | |
8642 | || JSTRING_P (TREE_TYPE (node)))) | |
8643 | node = java_refold (node); | |
15fdcfe9 PB |
8644 | CAN_COMPLETE_NORMALLY (node) = 1; |
8645 | return node; | |
e04a16fb AG |
8646 | |
8647 | case MULT_EXPR: | |
8648 | case PLUS_EXPR: | |
8649 | case MINUS_EXPR: | |
8650 | case LSHIFT_EXPR: | |
8651 | case RSHIFT_EXPR: | |
8652 | case URSHIFT_EXPR: | |
8653 | case BIT_AND_EXPR: | |
8654 | case BIT_XOR_EXPR: | |
8655 | case BIT_IOR_EXPR: | |
8656 | case TRUNC_MOD_EXPR: | |
8657 | case RDIV_EXPR: | |
8658 | case TRUTH_ANDIF_EXPR: | |
8659 | case TRUTH_ORIF_EXPR: | |
8660 | case EQ_EXPR: | |
8661 | case NE_EXPR: | |
8662 | case GT_EXPR: | |
8663 | case GE_EXPR: | |
8664 | case LT_EXPR: | |
8665 | case LE_EXPR: | |
8666 | /* Operands 0 and 1 are WFL in certain cases only. patch_binop | |
8667 | knows how to handle those cases. */ | |
8668 | wfl_op1 = TREE_OPERAND (node, 0); | |
8669 | wfl_op2 = TREE_OPERAND (node, 1); | |
b67d701b | 8670 | |
15fdcfe9 | 8671 | CAN_COMPLETE_NORMALLY (node) = 1; |
b67d701b PB |
8672 | /* Don't complete string nodes if dealing with the PLUS operand. */ |
8673 | if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1)) | |
2aa11e97 APB |
8674 | { |
8675 | nn = java_complete_tree (wfl_op1); | |
8676 | if (nn == error_mark_node) | |
8677 | return error_mark_node; | |
8678 | if ((cn = patch_string (nn))) | |
8679 | nn = cn; | |
8680 | TREE_OPERAND (node, 0) = nn; | |
8681 | } | |
b67d701b | 8682 | if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2)) |
2aa11e97 APB |
8683 | { |
8684 | nn = java_complete_tree (wfl_op2); | |
8685 | if (nn == error_mark_node) | |
8686 | return error_mark_node; | |
8687 | if ((cn = patch_string (nn))) | |
8688 | nn = cn; | |
8689 | TREE_OPERAND (node, 1) = nn; | |
8690 | } | |
dc0b3eff | 8691 | return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2)); |
e04a16fb | 8692 | |
5e942c50 APB |
8693 | case INSTANCEOF_EXPR: |
8694 | wfl_op1 = TREE_OPERAND (node, 0); | |
8695 | COMPLETE_CHECK_OP_0 (node); | |
ce6e9147 APB |
8696 | if (flag_emit_xref) |
8697 | { | |
8698 | TREE_TYPE (node) = boolean_type_node; | |
8699 | return node; | |
8700 | } | |
5e942c50 APB |
8701 | return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1)); |
8702 | ||
b67d701b | 8703 | case UNARY_PLUS_EXPR: |
e04a16fb AG |
8704 | case NEGATE_EXPR: |
8705 | case TRUTH_NOT_EXPR: | |
8706 | case BIT_NOT_EXPR: | |
8707 | case PREDECREMENT_EXPR: | |
8708 | case PREINCREMENT_EXPR: | |
8709 | case POSTDECREMENT_EXPR: | |
8710 | case POSTINCREMENT_EXPR: | |
8711 | case CONVERT_EXPR: | |
8712 | /* There are cases were wfl_op1 is a WFL. patch_unaryop knows | |
8713 | how to handle those cases. */ | |
8714 | wfl_op1 = TREE_OPERAND (node, 0); | |
15fdcfe9 | 8715 | CAN_COMPLETE_NORMALLY (node) = 1; |
e04a16fb AG |
8716 | TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1); |
8717 | if (TREE_OPERAND (node, 0) == error_mark_node) | |
8718 | return error_mark_node; | |
4a5f66c3 APB |
8719 | node = patch_unaryop (node, wfl_op1); |
8720 | CAN_COMPLETE_NORMALLY (node) = 1; | |
8721 | break; | |
e04a16fb AG |
8722 | |
8723 | case ARRAY_REF: | |
8724 | /* There are cases were wfl_op1 is a WFL. patch_array_ref knows | |
8725 | how to handle those cases. */ | |
8726 | wfl_op1 = TREE_OPERAND (node, 0); | |
8727 | TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1); | |
8728 | if (TREE_OPERAND (node, 0) == error_mark_node) | |
8729 | return error_mark_node; | |
7f1d4866 | 8730 | if (!flag_emit_class_files && !flag_emit_xref) |
b67d701b | 8731 | TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0)); |
e04a16fb AG |
8732 | /* The same applies to wfl_op2 */ |
8733 | wfl_op2 = TREE_OPERAND (node, 1); | |
8734 | TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2); | |
8735 | if (TREE_OPERAND (node, 1) == error_mark_node) | |
8736 | return error_mark_node; | |
7f1d4866 | 8737 | if (!flag_emit_class_files && !flag_emit_xref) |
22eed1e6 | 8738 | TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1)); |
939d7216 | 8739 | return patch_array_ref (node); |
e04a16fb | 8740 | |
63a212ed PB |
8741 | case RECORD_TYPE: |
8742 | return node;; | |
8743 | ||
8744 | case COMPONENT_REF: | |
8745 | /* The first step in the re-write of qualified name handling. FIXME. | |
8746 | So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */ | |
9bbc7d9f | 8747 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); |
63a212ed PB |
8748 | if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE) |
8749 | { | |
8750 | tree name = TREE_OPERAND (node, 1); | |
8751 | tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name); | |
8752 | if (field == NULL_TREE) | |
8753 | { | |
8754 | error ("missing static field `%s'", IDENTIFIER_POINTER (name)); | |
8755 | return error_mark_node; | |
8756 | } | |
8757 | if (! FIELD_STATIC (field)) | |
8758 | { | |
8759 | error ("not a static field `%s'", IDENTIFIER_POINTER (name)); | |
8760 | return error_mark_node; | |
8761 | } | |
8762 | return field; | |
8763 | } | |
8764 | else | |
8765 | fatal ("unimplemented java_complete_tree for COMPONENT_REF"); | |
9bbc7d9f | 8766 | break; |
9bbc7d9f | 8767 | |
b67d701b | 8768 | case THIS_EXPR: |
e04a16fb AG |
8769 | /* Can't use THIS in a static environment */ |
8770 | if (!current_this) | |
8771 | { | |
8772 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
781b0558 KG |
8773 | parse_error_context (wfl_operator, |
8774 | "Keyword `this' used outside allowed context"); | |
e04a16fb AG |
8775 | TREE_TYPE (node) = error_mark_node; |
8776 | return error_mark_node; | |
8777 | } | |
22eed1e6 APB |
8778 | if (ctxp->explicit_constructor_p) |
8779 | { | |
8780 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
8781 | parse_error_context | |
781b0558 | 8782 | (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called"); |
22eed1e6 APB |
8783 | TREE_TYPE (node) = error_mark_node; |
8784 | return error_mark_node; | |
8785 | } | |
e04a16fb AG |
8786 | return current_this; |
8787 | ||
e04a16fb | 8788 | default: |
15fdcfe9 | 8789 | CAN_COMPLETE_NORMALLY (node) = 1; |
b67d701b PB |
8790 | /* Ok: may be we have a STRING_CST or a crafted `StringBuffer' |
8791 | and it's time to turn it into the appropriate String object | |
8792 | */ | |
8793 | if ((node = patch_string (node))) | |
8794 | return node; | |
e04a16fb AG |
8795 | fatal ("No case for tree code `%s' - java_complete_tree\n", |
8796 | tree_code_name [TREE_CODE (node)]); | |
8797 | } | |
8798 | return node; | |
8799 | } | |
8800 | ||
8801 | /* Complete function call's argument. Return a non zero value is an | |
8802 | error was found. */ | |
8803 | ||
8804 | static int | |
8805 | complete_function_arguments (node) | |
8806 | tree node; | |
8807 | { | |
8808 | int flag = 0; | |
8809 | tree cn; | |
8810 | ||
f63991a8 | 8811 | ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0); |
e04a16fb AG |
8812 | for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn)) |
8813 | { | |
b67d701b | 8814 | tree wfl = TREE_VALUE (cn), parm, temp; |
e04a16fb AG |
8815 | parm = java_complete_tree (wfl); |
8816 | if (parm == error_mark_node) | |
8817 | { | |
8818 | flag = 1; | |
8819 | continue; | |
8820 | } | |
b67d701b PB |
8821 | /* If have a string literal that we haven't transformed yet or a |
8822 | crafted string buffer, as a result of use of the the String | |
8823 | `+' operator. Build `parm.toString()' and expand it. */ | |
8824 | if ((temp = patch_string (parm))) | |
b9f7e36c | 8825 | parm = temp; |
5e942c50 APB |
8826 | /* Inline PRIMTYPE.TYPE read access */ |
8827 | parm = maybe_build_primttype_type_ref (parm, wfl); | |
b9f7e36c | 8828 | |
5e942c50 | 8829 | TREE_VALUE (cn) = parm; |
e04a16fb | 8830 | } |
f63991a8 | 8831 | ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0); |
e04a16fb AG |
8832 | return flag; |
8833 | } | |
8834 | ||
8835 | /* Sometimes (for loops and variable initialized during their | |
8836 | declaration), we want to wrap a statement around a WFL and turn it | |
8837 | debugable. */ | |
8838 | ||
8839 | static tree | |
8840 | build_debugable_stmt (location, stmt) | |
8841 | int location; | |
8842 | tree stmt; | |
8843 | { | |
8844 | if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION) | |
8845 | { | |
8846 | stmt = build_expr_wfl (stmt, input_filename, 0, 0); | |
8847 | EXPR_WFL_LINECOL (stmt) = location; | |
8848 | } | |
8849 | JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt); | |
8850 | return stmt; | |
8851 | } | |
8852 | ||
8853 | static tree | |
8854 | build_expr_block (body, decls) | |
8855 | tree body, decls; | |
8856 | { | |
8857 | tree node = make_node (BLOCK); | |
8858 | BLOCK_EXPR_DECLS (node) = decls; | |
b67d701b | 8859 | BLOCK_EXPR_BODY (node) = body; |
e04a16fb AG |
8860 | if (body) |
8861 | TREE_TYPE (node) = TREE_TYPE (body); | |
8862 | TREE_SIDE_EFFECTS (node) = 1; | |
8863 | return node; | |
8864 | } | |
8865 | ||
b67d701b PB |
8866 | /* Create a new function block and link it approriately to current |
8867 | function block chain */ | |
e04a16fb AG |
8868 | |
8869 | static tree | |
8870 | enter_block () | |
8871 | { | |
b67d701b PB |
8872 | return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE))); |
8873 | } | |
8874 | ||
8875 | /* Link block B supercontext to the previous block. The current | |
8876 | function DECL is used as supercontext when enter_a_block is called | |
8877 | for the first time for a given function. The current function body | |
8878 | (DECL_FUNCTION_BODY) is set to be block B. */ | |
8879 | ||
8880 | static tree | |
8881 | enter_a_block (b) | |
8882 | tree b; | |
8883 | { | |
e04a16fb AG |
8884 | tree fndecl = current_function_decl; |
8885 | ||
f099f336 APB |
8886 | if (!fndecl) { |
8887 | BLOCK_SUPERCONTEXT (b) = current_static_block; | |
8888 | current_static_block = b; | |
8889 | } | |
8890 | ||
8891 | else if (!DECL_FUNCTION_BODY (fndecl)) | |
e04a16fb AG |
8892 | { |
8893 | BLOCK_SUPERCONTEXT (b) = fndecl; | |
8894 | DECL_FUNCTION_BODY (fndecl) = b; | |
8895 | } | |
8896 | else | |
8897 | { | |
8898 | BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl); | |
8899 | DECL_FUNCTION_BODY (fndecl) = b; | |
8900 | } | |
8901 | return b; | |
8902 | } | |
8903 | ||
8904 | /* Exit a block by changing the current function body | |
8905 | (DECL_FUNCTION_BODY) to the current block super context, only if | |
8906 | the block being exited isn't the method's top level one. */ | |
8907 | ||
8908 | static tree | |
8909 | exit_block () | |
8910 | { | |
f099f336 APB |
8911 | tree b; |
8912 | if (current_function_decl) | |
8913 | { | |
8914 | b = DECL_FUNCTION_BODY (current_function_decl); | |
8915 | if (BLOCK_SUPERCONTEXT (b) != current_function_decl) | |
8916 | DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b); | |
8917 | } | |
8918 | else | |
8919 | { | |
8920 | b = current_static_block; | |
e04a16fb | 8921 | |
f099f336 APB |
8922 | if (BLOCK_SUPERCONTEXT (b)) |
8923 | current_static_block = BLOCK_SUPERCONTEXT (b); | |
8924 | } | |
e04a16fb AG |
8925 | return b; |
8926 | } | |
8927 | ||
8928 | /* Lookup for NAME in the nested function's blocks, all the way up to | |
8929 | the current toplevel one. It complies with Java's local variable | |
8930 | scoping rules. */ | |
8931 | ||
8932 | static tree | |
8933 | lookup_name_in_blocks (name) | |
8934 | tree name; | |
8935 | { | |
f099f336 | 8936 | tree b = GET_CURRENT_BLOCK (current_function_decl); |
e04a16fb AG |
8937 | |
8938 | while (b != current_function_decl) | |
8939 | { | |
8940 | tree current; | |
8941 | ||
8942 | /* Paranoid sanity check. To be removed */ | |
8943 | if (TREE_CODE (b) != BLOCK) | |
8944 | fatal ("non block expr function body - lookup_name_in_blocks"); | |
8945 | ||
8946 | for (current = BLOCK_EXPR_DECLS (b); current; | |
8947 | current = TREE_CHAIN (current)) | |
8948 | if (DECL_NAME (current) == name) | |
8949 | return current; | |
8950 | b = BLOCK_SUPERCONTEXT (b); | |
8951 | } | |
8952 | return NULL_TREE; | |
8953 | } | |
8954 | ||
8955 | static void | |
8956 | maybe_absorb_scoping_blocks () | |
8957 | { | |
f099f336 | 8958 | while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl))) |
e04a16fb AG |
8959 | { |
8960 | tree b = exit_block (); | |
8961 | java_method_add_stmt (current_function_decl, b); | |
8962 | SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno)); | |
8963 | } | |
8964 | } | |
8965 | ||
8966 | \f | |
8967 | /* This section of the source is reserved to build_* functions that | |
8968 | are building incomplete tree nodes and the patch_* functions that | |
8969 | are completing them. */ | |
8970 | ||
9bbc7d9f | 8971 | /* Build a super() constructor invocation. Returns empty_stmt_node if |
22eed1e6 APB |
8972 | we're currently dealing with the class java.lang.Object. */ |
8973 | ||
8974 | static tree | |
e920ebc9 APB |
8975 | build_super_invocation (mdecl) |
8976 | tree mdecl; | |
22eed1e6 | 8977 | { |
e920ebc9 | 8978 | if (DECL_CONTEXT (mdecl) == object_type_node) |
9bbc7d9f | 8979 | return empty_stmt_node; |
22eed1e6 APB |
8980 | else |
8981 | { | |
9ee9b555 | 8982 | tree super_wfl = build_wfl_node (super_identifier_node); |
22eed1e6 APB |
8983 | return build_method_invocation (super_wfl, NULL_TREE); |
8984 | } | |
8985 | } | |
8986 | ||
8987 | /* Build a SUPER/THIS qualified method invocation. */ | |
8988 | ||
8989 | static tree | |
8990 | build_this_super_qualified_invocation (use_this, name, args, lloc, rloc) | |
8991 | int use_this; | |
8992 | tree name, args; | |
8993 | int lloc, rloc; | |
8994 | ||
8995 | { | |
8996 | tree invok; | |
8997 | tree wfl = | |
9ee9b555 | 8998 | build_wfl_node (use_this ? this_identifier_node : super_identifier_node); |
22eed1e6 APB |
8999 | EXPR_WFL_LINECOL (wfl) = lloc; |
9000 | invok = build_method_invocation (name, args); | |
9001 | return make_qualified_primary (wfl, invok, rloc); | |
9002 | } | |
9003 | ||
b67d701b | 9004 | /* Build an incomplete CALL_EXPR node. */ |
e04a16fb AG |
9005 | |
9006 | static tree | |
9007 | build_method_invocation (name, args) | |
9008 | tree name; | |
9009 | tree args; | |
9010 | { | |
9011 | tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE); | |
9012 | TREE_SIDE_EFFECTS (call) = 1; | |
b67d701b PB |
9013 | EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name); |
9014 | return call; | |
9015 | } | |
9016 | ||
9017 | /* Build an incomplete new xxx(...) node. */ | |
9018 | ||
9019 | static tree | |
9020 | build_new_invocation (name, args) | |
9021 | tree name, args; | |
9022 | { | |
9023 | tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE); | |
9024 | TREE_SIDE_EFFECTS (call) = 1; | |
e04a16fb AG |
9025 | EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name); |
9026 | return call; | |
9027 | } | |
9028 | ||
9029 | /* Build an incomplete assignment expression. */ | |
9030 | ||
9031 | static tree | |
9032 | build_assignment (op, op_location, lhs, rhs) | |
9033 | int op, op_location; | |
9034 | tree lhs, rhs; | |
9035 | { | |
9036 | tree assignment; | |
9037 | /* Build the corresponding binop if we deal with a Compound | |
9038 | Assignment operator. Mark the binop sub-tree as part of a | |
9039 | Compound Assignment expression */ | |
9040 | if (op != ASSIGN_TK) | |
9041 | { | |
9042 | rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs); | |
9043 | COMPOUND_ASSIGN_P (rhs) = 1; | |
9044 | } | |
9045 | assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs); | |
9046 | TREE_SIDE_EFFECTS (assignment) = 1; | |
9047 | EXPR_WFL_LINECOL (assignment) = op_location; | |
9048 | return assignment; | |
9049 | } | |
9050 | ||
9051 | /* Print an INTEGER_CST node in a static buffer, and return the buffer. */ | |
9052 | ||
15fdcfe9 | 9053 | char * |
e04a16fb AG |
9054 | print_int_node (node) |
9055 | tree node; | |
9056 | { | |
9057 | static char buffer [80]; | |
9058 | if (TREE_CONSTANT_OVERFLOW (node)) | |
9059 | sprintf (buffer, "<overflow>"); | |
9060 | ||
9061 | if (TREE_INT_CST_HIGH (node) == 0) | |
9062 | sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED, | |
9063 | TREE_INT_CST_LOW (node)); | |
9064 | else if (TREE_INT_CST_HIGH (node) == -1 | |
9065 | && TREE_INT_CST_LOW (node) != 0) | |
9066 | { | |
9067 | buffer [0] = '-'; | |
9068 | sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED, | |
9069 | -TREE_INT_CST_LOW (node)); | |
9070 | } | |
9071 | else | |
9072 | sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX, | |
9073 | TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node)); | |
9074 | ||
9075 | return buffer; | |
9076 | } | |
9077 | ||
7f1d4866 APB |
9078 | /* Return 1 if an assignment to a FINAL is attempted in a non suitable |
9079 | context. */ | |
5e942c50 APB |
9080 | |
9081 | static int | |
9082 | check_final_assignment (lvalue, wfl) | |
9083 | tree lvalue, wfl; | |
9084 | { | |
7f1d4866 APB |
9085 | if (JDECL_P (lvalue) |
9086 | && FIELD_FINAL (lvalue) && !IS_CLINIT (current_function_decl)) | |
5e942c50 APB |
9087 | { |
9088 | parse_error_context | |
9089 | (wfl, "Can't assign a value to the final variable `%s'", | |
9090 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
9091 | return 1; | |
9092 | } | |
9093 | return 0; | |
9094 | } | |
9095 | ||
9096 | /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in | |
9097 | read. This is needed to avoid circularities in the implementation | |
9098 | of these fields in libjava. */ | |
9099 | ||
9100 | static tree | |
9101 | maybe_build_primttype_type_ref (rhs, wfl) | |
9102 | tree rhs, wfl; | |
9103 | { | |
9104 | tree to_return = NULL_TREE; | |
9105 | tree rhs_type = TREE_TYPE (rhs); | |
9106 | if (TREE_CODE (rhs) == COMPOUND_EXPR) | |
9107 | { | |
9108 | tree n = TREE_OPERAND (rhs, 1); | |
9109 | if (TREE_CODE (n) == VAR_DECL | |
9110 | && DECL_NAME (n) == TYPE_identifier_node | |
9111 | && rhs_type == class_ptr_type) | |
9112 | { | |
49f48c71 | 9113 | const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)); |
5e942c50 APB |
9114 | if (!strncmp (self_name, "java.lang.", 10)) |
9115 | to_return = build_primtype_type_ref (self_name); | |
9116 | } | |
9117 | } | |
9118 | return (to_return ? to_return : rhs ); | |
9119 | } | |
9120 | ||
e04a16fb AG |
9121 | /* 15.25 Assignment operators. */ |
9122 | ||
9123 | static tree | |
9124 | patch_assignment (node, wfl_op1, wfl_op2) | |
9125 | tree node; | |
9126 | tree wfl_op1; | |
9127 | tree wfl_op2; | |
9128 | { | |
0a2138e2 | 9129 | tree rhs = TREE_OPERAND (node, 1); |
5e942c50 | 9130 | tree lvalue = TREE_OPERAND (node, 0), llvalue; |
cd531a2e | 9131 | tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE; |
e04a16fb AG |
9132 | int error_found = 0; |
9133 | int lvalue_from_array = 0; | |
9134 | ||
9135 | /* Can't assign to a final. */ | |
5e942c50 APB |
9136 | if (check_final_assignment (lvalue, wfl_op1)) |
9137 | error_found = 1; | |
e04a16fb AG |
9138 | |
9139 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
9140 | ||
9141 | /* Lhs can be a named variable */ | |
34f4db93 | 9142 | if (JDECL_P (lvalue)) |
e04a16fb | 9143 | { |
e04a16fb AG |
9144 | lhs_type = TREE_TYPE (lvalue); |
9145 | } | |
9146 | /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME + | |
9147 | comment on reason why */ | |
9148 | else if (TREE_CODE (wfl_op1) == ARRAY_REF) | |
9149 | { | |
9150 | lhs_type = TREE_TYPE (lvalue); | |
9151 | lvalue_from_array = 1; | |
9152 | } | |
9153 | /* Or a field access */ | |
9154 | else if (TREE_CODE (lvalue) == COMPONENT_REF) | |
9155 | lhs_type = TREE_TYPE (lvalue); | |
9156 | /* Or a function return slot */ | |
9157 | else if (TREE_CODE (lvalue) == RESULT_DECL) | |
9158 | lhs_type = TREE_TYPE (lvalue); | |
5e942c50 APB |
9159 | /* Otherwise, we might want to try to write into an optimized static |
9160 | final, this is an of a different nature, reported further on. */ | |
9161 | else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION | |
1504b2b4 | 9162 | && resolve_expression_name (wfl_op1, &llvalue)) |
5e942c50 | 9163 | { |
1504b2b4 APB |
9164 | if (check_final_assignment (llvalue, wfl_op1)) |
9165 | { | |
9166 | /* What we should do instead is resetting the all the flags | |
9167 | previously set, exchange lvalue for llvalue and continue. */ | |
9168 | error_found = 1; | |
9169 | return error_mark_node; | |
9170 | } | |
9171 | else | |
9172 | lhs_type = TREE_TYPE (lvalue); | |
5e942c50 APB |
9173 | } |
9174 | else | |
e04a16fb AG |
9175 | { |
9176 | parse_error_context (wfl_op1, "Invalid left hand side of assignment"); | |
9177 | error_found = 1; | |
9178 | } | |
9179 | ||
9180 | rhs_type = TREE_TYPE (rhs); | |
b67d701b | 9181 | /* 5.1 Try the assignment conversion for builtin type. */ |
0a2138e2 | 9182 | new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs); |
e04a16fb | 9183 | |
b67d701b | 9184 | /* 5.2 If it failed, try a reference conversion */ |
0a2138e2 | 9185 | if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs))) |
b67d701b | 9186 | lhs_type = promote_type (rhs_type); |
e04a16fb AG |
9187 | |
9188 | /* 15.25.2 If we have a compound assignment, convert RHS into the | |
9189 | type of the LHS */ | |
9190 | else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1))) | |
9191 | new_rhs = convert (lhs_type, rhs); | |
9192 | ||
9193 | /* Explicit cast required. This is an error */ | |
9194 | if (!new_rhs) | |
9195 | { | |
c2e3db92 KG |
9196 | char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0)); |
9197 | char *t2 = xstrdup (lang_printable_name (lhs_type, 0)); | |
e04a16fb AG |
9198 | tree wfl; |
9199 | char operation [32]; /* Max size known */ | |
9200 | ||
9201 | /* If the assignment is part of a declaration, we use the WFL of | |
9202 | the declared variable to point out the error and call it a | |
9203 | declaration problem. If the assignment is a genuine = | |
9204 | operator, we call is a operator `=' problem, otherwise we | |
9205 | call it an assignment problem. In both of these last cases, | |
9206 | we use the WFL of the operator to indicate the error. */ | |
9207 | ||
9208 | if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)) | |
9209 | { | |
9210 | wfl = wfl_op1; | |
9211 | strcpy (operation, "declaration"); | |
9212 | } | |
9213 | else | |
9214 | { | |
9215 | wfl = wfl_operator; | |
9216 | if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1))) | |
9217 | strcpy (operation, "assignment"); | |
9218 | else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL) | |
9219 | strcpy (operation, "`return'"); | |
9220 | else | |
9221 | strcpy (operation, "`='"); | |
9222 | } | |
9223 | ||
1ebadc60 | 9224 | if (!valid_cast_to_p (rhs_type, lhs_type)) |
781b0558 KG |
9225 | parse_error_context |
9226 | (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'", | |
9227 | operation, t1, t2); | |
1ebadc60 | 9228 | else |
781b0558 | 9229 | parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'", |
1ebadc60 | 9230 | operation, t1, t2); |
e04a16fb AG |
9231 | free (t1); free (t2); |
9232 | error_found = 1; | |
9233 | } | |
9234 | ||
c877974e APB |
9235 | /* Inline read access to java.lang.PRIMTYPE.TYPE */ |
9236 | if (new_rhs) | |
9237 | new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2); | |
5e942c50 | 9238 | |
e04a16fb AG |
9239 | if (error_found) |
9240 | return error_mark_node; | |
9241 | ||
2622b947 APB |
9242 | /* 10.10: Array Store Exception runtime check */ |
9243 | if (!flag_emit_class_files | |
e8fc7396 | 9244 | && !flag_emit_xref |
2622b947 | 9245 | && lvalue_from_array |
afc390b1 | 9246 | && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type))) |
2622b947 APB |
9247 | { |
9248 | tree check; | |
9249 | tree base = lvalue; | |
9250 | ||
9251 | /* We need to retrieve the right argument for _Jv_CheckArrayStore */ | |
9252 | if (TREE_CODE (lvalue) == COMPOUND_EXPR) | |
9253 | base = TREE_OPERAND (lvalue, 0); | |
9254 | else | |
9255 | { | |
9256 | if (flag_bounds_check) | |
9257 | base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0); | |
9258 | else | |
9259 | base = TREE_OPERAND (TREE_OPERAND (base, 0), 0); | |
9260 | } | |
9261 | ||
9262 | /* Build the invocation of _Jv_CheckArrayStore */ | |
dc4e6ccf | 9263 | new_rhs = save_expr (new_rhs); |
2622b947 APB |
9264 | check = build (CALL_EXPR, void_type_node, |
9265 | build_address_of (soft_checkarraystore_node), | |
9266 | tree_cons (NULL_TREE, base, | |
9267 | build_tree_list (NULL_TREE, new_rhs)), | |
9268 | NULL_TREE); | |
9269 | TREE_SIDE_EFFECTS (check) = 1; | |
9270 | ||
9271 | /* We have to decide on an insertion point */ | |
9272 | if (TREE_CODE (lvalue) == COMPOUND_EXPR) | |
9273 | { | |
9274 | tree t; | |
9275 | if (flag_bounds_check) | |
9276 | { | |
9277 | t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0); | |
9278 | TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) = | |
9279 | build (COMPOUND_EXPR, void_type_node, t, check); | |
9280 | } | |
9281 | else | |
9282 | TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type, | |
9283 | check, TREE_OPERAND (lvalue, 1)); | |
9284 | } | |
9285 | else | |
9286 | { | |
9287 | /* Make sure the bound check will happen before the store check */ | |
9288 | if (flag_bounds_check) | |
9289 | TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) = | |
9290 | build (COMPOUND_EXPR, void_type_node, | |
9291 | TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check); | |
9292 | else | |
9293 | lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue); | |
9294 | } | |
9295 | } | |
22eed1e6 | 9296 | |
e04a16fb AG |
9297 | TREE_OPERAND (node, 0) = lvalue; |
9298 | TREE_OPERAND (node, 1) = new_rhs; | |
9299 | TREE_TYPE (node) = lhs_type; | |
9300 | return node; | |
9301 | } | |
9302 | ||
b67d701b PB |
9303 | /* Check that type SOURCE can be cast into type DEST. If the cast |
9304 | can't occur at all, return 0 otherwise 1. This function is used to | |
9305 | produce accurate error messages on the reasons why an assignment | |
9306 | failed. */ | |
e04a16fb | 9307 | |
b67d701b PB |
9308 | static tree |
9309 | try_reference_assignconv (lhs_type, rhs) | |
9310 | tree lhs_type, rhs; | |
e04a16fb | 9311 | { |
b67d701b PB |
9312 | tree new_rhs = NULL_TREE; |
9313 | tree rhs_type = TREE_TYPE (rhs); | |
e04a16fb | 9314 | |
b67d701b PB |
9315 | if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type)) |
9316 | { | |
9317 | /* `null' may be assigned to any reference type */ | |
9318 | if (rhs == null_pointer_node) | |
9319 | new_rhs = null_pointer_node; | |
9320 | /* Try the reference assignment conversion */ | |
9321 | else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0)) | |
9322 | new_rhs = rhs; | |
9323 | /* This is a magic assignment that we process differently */ | |
9324 | else if (rhs == soft_exceptioninfo_call_node) | |
9325 | new_rhs = rhs; | |
9326 | } | |
9327 | return new_rhs; | |
9328 | } | |
9329 | ||
9330 | /* Check that RHS can be converted into LHS_TYPE by the assignment | |
9331 | conversion (5.2), for the cases of RHS being a builtin type. Return | |
9332 | NULL_TREE if the conversion fails or if because RHS isn't of a | |
9333 | builtin type. Return a converted RHS if the conversion is possible. */ | |
9334 | ||
9335 | static tree | |
9336 | try_builtin_assignconv (wfl_op1, lhs_type, rhs) | |
9337 | tree wfl_op1, lhs_type, rhs; | |
9338 | { | |
9339 | tree new_rhs = NULL_TREE; | |
9340 | tree rhs_type = TREE_TYPE (rhs); | |
9341 | ||
5e942c50 APB |
9342 | /* Zero accepted everywhere */ |
9343 | if (TREE_CODE (rhs) == INTEGER_CST | |
9344 | && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0 | |
9345 | && JPRIMITIVE_TYPE_P (rhs_type)) | |
9346 | new_rhs = convert (lhs_type, rhs); | |
9347 | ||
b67d701b PB |
9348 | /* 5.1.1 Try Identity Conversion, |
9349 | 5.1.2 Try Widening Primitive Conversion */ | |
5e942c50 | 9350 | else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)) |
b67d701b PB |
9351 | new_rhs = convert (lhs_type, rhs); |
9352 | ||
9353 | /* Try a narrowing primitive conversion (5.1.3): | |
9354 | - expression is a constant expression of type int AND | |
9355 | - variable is byte, short or char AND | |
9356 | - The value of the expression is representable in the type of the | |
9357 | variable */ | |
9358 | else if (rhs_type == int_type_node && TREE_CONSTANT (rhs) | |
9359 | && (lhs_type == byte_type_node || lhs_type == char_type_node | |
9360 | || lhs_type == short_type_node)) | |
9361 | { | |
9362 | if (int_fits_type_p (rhs, lhs_type)) | |
9363 | new_rhs = convert (lhs_type, rhs); | |
9364 | else if (wfl_op1) /* Might be called with a NULL */ | |
9365 | parse_warning_context | |
781b0558 | 9366 | (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'", |
0a2138e2 | 9367 | print_int_node (rhs), lang_printable_name (lhs_type, 0)); |
b67d701b PB |
9368 | /* Reported a warning that will turn into an error further |
9369 | down, so we don't return */ | |
9370 | } | |
9371 | ||
9372 | return new_rhs; | |
9373 | } | |
9374 | ||
9375 | /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity | |
9376 | conversion (5.1.1) or widening primitve conversion (5.1.2). Return | |
9377 | 0 is the conversion test fails. This implements parts the method | |
9378 | invocation convertion (5.3). */ | |
9379 | ||
9380 | static int | |
9381 | valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type) | |
9382 | tree lhs_type, rhs_type; | |
9383 | { | |
acd663ee | 9384 | /* 5.1.1: This is the identity conversion part. */ |
5e942c50 APB |
9385 | if (lhs_type == rhs_type) |
9386 | return 1; | |
9387 | ||
acd663ee APB |
9388 | /* Reject non primitive types */ |
9389 | if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type)) | |
b67d701b PB |
9390 | return 0; |
9391 | ||
acd663ee APB |
9392 | /* 5.1.2: widening primitive conversion. byte, even if it's smaller |
9393 | than a char can't be converted into a char. Short can't too, but | |
9394 | the < test below takes care of that */ | |
b67d701b PB |
9395 | if (lhs_type == char_type_node && rhs_type == byte_type_node) |
9396 | return 0; | |
9397 | ||
5e942c50 APB |
9398 | /* Accept all promoted type here. Note, we can't use <= in the test |
9399 | below, because we still need to bounce out assignments of short | |
9400 | to char and the likes */ | |
9401 | if (lhs_type == int_type_node | |
9402 | && (rhs_type == promoted_byte_type_node | |
9403 | || rhs_type == promoted_short_type_node | |
9404 | || rhs_type == promoted_char_type_node | |
9405 | || rhs_type == promoted_boolean_type_node)) | |
9406 | return 1; | |
9407 | ||
acd663ee APB |
9408 | /* From here, an integral is widened if its precision is smaller |
9409 | than the precision of the LHS or if the LHS is a floating point | |
9410 | type, or the RHS is a float and the RHS a double. */ | |
9411 | if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type) | |
9412 | && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type))) | |
9413 | || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type)) | |
9414 | || (rhs_type == float_type_node && lhs_type == double_type_node)) | |
b67d701b PB |
9415 | return 1; |
9416 | ||
9417 | return 0; | |
e04a16fb AG |
9418 | } |
9419 | ||
9420 | /* Check that something of SOURCE type can be assigned or cast to | |
9421 | something of DEST type at runtime. Return 1 if the operation is | |
9422 | valid, 0 otherwise. If CAST is set to 1, we're treating the case | |
9423 | were SOURCE is cast into DEST, which borrows a lot of the | |
9424 | assignment check. */ | |
9425 | ||
9426 | static int | |
9427 | valid_ref_assignconv_cast_p (source, dest, cast) | |
9428 | tree source; | |
9429 | tree dest; | |
9430 | int cast; | |
9431 | { | |
09ed0f70 APB |
9432 | /* SOURCE or DEST might be null if not from a declared entity. */ |
9433 | if (!source || !dest) | |
9434 | return 0; | |
5e942c50 APB |
9435 | if (JNULLP_TYPE_P (source)) |
9436 | return 1; | |
e04a16fb AG |
9437 | if (TREE_CODE (source) == POINTER_TYPE) |
9438 | source = TREE_TYPE (source); | |
9439 | if (TREE_CODE (dest) == POINTER_TYPE) | |
9440 | dest = TREE_TYPE (dest); | |
9441 | /* Case where SOURCE is a class type */ | |
9442 | if (TYPE_CLASS_P (source)) | |
9443 | { | |
9444 | if (TYPE_CLASS_P (dest)) | |
9445 | return source == dest || inherits_from_p (source, dest) | |
0a2138e2 | 9446 | || (cast && inherits_from_p (dest, source)); |
e04a16fb AG |
9447 | if (TYPE_INTERFACE_P (dest)) |
9448 | { | |
9449 | /* If doing a cast and SOURCE is final, the operation is | |
9450 | always correct a compile time (because even if SOURCE | |
9451 | does not implement DEST, a subclass of SOURCE might). */ | |
9452 | if (cast && !CLASS_FINAL (TYPE_NAME (source))) | |
9453 | return 1; | |
9454 | /* Otherwise, SOURCE must implement DEST */ | |
9455 | return interface_of_p (dest, source); | |
9456 | } | |
9457 | /* DEST is an array, cast permited if SOURCE is of Object type */ | |
9458 | return (cast && source == object_type_node ? 1 : 0); | |
9459 | } | |
9460 | if (TYPE_INTERFACE_P (source)) | |
9461 | { | |
9462 | if (TYPE_CLASS_P (dest)) | |
9463 | { | |
9464 | /* If not casting, DEST must be the Object type */ | |
9465 | if (!cast) | |
9466 | return dest == object_type_node; | |
9467 | /* We're doing a cast. The cast is always valid is class | |
9468 | DEST is not final, otherwise, DEST must implement SOURCE */ | |
b67d701b | 9469 | else if (!CLASS_FINAL (TYPE_NAME (dest))) |
e04a16fb AG |
9470 | return 1; |
9471 | else | |
9472 | return interface_of_p (source, dest); | |
9473 | } | |
9474 | if (TYPE_INTERFACE_P (dest)) | |
9475 | { | |
9476 | /* If doing a cast, then if SOURCE and DEST contain method | |
9477 | with the same signature but different return type, then | |
9478 | this is a (compile time) error */ | |
9479 | if (cast) | |
9480 | { | |
9481 | tree method_source, method_dest; | |
9482 | tree source_type; | |
0a2138e2 | 9483 | tree source_sig; |
e04a16fb AG |
9484 | tree source_name; |
9485 | for (method_source = TYPE_METHODS (source); method_source; | |
9486 | method_source = TREE_CHAIN (method_source)) | |
9487 | { | |
9488 | source_sig = | |
9489 | build_java_argument_signature (TREE_TYPE (method_source)); | |
9490 | source_type = TREE_TYPE (TREE_TYPE (method_source)); | |
9491 | source_name = DECL_NAME (method_source); | |
9492 | for (method_dest = TYPE_METHODS (dest); | |
9493 | method_dest; method_dest = TREE_CHAIN (method_dest)) | |
9494 | if (source_sig == | |
9495 | build_java_argument_signature (TREE_TYPE (method_dest)) | |
9496 | && source_name == DECL_NAME (method_dest) | |
9497 | && source_type != TREE_TYPE (TREE_TYPE (method_dest))) | |
9498 | return 0; | |
9499 | } | |
9500 | return 1; | |
9501 | } | |
9502 | else | |
9503 | return source == dest || interface_of_p (dest, source); | |
9504 | } | |
9505 | else /* Array */ | |
93024893 APB |
9506 | return (cast ? |
9507 | (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0); | |
e04a16fb AG |
9508 | } |
9509 | if (TYPE_ARRAY_P (source)) | |
9510 | { | |
9511 | if (TYPE_CLASS_P (dest)) | |
9512 | return dest == object_type_node; | |
09ed0f70 APB |
9513 | /* Can't cast an array to an interface unless the interface is |
9514 | java.lang.Cloneable */ | |
e04a16fb | 9515 | if (TYPE_INTERFACE_P (dest)) |
09ed0f70 | 9516 | return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0); |
e04a16fb AG |
9517 | else /* Arrays */ |
9518 | { | |
9519 | tree source_element_type = TYPE_ARRAY_ELEMENT (source); | |
9520 | tree dest_element_type = TYPE_ARRAY_ELEMENT (dest); | |
9521 | ||
b9f7e36c APB |
9522 | /* In case of severe errors, they turn out null */ |
9523 | if (!dest_element_type || !source_element_type) | |
9524 | return 0; | |
e04a16fb AG |
9525 | if (source_element_type == dest_element_type) |
9526 | return 1; | |
9527 | return valid_ref_assignconv_cast_p (source_element_type, | |
9528 | dest_element_type, cast); | |
9529 | } | |
9530 | return 0; | |
9531 | } | |
9532 | return 0; | |
9533 | } | |
9534 | ||
b67d701b PB |
9535 | static int |
9536 | valid_cast_to_p (source, dest) | |
9537 | tree source; | |
9538 | tree dest; | |
9539 | { | |
9540 | if (TREE_CODE (source) == POINTER_TYPE) | |
9541 | source = TREE_TYPE (source); | |
9542 | if (TREE_CODE (dest) == POINTER_TYPE) | |
9543 | dest = TREE_TYPE (dest); | |
9544 | ||
9545 | if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE) | |
9546 | return valid_ref_assignconv_cast_p (source, dest, 1); | |
9547 | ||
9548 | else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest)) | |
9549 | return 1; | |
9550 | ||
9551 | return 0; | |
9552 | } | |
9553 | ||
9554 | /* Method invocation conversion test. Return 1 if type SOURCE can be | |
9555 | converted to type DEST through the methond invocation conversion | |
9556 | process (5.3) */ | |
9557 | ||
15fdcfe9 PB |
9558 | static tree |
9559 | do_unary_numeric_promotion (arg) | |
9560 | tree arg; | |
9561 | { | |
9562 | tree type = TREE_TYPE (arg); | |
9563 | if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32 | |
9564 | : TREE_CODE (type) == CHAR_TYPE) | |
9565 | arg = convert (int_type_node, arg); | |
9566 | return arg; | |
9567 | } | |
9568 | ||
acd663ee APB |
9569 | /* Return a non zero value if SOURCE can be converted into DEST using |
9570 | the method invocation conversion rule (5.3). */ | |
b67d701b PB |
9571 | static int |
9572 | valid_method_invocation_conversion_p (dest, source) | |
9573 | tree dest, source; | |
9574 | { | |
e3884b71 | 9575 | return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest) |
acd663ee APB |
9576 | && valid_builtin_assignconv_identity_widening_p (dest, source)) |
9577 | || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source)) | |
9578 | && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest)) | |
9579 | && valid_ref_assignconv_cast_p (source, dest, 0))); | |
b67d701b PB |
9580 | } |
9581 | ||
e04a16fb AG |
9582 | /* Build an incomplete binop expression. */ |
9583 | ||
9584 | static tree | |
9585 | build_binop (op, op_location, op1, op2) | |
9586 | enum tree_code op; | |
9587 | int op_location; | |
9588 | tree op1, op2; | |
9589 | { | |
5e942c50 | 9590 | tree binop = build (op, NULL_TREE, op1, op2); |
e04a16fb AG |
9591 | TREE_SIDE_EFFECTS (binop) = 1; |
9592 | /* Store the location of the operator, for better error report. The | |
9593 | string of the operator will be rebuild based on the OP value. */ | |
9594 | EXPR_WFL_LINECOL (binop) = op_location; | |
9595 | return binop; | |
9596 | } | |
9597 | ||
9598 | /* Build the string of the operator retained by NODE. If NODE is part | |
9599 | of a compound expression, add an '=' at the end of the string. This | |
9600 | function is called when an error needs to be reported on an | |
9601 | operator. The string is returned as a pointer to a static character | |
9602 | buffer. */ | |
9603 | ||
9604 | static char * | |
9605 | operator_string (node) | |
9606 | tree node; | |
9607 | { | |
9608 | #define BUILD_OPERATOR_STRING(S) \ | |
9609 | { \ | |
9610 | sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \ | |
9611 | return buffer; \ | |
9612 | } | |
9613 | ||
9614 | static char buffer [10]; | |
9615 | switch (TREE_CODE (node)) | |
9616 | { | |
9617 | case MULT_EXPR: BUILD_OPERATOR_STRING ("*"); | |
9618 | case RDIV_EXPR: BUILD_OPERATOR_STRING ("/"); | |
9619 | case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%"); | |
9620 | case PLUS_EXPR: BUILD_OPERATOR_STRING ("+"); | |
9621 | case MINUS_EXPR: BUILD_OPERATOR_STRING ("-"); | |
9622 | case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<"); | |
9623 | case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>"); | |
9624 | case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>"); | |
9625 | case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&"); | |
9626 | case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^"); | |
9627 | case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|"); | |
9628 | case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&"); | |
9629 | case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||"); | |
9630 | case EQ_EXPR: BUILD_OPERATOR_STRING ("=="); | |
9631 | case NE_EXPR: BUILD_OPERATOR_STRING ("!="); | |
9632 | case GT_EXPR: BUILD_OPERATOR_STRING (">"); | |
9633 | case GE_EXPR: BUILD_OPERATOR_STRING (">="); | |
9634 | case LT_EXPR: BUILD_OPERATOR_STRING ("<"); | |
9635 | case LE_EXPR: BUILD_OPERATOR_STRING ("<="); | |
b67d701b | 9636 | case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+"); |
e04a16fb AG |
9637 | case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-"); |
9638 | case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!"); | |
9639 | case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~"); | |
9640 | case PREINCREMENT_EXPR: /* Fall through */ | |
9641 | case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++"); | |
9642 | case PREDECREMENT_EXPR: /* Fall through */ | |
9643 | case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--"); | |
9644 | default: | |
9645 | fatal ("unregistered operator %s - operator_string", | |
9646 | tree_code_name [TREE_CODE (node)]); | |
9647 | } | |
9648 | return NULL; | |
9649 | #undef BUILD_OPERATOR_STRING | |
9650 | } | |
9651 | ||
5cbdba64 APB |
9652 | /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */ |
9653 | ||
9654 | static int | |
9655 | java_decl_equiv (var_acc1, var_acc2) | |
9656 | tree var_acc1, var_acc2; | |
9657 | { | |
9658 | if (JDECL_P (var_acc1)) | |
9659 | return (var_acc1 == var_acc2); | |
9660 | ||
9661 | return (TREE_CODE (var_acc1) == COMPONENT_REF | |
9662 | && TREE_CODE (var_acc2) == COMPONENT_REF | |
9663 | && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0) | |
9664 | == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0) | |
9665 | && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1)); | |
9666 | } | |
9667 | ||
9668 | /* Return a non zero value if CODE is one of the operators that can be | |
9669 | used in conjunction with the `=' operator in a compound assignment. */ | |
9670 | ||
9671 | static int | |
9672 | binop_compound_p (code) | |
9673 | enum tree_code code; | |
9674 | { | |
9675 | int i; | |
9676 | for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++) | |
9677 | if (binop_lookup [i] == code) | |
9678 | break; | |
9679 | ||
9680 | return i < BINOP_COMPOUND_CANDIDATES; | |
9681 | } | |
9682 | ||
9683 | /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */ | |
9684 | ||
9685 | static tree | |
9686 | java_refold (t) | |
9687 | tree t; | |
9688 | { | |
9689 | tree c, b, ns, decl; | |
9690 | ||
9691 | if (TREE_CODE (t) != MODIFY_EXPR) | |
9692 | return t; | |
9693 | ||
9694 | c = TREE_OPERAND (t, 1); | |
9695 | if (! (c && TREE_CODE (c) == COMPOUND_EXPR | |
9696 | && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR | |
9697 | && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1))))) | |
9698 | return t; | |
9699 | ||
9700 | /* Now the left branch of the binary operator. */ | |
9701 | b = TREE_OPERAND (TREE_OPERAND (c, 1), 0); | |
9702 | if (! (b && TREE_CODE (b) == NOP_EXPR | |
9703 | && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR)) | |
9704 | return t; | |
9705 | ||
9706 | ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0); | |
9707 | if (! (ns && TREE_CODE (ns) == NOP_EXPR | |
9708 | && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR)) | |
9709 | return t; | |
9710 | ||
9711 | decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0); | |
9712 | if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF) | |
9713 | /* It's got to be the an equivalent decl */ | |
9714 | && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0))) | |
9715 | { | |
9716 | /* Shorten the NOP_EXPR/SAVE_EXPR path. */ | |
9717 | TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0); | |
9718 | /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */ | |
9719 | TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1); | |
9720 | /* Change the right part of the BINOP_EXPR */ | |
9721 | TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0); | |
9722 | } | |
9723 | ||
9724 | return t; | |
9725 | } | |
9726 | ||
e04a16fb AG |
9727 | /* Binary operators (15.16 up to 15.18). We return error_mark_node on |
9728 | errors but we modify NODE so that it contains the type computed | |
9729 | according to the expression, when it's fixed. Otherwise, we write | |
9730 | error_mark_node as the type. It allows us to further the analysis | |
9731 | of remaining nodes and detects more errors in certain cases. */ | |
9732 | ||
9733 | static tree | |
9734 | patch_binop (node, wfl_op1, wfl_op2) | |
9735 | tree node; | |
9736 | tree wfl_op1; | |
9737 | tree wfl_op2; | |
9738 | { | |
9739 | tree op1 = TREE_OPERAND (node, 0); | |
9740 | tree op2 = TREE_OPERAND (node, 1); | |
9741 | tree op1_type = TREE_TYPE (op1); | |
9742 | tree op2_type = TREE_TYPE (op2); | |
ab3a6dd6 | 9743 | tree prom_type = NULL_TREE; |
e04a16fb | 9744 | int code = TREE_CODE (node); |
b67d701b | 9745 | |
e04a16fb AG |
9746 | /* If 1, tell the routine that we have to return error_mark_node |
9747 | after checking for the initialization of the RHS */ | |
9748 | int error_found = 0; | |
9749 | ||
e04a16fb AG |
9750 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); |
9751 | ||
e04a16fb AG |
9752 | switch (code) |
9753 | { | |
9754 | /* 15.16 Multiplicative operators */ | |
9755 | case MULT_EXPR: /* 15.16.1 Multiplication Operator * */ | |
9756 | case RDIV_EXPR: /* 15.16.2 Division Operator / */ | |
9757 | case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */ | |
9758 | if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type)) | |
9759 | { | |
9760 | if (!JPRIMITIVE_TYPE_P (op1_type)) | |
9761 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type); | |
9762 | if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type)) | |
9763 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type); | |
9764 | TREE_TYPE (node) = error_mark_node; | |
9765 | error_found = 1; | |
9766 | break; | |
9767 | } | |
9768 | prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
9769 | /* Change the division operator if necessary */ | |
9770 | if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE) | |
9771 | TREE_SET_CODE (node, TRUNC_DIV_EXPR); | |
0b4d333e | 9772 | |
aa4759c1 AH |
9773 | if (TREE_CODE (prom_type) == INTEGER_TYPE |
9774 | && flag_use_divide_subroutine | |
9775 | && ! flag_emit_class_files | |
9776 | && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR)) | |
9777 | return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2); | |
9778 | ||
0b4d333e APB |
9779 | /* This one is more complicated. FLOATs are processed by a |
9780 | function call to soft_fmod. Duplicate the value of the | |
9781 | COMPOUND_ASSIGN_P flag. */ | |
e04a16fb | 9782 | if (code == TRUNC_MOD_EXPR) |
0b4d333e APB |
9783 | { |
9784 | tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2); | |
9785 | COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node); | |
dc0b3eff PB |
9786 | TREE_SIDE_EFFECTS (mod) |
9787 | = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); | |
0b4d333e APB |
9788 | return mod; |
9789 | } | |
e04a16fb AG |
9790 | break; |
9791 | ||
9792 | /* 15.17 Additive Operators */ | |
9793 | case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */ | |
b67d701b PB |
9794 | |
9795 | /* Operation is valid if either one argument is a string | |
9796 | constant, a String object or a StringBuffer crafted for the | |
9797 | purpose of the a previous usage of the String concatenation | |
9798 | operator */ | |
9799 | ||
9800 | if (TREE_CODE (op1) == STRING_CST | |
9801 | || TREE_CODE (op2) == STRING_CST | |
9802 | || JSTRING_TYPE_P (op1_type) | |
9803 | || JSTRING_TYPE_P (op2_type) | |
9804 | || IS_CRAFTED_STRING_BUFFER_P (op1) | |
9805 | || IS_CRAFTED_STRING_BUFFER_P (op2)) | |
9806 | return build_string_concatenation (op1, op2); | |
9807 | ||
e04a16fb AG |
9808 | case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for |
9809 | Numeric Types */ | |
9810 | if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type)) | |
9811 | { | |
9812 | if (!JPRIMITIVE_TYPE_P (op1_type)) | |
9813 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type); | |
9814 | if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type)) | |
9815 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type); | |
9816 | TREE_TYPE (node) = error_mark_node; | |
9817 | error_found = 1; | |
9818 | break; | |
9819 | } | |
9820 | prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
9821 | break; | |
9822 | ||
9823 | /* 15.18 Shift Operators */ | |
9824 | case LSHIFT_EXPR: | |
9825 | case RSHIFT_EXPR: | |
9826 | case URSHIFT_EXPR: | |
9827 | if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type)) | |
9828 | { | |
9829 | if (!JINTEGRAL_TYPE_P (op1_type)) | |
9830 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type); | |
9831 | else | |
1ebadc60 KG |
9832 | { |
9833 | if (JPRIMITIVE_TYPE_P (op2_type)) | |
9834 | parse_error_context (wfl_operator, | |
781b0558 | 9835 | "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral", |
1ebadc60 KG |
9836 | operator_string (node), |
9837 | lang_printable_name (op2_type, 0)); | |
9838 | else | |
781b0558 KG |
9839 | parse_error_context (wfl_operator, |
9840 | "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral", | |
1ebadc60 KG |
9841 | operator_string (node), |
9842 | lang_printable_name (op2_type, 0)); | |
9843 | } | |
e04a16fb AG |
9844 | TREE_TYPE (node) = error_mark_node; |
9845 | error_found = 1; | |
9846 | break; | |
9847 | } | |
9848 | ||
9849 | /* Unary numeric promotion (5.6.1) is performed on each operand | |
9850 | separatly */ | |
15fdcfe9 PB |
9851 | op1 = do_unary_numeric_promotion (op1); |
9852 | op2 = do_unary_numeric_promotion (op2); | |
e04a16fb AG |
9853 | |
9854 | /* The type of the shift expression is the type of the promoted | |
9855 | type of the left-hand operand */ | |
9856 | prom_type = TREE_TYPE (op1); | |
9857 | ||
6b924cc5 APB |
9858 | /* Shift int only up to 0x1f and long up to 0x3f. The bytecode |
9859 | generator should take care of removing this operation. FIXME */ | |
9860 | op2 = fold (build (BIT_AND_EXPR, prom_type, convert (prom_type, op2), | |
9861 | (prom_type == int_type_node ? build_int_2 (0x1f, 0) : | |
9862 | convert (prom_type, build_int_2 (0x3f, 0))))); | |
e04a16fb AG |
9863 | |
9864 | /* The >>> operator is a >> operating on unsigned quantities */ | |
15fdcfe9 | 9865 | if (code == URSHIFT_EXPR && ! flag_emit_class_files) |
e04a16fb | 9866 | { |
0b4d333e | 9867 | tree to_return; |
73333a87 AH |
9868 | tree utype = unsigned_type (prom_type); |
9869 | op1 = convert (utype, op1); | |
e04a16fb | 9870 | TREE_SET_CODE (node, RSHIFT_EXPR); |
73333a87 AH |
9871 | TREE_OPERAND (node, 0) = op1; |
9872 | TREE_OPERAND (node, 1) = op2; | |
9873 | TREE_TYPE (node) = utype; | |
0b4d333e APB |
9874 | to_return = convert (prom_type, node); |
9875 | /* Copy the original value of the COMPOUND_ASSIGN_P flag */ | |
9876 | COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node); | |
dc0b3eff PB |
9877 | TREE_SIDE_EFFECTS (to_return) |
9878 | = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); | |
0b4d333e | 9879 | return to_return; |
e04a16fb AG |
9880 | } |
9881 | break; | |
5e942c50 APB |
9882 | |
9883 | /* 15.19.1 Type Comparison Operator instaceof */ | |
9884 | case INSTANCEOF_EXPR: | |
9885 | ||
9886 | TREE_TYPE (node) = boolean_type_node; | |
9887 | ||
9888 | if (!(op2_type = resolve_type_during_patch (op2))) | |
9889 | return error_mark_node; | |
9890 | ||
9891 | /* The first operand must be a reference type or the null type */ | |
9892 | if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node) | |
9893 | error_found = 1; /* Error reported further below */ | |
9894 | ||
9895 | /* The second operand must be a reference type */ | |
9896 | if (!JREFERENCE_TYPE_P (op2_type)) | |
9897 | { | |
9898 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op2); | |
9899 | parse_error_context | |
9900 | (wfl_operator, "Invalid argument `%s' for `instanceof'", | |
9901 | lang_printable_name (op2_type, 0)); | |
9902 | error_found = 1; | |
9903 | } | |
9904 | ||
9905 | if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1)) | |
9906 | { | |
9907 | /* If the first operand is null, the result is always false */ | |
9908 | if (op1 == null_pointer_node) | |
9909 | return boolean_false_node; | |
15fdcfe9 PB |
9910 | else if (flag_emit_class_files) |
9911 | { | |
9912 | TREE_OPERAND (node, 1) = op2_type; | |
dc0b3eff | 9913 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1); |
15fdcfe9 PB |
9914 | return node; |
9915 | } | |
5e942c50 APB |
9916 | /* Otherwise we have to invoke instance of to figure it out */ |
9917 | else | |
9918 | { | |
9919 | tree call = | |
9920 | build (CALL_EXPR, boolean_type_node, | |
9921 | build_address_of (soft_instanceof_node), | |
9922 | tree_cons | |
9923 | (NULL_TREE, op1, | |
9924 | build_tree_list (NULL_TREE, | |
9925 | build_class_ref (op2_type))), | |
9926 | NULL_TREE); | |
dc0b3eff | 9927 | TREE_SIDE_EFFECTS (call) = TREE_SIDE_EFFECTS (op1); |
5e942c50 APB |
9928 | return call; |
9929 | } | |
9930 | } | |
9931 | /* There is no way the expression operand can be an instance of | |
9932 | the type operand. This is a compile time error. */ | |
9933 | else | |
9934 | { | |
c2e3db92 | 9935 | char *t1 = xstrdup (lang_printable_name (op1_type, 0)); |
5e942c50 APB |
9936 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); |
9937 | parse_error_context | |
9938 | (wfl_operator, "Impossible for `%s' to be instance of `%s'", | |
9939 | t1, lang_printable_name (op2_type, 0)); | |
9940 | free (t1); | |
9941 | error_found = 1; | |
9942 | } | |
e04a16fb | 9943 | |
5e942c50 | 9944 | break; |
e04a16fb AG |
9945 | |
9946 | /* 15.21 Bitwise and Logical Operators */ | |
9947 | case BIT_AND_EXPR: | |
9948 | case BIT_XOR_EXPR: | |
9949 | case BIT_IOR_EXPR: | |
9950 | if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type)) | |
9951 | /* Binary numeric promotion is performed on both operand and the | |
9952 | expression retain that type */ | |
9953 | prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
9954 | ||
9955 | else if (TREE_CODE (op1_type) == BOOLEAN_TYPE | |
9956 | && TREE_CODE (op1_type) == BOOLEAN_TYPE) | |
9957 | /* The type of the bitwise operator expression is BOOLEAN */ | |
9958 | prom_type = boolean_type_node; | |
9959 | else | |
9960 | { | |
9961 | if (!JINTEGRAL_TYPE_P (op1_type)) | |
9962 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type); | |
9963 | if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type)) | |
9964 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type); | |
9965 | TREE_TYPE (node) = error_mark_node; | |
9966 | error_found = 1; | |
9967 | /* Insert a break here if adding thing before the switch's | |
9968 | break for this case */ | |
9969 | } | |
9970 | break; | |
9971 | ||
9972 | /* 15.22 Conditional-And Operator */ | |
9973 | case TRUTH_ANDIF_EXPR: | |
9974 | /* 15.23 Conditional-Or Operator */ | |
9975 | case TRUTH_ORIF_EXPR: | |
9976 | /* Operands must be of BOOLEAN type */ | |
9977 | if (TREE_CODE (op1_type) != BOOLEAN_TYPE || | |
9978 | TREE_CODE (op2_type) != BOOLEAN_TYPE) | |
9979 | { | |
9980 | if (TREE_CODE (op1_type) != BOOLEAN_TYPE) | |
9981 | ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type); | |
9982 | if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type)) | |
9983 | ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type); | |
9984 | TREE_TYPE (node) = boolean_type_node; | |
9985 | error_found = 1; | |
9986 | break; | |
9987 | } | |
9988 | /* The type of the conditional operators is BOOLEAN */ | |
9989 | prom_type = boolean_type_node; | |
9990 | break; | |
9991 | ||
9992 | /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */ | |
9993 | case LT_EXPR: | |
9994 | case GT_EXPR: | |
9995 | case LE_EXPR: | |
9996 | case GE_EXPR: | |
9997 | /* The type of each of the operands must be a primitive numeric | |
9998 | type */ | |
9999 | if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type)) | |
10000 | { | |
10001 | if (!JNUMERIC_TYPE_P (op1_type)) | |
10002 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type); | |
10003 | if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type)) | |
10004 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type); | |
10005 | TREE_TYPE (node) = boolean_type_node; | |
10006 | error_found = 1; | |
10007 | break; | |
10008 | } | |
10009 | /* Binary numeric promotion is performed on the operands */ | |
10010 | binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
10011 | /* The type of the relation expression is always BOOLEAN */ | |
10012 | prom_type = boolean_type_node; | |
10013 | break; | |
10014 | ||
10015 | /* 15.20 Equality Operator */ | |
10016 | case EQ_EXPR: | |
10017 | case NE_EXPR: | |
10018 | /* 15.20.1 Numerical Equality Operators == and != */ | |
10019 | /* Binary numeric promotion is performed on the operands */ | |
5e942c50 | 10020 | if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type)) |
e04a16fb AG |
10021 | binary_numeric_promotion (op1_type, op2_type, &op1, &op2); |
10022 | ||
10023 | /* 15.20.2 Boolean Equality Operators == and != */ | |
10024 | else if (TREE_CODE (op1_type) == BOOLEAN_TYPE && | |
10025 | TREE_CODE (op2_type) == BOOLEAN_TYPE) | |
10026 | ; /* Nothing to do here */ | |
10027 | ||
10028 | /* 15.20.3 Reference Equality Operators == and != */ | |
5e942c50 APB |
10029 | /* Types have to be either references or the null type. If |
10030 | they're references, it must be possible to convert either | |
10031 | type to the other by casting conversion. */ | |
b9f7e36c APB |
10032 | else if (op1 == null_pointer_node || op2 == null_pointer_node |
10033 | || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type) | |
5e942c50 APB |
10034 | && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1) |
10035 | || valid_ref_assignconv_cast_p (op2_type, | |
10036 | op1_type, 1)))) | |
e04a16fb AG |
10037 | ; /* Nothing to do here */ |
10038 | ||
10039 | /* Else we have an error figure what can't be converted into | |
10040 | what and report the error */ | |
10041 | else | |
10042 | { | |
10043 | char *t1; | |
c2e3db92 | 10044 | t1 = xstrdup (lang_printable_name (op1_type, 0)); |
e04a16fb | 10045 | parse_error_context |
781b0558 KG |
10046 | (wfl_operator, |
10047 | "Incompatible type for `%s'. Can't convert `%s' to `%s'", | |
10048 | operator_string (node), t1, | |
0a2138e2 | 10049 | lang_printable_name (op2_type, 0)); |
e04a16fb AG |
10050 | free (t1); |
10051 | TREE_TYPE (node) = boolean_type_node; | |
10052 | error_found = 1; | |
10053 | break; | |
10054 | } | |
10055 | prom_type = boolean_type_node; | |
10056 | break; | |
10057 | } | |
10058 | ||
e04a16fb AG |
10059 | if (error_found) |
10060 | return error_mark_node; | |
10061 | ||
10062 | TREE_OPERAND (node, 0) = op1; | |
10063 | TREE_OPERAND (node, 1) = op2; | |
10064 | TREE_TYPE (node) = prom_type; | |
dc0b3eff PB |
10065 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); |
10066 | ||
ce6e9147 APB |
10067 | if (flag_emit_xref) |
10068 | return node; | |
10069 | ||
d1472141 PB |
10070 | /* fold does not respect side-effect order as required for Java but not C. |
10071 | * Also, it sometimes create SAVE_EXPRs which are bad when emitting | |
10072 | * bytecode. | |
10073 | */ | |
10074 | if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2)) | |
10075 | : ! TREE_SIDE_EFFECTS (node)) | |
aee48ef8 PB |
10076 | node = fold (node); |
10077 | return node; | |
e04a16fb AG |
10078 | } |
10079 | ||
b67d701b PB |
10080 | /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non |
10081 | zero value, the value of CSTE comes after the valude of STRING */ | |
10082 | ||
10083 | static tree | |
10084 | do_merge_string_cste (cste, string, string_len, after) | |
10085 | tree cste; | |
49f48c71 | 10086 | const char *string; |
b67d701b PB |
10087 | int string_len, after; |
10088 | { | |
10089 | int len = TREE_STRING_LENGTH (cste) + string_len; | |
49f48c71 | 10090 | const char *old = TREE_STRING_POINTER (cste); |
b67d701b PB |
10091 | TREE_STRING_LENGTH (cste) = len; |
10092 | TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1); | |
10093 | if (after) | |
10094 | { | |
10095 | strcpy (TREE_STRING_POINTER (cste), string); | |
10096 | strcat (TREE_STRING_POINTER (cste), old); | |
10097 | } | |
10098 | else | |
10099 | { | |
10100 | strcpy (TREE_STRING_POINTER (cste), old); | |
10101 | strcat (TREE_STRING_POINTER (cste), string); | |
10102 | } | |
10103 | return cste; | |
10104 | } | |
10105 | ||
10106 | /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a | |
10107 | new STRING_CST on success, NULL_TREE on failure */ | |
10108 | ||
10109 | static tree | |
10110 | merge_string_cste (op1, op2, after) | |
10111 | tree op1, op2; | |
10112 | int after; | |
10113 | { | |
10114 | /* Handle two string constants right away */ | |
10115 | if (TREE_CODE (op2) == STRING_CST) | |
10116 | return do_merge_string_cste (op1, TREE_STRING_POINTER (op2), | |
10117 | TREE_STRING_LENGTH (op2), after); | |
10118 | ||
10119 | /* Reasonable integer constant can be treated right away */ | |
10120 | if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2)) | |
10121 | { | |
49f48c71 KG |
10122 | static const char *boolean_true = "true"; |
10123 | static const char *boolean_false = "false"; | |
10124 | static const char *null_pointer = "null"; | |
b67d701b | 10125 | char ch[3]; |
49f48c71 | 10126 | const char *string; |
b67d701b PB |
10127 | |
10128 | if (op2 == boolean_true_node) | |
10129 | string = boolean_true; | |
10130 | else if (op2 == boolean_false_node) | |
10131 | string = boolean_false; | |
10132 | else if (op2 == null_pointer_node) | |
10133 | string = null_pointer; | |
10134 | else if (TREE_TYPE (op2) == char_type_node) | |
10135 | { | |
10136 | ch[0] = (char )TREE_INT_CST_LOW (op2); | |
10137 | ch[1] = '\0'; | |
10138 | string = ch; | |
10139 | } | |
10140 | else | |
10141 | string = print_int_node (op2); | |
10142 | ||
10143 | return do_merge_string_cste (op1, string, strlen (string), after); | |
10144 | } | |
10145 | return NULL_TREE; | |
10146 | } | |
10147 | ||
10148 | /* Tries to statically concatenate OP1 and OP2 if possible. Either one | |
10149 | has to be a STRING_CST and the other part must be a STRING_CST or a | |
10150 | INTEGRAL constant. Return a new STRING_CST if the operation | |
10151 | succeed, NULL_TREE otherwise. | |
10152 | ||
10153 | If the case we want to optimize for space, we might want to return | |
10154 | NULL_TREE for each invocation of this routine. FIXME */ | |
10155 | ||
10156 | static tree | |
10157 | string_constant_concatenation (op1, op2) | |
10158 | tree op1, op2; | |
10159 | { | |
10160 | if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST)) | |
10161 | { | |
0a2138e2 | 10162 | tree string, rest; |
b67d701b PB |
10163 | int invert; |
10164 | ||
10165 | string = (TREE_CODE (op1) == STRING_CST ? op1 : op2); | |
10166 | rest = (string == op1 ? op2 : op1); | |
10167 | invert = (string == op1 ? 0 : 1 ); | |
10168 | ||
10169 | /* Walk REST, only if it looks reasonable */ | |
10170 | if (TREE_CODE (rest) != STRING_CST | |
10171 | && !IS_CRAFTED_STRING_BUFFER_P (rest) | |
10172 | && !JSTRING_TYPE_P (TREE_TYPE (rest)) | |
10173 | && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION) | |
10174 | { | |
10175 | rest = java_complete_tree (rest); | |
10176 | if (rest == error_mark_node) | |
10177 | return error_mark_node; | |
10178 | rest = fold (rest); | |
10179 | } | |
10180 | return merge_string_cste (string, rest, invert); | |
10181 | } | |
10182 | return NULL_TREE; | |
10183 | } | |
10184 | ||
10185 | /* Implement the `+' operator. Does static optimization if possible, | |
10186 | otherwise create (if necessary) and append elements to a | |
10187 | StringBuffer. The StringBuffer will be carried around until it is | |
10188 | used for a function call or an assignment. Then toString() will be | |
10189 | called on it to turn it into a String object. */ | |
10190 | ||
10191 | static tree | |
10192 | build_string_concatenation (op1, op2) | |
10193 | tree op1, op2; | |
10194 | { | |
10195 | tree result; | |
dc0b3eff | 10196 | int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); |
ce6e9147 APB |
10197 | |
10198 | if (flag_emit_xref) | |
10199 | return build (PLUS_EXPR, string_type_node, op1, op2); | |
b67d701b PB |
10200 | |
10201 | /* Try to do some static optimization */ | |
10202 | if ((result = string_constant_concatenation (op1, op2))) | |
10203 | return result; | |
10204 | ||
c0d87ff6 PB |
10205 | /* Discard empty strings on either side of the expression */ |
10206 | if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0) | |
acd663ee APB |
10207 | { |
10208 | op1 = op2; | |
10209 | op2 = NULL_TREE; | |
10210 | } | |
c0d87ff6 | 10211 | else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0) |
acd663ee | 10212 | op2 = NULL_TREE; |
b67d701b | 10213 | |
acd663ee | 10214 | /* If operands are string constant, turn then into object references */ |
b67d701b PB |
10215 | if (TREE_CODE (op1) == STRING_CST) |
10216 | op1 = patch_string_cst (op1); | |
acd663ee | 10217 | if (op2 && TREE_CODE (op2) == STRING_CST) |
b67d701b PB |
10218 | op2 = patch_string_cst (op2); |
10219 | ||
acd663ee APB |
10220 | /* If either one of the constant is null and the other non null |
10221 | operand is a String object, return it. */ | |
10222 | if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2) | |
10223 | return op1; | |
10224 | ||
b67d701b PB |
10225 | /* If OP1 isn't already a StringBuffer, create and |
10226 | initialize a new one */ | |
10227 | if (!IS_CRAFTED_STRING_BUFFER_P (op1)) | |
10228 | { | |
10229 | /* Two solutions here: | |
c52b5771 AG |
10230 | 1) OP1 is a constant string reference, we call new StringBuffer(OP1) |
10231 | 2) OP1 is something else, we call new StringBuffer().append(OP1). */ | |
10232 | if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1))) | |
b67d701b PB |
10233 | op1 = BUILD_STRING_BUFFER (op1); |
10234 | else | |
10235 | { | |
10236 | tree aNew = BUILD_STRING_BUFFER (NULL_TREE); | |
10237 | op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0); | |
10238 | } | |
10239 | } | |
10240 | ||
acd663ee APB |
10241 | if (op2) |
10242 | { | |
10243 | /* OP1 is no longer the last node holding a crafted StringBuffer */ | |
10244 | IS_CRAFTED_STRING_BUFFER_P (op1) = 0; | |
10245 | /* Create a node for `{new...,xxx}.append (op2)' */ | |
10246 | if (op2) | |
10247 | op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0); | |
10248 | } | |
10249 | ||
b67d701b PB |
10250 | /* Mark the last node holding a crafted StringBuffer */ |
10251 | IS_CRAFTED_STRING_BUFFER_P (op1) = 1; | |
dc0b3eff PB |
10252 | |
10253 | TREE_SIDE_EFFECTS (op1) = side_effects; | |
b67d701b PB |
10254 | return op1; |
10255 | } | |
10256 | ||
10257 | /* Patch the string node NODE. NODE can be a STRING_CST of a crafted | |
10258 | StringBuffer. If no string were found to be patched, return | |
10259 | NULL. */ | |
10260 | ||
10261 | static tree | |
10262 | patch_string (node) | |
10263 | tree node; | |
10264 | { | |
1179ebc2 APB |
10265 | if (node == error_mark_node) |
10266 | return error_mark_node; | |
b67d701b PB |
10267 | if (TREE_CODE (node) == STRING_CST) |
10268 | return patch_string_cst (node); | |
10269 | else if (IS_CRAFTED_STRING_BUFFER_P (node)) | |
10270 | { | |
c877974e | 10271 | int saved = ctxp->explicit_constructor_p; |
b67d701b | 10272 | tree invoke = build_method_invocation (wfl_to_string, NULL_TREE); |
c877974e APB |
10273 | tree ret; |
10274 | /* Temporary disable forbid the use of `this'. */ | |
10275 | ctxp->explicit_constructor_p = 0; | |
10276 | ret = java_complete_tree (make_qualified_primary (node, invoke, 0)); | |
10277 | /* Restore it at its previous value */ | |
10278 | ctxp->explicit_constructor_p = saved; | |
10279 | return ret; | |
b67d701b PB |
10280 | } |
10281 | return NULL_TREE; | |
10282 | } | |
10283 | ||
10284 | /* Build the internal representation of a string constant. */ | |
10285 | ||
10286 | static tree | |
10287 | patch_string_cst (node) | |
10288 | tree node; | |
10289 | { | |
10290 | int location; | |
15fdcfe9 PB |
10291 | if (! flag_emit_class_files) |
10292 | { | |
10293 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
10294 | node = get_identifier (TREE_STRING_POINTER (node)); | |
10295 | location = alloc_name_constant (CONSTANT_String, node); | |
10296 | node = build_ref_from_constant_pool (location); | |
8226320b | 10297 | pop_obstacks (); |
15fdcfe9 | 10298 | } |
cd9643f7 | 10299 | TREE_TYPE (node) = string_ptr_type_node; |
b67d701b PB |
10300 | TREE_CONSTANT (node) = 1; |
10301 | return node; | |
10302 | } | |
10303 | ||
10304 | /* Build an incomplete unary operator expression. */ | |
e04a16fb AG |
10305 | |
10306 | static tree | |
10307 | build_unaryop (op_token, op_location, op1) | |
10308 | int op_token, op_location; | |
10309 | tree op1; | |
10310 | { | |
10311 | enum tree_code op; | |
10312 | tree unaryop; | |
10313 | switch (op_token) | |
10314 | { | |
b67d701b | 10315 | case PLUS_TK: op = UNARY_PLUS_EXPR; break; |
e04a16fb AG |
10316 | case MINUS_TK: op = NEGATE_EXPR; break; |
10317 | case NEG_TK: op = TRUTH_NOT_EXPR; break; | |
10318 | case NOT_TK: op = BIT_NOT_EXPR; break; | |
10319 | default: fatal ("Unknown token `%d' for unary operator - build_unaryop", | |
10320 | op_token); | |
10321 | } | |
10322 | ||
10323 | unaryop = build1 (op, NULL_TREE, op1); | |
e04a16fb AG |
10324 | TREE_SIDE_EFFECTS (unaryop) = 1; |
10325 | /* Store the location of the operator, for better error report. The | |
10326 | string of the operator will be rebuild based on the OP value. */ | |
10327 | EXPR_WFL_LINECOL (unaryop) = op_location; | |
10328 | return unaryop; | |
10329 | } | |
10330 | ||
10331 | /* Special case for the ++/-- operators, since they require an extra | |
10332 | argument to build, which is set to NULL and patched | |
10333 | later. IS_POST_P is 1 if the operator, 0 otherwise. */ | |
10334 | ||
10335 | static tree | |
10336 | build_incdec (op_token, op_location, op1, is_post_p) | |
10337 | int op_token, op_location; | |
10338 | tree op1; | |
10339 | int is_post_p; | |
10340 | { | |
10341 | static enum tree_code lookup [2][2] = | |
10342 | { | |
10343 | { PREDECREMENT_EXPR, PREINCREMENT_EXPR, }, | |
10344 | { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, }, | |
10345 | }; | |
10346 | tree node = build (lookup [is_post_p][(op_token - DECR_TK)], | |
10347 | NULL_TREE, op1, NULL_TREE); | |
10348 | TREE_SIDE_EFFECTS (node) = 1; | |
10349 | /* Store the location of the operator, for better error report. The | |
10350 | string of the operator will be rebuild based on the OP value. */ | |
10351 | EXPR_WFL_LINECOL (node) = op_location; | |
10352 | return node; | |
10353 | } | |
10354 | ||
10355 | /* Build an incomplete cast operator, based on the use of the | |
10356 | CONVERT_EXPR. Note that TREE_TYPE of the constructed node is | |
10357 | set. java_complete_tree is trained to walk a CONVERT_EXPR even | |
10358 | though its type is already set. */ | |
10359 | ||
10360 | static tree | |
10361 | build_cast (location, type, exp) | |
10362 | int location; | |
10363 | tree type, exp; | |
10364 | { | |
10365 | tree node = build1 (CONVERT_EXPR, type, exp); | |
10366 | EXPR_WFL_LINECOL (node) = location; | |
10367 | return node; | |
10368 | } | |
10369 | ||
10370 | /* 15.14 Unary operators. We return error_mark_node in case of error, | |
10371 | but preserve the type of NODE if the type is fixed. */ | |
10372 | ||
10373 | static tree | |
10374 | patch_unaryop (node, wfl_op) | |
10375 | tree node; | |
10376 | tree wfl_op; | |
10377 | { | |
10378 | tree op = TREE_OPERAND (node, 0); | |
10379 | tree op_type = TREE_TYPE (op); | |
ab3a6dd6 | 10380 | tree prom_type = NULL_TREE, value, decl; |
e04a16fb AG |
10381 | int code = TREE_CODE (node); |
10382 | int error_found = 0; | |
10383 | ||
10384 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
10385 | ||
10386 | switch (code) | |
10387 | { | |
10388 | /* 15.13.2 Postfix Increment Operator ++ */ | |
10389 | case POSTINCREMENT_EXPR: | |
10390 | /* 15.13.3 Postfix Increment Operator -- */ | |
10391 | case POSTDECREMENT_EXPR: | |
10392 | /* 15.14.1 Prefix Increment Operator ++ */ | |
10393 | case PREINCREMENT_EXPR: | |
10394 | /* 15.14.2 Prefix Decrement Operator -- */ | |
10395 | case PREDECREMENT_EXPR: | |
5cbdba64 | 10396 | op = decl = strip_out_static_field_access_decl (op); |
b3edebcf | 10397 | /* We really should have a JAVA_ARRAY_EXPR to avoid this */ |
e28cd97b | 10398 | if (!JDECL_P (decl) |
b3edebcf APB |
10399 | && TREE_CODE (decl) != COMPONENT_REF |
10400 | && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF) | |
10401 | && TREE_CODE (decl) != INDIRECT_REF | |
10402 | && !(TREE_CODE (decl) == COMPOUND_EXPR | |
10403 | && TREE_OPERAND (decl, 1) | |
10404 | && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF))) | |
e04a16fb | 10405 | { |
5e942c50 APB |
10406 | tree lvalue; |
10407 | /* Before screaming, check that we're not in fact trying to | |
10408 | increment a optimized static final access, in which case | |
10409 | we issue an different error message. */ | |
10410 | if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION | |
10411 | && resolve_expression_name (wfl_op, &lvalue) | |
10412 | && check_final_assignment (lvalue, wfl_op))) | |
10413 | parse_error_context (wfl_operator, "Invalid argument to `%s'", | |
10414 | operator_string (node)); | |
e04a16fb AG |
10415 | TREE_TYPE (node) = error_mark_node; |
10416 | error_found = 1; | |
10417 | } | |
5e942c50 APB |
10418 | else if (check_final_assignment (op, wfl_op)) |
10419 | error_found = 1; | |
10420 | ||
e04a16fb AG |
10421 | /* From now on, we know that op if a variable and that it has a |
10422 | valid wfl. We use wfl_op to locate errors related to the | |
10423 | ++/-- operand. */ | |
10424 | else if (!JNUMERIC_TYPE_P (op_type)) | |
10425 | { | |
10426 | parse_error_context | |
10427 | (wfl_op, "Invalid argument type `%s' to `%s'", | |
0a2138e2 | 10428 | lang_printable_name (op_type, 0), operator_string (node)); |
e04a16fb AG |
10429 | TREE_TYPE (node) = error_mark_node; |
10430 | error_found = 1; | |
10431 | } | |
10432 | else | |
10433 | { | |
4a5f66c3 | 10434 | /* Before the addition, binary numeric promotion is performed on |
5cbdba64 APB |
10435 | both operands, if really necessary */ |
10436 | if (JINTEGRAL_TYPE_P (op_type)) | |
10437 | { | |
10438 | value = build_int_2 (1, 0); | |
10439 | TREE_TYPE (value) = TREE_TYPE (node) = op_type; | |
10440 | } | |
10441 | else | |
10442 | { | |
10443 | value = build_int_2 (1, 0); | |
10444 | TREE_TYPE (node) = | |
10445 | binary_numeric_promotion (op_type, | |
10446 | TREE_TYPE (value), &op, &value); | |
10447 | } | |
10448 | /* And write back into the node. */ | |
4a5f66c3 | 10449 | TREE_OPERAND (node, 0) = op; |
e04a16fb | 10450 | TREE_OPERAND (node, 1) = value; |
5cbdba64 APB |
10451 | /* Convert the overall back into its original type, if |
10452 | necessary, and return */ | |
10453 | if (JINTEGRAL_TYPE_P (op_type)) | |
10454 | return fold (node); | |
10455 | else | |
10456 | return fold (convert (op_type, node)); | |
e04a16fb AG |
10457 | } |
10458 | break; | |
10459 | ||
10460 | /* 15.14.3 Unary Plus Operator + */ | |
b67d701b | 10461 | case UNARY_PLUS_EXPR: |
e04a16fb AG |
10462 | /* 15.14.4 Unary Minus Operator - */ |
10463 | case NEGATE_EXPR: | |
10464 | if (!JNUMERIC_TYPE_P (op_type)) | |
10465 | { | |
10466 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type); | |
10467 | TREE_TYPE (node) = error_mark_node; | |
10468 | error_found = 1; | |
10469 | } | |
10470 | /* Unary numeric promotion is performed on operand */ | |
10471 | else | |
10472 | { | |
15fdcfe9 PB |
10473 | op = do_unary_numeric_promotion (op); |
10474 | prom_type = TREE_TYPE (op); | |
b67d701b | 10475 | if (code == UNARY_PLUS_EXPR) |
4a5f66c3 | 10476 | return fold (op); |
e04a16fb AG |
10477 | } |
10478 | break; | |
10479 | ||
10480 | /* 15.14.5 Bitwise Complement Operator ~ */ | |
10481 | case BIT_NOT_EXPR: | |
10482 | if (!JINTEGRAL_TYPE_P (op_type)) | |
10483 | { | |
10484 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type); | |
10485 | TREE_TYPE (node) = error_mark_node; | |
10486 | error_found = 1; | |
10487 | } | |
10488 | else | |
10489 | { | |
15fdcfe9 PB |
10490 | op = do_unary_numeric_promotion (op); |
10491 | prom_type = TREE_TYPE (op); | |
e04a16fb AG |
10492 | } |
10493 | break; | |
10494 | ||
10495 | /* 15.14.6 Logical Complement Operator ! */ | |
10496 | case TRUTH_NOT_EXPR: | |
10497 | if (TREE_CODE (op_type) != BOOLEAN_TYPE) | |
10498 | { | |
10499 | ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type); | |
c877974e APB |
10500 | /* But the type is known. We will report an error if further |
10501 | attempt of a assignment is made with this rhs */ | |
e04a16fb AG |
10502 | TREE_TYPE (node) = boolean_type_node; |
10503 | error_found = 1; | |
10504 | } | |
10505 | else | |
10506 | prom_type = boolean_type_node; | |
10507 | break; | |
10508 | ||
10509 | /* 15.15 Cast Expression */ | |
10510 | case CONVERT_EXPR: | |
0a2138e2 | 10511 | value = patch_cast (node, wfl_operator); |
e04a16fb | 10512 | if (value == error_mark_node) |
c877974e APB |
10513 | { |
10514 | /* If this cast is part of an assignment, we tell the code | |
10515 | that deals with it not to complain about a mismatch, | |
10516 | because things have been cast, anyways */ | |
10517 | TREE_TYPE (node) = error_mark_node; | |
10518 | error_found = 1; | |
10519 | } | |
10520 | else | |
dc0b3eff PB |
10521 | { |
10522 | value = fold (value); | |
10523 | TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op); | |
10524 | return value; | |
10525 | } | |
e04a16fb AG |
10526 | break; |
10527 | } | |
10528 | ||
e04a16fb AG |
10529 | if (error_found) |
10530 | return error_mark_node; | |
4a5f66c3 APB |
10531 | |
10532 | /* There are cases where node has been replaced by something else | |
10533 | and we don't end up returning here: UNARY_PLUS_EXPR, | |
10534 | CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */ | |
7525cc04 | 10535 | TREE_OPERAND (node, 0) = fold (op); |
4a5f66c3 | 10536 | TREE_TYPE (node) = prom_type; |
dc0b3eff | 10537 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op); |
e04a16fb AG |
10538 | return fold (node); |
10539 | } | |
10540 | ||
10541 | /* Generic type resolution that sometimes takes place during node | |
10542 | patching. Returned the resolved type or generate an error | |
10543 | message. Return the resolved type or NULL_TREE. */ | |
10544 | ||
10545 | static tree | |
10546 | resolve_type_during_patch (type) | |
10547 | tree type; | |
10548 | { | |
10549 | if (unresolved_type_p (type, NULL)) | |
10550 | { | |
10551 | tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE); | |
10552 | if (!type_decl) | |
10553 | { | |
10554 | parse_error_context (type, | |
10555 | "Class `%s' not found in type declaration", | |
10556 | IDENTIFIER_POINTER (EXPR_WFL_NODE (type))); | |
10557 | return NULL_TREE; | |
10558 | } | |
10559 | else | |
5e942c50 APB |
10560 | { |
10561 | CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1; | |
10562 | return TREE_TYPE (type_decl); | |
10563 | } | |
e04a16fb AG |
10564 | } |
10565 | return type; | |
10566 | } | |
10567 | /* 5.5 Casting Conversion. error_mark_node is returned if an error is | |
10568 | found. Otherwise NODE or something meant to replace it is returned. */ | |
10569 | ||
10570 | static tree | |
0a2138e2 | 10571 | patch_cast (node, wfl_operator) |
e04a16fb | 10572 | tree node; |
e04a16fb AG |
10573 | tree wfl_operator; |
10574 | { | |
10575 | tree op = TREE_OPERAND (node, 0); | |
10576 | tree op_type = TREE_TYPE (op); | |
10577 | tree cast_type = TREE_TYPE (node); | |
10578 | char *t1; | |
10579 | ||
10580 | /* First resolve OP_TYPE if unresolved */ | |
10581 | if (!(cast_type = resolve_type_during_patch (cast_type))) | |
10582 | return error_mark_node; | |
10583 | ||
10584 | /* Check on cast that are proven correct at compile time */ | |
10585 | if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type)) | |
10586 | { | |
e04a16fb AG |
10587 | /* Same type */ |
10588 | if (cast_type == op_type) | |
10589 | return node; | |
10590 | ||
0b4d333e APB |
10591 | /* float and double type are converted to the original type main |
10592 | variant and then to the target type. */ | |
10593 | if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE) | |
10594 | op = convert (integer_type_node, op); | |
10595 | ||
e04a16fb AG |
10596 | /* Try widening/narowwing convertion. Potentially, things need |
10597 | to be worked out in gcc so we implement the extreme cases | |
10598 | correctly. fold_convert() needs to be fixed. */ | |
10599 | return convert (cast_type, op); | |
10600 | } | |
10601 | ||
0b4d333e APB |
10602 | /* It's also valid to cast a boolean into a boolean */ |
10603 | if (op_type == boolean_type_node && cast_type == boolean_type_node) | |
10604 | return node; | |
10605 | ||
5e942c50 APB |
10606 | /* null can be casted to references */ |
10607 | if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type)) | |
10608 | return build_null_of_type (cast_type); | |
10609 | ||
e04a16fb AG |
10610 | /* The remaining legal casts involve conversion between reference |
10611 | types. Check for their compile time correctness. */ | |
10612 | if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type) | |
09ed0f70 | 10613 | && valid_ref_assignconv_cast_p (op_type, cast_type, 1)) |
e04a16fb AG |
10614 | { |
10615 | TREE_TYPE (node) = promote_type (cast_type); | |
10616 | /* Now, the case can be determined correct at compile time if | |
10617 | OP_TYPE can be converted into CAST_TYPE by assignment | |
10618 | conversion (5.2) */ | |
10619 | ||
10620 | if (valid_ref_assignconv_cast_p (op_type, cast_type, 0)) | |
15fdcfe9 PB |
10621 | { |
10622 | TREE_SET_CODE (node, NOP_EXPR); | |
10623 | return node; | |
10624 | } | |
10625 | ||
10626 | if (flag_emit_class_files) | |
10627 | { | |
10628 | TREE_SET_CODE (node, CONVERT_EXPR); | |
10629 | return node; | |
10630 | } | |
e04a16fb AG |
10631 | |
10632 | /* The cast requires a run-time check */ | |
10633 | return build (CALL_EXPR, promote_type (cast_type), | |
10634 | build_address_of (soft_checkcast_node), | |
10635 | tree_cons (NULL_TREE, build_class_ref (cast_type), | |
10636 | build_tree_list (NULL_TREE, op)), | |
10637 | NULL_TREE); | |
10638 | } | |
10639 | ||
10640 | /* Any other casts are proven incorrect at compile time */ | |
c2e3db92 | 10641 | t1 = xstrdup (lang_printable_name (op_type, 0)); |
e04a16fb | 10642 | parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'", |
0a2138e2 | 10643 | t1, lang_printable_name (cast_type, 0)); |
e04a16fb AG |
10644 | free (t1); |
10645 | return error_mark_node; | |
10646 | } | |
10647 | ||
5e942c50 APB |
10648 | /* Build a null constant and give it the type TYPE. */ |
10649 | ||
10650 | static tree | |
10651 | build_null_of_type (type) | |
10652 | tree type; | |
10653 | { | |
10654 | tree node = build_int_2 (0, 0); | |
10655 | TREE_TYPE (node) = promote_type (type); | |
10656 | return node; | |
10657 | } | |
10658 | ||
e04a16fb AG |
10659 | /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't |
10660 | a list of indices. */ | |
10661 | static tree | |
10662 | build_array_ref (location, array, index) | |
10663 | int location; | |
10664 | tree array, index; | |
10665 | { | |
10666 | tree node = build (ARRAY_REF, NULL_TREE, array, index); | |
10667 | EXPR_WFL_LINECOL (node) = location; | |
10668 | return node; | |
10669 | } | |
10670 | ||
10671 | /* 15.12 Array Access Expression */ | |
10672 | ||
10673 | static tree | |
c877974e APB |
10674 | patch_array_ref (node) |
10675 | tree node; | |
e04a16fb AG |
10676 | { |
10677 | tree array = TREE_OPERAND (node, 0); | |
10678 | tree array_type = TREE_TYPE (array); | |
10679 | tree index = TREE_OPERAND (node, 1); | |
10680 | tree index_type = TREE_TYPE (index); | |
e04a16fb AG |
10681 | int error_found = 0; |
10682 | ||
10683 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
10684 | ||
e04a16fb AG |
10685 | if (TREE_CODE (array_type) == POINTER_TYPE) |
10686 | array_type = TREE_TYPE (array_type); | |
10687 | ||
10688 | /* The array reference must be an array */ | |
10689 | if (!TYPE_ARRAY_P (array_type)) | |
10690 | { | |
10691 | parse_error_context | |
781b0558 KG |
10692 | (wfl_operator, |
10693 | "`[]' can only be applied to arrays. It can't be applied to `%s'", | |
10694 | lang_printable_name (array_type, 0)); | |
e04a16fb AG |
10695 | TREE_TYPE (node) = error_mark_node; |
10696 | error_found = 1; | |
10697 | } | |
10698 | ||
10699 | /* The array index underdoes unary numeric promotion. The promoted | |
10700 | type must be int */ | |
15fdcfe9 PB |
10701 | index = do_unary_numeric_promotion (index); |
10702 | if (TREE_TYPE (index) != int_type_node) | |
e04a16fb | 10703 | { |
1ebadc60 | 10704 | if (valid_cast_to_p (index_type, int_type_node)) |
781b0558 KG |
10705 | parse_error_context (wfl_operator, |
10706 | "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'", | |
1ebadc60 KG |
10707 | lang_printable_name (index_type, 0)); |
10708 | else | |
781b0558 KG |
10709 | parse_error_context (wfl_operator, |
10710 | "Incompatible type for `[]'. Can't convert `%s' to `int'", | |
1ebadc60 | 10711 | lang_printable_name (index_type, 0)); |
e04a16fb AG |
10712 | TREE_TYPE (node) = error_mark_node; |
10713 | error_found = 1; | |
10714 | } | |
10715 | ||
e04a16fb AG |
10716 | if (error_found) |
10717 | return error_mark_node; | |
e04a16fb | 10718 | |
5e942c50 | 10719 | array_type = TYPE_ARRAY_ELEMENT (array_type); |
5e942c50 | 10720 | |
7f1d4866 | 10721 | if (flag_emit_class_files || flag_emit_xref) |
e04a16fb | 10722 | { |
15fdcfe9 PB |
10723 | TREE_OPERAND (node, 0) = array; |
10724 | TREE_OPERAND (node, 1) = index; | |
e04a16fb AG |
10725 | } |
10726 | else | |
939d7216 PB |
10727 | { |
10728 | /* The save_expr is for correct evaluation order. It would be cleaner | |
10729 | to use force_evaluation_order (see comment there), but that is | |
10730 | difficult when we also have to deal with bounds checking. */ | |
10731 | if (TREE_SIDE_EFFECTS (index)) | |
10732 | array = save_expr (array); | |
10733 | node = build_java_arrayaccess (array, array_type, index); | |
10734 | if (TREE_SIDE_EFFECTS (index)) | |
10735 | node = build (COMPOUND_EXPR, array_type, array, node); | |
10736 | } | |
e04a16fb AG |
10737 | TREE_TYPE (node) = array_type; |
10738 | return node; | |
10739 | } | |
10740 | ||
10741 | /* 15.9 Array Creation Expressions */ | |
10742 | ||
10743 | static tree | |
10744 | build_newarray_node (type, dims, extra_dims) | |
10745 | tree type; | |
10746 | tree dims; | |
10747 | int extra_dims; | |
10748 | { | |
10749 | tree node = | |
b67d701b | 10750 | build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims), |
e04a16fb | 10751 | build_int_2 (extra_dims, 0)); |
e04a16fb AG |
10752 | return node; |
10753 | } | |
10754 | ||
10755 | static tree | |
10756 | patch_newarray (node) | |
10757 | tree node; | |
10758 | { | |
10759 | tree type = TREE_OPERAND (node, 0); | |
10760 | tree dims = TREE_OPERAND (node, 1); | |
10761 | tree cdim, array_type; | |
10762 | int error_found = 0; | |
10763 | int ndims = 0; | |
10764 | int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2)); | |
e04a16fb AG |
10765 | |
10766 | /* Dimension types are verified. It's better for the types to be | |
10767 | verified in order. */ | |
10768 | for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ ) | |
10769 | { | |
10770 | int dim_error = 0; | |
10771 | tree dim = TREE_VALUE (cdim); | |
10772 | ||
10773 | /* Dim might have been saved during its evaluation */ | |
10774 | dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim); | |
10775 | ||
10776 | /* The type of each specified dimension must be an integral type. */ | |
10777 | if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim))) | |
10778 | dim_error = 1; | |
10779 | ||
10780 | /* Each expression undergoes an unary numeric promotion (5.6.1) and the | |
10781 | promoted type must be int. */ | |
10782 | else | |
10783 | { | |
15fdcfe9 | 10784 | dim = do_unary_numeric_promotion (dim); |
e04a16fb AG |
10785 | if (TREE_TYPE (dim) != int_type_node) |
10786 | dim_error = 1; | |
10787 | } | |
10788 | ||
10789 | /* Report errors on types here */ | |
10790 | if (dim_error) | |
10791 | { | |
10792 | parse_error_context | |
10793 | (TREE_PURPOSE (cdim), | |
781b0558 | 10794 | "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'", |
b67d701b | 10795 | (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ? |
e04a16fb | 10796 | "Explicit cast needed to" : "Can't"), |
0a2138e2 | 10797 | lang_printable_name (TREE_TYPE (dim), 0)); |
e04a16fb AG |
10798 | error_found = 1; |
10799 | } | |
10800 | ||
e04a16fb AG |
10801 | TREE_PURPOSE (cdim) = NULL_TREE; |
10802 | } | |
10803 | ||
10804 | /* Resolve array base type if unresolved */ | |
10805 | if (!(type = resolve_type_during_patch (type))) | |
10806 | error_found = 1; | |
10807 | ||
10808 | if (error_found) | |
10809 | { | |
10810 | /* We don't want further evaluation of this bogus array creation | |
10811 | operation */ | |
10812 | TREE_TYPE (node) = error_mark_node; | |
10813 | return error_mark_node; | |
10814 | } | |
10815 | ||
15fdcfe9 PB |
10816 | /* Set array_type to the actual (promoted) array type of the result. */ |
10817 | if (TREE_CODE (type) == RECORD_TYPE) | |
10818 | type = build_pointer_type (type); | |
10819 | while (--xdims >= 0) | |
10820 | { | |
10821 | type = promote_type (build_java_array_type (type, -1)); | |
10822 | } | |
10823 | dims = nreverse (dims); | |
10824 | array_type = type; | |
10825 | for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim)) | |
10826 | { | |
10827 | type = array_type; | |
10828 | array_type = build_java_array_type (type, | |
10829 | TREE_CODE (cdim) == INTEGER_CST ? | |
10830 | TREE_INT_CST_LOW (cdim) : -1); | |
10831 | array_type = promote_type (array_type); | |
10832 | } | |
10833 | dims = nreverse (dims); | |
10834 | ||
e04a16fb AG |
10835 | /* The node is transformed into a function call. Things are done |
10836 | differently according to the number of dimensions. If the number | |
10837 | of dimension is equal to 1, then the nature of the base type | |
10838 | (primitive or not) matters. */ | |
15fdcfe9 | 10839 | if (ndims == 1) |
fdec99c6 | 10840 | return build_new_array (type, TREE_VALUE (dims)); |
e04a16fb | 10841 | |
e04a16fb AG |
10842 | /* Can't reuse what's already written in expr.c because it uses the |
10843 | JVM stack representation. Provide a build_multianewarray. FIXME */ | |
15fdcfe9 | 10844 | return build (CALL_EXPR, array_type, |
e04a16fb | 10845 | build_address_of (soft_multianewarray_node), |
15fdcfe9 | 10846 | tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)), |
e04a16fb | 10847 | tree_cons (NULL_TREE, |
15fdcfe9 | 10848 | build_int_2 (ndims, 0), dims )), |
e04a16fb AG |
10849 | NULL_TREE); |
10850 | } | |
10851 | ||
f8976021 APB |
10852 | /* 10.6 Array initializer. */ |
10853 | ||
10854 | /* Build a wfl for array element that don't have one, so we can | |
10855 | pin-point errors. */ | |
10856 | ||
10857 | static tree | |
10858 | maybe_build_array_element_wfl (node) | |
10859 | tree node; | |
10860 | { | |
10861 | if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION) | |
10862 | return build_expr_wfl (NULL_TREE, ctxp->filename, | |
10863 | ctxp->elc.line, ctxp->elc.prev_col); | |
10864 | else | |
10865 | return NULL_TREE; | |
10866 | } | |
10867 | ||
10868 | /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes | |
10869 | identification of initialized arrays easier to detect during walk | |
10870 | and expansion. */ | |
10871 | ||
10872 | static tree | |
10873 | build_new_array_init (location, values) | |
10874 | int location; | |
10875 | tree values; | |
10876 | { | |
10877 | tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values); | |
10878 | tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor); | |
5bba4807 | 10879 | EXPR_WFL_LINECOL (to_return) = location; |
f8976021 APB |
10880 | return to_return; |
10881 | } | |
10882 | ||
10883 | /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error | |
10884 | occurred. Otherwise return NODE after having set its type | |
10885 | appropriately. */ | |
10886 | ||
10887 | static tree | |
10888 | patch_new_array_init (type, node) | |
10889 | tree type, node; | |
f8976021 APB |
10890 | { |
10891 | int error_seen = 0; | |
fdec99c6 | 10892 | tree current, element_type; |
f8976021 | 10893 | HOST_WIDE_INT length; |
fdec99c6 PB |
10894 | int all_constant = 1; |
10895 | tree init = TREE_OPERAND (node, 0); | |
f8976021 | 10896 | |
fdec99c6 PB |
10897 | if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type))) |
10898 | { | |
10899 | parse_error_context (node, | |
10900 | "Invalid array initializer for non-array type `%s'", | |
10901 | lang_printable_name (type, 1)); | |
10902 | return error_mark_node; | |
10903 | } | |
10904 | type = TREE_TYPE (type); | |
10905 | element_type = TYPE_ARRAY_ELEMENT (type); | |
f8976021 | 10906 | |
fdec99c6 PB |
10907 | CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init)); |
10908 | ||
10909 | for (length = 0, current = CONSTRUCTOR_ELTS (init); | |
10910 | current; length++, current = TREE_CHAIN (current)) | |
f8976021 | 10911 | { |
fdec99c6 PB |
10912 | tree elt = TREE_VALUE (current); |
10913 | if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT) | |
f8976021 | 10914 | { |
fdec99c6 | 10915 | error_seen |= array_constructor_check_entry (element_type, current); |
5bba4807 PB |
10916 | elt = TREE_VALUE (current); |
10917 | /* When compiling to native code, STRING_CST is converted to | |
10918 | INDIRECT_REF, but still with a TREE_CONSTANT flag. */ | |
10919 | if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF) | |
fdec99c6 | 10920 | all_constant = 0; |
f8976021 | 10921 | } |
fdec99c6 PB |
10922 | else |
10923 | { | |
10924 | TREE_VALUE (current) = patch_new_array_init (element_type, elt); | |
10925 | TREE_PURPOSE (current) = NULL_TREE; | |
10926 | all_constant = 0; | |
10927 | } | |
10928 | if (elt && TREE_VALUE (elt) == error_mark_node) | |
10929 | error_seen = 1; | |
f8976021 APB |
10930 | } |
10931 | ||
10932 | if (error_seen) | |
10933 | return error_mark_node; | |
10934 | ||
10935 | /* Create a new type. We can't reuse the one we have here by | |
10936 | patching its dimension because it originally is of dimension -1 | |
10937 | hence reused by gcc. This would prevent triangular arrays. */ | |
fdec99c6 PB |
10938 | type = build_java_array_type (element_type, length); |
10939 | TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type)))); | |
10940 | TREE_TYPE (node) = promote_type (type); | |
10941 | TREE_CONSTANT (init) = all_constant; | |
bc3ca41b | 10942 | TREE_CONSTANT (node) = all_constant; |
f8976021 APB |
10943 | return node; |
10944 | } | |
10945 | ||
10946 | /* Verify that one entry of the initializer element list can be | |
10947 | assigned to the array base type. Report 1 if an error occurred, 0 | |
10948 | otherwise. */ | |
10949 | ||
10950 | static int | |
10951 | array_constructor_check_entry (type, entry) | |
10952 | tree type, entry; | |
10953 | { | |
10954 | char *array_type_string = NULL; /* For error reports */ | |
10955 | tree value, type_value, new_value, wfl_value, patched; | |
10956 | int error_seen = 0; | |
10957 | ||
10958 | new_value = NULL_TREE; | |
10959 | wfl_value = TREE_VALUE (entry); | |
10960 | ||
100f7cd8 | 10961 | push_obstacks (&permanent_obstack, &permanent_obstack); |
f8976021 | 10962 | value = java_complete_tree (TREE_VALUE (entry)); |
1179ebc2 | 10963 | /* patch_string return error_mark_node if arg is error_mark_node */ |
f8976021 APB |
10964 | if ((patched = patch_string (value))) |
10965 | value = patched; | |
1179ebc2 APB |
10966 | if (value == error_mark_node) |
10967 | return 1; | |
f8976021 | 10968 | |
f8976021 APB |
10969 | type_value = TREE_TYPE (value); |
10970 | ||
1179ebc2 | 10971 | /* At anytime, try_builtin_assignconv can report a warning on |
f8976021 APB |
10972 | constant overflow during narrowing. */ |
10973 | SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value); | |
10974 | new_value = try_builtin_assignconv (wfl_operator, type, value); | |
10975 | if (!new_value && (new_value = try_reference_assignconv (type, value))) | |
10976 | type_value = promote_type (type); | |
100f7cd8 APB |
10977 | |
10978 | pop_obstacks (); | |
f8976021 APB |
10979 | /* Check and report errors */ |
10980 | if (!new_value) | |
10981 | { | |
49f48c71 | 10982 | const char *msg = (!valid_cast_to_p (type_value, type) ? |
f8976021 APB |
10983 | "Can't" : "Explicit cast needed to"); |
10984 | if (!array_type_string) | |
c2e3db92 | 10985 | array_type_string = xstrdup (lang_printable_name (type, 1)); |
f8976021 APB |
10986 | parse_error_context |
10987 | (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'", | |
10988 | msg, lang_printable_name (type_value, 1), array_type_string); | |
10989 | error_seen = 1; | |
10990 | } | |
10991 | ||
10992 | if (new_value) | |
10993 | { | |
10994 | new_value = maybe_build_primttype_type_ref (new_value, wfl_operator); | |
10995 | TREE_VALUE (entry) = new_value; | |
10996 | } | |
10997 | ||
10998 | if (array_type_string) | |
10999 | free (array_type_string); | |
11000 | ||
11001 | TREE_PURPOSE (entry) = NULL_TREE; | |
11002 | return error_seen; | |
11003 | } | |
11004 | ||
e04a16fb AG |
11005 | static tree |
11006 | build_this (location) | |
11007 | int location; | |
11008 | { | |
9ee9b555 | 11009 | tree node = build_wfl_node (this_identifier_node); |
b67d701b | 11010 | TREE_SET_CODE (node, THIS_EXPR); |
e04a16fb AG |
11011 | EXPR_WFL_LINECOL (node) = location; |
11012 | return node; | |
11013 | } | |
11014 | ||
11015 | /* 14.15 The return statement. It builds a modify expression that | |
11016 | assigns the returned value to the RESULT_DECL that hold the value | |
11017 | to be returned. */ | |
11018 | ||
11019 | static tree | |
11020 | build_return (location, op) | |
11021 | int location; | |
11022 | tree op; | |
11023 | { | |
11024 | tree node = build1 (RETURN_EXPR, NULL_TREE, op); | |
11025 | EXPR_WFL_LINECOL (node) = location; | |
b67d701b | 11026 | node = build_debugable_stmt (location, node); |
e04a16fb AG |
11027 | return node; |
11028 | } | |
11029 | ||
11030 | static tree | |
11031 | patch_return (node) | |
11032 | tree node; | |
11033 | { | |
11034 | tree return_exp = TREE_OPERAND (node, 0); | |
11035 | tree meth = current_function_decl; | |
11036 | tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl)); | |
e04a16fb AG |
11037 | int error_found = 0; |
11038 | ||
11039 | TREE_TYPE (node) = error_mark_node; | |
11040 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11041 | ||
11042 | /* It's invalid to have a return value within a function that is | |
11043 | declared with the keyword void or that is a constructor */ | |
11044 | if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth))) | |
11045 | error_found = 1; | |
11046 | ||
f099f336 | 11047 | /* It's invalid to use a return statement in a static block */ |
7f1d4866 | 11048 | if (IS_CLINIT (current_function_decl)) |
f099f336 APB |
11049 | error_found = 1; |
11050 | ||
e04a16fb AG |
11051 | /* It's invalid to have a no return value within a function that |
11052 | isn't declared with the keyword `void' */ | |
11053 | if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth))) | |
11054 | error_found = 2; | |
11055 | ||
11056 | if (error_found) | |
11057 | { | |
7f1d4866 | 11058 | if (IS_CLINIT (current_function_decl)) |
f099f336 APB |
11059 | parse_error_context (wfl_operator, |
11060 | "`return' inside static initializer."); | |
11061 | ||
11062 | else if (!DECL_CONSTRUCTOR_P (meth)) | |
22eed1e6 | 11063 | { |
c2e3db92 | 11064 | char *t = xstrdup (lang_printable_name (mtype, 0)); |
22eed1e6 APB |
11065 | parse_error_context (wfl_operator, |
11066 | "`return' with%s value from `%s %s'", | |
11067 | (error_found == 1 ? "" : "out"), | |
11068 | t, lang_printable_name (meth, 0)); | |
11069 | free (t); | |
11070 | } | |
11071 | else | |
11072 | parse_error_context (wfl_operator, | |
11073 | "`return' with value from constructor `%s'", | |
11074 | lang_printable_name (meth, 0)); | |
e04a16fb AG |
11075 | return error_mark_node; |
11076 | } | |
11077 | ||
5e942c50 APB |
11078 | /* If we have a return_exp, build a modify expression and expand |
11079 | it. Note: at that point, the assignment is declared valid, but we | |
11080 | may want to carry some more hacks */ | |
e04a16fb AG |
11081 | if (return_exp) |
11082 | { | |
5e942c50 APB |
11083 | tree exp = java_complete_tree (return_exp); |
11084 | tree modify, patched; | |
11085 | ||
11086 | /* If the function returned value and EXP are booleans, EXP has | |
11087 | to be converted into the type of DECL_RESULT, which is integer | |
11088 | (see complete_start_java_method) */ | |
11089 | if (TREE_TYPE (exp) == boolean_type_node && | |
11090 | TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node) | |
11091 | exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp); | |
11092 | ||
11093 | /* `null' can be assigned to a function returning a reference */ | |
11094 | if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) && | |
11095 | exp == null_pointer_node) | |
11096 | exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth))); | |
11097 | ||
11098 | if ((patched = patch_string (exp))) | |
11099 | exp = patched; | |
11100 | ||
11101 | modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp); | |
e04a16fb AG |
11102 | EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node); |
11103 | modify = java_complete_tree (modify); | |
5e942c50 | 11104 | |
e04a16fb AG |
11105 | if (modify != error_mark_node) |
11106 | { | |
11107 | TREE_SIDE_EFFECTS (modify) = 1; | |
11108 | TREE_OPERAND (node, 0) = modify; | |
11109 | } | |
11110 | else | |
11111 | return error_mark_node; | |
11112 | } | |
11113 | TREE_TYPE (node) = void_type_node; | |
11114 | TREE_SIDE_EFFECTS (node) = 1; | |
11115 | return node; | |
11116 | } | |
11117 | ||
11118 | /* 14.8 The if Statement */ | |
11119 | ||
11120 | static tree | |
11121 | build_if_else_statement (location, expression, if_body, else_body) | |
11122 | int location; | |
11123 | tree expression, if_body, else_body; | |
11124 | { | |
11125 | tree node; | |
e04a16fb | 11126 | if (!else_body) |
9bbc7d9f | 11127 | else_body = empty_stmt_node; |
e04a16fb AG |
11128 | node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body); |
11129 | EXPR_WFL_LINECOL (node) = location; | |
b67d701b | 11130 | node = build_debugable_stmt (location, node); |
e04a16fb AG |
11131 | return node; |
11132 | } | |
11133 | ||
11134 | static tree | |
11135 | patch_if_else_statement (node) | |
11136 | tree node; | |
11137 | { | |
11138 | tree expression = TREE_OPERAND (node, 0); | |
11139 | ||
11140 | TREE_TYPE (node) = error_mark_node; | |
11141 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11142 | ||
11143 | /* The type of expression must be boolean */ | |
b67d701b PB |
11144 | if (TREE_TYPE (expression) != boolean_type_node |
11145 | && TREE_TYPE (expression) != promoted_boolean_type_node) | |
e04a16fb AG |
11146 | { |
11147 | parse_error_context | |
11148 | (wfl_operator, | |
11149 | "Incompatible type for `if'. Can't convert `%s' to `boolean'", | |
0a2138e2 | 11150 | lang_printable_name (TREE_TYPE (expression), 0)); |
e04a16fb AG |
11151 | return error_mark_node; |
11152 | } | |
11153 | ||
11154 | TREE_TYPE (node) = void_type_node; | |
11155 | TREE_SIDE_EFFECTS (node) = 1; | |
15fdcfe9 | 11156 | CAN_COMPLETE_NORMALLY (node) |
9bbc7d9f PB |
11157 | = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)) |
11158 | | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)); | |
e04a16fb AG |
11159 | return node; |
11160 | } | |
11161 | ||
11162 | /* 14.6 Labeled Statements */ | |
11163 | ||
11164 | /* Action taken when a lableled statement is parsed. a new | |
11165 | LABELED_BLOCK_EXPR is created. No statement is attached to the | |
b635eb2f | 11166 | label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */ |
e04a16fb AG |
11167 | |
11168 | static tree | |
0a2138e2 | 11169 | build_labeled_block (location, label) |
e04a16fb | 11170 | int location; |
0a2138e2 | 11171 | tree label; |
e04a16fb | 11172 | { |
b635eb2f | 11173 | tree label_name ; |
e04a16fb | 11174 | tree label_decl, node; |
b635eb2f PB |
11175 | if (label == NULL_TREE || label == continue_identifier_node) |
11176 | label_name = label; | |
11177 | else | |
e04a16fb | 11178 | { |
b635eb2f PB |
11179 | label_name = merge_qualified_name (label_id, label); |
11180 | /* Issue an error if we try to reuse a label that was previously | |
11181 | declared */ | |
11182 | if (IDENTIFIER_LOCAL_VALUE (label_name)) | |
11183 | { | |
11184 | EXPR_WFL_LINECOL (wfl_operator) = location; | |
781b0558 KG |
11185 | parse_error_context (wfl_operator, |
11186 | "Declaration of `%s' shadows a previous label declaration", | |
b635eb2f PB |
11187 | IDENTIFIER_POINTER (label)); |
11188 | EXPR_WFL_LINECOL (wfl_operator) = | |
11189 | EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name)); | |
781b0558 KG |
11190 | parse_error_context (wfl_operator, |
11191 | "This is the location of the previous declaration of label `%s'", | |
b635eb2f PB |
11192 | IDENTIFIER_POINTER (label)); |
11193 | java_error_count--; | |
11194 | } | |
e04a16fb AG |
11195 | } |
11196 | ||
11197 | label_decl = create_label_decl (label_name); | |
11198 | node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE); | |
11199 | EXPR_WFL_LINECOL (node) = location; | |
11200 | TREE_SIDE_EFFECTS (node) = 1; | |
11201 | return node; | |
11202 | } | |
11203 | ||
b67d701b | 11204 | /* A labeled statement LBE is attached a statement. */ |
e04a16fb AG |
11205 | |
11206 | static tree | |
b635eb2f | 11207 | finish_labeled_statement (lbe, statement) |
e04a16fb AG |
11208 | tree lbe; /* Labeled block expr */ |
11209 | tree statement; | |
11210 | { | |
11211 | /* In anyways, tie the loop to its statement */ | |
11212 | LABELED_BLOCK_BODY (lbe) = statement; | |
b635eb2f PB |
11213 | pop_labeled_block (); |
11214 | POP_LABELED_BLOCK (); | |
e04a16fb AG |
11215 | return lbe; |
11216 | } | |
11217 | ||
11218 | /* 14.10, 14.11, 14.12 Loop Statements */ | |
11219 | ||
11220 | /* Create an empty LOOP_EXPR and make it the last in the nested loop | |
11221 | list. */ | |
11222 | ||
11223 | static tree | |
11224 | build_new_loop (loop_body) | |
11225 | tree loop_body; | |
11226 | { | |
11227 | tree loop = build (LOOP_EXPR, NULL_TREE, loop_body); | |
11228 | TREE_SIDE_EFFECTS (loop) = 1; | |
11229 | PUSH_LOOP (loop); | |
11230 | return loop; | |
11231 | } | |
11232 | ||
11233 | /* Create a loop body according to the following structure: | |
11234 | COMPOUND_EXPR | |
11235 | COMPOUND_EXPR (loop main body) | |
11236 | EXIT_EXPR (this order is for while/for loops. | |
11237 | LABELED_BLOCK_EXPR the order is reversed for do loops) | |
34f4db93 | 11238 | LABEL_DECL (a continue occuring here branches at the |
e04a16fb AG |
11239 | BODY end of this labeled block) |
11240 | INCREMENT (if any) | |
11241 | ||
11242 | REVERSED, if non zero, tells that the loop condition expr comes | |
b67d701b PB |
11243 | after the body, like in the do-while loop. |
11244 | ||
11245 | To obtain a loop, the loop body structure described above is | |
11246 | encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR: | |
11247 | ||
11248 | LABELED_BLOCK_EXPR | |
11249 | LABEL_DECL (use this label to exit the loop) | |
11250 | LOOP_EXPR | |
11251 | <structure described above> */ | |
e04a16fb AG |
11252 | |
11253 | static tree | |
11254 | build_loop_body (location, condition, reversed) | |
11255 | int location; | |
11256 | tree condition; | |
11257 | int reversed; | |
11258 | { | |
0a2138e2 | 11259 | tree first, second, body; |
e04a16fb AG |
11260 | |
11261 | condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */ | |
11262 | EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */ | |
11263 | condition = build_debugable_stmt (location, condition); | |
11264 | TREE_SIDE_EFFECTS (condition) = 1; | |
11265 | ||
b635eb2f | 11266 | body = build_labeled_block (0, continue_identifier_node); |
e04a16fb AG |
11267 | first = (reversed ? body : condition); |
11268 | second = (reversed ? condition : body); | |
11269 | return | |
11270 | build (COMPOUND_EXPR, NULL_TREE, | |
9bbc7d9f | 11271 | build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node); |
e04a16fb AG |
11272 | } |
11273 | ||
11274 | /* Install CONDITION (if any) and loop BODY (using REVERSED to tell | |
11275 | their order) on the current loop. Unlink the current loop from the | |
11276 | loop list. */ | |
11277 | ||
11278 | static tree | |
b635eb2f | 11279 | finish_loop_body (location, condition, body, reversed) |
e04a16fb AG |
11280 | int location; |
11281 | tree condition, body; | |
11282 | int reversed; | |
11283 | { | |
11284 | tree to_return = ctxp->current_loop; | |
11285 | tree loop_body = LOOP_EXPR_BODY (to_return); | |
11286 | if (condition) | |
11287 | { | |
11288 | tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed); | |
11289 | /* We wrapped the EXIT_EXPR around a WFL so we can debug it. | |
11290 | The real EXIT_EXPR is one operand further. */ | |
11291 | EXPR_WFL_LINECOL (cnode) = location; | |
11292 | /* This one is for accurate error reports */ | |
11293 | EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location; | |
11294 | TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition; | |
11295 | } | |
11296 | LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body; | |
11297 | POP_LOOP (); | |
11298 | return to_return; | |
11299 | } | |
11300 | ||
b635eb2f | 11301 | /* Tailored version of finish_loop_body for FOR loops, when FOR |
e04a16fb AG |
11302 | loops feature the condition part */ |
11303 | ||
11304 | static tree | |
b635eb2f | 11305 | finish_for_loop (location, condition, update, body) |
e04a16fb AG |
11306 | int location; |
11307 | tree condition, update, body; | |
11308 | { | |
11309 | /* Put the condition and the loop body in place */ | |
b635eb2f | 11310 | tree loop = finish_loop_body (location, condition, body, 0); |
e04a16fb AG |
11311 | /* LOOP is the current loop which has been now popped of the loop |
11312 | stack. Install the update block */ | |
11313 | LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update; | |
11314 | return loop; | |
11315 | } | |
11316 | ||
5cbdba64 APB |
11317 | /* Try to find the loop a block might be related to. This comprises |
11318 | the case where the LOOP_EXPR is found as the second operand of a | |
11319 | COMPOUND_EXPR, because the loop happens to have an initialization | |
11320 | part, then expressed as the first operand of the COMPOUND_EXPR. If | |
11321 | the search finds something, 1 is returned. Otherwise, 0 is | |
11322 | returned. The search is assumed to start from a | |
11323 | LABELED_BLOCK_EXPR's block. */ | |
11324 | ||
11325 | static tree | |
11326 | search_loop (statement) | |
11327 | tree statement; | |
11328 | { | |
11329 | if (TREE_CODE (statement) == LOOP_EXPR) | |
11330 | return statement; | |
11331 | ||
11332 | if (TREE_CODE (statement) == BLOCK) | |
11333 | statement = BLOCK_SUBBLOCKS (statement); | |
11334 | else | |
11335 | return NULL_TREE; | |
11336 | ||
11337 | if (statement && TREE_CODE (statement) == COMPOUND_EXPR) | |
11338 | while (statement && TREE_CODE (statement) == COMPOUND_EXPR) | |
11339 | statement = TREE_OPERAND (statement, 1); | |
11340 | ||
11341 | return (TREE_CODE (statement) == LOOP_EXPR | |
11342 | && IS_FOR_LOOP_P (statement) ? statement : NULL_TREE); | |
11343 | } | |
11344 | ||
11345 | /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is | |
11346 | returned otherwise. */ | |
11347 | ||
11348 | static int | |
11349 | labeled_block_contains_loop_p (block, loop) | |
11350 | tree block, loop; | |
11351 | { | |
11352 | if (!block) | |
11353 | return 0; | |
11354 | ||
11355 | if (LABELED_BLOCK_BODY (block) == loop) | |
11356 | return 1; | |
11357 | ||
11358 | if (IS_FOR_LOOP_P (loop) | |
11359 | && search_loop (LABELED_BLOCK_BODY (block)) == loop) | |
11360 | return 1; | |
11361 | ||
11362 | return 0; | |
11363 | } | |
11364 | ||
e04a16fb | 11365 | /* If the loop isn't surrounded by a labeled statement, create one and |
b635eb2f | 11366 | insert LOOP as its body. */ |
e04a16fb AG |
11367 | |
11368 | static tree | |
11369 | patch_loop_statement (loop) | |
11370 | tree loop; | |
11371 | { | |
cd9643f7 | 11372 | tree loop_label; |
5cbdba64 | 11373 | |
cd9643f7 | 11374 | TREE_TYPE (loop) = void_type_node; |
5cbdba64 APB |
11375 | if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop)) |
11376 | return loop; | |
11377 | ||
cd9643f7 | 11378 | loop_label = build_labeled_block (0, NULL_TREE); |
5cbdba64 APB |
11379 | /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL |
11380 | that LOOP_LABEL could enquire about, for a better accuracy. FIXME */ | |
cd9643f7 PB |
11381 | LABELED_BLOCK_BODY (loop_label) = loop; |
11382 | PUSH_LABELED_BLOCK (loop_label); | |
5cbdba64 | 11383 | return loop_label; |
e04a16fb AG |
11384 | } |
11385 | ||
11386 | /* 14.13, 14.14: break and continue Statements */ | |
11387 | ||
11388 | /* Build a break or a continue statement. a null NAME indicates an | |
11389 | unlabeled break/continue statement. */ | |
11390 | ||
11391 | static tree | |
11392 | build_bc_statement (location, is_break, name) | |
11393 | int location, is_break; | |
11394 | tree name; | |
11395 | { | |
11396 | tree break_continue, label_block_expr = NULL_TREE; | |
11397 | ||
11398 | if (name) | |
11399 | { | |
11400 | if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE | |
11401 | (merge_qualified_name (label_id, EXPR_WFL_NODE (name))))) | |
11402 | /* Null means that we don't have a target for this named | |
11403 | break/continue. In this case, we make the target to be the | |
11404 | label name, so that the error can be reported accuratly in | |
11405 | patch_bc_statement. */ | |
11406 | label_block_expr = EXPR_WFL_NODE (name); | |
11407 | } | |
11408 | /* Unlabeled break/continue will be handled during the | |
11409 | break/continue patch operation */ | |
11410 | break_continue | |
11411 | = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE); | |
11412 | ||
11413 | IS_BREAK_STMT_P (break_continue) = is_break; | |
11414 | TREE_SIDE_EFFECTS (break_continue) = 1; | |
11415 | EXPR_WFL_LINECOL (break_continue) = location; | |
b67d701b | 11416 | break_continue = build_debugable_stmt (location, break_continue); |
e04a16fb AG |
11417 | return break_continue; |
11418 | } | |
11419 | ||
11420 | /* Verification of a break/continue statement. */ | |
11421 | ||
11422 | static tree | |
11423 | patch_bc_statement (node) | |
11424 | tree node; | |
11425 | { | |
11426 | tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt; | |
b635eb2f | 11427 | tree labeled_block = ctxp->current_labeled_block; |
b67d701b | 11428 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); |
e04a16fb | 11429 | |
e04a16fb | 11430 | /* Having an identifier here means that the target is unknown. */ |
b635eb2f | 11431 | if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE) |
e04a16fb AG |
11432 | { |
11433 | parse_error_context (wfl_operator, "No label definition found for `%s'", | |
11434 | IDENTIFIER_POINTER (bc_label)); | |
11435 | return error_mark_node; | |
11436 | } | |
b635eb2f | 11437 | if (! IS_BREAK_STMT_P (node)) |
e04a16fb | 11438 | { |
b635eb2f PB |
11439 | /* It's a continue statement. */ |
11440 | for (;; labeled_block = TREE_CHAIN (labeled_block)) | |
e04a16fb | 11441 | { |
b635eb2f PB |
11442 | if (labeled_block == NULL_TREE) |
11443 | { | |
11444 | if (bc_label == NULL_TREE) | |
11445 | parse_error_context (wfl_operator, | |
11446 | "`continue' must be in loop"); | |
11447 | else | |
1504b2b4 APB |
11448 | parse_error_context |
11449 | (wfl_operator, "continue label `%s' does not name a loop", | |
11450 | IDENTIFIER_POINTER (bc_label)); | |
b635eb2f PB |
11451 | return error_mark_node; |
11452 | } | |
11453 | if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block)) | |
11454 | == continue_identifier_node) | |
11455 | && (bc_label == NULL_TREE | |
11456 | || TREE_CHAIN (labeled_block) == bc_label)) | |
11457 | { | |
11458 | bc_label = labeled_block; | |
11459 | break; | |
11460 | } | |
e04a16fb | 11461 | } |
e04a16fb | 11462 | } |
b635eb2f | 11463 | else if (!bc_label) |
34f4db93 | 11464 | { |
b635eb2f | 11465 | for (;; labeled_block = TREE_CHAIN (labeled_block)) |
e04a16fb | 11466 | { |
b635eb2f PB |
11467 | if (labeled_block == NULL_TREE) |
11468 | { | |
11469 | parse_error_context (wfl_operator, | |
11470 | "`break' must be in loop or switch"); | |
11471 | return error_mark_node; | |
11472 | } | |
11473 | target_stmt = LABELED_BLOCK_BODY (labeled_block); | |
11474 | if (TREE_CODE (target_stmt) == SWITCH_EXPR | |
5cbdba64 | 11475 | || search_loop (target_stmt)) |
b635eb2f PB |
11476 | { |
11477 | bc_label = labeled_block; | |
11478 | break; | |
11479 | } | |
e04a16fb | 11480 | } |
e04a16fb AG |
11481 | } |
11482 | ||
b635eb2f | 11483 | EXIT_BLOCK_LABELED_BLOCK (node) = bc_label; |
15fdcfe9 PB |
11484 | CAN_COMPLETE_NORMALLY (bc_label) = 1; |
11485 | ||
e04a16fb AG |
11486 | /* Our break/continue don't return values. */ |
11487 | TREE_TYPE (node) = void_type_node; | |
11488 | /* Encapsulate the break within a compound statement so that it's | |
5cbdba64 | 11489 | expanded all the times by expand_expr (and not clobbered |
e04a16fb AG |
11490 | sometimes, like after a if statement) */ |
11491 | node = add_stmt_to_compound (NULL_TREE, void_type_node, node); | |
11492 | TREE_SIDE_EFFECTS (node) = 1; | |
11493 | return node; | |
11494 | } | |
11495 | ||
11496 | /* Process the exit expression belonging to a loop. Its type must be | |
11497 | boolean. */ | |
11498 | ||
11499 | static tree | |
11500 | patch_exit_expr (node) | |
11501 | tree node; | |
11502 | { | |
11503 | tree expression = TREE_OPERAND (node, 0); | |
11504 | TREE_TYPE (node) = error_mark_node; | |
11505 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11506 | ||
11507 | /* The type of expression must be boolean */ | |
11508 | if (TREE_TYPE (expression) != boolean_type_node) | |
11509 | { | |
11510 | parse_error_context | |
11511 | (wfl_operator, | |
781b0558 | 11512 | "Incompatible type for loop conditional. Can't convert `%s' to `boolean'", |
0a2138e2 | 11513 | lang_printable_name (TREE_TYPE (expression), 0)); |
e04a16fb AG |
11514 | return error_mark_node; |
11515 | } | |
11516 | /* Now we know things are allright, invert the condition, fold and | |
11517 | return */ | |
11518 | TREE_OPERAND (node, 0) = | |
11519 | fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression)); | |
15fdcfe9 PB |
11520 | |
11521 | if (! integer_zerop (TREE_OPERAND (node, 0)) | |
11522 | && ctxp->current_loop != NULL_TREE | |
11523 | && TREE_CODE (ctxp->current_loop) == LOOP_EXPR) | |
11524 | CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1; | |
11525 | if (! integer_onep (TREE_OPERAND (node, 0))) | |
11526 | CAN_COMPLETE_NORMALLY (node) = 1; | |
11527 | ||
11528 | ||
e04a16fb AG |
11529 | TREE_TYPE (node) = void_type_node; |
11530 | return node; | |
11531 | } | |
b67d701b PB |
11532 | |
11533 | /* 14.9 Switch statement */ | |
11534 | ||
11535 | static tree | |
11536 | patch_switch_statement (node) | |
11537 | tree node; | |
11538 | { | |
c877974e | 11539 | tree se = TREE_OPERAND (node, 0), se_type; |
b67d701b PB |
11540 | |
11541 | /* Complete the switch expression */ | |
11542 | se = TREE_OPERAND (node, 0) = java_complete_tree (se); | |
11543 | se_type = TREE_TYPE (se); | |
11544 | /* The type of the switch expression must be char, byte, short or | |
11545 | int */ | |
11546 | if (!JINTEGRAL_TYPE_P (se_type)) | |
11547 | { | |
11548 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
781b0558 KG |
11549 | parse_error_context (wfl_operator, |
11550 | "Incompatible type for `switch'. Can't convert `%s' to `int'", | |
0a2138e2 | 11551 | lang_printable_name (se_type, 0)); |
b67d701b PB |
11552 | /* This is what java_complete_tree will check */ |
11553 | TREE_OPERAND (node, 0) = error_mark_node; | |
11554 | return error_mark_node; | |
11555 | } | |
11556 | ||
15fdcfe9 | 11557 | TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1)); |
b67d701b PB |
11558 | |
11559 | /* Ready to return */ | |
15fdcfe9 | 11560 | if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK) |
b67d701b PB |
11561 | { |
11562 | TREE_TYPE (node) = error_mark_node; | |
11563 | return error_mark_node; | |
11564 | } | |
11565 | TREE_TYPE (node) = void_type_node; | |
11566 | TREE_SIDE_EFFECTS (node) = 1; | |
15fdcfe9 | 11567 | CAN_COMPLETE_NORMALLY (node) |
c877974e APB |
11568 | = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)) |
11569 | || ! SWITCH_HAS_DEFAULT (node); | |
b67d701b PB |
11570 | return node; |
11571 | } | |
11572 | ||
b67d701b PB |
11573 | /* 14.18 The try statement */ |
11574 | ||
b67d701b | 11575 | static tree |
a7d8d81f | 11576 | build_try_statement (location, try_block, catches) |
b67d701b | 11577 | int location; |
a7d8d81f PB |
11578 | tree try_block, catches; |
11579 | { | |
11580 | tree node = build (TRY_EXPR, NULL_TREE, try_block, catches); | |
b67d701b | 11581 | EXPR_WFL_LINECOL (node) = location; |
a7d8d81f | 11582 | return node; |
b67d701b PB |
11583 | } |
11584 | ||
a7d8d81f PB |
11585 | static tree |
11586 | build_try_finally_statement (location, try_block, finally) | |
11587 | int location; | |
11588 | tree try_block, finally; | |
b67d701b | 11589 | { |
a7d8d81f PB |
11590 | tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally); |
11591 | EXPR_WFL_LINECOL (node) = location; | |
11592 | return node; | |
b67d701b PB |
11593 | } |
11594 | ||
11595 | static tree | |
11596 | patch_try_statement (node) | |
11597 | tree node; | |
11598 | { | |
11599 | int error_found = 0; | |
11600 | tree try = TREE_OPERAND (node, 0); | |
11601 | /* Exception handlers are considered in left to right order */ | |
11602 | tree catch = nreverse (TREE_OPERAND (node, 1)); | |
b9f7e36c | 11603 | tree current, caught_type_list = NULL_TREE; |
b67d701b PB |
11604 | |
11605 | /* Check catch clauses, if any. Every time we find an error, we try | |
b9f7e36c APB |
11606 | to process the next catch clause. We process the catch clause before |
11607 | the try block so that when processing the try block we can check thrown | |
11608 | exceptions againts the caught type list. */ | |
b67d701b PB |
11609 | for (current = catch; current; current = TREE_CHAIN (current)) |
11610 | { | |
11611 | tree carg_decl, carg_type; | |
11612 | tree sub_current, catch_block, catch_clause; | |
11613 | int unreachable; | |
11614 | ||
b67d701b | 11615 | /* At this point, the structure of the catch clause is |
b67d701b PB |
11616 | CATCH_EXPR (catch node) |
11617 | BLOCK (with the decl of the parameter) | |
11618 | COMPOUND_EXPR | |
7525cc04 | 11619 | MODIFY_EXPR (assignment of the catch parameter) |
b67d701b | 11620 | BLOCK (catch clause block) |
a7d8d81f PB |
11621 | */ |
11622 | catch_clause = TREE_OPERAND (current, 0); | |
b67d701b PB |
11623 | carg_decl = BLOCK_EXPR_DECLS (catch_clause); |
11624 | carg_type = TREE_TYPE (TREE_TYPE (carg_decl)); | |
11625 | ||
11626 | /* Catch clauses can't have more than one parameter declared, | |
11627 | but it's already enforced by the grammar. Make sure that the | |
11628 | only parameter of the clause statement in of class Throwable | |
11629 | or a subclass of Throwable, but that was done earlier. The | |
11630 | catch clause parameter type has also been resolved. */ | |
11631 | ||
11632 | /* Just make sure that the catch clause parameter type inherits | |
11633 | from java.lang.Throwable */ | |
11634 | if (!inherits_from_p (carg_type, throwable_type_node)) | |
11635 | { | |
11636 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current); | |
11637 | parse_error_context (wfl_operator, | |
781b0558 | 11638 | "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'", |
0a2138e2 | 11639 | lang_printable_name (carg_type, 0)); |
b67d701b PB |
11640 | error_found = 1; |
11641 | continue; | |
11642 | } | |
11643 | ||
11644 | /* Partial check for unreachable catch statement: The catch | |
11645 | clause is reachable iff is no earlier catch block A in | |
11646 | the try statement such that the type of the catch | |
11647 | clause's parameter is the same as or a subclass of the | |
11648 | type of A's parameter */ | |
11649 | unreachable = 0; | |
11650 | for (sub_current = catch; | |
11651 | sub_current != current; sub_current = TREE_CHAIN (sub_current)) | |
11652 | { | |
11653 | tree sub_catch_clause, decl; | |
a7d8d81f | 11654 | sub_catch_clause = TREE_OPERAND (sub_current, 0); |
b67d701b PB |
11655 | decl = BLOCK_EXPR_DECLS (sub_catch_clause); |
11656 | ||
11657 | if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl)))) | |
11658 | { | |
11659 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current); | |
11660 | parse_error_context | |
781b0558 KG |
11661 | (wfl_operator, |
11662 | "`catch' not reached because of the catch clause at line %d", | |
11663 | EXPR_WFL_LINENO (sub_current)); | |
b67d701b PB |
11664 | unreachable = error_found = 1; |
11665 | break; | |
11666 | } | |
11667 | } | |
b67d701b PB |
11668 | /* Complete the catch clause block */ |
11669 | catch_block = java_complete_tree (TREE_OPERAND (current, 0)); | |
11670 | if (catch_block == error_mark_node) | |
11671 | { | |
11672 | error_found = 1; | |
11673 | continue; | |
11674 | } | |
15fdcfe9 PB |
11675 | if (CAN_COMPLETE_NORMALLY (catch_block)) |
11676 | CAN_COMPLETE_NORMALLY (node) = 1; | |
b67d701b | 11677 | TREE_OPERAND (current, 0) = catch_block; |
15fdcfe9 PB |
11678 | |
11679 | if (unreachable) | |
11680 | continue; | |
11681 | ||
11682 | /* Things to do here: the exception must be thrown */ | |
11683 | ||
11684 | /* Link this type to the caught type list */ | |
11685 | caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list); | |
b67d701b PB |
11686 | } |
11687 | ||
b9f7e36c APB |
11688 | PUSH_EXCEPTIONS (caught_type_list); |
11689 | if ((try = java_complete_tree (try)) == error_mark_node) | |
11690 | error_found = 1; | |
15fdcfe9 PB |
11691 | if (CAN_COMPLETE_NORMALLY (try)) |
11692 | CAN_COMPLETE_NORMALLY (node) = 1; | |
b9f7e36c APB |
11693 | POP_EXCEPTIONS (); |
11694 | ||
b67d701b PB |
11695 | /* Verification ends here */ |
11696 | if (error_found) | |
11697 | return error_mark_node; | |
11698 | ||
11699 | TREE_OPERAND (node, 0) = try; | |
11700 | TREE_OPERAND (node, 1) = catch; | |
b67d701b PB |
11701 | TREE_TYPE (node) = void_type_node; |
11702 | return node; | |
11703 | } | |
b9f7e36c APB |
11704 | |
11705 | /* 14.17 The synchronized Statement */ | |
11706 | ||
11707 | static tree | |
11708 | patch_synchronized_statement (node, wfl_op1) | |
11709 | tree node, wfl_op1; | |
11710 | { | |
5a005d9e | 11711 | tree expr = java_complete_tree (TREE_OPERAND (node, 0)); |
b9f7e36c | 11712 | tree block = TREE_OPERAND (node, 1); |
5a005d9e | 11713 | |
d8fccff5 | 11714 | tree enter, exit, expr_decl, assignment; |
5a005d9e PB |
11715 | |
11716 | if (expr == error_mark_node) | |
11717 | { | |
11718 | block = java_complete_tree (block); | |
11719 | return expr; | |
11720 | } | |
b9f7e36c APB |
11721 | |
11722 | /* The TYPE of expr must be a reference type */ | |
5a005d9e | 11723 | if (!JREFERENCE_TYPE_P (TREE_TYPE (expr))) |
b9f7e36c APB |
11724 | { |
11725 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); | |
781b0558 | 11726 | parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'", |
0a2138e2 | 11727 | lang_printable_name (TREE_TYPE (expr), 0)); |
b9f7e36c APB |
11728 | return error_mark_node; |
11729 | } | |
11730 | ||
ce6e9147 APB |
11731 | if (flag_emit_xref) |
11732 | { | |
11733 | TREE_OPERAND (node, 0) = expr; | |
11734 | TREE_OPERAND (node, 1) = java_complete_tree (block); | |
11735 | CAN_COMPLETE_NORMALLY (node) = 1; | |
11736 | return node; | |
11737 | } | |
11738 | ||
b9f7e36c APB |
11739 | /* Generate a try-finally for the synchronized statement, except |
11740 | that the handler that catches all throw exception calls | |
11741 | _Jv_MonitorExit and then rethrow the exception. | |
11742 | The synchronized statement is then implemented as: | |
11743 | TRY | |
11744 | { | |
11745 | _Jv_MonitorEnter (expression) | |
11746 | synchronized_block | |
11747 | _Jv_MonitorExit (expression) | |
11748 | } | |
11749 | CATCH_ALL | |
11750 | { | |
11751 | e = _Jv_exception_info (); | |
11752 | _Jv_MonitorExit (expression) | |
11753 | Throw (e); | |
11754 | } */ | |
11755 | ||
5a005d9e PB |
11756 | expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr)); |
11757 | BUILD_MONITOR_ENTER (enter, expr_decl); | |
11758 | BUILD_MONITOR_EXIT (exit, expr_decl); | |
11759 | CAN_COMPLETE_NORMALLY (enter) = 1; | |
11760 | CAN_COMPLETE_NORMALLY (exit) = 1; | |
96847892 AH |
11761 | assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr); |
11762 | TREE_SIDE_EFFECTS (assignment) = 1; | |
5a005d9e PB |
11763 | node = build1 (CLEANUP_POINT_EXPR, NULL_TREE, |
11764 | build (COMPOUND_EXPR, NULL_TREE, | |
11765 | build (WITH_CLEANUP_EXPR, NULL_TREE, | |
11766 | build (COMPOUND_EXPR, NULL_TREE, | |
96847892 | 11767 | assignment, enter), |
5a005d9e PB |
11768 | NULL_TREE, exit), |
11769 | block)); | |
11770 | node = build_expr_block (node, expr_decl); | |
11771 | ||
11772 | return java_complete_tree (node); | |
b9f7e36c APB |
11773 | } |
11774 | ||
11775 | /* 14.16 The throw Statement */ | |
11776 | ||
11777 | static tree | |
11778 | patch_throw_statement (node, wfl_op1) | |
11779 | tree node, wfl_op1; | |
11780 | { | |
11781 | tree expr = TREE_OPERAND (node, 0); | |
11782 | tree type = TREE_TYPE (expr); | |
11783 | int unchecked_ok = 0, tryblock_throws_ok = 0; | |
11784 | ||
11785 | /* Thrown expression must be assignable to java.lang.Throwable */ | |
11786 | if (!try_reference_assignconv (throwable_type_node, expr)) | |
11787 | { | |
11788 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); | |
781b0558 KG |
11789 | parse_error_context (wfl_operator, |
11790 | "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'", | |
0a2138e2 | 11791 | lang_printable_name (type, 0)); |
b9f7e36c APB |
11792 | /* If the thrown expression was a reference, we further the |
11793 | compile-time check. */ | |
11794 | if (!JREFERENCE_TYPE_P (type)) | |
11795 | return error_mark_node; | |
11796 | } | |
11797 | ||
11798 | /* At least one of the following must be true */ | |
11799 | ||
11800 | /* The type of the throw expression is a not checked exception, | |
11801 | i.e. is a unchecked expression. */ | |
c877974e | 11802 | unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type)); |
b9f7e36c APB |
11803 | |
11804 | /* Throw is contained in a try statement and at least one catch | |
11805 | clause can receive the thrown expression or the current method is | |
11806 | declared to throw such an exception. Or, the throw statement is | |
11807 | contained in a method or constructor declaration and the type of | |
11808 | the Expression is assignable to at least one type listed in the | |
11809 | throws clause the declaration. */ | |
11810 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); | |
11811 | if (!unchecked_ok) | |
f099f336 | 11812 | tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr)); |
b9f7e36c APB |
11813 | if (!(unchecked_ok || tryblock_throws_ok)) |
11814 | { | |
11815 | /* If there is a surrounding try block that has no matching | |
11816 | clatch clause, report it first. A surrounding try block exits | |
11817 | only if there is something after the list of checked | |
11818 | exception thrown by the current function (if any). */ | |
11819 | if (IN_TRY_BLOCK_P ()) | |
781b0558 | 11820 | parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block", |
0a2138e2 | 11821 | lang_printable_name (type, 0)); |
b9f7e36c APB |
11822 | /* If we have no surrounding try statement and the method doesn't have |
11823 | any throws, report it now. FIXME */ | |
f099f336 APB |
11824 | |
11825 | /* We report that the exception can't be throw from a try block | |
11826 | in all circumstances but when the `throw' is inside a static | |
11827 | block. */ | |
b9f7e36c APB |
11828 | else if (!EXCEPTIONS_P (currently_caught_type_list) |
11829 | && !tryblock_throws_ok) | |
f099f336 | 11830 | { |
7f1d4866 | 11831 | if (IS_CLINIT (current_function_decl)) |
781b0558 KG |
11832 | parse_error_context (wfl_operator, |
11833 | "Checked exception `%s' can't be thrown in initializer", | |
f099f336 APB |
11834 | lang_printable_name (type, 0)); |
11835 | else | |
781b0558 KG |
11836 | parse_error_context (wfl_operator, |
11837 | "Checked exception `%s' isn't thrown from a `try' block", | |
f099f336 APB |
11838 | lang_printable_name (type, 0)); |
11839 | } | |
b9f7e36c APB |
11840 | /* Otherwise, the current method doesn't have the appropriate |
11841 | throws declaration */ | |
11842 | else | |
781b0558 | 11843 | parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)", |
0a2138e2 | 11844 | lang_printable_name (type, 0)); |
b9f7e36c APB |
11845 | return error_mark_node; |
11846 | } | |
11847 | ||
ce6e9147 | 11848 | if (! flag_emit_class_files && ! flag_emit_xref) |
15fdcfe9 | 11849 | BUILD_THROW (node, expr); |
ce6e9147 APB |
11850 | |
11851 | /* If doing xrefs, keep the location where the `throw' was seen. */ | |
11852 | if (flag_emit_xref) | |
11853 | EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1); | |
b9f7e36c APB |
11854 | return node; |
11855 | } | |
11856 | ||
11857 | /* Check that exception said to be thrown by method DECL can be | |
11858 | effectively caught from where DECL is invoked. */ | |
11859 | ||
11860 | static void | |
11861 | check_thrown_exceptions (location, decl) | |
11862 | int location; | |
11863 | tree decl; | |
11864 | { | |
11865 | tree throws; | |
11866 | /* For all the unchecked exceptions thrown by DECL */ | |
11867 | for (throws = DECL_FUNCTION_THROWS (decl); throws; | |
11868 | throws = TREE_CHAIN (throws)) | |
0a2138e2 | 11869 | if (!check_thrown_exceptions_do (TREE_VALUE (throws))) |
b9f7e36c | 11870 | { |
3e78f871 PB |
11871 | #if 1 |
11872 | /* Temporary hack to suppresses errors about cloning arrays. FIXME */ | |
11873 | if (DECL_NAME (decl) == get_identifier ("clone")) | |
11874 | continue; | |
11875 | #endif | |
b9f7e36c | 11876 | EXPR_WFL_LINECOL (wfl_operator) = location; |
7705e9db APB |
11877 | if (DECL_NAME (current_function_decl) == finit_identifier_node) |
11878 | parse_error_context | |
11879 | (wfl_operator, "Exception `%s' can't be thrown in initializer", | |
11880 | lang_printable_name (TREE_VALUE (throws), 0)); | |
11881 | else | |
11882 | { | |
11883 | parse_error_context | |
781b0558 | 11884 | (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'", |
7705e9db APB |
11885 | lang_printable_name (TREE_VALUE (throws), 0), |
11886 | (DECL_NAME (current_function_decl) == init_identifier_node ? | |
11887 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) : | |
11888 | IDENTIFIER_POINTER (DECL_NAME (current_function_decl)))); | |
11889 | } | |
b9f7e36c APB |
11890 | } |
11891 | } | |
11892 | ||
c877974e | 11893 | /* Return 1 if checked EXCEPTION is caught at the current nesting level of |
b9f7e36c APB |
11894 | try-catch blocks, OR is listed in the `throws' clause of the |
11895 | current method. */ | |
11896 | ||
11897 | static int | |
0a2138e2 | 11898 | check_thrown_exceptions_do (exception) |
b9f7e36c APB |
11899 | tree exception; |
11900 | { | |
11901 | tree list = currently_caught_type_list; | |
c877974e | 11902 | resolve_and_layout (exception, NULL_TREE); |
b9f7e36c APB |
11903 | /* First, all the nested try-catch-finally at that stage. The |
11904 | last element contains `throws' clause exceptions, if any. */ | |
c877974e APB |
11905 | if (IS_UNCHECKED_EXCEPTION_P (exception)) |
11906 | return 1; | |
b9f7e36c APB |
11907 | while (list) |
11908 | { | |
11909 | tree caught; | |
11910 | for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught)) | |
11911 | if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0)) | |
11912 | return 1; | |
11913 | list = TREE_CHAIN (list); | |
11914 | } | |
11915 | return 0; | |
11916 | } | |
11917 | ||
11918 | static void | |
11919 | purge_unchecked_exceptions (mdecl) | |
11920 | tree mdecl; | |
11921 | { | |
11922 | tree throws = DECL_FUNCTION_THROWS (mdecl); | |
11923 | tree new = NULL_TREE; | |
11924 | ||
11925 | while (throws) | |
11926 | { | |
11927 | tree next = TREE_CHAIN (throws); | |
c877974e | 11928 | if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws))) |
b9f7e36c APB |
11929 | { |
11930 | TREE_CHAIN (throws) = new; | |
11931 | new = throws; | |
11932 | } | |
11933 | throws = next; | |
11934 | } | |
11935 | /* List is inverted here, but it doesn't matter */ | |
11936 | DECL_FUNCTION_THROWS (mdecl) = new; | |
11937 | } | |
22eed1e6 APB |
11938 | |
11939 | /* 15.24 Conditional Operator ?: */ | |
11940 | ||
11941 | static tree | |
11942 | patch_conditional_expr (node, wfl_cond, wfl_op1) | |
11943 | tree node, wfl_cond, wfl_op1; | |
11944 | { | |
11945 | tree cond = TREE_OPERAND (node, 0); | |
11946 | tree op1 = TREE_OPERAND (node, 1); | |
11947 | tree op2 = TREE_OPERAND (node, 2); | |
22eed1e6 | 11948 | tree resulting_type = NULL_TREE; |
ac825856 | 11949 | tree t1, t2, patched; |
22eed1e6 APB |
11950 | int error_found = 0; |
11951 | ||
ac825856 APB |
11952 | /* Operands of ?: might be StringBuffers crafted as a result of a |
11953 | string concatenation. Obtain a descent operand here. */ | |
11954 | if ((patched = patch_string (op1))) | |
11955 | TREE_OPERAND (node, 1) = op1 = patched; | |
11956 | if ((patched = patch_string (op2))) | |
11957 | TREE_OPERAND (node, 2) = op2 = patched; | |
11958 | ||
11959 | t1 = TREE_TYPE (op1); | |
11960 | t2 = TREE_TYPE (op2); | |
11961 | ||
22eed1e6 APB |
11962 | /* The first expression must be a boolean */ |
11963 | if (TREE_TYPE (cond) != boolean_type_node) | |
11964 | { | |
11965 | SET_WFL_OPERATOR (wfl_operator, node, wfl_cond); | |
781b0558 KG |
11966 | parse_error_context (wfl_operator, |
11967 | "Incompatible type for `?:'. Can't convert `%s' to `boolean'", | |
22eed1e6 APB |
11968 | lang_printable_name (TREE_TYPE (cond), 0)); |
11969 | error_found = 1; | |
11970 | } | |
11971 | ||
11972 | /* Second and third can be numeric, boolean (i.e. primitive), | |
11973 | references or null. Anything else results in an error */ | |
11974 | if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2)) | |
11975 | || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node) | |
11976 | && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node)) | |
11977 | || (t1 == boolean_type_node && t2 == boolean_type_node))) | |
11978 | error_found = 1; | |
11979 | ||
11980 | /* Determine the type of the conditional expression. Same types are | |
11981 | easy to deal with */ | |
11982 | else if (t1 == t2) | |
11983 | resulting_type = t1; | |
11984 | ||
11985 | /* There are different rules for numeric types */ | |
11986 | else if (JNUMERIC_TYPE_P (t1)) | |
11987 | { | |
11988 | /* if byte/short found, the resulting type is short */ | |
11989 | if ((t1 == byte_type_node && t2 == short_type_node) | |
11990 | || (t1 == short_type_node && t2 == byte_type_node)) | |
11991 | resulting_type = short_type_node; | |
11992 | ||
11993 | /* If t1 is a constant int and t2 is of type byte, short or char | |
11994 | and t1's value fits in t2, then the resulting type is t2 */ | |
11995 | else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1))) | |
11996 | && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2)) | |
11997 | resulting_type = t2; | |
11998 | ||
11999 | /* If t2 is a constant int and t1 is of type byte, short or char | |
12000 | and t2's value fits in t1, then the resulting type is t1 */ | |
12001 | else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2))) | |
12002 | && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1)) | |
12003 | resulting_type = t1; | |
12004 | ||
12005 | /* Otherwise, binary numeric promotion is applied and the | |
12006 | resulting type is the promoted type of operand 1 and 2 */ | |
12007 | else | |
93024893 | 12008 | resulting_type = binary_numeric_promotion (t1, t2, |
22eed1e6 APB |
12009 | &TREE_OPERAND (node, 1), |
12010 | &TREE_OPERAND (node, 2)); | |
12011 | } | |
12012 | ||
12013 | /* Cases of a reference and a null type */ | |
12014 | else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node) | |
12015 | resulting_type = t1; | |
12016 | ||
12017 | else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node) | |
12018 | resulting_type = t2; | |
12019 | ||
12020 | /* Last case: different reference types. If a type can be converted | |
12021 | into the other one by assignment conversion, the latter | |
12022 | determines the type of the expression */ | |
12023 | else if ((resulting_type = try_reference_assignconv (t1, op2))) | |
12024 | resulting_type = promote_type (t1); | |
12025 | ||
12026 | else if ((resulting_type = try_reference_assignconv (t2, op1))) | |
12027 | resulting_type = promote_type (t2); | |
12028 | ||
12029 | /* If we don't have any resulting type, we're in trouble */ | |
12030 | if (!resulting_type) | |
12031 | { | |
c2e3db92 | 12032 | char *t = xstrdup (lang_printable_name (t1, 0)); |
22eed1e6 | 12033 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); |
781b0558 KG |
12034 | parse_error_context (wfl_operator, |
12035 | "Incompatible type for `?:'. Can't convert `%s' to `%s'", | |
12036 | t, lang_printable_name (t2, 0)); | |
22eed1e6 APB |
12037 | free (t); |
12038 | error_found = 1; | |
12039 | } | |
12040 | ||
12041 | if (error_found) | |
12042 | { | |
12043 | TREE_TYPE (node) = error_mark_node; | |
12044 | return error_mark_node; | |
12045 | } | |
12046 | ||
12047 | TREE_TYPE (node) = resulting_type; | |
12048 | TREE_SET_CODE (node, COND_EXPR); | |
15fdcfe9 | 12049 | CAN_COMPLETE_NORMALLY (node) = 1; |
22eed1e6 APB |
12050 | return node; |
12051 | } | |
ac825856 | 12052 | |
5b09b33e PB |
12053 | /* Try to constant fold NODE. |
12054 | If NODE is not a constant expression, return NULL_EXPR. | |
12055 | CONTEXT is a static final VAR_DECL whose initializer we are folding. */ | |
12056 | ||
12057 | static tree | |
12058 | fold_constant_for_init (node, context) | |
12059 | tree node; | |
12060 | tree context; | |
12061 | { | |
12062 | tree op0, op1, val; | |
12063 | enum tree_code code = TREE_CODE (node); | |
12064 | ||
93024893 | 12065 | if (code == STRING_CST) |
5b09b33e | 12066 | return node; |
93024893 APB |
12067 | |
12068 | if (code == INTEGER_CST || code == REAL_CST) | |
12069 | return convert (TREE_TYPE (context), node); | |
8576f094 | 12070 | if (TREE_TYPE (node) != NULL_TREE && code != VAR_DECL && code != FIELD_DECL) |
5b09b33e PB |
12071 | return NULL_TREE; |
12072 | ||
12073 | switch (code) | |
12074 | { | |
5b09b33e PB |
12075 | case PLUS_EXPR: |
12076 | case MINUS_EXPR: | |
bc3ca41b PB |
12077 | case MULT_EXPR: |
12078 | case TRUNC_MOD_EXPR: | |
12079 | case RDIV_EXPR: | |
5b09b33e PB |
12080 | case LSHIFT_EXPR: |
12081 | case RSHIFT_EXPR: | |
12082 | case URSHIFT_EXPR: | |
12083 | case BIT_AND_EXPR: | |
12084 | case BIT_XOR_EXPR: | |
12085 | case BIT_IOR_EXPR: | |
5b09b33e PB |
12086 | case TRUTH_ANDIF_EXPR: |
12087 | case TRUTH_ORIF_EXPR: | |
12088 | case EQ_EXPR: | |
12089 | case NE_EXPR: | |
12090 | case GT_EXPR: | |
12091 | case GE_EXPR: | |
12092 | case LT_EXPR: | |
12093 | case LE_EXPR: | |
12094 | op0 = TREE_OPERAND (node, 0); | |
12095 | op1 = TREE_OPERAND (node, 1); | |
12096 | val = fold_constant_for_init (op0, context); | |
12097 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12098 | return NULL_TREE; | |
12099 | TREE_OPERAND (node, 0) = val; | |
12100 | val = fold_constant_for_init (op1, context); | |
12101 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12102 | return NULL_TREE; | |
12103 | TREE_OPERAND (node, 1) = val; | |
12104 | return patch_binop (node, op0, op1); | |
12105 | ||
12106 | case UNARY_PLUS_EXPR: | |
12107 | case NEGATE_EXPR: | |
12108 | case TRUTH_NOT_EXPR: | |
12109 | case BIT_NOT_EXPR: | |
12110 | case CONVERT_EXPR: | |
12111 | op0 = TREE_OPERAND (node, 0); | |
12112 | val = fold_constant_for_init (op0, context); | |
12113 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12114 | return NULL_TREE; | |
12115 | TREE_OPERAND (node, 0) = val; | |
5a005d9e | 12116 | return patch_unaryop (node, op0); |
5b09b33e PB |
12117 | break; |
12118 | ||
12119 | case COND_EXPR: | |
12120 | val = fold_constant_for_init (TREE_OPERAND (node, 0), context); | |
12121 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12122 | return NULL_TREE; | |
12123 | TREE_OPERAND (node, 0) = val; | |
12124 | val = fold_constant_for_init (TREE_OPERAND (node, 1), context); | |
12125 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12126 | return NULL_TREE; | |
12127 | TREE_OPERAND (node, 1) = val; | |
12128 | val = fold_constant_for_init (TREE_OPERAND (node, 2), context); | |
12129 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12130 | return NULL_TREE; | |
12131 | TREE_OPERAND (node, 2) = val; | |
12132 | return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1) | |
12133 | : TREE_OPERAND (node, 2); | |
12134 | ||
12135 | case VAR_DECL: | |
8576f094 APB |
12136 | case FIELD_DECL: |
12137 | if (! FIELD_FINAL (node) | |
5b09b33e PB |
12138 | || DECL_INITIAL (node) == NULL_TREE) |
12139 | return NULL_TREE; | |
12140 | val = DECL_INITIAL (node); | |
12141 | /* Guard against infinite recursion. */ | |
12142 | DECL_INITIAL (node) = NULL_TREE; | |
cd9643f7 | 12143 | val = fold_constant_for_init (val, node); |
5b09b33e PB |
12144 | DECL_INITIAL (node) = val; |
12145 | return val; | |
12146 | ||
12147 | case EXPR_WITH_FILE_LOCATION: | |
12148 | /* Compare java_complete_tree and resolve_expression_name. */ | |
12149 | if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */ | |
12150 | || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE) | |
12151 | { | |
12152 | tree name = EXPR_WFL_NODE (node); | |
12153 | tree decl; | |
12154 | if (PRIMARY_P (node)) | |
12155 | return NULL_TREE; | |
12156 | else if (! QUALIFIED_P (name)) | |
12157 | { | |
12158 | decl = lookup_field_wrapper (DECL_CONTEXT (context), name); | |
8576f094 APB |
12159 | if (decl == NULL_TREE |
12160 | || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl))) | |
5b09b33e PB |
12161 | return NULL_TREE; |
12162 | return fold_constant_for_init (decl, decl); | |
12163 | } | |
12164 | else | |
12165 | { | |
5b09b33e PB |
12166 | /* Wait until the USE_COMPONENT_REF re-write. FIXME. */ |
12167 | qualify_ambiguous_name (node); | |
12168 | if (resolve_field_access (node, &decl, NULL) | |
12169 | && decl != NULL_TREE) | |
12170 | return fold_constant_for_init (decl, decl); | |
5b09b33e PB |
12171 | return NULL_TREE; |
12172 | } | |
12173 | } | |
12174 | else | |
12175 | { | |
12176 | op0 = TREE_OPERAND (node, 0); | |
12177 | val = fold_constant_for_init (op0, context); | |
12178 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12179 | return NULL_TREE; | |
12180 | TREE_OPERAND (node, 0) = val; | |
12181 | return val; | |
12182 | } | |
12183 | ||
bc3ca41b PB |
12184 | #ifdef USE_COMPONENT_REF |
12185 | case IDENTIFIER: | |
12186 | case COMPONENT_REF: | |
12187 | ?; | |
12188 | #endif | |
12189 | ||
5b09b33e PB |
12190 | default: |
12191 | return NULL_TREE; | |
12192 | } | |
12193 | } | |
bc3ca41b PB |
12194 | |
12195 | #ifdef USE_COMPONENT_REF | |
12196 | /* Context is 'T' for TypeName, 'P' for PackageName, | |
12197 | 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */ | |
12198 | ||
12199 | tree | |
12200 | resolve_simple_name (name, context) | |
12201 | tree name; | |
12202 | int context; | |
12203 | { | |
12204 | } | |
12205 | ||
12206 | tree | |
12207 | resolve_qualified_name (name, context) | |
12208 | tree name; | |
12209 | int context; | |
12210 | { | |
12211 | } | |
12212 | #endif |