]>
Commit | Line | Data |
---|---|---|
e04a16fb AG |
1 | /* Source code parsing and tree node generation for the GNU compiler |
2 | for the Java(TM) language. | |
df32d2ce | 3 | Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
e04a16fb AG |
4 | Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com) |
5 | ||
6 | This file is part of GNU CC. | |
7 | ||
8 | GNU CC is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU CC is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GNU CC; see the file COPYING. If not, write to | |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. | |
22 | ||
23 | Java and all Java-based marks are trademarks or registered trademarks | |
24 | of Sun Microsystems, Inc. in the United States and other countries. | |
25 | The Free Software Foundation is independent of Sun Microsystems, Inc. */ | |
26 | ||
27 | /* This file parses java source code and issues a tree node image | |
28 | suitable for code generation (byte code and targeted CPU assembly | |
29 | language). | |
30 | ||
31 | The grammar conforms to the Java grammar described in "The Java(TM) | |
32 | Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley | |
33 | 1996, ISBN 0-201-63451-1" | |
34 | ||
35 | The following modifications were brought to the original grammar: | |
36 | ||
37 | method_body: added the rule '| block SC_TK' | |
e04a16fb AG |
38 | static_initializer: added the rule 'static block SC_TK'. |
39 | ||
40 | Note: All the extra rules described above should go away when the | |
41 | empty_statement rule will work. | |
42 | ||
43 | statement_nsi: 'nsi' should be read no_short_if. | |
44 | ||
45 | Some rules have been modified to support JDK1.1 inner classes | |
46 | definitions and other extensions. */ | |
47 | ||
48 | %{ | |
e04a16fb | 49 | #include "config.h" |
36635152 GS |
50 | #include "system.h" |
51 | #include <dirent.h> | |
e04a16fb AG |
52 | #include "tree.h" |
53 | #include "rtl.h" | |
54 | #include "obstack.h" | |
0a2138e2 | 55 | #include "toplev.h" |
e04a16fb AG |
56 | #include "flags.h" |
57 | #include "java-tree.h" | |
58 | #include "jcf.h" | |
59 | #include "lex.h" | |
60 | #include "parse.h" | |
61 | #include "zipfile.h" | |
5e942c50 | 62 | #include "convert.h" |
63a212ed | 63 | #include "buffer.h" |
f099f336 | 64 | #include "xref.h" |
b384405b | 65 | #include "function.h" |
138657ec | 66 | #include "except.h" |
0ae70c6a | 67 | #include "defaults.h" |
19e223db | 68 | #include "ggc.h" |
e04a16fb | 69 | |
c2952b01 APB |
70 | #ifndef DIR_SEPARATOR |
71 | #define DIR_SEPARATOR '/' | |
72 | #endif | |
73 | ||
82371d41 | 74 | /* Local function prototypes */ |
df32d2ce KG |
75 | static char *java_accstring_lookup PARAMS ((int)); |
76 | static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree)); | |
77 | static void variable_redefinition_error PARAMS ((tree, tree, tree, int)); | |
df32d2ce KG |
78 | static tree create_class PARAMS ((int, tree, tree, tree)); |
79 | static tree create_interface PARAMS ((int, tree, tree)); | |
c2952b01 | 80 | static void end_class_declaration PARAMS ((int)); |
df32d2ce KG |
81 | static tree find_field PARAMS ((tree, tree)); |
82 | static tree lookup_field_wrapper PARAMS ((tree, tree)); | |
83 | static int duplicate_declaration_error_p PARAMS ((tree, tree, tree)); | |
84 | static void register_fields PARAMS ((int, tree, tree)); | |
98a52c2c | 85 | static tree parser_qualified_classname PARAMS ((tree)); |
df32d2ce KG |
86 | static int parser_check_super PARAMS ((tree, tree, tree)); |
87 | static int parser_check_super_interface PARAMS ((tree, tree, tree)); | |
88 | static void check_modifiers_consistency PARAMS ((int)); | |
89 | static tree lookup_cl PARAMS ((tree)); | |
90 | static tree lookup_java_method2 PARAMS ((tree, tree, int)); | |
91 | static tree method_header PARAMS ((int, tree, tree, tree)); | |
92 | static void fix_method_argument_names PARAMS ((tree ,tree)); | |
93 | static tree method_declarator PARAMS ((tree, tree)); | |
94 | static void parse_warning_context PARAMS ((tree cl, const char *msg, ...)) | |
d4476be2 | 95 | ATTRIBUTE_PRINTF_2; |
df32d2ce KG |
96 | static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list)); |
97 | static void parse_ctor_invocation_error PARAMS ((void)); | |
98 | static tree parse_jdk1_1_error PARAMS ((const char *)); | |
99 | static void complete_class_report_errors PARAMS ((jdep *)); | |
100 | static int process_imports PARAMS ((void)); | |
101 | static void read_import_dir PARAMS ((tree)); | |
102 | static int find_in_imports_on_demand PARAMS ((tree)); | |
9a7ab4b3 | 103 | static void find_in_imports PARAMS ((tree)); |
cf1748bf | 104 | static void check_inner_class_access PARAMS ((tree, tree, tree)); |
df32d2ce | 105 | static int check_pkg_class_access PARAMS ((tree, tree)); |
9a7ab4b3 | 106 | static void register_package PARAMS ((tree)); |
df32d2ce KG |
107 | static tree resolve_package PARAMS ((tree, tree *)); |
108 | static tree lookup_package_type PARAMS ((const char *, int)); | |
109 | static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *)); | |
c2952b01 | 110 | static tree resolve_class PARAMS ((tree, tree, tree, tree)); |
df32d2ce KG |
111 | static void declare_local_variables PARAMS ((int, tree, tree)); |
112 | static void source_start_java_method PARAMS ((tree)); | |
113 | static void source_end_java_method PARAMS ((void)); | |
114 | static void expand_start_java_method PARAMS ((tree)); | |
115 | static tree find_name_in_single_imports PARAMS ((tree)); | |
116 | static void check_abstract_method_header PARAMS ((tree)); | |
117 | static tree lookup_java_interface_method2 PARAMS ((tree, tree)); | |
118 | static tree resolve_expression_name PARAMS ((tree, tree *)); | |
c2952b01 | 119 | static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree)); |
df32d2ce | 120 | static int check_class_interface_creation PARAMS ((int, int, tree, |
82371d41 | 121 | tree, tree, tree)); |
df32d2ce | 122 | static tree patch_method_invocation PARAMS ((tree, tree, tree, |
89e09b9a | 123 | int *, tree *)); |
df32d2ce KG |
124 | static int breakdown_qualified PARAMS ((tree *, tree *, tree)); |
125 | static tree resolve_and_layout PARAMS ((tree, tree)); | |
9a7ab4b3 | 126 | static tree qualify_and_find PARAMS ((tree, tree, tree)); |
df32d2ce KG |
127 | static tree resolve_no_layout PARAMS ((tree, tree)); |
128 | static int invocation_mode PARAMS ((tree, int)); | |
129 | static tree find_applicable_accessible_methods_list PARAMS ((int, tree, | |
82371d41 | 130 | tree, tree)); |
df32d2ce | 131 | static void search_applicable_methods_list PARAMS ((int, tree, tree, tree, |
1982388a | 132 | tree *, tree *)); |
df32d2ce KG |
133 | static tree find_most_specific_methods_list PARAMS ((tree)); |
134 | static int argument_types_convertible PARAMS ((tree, tree)); | |
135 | static tree patch_invoke PARAMS ((tree, tree, tree)); | |
c2952b01 | 136 | static int maybe_use_access_method PARAMS ((int, tree *, tree *)); |
df32d2ce KG |
137 | static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree)); |
138 | static tree register_incomplete_type PARAMS ((int, tree, tree, tree)); | |
139 | static tree obtain_incomplete_type PARAMS ((tree)); | |
140 | static tree java_complete_lhs PARAMS ((tree)); | |
141 | static tree java_complete_tree PARAMS ((tree)); | |
c2952b01 | 142 | static tree maybe_generate_pre_expand_clinit PARAMS ((tree)); |
dba41d30 | 143 | static int analyze_clinit_body PARAMS ((tree)); |
92d83515 | 144 | static int maybe_yank_clinit PARAMS ((tree)); |
df32d2ce KG |
145 | static void java_complete_expand_method PARAMS ((tree)); |
146 | static int unresolved_type_p PARAMS ((tree, tree *)); | |
147 | static void create_jdep_list PARAMS ((struct parser_ctxt *)); | |
148 | static tree build_expr_block PARAMS ((tree, tree)); | |
149 | static tree enter_block PARAMS ((void)); | |
150 | static tree enter_a_block PARAMS ((tree)); | |
151 | static tree exit_block PARAMS ((void)); | |
152 | static tree lookup_name_in_blocks PARAMS ((tree)); | |
153 | static void maybe_absorb_scoping_blocks PARAMS ((void)); | |
154 | static tree build_method_invocation PARAMS ((tree, tree)); | |
155 | static tree build_new_invocation PARAMS ((tree, tree)); | |
156 | static tree build_assignment PARAMS ((int, int, tree, tree)); | |
157 | static tree build_binop PARAMS ((enum tree_code, int, tree, tree)); | |
158 | static int check_final_assignment PARAMS ((tree ,tree)); | |
159 | static tree patch_assignment PARAMS ((tree, tree, tree )); | |
160 | static tree patch_binop PARAMS ((tree, tree, tree)); | |
161 | static tree build_unaryop PARAMS ((int, int, tree)); | |
162 | static tree build_incdec PARAMS ((int, int, tree, int)); | |
163 | static tree patch_unaryop PARAMS ((tree, tree)); | |
164 | static tree build_cast PARAMS ((int, tree, tree)); | |
165 | static tree build_null_of_type PARAMS ((tree)); | |
166 | static tree patch_cast PARAMS ((tree, tree)); | |
167 | static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int)); | |
168 | static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree)); | |
169 | static int valid_cast_to_p PARAMS ((tree, tree)); | |
170 | static int valid_method_invocation_conversion_p PARAMS ((tree, tree)); | |
171 | static tree try_builtin_assignconv PARAMS ((tree, tree, tree)); | |
172 | static tree try_reference_assignconv PARAMS ((tree, tree)); | |
173 | static tree build_unresolved_array_type PARAMS ((tree)); | |
174 | static tree build_array_from_name PARAMS ((tree, tree, tree, tree *)); | |
175 | static tree build_array_ref PARAMS ((int, tree, tree)); | |
176 | static tree patch_array_ref PARAMS ((tree)); | |
177 | static tree make_qualified_name PARAMS ((tree, tree, int)); | |
178 | static tree merge_qualified_name PARAMS ((tree, tree)); | |
179 | static tree make_qualified_primary PARAMS ((tree, tree, int)); | |
180 | static int resolve_qualified_expression_name PARAMS ((tree, tree *, | |
82371d41 | 181 | tree *, tree *)); |
df32d2ce | 182 | static void qualify_ambiguous_name PARAMS ((tree)); |
df32d2ce KG |
183 | static tree resolve_field_access PARAMS ((tree, tree *, tree *)); |
184 | static tree build_newarray_node PARAMS ((tree, tree, int)); | |
185 | static tree patch_newarray PARAMS ((tree)); | |
186 | static tree resolve_type_during_patch PARAMS ((tree)); | |
187 | static tree build_this PARAMS ((int)); | |
9a7ab4b3 | 188 | static tree build_wfl_wrap PARAMS ((tree, int)); |
df32d2ce KG |
189 | static tree build_return PARAMS ((int, tree)); |
190 | static tree patch_return PARAMS ((tree)); | |
191 | static tree maybe_access_field PARAMS ((tree, tree, tree)); | |
192 | static int complete_function_arguments PARAMS ((tree)); | |
c2952b01 APB |
193 | static int check_for_static_method_reference PARAMS ((tree, tree, tree, |
194 | tree, tree)); | |
df32d2ce KG |
195 | static int not_accessible_p PARAMS ((tree, tree, int)); |
196 | static void check_deprecation PARAMS ((tree, tree)); | |
197 | static int class_in_current_package PARAMS ((tree)); | |
198 | static tree build_if_else_statement PARAMS ((int, tree, tree, tree)); | |
199 | static tree patch_if_else_statement PARAMS ((tree)); | |
200 | static tree add_stmt_to_compound PARAMS ((tree, tree, tree)); | |
201 | static tree add_stmt_to_block PARAMS ((tree, tree, tree)); | |
202 | static tree patch_exit_expr PARAMS ((tree)); | |
203 | static tree build_labeled_block PARAMS ((int, tree)); | |
204 | static tree finish_labeled_statement PARAMS ((tree, tree)); | |
205 | static tree build_bc_statement PARAMS ((int, int, tree)); | |
206 | static tree patch_bc_statement PARAMS ((tree)); | |
207 | static tree patch_loop_statement PARAMS ((tree)); | |
208 | static tree build_new_loop PARAMS ((tree)); | |
209 | static tree build_loop_body PARAMS ((int, tree, int)); | |
210 | static tree finish_loop_body PARAMS ((int, tree, tree, int)); | |
211 | static tree build_debugable_stmt PARAMS ((int, tree)); | |
212 | static tree finish_for_loop PARAMS ((int, tree, tree, tree)); | |
213 | static tree patch_switch_statement PARAMS ((tree)); | |
214 | static tree string_constant_concatenation PARAMS ((tree, tree)); | |
215 | static tree build_string_concatenation PARAMS ((tree, tree)); | |
216 | static tree patch_string_cst PARAMS ((tree)); | |
217 | static tree patch_string PARAMS ((tree)); | |
218 | static tree build_try_statement PARAMS ((int, tree, tree)); | |
219 | static tree build_try_finally_statement PARAMS ((int, tree, tree)); | |
220 | static tree patch_try_statement PARAMS ((tree)); | |
221 | static tree patch_synchronized_statement PARAMS ((tree, tree)); | |
222 | static tree patch_throw_statement PARAMS ((tree, tree)); | |
223 | static void check_thrown_exceptions PARAMS ((int, tree)); | |
224 | static int check_thrown_exceptions_do PARAMS ((tree)); | |
225 | static void purge_unchecked_exceptions PARAMS ((tree)); | |
226 | static void check_throws_clauses PARAMS ((tree, tree, tree)); | |
227 | static void finish_method_declaration PARAMS ((tree)); | |
228 | static tree build_super_invocation PARAMS ((tree)); | |
229 | static int verify_constructor_circularity PARAMS ((tree, tree)); | |
230 | static char *constructor_circularity_msg PARAMS ((tree, tree)); | |
231 | static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree, | |
82371d41 | 232 | int, int)); |
df32d2ce KG |
233 | static const char *get_printable_method_name PARAMS ((tree)); |
234 | static tree patch_conditional_expr PARAMS ((tree, tree, tree)); | |
c2952b01 APB |
235 | static tree generate_finit PARAMS ((tree)); |
236 | static void add_instance_initializer PARAMS ((tree)); | |
df32d2ce | 237 | static void fix_constructors PARAMS ((tree)); |
c2952b01 APB |
238 | static tree build_alias_initializer_parameter_list PARAMS ((int, tree, |
239 | tree, int *)); | |
240 | static void craft_constructor PARAMS ((tree, tree)); | |
241 | static int verify_constructor_super PARAMS ((tree)); | |
df32d2ce KG |
242 | static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree)); |
243 | static void start_artificial_method_body PARAMS ((tree)); | |
244 | static void end_artificial_method_body PARAMS ((tree)); | |
245 | static int check_method_redefinition PARAMS ((tree, tree)); | |
246 | static int reset_method_name PARAMS ((tree)); | |
165f37bc | 247 | static int check_method_types_complete PARAMS ((tree)); |
df32d2ce KG |
248 | static void java_check_regular_methods PARAMS ((tree)); |
249 | static void java_check_abstract_methods PARAMS ((tree)); | |
250 | static tree maybe_build_primttype_type_ref PARAMS ((tree, tree)); | |
251 | static void unreachable_stmt_error PARAMS ((tree)); | |
252 | static tree find_expr_with_wfl PARAMS ((tree)); | |
253 | static void missing_return_error PARAMS ((tree)); | |
254 | static tree build_new_array_init PARAMS ((int, tree)); | |
255 | static tree patch_new_array_init PARAMS ((tree, tree)); | |
256 | static tree maybe_build_array_element_wfl PARAMS ((tree)); | |
257 | static int array_constructor_check_entry PARAMS ((tree, tree)); | |
258 | static const char *purify_type_name PARAMS ((const char *)); | |
259 | static tree fold_constant_for_init PARAMS ((tree, tree)); | |
260 | static tree strip_out_static_field_access_decl PARAMS ((tree)); | |
261 | static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *)); | |
262 | static void static_ref_err PARAMS ((tree, tree, tree)); | |
263 | static void parser_add_interface PARAMS ((tree, tree, tree)); | |
264 | static void add_superinterfaces PARAMS ((tree, tree)); | |
265 | static tree jdep_resolve_class PARAMS ((jdep *)); | |
266 | static int note_possible_classname PARAMS ((const char *, int)); | |
c2952b01 APB |
267 | static void java_complete_expand_classes PARAMS ((void)); |
268 | static void java_complete_expand_class PARAMS ((tree)); | |
269 | static void java_complete_expand_methods PARAMS ((tree)); | |
df32d2ce KG |
270 | static tree cut_identifier_in_qualified PARAMS ((tree)); |
271 | static tree java_stabilize_reference PARAMS ((tree)); | |
272 | static tree do_unary_numeric_promotion PARAMS ((tree)); | |
273 | static char * operator_string PARAMS ((tree)); | |
274 | static tree do_merge_string_cste PARAMS ((tree, const char *, int, int)); | |
275 | static tree merge_string_cste PARAMS ((tree, tree, int)); | |
276 | static tree java_refold PARAMS ((tree)); | |
277 | static int java_decl_equiv PARAMS ((tree, tree)); | |
278 | static int binop_compound_p PARAMS ((enum tree_code)); | |
279 | static tree search_loop PARAMS ((tree)); | |
280 | static int labeled_block_contains_loop_p PARAMS ((tree, tree)); | |
1175b9b4 | 281 | static int check_abstract_method_definitions PARAMS ((int, tree, tree)); |
df32d2ce KG |
282 | static void java_check_abstract_method_definitions PARAMS ((tree)); |
283 | static void java_debug_context_do PARAMS ((int)); | |
c2952b01 APB |
284 | static void java_parser_context_push_initialized_field PARAMS ((void)); |
285 | static void java_parser_context_pop_initialized_field PARAMS ((void)); | |
286 | static tree reorder_static_initialized PARAMS ((tree)); | |
287 | static void java_parser_context_suspend PARAMS ((void)); | |
288 | static void java_parser_context_resume PARAMS ((void)); | |
289 | ||
290 | /* JDK 1.1 work. FIXME */ | |
291 | ||
292 | static tree maybe_make_nested_class_name PARAMS ((tree)); | |
293 | static void make_nested_class_name PARAMS ((tree)); | |
294 | static void set_nested_class_simple_name_value PARAMS ((tree, int)); | |
295 | static void link_nested_class_to_enclosing PARAMS ((void)); | |
296 | static tree find_as_inner_class PARAMS ((tree, tree, tree)); | |
297 | static tree find_as_inner_class_do PARAMS ((tree, tree)); | |
298 | static int check_inner_class_redefinition PARAMS ((tree, tree)); | |
299 | ||
300 | static tree build_thisn_assign PARAMS ((void)); | |
301 | static tree build_current_thisn PARAMS ((tree)); | |
302 | static tree build_access_to_thisn PARAMS ((tree, tree, int)); | |
303 | static tree maybe_build_thisn_access_method PARAMS ((tree)); | |
304 | ||
305 | static tree build_outer_field_access PARAMS ((tree, tree)); | |
306 | static tree build_outer_field_access_methods PARAMS ((tree)); | |
307 | static tree build_outer_field_access_expr PARAMS ((int, tree, tree, | |
308 | tree, tree)); | |
309 | static tree build_outer_method_access_method PARAMS ((tree)); | |
310 | static tree build_new_access_id PARAMS ((void)); | |
311 | static tree build_outer_field_access_method PARAMS ((tree, tree, tree, | |
312 | tree, tree)); | |
313 | ||
314 | static int outer_field_access_p PARAMS ((tree, tree)); | |
315 | static int outer_field_expanded_access_p PARAMS ((tree, tree *, | |
316 | tree *, tree *)); | |
317 | static tree outer_field_access_fix PARAMS ((tree, tree, tree)); | |
318 | static tree build_incomplete_class_ref PARAMS ((int, tree)); | |
319 | static tree patch_incomplete_class_ref PARAMS ((tree)); | |
320 | static tree create_anonymous_class PARAMS ((int, tree)); | |
321 | static void patch_anonymous_class PARAMS ((tree, tree, tree)); | |
322 | static void add_inner_class_fields PARAMS ((tree, tree)); | |
82371d41 | 323 | |
165f37bc APB |
324 | static tree build_dot_class_method PARAMS ((tree)); |
325 | static tree build_dot_class_method_invocation PARAMS ((tree)); | |
c0b864fc | 326 | static void create_new_parser_context PARAMS ((int)); |
f15b9af9 | 327 | static void mark_parser_ctxt PARAMS ((void *)); |
165f37bc | 328 | |
e04a16fb AG |
329 | /* Number of error found so far. */ |
330 | int java_error_count; | |
331 | /* Number of warning found so far. */ | |
332 | int java_warning_count; | |
ce6e9147 APB |
333 | /* Tell when not to fold, when doing xrefs */ |
334 | int do_not_fold; | |
c2952b01 APB |
335 | /* Cyclic inheritance report, as it can be set by layout_class */ |
336 | char *cyclic_inheritance_report; | |
f15b9af9 | 337 | |
c2952b01 APB |
338 | /* Tell when we're within an instance initializer */ |
339 | static int in_instance_initializer; | |
e04a16fb AG |
340 | |
341 | /* The current parser context */ | |
d4370213 | 342 | struct parser_ctxt *ctxp; |
e04a16fb | 343 | |
d4370213 | 344 | /* List of things that were analyzed for which code will be generated */ |
b351b287 APB |
345 | static struct parser_ctxt *ctxp_for_generation = NULL; |
346 | ||
e04a16fb AG |
347 | /* binop_lookup maps token to tree_code. It is used where binary |
348 | operations are involved and required by the parser. RDIV_EXPR | |
349 | covers both integral/floating point division. The code is changed | |
350 | once the type of both operator is worked out. */ | |
351 | ||
352 | static enum tree_code binop_lookup[19] = | |
353 | { | |
354 | PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR, | |
355 | LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR, | |
356 | BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR, | |
357 | TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR, | |
358 | EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR, | |
359 | }; | |
360 | #define BINOP_LOOKUP(VALUE) \ | |
6e2aa220 | 361 | binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)] |
e04a16fb | 362 | |
5cbdba64 APB |
363 | /* This is the end index for binary operators that can also be used |
364 | in compound assignements. */ | |
365 | #define BINOP_COMPOUND_CANDIDATES 11 | |
366 | ||
e04a16fb | 367 | /* The "$L" identifier we use to create labels. */ |
b67d701b PB |
368 | static tree label_id = NULL_TREE; |
369 | ||
370 | /* The "StringBuffer" identifier used for the String `+' operator. */ | |
371 | static tree wfl_string_buffer = NULL_TREE; | |
372 | ||
373 | /* The "append" identifier used for String `+' operator. */ | |
374 | static tree wfl_append = NULL_TREE; | |
375 | ||
376 | /* The "toString" identifier used for String `+' operator. */ | |
377 | static tree wfl_to_string = NULL_TREE; | |
ba179f9f APB |
378 | |
379 | /* The "java.lang" import qualified name. */ | |
380 | static tree java_lang_id = NULL_TREE; | |
09ed0f70 | 381 | |
c2952b01 APB |
382 | /* The generated `inst$' identifier used for generated enclosing |
383 | instance/field access functions. */ | |
384 | static tree inst_id = NULL_TREE; | |
385 | ||
09ed0f70 APB |
386 | /* The "java.lang.Cloneable" qualified name. */ |
387 | static tree java_lang_cloneable = NULL_TREE; | |
f099f336 | 388 | |
ee17a290 TT |
389 | /* The "java.io.Serializable" qualified name. */ |
390 | static tree java_io_serializable = NULL_TREE; | |
391 | ||
f099f336 APB |
392 | /* Context and flag for static blocks */ |
393 | static tree current_static_block = NULL_TREE; | |
394 | ||
c2952b01 APB |
395 | /* The generated `write_parm_value$' identifier. */ |
396 | static tree wpv_id; | |
397 | ||
ee07f4f4 APB |
398 | /* The list of all packages we've seen so far */ |
399 | static tree package_list = NULL_TREE; | |
2884c41e | 400 | |
19e223db MM |
401 | /* Hold THIS for the scope of the current public method decl. */ |
402 | static tree current_this; | |
403 | ||
404 | /* Hold a list of catch clauses list. The first element of this list is | |
405 | the list of the catch clauses of the currently analysed try block. */ | |
406 | static tree currently_caught_type_list; | |
407 | ||
2884c41e KG |
408 | /* Check modifiers. If one doesn't fit, retrieve it in its declaration |
409 | line and point it out. */ | |
410 | /* Should point out the one that don't fit. ASCII/unicode, going | |
411 | backward. FIXME */ | |
412 | ||
413 | #define check_modifiers(__message, __value, __mask) do { \ | |
414 | if ((__value) & ~(__mask)) \ | |
415 | { \ | |
416 | int i, remainder = (__value) & ~(__mask); \ | |
417 | for (i = 0; i <= 10; i++) \ | |
418 | if ((1 << i) & remainder) \ | |
419 | parse_error_context (ctxp->modifier_ctx [i], (__message), \ | |
420 | java_accstring_lookup (1 << i)); \ | |
421 | } \ | |
422 | } while (0) | |
ee07f4f4 | 423 | |
e04a16fb AG |
424 | %} |
425 | ||
426 | %union { | |
427 | tree node; | |
428 | int sub_token; | |
429 | struct { | |
430 | int token; | |
431 | int location; | |
432 | } operator; | |
433 | int value; | |
434 | } | |
435 | ||
9ee9b555 KG |
436 | %{ |
437 | #include "lex.c" | |
438 | %} | |
439 | ||
e04a16fb AG |
440 | %pure_parser |
441 | ||
442 | /* Things defined here have to match the order of what's in the | |
443 | binop_lookup table. */ | |
444 | ||
445 | %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK | |
446 | %token LS_TK SRS_TK ZRS_TK | |
447 | %token AND_TK XOR_TK OR_TK | |
448 | %token BOOL_AND_TK BOOL_OR_TK | |
449 | %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK | |
450 | ||
451 | /* This maps to the same binop_lookup entry than the token above */ | |
452 | ||
453 | %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK | |
454 | %token REM_ASSIGN_TK | |
455 | %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK | |
456 | %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK | |
457 | ||
458 | ||
459 | /* Modifier TOKEN have to be kept in this order. Don't scramble it */ | |
460 | ||
461 | %token PUBLIC_TK PRIVATE_TK PROTECTED_TK | |
462 | %token STATIC_TK FINAL_TK SYNCHRONIZED_TK | |
463 | %token VOLATILE_TK TRANSIENT_TK NATIVE_TK | |
464 | %token PAD_TK ABSTRACT_TK MODIFIER_TK | |
465 | ||
466 | /* Keep those two in order, too */ | |
467 | %token DECR_TK INCR_TK | |
468 | ||
469 | /* From now one, things can be in any order */ | |
470 | ||
471 | %token DEFAULT_TK IF_TK THROW_TK | |
472 | %token BOOLEAN_TK DO_TK IMPLEMENTS_TK | |
473 | %token THROWS_TK BREAK_TK IMPORT_TK | |
474 | %token ELSE_TK INSTANCEOF_TK RETURN_TK | |
475 | %token VOID_TK CATCH_TK INTERFACE_TK | |
476 | %token CASE_TK EXTENDS_TK FINALLY_TK | |
477 | %token SUPER_TK WHILE_TK CLASS_TK | |
478 | %token SWITCH_TK CONST_TK TRY_TK | |
479 | %token FOR_TK NEW_TK CONTINUE_TK | |
480 | %token GOTO_TK PACKAGE_TK THIS_TK | |
481 | ||
482 | %token BYTE_TK SHORT_TK INT_TK LONG_TK | |
483 | %token CHAR_TK INTEGRAL_TK | |
484 | ||
485 | %token FLOAT_TK DOUBLE_TK FP_TK | |
486 | ||
487 | %token ID_TK | |
488 | ||
489 | %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK | |
490 | ||
491 | %token ASSIGN_ANY_TK ASSIGN_TK | |
492 | %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK | |
493 | ||
494 | %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK | |
495 | %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK | |
496 | ||
c2952b01 | 497 | %type <value> modifiers MODIFIER_TK final synchronized |
e04a16fb AG |
498 | |
499 | %type <node> super ID_TK identifier | |
500 | %type <node> name simple_name qualified_name | |
c2952b01 | 501 | %type <node> type_declaration compilation_unit |
e04a16fb AG |
502 | field_declaration method_declaration extends_interfaces |
503 | interfaces interface_type_list | |
c2952b01 | 504 | class_member_declaration |
e04a16fb AG |
505 | import_declarations package_declaration |
506 | type_declarations interface_body | |
507 | interface_member_declaration constant_declaration | |
508 | interface_member_declarations interface_type | |
509 | abstract_method_declaration interface_type_list | |
510 | %type <node> class_body_declaration class_member_declaration | |
511 | static_initializer constructor_declaration block | |
22eed1e6 | 512 | %type <node> class_body_declarations constructor_header |
e04a16fb AG |
513 | %type <node> class_or_interface_type class_type class_type_list |
514 | constructor_declarator explicit_constructor_invocation | |
b9f7e36c | 515 | %type <node> dim_expr dim_exprs this_or_super throws |
e04a16fb AG |
516 | |
517 | %type <node> variable_declarator_id variable_declarator | |
518 | variable_declarators variable_initializer | |
22eed1e6 | 519 | variable_initializers constructor_body |
ac825856 | 520 | array_initializer |
e04a16fb | 521 | |
2e5eb5c5 | 522 | %type <node> class_body block_end constructor_block_end |
e04a16fb AG |
523 | %type <node> statement statement_without_trailing_substatement |
524 | labeled_statement if_then_statement label_decl | |
525 | if_then_else_statement while_statement for_statement | |
526 | statement_nsi labeled_statement_nsi do_statement | |
527 | if_then_else_statement_nsi while_statement_nsi | |
528 | for_statement_nsi statement_expression_list for_init | |
529 | for_update statement_expression expression_statement | |
530 | primary_no_new_array expression primary | |
531 | array_creation_expression array_type | |
532 | class_instance_creation_expression field_access | |
533 | method_invocation array_access something_dot_new | |
534 | argument_list postfix_expression while_expression | |
535 | post_increment_expression post_decrement_expression | |
536 | unary_expression_not_plus_minus unary_expression | |
537 | pre_increment_expression pre_decrement_expression | |
538 | unary_expression_not_plus_minus cast_expression | |
539 | multiplicative_expression additive_expression | |
540 | shift_expression relational_expression | |
541 | equality_expression and_expression | |
542 | exclusive_or_expression inclusive_or_expression | |
543 | conditional_and_expression conditional_or_expression | |
544 | conditional_expression assignment_expression | |
545 | left_hand_side assignment for_header for_begin | |
546 | constant_expression do_statement_begin empty_statement | |
b67d701b | 547 | switch_statement synchronized_statement throw_statement |
f8976021 | 548 | try_statement switch_expression switch_block |
15fdcfe9 | 549 | catches catch_clause catch_clause_parameter finally |
c2952b01 | 550 | anonymous_class_creation |
e04a16fb AG |
551 | %type <node> return_statement break_statement continue_statement |
552 | ||
553 | %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK | |
554 | %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK | |
555 | %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK | |
556 | %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK | |
557 | %type <operator> ASSIGN_ANY_TK assignment_operator | |
558 | %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK | |
559 | %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK | |
560 | %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK | |
7f10c2e2 | 561 | %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK |
5e942c50 | 562 | %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK |
b9f7e36c APB |
563 | %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK |
564 | %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK | |
c2952b01 | 565 | %type <operator> NEW_TK |
e04a16fb AG |
566 | |
567 | %type <node> method_body | |
568 | ||
569 | %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK | |
570 | STRING_LIT_TK NULL_TK VOID_TK | |
571 | ||
572 | %type <node> IF_TK WHILE_TK FOR_TK | |
573 | ||
574 | %type <node> formal_parameter_list formal_parameter | |
575 | method_declarator method_header | |
576 | ||
c2952b01 | 577 | %type <node> primitive_type reference_type type |
e04a16fb AG |
578 | BOOLEAN_TK INTEGRAL_TK FP_TK |
579 | ||
c2952b01 APB |
580 | /* Added or modified JDK 1.1 rule types */ |
581 | %type <node> type_literals array_type_literal | |
582 | ||
e04a16fb AG |
583 | %% |
584 | /* 19.2 Production from 2.3: The Syntactic Grammar */ | |
585 | goal: | |
19e223db MM |
586 | { |
587 | /* Register static variables with the garbage | |
588 | collector. */ | |
589 | ggc_add_tree_root (&label_id, 1); | |
590 | ggc_add_tree_root (&wfl_string_buffer, 1); | |
591 | ggc_add_tree_root (&wfl_append, 1); | |
592 | ggc_add_tree_root (&wfl_to_string, 1); | |
593 | ggc_add_tree_root (&java_lang_id, 1); | |
594 | ggc_add_tree_root (&inst_id, 1); | |
595 | ggc_add_tree_root (&java_lang_cloneable, 1); | |
596 | ggc_add_tree_root (&java_io_serializable, 1); | |
597 | ggc_add_tree_root (¤t_static_block, 1); | |
598 | ggc_add_tree_root (&wpv_id, 1); | |
599 | ggc_add_tree_root (&package_list, 1); | |
600 | ggc_add_tree_root (¤t_this, 1); | |
601 | ggc_add_tree_root (¤tly_caught_type_list, 1); | |
1f8f4a0b | 602 | ggc_add_string_root (&cyclic_inheritance_report, 1); |
f15b9af9 MM |
603 | ggc_add_root (&ctxp, 1, |
604 | sizeof (struct parser_ctxt *), | |
605 | mark_parser_ctxt); | |
606 | ggc_add_root (&ctxp_for_generation, 1, | |
607 | sizeof (struct parser_ctxt *), | |
608 | mark_parser_ctxt); | |
19e223db | 609 | } |
e04a16fb AG |
610 | compilation_unit |
611 | {} | |
612 | ; | |
613 | ||
614 | /* 19.3 Productions from 3: Lexical structure */ | |
615 | literal: | |
616 | INT_LIT_TK | |
617 | | FP_LIT_TK | |
618 | | BOOL_LIT_TK | |
619 | | CHAR_LIT_TK | |
620 | | STRING_LIT_TK | |
621 | | NULL_TK | |
622 | ; | |
623 | ||
624 | /* 19.4 Productions from 4: Types, Values and Variables */ | |
625 | type: | |
626 | primitive_type | |
627 | | reference_type | |
628 | ; | |
629 | ||
630 | primitive_type: | |
631 | INTEGRAL_TK | |
632 | | FP_TK | |
633 | | BOOLEAN_TK | |
634 | ; | |
635 | ||
636 | reference_type: | |
637 | class_or_interface_type | |
638 | | array_type | |
639 | ; | |
640 | ||
641 | class_or_interface_type: | |
642 | name | |
643 | ; | |
644 | ||
645 | class_type: | |
646 | class_or_interface_type /* Default rule */ | |
647 | ; | |
648 | ||
649 | interface_type: | |
650 | class_or_interface_type | |
651 | ; | |
652 | ||
653 | array_type: | |
654 | primitive_type OSB_TK CSB_TK | |
655 | { | |
656 | $$ = build_java_array_type ($1, -1); | |
657 | CLASS_LOADED_P ($$) = 1; | |
658 | } | |
659 | | name OSB_TK CSB_TK | |
660 | { $$ = build_unresolved_array_type ($1); } | |
661 | | array_type OSB_TK CSB_TK | |
662 | { $$ = build_unresolved_array_type ($1); } | |
663 | | primitive_type OSB_TK error | |
664 | {RULE ("']' expected"); RECOVER;} | |
665 | | array_type OSB_TK error | |
666 | {RULE ("']' expected"); RECOVER;} | |
667 | ; | |
668 | ||
669 | /* 19.5 Productions from 6: Names */ | |
670 | name: | |
671 | simple_name /* Default rule */ | |
672 | | qualified_name /* Default rule */ | |
673 | ; | |
674 | ||
675 | simple_name: | |
676 | identifier /* Default rule */ | |
677 | ; | |
678 | ||
679 | qualified_name: | |
680 | name DOT_TK identifier | |
681 | { $$ = make_qualified_name ($1, $3, $2.location); } | |
682 | ; | |
683 | ||
684 | identifier: | |
685 | ID_TK | |
686 | ; | |
687 | ||
688 | /* 19.6: Production from 7: Packages */ | |
689 | compilation_unit: | |
690 | {$$ = NULL;} | |
691 | | package_declaration | |
692 | | import_declarations | |
693 | | type_declarations | |
694 | | package_declaration import_declarations | |
695 | | package_declaration type_declarations | |
696 | | import_declarations type_declarations | |
697 | | package_declaration import_declarations type_declarations | |
698 | ; | |
699 | ||
700 | import_declarations: | |
701 | import_declaration | |
702 | { | |
703 | $$ = NULL; | |
704 | } | |
705 | | import_declarations import_declaration | |
706 | { | |
707 | $$ = NULL; | |
708 | } | |
709 | ; | |
710 | ||
711 | type_declarations: | |
712 | type_declaration | |
713 | | type_declarations type_declaration | |
714 | ; | |
715 | ||
716 | package_declaration: | |
717 | PACKAGE_TK name SC_TK | |
ee07f4f4 APB |
718 | { |
719 | ctxp->package = EXPR_WFL_NODE ($2); | |
9a7ab4b3 | 720 | register_package (ctxp->package); |
ee07f4f4 | 721 | } |
e04a16fb AG |
722 | | PACKAGE_TK error |
723 | {yyerror ("Missing name"); RECOVER;} | |
724 | | PACKAGE_TK name error | |
725 | {yyerror ("';' expected"); RECOVER;} | |
726 | ; | |
727 | ||
728 | import_declaration: | |
729 | single_type_import_declaration | |
730 | | type_import_on_demand_declaration | |
731 | ; | |
732 | ||
733 | single_type_import_declaration: | |
734 | IMPORT_TK name SC_TK | |
735 | { | |
9a7ab4b3 | 736 | tree name = EXPR_WFL_NODE ($2), last_name; |
e04a16fb | 737 | int i = IDENTIFIER_LENGTH (name)-1; |
49f48c71 | 738 | const char *last = &IDENTIFIER_POINTER (name)[i]; |
e04a16fb AG |
739 | while (last != IDENTIFIER_POINTER (name)) |
740 | { | |
741 | if (last [0] == '.') | |
742 | break; | |
743 | last--; | |
744 | } | |
745 | last_name = get_identifier (++last); | |
746 | if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name)) | |
747 | { | |
748 | tree err = find_name_in_single_imports (last_name); | |
749 | if (err && err != name) | |
750 | parse_error_context | |
751 | ($2, "Ambiguous class: `%s' and `%s'", | |
752 | IDENTIFIER_POINTER (name), | |
753 | IDENTIFIER_POINTER (err)); | |
5e942c50 | 754 | else |
9a7ab4b3 | 755 | REGISTER_IMPORT ($2, last_name); |
e04a16fb AG |
756 | } |
757 | else | |
5e942c50 | 758 | REGISTER_IMPORT ($2, last_name); |
e04a16fb AG |
759 | } |
760 | | IMPORT_TK error | |
761 | {yyerror ("Missing name"); RECOVER;} | |
762 | | IMPORT_TK name error | |
763 | {yyerror ("';' expected"); RECOVER;} | |
764 | ; | |
765 | ||
766 | type_import_on_demand_declaration: | |
767 | IMPORT_TK name DOT_TK MULT_TK SC_TK | |
768 | { | |
769 | tree name = EXPR_WFL_NODE ($2); | |
ba179f9f APB |
770 | /* Don't import java.lang.* twice. */ |
771 | if (name != java_lang_id) | |
772 | { | |
ba179f9f | 773 | read_import_dir ($2); |
9a7ab4b3 APB |
774 | ctxp->import_demand_list = |
775 | chainon (ctxp->import_demand_list, | |
776 | build_tree_list ($2, NULL_TREE)); | |
ba179f9f | 777 | } |
e04a16fb AG |
778 | } |
779 | | IMPORT_TK name DOT_TK error | |
780 | {yyerror ("'*' expected"); RECOVER;} | |
781 | | IMPORT_TK name DOT_TK MULT_TK error | |
782 | {yyerror ("';' expected"); RECOVER;} | |
783 | ; | |
784 | ||
785 | type_declaration: | |
786 | class_declaration | |
c2952b01 | 787 | { end_class_declaration (0); } |
e04a16fb | 788 | | interface_declaration |
c2952b01 | 789 | { end_class_declaration (0); } |
5f1c312a | 790 | | empty_statement |
e04a16fb AG |
791 | | error |
792 | { | |
793 | YYERROR_NOW; | |
794 | yyerror ("Class or interface declaration expected"); | |
795 | } | |
796 | ; | |
797 | ||
798 | /* 19.7 Shortened from the original: | |
799 | modifiers: modifier | modifiers modifier | |
800 | modifier: any of public... */ | |
801 | modifiers: | |
802 | MODIFIER_TK | |
803 | { | |
804 | $$ = (1 << $1); | |
805 | } | |
806 | | modifiers MODIFIER_TK | |
807 | { | |
808 | int acc = (1 << $2); | |
809 | if ($$ & acc) | |
810 | parse_error_context | |
811 | (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice", | |
812 | java_accstring_lookup (acc)); | |
813 | else | |
814 | { | |
815 | $$ |= acc; | |
816 | } | |
817 | } | |
818 | ; | |
819 | ||
820 | /* 19.8.1 Production from $8.1: Class Declaration */ | |
821 | class_declaration: | |
822 | modifiers CLASS_TK identifier super interfaces | |
823 | { create_class ($1, $3, $4, $5); } | |
824 | class_body | |
e04a16fb AG |
825 | | CLASS_TK identifier super interfaces |
826 | { create_class (0, $2, $3, $4); } | |
827 | class_body | |
e04a16fb AG |
828 | | modifiers CLASS_TK error |
829 | {yyerror ("Missing class name"); RECOVER;} | |
830 | | CLASS_TK error | |
831 | {yyerror ("Missing class name"); RECOVER;} | |
832 | | CLASS_TK identifier error | |
0b4d333e APB |
833 | { |
834 | if (!ctxp->class_err) yyerror ("'{' expected"); | |
835 | DRECOVER(class1); | |
836 | } | |
e04a16fb AG |
837 | | modifiers CLASS_TK identifier error |
838 | {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;} | |
839 | ; | |
840 | ||
841 | super: | |
842 | { $$ = NULL; } | |
843 | | EXTENDS_TK class_type | |
844 | { $$ = $2; } | |
845 | | EXTENDS_TK class_type error | |
846 | {yyerror ("'{' expected"); ctxp->class_err=1;} | |
847 | | EXTENDS_TK error | |
848 | {yyerror ("Missing super class name"); ctxp->class_err=1;} | |
849 | ; | |
850 | ||
851 | interfaces: | |
852 | { $$ = NULL_TREE; } | |
853 | | IMPLEMENTS_TK interface_type_list | |
854 | { $$ = $2; } | |
855 | | IMPLEMENTS_TK error | |
856 | { | |
857 | ctxp->class_err=1; | |
858 | yyerror ("Missing interface name"); | |
859 | } | |
860 | ; | |
861 | ||
862 | interface_type_list: | |
863 | interface_type | |
864 | { | |
865 | ctxp->interface_number = 1; | |
866 | $$ = build_tree_list ($1, NULL_TREE); | |
867 | } | |
868 | | interface_type_list C_TK interface_type | |
869 | { | |
870 | ctxp->interface_number++; | |
871 | $$ = chainon ($1, build_tree_list ($3, NULL_TREE)); | |
872 | } | |
873 | | interface_type_list C_TK error | |
874 | {yyerror ("Missing interface name"); RECOVER;} | |
875 | ; | |
876 | ||
877 | class_body: | |
878 | OCB_TK CCB_TK | |
7f10c2e2 APB |
879 | { |
880 | /* Store the location of the `}' when doing xrefs */ | |
881 | if (flag_emit_xref) | |
c2952b01 | 882 | DECL_END_SOURCE_LINE (GET_CPC ()) = |
7f10c2e2 | 883 | EXPR_WFL_ADD_COL ($2.location, 1); |
c2952b01 | 884 | $$ = GET_CPC (); |
7f10c2e2 | 885 | } |
e04a16fb | 886 | | OCB_TK class_body_declarations CCB_TK |
7f10c2e2 APB |
887 | { |
888 | /* Store the location of the `}' when doing xrefs */ | |
889 | if (flag_emit_xref) | |
c2952b01 | 890 | DECL_END_SOURCE_LINE (GET_CPC ()) = |
7f10c2e2 | 891 | EXPR_WFL_ADD_COL ($3.location, 1); |
c2952b01 | 892 | $$ = GET_CPC (); |
7f10c2e2 | 893 | } |
e04a16fb AG |
894 | ; |
895 | ||
896 | class_body_declarations: | |
897 | class_body_declaration | |
898 | | class_body_declarations class_body_declaration | |
899 | ; | |
900 | ||
901 | class_body_declaration: | |
902 | class_member_declaration | |
903 | | static_initializer | |
904 | | constructor_declaration | |
905 | | block /* Added, JDK1.1, instance initializer */ | |
c2952b01 APB |
906 | { |
907 | TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp); | |
908 | SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1); | |
909 | } | |
e04a16fb AG |
910 | ; |
911 | ||
912 | class_member_declaration: | |
913 | field_declaration | |
914 | | method_declaration | |
915 | | class_declaration /* Added, JDK1.1 inner classes */ | |
c2952b01 APB |
916 | { end_class_declaration (1); } |
917 | | interface_declaration /* Added, JDK1.1 inner interfaces */ | |
918 | { end_class_declaration (1); } | |
5f1c312a | 919 | | empty_statement |
e04a16fb AG |
920 | ; |
921 | ||
922 | /* 19.8.2 Productions from 8.3: Field Declarations */ | |
923 | field_declaration: | |
924 | type variable_declarators SC_TK | |
925 | { register_fields (0, $1, $2); } | |
926 | | modifiers type variable_declarators SC_TK | |
927 | { | |
e04a16fb AG |
928 | check_modifiers |
929 | ("Illegal modifier `%s' for field declaration", | |
930 | $1, FIELD_MODIFIERS); | |
931 | check_modifiers_consistency ($1); | |
932 | register_fields ($1, $2, $3); | |
933 | } | |
934 | ; | |
935 | ||
936 | variable_declarators: | |
937 | /* Should we use build_decl_list () instead ? FIXME */ | |
938 | variable_declarator /* Default rule */ | |
939 | | variable_declarators C_TK variable_declarator | |
940 | { $$ = chainon ($1, $3); } | |
941 | | variable_declarators C_TK error | |
942 | {yyerror ("Missing term"); RECOVER;} | |
943 | ; | |
944 | ||
945 | variable_declarator: | |
946 | variable_declarator_id | |
947 | { $$ = build_tree_list ($1, NULL_TREE); } | |
948 | | variable_declarator_id ASSIGN_TK variable_initializer | |
949 | { | |
950 | if (java_error_count) | |
951 | $3 = NULL_TREE; | |
952 | $$ = build_tree_list | |
953 | ($1, build_assignment ($2.token, $2.location, $1, $3)); | |
954 | } | |
955 | | variable_declarator_id ASSIGN_TK error | |
956 | { | |
957 | yyerror ("Missing variable initializer"); | |
958 | $$ = build_tree_list ($1, NULL_TREE); | |
959 | RECOVER; | |
960 | } | |
961 | | variable_declarator_id ASSIGN_TK variable_initializer error | |
962 | { | |
963 | yyerror ("';' expected"); | |
964 | $$ = build_tree_list ($1, NULL_TREE); | |
965 | RECOVER; | |
966 | } | |
967 | ; | |
968 | ||
969 | variable_declarator_id: | |
970 | identifier | |
971 | | variable_declarator_id OSB_TK CSB_TK | |
c583dd46 | 972 | { $$ = build_unresolved_array_type ($1); } |
e04a16fb AG |
973 | | identifier error |
974 | {yyerror ("Invalid declaration"); DRECOVER(vdi);} | |
975 | | variable_declarator_id OSB_TK error | |
29f8b718 APB |
976 | { |
977 | tree node = java_lval.node; | |
978 | if (node && (TREE_CODE (node) == INTEGER_CST | |
979 | || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION)) | |
980 | yyerror ("Can't specify array dimension in a declaration"); | |
981 | else | |
982 | yyerror ("']' expected"); | |
983 | DRECOVER(vdi); | |
984 | } | |
e04a16fb AG |
985 | | variable_declarator_id CSB_TK error |
986 | {yyerror ("Unbalanced ']'"); DRECOVER(vdi);} | |
987 | ; | |
988 | ||
989 | variable_initializer: | |
990 | expression | |
991 | | array_initializer | |
e04a16fb AG |
992 | ; |
993 | ||
994 | /* 19.8.3 Productions from 8.4: Method Declarations */ | |
995 | method_declaration: | |
996 | method_header | |
997 | { | |
998 | current_function_decl = $1; | |
c2952b01 APB |
999 | if (current_function_decl |
1000 | && TREE_CODE (current_function_decl) == FUNCTION_DECL) | |
1001 | source_start_java_method (current_function_decl); | |
1002 | else | |
1003 | current_function_decl = NULL_TREE; | |
e04a16fb AG |
1004 | } |
1005 | method_body | |
b635eb2f | 1006 | { finish_method_declaration ($3); } |
e04a16fb AG |
1007 | | method_header error |
1008 | {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;} | |
1009 | ; | |
1010 | ||
1011 | method_header: | |
1012 | type method_declarator throws | |
b9f7e36c | 1013 | { $$ = method_header (0, $1, $2, $3); } |
e04a16fb | 1014 | | VOID_TK method_declarator throws |
b9f7e36c | 1015 | { $$ = method_header (0, void_type_node, $2, $3); } |
e04a16fb | 1016 | | modifiers type method_declarator throws |
b9f7e36c | 1017 | { $$ = method_header ($1, $2, $3, $4); } |
e04a16fb | 1018 | | modifiers VOID_TK method_declarator throws |
b9f7e36c | 1019 | { $$ = method_header ($1, void_type_node, $3, $4); } |
e04a16fb | 1020 | | type error |
efa0a23f APB |
1021 | { |
1022 | yyerror ("Invalid method declaration, method name required"); | |
1023 | RECOVER; | |
1024 | } | |
e04a16fb AG |
1025 | | modifiers type error |
1026 | {RECOVER;} | |
1027 | | VOID_TK error | |
1028 | {yyerror ("Identifier expected"); RECOVER;} | |
1029 | | modifiers VOID_TK error | |
1030 | {yyerror ("Identifier expected"); RECOVER;} | |
1031 | | modifiers error | |
1032 | { | |
1033 | yyerror ("Invalid method declaration, return type required"); | |
1034 | RECOVER; | |
1035 | } | |
1036 | ; | |
1037 | ||
1038 | method_declarator: | |
1039 | identifier OP_TK CP_TK | |
c2952b01 APB |
1040 | { |
1041 | ctxp->formal_parameter_number = 0; | |
1042 | $$ = method_declarator ($1, NULL_TREE); | |
1043 | } | |
e04a16fb AG |
1044 | | identifier OP_TK formal_parameter_list CP_TK |
1045 | { $$ = method_declarator ($1, $3); } | |
1046 | | method_declarator OSB_TK CSB_TK | |
1047 | { | |
1886c9d8 APB |
1048 | EXPR_WFL_LINECOL (wfl_operator) = $2.location; |
1049 | TREE_PURPOSE ($1) = | |
1050 | build_unresolved_array_type (TREE_PURPOSE ($1)); | |
1051 | parse_warning_context | |
1052 | (wfl_operator, | |
1053 | "Discouraged form of returned type specification"); | |
e04a16fb AG |
1054 | } |
1055 | | identifier OP_TK error | |
1056 | {yyerror ("')' expected"); DRECOVER(method_declarator);} | |
1057 | | method_declarator OSB_TK error | |
1058 | {yyerror ("']' expected"); RECOVER;} | |
1059 | ; | |
1060 | ||
1061 | formal_parameter_list: | |
1062 | formal_parameter | |
1063 | { | |
1064 | ctxp->formal_parameter_number = 1; | |
1065 | } | |
1066 | | formal_parameter_list C_TK formal_parameter | |
1067 | { | |
1068 | ctxp->formal_parameter_number += 1; | |
1069 | $$ = chainon ($1, $3); | |
1070 | } | |
1071 | | formal_parameter_list C_TK error | |
c2952b01 | 1072 | { yyerror ("Missing formal parameter term"); RECOVER; } |
e04a16fb AG |
1073 | ; |
1074 | ||
1075 | formal_parameter: | |
1076 | type variable_declarator_id | |
1077 | { | |
1078 | $$ = build_tree_list ($2, $1); | |
1079 | } | |
18990de5 | 1080 | | final type variable_declarator_id /* Added, JDK1.1 final parms */ |
5256aa37 | 1081 | { |
5256aa37 | 1082 | $$ = build_tree_list ($3, $2); |
c2952b01 | 1083 | ARG_FINAL_P ($$) = 1; |
5256aa37 | 1084 | } |
e04a16fb | 1085 | | type error |
f8989a66 APB |
1086 | { |
1087 | yyerror ("Missing identifier"); RECOVER; | |
1088 | $$ = NULL_TREE; | |
1089 | } | |
18990de5 | 1090 | | final type error |
e04a16fb | 1091 | { |
e04a16fb | 1092 | yyerror ("Missing identifier"); RECOVER; |
f8989a66 | 1093 | $$ = NULL_TREE; |
e04a16fb AG |
1094 | } |
1095 | ; | |
1096 | ||
18990de5 JB |
1097 | final: |
1098 | modifiers | |
1099 | { | |
1100 | check_modifiers ("Illegal modifier `%s'. Only `final' was expected here", | |
1101 | $1, ACC_FINAL); | |
1102 | if ($1 != ACC_FINAL) | |
1103 | MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE); | |
1104 | } | |
1105 | ; | |
1106 | ||
e04a16fb | 1107 | throws: |
b9f7e36c | 1108 | { $$ = NULL_TREE; } |
e04a16fb | 1109 | | THROWS_TK class_type_list |
b9f7e36c | 1110 | { $$ = $2; } |
e04a16fb AG |
1111 | | THROWS_TK error |
1112 | {yyerror ("Missing class type term"); RECOVER;} | |
1113 | ; | |
1114 | ||
1115 | class_type_list: | |
1116 | class_type | |
c877974e | 1117 | { $$ = build_tree_list ($1, $1); } |
e04a16fb | 1118 | | class_type_list C_TK class_type |
c877974e | 1119 | { $$ = tree_cons ($3, $3, $1); } |
e04a16fb AG |
1120 | | class_type_list C_TK error |
1121 | {yyerror ("Missing class type term"); RECOVER;} | |
1122 | ; | |
1123 | ||
1124 | method_body: | |
1125 | block | |
5f1c312a | 1126 | | SC_TK { $$ = NULL_TREE; } |
e04a16fb AG |
1127 | ; |
1128 | ||
1129 | /* 19.8.4 Productions from 8.5: Static Initializers */ | |
1130 | static_initializer: | |
1131 | static block | |
1132 | { | |
c2952b01 APB |
1133 | TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp); |
1134 | SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2); | |
dba41d30 | 1135 | current_static_block = NULL_TREE; |
e04a16fb | 1136 | } |
e04a16fb AG |
1137 | ; |
1138 | ||
1139 | static: /* Test lval.sub_token here */ | |
c2952b01 | 1140 | modifiers |
e04a16fb | 1141 | { |
c2952b01 APB |
1142 | check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC); |
1143 | /* Can't have a static initializer in an innerclass */ | |
1144 | if ($1 | ACC_STATIC && | |
1145 | GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ())) | |
1146 | parse_error_context | |
1147 | (MODIFIER_WFL (STATIC_TK), | |
1148 | "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes", | |
1149 | IDENTIFIER_POINTER (DECL_NAME (GET_CPC ()))); | |
e04a16fb AG |
1150 | SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1)); |
1151 | } | |
1152 | ; | |
1153 | ||
1154 | /* 19.8.5 Productions from 8.6: Constructor Declarations */ | |
e04a16fb | 1155 | constructor_declaration: |
22eed1e6 | 1156 | constructor_header |
e04a16fb | 1157 | { |
22eed1e6 APB |
1158 | current_function_decl = $1; |
1159 | source_start_java_method (current_function_decl); | |
e04a16fb | 1160 | } |
22eed1e6 | 1161 | constructor_body |
b635eb2f | 1162 | { finish_method_declaration ($3); } |
22eed1e6 APB |
1163 | ; |
1164 | ||
1165 | constructor_header: | |
1166 | constructor_declarator throws | |
1167 | { $$ = method_header (0, NULL_TREE, $1, $2); } | |
1168 | | modifiers constructor_declarator throws | |
1169 | { $$ = method_header ($1, NULL_TREE, $2, $3); } | |
e04a16fb AG |
1170 | ; |
1171 | ||
1172 | constructor_declarator: | |
1173 | simple_name OP_TK CP_TK | |
c2952b01 APB |
1174 | { |
1175 | ctxp->formal_parameter_number = 0; | |
1176 | $$ = method_declarator ($1, NULL_TREE); | |
1177 | } | |
e04a16fb | 1178 | | simple_name OP_TK formal_parameter_list CP_TK |
22eed1e6 | 1179 | { $$ = method_declarator ($1, $3); } |
e04a16fb AG |
1180 | ; |
1181 | ||
1182 | constructor_body: | |
22eed1e6 APB |
1183 | /* Unlike regular method, we always need a complete (empty) |
1184 | body so we can safely perform all the required code | |
1185 | addition (super invocation and field initialization) */ | |
2e5eb5c5 | 1186 | block_begin constructor_block_end |
22eed1e6 | 1187 | { |
9bbc7d9f | 1188 | BLOCK_EXPR_BODY ($2) = empty_stmt_node; |
22eed1e6 APB |
1189 | $$ = $2; |
1190 | } | |
2e5eb5c5 | 1191 | | block_begin explicit_constructor_invocation constructor_block_end |
22eed1e6 | 1192 | { $$ = $3; } |
2e5eb5c5 | 1193 | | block_begin block_statements constructor_block_end |
22eed1e6 | 1194 | { $$ = $3; } |
2e5eb5c5 | 1195 | | block_begin explicit_constructor_invocation block_statements constructor_block_end |
22eed1e6 | 1196 | { $$ = $4; } |
e04a16fb AG |
1197 | ; |
1198 | ||
2e5eb5c5 APB |
1199 | constructor_block_end: |
1200 | block_end | |
5f1c312a | 1201 | ; |
2e5eb5c5 | 1202 | |
e04a16fb AG |
1203 | /* Error recovery for that rule moved down expression_statement: rule. */ |
1204 | explicit_constructor_invocation: | |
1205 | this_or_super OP_TK CP_TK SC_TK | |
22eed1e6 APB |
1206 | { |
1207 | $$ = build_method_invocation ($1, NULL_TREE); | |
1208 | $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$); | |
1209 | $$ = java_method_add_stmt (current_function_decl, $$); | |
1210 | } | |
e04a16fb | 1211 | | this_or_super OP_TK argument_list CP_TK SC_TK |
22eed1e6 APB |
1212 | { |
1213 | $$ = build_method_invocation ($1, $3); | |
1214 | $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$); | |
1215 | $$ = java_method_add_stmt (current_function_decl, $$); | |
1216 | } | |
e04a16fb AG |
1217 | /* Added, JDK1.1 inner classes. Modified because the rule |
1218 | 'primary' couldn't work. */ | |
1219 | | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK | |
b67d701b | 1220 | {$$ = parse_jdk1_1_error ("explicit constructor invocation"); } |
e04a16fb | 1221 | | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK |
b67d701b | 1222 | {$$ = parse_jdk1_1_error ("explicit constructor invocation"); } |
e04a16fb AG |
1223 | ; |
1224 | ||
1225 | this_or_super: /* Added, simplifies error diagnostics */ | |
1226 | THIS_TK | |
1227 | { | |
9ee9b555 | 1228 | tree wfl = build_wfl_node (this_identifier_node); |
e04a16fb AG |
1229 | EXPR_WFL_LINECOL (wfl) = $1.location; |
1230 | $$ = wfl; | |
1231 | } | |
1232 | | SUPER_TK | |
1233 | { | |
9ee9b555 | 1234 | tree wfl = build_wfl_node (super_identifier_node); |
e04a16fb AG |
1235 | EXPR_WFL_LINECOL (wfl) = $1.location; |
1236 | $$ = wfl; | |
1237 | } | |
1238 | ; | |
1239 | ||
1240 | /* 19.9 Productions from 9: Interfaces */ | |
1241 | /* 19.9.1 Productions from 9.1: Interfaces Declarations */ | |
1242 | interface_declaration: | |
1243 | INTERFACE_TK identifier | |
1244 | { create_interface (0, $2, NULL_TREE); } | |
1245 | interface_body | |
e04a16fb AG |
1246 | | modifiers INTERFACE_TK identifier |
1247 | { create_interface ($1, $3, NULL_TREE); } | |
1248 | interface_body | |
e04a16fb AG |
1249 | | INTERFACE_TK identifier extends_interfaces |
1250 | { create_interface (0, $2, $3); } | |
1251 | interface_body | |
e04a16fb AG |
1252 | | modifiers INTERFACE_TK identifier extends_interfaces |
1253 | { create_interface ($1, $3, $4); } | |
1254 | interface_body | |
e04a16fb | 1255 | | INTERFACE_TK identifier error |
0b4d333e | 1256 | {yyerror ("'{' expected"); RECOVER;} |
e04a16fb | 1257 | | modifiers INTERFACE_TK identifier error |
0b4d333e | 1258 | {yyerror ("'{' expected"); RECOVER;} |
e04a16fb AG |
1259 | ; |
1260 | ||
1261 | extends_interfaces: | |
1262 | EXTENDS_TK interface_type | |
1263 | { | |
1264 | ctxp->interface_number = 1; | |
1265 | $$ = build_tree_list ($2, NULL_TREE); | |
1266 | } | |
1267 | | extends_interfaces C_TK interface_type | |
1268 | { | |
1269 | ctxp->interface_number++; | |
1270 | $$ = chainon ($1, build_tree_list ($3, NULL_TREE)); | |
1271 | } | |
1272 | | EXTENDS_TK error | |
1273 | {yyerror ("Invalid interface type"); RECOVER;} | |
1274 | | extends_interfaces C_TK error | |
1275 | {yyerror ("Missing term"); RECOVER;} | |
1276 | ; | |
1277 | ||
1278 | interface_body: | |
1279 | OCB_TK CCB_TK | |
1280 | { $$ = NULL_TREE; } | |
1281 | | OCB_TK interface_member_declarations CCB_TK | |
1282 | { $$ = NULL_TREE; } | |
1283 | ; | |
1284 | ||
1285 | interface_member_declarations: | |
1286 | interface_member_declaration | |
1287 | | interface_member_declarations interface_member_declaration | |
1288 | ; | |
1289 | ||
1290 | interface_member_declaration: | |
1291 | constant_declaration | |
1292 | | abstract_method_declaration | |
1293 | | class_declaration /* Added, JDK1.1 inner classes */ | |
c2952b01 APB |
1294 | { end_class_declaration (1); } |
1295 | | interface_declaration /* Added, JDK1.1 inner interfaces */ | |
1296 | { end_class_declaration (1); } | |
e04a16fb AG |
1297 | ; |
1298 | ||
1299 | constant_declaration: | |
1300 | field_declaration | |
1301 | ; | |
1302 | ||
1303 | abstract_method_declaration: | |
1304 | method_header SC_TK | |
1305 | { | |
1306 | check_abstract_method_header ($1); | |
1307 | current_function_decl = NULL_TREE; /* FIXME ? */ | |
1308 | } | |
1309 | | method_header error | |
1310 | {yyerror ("';' expected"); RECOVER;} | |
1311 | ; | |
1312 | ||
1313 | /* 19.10 Productions from 10: Arrays */ | |
1314 | array_initializer: | |
1315 | OCB_TK CCB_TK | |
1179ebc2 | 1316 | { $$ = build_new_array_init ($1.location, NULL_TREE); } |
e04a16fb | 1317 | | OCB_TK variable_initializers CCB_TK |
f8976021 | 1318 | { $$ = build_new_array_init ($1.location, $2); } |
e04a16fb | 1319 | | OCB_TK variable_initializers C_TK CCB_TK |
f8976021 | 1320 | { $$ = build_new_array_init ($1.location, $2); } |
e04a16fb AG |
1321 | ; |
1322 | ||
1323 | variable_initializers: | |
1324 | variable_initializer | |
f8976021 APB |
1325 | { |
1326 | $$ = tree_cons (maybe_build_array_element_wfl ($1), | |
1327 | $1, NULL_TREE); | |
1328 | } | |
e04a16fb | 1329 | | variable_initializers C_TK variable_initializer |
1179ebc2 APB |
1330 | { |
1331 | $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1); | |
1332 | } | |
e04a16fb AG |
1333 | | variable_initializers C_TK error |
1334 | {yyerror ("Missing term"); RECOVER;} | |
1335 | ; | |
1336 | ||
1337 | /* 19.11 Production from 14: Blocks and Statements */ | |
1338 | block: | |
1339 | OCB_TK CCB_TK | |
7f10c2e2 APB |
1340 | { |
1341 | /* Store the location of the `}' when doing xrefs */ | |
1342 | if (current_function_decl && flag_emit_xref) | |
1343 | DECL_END_SOURCE_LINE (current_function_decl) = | |
1344 | EXPR_WFL_ADD_COL ($2.location, 1); | |
1345 | $$ = empty_stmt_node; | |
1346 | } | |
22eed1e6 APB |
1347 | | block_begin block_statements block_end |
1348 | { $$ = $3; } | |
1349 | ; | |
1350 | ||
1351 | block_begin: | |
1352 | OCB_TK | |
e04a16fb | 1353 | { enter_block (); } |
22eed1e6 APB |
1354 | ; |
1355 | ||
1356 | block_end: | |
e04a16fb AG |
1357 | CCB_TK |
1358 | { | |
1359 | maybe_absorb_scoping_blocks (); | |
7f10c2e2 APB |
1360 | /* Store the location of the `}' when doing xrefs */ |
1361 | if (current_function_decl && flag_emit_xref) | |
1362 | DECL_END_SOURCE_LINE (current_function_decl) = | |
1363 | EXPR_WFL_ADD_COL ($1.location, 1); | |
e04a16fb | 1364 | $$ = exit_block (); |
c280e37a APB |
1365 | if (!BLOCK_SUBBLOCKS ($$)) |
1366 | BLOCK_SUBBLOCKS ($$) = empty_stmt_node; | |
e04a16fb AG |
1367 | } |
1368 | ; | |
1369 | ||
1370 | block_statements: | |
1371 | block_statement | |
1372 | | block_statements block_statement | |
1373 | ; | |
1374 | ||
1375 | block_statement: | |
1376 | local_variable_declaration_statement | |
1377 | | statement | |
15fdcfe9 | 1378 | { java_method_add_stmt (current_function_decl, $1); } |
c2952b01 APB |
1379 | | class_declaration /* Added, JDK1.1 local classes */ |
1380 | { | |
1381 | LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1; | |
1382 | end_class_declaration (1); | |
1383 | } | |
e04a16fb AG |
1384 | ; |
1385 | ||
1386 | local_variable_declaration_statement: | |
1387 | local_variable_declaration SC_TK /* Can't catch missing ';' here */ | |
1388 | ; | |
1389 | ||
1390 | local_variable_declaration: | |
1391 | type variable_declarators | |
1392 | { declare_local_variables (0, $1, $2); } | |
a003f638 | 1393 | | final type variable_declarators /* Added, JDK1.1 final locals */ |
e04a16fb AG |
1394 | { declare_local_variables ($1, $2, $3); } |
1395 | ; | |
1396 | ||
1397 | statement: | |
1398 | statement_without_trailing_substatement | |
1399 | | labeled_statement | |
e04a16fb | 1400 | | if_then_statement |
e04a16fb | 1401 | | if_then_else_statement |
e04a16fb | 1402 | | while_statement |
e04a16fb | 1403 | | for_statement |
cd9643f7 | 1404 | { $$ = exit_block (); } |
e04a16fb AG |
1405 | ; |
1406 | ||
1407 | statement_nsi: | |
1408 | statement_without_trailing_substatement | |
1409 | | labeled_statement_nsi | |
e04a16fb | 1410 | | if_then_else_statement_nsi |
e04a16fb | 1411 | | while_statement_nsi |
e04a16fb | 1412 | | for_statement_nsi |
9dd939b2 | 1413 | { $$ = exit_block (); } |
e04a16fb AG |
1414 | ; |
1415 | ||
1416 | statement_without_trailing_substatement: | |
1417 | block | |
e04a16fb | 1418 | | empty_statement |
e04a16fb | 1419 | | expression_statement |
e04a16fb | 1420 | | switch_statement |
e04a16fb | 1421 | | do_statement |
e04a16fb | 1422 | | break_statement |
e04a16fb | 1423 | | continue_statement |
e04a16fb AG |
1424 | | return_statement |
1425 | | synchronized_statement | |
e04a16fb | 1426 | | throw_statement |
e04a16fb | 1427 | | try_statement |
e04a16fb AG |
1428 | ; |
1429 | ||
1430 | empty_statement: | |
1431 | SC_TK | |
5f1c312a APB |
1432 | { |
1433 | if (flag_extraneous_semicolon) | |
1434 | { | |
1435 | EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1); | |
1436 | parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used"); | |
1437 | } | |
1438 | $$ = empty_stmt_node; | |
1439 | } | |
e04a16fb AG |
1440 | ; |
1441 | ||
1442 | label_decl: | |
1443 | identifier REL_CL_TK | |
1444 | { | |
1445 | $$ = build_labeled_block (EXPR_WFL_LINECOL ($1), | |
0a2138e2 | 1446 | EXPR_WFL_NODE ($1)); |
e04a16fb AG |
1447 | pushlevel (2); |
1448 | push_labeled_block ($$); | |
1449 | PUSH_LABELED_BLOCK ($$); | |
1450 | } | |
1451 | ; | |
1452 | ||
1453 | labeled_statement: | |
1454 | label_decl statement | |
b635eb2f | 1455 | { $$ = finish_labeled_statement ($1, $2); } |
e04a16fb AG |
1456 | | identifier error |
1457 | {yyerror ("':' expected"); RECOVER;} | |
1458 | ; | |
1459 | ||
1460 | labeled_statement_nsi: | |
1461 | label_decl statement_nsi | |
b635eb2f | 1462 | { $$ = finish_labeled_statement ($1, $2); } |
e04a16fb AG |
1463 | ; |
1464 | ||
1465 | /* We concentrate here a bunch of error handling rules that we couldn't write | |
1466 | earlier, because expression_statement catches a missing ';'. */ | |
1467 | expression_statement: | |
1468 | statement_expression SC_TK | |
1469 | { | |
1470 | /* We have a statement. Generate a WFL around it so | |
1471 | we can debug it */ | |
1472 | $$ = build_expr_wfl ($1, input_filename, lineno, 0); | |
1473 | /* We know we have a statement, so set the debug | |
1474 | info to be eventually generate here. */ | |
1475 | $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$); | |
1476 | } | |
1477 | | error SC_TK | |
1478 | { | |
29f8b718 | 1479 | YYNOT_TWICE yyerror ("Invalid expression statement"); |
e04a16fb AG |
1480 | DRECOVER (expr_stmt); |
1481 | } | |
1482 | | error OCB_TK | |
1483 | { | |
29f8b718 | 1484 | YYNOT_TWICE yyerror ("Invalid expression statement"); |
e04a16fb AG |
1485 | DRECOVER (expr_stmt); |
1486 | } | |
1487 | | error CCB_TK | |
1488 | { | |
29f8b718 | 1489 | YYNOT_TWICE yyerror ("Invalid expression statement"); |
e04a16fb AG |
1490 | DRECOVER (expr_stmt); |
1491 | } | |
1492 | | this_or_super OP_TK error | |
1493 | {yyerror ("')' expected"); RECOVER;} | |
1494 | | this_or_super OP_TK CP_TK error | |
22eed1e6 | 1495 | { |
8119c720 | 1496 | parse_ctor_invocation_error (); |
22eed1e6 APB |
1497 | RECOVER; |
1498 | } | |
e04a16fb AG |
1499 | | this_or_super OP_TK argument_list error |
1500 | {yyerror ("')' expected"); RECOVER;} | |
1501 | | this_or_super OP_TK argument_list CP_TK error | |
22eed1e6 | 1502 | { |
8119c720 | 1503 | parse_ctor_invocation_error (); |
22eed1e6 APB |
1504 | RECOVER; |
1505 | } | |
e04a16fb AG |
1506 | | name DOT_TK SUPER_TK error |
1507 | {yyerror ("'(' expected"); RECOVER;} | |
1508 | | name DOT_TK SUPER_TK OP_TK error | |
1509 | {yyerror ("')' expected"); RECOVER;} | |
1510 | | name DOT_TK SUPER_TK OP_TK argument_list error | |
1511 | {yyerror ("')' expected"); RECOVER;} | |
1512 | | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error | |
1513 | {yyerror ("';' expected"); RECOVER;} | |
1514 | | name DOT_TK SUPER_TK OP_TK CP_TK error | |
1515 | {yyerror ("';' expected"); RECOVER;} | |
1516 | ; | |
1517 | ||
1518 | statement_expression: | |
1519 | assignment | |
1520 | | pre_increment_expression | |
e04a16fb | 1521 | | pre_decrement_expression |
e04a16fb | 1522 | | post_increment_expression |
e04a16fb | 1523 | | post_decrement_expression |
e04a16fb AG |
1524 | | method_invocation |
1525 | | class_instance_creation_expression | |
e04a16fb AG |
1526 | ; |
1527 | ||
1528 | if_then_statement: | |
1529 | IF_TK OP_TK expression CP_TK statement | |
2aa11e97 APB |
1530 | { |
1531 | $$ = build_if_else_statement ($2.location, $3, | |
1532 | $5, NULL_TREE); | |
1533 | } | |
e04a16fb AG |
1534 | | IF_TK error |
1535 | {yyerror ("'(' expected"); RECOVER;} | |
1536 | | IF_TK OP_TK error | |
1537 | {yyerror ("Missing term"); RECOVER;} | |
1538 | | IF_TK OP_TK expression error | |
1539 | {yyerror ("')' expected"); RECOVER;} | |
1540 | ; | |
1541 | ||
1542 | if_then_else_statement: | |
1543 | IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement | |
2aa11e97 | 1544 | { $$ = build_if_else_statement ($2.location, $3, $5, $7); } |
e04a16fb AG |
1545 | ; |
1546 | ||
1547 | if_then_else_statement_nsi: | |
1548 | IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi | |
2aa11e97 | 1549 | { $$ = build_if_else_statement ($2.location, $3, $5, $7); } |
e04a16fb AG |
1550 | ; |
1551 | ||
1552 | switch_statement: | |
15fdcfe9 PB |
1553 | switch_expression |
1554 | { | |
1555 | enter_block (); | |
1556 | } | |
1557 | switch_block | |
b67d701b | 1558 | { |
15fdcfe9 | 1559 | /* Make into "proper list" of COMPOUND_EXPRs. |
f8976021 APB |
1560 | I.e. make the last statment also have its own |
1561 | COMPOUND_EXPR. */ | |
15fdcfe9 PB |
1562 | maybe_absorb_scoping_blocks (); |
1563 | TREE_OPERAND ($1, 1) = exit_block (); | |
b67d701b PB |
1564 | $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1); |
1565 | } | |
1566 | ; | |
1567 | ||
1568 | switch_expression: | |
1569 | SWITCH_TK OP_TK expression CP_TK | |
1570 | { | |
1571 | $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE); | |
1572 | EXPR_WFL_LINECOL ($$) = $2.location; | |
1573 | } | |
e04a16fb AG |
1574 | | SWITCH_TK error |
1575 | {yyerror ("'(' expected"); RECOVER;} | |
1576 | | SWITCH_TK OP_TK error | |
1577 | {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);} | |
1578 | | SWITCH_TK OP_TK expression CP_TK error | |
1579 | {yyerror ("'{' expected"); RECOVER;} | |
1580 | ; | |
1581 | ||
f8976021 APB |
1582 | /* Default assignment is there to avoid type node on switch_block |
1583 | node. */ | |
1584 | ||
e04a16fb AG |
1585 | switch_block: |
1586 | OCB_TK CCB_TK | |
f8976021 | 1587 | { $$ = NULL_TREE; } |
e04a16fb | 1588 | | OCB_TK switch_labels CCB_TK |
f8976021 | 1589 | { $$ = NULL_TREE; } |
e04a16fb | 1590 | | OCB_TK switch_block_statement_groups CCB_TK |
f8976021 | 1591 | { $$ = NULL_TREE; } |
e04a16fb | 1592 | | OCB_TK switch_block_statement_groups switch_labels CCB_TK |
f8976021 | 1593 | { $$ = NULL_TREE; } |
e04a16fb AG |
1594 | ; |
1595 | ||
1596 | switch_block_statement_groups: | |
1597 | switch_block_statement_group | |
1598 | | switch_block_statement_groups switch_block_statement_group | |
1599 | ; | |
1600 | ||
1601 | switch_block_statement_group: | |
15fdcfe9 | 1602 | switch_labels block_statements |
e04a16fb AG |
1603 | ; |
1604 | ||
e04a16fb AG |
1605 | switch_labels: |
1606 | switch_label | |
1607 | | switch_labels switch_label | |
1608 | ; | |
1609 | ||
1610 | switch_label: | |
1611 | CASE_TK constant_expression REL_CL_TK | |
b67d701b | 1612 | { |
15fdcfe9 PB |
1613 | tree lab = build1 (CASE_EXPR, NULL_TREE, $2); |
1614 | EXPR_WFL_LINECOL (lab) = $1.location; | |
1615 | java_method_add_stmt (current_function_decl, lab); | |
b67d701b | 1616 | } |
e04a16fb | 1617 | | DEFAULT_TK REL_CL_TK |
b67d701b | 1618 | { |
15fdcfe9 PB |
1619 | tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE); |
1620 | EXPR_WFL_LINECOL (lab) = $1.location; | |
1621 | java_method_add_stmt (current_function_decl, lab); | |
b67d701b | 1622 | } |
e04a16fb AG |
1623 | | CASE_TK error |
1624 | {yyerror ("Missing or invalid constant expression"); RECOVER;} | |
1625 | | CASE_TK constant_expression error | |
1626 | {yyerror ("':' expected"); RECOVER;} | |
1627 | | DEFAULT_TK error | |
1628 | {yyerror ("':' expected"); RECOVER;} | |
1629 | ; | |
1630 | ||
1631 | while_expression: | |
1632 | WHILE_TK OP_TK expression CP_TK | |
1633 | { | |
1634 | tree body = build_loop_body ($2.location, $3, 0); | |
1635 | $$ = build_new_loop (body); | |
1636 | } | |
1637 | ; | |
1638 | ||
1639 | while_statement: | |
1640 | while_expression statement | |
b635eb2f | 1641 | { $$ = finish_loop_body (0, NULL_TREE, $2, 0); } |
e04a16fb AG |
1642 | | WHILE_TK error |
1643 | {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;} | |
1644 | | WHILE_TK OP_TK error | |
1645 | {yyerror ("Missing term and ')' expected"); RECOVER;} | |
1646 | | WHILE_TK OP_TK expression error | |
1647 | {yyerror ("')' expected"); RECOVER;} | |
1648 | ; | |
1649 | ||
1650 | while_statement_nsi: | |
1651 | while_expression statement_nsi | |
b635eb2f | 1652 | { $$ = finish_loop_body (0, NULL_TREE, $2, 0); } |
e04a16fb AG |
1653 | ; |
1654 | ||
1655 | do_statement_begin: | |
1656 | DO_TK | |
1657 | { | |
1658 | tree body = build_loop_body (0, NULL_TREE, 1); | |
1659 | $$ = build_new_loop (body); | |
1660 | } | |
1661 | /* Need error handing here. FIXME */ | |
1662 | ; | |
1663 | ||
1664 | do_statement: | |
1665 | do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK | |
b635eb2f | 1666 | { $$ = finish_loop_body ($4.location, $5, $2, 1); } |
e04a16fb AG |
1667 | ; |
1668 | ||
1669 | for_statement: | |
1670 | for_begin SC_TK expression SC_TK for_update CP_TK statement | |
774d2baf TT |
1671 | { |
1672 | if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c') | |
1673 | $3 = build_wfl_node ($3); | |
1674 | $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7); | |
1675 | } | |
e04a16fb AG |
1676 | | for_begin SC_TK SC_TK for_update CP_TK statement |
1677 | { | |
b635eb2f | 1678 | $$ = finish_for_loop (0, NULL_TREE, $4, $6); |
e04a16fb AG |
1679 | /* We have not condition, so we get rid of the EXIT_EXPR */ |
1680 | LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = | |
9bbc7d9f | 1681 | empty_stmt_node; |
e04a16fb AG |
1682 | } |
1683 | | for_begin SC_TK error | |
1684 | {yyerror ("Invalid control expression"); RECOVER;} | |
1685 | | for_begin SC_TK expression SC_TK error | |
1686 | {yyerror ("Invalid update expression"); RECOVER;} | |
1687 | | for_begin SC_TK SC_TK error | |
1688 | {yyerror ("Invalid update expression"); RECOVER;} | |
1689 | ; | |
1690 | ||
1691 | for_statement_nsi: | |
1692 | for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi | |
b635eb2f | 1693 | { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);} |
e04a16fb AG |
1694 | | for_begin SC_TK SC_TK for_update CP_TK statement_nsi |
1695 | { | |
b635eb2f | 1696 | $$ = finish_for_loop (0, NULL_TREE, $4, $6); |
e04a16fb AG |
1697 | /* We have not condition, so we get rid of the EXIT_EXPR */ |
1698 | LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = | |
9bbc7d9f | 1699 | empty_stmt_node; |
e04a16fb AG |
1700 | } |
1701 | ; | |
1702 | ||
1703 | for_header: | |
1704 | FOR_TK OP_TK | |
1705 | { | |
1706 | /* This scope defined for local variable that may be | |
1707 | defined within the scope of the for loop */ | |
1708 | enter_block (); | |
1709 | } | |
1710 | | FOR_TK error | |
1711 | {yyerror ("'(' expected"); DRECOVER(for_1);} | |
1712 | | FOR_TK OP_TK error | |
1713 | {yyerror ("Invalid init statement"); RECOVER;} | |
1714 | ; | |
1715 | ||
1716 | for_begin: | |
1717 | for_header for_init | |
1718 | { | |
1719 | /* We now declare the loop body. The loop is | |
1720 | declared as a for loop. */ | |
1721 | tree body = build_loop_body (0, NULL_TREE, 0); | |
1722 | $$ = build_new_loop (body); | |
c2952b01 | 1723 | FOR_LOOP_P ($$) = 1; |
e04a16fb AG |
1724 | /* The loop is added to the current block the for |
1725 | statement is defined within */ | |
1726 | java_method_add_stmt (current_function_decl, $$); | |
1727 | } | |
1728 | ; | |
1729 | for_init: /* Can be empty */ | |
9bbc7d9f | 1730 | { $$ = empty_stmt_node; } |
e04a16fb AG |
1731 | | statement_expression_list |
1732 | { | |
1733 | /* Init statement recorded within the previously | |
1734 | defined block scope */ | |
1735 | $$ = java_method_add_stmt (current_function_decl, $1); | |
1736 | } | |
1737 | | local_variable_declaration | |
1738 | { | |
1739 | /* Local variable are recorded within the previously | |
1740 | defined block scope */ | |
1741 | $$ = NULL_TREE; | |
1742 | } | |
1743 | | statement_expression_list error | |
1744 | {yyerror ("';' expected"); DRECOVER(for_init_1);} | |
1745 | ; | |
1746 | ||
1747 | for_update: /* Can be empty */ | |
9bbc7d9f | 1748 | {$$ = empty_stmt_node;} |
e04a16fb AG |
1749 | | statement_expression_list |
1750 | { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); } | |
1751 | ; | |
1752 | ||
1753 | statement_expression_list: | |
1754 | statement_expression | |
1755 | { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); } | |
1756 | | statement_expression_list C_TK statement_expression | |
1757 | { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); } | |
1758 | | statement_expression_list C_TK error | |
1759 | {yyerror ("Missing term"); RECOVER;} | |
1760 | ; | |
1761 | ||
1762 | break_statement: | |
1763 | BREAK_TK SC_TK | |
1764 | { $$ = build_bc_statement ($1.location, 1, NULL_TREE); } | |
1765 | | BREAK_TK identifier SC_TK | |
1766 | { $$ = build_bc_statement ($1.location, 1, $2); } | |
1767 | | BREAK_TK error | |
1768 | {yyerror ("Missing term"); RECOVER;} | |
1769 | | BREAK_TK identifier error | |
1770 | {yyerror ("';' expected"); RECOVER;} | |
1771 | ; | |
1772 | ||
1773 | continue_statement: | |
1774 | CONTINUE_TK SC_TK | |
1775 | { $$ = build_bc_statement ($1.location, 0, NULL_TREE); } | |
1776 | | CONTINUE_TK identifier SC_TK | |
1777 | { $$ = build_bc_statement ($1.location, 0, $2); } | |
1778 | | CONTINUE_TK error | |
1779 | {yyerror ("Missing term"); RECOVER;} | |
1780 | | CONTINUE_TK identifier error | |
1781 | {yyerror ("';' expected"); RECOVER;} | |
1782 | ; | |
1783 | ||
1784 | return_statement: | |
1785 | RETURN_TK SC_TK | |
1786 | { $$ = build_return ($1.location, NULL_TREE); } | |
1787 | | RETURN_TK expression SC_TK | |
1788 | { $$ = build_return ($1.location, $2); } | |
1789 | | RETURN_TK error | |
1790 | {yyerror ("Missing term"); RECOVER;} | |
1791 | | RETURN_TK expression error | |
1792 | {yyerror ("';' expected"); RECOVER;} | |
1793 | ; | |
1794 | ||
1795 | throw_statement: | |
1796 | THROW_TK expression SC_TK | |
b9f7e36c APB |
1797 | { |
1798 | $$ = build1 (THROW_EXPR, NULL_TREE, $2); | |
1799 | EXPR_WFL_LINECOL ($$) = $1.location; | |
1800 | } | |
e04a16fb AG |
1801 | | THROW_TK error |
1802 | {yyerror ("Missing term"); RECOVER;} | |
1803 | | THROW_TK expression error | |
1804 | {yyerror ("';' expected"); RECOVER;} | |
1805 | ; | |
1806 | ||
1807 | synchronized_statement: | |
1808 | synchronized OP_TK expression CP_TK block | |
b9f7e36c APB |
1809 | { |
1810 | $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5); | |
1811 | EXPR_WFL_LINECOL ($$) = | |
1812 | EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK)); | |
1813 | } | |
e04a16fb AG |
1814 | | synchronized OP_TK expression CP_TK error |
1815 | {yyerror ("'{' expected"); RECOVER;} | |
1816 | | synchronized error | |
1817 | {yyerror ("'(' expected"); RECOVER;} | |
1818 | | synchronized OP_TK error CP_TK | |
1819 | {yyerror ("Missing term"); RECOVER;} | |
1820 | | synchronized OP_TK error | |
1821 | {yyerror ("Missing term"); RECOVER;} | |
1822 | ; | |
1823 | ||
b9f7e36c | 1824 | synchronized: |
efa0a23f | 1825 | modifiers |
e04a16fb | 1826 | { |
781b0558 KG |
1827 | check_modifiers ( |
1828 | "Illegal modifier `%s'. Only `synchronized' was expected here", | |
efa0a23f APB |
1829 | $1, ACC_SYNCHRONIZED); |
1830 | if ($1 != ACC_SYNCHRONIZED) | |
1831 | MODIFIER_WFL (SYNCHRONIZED_TK) = | |
1832 | build_wfl_node (NULL_TREE); | |
e04a16fb AG |
1833 | } |
1834 | ; | |
1835 | ||
1836 | try_statement: | |
1837 | TRY_TK block catches | |
a7d8d81f | 1838 | { $$ = build_try_statement ($1.location, $2, $3); } |
e04a16fb | 1839 | | TRY_TK block finally |
a7d8d81f | 1840 | { $$ = build_try_finally_statement ($1.location, $2, $3); } |
e04a16fb | 1841 | | TRY_TK block catches finally |
2aa11e97 APB |
1842 | { $$ = build_try_finally_statement |
1843 | ($1.location, build_try_statement ($1.location, | |
1844 | $2, $3), $4); | |
1845 | } | |
e04a16fb AG |
1846 | | TRY_TK error |
1847 | {yyerror ("'{' expected"); DRECOVER (try_statement);} | |
1848 | ; | |
1849 | ||
1850 | catches: | |
1851 | catch_clause | |
1852 | | catches catch_clause | |
b67d701b PB |
1853 | { |
1854 | TREE_CHAIN ($2) = $1; | |
1855 | $$ = $2; | |
1856 | } | |
e04a16fb AG |
1857 | ; |
1858 | ||
1859 | catch_clause: | |
b67d701b PB |
1860 | catch_clause_parameter block |
1861 | { | |
1862 | java_method_add_stmt (current_function_decl, $2); | |
1863 | exit_block (); | |
1864 | $$ = $1; | |
1865 | } | |
1866 | ||
1867 | catch_clause_parameter: | |
1868 | CATCH_TK OP_TK formal_parameter CP_TK | |
1869 | { | |
1870 | /* We add a block to define a scope for | |
1871 | formal_parameter (CCBP). The formal parameter is | |
1872 | declared initialized by the appropriate function | |
1873 | call */ | |
1874 | tree ccpb = enter_block (); | |
1875 | tree init = build_assignment (ASSIGN_TK, $2.location, | |
1876 | TREE_PURPOSE ($3), | |
1877 | soft_exceptioninfo_call_node); | |
1878 | declare_local_variables (0, TREE_VALUE ($3), | |
1879 | build_tree_list (TREE_PURPOSE ($3), | |
1880 | init)); | |
1881 | $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb); | |
1882 | EXPR_WFL_LINECOL ($$) = $1.location; | |
1883 | } | |
e04a16fb | 1884 | | CATCH_TK error |
97f30284 | 1885 | {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;} |
e04a16fb | 1886 | | CATCH_TK OP_TK error |
97f30284 APB |
1887 | { |
1888 | yyerror ("Missing term or ')' expected"); | |
1889 | RECOVER; $$ = NULL_TREE; | |
1890 | } | |
b67d701b | 1891 | | CATCH_TK OP_TK error CP_TK /* That's for () */ |
97f30284 | 1892 | {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;} |
e04a16fb AG |
1893 | ; |
1894 | ||
1895 | finally: | |
1896 | FINALLY_TK block | |
a7d8d81f | 1897 | { $$ = $2; } |
e04a16fb AG |
1898 | | FINALLY_TK error |
1899 | {yyerror ("'{' expected"); RECOVER; } | |
1900 | ; | |
1901 | ||
1902 | /* 19.12 Production from 15: Expressions */ | |
1903 | primary: | |
1904 | primary_no_new_array | |
1905 | | array_creation_expression | |
1906 | ; | |
1907 | ||
1908 | primary_no_new_array: | |
1909 | literal | |
1910 | | THIS_TK | |
1911 | { $$ = build_this ($1.location); } | |
1912 | | OP_TK expression CP_TK | |
1913 | {$$ = $2;} | |
1914 | | class_instance_creation_expression | |
1915 | | field_access | |
1916 | | method_invocation | |
1917 | | array_access | |
c2952b01 | 1918 | | type_literals |
e04a16fb AG |
1919 | /* Added, JDK1.1 inner classes. Documentation is wrong |
1920 | refering to a 'ClassName' (class_name) rule that doesn't | |
c2952b01 | 1921 | exist. Used name: instead. */ |
e04a16fb | 1922 | | name DOT_TK THIS_TK |
c2952b01 APB |
1923 | { |
1924 | tree wfl = build_wfl_node (this_identifier_node); | |
1925 | $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1)); | |
1926 | } | |
e04a16fb AG |
1927 | | OP_TK expression error |
1928 | {yyerror ("')' expected"); RECOVER;} | |
1929 | | name DOT_TK error | |
1930 | {yyerror ("'class' or 'this' expected" ); RECOVER;} | |
1931 | | primitive_type DOT_TK error | |
1932 | {yyerror ("'class' expected" ); RECOVER;} | |
1933 | | VOID_TK DOT_TK error | |
1934 | {yyerror ("'class' expected" ); RECOVER;} | |
1935 | ; | |
1936 | ||
c2952b01 APB |
1937 | /* Added, JDK1.1 type literals. We can't use `type' directly, so we |
1938 | broke the rule down a bit. */ | |
1939 | ||
1940 | array_type_literal: | |
1941 | primitive_type OSB_TK CSB_TK | |
1942 | { | |
1943 | $$ = build_java_array_type ($1, -1); | |
1944 | CLASS_LOADED_P ($$) = 1; | |
1945 | } | |
1946 | | name OSB_TK CSB_TK | |
1947 | { $$ = build_unresolved_array_type ($1); } | |
1948 | /* This triggers two reduce/reduce conflict between array_type_literal and | |
1949 | dims. FIXME. | |
1950 | | array_type OSB_TK CSB_TK | |
1951 | { $$ = build_unresolved_array_type ($1); } | |
1952 | */ | |
1953 | ; | |
1954 | ||
1955 | type_literals: | |
1956 | name DOT_TK CLASS_TK | |
1957 | { $$ = build_incomplete_class_ref ($2.location, $1); } | |
1958 | | array_type_literal DOT_TK CLASS_TK | |
1959 | { $$ = build_incomplete_class_ref ($2.location, $1); } | |
1960 | | primitive_type DOT_TK CLASS_TK | |
1961 | { $$ = build_class_ref ($1); } | |
1962 | | VOID_TK DOT_TK CLASS_TK | |
1963 | { $$ = build_class_ref (void_type_node); } | |
1964 | ; | |
1965 | ||
e04a16fb AG |
1966 | class_instance_creation_expression: |
1967 | NEW_TK class_type OP_TK argument_list CP_TK | |
b67d701b | 1968 | { $$ = build_new_invocation ($2, $4); } |
e04a16fb | 1969 | | NEW_TK class_type OP_TK CP_TK |
b67d701b | 1970 | { $$ = build_new_invocation ($2, NULL_TREE); } |
c2952b01 | 1971 | | anonymous_class_creation |
e04a16fb AG |
1972 | /* Added, JDK1.1 inner classes, modified to use name or |
1973 | primary instead of primary solely which couldn't work in | |
1974 | all situations. */ | |
1975 | | something_dot_new identifier OP_TK CP_TK | |
c2952b01 APB |
1976 | { |
1977 | tree ctor = build_new_invocation ($2, NULL_TREE); | |
1978 | $$ = make_qualified_primary ($1, ctor, | |
1979 | EXPR_WFL_LINECOL ($1)); | |
1980 | } | |
e04a16fb AG |
1981 | | something_dot_new identifier OP_TK CP_TK class_body |
1982 | | something_dot_new identifier OP_TK argument_list CP_TK | |
c2952b01 APB |
1983 | { |
1984 | tree ctor = build_new_invocation ($2, $4); | |
1985 | $$ = make_qualified_primary ($1, ctor, | |
1986 | EXPR_WFL_LINECOL ($1)); | |
1987 | } | |
e04a16fb AG |
1988 | | something_dot_new identifier OP_TK argument_list CP_TK class_body |
1989 | | NEW_TK error SC_TK | |
1990 | {yyerror ("'(' expected"); DRECOVER(new_1);} | |
1991 | | NEW_TK class_type error | |
1992 | {yyerror ("'(' expected"); RECOVER;} | |
1993 | | NEW_TK class_type OP_TK error | |
1994 | {yyerror ("')' or term expected"); RECOVER;} | |
1995 | | NEW_TK class_type OP_TK argument_list error | |
1996 | {yyerror ("')' expected"); RECOVER;} | |
1997 | | something_dot_new error | |
1998 | {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;} | |
1999 | | something_dot_new identifier error | |
2000 | {yyerror ("'(' expected"); RECOVER;} | |
2001 | ; | |
2002 | ||
c2952b01 APB |
2003 | /* Created after JDK1.1 rules originally added to |
2004 | class_instance_creation_expression, but modified to use | |
2005 | 'class_type' instead of 'TypeName' (type_name) which is mentionned | |
2006 | in the documentation but doesn't exist. */ | |
2007 | ||
2008 | anonymous_class_creation: | |
2009 | NEW_TK class_type OP_TK argument_list CP_TK | |
2010 | { create_anonymous_class ($1.location, $2); } | |
2011 | class_body | |
2012 | { | |
2013 | tree id = build_wfl_node (DECL_NAME (GET_CPC ())); | |
2014 | EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2); | |
2015 | ||
2016 | end_class_declaration (1); | |
2017 | ||
2018 | /* Now we can craft the new expression */ | |
2019 | $$ = build_new_invocation (id, $4); | |
2020 | ||
2021 | /* Note that we can't possibly be here if | |
2022 | `class_type' is an interface (in which case the | |
2023 | anonymous class extends Object and implements | |
2024 | `class_type', hence its constructor can't have | |
2025 | arguments.) */ | |
2026 | ||
2027 | /* Otherwise, the innerclass must feature a | |
2028 | constructor matching `argument_list'. Anonymous | |
2029 | classes are a bit special: it's impossible to | |
2030 | define constructor for them, hence constructors | |
2031 | must be generated following the hints provided by | |
2032 | the `new' expression. Whether a super constructor | |
2033 | of that nature exists or not is to be verified | |
2034 | later on in verify_constructor_super. | |
2035 | ||
2036 | It's during the expansion of a `new' statement | |
2037 | refering to an anonymous class that a ctor will | |
2038 | be generated for the anonymous class, with the | |
2039 | right arguments. */ | |
2040 | ||
2041 | } | |
2042 | | NEW_TK class_type OP_TK CP_TK | |
2043 | { create_anonymous_class ($1.location, $2); } | |
2044 | class_body | |
2045 | { | |
2046 | tree id = build_wfl_node (DECL_NAME (GET_CPC ())); | |
2047 | EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2); | |
2048 | ||
2049 | end_class_declaration (1); | |
2050 | ||
2051 | /* Now we can craft the new expression. The | |
2052 | statement doesn't need to be remember so that a | |
2053 | constructor can be generated, since its signature | |
2054 | is already known. */ | |
2055 | $$ = build_new_invocation (id, NULL_TREE); | |
2056 | } | |
2057 | ; | |
2058 | ||
e04a16fb AG |
2059 | something_dot_new: /* Added, not part of the specs. */ |
2060 | name DOT_TK NEW_TK | |
c2952b01 | 2061 | { $$ = $1; } |
e04a16fb | 2062 | | primary DOT_TK NEW_TK |
c2952b01 | 2063 | { $$ = $1; } |
e04a16fb AG |
2064 | ; |
2065 | ||
2066 | argument_list: | |
2067 | expression | |
2068 | { | |
2069 | $$ = tree_cons (NULL_TREE, $1, NULL_TREE); | |
2070 | ctxp->formal_parameter_number = 1; | |
2071 | } | |
2072 | | argument_list C_TK expression | |
2073 | { | |
2074 | ctxp->formal_parameter_number += 1; | |
2075 | $$ = tree_cons (NULL_TREE, $3, $1); | |
2076 | } | |
2077 | | argument_list C_TK error | |
2078 | {yyerror ("Missing term"); RECOVER;} | |
2079 | ; | |
2080 | ||
2081 | array_creation_expression: | |
2082 | NEW_TK primitive_type dim_exprs | |
2083 | { $$ = build_newarray_node ($2, $3, 0); } | |
2084 | | NEW_TK class_or_interface_type dim_exprs | |
2085 | { $$ = build_newarray_node ($2, $3, 0); } | |
2086 | | NEW_TK primitive_type dim_exprs dims | |
ba179f9f | 2087 | { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));} |
e04a16fb | 2088 | | NEW_TK class_or_interface_type dim_exprs dims |
ba179f9f | 2089 | { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));} |
e04a16fb AG |
2090 | /* Added, JDK1.1 anonymous array. Initial documentation rule |
2091 | modified */ | |
2092 | | NEW_TK class_or_interface_type dims array_initializer | |
c2952b01 APB |
2093 | { |
2094 | char *sig; | |
2095 | while (CURRENT_OSB (ctxp)--) | |
2096 | obstack_1grow (&temporary_obstack, '['); | |
2097 | sig = obstack_finish (&temporary_obstack); | |
2098 | $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE, | |
2099 | $2, get_identifier (sig), $4); | |
2100 | } | |
e04a16fb | 2101 | | NEW_TK primitive_type dims array_initializer |
c2952b01 APB |
2102 | { |
2103 | tree type = $2; | |
2104 | while (CURRENT_OSB (ctxp)--) | |
2105 | type = build_java_array_type (type, -1); | |
2106 | $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE, | |
2107 | build_pointer_type (type), NULL_TREE, $4); | |
2108 | } | |
e04a16fb AG |
2109 | | NEW_TK error CSB_TK |
2110 | {yyerror ("'[' expected"); DRECOVER ("]");} | |
2111 | | NEW_TK error OSB_TK | |
2112 | {yyerror ("']' expected"); RECOVER;} | |
2113 | ; | |
2114 | ||
2115 | dim_exprs: | |
2116 | dim_expr | |
2117 | { $$ = build_tree_list (NULL_TREE, $1); } | |
2118 | | dim_exprs dim_expr | |
2119 | { $$ = tree_cons (NULL_TREE, $2, $$); } | |
2120 | ; | |
2121 | ||
2122 | dim_expr: | |
2123 | OSB_TK expression CSB_TK | |
2124 | { | |
9a7ab4b3 APB |
2125 | if (JNUMERIC_TYPE_P (TREE_TYPE ($2))) |
2126 | { | |
2127 | $2 = build_wfl_node ($2); | |
2128 | TREE_TYPE ($2) = NULL_TREE; | |
2129 | } | |
e04a16fb AG |
2130 | EXPR_WFL_LINECOL ($2) = $1.location; |
2131 | $$ = $2; | |
2132 | } | |
2133 | | OSB_TK expression error | |
2134 | {yyerror ("']' expected"); RECOVER;} | |
2135 | | OSB_TK error | |
2136 | { | |
2137 | yyerror ("Missing term"); | |
2138 | yyerror ("']' expected"); | |
2139 | RECOVER; | |
2140 | } | |
2141 | ; | |
2142 | ||
2143 | dims: | |
2144 | OSB_TK CSB_TK | |
ba179f9f APB |
2145 | { |
2146 | int allocate = 0; | |
2147 | /* If not initialized, allocate memory for the osb | |
2148 | numbers stack */ | |
2149 | if (!ctxp->osb_limit) | |
2150 | { | |
2151 | allocate = ctxp->osb_limit = 32; | |
2152 | ctxp->osb_depth = -1; | |
2153 | } | |
c2952b01 | 2154 | /* If capacity overflown, reallocate a bigger chunk */ |
ba179f9f APB |
2155 | else if (ctxp->osb_depth+1 == ctxp->osb_limit) |
2156 | allocate = ctxp->osb_limit << 1; | |
2157 | ||
2158 | if (allocate) | |
2159 | { | |
2160 | allocate *= sizeof (int); | |
2161 | if (ctxp->osb_number) | |
2162 | ctxp->osb_number = (int *)xrealloc (ctxp->osb_number, | |
2163 | allocate); | |
2164 | else | |
2165 | ctxp->osb_number = (int *)xmalloc (allocate); | |
2166 | } | |
2167 | ctxp->osb_depth++; | |
2168 | CURRENT_OSB (ctxp) = 1; | |
2169 | } | |
e04a16fb | 2170 | | dims OSB_TK CSB_TK |
ba179f9f | 2171 | { CURRENT_OSB (ctxp)++; } |
e04a16fb AG |
2172 | | dims OSB_TK error |
2173 | { yyerror ("']' expected"); RECOVER;} | |
2174 | ; | |
2175 | ||
2176 | field_access: | |
2177 | primary DOT_TK identifier | |
2178 | { $$ = make_qualified_primary ($1, $3, $2.location); } | |
9bbc7d9f PB |
2179 | /* FIXME - REWRITE TO: |
2180 | { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */ | |
e04a16fb AG |
2181 | | SUPER_TK DOT_TK identifier |
2182 | { | |
6e22695a | 2183 | tree super_wfl = build_wfl_node (super_identifier_node); |
e04a16fb AG |
2184 | EXPR_WFL_LINECOL (super_wfl) = $1.location; |
2185 | $$ = make_qualified_name (super_wfl, $3, $2.location); | |
2186 | } | |
2187 | | SUPER_TK error | |
2188 | {yyerror ("Field expected"); DRECOVER (super_field_acces);} | |
2189 | ; | |
2190 | ||
2191 | method_invocation: | |
2192 | name OP_TK CP_TK | |
2193 | { $$ = build_method_invocation ($1, NULL_TREE); } | |
2194 | | name OP_TK argument_list CP_TK | |
2195 | { $$ = build_method_invocation ($1, $3); } | |
2196 | | primary DOT_TK identifier OP_TK CP_TK | |
2197 | { | |
22eed1e6 APB |
2198 | if (TREE_CODE ($1) == THIS_EXPR) |
2199 | $$ = build_this_super_qualified_invocation | |
2200 | (1, $3, NULL_TREE, 0, $2.location); | |
2201 | else | |
2202 | { | |
2203 | tree invok = build_method_invocation ($3, NULL_TREE); | |
2204 | $$ = make_qualified_primary ($1, invok, $2.location); | |
2205 | } | |
e04a16fb AG |
2206 | } |
2207 | | primary DOT_TK identifier OP_TK argument_list CP_TK | |
2208 | { | |
22eed1e6 APB |
2209 | if (TREE_CODE ($1) == THIS_EXPR) |
2210 | $$ = build_this_super_qualified_invocation | |
2211 | (1, $3, $5, 0, $2.location); | |
2212 | else | |
2213 | { | |
2214 | tree invok = build_method_invocation ($3, $5); | |
2215 | $$ = make_qualified_primary ($1, invok, $2.location); | |
2216 | } | |
e04a16fb AG |
2217 | } |
2218 | | SUPER_TK DOT_TK identifier OP_TK CP_TK | |
22eed1e6 APB |
2219 | { |
2220 | $$ = build_this_super_qualified_invocation | |
2221 | (0, $3, NULL_TREE, $1.location, $2.location); | |
e04a16fb AG |
2222 | } |
2223 | | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK | |
2224 | { | |
22eed1e6 APB |
2225 | $$ = build_this_super_qualified_invocation |
2226 | (0, $3, $5, $1.location, $2.location); | |
e04a16fb AG |
2227 | } |
2228 | /* Screws up thing. I let it here until I'm convinced it can | |
2229 | be removed. FIXME | |
2230 | | primary DOT_TK error | |
2231 | {yyerror ("'(' expected"); DRECOVER(bad);} */ | |
2232 | | SUPER_TK DOT_TK error CP_TK | |
2233 | { yyerror ("'(' expected"); DRECOVER (method_invocation); } | |
2234 | | SUPER_TK DOT_TK error DOT_TK | |
2235 | { yyerror ("'(' expected"); DRECOVER (method_invocation); } | |
2236 | ; | |
2237 | ||
2238 | array_access: | |
2239 | name OSB_TK expression CSB_TK | |
2240 | { $$ = build_array_ref ($2.location, $1, $3); } | |
2241 | | primary_no_new_array OSB_TK expression CSB_TK | |
2242 | { $$ = build_array_ref ($2.location, $1, $3); } | |
2243 | | name OSB_TK error | |
2244 | { | |
2245 | yyerror ("Missing term and ']' expected"); | |
2246 | DRECOVER(array_access); | |
2247 | } | |
2248 | | name OSB_TK expression error | |
2249 | { | |
2250 | yyerror ("']' expected"); | |
2251 | DRECOVER(array_access); | |
2252 | } | |
2253 | | primary_no_new_array OSB_TK error | |
2254 | { | |
2255 | yyerror ("Missing term and ']' expected"); | |
2256 | DRECOVER(array_access); | |
2257 | } | |
2258 | | primary_no_new_array OSB_TK expression error | |
2259 | { | |
2260 | yyerror ("']' expected"); | |
2261 | DRECOVER(array_access); | |
2262 | } | |
2263 | ; | |
2264 | ||
2265 | postfix_expression: | |
2266 | primary | |
2267 | | name | |
2268 | | post_increment_expression | |
2269 | | post_decrement_expression | |
2270 | ; | |
2271 | ||
2272 | post_increment_expression: | |
2273 | postfix_expression INCR_TK | |
2274 | { $$ = build_incdec ($2.token, $2.location, $1, 1); } | |
2275 | ; | |
2276 | ||
2277 | post_decrement_expression: | |
2278 | postfix_expression DECR_TK | |
2279 | { $$ = build_incdec ($2.token, $2.location, $1, 1); } | |
2280 | ; | |
2281 | ||
2282 | unary_expression: | |
2283 | pre_increment_expression | |
2284 | | pre_decrement_expression | |
2285 | | PLUS_TK unary_expression | |
2286 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2287 | | MINUS_TK unary_expression | |
2288 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2289 | | unary_expression_not_plus_minus | |
2290 | | PLUS_TK error | |
2291 | {yyerror ("Missing term"); RECOVER} | |
2292 | | MINUS_TK error | |
2293 | {yyerror ("Missing term"); RECOVER} | |
2294 | ; | |
2295 | ||
2296 | pre_increment_expression: | |
2297 | INCR_TK unary_expression | |
2298 | {$$ = build_incdec ($1.token, $1.location, $2, 0); } | |
2299 | | INCR_TK error | |
2300 | {yyerror ("Missing term"); RECOVER} | |
2301 | ; | |
2302 | ||
2303 | pre_decrement_expression: | |
2304 | DECR_TK unary_expression | |
2305 | {$$ = build_incdec ($1.token, $1.location, $2, 0); } | |
2306 | | DECR_TK error | |
2307 | {yyerror ("Missing term"); RECOVER} | |
2308 | ; | |
2309 | ||
2310 | unary_expression_not_plus_minus: | |
2311 | postfix_expression | |
2312 | | NOT_TK unary_expression | |
2313 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2314 | | NEG_TK unary_expression | |
2315 | {$$ = build_unaryop ($1.token, $1.location, $2); } | |
2316 | | cast_expression | |
2317 | | NOT_TK error | |
2318 | {yyerror ("Missing term"); RECOVER} | |
2319 | | NEG_TK error | |
2320 | {yyerror ("Missing term"); RECOVER} | |
2321 | ; | |
2322 | ||
2323 | cast_expression: /* Error handling here is potentially weak */ | |
2324 | OP_TK primitive_type dims CP_TK unary_expression | |
2325 | { | |
2326 | tree type = $2; | |
ba179f9f | 2327 | while (CURRENT_OSB (ctxp)--) |
e04a16fb | 2328 | type = build_java_array_type (type, -1); |
ba179f9f | 2329 | ctxp->osb_depth--; |
e04a16fb AG |
2330 | $$ = build_cast ($1.location, type, $5); |
2331 | } | |
2332 | | OP_TK primitive_type CP_TK unary_expression | |
2333 | { $$ = build_cast ($1.location, $2, $4); } | |
2334 | | OP_TK expression CP_TK unary_expression_not_plus_minus | |
2335 | { $$ = build_cast ($1.location, $2, $4); } | |
2336 | | OP_TK name dims CP_TK unary_expression_not_plus_minus | |
2337 | { | |
49f48c71 | 2338 | const char *ptr; |
ba179f9f | 2339 | while (CURRENT_OSB (ctxp)--) |
e04a16fb | 2340 | obstack_1grow (&temporary_obstack, '['); |
ba179f9f | 2341 | ctxp->osb_depth--; |
e04a16fb AG |
2342 | obstack_grow0 (&temporary_obstack, |
2343 | IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)), | |
2344 | IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2))); | |
2345 | ptr = obstack_finish (&temporary_obstack); | |
2346 | EXPR_WFL_NODE ($2) = get_identifier (ptr); | |
2347 | $$ = build_cast ($1.location, $2, $5); | |
2348 | } | |
2349 | | OP_TK primitive_type OSB_TK error | |
2350 | {yyerror ("']' expected, invalid type expression");} | |
2351 | | OP_TK error | |
2352 | { | |
29f8b718 | 2353 | YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER; |
e04a16fb AG |
2354 | RECOVER; |
2355 | } | |
2356 | | OP_TK primitive_type dims CP_TK error | |
2357 | {yyerror ("Missing term"); RECOVER;} | |
2358 | | OP_TK primitive_type CP_TK error | |
2359 | {yyerror ("Missing term"); RECOVER;} | |
2360 | | OP_TK name dims CP_TK error | |
2361 | {yyerror ("Missing term"); RECOVER;} | |
2362 | ; | |
2363 | ||
2364 | multiplicative_expression: | |
2365 | unary_expression | |
2366 | | multiplicative_expression MULT_TK unary_expression | |
2367 | { | |
2368 | $$ = build_binop (BINOP_LOOKUP ($2.token), | |
2369 | $2.location, $1, $3); | |
2370 | } | |
2371 | | multiplicative_expression DIV_TK unary_expression | |
2372 | { | |
2373 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2374 | $1, $3); | |
2375 | } | |
2376 | | multiplicative_expression REM_TK unary_expression | |
2377 | { | |
2378 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2379 | $1, $3); | |
2380 | } | |
2381 | | multiplicative_expression MULT_TK error | |
2382 | {yyerror ("Missing term"); RECOVER;} | |
2383 | | multiplicative_expression DIV_TK error | |
2384 | {yyerror ("Missing term"); RECOVER;} | |
2385 | | multiplicative_expression REM_TK error | |
2386 | {yyerror ("Missing term"); RECOVER;} | |
2387 | ; | |
2388 | ||
2389 | additive_expression: | |
2390 | multiplicative_expression | |
2391 | | additive_expression PLUS_TK multiplicative_expression | |
2392 | { | |
2393 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2394 | $1, $3); | |
2395 | } | |
2396 | | additive_expression MINUS_TK multiplicative_expression | |
2397 | { | |
2398 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2399 | $1, $3); | |
2400 | } | |
2401 | | additive_expression PLUS_TK error | |
2402 | {yyerror ("Missing term"); RECOVER;} | |
2403 | | additive_expression MINUS_TK error | |
2404 | {yyerror ("Missing term"); RECOVER;} | |
2405 | ; | |
2406 | ||
2407 | shift_expression: | |
2408 | additive_expression | |
2409 | | shift_expression LS_TK additive_expression | |
2410 | { | |
2411 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2412 | $1, $3); | |
2413 | } | |
2414 | | shift_expression SRS_TK additive_expression | |
2415 | { | |
2416 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2417 | $1, $3); | |
2418 | } | |
2419 | | shift_expression ZRS_TK additive_expression | |
2420 | { | |
2421 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2422 | $1, $3); | |
2423 | } | |
2424 | | shift_expression LS_TK error | |
2425 | {yyerror ("Missing term"); RECOVER;} | |
2426 | | shift_expression SRS_TK error | |
2427 | {yyerror ("Missing term"); RECOVER;} | |
2428 | | shift_expression ZRS_TK error | |
2429 | {yyerror ("Missing term"); RECOVER;} | |
2430 | ; | |
2431 | ||
2432 | relational_expression: | |
2433 | shift_expression | |
2434 | | relational_expression LT_TK shift_expression | |
2435 | { | |
2436 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2437 | $1, $3); | |
2438 | } | |
2439 | | relational_expression GT_TK shift_expression | |
2440 | { | |
2441 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2442 | $1, $3); | |
2443 | } | |
2444 | | relational_expression LTE_TK shift_expression | |
2445 | { | |
2446 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2447 | $1, $3); | |
2448 | } | |
2449 | | relational_expression GTE_TK shift_expression | |
2450 | { | |
2451 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2452 | $1, $3); | |
2453 | } | |
2454 | | relational_expression INSTANCEOF_TK reference_type | |
5e942c50 | 2455 | { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); } |
e04a16fb AG |
2456 | | relational_expression LT_TK error |
2457 | {yyerror ("Missing term"); RECOVER;} | |
2458 | | relational_expression GT_TK error | |
2459 | {yyerror ("Missing term"); RECOVER;} | |
2460 | | relational_expression LTE_TK error | |
2461 | {yyerror ("Missing term"); RECOVER;} | |
2462 | | relational_expression GTE_TK error | |
2463 | {yyerror ("Missing term"); RECOVER;} | |
2464 | | relational_expression INSTANCEOF_TK error | |
2465 | {yyerror ("Invalid reference type"); RECOVER;} | |
2466 | ; | |
2467 | ||
2468 | equality_expression: | |
2469 | relational_expression | |
2470 | | equality_expression EQ_TK relational_expression | |
2471 | { | |
2472 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2473 | $1, $3); | |
2474 | } | |
2475 | | equality_expression NEQ_TK relational_expression | |
2476 | { | |
2477 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2478 | $1, $3); | |
2479 | } | |
2480 | | equality_expression EQ_TK error | |
2481 | {yyerror ("Missing term"); RECOVER;} | |
2482 | | equality_expression NEQ_TK error | |
2483 | {yyerror ("Missing term"); RECOVER;} | |
2484 | ; | |
2485 | ||
2486 | and_expression: | |
2487 | equality_expression | |
2488 | | and_expression AND_TK equality_expression | |
2489 | { | |
2490 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2491 | $1, $3); | |
2492 | } | |
2493 | | and_expression AND_TK error | |
2494 | {yyerror ("Missing term"); RECOVER;} | |
2495 | ; | |
2496 | ||
2497 | exclusive_or_expression: | |
2498 | and_expression | |
2499 | | exclusive_or_expression XOR_TK and_expression | |
2500 | { | |
2501 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2502 | $1, $3); | |
2503 | } | |
2504 | | exclusive_or_expression XOR_TK error | |
2505 | {yyerror ("Missing term"); RECOVER;} | |
2506 | ; | |
2507 | ||
2508 | inclusive_or_expression: | |
2509 | exclusive_or_expression | |
2510 | | inclusive_or_expression OR_TK exclusive_or_expression | |
2511 | { | |
2512 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2513 | $1, $3); | |
2514 | } | |
2515 | | inclusive_or_expression OR_TK error | |
2516 | {yyerror ("Missing term"); RECOVER;} | |
2517 | ; | |
2518 | ||
2519 | conditional_and_expression: | |
2520 | inclusive_or_expression | |
2521 | | conditional_and_expression BOOL_AND_TK inclusive_or_expression | |
2522 | { | |
2523 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2524 | $1, $3); | |
2525 | } | |
2526 | | conditional_and_expression BOOL_AND_TK error | |
2527 | {yyerror ("Missing term"); RECOVER;} | |
2528 | ; | |
2529 | ||
2530 | conditional_or_expression: | |
2531 | conditional_and_expression | |
2532 | | conditional_or_expression BOOL_OR_TK conditional_and_expression | |
2533 | { | |
2534 | $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location, | |
2535 | $1, $3); | |
2536 | } | |
2537 | | conditional_or_expression BOOL_OR_TK error | |
2538 | {yyerror ("Missing term"); RECOVER;} | |
2539 | ; | |
2540 | ||
2541 | conditional_expression: /* Error handling here is weak */ | |
2542 | conditional_or_expression | |
2543 | | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression | |
22eed1e6 APB |
2544 | { |
2545 | $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5); | |
2546 | EXPR_WFL_LINECOL ($$) = $2.location; | |
2547 | } | |
e04a16fb AG |
2548 | | conditional_or_expression REL_QM_TK REL_CL_TK error |
2549 | { | |
2550 | YYERROR_NOW; | |
2551 | yyerror ("Missing term"); | |
2552 | DRECOVER (1); | |
2553 | } | |
2554 | | conditional_or_expression REL_QM_TK error | |
2555 | {yyerror ("Missing term"); DRECOVER (2);} | |
2556 | | conditional_or_expression REL_QM_TK expression REL_CL_TK error | |
2557 | {yyerror ("Missing term"); DRECOVER (3);} | |
2558 | ; | |
2559 | ||
2560 | assignment_expression: | |
2561 | conditional_expression | |
2562 | | assignment | |
2563 | ; | |
2564 | ||
2565 | assignment: | |
2566 | left_hand_side assignment_operator assignment_expression | |
2567 | { $$ = build_assignment ($2.token, $2.location, $1, $3); } | |
2568 | | left_hand_side assignment_operator error | |
2569 | { | |
29f8b718 | 2570 | YYNOT_TWICE yyerror ("Missing term"); |
e04a16fb AG |
2571 | DRECOVER (assign); |
2572 | } | |
2573 | ; | |
2574 | ||
2575 | left_hand_side: | |
2576 | name | |
2577 | | field_access | |
2578 | | array_access | |
2579 | ; | |
2580 | ||
2581 | assignment_operator: | |
2582 | ASSIGN_ANY_TK | |
2583 | | ASSIGN_TK | |
2584 | ; | |
2585 | ||
2586 | expression: | |
2587 | assignment_expression | |
2588 | ; | |
2589 | ||
2590 | constant_expression: | |
2591 | expression | |
2592 | ; | |
2593 | ||
2594 | %% | |
2595 | \f | |
2596 | ||
c2952b01 APB |
2597 | /* This section of the code deal with save/restoring parser contexts. |
2598 | Add mode documentation here. FIXME */ | |
e04a16fb | 2599 | |
c2952b01 APB |
2600 | /* Helper function. Create a new parser context. With |
2601 | COPY_FROM_PREVIOUS set to a non zero value, content of the previous | |
2602 | context is copied, otherwise, the new context is zeroed. The newly | |
2603 | created context becomes the current one. */ | |
e04a16fb | 2604 | |
c2952b01 APB |
2605 | static void |
2606 | create_new_parser_context (copy_from_previous) | |
2607 | int copy_from_previous; | |
e04a16fb | 2608 | { |
c2952b01 | 2609 | struct parser_ctxt *new; |
e04a16fb | 2610 | |
c2952b01 APB |
2611 | new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt)); |
2612 | if (copy_from_previous) | |
2613 | { | |
2614 | memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt)); | |
2615 | new->saved_data_ctx = 1; | |
2616 | } | |
2617 | else | |
2618 | bzero ((PTR) new, sizeof (struct parser_ctxt)); | |
2619 | ||
e04a16fb AG |
2620 | new->next = ctxp; |
2621 | ctxp = new; | |
c2952b01 APB |
2622 | } |
2623 | ||
2624 | /* Create a new parser context and make it the current one. */ | |
2625 | ||
2626 | void | |
2627 | java_push_parser_context () | |
2628 | { | |
2629 | create_new_parser_context (0); | |
e04a16fb | 2630 | if (ctxp->next) |
5e942c50 APB |
2631 | { |
2632 | ctxp->incomplete_class = ctxp->next->incomplete_class; | |
2633 | ctxp->gclass_list = ctxp->next->gclass_list; | |
2634 | } | |
e04a16fb AG |
2635 | } |
2636 | ||
c2952b01 APB |
2637 | void |
2638 | java_pop_parser_context (generate) | |
2639 | int generate; | |
2640 | { | |
2641 | tree current; | |
2642 | struct parser_ctxt *toFree, *next; | |
2643 | ||
2644 | if (!ctxp) | |
2645 | return; | |
2646 | ||
2647 | toFree = ctxp; | |
2648 | next = ctxp->next; | |
2649 | if (next) | |
2650 | { | |
2651 | next->incomplete_class = ctxp->incomplete_class; | |
2652 | next->gclass_list = ctxp->gclass_list; | |
2653 | lineno = ctxp->lineno; | |
19e223db | 2654 | current_class = ctxp->class_type; |
c2952b01 APB |
2655 | } |
2656 | ||
d19cbcb5 TT |
2657 | /* If the old and new lexers differ, then free the old one. */ |
2658 | if (ctxp->lexer && next && ctxp->lexer != next->lexer) | |
2659 | java_destroy_lexer (ctxp->lexer); | |
2660 | ||
c2952b01 APB |
2661 | /* Set the single import class file flag to 0 for the current list |
2662 | of imported things */ | |
2663 | for (current = ctxp->import_list; current; current = TREE_CHAIN (current)) | |
2664 | IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0; | |
2665 | ||
2666 | /* And restore those of the previous context */ | |
2667 | if ((ctxp = next)) /* Assignment is really meant here */ | |
2668 | for (current = ctxp->import_list; current; current = TREE_CHAIN (current)) | |
2669 | IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1; | |
2670 | ||
2671 | /* If we pushed a context to parse a class intended to be generated, | |
2672 | we keep it so we can remember the class. What we could actually | |
2673 | do is to just update a list of class names. */ | |
2674 | if (generate) | |
2675 | { | |
2676 | toFree->next = ctxp_for_generation; | |
2677 | ctxp_for_generation = toFree; | |
2678 | } | |
2679 | else | |
2680 | free (toFree); | |
2681 | } | |
2682 | ||
2683 | /* Create a parser context for the use of saving some global | |
2684 | variables. */ | |
2685 | ||
e04a16fb AG |
2686 | void |
2687 | java_parser_context_save_global () | |
2688 | { | |
22eed1e6 APB |
2689 | if (!ctxp) |
2690 | { | |
2691 | java_push_parser_context (); | |
ee07f4f4 APB |
2692 | ctxp->saved_data_ctx = 1; |
2693 | } | |
c2952b01 APB |
2694 | |
2695 | /* If this context already stores data, create a new one suitable | |
2696 | for data storage. */ | |
ee07f4f4 | 2697 | else if (ctxp->saved_data) |
c2952b01 APB |
2698 | create_new_parser_context (1); |
2699 | ||
e04a16fb | 2700 | ctxp->lineno = lineno; |
19e223db | 2701 | ctxp->class_type = current_class; |
e04a16fb | 2702 | ctxp->filename = input_filename; |
19e223db | 2703 | ctxp->function_decl = current_function_decl; |
ee07f4f4 | 2704 | ctxp->saved_data = 1; |
e04a16fb AG |
2705 | } |
2706 | ||
c2952b01 APB |
2707 | /* Restore some global variables from the previous context. Make the |
2708 | previous context the current one. */ | |
2709 | ||
e04a16fb AG |
2710 | void |
2711 | java_parser_context_restore_global () | |
2712 | { | |
e04a16fb | 2713 | lineno = ctxp->lineno; |
19e223db | 2714 | current_class = ctxp->class_type; |
e04a16fb | 2715 | input_filename = ctxp->filename; |
dba41d30 APB |
2716 | if (wfl_operator) |
2717 | { | |
2718 | tree s; | |
2719 | BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename); | |
2720 | EXPR_WFL_FILENAME_NODE (wfl_operator) = s; | |
2721 | } | |
19e223db | 2722 | current_function_decl = ctxp->function_decl; |
c2952b01 | 2723 | ctxp->saved_data = 0; |
ee07f4f4 APB |
2724 | if (ctxp->saved_data_ctx) |
2725 | java_pop_parser_context (0); | |
e04a16fb AG |
2726 | } |
2727 | ||
c2952b01 APB |
2728 | /* Suspend vital data for the current class/function being parsed so |
2729 | that an other class can be parsed. Used to let local/anonymous | |
2730 | classes be parsed. */ | |
2731 | ||
2732 | static void | |
2733 | java_parser_context_suspend () | |
e04a16fb | 2734 | { |
c2952b01 | 2735 | /* This makes debugging through java_debug_context easier */ |
3b304f5b | 2736 | static const char *name = "<inner buffer context>"; |
e04a16fb | 2737 | |
c2952b01 APB |
2738 | /* Duplicate the previous context, use it to save the globals we're |
2739 | interested in */ | |
2740 | create_new_parser_context (1); | |
19e223db MM |
2741 | ctxp->function_decl = current_function_decl; |
2742 | ctxp->class_type = current_class; | |
5e942c50 | 2743 | |
c2952b01 APB |
2744 | /* Then create a new context which inherits all data from the |
2745 | previous one. This will be the new current context */ | |
2746 | create_new_parser_context (1); | |
2747 | ||
2748 | /* Help debugging */ | |
2749 | ctxp->next->filename = name; | |
2750 | } | |
2751 | ||
2752 | /* Resume vital data for the current class/function being parsed so | |
2753 | that an other class can be parsed. Used to let local/anonymous | |
2754 | classes be parsed. The trick is the data storing file position | |
2755 | informations must be restored to their current value, so parsing | |
2756 | can resume as if no context was ever saved. */ | |
2757 | ||
2758 | static void | |
2759 | java_parser_context_resume () | |
2760 | { | |
2761 | struct parser_ctxt *old = ctxp; /* This one is to be discarded */ | |
2762 | struct parser_ctxt *saver = old->next; /* This one contain saved info */ | |
2763 | struct parser_ctxt *restored = saver->next; /* This one is the old current */ | |
2764 | ||
2765 | /* We need to inherit the list of classes to complete/generate */ | |
2766 | restored->incomplete_class = old->incomplete_class; | |
2767 | restored->gclass_list = old->gclass_list; | |
2768 | restored->classd_list = old->classd_list; | |
2769 | restored->class_list = old->class_list; | |
2770 | ||
2771 | /* Restore the current class and function from the saver */ | |
19e223db MM |
2772 | current_class = saver->class_type; |
2773 | current_function_decl = saver->function_decl; | |
c2952b01 APB |
2774 | |
2775 | /* Retrive the restored context */ | |
2776 | ctxp = restored; | |
2777 | ||
2778 | /* Re-installed the data for the parsing to carry on */ | |
2779 | bcopy (&old->marker_begining, &ctxp->marker_begining, | |
2780 | (size_t)(&ctxp->marker_end - &ctxp->marker_begining)); | |
2781 | ||
2782 | /* Buffer context can now be discarded */ | |
2783 | free (saver); | |
2784 | free (old); | |
2785 | } | |
2786 | ||
2787 | /* Add a new anchor node to which all statement(s) initializing static | |
2788 | and non static initialized upon declaration field(s) will be | |
2789 | linked. */ | |
2790 | ||
2791 | static void | |
2792 | java_parser_context_push_initialized_field () | |
2793 | { | |
2794 | tree node; | |
2795 | ||
2796 | node = build_tree_list (NULL_TREE, NULL_TREE); | |
2797 | TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp); | |
2798 | CPC_STATIC_INITIALIZER_LIST (ctxp) = node; | |
2799 | ||
2800 | node = build_tree_list (NULL_TREE, NULL_TREE); | |
2801 | TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp); | |
2802 | CPC_INITIALIZER_LIST (ctxp) = node; | |
2803 | ||
2804 | node = build_tree_list (NULL_TREE, NULL_TREE); | |
2805 | TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp); | |
2806 | CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node; | |
2807 | } | |
2808 | ||
2809 | /* Pop the lists of initialized field. If this lists aren't empty, | |
c00f0fb2 | 2810 | remember them so we can use it to create and populate the finit$ |
c2952b01 APB |
2811 | or <clinit> functions. */ |
2812 | ||
2813 | static void | |
2814 | java_parser_context_pop_initialized_field () | |
2815 | { | |
2816 | tree stmts; | |
2817 | tree class_type = TREE_TYPE (GET_CPC ()); | |
2818 | ||
2819 | if (CPC_INITIALIZER_LIST (ctxp)) | |
e04a16fb | 2820 | { |
c2952b01 APB |
2821 | stmts = CPC_INITIALIZER_STMT (ctxp); |
2822 | CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp)); | |
2823 | if (stmts && !java_error_count) | |
2824 | TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts); | |
e04a16fb AG |
2825 | } |
2826 | ||
c2952b01 APB |
2827 | if (CPC_STATIC_INITIALIZER_LIST (ctxp)) |
2828 | { | |
2829 | stmts = CPC_STATIC_INITIALIZER_STMT (ctxp); | |
2830 | CPC_STATIC_INITIALIZER_LIST (ctxp) = | |
2831 | TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp)); | |
2832 | /* Keep initialization in order to enforce 8.5 */ | |
2833 | if (stmts && !java_error_count) | |
2834 | TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts); | |
2835 | } | |
e04a16fb | 2836 | |
c2952b01 APB |
2837 | /* JDK 1.1 instance initializers */ |
2838 | if (CPC_INSTANCE_INITIALIZER_LIST (ctxp)) | |
b351b287 | 2839 | { |
c2952b01 APB |
2840 | stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp); |
2841 | CPC_INSTANCE_INITIALIZER_LIST (ctxp) = | |
2842 | TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp)); | |
2843 | if (stmts && !java_error_count) | |
2844 | TYPE_II_STMT_LIST (class_type) = nreverse (stmts); | |
b351b287 | 2845 | } |
c2952b01 APB |
2846 | } |
2847 | ||
2848 | static tree | |
2849 | reorder_static_initialized (list) | |
2850 | tree list; | |
2851 | { | |
2852 | /* We have to keep things in order. The alias initializer have to | |
2853 | come first, then the initialized regular field, in reverse to | |
2854 | keep them in lexical order. */ | |
2855 | tree marker, previous = NULL_TREE; | |
2856 | for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker)) | |
2857 | if (TREE_CODE (marker) == TREE_LIST | |
2858 | && !TREE_VALUE (marker) && !TREE_PURPOSE (marker)) | |
2859 | break; | |
2860 | ||
2861 | /* No static initialized, the list is fine as is */ | |
2862 | if (!previous) | |
2863 | list = TREE_CHAIN (marker); | |
2864 | ||
2865 | /* No marker? reverse the whole list */ | |
2866 | else if (!marker) | |
2867 | list = nreverse (list); | |
2868 | ||
2869 | /* Otherwise, reverse what's after the marker and the new reordered | |
2870 | sublist will replace the marker. */ | |
b351b287 | 2871 | else |
c2952b01 APB |
2872 | { |
2873 | TREE_CHAIN (previous) = NULL_TREE; | |
2874 | list = nreverse (list); | |
2875 | list = chainon (TREE_CHAIN (marker), list); | |
2876 | } | |
2877 | return list; | |
e04a16fb AG |
2878 | } |
2879 | ||
c2952b01 APB |
2880 | /* Helper functions to dump the parser context stack. */ |
2881 | ||
2882 | #define TAB_CONTEXT(C) \ | |
2883 | {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);} | |
ee07f4f4 APB |
2884 | |
2885 | static void | |
2886 | java_debug_context_do (tab) | |
2887 | int tab; | |
2888 | { | |
ee07f4f4 APB |
2889 | struct parser_ctxt *copy = ctxp; |
2890 | while (copy) | |
2891 | { | |
c2952b01 | 2892 | TAB_CONTEXT (tab); |
ee07f4f4 | 2893 | fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy); |
c2952b01 | 2894 | TAB_CONTEXT (tab); |
ee07f4f4 | 2895 | fprintf (stderr, "filename: %s\n", copy->filename); |
c2952b01 APB |
2896 | TAB_CONTEXT (tab); |
2897 | fprintf (stderr, "lineno: %d\n", copy->lineno); | |
2898 | TAB_CONTEXT (tab); | |
ee07f4f4 APB |
2899 | fprintf (stderr, "package: %s\n", |
2900 | (copy->package ? | |
2901 | IDENTIFIER_POINTER (copy->package) : "<none>")); | |
c2952b01 | 2902 | TAB_CONTEXT (tab); |
ee07f4f4 | 2903 | fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx); |
c2952b01 | 2904 | TAB_CONTEXT (tab); |
ee07f4f4 APB |
2905 | fprintf (stderr, "saved data: %d\n", copy->saved_data); |
2906 | copy = copy->next; | |
2907 | tab += 2; | |
2908 | } | |
ee07f4f4 APB |
2909 | } |
2910 | ||
c2952b01 APB |
2911 | /* Dump the stacked up parser contexts. Intended to be called from a |
2912 | debugger. */ | |
2913 | ||
ee07f4f4 APB |
2914 | void |
2915 | java_debug_context () | |
2916 | { | |
2917 | java_debug_context_do (0); | |
2918 | } | |
2919 | ||
c2952b01 APB |
2920 | \f |
2921 | ||
2922 | /* Flag for the error report routine to issue the error the first time | |
2923 | it's called (overriding the default behavior which is to drop the | |
2924 | first invocation and honor the second one, taking advantage of a | |
2925 | richer context. */ | |
2926 | static int force_error = 0; | |
ee07f4f4 | 2927 | |
8119c720 APB |
2928 | /* Reporting an constructor invocation error. */ |
2929 | static void | |
2930 | parse_ctor_invocation_error () | |
2931 | { | |
2932 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
2933 | yyerror ("Constructor invocation must be first thing in a constructor"); | |
2934 | else | |
2935 | yyerror ("Only constructors can invoke constructors"); | |
2936 | } | |
2937 | ||
2938 | /* Reporting JDK1.1 features not implemented. */ | |
b67d701b PB |
2939 | |
2940 | static tree | |
2941 | parse_jdk1_1_error (msg) | |
49f48c71 | 2942 | const char *msg; |
b67d701b PB |
2943 | { |
2944 | sorry (": `%s' JDK1.1(TM) feature", msg); | |
2945 | java_error_count++; | |
9bbc7d9f | 2946 | return empty_stmt_node; |
b67d701b PB |
2947 | } |
2948 | ||
e04a16fb AG |
2949 | static int do_warning = 0; |
2950 | ||
2951 | void | |
2952 | yyerror (msg) | |
49f48c71 | 2953 | const char *msg; |
e04a16fb AG |
2954 | { |
2955 | static java_lc elc; | |
2956 | static int prev_lineno; | |
49f48c71 | 2957 | static const char *prev_msg; |
e04a16fb | 2958 | |
0a2138e2 | 2959 | int save_lineno; |
e04a16fb AG |
2960 | char *remainder, *code_from_source; |
2961 | extern struct obstack temporary_obstack; | |
2962 | ||
2963 | if (!force_error && prev_lineno == lineno) | |
2964 | return; | |
2965 | ||
2966 | /* Save current error location but report latter, when the context is | |
2967 | richer. */ | |
2968 | if (ctxp->java_error_flag == 0) | |
2969 | { | |
2970 | ctxp->java_error_flag = 1; | |
2971 | elc = ctxp->elc; | |
2972 | /* Do something to use the previous line if we're reaching the | |
2973 | end of the file... */ | |
2974 | #ifdef VERBOSE_SKELETON | |
2975 | printf ("* Error detected (%s)\n", (msg ? msg : "(null)")); | |
2976 | #endif | |
2977 | return; | |
2978 | } | |
2979 | ||
2980 | /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */ | |
2981 | if (!force_error && msg == prev_msg && prev_lineno == elc.line) | |
2982 | return; | |
2983 | ||
2984 | ctxp->java_error_flag = 0; | |
2985 | if (do_warning) | |
2986 | java_warning_count++; | |
2987 | else | |
2988 | java_error_count++; | |
2989 | ||
807bc1db | 2990 | if (elc.col == 0 && msg && msg[1] == ';') |
e04a16fb AG |
2991 | { |
2992 | elc.col = ctxp->p_line->char_col-1; | |
2993 | elc.line = ctxp->p_line->lineno; | |
2994 | } | |
2995 | ||
2996 | save_lineno = lineno; | |
2997 | prev_lineno = lineno = elc.line; | |
2998 | prev_msg = msg; | |
2999 | ||
3000 | code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col); | |
3001 | obstack_grow0 (&temporary_obstack, | |
3002 | code_from_source, strlen (code_from_source)); | |
3003 | remainder = obstack_finish (&temporary_obstack); | |
3004 | if (do_warning) | |
3005 | warning ("%s.\n%s", msg, remainder); | |
3006 | else | |
3007 | error ("%s.\n%s", msg, remainder); | |
3008 | ||
3009 | /* This allow us to cheaply avoid an extra 'Invalid expression | |
3010 | statement' error report when errors have been already reported on | |
3011 | the same line. This occurs when we report an error but don't have | |
3012 | a synchronization point other than ';', which | |
3013 | expression_statement is the only one to take care of. */ | |
3014 | ctxp->prevent_ese = lineno = save_lineno; | |
3015 | } | |
3016 | ||
3017 | static void | |
15fdcfe9 | 3018 | issue_warning_error_from_context (cl, msg, ap) |
5e942c50 | 3019 | tree cl; |
d4476be2 | 3020 | const char *msg; |
15fdcfe9 | 3021 | va_list ap; |
5e942c50 | 3022 | { |
3b304f5b | 3023 | const char *saved, *saved_input_filename; |
15fdcfe9 PB |
3024 | char buffer [4096]; |
3025 | vsprintf (buffer, msg, ap); | |
3026 | force_error = 1; | |
5e942c50 APB |
3027 | |
3028 | ctxp->elc.line = EXPR_WFL_LINENO (cl); | |
82371d41 APB |
3029 | ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 : |
3030 | (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl))); | |
5e942c50 APB |
3031 | |
3032 | /* We have a CL, that's a good reason for using it if it contains data */ | |
3033 | saved = ctxp->filename; | |
3034 | if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl)) | |
3035 | ctxp->filename = EXPR_WFL_FILENAME (cl); | |
1886c9d8 APB |
3036 | saved_input_filename = input_filename; |
3037 | input_filename = ctxp->filename; | |
15fdcfe9 PB |
3038 | java_error (NULL); |
3039 | java_error (buffer); | |
5e942c50 | 3040 | ctxp->filename = saved; |
1886c9d8 | 3041 | input_filename = saved_input_filename; |
15fdcfe9 | 3042 | force_error = 0; |
5e942c50 APB |
3043 | } |
3044 | ||
e04a16fb AG |
3045 | /* Issue an error message at a current source line CL */ |
3046 | ||
15fdcfe9 | 3047 | void |
df32d2ce | 3048 | parse_error_context VPARAMS ((tree cl, const char *msg, ...)) |
e04a16fb | 3049 | { |
d4476be2 | 3050 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 3051 | tree cl; |
d4476be2 | 3052 | const char *msg; |
e04a16fb | 3053 | #endif |
e04a16fb AG |
3054 | va_list ap; |
3055 | ||
3056 | VA_START (ap, msg); | |
d4476be2 | 3057 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 3058 | cl = va_arg (ap, tree); |
d4476be2 | 3059 | msg = va_arg (ap, const char *); |
e04a16fb | 3060 | #endif |
15fdcfe9 PB |
3061 | issue_warning_error_from_context (cl, msg, ap); |
3062 | va_end (ap); | |
e04a16fb AG |
3063 | } |
3064 | ||
3065 | /* Issue a warning at a current source line CL */ | |
3066 | ||
3067 | static void | |
df32d2ce | 3068 | parse_warning_context VPARAMS ((tree cl, const char *msg, ...)) |
e04a16fb | 3069 | { |
d4476be2 | 3070 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 3071 | tree cl; |
d4476be2 | 3072 | const char *msg; |
e04a16fb | 3073 | #endif |
e04a16fb AG |
3074 | va_list ap; |
3075 | ||
3076 | VA_START (ap, msg); | |
d4476be2 | 3077 | #ifndef ANSI_PROTOTYPES |
e04a16fb | 3078 | cl = va_arg (ap, tree); |
d4476be2 | 3079 | msg = va_arg (ap, const char *); |
e04a16fb | 3080 | #endif |
e04a16fb | 3081 | |
c877974e | 3082 | force_error = do_warning = 1; |
15fdcfe9 | 3083 | issue_warning_error_from_context (cl, msg, ap); |
c877974e | 3084 | do_warning = force_error = 0; |
15fdcfe9 | 3085 | va_end (ap); |
e04a16fb AG |
3086 | } |
3087 | ||
82371d41 APB |
3088 | static tree |
3089 | find_expr_with_wfl (node) | |
3090 | tree node; | |
3091 | { | |
3092 | while (node) | |
3093 | { | |
3094 | char code; | |
3095 | tree to_return; | |
3096 | ||
3097 | switch (TREE_CODE (node)) | |
3098 | { | |
3099 | case BLOCK: | |
c0d87ff6 PB |
3100 | node = BLOCK_EXPR_BODY (node); |
3101 | continue; | |
82371d41 APB |
3102 | |
3103 | case COMPOUND_EXPR: | |
3104 | to_return = find_expr_with_wfl (TREE_OPERAND (node, 0)); | |
3105 | if (to_return) | |
3106 | return to_return; | |
c0d87ff6 PB |
3107 | node = TREE_OPERAND (node, 1); |
3108 | continue; | |
82371d41 APB |
3109 | |
3110 | case LOOP_EXPR: | |
c0d87ff6 PB |
3111 | node = TREE_OPERAND (node, 0); |
3112 | continue; | |
82371d41 APB |
3113 | |
3114 | case LABELED_BLOCK_EXPR: | |
c0d87ff6 PB |
3115 | node = TREE_OPERAND (node, 1); |
3116 | continue; | |
3117 | ||
82371d41 APB |
3118 | default: |
3119 | code = TREE_CODE_CLASS (TREE_CODE (node)); | |
3120 | if (((code == '1') || (code == '2') || (code == 'e')) | |
3121 | && EXPR_WFL_LINECOL (node)) | |
3122 | return node; | |
ba179f9f | 3123 | return NULL_TREE; |
82371d41 APB |
3124 | } |
3125 | } | |
3126 | return NULL_TREE; | |
3127 | } | |
3128 | ||
3129 | /* Issue a missing return statement error. Uses METHOD to figure the | |
3130 | last line of the method the error occurs in. */ | |
3131 | ||
3132 | static void | |
3133 | missing_return_error (method) | |
3134 | tree method; | |
3135 | { | |
3136 | EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2); | |
3137 | parse_error_context (wfl_operator, "Missing return statement"); | |
3138 | } | |
3139 | ||
3140 | /* Issue an unreachable statement error. From NODE, find the next | |
3141 | statement to report appropriately. */ | |
3142 | static void | |
3143 | unreachable_stmt_error (node) | |
3144 | tree node; | |
3145 | { | |
3146 | /* Browse node to find the next expression node that has a WFL. Use | |
3147 | the location to report the error */ | |
3148 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
3149 | node = find_expr_with_wfl (TREE_OPERAND (node, 1)); | |
3150 | else | |
3151 | node = find_expr_with_wfl (node); | |
3152 | ||
3153 | if (node) | |
3154 | { | |
3155 | EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2); | |
3156 | parse_error_context (wfl_operator, "Unreachable statement"); | |
3157 | } | |
3158 | else | |
3159 | fatal ("Can't get valid statement - unreachable_stmt_error"); | |
3160 | } | |
3161 | ||
c877974e | 3162 | int |
e04a16fb AG |
3163 | java_report_errors () |
3164 | { | |
3165 | if (java_error_count) | |
3166 | fprintf (stderr, "%d error%s", | |
3167 | java_error_count, (java_error_count == 1 ? "" : "s")); | |
3168 | if (java_warning_count) | |
3169 | fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""), | |
3170 | java_warning_count, (java_warning_count == 1 ? "" : "s")); | |
3171 | if (java_error_count || java_warning_count) | |
3172 | putc ('\n', stderr); | |
c877974e | 3173 | return java_error_count; |
e04a16fb AG |
3174 | } |
3175 | ||
3176 | static char * | |
3177 | java_accstring_lookup (flags) | |
3178 | int flags; | |
3179 | { | |
3180 | static char buffer [80]; | |
3181 | #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;} | |
3182 | ||
3183 | /* Access modifier looked-up first for easier report on forbidden | |
3184 | access. */ | |
3185 | if (flags & ACC_PUBLIC) COPY_RETURN ("public"); | |
3186 | if (flags & ACC_PRIVATE) COPY_RETURN ("private"); | |
3187 | if (flags & ACC_PROTECTED) COPY_RETURN ("protected"); | |
3188 | if (flags & ACC_STATIC) COPY_RETURN ("static"); | |
3189 | if (flags & ACC_FINAL) COPY_RETURN ("final"); | |
3190 | if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized"); | |
3191 | if (flags & ACC_VOLATILE) COPY_RETURN ("volatile"); | |
3192 | if (flags & ACC_TRANSIENT) COPY_RETURN ("transient"); | |
3193 | if (flags & ACC_NATIVE) COPY_RETURN ("native"); | |
3194 | if (flags & ACC_INTERFACE) COPY_RETURN ("interface"); | |
3195 | if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract"); | |
3196 | ||
3197 | buffer [0] = '\0'; | |
3198 | return buffer; | |
3199 | #undef COPY_RETURN | |
3200 | } | |
3201 | ||
b67d701b PB |
3202 | /* Issuing error messages upon redefinition of classes, interfaces or |
3203 | variables. */ | |
3204 | ||
e04a16fb | 3205 | static void |
b67d701b | 3206 | classitf_redefinition_error (context, id, decl, cl) |
49f48c71 | 3207 | const char *context; |
e04a16fb AG |
3208 | tree id, decl, cl; |
3209 | { | |
3210 | parse_error_context (cl, "%s `%s' already defined in %s:%d", | |
3211 | context, IDENTIFIER_POINTER (id), | |
3212 | DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl)); | |
3213 | /* Here we should point out where its redefined. It's a unicode. FIXME */ | |
3214 | } | |
3215 | ||
b67d701b PB |
3216 | static void |
3217 | variable_redefinition_error (context, name, type, line) | |
3218 | tree context, name, type; | |
3219 | int line; | |
3220 | { | |
49f48c71 | 3221 | const char *type_name; |
b67d701b PB |
3222 | |
3223 | /* Figure a proper name for type. We might haven't resolved it */ | |
c877974e APB |
3224 | if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type)) |
3225 | type_name = IDENTIFIER_POINTER (TYPE_NAME (type)); | |
b67d701b | 3226 | else |
0a2138e2 | 3227 | type_name = lang_printable_name (type, 0); |
b67d701b PB |
3228 | |
3229 | parse_error_context (context, | |
781b0558 | 3230 | "Variable `%s' is already defined in this method and was declared `%s %s' at line %d", |
b67d701b PB |
3231 | IDENTIFIER_POINTER (name), |
3232 | type_name, IDENTIFIER_POINTER (name), line); | |
3233 | } | |
3234 | ||
c583dd46 APB |
3235 | static tree |
3236 | build_array_from_name (type, type_wfl, name, ret_name) | |
3237 | tree type, type_wfl, name, *ret_name; | |
3238 | { | |
3239 | int more_dims = 0; | |
49f48c71 | 3240 | const char *string; |
c583dd46 APB |
3241 | |
3242 | /* Eventually get more dims */ | |
3243 | string = IDENTIFIER_POINTER (name); | |
3244 | while (string [more_dims] == '[') | |
3245 | more_dims++; | |
3246 | ||
3247 | /* If we have, then craft a new type for this variable */ | |
3248 | if (more_dims) | |
3249 | { | |
c0d87ff6 | 3250 | name = get_identifier (&string [more_dims]); |
c583dd46 | 3251 | |
34f4db93 APB |
3252 | /* If we have a pointer, use its type */ |
3253 | if (TREE_CODE (type) == POINTER_TYPE) | |
3254 | type = TREE_TYPE (type); | |
c583dd46 APB |
3255 | |
3256 | /* Building the first dimension of a primitive type uses this | |
3257 | function */ | |
3258 | if (JPRIMITIVE_TYPE_P (type)) | |
3259 | { | |
3260 | type = build_java_array_type (type, -1); | |
22eed1e6 | 3261 | CLASS_LOADED_P (type) = 1; |
c583dd46 APB |
3262 | more_dims--; |
3263 | } | |
3264 | /* Otherwise, if we have a WFL for this type, use it (the type | |
3265 | is already an array on an unresolved type, and we just keep | |
3266 | on adding dimensions) */ | |
3267 | else if (type_wfl) | |
3268 | type = type_wfl; | |
3269 | ||
3270 | /* Add all the dimensions */ | |
3271 | while (more_dims--) | |
3272 | type = build_unresolved_array_type (type); | |
3273 | ||
3274 | /* The type may have been incomplete in the first place */ | |
3275 | if (type_wfl) | |
3276 | type = obtain_incomplete_type (type); | |
3277 | } | |
3278 | ||
c2952b01 APB |
3279 | if (ret_name) |
3280 | *ret_name = name; | |
c583dd46 APB |
3281 | return type; |
3282 | } | |
3283 | ||
e04a16fb AG |
3284 | /* Build something that the type identifier resolver will identify as |
3285 | being an array to an unresolved type. TYPE_WFL is a WFL on a | |
3286 | identifier. */ | |
3287 | ||
3288 | static tree | |
3289 | build_unresolved_array_type (type_or_wfl) | |
3290 | tree type_or_wfl; | |
3291 | { | |
49f48c71 | 3292 | const char *ptr; |
e04a16fb | 3293 | |
1886c9d8 | 3294 | /* TYPE_OR_WFL might be an array on a resolved type. In this case, |
e04a16fb AG |
3295 | just create a array type */ |
3296 | if (TREE_CODE (type_or_wfl) == RECORD_TYPE) | |
3297 | { | |
3298 | tree type = build_java_array_type (type_or_wfl, -1); | |
3299 | CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl); | |
3300 | return type; | |
3301 | } | |
3302 | ||
3303 | obstack_1grow (&temporary_obstack, '['); | |
3304 | obstack_grow0 (&temporary_obstack, | |
3305 | IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)), | |
3306 | IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl))); | |
3307 | ptr = obstack_finish (&temporary_obstack); | |
34d4df06 APB |
3308 | EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr); |
3309 | return type_or_wfl; | |
e04a16fb AG |
3310 | } |
3311 | ||
e04a16fb AG |
3312 | static void |
3313 | parser_add_interface (class_decl, interface_decl, wfl) | |
3314 | tree class_decl, interface_decl, wfl; | |
3315 | { | |
3316 | if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl))) | |
3317 | parse_error_context (wfl, "Interface `%s' repeated", | |
3318 | IDENTIFIER_POINTER (DECL_NAME (interface_decl))); | |
3319 | } | |
3320 | ||
3321 | /* Bulk of common class/interface checks. Return 1 if an error was | |
3322 | encountered. TAG is 0 for a class, 1 for an interface. */ | |
3323 | ||
3324 | static int | |
3325 | check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl) | |
3326 | int is_interface, flags; | |
3327 | tree raw_name, qualified_name, decl, cl; | |
3328 | { | |
3329 | tree node; | |
c2952b01 APB |
3330 | int sca = 0; /* Static class allowed */ |
3331 | int icaf = 0; /* Inner class allowed flags */ | |
3332 | int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */ | |
e04a16fb AG |
3333 | |
3334 | if (!quiet_flag) | |
c2952b01 APB |
3335 | fprintf (stderr, " %s%s %s", |
3336 | (CPC_INNER_P () ? "inner" : ""), | |
3337 | (is_interface ? "interface" : "class"), | |
e04a16fb AG |
3338 | IDENTIFIER_POINTER (qualified_name)); |
3339 | ||
3340 | /* Scope of an interface/class type name: | |
3341 | - Can't be imported by a single type import | |
3342 | - Can't already exists in the package */ | |
3343 | if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name) | |
34d4df06 APB |
3344 | && (node = find_name_in_single_imports (raw_name)) |
3345 | && !CPC_INNER_P ()) | |
e04a16fb AG |
3346 | { |
3347 | parse_error_context | |
3348 | (cl, "%s name `%s' clashes with imported type `%s'", | |
3349 | (is_interface ? "Interface" : "Class"), | |
3350 | IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node)); | |
3351 | return 1; | |
3352 | } | |
3353 | if (decl && CLASS_COMPLETE_P (decl)) | |
3354 | { | |
b67d701b PB |
3355 | classitf_redefinition_error ((is_interface ? "Interface" : "Class"), |
3356 | qualified_name, decl, cl); | |
e04a16fb AG |
3357 | return 1; |
3358 | } | |
3359 | ||
c2952b01 APB |
3360 | if (check_inner_class_redefinition (raw_name, cl)) |
3361 | return 1; | |
3362 | ||
3363 | /* If public, file name should match class/interface name, except | |
3364 | when dealing with an inner class */ | |
3365 | if (!CPC_INNER_P () && (flags & ACC_PUBLIC )) | |
e04a16fb | 3366 | { |
49f48c71 | 3367 | const char *f; |
e04a16fb AG |
3368 | |
3369 | /* Contains OS dependent assumption on path separator. FIXME */ | |
3370 | for (f = &input_filename [strlen (input_filename)]; | |
fa322ab5 TT |
3371 | f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR; |
3372 | f--) | |
3373 | ; | |
847fe791 | 3374 | if (f[0] == '/' || f[0] == DIR_SEPARATOR) |
e04a16fb AG |
3375 | f++; |
3376 | if (strncmp (IDENTIFIER_POINTER (raw_name), | |
3377 | f , IDENTIFIER_LENGTH (raw_name)) || | |
3378 | f [IDENTIFIER_LENGTH (raw_name)] != '.') | |
781b0558 KG |
3379 | parse_error_context |
3380 | (cl, "Public %s `%s' must be defined in a file called `%s.java'", | |
e04a16fb AG |
3381 | (is_interface ? "interface" : "class"), |
3382 | IDENTIFIER_POINTER (qualified_name), | |
3383 | IDENTIFIER_POINTER (raw_name)); | |
3384 | } | |
3385 | ||
c2952b01 APB |
3386 | /* Static classes can be declared only in top level classes. Note: |
3387 | once static, a inner class is a top level class. */ | |
3388 | if (flags & ACC_STATIC) | |
3389 | { | |
3390 | /* Catch the specific error of declaring an class inner class | |
3391 | with no toplevel enclosing class. Prevent check_modifiers from | |
3392 | complaining a second time */ | |
3393 | if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC())) | |
3394 | { | |
3395 | parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes", | |
3396 | IDENTIFIER_POINTER (qualified_name)); | |
3397 | sca = ACC_STATIC; | |
3398 | } | |
3399 | /* Else, in the context of a top-level class declaration, let | |
3400 | `check_modifiers' do its job, otherwise, give it a go */ | |
3401 | else | |
3402 | sca = (GET_CPC_LIST () ? ACC_STATIC : 0); | |
3403 | } | |
3404 | ||
a40d21da | 3405 | /* Inner classes can be declared private or protected |
c2952b01 APB |
3406 | within their enclosing classes. */ |
3407 | if (CPC_INNER_P ()) | |
3408 | { | |
3409 | /* A class which is local to a block can't be public, private, | |
3410 | protected or static. But it is created final, so allow this | |
3411 | one. */ | |
3412 | if (current_function_decl) | |
3413 | icaf = sca = uaaf = ACC_FINAL; | |
3414 | else | |
3415 | { | |
3416 | check_modifiers_consistency (flags); | |
3417 | icaf = ACC_PRIVATE|ACC_PROTECTED; | |
3418 | } | |
3419 | } | |
3420 | ||
a40d21da APB |
3421 | if (is_interface) |
3422 | { | |
3423 | if (CPC_INNER_P ()) | |
3424 | uaaf = INTERFACE_INNER_MODIFIERS; | |
3425 | else | |
3426 | uaaf = INTERFACE_MODIFIERS; | |
3427 | ||
3428 | check_modifiers ("Illegal modifier `%s' for interface declaration", | |
3429 | flags, uaaf); | |
3430 | } | |
2884c41e | 3431 | else |
a40d21da APB |
3432 | check_modifiers ((current_function_decl ? |
3433 | "Illegal modifier `%s' for local class declaration" : | |
3434 | "Illegal modifier `%s' for class declaration"), | |
c2952b01 | 3435 | flags, uaaf|sca|icaf); |
e04a16fb AG |
3436 | return 0; |
3437 | } | |
3438 | ||
c2952b01 APB |
3439 | static void |
3440 | make_nested_class_name (cpc_list) | |
3441 | tree cpc_list; | |
3442 | { | |
3443 | tree name; | |
3444 | ||
3445 | if (!cpc_list) | |
3446 | return; | |
3447 | else | |
3448 | make_nested_class_name (TREE_CHAIN (cpc_list)); | |
3449 | ||
3450 | /* Pick the qualified name when dealing with the first upmost | |
3451 | enclosing class */ | |
3452 | name = (TREE_CHAIN (cpc_list) ? | |
3453 | TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list))); | |
3454 | obstack_grow (&temporary_obstack, | |
3455 | IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name)); | |
3456 | /* Why is NO_DOLLAR_IN_LABEL defined? */ | |
3457 | #if 0 | |
3458 | #ifdef NO_DOLLAR_IN_LABEL | |
3459 | fatal ("make_nested_class_name: Can't use '$' as a separator " | |
3460 | "for inner classes"); | |
3461 | #endif | |
3462 | #endif | |
3463 | obstack_1grow (&temporary_obstack, '$'); | |
3464 | } | |
3465 | ||
3466 | /* Can't redefine a class already defined in an earlier scope. */ | |
3467 | ||
3468 | static int | |
3469 | check_inner_class_redefinition (raw_name, cl) | |
3470 | tree raw_name, cl; | |
3471 | { | |
3472 | tree scope_list; | |
3473 | ||
3474 | for (scope_list = GET_CPC_LIST (); scope_list; | |
3475 | scope_list = GET_NEXT_ENCLOSING_CPC (scope_list)) | |
3476 | if (raw_name == GET_CPC_UN_NODE (scope_list)) | |
3477 | { | |
3478 | parse_error_context | |
3479 | (cl, "The class name `%s' is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes", | |
3480 | IDENTIFIER_POINTER (raw_name)); | |
3481 | return 1; | |
3482 | } | |
3483 | return 0; | |
3484 | } | |
3485 | ||
3486 | static tree | |
3487 | find_as_inner_class (enclosing, name, cl) | |
3488 | tree enclosing, name, cl; | |
3489 | { | |
3490 | tree qual, to_return; | |
3491 | if (!enclosing) | |
3492 | return NULL_TREE; | |
3493 | ||
3494 | name = TYPE_NAME (name); | |
3495 | ||
3496 | /* First search: within the scope of `enclosing', search for name */ | |
3497 | if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name) | |
3498 | qual = EXPR_WFL_QUALIFICATION (cl); | |
3499 | else if (cl) | |
3500 | qual = build_tree_list (cl, NULL_TREE); | |
3501 | else | |
3502 | qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE); | |
3503 | ||
3504 | if ((to_return = find_as_inner_class_do (qual, enclosing))) | |
3505 | return to_return; | |
3506 | ||
3507 | /* We're dealing with a qualified name. Try to resolve thing until | |
3508 | we get something that is an enclosing class. */ | |
3509 | if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name) | |
3510 | { | |
3511 | tree acc = NULL_TREE, decl = NULL_TREE, ptr; | |
3512 | ||
0c2b8145 APB |
3513 | for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl; |
3514 | qual = TREE_CHAIN (qual)) | |
c2952b01 APB |
3515 | { |
3516 | acc = merge_qualified_name (acc, | |
3517 | EXPR_WFL_NODE (TREE_PURPOSE (qual))); | |
3518 | BUILD_PTR_FROM_NAME (ptr, acc); | |
3519 | decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl); | |
3520 | } | |
3521 | ||
3522 | /* A NULL qual and a decl means that the search ended | |
3523 | successfully?!? We have to do something then. FIXME */ | |
3524 | ||
3525 | if (decl) | |
3526 | enclosing = decl; | |
3527 | else | |
3528 | qual = EXPR_WFL_QUALIFICATION (cl); | |
3529 | } | |
3530 | /* Otherwise, create a qual for the other part of the resolution. */ | |
3531 | else | |
3532 | qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE); | |
3533 | ||
1e12ab9b | 3534 | return find_as_inner_class_do (qual, enclosing); |
c2952b01 APB |
3535 | } |
3536 | ||
3537 | /* We go inside the list of sub classes and try to find a way | |
3538 | through. */ | |
3539 | ||
3540 | static tree | |
3541 | find_as_inner_class_do (qual, enclosing) | |
3542 | tree qual, enclosing; | |
3543 | { | |
3544 | if (!qual) | |
3545 | return NULL_TREE; | |
3546 | ||
3547 | for (; qual && enclosing; qual = TREE_CHAIN (qual)) | |
3548 | { | |
3549 | tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual)); | |
3550 | tree next_enclosing = NULL_TREE; | |
3551 | tree inner_list; | |
3552 | ||
3553 | for (inner_list = DECL_INNER_CLASS_LIST (enclosing); | |
3554 | inner_list; inner_list = TREE_CHAIN (inner_list)) | |
3555 | { | |
3556 | if (TREE_VALUE (inner_list) == name_to_match) | |
3557 | { | |
3558 | next_enclosing = TREE_PURPOSE (inner_list); | |
3559 | break; | |
3560 | } | |
3561 | } | |
3562 | enclosing = next_enclosing; | |
3563 | } | |
3564 | ||
3565 | return (!qual && enclosing ? enclosing : NULL_TREE); | |
3566 | } | |
3567 | ||
3568 | /* Reach all inner classes and tie their unqualified name to a | |
3569 | DECL. */ | |
3570 | ||
3571 | static void | |
3572 | set_nested_class_simple_name_value (outer, set) | |
3573 | tree outer; | |
3574 | int set; | |
3575 | { | |
3576 | tree l; | |
3577 | ||
3578 | for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l)) | |
3579 | IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ? | |
3580 | TREE_PURPOSE (l) : NULL_TREE); | |
3581 | } | |
3582 | ||
3583 | static void | |
3584 | link_nested_class_to_enclosing () | |
3585 | { | |
3586 | if (GET_ENCLOSING_CPC ()) | |
3587 | { | |
3588 | tree enclosing = GET_ENCLOSING_CPC_CONTEXT (); | |
3589 | DECL_INNER_CLASS_LIST (enclosing) = | |
3590 | tree_cons (GET_CPC (), GET_CPC_UN (), | |
3591 | DECL_INNER_CLASS_LIST (enclosing)); | |
3592 | enclosing = enclosing; | |
3593 | } | |
3594 | } | |
3595 | ||
3596 | static tree | |
3597 | maybe_make_nested_class_name (name) | |
3598 | tree name; | |
3599 | { | |
3600 | tree id = NULL_TREE; | |
3601 | ||
3602 | if (CPC_INNER_P ()) | |
3603 | { | |
3604 | make_nested_class_name (GET_CPC_LIST ()); | |
48a840d9 APB |
3605 | obstack_grow0 (&temporary_obstack, |
3606 | IDENTIFIER_POINTER (name), | |
3607 | IDENTIFIER_LENGTH (name)); | |
c2952b01 APB |
3608 | id = get_identifier (obstack_finish (&temporary_obstack)); |
3609 | if (ctxp->package) | |
3610 | QUALIFIED_P (id) = 1; | |
3611 | } | |
3612 | return id; | |
3613 | } | |
3614 | ||
3615 | /* If DECL is NULL, create and push a new DECL, record the current | |
3616 | line CL and do other maintenance things. */ | |
3617 | ||
e04a16fb | 3618 | static tree |
c2952b01 APB |
3619 | maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl) |
3620 | tree decl, raw_name, qualified_name, cl; | |
e04a16fb | 3621 | { |
5e942c50 | 3622 | if (!decl) |
e04a16fb | 3623 | decl = push_class (make_class (), qualified_name); |
c2952b01 | 3624 | |
e04a16fb AG |
3625 | /* Take care of the file and line business */ |
3626 | DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl); | |
f099f336 APB |
3627 | /* If we're emiting xrefs, store the line/col number information */ |
3628 | if (flag_emit_xref) | |
3629 | DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl); | |
3630 | else | |
3631 | DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl); | |
e04a16fb | 3632 | CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1; |
b351b287 APB |
3633 | CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) = |
3634 | IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl)); | |
e04a16fb | 3635 | |
c2952b01 APB |
3636 | PUSH_CPC (decl, raw_name); |
3637 | DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT (); | |
3638 | ||
e04a16fb AG |
3639 | /* Link the declaration to the already seen ones */ |
3640 | TREE_CHAIN (decl) = ctxp->class_list; | |
3641 | ctxp->class_list = decl; | |
5e942c50 | 3642 | |
23a79c61 | 3643 | /* Create a new nodes in the global lists */ |
5e942c50 | 3644 | ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list); |
23a79c61 | 3645 | all_class_list = tree_cons (NULL_TREE, decl, all_class_list); |
5e942c50 | 3646 | |
e04a16fb AG |
3647 | /* Install a new dependency list element */ |
3648 | create_jdep_list (ctxp); | |
3649 | ||
3650 | SOURCE_FRONTEND_DEBUG (("Defining class/interface %s", | |
3651 | IDENTIFIER_POINTER (qualified_name))); | |
3652 | return decl; | |
3653 | } | |
3654 | ||
3655 | static void | |
3656 | add_superinterfaces (decl, interface_list) | |
3657 | tree decl, interface_list; | |
3658 | { | |
3659 | tree node; | |
3660 | /* Superinterface(s): if present and defined, parser_check_super_interface () | |
3661 | takes care of ensuring that: | |
3662 | - This is an accessible interface type, | |
3663 | - Circularity detection. | |
3664 | parser_add_interface is then called. If present but not defined, | |
3665 | the check operation is delayed until the super interface gets | |
3666 | defined. */ | |
3667 | for (node = interface_list; node; node = TREE_CHAIN (node)) | |
3668 | { | |
15fdcfe9 | 3669 | tree current = TREE_PURPOSE (node); |
5e942c50 APB |
3670 | tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current)); |
3671 | if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl))) | |
e04a16fb | 3672 | { |
5e942c50 APB |
3673 | if (!parser_check_super_interface (idecl, decl, current)) |
3674 | parser_add_interface (decl, idecl, current); | |
e04a16fb AG |
3675 | } |
3676 | else | |
3677 | register_incomplete_type (JDEP_INTERFACE, | |
3678 | current, decl, NULL_TREE); | |
3679 | } | |
3680 | } | |
3681 | ||
3682 | /* Create an interface in pass1 and return its decl. Return the | |
3683 | interface's decl in pass 2. */ | |
3684 | ||
3685 | static tree | |
3686 | create_interface (flags, id, super) | |
3687 | int flags; | |
3688 | tree id, super; | |
3689 | { | |
e04a16fb | 3690 | tree raw_name = EXPR_WFL_NODE (id); |
98a52c2c | 3691 | tree q_name = parser_qualified_classname (raw_name); |
e04a16fb AG |
3692 | tree decl = IDENTIFIER_CLASS_VALUE (q_name); |
3693 | ||
3694 | EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */ | |
3695 | ||
3696 | /* Basic checks: scope, redefinition, modifiers */ | |
3697 | if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id)) | |
c2952b01 APB |
3698 | { |
3699 | PUSH_ERROR (); | |
3700 | return NULL_TREE; | |
3701 | } | |
3702 | ||
3703 | /* Suspend the current parsing context if we're parsing an inner | |
3704 | interface */ | |
3705 | if (CPC_INNER_P ()) | |
3706 | java_parser_context_suspend (); | |
3707 | ||
3708 | /* Push a new context for (static) initialized upon declaration fields */ | |
3709 | java_parser_context_push_initialized_field (); | |
e04a16fb AG |
3710 | |
3711 | /* Interface modifiers check | |
3712 | - public/abstract allowed (already done at that point) | |
3713 | - abstract is obsolete (comes first, it's a warning, or should be) | |
3714 | - Can't use twice the same (checked in the modifier rule) */ | |
c877974e | 3715 | if ((flags & ACC_ABSTRACT) && flag_redundant) |
e04a16fb AG |
3716 | parse_warning_context |
3717 | (MODIFIER_WFL (ABSTRACT_TK), | |
781b0558 | 3718 | "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name)); |
e04a16fb AG |
3719 | |
3720 | /* Create a new decl if DECL is NULL, otherwise fix it */ | |
c2952b01 | 3721 | decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id); |
e04a16fb AG |
3722 | |
3723 | /* Set super info and mark the class a complete */ | |
2aa11e97 | 3724 | set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl), |
e04a16fb AG |
3725 | object_type_node, ctxp->interface_number); |
3726 | ctxp->interface_number = 0; | |
3727 | CLASS_COMPLETE_P (decl) = 1; | |
3728 | add_superinterfaces (decl, super); | |
3729 | ||
3730 | return decl; | |
3731 | } | |
3732 | ||
c2952b01 APB |
3733 | /* Anonymous class counter. Will be reset to 1 every time a non |
3734 | anonymous class gets created. */ | |
3735 | static int anonymous_class_counter = 1; | |
3736 | ||
3737 | /* Patch anonymous class CLASS, by either extending or implementing | |
3738 | DEP. */ | |
3739 | ||
3740 | static void | |
3741 | patch_anonymous_class (type_decl, class_decl, wfl) | |
3742 | tree type_decl, class_decl, wfl; | |
3743 | { | |
3744 | tree class = TREE_TYPE (class_decl); | |
3745 | tree type = TREE_TYPE (type_decl); | |
3746 | tree binfo = TYPE_BINFO (class); | |
3747 | ||
3748 | /* If it's an interface, implement it */ | |
3749 | if (CLASS_INTERFACE (type_decl)) | |
3750 | { | |
3751 | tree s_binfo; | |
3752 | int length; | |
3753 | ||
3754 | if (parser_check_super_interface (type_decl, class_decl, wfl)) | |
3755 | return; | |
3756 | ||
3757 | s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0); | |
3758 | length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1; | |
3759 | TYPE_BINFO_BASETYPES (class) = make_tree_vec (length); | |
3760 | TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo; | |
3761 | /* And add the interface */ | |
3762 | parser_add_interface (class_decl, type_decl, wfl); | |
3763 | } | |
3764 | /* Otherwise, it's a type we want to extend */ | |
3765 | else | |
3766 | { | |
3767 | if (parser_check_super (type_decl, class_decl, wfl)) | |
3768 | return; | |
3769 | BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type; | |
3770 | } | |
3771 | } | |
3772 | ||
3773 | static tree | |
3774 | create_anonymous_class (location, type_name) | |
3775 | int location; | |
3776 | tree type_name; | |
3777 | { | |
3778 | char buffer [80]; | |
3779 | tree super = NULL_TREE, itf = NULL_TREE; | |
3780 | tree id, type_decl, class; | |
3781 | ||
3782 | /* The unqualified name of the anonymous class. It's just a number. */ | |
3783 | sprintf (buffer, "%d", anonymous_class_counter++); | |
3784 | id = build_wfl_node (get_identifier (buffer)); | |
3785 | EXPR_WFL_LINECOL (id) = location; | |
3786 | ||
3787 | /* We know about the type to extend/implement. We go ahead */ | |
3788 | if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name)))) | |
3789 | { | |
3790 | /* Create a class which either implements on extends the designated | |
3791 | class. The class bears an innacessible name. */ | |
3792 | if (CLASS_INTERFACE (type_decl)) | |
3793 | { | |
3794 | /* It's OK to modify it here. It's been already used and | |
3795 | shouldn't be reused */ | |
3796 | ctxp->interface_number = 1; | |
3797 | /* Interfaces should presented as a list of WFLs */ | |
3798 | itf = build_tree_list (type_name, NULL_TREE); | |
3799 | } | |
3800 | else | |
3801 | super = type_name; | |
3802 | } | |
3803 | ||
3804 | class = create_class (ACC_FINAL, id, super, itf); | |
3805 | ||
3806 | /* We didn't know anything about the stuff. We register a dependence. */ | |
3807 | if (!type_decl) | |
3808 | register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE); | |
3809 | ||
3810 | ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1; | |
3811 | return class; | |
3812 | } | |
3813 | ||
a40d21da | 3814 | /* Create a class in pass1 and return its decl. Return class |
e04a16fb AG |
3815 | interface's decl in pass 2. */ |
3816 | ||
3817 | static tree | |
3818 | create_class (flags, id, super, interfaces) | |
3819 | int flags; | |
3820 | tree id, super, interfaces; | |
3821 | { | |
e04a16fb AG |
3822 | tree raw_name = EXPR_WFL_NODE (id); |
3823 | tree class_id, decl; | |
9ee9b555 | 3824 | tree super_decl_type; |
e04a16fb | 3825 | |
98a52c2c | 3826 | class_id = parser_qualified_classname (raw_name); |
e04a16fb AG |
3827 | decl = IDENTIFIER_CLASS_VALUE (class_id); |
3828 | EXPR_WFL_NODE (id) = class_id; | |
3829 | ||
3830 | /* Basic check: scope, redefinition, modifiers */ | |
3831 | if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id)) | |
c2952b01 APB |
3832 | { |
3833 | PUSH_ERROR (); | |
3834 | return NULL_TREE; | |
3835 | } | |
3836 | ||
3837 | /* Suspend the current parsing context if we're parsing an inner | |
3838 | class or an anonymous class. */ | |
3839 | if (CPC_INNER_P ()) | |
3840 | java_parser_context_suspend (); | |
3841 | /* Push a new context for (static) initialized upon declaration fields */ | |
3842 | java_parser_context_push_initialized_field (); | |
e04a16fb AG |
3843 | |
3844 | /* Class modifier check: | |
3845 | - Allowed modifier (already done at that point) | |
3846 | - abstract AND final forbidden | |
3847 | - Public classes defined in the correct file */ | |
3848 | if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL)) | |
781b0558 KG |
3849 | parse_error_context |
3850 | (id, "Class `%s' can't be declared both abstract and final", | |
3851 | IDENTIFIER_POINTER (raw_name)); | |
e04a16fb AG |
3852 | |
3853 | /* Create a new decl if DECL is NULL, otherwise fix it */ | |
c2952b01 | 3854 | decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id); |
e04a16fb AG |
3855 | |
3856 | /* If SUPER exists, use it, otherwise use Object */ | |
3857 | if (super) | |
3858 | { | |
3859 | /* Can't extend java.lang.Object */ | |
3860 | if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node) | |
3861 | { | |
3862 | parse_error_context (id, "Can't extend `java.lang.Object'"); | |
3863 | return NULL_TREE; | |
3864 | } | |
3865 | ||
2c3199bc PB |
3866 | super_decl_type = |
3867 | register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE); | |
e04a16fb AG |
3868 | } |
3869 | else if (TREE_TYPE (decl) != object_type_node) | |
3870 | super_decl_type = object_type_node; | |
3871 | /* We're defining java.lang.Object */ | |
3872 | else | |
3873 | super_decl_type = NULL_TREE; | |
3874 | ||
3875 | /* Set super info and mark the class a complete */ | |
3876 | set_super_info (flags, TREE_TYPE (decl), super_decl_type, | |
3877 | ctxp->interface_number); | |
3878 | ctxp->interface_number = 0; | |
3879 | CLASS_COMPLETE_P (decl) = 1; | |
3880 | add_superinterfaces (decl, interfaces); | |
3881 | ||
c2952b01 APB |
3882 | /* Add the private this$<n> field, Replicate final locals still in |
3883 | scope as private final fields mangled like val$<local_name>. | |
3884 | This doesn't not occur for top level (static) inner classes. */ | |
3885 | if (PURE_INNER_CLASS_DECL_P (decl)) | |
3886 | add_inner_class_fields (decl, current_function_decl); | |
3887 | ||
7f10c2e2 APB |
3888 | /* If doing xref, store the location at which the inherited class |
3889 | (if any) was seen. */ | |
3890 | if (flag_emit_xref && super) | |
3891 | DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super); | |
3892 | ||
5e942c50 APB |
3893 | /* Eventually sets the @deprecated tag flag */ |
3894 | CHECK_DEPRECATED (decl); | |
3895 | ||
165f37bc APB |
3896 | /* Reset the anonymous class counter when declaring non inner classes */ |
3897 | if (!INNER_CLASS_DECL_P (decl)) | |
c2952b01 APB |
3898 | anonymous_class_counter = 1; |
3899 | ||
e04a16fb AG |
3900 | return decl; |
3901 | } | |
3902 | ||
c2952b01 | 3903 | /* End a class declaration: register the statements used to create |
c00f0fb2 | 3904 | finit$ and <clinit>, pop the current class and resume the prior |
c2952b01 APB |
3905 | parser context if necessary. */ |
3906 | ||
3907 | static void | |
3908 | end_class_declaration (resume) | |
3909 | int resume; | |
3910 | { | |
3911 | /* If an error occured, context weren't pushed and won't need to be | |
3912 | popped by a resume. */ | |
3913 | int no_error_occured = ctxp->next && GET_CPC () != error_mark_node; | |
3914 | ||
3915 | java_parser_context_pop_initialized_field (); | |
3916 | POP_CPC (); | |
3917 | if (resume && no_error_occured) | |
3918 | java_parser_context_resume (); | |
93220702 APB |
3919 | |
3920 | /* We're ending a class declaration, this is a good time to reset | |
3921 | the interface cout. Note that might have been already done in | |
3922 | create_interface, but if at that time an inner class was being | |
3923 | dealt with, the interface count was reset in a context created | |
3924 | for the sake of handling inner classes declaration. */ | |
3925 | ctxp->interface_number = 0; | |
c2952b01 APB |
3926 | } |
3927 | ||
3928 | static void | |
3929 | add_inner_class_fields (class_decl, fct_decl) | |
3930 | tree class_decl; | |
3931 | tree fct_decl; | |
3932 | { | |
3933 | tree block, marker, f; | |
3934 | ||
3935 | f = add_field (TREE_TYPE (class_decl), | |
3936 | build_current_thisn (TREE_TYPE (class_decl)), | |
3937 | build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))), | |
3938 | ACC_PRIVATE); | |
3939 | FIELD_THISN (f) = 1; | |
3940 | ||
3941 | if (!fct_decl) | |
3942 | return; | |
3943 | ||
3944 | for (block = GET_CURRENT_BLOCK (fct_decl); | |
3945 | block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block)) | |
3946 | { | |
3947 | tree decl; | |
3948 | for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl)) | |
3949 | { | |
1f8f4a0b | 3950 | tree name, pname; |
c2952b01 APB |
3951 | tree wfl, init, list; |
3952 | ||
3953 | /* Avoid non final arguments. */ | |
3954 | if (!LOCAL_FINAL (decl)) | |
3955 | continue; | |
3956 | ||
3957 | MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl)); | |
3958 | MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl)); | |
1f8f4a0b MM |
3959 | wfl = build_wfl_node (name); |
3960 | init = build_wfl_node (pname); | |
c2952b01 | 3961 | /* Build an initialization for the field: it will be |
c00f0fb2 | 3962 | initialized by a parameter added to finit$, bearing a |
c2952b01 | 3963 | mangled name of the field itself (param$<n>.) The |
c00f0fb2 | 3964 | parameter is provided to finit$ by the constructor |
c2952b01 APB |
3965 | invoking it (hence the constructor will also feature a |
3966 | hidden parameter, set to the value of the outer context | |
3967 | local at the time the inner class is created.) | |
3968 | ||
3969 | Note: we take into account all possible locals that can | |
3970 | be accessed by the inner class. It's actually not trivial | |
3971 | to minimize these aliases down to the ones really | |
3972 | used. One way to do that would be to expand all regular | |
c00f0fb2 | 3973 | methods first, then finit$ to get a picture of what's |
c2952b01 APB |
3974 | used. It works with the exception that we would have to |
3975 | go back on all constructor invoked in regular methods to | |
3976 | have their invokation reworked (to include the right amount | |
3977 | of alias initializer parameters.) | |
3978 | ||
3979 | The only real way around, I think, is a first pass to | |
3980 | identify locals really used in the inner class. We leave | |
3981 | the flag FIELD_LOCAL_ALIAS_USED around for that future | |
3982 | use. | |
3983 | ||
3984 | On the other hand, it only affect local inner classes, | |
c00f0fb2 | 3985 | whose constructors (and finit$ call) will be featuring |
c2952b01 APB |
3986 | unecessary arguments. It's easy for a developper to keep |
3987 | this number of parameter down by using the `final' | |
3988 | keyword only when necessary. For the time being, we can | |
3989 | issue a warning on unecessary finals. FIXME */ | |
3990 | init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl), | |
3991 | wfl, init); | |
3992 | ||
3993 | /* Register the field. The TREE_LIST holding the part | |
3994 | initialized/initializer will be marked ARG_FINAL_P so | |
3995 | that the created field can be marked | |
3996 | FIELD_LOCAL_ALIAS. */ | |
3997 | list = build_tree_list (wfl, init); | |
3998 | ARG_FINAL_P (list) = 1; | |
3999 | register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list); | |
4000 | } | |
4001 | } | |
4002 | ||
4003 | if (!CPC_INITIALIZER_STMT (ctxp)) | |
4004 | return; | |
4005 | ||
4006 | /* If we ever registered an alias field, insert and marker to | |
4007 | remeber where the list ends. The second part of the list (the one | |
4008 | featuring initialized fields) so it can be later reversed to | |
4009 | enforce 8.5. The marker will be removed during that operation. */ | |
4010 | marker = build_tree_list (NULL_TREE, NULL_TREE); | |
4011 | TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp); | |
4012 | SET_CPC_INITIALIZER_STMT (ctxp, marker); | |
4013 | } | |
4014 | ||
e04a16fb AG |
4015 | /* Can't use lookup_field () since we don't want to load the class and |
4016 | can't set the CLASS_LOADED_P flag */ | |
4017 | ||
4018 | static tree | |
4019 | find_field (class, name) | |
4020 | tree class; | |
4021 | tree name; | |
4022 | { | |
4023 | tree decl; | |
4024 | for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl)) | |
4025 | { | |
4026 | if (DECL_NAME (decl) == name) | |
4027 | return decl; | |
4028 | } | |
4029 | return NULL_TREE; | |
4030 | } | |
4031 | ||
4032 | /* Wrap around lookup_field that doesn't potentially upset the value | |
4033 | of CLASS */ | |
4034 | ||
4035 | static tree | |
4036 | lookup_field_wrapper (class, name) | |
4037 | tree class, name; | |
4038 | { | |
4039 | tree type = class; | |
9a7ab4b3 | 4040 | tree decl = NULL_TREE; |
c877974e | 4041 | java_parser_context_save_global (); |
f2760b27 APB |
4042 | |
4043 | /* Last chance: if we're within the context of an inner class, we | |
4044 | might be trying to access a local variable defined in an outer | |
4045 | context. We try to look for it now. */ | |
9a7ab4b3 | 4046 | if (INNER_CLASS_TYPE_P (class)) |
f2760b27 | 4047 | { |
9a7ab4b3 | 4048 | tree new_name; |
1f8f4a0b | 4049 | MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name); |
9a7ab4b3 | 4050 | decl = lookup_field (&type, new_name); |
f2760b27 APB |
4051 | if (decl && decl != error_mark_node) |
4052 | FIELD_LOCAL_ALIAS_USED (decl) = 1; | |
4053 | } | |
9a7ab4b3 APB |
4054 | if (!decl || decl == error_mark_node) |
4055 | { | |
4056 | type = class; | |
4057 | decl = lookup_field (&type, name); | |
4058 | } | |
f2760b27 | 4059 | |
c877974e | 4060 | java_parser_context_restore_global (); |
93024893 | 4061 | return decl == error_mark_node ? NULL : decl; |
e04a16fb AG |
4062 | } |
4063 | ||
4064 | /* Find duplicate field within the same class declarations and report | |
c583dd46 APB |
4065 | the error. Returns 1 if a duplicated field was found, 0 |
4066 | otherwise. */ | |
e04a16fb AG |
4067 | |
4068 | static int | |
c583dd46 | 4069 | duplicate_declaration_error_p (new_field_name, new_type, cl) |
0a2138e2 | 4070 | tree new_field_name, new_type, cl; |
e04a16fb AG |
4071 | { |
4072 | /* This might be modified to work with method decl as well */ | |
c2952b01 | 4073 | tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name); |
e04a16fb AG |
4074 | if (decl) |
4075 | { | |
c2e3db92 | 4076 | char *t1 = xstrdup (purify_type_name |
4a5f66c3 APB |
4077 | ((TREE_CODE (new_type) == POINTER_TYPE |
4078 | && TREE_TYPE (new_type) == NULL_TREE) ? | |
4079 | IDENTIFIER_POINTER (TYPE_NAME (new_type)) : | |
4080 | lang_printable_name (new_type, 1))); | |
c877974e APB |
4081 | /* The type may not have been completed by the time we report |
4082 | the error */ | |
c2e3db92 | 4083 | char *t2 = xstrdup (purify_type_name |
4a5f66c3 | 4084 | ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE |
c877974e APB |
4085 | && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ? |
4086 | IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) : | |
4087 | lang_printable_name (TREE_TYPE (decl), 1))); | |
e04a16fb AG |
4088 | parse_error_context |
4089 | (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)", | |
4090 | t1, IDENTIFIER_POINTER (new_field_name), | |
4091 | t2, IDENTIFIER_POINTER (DECL_NAME (decl)), | |
4092 | DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl)); | |
4093 | free (t1); | |
4094 | free (t2); | |
c583dd46 | 4095 | return 1; |
e04a16fb | 4096 | } |
c583dd46 | 4097 | return 0; |
e04a16fb AG |
4098 | } |
4099 | ||
4100 | /* Field registration routine. If TYPE doesn't exist, field | |
4101 | declarations are linked to the undefined TYPE dependency list, to | |
4102 | be later resolved in java_complete_class () */ | |
4103 | ||
4104 | static void | |
4105 | register_fields (flags, type, variable_list) | |
4106 | int flags; | |
4107 | tree type, variable_list; | |
4108 | { | |
c583dd46 | 4109 | tree current, saved_type; |
c2952b01 | 4110 | tree class_type = NULL_TREE; |
e04a16fb AG |
4111 | int saved_lineno = lineno; |
4112 | int must_chain = 0; | |
4113 | tree wfl = NULL_TREE; | |
4114 | ||
c2952b01 APB |
4115 | if (GET_CPC ()) |
4116 | class_type = TREE_TYPE (GET_CPC ()); | |
4117 | ||
4118 | if (!class_type || class_type == error_mark_node) | |
4119 | return; | |
4120 | ||
e04a16fb AG |
4121 | /* If we're adding fields to interfaces, those fields are public, |
4122 | static, final */ | |
4123 | if (CLASS_INTERFACE (TYPE_NAME (class_type))) | |
4124 | { | |
4125 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), | |
2884c41e | 4126 | flags, ACC_PUBLIC, "interface field(s)"); |
e04a16fb | 4127 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK), |
2884c41e | 4128 | flags, ACC_STATIC, "interface field(s)"); |
e04a16fb | 4129 | OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK), |
2884c41e | 4130 | flags, ACC_FINAL, "interface field(s)"); |
e04a16fb AG |
4131 | check_modifiers ("Illegal interface member modifier `%s'", flags, |
4132 | INTERFACE_FIELD_MODIFIERS); | |
4133 | flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL); | |
4134 | } | |
4135 | ||
c583dd46 APB |
4136 | /* Obtain a suitable type for resolution, if necessary */ |
4137 | SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain); | |
4138 | ||
4139 | /* If TYPE is fully resolved and we don't have a reference, make one */ | |
1886c9d8 | 4140 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
e04a16fb | 4141 | |
c583dd46 APB |
4142 | for (current = variable_list, saved_type = type; current; |
4143 | current = TREE_CHAIN (current), type = saved_type) | |
e04a16fb | 4144 | { |
c877974e | 4145 | tree real_type; |
c583dd46 | 4146 | tree field_decl; |
e04a16fb AG |
4147 | tree cl = TREE_PURPOSE (current); |
4148 | tree init = TREE_VALUE (current); | |
4149 | tree current_name = EXPR_WFL_NODE (cl); | |
4150 | ||
cf1748bf | 4151 | /* Can't declare non-final static fields in inner classes */ |
c2952b01 | 4152 | if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type) |
cf1748bf | 4153 | && !(flags & ACC_FINAL)) |
c2952b01 | 4154 | parse_error_context |
cf1748bf | 4155 | (cl, "Field `%s' can't be static in inner class `%s' unless it is final", |
c2952b01 APB |
4156 | IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)), |
4157 | lang_printable_name (class_type, 0)); | |
4158 | ||
c583dd46 APB |
4159 | /* Process NAME, as it may specify extra dimension(s) for it */ |
4160 | type = build_array_from_name (type, wfl, current_name, ¤t_name); | |
4161 | ||
c583dd46 APB |
4162 | /* Type adjustment. We may have just readjusted TYPE because |
4163 | the variable specified more dimensions. Make sure we have | |
22eed1e6 APB |
4164 | a reference if we can and don't have one already. Also |
4165 | change the name if we have an init. */ | |
4166 | if (type != saved_type) | |
4167 | { | |
1886c9d8 | 4168 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
22eed1e6 APB |
4169 | if (init) |
4170 | EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name; | |
4171 | } | |
e04a16fb | 4172 | |
c877974e APB |
4173 | real_type = GET_REAL_TYPE (type); |
4174 | /* Check for redeclarations */ | |
4175 | if (duplicate_declaration_error_p (current_name, real_type, cl)) | |
4176 | continue; | |
4177 | ||
c583dd46 | 4178 | /* Set lineno to the line the field was found and create a |
5e942c50 | 4179 | declaration for it. Eventually sets the @deprecated tag flag. */ |
f099f336 APB |
4180 | if (flag_emit_xref) |
4181 | lineno = EXPR_WFL_LINECOL (cl); | |
4182 | else | |
4183 | lineno = EXPR_WFL_LINENO (cl); | |
c877974e | 4184 | field_decl = add_field (class_type, current_name, real_type, flags); |
5e942c50 | 4185 | CHECK_DEPRECATED (field_decl); |
c2952b01 APB |
4186 | |
4187 | /* If the couple initializer/initialized is marked ARG_FINAL_P, we | |
4188 | mark the created field FIELD_LOCAL_ALIAS, so that we can | |
c00f0fb2 | 4189 | hide parameters to this inner class finit$ and constructors. */ |
c2952b01 APB |
4190 | if (ARG_FINAL_P (current)) |
4191 | FIELD_LOCAL_ALIAS (field_decl) = 1; | |
c583dd46 APB |
4192 | |
4193 | /* Check if we must chain. */ | |
4194 | if (must_chain) | |
4195 | register_incomplete_type (JDEP_FIELD, wfl, field_decl, type); | |
e04a16fb | 4196 | |
c583dd46 APB |
4197 | /* If we have an initialization value tied to the field */ |
4198 | if (init) | |
4199 | { | |
4200 | /* The field is declared static */ | |
e04a16fb | 4201 | if (flags & ACC_STATIC) |
e04a16fb | 4202 | { |
7525cc04 APB |
4203 | /* We include the field and its initialization part into |
4204 | a list used to generate <clinit>. After <clinit> is | |
ba179f9f APB |
4205 | walked, field initializations will be processed and |
4206 | fields initialized with known constants will be taken | |
4207 | out of <clinit> and have their DECL_INITIAL set | |
7525cc04 | 4208 | appropriately. */ |
c2952b01 APB |
4209 | TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp); |
4210 | SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init); | |
7f10c2e2 APB |
4211 | if (TREE_OPERAND (init, 1) |
4212 | && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT) | |
5bba4807 | 4213 | TREE_STATIC (TREE_OPERAND (init, 1)) = 1; |
e04a16fb | 4214 | } |
5e942c50 APB |
4215 | /* A non-static field declared with an immediate initialization is |
4216 | to be initialized in <init>, if any. This field is remembered | |
4217 | to be processed at the time of the generation of <init>. */ | |
c583dd46 APB |
4218 | else |
4219 | { | |
c2952b01 APB |
4220 | TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp); |
4221 | SET_CPC_INITIALIZER_STMT (ctxp, init); | |
c583dd46 | 4222 | } |
5b09b33e | 4223 | MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1; |
8576f094 | 4224 | DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1); |
e04a16fb AG |
4225 | } |
4226 | } | |
4227 | lineno = saved_lineno; | |
4228 | } | |
4229 | ||
c00f0fb2 APB |
4230 | /* Generate finit$, using the list of initialized fields to populate |
4231 | its body. finit$'s parameter(s) list is adjusted to include the | |
c2952b01 APB |
4232 | one(s) used to initialized the field(s) caching outer context |
4233 | local(s). */ | |
22eed1e6 | 4234 | |
c2952b01 APB |
4235 | static tree |
4236 | generate_finit (class_type) | |
4237 | tree class_type; | |
22eed1e6 | 4238 | { |
c2952b01 APB |
4239 | int count = 0; |
4240 | tree list = TYPE_FINIT_STMT_LIST (class_type); | |
4241 | tree mdecl, current, parms; | |
4242 | ||
4243 | parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION, | |
4244 | class_type, NULL_TREE, | |
4245 | &count); | |
4246 | CRAFTED_PARAM_LIST_FIXUP (parms); | |
4247 | mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node, | |
4248 | finit_identifier_node, parms); | |
4249 | fix_method_argument_names (parms, mdecl); | |
4250 | layout_class_method (class_type, CLASSTYPE_SUPER (class_type), | |
4251 | mdecl, NULL_TREE); | |
4252 | DECL_FUNCTION_NAP (mdecl) = count; | |
22eed1e6 APB |
4253 | start_artificial_method_body (mdecl); |
4254 | ||
c2952b01 | 4255 | for (current = list; current; current = TREE_CHAIN (current)) |
22eed1e6 APB |
4256 | java_method_add_stmt (mdecl, |
4257 | build_debugable_stmt (EXPR_WFL_LINECOL (current), | |
4258 | current)); | |
22eed1e6 | 4259 | end_artificial_method_body (mdecl); |
c2952b01 | 4260 | return mdecl; |
22eed1e6 APB |
4261 | } |
4262 | ||
e04a16fb | 4263 | static void |
c2952b01 APB |
4264 | add_instance_initializer (mdecl) |
4265 | tree mdecl; | |
e04a16fb | 4266 | { |
c2952b01 APB |
4267 | tree current; |
4268 | tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl)); | |
4269 | tree compound = NULL_TREE; | |
e04a16fb | 4270 | |
c2952b01 | 4271 | if (stmt_list) |
e04a16fb | 4272 | { |
c2952b01 APB |
4273 | for (current = stmt_list; current; current = TREE_CHAIN (current)) |
4274 | compound = add_stmt_to_compound (compound, NULL_TREE, current); | |
e04a16fb | 4275 | |
c2952b01 APB |
4276 | java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR, |
4277 | NULL_TREE, compound)); | |
4278 | } | |
e04a16fb AG |
4279 | } |
4280 | ||
4281 | /* Shared accros method_declarator and method_header to remember the | |
4282 | patch stage that was reached during the declaration of the method. | |
4283 | A method DECL is built differently is there is no patch | |
4284 | (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN) | |
4285 | pending on the currently defined method. */ | |
4286 | ||
4287 | static int patch_stage; | |
4288 | ||
4289 | /* Check the method declaration and add the method to its current | |
4290 | class. If the argument list is known to contain incomplete types, | |
4291 | the method is partially added and the registration will be resume | |
22eed1e6 APB |
4292 | once the method arguments resolved. If TYPE is NULL, we're dealing |
4293 | with a constructor. */ | |
e04a16fb AG |
4294 | |
4295 | static tree | |
4296 | method_header (flags, type, mdecl, throws) | |
4297 | int flags; | |
4298 | tree type, mdecl, throws; | |
4299 | { | |
1886c9d8 | 4300 | tree type_wfl = NULL_TREE; |
79d13333 | 4301 | tree meth_name = NULL_TREE; |
c2952b01 | 4302 | tree current, orig_arg, this_class = NULL; |
34d4df06 | 4303 | tree id, meth; |
e04a16fb | 4304 | int saved_lineno; |
1886c9d8 | 4305 | int constructor_ok = 0, must_chain; |
c2952b01 | 4306 | int count; |
34d4df06 APB |
4307 | |
4308 | if (mdecl == error_mark_node) | |
4309 | return error_mark_node; | |
4310 | meth = TREE_VALUE (mdecl); | |
4311 | id = TREE_PURPOSE (mdecl); | |
e04a16fb AG |
4312 | |
4313 | check_modifiers_consistency (flags); | |
79d13333 | 4314 | |
c2952b01 APB |
4315 | if (GET_CPC ()) |
4316 | this_class = TREE_TYPE (GET_CPC ()); | |
4317 | ||
4318 | if (!this_class || this_class == error_mark_node) | |
79d13333 | 4319 | return NULL_TREE; |
e04a16fb AG |
4320 | |
4321 | /* There are some forbidden modifiers for an abstract method and its | |
4322 | class must be abstract as well. */ | |
22eed1e6 | 4323 | if (type && (flags & ACC_ABSTRACT)) |
e04a16fb AG |
4324 | { |
4325 | ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private"); | |
4326 | ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static"); | |
4327 | ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final"); | |
4328 | ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native"); | |
4329 | ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized"); | |
2aa11e97 APB |
4330 | if (!CLASS_ABSTRACT (TYPE_NAME (this_class)) |
4331 | && !CLASS_INTERFACE (TYPE_NAME (this_class))) | |
e04a16fb | 4332 | parse_error_context |
781b0558 | 4333 | (id, "Class `%s' must be declared abstract to define abstract method `%s'", |
e7c7bcef | 4334 | IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())), |
e04a16fb AG |
4335 | IDENTIFIER_POINTER (EXPR_WFL_NODE (id))); |
4336 | } | |
c2952b01 | 4337 | |
22eed1e6 APB |
4338 | /* Things to be checked when declaring a constructor */ |
4339 | if (!type) | |
4340 | { | |
4341 | int ec = java_error_count; | |
4342 | /* 8.6: Constructor declarations: we might be trying to define a | |
4343 | method without specifying a return type. */ | |
c2952b01 | 4344 | if (EXPR_WFL_NODE (id) != GET_CPC_UN ()) |
22eed1e6 APB |
4345 | parse_error_context |
4346 | (id, "Invalid method declaration, return type required"); | |
4347 | /* 8.6.3: Constructor modifiers */ | |
4348 | else | |
4349 | { | |
4350 | JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract"); | |
4351 | JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static"); | |
4352 | JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final"); | |
4353 | JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native"); | |
4354 | JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized"); | |
4355 | } | |
4356 | /* If we found error here, we don't consider it's OK to tread | |
4357 | the method definition as a constructor, for the rest of this | |
4358 | function */ | |
4359 | if (ec == java_error_count) | |
4360 | constructor_ok = 1; | |
4361 | } | |
e04a16fb AG |
4362 | |
4363 | /* Method declared within the scope of an interface are implicitly | |
4364 | abstract and public. Conflicts with other erroneously provided | |
c0d87ff6 | 4365 | modifiers are checked right after. */ |
e04a16fb AG |
4366 | |
4367 | if (CLASS_INTERFACE (TYPE_NAME (this_class))) | |
4368 | { | |
4369 | /* If FLAGS isn't set because of a modifier, turn the | |
4370 | corresponding modifier WFL to NULL so we issue a warning on | |
4371 | the obsolete use of the modifier */ | |
4372 | if (!(flags & ACC_PUBLIC)) | |
4373 | MODIFIER_WFL (PUBLIC_TK) = NULL; | |
4374 | if (!(flags & ACC_ABSTRACT)) | |
4375 | MODIFIER_WFL (ABSTRACT_TK) = NULL; | |
4376 | flags |= ACC_PUBLIC; | |
4377 | flags |= ACC_ABSTRACT; | |
4378 | } | |
4379 | ||
c2952b01 APB |
4380 | /* Inner class can't declare static methods */ |
4381 | if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class)) | |
4382 | { | |
4383 | parse_error_context | |
4384 | (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static", | |
4385 | IDENTIFIER_POINTER (EXPR_WFL_NODE (id)), | |
4386 | lang_printable_name (this_class, 0)); | |
4387 | } | |
4388 | ||
e04a16fb AG |
4389 | /* Modifiers context reset moved up, so abstract method declaration |
4390 | modifiers can be later checked. */ | |
4391 | ||
22eed1e6 APB |
4392 | /* Set constructor returned type to void and method name to <init>, |
4393 | unless we found an error identifier the constructor (in which | |
4394 | case we retain the original name) */ | |
4395 | if (!type) | |
4396 | { | |
4397 | type = void_type_node; | |
4398 | if (constructor_ok) | |
4399 | meth_name = init_identifier_node; | |
4400 | } | |
4401 | else | |
4402 | meth_name = EXPR_WFL_NODE (id); | |
e04a16fb | 4403 | |
1886c9d8 APB |
4404 | /* Do the returned type resolution and registration if necessary */ |
4405 | SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain); | |
4406 | ||
4a5f66c3 APB |
4407 | if (meth_name) |
4408 | type = build_array_from_name (type, type_wfl, meth_name, &meth_name); | |
1886c9d8 APB |
4409 | EXPR_WFL_NODE (id) = meth_name; |
4410 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); | |
4411 | ||
4412 | if (must_chain) | |
e04a16fb | 4413 | { |
1886c9d8 APB |
4414 | patch_stage = JDEP_METHOD_RETURN; |
4415 | register_incomplete_type (patch_stage, type_wfl, id, type); | |
4416 | TREE_TYPE (meth) = GET_REAL_TYPE (type); | |
e04a16fb AG |
4417 | } |
4418 | else | |
1886c9d8 | 4419 | TREE_TYPE (meth) = type; |
e04a16fb AG |
4420 | |
4421 | saved_lineno = lineno; | |
4422 | /* When defining an abstract or interface method, the curly | |
4423 | bracket at level 1 doesn't exist because there is no function | |
4424 | body */ | |
4425 | lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 : | |
4426 | EXPR_WFL_LINENO (id)); | |
4427 | ||
5e942c50 APB |
4428 | /* Remember the original argument list */ |
4429 | orig_arg = TYPE_ARG_TYPES (meth); | |
4430 | ||
e04a16fb AG |
4431 | if (patch_stage) /* includes ret type and/or all args */ |
4432 | { | |
4433 | jdep *jdep; | |
4434 | meth = add_method_1 (this_class, flags, meth_name, meth); | |
4435 | /* Patch for the return type */ | |
4436 | if (patch_stage == JDEP_METHOD_RETURN) | |
4437 | { | |
4438 | jdep = CLASSD_LAST (ctxp->classd_list); | |
4439 | JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth)); | |
4440 | } | |
4441 | /* This is the stop JDEP. METH allows the function's signature | |
4442 | to be computed. */ | |
4443 | register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE); | |
4444 | } | |
4445 | else | |
5e942c50 APB |
4446 | meth = add_method (this_class, flags, meth_name, |
4447 | build_java_signature (meth)); | |
4448 | ||
c2952b01 APB |
4449 | /* Remember final parameters */ |
4450 | MARK_FINAL_PARMS (meth, orig_arg); | |
4451 | ||
5e942c50 APB |
4452 | /* Fix the method argument list so we have the argument name |
4453 | information */ | |
4454 | fix_method_argument_names (orig_arg, meth); | |
4455 | ||
4456 | /* Register the parameter number and re-install the current line | |
4457 | number */ | |
e04a16fb AG |
4458 | DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1; |
4459 | lineno = saved_lineno; | |
b9f7e36c APB |
4460 | |
4461 | /* Register exception specified by the `throws' keyword for | |
4462 | resolution and set the method decl appropriate field to the list. | |
4463 | Note: the grammar ensures that what we get here are class | |
4464 | types. */ | |
4465 | if (throws) | |
4466 | { | |
4467 | throws = nreverse (throws); | |
4468 | for (current = throws; current; current = TREE_CHAIN (current)) | |
4469 | { | |
4470 | register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current), | |
4471 | NULL_TREE, NULL_TREE); | |
4472 | JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) = | |
4473 | &TREE_VALUE (current); | |
4474 | } | |
4475 | DECL_FUNCTION_THROWS (meth) = throws; | |
4476 | } | |
4477 | ||
e04a16fb AG |
4478 | /* We set the DECL_NAME to ID so we can track the location where |
4479 | the function was declared. This allow us to report | |
4480 | redefinition error accurately. When method are verified, | |
4481 | DECL_NAME is reinstalled properly (using the content of the | |
4482 | WFL node ID) (see check_method_redefinition). We don't do that | |
22eed1e6 APB |
4483 | when Object is being defined. Constructor <init> names will be |
4484 | reinstalled the same way. */ | |
c2952b01 | 4485 | if (TREE_TYPE (GET_CPC ()) != object_type_node) |
e04a16fb | 4486 | DECL_NAME (meth) = id; |
22eed1e6 APB |
4487 | |
4488 | /* Set the flag if we correctly processed a constructor */ | |
4489 | if (constructor_ok) | |
c2952b01 APB |
4490 | { |
4491 | DECL_CONSTRUCTOR_P (meth) = 1; | |
4492 | /* Compute and store the number of artificial parameters declared | |
4493 | for this constructor */ | |
4494 | for (count = 0, current = TYPE_FIELDS (this_class); current; | |
4495 | current = TREE_CHAIN (current)) | |
4496 | if (FIELD_LOCAL_ALIAS (current)) | |
4497 | count++; | |
4498 | DECL_FUNCTION_NAP (meth) = count; | |
4499 | } | |
22eed1e6 | 4500 | |
5e942c50 APB |
4501 | /* Eventually set the @deprecated tag flag */ |
4502 | CHECK_DEPRECATED (meth); | |
4503 | ||
7f10c2e2 APB |
4504 | /* If doing xref, store column and line number information instead |
4505 | of the line number only. */ | |
4506 | if (flag_emit_xref) | |
4507 | DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id); | |
4508 | ||
e04a16fb AG |
4509 | return meth; |
4510 | } | |
4511 | ||
5e942c50 APB |
4512 | static void |
4513 | fix_method_argument_names (orig_arg, meth) | |
4514 | tree orig_arg, meth; | |
4515 | { | |
4516 | tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth)); | |
4517 | if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE) | |
4518 | { | |
4519 | TREE_PURPOSE (arg) = this_identifier_node; | |
4520 | arg = TREE_CHAIN (arg); | |
4521 | } | |
de4c7b02 | 4522 | while (orig_arg != end_params_node) |
5e942c50 APB |
4523 | { |
4524 | TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg); | |
4525 | orig_arg = TREE_CHAIN (orig_arg); | |
4526 | arg = TREE_CHAIN (arg); | |
4527 | } | |
4528 | } | |
4529 | ||
22eed1e6 APB |
4530 | /* Complete the method declaration with METHOD_BODY. */ |
4531 | ||
4532 | static void | |
b635eb2f | 4533 | finish_method_declaration (method_body) |
22eed1e6 APB |
4534 | tree method_body; |
4535 | { | |
79d13333 APB |
4536 | int flags; |
4537 | ||
4538 | if (!current_function_decl) | |
4539 | return; | |
4540 | ||
4541 | flags = get_access_flags_from_decl (current_function_decl); | |
5256aa37 APB |
4542 | |
4543 | /* 8.4.5 Method Body */ | |
4544 | if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body) | |
4545 | { | |
4546 | tree wfl = DECL_NAME (current_function_decl); | |
4547 | parse_error_context (wfl, | |
4548 | "%s method `%s' can't have a body defined", | |
4549 | (METHOD_NATIVE (current_function_decl) ? | |
4550 | "Native" : "Abstract"), | |
4551 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
4552 | method_body = NULL_TREE; | |
4553 | } | |
4554 | else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body) | |
4555 | { | |
4556 | tree wfl = DECL_NAME (current_function_decl); | |
781b0558 KG |
4557 | parse_error_context |
4558 | (wfl, | |
4559 | "Non native and non abstract method `%s' must have a body defined", | |
4560 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
5256aa37 APB |
4561 | method_body = NULL_TREE; |
4562 | } | |
4563 | ||
2c56429a APB |
4564 | if (flag_emit_class_files && method_body |
4565 | && TREE_CODE (method_body) == NOP_EXPR | |
4566 | && TREE_TYPE (current_function_decl) | |
4567 | && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node) | |
4568 | method_body = build1 (RETURN_EXPR, void_type_node, NULL); | |
e803d3b2 | 4569 | |
22eed1e6 APB |
4570 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body; |
4571 | maybe_absorb_scoping_blocks (); | |
4572 | /* Exit function's body */ | |
4573 | exit_block (); | |
4574 | /* Merge last line of the function with first line, directly in the | |
4575 | function decl. It will be used to emit correct debug info. */ | |
7f10c2e2 APB |
4576 | if (!flag_emit_xref) |
4577 | DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1); | |
c2952b01 APB |
4578 | |
4579 | /* Since function's argument's list are shared, reset the | |
4580 | ARG_FINAL_P parameter that might have been set on some of this | |
4581 | function parameters. */ | |
4582 | UNMARK_FINAL_PARMS (current_function_decl); | |
4583 | ||
f099f336 APB |
4584 | /* So we don't have an irrelevant function declaration context for |
4585 | the next static block we'll see. */ | |
4586 | current_function_decl = NULL_TREE; | |
22eed1e6 APB |
4587 | } |
4588 | ||
4589 | /* Build a an error message for constructor circularity errors. */ | |
4590 | ||
4591 | static char * | |
4592 | constructor_circularity_msg (from, to) | |
4593 | tree from, to; | |
4594 | { | |
4595 | static char string [4096]; | |
c2e3db92 | 4596 | char *t = xstrdup (lang_printable_name (from, 0)); |
22eed1e6 APB |
4597 | sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0)); |
4598 | free (t); | |
4599 | return string; | |
4600 | } | |
4601 | ||
4602 | /* Verify a circular call to METH. Return 1 if an error is found, 0 | |
4603 | otherwise. */ | |
4604 | ||
4605 | static int | |
4606 | verify_constructor_circularity (meth, current) | |
4607 | tree meth, current; | |
4608 | { | |
4609 | static tree list = NULL_TREE; | |
19e223db | 4610 | static int initialized_p; |
22eed1e6 | 4611 | tree c; |
19e223db MM |
4612 | |
4613 | /* If we haven't already registered LIST with the garbage collector, | |
4614 | do so now. */ | |
4615 | if (!initialized_p) | |
4616 | { | |
4617 | ggc_add_tree_root (&list, 1); | |
4618 | initialized_p = 1; | |
4619 | } | |
4620 | ||
22eed1e6 APB |
4621 | for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c)) |
4622 | { | |
4623 | if (TREE_VALUE (c) == meth) | |
4624 | { | |
4625 | char *t; | |
4626 | if (list) | |
4627 | { | |
4628 | tree liste; | |
4629 | list = nreverse (list); | |
4630 | for (liste = list; liste; liste = TREE_CHAIN (liste)) | |
4631 | { | |
4632 | parse_error_context | |
c63b98cd | 4633 | (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s", |
22eed1e6 APB |
4634 | constructor_circularity_msg |
4635 | (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste)))); | |
4636 | java_error_count--; | |
4637 | } | |
4638 | } | |
c2e3db92 | 4639 | t = xstrdup (lang_printable_name (meth, 0)); |
22eed1e6 APB |
4640 | parse_error_context (TREE_PURPOSE (c), |
4641 | "%s: recursive invocation of constructor `%s'", | |
4642 | constructor_circularity_msg (current, meth), t); | |
4643 | free (t); | |
4644 | list = NULL_TREE; | |
4645 | return 1; | |
4646 | } | |
4647 | } | |
4648 | for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c)) | |
4649 | { | |
4650 | list = tree_cons (c, current, list); | |
4651 | if (verify_constructor_circularity (meth, TREE_VALUE (c))) | |
4652 | return 1; | |
4653 | list = TREE_CHAIN (list); | |
4654 | } | |
4655 | return 0; | |
4656 | } | |
4657 | ||
e04a16fb AG |
4658 | /* Check modifiers that can be declared but exclusively */ |
4659 | ||
4660 | static void | |
4661 | check_modifiers_consistency (flags) | |
4662 | int flags; | |
4663 | { | |
4664 | int acc_count = 0; | |
4665 | tree cl = NULL_TREE; | |
4666 | ||
e0fc4118 TT |
4667 | THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl); |
4668 | THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl); | |
4669 | THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl); | |
e04a16fb AG |
4670 | if (acc_count > 1) |
4671 | parse_error_context | |
e0fc4118 TT |
4672 | (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified"); |
4673 | ||
4674 | acc_count = 0; | |
4675 | cl = NULL_TREE; | |
14d075d8 TT |
4676 | THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl); |
4677 | THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl); | |
e0fc4118 TT |
4678 | if (acc_count > 1) |
4679 | parse_error_context (cl, | |
4680 | "Inconsistent member declaration. At most one of `final' or `volatile' may be specified"); | |
e04a16fb AG |
4681 | } |
4682 | ||
4683 | /* Check the methode header METH for abstract specifics features */ | |
4684 | ||
4685 | static void | |
4686 | check_abstract_method_header (meth) | |
4687 | tree meth; | |
4688 | { | |
4689 | int flags = get_access_flags_from_decl (meth); | |
4690 | /* DECL_NAME might still be a WFL node */ | |
c877974e | 4691 | tree name = GET_METHOD_NAME (meth); |
e04a16fb | 4692 | |
2884c41e KG |
4693 | OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags, |
4694 | ACC_ABSTRACT, "abstract method", | |
4695 | IDENTIFIER_POINTER (name)); | |
4696 | OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags, | |
4697 | ACC_PUBLIC, "abstract method", | |
4698 | IDENTIFIER_POINTER (name)); | |
e04a16fb AG |
4699 | |
4700 | check_modifiers ("Illegal modifier `%s' for interface method", | |
4701 | flags, INTERFACE_METHOD_MODIFIERS); | |
4702 | } | |
4703 | ||
4704 | /* Create a FUNCTION_TYPE node and start augmenting it with the | |
4705 | declared function arguments. Arguments type that can't be resolved | |
4706 | are left as they are, but the returned node is marked as containing | |
4707 | incomplete types. */ | |
4708 | ||
4709 | static tree | |
4710 | method_declarator (id, list) | |
4711 | tree id, list; | |
4712 | { | |
4713 | tree arg_types = NULL_TREE, current, node; | |
4714 | tree meth = make_node (FUNCTION_TYPE); | |
4715 | jdep *jdep; | |
e04a16fb AG |
4716 | |
4717 | patch_stage = JDEP_NO_PATCH; | |
c2952b01 | 4718 | |
34d4df06 APB |
4719 | if (GET_CPC () == error_mark_node) |
4720 | return error_mark_node; | |
4721 | ||
c2952b01 APB |
4722 | /* If we're dealing with an inner class constructor, we hide the |
4723 | this$<n> decl in the name field of its parameter declaration. We | |
4724 | also might have to hide the outer context local alias | |
4725 | initializers. Not done when the class is a toplevel class. */ | |
4726 | if (PURE_INNER_CLASS_DECL_P (GET_CPC ()) | |
4727 | && EXPR_WFL_NODE (id) == GET_CPC_UN ()) | |
4728 | { | |
4729 | tree aliases_list, type, thisn; | |
4730 | /* First the aliases, linked to the regular parameters */ | |
4731 | aliases_list = | |
4732 | build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION, | |
4733 | TREE_TYPE (GET_CPC ()), | |
4734 | NULL_TREE, NULL); | |
4735 | list = chainon (nreverse (aliases_list), list); | |
4736 | ||
4737 | /* Then this$<n> */ | |
4738 | type = TREE_TYPE (DECL_CONTEXT (GET_CPC ())); | |
9a7ab4b3 | 4739 | thisn = build_current_thisn (TREE_TYPE (GET_CPC ())); |
c2952b01 APB |
4740 | list = tree_cons (build_wfl_node (thisn), build_pointer_type (type), |
4741 | list); | |
4742 | } | |
e04a16fb AG |
4743 | |
4744 | for (current = list; current; current = TREE_CHAIN (current)) | |
4745 | { | |
c583dd46 | 4746 | int must_chain = 0; |
e04a16fb AG |
4747 | tree wfl_name = TREE_PURPOSE (current); |
4748 | tree type = TREE_VALUE (current); | |
4749 | tree name = EXPR_WFL_NODE (wfl_name); | |
c583dd46 APB |
4750 | tree already, arg_node; |
4751 | tree type_wfl = NULL_TREE; | |
23a79c61 | 4752 | tree real_type; |
c583dd46 APB |
4753 | |
4754 | /* Obtain a suitable type for resolution, if necessary */ | |
4755 | SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain); | |
4756 | ||
4757 | /* Process NAME, as it may specify extra dimension(s) for it */ | |
4758 | type = build_array_from_name (type, type_wfl, name, &name); | |
4759 | EXPR_WFL_NODE (wfl_name) = name; | |
e04a16fb | 4760 | |
23a79c61 APB |
4761 | real_type = GET_REAL_TYPE (type); |
4762 | if (TREE_CODE (real_type) == RECORD_TYPE) | |
4763 | { | |
4764 | real_type = promote_type (real_type); | |
4765 | if (TREE_CODE (type) == TREE_LIST) | |
4766 | TREE_PURPOSE (type) = real_type; | |
4767 | } | |
5e942c50 | 4768 | |
e04a16fb AG |
4769 | /* Check redefinition */ |
4770 | for (already = arg_types; already; already = TREE_CHAIN (already)) | |
4771 | if (TREE_PURPOSE (already) == name) | |
4772 | { | |
781b0558 KG |
4773 | parse_error_context |
4774 | (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'", | |
4775 | IDENTIFIER_POINTER (name), | |
e04a16fb AG |
4776 | IDENTIFIER_POINTER (EXPR_WFL_NODE (id))); |
4777 | break; | |
4778 | } | |
4779 | ||
4780 | /* If we've an incomplete argument type, we know there is a location | |
4781 | to patch when the type get resolved, later. */ | |
4782 | jdep = NULL; | |
c583dd46 | 4783 | if (must_chain) |
e04a16fb | 4784 | { |
c583dd46 APB |
4785 | patch_stage = JDEP_METHOD; |
4786 | type = register_incomplete_type (patch_stage, | |
4787 | type_wfl, wfl_name, type); | |
4788 | jdep = CLASSD_LAST (ctxp->classd_list); | |
4789 | JDEP_MISC (jdep) = id; | |
e04a16fb | 4790 | } |
c583dd46 | 4791 | |
c2952b01 | 4792 | /* The argument node: a name and a (possibly) incomplete type. */ |
23a79c61 | 4793 | arg_node = build_tree_list (name, real_type); |
c2952b01 APB |
4794 | /* Remeber arguments declared final. */ |
4795 | ARG_FINAL_P (arg_node) = ARG_FINAL_P (current); | |
4796 | ||
e04a16fb AG |
4797 | if (jdep) |
4798 | JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node); | |
4799 | TREE_CHAIN (arg_node) = arg_types; | |
4800 | arg_types = arg_node; | |
4801 | } | |
de4c7b02 | 4802 | TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node); |
e04a16fb AG |
4803 | node = build_tree_list (id, meth); |
4804 | return node; | |
4805 | } | |
4806 | ||
4807 | static int | |
4808 | unresolved_type_p (wfl, returned) | |
4809 | tree wfl; | |
4810 | tree *returned; | |
4811 | ||
4812 | { | |
4813 | if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION) | |
4814 | { | |
e04a16fb | 4815 | if (returned) |
165f37bc APB |
4816 | { |
4817 | tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl)); | |
4818 | if (decl && current_class && (decl == TYPE_NAME (current_class))) | |
4819 | *returned = TREE_TYPE (decl); | |
4820 | else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl)) | |
4821 | *returned = TREE_TYPE (GET_CPC ()); | |
4822 | else | |
4823 | *returned = NULL_TREE; | |
4824 | } | |
e04a16fb AG |
4825 | return 1; |
4826 | } | |
4827 | if (returned) | |
4828 | *returned = wfl; | |
4829 | return 0; | |
4830 | } | |
4831 | ||
4832 | /* From NAME, build a qualified identifier node using the | |
4833 | qualification from the current package definition. */ | |
4834 | ||
4835 | static tree | |
98a52c2c | 4836 | parser_qualified_classname (name) |
e04a16fb AG |
4837 | tree name; |
4838 | { | |
c2952b01 APB |
4839 | tree nested_class_name; |
4840 | ||
98a52c2c | 4841 | if ((nested_class_name = maybe_make_nested_class_name (name))) |
c2952b01 APB |
4842 | return nested_class_name; |
4843 | ||
e04a16fb | 4844 | if (ctxp->package) |
c2952b01 | 4845 | return merge_qualified_name (ctxp->package, name); |
e04a16fb | 4846 | else |
c2952b01 | 4847 | return name; |
e04a16fb AG |
4848 | } |
4849 | ||
4850 | /* Called once the type a interface extends is resolved. Returns 0 if | |
4851 | everything is OK. */ | |
4852 | ||
4853 | static int | |
4854 | parser_check_super_interface (super_decl, this_decl, this_wfl) | |
4855 | tree super_decl, this_decl, this_wfl; | |
4856 | { | |
4857 | tree super_type = TREE_TYPE (super_decl); | |
4858 | ||
4859 | /* Has to be an interface */ | |
c2952b01 | 4860 | if (!CLASS_INTERFACE (super_decl)) |
e04a16fb AG |
4861 | { |
4862 | parse_error_context | |
4863 | (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'", | |
4864 | (TYPE_ARRAY_P (super_type) ? "array" : "class"), | |
4865 | IDENTIFIER_POINTER (DECL_NAME (super_decl)), | |
4866 | (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ? | |
4867 | "interface" : "class"), | |
4868 | IDENTIFIER_POINTER (DECL_NAME (this_decl))); | |
4869 | return 1; | |
4870 | } | |
4871 | ||
4872 | /* Check scope: same package OK, other package: OK if public */ | |
4873 | if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl))) | |
4874 | return 1; | |
4875 | ||
4876 | SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s", | |
4877 | IDENTIFIER_POINTER (DECL_NAME (this_decl)), | |
4878 | IDENTIFIER_POINTER (DECL_NAME (super_decl)))); | |
4879 | return 0; | |
4880 | } | |
4881 | ||
4882 | /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns | |
4883 | 0 if everthing is OK. */ | |
4884 | ||
4885 | static int | |
4886 | parser_check_super (super_decl, this_decl, wfl) | |
4887 | tree super_decl, this_decl, wfl; | |
4888 | { | |
e04a16fb AG |
4889 | tree super_type = TREE_TYPE (super_decl); |
4890 | ||
4891 | /* SUPER should be a CLASS (neither an array nor an interface) */ | |
4892 | if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type))) | |
4893 | { | |
4894 | parse_error_context | |
4895 | (wfl, "Class `%s' can't subclass %s `%s'", | |
4896 | IDENTIFIER_POINTER (DECL_NAME (this_decl)), | |
4897 | (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"), | |
4898 | IDENTIFIER_POINTER (DECL_NAME (super_decl))); | |
4899 | return 1; | |
4900 | } | |
4901 | ||
4902 | if (CLASS_FINAL (TYPE_NAME (super_type))) | |
4903 | { | |
4904 | parse_error_context (wfl, "Can't subclass final classes: %s", | |
4905 | IDENTIFIER_POINTER (DECL_NAME (super_decl))); | |
4906 | return 1; | |
4907 | } | |
4908 | ||
4909 | /* Check scope: same package OK, other package: OK if public */ | |
4910 | if (check_pkg_class_access (DECL_NAME (super_decl), wfl)) | |
4911 | return 1; | |
4912 | ||
4913 | SOURCE_FRONTEND_DEBUG (("Completing class %s with %s", | |
4914 | IDENTIFIER_POINTER (DECL_NAME (this_decl)), | |
4915 | IDENTIFIER_POINTER (DECL_NAME (super_decl)))); | |
4916 | return 0; | |
4917 | } | |
4918 | ||
4919 | /* Create a new dependency list and link it (in a LIFO manner) to the | |
4920 | CTXP list of type dependency list. */ | |
4921 | ||
4922 | static void | |
4923 | create_jdep_list (ctxp) | |
4924 | struct parser_ctxt *ctxp; | |
4925 | { | |
23a79c61 | 4926 | jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist)); |
e04a16fb AG |
4927 | new->first = new->last = NULL; |
4928 | new->next = ctxp->classd_list; | |
4929 | ctxp->classd_list = new; | |
4930 | } | |
4931 | ||
4932 | static jdeplist * | |
4933 | reverse_jdep_list (ctxp) | |
4934 | struct parser_ctxt *ctxp; | |
4935 | { | |
4936 | register jdeplist *prev = NULL, *current, *next; | |
4937 | for (current = ctxp->classd_list; current; current = next) | |
4938 | { | |
4939 | next = current->next; | |
4940 | current->next = prev; | |
4941 | prev = current; | |
4942 | } | |
4943 | return prev; | |
4944 | } | |
4945 | ||
23a79c61 APB |
4946 | /* Create a fake pointer based on the ID stored in |
4947 | TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be | |
4948 | registered again. */ | |
e04a16fb AG |
4949 | |
4950 | static tree | |
23a79c61 APB |
4951 | obtain_incomplete_type (type_name) |
4952 | tree type_name; | |
e04a16fb | 4953 | { |
23a79c61 APB |
4954 | tree ptr, name; |
4955 | ||
4956 | if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION) | |
4957 | name = EXPR_WFL_NODE (type_name); | |
4958 | else if (INCOMPLETE_TYPE_P (type_name)) | |
4959 | name = TYPE_NAME (type_name); | |
4960 | else | |
4961 | fatal ("invalid type name - obtain_incomplete_type"); | |
e04a16fb AG |
4962 | |
4963 | for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr)) | |
78d21f92 | 4964 | if (TYPE_NAME (ptr) == name) |
e04a16fb AG |
4965 | break; |
4966 | ||
4967 | if (!ptr) | |
4968 | { | |
78d21f92 PB |
4969 | BUILD_PTR_FROM_NAME (ptr, name); |
4970 | layout_type (ptr); | |
e04a16fb AG |
4971 | TREE_CHAIN (ptr) = ctxp->incomplete_class; |
4972 | ctxp->incomplete_class = ptr; | |
4973 | } | |
4974 | ||
4975 | return ptr; | |
4976 | } | |
4977 | ||
4978 | /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is | |
4979 | non NULL instead of computing a new fake type based on WFL. The new | |
4980 | dependency is inserted in the current type dependency list, in FIFO | |
4981 | manner. */ | |
4982 | ||
4983 | static tree | |
4984 | register_incomplete_type (kind, wfl, decl, ptr) | |
4985 | int kind; | |
4986 | tree wfl, decl, ptr; | |
4987 | { | |
23a79c61 | 4988 | jdep *new = (jdep *)xmalloc (sizeof (jdep)); |
e04a16fb | 4989 | |
e04a16fb AG |
4990 | if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */ |
4991 | ptr = obtain_incomplete_type (wfl); | |
4992 | ||
4993 | JDEP_KIND (new) = kind; | |
4994 | JDEP_DECL (new) = decl; | |
4995 | JDEP_SOLV (new) = ptr; | |
4996 | JDEP_WFL (new) = wfl; | |
4997 | JDEP_CHAIN (new) = NULL; | |
4998 | JDEP_MISC (new) = NULL_TREE; | |
e803d3b2 APB |
4999 | /* For some dependencies, set the enclosing class of the current |
5000 | class to be the enclosing context */ | |
5001 | if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS) | |
165f37bc APB |
5002 | && GET_ENCLOSING_CPC ()) |
5003 | JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ()); | |
5004 | else | |
324ed8fd | 5005 | JDEP_ENCLOSING (new) = GET_CPC (); |
e04a16fb AG |
5006 | JDEP_GET_PATCH (new) = (tree *)NULL; |
5007 | ||
5008 | JDEP_INSERT (ctxp->classd_list, new); | |
5009 | ||
5010 | return ptr; | |
5011 | } | |
5012 | ||
5013 | void | |
5014 | java_check_circular_reference () | |
5015 | { | |
5016 | tree current; | |
5017 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) | |
5018 | { | |
5019 | tree type = TREE_TYPE (current); | |
e920ebc9 | 5020 | if (CLASS_INTERFACE (current)) |
e04a16fb AG |
5021 | { |
5022 | /* Check all interfaces this class extends */ | |
5023 | tree basetype_vec = TYPE_BINFO_BASETYPES (type); | |
5024 | int n, i; | |
5025 | ||
5026 | if (!basetype_vec) | |
5027 | return; | |
5028 | n = TREE_VEC_LENGTH (basetype_vec); | |
5029 | for (i = 0; i < n; i++) | |
5030 | { | |
5031 | tree vec_elt = TREE_VEC_ELT (basetype_vec, i); | |
5032 | if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node | |
5033 | && interface_of_p (type, BINFO_TYPE (vec_elt))) | |
5034 | parse_error_context (lookup_cl (current), | |
5035 | "Cyclic interface inheritance"); | |
5036 | } | |
5037 | } | |
5038 | else | |
5039 | if (inherits_from_p (CLASSTYPE_SUPER (type), type)) | |
5040 | parse_error_context (lookup_cl (current), | |
c2952b01 APB |
5041 | "Cyclic class inheritance%s", |
5042 | (cyclic_inheritance_report ? | |
5043 | cyclic_inheritance_report : "")); | |
5044 | } | |
5045 | } | |
5046 | ||
5047 | /* Augment the parameter list PARM with parameters crafted to | |
5048 | initialize outer context locals aliases. Through ARTIFICIAL, a | |
5049 | count is kept of the number of crafted parameters. MODE governs | |
5050 | what eventually gets created: something suitable for a function | |
5051 | creation or a function invocation, either the constructor or | |
c00f0fb2 | 5052 | finit$. */ |
c2952b01 APB |
5053 | |
5054 | static tree | |
5055 | build_alias_initializer_parameter_list (mode, class_type, parm, artificial) | |
5056 | int mode; | |
5057 | tree class_type, parm; | |
5058 | int *artificial; | |
5059 | { | |
5060 | tree field; | |
da632f2c APB |
5061 | tree additional_parms = NULL_TREE; |
5062 | ||
c2952b01 APB |
5063 | for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field)) |
5064 | if (FIELD_LOCAL_ALIAS (field)) | |
5065 | { | |
63ad61ed | 5066 | const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field)); |
c2952b01 | 5067 | tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE; |
1f8f4a0b | 5068 | tree mangled_id; |
c2952b01 APB |
5069 | |
5070 | switch (mode) | |
5071 | { | |
5072 | case AIPL_FUNCTION_DECLARATION: | |
1f8f4a0b MM |
5073 | MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id, |
5074 | &buffer [4]); | |
5075 | purpose = build_wfl_node (mangled_id); | |
c2952b01 APB |
5076 | if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE) |
5077 | value = build_wfl_node (TYPE_NAME (TREE_TYPE (field))); | |
5078 | else | |
5079 | value = TREE_TYPE (field); | |
5080 | break; | |
5081 | ||
5082 | case AIPL_FUNCTION_CREATION: | |
1f8f4a0b MM |
5083 | MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose, |
5084 | &buffer [4]); | |
c2952b01 APB |
5085 | value = TREE_TYPE (field); |
5086 | break; | |
5087 | ||
5088 | case AIPL_FUNCTION_FINIT_INVOCATION: | |
1f8f4a0b MM |
5089 | MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id, |
5090 | &buffer [4]); | |
c2952b01 APB |
5091 | /* Now, this is wrong. purpose should always be the NAME |
5092 | of something and value its matching value (decl, type, | |
5093 | etc...) FIXME -- but there is a lot to fix. */ | |
5094 | ||
5095 | /* When invoked for this kind of operation, we already | |
5096 | know whether a field is used or not. */ | |
5097 | purpose = TREE_TYPE (field); | |
1f8f4a0b | 5098 | value = build_wfl_node (mangled_id); |
c2952b01 APB |
5099 | break; |
5100 | ||
5101 | case AIPL_FUNCTION_CTOR_INVOCATION: | |
5102 | /* There are two case: the constructor invokation happends | |
5103 | outside the local inner, in which case, locales from the outer | |
5104 | context are directly used. | |
5105 | ||
5106 | Otherwise, we fold to using the alias directly. */ | |
5107 | if (class_type == current_class) | |
5108 | value = field; | |
5109 | else | |
5110 | { | |
5111 | name = get_identifier (&buffer[4]); | |
5112 | value = IDENTIFIER_LOCAL_VALUE (name); | |
5113 | } | |
5114 | break; | |
5115 | } | |
da632f2c | 5116 | additional_parms = tree_cons (purpose, value, additional_parms); |
c2952b01 APB |
5117 | if (artificial) |
5118 | *artificial +=1; | |
5119 | } | |
da632f2c APB |
5120 | if (additional_parms) |
5121 | { | |
5122 | if (ANONYMOUS_CLASS_P (class_type) | |
5123 | && mode == AIPL_FUNCTION_CTOR_INVOCATION) | |
5124 | additional_parms = nreverse (additional_parms); | |
5125 | parm = chainon (additional_parms, parm); | |
5126 | } | |
5127 | ||
5128 | return parm; | |
c2952b01 APB |
5129 | } |
5130 | ||
5131 | /* Craft a constructor for CLASS_DECL -- what we should do when none | |
5132 | where found. ARGS is non NULL when a special signature must be | |
5133 | enforced. This is the case for anonymous classes. */ | |
5134 | ||
5135 | static void | |
5136 | craft_constructor (class_decl, args) | |
5137 | tree class_decl, args; | |
5138 | { | |
5139 | tree class_type = TREE_TYPE (class_decl); | |
5140 | tree parm = NULL_TREE; | |
5141 | int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ? | |
5142 | ACC_PUBLIC : 0); | |
5143 | int i = 0, artificial = 0; | |
5144 | tree decl, ctor_name; | |
5145 | char buffer [80]; | |
5146 | ||
c2952b01 APB |
5147 | /* The constructor name is <init> unless we're dealing with an |
5148 | anonymous class, in which case the name will be fixed after having | |
5149 | be expanded. */ | |
5150 | if (ANONYMOUS_CLASS_P (class_type)) | |
5151 | ctor_name = DECL_NAME (class_decl); | |
5152 | else | |
5153 | ctor_name = init_identifier_node; | |
5154 | ||
5155 | /* If we're dealing with an inner class constructor, we hide the | |
5156 | this$<n> decl in the name field of its parameter declaration. */ | |
5157 | if (PURE_INNER_CLASS_TYPE_P (class_type)) | |
5158 | { | |
5159 | tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type))); | |
5160 | parm = tree_cons (build_current_thisn (class_type), | |
5161 | build_pointer_type (type), parm); | |
5162 | ||
5163 | /* Some more arguments to be hidden here. The values of the local | |
5164 | variables of the outer context that the inner class needs to see. */ | |
5165 | parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION, | |
5166 | class_type, parm, | |
5167 | &artificial); | |
5168 | } | |
5169 | ||
5170 | /* Then if there are any args to be enforced, enforce them now */ | |
5171 | for (; args && args != end_params_node; args = TREE_CHAIN (args)) | |
5172 | { | |
5173 | sprintf (buffer, "parm%d", i++); | |
5174 | parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm); | |
e04a16fb | 5175 | } |
c2952b01 APB |
5176 | |
5177 | CRAFTED_PARAM_LIST_FIXUP (parm); | |
5178 | decl = create_artificial_method (class_type, flags, void_type_node, | |
5179 | ctor_name, parm); | |
5180 | fix_method_argument_names (parm, decl); | |
5181 | /* Now, mark the artificial parameters. */ | |
5182 | DECL_FUNCTION_NAP (decl) = artificial; | |
c2952b01 | 5183 | DECL_CONSTRUCTOR_P (decl) = 1; |
e04a16fb AG |
5184 | } |
5185 | ||
c2952b01 | 5186 | |
e920ebc9 APB |
5187 | /* Fix the constructors. This will be called right after circular |
5188 | references have been checked. It is necessary to fix constructors | |
5189 | early even if no code generation will take place for that class: | |
5190 | some generated constructor might be required by the class whose | |
5191 | compilation triggered this one to be simply loaded. */ | |
5192 | ||
5193 | void | |
5194 | java_fix_constructors () | |
5195 | { | |
5196 | tree current; | |
5197 | ||
5198 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) | |
5199 | { | |
e920ebc9 APB |
5200 | tree class_type = TREE_TYPE (current); |
5201 | int saw_ctor = 0; | |
c2952b01 APB |
5202 | tree decl; |
5203 | ||
5204 | if (CLASS_INTERFACE (TYPE_NAME (class_type))) | |
5205 | continue; | |
e920ebc9 APB |
5206 | |
5207 | for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl)) | |
5208 | { | |
5209 | if (DECL_CONSTRUCTOR_P (decl)) | |
5210 | { | |
5211 | fix_constructors (decl); | |
5212 | saw_ctor = 1; | |
5213 | } | |
5214 | } | |
5215 | ||
c2952b01 APB |
5216 | /* Anonymous class constructor can't be generated that early. */ |
5217 | if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type)) | |
5218 | craft_constructor (current, NULL_TREE); | |
e920ebc9 APB |
5219 | } |
5220 | } | |
5221 | ||
23a79c61 APB |
5222 | /* safe_layout_class just makes sure that we can load a class without |
5223 | disrupting the current_class, input_file, lineno, etc, information | |
5224 | about the class processed currently. */ | |
5225 | ||
e04a16fb AG |
5226 | void |
5227 | safe_layout_class (class) | |
5228 | tree class; | |
5229 | { | |
5230 | tree save_current_class = current_class; | |
3b304f5b | 5231 | const char *save_input_filename = input_filename; |
e04a16fb | 5232 | int save_lineno = lineno; |
5e942c50 | 5233 | |
e04a16fb | 5234 | layout_class (class); |
5e942c50 | 5235 | |
e04a16fb AG |
5236 | current_class = save_current_class; |
5237 | input_filename = save_input_filename; | |
5238 | lineno = save_lineno; | |
5239 | CLASS_LOADED_P (class) = 1; | |
5240 | } | |
5241 | ||
5242 | static tree | |
5243 | jdep_resolve_class (dep) | |
5244 | jdep *dep; | |
5245 | { | |
5246 | tree decl; | |
5247 | ||
23a79c61 APB |
5248 | if (JDEP_RESOLVED_P (dep)) |
5249 | decl = JDEP_RESOLVED_DECL (dep); | |
5250 | else | |
e04a16fb | 5251 | { |
c2952b01 | 5252 | decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep), |
23a79c61 | 5253 | JDEP_DECL (dep), JDEP_WFL (dep)); |
e04a16fb AG |
5254 | JDEP_RESOLVED (dep, decl); |
5255 | } | |
23a79c61 | 5256 | |
e04a16fb | 5257 | if (!decl) |
23a79c61 | 5258 | complete_class_report_errors (dep); |
1e12ab9b | 5259 | else if (PURE_INNER_CLASS_DECL_P (decl)) |
4dbf4496 | 5260 | check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep)); |
e04a16fb AG |
5261 | return decl; |
5262 | } | |
5263 | ||
5264 | /* Complete unsatisfied class declaration and their dependencies */ | |
5265 | ||
5266 | void | |
5267 | java_complete_class () | |
5268 | { | |
e04a16fb AG |
5269 | tree cclass; |
5270 | jdeplist *cclassd; | |
5271 | int error_found; | |
b67d701b | 5272 | tree type; |
e04a16fb | 5273 | |
9a7ab4b3 | 5274 | /* Process imports */ |
e04a16fb | 5275 | process_imports (); |
e04a16fb AG |
5276 | |
5277 | /* Rever things so we have the right order */ | |
5278 | ctxp->class_list = nreverse (ctxp->class_list); | |
5279 | ctxp->classd_list = reverse_jdep_list (ctxp); | |
c877974e | 5280 | |
e04a16fb AG |
5281 | for (cclassd = ctxp->classd_list, cclass = ctxp->class_list; |
5282 | cclass && cclassd; | |
5283 | cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd)) | |
5284 | { | |
5285 | jdep *dep; | |
5286 | for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep)) | |
5287 | { | |
5288 | tree decl; | |
e04a16fb AG |
5289 | if (!(decl = jdep_resolve_class (dep))) |
5290 | continue; | |
5291 | ||
5292 | /* Now it's time to patch */ | |
5293 | switch (JDEP_KIND (dep)) | |
5294 | { | |
5295 | case JDEP_SUPER: | |
5296 | /* Simply patch super */ | |
5297 | if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep))) | |
5298 | continue; | |
5299 | BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO | |
5300 | (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl); | |
5301 | break; | |
5302 | ||
5303 | case JDEP_FIELD: | |
5304 | { | |
5305 | /* We do part of the job done in add_field */ | |
5306 | tree field_decl = JDEP_DECL (dep); | |
5307 | tree field_type = TREE_TYPE (decl); | |
e04a16fb | 5308 | if (TREE_CODE (field_type) == RECORD_TYPE) |
e04a16fb | 5309 | field_type = promote_type (field_type); |
e04a16fb | 5310 | TREE_TYPE (field_decl) = field_type; |
5e942c50 | 5311 | DECL_ALIGN (field_decl) = 0; |
11cf4d18 | 5312 | DECL_USER_ALIGN (field_decl) = 0; |
5e942c50 | 5313 | layout_decl (field_decl, 0); |
e04a16fb AG |
5314 | SOURCE_FRONTEND_DEBUG |
5315 | (("Completed field/var decl `%s' with `%s'", | |
5316 | IDENTIFIER_POINTER (DECL_NAME (field_decl)), | |
5317 | IDENTIFIER_POINTER (DECL_NAME (decl)))); | |
5318 | break; | |
5319 | } | |
5320 | case JDEP_METHOD: /* We start patching a method */ | |
5321 | case JDEP_METHOD_RETURN: | |
5322 | error_found = 0; | |
5323 | while (1) | |
5324 | { | |
5325 | if (decl) | |
5326 | { | |
b67d701b PB |
5327 | type = TREE_TYPE(decl); |
5328 | if (TREE_CODE (type) == RECORD_TYPE) | |
5329 | type = promote_type (type); | |
e04a16fb AG |
5330 | JDEP_APPLY_PATCH (dep, type); |
5331 | SOURCE_FRONTEND_DEBUG | |
5332 | (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ? | |
5333 | "Completing fct `%s' with ret type `%s'": | |
5334 | "Completing arg `%s' with type `%s'"), | |
5335 | IDENTIFIER_POINTER (EXPR_WFL_NODE | |
5336 | (JDEP_DECL_WFL (dep))), | |
5337 | IDENTIFIER_POINTER (DECL_NAME (decl)))); | |
5338 | } | |
5339 | else | |
5340 | error_found = 1; | |
5341 | dep = JDEP_CHAIN (dep); | |
5342 | if (JDEP_KIND (dep) == JDEP_METHOD_END) | |
5343 | break; | |
5344 | else | |
5345 | decl = jdep_resolve_class (dep); | |
5346 | } | |
5347 | if (!error_found) | |
5348 | { | |
5349 | tree mdecl = JDEP_DECL (dep), signature; | |
165f37bc APB |
5350 | /* Recompute and reset the signature, check first that |
5351 | all types are now defined. If they're not, | |
5352 | dont build the signature. */ | |
5353 | if (check_method_types_complete (mdecl)) | |
5354 | { | |
5355 | signature = build_java_signature (TREE_TYPE (mdecl)); | |
5356 | set_java_signature (TREE_TYPE (mdecl), signature); | |
5357 | } | |
e04a16fb AG |
5358 | } |
5359 | else | |
5360 | continue; | |
5361 | break; | |
5362 | ||
5363 | case JDEP_INTERFACE: | |
5364 | if (parser_check_super_interface (decl, JDEP_DECL (dep), | |
5365 | JDEP_WFL (dep))) | |
5366 | continue; | |
5367 | parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep)); | |
5368 | break; | |
5369 | ||
b67d701b | 5370 | case JDEP_PARM: |
e04a16fb | 5371 | case JDEP_VARIABLE: |
b67d701b PB |
5372 | type = TREE_TYPE(decl); |
5373 | if (TREE_CODE (type) == RECORD_TYPE) | |
5374 | type = promote_type (type); | |
5375 | JDEP_APPLY_PATCH (dep, type); | |
e04a16fb AG |
5376 | break; |
5377 | ||
5378 | case JDEP_TYPE: | |
5379 | JDEP_APPLY_PATCH (dep, TREE_TYPE (decl)); | |
5380 | SOURCE_FRONTEND_DEBUG | |
5381 | (("Completing a random type dependency on a '%s' node", | |
5382 | tree_code_name [TREE_CODE (JDEP_DECL (dep))])); | |
5383 | break; | |
5384 | ||
b9f7e36c | 5385 | case JDEP_EXCEPTION: |
c877974e APB |
5386 | JDEP_APPLY_PATCH (dep, TREE_TYPE (decl)); |
5387 | SOURCE_FRONTEND_DEBUG | |
5388 | (("Completing `%s' `throws' argument node", | |
5389 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))))); | |
b9f7e36c APB |
5390 | break; |
5391 | ||
c2952b01 APB |
5392 | case JDEP_ANONYMOUS: |
5393 | patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep)); | |
5394 | break; | |
5395 | ||
e04a16fb | 5396 | default: |
0a2138e2 APB |
5397 | fatal ("Can't handle patch code %d - java_complete_class", |
5398 | JDEP_KIND (dep)); | |
e04a16fb AG |
5399 | } |
5400 | } | |
5401 | } | |
e04a16fb AG |
5402 | return; |
5403 | } | |
5404 | ||
5405 | /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an | |
5406 | array. */ | |
5407 | ||
5408 | static tree | |
c2952b01 APB |
5409 | resolve_class (enclosing, class_type, decl, cl) |
5410 | tree enclosing, class_type, decl, cl; | |
e04a16fb | 5411 | { |
49f48c71 KG |
5412 | const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type)); |
5413 | const char *base = name; | |
78d21f92 PB |
5414 | tree resolved_type = TREE_TYPE (class_type); |
5415 | tree resolved_type_decl; | |
e04a16fb | 5416 | |
78d21f92 PB |
5417 | if (resolved_type != NULL_TREE) |
5418 | { | |
5419 | tree resolved_type_decl = TYPE_NAME (resolved_type); | |
5420 | if (resolved_type_decl == NULL_TREE | |
5421 | || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE) | |
5422 | { | |
5423 | resolved_type_decl = build_decl (TYPE_DECL, | |
5424 | TYPE_NAME (class_type), | |
5425 | resolved_type); | |
5426 | } | |
5427 | return resolved_type_decl; | |
5428 | } | |
5429 | ||
e04a16fb AG |
5430 | /* 1- Check to see if we have an array. If true, find what we really |
5431 | want to resolve */ | |
5432 | while (name[0] == '[') | |
5433 | name++; | |
5434 | if (base != name) | |
34d4df06 APB |
5435 | { |
5436 | TYPE_NAME (class_type) = get_identifier (name); | |
5437 | WFL_STRIP_BRACKET (cl, cl); | |
5438 | } | |
e04a16fb AG |
5439 | |
5440 | /* 2- Resolve the bare type */ | |
c2952b01 APB |
5441 | if (!(resolved_type_decl = do_resolve_class (enclosing, class_type, |
5442 | decl, cl))) | |
e04a16fb AG |
5443 | return NULL_TREE; |
5444 | resolved_type = TREE_TYPE (resolved_type_decl); | |
5445 | ||
5446 | /* 3- If we have and array, reconstruct the array down to its nesting */ | |
5447 | if (base != name) | |
5448 | { | |
5449 | while (base != name) | |
5450 | { | |
5451 | if (TREE_CODE (resolved_type) == RECORD_TYPE) | |
5452 | resolved_type = promote_type (resolved_type); | |
5453 | resolved_type = build_java_array_type (resolved_type, -1); | |
c583dd46 | 5454 | CLASS_LOADED_P (resolved_type) = 1; |
e04a16fb AG |
5455 | name--; |
5456 | } | |
5457 | /* Build a fake decl for this, since this is what is expected to | |
5458 | be returned. */ | |
5459 | resolved_type_decl = | |
5460 | build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type); | |
5461 | /* Figure how those two things are important for error report. FIXME */ | |
5462 | DECL_SOURCE_LINE (resolved_type_decl) = 0; | |
5463 | DECL_SOURCE_FILE (resolved_type_decl) = input_filename; | |
78d21f92 | 5464 | TYPE_NAME (class_type) = TYPE_NAME (resolved_type); |
e04a16fb | 5465 | } |
78d21f92 | 5466 | TREE_TYPE (class_type) = resolved_type; |
e04a16fb AG |
5467 | return resolved_type_decl; |
5468 | } | |
5469 | ||
5470 | /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL | |
5471 | are used to report error messages. */ | |
5472 | ||
78d21f92 | 5473 | tree |
c2952b01 APB |
5474 | do_resolve_class (enclosing, class_type, decl, cl) |
5475 | tree enclosing, class_type, decl, cl; | |
e04a16fb AG |
5476 | { |
5477 | tree new_class_decl; | |
e04a16fb AG |
5478 | |
5479 | /* Do not try to replace TYPE_NAME (class_type) by a variable, since | |
9a7ab4b3 APB |
5480 | it is changed by find_in_imports{_on_demand} and (but it doesn't |
5481 | really matter) qualify_and_find */ | |
e04a16fb | 5482 | |
c2952b01 APB |
5483 | /* 0- Search in the current class as an inner class */ |
5484 | ||
5485 | /* Maybe some code here should be added to load the class or | |
5486 | something, at least if the class isn't an inner class and ended | |
5487 | being loaded from class file. FIXME. */ | |
a40d21da APB |
5488 | while (enclosing) |
5489 | { | |
5490 | tree name; | |
5491 | ||
5492 | if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl))) | |
5493 | return new_class_decl; | |
5494 | ||
0c2b8145 APB |
5495 | /* Explore enclosing contexts. */ |
5496 | while (INNER_CLASS_DECL_P (enclosing)) | |
5497 | { | |
5498 | enclosing = DECL_CONTEXT (enclosing); | |
5499 | if ((new_class_decl = find_as_inner_class (enclosing, | |
5500 | class_type, cl))) | |
5501 | return new_class_decl; | |
5502 | } | |
5503 | ||
a40d21da APB |
5504 | /* Now go to the upper classes, bail out if necessary. */ |
5505 | enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing)); | |
5506 | if (!enclosing || enclosing == object_type_node) | |
5507 | break; | |
5508 | ||
5509 | if (TREE_CODE (enclosing) == RECORD_TYPE) | |
5510 | { | |
5511 | enclosing = TYPE_NAME (enclosing); | |
5512 | continue; | |
5513 | } | |
5514 | ||
5515 | if (TREE_CODE (enclosing) == IDENTIFIER_NODE) | |
0c2b8145 | 5516 | BUILD_PTR_FROM_NAME (name, enclosing); |
a40d21da APB |
5517 | else |
5518 | name = enclosing; | |
5519 | enclosing = do_resolve_class (NULL, name, NULL, NULL); | |
5520 | } | |
c2952b01 | 5521 | |
9a7ab4b3 APB |
5522 | /* 1- Check for the type in single imports. This will change |
5523 | TYPE_NAME() if something relevant is found */ | |
5524 | find_in_imports (class_type); | |
e04a16fb | 5525 | |
9a7ab4b3 | 5526 | /* 2- And check for the type in the current compilation unit */ |
e04a16fb AG |
5527 | if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)))) |
5528 | { | |
5529 | if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) && | |
5530 | !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl))) | |
5531 | load_class (TYPE_NAME (class_type), 0); | |
5532 | return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
5533 | } | |
5534 | ||
9a7ab4b3 APB |
5535 | /* 3- Search according to the current package definition */ |
5536 | if (!QUALIFIED_P (TYPE_NAME (class_type))) | |
5537 | { | |
5538 | if ((new_class_decl = qualify_and_find (class_type, ctxp->package, | |
5539 | TYPE_NAME (class_type)))) | |
5540 | return new_class_decl; | |
5541 | } | |
5542 | ||
5543 | /* 4- Check the import on demands. Don't allow bar.baz to be | |
5544 | imported from foo.* */ | |
5545 | if (!QUALIFIED_P (TYPE_NAME (class_type))) | |
5546 | if (find_in_imports_on_demand (class_type)) | |
5547 | return NULL_TREE; | |
5548 | ||
5549 | /* If found in find_in_imports_on_demant, the type has already been | |
5550 | loaded. */ | |
5551 | if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)))) | |
5552 | return new_class_decl; | |
5553 | ||
5554 | /* 5- Try with a name qualified with the package name we've seen so far */ | |
ee07f4f4 | 5555 | if (!QUALIFIED_P (TYPE_NAME (class_type))) |
bc3ca41b | 5556 | { |
ee07f4f4 | 5557 | tree package; |
d6baf6f5 APB |
5558 | |
5559 | /* If there is a current package (ctxp->package), it's the first | |
5560 | element of package_list and we can skip it. */ | |
5561 | for (package = (ctxp->package ? | |
5562 | TREE_CHAIN (package_list) : package_list); | |
5563 | package; package = TREE_CHAIN (package)) | |
9a7ab4b3 APB |
5564 | if ((new_class_decl = qualify_and_find (class_type, |
5565 | TREE_PURPOSE (package), | |
5566 | TYPE_NAME (class_type)))) | |
5567 | return new_class_decl; | |
5568 | } | |
5569 | ||
5570 | /* 5- Check an other compilation unit that bears the name of type */ | |
e04a16fb AG |
5571 | load_class (TYPE_NAME (class_type), 0); |
5572 | if (check_pkg_class_access (TYPE_NAME (class_type), | |
5573 | (cl ? cl : lookup_cl (decl)))) | |
5574 | return NULL_TREE; | |
5575 | ||
9a7ab4b3 | 5576 | /* 6- Last call for a resolution */ |
e04a16fb AG |
5577 | return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); |
5578 | } | |
5579 | ||
9a7ab4b3 APB |
5580 | static tree |
5581 | qualify_and_find (class_type, package, name) | |
5582 | tree class_type, package, name; | |
5583 | { | |
5584 | tree new_qualified = merge_qualified_name (package, name); | |
5585 | tree new_class_decl; | |
5586 | ||
5587 | if (!IDENTIFIER_CLASS_VALUE (new_qualified)) | |
5588 | load_class (new_qualified, 0); | |
5589 | if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified))) | |
5590 | { | |
5591 | if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) && | |
5592 | !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl))) | |
5593 | load_class (new_qualified, 0); | |
5594 | TYPE_NAME (class_type) = new_qualified; | |
5595 | return IDENTIFIER_CLASS_VALUE (new_qualified); | |
5596 | } | |
5597 | return NULL_TREE; | |
5598 | } | |
5599 | ||
e04a16fb | 5600 | /* Resolve NAME and lay it out (if not done and if not the current |
23a79c61 APB |
5601 | parsed class). Return a decl node. This function is meant to be |
5602 | called when type resolution is necessary during the walk pass. */ | |
e04a16fb AG |
5603 | |
5604 | static tree | |
c877974e APB |
5605 | resolve_and_layout (something, cl) |
5606 | tree something; | |
e04a16fb AG |
5607 | tree cl; |
5608 | { | |
34d4df06 | 5609 | tree decl, decl_type; |
c877974e | 5610 | |
23a79c61 APB |
5611 | /* Don't do that on the current class */ |
5612 | if (something == current_class) | |
5613 | return TYPE_NAME (current_class); | |
c877974e | 5614 | |
23a79c61 | 5615 | /* Don't do anything for void and other primitive types */ |
c877974e APB |
5616 | if (JPRIMITIVE_TYPE_P (something) || something == void_type_node) |
5617 | return NULL_TREE; | |
5618 | ||
23a79c61 APB |
5619 | /* Pointer types can be reall pointer types or fake pointers. When |
5620 | finding a real pointer, recheck for primitive types */ | |
5621 | if (TREE_CODE (something) == POINTER_TYPE) | |
5622 | { | |
5623 | if (TREE_TYPE (something)) | |
5624 | { | |
5625 | something = TREE_TYPE (something); | |
5626 | if (JPRIMITIVE_TYPE_P (something) || something == void_type_node) | |
5627 | return NULL_TREE; | |
5628 | } | |
5629 | else | |
5630 | something = TYPE_NAME (something); | |
5631 | } | |
5632 | ||
5633 | /* Don't do anything for arrays of primitive types */ | |
5634 | if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something) | |
5635 | && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something))) | |
5636 | return NULL_TREE; | |
5637 | ||
c2952b01 APB |
5638 | /* Something might be a WFL */ |
5639 | if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION) | |
5640 | something = EXPR_WFL_NODE (something); | |
5641 | ||
5642 | /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a | |
5643 | TYPE_DECL or a real TYPE */ | |
5644 | else if (TREE_CODE (something) != IDENTIFIER_NODE) | |
c877974e APB |
5645 | something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ? |
5646 | DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something)); | |
5647 | ||
23a79c61 APB |
5648 | if (!(decl = resolve_no_layout (something, cl))) |
5649 | return NULL_TREE; | |
5650 | ||
5651 | /* Resolve and layout if necessary */ | |
34d4df06 APB |
5652 | decl_type = TREE_TYPE (decl); |
5653 | layout_class_methods (decl_type); | |
5654 | /* Check methods */ | |
5655 | if (CLASS_FROM_SOURCE_P (decl_type)) | |
5656 | java_check_methods (decl); | |
5657 | /* Layout the type if necessary */ | |
5658 | if (decl_type != current_class && !CLASS_LOADED_P (decl_type)) | |
5659 | safe_layout_class (decl_type); | |
23a79c61 | 5660 | |
e04a16fb AG |
5661 | return decl; |
5662 | } | |
5663 | ||
5664 | /* Resolve a class, returns its decl but doesn't perform any | |
5665 | layout. The current parsing context is saved and restored */ | |
5666 | ||
5667 | static tree | |
5668 | resolve_no_layout (name, cl) | |
5669 | tree name, cl; | |
5670 | { | |
5671 | tree ptr, decl; | |
5672 | BUILD_PTR_FROM_NAME (ptr, name); | |
5673 | java_parser_context_save_global (); | |
c2952b01 | 5674 | decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl); |
e04a16fb AG |
5675 | java_parser_context_restore_global (); |
5676 | ||
5677 | return decl; | |
5678 | } | |
5679 | ||
23a79c61 APB |
5680 | /* Called when reporting errors. Skip leader '[' in a complex array |
5681 | type description that failed to be resolved. */ | |
e04a16fb | 5682 | |
49f48c71 | 5683 | static const char * |
e04a16fb | 5684 | purify_type_name (name) |
49f48c71 | 5685 | const char *name; |
e04a16fb AG |
5686 | { |
5687 | while (*name && *name == '[') | |
5688 | name++; | |
5689 | return name; | |
5690 | } | |
5691 | ||
5692 | /* The type CURRENT refers to can't be found. We print error messages. */ | |
5693 | ||
5694 | static void | |
5695 | complete_class_report_errors (dep) | |
5696 | jdep *dep; | |
5697 | { | |
49f48c71 | 5698 | const char *name; |
23a79c61 APB |
5699 | |
5700 | if (!JDEP_WFL (dep)) | |
5701 | return; | |
5702 | ||
5703 | name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))); | |
e04a16fb AG |
5704 | switch (JDEP_KIND (dep)) |
5705 | { | |
5706 | case JDEP_SUPER: | |
5707 | parse_error_context | |
5708 | (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found", | |
23a79c61 | 5709 | purify_type_name (name), |
e04a16fb AG |
5710 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); |
5711 | break; | |
5712 | case JDEP_FIELD: | |
5713 | parse_error_context | |
5714 | (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'", | |
23a79c61 | 5715 | purify_type_name (name), |
e04a16fb AG |
5716 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); |
5717 | break; | |
5718 | case JDEP_METHOD: /* Covers arguments */ | |
5719 | parse_error_context | |
781b0558 | 5720 | (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'", |
23a79c61 | 5721 | purify_type_name (name), |
e04a16fb AG |
5722 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))), |
5723 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep)))); | |
5724 | break; | |
5725 | case JDEP_METHOD_RETURN: /* Covers return type */ | |
5726 | parse_error_context | |
781b0558 | 5727 | (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'", |
23a79c61 | 5728 | purify_type_name (name), |
e04a16fb AG |
5729 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep)))); |
5730 | break; | |
5731 | case JDEP_INTERFACE: | |
5732 | parse_error_context | |
5733 | (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found", | |
5734 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))), | |
5735 | (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")), | |
5736 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); | |
5737 | break; | |
5738 | case JDEP_VARIABLE: | |
5739 | parse_error_context | |
781b0558 | 5740 | (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'", |
b67d701b PB |
5741 | purify_type_name (IDENTIFIER_POINTER |
5742 | (EXPR_WFL_NODE (JDEP_WFL (dep)))), | |
e04a16fb AG |
5743 | IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep)))); |
5744 | break; | |
b9f7e36c APB |
5745 | case JDEP_EXCEPTION: /* As specified by `throws' */ |
5746 | parse_error_context | |
5747 | (JDEP_WFL (dep), "Class `%s' not found in `throws'", | |
5748 | IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))); | |
5749 | break; | |
0a2138e2 APB |
5750 | default: |
5751 | /* Fix for -Wall. Just break doing nothing. The error will be | |
5752 | caught later */ | |
5753 | break; | |
e04a16fb AG |
5754 | } |
5755 | } | |
5756 | ||
22eed1e6 APB |
5757 | /* Return a static string containing the DECL prototype string. If |
5758 | DECL is a constructor, use the class name instead of the form | |
5759 | <init> */ | |
5760 | ||
49f48c71 | 5761 | static const char * |
22eed1e6 APB |
5762 | get_printable_method_name (decl) |
5763 | tree decl; | |
5764 | { | |
49f48c71 | 5765 | const char *to_return; |
9ee9b555 | 5766 | tree name = NULL_TREE; |
22eed1e6 APB |
5767 | |
5768 | if (DECL_CONSTRUCTOR_P (decl)) | |
5769 | { | |
5770 | name = DECL_NAME (decl); | |
5e942c50 | 5771 | DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))); |
22eed1e6 APB |
5772 | } |
5773 | ||
5774 | to_return = lang_printable_name (decl, 0); | |
5775 | if (DECL_CONSTRUCTOR_P (decl)) | |
5776 | DECL_NAME (decl) = name; | |
5777 | ||
5778 | return to_return; | |
5779 | } | |
5780 | ||
5e942c50 APB |
5781 | /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method |
5782 | nevertheless needs to be verfied, 1 otherwise. */ | |
5783 | ||
5784 | static int | |
5785 | reset_method_name (method) | |
5786 | tree method; | |
5787 | { | |
c2952b01 | 5788 | if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method)) |
5e942c50 APB |
5789 | { |
5790 | /* NAME is just the plain name when Object is being defined */ | |
5791 | if (DECL_CONTEXT (method) != object_type_node) | |
c877974e APB |
5792 | DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ? |
5793 | init_identifier_node : GET_METHOD_NAME (method)); | |
5e942c50 APB |
5794 | return 0; |
5795 | } | |
5796 | else | |
5797 | return 1; | |
5798 | } | |
5799 | ||
c877974e APB |
5800 | /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */ |
5801 | ||
5802 | tree | |
5803 | java_get_real_method_name (method_decl) | |
5804 | tree method_decl; | |
5805 | { | |
5806 | tree method_name = DECL_NAME (method_decl); | |
5807 | if (DECL_CONSTRUCTOR_P (method_decl)) | |
5808 | return init_identifier_node; | |
82371d41 APB |
5809 | |
5810 | /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P | |
5811 | and still can be a constructor. FIXME */ | |
5812 | ||
23a79c61 APB |
5813 | /* Don't confuse method only bearing the name of their class as |
5814 | constructors */ | |
82371d41 APB |
5815 | else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl)) |
5816 | && ctxp | |
c2952b01 | 5817 | && GET_CPC_UN () == EXPR_WFL_NODE (method_name) |
23a79c61 APB |
5818 | && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED |
5819 | && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node) | |
c877974e APB |
5820 | return init_identifier_node; |
5821 | else | |
5822 | return EXPR_WFL_NODE (method_name); | |
5823 | } | |
5824 | ||
22eed1e6 APB |
5825 | /* Track method being redefined inside the same class. As a side |
5826 | effect, set DECL_NAME to an IDENTIFIER (prior entering this | |
d77613be | 5827 | function it's a FWL, so we can track errors more accurately.) */ |
22eed1e6 | 5828 | |
e04a16fb AG |
5829 | static int |
5830 | check_method_redefinition (class, method) | |
5831 | tree class, method; | |
5832 | { | |
5833 | tree redef, name; | |
5834 | tree cl = DECL_NAME (method); | |
c3f2a476 | 5835 | tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method)); |
c00f0fb2 | 5836 | /* decl name of artificial <clinit> and finit$ doesn't need to be |
ba179f9f | 5837 | fixed and checked */ |
5e942c50 APB |
5838 | |
5839 | /* Reset the method name before running the check. If it returns 1, | |
5840 | the method doesn't need to be verified with respect to method | |
5841 | redeclaration and we return 0 */ | |
5842 | if (reset_method_name (method)) | |
e04a16fb | 5843 | return 0; |
5e942c50 APB |
5844 | |
5845 | name = DECL_NAME (method); | |
e04a16fb AG |
5846 | for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef)) |
5847 | { | |
c3f2a476 | 5848 | if (redef == method) |
e04a16fb | 5849 | break; |
c3f2a476 APB |
5850 | if (DECL_NAME (redef) == name |
5851 | && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))) | |
e04a16fb | 5852 | { |
22eed1e6 APB |
5853 | parse_error_context |
5854 | (cl, "Duplicate %s declaration `%s'", | |
5855 | (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"), | |
5856 | get_printable_method_name (redef)); | |
e04a16fb AG |
5857 | return 1; |
5858 | } | |
5859 | } | |
5860 | return 0; | |
5861 | } | |
5862 | ||
1175b9b4 TT |
5863 | /* Return 1 if check went ok, 0 otherwise. */ |
5864 | static int | |
d77613be APB |
5865 | check_abstract_method_definitions (do_interface, class_decl, type) |
5866 | int do_interface; | |
5867 | tree class_decl, type; | |
5868 | { | |
5869 | tree class = TREE_TYPE (class_decl); | |
5870 | tree method, end_type; | |
1175b9b4 | 5871 | int ok = 1; |
d77613be APB |
5872 | |
5873 | end_type = (do_interface ? object_type_node : type); | |
5874 | for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method)) | |
5875 | { | |
5876 | tree other_super, other_method, method_sig, method_name; | |
5877 | int found = 0; | |
165f37bc | 5878 | int end_type_reached = 0; |
d77613be APB |
5879 | |
5880 | if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method)) | |
5881 | continue; | |
5882 | ||
5883 | /* Now verify that somewhere in between TYPE and CLASS, | |
5884 | abstract method METHOD gets a non abstract definition | |
5885 | that is inherited by CLASS. */ | |
5886 | ||
5887 | method_sig = build_java_signature (TREE_TYPE (method)); | |
5888 | method_name = DECL_NAME (method); | |
5889 | if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION) | |
5890 | method_name = EXPR_WFL_NODE (method_name); | |
5891 | ||
165f37bc APB |
5892 | other_super = class; |
5893 | do { | |
5894 | if (other_super == end_type) | |
5895 | end_type_reached = 1; | |
5896 | ||
5897 | /* Method search */ | |
5898 | for (other_method = TYPE_METHODS (other_super); other_method; | |
5899 | other_method = TREE_CHAIN (other_method)) | |
5900 | { | |
5901 | tree s = build_java_signature (TREE_TYPE (other_method)); | |
5902 | tree other_name = DECL_NAME (other_method); | |
5903 | ||
5904 | if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION) | |
5905 | other_name = EXPR_WFL_NODE (other_name); | |
5906 | if (!DECL_CLINIT_P (other_method) | |
5907 | && !DECL_CONSTRUCTOR_P (other_method) | |
120f0c10 TT |
5908 | && method_name == other_name |
5909 | && method_sig == s | |
5910 | && !METHOD_ABSTRACT (other_method)) | |
165f37bc APB |
5911 | { |
5912 | found = 1; | |
5913 | break; | |
5914 | } | |
5915 | } | |
5916 | other_super = CLASSTYPE_SUPER (other_super); | |
5917 | } while (!end_type_reached); | |
5918 | ||
d77613be APB |
5919 | /* Report that abstract METHOD didn't find an implementation |
5920 | that CLASS can use. */ | |
5921 | if (!found) | |
5922 | { | |
c2e3db92 | 5923 | char *t = xstrdup (lang_printable_name |
d77613be APB |
5924 | (TREE_TYPE (TREE_TYPE (method)), 0)); |
5925 | tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method))); | |
5926 | tree saved_wfl = NULL_TREE; | |
5927 | ||
5928 | if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION) | |
5929 | { | |
5930 | saved_wfl = DECL_NAME (method); | |
5931 | DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method)); | |
5932 | } | |
5933 | ||
5934 | parse_error_context | |
5935 | (lookup_cl (class_decl), | |
781b0558 | 5936 | "Class `%s' doesn't define the abstract method `%s %s' from %s `%s'. This method must be defined or %s `%s' must be declared abstract", |
d77613be APB |
5937 | IDENTIFIER_POINTER (DECL_NAME (class_decl)), |
5938 | t, lang_printable_name (method, 0), | |
5939 | (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ? | |
5940 | "interface" : "class"), | |
5941 | IDENTIFIER_POINTER (ccn), | |
5942 | (CLASS_INTERFACE (class_decl) ? "interface" : "class"), | |
5943 | IDENTIFIER_POINTER (DECL_NAME (class_decl))); | |
1175b9b4 | 5944 | ok = 0; |
d77613be | 5945 | free (t); |
1175b9b4 | 5946 | |
d77613be APB |
5947 | if (saved_wfl) |
5948 | DECL_NAME (method) = saved_wfl; | |
5949 | } | |
5950 | } | |
1175b9b4 TT |
5951 | |
5952 | if (ok && do_interface) | |
5953 | { | |
5954 | /* Check for implemented interfaces. */ | |
5955 | int i; | |
5956 | tree vector = TYPE_BINFO_BASETYPES (type); | |
5957 | for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++) | |
5958 | { | |
5959 | tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i)); | |
5960 | ok = check_abstract_method_definitions (1, class_decl, super); | |
5961 | } | |
5962 | } | |
5963 | ||
5964 | return ok; | |
d77613be APB |
5965 | } |
5966 | ||
614eaae0 | 5967 | /* Check that CLASS_DECL somehow implements all inherited abstract |
d77613be APB |
5968 | methods. */ |
5969 | ||
5970 | static void | |
5971 | java_check_abstract_method_definitions (class_decl) | |
5972 | tree class_decl; | |
5973 | { | |
5974 | tree class = TREE_TYPE (class_decl); | |
5975 | tree super, vector; | |
5976 | int i; | |
5977 | ||
5978 | if (CLASS_ABSTRACT (class_decl)) | |
5979 | return; | |
5980 | ||
5981 | /* Check for inherited types */ | |
165f37bc APB |
5982 | super = class; |
5983 | do { | |
5984 | super = CLASSTYPE_SUPER (super); | |
5985 | check_abstract_method_definitions (0, class_decl, super); | |
5986 | } while (super != object_type_node); | |
d77613be APB |
5987 | |
5988 | /* Check for implemented interfaces. */ | |
5989 | vector = TYPE_BINFO_BASETYPES (class); | |
5990 | for (i = 1; i < TREE_VEC_LENGTH (vector); i++) | |
5991 | { | |
5992 | super = BINFO_TYPE (TREE_VEC_ELT (vector, i)); | |
5993 | check_abstract_method_definitions (1, class_decl, super); | |
5994 | } | |
5995 | } | |
5996 | ||
165f37bc APB |
5997 | /* Check all the types method DECL uses and return 1 if all of them |
5998 | are now complete, 0 otherwise. This is used to check whether its | |
5999 | safe to build a method signature or not. */ | |
6000 | ||
6001 | static int | |
6002 | check_method_types_complete (decl) | |
6003 | tree decl; | |
6004 | { | |
6005 | tree type = TREE_TYPE (decl); | |
6006 | tree args; | |
6007 | ||
6008 | if (!INCOMPLETE_TYPE_P (TREE_TYPE (type))) | |
6009 | return 0; | |
6010 | ||
6011 | args = TYPE_ARG_TYPES (type); | |
6012 | if (TREE_CODE (type) == METHOD_TYPE) | |
6013 | args = TREE_CHAIN (args); | |
6014 | for (; args != end_params_node; args = TREE_CHAIN (args)) | |
6015 | if (INCOMPLETE_TYPE_P (TREE_VALUE (args))) | |
6016 | return 0; | |
6017 | ||
6018 | return 1; | |
6019 | } | |
6020 | ||
34d4df06 APB |
6021 | /* Visible interface to check methods contained in CLASS_DECL */ |
6022 | ||
6023 | void | |
6024 | java_check_methods (class_decl) | |
6025 | tree class_decl; | |
6026 | { | |
6027 | if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl))) | |
6028 | return; | |
6029 | ||
6030 | if (CLASS_INTERFACE (class_decl)) | |
6031 | java_check_abstract_methods (class_decl); | |
6032 | else | |
6033 | java_check_regular_methods (class_decl); | |
6034 | ||
6035 | CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1; | |
6036 | } | |
6037 | ||
d77613be APB |
6038 | /* Check all the methods of CLASS_DECL. Methods are first completed |
6039 | then checked according to regular method existance rules. If no | |
6040 | constructor for CLASS_DECL were encountered, then build its | |
6041 | declaration. */ | |
e04a16fb AG |
6042 | |
6043 | static void | |
6044 | java_check_regular_methods (class_decl) | |
6045 | tree class_decl; | |
6046 | { | |
c2952b01 | 6047 | int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl)); |
e04a16fb AG |
6048 | tree method; |
6049 | tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl)); | |
5e942c50 | 6050 | tree saved_found_wfl = NULL_TREE, found = NULL_TREE; |
c877974e APB |
6051 | tree mthrows; |
6052 | ||
6053 | /* It is not necessary to check methods defined in java.lang.Object */ | |
6054 | if (class == object_type_node) | |
6055 | return; | |
e04a16fb | 6056 | |
23a79c61 APB |
6057 | if (!TYPE_NVIRTUALS (class)) |
6058 | TYPE_METHODS (class) = nreverse (TYPE_METHODS (class)); | |
e04a16fb AG |
6059 | |
6060 | /* Should take interfaces into account. FIXME */ | |
6061 | for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method)) | |
6062 | { | |
5e942c50 | 6063 | tree sig; |
e04a16fb AG |
6064 | tree method_wfl = DECL_NAME (method); |
6065 | int aflags; | |
6066 | ||
5e942c50 APB |
6067 | /* If we previously found something and its name was saved, |
6068 | reinstall it now */ | |
6069 | if (found && saved_found_wfl) | |
ba179f9f APB |
6070 | { |
6071 | DECL_NAME (found) = saved_found_wfl; | |
6072 | saved_found_wfl = NULL_TREE; | |
6073 | } | |
5e942c50 | 6074 | |
e04a16fb AG |
6075 | /* Check for redefinitions */ |
6076 | if (check_method_redefinition (class, method)) | |
6077 | continue; | |
6078 | ||
22eed1e6 APB |
6079 | /* If we see one constructor a mark so we don't generate the |
6080 | default one. Also skip other verifications: constructors | |
6081 | can't be inherited hence hiden or overriden */ | |
6082 | if (DECL_CONSTRUCTOR_P (method)) | |
6083 | { | |
6084 | saw_constructor = 1; | |
6085 | continue; | |
6086 | } | |
6087 | ||
c877974e APB |
6088 | /* We verify things thrown by the method. They must inherits from |
6089 | java.lang.Throwable */ | |
6090 | for (mthrows = DECL_FUNCTION_THROWS (method); | |
6091 | mthrows; mthrows = TREE_CHAIN (mthrows)) | |
6092 | { | |
6093 | if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node)) | |
6094 | parse_error_context | |
781b0558 | 6095 | (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'", |
c877974e APB |
6096 | IDENTIFIER_POINTER |
6097 | (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows))))); | |
6098 | } | |
6099 | ||
e04a16fb | 6100 | sig = build_java_argument_signature (TREE_TYPE (method)); |
614eaae0 | 6101 | found = lookup_argument_method2 (class, DECL_NAME (method), sig); |
b9f7e36c | 6102 | |
c2952b01 APB |
6103 | /* Inner class can't declare static methods */ |
6104 | if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl)) | |
6105 | { | |
6106 | char *t = xstrdup (lang_printable_name (class, 0)); | |
6107 | parse_error_context | |
6108 | (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static", | |
6109 | lang_printable_name (method, 0), t); | |
6110 | free (t); | |
6111 | } | |
6112 | ||
5e942c50 | 6113 | /* Nothing overrides or it's a private method. */ |
aabd7048 | 6114 | if (!found) |
5e942c50 | 6115 | continue; |
aabd7048 PB |
6116 | if (METHOD_PRIVATE (found)) |
6117 | { | |
6118 | found = NULL_TREE; | |
6119 | continue; | |
6120 | } | |
5e942c50 APB |
6121 | |
6122 | /* If found wasn't verified, it's DECL_NAME won't be set properly. | |
6123 | We set it temporarily for the sake of the error report. */ | |
6124 | saved_found_wfl = DECL_NAME (found); | |
6125 | reset_method_name (found); | |
6126 | ||
614eaae0 APB |
6127 | /* If `found' is declared in an interface, make sure the |
6128 | modifier matches. */ | |
6129 | if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found))) | |
6130 | && clinit_identifier_node != DECL_NAME (found) | |
6131 | && !METHOD_PUBLIC (method)) | |
6132 | { | |
6133 | tree found_decl = TYPE_NAME (DECL_CONTEXT (found)); | |
6134 | parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'", | |
6135 | IDENTIFIER_POINTER (DECL_NAME (class_decl)), | |
6136 | lang_printable_name (method, 0), | |
6137 | IDENTIFIER_POINTER (DECL_NAME (found_decl))); | |
6138 | } | |
6139 | ||
e04a16fb AG |
6140 | /* Can't override a method with the same name and different return |
6141 | types. */ | |
6142 | if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method))) | |
b9f7e36c | 6143 | { |
614eaae0 APB |
6144 | char *t = xstrdup |
6145 | (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0)); | |
b9f7e36c | 6146 | parse_error_context |
7f10c2e2 | 6147 | (method_wfl, |
b9f7e36c | 6148 | "Method `%s' was defined with return type `%s' in class `%s'", |
0a2138e2 | 6149 | lang_printable_name (found, 0), t, |
b9f7e36c APB |
6150 | IDENTIFIER_POINTER |
6151 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
6152 | free (t); | |
6153 | } | |
e04a16fb | 6154 | |
7f10c2e2 APB |
6155 | aflags = get_access_flags_from_decl (found); |
6156 | /* If the method has default, access in an other package, then | |
6157 | issue a warning that the current method doesn't override the | |
6158 | one that was found elsewhere. Do not issue this warning when | |
6159 | the match was found in java.lang.Object. */ | |
6160 | if (DECL_CONTEXT (found) != object_type_node | |
a003f638 | 6161 | && ((aflags & ACC_VISIBILITY) == 0) |
7f10c2e2 | 6162 | && !class_in_current_package (DECL_CONTEXT (found)) |
c2952b01 | 6163 | && !DECL_CLINIT_P (found) |
7f10c2e2 APB |
6164 | && flag_not_overriding) |
6165 | { | |
6166 | parse_warning_context | |
781b0558 | 6167 | (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package", |
7f10c2e2 APB |
6168 | lang_printable_name (found, 0), |
6169 | IDENTIFIER_POINTER (DECL_NAME (class_decl)), | |
6170 | IDENTIFIER_POINTER (DECL_NAME | |
6171 | (TYPE_NAME (DECL_CONTEXT (found))))); | |
6172 | continue; | |
6173 | } | |
6174 | ||
e04a16fb AG |
6175 | /* Can't override final. Can't override static. */ |
6176 | if (METHOD_FINAL (found) || METHOD_STATIC (found)) | |
6177 | { | |
6178 | /* Static *can* override static */ | |
6179 | if (METHOD_STATIC (found) && METHOD_STATIC (method)) | |
6180 | continue; | |
6181 | parse_error_context | |
6182 | (method_wfl, | |
6183 | "%s methods can't be overriden. Method `%s' is %s in class `%s'", | |
6184 | (METHOD_FINAL (found) ? "Final" : "Static"), | |
0a2138e2 | 6185 | lang_printable_name (found, 0), |
e04a16fb AG |
6186 | (METHOD_FINAL (found) ? "final" : "static"), |
6187 | IDENTIFIER_POINTER | |
6188 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
6189 | continue; | |
6190 | } | |
7f10c2e2 | 6191 | |
e04a16fb AG |
6192 | /* Static method can't override instance method. */ |
6193 | if (METHOD_STATIC (method)) | |
6194 | { | |
6195 | parse_error_context | |
6196 | (method_wfl, | |
781b0558 | 6197 | "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'", |
0a2138e2 | 6198 | lang_printable_name (found, 0), |
e04a16fb AG |
6199 | IDENTIFIER_POINTER |
6200 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
6201 | continue; | |
6202 | } | |
5e942c50 | 6203 | |
5e942c50 APB |
6204 | /* - Overriding/hiding public must be public |
6205 | - Overriding/hiding protected must be protected or public | |
6206 | - If the overriden or hidden method has default (package) | |
6207 | access, then the overriding or hiding method must not be | |
614eaae0 APB |
6208 | private; otherwise, a compile-time error occurs. If |
6209 | `found' belongs to an interface, things have been already | |
6210 | taken care of. */ | |
6211 | if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found))) | |
6212 | && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method)) | |
6213 | || (METHOD_PROTECTED (found) | |
6214 | && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method))) | |
6215 | || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC)) | |
6216 | && METHOD_PRIVATE (method)))) | |
e04a16fb AG |
6217 | { |
6218 | parse_error_context | |
6219 | (method_wfl, | |
781b0558 | 6220 | "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0), |
5e942c50 APB |
6221 | (METHOD_PUBLIC (method) ? "public" : |
6222 | (METHOD_PRIVATE (method) ? "private" : "protected")), | |
6223 | IDENTIFIER_POINTER (DECL_NAME | |
6224 | (TYPE_NAME (DECL_CONTEXT (found))))); | |
e04a16fb AG |
6225 | continue; |
6226 | } | |
6227 | ||
b9f7e36c APB |
6228 | /* Overriding methods must have compatible `throws' clauses on checked |
6229 | exceptions, if any */ | |
6230 | check_throws_clauses (method, method_wfl, found); | |
6231 | ||
e04a16fb AG |
6232 | /* Inheriting multiple methods with the same signature. FIXME */ |
6233 | } | |
6234 | ||
5e942c50 APB |
6235 | /* Don't forget eventual pending found and saved_found_wfl. Take |
6236 | into account that we might have exited because we saw an | |
d77613be | 6237 | artificial method as the last entry. */ |
5e942c50 APB |
6238 | |
6239 | if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl) | |
6240 | DECL_NAME (found) = saved_found_wfl; | |
6241 | ||
23a79c61 APB |
6242 | if (!TYPE_NVIRTUALS (class)) |
6243 | TYPE_METHODS (class) = nreverse (TYPE_METHODS (class)); | |
e04a16fb | 6244 | |
d77613be APB |
6245 | /* Search for inherited abstract method not yet implemented in this |
6246 | class. */ | |
6247 | java_check_abstract_method_definitions (class_decl); | |
6248 | ||
22eed1e6 | 6249 | if (!saw_constructor) |
e920ebc9 | 6250 | fatal ("No constructor found"); |
e04a16fb AG |
6251 | } |
6252 | ||
b9f7e36c APB |
6253 | /* Return a non zero value if the `throws' clause of METHOD (if any) |
6254 | is incompatible with the `throws' clause of FOUND (if any). */ | |
6255 | ||
6256 | static void | |
6257 | check_throws_clauses (method, method_wfl, found) | |
6258 | tree method, method_wfl, found; | |
6259 | { | |
6260 | tree mthrows, fthrows; | |
6261 | ||
c877974e APB |
6262 | /* Can't check these things with class loaded from bytecode. FIXME */ |
6263 | if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found))) | |
6264 | return; | |
6265 | ||
b9f7e36c APB |
6266 | for (mthrows = DECL_FUNCTION_THROWS (method); |
6267 | mthrows; mthrows = TREE_CHAIN (mthrows)) | |
6268 | { | |
6269 | /* We don't verify unchecked expressions */ | |
c877974e | 6270 | if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows))) |
b9f7e36c APB |
6271 | continue; |
6272 | /* Checked expression must be compatible */ | |
6273 | for (fthrows = DECL_FUNCTION_THROWS (found); | |
6274 | fthrows; fthrows = TREE_CHAIN (fthrows)) | |
6275 | if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows))) | |
6276 | break; | |
6277 | if (!fthrows) | |
6278 | { | |
6279 | parse_error_context | |
781b0558 | 6280 | (method_wfl, "Invalid checked exception class `%s' in `throws' clause. The exception must be a subclass of an exception thrown by `%s' from class `%s'", |
b9f7e36c | 6281 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))), |
0a2138e2 | 6282 | lang_printable_name (found, 0), |
b9f7e36c APB |
6283 | IDENTIFIER_POINTER |
6284 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
6285 | } | |
6286 | } | |
6287 | } | |
6288 | ||
e04a16fb AG |
6289 | /* Check abstract method of interface INTERFACE */ |
6290 | ||
6291 | static void | |
5e942c50 APB |
6292 | java_check_abstract_methods (interface_decl) |
6293 | tree interface_decl; | |
e04a16fb AG |
6294 | { |
6295 | int i, n; | |
6296 | tree method, basetype_vec, found; | |
5e942c50 | 6297 | tree interface = TREE_TYPE (interface_decl); |
e04a16fb AG |
6298 | |
6299 | for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method)) | |
6300 | { | |
b9f7e36c | 6301 | tree method_wfl = DECL_NAME (method); |
e04a16fb AG |
6302 | |
6303 | /* 2- Check for double definition inside the defining interface */ | |
6304 | if (check_method_redefinition (interface, method)) | |
6305 | continue; | |
6306 | ||
6307 | /* 3- Overriding is OK as far as we preserve the return type and | |
b9f7e36c | 6308 | the thrown exceptions (FIXME) */ |
e04a16fb AG |
6309 | found = lookup_java_interface_method2 (interface, method); |
6310 | if (found) | |
6311 | { | |
5e942c50 APB |
6312 | char *t; |
6313 | tree saved_found_wfl = DECL_NAME (found); | |
6314 | reset_method_name (found); | |
c2e3db92 | 6315 | t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0)); |
e04a16fb | 6316 | parse_error_context |
b9f7e36c | 6317 | (method_wfl, |
5e942c50 | 6318 | "Method `%s' was defined with return type `%s' in class `%s'", |
0a2138e2 | 6319 | lang_printable_name (found, 0), t, |
b9f7e36c APB |
6320 | IDENTIFIER_POINTER |
6321 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
6322 | free (t); | |
5e942c50 | 6323 | DECL_NAME (found) = saved_found_wfl; |
c63b98cd | 6324 | continue; |
e04a16fb AG |
6325 | } |
6326 | } | |
6327 | ||
6328 | /* 4- Inherited methods can't differ by their returned types */ | |
6329 | if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface))) | |
6330 | return; | |
6331 | n = TREE_VEC_LENGTH (basetype_vec); | |
6332 | for (i = 0; i < n; i++) | |
6333 | { | |
6334 | tree sub_interface_method, sub_interface; | |
6335 | tree vec_elt = TREE_VEC_ELT (basetype_vec, i); | |
6336 | if (!vec_elt) | |
6337 | continue; | |
6338 | sub_interface = BINFO_TYPE (vec_elt); | |
6339 | for (sub_interface_method = TYPE_METHODS (sub_interface); | |
6340 | sub_interface_method; | |
6341 | sub_interface_method = TREE_CHAIN (sub_interface_method)) | |
6342 | { | |
6343 | found = lookup_java_interface_method2 (interface, | |
6344 | sub_interface_method); | |
6345 | if (found && (found != sub_interface_method)) | |
5e942c50 APB |
6346 | { |
6347 | tree saved_found_wfl = DECL_NAME (found); | |
6348 | reset_method_name (found); | |
6349 | parse_error_context | |
6350 | (lookup_cl (sub_interface_method), | |
781b0558 | 6351 | "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'", |
5e942c50 APB |
6352 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))), |
6353 | lang_printable_name (found, 0), | |
6354 | IDENTIFIER_POINTER | |
6355 | (DECL_NAME (TYPE_NAME | |
6356 | (DECL_CONTEXT (sub_interface_method)))), | |
6357 | IDENTIFIER_POINTER | |
6358 | (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found))))); | |
6359 | DECL_NAME (found) = saved_found_wfl; | |
6360 | } | |
e04a16fb AG |
6361 | } |
6362 | } | |
6363 | } | |
6364 | ||
e04a16fb AG |
6365 | /* Lookup methods in interfaces using their name and partial |
6366 | signature. Return a matching method only if their types differ. */ | |
6367 | ||
6368 | static tree | |
6369 | lookup_java_interface_method2 (class, method_decl) | |
6370 | tree class, method_decl; | |
6371 | { | |
6372 | int i, n; | |
6373 | tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return; | |
6374 | ||
6375 | if (!basetype_vec) | |
6376 | return NULL_TREE; | |
6377 | ||
6378 | n = TREE_VEC_LENGTH (basetype_vec); | |
6379 | for (i = 0; i < n; i++) | |
6380 | { | |
6381 | tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return; | |
6382 | if ((BINFO_TYPE (vec_elt) != object_type_node) | |
6383 | && (to_return = | |
6384 | lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1))) | |
6385 | return to_return; | |
6386 | } | |
6387 | for (i = 0; i < n; i++) | |
6388 | { | |
6389 | to_return = lookup_java_interface_method2 | |
6390 | (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl); | |
6391 | if (to_return) | |
6392 | return to_return; | |
6393 | } | |
6394 | ||
6395 | return NULL_TREE; | |
6396 | } | |
6397 | ||
6398 | /* Lookup method using their name and partial signature. Return a | |
6399 | matching method only if their types differ. */ | |
6400 | ||
6401 | static tree | |
6402 | lookup_java_method2 (clas, method_decl, do_interface) | |
6403 | tree clas, method_decl; | |
6404 | int do_interface; | |
6405 | { | |
5e942c50 APB |
6406 | tree method, method_signature, method_name, method_type, name; |
6407 | ||
e04a16fb | 6408 | method_signature = build_java_argument_signature (TREE_TYPE (method_decl)); |
5e942c50 APB |
6409 | name = DECL_NAME (method_decl); |
6410 | method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ? | |
6411 | EXPR_WFL_NODE (name) : name); | |
e04a16fb AG |
6412 | method_type = TREE_TYPE (TREE_TYPE (method_decl)); |
6413 | ||
6414 | while (clas != NULL_TREE) | |
6415 | { | |
6416 | for (method = TYPE_METHODS (clas); | |
6417 | method != NULL_TREE; method = TREE_CHAIN (method)) | |
6418 | { | |
6419 | tree method_sig = build_java_argument_signature (TREE_TYPE (method)); | |
5e942c50 APB |
6420 | tree name = DECL_NAME (method); |
6421 | if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ? | |
6422 | EXPR_WFL_NODE (name) : name) == method_name | |
e04a16fb AG |
6423 | && method_sig == method_signature |
6424 | && TREE_TYPE (TREE_TYPE (method)) != method_type) | |
5e942c50 | 6425 | return method; |
e04a16fb AG |
6426 | } |
6427 | clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas)); | |
6428 | } | |
6429 | return NULL_TREE; | |
6430 | } | |
6431 | ||
f441f671 APB |
6432 | /* Return the line that matches DECL line number, and try its best to |
6433 | position the column number. Used during error reports. */ | |
e04a16fb AG |
6434 | |
6435 | static tree | |
6436 | lookup_cl (decl) | |
6437 | tree decl; | |
6438 | { | |
6439 | static tree cl = NULL_TREE; | |
f441f671 | 6440 | char *line, *found; |
e04a16fb AG |
6441 | |
6442 | if (!decl) | |
6443 | return NULL_TREE; | |
6444 | ||
6445 | if (cl == NULL_TREE) | |
19e223db MM |
6446 | { |
6447 | cl = build_expr_wfl (NULL_TREE, NULL, 0, 0); | |
6448 | ggc_add_tree_root (&cl, 1); | |
6449 | } | |
e04a16fb AG |
6450 | |
6451 | EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl)); | |
6452 | EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1); | |
6453 | ||
dba41d30 | 6454 | line = java_get_line_col (EXPR_WFL_FILENAME (cl), |
f441f671 APB |
6455 | EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl)); |
6456 | ||
6457 | found = strstr ((const char *)line, | |
6458 | (const char *)IDENTIFIER_POINTER (DECL_NAME (decl))); | |
6459 | if (found) | |
6460 | EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line); | |
6461 | ||
e04a16fb AG |
6462 | return cl; |
6463 | } | |
6464 | ||
6465 | /* Look for a simple name in the single-type import list */ | |
6466 | ||
6467 | static tree | |
6468 | find_name_in_single_imports (name) | |
6469 | tree name; | |
6470 | { | |
6471 | tree node; | |
6472 | ||
6473 | for (node = ctxp->import_list; node; node = TREE_CHAIN (node)) | |
6474 | if (TREE_VALUE (node) == name) | |
6475 | return (EXPR_WFL_NODE (TREE_PURPOSE (node))); | |
6476 | ||
6477 | return NULL_TREE; | |
6478 | } | |
6479 | ||
6480 | /* Process all single-type import. */ | |
6481 | ||
6482 | static int | |
6483 | process_imports () | |
6484 | { | |
6485 | tree import; | |
6486 | int error_found; | |
6487 | ||
6488 | for (import = ctxp->import_list; import; import = TREE_CHAIN (import)) | |
6489 | { | |
6490 | tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import)); | |
6491 | ||
6492 | /* Don't load twice something already defined. */ | |
6493 | if (IDENTIFIER_CLASS_VALUE (to_be_found)) | |
6494 | continue; | |
02ae6e2e APB |
6495 | |
6496 | while (1) | |
6497 | { | |
6498 | tree left; | |
6499 | ||
6500 | QUALIFIED_P (to_be_found) = 1; | |
6501 | load_class (to_be_found, 0); | |
6502 | error_found = | |
6503 | check_pkg_class_access (to_be_found, TREE_PURPOSE (import)); | |
6504 | ||
6505 | /* We found it, we can bail out */ | |
6506 | if (IDENTIFIER_CLASS_VALUE (to_be_found)) | |
6507 | break; | |
6508 | ||
6509 | /* We haven't found it. Maybe we're trying to access an | |
6510 | inner class. The only way for us to know is to try again | |
6511 | after having dropped a qualifier. If we can't break it further, | |
6512 | we have an error. */ | |
6513 | if (breakdown_qualified (&left, NULL, to_be_found)) | |
6514 | break; | |
6515 | ||
6516 | to_be_found = left; | |
6517 | } | |
e04a16fb AG |
6518 | if (!IDENTIFIER_CLASS_VALUE (to_be_found)) |
6519 | { | |
6520 | parse_error_context (TREE_PURPOSE (import), | |
6521 | "Class or interface `%s' not found in import", | |
6522 | IDENTIFIER_POINTER (to_be_found)); | |
6523 | return 1; | |
6524 | } | |
6525 | if (error_found) | |
6526 | return 1; | |
6527 | } | |
6528 | return 0; | |
6529 | } | |
6530 | ||
9a7ab4b3 APB |
6531 | /* Possibly find and mark a class imported by a single-type import |
6532 | statement. */ | |
e04a16fb | 6533 | |
9a7ab4b3 | 6534 | static void |
e04a16fb AG |
6535 | find_in_imports (class_type) |
6536 | tree class_type; | |
6537 | { | |
6538 | tree import; | |
6539 | ||
6540 | for (import = ctxp->import_list; import; import = TREE_CHAIN (import)) | |
6541 | if (TREE_VALUE (import) == TYPE_NAME (class_type)) | |
6542 | { | |
6543 | TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import)); | |
6544 | QUALIFIED_P (TYPE_NAME (class_type)) = 1; | |
e04a16fb | 6545 | } |
e04a16fb AG |
6546 | } |
6547 | ||
e04a16fb | 6548 | static int |
63a212ed | 6549 | note_possible_classname (name, len) |
49f48c71 | 6550 | const char *name; |
63a212ed | 6551 | int len; |
e04a16fb | 6552 | { |
63a212ed PB |
6553 | tree node; |
6554 | if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0) | |
6555 | len = len - 5; | |
6556 | else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0) | |
6557 | len = len - 6; | |
e04a16fb | 6558 | else |
63a212ed PB |
6559 | return 0; |
6560 | node = ident_subst (name, len, "", '/', '.', ""); | |
6561 | IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */ | |
fe0e4d76 | 6562 | QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0; |
63a212ed | 6563 | return 1; |
e04a16fb AG |
6564 | } |
6565 | ||
6566 | /* Read a import directory, gathering potential match for further type | |
6567 | references. Indifferently reads a filesystem or a ZIP archive | |
6568 | directory. */ | |
6569 | ||
6570 | static void | |
6571 | read_import_dir (wfl) | |
6572 | tree wfl; | |
6573 | { | |
63a212ed | 6574 | tree package_id = EXPR_WFL_NODE (wfl); |
49f48c71 | 6575 | const char *package_name = IDENTIFIER_POINTER (package_id); |
63a212ed | 6576 | int package_length = IDENTIFIER_LENGTH (package_id); |
e04a16fb | 6577 | DIR *dirp = NULL; |
d8fccff5 | 6578 | JCF *saved_jcf = current_jcf; |
e04a16fb | 6579 | |
63a212ed PB |
6580 | int found = 0; |
6581 | int k; | |
6582 | void *entry; | |
6583 | struct buffer filename[1]; | |
6584 | ||
6585 | ||
6586 | if (IS_AN_IMPORT_ON_DEMAND_P (package_id)) | |
6587 | return; | |
6588 | IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1; | |
6589 | ||
6590 | BUFFER_INIT (filename); | |
6591 | buffer_grow (filename, package_length + 100); | |
6592 | ||
6593 | for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry)) | |
6594 | { | |
49f48c71 | 6595 | const char *entry_name = jcf_path_name (entry); |
63a212ed PB |
6596 | int entry_length = strlen (entry_name); |
6597 | if (jcf_path_is_zipfile (entry)) | |
6598 | { | |
6599 | ZipFile *zipf; | |
6600 | buffer_grow (filename, entry_length); | |
6601 | memcpy (filename->data, entry_name, entry_length - 1); | |
6602 | filename->data[entry_length-1] = '\0'; | |
6603 | zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry)); | |
6604 | if (zipf == NULL) | |
6605 | error ("malformed .zip archive in CLASSPATH: %s", entry_name); | |
6606 | else | |
6607 | { | |
6608 | ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory; | |
6609 | BUFFER_RESET (filename); | |
6610 | for (k = 0; k < package_length; k++) | |
6611 | { | |
6612 | char ch = package_name[k]; | |
6613 | *filename->ptr++ = ch == '.' ? '/' : ch; | |
6614 | } | |
6615 | *filename->ptr++ = '/'; | |
6616 | ||
345137c7 | 6617 | for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd)) |
63a212ed | 6618 | { |
49f48c71 | 6619 | const char *current_entry = ZIPDIR_FILENAME (zipd); |
63a212ed PB |
6620 | int current_entry_len = zipd->filename_length; |
6621 | ||
345137c7 TT |
6622 | if (current_entry_len >= BUFFER_LENGTH (filename) |
6623 | && strncmp (filename->data, current_entry, | |
6624 | BUFFER_LENGTH (filename)) != 0) | |
63a212ed | 6625 | continue; |
345137c7 | 6626 | found |= note_possible_classname (current_entry, |
63a212ed PB |
6627 | current_entry_len); |
6628 | } | |
6629 | } | |
6630 | } | |
6631 | else | |
6632 | { | |
6633 | BUFFER_RESET (filename); | |
6634 | buffer_grow (filename, entry_length + package_length + 4); | |
6635 | strcpy (filename->data, entry_name); | |
6636 | filename->ptr = filename->data + entry_length; | |
6637 | for (k = 0; k < package_length; k++) | |
6638 | { | |
6639 | char ch = package_name[k]; | |
6640 | *filename->ptr++ = ch == '.' ? '/' : ch; | |
6641 | } | |
6642 | *filename->ptr = '\0'; | |
6643 | ||
6644 | dirp = opendir (filename->data); | |
6645 | if (dirp == NULL) | |
6646 | continue; | |
6647 | *filename->ptr++ = '/'; | |
6648 | for (;;) | |
6649 | { | |
63a212ed | 6650 | int len; |
49f48c71 | 6651 | const char *d_name; |
63a212ed PB |
6652 | struct dirent *direntp = readdir (dirp); |
6653 | if (!direntp) | |
6654 | break; | |
6655 | d_name = direntp->d_name; | |
6656 | len = strlen (direntp->d_name); | |
6657 | buffer_grow (filename, len+1); | |
6658 | strcpy (filename->ptr, d_name); | |
345137c7 | 6659 | found |= note_possible_classname (filename->data + entry_length, |
63a212ed PB |
6660 | package_length+len+1); |
6661 | } | |
6662 | if (dirp) | |
6663 | closedir (dirp); | |
6664 | } | |
6665 | } | |
e04a16fb | 6666 | |
63a212ed | 6667 | free (filename->data); |
e04a16fb | 6668 | |
63a212ed PB |
6669 | /* Here we should have a unified way of retrieving an entry, to be |
6670 | indexed. */ | |
6671 | if (!found) | |
e04a16fb AG |
6672 | { |
6673 | static int first = 1; | |
6674 | if (first) | |
6675 | { | |
781b0558 | 6676 | error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name); |
e04a16fb AG |
6677 | java_error_count++; |
6678 | first = 0; | |
6679 | } | |
6680 | else | |
63a212ed PB |
6681 | parse_error_context (wfl, "Package `%s' not found in import", |
6682 | package_name); | |
e04a16fb AG |
6683 | current_jcf = saved_jcf; |
6684 | return; | |
6685 | } | |
e04a16fb AG |
6686 | current_jcf = saved_jcf; |
6687 | } | |
6688 | ||
6689 | /* Possibly find a type in the import on demands specified | |
6690 | types. Returns 1 if an error occured, 0 otherwise. Run throught the | |
6691 | entire list, to detected potential double definitions. */ | |
6692 | ||
6693 | static int | |
6694 | find_in_imports_on_demand (class_type) | |
6695 | tree class_type; | |
6696 | { | |
ab3a6dd6 | 6697 | tree node, import, node_to_use = NULL_TREE; |
e04a16fb | 6698 | int seen_once = -1; |
ab3a6dd6 | 6699 | tree cl = NULL_TREE; |
e04a16fb AG |
6700 | |
6701 | for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import)) | |
6702 | { | |
49f48c71 | 6703 | const char *id_name; |
e04a16fb AG |
6704 | obstack_grow (&temporary_obstack, |
6705 | IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))), | |
6706 | IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import)))); | |
63a212ed | 6707 | obstack_1grow (&temporary_obstack, '.'); |
e04a16fb AG |
6708 | obstack_grow0 (&temporary_obstack, |
6709 | IDENTIFIER_POINTER (TYPE_NAME (class_type)), | |
6710 | IDENTIFIER_LENGTH (TYPE_NAME (class_type))); | |
6711 | id_name = obstack_finish (&temporary_obstack); | |
6712 | ||
6713 | node = maybe_get_identifier (id_name); | |
6714 | if (node && IS_A_CLASSFILE_NAME (node)) | |
6715 | { | |
6716 | if (seen_once < 0) | |
6717 | { | |
6718 | cl = TREE_PURPOSE (import); | |
6719 | seen_once = 1; | |
6720 | node_to_use = node; | |
6721 | } | |
6722 | else | |
6723 | { | |
6724 | seen_once++; | |
6725 | parse_error_context | |
1e12ab9b APB |
6726 | (TREE_PURPOSE (import), |
6727 | "Type `%s' also potentially defined in package `%s'", | |
e04a16fb AG |
6728 | IDENTIFIER_POINTER (TYPE_NAME (class_type)), |
6729 | IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import)))); | |
6730 | } | |
6731 | } | |
6732 | } | |
6733 | ||
6734 | if (seen_once == 1) | |
6735 | { | |
6736 | /* Setup lineno so that it refers to the line of the import (in | |
6737 | case we parse a class file and encounter errors */ | |
6738 | tree decl; | |
6739 | int saved_lineno = lineno; | |
6740 | lineno = EXPR_WFL_LINENO (cl); | |
63a212ed | 6741 | TYPE_NAME (class_type) = node_to_use; |
e04a16fb AG |
6742 | QUALIFIED_P (TYPE_NAME (class_type)) = 1; |
6743 | decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type)); | |
6744 | /* If there is no DECL set for the class or if the class isn't | |
6745 | loaded and not seen in source yet, the load */ | |
6746 | if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl)) | |
6747 | && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl)))) | |
6748 | load_class (node_to_use, 0); | |
6749 | lineno = saved_lineno; | |
6750 | return check_pkg_class_access (TYPE_NAME (class_type), cl); | |
6751 | } | |
6752 | else | |
6753 | return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */ | |
6754 | } | |
6755 | ||
9a7ab4b3 APB |
6756 | /* Add package NAME to the list of package encountered so far. To |
6757 | speed up class lookup in do_resolve_class, we make sure a | |
6758 | particular package is added only once. */ | |
6759 | ||
6760 | static void | |
6761 | register_package (name) | |
6762 | tree name; | |
6763 | { | |
6764 | static struct hash_table _pht, *pht = NULL; | |
6765 | ||
6766 | if (!pht) | |
6767 | { | |
6768 | hash_table_init (&_pht, hash_newfunc, | |
6769 | java_hash_hash_tree_node, java_hash_compare_tree_node); | |
6770 | pht = &_pht; | |
6771 | } | |
6772 | ||
6773 | if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL)) | |
6774 | { | |
6775 | package_list = chainon (package_list, build_tree_list (name, NULL)); | |
6776 | hash_lookup (pht, (const hash_table_key) name, TRUE, NULL); | |
6777 | } | |
6778 | } | |
6779 | ||
5e942c50 APB |
6780 | static tree |
6781 | resolve_package (pkg, next) | |
6782 | tree pkg, *next; | |
6783 | { | |
c2952b01 | 6784 | tree current, acc; |
5e942c50 | 6785 | tree type_name = NULL_TREE; |
49f48c71 | 6786 | const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg)); |
5e942c50 APB |
6787 | |
6788 | /* The trick is to determine when the package name stops and were | |
6789 | the name of something contained in the package starts. Then we | |
6790 | return a fully qualified name of what we want to get. */ | |
6791 | ||
6792 | /* Do a quick search on well known package names */ | |
6793 | if (!strncmp (name, "java.lang.reflect", 17)) | |
6794 | { | |
6795 | *next = | |
6796 | TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)))); | |
6797 | type_name = lookup_package_type (name, 17); | |
6798 | } | |
6799 | else if (!strncmp (name, "java.lang", 9)) | |
6800 | { | |
6801 | *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))); | |
6802 | type_name = lookup_package_type (name, 9); | |
6803 | } | |
5e942c50 | 6804 | |
2c56429a APB |
6805 | /* If we found something here, return */ |
6806 | if (type_name) | |
6807 | return type_name; | |
6808 | ||
6809 | *next = EXPR_WFL_QUALIFICATION (pkg); | |
6810 | ||
6811 | /* Try the current package. */ | |
6812 | if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package), | |
6813 | IDENTIFIER_LENGTH (ctxp->package))) | |
6814 | { | |
6815 | type_name = | |
6816 | lookup_package_type_and_set_next (name, | |
6817 | IDENTIFIER_LENGTH (ctxp->package), | |
6818 | next ); | |
6819 | if (type_name) | |
6820 | return type_name; | |
6821 | } | |
6822 | ||
6823 | /* Search in imported package */ | |
6824 | for (current = ctxp->import_list; current; current = TREE_CHAIN (current)) | |
6825 | { | |
6826 | tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current)); | |
6827 | int len = IDENTIFIER_LENGTH (current_pkg_name); | |
6828 | if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len)) | |
6829 | { | |
6830 | tree left, dummy; | |
6831 | ||
6832 | breakdown_qualified (&left, &dummy, current_pkg_name); | |
6833 | len = IDENTIFIER_LENGTH (left); | |
6834 | type_name = lookup_package_type_and_set_next (name, len, next); | |
6835 | if (type_name) | |
6836 | break; | |
6837 | } | |
6838 | } | |
6839 | ||
c2952b01 APB |
6840 | /* Try to progressively construct a type name */ |
6841 | if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION) | |
6842 | for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg); | |
6843 | current; current = TREE_CHAIN (current)) | |
6844 | { | |
6845 | acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current))); | |
6846 | if ((type_name = resolve_no_layout (acc, NULL_TREE))) | |
6847 | { | |
6848 | type_name = acc; | |
6b48deee APB |
6849 | /* resolve_package should be used in a loop, hence we |
6850 | point at this one to naturally process the next one at | |
6851 | the next iteration. */ | |
6852 | *next = current; | |
c2952b01 APB |
6853 | break; |
6854 | } | |
6855 | } | |
2c56429a APB |
6856 | return type_name; |
6857 | } | |
6858 | ||
6859 | static tree | |
6860 | lookup_package_type_and_set_next (name, len, next) | |
49f48c71 | 6861 | const char *name; |
2c56429a APB |
6862 | int len; |
6863 | tree *next; | |
6864 | { | |
49f48c71 | 6865 | const char *ptr; |
2c56429a APB |
6866 | tree type_name = lookup_package_type (name, len); |
6867 | ||
6868 | if (!type_name) | |
6869 | return NULL; | |
6870 | ||
6871 | ptr = IDENTIFIER_POINTER (type_name); | |
6872 | while (ptr && (ptr = strchr (ptr, '.'))) | |
6873 | { | |
6874 | *next = TREE_CHAIN (*next); | |
6875 | ptr++; | |
6876 | } | |
5e942c50 APB |
6877 | return type_name; |
6878 | } | |
6879 | ||
6880 | static tree | |
6881 | lookup_package_type (name, from) | |
49f48c71 | 6882 | const char *name; |
5e942c50 APB |
6883 | int from; |
6884 | { | |
6885 | char subname [128]; | |
49f48c71 | 6886 | const char *sub = &name[from+1]; |
5e942c50 APB |
6887 | while (*sub != '.' && *sub) |
6888 | sub++; | |
6889 | strncpy (subname, name, sub-name); | |
6890 | subname [sub-name] = '\0'; | |
6891 | return get_identifier (subname); | |
6892 | } | |
6893 | ||
cf1748bf | 6894 | static void |
4dbf4496 APB |
6895 | check_inner_class_access (decl, enclosing_decl, cl) |
6896 | tree decl, enclosing_decl, cl; | |
cf1748bf | 6897 | { |
4dbf4496 APB |
6898 | int access = 0; |
6899 | ||
cf1748bf | 6900 | /* We don't issue an error message when CL is null. CL can be null |
4dbf4496 APB |
6901 | as a result of processing a JDEP crafted by source_start_java_method |
6902 | for the purpose of patching its parm decl. But the error would | |
6903 | have been already trapped when fixing the method's signature. | |
6904 | DECL can also be NULL in case of earlier errors. */ | |
6905 | if (!decl || !cl) | |
cf1748bf APB |
6906 | return; |
6907 | ||
4dbf4496 APB |
6908 | /* We grant access to private and protected inner classes if the |
6909 | location from where we're trying to access DECL is an enclosing | |
6910 | context for DECL or if both have a common enclosing context. */ | |
6911 | if (CLASS_PRIVATE (decl)) | |
6912 | access = 1; | |
6913 | if (CLASS_PROTECTED (decl)) | |
6914 | access = 2; | |
6915 | if (!access) | |
6916 | return; | |
6917 | ||
6918 | if (common_enclosing_context_p (TREE_TYPE (enclosing_decl), | |
6919 | TREE_TYPE (decl)) | |
6920 | || enclosing_context_p (TREE_TYPE (enclosing_decl), | |
6921 | TREE_TYPE (decl))) | |
6922 | return; | |
6923 | ||
6924 | parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed", | |
6925 | (access == 1 ? "private" : "protected"), | |
cf1748bf APB |
6926 | (CLASS_INTERFACE (decl) ? "interface" : "class"), |
6927 | lang_printable_name (decl, 0)); | |
6928 | } | |
6929 | ||
e04a16fb AG |
6930 | /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no |
6931 | access violations were found, 1 otherwise. */ | |
6932 | ||
6933 | static int | |
6934 | check_pkg_class_access (class_name, cl) | |
6935 | tree class_name; | |
6936 | tree cl; | |
6937 | { | |
6938 | tree type; | |
e04a16fb AG |
6939 | |
6940 | if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name)) | |
6941 | return 0; | |
6942 | ||
6943 | if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name)))) | |
6944 | return 0; | |
6945 | ||
6946 | if (!CLASS_PUBLIC (TYPE_NAME (type))) | |
6947 | { | |
e28cd97b APB |
6948 | /* Access to a private class within the same package is |
6949 | allowed. */ | |
6950 | tree l, r; | |
6951 | breakdown_qualified (&l, &r, class_name); | |
6952 | if (l == ctxp->package) | |
6953 | return 0; | |
6954 | ||
e04a16fb | 6955 | parse_error_context |
781b0558 | 6956 | (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed", |
e04a16fb AG |
6957 | (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"), |
6958 | IDENTIFIER_POINTER (class_name)); | |
6959 | return 1; | |
6960 | } | |
6961 | return 0; | |
6962 | } | |
6963 | ||
6964 | /* Local variable declaration. */ | |
6965 | ||
6966 | static void | |
6967 | declare_local_variables (modifier, type, vlist) | |
6968 | int modifier; | |
6969 | tree type; | |
6970 | tree vlist; | |
6971 | { | |
c583dd46 APB |
6972 | tree decl, current, saved_type; |
6973 | tree type_wfl = NULL_TREE; | |
e04a16fb | 6974 | int must_chain = 0; |
c2952b01 | 6975 | int final_p = 0; |
e04a16fb | 6976 | |
2aa11e97 | 6977 | /* Push a new block if statements were seen between the last time we |
e04a16fb | 6978 | pushed a block and now. Keep a cound of block to close */ |
f099f336 | 6979 | if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl))) |
e04a16fb | 6980 | { |
f099f336 | 6981 | tree body = GET_CURRENT_BLOCK (current_function_decl); |
e04a16fb | 6982 | tree b = enter_block (); |
f099f336 | 6983 | BLOCK_EXPR_ORIGIN (b) = body; |
e04a16fb AG |
6984 | } |
6985 | ||
6986 | if (modifier) | |
6987 | { | |
6988 | int i; | |
6989 | for (i = 0; i <= 10; i++) if (1 << i & modifier) break; | |
c877974e | 6990 | if (modifier == ACC_FINAL) |
c2952b01 | 6991 | final_p = 1; |
c877974e APB |
6992 | else |
6993 | { | |
6994 | parse_error_context | |
6995 | (ctxp->modifier_ctx [i], | |
6996 | "Only `final' is allowed as a local variables modifier"); | |
6997 | return; | |
6998 | } | |
e04a16fb AG |
6999 | } |
7000 | ||
c583dd46 APB |
7001 | /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will |
7002 | hold the TYPE value if a new incomplete has to be created (as | |
7003 | opposed to being found already existing and reused). */ | |
7004 | SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain); | |
7005 | ||
7006 | /* If TYPE is fully resolved and we don't have a reference, make one */ | |
1886c9d8 | 7007 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
c583dd46 APB |
7008 | |
7009 | /* Go through all the declared variables */ | |
7010 | for (current = vlist, saved_type = type; current; | |
7011 | current = TREE_CHAIN (current), type = saved_type) | |
e04a16fb | 7012 | { |
c877974e | 7013 | tree other, real_type; |
e04a16fb AG |
7014 | tree wfl = TREE_PURPOSE (current); |
7015 | tree name = EXPR_WFL_NODE (wfl); | |
7016 | tree init = TREE_VALUE (current); | |
e04a16fb | 7017 | |
c583dd46 APB |
7018 | /* Process NAME, as it may specify extra dimension(s) for it */ |
7019 | type = build_array_from_name (type, type_wfl, name, &name); | |
7020 | ||
7021 | /* Variable redefinition check */ | |
7022 | if ((other = lookup_name_in_blocks (name))) | |
7023 | { | |
7024 | variable_redefinition_error (wfl, name, TREE_TYPE (other), | |
7025 | DECL_SOURCE_LINE (other)); | |
7026 | continue; | |
7027 | } | |
7028 | ||
7029 | /* Type adjustment. We may have just readjusted TYPE because | |
7030 | the variable specified more dimensions. Make sure we have | |
7031 | a reference if we can and don't have one already. */ | |
1886c9d8 | 7032 | PROMOTE_RECORD_IF_COMPLETE (type, must_chain); |
c877974e APB |
7033 | |
7034 | real_type = GET_REAL_TYPE (type); | |
c583dd46 APB |
7035 | /* Never layout this decl. This will be done when its scope |
7036 | will be entered */ | |
c877974e | 7037 | decl = build_decl (VAR_DECL, name, real_type); |
c2952b01 | 7038 | LOCAL_FINAL (decl) = final_p; |
c583dd46 APB |
7039 | BLOCK_CHAIN_DECL (decl); |
7040 | ||
d4370213 APB |
7041 | /* If doing xreferencing, replace the line number with the WFL |
7042 | compound value */ | |
7043 | if (flag_emit_xref) | |
7044 | DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl); | |
7045 | ||
e04a16fb AG |
7046 | /* Don't try to use an INIT statement when an error was found */ |
7047 | if (init && java_error_count) | |
7048 | init = NULL_TREE; | |
c583dd46 APB |
7049 | |
7050 | /* Add the initialization function to the current function's code */ | |
7051 | if (init) | |
e04a16fb | 7052 | { |
c583dd46 APB |
7053 | /* Name might have been readjusted */ |
7054 | EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name; | |
7055 | MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1; | |
7056 | java_method_add_stmt (current_function_decl, | |
7057 | build_debugable_stmt (EXPR_WFL_LINECOL (init), | |
7058 | init)); | |
7059 | } | |
7060 | ||
7061 | /* Setup dependency the type of the decl */ | |
7062 | if (must_chain) | |
7063 | { | |
7064 | jdep *dep; | |
7065 | register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type); | |
7066 | dep = CLASSD_LAST (ctxp->classd_list); | |
7067 | JDEP_GET_PATCH (dep) = &TREE_TYPE (decl); | |
e04a16fb AG |
7068 | } |
7069 | } | |
7070 | SOURCE_FRONTEND_DEBUG (("Defined locals")); | |
7071 | } | |
7072 | ||
7073 | /* Called during parsing. Build decls from argument list. */ | |
7074 | ||
7075 | static void | |
7076 | source_start_java_method (fndecl) | |
7077 | tree fndecl; | |
7078 | { | |
7079 | tree tem; | |
7080 | tree parm_decl; | |
7081 | int i; | |
7082 | ||
79d13333 APB |
7083 | if (!fndecl) |
7084 | return; | |
7085 | ||
e04a16fb AG |
7086 | current_function_decl = fndecl; |
7087 | ||
7088 | /* New scope for the function */ | |
7089 | enter_block (); | |
7090 | for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0; | |
de4c7b02 | 7091 | tem != end_params_node; tem = TREE_CHAIN (tem), i++) |
e04a16fb AG |
7092 | { |
7093 | tree type = TREE_VALUE (tem); | |
7094 | tree name = TREE_PURPOSE (tem); | |
7095 | ||
23a79c61 APB |
7096 | /* If type is incomplete. Create an incomplete decl and ask for |
7097 | the decl to be patched later */ | |
e04a16fb AG |
7098 | if (INCOMPLETE_TYPE_P (type)) |
7099 | { | |
7100 | jdep *jdep; | |
c877974e APB |
7101 | tree real_type = GET_REAL_TYPE (type); |
7102 | parm_decl = build_decl (PARM_DECL, name, real_type); | |
23a79c61 | 7103 | type = obtain_incomplete_type (type); |
e04a16fb AG |
7104 | register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type); |
7105 | jdep = CLASSD_LAST (ctxp->classd_list); | |
7106 | JDEP_MISC (jdep) = name; | |
7107 | JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl); | |
7108 | } | |
7109 | else | |
7110 | parm_decl = build_decl (PARM_DECL, name, type); | |
7111 | ||
c2952b01 APB |
7112 | /* Remember if a local variable was declared final (via its |
7113 | TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */ | |
7114 | if (ARG_FINAL_P (tem)) | |
7115 | LOCAL_FINAL (parm_decl) = 1; | |
7116 | ||
e04a16fb AG |
7117 | BLOCK_CHAIN_DECL (parm_decl); |
7118 | } | |
7119 | tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)); | |
7120 | BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) = | |
7121 | nreverse (tem); | |
7122 | DECL_ARG_SLOT_COUNT (current_function_decl) = i; | |
c2952b01 | 7123 | DECL_MAX_LOCALS (current_function_decl) = i; |
e04a16fb AG |
7124 | } |
7125 | ||
22eed1e6 APB |
7126 | /* Called during parsing. Creates an artificial method declaration. */ |
7127 | ||
7128 | static tree | |
7129 | create_artificial_method (class, flags, type, name, args) | |
7130 | tree class; | |
7131 | int flags; | |
7132 | tree type, name, args; | |
7133 | { | |
22eed1e6 APB |
7134 | tree mdecl; |
7135 | ||
c2952b01 | 7136 | java_parser_context_save_global (); |
22eed1e6 APB |
7137 | lineno = 0; |
7138 | mdecl = make_node (FUNCTION_TYPE); | |
7139 | TREE_TYPE (mdecl) = type; | |
7140 | TYPE_ARG_TYPES (mdecl) = args; | |
7141 | mdecl = add_method (class, flags, name, build_java_signature (mdecl)); | |
c2952b01 | 7142 | java_parser_context_restore_global (); |
22eed1e6 APB |
7143 | DECL_ARTIFICIAL (mdecl) = 1; |
7144 | return mdecl; | |
7145 | } | |
7146 | ||
7147 | /* Starts the body if an artifical method. */ | |
7148 | ||
7149 | static void | |
7150 | start_artificial_method_body (mdecl) | |
7151 | tree mdecl; | |
7152 | { | |
7153 | DECL_SOURCE_LINE (mdecl) = 1; | |
7154 | DECL_SOURCE_LINE_MERGE (mdecl, 1); | |
7155 | source_start_java_method (mdecl); | |
7156 | enter_block (); | |
7157 | } | |
7158 | ||
7159 | static void | |
7160 | end_artificial_method_body (mdecl) | |
7161 | tree mdecl; | |
7162 | { | |
7163 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block (); | |
7164 | exit_block (); | |
7165 | } | |
7166 | ||
e04a16fb AG |
7167 | /* Called during expansion. Push decls formerly built from argument |
7168 | list so they're usable during expansion. */ | |
7169 | ||
7170 | static void | |
7171 | expand_start_java_method (fndecl) | |
7172 | tree fndecl; | |
7173 | { | |
7174 | tree tem, *ptr; | |
e04a16fb | 7175 | |
e04a16fb AG |
7176 | current_function_decl = fndecl; |
7177 | ||
c2952b01 APB |
7178 | if (! quiet_flag) |
7179 | fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0)); | |
e04a16fb | 7180 | announce_function (fndecl); |
c2952b01 APB |
7181 | if (! quiet_flag) |
7182 | fprintf (stderr, "]"); | |
7183 | ||
7184 | pushlevel (1); /* Prepare for a parameter push */ | |
e04a16fb AG |
7185 | ptr = &DECL_ARGUMENTS (fndecl); |
7186 | tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)); | |
7187 | while (tem) | |
7188 | { | |
7189 | tree next = TREE_CHAIN (tem); | |
b67d701b | 7190 | tree type = TREE_TYPE (tem); |
e438e1b7 JJ |
7191 | if (PROMOTE_PROTOTYPES |
7192 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node) | |
b67d701b PB |
7193 | && INTEGRAL_TYPE_P (type)) |
7194 | type = integer_type_node; | |
b67d701b | 7195 | DECL_ARG_TYPE (tem) = type; |
e04a16fb AG |
7196 | layout_decl (tem, 0); |
7197 | pushdecl (tem); | |
e04a16fb AG |
7198 | *ptr = tem; |
7199 | ptr = &TREE_CHAIN (tem); | |
7200 | tem = next; | |
7201 | } | |
7202 | *ptr = NULL_TREE; | |
7203 | pushdecl_force_head (DECL_ARGUMENTS (fndecl)); | |
7204 | lineno = DECL_SOURCE_LINE_FIRST (fndecl); | |
e04a16fb AG |
7205 | } |
7206 | ||
7207 | /* Terminate a function and expand its body. */ | |
7208 | ||
7209 | static void | |
7210 | source_end_java_method () | |
7211 | { | |
7212 | tree fndecl = current_function_decl; | |
138657ec | 7213 | int flag_asynchronous_exceptions = asynchronous_exceptions; |
e04a16fb | 7214 | |
79d13333 APB |
7215 | if (!fndecl) |
7216 | return; | |
7217 | ||
e04a16fb AG |
7218 | java_parser_context_save_global (); |
7219 | lineno = ctxp->last_ccb_indent1; | |
7220 | ||
b67d701b PB |
7221 | /* Set EH language codes */ |
7222 | java_set_exception_lang_code (); | |
7223 | ||
5423609c APB |
7224 | /* Turn function bodies with only a NOP expr null, so they don't get |
7225 | generated at all and we won't get warnings when using the -W | |
7226 | -Wall flags. */ | |
7227 | if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node) | |
7228 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE; | |
7229 | ||
e04a16fb AG |
7230 | /* Generate function's code */ |
7231 | if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) | |
e8fc7396 APB |
7232 | && ! flag_emit_class_files |
7233 | && ! flag_emit_xref) | |
e04a16fb AG |
7234 | expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))); |
7235 | ||
7236 | /* pop out of its parameters */ | |
7237 | pushdecl_force_head (DECL_ARGUMENTS (fndecl)); | |
7238 | poplevel (1, 0, 1); | |
7239 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
7240 | ||
7241 | /* Generate rtl for function exit. */ | |
e8fc7396 | 7242 | if (! flag_emit_class_files && ! flag_emit_xref) |
e04a16fb AG |
7243 | { |
7244 | lineno = DECL_SOURCE_LINE_LAST (fndecl); | |
b67d701b PB |
7245 | /* Emit catch-finally clauses */ |
7246 | emit_handlers (); | |
e04a16fb AG |
7247 | expand_function_end (input_filename, lineno, 0); |
7248 | ||
138657ec AH |
7249 | /* FIXME: If the current method contains any exception handlers, |
7250 | force asynchronous_exceptions: this is necessary because signal | |
7251 | handlers in libjava may throw exceptions. This is far from being | |
7252 | a perfect solution, but it's better than doing nothing at all.*/ | |
7253 | if (catch_clauses) | |
7254 | asynchronous_exceptions = 1; | |
7255 | ||
e04a16fb AG |
7256 | /* Run the optimizers and output assembler code for this function. */ |
7257 | rest_of_compilation (fndecl); | |
7258 | } | |
7259 | ||
7260 | current_function_decl = NULL_TREE; | |
e04a16fb | 7261 | java_parser_context_restore_global (); |
138657ec | 7262 | asynchronous_exceptions = flag_asynchronous_exceptions; |
e04a16fb AG |
7263 | } |
7264 | ||
7265 | /* Record EXPR in the current function block. Complements compound | |
7266 | expression second operand if necessary. */ | |
7267 | ||
7268 | tree | |
7269 | java_method_add_stmt (fndecl, expr) | |
7270 | tree fndecl, expr; | |
7271 | { | |
b771925e APB |
7272 | if (!GET_CURRENT_BLOCK (fndecl)) |
7273 | return NULL_TREE; | |
f099f336 | 7274 | return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr); |
b67d701b | 7275 | } |
e04a16fb | 7276 | |
b67d701b PB |
7277 | static tree |
7278 | add_stmt_to_block (b, type, stmt) | |
7279 | tree b, type, stmt; | |
7280 | { | |
7281 | tree body = BLOCK_EXPR_BODY (b), c; | |
7282 | ||
e04a16fb AG |
7283 | if (java_error_count) |
7284 | return body; | |
b67d701b PB |
7285 | |
7286 | if ((c = add_stmt_to_compound (body, type, stmt)) == body) | |
e04a16fb AG |
7287 | return body; |
7288 | ||
b67d701b PB |
7289 | BLOCK_EXPR_BODY (b) = c; |
7290 | TREE_SIDE_EFFECTS (c) = 1; | |
7291 | return c; | |
e04a16fb AG |
7292 | } |
7293 | ||
7294 | /* Add STMT to EXISTING if possible, otherwise create a new | |
7295 | COMPOUND_EXPR and add STMT to it. */ | |
7296 | ||
7297 | static tree | |
7298 | add_stmt_to_compound (existing, type, stmt) | |
7299 | tree existing, type, stmt; | |
7300 | { | |
15fdcfe9 PB |
7301 | if (existing) |
7302 | return build (COMPOUND_EXPR, type, existing, stmt); | |
e04a16fb | 7303 | else |
15fdcfe9 | 7304 | return stmt; |
e04a16fb AG |
7305 | } |
7306 | ||
1886c9d8 APB |
7307 | void java_layout_seen_class_methods () |
7308 | { | |
7309 | tree previous_list = all_class_list; | |
7310 | tree end = NULL_TREE; | |
7311 | tree current; | |
7312 | ||
7313 | while (1) | |
7314 | { | |
7315 | for (current = previous_list; | |
7316 | current != end; current = TREE_CHAIN (current)) | |
7317 | layout_class_methods (TREE_TYPE (TREE_VALUE (current))); | |
7318 | ||
7319 | if (previous_list != all_class_list) | |
7320 | { | |
7321 | end = previous_list; | |
7322 | previous_list = all_class_list; | |
7323 | } | |
7324 | else | |
7325 | break; | |
7326 | } | |
7327 | } | |
7328 | ||
e04a16fb | 7329 | void |
c2952b01 | 7330 | java_reorder_fields () |
e04a16fb | 7331 | { |
c2952b01 | 7332 | static tree stop_reordering = NULL_TREE; |
19e223db | 7333 | static int initialized_p; |
c2952b01 | 7334 | tree current; |
19e223db MM |
7335 | |
7336 | /* Register STOP_REORDERING with the garbage collector. */ | |
7337 | if (!initialized_p) | |
7338 | { | |
7339 | ggc_add_tree_root (&stop_reordering, 1); | |
7340 | initialized_p = 1; | |
7341 | } | |
7342 | ||
5e942c50 | 7343 | for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current)) |
e04a16fb | 7344 | { |
5e942c50 | 7345 | current_class = TREE_TYPE (TREE_VALUE (current)); |
22eed1e6 | 7346 | |
c2952b01 APB |
7347 | if (current_class == stop_reordering) |
7348 | break; | |
7349 | ||
c877974e APB |
7350 | /* Reverse the fields, but leave the dummy field in front. |
7351 | Fields are already ordered for Object and Class */ | |
7352 | if (TYPE_FIELDS (current_class) && current_class != object_type_node | |
7353 | && current_class != class_type_node) | |
7354 | { | |
23a79c61 APB |
7355 | /* If the dummy field is there, reverse the right fields and |
7356 | just layout the type for proper fields offset */ | |
c877974e APB |
7357 | if (!DECL_NAME (TYPE_FIELDS (current_class))) |
7358 | { | |
7359 | tree fields = TYPE_FIELDS (current_class); | |
7360 | TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields)); | |
7361 | TYPE_SIZE (current_class) = NULL_TREE; | |
c877974e | 7362 | } |
23a79c61 APB |
7363 | /* We don't have a dummy field, we need to layout the class, |
7364 | after having reversed the fields */ | |
c877974e APB |
7365 | else |
7366 | { | |
7367 | TYPE_FIELDS (current_class) = | |
7368 | nreverse (TYPE_FIELDS (current_class)); | |
7369 | TYPE_SIZE (current_class) = NULL_TREE; | |
c877974e APB |
7370 | } |
7371 | } | |
c2952b01 APB |
7372 | } |
7373 | stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list)); | |
7374 | } | |
7375 | ||
7376 | /* Layout the methods of all classes loaded in one way on an | |
7377 | other. Check methods of source parsed classes. Then reorder the | |
7378 | fields and layout the classes or the type of all source parsed | |
7379 | classes */ | |
7380 | ||
7381 | void | |
7382 | java_layout_classes () | |
7383 | { | |
7384 | tree current; | |
7385 | int save_error_count = java_error_count; | |
7386 | ||
7387 | /* Layout the methods of all classes seen so far */ | |
7388 | java_layout_seen_class_methods (); | |
7389 | java_parse_abort_on_error (); | |
7390 | all_class_list = NULL_TREE; | |
7391 | ||
7392 | /* Then check the methods of all parsed classes */ | |
7393 | for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current)) | |
7394 | if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current)))) | |
34d4df06 | 7395 | java_check_methods (TREE_VALUE (current)); |
c2952b01 APB |
7396 | java_parse_abort_on_error (); |
7397 | ||
7398 | for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current)) | |
7399 | { | |
7400 | current_class = TREE_TYPE (TREE_VALUE (current)); | |
7401 | layout_class (current_class); | |
5e942c50 | 7402 | |
c877974e APB |
7403 | /* From now on, the class is considered completely loaded */ |
7404 | CLASS_LOADED_P (current_class) = 1; | |
7405 | ||
5e942c50 APB |
7406 | /* Error reported by the caller */ |
7407 | if (java_error_count) | |
7408 | return; | |
e04a16fb | 7409 | } |
23a79c61 APB |
7410 | |
7411 | /* We might have reloaded classes durign the process of laying out | |
7412 | classes for code generation. We must layout the methods of those | |
7413 | late additions, as constructor checks might use them */ | |
1886c9d8 | 7414 | java_layout_seen_class_methods (); |
23a79c61 | 7415 | java_parse_abort_on_error (); |
e04a16fb AG |
7416 | } |
7417 | ||
c2952b01 APB |
7418 | /* Expand methods in the current set of classes rememebered for |
7419 | generation. */ | |
e04a16fb | 7420 | |
49f48c71 | 7421 | static void |
c2952b01 | 7422 | java_complete_expand_classes () |
e04a16fb AG |
7423 | { |
7424 | tree current; | |
ce6e9147 APB |
7425 | |
7426 | do_not_fold = flag_emit_xref; | |
c2952b01 | 7427 | |
e04a16fb | 7428 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) |
c2952b01 APB |
7429 | if (!INNER_CLASS_DECL_P (current)) |
7430 | java_complete_expand_class (current); | |
7431 | } | |
e04a16fb | 7432 | |
c2952b01 APB |
7433 | /* Expand the methods found in OUTER, starting first by OUTER's inner |
7434 | classes, if any. */ | |
e04a16fb | 7435 | |
c2952b01 APB |
7436 | static void |
7437 | java_complete_expand_class (outer) | |
7438 | tree outer; | |
7439 | { | |
7440 | tree inner_list; | |
e04a16fb | 7441 | |
c2952b01 | 7442 | set_nested_class_simple_name_value (outer, 1); /* Set */ |
cd9643f7 | 7443 | |
c2952b01 APB |
7444 | /* We need to go after all inner classes and start expanding them, |
7445 | starting with most nested ones. We have to do that because nested | |
7446 | classes might add functions to outer classes */ | |
e04a16fb | 7447 | |
c2952b01 APB |
7448 | for (inner_list = DECL_INNER_CLASS_LIST (outer); |
7449 | inner_list; inner_list = TREE_CHAIN (inner_list)) | |
7450 | java_complete_expand_class (TREE_PURPOSE (inner_list)); | |
22eed1e6 | 7451 | |
c2952b01 APB |
7452 | java_complete_expand_methods (outer); |
7453 | set_nested_class_simple_name_value (outer, 0); /* Reset */ | |
7454 | } | |
7455 | ||
7456 | /* Expand methods registered in CLASS_DECL. The general idea is that | |
7457 | we expand regular methods first. This allows us get an estimate on | |
7458 | how outer context local alias fields are really used so we can add | |
7459 | to the constructor just enough code to initialize them properly (it | |
c00f0fb2 | 7460 | also lets us generate finit$ correctly.) Then we expand the |
c2952b01 APB |
7461 | constructors and then <clinit>. */ |
7462 | ||
7463 | static void | |
7464 | java_complete_expand_methods (class_decl) | |
7465 | tree class_decl; | |
7466 | { | |
7467 | tree clinit, finit, decl, first_decl; | |
7468 | ||
7469 | current_class = TREE_TYPE (class_decl); | |
7470 | ||
7471 | /* Initialize a new constant pool */ | |
7472 | init_outgoing_cpool (); | |
7473 | ||
7474 | /* Pre-expand <clinit> to figure whether we really need it or | |
7475 | not. If we do need it, we pre-expand the static fields so they're | |
7476 | ready to be used somewhere else. <clinit> will be fully expanded | |
7477 | after we processed the constructors. */ | |
7478 | first_decl = TYPE_METHODS (current_class); | |
7479 | clinit = maybe_generate_pre_expand_clinit (current_class); | |
7480 | ||
c00f0fb2 | 7481 | /* Then generate finit$ (if we need to) because constructor will |
c2952b01 APB |
7482 | try to use it.*/ |
7483 | if (TYPE_FINIT_STMT_LIST (current_class)) | |
7484 | { | |
7485 | finit = generate_finit (current_class); | |
7486 | java_complete_expand_method (finit); | |
7487 | } | |
7488 | ||
7489 | /* Now do the constructors */ | |
7490 | for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl)) | |
7491 | { | |
7492 | int no_body; | |
7493 | ||
7494 | if (!DECL_CONSTRUCTOR_P (decl)) | |
7495 | continue; | |
7496 | ||
7497 | no_body = !DECL_FUNCTION_BODY (decl); | |
7498 | /* Don't generate debug info on line zero when expanding a | |
7499 | generated constructor. */ | |
7500 | if (no_body) | |
7501 | restore_line_number_status (1); | |
7502 | ||
7503 | java_complete_expand_method (decl); | |
7504 | ||
7505 | if (no_body) | |
7506 | restore_line_number_status (0); | |
7507 | } | |
7508 | ||
7509 | /* First, do the ordinary methods. */ | |
7510 | for (decl = first_decl; decl; decl = TREE_CHAIN (decl)) | |
7511 | { | |
7145d9fe TT |
7512 | /* Skip abstract or native methods -- but do handle native |
7513 | methods when generating JNI stubs. */ | |
7514 | if (METHOD_ABSTRACT (decl) | |
7515 | || (! flag_jni && METHOD_NATIVE (decl)) | |
b7805411 | 7516 | || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl)) |
c2952b01 | 7517 | continue; |
7145d9fe TT |
7518 | |
7519 | if (METHOD_NATIVE (decl)) | |
7520 | { | |
7521 | tree body = build_jni_stub (decl); | |
7522 | BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body; | |
7523 | } | |
7524 | ||
c2952b01 APB |
7525 | java_complete_expand_method (decl); |
7526 | } | |
7527 | ||
7528 | /* If there is indeed a <clinit>, fully expand it now */ | |
7529 | if (clinit) | |
7530 | { | |
7531 | /* Prevent the use of `this' inside <clinit> */ | |
7532 | ctxp->explicit_constructor_p = 1; | |
7533 | java_complete_expand_method (clinit); | |
7534 | ctxp->explicit_constructor_p = 0; | |
e04a16fb | 7535 | } |
c2952b01 | 7536 | |
165f37bc APB |
7537 | /* We might have generated a class$ that we now want to expand */ |
7538 | if (TYPE_DOT_CLASS (current_class)) | |
7539 | java_complete_expand_method (TYPE_DOT_CLASS (current_class)); | |
7540 | ||
c2952b01 APB |
7541 | /* Now verify constructor circularity (stop after the first one we |
7542 | prove wrong.) */ | |
7543 | if (!CLASS_INTERFACE (class_decl)) | |
7544 | for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl)) | |
7545 | if (DECL_CONSTRUCTOR_P (decl) | |
7546 | && verify_constructor_circularity (decl, decl)) | |
7547 | break; | |
7548 | ||
7549 | /* Save the constant pool. We'll need to restore it later. */ | |
7550 | TYPE_CPOOL (current_class) = outgoing_cpool; | |
e04a16fb AG |
7551 | } |
7552 | ||
c2952b01 APB |
7553 | /* Attempt to create <clinit>. Pre-expand static fields so they can be |
7554 | safely used in some other methods/constructors. */ | |
e920ebc9 | 7555 | |
c2952b01 APB |
7556 | static tree |
7557 | maybe_generate_pre_expand_clinit (class_type) | |
7558 | tree class_type; | |
e920ebc9 | 7559 | { |
c2952b01 APB |
7560 | tree current, mdecl; |
7561 | ||
7562 | if (!TYPE_CLINIT_STMT_LIST (class_type)) | |
7563 | return NULL_TREE; | |
e920ebc9 | 7564 | |
c2952b01 APB |
7565 | /* Go through all static fields and pre expand them */ |
7566 | for (current = TYPE_FIELDS (class_type); current; | |
7567 | current = TREE_CHAIN (current)) | |
7568 | if (FIELD_STATIC (current)) | |
7569 | build_field_ref (NULL_TREE, class_type, DECL_NAME (current)); | |
7570 | ||
7571 | /* Then build the <clinit> method */ | |
7572 | mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node, | |
7573 | clinit_identifier_node, end_params_node); | |
7574 | layout_class_method (class_type, CLASSTYPE_SUPER (class_type), | |
7575 | mdecl, NULL_TREE); | |
7576 | start_artificial_method_body (mdecl); | |
7577 | ||
7578 | /* We process the list of assignment we produced as the result of | |
7579 | the declaration of initialized static field and add them as | |
7580 | statement to the <clinit> method. */ | |
7581 | for (current = TYPE_CLINIT_STMT_LIST (class_type); current; | |
7582 | current = TREE_CHAIN (current)) | |
e920ebc9 | 7583 | { |
9a7ab4b3 | 7584 | tree stmt = current; |
c2952b01 APB |
7585 | /* We build the assignment expression that will initialize the |
7586 | field to its value. There are strict rules on static | |
7587 | initializers (8.5). FIXME */ | |
98a52c2c | 7588 | if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node) |
9a7ab4b3 | 7589 | stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt); |
c2952b01 APB |
7590 | java_method_add_stmt (mdecl, stmt); |
7591 | } | |
e920ebc9 | 7592 | |
c2952b01 APB |
7593 | end_artificial_method_body (mdecl); |
7594 | ||
92d83515 APB |
7595 | /* Now we want to place <clinit> as the last method (because we need |
7596 | it at least for interface so that it doesn't interfere with the | |
7597 | dispatch table based lookup. */ | |
7598 | if (TREE_CHAIN (TYPE_METHODS (class_type))) | |
c2952b01 | 7599 | { |
92d83515 APB |
7600 | current = TREE_CHAIN (TYPE_METHODS (class_type)); |
7601 | TYPE_METHODS (class_type) = current; | |
c2952b01 APB |
7602 | |
7603 | while (TREE_CHAIN (current)) | |
7604 | current = TREE_CHAIN (current); | |
92d83515 | 7605 | |
c2952b01 APB |
7606 | TREE_CHAIN (current) = mdecl; |
7607 | TREE_CHAIN (mdecl) = NULL_TREE; | |
e920ebc9 | 7608 | } |
c2952b01 APB |
7609 | |
7610 | return mdecl; | |
e920ebc9 APB |
7611 | } |
7612 | ||
dba41d30 APB |
7613 | /* Analyzes a method body and look for something that isn't a |
7614 | MODIFY_EXPR. */ | |
7615 | ||
7616 | static int | |
7617 | analyze_clinit_body (bbody) | |
7618 | tree bbody; | |
7619 | { | |
7620 | while (bbody) | |
7621 | switch (TREE_CODE (bbody)) | |
7622 | { | |
7623 | case BLOCK: | |
7624 | bbody = BLOCK_EXPR_BODY (bbody); | |
7625 | break; | |
7626 | ||
7627 | case EXPR_WITH_FILE_LOCATION: | |
7628 | bbody = EXPR_WFL_NODE (bbody); | |
7629 | break; | |
7630 | ||
7631 | case COMPOUND_EXPR: | |
7632 | if (analyze_clinit_body (TREE_OPERAND (bbody, 0))) | |
7633 | return 1; | |
7634 | bbody = TREE_OPERAND (bbody, 1); | |
7635 | break; | |
7636 | ||
7637 | case MODIFY_EXPR: | |
7638 | bbody = NULL_TREE; | |
7639 | break; | |
7640 | ||
7641 | default: | |
7642 | bbody = NULL_TREE; | |
7643 | return 1; | |
7644 | } | |
7645 | return 0; | |
7646 | } | |
7647 | ||
7648 | ||
92d83515 APB |
7649 | /* See whether we could get rid of <clinit>. Criteria are: all static |
7650 | final fields have constant initial values and the body of <clinit> | |
7651 | is empty. Return 1 if <clinit> was discarded, 0 otherwise. */ | |
7652 | ||
7653 | static int | |
7654 | maybe_yank_clinit (mdecl) | |
7655 | tree mdecl; | |
7656 | { | |
7657 | tree type, current; | |
7658 | tree fbody, bbody; | |
99eaf8d4 | 7659 | int found = 0; |
92d83515 APB |
7660 | |
7661 | if (!DECL_CLINIT_P (mdecl)) | |
7662 | return 0; | |
f0f3a777 APB |
7663 | |
7664 | /* If the body isn't empty, then we keep <clinit>. Note that if | |
7665 | we're emitting classfiles, this isn't enough not to rule it | |
7666 | out. */ | |
92d83515 | 7667 | fbody = DECL_FUNCTION_BODY (mdecl); |
dba41d30 APB |
7668 | bbody = BLOCK_EXPR_BODY (fbody); |
7669 | if (bbody && bbody != error_mark_node) | |
92d83515 | 7670 | bbody = BLOCK_EXPR_BODY (bbody); |
dba41d30 APB |
7671 | else |
7672 | return 0; | |
f0f3a777 | 7673 | if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node) |
92d83515 APB |
7674 | return 0; |
7675 | ||
7676 | type = DECL_CONTEXT (mdecl); | |
7677 | current = TYPE_FIELDS (type); | |
7678 | ||
7679 | for (current = (current ? TREE_CHAIN (current) : current); | |
7680 | current; current = TREE_CHAIN (current)) | |
f0f3a777 APB |
7681 | { |
7682 | tree f_init; | |
7683 | ||
7684 | /* We're not interested in non static field */ | |
7685 | if (!FIELD_STATIC (current)) | |
7686 | continue; | |
7687 | ||
7688 | /* Anything that isn't String or a basic type is ruled out -- or | |
7689 | if we now how to deal with it (when doing things natively) we | |
7690 | should generated an empty <clinit> so that SUID are computed | |
7691 | correctly. */ | |
7692 | if (! JSTRING_TYPE_P (TREE_TYPE (current)) | |
7693 | && ! JNUMERIC_TYPE_P (TREE_TYPE (current))) | |
7694 | break; | |
7695 | ||
7696 | f_init = DECL_INITIAL (current); | |
7697 | /* If we're emitting native code, we want static final fields to | |
7698 | have constant initializers. If we don't meet these | |
7699 | conditions, we keep <clinit> */ | |
7700 | if (!flag_emit_class_files | |
7701 | && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init))) | |
7702 | break; | |
7703 | /* If we're emitting bytecode, we want static fields to have | |
7704 | constant initializers or no initializer. If we don't meet | |
7705 | these conditions, we keep <clinit> */ | |
7706 | if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init)) | |
7707 | break; | |
7708 | } | |
92d83515 | 7709 | |
99eaf8d4 APB |
7710 | /* Now we analyze the method body and look for something that |
7711 | isn't a MODIFY_EXPR */ | |
7712 | if (bbody == empty_stmt_node) | |
dba41d30 APB |
7713 | found = 0; |
7714 | else | |
7715 | found = analyze_clinit_body (bbody); | |
99eaf8d4 APB |
7716 | |
7717 | if (current || found) | |
92d83515 APB |
7718 | return 0; |
7719 | ||
7720 | /* Get rid of <clinit> in the class' list of methods */ | |
7721 | if (TYPE_METHODS (type) == mdecl) | |
7722 | TYPE_METHODS (type) = TREE_CHAIN (mdecl); | |
7723 | else | |
7724 | for (current = TYPE_METHODS (type); current; | |
7725 | current = TREE_CHAIN (current)) | |
7726 | if (TREE_CHAIN (current) == mdecl) | |
7727 | { | |
7728 | TREE_CHAIN (current) = TREE_CHAIN (mdecl); | |
7729 | break; | |
7730 | } | |
7731 | ||
7732 | return 1; | |
7733 | } | |
7734 | ||
7735 | ||
e04a16fb AG |
7736 | /* Complete and expand a method. */ |
7737 | ||
7738 | static void | |
7739 | java_complete_expand_method (mdecl) | |
7740 | tree mdecl; | |
7741 | { | |
92d83515 APB |
7742 | int yank_clinit = 0; |
7743 | ||
c2952b01 | 7744 | current_function_decl = mdecl; |
22eed1e6 APB |
7745 | /* Fix constructors before expanding them */ |
7746 | if (DECL_CONSTRUCTOR_P (mdecl)) | |
7747 | fix_constructors (mdecl); | |
e04a16fb | 7748 | |
22eed1e6 | 7749 | /* Expand functions that have a body */ |
e04a16fb AG |
7750 | if (DECL_FUNCTION_BODY (mdecl)) |
7751 | { | |
9bbc7d9f PB |
7752 | tree fbody = DECL_FUNCTION_BODY (mdecl); |
7753 | tree block_body = BLOCK_EXPR_BODY (fbody); | |
cd531a2e | 7754 | tree exception_copy = NULL_TREE; |
e04a16fb | 7755 | expand_start_java_method (mdecl); |
939d7216 | 7756 | build_result_decl (mdecl); |
e04a16fb AG |
7757 | |
7758 | current_this | |
7759 | = (!METHOD_STATIC (mdecl) ? | |
7760 | BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE); | |
7761 | ||
ce6e9147 APB |
7762 | /* Purge the `throws' list of unchecked exceptions. If we're |
7763 | doing xref, save a copy of the list and re-install it | |
7764 | later. */ | |
7765 | if (flag_emit_xref) | |
7766 | exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl)); | |
7767 | ||
b9f7e36c APB |
7768 | purge_unchecked_exceptions (mdecl); |
7769 | ||
7770 | /* Install exceptions thrown with `throws' */ | |
7771 | PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl)); | |
7772 | ||
9bbc7d9f | 7773 | if (block_body != NULL_TREE) |
bc3ca41b PB |
7774 | { |
7775 | block_body = java_complete_tree (block_body); | |
c2952b01 | 7776 | |
7145d9fe | 7777 | if (! flag_emit_xref && ! METHOD_NATIVE (mdecl)) |
ce6e9147 | 7778 | check_for_initialization (block_body); |
f099f336 | 7779 | ctxp->explicit_constructor_p = 0; |
bc3ca41b | 7780 | } |
e803d3b2 | 7781 | |
9bbc7d9f | 7782 | BLOCK_EXPR_BODY (fbody) = block_body; |
5e942c50 | 7783 | |
c2952b01 APB |
7784 | /* If we saw a return but couldn't evaluate it properly, we'll |
7785 | have an error_mark_node here. */ | |
7786 | if (block_body != error_mark_node | |
7787 | && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body)) | |
ce6e9147 APB |
7788 | && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE |
7789 | && !flag_emit_xref) | |
82371d41 | 7790 | missing_return_error (current_function_decl); |
7525cc04 | 7791 | |
92d83515 APB |
7792 | /* Check wether we could just get rid of clinit, now the picture |
7793 | is complete. */ | |
7794 | if (!(yank_clinit = maybe_yank_clinit (mdecl))) | |
7795 | complete_start_java_method (mdecl); | |
7796 | ||
e04a16fb | 7797 | /* Don't go any further if we've found error(s) during the |
92d83515 APB |
7798 | expansion */ |
7799 | if (!java_error_count && !yank_clinit) | |
e04a16fb | 7800 | source_end_java_method (); |
22eed1e6 APB |
7801 | else |
7802 | { | |
92d83515 APB |
7803 | if (java_error_count) |
7804 | pushdecl_force_head (DECL_ARGUMENTS (mdecl)); | |
22eed1e6 APB |
7805 | poplevel (1, 0, 1); |
7806 | } | |
b9f7e36c APB |
7807 | |
7808 | /* Pop the exceptions and sanity check */ | |
7809 | POP_EXCEPTIONS(); | |
7810 | if (currently_caught_type_list) | |
7811 | fatal ("Exception list non empty - java_complete_expand_method"); | |
ce6e9147 APB |
7812 | |
7813 | if (flag_emit_xref) | |
7814 | DECL_FUNCTION_THROWS (mdecl) = exception_copy; | |
e04a16fb AG |
7815 | } |
7816 | } | |
7817 | ||
c2952b01 APB |
7818 | \f |
7819 | ||
7820 | /* This section of the code deals with accessing enclosing context | |
7821 | fields either directly by using the relevant access to this$<n> or | |
7822 | by invoking an access method crafted for that purpose. */ | |
7823 | ||
7824 | /* Build the necessary access from an inner class to an outer | |
7825 | class. This routine could be optimized to cache previous result | |
7826 | (decl, current_class and returned access). When an access method | |
7827 | needs to be generated, it always takes the form of a read. It might | |
7828 | be later turned into a write by calling outer_field_access_fix. */ | |
7829 | ||
7830 | static tree | |
7831 | build_outer_field_access (id, decl) | |
7832 | tree id, decl; | |
7833 | { | |
7834 | tree access = NULL_TREE; | |
7835 | tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class))); | |
7836 | ||
7837 | /* If decl's class is the direct outer class of the current_class, | |
f0f3a777 | 7838 | build the access as `this$<n>.<field>'. Note that we will break |
c2952b01 APB |
7839 | the `private' barrier if we're not emitting bytecodes. */ |
7840 | if (ctx == DECL_CONTEXT (decl) | |
7841 | && (!FIELD_PRIVATE (decl) || !flag_emit_class_files )) | |
7842 | { | |
7843 | tree thisn = build_current_thisn (current_class); | |
7844 | access = make_qualified_primary (build_wfl_node (thisn), | |
7845 | id, EXPR_WFL_LINECOL (id)); | |
7846 | } | |
7847 | /* Otherwise, generate access methods to outer this and access the | |
7848 | field (either using an access method or by direct access.) */ | |
7849 | else | |
7850 | { | |
7851 | int lc = EXPR_WFL_LINECOL (id); | |
7852 | ||
7853 | /* Now we chain the required number of calls to the access$0 to | |
f0f3a777 | 7854 | get a hold to the enclosing instance we need, and then we |
c2952b01 | 7855 | build the field access. */ |
dba41d30 | 7856 | access = build_access_to_thisn (current_class, DECL_CONTEXT (decl), lc); |
c2952b01 APB |
7857 | |
7858 | /* If the field is private and we're generating bytecode, then | |
7859 | we generate an access method */ | |
7860 | if (FIELD_PRIVATE (decl) && flag_emit_class_files ) | |
7861 | { | |
7862 | tree name = build_outer_field_access_methods (decl); | |
7863 | access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl), | |
7864 | name, access, NULL_TREE); | |
7865 | } | |
7866 | /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'. | |
7867 | Once again we break the `private' access rule from a foreign | |
7868 | class. */ | |
7869 | else | |
7870 | access = make_qualified_primary (access, id, lc); | |
7871 | } | |
7872 | return resolve_expression_name (access, NULL); | |
7873 | } | |
7874 | ||
7875 | /* Return a non zero value if NODE describes an outer field inner | |
7876 | access. */ | |
7877 | ||
7878 | static int | |
7879 | outer_field_access_p (type, decl) | |
7880 | tree type, decl; | |
7881 | { | |
7882 | if (!INNER_CLASS_TYPE_P (type) | |
7883 | || TREE_CODE (decl) != FIELD_DECL | |
7884 | || DECL_CONTEXT (decl) == type) | |
7885 | return 0; | |
7886 | ||
7887 | for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ; | |
7888 | type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type)))) | |
7889 | { | |
7890 | if (type == DECL_CONTEXT (decl)) | |
7891 | return 1; | |
7892 | if (!DECL_CONTEXT (TYPE_NAME (type))) | |
7893 | break; | |
7894 | } | |
7895 | ||
7896 | return 0; | |
7897 | } | |
7898 | ||
7899 | /* Return a non zero value if NODE represents an outer field inner | |
7900 | access that was been already expanded. As a side effect, it returns | |
7901 | the name of the field being accessed and the argument passed to the | |
7902 | access function, suitable for a regeneration of the access method | |
7903 | call if necessary. */ | |
7904 | ||
7905 | static int | |
7906 | outer_field_expanded_access_p (node, name, arg_type, arg) | |
7907 | tree node, *name, *arg_type, *arg; | |
7908 | { | |
7909 | int identified = 0; | |
7910 | ||
7911 | if (TREE_CODE (node) != CALL_EXPR) | |
7912 | return 0; | |
7913 | ||
7914 | /* Well, gcj generates slightly different tree nodes when compiling | |
7915 | to native or bytecodes. It's the case for function calls. */ | |
7916 | ||
7917 | if (flag_emit_class_files | |
7918 | && TREE_CODE (node) == CALL_EXPR | |
7919 | && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0)))) | |
7920 | identified = 1; | |
7921 | else if (!flag_emit_class_files) | |
7922 | { | |
7923 | node = TREE_OPERAND (node, 0); | |
7924 | ||
7925 | if (node && TREE_OPERAND (node, 0) | |
7926 | && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR) | |
7927 | { | |
7928 | node = TREE_OPERAND (node, 0); | |
7929 | if (TREE_OPERAND (node, 0) | |
7930 | && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL | |
7931 | && (OUTER_FIELD_ACCESS_IDENTIFIER_P | |
7932 | (DECL_NAME (TREE_OPERAND (node, 0))))) | |
7933 | identified = 1; | |
7934 | } | |
7935 | } | |
7936 | ||
7937 | if (identified && name && arg_type && arg) | |
7938 | { | |
7939 | tree argument = TREE_OPERAND (node, 1); | |
7940 | *name = DECL_NAME (TREE_OPERAND (node, 0)); | |
7941 | *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument))); | |
7942 | *arg = TREE_VALUE (argument); | |
7943 | } | |
7944 | return identified; | |
7945 | } | |
7946 | ||
7947 | /* Detect in NODE an outer field read access from an inner class and | |
7948 | transform it into a write with RHS as an argument. This function is | |
7949 | called from the java_complete_lhs when an assignment to a LHS can | |
7950 | be identified. */ | |
7951 | ||
7952 | static tree | |
7953 | outer_field_access_fix (wfl, node, rhs) | |
7954 | tree wfl, node, rhs; | |
7955 | { | |
7956 | tree name, arg_type, arg; | |
7957 | ||
7958 | if (outer_field_expanded_access_p (node, &name, &arg_type, &arg)) | |
7959 | { | |
7960 | /* At any rate, check whether we're trying to assign a value to | |
7961 | a final. */ | |
7962 | tree accessed = (JDECL_P (node) ? node : | |
7963 | (TREE_CODE (node) == COMPONENT_REF ? | |
7964 | TREE_OPERAND (node, 1) : node)); | |
7965 | if (check_final_assignment (accessed, wfl)) | |
7966 | return error_mark_node; | |
7967 | ||
7968 | node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl), | |
7969 | arg_type, name, arg, rhs); | |
7970 | return java_complete_tree (node); | |
7971 | } | |
7972 | return NULL_TREE; | |
7973 | } | |
7974 | ||
7975 | /* Construct the expression that calls an access method: | |
7976 | <type>.access$<n>(<arg1> [, <arg2>]); | |
7977 | ||
7978 | ARG2 can be NULL and will be omitted in that case. It will denote a | |
7979 | read access. */ | |
7980 | ||
7981 | static tree | |
7982 | build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2) | |
7983 | int lc; | |
7984 | tree type, access_method_name, arg1, arg2; | |
7985 | { | |
7986 | tree args, cn, access; | |
7987 | ||
7988 | args = arg1 ? arg1 : | |
7989 | build_wfl_node (build_current_thisn (current_class)); | |
7990 | args = build_tree_list (NULL_TREE, args); | |
7991 | ||
7992 | if (arg2) | |
7993 | args = tree_cons (NULL_TREE, arg2, args); | |
7994 | ||
7995 | access = build_method_invocation (build_wfl_node (access_method_name), args); | |
7996 | cn = build_wfl_node (DECL_NAME (TYPE_NAME (type))); | |
7997 | return make_qualified_primary (cn, access, lc); | |
7998 | } | |
7999 | ||
8000 | static tree | |
8001 | build_new_access_id () | |
8002 | { | |
8003 | static int access_n_counter = 1; | |
8004 | char buffer [128]; | |
8005 | ||
8006 | sprintf (buffer, "access$%d", access_n_counter++); | |
8007 | return get_identifier (buffer); | |
8008 | } | |
8009 | ||
8010 | /* Create the static access functions for the outer field DECL. We define a | |
8011 | read: | |
8012 | TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) { | |
8013 | return inst$.field; | |
8014 | } | |
8015 | and a write access: | |
8016 | TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$, | |
8017 | TREE_TYPE (<field>) value$) { | |
8018 | return inst$.field = value$; | |
8019 | } | |
8020 | We should have a usage flags on the DECL so we can lazily turn the ones | |
8021 | we're using for code generation. FIXME. | |
8022 | */ | |
8023 | ||
8024 | static tree | |
8025 | build_outer_field_access_methods (decl) | |
8026 | tree decl; | |
8027 | { | |
8028 | tree id, args, stmt, mdecl; | |
8029 | ||
8030 | /* Check point, to be removed. FIXME */ | |
8031 | if (FIELD_INNER_ACCESS (decl) | |
8032 | && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE) | |
8033 | abort (); | |
8034 | ||
8035 | if (FIELD_INNER_ACCESS (decl)) | |
8036 | return FIELD_INNER_ACCESS (decl); | |
8037 | ||
c2952b01 APB |
8038 | /* Create the identifier and a function named after it. */ |
8039 | id = build_new_access_id (); | |
8040 | ||
8041 | /* The identifier is marked as bearing the name of a generated write | |
8042 | access function for outer field accessed from inner classes. */ | |
8043 | OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1; | |
8044 | ||
8045 | /* Create the read access */ | |
8046 | args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl))); | |
8047 | TREE_CHAIN (args) = end_params_node; | |
8048 | stmt = make_qualified_primary (build_wfl_node (inst_id), | |
8049 | build_wfl_node (DECL_NAME (decl)), 0); | |
8050 | stmt = build_return (0, stmt); | |
8051 | mdecl = build_outer_field_access_method (DECL_CONTEXT (decl), | |
8052 | TREE_TYPE (decl), id, args, stmt); | |
8053 | DECL_FUNCTION_ACCESS_DECL (mdecl) = decl; | |
8054 | ||
8055 | /* Create the write access method */ | |
8056 | args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl))); | |
8057 | TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl)); | |
8058 | TREE_CHAIN (TREE_CHAIN (args)) = end_params_node; | |
8059 | stmt = make_qualified_primary (build_wfl_node (inst_id), | |
8060 | build_wfl_node (DECL_NAME (decl)), 0); | |
8061 | stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt, | |
8062 | build_wfl_node (wpv_id))); | |
8063 | ||
8064 | mdecl = build_outer_field_access_method (DECL_CONTEXT (decl), | |
8065 | TREE_TYPE (decl), id, args, stmt); | |
8066 | DECL_FUNCTION_ACCESS_DECL (mdecl) = decl; | |
c2952b01 APB |
8067 | |
8068 | /* Return the access name */ | |
8069 | return FIELD_INNER_ACCESS (decl) = id; | |
8070 | } | |
8071 | ||
8072 | /* Build an field access method NAME. */ | |
8073 | ||
8074 | static tree | |
8075 | build_outer_field_access_method (class, type, name, args, body) | |
8076 | tree class, type, name, args, body; | |
8077 | { | |
8078 | tree saved_current_function_decl, mdecl; | |
8079 | ||
8080 | /* Create the method */ | |
8081 | mdecl = create_artificial_method (class, ACC_STATIC, type, name, args); | |
8082 | fix_method_argument_names (args, mdecl); | |
8083 | layout_class_method (class, NULL_TREE, mdecl, NULL_TREE); | |
8084 | ||
8085 | /* Attach the method body. */ | |
8086 | saved_current_function_decl = current_function_decl; | |
8087 | start_artificial_method_body (mdecl); | |
8088 | java_method_add_stmt (mdecl, body); | |
8089 | end_artificial_method_body (mdecl); | |
8090 | current_function_decl = saved_current_function_decl; | |
8091 | ||
8092 | return mdecl; | |
8093 | } | |
8094 | ||
8095 | \f | |
8096 | /* This section deals with building access function necessary for | |
8097 | certain kinds of method invocation from inner classes. */ | |
8098 | ||
8099 | static tree | |
8100 | build_outer_method_access_method (decl) | |
8101 | tree decl; | |
8102 | { | |
8103 | tree saved_current_function_decl, mdecl; | |
8104 | tree args = NULL_TREE, call_args = NULL_TREE; | |
8105 | tree carg, id, body, class; | |
8106 | char buffer [80]; | |
8107 | int parm_id_count = 0; | |
8108 | ||
8109 | /* Test this abort with an access to a private field */ | |
8110 | if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$")) | |
8111 | abort (); | |
8112 | ||
8113 | /* Check the cache first */ | |
8114 | if (DECL_FUNCTION_INNER_ACCESS (decl)) | |
8115 | return DECL_FUNCTION_INNER_ACCESS (decl); | |
8116 | ||
8117 | class = DECL_CONTEXT (decl); | |
8118 | ||
8119 | /* Obtain an access identifier and mark it */ | |
8120 | id = build_new_access_id (); | |
8121 | OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1; | |
8122 | ||
c2952b01 APB |
8123 | carg = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
8124 | /* Create the arguments, as much as the original */ | |
8125 | for (; carg && carg != end_params_node; | |
8126 | carg = TREE_CHAIN (carg)) | |
8127 | { | |
8128 | sprintf (buffer, "write_parm_value$%d", parm_id_count++); | |
8129 | args = chainon (args, build_tree_list (get_identifier (buffer), | |
8130 | TREE_VALUE (carg))); | |
8131 | } | |
8132 | args = chainon (args, end_params_node); | |
8133 | ||
8134 | /* Create the method */ | |
8135 | mdecl = create_artificial_method (class, ACC_STATIC, | |
8136 | TREE_TYPE (TREE_TYPE (decl)), id, args); | |
8137 | layout_class_method (class, NULL_TREE, mdecl, NULL_TREE); | |
8138 | /* There is a potential bug here. We should be able to use | |
8139 | fix_method_argument_names, but then arg names get mixed up and | |
8140 | eventually a constructor will have its this$0 altered and the | |
8141 | outer context won't be assignment properly. The test case is | |
8142 | stub.java FIXME */ | |
8143 | TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args; | |
8144 | ||
8145 | /* Attach the method body. */ | |
8146 | saved_current_function_decl = current_function_decl; | |
8147 | start_artificial_method_body (mdecl); | |
8148 | ||
8149 | /* The actual method invocation uses the same args. When invoking a | |
8150 | static methods that way, we don't want to skip the first | |
8151 | argument. */ | |
8152 | carg = args; | |
8153 | if (!METHOD_STATIC (decl)) | |
8154 | carg = TREE_CHAIN (carg); | |
8155 | for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg)) | |
8156 | call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)), | |
8157 | call_args); | |
8158 | ||
8159 | body = build_method_invocation (build_wfl_node (DECL_NAME (decl)), | |
8160 | call_args); | |
8161 | if (!METHOD_STATIC (decl)) | |
8162 | body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)), | |
8163 | body, 0); | |
8164 | if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node) | |
8165 | body = build_return (0, body); | |
8166 | java_method_add_stmt (mdecl,body); | |
8167 | end_artificial_method_body (mdecl); | |
8168 | current_function_decl = saved_current_function_decl; | |
c2952b01 APB |
8169 | |
8170 | /* Back tag the access function so it know what it accesses */ | |
8171 | DECL_FUNCTION_ACCESS_DECL (decl) = mdecl; | |
8172 | ||
8173 | /* Tag the current method so it knows it has an access generated */ | |
8174 | return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl; | |
8175 | } | |
8176 | ||
8177 | \f | |
8178 | /* This section of the code deals with building expressions to access | |
8179 | the enclosing instance of an inner class. The enclosing instance is | |
8180 | kept in a generated field called this$<n>, with <n> being the | |
8181 | inner class nesting level (starting from 0.) */ | |
8182 | ||
dba41d30 APB |
8183 | /* Build an access to a given this$<n>, always chaining access call to |
8184 | others. Access methods to this$<n> are build on the fly if | |
8185 | necessary. This CAN'T be used to solely access this$<n-1> from | |
8186 | this$<n> (which alway yield to special cases and optimization, see | |
8187 | for example build_outer_field_access). */ | |
c2952b01 APB |
8188 | |
8189 | static tree | |
8190 | build_access_to_thisn (from, to, lc) | |
8191 | tree from, to; | |
8192 | int lc; | |
8193 | { | |
8194 | tree access = NULL_TREE; | |
8195 | ||
8196 | while (from != to) | |
8197 | { | |
c2952b01 | 8198 | if (!access) |
dba41d30 APB |
8199 | { |
8200 | access = build_current_thisn (from); | |
8201 | access = build_wfl_node (access); | |
8202 | } | |
8203 | else | |
c2952b01 | 8204 | { |
dba41d30 APB |
8205 | tree access0_wfl, cn; |
8206 | ||
8207 | maybe_build_thisn_access_method (from); | |
8208 | access0_wfl = build_wfl_node (access0_identifier_node); | |
8209 | cn = build_wfl_node (DECL_NAME (TYPE_NAME (from))); | |
8210 | EXPR_WFL_LINECOL (access0_wfl) = lc; | |
8211 | access = build_tree_list (NULL_TREE, access); | |
8212 | access = build_method_invocation (access0_wfl, access); | |
8213 | access = make_qualified_primary (cn, access, lc); | |
c2952b01 | 8214 | } |
c2952b01 APB |
8215 | |
8216 | from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from))); | |
8217 | } | |
8218 | return access; | |
8219 | } | |
8220 | ||
8221 | /* Build an access function to the this$<n> local to TYPE. NULL_TREE | |
8222 | is returned if nothing needs to be generated. Otherwise, the method | |
152de068 | 8223 | generated and a method decl is returned. |
c2952b01 APB |
8224 | |
8225 | NOTE: These generated methods should be declared in a class file | |
8226 | attribute so that they can't be referred to directly. */ | |
8227 | ||
8228 | static tree | |
8229 | maybe_build_thisn_access_method (type) | |
8230 | tree type; | |
8231 | { | |
8232 | tree mdecl, args, stmt, rtype; | |
8233 | tree saved_current_function_decl; | |
8234 | ||
8235 | /* If TYPE is a top-level class, no access method is required. | |
8236 | If there already is such an access method, bail out. */ | |
8237 | if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type)) | |
8238 | return NULL_TREE; | |
8239 | ||
8240 | /* We generate the method. The method looks like: | |
8241 | static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; } | |
8242 | */ | |
c2952b01 APB |
8243 | args = build_tree_list (inst_id, build_pointer_type (type)); |
8244 | TREE_CHAIN (args) = end_params_node; | |
8245 | rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type)))); | |
8246 | mdecl = create_artificial_method (type, ACC_STATIC, rtype, | |
8247 | access0_identifier_node, args); | |
8248 | fix_method_argument_names (args, mdecl); | |
8249 | layout_class_method (type, NULL_TREE, mdecl, NULL_TREE); | |
8250 | stmt = build_current_thisn (type); | |
8251 | stmt = make_qualified_primary (build_wfl_node (inst_id), | |
8252 | build_wfl_node (stmt), 0); | |
8253 | stmt = build_return (0, stmt); | |
8254 | ||
8255 | saved_current_function_decl = current_function_decl; | |
8256 | start_artificial_method_body (mdecl); | |
8257 | java_method_add_stmt (mdecl, stmt); | |
8258 | end_artificial_method_body (mdecl); | |
8259 | current_function_decl = saved_current_function_decl; | |
c2952b01 APB |
8260 | |
8261 | CLASS_ACCESS0_GENERATED_P (type) = 1; | |
8262 | ||
8263 | return mdecl; | |
8264 | } | |
8265 | ||
8266 | /* Craft an correctly numbered `this$<n>'string. this$0 is used for | |
8267 | the first level of innerclassing. this$1 for the next one, etc... | |
8268 | This function can be invoked with TYPE to NULL, available and then | |
8269 | has to count the parser context. */ | |
8270 | ||
8271 | static tree | |
8272 | build_current_thisn (type) | |
8273 | tree type; | |
8274 | { | |
8275 | static int saved_i = -1; | |
8276 | static tree saved_thisn = NULL_TREE; | |
19e223db MM |
8277 | static tree saved_type = NULL_TREE; |
8278 | static int saved_type_i = 0; | |
8279 | static int initialized_p; | |
c2952b01 APB |
8280 | tree decl; |
8281 | char buffer [80]; | |
8282 | int i = 0; | |
8283 | ||
19e223db MM |
8284 | /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */ |
8285 | if (!initialized_p) | |
c2952b01 | 8286 | { |
19e223db MM |
8287 | ggc_add_tree_root (&saved_thisn, 1); |
8288 | ggc_add_tree_root (&saved_type, 1); | |
8289 | initialized_p = 1; | |
8290 | } | |
c2952b01 | 8291 | |
19e223db MM |
8292 | if (type) |
8293 | { | |
c2952b01 APB |
8294 | if (type == saved_type) |
8295 | i = saved_type_i; | |
8296 | else | |
8297 | { | |
8298 | for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type)); | |
8299 | decl; decl = DECL_CONTEXT (decl), i++) | |
8300 | ; | |
8301 | ||
8302 | saved_type = type; | |
8303 | saved_type_i = i; | |
8304 | } | |
8305 | } | |
8306 | else | |
8307 | i = list_length (GET_CPC_LIST ())-2; | |
8308 | ||
8309 | if (i == saved_i) | |
8310 | return saved_thisn; | |
8311 | ||
8312 | sprintf (buffer, "this$%d", i); | |
8313 | saved_i = i; | |
8314 | saved_thisn = get_identifier (buffer); | |
8315 | return saved_thisn; | |
8316 | } | |
8317 | ||
8318 | /* Return the assignement to the hidden enclosing context `this$<n>' | |
8319 | by the second incoming parameter to the innerclass constructor. The | |
8320 | form used is `this.this$<n> = this$<n>;'. */ | |
8321 | ||
8322 | static tree | |
8323 | build_thisn_assign () | |
8324 | { | |
8325 | if (current_class && PURE_INNER_CLASS_TYPE_P (current_class)) | |
8326 | { | |
8327 | tree thisn = build_current_thisn (current_class); | |
8328 | tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node), | |
8329 | build_wfl_node (thisn), 0); | |
8330 | tree rhs = build_wfl_node (thisn); | |
8331 | EXPR_WFL_SET_LINECOL (lhs, lineno, 0); | |
8332 | return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs); | |
8333 | } | |
8334 | return NULL_TREE; | |
8335 | } | |
8336 | ||
8337 | \f | |
165f37bc APB |
8338 | /* Building the synthetic `class$' used to implement the `.class' 1.1 |
8339 | extension for non primitive types. This method looks like: | |
8340 | ||
8341 | static Class class$(String type) throws NoClassDefFoundError | |
8342 | { | |
8343 | try {return (java.lang.Class.forName (String));} | |
8344 | catch (ClassNotFoundException e) { | |
8345 | throw new NoClassDefFoundError(e.getMessage());} | |
8346 | } */ | |
8347 | ||
8348 | static tree | |
8349 | build_dot_class_method (class) | |
8350 | tree class; | |
8351 | { | |
8352 | #define BWF(S) build_wfl_node (get_identifier ((S))) | |
8353 | #define MQN(X,Y) make_qualified_name ((X), (Y), 0) | |
8354 | tree args, tmp, saved_current_function_decl, mdecl; | |
8355 | tree stmt, throw_stmt, catch, catch_block, try_block; | |
8356 | tree catch_clause_param; | |
8357 | tree class_not_found_exception, no_class_def_found_error; | |
8358 | ||
8359 | static tree get_message_wfl, type_parm_wfl; | |
8360 | ||
8361 | if (!get_message_wfl) | |
8362 | { | |
8363 | get_message_wfl = build_wfl_node (get_identifier ("getMessage")); | |
8364 | type_parm_wfl = build_wfl_node (get_identifier ("type$")); | |
19e223db MM |
8365 | ggc_add_tree_root (&get_message_wfl, 1); |
8366 | ggc_add_tree_root (&type_parm_wfl, 1); | |
165f37bc APB |
8367 | } |
8368 | ||
8369 | /* Build the arguments */ | |
8370 | args = build_tree_list (get_identifier ("type$"), | |
8371 | build_pointer_type (string_type_node)); | |
8372 | TREE_CHAIN (args) = end_params_node; | |
8373 | ||
8374 | /* Build the qualified name java.lang.Class.forName */ | |
8375 | tmp = MQN (MQN (MQN (BWF ("java"), | |
8376 | BWF ("lang")), BWF ("Class")), BWF ("forName")); | |
8377 | ||
8378 | /* For things we have to catch and throw */ | |
8379 | class_not_found_exception = | |
8380 | lookup_class (get_identifier ("java.lang.ClassNotFoundException")); | |
8381 | no_class_def_found_error = | |
8382 | lookup_class (get_identifier ("java.lang.NoClassDefFoundError")); | |
8383 | load_class (class_not_found_exception, 1); | |
8384 | load_class (no_class_def_found_error, 1); | |
8385 | ||
8386 | /* Create the "class$" function */ | |
8387 | mdecl = create_artificial_method (class, ACC_STATIC, | |
8388 | build_pointer_type (class_type_node), | |
8389 | get_identifier ("class$"), args); | |
8390 | DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, | |
8391 | no_class_def_found_error); | |
8392 | ||
8393 | /* We start by building the try block. We need to build: | |
8394 | return (java.lang.Class.forName (type)); */ | |
8395 | stmt = build_method_invocation (tmp, | |
8396 | build_tree_list (NULL_TREE, type_parm_wfl)); | |
8397 | stmt = build_return (0, stmt); | |
8398 | /* Put it in a block. That's the try block */ | |
8399 | try_block = build_expr_block (stmt, NULL_TREE); | |
8400 | ||
8401 | /* Now onto the catch block. We start by building the expression | |
8402 | throwing a new exception: | |
8403 | throw new NoClassDefFoundError (_.getMessage); */ | |
8404 | throw_stmt = make_qualified_name (build_wfl_node (wpv_id), | |
8405 | get_message_wfl, 0); | |
8406 | throw_stmt = build_method_invocation (throw_stmt, NULL_TREE); | |
8407 | ||
8408 | /* Build new NoClassDefFoundError (_.getMessage) */ | |
8409 | throw_stmt = build_new_invocation | |
8410 | (build_wfl_node (get_identifier ("NoClassDefFoundError")), | |
8411 | build_tree_list (build_pointer_type (string_type_node), throw_stmt)); | |
8412 | ||
8413 | /* Build the throw, (it's too early to use BUILD_THROW) */ | |
8414 | throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt); | |
8415 | ||
8416 | /* Build the catch block to encapsulate all this. We begin by | |
8417 | building an decl for the catch clause parameter and link it to | |
8418 | newly created block, the catch block. */ | |
8419 | catch_clause_param = | |
8420 | build_decl (VAR_DECL, wpv_id, | |
8421 | build_pointer_type (class_not_found_exception)); | |
8422 | catch_block = build_expr_block (NULL_TREE, catch_clause_param); | |
8423 | ||
8424 | /* We initialize the variable with the exception handler. */ | |
8425 | catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param, | |
8426 | soft_exceptioninfo_call_node); | |
8427 | add_stmt_to_block (catch_block, NULL_TREE, catch); | |
8428 | ||
8429 | /* We add the statement throwing the new exception */ | |
8430 | add_stmt_to_block (catch_block, NULL_TREE, throw_stmt); | |
8431 | ||
8432 | /* Build a catch expression for all this */ | |
8433 | catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block); | |
8434 | ||
8435 | /* Build the try/catch sequence */ | |
8436 | stmt = build_try_statement (0, try_block, catch_block); | |
8437 | ||
8438 | fix_method_argument_names (args, mdecl); | |
8439 | layout_class_method (class, NULL_TREE, mdecl, NULL_TREE); | |
8440 | saved_current_function_decl = current_function_decl; | |
8441 | start_artificial_method_body (mdecl); | |
8442 | java_method_add_stmt (mdecl, stmt); | |
8443 | end_artificial_method_body (mdecl); | |
8444 | current_function_decl = saved_current_function_decl; | |
8445 | TYPE_DOT_CLASS (class) = mdecl; | |
8446 | ||
8447 | return mdecl; | |
8448 | } | |
8449 | ||
8450 | static tree | |
f0f3a777 APB |
8451 | build_dot_class_method_invocation (type) |
8452 | tree type; | |
165f37bc | 8453 | { |
f0f3a777 APB |
8454 | tree sig_id, s; |
8455 | ||
8456 | if (TYPE_ARRAY_P (type)) | |
8457 | sig_id = build_java_signature (type); | |
8458 | else | |
8459 | sig_id = DECL_NAME (TYPE_NAME (type)); | |
8460 | ||
1f8f4a0b MM |
8461 | s = build_string (IDENTIFIER_LENGTH (sig_id), |
8462 | IDENTIFIER_POINTER (sig_id)); | |
165f37bc APB |
8463 | return build_method_invocation (build_wfl_node (get_identifier ("class$")), |
8464 | build_tree_list (NULL_TREE, s)); | |
8465 | } | |
8466 | ||
c2952b01 APB |
8467 | /* This section of the code deals with constructor. */ |
8468 | ||
22eed1e6 APB |
8469 | /* Craft a body for default constructor. Patch existing constructor |
8470 | bodies with call to super() and field initialization statements if | |
8471 | necessary. */ | |
8472 | ||
8473 | static void | |
8474 | fix_constructors (mdecl) | |
8475 | tree mdecl; | |
8476 | { | |
8477 | tree body = DECL_FUNCTION_BODY (mdecl); | |
c2952b01 APB |
8478 | tree thisn_assign, compound = NULL_TREE; |
8479 | tree class_type = DECL_CONTEXT (mdecl); | |
22eed1e6 | 8480 | |
22eed1e6 APB |
8481 | if (!body) |
8482 | { | |
22eed1e6 APB |
8483 | /* It is an error for the compiler to generate a default |
8484 | constructor if the superclass doesn't have a constructor that | |
c2952b01 APB |
8485 | takes no argument, or the same args for an anonymous class */ |
8486 | if (verify_constructor_super (mdecl)) | |
22eed1e6 | 8487 | { |
c2952b01 APB |
8488 | tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type)); |
8489 | tree save = DECL_NAME (mdecl); | |
49f48c71 | 8490 | const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl)); |
c2952b01 | 8491 | DECL_NAME (mdecl) = DECL_NAME (sclass_decl); |
781b0558 | 8492 | parse_error_context |
c2952b01 APB |
8493 | (lookup_cl (TYPE_NAME (class_type)), |
8494 | "No constructor matching `%s' found in class `%s'", | |
8495 | lang_printable_name (mdecl, 0), n); | |
8496 | DECL_NAME (mdecl) = save; | |
22eed1e6 APB |
8497 | } |
8498 | ||
c2952b01 APB |
8499 | /* The constructor body must be crafted by hand. It's the |
8500 | constructor we defined when we realize we didn't have the | |
8501 | CLASSNAME() constructor */ | |
22eed1e6 APB |
8502 | start_artificial_method_body (mdecl); |
8503 | ||
f0f3a777 APB |
8504 | /* Insert an assignment to the this$<n> hidden field, if |
8505 | necessary */ | |
8506 | if ((thisn_assign = build_thisn_assign ())) | |
8507 | java_method_add_stmt (mdecl, thisn_assign); | |
8508 | ||
22eed1e6 APB |
8509 | /* We don't generate a super constructor invocation if we're |
8510 | compiling java.lang.Object. build_super_invocation takes care | |
8511 | of that. */ | |
e920ebc9 | 8512 | compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl)); |
22eed1e6 | 8513 | |
c2952b01 APB |
8514 | /* Insert the instance initializer block right here, after the |
8515 | super invocation. */ | |
8516 | add_instance_initializer (mdecl); | |
8517 | ||
22eed1e6 APB |
8518 | end_artificial_method_body (mdecl); |
8519 | } | |
8520 | /* Search for an explicit constructor invocation */ | |
8521 | else | |
8522 | { | |
8523 | int found = 0; | |
8524 | tree main_block = BLOCK_EXPR_BODY (body); | |
22eed1e6 APB |
8525 | |
8526 | while (body) | |
8527 | switch (TREE_CODE (body)) | |
8528 | { | |
8529 | case CALL_EXPR: | |
8530 | found = CALL_EXPLICIT_CONSTRUCTOR_P (body); | |
8531 | body = NULL_TREE; | |
8532 | break; | |
8533 | case COMPOUND_EXPR: | |
8534 | case EXPR_WITH_FILE_LOCATION: | |
8535 | body = TREE_OPERAND (body, 0); | |
8536 | break; | |
8537 | case BLOCK: | |
8538 | body = BLOCK_EXPR_BODY (body); | |
8539 | break; | |
8540 | default: | |
8541 | found = 0; | |
8542 | body = NULL_TREE; | |
8543 | } | |
8544 | /* The constructor is missing an invocation of super() */ | |
8545 | if (!found) | |
8546 | compound = add_stmt_to_compound (compound, NULL_TREE, | |
c2952b01 | 8547 | build_super_invocation (mdecl)); |
22eed1e6 | 8548 | |
c2952b01 APB |
8549 | /* Generate the assignment to this$<n>, if necessary */ |
8550 | if ((thisn_assign = build_thisn_assign ())) | |
8551 | compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign); | |
8552 | ||
f0f3a777 APB |
8553 | /* Insert the instance initializer block right here, after the |
8554 | super invocation. */ | |
8555 | add_instance_initializer (mdecl); | |
8556 | ||
22eed1e6 APB |
8557 | /* Fix the constructor main block if we're adding extra stmts */ |
8558 | if (compound) | |
8559 | { | |
8560 | compound = add_stmt_to_compound (compound, NULL_TREE, | |
8561 | BLOCK_EXPR_BODY (main_block)); | |
8562 | BLOCK_EXPR_BODY (main_block) = compound; | |
8563 | } | |
8564 | } | |
8565 | } | |
8566 | ||
8567 | /* Browse constructors in the super class, searching for a constructor | |
8568 | that doesn't take any argument. Return 0 if one is found, 1 | |
c2952b01 APB |
8569 | otherwise. If the current class is an anonymous inner class, look |
8570 | for something that has the same signature. */ | |
22eed1e6 APB |
8571 | |
8572 | static int | |
c2952b01 APB |
8573 | verify_constructor_super (mdecl) |
8574 | tree mdecl; | |
22eed1e6 APB |
8575 | { |
8576 | tree class = CLASSTYPE_SUPER (current_class); | |
152de068 | 8577 | int super_inner = PURE_INNER_CLASS_TYPE_P (class); |
c2952b01 APB |
8578 | tree sdecl; |
8579 | ||
22eed1e6 APB |
8580 | if (!class) |
8581 | return 0; | |
8582 | ||
c2952b01 | 8583 | if (ANONYMOUS_CLASS_P (current_class)) |
22eed1e6 | 8584 | { |
c2952b01 APB |
8585 | tree mdecl_arg_type; |
8586 | SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl); | |
8587 | for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl)) | |
8588 | if (DECL_CONSTRUCTOR_P (sdecl)) | |
8589 | { | |
cf1b2274 | 8590 | tree m_arg_type; |
152de068 APB |
8591 | tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl))); |
8592 | if (super_inner) | |
8593 | arg_type = TREE_CHAIN (arg_type); | |
cf1b2274 APB |
8594 | for (m_arg_type = mdecl_arg_type; |
8595 | (arg_type != end_params_node | |
8596 | && m_arg_type != end_params_node); | |
c2952b01 | 8597 | arg_type = TREE_CHAIN (arg_type), |
cf1b2274 APB |
8598 | m_arg_type = TREE_CHAIN (m_arg_type)) |
8599 | if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type)) | |
c2952b01 APB |
8600 | break; |
8601 | ||
cf1b2274 | 8602 | if (arg_type == end_params_node && m_arg_type == end_params_node) |
c2952b01 APB |
8603 | return 0; |
8604 | } | |
8605 | } | |
8606 | else | |
8607 | { | |
8608 | for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl)) | |
22eed1e6 | 8609 | { |
152de068 APB |
8610 | tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl))); |
8611 | if (super_inner) | |
8612 | arg = TREE_CHAIN (arg); | |
8613 | if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node) | |
22eed1e6 APB |
8614 | return 0; |
8615 | } | |
8616 | } | |
8617 | return 1; | |
8618 | } | |
8619 | ||
22eed1e6 | 8620 | /* Generate code for all context remembered for code generation. */ |
b351b287 APB |
8621 | |
8622 | void | |
8623 | java_expand_classes () | |
8624 | { | |
5423609c | 8625 | int save_error_count = 0; |
c2952b01 APB |
8626 | static struct parser_ctxt *saved_ctxp = NULL; |
8627 | ||
23a79c61 APB |
8628 | java_parse_abort_on_error (); |
8629 | if (!(ctxp = ctxp_for_generation)) | |
5e942c50 APB |
8630 | return; |
8631 | java_layout_classes (); | |
8632 | java_parse_abort_on_error (); | |
8633 | ||
c2952b01 | 8634 | saved_ctxp = ctxp_for_generation; |
b351b287 APB |
8635 | for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next) |
8636 | { | |
8637 | ctxp = ctxp_for_generation; | |
8638 | lang_init_source (2); /* Error msgs have method prototypes */ | |
c2952b01 | 8639 | java_complete_expand_classes (); /* Complete and expand classes */ |
b351b287 APB |
8640 | java_parse_abort_on_error (); |
8641 | } | |
c2952b01 APB |
8642 | |
8643 | /* Find anonymous classes and expand their constructor, now they | |
8644 | have been fixed. */ | |
8645 | for (ctxp_for_generation = saved_ctxp; | |
8646 | ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next) | |
8647 | { | |
8648 | tree current; | |
8649 | ctxp = ctxp_for_generation; | |
8650 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) | |
8651 | { | |
8652 | current_class = TREE_TYPE (current); | |
8653 | if (ANONYMOUS_CLASS_P (current_class)) | |
8654 | { | |
8655 | tree d; | |
8656 | for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d)) | |
8657 | { | |
8658 | if (DECL_CONSTRUCTOR_P (d)) | |
8659 | { | |
8660 | restore_line_number_status (1); | |
8661 | reset_method_name (d); | |
8662 | java_complete_expand_method (d); | |
8663 | restore_line_number_status (0); | |
8664 | break; /* We now there are no other ones */ | |
8665 | } | |
8666 | } | |
8667 | } | |
8668 | } | |
8669 | } | |
8670 | ||
8671 | /* If we've found error at that stage, don't try to generate | |
8672 | anything, unless we're emitting xrefs or checking the syntax only | |
8673 | (but not using -fsyntax-only for the purpose of generating | |
8674 | bytecode. */ | |
8675 | if (java_error_count && !flag_emit_xref | |
8676 | && (!flag_syntax_only && !flag_emit_class_files)) | |
8677 | return; | |
8678 | ||
8679 | /* Now things are stable, go for generation of the class data. */ | |
8680 | for (ctxp_for_generation = saved_ctxp; | |
8681 | ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next) | |
8682 | { | |
8683 | tree current; | |
8684 | ctxp = ctxp_for_generation; | |
8685 | for (current = ctxp->class_list; current; current = TREE_CHAIN (current)) | |
8686 | { | |
8687 | current_class = TREE_TYPE (current); | |
8688 | outgoing_cpool = TYPE_CPOOL (current_class); | |
8689 | if (flag_emit_class_files) | |
8690 | write_classfile (current_class); | |
8691 | if (flag_emit_xref) | |
8692 | expand_xref (current_class); | |
8693 | else if (! flag_syntax_only) | |
8694 | finish_class (); | |
8695 | } | |
8696 | } | |
b351b287 APB |
8697 | } |
8698 | ||
e04a16fb AG |
8699 | /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to |
8700 | a tree list node containing RIGHT. Fore coming RIGHTs will be | |
8701 | chained to this hook. LOCATION contains the location of the | |
8702 | separating `.' operator. */ | |
8703 | ||
8704 | static tree | |
8705 | make_qualified_primary (primary, right, location) | |
8706 | tree primary, right; | |
8707 | int location; | |
8708 | { | |
8709 | tree wfl; | |
8710 | ||
c2952b01 | 8711 | if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION) |
9a7ab4b3 | 8712 | wfl = build_wfl_wrap (primary, location); |
e04a16fb AG |
8713 | else |
8714 | { | |
8715 | wfl = primary; | |
c2952b01 APB |
8716 | /* If wfl wasn't qualified, we build a first anchor */ |
8717 | if (!EXPR_WFL_QUALIFICATION (wfl)) | |
8718 | EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE); | |
e04a16fb AG |
8719 | } |
8720 | ||
c2952b01 | 8721 | /* And chain them */ |
e04a16fb AG |
8722 | EXPR_WFL_LINECOL (right) = location; |
8723 | chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE)); | |
8724 | PRIMARY_P (wfl) = 1; | |
8725 | return wfl; | |
8726 | } | |
8727 | ||
8728 | /* Simple merge of two name separated by a `.' */ | |
8729 | ||
8730 | static tree | |
8731 | merge_qualified_name (left, right) | |
8732 | tree left, right; | |
8733 | { | |
8734 | tree node; | |
c2952b01 APB |
8735 | if (!left && !right) |
8736 | return NULL_TREE; | |
8737 | ||
8738 | if (!left) | |
8739 | return right; | |
8740 | ||
8741 | if (!right) | |
8742 | return left; | |
8743 | ||
e04a16fb AG |
8744 | obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left), |
8745 | IDENTIFIER_LENGTH (left)); | |
8746 | obstack_1grow (&temporary_obstack, '.'); | |
8747 | obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right), | |
8748 | IDENTIFIER_LENGTH (right)); | |
8749 | node = get_identifier (obstack_base (&temporary_obstack)); | |
8750 | obstack_free (&temporary_obstack, obstack_base (&temporary_obstack)); | |
8751 | QUALIFIED_P (node) = 1; | |
8752 | return node; | |
8753 | } | |
8754 | ||
8755 | /* Merge the two parts of a qualified name into LEFT. Set the | |
8756 | location information of the resulting node to LOCATION, usually | |
8757 | inherited from the location information of the `.' operator. */ | |
8758 | ||
8759 | static tree | |
8760 | make_qualified_name (left, right, location) | |
8761 | tree left, right; | |
8762 | int location; | |
8763 | { | |
bc3ca41b PB |
8764 | #ifdef USE_COMPONENT_REF |
8765 | tree node = build (COMPONENT_REF, NULL_TREE, left, right); | |
8766 | EXPR_WFL_LINECOL (node) = location; | |
8767 | return node; | |
8768 | #else | |
e04a16fb AG |
8769 | tree left_id = EXPR_WFL_NODE (left); |
8770 | tree right_id = EXPR_WFL_NODE (right); | |
8771 | tree wfl, merge; | |
8772 | ||
8773 | merge = merge_qualified_name (left_id, right_id); | |
8774 | ||
8775 | /* Left wasn't qualified and is now qualified */ | |
8776 | if (!QUALIFIED_P (left_id)) | |
8777 | { | |
8778 | tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0); | |
8779 | EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left); | |
8780 | EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE); | |
8781 | } | |
8782 | ||
8783 | wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0); | |
8784 | EXPR_WFL_LINECOL (wfl) = location; | |
8785 | chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE)); | |
8786 | ||
8787 | EXPR_WFL_NODE (left) = merge; | |
8788 | return left; | |
bc3ca41b | 8789 | #endif |
e04a16fb AG |
8790 | } |
8791 | ||
8792 | /* Extract the last identifier component of the qualified in WFL. The | |
8793 | last identifier is removed from the linked list */ | |
8794 | ||
8795 | static tree | |
8796 | cut_identifier_in_qualified (wfl) | |
8797 | tree wfl; | |
8798 | { | |
8799 | tree q; | |
8800 | tree previous = NULL_TREE; | |
8801 | for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q)) | |
8802 | if (!TREE_CHAIN (q)) | |
8803 | { | |
8804 | if (!previous) | |
781b0558 | 8805 | fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified"); |
e04a16fb AG |
8806 | TREE_CHAIN (previous) = NULL_TREE; |
8807 | return TREE_PURPOSE (q); | |
8808 | } | |
8809 | } | |
8810 | ||
8811 | /* Resolve the expression name NAME. Return its decl. */ | |
8812 | ||
8813 | static tree | |
5e942c50 | 8814 | resolve_expression_name (id, orig) |
e04a16fb | 8815 | tree id; |
5e942c50 | 8816 | tree *orig; |
e04a16fb AG |
8817 | { |
8818 | tree name = EXPR_WFL_NODE (id); | |
8819 | tree decl; | |
8820 | ||
8821 | /* 6.5.5.1: Simple expression names */ | |
8822 | if (!PRIMARY_P (id) && !QUALIFIED_P (name)) | |
8823 | { | |
8824 | /* 15.13.1: NAME can appear within the scope of a local variable | |
8825 | declaration */ | |
8826 | if ((decl = IDENTIFIER_LOCAL_VALUE (name))) | |
8827 | return decl; | |
8828 | ||
8829 | /* 15.13.1: NAME can appear within a class declaration */ | |
8830 | else | |
8831 | { | |
8832 | decl = lookup_field_wrapper (current_class, name); | |
8833 | if (decl) | |
8834 | { | |
c2952b01 | 8835 | tree access = NULL_TREE; |
e04a16fb | 8836 | int fs = FIELD_STATIC (decl); |
f2760b27 APB |
8837 | |
8838 | /* If we're accessing an outer scope local alias, make | |
8839 | sure we change the name of the field we're going to | |
8840 | build access to. */ | |
8841 | if (FIELD_LOCAL_ALIAS_USED (decl)) | |
8842 | name = DECL_NAME (decl); | |
8843 | ||
e04a16fb AG |
8844 | /* Instance variable (8.3.1.1) can't appear within |
8845 | static method, static initializer or initializer for | |
8846 | a static variable. */ | |
8847 | if (!fs && METHOD_STATIC (current_function_decl)) | |
8848 | { | |
7f10c2e2 | 8849 | static_ref_err (id, name, current_class); |
e04a16fb AG |
8850 | return error_mark_node; |
8851 | } | |
22eed1e6 APB |
8852 | /* Instance variables can't appear as an argument of |
8853 | an explicit constructor invocation */ | |
8854 | if (!fs && ctxp->explicit_constructor_p) | |
8855 | { | |
8856 | parse_error_context | |
781b0558 | 8857 | (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name)); |
22eed1e6 APB |
8858 | return error_mark_node; |
8859 | } | |
5e942c50 | 8860 | |
c2952b01 APB |
8861 | /* If we're processing an inner class and we're trying |
8862 | to access a field belonging to an outer class, build | |
8863 | the access to the field */ | |
8864 | if (!fs && outer_field_access_p (current_class, decl)) | |
8865 | return build_outer_field_access (id, decl); | |
8866 | ||
5e942c50 | 8867 | /* Otherwise build what it takes to access the field */ |
c2952b01 APB |
8868 | access = build_field_ref ((fs ? NULL_TREE : current_this), |
8869 | DECL_CONTEXT (decl), name); | |
e8fc7396 | 8870 | if (fs && !flag_emit_class_files && !flag_emit_xref) |
c2952b01 | 8871 | access = build_class_init (DECL_CONTEXT (access), access); |
5e942c50 APB |
8872 | /* We may be asked to save the real field access node */ |
8873 | if (orig) | |
c2952b01 | 8874 | *orig = access; |
5e942c50 | 8875 | /* And we return what we got */ |
c2952b01 | 8876 | return access; |
e04a16fb AG |
8877 | } |
8878 | /* Fall down to error report on undefined variable */ | |
8879 | } | |
8880 | } | |
8881 | /* 6.5.5.2 Qualified Expression Names */ | |
8882 | else | |
8883 | { | |
5e942c50 APB |
8884 | if (orig) |
8885 | *orig = NULL_TREE; | |
e04a16fb AG |
8886 | qualify_ambiguous_name (id); |
8887 | /* 15.10.1 Field Access Using a Primary and/or Expression Name */ | |
8888 | /* 15.10.2: Accessing Superclass Members using super */ | |
98f3c1db | 8889 | return resolve_field_access (id, orig, NULL); |
e04a16fb AG |
8890 | } |
8891 | ||
8892 | /* We've got an error here */ | |
8893 | parse_error_context (id, "Undefined variable `%s'", | |
8894 | IDENTIFIER_POINTER (name)); | |
8895 | ||
8896 | return error_mark_node; | |
8897 | } | |
8898 | ||
7f10c2e2 APB |
8899 | static void |
8900 | static_ref_err (wfl, field_id, class_type) | |
8901 | tree wfl, field_id, class_type; | |
8902 | { | |
8903 | parse_error_context | |
8904 | (wfl, | |
8905 | "Can't make a static reference to nonstatic variable `%s' in class `%s'", | |
8906 | IDENTIFIER_POINTER (field_id), | |
8907 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type)))); | |
8908 | } | |
8909 | ||
e04a16fb AG |
8910 | /* 15.10.1 Field Acess Using a Primary and/or Expression Name. |
8911 | We return something suitable to generate the field access. We also | |
8912 | return the field decl in FIELD_DECL and its type in FIELD_TYPE. If | |
8913 | recipient's address can be null. */ | |
8914 | ||
8915 | static tree | |
8916 | resolve_field_access (qual_wfl, field_decl, field_type) | |
8917 | tree qual_wfl; | |
8918 | tree *field_decl, *field_type; | |
8919 | { | |
8920 | int is_static = 0; | |
8921 | tree field_ref; | |
8922 | tree decl, where_found, type_found; | |
8923 | ||
8924 | if (resolve_qualified_expression_name (qual_wfl, &decl, | |
8925 | &where_found, &type_found)) | |
8926 | return error_mark_node; | |
8927 | ||
8928 | /* Resolve the LENGTH field of an array here */ | |
9a7ab4b3 | 8929 | if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node |
6eaeeb55 | 8930 | && type_found && TYPE_ARRAY_P (type_found) |
e8fc7396 | 8931 | && ! flag_emit_class_files && ! flag_emit_xref) |
e04a16fb AG |
8932 | { |
8933 | tree length = build_java_array_length_access (where_found); | |
8934 | field_ref = | |
8935 | build_java_arraynull_check (type_found, length, int_type_node); | |
611a4b87 APB |
8936 | |
8937 | /* In case we're dealing with a static array, we need to | |
8938 | initialize its class before the array length can be fetched. | |
8939 | It's also a good time to create a DECL_RTL for the field if | |
8940 | none already exists, otherwise if the field was declared in a | |
8941 | class found in an external file and hasn't been (and won't | |
8942 | be) accessed for its value, none will be created. */ | |
8943 | if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found)) | |
8944 | { | |
8945 | build_static_field_ref (where_found); | |
8946 | field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref); | |
8947 | } | |
e04a16fb AG |
8948 | } |
8949 | /* We might have been trying to resolve field.method(). In which | |
8950 | case, the resolution is over and decl is the answer */ | |
34f4db93 | 8951 | else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl) |
e04a16fb | 8952 | field_ref = decl; |
34f4db93 | 8953 | else if (JDECL_P (decl)) |
e04a16fb | 8954 | { |
5e942c50 APB |
8955 | int static_final_found = 0; |
8956 | if (!type_found) | |
8957 | type_found = DECL_CONTEXT (decl); | |
34f4db93 | 8958 | is_static = JDECL_P (decl) && FIELD_STATIC (decl); |
0c2b8145 | 8959 | if (FIELD_FINAL (decl) && FIELD_STATIC (decl) |
5e942c50 | 8960 | && JPRIMITIVE_TYPE_P (TREE_TYPE (decl)) |
7525cc04 | 8961 | && DECL_INITIAL (decl)) |
5e942c50 | 8962 | { |
0c2b8145 APB |
8963 | /* When called on a FIELD_DECL of the right (primitive) |
8964 | type, java_complete_tree will try to substitue the decl | |
8965 | for it's initial value. */ | |
70541f45 | 8966 | field_ref = java_complete_tree (decl); |
5e942c50 APB |
8967 | static_final_found = 1; |
8968 | } | |
8969 | else | |
7f10c2e2 APB |
8970 | field_ref = build_field_ref ((is_static && !flag_emit_xref? |
8971 | NULL_TREE : where_found), | |
5e942c50 | 8972 | type_found, DECL_NAME (decl)); |
e04a16fb AG |
8973 | if (field_ref == error_mark_node) |
8974 | return error_mark_node; | |
e8fc7396 APB |
8975 | if (is_static && !static_final_found |
8976 | && !flag_emit_class_files && !flag_emit_xref) | |
40aaba2b | 8977 | field_ref = build_class_init (DECL_CONTEXT (decl), field_ref); |
e04a16fb AG |
8978 | } |
8979 | else | |
8980 | field_ref = decl; | |
8981 | ||
8982 | if (field_decl) | |
8983 | *field_decl = decl; | |
8984 | if (field_type) | |
c877974e APB |
8985 | *field_type = (QUAL_DECL_TYPE (decl) ? |
8986 | QUAL_DECL_TYPE (decl) : TREE_TYPE (decl)); | |
e04a16fb AG |
8987 | return field_ref; |
8988 | } | |
8989 | ||
e28cd97b APB |
8990 | /* If NODE is an access to f static field, strip out the class |
8991 | initialization part and return the field decl, otherwise, return | |
8992 | NODE. */ | |
8993 | ||
8994 | static tree | |
8995 | strip_out_static_field_access_decl (node) | |
8996 | tree node; | |
8997 | { | |
8998 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
8999 | { | |
9000 | tree op1 = TREE_OPERAND (node, 1); | |
9001 | if (TREE_CODE (op1) == COMPOUND_EXPR) | |
9002 | { | |
9003 | tree call = TREE_OPERAND (op1, 0); | |
9004 | if (TREE_CODE (call) == CALL_EXPR | |
9005 | && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR | |
9006 | && TREE_OPERAND (TREE_OPERAND (call, 0), 0) | |
9007 | == soft_initclass_node) | |
9008 | return TREE_OPERAND (op1, 1); | |
9009 | } | |
2f11d407 TT |
9010 | else if (JDECL_P (op1)) |
9011 | return op1; | |
e28cd97b APB |
9012 | } |
9013 | return node; | |
9014 | } | |
9015 | ||
e04a16fb AG |
9016 | /* 6.5.5.2: Qualified Expression Names */ |
9017 | ||
9018 | static int | |
9019 | resolve_qualified_expression_name (wfl, found_decl, where_found, type_found) | |
9020 | tree wfl; | |
9021 | tree *found_decl, *type_found, *where_found; | |
9022 | { | |
9023 | int from_type = 0; /* Field search initiated from a type */ | |
c2952b01 | 9024 | int from_super = 0, from_cast = 0, from_qualified_this = 0; |
e04a16fb AG |
9025 | int previous_call_static = 0; |
9026 | int is_static; | |
9027 | tree decl = NULL_TREE, type = NULL_TREE, q; | |
c2952b01 APB |
9028 | /* For certain for of inner class instantiation */ |
9029 | tree saved_current, saved_this; | |
9030 | #define RESTORE_THIS_AND_CURRENT_CLASS \ | |
9031 | { current_class = saved_current; current_this = saved_this;} | |
9032 | ||
c877974e | 9033 | *type_found = *where_found = NULL_TREE; |
e04a16fb AG |
9034 | |
9035 | for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q)) | |
9036 | { | |
9037 | tree qual_wfl = QUAL_WFL (q); | |
7705e9db APB |
9038 | tree ret_decl; /* for EH checking */ |
9039 | int location; /* for EH checking */ | |
e04a16fb AG |
9040 | |
9041 | /* 15.10.1 Field Access Using a Primary */ | |
e04a16fb AG |
9042 | switch (TREE_CODE (qual_wfl)) |
9043 | { | |
9044 | case CALL_EXPR: | |
b67d701b | 9045 | case NEW_CLASS_EXPR: |
e04a16fb AG |
9046 | /* If the access to the function call is a non static field, |
9047 | build the code to access it. */ | |
34f4db93 | 9048 | if (JDECL_P (decl) && !FIELD_STATIC (decl)) |
e04a16fb | 9049 | { |
ac825856 APB |
9050 | decl = maybe_access_field (decl, *where_found, |
9051 | DECL_CONTEXT (decl)); | |
e04a16fb AG |
9052 | if (decl == error_mark_node) |
9053 | return 1; | |
9054 | } | |
c2952b01 | 9055 | |
e04a16fb AG |
9056 | /* And code for the function call */ |
9057 | if (complete_function_arguments (qual_wfl)) | |
9058 | return 1; | |
c2952b01 APB |
9059 | |
9060 | /* We might have to setup a new current class and a new this | |
9061 | for the search of an inner class, relative to the type of | |
9062 | a expression resolved as `decl'. The current values are | |
9063 | saved and restored shortly after */ | |
9064 | saved_current = current_class; | |
9065 | saved_this = current_this; | |
dba41d30 APB |
9066 | if (decl |
9067 | && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR | |
9068 | || from_qualified_this)) | |
c2952b01 | 9069 | { |
dba41d30 APB |
9070 | /* If we still have `from_qualified_this', we have the form |
9071 | <T>.this.f() and we need to build <T>.this */ | |
9072 | if (from_qualified_this) | |
9073 | { | |
9074 | decl = build_access_to_thisn (current_class, type, 0); | |
9075 | decl = java_complete_tree (decl); | |
9076 | type = TREE_TYPE (TREE_TYPE (decl)); | |
9077 | } | |
c2952b01 APB |
9078 | current_class = type; |
9079 | current_this = decl; | |
dba41d30 | 9080 | from_qualified_this = 0; |
c2952b01 APB |
9081 | } |
9082 | ||
89e09b9a PB |
9083 | if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR) |
9084 | CALL_USING_SUPER (qual_wfl) = 1; | |
7705e9db APB |
9085 | location = (TREE_CODE (qual_wfl) == CALL_EXPR ? |
9086 | EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0); | |
9087 | *where_found = patch_method_invocation (qual_wfl, decl, type, | |
9088 | &is_static, &ret_decl); | |
e04a16fb | 9089 | if (*where_found == error_mark_node) |
c2952b01 APB |
9090 | { |
9091 | RESTORE_THIS_AND_CURRENT_CLASS; | |
9092 | return 1; | |
9093 | } | |
e04a16fb AG |
9094 | *type_found = type = QUAL_DECL_TYPE (*where_found); |
9095 | ||
c2952b01 APB |
9096 | /* If we're creating an inner class instance, check for that |
9097 | an enclosing instance is in scope */ | |
9098 | if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR | |
165f37bc | 9099 | && INNER_ENCLOSING_SCOPE_CHECK (type)) |
c2952b01 APB |
9100 | { |
9101 | parse_error_context | |
165f37bc APB |
9102 | (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s", |
9103 | lang_printable_name (type, 0), | |
9104 | (!current_this ? "" : | |
9105 | "; an explicit one must be provided when creating this inner class")); | |
c2952b01 APB |
9106 | RESTORE_THIS_AND_CURRENT_CLASS; |
9107 | return 1; | |
9108 | } | |
9109 | ||
9110 | /* In case we had to change then to resolve a inner class | |
9111 | instantiation using a primary qualified by a `new' */ | |
9112 | RESTORE_THIS_AND_CURRENT_CLASS; | |
9113 | ||
34d4df06 APB |
9114 | /* EH check. No check on access$<n> functions */ |
9115 | if (location | |
9116 | && !OUTER_FIELD_ACCESS_IDENTIFIER_P | |
9117 | (DECL_NAME (current_function_decl))) | |
7705e9db APB |
9118 | check_thrown_exceptions (location, ret_decl); |
9119 | ||
e04a16fb AG |
9120 | /* If the previous call was static and this one is too, |
9121 | build a compound expression to hold the two (because in | |
9122 | that case, previous function calls aren't transported as | |
9123 | forcoming function's argument. */ | |
9124 | if (previous_call_static && is_static) | |
9125 | { | |
9126 | decl = build (COMPOUND_EXPR, type, decl, *where_found); | |
9127 | TREE_SIDE_EFFECTS (decl) = 1; | |
9128 | } | |
9129 | else | |
9130 | { | |
9131 | previous_call_static = is_static; | |
9132 | decl = *where_found; | |
9133 | } | |
c2952b01 | 9134 | from_type = 0; |
e04a16fb AG |
9135 | continue; |
9136 | ||
d8fccff5 | 9137 | case NEW_ARRAY_EXPR: |
c2952b01 | 9138 | case NEW_ANONYMOUS_ARRAY_EXPR: |
d8fccff5 APB |
9139 | *where_found = decl = java_complete_tree (qual_wfl); |
9140 | if (decl == error_mark_node) | |
9141 | return 1; | |
9142 | *type_found = type = QUAL_DECL_TYPE (decl); | |
9143 | CLASS_LOADED_P (type) = 1; | |
9144 | continue; | |
9145 | ||
e04a16fb AG |
9146 | case CONVERT_EXPR: |
9147 | *where_found = decl = java_complete_tree (qual_wfl); | |
9148 | if (decl == error_mark_node) | |
9149 | return 1; | |
9150 | *type_found = type = QUAL_DECL_TYPE (decl); | |
9151 | from_cast = 1; | |
9152 | continue; | |
9153 | ||
22eed1e6 | 9154 | case CONDITIONAL_EXPR: |
5e942c50 | 9155 | case STRING_CST: |
ac22f9cb | 9156 | case MODIFY_EXPR: |
22eed1e6 APB |
9157 | *where_found = decl = java_complete_tree (qual_wfl); |
9158 | if (decl == error_mark_node) | |
9159 | return 1; | |
9160 | *type_found = type = QUAL_DECL_TYPE (decl); | |
9161 | continue; | |
9162 | ||
e04a16fb AG |
9163 | case ARRAY_REF: |
9164 | /* If the access to the function call is a non static field, | |
9165 | build the code to access it. */ | |
34f4db93 | 9166 | if (JDECL_P (decl) && !FIELD_STATIC (decl)) |
e04a16fb AG |
9167 | { |
9168 | decl = maybe_access_field (decl, *where_found, type); | |
9169 | if (decl == error_mark_node) | |
9170 | return 1; | |
9171 | } | |
9172 | /* And code for the array reference expression */ | |
9173 | decl = java_complete_tree (qual_wfl); | |
9174 | if (decl == error_mark_node) | |
9175 | return 1; | |
9176 | type = QUAL_DECL_TYPE (decl); | |
9177 | continue; | |
0a2138e2 | 9178 | |
37feda7d APB |
9179 | case PLUS_EXPR: |
9180 | if ((decl = java_complete_tree (qual_wfl)) == error_mark_node) | |
9181 | return 1; | |
9182 | if ((type = patch_string (decl))) | |
9183 | decl = type; | |
9184 | *where_found = QUAL_RESOLUTION (q) = decl; | |
9185 | *type_found = type = TREE_TYPE (decl); | |
9186 | break; | |
9187 | ||
165f37bc APB |
9188 | case CLASS_LITERAL: |
9189 | if ((decl = java_complete_tree (qual_wfl)) == error_mark_node) | |
9190 | return 1; | |
9191 | *where_found = QUAL_RESOLUTION (q) = decl; | |
9192 | *type_found = type = TREE_TYPE (decl); | |
9193 | break; | |
9194 | ||
0a2138e2 APB |
9195 | default: |
9196 | /* Fix for -Wall Just go to the next statement. Don't | |
9197 | continue */ | |
a3f406ce | 9198 | break; |
e04a16fb AG |
9199 | } |
9200 | ||
9201 | /* If we fall here, we weren't processing a (static) function call. */ | |
9202 | previous_call_static = 0; | |
9203 | ||
9204 | /* It can be the keyword THIS */ | |
9205 | if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node) | |
9206 | { | |
9207 | if (!current_this) | |
9208 | { | |
9209 | parse_error_context | |
9210 | (wfl, "Keyword `this' used outside allowed context"); | |
9211 | return 1; | |
9212 | } | |
f63991a8 APB |
9213 | if (ctxp->explicit_constructor_p) |
9214 | { | |
781b0558 | 9215 | parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called"); |
f63991a8 APB |
9216 | return 1; |
9217 | } | |
e04a16fb | 9218 | /* We have to generate code for intermediate acess */ |
c2952b01 APB |
9219 | if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type) |
9220 | { | |
9221 | *where_found = decl = current_this; | |
9222 | *type_found = type = QUAL_DECL_TYPE (decl); | |
9223 | } | |
4dbf4496 APB |
9224 | /* We're trying to access the this from somewhere else. Make sure |
9225 | it's allowed before doing so. */ | |
c2952b01 APB |
9226 | else |
9227 | { | |
4dbf4496 APB |
9228 | if (!enclosing_context_p (type, current_class)) |
9229 | { | |
9230 | char *p = xstrdup (lang_printable_name (type, 0)); | |
9231 | parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'", | |
9232 | p, p, | |
9233 | lang_printable_name (current_class, 0)); | |
9234 | free (p); | |
9235 | return 1; | |
9236 | } | |
c2952b01 | 9237 | from_qualified_this = 1; |
dba41d30 APB |
9238 | /* If there's nothing else after that, we need to |
9239 | produce something now, otherwise, the section of the | |
9240 | code that needs to produce <T>.this will generate | |
9241 | what is necessary. */ | |
9242 | if (!TREE_CHAIN (q)) | |
9243 | { | |
9244 | decl = build_access_to_thisn (current_class, type, 0); | |
9245 | *where_found = decl = java_complete_tree (decl); | |
9246 | *type_found = type = TREE_TYPE (decl); | |
9247 | } | |
c2952b01 APB |
9248 | } |
9249 | ||
9250 | from_type = 0; | |
e04a16fb AG |
9251 | continue; |
9252 | } | |
9253 | ||
9254 | /* 15.10.2 Accessing Superclass Members using SUPER */ | |
9255 | if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node) | |
9256 | { | |
9257 | tree node; | |
9258 | /* Check on the restricted use of SUPER */ | |
9259 | if (METHOD_STATIC (current_function_decl) | |
9260 | || current_class == object_type_node) | |
9261 | { | |
9262 | parse_error_context | |
9263 | (wfl, "Keyword `super' used outside allowed context"); | |
9264 | return 1; | |
9265 | } | |
9266 | /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */ | |
9267 | node = build_cast (EXPR_WFL_LINECOL (qual_wfl), | |
9268 | CLASSTYPE_SUPER (current_class), | |
9269 | build_this (EXPR_WFL_LINECOL (qual_wfl))); | |
9270 | *where_found = decl = java_complete_tree (node); | |
22eed1e6 APB |
9271 | if (decl == error_mark_node) |
9272 | return 1; | |
e04a16fb AG |
9273 | *type_found = type = QUAL_DECL_TYPE (decl); |
9274 | from_super = from_type = 1; | |
9275 | continue; | |
9276 | } | |
9277 | ||
9278 | /* 15.13.1: Can't search for field name in packages, so we | |
9279 | assume a variable/class name was meant. */ | |
9280 | if (RESOLVE_PACKAGE_NAME_P (qual_wfl)) | |
9281 | { | |
5e942c50 APB |
9282 | tree name = resolve_package (wfl, &q); |
9283 | if (name) | |
9284 | { | |
c2952b01 | 9285 | tree list; |
5e942c50 | 9286 | *where_found = decl = resolve_no_layout (name, qual_wfl); |
6b48deee | 9287 | /* We want to be absolutely sure that the class is laid |
5e942c50 APB |
9288 | out. We're going to search something inside it. */ |
9289 | *type_found = type = TREE_TYPE (decl); | |
9290 | layout_class (type); | |
9291 | from_type = 1; | |
c2952b01 | 9292 | |
dde1da72 APB |
9293 | /* Fix them all the way down, if any are left. */ |
9294 | if (q) | |
c2952b01 | 9295 | { |
dde1da72 APB |
9296 | list = TREE_CHAIN (q); |
9297 | while (list) | |
9298 | { | |
9299 | RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1; | |
9300 | RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0; | |
9301 | list = TREE_CHAIN (list); | |
9302 | } | |
c2952b01 | 9303 | } |
5e942c50 | 9304 | } |
e04a16fb | 9305 | else |
5e942c50 APB |
9306 | { |
9307 | if (from_super || from_cast) | |
9308 | parse_error_context | |
9309 | ((from_cast ? qual_wfl : wfl), | |
9310 | "No variable `%s' defined in class `%s'", | |
9311 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), | |
9312 | lang_printable_name (type, 0)); | |
9313 | else | |
9314 | parse_error_context | |
9315 | (qual_wfl, "Undefined variable or class name: `%s'", | |
9316 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl))); | |
9317 | return 1; | |
9318 | } | |
e04a16fb AG |
9319 | } |
9320 | ||
9321 | /* We have a type name. It's been already resolved when the | |
9322 | expression was qualified. */ | |
9323 | else if (RESOLVE_TYPE_NAME_P (qual_wfl)) | |
9324 | { | |
9325 | if (!(decl = QUAL_RESOLUTION (q))) | |
9326 | return 1; /* Error reported already */ | |
9327 | ||
c2952b01 APB |
9328 | /* Sneak preview. If next we see a `new', we're facing a |
9329 | qualification with resulted in a type being selected | |
9330 | instead of a field. Report the error */ | |
9331 | if(TREE_CHAIN (q) | |
9332 | && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR) | |
9333 | { | |
9334 | parse_error_context (qual_wfl, "Undefined variable `%s'", | |
9335 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
9336 | return 1; | |
9337 | } | |
9338 | ||
e04a16fb AG |
9339 | if (not_accessible_p (TREE_TYPE (decl), decl, 0)) |
9340 | { | |
9341 | parse_error_context | |
9342 | (qual_wfl, "Can't access %s field `%s.%s' from `%s'", | |
9343 | java_accstring_lookup (get_access_flags_from_decl (decl)), | |
2aa11e97 | 9344 | GET_TYPE_NAME (type), |
e04a16fb AG |
9345 | IDENTIFIER_POINTER (DECL_NAME (decl)), |
9346 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)))); | |
9347 | return 1; | |
9348 | } | |
5e942c50 | 9349 | check_deprecation (qual_wfl, decl); |
c2952b01 | 9350 | |
e04a16fb AG |
9351 | type = TREE_TYPE (decl); |
9352 | from_type = 1; | |
9353 | } | |
9354 | /* We resolve and expression name */ | |
9355 | else | |
9356 | { | |
cd531a2e | 9357 | tree field_decl = NULL_TREE; |
e04a16fb AG |
9358 | |
9359 | /* If there exists an early resolution, use it. That occurs | |
9360 | only once and we know that there are more things to | |
9361 | come. Don't do that when processing something after SUPER | |
9362 | (we need more thing to be put in place below */ | |
9363 | if (!from_super && QUAL_RESOLUTION (q)) | |
b67d701b PB |
9364 | { |
9365 | decl = QUAL_RESOLUTION (q); | |
c877974e | 9366 | if (!type) |
5e942c50 | 9367 | { |
7f10c2e2 APB |
9368 | if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl)) |
9369 | { | |
9370 | if (current_this) | |
9371 | *where_found = current_this; | |
9372 | else | |
9373 | { | |
9374 | static_ref_err (qual_wfl, DECL_NAME (decl), | |
9375 | current_class); | |
9376 | return 1; | |
9377 | } | |
f0f3a777 APB |
9378 | if (outer_field_access_p (current_class, decl)) |
9379 | decl = build_outer_field_access (qual_wfl, decl); | |
7f10c2e2 | 9380 | } |
c877974e APB |
9381 | else |
9382 | { | |
9383 | *where_found = TREE_TYPE (decl); | |
9384 | if (TREE_CODE (*where_found) == POINTER_TYPE) | |
9385 | *where_found = TREE_TYPE (*where_found); | |
9386 | } | |
5e942c50 | 9387 | } |
b67d701b | 9388 | } |
e04a16fb AG |
9389 | |
9390 | /* We have to search for a field, knowing the type of its | |
9391 | container. The flag FROM_TYPE indicates that we resolved | |
9392 | the last member of the expression as a type name, which | |
5e942c50 APB |
9393 | means that for the resolution of this field, we'll look |
9394 | for other errors than if it was resolved as a member of | |
9395 | an other field. */ | |
e04a16fb AG |
9396 | else |
9397 | { | |
9398 | int is_static; | |
5e942c50 APB |
9399 | tree field_decl_type; /* For layout */ |
9400 | ||
e04a16fb AG |
9401 | if (!from_type && !JREFERENCE_TYPE_P (type)) |
9402 | { | |
9403 | parse_error_context | |
9404 | (qual_wfl, "Attempt to reference field `%s' in `%s %s'", | |
9405 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), | |
0a2138e2 | 9406 | lang_printable_name (type, 0), |
e04a16fb AG |
9407 | IDENTIFIER_POINTER (DECL_NAME (field_decl))); |
9408 | return 1; | |
9409 | } | |
9410 | ||
dc0b3eff PB |
9411 | field_decl = lookup_field_wrapper (type, |
9412 | EXPR_WFL_NODE (qual_wfl)); | |
4dbf4496 APB |
9413 | |
9414 | /* Maybe what we're trying to access an inner class. */ | |
9415 | if (!field_decl) | |
9416 | { | |
9417 | tree ptr, inner_decl; | |
9418 | ||
9419 | BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl)); | |
9420 | inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl); | |
9421 | if (inner_decl) | |
9422 | { | |
9423 | check_inner_class_access (inner_decl, decl, qual_wfl); | |
9424 | type = TREE_TYPE (inner_decl); | |
9425 | decl = inner_decl; | |
9426 | from_type = 1; | |
9427 | continue; | |
9428 | } | |
9429 | } | |
9430 | ||
dc0b3eff | 9431 | if (field_decl == NULL_TREE) |
e04a16fb AG |
9432 | { |
9433 | parse_error_context | |
2aa11e97 | 9434 | (qual_wfl, "No variable `%s' defined in type `%s'", |
e04a16fb | 9435 | IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), |
2aa11e97 | 9436 | GET_TYPE_NAME (type)); |
e04a16fb AG |
9437 | return 1; |
9438 | } | |
dc0b3eff PB |
9439 | if (field_decl == error_mark_node) |
9440 | return 1; | |
5e942c50 APB |
9441 | |
9442 | /* Layout the type of field_decl, since we may need | |
c877974e APB |
9443 | it. Don't do primitive types or loaded classes. The |
9444 | situation of non primitive arrays may not handled | |
9445 | properly here. FIXME */ | |
5e942c50 APB |
9446 | if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE) |
9447 | field_decl_type = TREE_TYPE (TREE_TYPE (field_decl)); | |
9448 | else | |
9449 | field_decl_type = TREE_TYPE (field_decl); | |
9450 | if (!JPRIMITIVE_TYPE_P (field_decl_type) | |
c877974e APB |
9451 | && !CLASS_LOADED_P (field_decl_type) |
9452 | && !TYPE_ARRAY_P (field_decl_type)) | |
9453 | resolve_and_layout (field_decl_type, NULL_TREE); | |
9454 | if (TYPE_ARRAY_P (field_decl_type)) | |
9455 | CLASS_LOADED_P (field_decl_type) = 1; | |
e04a16fb AG |
9456 | |
9457 | /* Check on accessibility here */ | |
9458 | if (not_accessible_p (type, field_decl, from_super)) | |
9459 | { | |
9460 | parse_error_context | |
9461 | (qual_wfl, | |
9462 | "Can't access %s field `%s.%s' from `%s'", | |
9463 | java_accstring_lookup | |
9464 | (get_access_flags_from_decl (field_decl)), | |
2aa11e97 | 9465 | GET_TYPE_NAME (type), |
e04a16fb AG |
9466 | IDENTIFIER_POINTER (DECL_NAME (field_decl)), |
9467 | IDENTIFIER_POINTER | |
9468 | (DECL_NAME (TYPE_NAME (current_class)))); | |
9469 | return 1; | |
9470 | } | |
5e942c50 | 9471 | check_deprecation (qual_wfl, field_decl); |
e04a16fb AG |
9472 | |
9473 | /* There are things to check when fields are accessed | |
9474 | from type. There are no restrictions on a static | |
9475 | declaration of the field when it is accessed from an | |
9476 | interface */ | |
9477 | is_static = FIELD_STATIC (field_decl); | |
9478 | if (!from_super && from_type | |
c2952b01 APB |
9479 | && !TYPE_INTERFACE_P (type) |
9480 | && !is_static | |
9481 | && (current_function_decl | |
9482 | && METHOD_STATIC (current_function_decl))) | |
e04a16fb | 9483 | { |
7f10c2e2 | 9484 | static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type); |
e04a16fb AG |
9485 | return 1; |
9486 | } | |
9487 | from_cast = from_super = 0; | |
9488 | ||
c2952b01 APB |
9489 | /* It's an access from a type but it isn't static, we |
9490 | make it relative to `this'. */ | |
9491 | if (!is_static && from_type) | |
9492 | decl = current_this; | |
9493 | ||
5e942c50 APB |
9494 | /* If we need to generate something to get a proper |
9495 | handle on what this field is accessed from, do it | |
9496 | now. */ | |
e04a16fb AG |
9497 | if (!is_static) |
9498 | { | |
c583dd46 | 9499 | decl = maybe_access_field (decl, *where_found, *type_found); |
e04a16fb AG |
9500 | if (decl == error_mark_node) |
9501 | return 1; | |
9502 | } | |
9503 | ||
9504 | /* We want to keep the location were found it, and the type | |
9505 | we found. */ | |
9506 | *where_found = decl; | |
9507 | *type_found = type; | |
9508 | ||
c2952b01 APB |
9509 | /* Generate the correct expression for field access from |
9510 | qualified this */ | |
9511 | if (from_qualified_this) | |
9512 | { | |
9513 | field_decl = build_outer_field_access (qual_wfl, field_decl); | |
9514 | from_qualified_this = 0; | |
9515 | } | |
9516 | ||
e04a16fb AG |
9517 | /* This is the decl found and eventually the next one to |
9518 | search from */ | |
9519 | decl = field_decl; | |
9520 | } | |
e04a16fb AG |
9521 | from_type = 0; |
9522 | type = QUAL_DECL_TYPE (decl); | |
c2952b01 APB |
9523 | |
9524 | /* Sneak preview. If decl is qualified by a `new', report | |
9525 | the error here to be accurate on the peculiar construct */ | |
9526 | if (TREE_CHAIN (q) | |
9527 | && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR | |
9528 | && !JREFERENCE_TYPE_P (type)) | |
9529 | { | |
9530 | parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'", | |
9531 | lang_printable_name (type, 0)); | |
9532 | return 1; | |
9533 | } | |
e04a16fb | 9534 | } |
dde1da72 APB |
9535 | /* `q' might have changed due to a after package resolution |
9536 | re-qualification */ | |
9537 | if (!q) | |
9538 | break; | |
e04a16fb AG |
9539 | } |
9540 | *found_decl = decl; | |
9541 | return 0; | |
9542 | } | |
9543 | ||
9544 | /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl) | |
4dbf4496 APB |
9545 | can't be accessed from REFERENCE (a record type). This should be |
9546 | used when decl is a field or a method.*/ | |
e04a16fb | 9547 | |
be245ac0 KG |
9548 | static int |
9549 | not_accessible_p (reference, member, from_super) | |
e04a16fb AG |
9550 | tree reference, member; |
9551 | int from_super; | |
9552 | { | |
9553 | int access_flag = get_access_flags_from_decl (member); | |
9554 | ||
4dbf4496 APB |
9555 | /* Inner classes are processed by check_inner_class_access */ |
9556 | if (INNER_CLASS_TYPE_P (reference)) | |
9557 | return 0; | |
9558 | ||
e04a16fb AG |
9559 | /* Access always granted for members declared public */ |
9560 | if (access_flag & ACC_PUBLIC) | |
9561 | return 0; | |
9562 | ||
9563 | /* Check access on protected members */ | |
9564 | if (access_flag & ACC_PROTECTED) | |
9565 | { | |
9566 | /* Access granted if it occurs from within the package | |
9567 | containing the class in which the protected member is | |
9568 | declared */ | |
9569 | if (class_in_current_package (DECL_CONTEXT (member))) | |
9570 | return 0; | |
9571 | ||
9bbc7d9f PB |
9572 | /* If accessed with the form `super.member', then access is granted */ |
9573 | if (from_super) | |
9574 | return 0; | |
e04a16fb | 9575 | |
9bbc7d9f | 9576 | /* Otherwise, access is granted if occuring from the class where |
4dbf4496 APB |
9577 | member is declared or a subclass of it. Find the right |
9578 | context to perform the check */ | |
9579 | if (PURE_INNER_CLASS_TYPE_P (reference)) | |
9580 | { | |
9581 | while (INNER_CLASS_TYPE_P (reference)) | |
9582 | { | |
9583 | if (inherits_from_p (reference, DECL_CONTEXT (member))) | |
9584 | return 0; | |
9585 | reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference))); | |
9586 | } | |
9587 | } | |
473e7b07 | 9588 | if (inherits_from_p (reference, DECL_CONTEXT (member))) |
9bbc7d9f | 9589 | return 0; |
e04a16fb AG |
9590 | return 1; |
9591 | } | |
9592 | ||
9593 | /* Check access on private members. Access is granted only if it | |
473e7b07 | 9594 | occurs from within the class in which it is declared. Exceptions |
4dbf4496 | 9595 | are accesses from inner-classes. */ |
e04a16fb | 9596 | if (access_flag & ACC_PRIVATE) |
c2952b01 APB |
9597 | return (current_class == DECL_CONTEXT (member) ? 0 : |
9598 | (INNER_CLASS_TYPE_P (current_class) ? 0 : 1)); | |
e04a16fb AG |
9599 | |
9600 | /* Default access are permitted only when occuring within the | |
9601 | package in which the type (REFERENCE) is declared. In other words, | |
9602 | REFERENCE is defined in the current package */ | |
9603 | if (ctxp->package) | |
9604 | return !class_in_current_package (reference); | |
473e7b07 | 9605 | |
e04a16fb AG |
9606 | /* Otherwise, access is granted */ |
9607 | return 0; | |
9608 | } | |
9609 | ||
5e942c50 APB |
9610 | /* Test deprecated decl access. */ |
9611 | static void | |
9612 | check_deprecation (wfl, decl) | |
9613 | tree wfl, decl; | |
9614 | { | |
49f48c71 | 9615 | const char *file = DECL_SOURCE_FILE (decl); |
5e942c50 APB |
9616 | /* Complain if the field is deprecated and the file it was defined |
9617 | in isn't compiled at the same time the file which contains its | |
9618 | use is */ | |
9619 | if (DECL_DEPRECATED (decl) | |
9620 | && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file))) | |
9621 | { | |
9622 | char the [20]; | |
9623 | switch (TREE_CODE (decl)) | |
9624 | { | |
9625 | case FUNCTION_DECL: | |
9626 | strcpy (the, "method"); | |
9627 | break; | |
9628 | case FIELD_DECL: | |
9629 | strcpy (the, "field"); | |
9630 | break; | |
9631 | case TYPE_DECL: | |
9632 | strcpy (the, "class"); | |
9633 | break; | |
15fdcfe9 PB |
9634 | default: |
9635 | fatal ("unexpected DECL code - check_deprecation"); | |
5e942c50 APB |
9636 | } |
9637 | parse_warning_context | |
9638 | (wfl, "The %s `%s' in class `%s' has been deprecated", | |
9639 | the, lang_printable_name (decl, 0), | |
9640 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))); | |
9641 | } | |
9642 | } | |
9643 | ||
e04a16fb AG |
9644 | /* Returns 1 if class was declared in the current package, 0 otherwise */ |
9645 | ||
9646 | static int | |
9647 | class_in_current_package (class) | |
9648 | tree class; | |
9649 | { | |
9650 | static tree cache = NULL_TREE; | |
9651 | int qualified_flag; | |
9652 | tree left; | |
9653 | ||
9654 | if (cache == class) | |
9655 | return 1; | |
9656 | ||
9657 | qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class))); | |
9658 | ||
9659 | /* If the current package is empty and the name of CLASS is | |
9660 | qualified, class isn't in the current package. If there is a | |
9661 | current package and the name of the CLASS is not qualified, class | |
9662 | isn't in the current package */ | |
0a2138e2 | 9663 | if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag)) |
e04a16fb AG |
9664 | return 0; |
9665 | ||
9666 | /* If there is not package and the name of CLASS isn't qualified, | |
9667 | they belong to the same unnamed package */ | |
9668 | if (!ctxp->package && !qualified_flag) | |
9669 | return 1; | |
9670 | ||
9671 | /* Compare the left part of the name of CLASS with the package name */ | |
9672 | breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class))); | |
9673 | if (ctxp->package == left) | |
9674 | { | |
19e223db MM |
9675 | static int initialized_p; |
9676 | /* Register CACHE with the garbage collector. */ | |
9677 | if (!initialized_p) | |
9678 | { | |
9679 | ggc_add_tree_root (&cache, 1); | |
9680 | initialized_p = 1; | |
9681 | } | |
9682 | ||
e04a16fb AG |
9683 | cache = class; |
9684 | return 1; | |
9685 | } | |
9686 | return 0; | |
9687 | } | |
9688 | ||
9689 | /* This function may generate code to access DECL from WHERE. This is | |
9690 | done only if certain conditions meet. */ | |
9691 | ||
9692 | static tree | |
9693 | maybe_access_field (decl, where, type) | |
9694 | tree decl, where, type; | |
9695 | { | |
5e942c50 APB |
9696 | if (TREE_CODE (decl) == FIELD_DECL && decl != current_this |
9697 | && !FIELD_STATIC (decl)) | |
e04a16fb | 9698 | decl = build_field_ref (where ? where : current_this, |
c583dd46 APB |
9699 | (type ? type : DECL_CONTEXT (decl)), |
9700 | DECL_NAME (decl)); | |
e04a16fb AG |
9701 | return decl; |
9702 | } | |
9703 | ||
15fdcfe9 | 9704 | /* Build a method invocation, by patching PATCH. If non NULL |
e04a16fb AG |
9705 | and according to the situation, PRIMARY and WHERE may be |
9706 | used. IS_STATIC is set to 1 if the invoked function is static. */ | |
9707 | ||
9708 | static tree | |
89e09b9a | 9709 | patch_method_invocation (patch, primary, where, is_static, ret_decl) |
e04a16fb AG |
9710 | tree patch, primary, where; |
9711 | int *is_static; | |
b9f7e36c | 9712 | tree *ret_decl; |
e04a16fb AG |
9713 | { |
9714 | tree wfl = TREE_OPERAND (patch, 0); | |
9715 | tree args = TREE_OPERAND (patch, 1); | |
9716 | tree name = EXPR_WFL_NODE (wfl); | |
5e942c50 | 9717 | tree list; |
22eed1e6 | 9718 | int is_static_flag = 0; |
89e09b9a | 9719 | int is_super_init = 0; |
bccaf73a | 9720 | tree this_arg = NULL_TREE; |
e04a16fb AG |
9721 | |
9722 | /* Should be overriden if everything goes well. Otherwise, if | |
9723 | something fails, it should keep this value. It stop the | |
9724 | evaluation of a bogus assignment. See java_complete_tree, | |
9725 | MODIFY_EXPR: for the reasons why we sometimes want to keep on | |
9726 | evaluating an assignment */ | |
9727 | TREE_TYPE (patch) = error_mark_node; | |
9728 | ||
9729 | /* Since lookup functions are messing with line numbers, save the | |
9730 | context now. */ | |
9731 | java_parser_context_save_global (); | |
9732 | ||
9733 | /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */ | |
9734 | ||
9735 | /* Resolution of qualified name, excluding constructors */ | |
9736 | if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch)) | |
9737 | { | |
dde1da72 | 9738 | tree identifier, identifier_wfl, type, resolved; |
e04a16fb AG |
9739 | /* Extract the last IDENTIFIER of the qualified |
9740 | expression. This is a wfl and we will use it's location | |
9741 | data during error report. */ | |
9742 | identifier_wfl = cut_identifier_in_qualified (wfl); | |
9743 | identifier = EXPR_WFL_NODE (identifier_wfl); | |
9744 | ||
9745 | /* Given the context, IDENTIFIER is syntactically qualified | |
9746 | as a MethodName. We need to qualify what's before */ | |
9747 | qualify_ambiguous_name (wfl); | |
dde1da72 | 9748 | resolved = resolve_field_access (wfl, NULL, NULL); |
e04a16fb | 9749 | |
dde1da72 APB |
9750 | if (resolved == error_mark_node) |
9751 | PATCH_METHOD_RETURN_ERROR (); | |
9752 | ||
9753 | type = GET_SKIP_TYPE (resolved); | |
9754 | resolve_and_layout (type, NULL_TREE); | |
6518c7b5 BM |
9755 | |
9756 | if (JPRIMITIVE_TYPE_P (type)) | |
9757 | { | |
7e51098e TT |
9758 | parse_error_context |
9759 | (identifier_wfl, | |
9760 | "Can't invoke a method on primitive type `%s'", | |
9761 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); | |
9762 | PATCH_METHOD_RETURN_ERROR (); | |
9763 | } | |
9764 | ||
dde1da72 APB |
9765 | list = lookup_method_invoke (0, identifier_wfl, type, identifier, args); |
9766 | args = nreverse (args); | |
2c56429a | 9767 | |
e04a16fb | 9768 | /* We're resolving a call from a type */ |
dde1da72 | 9769 | if (TREE_CODE (resolved) == TYPE_DECL) |
e04a16fb | 9770 | { |
dde1da72 | 9771 | if (CLASS_INTERFACE (resolved)) |
e04a16fb AG |
9772 | { |
9773 | parse_error_context | |
781b0558 KG |
9774 | (identifier_wfl, |
9775 | "Can't make static reference to method `%s' in interface `%s'", | |
9776 | IDENTIFIER_POINTER (identifier), | |
e04a16fb | 9777 | IDENTIFIER_POINTER (name)); |
b9f7e36c | 9778 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb | 9779 | } |
e04a16fb AG |
9780 | if (list && !METHOD_STATIC (list)) |
9781 | { | |
c2e3db92 | 9782 | char *fct_name = xstrdup (lang_printable_name (list, 0)); |
e04a16fb AG |
9783 | parse_error_context |
9784 | (identifier_wfl, | |
9785 | "Can't make static reference to method `%s %s' in class `%s'", | |
0a2138e2 APB |
9786 | lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0), |
9787 | fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); | |
e04a16fb | 9788 | free (fct_name); |
b9f7e36c | 9789 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb AG |
9790 | } |
9791 | } | |
e04a16fb | 9792 | else |
dde1da72 APB |
9793 | this_arg = primary = resolved; |
9794 | ||
5e942c50 | 9795 | /* IDENTIFIER_WFL will be used to report any problem further */ |
e04a16fb AG |
9796 | wfl = identifier_wfl; |
9797 | } | |
9798 | /* Resolution of simple names, names generated after a primary: or | |
9799 | constructors */ | |
9800 | else | |
9801 | { | |
cd531a2e | 9802 | tree class_to_search = NULL_TREE; |
c2952b01 | 9803 | int lc; /* Looking for Constructor */ |
e04a16fb AG |
9804 | |
9805 | /* We search constructor in their target class */ | |
9806 | if (CALL_CONSTRUCTOR_P (patch)) | |
9807 | { | |
22eed1e6 APB |
9808 | if (TREE_CODE (patch) == NEW_CLASS_EXPR) |
9809 | class_to_search = EXPR_WFL_NODE (wfl); | |
9810 | else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) == | |
9811 | this_identifier_node) | |
9812 | class_to_search = NULL_TREE; | |
9813 | else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) == | |
9814 | super_identifier_node) | |
e04a16fb | 9815 | { |
89e09b9a | 9816 | is_super_init = 1; |
22eed1e6 APB |
9817 | if (CLASSTYPE_SUPER (current_class)) |
9818 | class_to_search = | |
9819 | DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class))); | |
9820 | else | |
9821 | { | |
781b0558 | 9822 | parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object"); |
22eed1e6 APB |
9823 | PATCH_METHOD_RETURN_ERROR (); |
9824 | } | |
e04a16fb | 9825 | } |
22eed1e6 APB |
9826 | |
9827 | /* Class to search is NULL if we're searching the current one */ | |
9828 | if (class_to_search) | |
e04a16fb | 9829 | { |
c2952b01 APB |
9830 | class_to_search = resolve_and_layout (class_to_search, wfl); |
9831 | ||
22eed1e6 APB |
9832 | if (!class_to_search) |
9833 | { | |
9834 | parse_error_context | |
9835 | (wfl, "Class `%s' not found in type declaration", | |
9836 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
9837 | PATCH_METHOD_RETURN_ERROR (); | |
9838 | } | |
9839 | ||
5e942c50 APB |
9840 | /* Can't instantiate an abstract class, but we can |
9841 | invoke it's constructor. It's use within the `new' | |
9842 | context is denied here. */ | |
9843 | if (CLASS_ABSTRACT (class_to_search) | |
9844 | && TREE_CODE (patch) == NEW_CLASS_EXPR) | |
22eed1e6 APB |
9845 | { |
9846 | parse_error_context | |
781b0558 KG |
9847 | (wfl, "Class `%s' is an abstract class. It can't be instantiated", |
9848 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
22eed1e6 APB |
9849 | PATCH_METHOD_RETURN_ERROR (); |
9850 | } | |
c2952b01 | 9851 | |
22eed1e6 | 9852 | class_to_search = TREE_TYPE (class_to_search); |
e04a16fb | 9853 | } |
22eed1e6 APB |
9854 | else |
9855 | class_to_search = current_class; | |
e04a16fb AG |
9856 | lc = 1; |
9857 | } | |
9858 | /* This is a regular search in the local class, unless an | |
9859 | alternate class is specified. */ | |
9860 | else | |
9861 | { | |
9862 | class_to_search = (where ? where : current_class); | |
9863 | lc = 0; | |
9864 | } | |
c2952b01 | 9865 | |
e04a16fb AG |
9866 | /* NAME is a simple identifier or comes from a primary. Search |
9867 | in the class whose declaration contain the method being | |
9868 | invoked. */ | |
c877974e | 9869 | resolve_and_layout (class_to_search, NULL_TREE); |
e04a16fb | 9870 | |
c2952b01 | 9871 | list = lookup_method_invoke (lc, wfl, class_to_search, name, args); |
e04a16fb AG |
9872 | /* Don't continue if no method were found, as the next statement |
9873 | can't be executed then. */ | |
b9f7e36c APB |
9874 | if (!list) |
9875 | PATCH_METHOD_RETURN_ERROR (); | |
e04a16fb AG |
9876 | |
9877 | /* Check for static reference if non static methods */ | |
9878 | if (check_for_static_method_reference (wfl, patch, list, | |
9879 | class_to_search, primary)) | |
b9f7e36c | 9880 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb | 9881 | |
165f37bc APB |
9882 | /* Check for inner classes creation from illegal contexts */ |
9883 | if (lc && (INNER_CLASS_TYPE_P (class_to_search) | |
9884 | && !CLASS_STATIC (TYPE_NAME (class_to_search))) | |
9885 | && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)) | |
9886 | { | |
9887 | parse_error_context | |
9888 | (wfl, "No enclosing instance for inner class `%s' is in scope%s", | |
9889 | lang_printable_name (class_to_search, 0), | |
9890 | (!current_this ? "" : | |
9891 | "; an explicit one must be provided when creating this inner class")); | |
9892 | PATCH_METHOD_RETURN_ERROR (); | |
9893 | } | |
9894 | ||
22eed1e6 APB |
9895 | /* Non static methods are called with the current object extra |
9896 | argument. If patch a `new TYPE()', the argument is the value | |
9897 | returned by the object allocator. If method is resolved as a | |
9898 | primary, use the primary otherwise use the current THIS. */ | |
b9f7e36c | 9899 | args = nreverse (args); |
bccaf73a | 9900 | if (TREE_CODE (patch) != NEW_CLASS_EXPR) |
c2952b01 APB |
9901 | { |
9902 | this_arg = primary ? primary : current_this; | |
9903 | ||
9904 | /* If we're using an access method, things are different. | |
9905 | There are two familly of cases: | |
9906 | ||
9907 | 1) We're not generating bytecodes: | |
9908 | ||
9909 | - LIST is non static. It's invocation is transformed from | |
9910 | x(a1,...,an) into this$<n>.x(a1,....an). | |
9911 | - LIST is static. It's invocation is transformed from | |
9912 | x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an) | |
9913 | ||
9914 | 2) We're generating bytecodes: | |
9915 | ||
9916 | - LIST is non static. It's invocation is transformed from | |
9917 | x(a1,....,an) into access$<n>(this$<n>,a1,...,an). | |
9918 | - LIST is static. It's invocation is transformed from | |
9919 | x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an). | |
9920 | ||
9921 | Of course, this$<n> can be abitrary complex, ranging from | |
9922 | this$0 (the immediate outer context) to | |
9923 | access$0(access$0(...(this$0))). | |
9924 | ||
9925 | maybe_use_access_method returns a non zero value if the | |
dfb99c83 | 9926 | this_arg has to be moved into the (then generated) stub |
4dbf4496 | 9927 | argument list. In the meantime, the selected function |
dfb99c83 | 9928 | might have be replaced by a generated stub. */ |
c2952b01 APB |
9929 | if (maybe_use_access_method (is_super_init, &list, &this_arg)) |
9930 | args = tree_cons (NULL_TREE, this_arg, args); | |
9931 | } | |
e04a16fb | 9932 | } |
b67d701b | 9933 | |
e04a16fb AG |
9934 | /* Merge point of all resolution schemes. If we have nothing, this |
9935 | is an error, already signaled */ | |
b9f7e36c APB |
9936 | if (!list) |
9937 | PATCH_METHOD_RETURN_ERROR (); | |
b67d701b | 9938 | |
e04a16fb AG |
9939 | /* Check accessibility, position the is_static flag, build and |
9940 | return the call */ | |
9bbc7d9f | 9941 | if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0)) |
e04a16fb | 9942 | { |
c2e3db92 | 9943 | char *fct_name = xstrdup (lang_printable_name (list, 0)); |
e04a16fb AG |
9944 | parse_error_context |
9945 | (wfl, "Can't access %s method `%s %s.%s' from `%s'", | |
9946 | java_accstring_lookup (get_access_flags_from_decl (list)), | |
0a2138e2 | 9947 | lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0), |
5e942c50 APB |
9948 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))), |
9949 | fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)))); | |
e04a16fb | 9950 | free (fct_name); |
b9f7e36c | 9951 | PATCH_METHOD_RETURN_ERROR (); |
e04a16fb | 9952 | } |
5e942c50 | 9953 | check_deprecation (wfl, list); |
22eed1e6 | 9954 | |
c2952b01 APB |
9955 | /* If invoking a innerclass constructor, there are hidden parameters |
9956 | to pass */ | |
9957 | if (TREE_CODE (patch) == NEW_CLASS_EXPR | |
9958 | && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list))) | |
9959 | { | |
9960 | /* And make sure we add the accessed local variables to be saved | |
9961 | in field aliases. */ | |
9962 | args = build_alias_initializer_parameter_list | |
9963 | (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL); | |
9964 | ||
da632f2c | 9965 | /* Secretly pass the current_this/primary as a second argument */ |
165f37bc APB |
9966 | if (primary || current_this) |
9967 | args = tree_cons (NULL_TREE, (primary ? primary : current_this), args); | |
9968 | else | |
9969 | args = tree_cons (NULL_TREE, integer_zero_node, args); | |
c2952b01 APB |
9970 | } |
9971 | ||
152de068 APB |
9972 | /* This handles the situation where a constructor invocation needs |
9973 | to have an enclosing context passed as a second parameter (the | |
9974 | constructor is one of an inner class. We extract it from the | |
9975 | current function. */ | |
9976 | if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list))) | |
9977 | { | |
9978 | tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class)); | |
9979 | tree extra_arg; | |
9980 | ||
9981 | if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl)) | |
9982 | { | |
9983 | extra_arg = DECL_FUNCTION_BODY (current_function_decl); | |
9984 | extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg)); | |
9985 | } | |
9986 | else | |
9987 | { | |
9988 | tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl)); | |
9989 | extra_arg = | |
9990 | build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0); | |
9991 | extra_arg = java_complete_tree (extra_arg); | |
9992 | } | |
9993 | args = tree_cons (NULL_TREE, extra_arg, args); | |
9994 | } | |
9995 | ||
22eed1e6 | 9996 | is_static_flag = METHOD_STATIC (list); |
dba41d30 | 9997 | if (! is_static_flag && this_arg != NULL_TREE) |
bccaf73a | 9998 | args = tree_cons (NULL_TREE, this_arg, args); |
22eed1e6 | 9999 | |
c3f2a476 APB |
10000 | /* In the context of an explicit constructor invocation, we can't |
10001 | invoke any method relying on `this'. Exceptions are: we're | |
10002 | invoking a static function, primary exists and is not the current | |
10003 | this, we're creating a new object. */ | |
22eed1e6 | 10004 | if (ctxp->explicit_constructor_p |
c3f2a476 APB |
10005 | && !is_static_flag |
10006 | && (!primary || primary == current_this) | |
10007 | && (TREE_CODE (patch) != NEW_CLASS_EXPR)) | |
22eed1e6 | 10008 | { |
781b0558 | 10009 | parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called"); |
22eed1e6 APB |
10010 | PATCH_METHOD_RETURN_ERROR (); |
10011 | } | |
e04a16fb | 10012 | java_parser_context_restore_global (); |
22eed1e6 APB |
10013 | if (is_static) |
10014 | *is_static = is_static_flag; | |
b9f7e36c APB |
10015 | /* Sometimes, we want the decl of the selected method. Such as for |
10016 | EH checking */ | |
10017 | if (ret_decl) | |
10018 | *ret_decl = list; | |
89e09b9a PB |
10019 | patch = patch_invoke (patch, list, args); |
10020 | if (is_super_init && CLASS_HAS_FINIT_P (current_class)) | |
10021 | { | |
c2952b01 APB |
10022 | tree finit_parms, finit_call; |
10023 | ||
c00f0fb2 | 10024 | /* Prepare to pass hidden parameters to finit$, if any. */ |
c2952b01 APB |
10025 | finit_parms = build_alias_initializer_parameter_list |
10026 | (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL); | |
89e09b9a | 10027 | |
c2952b01 APB |
10028 | finit_call = |
10029 | build_method_invocation (build_wfl_node (finit_identifier_node), | |
10030 | finit_parms); | |
10031 | ||
10032 | /* Generate the code used to initialize fields declared with an | |
10033 | initialization statement and build a compound statement along | |
10034 | with the super constructor invocation. */ | |
89e09b9a PB |
10035 | patch = build (COMPOUND_EXPR, void_type_node, patch, |
10036 | java_complete_tree (finit_call)); | |
10037 | CAN_COMPLETE_NORMALLY (patch) = 1; | |
10038 | } | |
10039 | return patch; | |
e04a16fb AG |
10040 | } |
10041 | ||
10042 | /* Check that we're not trying to do a static reference to a method in | |
10043 | non static method. Return 1 if it's the case, 0 otherwise. */ | |
10044 | ||
10045 | static int | |
10046 | check_for_static_method_reference (wfl, node, method, where, primary) | |
10047 | tree wfl, node, method, where, primary; | |
10048 | { | |
10049 | if (METHOD_STATIC (current_function_decl) | |
10050 | && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node)) | |
10051 | { | |
c2e3db92 | 10052 | char *fct_name = xstrdup (lang_printable_name (method, 0)); |
e04a16fb AG |
10053 | parse_error_context |
10054 | (wfl, "Can't make static reference to method `%s %s' in class `%s'", | |
0a2138e2 | 10055 | lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name, |
e04a16fb AG |
10056 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where)))); |
10057 | free (fct_name); | |
10058 | return 1; | |
10059 | } | |
10060 | return 0; | |
10061 | } | |
10062 | ||
c2952b01 APB |
10063 | /* Fix the invocation of *MDECL if necessary in the case of a |
10064 | invocation from an inner class. *THIS_ARG might be modified | |
10065 | appropriately and an alternative access to *MDECL might be | |
10066 | returned. */ | |
10067 | ||
10068 | static int | |
10069 | maybe_use_access_method (is_super_init, mdecl, this_arg) | |
10070 | int is_super_init; | |
10071 | tree *mdecl, *this_arg; | |
10072 | { | |
10073 | tree ctx; | |
10074 | tree md = *mdecl, ta = *this_arg; | |
10075 | int to_return = 0; | |
10076 | int non_static_context = !METHOD_STATIC (md); | |
10077 | ||
10078 | if (is_super_init | |
165f37bc APB |
10079 | || DECL_CONTEXT (md) == current_class |
10080 | || !PURE_INNER_CLASS_TYPE_P (current_class) | |
10081 | || DECL_FINIT_P (md)) | |
c2952b01 APB |
10082 | return 0; |
10083 | ||
10084 | /* If we're calling a method found in an enclosing class, generate | |
10085 | what it takes to retrieve the right this. Don't do that if we're | |
10086 | invoking a static method. */ | |
10087 | ||
10088 | if (non_static_context) | |
10089 | { | |
10090 | ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class))); | |
4dbf4496 | 10091 | if (inherits_from_p (ctx, DECL_CONTEXT (md))) |
c2952b01 APB |
10092 | { |
10093 | ta = build_current_thisn (current_class); | |
10094 | ta = build_wfl_node (ta); | |
10095 | } | |
10096 | else | |
10097 | { | |
10098 | tree type = ctx; | |
10099 | while (type) | |
10100 | { | |
10101 | maybe_build_thisn_access_method (type); | |
4dbf4496 | 10102 | if (inherits_from_p (type, DECL_CONTEXT (md))) |
c2952b01 APB |
10103 | { |
10104 | ta = build_access_to_thisn (ctx, type, 0); | |
10105 | break; | |
10106 | } | |
10107 | type = (DECL_CONTEXT (TYPE_NAME (type)) ? | |
10108 | TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE); | |
10109 | } | |
10110 | } | |
10111 | ta = java_complete_tree (ta); | |
10112 | } | |
10113 | ||
10114 | /* We might have to use an access method to get to MD. We can | |
10115 | break the method access rule as far as we're not generating | |
10116 | bytecode */ | |
10117 | if (METHOD_PRIVATE (md) && flag_emit_class_files) | |
10118 | { | |
10119 | md = build_outer_method_access_method (md); | |
10120 | to_return = 1; | |
10121 | } | |
10122 | ||
10123 | *mdecl = md; | |
10124 | *this_arg = ta; | |
10125 | ||
10126 | /* Returnin a non zero value indicates we were doing a non static | |
10127 | method invokation that is now a static invocation. It will have | |
10128 | callee displace `this' to insert it in the regular argument | |
10129 | list. */ | |
10130 | return (non_static_context && to_return); | |
10131 | } | |
10132 | ||
e04a16fb AG |
10133 | /* Patch an invoke expression METHOD and ARGS, based on its invocation |
10134 | mode. */ | |
10135 | ||
10136 | static tree | |
89e09b9a | 10137 | patch_invoke (patch, method, args) |
e04a16fb | 10138 | tree patch, method, args; |
e04a16fb AG |
10139 | { |
10140 | tree dtable, func; | |
0a2138e2 | 10141 | tree original_call, t, ta; |
e815887f | 10142 | tree cond = NULL_TREE; |
e04a16fb | 10143 | |
5e942c50 APB |
10144 | /* Last step for args: convert build-in types. If we're dealing with |
10145 | a new TYPE() type call, the first argument to the constructor | |
e815887f | 10146 | isn't found in the incoming argument list, but delivered by |
5e942c50 APB |
10147 | `new' */ |
10148 | t = TYPE_ARG_TYPES (TREE_TYPE (method)); | |
10149 | if (TREE_CODE (patch) == NEW_CLASS_EXPR) | |
10150 | t = TREE_CHAIN (t); | |
ac825856 APB |
10151 | for (ta = args; t != end_params_node && ta; |
10152 | t = TREE_CHAIN (t), ta = TREE_CHAIN (ta)) | |
b9f7e36c APB |
10153 | if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) && |
10154 | TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t)) | |
10155 | TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta)); | |
1a6d4fb7 APB |
10156 | |
10157 | /* Resolve unresolved returned type isses */ | |
10158 | t = TREE_TYPE (TREE_TYPE (method)); | |
10159 | if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t))) | |
10160 | resolve_and_layout (TREE_TYPE (t), NULL); | |
c2952b01 | 10161 | |
e8fc7396 | 10162 | if (flag_emit_class_files || flag_emit_xref) |
15fdcfe9 PB |
10163 | func = method; |
10164 | else | |
e04a16fb | 10165 | { |
15fdcfe9 | 10166 | tree signature = build_java_signature (TREE_TYPE (method)); |
89e09b9a | 10167 | switch (invocation_mode (method, CALL_USING_SUPER (patch))) |
15fdcfe9 PB |
10168 | { |
10169 | case INVOKE_VIRTUAL: | |
10170 | dtable = invoke_build_dtable (0, args); | |
10171 | func = build_invokevirtual (dtable, method); | |
10172 | break; | |
b9f7e36c | 10173 | |
e815887f TT |
10174 | case INVOKE_NONVIRTUAL: |
10175 | /* If the object for the method call is null, we throw an | |
10176 | exception. We don't do this if the object is the current | |
10177 | method's `this'. In other cases we just rely on an | |
10178 | optimization pass to eliminate redundant checks. */ | |
10179 | if (TREE_VALUE (args) != current_this) | |
10180 | { | |
10181 | /* We use a SAVE_EXPR here to make sure we only evaluate | |
10182 | the new `self' expression once. */ | |
10183 | tree save_arg = save_expr (TREE_VALUE (args)); | |
10184 | TREE_VALUE (args) = save_arg; | |
10185 | cond = build (EQ_EXPR, boolean_type_node, save_arg, | |
10186 | null_pointer_node); | |
10187 | } | |
10188 | /* Fall through. */ | |
10189 | ||
15fdcfe9 PB |
10190 | case INVOKE_SUPER: |
10191 | case INVOKE_STATIC: | |
10192 | func = build_known_method_ref (method, TREE_TYPE (method), | |
10193 | DECL_CONTEXT (method), | |
10194 | signature, args); | |
10195 | break; | |
e04a16fb | 10196 | |
15fdcfe9 PB |
10197 | case INVOKE_INTERFACE: |
10198 | dtable = invoke_build_dtable (1, args); | |
173f556c | 10199 | func = build_invokeinterface (dtable, method); |
15fdcfe9 | 10200 | break; |
5e942c50 | 10201 | |
15fdcfe9 | 10202 | default: |
89e09b9a | 10203 | fatal ("internal error - unknown invocation_mode result"); |
15fdcfe9 PB |
10204 | } |
10205 | ||
10206 | /* Ensure self_type is initialized, (invokestatic). FIXME */ | |
10207 | func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func); | |
e04a16fb AG |
10208 | } |
10209 | ||
e04a16fb AG |
10210 | TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method)); |
10211 | TREE_OPERAND (patch, 0) = func; | |
10212 | TREE_OPERAND (patch, 1) = args; | |
10213 | original_call = patch; | |
10214 | ||
e815887f | 10215 | /* We're processing a `new TYPE ()' form. New is called and its |
22eed1e6 APB |
10216 | returned value is the first argument to the constructor. We build |
10217 | a COMPOUND_EXPR and use saved expression so that the overall NEW | |
10218 | expression value is a pointer to a newly created and initialized | |
10219 | class. */ | |
10220 | if (TREE_CODE (original_call) == NEW_CLASS_EXPR) | |
e04a16fb AG |
10221 | { |
10222 | tree class = DECL_CONTEXT (method); | |
10223 | tree c1, saved_new, size, new; | |
e8fc7396 | 10224 | if (flag_emit_class_files || flag_emit_xref) |
15fdcfe9 PB |
10225 | { |
10226 | TREE_TYPE (patch) = build_pointer_type (class); | |
10227 | return patch; | |
10228 | } | |
e04a16fb AG |
10229 | if (!TYPE_SIZE (class)) |
10230 | safe_layout_class (class); | |
10231 | size = size_in_bytes (class); | |
10232 | new = build (CALL_EXPR, promote_type (class), | |
10233 | build_address_of (alloc_object_node), | |
10234 | tree_cons (NULL_TREE, build_class_ref (class), | |
10235 | build_tree_list (NULL_TREE, | |
10236 | size_in_bytes (class))), | |
10237 | NULL_TREE); | |
10238 | saved_new = save_expr (new); | |
10239 | c1 = build_tree_list (NULL_TREE, saved_new); | |
10240 | TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1); | |
10241 | TREE_OPERAND (original_call, 1) = c1; | |
10242 | TREE_SET_CODE (original_call, CALL_EXPR); | |
10243 | patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new); | |
10244 | } | |
e815887f TT |
10245 | |
10246 | /* If COND is set, then we are building a check to see if the object | |
10247 | is NULL. */ | |
10248 | if (cond != NULL_TREE) | |
10249 | { | |
10250 | /* We have to make the `then' branch a compound expression to | |
10251 | make the types turn out right. This seems bizarre. */ | |
10252 | patch = build (COND_EXPR, TREE_TYPE (patch), cond, | |
10253 | build (COMPOUND_EXPR, TREE_TYPE (patch), | |
10254 | build (CALL_EXPR, void_type_node, | |
10255 | build_address_of (soft_nullpointer_node), | |
10256 | NULL_TREE, NULL_TREE), | |
10257 | (FLOAT_TYPE_P (TREE_TYPE (patch)) | |
10258 | ? build_real (TREE_TYPE (patch), dconst0) | |
10259 | : build1 (CONVERT_EXPR, TREE_TYPE (patch), | |
10260 | integer_zero_node))), | |
10261 | patch); | |
10262 | TREE_SIDE_EFFECTS (patch) = 1; | |
10263 | } | |
10264 | ||
e04a16fb AG |
10265 | return patch; |
10266 | } | |
10267 | ||
10268 | static int | |
10269 | invocation_mode (method, super) | |
10270 | tree method; | |
10271 | int super; | |
10272 | { | |
10273 | int access = get_access_flags_from_decl (method); | |
10274 | ||
22eed1e6 APB |
10275 | if (super) |
10276 | return INVOKE_SUPER; | |
10277 | ||
e815887f | 10278 | if (access & ACC_STATIC) |
e04a16fb AG |
10279 | return INVOKE_STATIC; |
10280 | ||
e815887f TT |
10281 | /* We have to look for a constructor before we handle nonvirtual |
10282 | calls; otherwise the constructor will look nonvirtual. */ | |
10283 | if (DECL_CONSTRUCTOR_P (method)) | |
e04a16fb | 10284 | return INVOKE_STATIC; |
e815887f TT |
10285 | |
10286 | if (access & ACC_FINAL || access & ACC_PRIVATE) | |
10287 | return INVOKE_NONVIRTUAL; | |
10288 | ||
10289 | if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method)))) | |
10290 | return INVOKE_NONVIRTUAL; | |
10291 | ||
e04a16fb AG |
10292 | if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))) |
10293 | return INVOKE_INTERFACE; | |
22eed1e6 | 10294 | |
e04a16fb AG |
10295 | return INVOKE_VIRTUAL; |
10296 | } | |
10297 | ||
b67d701b PB |
10298 | /* Retrieve a refined list of matching methods. It covers the step |
10299 | 15.11.2 (Compile-Time Step 2) */ | |
e04a16fb AG |
10300 | |
10301 | static tree | |
10302 | lookup_method_invoke (lc, cl, class, name, arg_list) | |
10303 | int lc; | |
10304 | tree cl; | |
10305 | tree class, name, arg_list; | |
10306 | { | |
de4c7b02 | 10307 | tree atl = end_params_node; /* Arg Type List */ |
c877974e | 10308 | tree method, signature, list, node; |
49f48c71 | 10309 | const char *candidates; /* Used for error report */ |
b5b8a0e7 | 10310 | char *dup; |
e04a16fb | 10311 | |
5e942c50 | 10312 | /* Fix the arguments */ |
e04a16fb AG |
10313 | for (node = arg_list; node; node = TREE_CHAIN (node)) |
10314 | { | |
e3884b71 | 10315 | tree current_arg = TREE_TYPE (TREE_VALUE (node)); |
c877974e | 10316 | /* Non primitive type may have to be resolved */ |
e3884b71 | 10317 | if (!JPRIMITIVE_TYPE_P (current_arg)) |
c877974e APB |
10318 | resolve_and_layout (current_arg, NULL_TREE); |
10319 | /* And promoted */ | |
b67d701b | 10320 | if (TREE_CODE (current_arg) == RECORD_TYPE) |
c877974e | 10321 | current_arg = promote_type (current_arg); |
5e942c50 | 10322 | atl = tree_cons (NULL_TREE, current_arg, atl); |
e04a16fb | 10323 | } |
e04a16fb | 10324 | |
c2952b01 APB |
10325 | /* Presto. If we're dealing with an anonymous class and a |
10326 | constructor call, generate the right constructor now, since we | |
10327 | know the arguments' types. */ | |
10328 | ||
10329 | if (lc && ANONYMOUS_CLASS_P (class)) | |
10330 | craft_constructor (TYPE_NAME (class), atl); | |
10331 | ||
5e942c50 APB |
10332 | /* Find all candidates and then refine the list, searching for the |
10333 | most specific method. */ | |
10334 | list = find_applicable_accessible_methods_list (lc, class, name, atl); | |
10335 | list = find_most_specific_methods_list (list); | |
b67d701b PB |
10336 | if (list && !TREE_CHAIN (list)) |
10337 | return TREE_VALUE (list); | |
e04a16fb | 10338 | |
b67d701b PB |
10339 | /* Issue an error. List candidates if any. Candidates are listed |
10340 | only if accessible (non accessible methods may end-up here for | |
10341 | the sake of a better error report). */ | |
10342 | candidates = NULL; | |
10343 | if (list) | |
e04a16fb | 10344 | { |
e04a16fb | 10345 | tree current; |
b67d701b | 10346 | obstack_grow (&temporary_obstack, ". Candidates are:\n", 18); |
e04a16fb AG |
10347 | for (current = list; current; current = TREE_CHAIN (current)) |
10348 | { | |
b67d701b PB |
10349 | tree cm = TREE_VALUE (current); |
10350 | char string [4096]; | |
10351 | if (!cm || not_accessible_p (class, cm, 0)) | |
10352 | continue; | |
b67d701b | 10353 | sprintf |
22eed1e6 APB |
10354 | (string, " `%s' in `%s'%s", |
10355 | get_printable_method_name (cm), | |
b67d701b PB |
10356 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))), |
10357 | (TREE_CHAIN (current) ? "\n" : "")); | |
10358 | obstack_grow (&temporary_obstack, string, strlen (string)); | |
10359 | } | |
10360 | obstack_1grow (&temporary_obstack, '\0'); | |
10361 | candidates = obstack_finish (&temporary_obstack); | |
10362 | } | |
10363 | /* Issue the error message */ | |
c877974e APB |
10364 | method = make_node (FUNCTION_TYPE); |
10365 | TYPE_ARG_TYPES (method) = atl; | |
b67d701b | 10366 | signature = build_java_argument_signature (method); |
c63b98cd | 10367 | dup = xstrdup (lang_printable_name (class, 0)); |
b5b8a0e7 | 10368 | parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s", |
22eed1e6 | 10369 | (lc ? "constructor" : "method"), |
b5b8a0e7 APB |
10370 | (lc ? dup : IDENTIFIER_POINTER (name)), |
10371 | IDENTIFIER_POINTER (signature), dup, | |
b67d701b | 10372 | (candidates ? candidates : "")); |
b5b8a0e7 | 10373 | free (dup); |
b67d701b PB |
10374 | return NULL_TREE; |
10375 | } | |
10376 | ||
5e942c50 APB |
10377 | /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1 |
10378 | when we're looking for a constructor. */ | |
b67d701b PB |
10379 | |
10380 | static tree | |
5e942c50 APB |
10381 | find_applicable_accessible_methods_list (lc, class, name, arglist) |
10382 | int lc; | |
b67d701b PB |
10383 | tree class, name, arglist; |
10384 | { | |
ad69b5b6 BM |
10385 | static struct hash_table t, *searched_classes = NULL; |
10386 | static int search_not_done = 0; | |
b67d701b PB |
10387 | tree list = NULL_TREE, all_list = NULL_TREE; |
10388 | ||
ad69b5b6 BM |
10389 | /* Check the hash table to determine if this class has been searched |
10390 | already. */ | |
10391 | if (searched_classes) | |
10392 | { | |
10393 | if (hash_lookup (searched_classes, | |
10394 | (const hash_table_key) class, FALSE, NULL)) | |
10395 | return NULL; | |
10396 | } | |
10397 | else | |
10398 | { | |
10399 | hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node, | |
10400 | java_hash_compare_tree_node); | |
10401 | searched_classes = &t; | |
10402 | } | |
10403 | ||
10404 | search_not_done++; | |
10405 | hash_lookup (searched_classes, | |
0c2b8145 | 10406 | (const hash_table_key) class, TRUE, NULL); |
ad69b5b6 | 10407 | |
c2952b01 APB |
10408 | if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class)) |
10409 | { | |
10410 | load_class (class, 1); | |
10411 | safe_layout_class (class); | |
10412 | } | |
10413 | ||
1982388a | 10414 | /* Search interfaces */ |
9a7ab4b3 APB |
10415 | if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL |
10416 | && CLASS_INTERFACE (TYPE_NAME (class))) | |
b67d701b | 10417 | { |
1982388a APB |
10418 | int i, n; |
10419 | tree basetype_vec = TYPE_BINFO_BASETYPES (class); | |
165f37bc APB |
10420 | search_applicable_methods_list (lc, TYPE_METHODS (class), |
10421 | name, arglist, &list, &all_list); | |
1982388a | 10422 | n = TREE_VEC_LENGTH (basetype_vec); |
165f37bc | 10423 | for (i = 1; i < n; i++) |
b67d701b | 10424 | { |
de0b553f APB |
10425 | tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)); |
10426 | tree rlist; | |
10427 | ||
de0b553f APB |
10428 | rlist = find_applicable_accessible_methods_list (lc, t, name, |
10429 | arglist); | |
165f37bc | 10430 | list = chainon (rlist, list); |
e04a16fb | 10431 | } |
e04a16fb | 10432 | } |
1982388a APB |
10433 | /* Search classes */ |
10434 | else | |
c2952b01 | 10435 | { |
165f37bc APB |
10436 | tree sc = class; |
10437 | int seen_inner_class = 0; | |
c2952b01 APB |
10438 | search_applicable_methods_list (lc, TYPE_METHODS (class), |
10439 | name, arglist, &list, &all_list); | |
10440 | ||
165f37bc APB |
10441 | /* We must search all interfaces of this class */ |
10442 | if (!lc) | |
10443 | { | |
10444 | tree basetype_vec = TYPE_BINFO_BASETYPES (sc); | |
10445 | int n = TREE_VEC_LENGTH (basetype_vec), i; | |
165f37bc APB |
10446 | for (i = 1; i < n; i++) |
10447 | { | |
10448 | tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)); | |
165f37bc | 10449 | if (t != object_type_node) |
30a3caef ZW |
10450 | { |
10451 | tree rlist | |
10452 | = find_applicable_accessible_methods_list (lc, t, | |
10453 | name, arglist); | |
10454 | list = chainon (rlist, list); | |
10455 | } | |
165f37bc | 10456 | } |
165f37bc APB |
10457 | } |
10458 | ||
c2952b01 APB |
10459 | /* Search enclosing context of inner classes before looking |
10460 | ancestors up. */ | |
10461 | while (!lc && INNER_CLASS_TYPE_P (class)) | |
10462 | { | |
165f37bc APB |
10463 | tree rlist; |
10464 | seen_inner_class = 1; | |
c2952b01 | 10465 | class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class))); |
165f37bc APB |
10466 | rlist = find_applicable_accessible_methods_list (lc, class, |
10467 | name, arglist); | |
10468 | list = chainon (rlist, list); | |
c2952b01 | 10469 | } |
165f37bc APB |
10470 | |
10471 | if (!lc && seen_inner_class | |
10472 | && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc)) | |
10473 | class = CLASSTYPE_SUPER (sc); | |
10474 | else | |
10475 | class = sc; | |
10476 | ||
ad69b5b6 BM |
10477 | /* Search superclass */ |
10478 | if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE) | |
10479 | { | |
10480 | tree rlist; | |
10481 | class = CLASSTYPE_SUPER (class); | |
10482 | rlist = find_applicable_accessible_methods_list (lc, class, | |
10483 | name, arglist); | |
10484 | list = chainon (rlist, list); | |
10485 | } | |
10486 | } | |
10487 | ||
10488 | search_not_done--; | |
10489 | ||
10490 | /* We're done. Reset the searched classes list and finally search | |
10491 | java.lang.Object if it wasn't searched already. */ | |
10492 | if (!search_not_done) | |
10493 | { | |
10494 | if (!lc | |
10495 | && TYPE_METHODS (object_type_node) | |
10496 | && !hash_lookup (searched_classes, | |
10497 | (const hash_table_key) object_type_node, | |
10498 | FALSE, NULL)) | |
10499 | { | |
10500 | search_applicable_methods_list (lc, | |
10501 | TYPE_METHODS (object_type_node), | |
10502 | name, arglist, &list, &all_list); | |
10503 | } | |
10504 | hash_table_free (searched_classes); | |
10505 | searched_classes = NULL; | |
c2952b01 | 10506 | } |
1982388a | 10507 | |
b67d701b PB |
10508 | /* Either return the list obtained or all selected (but |
10509 | inaccessible) methods for better error report. */ | |
10510 | return (!list ? all_list : list); | |
10511 | } | |
e04a16fb | 10512 | |
ad69b5b6 | 10513 | /* Effectively search for the appropriate method in method */ |
1982388a APB |
10514 | |
10515 | static void | |
c2952b01 | 10516 | search_applicable_methods_list (lc, method, name, arglist, list, all_list) |
1982388a APB |
10517 | int lc; |
10518 | tree method, name, arglist; | |
10519 | tree *list, *all_list; | |
10520 | { | |
10521 | for (; method; method = TREE_CHAIN (method)) | |
10522 | { | |
10523 | /* When dealing with constructor, stop here, otherwise search | |
10524 | other classes */ | |
10525 | if (lc && !DECL_CONSTRUCTOR_P (method)) | |
10526 | continue; | |
10527 | else if (!lc && (DECL_CONSTRUCTOR_P (method) | |
10528 | || (GET_METHOD_NAME (method) != name))) | |
10529 | continue; | |
10530 | ||
10531 | if (argument_types_convertible (method, arglist)) | |
10532 | { | |
10533 | /* Retain accessible methods only */ | |
10534 | if (!not_accessible_p (DECL_CONTEXT (current_function_decl), | |
10535 | method, 0)) | |
10536 | *list = tree_cons (NULL_TREE, method, *list); | |
10537 | else | |
10538 | /* Also retain all selected method here */ | |
10539 | *all_list = tree_cons (NULL_TREE, method, *list); | |
10540 | } | |
10541 | } | |
ad69b5b6 | 10542 | } |
1982388a | 10543 | |
b67d701b PB |
10544 | /* 15.11.2.2 Choose the Most Specific Method */ |
10545 | ||
10546 | static tree | |
10547 | find_most_specific_methods_list (list) | |
10548 | tree list; | |
10549 | { | |
10550 | int max = 0; | |
9a7ab4b3 | 10551 | int abstract, candidates; |
b67d701b PB |
10552 | tree current, new_list = NULL_TREE; |
10553 | for (current = list; current; current = TREE_CHAIN (current)) | |
e04a16fb | 10554 | { |
b67d701b PB |
10555 | tree method; |
10556 | DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0; | |
10557 | ||
10558 | for (method = list; method; method = TREE_CHAIN (method)) | |
10559 | { | |
0c2b8145 | 10560 | tree method_v, current_v; |
b67d701b PB |
10561 | /* Don't test a method against itself */ |
10562 | if (method == current) | |
10563 | continue; | |
10564 | ||
0c2b8145 APB |
10565 | method_v = TREE_VALUE (method); |
10566 | current_v = TREE_VALUE (current); | |
10567 | ||
10568 | /* Compare arguments and location where methods where declared */ | |
10569 | if (argument_types_convertible (method_v, current_v)) | |
b67d701b | 10570 | { |
0c2b8145 APB |
10571 | if (valid_method_invocation_conversion_p |
10572 | (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v)) | |
10573 | || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) | |
10574 | && enclosing_context_p (DECL_CONTEXT (method_v), | |
10575 | DECL_CONTEXT (current_v)))) | |
10576 | { | |
10577 | int v = (DECL_SPECIFIC_COUNT (current_v) += | |
10578 | (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1)); | |
10579 | max = (v > max ? v : max); | |
10580 | } | |
b67d701b PB |
10581 | } |
10582 | } | |
e04a16fb AG |
10583 | } |
10584 | ||
b67d701b | 10585 | /* Review the list and select the maximally specific methods */ |
9a7ab4b3 APB |
10586 | for (current = list, abstract = -1, candidates = -1; |
10587 | current; current = TREE_CHAIN (current)) | |
b67d701b | 10588 | if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max) |
9a7ab4b3 APB |
10589 | { |
10590 | new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list); | |
10591 | abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0); | |
10592 | candidates++; | |
10593 | } | |
b67d701b | 10594 | |
165f37bc APB |
10595 | /* If we have several and they're all abstract, just pick the |
10596 | closest one. */ | |
9a7ab4b3 APB |
10597 | if (candidates > 0 && (candidates == abstract)) |
10598 | { | |
10599 | new_list = nreverse (new_list); | |
10600 | TREE_CHAIN (new_list) = NULL_TREE; | |
10601 | } | |
165f37bc | 10602 | |
cab8e2bd BM |
10603 | /* We have several (we couldn't find a most specific), all but one |
10604 | are abstract, we pick the only non abstract one. */ | |
10605 | if (candidates > 0 && (candidates == abstract+1)) | |
165f37bc | 10606 | { |
9a7ab4b3 APB |
10607 | for (current = new_list; current; current = TREE_CHAIN (current)) |
10608 | if (!METHOD_ABSTRACT (TREE_VALUE (current))) | |
10609 | { | |
10610 | TREE_CHAIN (current) = NULL_TREE; | |
10611 | new_list = current; | |
10612 | } | |
165f37bc APB |
10613 | } |
10614 | ||
b67d701b PB |
10615 | /* If we can't find one, lower expectations and try to gather multiple |
10616 | maximally specific methods */ | |
165f37bc | 10617 | while (!new_list && max) |
b67d701b PB |
10618 | { |
10619 | while (--max > 0) | |
10620 | { | |
10621 | if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max) | |
10622 | new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list); | |
10623 | } | |
b67d701b PB |
10624 | } |
10625 | ||
10626 | return new_list; | |
e04a16fb AG |
10627 | } |
10628 | ||
b67d701b PB |
10629 | /* Make sure that the type of each M2_OR_ARGLIST arguments can be |
10630 | converted by method invocation conversion (5.3) to the type of the | |
10631 | corresponding parameter of M1. Implementation expects M2_OR_ARGLIST | |
10632 | to change less often than M1. */ | |
e04a16fb | 10633 | |
b67d701b PB |
10634 | static int |
10635 | argument_types_convertible (m1, m2_or_arglist) | |
10636 | tree m1, m2_or_arglist; | |
e04a16fb | 10637 | { |
b67d701b PB |
10638 | static tree m2_arg_value = NULL_TREE; |
10639 | static tree m2_arg_cache = NULL_TREE; | |
19e223db | 10640 | static int initialized_p; |
e04a16fb | 10641 | |
b67d701b | 10642 | register tree m1_arg, m2_arg; |
e04a16fb | 10643 | |
19e223db MM |
10644 | /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage |
10645 | collector. */ | |
10646 | if (!initialized_p) | |
10647 | { | |
10648 | ggc_add_tree_root (&m2_arg_value, 1); | |
10649 | ggc_add_tree_root (&m2_arg_cache, 1); | |
10650 | initialized_p = 1; | |
10651 | } | |
10652 | ||
c2952b01 | 10653 | SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1) |
e04a16fb | 10654 | |
b67d701b PB |
10655 | if (m2_arg_value == m2_or_arglist) |
10656 | m2_arg = m2_arg_cache; | |
10657 | else | |
10658 | { | |
10659 | /* M2_OR_ARGLIST can be a function DECL or a raw list of | |
10660 | argument types */ | |
10661 | if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL) | |
10662 | { | |
10663 | m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist)); | |
10664 | if (!METHOD_STATIC (m2_or_arglist)) | |
10665 | m2_arg = TREE_CHAIN (m2_arg); | |
10666 | } | |
10667 | else | |
10668 | m2_arg = m2_or_arglist; | |
e04a16fb | 10669 | |
b67d701b PB |
10670 | m2_arg_value = m2_or_arglist; |
10671 | m2_arg_cache = m2_arg; | |
10672 | } | |
e04a16fb | 10673 | |
de4c7b02 | 10674 | while (m1_arg != end_params_node && m2_arg != end_params_node) |
b67d701b | 10675 | { |
c877974e | 10676 | resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE); |
b67d701b PB |
10677 | if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg), |
10678 | TREE_VALUE (m2_arg))) | |
10679 | break; | |
10680 | m1_arg = TREE_CHAIN (m1_arg); | |
10681 | m2_arg = TREE_CHAIN (m2_arg); | |
e04a16fb | 10682 | } |
de4c7b02 | 10683 | return m1_arg == end_params_node && m2_arg == end_params_node; |
e04a16fb AG |
10684 | } |
10685 | ||
10686 | /* Qualification routines */ | |
10687 | ||
10688 | static void | |
10689 | qualify_ambiguous_name (id) | |
10690 | tree id; | |
10691 | { | |
cd531a2e KG |
10692 | tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE, |
10693 | saved_current_class; | |
d8fccff5 | 10694 | int again, super_found = 0, this_found = 0, new_array_found = 0; |
8576f094 | 10695 | int code; |
e04a16fb AG |
10696 | |
10697 | /* We first qualify the first element, then derive qualification of | |
10698 | others based on the first one. If the first element is qualified | |
10699 | by a resolution (field or type), this resolution is stored in the | |
10700 | QUAL_RESOLUTION of the qual element being examined. We need to | |
10701 | save the current_class since the use of SUPER might change the | |
10702 | its value. */ | |
10703 | saved_current_class = current_class; | |
10704 | qual = EXPR_WFL_QUALIFICATION (id); | |
10705 | do { | |
10706 | ||
10707 | /* Simple qualified expression feature a qual_wfl that is a | |
10708 | WFL. Expression derived from a primary feature more complicated | |
10709 | things like a CALL_EXPR. Expression from primary need to be | |
10710 | worked out to extract the part on which the qualification will | |
10711 | take place. */ | |
10712 | qual_wfl = QUAL_WFL (qual); | |
10713 | switch (TREE_CODE (qual_wfl)) | |
10714 | { | |
10715 | case CALL_EXPR: | |
10716 | qual_wfl = TREE_OPERAND (qual_wfl, 0); | |
10717 | if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION) | |
10718 | { | |
10719 | qual = EXPR_WFL_QUALIFICATION (qual_wfl); | |
10720 | qual_wfl = QUAL_WFL (qual); | |
10721 | } | |
10722 | break; | |
d8fccff5 | 10723 | case NEW_ARRAY_EXPR: |
c2952b01 | 10724 | case NEW_ANONYMOUS_ARRAY_EXPR: |
d8fccff5 | 10725 | qual = TREE_CHAIN (qual); |
1a6d4fb7 | 10726 | again = new_array_found = 1; |
d8fccff5 | 10727 | continue; |
e04a16fb | 10728 | case CONVERT_EXPR: |
f2760b27 APB |
10729 | break; |
10730 | case NEW_CLASS_EXPR: | |
e04a16fb AG |
10731 | qual_wfl = TREE_OPERAND (qual_wfl, 0); |
10732 | break; | |
c583dd46 APB |
10733 | case ARRAY_REF: |
10734 | while (TREE_CODE (qual_wfl) == ARRAY_REF) | |
10735 | qual_wfl = TREE_OPERAND (qual_wfl, 0); | |
10736 | break; | |
8576f094 APB |
10737 | case STRING_CST: |
10738 | qual = TREE_CHAIN (qual); | |
10739 | qual_wfl = QUAL_WFL (qual); | |
10740 | break; | |
165f37bc APB |
10741 | case CLASS_LITERAL: |
10742 | qual = TREE_CHAIN (qual); | |
10743 | qual_wfl = QUAL_WFL (qual); | |
10744 | break; | |
0a2138e2 APB |
10745 | default: |
10746 | /* Fix for -Wall. Just break doing nothing */ | |
10747 | break; | |
e04a16fb | 10748 | } |
8576f094 | 10749 | |
e04a16fb AG |
10750 | ptr_type = current_class; |
10751 | again = 0; | |
8576f094 APB |
10752 | code = TREE_CODE (qual_wfl); |
10753 | ||
10754 | /* Pos evaluation: non WFL leading expression nodes */ | |
10755 | if (code == CONVERT_EXPR | |
10756 | && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION) | |
10757 | name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl)); | |
10758 | ||
cd7c5840 APB |
10759 | else if (code == INTEGER_CST) |
10760 | name = qual_wfl; | |
10761 | ||
ac22f9cb | 10762 | else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) && |
8576f094 APB |
10763 | TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION) |
10764 | name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0)); | |
10765 | ||
c2952b01 APB |
10766 | else if (code == TREE_LIST) |
10767 | name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl)); | |
10768 | ||
37feda7d APB |
10769 | else if (code == STRING_CST || code == CONDITIONAL_EXPR |
10770 | || code == PLUS_EXPR) | |
8576f094 APB |
10771 | { |
10772 | qual = TREE_CHAIN (qual); | |
10773 | qual_wfl = QUAL_WFL (qual); | |
10774 | again = 1; | |
10775 | } | |
10776 | else | |
f441f671 APB |
10777 | { |
10778 | name = EXPR_WFL_NODE (qual_wfl); | |
10779 | if (!name) | |
10780 | { | |
10781 | qual = EXPR_WFL_QUALIFICATION (qual_wfl); | |
10782 | again = 1; | |
10783 | } | |
10784 | } | |
10785 | ||
e04a16fb AG |
10786 | /* If we have a THIS (from a primary), we set the context accordingly */ |
10787 | if (name == this_identifier_node) | |
10788 | { | |
6e22695a APB |
10789 | /* This isn't really elegant. One more added irregularity |
10790 | before I start using COMPONENT_REF (hopefully very soon.) */ | |
10791 | if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF | |
10792 | && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) == | |
10793 | EXPR_WITH_FILE_LOCATION | |
10794 | && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) == | |
10795 | this_identifier_node) | |
10796 | { | |
10797 | qual = TREE_OPERAND (TREE_PURPOSE (qual), 0); | |
10798 | qual = EXPR_WFL_QUALIFICATION (qual); | |
10799 | } | |
e04a16fb AG |
10800 | qual = TREE_CHAIN (qual); |
10801 | qual_wfl = QUAL_WFL (qual); | |
22eed1e6 APB |
10802 | if (TREE_CODE (qual_wfl) == CALL_EXPR) |
10803 | again = 1; | |
10804 | else | |
10805 | name = EXPR_WFL_NODE (qual_wfl); | |
e04a16fb AG |
10806 | this_found = 1; |
10807 | } | |
10808 | /* If we have a SUPER, we set the context accordingly */ | |
10809 | if (name == super_identifier_node) | |
10810 | { | |
10811 | current_class = CLASSTYPE_SUPER (ptr_type); | |
10812 | /* Check that there is such a thing as a super class. If not, | |
10813 | return. The error will be caught later on, during the | |
10814 | resolution */ | |
10815 | if (!current_class) | |
10816 | { | |
10817 | current_class = saved_current_class; | |
10818 | return; | |
10819 | } | |
10820 | qual = TREE_CHAIN (qual); | |
10821 | /* Do one more interation to set things up */ | |
10822 | super_found = again = 1; | |
10823 | } | |
10824 | } while (again); | |
10825 | ||
f2760b27 APB |
10826 | /* If name appears within the scope of a local variable declaration |
10827 | or parameter declaration, then it is an expression name. We don't | |
10828 | carry this test out if we're in the context of the use of SUPER | |
10829 | or THIS */ | |
cd7c5840 APB |
10830 | if (!this_found && !super_found |
10831 | && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST | |
10832 | && (decl = IDENTIFIER_LOCAL_VALUE (name))) | |
e04a16fb AG |
10833 | { |
10834 | RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1; | |
10835 | QUAL_RESOLUTION (qual) = decl; | |
10836 | } | |
10837 | ||
10838 | /* If within the class/interface NAME was found to be used there | |
10839 | exists a (possibly inherited) field named NAME, then this is an | |
d8fccff5 APB |
10840 | expression name. If we saw a NEW_ARRAY_EXPR before and want to |
10841 | address length, it is OK. */ | |
10842 | else if ((decl = lookup_field_wrapper (ptr_type, name)) | |
10843 | || (new_array_found && name == length_identifier_node)) | |
e04a16fb AG |
10844 | { |
10845 | RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1; | |
d8fccff5 | 10846 | QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl); |
e04a16fb AG |
10847 | } |
10848 | ||
1a6d4fb7 | 10849 | /* We reclassify NAME as yielding to a type name resolution if: |
e04a16fb AG |
10850 | - NAME is a class/interface declared within the compilation |
10851 | unit containing NAME, | |
10852 | - NAME is imported via a single-type-import declaration, | |
10853 | - NAME is declared in an another compilation unit of the package | |
10854 | of the compilation unit containing NAME, | |
10855 | - NAME is declared by exactly on type-import-on-demand declaration | |
1a6d4fb7 APB |
10856 | of the compilation unit containing NAME. |
10857 | - NAME is actually a STRING_CST. */ | |
cd7c5840 APB |
10858 | else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST |
10859 | || (decl = resolve_and_layout (name, NULL_TREE))) | |
e04a16fb AG |
10860 | { |
10861 | RESOLVE_TYPE_NAME_P (qual_wfl) = 1; | |
10862 | QUAL_RESOLUTION (qual) = decl; | |
10863 | } | |
10864 | ||
f2760b27 | 10865 | /* Method call, array references and cast are expression name */ |
9bbc7d9f | 10866 | else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR |
8576f094 APB |
10867 | || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF |
10868 | || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR) | |
e04a16fb AG |
10869 | RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1; |
10870 | ||
10871 | /* Check here that NAME isn't declared by more than one | |
10872 | type-import-on-demand declaration of the compilation unit | |
10873 | containing NAME. FIXME */ | |
10874 | ||
10875 | /* Otherwise, NAME is reclassified as a package name */ | |
10876 | else | |
10877 | RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1; | |
10878 | ||
10879 | /* Propagate the qualification accross other components of the | |
10880 | qualified name */ | |
10881 | for (qual = TREE_CHAIN (qual); qual; | |
10882 | qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual)) | |
10883 | { | |
10884 | if (RESOLVE_PACKAGE_NAME_P (qual_wfl)) | |
10885 | RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1; | |
10886 | else | |
10887 | RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1; | |
10888 | } | |
10889 | ||
10890 | /* Store the global qualification for the ambiguous part of ID back | |
10891 | into ID fields */ | |
10892 | if (RESOLVE_EXPRESSION_NAME_P (qual_wfl)) | |
10893 | RESOLVE_EXPRESSION_NAME_P (id) = 1; | |
10894 | else if (RESOLVE_TYPE_NAME_P (qual_wfl)) | |
10895 | RESOLVE_TYPE_NAME_P (id) = 1; | |
10896 | else if (RESOLVE_PACKAGE_NAME_P (qual_wfl)) | |
10897 | RESOLVE_PACKAGE_NAME_P (id) = 1; | |
10898 | ||
10899 | /* Restore the current class */ | |
10900 | current_class = saved_current_class; | |
10901 | } | |
10902 | ||
10903 | static int | |
10904 | breakdown_qualified (left, right, source) | |
10905 | tree *left, *right, source; | |
10906 | { | |
63ad61ed | 10907 | char *p, *base; |
e04a16fb AG |
10908 | int l = IDENTIFIER_LENGTH (source); |
10909 | ||
63ad61ed ZW |
10910 | base = alloca (l + 1); |
10911 | memcpy (base, IDENTIFIER_POINTER (source), l + 1); | |
10912 | ||
e04a16fb | 10913 | /* Breakdown NAME into REMAINDER . IDENTIFIER */ |
63ad61ed | 10914 | p = base + l - 1; |
e04a16fb AG |
10915 | while (*p != '.' && p != base) |
10916 | p--; | |
10917 | ||
10918 | /* We didn't find a '.'. Return an error */ | |
10919 | if (p == base) | |
10920 | return 1; | |
10921 | ||
10922 | *p = '\0'; | |
10923 | if (right) | |
10924 | *right = get_identifier (p+1); | |
63ad61ed | 10925 | *left = get_identifier (base); |
e04a16fb AG |
10926 | |
10927 | return 0; | |
10928 | } | |
10929 | ||
e04a16fb | 10930 | /* Patch tree nodes in a function body. When a BLOCK is found, push |
5b09b33e PB |
10931 | local variable decls if present. |
10932 | Same as java_complete_lhs, but does resolve static finals to values. */ | |
e04a16fb AG |
10933 | |
10934 | static tree | |
10935 | java_complete_tree (node) | |
10936 | tree node; | |
5b09b33e PB |
10937 | { |
10938 | node = java_complete_lhs (node); | |
0c2b8145 APB |
10939 | if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node) |
10940 | && DECL_INITIAL (node) != NULL_TREE | |
7f10c2e2 | 10941 | && !flag_emit_xref) |
5b09b33e PB |
10942 | { |
10943 | tree value = DECL_INITIAL (node); | |
10944 | DECL_INITIAL (node) = NULL_TREE; | |
10945 | value = fold_constant_for_init (value, node); | |
10946 | DECL_INITIAL (node) = value; | |
10947 | if (value != NULL_TREE) | |
c2952b01 APB |
10948 | { |
10949 | /* fold_constant_for_init sometimes widen the original type | |
10950 | of the constant (i.e. byte to int.) It's not desirable, | |
10951 | especially if NODE is a function argument. */ | |
10952 | if (TREE_CODE (value) == INTEGER_CST | |
10953 | && TREE_TYPE (node) != TREE_TYPE (value)) | |
10954 | return convert (TREE_TYPE (node), value); | |
10955 | else | |
10956 | return value; | |
10957 | } | |
5b09b33e PB |
10958 | } |
10959 | return node; | |
10960 | } | |
10961 | ||
2aa11e97 APB |
10962 | static tree |
10963 | java_stabilize_reference (node) | |
10964 | tree node; | |
10965 | { | |
10966 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
10967 | { | |
10968 | tree op0 = TREE_OPERAND (node, 0); | |
10969 | tree op1 = TREE_OPERAND (node, 1); | |
642f15d1 | 10970 | TREE_OPERAND (node, 0) = save_expr (op0); |
2aa11e97 APB |
10971 | TREE_OPERAND (node, 1) = java_stabilize_reference (op1); |
10972 | return node; | |
10973 | } | |
5cbdba64 | 10974 | return stabilize_reference (node); |
2aa11e97 APB |
10975 | } |
10976 | ||
5b09b33e PB |
10977 | /* Patch tree nodes in a function body. When a BLOCK is found, push |
10978 | local variable decls if present. | |
10979 | Same as java_complete_tree, but does not resolve static finals to values. */ | |
10980 | ||
10981 | static tree | |
10982 | java_complete_lhs (node) | |
10983 | tree node; | |
e04a16fb | 10984 | { |
22eed1e6 | 10985 | tree nn, cn, wfl_op1, wfl_op2, wfl_op3; |
b67d701b | 10986 | int flag; |
e04a16fb AG |
10987 | |
10988 | /* CONVERT_EXPR always has its type set, even though it needs to be | |
b67d701b | 10989 | worked out. */ |
e04a16fb AG |
10990 | if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR) |
10991 | return node; | |
10992 | ||
10993 | /* The switch block implements cases processing container nodes | |
10994 | first. Contained nodes are always written back. Leaves come | |
10995 | next and return a value. */ | |
10996 | switch (TREE_CODE (node)) | |
10997 | { | |
10998 | case BLOCK: | |
10999 | ||
11000 | /* 1- Block section. | |
11001 | Set the local values on decl names so we can identify them | |
11002 | faster when they're referenced. At that stage, identifiers | |
11003 | are legal so we don't check for declaration errors. */ | |
11004 | for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn)) | |
11005 | { | |
11006 | DECL_CONTEXT (cn) = current_function_decl; | |
11007 | IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn; | |
e04a16fb | 11008 | } |
15fdcfe9 PB |
11009 | if (BLOCK_EXPR_BODY (node) == NULL_TREE) |
11010 | CAN_COMPLETE_NORMALLY (node) = 1; | |
11011 | else | |
e04a16fb | 11012 | { |
15fdcfe9 PB |
11013 | tree stmt = BLOCK_EXPR_BODY (node); |
11014 | tree *ptr; | |
11015 | int error_seen = 0; | |
11016 | if (TREE_CODE (stmt) == COMPOUND_EXPR) | |
11017 | { | |
c877974e APB |
11018 | /* Re-order from (((A; B); C); ...; Z) to |
11019 | (A; (B; (C ; (...; Z)))). | |
15fdcfe9 PB |
11020 | This makes it easier to scan the statements left-to-right |
11021 | without using recursion (which might overflow the stack | |
11022 | if the block has many statements. */ | |
11023 | for (;;) | |
11024 | { | |
11025 | tree left = TREE_OPERAND (stmt, 0); | |
11026 | if (TREE_CODE (left) != COMPOUND_EXPR) | |
11027 | break; | |
11028 | TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1); | |
11029 | TREE_OPERAND (left, 1) = stmt; | |
11030 | stmt = left; | |
11031 | } | |
11032 | BLOCK_EXPR_BODY (node) = stmt; | |
11033 | } | |
11034 | ||
c877974e APB |
11035 | /* Now do the actual complete, without deep recursion for |
11036 | long blocks. */ | |
15fdcfe9 | 11037 | ptr = &BLOCK_EXPR_BODY (node); |
dc0b3eff PB |
11038 | while (TREE_CODE (*ptr) == COMPOUND_EXPR |
11039 | && TREE_OPERAND (*ptr, 1) != empty_stmt_node) | |
15fdcfe9 PB |
11040 | { |
11041 | tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0)); | |
11042 | tree *next = &TREE_OPERAND (*ptr, 1); | |
11043 | TREE_OPERAND (*ptr, 0) = cur; | |
cd9643f7 PB |
11044 | if (cur == empty_stmt_node) |
11045 | { | |
11046 | /* Optimization; makes it easier to detect empty bodies. | |
11047 | Most useful for <clinit> with all-constant initializer. */ | |
11048 | *ptr = *next; | |
11049 | continue; | |
11050 | } | |
15fdcfe9 PB |
11051 | if (TREE_CODE (cur) == ERROR_MARK) |
11052 | error_seen++; | |
11053 | else if (! CAN_COMPLETE_NORMALLY (cur)) | |
11054 | { | |
11055 | wfl_op2 = *next; | |
11056 | for (;;) | |
11057 | { | |
11058 | if (TREE_CODE (wfl_op2) == BLOCK) | |
11059 | wfl_op2 = BLOCK_EXPR_BODY (wfl_op2); | |
11060 | else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR) | |
11061 | wfl_op2 = TREE_OPERAND (wfl_op2, 0); | |
11062 | else | |
11063 | break; | |
11064 | } | |
11065 | if (TREE_CODE (wfl_op2) != CASE_EXPR | |
dc0b3eff | 11066 | && TREE_CODE (wfl_op2) != DEFAULT_EXPR) |
82371d41 | 11067 | unreachable_stmt_error (*ptr); |
15fdcfe9 PB |
11068 | } |
11069 | ptr = next; | |
11070 | } | |
11071 | *ptr = java_complete_tree (*ptr); | |
11072 | ||
11073 | if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0) | |
e04a16fb | 11074 | return error_mark_node; |
15fdcfe9 | 11075 | CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr); |
e04a16fb AG |
11076 | } |
11077 | /* Turn local bindings to null */ | |
11078 | for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn)) | |
11079 | IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE; | |
11080 | ||
11081 | TREE_TYPE (node) = void_type_node; | |
11082 | break; | |
11083 | ||
11084 | /* 2- They are expressions but ultimately deal with statements */ | |
b67d701b | 11085 | |
b9f7e36c APB |
11086 | case THROW_EXPR: |
11087 | wfl_op1 = TREE_OPERAND (node, 0); | |
11088 | COMPLETE_CHECK_OP_0 (node); | |
c2952b01 APB |
11089 | /* 14.19 A throw statement cannot complete normally. */ |
11090 | CAN_COMPLETE_NORMALLY (node) = 0; | |
b9f7e36c APB |
11091 | return patch_throw_statement (node, wfl_op1); |
11092 | ||
11093 | case SYNCHRONIZED_EXPR: | |
11094 | wfl_op1 = TREE_OPERAND (node, 0); | |
b9f7e36c APB |
11095 | return patch_synchronized_statement (node, wfl_op1); |
11096 | ||
b67d701b PB |
11097 | case TRY_EXPR: |
11098 | return patch_try_statement (node); | |
11099 | ||
a7d8d81f PB |
11100 | case TRY_FINALLY_EXPR: |
11101 | COMPLETE_CHECK_OP_0 (node); | |
11102 | COMPLETE_CHECK_OP_1 (node); | |
11103 | CAN_COMPLETE_NORMALLY (node) | |
11104 | = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0)) | |
11105 | && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))); | |
11106 | TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0)); | |
11107 | return node; | |
11108 | ||
5a005d9e PB |
11109 | case CLEANUP_POINT_EXPR: |
11110 | COMPLETE_CHECK_OP_0 (node); | |
11111 | TREE_TYPE (node) = void_type_node; | |
2aa11e97 APB |
11112 | CAN_COMPLETE_NORMALLY (node) = |
11113 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0)); | |
5a005d9e PB |
11114 | return node; |
11115 | ||
11116 | case WITH_CLEANUP_EXPR: | |
11117 | COMPLETE_CHECK_OP_0 (node); | |
11118 | COMPLETE_CHECK_OP_2 (node); | |
2aa11e97 APB |
11119 | CAN_COMPLETE_NORMALLY (node) = |
11120 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0)); | |
5a005d9e PB |
11121 | TREE_TYPE (node) = void_type_node; |
11122 | return node; | |
11123 | ||
e04a16fb AG |
11124 | case LABELED_BLOCK_EXPR: |
11125 | PUSH_LABELED_BLOCK (node); | |
11126 | if (LABELED_BLOCK_BODY (node)) | |
11127 | COMPLETE_CHECK_OP_1 (node); | |
11128 | TREE_TYPE (node) = void_type_node; | |
11129 | POP_LABELED_BLOCK (); | |
1fb89a4d APB |
11130 | |
11131 | if (LABELED_BLOCK_BODY (node) == empty_stmt_node) | |
9dd939b2 APB |
11132 | { |
11133 | LABELED_BLOCK_BODY (node) = NULL_TREE; | |
11134 | CAN_COMPLETE_NORMALLY (node) = 1; | |
11135 | } | |
1fb89a4d | 11136 | else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node))) |
15fdcfe9 | 11137 | CAN_COMPLETE_NORMALLY (node) = 1; |
e04a16fb AG |
11138 | return node; |
11139 | ||
11140 | case EXIT_BLOCK_EXPR: | |
11141 | /* We don't complete operand 1, because it's the return value of | |
11142 | the EXIT_BLOCK_EXPR which doesn't exist it Java */ | |
11143 | return patch_bc_statement (node); | |
11144 | ||
15fdcfe9 PB |
11145 | case CASE_EXPR: |
11146 | cn = java_complete_tree (TREE_OPERAND (node, 0)); | |
11147 | if (cn == error_mark_node) | |
11148 | return cn; | |
11149 | ||
8576f094 APB |
11150 | /* First, the case expression must be constant. Values of final |
11151 | fields are accepted. */ | |
15fdcfe9 | 11152 | cn = fold (cn); |
8576f094 APB |
11153 | if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF) |
11154 | && JDECL_P (TREE_OPERAND (cn, 1)) | |
11155 | && FIELD_FINAL (TREE_OPERAND (cn, 1)) | |
11156 | && DECL_INITIAL (TREE_OPERAND (cn, 1))) | |
100f7cd8 | 11157 | { |
100f7cd8 APB |
11158 | cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)), |
11159 | TREE_OPERAND (cn, 1)); | |
100f7cd8 | 11160 | } |
15fdcfe9 | 11161 | |
ce6e9147 | 11162 | if (!TREE_CONSTANT (cn) && !flag_emit_xref) |
15fdcfe9 PB |
11163 | { |
11164 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11165 | parse_error_context (node, "Constant expression required"); | |
11166 | return error_mark_node; | |
11167 | } | |
11168 | ||
11169 | nn = ctxp->current_loop; | |
11170 | ||
11171 | /* It must be assignable to the type of the switch expression. */ | |
c877974e APB |
11172 | if (!try_builtin_assignconv (NULL_TREE, |
11173 | TREE_TYPE (TREE_OPERAND (nn, 0)), cn)) | |
15fdcfe9 PB |
11174 | { |
11175 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11176 | parse_error_context | |
11177 | (wfl_operator, | |
11178 | "Incompatible type for case. Can't convert `%s' to `int'", | |
11179 | lang_printable_name (TREE_TYPE (cn), 0)); | |
11180 | return error_mark_node; | |
11181 | } | |
11182 | ||
11183 | cn = fold (convert (int_type_node, cn)); | |
11184 | ||
11185 | /* Multiple instance of a case label bearing the same | |
11186 | value is checked during code generation. The case | |
11187 | expression is allright so far. */ | |
34d4df06 APB |
11188 | if (TREE_CODE (cn) == VAR_DECL) |
11189 | cn = DECL_INITIAL (cn); | |
15fdcfe9 | 11190 | TREE_OPERAND (node, 0) = cn; |
9bbc7d9f | 11191 | TREE_TYPE (node) = void_type_node; |
15fdcfe9 | 11192 | CAN_COMPLETE_NORMALLY (node) = 1; |
10100cc7 | 11193 | TREE_SIDE_EFFECTS (node) = 1; |
15fdcfe9 PB |
11194 | break; |
11195 | ||
11196 | case DEFAULT_EXPR: | |
11197 | nn = ctxp->current_loop; | |
11198 | /* Only one default label is allowed per switch statement */ | |
11199 | if (SWITCH_HAS_DEFAULT (nn)) | |
11200 | { | |
11201 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11202 | parse_error_context (wfl_operator, | |
11203 | "Duplicate case label: `default'"); | |
11204 | return error_mark_node; | |
11205 | } | |
11206 | else | |
11207 | SWITCH_HAS_DEFAULT (nn) = 1; | |
9bbc7d9f | 11208 | TREE_TYPE (node) = void_type_node; |
10100cc7 | 11209 | TREE_SIDE_EFFECTS (node) = 1; |
15fdcfe9 PB |
11210 | CAN_COMPLETE_NORMALLY (node) = 1; |
11211 | break; | |
11212 | ||
b67d701b | 11213 | case SWITCH_EXPR: |
e04a16fb AG |
11214 | case LOOP_EXPR: |
11215 | PUSH_LOOP (node); | |
11216 | /* Check whether the loop was enclosed in a labeled | |
11217 | statement. If not, create one, insert the loop in it and | |
11218 | return the node */ | |
11219 | nn = patch_loop_statement (node); | |
b67d701b | 11220 | |
e04a16fb | 11221 | /* Anyways, walk the body of the loop */ |
b67d701b PB |
11222 | if (TREE_CODE (node) == LOOP_EXPR) |
11223 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); | |
11224 | /* Switch statement: walk the switch expression and the cases */ | |
11225 | else | |
11226 | node = patch_switch_statement (node); | |
11227 | ||
e7c7bcef | 11228 | if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node) |
b635eb2f PB |
11229 | nn = error_mark_node; |
11230 | else | |
15fdcfe9 | 11231 | { |
b635eb2f PB |
11232 | TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node; |
11233 | /* If we returned something different, that's because we | |
11234 | inserted a label. Pop the label too. */ | |
11235 | if (nn != node) | |
11236 | { | |
11237 | if (CAN_COMPLETE_NORMALLY (node)) | |
11238 | CAN_COMPLETE_NORMALLY (nn) = 1; | |
11239 | POP_LABELED_BLOCK (); | |
11240 | } | |
15fdcfe9 | 11241 | } |
e04a16fb AG |
11242 | POP_LOOP (); |
11243 | return nn; | |
11244 | ||
11245 | case EXIT_EXPR: | |
11246 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); | |
11247 | return patch_exit_expr (node); | |
11248 | ||
11249 | case COND_EXPR: | |
11250 | /* Condition */ | |
11251 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); | |
11252 | if (TREE_OPERAND (node, 0) == error_mark_node) | |
11253 | return error_mark_node; | |
11254 | /* then-else branches */ | |
11255 | TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1)); | |
11256 | if (TREE_OPERAND (node, 1) == error_mark_node) | |
11257 | return error_mark_node; | |
11258 | TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2)); | |
11259 | if (TREE_OPERAND (node, 2) == error_mark_node) | |
11260 | return error_mark_node; | |
11261 | return patch_if_else_statement (node); | |
11262 | break; | |
11263 | ||
22eed1e6 APB |
11264 | case CONDITIONAL_EXPR: |
11265 | /* Condition */ | |
11266 | wfl_op1 = TREE_OPERAND (node, 0); | |
11267 | COMPLETE_CHECK_OP_0 (node); | |
11268 | wfl_op2 = TREE_OPERAND (node, 1); | |
11269 | COMPLETE_CHECK_OP_1 (node); | |
11270 | wfl_op3 = TREE_OPERAND (node, 2); | |
11271 | COMPLETE_CHECK_OP_2 (node); | |
11272 | return patch_conditional_expr (node, wfl_op1, wfl_op2); | |
11273 | ||
e04a16fb AG |
11274 | /* 3- Expression section */ |
11275 | case COMPOUND_EXPR: | |
15fdcfe9 | 11276 | wfl_op2 = TREE_OPERAND (node, 1); |
ac825856 APB |
11277 | TREE_OPERAND (node, 0) = nn = |
11278 | java_complete_tree (TREE_OPERAND (node, 0)); | |
dc0b3eff PB |
11279 | if (wfl_op2 == empty_stmt_node) |
11280 | CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn); | |
11281 | else | |
15fdcfe9 | 11282 | { |
dc0b3eff | 11283 | if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK) |
bccaf73a | 11284 | { |
dc0b3eff PB |
11285 | /* An unreachable condition in a do-while statement |
11286 | is *not* (technically) an unreachable statement. */ | |
11287 | nn = wfl_op2; | |
11288 | if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION) | |
11289 | nn = EXPR_WFL_NODE (nn); | |
11290 | if (TREE_CODE (nn) != EXIT_EXPR) | |
11291 | { | |
11292 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op2); | |
11293 | parse_error_context (wfl_operator, "Unreachable statement"); | |
11294 | } | |
bccaf73a | 11295 | } |
dc0b3eff PB |
11296 | TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1)); |
11297 | if (TREE_OPERAND (node, 1) == error_mark_node) | |
11298 | return error_mark_node; | |
11299 | CAN_COMPLETE_NORMALLY (node) | |
11300 | = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)); | |
15fdcfe9 | 11301 | } |
e04a16fb AG |
11302 | TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1)); |
11303 | break; | |
11304 | ||
11305 | case RETURN_EXPR: | |
15fdcfe9 | 11306 | /* CAN_COMPLETE_NORMALLY (node) = 0; */ |
e04a16fb AG |
11307 | return patch_return (node); |
11308 | ||
11309 | case EXPR_WITH_FILE_LOCATION: | |
11310 | if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */ | |
11311 | || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE) | |
15fdcfe9 | 11312 | { |
5423609c | 11313 | tree wfl = node; |
15fdcfe9 | 11314 | node = resolve_expression_name (node, NULL); |
dc0b3eff PB |
11315 | if (node == error_mark_node) |
11316 | return node; | |
5423609c APB |
11317 | /* Keep line number information somewhere were it doesn't |
11318 | disrupt the completion process. */ | |
2c56429a | 11319 | if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR) |
5423609c APB |
11320 | { |
11321 | EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1); | |
11322 | TREE_OPERAND (node, 1) = wfl; | |
11323 | } | |
15fdcfe9 PB |
11324 | CAN_COMPLETE_NORMALLY (node) = 1; |
11325 | } | |
e04a16fb AG |
11326 | else |
11327 | { | |
5b09b33e PB |
11328 | tree body; |
11329 | int save_lineno = lineno; | |
11330 | lineno = EXPR_WFL_LINENO (node); | |
11331 | body = java_complete_tree (EXPR_WFL_NODE (node)); | |
11332 | lineno = save_lineno; | |
15fdcfe9 | 11333 | EXPR_WFL_NODE (node) = body; |
dc0b3eff | 11334 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body); |
15fdcfe9 | 11335 | CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body); |
cd9643f7 PB |
11336 | if (body == empty_stmt_node) |
11337 | { | |
11338 | /* Optimization; makes it easier to detect empty bodies. */ | |
11339 | return body; | |
11340 | } | |
dc0b3eff | 11341 | if (body == error_mark_node) |
e04a16fb AG |
11342 | { |
11343 | /* Its important for the evaluation of assignment that | |
11344 | this mark on the TREE_TYPE is propagated. */ | |
11345 | TREE_TYPE (node) = error_mark_node; | |
11346 | return error_mark_node; | |
11347 | } | |
11348 | else | |
11349 | TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node)); | |
15fdcfe9 | 11350 | |
e04a16fb AG |
11351 | } |
11352 | break; | |
11353 | ||
b67d701b | 11354 | case NEW_ARRAY_EXPR: |
e04a16fb AG |
11355 | /* Patch all the dimensions */ |
11356 | flag = 0; | |
11357 | for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn)) | |
11358 | { | |
11359 | int location = EXPR_WFL_LINECOL (TREE_VALUE (cn)); | |
3a1760ac APB |
11360 | tree dim = convert (int_type_node, |
11361 | java_complete_tree (TREE_VALUE (cn))); | |
e04a16fb AG |
11362 | if (dim == error_mark_node) |
11363 | { | |
11364 | flag = 1; | |
11365 | continue; | |
11366 | } | |
11367 | else | |
11368 | { | |
b9f7e36c | 11369 | TREE_VALUE (cn) = dim; |
e04a16fb AG |
11370 | /* Setup the location of the current dimension, for |
11371 | later error report. */ | |
11372 | TREE_PURPOSE (cn) = | |
11373 | build_expr_wfl (NULL_TREE, input_filename, 0, 0); | |
11374 | EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location; | |
11375 | } | |
11376 | } | |
11377 | /* They complete the array creation expression, if no errors | |
11378 | were found. */ | |
15fdcfe9 | 11379 | CAN_COMPLETE_NORMALLY (node) = 1; |
aee48ef8 PB |
11380 | return (flag ? error_mark_node |
11381 | : force_evaluation_order (patch_newarray (node))); | |
e04a16fb | 11382 | |
c2952b01 APB |
11383 | case NEW_ANONYMOUS_ARRAY_EXPR: |
11384 | /* Create the array type if necessary. */ | |
11385 | if (ANONYMOUS_ARRAY_DIMS_SIG (node)) | |
11386 | { | |
11387 | tree type = ANONYMOUS_ARRAY_BASE_TYPE (node); | |
11388 | if (!(type = resolve_type_during_patch (type))) | |
11389 | return error_mark_node; | |
11390 | type = build_array_from_name (type, NULL_TREE, | |
11391 | ANONYMOUS_ARRAY_DIMS_SIG (node), NULL); | |
11392 | ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type); | |
11393 | } | |
11394 | node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node), | |
11395 | ANONYMOUS_ARRAY_INITIALIZER (node)); | |
11396 | if (node == error_mark_node) | |
11397 | return error_mark_node; | |
11398 | CAN_COMPLETE_NORMALLY (node) = 1; | |
11399 | return node; | |
11400 | ||
b67d701b | 11401 | case NEW_CLASS_EXPR: |
e04a16fb | 11402 | case CALL_EXPR: |
b67d701b | 11403 | /* Complete function's argument(s) first */ |
e04a16fb AG |
11404 | if (complete_function_arguments (node)) |
11405 | return error_mark_node; | |
11406 | else | |
b9f7e36c | 11407 | { |
22eed1e6 APB |
11408 | tree decl, wfl = TREE_OPERAND (node, 0); |
11409 | int in_this = CALL_THIS_CONSTRUCTOR_P (node); | |
11410 | ||
c877974e | 11411 | node = patch_method_invocation (node, NULL_TREE, |
89e09b9a | 11412 | NULL_TREE, 0, &decl); |
c877974e APB |
11413 | if (node == error_mark_node) |
11414 | return error_mark_node; | |
11415 | ||
11416 | check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl); | |
11417 | /* If we call this(...), register signature and positions */ | |
11418 | if (in_this) | |
11419 | DECL_CONSTRUCTOR_CALLS (current_function_decl) = | |
11420 | tree_cons (wfl, decl, | |
11421 | DECL_CONSTRUCTOR_CALLS (current_function_decl)); | |
de4c7b02 | 11422 | CAN_COMPLETE_NORMALLY (node) = 1; |
dc0b3eff | 11423 | return force_evaluation_order (node); |
b9f7e36c | 11424 | } |
e04a16fb AG |
11425 | |
11426 | case MODIFY_EXPR: | |
11427 | /* Save potential wfls */ | |
11428 | wfl_op1 = TREE_OPERAND (node, 0); | |
cd9643f7 | 11429 | TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1); |
c2952b01 | 11430 | |
cd9643f7 PB |
11431 | if (MODIFY_EXPR_FROM_INITIALIZATION_P (node) |
11432 | && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn) | |
11433 | && DECL_INITIAL (nn) != NULL_TREE) | |
11434 | { | |
100f7cd8 APB |
11435 | tree value; |
11436 | ||
100f7cd8 | 11437 | value = fold_constant_for_init (nn, nn); |
c2952b01 | 11438 | |
cd9643f7 PB |
11439 | if (value != NULL_TREE) |
11440 | { | |
11441 | tree type = TREE_TYPE (value); | |
c2952b01 APB |
11442 | if (JPRIMITIVE_TYPE_P (type) || |
11443 | (type == string_ptr_type_node && ! flag_emit_class_files)) | |
cd9643f7 PB |
11444 | return empty_stmt_node; |
11445 | } | |
629d4b4d APB |
11446 | if (! flag_emit_class_files) |
11447 | DECL_INITIAL (nn) = NULL_TREE; | |
cd9643f7 | 11448 | } |
e04a16fb | 11449 | wfl_op2 = TREE_OPERAND (node, 1); |
cd9643f7 | 11450 | |
e04a16fb AG |
11451 | if (TREE_OPERAND (node, 0) == error_mark_node) |
11452 | return error_mark_node; | |
11453 | ||
5cbdba64 APB |
11454 | flag = COMPOUND_ASSIGN_P (wfl_op2); |
11455 | if (flag) | |
e04a16fb | 11456 | { |
c2952b01 APB |
11457 | /* This might break when accessing outer field from inner |
11458 | class. TESTME, FIXME */ | |
2aa11e97 | 11459 | tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0)); |
e04a16fb AG |
11460 | |
11461 | /* Hand stablize the lhs on both places */ | |
e04a16fb | 11462 | TREE_OPERAND (node, 0) = lvalue; |
5cbdba64 APB |
11463 | TREE_OPERAND (TREE_OPERAND (node, 1), 0) = |
11464 | (flag_emit_class_files ? lvalue : save_expr (lvalue)); | |
2aa11e97 | 11465 | |
5cbdba64 | 11466 | /* 15.25.2.a: Left hand is not an array access. FIXME */ |
2aa11e97 APB |
11467 | /* Now complete the RHS. We write it back later on. */ |
11468 | nn = java_complete_tree (TREE_OPERAND (node, 1)); | |
11469 | ||
642f15d1 APB |
11470 | if ((cn = patch_string (nn))) |
11471 | nn = cn; | |
11472 | ||
2aa11e97 APB |
11473 | /* The last part of the rewrite for E1 op= E2 is to have |
11474 | E1 = (T)(E1 op E2), with T being the type of E1. */ | |
642f15d1 APB |
11475 | nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2), |
11476 | TREE_TYPE (lvalue), nn)); | |
5cbdba64 APB |
11477 | |
11478 | /* 15.25.2.b: Left hand is an array access. FIXME */ | |
e04a16fb AG |
11479 | } |
11480 | ||
f8976021 | 11481 | /* If we're about to patch a NEW_ARRAY_INIT, we call a special |
c2952b01 APB |
11482 | function to complete this RHS. Note that a NEW_ARRAY_INIT |
11483 | might have been already fully expanded if created as a result | |
11484 | of processing an anonymous array initializer. We avoid doing | |
11485 | the operation twice by testing whether the node already bears | |
11486 | a type. */ | |
11487 | else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2)) | |
fdec99c6 | 11488 | nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)), |
f8976021 | 11489 | TREE_OPERAND (node, 1)); |
2aa11e97 | 11490 | /* Otherwise we simply complete the RHS */ |
f8976021 APB |
11491 | else |
11492 | nn = java_complete_tree (TREE_OPERAND (node, 1)); | |
11493 | ||
e04a16fb | 11494 | if (nn == error_mark_node) |
c0d87ff6 | 11495 | return error_mark_node; |
2aa11e97 APB |
11496 | |
11497 | /* Write back the RHS as we evaluated it. */ | |
e04a16fb | 11498 | TREE_OPERAND (node, 1) = nn; |
b67d701b PB |
11499 | |
11500 | /* In case we're handling = with a String as a RHS, we need to | |
11501 | produce a String out of the RHS (it might still be a | |
11502 | STRING_CST or a StringBuffer at this stage */ | |
11503 | if ((nn = patch_string (TREE_OPERAND (node, 1)))) | |
11504 | TREE_OPERAND (node, 1) = nn; | |
c2952b01 APB |
11505 | |
11506 | if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0), | |
11507 | TREE_OPERAND (node, 1)))) | |
11508 | { | |
11509 | /* We return error_mark_node if outer_field_access_fix | |
11510 | detects we write into a final. */ | |
11511 | if (nn == error_mark_node) | |
11512 | return error_mark_node; | |
11513 | node = nn; | |
11514 | } | |
11515 | else | |
11516 | { | |
11517 | node = patch_assignment (node, wfl_op1, wfl_op2); | |
11518 | /* Reorganize the tree if necessary. */ | |
11519 | if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node)) | |
11520 | || JSTRING_P (TREE_TYPE (node)))) | |
11521 | node = java_refold (node); | |
11522 | } | |
11523 | ||
15fdcfe9 PB |
11524 | CAN_COMPLETE_NORMALLY (node) = 1; |
11525 | return node; | |
e04a16fb AG |
11526 | |
11527 | case MULT_EXPR: | |
11528 | case PLUS_EXPR: | |
11529 | case MINUS_EXPR: | |
11530 | case LSHIFT_EXPR: | |
11531 | case RSHIFT_EXPR: | |
11532 | case URSHIFT_EXPR: | |
11533 | case BIT_AND_EXPR: | |
11534 | case BIT_XOR_EXPR: | |
11535 | case BIT_IOR_EXPR: | |
11536 | case TRUNC_MOD_EXPR: | |
c2952b01 | 11537 | case TRUNC_DIV_EXPR: |
e04a16fb AG |
11538 | case RDIV_EXPR: |
11539 | case TRUTH_ANDIF_EXPR: | |
11540 | case TRUTH_ORIF_EXPR: | |
11541 | case EQ_EXPR: | |
11542 | case NE_EXPR: | |
11543 | case GT_EXPR: | |
11544 | case GE_EXPR: | |
11545 | case LT_EXPR: | |
11546 | case LE_EXPR: | |
11547 | /* Operands 0 and 1 are WFL in certain cases only. patch_binop | |
11548 | knows how to handle those cases. */ | |
11549 | wfl_op1 = TREE_OPERAND (node, 0); | |
11550 | wfl_op2 = TREE_OPERAND (node, 1); | |
b67d701b | 11551 | |
15fdcfe9 | 11552 | CAN_COMPLETE_NORMALLY (node) = 1; |
b67d701b PB |
11553 | /* Don't complete string nodes if dealing with the PLUS operand. */ |
11554 | if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1)) | |
2aa11e97 APB |
11555 | { |
11556 | nn = java_complete_tree (wfl_op1); | |
11557 | if (nn == error_mark_node) | |
11558 | return error_mark_node; | |
48a840d9 | 11559 | |
2aa11e97 APB |
11560 | TREE_OPERAND (node, 0) = nn; |
11561 | } | |
b67d701b | 11562 | if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2)) |
2aa11e97 APB |
11563 | { |
11564 | nn = java_complete_tree (wfl_op2); | |
11565 | if (nn == error_mark_node) | |
11566 | return error_mark_node; | |
48a840d9 | 11567 | |
2aa11e97 APB |
11568 | TREE_OPERAND (node, 1) = nn; |
11569 | } | |
dc0b3eff | 11570 | return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2)); |
e04a16fb | 11571 | |
5e942c50 APB |
11572 | case INSTANCEOF_EXPR: |
11573 | wfl_op1 = TREE_OPERAND (node, 0); | |
11574 | COMPLETE_CHECK_OP_0 (node); | |
ce6e9147 APB |
11575 | if (flag_emit_xref) |
11576 | { | |
11577 | TREE_TYPE (node) = boolean_type_node; | |
11578 | return node; | |
11579 | } | |
5e942c50 APB |
11580 | return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1)); |
11581 | ||
b67d701b | 11582 | case UNARY_PLUS_EXPR: |
e04a16fb AG |
11583 | case NEGATE_EXPR: |
11584 | case TRUTH_NOT_EXPR: | |
11585 | case BIT_NOT_EXPR: | |
11586 | case PREDECREMENT_EXPR: | |
11587 | case PREINCREMENT_EXPR: | |
11588 | case POSTDECREMENT_EXPR: | |
11589 | case POSTINCREMENT_EXPR: | |
11590 | case CONVERT_EXPR: | |
11591 | /* There are cases were wfl_op1 is a WFL. patch_unaryop knows | |
11592 | how to handle those cases. */ | |
11593 | wfl_op1 = TREE_OPERAND (node, 0); | |
15fdcfe9 | 11594 | CAN_COMPLETE_NORMALLY (node) = 1; |
e04a16fb AG |
11595 | TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1); |
11596 | if (TREE_OPERAND (node, 0) == error_mark_node) | |
11597 | return error_mark_node; | |
4a5f66c3 APB |
11598 | node = patch_unaryop (node, wfl_op1); |
11599 | CAN_COMPLETE_NORMALLY (node) = 1; | |
11600 | break; | |
e04a16fb AG |
11601 | |
11602 | case ARRAY_REF: | |
11603 | /* There are cases were wfl_op1 is a WFL. patch_array_ref knows | |
11604 | how to handle those cases. */ | |
11605 | wfl_op1 = TREE_OPERAND (node, 0); | |
11606 | TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1); | |
11607 | if (TREE_OPERAND (node, 0) == error_mark_node) | |
11608 | return error_mark_node; | |
7f1d4866 | 11609 | if (!flag_emit_class_files && !flag_emit_xref) |
b67d701b | 11610 | TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0)); |
e04a16fb AG |
11611 | /* The same applies to wfl_op2 */ |
11612 | wfl_op2 = TREE_OPERAND (node, 1); | |
11613 | TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2); | |
11614 | if (TREE_OPERAND (node, 1) == error_mark_node) | |
11615 | return error_mark_node; | |
7f1d4866 | 11616 | if (!flag_emit_class_files && !flag_emit_xref) |
22eed1e6 | 11617 | TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1)); |
939d7216 | 11618 | return patch_array_ref (node); |
e04a16fb | 11619 | |
63a212ed PB |
11620 | case RECORD_TYPE: |
11621 | return node;; | |
11622 | ||
11623 | case COMPONENT_REF: | |
11624 | /* The first step in the re-write of qualified name handling. FIXME. | |
11625 | So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */ | |
9bbc7d9f | 11626 | TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0)); |
63a212ed PB |
11627 | if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE) |
11628 | { | |
11629 | tree name = TREE_OPERAND (node, 1); | |
11630 | tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name); | |
11631 | if (field == NULL_TREE) | |
11632 | { | |
11633 | error ("missing static field `%s'", IDENTIFIER_POINTER (name)); | |
11634 | return error_mark_node; | |
11635 | } | |
11636 | if (! FIELD_STATIC (field)) | |
11637 | { | |
11638 | error ("not a static field `%s'", IDENTIFIER_POINTER (name)); | |
11639 | return error_mark_node; | |
11640 | } | |
11641 | return field; | |
11642 | } | |
11643 | else | |
11644 | fatal ("unimplemented java_complete_tree for COMPONENT_REF"); | |
9bbc7d9f | 11645 | break; |
9bbc7d9f | 11646 | |
b67d701b | 11647 | case THIS_EXPR: |
e04a16fb AG |
11648 | /* Can't use THIS in a static environment */ |
11649 | if (!current_this) | |
11650 | { | |
11651 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
781b0558 KG |
11652 | parse_error_context (wfl_operator, |
11653 | "Keyword `this' used outside allowed context"); | |
e04a16fb AG |
11654 | TREE_TYPE (node) = error_mark_node; |
11655 | return error_mark_node; | |
11656 | } | |
22eed1e6 APB |
11657 | if (ctxp->explicit_constructor_p) |
11658 | { | |
11659 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
11660 | parse_error_context | |
781b0558 | 11661 | (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called"); |
22eed1e6 APB |
11662 | TREE_TYPE (node) = error_mark_node; |
11663 | return error_mark_node; | |
11664 | } | |
e04a16fb | 11665 | return current_this; |
c2952b01 APB |
11666 | |
11667 | case CLASS_LITERAL: | |
11668 | CAN_COMPLETE_NORMALLY (node) = 1; | |
11669 | node = patch_incomplete_class_ref (node); | |
11670 | if (node == error_mark_node) | |
11671 | return error_mark_node; | |
11672 | break; | |
11673 | ||
11674 | case INSTANCE_INITIALIZERS_EXPR: | |
11675 | in_instance_initializer++; | |
11676 | node = java_complete_tree (TREE_OPERAND (node, 0)); | |
11677 | in_instance_initializer--; | |
11678 | if (node != error_mark_node) | |
11679 | TREE_TYPE (node) = void_type_node; | |
11680 | else | |
11681 | return error_mark_node; | |
11682 | break; | |
e04a16fb | 11683 | |
e04a16fb | 11684 | default: |
15fdcfe9 | 11685 | CAN_COMPLETE_NORMALLY (node) = 1; |
b67d701b | 11686 | /* Ok: may be we have a STRING_CST or a crafted `StringBuffer' |
c2952b01 APB |
11687 | and it's time to turn it into the appropriate String object */ |
11688 | if ((nn = patch_string (node))) | |
11689 | node = nn; | |
11690 | else | |
11691 | fatal ("No case for tree code `%s' - java_complete_tree\n", | |
11692 | tree_code_name [TREE_CODE (node)]); | |
e04a16fb AG |
11693 | } |
11694 | return node; | |
11695 | } | |
11696 | ||
11697 | /* Complete function call's argument. Return a non zero value is an | |
11698 | error was found. */ | |
11699 | ||
11700 | static int | |
11701 | complete_function_arguments (node) | |
11702 | tree node; | |
11703 | { | |
11704 | int flag = 0; | |
11705 | tree cn; | |
11706 | ||
f63991a8 | 11707 | ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0); |
e04a16fb AG |
11708 | for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn)) |
11709 | { | |
b67d701b | 11710 | tree wfl = TREE_VALUE (cn), parm, temp; |
e04a16fb | 11711 | parm = java_complete_tree (wfl); |
c2952b01 | 11712 | |
e04a16fb AG |
11713 | if (parm == error_mark_node) |
11714 | { | |
11715 | flag = 1; | |
11716 | continue; | |
11717 | } | |
b67d701b PB |
11718 | /* If have a string literal that we haven't transformed yet or a |
11719 | crafted string buffer, as a result of use of the the String | |
11720 | `+' operator. Build `parm.toString()' and expand it. */ | |
11721 | if ((temp = patch_string (parm))) | |
b9f7e36c | 11722 | parm = temp; |
5e942c50 APB |
11723 | /* Inline PRIMTYPE.TYPE read access */ |
11724 | parm = maybe_build_primttype_type_ref (parm, wfl); | |
b9f7e36c | 11725 | |
5e942c50 | 11726 | TREE_VALUE (cn) = parm; |
e04a16fb | 11727 | } |
f63991a8 | 11728 | ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0); |
e04a16fb AG |
11729 | return flag; |
11730 | } | |
11731 | ||
11732 | /* Sometimes (for loops and variable initialized during their | |
11733 | declaration), we want to wrap a statement around a WFL and turn it | |
11734 | debugable. */ | |
11735 | ||
11736 | static tree | |
11737 | build_debugable_stmt (location, stmt) | |
11738 | int location; | |
11739 | tree stmt; | |
11740 | { | |
11741 | if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION) | |
11742 | { | |
11743 | stmt = build_expr_wfl (stmt, input_filename, 0, 0); | |
11744 | EXPR_WFL_LINECOL (stmt) = location; | |
11745 | } | |
11746 | JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt); | |
11747 | return stmt; | |
11748 | } | |
11749 | ||
11750 | static tree | |
11751 | build_expr_block (body, decls) | |
11752 | tree body, decls; | |
11753 | { | |
11754 | tree node = make_node (BLOCK); | |
11755 | BLOCK_EXPR_DECLS (node) = decls; | |
b67d701b | 11756 | BLOCK_EXPR_BODY (node) = body; |
e04a16fb AG |
11757 | if (body) |
11758 | TREE_TYPE (node) = TREE_TYPE (body); | |
11759 | TREE_SIDE_EFFECTS (node) = 1; | |
11760 | return node; | |
11761 | } | |
11762 | ||
b67d701b PB |
11763 | /* Create a new function block and link it approriately to current |
11764 | function block chain */ | |
e04a16fb AG |
11765 | |
11766 | static tree | |
11767 | enter_block () | |
11768 | { | |
b67d701b PB |
11769 | return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE))); |
11770 | } | |
11771 | ||
11772 | /* Link block B supercontext to the previous block. The current | |
11773 | function DECL is used as supercontext when enter_a_block is called | |
11774 | for the first time for a given function. The current function body | |
11775 | (DECL_FUNCTION_BODY) is set to be block B. */ | |
11776 | ||
11777 | static tree | |
11778 | enter_a_block (b) | |
11779 | tree b; | |
11780 | { | |
e04a16fb AG |
11781 | tree fndecl = current_function_decl; |
11782 | ||
f099f336 APB |
11783 | if (!fndecl) { |
11784 | BLOCK_SUPERCONTEXT (b) = current_static_block; | |
11785 | current_static_block = b; | |
11786 | } | |
11787 | ||
11788 | else if (!DECL_FUNCTION_BODY (fndecl)) | |
e04a16fb AG |
11789 | { |
11790 | BLOCK_SUPERCONTEXT (b) = fndecl; | |
11791 | DECL_FUNCTION_BODY (fndecl) = b; | |
11792 | } | |
11793 | else | |
11794 | { | |
11795 | BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl); | |
11796 | DECL_FUNCTION_BODY (fndecl) = b; | |
11797 | } | |
11798 | return b; | |
11799 | } | |
11800 | ||
11801 | /* Exit a block by changing the current function body | |
11802 | (DECL_FUNCTION_BODY) to the current block super context, only if | |
11803 | the block being exited isn't the method's top level one. */ | |
11804 | ||
11805 | static tree | |
11806 | exit_block () | |
11807 | { | |
f099f336 APB |
11808 | tree b; |
11809 | if (current_function_decl) | |
11810 | { | |
11811 | b = DECL_FUNCTION_BODY (current_function_decl); | |
11812 | if (BLOCK_SUPERCONTEXT (b) != current_function_decl) | |
11813 | DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b); | |
11814 | } | |
11815 | else | |
11816 | { | |
11817 | b = current_static_block; | |
e04a16fb | 11818 | |
f099f336 APB |
11819 | if (BLOCK_SUPERCONTEXT (b)) |
11820 | current_static_block = BLOCK_SUPERCONTEXT (b); | |
11821 | } | |
e04a16fb AG |
11822 | return b; |
11823 | } | |
11824 | ||
11825 | /* Lookup for NAME in the nested function's blocks, all the way up to | |
11826 | the current toplevel one. It complies with Java's local variable | |
11827 | scoping rules. */ | |
11828 | ||
11829 | static tree | |
11830 | lookup_name_in_blocks (name) | |
11831 | tree name; | |
11832 | { | |
f099f336 | 11833 | tree b = GET_CURRENT_BLOCK (current_function_decl); |
e04a16fb AG |
11834 | |
11835 | while (b != current_function_decl) | |
11836 | { | |
11837 | tree current; | |
11838 | ||
11839 | /* Paranoid sanity check. To be removed */ | |
11840 | if (TREE_CODE (b) != BLOCK) | |
11841 | fatal ("non block expr function body - lookup_name_in_blocks"); | |
11842 | ||
11843 | for (current = BLOCK_EXPR_DECLS (b); current; | |
11844 | current = TREE_CHAIN (current)) | |
11845 | if (DECL_NAME (current) == name) | |
11846 | return current; | |
11847 | b = BLOCK_SUPERCONTEXT (b); | |
11848 | } | |
11849 | return NULL_TREE; | |
11850 | } | |
11851 | ||
11852 | static void | |
11853 | maybe_absorb_scoping_blocks () | |
11854 | { | |
f099f336 | 11855 | while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl))) |
e04a16fb AG |
11856 | { |
11857 | tree b = exit_block (); | |
11858 | java_method_add_stmt (current_function_decl, b); | |
11859 | SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno)); | |
11860 | } | |
11861 | } | |
11862 | ||
11863 | \f | |
11864 | /* This section of the source is reserved to build_* functions that | |
11865 | are building incomplete tree nodes and the patch_* functions that | |
11866 | are completing them. */ | |
11867 | ||
c2952b01 APB |
11868 | /* Wrap a non WFL node around a WFL. */ |
11869 | static tree | |
9a7ab4b3 | 11870 | build_wfl_wrap (node, location) |
c2952b01 | 11871 | tree node; |
9a7ab4b3 | 11872 | int location; |
c2952b01 APB |
11873 | { |
11874 | tree wfl, node_to_insert = node; | |
11875 | ||
11876 | /* We want to process THIS . xxx symbolicaly, to keep it consistent | |
11877 | with the way we're processing SUPER. A THIS from a primary as a | |
11878 | different form than a SUPER. Turn THIS into something symbolic */ | |
11879 | if (TREE_CODE (node) == THIS_EXPR) | |
11880 | node_to_insert = wfl = build_wfl_node (this_identifier_node); | |
11881 | else | |
11882 | wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0); | |
11883 | ||
9a7ab4b3 | 11884 | EXPR_WFL_LINECOL (wfl) = location; |
c2952b01 APB |
11885 | EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE); |
11886 | return wfl; | |
11887 | } | |
11888 | ||
11889 | ||
9bbc7d9f | 11890 | /* Build a super() constructor invocation. Returns empty_stmt_node if |
22eed1e6 APB |
11891 | we're currently dealing with the class java.lang.Object. */ |
11892 | ||
11893 | static tree | |
e920ebc9 APB |
11894 | build_super_invocation (mdecl) |
11895 | tree mdecl; | |
22eed1e6 | 11896 | { |
e920ebc9 | 11897 | if (DECL_CONTEXT (mdecl) == object_type_node) |
9bbc7d9f | 11898 | return empty_stmt_node; |
22eed1e6 APB |
11899 | else |
11900 | { | |
9ee9b555 | 11901 | tree super_wfl = build_wfl_node (super_identifier_node); |
c2952b01 APB |
11902 | tree a = NULL_TREE, t; |
11903 | /* If we're dealing with an anonymous class, pass the arguments | |
11904 | of the crafted constructor along. */ | |
11905 | if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl))) | |
11906 | { | |
11907 | SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl); | |
11908 | for (; t != end_params_node; t = TREE_CHAIN (t)) | |
11909 | a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a); | |
11910 | } | |
11911 | return build_method_invocation (super_wfl, a); | |
22eed1e6 APB |
11912 | } |
11913 | } | |
11914 | ||
11915 | /* Build a SUPER/THIS qualified method invocation. */ | |
11916 | ||
11917 | static tree | |
11918 | build_this_super_qualified_invocation (use_this, name, args, lloc, rloc) | |
11919 | int use_this; | |
11920 | tree name, args; | |
11921 | int lloc, rloc; | |
22eed1e6 APB |
11922 | { |
11923 | tree invok; | |
11924 | tree wfl = | |
9ee9b555 | 11925 | build_wfl_node (use_this ? this_identifier_node : super_identifier_node); |
22eed1e6 APB |
11926 | EXPR_WFL_LINECOL (wfl) = lloc; |
11927 | invok = build_method_invocation (name, args); | |
11928 | return make_qualified_primary (wfl, invok, rloc); | |
11929 | } | |
11930 | ||
b67d701b | 11931 | /* Build an incomplete CALL_EXPR node. */ |
e04a16fb AG |
11932 | |
11933 | static tree | |
11934 | build_method_invocation (name, args) | |
11935 | tree name; | |
11936 | tree args; | |
11937 | { | |
11938 | tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE); | |
11939 | TREE_SIDE_EFFECTS (call) = 1; | |
b67d701b PB |
11940 | EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name); |
11941 | return call; | |
11942 | } | |
11943 | ||
11944 | /* Build an incomplete new xxx(...) node. */ | |
11945 | ||
11946 | static tree | |
11947 | build_new_invocation (name, args) | |
11948 | tree name, args; | |
11949 | { | |
11950 | tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE); | |
11951 | TREE_SIDE_EFFECTS (call) = 1; | |
e04a16fb AG |
11952 | EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name); |
11953 | return call; | |
11954 | } | |
11955 | ||
11956 | /* Build an incomplete assignment expression. */ | |
11957 | ||
11958 | static tree | |
11959 | build_assignment (op, op_location, lhs, rhs) | |
11960 | int op, op_location; | |
11961 | tree lhs, rhs; | |
11962 | { | |
11963 | tree assignment; | |
11964 | /* Build the corresponding binop if we deal with a Compound | |
11965 | Assignment operator. Mark the binop sub-tree as part of a | |
11966 | Compound Assignment expression */ | |
11967 | if (op != ASSIGN_TK) | |
11968 | { | |
11969 | rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs); | |
11970 | COMPOUND_ASSIGN_P (rhs) = 1; | |
11971 | } | |
11972 | assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs); | |
11973 | TREE_SIDE_EFFECTS (assignment) = 1; | |
11974 | EXPR_WFL_LINECOL (assignment) = op_location; | |
11975 | return assignment; | |
11976 | } | |
11977 | ||
11978 | /* Print an INTEGER_CST node in a static buffer, and return the buffer. */ | |
11979 | ||
15fdcfe9 | 11980 | char * |
e04a16fb AG |
11981 | print_int_node (node) |
11982 | tree node; | |
11983 | { | |
11984 | static char buffer [80]; | |
11985 | if (TREE_CONSTANT_OVERFLOW (node)) | |
11986 | sprintf (buffer, "<overflow>"); | |
11987 | ||
11988 | if (TREE_INT_CST_HIGH (node) == 0) | |
11989 | sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED, | |
11990 | TREE_INT_CST_LOW (node)); | |
11991 | else if (TREE_INT_CST_HIGH (node) == -1 | |
11992 | && TREE_INT_CST_LOW (node) != 0) | |
11993 | { | |
11994 | buffer [0] = '-'; | |
11995 | sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED, | |
11996 | -TREE_INT_CST_LOW (node)); | |
11997 | } | |
11998 | else | |
11999 | sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX, | |
12000 | TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node)); | |
12001 | ||
12002 | return buffer; | |
12003 | } | |
12004 | ||
7f1d4866 APB |
12005 | /* Return 1 if an assignment to a FINAL is attempted in a non suitable |
12006 | context. */ | |
5e942c50 APB |
12007 | |
12008 | static int | |
12009 | check_final_assignment (lvalue, wfl) | |
12010 | tree lvalue, wfl; | |
12011 | { | |
6632dcdd APB |
12012 | if (TREE_CODE (lvalue) == COMPOUND_EXPR |
12013 | && JDECL_P (TREE_OPERAND (lvalue, 1))) | |
12014 | lvalue = TREE_OPERAND (lvalue, 1); | |
12015 | ||
bc2874c9 TT |
12016 | /* When generating class files, references to the `length' field |
12017 | look a bit different. */ | |
12018 | if ((flag_emit_class_files | |
12019 | && TREE_CODE (lvalue) == COMPONENT_REF | |
12020 | && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0))) | |
12021 | && FIELD_FINAL (TREE_OPERAND (lvalue, 1))) | |
12022 | || (TREE_CODE (lvalue) == FIELD_DECL | |
12023 | && FIELD_FINAL (lvalue) | |
12024 | && !DECL_CLINIT_P (current_function_decl) | |
12025 | && !DECL_FINIT_P (current_function_decl))) | |
5e942c50 APB |
12026 | { |
12027 | parse_error_context | |
12028 | (wfl, "Can't assign a value to the final variable `%s'", | |
12029 | IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl))); | |
12030 | return 1; | |
12031 | } | |
12032 | return 0; | |
12033 | } | |
12034 | ||
12035 | /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in | |
12036 | read. This is needed to avoid circularities in the implementation | |
12037 | of these fields in libjava. */ | |
12038 | ||
12039 | static tree | |
12040 | maybe_build_primttype_type_ref (rhs, wfl) | |
12041 | tree rhs, wfl; | |
12042 | { | |
12043 | tree to_return = NULL_TREE; | |
12044 | tree rhs_type = TREE_TYPE (rhs); | |
12045 | if (TREE_CODE (rhs) == COMPOUND_EXPR) | |
12046 | { | |
12047 | tree n = TREE_OPERAND (rhs, 1); | |
12048 | if (TREE_CODE (n) == VAR_DECL | |
12049 | && DECL_NAME (n) == TYPE_identifier_node | |
9a7ab4b3 APB |
12050 | && rhs_type == class_ptr_type |
12051 | && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE) | |
5e942c50 | 12052 | { |
49f48c71 | 12053 | const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)); |
5e942c50 APB |
12054 | if (!strncmp (self_name, "java.lang.", 10)) |
12055 | to_return = build_primtype_type_ref (self_name); | |
12056 | } | |
12057 | } | |
12058 | return (to_return ? to_return : rhs ); | |
12059 | } | |
12060 | ||
e04a16fb AG |
12061 | /* 15.25 Assignment operators. */ |
12062 | ||
12063 | static tree | |
12064 | patch_assignment (node, wfl_op1, wfl_op2) | |
12065 | tree node; | |
12066 | tree wfl_op1; | |
12067 | tree wfl_op2; | |
12068 | { | |
0a2138e2 | 12069 | tree rhs = TREE_OPERAND (node, 1); |
5e942c50 | 12070 | tree lvalue = TREE_OPERAND (node, 0), llvalue; |
cd531a2e | 12071 | tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE; |
e04a16fb AG |
12072 | int error_found = 0; |
12073 | int lvalue_from_array = 0; | |
12074 | ||
c2952b01 | 12075 | /* Can't assign to a (blank) final. */ |
5e942c50 APB |
12076 | if (check_final_assignment (lvalue, wfl_op1)) |
12077 | error_found = 1; | |
e04a16fb AG |
12078 | |
12079 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
12080 | ||
12081 | /* Lhs can be a named variable */ | |
34f4db93 | 12082 | if (JDECL_P (lvalue)) |
e04a16fb | 12083 | { |
e04a16fb AG |
12084 | lhs_type = TREE_TYPE (lvalue); |
12085 | } | |
12086 | /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME + | |
12087 | comment on reason why */ | |
12088 | else if (TREE_CODE (wfl_op1) == ARRAY_REF) | |
12089 | { | |
12090 | lhs_type = TREE_TYPE (lvalue); | |
12091 | lvalue_from_array = 1; | |
12092 | } | |
12093 | /* Or a field access */ | |
12094 | else if (TREE_CODE (lvalue) == COMPONENT_REF) | |
12095 | lhs_type = TREE_TYPE (lvalue); | |
12096 | /* Or a function return slot */ | |
12097 | else if (TREE_CODE (lvalue) == RESULT_DECL) | |
12098 | lhs_type = TREE_TYPE (lvalue); | |
5e942c50 APB |
12099 | /* Otherwise, we might want to try to write into an optimized static |
12100 | final, this is an of a different nature, reported further on. */ | |
12101 | else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION | |
1504b2b4 | 12102 | && resolve_expression_name (wfl_op1, &llvalue)) |
5e942c50 | 12103 | { |
6632dcdd | 12104 | if (!error_found && check_final_assignment (llvalue, wfl_op1)) |
1504b2b4 APB |
12105 | { |
12106 | /* What we should do instead is resetting the all the flags | |
12107 | previously set, exchange lvalue for llvalue and continue. */ | |
12108 | error_found = 1; | |
12109 | return error_mark_node; | |
12110 | } | |
12111 | else | |
12112 | lhs_type = TREE_TYPE (lvalue); | |
5e942c50 APB |
12113 | } |
12114 | else | |
e04a16fb AG |
12115 | { |
12116 | parse_error_context (wfl_op1, "Invalid left hand side of assignment"); | |
12117 | error_found = 1; | |
12118 | } | |
12119 | ||
12120 | rhs_type = TREE_TYPE (rhs); | |
b67d701b | 12121 | /* 5.1 Try the assignment conversion for builtin type. */ |
0a2138e2 | 12122 | new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs); |
e04a16fb | 12123 | |
b67d701b | 12124 | /* 5.2 If it failed, try a reference conversion */ |
0a2138e2 | 12125 | if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs))) |
b67d701b | 12126 | lhs_type = promote_type (rhs_type); |
e04a16fb AG |
12127 | |
12128 | /* 15.25.2 If we have a compound assignment, convert RHS into the | |
12129 | type of the LHS */ | |
12130 | else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1))) | |
12131 | new_rhs = convert (lhs_type, rhs); | |
12132 | ||
12133 | /* Explicit cast required. This is an error */ | |
12134 | if (!new_rhs) | |
12135 | { | |
c2e3db92 KG |
12136 | char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0)); |
12137 | char *t2 = xstrdup (lang_printable_name (lhs_type, 0)); | |
e04a16fb AG |
12138 | tree wfl; |
12139 | char operation [32]; /* Max size known */ | |
12140 | ||
12141 | /* If the assignment is part of a declaration, we use the WFL of | |
12142 | the declared variable to point out the error and call it a | |
12143 | declaration problem. If the assignment is a genuine = | |
12144 | operator, we call is a operator `=' problem, otherwise we | |
12145 | call it an assignment problem. In both of these last cases, | |
12146 | we use the WFL of the operator to indicate the error. */ | |
12147 | ||
12148 | if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)) | |
12149 | { | |
12150 | wfl = wfl_op1; | |
12151 | strcpy (operation, "declaration"); | |
12152 | } | |
12153 | else | |
12154 | { | |
12155 | wfl = wfl_operator; | |
12156 | if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1))) | |
12157 | strcpy (operation, "assignment"); | |
12158 | else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL) | |
12159 | strcpy (operation, "`return'"); | |
12160 | else | |
12161 | strcpy (operation, "`='"); | |
12162 | } | |
12163 | ||
1ebadc60 | 12164 | if (!valid_cast_to_p (rhs_type, lhs_type)) |
781b0558 KG |
12165 | parse_error_context |
12166 | (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'", | |
12167 | operation, t1, t2); | |
1ebadc60 | 12168 | else |
781b0558 | 12169 | parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'", |
1ebadc60 | 12170 | operation, t1, t2); |
e04a16fb AG |
12171 | free (t1); free (t2); |
12172 | error_found = 1; | |
12173 | } | |
12174 | ||
c877974e APB |
12175 | /* Inline read access to java.lang.PRIMTYPE.TYPE */ |
12176 | if (new_rhs) | |
12177 | new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2); | |
5e942c50 | 12178 | |
e04a16fb AG |
12179 | if (error_found) |
12180 | return error_mark_node; | |
12181 | ||
2622b947 APB |
12182 | /* 10.10: Array Store Exception runtime check */ |
12183 | if (!flag_emit_class_files | |
e8fc7396 | 12184 | && !flag_emit_xref |
2622b947 | 12185 | && lvalue_from_array |
afc390b1 | 12186 | && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type))) |
2622b947 APB |
12187 | { |
12188 | tree check; | |
12189 | tree base = lvalue; | |
12190 | ||
12191 | /* We need to retrieve the right argument for _Jv_CheckArrayStore */ | |
12192 | if (TREE_CODE (lvalue) == COMPOUND_EXPR) | |
12193 | base = TREE_OPERAND (lvalue, 0); | |
12194 | else | |
12195 | { | |
12196 | if (flag_bounds_check) | |
12197 | base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0); | |
12198 | else | |
12199 | base = TREE_OPERAND (TREE_OPERAND (base, 0), 0); | |
12200 | } | |
12201 | ||
12202 | /* Build the invocation of _Jv_CheckArrayStore */ | |
dc4e6ccf | 12203 | new_rhs = save_expr (new_rhs); |
2622b947 APB |
12204 | check = build (CALL_EXPR, void_type_node, |
12205 | build_address_of (soft_checkarraystore_node), | |
12206 | tree_cons (NULL_TREE, base, | |
12207 | build_tree_list (NULL_TREE, new_rhs)), | |
12208 | NULL_TREE); | |
12209 | TREE_SIDE_EFFECTS (check) = 1; | |
12210 | ||
12211 | /* We have to decide on an insertion point */ | |
12212 | if (TREE_CODE (lvalue) == COMPOUND_EXPR) | |
12213 | { | |
12214 | tree t; | |
12215 | if (flag_bounds_check) | |
12216 | { | |
12217 | t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0); | |
12218 | TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) = | |
12219 | build (COMPOUND_EXPR, void_type_node, t, check); | |
12220 | } | |
12221 | else | |
12222 | TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type, | |
12223 | check, TREE_OPERAND (lvalue, 1)); | |
12224 | } | |
12225 | else | |
12226 | { | |
12227 | /* Make sure the bound check will happen before the store check */ | |
12228 | if (flag_bounds_check) | |
12229 | TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) = | |
12230 | build (COMPOUND_EXPR, void_type_node, | |
12231 | TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check); | |
12232 | else | |
12233 | lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue); | |
12234 | } | |
12235 | } | |
22eed1e6 | 12236 | |
34d4df06 APB |
12237 | /* Final locals can be used as case values in switch |
12238 | statement. Prepare them for this eventuality. */ | |
12239 | if (TREE_CODE (lvalue) == VAR_DECL | |
12240 | && LOCAL_FINAL (lvalue) | |
12241 | && TREE_CONSTANT (new_rhs) | |
12242 | && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue)) | |
12243 | && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue)) | |
12244 | ) | |
12245 | { | |
12246 | TREE_CONSTANT (lvalue) = 1; | |
12247 | DECL_INITIAL (lvalue) = new_rhs; | |
12248 | } | |
12249 | ||
e04a16fb AG |
12250 | TREE_OPERAND (node, 0) = lvalue; |
12251 | TREE_OPERAND (node, 1) = new_rhs; | |
12252 | TREE_TYPE (node) = lhs_type; | |
12253 | return node; | |
12254 | } | |
12255 | ||
b67d701b PB |
12256 | /* Check that type SOURCE can be cast into type DEST. If the cast |
12257 | can't occur at all, return 0 otherwise 1. This function is used to | |
12258 | produce accurate error messages on the reasons why an assignment | |
12259 | failed. */ | |
e04a16fb | 12260 | |
b67d701b PB |
12261 | static tree |
12262 | try_reference_assignconv (lhs_type, rhs) | |
12263 | tree lhs_type, rhs; | |
e04a16fb | 12264 | { |
b67d701b PB |
12265 | tree new_rhs = NULL_TREE; |
12266 | tree rhs_type = TREE_TYPE (rhs); | |
e04a16fb | 12267 | |
b67d701b PB |
12268 | if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type)) |
12269 | { | |
12270 | /* `null' may be assigned to any reference type */ | |
12271 | if (rhs == null_pointer_node) | |
12272 | new_rhs = null_pointer_node; | |
12273 | /* Try the reference assignment conversion */ | |
12274 | else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0)) | |
12275 | new_rhs = rhs; | |
12276 | /* This is a magic assignment that we process differently */ | |
12277 | else if (rhs == soft_exceptioninfo_call_node) | |
12278 | new_rhs = rhs; | |
12279 | } | |
12280 | return new_rhs; | |
12281 | } | |
12282 | ||
12283 | /* Check that RHS can be converted into LHS_TYPE by the assignment | |
12284 | conversion (5.2), for the cases of RHS being a builtin type. Return | |
12285 | NULL_TREE if the conversion fails or if because RHS isn't of a | |
12286 | builtin type. Return a converted RHS if the conversion is possible. */ | |
12287 | ||
12288 | static tree | |
12289 | try_builtin_assignconv (wfl_op1, lhs_type, rhs) | |
12290 | tree wfl_op1, lhs_type, rhs; | |
12291 | { | |
12292 | tree new_rhs = NULL_TREE; | |
12293 | tree rhs_type = TREE_TYPE (rhs); | |
12294 | ||
7e51098e TT |
12295 | /* Handle boolean specially. */ |
12296 | if (TREE_CODE (rhs_type) == BOOLEAN_TYPE | |
12297 | || TREE_CODE (lhs_type) == BOOLEAN_TYPE) | |
12298 | { | |
12299 | if (TREE_CODE (rhs_type) == BOOLEAN_TYPE | |
12300 | && TREE_CODE (lhs_type) == BOOLEAN_TYPE) | |
12301 | new_rhs = rhs; | |
12302 | } | |
12303 | ||
5e942c50 | 12304 | /* Zero accepted everywhere */ |
7e51098e | 12305 | else if (TREE_CODE (rhs) == INTEGER_CST |
5e942c50 APB |
12306 | && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0 |
12307 | && JPRIMITIVE_TYPE_P (rhs_type)) | |
12308 | new_rhs = convert (lhs_type, rhs); | |
12309 | ||
b67d701b PB |
12310 | /* 5.1.1 Try Identity Conversion, |
12311 | 5.1.2 Try Widening Primitive Conversion */ | |
5e942c50 | 12312 | else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)) |
b67d701b PB |
12313 | new_rhs = convert (lhs_type, rhs); |
12314 | ||
12315 | /* Try a narrowing primitive conversion (5.1.3): | |
12316 | - expression is a constant expression of type int AND | |
12317 | - variable is byte, short or char AND | |
12318 | - The value of the expression is representable in the type of the | |
12319 | variable */ | |
12320 | else if (rhs_type == int_type_node && TREE_CONSTANT (rhs) | |
12321 | && (lhs_type == byte_type_node || lhs_type == char_type_node | |
12322 | || lhs_type == short_type_node)) | |
12323 | { | |
12324 | if (int_fits_type_p (rhs, lhs_type)) | |
12325 | new_rhs = convert (lhs_type, rhs); | |
12326 | else if (wfl_op1) /* Might be called with a NULL */ | |
12327 | parse_warning_context | |
7e51098e | 12328 | (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'", |
0a2138e2 | 12329 | print_int_node (rhs), lang_printable_name (lhs_type, 0)); |
b67d701b PB |
12330 | /* Reported a warning that will turn into an error further |
12331 | down, so we don't return */ | |
12332 | } | |
12333 | ||
12334 | return new_rhs; | |
12335 | } | |
12336 | ||
12337 | /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity | |
c00f0fb2 | 12338 | conversion (5.1.1) or widening primitive conversion (5.1.2). Return |
b67d701b PB |
12339 | 0 is the conversion test fails. This implements parts the method |
12340 | invocation convertion (5.3). */ | |
12341 | ||
12342 | static int | |
12343 | valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type) | |
12344 | tree lhs_type, rhs_type; | |
12345 | { | |
acd663ee | 12346 | /* 5.1.1: This is the identity conversion part. */ |
5e942c50 APB |
12347 | if (lhs_type == rhs_type) |
12348 | return 1; | |
12349 | ||
7e51098e TT |
12350 | /* Reject non primitive types and boolean conversions. */ |
12351 | if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type)) | |
b67d701b PB |
12352 | return 0; |
12353 | ||
acd663ee APB |
12354 | /* 5.1.2: widening primitive conversion. byte, even if it's smaller |
12355 | than a char can't be converted into a char. Short can't too, but | |
12356 | the < test below takes care of that */ | |
b67d701b PB |
12357 | if (lhs_type == char_type_node && rhs_type == byte_type_node) |
12358 | return 0; | |
12359 | ||
5e942c50 APB |
12360 | /* Accept all promoted type here. Note, we can't use <= in the test |
12361 | below, because we still need to bounce out assignments of short | |
12362 | to char and the likes */ | |
12363 | if (lhs_type == int_type_node | |
12364 | && (rhs_type == promoted_byte_type_node | |
12365 | || rhs_type == promoted_short_type_node | |
12366 | || rhs_type == promoted_char_type_node | |
12367 | || rhs_type == promoted_boolean_type_node)) | |
12368 | return 1; | |
12369 | ||
acd663ee APB |
12370 | /* From here, an integral is widened if its precision is smaller |
12371 | than the precision of the LHS or if the LHS is a floating point | |
12372 | type, or the RHS is a float and the RHS a double. */ | |
12373 | if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type) | |
12374 | && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type))) | |
12375 | || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type)) | |
12376 | || (rhs_type == float_type_node && lhs_type == double_type_node)) | |
b67d701b PB |
12377 | return 1; |
12378 | ||
12379 | return 0; | |
e04a16fb AG |
12380 | } |
12381 | ||
12382 | /* Check that something of SOURCE type can be assigned or cast to | |
12383 | something of DEST type at runtime. Return 1 if the operation is | |
12384 | valid, 0 otherwise. If CAST is set to 1, we're treating the case | |
12385 | were SOURCE is cast into DEST, which borrows a lot of the | |
12386 | assignment check. */ | |
12387 | ||
12388 | static int | |
12389 | valid_ref_assignconv_cast_p (source, dest, cast) | |
12390 | tree source; | |
12391 | tree dest; | |
12392 | int cast; | |
12393 | { | |
09ed0f70 APB |
12394 | /* SOURCE or DEST might be null if not from a declared entity. */ |
12395 | if (!source || !dest) | |
12396 | return 0; | |
5e942c50 APB |
12397 | if (JNULLP_TYPE_P (source)) |
12398 | return 1; | |
e04a16fb AG |
12399 | if (TREE_CODE (source) == POINTER_TYPE) |
12400 | source = TREE_TYPE (source); | |
12401 | if (TREE_CODE (dest) == POINTER_TYPE) | |
12402 | dest = TREE_TYPE (dest); | |
12403 | /* Case where SOURCE is a class type */ | |
12404 | if (TYPE_CLASS_P (source)) | |
12405 | { | |
12406 | if (TYPE_CLASS_P (dest)) | |
c2952b01 APB |
12407 | return (source == dest |
12408 | || inherits_from_p (source, dest) | |
c2952b01 | 12409 | || (cast && inherits_from_p (dest, source))); |
e04a16fb AG |
12410 | if (TYPE_INTERFACE_P (dest)) |
12411 | { | |
12412 | /* If doing a cast and SOURCE is final, the operation is | |
12413 | always correct a compile time (because even if SOURCE | |
12414 | does not implement DEST, a subclass of SOURCE might). */ | |
12415 | if (cast && !CLASS_FINAL (TYPE_NAME (source))) | |
12416 | return 1; | |
12417 | /* Otherwise, SOURCE must implement DEST */ | |
12418 | return interface_of_p (dest, source); | |
12419 | } | |
12420 | /* DEST is an array, cast permited if SOURCE is of Object type */ | |
12421 | return (cast && source == object_type_node ? 1 : 0); | |
12422 | } | |
12423 | if (TYPE_INTERFACE_P (source)) | |
12424 | { | |
12425 | if (TYPE_CLASS_P (dest)) | |
12426 | { | |
12427 | /* If not casting, DEST must be the Object type */ | |
12428 | if (!cast) | |
12429 | return dest == object_type_node; | |
12430 | /* We're doing a cast. The cast is always valid is class | |
12431 | DEST is not final, otherwise, DEST must implement SOURCE */ | |
b67d701b | 12432 | else if (!CLASS_FINAL (TYPE_NAME (dest))) |
e04a16fb AG |
12433 | return 1; |
12434 | else | |
12435 | return interface_of_p (source, dest); | |
12436 | } | |
12437 | if (TYPE_INTERFACE_P (dest)) | |
12438 | { | |
12439 | /* If doing a cast, then if SOURCE and DEST contain method | |
12440 | with the same signature but different return type, then | |
12441 | this is a (compile time) error */ | |
12442 | if (cast) | |
12443 | { | |
12444 | tree method_source, method_dest; | |
12445 | tree source_type; | |
0a2138e2 | 12446 | tree source_sig; |
e04a16fb AG |
12447 | tree source_name; |
12448 | for (method_source = TYPE_METHODS (source); method_source; | |
12449 | method_source = TREE_CHAIN (method_source)) | |
12450 | { | |
12451 | source_sig = | |
12452 | build_java_argument_signature (TREE_TYPE (method_source)); | |
12453 | source_type = TREE_TYPE (TREE_TYPE (method_source)); | |
12454 | source_name = DECL_NAME (method_source); | |
12455 | for (method_dest = TYPE_METHODS (dest); | |
12456 | method_dest; method_dest = TREE_CHAIN (method_dest)) | |
12457 | if (source_sig == | |
12458 | build_java_argument_signature (TREE_TYPE (method_dest)) | |
12459 | && source_name == DECL_NAME (method_dest) | |
12460 | && source_type != TREE_TYPE (TREE_TYPE (method_dest))) | |
12461 | return 0; | |
12462 | } | |
12463 | return 1; | |
12464 | } | |
12465 | else | |
12466 | return source == dest || interface_of_p (dest, source); | |
12467 | } | |
ee17a290 TT |
12468 | else |
12469 | { | |
12470 | /* Array */ | |
12471 | return (cast | |
12472 | && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable | |
12473 | || (DECL_NAME (TYPE_NAME (source)) | |
12474 | == java_io_serializable))); | |
12475 | } | |
e04a16fb AG |
12476 | } |
12477 | if (TYPE_ARRAY_P (source)) | |
12478 | { | |
12479 | if (TYPE_CLASS_P (dest)) | |
12480 | return dest == object_type_node; | |
09ed0f70 | 12481 | /* Can't cast an array to an interface unless the interface is |
ee17a290 | 12482 | java.lang.Cloneable or java.io.Serializable. */ |
e04a16fb | 12483 | if (TYPE_INTERFACE_P (dest)) |
ee17a290 TT |
12484 | return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable |
12485 | || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable); | |
e04a16fb AG |
12486 | else /* Arrays */ |
12487 | { | |
12488 | tree source_element_type = TYPE_ARRAY_ELEMENT (source); | |
12489 | tree dest_element_type = TYPE_ARRAY_ELEMENT (dest); | |
12490 | ||
b9f7e36c APB |
12491 | /* In case of severe errors, they turn out null */ |
12492 | if (!dest_element_type || !source_element_type) | |
12493 | return 0; | |
e04a16fb AG |
12494 | if (source_element_type == dest_element_type) |
12495 | return 1; | |
12496 | return valid_ref_assignconv_cast_p (source_element_type, | |
12497 | dest_element_type, cast); | |
12498 | } | |
12499 | return 0; | |
12500 | } | |
12501 | return 0; | |
12502 | } | |
12503 | ||
b67d701b PB |
12504 | static int |
12505 | valid_cast_to_p (source, dest) | |
12506 | tree source; | |
12507 | tree dest; | |
12508 | { | |
12509 | if (TREE_CODE (source) == POINTER_TYPE) | |
12510 | source = TREE_TYPE (source); | |
12511 | if (TREE_CODE (dest) == POINTER_TYPE) | |
12512 | dest = TREE_TYPE (dest); | |
12513 | ||
12514 | if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE) | |
12515 | return valid_ref_assignconv_cast_p (source, dest, 1); | |
12516 | ||
12517 | else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest)) | |
12518 | return 1; | |
12519 | ||
7e51098e TT |
12520 | else if (TREE_CODE (source) == BOOLEAN_TYPE |
12521 | && TREE_CODE (dest) == BOOLEAN_TYPE) | |
12522 | return 1; | |
12523 | ||
b67d701b PB |
12524 | return 0; |
12525 | } | |
12526 | ||
15fdcfe9 PB |
12527 | static tree |
12528 | do_unary_numeric_promotion (arg) | |
12529 | tree arg; | |
12530 | { | |
12531 | tree type = TREE_TYPE (arg); | |
7e51098e TT |
12532 | if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32) |
12533 | || TREE_CODE (type) == CHAR_TYPE) | |
15fdcfe9 PB |
12534 | arg = convert (int_type_node, arg); |
12535 | return arg; | |
12536 | } | |
12537 | ||
acd663ee APB |
12538 | /* Return a non zero value if SOURCE can be converted into DEST using |
12539 | the method invocation conversion rule (5.3). */ | |
b67d701b PB |
12540 | static int |
12541 | valid_method_invocation_conversion_p (dest, source) | |
12542 | tree dest, source; | |
12543 | { | |
e3884b71 | 12544 | return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest) |
acd663ee APB |
12545 | && valid_builtin_assignconv_identity_widening_p (dest, source)) |
12546 | || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source)) | |
12547 | && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest)) | |
12548 | && valid_ref_assignconv_cast_p (source, dest, 0))); | |
b67d701b PB |
12549 | } |
12550 | ||
e04a16fb AG |
12551 | /* Build an incomplete binop expression. */ |
12552 | ||
12553 | static tree | |
12554 | build_binop (op, op_location, op1, op2) | |
12555 | enum tree_code op; | |
12556 | int op_location; | |
12557 | tree op1, op2; | |
12558 | { | |
5e942c50 | 12559 | tree binop = build (op, NULL_TREE, op1, op2); |
e04a16fb AG |
12560 | TREE_SIDE_EFFECTS (binop) = 1; |
12561 | /* Store the location of the operator, for better error report. The | |
12562 | string of the operator will be rebuild based on the OP value. */ | |
12563 | EXPR_WFL_LINECOL (binop) = op_location; | |
12564 | return binop; | |
12565 | } | |
12566 | ||
12567 | /* Build the string of the operator retained by NODE. If NODE is part | |
12568 | of a compound expression, add an '=' at the end of the string. This | |
12569 | function is called when an error needs to be reported on an | |
12570 | operator. The string is returned as a pointer to a static character | |
12571 | buffer. */ | |
12572 | ||
12573 | static char * | |
12574 | operator_string (node) | |
12575 | tree node; | |
12576 | { | |
12577 | #define BUILD_OPERATOR_STRING(S) \ | |
12578 | { \ | |
12579 | sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \ | |
12580 | return buffer; \ | |
12581 | } | |
12582 | ||
12583 | static char buffer [10]; | |
12584 | switch (TREE_CODE (node)) | |
12585 | { | |
12586 | case MULT_EXPR: BUILD_OPERATOR_STRING ("*"); | |
12587 | case RDIV_EXPR: BUILD_OPERATOR_STRING ("/"); | |
12588 | case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%"); | |
12589 | case PLUS_EXPR: BUILD_OPERATOR_STRING ("+"); | |
12590 | case MINUS_EXPR: BUILD_OPERATOR_STRING ("-"); | |
12591 | case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<"); | |
12592 | case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>"); | |
12593 | case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>"); | |
12594 | case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&"); | |
12595 | case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^"); | |
12596 | case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|"); | |
12597 | case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&"); | |
12598 | case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||"); | |
12599 | case EQ_EXPR: BUILD_OPERATOR_STRING ("=="); | |
12600 | case NE_EXPR: BUILD_OPERATOR_STRING ("!="); | |
12601 | case GT_EXPR: BUILD_OPERATOR_STRING (">"); | |
12602 | case GE_EXPR: BUILD_OPERATOR_STRING (">="); | |
12603 | case LT_EXPR: BUILD_OPERATOR_STRING ("<"); | |
12604 | case LE_EXPR: BUILD_OPERATOR_STRING ("<="); | |
b67d701b | 12605 | case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+"); |
e04a16fb AG |
12606 | case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-"); |
12607 | case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!"); | |
12608 | case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~"); | |
12609 | case PREINCREMENT_EXPR: /* Fall through */ | |
12610 | case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++"); | |
12611 | case PREDECREMENT_EXPR: /* Fall through */ | |
12612 | case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--"); | |
12613 | default: | |
12614 | fatal ("unregistered operator %s - operator_string", | |
12615 | tree_code_name [TREE_CODE (node)]); | |
12616 | } | |
12617 | return NULL; | |
12618 | #undef BUILD_OPERATOR_STRING | |
12619 | } | |
12620 | ||
5cbdba64 APB |
12621 | /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */ |
12622 | ||
12623 | static int | |
12624 | java_decl_equiv (var_acc1, var_acc2) | |
12625 | tree var_acc1, var_acc2; | |
12626 | { | |
12627 | if (JDECL_P (var_acc1)) | |
12628 | return (var_acc1 == var_acc2); | |
12629 | ||
12630 | return (TREE_CODE (var_acc1) == COMPONENT_REF | |
12631 | && TREE_CODE (var_acc2) == COMPONENT_REF | |
12632 | && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0) | |
12633 | == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0) | |
12634 | && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1)); | |
12635 | } | |
12636 | ||
12637 | /* Return a non zero value if CODE is one of the operators that can be | |
12638 | used in conjunction with the `=' operator in a compound assignment. */ | |
12639 | ||
12640 | static int | |
12641 | binop_compound_p (code) | |
12642 | enum tree_code code; | |
12643 | { | |
12644 | int i; | |
12645 | for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++) | |
12646 | if (binop_lookup [i] == code) | |
12647 | break; | |
12648 | ||
12649 | return i < BINOP_COMPOUND_CANDIDATES; | |
12650 | } | |
12651 | ||
12652 | /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */ | |
12653 | ||
12654 | static tree | |
12655 | java_refold (t) | |
12656 | tree t; | |
12657 | { | |
12658 | tree c, b, ns, decl; | |
12659 | ||
12660 | if (TREE_CODE (t) != MODIFY_EXPR) | |
12661 | return t; | |
12662 | ||
12663 | c = TREE_OPERAND (t, 1); | |
12664 | if (! (c && TREE_CODE (c) == COMPOUND_EXPR | |
12665 | && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR | |
12666 | && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1))))) | |
12667 | return t; | |
12668 | ||
12669 | /* Now the left branch of the binary operator. */ | |
12670 | b = TREE_OPERAND (TREE_OPERAND (c, 1), 0); | |
12671 | if (! (b && TREE_CODE (b) == NOP_EXPR | |
12672 | && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR)) | |
12673 | return t; | |
12674 | ||
12675 | ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0); | |
12676 | if (! (ns && TREE_CODE (ns) == NOP_EXPR | |
12677 | && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR)) | |
12678 | return t; | |
12679 | ||
12680 | decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0); | |
12681 | if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF) | |
12682 | /* It's got to be the an equivalent decl */ | |
12683 | && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0))) | |
12684 | { | |
12685 | /* Shorten the NOP_EXPR/SAVE_EXPR path. */ | |
12686 | TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0); | |
12687 | /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */ | |
12688 | TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1); | |
12689 | /* Change the right part of the BINOP_EXPR */ | |
12690 | TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0); | |
12691 | } | |
12692 | ||
12693 | return t; | |
12694 | } | |
12695 | ||
e04a16fb AG |
12696 | /* Binary operators (15.16 up to 15.18). We return error_mark_node on |
12697 | errors but we modify NODE so that it contains the type computed | |
12698 | according to the expression, when it's fixed. Otherwise, we write | |
12699 | error_mark_node as the type. It allows us to further the analysis | |
12700 | of remaining nodes and detects more errors in certain cases. */ | |
12701 | ||
12702 | static tree | |
12703 | patch_binop (node, wfl_op1, wfl_op2) | |
12704 | tree node; | |
12705 | tree wfl_op1; | |
12706 | tree wfl_op2; | |
12707 | { | |
12708 | tree op1 = TREE_OPERAND (node, 0); | |
12709 | tree op2 = TREE_OPERAND (node, 1); | |
12710 | tree op1_type = TREE_TYPE (op1); | |
12711 | tree op2_type = TREE_TYPE (op2); | |
48a840d9 | 12712 | tree prom_type = NULL_TREE, cn; |
e04a16fb | 12713 | int code = TREE_CODE (node); |
b67d701b | 12714 | |
e04a16fb AG |
12715 | /* If 1, tell the routine that we have to return error_mark_node |
12716 | after checking for the initialization of the RHS */ | |
12717 | int error_found = 0; | |
12718 | ||
e04a16fb AG |
12719 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); |
12720 | ||
e04a16fb AG |
12721 | switch (code) |
12722 | { | |
12723 | /* 15.16 Multiplicative operators */ | |
12724 | case MULT_EXPR: /* 15.16.1 Multiplication Operator * */ | |
12725 | case RDIV_EXPR: /* 15.16.2 Division Operator / */ | |
c2952b01 | 12726 | case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */ |
e04a16fb | 12727 | case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */ |
7e51098e | 12728 | if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type)) |
e04a16fb | 12729 | { |
7e51098e | 12730 | if (!JNUMERIC_TYPE_P (op1_type)) |
e04a16fb | 12731 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type); |
7e51098e | 12732 | if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type)) |
e04a16fb AG |
12733 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type); |
12734 | TREE_TYPE (node) = error_mark_node; | |
12735 | error_found = 1; | |
12736 | break; | |
12737 | } | |
12738 | prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
12739 | /* Change the division operator if necessary */ | |
12740 | if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE) | |
12741 | TREE_SET_CODE (node, TRUNC_DIV_EXPR); | |
0b4d333e | 12742 | |
aa4759c1 AH |
12743 | if (TREE_CODE (prom_type) == INTEGER_TYPE |
12744 | && flag_use_divide_subroutine | |
12745 | && ! flag_emit_class_files | |
12746 | && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR)) | |
12747 | return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2); | |
12748 | ||
0b4d333e APB |
12749 | /* This one is more complicated. FLOATs are processed by a |
12750 | function call to soft_fmod. Duplicate the value of the | |
12751 | COMPOUND_ASSIGN_P flag. */ | |
e04a16fb | 12752 | if (code == TRUNC_MOD_EXPR) |
0b4d333e APB |
12753 | { |
12754 | tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2); | |
12755 | COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node); | |
dc0b3eff PB |
12756 | TREE_SIDE_EFFECTS (mod) |
12757 | = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); | |
0b4d333e APB |
12758 | return mod; |
12759 | } | |
e04a16fb AG |
12760 | break; |
12761 | ||
12762 | /* 15.17 Additive Operators */ | |
12763 | case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */ | |
b67d701b PB |
12764 | |
12765 | /* Operation is valid if either one argument is a string | |
12766 | constant, a String object or a StringBuffer crafted for the | |
12767 | purpose of the a previous usage of the String concatenation | |
12768 | operator */ | |
12769 | ||
12770 | if (TREE_CODE (op1) == STRING_CST | |
12771 | || TREE_CODE (op2) == STRING_CST | |
12772 | || JSTRING_TYPE_P (op1_type) | |
12773 | || JSTRING_TYPE_P (op2_type) | |
12774 | || IS_CRAFTED_STRING_BUFFER_P (op1) | |
12775 | || IS_CRAFTED_STRING_BUFFER_P (op2)) | |
12776 | return build_string_concatenation (op1, op2); | |
12777 | ||
e04a16fb AG |
12778 | case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for |
12779 | Numeric Types */ | |
7e51098e | 12780 | if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type)) |
e04a16fb | 12781 | { |
7e51098e | 12782 | if (!JNUMERIC_TYPE_P (op1_type)) |
e04a16fb | 12783 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type); |
7e51098e | 12784 | if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type)) |
e04a16fb AG |
12785 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type); |
12786 | TREE_TYPE (node) = error_mark_node; | |
12787 | error_found = 1; | |
12788 | break; | |
12789 | } | |
12790 | prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
12791 | break; | |
12792 | ||
12793 | /* 15.18 Shift Operators */ | |
12794 | case LSHIFT_EXPR: | |
12795 | case RSHIFT_EXPR: | |
12796 | case URSHIFT_EXPR: | |
12797 | if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type)) | |
12798 | { | |
12799 | if (!JINTEGRAL_TYPE_P (op1_type)) | |
12800 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type); | |
12801 | else | |
1ebadc60 | 12802 | { |
7e51098e | 12803 | if (JNUMERIC_TYPE_P (op2_type)) |
1ebadc60 | 12804 | parse_error_context (wfl_operator, |
781b0558 | 12805 | "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral", |
1ebadc60 KG |
12806 | operator_string (node), |
12807 | lang_printable_name (op2_type, 0)); | |
12808 | else | |
781b0558 KG |
12809 | parse_error_context (wfl_operator, |
12810 | "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral", | |
1ebadc60 KG |
12811 | operator_string (node), |
12812 | lang_printable_name (op2_type, 0)); | |
12813 | } | |
e04a16fb AG |
12814 | TREE_TYPE (node) = error_mark_node; |
12815 | error_found = 1; | |
12816 | break; | |
12817 | } | |
12818 | ||
12819 | /* Unary numeric promotion (5.6.1) is performed on each operand | |
12820 | separatly */ | |
15fdcfe9 PB |
12821 | op1 = do_unary_numeric_promotion (op1); |
12822 | op2 = do_unary_numeric_promotion (op2); | |
e04a16fb AG |
12823 | |
12824 | /* The type of the shift expression is the type of the promoted | |
12825 | type of the left-hand operand */ | |
12826 | prom_type = TREE_TYPE (op1); | |
12827 | ||
c2952b01 APB |
12828 | /* Shift int only up to 0x1f and long up to 0x3f */ |
12829 | if (prom_type == int_type_node) | |
12830 | op2 = fold (build (BIT_AND_EXPR, int_type_node, op2, | |
12831 | build_int_2 (0x1f, 0))); | |
12832 | else | |
12833 | op2 = fold (build (BIT_AND_EXPR, int_type_node, op2, | |
12834 | build_int_2 (0x3f, 0))); | |
e04a16fb AG |
12835 | |
12836 | /* The >>> operator is a >> operating on unsigned quantities */ | |
15fdcfe9 | 12837 | if (code == URSHIFT_EXPR && ! flag_emit_class_files) |
e04a16fb | 12838 | { |
0b4d333e | 12839 | tree to_return; |
73333a87 AH |
12840 | tree utype = unsigned_type (prom_type); |
12841 | op1 = convert (utype, op1); | |
e04a16fb | 12842 | TREE_SET_CODE (node, RSHIFT_EXPR); |
73333a87 AH |
12843 | TREE_OPERAND (node, 0) = op1; |
12844 | TREE_OPERAND (node, 1) = op2; | |
12845 | TREE_TYPE (node) = utype; | |
0b4d333e APB |
12846 | to_return = convert (prom_type, node); |
12847 | /* Copy the original value of the COMPOUND_ASSIGN_P flag */ | |
12848 | COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node); | |
dc0b3eff PB |
12849 | TREE_SIDE_EFFECTS (to_return) |
12850 | = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); | |
0b4d333e | 12851 | return to_return; |
e04a16fb AG |
12852 | } |
12853 | break; | |
5e942c50 APB |
12854 | |
12855 | /* 15.19.1 Type Comparison Operator instaceof */ | |
12856 | case INSTANCEOF_EXPR: | |
12857 | ||
12858 | TREE_TYPE (node) = boolean_type_node; | |
12859 | ||
12860 | if (!(op2_type = resolve_type_during_patch (op2))) | |
12861 | return error_mark_node; | |
12862 | ||
12863 | /* The first operand must be a reference type or the null type */ | |
12864 | if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node) | |
12865 | error_found = 1; /* Error reported further below */ | |
12866 | ||
12867 | /* The second operand must be a reference type */ | |
12868 | if (!JREFERENCE_TYPE_P (op2_type)) | |
12869 | { | |
12870 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op2); | |
12871 | parse_error_context | |
12872 | (wfl_operator, "Invalid argument `%s' for `instanceof'", | |
12873 | lang_printable_name (op2_type, 0)); | |
12874 | error_found = 1; | |
12875 | } | |
12876 | ||
12877 | if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1)) | |
12878 | { | |
12879 | /* If the first operand is null, the result is always false */ | |
12880 | if (op1 == null_pointer_node) | |
12881 | return boolean_false_node; | |
15fdcfe9 PB |
12882 | else if (flag_emit_class_files) |
12883 | { | |
12884 | TREE_OPERAND (node, 1) = op2_type; | |
dc0b3eff | 12885 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1); |
15fdcfe9 PB |
12886 | return node; |
12887 | } | |
5e942c50 APB |
12888 | /* Otherwise we have to invoke instance of to figure it out */ |
12889 | else | |
67db0ce7 | 12890 | return build_instanceof (op1, op2_type); |
5e942c50 APB |
12891 | } |
12892 | /* There is no way the expression operand can be an instance of | |
12893 | the type operand. This is a compile time error. */ | |
12894 | else | |
12895 | { | |
c2e3db92 | 12896 | char *t1 = xstrdup (lang_printable_name (op1_type, 0)); |
5e942c50 APB |
12897 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); |
12898 | parse_error_context | |
12899 | (wfl_operator, "Impossible for `%s' to be instance of `%s'", | |
12900 | t1, lang_printable_name (op2_type, 0)); | |
12901 | free (t1); | |
12902 | error_found = 1; | |
12903 | } | |
e04a16fb | 12904 | |
5e942c50 | 12905 | break; |
e04a16fb AG |
12906 | |
12907 | /* 15.21 Bitwise and Logical Operators */ | |
12908 | case BIT_AND_EXPR: | |
12909 | case BIT_XOR_EXPR: | |
12910 | case BIT_IOR_EXPR: | |
12911 | if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type)) | |
12912 | /* Binary numeric promotion is performed on both operand and the | |
12913 | expression retain that type */ | |
12914 | prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
12915 | ||
12916 | else if (TREE_CODE (op1_type) == BOOLEAN_TYPE | |
12917 | && TREE_CODE (op1_type) == BOOLEAN_TYPE) | |
12918 | /* The type of the bitwise operator expression is BOOLEAN */ | |
12919 | prom_type = boolean_type_node; | |
12920 | else | |
12921 | { | |
12922 | if (!JINTEGRAL_TYPE_P (op1_type)) | |
12923 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type); | |
12924 | if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type)) | |
12925 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type); | |
12926 | TREE_TYPE (node) = error_mark_node; | |
12927 | error_found = 1; | |
12928 | /* Insert a break here if adding thing before the switch's | |
12929 | break for this case */ | |
12930 | } | |
12931 | break; | |
12932 | ||
12933 | /* 15.22 Conditional-And Operator */ | |
12934 | case TRUTH_ANDIF_EXPR: | |
12935 | /* 15.23 Conditional-Or Operator */ | |
12936 | case TRUTH_ORIF_EXPR: | |
12937 | /* Operands must be of BOOLEAN type */ | |
12938 | if (TREE_CODE (op1_type) != BOOLEAN_TYPE || | |
12939 | TREE_CODE (op2_type) != BOOLEAN_TYPE) | |
12940 | { | |
12941 | if (TREE_CODE (op1_type) != BOOLEAN_TYPE) | |
12942 | ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type); | |
12943 | if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type)) | |
12944 | ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type); | |
12945 | TREE_TYPE (node) = boolean_type_node; | |
12946 | error_found = 1; | |
12947 | break; | |
12948 | } | |
12949 | /* The type of the conditional operators is BOOLEAN */ | |
12950 | prom_type = boolean_type_node; | |
12951 | break; | |
12952 | ||
12953 | /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */ | |
12954 | case LT_EXPR: | |
12955 | case GT_EXPR: | |
12956 | case LE_EXPR: | |
12957 | case GE_EXPR: | |
12958 | /* The type of each of the operands must be a primitive numeric | |
12959 | type */ | |
12960 | if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type)) | |
12961 | { | |
12962 | if (!JNUMERIC_TYPE_P (op1_type)) | |
12963 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type); | |
12964 | if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type)) | |
12965 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type); | |
12966 | TREE_TYPE (node) = boolean_type_node; | |
12967 | error_found = 1; | |
12968 | break; | |
12969 | } | |
12970 | /* Binary numeric promotion is performed on the operands */ | |
12971 | binary_numeric_promotion (op1_type, op2_type, &op1, &op2); | |
12972 | /* The type of the relation expression is always BOOLEAN */ | |
12973 | prom_type = boolean_type_node; | |
12974 | break; | |
12975 | ||
12976 | /* 15.20 Equality Operator */ | |
12977 | case EQ_EXPR: | |
12978 | case NE_EXPR: | |
48a840d9 APB |
12979 | /* It's time for us to patch the strings. */ |
12980 | if ((cn = patch_string (op1))) | |
12981 | { | |
12982 | op1 = cn; | |
12983 | op1_type = TREE_TYPE (op1); | |
12984 | } | |
12985 | if ((cn = patch_string (op2))) | |
12986 | { | |
12987 | op2 = cn; | |
12988 | op2_type = TREE_TYPE (op2); | |
12989 | } | |
12990 | ||
e04a16fb AG |
12991 | /* 15.20.1 Numerical Equality Operators == and != */ |
12992 | /* Binary numeric promotion is performed on the operands */ | |
5e942c50 | 12993 | if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type)) |
e04a16fb AG |
12994 | binary_numeric_promotion (op1_type, op2_type, &op1, &op2); |
12995 | ||
12996 | /* 15.20.2 Boolean Equality Operators == and != */ | |
12997 | else if (TREE_CODE (op1_type) == BOOLEAN_TYPE && | |
12998 | TREE_CODE (op2_type) == BOOLEAN_TYPE) | |
12999 | ; /* Nothing to do here */ | |
13000 | ||
13001 | /* 15.20.3 Reference Equality Operators == and != */ | |
5e942c50 APB |
13002 | /* Types have to be either references or the null type. If |
13003 | they're references, it must be possible to convert either | |
13004 | type to the other by casting conversion. */ | |
b9f7e36c APB |
13005 | else if (op1 == null_pointer_node || op2 == null_pointer_node |
13006 | || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type) | |
5e942c50 APB |
13007 | && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1) |
13008 | || valid_ref_assignconv_cast_p (op2_type, | |
13009 | op1_type, 1)))) | |
e04a16fb AG |
13010 | ; /* Nothing to do here */ |
13011 | ||
13012 | /* Else we have an error figure what can't be converted into | |
13013 | what and report the error */ | |
13014 | else | |
13015 | { | |
13016 | char *t1; | |
c2e3db92 | 13017 | t1 = xstrdup (lang_printable_name (op1_type, 0)); |
e04a16fb | 13018 | parse_error_context |
781b0558 KG |
13019 | (wfl_operator, |
13020 | "Incompatible type for `%s'. Can't convert `%s' to `%s'", | |
13021 | operator_string (node), t1, | |
0a2138e2 | 13022 | lang_printable_name (op2_type, 0)); |
e04a16fb AG |
13023 | free (t1); |
13024 | TREE_TYPE (node) = boolean_type_node; | |
13025 | error_found = 1; | |
13026 | break; | |
13027 | } | |
13028 | prom_type = boolean_type_node; | |
13029 | break; | |
13030 | } | |
13031 | ||
e04a16fb AG |
13032 | if (error_found) |
13033 | return error_mark_node; | |
13034 | ||
13035 | TREE_OPERAND (node, 0) = op1; | |
13036 | TREE_OPERAND (node, 1) = op2; | |
13037 | TREE_TYPE (node) = prom_type; | |
dc0b3eff PB |
13038 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); |
13039 | ||
ce6e9147 APB |
13040 | if (flag_emit_xref) |
13041 | return node; | |
13042 | ||
d1472141 PB |
13043 | /* fold does not respect side-effect order as required for Java but not C. |
13044 | * Also, it sometimes create SAVE_EXPRs which are bad when emitting | |
13045 | * bytecode. | |
13046 | */ | |
13047 | if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2)) | |
13048 | : ! TREE_SIDE_EFFECTS (node)) | |
aee48ef8 PB |
13049 | node = fold (node); |
13050 | return node; | |
e04a16fb AG |
13051 | } |
13052 | ||
b67d701b PB |
13053 | /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non |
13054 | zero value, the value of CSTE comes after the valude of STRING */ | |
13055 | ||
13056 | static tree | |
13057 | do_merge_string_cste (cste, string, string_len, after) | |
13058 | tree cste; | |
49f48c71 | 13059 | const char *string; |
b67d701b PB |
13060 | int string_len, after; |
13061 | { | |
49f48c71 | 13062 | const char *old = TREE_STRING_POINTER (cste); |
354e99ce APB |
13063 | int old_len = TREE_STRING_LENGTH (cste); |
13064 | int len = old_len + string_len; | |
13065 | char *new; | |
13066 | ||
13067 | cste = make_node (STRING_CST); | |
b67d701b | 13068 | TREE_STRING_LENGTH (cste) = len; |
1f8f4a0b | 13069 | new = TREE_STRING_POINTER (cste) = ggc_alloc (len+1); |
354e99ce | 13070 | |
b67d701b PB |
13071 | if (after) |
13072 | { | |
354e99ce APB |
13073 | memcpy (new, string, string_len); |
13074 | memcpy (&new [string_len], old, old_len); | |
b67d701b PB |
13075 | } |
13076 | else | |
13077 | { | |
354e99ce APB |
13078 | memcpy (new, old, old_len); |
13079 | memcpy (&new [old_len], string, string_len); | |
b67d701b | 13080 | } |
354e99ce | 13081 | new [len] = '\0'; |
b67d701b PB |
13082 | return cste; |
13083 | } | |
13084 | ||
13085 | /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a | |
13086 | new STRING_CST on success, NULL_TREE on failure */ | |
13087 | ||
13088 | static tree | |
13089 | merge_string_cste (op1, op2, after) | |
13090 | tree op1, op2; | |
13091 | int after; | |
13092 | { | |
13093 | /* Handle two string constants right away */ | |
13094 | if (TREE_CODE (op2) == STRING_CST) | |
13095 | return do_merge_string_cste (op1, TREE_STRING_POINTER (op2), | |
13096 | TREE_STRING_LENGTH (op2), after); | |
13097 | ||
13098 | /* Reasonable integer constant can be treated right away */ | |
13099 | if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2)) | |
13100 | { | |
49f48c71 KG |
13101 | static const char *boolean_true = "true"; |
13102 | static const char *boolean_false = "false"; | |
13103 | static const char *null_pointer = "null"; | |
b67d701b | 13104 | char ch[3]; |
49f48c71 | 13105 | const char *string; |
b67d701b PB |
13106 | |
13107 | if (op2 == boolean_true_node) | |
13108 | string = boolean_true; | |
13109 | else if (op2 == boolean_false_node) | |
13110 | string = boolean_false; | |
13111 | else if (op2 == null_pointer_node) | |
13112 | string = null_pointer; | |
13113 | else if (TREE_TYPE (op2) == char_type_node) | |
13114 | { | |
13115 | ch[0] = (char )TREE_INT_CST_LOW (op2); | |
13116 | ch[1] = '\0'; | |
13117 | string = ch; | |
13118 | } | |
13119 | else | |
13120 | string = print_int_node (op2); | |
13121 | ||
13122 | return do_merge_string_cste (op1, string, strlen (string), after); | |
13123 | } | |
13124 | return NULL_TREE; | |
13125 | } | |
13126 | ||
13127 | /* Tries to statically concatenate OP1 and OP2 if possible. Either one | |
13128 | has to be a STRING_CST and the other part must be a STRING_CST or a | |
13129 | INTEGRAL constant. Return a new STRING_CST if the operation | |
13130 | succeed, NULL_TREE otherwise. | |
13131 | ||
13132 | If the case we want to optimize for space, we might want to return | |
13133 | NULL_TREE for each invocation of this routine. FIXME */ | |
13134 | ||
13135 | static tree | |
13136 | string_constant_concatenation (op1, op2) | |
13137 | tree op1, op2; | |
13138 | { | |
13139 | if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST)) | |
13140 | { | |
0a2138e2 | 13141 | tree string, rest; |
b67d701b PB |
13142 | int invert; |
13143 | ||
13144 | string = (TREE_CODE (op1) == STRING_CST ? op1 : op2); | |
13145 | rest = (string == op1 ? op2 : op1); | |
13146 | invert = (string == op1 ? 0 : 1 ); | |
13147 | ||
13148 | /* Walk REST, only if it looks reasonable */ | |
13149 | if (TREE_CODE (rest) != STRING_CST | |
13150 | && !IS_CRAFTED_STRING_BUFFER_P (rest) | |
13151 | && !JSTRING_TYPE_P (TREE_TYPE (rest)) | |
13152 | && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION) | |
13153 | { | |
13154 | rest = java_complete_tree (rest); | |
13155 | if (rest == error_mark_node) | |
13156 | return error_mark_node; | |
13157 | rest = fold (rest); | |
13158 | } | |
13159 | return merge_string_cste (string, rest, invert); | |
13160 | } | |
13161 | return NULL_TREE; | |
13162 | } | |
13163 | ||
13164 | /* Implement the `+' operator. Does static optimization if possible, | |
13165 | otherwise create (if necessary) and append elements to a | |
13166 | StringBuffer. The StringBuffer will be carried around until it is | |
13167 | used for a function call or an assignment. Then toString() will be | |
13168 | called on it to turn it into a String object. */ | |
13169 | ||
13170 | static tree | |
13171 | build_string_concatenation (op1, op2) | |
13172 | tree op1, op2; | |
13173 | { | |
13174 | tree result; | |
dc0b3eff | 13175 | int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2); |
ce6e9147 APB |
13176 | |
13177 | if (flag_emit_xref) | |
13178 | return build (PLUS_EXPR, string_type_node, op1, op2); | |
b67d701b PB |
13179 | |
13180 | /* Try to do some static optimization */ | |
13181 | if ((result = string_constant_concatenation (op1, op2))) | |
13182 | return result; | |
13183 | ||
c0d87ff6 PB |
13184 | /* Discard empty strings on either side of the expression */ |
13185 | if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0) | |
acd663ee APB |
13186 | { |
13187 | op1 = op2; | |
13188 | op2 = NULL_TREE; | |
13189 | } | |
c0d87ff6 | 13190 | else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0) |
acd663ee | 13191 | op2 = NULL_TREE; |
b67d701b | 13192 | |
acd663ee | 13193 | /* If operands are string constant, turn then into object references */ |
b67d701b PB |
13194 | if (TREE_CODE (op1) == STRING_CST) |
13195 | op1 = patch_string_cst (op1); | |
acd663ee | 13196 | if (op2 && TREE_CODE (op2) == STRING_CST) |
b67d701b PB |
13197 | op2 = patch_string_cst (op2); |
13198 | ||
acd663ee APB |
13199 | /* If either one of the constant is null and the other non null |
13200 | operand is a String object, return it. */ | |
13201 | if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2) | |
13202 | return op1; | |
13203 | ||
b67d701b PB |
13204 | /* If OP1 isn't already a StringBuffer, create and |
13205 | initialize a new one */ | |
13206 | if (!IS_CRAFTED_STRING_BUFFER_P (op1)) | |
13207 | { | |
13208 | /* Two solutions here: | |
c52b5771 AG |
13209 | 1) OP1 is a constant string reference, we call new StringBuffer(OP1) |
13210 | 2) OP1 is something else, we call new StringBuffer().append(OP1). */ | |
13211 | if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1))) | |
b67d701b PB |
13212 | op1 = BUILD_STRING_BUFFER (op1); |
13213 | else | |
13214 | { | |
13215 | tree aNew = BUILD_STRING_BUFFER (NULL_TREE); | |
13216 | op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0); | |
13217 | } | |
13218 | } | |
13219 | ||
acd663ee APB |
13220 | if (op2) |
13221 | { | |
13222 | /* OP1 is no longer the last node holding a crafted StringBuffer */ | |
13223 | IS_CRAFTED_STRING_BUFFER_P (op1) = 0; | |
13224 | /* Create a node for `{new...,xxx}.append (op2)' */ | |
13225 | if (op2) | |
13226 | op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0); | |
13227 | } | |
13228 | ||
b67d701b PB |
13229 | /* Mark the last node holding a crafted StringBuffer */ |
13230 | IS_CRAFTED_STRING_BUFFER_P (op1) = 1; | |
dc0b3eff PB |
13231 | |
13232 | TREE_SIDE_EFFECTS (op1) = side_effects; | |
b67d701b PB |
13233 | return op1; |
13234 | } | |
13235 | ||
13236 | /* Patch the string node NODE. NODE can be a STRING_CST of a crafted | |
13237 | StringBuffer. If no string were found to be patched, return | |
13238 | NULL. */ | |
13239 | ||
13240 | static tree | |
13241 | patch_string (node) | |
13242 | tree node; | |
13243 | { | |
1179ebc2 APB |
13244 | if (node == error_mark_node) |
13245 | return error_mark_node; | |
b67d701b PB |
13246 | if (TREE_CODE (node) == STRING_CST) |
13247 | return patch_string_cst (node); | |
13248 | else if (IS_CRAFTED_STRING_BUFFER_P (node)) | |
13249 | { | |
c877974e | 13250 | int saved = ctxp->explicit_constructor_p; |
b67d701b | 13251 | tree invoke = build_method_invocation (wfl_to_string, NULL_TREE); |
c877974e APB |
13252 | tree ret; |
13253 | /* Temporary disable forbid the use of `this'. */ | |
13254 | ctxp->explicit_constructor_p = 0; | |
13255 | ret = java_complete_tree (make_qualified_primary (node, invoke, 0)); | |
1729c265 APB |
13256 | /* String concatenation arguments must be evaluated in order too. */ |
13257 | ret = force_evaluation_order (ret); | |
c877974e APB |
13258 | /* Restore it at its previous value */ |
13259 | ctxp->explicit_constructor_p = saved; | |
13260 | return ret; | |
b67d701b PB |
13261 | } |
13262 | return NULL_TREE; | |
13263 | } | |
13264 | ||
13265 | /* Build the internal representation of a string constant. */ | |
13266 | ||
13267 | static tree | |
13268 | patch_string_cst (node) | |
13269 | tree node; | |
13270 | { | |
13271 | int location; | |
15fdcfe9 PB |
13272 | if (! flag_emit_class_files) |
13273 | { | |
15fdcfe9 PB |
13274 | node = get_identifier (TREE_STRING_POINTER (node)); |
13275 | location = alloc_name_constant (CONSTANT_String, node); | |
13276 | node = build_ref_from_constant_pool (location); | |
13277 | } | |
cd9643f7 | 13278 | TREE_TYPE (node) = string_ptr_type_node; |
b67d701b PB |
13279 | TREE_CONSTANT (node) = 1; |
13280 | return node; | |
13281 | } | |
13282 | ||
13283 | /* Build an incomplete unary operator expression. */ | |
e04a16fb AG |
13284 | |
13285 | static tree | |
13286 | build_unaryop (op_token, op_location, op1) | |
13287 | int op_token, op_location; | |
13288 | tree op1; | |
13289 | { | |
13290 | enum tree_code op; | |
13291 | tree unaryop; | |
13292 | switch (op_token) | |
13293 | { | |
b67d701b | 13294 | case PLUS_TK: op = UNARY_PLUS_EXPR; break; |
e04a16fb AG |
13295 | case MINUS_TK: op = NEGATE_EXPR; break; |
13296 | case NEG_TK: op = TRUTH_NOT_EXPR; break; | |
13297 | case NOT_TK: op = BIT_NOT_EXPR; break; | |
13298 | default: fatal ("Unknown token `%d' for unary operator - build_unaryop", | |
13299 | op_token); | |
13300 | } | |
13301 | ||
13302 | unaryop = build1 (op, NULL_TREE, op1); | |
e04a16fb AG |
13303 | TREE_SIDE_EFFECTS (unaryop) = 1; |
13304 | /* Store the location of the operator, for better error report. The | |
13305 | string of the operator will be rebuild based on the OP value. */ | |
13306 | EXPR_WFL_LINECOL (unaryop) = op_location; | |
13307 | return unaryop; | |
13308 | } | |
13309 | ||
13310 | /* Special case for the ++/-- operators, since they require an extra | |
13311 | argument to build, which is set to NULL and patched | |
13312 | later. IS_POST_P is 1 if the operator, 0 otherwise. */ | |
13313 | ||
13314 | static tree | |
13315 | build_incdec (op_token, op_location, op1, is_post_p) | |
13316 | int op_token, op_location; | |
13317 | tree op1; | |
13318 | int is_post_p; | |
13319 | { | |
13320 | static enum tree_code lookup [2][2] = | |
13321 | { | |
13322 | { PREDECREMENT_EXPR, PREINCREMENT_EXPR, }, | |
13323 | { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, }, | |
13324 | }; | |
13325 | tree node = build (lookup [is_post_p][(op_token - DECR_TK)], | |
13326 | NULL_TREE, op1, NULL_TREE); | |
13327 | TREE_SIDE_EFFECTS (node) = 1; | |
13328 | /* Store the location of the operator, for better error report. The | |
13329 | string of the operator will be rebuild based on the OP value. */ | |
13330 | EXPR_WFL_LINECOL (node) = op_location; | |
13331 | return node; | |
13332 | } | |
13333 | ||
13334 | /* Build an incomplete cast operator, based on the use of the | |
13335 | CONVERT_EXPR. Note that TREE_TYPE of the constructed node is | |
13336 | set. java_complete_tree is trained to walk a CONVERT_EXPR even | |
13337 | though its type is already set. */ | |
13338 | ||
13339 | static tree | |
13340 | build_cast (location, type, exp) | |
13341 | int location; | |
13342 | tree type, exp; | |
13343 | { | |
13344 | tree node = build1 (CONVERT_EXPR, type, exp); | |
13345 | EXPR_WFL_LINECOL (node) = location; | |
13346 | return node; | |
13347 | } | |
13348 | ||
c2952b01 APB |
13349 | /* Build an incomplete class reference operator. */ |
13350 | static tree | |
13351 | build_incomplete_class_ref (location, class_name) | |
13352 | int location; | |
13353 | tree class_name; | |
13354 | { | |
13355 | tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name); | |
13356 | EXPR_WFL_LINECOL (node) = location; | |
13357 | return node; | |
13358 | } | |
13359 | ||
13360 | /* Complete an incomplete class reference operator. */ | |
13361 | static tree | |
13362 | patch_incomplete_class_ref (node) | |
13363 | tree node; | |
13364 | { | |
13365 | tree type = TREE_OPERAND (node, 0); | |
13366 | tree ref_type; | |
13367 | ||
13368 | if (!(ref_type = resolve_type_during_patch (type))) | |
13369 | return error_mark_node; | |
13370 | ||
165f37bc | 13371 | if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type)) |
f1ff439a TT |
13372 | { |
13373 | /* A class referenced by `foo.class' is initialized. */ | |
13374 | return build_class_init (ref_type, build_class_ref (ref_type)); | |
13375 | } | |
165f37bc APB |
13376 | |
13377 | /* If we're emitting class files and we have to deal with non | |
13378 | primitive types, we invoke (and consider generating) the | |
13379 | synthetic static method `class$'. */ | |
13380 | if (!TYPE_DOT_CLASS (current_class)) | |
13381 | build_dot_class_method (current_class); | |
f0f3a777 | 13382 | ref_type = build_dot_class_method_invocation (ref_type); |
165f37bc | 13383 | return java_complete_tree (ref_type); |
c2952b01 APB |
13384 | } |
13385 | ||
e04a16fb AG |
13386 | /* 15.14 Unary operators. We return error_mark_node in case of error, |
13387 | but preserve the type of NODE if the type is fixed. */ | |
13388 | ||
13389 | static tree | |
13390 | patch_unaryop (node, wfl_op) | |
13391 | tree node; | |
13392 | tree wfl_op; | |
13393 | { | |
13394 | tree op = TREE_OPERAND (node, 0); | |
13395 | tree op_type = TREE_TYPE (op); | |
ab3a6dd6 | 13396 | tree prom_type = NULL_TREE, value, decl; |
c2952b01 | 13397 | int outer_field_flag = 0; |
e04a16fb AG |
13398 | int code = TREE_CODE (node); |
13399 | int error_found = 0; | |
13400 | ||
13401 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
13402 | ||
13403 | switch (code) | |
13404 | { | |
13405 | /* 15.13.2 Postfix Increment Operator ++ */ | |
13406 | case POSTINCREMENT_EXPR: | |
13407 | /* 15.13.3 Postfix Increment Operator -- */ | |
13408 | case POSTDECREMENT_EXPR: | |
13409 | /* 15.14.1 Prefix Increment Operator ++ */ | |
13410 | case PREINCREMENT_EXPR: | |
13411 | /* 15.14.2 Prefix Decrement Operator -- */ | |
13412 | case PREDECREMENT_EXPR: | |
5cbdba64 | 13413 | op = decl = strip_out_static_field_access_decl (op); |
c2952b01 APB |
13414 | outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL); |
13415 | /* We might be trying to change an outer field accessed using | |
13416 | access method. */ | |
13417 | if (outer_field_flag) | |
13418 | { | |
13419 | /* Retrieve the decl of the field we're trying to access. We | |
13420 | do that by first retrieving the function we would call to | |
13421 | access the field. It has been already verified that this | |
13422 | field isn't final */ | |
13423 | if (flag_emit_class_files) | |
13424 | decl = TREE_OPERAND (op, 0); | |
13425 | else | |
13426 | decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0); | |
13427 | decl = DECL_FUNCTION_ACCESS_DECL (decl); | |
13428 | } | |
b3edebcf | 13429 | /* We really should have a JAVA_ARRAY_EXPR to avoid this */ |
c2952b01 | 13430 | else if (!JDECL_P (decl) |
b3edebcf APB |
13431 | && TREE_CODE (decl) != COMPONENT_REF |
13432 | && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF) | |
13433 | && TREE_CODE (decl) != INDIRECT_REF | |
13434 | && !(TREE_CODE (decl) == COMPOUND_EXPR | |
13435 | && TREE_OPERAND (decl, 1) | |
13436 | && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF))) | |
e04a16fb | 13437 | { |
5e942c50 APB |
13438 | tree lvalue; |
13439 | /* Before screaming, check that we're not in fact trying to | |
13440 | increment a optimized static final access, in which case | |
13441 | we issue an different error message. */ | |
13442 | if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION | |
13443 | && resolve_expression_name (wfl_op, &lvalue) | |
13444 | && check_final_assignment (lvalue, wfl_op))) | |
13445 | parse_error_context (wfl_operator, "Invalid argument to `%s'", | |
13446 | operator_string (node)); | |
e04a16fb AG |
13447 | TREE_TYPE (node) = error_mark_node; |
13448 | error_found = 1; | |
13449 | } | |
c2952b01 APB |
13450 | |
13451 | if (check_final_assignment (op, wfl_op)) | |
5e942c50 APB |
13452 | error_found = 1; |
13453 | ||
e04a16fb AG |
13454 | /* From now on, we know that op if a variable and that it has a |
13455 | valid wfl. We use wfl_op to locate errors related to the | |
13456 | ++/-- operand. */ | |
13457 | else if (!JNUMERIC_TYPE_P (op_type)) | |
13458 | { | |
13459 | parse_error_context | |
13460 | (wfl_op, "Invalid argument type `%s' to `%s'", | |
0a2138e2 | 13461 | lang_printable_name (op_type, 0), operator_string (node)); |
e04a16fb AG |
13462 | TREE_TYPE (node) = error_mark_node; |
13463 | error_found = 1; | |
13464 | } | |
13465 | else | |
13466 | { | |
4a5f66c3 | 13467 | /* Before the addition, binary numeric promotion is performed on |
5cbdba64 APB |
13468 | both operands, if really necessary */ |
13469 | if (JINTEGRAL_TYPE_P (op_type)) | |
13470 | { | |
13471 | value = build_int_2 (1, 0); | |
13472 | TREE_TYPE (value) = TREE_TYPE (node) = op_type; | |
13473 | } | |
13474 | else | |
13475 | { | |
13476 | value = build_int_2 (1, 0); | |
13477 | TREE_TYPE (node) = | |
13478 | binary_numeric_promotion (op_type, | |
13479 | TREE_TYPE (value), &op, &value); | |
13480 | } | |
c2952b01 APB |
13481 | |
13482 | /* We remember we might be accessing an outer field */ | |
13483 | if (outer_field_flag) | |
13484 | { | |
13485 | /* We re-generate an access to the field */ | |
13486 | value = build (PLUS_EXPR, TREE_TYPE (op), | |
13487 | build_outer_field_access (wfl_op, decl), value); | |
13488 | ||
13489 | /* And we patch the original access$() into a write | |
13490 | with plus_op as a rhs */ | |
13491 | return outer_field_access_fix (node, op, value); | |
13492 | } | |
13493 | ||
5cbdba64 | 13494 | /* And write back into the node. */ |
4a5f66c3 | 13495 | TREE_OPERAND (node, 0) = op; |
e04a16fb | 13496 | TREE_OPERAND (node, 1) = value; |
5cbdba64 APB |
13497 | /* Convert the overall back into its original type, if |
13498 | necessary, and return */ | |
13499 | if (JINTEGRAL_TYPE_P (op_type)) | |
13500 | return fold (node); | |
13501 | else | |
13502 | return fold (convert (op_type, node)); | |
e04a16fb AG |
13503 | } |
13504 | break; | |
13505 | ||
13506 | /* 15.14.3 Unary Plus Operator + */ | |
b67d701b | 13507 | case UNARY_PLUS_EXPR: |
e04a16fb AG |
13508 | /* 15.14.4 Unary Minus Operator - */ |
13509 | case NEGATE_EXPR: | |
13510 | if (!JNUMERIC_TYPE_P (op_type)) | |
13511 | { | |
13512 | ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type); | |
13513 | TREE_TYPE (node) = error_mark_node; | |
13514 | error_found = 1; | |
13515 | } | |
13516 | /* Unary numeric promotion is performed on operand */ | |
13517 | else | |
13518 | { | |
15fdcfe9 PB |
13519 | op = do_unary_numeric_promotion (op); |
13520 | prom_type = TREE_TYPE (op); | |
b67d701b | 13521 | if (code == UNARY_PLUS_EXPR) |
4a5f66c3 | 13522 | return fold (op); |
e04a16fb AG |
13523 | } |
13524 | break; | |
13525 | ||
13526 | /* 15.14.5 Bitwise Complement Operator ~ */ | |
13527 | case BIT_NOT_EXPR: | |
13528 | if (!JINTEGRAL_TYPE_P (op_type)) | |
13529 | { | |
13530 | ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type); | |
13531 | TREE_TYPE (node) = error_mark_node; | |
13532 | error_found = 1; | |
13533 | } | |
13534 | else | |
13535 | { | |
15fdcfe9 PB |
13536 | op = do_unary_numeric_promotion (op); |
13537 | prom_type = TREE_TYPE (op); | |
e04a16fb AG |
13538 | } |
13539 | break; | |
13540 | ||
13541 | /* 15.14.6 Logical Complement Operator ! */ | |
13542 | case TRUTH_NOT_EXPR: | |
13543 | if (TREE_CODE (op_type) != BOOLEAN_TYPE) | |
13544 | { | |
13545 | ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type); | |
c877974e APB |
13546 | /* But the type is known. We will report an error if further |
13547 | attempt of a assignment is made with this rhs */ | |
e04a16fb AG |
13548 | TREE_TYPE (node) = boolean_type_node; |
13549 | error_found = 1; | |
13550 | } | |
13551 | else | |
13552 | prom_type = boolean_type_node; | |
13553 | break; | |
13554 | ||
13555 | /* 15.15 Cast Expression */ | |
13556 | case CONVERT_EXPR: | |
0a2138e2 | 13557 | value = patch_cast (node, wfl_operator); |
e04a16fb | 13558 | if (value == error_mark_node) |
c877974e APB |
13559 | { |
13560 | /* If this cast is part of an assignment, we tell the code | |
13561 | that deals with it not to complain about a mismatch, | |
13562 | because things have been cast, anyways */ | |
13563 | TREE_TYPE (node) = error_mark_node; | |
13564 | error_found = 1; | |
13565 | } | |
13566 | else | |
dc0b3eff PB |
13567 | { |
13568 | value = fold (value); | |
13569 | TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op); | |
13570 | return value; | |
13571 | } | |
e04a16fb AG |
13572 | break; |
13573 | } | |
13574 | ||
e04a16fb AG |
13575 | if (error_found) |
13576 | return error_mark_node; | |
4a5f66c3 APB |
13577 | |
13578 | /* There are cases where node has been replaced by something else | |
13579 | and we don't end up returning here: UNARY_PLUS_EXPR, | |
13580 | CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */ | |
7525cc04 | 13581 | TREE_OPERAND (node, 0) = fold (op); |
4a5f66c3 | 13582 | TREE_TYPE (node) = prom_type; |
dc0b3eff | 13583 | TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op); |
e04a16fb AG |
13584 | return fold (node); |
13585 | } | |
13586 | ||
13587 | /* Generic type resolution that sometimes takes place during node | |
13588 | patching. Returned the resolved type or generate an error | |
13589 | message. Return the resolved type or NULL_TREE. */ | |
13590 | ||
13591 | static tree | |
13592 | resolve_type_during_patch (type) | |
13593 | tree type; | |
13594 | { | |
13595 | if (unresolved_type_p (type, NULL)) | |
13596 | { | |
4142b247 | 13597 | tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type); |
e04a16fb AG |
13598 | if (!type_decl) |
13599 | { | |
13600 | parse_error_context (type, | |
13601 | "Class `%s' not found in type declaration", | |
13602 | IDENTIFIER_POINTER (EXPR_WFL_NODE (type))); | |
13603 | return NULL_TREE; | |
13604 | } | |
13605 | else | |
5e942c50 APB |
13606 | { |
13607 | CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1; | |
13608 | return TREE_TYPE (type_decl); | |
13609 | } | |
e04a16fb AG |
13610 | } |
13611 | return type; | |
13612 | } | |
13613 | /* 5.5 Casting Conversion. error_mark_node is returned if an error is | |
13614 | found. Otherwise NODE or something meant to replace it is returned. */ | |
13615 | ||
13616 | static tree | |
19e223db | 13617 | patch_cast (node, wfl_op) |
e04a16fb | 13618 | tree node; |
19e223db | 13619 | tree wfl_op; |
e04a16fb AG |
13620 | { |
13621 | tree op = TREE_OPERAND (node, 0); | |
13622 | tree op_type = TREE_TYPE (op); | |
13623 | tree cast_type = TREE_TYPE (node); | |
13624 | char *t1; | |
13625 | ||
13626 | /* First resolve OP_TYPE if unresolved */ | |
13627 | if (!(cast_type = resolve_type_during_patch (cast_type))) | |
13628 | return error_mark_node; | |
13629 | ||
13630 | /* Check on cast that are proven correct at compile time */ | |
13631 | if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type)) | |
13632 | { | |
e04a16fb AG |
13633 | /* Same type */ |
13634 | if (cast_type == op_type) | |
13635 | return node; | |
13636 | ||
0b4d333e APB |
13637 | /* float and double type are converted to the original type main |
13638 | variant and then to the target type. */ | |
13639 | if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE) | |
13640 | op = convert (integer_type_node, op); | |
13641 | ||
e04a16fb AG |
13642 | /* Try widening/narowwing convertion. Potentially, things need |
13643 | to be worked out in gcc so we implement the extreme cases | |
13644 | correctly. fold_convert() needs to be fixed. */ | |
13645 | return convert (cast_type, op); | |
13646 | } | |
13647 | ||
0b4d333e APB |
13648 | /* It's also valid to cast a boolean into a boolean */ |
13649 | if (op_type == boolean_type_node && cast_type == boolean_type_node) | |
13650 | return node; | |
13651 | ||
5e942c50 APB |
13652 | /* null can be casted to references */ |
13653 | if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type)) | |
13654 | return build_null_of_type (cast_type); | |
13655 | ||
e04a16fb AG |
13656 | /* The remaining legal casts involve conversion between reference |
13657 | types. Check for their compile time correctness. */ | |
13658 | if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type) | |
09ed0f70 | 13659 | && valid_ref_assignconv_cast_p (op_type, cast_type, 1)) |
e04a16fb AG |
13660 | { |
13661 | TREE_TYPE (node) = promote_type (cast_type); | |
13662 | /* Now, the case can be determined correct at compile time if | |
13663 | OP_TYPE can be converted into CAST_TYPE by assignment | |
13664 | conversion (5.2) */ | |
13665 | ||
13666 | if (valid_ref_assignconv_cast_p (op_type, cast_type, 0)) | |
15fdcfe9 PB |
13667 | { |
13668 | TREE_SET_CODE (node, NOP_EXPR); | |
13669 | return node; | |
13670 | } | |
13671 | ||
13672 | if (flag_emit_class_files) | |
13673 | { | |
13674 | TREE_SET_CODE (node, CONVERT_EXPR); | |
13675 | return node; | |
13676 | } | |
e04a16fb AG |
13677 | |
13678 | /* The cast requires a run-time check */ | |
13679 | return build (CALL_EXPR, promote_type (cast_type), | |
13680 | build_address_of (soft_checkcast_node), | |
13681 | tree_cons (NULL_TREE, build_class_ref (cast_type), | |
13682 | build_tree_list (NULL_TREE, op)), | |
13683 | NULL_TREE); | |
13684 | } | |
13685 | ||
13686 | /* Any other casts are proven incorrect at compile time */ | |
c2e3db92 | 13687 | t1 = xstrdup (lang_printable_name (op_type, 0)); |
19e223db | 13688 | parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'", |
0a2138e2 | 13689 | t1, lang_printable_name (cast_type, 0)); |
e04a16fb AG |
13690 | free (t1); |
13691 | return error_mark_node; | |
13692 | } | |
13693 | ||
5e942c50 APB |
13694 | /* Build a null constant and give it the type TYPE. */ |
13695 | ||
13696 | static tree | |
13697 | build_null_of_type (type) | |
13698 | tree type; | |
13699 | { | |
13700 | tree node = build_int_2 (0, 0); | |
13701 | TREE_TYPE (node) = promote_type (type); | |
13702 | return node; | |
13703 | } | |
13704 | ||
e04a16fb AG |
13705 | /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't |
13706 | a list of indices. */ | |
13707 | static tree | |
13708 | build_array_ref (location, array, index) | |
13709 | int location; | |
13710 | tree array, index; | |
13711 | { | |
13712 | tree node = build (ARRAY_REF, NULL_TREE, array, index); | |
13713 | EXPR_WFL_LINECOL (node) = location; | |
13714 | return node; | |
13715 | } | |
13716 | ||
13717 | /* 15.12 Array Access Expression */ | |
13718 | ||
13719 | static tree | |
c877974e APB |
13720 | patch_array_ref (node) |
13721 | tree node; | |
e04a16fb AG |
13722 | { |
13723 | tree array = TREE_OPERAND (node, 0); | |
13724 | tree array_type = TREE_TYPE (array); | |
13725 | tree index = TREE_OPERAND (node, 1); | |
13726 | tree index_type = TREE_TYPE (index); | |
e04a16fb AG |
13727 | int error_found = 0; |
13728 | ||
13729 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
13730 | ||
e04a16fb AG |
13731 | if (TREE_CODE (array_type) == POINTER_TYPE) |
13732 | array_type = TREE_TYPE (array_type); | |
13733 | ||
13734 | /* The array reference must be an array */ | |
13735 | if (!TYPE_ARRAY_P (array_type)) | |
13736 | { | |
13737 | parse_error_context | |
781b0558 KG |
13738 | (wfl_operator, |
13739 | "`[]' can only be applied to arrays. It can't be applied to `%s'", | |
13740 | lang_printable_name (array_type, 0)); | |
e04a16fb AG |
13741 | TREE_TYPE (node) = error_mark_node; |
13742 | error_found = 1; | |
13743 | } | |
13744 | ||
c2952b01 | 13745 | /* The array index undergoes unary numeric promotion. The promoted |
e04a16fb | 13746 | type must be int */ |
15fdcfe9 PB |
13747 | index = do_unary_numeric_promotion (index); |
13748 | if (TREE_TYPE (index) != int_type_node) | |
e04a16fb | 13749 | { |
1ebadc60 | 13750 | if (valid_cast_to_p (index_type, int_type_node)) |
781b0558 KG |
13751 | parse_error_context (wfl_operator, |
13752 | "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'", | |
1ebadc60 KG |
13753 | lang_printable_name (index_type, 0)); |
13754 | else | |
781b0558 KG |
13755 | parse_error_context (wfl_operator, |
13756 | "Incompatible type for `[]'. Can't convert `%s' to `int'", | |
1ebadc60 | 13757 | lang_printable_name (index_type, 0)); |
e04a16fb AG |
13758 | TREE_TYPE (node) = error_mark_node; |
13759 | error_found = 1; | |
13760 | } | |
13761 | ||
e04a16fb AG |
13762 | if (error_found) |
13763 | return error_mark_node; | |
e04a16fb | 13764 | |
5e942c50 | 13765 | array_type = TYPE_ARRAY_ELEMENT (array_type); |
5e942c50 | 13766 | |
7f1d4866 | 13767 | if (flag_emit_class_files || flag_emit_xref) |
e04a16fb | 13768 | { |
15fdcfe9 PB |
13769 | TREE_OPERAND (node, 0) = array; |
13770 | TREE_OPERAND (node, 1) = index; | |
e04a16fb AG |
13771 | } |
13772 | else | |
939d7216 PB |
13773 | { |
13774 | /* The save_expr is for correct evaluation order. It would be cleaner | |
13775 | to use force_evaluation_order (see comment there), but that is | |
13776 | difficult when we also have to deal with bounds checking. */ | |
13777 | if (TREE_SIDE_EFFECTS (index)) | |
13778 | array = save_expr (array); | |
13779 | node = build_java_arrayaccess (array, array_type, index); | |
13780 | if (TREE_SIDE_EFFECTS (index)) | |
13781 | node = build (COMPOUND_EXPR, array_type, array, node); | |
13782 | } | |
e04a16fb AG |
13783 | TREE_TYPE (node) = array_type; |
13784 | return node; | |
13785 | } | |
13786 | ||
13787 | /* 15.9 Array Creation Expressions */ | |
13788 | ||
13789 | static tree | |
13790 | build_newarray_node (type, dims, extra_dims) | |
13791 | tree type; | |
13792 | tree dims; | |
13793 | int extra_dims; | |
13794 | { | |
13795 | tree node = | |
b67d701b | 13796 | build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims), |
e04a16fb | 13797 | build_int_2 (extra_dims, 0)); |
e04a16fb AG |
13798 | return node; |
13799 | } | |
13800 | ||
13801 | static tree | |
13802 | patch_newarray (node) | |
13803 | tree node; | |
13804 | { | |
13805 | tree type = TREE_OPERAND (node, 0); | |
13806 | tree dims = TREE_OPERAND (node, 1); | |
13807 | tree cdim, array_type; | |
13808 | int error_found = 0; | |
13809 | int ndims = 0; | |
13810 | int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2)); | |
e04a16fb AG |
13811 | |
13812 | /* Dimension types are verified. It's better for the types to be | |
13813 | verified in order. */ | |
13814 | for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ ) | |
13815 | { | |
13816 | int dim_error = 0; | |
13817 | tree dim = TREE_VALUE (cdim); | |
13818 | ||
13819 | /* Dim might have been saved during its evaluation */ | |
dba41d30 | 13820 | dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim); |
e04a16fb AG |
13821 | |
13822 | /* The type of each specified dimension must be an integral type. */ | |
13823 | if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim))) | |
13824 | dim_error = 1; | |
13825 | ||
13826 | /* Each expression undergoes an unary numeric promotion (5.6.1) and the | |
13827 | promoted type must be int. */ | |
13828 | else | |
13829 | { | |
15fdcfe9 | 13830 | dim = do_unary_numeric_promotion (dim); |
e04a16fb AG |
13831 | if (TREE_TYPE (dim) != int_type_node) |
13832 | dim_error = 1; | |
13833 | } | |
13834 | ||
13835 | /* Report errors on types here */ | |
13836 | if (dim_error) | |
13837 | { | |
13838 | parse_error_context | |
13839 | (TREE_PURPOSE (cdim), | |
781b0558 | 13840 | "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'", |
b67d701b | 13841 | (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ? |
e04a16fb | 13842 | "Explicit cast needed to" : "Can't"), |
0a2138e2 | 13843 | lang_printable_name (TREE_TYPE (dim), 0)); |
e04a16fb AG |
13844 | error_found = 1; |
13845 | } | |
13846 | ||
e04a16fb AG |
13847 | TREE_PURPOSE (cdim) = NULL_TREE; |
13848 | } | |
13849 | ||
13850 | /* Resolve array base type if unresolved */ | |
13851 | if (!(type = resolve_type_during_patch (type))) | |
13852 | error_found = 1; | |
13853 | ||
13854 | if (error_found) | |
13855 | { | |
13856 | /* We don't want further evaluation of this bogus array creation | |
13857 | operation */ | |
13858 | TREE_TYPE (node) = error_mark_node; | |
13859 | return error_mark_node; | |
13860 | } | |
13861 | ||
15fdcfe9 PB |
13862 | /* Set array_type to the actual (promoted) array type of the result. */ |
13863 | if (TREE_CODE (type) == RECORD_TYPE) | |
13864 | type = build_pointer_type (type); | |
13865 | while (--xdims >= 0) | |
13866 | { | |
13867 | type = promote_type (build_java_array_type (type, -1)); | |
13868 | } | |
13869 | dims = nreverse (dims); | |
13870 | array_type = type; | |
13871 | for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim)) | |
13872 | { | |
13873 | type = array_type; | |
05bccae2 RK |
13874 | array_type |
13875 | = build_java_array_type (type, | |
13876 | TREE_CODE (cdim) == INTEGER_CST | |
13877 | ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim) | |
13878 | : -1); | |
15fdcfe9 PB |
13879 | array_type = promote_type (array_type); |
13880 | } | |
13881 | dims = nreverse (dims); | |
13882 | ||
e04a16fb AG |
13883 | /* The node is transformed into a function call. Things are done |
13884 | differently according to the number of dimensions. If the number | |
13885 | of dimension is equal to 1, then the nature of the base type | |
13886 | (primitive or not) matters. */ | |
15fdcfe9 | 13887 | if (ndims == 1) |
fdec99c6 | 13888 | return build_new_array (type, TREE_VALUE (dims)); |
e04a16fb | 13889 | |
e04a16fb AG |
13890 | /* Can't reuse what's already written in expr.c because it uses the |
13891 | JVM stack representation. Provide a build_multianewarray. FIXME */ | |
15fdcfe9 | 13892 | return build (CALL_EXPR, array_type, |
e04a16fb | 13893 | build_address_of (soft_multianewarray_node), |
15fdcfe9 | 13894 | tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)), |
e04a16fb | 13895 | tree_cons (NULL_TREE, |
15fdcfe9 | 13896 | build_int_2 (ndims, 0), dims )), |
e04a16fb AG |
13897 | NULL_TREE); |
13898 | } | |
13899 | ||
f8976021 APB |
13900 | /* 10.6 Array initializer. */ |
13901 | ||
13902 | /* Build a wfl for array element that don't have one, so we can | |
13903 | pin-point errors. */ | |
13904 | ||
13905 | static tree | |
13906 | maybe_build_array_element_wfl (node) | |
13907 | tree node; | |
13908 | { | |
13909 | if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION) | |
13910 | return build_expr_wfl (NULL_TREE, ctxp->filename, | |
13911 | ctxp->elc.line, ctxp->elc.prev_col); | |
13912 | else | |
13913 | return NULL_TREE; | |
13914 | } | |
13915 | ||
13916 | /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes | |
13917 | identification of initialized arrays easier to detect during walk | |
13918 | and expansion. */ | |
13919 | ||
13920 | static tree | |
13921 | build_new_array_init (location, values) | |
13922 | int location; | |
13923 | tree values; | |
13924 | { | |
13925 | tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values); | |
13926 | tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor); | |
5bba4807 | 13927 | EXPR_WFL_LINECOL (to_return) = location; |
f8976021 APB |
13928 | return to_return; |
13929 | } | |
13930 | ||
13931 | /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error | |
13932 | occurred. Otherwise return NODE after having set its type | |
13933 | appropriately. */ | |
13934 | ||
13935 | static tree | |
13936 | patch_new_array_init (type, node) | |
13937 | tree type, node; | |
f8976021 APB |
13938 | { |
13939 | int error_seen = 0; | |
fdec99c6 | 13940 | tree current, element_type; |
f8976021 | 13941 | HOST_WIDE_INT length; |
fdec99c6 PB |
13942 | int all_constant = 1; |
13943 | tree init = TREE_OPERAND (node, 0); | |
f8976021 | 13944 | |
fdec99c6 PB |
13945 | if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type))) |
13946 | { | |
13947 | parse_error_context (node, | |
13948 | "Invalid array initializer for non-array type `%s'", | |
13949 | lang_printable_name (type, 1)); | |
13950 | return error_mark_node; | |
13951 | } | |
13952 | type = TREE_TYPE (type); | |
13953 | element_type = TYPE_ARRAY_ELEMENT (type); | |
f8976021 | 13954 | |
fdec99c6 PB |
13955 | CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init)); |
13956 | ||
13957 | for (length = 0, current = CONSTRUCTOR_ELTS (init); | |
13958 | current; length++, current = TREE_CHAIN (current)) | |
f8976021 | 13959 | { |
fdec99c6 PB |
13960 | tree elt = TREE_VALUE (current); |
13961 | if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT) | |
f8976021 | 13962 | { |
fdec99c6 | 13963 | error_seen |= array_constructor_check_entry (element_type, current); |
5bba4807 PB |
13964 | elt = TREE_VALUE (current); |
13965 | /* When compiling to native code, STRING_CST is converted to | |
13966 | INDIRECT_REF, but still with a TREE_CONSTANT flag. */ | |
13967 | if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF) | |
fdec99c6 | 13968 | all_constant = 0; |
f8976021 | 13969 | } |
fdec99c6 PB |
13970 | else |
13971 | { | |
13972 | TREE_VALUE (current) = patch_new_array_init (element_type, elt); | |
13973 | TREE_PURPOSE (current) = NULL_TREE; | |
13974 | all_constant = 0; | |
13975 | } | |
9a7ab4b3 APB |
13976 | if (elt && TREE_CODE (elt) == TREE_LIST |
13977 | && TREE_VALUE (elt) == error_mark_node) | |
fdec99c6 | 13978 | error_seen = 1; |
f8976021 APB |
13979 | } |
13980 | ||
13981 | if (error_seen) | |
13982 | return error_mark_node; | |
13983 | ||
13984 | /* Create a new type. We can't reuse the one we have here by | |
13985 | patching its dimension because it originally is of dimension -1 | |
13986 | hence reused by gcc. This would prevent triangular arrays. */ | |
fdec99c6 PB |
13987 | type = build_java_array_type (element_type, length); |
13988 | TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type)))); | |
13989 | TREE_TYPE (node) = promote_type (type); | |
13990 | TREE_CONSTANT (init) = all_constant; | |
bc3ca41b | 13991 | TREE_CONSTANT (node) = all_constant; |
f8976021 APB |
13992 | return node; |
13993 | } | |
13994 | ||
13995 | /* Verify that one entry of the initializer element list can be | |
13996 | assigned to the array base type. Report 1 if an error occurred, 0 | |
13997 | otherwise. */ | |
13998 | ||
13999 | static int | |
14000 | array_constructor_check_entry (type, entry) | |
14001 | tree type, entry; | |
14002 | { | |
14003 | char *array_type_string = NULL; /* For error reports */ | |
14004 | tree value, type_value, new_value, wfl_value, patched; | |
14005 | int error_seen = 0; | |
14006 | ||
14007 | new_value = NULL_TREE; | |
14008 | wfl_value = TREE_VALUE (entry); | |
14009 | ||
f8976021 | 14010 | value = java_complete_tree (TREE_VALUE (entry)); |
1179ebc2 | 14011 | /* patch_string return error_mark_node if arg is error_mark_node */ |
f8976021 APB |
14012 | if ((patched = patch_string (value))) |
14013 | value = patched; | |
1179ebc2 APB |
14014 | if (value == error_mark_node) |
14015 | return 1; | |
f8976021 | 14016 | |
f8976021 APB |
14017 | type_value = TREE_TYPE (value); |
14018 | ||
1179ebc2 | 14019 | /* At anytime, try_builtin_assignconv can report a warning on |
f8976021 APB |
14020 | constant overflow during narrowing. */ |
14021 | SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value); | |
14022 | new_value = try_builtin_assignconv (wfl_operator, type, value); | |
14023 | if (!new_value && (new_value = try_reference_assignconv (type, value))) | |
14024 | type_value = promote_type (type); | |
100f7cd8 | 14025 | |
f8976021 APB |
14026 | /* Check and report errors */ |
14027 | if (!new_value) | |
14028 | { | |
49f48c71 | 14029 | const char *msg = (!valid_cast_to_p (type_value, type) ? |
f8976021 APB |
14030 | "Can't" : "Explicit cast needed to"); |
14031 | if (!array_type_string) | |
c2e3db92 | 14032 | array_type_string = xstrdup (lang_printable_name (type, 1)); |
f8976021 APB |
14033 | parse_error_context |
14034 | (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'", | |
14035 | msg, lang_printable_name (type_value, 1), array_type_string); | |
14036 | error_seen = 1; | |
14037 | } | |
14038 | ||
14039 | if (new_value) | |
14040 | { | |
b8c5b1c6 | 14041 | new_value = maybe_build_primttype_type_ref (new_value, wfl_value); |
f8976021 APB |
14042 | TREE_VALUE (entry) = new_value; |
14043 | } | |
14044 | ||
14045 | if (array_type_string) | |
14046 | free (array_type_string); | |
14047 | ||
14048 | TREE_PURPOSE (entry) = NULL_TREE; | |
14049 | return error_seen; | |
14050 | } | |
14051 | ||
e04a16fb AG |
14052 | static tree |
14053 | build_this (location) | |
14054 | int location; | |
14055 | { | |
9ee9b555 | 14056 | tree node = build_wfl_node (this_identifier_node); |
b67d701b | 14057 | TREE_SET_CODE (node, THIS_EXPR); |
e04a16fb AG |
14058 | EXPR_WFL_LINECOL (node) = location; |
14059 | return node; | |
14060 | } | |
14061 | ||
14062 | /* 14.15 The return statement. It builds a modify expression that | |
14063 | assigns the returned value to the RESULT_DECL that hold the value | |
14064 | to be returned. */ | |
14065 | ||
14066 | static tree | |
14067 | build_return (location, op) | |
14068 | int location; | |
14069 | tree op; | |
14070 | { | |
14071 | tree node = build1 (RETURN_EXPR, NULL_TREE, op); | |
14072 | EXPR_WFL_LINECOL (node) = location; | |
b67d701b | 14073 | node = build_debugable_stmt (location, node); |
e04a16fb AG |
14074 | return node; |
14075 | } | |
14076 | ||
14077 | static tree | |
14078 | patch_return (node) | |
14079 | tree node; | |
14080 | { | |
14081 | tree return_exp = TREE_OPERAND (node, 0); | |
14082 | tree meth = current_function_decl; | |
14083 | tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl)); | |
e04a16fb AG |
14084 | int error_found = 0; |
14085 | ||
14086 | TREE_TYPE (node) = error_mark_node; | |
14087 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
14088 | ||
14089 | /* It's invalid to have a return value within a function that is | |
14090 | declared with the keyword void or that is a constructor */ | |
14091 | if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth))) | |
14092 | error_found = 1; | |
14093 | ||
f099f336 | 14094 | /* It's invalid to use a return statement in a static block */ |
c2952b01 | 14095 | if (DECL_CLINIT_P (current_function_decl)) |
f099f336 APB |
14096 | error_found = 1; |
14097 | ||
e04a16fb AG |
14098 | /* It's invalid to have a no return value within a function that |
14099 | isn't declared with the keyword `void' */ | |
14100 | if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth))) | |
14101 | error_found = 2; | |
c2952b01 APB |
14102 | |
14103 | if (in_instance_initializer) | |
14104 | error_found = 1; | |
e04a16fb AG |
14105 | |
14106 | if (error_found) | |
14107 | { | |
c2952b01 | 14108 | if (in_instance_initializer) |
f099f336 | 14109 | parse_error_context (wfl_operator, |
c2952b01 APB |
14110 | "`return' inside instance initializer"); |
14111 | ||
14112 | else if (DECL_CLINIT_P (current_function_decl)) | |
14113 | parse_error_context (wfl_operator, | |
14114 | "`return' inside static initializer"); | |
f099f336 APB |
14115 | |
14116 | else if (!DECL_CONSTRUCTOR_P (meth)) | |
22eed1e6 | 14117 | { |
c2e3db92 | 14118 | char *t = xstrdup (lang_printable_name (mtype, 0)); |
22eed1e6 APB |
14119 | parse_error_context (wfl_operator, |
14120 | "`return' with%s value from `%s %s'", | |
14121 | (error_found == 1 ? "" : "out"), | |
14122 | t, lang_printable_name (meth, 0)); | |
14123 | free (t); | |
14124 | } | |
14125 | else | |
14126 | parse_error_context (wfl_operator, | |
14127 | "`return' with value from constructor `%s'", | |
14128 | lang_printable_name (meth, 0)); | |
e04a16fb AG |
14129 | return error_mark_node; |
14130 | } | |
14131 | ||
5e942c50 APB |
14132 | /* If we have a return_exp, build a modify expression and expand |
14133 | it. Note: at that point, the assignment is declared valid, but we | |
14134 | may want to carry some more hacks */ | |
e04a16fb AG |
14135 | if (return_exp) |
14136 | { | |
5e942c50 APB |
14137 | tree exp = java_complete_tree (return_exp); |
14138 | tree modify, patched; | |
14139 | ||
14140 | /* If the function returned value and EXP are booleans, EXP has | |
14141 | to be converted into the type of DECL_RESULT, which is integer | |
14142 | (see complete_start_java_method) */ | |
14143 | if (TREE_TYPE (exp) == boolean_type_node && | |
14144 | TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node) | |
14145 | exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp); | |
14146 | ||
14147 | /* `null' can be assigned to a function returning a reference */ | |
14148 | if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) && | |
14149 | exp == null_pointer_node) | |
14150 | exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth))); | |
14151 | ||
14152 | if ((patched = patch_string (exp))) | |
14153 | exp = patched; | |
14154 | ||
14155 | modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp); | |
e04a16fb AG |
14156 | EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node); |
14157 | modify = java_complete_tree (modify); | |
5e942c50 | 14158 | |
e04a16fb AG |
14159 | if (modify != error_mark_node) |
14160 | { | |
14161 | TREE_SIDE_EFFECTS (modify) = 1; | |
14162 | TREE_OPERAND (node, 0) = modify; | |
14163 | } | |
14164 | else | |
14165 | return error_mark_node; | |
14166 | } | |
14167 | TREE_TYPE (node) = void_type_node; | |
14168 | TREE_SIDE_EFFECTS (node) = 1; | |
14169 | return node; | |
14170 | } | |
14171 | ||
14172 | /* 14.8 The if Statement */ | |
14173 | ||
14174 | static tree | |
14175 | build_if_else_statement (location, expression, if_body, else_body) | |
14176 | int location; | |
14177 | tree expression, if_body, else_body; | |
14178 | { | |
14179 | tree node; | |
e04a16fb | 14180 | if (!else_body) |
9bbc7d9f | 14181 | else_body = empty_stmt_node; |
e04a16fb AG |
14182 | node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body); |
14183 | EXPR_WFL_LINECOL (node) = location; | |
b67d701b | 14184 | node = build_debugable_stmt (location, node); |
e04a16fb AG |
14185 | return node; |
14186 | } | |
14187 | ||
14188 | static tree | |
14189 | patch_if_else_statement (node) | |
14190 | tree node; | |
14191 | { | |
14192 | tree expression = TREE_OPERAND (node, 0); | |
14193 | ||
14194 | TREE_TYPE (node) = error_mark_node; | |
14195 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
14196 | ||
14197 | /* The type of expression must be boolean */ | |
b67d701b PB |
14198 | if (TREE_TYPE (expression) != boolean_type_node |
14199 | && TREE_TYPE (expression) != promoted_boolean_type_node) | |
e04a16fb AG |
14200 | { |
14201 | parse_error_context | |
14202 | (wfl_operator, | |
14203 | "Incompatible type for `if'. Can't convert `%s' to `boolean'", | |
0a2138e2 | 14204 | lang_printable_name (TREE_TYPE (expression), 0)); |
e04a16fb AG |
14205 | return error_mark_node; |
14206 | } | |
14207 | ||
14208 | TREE_TYPE (node) = void_type_node; | |
14209 | TREE_SIDE_EFFECTS (node) = 1; | |
15fdcfe9 | 14210 | CAN_COMPLETE_NORMALLY (node) |
9bbc7d9f PB |
14211 | = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)) |
14212 | | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)); | |
e04a16fb AG |
14213 | return node; |
14214 | } | |
14215 | ||
14216 | /* 14.6 Labeled Statements */ | |
14217 | ||
14218 | /* Action taken when a lableled statement is parsed. a new | |
14219 | LABELED_BLOCK_EXPR is created. No statement is attached to the | |
b635eb2f | 14220 | label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */ |
e04a16fb AG |
14221 | |
14222 | static tree | |
0a2138e2 | 14223 | build_labeled_block (location, label) |
e04a16fb | 14224 | int location; |
0a2138e2 | 14225 | tree label; |
e04a16fb | 14226 | { |
b635eb2f | 14227 | tree label_name ; |
e04a16fb | 14228 | tree label_decl, node; |
b635eb2f PB |
14229 | if (label == NULL_TREE || label == continue_identifier_node) |
14230 | label_name = label; | |
14231 | else | |
e04a16fb | 14232 | { |
b635eb2f PB |
14233 | label_name = merge_qualified_name (label_id, label); |
14234 | /* Issue an error if we try to reuse a label that was previously | |
14235 | declared */ | |
14236 | if (IDENTIFIER_LOCAL_VALUE (label_name)) | |
14237 | { | |
14238 | EXPR_WFL_LINECOL (wfl_operator) = location; | |
781b0558 KG |
14239 | parse_error_context (wfl_operator, |
14240 | "Declaration of `%s' shadows a previous label declaration", | |
b635eb2f PB |
14241 | IDENTIFIER_POINTER (label)); |
14242 | EXPR_WFL_LINECOL (wfl_operator) = | |
14243 | EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name)); | |
781b0558 KG |
14244 | parse_error_context (wfl_operator, |
14245 | "This is the location of the previous declaration of label `%s'", | |
b635eb2f PB |
14246 | IDENTIFIER_POINTER (label)); |
14247 | java_error_count--; | |
14248 | } | |
e04a16fb AG |
14249 | } |
14250 | ||
14251 | label_decl = create_label_decl (label_name); | |
14252 | node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE); | |
14253 | EXPR_WFL_LINECOL (node) = location; | |
14254 | TREE_SIDE_EFFECTS (node) = 1; | |
14255 | return node; | |
14256 | } | |
14257 | ||
b67d701b | 14258 | /* A labeled statement LBE is attached a statement. */ |
e04a16fb AG |
14259 | |
14260 | static tree | |
b635eb2f | 14261 | finish_labeled_statement (lbe, statement) |
e04a16fb AG |
14262 | tree lbe; /* Labeled block expr */ |
14263 | tree statement; | |
14264 | { | |
14265 | /* In anyways, tie the loop to its statement */ | |
14266 | LABELED_BLOCK_BODY (lbe) = statement; | |
b635eb2f PB |
14267 | pop_labeled_block (); |
14268 | POP_LABELED_BLOCK (); | |
e04a16fb AG |
14269 | return lbe; |
14270 | } | |
14271 | ||
14272 | /* 14.10, 14.11, 14.12 Loop Statements */ | |
14273 | ||
14274 | /* Create an empty LOOP_EXPR and make it the last in the nested loop | |
14275 | list. */ | |
14276 | ||
14277 | static tree | |
14278 | build_new_loop (loop_body) | |
14279 | tree loop_body; | |
14280 | { | |
14281 | tree loop = build (LOOP_EXPR, NULL_TREE, loop_body); | |
14282 | TREE_SIDE_EFFECTS (loop) = 1; | |
14283 | PUSH_LOOP (loop); | |
14284 | return loop; | |
14285 | } | |
14286 | ||
14287 | /* Create a loop body according to the following structure: | |
14288 | COMPOUND_EXPR | |
14289 | COMPOUND_EXPR (loop main body) | |
14290 | EXIT_EXPR (this order is for while/for loops. | |
14291 | LABELED_BLOCK_EXPR the order is reversed for do loops) | |
34f4db93 | 14292 | LABEL_DECL (a continue occuring here branches at the |
e04a16fb AG |
14293 | BODY end of this labeled block) |
14294 | INCREMENT (if any) | |
14295 | ||
14296 | REVERSED, if non zero, tells that the loop condition expr comes | |
b67d701b PB |
14297 | after the body, like in the do-while loop. |
14298 | ||
14299 | To obtain a loop, the loop body structure described above is | |
14300 | encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR: | |
14301 | ||
14302 | LABELED_BLOCK_EXPR | |
14303 | LABEL_DECL (use this label to exit the loop) | |
14304 | LOOP_EXPR | |
14305 | <structure described above> */ | |
e04a16fb AG |
14306 | |
14307 | static tree | |
14308 | build_loop_body (location, condition, reversed) | |
14309 | int location; | |
14310 | tree condition; | |
14311 | int reversed; | |
14312 | { | |
0a2138e2 | 14313 | tree first, second, body; |
e04a16fb AG |
14314 | |
14315 | condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */ | |
14316 | EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */ | |
14317 | condition = build_debugable_stmt (location, condition); | |
14318 | TREE_SIDE_EFFECTS (condition) = 1; | |
14319 | ||
b635eb2f | 14320 | body = build_labeled_block (0, continue_identifier_node); |
e04a16fb AG |
14321 | first = (reversed ? body : condition); |
14322 | second = (reversed ? condition : body); | |
14323 | return | |
14324 | build (COMPOUND_EXPR, NULL_TREE, | |
9bbc7d9f | 14325 | build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node); |
e04a16fb AG |
14326 | } |
14327 | ||
14328 | /* Install CONDITION (if any) and loop BODY (using REVERSED to tell | |
14329 | their order) on the current loop. Unlink the current loop from the | |
14330 | loop list. */ | |
14331 | ||
14332 | static tree | |
b635eb2f | 14333 | finish_loop_body (location, condition, body, reversed) |
e04a16fb AG |
14334 | int location; |
14335 | tree condition, body; | |
14336 | int reversed; | |
14337 | { | |
14338 | tree to_return = ctxp->current_loop; | |
14339 | tree loop_body = LOOP_EXPR_BODY (to_return); | |
14340 | if (condition) | |
14341 | { | |
14342 | tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed); | |
14343 | /* We wrapped the EXIT_EXPR around a WFL so we can debug it. | |
14344 | The real EXIT_EXPR is one operand further. */ | |
14345 | EXPR_WFL_LINECOL (cnode) = location; | |
14346 | /* This one is for accurate error reports */ | |
14347 | EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location; | |
14348 | TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition; | |
14349 | } | |
14350 | LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body; | |
14351 | POP_LOOP (); | |
14352 | return to_return; | |
14353 | } | |
14354 | ||
b635eb2f | 14355 | /* Tailored version of finish_loop_body for FOR loops, when FOR |
e04a16fb AG |
14356 | loops feature the condition part */ |
14357 | ||
14358 | static tree | |
b635eb2f | 14359 | finish_for_loop (location, condition, update, body) |
e04a16fb AG |
14360 | int location; |
14361 | tree condition, update, body; | |
14362 | { | |
14363 | /* Put the condition and the loop body in place */ | |
b635eb2f | 14364 | tree loop = finish_loop_body (location, condition, body, 0); |
e04a16fb AG |
14365 | /* LOOP is the current loop which has been now popped of the loop |
14366 | stack. Install the update block */ | |
14367 | LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update; | |
14368 | return loop; | |
14369 | } | |
14370 | ||
5cbdba64 APB |
14371 | /* Try to find the loop a block might be related to. This comprises |
14372 | the case where the LOOP_EXPR is found as the second operand of a | |
14373 | COMPOUND_EXPR, because the loop happens to have an initialization | |
14374 | part, then expressed as the first operand of the COMPOUND_EXPR. If | |
14375 | the search finds something, 1 is returned. Otherwise, 0 is | |
14376 | returned. The search is assumed to start from a | |
14377 | LABELED_BLOCK_EXPR's block. */ | |
14378 | ||
14379 | static tree | |
14380 | search_loop (statement) | |
14381 | tree statement; | |
14382 | { | |
14383 | if (TREE_CODE (statement) == LOOP_EXPR) | |
14384 | return statement; | |
14385 | ||
14386 | if (TREE_CODE (statement) == BLOCK) | |
14387 | statement = BLOCK_SUBBLOCKS (statement); | |
14388 | else | |
14389 | return NULL_TREE; | |
14390 | ||
14391 | if (statement && TREE_CODE (statement) == COMPOUND_EXPR) | |
14392 | while (statement && TREE_CODE (statement) == COMPOUND_EXPR) | |
14393 | statement = TREE_OPERAND (statement, 1); | |
14394 | ||
14395 | return (TREE_CODE (statement) == LOOP_EXPR | |
c2952b01 | 14396 | && FOR_LOOP_P (statement) ? statement : NULL_TREE); |
5cbdba64 APB |
14397 | } |
14398 | ||
14399 | /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is | |
14400 | returned otherwise. */ | |
14401 | ||
14402 | static int | |
14403 | labeled_block_contains_loop_p (block, loop) | |
14404 | tree block, loop; | |
14405 | { | |
14406 | if (!block) | |
14407 | return 0; | |
14408 | ||
14409 | if (LABELED_BLOCK_BODY (block) == loop) | |
14410 | return 1; | |
14411 | ||
c2952b01 | 14412 | if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop) |
5cbdba64 APB |
14413 | return 1; |
14414 | ||
14415 | return 0; | |
14416 | } | |
14417 | ||
e04a16fb | 14418 | /* If the loop isn't surrounded by a labeled statement, create one and |
b635eb2f | 14419 | insert LOOP as its body. */ |
e04a16fb AG |
14420 | |
14421 | static tree | |
14422 | patch_loop_statement (loop) | |
14423 | tree loop; | |
14424 | { | |
cd9643f7 | 14425 | tree loop_label; |
5cbdba64 | 14426 | |
cd9643f7 | 14427 | TREE_TYPE (loop) = void_type_node; |
5cbdba64 APB |
14428 | if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop)) |
14429 | return loop; | |
14430 | ||
cd9643f7 | 14431 | loop_label = build_labeled_block (0, NULL_TREE); |
5cbdba64 APB |
14432 | /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL |
14433 | that LOOP_LABEL could enquire about, for a better accuracy. FIXME */ | |
cd9643f7 PB |
14434 | LABELED_BLOCK_BODY (loop_label) = loop; |
14435 | PUSH_LABELED_BLOCK (loop_label); | |
5cbdba64 | 14436 | return loop_label; |
e04a16fb AG |
14437 | } |
14438 | ||
14439 | /* 14.13, 14.14: break and continue Statements */ | |
14440 | ||
14441 | /* Build a break or a continue statement. a null NAME indicates an | |
14442 | unlabeled break/continue statement. */ | |
14443 | ||
14444 | static tree | |
14445 | build_bc_statement (location, is_break, name) | |
14446 | int location, is_break; | |
14447 | tree name; | |
14448 | { | |
14449 | tree break_continue, label_block_expr = NULL_TREE; | |
14450 | ||
14451 | if (name) | |
14452 | { | |
14453 | if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE | |
14454 | (merge_qualified_name (label_id, EXPR_WFL_NODE (name))))) | |
14455 | /* Null means that we don't have a target for this named | |
14456 | break/continue. In this case, we make the target to be the | |
14457 | label name, so that the error can be reported accuratly in | |
14458 | patch_bc_statement. */ | |
14459 | label_block_expr = EXPR_WFL_NODE (name); | |
14460 | } | |
14461 | /* Unlabeled break/continue will be handled during the | |
14462 | break/continue patch operation */ | |
14463 | break_continue | |
14464 | = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE); | |
14465 | ||
14466 | IS_BREAK_STMT_P (break_continue) = is_break; | |
14467 | TREE_SIDE_EFFECTS (break_continue) = 1; | |
14468 | EXPR_WFL_LINECOL (break_continue) = location; | |
b67d701b | 14469 | break_continue = build_debugable_stmt (location, break_continue); |
e04a16fb AG |
14470 | return break_continue; |
14471 | } | |
14472 | ||
14473 | /* Verification of a break/continue statement. */ | |
14474 | ||
14475 | static tree | |
14476 | patch_bc_statement (node) | |
14477 | tree node; | |
14478 | { | |
14479 | tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt; | |
b635eb2f | 14480 | tree labeled_block = ctxp->current_labeled_block; |
b67d701b | 14481 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); |
e04a16fb | 14482 | |
e04a16fb | 14483 | /* Having an identifier here means that the target is unknown. */ |
b635eb2f | 14484 | if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE) |
e04a16fb AG |
14485 | { |
14486 | parse_error_context (wfl_operator, "No label definition found for `%s'", | |
14487 | IDENTIFIER_POINTER (bc_label)); | |
14488 | return error_mark_node; | |
14489 | } | |
b635eb2f | 14490 | if (! IS_BREAK_STMT_P (node)) |
e04a16fb | 14491 | { |
b635eb2f PB |
14492 | /* It's a continue statement. */ |
14493 | for (;; labeled_block = TREE_CHAIN (labeled_block)) | |
e04a16fb | 14494 | { |
b635eb2f PB |
14495 | if (labeled_block == NULL_TREE) |
14496 | { | |
14497 | if (bc_label == NULL_TREE) | |
14498 | parse_error_context (wfl_operator, | |
14499 | "`continue' must be in loop"); | |
14500 | else | |
1504b2b4 APB |
14501 | parse_error_context |
14502 | (wfl_operator, "continue label `%s' does not name a loop", | |
14503 | IDENTIFIER_POINTER (bc_label)); | |
b635eb2f PB |
14504 | return error_mark_node; |
14505 | } | |
14506 | if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block)) | |
14507 | == continue_identifier_node) | |
14508 | && (bc_label == NULL_TREE | |
14509 | || TREE_CHAIN (labeled_block) == bc_label)) | |
14510 | { | |
14511 | bc_label = labeled_block; | |
14512 | break; | |
14513 | } | |
e04a16fb | 14514 | } |
e04a16fb | 14515 | } |
b635eb2f | 14516 | else if (!bc_label) |
34f4db93 | 14517 | { |
b635eb2f | 14518 | for (;; labeled_block = TREE_CHAIN (labeled_block)) |
e04a16fb | 14519 | { |
b635eb2f PB |
14520 | if (labeled_block == NULL_TREE) |
14521 | { | |
14522 | parse_error_context (wfl_operator, | |
14523 | "`break' must be in loop or switch"); | |
14524 | return error_mark_node; | |
14525 | } | |
14526 | target_stmt = LABELED_BLOCK_BODY (labeled_block); | |
14527 | if (TREE_CODE (target_stmt) == SWITCH_EXPR | |
5cbdba64 | 14528 | || search_loop (target_stmt)) |
b635eb2f PB |
14529 | { |
14530 | bc_label = labeled_block; | |
14531 | break; | |
14532 | } | |
e04a16fb | 14533 | } |
e04a16fb AG |
14534 | } |
14535 | ||
b635eb2f | 14536 | EXIT_BLOCK_LABELED_BLOCK (node) = bc_label; |
15fdcfe9 PB |
14537 | CAN_COMPLETE_NORMALLY (bc_label) = 1; |
14538 | ||
e04a16fb AG |
14539 | /* Our break/continue don't return values. */ |
14540 | TREE_TYPE (node) = void_type_node; | |
14541 | /* Encapsulate the break within a compound statement so that it's | |
5cbdba64 | 14542 | expanded all the times by expand_expr (and not clobbered |
e04a16fb AG |
14543 | sometimes, like after a if statement) */ |
14544 | node = add_stmt_to_compound (NULL_TREE, void_type_node, node); | |
14545 | TREE_SIDE_EFFECTS (node) = 1; | |
14546 | return node; | |
14547 | } | |
14548 | ||
14549 | /* Process the exit expression belonging to a loop. Its type must be | |
14550 | boolean. */ | |
14551 | ||
14552 | static tree | |
14553 | patch_exit_expr (node) | |
14554 | tree node; | |
14555 | { | |
14556 | tree expression = TREE_OPERAND (node, 0); | |
14557 | TREE_TYPE (node) = error_mark_node; | |
14558 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
14559 | ||
14560 | /* The type of expression must be boolean */ | |
14561 | if (TREE_TYPE (expression) != boolean_type_node) | |
14562 | { | |
14563 | parse_error_context | |
14564 | (wfl_operator, | |
781b0558 | 14565 | "Incompatible type for loop conditional. Can't convert `%s' to `boolean'", |
0a2138e2 | 14566 | lang_printable_name (TREE_TYPE (expression), 0)); |
e04a16fb AG |
14567 | return error_mark_node; |
14568 | } | |
14569 | /* Now we know things are allright, invert the condition, fold and | |
14570 | return */ | |
14571 | TREE_OPERAND (node, 0) = | |
14572 | fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression)); | |
15fdcfe9 PB |
14573 | |
14574 | if (! integer_zerop (TREE_OPERAND (node, 0)) | |
14575 | && ctxp->current_loop != NULL_TREE | |
14576 | && TREE_CODE (ctxp->current_loop) == LOOP_EXPR) | |
14577 | CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1; | |
14578 | if (! integer_onep (TREE_OPERAND (node, 0))) | |
14579 | CAN_COMPLETE_NORMALLY (node) = 1; | |
14580 | ||
14581 | ||
e04a16fb AG |
14582 | TREE_TYPE (node) = void_type_node; |
14583 | return node; | |
14584 | } | |
b67d701b PB |
14585 | |
14586 | /* 14.9 Switch statement */ | |
14587 | ||
14588 | static tree | |
14589 | patch_switch_statement (node) | |
14590 | tree node; | |
14591 | { | |
c877974e | 14592 | tree se = TREE_OPERAND (node, 0), se_type; |
b67d701b PB |
14593 | |
14594 | /* Complete the switch expression */ | |
14595 | se = TREE_OPERAND (node, 0) = java_complete_tree (se); | |
14596 | se_type = TREE_TYPE (se); | |
14597 | /* The type of the switch expression must be char, byte, short or | |
14598 | int */ | |
2e0f0aff | 14599 | if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node) |
b67d701b PB |
14600 | { |
14601 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node); | |
781b0558 KG |
14602 | parse_error_context (wfl_operator, |
14603 | "Incompatible type for `switch'. Can't convert `%s' to `int'", | |
0a2138e2 | 14604 | lang_printable_name (se_type, 0)); |
b67d701b PB |
14605 | /* This is what java_complete_tree will check */ |
14606 | TREE_OPERAND (node, 0) = error_mark_node; | |
14607 | return error_mark_node; | |
14608 | } | |
14609 | ||
15fdcfe9 | 14610 | TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1)); |
b67d701b PB |
14611 | |
14612 | /* Ready to return */ | |
15fdcfe9 | 14613 | if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK) |
b67d701b PB |
14614 | { |
14615 | TREE_TYPE (node) = error_mark_node; | |
14616 | return error_mark_node; | |
14617 | } | |
14618 | TREE_TYPE (node) = void_type_node; | |
14619 | TREE_SIDE_EFFECTS (node) = 1; | |
15fdcfe9 | 14620 | CAN_COMPLETE_NORMALLY (node) |
c877974e APB |
14621 | = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)) |
14622 | || ! SWITCH_HAS_DEFAULT (node); | |
b67d701b PB |
14623 | return node; |
14624 | } | |
14625 | ||
165f37bc | 14626 | /* 14.18 The try/catch statements */ |
b67d701b | 14627 | |
b67d701b | 14628 | static tree |
a7d8d81f | 14629 | build_try_statement (location, try_block, catches) |
b67d701b | 14630 | int location; |
a7d8d81f PB |
14631 | tree try_block, catches; |
14632 | { | |
14633 | tree node = build (TRY_EXPR, NULL_TREE, try_block, catches); | |
b67d701b | 14634 | EXPR_WFL_LINECOL (node) = location; |
a7d8d81f | 14635 | return node; |
b67d701b PB |
14636 | } |
14637 | ||
a7d8d81f PB |
14638 | static tree |
14639 | build_try_finally_statement (location, try_block, finally) | |
14640 | int location; | |
14641 | tree try_block, finally; | |
b67d701b | 14642 | { |
a7d8d81f PB |
14643 | tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally); |
14644 | EXPR_WFL_LINECOL (node) = location; | |
14645 | return node; | |
b67d701b PB |
14646 | } |
14647 | ||
14648 | static tree | |
14649 | patch_try_statement (node) | |
14650 | tree node; | |
14651 | { | |
14652 | int error_found = 0; | |
14653 | tree try = TREE_OPERAND (node, 0); | |
14654 | /* Exception handlers are considered in left to right order */ | |
14655 | tree catch = nreverse (TREE_OPERAND (node, 1)); | |
b9f7e36c | 14656 | tree current, caught_type_list = NULL_TREE; |
b67d701b PB |
14657 | |
14658 | /* Check catch clauses, if any. Every time we find an error, we try | |
b9f7e36c APB |
14659 | to process the next catch clause. We process the catch clause before |
14660 | the try block so that when processing the try block we can check thrown | |
14661 | exceptions againts the caught type list. */ | |
b67d701b PB |
14662 | for (current = catch; current; current = TREE_CHAIN (current)) |
14663 | { | |
14664 | tree carg_decl, carg_type; | |
14665 | tree sub_current, catch_block, catch_clause; | |
14666 | int unreachable; | |
14667 | ||
b67d701b | 14668 | /* At this point, the structure of the catch clause is |
b67d701b PB |
14669 | CATCH_EXPR (catch node) |
14670 | BLOCK (with the decl of the parameter) | |
14671 | COMPOUND_EXPR | |
7525cc04 | 14672 | MODIFY_EXPR (assignment of the catch parameter) |
b67d701b | 14673 | BLOCK (catch clause block) |
a7d8d81f PB |
14674 | */ |
14675 | catch_clause = TREE_OPERAND (current, 0); | |
b67d701b PB |
14676 | carg_decl = BLOCK_EXPR_DECLS (catch_clause); |
14677 | carg_type = TREE_TYPE (TREE_TYPE (carg_decl)); | |
14678 | ||
14679 | /* Catch clauses can't have more than one parameter declared, | |
14680 | but it's already enforced by the grammar. Make sure that the | |
14681 | only parameter of the clause statement in of class Throwable | |
14682 | or a subclass of Throwable, but that was done earlier. The | |
14683 | catch clause parameter type has also been resolved. */ | |
14684 | ||
14685 | /* Just make sure that the catch clause parameter type inherits | |
14686 | from java.lang.Throwable */ | |
14687 | if (!inherits_from_p (carg_type, throwable_type_node)) | |
14688 | { | |
14689 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current); | |
14690 | parse_error_context (wfl_operator, | |
781b0558 | 14691 | "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'", |
0a2138e2 | 14692 | lang_printable_name (carg_type, 0)); |
b67d701b PB |
14693 | error_found = 1; |
14694 | continue; | |
14695 | } | |
14696 | ||
14697 | /* Partial check for unreachable catch statement: The catch | |
14698 | clause is reachable iff is no earlier catch block A in | |
14699 | the try statement such that the type of the catch | |
14700 | clause's parameter is the same as or a subclass of the | |
14701 | type of A's parameter */ | |
14702 | unreachable = 0; | |
14703 | for (sub_current = catch; | |
14704 | sub_current != current; sub_current = TREE_CHAIN (sub_current)) | |
14705 | { | |
14706 | tree sub_catch_clause, decl; | |
a7d8d81f | 14707 | sub_catch_clause = TREE_OPERAND (sub_current, 0); |
b67d701b PB |
14708 | decl = BLOCK_EXPR_DECLS (sub_catch_clause); |
14709 | ||
14710 | if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl)))) | |
14711 | { | |
14712 | EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current); | |
14713 | parse_error_context | |
781b0558 KG |
14714 | (wfl_operator, |
14715 | "`catch' not reached because of the catch clause at line %d", | |
14716 | EXPR_WFL_LINENO (sub_current)); | |
b67d701b PB |
14717 | unreachable = error_found = 1; |
14718 | break; | |
14719 | } | |
14720 | } | |
b67d701b PB |
14721 | /* Complete the catch clause block */ |
14722 | catch_block = java_complete_tree (TREE_OPERAND (current, 0)); | |
14723 | if (catch_block == error_mark_node) | |
14724 | { | |
14725 | error_found = 1; | |
14726 | continue; | |
14727 | } | |
15fdcfe9 PB |
14728 | if (CAN_COMPLETE_NORMALLY (catch_block)) |
14729 | CAN_COMPLETE_NORMALLY (node) = 1; | |
b67d701b | 14730 | TREE_OPERAND (current, 0) = catch_block; |
15fdcfe9 PB |
14731 | |
14732 | if (unreachable) | |
14733 | continue; | |
14734 | ||
14735 | /* Things to do here: the exception must be thrown */ | |
14736 | ||
14737 | /* Link this type to the caught type list */ | |
14738 | caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list); | |
b67d701b PB |
14739 | } |
14740 | ||
b9f7e36c APB |
14741 | PUSH_EXCEPTIONS (caught_type_list); |
14742 | if ((try = java_complete_tree (try)) == error_mark_node) | |
14743 | error_found = 1; | |
15fdcfe9 PB |
14744 | if (CAN_COMPLETE_NORMALLY (try)) |
14745 | CAN_COMPLETE_NORMALLY (node) = 1; | |
b9f7e36c APB |
14746 | POP_EXCEPTIONS (); |
14747 | ||
b67d701b PB |
14748 | /* Verification ends here */ |
14749 | if (error_found) | |
14750 | return error_mark_node; | |
14751 | ||
14752 | TREE_OPERAND (node, 0) = try; | |
14753 | TREE_OPERAND (node, 1) = catch; | |
b67d701b PB |
14754 | TREE_TYPE (node) = void_type_node; |
14755 | return node; | |
14756 | } | |
b9f7e36c APB |
14757 | |
14758 | /* 14.17 The synchronized Statement */ | |
14759 | ||
14760 | static tree | |
14761 | patch_synchronized_statement (node, wfl_op1) | |
14762 | tree node, wfl_op1; | |
14763 | { | |
5a005d9e | 14764 | tree expr = java_complete_tree (TREE_OPERAND (node, 0)); |
b9f7e36c | 14765 | tree block = TREE_OPERAND (node, 1); |
5a005d9e | 14766 | |
d8fccff5 | 14767 | tree enter, exit, expr_decl, assignment; |
5a005d9e PB |
14768 | |
14769 | if (expr == error_mark_node) | |
14770 | { | |
14771 | block = java_complete_tree (block); | |
14772 | return expr; | |
14773 | } | |
b9f7e36c APB |
14774 | |
14775 | /* The TYPE of expr must be a reference type */ | |
5a005d9e | 14776 | if (!JREFERENCE_TYPE_P (TREE_TYPE (expr))) |
b9f7e36c APB |
14777 | { |
14778 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); | |
781b0558 | 14779 | parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'", |
0a2138e2 | 14780 | lang_printable_name (TREE_TYPE (expr), 0)); |
b9f7e36c APB |
14781 | return error_mark_node; |
14782 | } | |
14783 | ||
ce6e9147 APB |
14784 | if (flag_emit_xref) |
14785 | { | |
14786 | TREE_OPERAND (node, 0) = expr; | |
14787 | TREE_OPERAND (node, 1) = java_complete_tree (block); | |
14788 | CAN_COMPLETE_NORMALLY (node) = 1; | |
14789 | return node; | |
14790 | } | |
14791 | ||
b9f7e36c APB |
14792 | /* Generate a try-finally for the synchronized statement, except |
14793 | that the handler that catches all throw exception calls | |
14794 | _Jv_MonitorExit and then rethrow the exception. | |
14795 | The synchronized statement is then implemented as: | |
14796 | TRY | |
14797 | { | |
14798 | _Jv_MonitorEnter (expression) | |
14799 | synchronized_block | |
14800 | _Jv_MonitorExit (expression) | |
14801 | } | |
14802 | CATCH_ALL | |
14803 | { | |
14804 | e = _Jv_exception_info (); | |
14805 | _Jv_MonitorExit (expression) | |
14806 | Throw (e); | |
14807 | } */ | |
14808 | ||
5a005d9e PB |
14809 | expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr)); |
14810 | BUILD_MONITOR_ENTER (enter, expr_decl); | |
14811 | BUILD_MONITOR_EXIT (exit, expr_decl); | |
14812 | CAN_COMPLETE_NORMALLY (enter) = 1; | |
14813 | CAN_COMPLETE_NORMALLY (exit) = 1; | |
96847892 AH |
14814 | assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr); |
14815 | TREE_SIDE_EFFECTS (assignment) = 1; | |
5a005d9e PB |
14816 | node = build1 (CLEANUP_POINT_EXPR, NULL_TREE, |
14817 | build (COMPOUND_EXPR, NULL_TREE, | |
14818 | build (WITH_CLEANUP_EXPR, NULL_TREE, | |
14819 | build (COMPOUND_EXPR, NULL_TREE, | |
96847892 | 14820 | assignment, enter), |
5a005d9e PB |
14821 | NULL_TREE, exit), |
14822 | block)); | |
14823 | node = build_expr_block (node, expr_decl); | |
14824 | ||
14825 | return java_complete_tree (node); | |
b9f7e36c APB |
14826 | } |
14827 | ||
14828 | /* 14.16 The throw Statement */ | |
14829 | ||
14830 | static tree | |
14831 | patch_throw_statement (node, wfl_op1) | |
14832 | tree node, wfl_op1; | |
14833 | { | |
14834 | tree expr = TREE_OPERAND (node, 0); | |
14835 | tree type = TREE_TYPE (expr); | |
14836 | int unchecked_ok = 0, tryblock_throws_ok = 0; | |
14837 | ||
14838 | /* Thrown expression must be assignable to java.lang.Throwable */ | |
14839 | if (!try_reference_assignconv (throwable_type_node, expr)) | |
14840 | { | |
14841 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); | |
781b0558 KG |
14842 | parse_error_context (wfl_operator, |
14843 | "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'", | |
0a2138e2 | 14844 | lang_printable_name (type, 0)); |
b9f7e36c APB |
14845 | /* If the thrown expression was a reference, we further the |
14846 | compile-time check. */ | |
14847 | if (!JREFERENCE_TYPE_P (type)) | |
14848 | return error_mark_node; | |
14849 | } | |
14850 | ||
14851 | /* At least one of the following must be true */ | |
14852 | ||
14853 | /* The type of the throw expression is a not checked exception, | |
14854 | i.e. is a unchecked expression. */ | |
c877974e | 14855 | unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type)); |
b9f7e36c | 14856 | |
c2952b01 APB |
14857 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); |
14858 | /* An instance can't throw a checked excetion unless that exception | |
14859 | is explicitely declared in the `throws' clause of each | |
14860 | constructor. This doesn't apply to anonymous classes, since they | |
14861 | don't have declared constructors. */ | |
14862 | if (!unchecked_ok | |
14863 | && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class)) | |
14864 | { | |
14865 | tree current; | |
14866 | for (current = TYPE_METHODS (current_class); current; | |
14867 | current = TREE_CHAIN (current)) | |
14868 | if (DECL_CONSTRUCTOR_P (current) | |
14869 | && !check_thrown_exceptions_do (TREE_TYPE (expr))) | |
14870 | { | |
14871 | parse_error_context (wfl_operator, "Checked exception `%s' can't be thrown in instance initializer (not all declared constructor are declaring it in their `throws' clause)", | |
14872 | lang_printable_name (TREE_TYPE (expr), 0)); | |
14873 | return error_mark_node; | |
14874 | } | |
14875 | } | |
14876 | ||
b9f7e36c APB |
14877 | /* Throw is contained in a try statement and at least one catch |
14878 | clause can receive the thrown expression or the current method is | |
14879 | declared to throw such an exception. Or, the throw statement is | |
14880 | contained in a method or constructor declaration and the type of | |
14881 | the Expression is assignable to at least one type listed in the | |
14882 | throws clause the declaration. */ | |
b9f7e36c | 14883 | if (!unchecked_ok) |
f099f336 | 14884 | tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr)); |
b9f7e36c APB |
14885 | if (!(unchecked_ok || tryblock_throws_ok)) |
14886 | { | |
14887 | /* If there is a surrounding try block that has no matching | |
14888 | clatch clause, report it first. A surrounding try block exits | |
14889 | only if there is something after the list of checked | |
14890 | exception thrown by the current function (if any). */ | |
14891 | if (IN_TRY_BLOCK_P ()) | |
781b0558 | 14892 | parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block", |
0a2138e2 | 14893 | lang_printable_name (type, 0)); |
b9f7e36c APB |
14894 | /* If we have no surrounding try statement and the method doesn't have |
14895 | any throws, report it now. FIXME */ | |
f099f336 APB |
14896 | |
14897 | /* We report that the exception can't be throw from a try block | |
14898 | in all circumstances but when the `throw' is inside a static | |
14899 | block. */ | |
b9f7e36c APB |
14900 | else if (!EXCEPTIONS_P (currently_caught_type_list) |
14901 | && !tryblock_throws_ok) | |
f099f336 | 14902 | { |
c2952b01 | 14903 | if (DECL_CLINIT_P (current_function_decl)) |
781b0558 KG |
14904 | parse_error_context (wfl_operator, |
14905 | "Checked exception `%s' can't be thrown in initializer", | |
f099f336 APB |
14906 | lang_printable_name (type, 0)); |
14907 | else | |
781b0558 KG |
14908 | parse_error_context (wfl_operator, |
14909 | "Checked exception `%s' isn't thrown from a `try' block", | |
f099f336 APB |
14910 | lang_printable_name (type, 0)); |
14911 | } | |
b9f7e36c APB |
14912 | /* Otherwise, the current method doesn't have the appropriate |
14913 | throws declaration */ | |
14914 | else | |
781b0558 | 14915 | parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)", |
0a2138e2 | 14916 | lang_printable_name (type, 0)); |
b9f7e36c APB |
14917 | return error_mark_node; |
14918 | } | |
14919 | ||
ce6e9147 | 14920 | if (! flag_emit_class_files && ! flag_emit_xref) |
15fdcfe9 | 14921 | BUILD_THROW (node, expr); |
ce6e9147 APB |
14922 | |
14923 | /* If doing xrefs, keep the location where the `throw' was seen. */ | |
14924 | if (flag_emit_xref) | |
14925 | EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1); | |
b9f7e36c APB |
14926 | return node; |
14927 | } | |
14928 | ||
14929 | /* Check that exception said to be thrown by method DECL can be | |
14930 | effectively caught from where DECL is invoked. */ | |
14931 | ||
14932 | static void | |
14933 | check_thrown_exceptions (location, decl) | |
14934 | int location; | |
14935 | tree decl; | |
14936 | { | |
14937 | tree throws; | |
14938 | /* For all the unchecked exceptions thrown by DECL */ | |
14939 | for (throws = DECL_FUNCTION_THROWS (decl); throws; | |
14940 | throws = TREE_CHAIN (throws)) | |
0a2138e2 | 14941 | if (!check_thrown_exceptions_do (TREE_VALUE (throws))) |
b9f7e36c | 14942 | { |
3e78f871 PB |
14943 | #if 1 |
14944 | /* Temporary hack to suppresses errors about cloning arrays. FIXME */ | |
14945 | if (DECL_NAME (decl) == get_identifier ("clone")) | |
14946 | continue; | |
14947 | #endif | |
b9f7e36c | 14948 | EXPR_WFL_LINECOL (wfl_operator) = location; |
c2952b01 | 14949 | if (DECL_FINIT_P (current_function_decl)) |
7705e9db APB |
14950 | parse_error_context |
14951 | (wfl_operator, "Exception `%s' can't be thrown in initializer", | |
14952 | lang_printable_name (TREE_VALUE (throws), 0)); | |
14953 | else | |
14954 | { | |
14955 | parse_error_context | |
781b0558 | 14956 | (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'", |
7705e9db | 14957 | lang_printable_name (TREE_VALUE (throws), 0), |
c2952b01 | 14958 | (DECL_INIT_P (current_function_decl) ? |
7705e9db APB |
14959 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) : |
14960 | IDENTIFIER_POINTER (DECL_NAME (current_function_decl)))); | |
14961 | } | |
b9f7e36c APB |
14962 | } |
14963 | } | |
14964 | ||
c877974e | 14965 | /* Return 1 if checked EXCEPTION is caught at the current nesting level of |
b9f7e36c APB |
14966 | try-catch blocks, OR is listed in the `throws' clause of the |
14967 | current method. */ | |
14968 | ||
14969 | static int | |
0a2138e2 | 14970 | check_thrown_exceptions_do (exception) |
b9f7e36c APB |
14971 | tree exception; |
14972 | { | |
14973 | tree list = currently_caught_type_list; | |
c877974e | 14974 | resolve_and_layout (exception, NULL_TREE); |
b9f7e36c APB |
14975 | /* First, all the nested try-catch-finally at that stage. The |
14976 | last element contains `throws' clause exceptions, if any. */ | |
c877974e APB |
14977 | if (IS_UNCHECKED_EXCEPTION_P (exception)) |
14978 | return 1; | |
b9f7e36c APB |
14979 | while (list) |
14980 | { | |
14981 | tree caught; | |
14982 | for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught)) | |
14983 | if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0)) | |
14984 | return 1; | |
14985 | list = TREE_CHAIN (list); | |
14986 | } | |
14987 | return 0; | |
14988 | } | |
14989 | ||
14990 | static void | |
14991 | purge_unchecked_exceptions (mdecl) | |
14992 | tree mdecl; | |
14993 | { | |
14994 | tree throws = DECL_FUNCTION_THROWS (mdecl); | |
14995 | tree new = NULL_TREE; | |
14996 | ||
14997 | while (throws) | |
14998 | { | |
14999 | tree next = TREE_CHAIN (throws); | |
c877974e | 15000 | if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws))) |
b9f7e36c APB |
15001 | { |
15002 | TREE_CHAIN (throws) = new; | |
15003 | new = throws; | |
15004 | } | |
15005 | throws = next; | |
15006 | } | |
15007 | /* List is inverted here, but it doesn't matter */ | |
15008 | DECL_FUNCTION_THROWS (mdecl) = new; | |
15009 | } | |
22eed1e6 APB |
15010 | |
15011 | /* 15.24 Conditional Operator ?: */ | |
15012 | ||
15013 | static tree | |
15014 | patch_conditional_expr (node, wfl_cond, wfl_op1) | |
15015 | tree node, wfl_cond, wfl_op1; | |
15016 | { | |
15017 | tree cond = TREE_OPERAND (node, 0); | |
15018 | tree op1 = TREE_OPERAND (node, 1); | |
15019 | tree op2 = TREE_OPERAND (node, 2); | |
22eed1e6 | 15020 | tree resulting_type = NULL_TREE; |
ac825856 | 15021 | tree t1, t2, patched; |
22eed1e6 APB |
15022 | int error_found = 0; |
15023 | ||
ac825856 APB |
15024 | /* Operands of ?: might be StringBuffers crafted as a result of a |
15025 | string concatenation. Obtain a descent operand here. */ | |
15026 | if ((patched = patch_string (op1))) | |
15027 | TREE_OPERAND (node, 1) = op1 = patched; | |
15028 | if ((patched = patch_string (op2))) | |
15029 | TREE_OPERAND (node, 2) = op2 = patched; | |
15030 | ||
15031 | t1 = TREE_TYPE (op1); | |
15032 | t2 = TREE_TYPE (op2); | |
15033 | ||
22eed1e6 APB |
15034 | /* The first expression must be a boolean */ |
15035 | if (TREE_TYPE (cond) != boolean_type_node) | |
15036 | { | |
15037 | SET_WFL_OPERATOR (wfl_operator, node, wfl_cond); | |
781b0558 KG |
15038 | parse_error_context (wfl_operator, |
15039 | "Incompatible type for `?:'. Can't convert `%s' to `boolean'", | |
22eed1e6 APB |
15040 | lang_printable_name (TREE_TYPE (cond), 0)); |
15041 | error_found = 1; | |
15042 | } | |
15043 | ||
15044 | /* Second and third can be numeric, boolean (i.e. primitive), | |
15045 | references or null. Anything else results in an error */ | |
15046 | if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2)) | |
15047 | || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node) | |
15048 | && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node)) | |
15049 | || (t1 == boolean_type_node && t2 == boolean_type_node))) | |
15050 | error_found = 1; | |
15051 | ||
15052 | /* Determine the type of the conditional expression. Same types are | |
15053 | easy to deal with */ | |
15054 | else if (t1 == t2) | |
15055 | resulting_type = t1; | |
15056 | ||
15057 | /* There are different rules for numeric types */ | |
15058 | else if (JNUMERIC_TYPE_P (t1)) | |
15059 | { | |
15060 | /* if byte/short found, the resulting type is short */ | |
15061 | if ((t1 == byte_type_node && t2 == short_type_node) | |
15062 | || (t1 == short_type_node && t2 == byte_type_node)) | |
15063 | resulting_type = short_type_node; | |
15064 | ||
15065 | /* If t1 is a constant int and t2 is of type byte, short or char | |
15066 | and t1's value fits in t2, then the resulting type is t2 */ | |
15067 | else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1))) | |
15068 | && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2)) | |
15069 | resulting_type = t2; | |
15070 | ||
15071 | /* If t2 is a constant int and t1 is of type byte, short or char | |
15072 | and t2's value fits in t1, then the resulting type is t1 */ | |
15073 | else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2))) | |
15074 | && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1)) | |
15075 | resulting_type = t1; | |
15076 | ||
15077 | /* Otherwise, binary numeric promotion is applied and the | |
15078 | resulting type is the promoted type of operand 1 and 2 */ | |
15079 | else | |
93024893 | 15080 | resulting_type = binary_numeric_promotion (t1, t2, |
22eed1e6 APB |
15081 | &TREE_OPERAND (node, 1), |
15082 | &TREE_OPERAND (node, 2)); | |
15083 | } | |
15084 | ||
15085 | /* Cases of a reference and a null type */ | |
15086 | else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node) | |
15087 | resulting_type = t1; | |
15088 | ||
15089 | else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node) | |
15090 | resulting_type = t2; | |
15091 | ||
15092 | /* Last case: different reference types. If a type can be converted | |
15093 | into the other one by assignment conversion, the latter | |
15094 | determines the type of the expression */ | |
15095 | else if ((resulting_type = try_reference_assignconv (t1, op2))) | |
15096 | resulting_type = promote_type (t1); | |
15097 | ||
15098 | else if ((resulting_type = try_reference_assignconv (t2, op1))) | |
15099 | resulting_type = promote_type (t2); | |
15100 | ||
15101 | /* If we don't have any resulting type, we're in trouble */ | |
15102 | if (!resulting_type) | |
15103 | { | |
c2e3db92 | 15104 | char *t = xstrdup (lang_printable_name (t1, 0)); |
22eed1e6 | 15105 | SET_WFL_OPERATOR (wfl_operator, node, wfl_op1); |
781b0558 KG |
15106 | parse_error_context (wfl_operator, |
15107 | "Incompatible type for `?:'. Can't convert `%s' to `%s'", | |
15108 | t, lang_printable_name (t2, 0)); | |
22eed1e6 APB |
15109 | free (t); |
15110 | error_found = 1; | |
15111 | } | |
15112 | ||
15113 | if (error_found) | |
15114 | { | |
15115 | TREE_TYPE (node) = error_mark_node; | |
15116 | return error_mark_node; | |
15117 | } | |
15118 | ||
15119 | TREE_TYPE (node) = resulting_type; | |
15120 | TREE_SET_CODE (node, COND_EXPR); | |
15fdcfe9 | 15121 | CAN_COMPLETE_NORMALLY (node) = 1; |
22eed1e6 APB |
15122 | return node; |
15123 | } | |
ac825856 | 15124 | |
5b09b33e PB |
15125 | /* Try to constant fold NODE. |
15126 | If NODE is not a constant expression, return NULL_EXPR. | |
15127 | CONTEXT is a static final VAR_DECL whose initializer we are folding. */ | |
15128 | ||
15129 | static tree | |
15130 | fold_constant_for_init (node, context) | |
15131 | tree node; | |
15132 | tree context; | |
15133 | { | |
15134 | tree op0, op1, val; | |
15135 | enum tree_code code = TREE_CODE (node); | |
15136 | ||
ee97d354 | 15137 | if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST) |
5b09b33e | 15138 | return node; |
93024893 | 15139 | |
5b09b33e PB |
15140 | switch (code) |
15141 | { | |
5b09b33e PB |
15142 | case PLUS_EXPR: |
15143 | case MINUS_EXPR: | |
bc3ca41b PB |
15144 | case MULT_EXPR: |
15145 | case TRUNC_MOD_EXPR: | |
15146 | case RDIV_EXPR: | |
5b09b33e PB |
15147 | case LSHIFT_EXPR: |
15148 | case RSHIFT_EXPR: | |
15149 | case URSHIFT_EXPR: | |
15150 | case BIT_AND_EXPR: | |
15151 | case BIT_XOR_EXPR: | |
15152 | case BIT_IOR_EXPR: | |
5b09b33e PB |
15153 | case TRUTH_ANDIF_EXPR: |
15154 | case TRUTH_ORIF_EXPR: | |
15155 | case EQ_EXPR: | |
15156 | case NE_EXPR: | |
15157 | case GT_EXPR: | |
15158 | case GE_EXPR: | |
15159 | case LT_EXPR: | |
15160 | case LE_EXPR: | |
15161 | op0 = TREE_OPERAND (node, 0); | |
15162 | op1 = TREE_OPERAND (node, 1); | |
15163 | val = fold_constant_for_init (op0, context); | |
15164 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
15165 | return NULL_TREE; | |
15166 | TREE_OPERAND (node, 0) = val; | |
15167 | val = fold_constant_for_init (op1, context); | |
15168 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
15169 | return NULL_TREE; | |
15170 | TREE_OPERAND (node, 1) = val; | |
15171 | return patch_binop (node, op0, op1); | |
15172 | ||
15173 | case UNARY_PLUS_EXPR: | |
15174 | case NEGATE_EXPR: | |
15175 | case TRUTH_NOT_EXPR: | |
15176 | case BIT_NOT_EXPR: | |
15177 | case CONVERT_EXPR: | |
15178 | op0 = TREE_OPERAND (node, 0); | |
15179 | val = fold_constant_for_init (op0, context); | |
15180 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
15181 | return NULL_TREE; | |
15182 | TREE_OPERAND (node, 0) = val; | |
5a005d9e | 15183 | return patch_unaryop (node, op0); |
5b09b33e PB |
15184 | break; |
15185 | ||
15186 | case COND_EXPR: | |
15187 | val = fold_constant_for_init (TREE_OPERAND (node, 0), context); | |
15188 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
15189 | return NULL_TREE; | |
15190 | TREE_OPERAND (node, 0) = val; | |
15191 | val = fold_constant_for_init (TREE_OPERAND (node, 1), context); | |
15192 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
15193 | return NULL_TREE; | |
15194 | TREE_OPERAND (node, 1) = val; | |
15195 | val = fold_constant_for_init (TREE_OPERAND (node, 2), context); | |
15196 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
15197 | return NULL_TREE; | |
15198 | TREE_OPERAND (node, 2) = val; | |
15199 | return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1) | |
15200 | : TREE_OPERAND (node, 2); | |
15201 | ||
15202 | case VAR_DECL: | |
8576f094 APB |
15203 | case FIELD_DECL: |
15204 | if (! FIELD_FINAL (node) | |
5b09b33e PB |
15205 | || DECL_INITIAL (node) == NULL_TREE) |
15206 | return NULL_TREE; | |
15207 | val = DECL_INITIAL (node); | |
15208 | /* Guard against infinite recursion. */ | |
15209 | DECL_INITIAL (node) = NULL_TREE; | |
cd9643f7 | 15210 | val = fold_constant_for_init (val, node); |
5b09b33e PB |
15211 | DECL_INITIAL (node) = val; |
15212 | return val; | |
15213 | ||
15214 | case EXPR_WITH_FILE_LOCATION: | |
15215 | /* Compare java_complete_tree and resolve_expression_name. */ | |
15216 | if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */ | |
15217 | || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE) | |
15218 | { | |
15219 | tree name = EXPR_WFL_NODE (node); | |
15220 | tree decl; | |
15221 | if (PRIMARY_P (node)) | |
15222 | return NULL_TREE; | |
15223 | else if (! QUALIFIED_P (name)) | |
15224 | { | |
15225 | decl = lookup_field_wrapper (DECL_CONTEXT (context), name); | |
8576f094 APB |
15226 | if (decl == NULL_TREE |
15227 | || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl))) | |
5b09b33e PB |
15228 | return NULL_TREE; |
15229 | return fold_constant_for_init (decl, decl); | |
15230 | } | |
15231 | else | |
15232 | { | |
5b09b33e PB |
15233 | /* Wait until the USE_COMPONENT_REF re-write. FIXME. */ |
15234 | qualify_ambiguous_name (node); | |
15235 | if (resolve_field_access (node, &decl, NULL) | |
15236 | && decl != NULL_TREE) | |
15237 | return fold_constant_for_init (decl, decl); | |
5b09b33e PB |
15238 | return NULL_TREE; |
15239 | } | |
15240 | } | |
15241 | else | |
15242 | { | |
15243 | op0 = TREE_OPERAND (node, 0); | |
15244 | val = fold_constant_for_init (op0, context); | |
15245 | if (val == NULL_TREE || ! TREE_CONSTANT (val)) | |
15246 | return NULL_TREE; | |
15247 | TREE_OPERAND (node, 0) = val; | |
15248 | return val; | |
15249 | } | |
15250 | ||
bc3ca41b PB |
15251 | #ifdef USE_COMPONENT_REF |
15252 | case IDENTIFIER: | |
15253 | case COMPONENT_REF: | |
15254 | ?; | |
15255 | #endif | |
15256 | ||
5b09b33e PB |
15257 | default: |
15258 | return NULL_TREE; | |
15259 | } | |
15260 | } | |
bc3ca41b PB |
15261 | |
15262 | #ifdef USE_COMPONENT_REF | |
15263 | /* Context is 'T' for TypeName, 'P' for PackageName, | |
15264 | 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */ | |
15265 | ||
15266 | tree | |
15267 | resolve_simple_name (name, context) | |
15268 | tree name; | |
15269 | int context; | |
15270 | { | |
15271 | } | |
15272 | ||
15273 | tree | |
15274 | resolve_qualified_name (name, context) | |
15275 | tree name; | |
15276 | int context; | |
15277 | { | |
15278 | } | |
15279 | #endif | |
f15b9af9 MM |
15280 | |
15281 | /* Mark P, which is really a `struct parser_ctxt **' for GC. */ | |
15282 | ||
15283 | static void | |
15284 | mark_parser_ctxt (p) | |
15285 | void *p; | |
15286 | { | |
15287 | struct parser_ctxt *pc = *((struct parser_ctxt **) p); | |
15288 | int i; | |
15289 | ||
15290 | if (!pc) | |
15291 | return; | |
15292 | ||
15293 | #ifndef JC1_LITE | |
15294 | for (i = 0; i < 11; ++i) | |
15295 | ggc_mark_tree (pc->modifier_ctx[i]); | |
15296 | ggc_mark_tree (pc->class_type); | |
15297 | ggc_mark_tree (pc->function_decl); | |
15298 | ggc_mark_tree (pc->package); | |
15299 | ggc_mark_tree (pc->incomplete_class); | |
15300 | ggc_mark_tree (pc->gclass_list); | |
15301 | ggc_mark_tree (pc->class_list); | |
15302 | ggc_mark_tree (pc->current_parsed_class); | |
15303 | ggc_mark_tree (pc->current_parsed_class_un); | |
15304 | ggc_mark_tree (pc->non_static_initialized); | |
15305 | ggc_mark_tree (pc->static_initialized); | |
15306 | ggc_mark_tree (pc->instance_initializers); | |
15307 | ggc_mark_tree (pc->import_list); | |
15308 | ggc_mark_tree (pc->import_demand_list); | |
15309 | ggc_mark_tree (pc->current_loop); | |
15310 | ggc_mark_tree (pc->current_labeled_block); | |
15311 | #endif /* JC1_LITE */ | |
15312 | ||
15313 | if (pc->next) | |
15314 | mark_parser_ctxt (&pc->next); | |
15315 | } |