]>
Commit | Line | Data |
---|---|---|
e04a16fb AG |
1 | /* Source code parsing and tree node generation for the GNU compiler |
2 | for the Java(TM) language. | |
e511adc0 | 3 | Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc. |
e04a16fb AG |
4 | Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com) |
5 | ||
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 | |
fa322ab5 TT |
69 | #ifndef DIR_SEPARATOR |
70 | #define DIR_SEPARATOR '/' | |
71 | #endif | |
72 | ||
82371d41 APB |
73 | /* Local function prototypes */ |
74 | static char *java_accstring_lookup PROTO ((int)); | |
49f48c71 | 75 | static void classitf_redefinition_error PROTO ((const char *,tree, tree, tree)); |
82371d41 | 76 | static void variable_redefinition_error PROTO ((tree, tree, tree, int)); |
49f48c71 | 77 | static void check_modifiers PROTO ((const char *, int, int)); |
82371d41 APB |
78 | static tree create_class PROTO ((int, tree, tree, tree)); |
79 | static tree create_interface PROTO ((int, tree, tree)); | |
80 | static tree find_field PROTO ((tree, tree)); | |
81 | static tree lookup_field_wrapper PROTO ((tree, tree)); | |
82 | static int duplicate_declaration_error_p PROTO ((tree, tree, tree)); | |
83 | static void register_fields PROTO ((int, tree, tree)); | |
84 | static tree parser_qualified_classname PROTO ((tree)); | |
85 | static int parser_check_super PROTO ((tree, tree, tree)); | |
86 | static int parser_check_super_interface PROTO ((tree, tree, tree)); | |
87 | static void check_modifiers_consistency PROTO ((int)); | |
88 | static tree lookup_cl PROTO ((tree)); | |
89 | static tree lookup_java_method2 PROTO ((tree, tree, int)); | |
90 | static tree method_header PROTO ((int, tree, tree, tree)); | |
91 | static void fix_method_argument_names PROTO ((tree ,tree)); | |
92 | static tree method_declarator PROTO ((tree, tree)); | |
d4476be2 KG |
93 | static void parse_warning_context PVPROTO ((tree cl, const char *msg, ...)) |
94 | ATTRIBUTE_PRINTF_2; | |
95 | static void issue_warning_error_from_context PROTO ((tree, const char *msg, va_list)); | |
8119c720 | 96 | static void parse_ctor_invocation_error PROTO ((void)); |
49f48c71 | 97 | static tree parse_jdk1_1_error PROTO ((const char *)); |
82371d41 APB |
98 | static void complete_class_report_errors PROTO ((jdep *)); |
99 | static int process_imports PROTO ((void)); | |
100 | static void read_import_dir PROTO ((tree)); | |
101 | static int find_in_imports_on_demand PROTO ((tree)); | |
102 | static int find_in_imports PROTO ((tree)); | |
103 | static int check_pkg_class_access PROTO ((tree, tree)); | |
104 | static tree resolve_package PROTO ((tree, tree *)); | |
49f48c71 KG |
105 | static tree lookup_package_type PROTO ((const char *, int)); |
106 | static tree lookup_package_type_and_set_next PROTO ((const char *, int, tree *)); | |
82371d41 | 107 | static tree resolve_class PROTO ((tree, tree, tree)); |
82371d41 APB |
108 | static void declare_local_variables PROTO ((int, tree, tree)); |
109 | static void source_start_java_method PROTO ((tree)); | |
110 | static void source_end_java_method PROTO ((void)); | |
111 | static void expand_start_java_method PROTO ((tree)); | |
112 | static tree find_name_in_single_imports PROTO ((tree)); | |
113 | static void check_abstract_method_header PROTO ((tree)); | |
114 | static tree lookup_java_interface_method2 PROTO ((tree, tree)); | |
115 | static tree resolve_expression_name PROTO ((tree, tree *)); | |
116 | static tree maybe_create_class_interface_decl PROTO ((tree, tree, tree)); | |
117 | static int check_class_interface_creation PROTO ((int, int, tree, | |
118 | tree, tree, tree)); | |
119 | static tree patch_method_invocation PROTO ((tree, tree, tree, | |
89e09b9a | 120 | int *, tree *)); |
82371d41 APB |
121 | static int breakdown_qualified PROTO ((tree *, tree *, tree)); |
122 | static tree resolve_and_layout PROTO ((tree, tree)); | |
123 | static tree resolve_no_layout PROTO ((tree, tree)); | |
124 | static int invocation_mode PROTO ((tree, int)); | |
125 | static tree find_applicable_accessible_methods_list PROTO ((int, tree, | |
126 | tree, tree)); | |
1982388a APB |
127 | static void search_applicable_methods_list PROTO ((int, tree, tree, tree, |
128 | tree *, tree *)); | |
82371d41 APB |
129 | static tree find_most_specific_methods_list PROTO ((tree)); |
130 | static int argument_types_convertible PROTO ((tree, tree)); | |
89e09b9a | 131 | static tree patch_invoke PROTO ((tree, tree, tree)); |
82371d41 APB |
132 | static tree lookup_method_invoke PROTO ((int, tree, tree, tree, tree)); |
133 | static tree register_incomplete_type PROTO ((int, tree, tree, tree)); | |
134 | static tree obtain_incomplete_type PROTO ((tree)); | |
5b09b33e | 135 | static tree java_complete_lhs PROTO ((tree)); |
82371d41 APB |
136 | static tree java_complete_tree PROTO ((tree)); |
137 | static void java_complete_expand_method PROTO ((tree)); | |
138 | static int unresolved_type_p PROTO ((tree, tree *)); | |
139 | static void create_jdep_list PROTO ((struct parser_ctxt *)); | |
140 | static tree build_expr_block PROTO ((tree, tree)); | |
141 | static tree enter_block PROTO ((void)); | |
142 | static tree enter_a_block PROTO ((tree)); | |
143 | static tree exit_block PROTO ((void)); | |
144 | static tree lookup_name_in_blocks PROTO ((tree)); | |
145 | static void maybe_absorb_scoping_blocks PROTO ((void)); | |
146 | static tree build_method_invocation PROTO ((tree, tree)); | |
147 | static tree build_new_invocation PROTO ((tree, tree)); | |
148 | static tree build_assignment PROTO ((int, int, tree, tree)); | |
149 | static tree build_binop PROTO ((enum tree_code, int, tree, tree)); | |
150 | static int check_final_assignment PROTO ((tree ,tree)); | |
151 | static tree patch_assignment PROTO ((tree, tree, tree )); | |
152 | static tree patch_binop PROTO ((tree, tree, tree)); | |
153 | static tree build_unaryop PROTO ((int, int, tree)); | |
154 | static tree build_incdec PROTO ((int, int, tree, int)); | |
155 | static tree patch_unaryop PROTO ((tree, tree)); | |
156 | static tree build_cast PROTO ((int, tree, tree)); | |
157 | static tree build_null_of_type PROTO ((tree)); | |
158 | static tree patch_cast PROTO ((tree, tree)); | |
159 | static int valid_ref_assignconv_cast_p PROTO ((tree, tree, int)); | |
160 | static int valid_builtin_assignconv_identity_widening_p PROTO ((tree, tree)); | |
161 | static int valid_cast_to_p PROTO ((tree, tree)); | |
162 | static int valid_method_invocation_conversion_p PROTO ((tree, tree)); | |
163 | static tree try_builtin_assignconv PROTO ((tree, tree, tree)); | |
164 | static tree try_reference_assignconv PROTO ((tree, tree)); | |
165 | static tree build_unresolved_array_type PROTO ((tree)); | |
166 | static tree build_array_from_name PROTO ((tree, tree, tree, tree *)); | |
167 | static tree build_array_ref PROTO ((int, tree, tree)); | |
168 | static tree patch_array_ref PROTO ((tree)); | |
169 | static tree make_qualified_name PROTO ((tree, tree, int)); | |
170 | static tree merge_qualified_name PROTO ((tree, tree)); | |
171 | static tree make_qualified_primary PROTO ((tree, tree, int)); | |
172 | static int resolve_qualified_expression_name PROTO ((tree, tree *, | |
173 | tree *, tree *)); | |
174 | static void qualify_ambiguous_name PROTO ((tree)); | |
175 | static void maybe_generate_clinit PROTO ((void)); | |
176 | static tree resolve_field_access PROTO ((tree, tree *, tree *)); | |
177 | static tree build_newarray_node PROTO ((tree, tree, int)); | |
178 | static tree patch_newarray PROTO ((tree)); | |
179 | static tree resolve_type_during_patch PROTO ((tree)); | |
180 | static tree build_this PROTO ((int)); | |
181 | static tree build_return PROTO ((int, tree)); | |
182 | static tree patch_return PROTO ((tree)); | |
183 | static tree maybe_access_field PROTO ((tree, tree, tree)); | |
184 | static int complete_function_arguments PROTO ((tree)); | |
185 | static int check_for_static_method_reference PROTO ((tree, tree, tree, tree, tree)); | |
186 | static int not_accessible_p PROTO ((tree, tree, int)); | |
187 | static void check_deprecation PROTO ((tree, tree)); | |
188 | static int class_in_current_package PROTO ((tree)); | |
189 | static tree build_if_else_statement PROTO ((int, tree, tree, tree)); | |
190 | static tree patch_if_else_statement PROTO ((tree)); | |
191 | static tree add_stmt_to_compound PROTO ((tree, tree, tree)); | |
192 | static tree add_stmt_to_block PROTO ((tree, tree, tree)); | |
193 | static tree patch_exit_expr PROTO ((tree)); | |
194 | static tree build_labeled_block PROTO ((int, tree)); | |
b635eb2f | 195 | static tree finish_labeled_statement PROTO ((tree, tree)); |
82371d41 APB |
196 | static tree build_bc_statement PROTO ((int, int, tree)); |
197 | static tree patch_bc_statement PROTO ((tree)); | |
198 | static tree patch_loop_statement PROTO ((tree)); | |
199 | static tree build_new_loop PROTO ((tree)); | |
200 | static tree build_loop_body PROTO ((int, tree, int)); | |
b635eb2f | 201 | static tree finish_loop_body PROTO ((int, tree, tree, int)); |
82371d41 | 202 | static tree build_debugable_stmt PROTO ((int, tree)); |
b635eb2f | 203 | static tree finish_for_loop PROTO ((int, tree, tree, tree)); |
82371d41 APB |
204 | static tree patch_switch_statement PROTO ((tree)); |
205 | static tree string_constant_concatenation PROTO ((tree, tree)); | |
206 | static tree build_string_concatenation PROTO ((tree, tree)); | |
207 | static tree patch_string_cst PROTO ((tree)); | |
208 | static tree patch_string PROTO ((tree)); | |
a7d8d81f PB |
209 | static tree build_try_statement PROTO ((int, tree, tree)); |
210 | static tree build_try_finally_statement PROTO ((int, tree, tree)); | |
82371d41 APB |
211 | static tree patch_try_statement PROTO ((tree)); |
212 | static tree patch_synchronized_statement PROTO ((tree, tree)); | |
213 | static tree patch_throw_statement PROTO ((tree, tree)); | |
214 | static void check_thrown_exceptions PROTO ((int, tree)); | |
215 | static int check_thrown_exceptions_do PROTO ((tree)); | |
216 | static void purge_unchecked_exceptions PROTO ((tree)); | |
217 | static void check_throws_clauses PROTO ((tree, tree, tree)); | |
b635eb2f | 218 | static void finish_method_declaration PROTO ((tree)); |
49f48c71 | 219 | static tree build_super_invocation PROTO ((void)); |
82371d41 APB |
220 | static int verify_constructor_circularity PROTO ((tree, tree)); |
221 | static char *constructor_circularity_msg PROTO ((tree, tree)); | |
222 | static tree build_this_super_qualified_invocation PROTO ((int, tree, tree, | |
223 | int, int)); | |
49f48c71 | 224 | static const char *get_printable_method_name PROTO ((tree)); |
82371d41 | 225 | static tree patch_conditional_expr PROTO ((tree, tree, tree)); |
49f48c71 | 226 | static void maybe_generate_finit PROTO ((void)); |
82371d41 | 227 | static void fix_constructors PROTO ((tree)); |
49f48c71 | 228 | static int verify_constructor_super PROTO ((void)); |
82371d41 APB |
229 | static tree create_artificial_method PROTO ((tree, int, tree, tree, tree)); |
230 | static void start_artificial_method_body PROTO ((tree)); | |
231 | static void end_artificial_method_body PROTO ((tree)); | |
82371d41 APB |
232 | static int check_method_redefinition PROTO ((tree, tree)); |
233 | static int reset_method_name PROTO ((tree)); | |
234 | static void java_check_regular_methods PROTO ((tree)); | |
235 | static void java_check_abstract_methods PROTO ((tree)); | |
236 | static tree maybe_build_primttype_type_ref PROTO ((tree, tree)); | |
237 | static void unreachable_stmt_error PROTO ((tree)); | |
238 | static tree find_expr_with_wfl PROTO ((tree)); | |
239 | static void missing_return_error PROTO ((tree)); | |
f8976021 APB |
240 | static tree build_new_array_init PROTO ((int, tree)); |
241 | static tree patch_new_array_init PROTO ((tree, tree)); | |
f8976021 APB |
242 | static tree maybe_build_array_element_wfl PROTO ((tree)); |
243 | static int array_constructor_check_entry PROTO ((tree, tree)); | |
49f48c71 | 244 | static const char *purify_type_name PROTO ((const char *)); |
5b09b33e | 245 | static tree fold_constant_for_init PROTO ((tree, tree)); |
e28cd97b | 246 | static tree strip_out_static_field_access_decl PROTO ((tree)); |
7e21fe59 | 247 | static jdeplist *reverse_jdep_list PROTO ((struct parser_ctxt *)); |
7f10c2e2 | 248 | static void static_ref_err PROTO ((tree, tree, tree)); |
49f48c71 KG |
249 | static void parser_add_interface PROTO ((tree, tree, tree)); |
250 | static void add_superinterfaces PROTO ((tree, tree)); | |
251 | static tree jdep_resolve_class PROTO ((jdep *)); | |
252 | static int note_possible_classname PROTO ((const char *, int)); | |
253 | static void java_complete_expand_methods PROTO ((void)); | |
254 | static void java_expand_finals PROTO ((void)); | |
255 | static tree cut_identifier_in_qualified PROTO ((tree)); | |
256 | static tree java_stabilize_reference PROTO ((tree)); | |
257 | static tree do_unary_numeric_promotion PROTO ((tree)); | |
258 | static char * operator_string PROTO ((tree)); | |
259 | static tree do_merge_string_cste PROTO ((tree, const char *, int, int)); | |
260 | static tree merge_string_cste PROTO ((tree, tree, int)); | |
5cbdba64 | 261 | static tree java_refold PROTO ((tree)); |
be245ac0 KG |
262 | static int java_decl_equiv PROTO ((tree, tree)); |
263 | static int binop_compound_p PROTO ((enum tree_code)); | |
264 | static tree search_loop PROTO ((tree)); | |
265 | static int labeled_block_contains_loop_p PROTO ((tree, tree)); | |
d593dd8c KG |
266 | static void check_abstract_method_definitions PROTO ((int, tree, tree)); |
267 | static void java_check_abstract_method_definitions PROTO ((tree)); | |
82371d41 | 268 | |
e04a16fb AG |
269 | /* Number of error found so far. */ |
270 | int java_error_count; | |
271 | /* Number of warning found so far. */ | |
272 | int java_warning_count; | |
ce6e9147 APB |
273 | /* Tell when not to fold, when doing xrefs */ |
274 | int do_not_fold; | |
e04a16fb AG |
275 | |
276 | /* The current parser context */ | |
d4370213 | 277 | struct parser_ctxt *ctxp; |
e04a16fb | 278 | |
d4370213 | 279 | /* List of things that were analyzed for which code will be generated */ |
b351b287 APB |
280 | static struct parser_ctxt *ctxp_for_generation = NULL; |
281 | ||
e04a16fb AG |
282 | /* binop_lookup maps token to tree_code. It is used where binary |
283 | operations are involved and required by the parser. RDIV_EXPR | |
284 | covers both integral/floating point division. The code is changed | |
285 | once the type of both operator is worked out. */ | |
286 | ||
287 | static enum tree_code binop_lookup[19] = | |
288 | { | |
289 | PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR, | |
290 | LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR, | |
291 | BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR, | |
292 | TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR, | |
293 | EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR, | |
294 | }; | |
295 | #define BINOP_LOOKUP(VALUE) \ | |
296 | binop_lookup [((VALUE) - PLUS_TK)% \ | |
297 | (sizeof (binop_lookup) / sizeof (binop_lookup[0]))] | |
298 | ||
5cbdba64 APB |
299 | /* This is the end index for binary operators that can also be used |
300 | in compound assignements. */ | |
301 | #define BINOP_COMPOUND_CANDIDATES 11 | |
302 | ||
e04a16fb AG |
303 | /* Fake WFL used to report error message. It is initialized once if |
304 | needed and reused with it's location information is overriden. */ | |
15fdcfe9 | 305 | tree wfl_operator = NULL_TREE; |
e04a16fb AG |
306 | |
307 | /* The "$L" identifier we use to create labels. */ | |
b67d701b PB |
308 | static tree label_id = NULL_TREE; |
309 | ||
310 | /* The "StringBuffer" identifier used for the String `+' operator. */ | |
311 | static tree wfl_string_buffer = NULL_TREE; | |
312 | ||
313 | /* The "append" identifier used for String `+' operator. */ | |
314 | static tree wfl_append = NULL_TREE; | |
315 | ||
316 | /* The "toString" identifier used for String `+' operator. */ | |
317 | static tree wfl_to_string = NULL_TREE; | |
ba179f9f APB |
318 | |
319 | /* The "java.lang" import qualified name. */ | |
320 | static tree java_lang_id = NULL_TREE; | |
09ed0f70 APB |
321 | |
322 | /* The "java.lang.Cloneable" qualified name. */ | |
323 | static tree java_lang_cloneable = NULL_TREE; | |
f099f336 APB |
324 | |
325 | /* Context and flag for static blocks */ | |
326 | static tree current_static_block = NULL_TREE; | |
327 | ||
e04a16fb AG |
328 | %} |
329 | ||
330 | %union { | |
331 | tree node; | |
332 | int sub_token; | |
333 | struct { | |
334 | int token; | |
335 | int location; | |
336 | } operator; | |
337 | int value; | |
338 | } | |
339 | ||
9ee9b555 KG |
340 | %{ |
341 | #include "lex.c" | |
342 | %} | |
343 | ||
e04a16fb AG |
344 | %pure_parser |
345 | ||
346 | /* Things defined here have to match the order of what's in the | |
347 | binop_lookup table. */ | |
348 | ||
349 | %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK | |
350 | %token LS_TK SRS_TK ZRS_TK | |
351 | %token AND_TK XOR_TK OR_TK | |
352 | %token BOOL_AND_TK BOOL_OR_TK | |
353 | %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK | |
354 | ||
355 | /* This maps to the same binop_lookup entry than the token above */ | |
356 | ||
357 | %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK | |
358 | %token REM_ASSIGN_TK | |
359 | %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK | |
360 | %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK | |
361 | ||
362 | ||
363 | /* Modifier TOKEN have to be kept in this order. Don't scramble it */ | |
364 | ||
365 | %token PUBLIC_TK PRIVATE_TK PROTECTED_TK | |
366 | %token STATIC_TK FINAL_TK SYNCHRONIZED_TK | |
367 | %token VOLATILE_TK TRANSIENT_TK NATIVE_TK | |
368 | %token PAD_TK ABSTRACT_TK MODIFIER_TK | |
369 | ||
370 | /* Keep those two in order, too */ | |
371 | %token DECR_TK INCR_TK | |
372 | ||
373 | /* From now one, things can be in any order */ | |
374 | ||
375 | %token DEFAULT_TK IF_TK THROW_TK | |
376 | %token BOOLEAN_TK DO_TK IMPLEMENTS_TK | |
377 | %token THROWS_TK BREAK_TK IMPORT_TK | |
378 | %token ELSE_TK INSTANCEOF_TK RETURN_TK | |
379 | %token VOID_TK CATCH_TK INTERFACE_TK | |
380 | %token CASE_TK EXTENDS_TK FINALLY_TK | |
381 | %token SUPER_TK WHILE_TK CLASS_TK | |
382 | %token SWITCH_TK CONST_TK TRY_TK | |
383 | %token FOR_TK NEW_TK CONTINUE_TK | |
384 | %token GOTO_TK PACKAGE_TK THIS_TK | |
385 | ||
386 | %token BYTE_TK SHORT_TK INT_TK LONG_TK | |
387 | %token CHAR_TK INTEGRAL_TK | |
388 | ||
389 | %token FLOAT_TK DOUBLE_TK FP_TK | |
390 | ||
391 | %token ID_TK | |
392 | ||
393 | %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK | |
394 | ||
395 | %token ASSIGN_ANY_TK ASSIGN_TK | |
396 | %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK | |
397 | ||
398 | %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK | |
399 | %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK | |
400 | ||
401 | %type <value> modifiers MODIFIER_TK | |
402 | ||
403 | %type <node> super ID_TK identifier | |
404 | %type <node> name simple_name qualified_name | |
405 | %type <node> class_declaration type_declaration compilation_unit | |
406 | field_declaration method_declaration extends_interfaces | |
407 | interfaces interface_type_list | |
408 | interface_declaration class_member_declaration | |
409 | import_declarations package_declaration | |
410 | type_declarations interface_body | |
411 | interface_member_declaration constant_declaration | |
412 | interface_member_declarations interface_type | |
413 | abstract_method_declaration interface_type_list | |
414 | %type <node> class_body_declaration class_member_declaration | |
415 | static_initializer constructor_declaration block | |
22eed1e6 | 416 | %type <node> class_body_declarations constructor_header |
e04a16fb AG |
417 | %type <node> class_or_interface_type class_type class_type_list |
418 | constructor_declarator explicit_constructor_invocation | |
b9f7e36c | 419 | %type <node> dim_expr dim_exprs this_or_super throws |
e04a16fb AG |
420 | |
421 | %type <node> variable_declarator_id variable_declarator | |
422 | variable_declarators variable_initializer | |
22eed1e6 | 423 | variable_initializers constructor_body |
ac825856 | 424 | array_initializer |
e04a16fb | 425 | |
2e5eb5c5 | 426 | %type <node> class_body block_end constructor_block_end |
e04a16fb AG |
427 | %type <node> statement statement_without_trailing_substatement |
428 | labeled_statement if_then_statement label_decl | |
429 | if_then_else_statement while_statement for_statement | |
430 | statement_nsi labeled_statement_nsi do_statement | |
431 | if_then_else_statement_nsi while_statement_nsi | |
432 | for_statement_nsi statement_expression_list for_init | |
433 | for_update statement_expression expression_statement | |
434 | primary_no_new_array expression primary | |
435 | array_creation_expression array_type | |
436 | class_instance_creation_expression field_access | |
437 | method_invocation array_access something_dot_new | |
438 | argument_list postfix_expression while_expression | |
439 | post_increment_expression post_decrement_expression | |
440 | unary_expression_not_plus_minus unary_expression | |
441 | pre_increment_expression pre_decrement_expression | |
442 | unary_expression_not_plus_minus cast_expression | |
443 | multiplicative_expression additive_expression | |
444 | shift_expression relational_expression | |
445 | equality_expression and_expression | |
446 | exclusive_or_expression inclusive_or_expression | |
447 | conditional_and_expression conditional_or_expression | |
448 | conditional_expression assignment_expression | |
449 | left_hand_side assignment for_header for_begin | |
450 | constant_expression do_statement_begin empty_statement | |
b67d701b | 451 | switch_statement synchronized_statement throw_statement |
f8976021 | 452 | try_statement switch_expression switch_block |
15fdcfe9 | 453 | catches catch_clause catch_clause_parameter finally |
e04a16fb AG |
454 | %type <node> return_statement break_statement continue_statement |
455 | ||
456 | %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK | |
457 | %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK | |
458 | %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK | |
459 | %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK | |
460 | %type <operator> ASSIGN_ANY_TK assignment_operator | |
461 | %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK | |
462 | %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK | |
463 | %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK | |
7f10c2e2 | 464 | %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK |
5e942c50 | 465 | %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK |
b9f7e36c APB |
466 | %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK |
467 | %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK | |
e04a16fb AG |
468 | |
469 | %type <node> method_body | |
470 | ||
471 | %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK | |
472 | STRING_LIT_TK NULL_TK VOID_TK | |
473 | ||
474 | %type <node> IF_TK WHILE_TK FOR_TK | |
475 | ||
476 | %type <node> formal_parameter_list formal_parameter | |
477 | method_declarator method_header | |
478 | ||
479 | %type <node> primitive_type reference_type type | |
480 | BOOLEAN_TK INTEGRAL_TK FP_TK | |
481 | ||
482 | %% | |
483 | /* 19.2 Production from 2.3: The Syntactic Grammar */ | |
484 | goal: | |
485 | compilation_unit | |
486 | {} | |
487 | ; | |
488 | ||
489 | /* 19.3 Productions from 3: Lexical structure */ | |
490 | literal: | |
491 | INT_LIT_TK | |
492 | | FP_LIT_TK | |
493 | | BOOL_LIT_TK | |
494 | | CHAR_LIT_TK | |
495 | | STRING_LIT_TK | |
496 | | NULL_TK | |
497 | ; | |
498 | ||
499 | /* 19.4 Productions from 4: Types, Values and Variables */ | |
500 | type: | |
501 | primitive_type | |
502 | | reference_type | |
503 | ; | |
504 | ||
505 | primitive_type: | |
506 | INTEGRAL_TK | |
507 | | FP_TK | |
508 | | BOOLEAN_TK | |
509 | ; | |
510 | ||
511 | reference_type: | |
512 | class_or_interface_type | |
513 | | array_type | |
514 | ; | |
515 | ||
516 | class_or_interface_type: | |
517 | name | |
518 | ; | |
519 | ||
520 | class_type: | |
521 | class_or_interface_type /* Default rule */ | |
522 | ; | |
523 | ||
524 | interface_type: | |
525 | class_or_interface_type | |
526 | ; | |
527 | ||
528 | array_type: | |
529 | primitive_type OSB_TK CSB_TK | |
530 | { | |
531 | $$ = build_java_array_type ($1, -1); | |
532 | CLASS_LOADED_P ($$) = 1; | |
533 | } | |
534 | | name OSB_TK CSB_TK | |
535 | { $$ = build_unresolved_array_type ($1); } | |
536 | | array_type OSB_TK CSB_TK | |
537 | { $$ = build_unresolved_array_type ($1); } | |
538 | | primitive_type OSB_TK error | |
539 | {RULE ("']' expected"); RECOVER;} | |
540 | | array_type OSB_TK error | |
541 | {RULE ("']' expected"); RECOVER;} | |
542 | ; | |
543 | ||
544 | /* 19.5 Productions from 6: Names */ | |
545 | name: | |
546 | simple_name /* Default rule */ | |
547 | | qualified_name /* Default rule */ | |
548 | ; | |
549 | ||
550 | simple_name: | |
551 | identifier /* Default rule */ | |
552 | ; | |
553 | ||
554 | qualified_name: | |
555 | name DOT_TK identifier | |
556 | { $$ = make_qualified_name ($1, $3, $2.location); } | |
557 | ; | |
558 | ||
559 | identifier: | |
560 | ID_TK | |
561 | ; | |
562 | ||
563 | /* 19.6: Production from 7: Packages */ | |
564 | compilation_unit: | |
565 | {$$ = NULL;} | |
566 | | package_declaration | |
567 | | import_declarations | |
568 | | type_declarations | |
569 | | package_declaration import_declarations | |
570 | | package_declaration type_declarations | |
571 | | import_declarations type_declarations | |
572 | | package_declaration import_declarations type_declarations | |
573 | ; | |
574 | ||
575 | import_declarations: | |
576 | import_declaration | |
577 | { | |
578 | $$ = NULL; | |
579 | } | |
580 | | import_declarations import_declaration | |
581 | { | |
582 | $$ = NULL; | |
583 | } | |
584 | ; | |
585 | ||
586 | type_declarations: | |
587 | type_declaration | |
588 | | type_declarations type_declaration | |
589 | ; | |
590 | ||
591 | package_declaration: | |
592 | PACKAGE_TK name SC_TK | |
593 | { ctxp->package = EXPR_WFL_NODE ($2); } | |
594 | | PACKAGE_TK error | |
595 | {yyerror ("Missing name"); RECOVER;} | |
596 | | PACKAGE_TK name error | |
597 | {yyerror ("';' expected"); RECOVER;} | |
598 | ; | |
599 | ||
600 | import_declaration: | |
601 | single_type_import_declaration | |
602 | | type_import_on_demand_declaration | |
603 | ; | |
604 | ||
605 | single_type_import_declaration: | |
606 | IMPORT_TK name SC_TK | |
607 | { | |
608 | tree name = EXPR_WFL_NODE ($2), node, last_name; | |
609 | int i = IDENTIFIER_LENGTH (name)-1; | |
49f48c71 | 610 | const char *last = &IDENTIFIER_POINTER (name)[i]; |
e04a16fb AG |
611 | while (last != IDENTIFIER_POINTER (name)) |
612 | { | |
613 | if (last [0] == '.') | |
614 | break; | |
615 | last--; | |
616 | } | |
617 | last_name = get_identifier (++last); | |
618 | if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name)) | |
619 | { | |
620 | tree err = find_name_in_single_imports (last_name); | |
621 | if (err && err != name) | |
622 | parse_error_context | |
623 | ($2, "Ambiguous class: `%s' and `%s'", | |
624 | IDENTIFIER_POINTER (name), | |
625 | IDENTIFIER_POINTER (err)); | |
5e942c50 APB |
626 | else |
627 | REGISTER_IMPORT ($2, last_name) | |
e04a16fb AG |
628 | } |
629 | else | |
5e942c50 | 630 | REGISTER_IMPORT ($2, last_name); |
e04a16fb AG |
631 | } |
632 | | IMPORT_TK error | |
633 | {yyerror ("Missing name"); RECOVER;} | |
634 | | IMPORT_TK name error | |
635 | {yyerror ("';' expected"); RECOVER;} | |
636 | ; | |
637 | ||
638 | type_import_on_demand_declaration: | |
639 | IMPORT_TK name DOT_TK MULT_TK SC_TK | |
640 | { | |
641 | tree name = EXPR_WFL_NODE ($2); | |
ba179f9f APB |
642 | /* Don't import java.lang.* twice. */ |
643 | if (name != java_lang_id) | |
644 | { | |
645 | tree node = build_tree_list ($2, NULL_TREE); | |
646 | read_import_dir ($2); | |
647 | TREE_CHAIN (node) = ctxp->import_demand_list; | |
648 | ctxp->import_demand_list = node; | |
649 | } | |
e04a16fb AG |
650 | } |
651 | | IMPORT_TK name DOT_TK error | |
652 | {yyerror ("'*' expected"); RECOVER;} | |
653 | | IMPORT_TK name DOT_TK MULT_TK error | |
654 | {yyerror ("';' expected"); RECOVER;} | |
655 | ; | |
656 | ||
657 | type_declaration: | |
658 | class_declaration | |
659 | { | |
22eed1e6 | 660 | maybe_generate_finit (); |
7525cc04 | 661 | maybe_generate_clinit (); |
e04a16fb AG |
662 | $$ = $1; |
663 | } | |
664 | | interface_declaration | |
7f1d4866 APB |
665 | { |
666 | maybe_generate_clinit (); | |
667 | $$ = $1; | |
668 | } | |
e04a16fb AG |
669 | | SC_TK |
670 | { $$ = NULL; } | |
671 | | error | |
672 | { | |
673 | YYERROR_NOW; | |
674 | yyerror ("Class or interface declaration expected"); | |
675 | } | |
676 | ; | |
677 | ||
678 | /* 19.7 Shortened from the original: | |
679 | modifiers: modifier | modifiers modifier | |
680 | modifier: any of public... */ | |
681 | modifiers: | |
682 | MODIFIER_TK | |
683 | { | |
684 | $$ = (1 << $1); | |
685 | } | |
686 | | modifiers MODIFIER_TK | |
687 | { | |
688 | int acc = (1 << $2); | |
689 | if ($$ & acc) | |
690 | parse_error_context | |
691 | (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice", | |
692 | java_accstring_lookup (acc)); | |
693 | else | |
694 | { | |
695 | $$ |= acc; | |
696 | } | |
697 | } | |
698 | ; | |
699 | ||
700 | /* 19.8.1 Production from $8.1: Class Declaration */ | |
701 | class_declaration: | |
702 | modifiers CLASS_TK identifier super interfaces | |
703 | { create_class ($1, $3, $4, $5); } | |
704 | class_body | |
705 | { | |
706 | $$ = $7; | |
707 | } | |
708 | | CLASS_TK identifier super interfaces | |
709 | { create_class (0, $2, $3, $4); } | |
710 | class_body | |
711 | { | |
712 | $$ = $6; | |
713 | } | |
714 | | modifiers CLASS_TK error | |
715 | {yyerror ("Missing class name"); RECOVER;} | |
716 | | CLASS_TK error | |
717 | {yyerror ("Missing class name"); RECOVER;} | |
718 | | CLASS_TK identifier error | |
0b4d333e APB |
719 | { |
720 | if (!ctxp->class_err) yyerror ("'{' expected"); | |
721 | DRECOVER(class1); | |
722 | } | |
e04a16fb AG |
723 | | modifiers CLASS_TK identifier error |
724 | {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;} | |
725 | ; | |
726 | ||
727 | super: | |
728 | { $$ = NULL; } | |
729 | | EXTENDS_TK class_type | |
730 | { $$ = $2; } | |
731 | | EXTENDS_TK class_type error | |
732 | {yyerror ("'{' expected"); ctxp->class_err=1;} | |
733 | | EXTENDS_TK error | |
734 | {yyerror ("Missing super class name"); ctxp->class_err=1;} | |
735 | ; | |
736 | ||
737 | interfaces: | |
738 | { $$ = NULL_TREE; } | |
739 | | IMPLEMENTS_TK interface_type_list | |
740 | { $$ = $2; } | |
741 | | IMPLEMENTS_TK error | |
742 | { | |
743 | ctxp->class_err=1; | |
744 | yyerror ("Missing interface name"); | |
745 | } | |
746 | ; | |
747 | ||
748 | interface_type_list: | |
749 | interface_type | |
750 | { | |
751 | ctxp->interface_number = 1; | |
752 | $$ = build_tree_list ($1, NULL_TREE); | |
753 | } | |
754 | | interface_type_list C_TK interface_type | |
755 | { | |
756 | ctxp->interface_number++; | |
757 | $$ = chainon ($1, build_tree_list ($3, NULL_TREE)); | |
758 | } | |
759 | | interface_type_list C_TK error | |
760 | {yyerror ("Missing interface name"); RECOVER;} | |
761 | ; | |
762 | ||
763 | class_body: | |
764 | OCB_TK CCB_TK | |
7f10c2e2 APB |
765 | { |
766 | /* Store the location of the `}' when doing xrefs */ | |
767 | if (flag_emit_xref) | |
768 | DECL_END_SOURCE_LINE (ctxp->current_parsed_class) = | |
769 | EXPR_WFL_ADD_COL ($2.location, 1); | |
770 | $$ = ctxp->current_parsed_class; | |
771 | } | |
e04a16fb | 772 | | OCB_TK class_body_declarations CCB_TK |
7f10c2e2 APB |
773 | { |
774 | /* Store the location of the `}' when doing xrefs */ | |
775 | if (flag_emit_xref) | |
776 | DECL_END_SOURCE_LINE (ctxp->current_parsed_class) = | |
777 | EXPR_WFL_ADD_COL ($3.location, 1); | |
778 | $$ = ctxp->current_parsed_class; | |
779 | } | |
e04a16fb AG |
780 | ; |
781 | ||
782 | class_body_declarations: | |
783 | class_body_declaration | |
784 | | class_body_declarations class_body_declaration | |
785 | ; | |
786 | ||
787 | class_body_declaration: | |
788 | class_member_declaration | |
789 | | static_initializer | |
790 | | constructor_declaration | |
791 | | block /* Added, JDK1.1, instance initializer */ | |
b67d701b | 792 | { $$ = parse_jdk1_1_error ("instance initializer"); } |
e04a16fb AG |
793 | ; |
794 | ||
795 | class_member_declaration: | |
796 | field_declaration | |
0b4d333e APB |
797 | | field_declaration SC_TK |
798 | { $$ = $1; } | |
e04a16fb AG |
799 | | method_declaration |
800 | | class_declaration /* Added, JDK1.1 inner classes */ | |
b67d701b | 801 | { $$ = parse_jdk1_1_error ("inner classe declaration"); } |
e04a16fb | 802 | | interface_declaration /* Added, JDK1.1 inner classes */ |
b67d701b | 803 | { $$ = parse_jdk1_1_error ("inner interface declaration"); } |
e04a16fb AG |
804 | ; |
805 | ||
806 | /* 19.8.2 Productions from 8.3: Field Declarations */ | |
807 | field_declaration: | |
808 | type variable_declarators SC_TK | |
809 | { register_fields (0, $1, $2); } | |
810 | | modifiers type variable_declarators SC_TK | |
811 | { | |
e04a16fb AG |
812 | check_modifiers |
813 | ("Illegal modifier `%s' for field declaration", | |
814 | $1, FIELD_MODIFIERS); | |
815 | check_modifiers_consistency ($1); | |
816 | register_fields ($1, $2, $3); | |
817 | } | |
818 | ; | |
819 | ||
820 | variable_declarators: | |
821 | /* Should we use build_decl_list () instead ? FIXME */ | |
822 | variable_declarator /* Default rule */ | |
823 | | variable_declarators C_TK variable_declarator | |
824 | { $$ = chainon ($1, $3); } | |
825 | | variable_declarators C_TK error | |
826 | {yyerror ("Missing term"); RECOVER;} | |
827 | ; | |
828 | ||
829 | variable_declarator: | |
830 | variable_declarator_id | |
831 | { $$ = build_tree_list ($1, NULL_TREE); } | |
832 | | variable_declarator_id ASSIGN_TK variable_initializer | |
833 | { | |
834 | if (java_error_count) | |
835 | $3 = NULL_TREE; | |
836 | $$ = build_tree_list | |
837 | ($1, build_assignment ($2.token, $2.location, $1, $3)); | |
838 | } | |
839 | | variable_declarator_id ASSIGN_TK error | |
840 | { | |
841 | yyerror ("Missing variable initializer"); | |
842 | $$ = build_tree_list ($1, NULL_TREE); | |
843 | RECOVER; | |
844 | } | |
845 | | variable_declarator_id ASSIGN_TK variable_initializer error | |
846 | { | |
847 | yyerror ("';' expected"); | |
848 | $$ = build_tree_list ($1, NULL_TREE); | |
849 | RECOVER; | |
850 | } | |
851 | ; | |
852 | ||
853 | variable_declarator_id: | |
854 | identifier | |
855 | | variable_declarator_id OSB_TK CSB_TK | |
c583dd46 | 856 | { $$ = build_unresolved_array_type ($1); } |
e04a16fb AG |
857 | | identifier error |
858 | {yyerror ("Invalid declaration"); DRECOVER(vdi);} | |
859 | | variable_declarator_id OSB_TK error | |
860 | {yyerror ("']' expected"); DRECOVER(vdi);} | |
861 | | variable_declarator_id CSB_TK error | |
862 | {yyerror ("Unbalanced ']'"); DRECOVER(vdi);} | |
863 | ; | |
864 | ||
865 | variable_initializer: | |
866 | expression | |
867 | | array_initializer | |
e04a16fb AG |
868 | ; |
869 | ||
870 | /* 19.8.3 Productions from 8.4: Method Declarations */ | |
871 | method_declaration: | |
872 | method_header | |
873 | { | |
874 | current_function_decl = $1; | |
875 | source_start_java_method (current_function_decl); | |
876 | } | |
877 | method_body | |
b635eb2f | 878 | { finish_method_declaration ($3); } |
e04a16fb AG |
879 | | method_header error |
880 | {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;} | |
881 | ; | |
882 | ||
883 | method_header: | |
884 | type method_declarator throws | |
b9f7e36c | 885 | { $$ = method_header (0, $1, $2, $3); } |
e04a16fb | 886 | | VOID_TK method_declarator throws |
b9f7e36c | 887 | { $$ = method_header (0, void_type_node, $2, $3); } |
e04a16fb | 888 | | modifiers type method_declarator throws |
b9f7e36c | 889 | { $$ = method_header ($1, $2, $3, $4); } |
e04a16fb | 890 | | modifiers VOID_TK method_declarator throws |
b9f7e36c | 891 | { $$ = method_header ($1, void_type_node, $3, $4); } |
e04a16fb AG |
892 | | type error |
893 | {RECOVER;} | |
894 | | modifiers type error | |
895 | {RECOVER;} | |
896 | | VOID_TK error | |
897 | {yyerror ("Identifier expected"); RECOVER;} | |
898 | | modifiers VOID_TK error | |
899 | {yyerror ("Identifier expected"); RECOVER;} | |
900 | | modifiers error | |
901 | { | |
902 | yyerror ("Invalid method declaration, return type required"); | |
903 | RECOVER; | |
904 | } | |
905 | ; | |
906 | ||
907 | method_declarator: | |
908 | identifier OP_TK CP_TK | |
909 | { $$ = method_declarator ($1, NULL_TREE); } | |
910 | | identifier OP_TK formal_parameter_list CP_TK | |
911 | { $$ = method_declarator ($1, $3); } | |
912 | | method_declarator OSB_TK CSB_TK | |
913 | { | |
1886c9d8 APB |
914 | EXPR_WFL_LINECOL (wfl_operator) = $2.location; |
915 | TREE_PURPOSE ($1) = | |
916 | build_unresolved_array_type (TREE_PURPOSE ($1)); | |
917 | parse_warning_context | |
918 | (wfl_operator, | |
919 | "Discouraged form of returned type specification"); | |
e04a16fb AG |
920 | } |
921 | | identifier OP_TK error | |
922 | {yyerror ("')' expected"); DRECOVER(method_declarator);} | |
923 | | method_declarator OSB_TK error | |
924 | {yyerror ("']' expected"); RECOVER;} | |
925 | ; | |
926 | ||
927 | formal_parameter_list: | |
928 | formal_parameter | |
929 | { | |
930 | ctxp->formal_parameter_number = 1; | |
931 | } | |
932 | | formal_parameter_list C_TK formal_parameter | |
933 | { | |
934 | ctxp->formal_parameter_number += 1; | |
935 | $$ = chainon ($1, $3); | |
936 | } | |
937 | | formal_parameter_list C_TK error | |
938 | {yyerror ("Missing formal parameter term"); RECOVER;} | |
939 | ; | |
940 | ||
941 | formal_parameter: | |
942 | type variable_declarator_id | |
943 | { | |
944 | $$ = build_tree_list ($2, $1); | |
945 | } | |
c877974e | 946 | | modifiers type variable_declarator_id /* Added, JDK1.1 final parms */ |
5256aa37 APB |
947 | { |
948 | parse_jdk1_1_error ("final parameters"); | |
949 | $$ = build_tree_list ($3, $2); | |
950 | } | |
e04a16fb AG |
951 | | type error |
952 | {yyerror ("Missing identifier"); RECOVER;} | |
953 | | modifiers type error | |
954 | { | |
955 | SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1)); | |
956 | yyerror ("Missing identifier"); RECOVER; | |
957 | } | |
958 | ; | |
959 | ||
960 | throws: | |
b9f7e36c | 961 | { $$ = NULL_TREE; } |
e04a16fb | 962 | | THROWS_TK class_type_list |
b9f7e36c | 963 | { $$ = $2; } |
e04a16fb AG |
964 | | THROWS_TK error |
965 | {yyerror ("Missing class type term"); RECOVER;} | |
966 | ; | |
967 | ||
968 | class_type_list: | |
969 | class_type | |
c877974e | 970 | { $$ = build_tree_list ($1, $1); } |
e04a16fb | 971 | | class_type_list C_TK class_type |
c877974e | 972 | { $$ = tree_cons ($3, $3, $1); } |
e04a16fb AG |
973 | | class_type_list C_TK error |
974 | {yyerror ("Missing class type term"); RECOVER;} | |
975 | ; | |
976 | ||
977 | method_body: | |
978 | block | |
979 | | block SC_TK | |
980 | | SC_TK | |
981 | { $$ = NULL_TREE; } /* Probably not the right thing to do. */ | |
982 | ; | |
983 | ||
984 | /* 19.8.4 Productions from 8.5: Static Initializers */ | |
985 | static_initializer: | |
986 | static block | |
987 | { | |
f099f336 APB |
988 | TREE_CHAIN ($2) = ctxp->static_initialized; |
989 | ctxp->static_initialized = $2; | |
e04a16fb AG |
990 | } |
991 | | static block SC_TK /* Shouldn't be here. FIXME */ | |
992 | { | |
f099f336 APB |
993 | TREE_CHAIN ($2) = ctxp->static_initialized; |
994 | ctxp->static_initialized = $2; | |
e04a16fb AG |
995 | } |
996 | ; | |
997 | ||
998 | static: /* Test lval.sub_token here */ | |
999 | MODIFIER_TK | |
1000 | { | |
1001 | SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1)); | |
1002 | } | |
1003 | ; | |
1004 | ||
1005 | /* 19.8.5 Productions from 8.6: Constructor Declarations */ | |
e04a16fb | 1006 | constructor_declaration: |
22eed1e6 | 1007 | constructor_header |
e04a16fb | 1008 | { |
22eed1e6 APB |
1009 | current_function_decl = $1; |
1010 | source_start_java_method (current_function_decl); | |
e04a16fb | 1011 | } |
22eed1e6 | 1012 | constructor_body |
b635eb2f | 1013 | { finish_method_declaration ($3); } |
22eed1e6 APB |
1014 | ; |
1015 | ||
1016 | constructor_header: | |
1017 | constructor_declarator throws | |
1018 | { $$ = method_header (0, NULL_TREE, $1, $2); } | |
1019 | | modifiers constructor_declarator throws | |
1020 | { $$ = method_header ($1, NULL_TREE, $2, $3); } | |
e04a16fb AG |
1021 | ; |
1022 | ||
1023 | constructor_declarator: | |
1024 | simple_name OP_TK CP_TK | |
22eed1e6 | 1025 | { $$ = method_declarator ($1, NULL_TREE); } |
e04a16fb | 1026 | | simple_name OP_TK formal_parameter_list CP_TK |
22eed1e6 | 1027 | { $$ = method_declarator ($1, $3); } |
e04a16fb AG |
1028 | ; |
1029 | ||
1030 | constructor_body: | |
22eed1e6 APB |
1031 | /* Unlike regular method, we always need a complete (empty) |
1032 | body so we can safely perform all the required code | |
1033 | addition (super invocation and field initialization) */ | |
2e5eb5c5 | 1034 | block_begin constructor_block_end |
22eed1e6 | 1035 | { |
9bbc7d9f | 1036 | BLOCK_EXPR_BODY ($2) = empty_stmt_node; |
22eed1e6 APB |
1037 | $$ = $2; |
1038 | } | |
2e5eb5c5 | 1039 | | block_begin explicit_constructor_invocation constructor_block_end |
22eed1e6 | 1040 | { $$ = $3; } |
2e5eb5c5 | 1041 | | block_begin block_statements constructor_block_end |
22eed1e6 | 1042 | { $$ = $3; } |
2e5eb5c5 | 1043 | | block_begin explicit_constructor_invocation block_statements constructor_block_end |
22eed1e6 | 1044 | { $$ = $4; } |
e04a16fb AG |
1045 | ; |
1046 | ||
2e5eb5c5 APB |
1047 | constructor_block_end: |
1048 | block_end | |
1049 | | block_end SC_TK | |
1050 | ||
e04a16fb AG |
1051 | /* Error recovery for that rule moved down expression_statement: rule. */ |
1052 | explicit_constructor_invocation: | |
1053 | this_or_super OP_TK CP_TK SC_TK | |
22eed1e6 APB |
1054 | { |
1055 | $$ = build_method_invocation ($1, NULL_TREE); | |
1056 | $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$); | |
1057 | $$ = java_method_add_stmt (current_function_decl, $$); | |
1058 | } | |
e04a16fb | 1059 | | this_or_super OP_TK argument_list CP_TK SC_TK |
22eed1e6 APB |
1060 | { |
1061 | $$ = build_method_invocation ($1, $3); | |
1062 | $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$); | |
1063 | $$ = java_method_add_stmt (current_function_decl, $$); | |
1064 | } | |
e04a16fb AG |
1065 | /* Added, JDK1.1 inner classes. Modified because the rule |
1066 | 'primary' couldn't work. */ | |
1067 | | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK | |
b67d701b | 1068 | {$$ = parse_jdk1_1_error ("explicit constructor invocation"); } |
e04a16fb | 1069 | | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK |
b67d701b | 1070 | {$$ = parse_jdk1_1_error ("explicit constructor invocation"); } |
e04a16fb AG |
1071 | ; |
1072 | ||
1073 | this_or_super: /* Added, simplifies error diagnostics */ | |
1074 | THIS_TK | |
1075 | { | |
9ee9b555 | 1076 | tree wfl = build_wfl_node (this_identifier_node); |
e04a16fb AG |
1077 | EXPR_WFL_LINECOL (wfl) = $1.location; |
1078 | $$ = wfl; | |
1079 | } | |
1080 | | SUPER_TK | |
1081 | { | |
9ee9b555 | 1082 | tree wfl = build_wfl_node (super_identifier_node); |
e04a16fb AG |
1083 | EXPR_WFL_LINECOL (wfl) = $1.location; |
1084 | $$ = wfl; | |
1085 | } | |
1086 | ; | |
1087 | ||
1088 | /* 19.9 Productions from 9: Interfaces */ | |
1089 | /* 19.9.1 Productions from 9.1: Interfaces Declarations */ | |
1090 | interface_declaration: | |
1091 | INTERFACE_TK identifier | |
1092 | { create_interface (0, $2, NULL_TREE); } | |
1093 | interface_body | |
1094 | { | |
1095 | $$ = $4; | |
1096 | } | |
1097 | | modifiers INTERFACE_TK identifier | |
1098 | { create_interface ($1, $3, NULL_TREE); } | |
1099 | interface_body | |
1100 | { | |
1101 | $$ = $5; | |
1102 | } | |
1103 | | INTERFACE_TK identifier extends_interfaces | |
1104 | { create_interface (0, $2, $3); } | |
1105 | interface_body | |
1106 | { | |
1107 | $$ = $5; | |
1108 | } | |
1109 | | modifiers INTERFACE_TK identifier extends_interfaces | |
1110 | { create_interface ($1, $3, $4); } | |
1111 | interface_body | |
1112 | { | |
1113 | $$ = $6; | |
1114 | } | |
1115 | | INTERFACE_TK identifier error | |
0b4d333e | 1116 | {yyerror ("'{' expected"); RECOVER;} |
e04a16fb | 1117 | | modifiers INTERFACE_TK identifier error |
0b4d333e | 1118 | {yyerror ("'{' expected"); RECOVER;} |
e04a16fb AG |
1119 | ; |
1120 | ||
1121 | extends_interfaces: | |
1122 | EXTENDS_TK interface_type | |
1123 | { | |
1124 | ctxp->interface_number = 1; | |
1125 | $$ = build_tree_list ($2, NULL_TREE); | |
1126 | } | |
1127 | | extends_interfaces C_TK interface_type | |
1128 | { | |
1129 | ctxp->interface_number++; | |
1130 | $$ = chainon ($1, build_tree_list ($3, NULL_TREE)); | |
1131 | } | |
1132 | | EXTENDS_TK error | |
1133 | {yyerror ("Invalid interface type"); RECOVER;} | |
1134 | | extends_interfaces C_TK error | |
1135 | {yyerror ("Missing term"); RECOVER;} | |
1136 | ; | |
1137 | ||
1138 | interface_body: | |
1139 | OCB_TK CCB_TK | |
1140 | { $$ = NULL_TREE; } | |
1141 | | OCB_TK interface_member_declarations CCB_TK | |
1142 | { $$ = NULL_TREE; } | |
1143 | ; | |
1144 | ||
1145 | interface_member_declarations: | |
1146 | interface_member_declaration | |
1147 | | interface_member_declarations interface_member_declaration | |
1148 | ; | |
1149 | ||
1150 | interface_member_declaration: | |
1151 | constant_declaration | |
1152 | | abstract_method_declaration | |
1153 | | class_declaration /* Added, JDK1.1 inner classes */ | |
b67d701b | 1154 | { $$ = parse_jdk1_1_error ("inner class declaration"); } |
e04a16fb | 1155 | | interface_declaration /* Added, JDK1.1 inner classes */ |
b67d701b | 1156 | { $$ = parse_jdk1_1_error ("inner interface declaration"); } |
e04a16fb AG |
1157 | ; |
1158 | ||
1159 | constant_declaration: | |
1160 | field_declaration | |
1161 | ; | |
1162 | ||
1163 | abstract_method_declaration: | |
1164 | method_header SC_TK | |
1165 | { | |
1166 | check_abstract_method_header ($1); | |
1167 | current_function_decl = NULL_TREE; /* FIXME ? */ | |
1168 | } | |
1169 | | method_header error | |
1170 | {yyerror ("';' expected"); RECOVER;} | |
1171 | ; | |
1172 | ||
1173 | /* 19.10 Productions from 10: Arrays */ | |
1174 | array_initializer: | |
1175 | OCB_TK CCB_TK | |
1179ebc2 | 1176 | { $$ = build_new_array_init ($1.location, NULL_TREE); } |
e04a16fb | 1177 | | OCB_TK variable_initializers CCB_TK |
f8976021 | 1178 | { $$ = build_new_array_init ($1.location, $2); } |
e04a16fb | 1179 | | OCB_TK variable_initializers C_TK CCB_TK |
f8976021 | 1180 | { $$ = build_new_array_init ($1.location, $2); } |
e04a16fb AG |
1181 | ; |
1182 | ||
1183 | variable_initializers: | |
1184 | variable_initializer | |
f8976021 APB |
1185 | { |
1186 | $$ = tree_cons (maybe_build_array_element_wfl ($1), | |
1187 | $1, NULL_TREE); | |
1188 | } | |
e04a16fb | 1189 | | variable_initializers C_TK variable_initializer |
1179ebc2 APB |
1190 | { |
1191 | $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1); | |
1192 | } | |
e04a16fb AG |
1193 | | variable_initializers C_TK error |
1194 | {yyerror ("Missing term"); RECOVER;} | |
1195 | ; | |
1196 | ||
1197 | /* 19.11 Production from 14: Blocks and Statements */ | |
1198 | block: | |
1199 | OCB_TK CCB_TK | |
7f10c2e2 APB |
1200 | { |
1201 | /* Store the location of the `}' when doing xrefs */ | |
1202 | if (current_function_decl && flag_emit_xref) | |
1203 | DECL_END_SOURCE_LINE (current_function_decl) = | |
1204 | EXPR_WFL_ADD_COL ($2.location, 1); | |
1205 | $$ = empty_stmt_node; | |
1206 | } | |
22eed1e6 APB |
1207 | | block_begin block_statements block_end |
1208 | { $$ = $3; } | |
1209 | ; | |
1210 | ||
1211 | block_begin: | |
1212 | OCB_TK | |
e04a16fb | 1213 | { enter_block (); } |
22eed1e6 APB |
1214 | ; |
1215 | ||
1216 | block_end: | |
e04a16fb AG |
1217 | CCB_TK |
1218 | { | |
1219 | maybe_absorb_scoping_blocks (); | |
7f10c2e2 APB |
1220 | /* Store the location of the `}' when doing xrefs */ |
1221 | if (current_function_decl && flag_emit_xref) | |
1222 | DECL_END_SOURCE_LINE (current_function_decl) = | |
1223 | EXPR_WFL_ADD_COL ($1.location, 1); | |
e04a16fb AG |
1224 | $$ = exit_block (); |
1225 | } | |
1226 | ; | |
1227 | ||
1228 | block_statements: | |
1229 | block_statement | |
1230 | | block_statements block_statement | |
1231 | ; | |
1232 | ||
1233 | block_statement: | |
1234 | local_variable_declaration_statement | |
1235 | | statement | |
15fdcfe9 | 1236 | { java_method_add_stmt (current_function_decl, $1); } |
e04a16fb | 1237 | | class_declaration /* Added, JDK1.1 inner classes */ |
15fdcfe9 | 1238 | { parse_jdk1_1_error ("inner class declaration"); } |
e04a16fb AG |
1239 | ; |
1240 | ||
1241 | local_variable_declaration_statement: | |
1242 | local_variable_declaration SC_TK /* Can't catch missing ';' here */ | |
1243 | ; | |
1244 | ||
1245 | local_variable_declaration: | |
1246 | type variable_declarators | |
1247 | { declare_local_variables (0, $1, $2); } | |
1248 | | modifiers type variable_declarators /* Added, JDK1.1 final locals */ | |
1249 | { declare_local_variables ($1, $2, $3); } | |
1250 | ; | |
1251 | ||
1252 | statement: | |
1253 | statement_without_trailing_substatement | |
1254 | | labeled_statement | |
e04a16fb | 1255 | | if_then_statement |
e04a16fb | 1256 | | if_then_else_statement |
e04a16fb | 1257 | | while_statement |
e04a16fb | 1258 | | for_statement |
cd9643f7 | 1259 | { $$ = exit_block (); } |
e04a16fb AG |
1260 | ; |
1261 | ||
1262 | statement_nsi: | |
1263 | statement_without_trailing_substatement | |
1264 | | labeled_statement_nsi | |
e04a16fb | 1265 | | if_then_else_statement_nsi |
e04a16fb | 1266 | | while_statement_nsi |
e04a16fb | 1267 | | for_statement_nsi |
9dd939b2 | 1268 | { $$ = exit_block (); } |
e04a16fb AG |
1269 | ; |
1270 | ||
1271 | statement_without_trailing_substatement: | |
1272 | block | |
e04a16fb | 1273 | | empty_statement |
e04a16fb | 1274 | | expression_statement |
e04a16fb | 1275 | | switch_statement |
e04a16fb | 1276 | | do_statement |
e04a16fb | 1277 | | break_statement |
e04a16fb | 1278 | | continue_statement |
e04a16fb AG |
1279 | | return_statement |
1280 | | synchronized_statement | |
e04a16fb | 1281 | | throw_statement |
e04a16fb | 1282 | | try_statement |
e04a16fb AG |
1283 | ; |
1284 | ||
1285 | empty_statement: | |
1286 | SC_TK | |
9bbc7d9f | 1287 | { $$ = empty_stmt_node; } |
e04a16fb AG |
1288 | ; |
1289 | ||
1290 | label_decl: | |
1291 | identifier REL_CL_TK | |
1292 | { | |
1293 | $$ = build_labeled_block (EXPR_WFL_LINECOL ($1), | |
0a2138e2 | 1294 | EXPR_WFL_NODE ($1)); |
e04a16fb AG |
1295 | pushlevel (2); |
1296 | push_labeled_block ($$); | |
1297 | PUSH_LABELED_BLOCK ($$); | |
1298 | } | |
1299 | ; | |
1300 | ||
1301 | labeled_statement: | |
1302 | label_decl statement | |
b635eb2f | 1303 | { $$ = finish_labeled_statement ($1, $2); } |
e04a16fb AG |
1304 | | identifier error |
1305 | {yyerror ("':' expected"); RECOVER;} | |
1306 | ; | |
1307 | ||
1308 | labeled_statement_nsi: | |
1309 | label_decl statement_nsi | |
b635eb2f | 1310 | { $$ = finish_labeled_statement ($1, $2); } |
e04a16fb AG |
1311 | ; |
1312 | ||
1313 | /* We concentrate here a bunch of error handling rules that we couldn't write | |
1314 | earlier, because expression_statement catches a missing ';'. */ | |
1315 | expression_statement: | |
1316 | statement_expression SC_TK | |
1317 | { | |
1318 | /* We have a statement. Generate a WFL around it so | |
1319 | we can debug it */ | |
1320 | $$ = build_expr_wfl ($1, input_filename, lineno, 0); | |
1321 | /* We know we have a statement, so set the debug | |
1322 | info to be eventually generate here. */ | |
1323 | $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$); | |
1324 | } | |
1325 | | error SC_TK | |
1326 | { | |
1327 | if (ctxp->prevent_ese != lineno) | |
1328 | yyerror ("Invalid expression statement"); | |
1329 | DRECOVER (expr_stmt); | |
1330 | } | |
1331 | | error OCB_TK | |
1332 | { | |
1333 | if (ctxp->prevent_ese != lineno) | |
1334 | yyerror ("Invalid expression statement"); | |
1335 | DRECOVER (expr_stmt); | |
1336 | } | |
1337 | | error CCB_TK | |
1338 | { | |
1339 | if (ctxp->prevent_ese != lineno) | |
1340 | yyerror ("Invalid expression statement"); | |
1341 | DRECOVER (expr_stmt); | |
1342 | } | |
1343 | | this_or_super OP_TK error | |
1344 | {yyerror ("')' expected"); RECOVER;} | |
1345 | | this_or_super OP_TK CP_TK error | |
22eed1e6 | 1346 | { |
8119c720 | 1347 | parse_ctor_invocation_error (); |
22eed1e6 APB |
1348 | RECOVER; |
1349 | } | |
e04a16fb AG |
1350 | | this_or_super OP_TK argument_list error |
1351 | {yyerror ("')' expected"); RECOVER;} | |
1352 | | this_or_super OP_TK argument_list CP_TK error | |
22eed1e6 | 1353 | { |
8119c720 | 1354 | parse_ctor_invocation_error (); |
22eed1e6 APB |
1355 | RECOVER; |
1356 | } | |
e04a16fb AG |
1357 | | name DOT_TK SUPER_TK error |
1358 | {yyerror ("'(' expected"); RECOVER;} | |
1359 | | name DOT_TK SUPER_TK OP_TK error | |
1360 | {yyerror ("')' expected"); RECOVER;} | |
1361 | | name DOT_TK SUPER_TK OP_TK argument_list error | |
1362 | {yyerror ("')' expected"); RECOVER;} | |
1363 | | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error | |
1364 | {yyerror ("';' expected"); RECOVER;} | |
1365 | | name DOT_TK SUPER_TK OP_TK CP_TK error | |
1366 | {yyerror ("';' expected"); RECOVER;} | |
1367 | ; | |
1368 | ||
1369 | statement_expression: | |
1370 | assignment | |
1371 | | pre_increment_expression | |
e04a16fb | 1372 | | pre_decrement_expression |
e04a16fb | 1373 | | post_increment_expression |
e04a16fb | 1374 | | post_decrement_expression |
e04a16fb AG |
1375 | | method_invocation |
1376 | | class_instance_creation_expression | |
e04a16fb AG |
1377 | ; |
1378 | ||
1379 | if_then_statement: | |
1380 | IF_TK OP_TK expression CP_TK statement | |
2aa11e97 APB |
1381 | { |
1382 | $$ = build_if_else_statement ($2.location, $3, | |
1383 | $5, NULL_TREE); | |
1384 | } | |
e04a16fb AG |
1385 | | IF_TK error |
1386 | {yyerror ("'(' expected"); RECOVER;} | |
1387 | | IF_TK OP_TK error | |
1388 | {yyerror ("Missing term"); RECOVER;} | |
1389 | | IF_TK OP_TK expression error | |
1390 | {yyerror ("')' expected"); RECOVER;} | |
1391 | ; | |
1392 | ||
1393 | if_then_else_statement: | |
1394 | IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement | |
2aa11e97 | 1395 | { $$ = build_if_else_statement ($2.location, $3, $5, $7); } |
e04a16fb AG |
1396 | ; |
1397 | ||
1398 | if_then_else_statement_nsi: | |
1399 | IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi | |
2aa11e97 | 1400 | { $$ = build_if_else_statement ($2.location, $3, $5, $7); } |
e04a16fb AG |
1401 | ; |
1402 | ||
1403 | switch_statement: | |
15fdcfe9 PB |
1404 | switch_expression |
1405 | { | |
1406 | enter_block (); | |
1407 | } | |
1408 | switch_block | |
b67d701b | 1409 | { |
15fdcfe9 | 1410 | /* Make into "proper list" of COMPOUND_EXPRs. |
f8976021 APB |
1411 | I.e. make the last statment also have its own |
1412 | COMPOUND_EXPR. */ | |
15fdcfe9 PB |
1413 | maybe_absorb_scoping_blocks (); |
1414 | TREE_OPERAND ($1, 1) = exit_block (); | |
b67d701b PB |
1415 | $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1); |
1416 | } | |
1417 | ; | |
1418 | ||
1419 | switch_expression: | |
1420 | SWITCH_TK OP_TK expression CP_TK | |
1421 | { | |
1422 | $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE); | |
1423 | EXPR_WFL_LINECOL ($$) = $2.location; | |
1424 | } | |
e04a16fb AG |
1425 | | SWITCH_TK error |
1426 | {yyerror ("'(' expected"); RECOVER;} | |
1427 | | SWITCH_TK OP_TK error | |
1428 | {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);} | |
1429 | | SWITCH_TK OP_TK expression CP_TK error | |
1430 | {yyerror ("'{' expected"); RECOVER;} | |
1431 | ; | |
1432 | ||
f8976021 APB |
1433 | /* Default assignment is there to avoid type node on switch_block |
1434 | node. */ | |
1435 | ||
e04a16fb AG |
1436 | switch_block: |
1437 | OCB_TK CCB_TK | |
f8976021 | 1438 | { $$ = NULL_TREE; } |
e04a16fb | 1439 | | OCB_TK switch_labels CCB_TK |
f8976021 | 1440 | { $$ = NULL_TREE; } |
e04a16fb | 1441 | | OCB_TK switch_block_statement_groups CCB_TK |
f8976021 | 1442 | { $$ = NULL_TREE; } |
e04a16fb | 1443 | | OCB_TK switch_block_statement_groups switch_labels CCB_TK |
f8976021 | 1444 | { $$ = NULL_TREE; } |
e04a16fb AG |
1445 | ; |
1446 | ||
1447 | switch_block_statement_groups: | |
1448 | switch_block_statement_group | |
1449 | | switch_block_statement_groups switch_block_statement_group | |
1450 | ; | |
1451 | ||
1452 | switch_block_statement_group: | |
15fdcfe9 | 1453 | switch_labels block_statements |
e04a16fb AG |
1454 | ; |
1455 | ||
e04a16fb AG |
1456 | switch_labels: |
1457 | switch_label | |
1458 | | switch_labels switch_label | |
1459 | ; | |
1460 | ||
1461 | switch_label: | |
1462 | CASE_TK constant_expression REL_CL_TK | |
b67d701b | 1463 | { |
15fdcfe9 PB |
1464 | tree lab = build1 (CASE_EXPR, NULL_TREE, $2); |
1465 | EXPR_WFL_LINECOL (lab) = $1.location; | |
1466 | java_method_add_stmt (current_function_decl, lab); | |
b67d701b | 1467 | } |
e04a16fb | 1468 | | DEFAULT_TK REL_CL_TK |
b67d701b | 1469 | { |
15fdcfe9 PB |
1470 | tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE); |
1471 | EXPR_WFL_LINECOL (lab) = $1.location; | |
1472 | java_method_add_stmt (current_function_decl, lab); | |
b67d701b | 1473 | } |
e04a16fb AG |
1474 | | CASE_TK error |
1475 | {yyerror ("Missing or invalid constant expression"); RECOVER;} | |
1476 | | CASE_TK constant_expression error | |
1477 | {yyerror ("':' expected"); RECOVER;} | |
1478 | | DEFAULT_TK error | |
1479 | {yyerror ("':' expected"); RECOVER;} | |
1480 | ; | |
1481 | ||
1482 | while_expression: | |
1483 | WHILE_TK OP_TK expression CP_TK | |
1484 | { | |
1485 | tree body = build_loop_body ($2.location, $3, 0); | |
1486 | $$ = build_new_loop (body); | |
1487 | } | |
1488 | ; | |
1489 | ||
1490 | while_statement: | |
1491 | while_expression statement | |
b635eb2f | 1492 | { $$ = finish_loop_body (0, NULL_TREE, $2, 0); } |
e04a16fb AG |
1493 | | WHILE_TK error |
1494 | {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;} | |
1495 | | WHILE_TK OP_TK error | |
1496 | {yyerror ("Missing term and ')' expected"); RECOVER;} | |
1497 | | WHILE_TK OP_TK expression error | |
1498 | {yyerror ("')' expected"); RECOVER;} | |
1499 | ; | |
1500 | ||
1501 | while_statement_nsi: | |
1502 | while_expression statement_nsi | |
b635eb2f | 1503 | { $$ = finish_loop_body (0, NULL_TREE, $2, 0); } |
e04a16fb AG |
1504 | ; |
1505 | ||
1506 | do_statement_begin: | |
1507 | DO_TK | |
1508 | { | |
1509 | tree body = build_loop_body (0, NULL_TREE, 1); | |
1510 | $$ = build_new_loop (body); | |
1511 | } | |
1512 | /* Need error handing here. FIXME */ | |
1513 | ; | |
1514 | ||
1515 | do_statement: | |
1516 | do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK | |
b635eb2f | 1517 | { $$ = finish_loop_body ($4.location, $5, $2, 1); } |
e04a16fb AG |
1518 | ; |
1519 | ||
1520 | for_statement: | |
1521 | for_begin SC_TK expression SC_TK for_update CP_TK statement | |
b635eb2f | 1522 | { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7); } |
e04a16fb AG |
1523 | | for_begin SC_TK SC_TK for_update CP_TK statement |
1524 | { | |
b635eb2f | 1525 | $$ = finish_for_loop (0, NULL_TREE, $4, $6); |
e04a16fb AG |
1526 | /* We have not condition, so we get rid of the EXIT_EXPR */ |
1527 | LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = | |
9bbc7d9f | 1528 | empty_stmt_node; |
e04a16fb AG |
1529 | } |
1530 | | for_begin SC_TK error | |
1531 | {yyerror ("Invalid control expression"); RECOVER;} | |
1532 | | for_begin SC_TK expression SC_TK error | |
1533 | {yyerror ("Invalid update expression"); RECOVER;} | |
1534 | | for_begin SC_TK SC_TK error | |
1535 | {yyerror ("Invalid update expression"); RECOVER;} | |
1536 | ; | |
1537 | ||
1538 | for_statement_nsi: | |
1539 | for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi | |
b635eb2f | 1540 | { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);} |
e04a16fb AG |
1541 | | for_begin SC_TK SC_TK for_update CP_TK statement_nsi |
1542 | { | |
b635eb2f | 1543 | $$ = finish_for_loop (0, NULL_TREE, $4, $6); |
e04a16fb AG |
1544 | /* We have not condition, so we get rid of the EXIT_EXPR */ |
1545 | LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = | |
9bbc7d9f | 1546 | empty_stmt_node; |
e04a16fb AG |
1547 | } |
1548 | ; | |
1549 | ||
1550 | for_header: | |
1551 | FOR_TK OP_TK | |
1552 | { | |
1553 | /* This scope defined for local variable that may be | |
1554 | defined within the scope of the for loop */ | |
1555 | enter_block (); | |
1556 | } | |
1557 | | FOR_TK error | |
1558 | {yyerror ("'(' expected"); DRECOVER(for_1);} | |
1559 | | FOR_TK OP_TK error | |
1560 | {yyerror ("Invalid init statement"); RECOVER;} | |
1561 | ; | |
1562 | ||
1563 | for_begin: | |
1564 | for_header for_init | |
1565 | { | |
1566 | /* We now declare the loop body. The loop is | |
1567 | declared as a for loop. */ | |
1568 | tree body = build_loop_body (0, NULL_TREE, 0); | |
1569 | $$ = build_new_loop (body); | |
1570 | IS_FOR_LOOP_P ($$) = 1; | |
1571 | /* The loop is added to the current block the for | |
1572 | statement is defined within */ | |
1573 | java_method_add_stmt (current_function_decl, $$); | |
1574 | } | |
1575 | ; | |
1576 | for_init: /* Can be empty */ | |
9bbc7d9f | 1577 | { $$ = empty_stmt_node; } |
e04a16fb AG |
1578 | | statement_expression_list |
1579 | { | |
1580 | /* Init statement recorded within the previously | |
1581 | defined block scope */ | |
1582 | $$ = java_method_add_stmt (current_function_decl, $1); | |
1583 | } | |
1584 | | local_variable_declaration | |
1585 | { | |
1586 | /* Local variable are recorded within the previously | |
1587 | defined block scope */ | |
1588 | $$ = NULL_TREE; | |
1589 | } | |
1590 | | statement_expression_list error | |
1591 | {yyerror ("';' expected"); DRECOVER(for_init_1);} | |
1592 | ; | |
1593 | ||
1594 | for_update: /* Can be empty */ | |
9bbc7d9f | 1595 | {$$ = empty_stmt_node;} |
e04a16fb AG |
1596 | | statement_expression_list |
1597 | { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); } | |
1598 | ; | |
1599 | ||
1600 | statement_expression_list: | |
1601 | statement_expression | |
1602 | { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); } | |
1603 | | statement_expression_list C_TK statement_expression | |
1604 | { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); } | |
1605 | | statement_expression_list C_TK error | |
1606 | {yyerror ("Missing term"); RECOVER;} | |
1607 | ; | |
1608 | ||
1609 | break_statement: | |
1610 | BREAK_TK SC_TK | |
1611 | { $$ = build_bc_statement ($1.location, 1, NULL_TREE); } | |
1612 | | BREAK_TK identifier SC_TK | |
1613 | { $$ = build_bc_statement ($1.location, 1, $2); } | |
1614 | | BREAK_TK error | |
1615 | {yyerror ("Missing term"); RECOVER;} | |
1616 | | BREAK_TK identifier error | |
1617 | {yyerror ("';' expected"); RECOVER;} | |
1618 | ; | |
1619 | ||
1620 | continue_statement: | |
1621 | CONTINUE_TK SC_TK | |
1622 | { $$ = build_bc_statement ($1.location, 0, NULL_TREE); } | |
1623 | | CONTINUE_TK identifier SC_TK | |
1624 | { $$ = build_bc_statement ($1.location, 0, $2); } | |
1625 | | CONTINUE_TK error | |
1626 | {yyerror ("Missing term"); RECOVER;} | |
1627 | | CONTINUE_TK identifier error | |
1628 | {yyerror ("';' expected"); RECOVER;} | |
1629 | ; | |
1630 | ||
1631 | return_statement: | |
1632 | RETURN_TK SC_TK | |
1633 | { $$ = build_return ($1.location, NULL_TREE); } | |
1634 | | RETURN_TK expression SC_TK | |
1635 | { $$ = build_return ($1.location, $2); } | |
1636 | | RETURN_TK error | |
1637 | {yyerror ("Missing term"); RECOVER;} | |
1638 | | RETURN_TK expression error | |
1639 | {yyerror ("';' expected"); RECOVER;} | |
1640 | ; | |
1641 | ||
1642 | throw_statement: | |
1643 | THROW_TK expression SC_TK | |
b9f7e36c APB |
1644 | { |
1645 | $$ = build1 (THROW_EXPR, NULL_TREE, $2); | |
1646 | EXPR_WFL_LINECOL ($$) = $1.location; | |
1647 | } | |
e04a16fb AG |
1648 | | THROW_TK error |
1649 | {yyerror ("Missing term"); RECOVER;} | |
1650 | | THROW_TK expression error | |
1651 | {yyerror ("';' expected"); RECOVER;} | |
1652 | ; | |
1653 | ||
1654 | synchronized_statement: | |
1655 | synchronized OP_TK expression CP_TK block | |
b9f7e36c APB |
1656 | { |
1657 | $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5); | |
1658 | EXPR_WFL_LINECOL ($$) = | |
1659 | EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK)); | |
1660 | } | |
e04a16fb AG |
1661 | | synchronized OP_TK expression CP_TK error |
1662 | {yyerror ("'{' expected"); RECOVER;} | |
1663 | | synchronized error | |
1664 | {yyerror ("'(' expected"); RECOVER;} | |
1665 | | synchronized OP_TK error CP_TK | |
1666 | {yyerror ("Missing term"); RECOVER;} | |
1667 | | synchronized OP_TK error | |
1668 | {yyerror ("Missing term"); RECOVER;} | |
1669 | ; | |
1670 | ||
b9f7e36c | 1671 | synchronized: |
e04a16fb AG |
1672 | MODIFIER_TK |
1673 | { | |
b9f7e36c APB |
1674 | if ((1 << $1) != ACC_SYNCHRONIZED) |
1675 | fatal ("synchronized was '%d' - yyparse", (1 << $1)); | |
e04a16fb AG |
1676 | } |
1677 | ; | |
1678 | ||
1679 | try_statement: | |
1680 | TRY_TK block catches | |
a7d8d81f | 1681 | { $$ = build_try_statement ($1.location, $2, $3); } |
e04a16fb | 1682 | | TRY_TK block finally |
a7d8d81f | 1683 | { $$ = build_try_finally_statement ($1.location, $2, $3); } |
e04a16fb | 1684 | | TRY_TK block catches finally |
2aa11e97 APB |
1685 | { $$ = build_try_finally_statement |
1686 | ($1.location, build_try_statement ($1.location, | |
1687 | $2, $3), $4); | |
1688 | } | |
e04a16fb AG |
1689 | | TRY_TK error |
1690 | {yyerror ("'{' expected"); DRECOVER (try_statement);} | |
1691 | ; | |
1692 | ||
1693 | catches: | |
1694 | catch_clause | |
1695 | | catches catch_clause | |
b67d701b PB |
1696 | { |
1697 | TREE_CHAIN ($2) = $1; | |
1698 | $$ = $2; | |
1699 | } | |
e04a16fb AG |
1700 | ; |
1701 | ||
1702 | catch_clause: | |
b67d701b PB |
1703 | catch_clause_parameter block |
1704 | { | |
1705 | java_method_add_stmt (current_function_decl, $2); | |
1706 | exit_block (); | |
1707 | $$ = $1; | |
1708 | } | |
1709 | ||
1710 | catch_clause_parameter: | |
1711 | CATCH_TK OP_TK formal_parameter CP_TK | |
1712 | { | |
1713 | /* We add a block to define a scope for | |
1714 | formal_parameter (CCBP). The formal parameter is | |
1715 | declared initialized by the appropriate function | |
1716 | call */ | |
1717 | tree ccpb = enter_block (); | |
1718 | tree init = build_assignment (ASSIGN_TK, $2.location, | |
1719 | TREE_PURPOSE ($3), | |
1720 | soft_exceptioninfo_call_node); | |
1721 | declare_local_variables (0, TREE_VALUE ($3), | |
1722 | build_tree_list (TREE_PURPOSE ($3), | |
1723 | init)); | |
1724 | $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb); | |
1725 | EXPR_WFL_LINECOL ($$) = $1.location; | |
1726 | } | |
e04a16fb AG |
1727 | | CATCH_TK error |
1728 | {yyerror ("'(' expected"); RECOVER;} | |
e04a16fb | 1729 | | CATCH_TK OP_TK error |
b67d701b PB |
1730 | {yyerror ("Missing term or ')' expected"); DRECOVER (2);} |
1731 | | CATCH_TK OP_TK error CP_TK /* That's for () */ | |
1732 | {yyerror ("')' expected"); DRECOVER (1);} | |
e04a16fb AG |
1733 | ; |
1734 | ||
1735 | finally: | |
1736 | FINALLY_TK block | |
a7d8d81f | 1737 | { $$ = $2; } |
e04a16fb AG |
1738 | | FINALLY_TK error |
1739 | {yyerror ("'{' expected"); RECOVER; } | |
1740 | ; | |
1741 | ||
1742 | /* 19.12 Production from 15: Expressions */ | |
1743 | primary: | |
1744 | primary_no_new_array | |
1745 | | array_creation_expression | |
1746 | ; | |
1747 | ||
1748 | primary_no_new_array: | |
1749 | literal | |
1750 | | THIS_TK | |
1751 | { $$ = build_this ($1.location); } | |
1752 | | OP_TK expression CP_TK | |
1753 | {$$ = $2;} | |
1754 | | class_instance_creation_expression | |
1755 | | field_access | |
1756 | | method_invocation | |
1757 | | array_access | |
1758 | /* type DOT_TK CLASS_TK doens't work. So we split the rule | |
1759 | 'type' into its components. Missing is something for array, | |
1760 | which will complete the reference_type part. FIXME */ | |
1761 | | name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */ | |
c877974e | 1762 | { $$ = parse_jdk1_1_error ("named class literals"); } |
e04a16fb | 1763 | | primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */ |
c877974e | 1764 | { $$ = build_class_ref ($1); } |
e04a16fb | 1765 | | VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */ |
c877974e | 1766 | { $$ = build_class_ref (void_type_node); } |
e04a16fb AG |
1767 | /* Added, JDK1.1 inner classes. Documentation is wrong |
1768 | refering to a 'ClassName' (class_name) rule that doesn't | |
1769 | exist. Used name instead. */ | |
1770 | | name DOT_TK THIS_TK | |
b67d701b | 1771 | { $$ = parse_jdk1_1_error ("class literals"); } |
e04a16fb AG |
1772 | | OP_TK expression error |
1773 | {yyerror ("')' expected"); RECOVER;} | |
1774 | | name DOT_TK error | |
1775 | {yyerror ("'class' or 'this' expected" ); RECOVER;} | |
1776 | | primitive_type DOT_TK error | |
1777 | {yyerror ("'class' expected" ); RECOVER;} | |
1778 | | VOID_TK DOT_TK error | |
1779 | {yyerror ("'class' expected" ); RECOVER;} | |
1780 | ; | |
1781 | ||
1782 | class_instance_creation_expression: | |
1783 | NEW_TK class_type OP_TK argument_list CP_TK | |
b67d701b | 1784 | { $$ = build_new_invocation ($2, $4); } |
e04a16fb | 1785 | | NEW_TK class_type OP_TK CP_TK |
b67d701b | 1786 | { $$ = build_new_invocation ($2, NULL_TREE); } |
e04a16fb AG |
1787 | /* Added, JDK1.1 inner classes but modified to use |
1788 | 'class_type' instead of 'TypeName' (type_name) mentionned | |
1789 | in the documentation but doesn't exist. */ | |
1790 | | NEW_TK class_type OP_TK argument_list CP_TK class_body | |
b67d701b | 1791 | { $$ = parse_jdk1_1_error ("inner class instance creation"); } |
e04a16fb | 1792 | | NEW_TK class_type OP_TK CP_TK class_body |
b67d701b | 1793 | { $$ = parse_jdk1_1_error ("inner class instance creation"); } |
e04a16fb AG |
1794 | /* Added, JDK1.1 inner classes, modified to use name or |
1795 | primary instead of primary solely which couldn't work in | |
1796 | all situations. */ | |
1797 | | something_dot_new identifier OP_TK CP_TK | |
1798 | | something_dot_new identifier OP_TK CP_TK class_body | |
1799 | | something_dot_new identifier OP_TK argument_list CP_TK | |
1800 | | something_dot_new identifier OP_TK argument_list CP_TK class_body | |
1801 | | NEW_TK error SC_TK | |
1802 | {yyerror ("'(' expected"); DRECOVER(new_1);} | |
1803 | | NEW_TK class_type error | |
1804 | {yyerror ("'(' expected"); RECOVER;} | |
1805 | | NEW_TK class_type OP_TK error | |
1806 | {yyerror ("')' or term expected"); RECOVER;} | |
1807 | | NEW_TK class_type OP_TK argument_list error | |
1808 | {yyerror ("')' expected"); RECOVER;} | |
1809 | | something_dot_new error | |
1810 | {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;} | |
1811 | | something_dot_new identifier error | |
1812 | {yyerror ("'(' expected"); RECOVER;} | |
1813 | ; | |
1814 | ||
1815 | something_dot_new: /* Added, not part of the specs. */ | |
1816 | name DOT_TK NEW_TK | |
1817 | | primary DOT_TK NEW_TK | |
1818 | ; | |
1819 | ||
1820 | argument_list: | |
1821 | expression | |
1822 | { | |
1823 | $$ = tree_cons (NULL_TREE, $1, NULL_TREE); | |
1824 | ctxp->formal_parameter_number = 1; | |
1825 | } | |
1826 | | argument_list C_TK expression | |
1827 | { | |
1828 | ctxp->formal_parameter_number += 1; | |
1829 | $$ = tree_cons (NULL_TREE, $3, $1); | |
1830 | } | |
1831 | | argument_list C_TK error | |
1832 | {yyerror ("Missing term"); RECOVER;} | |
1833 | ; | |
1834 | ||
1835 | array_creation_expression: | |
1836 | NEW_TK primitive_type dim_exprs | |
1837 | { $$ = build_newarray_node ($2, $3, 0); } | |
1838 | | NEW_TK class_or_interface_type dim_exprs | |
1839 | { $$ = build_newarray_node ($2, $3, 0); } | |
1840 | | NEW_TK primitive_type dim_exprs dims | |
ba179f9f | 1841 | { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));} |
e04a16fb | 1842 | | NEW_TK class_or_interface_type dim_exprs dims |
ba179f9f | 1843 | { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));} |
e04a16fb AG |
1844 | /* Added, JDK1.1 anonymous array. Initial documentation rule |
1845 | modified */ | |
1846 | | NEW_TK class_or_interface_type dims array_initializer | |
b67d701b | 1847 | { $$ = parse_jdk1_1_error ("anonymous array"); } |
e04a16fb | 1848 | | NEW_TK primitive_type dims array_initializer |
b67d701b | 1849 | { $$ = parse_jdk1_1_error ("anonymous array"); } |
e04a16fb AG |
1850 | | NEW_TK error CSB_TK |
1851 | {yyerror ("'[' expected"); DRECOVER ("]");} | |
1852 | | NEW_TK error OSB_TK | |
1853 | {yyerror ("']' expected"); RECOVER;} | |
1854 | ; | |
1855 | ||
1856 | dim_exprs: | |
1857 | dim_expr | |
1858 | { $$ = build_tree_list (NULL_TREE, $1); } | |
1859 | | dim_exprs dim_expr | |
1860 | { $$ = tree_cons (NULL_TREE, $2, $$); } | |
1861 | ; | |
1862 | ||
1863 | dim_expr: | |
1864 | OSB_TK expression CSB_TK | |
1865 | { | |
1866 | EXPR_WFL_LINECOL ($2) = $1.location; | |
1867 | $$ = $2; | |
1868 | } | |
1869 | | OSB_TK expression error | |
1870 | {yyerror ("']' expected"); RECOVER;} | |
1871 | | OSB_TK error | |
1872 | { | |
1873 | yyerror ("Missing term"); | |
1874 | yyerror ("']' expected"); | |
1875 | RECOVER; | |
1876 | } | |
1877 | ; | |
1878 | ||
1879 | dims: | |
1880 | OSB_TK CSB_TK | |
ba179f9f APB |
1881 | { |
1882 | int allocate = 0; | |
1883 | /* If not initialized, allocate memory for the osb | |
1884 | numbers stack */ | |
1885 | if (!ctxp->osb_limit) | |
1886 | { | |
1887 | allocate = ctxp->osb_limit = 32; | |
1888 | ctxp->osb_depth = -1; | |
1889 | } | |
1890 | /* If capacity overflown, reallocate a bigger chuck */ | |
1891 | else if (ctxp->osb_depth+1 == ctxp->osb_limit) | |
1892 | allocate = ctxp->osb_limit << 1; | |
1893 | ||
1894 | if (allocate) | |
1895 | { | |
1896 | allocate *= sizeof (int); | |
1897 | if (ctxp->osb_number) | |
1898 | ctxp->osb_number = (int *)xrealloc (ctxp->osb_number, | |
1899 | allocate); | |
1900 | else | |
1901 | ctxp->osb_number = (int *)xmalloc (allocate); | |
1902 | } | |
1903 | ctxp->osb_depth++; | |
1904 | CURRENT_OSB (ctxp) = 1; | |
1905 | } | |
e04a16fb | 1906 | | dims OSB_TK CSB_TK |
ba179f9f | 1907 | { CURRENT_OSB (ctxp)++; } |
e04a16fb AG |
1908 | | dims OSB_TK error |
1909 | { yyerror ("']' expected"); RECOVER;} | |
1910 | ; | |
1911 | ||
1912 | field_access: | |
1913 | primary DOT_TK identifier | |
1914 | { $$ = make_qualified_primary ($1, $3, $2.location); } | |
9bbc7d9f PB |
1915 | /* FIXME - REWRITE TO: |
1916 | { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */ | |
e04a16fb AG |
1917 | | SUPER_TK DOT_TK identifier |
1918 | { | |
1919 | tree super_wfl = | |
9ee9b555 | 1920 | build_wfl_node (super_identifier_node); |
e04a16fb AG |
1921 | EXPR_WFL_LINECOL (super_wfl) = $1.location; |
1922 | $$ = make_qualified_name (super_wfl, $3, $2.location); | |
1923 | } | |
1924 | | SUPER_TK error | |
1925 | {yyerror ("Field expected"); DRECOVER (super_field_acces);} | |
1926 | ; | |
1927 | ||
1928 | method_invocation: | |
1929 | name OP_TK CP_TK | |
1930 | { $$ = build_method_invocation ($1, NULL_TREE); } | |
1931 | | name OP_TK argument_list CP_TK | |
1932 | { $$ = build_method_invocation ($1, $3); } | |
1933 | | primary DOT_TK identifier OP_TK CP_TK | |
1934 | { | |
22eed1e6 APB |
1935 | if (TREE_CODE ($1) == THIS_EXPR) |
1936 | $$ = build_this_super_qualified_invocation | |
1937 | (1, $3, NULL_TREE, 0, $2.location); | |
1938 | else | |
1939 | { | |
1940 | tree invok = build_method_invocation ($3, NULL_TREE); | |
1941 | $$ = make_qualified_primary ($1, invok, $2.location); | |
1942 | } | |
e04a16fb AG |
1943 | } |
1944 | | primary DOT_TK identifier OP_TK argument_list CP_TK | |
1945 | { | |
22eed1e6 APB |
1946 | if (TREE_CODE ($1) == THIS_EXPR) |
1947 | $$ = build_this_super_qualified_invocation | |
1948 | (1, $3, $5, 0, $2.location); | |
1949 | else | |
1950 | { | |
1951 | tree invok = build_method_invocation ($3, $5); | |
1952 | $$ = make_qualified_primary ($1, invok, $2.location); | |
1953 | } | |
e04a16fb AG |
1954 | } |
1955 | | SUPER_TK DOT_TK identifier OP_TK CP_TK | |
22eed1e6 APB |
1956 | { |
1957 | $$ = build_this_super_qualified_invocation | |
1958 | (0, $3, NULL_TREE, $1.location, $2.location); | |
e04a16fb AG |
1959 | } |
1960 | | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK | |
1961 | { | |
22eed1e6 APB |
1962 | $$ = build_this_super_qualified_invocation |
1963 | (0, $3, $5, $1.location, $2.location); | |
e04a16fb AG |
1964 | } |
1965 | /* Screws up thing. I let it here until I'm convinced it can | |
1966 | be removed. FIXME | |
1967 | | primary DOT_TK error | |
1968 | {yyerror ("'(' expected"); DRECOVER(bad);} */ | |
1969 | | SUPER_TK DOT_TK error CP_TK | |
1970 | { yyerror ("'(' expected"); DRECOVER (method_invocation); } | |
1971 | | SUPER_TK DOT_TK error DOT_TK | |
1972 | { yyerror ("'(' expected"); DRECOVER (method_invocation); } | |
1973 | ; | |
1974 | ||
1975 | array_access: | |
1976 | name OSB_TK expression CSB_TK | |
1977 | { $$ = build_array_ref ($2.location, $1, $3); } | |
1978 | | primary_no_new_array OSB_TK expression CSB_TK | |
1979 | { $$ = build_array_ref ($2.location, $1, $3); } | |
1980 | | name OSB_TK error | |
1981 | { | |
1982 | yyerror ("Missing term and ']' expected"); | |
1983 | DRECOVER(array_access); | |
1984 | } | |
1985 | | name OSB_TK expression error | |
1986 | { | |
1987 | yyerror ("']' expected"); | |
1988 | DRECOVER(array_access); | |
1989 | } | |
1990 | | primary_no_new_array OSB_TK error | |
1991 | { | |
1992 | yyerror ("Missing term and ']' expected"); | |
1993 | DRECOVER(array_access); | |
1994 | } | |
1995 | | primary_no_new_array OSB_TK expression error | |
1996 | { | |
1997 | yyerror ("']' expected"); | |
1998 | DRECOVER(array_access); | |
1999 | } | |
2000 | ; | |
2001 | ||
2002 | postfix_expression: | |
2003 | primary | |
2004 | | name | |
2005 | | post_increment_expression | |
2006 | | post_decrement_expression | |
2007 | ; | |
2008 | ||
2009 | post_increment_expression: | |
2010 | postfix_expression INCR_TK | |
2011 | { $$ = build_incdec ($2.token, $2.location, $1, 1); } | |
2012 | ; | |
2013 | ||
2014 | post_decrement_expression: | |
2015 | postfix_expression DECR_TK | |
2016 | { $$ = build_incdec ($2.token, $2.location, $1, 1); } | |
2017 | ; | |
2018 | ||
2019 | unary_expression: | |
2020 | pre_increment_expression | |
2021 | | pre_decrement_expression | |
2022 | | PLUS_TK unary_expression | |
2023 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2024 | | MINUS_TK unary_expression | |
2025 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2026 | | unary_expression_not_plus_minus | |
2027 | | PLUS_TK error | |
2028 | {yyerror ("Missing term"); RECOVER} | |
2029 | | MINUS_TK error | |
2030 | {yyerror ("Missing term"); RECOVER} | |
2031 | ; | |
2032 | ||
2033 | pre_increment_expression: | |
2034 | INCR_TK unary_expression | |
2035 | {$$ = build_incdec ($1.token, $1.location, $2, 0); } | |
2036 | | INCR_TK error | |
2037 | {yyerror ("Missing term"); RECOVER} | |
2038 | ; | |
2039 | ||
2040 | pre_decrement_expression: | |
2041 | DECR_TK unary_expression | |
2042 | {$$ = build_incdec ($1.token, $1.location, $2, 0); } | |
2043 | | DECR_TK error | |
2044 | {yyerror ("Missing term"); RECOVER} | |
2045 | ; | |
2046 | ||
2047 | unary_expression_not_plus_minus: | |
2048 | postfix_expression | |
2049 | | NOT_TK unary_expression | |
2050 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2051 | | NEG_TK unary_expression | |
2052 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2053 | | cast_expression | |
2054 | | NOT_TK error | |
2055 | {yyerror ("Missing term"); RECOVER} | |
2056 | | NEG_TK error | |
2057 | {yyerror ("Missing term"); RECOVER} | |
2058 | ; | |
2059 | ||
2060 | cast_expression: /* Error handling here is potentially weak */ | |
2061 | OP_TK primitive_type dims CP_TK unary_expression | |
2062 | { | |
2063 | tree type = $2; | |
ba179f9f | 2064 | while (CURRENT_OSB (ctxp)--) |
e04a16fb | 2065 | type = build_java_array_type (type, -1); |
ba179f9f | 2066 | ctxp->osb_depth--; |
e04a16fb AG |
2067 | $$ = build_cast ($1.location, type, $5); |
2068 | } | |
2069 | | OP_TK primitive_type CP_TK unary_expression | |
2070 | { $$ = build_cast ($1.location, $2, $4); } | |
2071 | | OP_TK expression CP_TK unary_expression_not_plus_minus | |
2072 | { $$ = build_cast ($1.location, $2, $4); } | |
2073 | | OP_TK name dims CP_TK unary_expression_not_plus_minus | |
2074 | { | |
49f48c71 | 2075 | const char *ptr; |
ba179f9f | 2076 | while (CURRENT_OSB (ctxp)--) |
e04a16fb | 2077 | obstack_1grow (&temporary_obstack, '['); |
ba179f9f | 2078 | ctxp->osb_depth--; |
e04a16fb AG |
2079 | obstack_grow0 (&temporary_obstack, |
2080 | IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)), | |
2081 | IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2))); | |
2082 | ptr = obstack_finish (&temporary_obstack); | |
2083 | EXPR_WFL_NODE ($2) = get_identifier (ptr); | |
2084 | $$ = build_cast ($1.location, $2, $5); | |
2085 | } | |
2086 | | OP_TK primitive_type OSB_TK error | |
2087 | {yyerror ("']' expected, invalid type expression");} | |
2088 | | OP_TK error | |
2089 | { | |
2090 | if (ctxp->prevent_ese != lineno) | |
2091 | yyerror ("Invalid type expression"); RECOVER; | |
2092 | RECOVER; | |
2093 | } | |
2094 | | OP_TK primitive_type dims CP_TK error | |
2095 | {yyerror ("Missing term"); RECOVER;} | |
2096 | | OP_TK primitive_type CP_TK error | |
2097 | {yyerror ("Missing term"); RECOVER;} | |
2098 | | OP_TK name dims CP_TK error | |
2099 | {yyerror ("Missing term"); RECOVER;} | |
2100 | ; | |
2101 | ||
2102 | multiplicative_expression: | |
2103 | unary_expression | |
2104 | | multiplicative_expression MULT_TK unary_expression | |
2105 | { | |
2106 | $$ = build_binop (BINOP_LOOKUP ($2.token), | |
2107 | $2.location, $1, $3); | |
2108 | } | |
2109 | | multiplicative_expression DIV_TK unary_expression | |
2110 | { | |
2111 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2112 | $1, $3); | |
2113 | } | |
2114 | | multiplicative_expression REM_TK unary_expression | |
2115 | { | |
2116 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2117 | $1, $3); | |
2118 | } | |
2119 | | multiplicative_expression MULT_TK error | |
2120 | {yyerror ("Missing term"); RECOVER;} | |
2121 | | multiplicative_expression DIV_TK error | |
2122 | {yyerror ("Missing term"); RECOVER;} | |
2123 | | multiplicative_expression REM_TK error | |
2124 | {yyerror ("Missing term"); RECOVER;} | |
2125 | ; | |
2126 | ||
2127 | additive_expression: | |
2128 | multiplicative_expression | |
2129 | | additive_expression PLUS_TK multiplicative_expression | |
2130 | { | |
2131 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2132 | $1, $3); | |
2133 | } | |
2134 | | additive_expression MINUS_TK multiplicative_expression | |
2135 | { | |
2136 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2137 | $1, $3); | |
2138 | } | |
2139 | | additive_expression PLUS_TK error | |
2140 | {yyerror ("Missing term"); RECOVER;} | |
2141 | | additive_expression MINUS_TK error | |
2142 | {yyerror ("Missing term"); RECOVER;} | |
2143 | ; | |
2144 | ||
2145 | shift_expression: | |
2146 | additive_expression | |
2147 | | shift_expression LS_TK additive_expression | |
2148 | { | |
2149 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2150 | $1, $3); | |
2151 | } | |
2152 | | shift_expression SRS_TK additive_expression | |
2153 | { | |
2154 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2155 | $1, $3); | |
2156 | } | |
2157 | | shift_expression ZRS_TK additive_expression | |
2158 | { | |
2159 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2160 | $1, $3); | |
2161 | } | |
2162 | | shift_expression LS_TK error | |
2163 | {yyerror ("Missing term"); RECOVER;} | |
2164 | | shift_expression SRS_TK error | |
2165 | {yyerror ("Missing term"); RECOVER;} | |
2166 | | shift_expression ZRS_TK error | |
2167 | {yyerror ("Missing term"); RECOVER;} | |
2168 | ; | |
2169 | ||
2170 | relational_expression: | |
2171 | shift_expression | |
2172 | | relational_expression LT_TK shift_expression | |
2173 | { | |
2174 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2175 | $1, $3); | |
2176 | } | |
2177 | | relational_expression GT_TK shift_expression | |
2178 | { | |
2179 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2180 | $1, $3); | |
2181 | } | |
2182 | | relational_expression LTE_TK shift_expression | |
2183 | { | |
2184 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2185 | $1, $3); | |
2186 | } | |
2187 | | relational_expression GTE_TK shift_expression | |
2188 | { | |
2189 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2190 | $1, $3); | |
2191 | } | |
2192 | | relational_expression INSTANCEOF_TK reference_type | |
5e942c50 | 2193 | { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); } |
e04a16fb AG |
2194 | | relational_expression LT_TK error |
2195 | {yyerror ("Missing term"); RECOVER;} | |
2196 | | relational_expression GT_TK error | |
2197 | {yyerror ("Missing term"); RECOVER;} | |
2198 | | relational_expression LTE_TK error | |
2199 | {yyerror ("Missing term"); RECOVER;} | |
2200 | | relational_expression GTE_TK error | |
2201 | {yyerror ("Missing term"); RECOVER;} | |
2202 | | relational_expression INSTANCEOF_TK error | |
2203 | {yyerror ("Invalid reference type"); RECOVER;} | |
2204 | ; | |
2205 | ||
2206 | equality_expression: | |
2207 | relational_expression | |
2208 | | equality_expression EQ_TK relational_expression | |
2209 | { | |
2210 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2211 | $1, $3); | |
2212 | } | |
2213 | | equality_expression NEQ_TK relational_expression | |
2214 | { | |
2215 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2216 | $1, $3); | |
2217 | } | |
2218 | | equality_expression EQ_TK error | |
2219 | {yyerror ("Missing term"); RECOVER;} | |
2220 | | equality_expression NEQ_TK error | |
2221 | {yyerror ("Missing term"); RECOVER;} | |
2222 | ; | |
2223 | ||
2224 | and_expression: | |
2225 | equality_expression | |
2226 | | and_expression AND_TK equality_expression | |
2227 | { | |
2228 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2229 | $1, $3); | |
2230 | } | |
2231 | | and_expression AND_TK error | |
2232 | {yyerror ("Missing term"); RECOVER;} | |
2233 | ; | |
2234 | ||
2235 | exclusive_or_expression: | |
2236 | and_expression | |
2237 | | exclusive_or_expression XOR_TK and_expression | |
2238 | { | |
2239 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2240 | $1, $3); | |
2241 | } | |
2242 | | exclusive_or_expression XOR_TK error | |
2243 | {yyerror ("Missing term"); RECOVER;} | |
2244 | ; | |
2245 | ||
2246 | inclusive_or_expression: | |
2247 | exclusive_or_expression | |
2248 | | inclusive_or_expression OR_TK exclusive_or_expression | |
2249 | { | |
2250 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2251 | $1, $3); | |
2252 | } | |
2253 | | inclusive_or_expression OR_TK error | |
2254 | {yyerror ("Missing term"); RECOVER;} | |
2255 | ; | |
2256 | ||
2257 | conditional_and_expression: | |
2258 | inclusive_or_expression | |
2259 | | conditional_and_expression BOOL_AND_TK inclusive_or_expression | |
2260 | { | |
2261 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2262 | $1, $3); | |
2263 | } | |
2264 | | conditional_and_expression BOOL_AND_TK error | |
2265 | {yyerror ("Missing term"); RECOVER;} | |
2266 | ; | |
2267 | ||
2268 | conditional_or_expression: | |
2269 | conditional_and_expression | |
2270 | | conditional_or_expression BOOL_OR_TK conditional_and_expression | |
2271 | { | |
2272 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2273 | $1, $3); | |
2274 | } | |
2275 | | conditional_or_expression BOOL_OR_TK error | |
2276 | {yyerror ("Missing term"); RECOVER;} | |
2277 | ; | |
2278 | ||
2279 | conditional_expression: /* Error handling here is weak */ | |
2280 | conditional_or_expression | |
2281 | | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression | |
22eed1e6 APB |
2282 | { |
2283 | $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5); | |
2284 | EXPR_WFL_LINECOL ($$) = $2.location; | |
2285 | } | |
e04a16fb AG |
2286 | | conditional_or_expression REL_QM_TK REL_CL_TK error |
2287 | { | |
2288 | YYERROR_NOW; | |
2289 | yyerror ("Missing term"); | |
2290 | DRECOVER (1); | |
2291 | } | |
2292 | | conditional_or_expression REL_QM_TK error | |
2293 | {yyerror ("Missing term"); DRECOVER (2);} | |
2294 | | conditional_or_expression REL_QM_TK expression REL_CL_TK error | |
2295 | {yyerror ("Missing term"); DRECOVER (3);} | |
2296 | ; | |
2297 | ||
2298 | assignment_expression: | |
2299 | conditional_expression | |
2300 | | assignment | |
2301 | ; | |
2302 | ||
2303 | assignment: | |
2304 | left_hand_side assignment_operator assignment_expression | |
2305 | { $$ = build_assignment ($2.token, $2.location, $1, $3); } | |
2306 | | left_hand_side assignment_operator error | |
2307 | { | |
2308 | if (ctxp->prevent_ese != lineno) | |
2309 | yyerror ("Missing term"); | |
2310 | DRECOVER (assign); | |
2311 | } | |
2312 | ; | |
2313 | ||
2314 | left_hand_side: | |
2315 | name | |
2316 | | field_access | |
2317 | | array_access | |
2318 | ; | |
2319 | ||
2320 | assignment_operator: | |
2321 | ASSIGN_ANY_TK | |
2322 | | ASSIGN_TK | |
2323 | ; | |
2324 | ||
2325 | expression: | |
2326 | assignment_expression | |
2327 | ; | |
2328 | ||
2329 | constant_expression: | |
2330 | expression | |
2331 | ; | |
2332 | ||
2333 | %% | |
2334 | \f | |
2335 | ||
e04a16fb AG |
2336 | /* Flag for the error report routine to issue the error the first time |
2337 | it's called (overriding the default behavior which is to drop the | |
2338 | first invocation and honor the second one, taking advantage of a | |
2339 | richer context. */ | |
2340 | static int force_error = 0; | |
2341 | ||
2342 | /* Create a new parser context and make it the current one. */ | |
2343 | ||
2344 | void | |
2345 | java_push_parser_context () | |
2346 | { | |
2347 | struct parser_ctxt *new = | |
23a79c61 | 2348 | (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt)); |
e04a16fb | 2349 | |
4504ead1 | 2350 | bzero ((PTR) new, sizeof (struct parser_ctxt)); |
e04a16fb AG |
2351 | new->next = ctxp; |
2352 | ctxp = new; | |
2353 | if (ctxp->next) | |
5e942c50 APB |
2354 | { |
2355 | ctxp->incomplete_class = ctxp->next->incomplete_class; | |
2356 | ctxp->gclass_list = ctxp->next->gclass_list; | |
2357 | } | |
e04a16fb AG |
2358 | } |
2359 | ||
22eed1e6 APB |
2360 | /* If the first file of a file list was a class file, no context |
2361 | exists for a source file to be parsed. This boolean remembers that | |
2362 | java_parser_context_save_global might have created a dummy one, so | |
2363 | that java_parser_context_restore_global can pop it. */ | |
2364 | static int extra_ctxp_pushed_p = 0; | |
2365 | ||
e04a16fb AG |
2366 | void |
2367 | java_parser_context_save_global () | |
2368 | { | |
22eed1e6 APB |
2369 | if (!ctxp) |
2370 | { | |
2371 | java_push_parser_context (); | |
2372 | extra_ctxp_pushed_p = 1; | |
2373 | } | |
e04a16fb AG |
2374 | ctxp->finput = finput; |
2375 | ctxp->lineno = lineno; | |
2376 | ctxp->current_class = current_class; | |
2377 | ctxp->filename = input_filename; | |
2378 | ctxp->current_function_decl = current_function_decl; | |
2379 | } | |
2380 | ||
2381 | void | |
2382 | java_parser_context_restore_global () | |
2383 | { | |
2384 | finput = ctxp->finput; | |
2385 | lineno = ctxp->lineno; | |
2386 | current_class = ctxp->current_class; | |
2387 | input_filename = ctxp->filename; | |
2388 | current_function_decl = ctxp->current_function_decl; | |
23a79c61 | 2389 | if (!ctxp->next && extra_ctxp_pushed_p) |
22eed1e6 APB |
2390 | { |
2391 | java_pop_parser_context (0); | |
2392 | extra_ctxp_pushed_p = 0; | |
2393 | } | |
e04a16fb AG |
2394 | } |
2395 | ||
2396 | void | |
b351b287 APB |
2397 | java_pop_parser_context (generate) |
2398 | int generate; | |
e04a16fb AG |
2399 | { |
2400 | tree current; | |
5e942c50 | 2401 | struct parser_ctxt *toFree, *next; |
e04a16fb | 2402 | |
5e942c50 APB |
2403 | if (!ctxp) |
2404 | return; | |
2405 | ||
2406 | toFree = ctxp; | |
2407 | next = ctxp->next; | |
e04a16fb AG |
2408 | if (next) |
2409 | { | |
2410 | next->incomplete_class = ctxp->incomplete_class; | |
5e942c50 | 2411 | next->gclass_list = ctxp->gclass_list; |
e04a16fb AG |
2412 | lineno = ctxp->lineno; |
2413 | finput = ctxp->finput; | |
2414 | current_class = ctxp->current_class; | |
2415 | } | |
2416 | ||
2417 | /* Set the single import class file flag to 0 for the current list | |
2418 | of imported things */ | |
2419 | for (current = ctxp->import_list; current; current = TREE_CHAIN (current)) | |
2420 | IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0; | |
2421 | ||
2422 | /* And restore those of the previous context */ | |
0a2138e2 | 2423 | if ((ctxp = next)) /* Assignment is really meant here */ |
e04a16fb AG |
2424 | for (current = ctxp->import_list; current; current = TREE_CHAIN (current)) |
2425 | IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1; | |
2426 | ||
b351b287 APB |
2427 | if (generate) |
2428 | { | |
2429 | toFree->next = ctxp_for_generation; | |
2430 | ctxp_for_generation = toFree; | |
2431 | } | |
2432 | else | |
2433 | free (toFree); | |
e04a16fb AG |
2434 | } |
2435 | ||
8119c720 APB |
2436 | /* Reporting an constructor invocation error. */ |
2437 | static void | |
2438 | parse_ctor_invocation_error () | |
2439 | { | |
2440 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
2441 | yyerror ("Constructor invocation must be first thing in a constructor"); | |
2442 | else | |
2443 | yyerror ("Only constructors can invoke constructors"); | |
2444 | } | |
2445 | ||
2446 | /* Reporting JDK1.1 features not implemented. */ | |
b67d701b PB |
2447 | |
2448 | static tree | |
2449 | parse_jdk1_1_error (msg) | |
49f48c71 | 2450 | const char *msg; |
b67d701b PB |
2451 | { |
2452 | sorry (": `%s' JDK1.1(TM) feature", msg); | |
2453 | java_error_count++; | |
9bbc7d9f | 2454 | return empty_stmt_node; |
b67d701b PB |
2455 | } |
2456 | ||
e04a16fb AG |
2457 | static int do_warning = 0; |
2458 | ||
2459 | void | |
2460 | yyerror (msg) | |
49f48c71 | 2461 | const char *msg; |
e04a16fb AG |
2462 | { |
2463 | static java_lc elc; | |
2464 | static int prev_lineno; | |
49f48c71 | 2465 | static const char *prev_msg; |
e04a16fb | 2466 | |
0a2138e2 | 2467 | int save_lineno; |
e04a16fb AG |
2468 | char *remainder, *code_from_source; |
2469 | extern struct obstack temporary_obstack; | |
2470 | ||
2471 | if (!force_error && prev_lineno == lineno) | |
2472 | return; | |
2473 | ||
2474 | /* Save current error location but report latter, when the context is | |
2475 | richer. */ | |
2476 | if (ctxp->java_error_flag == 0) | |
2477 | { | |
2478 | ctxp->java_error_flag = 1; | |
2479 | elc = ctxp->elc; | |
2480 | /* Do something to use the previous line if we're reaching the | |
2481 | end of the file... */ | |
2482 | #ifdef VERBOSE_SKELETON | |
2483 | printf ("* Error detected (%s)\n", (msg ? msg : "(null)")); | |
2484 | #endif | |
2485 | return; | |
2486 | } | |
2487 | ||
2488 | /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */ | |
2489 | if (!force_error && msg == prev_msg && prev_lineno == elc.line) | |
2490 | return; | |
2491 | ||
2492 | ctxp->java_error_flag = 0; | |
2493 | if (do_warning) | |
2494 | java_warning_count++; | |
2495 | else | |
2496 | java_error_count++; | |
2497 | ||
2498 | if (elc.col == 0 && msg[1] == ';') | |
2499 | { | |
2500 | elc.col = ctxp->p_line->char_col-1; | |
2501 | elc.line = ctxp->p_line->lineno; | |
2502 | } | |
2503 | ||
2504 | save_lineno = lineno; | |
2505 | prev_lineno = lineno = elc.line; | |
2506 | prev_msg = msg; | |
2507 | ||
2508 | code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col); | |
2509 | obstack_grow0 (&temporary_obstack, | |
2510 | code_from_source, strlen (code_from_source)); | |
2511 | remainder = obstack_finish (&temporary_obstack); | |
2512 | if (do_warning) | |
2513 | warning ("%s.\n%s", msg, remainder); | |
2514 | else | |
2515 | error ("%s.\n%s", msg, remainder); | |
2516 | ||
2517 | /* This allow us to cheaply avoid an extra 'Invalid expression | |
2518 | statement' error report when errors have been already reported on | |
2519 | the same line. This occurs when we report an error but don't have | |
2520 | a synchronization point other than ';', which | |
2521 | expression_statement is the only one to take care of. */ | |
2522 | ctxp->prevent_ese = lineno = save_lineno; | |
2523 | } | |
2524 | ||
2525 | static void | |
15fdcfe9 | 2526 | issue_warning_error_from_context (cl, msg, ap) |
5e942c50 | 2527 | tree cl; |
d4476be2 | 2528 | const char *msg; |
15fdcfe9 | 2529 | va_list ap; |
5e942c50 | 2530 | { |
1886c9d8 | 2531 | char *saved, *saved_input_filename; |
15fdcfe9 PB |
2532 | char buffer [4096]; |
2533 | vsprintf (buffer, msg, ap); | |
2534 | force_error = 1; | |
5e942c50 APB |
2535 | |
2536 | ctxp->elc.line = EXPR_WFL_LINENO (cl); | |
82371d41 APB |
2537 | ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 : |
2538 | (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl))); | |
5e942c50 APB |
2539 | |
2540 | /* We have a CL, that's a good reason for using it if it contains data */ | |
2541 | saved = ctxp->filename; | |
2542 | if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl)) | |
2543 | ctxp->filename = EXPR_WFL_FILENAME (cl); | |
1886c9d8 APB |
2544 | saved_input_filename = input_filename; |
2545 | input_filename = ctxp->filename; | |
15fdcfe9 PB |
2546 | java_error (NULL); |
2547 | java_error (buffer); | |
5e942c50 | 2548 | ctxp->filename = saved; |
1886c9d8 | 2549 | input_filename = saved_input_filename; |
15fdcfe9 | 2550 | force_error = 0; |
5e942c50 APB |
2551 | } |
2552 | ||
e04a16fb AG |
2553 | /* Issue an error message at a current source line CL */ |
2554 | ||
15fdcfe9 | 2555 | void |
d4476be2 | 2556 | parse_error_context VPROTO ((tree cl, const char *msg, ...)) |
e04a16fb | 2557 | { |
d4476be2 | 2558 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 2559 | tree cl; |
d4476be2 | 2560 | const char *msg; |
e04a16fb | 2561 | #endif |
e04a16fb AG |
2562 | va_list ap; |
2563 | ||
2564 | VA_START (ap, msg); | |
d4476be2 | 2565 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 2566 | cl = va_arg (ap, tree); |
d4476be2 | 2567 | msg = va_arg (ap, const char *); |
e04a16fb | 2568 | #endif |
15fdcfe9 PB |
2569 | issue_warning_error_from_context (cl, msg, ap); |
2570 | va_end (ap); | |
e04a16fb AG |
2571 | } |
2572 | ||
2573 | /* Issue a warning at a current source line CL */ | |
2574 | ||
2575 | static void | |
d4476be2 | 2576 | parse_warning_context VPROTO ((tree cl, const char *msg, ...)) |
e04a16fb | 2577 | { |
d4476be2 | 2578 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 2579 | tree cl; |
d4476be2 | 2580 | const char *msg; |
e04a16fb | 2581 | #endif |
e04a16fb AG |
2582 | va_list ap; |
2583 | ||
2584 | VA_START (ap, msg); | |
d4476be2 | 2585 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 2586 | cl = va_arg (ap, tree); |
d4476be2 | 2587 | msg = va_arg (ap, const char *); |
e04a16fb | 2588 | #endif |
e04a16fb | 2589 | |
c877974e | 2590 | force_error = do_warning = 1; |
15fdcfe9 | 2591 | issue_warning_error_from_context (cl, msg, ap); |
c877974e | 2592 | do_warning = force_error = 0; |
15fdcfe9 | 2593 | va_end (ap); |
e04a16fb AG |
2594 | } |
2595 | ||
82371d41 APB |
2596 | static tree |
2597 | find_expr_with_wfl (node) | |
2598 | tree node; | |
2599 | { | |
2600 | while (node) | |
2601 | { | |
2602 | char code; | |
2603 | tree to_return; | |
2604 | ||
2605 | switch (TREE_CODE (node)) | |
2606 | { | |
2607 | case BLOCK: | |
c0d87ff6 PB |
2608 | node = BLOCK_EXPR_BODY (node); |
2609 | continue; | |
82371d41 APB |
2610 | |
2611 | case COMPOUND_EXPR: | |
2612 | to_return = find_expr_with_wfl (TREE_OPERAND (node, 0)); | |
2613 | if (to_return) | |
2614 | return to_return; | |
c0d87ff6 PB |
2615 | node = TREE_OPERAND (node, 1); |
2616 | continue; | |
82371d41 APB |
2617 | |
2618 | case LOOP_EXPR: | |
c0d87ff6 PB |
2619 | node = TREE_OPERAND (node, 0); |
2620 | continue; | |
82371d41 APB |
2621 | |
2622 | case LABELED_BLOCK_EXPR: | |
c0d87ff6 PB |
2623 | node = TREE_OPERAND (node, 1); |
2624 | continue; | |
2625 | ||
82371d41 APB |
2626 | default: |
2627 | code = TREE_CODE_CLASS (TREE_CODE (node)); | |
2628 | if (((code == '1') || (code == '2') || (code == 'e')) | |
2629 | && EXPR_WFL_LINECOL (node)) | |
2630 | return node; | |
ba179f9f | 2631 | return NULL_TREE; |
82371d41 APB |
2632 | } |
2633 | } | |
2634 | return NULL_TREE; | |
2635 | } | |
2636 | ||
2637 | /* Issue a missing return statement error. Uses METHOD to figure the | |
2638 | last line of the method the error occurs in. */ | |
2639 | ||
2640 | static void | |
2641 | missing_return_error (method) | |
2642 | tree method; | |
2643 | { | |
2644 | EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2); | |
2645 | parse_error_context (wfl_operator, "Missing return statement"); | |
2646 | } | |
2647 | ||
2648 | /* Issue an unreachable statement error. From NODE, find the next | |
2649 | statement to report appropriately. */ | |
2650 | static void | |
2651 | unreachable_stmt_error (node) | |
2652 | tree node; | |
2653 | { | |
2654 | /* Browse node to find the next expression node that has a WFL. Use | |
2655 | the location to report the error */ | |
2656 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
2657 | node = find_expr_with_wfl (TREE_OPERAND (node, 1)); | |
2658 | else | |
2659 | node = find_expr_with_wfl (node); | |
2660 | ||
2661 | if (node) | |
2662 | { | |
2663 | EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2); | |
2664 | parse_error_context (wfl_operator, "Unreachable statement"); | |
2665 | } | |
2666 | else | |
2667 | fatal ("Can't get valid statement - unreachable_stmt_error"); | |
2668 | } | |
2669 | ||
c877974e | 2670 | int |
e04a16fb AG |
2671 | java_report_errors () |
2672 | { | |
2673 | if (java_error_count) | |
2674 | fprintf (stderr, "%d error%s", | |
2675 | java_error_count, (java_error_count == 1 ? "" : "s")); | |
2676 | if (java_warning_count) | |
2677 | fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""), | |
2678 | java_warning_count, (java_warning_count == 1 ? "" : "s")); | |
2679 | if (java_error_count || java_warning_count) | |
2680 | putc ('\n', stderr); | |
c877974e | 2681 | return java_error_count; |
e04a16fb AG |
2682 | } |
2683 | ||
2684 | static char * | |
2685 | java_accstring_lookup (flags) | |
2686 | int flags; | |
2687 | { | |
2688 | static char buffer [80]; | |
2689 | #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;} | |
2690 | ||
2691 | /* Access modifier looked-up first for easier report on forbidden | |
2692 | access. */ | |
2693 | if (flags & ACC_PUBLIC) COPY_RETURN ("public"); | |
2694 | if (flags & ACC_PRIVATE) COPY_RETURN ("private"); | |
2695 | if (flags & ACC_PROTECTED) COPY_RETURN ("protected"); | |
2696 | if (flags & ACC_STATIC) COPY_RETURN ("static"); | |
2697 | if (flags & ACC_FINAL) COPY_RETURN ("final"); | |
2698 | if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized"); | |
2699 | if (flags & ACC_VOLATILE) COPY_RETURN ("volatile"); | |
2700 | if (flags & ACC_TRANSIENT) COPY_RETURN ("transient"); | |
2701 | if (flags & ACC_NATIVE) COPY_RETURN ("native"); | |
2702 | if (flags & ACC_INTERFACE) COPY_RETURN ("interface"); | |
2703 | if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract"); | |
2704 | ||
2705 | buffer [0] = '\0'; | |
2706 | return buffer; | |
2707 | #undef COPY_RETURN | |
2708 | } | |
2709 | ||
b67d701b PB |
2710 | /* Issuing error messages upon redefinition of classes, interfaces or |
2711 | variables. */ | |
2712 | ||
e04a16fb | 2713 | static void |
b67d701b | 2714 | classitf_redefinition_error (context, id, decl, cl) |
49f48c71 | 2715 | const char *context; |
e04a16fb AG |
2716 | tree id, decl, cl; |
2717 | { | |
2718 | parse_error_context (cl, "%s `%s' already defined in %s:%d", | |
2719 | context, IDENTIFIER_POINTER (id), | |
2720 | DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl)); | |
2721 | /* Here we should point out where its redefined. It's a unicode. FIXME */ | |
2722 | } | |
2723 | ||
b67d701b PB |
2724 | static void |
2725 | variable_redefinition_error (context, name, type, line) | |
2726 | tree context, name, type; | |
2727 | int line; | |
2728 | { | |
49f48c71 | 2729 | const char *type_name; |
b67d701b PB |
2730 | |
2731 | /* Figure a proper name for type. We might haven't resolved it */ | |
c877974e APB |
2732 | if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type)) |
2733 | type_name = IDENTIFIER_POINTER (TYPE_NAME (type)); | |
b67d701b | 2734 | else |
0a2138e2 | 2735 | type_name = lang_printable_name (type, 0); |
b67d701b PB |
2736 | |
2737 | parse_error_context (context, | |
2738 | "Variable `%s' is already defined in this method and " | |
2739 | "was declared `%s %s' at line %d", | |
2740 | IDENTIFIER_POINTER (name), | |
2741 | type_name, IDENTIFIER_POINTER (name), line); | |
2742 | } | |
2743 | ||
c583dd46 APB |
2744 | static tree |
2745 | build_array_from_name (type, type_wfl, name, ret_name) | |
2746 | tree type, type_wfl, name, *ret_name; | |
2747 | { | |
2748 | int more_dims = 0; | |
49f48c71 | 2749 | const char *string; |
c583dd46 APB |
2750 | |
2751 | /* Eventually get more dims */ | |
2752 | string = IDENTIFIER_POINTER (name); | |
2753 | while (string [more_dims] == '[') | |
2754 | more_dims++; | |
2755 | ||
2756 | /* If we have, then craft a new type for this variable */ | |
2757 | if (more_dims) | |
2758 | { | |
c0d87ff6 | 2759 | name = get_identifier (&string [more_dims]); |
c583dd46 | 2760 | |
34f4db93 APB |
2761 | /* If we have a pointer, use its type */ |
2762 | if (TREE_CODE (type) == POINTER_TYPE) | |
2763 | type = TREE_TYPE (type); | |
c583dd46 APB |
2764 | |
2765 | /* Building the first dimension of a primitive type uses this | |
2766 | function */ | |
2767 | if (JPRIMITIVE_TYPE_P (type)) | |
2768 | { | |
2769 | type = build_java_array_type (type, -1); | |
22eed1e6 | 2770 | CLASS_LOADED_P (type) = 1; |
c583dd46 APB |
2771 | more_dims--; |
2772 | } | |
2773 | /* Otherwise, if we have a WFL for this type, use it (the type | |
2774 | is already an array on an unresolved type, and we just keep | |
2775 | on adding dimensions) */ | |
2776 | else if (type_wfl) | |
2777 | type = type_wfl; | |
2778 | ||
2779 | /* Add all the dimensions */ | |
2780 | while (more_dims--) | |
2781 | type = build_unresolved_array_type (type); | |
2782 | ||
2783 | /* The type may have been incomplete in the first place */ | |
2784 | if (type_wfl) | |
2785 | type = obtain_incomplete_type (type); | |
2786 | } | |
2787 | ||
2788 | *ret_name = name; | |
2789 | return type; | |
2790 | } | |
2791 | ||
e04a16fb AG |
2792 | /* Build something that the type identifier resolver will identify as |
2793 | being an array to an unresolved type. TYPE_WFL is a WFL on a | |
2794 | identifier. */ | |
2795 | ||
2796 | static tree | |
2797 | build_unresolved_array_type (type_or_wfl) | |
2798 | tree type_or_wfl; | |
2799 | { | |
49f48c71 | 2800 | const char *ptr; |
e04a16fb | 2801 | |
1886c9d8 | 2802 | /* TYPE_OR_WFL might be an array on a resolved type. In this case, |
e04a16fb AG |
2803 | just create a array type */ |
2804 | if (TREE_CODE (type_or_wfl) == RECORD_TYPE) | |
2805 | { | |
2806 | tree type = build_java_array_type (type_or_wfl, -1); | |
2807 | CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl); | |
2808 | return type; | |
2809 | } | |
2810 | ||
2811 | obstack_1grow (&temporary_obstack, '['); | |
2812 | obstack_grow0 (&temporary_obstack, | |
2813 | IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)), | |
2814 | IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl))); | |
2815 | ptr = obstack_finish (&temporary_obstack); | |
2816 | return build_expr_wfl (get_identifier (ptr), | |
2817 | EXPR_WFL_FILENAME (type_or_wfl), | |
2818 | EXPR_WFL_LINENO (type_or_wfl), | |
2819 | EXPR_WFL_COLNO (type_or_wfl)); | |
2820 | } | |
2821 | ||
2822 | /* Check modifiers. If one doesn't fit, retrieve it in its declaration line | |
2823 | and point it out. */ | |
2824 | ||
2825 | static void | |
2826 | check_modifiers (message, value, mask) | |
49f48c71 | 2827 | const char *message; |
e04a16fb AG |
2828 | int value; |
2829 | int mask; | |
2830 | { | |
2831 | /* Should point out the one that don't fit. ASCII/unicode, | |
2832 | going backward. FIXME */ | |
2833 | if (value & ~mask) | |
2834 | { | |
2835 | int i, remainder = value & ~mask; | |
2836 | for (i = 0; i <= 10; i++) | |
2837 | if ((1 << i) & remainder) | |
2838 | parse_error_context (ctxp->modifier_ctx [i], message, | |
2839 | java_accstring_lookup (1 << i)); | |
2840 | } | |
2841 | } | |
2842 | ||
2843 | static void | |
2844 | parser_add_interface (class_decl, interface_decl, wfl) | |
2845 | tree class_decl, interface_decl, wfl; | |
2846 | { | |
2847 | if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl))) | |
2848 | parse_error_context (wfl, "Interface `%s' repeated", | |
2849 | IDENTIFIER_POINTER (DECL_NAME (interface_decl))); | |
2850 | } | |
2851 | ||
2852 | /* Bulk of common class/interface checks. Return 1 if an error was | |
2853 | encountered. TAG is 0 for a class, 1 for an interface. */ | |
2854 | ||
2855 | static int | |
2856 | check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl) | |
2857 | int is_interface, flags; | |
2858 | tree raw_name, qualified_name, decl, cl; | |
2859 | { | |
2860 | tree node; | |
2861 | ||
2862 | if (!quiet_flag) | |
2863 | fprintf (stderr, " %s %s", (is_interface ? "interface" : "class"), | |
2864 | IDENTIFIER_POINTER (qualified_name)); | |
2865 | ||
2866 | /* Scope of an interface/class type name: | |
2867 | - Can't be imported by a single type import | |
2868 | - Can't already exists in the package */ | |
2869 | if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name) | |
2870 | && (node = find_name_in_single_imports (raw_name))) | |
2871 | { | |
2872 | parse_error_context | |
2873 | (cl, "%s name `%s' clashes with imported type `%s'", | |
2874 | (is_interface ? "Interface" : "Class"), | |
2875 | IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node)); | |
2876 | return 1; | |
2877 | } | |
2878 | if (decl && CLASS_COMPLETE_P (decl)) | |
2879 | { | |
b67d701b PB |
2880 | classitf_redefinition_error ((is_interface ? "Interface" : "Class"), |
2881 | qualified_name, decl, cl); | |
e04a16fb AG |
2882 | return 1; |
2883 | } | |
2884 | ||
2885 | /* If public, file name should match class/interface name */ | |
2886 | if (flags & ACC_PUBLIC) | |
2887 | { | |
49f48c71 | 2888 | const char *f; |
e04a16fb AG |
2889 | |
2890 | /* Contains OS dependent assumption on path separator. FIXME */ | |
2891 | for (f = &input_filename [strlen (input_filename)]; | |
fa322ab5 TT |
2892 | f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR; |
2893 | f--) | |
2894 | ; | |
847fe791 | 2895 | if (f[0] == '/' || f[0] == DIR_SEPARATOR) |
e04a16fb AG |
2896 | f++; |
2897 | if (strncmp (IDENTIFIER_POINTER (raw_name), | |
2898 | f , IDENTIFIER_LENGTH (raw_name)) || | |
2899 | f [IDENTIFIER_LENGTH (raw_name)] != '.') | |
2900 | parse_error_context (cl, "Public %s `%s' must be defined in a file " | |
2901 | "called `%s.java'", | |
2902 | (is_interface ? "interface" : "class"), | |
2903 | IDENTIFIER_POINTER (qualified_name), | |
2904 | IDENTIFIER_POINTER (raw_name)); | |
2905 | } | |
2906 | ||
2907 | check_modifiers ((is_interface ? | |
2908 | "Illegal modifier `%s' for interface declaration" : | |
2909 | "Illegal modifier `%s' for class declaration"), flags, | |
2910 | (is_interface ? INTERFACE_MODIFIERS : CLASS_MODIFIERS)); | |
2911 | return 0; | |
2912 | } | |
2913 | ||
2914 | /* If DECL is NULL, create and push a new DECL, record the current | |
2915 | line CL and do other maintenance things. */ | |
2916 | ||
2917 | static tree | |
2918 | maybe_create_class_interface_decl (decl, qualified_name, cl) | |
2919 | tree decl, qualified_name, cl; | |
2920 | { | |
5e942c50 | 2921 | if (!decl) |
e04a16fb AG |
2922 | decl = push_class (make_class (), qualified_name); |
2923 | ||
2924 | /* Take care of the file and line business */ | |
2925 | DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl); | |
f099f336 APB |
2926 | /* If we're emiting xrefs, store the line/col number information */ |
2927 | if (flag_emit_xref) | |
2928 | DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl); | |
2929 | else | |
2930 | DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl); | |
e04a16fb | 2931 | CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1; |
b351b287 APB |
2932 | CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) = |
2933 | IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl)); | |
e04a16fb AG |
2934 | |
2935 | ctxp->current_parsed_class = decl; | |
2936 | ||
2937 | /* Link the declaration to the already seen ones */ | |
2938 | TREE_CHAIN (decl) = ctxp->class_list; | |
2939 | ctxp->class_list = decl; | |
5e942c50 | 2940 | |
23a79c61 | 2941 | /* Create a new nodes in the global lists */ |
5e942c50 | 2942 | ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list); |
23a79c61 | 2943 | all_class_list = tree_cons (NULL_TREE, decl, all_class_list); |
5e942c50 | 2944 | |
e04a16fb AG |
2945 | /* Install a new dependency list element */ |
2946 | create_jdep_list (ctxp); | |
2947 | ||
2948 | SOURCE_FRONTEND_DEBUG (("Defining class/interface %s", | |
2949 | IDENTIFIER_POINTER (qualified_name))); | |
2950 | return decl; | |
2951 | } | |
2952 | ||
2953 | static void | |
2954 | add_superinterfaces (decl, interface_list) | |
2955 | tree decl, interface_list; | |
2956 | { | |
2957 | tree node; | |
2958 | /* Superinterface(s): if present and defined, parser_check_super_interface () | |
2959 | takes care of ensuring that: | |
2960 | - This is an accessible interface type, | |
2961 | - Circularity detection. | |
2962 | parser_add_interface is then called. If present but not defined, | |
2963 | the check operation is delayed until the super interface gets | |
2964 | defined. */ | |
2965 | for (node = interface_list; node; node = TREE_CHAIN (node)) | |
2966 | { | |
15fdcfe9 | 2967 | tree current = TREE_PURPOSE (node); |
5e942c50 APB |
2968 | tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current)); |
2969 | if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl))) | |
e04a16fb | 2970 | { |
5e942c50 APB |
2971 | if (!parser_check_super_interface (idecl, decl, current)) |
2972 | parser_add_interface (decl, idecl, current); | |
e04a16fb AG |
2973 | } |
2974 | else | |
2975 | register_incomplete_type (JDEP_INTERFACE, | |
2976 | current, decl, NULL_TREE); | |
2977 | } | |
2978 | } | |
2979 | ||
2980 | /* Create an interface in pass1 and return its decl. Return the | |
2981 | interface's decl in pass 2. */ | |
2982 | ||
2983 | static tree | |
2984 | create_interface (flags, id, super) | |
2985 | int flags; | |
2986 | tree id, super; | |
2987 | { | |
e04a16fb AG |
2988 | tree raw_name = EXPR_WFL_NODE (id); |
2989 | tree q_name = parser_qualified_classname (id); | |
2990 | tree decl = IDENTIFIER_CLASS_VALUE (q_name); | |
2991 | ||
2992 | EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */ | |
2993 | ||
2994 | /* Basic checks: scope, redefinition, modifiers */ | |
2995 | if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id)) | |
2996 | return NULL_TREE; | |
2997 | ||
2998 | /* Interface modifiers check | |
2999 | - public/abstract allowed (already done at that point) | |
3000 | - abstract is obsolete (comes first, it's a warning, or should be) | |
3001 | - Can't use twice the same (checked in the modifier rule) */ | |
c877974e | 3002 | if ((flags & ACC_ABSTRACT) && flag_redundant) |
e04a16fb AG |
3003 | parse_warning_context |
3004 | (MODIFIER_WFL (ABSTRACT_TK), | |
c877974e | 3005 | "Redundant use of `abstract' modifier. Interface `%s' is implicitely " |
e04a16fb | 3006 | "abstract", IDENTIFIER_POINTER (raw_name)); |
e04a16fb AG |
3007 | |
3008 | /* Create a new decl if DECL is NULL, otherwise fix it */ | |
3009 | decl = maybe_create_class_interface_decl (decl, q_name, id); | |
3010 | ||
3011 | /* Set super info and mark the class a complete */ | |
2aa11e97 | 3012 | set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl), |
e04a16fb AG |
3013 | object_type_node, ctxp->interface_number); |
3014 | ctxp->interface_number = 0; | |
3015 | CLASS_COMPLETE_P (decl) = 1; | |
3016 | add_superinterfaces (decl, super); | |
3017 | ||
3018 | return decl; | |
3019 | } | |
3020 | ||
3021 | /* Create an class in pass1 and return its decl. Return class | |
3022 | interface's decl in pass 2. */ | |
3023 | ||
3024 | static tree | |
3025 | create_class (flags, id, super, interfaces) | |
3026 | int flags; | |
3027 | tree id, super, interfaces; | |
3028 | { | |
e04a16fb AG |
3029 | tree raw_name = EXPR_WFL_NODE (id); |
3030 | tree class_id, decl; | |
9ee9b555 | 3031 | tree super_decl_type; |
e04a16fb AG |
3032 | |
3033 | class_id = parser_qualified_classname (id); | |
3034 | decl = IDENTIFIER_CLASS_VALUE (class_id); | |
5e942c50 | 3035 | ctxp->current_parsed_class_un = EXPR_WFL_NODE (id); |
e04a16fb AG |
3036 | EXPR_WFL_NODE (id) = class_id; |
3037 | ||
3038 | /* Basic check: scope, redefinition, modifiers */ | |
3039 | if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id)) | |
3040 | return NULL_TREE; | |
3041 | ||
3042 | /* Class modifier check: | |
3043 | - Allowed modifier (already done at that point) | |
3044 | - abstract AND final forbidden | |
3045 | - Public classes defined in the correct file */ | |
3046 | if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL)) | |
3047 | parse_error_context (id, "Class `%s' can't be declared both abstract " | |
3048 | "and final", IDENTIFIER_POINTER (raw_name)); | |
3049 | ||
3050 | /* Create a new decl if DECL is NULL, otherwise fix it */ | |
3051 | decl = maybe_create_class_interface_decl (decl, class_id, id); | |
3052 | ||
3053 | /* If SUPER exists, use it, otherwise use Object */ | |
3054 | if (super) | |
3055 | { | |
3056 | /* Can't extend java.lang.Object */ | |
3057 | if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node) | |
3058 | { | |
3059 | parse_error_context (id, "Can't extend `java.lang.Object'"); | |
3060 | return NULL_TREE; | |
3061 | } | |
3062 | ||
2c3199bc PB |
3063 | super_decl_type = |
3064 | register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE); | |
e04a16fb AG |
3065 | } |
3066 | else if (TREE_TYPE (decl) != object_type_node) | |
3067 | super_decl_type = object_type_node; | |
3068 | /* We're defining java.lang.Object */ | |
3069 | else | |
3070 | super_decl_type = NULL_TREE; | |
3071 | ||
3072 | /* Set super info and mark the class a complete */ | |
3073 | set_super_info (flags, TREE_TYPE (decl), super_decl_type, | |
3074 | ctxp->interface_number); | |
3075 | ctxp->interface_number = 0; | |
3076 | CLASS_COMPLETE_P (decl) = 1; | |
3077 | add_superinterfaces (decl, interfaces); | |
3078 | ||
7f10c2e2 APB |
3079 | /* If doing xref, store the location at which the inherited class |
3080 | (if any) was seen. */ | |
3081 | if (flag_emit_xref && super) | |
3082 | DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super); | |
3083 | ||
5e942c50 APB |
3084 | /* Eventually sets the @deprecated tag flag */ |
3085 | CHECK_DEPRECATED (decl); | |
3086 | ||
e04a16fb AG |
3087 | return decl; |
3088 | } | |
3089 | ||
3090 | /* Can't use lookup_field () since we don't want to load the class and | |
3091 | can't set the CLASS_LOADED_P flag */ | |
3092 | ||
3093 | static tree | |
3094 | find_field (class, name) | |
3095 | tree class; | |
3096 | tree name; | |
3097 | { | |
3098 | tree decl; | |
3099 | for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl)) | |
3100 | { | |
3101 | if (DECL_NAME (decl) == name) | |
3102 | return decl; | |
3103 | } | |
3104 | return NULL_TREE; | |
3105 | } | |
3106 | ||
3107 | /* Wrap around lookup_field that doesn't potentially upset the value | |
3108 | of CLASS */ | |
3109 | ||
3110 | static tree | |
3111 | lookup_field_wrapper (class, name) | |
3112 | tree class, name; | |
3113 | { | |
3114 | tree type = class; | |
5b09b33e | 3115 | tree decl; |
c877974e | 3116 | java_parser_context_save_global (); |
5b09b33e | 3117 | decl = lookup_field (&type, name); |
c877974e | 3118 | java_parser_context_restore_global (); |
93024893 | 3119 | return decl == error_mark_node ? NULL : decl; |
e04a16fb AG |
3120 | } |
3121 | ||
3122 | /* Find duplicate field within the same class declarations and report | |
c583dd46 APB |
3123 | the error. Returns 1 if a duplicated field was found, 0 |
3124 | otherwise. */ | |
e04a16fb AG |
3125 | |
3126 | static int | |
c583dd46 | 3127 | duplicate_declaration_error_p (new_field_name, new_type, cl) |
0a2138e2 | 3128 | tree new_field_name, new_type, cl; |
e04a16fb AG |
3129 | { |
3130 | /* This might be modified to work with method decl as well */ | |
3131 | tree decl = find_field (TREE_TYPE (ctxp->current_parsed_class), | |
3132 | new_field_name); | |
3133 | if (decl) | |
3134 | { | |
c2e3db92 | 3135 | char *t1 = xstrdup (purify_type_name |
4a5f66c3 APB |
3136 | ((TREE_CODE (new_type) == POINTER_TYPE |
3137 | && TREE_TYPE (new_type) == NULL_TREE) ? | |
3138 | IDENTIFIER_POINTER (TYPE_NAME (new_type)) : | |
3139 | lang_printable_name (new_type, 1))); | |
c877974e APB |
3140 | /* The type may not have been completed by the time we report |
3141 | the error */ | |
c2e3db92 | 3142 | char *t2 = xstrdup (purify_type_name |
4a5f66c3 | 3143 | ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE |
c877974e APB |
3144 | && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ? |
3145 | IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) : | |
3146 | lang_printable_name (TREE_TYPE (decl), 1))); | |
e04a16fb AG |
3147 | parse_error_context |
3148 | (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)", | |
3149 | t1, IDENTIFIER_POINTER (new_field_name), | |
3150 | t2, IDENTIFIER_POINTER (DECL_NAME (decl)), | |
3151 | DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl)); | |
3152 | free (t1); | |
3153 | free (t2); | |
c583dd46 | 3154 | return 1; |
e04a16fb | 3155 | } |
c583dd46 | 3156 | return 0; |
e04a16fb AG |
3157 | } |
3158 | ||
3159 | /* Field registration routine. If TYPE doesn't exist, field | |
3160 | declarations are linked to the undefined TYPE dependency list, to | |
3161 | be later resolved in java_complete_class () */ | |
3162 | ||
3163 | static void | |
3164 | register_fields (flags, type, variable_list) | |
3165 | int flags; | |
3166 | tree type, variable_list; | |
3167 | { | |
c583dd46 | 3168 | tree current, saved_type; |
e04a16fb AG |
3169 | tree class_type = TREE_TYPE (ctxp->current_parsed_class); |
3170 | int saved_lineno = lineno; | |
3171 | int must_chain = 0; | |
3172 | tree wfl = NULL_TREE; | |
3173 | ||
3174 | /* If we're adding fields to interfaces, those fields are public, | |
3175 | static, final */ | |
3176 | if (CLASS_INTERFACE (TYPE_NAME (class_type))) | |
3177 | { | |
3178 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), | |
3179 | flags, ACC_PUBLIC, | |
3180 | "%s", "interface field(s)"); | |
3181 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK), | |
3182 | flags, ACC_STATIC, | |
3183 | "%s", "interface field(s)"); | |
3184 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK), | |
3185 | flags, ACC_FINAL, "%s", "interface field(s)"); | |
3186 | check_modifiers ("Illegal interface member modifier `%s'", flags, | |
3187 | INTERFACE_FIELD_MODIFIERS); | |
3188 | flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL); | |
3189 | } | |
3190 | ||
c583dd46 APB |
3191 | /* Obtain a suitable type for resolution, if necessary */ |
3192 | SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain); | |
3193 | ||
3194 | /* If TYPE is fully resolved and we don't have a reference, make one */ | |
1886c9d8 | 3195 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
e04a16fb | 3196 | |
c583dd46 APB |
3197 | for (current = variable_list, saved_type = type; current; |
3198 | current = TREE_CHAIN (current), type = saved_type) | |
e04a16fb | 3199 | { |
c877974e | 3200 | tree real_type; |
c583dd46 | 3201 | tree field_decl; |
e04a16fb AG |
3202 | tree cl = TREE_PURPOSE (current); |
3203 | tree init = TREE_VALUE (current); | |
3204 | tree current_name = EXPR_WFL_NODE (cl); | |
3205 | ||
c583dd46 APB |
3206 | /* Process NAME, as it may specify extra dimension(s) for it */ |
3207 | type = build_array_from_name (type, wfl, current_name, ¤t_name); | |
3208 | ||
c583dd46 APB |
3209 | /* Type adjustment. We may have just readjusted TYPE because |
3210 | the variable specified more dimensions. Make sure we have | |
22eed1e6 APB |
3211 | a reference if we can and don't have one already. Also |
3212 | change the name if we have an init. */ | |
3213 | if (type != saved_type) | |
3214 | { | |
1886c9d8 | 3215 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
22eed1e6 APB |
3216 | if (init) |
3217 | EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name; | |
3218 | } | |
e04a16fb | 3219 | |
c877974e APB |
3220 | real_type = GET_REAL_TYPE (type); |
3221 | /* Check for redeclarations */ | |
3222 | if (duplicate_declaration_error_p (current_name, real_type, cl)) | |
3223 | continue; | |
3224 | ||
c583dd46 | 3225 | /* Set lineno to the line the field was found and create a |
5e942c50 | 3226 | declaration for it. Eventually sets the @deprecated tag flag. */ |
f099f336 APB |
3227 | if (flag_emit_xref) |
3228 | lineno = EXPR_WFL_LINECOL (cl); | |
3229 | else | |
3230 | lineno = EXPR_WFL_LINENO (cl); | |
c877974e | 3231 | field_decl = add_field (class_type, current_name, real_type, flags); |
5e942c50 | 3232 | CHECK_DEPRECATED (field_decl); |
c583dd46 APB |
3233 | |
3234 | /* Check if we must chain. */ | |
3235 | if (must_chain) | |
3236 | register_incomplete_type (JDEP_FIELD, wfl, field_decl, type); | |
e04a16fb | 3237 | |
c583dd46 APB |
3238 | /* If we have an initialization value tied to the field */ |
3239 | if (init) | |
3240 | { | |
3241 | /* The field is declared static */ | |
e04a16fb | 3242 | if (flags & ACC_STATIC) |
e04a16fb | 3243 | { |
7525cc04 APB |
3244 | /* We include the field and its initialization part into |
3245 | a list used to generate <clinit>. After <clinit> is | |
ba179f9f APB |
3246 | walked, field initializations will be processed and |
3247 | fields initialized with known constants will be taken | |
3248 | out of <clinit> and have their DECL_INITIAL set | |
7525cc04 APB |
3249 | appropriately. */ |
3250 | TREE_CHAIN (init) = ctxp->static_initialized; | |
3251 | ctxp->static_initialized = init; | |
7f10c2e2 APB |
3252 | if (TREE_OPERAND (init, 1) |
3253 | && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT) | |
5bba4807 | 3254 | TREE_STATIC (TREE_OPERAND (init, 1)) = 1; |
e04a16fb | 3255 | } |
5e942c50 APB |
3256 | /* A non-static field declared with an immediate initialization is |
3257 | to be initialized in <init>, if any. This field is remembered | |
3258 | to be processed at the time of the generation of <init>. */ | |
c583dd46 APB |
3259 | else |
3260 | { | |
3261 | TREE_CHAIN (init) = ctxp->non_static_initialized; | |
3262 | ctxp->non_static_initialized = init; | |
3263 | } | |
5b09b33e | 3264 | MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1; |
8576f094 | 3265 | DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1); |
e04a16fb AG |
3266 | } |
3267 | } | |
3268 | lineno = saved_lineno; | |
3269 | } | |
3270 | ||
89e09b9a | 3271 | /* Generate the method $finit$ that initializes fields initialized |
22eed1e6 APB |
3272 | upon declaration. */ |
3273 | ||
3274 | static void | |
3275 | maybe_generate_finit () | |
3276 | { | |
3277 | tree mdecl, current; | |
3278 | ||
3279 | if (!ctxp->non_static_initialized || java_error_count) | |
3280 | return; | |
3281 | ||
3282 | mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class), | |
89e09b9a | 3283 | ACC_PRIVATE, void_type_node, |
de4c7b02 | 3284 | finit_identifier_node, end_params_node); |
22eed1e6 APB |
3285 | start_artificial_method_body (mdecl); |
3286 | ||
3287 | ctxp->non_static_initialized = nreverse (ctxp->non_static_initialized); | |
3288 | for (current = ctxp->non_static_initialized; current; | |
3289 | current = TREE_CHAIN (current)) | |
3290 | java_method_add_stmt (mdecl, | |
3291 | build_debugable_stmt (EXPR_WFL_LINECOL (current), | |
3292 | current)); | |
3293 | ||
3294 | end_artificial_method_body (mdecl); | |
3295 | CLASS_HAS_FINIT_P (TREE_TYPE (ctxp->current_parsed_class)) = 1; | |
3296 | ctxp->non_static_initialized = NULL_TREE; | |
3297 | } | |
3298 | ||
e04a16fb AG |
3299 | /* Check whether it is necessary to generate a <clinit> for the class |
3300 | we just parsed. */ | |
3301 | ||
3302 | static void | |
3303 | maybe_generate_clinit () | |
3304 | { | |
22eed1e6 | 3305 | tree mdecl, c; |
e04a16fb AG |
3306 | |
3307 | if (!ctxp->static_initialized || java_error_count) | |
3308 | return; | |
3309 | ||
22eed1e6 APB |
3310 | mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class), |
3311 | ACC_STATIC, void_type_node, | |
de4c7b02 | 3312 | clinit_identifier_node, end_params_node); |
22eed1e6 | 3313 | start_artificial_method_body (mdecl); |
e04a16fb AG |
3314 | |
3315 | /* Keep initialization in order to enforce 8.5 */ | |
3316 | ctxp->static_initialized = nreverse (ctxp->static_initialized); | |
3317 | ||
3318 | /* We process the list of assignment we produced as the result of | |
3319 | the declaration of initialized static field and add them as | |
3320 | statement to the <clinit> method. */ | |
3321 | for (c = ctxp->static_initialized; c; c = TREE_CHAIN (c)) | |
3322 | { | |
3323 | /* We build the assignment expression that will initialize the | |
3324 | field to its value. There are strict rules on static | |
3325 | initializers (8.5). FIXME */ | |
22eed1e6 APB |
3326 | java_method_add_stmt (mdecl, |
3327 | build_debugable_stmt (EXPR_WFL_LINECOL (c), c)); | |
e04a16fb AG |
3328 | } |
3329 | ||
22eed1e6 | 3330 | end_artificial_method_body (mdecl); |
e04a16fb AG |
3331 | ctxp->static_initialized = NULL_TREE; |
3332 | } | |
3333 | ||
3334 | /* Shared accros method_declarator and method_header to remember the | |
3335 | patch stage that was reached during the declaration of the method. | |
3336 | A method DECL is built differently is there is no patch | |
3337 | (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN) | |
3338 | pending on the currently defined method. */ | |
3339 | ||
3340 | static int patch_stage; | |
3341 | ||
3342 | /* Check the method declaration and add the method to its current | |
3343 | class. If the argument list is known to contain incomplete types, | |
3344 | the method is partially added and the registration will be resume | |
22eed1e6 APB |
3345 | once the method arguments resolved. If TYPE is NULL, we're dealing |
3346 | with a constructor. */ | |
e04a16fb AG |
3347 | |
3348 | static tree | |
3349 | method_header (flags, type, mdecl, throws) | |
3350 | int flags; | |
3351 | tree type, mdecl, throws; | |
3352 | { | |
3353 | tree meth = TREE_VALUE (mdecl); | |
3354 | tree id = TREE_PURPOSE (mdecl); | |
1886c9d8 | 3355 | tree type_wfl = NULL_TREE; |
79d13333 APB |
3356 | tree meth_name = NULL_TREE; |
3357 | tree current, orig_arg, this_class; | |
e04a16fb | 3358 | int saved_lineno; |
1886c9d8 | 3359 | int constructor_ok = 0, must_chain; |
e04a16fb AG |
3360 | |
3361 | check_modifiers_consistency (flags); | |
79d13333 APB |
3362 | |
3363 | if (ctxp->current_parsed_class) | |
3364 | this_class = TREE_TYPE (ctxp->current_parsed_class); | |
3365 | else | |
3366 | return NULL_TREE; | |
e04a16fb AG |
3367 | |
3368 | /* There are some forbidden modifiers for an abstract method and its | |
3369 | class must be abstract as well. */ | |
22eed1e6 | 3370 | if (type && (flags & ACC_ABSTRACT)) |
e04a16fb AG |
3371 | { |
3372 | ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private"); | |
3373 | ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static"); | |
3374 | ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final"); | |
3375 | ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native"); | |
3376 | ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized"); | |
2aa11e97 APB |
3377 | if (!CLASS_ABSTRACT (TYPE_NAME (this_class)) |
3378 | && !CLASS_INTERFACE (TYPE_NAME (this_class))) | |
e04a16fb AG |
3379 | parse_error_context |
3380 | (id, "Class `%s' must be declared abstract to define abstract " | |
3381 | "method `%s'", | |
3382 | IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)), | |
3383 | IDENTIFIER_POINTER (EXPR_WFL_NODE (id))); | |
3384 | } | |
22eed1e6 APB |
3385 | /* Things to be checked when declaring a constructor */ |
3386 | if (!type) | |
3387 | { | |
3388 | int ec = java_error_count; | |
3389 | /* 8.6: Constructor declarations: we might be trying to define a | |
3390 | method without specifying a return type. */ | |
5e942c50 | 3391 | if (EXPR_WFL_NODE (id) != ctxp->current_parsed_class_un) |
22eed1e6 APB |
3392 | parse_error_context |
3393 | (id, "Invalid method declaration, return type required"); | |
3394 | /* 8.6.3: Constructor modifiers */ | |
3395 | else | |
3396 | { | |
3397 | JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract"); | |
3398 | JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static"); | |
3399 | JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final"); | |
3400 | JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native"); | |
3401 | JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized"); | |
3402 | } | |
3403 | /* If we found error here, we don't consider it's OK to tread | |
3404 | the method definition as a constructor, for the rest of this | |
3405 | function */ | |
3406 | if (ec == java_error_count) | |
3407 | constructor_ok = 1; | |
3408 | } | |
e04a16fb AG |
3409 | |
3410 | /* Method declared within the scope of an interface are implicitly | |
3411 | abstract and public. Conflicts with other erroneously provided | |
c0d87ff6 | 3412 | modifiers are checked right after. */ |
e04a16fb AG |
3413 | |
3414 | if (CLASS_INTERFACE (TYPE_NAME (this_class))) | |
3415 | { | |
3416 | /* If FLAGS isn't set because of a modifier, turn the | |
3417 | corresponding modifier WFL to NULL so we issue a warning on | |
3418 | the obsolete use of the modifier */ | |
3419 | if (!(flags & ACC_PUBLIC)) | |
3420 | MODIFIER_WFL (PUBLIC_TK) = NULL; | |
3421 | if (!(flags & ACC_ABSTRACT)) | |
3422 | MODIFIER_WFL (ABSTRACT_TK) = NULL; | |
3423 | flags |= ACC_PUBLIC; | |
3424 | flags |= ACC_ABSTRACT; | |
3425 | } | |
3426 | ||
3427 | /* Modifiers context reset moved up, so abstract method declaration | |
3428 | modifiers can be later checked. */ | |
3429 | ||
22eed1e6 APB |
3430 | /* Set constructor returned type to void and method name to <init>, |
3431 | unless we found an error identifier the constructor (in which | |
3432 | case we retain the original name) */ | |
3433 | if (!type) | |
3434 | { | |
3435 | type = void_type_node; | |
3436 | if (constructor_ok) | |
3437 | meth_name = init_identifier_node; | |
3438 | } | |
3439 | else | |
3440 | meth_name = EXPR_WFL_NODE (id); | |
e04a16fb | 3441 | |
1886c9d8 APB |
3442 | /* Do the returned type resolution and registration if necessary */ |
3443 | SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain); | |
3444 | ||
4a5f66c3 APB |
3445 | if (meth_name) |
3446 | type = build_array_from_name (type, type_wfl, meth_name, &meth_name); | |
1886c9d8 APB |
3447 | EXPR_WFL_NODE (id) = meth_name; |
3448 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); | |
3449 | ||
3450 | if (must_chain) | |
e04a16fb | 3451 | { |
1886c9d8 APB |
3452 | patch_stage = JDEP_METHOD_RETURN; |
3453 | register_incomplete_type (patch_stage, type_wfl, id, type); | |
3454 | TREE_TYPE (meth) = GET_REAL_TYPE (type); | |
e04a16fb AG |
3455 | } |
3456 | else | |
1886c9d8 | 3457 | TREE_TYPE (meth) = type; |
e04a16fb AG |
3458 | |
3459 | saved_lineno = lineno; | |
3460 | /* When defining an abstract or interface method, the curly | |
3461 | bracket at level 1 doesn't exist because there is no function | |
3462 | body */ | |
3463 | lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 : | |
3464 | EXPR_WFL_LINENO (id)); | |
3465 | ||
5e942c50 APB |
3466 | /* Remember the original argument list */ |
3467 | orig_arg = TYPE_ARG_TYPES (meth); | |
3468 | ||
e04a16fb AG |
3469 | if (patch_stage) /* includes ret type and/or all args */ |
3470 | { | |
3471 | jdep *jdep; | |
3472 | meth = add_method_1 (this_class, flags, meth_name, meth); | |
3473 | /* Patch for the return type */ | |
3474 | if (patch_stage == JDEP_METHOD_RETURN) | |
3475 | { | |
3476 | jdep = CLASSD_LAST (ctxp->classd_list); | |
3477 | JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth)); | |
3478 | } | |
3479 | /* This is the stop JDEP. METH allows the function's signature | |
3480 | to be computed. */ | |
3481 | register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE); | |
3482 | } | |
3483 | else | |
5e942c50 APB |
3484 | meth = add_method (this_class, flags, meth_name, |
3485 | build_java_signature (meth)); | |
3486 | ||
3487 | /* Fix the method argument list so we have the argument name | |
3488 | information */ | |
3489 | fix_method_argument_names (orig_arg, meth); | |
3490 | ||
3491 | /* Register the parameter number and re-install the current line | |
3492 | number */ | |
e04a16fb AG |
3493 | DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1; |
3494 | lineno = saved_lineno; | |
b9f7e36c APB |
3495 | |
3496 | /* Register exception specified by the `throws' keyword for | |
3497 | resolution and set the method decl appropriate field to the list. | |
3498 | Note: the grammar ensures that what we get here are class | |
3499 | types. */ | |
3500 | if (throws) | |
3501 | { | |
3502 | throws = nreverse (throws); | |
3503 | for (current = throws; current; current = TREE_CHAIN (current)) | |
3504 | { | |
3505 | register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current), | |
3506 | NULL_TREE, NULL_TREE); | |
3507 | JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) = | |
3508 | &TREE_VALUE (current); | |
3509 | } | |
3510 | DECL_FUNCTION_THROWS (meth) = throws; | |
3511 | } | |
3512 | ||
e04a16fb AG |
3513 | /* We set the DECL_NAME to ID so we can track the location where |
3514 | the function was declared. This allow us to report | |
3515 | redefinition error accurately. When method are verified, | |
3516 | DECL_NAME is reinstalled properly (using the content of the | |
3517 | WFL node ID) (see check_method_redefinition). We don't do that | |
22eed1e6 APB |
3518 | when Object is being defined. Constructor <init> names will be |
3519 | reinstalled the same way. */ | |
e04a16fb AG |
3520 | if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node) |
3521 | DECL_NAME (meth) = id; | |
22eed1e6 APB |
3522 | |
3523 | /* Set the flag if we correctly processed a constructor */ | |
3524 | if (constructor_ok) | |
3525 | DECL_CONSTRUCTOR_P (meth) = 1; | |
3526 | ||
5e942c50 APB |
3527 | /* Eventually set the @deprecated tag flag */ |
3528 | CHECK_DEPRECATED (meth); | |
3529 | ||
7f10c2e2 APB |
3530 | /* If doing xref, store column and line number information instead |
3531 | of the line number only. */ | |
3532 | if (flag_emit_xref) | |
3533 | DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id); | |
3534 | ||
e04a16fb AG |
3535 | return meth; |
3536 | } | |
3537 | ||
5e942c50 APB |
3538 | static void |
3539 | fix_method_argument_names (orig_arg, meth) | |
3540 | tree orig_arg, meth; | |
3541 | { | |
3542 | tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth)); | |
3543 | if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE) | |
3544 | { | |
3545 | TREE_PURPOSE (arg) = this_identifier_node; | |
3546 | arg = TREE_CHAIN (arg); | |
3547 | } | |
de4c7b02 | 3548 | while (orig_arg != end_params_node) |
5e942c50 APB |
3549 | { |
3550 | TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg); | |
3551 | orig_arg = TREE_CHAIN (orig_arg); | |
3552 | arg = TREE_CHAIN (arg); | |
3553 | } | |
3554 | } | |
3555 | ||
22eed1e6 APB |
3556 | /* Complete the method declaration with METHOD_BODY. */ |
3557 | ||
3558 | static void | |
b635eb2f | 3559 | finish_method_declaration (method_body) |
22eed1e6 APB |
3560 | tree method_body; |
3561 | { | |
79d13333 APB |
3562 | int flags; |
3563 | ||
3564 | if (!current_function_decl) | |
3565 | return; | |
3566 | ||
3567 | flags = get_access_flags_from_decl (current_function_decl); | |
5256aa37 APB |
3568 | |
3569 | /* 8.4.5 Method Body */ | |
3570 | if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body) | |
3571 | { | |
3572 | tree wfl = DECL_NAME (current_function_decl); | |
3573 | parse_error_context (wfl, | |
3574 | "%s method `%s' can't have a body defined", | |
3575 | (METHOD_NATIVE (current_function_decl) ? | |
3576 | "Native" : "Abstract"), | |
3577 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
3578 | method_body = NULL_TREE; | |
3579 | } | |
3580 | else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body) | |
3581 | { | |
3582 | tree wfl = DECL_NAME (current_function_decl); | |
3583 | parse_error_context (wfl, | |
3584 | "Non native and non abstract method `%s' must " | |
3585 | "have a body defined", | |
3586 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
3587 | method_body = NULL_TREE; | |
3588 | } | |
3589 | ||
2c56429a APB |
3590 | if (flag_emit_class_files && method_body |
3591 | && TREE_CODE (method_body) == NOP_EXPR | |
3592 | && TREE_TYPE (current_function_decl) | |
3593 | && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node) | |
3594 | method_body = build1 (RETURN_EXPR, void_type_node, NULL); | |
3595 | ||
22eed1e6 APB |
3596 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body; |
3597 | maybe_absorb_scoping_blocks (); | |
3598 | /* Exit function's body */ | |
3599 | exit_block (); | |
3600 | /* Merge last line of the function with first line, directly in the | |
3601 | function decl. It will be used to emit correct debug info. */ | |
7f10c2e2 APB |
3602 | if (!flag_emit_xref) |
3603 | DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1); | |
f099f336 APB |
3604 | /* So we don't have an irrelevant function declaration context for |
3605 | the next static block we'll see. */ | |
3606 | current_function_decl = NULL_TREE; | |
22eed1e6 APB |
3607 | } |
3608 | ||
3609 | /* Build a an error message for constructor circularity errors. */ | |
3610 | ||
3611 | static char * | |
3612 | constructor_circularity_msg (from, to) | |
3613 | tree from, to; | |
3614 | { | |
3615 | static char string [4096]; | |
c2e3db92 | 3616 | char *t = xstrdup (lang_printable_name (from, 0)); |
22eed1e6 APB |
3617 | sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0)); |
3618 | free (t); | |
3619 | return string; | |
3620 | } | |
3621 | ||
3622 | /* Verify a circular call to METH. Return 1 if an error is found, 0 | |
3623 | otherwise. */ | |
3624 | ||
3625 | static int | |
3626 | verify_constructor_circularity (meth, current) | |
3627 | tree meth, current; | |
3628 | { | |
3629 | static tree list = NULL_TREE; | |
3630 | tree c; | |
3631 | for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c)) | |
3632 | { | |
3633 | if (TREE_VALUE (c) == meth) | |
3634 | { | |
3635 | char *t; | |
3636 | if (list) | |
3637 | { | |
3638 | tree liste; | |
3639 | list = nreverse (list); | |
3640 | for (liste = list; liste; liste = TREE_CHAIN (liste)) | |
3641 | { | |
3642 | parse_error_context | |
3643 | (TREE_PURPOSE (TREE_PURPOSE (liste)), | |
3644 | constructor_circularity_msg | |
3645 | (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste)))); | |
3646 | java_error_count--; | |
3647 | } | |
3648 | } | |
c2e3db92 | 3649 | t = xstrdup (lang_printable_name (meth, 0)); |
22eed1e6 APB |
3650 | parse_error_context (TREE_PURPOSE (c), |
3651 | "%s: recursive invocation of constructor `%s'", | |
3652 | constructor_circularity_msg (current, meth), t); | |
3653 | free (t); | |
3654 | list = NULL_TREE; | |
3655 | return 1; | |
3656 | } | |
3657 | } | |
3658 | for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c)) | |
3659 | { | |
3660 | list = tree_cons (c, current, list); | |
3661 | if (verify_constructor_circularity (meth, TREE_VALUE (c))) | |
3662 | return 1; | |
3663 | list = TREE_CHAIN (list); | |
3664 | } | |
3665 | return 0; | |
3666 | } | |
3667 | ||
e04a16fb AG |
3668 | /* Check modifiers that can be declared but exclusively */ |
3669 | ||
3670 | static void | |
3671 | check_modifiers_consistency (flags) | |
3672 | int flags; | |
3673 | { | |
3674 | int acc_count = 0; | |
3675 | tree cl = NULL_TREE; | |
3676 | ||
3677 | THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, 0, acc_count, cl); | |
3678 | THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, 1, acc_count, cl); | |
3679 | THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, 2, acc_count, cl); | |
3680 | if (acc_count > 1) | |
3681 | parse_error_context | |
3682 | (cl, "Inconsistent member declaration. At most one of `public', " | |
3683 | "`private', or `protected' may be specified"); | |
3684 | } | |
3685 | ||
3686 | /* Check the methode header METH for abstract specifics features */ | |
3687 | ||
3688 | static void | |
3689 | check_abstract_method_header (meth) | |
3690 | tree meth; | |
3691 | { | |
3692 | int flags = get_access_flags_from_decl (meth); | |
3693 | /* DECL_NAME might still be a WFL node */ | |
c877974e | 3694 | tree name = GET_METHOD_NAME (meth); |
e04a16fb AG |
3695 | |
3696 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (ABSTRACT_TK), flags, | |
3697 | ACC_ABSTRACT, "abstract method `%s'", | |
3698 | IDENTIFIER_POINTER (name)); | |
3699 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), flags, | |
3700 | ACC_PUBLIC, "abstract method `%s'", | |
3701 | IDENTIFIER_POINTER (name)); | |
3702 | ||
3703 | check_modifiers ("Illegal modifier `%s' for interface method", | |
3704 | flags, INTERFACE_METHOD_MODIFIERS); | |
3705 | } | |
3706 | ||
3707 | /* Create a FUNCTION_TYPE node and start augmenting it with the | |
3708 | declared function arguments. Arguments type that can't be resolved | |
3709 | are left as they are, but the returned node is marked as containing | |
3710 | incomplete types. */ | |
3711 | ||
3712 | static tree | |
3713 | method_declarator (id, list) | |
3714 | tree id, list; | |
3715 | { | |
3716 | tree arg_types = NULL_TREE, current, node; | |
3717 | tree meth = make_node (FUNCTION_TYPE); | |
3718 | jdep *jdep; | |
e04a16fb AG |
3719 | |
3720 | patch_stage = JDEP_NO_PATCH; | |
3721 | ||
3722 | for (current = list; current; current = TREE_CHAIN (current)) | |
3723 | { | |
c583dd46 | 3724 | int must_chain = 0; |
e04a16fb AG |
3725 | tree wfl_name = TREE_PURPOSE (current); |
3726 | tree type = TREE_VALUE (current); | |
3727 | tree name = EXPR_WFL_NODE (wfl_name); | |
c583dd46 APB |
3728 | tree already, arg_node; |
3729 | tree type_wfl = NULL_TREE; | |
23a79c61 | 3730 | tree real_type; |
c583dd46 APB |
3731 | |
3732 | /* Obtain a suitable type for resolution, if necessary */ | |
3733 | SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain); | |
3734 | ||
3735 | /* Process NAME, as it may specify extra dimension(s) for it */ | |
3736 | type = build_array_from_name (type, type_wfl, name, &name); | |
3737 | EXPR_WFL_NODE (wfl_name) = name; | |
e04a16fb | 3738 | |
23a79c61 APB |
3739 | real_type = GET_REAL_TYPE (type); |
3740 | if (TREE_CODE (real_type) == RECORD_TYPE) | |
3741 | { | |
3742 | real_type = promote_type (real_type); | |
3743 | if (TREE_CODE (type) == TREE_LIST) | |
3744 | TREE_PURPOSE (type) = real_type; | |
3745 | } | |
5e942c50 | 3746 | |
e04a16fb AG |
3747 | /* Check redefinition */ |
3748 | for (already = arg_types; already; already = TREE_CHAIN (already)) | |
3749 | if (TREE_PURPOSE (already) == name) | |
3750 | { | |
3751 | parse_error_context | |
3752 | (wfl_name, "Variable `%s' is used more than once in the " | |
3753 | "argument list of method `%s'", IDENTIFIER_POINTER (name), | |
3754 | IDENTIFIER_POINTER (EXPR_WFL_NODE (id))); | |
3755 | break; | |
3756 | } | |
3757 | ||
3758 | /* If we've an incomplete argument type, we know there is a location | |
3759 | to patch when the type get resolved, later. */ | |
3760 | jdep = NULL; | |
c583dd46 | 3761 | if (must_chain) |
e04a16fb | 3762 | { |
c583dd46 APB |
3763 | patch_stage = JDEP_METHOD; |
3764 | type = register_incomplete_type (patch_stage, | |
3765 | type_wfl, wfl_name, type); | |
3766 | jdep = CLASSD_LAST (ctxp->classd_list); | |
3767 | JDEP_MISC (jdep) = id; | |
e04a16fb | 3768 | } |
c583dd46 | 3769 | |
e04a16fb | 3770 | /* The argument node: a name and a (possibly) incomplete type */ |
23a79c61 | 3771 | arg_node = build_tree_list (name, real_type); |
e04a16fb AG |
3772 | if (jdep) |
3773 | JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node); | |
3774 | TREE_CHAIN (arg_node) = arg_types; | |
3775 | arg_types = arg_node; | |
3776 | } | |
de4c7b02 | 3777 | TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node); |
e04a16fb AG |
3778 | node = build_tree_list (id, meth); |
3779 | return node; | |
3780 | } | |
3781 | ||
3782 | static int | |
3783 | unresolved_type_p (wfl, returned) | |
3784 | tree wfl; | |
3785 | tree *returned; | |
3786 | ||
3787 | { | |
3788 | if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION) | |
3789 | { | |
3790 | tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl)); | |
3791 | if (returned) | |
3792 | *returned = (decl ? TREE_TYPE (decl) : NULL_TREE); | |
3793 | return 1; | |
3794 | } | |
3795 | if (returned) | |
3796 | *returned = wfl; | |
3797 | return 0; | |
3798 | } | |
3799 | ||
3800 | /* From NAME, build a qualified identifier node using the | |
3801 | qualification from the current package definition. */ | |
3802 | ||
3803 | static tree | |
3804 | parser_qualified_classname (name) | |
3805 | tree name; | |
3806 | { | |
3807 | if (ctxp->package) | |
3808 | return merge_qualified_name (ctxp->package, EXPR_WFL_NODE (name)); | |
3809 | else | |
3810 | return EXPR_WFL_NODE (name); | |
3811 | } | |
3812 | ||
3813 | /* Called once the type a interface extends is resolved. Returns 0 if | |
3814 | everything is OK. */ | |
3815 | ||
3816 | static int | |
3817 | parser_check_super_interface (super_decl, this_decl, this_wfl) | |
3818 | tree super_decl, this_decl, this_wfl; | |
3819 | { | |
3820 | tree super_type = TREE_TYPE (super_decl); | |
3821 | ||
3822 | /* Has to be an interface */ | |
3823 | if (!CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (super_decl)))) | |
3824 | { | |
3825 | parse_error_context | |
3826 | (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'", | |
3827 | (TYPE_ARRAY_P (super_type) ? "array" : "class"), | |
3828 | IDENTIFIER_POINTER (DECL_NAME (super_decl)), | |
3829 | (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ? | |
3830 | "interface" : "class"), | |
3831 | IDENTIFIER_POINTER (DECL_NAME (this_decl))); | |
3832 | return 1; | |
3833 | } | |
3834 | ||
3835 | /* Check scope: same package OK, other package: OK if public */ | |
3836 | if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl))) | |
3837 | return 1; | |
3838 | ||
3839 | SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s", | |
3840 | IDENTIFIER_POINTER (DECL_NAME (this_decl)), | |
3841 | IDENTIFIER_POINTER (DECL_NAME (super_decl)))); | |
3842 | return 0; | |
3843 | } | |
3844 | ||
3845 | /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns | |
3846 | 0 if everthing is OK. */ | |
3847 | ||
3848 | static int | |
3849 | parser_check_super (super_decl, this_decl, wfl) | |
3850 | tree super_decl, this_decl, wfl; | |
3851 | { | |
e04a16fb AG |
3852 | tree super_type = TREE_TYPE (super_decl); |
3853 | ||
3854 | /* SUPER should be a CLASS (neither an array nor an interface) */ | |
3855 | if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type))) | |
3856 | { | |
3857 | parse_error_context | |
3858 | (wfl, "Class `%s' can't subclass %s `%s'", | |
3859 | IDENTIFIER_POINTER (DECL_NAME (this_decl)), | |
3860 | (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"), | |
3861 | IDENTIFIER_POINTER (DECL_NAME (super_decl))); | |
3862 | return 1; | |
3863 | } | |
3864 | ||
3865 | if (CLASS_FINAL (TYPE_NAME (super_type))) | |
3866 | { | |
3867 | parse_error_context (wfl, "Can't subclass final classes: %s", | |
3868 | IDENTIFIER_POINTER (DECL_NAME (super_decl))); | |
3869 | return 1; | |
3870 | } | |
3871 | ||
3872 | /* Check scope: same package OK, other package: OK if public */ | |
3873 | if (check_pkg_class_access (DECL_NAME (super_decl), wfl)) | |
3874 | return 1; | |
3875 | ||
3876 | SOURCE_FRONTEND_DEBUG (("Completing class %s with %s", | |
3877 | IDENTIFIER_POINTER (DECL_NAME (this_decl)), | |
3878 | IDENTIFIER_POINTER (DECL_NAME (super_decl)))); | |
3879 | return 0; | |
3880 | } | |
3881 | ||
3882 | /* Create a new dependency list and link it (in a LIFO manner) to the | |
3883 | CTXP list of type dependency list. */ | |
3884 | ||
3885 | static void | |
3886 | create_jdep_list (ctxp) | |
3887 | struct parser_ctxt *ctxp; | |
3888 | { | |
23a79c61 | 3889 | jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist)); |
e04a16fb AG |
3890 | new->first = new->last = NULL; |
3891 | new->next = ctxp->classd_list; | |
3892 | ctxp->classd_list = new; | |
3893 | } | |
3894 | ||
3895 | static jdeplist * | |
3896 | reverse_jdep_list (ctxp) | |
3897 | struct parser_ctxt *ctxp; | |
3898 | { | |
3899 | register jdeplist *prev = NULL, *current, *next; | |
3900 | for (current = ctxp->classd_list; current; current = next) | |
3901 | { | |
3902 | next = current->next; | |
3903 | current->next = prev; | |
3904 | prev = current; | |
3905 | } | |
3906 | return prev; | |
3907 | } | |
3908 | ||
23a79c61 APB |
3909 | /* Create a fake pointer based on the ID stored in |
3910 | TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be | |
3911 | registered again. */ | |
e04a16fb AG |
3912 | |
3913 | static tree | |
23a79c61 APB |
3914 | obtain_incomplete_type (type_name) |
3915 | tree type_name; | |
e04a16fb | 3916 | { |
23a79c61 APB |
3917 | tree ptr, name; |
3918 | ||
3919 | if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION) | |
3920 | name = EXPR_WFL_NODE (type_name); | |
3921 | else if (INCOMPLETE_TYPE_P (type_name)) | |
3922 | name = TYPE_NAME (type_name); | |
3923 | else | |
3924 | fatal ("invalid type name - obtain_incomplete_type"); | |
e04a16fb AG |
3925 | |
3926 | for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr)) | |
78d21f92 | 3927 | if (TYPE_NAME (ptr) == name) |
e04a16fb AG |
3928 | break; |
3929 | ||
3930 | if (!ptr) | |
3931 | { | |
e04a16fb | 3932 | push_obstacks (&permanent_obstack, &permanent_obstack); |
78d21f92 PB |
3933 | BUILD_PTR_FROM_NAME (ptr, name); |
3934 | layout_type (ptr); | |
e04a16fb AG |
3935 | pop_obstacks (); |
3936 | TREE_CHAIN (ptr) = ctxp->incomplete_class; | |
3937 | ctxp->incomplete_class = ptr; | |
3938 | } | |
3939 | ||
3940 | return ptr; | |
3941 | } | |
3942 | ||
3943 | /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is | |
3944 | non NULL instead of computing a new fake type based on WFL. The new | |
3945 | dependency is inserted in the current type dependency list, in FIFO | |
3946 | manner. */ | |
3947 | ||
3948 | static tree | |
3949 | register_incomplete_type (kind, wfl, decl, ptr) | |
3950 | int kind; | |
3951 | tree wfl, decl, ptr; | |
3952 | { | |
23a79c61 | 3953 | jdep *new = (jdep *)xmalloc (sizeof (jdep)); |
e04a16fb | 3954 | |
e04a16fb AG |
3955 | if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */ |
3956 | ptr = obtain_incomplete_type (wfl); | |
3957 | ||
3958 | JDEP_KIND (new) = kind; | |
3959 | JDEP_DECL (new) = decl; | |
3960 | JDEP_SOLV (new) = ptr; | |
3961 | JDEP_WFL (new) = wfl; | |
3962 | JDEP_CHAIN (new) = NULL; | |
3963 | JDEP_MISC (new) = NULL_TREE; | |
3964 | JDEP_GET_PATCH (new) = (tree *)NULL; | |
3965 | ||
3966 | JDEP_INSERT (ctxp->classd_list, new); | |
3967 | ||
3968 | return ptr; | |
3969 | } | |
3970 | ||
3971 | void | |
3972 | java_check_circular_reference () | |
3973 | { | |
3974 | tree current; | |
3975 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) | |
3976 | { | |
3977 | tree type = TREE_TYPE (current); | |
3978 | if (CLASS_INTERFACE (TYPE_NAME (type))) | |
3979 | { | |
3980 | /* Check all interfaces this class extends */ | |
3981 | tree basetype_vec = TYPE_BINFO_BASETYPES (type); | |
3982 | int n, i; | |
3983 | ||
3984 | if (!basetype_vec) | |
3985 | return; | |
3986 | n = TREE_VEC_LENGTH (basetype_vec); | |
3987 | for (i = 0; i < n; i++) | |
3988 | { | |
3989 | tree vec_elt = TREE_VEC_ELT (basetype_vec, i); | |
3990 | if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node | |
3991 | && interface_of_p (type, BINFO_TYPE (vec_elt))) | |
3992 | parse_error_context (lookup_cl (current), | |
3993 | "Cyclic interface inheritance"); | |
3994 | } | |
3995 | } | |
3996 | else | |
3997 | if (inherits_from_p (CLASSTYPE_SUPER (type), type)) | |
3998 | parse_error_context (lookup_cl (current), | |
3999 | "Cyclic class inheritance"); | |
4000 | } | |
4001 | } | |
4002 | ||
23a79c61 APB |
4003 | /* safe_layout_class just makes sure that we can load a class without |
4004 | disrupting the current_class, input_file, lineno, etc, information | |
4005 | about the class processed currently. */ | |
4006 | ||
e04a16fb AG |
4007 | void |
4008 | safe_layout_class (class) | |
4009 | tree class; | |
4010 | { | |
4011 | tree save_current_class = current_class; | |
4012 | char *save_input_filename = input_filename; | |
4013 | int save_lineno = lineno; | |
5e942c50 | 4014 | |
e04a16fb | 4015 | push_obstacks (&permanent_obstack, &permanent_obstack); |
5e942c50 | 4016 | |
e04a16fb AG |
4017 | layout_class (class); |
4018 | pop_obstacks (); | |
5e942c50 | 4019 | |
e04a16fb AG |
4020 | current_class = save_current_class; |
4021 | input_filename = save_input_filename; | |
4022 | lineno = save_lineno; | |
4023 | CLASS_LOADED_P (class) = 1; | |
4024 | } | |
4025 | ||
4026 | static tree | |
4027 | jdep_resolve_class (dep) | |
4028 | jdep *dep; | |
4029 | { | |
4030 | tree decl; | |
4031 | ||
23a79c61 APB |
4032 | if (JDEP_RESOLVED_P (dep)) |
4033 | decl = JDEP_RESOLVED_DECL (dep); | |
4034 | else | |
e04a16fb | 4035 | { |
23a79c61 APB |
4036 | decl = resolve_class (JDEP_TO_RESOLVE (dep), |
4037 | JDEP_DECL (dep), JDEP_WFL (dep)); | |
e04a16fb AG |
4038 | JDEP_RESOLVED (dep, decl); |
4039 | } | |
23a79c61 | 4040 | |
e04a16fb | 4041 | if (!decl) |
23a79c61 APB |
4042 | complete_class_report_errors (dep); |
4043 | ||
e04a16fb AG |
4044 | return decl; |
4045 | } | |
4046 | ||
4047 | /* Complete unsatisfied class declaration and their dependencies */ | |
4048 | ||
4049 | void | |
4050 | java_complete_class () | |
4051 | { | |
e04a16fb AG |
4052 | tree cclass; |
4053 | jdeplist *cclassd; | |
4054 | int error_found; | |
b67d701b | 4055 | tree type; |
e04a16fb AG |
4056 | |
4057 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
4058 | ||
4059 | /* Process imports and reverse the import on demand list */ | |
4060 | process_imports (); | |
4061 | if (ctxp->import_demand_list) | |
4062 | ctxp->import_demand_list = nreverse (ctxp->import_demand_list); | |
4063 | ||
4064 | /* Rever things so we have the right order */ | |
4065 | ctxp->class_list = nreverse (ctxp->class_list); | |
4066 | ctxp->classd_list = reverse_jdep_list (ctxp); | |
c877974e | 4067 | |
e04a16fb AG |
4068 | for (cclassd = ctxp->classd_list, cclass = ctxp->class_list; |
4069 | cclass && cclassd; | |
4070 | cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd)) | |
4071 | { | |
4072 | jdep *dep; | |
4073 | for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep)) | |
4074 | { | |
4075 | tree decl; | |
e04a16fb AG |
4076 | if (!(decl = jdep_resolve_class (dep))) |
4077 | continue; | |
4078 | ||
4079 | /* Now it's time to patch */ | |
4080 | switch (JDEP_KIND (dep)) | |
4081 | { | |
4082 | case JDEP_SUPER: | |
4083 | /* Simply patch super */ | |
4084 | if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep))) | |
4085 | continue; | |
4086 | BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO | |
4087 | (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl); | |
4088 | break; | |
4089 | ||
4090 | case JDEP_FIELD: | |
4091 | { | |
4092 | /* We do part of the job done in add_field */ | |
4093 | tree field_decl = JDEP_DECL (dep); | |
4094 | tree field_type = TREE_TYPE (decl); | |
4095 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
e04a16fb | 4096 | if (TREE_CODE (field_type) == RECORD_TYPE) |
e04a16fb AG |
4097 | field_type = promote_type (field_type); |
4098 | pop_obstacks (); | |
4099 | TREE_TYPE (field_decl) = field_type; | |
5e942c50 APB |
4100 | DECL_ALIGN (field_decl) = 0; |
4101 | layout_decl (field_decl, 0); | |
e04a16fb AG |
4102 | SOURCE_FRONTEND_DEBUG |
4103 | (("Completed field/var decl `%s' with `%s'", | |
4104 | IDENTIFIER_POINTER (DECL_NAME (field_decl)), | |
4105 | IDENTIFIER_POINTER (DECL_NAME (decl)))); | |
4106 | break; | |
4107 | } | |
4108 | case JDEP_METHOD: /* We start patching a method */ | |
4109 | case JDEP_METHOD_RETURN: | |
4110 | error_found = 0; | |
4111 | while (1) | |
4112 | { | |
4113 | if (decl) | |
4114 | { | |
b67d701b PB |
4115 | type = TREE_TYPE(decl); |
4116 | if (TREE_CODE (type) == RECORD_TYPE) | |
4117 | type = promote_type (type); | |
e04a16fb AG |
4118 | JDEP_APPLY_PATCH (dep, type); |
4119 | SOURCE_FRONTEND_DEBUG | |
4120 | (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ? | |
4121 | "Completing fct `%s' with ret type `%s'": | |
4122 | "Completing arg `%s' with type `%s'"), | |
4123 | IDENTIFIER_POINTER (EXPR_WFL_NODE | |
4124 | (JDEP_DECL_WFL (dep))), | |
4125 | IDENTIFIER_POINTER (DECL_NAME (decl)))); | |
4126 | } | |
4127 | else | |
4128 | error_found = 1; | |
4129 | dep = JDEP_CHAIN (dep); | |
4130 | if (JDEP_KIND (dep) == JDEP_METHOD_END) | |
4131 | break; | |
4132 | else | |
4133 | decl = jdep_resolve_class (dep); | |
4134 | } | |
4135 | if (!error_found) | |
4136 | { | |
4137 | tree mdecl = JDEP_DECL (dep), signature; | |
4138 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
4139 | /* Recompute and reset the signature */ | |
4140 | signature = build_java_signature (TREE_TYPE (mdecl)); | |
4141 | set_java_signature (TREE_TYPE (mdecl), signature); | |
4142 | pop_obstacks (); | |
4143 | } | |
4144 | else | |
4145 | continue; | |
4146 | break; | |
4147 | ||
4148 | case JDEP_INTERFACE: | |
4149 | if (parser_check_super_interface (decl, JDEP_DECL (dep), | |
4150 | JDEP_WFL (dep))) | |
4151 | continue; | |
4152 | parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep)); | |
4153 | break; | |
4154 | ||
b67d701b | 4155 | case JDEP_PARM: |
e04a16fb | 4156 | case JDEP_VARIABLE: |
b67d701b PB |
4157 | type = TREE_TYPE(decl); |
4158 | if (TREE_CODE (type) == RECORD_TYPE) | |
4159 | type = promote_type (type); | |
4160 | JDEP_APPLY_PATCH (dep, type); | |
e04a16fb AG |
4161 | break; |
4162 | ||
4163 | case JDEP_TYPE: | |
4164 | JDEP_APPLY_PATCH (dep, TREE_TYPE (decl)); | |
4165 | SOURCE_FRONTEND_DEBUG | |
4166 | (("Completing a random type dependency on a '%s' node", | |
4167 | tree_code_name [TREE_CODE (JDEP_DECL (dep))])); | |
4168 | break; | |
4169 | ||
b9f7e36c | 4170 | case JDEP_EXCEPTION: |
c877974e APB |
4171 | JDEP_APPLY_PATCH (dep, TREE_TYPE (decl)); |
4172 | SOURCE_FRONTEND_DEBUG | |
4173 | (("Completing `%s' `throws' argument node", | |
4174 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))))); | |
b9f7e36c APB |
4175 | break; |
4176 | ||
e04a16fb | 4177 | default: |
0a2138e2 APB |
4178 | fatal ("Can't handle patch code %d - java_complete_class", |
4179 | JDEP_KIND (dep)); | |
e04a16fb AG |
4180 | } |
4181 | } | |
4182 | } | |
4183 | pop_obstacks (); | |
4184 | return; | |
4185 | } | |
4186 | ||
4187 | /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an | |
4188 | array. */ | |
4189 | ||
4190 | static tree | |
4191 | resolve_class (class_type, decl, cl) | |
4192 | tree class_type, decl, cl; | |
4193 | { | |
49f48c71 KG |
4194 | const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type)); |
4195 | const char *base = name; | |
78d21f92 PB |
4196 | tree resolved_type = TREE_TYPE (class_type); |
4197 | tree resolved_type_decl; | |
e04a16fb | 4198 | |
78d21f92 PB |
4199 | if (resolved_type != NULL_TREE) |
4200 | { | |
4201 | tree resolved_type_decl = TYPE_NAME (resolved_type); | |
4202 | if (resolved_type_decl == NULL_TREE | |
4203 | || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE) | |
4204 | { | |
4205 | resolved_type_decl = build_decl (TYPE_DECL, | |
4206 | TYPE_NAME (class_type), | |
4207 | resolved_type); | |
4208 | } | |
4209 | return resolved_type_decl; | |
4210 | } | |
4211 | ||
e04a16fb AG |
4212 | /* 1- Check to see if we have an array. If true, find what we really |
4213 | want to resolve */ | |
4214 | while (name[0] == '[') | |
4215 | name++; | |
4216 | if (base != name) | |
4217 | TYPE_NAME (class_type) = get_identifier (name); | |
4218 | ||
4219 | /* 2- Resolve the bare type */ | |
4220 | if (!(resolved_type_decl = do_resolve_class (class_type, decl, cl))) | |
4221 | return NULL_TREE; | |
4222 | resolved_type = TREE_TYPE (resolved_type_decl); | |
4223 | ||
4224 | /* 3- If we have and array, reconstruct the array down to its nesting */ | |
4225 | if (base != name) | |
4226 | { | |
4227 | while (base != name) | |
4228 | { | |
4229 | if (TREE_CODE (resolved_type) == RECORD_TYPE) | |
4230 | resolved_type = promote_type (resolved_type); | |
4231 | resolved_type = build_java_array_type (resolved_type, -1); | |
c583dd46 | 4232 | CLASS_LOADED_P (resolved_type) = 1; |
e04a16fb AG |
4233 | name--; |
4234 | } | |
4235 | /* Build a fake decl for this, since this is what is expected to | |
4236 | be returned. */ | |
4237 | resolved_type_decl = | |
4238 | build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type); | |
4239 | /* Figure how those two things are important for error report. FIXME */ | |
4240 | DECL_SOURCE_LINE (resolved_type_decl) = 0; | |
4241 | DECL_SOURCE_FILE (resolved_type_decl) = input_filename; | |
78d21f92 | 4242 | TYPE_NAME (class_type) = TYPE_NAME (resolved_type); |
e04a16fb | 4243 | } |
78d21f92 | 4244 | TREE_TYPE (class_type) = resolved_type; |
e04a16fb AG |
4245 | return resolved_type_decl; |
4246 | } | |
4247 | ||
4248 | /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL | |
4249 | are used to report error messages. */ | |
4250 | ||
78d21f92 | 4251 | tree |
e04a16fb AG |
4252 | do_resolve_class (class_type, decl, cl) |
4253 | tree class_type; | |
4254 | tree decl; | |
4255 | tree cl; | |
4256 | { | |
4257 | tree new_class_decl; | |
4258 | tree original_name = NULL_TREE; | |
4259 | ||
4260 | /* Do not try to replace TYPE_NAME (class_type) by a variable, since | |
4261 | its is changed by find_in_imports{_on_demand} */ | |
4262 | ||
4263 | /* 1- Check for the type in single imports */ | |
4264 | if (find_in_imports (class_type)) | |
4265 | return NULL_TREE; | |
4266 | ||
4267 | /* 2- And check for the type in the current compilation unit. If it fails, | |
4268 | try with a name qualified with the package name if appropriate. */ | |
e04a16fb AG |
4269 | if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)))) |
4270 | { | |
4271 | if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) && | |
4272 | !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl))) | |
4273 | load_class (TYPE_NAME (class_type), 0); | |
4274 | return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
4275 | } | |
4276 | ||
4277 | original_name = TYPE_NAME (class_type); | |
4278 | if (!QUALIFIED_P (TYPE_NAME (class_type)) && ctxp->package) | |
4279 | TYPE_NAME (class_type) = merge_qualified_name (ctxp->package, | |
4280 | TYPE_NAME (class_type)); | |
bc3ca41b | 4281 | #if 1 |
5e942c50 APB |
4282 | if (!(new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)))) |
4283 | load_class (TYPE_NAME (class_type), 0); | |
e04a16fb AG |
4284 | if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)))) |
4285 | { | |
4286 | if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) && | |
4287 | !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl))) | |
4288 | load_class (TYPE_NAME (class_type), 0); | |
4289 | return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
4290 | } | |
bc3ca41b PB |
4291 | #else |
4292 | new_name = TYPE_NAME (class_type); | |
4293 | if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_name)) != NULL_TREE) | |
4294 | { | |
4295 | if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) && | |
4296 | !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl))) | |
4297 | load_class (new_name, 0); | |
4298 | return IDENTIFIER_CLASS_VALUE (new_name); | |
4299 | } | |
4300 | else | |
4301 | { | |
4302 | tree class = read_class (new_name); | |
4303 | if (class != NULL_TREE) | |
4304 | { | |
4305 | tree decl = IDENTIFIER_CLASS_VALUE (new_name); | |
4306 | if (decl == NULL_TREE) | |
4307 | decl = push_class (class, new_name); | |
4308 | return decl; | |
4309 | } | |
4310 | } | |
4311 | #endif | |
e04a16fb AG |
4312 | TYPE_NAME (class_type) = original_name; |
4313 | ||
4314 | /* 3- Check an other compilation unit that bears the name of type */ | |
4315 | load_class (TYPE_NAME (class_type), 0); | |
4316 | if (check_pkg_class_access (TYPE_NAME (class_type), | |
4317 | (cl ? cl : lookup_cl (decl)))) | |
4318 | return NULL_TREE; | |
4319 | ||
4320 | if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)))) | |
4321 | return new_class_decl; | |
4322 | ||
4323 | /* 4- Check the import on demands. Don't allow bar.baz to be | |
4324 | imported from foo.* */ | |
4325 | if (!QUALIFIED_P (TYPE_NAME (class_type))) | |
4326 | if (find_in_imports_on_demand (class_type)) | |
4327 | return NULL_TREE; | |
4328 | ||
4329 | /* 5- Last call for a resolution */ | |
4330 | return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
4331 | } | |
4332 | ||
4333 | /* Resolve NAME and lay it out (if not done and if not the current | |
23a79c61 APB |
4334 | parsed class). Return a decl node. This function is meant to be |
4335 | called when type resolution is necessary during the walk pass. */ | |
e04a16fb AG |
4336 | |
4337 | static tree | |
c877974e APB |
4338 | resolve_and_layout (something, cl) |
4339 | tree something; | |
e04a16fb AG |
4340 | tree cl; |
4341 | { | |
c877974e APB |
4342 | tree decl; |
4343 | ||
23a79c61 APB |
4344 | /* Don't do that on the current class */ |
4345 | if (something == current_class) | |
4346 | return TYPE_NAME (current_class); | |
c877974e | 4347 | |
23a79c61 | 4348 | /* Don't do anything for void and other primitive types */ |
c877974e APB |
4349 | if (JPRIMITIVE_TYPE_P (something) || something == void_type_node) |
4350 | return NULL_TREE; | |
4351 | ||
23a79c61 APB |
4352 | /* Pointer types can be reall pointer types or fake pointers. When |
4353 | finding a real pointer, recheck for primitive types */ | |
4354 | if (TREE_CODE (something) == POINTER_TYPE) | |
4355 | { | |
4356 | if (TREE_TYPE (something)) | |
4357 | { | |
4358 | something = TREE_TYPE (something); | |
4359 | if (JPRIMITIVE_TYPE_P (something) || something == void_type_node) | |
4360 | return NULL_TREE; | |
4361 | } | |
4362 | else | |
4363 | something = TYPE_NAME (something); | |
4364 | } | |
4365 | ||
4366 | /* Don't do anything for arrays of primitive types */ | |
4367 | if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something) | |
4368 | && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something))) | |
4369 | return NULL_TREE; | |
4370 | ||
4371 | /* If something is not and IDENTIFIER_NODE, it can be a a TYPE_DECL | |
4372 | or a real TYPE */ | |
c877974e APB |
4373 | if (TREE_CODE (something) != IDENTIFIER_NODE) |
4374 | something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ? | |
4375 | DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something)); | |
4376 | ||
23a79c61 APB |
4377 | if (!(decl = resolve_no_layout (something, cl))) |
4378 | return NULL_TREE; | |
4379 | ||
4380 | /* Resolve and layout if necessary */ | |
4381 | layout_class_methods (TREE_TYPE (decl)); | |
7705e9db APB |
4382 | /* Check methods, but only once */ |
4383 | if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) | |
4384 | && !CLASS_LOADED_P (TREE_TYPE (decl))) | |
23a79c61 APB |
4385 | CHECK_METHODS (decl); |
4386 | if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl))) | |
e04a16fb | 4387 | safe_layout_class (TREE_TYPE (decl)); |
23a79c61 | 4388 | |
e04a16fb AG |
4389 | return decl; |
4390 | } | |
4391 | ||
4392 | /* Resolve a class, returns its decl but doesn't perform any | |
4393 | layout. The current parsing context is saved and restored */ | |
4394 | ||
4395 | static tree | |
4396 | resolve_no_layout (name, cl) | |
4397 | tree name, cl; | |
4398 | { | |
4399 | tree ptr, decl; | |
4400 | BUILD_PTR_FROM_NAME (ptr, name); | |
4401 | java_parser_context_save_global (); | |
4402 | decl = resolve_class (ptr, NULL_TREE, cl); | |
4403 | java_parser_context_restore_global (); | |
4404 | ||
4405 | return decl; | |
4406 | } | |
4407 | ||
23a79c61 APB |
4408 | /* Called when reporting errors. Skip leader '[' in a complex array |
4409 | type description that failed to be resolved. */ | |
e04a16fb | 4410 | |
49f48c71 | 4411 | static const char * |
e04a16fb | 4412 | purify_type_name (name) |
49f48c71 | 4413 | const char *name; |
e04a16fb AG |
4414 | { |
4415 | while (*name && *name == '[') | |
4416 | name++; | |
4417 | return name; | |
4418 | } | |
4419 | ||
4420 | /* The type CURRENT refers to can't be found. We print error messages. */ | |
4421 | ||
4422 | static void | |
4423 | complete_class_report_errors (dep) | |
4424 | jdep *dep; | |
4425 | { | |
49f48c71 | 4426 | const char *name; |
23a79c61 APB |
4427 | |
4428 | if (!JDEP_WFL (dep)) | |
4429 | return; | |
4430 | ||
4431 | name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))); | |
e04a16fb AG |
4432 | switch (JDEP_KIND (dep)) |
4433 | { | |
4434 | case JDEP_SUPER: | |
4435 | parse_error_context | |
4436 | (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found", | |
23a79c61 | 4437 | purify_type_name (name), |
e04a16fb AG |
4438 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); |
4439 | break; | |
4440 | case JDEP_FIELD: | |
4441 | parse_error_context | |
4442 | (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'", | |
23a79c61 | 4443 | purify_type_name (name), |
e04a16fb AG |
4444 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); |
4445 | break; | |
4446 | case JDEP_METHOD: /* Covers arguments */ | |
4447 | parse_error_context | |
4448 | (JDEP_WFL (dep), "Type `%s' not found in the declaration of the " | |
4449 | "argument `%s' of method `%s'", | |
23a79c61 | 4450 | purify_type_name (name), |
e04a16fb AG |
4451 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))), |
4452 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep)))); | |
4453 | break; | |
4454 | case JDEP_METHOD_RETURN: /* Covers return type */ | |
4455 | parse_error_context | |
4456 | (JDEP_WFL (dep), "Type `%s' not found in the declaration of the " | |
4457 | "return type of method `%s'", | |
23a79c61 | 4458 | purify_type_name (name), |
e04a16fb AG |
4459 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep)))); |
4460 | break; | |
4461 | case JDEP_INTERFACE: | |
4462 | parse_error_context | |
4463 | (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found", | |
4464 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))), | |
4465 | (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")), | |
4466 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); | |
4467 | break; | |
4468 | case JDEP_VARIABLE: | |
4469 | parse_error_context | |
4470 | (JDEP_WFL (dep), "Type `%s' not found in the declaration of the " | |
4471 | "local variable `%s'", | |
b67d701b PB |
4472 | purify_type_name (IDENTIFIER_POINTER |
4473 | (EXPR_WFL_NODE (JDEP_WFL (dep)))), | |
e04a16fb AG |
4474 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); |
4475 | break; | |
b9f7e36c APB |
4476 | case JDEP_EXCEPTION: /* As specified by `throws' */ |
4477 | parse_error_context | |
4478 | (JDEP_WFL (dep), "Class `%s' not found in `throws'", | |
4479 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))); | |
4480 | break; | |
0a2138e2 APB |
4481 | default: |
4482 | /* Fix for -Wall. Just break doing nothing. The error will be | |
4483 | caught later */ | |
4484 | break; | |
e04a16fb AG |
4485 | } |
4486 | } | |
4487 | ||
4488 | /* Check uninitialized final. */ | |
4489 | ||
4490 | void | |
4491 | java_check_final () | |
4492 | { | |
4493 | } | |
4494 | ||
22eed1e6 APB |
4495 | /* Return a static string containing the DECL prototype string. If |
4496 | DECL is a constructor, use the class name instead of the form | |
4497 | <init> */ | |
4498 | ||
49f48c71 | 4499 | static const char * |
22eed1e6 APB |
4500 | get_printable_method_name (decl) |
4501 | tree decl; | |
4502 | { | |
49f48c71 | 4503 | const char *to_return; |
9ee9b555 | 4504 | tree name = NULL_TREE; |
22eed1e6 APB |
4505 | |
4506 | if (DECL_CONSTRUCTOR_P (decl)) | |
4507 | { | |
4508 | name = DECL_NAME (decl); | |
5e942c50 | 4509 | DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))); |
22eed1e6 APB |
4510 | } |
4511 | ||
4512 | to_return = lang_printable_name (decl, 0); | |
4513 | if (DECL_CONSTRUCTOR_P (decl)) | |
4514 | DECL_NAME (decl) = name; | |
4515 | ||
4516 | return to_return; | |
4517 | } | |
4518 | ||
5e942c50 APB |
4519 | /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method |
4520 | nevertheless needs to be verfied, 1 otherwise. */ | |
4521 | ||
4522 | static int | |
4523 | reset_method_name (method) | |
4524 | tree method; | |
4525 | { | |
7f1d4866 | 4526 | if (!IS_CLINIT (method) && DECL_NAME (method) != finit_identifier_node) |
5e942c50 APB |
4527 | { |
4528 | /* NAME is just the plain name when Object is being defined */ | |
4529 | if (DECL_CONTEXT (method) != object_type_node) | |
c877974e APB |
4530 | DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ? |
4531 | init_identifier_node : GET_METHOD_NAME (method)); | |
5e942c50 APB |
4532 | return 0; |
4533 | } | |
4534 | else | |
4535 | return 1; | |
4536 | } | |
4537 | ||
c877974e APB |
4538 | /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */ |
4539 | ||
4540 | tree | |
4541 | java_get_real_method_name (method_decl) | |
4542 | tree method_decl; | |
4543 | { | |
4544 | tree method_name = DECL_NAME (method_decl); | |
4545 | if (DECL_CONSTRUCTOR_P (method_decl)) | |
4546 | return init_identifier_node; | |
82371d41 APB |
4547 | |
4548 | /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P | |
4549 | and still can be a constructor. FIXME */ | |
4550 | ||
23a79c61 APB |
4551 | /* Don't confuse method only bearing the name of their class as |
4552 | constructors */ | |
82371d41 APB |
4553 | else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl)) |
4554 | && ctxp | |
4555 | && ctxp->current_parsed_class_un == EXPR_WFL_NODE (method_name) | |
23a79c61 APB |
4556 | && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED |
4557 | && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node) | |
c877974e APB |
4558 | return init_identifier_node; |
4559 | else | |
4560 | return EXPR_WFL_NODE (method_name); | |
4561 | } | |
4562 | ||
22eed1e6 APB |
4563 | /* Track method being redefined inside the same class. As a side |
4564 | effect, set DECL_NAME to an IDENTIFIER (prior entering this | |
d77613be | 4565 | function it's a FWL, so we can track errors more accurately.) */ |
22eed1e6 | 4566 | |
e04a16fb AG |
4567 | static int |
4568 | check_method_redefinition (class, method) | |
4569 | tree class, method; | |
4570 | { | |
4571 | tree redef, name; | |
4572 | tree cl = DECL_NAME (method); | |
c3f2a476 | 4573 | tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method)); |
ba179f9f APB |
4574 | /* decl name of artificial <clinit> and $finit$ doesn't need to be |
4575 | fixed and checked */ | |
5e942c50 APB |
4576 | |
4577 | /* Reset the method name before running the check. If it returns 1, | |
4578 | the method doesn't need to be verified with respect to method | |
4579 | redeclaration and we return 0 */ | |
4580 | if (reset_method_name (method)) | |
e04a16fb | 4581 | return 0; |
5e942c50 APB |
4582 | |
4583 | name = DECL_NAME (method); | |
e04a16fb AG |
4584 | for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef)) |
4585 | { | |
c3f2a476 | 4586 | if (redef == method) |
e04a16fb | 4587 | break; |
c3f2a476 APB |
4588 | if (DECL_NAME (redef) == name |
4589 | && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))) | |
e04a16fb | 4590 | { |
22eed1e6 APB |
4591 | parse_error_context |
4592 | (cl, "Duplicate %s declaration `%s'", | |
4593 | (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"), | |
4594 | get_printable_method_name (redef)); | |
e04a16fb AG |
4595 | return 1; |
4596 | } | |
4597 | } | |
4598 | return 0; | |
4599 | } | |
4600 | ||
d77613be APB |
4601 | static void |
4602 | check_abstract_method_definitions (do_interface, class_decl, type) | |
4603 | int do_interface; | |
4604 | tree class_decl, type; | |
4605 | { | |
4606 | tree class = TREE_TYPE (class_decl); | |
4607 | tree method, end_type; | |
4608 | ||
4609 | end_type = (do_interface ? object_type_node : type); | |
4610 | for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method)) | |
4611 | { | |
4612 | tree other_super, other_method, method_sig, method_name; | |
4613 | int found = 0; | |
4614 | ||
4615 | if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method)) | |
4616 | continue; | |
4617 | ||
4618 | /* Now verify that somewhere in between TYPE and CLASS, | |
4619 | abstract method METHOD gets a non abstract definition | |
4620 | that is inherited by CLASS. */ | |
4621 | ||
4622 | method_sig = build_java_signature (TREE_TYPE (method)); | |
4623 | method_name = DECL_NAME (method); | |
4624 | if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION) | |
4625 | method_name = EXPR_WFL_NODE (method_name); | |
4626 | ||
4627 | for (other_super = class; other_super != end_type; | |
4628 | other_super = CLASSTYPE_SUPER (other_super)) | |
4629 | { | |
4630 | for (other_method = TYPE_METHODS (other_super); other_method; | |
4631 | other_method = TREE_CHAIN (other_method)) | |
4632 | { | |
4633 | tree s = build_java_signature (TREE_TYPE (other_method)); | |
4634 | tree other_name = DECL_NAME (other_method); | |
4635 | ||
4636 | if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION) | |
4637 | other_name = EXPR_WFL_NODE (other_name); | |
4638 | if (!IS_CLINIT (other_method) | |
4639 | && !DECL_CONSTRUCTOR_P (other_method) | |
4640 | && method_name == other_name && method_sig == s) | |
4641 | { | |
4642 | found = 1; | |
4643 | break; | |
4644 | } | |
4645 | } | |
4646 | } | |
4647 | ||
4648 | /* Report that abstract METHOD didn't find an implementation | |
4649 | that CLASS can use. */ | |
4650 | if (!found) | |
4651 | { | |
c2e3db92 | 4652 | char *t = xstrdup (lang_printable_name |
d77613be APB |
4653 | (TREE_TYPE (TREE_TYPE (method)), 0)); |
4654 | tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method))); | |
4655 | tree saved_wfl = NULL_TREE; | |
4656 | ||
4657 | if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION) | |
4658 | { | |
4659 | saved_wfl = DECL_NAME (method); | |
4660 | DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method)); | |
4661 | } | |
4662 | ||
4663 | parse_error_context | |
4664 | (lookup_cl (class_decl), | |
4665 | "Class `%s' doesn't define the abstract method `%s %s' from " | |
4666 | "%s `%s'. This method must be defined or %s `%s' must be " | |
4667 | "declared abstract", | |
4668 | IDENTIFIER_POINTER (DECL_NAME (class_decl)), | |
4669 | t, lang_printable_name (method, 0), | |
4670 | (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ? | |
4671 | "interface" : "class"), | |
4672 | IDENTIFIER_POINTER (ccn), | |
4673 | (CLASS_INTERFACE (class_decl) ? "interface" : "class"), | |
4674 | IDENTIFIER_POINTER (DECL_NAME (class_decl))); | |
4675 | ||
4676 | free (t); | |
4677 | ||
4678 | if (saved_wfl) | |
4679 | DECL_NAME (method) = saved_wfl; | |
4680 | } | |
4681 | } | |
4682 | } | |
4683 | ||
4684 | /* Check that CLASS_DECL somehoow implements all inherited abstract | |
4685 | methods. */ | |
4686 | ||
4687 | static void | |
4688 | java_check_abstract_method_definitions (class_decl) | |
4689 | tree class_decl; | |
4690 | { | |
4691 | tree class = TREE_TYPE (class_decl); | |
4692 | tree super, vector; | |
4693 | int i; | |
4694 | ||
4695 | if (CLASS_ABSTRACT (class_decl)) | |
4696 | return; | |
4697 | ||
4698 | /* Check for inherited types */ | |
4699 | for (super = CLASSTYPE_SUPER (class); super != object_type_node; | |
4700 | super = CLASSTYPE_SUPER (super)) | |
4701 | { | |
4702 | if (!CLASS_ABSTRACT (TYPE_NAME (super))) | |
4703 | continue; | |
4704 | ||
4705 | check_abstract_method_definitions (0, class_decl, super); | |
4706 | } | |
4707 | ||
4708 | /* Check for implemented interfaces. */ | |
4709 | vector = TYPE_BINFO_BASETYPES (class); | |
4710 | for (i = 1; i < TREE_VEC_LENGTH (vector); i++) | |
4711 | { | |
4712 | super = BINFO_TYPE (TREE_VEC_ELT (vector, i)); | |
4713 | check_abstract_method_definitions (1, class_decl, super); | |
4714 | } | |
4715 | } | |
4716 | ||
4717 | /* Check all the methods of CLASS_DECL. Methods are first completed | |
4718 | then checked according to regular method existance rules. If no | |
4719 | constructor for CLASS_DECL were encountered, then build its | |
4720 | declaration. */ | |
e04a16fb AG |
4721 | |
4722 | static void | |
4723 | java_check_regular_methods (class_decl) | |
4724 | tree class_decl; | |
4725 | { | |
5e942c50 | 4726 | int saw_constructor = 0; |
e04a16fb AG |
4727 | tree method; |
4728 | tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl)); | |
4729 | tree super_class = CLASSTYPE_SUPER (class); | |
5e942c50 | 4730 | tree saved_found_wfl = NULL_TREE, found = NULL_TREE; |
c877974e APB |
4731 | tree mthrows; |
4732 | ||
4733 | /* It is not necessary to check methods defined in java.lang.Object */ | |
4734 | if (class == object_type_node) | |
4735 | return; | |
e04a16fb | 4736 | |
23a79c61 APB |
4737 | if (!TYPE_NVIRTUALS (class)) |
4738 | TYPE_METHODS (class) = nreverse (TYPE_METHODS (class)); | |
e04a16fb AG |
4739 | |
4740 | /* Should take interfaces into account. FIXME */ | |
4741 | for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method)) | |
4742 | { | |
5e942c50 | 4743 | tree sig; |
e04a16fb AG |
4744 | tree method_wfl = DECL_NAME (method); |
4745 | int aflags; | |
4746 | ||
5e942c50 APB |
4747 | /* If we previously found something and its name was saved, |
4748 | reinstall it now */ | |
4749 | if (found && saved_found_wfl) | |
ba179f9f APB |
4750 | { |
4751 | DECL_NAME (found) = saved_found_wfl; | |
4752 | saved_found_wfl = NULL_TREE; | |
4753 | } | |
5e942c50 | 4754 | |
e04a16fb AG |
4755 | /* Check for redefinitions */ |
4756 | if (check_method_redefinition (class, method)) | |
4757 | continue; | |
4758 | ||
22eed1e6 APB |
4759 | /* If we see one constructor a mark so we don't generate the |
4760 | default one. Also skip other verifications: constructors | |
4761 | can't be inherited hence hiden or overriden */ | |
4762 | if (DECL_CONSTRUCTOR_P (method)) | |
4763 | { | |
4764 | saw_constructor = 1; | |
4765 | continue; | |
4766 | } | |
4767 | ||
c877974e APB |
4768 | /* We verify things thrown by the method. They must inherits from |
4769 | java.lang.Throwable */ | |
4770 | for (mthrows = DECL_FUNCTION_THROWS (method); | |
4771 | mthrows; mthrows = TREE_CHAIN (mthrows)) | |
4772 | { | |
4773 | if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node)) | |
4774 | parse_error_context | |
4775 | (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be " | |
4776 | "a subclass of class `java.lang.Throwable'", | |
4777 | IDENTIFIER_POINTER | |
4778 | (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows))))); | |
4779 | } | |
4780 | ||
e04a16fb | 4781 | sig = build_java_argument_signature (TREE_TYPE (method)); |
e04a16fb | 4782 | found = lookup_argument_method (super_class, DECL_NAME (method), sig); |
b9f7e36c | 4783 | |
5e942c50 | 4784 | /* Nothing overrides or it's a private method. */ |
aabd7048 | 4785 | if (!found) |
5e942c50 | 4786 | continue; |
aabd7048 PB |
4787 | if (METHOD_PRIVATE (found)) |
4788 | { | |
4789 | found = NULL_TREE; | |
4790 | continue; | |
4791 | } | |
5e942c50 APB |
4792 | |
4793 | /* If found wasn't verified, it's DECL_NAME won't be set properly. | |
4794 | We set it temporarily for the sake of the error report. */ | |
4795 | saved_found_wfl = DECL_NAME (found); | |
4796 | reset_method_name (found); | |
4797 | ||
e04a16fb AG |
4798 | /* Can't override a method with the same name and different return |
4799 | types. */ | |
4800 | if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method))) | |
b9f7e36c | 4801 | { |
c2e3db92 | 4802 | char *t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), |
0a2138e2 | 4803 | 0)); |
b9f7e36c | 4804 | parse_error_context |
7f10c2e2 | 4805 | (method_wfl, |
b9f7e36c | 4806 | "Method `%s' was defined with return type `%s' in class `%s'", |
0a2138e2 | 4807 | lang_printable_name (found, 0), t, |
b9f7e36c APB |
4808 | IDENTIFIER_POINTER |
4809 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
4810 | free (t); | |
4811 | } | |
e04a16fb | 4812 | |
7f10c2e2 APB |
4813 | aflags = get_access_flags_from_decl (found); |
4814 | /* If the method has default, access in an other package, then | |
4815 | issue a warning that the current method doesn't override the | |
4816 | one that was found elsewhere. Do not issue this warning when | |
4817 | the match was found in java.lang.Object. */ | |
4818 | if (DECL_CONTEXT (found) != object_type_node | |
4819 | && ((aflags & 0x7) == 0) | |
4820 | && !class_in_current_package (DECL_CONTEXT (found)) | |
1fb89a4d | 4821 | && DECL_NAME (found) != clinit_identifier_node |
7f10c2e2 APB |
4822 | && flag_not_overriding) |
4823 | { | |
4824 | parse_warning_context | |
4825 | (method_wfl, "Method `%s' in class `%s' does not " | |
4826 | "override the corresponding method in class `%s', which is " | |
4827 | "private to a different package", | |
4828 | lang_printable_name (found, 0), | |
4829 | IDENTIFIER_POINTER (DECL_NAME (class_decl)), | |
4830 | IDENTIFIER_POINTER (DECL_NAME | |
4831 | (TYPE_NAME (DECL_CONTEXT (found))))); | |
4832 | continue; | |
4833 | } | |
4834 | ||
e04a16fb AG |
4835 | /* Can't override final. Can't override static. */ |
4836 | if (METHOD_FINAL (found) || METHOD_STATIC (found)) | |
4837 | { | |
4838 | /* Static *can* override static */ | |
4839 | if (METHOD_STATIC (found) && METHOD_STATIC (method)) | |
4840 | continue; | |
4841 | parse_error_context | |
4842 | (method_wfl, | |
4843 | "%s methods can't be overriden. Method `%s' is %s in class `%s'", | |
4844 | (METHOD_FINAL (found) ? "Final" : "Static"), | |
0a2138e2 | 4845 | lang_printable_name (found, 0), |
e04a16fb AG |
4846 | (METHOD_FINAL (found) ? "final" : "static"), |
4847 | IDENTIFIER_POINTER | |
4848 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
4849 | continue; | |
4850 | } | |
7f10c2e2 | 4851 | |
e04a16fb AG |
4852 | /* Static method can't override instance method. */ |
4853 | if (METHOD_STATIC (method)) | |
4854 | { | |
4855 | parse_error_context | |
4856 | (method_wfl, | |
4857 | "Instance methods can't be overriden by a static method. Method " | |
4858 | "`%s' is an instance method in class `%s'", | |
0a2138e2 | 4859 | lang_printable_name (found, 0), |
e04a16fb AG |
4860 | IDENTIFIER_POINTER |
4861 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
4862 | continue; | |
4863 | } | |
5e942c50 | 4864 | |
5e942c50 APB |
4865 | /* - Overriding/hiding public must be public |
4866 | - Overriding/hiding protected must be protected or public | |
4867 | - If the overriden or hidden method has default (package) | |
4868 | access, then the overriding or hiding method must not be | |
4869 | private; otherwise, a compile-time error occurs */ | |
4870 | if ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method)) | |
4871 | || (METHOD_PROTECTED (found) | |
4872 | && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method))) | |
4873 | || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC)) | |
4874 | && METHOD_PRIVATE (method))) | |
e04a16fb AG |
4875 | { |
4876 | parse_error_context | |
4877 | (method_wfl, | |
4878 | "Methods can't be overridden to be more private. Method `%s' is " | |
5e942c50 APB |
4879 | "not %s in class `%s'", lang_printable_name (method, 0), |
4880 | (METHOD_PUBLIC (method) ? "public" : | |
4881 | (METHOD_PRIVATE (method) ? "private" : "protected")), | |
4882 | IDENTIFIER_POINTER (DECL_NAME | |
4883 | (TYPE_NAME (DECL_CONTEXT (found))))); | |
e04a16fb AG |
4884 | continue; |
4885 | } | |
4886 | ||
b9f7e36c APB |
4887 | /* Overriding methods must have compatible `throws' clauses on checked |
4888 | exceptions, if any */ | |
4889 | check_throws_clauses (method, method_wfl, found); | |
4890 | ||
e04a16fb AG |
4891 | /* Inheriting multiple methods with the same signature. FIXME */ |
4892 | } | |
4893 | ||
5e942c50 APB |
4894 | /* Don't forget eventual pending found and saved_found_wfl. Take |
4895 | into account that we might have exited because we saw an | |
d77613be | 4896 | artificial method as the last entry. */ |
5e942c50 APB |
4897 | |
4898 | if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl) | |
4899 | DECL_NAME (found) = saved_found_wfl; | |
4900 | ||
23a79c61 APB |
4901 | if (!TYPE_NVIRTUALS (class)) |
4902 | TYPE_METHODS (class) = nreverse (TYPE_METHODS (class)); | |
e04a16fb | 4903 | |
d77613be APB |
4904 | /* Search for inherited abstract method not yet implemented in this |
4905 | class. */ | |
4906 | java_check_abstract_method_definitions (class_decl); | |
4907 | ||
22eed1e6 | 4908 | if (!saw_constructor) |
e04a16fb | 4909 | { |
23a79c61 APB |
4910 | /* No constructor seen, we craft one, at line 0. Since this |
4911 | operation takes place after we laid methods out | |
4912 | (layout_class_methods), we prepare the its DECL | |
4913 | appropriately. */ | |
22eed1e6 APB |
4914 | int flags; |
4915 | tree decl; | |
4916 | ||
4917 | /* If the class is declared PUBLIC, the default constructor is | |
4918 | PUBLIC otherwise it has default access implied by no access | |
4919 | modifiers. */ | |
4920 | flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ? | |
4921 | ACC_PUBLIC : 0); | |
4922 | decl = create_artificial_method (class, flags, void_type_node, | |
de4c7b02 | 4923 | init_identifier_node, end_params_node); |
e04a16fb | 4924 | DECL_CONSTRUCTOR_P (decl) = 1; |
23a79c61 | 4925 | layout_class_method (TREE_TYPE (class_decl), NULL_TREE, decl, NULL_TREE); |
e04a16fb AG |
4926 | } |
4927 | } | |
4928 | ||
b9f7e36c APB |
4929 | /* Return a non zero value if the `throws' clause of METHOD (if any) |
4930 | is incompatible with the `throws' clause of FOUND (if any). */ | |
4931 | ||
4932 | static void | |
4933 | check_throws_clauses (method, method_wfl, found) | |
4934 | tree method, method_wfl, found; | |
4935 | { | |
4936 | tree mthrows, fthrows; | |
4937 | ||
c877974e APB |
4938 | /* Can't check these things with class loaded from bytecode. FIXME */ |
4939 | if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found))) | |
4940 | return; | |
4941 | ||
b9f7e36c APB |
4942 | for (mthrows = DECL_FUNCTION_THROWS (method); |
4943 | mthrows; mthrows = TREE_CHAIN (mthrows)) | |
4944 | { | |
4945 | /* We don't verify unchecked expressions */ | |
c877974e | 4946 | if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows))) |
b9f7e36c APB |
4947 | continue; |
4948 | /* Checked expression must be compatible */ | |
4949 | for (fthrows = DECL_FUNCTION_THROWS (found); | |
4950 | fthrows; fthrows = TREE_CHAIN (fthrows)) | |
4951 | if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows))) | |
4952 | break; | |
4953 | if (!fthrows) | |
4954 | { | |
4955 | parse_error_context | |
4956 | (method_wfl, "Invalid checked exception class `%s' in " | |
4957 | "`throws' clause. The exception must be a subclass of an " | |
4958 | "exception thrown by `%s' from class `%s'", | |
4959 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))), | |
0a2138e2 | 4960 | lang_printable_name (found, 0), |
b9f7e36c APB |
4961 | IDENTIFIER_POINTER |
4962 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
4963 | } | |
4964 | } | |
4965 | } | |
4966 | ||
e04a16fb AG |
4967 | /* Check abstract method of interface INTERFACE */ |
4968 | ||
4969 | static void | |
5e942c50 APB |
4970 | java_check_abstract_methods (interface_decl) |
4971 | tree interface_decl; | |
e04a16fb AG |
4972 | { |
4973 | int i, n; | |
4974 | tree method, basetype_vec, found; | |
5e942c50 | 4975 | tree interface = TREE_TYPE (interface_decl); |
e04a16fb AG |
4976 | |
4977 | for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method)) | |
4978 | { | |
b9f7e36c | 4979 | tree method_wfl = DECL_NAME (method); |
e04a16fb AG |
4980 | |
4981 | /* 2- Check for double definition inside the defining interface */ | |
4982 | if (check_method_redefinition (interface, method)) | |
4983 | continue; | |
4984 | ||
4985 | /* 3- Overriding is OK as far as we preserve the return type and | |
b9f7e36c | 4986 | the thrown exceptions (FIXME) */ |
e04a16fb AG |
4987 | found = lookup_java_interface_method2 (interface, method); |
4988 | if (found) | |
4989 | { | |
5e942c50 APB |
4990 | char *t; |
4991 | tree saved_found_wfl = DECL_NAME (found); | |
4992 | reset_method_name (found); | |
c2e3db92 | 4993 | t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0)); |
e04a16fb | 4994 | parse_error_context |
b9f7e36c | 4995 | (method_wfl, |
5e942c50 | 4996 | "Method `%s' was defined with return type `%s' in class `%s'", |
0a2138e2 | 4997 | lang_printable_name (found, 0), t, |
b9f7e36c APB |
4998 | IDENTIFIER_POINTER |
4999 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
5000 | free (t); | |
e04a16fb | 5001 | continue; |
5e942c50 APB |
5002 | |
5003 | DECL_NAME (found) = saved_found_wfl; | |
e04a16fb AG |
5004 | } |
5005 | } | |
5006 | ||
5007 | /* 4- Inherited methods can't differ by their returned types */ | |
5008 | if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface))) | |
5009 | return; | |
5010 | n = TREE_VEC_LENGTH (basetype_vec); | |
5011 | for (i = 0; i < n; i++) | |
5012 | { | |
5013 | tree sub_interface_method, sub_interface; | |
5014 | tree vec_elt = TREE_VEC_ELT (basetype_vec, i); | |
5015 | if (!vec_elt) | |
5016 | continue; | |
5017 | sub_interface = BINFO_TYPE (vec_elt); | |
5018 | for (sub_interface_method = TYPE_METHODS (sub_interface); | |
5019 | sub_interface_method; | |
5020 | sub_interface_method = TREE_CHAIN (sub_interface_method)) | |
5021 | { | |
5022 | found = lookup_java_interface_method2 (interface, | |
5023 | sub_interface_method); | |
5024 | if (found && (found != sub_interface_method)) | |
5e942c50 APB |
5025 | { |
5026 | tree saved_found_wfl = DECL_NAME (found); | |
5027 | reset_method_name (found); | |
5028 | parse_error_context | |
5029 | (lookup_cl (sub_interface_method), | |
5030 | "Interface `%s' inherits method `%s' from interface `%s'. " | |
5031 | "This method is redefined with a different return type in " | |
5032 | "interface `%s'", | |
5033 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))), | |
5034 | lang_printable_name (found, 0), | |
5035 | IDENTIFIER_POINTER | |
5036 | (DECL_NAME (TYPE_NAME | |
5037 | (DECL_CONTEXT (sub_interface_method)))), | |
5038 | IDENTIFIER_POINTER | |
5039 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
5040 | DECL_NAME (found) = saved_found_wfl; | |
5041 | } | |
e04a16fb AG |
5042 | } |
5043 | } | |
5044 | } | |
5045 | ||
e04a16fb AG |
5046 | /* Lookup methods in interfaces using their name and partial |
5047 | signature. Return a matching method only if their types differ. */ | |
5048 | ||
5049 | static tree | |
5050 | lookup_java_interface_method2 (class, method_decl) | |
5051 | tree class, method_decl; | |
5052 | { | |
5053 | int i, n; | |
5054 | tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return; | |
5055 | ||
5056 | if (!basetype_vec) | |
5057 | return NULL_TREE; | |
5058 | ||
5059 | n = TREE_VEC_LENGTH (basetype_vec); | |
5060 | for (i = 0; i < n; i++) | |
5061 | { | |
5062 | tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return; | |
5063 | if ((BINFO_TYPE (vec_elt) != object_type_node) | |
5064 | && (to_return = | |
5065 | lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1))) | |
5066 | return to_return; | |
5067 | } | |
5068 | for (i = 0; i < n; i++) | |
5069 | { | |
5070 | to_return = lookup_java_interface_method2 | |
5071 | (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl); | |
5072 | if (to_return) | |
5073 | return to_return; | |
5074 | } | |
5075 | ||
5076 | return NULL_TREE; | |
5077 | } | |
5078 | ||
5079 | /* Lookup method using their name and partial signature. Return a | |
5080 | matching method only if their types differ. */ | |
5081 | ||
5082 | static tree | |
5083 | lookup_java_method2 (clas, method_decl, do_interface) | |
5084 | tree clas, method_decl; | |
5085 | int do_interface; | |
5086 | { | |
5e942c50 APB |
5087 | tree method, method_signature, method_name, method_type, name; |
5088 | ||
e04a16fb | 5089 | method_signature = build_java_argument_signature (TREE_TYPE (method_decl)); |
5e942c50 APB |
5090 | name = DECL_NAME (method_decl); |
5091 | method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ? | |
5092 | EXPR_WFL_NODE (name) : name); | |
e04a16fb AG |
5093 | method_type = TREE_TYPE (TREE_TYPE (method_decl)); |
5094 | ||
5095 | while (clas != NULL_TREE) | |
5096 | { | |
5097 | for (method = TYPE_METHODS (clas); | |
5098 | method != NULL_TREE; method = TREE_CHAIN (method)) | |
5099 | { | |
5100 | tree method_sig = build_java_argument_signature (TREE_TYPE (method)); | |
5e942c50 APB |
5101 | tree name = DECL_NAME (method); |
5102 | if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ? | |
5103 | EXPR_WFL_NODE (name) : name) == method_name | |
e04a16fb AG |
5104 | && method_sig == method_signature |
5105 | && TREE_TYPE (TREE_TYPE (method)) != method_type) | |
5e942c50 | 5106 | return method; |
e04a16fb AG |
5107 | } |
5108 | clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas)); | |
5109 | } | |
5110 | return NULL_TREE; | |
5111 | } | |
5112 | ||
f441f671 APB |
5113 | /* Return the line that matches DECL line number, and try its best to |
5114 | position the column number. Used during error reports. */ | |
e04a16fb AG |
5115 | |
5116 | static tree | |
5117 | lookup_cl (decl) | |
5118 | tree decl; | |
5119 | { | |
5120 | static tree cl = NULL_TREE; | |
f441f671 | 5121 | char *line, *found; |
e04a16fb AG |
5122 | |
5123 | if (!decl) | |
5124 | return NULL_TREE; | |
5125 | ||
5126 | if (cl == NULL_TREE) | |
5127 | cl = build_expr_wfl (NULL_TREE, NULL, 0, 0); | |
5128 | ||
5129 | EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl)); | |
5130 | EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1); | |
5131 | ||
f441f671 APB |
5132 | line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)), |
5133 | EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl)); | |
5134 | ||
5135 | found = strstr ((const char *)line, | |
5136 | (const char *)IDENTIFIER_POINTER (DECL_NAME (decl))); | |
5137 | if (found) | |
5138 | EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line); | |
5139 | ||
e04a16fb AG |
5140 | return cl; |
5141 | } | |
5142 | ||
5143 | /* Look for a simple name in the single-type import list */ | |
5144 | ||
5145 | static tree | |
5146 | find_name_in_single_imports (name) | |
5147 | tree name; | |
5148 | { | |
5149 | tree node; | |
5150 | ||
5151 | for (node = ctxp->import_list; node; node = TREE_CHAIN (node)) | |
5152 | if (TREE_VALUE (node) == name) | |
5153 | return (EXPR_WFL_NODE (TREE_PURPOSE (node))); | |
5154 | ||
5155 | return NULL_TREE; | |
5156 | } | |
5157 | ||
5158 | /* Process all single-type import. */ | |
5159 | ||
5160 | static int | |
5161 | process_imports () | |
5162 | { | |
5163 | tree import; | |
5164 | int error_found; | |
5165 | ||
5166 | for (import = ctxp->import_list; import; import = TREE_CHAIN (import)) | |
5167 | { | |
5168 | tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import)); | |
5169 | ||
5170 | /* Don't load twice something already defined. */ | |
5171 | if (IDENTIFIER_CLASS_VALUE (to_be_found)) | |
5172 | continue; | |
5173 | QUALIFIED_P (to_be_found) = 1; | |
5174 | load_class (to_be_found, 0); | |
5175 | error_found = | |
5176 | check_pkg_class_access (to_be_found, TREE_PURPOSE (import)); | |
5177 | if (!IDENTIFIER_CLASS_VALUE (to_be_found)) | |
5178 | { | |
5179 | parse_error_context (TREE_PURPOSE (import), | |
5180 | "Class or interface `%s' not found in import", | |
5181 | IDENTIFIER_POINTER (to_be_found)); | |
5182 | return 1; | |
5183 | } | |
5184 | if (error_found) | |
5185 | return 1; | |
5186 | } | |
5187 | return 0; | |
5188 | } | |
5189 | ||
5190 | /* Possibly find a class imported by a single-type import statement. Return | |
5191 | 1 if an error occured, 0 otherwise. */ | |
5192 | ||
5193 | static int | |
5194 | find_in_imports (class_type) | |
5195 | tree class_type; | |
5196 | { | |
5197 | tree import; | |
5198 | ||
5199 | for (import = ctxp->import_list; import; import = TREE_CHAIN (import)) | |
5200 | if (TREE_VALUE (import) == TYPE_NAME (class_type)) | |
5201 | { | |
5202 | TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import)); | |
5203 | QUALIFIED_P (TYPE_NAME (class_type)) = 1; | |
e04a16fb AG |
5204 | } |
5205 | return 0; | |
5206 | } | |
5207 | ||
e04a16fb | 5208 | static int |
63a212ed | 5209 | note_possible_classname (name, len) |
49f48c71 | 5210 | const char *name; |
63a212ed | 5211 | int len; |
e04a16fb | 5212 | { |
63a212ed PB |
5213 | tree node; |
5214 | if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0) | |
5215 | len = len - 5; | |
5216 | else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0) | |
5217 | len = len - 6; | |
e04a16fb | 5218 | else |
63a212ed PB |
5219 | return 0; |
5220 | node = ident_subst (name, len, "", '/', '.', ""); | |
5221 | IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */ | |
fe0e4d76 | 5222 | QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0; |
63a212ed | 5223 | return 1; |
e04a16fb AG |
5224 | } |
5225 | ||
5226 | /* Read a import directory, gathering potential match for further type | |
5227 | references. Indifferently reads a filesystem or a ZIP archive | |
5228 | directory. */ | |
5229 | ||
5230 | static void | |
5231 | read_import_dir (wfl) | |
5232 | tree wfl; | |
5233 | { | |
63a212ed | 5234 | tree package_id = EXPR_WFL_NODE (wfl); |
49f48c71 | 5235 | const char *package_name = IDENTIFIER_POINTER (package_id); |
63a212ed | 5236 | int package_length = IDENTIFIER_LENGTH (package_id); |
e04a16fb | 5237 | DIR *dirp = NULL; |
d8fccff5 | 5238 | JCF *saved_jcf = current_jcf; |
e04a16fb | 5239 | |
63a212ed PB |
5240 | int found = 0; |
5241 | int k; | |
5242 | void *entry; | |
5243 | struct buffer filename[1]; | |
5244 | ||
5245 | ||
5246 | if (IS_AN_IMPORT_ON_DEMAND_P (package_id)) | |
5247 | return; | |
5248 | IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1; | |
5249 | ||
5250 | BUFFER_INIT (filename); | |
5251 | buffer_grow (filename, package_length + 100); | |
5252 | ||
5253 | for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry)) | |
5254 | { | |
49f48c71 | 5255 | const char *entry_name = jcf_path_name (entry); |
63a212ed PB |
5256 | int entry_length = strlen (entry_name); |
5257 | if (jcf_path_is_zipfile (entry)) | |
5258 | { | |
5259 | ZipFile *zipf; | |
5260 | buffer_grow (filename, entry_length); | |
5261 | memcpy (filename->data, entry_name, entry_length - 1); | |
5262 | filename->data[entry_length-1] = '\0'; | |
5263 | zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry)); | |
5264 | if (zipf == NULL) | |
5265 | error ("malformed .zip archive in CLASSPATH: %s", entry_name); | |
5266 | else | |
5267 | { | |
5268 | ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory; | |
5269 | BUFFER_RESET (filename); | |
5270 | for (k = 0; k < package_length; k++) | |
5271 | { | |
5272 | char ch = package_name[k]; | |
5273 | *filename->ptr++ = ch == '.' ? '/' : ch; | |
5274 | } | |
5275 | *filename->ptr++ = '/'; | |
5276 | ||
345137c7 | 5277 | for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd)) |
63a212ed | 5278 | { |
49f48c71 | 5279 | const char *current_entry = ZIPDIR_FILENAME (zipd); |
63a212ed PB |
5280 | int current_entry_len = zipd->filename_length; |
5281 | ||
345137c7 TT |
5282 | if (current_entry_len >= BUFFER_LENGTH (filename) |
5283 | && strncmp (filename->data, current_entry, | |
5284 | BUFFER_LENGTH (filename)) != 0) | |
63a212ed | 5285 | continue; |
345137c7 | 5286 | found |= note_possible_classname (current_entry, |
63a212ed PB |
5287 | current_entry_len); |
5288 | } | |
5289 | } | |
5290 | } | |
5291 | else | |
5292 | { | |
5293 | BUFFER_RESET (filename); | |
5294 | buffer_grow (filename, entry_length + package_length + 4); | |
5295 | strcpy (filename->data, entry_name); | |
5296 | filename->ptr = filename->data + entry_length; | |
5297 | for (k = 0; k < package_length; k++) | |
5298 | { | |
5299 | char ch = package_name[k]; | |
5300 | *filename->ptr++ = ch == '.' ? '/' : ch; | |
5301 | } | |
5302 | *filename->ptr = '\0'; | |
5303 | ||
5304 | dirp = opendir (filename->data); | |
5305 | if (dirp == NULL) | |
5306 | continue; | |
5307 | *filename->ptr++ = '/'; | |
5308 | for (;;) | |
5309 | { | |
63a212ed | 5310 | int len; |
49f48c71 | 5311 | const char *d_name; |
63a212ed PB |
5312 | struct dirent *direntp = readdir (dirp); |
5313 | if (!direntp) | |
5314 | break; | |
5315 | d_name = direntp->d_name; | |
5316 | len = strlen (direntp->d_name); | |
5317 | buffer_grow (filename, len+1); | |
5318 | strcpy (filename->ptr, d_name); | |
345137c7 | 5319 | found |= note_possible_classname (filename->data + entry_length, |
63a212ed PB |
5320 | package_length+len+1); |
5321 | } | |
5322 | if (dirp) | |
5323 | closedir (dirp); | |
5324 | } | |
5325 | } | |
e04a16fb | 5326 | |
63a212ed | 5327 | free (filename->data); |
e04a16fb | 5328 | |
63a212ed PB |
5329 | /* Here we should have a unified way of retrieving an entry, to be |
5330 | indexed. */ | |
5331 | if (!found) | |
e04a16fb AG |
5332 | { |
5333 | static int first = 1; | |
5334 | if (first) | |
5335 | { | |
1ebadc60 KG |
5336 | error ("Can't find default package `%s'. Check " |
5337 | "the CLASSPATH environment variable and the access to the " | |
5338 | "archives.", package_name); | |
e04a16fb AG |
5339 | java_error_count++; |
5340 | first = 0; | |
5341 | } | |
5342 | else | |
63a212ed PB |
5343 | parse_error_context (wfl, "Package `%s' not found in import", |
5344 | package_name); | |
e04a16fb AG |
5345 | current_jcf = saved_jcf; |
5346 | return; | |
5347 | } | |
e04a16fb AG |
5348 | current_jcf = saved_jcf; |
5349 | } | |
5350 | ||
5351 | /* Possibly find a type in the import on demands specified | |
5352 | types. Returns 1 if an error occured, 0 otherwise. Run throught the | |
5353 | entire list, to detected potential double definitions. */ | |
5354 | ||
5355 | static int | |
5356 | find_in_imports_on_demand (class_type) | |
5357 | tree class_type; | |
5358 | { | |
ab3a6dd6 | 5359 | tree node, import, node_to_use = NULL_TREE; |
e04a16fb | 5360 | int seen_once = -1; |
ab3a6dd6 | 5361 | tree cl = NULL_TREE; |
e04a16fb AG |
5362 | |
5363 | for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import)) | |
5364 | { | |
49f48c71 | 5365 | const char *id_name; |
e04a16fb AG |
5366 | obstack_grow (&temporary_obstack, |
5367 | IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))), | |
5368 | IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import)))); | |
63a212ed | 5369 | obstack_1grow (&temporary_obstack, '.'); |
e04a16fb AG |
5370 | obstack_grow0 (&temporary_obstack, |
5371 | IDENTIFIER_POINTER (TYPE_NAME (class_type)), | |
5372 | IDENTIFIER_LENGTH (TYPE_NAME (class_type))); | |
5373 | id_name = obstack_finish (&temporary_obstack); | |
5374 | ||
5375 | node = maybe_get_identifier (id_name); | |
5376 | if (node && IS_A_CLASSFILE_NAME (node)) | |
5377 | { | |
5378 | if (seen_once < 0) | |
5379 | { | |
5380 | cl = TREE_PURPOSE (import); | |
5381 | seen_once = 1; | |
5382 | node_to_use = node; | |
5383 | } | |
5384 | else | |
5385 | { | |
5386 | seen_once++; | |
5387 | parse_error_context | |
5388 | (import, "Type `%s' also potentially defined in package `%s'", | |
5389 | IDENTIFIER_POINTER (TYPE_NAME (class_type)), | |
5390 | IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import)))); | |
5391 | } | |
5392 | } | |
5393 | } | |
5394 | ||
5395 | if (seen_once == 1) | |
5396 | { | |
5397 | /* Setup lineno so that it refers to the line of the import (in | |
5398 | case we parse a class file and encounter errors */ | |
5399 | tree decl; | |
5400 | int saved_lineno = lineno; | |
5401 | lineno = EXPR_WFL_LINENO (cl); | |
63a212ed | 5402 | TYPE_NAME (class_type) = node_to_use; |
e04a16fb AG |
5403 | QUALIFIED_P (TYPE_NAME (class_type)) = 1; |
5404 | decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
5405 | /* If there is no DECL set for the class or if the class isn't | |
5406 | loaded and not seen in source yet, the load */ | |
5407 | if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl)) | |
5408 | && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl)))) | |
5409 | load_class (node_to_use, 0); | |
5410 | lineno = saved_lineno; | |
5411 | return check_pkg_class_access (TYPE_NAME (class_type), cl); | |
5412 | } | |
5413 | else | |
5414 | return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */ | |
5415 | } | |
5416 | ||
5e942c50 APB |
5417 | static tree |
5418 | resolve_package (pkg, next) | |
5419 | tree pkg, *next; | |
5420 | { | |
2c56429a | 5421 | tree current; |
5e942c50 | 5422 | tree type_name = NULL_TREE; |
49f48c71 | 5423 | const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg)); |
5e942c50 APB |
5424 | |
5425 | /* The trick is to determine when the package name stops and were | |
5426 | the name of something contained in the package starts. Then we | |
5427 | return a fully qualified name of what we want to get. */ | |
5428 | ||
5429 | /* Do a quick search on well known package names */ | |
5430 | if (!strncmp (name, "java.lang.reflect", 17)) | |
5431 | { | |
5432 | *next = | |
5433 | TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)))); | |
5434 | type_name = lookup_package_type (name, 17); | |
5435 | } | |
5436 | else if (!strncmp (name, "java.lang", 9)) | |
5437 | { | |
5438 | *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))); | |
5439 | type_name = lookup_package_type (name, 9); | |
5440 | } | |
5e942c50 | 5441 | |
2c56429a APB |
5442 | /* If we found something here, return */ |
5443 | if (type_name) | |
5444 | return type_name; | |
5445 | ||
5446 | *next = EXPR_WFL_QUALIFICATION (pkg); | |
5447 | ||
5448 | /* Try the current package. */ | |
5449 | if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package), | |
5450 | IDENTIFIER_LENGTH (ctxp->package))) | |
5451 | { | |
5452 | type_name = | |
5453 | lookup_package_type_and_set_next (name, | |
5454 | IDENTIFIER_LENGTH (ctxp->package), | |
5455 | next ); | |
5456 | if (type_name) | |
5457 | return type_name; | |
5458 | } | |
5459 | ||
5460 | /* Search in imported package */ | |
5461 | for (current = ctxp->import_list; current; current = TREE_CHAIN (current)) | |
5462 | { | |
5463 | tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current)); | |
5464 | int len = IDENTIFIER_LENGTH (current_pkg_name); | |
5465 | if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len)) | |
5466 | { | |
5467 | tree left, dummy; | |
5468 | ||
5469 | breakdown_qualified (&left, &dummy, current_pkg_name); | |
5470 | len = IDENTIFIER_LENGTH (left); | |
5471 | type_name = lookup_package_type_and_set_next (name, len, next); | |
5472 | if (type_name) | |
5473 | break; | |
5474 | } | |
5475 | } | |
5476 | ||
5477 | return type_name; | |
5478 | } | |
5479 | ||
5480 | static tree | |
5481 | lookup_package_type_and_set_next (name, len, next) | |
49f48c71 | 5482 | const char *name; |
2c56429a APB |
5483 | int len; |
5484 | tree *next; | |
5485 | { | |
49f48c71 | 5486 | const char *ptr; |
2c56429a APB |
5487 | tree type_name = lookup_package_type (name, len); |
5488 | ||
5489 | if (!type_name) | |
5490 | return NULL; | |
5491 | ||
5492 | ptr = IDENTIFIER_POINTER (type_name); | |
5493 | while (ptr && (ptr = strchr (ptr, '.'))) | |
5494 | { | |
5495 | *next = TREE_CHAIN (*next); | |
5496 | ptr++; | |
5497 | } | |
5e942c50 APB |
5498 | return type_name; |
5499 | } | |
5500 | ||
5501 | static tree | |
5502 | lookup_package_type (name, from) | |
49f48c71 | 5503 | const char *name; |
5e942c50 APB |
5504 | int from; |
5505 | { | |
5506 | char subname [128]; | |
49f48c71 | 5507 | const char *sub = &name[from+1]; |
5e942c50 APB |
5508 | while (*sub != '.' && *sub) |
5509 | sub++; | |
5510 | strncpy (subname, name, sub-name); | |
5511 | subname [sub-name] = '\0'; | |
5512 | return get_identifier (subname); | |
5513 | } | |
5514 | ||
e04a16fb AG |
5515 | /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no |
5516 | access violations were found, 1 otherwise. */ | |
5517 | ||
5518 | static int | |
5519 | check_pkg_class_access (class_name, cl) | |
5520 | tree class_name; | |
5521 | tree cl; | |
5522 | { | |
5523 | tree type; | |
e04a16fb AG |
5524 | |
5525 | if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name)) | |
5526 | return 0; | |
5527 | ||
5528 | if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name)))) | |
5529 | return 0; | |
5530 | ||
5531 | if (!CLASS_PUBLIC (TYPE_NAME (type))) | |
5532 | { | |
e28cd97b APB |
5533 | /* Access to a private class within the same package is |
5534 | allowed. */ | |
5535 | tree l, r; | |
5536 | breakdown_qualified (&l, &r, class_name); | |
5537 | if (l == ctxp->package) | |
5538 | return 0; | |
5539 | ||
e04a16fb AG |
5540 | parse_error_context |
5541 | (cl, "Can't access %s `%s'. Only public classes and interfaces in " | |
5542 | "other packages can be accessed", | |
5543 | (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"), | |
5544 | IDENTIFIER_POINTER (class_name)); | |
5545 | return 1; | |
5546 | } | |
5547 | return 0; | |
5548 | } | |
5549 | ||
5550 | /* Local variable declaration. */ | |
5551 | ||
5552 | static void | |
5553 | declare_local_variables (modifier, type, vlist) | |
5554 | int modifier; | |
5555 | tree type; | |
5556 | tree vlist; | |
5557 | { | |
c583dd46 APB |
5558 | tree decl, current, saved_type; |
5559 | tree type_wfl = NULL_TREE; | |
e04a16fb AG |
5560 | int must_chain = 0; |
5561 | ||
2aa11e97 | 5562 | /* Push a new block if statements were seen between the last time we |
e04a16fb | 5563 | pushed a block and now. Keep a cound of block to close */ |
f099f336 | 5564 | if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl))) |
e04a16fb | 5565 | { |
f099f336 | 5566 | tree body = GET_CURRENT_BLOCK (current_function_decl); |
e04a16fb | 5567 | tree b = enter_block (); |
f099f336 | 5568 | BLOCK_EXPR_ORIGIN (b) = body; |
e04a16fb AG |
5569 | } |
5570 | ||
5571 | if (modifier) | |
5572 | { | |
5573 | int i; | |
5574 | for (i = 0; i <= 10; i++) if (1 << i & modifier) break; | |
c877974e APB |
5575 | if (modifier == ACC_FINAL) |
5576 | { | |
5577 | if (flag_static_local_jdk1_1) | |
5578 | parse_warning_context (ctxp->modifier_ctx [i], | |
5579 | "Unsupported JDK1.1 `final' local variable " | |
5580 | "(treated as non final)"); | |
5581 | } | |
5582 | else | |
5583 | { | |
5584 | parse_error_context | |
5585 | (ctxp->modifier_ctx [i], | |
5586 | "Only `final' is allowed as a local variables modifier"); | |
5587 | return; | |
5588 | } | |
e04a16fb AG |
5589 | } |
5590 | ||
c583dd46 APB |
5591 | /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will |
5592 | hold the TYPE value if a new incomplete has to be created (as | |
5593 | opposed to being found already existing and reused). */ | |
5594 | SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain); | |
5595 | ||
5596 | /* If TYPE is fully resolved and we don't have a reference, make one */ | |
1886c9d8 | 5597 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
c583dd46 APB |
5598 | |
5599 | /* Go through all the declared variables */ | |
5600 | for (current = vlist, saved_type = type; current; | |
5601 | current = TREE_CHAIN (current), type = saved_type) | |
e04a16fb | 5602 | { |
c877974e | 5603 | tree other, real_type; |
e04a16fb AG |
5604 | tree wfl = TREE_PURPOSE (current); |
5605 | tree name = EXPR_WFL_NODE (wfl); | |
5606 | tree init = TREE_VALUE (current); | |
e04a16fb | 5607 | |
c583dd46 APB |
5608 | /* Process NAME, as it may specify extra dimension(s) for it */ |
5609 | type = build_array_from_name (type, type_wfl, name, &name); | |
5610 | ||
5611 | /* Variable redefinition check */ | |
5612 | if ((other = lookup_name_in_blocks (name))) | |
5613 | { | |
5614 | variable_redefinition_error (wfl, name, TREE_TYPE (other), | |
5615 | DECL_SOURCE_LINE (other)); | |
5616 | continue; | |
5617 | } | |
5618 | ||
5619 | /* Type adjustment. We may have just readjusted TYPE because | |
5620 | the variable specified more dimensions. Make sure we have | |
5621 | a reference if we can and don't have one already. */ | |
1886c9d8 | 5622 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
c877974e APB |
5623 | |
5624 | real_type = GET_REAL_TYPE (type); | |
c583dd46 APB |
5625 | /* Never layout this decl. This will be done when its scope |
5626 | will be entered */ | |
c877974e | 5627 | decl = build_decl (VAR_DECL, name, real_type); |
c583dd46 APB |
5628 | BLOCK_CHAIN_DECL (decl); |
5629 | ||
d4370213 APB |
5630 | /* If doing xreferencing, replace the line number with the WFL |
5631 | compound value */ | |
5632 | if (flag_emit_xref) | |
5633 | DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl); | |
5634 | ||
e04a16fb AG |
5635 | /* Don't try to use an INIT statement when an error was found */ |
5636 | if (init && java_error_count) | |
5637 | init = NULL_TREE; | |
c583dd46 APB |
5638 | |
5639 | /* Add the initialization function to the current function's code */ | |
5640 | if (init) | |
e04a16fb | 5641 | { |
c583dd46 APB |
5642 | /* Name might have been readjusted */ |
5643 | EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name; | |
5644 | MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1; | |
5645 | java_method_add_stmt (current_function_decl, | |
5646 | build_debugable_stmt (EXPR_WFL_LINECOL (init), | |
5647 | init)); | |
5648 | } | |
5649 | ||
5650 | /* Setup dependency the type of the decl */ | |
5651 | if (must_chain) | |
5652 | { | |
5653 | jdep *dep; | |
5654 | register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type); | |
5655 | dep = CLASSD_LAST (ctxp->classd_list); | |
5656 | JDEP_GET_PATCH (dep) = &TREE_TYPE (decl); | |
e04a16fb AG |
5657 | } |
5658 | } | |
5659 | SOURCE_FRONTEND_DEBUG (("Defined locals")); | |
5660 | } | |
5661 | ||
5662 | /* Called during parsing. Build decls from argument list. */ | |
5663 | ||
5664 | static void | |
5665 | source_start_java_method (fndecl) | |
5666 | tree fndecl; | |
5667 | { | |
5668 | tree tem; | |
5669 | tree parm_decl; | |
5670 | int i; | |
5671 | ||
79d13333 APB |
5672 | if (!fndecl) |
5673 | return; | |
5674 | ||
e04a16fb AG |
5675 | current_function_decl = fndecl; |
5676 | ||
5677 | /* New scope for the function */ | |
5678 | enter_block (); | |
5679 | for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0; | |
de4c7b02 | 5680 | tem != end_params_node; tem = TREE_CHAIN (tem), i++) |
e04a16fb AG |
5681 | { |
5682 | tree type = TREE_VALUE (tem); | |
5683 | tree name = TREE_PURPOSE (tem); | |
5684 | ||
23a79c61 APB |
5685 | /* If type is incomplete. Create an incomplete decl and ask for |
5686 | the decl to be patched later */ | |
e04a16fb AG |
5687 | if (INCOMPLETE_TYPE_P (type)) |
5688 | { | |
5689 | jdep *jdep; | |
c877974e APB |
5690 | tree real_type = GET_REAL_TYPE (type); |
5691 | parm_decl = build_decl (PARM_DECL, name, real_type); | |
23a79c61 | 5692 | type = obtain_incomplete_type (type); |
e04a16fb AG |
5693 | register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type); |
5694 | jdep = CLASSD_LAST (ctxp->classd_list); | |
5695 | JDEP_MISC (jdep) = name; | |
5696 | JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl); | |
5697 | } | |
5698 | else | |
5699 | parm_decl = build_decl (PARM_DECL, name, type); | |
5700 | ||
5701 | BLOCK_CHAIN_DECL (parm_decl); | |
5702 | } | |
5703 | tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)); | |
5704 | BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) = | |
5705 | nreverse (tem); | |
5706 | DECL_ARG_SLOT_COUNT (current_function_decl) = i; | |
5707 | } | |
5708 | ||
22eed1e6 APB |
5709 | /* Called during parsing. Creates an artificial method declaration. */ |
5710 | ||
5711 | static tree | |
5712 | create_artificial_method (class, flags, type, name, args) | |
5713 | tree class; | |
5714 | int flags; | |
5715 | tree type, name, args; | |
5716 | { | |
5717 | int saved_lineno = lineno; | |
5718 | tree mdecl; | |
5719 | ||
5720 | lineno = 0; | |
5721 | mdecl = make_node (FUNCTION_TYPE); | |
5722 | TREE_TYPE (mdecl) = type; | |
5723 | TYPE_ARG_TYPES (mdecl) = args; | |
5724 | mdecl = add_method (class, flags, name, build_java_signature (mdecl)); | |
5725 | lineno = saved_lineno; | |
5726 | DECL_ARTIFICIAL (mdecl) = 1; | |
5727 | return mdecl; | |
5728 | } | |
5729 | ||
5730 | /* Starts the body if an artifical method. */ | |
5731 | ||
5732 | static void | |
5733 | start_artificial_method_body (mdecl) | |
5734 | tree mdecl; | |
5735 | { | |
5736 | DECL_SOURCE_LINE (mdecl) = 1; | |
5737 | DECL_SOURCE_LINE_MERGE (mdecl, 1); | |
5738 | source_start_java_method (mdecl); | |
5739 | enter_block (); | |
5740 | } | |
5741 | ||
5742 | static void | |
5743 | end_artificial_method_body (mdecl) | |
5744 | tree mdecl; | |
5745 | { | |
5746 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block (); | |
5747 | exit_block (); | |
5748 | } | |
5749 | ||
e04a16fb AG |
5750 | /* Called during expansion. Push decls formerly built from argument |
5751 | list so they're usable during expansion. */ | |
5752 | ||
5753 | static void | |
5754 | expand_start_java_method (fndecl) | |
5755 | tree fndecl; | |
5756 | { | |
5757 | tree tem, *ptr; | |
e04a16fb | 5758 | |
e04a16fb AG |
5759 | current_function_decl = fndecl; |
5760 | ||
5761 | announce_function (fndecl); | |
5762 | pushlevel (1); /* Push parameters */ | |
5763 | ptr = &DECL_ARGUMENTS (fndecl); | |
5764 | tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)); | |
5765 | while (tem) | |
5766 | { | |
5767 | tree next = TREE_CHAIN (tem); | |
b67d701b | 5768 | tree type = TREE_TYPE (tem); |
e438e1b7 JJ |
5769 | if (PROMOTE_PROTOTYPES |
5770 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node) | |
b67d701b PB |
5771 | && INTEGRAL_TYPE_P (type)) |
5772 | type = integer_type_node; | |
b67d701b | 5773 | DECL_ARG_TYPE (tem) = type; |
e04a16fb AG |
5774 | layout_decl (tem, 0); |
5775 | pushdecl (tem); | |
e04a16fb AG |
5776 | *ptr = tem; |
5777 | ptr = &TREE_CHAIN (tem); | |
5778 | tem = next; | |
5779 | } | |
5780 | *ptr = NULL_TREE; | |
5781 | pushdecl_force_head (DECL_ARGUMENTS (fndecl)); | |
5782 | lineno = DECL_SOURCE_LINE_FIRST (fndecl); | |
e04a16fb AG |
5783 | } |
5784 | ||
5785 | /* Terminate a function and expand its body. */ | |
5786 | ||
5787 | static void | |
5788 | source_end_java_method () | |
5789 | { | |
5790 | tree fndecl = current_function_decl; | |
138657ec | 5791 | int flag_asynchronous_exceptions = asynchronous_exceptions; |
e04a16fb | 5792 | |
79d13333 APB |
5793 | if (!fndecl) |
5794 | return; | |
5795 | ||
e04a16fb AG |
5796 | java_parser_context_save_global (); |
5797 | lineno = ctxp->last_ccb_indent1; | |
5798 | ||
b67d701b PB |
5799 | /* Set EH language codes */ |
5800 | java_set_exception_lang_code (); | |
5801 | ||
5423609c APB |
5802 | /* Turn function bodies with only a NOP expr null, so they don't get |
5803 | generated at all and we won't get warnings when using the -W | |
5804 | -Wall flags. */ | |
5805 | if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node) | |
5806 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE; | |
5807 | ||
e04a16fb AG |
5808 | /* Generate function's code */ |
5809 | if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) | |
e8fc7396 APB |
5810 | && ! flag_emit_class_files |
5811 | && ! flag_emit_xref) | |
e04a16fb AG |
5812 | expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))); |
5813 | ||
5814 | /* pop out of its parameters */ | |
5815 | pushdecl_force_head (DECL_ARGUMENTS (fndecl)); | |
5816 | poplevel (1, 0, 1); | |
5817 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
5818 | ||
5819 | /* Generate rtl for function exit. */ | |
e8fc7396 | 5820 | if (! flag_emit_class_files && ! flag_emit_xref) |
e04a16fb AG |
5821 | { |
5822 | lineno = DECL_SOURCE_LINE_LAST (fndecl); | |
b67d701b PB |
5823 | /* Emit catch-finally clauses */ |
5824 | emit_handlers (); | |
e04a16fb AG |
5825 | expand_function_end (input_filename, lineno, 0); |
5826 | ||
138657ec AH |
5827 | /* FIXME: If the current method contains any exception handlers, |
5828 | force asynchronous_exceptions: this is necessary because signal | |
5829 | handlers in libjava may throw exceptions. This is far from being | |
5830 | a perfect solution, but it's better than doing nothing at all.*/ | |
5831 | if (catch_clauses) | |
5832 | asynchronous_exceptions = 1; | |
5833 | ||
e04a16fb AG |
5834 | /* Run the optimizers and output assembler code for this function. */ |
5835 | rest_of_compilation (fndecl); | |
5836 | } | |
5837 | ||
5838 | current_function_decl = NULL_TREE; | |
5839 | /* permanent_allocation (1); */ | |
5840 | java_parser_context_restore_global (); | |
138657ec | 5841 | asynchronous_exceptions = flag_asynchronous_exceptions; |
e04a16fb AG |
5842 | } |
5843 | ||
5844 | /* Record EXPR in the current function block. Complements compound | |
5845 | expression second operand if necessary. */ | |
5846 | ||
5847 | tree | |
5848 | java_method_add_stmt (fndecl, expr) | |
5849 | tree fndecl, expr; | |
5850 | { | |
b771925e APB |
5851 | if (!GET_CURRENT_BLOCK (fndecl)) |
5852 | return NULL_TREE; | |
f099f336 | 5853 | return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr); |
b67d701b | 5854 | } |
e04a16fb | 5855 | |
b67d701b PB |
5856 | static tree |
5857 | add_stmt_to_block (b, type, stmt) | |
5858 | tree b, type, stmt; | |
5859 | { | |
5860 | tree body = BLOCK_EXPR_BODY (b), c; | |
5861 | ||
e04a16fb AG |
5862 | if (java_error_count) |
5863 | return body; | |
b67d701b PB |
5864 | |
5865 | if ((c = add_stmt_to_compound (body, type, stmt)) == body) | |
e04a16fb AG |
5866 | return body; |
5867 | ||
b67d701b PB |
5868 | BLOCK_EXPR_BODY (b) = c; |
5869 | TREE_SIDE_EFFECTS (c) = 1; | |
5870 | return c; | |
e04a16fb AG |
5871 | } |
5872 | ||
5873 | /* Add STMT to EXISTING if possible, otherwise create a new | |
5874 | COMPOUND_EXPR and add STMT to it. */ | |
5875 | ||
5876 | static tree | |
5877 | add_stmt_to_compound (existing, type, stmt) | |
5878 | tree existing, type, stmt; | |
5879 | { | |
15fdcfe9 PB |
5880 | if (existing) |
5881 | return build (COMPOUND_EXPR, type, existing, stmt); | |
e04a16fb | 5882 | else |
15fdcfe9 | 5883 | return stmt; |
e04a16fb AG |
5884 | } |
5885 | ||
5886 | /* Hold THIS for the scope of the current public method decl. */ | |
5887 | static tree current_this; | |
5888 | ||
1886c9d8 APB |
5889 | void java_layout_seen_class_methods () |
5890 | { | |
5891 | tree previous_list = all_class_list; | |
5892 | tree end = NULL_TREE; | |
5893 | tree current; | |
5894 | ||
5895 | while (1) | |
5896 | { | |
5897 | for (current = previous_list; | |
5898 | current != end; current = TREE_CHAIN (current)) | |
5899 | layout_class_methods (TREE_TYPE (TREE_VALUE (current))); | |
5900 | ||
5901 | if (previous_list != all_class_list) | |
5902 | { | |
5903 | end = previous_list; | |
5904 | previous_list = all_class_list; | |
5905 | } | |
5906 | else | |
5907 | break; | |
5908 | } | |
5909 | } | |
5910 | ||
23a79c61 APB |
5911 | /* Layout the methods of all classes loaded in one way on an |
5912 | other. Check methods of source parsed classes. Then reorder the | |
5913 | fields and layout the classes or the type of all source parsed | |
5914 | classes */ | |
e04a16fb AG |
5915 | |
5916 | void | |
5917 | java_layout_classes () | |
5918 | { | |
5919 | tree current; | |
bc3ca41b | 5920 | int save_error_count = java_error_count; |
5e942c50 | 5921 | |
23a79c61 | 5922 | /* Layout the methods of all classes seen so far */ |
1886c9d8 | 5923 | java_layout_seen_class_methods (); |
23a79c61 APB |
5924 | java_parse_abort_on_error (); |
5925 | all_class_list = NULL_TREE; | |
5926 | ||
5927 | /* Then check the methods of all parsed classes */ | |
5928 | for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current)) | |
5929 | if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current)))) | |
5930 | CHECK_METHODS (TREE_VALUE (current)); | |
5931 | java_parse_abort_on_error (); | |
5932 | ||
5e942c50 | 5933 | for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current)) |
e04a16fb | 5934 | { |
5e942c50 | 5935 | current_class = TREE_TYPE (TREE_VALUE (current)); |
22eed1e6 | 5936 | |
c877974e APB |
5937 | /* Reverse the fields, but leave the dummy field in front. |
5938 | Fields are already ordered for Object and Class */ | |
5939 | if (TYPE_FIELDS (current_class) && current_class != object_type_node | |
5940 | && current_class != class_type_node) | |
5941 | { | |
23a79c61 APB |
5942 | /* If the dummy field is there, reverse the right fields and |
5943 | just layout the type for proper fields offset */ | |
c877974e APB |
5944 | if (!DECL_NAME (TYPE_FIELDS (current_class))) |
5945 | { | |
5946 | tree fields = TYPE_FIELDS (current_class); | |
5947 | TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields)); | |
5948 | TYPE_SIZE (current_class) = NULL_TREE; | |
5949 | layout_type (current_class); | |
5950 | } | |
23a79c61 APB |
5951 | /* We don't have a dummy field, we need to layout the class, |
5952 | after having reversed the fields */ | |
c877974e APB |
5953 | else |
5954 | { | |
5955 | TYPE_FIELDS (current_class) = | |
5956 | nreverse (TYPE_FIELDS (current_class)); | |
5957 | TYPE_SIZE (current_class) = NULL_TREE; | |
5958 | layout_class (current_class); | |
5959 | } | |
5960 | } | |
23a79c61 APB |
5961 | else |
5962 | layout_class (current_class); | |
5e942c50 | 5963 | |
c877974e APB |
5964 | /* From now on, the class is considered completely loaded */ |
5965 | CLASS_LOADED_P (current_class) = 1; | |
5966 | ||
5e942c50 APB |
5967 | /* Error reported by the caller */ |
5968 | if (java_error_count) | |
5969 | return; | |
e04a16fb | 5970 | } |
23a79c61 APB |
5971 | |
5972 | /* We might have reloaded classes durign the process of laying out | |
5973 | classes for code generation. We must layout the methods of those | |
5974 | late additions, as constructor checks might use them */ | |
1886c9d8 | 5975 | java_layout_seen_class_methods (); |
23a79c61 | 5976 | java_parse_abort_on_error (); |
e04a16fb AG |
5977 | } |
5978 | ||
5979 | /* Expand all methods in all registered classes. */ | |
5980 | ||
49f48c71 | 5981 | static void |
e04a16fb AG |
5982 | java_complete_expand_methods () |
5983 | { | |
5984 | tree current; | |
ce6e9147 APB |
5985 | |
5986 | do_not_fold = flag_emit_xref; | |
e04a16fb AG |
5987 | |
5988 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) | |
5989 | { | |
7f1d4866 | 5990 | int is_interface; |
e04a16fb AG |
5991 | tree class_type = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current)); |
5992 | tree decl; | |
e04a16fb AG |
5993 | |
5994 | current_class = TREE_TYPE (current); | |
7f1d4866 | 5995 | is_interface = CLASS_INTERFACE (TYPE_NAME (current_class)); |
e04a16fb AG |
5996 | |
5997 | /* Initialize a new constant pool */ | |
5998 | init_outgoing_cpool (); | |
5999 | ||
7525cc04 APB |
6000 | /* We want <clinit> (if any) to be processed first. */ |
6001 | decl = tree_last (TYPE_METHODS (class_type)); | |
7f1d4866 | 6002 | if (IS_CLINIT (decl)) |
7525cc04 | 6003 | { |
cd9643f7 PB |
6004 | tree fbody = DECL_FUNCTION_BODY (decl); |
6005 | tree list; | |
6006 | if (fbody != NULL_TREE) | |
6007 | { | |
6008 | /* First check if we can ignore empty <clinit> */ | |
6009 | tree block_body = BLOCK_EXPR_BODY (fbody); | |
6010 | ||
6011 | current_this = NULL_TREE; | |
6012 | current_function_decl = decl; | |
6013 | if (block_body != NULL_TREE) | |
6014 | { | |
6015 | /* Prevent the use of `this' inside <clinit> */ | |
6016 | ctxp->explicit_constructor_p = 1; | |
6017 | ||
6018 | block_body = java_complete_tree (block_body); | |
6019 | ctxp->explicit_constructor_p = 0; | |
6020 | BLOCK_EXPR_BODY (fbody) = block_body; | |
6021 | if (block_body != NULL_TREE | |
6022 | && TREE_CODE (block_body) == BLOCK | |
6023 | && BLOCK_EXPR_BODY (block_body) == empty_stmt_node) | |
6024 | decl = NULL_TREE; | |
6025 | } | |
6026 | } | |
6027 | list = nreverse (TREE_CHAIN (nreverse (TYPE_METHODS (class_type)))); | |
6028 | if (decl != NULL_TREE) | |
6029 | { | |
6030 | TREE_CHAIN (decl) = list; | |
6031 | TYPE_METHODS (class_type) = decl; | |
6032 | } | |
6033 | else | |
6034 | TYPE_METHODS (class_type) = list; | |
7525cc04 | 6035 | } |
7f1d4866 APB |
6036 | |
6037 | for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl)) | |
6038 | { | |
7f1d4866 APB |
6039 | current_function_decl = decl; |
6040 | /* Don't generate debug info on line zero when expanding a | |
6041 | generated constructor. */ | |
6042 | if (DECL_CONSTRUCTOR_P (decl) && !DECL_FUNCTION_BODY (decl)) | |
6043 | { | |
6044 | /* If we found errors, it's too dangerous to try to | |
6045 | generate and expand a constructor */ | |
6046 | if (!java_error_count) | |
6047 | { | |
6048 | restore_line_number_status (1); | |
6049 | java_complete_expand_method (decl); | |
6050 | restore_line_number_status (0); | |
e04a16fb | 6051 | } |
7f1d4866 APB |
6052 | } |
6053 | else if (METHOD_ABSTRACT (decl) || METHOD_NATIVE (decl)) | |
6054 | continue; | |
6055 | else | |
6056 | java_complete_expand_method (decl); | |
6057 | } | |
e04a16fb | 6058 | |
22eed1e6 APB |
6059 | /* Now verify constructor circularity (stop after the first one |
6060 | we find) */ | |
7f1d4866 | 6061 | if (!is_interface) |
22eed1e6 APB |
6062 | for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl)) |
6063 | if (DECL_CONSTRUCTOR_P (decl) && | |
6064 | verify_constructor_circularity (decl, decl)) | |
6065 | break; | |
6066 | ||
e04a16fb AG |
6067 | /* Make the class data, register it and run the rest of decl |
6068 | compilation on it */ | |
63a212ed PB |
6069 | if (!java_error_count) |
6070 | { | |
6071 | if (flag_emit_class_files) | |
6072 | write_classfile (current_class); | |
f099f336 APB |
6073 | if (flag_emit_xref) |
6074 | expand_xref (current_class); | |
aabd7048 | 6075 | else if (! flag_syntax_only) |
d593dd8c | 6076 | finish_class (); |
63a212ed | 6077 | } |
e04a16fb AG |
6078 | } |
6079 | } | |
6080 | ||
b9f7e36c APB |
6081 | /* Hold a list of catch clauses list. The first element of this list is |
6082 | the list of the catch clauses of the currently analysed try block. */ | |
6083 | static tree currently_caught_type_list; | |
6084 | ||
e04a16fb AG |
6085 | /* Complete and expand a method. */ |
6086 | ||
6087 | static void | |
6088 | java_complete_expand_method (mdecl) | |
6089 | tree mdecl; | |
6090 | { | |
22eed1e6 APB |
6091 | /* Fix constructors before expanding them */ |
6092 | if (DECL_CONSTRUCTOR_P (mdecl)) | |
6093 | fix_constructors (mdecl); | |
e04a16fb | 6094 | |
22eed1e6 | 6095 | /* Expand functions that have a body */ |
e04a16fb AG |
6096 | if (DECL_FUNCTION_BODY (mdecl)) |
6097 | { | |
9bbc7d9f PB |
6098 | tree fbody = DECL_FUNCTION_BODY (mdecl); |
6099 | tree block_body = BLOCK_EXPR_BODY (fbody); | |
cd531a2e | 6100 | tree exception_copy = NULL_TREE; |
e04a16fb | 6101 | expand_start_java_method (mdecl); |
939d7216 | 6102 | build_result_decl (mdecl); |
e04a16fb AG |
6103 | |
6104 | current_this | |
6105 | = (!METHOD_STATIC (mdecl) ? | |
6106 | BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE); | |
6107 | ||
ce6e9147 APB |
6108 | /* Purge the `throws' list of unchecked exceptions. If we're |
6109 | doing xref, save a copy of the list and re-install it | |
6110 | later. */ | |
6111 | if (flag_emit_xref) | |
6112 | exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl)); | |
6113 | ||
b9f7e36c APB |
6114 | purge_unchecked_exceptions (mdecl); |
6115 | ||
6116 | /* Install exceptions thrown with `throws' */ | |
6117 | PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl)); | |
6118 | ||
9bbc7d9f | 6119 | if (block_body != NULL_TREE) |
bc3ca41b PB |
6120 | { |
6121 | block_body = java_complete_tree (block_body); | |
ce6e9147 APB |
6122 | if (!flag_emit_xref) |
6123 | check_for_initialization (block_body); | |
f099f336 | 6124 | ctxp->explicit_constructor_p = 0; |
bc3ca41b | 6125 | } |
9bbc7d9f | 6126 | BLOCK_EXPR_BODY (fbody) = block_body; |
5e942c50 | 6127 | |
9bbc7d9f | 6128 | if ((block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body)) |
ce6e9147 APB |
6129 | && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE |
6130 | && !flag_emit_xref) | |
82371d41 | 6131 | missing_return_error (current_function_decl); |
7525cc04 | 6132 | |
939d7216 PB |
6133 | complete_start_java_method (mdecl); |
6134 | ||
e04a16fb AG |
6135 | /* Don't go any further if we've found error(s) during the |
6136 | expansion */ | |
6137 | if (!java_error_count) | |
6138 | source_end_java_method (); | |
22eed1e6 APB |
6139 | else |
6140 | { | |
6141 | pushdecl_force_head (DECL_ARGUMENTS (mdecl)); | |
6142 | poplevel (1, 0, 1); | |
6143 | } | |
b9f7e36c APB |
6144 | |
6145 | /* Pop the exceptions and sanity check */ | |
6146 | POP_EXCEPTIONS(); | |
6147 | if (currently_caught_type_list) | |
6148 | fatal ("Exception list non empty - java_complete_expand_method"); | |
ce6e9147 APB |
6149 | |
6150 | if (flag_emit_xref) | |
6151 | DECL_FUNCTION_THROWS (mdecl) = exception_copy; | |
e04a16fb AG |
6152 | } |
6153 | } | |
6154 | ||
22eed1e6 APB |
6155 | /* Craft a body for default constructor. Patch existing constructor |
6156 | bodies with call to super() and field initialization statements if | |
6157 | necessary. */ | |
6158 | ||
6159 | static void | |
6160 | fix_constructors (mdecl) | |
6161 | tree mdecl; | |
6162 | { | |
6163 | tree body = DECL_FUNCTION_BODY (mdecl); | |
22eed1e6 | 6164 | |
22eed1e6 APB |
6165 | if (!body) |
6166 | { | |
89e09b9a PB |
6167 | /* The constructor body must be crafted by hand. It's the |
6168 | constructor we defined when we realize we didn't have the | |
6169 | CLASSNAME() constructor */ | |
6170 | ||
22eed1e6 APB |
6171 | tree compound; |
6172 | ||
6173 | /* It is an error for the compiler to generate a default | |
6174 | constructor if the superclass doesn't have a constructor that | |
6175 | takes no argument */ | |
6176 | if (verify_constructor_super ()) | |
6177 | { | |
6178 | tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (current_class)); | |
49f48c71 | 6179 | const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl)); |
22eed1e6 APB |
6180 | parse_error_context (lookup_cl (TYPE_NAME (current_class)), |
6181 | "No constructor matching `%s()' found in " | |
6182 | "class `%s'", n, n); | |
6183 | } | |
6184 | ||
6185 | start_artificial_method_body (mdecl); | |
6186 | ||
6187 | /* We don't generate a super constructor invocation if we're | |
6188 | compiling java.lang.Object. build_super_invocation takes care | |
6189 | of that. */ | |
6190 | compound = java_method_add_stmt (mdecl, build_super_invocation ()); | |
22eed1e6 APB |
6191 | |
6192 | end_artificial_method_body (mdecl); | |
6193 | } | |
6194 | /* Search for an explicit constructor invocation */ | |
6195 | else | |
6196 | { | |
6197 | int found = 0; | |
6198 | tree main_block = BLOCK_EXPR_BODY (body); | |
6199 | tree compound = NULL_TREE; | |
6200 | ||
6201 | while (body) | |
6202 | switch (TREE_CODE (body)) | |
6203 | { | |
6204 | case CALL_EXPR: | |
6205 | found = CALL_EXPLICIT_CONSTRUCTOR_P (body); | |
6206 | body = NULL_TREE; | |
6207 | break; | |
6208 | case COMPOUND_EXPR: | |
6209 | case EXPR_WITH_FILE_LOCATION: | |
6210 | body = TREE_OPERAND (body, 0); | |
6211 | break; | |
6212 | case BLOCK: | |
6213 | body = BLOCK_EXPR_BODY (body); | |
6214 | break; | |
6215 | default: | |
6216 | found = 0; | |
6217 | body = NULL_TREE; | |
6218 | } | |
6219 | /* The constructor is missing an invocation of super() */ | |
6220 | if (!found) | |
6221 | compound = add_stmt_to_compound (compound, NULL_TREE, | |
6222 | build_super_invocation ()); | |
6223 | ||
22eed1e6 APB |
6224 | /* Fix the constructor main block if we're adding extra stmts */ |
6225 | if (compound) | |
6226 | { | |
6227 | compound = add_stmt_to_compound (compound, NULL_TREE, | |
6228 | BLOCK_EXPR_BODY (main_block)); | |
6229 | BLOCK_EXPR_BODY (main_block) = compound; | |
6230 | } | |
6231 | } | |
6232 | } | |
6233 | ||
6234 | /* Browse constructors in the super class, searching for a constructor | |
6235 | that doesn't take any argument. Return 0 if one is found, 1 | |
6236 | otherwise. */ | |
6237 | ||
6238 | static int | |
6239 | verify_constructor_super () | |
6240 | { | |
6241 | tree class = CLASSTYPE_SUPER (current_class); | |
6242 | if (!class) | |
6243 | return 0; | |
6244 | ||
6245 | if (class) | |
6246 | { | |
6247 | tree mdecl; | |
6248 | for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl)) | |
6249 | { | |
6250 | if (DECL_CONSTRUCTOR_P (mdecl) | |
d77613be APB |
6251 | && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (mdecl))) |
6252 | == end_params_node) | |
22eed1e6 APB |
6253 | return 0; |
6254 | } | |
6255 | } | |
6256 | return 1; | |
6257 | } | |
6258 | ||
e04a16fb AG |
6259 | /* Expand finals. */ |
6260 | ||
49f48c71 | 6261 | static void |
e04a16fb AG |
6262 | java_expand_finals () |
6263 | { | |
6264 | } | |
6265 | ||
22eed1e6 | 6266 | /* Generate code for all context remembered for code generation. */ |
b351b287 APB |
6267 | |
6268 | void | |
6269 | java_expand_classes () | |
6270 | { | |
5423609c | 6271 | int save_error_count = 0; |
23a79c61 APB |
6272 | java_parse_abort_on_error (); |
6273 | if (!(ctxp = ctxp_for_generation)) | |
5e942c50 APB |
6274 | return; |
6275 | java_layout_classes (); | |
6276 | java_parse_abort_on_error (); | |
6277 | ||
b351b287 APB |
6278 | for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next) |
6279 | { | |
6280 | ctxp = ctxp_for_generation; | |
6281 | lang_init_source (2); /* Error msgs have method prototypes */ | |
6282 | java_complete_expand_methods (); /* Complete and expand method bodies */ | |
6283 | java_parse_abort_on_error (); | |
6284 | java_expand_finals (); /* Expand and check the finals */ | |
6285 | java_parse_abort_on_error (); | |
6286 | java_check_final (); /* Check unitialized final */ | |
6287 | java_parse_abort_on_error (); | |
6288 | } | |
b351b287 APB |
6289 | } |
6290 | ||
e04a16fb AG |
6291 | /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to |
6292 | a tree list node containing RIGHT. Fore coming RIGHTs will be | |
6293 | chained to this hook. LOCATION contains the location of the | |
6294 | separating `.' operator. */ | |
6295 | ||
6296 | static tree | |
6297 | make_qualified_primary (primary, right, location) | |
6298 | tree primary, right; | |
6299 | int location; | |
6300 | { | |
6301 | tree wfl; | |
6302 | ||
6303 | /* We want to process THIS . xxx symbolicaly, to keep it consistent | |
6304 | with the way we're processing SUPER. A THIS from a primary as a | |
6305 | different form than a SUPER. Turn THIS into something symbolic */ | |
b67d701b | 6306 | if (TREE_CODE (primary) == THIS_EXPR) |
e04a16fb | 6307 | { |
9ee9b555 | 6308 | wfl = build_wfl_node (this_identifier_node); |
e04a16fb AG |
6309 | EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary); |
6310 | wfl = make_qualified_name (wfl, right, location); | |
6311 | PRIMARY_P (wfl) = 1; | |
6312 | return wfl; | |
6313 | } | |
6314 | /* Other non WFL node are wrapped around a WFL */ | |
6315 | else if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION) | |
6316 | { | |
6317 | wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0); | |
6318 | EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary); | |
6319 | EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (primary, NULL_TREE); | |
6320 | } | |
6321 | else | |
6322 | { | |
6323 | wfl = primary; | |
6324 | if (!EXPR_WFL_QUALIFICATION (primary)) | |
6325 | EXPR_WFL_QUALIFICATION (primary) = | |
6326 | build_tree_list (primary, NULL_TREE); | |
6327 | } | |
6328 | ||
6329 | EXPR_WFL_LINECOL (right) = location; | |
6330 | chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE)); | |
6331 | PRIMARY_P (wfl) = 1; | |
6332 | return wfl; | |
6333 | } | |
6334 | ||
6335 | /* Simple merge of two name separated by a `.' */ | |
6336 | ||
6337 | static tree | |
6338 | merge_qualified_name (left, right) | |
6339 | tree left, right; | |
6340 | { | |
6341 | tree node; | |
6342 | obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left), | |
6343 | IDENTIFIER_LENGTH (left)); | |
6344 | obstack_1grow (&temporary_obstack, '.'); | |
6345 | obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right), | |
6346 | IDENTIFIER_LENGTH (right)); | |
6347 | node = get_identifier (obstack_base (&temporary_obstack)); | |
6348 | obstack_free (&temporary_obstack, obstack_base (&temporary_obstack)); | |
6349 | QUALIFIED_P (node) = 1; | |
6350 | return node; | |
6351 | } | |
6352 | ||
6353 | /* Merge the two parts of a qualified name into LEFT. Set the | |
6354 | location information of the resulting node to LOCATION, usually | |
6355 | inherited from the location information of the `.' operator. */ | |
6356 | ||
6357 | static tree | |
6358 | make_qualified_name (left, right, location) | |
6359 | tree left, right; | |
6360 | int location; | |
6361 | { | |
bc3ca41b PB |
6362 | #ifdef USE_COMPONENT_REF |
6363 | tree node = build (COMPONENT_REF, NULL_TREE, left, right); | |
6364 | EXPR_WFL_LINECOL (node) = location; | |
6365 | return node; | |
6366 | #else | |
e04a16fb AG |
6367 | tree left_id = EXPR_WFL_NODE (left); |
6368 | tree right_id = EXPR_WFL_NODE (right); | |
6369 | tree wfl, merge; | |
6370 | ||
6371 | merge = merge_qualified_name (left_id, right_id); | |
6372 | ||
6373 | /* Left wasn't qualified and is now qualified */ | |
6374 | if (!QUALIFIED_P (left_id)) | |
6375 | { | |
6376 | tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0); | |
6377 | EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left); | |
6378 | EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE); | |
6379 | } | |
6380 | ||
6381 | wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0); | |
6382 | EXPR_WFL_LINECOL (wfl) = location; | |
6383 | chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE)); | |
6384 | ||
6385 | EXPR_WFL_NODE (left) = merge; | |
6386 | return left; | |
bc3ca41b | 6387 | #endif |
e04a16fb AG |
6388 | } |
6389 | ||
6390 | /* Extract the last identifier component of the qualified in WFL. The | |
6391 | last identifier is removed from the linked list */ | |
6392 | ||
6393 | static tree | |
6394 | cut_identifier_in_qualified (wfl) | |
6395 | tree wfl; | |
6396 | { | |
6397 | tree q; | |
6398 | tree previous = NULL_TREE; | |
6399 | for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q)) | |
6400 | if (!TREE_CHAIN (q)) | |
6401 | { | |
6402 | if (!previous) | |
6403 | fatal ("Operating on a non qualified qualified WFL - " | |
6404 | "cut_identifier_in_qualified"); | |
6405 | TREE_CHAIN (previous) = NULL_TREE; | |
6406 | return TREE_PURPOSE (q); | |
6407 | } | |
6408 | } | |
6409 | ||
6410 | /* Resolve the expression name NAME. Return its decl. */ | |
6411 | ||
6412 | static tree | |
5e942c50 | 6413 | resolve_expression_name (id, orig) |
e04a16fb | 6414 | tree id; |
5e942c50 | 6415 | tree *orig; |
e04a16fb AG |
6416 | { |
6417 | tree name = EXPR_WFL_NODE (id); | |
6418 | tree decl; | |
6419 | ||
6420 | /* 6.5.5.1: Simple expression names */ | |
6421 | if (!PRIMARY_P (id) && !QUALIFIED_P (name)) | |
6422 | { | |
6423 | /* 15.13.1: NAME can appear within the scope of a local variable | |
6424 | declaration */ | |
6425 | if ((decl = IDENTIFIER_LOCAL_VALUE (name))) | |
6426 | return decl; | |
6427 | ||
6428 | /* 15.13.1: NAME can appear within a class declaration */ | |
6429 | else | |
6430 | { | |
6431 | decl = lookup_field_wrapper (current_class, name); | |
6432 | if (decl) | |
6433 | { | |
6434 | int fs = FIELD_STATIC (decl); | |
6435 | /* Instance variable (8.3.1.1) can't appear within | |
6436 | static method, static initializer or initializer for | |
6437 | a static variable. */ | |
6438 | if (!fs && METHOD_STATIC (current_function_decl)) | |
6439 | { | |
7f10c2e2 | 6440 | static_ref_err (id, name, current_class); |
e04a16fb AG |
6441 | return error_mark_node; |
6442 | } | |
22eed1e6 APB |
6443 | /* Instance variables can't appear as an argument of |
6444 | an explicit constructor invocation */ | |
6445 | if (!fs && ctxp->explicit_constructor_p) | |
6446 | { | |
6447 | parse_error_context | |
6448 | (id, "Can't reference `%s' before the superclass " | |
6449 | "constructor has been called", IDENTIFIER_POINTER (name)); | |
6450 | return error_mark_node; | |
6451 | } | |
5e942c50 APB |
6452 | |
6453 | /* Otherwise build what it takes to access the field */ | |
e04a16fb | 6454 | decl = build_field_ref ((fs ? NULL_TREE : current_this), |
7f1d4866 | 6455 | DECL_CONTEXT (decl), name); |
e8fc7396 | 6456 | if (fs && !flag_emit_class_files && !flag_emit_xref) |
7f1d4866 | 6457 | decl = build_class_init (DECL_CONTEXT (decl), decl); |
5e942c50 APB |
6458 | /* We may be asked to save the real field access node */ |
6459 | if (orig) | |
6460 | *orig = decl; | |
6461 | /* And we return what we got */ | |
5b09b33e | 6462 | return decl; |
e04a16fb AG |
6463 | } |
6464 | /* Fall down to error report on undefined variable */ | |
6465 | } | |
6466 | } | |
6467 | /* 6.5.5.2 Qualified Expression Names */ | |
6468 | else | |
6469 | { | |
5e942c50 APB |
6470 | if (orig) |
6471 | *orig = NULL_TREE; | |
e04a16fb AG |
6472 | qualify_ambiguous_name (id); |
6473 | /* 15.10.1 Field Access Using a Primary and/or Expression Name */ | |
6474 | /* 15.10.2: Accessing Superclass Members using super */ | |
6475 | return resolve_field_access (id, NULL, NULL); | |
6476 | } | |
6477 | ||
6478 | /* We've got an error here */ | |
6479 | parse_error_context (id, "Undefined variable `%s'", | |
6480 | IDENTIFIER_POINTER (name)); | |
6481 | ||
6482 | return error_mark_node; | |
6483 | } | |
6484 | ||
7f10c2e2 APB |
6485 | static void |
6486 | static_ref_err (wfl, field_id, class_type) | |
6487 | tree wfl, field_id, class_type; | |
6488 | { | |
6489 | parse_error_context | |
6490 | (wfl, | |
6491 | "Can't make a static reference to nonstatic variable `%s' in class `%s'", | |
6492 | IDENTIFIER_POINTER (field_id), | |
6493 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type)))); | |
6494 | } | |
6495 | ||
e04a16fb AG |
6496 | /* 15.10.1 Field Acess Using a Primary and/or Expression Name. |
6497 | We return something suitable to generate the field access. We also | |
6498 | return the field decl in FIELD_DECL and its type in FIELD_TYPE. If | |
6499 | recipient's address can be null. */ | |
6500 | ||
6501 | static tree | |
6502 | resolve_field_access (qual_wfl, field_decl, field_type) | |
6503 | tree qual_wfl; | |
6504 | tree *field_decl, *field_type; | |
6505 | { | |
6506 | int is_static = 0; | |
6507 | tree field_ref; | |
6508 | tree decl, where_found, type_found; | |
6509 | ||
6510 | if (resolve_qualified_expression_name (qual_wfl, &decl, | |
6511 | &where_found, &type_found)) | |
6512 | return error_mark_node; | |
6513 | ||
6514 | /* Resolve the LENGTH field of an array here */ | |
6515 | if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found) | |
e8fc7396 | 6516 | && ! flag_emit_class_files && ! flag_emit_xref) |
e04a16fb AG |
6517 | { |
6518 | tree length = build_java_array_length_access (where_found); | |
6519 | field_ref = | |
6520 | build_java_arraynull_check (type_found, length, int_type_node); | |
6521 | } | |
6522 | /* We might have been trying to resolve field.method(). In which | |
6523 | case, the resolution is over and decl is the answer */ | |
34f4db93 | 6524 | else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl) |
e04a16fb | 6525 | field_ref = decl; |
34f4db93 | 6526 | else if (JDECL_P (decl)) |
e04a16fb | 6527 | { |
5e942c50 APB |
6528 | int static_final_found = 0; |
6529 | if (!type_found) | |
6530 | type_found = DECL_CONTEXT (decl); | |
34f4db93 | 6531 | is_static = JDECL_P (decl) && FIELD_STATIC (decl); |
5e942c50 APB |
6532 | if (FIELD_FINAL (decl) |
6533 | && JPRIMITIVE_TYPE_P (TREE_TYPE (decl)) | |
6534 | && DECL_LANG_SPECIFIC (decl) | |
7525cc04 | 6535 | && DECL_INITIAL (decl)) |
5e942c50 | 6536 | { |
7525cc04 | 6537 | field_ref = DECL_INITIAL (decl); |
5e942c50 APB |
6538 | static_final_found = 1; |
6539 | } | |
6540 | else | |
7f10c2e2 APB |
6541 | field_ref = build_field_ref ((is_static && !flag_emit_xref? |
6542 | NULL_TREE : where_found), | |
5e942c50 | 6543 | type_found, DECL_NAME (decl)); |
e04a16fb AG |
6544 | if (field_ref == error_mark_node) |
6545 | return error_mark_node; | |
e8fc7396 APB |
6546 | if (is_static && !static_final_found |
6547 | && !flag_emit_class_files && !flag_emit_xref) | |
e04a16fb AG |
6548 | { |
6549 | field_ref = build_class_init (type_found, field_ref); | |
6550 | /* If the static field was identified by an expression that | |
6551 | needs to be generated, make the field access a compound | |
7f10c2e2 | 6552 | expression whose first part is the evaluation of the |
e04a16fb | 6553 | field selector part. */ |
c877974e APB |
6554 | if (where_found && TREE_CODE (where_found) != TYPE_DECL |
6555 | && TREE_CODE (where_found) != RECORD_TYPE) | |
e04a16fb AG |
6556 | { |
6557 | tree type = QUAL_DECL_TYPE (field_ref); | |
8576f094 APB |
6558 | if (TREE_CODE (type) == RECORD_TYPE) |
6559 | type = build_pointer_type (type); | |
e04a16fb AG |
6560 | field_ref = build (COMPOUND_EXPR, type, where_found, field_ref); |
6561 | } | |
6562 | } | |
6563 | } | |
6564 | else | |
6565 | field_ref = decl; | |
6566 | ||
6567 | if (field_decl) | |
6568 | *field_decl = decl; | |
6569 | if (field_type) | |
c877974e APB |
6570 | *field_type = (QUAL_DECL_TYPE (decl) ? |
6571 | QUAL_DECL_TYPE (decl) : TREE_TYPE (decl)); | |
e04a16fb AG |
6572 | return field_ref; |
6573 | } | |
6574 | ||
e28cd97b APB |
6575 | /* If NODE is an access to f static field, strip out the class |
6576 | initialization part and return the field decl, otherwise, return | |
6577 | NODE. */ | |
6578 | ||
6579 | static tree | |
6580 | strip_out_static_field_access_decl (node) | |
6581 | tree node; | |
6582 | { | |
6583 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
6584 | { | |
6585 | tree op1 = TREE_OPERAND (node, 1); | |
6586 | if (TREE_CODE (op1) == COMPOUND_EXPR) | |
6587 | { | |
6588 | tree call = TREE_OPERAND (op1, 0); | |
6589 | if (TREE_CODE (call) == CALL_EXPR | |
6590 | && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR | |
6591 | && TREE_OPERAND (TREE_OPERAND (call, 0), 0) | |
6592 | == soft_initclass_node) | |
6593 | return TREE_OPERAND (op1, 1); | |
6594 | } | |
2f11d407 TT |
6595 | else if (JDECL_P (op1)) |
6596 | return op1; | |
e28cd97b APB |
6597 | } |
6598 | return node; | |
6599 | } | |
6600 | ||
e04a16fb AG |
6601 | /* 6.5.5.2: Qualified Expression Names */ |
6602 | ||
6603 | static int | |
6604 | resolve_qualified_expression_name (wfl, found_decl, where_found, type_found) | |
6605 | tree wfl; | |
6606 | tree *found_decl, *type_found, *where_found; | |
6607 | { | |
6608 | int from_type = 0; /* Field search initiated from a type */ | |
6609 | int from_super = 0, from_cast = 0; | |
6610 | int previous_call_static = 0; | |
6611 | int is_static; | |
6612 | tree decl = NULL_TREE, type = NULL_TREE, q; | |
c877974e | 6613 | *type_found = *where_found = NULL_TREE; |
e04a16fb AG |
6614 | |
6615 | for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q)) | |
6616 | { | |
6617 | tree qual_wfl = QUAL_WFL (q); | |
7705e9db APB |
6618 | tree ret_decl; /* for EH checking */ |
6619 | int location; /* for EH checking */ | |
e04a16fb AG |
6620 | |
6621 | /* 15.10.1 Field Access Using a Primary */ | |
e04a16fb AG |
6622 | switch (TREE_CODE (qual_wfl)) |
6623 | { | |
6624 | case CALL_EXPR: | |
b67d701b | 6625 | case NEW_CLASS_EXPR: |
e04a16fb AG |
6626 | /* If the access to the function call is a non static field, |
6627 | build the code to access it. */ | |
34f4db93 | 6628 | if (JDECL_P (decl) && !FIELD_STATIC (decl)) |
e04a16fb | 6629 | { |
ac825856 APB |
6630 | decl = maybe_access_field (decl, *where_found, |
6631 | DECL_CONTEXT (decl)); | |
e04a16fb AG |
6632 | if (decl == error_mark_node) |
6633 | return 1; | |
6634 | } | |
6635 | /* And code for the function call */ | |
6636 | if (complete_function_arguments (qual_wfl)) | |
6637 | return 1; | |
7705e9db | 6638 | |
89e09b9a PB |
6639 | if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR) |
6640 | CALL_USING_SUPER (qual_wfl) = 1; | |
7705e9db APB |
6641 | location = (TREE_CODE (qual_wfl) == CALL_EXPR ? |
6642 | EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0); | |
6643 | *where_found = patch_method_invocation (qual_wfl, decl, type, | |
6644 | &is_static, &ret_decl); | |
e04a16fb AG |
6645 | if (*where_found == error_mark_node) |
6646 | return 1; | |
6647 | *type_found = type = QUAL_DECL_TYPE (*where_found); | |
6648 | ||
7705e9db APB |
6649 | /* EH check */ |
6650 | if (location) | |
6651 | check_thrown_exceptions (location, ret_decl); | |
6652 | ||
e04a16fb AG |
6653 | /* If the previous call was static and this one is too, |
6654 | build a compound expression to hold the two (because in | |
6655 | that case, previous function calls aren't transported as | |
6656 | forcoming function's argument. */ | |
6657 | if (previous_call_static && is_static) | |
6658 | { | |
6659 | decl = build (COMPOUND_EXPR, type, decl, *where_found); | |
6660 | TREE_SIDE_EFFECTS (decl) = 1; | |
6661 | } | |
6662 | else | |
6663 | { | |
6664 | previous_call_static = is_static; | |
6665 | decl = *where_found; | |
6666 | } | |
6667 | continue; | |
6668 | ||
d8fccff5 APB |
6669 | case NEW_ARRAY_EXPR: |
6670 | *where_found = decl = java_complete_tree (qual_wfl); | |
6671 | if (decl == error_mark_node) | |
6672 | return 1; | |
6673 | *type_found = type = QUAL_DECL_TYPE (decl); | |
6674 | CLASS_LOADED_P (type) = 1; | |
6675 | continue; | |
6676 | ||
e04a16fb AG |
6677 | case CONVERT_EXPR: |
6678 | *where_found = decl = java_complete_tree (qual_wfl); | |
6679 | if (decl == error_mark_node) | |
6680 | return 1; | |
6681 | *type_found = type = QUAL_DECL_TYPE (decl); | |
6682 | from_cast = 1; | |
6683 | continue; | |
6684 | ||
22eed1e6 | 6685 | case CONDITIONAL_EXPR: |
5e942c50 | 6686 | case STRING_CST: |
ac22f9cb | 6687 | case MODIFY_EXPR: |
22eed1e6 APB |
6688 | *where_found = decl = java_complete_tree (qual_wfl); |
6689 | if (decl == error_mark_node) | |
6690 | return 1; | |
6691 | *type_found = type = QUAL_DECL_TYPE (decl); | |
6692 | continue; | |
6693 | ||
e04a16fb AG |
6694 | case ARRAY_REF: |
6695 | /* If the access to the function call is a non static field, | |
6696 | build the code to access it. */ | |
34f4db93 | 6697 | if (JDECL_P (decl) && !FIELD_STATIC (decl)) |
e04a16fb AG |
6698 | { |
6699 | decl = maybe_access_field (decl, *where_found, type); | |
6700 | if (decl == error_mark_node) | |
6701 | return 1; | |
6702 | } | |
6703 | /* And code for the array reference expression */ | |
6704 | decl = java_complete_tree (qual_wfl); | |
6705 | if (decl == error_mark_node) | |
6706 | return 1; | |
6707 | type = QUAL_DECL_TYPE (decl); | |
6708 | continue; | |
0a2138e2 APB |
6709 | |
6710 | default: | |
6711 | /* Fix for -Wall Just go to the next statement. Don't | |
6712 | continue */ | |
a3f406ce | 6713 | break; |
e04a16fb AG |
6714 | } |
6715 | ||
6716 | /* If we fall here, we weren't processing a (static) function call. */ | |
6717 | previous_call_static = 0; | |
6718 | ||
6719 | /* It can be the keyword THIS */ | |
6720 | if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node) | |
6721 | { | |
6722 | if (!current_this) | |
6723 | { | |
6724 | parse_error_context | |
6725 | (wfl, "Keyword `this' used outside allowed context"); | |
6726 | return 1; | |
6727 | } | |
6728 | /* We have to generate code for intermediate acess */ | |
6729 | *where_found = decl = current_this; | |
5e942c50 | 6730 | *type_found = type = QUAL_DECL_TYPE (decl); |
e04a16fb AG |
6731 | continue; |
6732 | } | |
6733 | ||
6734 | /* 15.10.2 Accessing Superclass Members using SUPER */ | |
6735 | if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node) | |
6736 | { | |
6737 | tree node; | |
6738 | /* Check on the restricted use of SUPER */ | |
6739 | if (METHOD_STATIC (current_function_decl) | |
6740 | || current_class == object_type_node) | |
6741 | { | |
6742 | parse_error_context | |
6743 | (wfl, "Keyword `super' used outside allowed context"); | |
6744 | return 1; | |
6745 | } | |
6746 | /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */ | |
6747 | node = build_cast (EXPR_WFL_LINECOL (qual_wfl), | |
6748 | CLASSTYPE_SUPER (current_class), | |
6749 | build_this (EXPR_WFL_LINECOL (qual_wfl))); | |
6750 | *where_found = decl = java_complete_tree (node); | |
22eed1e6 APB |
6751 | if (decl == error_mark_node) |
6752 | return 1; | |
e04a16fb AG |
6753 | *type_found = type = QUAL_DECL_TYPE (decl); |
6754 | from_super = from_type = 1; | |
6755 | continue; | |
6756 | } | |
6757 | ||
6758 | /* 15.13.1: Can't search for field name in packages, so we | |
6759 | assume a variable/class name was meant. */ | |
6760 | if (RESOLVE_PACKAGE_NAME_P (qual_wfl)) | |
6761 | { | |
5e942c50 APB |
6762 | tree name = resolve_package (wfl, &q); |
6763 | if (name) | |
6764 | { | |
6765 | *where_found = decl = resolve_no_layout (name, qual_wfl); | |
6766 | /* We wan't to be absolutely that the class is laid | |
6767 | out. We're going to search something inside it. */ | |
6768 | *type_found = type = TREE_TYPE (decl); | |
6769 | layout_class (type); | |
6770 | from_type = 1; | |
6771 | /* Should be a list, really. FIXME */ | |
6772 | RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 1; | |
6773 | RESOLVE_PACKAGE_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 0; | |
6774 | } | |
e04a16fb | 6775 | else |
5e942c50 APB |
6776 | { |
6777 | if (from_super || from_cast) | |
6778 | parse_error_context | |
6779 | ((from_cast ? qual_wfl : wfl), | |
6780 | "No variable `%s' defined in class `%s'", | |
6781 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), | |
6782 | lang_printable_name (type, 0)); | |
6783 | else | |
6784 | parse_error_context | |
6785 | (qual_wfl, "Undefined variable or class name: `%s'", | |
6786 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl))); | |
6787 | return 1; | |
6788 | } | |
e04a16fb AG |
6789 | } |
6790 | ||
6791 | /* We have a type name. It's been already resolved when the | |
6792 | expression was qualified. */ | |
6793 | else if (RESOLVE_TYPE_NAME_P (qual_wfl)) | |
6794 | { | |
6795 | if (!(decl = QUAL_RESOLUTION (q))) | |
6796 | return 1; /* Error reported already */ | |
6797 | ||
6798 | if (not_accessible_p (TREE_TYPE (decl), decl, 0)) | |
6799 | { | |
6800 | parse_error_context | |
6801 | (qual_wfl, "Can't access %s field `%s.%s' from `%s'", | |
6802 | java_accstring_lookup (get_access_flags_from_decl (decl)), | |
2aa11e97 | 6803 | GET_TYPE_NAME (type), |
e04a16fb AG |
6804 | IDENTIFIER_POINTER (DECL_NAME (decl)), |
6805 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)))); | |
6806 | return 1; | |
6807 | } | |
5e942c50 | 6808 | check_deprecation (qual_wfl, decl); |
e04a16fb AG |
6809 | |
6810 | type = TREE_TYPE (decl); | |
6811 | from_type = 1; | |
6812 | } | |
6813 | /* We resolve and expression name */ | |
6814 | else | |
6815 | { | |
cd531a2e | 6816 | tree field_decl = NULL_TREE; |
e04a16fb AG |
6817 | |
6818 | /* If there exists an early resolution, use it. That occurs | |
6819 | only once and we know that there are more things to | |
6820 | come. Don't do that when processing something after SUPER | |
6821 | (we need more thing to be put in place below */ | |
6822 | if (!from_super && QUAL_RESOLUTION (q)) | |
b67d701b PB |
6823 | { |
6824 | decl = QUAL_RESOLUTION (q); | |
c877974e | 6825 | if (!type) |
5e942c50 | 6826 | { |
7f10c2e2 APB |
6827 | if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl)) |
6828 | { | |
6829 | if (current_this) | |
6830 | *where_found = current_this; | |
6831 | else | |
6832 | { | |
6833 | static_ref_err (qual_wfl, DECL_NAME (decl), | |
6834 | current_class); | |
6835 | return 1; | |
6836 | } | |
6837 | } | |
c877974e APB |
6838 | else |
6839 | { | |
6840 | *where_found = TREE_TYPE (decl); | |
6841 | if (TREE_CODE (*where_found) == POINTER_TYPE) | |
6842 | *where_found = TREE_TYPE (*where_found); | |
6843 | } | |
5e942c50 | 6844 | } |
b67d701b | 6845 | } |
e04a16fb AG |
6846 | |
6847 | /* We have to search for a field, knowing the type of its | |
6848 | container. The flag FROM_TYPE indicates that we resolved | |
6849 | the last member of the expression as a type name, which | |
5e942c50 APB |
6850 | means that for the resolution of this field, we'll look |
6851 | for other errors than if it was resolved as a member of | |
6852 | an other field. */ | |
e04a16fb AG |
6853 | else |
6854 | { | |
6855 | int is_static; | |
5e942c50 APB |
6856 | tree field_decl_type; /* For layout */ |
6857 | ||
e04a16fb AG |
6858 | if (!from_type && !JREFERENCE_TYPE_P (type)) |
6859 | { | |
6860 | parse_error_context | |
6861 | (qual_wfl, "Attempt to reference field `%s' in `%s %s'", | |
6862 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), | |
0a2138e2 | 6863 | lang_printable_name (type, 0), |
e04a16fb AG |
6864 | IDENTIFIER_POINTER (DECL_NAME (field_decl))); |
6865 | return 1; | |
6866 | } | |
6867 | ||
dc0b3eff PB |
6868 | field_decl = lookup_field_wrapper (type, |
6869 | EXPR_WFL_NODE (qual_wfl)); | |
6870 | if (field_decl == NULL_TREE) | |
e04a16fb AG |
6871 | { |
6872 | parse_error_context | |
2aa11e97 | 6873 | (qual_wfl, "No variable `%s' defined in type `%s'", |
e04a16fb | 6874 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), |
2aa11e97 | 6875 | GET_TYPE_NAME (type)); |
e04a16fb AG |
6876 | return 1; |
6877 | } | |
dc0b3eff PB |
6878 | if (field_decl == error_mark_node) |
6879 | return 1; | |
5e942c50 APB |
6880 | |
6881 | /* Layout the type of field_decl, since we may need | |
c877974e APB |
6882 | it. Don't do primitive types or loaded classes. The |
6883 | situation of non primitive arrays may not handled | |
6884 | properly here. FIXME */ | |
5e942c50 APB |
6885 | if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE) |
6886 | field_decl_type = TREE_TYPE (TREE_TYPE (field_decl)); | |
6887 | else | |
6888 | field_decl_type = TREE_TYPE (field_decl); | |
6889 | if (!JPRIMITIVE_TYPE_P (field_decl_type) | |
c877974e APB |
6890 | && !CLASS_LOADED_P (field_decl_type) |
6891 | && !TYPE_ARRAY_P (field_decl_type)) | |
6892 | resolve_and_layout (field_decl_type, NULL_TREE); | |
6893 | if (TYPE_ARRAY_P (field_decl_type)) | |
6894 | CLASS_LOADED_P (field_decl_type) = 1; | |
e04a16fb AG |
6895 | |
6896 | /* Check on accessibility here */ | |
6897 | if (not_accessible_p (type, field_decl, from_super)) | |
6898 | { | |
6899 | parse_error_context | |
6900 | (qual_wfl, | |
6901 | "Can't access %s field `%s.%s' from `%s'", | |
6902 | java_accstring_lookup | |
6903 | (get_access_flags_from_decl (field_decl)), | |
2aa11e97 | 6904 | GET_TYPE_NAME (type), |
e04a16fb AG |
6905 | IDENTIFIER_POINTER (DECL_NAME (field_decl)), |
6906 | IDENTIFIER_POINTER | |
6907 | (DECL_NAME (TYPE_NAME (current_class)))); | |
6908 | return 1; | |
6909 | } | |
5e942c50 | 6910 | check_deprecation (qual_wfl, field_decl); |
e04a16fb AG |
6911 | |
6912 | /* There are things to check when fields are accessed | |
6913 | from type. There are no restrictions on a static | |
6914 | declaration of the field when it is accessed from an | |
6915 | interface */ | |
6916 | is_static = FIELD_STATIC (field_decl); | |
6917 | if (!from_super && from_type | |
6918 | && !TYPE_INTERFACE_P (type) && !is_static) | |
6919 | { | |
7f10c2e2 | 6920 | static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type); |
e04a16fb AG |
6921 | return 1; |
6922 | } | |
6923 | from_cast = from_super = 0; | |
6924 | ||
5e942c50 APB |
6925 | /* If we need to generate something to get a proper |
6926 | handle on what this field is accessed from, do it | |
6927 | now. */ | |
e04a16fb AG |
6928 | if (!is_static) |
6929 | { | |
c583dd46 | 6930 | decl = maybe_access_field (decl, *where_found, *type_found); |
e04a16fb AG |
6931 | if (decl == error_mark_node) |
6932 | return 1; | |
6933 | } | |
6934 | ||
6935 | /* We want to keep the location were found it, and the type | |
6936 | we found. */ | |
6937 | *where_found = decl; | |
6938 | *type_found = type; | |
6939 | ||
6940 | /* This is the decl found and eventually the next one to | |
6941 | search from */ | |
6942 | decl = field_decl; | |
6943 | } | |
e04a16fb AG |
6944 | from_type = 0; |
6945 | type = QUAL_DECL_TYPE (decl); | |
6946 | } | |
6947 | } | |
6948 | *found_decl = decl; | |
6949 | return 0; | |
6950 | } | |
6951 | ||
6952 | /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl) | |
6953 | can't be accessed from REFERENCE (a record type). */ | |
6954 | ||
be245ac0 KG |
6955 | static int |
6956 | not_accessible_p (reference, member, from_super) | |
e04a16fb AG |
6957 | tree reference, member; |
6958 | int from_super; | |
6959 | { | |
6960 | int access_flag = get_access_flags_from_decl (member); | |
6961 | ||
6962 | /* Access always granted for members declared public */ | |
6963 | if (access_flag & ACC_PUBLIC) | |
6964 | return 0; | |
6965 | ||
6966 | /* Check access on protected members */ | |
6967 | if (access_flag & ACC_PROTECTED) | |
6968 | { | |
6969 | /* Access granted if it occurs from within the package | |
6970 | containing the class in which the protected member is | |
6971 | declared */ | |
6972 | if (class_in_current_package (DECL_CONTEXT (member))) | |
6973 | return 0; | |
6974 | ||
9bbc7d9f PB |
6975 | /* If accessed with the form `super.member', then access is granted */ |
6976 | if (from_super) | |
6977 | return 0; | |
e04a16fb | 6978 | |
9bbc7d9f PB |
6979 | /* Otherwise, access is granted if occuring from the class where |
6980 | member is declared or a subclass of it */ | |
6981 | if (inherits_from_p (reference, current_class)) | |
6982 | return 0; | |
e04a16fb AG |
6983 | return 1; |
6984 | } | |
6985 | ||
6986 | /* Check access on private members. Access is granted only if it | |
c877974e | 6987 | occurs from within the class in witch it is declared */ |
e04a16fb AG |
6988 | if (access_flag & ACC_PRIVATE) |
6989 | return (current_class == DECL_CONTEXT (member) ? 0 : 1); | |
6990 | ||
6991 | /* Default access are permitted only when occuring within the | |
6992 | package in which the type (REFERENCE) is declared. In other words, | |
6993 | REFERENCE is defined in the current package */ | |
6994 | if (ctxp->package) | |
6995 | return !class_in_current_package (reference); | |
6996 | ||
6997 | /* Otherwise, access is granted */ | |
6998 | return 0; | |
6999 | } | |
7000 | ||
5e942c50 APB |
7001 | /* Test deprecated decl access. */ |
7002 | static void | |
7003 | check_deprecation (wfl, decl) | |
7004 | tree wfl, decl; | |
7005 | { | |
49f48c71 | 7006 | const char *file = DECL_SOURCE_FILE (decl); |
5e942c50 APB |
7007 | /* Complain if the field is deprecated and the file it was defined |
7008 | in isn't compiled at the same time the file which contains its | |
7009 | use is */ | |
7010 | if (DECL_DEPRECATED (decl) | |
7011 | && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file))) | |
7012 | { | |
7013 | char the [20]; | |
7014 | switch (TREE_CODE (decl)) | |
7015 | { | |
7016 | case FUNCTION_DECL: | |
7017 | strcpy (the, "method"); | |
7018 | break; | |
7019 | case FIELD_DECL: | |
7020 | strcpy (the, "field"); | |
7021 | break; | |
7022 | case TYPE_DECL: | |
7023 | strcpy (the, "class"); | |
7024 | break; | |
15fdcfe9 PB |
7025 | default: |
7026 | fatal ("unexpected DECL code - check_deprecation"); | |
5e942c50 APB |
7027 | } |
7028 | parse_warning_context | |
7029 | (wfl, "The %s `%s' in class `%s' has been deprecated", | |
7030 | the, lang_printable_name (decl, 0), | |
7031 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))); | |
7032 | } | |
7033 | } | |
7034 | ||
e04a16fb AG |
7035 | /* Returns 1 if class was declared in the current package, 0 otherwise */ |
7036 | ||
7037 | static int | |
7038 | class_in_current_package (class) | |
7039 | tree class; | |
7040 | { | |
7041 | static tree cache = NULL_TREE; | |
7042 | int qualified_flag; | |
7043 | tree left; | |
7044 | ||
7045 | if (cache == class) | |
7046 | return 1; | |
7047 | ||
7048 | qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class))); | |
7049 | ||
7050 | /* If the current package is empty and the name of CLASS is | |
7051 | qualified, class isn't in the current package. If there is a | |
7052 | current package and the name of the CLASS is not qualified, class | |
7053 | isn't in the current package */ | |
0a2138e2 | 7054 | if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag)) |
e04a16fb AG |
7055 | return 0; |
7056 | ||
7057 | /* If there is not package and the name of CLASS isn't qualified, | |
7058 | they belong to the same unnamed package */ | |
7059 | if (!ctxp->package && !qualified_flag) | |
7060 | return 1; | |
7061 | ||
7062 | /* Compare the left part of the name of CLASS with the package name */ | |
7063 | breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class))); | |
7064 | if (ctxp->package == left) | |
7065 | { | |
7066 | cache = class; | |
7067 | return 1; | |
7068 | } | |
7069 | return 0; | |
7070 | } | |
7071 | ||
7072 | /* This function may generate code to access DECL from WHERE. This is | |
7073 | done only if certain conditions meet. */ | |
7074 | ||
7075 | static tree | |
7076 | maybe_access_field (decl, where, type) | |
7077 | tree decl, where, type; | |
7078 | { | |
5e942c50 APB |
7079 | if (TREE_CODE (decl) == FIELD_DECL && decl != current_this |
7080 | && !FIELD_STATIC (decl)) | |
e04a16fb | 7081 | decl = build_field_ref (where ? where : current_this, |
c583dd46 APB |
7082 | (type ? type : DECL_CONTEXT (decl)), |
7083 | DECL_NAME (decl)); | |
e04a16fb AG |
7084 | return decl; |
7085 | } | |
7086 | ||
15fdcfe9 | 7087 | /* Build a method invocation, by patching PATCH. If non NULL |
e04a16fb AG |
7088 | and according to the situation, PRIMARY and WHERE may be |
7089 | used. IS_STATIC is set to 1 if the invoked function is static. */ | |
7090 | ||
7091 | static tree | |
89e09b9a | 7092 | patch_method_invocation (patch, primary, where, is_static, ret_decl) |
e04a16fb AG |
7093 | tree patch, primary, where; |
7094 | int *is_static; | |
b9f7e36c | 7095 | tree *ret_decl; |
e04a16fb AG |
7096 | { |
7097 | tree wfl = TREE_OPERAND (patch, 0); | |
7098 | tree args = TREE_OPERAND (patch, 1); | |
7099 | tree name = EXPR_WFL_NODE (wfl); | |
5e942c50 | 7100 | tree list; |
22eed1e6 | 7101 | int is_static_flag = 0; |
89e09b9a | 7102 | int is_super_init = 0; |
bccaf73a | 7103 | tree this_arg = NULL_TREE; |
e04a16fb AG |
7104 | |
7105 | /* Should be overriden if everything goes well. Otherwise, if | |
7106 | something fails, it should keep this value. It stop the | |
7107 | evaluation of a bogus assignment. See java_complete_tree, | |
7108 | MODIFY_EXPR: for the reasons why we sometimes want to keep on | |
7109 | evaluating an assignment */ | |
7110 | TREE_TYPE (patch) = error_mark_node; | |
7111 | ||
7112 | /* Since lookup functions are messing with line numbers, save the | |
7113 | context now. */ | |
7114 | java_parser_context_save_global (); | |
7115 | ||
7116 | /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */ | |
7117 | ||
7118 | /* Resolution of qualified name, excluding constructors */ | |
7119 | if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch)) | |
7120 | { | |
7121 | tree class_decl, identifier, identifier_wfl; | |
7122 | /* Extract the last IDENTIFIER of the qualified | |
7123 | expression. This is a wfl and we will use it's location | |
7124 | data during error report. */ | |
7125 | identifier_wfl = cut_identifier_in_qualified (wfl); | |
7126 | identifier = EXPR_WFL_NODE (identifier_wfl); | |
7127 | ||
7128 | /* Given the context, IDENTIFIER is syntactically qualified | |
7129 | as a MethodName. We need to qualify what's before */ | |
7130 | qualify_ambiguous_name (wfl); | |
7131 | ||
2c56429a | 7132 | /* Package resolution */ |
e04a16fb AG |
7133 | if (RESOLVE_PACKAGE_NAME_P (wfl)) |
7134 | { | |
2c56429a APB |
7135 | tree next, decl, name = resolve_package (wfl, &next); |
7136 | ||
7137 | if (!name) | |
7138 | { | |
7139 | tree remainder; | |
7140 | breakdown_qualified (&remainder, NULL, EXPR_WFL_NODE (wfl)); | |
7141 | parse_error_context (wfl, "Can't search method `%s' in package " | |
7142 | "`%s'",IDENTIFIER_POINTER (identifier), | |
7143 | IDENTIFIER_POINTER (remainder)); | |
7144 | PATCH_METHOD_RETURN_ERROR (); | |
7145 | } | |
7146 | RESOLVE_PACKAGE_NAME_P (wfl) = 0; | |
7147 | if ((decl = resolve_no_layout (name, QUAL_WFL (next)))) | |
7148 | { | |
7149 | QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl)) = decl; | |
7150 | RESOLVE_EXPRESSION_NAME_P (wfl) = 0; | |
7151 | RESOLVE_TYPE_NAME_P (wfl) = 1; | |
7152 | } | |
7153 | else | |
7154 | { | |
7155 | RESOLVE_EXPRESSION_NAME_P (wfl) = 1; | |
7156 | RESOLVE_TYPE_NAME_P (wfl) = 0; | |
7157 | } | |
e04a16fb | 7158 | } |
2c56429a | 7159 | |
e04a16fb | 7160 | /* We're resolving a call from a type */ |
2c56429a | 7161 | if (RESOLVE_TYPE_NAME_P (wfl)) |
e04a16fb AG |
7162 | { |
7163 | tree decl = QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl)); | |
7164 | tree name = DECL_NAME (decl); | |
7165 | tree type; | |
7166 | ||
7167 | class_decl = resolve_and_layout (name, wfl); | |
7168 | if (CLASS_INTERFACE (decl)) | |
7169 | { | |
7170 | parse_error_context | |
7171 | (identifier_wfl, "Can't make static reference to method " | |
7172 | "`%s' in interface `%s'", IDENTIFIER_POINTER (identifier), | |
7173 | IDENTIFIER_POINTER (name)); | |
b9f7e36c | 7174 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb AG |
7175 | } |
7176 | /* Look the method up in the type selector. The method ought | |
7177 | to be static. */ | |
7178 | type = TREE_TYPE (class_decl); | |
7179 | list = lookup_method_invoke (0, wfl, type, identifier, args); | |
7180 | if (list && !METHOD_STATIC (list)) | |
7181 | { | |
c2e3db92 | 7182 | char *fct_name = xstrdup (lang_printable_name (list, 0)); |
e04a16fb AG |
7183 | parse_error_context |
7184 | (identifier_wfl, | |
7185 | "Can't make static reference to method `%s %s' in class `%s'", | |
0a2138e2 APB |
7186 | lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0), |
7187 | fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); | |
e04a16fb | 7188 | free (fct_name); |
b9f7e36c | 7189 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb | 7190 | } |
5e942c50 | 7191 | args = nreverse (args); |
e04a16fb AG |
7192 | } |
7193 | /* We're resolving an expression name */ | |
7194 | else | |
7195 | { | |
7196 | tree field, type; | |
7197 | ||
7198 | /* 1- Find the field to which the call applies */ | |
7199 | field = resolve_field_access (wfl, NULL, &type); | |
7200 | if (field == error_mark_node) | |
b9f7e36c | 7201 | PATCH_METHOD_RETURN_ERROR (); |
c3f2a476 APB |
7202 | /* field is used in lieu of a primary. It alows us not to |
7203 | report errors on erroneous use of `this' in | |
7204 | constructors. */ | |
7205 | primary = field; | |
e04a16fb AG |
7206 | |
7207 | /* 2- Do the layout of the class where the last field | |
7208 | was found, so we can search it. */ | |
c877974e | 7209 | class_decl = resolve_and_layout (type, NULL_TREE); |
3e78f871 | 7210 | if (class_decl != NULL_TREE) |
c877974e APB |
7211 | type = TREE_TYPE (class_decl); |
7212 | ||
e04a16fb AG |
7213 | /* 3- Retrieve a filtered list of method matches, Refine |
7214 | if necessary. In any cases, point out errors. */ | |
7215 | list = lookup_method_invoke (0, identifier_wfl, type, | |
7216 | identifier, args); | |
7217 | ||
7218 | /* 4- Add the field as an argument */ | |
bccaf73a PB |
7219 | args = nreverse (args); |
7220 | this_arg = field; | |
e04a16fb AG |
7221 | } |
7222 | ||
5e942c50 | 7223 | /* IDENTIFIER_WFL will be used to report any problem further */ |
e04a16fb AG |
7224 | wfl = identifier_wfl; |
7225 | } | |
7226 | /* Resolution of simple names, names generated after a primary: or | |
7227 | constructors */ | |
7228 | else | |
7229 | { | |
cd531a2e | 7230 | tree class_to_search = NULL_TREE; |
e04a16fb AG |
7231 | int lc; /* Looking for Constructor */ |
7232 | ||
7233 | /* We search constructor in their target class */ | |
7234 | if (CALL_CONSTRUCTOR_P (patch)) | |
7235 | { | |
22eed1e6 APB |
7236 | if (TREE_CODE (patch) == NEW_CLASS_EXPR) |
7237 | class_to_search = EXPR_WFL_NODE (wfl); | |
7238 | else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) == | |
7239 | this_identifier_node) | |
7240 | class_to_search = NULL_TREE; | |
7241 | else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) == | |
7242 | super_identifier_node) | |
e04a16fb | 7243 | { |
89e09b9a | 7244 | is_super_init = 1; |
22eed1e6 APB |
7245 | if (CLASSTYPE_SUPER (current_class)) |
7246 | class_to_search = | |
7247 | DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class))); | |
7248 | else | |
7249 | { | |
7250 | parse_error_context (wfl, "Can't invoke super constructor " | |
7251 | "on java.lang.Object"); | |
7252 | PATCH_METHOD_RETURN_ERROR (); | |
7253 | } | |
e04a16fb | 7254 | } |
22eed1e6 APB |
7255 | |
7256 | /* Class to search is NULL if we're searching the current one */ | |
7257 | if (class_to_search) | |
e04a16fb | 7258 | { |
23a79c61 APB |
7259 | class_to_search = resolve_and_layout (class_to_search, |
7260 | NULL_TREE); | |
22eed1e6 APB |
7261 | if (!class_to_search) |
7262 | { | |
7263 | parse_error_context | |
7264 | (wfl, "Class `%s' not found in type declaration", | |
7265 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
7266 | PATCH_METHOD_RETURN_ERROR (); | |
7267 | } | |
7268 | ||
5e942c50 APB |
7269 | /* Can't instantiate an abstract class, but we can |
7270 | invoke it's constructor. It's use within the `new' | |
7271 | context is denied here. */ | |
7272 | if (CLASS_ABSTRACT (class_to_search) | |
7273 | && TREE_CODE (patch) == NEW_CLASS_EXPR) | |
22eed1e6 APB |
7274 | { |
7275 | parse_error_context | |
7276 | (wfl, "Class `%s' is an abstract class. It can't be " | |
7277 | "instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
7278 | PATCH_METHOD_RETURN_ERROR (); | |
7279 | } | |
7280 | class_to_search = TREE_TYPE (class_to_search); | |
e04a16fb | 7281 | } |
22eed1e6 APB |
7282 | else |
7283 | class_to_search = current_class; | |
e04a16fb AG |
7284 | lc = 1; |
7285 | } | |
7286 | /* This is a regular search in the local class, unless an | |
7287 | alternate class is specified. */ | |
7288 | else | |
7289 | { | |
7290 | class_to_search = (where ? where : current_class); | |
7291 | lc = 0; | |
7292 | } | |
7293 | ||
7294 | /* NAME is a simple identifier or comes from a primary. Search | |
7295 | in the class whose declaration contain the method being | |
7296 | invoked. */ | |
c877974e | 7297 | resolve_and_layout (class_to_search, NULL_TREE); |
e04a16fb AG |
7298 | list = lookup_method_invoke (lc, wfl, class_to_search, name, args); |
7299 | ||
7300 | /* Don't continue if no method were found, as the next statement | |
7301 | can't be executed then. */ | |
b9f7e36c APB |
7302 | if (!list) |
7303 | PATCH_METHOD_RETURN_ERROR (); | |
e04a16fb AG |
7304 | |
7305 | /* Check for static reference if non static methods */ | |
7306 | if (check_for_static_method_reference (wfl, patch, list, | |
7307 | class_to_search, primary)) | |
b9f7e36c | 7308 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb | 7309 | |
22eed1e6 APB |
7310 | /* Non static methods are called with the current object extra |
7311 | argument. If patch a `new TYPE()', the argument is the value | |
7312 | returned by the object allocator. If method is resolved as a | |
7313 | primary, use the primary otherwise use the current THIS. */ | |
b9f7e36c | 7314 | args = nreverse (args); |
bccaf73a PB |
7315 | if (TREE_CODE (patch) != NEW_CLASS_EXPR) |
7316 | this_arg = primary ? primary : current_this; | |
e04a16fb | 7317 | } |
b67d701b | 7318 | |
e04a16fb AG |
7319 | /* Merge point of all resolution schemes. If we have nothing, this |
7320 | is an error, already signaled */ | |
b9f7e36c APB |
7321 | if (!list) |
7322 | PATCH_METHOD_RETURN_ERROR (); | |
b67d701b | 7323 | |
e04a16fb AG |
7324 | /* Check accessibility, position the is_static flag, build and |
7325 | return the call */ | |
9bbc7d9f | 7326 | if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0)) |
e04a16fb | 7327 | { |
c2e3db92 | 7328 | char *fct_name = xstrdup (lang_printable_name (list, 0)); |
e04a16fb AG |
7329 | parse_error_context |
7330 | (wfl, "Can't access %s method `%s %s.%s' from `%s'", | |
7331 | java_accstring_lookup (get_access_flags_from_decl (list)), | |
0a2138e2 | 7332 | lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0), |
5e942c50 APB |
7333 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))), |
7334 | fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)))); | |
e04a16fb | 7335 | free (fct_name); |
b9f7e36c | 7336 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb | 7337 | } |
5e942c50 | 7338 | check_deprecation (wfl, list); |
22eed1e6 APB |
7339 | |
7340 | is_static_flag = METHOD_STATIC (list); | |
bccaf73a PB |
7341 | if (! METHOD_STATIC (list) && this_arg != NULL_TREE) |
7342 | args = tree_cons (NULL_TREE, this_arg, args); | |
22eed1e6 | 7343 | |
c3f2a476 APB |
7344 | /* In the context of an explicit constructor invocation, we can't |
7345 | invoke any method relying on `this'. Exceptions are: we're | |
7346 | invoking a static function, primary exists and is not the current | |
7347 | this, we're creating a new object. */ | |
22eed1e6 | 7348 | if (ctxp->explicit_constructor_p |
c3f2a476 APB |
7349 | && !is_static_flag |
7350 | && (!primary || primary == current_this) | |
7351 | && (TREE_CODE (patch) != NEW_CLASS_EXPR)) | |
22eed1e6 APB |
7352 | { |
7353 | parse_error_context | |
7354 | (wfl, "Can't reference `this' before the superclass constructor has " | |
7355 | "been called"); | |
7356 | PATCH_METHOD_RETURN_ERROR (); | |
7357 | } | |
e04a16fb | 7358 | java_parser_context_restore_global (); |
22eed1e6 APB |
7359 | if (is_static) |
7360 | *is_static = is_static_flag; | |
b9f7e36c APB |
7361 | /* Sometimes, we want the decl of the selected method. Such as for |
7362 | EH checking */ | |
7363 | if (ret_decl) | |
7364 | *ret_decl = list; | |
89e09b9a PB |
7365 | patch = patch_invoke (patch, list, args); |
7366 | if (is_super_init && CLASS_HAS_FINIT_P (current_class)) | |
7367 | { | |
7368 | /* Generate the code used to initialize fields declared with an | |
7369 | initialization statement. For now, it returns a call the the | |
7370 | artificial function $finit$, if required. */ | |
7371 | ||
7372 | tree finit_call = | |
7373 | build_method_invocation (build_expr_wfl (finit_identifier_node, | |
7374 | input_filename, 0, 0), | |
7375 | NULL_TREE); | |
7376 | patch = build (COMPOUND_EXPR, void_type_node, patch, | |
7377 | java_complete_tree (finit_call)); | |
7378 | CAN_COMPLETE_NORMALLY (patch) = 1; | |
7379 | } | |
7380 | return patch; | |
e04a16fb AG |
7381 | } |
7382 | ||
7383 | /* Check that we're not trying to do a static reference to a method in | |
7384 | non static method. Return 1 if it's the case, 0 otherwise. */ | |
7385 | ||
7386 | static int | |
7387 | check_for_static_method_reference (wfl, node, method, where, primary) | |
7388 | tree wfl, node, method, where, primary; | |
7389 | { | |
7390 | if (METHOD_STATIC (current_function_decl) | |
7391 | && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node)) | |
7392 | { | |
c2e3db92 | 7393 | char *fct_name = xstrdup (lang_printable_name (method, 0)); |
e04a16fb AG |
7394 | parse_error_context |
7395 | (wfl, "Can't make static reference to method `%s %s' in class `%s'", | |
0a2138e2 | 7396 | lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name, |
e04a16fb AG |
7397 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where)))); |
7398 | free (fct_name); | |
7399 | return 1; | |
7400 | } | |
7401 | return 0; | |
7402 | } | |
7403 | ||
7404 | /* Patch an invoke expression METHOD and ARGS, based on its invocation | |
7405 | mode. */ | |
7406 | ||
7407 | static tree | |
89e09b9a | 7408 | patch_invoke (patch, method, args) |
e04a16fb | 7409 | tree patch, method, args; |
e04a16fb AG |
7410 | { |
7411 | tree dtable, func; | |
0a2138e2 | 7412 | tree original_call, t, ta; |
e04a16fb | 7413 | |
5e942c50 APB |
7414 | /* Last step for args: convert build-in types. If we're dealing with |
7415 | a new TYPE() type call, the first argument to the constructor | |
7416 | isn't found in the incomming argument list, but delivered by | |
7417 | `new' */ | |
7418 | t = TYPE_ARG_TYPES (TREE_TYPE (method)); | |
7419 | if (TREE_CODE (patch) == NEW_CLASS_EXPR) | |
7420 | t = TREE_CHAIN (t); | |
ac825856 APB |
7421 | for (ta = args; t != end_params_node && ta; |
7422 | t = TREE_CHAIN (t), ta = TREE_CHAIN (ta)) | |
b9f7e36c APB |
7423 | if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) && |
7424 | TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t)) | |
7425 | TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta)); | |
1a6d4fb7 APB |
7426 | |
7427 | /* Resolve unresolved returned type isses */ | |
7428 | t = TREE_TYPE (TREE_TYPE (method)); | |
7429 | if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t))) | |
7430 | resolve_and_layout (TREE_TYPE (t), NULL); | |
22eed1e6 | 7431 | |
e8fc7396 | 7432 | if (flag_emit_class_files || flag_emit_xref) |
15fdcfe9 PB |
7433 | func = method; |
7434 | else | |
e04a16fb | 7435 | { |
15fdcfe9 | 7436 | tree signature = build_java_signature (TREE_TYPE (method)); |
89e09b9a | 7437 | switch (invocation_mode (method, CALL_USING_SUPER (patch))) |
15fdcfe9 PB |
7438 | { |
7439 | case INVOKE_VIRTUAL: | |
7440 | dtable = invoke_build_dtable (0, args); | |
7441 | func = build_invokevirtual (dtable, method); | |
7442 | break; | |
b9f7e36c | 7443 | |
15fdcfe9 PB |
7444 | case INVOKE_SUPER: |
7445 | case INVOKE_STATIC: | |
7446 | func = build_known_method_ref (method, TREE_TYPE (method), | |
7447 | DECL_CONTEXT (method), | |
7448 | signature, args); | |
7449 | break; | |
e04a16fb | 7450 | |
15fdcfe9 PB |
7451 | case INVOKE_INTERFACE: |
7452 | dtable = invoke_build_dtable (1, args); | |
7453 | func = build_invokeinterface (dtable, DECL_NAME (method), signature); | |
7454 | break; | |
5e942c50 | 7455 | |
15fdcfe9 | 7456 | default: |
89e09b9a | 7457 | fatal ("internal error - unknown invocation_mode result"); |
15fdcfe9 PB |
7458 | } |
7459 | ||
7460 | /* Ensure self_type is initialized, (invokestatic). FIXME */ | |
7461 | func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func); | |
e04a16fb AG |
7462 | } |
7463 | ||
e04a16fb AG |
7464 | TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method)); |
7465 | TREE_OPERAND (patch, 0) = func; | |
7466 | TREE_OPERAND (patch, 1) = args; | |
7467 | original_call = patch; | |
7468 | ||
22eed1e6 APB |
7469 | /* We're processing a `new TYPE ()' form. New is called an its |
7470 | returned value is the first argument to the constructor. We build | |
7471 | a COMPOUND_EXPR and use saved expression so that the overall NEW | |
7472 | expression value is a pointer to a newly created and initialized | |
7473 | class. */ | |
7474 | if (TREE_CODE (original_call) == NEW_CLASS_EXPR) | |
e04a16fb AG |
7475 | { |
7476 | tree class = DECL_CONTEXT (method); | |
7477 | tree c1, saved_new, size, new; | |
e8fc7396 | 7478 | if (flag_emit_class_files || flag_emit_xref) |
15fdcfe9 PB |
7479 | { |
7480 | TREE_TYPE (patch) = build_pointer_type (class); | |
7481 | return patch; | |
7482 | } | |
e04a16fb AG |
7483 | if (!TYPE_SIZE (class)) |
7484 | safe_layout_class (class); | |
7485 | size = size_in_bytes (class); | |
7486 | new = build (CALL_EXPR, promote_type (class), | |
7487 | build_address_of (alloc_object_node), | |
7488 | tree_cons (NULL_TREE, build_class_ref (class), | |
7489 | build_tree_list (NULL_TREE, | |
7490 | size_in_bytes (class))), | |
7491 | NULL_TREE); | |
7492 | saved_new = save_expr (new); | |
7493 | c1 = build_tree_list (NULL_TREE, saved_new); | |
7494 | TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1); | |
7495 | TREE_OPERAND (original_call, 1) = c1; | |
7496 | TREE_SET_CODE (original_call, CALL_EXPR); | |
7497 | patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new); | |
7498 | } | |
7499 | return patch; | |
7500 | } | |
7501 | ||
7502 | static int | |
7503 | invocation_mode (method, super) | |
7504 | tree method; | |
7505 | int super; | |
7506 | { | |
7507 | int access = get_access_flags_from_decl (method); | |
7508 | ||
22eed1e6 APB |
7509 | if (super) |
7510 | return INVOKE_SUPER; | |
7511 | ||
82371d41 | 7512 | if (access & ACC_STATIC || access & ACC_FINAL || access & ACC_PRIVATE) |
e04a16fb AG |
7513 | return INVOKE_STATIC; |
7514 | ||
7515 | if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method)))) | |
7516 | return INVOKE_STATIC; | |
7517 | ||
e04a16fb AG |
7518 | if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))) |
7519 | return INVOKE_INTERFACE; | |
7520 | ||
7521 | if (DECL_CONSTRUCTOR_P (method)) | |
7522 | return INVOKE_STATIC; | |
22eed1e6 | 7523 | |
e04a16fb AG |
7524 | return INVOKE_VIRTUAL; |
7525 | } | |
7526 | ||
b67d701b PB |
7527 | /* Retrieve a refined list of matching methods. It covers the step |
7528 | 15.11.2 (Compile-Time Step 2) */ | |
e04a16fb AG |
7529 | |
7530 | static tree | |
7531 | lookup_method_invoke (lc, cl, class, name, arg_list) | |
7532 | int lc; | |
7533 | tree cl; | |
7534 | tree class, name, arg_list; | |
7535 | { | |
de4c7b02 | 7536 | tree atl = end_params_node; /* Arg Type List */ |
c877974e | 7537 | tree method, signature, list, node; |
49f48c71 | 7538 | const char *candidates; /* Used for error report */ |
e04a16fb | 7539 | |
5e942c50 | 7540 | /* Fix the arguments */ |
e04a16fb AG |
7541 | for (node = arg_list; node; node = TREE_CHAIN (node)) |
7542 | { | |
e3884b71 | 7543 | tree current_arg = TREE_TYPE (TREE_VALUE (node)); |
c877974e | 7544 | /* Non primitive type may have to be resolved */ |
e3884b71 | 7545 | if (!JPRIMITIVE_TYPE_P (current_arg)) |
c877974e APB |
7546 | resolve_and_layout (current_arg, NULL_TREE); |
7547 | /* And promoted */ | |
b67d701b | 7548 | if (TREE_CODE (current_arg) == RECORD_TYPE) |
c877974e | 7549 | current_arg = promote_type (current_arg); |
5e942c50 | 7550 | atl = tree_cons (NULL_TREE, current_arg, atl); |
e04a16fb | 7551 | } |
e04a16fb | 7552 | |
5e942c50 APB |
7553 | /* Find all candidates and then refine the list, searching for the |
7554 | most specific method. */ | |
7555 | list = find_applicable_accessible_methods_list (lc, class, name, atl); | |
7556 | list = find_most_specific_methods_list (list); | |
b67d701b PB |
7557 | if (list && !TREE_CHAIN (list)) |
7558 | return TREE_VALUE (list); | |
e04a16fb | 7559 | |
b67d701b PB |
7560 | /* Issue an error. List candidates if any. Candidates are listed |
7561 | only if accessible (non accessible methods may end-up here for | |
7562 | the sake of a better error report). */ | |
7563 | candidates = NULL; | |
7564 | if (list) | |
e04a16fb | 7565 | { |
e04a16fb | 7566 | tree current; |
b67d701b | 7567 | obstack_grow (&temporary_obstack, ". Candidates are:\n", 18); |
e04a16fb AG |
7568 | for (current = list; current; current = TREE_CHAIN (current)) |
7569 | { | |
b67d701b PB |
7570 | tree cm = TREE_VALUE (current); |
7571 | char string [4096]; | |
7572 | if (!cm || not_accessible_p (class, cm, 0)) | |
7573 | continue; | |
b67d701b | 7574 | sprintf |
22eed1e6 APB |
7575 | (string, " `%s' in `%s'%s", |
7576 | get_printable_method_name (cm), | |
b67d701b PB |
7577 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))), |
7578 | (TREE_CHAIN (current) ? "\n" : "")); | |
7579 | obstack_grow (&temporary_obstack, string, strlen (string)); | |
7580 | } | |
7581 | obstack_1grow (&temporary_obstack, '\0'); | |
7582 | candidates = obstack_finish (&temporary_obstack); | |
7583 | } | |
7584 | /* Issue the error message */ | |
c877974e APB |
7585 | method = make_node (FUNCTION_TYPE); |
7586 | TYPE_ARG_TYPES (method) = atl; | |
b67d701b | 7587 | signature = build_java_argument_signature (method); |
22eed1e6 APB |
7588 | parse_error_context (cl, "Can't find %s `%s(%s)' in class `%s'%s", |
7589 | (lc ? "constructor" : "method"), | |
7590 | (lc ? | |
d77613be | 7591 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))) : |
22eed1e6 | 7592 | IDENTIFIER_POINTER (name)), |
b67d701b PB |
7593 | IDENTIFIER_POINTER (signature), |
7594 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))), | |
7595 | (candidates ? candidates : "")); | |
7596 | return NULL_TREE; | |
7597 | } | |
7598 | ||
5e942c50 APB |
7599 | /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1 |
7600 | when we're looking for a constructor. */ | |
b67d701b PB |
7601 | |
7602 | static tree | |
5e942c50 APB |
7603 | find_applicable_accessible_methods_list (lc, class, name, arglist) |
7604 | int lc; | |
b67d701b PB |
7605 | tree class, name, arglist; |
7606 | { | |
b67d701b PB |
7607 | tree list = NULL_TREE, all_list = NULL_TREE; |
7608 | ||
1982388a | 7609 | /* Search interfaces */ |
f4e16489 APB |
7610 | if (CLASS_INTERFACE (TYPE_NAME (class)) |
7611 | || CLASS_ABSTRACT (TYPE_NAME (class))) | |
b67d701b | 7612 | { |
de0b553f APB |
7613 | static tree searched_interfaces = NULL_TREE; |
7614 | static int search_not_done = 0; | |
1982388a APB |
7615 | int i, n; |
7616 | tree basetype_vec = TYPE_BINFO_BASETYPES (class); | |
7617 | ||
de0b553f APB |
7618 | /* Have we searched this interface already? */ |
7619 | if (searched_interfaces) | |
7620 | { | |
7621 | tree current; | |
7622 | for (current = searched_interfaces; | |
7623 | current; current = TREE_CHAIN (current)) | |
7624 | if (TREE_VALUE (current) == class) | |
7625 | return NULL; | |
7626 | } | |
7627 | searched_interfaces = tree_cons (NULL_TREE, class, searched_interfaces); | |
7628 | ||
1982388a APB |
7629 | search_applicable_methods_list |
7630 | (lc, TYPE_METHODS (class), name, arglist, &list, &all_list); | |
7631 | ||
7632 | n = TREE_VEC_LENGTH (basetype_vec); | |
7633 | for (i = 0; i < n; i++) | |
b67d701b | 7634 | { |
de0b553f APB |
7635 | tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)); |
7636 | tree rlist; | |
7637 | ||
7638 | /* Skip java.lang.Object (we'll search it once later.) */ | |
7639 | if (t == object_type_node) | |
7640 | continue; | |
7641 | ||
7642 | search_not_done++; | |
7643 | rlist = find_applicable_accessible_methods_list (lc, t, name, | |
7644 | arglist); | |
1982388a | 7645 | all_list = chainon (rlist, (list ? list : all_list)); |
de0b553f APB |
7646 | search_not_done--; |
7647 | } | |
7648 | ||
7649 | /* We're done. Reset the searched interfaces list and finally search | |
7650 | java.lang.Object */ | |
7651 | if (!search_not_done) | |
7652 | { | |
7653 | searched_interfaces = NULL_TREE; | |
7654 | search_applicable_methods_list (lc, TYPE_METHODS (object_type_node), | |
7655 | name, arglist, &list, &all_list); | |
e04a16fb | 7656 | } |
e04a16fb | 7657 | } |
1982388a APB |
7658 | /* Search classes */ |
7659 | else | |
7660 | while (class != NULL_TREE) | |
7661 | { | |
7662 | search_applicable_methods_list | |
7663 | (lc, TYPE_METHODS (class), name, arglist, &list, &all_list); | |
7664 | class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class)); | |
7665 | } | |
7666 | ||
b67d701b PB |
7667 | /* Either return the list obtained or all selected (but |
7668 | inaccessible) methods for better error report. */ | |
7669 | return (!list ? all_list : list); | |
7670 | } | |
e04a16fb | 7671 | |
1982388a APB |
7672 | /* Effectively search for the approriate method in method */ |
7673 | ||
7674 | static void | |
7675 | search_applicable_methods_list(lc, method, name, arglist, list, all_list) | |
7676 | int lc; | |
7677 | tree method, name, arglist; | |
7678 | tree *list, *all_list; | |
7679 | { | |
7680 | for (; method; method = TREE_CHAIN (method)) | |
7681 | { | |
7682 | /* When dealing with constructor, stop here, otherwise search | |
7683 | other classes */ | |
7684 | if (lc && !DECL_CONSTRUCTOR_P (method)) | |
7685 | continue; | |
7686 | else if (!lc && (DECL_CONSTRUCTOR_P (method) | |
7687 | || (GET_METHOD_NAME (method) != name))) | |
7688 | continue; | |
7689 | ||
7690 | if (argument_types_convertible (method, arglist)) | |
7691 | { | |
7692 | /* Retain accessible methods only */ | |
7693 | if (!not_accessible_p (DECL_CONTEXT (current_function_decl), | |
7694 | method, 0)) | |
7695 | *list = tree_cons (NULL_TREE, method, *list); | |
7696 | else | |
7697 | /* Also retain all selected method here */ | |
7698 | *all_list = tree_cons (NULL_TREE, method, *list); | |
7699 | } | |
7700 | } | |
7701 | } | |
7702 | ||
b67d701b PB |
7703 | /* 15.11.2.2 Choose the Most Specific Method */ |
7704 | ||
7705 | static tree | |
7706 | find_most_specific_methods_list (list) | |
7707 | tree list; | |
7708 | { | |
7709 | int max = 0; | |
7710 | tree current, new_list = NULL_TREE; | |
7711 | for (current = list; current; current = TREE_CHAIN (current)) | |
e04a16fb | 7712 | { |
b67d701b PB |
7713 | tree method; |
7714 | DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0; | |
7715 | ||
7716 | for (method = list; method; method = TREE_CHAIN (method)) | |
7717 | { | |
7718 | /* Don't test a method against itself */ | |
7719 | if (method == current) | |
7720 | continue; | |
7721 | ||
7722 | /* Compare arguments and location where method where declared */ | |
7723 | if (argument_types_convertible (TREE_VALUE (method), | |
7724 | TREE_VALUE (current)) | |
7725 | && valid_method_invocation_conversion_p | |
7726 | (DECL_CONTEXT (TREE_VALUE (method)), | |
7727 | DECL_CONTEXT (TREE_VALUE (current)))) | |
7728 | { | |
7729 | int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current)); | |
7730 | max = (v > max ? v : max); | |
7731 | } | |
7732 | } | |
e04a16fb AG |
7733 | } |
7734 | ||
b67d701b PB |
7735 | /* Review the list and select the maximally specific methods */ |
7736 | for (current = list; current; current = TREE_CHAIN (current)) | |
7737 | if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max) | |
7738 | new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list); | |
7739 | ||
7740 | /* If we can't find one, lower expectations and try to gather multiple | |
7741 | maximally specific methods */ | |
7742 | while (!new_list) | |
7743 | { | |
7744 | while (--max > 0) | |
7745 | { | |
7746 | if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max) | |
7747 | new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list); | |
7748 | } | |
7749 | return new_list; | |
7750 | } | |
7751 | ||
7752 | return new_list; | |
e04a16fb AG |
7753 | } |
7754 | ||
b67d701b PB |
7755 | /* Make sure that the type of each M2_OR_ARGLIST arguments can be |
7756 | converted by method invocation conversion (5.3) to the type of the | |
7757 | corresponding parameter of M1. Implementation expects M2_OR_ARGLIST | |
7758 | to change less often than M1. */ | |
e04a16fb | 7759 | |
b67d701b PB |
7760 | static int |
7761 | argument_types_convertible (m1, m2_or_arglist) | |
7762 | tree m1, m2_or_arglist; | |
e04a16fb | 7763 | { |
b67d701b PB |
7764 | static tree m2_arg_value = NULL_TREE; |
7765 | static tree m2_arg_cache = NULL_TREE; | |
e04a16fb | 7766 | |
b67d701b | 7767 | register tree m1_arg, m2_arg; |
e04a16fb | 7768 | |
b67d701b PB |
7769 | m1_arg = TYPE_ARG_TYPES (TREE_TYPE (m1)); |
7770 | if (!METHOD_STATIC (m1)) | |
7771 | m1_arg = TREE_CHAIN (m1_arg); | |
e04a16fb | 7772 | |
b67d701b PB |
7773 | if (m2_arg_value == m2_or_arglist) |
7774 | m2_arg = m2_arg_cache; | |
7775 | else | |
7776 | { | |
7777 | /* M2_OR_ARGLIST can be a function DECL or a raw list of | |
7778 | argument types */ | |
7779 | if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL) | |
7780 | { | |
7781 | m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist)); | |
7782 | if (!METHOD_STATIC (m2_or_arglist)) | |
7783 | m2_arg = TREE_CHAIN (m2_arg); | |
7784 | } | |
7785 | else | |
7786 | m2_arg = m2_or_arglist; | |
e04a16fb | 7787 | |
b67d701b PB |
7788 | m2_arg_value = m2_or_arglist; |
7789 | m2_arg_cache = m2_arg; | |
7790 | } | |
e04a16fb | 7791 | |
de4c7b02 | 7792 | while (m1_arg != end_params_node && m2_arg != end_params_node) |
b67d701b | 7793 | { |
c877974e | 7794 | resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE); |
b67d701b PB |
7795 | if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg), |
7796 | TREE_VALUE (m2_arg))) | |
7797 | break; | |
7798 | m1_arg = TREE_CHAIN (m1_arg); | |
7799 | m2_arg = TREE_CHAIN (m2_arg); | |
e04a16fb | 7800 | } |
de4c7b02 | 7801 | return m1_arg == end_params_node && m2_arg == end_params_node; |
e04a16fb AG |
7802 | } |
7803 | ||
7804 | /* Qualification routines */ | |
7805 | ||
7806 | static void | |
7807 | qualify_ambiguous_name (id) | |
7808 | tree id; | |
7809 | { | |
cd531a2e KG |
7810 | tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE, |
7811 | saved_current_class; | |
d8fccff5 | 7812 | int again, super_found = 0, this_found = 0, new_array_found = 0; |
8576f094 | 7813 | int code; |
e04a16fb AG |
7814 | |
7815 | /* We first qualify the first element, then derive qualification of | |
7816 | others based on the first one. If the first element is qualified | |
7817 | by a resolution (field or type), this resolution is stored in the | |
7818 | QUAL_RESOLUTION of the qual element being examined. We need to | |
7819 | save the current_class since the use of SUPER might change the | |
7820 | its value. */ | |
7821 | saved_current_class = current_class; | |
7822 | qual = EXPR_WFL_QUALIFICATION (id); | |
7823 | do { | |
7824 | ||
7825 | /* Simple qualified expression feature a qual_wfl that is a | |
7826 | WFL. Expression derived from a primary feature more complicated | |
7827 | things like a CALL_EXPR. Expression from primary need to be | |
7828 | worked out to extract the part on which the qualification will | |
7829 | take place. */ | |
7830 | qual_wfl = QUAL_WFL (qual); | |
7831 | switch (TREE_CODE (qual_wfl)) | |
7832 | { | |
7833 | case CALL_EXPR: | |
7834 | qual_wfl = TREE_OPERAND (qual_wfl, 0); | |
7835 | if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION) | |
7836 | { | |
7837 | qual = EXPR_WFL_QUALIFICATION (qual_wfl); | |
7838 | qual_wfl = QUAL_WFL (qual); | |
7839 | } | |
7840 | break; | |
d8fccff5 APB |
7841 | case NEW_ARRAY_EXPR: |
7842 | qual = TREE_CHAIN (qual); | |
1a6d4fb7 | 7843 | again = new_array_found = 1; |
d8fccff5 | 7844 | continue; |
b67d701b | 7845 | case NEW_CLASS_EXPR: |
e04a16fb | 7846 | case CONVERT_EXPR: |
e04a16fb AG |
7847 | qual_wfl = TREE_OPERAND (qual_wfl, 0); |
7848 | break; | |
c583dd46 APB |
7849 | case ARRAY_REF: |
7850 | while (TREE_CODE (qual_wfl) == ARRAY_REF) | |
7851 | qual_wfl = TREE_OPERAND (qual_wfl, 0); | |
7852 | break; | |
8576f094 APB |
7853 | case STRING_CST: |
7854 | qual = TREE_CHAIN (qual); | |
7855 | qual_wfl = QUAL_WFL (qual); | |
7856 | break; | |
0a2138e2 APB |
7857 | default: |
7858 | /* Fix for -Wall. Just break doing nothing */ | |
7859 | break; | |
e04a16fb | 7860 | } |
8576f094 | 7861 | |
e04a16fb AG |
7862 | ptr_type = current_class; |
7863 | again = 0; | |
8576f094 APB |
7864 | code = TREE_CODE (qual_wfl); |
7865 | ||
7866 | /* Pos evaluation: non WFL leading expression nodes */ | |
7867 | if (code == CONVERT_EXPR | |
7868 | && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION) | |
7869 | name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl)); | |
7870 | ||
ac22f9cb | 7871 | else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) && |
8576f094 APB |
7872 | TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION) |
7873 | name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0)); | |
7874 | ||
7875 | else if (code == STRING_CST || code == CONDITIONAL_EXPR) | |
7876 | { | |
7877 | qual = TREE_CHAIN (qual); | |
7878 | qual_wfl = QUAL_WFL (qual); | |
7879 | again = 1; | |
7880 | } | |
7881 | else | |
f441f671 APB |
7882 | { |
7883 | name = EXPR_WFL_NODE (qual_wfl); | |
7884 | if (!name) | |
7885 | { | |
7886 | qual = EXPR_WFL_QUALIFICATION (qual_wfl); | |
7887 | again = 1; | |
7888 | } | |
7889 | } | |
7890 | ||
e04a16fb AG |
7891 | /* If we have a THIS (from a primary), we set the context accordingly */ |
7892 | if (name == this_identifier_node) | |
7893 | { | |
7894 | qual = TREE_CHAIN (qual); | |
7895 | qual_wfl = QUAL_WFL (qual); | |
22eed1e6 APB |
7896 | if (TREE_CODE (qual_wfl) == CALL_EXPR) |
7897 | again = 1; | |
7898 | else | |
7899 | name = EXPR_WFL_NODE (qual_wfl); | |
e04a16fb AG |
7900 | this_found = 1; |
7901 | } | |
7902 | /* If we have a SUPER, we set the context accordingly */ | |
7903 | if (name == super_identifier_node) | |
7904 | { | |
7905 | current_class = CLASSTYPE_SUPER (ptr_type); | |
7906 | /* Check that there is such a thing as a super class. If not, | |
7907 | return. The error will be caught later on, during the | |
7908 | resolution */ | |
7909 | if (!current_class) | |
7910 | { | |
7911 | current_class = saved_current_class; | |
7912 | return; | |
7913 | } | |
7914 | qual = TREE_CHAIN (qual); | |
7915 | /* Do one more interation to set things up */ | |
7916 | super_found = again = 1; | |
7917 | } | |
7918 | } while (again); | |
7919 | ||
7920 | /* If name appears within the scope of a location variable | |
7921 | declaration or parameter declaration, then it is an expression | |
7922 | name. We don't carry this test out if we're in the context of the | |
7923 | use of SUPER or THIS */ | |
1a6d4fb7 APB |
7924 | if (!this_found && !super_found && |
7925 | TREE_CODE (name) != STRING_CST && (decl = IDENTIFIER_LOCAL_VALUE (name))) | |
e04a16fb AG |
7926 | { |
7927 | RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1; | |
7928 | QUAL_RESOLUTION (qual) = decl; | |
7929 | } | |
7930 | ||
7931 | /* If within the class/interface NAME was found to be used there | |
7932 | exists a (possibly inherited) field named NAME, then this is an | |
d8fccff5 APB |
7933 | expression name. If we saw a NEW_ARRAY_EXPR before and want to |
7934 | address length, it is OK. */ | |
7935 | else if ((decl = lookup_field_wrapper (ptr_type, name)) | |
7936 | || (new_array_found && name == length_identifier_node)) | |
e04a16fb AG |
7937 | { |
7938 | RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1; | |
d8fccff5 | 7939 | QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl); |
e04a16fb AG |
7940 | } |
7941 | ||
1a6d4fb7 | 7942 | /* We reclassify NAME as yielding to a type name resolution if: |
e04a16fb AG |
7943 | - NAME is a class/interface declared within the compilation |
7944 | unit containing NAME, | |
7945 | - NAME is imported via a single-type-import declaration, | |
7946 | - NAME is declared in an another compilation unit of the package | |
7947 | of the compilation unit containing NAME, | |
7948 | - NAME is declared by exactly on type-import-on-demand declaration | |
1a6d4fb7 APB |
7949 | of the compilation unit containing NAME. |
7950 | - NAME is actually a STRING_CST. */ | |
7951 | else if (TREE_CODE (name) == STRING_CST || | |
7952 | (decl = resolve_and_layout (name, NULL_TREE))) | |
e04a16fb AG |
7953 | { |
7954 | RESOLVE_TYPE_NAME_P (qual_wfl) = 1; | |
7955 | QUAL_RESOLUTION (qual) = decl; | |
7956 | } | |
7957 | ||
7958 | /* Method call are expression name */ | |
9bbc7d9f | 7959 | else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR |
8576f094 APB |
7960 | || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF |
7961 | || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR) | |
e04a16fb AG |
7962 | RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1; |
7963 | ||
7964 | /* Check here that NAME isn't declared by more than one | |
7965 | type-import-on-demand declaration of the compilation unit | |
7966 | containing NAME. FIXME */ | |
7967 | ||
7968 | /* Otherwise, NAME is reclassified as a package name */ | |
7969 | else | |
7970 | RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1; | |
7971 | ||
7972 | /* Propagate the qualification accross other components of the | |
7973 | qualified name */ | |
7974 | for (qual = TREE_CHAIN (qual); qual; | |
7975 | qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual)) | |
7976 | { | |
7977 | if (RESOLVE_PACKAGE_NAME_P (qual_wfl)) | |
7978 | RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1; | |
7979 | else | |
7980 | RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1; | |
7981 | } | |
7982 | ||
7983 | /* Store the global qualification for the ambiguous part of ID back | |
7984 | into ID fields */ | |
7985 | if (RESOLVE_EXPRESSION_NAME_P (qual_wfl)) | |
7986 | RESOLVE_EXPRESSION_NAME_P (id) = 1; | |
7987 | else if (RESOLVE_TYPE_NAME_P (qual_wfl)) | |
7988 | RESOLVE_TYPE_NAME_P (id) = 1; | |
7989 | else if (RESOLVE_PACKAGE_NAME_P (qual_wfl)) | |
7990 | RESOLVE_PACKAGE_NAME_P (id) = 1; | |
7991 | ||
7992 | /* Restore the current class */ | |
7993 | current_class = saved_current_class; | |
7994 | } | |
7995 | ||
7996 | static int | |
7997 | breakdown_qualified (left, right, source) | |
7998 | tree *left, *right, source; | |
7999 | { | |
8000 | char *p = IDENTIFIER_POINTER (source), *base; | |
8001 | int l = IDENTIFIER_LENGTH (source); | |
8002 | ||
8003 | /* Breakdown NAME into REMAINDER . IDENTIFIER */ | |
8004 | base = p; | |
8005 | p += (l-1); | |
8006 | while (*p != '.' && p != base) | |
8007 | p--; | |
8008 | ||
8009 | /* We didn't find a '.'. Return an error */ | |
8010 | if (p == base) | |
8011 | return 1; | |
8012 | ||
8013 | *p = '\0'; | |
8014 | if (right) | |
8015 | *right = get_identifier (p+1); | |
8016 | *left = get_identifier (IDENTIFIER_POINTER (source)); | |
8017 | *p = '.'; | |
8018 | ||
8019 | return 0; | |
8020 | } | |
8021 | ||
e04a16fb | 8022 | /* Patch tree nodes in a function body. When a BLOCK is found, push |
5b09b33e PB |
8023 | local variable decls if present. |
8024 | Same as java_complete_lhs, but does resolve static finals to values. */ | |
e04a16fb AG |
8025 | |
8026 | static tree | |
8027 | java_complete_tree (node) | |
8028 | tree node; | |
5b09b33e PB |
8029 | { |
8030 | node = java_complete_lhs (node); | |
8031 | if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node) | |
7f10c2e2 APB |
8032 | && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE |
8033 | && !flag_emit_xref) | |
5b09b33e PB |
8034 | { |
8035 | tree value = DECL_INITIAL (node); | |
8036 | DECL_INITIAL (node) = NULL_TREE; | |
100f7cd8 | 8037 | push_obstacks (&permanent_obstack, &permanent_obstack); |
5b09b33e | 8038 | value = fold_constant_for_init (value, node); |
100f7cd8 | 8039 | pop_obstacks (); |
5b09b33e PB |
8040 | DECL_INITIAL (node) = value; |
8041 | if (value != NULL_TREE) | |
8042 | return value; | |
8043 | } | |
8044 | return node; | |
8045 | } | |
8046 | ||
2aa11e97 APB |
8047 | static tree |
8048 | java_stabilize_reference (node) | |
8049 | tree node; | |
8050 | { | |
8051 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
8052 | { | |
8053 | tree op0 = TREE_OPERAND (node, 0); | |
8054 | tree op1 = TREE_OPERAND (node, 1); | |
642f15d1 | 8055 | TREE_OPERAND (node, 0) = save_expr (op0); |
2aa11e97 APB |
8056 | TREE_OPERAND (node, 1) = java_stabilize_reference (op1); |
8057 | return node; | |
8058 | } | |
5cbdba64 | 8059 | return stabilize_reference (node); |
2aa11e97 APB |
8060 | } |
8061 | ||
5b09b33e PB |
8062 | /* Patch tree nodes in a function body. When a BLOCK is found, push |
8063 | local variable decls if present. | |
8064 | Same as java_complete_tree, but does not resolve static finals to values. */ | |
8065 | ||
8066 | static tree | |
8067 | java_complete_lhs (node) | |
8068 | tree node; | |
e04a16fb | 8069 | { |
22eed1e6 | 8070 | tree nn, cn, wfl_op1, wfl_op2, wfl_op3; |
b67d701b | 8071 | int flag; |
e04a16fb AG |
8072 | |
8073 | /* CONVERT_EXPR always has its type set, even though it needs to be | |
b67d701b | 8074 | worked out. */ |
e04a16fb AG |
8075 | if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR) |
8076 | return node; | |
8077 | ||
8078 | /* The switch block implements cases processing container nodes | |
8079 | first. Contained nodes are always written back. Leaves come | |
8080 | next and return a value. */ | |
8081 | switch (TREE_CODE (node)) | |
8082 | { | |
8083 | case BLOCK: | |
8084 | ||
8085 | /* 1- Block section. | |
8086 | Set the local values on decl names so we can identify them | |
8087 | faster when they're referenced. At that stage, identifiers | |
8088 | are legal so we don't check for declaration errors. */ | |
8089 | for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn)) | |
8090 | { | |
8091 | DECL_CONTEXT (cn) = current_function_decl; | |
8092 | IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn; | |
e04a16fb | 8093 | } |
15fdcfe9 PB |
8094 | if (BLOCK_EXPR_BODY (node) == NULL_TREE) |
8095 | CAN_COMPLETE_NORMALLY (node) = 1; | |
8096 | else | |
e04a16fb | 8097 | { |
15fdcfe9 PB |
8098 | tree stmt = BLOCK_EXPR_BODY (node); |
8099 | tree *ptr; | |
8100 | int error_seen = 0; | |
8101 | if (TREE_CODE (stmt) == COMPOUND_EXPR) | |
8102 | { | |
c877974e APB |
8103 | /* Re-order from (((A; B); C); ...; Z) to |
8104 | (A; (B; (C ; (...; Z)))). | |
15fdcfe9 PB |
8105 | This makes it easier to scan the statements left-to-right |
8106 | without using recursion (which might overflow the stack | |
8107 | if the block has many statements. */ | |
8108 | for (;;) | |
8109 | { | |
8110 | tree left = TREE_OPERAND (stmt, 0); | |
8111 | if (TREE_CODE (left) != COMPOUND_EXPR) | |
8112 | break; | |
8113 | TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1); | |
8114 | TREE_OPERAND (left, 1) = stmt; | |
8115 | stmt = left; | |
8116 | } | |
8117 | BLOCK_EXPR_BODY (node) = stmt; | |
8118 | } | |
8119 | ||
c877974e APB |
8120 | /* Now do the actual complete, without deep recursion for |
8121 | long blocks. */ | |
15fdcfe9 | 8122 | ptr = &BLOCK_EXPR_BODY (node); |
dc0b3eff PB |
8123 | while (TREE_CODE (*ptr) == COMPOUND_EXPR |
8124 | && TREE_OPERAND (*ptr, 1) != empty_stmt_node) | |
15fdcfe9 PB |
8125 | { |
8126 | tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0)); | |
8127 | tree *next = &TREE_OPERAND (*ptr, 1); | |
8128 | TREE_OPERAND (*ptr, 0) = cur; | |
cd9643f7 PB |
8129 | if (cur == empty_stmt_node) |
8130 | { | |
8131 | /* Optimization; makes it easier to detect empty bodies. | |
8132 | Most useful for <clinit> with all-constant initializer. */ | |
8133 | *ptr = *next; | |
8134 | continue; | |
8135 | } | |
15fdcfe9 PB |
8136 | if (TREE_CODE (cur) == ERROR_MARK) |
8137 | error_seen++; | |
8138 | else if (! CAN_COMPLETE_NORMALLY (cur)) | |
8139 | { | |
8140 | wfl_op2 = *next; | |
8141 | for (;;) | |
8142 | { | |
8143 | if (TREE_CODE (wfl_op2) == BLOCK) | |
8144 | wfl_op2 = BLOCK_EXPR_BODY (wfl_op2); | |
8145 | else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR) | |
8146 | wfl_op2 = TREE_OPERAND (wfl_op2, 0); | |
8147 | else | |
8148 | break; | |
8149 | } | |
8150 | if (TREE_CODE (wfl_op2) != CASE_EXPR | |
dc0b3eff | 8151 | && TREE_CODE (wfl_op2) != DEFAULT_EXPR) |
82371d41 | 8152 | unreachable_stmt_error (*ptr); |
15fdcfe9 PB |
8153 | } |
8154 | ptr = next; | |
8155 | } | |
8156 | *ptr = java_complete_tree (*ptr); | |
8157 | ||
8158 | if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0) | |
e04a16fb | 8159 | return error_mark_node; |
15fdcfe9 | 8160 | CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr); |
e04a16fb AG |
8161 | } |
8162 | /* Turn local bindings to null */ | |
8163 | for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn)) | |
8164 | IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE; | |
8165 | ||
8166 | TREE_TYPE (node) = void_type_node; | |
8167 | break; | |
8168 | ||
8169 | /* 2- They are expressions but ultimately deal with statements */ | |
b67d701b | 8170 | |
b9f7e36c APB |
8171 | case THROW_EXPR: |
8172 | wfl_op1 = TREE_OPERAND (node, 0); | |
8173 | COMPLETE_CHECK_OP_0 (node); | |
15fdcfe9 | 8174 | /* CAN_COMPLETE_NORMALLY (node) = 0; */ |
b9f7e36c APB |
8175 | return patch_throw_statement (node, wfl_op1); |
8176 | ||
8177 | case SYNCHRONIZED_EXPR: | |
8178 | wfl_op1 = TREE_OPERAND (node, 0); | |
b9f7e36c APB |
8179 | return patch_synchronized_statement (node, wfl_op1); |
8180 | ||
b67d701b PB |
8181 | case TRY_EXPR: |
8182 | return patch_try_statement (node); | |
8183 | ||
a7d8d81f PB |
8184 | case TRY_FINALLY_EXPR: |
8185 | COMPLETE_CHECK_OP_0 (node); | |
8186 | COMPLETE_CHECK_OP_1 (node); | |
8187 | CAN_COMPLETE_NORMALLY (node) | |
8188 | = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0)) | |
8189 | && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))); | |
8190 | TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0)); | |
8191 | return node; | |
8192 | ||
5a005d9e PB |
8193 | case CLEANUP_POINT_EXPR: |
8194 | COMPLETE_CHECK_OP_0 (node); | |
8195 | TREE_TYPE (node) = void_type_node; | |
2aa11e97 APB |
8196 | CAN_COMPLETE_NORMALLY (node) = |
8197 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0)); | |
5a005d9e PB |
8198 | return node; |
8199 | ||
8200 | case WITH_CLEANUP_EXPR: | |
8201 | COMPLETE_CHECK_OP_0 (node); | |
8202 | COMPLETE_CHECK_OP_2 (node); | |
2aa11e97 APB |
8203 | CAN_COMPLETE_NORMALLY (node) = |
8204 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0)); | |
5a005d9e PB |
8205 | TREE_TYPE (node) = void_type_node; |
8206 | return node; | |
8207 | ||
e04a16fb AG |
8208 | case LABELED_BLOCK_EXPR: |
8209 | PUSH_LABELED_BLOCK (node); | |
8210 | if (LABELED_BLOCK_BODY (node)) | |
8211 | COMPLETE_CHECK_OP_1 (node); | |
8212 | TREE_TYPE (node) = void_type_node; | |
8213 | POP_LABELED_BLOCK (); | |
1fb89a4d APB |
8214 | |
8215 | if (LABELED_BLOCK_BODY (node) == empty_stmt_node) | |
9dd939b2 APB |
8216 | { |
8217 | LABELED_BLOCK_BODY (node) = NULL_TREE; | |
8218 | CAN_COMPLETE_NORMALLY (node) = 1; | |
8219 | } | |
1fb89a4d | 8220 | else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node))) |
15fdcfe9 | 8221 | CAN_COMPLETE_NORMALLY (node) = 1; |
e04a16fb AG |
8222 | return node; |
8223 | ||
8224 | case EXIT_BLOCK_EXPR: | |
8225 | /* We don't complete operand 1, because it's the return value of | |
8226 | the EXIT_BLOCK_EXPR which doesn't exist it Java */ | |
8227 | return patch_bc_statement (node); | |
8228 | ||
15fdcfe9 PB |
8229 | case CASE_EXPR: |
8230 | cn = java_complete_tree (TREE_OPERAND (node, 0)); | |
8231 | if (cn == error_mark_node) | |
8232 | return cn; | |
8233 | ||
8576f094 APB |
8234 | /* First, the case expression must be constant. Values of final |
8235 | fields are accepted. */ | |
15fdcfe9 | 8236 | cn = fold (cn); |
8576f094 APB |
8237 | if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF) |
8238 | && JDECL_P (TREE_OPERAND (cn, 1)) | |
8239 | && FIELD_FINAL (TREE_OPERAND (cn, 1)) | |
8240 | && DECL_INITIAL (TREE_OPERAND (cn, 1))) | |
100f7cd8 APB |
8241 | { |
8242 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
8243 | cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)), | |
8244 | TREE_OPERAND (cn, 1)); | |
8245 | pop_obstacks (); | |
8246 | } | |
15fdcfe9 | 8247 | |
ce6e9147 | 8248 | if (!TREE_CONSTANT (cn) && !flag_emit_xref) |
15fdcfe9 PB |
8249 | { |
8250 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
8251 | parse_error_context (node, "Constant expression required"); | |
8252 | return error_mark_node; | |
8253 | } | |
8254 | ||
8255 | nn = ctxp->current_loop; | |
8256 | ||
8257 | /* It must be assignable to the type of the switch expression. */ | |
c877974e APB |
8258 | if (!try_builtin_assignconv (NULL_TREE, |
8259 | TREE_TYPE (TREE_OPERAND (nn, 0)), cn)) | |
15fdcfe9 PB |
8260 | { |
8261 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
8262 | parse_error_context | |
8263 | (wfl_operator, | |
8264 | "Incompatible type for case. Can't convert `%s' to `int'", | |
8265 | lang_printable_name (TREE_TYPE (cn), 0)); | |
8266 | return error_mark_node; | |
8267 | } | |
8268 | ||
8269 | cn = fold (convert (int_type_node, cn)); | |
8270 | ||
8271 | /* Multiple instance of a case label bearing the same | |
8272 | value is checked during code generation. The case | |
8273 | expression is allright so far. */ | |
8274 | TREE_OPERAND (node, 0) = cn; | |
9bbc7d9f | 8275 | TREE_TYPE (node) = void_type_node; |
15fdcfe9 | 8276 | CAN_COMPLETE_NORMALLY (node) = 1; |
10100cc7 | 8277 | TREE_SIDE_EFFECTS (node) = 1; |
15fdcfe9 PB |
8278 | break; |
8279 | ||
8280 | case DEFAULT_EXPR: | |
8281 | nn = ctxp->current_loop; | |
8282 | /* Only one default label is allowed per switch statement */ | |
8283 | if (SWITCH_HAS_DEFAULT (nn)) | |
8284 | { | |
8285 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
8286 | parse_error_context (wfl_operator, | |
8287 | "Duplicate case label: `default'"); | |
8288 | return error_mark_node; | |
8289 | } | |
8290 | else | |
8291 | SWITCH_HAS_DEFAULT (nn) = 1; | |
9bbc7d9f | 8292 | TREE_TYPE (node) = void_type_node; |
10100cc7 | 8293 | TREE_SIDE_EFFECTS (node) = 1; |
15fdcfe9 PB |
8294 | CAN_COMPLETE_NORMALLY (node) = 1; |
8295 | break; | |
8296 | ||
b67d701b | 8297 | case SWITCH_EXPR: |
e04a16fb AG |
8298 | case LOOP_EXPR: |
8299 | PUSH_LOOP (node); | |
8300 | /* Check whether the loop was enclosed in a labeled | |
8301 | statement. If not, create one, insert the loop in it and | |
8302 | return the node */ | |
8303 | nn = patch_loop_statement (node); | |
b67d701b | 8304 | |
e04a16fb | 8305 | /* Anyways, walk the body of the loop */ |
b67d701b PB |
8306 | if (TREE_CODE (node) == LOOP_EXPR) |
8307 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); | |
8308 | /* Switch statement: walk the switch expression and the cases */ | |
8309 | else | |
8310 | node = patch_switch_statement (node); | |
8311 | ||
e04a16fb | 8312 | if (TREE_OPERAND (node, 0) == error_mark_node) |
b635eb2f PB |
8313 | nn = error_mark_node; |
8314 | else | |
15fdcfe9 | 8315 | { |
b635eb2f PB |
8316 | TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node; |
8317 | /* If we returned something different, that's because we | |
8318 | inserted a label. Pop the label too. */ | |
8319 | if (nn != node) | |
8320 | { | |
8321 | if (CAN_COMPLETE_NORMALLY (node)) | |
8322 | CAN_COMPLETE_NORMALLY (nn) = 1; | |
8323 | POP_LABELED_BLOCK (); | |
8324 | } | |
15fdcfe9 | 8325 | } |
e04a16fb AG |
8326 | POP_LOOP (); |
8327 | return nn; | |
8328 | ||
8329 | case EXIT_EXPR: | |
8330 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); | |
8331 | return patch_exit_expr (node); | |
8332 | ||
8333 | case COND_EXPR: | |
8334 | /* Condition */ | |
8335 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); | |
8336 | if (TREE_OPERAND (node, 0) == error_mark_node) | |
8337 | return error_mark_node; | |
8338 | /* then-else branches */ | |
8339 | TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1)); | |
8340 | if (TREE_OPERAND (node, 1) == error_mark_node) | |
8341 | return error_mark_node; | |
8342 | TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2)); | |
8343 | if (TREE_OPERAND (node, 2) == error_mark_node) | |
8344 | return error_mark_node; | |
8345 | return patch_if_else_statement (node); | |
8346 | break; | |
8347 | ||
22eed1e6 APB |
8348 | case CONDITIONAL_EXPR: |
8349 | /* Condition */ | |
8350 | wfl_op1 = TREE_OPERAND (node, 0); | |
8351 | COMPLETE_CHECK_OP_0 (node); | |
8352 | wfl_op2 = TREE_OPERAND (node, 1); | |
8353 | COMPLETE_CHECK_OP_1 (node); | |
8354 | wfl_op3 = TREE_OPERAND (node, 2); | |
8355 | COMPLETE_CHECK_OP_2 (node); | |
8356 | return patch_conditional_expr (node, wfl_op1, wfl_op2); | |
8357 | ||
e04a16fb AG |
8358 | /* 3- Expression section */ |
8359 | case COMPOUND_EXPR: | |
15fdcfe9 | 8360 | wfl_op2 = TREE_OPERAND (node, 1); |
ac825856 APB |
8361 | TREE_OPERAND (node, 0) = nn = |
8362 | java_complete_tree (TREE_OPERAND (node, 0)); | |
dc0b3eff PB |
8363 | if (wfl_op2 == empty_stmt_node) |
8364 | CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn); | |
8365 | else | |
15fdcfe9 | 8366 | { |
dc0b3eff | 8367 | if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK) |
bccaf73a | 8368 | { |
dc0b3eff PB |
8369 | /* An unreachable condition in a do-while statement |
8370 | is *not* (technically) an unreachable statement. */ | |
8371 | nn = wfl_op2; | |
8372 | if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION) | |
8373 | nn = EXPR_WFL_NODE (nn); | |
8374 | if (TREE_CODE (nn) != EXIT_EXPR) | |
8375 | { | |
8376 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op2); | |
8377 | parse_error_context (wfl_operator, "Unreachable statement"); | |
8378 | } | |
bccaf73a | 8379 | } |
dc0b3eff PB |
8380 | TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1)); |
8381 | if (TREE_OPERAND (node, 1) == error_mark_node) | |
8382 | return error_mark_node; | |
8383 | CAN_COMPLETE_NORMALLY (node) | |
8384 | = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)); | |
15fdcfe9 | 8385 | } |
e04a16fb AG |
8386 | TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1)); |
8387 | break; | |
8388 | ||
8389 | case RETURN_EXPR: | |
15fdcfe9 | 8390 | /* CAN_COMPLETE_NORMALLY (node) = 0; */ |
e04a16fb AG |
8391 | return patch_return (node); |
8392 | ||
8393 | case EXPR_WITH_FILE_LOCATION: | |
8394 | if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */ | |
8395 | || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE) | |
15fdcfe9 | 8396 | { |
5423609c | 8397 | tree wfl = node; |
15fdcfe9 | 8398 | node = resolve_expression_name (node, NULL); |
dc0b3eff PB |
8399 | if (node == error_mark_node) |
8400 | return node; | |
5423609c APB |
8401 | /* Keep line number information somewhere were it doesn't |
8402 | disrupt the completion process. */ | |
2c56429a | 8403 | if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR) |
5423609c APB |
8404 | { |
8405 | EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1); | |
8406 | TREE_OPERAND (node, 1) = wfl; | |
8407 | } | |
15fdcfe9 PB |
8408 | CAN_COMPLETE_NORMALLY (node) = 1; |
8409 | } | |
e04a16fb AG |
8410 | else |
8411 | { | |
5b09b33e PB |
8412 | tree body; |
8413 | int save_lineno = lineno; | |
8414 | lineno = EXPR_WFL_LINENO (node); | |
8415 | body = java_complete_tree (EXPR_WFL_NODE (node)); | |
8416 | lineno = save_lineno; | |
15fdcfe9 | 8417 | EXPR_WFL_NODE (node) = body; |
dc0b3eff | 8418 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body); |
15fdcfe9 | 8419 | CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body); |
cd9643f7 PB |
8420 | if (body == empty_stmt_node) |
8421 | { | |
8422 | /* Optimization; makes it easier to detect empty bodies. */ | |
8423 | return body; | |
8424 | } | |
dc0b3eff | 8425 | if (body == error_mark_node) |
e04a16fb AG |
8426 | { |
8427 | /* Its important for the evaluation of assignment that | |
8428 | this mark on the TREE_TYPE is propagated. */ | |
8429 | TREE_TYPE (node) = error_mark_node; | |
8430 | return error_mark_node; | |
8431 | } | |
8432 | else | |
8433 | TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node)); | |
15fdcfe9 | 8434 | |
e04a16fb AG |
8435 | } |
8436 | break; | |
8437 | ||
b67d701b | 8438 | case NEW_ARRAY_EXPR: |
e04a16fb AG |
8439 | /* Patch all the dimensions */ |
8440 | flag = 0; | |
8441 | for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn)) | |
8442 | { | |
8443 | int location = EXPR_WFL_LINECOL (TREE_VALUE (cn)); | |
8444 | tree dim = java_complete_tree (TREE_VALUE (cn)); | |
8445 | if (dim == error_mark_node) | |
8446 | { | |
8447 | flag = 1; | |
8448 | continue; | |
8449 | } | |
8450 | else | |
8451 | { | |
b9f7e36c | 8452 | TREE_VALUE (cn) = dim; |
e04a16fb AG |
8453 | /* Setup the location of the current dimension, for |
8454 | later error report. */ | |
8455 | TREE_PURPOSE (cn) = | |
8456 | build_expr_wfl (NULL_TREE, input_filename, 0, 0); | |
8457 | EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location; | |
8458 | } | |
8459 | } | |
8460 | /* They complete the array creation expression, if no errors | |
8461 | were found. */ | |
15fdcfe9 | 8462 | CAN_COMPLETE_NORMALLY (node) = 1; |
aee48ef8 PB |
8463 | return (flag ? error_mark_node |
8464 | : force_evaluation_order (patch_newarray (node))); | |
e04a16fb | 8465 | |
b67d701b | 8466 | case NEW_CLASS_EXPR: |
e04a16fb | 8467 | case CALL_EXPR: |
b67d701b | 8468 | /* Complete function's argument(s) first */ |
e04a16fb AG |
8469 | if (complete_function_arguments (node)) |
8470 | return error_mark_node; | |
8471 | else | |
b9f7e36c | 8472 | { |
22eed1e6 APB |
8473 | tree decl, wfl = TREE_OPERAND (node, 0); |
8474 | int in_this = CALL_THIS_CONSTRUCTOR_P (node); | |
8475 | ||
c877974e | 8476 | node = patch_method_invocation (node, NULL_TREE, |
89e09b9a | 8477 | NULL_TREE, 0, &decl); |
c877974e APB |
8478 | if (node == error_mark_node) |
8479 | return error_mark_node; | |
8480 | ||
8481 | check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl); | |
8482 | /* If we call this(...), register signature and positions */ | |
8483 | if (in_this) | |
8484 | DECL_CONSTRUCTOR_CALLS (current_function_decl) = | |
8485 | tree_cons (wfl, decl, | |
8486 | DECL_CONSTRUCTOR_CALLS (current_function_decl)); | |
de4c7b02 | 8487 | CAN_COMPLETE_NORMALLY (node) = 1; |
dc0b3eff | 8488 | return force_evaluation_order (node); |
b9f7e36c | 8489 | } |
e04a16fb AG |
8490 | |
8491 | case MODIFY_EXPR: | |
8492 | /* Save potential wfls */ | |
8493 | wfl_op1 = TREE_OPERAND (node, 0); | |
cd9643f7 PB |
8494 | TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1); |
8495 | if (MODIFY_EXPR_FROM_INITIALIZATION_P (node) | |
8496 | && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn) | |
8497 | && DECL_INITIAL (nn) != NULL_TREE) | |
8498 | { | |
100f7cd8 APB |
8499 | tree value; |
8500 | ||
8501 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
8502 | value = fold_constant_for_init (nn, nn); | |
8503 | pop_obstacks (); | |
cd9643f7 PB |
8504 | if (value != NULL_TREE) |
8505 | { | |
8506 | tree type = TREE_TYPE (value); | |
8507 | if (JPRIMITIVE_TYPE_P (type) || type == string_ptr_type_node) | |
8508 | return empty_stmt_node; | |
8509 | } | |
8510 | DECL_INITIAL (nn) = NULL_TREE; | |
8511 | } | |
e04a16fb | 8512 | wfl_op2 = TREE_OPERAND (node, 1); |
cd9643f7 | 8513 | |
e04a16fb AG |
8514 | if (TREE_OPERAND (node, 0) == error_mark_node) |
8515 | return error_mark_node; | |
8516 | ||
5cbdba64 APB |
8517 | flag = COMPOUND_ASSIGN_P (wfl_op2); |
8518 | if (flag) | |
e04a16fb | 8519 | { |
2aa11e97 | 8520 | tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0)); |
e04a16fb AG |
8521 | |
8522 | /* Hand stablize the lhs on both places */ | |
e04a16fb | 8523 | TREE_OPERAND (node, 0) = lvalue; |
5cbdba64 APB |
8524 | TREE_OPERAND (TREE_OPERAND (node, 1), 0) = |
8525 | (flag_emit_class_files ? lvalue : save_expr (lvalue)); | |
2aa11e97 | 8526 | |
5cbdba64 | 8527 | /* 15.25.2.a: Left hand is not an array access. FIXME */ |
2aa11e97 APB |
8528 | /* Now complete the RHS. We write it back later on. */ |
8529 | nn = java_complete_tree (TREE_OPERAND (node, 1)); | |
8530 | ||
642f15d1 APB |
8531 | if ((cn = patch_string (nn))) |
8532 | nn = cn; | |
8533 | ||
2aa11e97 APB |
8534 | /* The last part of the rewrite for E1 op= E2 is to have |
8535 | E1 = (T)(E1 op E2), with T being the type of E1. */ | |
642f15d1 APB |
8536 | nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2), |
8537 | TREE_TYPE (lvalue), nn)); | |
5cbdba64 APB |
8538 | |
8539 | /* 15.25.2.b: Left hand is an array access. FIXME */ | |
e04a16fb AG |
8540 | } |
8541 | ||
f8976021 APB |
8542 | /* If we're about to patch a NEW_ARRAY_INIT, we call a special |
8543 | function to complete this RHS */ | |
2aa11e97 | 8544 | else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT) |
fdec99c6 | 8545 | nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)), |
f8976021 | 8546 | TREE_OPERAND (node, 1)); |
2aa11e97 | 8547 | /* Otherwise we simply complete the RHS */ |
f8976021 APB |
8548 | else |
8549 | nn = java_complete_tree (TREE_OPERAND (node, 1)); | |
8550 | ||
e04a16fb | 8551 | if (nn == error_mark_node) |
c0d87ff6 | 8552 | return error_mark_node; |
2aa11e97 APB |
8553 | |
8554 | /* Write back the RHS as we evaluated it. */ | |
e04a16fb | 8555 | TREE_OPERAND (node, 1) = nn; |
b67d701b PB |
8556 | |
8557 | /* In case we're handling = with a String as a RHS, we need to | |
8558 | produce a String out of the RHS (it might still be a | |
8559 | STRING_CST or a StringBuffer at this stage */ | |
8560 | if ((nn = patch_string (TREE_OPERAND (node, 1)))) | |
8561 | TREE_OPERAND (node, 1) = nn; | |
15fdcfe9 | 8562 | node = patch_assignment (node, wfl_op1, wfl_op2); |
5cbdba64 APB |
8563 | /* Reorganize the tree if necessary. */ |
8564 | if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node)) | |
8565 | || JSTRING_P (TREE_TYPE (node)))) | |
8566 | node = java_refold (node); | |
15fdcfe9 PB |
8567 | CAN_COMPLETE_NORMALLY (node) = 1; |
8568 | return node; | |
e04a16fb AG |
8569 | |
8570 | case MULT_EXPR: | |
8571 | case PLUS_EXPR: | |
8572 | case MINUS_EXPR: | |
8573 | case LSHIFT_EXPR: | |
8574 | case RSHIFT_EXPR: | |
8575 | case URSHIFT_EXPR: | |
8576 | case BIT_AND_EXPR: | |
8577 | case BIT_XOR_EXPR: | |
8578 | case BIT_IOR_EXPR: | |
8579 | case TRUNC_MOD_EXPR: | |
8580 | case RDIV_EXPR: | |
8581 | case TRUTH_ANDIF_EXPR: | |
8582 | case TRUTH_ORIF_EXPR: | |
8583 | case EQ_EXPR: | |
8584 | case NE_EXPR: | |
8585 | case GT_EXPR: | |
8586 | case GE_EXPR: | |
8587 | case LT_EXPR: | |
8588 | case LE_EXPR: | |
8589 | /* Operands 0 and 1 are WFL in certain cases only. patch_binop | |
8590 | knows how to handle those cases. */ | |
8591 | wfl_op1 = TREE_OPERAND (node, 0); | |
8592 | wfl_op2 = TREE_OPERAND (node, 1); | |
b67d701b | 8593 | |
15fdcfe9 | 8594 | CAN_COMPLETE_NORMALLY (node) = 1; |
b67d701b PB |
8595 | /* Don't complete string nodes if dealing with the PLUS operand. */ |
8596 | if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1)) | |
2aa11e97 APB |
8597 | { |
8598 | nn = java_complete_tree (wfl_op1); | |
8599 | if (nn == error_mark_node) | |
8600 | return error_mark_node; | |
8601 | if ((cn = patch_string (nn))) | |
8602 | nn = cn; | |
8603 | TREE_OPERAND (node, 0) = nn; | |
8604 | } | |
b67d701b | 8605 | if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2)) |
2aa11e97 APB |
8606 | { |
8607 | nn = java_complete_tree (wfl_op2); | |
8608 | if (nn == error_mark_node) | |
8609 | return error_mark_node; | |
8610 | if ((cn = patch_string (nn))) | |
8611 | nn = cn; | |
8612 | TREE_OPERAND (node, 1) = nn; | |
8613 | } | |
dc0b3eff | 8614 | return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2)); |
e04a16fb | 8615 | |
5e942c50 APB |
8616 | case INSTANCEOF_EXPR: |
8617 | wfl_op1 = TREE_OPERAND (node, 0); | |
8618 | COMPLETE_CHECK_OP_0 (node); | |
ce6e9147 APB |
8619 | if (flag_emit_xref) |
8620 | { | |
8621 | TREE_TYPE (node) = boolean_type_node; | |
8622 | return node; | |
8623 | } | |
5e942c50 APB |
8624 | return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1)); |
8625 | ||
b67d701b | 8626 | case UNARY_PLUS_EXPR: |
e04a16fb AG |
8627 | case NEGATE_EXPR: |
8628 | case TRUTH_NOT_EXPR: | |
8629 | case BIT_NOT_EXPR: | |
8630 | case PREDECREMENT_EXPR: | |
8631 | case PREINCREMENT_EXPR: | |
8632 | case POSTDECREMENT_EXPR: | |
8633 | case POSTINCREMENT_EXPR: | |
8634 | case CONVERT_EXPR: | |
8635 | /* There are cases were wfl_op1 is a WFL. patch_unaryop knows | |
8636 | how to handle those cases. */ | |
8637 | wfl_op1 = TREE_OPERAND (node, 0); | |
15fdcfe9 | 8638 | CAN_COMPLETE_NORMALLY (node) = 1; |
e04a16fb AG |
8639 | TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1); |
8640 | if (TREE_OPERAND (node, 0) == error_mark_node) | |
8641 | return error_mark_node; | |
4a5f66c3 APB |
8642 | node = patch_unaryop (node, wfl_op1); |
8643 | CAN_COMPLETE_NORMALLY (node) = 1; | |
8644 | break; | |
e04a16fb AG |
8645 | |
8646 | case ARRAY_REF: | |
8647 | /* There are cases were wfl_op1 is a WFL. patch_array_ref knows | |
8648 | how to handle those cases. */ | |
8649 | wfl_op1 = TREE_OPERAND (node, 0); | |
8650 | TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1); | |
8651 | if (TREE_OPERAND (node, 0) == error_mark_node) | |
8652 | return error_mark_node; | |
7f1d4866 | 8653 | if (!flag_emit_class_files && !flag_emit_xref) |
b67d701b | 8654 | TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0)); |
e04a16fb AG |
8655 | /* The same applies to wfl_op2 */ |
8656 | wfl_op2 = TREE_OPERAND (node, 1); | |
8657 | TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2); | |
8658 | if (TREE_OPERAND (node, 1) == error_mark_node) | |
8659 | return error_mark_node; | |
7f1d4866 | 8660 | if (!flag_emit_class_files && !flag_emit_xref) |
22eed1e6 | 8661 | TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1)); |
939d7216 | 8662 | return patch_array_ref (node); |
e04a16fb | 8663 | |
63a212ed PB |
8664 | case RECORD_TYPE: |
8665 | return node;; | |
8666 | ||
8667 | case COMPONENT_REF: | |
8668 | /* The first step in the re-write of qualified name handling. FIXME. | |
8669 | So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */ | |
9bbc7d9f | 8670 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); |
63a212ed PB |
8671 | if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE) |
8672 | { | |
8673 | tree name = TREE_OPERAND (node, 1); | |
8674 | tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name); | |
8675 | if (field == NULL_TREE) | |
8676 | { | |
8677 | error ("missing static field `%s'", IDENTIFIER_POINTER (name)); | |
8678 | return error_mark_node; | |
8679 | } | |
8680 | if (! FIELD_STATIC (field)) | |
8681 | { | |
8682 | error ("not a static field `%s'", IDENTIFIER_POINTER (name)); | |
8683 | return error_mark_node; | |
8684 | } | |
8685 | return field; | |
8686 | } | |
8687 | else | |
8688 | fatal ("unimplemented java_complete_tree for COMPONENT_REF"); | |
9bbc7d9f | 8689 | break; |
9bbc7d9f | 8690 | |
b67d701b | 8691 | case THIS_EXPR: |
e04a16fb AG |
8692 | /* Can't use THIS in a static environment */ |
8693 | if (!current_this) | |
8694 | { | |
8695 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
8696 | parse_error_context (wfl_operator, "Keyword `this' used outside " | |
8697 | "allowed context"); | |
8698 | TREE_TYPE (node) = error_mark_node; | |
8699 | return error_mark_node; | |
8700 | } | |
22eed1e6 APB |
8701 | if (ctxp->explicit_constructor_p) |
8702 | { | |
8703 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
8704 | parse_error_context | |
8705 | (wfl_operator, "Can't reference `this' or `super' before the " | |
8706 | "superclass constructor has been called"); | |
8707 | TREE_TYPE (node) = error_mark_node; | |
8708 | return error_mark_node; | |
8709 | } | |
e04a16fb AG |
8710 | return current_this; |
8711 | ||
e04a16fb | 8712 | default: |
15fdcfe9 | 8713 | CAN_COMPLETE_NORMALLY (node) = 1; |
b67d701b PB |
8714 | /* Ok: may be we have a STRING_CST or a crafted `StringBuffer' |
8715 | and it's time to turn it into the appropriate String object | |
8716 | */ | |
8717 | if ((node = patch_string (node))) | |
8718 | return node; | |
e04a16fb AG |
8719 | fatal ("No case for tree code `%s' - java_complete_tree\n", |
8720 | tree_code_name [TREE_CODE (node)]); | |
8721 | } | |
8722 | return node; | |
8723 | } | |
8724 | ||
8725 | /* Complete function call's argument. Return a non zero value is an | |
8726 | error was found. */ | |
8727 | ||
8728 | static int | |
8729 | complete_function_arguments (node) | |
8730 | tree node; | |
8731 | { | |
8732 | int flag = 0; | |
8733 | tree cn; | |
8734 | ||
22eed1e6 | 8735 | ctxp->explicit_constructor_p += (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0); |
e04a16fb AG |
8736 | for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn)) |
8737 | { | |
b67d701b | 8738 | tree wfl = TREE_VALUE (cn), parm, temp; |
e04a16fb AG |
8739 | parm = java_complete_tree (wfl); |
8740 | if (parm == error_mark_node) | |
8741 | { | |
8742 | flag = 1; | |
8743 | continue; | |
8744 | } | |
b67d701b PB |
8745 | /* If have a string literal that we haven't transformed yet or a |
8746 | crafted string buffer, as a result of use of the the String | |
8747 | `+' operator. Build `parm.toString()' and expand it. */ | |
8748 | if ((temp = patch_string (parm))) | |
b9f7e36c | 8749 | parm = temp; |
5e942c50 APB |
8750 | /* Inline PRIMTYPE.TYPE read access */ |
8751 | parm = maybe_build_primttype_type_ref (parm, wfl); | |
b9f7e36c | 8752 | |
5e942c50 | 8753 | TREE_VALUE (cn) = parm; |
e04a16fb | 8754 | } |
22eed1e6 | 8755 | ctxp->explicit_constructor_p -= (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0); |
e04a16fb AG |
8756 | return flag; |
8757 | } | |
8758 | ||
8759 | /* Sometimes (for loops and variable initialized during their | |
8760 | declaration), we want to wrap a statement around a WFL and turn it | |
8761 | debugable. */ | |
8762 | ||
8763 | static tree | |
8764 | build_debugable_stmt (location, stmt) | |
8765 | int location; | |
8766 | tree stmt; | |
8767 | { | |
8768 | if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION) | |
8769 | { | |
8770 | stmt = build_expr_wfl (stmt, input_filename, 0, 0); | |
8771 | EXPR_WFL_LINECOL (stmt) = location; | |
8772 | } | |
8773 | JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt); | |
8774 | return stmt; | |
8775 | } | |
8776 | ||
8777 | static tree | |
8778 | build_expr_block (body, decls) | |
8779 | tree body, decls; | |
8780 | { | |
8781 | tree node = make_node (BLOCK); | |
8782 | BLOCK_EXPR_DECLS (node) = decls; | |
b67d701b | 8783 | BLOCK_EXPR_BODY (node) = body; |
e04a16fb AG |
8784 | if (body) |
8785 | TREE_TYPE (node) = TREE_TYPE (body); | |
8786 | TREE_SIDE_EFFECTS (node) = 1; | |
8787 | return node; | |
8788 | } | |
8789 | ||
b67d701b PB |
8790 | /* Create a new function block and link it approriately to current |
8791 | function block chain */ | |
e04a16fb AG |
8792 | |
8793 | static tree | |
8794 | enter_block () | |
8795 | { | |
b67d701b PB |
8796 | return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE))); |
8797 | } | |
8798 | ||
8799 | /* Link block B supercontext to the previous block. The current | |
8800 | function DECL is used as supercontext when enter_a_block is called | |
8801 | for the first time for a given function. The current function body | |
8802 | (DECL_FUNCTION_BODY) is set to be block B. */ | |
8803 | ||
8804 | static tree | |
8805 | enter_a_block (b) | |
8806 | tree b; | |
8807 | { | |
e04a16fb AG |
8808 | tree fndecl = current_function_decl; |
8809 | ||
f099f336 APB |
8810 | if (!fndecl) { |
8811 | BLOCK_SUPERCONTEXT (b) = current_static_block; | |
8812 | current_static_block = b; | |
8813 | } | |
8814 | ||
8815 | else if (!DECL_FUNCTION_BODY (fndecl)) | |
e04a16fb AG |
8816 | { |
8817 | BLOCK_SUPERCONTEXT (b) = fndecl; | |
8818 | DECL_FUNCTION_BODY (fndecl) = b; | |
8819 | } | |
8820 | else | |
8821 | { | |
8822 | BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl); | |
8823 | DECL_FUNCTION_BODY (fndecl) = b; | |
8824 | } | |
8825 | return b; | |
8826 | } | |
8827 | ||
8828 | /* Exit a block by changing the current function body | |
8829 | (DECL_FUNCTION_BODY) to the current block super context, only if | |
8830 | the block being exited isn't the method's top level one. */ | |
8831 | ||
8832 | static tree | |
8833 | exit_block () | |
8834 | { | |
f099f336 APB |
8835 | tree b; |
8836 | if (current_function_decl) | |
8837 | { | |
8838 | b = DECL_FUNCTION_BODY (current_function_decl); | |
8839 | if (BLOCK_SUPERCONTEXT (b) != current_function_decl) | |
8840 | DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b); | |
8841 | } | |
8842 | else | |
8843 | { | |
8844 | b = current_static_block; | |
e04a16fb | 8845 | |
f099f336 APB |
8846 | if (BLOCK_SUPERCONTEXT (b)) |
8847 | current_static_block = BLOCK_SUPERCONTEXT (b); | |
8848 | } | |
e04a16fb AG |
8849 | return b; |
8850 | } | |
8851 | ||
8852 | /* Lookup for NAME in the nested function's blocks, all the way up to | |
8853 | the current toplevel one. It complies with Java's local variable | |
8854 | scoping rules. */ | |
8855 | ||
8856 | static tree | |
8857 | lookup_name_in_blocks (name) | |
8858 | tree name; | |
8859 | { | |
f099f336 | 8860 | tree b = GET_CURRENT_BLOCK (current_function_decl); |
e04a16fb AG |
8861 | |
8862 | while (b != current_function_decl) | |
8863 | { | |
8864 | tree current; | |
8865 | ||
8866 | /* Paranoid sanity check. To be removed */ | |
8867 | if (TREE_CODE (b) != BLOCK) | |
8868 | fatal ("non block expr function body - lookup_name_in_blocks"); | |
8869 | ||
8870 | for (current = BLOCK_EXPR_DECLS (b); current; | |
8871 | current = TREE_CHAIN (current)) | |
8872 | if (DECL_NAME (current) == name) | |
8873 | return current; | |
8874 | b = BLOCK_SUPERCONTEXT (b); | |
8875 | } | |
8876 | return NULL_TREE; | |
8877 | } | |
8878 | ||
8879 | static void | |
8880 | maybe_absorb_scoping_blocks () | |
8881 | { | |
f099f336 | 8882 | while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl))) |
e04a16fb AG |
8883 | { |
8884 | tree b = exit_block (); | |
8885 | java_method_add_stmt (current_function_decl, b); | |
8886 | SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno)); | |
8887 | } | |
8888 | } | |
8889 | ||
8890 | \f | |
8891 | /* This section of the source is reserved to build_* functions that | |
8892 | are building incomplete tree nodes and the patch_* functions that | |
8893 | are completing them. */ | |
8894 | ||
9bbc7d9f | 8895 | /* Build a super() constructor invocation. Returns empty_stmt_node if |
22eed1e6 APB |
8896 | we're currently dealing with the class java.lang.Object. */ |
8897 | ||
8898 | static tree | |
8899 | build_super_invocation () | |
8900 | { | |
8901 | if (current_class == object_type_node) | |
9bbc7d9f | 8902 | return empty_stmt_node; |
22eed1e6 APB |
8903 | else |
8904 | { | |
9ee9b555 | 8905 | tree super_wfl = build_wfl_node (super_identifier_node); |
22eed1e6 APB |
8906 | return build_method_invocation (super_wfl, NULL_TREE); |
8907 | } | |
8908 | } | |
8909 | ||
8910 | /* Build a SUPER/THIS qualified method invocation. */ | |
8911 | ||
8912 | static tree | |
8913 | build_this_super_qualified_invocation (use_this, name, args, lloc, rloc) | |
8914 | int use_this; | |
8915 | tree name, args; | |
8916 | int lloc, rloc; | |
8917 | ||
8918 | { | |
8919 | tree invok; | |
8920 | tree wfl = | |
9ee9b555 | 8921 | build_wfl_node (use_this ? this_identifier_node : super_identifier_node); |
22eed1e6 APB |
8922 | EXPR_WFL_LINECOL (wfl) = lloc; |
8923 | invok = build_method_invocation (name, args); | |
8924 | return make_qualified_primary (wfl, invok, rloc); | |
8925 | } | |
8926 | ||
b67d701b | 8927 | /* Build an incomplete CALL_EXPR node. */ |
e04a16fb AG |
8928 | |
8929 | static tree | |
8930 | build_method_invocation (name, args) | |
8931 | tree name; | |
8932 | tree args; | |
8933 | { | |
8934 | tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE); | |
8935 | TREE_SIDE_EFFECTS (call) = 1; | |
b67d701b PB |
8936 | EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name); |
8937 | return call; | |
8938 | } | |
8939 | ||
8940 | /* Build an incomplete new xxx(...) node. */ | |
8941 | ||
8942 | static tree | |
8943 | build_new_invocation (name, args) | |
8944 | tree name, args; | |
8945 | { | |
8946 | tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE); | |
8947 | TREE_SIDE_EFFECTS (call) = 1; | |
e04a16fb AG |
8948 | EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name); |
8949 | return call; | |
8950 | } | |
8951 | ||
8952 | /* Build an incomplete assignment expression. */ | |
8953 | ||
8954 | static tree | |
8955 | build_assignment (op, op_location, lhs, rhs) | |
8956 | int op, op_location; | |
8957 | tree lhs, rhs; | |
8958 | { | |
8959 | tree assignment; | |
8960 | /* Build the corresponding binop if we deal with a Compound | |
8961 | Assignment operator. Mark the binop sub-tree as part of a | |
8962 | Compound Assignment expression */ | |
8963 | if (op != ASSIGN_TK) | |
8964 | { | |
8965 | rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs); | |
8966 | COMPOUND_ASSIGN_P (rhs) = 1; | |
8967 | } | |
8968 | assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs); | |
8969 | TREE_SIDE_EFFECTS (assignment) = 1; | |
8970 | EXPR_WFL_LINECOL (assignment) = op_location; | |
8971 | return assignment; | |
8972 | } | |
8973 | ||
8974 | /* Print an INTEGER_CST node in a static buffer, and return the buffer. */ | |
8975 | ||
15fdcfe9 | 8976 | char * |
e04a16fb AG |
8977 | print_int_node (node) |
8978 | tree node; | |
8979 | { | |
8980 | static char buffer [80]; | |
8981 | if (TREE_CONSTANT_OVERFLOW (node)) | |
8982 | sprintf (buffer, "<overflow>"); | |
8983 | ||
8984 | if (TREE_INT_CST_HIGH (node) == 0) | |
8985 | sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED, | |
8986 | TREE_INT_CST_LOW (node)); | |
8987 | else if (TREE_INT_CST_HIGH (node) == -1 | |
8988 | && TREE_INT_CST_LOW (node) != 0) | |
8989 | { | |
8990 | buffer [0] = '-'; | |
8991 | sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED, | |
8992 | -TREE_INT_CST_LOW (node)); | |
8993 | } | |
8994 | else | |
8995 | sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX, | |
8996 | TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node)); | |
8997 | ||
8998 | return buffer; | |
8999 | } | |
9000 | ||
7f1d4866 APB |
9001 | /* Return 1 if an assignment to a FINAL is attempted in a non suitable |
9002 | context. */ | |
5e942c50 APB |
9003 | |
9004 | static int | |
9005 | check_final_assignment (lvalue, wfl) | |
9006 | tree lvalue, wfl; | |
9007 | { | |
7f1d4866 APB |
9008 | if (JDECL_P (lvalue) |
9009 | && FIELD_FINAL (lvalue) && !IS_CLINIT (current_function_decl)) | |
5e942c50 APB |
9010 | { |
9011 | parse_error_context | |
9012 | (wfl, "Can't assign a value to the final variable `%s'", | |
9013 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
9014 | return 1; | |
9015 | } | |
9016 | return 0; | |
9017 | } | |
9018 | ||
9019 | /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in | |
9020 | read. This is needed to avoid circularities in the implementation | |
9021 | of these fields in libjava. */ | |
9022 | ||
9023 | static tree | |
9024 | maybe_build_primttype_type_ref (rhs, wfl) | |
9025 | tree rhs, wfl; | |
9026 | { | |
9027 | tree to_return = NULL_TREE; | |
9028 | tree rhs_type = TREE_TYPE (rhs); | |
9029 | if (TREE_CODE (rhs) == COMPOUND_EXPR) | |
9030 | { | |
9031 | tree n = TREE_OPERAND (rhs, 1); | |
9032 | if (TREE_CODE (n) == VAR_DECL | |
9033 | && DECL_NAME (n) == TYPE_identifier_node | |
9034 | && rhs_type == class_ptr_type) | |
9035 | { | |
49f48c71 | 9036 | const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)); |
5e942c50 APB |
9037 | if (!strncmp (self_name, "java.lang.", 10)) |
9038 | to_return = build_primtype_type_ref (self_name); | |
9039 | } | |
9040 | } | |
9041 | return (to_return ? to_return : rhs ); | |
9042 | } | |
9043 | ||
e04a16fb AG |
9044 | /* 15.25 Assignment operators. */ |
9045 | ||
9046 | static tree | |
9047 | patch_assignment (node, wfl_op1, wfl_op2) | |
9048 | tree node; | |
9049 | tree wfl_op1; | |
9050 | tree wfl_op2; | |
9051 | { | |
0a2138e2 | 9052 | tree rhs = TREE_OPERAND (node, 1); |
5e942c50 | 9053 | tree lvalue = TREE_OPERAND (node, 0), llvalue; |
cd531a2e | 9054 | tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE; |
e04a16fb AG |
9055 | int error_found = 0; |
9056 | int lvalue_from_array = 0; | |
9057 | ||
9058 | /* Can't assign to a final. */ | |
5e942c50 APB |
9059 | if (check_final_assignment (lvalue, wfl_op1)) |
9060 | error_found = 1; | |
e04a16fb AG |
9061 | |
9062 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
9063 | ||
9064 | /* Lhs can be a named variable */ | |
34f4db93 | 9065 | if (JDECL_P (lvalue)) |
e04a16fb | 9066 | { |
e04a16fb AG |
9067 | lhs_type = TREE_TYPE (lvalue); |
9068 | } | |
9069 | /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME + | |
9070 | comment on reason why */ | |
9071 | else if (TREE_CODE (wfl_op1) == ARRAY_REF) | |
9072 | { | |
9073 | lhs_type = TREE_TYPE (lvalue); | |
9074 | lvalue_from_array = 1; | |
9075 | } | |
9076 | /* Or a field access */ | |
9077 | else if (TREE_CODE (lvalue) == COMPONENT_REF) | |
9078 | lhs_type = TREE_TYPE (lvalue); | |
9079 | /* Or a function return slot */ | |
9080 | else if (TREE_CODE (lvalue) == RESULT_DECL) | |
9081 | lhs_type = TREE_TYPE (lvalue); | |
5e942c50 APB |
9082 | /* Otherwise, we might want to try to write into an optimized static |
9083 | final, this is an of a different nature, reported further on. */ | |
9084 | else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION | |
1504b2b4 | 9085 | && resolve_expression_name (wfl_op1, &llvalue)) |
5e942c50 | 9086 | { |
1504b2b4 APB |
9087 | if (check_final_assignment (llvalue, wfl_op1)) |
9088 | { | |
9089 | /* What we should do instead is resetting the all the flags | |
9090 | previously set, exchange lvalue for llvalue and continue. */ | |
9091 | error_found = 1; | |
9092 | return error_mark_node; | |
9093 | } | |
9094 | else | |
9095 | lhs_type = TREE_TYPE (lvalue); | |
5e942c50 APB |
9096 | } |
9097 | else | |
e04a16fb AG |
9098 | { |
9099 | parse_error_context (wfl_op1, "Invalid left hand side of assignment"); | |
9100 | error_found = 1; | |
9101 | } | |
9102 | ||
9103 | rhs_type = TREE_TYPE (rhs); | |
b67d701b | 9104 | /* 5.1 Try the assignment conversion for builtin type. */ |
0a2138e2 | 9105 | new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs); |
e04a16fb | 9106 | |
b67d701b | 9107 | /* 5.2 If it failed, try a reference conversion */ |
0a2138e2 | 9108 | if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs))) |
b67d701b | 9109 | lhs_type = promote_type (rhs_type); |
e04a16fb AG |
9110 | |
9111 | /* 15.25.2 If we have a compound assignment, convert RHS into the | |
9112 | type of the LHS */ | |
9113 | else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1))) | |
9114 | new_rhs = convert (lhs_type, rhs); | |
9115 | ||
9116 | /* Explicit cast required. This is an error */ | |
9117 | if (!new_rhs) | |
9118 | { | |
c2e3db92 KG |
9119 | char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0)); |
9120 | char *t2 = xstrdup (lang_printable_name (lhs_type, 0)); | |
e04a16fb AG |
9121 | tree wfl; |
9122 | char operation [32]; /* Max size known */ | |
9123 | ||
9124 | /* If the assignment is part of a declaration, we use the WFL of | |
9125 | the declared variable to point out the error and call it a | |
9126 | declaration problem. If the assignment is a genuine = | |
9127 | operator, we call is a operator `=' problem, otherwise we | |
9128 | call it an assignment problem. In both of these last cases, | |
9129 | we use the WFL of the operator to indicate the error. */ | |
9130 | ||
9131 | if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)) | |
9132 | { | |
9133 | wfl = wfl_op1; | |
9134 | strcpy (operation, "declaration"); | |
9135 | } | |
9136 | else | |
9137 | { | |
9138 | wfl = wfl_operator; | |
9139 | if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1))) | |
9140 | strcpy (operation, "assignment"); | |
9141 | else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL) | |
9142 | strcpy (operation, "`return'"); | |
9143 | else | |
9144 | strcpy (operation, "`='"); | |
9145 | } | |
9146 | ||
1ebadc60 KG |
9147 | if (!valid_cast_to_p (rhs_type, lhs_type)) |
9148 | parse_error_context (wfl, "Incompatible type for %s. " | |
9149 | "Can't convert `%s' to `%s'", | |
9150 | operation, t1, t2); | |
9151 | else | |
9152 | parse_error_context (wfl, "Incompatible type for %s. " | |
9153 | "Explicit cast needed to convert `%s' to `%s'", | |
9154 | operation, t1, t2); | |
e04a16fb AG |
9155 | free (t1); free (t2); |
9156 | error_found = 1; | |
9157 | } | |
9158 | ||
c877974e APB |
9159 | /* Inline read access to java.lang.PRIMTYPE.TYPE */ |
9160 | if (new_rhs) | |
9161 | new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2); | |
5e942c50 | 9162 | |
e04a16fb AG |
9163 | if (error_found) |
9164 | return error_mark_node; | |
9165 | ||
2622b947 APB |
9166 | /* 10.10: Array Store Exception runtime check */ |
9167 | if (!flag_emit_class_files | |
e8fc7396 | 9168 | && !flag_emit_xref |
2622b947 APB |
9169 | && lvalue_from_array |
9170 | && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)) | |
9171 | && !CLASS_FINAL (TYPE_NAME (GET_SKIP_TYPE (rhs_type)))) | |
9172 | { | |
9173 | tree check; | |
9174 | tree base = lvalue; | |
9175 | ||
9176 | /* We need to retrieve the right argument for _Jv_CheckArrayStore */ | |
9177 | if (TREE_CODE (lvalue) == COMPOUND_EXPR) | |
9178 | base = TREE_OPERAND (lvalue, 0); | |
9179 | else | |
9180 | { | |
9181 | if (flag_bounds_check) | |
9182 | base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0); | |
9183 | else | |
9184 | base = TREE_OPERAND (TREE_OPERAND (base, 0), 0); | |
9185 | } | |
9186 | ||
9187 | /* Build the invocation of _Jv_CheckArrayStore */ | |
dc4e6ccf | 9188 | new_rhs = save_expr (new_rhs); |
2622b947 APB |
9189 | check = build (CALL_EXPR, void_type_node, |
9190 | build_address_of (soft_checkarraystore_node), | |
9191 | tree_cons (NULL_TREE, base, | |
9192 | build_tree_list (NULL_TREE, new_rhs)), | |
9193 | NULL_TREE); | |
9194 | TREE_SIDE_EFFECTS (check) = 1; | |
9195 | ||
9196 | /* We have to decide on an insertion point */ | |
9197 | if (TREE_CODE (lvalue) == COMPOUND_EXPR) | |
9198 | { | |
9199 | tree t; | |
9200 | if (flag_bounds_check) | |
9201 | { | |
9202 | t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0); | |
9203 | TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) = | |
9204 | build (COMPOUND_EXPR, void_type_node, t, check); | |
9205 | } | |
9206 | else | |
9207 | TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type, | |
9208 | check, TREE_OPERAND (lvalue, 1)); | |
9209 | } | |
9210 | else | |
9211 | { | |
9212 | /* Make sure the bound check will happen before the store check */ | |
9213 | if (flag_bounds_check) | |
9214 | TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) = | |
9215 | build (COMPOUND_EXPR, void_type_node, | |
9216 | TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check); | |
9217 | else | |
9218 | lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue); | |
9219 | } | |
9220 | } | |
22eed1e6 | 9221 | |
e04a16fb AG |
9222 | TREE_OPERAND (node, 0) = lvalue; |
9223 | TREE_OPERAND (node, 1) = new_rhs; | |
9224 | TREE_TYPE (node) = lhs_type; | |
9225 | return node; | |
9226 | } | |
9227 | ||
b67d701b PB |
9228 | /* Check that type SOURCE can be cast into type DEST. If the cast |
9229 | can't occur at all, return 0 otherwise 1. This function is used to | |
9230 | produce accurate error messages on the reasons why an assignment | |
9231 | failed. */ | |
e04a16fb | 9232 | |
b67d701b PB |
9233 | static tree |
9234 | try_reference_assignconv (lhs_type, rhs) | |
9235 | tree lhs_type, rhs; | |
e04a16fb | 9236 | { |
b67d701b PB |
9237 | tree new_rhs = NULL_TREE; |
9238 | tree rhs_type = TREE_TYPE (rhs); | |
e04a16fb | 9239 | |
b67d701b PB |
9240 | if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type)) |
9241 | { | |
9242 | /* `null' may be assigned to any reference type */ | |
9243 | if (rhs == null_pointer_node) | |
9244 | new_rhs = null_pointer_node; | |
9245 | /* Try the reference assignment conversion */ | |
9246 | else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0)) | |
9247 | new_rhs = rhs; | |
9248 | /* This is a magic assignment that we process differently */ | |
9249 | else if (rhs == soft_exceptioninfo_call_node) | |
9250 | new_rhs = rhs; | |
9251 | } | |
9252 | return new_rhs; | |
9253 | } | |
9254 | ||
9255 | /* Check that RHS can be converted into LHS_TYPE by the assignment | |
9256 | conversion (5.2), for the cases of RHS being a builtin type. Return | |
9257 | NULL_TREE if the conversion fails or if because RHS isn't of a | |
9258 | builtin type. Return a converted RHS if the conversion is possible. */ | |
9259 | ||
9260 | static tree | |
9261 | try_builtin_assignconv (wfl_op1, lhs_type, rhs) | |
9262 | tree wfl_op1, lhs_type, rhs; | |
9263 | { | |
9264 | tree new_rhs = NULL_TREE; | |
9265 | tree rhs_type = TREE_TYPE (rhs); | |
9266 | ||
5e942c50 APB |
9267 | /* Zero accepted everywhere */ |
9268 | if (TREE_CODE (rhs) == INTEGER_CST | |
9269 | && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0 | |
9270 | && JPRIMITIVE_TYPE_P (rhs_type)) | |
9271 | new_rhs = convert (lhs_type, rhs); | |
9272 | ||
b67d701b PB |
9273 | /* 5.1.1 Try Identity Conversion, |
9274 | 5.1.2 Try Widening Primitive Conversion */ | |
5e942c50 | 9275 | else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)) |
b67d701b PB |
9276 | new_rhs = convert (lhs_type, rhs); |
9277 | ||
9278 | /* Try a narrowing primitive conversion (5.1.3): | |
9279 | - expression is a constant expression of type int AND | |
9280 | - variable is byte, short or char AND | |
9281 | - The value of the expression is representable in the type of the | |
9282 | variable */ | |
9283 | else if (rhs_type == int_type_node && TREE_CONSTANT (rhs) | |
9284 | && (lhs_type == byte_type_node || lhs_type == char_type_node | |
9285 | || lhs_type == short_type_node)) | |
9286 | { | |
9287 | if (int_fits_type_p (rhs, lhs_type)) | |
9288 | new_rhs = convert (lhs_type, rhs); | |
9289 | else if (wfl_op1) /* Might be called with a NULL */ | |
9290 | parse_warning_context | |
9291 | (wfl_op1, "Constant expression `%s' to wide for narrowing " | |
9292 | "primitive conversion to `%s'", | |
0a2138e2 | 9293 | print_int_node (rhs), lang_printable_name (lhs_type, 0)); |
b67d701b PB |
9294 | /* Reported a warning that will turn into an error further |
9295 | down, so we don't return */ | |
9296 | } | |
9297 | ||
9298 | return new_rhs; | |
9299 | } | |
9300 | ||
9301 | /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity | |
9302 | conversion (5.1.1) or widening primitve conversion (5.1.2). Return | |
9303 | 0 is the conversion test fails. This implements parts the method | |
9304 | invocation convertion (5.3). */ | |
9305 | ||
9306 | static int | |
9307 | valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type) | |
9308 | tree lhs_type, rhs_type; | |
9309 | { | |
acd663ee | 9310 | /* 5.1.1: This is the identity conversion part. */ |
5e942c50 APB |
9311 | if (lhs_type == rhs_type) |
9312 | return 1; | |
9313 | ||
acd663ee APB |
9314 | /* Reject non primitive types */ |
9315 | if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type)) | |
b67d701b PB |
9316 | return 0; |
9317 | ||
acd663ee APB |
9318 | /* 5.1.2: widening primitive conversion. byte, even if it's smaller |
9319 | than a char can't be converted into a char. Short can't too, but | |
9320 | the < test below takes care of that */ | |
b67d701b PB |
9321 | if (lhs_type == char_type_node && rhs_type == byte_type_node) |
9322 | return 0; | |
9323 | ||
5e942c50 APB |
9324 | /* Accept all promoted type here. Note, we can't use <= in the test |
9325 | below, because we still need to bounce out assignments of short | |
9326 | to char and the likes */ | |
9327 | if (lhs_type == int_type_node | |
9328 | && (rhs_type == promoted_byte_type_node | |
9329 | || rhs_type == promoted_short_type_node | |
9330 | || rhs_type == promoted_char_type_node | |
9331 | || rhs_type == promoted_boolean_type_node)) | |
9332 | return 1; | |
9333 | ||
acd663ee APB |
9334 | /* From here, an integral is widened if its precision is smaller |
9335 | than the precision of the LHS or if the LHS is a floating point | |
9336 | type, or the RHS is a float and the RHS a double. */ | |
9337 | if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type) | |
9338 | && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type))) | |
9339 | || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type)) | |
9340 | || (rhs_type == float_type_node && lhs_type == double_type_node)) | |
b67d701b PB |
9341 | return 1; |
9342 | ||
9343 | return 0; | |
e04a16fb AG |
9344 | } |
9345 | ||
9346 | /* Check that something of SOURCE type can be assigned or cast to | |
9347 | something of DEST type at runtime. Return 1 if the operation is | |
9348 | valid, 0 otherwise. If CAST is set to 1, we're treating the case | |
9349 | were SOURCE is cast into DEST, which borrows a lot of the | |
9350 | assignment check. */ | |
9351 | ||
9352 | static int | |
9353 | valid_ref_assignconv_cast_p (source, dest, cast) | |
9354 | tree source; | |
9355 | tree dest; | |
9356 | int cast; | |
9357 | { | |
09ed0f70 APB |
9358 | /* SOURCE or DEST might be null if not from a declared entity. */ |
9359 | if (!source || !dest) | |
9360 | return 0; | |
5e942c50 APB |
9361 | if (JNULLP_TYPE_P (source)) |
9362 | return 1; | |
e04a16fb AG |
9363 | if (TREE_CODE (source) == POINTER_TYPE) |
9364 | source = TREE_TYPE (source); | |
9365 | if (TREE_CODE (dest) == POINTER_TYPE) | |
9366 | dest = TREE_TYPE (dest); | |
9367 | /* Case where SOURCE is a class type */ | |
9368 | if (TYPE_CLASS_P (source)) | |
9369 | { | |
9370 | if (TYPE_CLASS_P (dest)) | |
9371 | return source == dest || inherits_from_p (source, dest) | |
0a2138e2 | 9372 | || (cast && inherits_from_p (dest, source)); |
e04a16fb AG |
9373 | if (TYPE_INTERFACE_P (dest)) |
9374 | { | |
9375 | /* If doing a cast and SOURCE is final, the operation is | |
9376 | always correct a compile time (because even if SOURCE | |
9377 | does not implement DEST, a subclass of SOURCE might). */ | |
9378 | if (cast && !CLASS_FINAL (TYPE_NAME (source))) | |
9379 | return 1; | |
9380 | /* Otherwise, SOURCE must implement DEST */ | |
9381 | return interface_of_p (dest, source); | |
9382 | } | |
9383 | /* DEST is an array, cast permited if SOURCE is of Object type */ | |
9384 | return (cast && source == object_type_node ? 1 : 0); | |
9385 | } | |
9386 | if (TYPE_INTERFACE_P (source)) | |
9387 | { | |
9388 | if (TYPE_CLASS_P (dest)) | |
9389 | { | |
9390 | /* If not casting, DEST must be the Object type */ | |
9391 | if (!cast) | |
9392 | return dest == object_type_node; | |
9393 | /* We're doing a cast. The cast is always valid is class | |
9394 | DEST is not final, otherwise, DEST must implement SOURCE */ | |
b67d701b | 9395 | else if (!CLASS_FINAL (TYPE_NAME (dest))) |
e04a16fb AG |
9396 | return 1; |
9397 | else | |
9398 | return interface_of_p (source, dest); | |
9399 | } | |
9400 | if (TYPE_INTERFACE_P (dest)) | |
9401 | { | |
9402 | /* If doing a cast, then if SOURCE and DEST contain method | |
9403 | with the same signature but different return type, then | |
9404 | this is a (compile time) error */ | |
9405 | if (cast) | |
9406 | { | |
9407 | tree method_source, method_dest; | |
9408 | tree source_type; | |
0a2138e2 | 9409 | tree source_sig; |
e04a16fb AG |
9410 | tree source_name; |
9411 | for (method_source = TYPE_METHODS (source); method_source; | |
9412 | method_source = TREE_CHAIN (method_source)) | |
9413 | { | |
9414 | source_sig = | |
9415 | build_java_argument_signature (TREE_TYPE (method_source)); | |
9416 | source_type = TREE_TYPE (TREE_TYPE (method_source)); | |
9417 | source_name = DECL_NAME (method_source); | |
9418 | for (method_dest = TYPE_METHODS (dest); | |
9419 | method_dest; method_dest = TREE_CHAIN (method_dest)) | |
9420 | if (source_sig == | |
9421 | build_java_argument_signature (TREE_TYPE (method_dest)) | |
9422 | && source_name == DECL_NAME (method_dest) | |
9423 | && source_type != TREE_TYPE (TREE_TYPE (method_dest))) | |
9424 | return 0; | |
9425 | } | |
9426 | return 1; | |
9427 | } | |
9428 | else | |
9429 | return source == dest || interface_of_p (dest, source); | |
9430 | } | |
9431 | else /* Array */ | |
93024893 APB |
9432 | return (cast ? |
9433 | (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0); | |
e04a16fb AG |
9434 | } |
9435 | if (TYPE_ARRAY_P (source)) | |
9436 | { | |
9437 | if (TYPE_CLASS_P (dest)) | |
9438 | return dest == object_type_node; | |
09ed0f70 APB |
9439 | /* Can't cast an array to an interface unless the interface is |
9440 | java.lang.Cloneable */ | |
e04a16fb | 9441 | if (TYPE_INTERFACE_P (dest)) |
09ed0f70 | 9442 | return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0); |
e04a16fb AG |
9443 | else /* Arrays */ |
9444 | { | |
9445 | tree source_element_type = TYPE_ARRAY_ELEMENT (source); | |
9446 | tree dest_element_type = TYPE_ARRAY_ELEMENT (dest); | |
9447 | ||
b9f7e36c APB |
9448 | /* In case of severe errors, they turn out null */ |
9449 | if (!dest_element_type || !source_element_type) | |
9450 | return 0; | |
e04a16fb AG |
9451 | if (source_element_type == dest_element_type) |
9452 | return 1; | |
9453 | return valid_ref_assignconv_cast_p (source_element_type, | |
9454 | dest_element_type, cast); | |
9455 | } | |
9456 | return 0; | |
9457 | } | |
9458 | return 0; | |
9459 | } | |
9460 | ||
b67d701b PB |
9461 | static int |
9462 | valid_cast_to_p (source, dest) | |
9463 | tree source; | |
9464 | tree dest; | |
9465 | { | |
9466 | if (TREE_CODE (source) == POINTER_TYPE) | |
9467 | source = TREE_TYPE (source); | |
9468 | if (TREE_CODE (dest) == POINTER_TYPE) | |
9469 | dest = TREE_TYPE (dest); | |
9470 | ||
9471 | if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE) | |
9472 | return valid_ref_assignconv_cast_p (source, dest, 1); | |
9473 | ||
9474 | else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest)) | |
9475 | return 1; | |
9476 | ||
9477 | return 0; | |
9478 | } | |
9479 | ||
9480 | /* Method invocation conversion test. Return 1 if type SOURCE can be | |
9481 | converted to type DEST through the methond invocation conversion | |
9482 | process (5.3) */ | |
9483 | ||
15fdcfe9 PB |
9484 | static tree |
9485 | do_unary_numeric_promotion (arg) | |
9486 | tree arg; | |
9487 | { | |
9488 | tree type = TREE_TYPE (arg); | |
9489 | if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32 | |
9490 | : TREE_CODE (type) == CHAR_TYPE) | |
9491 | arg = convert (int_type_node, arg); | |
9492 | return arg; | |
9493 | } | |
9494 | ||
acd663ee APB |
9495 | /* Return a non zero value if SOURCE can be converted into DEST using |
9496 | the method invocation conversion rule (5.3). */ | |
b67d701b PB |
9497 | static int |
9498 | valid_method_invocation_conversion_p (dest, source) | |
9499 | tree dest, source; | |
9500 | { | |
e3884b71 | 9501 | return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest) |
acd663ee APB |
9502 | && valid_builtin_assignconv_identity_widening_p (dest, source)) |
9503 | || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source)) | |
9504 | && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest)) | |
9505 | && valid_ref_assignconv_cast_p (source, dest, 0))); | |
b67d701b PB |
9506 | } |
9507 | ||
e04a16fb AG |
9508 | /* Build an incomplete binop expression. */ |
9509 | ||
9510 | static tree | |
9511 | build_binop (op, op_location, op1, op2) | |
9512 | enum tree_code op; | |
9513 | int op_location; | |
9514 | tree op1, op2; | |
9515 | { | |
5e942c50 | 9516 | tree binop = build (op, NULL_TREE, op1, op2); |
e04a16fb AG |
9517 | TREE_SIDE_EFFECTS (binop) = 1; |
9518 | /* Store the location of the operator, for better error report. The | |
9519 | string of the operator will be rebuild based on the OP value. */ | |
9520 | EXPR_WFL_LINECOL (binop) = op_location; | |
9521 | return binop; | |
9522 | } | |
9523 | ||
9524 | /* Build the string of the operator retained by NODE. If NODE is part | |
9525 | of a compound expression, add an '=' at the end of the string. This | |
9526 | function is called when an error needs to be reported on an | |
9527 | operator. The string is returned as a pointer to a static character | |
9528 | buffer. */ | |
9529 | ||
9530 | static char * | |
9531 | operator_string (node) | |
9532 | tree node; | |
9533 | { | |
9534 | #define BUILD_OPERATOR_STRING(S) \ | |
9535 | { \ | |
9536 | sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \ | |
9537 | return buffer; \ | |
9538 | } | |
9539 | ||
9540 | static char buffer [10]; | |
9541 | switch (TREE_CODE (node)) | |
9542 | { | |
9543 | case MULT_EXPR: BUILD_OPERATOR_STRING ("*"); | |
9544 | case RDIV_EXPR: BUILD_OPERATOR_STRING ("/"); | |
9545 | case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%"); | |
9546 | case PLUS_EXPR: BUILD_OPERATOR_STRING ("+"); | |
9547 | case MINUS_EXPR: BUILD_OPERATOR_STRING ("-"); | |
9548 | case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<"); | |
9549 | case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>"); | |
9550 | case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>"); | |
9551 | case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&"); | |
9552 | case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^"); | |
9553 | case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|"); | |
9554 | case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&"); | |
9555 | case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||"); | |
9556 | case EQ_EXPR: BUILD_OPERATOR_STRING ("=="); | |
9557 | case NE_EXPR: BUILD_OPERATOR_STRING ("!="); | |
9558 | case GT_EXPR: BUILD_OPERATOR_STRING (">"); | |
9559 | case GE_EXPR: BUILD_OPERATOR_STRING (">="); | |
9560 | case LT_EXPR: BUILD_OPERATOR_STRING ("<"); | |
9561 | case LE_EXPR: BUILD_OPERATOR_STRING ("<="); | |
b67d701b | 9562 | case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+"); |
e04a16fb AG |
9563 | case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-"); |
9564 | case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!"); | |
9565 | case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~"); | |
9566 | case PREINCREMENT_EXPR: /* Fall through */ | |
9567 | case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++"); | |
9568 | case PREDECREMENT_EXPR: /* Fall through */ | |
9569 | case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--"); | |
9570 | default: | |
9571 | fatal ("unregistered operator %s - operator_string", | |
9572 | tree_code_name [TREE_CODE (node)]); | |
9573 | } | |
9574 | return NULL; | |
9575 | #undef BUILD_OPERATOR_STRING | |
9576 | } | |
9577 | ||
5cbdba64 APB |
9578 | /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */ |
9579 | ||
9580 | static int | |
9581 | java_decl_equiv (var_acc1, var_acc2) | |
9582 | tree var_acc1, var_acc2; | |
9583 | { | |
9584 | if (JDECL_P (var_acc1)) | |
9585 | return (var_acc1 == var_acc2); | |
9586 | ||
9587 | return (TREE_CODE (var_acc1) == COMPONENT_REF | |
9588 | && TREE_CODE (var_acc2) == COMPONENT_REF | |
9589 | && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0) | |
9590 | == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0) | |
9591 | && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1)); | |
9592 | } | |
9593 | ||
9594 | /* Return a non zero value if CODE is one of the operators that can be | |
9595 | used in conjunction with the `=' operator in a compound assignment. */ | |
9596 | ||
9597 | static int | |
9598 | binop_compound_p (code) | |
9599 | enum tree_code code; | |
9600 | { | |
9601 | int i; | |
9602 | for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++) | |
9603 | if (binop_lookup [i] == code) | |
9604 | break; | |
9605 | ||
9606 | return i < BINOP_COMPOUND_CANDIDATES; | |
9607 | } | |
9608 | ||
9609 | /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */ | |
9610 | ||
9611 | static tree | |
9612 | java_refold (t) | |
9613 | tree t; | |
9614 | { | |
9615 | tree c, b, ns, decl; | |
9616 | ||
9617 | if (TREE_CODE (t) != MODIFY_EXPR) | |
9618 | return t; | |
9619 | ||
9620 | c = TREE_OPERAND (t, 1); | |
9621 | if (! (c && TREE_CODE (c) == COMPOUND_EXPR | |
9622 | && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR | |
9623 | && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1))))) | |
9624 | return t; | |
9625 | ||
9626 | /* Now the left branch of the binary operator. */ | |
9627 | b = TREE_OPERAND (TREE_OPERAND (c, 1), 0); | |
9628 | if (! (b && TREE_CODE (b) == NOP_EXPR | |
9629 | && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR)) | |
9630 | return t; | |
9631 | ||
9632 | ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0); | |
9633 | if (! (ns && TREE_CODE (ns) == NOP_EXPR | |
9634 | && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR)) | |
9635 | return t; | |
9636 | ||
9637 | decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0); | |
9638 | if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF) | |
9639 | /* It's got to be the an equivalent decl */ | |
9640 | && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0))) | |
9641 | { | |
9642 | /* Shorten the NOP_EXPR/SAVE_EXPR path. */ | |
9643 | TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0); | |
9644 | /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */ | |
9645 | TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1); | |
9646 | /* Change the right part of the BINOP_EXPR */ | |
9647 | TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0); | |
9648 | } | |
9649 | ||
9650 | return t; | |
9651 | } | |
9652 | ||
e04a16fb AG |
9653 | /* Binary operators (15.16 up to 15.18). We return error_mark_node on |
9654 | errors but we modify NODE so that it contains the type computed | |
9655 | according to the expression, when it's fixed. Otherwise, we write | |
9656 | error_mark_node as the type. It allows us to further the analysis | |
9657 | of remaining nodes and detects more errors in certain cases. */ | |
9658 | ||
9659 | static tree | |
9660 | patch_binop (node, wfl_op1, wfl_op2) | |
9661 | tree node; | |
9662 | tree wfl_op1; | |
9663 | tree wfl_op2; | |
9664 | { | |
9665 | tree op1 = TREE_OPERAND (node, 0); | |
9666 | tree op2 = TREE_OPERAND (node, 1); | |
9667 | tree op1_type = TREE_TYPE (op1); | |
9668 | tree op2_type = TREE_TYPE (op2); | |
ab3a6dd6 | 9669 | tree prom_type = NULL_TREE; |
e04a16fb | 9670 | int code = TREE_CODE (node); |
b67d701b | 9671 | |
e04a16fb AG |
9672 | /* If 1, tell the routine that we have to return error_mark_node |
9673 | after checking for the initialization of the RHS */ | |
9674 | int error_found = 0; | |
9675 | ||
e04a16fb AG |
9676 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); |
9677 | ||
e04a16fb AG |
9678 | switch (code) |
9679 | { | |
9680 | /* 15.16 Multiplicative operators */ | |
9681 | case MULT_EXPR: /* 15.16.1 Multiplication Operator * */ | |
9682 | case RDIV_EXPR: /* 15.16.2 Division Operator / */ | |
9683 | case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */ | |
9684 | if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type)) | |
9685 | { | |
9686 | if (!JPRIMITIVE_TYPE_P (op1_type)) | |
9687 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type); | |
9688 | if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type)) | |
9689 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type); | |
9690 | TREE_TYPE (node) = error_mark_node; | |
9691 | error_found = 1; | |
9692 | break; | |
9693 | } | |
9694 | prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
9695 | /* Change the division operator if necessary */ | |
9696 | if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE) | |
9697 | TREE_SET_CODE (node, TRUNC_DIV_EXPR); | |
0b4d333e | 9698 | |
aa4759c1 AH |
9699 | if (TREE_CODE (prom_type) == INTEGER_TYPE |
9700 | && flag_use_divide_subroutine | |
9701 | && ! flag_emit_class_files | |
9702 | && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR)) | |
9703 | return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2); | |
9704 | ||
0b4d333e APB |
9705 | /* This one is more complicated. FLOATs are processed by a |
9706 | function call to soft_fmod. Duplicate the value of the | |
9707 | COMPOUND_ASSIGN_P flag. */ | |
e04a16fb | 9708 | if (code == TRUNC_MOD_EXPR) |
0b4d333e APB |
9709 | { |
9710 | tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2); | |
9711 | COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node); | |
dc0b3eff PB |
9712 | TREE_SIDE_EFFECTS (mod) |
9713 | = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); | |
0b4d333e APB |
9714 | return mod; |
9715 | } | |
e04a16fb AG |
9716 | break; |
9717 | ||
9718 | /* 15.17 Additive Operators */ | |
9719 | case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */ | |
b67d701b PB |
9720 | |
9721 | /* Operation is valid if either one argument is a string | |
9722 | constant, a String object or a StringBuffer crafted for the | |
9723 | purpose of the a previous usage of the String concatenation | |
9724 | operator */ | |
9725 | ||
9726 | if (TREE_CODE (op1) == STRING_CST | |
9727 | || TREE_CODE (op2) == STRING_CST | |
9728 | || JSTRING_TYPE_P (op1_type) | |
9729 | || JSTRING_TYPE_P (op2_type) | |
9730 | || IS_CRAFTED_STRING_BUFFER_P (op1) | |
9731 | || IS_CRAFTED_STRING_BUFFER_P (op2)) | |
9732 | return build_string_concatenation (op1, op2); | |
9733 | ||
e04a16fb AG |
9734 | case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for |
9735 | Numeric Types */ | |
9736 | if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type)) | |
9737 | { | |
9738 | if (!JPRIMITIVE_TYPE_P (op1_type)) | |
9739 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type); | |
9740 | if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type)) | |
9741 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type); | |
9742 | TREE_TYPE (node) = error_mark_node; | |
9743 | error_found = 1; | |
9744 | break; | |
9745 | } | |
9746 | prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
9747 | break; | |
9748 | ||
9749 | /* 15.18 Shift Operators */ | |
9750 | case LSHIFT_EXPR: | |
9751 | case RSHIFT_EXPR: | |
9752 | case URSHIFT_EXPR: | |
9753 | if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type)) | |
9754 | { | |
9755 | if (!JINTEGRAL_TYPE_P (op1_type)) | |
9756 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type); | |
9757 | else | |
1ebadc60 KG |
9758 | { |
9759 | if (JPRIMITIVE_TYPE_P (op2_type)) | |
9760 | parse_error_context (wfl_operator, | |
9761 | "Incompatible type for `%s'. " | |
9762 | "Explicit cast needed to convert " | |
9763 | "shift distance from `%s' to integral", | |
9764 | operator_string (node), | |
9765 | lang_printable_name (op2_type, 0)); | |
9766 | else | |
9767 | parse_error_context (wfl_operator, "Incompatible type for `%s'." | |
9768 | " Can't convert shift distance from " | |
9769 | "`%s' to integral", | |
9770 | operator_string (node), | |
9771 | lang_printable_name (op2_type, 0)); | |
9772 | } | |
e04a16fb AG |
9773 | TREE_TYPE (node) = error_mark_node; |
9774 | error_found = 1; | |
9775 | break; | |
9776 | } | |
9777 | ||
9778 | /* Unary numeric promotion (5.6.1) is performed on each operand | |
9779 | separatly */ | |
15fdcfe9 PB |
9780 | op1 = do_unary_numeric_promotion (op1); |
9781 | op2 = do_unary_numeric_promotion (op2); | |
e04a16fb AG |
9782 | |
9783 | /* The type of the shift expression is the type of the promoted | |
9784 | type of the left-hand operand */ | |
9785 | prom_type = TREE_TYPE (op1); | |
9786 | ||
9787 | /* Shift int only up to 0x1f and long up to 0x3f */ | |
9788 | if (prom_type == int_type_node) | |
9789 | op2 = fold (build (BIT_AND_EXPR, int_type_node, op2, | |
9790 | build_int_2 (0x1f, 0))); | |
9791 | else | |
9792 | op2 = fold (build (BIT_AND_EXPR, int_type_node, op2, | |
9793 | build_int_2 (0x3f, 0))); | |
9794 | ||
9795 | /* The >>> operator is a >> operating on unsigned quantities */ | |
15fdcfe9 | 9796 | if (code == URSHIFT_EXPR && ! flag_emit_class_files) |
e04a16fb | 9797 | { |
0b4d333e | 9798 | tree to_return; |
73333a87 AH |
9799 | tree utype = unsigned_type (prom_type); |
9800 | op1 = convert (utype, op1); | |
e04a16fb | 9801 | TREE_SET_CODE (node, RSHIFT_EXPR); |
73333a87 AH |
9802 | TREE_OPERAND (node, 0) = op1; |
9803 | TREE_OPERAND (node, 1) = op2; | |
9804 | TREE_TYPE (node) = utype; | |
0b4d333e APB |
9805 | to_return = convert (prom_type, node); |
9806 | /* Copy the original value of the COMPOUND_ASSIGN_P flag */ | |
9807 | COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node); | |
dc0b3eff PB |
9808 | TREE_SIDE_EFFECTS (to_return) |
9809 | = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); | |
0b4d333e | 9810 | return to_return; |
e04a16fb AG |
9811 | } |
9812 | break; | |
5e942c50 APB |
9813 | |
9814 | /* 15.19.1 Type Comparison Operator instaceof */ | |
9815 | case INSTANCEOF_EXPR: | |
9816 | ||
9817 | TREE_TYPE (node) = boolean_type_node; | |
9818 | ||
9819 | if (!(op2_type = resolve_type_during_patch (op2))) | |
9820 | return error_mark_node; | |
9821 | ||
9822 | /* The first operand must be a reference type or the null type */ | |
9823 | if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node) | |
9824 | error_found = 1; /* Error reported further below */ | |
9825 | ||
9826 | /* The second operand must be a reference type */ | |
9827 | if (!JREFERENCE_TYPE_P (op2_type)) | |
9828 | { | |
9829 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op2); | |
9830 | parse_error_context | |
9831 | (wfl_operator, "Invalid argument `%s' for `instanceof'", | |
9832 | lang_printable_name (op2_type, 0)); | |
9833 | error_found = 1; | |
9834 | } | |
9835 | ||
9836 | if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1)) | |
9837 | { | |
9838 | /* If the first operand is null, the result is always false */ | |
9839 | if (op1 == null_pointer_node) | |
9840 | return boolean_false_node; | |
15fdcfe9 PB |
9841 | else if (flag_emit_class_files) |
9842 | { | |
9843 | TREE_OPERAND (node, 1) = op2_type; | |
dc0b3eff | 9844 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1); |
15fdcfe9 PB |
9845 | return node; |
9846 | } | |
5e942c50 APB |
9847 | /* Otherwise we have to invoke instance of to figure it out */ |
9848 | else | |
9849 | { | |
9850 | tree call = | |
9851 | build (CALL_EXPR, boolean_type_node, | |
9852 | build_address_of (soft_instanceof_node), | |
9853 | tree_cons | |
9854 | (NULL_TREE, op1, | |
9855 | build_tree_list (NULL_TREE, | |
9856 | build_class_ref (op2_type))), | |
9857 | NULL_TREE); | |
dc0b3eff | 9858 | TREE_SIDE_EFFECTS (call) = TREE_SIDE_EFFECTS (op1); |
5e942c50 APB |
9859 | return call; |
9860 | } | |
9861 | } | |
9862 | /* There is no way the expression operand can be an instance of | |
9863 | the type operand. This is a compile time error. */ | |
9864 | else | |
9865 | { | |
c2e3db92 | 9866 | char *t1 = xstrdup (lang_printable_name (op1_type, 0)); |
5e942c50 APB |
9867 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); |
9868 | parse_error_context | |
9869 | (wfl_operator, "Impossible for `%s' to be instance of `%s'", | |
9870 | t1, lang_printable_name (op2_type, 0)); | |
9871 | free (t1); | |
9872 | error_found = 1; | |
9873 | } | |
e04a16fb | 9874 | |
5e942c50 | 9875 | break; |
e04a16fb AG |
9876 | |
9877 | /* 15.21 Bitwise and Logical Operators */ | |
9878 | case BIT_AND_EXPR: | |
9879 | case BIT_XOR_EXPR: | |
9880 | case BIT_IOR_EXPR: | |
9881 | if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type)) | |
9882 | /* Binary numeric promotion is performed on both operand and the | |
9883 | expression retain that type */ | |
9884 | prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
9885 | ||
9886 | else if (TREE_CODE (op1_type) == BOOLEAN_TYPE | |
9887 | && TREE_CODE (op1_type) == BOOLEAN_TYPE) | |
9888 | /* The type of the bitwise operator expression is BOOLEAN */ | |
9889 | prom_type = boolean_type_node; | |
9890 | else | |
9891 | { | |
9892 | if (!JINTEGRAL_TYPE_P (op1_type)) | |
9893 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type); | |
9894 | if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type)) | |
9895 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type); | |
9896 | TREE_TYPE (node) = error_mark_node; | |
9897 | error_found = 1; | |
9898 | /* Insert a break here if adding thing before the switch's | |
9899 | break for this case */ | |
9900 | } | |
9901 | break; | |
9902 | ||
9903 | /* 15.22 Conditional-And Operator */ | |
9904 | case TRUTH_ANDIF_EXPR: | |
9905 | /* 15.23 Conditional-Or Operator */ | |
9906 | case TRUTH_ORIF_EXPR: | |
9907 | /* Operands must be of BOOLEAN type */ | |
9908 | if (TREE_CODE (op1_type) != BOOLEAN_TYPE || | |
9909 | TREE_CODE (op2_type) != BOOLEAN_TYPE) | |
9910 | { | |
9911 | if (TREE_CODE (op1_type) != BOOLEAN_TYPE) | |
9912 | ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type); | |
9913 | if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type)) | |
9914 | ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type); | |
9915 | TREE_TYPE (node) = boolean_type_node; | |
9916 | error_found = 1; | |
9917 | break; | |
9918 | } | |
9919 | /* The type of the conditional operators is BOOLEAN */ | |
9920 | prom_type = boolean_type_node; | |
9921 | break; | |
9922 | ||
9923 | /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */ | |
9924 | case LT_EXPR: | |
9925 | case GT_EXPR: | |
9926 | case LE_EXPR: | |
9927 | case GE_EXPR: | |
9928 | /* The type of each of the operands must be a primitive numeric | |
9929 | type */ | |
9930 | if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type)) | |
9931 | { | |
9932 | if (!JNUMERIC_TYPE_P (op1_type)) | |
9933 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type); | |
9934 | if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type)) | |
9935 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type); | |
9936 | TREE_TYPE (node) = boolean_type_node; | |
9937 | error_found = 1; | |
9938 | break; | |
9939 | } | |
9940 | /* Binary numeric promotion is performed on the operands */ | |
9941 | binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
9942 | /* The type of the relation expression is always BOOLEAN */ | |
9943 | prom_type = boolean_type_node; | |
9944 | break; | |
9945 | ||
9946 | /* 15.20 Equality Operator */ | |
9947 | case EQ_EXPR: | |
9948 | case NE_EXPR: | |
9949 | /* 15.20.1 Numerical Equality Operators == and != */ | |
9950 | /* Binary numeric promotion is performed on the operands */ | |
5e942c50 | 9951 | if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type)) |
e04a16fb AG |
9952 | binary_numeric_promotion (op1_type, op2_type, &op1, &op2); |
9953 | ||
9954 | /* 15.20.2 Boolean Equality Operators == and != */ | |
9955 | else if (TREE_CODE (op1_type) == BOOLEAN_TYPE && | |
9956 | TREE_CODE (op2_type) == BOOLEAN_TYPE) | |
9957 | ; /* Nothing to do here */ | |
9958 | ||
9959 | /* 15.20.3 Reference Equality Operators == and != */ | |
5e942c50 APB |
9960 | /* Types have to be either references or the null type. If |
9961 | they're references, it must be possible to convert either | |
9962 | type to the other by casting conversion. */ | |
b9f7e36c APB |
9963 | else if (op1 == null_pointer_node || op2 == null_pointer_node |
9964 | || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type) | |
5e942c50 APB |
9965 | && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1) |
9966 | || valid_ref_assignconv_cast_p (op2_type, | |
9967 | op1_type, 1)))) | |
e04a16fb AG |
9968 | ; /* Nothing to do here */ |
9969 | ||
9970 | /* Else we have an error figure what can't be converted into | |
9971 | what and report the error */ | |
9972 | else | |
9973 | { | |
9974 | char *t1; | |
c2e3db92 | 9975 | t1 = xstrdup (lang_printable_name (op1_type, 0)); |
e04a16fb AG |
9976 | parse_error_context |
9977 | (wfl_operator, "Incompatible type for `%s'. Can't convert `%s' " | |
9978 | "to `%s'", operator_string (node), t1, | |
0a2138e2 | 9979 | lang_printable_name (op2_type, 0)); |
e04a16fb AG |
9980 | free (t1); |
9981 | TREE_TYPE (node) = boolean_type_node; | |
9982 | error_found = 1; | |
9983 | break; | |
9984 | } | |
9985 | prom_type = boolean_type_node; | |
9986 | break; | |
9987 | } | |
9988 | ||
e04a16fb AG |
9989 | if (error_found) |
9990 | return error_mark_node; | |
9991 | ||
9992 | TREE_OPERAND (node, 0) = op1; | |
9993 | TREE_OPERAND (node, 1) = op2; | |
9994 | TREE_TYPE (node) = prom_type; | |
dc0b3eff PB |
9995 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); |
9996 | ||
ce6e9147 APB |
9997 | if (flag_emit_xref) |
9998 | return node; | |
9999 | ||
d1472141 PB |
10000 | /* fold does not respect side-effect order as required for Java but not C. |
10001 | * Also, it sometimes create SAVE_EXPRs which are bad when emitting | |
10002 | * bytecode. | |
10003 | */ | |
10004 | if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2)) | |
10005 | : ! TREE_SIDE_EFFECTS (node)) | |
aee48ef8 PB |
10006 | node = fold (node); |
10007 | return node; | |
e04a16fb AG |
10008 | } |
10009 | ||
b67d701b PB |
10010 | /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non |
10011 | zero value, the value of CSTE comes after the valude of STRING */ | |
10012 | ||
10013 | static tree | |
10014 | do_merge_string_cste (cste, string, string_len, after) | |
10015 | tree cste; | |
49f48c71 | 10016 | const char *string; |
b67d701b PB |
10017 | int string_len, after; |
10018 | { | |
10019 | int len = TREE_STRING_LENGTH (cste) + string_len; | |
49f48c71 | 10020 | const char *old = TREE_STRING_POINTER (cste); |
b67d701b PB |
10021 | TREE_STRING_LENGTH (cste) = len; |
10022 | TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1); | |
10023 | if (after) | |
10024 | { | |
10025 | strcpy (TREE_STRING_POINTER (cste), string); | |
10026 | strcat (TREE_STRING_POINTER (cste), old); | |
10027 | } | |
10028 | else | |
10029 | { | |
10030 | strcpy (TREE_STRING_POINTER (cste), old); | |
10031 | strcat (TREE_STRING_POINTER (cste), string); | |
10032 | } | |
10033 | return cste; | |
10034 | } | |
10035 | ||
10036 | /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a | |
10037 | new STRING_CST on success, NULL_TREE on failure */ | |
10038 | ||
10039 | static tree | |
10040 | merge_string_cste (op1, op2, after) | |
10041 | tree op1, op2; | |
10042 | int after; | |
10043 | { | |
10044 | /* Handle two string constants right away */ | |
10045 | if (TREE_CODE (op2) == STRING_CST) | |
10046 | return do_merge_string_cste (op1, TREE_STRING_POINTER (op2), | |
10047 | TREE_STRING_LENGTH (op2), after); | |
10048 | ||
10049 | /* Reasonable integer constant can be treated right away */ | |
10050 | if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2)) | |
10051 | { | |
49f48c71 KG |
10052 | static const char *boolean_true = "true"; |
10053 | static const char *boolean_false = "false"; | |
10054 | static const char *null_pointer = "null"; | |
b67d701b | 10055 | char ch[3]; |
49f48c71 | 10056 | const char *string; |
b67d701b PB |
10057 | |
10058 | if (op2 == boolean_true_node) | |
10059 | string = boolean_true; | |
10060 | else if (op2 == boolean_false_node) | |
10061 | string = boolean_false; | |
10062 | else if (op2 == null_pointer_node) | |
10063 | string = null_pointer; | |
10064 | else if (TREE_TYPE (op2) == char_type_node) | |
10065 | { | |
10066 | ch[0] = (char )TREE_INT_CST_LOW (op2); | |
10067 | ch[1] = '\0'; | |
10068 | string = ch; | |
10069 | } | |
10070 | else | |
10071 | string = print_int_node (op2); | |
10072 | ||
10073 | return do_merge_string_cste (op1, string, strlen (string), after); | |
10074 | } | |
10075 | return NULL_TREE; | |
10076 | } | |
10077 | ||
10078 | /* Tries to statically concatenate OP1 and OP2 if possible. Either one | |
10079 | has to be a STRING_CST and the other part must be a STRING_CST or a | |
10080 | INTEGRAL constant. Return a new STRING_CST if the operation | |
10081 | succeed, NULL_TREE otherwise. | |
10082 | ||
10083 | If the case we want to optimize for space, we might want to return | |
10084 | NULL_TREE for each invocation of this routine. FIXME */ | |
10085 | ||
10086 | static tree | |
10087 | string_constant_concatenation (op1, op2) | |
10088 | tree op1, op2; | |
10089 | { | |
10090 | if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST)) | |
10091 | { | |
0a2138e2 | 10092 | tree string, rest; |
b67d701b PB |
10093 | int invert; |
10094 | ||
10095 | string = (TREE_CODE (op1) == STRING_CST ? op1 : op2); | |
10096 | rest = (string == op1 ? op2 : op1); | |
10097 | invert = (string == op1 ? 0 : 1 ); | |
10098 | ||
10099 | /* Walk REST, only if it looks reasonable */ | |
10100 | if (TREE_CODE (rest) != STRING_CST | |
10101 | && !IS_CRAFTED_STRING_BUFFER_P (rest) | |
10102 | && !JSTRING_TYPE_P (TREE_TYPE (rest)) | |
10103 | && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION) | |
10104 | { | |
10105 | rest = java_complete_tree (rest); | |
10106 | if (rest == error_mark_node) | |
10107 | return error_mark_node; | |
10108 | rest = fold (rest); | |
10109 | } | |
10110 | return merge_string_cste (string, rest, invert); | |
10111 | } | |
10112 | return NULL_TREE; | |
10113 | } | |
10114 | ||
10115 | /* Implement the `+' operator. Does static optimization if possible, | |
10116 | otherwise create (if necessary) and append elements to a | |
10117 | StringBuffer. The StringBuffer will be carried around until it is | |
10118 | used for a function call or an assignment. Then toString() will be | |
10119 | called on it to turn it into a String object. */ | |
10120 | ||
10121 | static tree | |
10122 | build_string_concatenation (op1, op2) | |
10123 | tree op1, op2; | |
10124 | { | |
10125 | tree result; | |
dc0b3eff | 10126 | int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); |
ce6e9147 APB |
10127 | |
10128 | if (flag_emit_xref) | |
10129 | return build (PLUS_EXPR, string_type_node, op1, op2); | |
b67d701b PB |
10130 | |
10131 | /* Try to do some static optimization */ | |
10132 | if ((result = string_constant_concatenation (op1, op2))) | |
10133 | return result; | |
10134 | ||
c0d87ff6 PB |
10135 | /* Discard empty strings on either side of the expression */ |
10136 | if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0) | |
acd663ee APB |
10137 | { |
10138 | op1 = op2; | |
10139 | op2 = NULL_TREE; | |
10140 | } | |
c0d87ff6 | 10141 | else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0) |
acd663ee | 10142 | op2 = NULL_TREE; |
b67d701b | 10143 | |
acd663ee | 10144 | /* If operands are string constant, turn then into object references */ |
b67d701b PB |
10145 | if (TREE_CODE (op1) == STRING_CST) |
10146 | op1 = patch_string_cst (op1); | |
acd663ee | 10147 | if (op2 && TREE_CODE (op2) == STRING_CST) |
b67d701b PB |
10148 | op2 = patch_string_cst (op2); |
10149 | ||
acd663ee APB |
10150 | /* If either one of the constant is null and the other non null |
10151 | operand is a String object, return it. */ | |
10152 | if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2) | |
10153 | return op1; | |
10154 | ||
b67d701b PB |
10155 | /* If OP1 isn't already a StringBuffer, create and |
10156 | initialize a new one */ | |
10157 | if (!IS_CRAFTED_STRING_BUFFER_P (op1)) | |
10158 | { | |
10159 | /* Two solutions here: | |
10160 | 1) OP1 is a string reference, we call new StringBuffer(OP1) | |
acd663ee | 10161 | 2) OP1 is something else, we call new StringBuffer().append(OP1). */ |
b67d701b PB |
10162 | if (JSTRING_TYPE_P (TREE_TYPE (op1))) |
10163 | op1 = BUILD_STRING_BUFFER (op1); | |
10164 | else | |
10165 | { | |
10166 | tree aNew = BUILD_STRING_BUFFER (NULL_TREE); | |
10167 | op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0); | |
10168 | } | |
10169 | } | |
10170 | ||
acd663ee APB |
10171 | if (op2) |
10172 | { | |
10173 | /* OP1 is no longer the last node holding a crafted StringBuffer */ | |
10174 | IS_CRAFTED_STRING_BUFFER_P (op1) = 0; | |
10175 | /* Create a node for `{new...,xxx}.append (op2)' */ | |
10176 | if (op2) | |
10177 | op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0); | |
10178 | } | |
10179 | ||
b67d701b PB |
10180 | /* Mark the last node holding a crafted StringBuffer */ |
10181 | IS_CRAFTED_STRING_BUFFER_P (op1) = 1; | |
dc0b3eff PB |
10182 | |
10183 | TREE_SIDE_EFFECTS (op1) = side_effects; | |
b67d701b PB |
10184 | return op1; |
10185 | } | |
10186 | ||
10187 | /* Patch the string node NODE. NODE can be a STRING_CST of a crafted | |
10188 | StringBuffer. If no string were found to be patched, return | |
10189 | NULL. */ | |
10190 | ||
10191 | static tree | |
10192 | patch_string (node) | |
10193 | tree node; | |
10194 | { | |
1179ebc2 APB |
10195 | if (node == error_mark_node) |
10196 | return error_mark_node; | |
b67d701b PB |
10197 | if (TREE_CODE (node) == STRING_CST) |
10198 | return patch_string_cst (node); | |
10199 | else if (IS_CRAFTED_STRING_BUFFER_P (node)) | |
10200 | { | |
c877974e | 10201 | int saved = ctxp->explicit_constructor_p; |
b67d701b | 10202 | tree invoke = build_method_invocation (wfl_to_string, NULL_TREE); |
c877974e APB |
10203 | tree ret; |
10204 | /* Temporary disable forbid the use of `this'. */ | |
10205 | ctxp->explicit_constructor_p = 0; | |
10206 | ret = java_complete_tree (make_qualified_primary (node, invoke, 0)); | |
10207 | /* Restore it at its previous value */ | |
10208 | ctxp->explicit_constructor_p = saved; | |
10209 | return ret; | |
b67d701b PB |
10210 | } |
10211 | return NULL_TREE; | |
10212 | } | |
10213 | ||
10214 | /* Build the internal representation of a string constant. */ | |
10215 | ||
10216 | static tree | |
10217 | patch_string_cst (node) | |
10218 | tree node; | |
10219 | { | |
10220 | int location; | |
15fdcfe9 PB |
10221 | if (! flag_emit_class_files) |
10222 | { | |
10223 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
10224 | node = get_identifier (TREE_STRING_POINTER (node)); | |
10225 | location = alloc_name_constant (CONSTANT_String, node); | |
10226 | node = build_ref_from_constant_pool (location); | |
10227 | } | |
cd9643f7 | 10228 | TREE_TYPE (node) = string_ptr_type_node; |
b67d701b PB |
10229 | TREE_CONSTANT (node) = 1; |
10230 | return node; | |
10231 | } | |
10232 | ||
10233 | /* Build an incomplete unary operator expression. */ | |
e04a16fb AG |
10234 | |
10235 | static tree | |
10236 | build_unaryop (op_token, op_location, op1) | |
10237 | int op_token, op_location; | |
10238 | tree op1; | |
10239 | { | |
10240 | enum tree_code op; | |
10241 | tree unaryop; | |
10242 | switch (op_token) | |
10243 | { | |
b67d701b | 10244 | case PLUS_TK: op = UNARY_PLUS_EXPR; break; |
e04a16fb AG |
10245 | case MINUS_TK: op = NEGATE_EXPR; break; |
10246 | case NEG_TK: op = TRUTH_NOT_EXPR; break; | |
10247 | case NOT_TK: op = BIT_NOT_EXPR; break; | |
10248 | default: fatal ("Unknown token `%d' for unary operator - build_unaryop", | |
10249 | op_token); | |
10250 | } | |
10251 | ||
10252 | unaryop = build1 (op, NULL_TREE, op1); | |
e04a16fb AG |
10253 | TREE_SIDE_EFFECTS (unaryop) = 1; |
10254 | /* Store the location of the operator, for better error report. The | |
10255 | string of the operator will be rebuild based on the OP value. */ | |
10256 | EXPR_WFL_LINECOL (unaryop) = op_location; | |
10257 | return unaryop; | |
10258 | } | |
10259 | ||
10260 | /* Special case for the ++/-- operators, since they require an extra | |
10261 | argument to build, which is set to NULL and patched | |
10262 | later. IS_POST_P is 1 if the operator, 0 otherwise. */ | |
10263 | ||
10264 | static tree | |
10265 | build_incdec (op_token, op_location, op1, is_post_p) | |
10266 | int op_token, op_location; | |
10267 | tree op1; | |
10268 | int is_post_p; | |
10269 | { | |
10270 | static enum tree_code lookup [2][2] = | |
10271 | { | |
10272 | { PREDECREMENT_EXPR, PREINCREMENT_EXPR, }, | |
10273 | { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, }, | |
10274 | }; | |
10275 | tree node = build (lookup [is_post_p][(op_token - DECR_TK)], | |
10276 | NULL_TREE, op1, NULL_TREE); | |
10277 | TREE_SIDE_EFFECTS (node) = 1; | |
10278 | /* Store the location of the operator, for better error report. The | |
10279 | string of the operator will be rebuild based on the OP value. */ | |
10280 | EXPR_WFL_LINECOL (node) = op_location; | |
10281 | return node; | |
10282 | } | |
10283 | ||
10284 | /* Build an incomplete cast operator, based on the use of the | |
10285 | CONVERT_EXPR. Note that TREE_TYPE of the constructed node is | |
10286 | set. java_complete_tree is trained to walk a CONVERT_EXPR even | |
10287 | though its type is already set. */ | |
10288 | ||
10289 | static tree | |
10290 | build_cast (location, type, exp) | |
10291 | int location; | |
10292 | tree type, exp; | |
10293 | { | |
10294 | tree node = build1 (CONVERT_EXPR, type, exp); | |
10295 | EXPR_WFL_LINECOL (node) = location; | |
10296 | return node; | |
10297 | } | |
10298 | ||
10299 | /* 15.14 Unary operators. We return error_mark_node in case of error, | |
10300 | but preserve the type of NODE if the type is fixed. */ | |
10301 | ||
10302 | static tree | |
10303 | patch_unaryop (node, wfl_op) | |
10304 | tree node; | |
10305 | tree wfl_op; | |
10306 | { | |
10307 | tree op = TREE_OPERAND (node, 0); | |
10308 | tree op_type = TREE_TYPE (op); | |
ab3a6dd6 | 10309 | tree prom_type = NULL_TREE, value, decl; |
e04a16fb AG |
10310 | int code = TREE_CODE (node); |
10311 | int error_found = 0; | |
10312 | ||
10313 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
10314 | ||
10315 | switch (code) | |
10316 | { | |
10317 | /* 15.13.2 Postfix Increment Operator ++ */ | |
10318 | case POSTINCREMENT_EXPR: | |
10319 | /* 15.13.3 Postfix Increment Operator -- */ | |
10320 | case POSTDECREMENT_EXPR: | |
10321 | /* 15.14.1 Prefix Increment Operator ++ */ | |
10322 | case PREINCREMENT_EXPR: | |
10323 | /* 15.14.2 Prefix Decrement Operator -- */ | |
10324 | case PREDECREMENT_EXPR: | |
5cbdba64 | 10325 | op = decl = strip_out_static_field_access_decl (op); |
b3edebcf | 10326 | /* We really should have a JAVA_ARRAY_EXPR to avoid this */ |
e28cd97b | 10327 | if (!JDECL_P (decl) |
b3edebcf APB |
10328 | && TREE_CODE (decl) != COMPONENT_REF |
10329 | && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF) | |
10330 | && TREE_CODE (decl) != INDIRECT_REF | |
10331 | && !(TREE_CODE (decl) == COMPOUND_EXPR | |
10332 | && TREE_OPERAND (decl, 1) | |
10333 | && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF))) | |
e04a16fb | 10334 | { |
5e942c50 APB |
10335 | tree lvalue; |
10336 | /* Before screaming, check that we're not in fact trying to | |
10337 | increment a optimized static final access, in which case | |
10338 | we issue an different error message. */ | |
10339 | if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION | |
10340 | && resolve_expression_name (wfl_op, &lvalue) | |
10341 | && check_final_assignment (lvalue, wfl_op))) | |
10342 | parse_error_context (wfl_operator, "Invalid argument to `%s'", | |
10343 | operator_string (node)); | |
e04a16fb AG |
10344 | TREE_TYPE (node) = error_mark_node; |
10345 | error_found = 1; | |
10346 | } | |
5e942c50 APB |
10347 | else if (check_final_assignment (op, wfl_op)) |
10348 | error_found = 1; | |
10349 | ||
e04a16fb AG |
10350 | /* From now on, we know that op if a variable and that it has a |
10351 | valid wfl. We use wfl_op to locate errors related to the | |
10352 | ++/-- operand. */ | |
10353 | else if (!JNUMERIC_TYPE_P (op_type)) | |
10354 | { | |
10355 | parse_error_context | |
10356 | (wfl_op, "Invalid argument type `%s' to `%s'", | |
0a2138e2 | 10357 | lang_printable_name (op_type, 0), operator_string (node)); |
e04a16fb AG |
10358 | TREE_TYPE (node) = error_mark_node; |
10359 | error_found = 1; | |
10360 | } | |
10361 | else | |
10362 | { | |
4a5f66c3 | 10363 | /* Before the addition, binary numeric promotion is performed on |
5cbdba64 APB |
10364 | both operands, if really necessary */ |
10365 | if (JINTEGRAL_TYPE_P (op_type)) | |
10366 | { | |
10367 | value = build_int_2 (1, 0); | |
10368 | TREE_TYPE (value) = TREE_TYPE (node) = op_type; | |
10369 | } | |
10370 | else | |
10371 | { | |
10372 | value = build_int_2 (1, 0); | |
10373 | TREE_TYPE (node) = | |
10374 | binary_numeric_promotion (op_type, | |
10375 | TREE_TYPE (value), &op, &value); | |
10376 | } | |
10377 | /* And write back into the node. */ | |
4a5f66c3 | 10378 | TREE_OPERAND (node, 0) = op; |
e04a16fb | 10379 | TREE_OPERAND (node, 1) = value; |
5cbdba64 APB |
10380 | /* Convert the overall back into its original type, if |
10381 | necessary, and return */ | |
10382 | if (JINTEGRAL_TYPE_P (op_type)) | |
10383 | return fold (node); | |
10384 | else | |
10385 | return fold (convert (op_type, node)); | |
e04a16fb AG |
10386 | } |
10387 | break; | |
10388 | ||
10389 | /* 15.14.3 Unary Plus Operator + */ | |
b67d701b | 10390 | case UNARY_PLUS_EXPR: |
e04a16fb AG |
10391 | /* 15.14.4 Unary Minus Operator - */ |
10392 | case NEGATE_EXPR: | |
10393 | if (!JNUMERIC_TYPE_P (op_type)) | |
10394 | { | |
10395 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type); | |
10396 | TREE_TYPE (node) = error_mark_node; | |
10397 | error_found = 1; | |
10398 | } | |
10399 | /* Unary numeric promotion is performed on operand */ | |
10400 | else | |
10401 | { | |
15fdcfe9 PB |
10402 | op = do_unary_numeric_promotion (op); |
10403 | prom_type = TREE_TYPE (op); | |
b67d701b | 10404 | if (code == UNARY_PLUS_EXPR) |
4a5f66c3 | 10405 | return fold (op); |
e04a16fb AG |
10406 | } |
10407 | break; | |
10408 | ||
10409 | /* 15.14.5 Bitwise Complement Operator ~ */ | |
10410 | case BIT_NOT_EXPR: | |
10411 | if (!JINTEGRAL_TYPE_P (op_type)) | |
10412 | { | |
10413 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type); | |
10414 | TREE_TYPE (node) = error_mark_node; | |
10415 | error_found = 1; | |
10416 | } | |
10417 | else | |
10418 | { | |
15fdcfe9 PB |
10419 | op = do_unary_numeric_promotion (op); |
10420 | prom_type = TREE_TYPE (op); | |
e04a16fb AG |
10421 | } |
10422 | break; | |
10423 | ||
10424 | /* 15.14.6 Logical Complement Operator ! */ | |
10425 | case TRUTH_NOT_EXPR: | |
10426 | if (TREE_CODE (op_type) != BOOLEAN_TYPE) | |
10427 | { | |
10428 | ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type); | |
c877974e APB |
10429 | /* But the type is known. We will report an error if further |
10430 | attempt of a assignment is made with this rhs */ | |
e04a16fb AG |
10431 | TREE_TYPE (node) = boolean_type_node; |
10432 | error_found = 1; | |
10433 | } | |
10434 | else | |
10435 | prom_type = boolean_type_node; | |
10436 | break; | |
10437 | ||
10438 | /* 15.15 Cast Expression */ | |
10439 | case CONVERT_EXPR: | |
0a2138e2 | 10440 | value = patch_cast (node, wfl_operator); |
e04a16fb | 10441 | if (value == error_mark_node) |
c877974e APB |
10442 | { |
10443 | /* If this cast is part of an assignment, we tell the code | |
10444 | that deals with it not to complain about a mismatch, | |
10445 | because things have been cast, anyways */ | |
10446 | TREE_TYPE (node) = error_mark_node; | |
10447 | error_found = 1; | |
10448 | } | |
10449 | else | |
dc0b3eff PB |
10450 | { |
10451 | value = fold (value); | |
10452 | TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op); | |
10453 | return value; | |
10454 | } | |
e04a16fb AG |
10455 | break; |
10456 | } | |
10457 | ||
e04a16fb AG |
10458 | if (error_found) |
10459 | return error_mark_node; | |
4a5f66c3 APB |
10460 | |
10461 | /* There are cases where node has been replaced by something else | |
10462 | and we don't end up returning here: UNARY_PLUS_EXPR, | |
10463 | CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */ | |
7525cc04 | 10464 | TREE_OPERAND (node, 0) = fold (op); |
4a5f66c3 | 10465 | TREE_TYPE (node) = prom_type; |
dc0b3eff | 10466 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op); |
e04a16fb AG |
10467 | return fold (node); |
10468 | } | |
10469 | ||
10470 | /* Generic type resolution that sometimes takes place during node | |
10471 | patching. Returned the resolved type or generate an error | |
10472 | message. Return the resolved type or NULL_TREE. */ | |
10473 | ||
10474 | static tree | |
10475 | resolve_type_during_patch (type) | |
10476 | tree type; | |
10477 | { | |
10478 | if (unresolved_type_p (type, NULL)) | |
10479 | { | |
10480 | tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE); | |
10481 | if (!type_decl) | |
10482 | { | |
10483 | parse_error_context (type, | |
10484 | "Class `%s' not found in type declaration", | |
10485 | IDENTIFIER_POINTER (EXPR_WFL_NODE (type))); | |
10486 | return NULL_TREE; | |
10487 | } | |
10488 | else | |
5e942c50 APB |
10489 | { |
10490 | CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1; | |
10491 | return TREE_TYPE (type_decl); | |
10492 | } | |
e04a16fb AG |
10493 | } |
10494 | return type; | |
10495 | } | |
10496 | /* 5.5 Casting Conversion. error_mark_node is returned if an error is | |
10497 | found. Otherwise NODE or something meant to replace it is returned. */ | |
10498 | ||
10499 | static tree | |
0a2138e2 | 10500 | patch_cast (node, wfl_operator) |
e04a16fb | 10501 | tree node; |
e04a16fb AG |
10502 | tree wfl_operator; |
10503 | { | |
10504 | tree op = TREE_OPERAND (node, 0); | |
10505 | tree op_type = TREE_TYPE (op); | |
10506 | tree cast_type = TREE_TYPE (node); | |
10507 | char *t1; | |
10508 | ||
10509 | /* First resolve OP_TYPE if unresolved */ | |
10510 | if (!(cast_type = resolve_type_during_patch (cast_type))) | |
10511 | return error_mark_node; | |
10512 | ||
10513 | /* Check on cast that are proven correct at compile time */ | |
10514 | if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type)) | |
10515 | { | |
e04a16fb AG |
10516 | /* Same type */ |
10517 | if (cast_type == op_type) | |
10518 | return node; | |
10519 | ||
0b4d333e APB |
10520 | /* float and double type are converted to the original type main |
10521 | variant and then to the target type. */ | |
10522 | if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE) | |
10523 | op = convert (integer_type_node, op); | |
10524 | ||
e04a16fb AG |
10525 | /* Try widening/narowwing convertion. Potentially, things need |
10526 | to be worked out in gcc so we implement the extreme cases | |
10527 | correctly. fold_convert() needs to be fixed. */ | |
10528 | return convert (cast_type, op); | |
10529 | } | |
10530 | ||
0b4d333e APB |
10531 | /* It's also valid to cast a boolean into a boolean */ |
10532 | if (op_type == boolean_type_node && cast_type == boolean_type_node) | |
10533 | return node; | |
10534 | ||
5e942c50 APB |
10535 | /* null can be casted to references */ |
10536 | if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type)) | |
10537 | return build_null_of_type (cast_type); | |
10538 | ||
e04a16fb AG |
10539 | /* The remaining legal casts involve conversion between reference |
10540 | types. Check for their compile time correctness. */ | |
10541 | if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type) | |
09ed0f70 | 10542 | && valid_ref_assignconv_cast_p (op_type, cast_type, 1)) |
e04a16fb AG |
10543 | { |
10544 | TREE_TYPE (node) = promote_type (cast_type); | |
10545 | /* Now, the case can be determined correct at compile time if | |
10546 | OP_TYPE can be converted into CAST_TYPE by assignment | |
10547 | conversion (5.2) */ | |
10548 | ||
10549 | if (valid_ref_assignconv_cast_p (op_type, cast_type, 0)) | |
15fdcfe9 PB |
10550 | { |
10551 | TREE_SET_CODE (node, NOP_EXPR); | |
10552 | return node; | |
10553 | } | |
10554 | ||
10555 | if (flag_emit_class_files) | |
10556 | { | |
10557 | TREE_SET_CODE (node, CONVERT_EXPR); | |
10558 | return node; | |
10559 | } | |
e04a16fb AG |
10560 | |
10561 | /* The cast requires a run-time check */ | |
10562 | return build (CALL_EXPR, promote_type (cast_type), | |
10563 | build_address_of (soft_checkcast_node), | |
10564 | tree_cons (NULL_TREE, build_class_ref (cast_type), | |
10565 | build_tree_list (NULL_TREE, op)), | |
10566 | NULL_TREE); | |
10567 | } | |
10568 | ||
10569 | /* Any other casts are proven incorrect at compile time */ | |
c2e3db92 | 10570 | t1 = xstrdup (lang_printable_name (op_type, 0)); |
e04a16fb | 10571 | parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'", |
0a2138e2 | 10572 | t1, lang_printable_name (cast_type, 0)); |
e04a16fb AG |
10573 | free (t1); |
10574 | return error_mark_node; | |
10575 | } | |
10576 | ||
5e942c50 APB |
10577 | /* Build a null constant and give it the type TYPE. */ |
10578 | ||
10579 | static tree | |
10580 | build_null_of_type (type) | |
10581 | tree type; | |
10582 | { | |
10583 | tree node = build_int_2 (0, 0); | |
10584 | TREE_TYPE (node) = promote_type (type); | |
10585 | return node; | |
10586 | } | |
10587 | ||
e04a16fb AG |
10588 | /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't |
10589 | a list of indices. */ | |
10590 | static tree | |
10591 | build_array_ref (location, array, index) | |
10592 | int location; | |
10593 | tree array, index; | |
10594 | { | |
10595 | tree node = build (ARRAY_REF, NULL_TREE, array, index); | |
10596 | EXPR_WFL_LINECOL (node) = location; | |
10597 | return node; | |
10598 | } | |
10599 | ||
10600 | /* 15.12 Array Access Expression */ | |
10601 | ||
10602 | static tree | |
c877974e APB |
10603 | patch_array_ref (node) |
10604 | tree node; | |
e04a16fb AG |
10605 | { |
10606 | tree array = TREE_OPERAND (node, 0); | |
10607 | tree array_type = TREE_TYPE (array); | |
10608 | tree index = TREE_OPERAND (node, 1); | |
10609 | tree index_type = TREE_TYPE (index); | |
e04a16fb AG |
10610 | int error_found = 0; |
10611 | ||
10612 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
10613 | ||
e04a16fb AG |
10614 | if (TREE_CODE (array_type) == POINTER_TYPE) |
10615 | array_type = TREE_TYPE (array_type); | |
10616 | ||
10617 | /* The array reference must be an array */ | |
10618 | if (!TYPE_ARRAY_P (array_type)) | |
10619 | { | |
10620 | parse_error_context | |
10621 | (wfl_operator, "`[]' can only be applied to arrays. It can't be " | |
0a2138e2 | 10622 | "applied to `%s'", lang_printable_name (array_type, 0)); |
e04a16fb AG |
10623 | TREE_TYPE (node) = error_mark_node; |
10624 | error_found = 1; | |
10625 | } | |
10626 | ||
10627 | /* The array index underdoes unary numeric promotion. The promoted | |
10628 | type must be int */ | |
15fdcfe9 PB |
10629 | index = do_unary_numeric_promotion (index); |
10630 | if (TREE_TYPE (index) != int_type_node) | |
e04a16fb | 10631 | { |
1ebadc60 KG |
10632 | if (valid_cast_to_p (index_type, int_type_node)) |
10633 | parse_error_context (wfl_operator, "Incompatible type for `[]'. " | |
10634 | "Explicit cast needed to convert `%s' to `int'", | |
10635 | lang_printable_name (index_type, 0)); | |
10636 | else | |
10637 | parse_error_context (wfl_operator, "Incompatible type for `[]'. " | |
10638 | "Can't convert `%s' to `int'", | |
10639 | lang_printable_name (index_type, 0)); | |
e04a16fb AG |
10640 | TREE_TYPE (node) = error_mark_node; |
10641 | error_found = 1; | |
10642 | } | |
10643 | ||
e04a16fb AG |
10644 | if (error_found) |
10645 | return error_mark_node; | |
e04a16fb | 10646 | |
5e942c50 | 10647 | array_type = TYPE_ARRAY_ELEMENT (array_type); |
5e942c50 | 10648 | |
7f1d4866 | 10649 | if (flag_emit_class_files || flag_emit_xref) |
e04a16fb | 10650 | { |
15fdcfe9 PB |
10651 | TREE_OPERAND (node, 0) = array; |
10652 | TREE_OPERAND (node, 1) = index; | |
e04a16fb AG |
10653 | } |
10654 | else | |
939d7216 PB |
10655 | { |
10656 | /* The save_expr is for correct evaluation order. It would be cleaner | |
10657 | to use force_evaluation_order (see comment there), but that is | |
10658 | difficult when we also have to deal with bounds checking. */ | |
10659 | if (TREE_SIDE_EFFECTS (index)) | |
10660 | array = save_expr (array); | |
10661 | node = build_java_arrayaccess (array, array_type, index); | |
10662 | if (TREE_SIDE_EFFECTS (index)) | |
10663 | node = build (COMPOUND_EXPR, array_type, array, node); | |
10664 | } | |
e04a16fb AG |
10665 | TREE_TYPE (node) = array_type; |
10666 | return node; | |
10667 | } | |
10668 | ||
10669 | /* 15.9 Array Creation Expressions */ | |
10670 | ||
10671 | static tree | |
10672 | build_newarray_node (type, dims, extra_dims) | |
10673 | tree type; | |
10674 | tree dims; | |
10675 | int extra_dims; | |
10676 | { | |
10677 | tree node = | |
b67d701b | 10678 | build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims), |
e04a16fb | 10679 | build_int_2 (extra_dims, 0)); |
e04a16fb AG |
10680 | return node; |
10681 | } | |
10682 | ||
10683 | static tree | |
10684 | patch_newarray (node) | |
10685 | tree node; | |
10686 | { | |
10687 | tree type = TREE_OPERAND (node, 0); | |
10688 | tree dims = TREE_OPERAND (node, 1); | |
10689 | tree cdim, array_type; | |
10690 | int error_found = 0; | |
10691 | int ndims = 0; | |
10692 | int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2)); | |
e04a16fb AG |
10693 | |
10694 | /* Dimension types are verified. It's better for the types to be | |
10695 | verified in order. */ | |
10696 | for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ ) | |
10697 | { | |
10698 | int dim_error = 0; | |
10699 | tree dim = TREE_VALUE (cdim); | |
10700 | ||
10701 | /* Dim might have been saved during its evaluation */ | |
10702 | dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim); | |
10703 | ||
10704 | /* The type of each specified dimension must be an integral type. */ | |
10705 | if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim))) | |
10706 | dim_error = 1; | |
10707 | ||
10708 | /* Each expression undergoes an unary numeric promotion (5.6.1) and the | |
10709 | promoted type must be int. */ | |
10710 | else | |
10711 | { | |
15fdcfe9 | 10712 | dim = do_unary_numeric_promotion (dim); |
e04a16fb AG |
10713 | if (TREE_TYPE (dim) != int_type_node) |
10714 | dim_error = 1; | |
10715 | } | |
10716 | ||
10717 | /* Report errors on types here */ | |
10718 | if (dim_error) | |
10719 | { | |
10720 | parse_error_context | |
10721 | (TREE_PURPOSE (cdim), | |
10722 | "Incompatible type for dimension in array creation expression. " | |
10723 | "%s convert `%s' to `int'", | |
b67d701b | 10724 | (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ? |
e04a16fb | 10725 | "Explicit cast needed to" : "Can't"), |
0a2138e2 | 10726 | lang_printable_name (TREE_TYPE (dim), 0)); |
e04a16fb AG |
10727 | error_found = 1; |
10728 | } | |
10729 | ||
e04a16fb AG |
10730 | TREE_PURPOSE (cdim) = NULL_TREE; |
10731 | } | |
10732 | ||
10733 | /* Resolve array base type if unresolved */ | |
10734 | if (!(type = resolve_type_during_patch (type))) | |
10735 | error_found = 1; | |
10736 | ||
10737 | if (error_found) | |
10738 | { | |
10739 | /* We don't want further evaluation of this bogus array creation | |
10740 | operation */ | |
10741 | TREE_TYPE (node) = error_mark_node; | |
10742 | return error_mark_node; | |
10743 | } | |
10744 | ||
15fdcfe9 PB |
10745 | /* Set array_type to the actual (promoted) array type of the result. */ |
10746 | if (TREE_CODE (type) == RECORD_TYPE) | |
10747 | type = build_pointer_type (type); | |
10748 | while (--xdims >= 0) | |
10749 | { | |
10750 | type = promote_type (build_java_array_type (type, -1)); | |
10751 | } | |
10752 | dims = nreverse (dims); | |
10753 | array_type = type; | |
10754 | for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim)) | |
10755 | { | |
10756 | type = array_type; | |
10757 | array_type = build_java_array_type (type, | |
10758 | TREE_CODE (cdim) == INTEGER_CST ? | |
10759 | TREE_INT_CST_LOW (cdim) : -1); | |
10760 | array_type = promote_type (array_type); | |
10761 | } | |
10762 | dims = nreverse (dims); | |
10763 | ||
e04a16fb AG |
10764 | /* The node is transformed into a function call. Things are done |
10765 | differently according to the number of dimensions. If the number | |
10766 | of dimension is equal to 1, then the nature of the base type | |
10767 | (primitive or not) matters. */ | |
15fdcfe9 | 10768 | if (ndims == 1) |
fdec99c6 | 10769 | return build_new_array (type, TREE_VALUE (dims)); |
e04a16fb | 10770 | |
e04a16fb AG |
10771 | /* Can't reuse what's already written in expr.c because it uses the |
10772 | JVM stack representation. Provide a build_multianewarray. FIXME */ | |
15fdcfe9 | 10773 | return build (CALL_EXPR, array_type, |
e04a16fb | 10774 | build_address_of (soft_multianewarray_node), |
15fdcfe9 | 10775 | tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)), |
e04a16fb | 10776 | tree_cons (NULL_TREE, |
15fdcfe9 | 10777 | build_int_2 (ndims, 0), dims )), |
e04a16fb AG |
10778 | NULL_TREE); |
10779 | } | |
10780 | ||
f8976021 APB |
10781 | /* 10.6 Array initializer. */ |
10782 | ||
10783 | /* Build a wfl for array element that don't have one, so we can | |
10784 | pin-point errors. */ | |
10785 | ||
10786 | static tree | |
10787 | maybe_build_array_element_wfl (node) | |
10788 | tree node; | |
10789 | { | |
10790 | if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION) | |
10791 | return build_expr_wfl (NULL_TREE, ctxp->filename, | |
10792 | ctxp->elc.line, ctxp->elc.prev_col); | |
10793 | else | |
10794 | return NULL_TREE; | |
10795 | } | |
10796 | ||
10797 | /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes | |
10798 | identification of initialized arrays easier to detect during walk | |
10799 | and expansion. */ | |
10800 | ||
10801 | static tree | |
10802 | build_new_array_init (location, values) | |
10803 | int location; | |
10804 | tree values; | |
10805 | { | |
10806 | tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values); | |
10807 | tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor); | |
5bba4807 | 10808 | EXPR_WFL_LINECOL (to_return) = location; |
f8976021 APB |
10809 | return to_return; |
10810 | } | |
10811 | ||
10812 | /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error | |
10813 | occurred. Otherwise return NODE after having set its type | |
10814 | appropriately. */ | |
10815 | ||
10816 | static tree | |
10817 | patch_new_array_init (type, node) | |
10818 | tree type, node; | |
f8976021 APB |
10819 | { |
10820 | int error_seen = 0; | |
fdec99c6 | 10821 | tree current, element_type; |
f8976021 | 10822 | HOST_WIDE_INT length; |
fdec99c6 PB |
10823 | int all_constant = 1; |
10824 | tree init = TREE_OPERAND (node, 0); | |
f8976021 | 10825 | |
fdec99c6 PB |
10826 | if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type))) |
10827 | { | |
10828 | parse_error_context (node, | |
10829 | "Invalid array initializer for non-array type `%s'", | |
10830 | lang_printable_name (type, 1)); | |
10831 | return error_mark_node; | |
10832 | } | |
10833 | type = TREE_TYPE (type); | |
10834 | element_type = TYPE_ARRAY_ELEMENT (type); | |
f8976021 | 10835 | |
fdec99c6 PB |
10836 | CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init)); |
10837 | ||
10838 | for (length = 0, current = CONSTRUCTOR_ELTS (init); | |
10839 | current; length++, current = TREE_CHAIN (current)) | |
f8976021 | 10840 | { |
fdec99c6 PB |
10841 | tree elt = TREE_VALUE (current); |
10842 | if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT) | |
f8976021 | 10843 | { |
fdec99c6 | 10844 | error_seen |= array_constructor_check_entry (element_type, current); |
5bba4807 PB |
10845 | elt = TREE_VALUE (current); |
10846 | /* When compiling to native code, STRING_CST is converted to | |
10847 | INDIRECT_REF, but still with a TREE_CONSTANT flag. */ | |
10848 | if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF) | |
fdec99c6 | 10849 | all_constant = 0; |
f8976021 | 10850 | } |
fdec99c6 PB |
10851 | else |
10852 | { | |
10853 | TREE_VALUE (current) = patch_new_array_init (element_type, elt); | |
10854 | TREE_PURPOSE (current) = NULL_TREE; | |
10855 | all_constant = 0; | |
10856 | } | |
10857 | if (elt && TREE_VALUE (elt) == error_mark_node) | |
10858 | error_seen = 1; | |
f8976021 APB |
10859 | } |
10860 | ||
10861 | if (error_seen) | |
10862 | return error_mark_node; | |
10863 | ||
10864 | /* Create a new type. We can't reuse the one we have here by | |
10865 | patching its dimension because it originally is of dimension -1 | |
10866 | hence reused by gcc. This would prevent triangular arrays. */ | |
fdec99c6 PB |
10867 | type = build_java_array_type (element_type, length); |
10868 | TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type)))); | |
10869 | TREE_TYPE (node) = promote_type (type); | |
10870 | TREE_CONSTANT (init) = all_constant; | |
bc3ca41b | 10871 | TREE_CONSTANT (node) = all_constant; |
f8976021 APB |
10872 | return node; |
10873 | } | |
10874 | ||
10875 | /* Verify that one entry of the initializer element list can be | |
10876 | assigned to the array base type. Report 1 if an error occurred, 0 | |
10877 | otherwise. */ | |
10878 | ||
10879 | static int | |
10880 | array_constructor_check_entry (type, entry) | |
10881 | tree type, entry; | |
10882 | { | |
10883 | char *array_type_string = NULL; /* For error reports */ | |
10884 | tree value, type_value, new_value, wfl_value, patched; | |
10885 | int error_seen = 0; | |
10886 | ||
10887 | new_value = NULL_TREE; | |
10888 | wfl_value = TREE_VALUE (entry); | |
10889 | ||
100f7cd8 | 10890 | push_obstacks (&permanent_obstack, &permanent_obstack); |
f8976021 | 10891 | value = java_complete_tree (TREE_VALUE (entry)); |
1179ebc2 | 10892 | /* patch_string return error_mark_node if arg is error_mark_node */ |
f8976021 APB |
10893 | if ((patched = patch_string (value))) |
10894 | value = patched; | |
1179ebc2 APB |
10895 | if (value == error_mark_node) |
10896 | return 1; | |
f8976021 | 10897 | |
f8976021 APB |
10898 | type_value = TREE_TYPE (value); |
10899 | ||
1179ebc2 | 10900 | /* At anytime, try_builtin_assignconv can report a warning on |
f8976021 APB |
10901 | constant overflow during narrowing. */ |
10902 | SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value); | |
10903 | new_value = try_builtin_assignconv (wfl_operator, type, value); | |
10904 | if (!new_value && (new_value = try_reference_assignconv (type, value))) | |
10905 | type_value = promote_type (type); | |
100f7cd8 APB |
10906 | |
10907 | pop_obstacks (); | |
f8976021 APB |
10908 | /* Check and report errors */ |
10909 | if (!new_value) | |
10910 | { | |
49f48c71 | 10911 | const char *msg = (!valid_cast_to_p (type_value, type) ? |
f8976021 APB |
10912 | "Can't" : "Explicit cast needed to"); |
10913 | if (!array_type_string) | |
c2e3db92 | 10914 | array_type_string = xstrdup (lang_printable_name (type, 1)); |
f8976021 APB |
10915 | parse_error_context |
10916 | (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'", | |
10917 | msg, lang_printable_name (type_value, 1), array_type_string); | |
10918 | error_seen = 1; | |
10919 | } | |
10920 | ||
10921 | if (new_value) | |
10922 | { | |
10923 | new_value = maybe_build_primttype_type_ref (new_value, wfl_operator); | |
10924 | TREE_VALUE (entry) = new_value; | |
10925 | } | |
10926 | ||
10927 | if (array_type_string) | |
10928 | free (array_type_string); | |
10929 | ||
10930 | TREE_PURPOSE (entry) = NULL_TREE; | |
10931 | return error_seen; | |
10932 | } | |
10933 | ||
e04a16fb AG |
10934 | static tree |
10935 | build_this (location) | |
10936 | int location; | |
10937 | { | |
9ee9b555 | 10938 | tree node = build_wfl_node (this_identifier_node); |
b67d701b | 10939 | TREE_SET_CODE (node, THIS_EXPR); |
e04a16fb AG |
10940 | EXPR_WFL_LINECOL (node) = location; |
10941 | return node; | |
10942 | } | |
10943 | ||
10944 | /* 14.15 The return statement. It builds a modify expression that | |
10945 | assigns the returned value to the RESULT_DECL that hold the value | |
10946 | to be returned. */ | |
10947 | ||
10948 | static tree | |
10949 | build_return (location, op) | |
10950 | int location; | |
10951 | tree op; | |
10952 | { | |
10953 | tree node = build1 (RETURN_EXPR, NULL_TREE, op); | |
10954 | EXPR_WFL_LINECOL (node) = location; | |
b67d701b | 10955 | node = build_debugable_stmt (location, node); |
e04a16fb AG |
10956 | return node; |
10957 | } | |
10958 | ||
10959 | static tree | |
10960 | patch_return (node) | |
10961 | tree node; | |
10962 | { | |
10963 | tree return_exp = TREE_OPERAND (node, 0); | |
10964 | tree meth = current_function_decl; | |
10965 | tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl)); | |
e04a16fb AG |
10966 | int error_found = 0; |
10967 | ||
10968 | TREE_TYPE (node) = error_mark_node; | |
10969 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
10970 | ||
10971 | /* It's invalid to have a return value within a function that is | |
10972 | declared with the keyword void or that is a constructor */ | |
10973 | if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth))) | |
10974 | error_found = 1; | |
10975 | ||
f099f336 | 10976 | /* It's invalid to use a return statement in a static block */ |
7f1d4866 | 10977 | if (IS_CLINIT (current_function_decl)) |
f099f336 APB |
10978 | error_found = 1; |
10979 | ||
e04a16fb AG |
10980 | /* It's invalid to have a no return value within a function that |
10981 | isn't declared with the keyword `void' */ | |
10982 | if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth))) | |
10983 | error_found = 2; | |
10984 | ||
10985 | if (error_found) | |
10986 | { | |
7f1d4866 | 10987 | if (IS_CLINIT (current_function_decl)) |
f099f336 APB |
10988 | parse_error_context (wfl_operator, |
10989 | "`return' inside static initializer."); | |
10990 | ||
10991 | else if (!DECL_CONSTRUCTOR_P (meth)) | |
22eed1e6 | 10992 | { |
c2e3db92 | 10993 | char *t = xstrdup (lang_printable_name (mtype, 0)); |
22eed1e6 APB |
10994 | parse_error_context (wfl_operator, |
10995 | "`return' with%s value from `%s %s'", | |
10996 | (error_found == 1 ? "" : "out"), | |
10997 | t, lang_printable_name (meth, 0)); | |
10998 | free (t); | |
10999 | } | |
11000 | else | |
11001 | parse_error_context (wfl_operator, | |
11002 | "`return' with value from constructor `%s'", | |
11003 | lang_printable_name (meth, 0)); | |
e04a16fb AG |
11004 | return error_mark_node; |
11005 | } | |
11006 | ||
5e942c50 APB |
11007 | /* If we have a return_exp, build a modify expression and expand |
11008 | it. Note: at that point, the assignment is declared valid, but we | |
11009 | may want to carry some more hacks */ | |
e04a16fb AG |
11010 | if (return_exp) |
11011 | { | |
5e942c50 APB |
11012 | tree exp = java_complete_tree (return_exp); |
11013 | tree modify, patched; | |
11014 | ||
11015 | /* If the function returned value and EXP are booleans, EXP has | |
11016 | to be converted into the type of DECL_RESULT, which is integer | |
11017 | (see complete_start_java_method) */ | |
11018 | if (TREE_TYPE (exp) == boolean_type_node && | |
11019 | TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node) | |
11020 | exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp); | |
11021 | ||
11022 | /* `null' can be assigned to a function returning a reference */ | |
11023 | if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) && | |
11024 | exp == null_pointer_node) | |
11025 | exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth))); | |
11026 | ||
11027 | if ((patched = patch_string (exp))) | |
11028 | exp = patched; | |
11029 | ||
11030 | modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp); | |
e04a16fb AG |
11031 | EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node); |
11032 | modify = java_complete_tree (modify); | |
5e942c50 | 11033 | |
e04a16fb AG |
11034 | if (modify != error_mark_node) |
11035 | { | |
11036 | TREE_SIDE_EFFECTS (modify) = 1; | |
11037 | TREE_OPERAND (node, 0) = modify; | |
11038 | } | |
11039 | else | |
11040 | return error_mark_node; | |
11041 | } | |
11042 | TREE_TYPE (node) = void_type_node; | |
11043 | TREE_SIDE_EFFECTS (node) = 1; | |
11044 | return node; | |
11045 | } | |
11046 | ||
11047 | /* 14.8 The if Statement */ | |
11048 | ||
11049 | static tree | |
11050 | build_if_else_statement (location, expression, if_body, else_body) | |
11051 | int location; | |
11052 | tree expression, if_body, else_body; | |
11053 | { | |
11054 | tree node; | |
e04a16fb | 11055 | if (!else_body) |
9bbc7d9f | 11056 | else_body = empty_stmt_node; |
e04a16fb AG |
11057 | node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body); |
11058 | EXPR_WFL_LINECOL (node) = location; | |
b67d701b | 11059 | node = build_debugable_stmt (location, node); |
e04a16fb AG |
11060 | return node; |
11061 | } | |
11062 | ||
11063 | static tree | |
11064 | patch_if_else_statement (node) | |
11065 | tree node; | |
11066 | { | |
11067 | tree expression = TREE_OPERAND (node, 0); | |
11068 | ||
11069 | TREE_TYPE (node) = error_mark_node; | |
11070 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11071 | ||
11072 | /* The type of expression must be boolean */ | |
b67d701b PB |
11073 | if (TREE_TYPE (expression) != boolean_type_node |
11074 | && TREE_TYPE (expression) != promoted_boolean_type_node) | |
e04a16fb AG |
11075 | { |
11076 | parse_error_context | |
11077 | (wfl_operator, | |
11078 | "Incompatible type for `if'. Can't convert `%s' to `boolean'", | |
0a2138e2 | 11079 | lang_printable_name (TREE_TYPE (expression), 0)); |
e04a16fb AG |
11080 | return error_mark_node; |
11081 | } | |
11082 | ||
11083 | TREE_TYPE (node) = void_type_node; | |
11084 | TREE_SIDE_EFFECTS (node) = 1; | |
15fdcfe9 | 11085 | CAN_COMPLETE_NORMALLY (node) |
9bbc7d9f PB |
11086 | = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)) |
11087 | | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)); | |
e04a16fb AG |
11088 | return node; |
11089 | } | |
11090 | ||
11091 | /* 14.6 Labeled Statements */ | |
11092 | ||
11093 | /* Action taken when a lableled statement is parsed. a new | |
11094 | LABELED_BLOCK_EXPR is created. No statement is attached to the | |
b635eb2f | 11095 | label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */ |
e04a16fb AG |
11096 | |
11097 | static tree | |
0a2138e2 | 11098 | build_labeled_block (location, label) |
e04a16fb | 11099 | int location; |
0a2138e2 | 11100 | tree label; |
e04a16fb | 11101 | { |
b635eb2f | 11102 | tree label_name ; |
e04a16fb | 11103 | tree label_decl, node; |
b635eb2f PB |
11104 | if (label == NULL_TREE || label == continue_identifier_node) |
11105 | label_name = label; | |
11106 | else | |
e04a16fb | 11107 | { |
b635eb2f PB |
11108 | label_name = merge_qualified_name (label_id, label); |
11109 | /* Issue an error if we try to reuse a label that was previously | |
11110 | declared */ | |
11111 | if (IDENTIFIER_LOCAL_VALUE (label_name)) | |
11112 | { | |
11113 | EXPR_WFL_LINECOL (wfl_operator) = location; | |
11114 | parse_error_context (wfl_operator, "Declaration of `%s' shadows " | |
11115 | "a previous label declaration", | |
11116 | IDENTIFIER_POINTER (label)); | |
11117 | EXPR_WFL_LINECOL (wfl_operator) = | |
11118 | EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name)); | |
11119 | parse_error_context (wfl_operator, "This is the location of the " | |
11120 | "previous declaration of label `%s'", | |
11121 | IDENTIFIER_POINTER (label)); | |
11122 | java_error_count--; | |
11123 | } | |
e04a16fb AG |
11124 | } |
11125 | ||
11126 | label_decl = create_label_decl (label_name); | |
11127 | node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE); | |
11128 | EXPR_WFL_LINECOL (node) = location; | |
11129 | TREE_SIDE_EFFECTS (node) = 1; | |
11130 | return node; | |
11131 | } | |
11132 | ||
b67d701b | 11133 | /* A labeled statement LBE is attached a statement. */ |
e04a16fb AG |
11134 | |
11135 | static tree | |
b635eb2f | 11136 | finish_labeled_statement (lbe, statement) |
e04a16fb AG |
11137 | tree lbe; /* Labeled block expr */ |
11138 | tree statement; | |
11139 | { | |
11140 | /* In anyways, tie the loop to its statement */ | |
11141 | LABELED_BLOCK_BODY (lbe) = statement; | |
b635eb2f PB |
11142 | pop_labeled_block (); |
11143 | POP_LABELED_BLOCK (); | |
e04a16fb AG |
11144 | return lbe; |
11145 | } | |
11146 | ||
11147 | /* 14.10, 14.11, 14.12 Loop Statements */ | |
11148 | ||
11149 | /* Create an empty LOOP_EXPR and make it the last in the nested loop | |
11150 | list. */ | |
11151 | ||
11152 | static tree | |
11153 | build_new_loop (loop_body) | |
11154 | tree loop_body; | |
11155 | { | |
11156 | tree loop = build (LOOP_EXPR, NULL_TREE, loop_body); | |
11157 | TREE_SIDE_EFFECTS (loop) = 1; | |
11158 | PUSH_LOOP (loop); | |
11159 | return loop; | |
11160 | } | |
11161 | ||
11162 | /* Create a loop body according to the following structure: | |
11163 | COMPOUND_EXPR | |
11164 | COMPOUND_EXPR (loop main body) | |
11165 | EXIT_EXPR (this order is for while/for loops. | |
11166 | LABELED_BLOCK_EXPR the order is reversed for do loops) | |
34f4db93 | 11167 | LABEL_DECL (a continue occuring here branches at the |
e04a16fb AG |
11168 | BODY end of this labeled block) |
11169 | INCREMENT (if any) | |
11170 | ||
11171 | REVERSED, if non zero, tells that the loop condition expr comes | |
b67d701b PB |
11172 | after the body, like in the do-while loop. |
11173 | ||
11174 | To obtain a loop, the loop body structure described above is | |
11175 | encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR: | |
11176 | ||
11177 | LABELED_BLOCK_EXPR | |
11178 | LABEL_DECL (use this label to exit the loop) | |
11179 | LOOP_EXPR | |
11180 | <structure described above> */ | |
e04a16fb AG |
11181 | |
11182 | static tree | |
11183 | build_loop_body (location, condition, reversed) | |
11184 | int location; | |
11185 | tree condition; | |
11186 | int reversed; | |
11187 | { | |
0a2138e2 | 11188 | tree first, second, body; |
e04a16fb AG |
11189 | |
11190 | condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */ | |
11191 | EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */ | |
11192 | condition = build_debugable_stmt (location, condition); | |
11193 | TREE_SIDE_EFFECTS (condition) = 1; | |
11194 | ||
b635eb2f | 11195 | body = build_labeled_block (0, continue_identifier_node); |
e04a16fb AG |
11196 | first = (reversed ? body : condition); |
11197 | second = (reversed ? condition : body); | |
11198 | return | |
11199 | build (COMPOUND_EXPR, NULL_TREE, | |
9bbc7d9f | 11200 | build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node); |
e04a16fb AG |
11201 | } |
11202 | ||
11203 | /* Install CONDITION (if any) and loop BODY (using REVERSED to tell | |
11204 | their order) on the current loop. Unlink the current loop from the | |
11205 | loop list. */ | |
11206 | ||
11207 | static tree | |
b635eb2f | 11208 | finish_loop_body (location, condition, body, reversed) |
e04a16fb AG |
11209 | int location; |
11210 | tree condition, body; | |
11211 | int reversed; | |
11212 | { | |
11213 | tree to_return = ctxp->current_loop; | |
11214 | tree loop_body = LOOP_EXPR_BODY (to_return); | |
11215 | if (condition) | |
11216 | { | |
11217 | tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed); | |
11218 | /* We wrapped the EXIT_EXPR around a WFL so we can debug it. | |
11219 | The real EXIT_EXPR is one operand further. */ | |
11220 | EXPR_WFL_LINECOL (cnode) = location; | |
11221 | /* This one is for accurate error reports */ | |
11222 | EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location; | |
11223 | TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition; | |
11224 | } | |
11225 | LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body; | |
11226 | POP_LOOP (); | |
11227 | return to_return; | |
11228 | } | |
11229 | ||
b635eb2f | 11230 | /* Tailored version of finish_loop_body for FOR loops, when FOR |
e04a16fb AG |
11231 | loops feature the condition part */ |
11232 | ||
11233 | static tree | |
b635eb2f | 11234 | finish_for_loop (location, condition, update, body) |
e04a16fb AG |
11235 | int location; |
11236 | tree condition, update, body; | |
11237 | { | |
11238 | /* Put the condition and the loop body in place */ | |
b635eb2f | 11239 | tree loop = finish_loop_body (location, condition, body, 0); |
e04a16fb AG |
11240 | /* LOOP is the current loop which has been now popped of the loop |
11241 | stack. Install the update block */ | |
11242 | LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update; | |
11243 | return loop; | |
11244 | } | |
11245 | ||
5cbdba64 APB |
11246 | /* Try to find the loop a block might be related to. This comprises |
11247 | the case where the LOOP_EXPR is found as the second operand of a | |
11248 | COMPOUND_EXPR, because the loop happens to have an initialization | |
11249 | part, then expressed as the first operand of the COMPOUND_EXPR. If | |
11250 | the search finds something, 1 is returned. Otherwise, 0 is | |
11251 | returned. The search is assumed to start from a | |
11252 | LABELED_BLOCK_EXPR's block. */ | |
11253 | ||
11254 | static tree | |
11255 | search_loop (statement) | |
11256 | tree statement; | |
11257 | { | |
11258 | if (TREE_CODE (statement) == LOOP_EXPR) | |
11259 | return statement; | |
11260 | ||
11261 | if (TREE_CODE (statement) == BLOCK) | |
11262 | statement = BLOCK_SUBBLOCKS (statement); | |
11263 | else | |
11264 | return NULL_TREE; | |
11265 | ||
11266 | if (statement && TREE_CODE (statement) == COMPOUND_EXPR) | |
11267 | while (statement && TREE_CODE (statement) == COMPOUND_EXPR) | |
11268 | statement = TREE_OPERAND (statement, 1); | |
11269 | ||
11270 | return (TREE_CODE (statement) == LOOP_EXPR | |
11271 | && IS_FOR_LOOP_P (statement) ? statement : NULL_TREE); | |
11272 | } | |
11273 | ||
11274 | /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is | |
11275 | returned otherwise. */ | |
11276 | ||
11277 | static int | |
11278 | labeled_block_contains_loop_p (block, loop) | |
11279 | tree block, loop; | |
11280 | { | |
11281 | if (!block) | |
11282 | return 0; | |
11283 | ||
11284 | if (LABELED_BLOCK_BODY (block) == loop) | |
11285 | return 1; | |
11286 | ||
11287 | if (IS_FOR_LOOP_P (loop) | |
11288 | && search_loop (LABELED_BLOCK_BODY (block)) == loop) | |
11289 | return 1; | |
11290 | ||
11291 | return 0; | |
11292 | } | |
11293 | ||
e04a16fb | 11294 | /* If the loop isn't surrounded by a labeled statement, create one and |
b635eb2f | 11295 | insert LOOP as its body. */ |
e04a16fb AG |
11296 | |
11297 | static tree | |
11298 | patch_loop_statement (loop) | |
11299 | tree loop; | |
11300 | { | |
cd9643f7 | 11301 | tree loop_label; |
5cbdba64 | 11302 | |
cd9643f7 | 11303 | TREE_TYPE (loop) = void_type_node; |
5cbdba64 APB |
11304 | if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop)) |
11305 | return loop; | |
11306 | ||
cd9643f7 | 11307 | loop_label = build_labeled_block (0, NULL_TREE); |
5cbdba64 APB |
11308 | /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL |
11309 | that LOOP_LABEL could enquire about, for a better accuracy. FIXME */ | |
cd9643f7 PB |
11310 | LABELED_BLOCK_BODY (loop_label) = loop; |
11311 | PUSH_LABELED_BLOCK (loop_label); | |
5cbdba64 | 11312 | return loop_label; |
e04a16fb AG |
11313 | } |
11314 | ||
11315 | /* 14.13, 14.14: break and continue Statements */ | |
11316 | ||
11317 | /* Build a break or a continue statement. a null NAME indicates an | |
11318 | unlabeled break/continue statement. */ | |
11319 | ||
11320 | static tree | |
11321 | build_bc_statement (location, is_break, name) | |
11322 | int location, is_break; | |
11323 | tree name; | |
11324 | { | |
11325 | tree break_continue, label_block_expr = NULL_TREE; | |
11326 | ||
11327 | if (name) | |
11328 | { | |
11329 | if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE | |
11330 | (merge_qualified_name (label_id, EXPR_WFL_NODE (name))))) | |
11331 | /* Null means that we don't have a target for this named | |
11332 | break/continue. In this case, we make the target to be the | |
11333 | label name, so that the error can be reported accuratly in | |
11334 | patch_bc_statement. */ | |
11335 | label_block_expr = EXPR_WFL_NODE (name); | |
11336 | } | |
11337 | /* Unlabeled break/continue will be handled during the | |
11338 | break/continue patch operation */ | |
11339 | break_continue | |
11340 | = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE); | |
11341 | ||
11342 | IS_BREAK_STMT_P (break_continue) = is_break; | |
11343 | TREE_SIDE_EFFECTS (break_continue) = 1; | |
11344 | EXPR_WFL_LINECOL (break_continue) = location; | |
b67d701b | 11345 | break_continue = build_debugable_stmt (location, break_continue); |
e04a16fb AG |
11346 | return break_continue; |
11347 | } | |
11348 | ||
11349 | /* Verification of a break/continue statement. */ | |
11350 | ||
11351 | static tree | |
11352 | patch_bc_statement (node) | |
11353 | tree node; | |
11354 | { | |
11355 | tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt; | |
b635eb2f | 11356 | tree labeled_block = ctxp->current_labeled_block; |
b67d701b | 11357 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); |
e04a16fb | 11358 | |
e04a16fb | 11359 | /* Having an identifier here means that the target is unknown. */ |
b635eb2f | 11360 | if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE) |
e04a16fb AG |
11361 | { |
11362 | parse_error_context (wfl_operator, "No label definition found for `%s'", | |
11363 | IDENTIFIER_POINTER (bc_label)); | |
11364 | return error_mark_node; | |
11365 | } | |
b635eb2f | 11366 | if (! IS_BREAK_STMT_P (node)) |
e04a16fb | 11367 | { |
b635eb2f PB |
11368 | /* It's a continue statement. */ |
11369 | for (;; labeled_block = TREE_CHAIN (labeled_block)) | |
e04a16fb | 11370 | { |
b635eb2f PB |
11371 | if (labeled_block == NULL_TREE) |
11372 | { | |
11373 | if (bc_label == NULL_TREE) | |
11374 | parse_error_context (wfl_operator, | |
11375 | "`continue' must be in loop"); | |
11376 | else | |
1504b2b4 APB |
11377 | parse_error_context |
11378 | (wfl_operator, "continue label `%s' does not name a loop", | |
11379 | IDENTIFIER_POINTER (bc_label)); | |
b635eb2f PB |
11380 | return error_mark_node; |
11381 | } | |
11382 | if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block)) | |
11383 | == continue_identifier_node) | |
11384 | && (bc_label == NULL_TREE | |
11385 | || TREE_CHAIN (labeled_block) == bc_label)) | |
11386 | { | |
11387 | bc_label = labeled_block; | |
11388 | break; | |
11389 | } | |
e04a16fb | 11390 | } |
e04a16fb | 11391 | } |
b635eb2f | 11392 | else if (!bc_label) |
34f4db93 | 11393 | { |
b635eb2f | 11394 | for (;; labeled_block = TREE_CHAIN (labeled_block)) |
e04a16fb | 11395 | { |
b635eb2f PB |
11396 | if (labeled_block == NULL_TREE) |
11397 | { | |
11398 | parse_error_context (wfl_operator, | |
11399 | "`break' must be in loop or switch"); | |
11400 | return error_mark_node; | |
11401 | } | |
11402 | target_stmt = LABELED_BLOCK_BODY (labeled_block); | |
11403 | if (TREE_CODE (target_stmt) == SWITCH_EXPR | |
5cbdba64 | 11404 | || search_loop (target_stmt)) |
b635eb2f PB |
11405 | { |
11406 | bc_label = labeled_block; | |
11407 | break; | |
11408 | } | |
e04a16fb | 11409 | } |
e04a16fb AG |
11410 | } |
11411 | ||
b635eb2f | 11412 | EXIT_BLOCK_LABELED_BLOCK (node) = bc_label; |
15fdcfe9 PB |
11413 | CAN_COMPLETE_NORMALLY (bc_label) = 1; |
11414 | ||
e04a16fb AG |
11415 | /* Our break/continue don't return values. */ |
11416 | TREE_TYPE (node) = void_type_node; | |
11417 | /* Encapsulate the break within a compound statement so that it's | |
5cbdba64 | 11418 | expanded all the times by expand_expr (and not clobbered |
e04a16fb AG |
11419 | sometimes, like after a if statement) */ |
11420 | node = add_stmt_to_compound (NULL_TREE, void_type_node, node); | |
11421 | TREE_SIDE_EFFECTS (node) = 1; | |
11422 | return node; | |
11423 | } | |
11424 | ||
11425 | /* Process the exit expression belonging to a loop. Its type must be | |
11426 | boolean. */ | |
11427 | ||
11428 | static tree | |
11429 | patch_exit_expr (node) | |
11430 | tree node; | |
11431 | { | |
11432 | tree expression = TREE_OPERAND (node, 0); | |
11433 | TREE_TYPE (node) = error_mark_node; | |
11434 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11435 | ||
11436 | /* The type of expression must be boolean */ | |
11437 | if (TREE_TYPE (expression) != boolean_type_node) | |
11438 | { | |
11439 | parse_error_context | |
11440 | (wfl_operator, | |
11441 | "Incompatible type for loop conditional. Can't convert `%s' to " | |
11442 | "`boolean'", | |
0a2138e2 | 11443 | lang_printable_name (TREE_TYPE (expression), 0)); |
e04a16fb AG |
11444 | return error_mark_node; |
11445 | } | |
11446 | /* Now we know things are allright, invert the condition, fold and | |
11447 | return */ | |
11448 | TREE_OPERAND (node, 0) = | |
11449 | fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression)); | |
15fdcfe9 PB |
11450 | |
11451 | if (! integer_zerop (TREE_OPERAND (node, 0)) | |
11452 | && ctxp->current_loop != NULL_TREE | |
11453 | && TREE_CODE (ctxp->current_loop) == LOOP_EXPR) | |
11454 | CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1; | |
11455 | if (! integer_onep (TREE_OPERAND (node, 0))) | |
11456 | CAN_COMPLETE_NORMALLY (node) = 1; | |
11457 | ||
11458 | ||
e04a16fb AG |
11459 | TREE_TYPE (node) = void_type_node; |
11460 | return node; | |
11461 | } | |
b67d701b PB |
11462 | |
11463 | /* 14.9 Switch statement */ | |
11464 | ||
11465 | static tree | |
11466 | patch_switch_statement (node) | |
11467 | tree node; | |
11468 | { | |
c877974e | 11469 | tree se = TREE_OPERAND (node, 0), se_type; |
b67d701b PB |
11470 | |
11471 | /* Complete the switch expression */ | |
11472 | se = TREE_OPERAND (node, 0) = java_complete_tree (se); | |
11473 | se_type = TREE_TYPE (se); | |
11474 | /* The type of the switch expression must be char, byte, short or | |
11475 | int */ | |
11476 | if (!JINTEGRAL_TYPE_P (se_type)) | |
11477 | { | |
11478 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11479 | parse_error_context (wfl_operator, "Incompatible type for `switch'. " | |
11480 | "Can't convert `%s' to `int'", | |
0a2138e2 | 11481 | lang_printable_name (se_type, 0)); |
b67d701b PB |
11482 | /* This is what java_complete_tree will check */ |
11483 | TREE_OPERAND (node, 0) = error_mark_node; | |
11484 | return error_mark_node; | |
11485 | } | |
11486 | ||
15fdcfe9 | 11487 | TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1)); |
b67d701b PB |
11488 | |
11489 | /* Ready to return */ | |
15fdcfe9 | 11490 | if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK) |
b67d701b PB |
11491 | { |
11492 | TREE_TYPE (node) = error_mark_node; | |
11493 | return error_mark_node; | |
11494 | } | |
11495 | TREE_TYPE (node) = void_type_node; | |
11496 | TREE_SIDE_EFFECTS (node) = 1; | |
15fdcfe9 | 11497 | CAN_COMPLETE_NORMALLY (node) |
c877974e APB |
11498 | = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)) |
11499 | || ! SWITCH_HAS_DEFAULT (node); | |
b67d701b PB |
11500 | return node; |
11501 | } | |
11502 | ||
b67d701b PB |
11503 | /* 14.18 The try statement */ |
11504 | ||
b67d701b | 11505 | static tree |
a7d8d81f | 11506 | build_try_statement (location, try_block, catches) |
b67d701b | 11507 | int location; |
a7d8d81f PB |
11508 | tree try_block, catches; |
11509 | { | |
11510 | tree node = build (TRY_EXPR, NULL_TREE, try_block, catches); | |
b67d701b | 11511 | EXPR_WFL_LINECOL (node) = location; |
a7d8d81f | 11512 | return node; |
b67d701b PB |
11513 | } |
11514 | ||
a7d8d81f PB |
11515 | static tree |
11516 | build_try_finally_statement (location, try_block, finally) | |
11517 | int location; | |
11518 | tree try_block, finally; | |
b67d701b | 11519 | { |
a7d8d81f PB |
11520 | tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally); |
11521 | EXPR_WFL_LINECOL (node) = location; | |
11522 | return node; | |
b67d701b PB |
11523 | } |
11524 | ||
11525 | static tree | |
11526 | patch_try_statement (node) | |
11527 | tree node; | |
11528 | { | |
11529 | int error_found = 0; | |
11530 | tree try = TREE_OPERAND (node, 0); | |
11531 | /* Exception handlers are considered in left to right order */ | |
11532 | tree catch = nreverse (TREE_OPERAND (node, 1)); | |
b9f7e36c | 11533 | tree current, caught_type_list = NULL_TREE; |
b67d701b PB |
11534 | |
11535 | /* Check catch clauses, if any. Every time we find an error, we try | |
b9f7e36c APB |
11536 | to process the next catch clause. We process the catch clause before |
11537 | the try block so that when processing the try block we can check thrown | |
11538 | exceptions againts the caught type list. */ | |
b67d701b PB |
11539 | for (current = catch; current; current = TREE_CHAIN (current)) |
11540 | { | |
11541 | tree carg_decl, carg_type; | |
11542 | tree sub_current, catch_block, catch_clause; | |
11543 | int unreachable; | |
11544 | ||
b67d701b | 11545 | /* At this point, the structure of the catch clause is |
b67d701b PB |
11546 | CATCH_EXPR (catch node) |
11547 | BLOCK (with the decl of the parameter) | |
11548 | COMPOUND_EXPR | |
7525cc04 | 11549 | MODIFY_EXPR (assignment of the catch parameter) |
b67d701b | 11550 | BLOCK (catch clause block) |
a7d8d81f PB |
11551 | */ |
11552 | catch_clause = TREE_OPERAND (current, 0); | |
b67d701b PB |
11553 | carg_decl = BLOCK_EXPR_DECLS (catch_clause); |
11554 | carg_type = TREE_TYPE (TREE_TYPE (carg_decl)); | |
11555 | ||
11556 | /* Catch clauses can't have more than one parameter declared, | |
11557 | but it's already enforced by the grammar. Make sure that the | |
11558 | only parameter of the clause statement in of class Throwable | |
11559 | or a subclass of Throwable, but that was done earlier. The | |
11560 | catch clause parameter type has also been resolved. */ | |
11561 | ||
11562 | /* Just make sure that the catch clause parameter type inherits | |
11563 | from java.lang.Throwable */ | |
11564 | if (!inherits_from_p (carg_type, throwable_type_node)) | |
11565 | { | |
11566 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current); | |
11567 | parse_error_context (wfl_operator, | |
11568 | "Can't catch class `%s'. Catch clause " | |
11569 | "parameter type must be a subclass of " | |
11570 | "class `java.lang.Throwable'", | |
0a2138e2 | 11571 | lang_printable_name (carg_type, 0)); |
b67d701b PB |
11572 | error_found = 1; |
11573 | continue; | |
11574 | } | |
11575 | ||
11576 | /* Partial check for unreachable catch statement: The catch | |
11577 | clause is reachable iff is no earlier catch block A in | |
11578 | the try statement such that the type of the catch | |
11579 | clause's parameter is the same as or a subclass of the | |
11580 | type of A's parameter */ | |
11581 | unreachable = 0; | |
11582 | for (sub_current = catch; | |
11583 | sub_current != current; sub_current = TREE_CHAIN (sub_current)) | |
11584 | { | |
11585 | tree sub_catch_clause, decl; | |
a7d8d81f | 11586 | sub_catch_clause = TREE_OPERAND (sub_current, 0); |
b67d701b PB |
11587 | decl = BLOCK_EXPR_DECLS (sub_catch_clause); |
11588 | ||
11589 | if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl)))) | |
11590 | { | |
11591 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current); | |
11592 | parse_error_context | |
11593 | (wfl_operator, "`catch' not reached because of the catch " | |
11594 | "clause at line %d", EXPR_WFL_LINENO (sub_current)); | |
11595 | unreachable = error_found = 1; | |
11596 | break; | |
11597 | } | |
11598 | } | |
b67d701b PB |
11599 | /* Complete the catch clause block */ |
11600 | catch_block = java_complete_tree (TREE_OPERAND (current, 0)); | |
11601 | if (catch_block == error_mark_node) | |
11602 | { | |
11603 | error_found = 1; | |
11604 | continue; | |
11605 | } | |
15fdcfe9 PB |
11606 | if (CAN_COMPLETE_NORMALLY (catch_block)) |
11607 | CAN_COMPLETE_NORMALLY (node) = 1; | |
b67d701b | 11608 | TREE_OPERAND (current, 0) = catch_block; |
15fdcfe9 PB |
11609 | |
11610 | if (unreachable) | |
11611 | continue; | |
11612 | ||
11613 | /* Things to do here: the exception must be thrown */ | |
11614 | ||
11615 | /* Link this type to the caught type list */ | |
11616 | caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list); | |
b67d701b PB |
11617 | } |
11618 | ||
b9f7e36c APB |
11619 | PUSH_EXCEPTIONS (caught_type_list); |
11620 | if ((try = java_complete_tree (try)) == error_mark_node) | |
11621 | error_found = 1; | |
15fdcfe9 PB |
11622 | if (CAN_COMPLETE_NORMALLY (try)) |
11623 | CAN_COMPLETE_NORMALLY (node) = 1; | |
b9f7e36c APB |
11624 | POP_EXCEPTIONS (); |
11625 | ||
b67d701b PB |
11626 | /* Verification ends here */ |
11627 | if (error_found) | |
11628 | return error_mark_node; | |
11629 | ||
11630 | TREE_OPERAND (node, 0) = try; | |
11631 | TREE_OPERAND (node, 1) = catch; | |
b67d701b PB |
11632 | TREE_TYPE (node) = void_type_node; |
11633 | return node; | |
11634 | } | |
b9f7e36c APB |
11635 | |
11636 | /* 14.17 The synchronized Statement */ | |
11637 | ||
11638 | static tree | |
11639 | patch_synchronized_statement (node, wfl_op1) | |
11640 | tree node, wfl_op1; | |
11641 | { | |
5a005d9e | 11642 | tree expr = java_complete_tree (TREE_OPERAND (node, 0)); |
b9f7e36c | 11643 | tree block = TREE_OPERAND (node, 1); |
5a005d9e | 11644 | |
d8fccff5 | 11645 | tree enter, exit, expr_decl, assignment; |
5a005d9e PB |
11646 | |
11647 | if (expr == error_mark_node) | |
11648 | { | |
11649 | block = java_complete_tree (block); | |
11650 | return expr; | |
11651 | } | |
b9f7e36c APB |
11652 | |
11653 | /* The TYPE of expr must be a reference type */ | |
5a005d9e | 11654 | if (!JREFERENCE_TYPE_P (TREE_TYPE (expr))) |
b9f7e36c APB |
11655 | { |
11656 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); | |
11657 | parse_error_context (wfl_operator, "Incompatible type for `synchronized'" | |
11658 | ". Can't convert `%s' to `java.lang.Object'", | |
0a2138e2 | 11659 | lang_printable_name (TREE_TYPE (expr), 0)); |
b9f7e36c APB |
11660 | return error_mark_node; |
11661 | } | |
11662 | ||
ce6e9147 APB |
11663 | if (flag_emit_xref) |
11664 | { | |
11665 | TREE_OPERAND (node, 0) = expr; | |
11666 | TREE_OPERAND (node, 1) = java_complete_tree (block); | |
11667 | CAN_COMPLETE_NORMALLY (node) = 1; | |
11668 | return node; | |
11669 | } | |
11670 | ||
b9f7e36c APB |
11671 | /* Generate a try-finally for the synchronized statement, except |
11672 | that the handler that catches all throw exception calls | |
11673 | _Jv_MonitorExit and then rethrow the exception. | |
11674 | The synchronized statement is then implemented as: | |
11675 | TRY | |
11676 | { | |
11677 | _Jv_MonitorEnter (expression) | |
11678 | synchronized_block | |
11679 | _Jv_MonitorExit (expression) | |
11680 | } | |
11681 | CATCH_ALL | |
11682 | { | |
11683 | e = _Jv_exception_info (); | |
11684 | _Jv_MonitorExit (expression) | |
11685 | Throw (e); | |
11686 | } */ | |
11687 | ||
5a005d9e PB |
11688 | expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr)); |
11689 | BUILD_MONITOR_ENTER (enter, expr_decl); | |
11690 | BUILD_MONITOR_EXIT (exit, expr_decl); | |
11691 | CAN_COMPLETE_NORMALLY (enter) = 1; | |
11692 | CAN_COMPLETE_NORMALLY (exit) = 1; | |
96847892 AH |
11693 | assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr); |
11694 | TREE_SIDE_EFFECTS (assignment) = 1; | |
5a005d9e PB |
11695 | node = build1 (CLEANUP_POINT_EXPR, NULL_TREE, |
11696 | build (COMPOUND_EXPR, NULL_TREE, | |
11697 | build (WITH_CLEANUP_EXPR, NULL_TREE, | |
11698 | build (COMPOUND_EXPR, NULL_TREE, | |
96847892 | 11699 | assignment, enter), |
5a005d9e PB |
11700 | NULL_TREE, exit), |
11701 | block)); | |
11702 | node = build_expr_block (node, expr_decl); | |
11703 | ||
11704 | return java_complete_tree (node); | |
b9f7e36c APB |
11705 | } |
11706 | ||
11707 | /* 14.16 The throw Statement */ | |
11708 | ||
11709 | static tree | |
11710 | patch_throw_statement (node, wfl_op1) | |
11711 | tree node, wfl_op1; | |
11712 | { | |
11713 | tree expr = TREE_OPERAND (node, 0); | |
11714 | tree type = TREE_TYPE (expr); | |
11715 | int unchecked_ok = 0, tryblock_throws_ok = 0; | |
11716 | ||
11717 | /* Thrown expression must be assignable to java.lang.Throwable */ | |
11718 | if (!try_reference_assignconv (throwable_type_node, expr)) | |
11719 | { | |
11720 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); | |
11721 | parse_error_context (wfl_operator, "Can't throw `%s'; it must be a " | |
11722 | "subclass of class `java.lang.Throwable'", | |
0a2138e2 | 11723 | lang_printable_name (type, 0)); |
b9f7e36c APB |
11724 | /* If the thrown expression was a reference, we further the |
11725 | compile-time check. */ | |
11726 | if (!JREFERENCE_TYPE_P (type)) | |
11727 | return error_mark_node; | |
11728 | } | |
11729 | ||
11730 | /* At least one of the following must be true */ | |
11731 | ||
11732 | /* The type of the throw expression is a not checked exception, | |
11733 | i.e. is a unchecked expression. */ | |
c877974e | 11734 | unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type)); |
b9f7e36c APB |
11735 | |
11736 | /* Throw is contained in a try statement and at least one catch | |
11737 | clause can receive the thrown expression or the current method is | |
11738 | declared to throw such an exception. Or, the throw statement is | |
11739 | contained in a method or constructor declaration and the type of | |
11740 | the Expression is assignable to at least one type listed in the | |
11741 | throws clause the declaration. */ | |
11742 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); | |
11743 | if (!unchecked_ok) | |
f099f336 | 11744 | tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr)); |
b9f7e36c APB |
11745 | if (!(unchecked_ok || tryblock_throws_ok)) |
11746 | { | |
11747 | /* If there is a surrounding try block that has no matching | |
11748 | clatch clause, report it first. A surrounding try block exits | |
11749 | only if there is something after the list of checked | |
11750 | exception thrown by the current function (if any). */ | |
11751 | if (IN_TRY_BLOCK_P ()) | |
11752 | parse_error_context (wfl_operator, "Checked exception `%s' can't be " | |
11753 | "caught by any of the catch clause(s) " | |
11754 | "of the surrounding `try' block", | |
0a2138e2 | 11755 | lang_printable_name (type, 0)); |
b9f7e36c APB |
11756 | /* If we have no surrounding try statement and the method doesn't have |
11757 | any throws, report it now. FIXME */ | |
f099f336 APB |
11758 | |
11759 | /* We report that the exception can't be throw from a try block | |
11760 | in all circumstances but when the `throw' is inside a static | |
11761 | block. */ | |
b9f7e36c APB |
11762 | else if (!EXCEPTIONS_P (currently_caught_type_list) |
11763 | && !tryblock_throws_ok) | |
f099f336 | 11764 | { |
7f1d4866 | 11765 | if (IS_CLINIT (current_function_decl)) |
f099f336 APB |
11766 | parse_error_context (wfl_operator, "Checked exception `%s' can't " |
11767 | "be thrown in initializer", | |
11768 | lang_printable_name (type, 0)); | |
11769 | else | |
11770 | parse_error_context (wfl_operator, "Checked exception `%s' isn't " | |
11771 | "thrown from a `try' block", | |
11772 | lang_printable_name (type, 0)); | |
11773 | } | |
b9f7e36c APB |
11774 | /* Otherwise, the current method doesn't have the appropriate |
11775 | throws declaration */ | |
11776 | else | |
11777 | parse_error_context (wfl_operator, "Checked exception `%s' doesn't " | |
11778 | "match any of current method's `throws' " | |
11779 | "declaration(s)", | |
0a2138e2 | 11780 | lang_printable_name (type, 0)); |
b9f7e36c APB |
11781 | return error_mark_node; |
11782 | } | |
11783 | ||
ce6e9147 | 11784 | if (! flag_emit_class_files && ! flag_emit_xref) |
15fdcfe9 | 11785 | BUILD_THROW (node, expr); |
ce6e9147 APB |
11786 | |
11787 | /* If doing xrefs, keep the location where the `throw' was seen. */ | |
11788 | if (flag_emit_xref) | |
11789 | EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1); | |
b9f7e36c APB |
11790 | return node; |
11791 | } | |
11792 | ||
11793 | /* Check that exception said to be thrown by method DECL can be | |
11794 | effectively caught from where DECL is invoked. */ | |
11795 | ||
11796 | static void | |
11797 | check_thrown_exceptions (location, decl) | |
11798 | int location; | |
11799 | tree decl; | |
11800 | { | |
11801 | tree throws; | |
11802 | /* For all the unchecked exceptions thrown by DECL */ | |
11803 | for (throws = DECL_FUNCTION_THROWS (decl); throws; | |
11804 | throws = TREE_CHAIN (throws)) | |
0a2138e2 | 11805 | if (!check_thrown_exceptions_do (TREE_VALUE (throws))) |
b9f7e36c | 11806 | { |
3e78f871 PB |
11807 | #if 1 |
11808 | /* Temporary hack to suppresses errors about cloning arrays. FIXME */ | |
11809 | if (DECL_NAME (decl) == get_identifier ("clone")) | |
11810 | continue; | |
11811 | #endif | |
b9f7e36c | 11812 | EXPR_WFL_LINECOL (wfl_operator) = location; |
7705e9db APB |
11813 | if (DECL_NAME (current_function_decl) == finit_identifier_node) |
11814 | parse_error_context | |
11815 | (wfl_operator, "Exception `%s' can't be thrown in initializer", | |
11816 | lang_printable_name (TREE_VALUE (throws), 0)); | |
11817 | else | |
11818 | { | |
11819 | parse_error_context | |
11820 | (wfl_operator, "Exception `%s' must be caught, or it must be " | |
11821 | "declared in the `throws' clause of `%s'", | |
11822 | lang_printable_name (TREE_VALUE (throws), 0), | |
11823 | (DECL_NAME (current_function_decl) == init_identifier_node ? | |
11824 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) : | |
11825 | IDENTIFIER_POINTER (DECL_NAME (current_function_decl)))); | |
11826 | } | |
b9f7e36c APB |
11827 | } |
11828 | } | |
11829 | ||
c877974e | 11830 | /* Return 1 if checked EXCEPTION is caught at the current nesting level of |
b9f7e36c APB |
11831 | try-catch blocks, OR is listed in the `throws' clause of the |
11832 | current method. */ | |
11833 | ||
11834 | static int | |
0a2138e2 | 11835 | check_thrown_exceptions_do (exception) |
b9f7e36c APB |
11836 | tree exception; |
11837 | { | |
11838 | tree list = currently_caught_type_list; | |
c877974e | 11839 | resolve_and_layout (exception, NULL_TREE); |
b9f7e36c APB |
11840 | /* First, all the nested try-catch-finally at that stage. The |
11841 | last element contains `throws' clause exceptions, if any. */ | |
c877974e APB |
11842 | if (IS_UNCHECKED_EXCEPTION_P (exception)) |
11843 | return 1; | |
b9f7e36c APB |
11844 | while (list) |
11845 | { | |
11846 | tree caught; | |
11847 | for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught)) | |
11848 | if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0)) | |
11849 | return 1; | |
11850 | list = TREE_CHAIN (list); | |
11851 | } | |
11852 | return 0; | |
11853 | } | |
11854 | ||
11855 | static void | |
11856 | purge_unchecked_exceptions (mdecl) | |
11857 | tree mdecl; | |
11858 | { | |
11859 | tree throws = DECL_FUNCTION_THROWS (mdecl); | |
11860 | tree new = NULL_TREE; | |
11861 | ||
11862 | while (throws) | |
11863 | { | |
11864 | tree next = TREE_CHAIN (throws); | |
c877974e | 11865 | if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws))) |
b9f7e36c APB |
11866 | { |
11867 | TREE_CHAIN (throws) = new; | |
11868 | new = throws; | |
11869 | } | |
11870 | throws = next; | |
11871 | } | |
11872 | /* List is inverted here, but it doesn't matter */ | |
11873 | DECL_FUNCTION_THROWS (mdecl) = new; | |
11874 | } | |
22eed1e6 APB |
11875 | |
11876 | /* 15.24 Conditional Operator ?: */ | |
11877 | ||
11878 | static tree | |
11879 | patch_conditional_expr (node, wfl_cond, wfl_op1) | |
11880 | tree node, wfl_cond, wfl_op1; | |
11881 | { | |
11882 | tree cond = TREE_OPERAND (node, 0); | |
11883 | tree op1 = TREE_OPERAND (node, 1); | |
11884 | tree op2 = TREE_OPERAND (node, 2); | |
22eed1e6 | 11885 | tree resulting_type = NULL_TREE; |
ac825856 | 11886 | tree t1, t2, patched; |
22eed1e6 APB |
11887 | int error_found = 0; |
11888 | ||
ac825856 APB |
11889 | /* Operands of ?: might be StringBuffers crafted as a result of a |
11890 | string concatenation. Obtain a descent operand here. */ | |
11891 | if ((patched = patch_string (op1))) | |
11892 | TREE_OPERAND (node, 1) = op1 = patched; | |
11893 | if ((patched = patch_string (op2))) | |
11894 | TREE_OPERAND (node, 2) = op2 = patched; | |
11895 | ||
11896 | t1 = TREE_TYPE (op1); | |
11897 | t2 = TREE_TYPE (op2); | |
11898 | ||
22eed1e6 APB |
11899 | /* The first expression must be a boolean */ |
11900 | if (TREE_TYPE (cond) != boolean_type_node) | |
11901 | { | |
11902 | SET_WFL_OPERATOR (wfl_operator, node, wfl_cond); | |
11903 | parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't " | |
11904 | "convert `%s' to `boolean'", | |
11905 | lang_printable_name (TREE_TYPE (cond), 0)); | |
11906 | error_found = 1; | |
11907 | } | |
11908 | ||
11909 | /* Second and third can be numeric, boolean (i.e. primitive), | |
11910 | references or null. Anything else results in an error */ | |
11911 | if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2)) | |
11912 | || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node) | |
11913 | && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node)) | |
11914 | || (t1 == boolean_type_node && t2 == boolean_type_node))) | |
11915 | error_found = 1; | |
11916 | ||
11917 | /* Determine the type of the conditional expression. Same types are | |
11918 | easy to deal with */ | |
11919 | else if (t1 == t2) | |
11920 | resulting_type = t1; | |
11921 | ||
11922 | /* There are different rules for numeric types */ | |
11923 | else if (JNUMERIC_TYPE_P (t1)) | |
11924 | { | |
11925 | /* if byte/short found, the resulting type is short */ | |
11926 | if ((t1 == byte_type_node && t2 == short_type_node) | |
11927 | || (t1 == short_type_node && t2 == byte_type_node)) | |
11928 | resulting_type = short_type_node; | |
11929 | ||
11930 | /* If t1 is a constant int and t2 is of type byte, short or char | |
11931 | and t1's value fits in t2, then the resulting type is t2 */ | |
11932 | else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1))) | |
11933 | && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2)) | |
11934 | resulting_type = t2; | |
11935 | ||
11936 | /* If t2 is a constant int and t1 is of type byte, short or char | |
11937 | and t2's value fits in t1, then the resulting type is t1 */ | |
11938 | else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2))) | |
11939 | && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1)) | |
11940 | resulting_type = t1; | |
11941 | ||
11942 | /* Otherwise, binary numeric promotion is applied and the | |
11943 | resulting type is the promoted type of operand 1 and 2 */ | |
11944 | else | |
93024893 | 11945 | resulting_type = binary_numeric_promotion (t1, t2, |
22eed1e6 APB |
11946 | &TREE_OPERAND (node, 1), |
11947 | &TREE_OPERAND (node, 2)); | |
11948 | } | |
11949 | ||
11950 | /* Cases of a reference and a null type */ | |
11951 | else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node) | |
11952 | resulting_type = t1; | |
11953 | ||
11954 | else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node) | |
11955 | resulting_type = t2; | |
11956 | ||
11957 | /* Last case: different reference types. If a type can be converted | |
11958 | into the other one by assignment conversion, the latter | |
11959 | determines the type of the expression */ | |
11960 | else if ((resulting_type = try_reference_assignconv (t1, op2))) | |
11961 | resulting_type = promote_type (t1); | |
11962 | ||
11963 | else if ((resulting_type = try_reference_assignconv (t2, op1))) | |
11964 | resulting_type = promote_type (t2); | |
11965 | ||
11966 | /* If we don't have any resulting type, we're in trouble */ | |
11967 | if (!resulting_type) | |
11968 | { | |
c2e3db92 | 11969 | char *t = xstrdup (lang_printable_name (t1, 0)); |
22eed1e6 APB |
11970 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); |
11971 | parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't " | |
11972 | "convert `%s' to `%s'", t, | |
11973 | lang_printable_name (t2, 0)); | |
11974 | free (t); | |
11975 | error_found = 1; | |
11976 | } | |
11977 | ||
11978 | if (error_found) | |
11979 | { | |
11980 | TREE_TYPE (node) = error_mark_node; | |
11981 | return error_mark_node; | |
11982 | } | |
11983 | ||
11984 | TREE_TYPE (node) = resulting_type; | |
11985 | TREE_SET_CODE (node, COND_EXPR); | |
15fdcfe9 | 11986 | CAN_COMPLETE_NORMALLY (node) = 1; |
22eed1e6 APB |
11987 | return node; |
11988 | } | |
ac825856 | 11989 | |
5b09b33e PB |
11990 | /* Try to constant fold NODE. |
11991 | If NODE is not a constant expression, return NULL_EXPR. | |
11992 | CONTEXT is a static final VAR_DECL whose initializer we are folding. */ | |
11993 | ||
11994 | static tree | |
11995 | fold_constant_for_init (node, context) | |
11996 | tree node; | |
11997 | tree context; | |
11998 | { | |
11999 | tree op0, op1, val; | |
12000 | enum tree_code code = TREE_CODE (node); | |
12001 | ||
93024893 | 12002 | if (code == STRING_CST) |
5b09b33e | 12003 | return node; |
93024893 APB |
12004 | |
12005 | if (code == INTEGER_CST || code == REAL_CST) | |
12006 | return convert (TREE_TYPE (context), node); | |
8576f094 | 12007 | if (TREE_TYPE (node) != NULL_TREE && code != VAR_DECL && code != FIELD_DECL) |
5b09b33e PB |
12008 | return NULL_TREE; |
12009 | ||
12010 | switch (code) | |
12011 | { | |
5b09b33e PB |
12012 | case PLUS_EXPR: |
12013 | case MINUS_EXPR: | |
bc3ca41b PB |
12014 | case MULT_EXPR: |
12015 | case TRUNC_MOD_EXPR: | |
12016 | case RDIV_EXPR: | |
5b09b33e PB |
12017 | case LSHIFT_EXPR: |
12018 | case RSHIFT_EXPR: | |
12019 | case URSHIFT_EXPR: | |
12020 | case BIT_AND_EXPR: | |
12021 | case BIT_XOR_EXPR: | |
12022 | case BIT_IOR_EXPR: | |
5b09b33e PB |
12023 | case TRUTH_ANDIF_EXPR: |
12024 | case TRUTH_ORIF_EXPR: | |
12025 | case EQ_EXPR: | |
12026 | case NE_EXPR: | |
12027 | case GT_EXPR: | |
12028 | case GE_EXPR: | |
12029 | case LT_EXPR: | |
12030 | case LE_EXPR: | |
12031 | op0 = TREE_OPERAND (node, 0); | |
12032 | op1 = TREE_OPERAND (node, 1); | |
12033 | val = fold_constant_for_init (op0, context); | |
12034 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12035 | return NULL_TREE; | |
12036 | TREE_OPERAND (node, 0) = val; | |
12037 | val = fold_constant_for_init (op1, context); | |
12038 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12039 | return NULL_TREE; | |
12040 | TREE_OPERAND (node, 1) = val; | |
12041 | return patch_binop (node, op0, op1); | |
12042 | ||
12043 | case UNARY_PLUS_EXPR: | |
12044 | case NEGATE_EXPR: | |
12045 | case TRUTH_NOT_EXPR: | |
12046 | case BIT_NOT_EXPR: | |
12047 | case CONVERT_EXPR: | |
12048 | op0 = TREE_OPERAND (node, 0); | |
12049 | val = fold_constant_for_init (op0, context); | |
12050 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12051 | return NULL_TREE; | |
12052 | TREE_OPERAND (node, 0) = val; | |
5a005d9e | 12053 | return patch_unaryop (node, op0); |
5b09b33e PB |
12054 | break; |
12055 | ||
12056 | case COND_EXPR: | |
12057 | val = fold_constant_for_init (TREE_OPERAND (node, 0), context); | |
12058 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12059 | return NULL_TREE; | |
12060 | TREE_OPERAND (node, 0) = val; | |
12061 | val = fold_constant_for_init (TREE_OPERAND (node, 1), context); | |
12062 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12063 | return NULL_TREE; | |
12064 | TREE_OPERAND (node, 1) = val; | |
12065 | val = fold_constant_for_init (TREE_OPERAND (node, 2), context); | |
12066 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12067 | return NULL_TREE; | |
12068 | TREE_OPERAND (node, 2) = val; | |
12069 | return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1) | |
12070 | : TREE_OPERAND (node, 2); | |
12071 | ||
12072 | case VAR_DECL: | |
8576f094 APB |
12073 | case FIELD_DECL: |
12074 | if (! FIELD_FINAL (node) | |
5b09b33e PB |
12075 | || DECL_INITIAL (node) == NULL_TREE) |
12076 | return NULL_TREE; | |
12077 | val = DECL_INITIAL (node); | |
12078 | /* Guard against infinite recursion. */ | |
12079 | DECL_INITIAL (node) = NULL_TREE; | |
cd9643f7 | 12080 | val = fold_constant_for_init (val, node); |
5b09b33e PB |
12081 | DECL_INITIAL (node) = val; |
12082 | return val; | |
12083 | ||
12084 | case EXPR_WITH_FILE_LOCATION: | |
12085 | /* Compare java_complete_tree and resolve_expression_name. */ | |
12086 | if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */ | |
12087 | || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE) | |
12088 | { | |
12089 | tree name = EXPR_WFL_NODE (node); | |
12090 | tree decl; | |
12091 | if (PRIMARY_P (node)) | |
12092 | return NULL_TREE; | |
12093 | else if (! QUALIFIED_P (name)) | |
12094 | { | |
12095 | decl = lookup_field_wrapper (DECL_CONTEXT (context), name); | |
8576f094 APB |
12096 | if (decl == NULL_TREE |
12097 | || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl))) | |
5b09b33e PB |
12098 | return NULL_TREE; |
12099 | return fold_constant_for_init (decl, decl); | |
12100 | } | |
12101 | else | |
12102 | { | |
5b09b33e PB |
12103 | /* Wait until the USE_COMPONENT_REF re-write. FIXME. */ |
12104 | qualify_ambiguous_name (node); | |
12105 | if (resolve_field_access (node, &decl, NULL) | |
12106 | && decl != NULL_TREE) | |
12107 | return fold_constant_for_init (decl, decl); | |
5b09b33e PB |
12108 | return NULL_TREE; |
12109 | } | |
12110 | } | |
12111 | else | |
12112 | { | |
12113 | op0 = TREE_OPERAND (node, 0); | |
12114 | val = fold_constant_for_init (op0, context); | |
12115 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
12116 | return NULL_TREE; | |
12117 | TREE_OPERAND (node, 0) = val; | |
12118 | return val; | |
12119 | } | |
12120 | ||
bc3ca41b PB |
12121 | #ifdef USE_COMPONENT_REF |
12122 | case IDENTIFIER: | |
12123 | case COMPONENT_REF: | |
12124 | ?; | |
12125 | #endif | |
12126 | ||
5b09b33e PB |
12127 | default: |
12128 | return NULL_TREE; | |
12129 | } | |
12130 | } | |
bc3ca41b PB |
12131 | |
12132 | #ifdef USE_COMPONENT_REF | |
12133 | /* Context is 'T' for TypeName, 'P' for PackageName, | |
12134 | 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */ | |
12135 | ||
12136 | tree | |
12137 | resolve_simple_name (name, context) | |
12138 | tree name; | |
12139 | int context; | |
12140 | { | |
12141 | } | |
12142 | ||
12143 | tree | |
12144 | resolve_qualified_name (name, context) | |
12145 | tree name; | |
12146 | int context; | |
12147 | { | |
12148 | } | |
12149 | #endif |