]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/java/parse.y
gcc.c (default_compilers): Const-ify.
[thirdparty/gcc.git] / gcc / java / parse.y
1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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'
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 %{
49 #include "config.h"
50 #include "system.h"
51 #include <dirent.h>
52 #include "tree.h"
53 #include "rtl.h"
54 #include "obstack.h"
55 #include "toplev.h"
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"
62 #include "convert.h"
63 #include "buffer.h"
64 #include "xref.h"
65 #include "function.h"
66 #include "except.h"
67 #include "ggc.h"
68
69 #ifndef DIR_SEPARATOR
70 #define DIR_SEPARATOR '/'
71 #endif
72
73 /* Local function prototypes */
74 static char *java_accstring_lookup PARAMS ((int));
75 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
77 static tree create_class PARAMS ((int, tree, tree, tree));
78 static tree create_interface PARAMS ((int, tree, tree));
79 static void end_class_declaration PARAMS ((int));
80 static tree find_field PARAMS ((tree, tree));
81 static tree lookup_field_wrapper PARAMS ((tree, tree));
82 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83 static void register_fields PARAMS ((int, tree, tree));
84 static tree parser_qualified_classname PARAMS ((tree));
85 static int parser_check_super PARAMS ((tree, tree, tree));
86 static int parser_check_super_interface PARAMS ((tree, tree, tree));
87 static void check_modifiers_consistency PARAMS ((int));
88 static tree lookup_cl PARAMS ((tree));
89 static tree lookup_java_method2 PARAMS ((tree, tree, int));
90 static tree method_header PARAMS ((int, tree, tree, tree));
91 static void fix_method_argument_names PARAMS ((tree ,tree));
92 static tree method_declarator PARAMS ((tree, tree));
93 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
94 ATTRIBUTE_PRINTF_2;
95 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
96 ATTRIBUTE_PRINTF (2, 0);
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, tree));
103 static void find_in_imports PARAMS ((tree, tree));
104 static void check_inner_class_access PARAMS ((tree, tree, tree));
105 static int check_pkg_class_access PARAMS ((tree, tree, bool));
106 static void register_package PARAMS ((tree));
107 static tree resolve_package PARAMS ((tree, tree *));
108 static tree lookup_package_type PARAMS ((const char *, int));
109 static tree resolve_class PARAMS ((tree, tree, tree, tree));
110 static void declare_local_variables PARAMS ((int, tree, tree));
111 static void dump_java_tree PARAMS ((enum tree_dump_index, tree));
112 static void source_start_java_method PARAMS ((tree));
113 static void source_end_java_method PARAMS ((void));
114 static tree find_name_in_single_imports PARAMS ((tree));
115 static void check_abstract_method_header PARAMS ((tree));
116 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
117 static tree resolve_expression_name PARAMS ((tree, tree *));
118 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
119 static int check_class_interface_creation PARAMS ((int, int, tree,
120 tree, tree, tree));
121 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
122 int *, tree *));
123 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
124 static int in_same_package PARAMS ((tree, tree));
125 static tree resolve_and_layout PARAMS ((tree, tree));
126 static tree qualify_and_find PARAMS ((tree, tree, tree));
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,
130 tree, tree));
131 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
132 tree *, tree *));
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));
136 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
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 check_inner_circular_reference PARAMS ((tree, tree));
140 static tree check_circular_reference PARAMS ((tree));
141 static tree obtain_incomplete_type PARAMS ((tree));
142 static tree java_complete_lhs PARAMS ((tree));
143 static tree java_complete_tree PARAMS ((tree));
144 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
145 static int analyze_clinit_body PARAMS ((tree, tree));
146 static int maybe_yank_clinit PARAMS ((tree));
147 static void start_complete_expand_method PARAMS ((tree));
148 static void java_complete_expand_method PARAMS ((tree));
149 static void java_expand_method_bodies PARAMS ((tree));
150 static int unresolved_type_p PARAMS ((tree, tree *));
151 static void create_jdep_list PARAMS ((struct parser_ctxt *));
152 static tree build_expr_block PARAMS ((tree, tree));
153 static tree enter_block PARAMS ((void));
154 static tree exit_block PARAMS ((void));
155 static tree lookup_name_in_blocks PARAMS ((tree));
156 static void maybe_absorb_scoping_blocks PARAMS ((void));
157 static tree build_method_invocation PARAMS ((tree, tree));
158 static tree build_new_invocation PARAMS ((tree, tree));
159 static tree build_assignment PARAMS ((int, int, tree, tree));
160 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
161 static tree patch_assignment PARAMS ((tree, tree));
162 static tree patch_binop PARAMS ((tree, tree, tree));
163 static tree build_unaryop PARAMS ((int, int, tree));
164 static tree build_incdec PARAMS ((int, int, tree, int));
165 static tree patch_unaryop PARAMS ((tree, tree));
166 static tree build_cast PARAMS ((int, tree, tree));
167 static tree build_null_of_type PARAMS ((tree));
168 static tree patch_cast PARAMS ((tree, tree));
169 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
170 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
171 static int valid_cast_to_p PARAMS ((tree, tree));
172 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
173 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
174 static tree try_reference_assignconv PARAMS ((tree, tree));
175 static tree build_unresolved_array_type PARAMS ((tree));
176 static int build_type_name_from_array_name PARAMS ((tree, tree *));
177 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
178 static tree build_array_ref PARAMS ((int, tree, tree));
179 static tree patch_array_ref PARAMS ((tree));
180 static tree make_qualified_name PARAMS ((tree, tree, int));
181 static tree merge_qualified_name PARAMS ((tree, tree));
182 static tree make_qualified_primary PARAMS ((tree, tree, int));
183 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
184 tree *, tree *));
185 static void qualify_ambiguous_name PARAMS ((tree));
186 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
187 static tree build_newarray_node PARAMS ((tree, tree, int));
188 static tree patch_newarray PARAMS ((tree));
189 static tree resolve_type_during_patch PARAMS ((tree));
190 static tree build_this PARAMS ((int));
191 static tree build_wfl_wrap PARAMS ((tree, int));
192 static tree build_return PARAMS ((int, tree));
193 static tree patch_return PARAMS ((tree));
194 static tree maybe_access_field PARAMS ((tree, tree, tree));
195 static int complete_function_arguments PARAMS ((tree));
196 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
197 tree, tree));
198 static int not_accessible_p PARAMS ((tree, tree, tree, int));
199 static void check_deprecation PARAMS ((tree, tree));
200 static int class_in_current_package PARAMS ((tree));
201 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
202 static tree patch_if_else_statement PARAMS ((tree));
203 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
204 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
205 static tree patch_exit_expr PARAMS ((tree));
206 static tree build_labeled_block PARAMS ((int, tree));
207 static tree finish_labeled_statement PARAMS ((tree, tree));
208 static tree build_bc_statement PARAMS ((int, int, tree));
209 static tree patch_bc_statement PARAMS ((tree));
210 static tree patch_loop_statement PARAMS ((tree));
211 static tree build_new_loop PARAMS ((tree));
212 static tree build_loop_body PARAMS ((int, tree, int));
213 static tree finish_loop_body PARAMS ((int, tree, tree, int));
214 static tree build_debugable_stmt PARAMS ((int, tree));
215 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
216 static tree patch_switch_statement PARAMS ((tree));
217 static tree string_constant_concatenation PARAMS ((tree, tree));
218 static tree build_string_concatenation PARAMS ((tree, tree));
219 static tree patch_string_cst PARAMS ((tree));
220 static tree patch_string PARAMS ((tree));
221 static tree encapsulate_with_try_catch PARAMS ((int, tree, tree, tree));
222 static tree build_try_statement PARAMS ((int, tree, tree));
223 static tree build_try_finally_statement PARAMS ((int, tree, tree));
224 static tree patch_try_statement PARAMS ((tree));
225 static tree patch_synchronized_statement PARAMS ((tree, tree));
226 static tree patch_throw_statement PARAMS ((tree, tree));
227 static void check_thrown_exceptions PARAMS ((int, tree));
228 static int check_thrown_exceptions_do PARAMS ((tree));
229 static void purge_unchecked_exceptions PARAMS ((tree));
230 static bool ctors_unchecked_throws_clause_p PARAMS ((tree));
231 static void check_throws_clauses PARAMS ((tree, tree, tree));
232 static void finish_method_declaration PARAMS ((tree));
233 static tree build_super_invocation PARAMS ((tree));
234 static int verify_constructor_circularity PARAMS ((tree, tree));
235 static char *constructor_circularity_msg PARAMS ((tree, tree));
236 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
237 int, int));
238 static const char *get_printable_method_name PARAMS ((tree));
239 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
240 static tree generate_finit PARAMS ((tree));
241 static tree generate_instinit PARAMS ((tree));
242 static tree build_instinit_invocation PARAMS ((tree));
243 static void fix_constructors PARAMS ((tree));
244 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
245 tree, int *));
246 static void craft_constructor PARAMS ((tree, tree));
247 static int verify_constructor_super PARAMS ((tree));
248 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
249 static void start_artificial_method_body PARAMS ((tree));
250 static void end_artificial_method_body PARAMS ((tree));
251 static int check_method_redefinition PARAMS ((tree, tree));
252 static int check_method_types_complete PARAMS ((tree));
253 static void java_check_regular_methods PARAMS ((tree));
254 static void java_check_abstract_methods PARAMS ((tree));
255 static void unreachable_stmt_error PARAMS ((tree));
256 static tree find_expr_with_wfl PARAMS ((tree));
257 static void missing_return_error PARAMS ((tree));
258 static tree build_new_array_init PARAMS ((int, tree));
259 static tree patch_new_array_init PARAMS ((tree, tree));
260 static tree maybe_build_array_element_wfl PARAMS ((tree));
261 static int array_constructor_check_entry PARAMS ((tree, tree));
262 static const char *purify_type_name PARAMS ((const char *));
263 static tree fold_constant_for_init PARAMS ((tree, tree));
264 static tree strip_out_static_field_access_decl PARAMS ((tree));
265 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
266 static void static_ref_err PARAMS ((tree, tree, tree));
267 static void parser_add_interface PARAMS ((tree, tree, tree));
268 static void add_superinterfaces PARAMS ((tree, tree));
269 static tree jdep_resolve_class PARAMS ((jdep *));
270 static int note_possible_classname PARAMS ((const char *, int));
271 static void java_complete_expand_classes PARAMS ((void));
272 static void java_complete_expand_class PARAMS ((tree));
273 static void java_complete_expand_methods PARAMS ((tree));
274 static tree cut_identifier_in_qualified PARAMS ((tree));
275 static tree java_stabilize_reference PARAMS ((tree));
276 static tree do_unary_numeric_promotion PARAMS ((tree));
277 static char * operator_string PARAMS ((tree));
278 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
279 static tree merge_string_cste PARAMS ((tree, tree, int));
280 static tree java_refold PARAMS ((tree));
281 static int java_decl_equiv PARAMS ((tree, tree));
282 static int binop_compound_p PARAMS ((enum tree_code));
283 static tree search_loop PARAMS ((tree));
284 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
285 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
286 static void java_check_abstract_method_definitions PARAMS ((tree));
287 static void java_debug_context_do PARAMS ((int));
288 static void java_parser_context_push_initialized_field PARAMS ((void));
289 static void java_parser_context_pop_initialized_field PARAMS ((void));
290 static tree reorder_static_initialized PARAMS ((tree));
291 static void java_parser_context_suspend PARAMS ((void));
292 static void java_parser_context_resume PARAMS ((void));
293 static int pop_current_osb PARAMS ((struct parser_ctxt *));
294
295 /* JDK 1.1 work. FIXME */
296
297 static tree maybe_make_nested_class_name PARAMS ((tree));
298 static void make_nested_class_name PARAMS ((tree));
299 static void set_nested_class_simple_name_value PARAMS ((tree, int));
300 static void link_nested_class_to_enclosing PARAMS ((void));
301 static tree resolve_inner_class PARAMS ((struct hash_table *, tree, tree *,
302 tree *, tree));
303 static tree find_as_inner_class PARAMS ((tree, tree, tree));
304 static tree find_as_inner_class_do PARAMS ((tree, tree));
305 static int check_inner_class_redefinition PARAMS ((tree, tree));
306
307 static tree build_thisn_assign PARAMS ((void));
308 static tree build_current_thisn PARAMS ((tree));
309 static tree build_access_to_thisn PARAMS ((tree, tree, int));
310 static tree maybe_build_thisn_access_method PARAMS ((tree));
311
312 static tree build_outer_field_access PARAMS ((tree, tree));
313 static tree build_outer_field_access_methods PARAMS ((tree));
314 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
315 tree, tree));
316 static tree build_outer_method_access_method PARAMS ((tree));
317 static tree build_new_access_id PARAMS ((void));
318 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
319 tree, tree));
320
321 static int outer_field_access_p PARAMS ((tree, tree));
322 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
323 tree *, tree *));
324 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
325 static tree build_incomplete_class_ref PARAMS ((int, tree));
326 static tree patch_incomplete_class_ref PARAMS ((tree));
327 static tree create_anonymous_class PARAMS ((int, tree));
328 static void patch_anonymous_class PARAMS ((tree, tree, tree));
329 static void add_inner_class_fields PARAMS ((tree, tree));
330
331 static tree build_dot_class_method PARAMS ((tree));
332 static tree build_dot_class_method_invocation PARAMS ((tree));
333 static void create_new_parser_context PARAMS ((int));
334 static void mark_parser_ctxt PARAMS ((void *));
335 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
336
337 static bool attach_init_test_initialization_flags PARAMS ((struct hash_entry *,
338 PTR));
339 static bool emit_test_initialization PARAMS ((struct hash_entry *, PTR));
340
341 /* Number of error found so far. */
342 int java_error_count;
343 /* Number of warning found so far. */
344 int java_warning_count;
345 /* Tell when not to fold, when doing xrefs */
346 int do_not_fold;
347 /* Cyclic inheritance report, as it can be set by layout_class */
348 const char *cyclic_inheritance_report;
349
350 /* The current parser context */
351 struct parser_ctxt *ctxp;
352
353 /* List of things that were analyzed for which code will be generated */
354 struct parser_ctxt *ctxp_for_generation = NULL;
355
356 /* binop_lookup maps token to tree_code. It is used where binary
357 operations are involved and required by the parser. RDIV_EXPR
358 covers both integral/floating point division. The code is changed
359 once the type of both operator is worked out. */
360
361 static const enum tree_code binop_lookup[19] =
362 {
363 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
364 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
365 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
366 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
367 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
368 };
369 #define BINOP_LOOKUP(VALUE) \
370 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
371
372 /* This is the end index for binary operators that can also be used
373 in compound assignements. */
374 #define BINOP_COMPOUND_CANDIDATES 11
375
376 /* The "$L" identifier we use to create labels. */
377 static tree label_id = NULL_TREE;
378
379 /* The "StringBuffer" identifier used for the String `+' operator. */
380 static tree wfl_string_buffer = NULL_TREE;
381
382 /* The "append" identifier used for String `+' operator. */
383 static tree wfl_append = NULL_TREE;
384
385 /* The "toString" identifier used for String `+' operator. */
386 static tree wfl_to_string = NULL_TREE;
387
388 /* The "java.lang" import qualified name. */
389 static tree java_lang_id = NULL_TREE;
390
391 /* The generated `inst$' identifier used for generated enclosing
392 instance/field access functions. */
393 static tree inst_id = NULL_TREE;
394
395 /* The "java.lang.Cloneable" qualified name. */
396 static tree java_lang_cloneable = NULL_TREE;
397
398 /* The "java.io.Serializable" qualified name. */
399 static tree java_io_serializable = NULL_TREE;
400
401 /* Context and flag for static blocks */
402 static tree current_static_block = NULL_TREE;
403
404 /* The generated `write_parm_value$' identifier. */
405 static tree wpv_id;
406
407 /* The list of all packages we've seen so far */
408 static tree package_list = NULL_TREE;
409
410 /* Hold THIS for the scope of the current method decl. */
411 static tree current_this;
412
413 /* Hold a list of catch clauses list. The first element of this list is
414 the list of the catch clauses of the currently analysed try block. */
415 static tree currently_caught_type_list;
416
417 /* This holds a linked list of all the case labels for the current
418 switch statement. It is only used when checking to see if there
419 are duplicate labels. FIXME: probably this should just be attached
420 to the switch itself; then it could be referenced via
421 `ctxp->current_loop'. */
422 static tree case_label_list;
423
424 static tree src_parse_roots[1];
425
426 /* All classes seen from source code */
427 #define gclass_list src_parse_roots[0]
428
429 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
430 line and point it out. */
431 /* Should point out the one that don't fit. ASCII/unicode, going
432 backward. FIXME */
433
434 #define check_modifiers(__message, __value, __mask) do { \
435 if ((__value) & ~(__mask)) \
436 { \
437 int i, remainder = (__value) & ~(__mask); \
438 for (i = 0; i <= 10; i++) \
439 if ((1 << i) & remainder) \
440 parse_error_context (ctxp->modifier_ctx [i], (__message), \
441 java_accstring_lookup (1 << i)); \
442 } \
443 } while (0)
444
445 %}
446
447 %union {
448 tree node;
449 int sub_token;
450 struct {
451 int token;
452 int location;
453 } operator;
454 int value;
455 }
456
457 %{
458 #include "lex.c"
459 %}
460
461 %pure_parser
462
463 /* Things defined here have to match the order of what's in the
464 binop_lookup table. */
465
466 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
467 %token LS_TK SRS_TK ZRS_TK
468 %token AND_TK XOR_TK OR_TK
469 %token BOOL_AND_TK BOOL_OR_TK
470 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
471
472 /* This maps to the same binop_lookup entry than the token above */
473
474 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
475 %token REM_ASSIGN_TK
476 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
477 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
478
479
480 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
481
482 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
483 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
484 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
485 %token PAD_TK ABSTRACT_TK MODIFIER_TK
486 %token STRICT_TK
487
488 /* Keep those two in order, too */
489 %token DECR_TK INCR_TK
490
491 /* From now one, things can be in any order */
492
493 %token DEFAULT_TK IF_TK THROW_TK
494 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
495 %token THROWS_TK BREAK_TK IMPORT_TK
496 %token ELSE_TK INSTANCEOF_TK RETURN_TK
497 %token VOID_TK CATCH_TK INTERFACE_TK
498 %token CASE_TK EXTENDS_TK FINALLY_TK
499 %token SUPER_TK WHILE_TK CLASS_TK
500 %token SWITCH_TK CONST_TK TRY_TK
501 %token FOR_TK NEW_TK CONTINUE_TK
502 %token GOTO_TK PACKAGE_TK THIS_TK
503
504 %token BYTE_TK SHORT_TK INT_TK LONG_TK
505 %token CHAR_TK INTEGRAL_TK
506
507 %token FLOAT_TK DOUBLE_TK FP_TK
508
509 %token ID_TK
510
511 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
512
513 %token ASSIGN_ANY_TK ASSIGN_TK
514 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
515
516 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
517 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
518
519 %type <value> modifiers MODIFIER_TK final synchronized
520
521 %type <node> super ID_TK identifier
522 %type <node> name simple_name qualified_name
523 %type <node> type_declaration compilation_unit
524 field_declaration method_declaration extends_interfaces
525 interfaces interface_type_list
526 class_member_declaration
527 import_declarations package_declaration
528 type_declarations interface_body
529 interface_member_declaration constant_declaration
530 interface_member_declarations interface_type
531 abstract_method_declaration interface_type_list
532 %type <node> class_body_declaration class_member_declaration
533 static_initializer constructor_declaration block
534 %type <node> class_body_declarations constructor_header
535 %type <node> class_or_interface_type class_type class_type_list
536 constructor_declarator explicit_constructor_invocation
537 %type <node> dim_expr dim_exprs this_or_super throws
538
539 %type <node> variable_declarator_id variable_declarator
540 variable_declarators variable_initializer
541 variable_initializers constructor_body
542 array_initializer
543
544 %type <node> class_body block_end constructor_block_end
545 %type <node> statement statement_without_trailing_substatement
546 labeled_statement if_then_statement label_decl
547 if_then_else_statement while_statement for_statement
548 statement_nsi labeled_statement_nsi do_statement
549 if_then_else_statement_nsi while_statement_nsi
550 for_statement_nsi statement_expression_list for_init
551 for_update statement_expression expression_statement
552 primary_no_new_array expression primary
553 array_creation_expression array_type
554 class_instance_creation_expression field_access
555 method_invocation array_access something_dot_new
556 argument_list postfix_expression while_expression
557 post_increment_expression post_decrement_expression
558 unary_expression_not_plus_minus unary_expression
559 pre_increment_expression pre_decrement_expression
560 unary_expression_not_plus_minus cast_expression
561 multiplicative_expression additive_expression
562 shift_expression relational_expression
563 equality_expression and_expression
564 exclusive_or_expression inclusive_or_expression
565 conditional_and_expression conditional_or_expression
566 conditional_expression assignment_expression
567 left_hand_side assignment for_header for_begin
568 constant_expression do_statement_begin empty_statement
569 switch_statement synchronized_statement throw_statement
570 try_statement switch_expression switch_block
571 catches catch_clause catch_clause_parameter finally
572 anonymous_class_creation trap_overflow_corner_case
573 %type <node> return_statement break_statement continue_statement
574
575 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
576 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
577 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
578 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
579 %type <operator> ASSIGN_ANY_TK assignment_operator
580 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
581 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
582 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
583 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
584 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
585 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
586 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
587 %type <operator> NEW_TK
588
589 %type <node> method_body
590
591 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
592 STRING_LIT_TK NULL_TK VOID_TK
593
594 %type <node> IF_TK WHILE_TK FOR_TK
595
596 %type <node> formal_parameter_list formal_parameter
597 method_declarator method_header
598
599 %type <node> primitive_type reference_type type
600 BOOLEAN_TK INTEGRAL_TK FP_TK
601
602 /* Added or modified JDK 1.1 rule types */
603 %type <node> type_literals
604
605 %%
606 /* 19.2 Production from 2.3: The Syntactic Grammar */
607 goal:
608 {
609 /* Register static variables with the garbage
610 collector. */
611 ggc_add_tree_root (&label_id, 1);
612 ggc_add_tree_root (&wfl_string_buffer, 1);
613 ggc_add_tree_root (&wfl_append, 1);
614 ggc_add_tree_root (&wfl_to_string, 1);
615 ggc_add_tree_root (&java_lang_id, 1);
616 ggc_add_tree_root (&inst_id, 1);
617 ggc_add_tree_root (&java_lang_cloneable, 1);
618 ggc_add_tree_root (&java_io_serializable, 1);
619 ggc_add_tree_root (&current_static_block, 1);
620 ggc_add_tree_root (&wpv_id, 1);
621 ggc_add_tree_root (&package_list, 1);
622 ggc_add_tree_root (&current_this, 1);
623 ggc_add_tree_root (&currently_caught_type_list, 1);
624 ggc_add_tree_root (&case_label_list, 1);
625 ggc_add_root (&ctxp, 1,
626 sizeof (struct parser_ctxt *),
627 mark_parser_ctxt);
628 ggc_add_root (&ctxp_for_generation, 1,
629 sizeof (struct parser_ctxt *),
630 mark_parser_ctxt);
631 }
632 compilation_unit
633 {}
634 ;
635
636 /* 19.3 Productions from 3: Lexical structure */
637 literal:
638 INT_LIT_TK
639 | FP_LIT_TK
640 | BOOL_LIT_TK
641 | CHAR_LIT_TK
642 | STRING_LIT_TK
643 | NULL_TK
644 ;
645
646 /* 19.4 Productions from 4: Types, Values and Variables */
647 type:
648 primitive_type
649 | reference_type
650 ;
651
652 primitive_type:
653 INTEGRAL_TK
654 | FP_TK
655 | BOOLEAN_TK
656 ;
657
658 reference_type:
659 class_or_interface_type
660 | array_type
661 ;
662
663 class_or_interface_type:
664 name
665 ;
666
667 class_type:
668 class_or_interface_type /* Default rule */
669 ;
670
671 interface_type:
672 class_or_interface_type
673 ;
674
675 array_type:
676 primitive_type dims
677 {
678 int osb = pop_current_osb (ctxp);
679 tree t = build_java_array_type (($1), -1);
680 while (--osb)
681 t = build_unresolved_array_type (t);
682 $$ = t;
683 }
684 | name dims
685 {
686 int osb = pop_current_osb (ctxp);
687 tree t = $1;
688 while (osb--)
689 t = build_unresolved_array_type (t);
690 $$ = t;
691 }
692 ;
693
694 /* 19.5 Productions from 6: Names */
695 name:
696 simple_name /* Default rule */
697 | qualified_name /* Default rule */
698 ;
699
700 simple_name:
701 identifier /* Default rule */
702 ;
703
704 qualified_name:
705 name DOT_TK identifier
706 { $$ = make_qualified_name ($1, $3, $2.location); }
707 ;
708
709 identifier:
710 ID_TK
711 ;
712
713 /* 19.6: Production from 7: Packages */
714 compilation_unit:
715 {$$ = NULL;}
716 | package_declaration
717 | import_declarations
718 | type_declarations
719 | package_declaration import_declarations
720 | package_declaration type_declarations
721 | import_declarations type_declarations
722 | package_declaration import_declarations type_declarations
723 ;
724
725 import_declarations:
726 import_declaration
727 {
728 $$ = NULL;
729 }
730 | import_declarations import_declaration
731 {
732 $$ = NULL;
733 }
734 ;
735
736 type_declarations:
737 type_declaration
738 | type_declarations type_declaration
739 ;
740
741 package_declaration:
742 PACKAGE_TK name SC_TK
743 {
744 ctxp->package = EXPR_WFL_NODE ($2);
745 register_package (ctxp->package);
746 }
747 | PACKAGE_TK error
748 {yyerror ("Missing name"); RECOVER;}
749 | PACKAGE_TK name error
750 {yyerror ("';' expected"); RECOVER;}
751 ;
752
753 import_declaration:
754 single_type_import_declaration
755 | type_import_on_demand_declaration
756 ;
757
758 single_type_import_declaration:
759 IMPORT_TK name SC_TK
760 {
761 tree name = EXPR_WFL_NODE ($2), last_name;
762 int i = IDENTIFIER_LENGTH (name)-1;
763 const char *last = &IDENTIFIER_POINTER (name)[i];
764 while (last != IDENTIFIER_POINTER (name))
765 {
766 if (last [0] == '.')
767 break;
768 last--;
769 }
770 last_name = get_identifier (++last);
771 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
772 {
773 tree err = find_name_in_single_imports (last_name);
774 if (err && err != name)
775 parse_error_context
776 ($2, "Ambiguous class: `%s' and `%s'",
777 IDENTIFIER_POINTER (name),
778 IDENTIFIER_POINTER (err));
779 else
780 REGISTER_IMPORT ($2, last_name);
781 }
782 else
783 REGISTER_IMPORT ($2, last_name);
784 }
785 | IMPORT_TK error
786 {yyerror ("Missing name"); RECOVER;}
787 | IMPORT_TK name error
788 {yyerror ("';' expected"); RECOVER;}
789 ;
790
791 type_import_on_demand_declaration:
792 IMPORT_TK name DOT_TK MULT_TK SC_TK
793 {
794 tree name = EXPR_WFL_NODE ($2);
795 tree it;
796 /* Search for duplicates. */
797 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
798 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
799 break;
800 /* Don't import the same thing more than once, just ignore
801 duplicates (7.5.2) */
802 if (! it)
803 {
804 read_import_dir ($2);
805 ctxp->import_demand_list =
806 chainon (ctxp->import_demand_list,
807 build_tree_list ($2, NULL_TREE));
808 }
809 }
810 | IMPORT_TK name DOT_TK error
811 {yyerror ("'*' expected"); RECOVER;}
812 | IMPORT_TK name DOT_TK MULT_TK error
813 {yyerror ("';' expected"); RECOVER;}
814 ;
815
816 type_declaration:
817 class_declaration
818 { end_class_declaration (0); }
819 | interface_declaration
820 { end_class_declaration (0); }
821 | empty_statement
822 | error
823 {
824 YYERROR_NOW;
825 yyerror ("Class or interface declaration expected");
826 }
827 ;
828
829 /* 19.7 Shortened from the original:
830 modifiers: modifier | modifiers modifier
831 modifier: any of public... */
832 modifiers:
833 MODIFIER_TK
834 {
835 $$ = (1 << $1);
836 }
837 | modifiers MODIFIER_TK
838 {
839 int acc = (1 << $2);
840 if ($$ & acc)
841 parse_error_context
842 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
843 java_accstring_lookup (acc));
844 else
845 {
846 $$ |= acc;
847 }
848 }
849 ;
850
851 /* 19.8.1 Production from $8.1: Class Declaration */
852 class_declaration:
853 modifiers CLASS_TK identifier super interfaces
854 { create_class ($1, $3, $4, $5); }
855 class_body
856 | CLASS_TK identifier super interfaces
857 { create_class (0, $2, $3, $4); }
858 class_body
859 | modifiers CLASS_TK error
860 {yyerror ("Missing class name"); RECOVER;}
861 | CLASS_TK error
862 {yyerror ("Missing class name"); RECOVER;}
863 | CLASS_TK identifier error
864 {
865 if (!ctxp->class_err) yyerror ("'{' expected");
866 DRECOVER(class1);
867 }
868 | modifiers CLASS_TK identifier error
869 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
870 ;
871
872 super:
873 { $$ = NULL; }
874 | EXTENDS_TK class_type
875 { $$ = $2; }
876 | EXTENDS_TK class_type error
877 {yyerror ("'{' expected"); ctxp->class_err=1;}
878 | EXTENDS_TK error
879 {yyerror ("Missing super class name"); ctxp->class_err=1;}
880 ;
881
882 interfaces:
883 { $$ = NULL_TREE; }
884 | IMPLEMENTS_TK interface_type_list
885 { $$ = $2; }
886 | IMPLEMENTS_TK error
887 {
888 ctxp->class_err=1;
889 yyerror ("Missing interface name");
890 }
891 ;
892
893 interface_type_list:
894 interface_type
895 {
896 ctxp->interface_number = 1;
897 $$ = build_tree_list ($1, NULL_TREE);
898 }
899 | interface_type_list C_TK interface_type
900 {
901 ctxp->interface_number++;
902 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
903 }
904 | interface_type_list C_TK error
905 {yyerror ("Missing interface name"); RECOVER;}
906 ;
907
908 class_body:
909 OCB_TK CCB_TK
910 {
911 /* Store the location of the `}' when doing xrefs */
912 if (flag_emit_xref)
913 DECL_END_SOURCE_LINE (GET_CPC ()) =
914 EXPR_WFL_ADD_COL ($2.location, 1);
915 $$ = GET_CPC ();
916 }
917 | OCB_TK class_body_declarations CCB_TK
918 {
919 /* Store the location of the `}' when doing xrefs */
920 if (flag_emit_xref)
921 DECL_END_SOURCE_LINE (GET_CPC ()) =
922 EXPR_WFL_ADD_COL ($3.location, 1);
923 $$ = GET_CPC ();
924 }
925 ;
926
927 class_body_declarations:
928 class_body_declaration
929 | class_body_declarations class_body_declaration
930 ;
931
932 class_body_declaration:
933 class_member_declaration
934 | static_initializer
935 | constructor_declaration
936 | block /* Added, JDK1.1, instance initializer */
937 {
938 if ($1 != empty_stmt_node)
939 {
940 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
941 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
942 }
943 }
944 ;
945
946 class_member_declaration:
947 field_declaration
948 | method_declaration
949 | class_declaration /* Added, JDK1.1 inner classes */
950 { end_class_declaration (1); }
951 | interface_declaration /* Added, JDK1.1 inner interfaces */
952 { end_class_declaration (1); }
953 | empty_statement
954 ;
955
956 /* 19.8.2 Productions from 8.3: Field Declarations */
957 field_declaration:
958 type variable_declarators SC_TK
959 { register_fields (0, $1, $2); }
960 | modifiers type variable_declarators SC_TK
961 {
962 check_modifiers
963 ("Illegal modifier `%s' for field declaration",
964 $1, FIELD_MODIFIERS);
965 check_modifiers_consistency ($1);
966 register_fields ($1, $2, $3);
967 }
968 ;
969
970 variable_declarators:
971 /* Should we use build_decl_list () instead ? FIXME */
972 variable_declarator /* Default rule */
973 | variable_declarators C_TK variable_declarator
974 { $$ = chainon ($1, $3); }
975 | variable_declarators C_TK error
976 {yyerror ("Missing term"); RECOVER;}
977 ;
978
979 variable_declarator:
980 variable_declarator_id
981 { $$ = build_tree_list ($1, NULL_TREE); }
982 | variable_declarator_id ASSIGN_TK variable_initializer
983 {
984 if (java_error_count)
985 $3 = NULL_TREE;
986 $$ = build_tree_list
987 ($1, build_assignment ($2.token, $2.location, $1, $3));
988 }
989 | variable_declarator_id ASSIGN_TK error
990 {
991 yyerror ("Missing variable initializer");
992 $$ = build_tree_list ($1, NULL_TREE);
993 RECOVER;
994 }
995 | variable_declarator_id ASSIGN_TK variable_initializer error
996 {
997 yyerror ("';' expected");
998 $$ = build_tree_list ($1, NULL_TREE);
999 RECOVER;
1000 }
1001 ;
1002
1003 variable_declarator_id:
1004 identifier
1005 | variable_declarator_id OSB_TK CSB_TK
1006 { $$ = build_unresolved_array_type ($1); }
1007 | identifier error
1008 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
1009 | variable_declarator_id OSB_TK error
1010 {
1011 tree node = java_lval.node;
1012 if (node && (TREE_CODE (node) == INTEGER_CST
1013 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
1014 yyerror ("Can't specify array dimension in a declaration");
1015 else
1016 yyerror ("']' expected");
1017 DRECOVER(vdi);
1018 }
1019 | variable_declarator_id CSB_TK error
1020 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1021 ;
1022
1023 variable_initializer:
1024 expression
1025 | array_initializer
1026 ;
1027
1028 /* 19.8.3 Productions from 8.4: Method Declarations */
1029 method_declaration:
1030 method_header
1031 {
1032 current_function_decl = $1;
1033 if (current_function_decl
1034 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1035 source_start_java_method (current_function_decl);
1036 else
1037 current_function_decl = NULL_TREE;
1038 }
1039 method_body
1040 { finish_method_declaration ($3); }
1041 | method_header error
1042 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1043 ;
1044
1045 method_header:
1046 type method_declarator throws
1047 { $$ = method_header (0, $1, $2, $3); }
1048 | VOID_TK method_declarator throws
1049 { $$ = method_header (0, void_type_node, $2, $3); }
1050 | modifiers type method_declarator throws
1051 { $$ = method_header ($1, $2, $3, $4); }
1052 | modifiers VOID_TK method_declarator throws
1053 { $$ = method_header ($1, void_type_node, $3, $4); }
1054 | type error
1055 {
1056 yyerror ("Invalid method declaration, method name required");
1057 RECOVER;
1058 }
1059 | modifiers type error
1060 {RECOVER;}
1061 | VOID_TK error
1062 {yyerror ("Identifier expected"); RECOVER;}
1063 | modifiers VOID_TK error
1064 {yyerror ("Identifier expected"); RECOVER;}
1065 | modifiers error
1066 {
1067 yyerror ("Invalid method declaration, return type required");
1068 RECOVER;
1069 }
1070 ;
1071
1072 method_declarator:
1073 identifier OP_TK CP_TK
1074 {
1075 ctxp->formal_parameter_number = 0;
1076 $$ = method_declarator ($1, NULL_TREE);
1077 }
1078 | identifier OP_TK formal_parameter_list CP_TK
1079 { $$ = method_declarator ($1, $3); }
1080 | method_declarator OSB_TK CSB_TK
1081 {
1082 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1083 TREE_PURPOSE ($1) =
1084 build_unresolved_array_type (TREE_PURPOSE ($1));
1085 parse_warning_context
1086 (wfl_operator,
1087 "Discouraged form of returned type specification");
1088 }
1089 | identifier OP_TK error
1090 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1091 | method_declarator OSB_TK error
1092 {yyerror ("']' expected"); RECOVER;}
1093 ;
1094
1095 formal_parameter_list:
1096 formal_parameter
1097 {
1098 ctxp->formal_parameter_number = 1;
1099 }
1100 | formal_parameter_list C_TK formal_parameter
1101 {
1102 ctxp->formal_parameter_number += 1;
1103 $$ = chainon ($1, $3);
1104 }
1105 | formal_parameter_list C_TK error
1106 { yyerror ("Missing formal parameter term"); RECOVER; }
1107 ;
1108
1109 formal_parameter:
1110 type variable_declarator_id
1111 {
1112 $$ = build_tree_list ($2, $1);
1113 }
1114 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1115 {
1116 $$ = build_tree_list ($3, $2);
1117 ARG_FINAL_P ($$) = 1;
1118 }
1119 | type error
1120 {
1121 yyerror ("Missing identifier"); RECOVER;
1122 $$ = NULL_TREE;
1123 }
1124 | final type error
1125 {
1126 yyerror ("Missing identifier"); RECOVER;
1127 $$ = NULL_TREE;
1128 }
1129 ;
1130
1131 final:
1132 modifiers
1133 {
1134 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1135 $1, ACC_FINAL);
1136 if ($1 != ACC_FINAL)
1137 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1138 }
1139 ;
1140
1141 throws:
1142 { $$ = NULL_TREE; }
1143 | THROWS_TK class_type_list
1144 { $$ = $2; }
1145 | THROWS_TK error
1146 {yyerror ("Missing class type term"); RECOVER;}
1147 ;
1148
1149 class_type_list:
1150 class_type
1151 { $$ = build_tree_list ($1, $1); }
1152 | class_type_list C_TK class_type
1153 { $$ = tree_cons ($3, $3, $1); }
1154 | class_type_list C_TK error
1155 {yyerror ("Missing class type term"); RECOVER;}
1156 ;
1157
1158 method_body:
1159 block
1160 | SC_TK { $$ = NULL_TREE; }
1161 ;
1162
1163 /* 19.8.4 Productions from 8.5: Static Initializers */
1164 static_initializer:
1165 static block
1166 {
1167 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1168 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1169 current_static_block = NULL_TREE;
1170 }
1171 ;
1172
1173 static: /* Test lval.sub_token here */
1174 modifiers
1175 {
1176 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1177 /* Can't have a static initializer in an innerclass */
1178 if ($1 | ACC_STATIC &&
1179 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1180 parse_error_context
1181 (MODIFIER_WFL (STATIC_TK),
1182 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1183 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1184 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1185 }
1186 ;
1187
1188 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1189 constructor_declaration:
1190 constructor_header
1191 {
1192 current_function_decl = $1;
1193 source_start_java_method (current_function_decl);
1194 }
1195 constructor_body
1196 { finish_method_declaration ($3); }
1197 ;
1198
1199 constructor_header:
1200 constructor_declarator throws
1201 { $$ = method_header (0, NULL_TREE, $1, $2); }
1202 | modifiers constructor_declarator throws
1203 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1204 ;
1205
1206 constructor_declarator:
1207 simple_name OP_TK CP_TK
1208 {
1209 ctxp->formal_parameter_number = 0;
1210 $$ = method_declarator ($1, NULL_TREE);
1211 }
1212 | simple_name OP_TK formal_parameter_list CP_TK
1213 { $$ = method_declarator ($1, $3); }
1214 ;
1215
1216 constructor_body:
1217 /* Unlike regular method, we always need a complete (empty)
1218 body so we can safely perform all the required code
1219 addition (super invocation and field initialization) */
1220 block_begin constructor_block_end
1221 {
1222 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1223 $$ = $2;
1224 }
1225 | block_begin explicit_constructor_invocation constructor_block_end
1226 { $$ = $3; }
1227 | block_begin block_statements constructor_block_end
1228 { $$ = $3; }
1229 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1230 { $$ = $4; }
1231 ;
1232
1233 constructor_block_end:
1234 block_end
1235 ;
1236
1237 /* Error recovery for that rule moved down expression_statement: rule. */
1238 explicit_constructor_invocation:
1239 this_or_super OP_TK CP_TK SC_TK
1240 {
1241 $$ = build_method_invocation ($1, NULL_TREE);
1242 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1243 $$ = java_method_add_stmt (current_function_decl, $$);
1244 }
1245 | this_or_super OP_TK argument_list CP_TK SC_TK
1246 {
1247 $$ = build_method_invocation ($1, $3);
1248 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1249 $$ = java_method_add_stmt (current_function_decl, $$);
1250 }
1251 /* Added, JDK1.1 inner classes. Modified because the rule
1252 'primary' couldn't work. */
1253 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1254 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1255 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1256 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1257 ;
1258
1259 this_or_super: /* Added, simplifies error diagnostics */
1260 THIS_TK
1261 {
1262 tree wfl = build_wfl_node (this_identifier_node);
1263 EXPR_WFL_LINECOL (wfl) = $1.location;
1264 $$ = wfl;
1265 }
1266 | SUPER_TK
1267 {
1268 tree wfl = build_wfl_node (super_identifier_node);
1269 EXPR_WFL_LINECOL (wfl) = $1.location;
1270 $$ = wfl;
1271 }
1272 ;
1273
1274 /* 19.9 Productions from 9: Interfaces */
1275 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1276 interface_declaration:
1277 INTERFACE_TK identifier
1278 { create_interface (0, $2, NULL_TREE); }
1279 interface_body
1280 | modifiers INTERFACE_TK identifier
1281 { create_interface ($1, $3, NULL_TREE); }
1282 interface_body
1283 | INTERFACE_TK identifier extends_interfaces
1284 { create_interface (0, $2, $3); }
1285 interface_body
1286 | modifiers INTERFACE_TK identifier extends_interfaces
1287 { create_interface ($1, $3, $4); }
1288 interface_body
1289 | INTERFACE_TK identifier error
1290 {yyerror ("'{' expected"); RECOVER;}
1291 | modifiers INTERFACE_TK identifier error
1292 {yyerror ("'{' expected"); RECOVER;}
1293 ;
1294
1295 extends_interfaces:
1296 EXTENDS_TK interface_type
1297 {
1298 ctxp->interface_number = 1;
1299 $$ = build_tree_list ($2, NULL_TREE);
1300 }
1301 | extends_interfaces C_TK interface_type
1302 {
1303 ctxp->interface_number++;
1304 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1305 }
1306 | EXTENDS_TK error
1307 {yyerror ("Invalid interface type"); RECOVER;}
1308 | extends_interfaces C_TK error
1309 {yyerror ("Missing term"); RECOVER;}
1310 ;
1311
1312 interface_body:
1313 OCB_TK CCB_TK
1314 { $$ = NULL_TREE; }
1315 | OCB_TK interface_member_declarations CCB_TK
1316 { $$ = NULL_TREE; }
1317 ;
1318
1319 interface_member_declarations:
1320 interface_member_declaration
1321 | interface_member_declarations interface_member_declaration
1322 ;
1323
1324 interface_member_declaration:
1325 constant_declaration
1326 | abstract_method_declaration
1327 | class_declaration /* Added, JDK1.1 inner classes */
1328 { end_class_declaration (1); }
1329 | interface_declaration /* Added, JDK1.1 inner interfaces */
1330 { end_class_declaration (1); }
1331 ;
1332
1333 constant_declaration:
1334 field_declaration
1335 ;
1336
1337 abstract_method_declaration:
1338 method_header SC_TK
1339 {
1340 check_abstract_method_header ($1);
1341 current_function_decl = NULL_TREE; /* FIXME ? */
1342 }
1343 | method_header error
1344 {yyerror ("';' expected"); RECOVER;}
1345 ;
1346
1347 /* 19.10 Productions from 10: Arrays */
1348 array_initializer:
1349 OCB_TK CCB_TK
1350 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1351 | OCB_TK C_TK CCB_TK
1352 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1353 | OCB_TK variable_initializers CCB_TK
1354 { $$ = build_new_array_init ($1.location, $2); }
1355 | OCB_TK variable_initializers C_TK CCB_TK
1356 { $$ = build_new_array_init ($1.location, $2); }
1357 ;
1358
1359 variable_initializers:
1360 variable_initializer
1361 {
1362 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1363 $1, NULL_TREE);
1364 }
1365 | variable_initializers C_TK variable_initializer
1366 {
1367 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1368 }
1369 | variable_initializers C_TK error
1370 {yyerror ("Missing term"); RECOVER;}
1371 ;
1372
1373 /* 19.11 Production from 14: Blocks and Statements */
1374 block:
1375 OCB_TK CCB_TK
1376 {
1377 /* Store the location of the `}' when doing xrefs */
1378 if (current_function_decl && flag_emit_xref)
1379 DECL_END_SOURCE_LINE (current_function_decl) =
1380 EXPR_WFL_ADD_COL ($2.location, 1);
1381 $$ = empty_stmt_node;
1382 }
1383 | block_begin block_statements block_end
1384 { $$ = $3; }
1385 ;
1386
1387 block_begin:
1388 OCB_TK
1389 { enter_block (); }
1390 ;
1391
1392 block_end:
1393 CCB_TK
1394 {
1395 maybe_absorb_scoping_blocks ();
1396 /* Store the location of the `}' when doing xrefs */
1397 if (current_function_decl && flag_emit_xref)
1398 DECL_END_SOURCE_LINE (current_function_decl) =
1399 EXPR_WFL_ADD_COL ($1.location, 1);
1400 $$ = exit_block ();
1401 if (!BLOCK_SUBBLOCKS ($$))
1402 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1403 }
1404 ;
1405
1406 block_statements:
1407 block_statement
1408 | block_statements block_statement
1409 ;
1410
1411 block_statement:
1412 local_variable_declaration_statement
1413 | statement
1414 { java_method_add_stmt (current_function_decl, $1); }
1415 | class_declaration /* Added, JDK1.1 local classes */
1416 {
1417 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1418 end_class_declaration (1);
1419 }
1420 ;
1421
1422 local_variable_declaration_statement:
1423 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1424 ;
1425
1426 local_variable_declaration:
1427 type variable_declarators
1428 { declare_local_variables (0, $1, $2); }
1429 | final type variable_declarators /* Added, JDK1.1 final locals */
1430 { declare_local_variables ($1, $2, $3); }
1431 ;
1432
1433 statement:
1434 statement_without_trailing_substatement
1435 | labeled_statement
1436 | if_then_statement
1437 | if_then_else_statement
1438 | while_statement
1439 | for_statement
1440 { $$ = exit_block (); }
1441 ;
1442
1443 statement_nsi:
1444 statement_without_trailing_substatement
1445 | labeled_statement_nsi
1446 | if_then_else_statement_nsi
1447 | while_statement_nsi
1448 | for_statement_nsi
1449 { $$ = exit_block (); }
1450 ;
1451
1452 statement_without_trailing_substatement:
1453 block
1454 | empty_statement
1455 | expression_statement
1456 | switch_statement
1457 | do_statement
1458 | break_statement
1459 | continue_statement
1460 | return_statement
1461 | synchronized_statement
1462 | throw_statement
1463 | try_statement
1464 ;
1465
1466 empty_statement:
1467 SC_TK
1468 {
1469 if (flag_extraneous_semicolon
1470 && ! current_static_block
1471 && (! current_function_decl ||
1472 /* Verify we're not in a inner class declaration */
1473 (GET_CPC () != TYPE_NAME
1474 (DECL_CONTEXT (current_function_decl)))))
1475
1476 {
1477 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1478 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1479 }
1480 $$ = empty_stmt_node;
1481 }
1482 ;
1483
1484 label_decl:
1485 identifier REL_CL_TK
1486 {
1487 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1488 EXPR_WFL_NODE ($1));
1489 pushlevel (2);
1490 push_labeled_block ($$);
1491 PUSH_LABELED_BLOCK ($$);
1492 }
1493 ;
1494
1495 labeled_statement:
1496 label_decl statement
1497 { $$ = finish_labeled_statement ($1, $2); }
1498 | identifier error
1499 {yyerror ("':' expected"); RECOVER;}
1500 ;
1501
1502 labeled_statement_nsi:
1503 label_decl statement_nsi
1504 { $$ = finish_labeled_statement ($1, $2); }
1505 ;
1506
1507 /* We concentrate here a bunch of error handling rules that we couldn't write
1508 earlier, because expression_statement catches a missing ';'. */
1509 expression_statement:
1510 statement_expression SC_TK
1511 {
1512 /* We have a statement. Generate a WFL around it so
1513 we can debug it */
1514 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1515 /* We know we have a statement, so set the debug
1516 info to be eventually generate here. */
1517 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1518 }
1519 | error SC_TK
1520 {
1521 YYNOT_TWICE yyerror ("Invalid expression statement");
1522 DRECOVER (expr_stmt);
1523 }
1524 | error OCB_TK
1525 {
1526 YYNOT_TWICE yyerror ("Invalid expression statement");
1527 DRECOVER (expr_stmt);
1528 }
1529 | error CCB_TK
1530 {
1531 YYNOT_TWICE yyerror ("Invalid expression statement");
1532 DRECOVER (expr_stmt);
1533 }
1534 | this_or_super OP_TK error
1535 {yyerror ("')' expected"); RECOVER;}
1536 | this_or_super OP_TK CP_TK error
1537 {
1538 parse_ctor_invocation_error ();
1539 RECOVER;
1540 }
1541 | this_or_super OP_TK argument_list error
1542 {yyerror ("')' expected"); RECOVER;}
1543 | this_or_super OP_TK argument_list CP_TK error
1544 {
1545 parse_ctor_invocation_error ();
1546 RECOVER;
1547 }
1548 | name DOT_TK SUPER_TK error
1549 {yyerror ("'(' expected"); RECOVER;}
1550 | name DOT_TK SUPER_TK OP_TK error
1551 {yyerror ("')' expected"); RECOVER;}
1552 | name DOT_TK SUPER_TK OP_TK argument_list error
1553 {yyerror ("')' expected"); RECOVER;}
1554 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1555 {yyerror ("';' expected"); RECOVER;}
1556 | name DOT_TK SUPER_TK OP_TK CP_TK error
1557 {yyerror ("';' expected"); RECOVER;}
1558 ;
1559
1560 statement_expression:
1561 assignment
1562 | pre_increment_expression
1563 | pre_decrement_expression
1564 | post_increment_expression
1565 | post_decrement_expression
1566 | method_invocation
1567 | class_instance_creation_expression
1568 ;
1569
1570 if_then_statement:
1571 IF_TK OP_TK expression CP_TK statement
1572 {
1573 $$ = build_if_else_statement ($2.location, $3,
1574 $5, NULL_TREE);
1575 }
1576 | IF_TK error
1577 {yyerror ("'(' expected"); RECOVER;}
1578 | IF_TK OP_TK error
1579 {yyerror ("Missing term"); RECOVER;}
1580 | IF_TK OP_TK expression error
1581 {yyerror ("')' expected"); RECOVER;}
1582 ;
1583
1584 if_then_else_statement:
1585 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1586 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1587 ;
1588
1589 if_then_else_statement_nsi:
1590 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1591 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1592 ;
1593
1594 switch_statement:
1595 switch_expression
1596 {
1597 enter_block ();
1598 }
1599 switch_block
1600 {
1601 /* Make into "proper list" of COMPOUND_EXPRs.
1602 I.e. make the last statement also have its own
1603 COMPOUND_EXPR. */
1604 maybe_absorb_scoping_blocks ();
1605 TREE_OPERAND ($1, 1) = exit_block ();
1606 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1607 }
1608 ;
1609
1610 switch_expression:
1611 SWITCH_TK OP_TK expression CP_TK
1612 {
1613 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1614 EXPR_WFL_LINECOL ($$) = $2.location;
1615 }
1616 | SWITCH_TK error
1617 {yyerror ("'(' expected"); RECOVER;}
1618 | SWITCH_TK OP_TK error
1619 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1620 | SWITCH_TK OP_TK expression CP_TK error
1621 {yyerror ("'{' expected"); RECOVER;}
1622 ;
1623
1624 /* Default assignment is there to avoid type node on switch_block
1625 node. */
1626
1627 switch_block:
1628 OCB_TK CCB_TK
1629 { $$ = NULL_TREE; }
1630 | OCB_TK switch_labels CCB_TK
1631 { $$ = NULL_TREE; }
1632 | OCB_TK switch_block_statement_groups CCB_TK
1633 { $$ = NULL_TREE; }
1634 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1635 { $$ = NULL_TREE; }
1636 ;
1637
1638 switch_block_statement_groups:
1639 switch_block_statement_group
1640 | switch_block_statement_groups switch_block_statement_group
1641 ;
1642
1643 switch_block_statement_group:
1644 switch_labels block_statements
1645 ;
1646
1647 switch_labels:
1648 switch_label
1649 | switch_labels switch_label
1650 ;
1651
1652 switch_label:
1653 CASE_TK constant_expression REL_CL_TK
1654 {
1655 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1656 EXPR_WFL_LINECOL (lab) = $1.location;
1657 java_method_add_stmt (current_function_decl, lab);
1658 }
1659 | DEFAULT_TK REL_CL_TK
1660 {
1661 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1662 EXPR_WFL_LINECOL (lab) = $1.location;
1663 java_method_add_stmt (current_function_decl, lab);
1664 }
1665 | CASE_TK error
1666 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1667 | CASE_TK constant_expression error
1668 {yyerror ("':' expected"); RECOVER;}
1669 | DEFAULT_TK error
1670 {yyerror ("':' expected"); RECOVER;}
1671 ;
1672
1673 while_expression:
1674 WHILE_TK OP_TK expression CP_TK
1675 {
1676 tree body = build_loop_body ($2.location, $3, 0);
1677 $$ = build_new_loop (body);
1678 }
1679 ;
1680
1681 while_statement:
1682 while_expression statement
1683 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1684 | WHILE_TK error
1685 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1686 | WHILE_TK OP_TK error
1687 {yyerror ("Missing term and ')' expected"); RECOVER;}
1688 | WHILE_TK OP_TK expression error
1689 {yyerror ("')' expected"); RECOVER;}
1690 ;
1691
1692 while_statement_nsi:
1693 while_expression statement_nsi
1694 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1695 ;
1696
1697 do_statement_begin:
1698 DO_TK
1699 {
1700 tree body = build_loop_body (0, NULL_TREE, 1);
1701 $$ = build_new_loop (body);
1702 }
1703 /* Need error handing here. FIXME */
1704 ;
1705
1706 do_statement:
1707 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1708 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1709 ;
1710
1711 for_statement:
1712 for_begin SC_TK expression SC_TK for_update CP_TK statement
1713 {
1714 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1715 $3 = build_wfl_node ($3);
1716 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1717 }
1718 | for_begin SC_TK SC_TK for_update CP_TK statement
1719 {
1720 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1721 /* We have not condition, so we get rid of the EXIT_EXPR */
1722 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1723 empty_stmt_node;
1724 }
1725 | for_begin SC_TK error
1726 {yyerror ("Invalid control expression"); RECOVER;}
1727 | for_begin SC_TK expression SC_TK error
1728 {yyerror ("Invalid update expression"); RECOVER;}
1729 | for_begin SC_TK SC_TK error
1730 {yyerror ("Invalid update expression"); RECOVER;}
1731 ;
1732
1733 for_statement_nsi:
1734 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1735 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1736 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1737 {
1738 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1739 /* We have not condition, so we get rid of the EXIT_EXPR */
1740 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1741 empty_stmt_node;
1742 }
1743 ;
1744
1745 for_header:
1746 FOR_TK OP_TK
1747 {
1748 /* This scope defined for local variable that may be
1749 defined within the scope of the for loop */
1750 enter_block ();
1751 }
1752 | FOR_TK error
1753 {yyerror ("'(' expected"); DRECOVER(for_1);}
1754 | FOR_TK OP_TK error
1755 {yyerror ("Invalid init statement"); RECOVER;}
1756 ;
1757
1758 for_begin:
1759 for_header for_init
1760 {
1761 /* We now declare the loop body. The loop is
1762 declared as a for loop. */
1763 tree body = build_loop_body (0, NULL_TREE, 0);
1764 $$ = build_new_loop (body);
1765 FOR_LOOP_P ($$) = 1;
1766 /* The loop is added to the current block the for
1767 statement is defined within */
1768 java_method_add_stmt (current_function_decl, $$);
1769 }
1770 ;
1771 for_init: /* Can be empty */
1772 { $$ = empty_stmt_node; }
1773 | statement_expression_list
1774 {
1775 /* Init statement recorded within the previously
1776 defined block scope */
1777 $$ = java_method_add_stmt (current_function_decl, $1);
1778 }
1779 | local_variable_declaration
1780 {
1781 /* Local variable are recorded within the previously
1782 defined block scope */
1783 $$ = NULL_TREE;
1784 }
1785 | statement_expression_list error
1786 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1787 ;
1788
1789 for_update: /* Can be empty */
1790 {$$ = empty_stmt_node;}
1791 | statement_expression_list
1792 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1793 ;
1794
1795 statement_expression_list:
1796 statement_expression
1797 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1798 | statement_expression_list C_TK statement_expression
1799 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1800 | statement_expression_list C_TK error
1801 {yyerror ("Missing term"); RECOVER;}
1802 ;
1803
1804 break_statement:
1805 BREAK_TK SC_TK
1806 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1807 | BREAK_TK identifier SC_TK
1808 { $$ = build_bc_statement ($1.location, 1, $2); }
1809 | BREAK_TK error
1810 {yyerror ("Missing term"); RECOVER;}
1811 | BREAK_TK identifier error
1812 {yyerror ("';' expected"); RECOVER;}
1813 ;
1814
1815 continue_statement:
1816 CONTINUE_TK SC_TK
1817 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1818 | CONTINUE_TK identifier SC_TK
1819 { $$ = build_bc_statement ($1.location, 0, $2); }
1820 | CONTINUE_TK error
1821 {yyerror ("Missing term"); RECOVER;}
1822 | CONTINUE_TK identifier error
1823 {yyerror ("';' expected"); RECOVER;}
1824 ;
1825
1826 return_statement:
1827 RETURN_TK SC_TK
1828 { $$ = build_return ($1.location, NULL_TREE); }
1829 | RETURN_TK expression SC_TK
1830 { $$ = build_return ($1.location, $2); }
1831 | RETURN_TK error
1832 {yyerror ("Missing term"); RECOVER;}
1833 | RETURN_TK expression error
1834 {yyerror ("';' expected"); RECOVER;}
1835 ;
1836
1837 throw_statement:
1838 THROW_TK expression SC_TK
1839 {
1840 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1841 EXPR_WFL_LINECOL ($$) = $1.location;
1842 }
1843 | THROW_TK error
1844 {yyerror ("Missing term"); RECOVER;}
1845 | THROW_TK expression error
1846 {yyerror ("';' expected"); RECOVER;}
1847 ;
1848
1849 synchronized_statement:
1850 synchronized OP_TK expression CP_TK block
1851 {
1852 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1853 EXPR_WFL_LINECOL ($$) =
1854 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1855 }
1856 | synchronized OP_TK expression CP_TK error
1857 {yyerror ("'{' expected"); RECOVER;}
1858 | synchronized error
1859 {yyerror ("'(' expected"); RECOVER;}
1860 | synchronized OP_TK error CP_TK
1861 {yyerror ("Missing term"); RECOVER;}
1862 | synchronized OP_TK error
1863 {yyerror ("Missing term"); RECOVER;}
1864 ;
1865
1866 synchronized:
1867 modifiers
1868 {
1869 check_modifiers (
1870 "Illegal modifier `%s'. Only `synchronized' was expected here",
1871 $1, ACC_SYNCHRONIZED);
1872 if ($1 != ACC_SYNCHRONIZED)
1873 MODIFIER_WFL (SYNCHRONIZED_TK) =
1874 build_wfl_node (NULL_TREE);
1875 }
1876 ;
1877
1878 try_statement:
1879 TRY_TK block catches
1880 { $$ = build_try_statement ($1.location, $2, $3); }
1881 | TRY_TK block finally
1882 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1883 | TRY_TK block catches finally
1884 { $$ = build_try_finally_statement
1885 ($1.location, build_try_statement ($1.location,
1886 $2, $3), $4);
1887 }
1888 | TRY_TK error
1889 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1890 ;
1891
1892 catches:
1893 catch_clause
1894 | catches catch_clause
1895 {
1896 TREE_CHAIN ($2) = $1;
1897 $$ = $2;
1898 }
1899 ;
1900
1901 catch_clause:
1902 catch_clause_parameter block
1903 {
1904 java_method_add_stmt (current_function_decl, $2);
1905 exit_block ();
1906 $$ = $1;
1907 }
1908
1909 catch_clause_parameter:
1910 CATCH_TK OP_TK formal_parameter CP_TK
1911 {
1912 /* We add a block to define a scope for
1913 formal_parameter (CCBP). The formal parameter is
1914 declared initialized by the appropriate function
1915 call */
1916 tree ccpb = enter_block ();
1917 tree init = build_assignment
1918 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1919 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1920 declare_local_variables (0, TREE_VALUE ($3),
1921 build_tree_list (TREE_PURPOSE ($3),
1922 init));
1923 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1924 EXPR_WFL_LINECOL ($$) = $1.location;
1925 }
1926 | CATCH_TK error
1927 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1928 | CATCH_TK OP_TK error
1929 {
1930 yyerror ("Missing term or ')' expected");
1931 RECOVER; $$ = NULL_TREE;
1932 }
1933 | CATCH_TK OP_TK error CP_TK /* That's for () */
1934 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1935 ;
1936
1937 finally:
1938 FINALLY_TK block
1939 { $$ = $2; }
1940 | FINALLY_TK error
1941 {yyerror ("'{' expected"); RECOVER; }
1942 ;
1943
1944 /* 19.12 Production from 15: Expressions */
1945 primary:
1946 primary_no_new_array
1947 | array_creation_expression
1948 ;
1949
1950 primary_no_new_array:
1951 literal
1952 | THIS_TK
1953 { $$ = build_this ($1.location); }
1954 | OP_TK expression CP_TK
1955 {$$ = $2;}
1956 | class_instance_creation_expression
1957 | field_access
1958 | method_invocation
1959 | array_access
1960 | type_literals
1961 /* Added, JDK1.1 inner classes. Documentation is wrong
1962 refering to a 'ClassName' (class_name) rule that doesn't
1963 exist. Used name: instead. */
1964 | name DOT_TK THIS_TK
1965 {
1966 tree wfl = build_wfl_node (this_identifier_node);
1967 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1968 }
1969 | OP_TK expression error
1970 {yyerror ("')' expected"); RECOVER;}
1971 | name DOT_TK error
1972 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1973 | primitive_type DOT_TK error
1974 {yyerror ("'class' expected" ); RECOVER;}
1975 | VOID_TK DOT_TK error
1976 {yyerror ("'class' expected" ); RECOVER;}
1977 ;
1978
1979 type_literals:
1980 name DOT_TK CLASS_TK
1981 { $$ = build_incomplete_class_ref ($2.location, $1); }
1982 | array_type DOT_TK CLASS_TK
1983 { $$ = build_incomplete_class_ref ($2.location, $1); }
1984 | primitive_type DOT_TK CLASS_TK
1985 { $$ = build_incomplete_class_ref ($2.location, $1); }
1986 | VOID_TK DOT_TK CLASS_TK
1987 {
1988 $$ = build_incomplete_class_ref ($2.location,
1989 void_type_node);
1990 }
1991 ;
1992
1993 class_instance_creation_expression:
1994 NEW_TK class_type OP_TK argument_list CP_TK
1995 { $$ = build_new_invocation ($2, $4); }
1996 | NEW_TK class_type OP_TK CP_TK
1997 { $$ = build_new_invocation ($2, NULL_TREE); }
1998 | anonymous_class_creation
1999 /* Added, JDK1.1 inner classes, modified to use name or
2000 primary instead of primary solely which couldn't work in
2001 all situations. */
2002 | something_dot_new identifier OP_TK CP_TK
2003 {
2004 tree ctor = build_new_invocation ($2, NULL_TREE);
2005 $$ = make_qualified_primary ($1, ctor,
2006 EXPR_WFL_LINECOL ($1));
2007 }
2008 | something_dot_new identifier OP_TK CP_TK class_body
2009 | something_dot_new identifier OP_TK argument_list CP_TK
2010 {
2011 tree ctor = build_new_invocation ($2, $4);
2012 $$ = make_qualified_primary ($1, ctor,
2013 EXPR_WFL_LINECOL ($1));
2014 }
2015 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2016 | NEW_TK error SC_TK
2017 {yyerror ("'(' expected"); DRECOVER(new_1);}
2018 | NEW_TK class_type error
2019 {yyerror ("'(' expected"); RECOVER;}
2020 | NEW_TK class_type OP_TK error
2021 {yyerror ("')' or term expected"); RECOVER;}
2022 | NEW_TK class_type OP_TK argument_list error
2023 {yyerror ("')' expected"); RECOVER;}
2024 | something_dot_new error
2025 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2026 | something_dot_new identifier error
2027 {yyerror ("'(' expected"); RECOVER;}
2028 ;
2029
2030 /* Created after JDK1.1 rules originally added to
2031 class_instance_creation_expression, but modified to use
2032 'class_type' instead of 'TypeName' (type_name) which is mentionned
2033 in the documentation but doesn't exist. */
2034
2035 anonymous_class_creation:
2036 NEW_TK class_type OP_TK argument_list CP_TK
2037 { create_anonymous_class ($1.location, $2); }
2038 class_body
2039 {
2040 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2041 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2042
2043 end_class_declaration (1);
2044
2045 /* Now we can craft the new expression */
2046 $$ = build_new_invocation (id, $4);
2047
2048 /* Note that we can't possibly be here if
2049 `class_type' is an interface (in which case the
2050 anonymous class extends Object and implements
2051 `class_type', hence its constructor can't have
2052 arguments.) */
2053
2054 /* Otherwise, the innerclass must feature a
2055 constructor matching `argument_list'. Anonymous
2056 classes are a bit special: it's impossible to
2057 define constructor for them, hence constructors
2058 must be generated following the hints provided by
2059 the `new' expression. Whether a super constructor
2060 of that nature exists or not is to be verified
2061 later on in verify_constructor_super.
2062
2063 It's during the expansion of a `new' statement
2064 refering to an anonymous class that a ctor will
2065 be generated for the anonymous class, with the
2066 right arguments. */
2067
2068 }
2069 | NEW_TK class_type OP_TK CP_TK
2070 { create_anonymous_class ($1.location, $2); }
2071 class_body
2072 {
2073 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2074 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2075
2076 end_class_declaration (1);
2077
2078 /* Now we can craft the new expression. The
2079 statement doesn't need to be remember so that a
2080 constructor can be generated, since its signature
2081 is already known. */
2082 $$ = build_new_invocation (id, NULL_TREE);
2083 }
2084 ;
2085
2086 something_dot_new: /* Added, not part of the specs. */
2087 name DOT_TK NEW_TK
2088 { $$ = $1; }
2089 | primary DOT_TK NEW_TK
2090 { $$ = $1; }
2091 ;
2092
2093 argument_list:
2094 expression
2095 {
2096 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2097 ctxp->formal_parameter_number = 1;
2098 }
2099 | argument_list C_TK expression
2100 {
2101 ctxp->formal_parameter_number += 1;
2102 $$ = tree_cons (NULL_TREE, $3, $1);
2103 }
2104 | argument_list C_TK error
2105 {yyerror ("Missing term"); RECOVER;}
2106 ;
2107
2108 array_creation_expression:
2109 NEW_TK primitive_type dim_exprs
2110 { $$ = build_newarray_node ($2, $3, 0); }
2111 | NEW_TK class_or_interface_type dim_exprs
2112 { $$ = build_newarray_node ($2, $3, 0); }
2113 | NEW_TK primitive_type dim_exprs dims
2114 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2115 | NEW_TK class_or_interface_type dim_exprs dims
2116 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2117 /* Added, JDK1.1 anonymous array. Initial documentation rule
2118 modified */
2119 | NEW_TK class_or_interface_type dims array_initializer
2120 {
2121 char *sig;
2122 int osb = pop_current_osb (ctxp);
2123 while (osb--)
2124 obstack_grow (&temporary_obstack, "[]", 2);
2125 obstack_1grow (&temporary_obstack, '\0');
2126 sig = obstack_finish (&temporary_obstack);
2127 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2128 $2, get_identifier (sig), $4);
2129 }
2130 | NEW_TK primitive_type dims array_initializer
2131 {
2132 int osb = pop_current_osb (ctxp);
2133 tree type = $2;
2134 while (osb--)
2135 type = build_java_array_type (type, -1);
2136 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2137 build_pointer_type (type), NULL_TREE, $4);
2138 }
2139 | NEW_TK error CSB_TK
2140 {yyerror ("'[' expected"); DRECOVER ("]");}
2141 | NEW_TK error OSB_TK
2142 {yyerror ("']' expected"); RECOVER;}
2143 ;
2144
2145 dim_exprs:
2146 dim_expr
2147 { $$ = build_tree_list (NULL_TREE, $1); }
2148 | dim_exprs dim_expr
2149 { $$ = tree_cons (NULL_TREE, $2, $$); }
2150 ;
2151
2152 dim_expr:
2153 OSB_TK expression CSB_TK
2154 {
2155 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2156 {
2157 $2 = build_wfl_node ($2);
2158 TREE_TYPE ($2) = NULL_TREE;
2159 }
2160 EXPR_WFL_LINECOL ($2) = $1.location;
2161 $$ = $2;
2162 }
2163 | OSB_TK expression error
2164 {yyerror ("']' expected"); RECOVER;}
2165 | OSB_TK error
2166 {
2167 yyerror ("Missing term");
2168 yyerror ("']' expected");
2169 RECOVER;
2170 }
2171 ;
2172
2173 dims:
2174 OSB_TK CSB_TK
2175 {
2176 int allocate = 0;
2177 /* If not initialized, allocate memory for the osb
2178 numbers stack */
2179 if (!ctxp->osb_limit)
2180 {
2181 allocate = ctxp->osb_limit = 32;
2182 ctxp->osb_depth = -1;
2183 }
2184 /* If capacity overflown, reallocate a bigger chunk */
2185 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2186 allocate = ctxp->osb_limit << 1;
2187
2188 if (allocate)
2189 {
2190 allocate *= sizeof (int);
2191 if (ctxp->osb_number)
2192 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2193 allocate);
2194 else
2195 ctxp->osb_number = (int *)xmalloc (allocate);
2196 }
2197 ctxp->osb_depth++;
2198 CURRENT_OSB (ctxp) = 1;
2199 }
2200 | dims OSB_TK CSB_TK
2201 { CURRENT_OSB (ctxp)++; }
2202 | dims OSB_TK error
2203 { yyerror ("']' expected"); RECOVER;}
2204 ;
2205
2206 field_access:
2207 primary DOT_TK identifier
2208 { $$ = make_qualified_primary ($1, $3, $2.location); }
2209 /* FIXME - REWRITE TO:
2210 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2211 | SUPER_TK DOT_TK identifier
2212 {
2213 tree super_wfl = build_wfl_node (super_identifier_node);
2214 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2215 $$ = make_qualified_name (super_wfl, $3, $2.location);
2216 }
2217 | SUPER_TK error
2218 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2219 ;
2220
2221 method_invocation:
2222 name OP_TK CP_TK
2223 { $$ = build_method_invocation ($1, NULL_TREE); }
2224 | name OP_TK argument_list CP_TK
2225 { $$ = build_method_invocation ($1, $3); }
2226 | primary DOT_TK identifier OP_TK CP_TK
2227 {
2228 if (TREE_CODE ($1) == THIS_EXPR)
2229 $$ = build_this_super_qualified_invocation
2230 (1, $3, NULL_TREE, 0, $2.location);
2231 else
2232 {
2233 tree invok = build_method_invocation ($3, NULL_TREE);
2234 $$ = make_qualified_primary ($1, invok, $2.location);
2235 }
2236 }
2237 | primary DOT_TK identifier OP_TK argument_list CP_TK
2238 {
2239 if (TREE_CODE ($1) == THIS_EXPR)
2240 $$ = build_this_super_qualified_invocation
2241 (1, $3, $5, 0, $2.location);
2242 else
2243 {
2244 tree invok = build_method_invocation ($3, $5);
2245 $$ = make_qualified_primary ($1, invok, $2.location);
2246 }
2247 }
2248 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2249 {
2250 $$ = build_this_super_qualified_invocation
2251 (0, $3, NULL_TREE, $1.location, $2.location);
2252 }
2253 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2254 {
2255 $$ = build_this_super_qualified_invocation
2256 (0, $3, $5, $1.location, $2.location);
2257 }
2258 /* Screws up thing. I let it here until I'm convinced it can
2259 be removed. FIXME
2260 | primary DOT_TK error
2261 {yyerror ("'(' expected"); DRECOVER(bad);} */
2262 | SUPER_TK DOT_TK error CP_TK
2263 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2264 | SUPER_TK DOT_TK error DOT_TK
2265 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2266 ;
2267
2268 array_access:
2269 name OSB_TK expression CSB_TK
2270 { $$ = build_array_ref ($2.location, $1, $3); }
2271 | primary_no_new_array OSB_TK expression CSB_TK
2272 { $$ = build_array_ref ($2.location, $1, $3); }
2273 | name OSB_TK error
2274 {
2275 yyerror ("Missing term and ']' expected");
2276 DRECOVER(array_access);
2277 }
2278 | name OSB_TK expression error
2279 {
2280 yyerror ("']' expected");
2281 DRECOVER(array_access);
2282 }
2283 | primary_no_new_array OSB_TK error
2284 {
2285 yyerror ("Missing term and ']' expected");
2286 DRECOVER(array_access);
2287 }
2288 | primary_no_new_array OSB_TK expression error
2289 {
2290 yyerror ("']' expected");
2291 DRECOVER(array_access);
2292 }
2293 ;
2294
2295 postfix_expression:
2296 primary
2297 | name
2298 | post_increment_expression
2299 | post_decrement_expression
2300 ;
2301
2302 post_increment_expression:
2303 postfix_expression INCR_TK
2304 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2305 ;
2306
2307 post_decrement_expression:
2308 postfix_expression DECR_TK
2309 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2310 ;
2311
2312 trap_overflow_corner_case:
2313 pre_increment_expression
2314 | pre_decrement_expression
2315 | PLUS_TK unary_expression
2316 {$$ = build_unaryop ($1.token, $1.location, $2); }
2317 | unary_expression_not_plus_minus
2318 | PLUS_TK error
2319 {yyerror ("Missing term"); RECOVER}
2320 ;
2321
2322 unary_expression:
2323 trap_overflow_corner_case
2324 {
2325 error_if_numeric_overflow ($1);
2326 $$ = $1;
2327 }
2328 | MINUS_TK trap_overflow_corner_case
2329 {$$ = build_unaryop ($1.token, $1.location, $2); }
2330 | MINUS_TK error
2331 {yyerror ("Missing term"); RECOVER}
2332 ;
2333
2334 pre_increment_expression:
2335 INCR_TK unary_expression
2336 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2337 | INCR_TK error
2338 {yyerror ("Missing term"); RECOVER}
2339 ;
2340
2341 pre_decrement_expression:
2342 DECR_TK unary_expression
2343 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2344 | DECR_TK error
2345 {yyerror ("Missing term"); RECOVER}
2346 ;
2347
2348 unary_expression_not_plus_minus:
2349 postfix_expression
2350 | NOT_TK unary_expression
2351 {$$ = build_unaryop ($1.token, $1.location, $2); }
2352 | NEG_TK unary_expression
2353 {$$ = build_unaryop ($1.token, $1.location, $2); }
2354 | cast_expression
2355 | NOT_TK error
2356 {yyerror ("Missing term"); RECOVER}
2357 | NEG_TK error
2358 {yyerror ("Missing term"); RECOVER}
2359 ;
2360
2361 cast_expression: /* Error handling here is potentially weak */
2362 OP_TK primitive_type dims CP_TK unary_expression
2363 {
2364 tree type = $2;
2365 int osb = pop_current_osb (ctxp);
2366 while (osb--)
2367 type = build_java_array_type (type, -1);
2368 $$ = build_cast ($1.location, type, $5);
2369 }
2370 | OP_TK primitive_type CP_TK unary_expression
2371 { $$ = build_cast ($1.location, $2, $4); }
2372 | OP_TK expression CP_TK unary_expression_not_plus_minus
2373 { $$ = build_cast ($1.location, $2, $4); }
2374 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2375 {
2376 const char *ptr;
2377 int osb = pop_current_osb (ctxp);
2378 obstack_grow (&temporary_obstack,
2379 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2380 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2381 while (osb--)
2382 obstack_grow (&temporary_obstack, "[]", 2);
2383 obstack_1grow (&temporary_obstack, '\0');
2384 ptr = obstack_finish (&temporary_obstack);
2385 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2386 $$ = build_cast ($1.location, $2, $5);
2387 }
2388 | OP_TK primitive_type OSB_TK error
2389 {yyerror ("']' expected, invalid type expression");}
2390 | OP_TK error
2391 {
2392 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2393 RECOVER;
2394 }
2395 | OP_TK primitive_type dims CP_TK error
2396 {yyerror ("Missing term"); RECOVER;}
2397 | OP_TK primitive_type CP_TK error
2398 {yyerror ("Missing term"); RECOVER;}
2399 | OP_TK name dims CP_TK error
2400 {yyerror ("Missing term"); RECOVER;}
2401 ;
2402
2403 multiplicative_expression:
2404 unary_expression
2405 | multiplicative_expression MULT_TK unary_expression
2406 {
2407 $$ = build_binop (BINOP_LOOKUP ($2.token),
2408 $2.location, $1, $3);
2409 }
2410 | multiplicative_expression DIV_TK unary_expression
2411 {
2412 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2413 $1, $3);
2414 }
2415 | multiplicative_expression REM_TK unary_expression
2416 {
2417 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2418 $1, $3);
2419 }
2420 | multiplicative_expression MULT_TK error
2421 {yyerror ("Missing term"); RECOVER;}
2422 | multiplicative_expression DIV_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424 | multiplicative_expression REM_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426 ;
2427
2428 additive_expression:
2429 multiplicative_expression
2430 | additive_expression PLUS_TK multiplicative_expression
2431 {
2432 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2433 $1, $3);
2434 }
2435 | additive_expression MINUS_TK multiplicative_expression
2436 {
2437 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2438 $1, $3);
2439 }
2440 | additive_expression PLUS_TK error
2441 {yyerror ("Missing term"); RECOVER;}
2442 | additive_expression MINUS_TK error
2443 {yyerror ("Missing term"); RECOVER;}
2444 ;
2445
2446 shift_expression:
2447 additive_expression
2448 | shift_expression LS_TK additive_expression
2449 {
2450 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2451 $1, $3);
2452 }
2453 | shift_expression SRS_TK additive_expression
2454 {
2455 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2456 $1, $3);
2457 }
2458 | shift_expression ZRS_TK additive_expression
2459 {
2460 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2461 $1, $3);
2462 }
2463 | shift_expression LS_TK error
2464 {yyerror ("Missing term"); RECOVER;}
2465 | shift_expression SRS_TK error
2466 {yyerror ("Missing term"); RECOVER;}
2467 | shift_expression ZRS_TK error
2468 {yyerror ("Missing term"); RECOVER;}
2469 ;
2470
2471 relational_expression:
2472 shift_expression
2473 | relational_expression LT_TK shift_expression
2474 {
2475 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2476 $1, $3);
2477 }
2478 | relational_expression GT_TK shift_expression
2479 {
2480 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2481 $1, $3);
2482 }
2483 | relational_expression LTE_TK shift_expression
2484 {
2485 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2486 $1, $3);
2487 }
2488 | relational_expression GTE_TK shift_expression
2489 {
2490 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2491 $1, $3);
2492 }
2493 | relational_expression INSTANCEOF_TK reference_type
2494 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2495 | relational_expression LT_TK error
2496 {yyerror ("Missing term"); RECOVER;}
2497 | relational_expression GT_TK error
2498 {yyerror ("Missing term"); RECOVER;}
2499 | relational_expression LTE_TK error
2500 {yyerror ("Missing term"); RECOVER;}
2501 | relational_expression GTE_TK error
2502 {yyerror ("Missing term"); RECOVER;}
2503 | relational_expression INSTANCEOF_TK error
2504 {yyerror ("Invalid reference type"); RECOVER;}
2505 ;
2506
2507 equality_expression:
2508 relational_expression
2509 | equality_expression EQ_TK relational_expression
2510 {
2511 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2512 $1, $3);
2513 }
2514 | equality_expression NEQ_TK relational_expression
2515 {
2516 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2517 $1, $3);
2518 }
2519 | equality_expression EQ_TK error
2520 {yyerror ("Missing term"); RECOVER;}
2521 | equality_expression NEQ_TK error
2522 {yyerror ("Missing term"); RECOVER;}
2523 ;
2524
2525 and_expression:
2526 equality_expression
2527 | and_expression AND_TK equality_expression
2528 {
2529 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2530 $1, $3);
2531 }
2532 | and_expression AND_TK error
2533 {yyerror ("Missing term"); RECOVER;}
2534 ;
2535
2536 exclusive_or_expression:
2537 and_expression
2538 | exclusive_or_expression XOR_TK and_expression
2539 {
2540 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2541 $1, $3);
2542 }
2543 | exclusive_or_expression XOR_TK error
2544 {yyerror ("Missing term"); RECOVER;}
2545 ;
2546
2547 inclusive_or_expression:
2548 exclusive_or_expression
2549 | inclusive_or_expression OR_TK exclusive_or_expression
2550 {
2551 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2552 $1, $3);
2553 }
2554 | inclusive_or_expression OR_TK error
2555 {yyerror ("Missing term"); RECOVER;}
2556 ;
2557
2558 conditional_and_expression:
2559 inclusive_or_expression
2560 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2561 {
2562 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2563 $1, $3);
2564 }
2565 | conditional_and_expression BOOL_AND_TK error
2566 {yyerror ("Missing term"); RECOVER;}
2567 ;
2568
2569 conditional_or_expression:
2570 conditional_and_expression
2571 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2572 {
2573 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2574 $1, $3);
2575 }
2576 | conditional_or_expression BOOL_OR_TK error
2577 {yyerror ("Missing term"); RECOVER;}
2578 ;
2579
2580 conditional_expression: /* Error handling here is weak */
2581 conditional_or_expression
2582 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2583 {
2584 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2585 EXPR_WFL_LINECOL ($$) = $2.location;
2586 }
2587 | conditional_or_expression REL_QM_TK REL_CL_TK error
2588 {
2589 YYERROR_NOW;
2590 yyerror ("Missing term");
2591 DRECOVER (1);
2592 }
2593 | conditional_or_expression REL_QM_TK error
2594 {yyerror ("Missing term"); DRECOVER (2);}
2595 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2596 {yyerror ("Missing term"); DRECOVER (3);}
2597 ;
2598
2599 assignment_expression:
2600 conditional_expression
2601 | assignment
2602 ;
2603
2604 assignment:
2605 left_hand_side assignment_operator assignment_expression
2606 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2607 | left_hand_side assignment_operator error
2608 {
2609 YYNOT_TWICE yyerror ("Missing term");
2610 DRECOVER (assign);
2611 }
2612 ;
2613
2614 left_hand_side:
2615 name
2616 | field_access
2617 | array_access
2618 ;
2619
2620 assignment_operator:
2621 ASSIGN_ANY_TK
2622 | ASSIGN_TK
2623 ;
2624
2625 expression:
2626 assignment_expression
2627 ;
2628
2629 constant_expression:
2630 expression
2631 ;
2632
2633 %%
2634
2635 /* Helper function to retrieve an OSB count. Should be used when the
2636 `dims:' rule is being used. */
2637
2638 static int
2639 pop_current_osb (ctxp)
2640 struct parser_ctxt *ctxp;
2641 {
2642 int to_return;
2643
2644 if (ctxp->osb_depth < 0)
2645 abort ();
2646
2647 to_return = CURRENT_OSB (ctxp);
2648 ctxp->osb_depth--;
2649
2650 return to_return;
2651 }
2652
2653 \f
2654
2655 /* This section of the code deal with save/restoring parser contexts.
2656 Add mode documentation here. FIXME */
2657
2658 /* Helper function. Create a new parser context. With
2659 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2660 context is copied, otherwise, the new context is zeroed. The newly
2661 created context becomes the current one. */
2662
2663 static void
2664 create_new_parser_context (copy_from_previous)
2665 int copy_from_previous;
2666 {
2667 struct parser_ctxt *new;
2668
2669 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2670 if (copy_from_previous)
2671 {
2672 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2673 new->saved_data_ctx = 1;
2674 }
2675 else
2676 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2677
2678 new->next = ctxp;
2679 ctxp = new;
2680 }
2681
2682 /* Create a new parser context and make it the current one. */
2683
2684 void
2685 java_push_parser_context ()
2686 {
2687 create_new_parser_context (0);
2688 }
2689
2690 void
2691 java_pop_parser_context (generate)
2692 int generate;
2693 {
2694 tree current;
2695 struct parser_ctxt *toFree, *next;
2696
2697 if (!ctxp)
2698 return;
2699
2700 toFree = ctxp;
2701 next = ctxp->next;
2702 if (next)
2703 {
2704 lineno = ctxp->lineno;
2705 current_class = ctxp->class_type;
2706 }
2707
2708 /* If the old and new lexers differ, then free the old one. */
2709 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2710 java_destroy_lexer (ctxp->lexer);
2711
2712 /* Set the single import class file flag to 0 for the current list
2713 of imported things */
2714 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2715 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2716
2717 /* And restore those of the previous context */
2718 if ((ctxp = next)) /* Assignment is really meant here */
2719 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2720 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2721
2722 /* If we pushed a context to parse a class intended to be generated,
2723 we keep it so we can remember the class. What we could actually
2724 do is to just update a list of class names. */
2725 if (generate)
2726 {
2727 toFree->next = ctxp_for_generation;
2728 ctxp_for_generation = toFree;
2729 }
2730 else
2731 free (toFree);
2732 }
2733
2734 /* Create a parser context for the use of saving some global
2735 variables. */
2736
2737 void
2738 java_parser_context_save_global ()
2739 {
2740 if (!ctxp)
2741 {
2742 java_push_parser_context ();
2743 ctxp->saved_data_ctx = 1;
2744 }
2745
2746 /* If this context already stores data, create a new one suitable
2747 for data storage. */
2748 else if (ctxp->saved_data)
2749 create_new_parser_context (1);
2750
2751 ctxp->lineno = lineno;
2752 ctxp->class_type = current_class;
2753 ctxp->filename = input_filename;
2754 ctxp->function_decl = current_function_decl;
2755 ctxp->saved_data = 1;
2756 }
2757
2758 /* Restore some global variables from the previous context. Make the
2759 previous context the current one. */
2760
2761 void
2762 java_parser_context_restore_global ()
2763 {
2764 lineno = ctxp->lineno;
2765 current_class = ctxp->class_type;
2766 input_filename = ctxp->filename;
2767 if (wfl_operator)
2768 {
2769 tree s;
2770 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2771 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2772 }
2773 current_function_decl = ctxp->function_decl;
2774 ctxp->saved_data = 0;
2775 if (ctxp->saved_data_ctx)
2776 java_pop_parser_context (0);
2777 }
2778
2779 /* Suspend vital data for the current class/function being parsed so
2780 that an other class can be parsed. Used to let local/anonymous
2781 classes be parsed. */
2782
2783 static void
2784 java_parser_context_suspend ()
2785 {
2786 /* This makes debugging through java_debug_context easier */
2787 static const char *const name = "<inner buffer context>";
2788
2789 /* Duplicate the previous context, use it to save the globals we're
2790 interested in */
2791 create_new_parser_context (1);
2792 ctxp->function_decl = current_function_decl;
2793 ctxp->class_type = current_class;
2794
2795 /* Then create a new context which inherits all data from the
2796 previous one. This will be the new current context */
2797 create_new_parser_context (1);
2798
2799 /* Help debugging */
2800 ctxp->next->filename = name;
2801 }
2802
2803 /* Resume vital data for the current class/function being parsed so
2804 that an other class can be parsed. Used to let local/anonymous
2805 classes be parsed. The trick is the data storing file position
2806 informations must be restored to their current value, so parsing
2807 can resume as if no context was ever saved. */
2808
2809 static void
2810 java_parser_context_resume ()
2811 {
2812 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2813 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2814 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2815
2816 /* We need to inherit the list of classes to complete/generate */
2817 restored->classd_list = old->classd_list;
2818 restored->class_list = old->class_list;
2819
2820 /* Restore the current class and function from the saver */
2821 current_class = saver->class_type;
2822 current_function_decl = saver->function_decl;
2823
2824 /* Retrive the restored context */
2825 ctxp = restored;
2826
2827 /* Re-installed the data for the parsing to carry on */
2828 memcpy (&ctxp->marker_begining, &old->marker_begining,
2829 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2830
2831 /* Buffer context can now be discarded */
2832 free (saver);
2833 free (old);
2834 }
2835
2836 /* Add a new anchor node to which all statement(s) initializing static
2837 and non static initialized upon declaration field(s) will be
2838 linked. */
2839
2840 static void
2841 java_parser_context_push_initialized_field ()
2842 {
2843 tree node;
2844
2845 node = build_tree_list (NULL_TREE, NULL_TREE);
2846 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2847 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2848
2849 node = build_tree_list (NULL_TREE, NULL_TREE);
2850 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2851 CPC_INITIALIZER_LIST (ctxp) = node;
2852
2853 node = build_tree_list (NULL_TREE, NULL_TREE);
2854 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2855 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2856 }
2857
2858 /* Pop the lists of initialized field. If this lists aren't empty,
2859 remember them so we can use it to create and populate the finit$
2860 or <clinit> functions. */
2861
2862 static void
2863 java_parser_context_pop_initialized_field ()
2864 {
2865 tree stmts;
2866 tree class_type = TREE_TYPE (GET_CPC ());
2867
2868 if (CPC_INITIALIZER_LIST (ctxp))
2869 {
2870 stmts = CPC_INITIALIZER_STMT (ctxp);
2871 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2872 if (stmts && !java_error_count)
2873 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2874 }
2875
2876 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2877 {
2878 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2879 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2880 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2881 /* Keep initialization in order to enforce 8.5 */
2882 if (stmts && !java_error_count)
2883 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2884 }
2885
2886 /* JDK 1.1 instance initializers */
2887 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2888 {
2889 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2890 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2891 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2892 if (stmts && !java_error_count)
2893 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2894 }
2895 }
2896
2897 static tree
2898 reorder_static_initialized (list)
2899 tree list;
2900 {
2901 /* We have to keep things in order. The alias initializer have to
2902 come first, then the initialized regular field, in reverse to
2903 keep them in lexical order. */
2904 tree marker, previous = NULL_TREE;
2905 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2906 if (TREE_CODE (marker) == TREE_LIST
2907 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2908 break;
2909
2910 /* No static initialized, the list is fine as is */
2911 if (!previous)
2912 list = TREE_CHAIN (marker);
2913
2914 /* No marker? reverse the whole list */
2915 else if (!marker)
2916 list = nreverse (list);
2917
2918 /* Otherwise, reverse what's after the marker and the new reordered
2919 sublist will replace the marker. */
2920 else
2921 {
2922 TREE_CHAIN (previous) = NULL_TREE;
2923 list = nreverse (list);
2924 list = chainon (TREE_CHAIN (marker), list);
2925 }
2926 return list;
2927 }
2928
2929 /* Helper functions to dump the parser context stack. */
2930
2931 #define TAB_CONTEXT(C) \
2932 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2933
2934 static void
2935 java_debug_context_do (tab)
2936 int tab;
2937 {
2938 struct parser_ctxt *copy = ctxp;
2939 while (copy)
2940 {
2941 TAB_CONTEXT (tab);
2942 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2943 TAB_CONTEXT (tab);
2944 fprintf (stderr, "filename: %s\n", copy->filename);
2945 TAB_CONTEXT (tab);
2946 fprintf (stderr, "lineno: %d\n", copy->lineno);
2947 TAB_CONTEXT (tab);
2948 fprintf (stderr, "package: %s\n",
2949 (copy->package ?
2950 IDENTIFIER_POINTER (copy->package) : "<none>"));
2951 TAB_CONTEXT (tab);
2952 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2953 TAB_CONTEXT (tab);
2954 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2955 copy = copy->next;
2956 tab += 2;
2957 }
2958 }
2959
2960 /* Dump the stacked up parser contexts. Intended to be called from a
2961 debugger. */
2962
2963 void
2964 java_debug_context ()
2965 {
2966 java_debug_context_do (0);
2967 }
2968
2969 \f
2970
2971 /* Flag for the error report routine to issue the error the first time
2972 it's called (overriding the default behavior which is to drop the
2973 first invocation and honor the second one, taking advantage of a
2974 richer context. */
2975 static int force_error = 0;
2976
2977 /* Reporting an constructor invocation error. */
2978 static void
2979 parse_ctor_invocation_error ()
2980 {
2981 if (DECL_CONSTRUCTOR_P (current_function_decl))
2982 yyerror ("Constructor invocation must be first thing in a constructor");
2983 else
2984 yyerror ("Only constructors can invoke constructors");
2985 }
2986
2987 /* Reporting JDK1.1 features not implemented. */
2988
2989 static tree
2990 parse_jdk1_1_error (msg)
2991 const char *msg;
2992 {
2993 sorry (": `%s' JDK1.1(TM) feature", msg);
2994 java_error_count++;
2995 return empty_stmt_node;
2996 }
2997
2998 static int do_warning = 0;
2999
3000 void
3001 yyerror (msg)
3002 const char *msg;
3003 {
3004 static java_lc elc;
3005 static int prev_lineno;
3006 static const char *prev_msg;
3007
3008 int save_lineno;
3009 char *remainder, *code_from_source;
3010
3011 if (!force_error && prev_lineno == lineno)
3012 return;
3013
3014 /* Save current error location but report latter, when the context is
3015 richer. */
3016 if (ctxp->java_error_flag == 0)
3017 {
3018 ctxp->java_error_flag = 1;
3019 elc = ctxp->elc;
3020 /* Do something to use the previous line if we're reaching the
3021 end of the file... */
3022 #ifdef VERBOSE_SKELETON
3023 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3024 #endif
3025 return;
3026 }
3027
3028 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3029 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3030 return;
3031
3032 ctxp->java_error_flag = 0;
3033 if (do_warning)
3034 java_warning_count++;
3035 else
3036 java_error_count++;
3037
3038 if (elc.col == 0 && msg && msg[1] == ';')
3039 {
3040 elc.col = ctxp->p_line->char_col-1;
3041 elc.line = ctxp->p_line->lineno;
3042 }
3043
3044 save_lineno = lineno;
3045 prev_lineno = lineno = elc.line;
3046 prev_msg = msg;
3047
3048 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3049 obstack_grow0 (&temporary_obstack,
3050 code_from_source, strlen (code_from_source));
3051 remainder = obstack_finish (&temporary_obstack);
3052 if (do_warning)
3053 warning ("%s.\n%s", msg, remainder);
3054 else
3055 error ("%s.\n%s", msg, remainder);
3056
3057 /* This allow us to cheaply avoid an extra 'Invalid expression
3058 statement' error report when errors have been already reported on
3059 the same line. This occurs when we report an error but don't have
3060 a synchronization point other than ';', which
3061 expression_statement is the only one to take care of. */
3062 ctxp->prevent_ese = lineno = save_lineno;
3063 }
3064
3065 static void
3066 issue_warning_error_from_context (cl, msg, ap)
3067 tree cl;
3068 const char *msg;
3069 va_list ap;
3070 {
3071 const char *saved, *saved_input_filename;
3072 char buffer [4096];
3073 vsprintf (buffer, msg, ap);
3074 force_error = 1;
3075
3076 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3077 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3078 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3079
3080 /* We have a CL, that's a good reason for using it if it contains data */
3081 saved = ctxp->filename;
3082 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3083 ctxp->filename = EXPR_WFL_FILENAME (cl);
3084 saved_input_filename = input_filename;
3085 input_filename = ctxp->filename;
3086 java_error (NULL);
3087 java_error (buffer);
3088 ctxp->filename = saved;
3089 input_filename = saved_input_filename;
3090 force_error = 0;
3091 }
3092
3093 /* Issue an error message at a current source line CL */
3094
3095 void
3096 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3097 {
3098 VA_OPEN (ap, msg);
3099 VA_FIXEDARG (ap, tree, cl);
3100 VA_FIXEDARG (ap, const char *, msg);
3101 issue_warning_error_from_context (cl, msg, ap);
3102 VA_CLOSE (ap);
3103 }
3104
3105 /* Issue a warning at a current source line CL */
3106
3107 static void
3108 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3109 {
3110 VA_OPEN (ap, msg);
3111 VA_FIXEDARG (ap, tree, cl);
3112 VA_FIXEDARG (ap, const char *, msg);
3113
3114 force_error = do_warning = 1;
3115 issue_warning_error_from_context (cl, msg, ap);
3116 do_warning = force_error = 0;
3117 VA_CLOSE (ap);
3118 }
3119
3120 static tree
3121 find_expr_with_wfl (node)
3122 tree node;
3123 {
3124 while (node)
3125 {
3126 char code;
3127 tree to_return;
3128
3129 switch (TREE_CODE (node))
3130 {
3131 case BLOCK:
3132 node = BLOCK_EXPR_BODY (node);
3133 continue;
3134
3135 case COMPOUND_EXPR:
3136 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3137 if (to_return)
3138 return to_return;
3139 node = TREE_OPERAND (node, 1);
3140 continue;
3141
3142 case LOOP_EXPR:
3143 node = TREE_OPERAND (node, 0);
3144 continue;
3145
3146 case LABELED_BLOCK_EXPR:
3147 node = TREE_OPERAND (node, 1);
3148 continue;
3149
3150 default:
3151 code = TREE_CODE_CLASS (TREE_CODE (node));
3152 if (((code == '1') || (code == '2') || (code == 'e'))
3153 && EXPR_WFL_LINECOL (node))
3154 return node;
3155 return NULL_TREE;
3156 }
3157 }
3158 return NULL_TREE;
3159 }
3160
3161 /* Issue a missing return statement error. Uses METHOD to figure the
3162 last line of the method the error occurs in. */
3163
3164 static void
3165 missing_return_error (method)
3166 tree method;
3167 {
3168 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3169 parse_error_context (wfl_operator, "Missing return statement");
3170 }
3171
3172 /* Issue an unreachable statement error. From NODE, find the next
3173 statement to report appropriately. */
3174 static void
3175 unreachable_stmt_error (node)
3176 tree node;
3177 {
3178 /* Browse node to find the next expression node that has a WFL. Use
3179 the location to report the error */
3180 if (TREE_CODE (node) == COMPOUND_EXPR)
3181 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3182 else
3183 node = find_expr_with_wfl (node);
3184
3185 if (node)
3186 {
3187 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3188 parse_error_context (wfl_operator, "Unreachable statement");
3189 }
3190 else
3191 abort ();
3192 }
3193
3194 int
3195 java_report_errors ()
3196 {
3197 if (java_error_count)
3198 fprintf (stderr, "%d error%s",
3199 java_error_count, (java_error_count == 1 ? "" : "s"));
3200 if (java_warning_count)
3201 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3202 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3203 if (java_error_count || java_warning_count)
3204 putc ('\n', stderr);
3205 return java_error_count;
3206 }
3207
3208 static char *
3209 java_accstring_lookup (flags)
3210 int flags;
3211 {
3212 static char buffer [80];
3213 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3214
3215 /* Access modifier looked-up first for easier report on forbidden
3216 access. */
3217 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3218 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3219 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3220 if (flags & ACC_STATIC) COPY_RETURN ("static");
3221 if (flags & ACC_FINAL) COPY_RETURN ("final");
3222 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3223 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3224 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3225 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3226 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3227 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3228
3229 buffer [0] = '\0';
3230 return buffer;
3231 #undef COPY_RETURN
3232 }
3233
3234 /* Issuing error messages upon redefinition of classes, interfaces or
3235 variables. */
3236
3237 static void
3238 classitf_redefinition_error (context, id, decl, cl)
3239 const char *context;
3240 tree id, decl, cl;
3241 {
3242 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3243 context, IDENTIFIER_POINTER (id),
3244 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3245 /* Here we should point out where its redefined. It's a unicode. FIXME */
3246 }
3247
3248 static void
3249 variable_redefinition_error (context, name, type, line)
3250 tree context, name, type;
3251 int line;
3252 {
3253 const char *type_name;
3254
3255 /* Figure a proper name for type. We might haven't resolved it */
3256 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3257 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3258 else
3259 type_name = lang_printable_name (type, 0);
3260
3261 parse_error_context (context,
3262 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3263 IDENTIFIER_POINTER (name),
3264 type_name, IDENTIFIER_POINTER (name), line);
3265 }
3266
3267 /* If ANAME is terminated with `[]', it indicates an array. This
3268 function returns the number of `[]' found and if this number is
3269 greater than zero, it extracts the array type name and places it in
3270 the node pointed to by TRIMMED unless TRIMMED is null. */
3271
3272 static int
3273 build_type_name_from_array_name (aname, trimmed)
3274 tree aname;
3275 tree *trimmed;
3276 {
3277 const char *name = IDENTIFIER_POINTER (aname);
3278 int len = IDENTIFIER_LENGTH (aname);
3279 int array_dims;
3280
3281 STRING_STRIP_BRACKETS (name, len, array_dims);
3282
3283 if (array_dims && trimmed)
3284 *trimmed = get_identifier_with_length (name, len);
3285
3286 return array_dims;
3287 }
3288
3289 static tree
3290 build_array_from_name (type, type_wfl, name, ret_name)
3291 tree type, type_wfl, name, *ret_name;
3292 {
3293 int more_dims = 0;
3294
3295 /* Eventually get more dims */
3296 more_dims = build_type_name_from_array_name (name, &name);
3297
3298 /* If we have, then craft a new type for this variable */
3299 if (more_dims)
3300 {
3301 tree save = type;
3302
3303 /* If we have a pointer, use its type */
3304 if (TREE_CODE (type) == POINTER_TYPE)
3305 type = TREE_TYPE (type);
3306
3307 /* Building the first dimension of a primitive type uses this
3308 function */
3309 if (JPRIMITIVE_TYPE_P (type))
3310 {
3311 type = build_java_array_type (type, -1);
3312 more_dims--;
3313 }
3314 /* Otherwise, if we have a WFL for this type, use it (the type
3315 is already an array on an unresolved type, and we just keep
3316 on adding dimensions) */
3317 else if (type_wfl)
3318 {
3319 type = type_wfl;
3320 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3321 NULL);
3322 }
3323
3324 /* Add all the dimensions */
3325 while (more_dims--)
3326 type = build_unresolved_array_type (type);
3327
3328 /* The type may have been incomplete in the first place */
3329 if (type_wfl)
3330 type = obtain_incomplete_type (type);
3331 }
3332
3333 if (ret_name)
3334 *ret_name = name;
3335 return type;
3336 }
3337
3338 /* Build something that the type identifier resolver will identify as
3339 being an array to an unresolved type. TYPE_WFL is a WFL on a
3340 identifier. */
3341
3342 static tree
3343 build_unresolved_array_type (type_or_wfl)
3344 tree type_or_wfl;
3345 {
3346 const char *ptr;
3347 tree wfl;
3348
3349 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3350 just create a array type */
3351 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3352 return build_java_array_type (type_or_wfl, -1);
3353
3354 obstack_grow (&temporary_obstack,
3355 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3356 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3357 obstack_grow0 (&temporary_obstack, "[]", 2);
3358 ptr = obstack_finish (&temporary_obstack);
3359 wfl = build_expr_wfl (get_identifier (ptr),
3360 EXPR_WFL_FILENAME (type_or_wfl),
3361 EXPR_WFL_LINENO (type_or_wfl),
3362 EXPR_WFL_COLNO (type_or_wfl));
3363 /* Re-install the existing qualifications so that the type can be
3364 resolved properly. */
3365 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3366 return wfl;
3367 }
3368
3369 static void
3370 parser_add_interface (class_decl, interface_decl, wfl)
3371 tree class_decl, interface_decl, wfl;
3372 {
3373 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3374 parse_error_context (wfl, "Interface `%s' repeated",
3375 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3376 }
3377
3378 /* Bulk of common class/interface checks. Return 1 if an error was
3379 encountered. TAG is 0 for a class, 1 for an interface. */
3380
3381 static int
3382 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3383 int is_interface, flags;
3384 tree raw_name, qualified_name, decl, cl;
3385 {
3386 tree node;
3387 int sca = 0; /* Static class allowed */
3388 int icaf = 0; /* Inner class allowed flags */
3389 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3390
3391 if (!quiet_flag)
3392 fprintf (stderr, " %s%s %s",
3393 (CPC_INNER_P () ? "inner" : ""),
3394 (is_interface ? "interface" : "class"),
3395 IDENTIFIER_POINTER (qualified_name));
3396
3397 /* Scope of an interface/class type name:
3398 - Can't be imported by a single type import
3399 - Can't already exists in the package */
3400 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3401 && (node = find_name_in_single_imports (raw_name))
3402 && !CPC_INNER_P ())
3403 {
3404 parse_error_context
3405 (cl, "%s name `%s' clashes with imported type `%s'",
3406 (is_interface ? "Interface" : "Class"),
3407 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3408 return 1;
3409 }
3410 if (decl && CLASS_COMPLETE_P (decl))
3411 {
3412 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3413 qualified_name, decl, cl);
3414 return 1;
3415 }
3416
3417 if (check_inner_class_redefinition (raw_name, cl))
3418 return 1;
3419
3420 /* If public, file name should match class/interface name, except
3421 when dealing with an inner class */
3422 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3423 {
3424 const char *f;
3425
3426 /* Contains OS dependent assumption on path separator. FIXME */
3427 for (f = &input_filename [strlen (input_filename)];
3428 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3429 f--)
3430 ;
3431 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3432 f++;
3433 if (strncmp (IDENTIFIER_POINTER (raw_name),
3434 f , IDENTIFIER_LENGTH (raw_name)) ||
3435 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3436 parse_error_context
3437 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3438 (is_interface ? "interface" : "class"),
3439 IDENTIFIER_POINTER (qualified_name),
3440 IDENTIFIER_POINTER (raw_name));
3441 }
3442
3443 /* Static classes can be declared only in top level classes. Note:
3444 once static, a inner class is a top level class. */
3445 if (flags & ACC_STATIC)
3446 {
3447 /* Catch the specific error of declaring an class inner class
3448 with no toplevel enclosing class. Prevent check_modifiers from
3449 complaining a second time */
3450 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3451 {
3452 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3453 IDENTIFIER_POINTER (qualified_name));
3454 sca = ACC_STATIC;
3455 }
3456 /* Else, in the context of a top-level class declaration, let
3457 `check_modifiers' do its job, otherwise, give it a go */
3458 else
3459 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3460 }
3461
3462 /* Inner classes can be declared private or protected
3463 within their enclosing classes. */
3464 if (CPC_INNER_P ())
3465 {
3466 /* A class which is local to a block can't be public, private,
3467 protected or static. But it is created final, so allow this
3468 one. */
3469 if (current_function_decl)
3470 icaf = sca = uaaf = ACC_FINAL;
3471 else
3472 {
3473 check_modifiers_consistency (flags);
3474 icaf = ACC_PROTECTED;
3475 if (! CLASS_INTERFACE (GET_CPC ()))
3476 icaf |= ACC_PRIVATE;
3477 }
3478 }
3479
3480 if (is_interface)
3481 {
3482 if (CPC_INNER_P ())
3483 uaaf = INTERFACE_INNER_MODIFIERS;
3484 else
3485 uaaf = INTERFACE_MODIFIERS;
3486
3487 check_modifiers ("Illegal modifier `%s' for interface declaration",
3488 flags, uaaf);
3489 }
3490 else
3491 check_modifiers ((current_function_decl ?
3492 "Illegal modifier `%s' for local class declaration" :
3493 "Illegal modifier `%s' for class declaration"),
3494 flags, uaaf|sca|icaf);
3495 return 0;
3496 }
3497
3498 static void
3499 make_nested_class_name (cpc_list)
3500 tree cpc_list;
3501 {
3502 tree name;
3503
3504 if (!cpc_list)
3505 return;
3506 else
3507 make_nested_class_name (TREE_CHAIN (cpc_list));
3508
3509 /* Pick the qualified name when dealing with the first upmost
3510 enclosing class */
3511 name = (TREE_CHAIN (cpc_list) ?
3512 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3513 obstack_grow (&temporary_obstack,
3514 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3515 obstack_1grow (&temporary_obstack, '$');
3516 }
3517
3518 /* Can't redefine a class already defined in an earlier scope. */
3519
3520 static int
3521 check_inner_class_redefinition (raw_name, cl)
3522 tree raw_name, cl;
3523 {
3524 tree scope_list;
3525
3526 for (scope_list = GET_CPC_LIST (); scope_list;
3527 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3528 if (raw_name == GET_CPC_UN_NODE (scope_list))
3529 {
3530 parse_error_context
3531 (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",
3532 IDENTIFIER_POINTER (raw_name));
3533 return 1;
3534 }
3535 return 0;
3536 }
3537
3538 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3539 we remember ENCLOSING and SUPER. */
3540
3541 static tree
3542 resolve_inner_class (circularity_hash, cl, enclosing, super, class_type)
3543 struct hash_table *circularity_hash;
3544 tree cl, *enclosing, *super, class_type;
3545 {
3546 tree local_enclosing = *enclosing;
3547 tree local_super = NULL_TREE;
3548
3549 while (local_enclosing)
3550 {
3551 tree intermediate, decl;
3552
3553 hash_lookup (circularity_hash,
3554 (const hash_table_key) local_enclosing, TRUE, NULL);
3555
3556 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3557 return decl;
3558
3559 intermediate = local_enclosing;
3560 /* Explore enclosing contexts. */
3561 while (INNER_CLASS_DECL_P (intermediate))
3562 {
3563 intermediate = DECL_CONTEXT (intermediate);
3564 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3565 return decl;
3566 }
3567
3568 /* Now go to the upper classes, bail out if necessary. We will
3569 analyze the returned SUPER and act accordingly (see
3570 do_resolve_class.) */
3571 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3572 if (!local_super || local_super == object_type_node)
3573 break;
3574
3575 if (TREE_CODE (local_super) == POINTER_TYPE)
3576 local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3577 else
3578 local_super = TYPE_NAME (local_super);
3579
3580 /* We may not have checked for circular inheritance yet, so do so
3581 here to prevent an infinite loop. */
3582 if (hash_lookup (circularity_hash,
3583 (const hash_table_key) local_super, FALSE, NULL))
3584 {
3585 if (!cl)
3586 cl = lookup_cl (local_enclosing);
3587
3588 parse_error_context
3589 (cl, "Cyclic inheritance involving %s",
3590 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3591 local_enclosing = NULL_TREE;
3592 }
3593 else
3594 local_enclosing = local_super;
3595 }
3596
3597 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3598 *super = local_super;
3599 *enclosing = local_enclosing;
3600
3601 return NULL_TREE;
3602 }
3603
3604 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3605 qualified. */
3606
3607 static tree
3608 find_as_inner_class (enclosing, name, cl)
3609 tree enclosing, name, cl;
3610 {
3611 tree qual, to_return;
3612 if (!enclosing)
3613 return NULL_TREE;
3614
3615 name = TYPE_NAME (name);
3616
3617 /* First search: within the scope of `enclosing', search for name */
3618 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3619 qual = EXPR_WFL_QUALIFICATION (cl);
3620 else if (cl)
3621 qual = build_tree_list (cl, NULL_TREE);
3622 else
3623 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3624
3625 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3626 return to_return;
3627
3628 /* We're dealing with a qualified name. Try to resolve thing until
3629 we get something that is an enclosing class. */
3630 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3631 {
3632 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3633
3634 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3635 qual = TREE_CHAIN (qual))
3636 {
3637 acc = merge_qualified_name (acc,
3638 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3639 BUILD_PTR_FROM_NAME (ptr, acc);
3640 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3641 }
3642
3643 /* A NULL qual and a decl means that the search ended
3644 successfully?!? We have to do something then. FIXME */
3645
3646 if (decl)
3647 enclosing = decl;
3648 else
3649 qual = EXPR_WFL_QUALIFICATION (cl);
3650 }
3651 /* Otherwise, create a qual for the other part of the resolution. */
3652 else
3653 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3654
3655 return find_as_inner_class_do (qual, enclosing);
3656 }
3657
3658 /* We go inside the list of sub classes and try to find a way
3659 through. */
3660
3661 static tree
3662 find_as_inner_class_do (qual, enclosing)
3663 tree qual, enclosing;
3664 {
3665 if (!qual)
3666 return NULL_TREE;
3667
3668 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3669 {
3670 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3671 tree next_enclosing = NULL_TREE;
3672 tree inner_list;
3673
3674 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3675 inner_list; inner_list = TREE_CHAIN (inner_list))
3676 {
3677 if (TREE_VALUE (inner_list) == name_to_match)
3678 {
3679 next_enclosing = TREE_PURPOSE (inner_list);
3680 break;
3681 }
3682 }
3683 enclosing = next_enclosing;
3684 }
3685
3686 return (!qual && enclosing ? enclosing : NULL_TREE);
3687 }
3688
3689 /* Reach all inner classes and tie their unqualified name to a
3690 DECL. */
3691
3692 static void
3693 set_nested_class_simple_name_value (outer, set)
3694 tree outer;
3695 int set;
3696 {
3697 tree l;
3698
3699 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3700 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3701 TREE_PURPOSE (l) : NULL_TREE);
3702 }
3703
3704 static void
3705 link_nested_class_to_enclosing ()
3706 {
3707 if (GET_ENCLOSING_CPC ())
3708 {
3709 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3710 DECL_INNER_CLASS_LIST (enclosing) =
3711 tree_cons (GET_CPC (), GET_CPC_UN (),
3712 DECL_INNER_CLASS_LIST (enclosing));
3713 }
3714 }
3715
3716 static tree
3717 maybe_make_nested_class_name (name)
3718 tree name;
3719 {
3720 tree id = NULL_TREE;
3721
3722 if (CPC_INNER_P ())
3723 {
3724 make_nested_class_name (GET_CPC_LIST ());
3725 obstack_grow0 (&temporary_obstack,
3726 IDENTIFIER_POINTER (name),
3727 IDENTIFIER_LENGTH (name));
3728 id = get_identifier (obstack_finish (&temporary_obstack));
3729 if (ctxp->package)
3730 QUALIFIED_P (id) = 1;
3731 }
3732 return id;
3733 }
3734
3735 /* If DECL is NULL, create and push a new DECL, record the current
3736 line CL and do other maintenance things. */
3737
3738 static tree
3739 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3740 tree decl, raw_name, qualified_name, cl;
3741 {
3742 if (!decl)
3743 decl = push_class (make_class (), qualified_name);
3744
3745 /* Take care of the file and line business */
3746 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3747 /* If we're emiting xrefs, store the line/col number information */
3748 if (flag_emit_xref)
3749 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3750 else
3751 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3752 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3753 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3754 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3755 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3756
3757 PUSH_CPC (decl, raw_name);
3758 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3759
3760 /* Link the declaration to the already seen ones */
3761 TREE_CHAIN (decl) = ctxp->class_list;
3762 ctxp->class_list = decl;
3763
3764 /* Create a new nodes in the global lists */
3765 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3766 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3767
3768 /* Install a new dependency list element */
3769 create_jdep_list (ctxp);
3770
3771 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3772 IDENTIFIER_POINTER (qualified_name)));
3773 return decl;
3774 }
3775
3776 static void
3777 add_superinterfaces (decl, interface_list)
3778 tree decl, interface_list;
3779 {
3780 tree node;
3781 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3782 takes care of ensuring that:
3783 - This is an accessible interface type,
3784 - Circularity detection.
3785 parser_add_interface is then called. If present but not defined,
3786 the check operation is delayed until the super interface gets
3787 defined. */
3788 for (node = interface_list; node; node = TREE_CHAIN (node))
3789 {
3790 tree current = TREE_PURPOSE (node);
3791 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3792 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3793 {
3794 if (!parser_check_super_interface (idecl, decl, current))
3795 parser_add_interface (decl, idecl, current);
3796 }
3797 else
3798 register_incomplete_type (JDEP_INTERFACE,
3799 current, decl, NULL_TREE);
3800 }
3801 }
3802
3803 /* Create an interface in pass1 and return its decl. Return the
3804 interface's decl in pass 2. */
3805
3806 static tree
3807 create_interface (flags, id, super)
3808 int flags;
3809 tree id, super;
3810 {
3811 tree raw_name = EXPR_WFL_NODE (id);
3812 tree q_name = parser_qualified_classname (raw_name);
3813 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3814
3815 /* Certain syntax errors are making SUPER be like ID. Avoid this
3816 case. */
3817 if (ctxp->class_err && id == super)
3818 super = NULL;
3819
3820 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3821
3822 /* Basic checks: scope, redefinition, modifiers */
3823 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3824 {
3825 PUSH_ERROR ();
3826 return NULL_TREE;
3827 }
3828
3829 /* Suspend the current parsing context if we're parsing an inner
3830 interface */
3831 if (CPC_INNER_P ())
3832 {
3833 java_parser_context_suspend ();
3834 /* Interface members are public. */
3835 if (CLASS_INTERFACE (GET_CPC ()))
3836 flags |= ACC_PUBLIC;
3837 }
3838
3839 /* Push a new context for (static) initialized upon declaration fields */
3840 java_parser_context_push_initialized_field ();
3841
3842 /* Interface modifiers check
3843 - public/abstract allowed (already done at that point)
3844 - abstract is obsolete (comes first, it's a warning, or should be)
3845 - Can't use twice the same (checked in the modifier rule) */
3846 if ((flags & ACC_ABSTRACT) && flag_redundant)
3847 parse_warning_context
3848 (MODIFIER_WFL (ABSTRACT_TK),
3849 "Redundant use of `abstract' modifier. Interface `%s' is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3850
3851 /* Create a new decl if DECL is NULL, otherwise fix it */
3852 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3853
3854 /* Set super info and mark the class a complete */
3855 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3856 object_type_node, ctxp->interface_number);
3857 ctxp->interface_number = 0;
3858 CLASS_COMPLETE_P (decl) = 1;
3859 add_superinterfaces (decl, super);
3860
3861 return decl;
3862 }
3863
3864 /* Anonymous class counter. Will be reset to 1 every time a non
3865 anonymous class gets created. */
3866 static int anonymous_class_counter = 1;
3867
3868 /* Patch anonymous class CLASS, by either extending or implementing
3869 DEP. */
3870
3871 static void
3872 patch_anonymous_class (type_decl, class_decl, wfl)
3873 tree type_decl, class_decl, wfl;
3874 {
3875 tree class = TREE_TYPE (class_decl);
3876 tree type = TREE_TYPE (type_decl);
3877 tree binfo = TYPE_BINFO (class);
3878
3879 /* If it's an interface, implement it */
3880 if (CLASS_INTERFACE (type_decl))
3881 {
3882 tree s_binfo;
3883 int length;
3884
3885 if (parser_check_super_interface (type_decl, class_decl, wfl))
3886 return;
3887
3888 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3889 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3890 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3891 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3892 /* And add the interface */
3893 parser_add_interface (class_decl, type_decl, wfl);
3894 }
3895 /* Otherwise, it's a type we want to extend */
3896 else
3897 {
3898 if (parser_check_super (type_decl, class_decl, wfl))
3899 return;
3900 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3901 }
3902 }
3903
3904 static tree
3905 create_anonymous_class (location, type_name)
3906 int location;
3907 tree type_name;
3908 {
3909 char buffer [80];
3910 tree super = NULL_TREE, itf = NULL_TREE;
3911 tree id, type_decl, class;
3912
3913 /* The unqualified name of the anonymous class. It's just a number. */
3914 sprintf (buffer, "%d", anonymous_class_counter++);
3915 id = build_wfl_node (get_identifier (buffer));
3916 EXPR_WFL_LINECOL (id) = location;
3917
3918 /* We know about the type to extend/implement. We go ahead */
3919 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3920 {
3921 /* Create a class which either implements on extends the designated
3922 class. The class bears an innacessible name. */
3923 if (CLASS_INTERFACE (type_decl))
3924 {
3925 /* It's OK to modify it here. It's been already used and
3926 shouldn't be reused */
3927 ctxp->interface_number = 1;
3928 /* Interfaces should presented as a list of WFLs */
3929 itf = build_tree_list (type_name, NULL_TREE);
3930 }
3931 else
3932 super = type_name;
3933 }
3934
3935 class = create_class (ACC_FINAL, id, super, itf);
3936
3937 /* We didn't know anything about the stuff. We register a dependence. */
3938 if (!type_decl)
3939 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3940
3941 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3942 return class;
3943 }
3944
3945 /* Create a class in pass1 and return its decl. Return class
3946 interface's decl in pass 2. */
3947
3948 static tree
3949 create_class (flags, id, super, interfaces)
3950 int flags;
3951 tree id, super, interfaces;
3952 {
3953 tree raw_name = EXPR_WFL_NODE (id);
3954 tree class_id, decl;
3955 tree super_decl_type;
3956
3957 /* Certain syntax errors are making SUPER be like ID. Avoid this
3958 case. */
3959 if (ctxp->class_err && id == super)
3960 super = NULL;
3961
3962 class_id = parser_qualified_classname (raw_name);
3963 decl = IDENTIFIER_CLASS_VALUE (class_id);
3964 EXPR_WFL_NODE (id) = class_id;
3965
3966 /* Basic check: scope, redefinition, modifiers */
3967 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3968 {
3969 PUSH_ERROR ();
3970 return NULL_TREE;
3971 }
3972
3973 /* Suspend the current parsing context if we're parsing an inner
3974 class or an anonymous class. */
3975 if (CPC_INNER_P ())
3976 {
3977 java_parser_context_suspend ();
3978 /* Interface members are public. */
3979 if (CLASS_INTERFACE (GET_CPC ()))
3980 flags |= ACC_PUBLIC;
3981 }
3982
3983 /* Push a new context for (static) initialized upon declaration fields */
3984 java_parser_context_push_initialized_field ();
3985
3986 /* Class modifier check:
3987 - Allowed modifier (already done at that point)
3988 - abstract AND final forbidden
3989 - Public classes defined in the correct file */
3990 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3991 parse_error_context
3992 (id, "Class `%s' can't be declared both abstract and final",
3993 IDENTIFIER_POINTER (raw_name));
3994
3995 /* Create a new decl if DECL is NULL, otherwise fix it */
3996 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3997
3998 /* If SUPER exists, use it, otherwise use Object */
3999 if (super)
4000 {
4001 /* Can't extend java.lang.Object */
4002 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4003 {
4004 parse_error_context (id, "Can't extend `java.lang.Object'");
4005 return NULL_TREE;
4006 }
4007
4008 super_decl_type =
4009 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4010 }
4011 else if (TREE_TYPE (decl) != object_type_node)
4012 super_decl_type = object_type_node;
4013 /* We're defining java.lang.Object */
4014 else
4015 super_decl_type = NULL_TREE;
4016
4017 /* A class nested in an interface is implicitly static. */
4018 if (INNER_CLASS_DECL_P (decl)
4019 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4020 {
4021 flags |= ACC_STATIC;
4022 }
4023
4024 /* Set super info and mark the class as complete. */
4025 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4026 ctxp->interface_number);
4027 ctxp->interface_number = 0;
4028 CLASS_COMPLETE_P (decl) = 1;
4029 add_superinterfaces (decl, interfaces);
4030
4031 /* Add the private this$<n> field, Replicate final locals still in
4032 scope as private final fields mangled like val$<local_name>.
4033 This doesn't not occur for top level (static) inner classes. */
4034 if (PURE_INNER_CLASS_DECL_P (decl))
4035 add_inner_class_fields (decl, current_function_decl);
4036
4037 /* If doing xref, store the location at which the inherited class
4038 (if any) was seen. */
4039 if (flag_emit_xref && super)
4040 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4041
4042 /* Eventually sets the @deprecated tag flag */
4043 CHECK_DEPRECATED (decl);
4044
4045 /* Reset the anonymous class counter when declaring non inner classes */
4046 if (!INNER_CLASS_DECL_P (decl))
4047 anonymous_class_counter = 1;
4048
4049 return decl;
4050 }
4051
4052 /* End a class declaration: register the statements used to create
4053 finit$ and <clinit>, pop the current class and resume the prior
4054 parser context if necessary. */
4055
4056 static void
4057 end_class_declaration (resume)
4058 int resume;
4059 {
4060 /* If an error occurred, context weren't pushed and won't need to be
4061 popped by a resume. */
4062 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4063
4064 if (GET_CPC () != error_mark_node)
4065 dump_java_tree (TDI_class, GET_CPC ());
4066
4067 java_parser_context_pop_initialized_field ();
4068 POP_CPC ();
4069 if (resume && no_error_occurred)
4070 java_parser_context_resume ();
4071
4072 /* We're ending a class declaration, this is a good time to reset
4073 the interface cout. Note that might have been already done in
4074 create_interface, but if at that time an inner class was being
4075 dealt with, the interface count was reset in a context created
4076 for the sake of handling inner classes declaration. */
4077 ctxp->interface_number = 0;
4078 }
4079
4080 static void
4081 add_inner_class_fields (class_decl, fct_decl)
4082 tree class_decl;
4083 tree fct_decl;
4084 {
4085 tree block, marker, f;
4086
4087 f = add_field (TREE_TYPE (class_decl),
4088 build_current_thisn (TREE_TYPE (class_decl)),
4089 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4090 ACC_PRIVATE);
4091 FIELD_THISN (f) = 1;
4092
4093 if (!fct_decl)
4094 return;
4095
4096 for (block = GET_CURRENT_BLOCK (fct_decl);
4097 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4098 {
4099 tree decl;
4100 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4101 {
4102 tree name, pname;
4103 tree wfl, init, list;
4104
4105 /* Avoid non final arguments. */
4106 if (!LOCAL_FINAL_P (decl))
4107 continue;
4108
4109 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4110 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4111 wfl = build_wfl_node (name);
4112 init = build_wfl_node (pname);
4113 /* Build an initialization for the field: it will be
4114 initialized by a parameter added to finit$, bearing a
4115 mangled name of the field itself (param$<n>.) The
4116 parameter is provided to finit$ by the constructor
4117 invoking it (hence the constructor will also feature a
4118 hidden parameter, set to the value of the outer context
4119 local at the time the inner class is created.)
4120
4121 Note: we take into account all possible locals that can
4122 be accessed by the inner class. It's actually not trivial
4123 to minimize these aliases down to the ones really
4124 used. One way to do that would be to expand all regular
4125 methods first, then finit$ to get a picture of what's
4126 used. It works with the exception that we would have to
4127 go back on all constructor invoked in regular methods to
4128 have their invokation reworked (to include the right amount
4129 of alias initializer parameters.)
4130
4131 The only real way around, I think, is a first pass to
4132 identify locals really used in the inner class. We leave
4133 the flag FIELD_LOCAL_ALIAS_USED around for that future
4134 use.
4135
4136 On the other hand, it only affect local inner classes,
4137 whose constructors (and finit$ call) will be featuring
4138 unecessary arguments. It's easy for a developper to keep
4139 this number of parameter down by using the `final'
4140 keyword only when necessary. For the time being, we can
4141 issue a warning on unecessary finals. FIXME */
4142 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4143 wfl, init);
4144
4145 /* Register the field. The TREE_LIST holding the part
4146 initialized/initializer will be marked ARG_FINAL_P so
4147 that the created field can be marked
4148 FIELD_LOCAL_ALIAS. */
4149 list = build_tree_list (wfl, init);
4150 ARG_FINAL_P (list) = 1;
4151 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4152 }
4153 }
4154
4155 if (!CPC_INITIALIZER_STMT (ctxp))
4156 return;
4157
4158 /* If we ever registered an alias field, insert and marker to
4159 remeber where the list ends. The second part of the list (the one
4160 featuring initialized fields) so it can be later reversed to
4161 enforce 8.5. The marker will be removed during that operation. */
4162 marker = build_tree_list (NULL_TREE, NULL_TREE);
4163 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4164 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4165 }
4166
4167 /* Can't use lookup_field () since we don't want to load the class and
4168 can't set the CLASS_LOADED_P flag */
4169
4170 static tree
4171 find_field (class, name)
4172 tree class;
4173 tree name;
4174 {
4175 tree decl;
4176 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4177 {
4178 if (DECL_NAME (decl) == name)
4179 return decl;
4180 }
4181 return NULL_TREE;
4182 }
4183
4184 /* Wrap around lookup_field that doesn't potentially upset the value
4185 of CLASS */
4186
4187 static tree
4188 lookup_field_wrapper (class, name)
4189 tree class, name;
4190 {
4191 tree type = class;
4192 tree decl = NULL_TREE;
4193 java_parser_context_save_global ();
4194
4195 /* Last chance: if we're within the context of an inner class, we
4196 might be trying to access a local variable defined in an outer
4197 context. We try to look for it now. */
4198 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4199 {
4200 tree new_name;
4201 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4202 decl = lookup_field (&type, new_name);
4203 if (decl && decl != error_mark_node)
4204 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4205 }
4206 if (!decl || decl == error_mark_node)
4207 {
4208 type = class;
4209 decl = lookup_field (&type, name);
4210 }
4211
4212 java_parser_context_restore_global ();
4213 return decl == error_mark_node ? NULL : decl;
4214 }
4215
4216 /* Find duplicate field within the same class declarations and report
4217 the error. Returns 1 if a duplicated field was found, 0
4218 otherwise. */
4219
4220 static int
4221 duplicate_declaration_error_p (new_field_name, new_type, cl)
4222 tree new_field_name, new_type, cl;
4223 {
4224 /* This might be modified to work with method decl as well */
4225 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4226 if (decl)
4227 {
4228 char *t1 = xstrdup (purify_type_name
4229 ((TREE_CODE (new_type) == POINTER_TYPE
4230 && TREE_TYPE (new_type) == NULL_TREE) ?
4231 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4232 lang_printable_name (new_type, 1)));
4233 /* The type may not have been completed by the time we report
4234 the error */
4235 char *t2 = xstrdup (purify_type_name
4236 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4237 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4238 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4239 lang_printable_name (TREE_TYPE (decl), 1)));
4240 parse_error_context
4241 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4242 t1, IDENTIFIER_POINTER (new_field_name),
4243 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4244 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4245 free (t1);
4246 free (t2);
4247 return 1;
4248 }
4249 return 0;
4250 }
4251
4252 /* Field registration routine. If TYPE doesn't exist, field
4253 declarations are linked to the undefined TYPE dependency list, to
4254 be later resolved in java_complete_class () */
4255
4256 static void
4257 register_fields (flags, type, variable_list)
4258 int flags;
4259 tree type, variable_list;
4260 {
4261 tree current, saved_type;
4262 tree class_type = NULL_TREE;
4263 int saved_lineno = lineno;
4264 int must_chain = 0;
4265 tree wfl = NULL_TREE;
4266
4267 if (GET_CPC ())
4268 class_type = TREE_TYPE (GET_CPC ());
4269
4270 if (!class_type || class_type == error_mark_node)
4271 return;
4272
4273 /* If we're adding fields to interfaces, those fields are public,
4274 static, final */
4275 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4276 {
4277 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4278 flags, ACC_PUBLIC, "interface field(s)");
4279 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4280 flags, ACC_STATIC, "interface field(s)");
4281 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4282 flags, ACC_FINAL, "interface field(s)");
4283 check_modifiers ("Illegal interface member modifier `%s'", flags,
4284 INTERFACE_FIELD_MODIFIERS);
4285 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4286 }
4287
4288 /* Obtain a suitable type for resolution, if necessary */
4289 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4290
4291 /* If TYPE is fully resolved and we don't have a reference, make one */
4292 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4293
4294 for (current = variable_list, saved_type = type; current;
4295 current = TREE_CHAIN (current), type = saved_type)
4296 {
4297 tree real_type;
4298 tree field_decl;
4299 tree cl = TREE_PURPOSE (current);
4300 tree init = TREE_VALUE (current);
4301 tree current_name = EXPR_WFL_NODE (cl);
4302
4303 /* Can't declare non-final static fields in inner classes */
4304 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4305 && !(flags & ACC_FINAL))
4306 parse_error_context
4307 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4308 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4309 lang_printable_name (class_type, 0));
4310
4311 /* Process NAME, as it may specify extra dimension(s) for it */
4312 type = build_array_from_name (type, wfl, current_name, &current_name);
4313
4314 /* Type adjustment. We may have just readjusted TYPE because
4315 the variable specified more dimensions. Make sure we have
4316 a reference if we can and don't have one already. Also
4317 change the name if we have an init. */
4318 if (type != saved_type)
4319 {
4320 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4321 if (init)
4322 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4323 }
4324
4325 real_type = GET_REAL_TYPE (type);
4326 /* Check for redeclarations */
4327 if (duplicate_declaration_error_p (current_name, real_type, cl))
4328 continue;
4329
4330 /* Set lineno to the line the field was found and create a
4331 declaration for it. Eventually sets the @deprecated tag flag. */
4332 if (flag_emit_xref)
4333 lineno = EXPR_WFL_LINECOL (cl);
4334 else
4335 lineno = EXPR_WFL_LINENO (cl);
4336 field_decl = add_field (class_type, current_name, real_type, flags);
4337 CHECK_DEPRECATED (field_decl);
4338
4339 /* If the field denotes a final instance variable, then we
4340 allocate a LANG_DECL_SPECIFIC part to keep track of its
4341 initialization. We also mark whether the field was
4342 initialized upon its declaration. We don't do that if the
4343 created field is an alias to a final local. */
4344 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4345 {
4346 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4347 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4348 }
4349
4350 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4351 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4352 hide parameters to this inner class finit$ and
4353 constructors. It also means that the field isn't final per
4354 say. */
4355 if (ARG_FINAL_P (current))
4356 {
4357 FIELD_LOCAL_ALIAS (field_decl) = 1;
4358 FIELD_FINAL (field_decl) = 0;
4359 }
4360
4361 /* Check if we must chain. */
4362 if (must_chain)
4363 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4364
4365 /* If we have an initialization value tied to the field */
4366 if (init)
4367 {
4368 /* The field is declared static */
4369 if (flags & ACC_STATIC)
4370 {
4371 /* We include the field and its initialization part into
4372 a list used to generate <clinit>. After <clinit> is
4373 walked, field initializations will be processed and
4374 fields initialized with known constants will be taken
4375 out of <clinit> and have their DECL_INITIAL set
4376 appropriately. */
4377 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4378 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4379 if (TREE_OPERAND (init, 1)
4380 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4381 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4382 }
4383 /* A non-static field declared with an immediate initialization is
4384 to be initialized in <init>, if any. This field is remembered
4385 to be processed at the time of the generation of <init>. */
4386 else
4387 {
4388 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4389 SET_CPC_INITIALIZER_STMT (ctxp, init);
4390 }
4391 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4392 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4393 }
4394 }
4395 lineno = saved_lineno;
4396 }
4397
4398 /* Generate finit$, using the list of initialized fields to populate
4399 its body. finit$'s parameter(s) list is adjusted to include the
4400 one(s) used to initialized the field(s) caching outer context
4401 local(s). */
4402
4403 static tree
4404 generate_finit (class_type)
4405 tree class_type;
4406 {
4407 int count = 0;
4408 tree list = TYPE_FINIT_STMT_LIST (class_type);
4409 tree mdecl, current, parms;
4410
4411 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4412 class_type, NULL_TREE,
4413 &count);
4414 CRAFTED_PARAM_LIST_FIXUP (parms);
4415 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4416 finit_identifier_node, parms);
4417 fix_method_argument_names (parms, mdecl);
4418 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4419 mdecl, NULL_TREE);
4420 DECL_FUNCTION_NAP (mdecl) = count;
4421 start_artificial_method_body (mdecl);
4422
4423 for (current = list; current; current = TREE_CHAIN (current))
4424 java_method_add_stmt (mdecl,
4425 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4426 current));
4427 end_artificial_method_body (mdecl);
4428 return mdecl;
4429 }
4430
4431 /* Generate a function to run the instance initialization code. The
4432 private method is called `instinit$'. Unless we're dealing with an
4433 anonymous class, we determine whether all ctors of CLASS_TYPE
4434 declare a checked exception in their `throws' clause in order to
4435 see whether it's necessary to encapsulate the instance initializer
4436 statements in a try/catch/rethrow sequence. */
4437
4438 static tree
4439 generate_instinit (class_type)
4440 tree class_type;
4441 {
4442 tree current;
4443 tree compound = NULL_TREE;
4444 tree parms = tree_cons (this_identifier_node,
4445 build_pointer_type (class_type), end_params_node);
4446 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4447 void_type_node,
4448 instinit_identifier_node, parms);
4449
4450 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4451 mdecl, NULL_TREE);
4452
4453 /* Gather all the statements in a compound */
4454 for (current = TYPE_II_STMT_LIST (class_type);
4455 current; current = TREE_CHAIN (current))
4456 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4457
4458 /* We need to encapsulate COMPOUND by a try/catch statement to
4459 rethrow exceptions that might occur in the instance initializer.
4460 We do that only if all ctors of CLASS_TYPE are set to catch a
4461 checked exception. This doesn't apply to anonymous classes (since
4462 they don't have declared ctors.) */
4463 if (!ANONYMOUS_CLASS_P (class_type) &&
4464 ctors_unchecked_throws_clause_p (class_type))
4465 {
4466 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4467 build1 (THROW_EXPR, NULL_TREE,
4468 build_wfl_node (wpv_id)));
4469 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4470 exception_type_node);
4471 }
4472
4473 start_artificial_method_body (mdecl);
4474 java_method_add_stmt (mdecl, compound);
4475 end_artificial_method_body (mdecl);
4476
4477 return mdecl;
4478 }
4479
4480 /* FIXME */
4481 static tree
4482 build_instinit_invocation (class_type)
4483 tree class_type;
4484 {
4485 tree to_return = NULL_TREE;
4486
4487 if (TYPE_II_STMT_LIST (class_type))
4488 {
4489 tree parm = build_tree_list (NULL_TREE,
4490 build_wfl_node (this_identifier_node));
4491 to_return =
4492 build_method_invocation (build_wfl_node (instinit_identifier_node),
4493 parm);
4494 }
4495 return to_return;
4496 }
4497
4498 /* Shared accros method_declarator and method_header to remember the
4499 patch stage that was reached during the declaration of the method.
4500 A method DECL is built differently is there is no patch
4501 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4502 pending on the currently defined method. */
4503
4504 static int patch_stage;
4505
4506 /* Check the method declaration and add the method to its current
4507 class. If the argument list is known to contain incomplete types,
4508 the method is partially added and the registration will be resume
4509 once the method arguments resolved. If TYPE is NULL, we're dealing
4510 with a constructor. */
4511
4512 static tree
4513 method_header (flags, type, mdecl, throws)
4514 int flags;
4515 tree type, mdecl, throws;
4516 {
4517 tree type_wfl = NULL_TREE;
4518 tree meth_name = NULL_TREE;
4519 tree current, orig_arg, this_class = NULL;
4520 tree id, meth;
4521 int saved_lineno;
4522 int constructor_ok = 0, must_chain;
4523 int count;
4524
4525 if (mdecl == error_mark_node)
4526 return error_mark_node;
4527 meth = TREE_VALUE (mdecl);
4528 id = TREE_PURPOSE (mdecl);
4529
4530 check_modifiers_consistency (flags);
4531
4532 if (GET_CPC ())
4533 this_class = TREE_TYPE (GET_CPC ());
4534
4535 if (!this_class || this_class == error_mark_node)
4536 return NULL_TREE;
4537
4538 /* There are some forbidden modifiers for an abstract method and its
4539 class must be abstract as well. */
4540 if (type && (flags & ACC_ABSTRACT))
4541 {
4542 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4543 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4544 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4545 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4546 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4547 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4548 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4549 parse_error_context
4550 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4551 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4552 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4553 }
4554
4555 /* Things to be checked when declaring a constructor */
4556 if (!type)
4557 {
4558 int ec = java_error_count;
4559 /* 8.6: Constructor declarations: we might be trying to define a
4560 method without specifying a return type. */
4561 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4562 parse_error_context
4563 (id, "Invalid method declaration, return type required");
4564 /* 8.6.3: Constructor modifiers */
4565 else
4566 {
4567 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4568 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4569 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4570 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4571 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4572 }
4573 /* If we found error here, we don't consider it's OK to tread
4574 the method definition as a constructor, for the rest of this
4575 function */
4576 if (ec == java_error_count)
4577 constructor_ok = 1;
4578 }
4579
4580 /* Method declared within the scope of an interface are implicitly
4581 abstract and public. Conflicts with other erroneously provided
4582 modifiers are checked right after. */
4583
4584 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4585 {
4586 /* If FLAGS isn't set because of a modifier, turn the
4587 corresponding modifier WFL to NULL so we issue a warning on
4588 the obsolete use of the modifier */
4589 if (!(flags & ACC_PUBLIC))
4590 MODIFIER_WFL (PUBLIC_TK) = NULL;
4591 if (!(flags & ACC_ABSTRACT))
4592 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4593 flags |= ACC_PUBLIC;
4594 flags |= ACC_ABSTRACT;
4595 }
4596
4597 /* Inner class can't declare static methods */
4598 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4599 {
4600 parse_error_context
4601 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4602 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4603 lang_printable_name (this_class, 0));
4604 }
4605
4606 /* Modifiers context reset moved up, so abstract method declaration
4607 modifiers can be later checked. */
4608
4609 /* Set constructor returned type to void and method name to <init>,
4610 unless we found an error identifier the constructor (in which
4611 case we retain the original name) */
4612 if (!type)
4613 {
4614 type = void_type_node;
4615 if (constructor_ok)
4616 meth_name = init_identifier_node;
4617 }
4618 else
4619 meth_name = EXPR_WFL_NODE (id);
4620
4621 /* Do the returned type resolution and registration if necessary */
4622 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4623
4624 if (meth_name)
4625 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4626 EXPR_WFL_NODE (id) = meth_name;
4627 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4628
4629 if (must_chain)
4630 {
4631 patch_stage = JDEP_METHOD_RETURN;
4632 register_incomplete_type (patch_stage, type_wfl, id, type);
4633 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4634 }
4635 else
4636 TREE_TYPE (meth) = type;
4637
4638 saved_lineno = lineno;
4639 /* When defining an abstract or interface method, the curly
4640 bracket at level 1 doesn't exist because there is no function
4641 body */
4642 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4643 EXPR_WFL_LINENO (id));
4644
4645 /* Remember the original argument list */
4646 orig_arg = TYPE_ARG_TYPES (meth);
4647
4648 if (patch_stage) /* includes ret type and/or all args */
4649 {
4650 jdep *jdep;
4651 meth = add_method_1 (this_class, flags, meth_name, meth);
4652 /* Patch for the return type */
4653 if (patch_stage == JDEP_METHOD_RETURN)
4654 {
4655 jdep = CLASSD_LAST (ctxp->classd_list);
4656 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4657 }
4658 /* This is the stop JDEP. METH allows the function's signature
4659 to be computed. */
4660 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4661 }
4662 else
4663 meth = add_method (this_class, flags, meth_name,
4664 build_java_signature (meth));
4665
4666 /* Remember final parameters */
4667 MARK_FINAL_PARMS (meth, orig_arg);
4668
4669 /* Fix the method argument list so we have the argument name
4670 information */
4671 fix_method_argument_names (orig_arg, meth);
4672
4673 /* Register the parameter number and re-install the current line
4674 number */
4675 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4676 lineno = saved_lineno;
4677
4678 /* Register exception specified by the `throws' keyword for
4679 resolution and set the method decl appropriate field to the list.
4680 Note: the grammar ensures that what we get here are class
4681 types. */
4682 if (throws)
4683 {
4684 throws = nreverse (throws);
4685 for (current = throws; current; current = TREE_CHAIN (current))
4686 {
4687 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4688 NULL_TREE, NULL_TREE);
4689 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4690 &TREE_VALUE (current);
4691 }
4692 DECL_FUNCTION_THROWS (meth) = throws;
4693 }
4694
4695 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4696 DECL_FUNCTION_WFL (meth) = id;
4697
4698 /* Set the flag if we correctly processed a constructor */
4699 if (constructor_ok)
4700 {
4701 DECL_CONSTRUCTOR_P (meth) = 1;
4702 /* Compute and store the number of artificial parameters declared
4703 for this constructor */
4704 for (count = 0, current = TYPE_FIELDS (this_class); current;
4705 current = TREE_CHAIN (current))
4706 if (FIELD_LOCAL_ALIAS (current))
4707 count++;
4708 DECL_FUNCTION_NAP (meth) = count;
4709 }
4710
4711 /* Eventually set the @deprecated tag flag */
4712 CHECK_DEPRECATED (meth);
4713
4714 /* If doing xref, store column and line number information instead
4715 of the line number only. */
4716 if (flag_emit_xref)
4717 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4718
4719 return meth;
4720 }
4721
4722 static void
4723 fix_method_argument_names (orig_arg, meth)
4724 tree orig_arg, meth;
4725 {
4726 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4727 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4728 {
4729 TREE_PURPOSE (arg) = this_identifier_node;
4730 arg = TREE_CHAIN (arg);
4731 }
4732 while (orig_arg != end_params_node)
4733 {
4734 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4735 orig_arg = TREE_CHAIN (orig_arg);
4736 arg = TREE_CHAIN (arg);
4737 }
4738 }
4739
4740 /* Complete the method declaration with METHOD_BODY. */
4741
4742 static void
4743 finish_method_declaration (method_body)
4744 tree method_body;
4745 {
4746 int flags;
4747
4748 if (!current_function_decl)
4749 return;
4750
4751 flags = get_access_flags_from_decl (current_function_decl);
4752
4753 /* 8.4.5 Method Body */
4754 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4755 {
4756 tree name = DECL_NAME (current_function_decl);
4757 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4758 "%s method `%s' can't have a body defined",
4759 (METHOD_NATIVE (current_function_decl) ?
4760 "Native" : "Abstract"),
4761 IDENTIFIER_POINTER (name));
4762 method_body = NULL_TREE;
4763 }
4764 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4765 {
4766 tree name = DECL_NAME (current_function_decl);
4767 parse_error_context
4768 (DECL_FUNCTION_WFL (current_function_decl),
4769 "Non native and non abstract method `%s' must have a body defined",
4770 IDENTIFIER_POINTER (name));
4771 method_body = NULL_TREE;
4772 }
4773
4774 if (flag_emit_class_files && method_body
4775 && TREE_CODE (method_body) == NOP_EXPR
4776 && TREE_TYPE (current_function_decl)
4777 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4778 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4779
4780 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4781 maybe_absorb_scoping_blocks ();
4782 /* Exit function's body */
4783 exit_block ();
4784 /* Merge last line of the function with first line, directly in the
4785 function decl. It will be used to emit correct debug info. */
4786 if (!flag_emit_xref)
4787 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4788
4789 /* Since function's argument's list are shared, reset the
4790 ARG_FINAL_P parameter that might have been set on some of this
4791 function parameters. */
4792 UNMARK_FINAL_PARMS (current_function_decl);
4793
4794 /* So we don't have an irrelevant function declaration context for
4795 the next static block we'll see. */
4796 current_function_decl = NULL_TREE;
4797 }
4798
4799 /* Build a an error message for constructor circularity errors. */
4800
4801 static char *
4802 constructor_circularity_msg (from, to)
4803 tree from, to;
4804 {
4805 static char string [4096];
4806 char *t = xstrdup (lang_printable_name (from, 0));
4807 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4808 free (t);
4809 return string;
4810 }
4811
4812 /* Verify a circular call to METH. Return 1 if an error is found, 0
4813 otherwise. */
4814
4815 static int
4816 verify_constructor_circularity (meth, current)
4817 tree meth, current;
4818 {
4819 static tree list = NULL_TREE;
4820 static int initialized_p;
4821 tree c;
4822
4823 /* If we haven't already registered LIST with the garbage collector,
4824 do so now. */
4825 if (!initialized_p)
4826 {
4827 ggc_add_tree_root (&list, 1);
4828 initialized_p = 1;
4829 }
4830
4831 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4832 {
4833 if (TREE_VALUE (c) == meth)
4834 {
4835 char *t;
4836 if (list)
4837 {
4838 tree liste;
4839 list = nreverse (list);
4840 for (liste = list; liste; liste = TREE_CHAIN (liste))
4841 {
4842 parse_error_context
4843 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4844 constructor_circularity_msg
4845 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4846 java_error_count--;
4847 }
4848 }
4849 t = xstrdup (lang_printable_name (meth, 0));
4850 parse_error_context (TREE_PURPOSE (c),
4851 "%s: recursive invocation of constructor `%s'",
4852 constructor_circularity_msg (current, meth), t);
4853 free (t);
4854 list = NULL_TREE;
4855 return 1;
4856 }
4857 }
4858 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4859 {
4860 list = tree_cons (c, current, list);
4861 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4862 return 1;
4863 list = TREE_CHAIN (list);
4864 }
4865 return 0;
4866 }
4867
4868 /* Check modifiers that can be declared but exclusively */
4869
4870 static void
4871 check_modifiers_consistency (flags)
4872 int flags;
4873 {
4874 int acc_count = 0;
4875 tree cl = NULL_TREE;
4876
4877 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4878 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4879 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4880 if (acc_count > 1)
4881 parse_error_context
4882 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4883
4884 acc_count = 0;
4885 cl = NULL_TREE;
4886 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4887 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4888 if (acc_count > 1)
4889 parse_error_context (cl,
4890 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4891 }
4892
4893 /* Check the methode header METH for abstract specifics features */
4894
4895 static void
4896 check_abstract_method_header (meth)
4897 tree meth;
4898 {
4899 int flags = get_access_flags_from_decl (meth);
4900
4901 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4902 ACC_ABSTRACT, "abstract method",
4903 IDENTIFIER_POINTER (DECL_NAME (meth)));
4904 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4905 ACC_PUBLIC, "abstract method",
4906 IDENTIFIER_POINTER (DECL_NAME (meth)));
4907
4908 check_modifiers ("Illegal modifier `%s' for interface method",
4909 flags, INTERFACE_METHOD_MODIFIERS);
4910 }
4911
4912 /* Create a FUNCTION_TYPE node and start augmenting it with the
4913 declared function arguments. Arguments type that can't be resolved
4914 are left as they are, but the returned node is marked as containing
4915 incomplete types. */
4916
4917 static tree
4918 method_declarator (id, list)
4919 tree id, list;
4920 {
4921 tree arg_types = NULL_TREE, current, node;
4922 tree meth = make_node (FUNCTION_TYPE);
4923 jdep *jdep;
4924
4925 patch_stage = JDEP_NO_PATCH;
4926
4927 if (GET_CPC () == error_mark_node)
4928 return error_mark_node;
4929
4930 /* If we're dealing with an inner class constructor, we hide the
4931 this$<n> decl in the name field of its parameter declaration. We
4932 also might have to hide the outer context local alias
4933 initializers. Not done when the class is a toplevel class. */
4934 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4935 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4936 {
4937 tree aliases_list, type, thisn;
4938 /* First the aliases, linked to the regular parameters */
4939 aliases_list =
4940 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4941 TREE_TYPE (GET_CPC ()),
4942 NULL_TREE, NULL);
4943 list = chainon (nreverse (aliases_list), list);
4944
4945 /* Then this$<n> */
4946 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4947 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4948 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4949 list);
4950 }
4951
4952 for (current = list; current; current = TREE_CHAIN (current))
4953 {
4954 int must_chain = 0;
4955 tree wfl_name = TREE_PURPOSE (current);
4956 tree type = TREE_VALUE (current);
4957 tree name = EXPR_WFL_NODE (wfl_name);
4958 tree already, arg_node;
4959 tree type_wfl = NULL_TREE;
4960 tree real_type;
4961
4962 /* Obtain a suitable type for resolution, if necessary */
4963 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4964
4965 /* Process NAME, as it may specify extra dimension(s) for it */
4966 type = build_array_from_name (type, type_wfl, name, &name);
4967 EXPR_WFL_NODE (wfl_name) = name;
4968
4969 real_type = GET_REAL_TYPE (type);
4970 if (TREE_CODE (real_type) == RECORD_TYPE)
4971 {
4972 real_type = promote_type (real_type);
4973 if (TREE_CODE (type) == TREE_LIST)
4974 TREE_PURPOSE (type) = real_type;
4975 }
4976
4977 /* Check redefinition */
4978 for (already = arg_types; already; already = TREE_CHAIN (already))
4979 if (TREE_PURPOSE (already) == name)
4980 {
4981 parse_error_context
4982 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4983 IDENTIFIER_POINTER (name),
4984 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4985 break;
4986 }
4987
4988 /* If we've an incomplete argument type, we know there is a location
4989 to patch when the type get resolved, later. */
4990 jdep = NULL;
4991 if (must_chain)
4992 {
4993 patch_stage = JDEP_METHOD;
4994 type = register_incomplete_type (patch_stage,
4995 type_wfl, wfl_name, type);
4996 jdep = CLASSD_LAST (ctxp->classd_list);
4997 JDEP_MISC (jdep) = id;
4998 }
4999
5000 /* The argument node: a name and a (possibly) incomplete type. */
5001 arg_node = build_tree_list (name, real_type);
5002 /* Remeber arguments declared final. */
5003 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5004
5005 if (jdep)
5006 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5007 TREE_CHAIN (arg_node) = arg_types;
5008 arg_types = arg_node;
5009 }
5010 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5011 node = build_tree_list (id, meth);
5012 return node;
5013 }
5014
5015 static int
5016 unresolved_type_p (wfl, returned)
5017 tree wfl;
5018 tree *returned;
5019
5020 {
5021 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5022 {
5023 if (returned)
5024 {
5025 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5026 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5027 *returned = TREE_TYPE (decl);
5028 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5029 *returned = TREE_TYPE (GET_CPC ());
5030 else
5031 *returned = NULL_TREE;
5032 }
5033 return 1;
5034 }
5035 if (returned)
5036 *returned = wfl;
5037 return 0;
5038 }
5039
5040 /* From NAME, build a qualified identifier node using the
5041 qualification from the current package definition. */
5042
5043 static tree
5044 parser_qualified_classname (name)
5045 tree name;
5046 {
5047 tree nested_class_name;
5048
5049 if ((nested_class_name = maybe_make_nested_class_name (name)))
5050 return nested_class_name;
5051
5052 if (ctxp->package)
5053 return merge_qualified_name (ctxp->package, name);
5054 else
5055 return name;
5056 }
5057
5058 /* Called once the type a interface extends is resolved. Returns 0 if
5059 everything is OK. */
5060
5061 static int
5062 parser_check_super_interface (super_decl, this_decl, this_wfl)
5063 tree super_decl, this_decl, this_wfl;
5064 {
5065 tree super_type = TREE_TYPE (super_decl);
5066
5067 /* Has to be an interface */
5068 if (!CLASS_INTERFACE (super_decl))
5069 {
5070 parse_error_context
5071 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
5072 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5073 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
5074 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5075 "interface" : "class"),
5076 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
5077 return 1;
5078 }
5079
5080 /* Check top-level interface access. Inner classes are subject to member
5081 access rules (6.6.1). */
5082 if (! INNER_CLASS_P (super_type)
5083 && check_pkg_class_access (DECL_NAME (super_decl),
5084 lookup_cl (this_decl), true))
5085 return 1;
5086
5087 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5088 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5089 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5090 return 0;
5091 }
5092
5093 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5094 0 if everthing is OK. */
5095
5096 static int
5097 parser_check_super (super_decl, this_decl, wfl)
5098 tree super_decl, this_decl, wfl;
5099 {
5100 tree super_type = TREE_TYPE (super_decl);
5101
5102 /* SUPER should be a CLASS (neither an array nor an interface) */
5103 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5104 {
5105 parse_error_context
5106 (wfl, "Class `%s' can't subclass %s `%s'",
5107 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5108 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5109 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5110 return 1;
5111 }
5112
5113 if (CLASS_FINAL (TYPE_NAME (super_type)))
5114 {
5115 parse_error_context (wfl, "Can't subclass final classes: %s",
5116 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5117 return 1;
5118 }
5119
5120 /* Check top-level class scope. Inner classes are subject to member access
5121 rules (6.6.1). */
5122 if (! INNER_CLASS_P (super_type)
5123 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true)))
5124 return 1;
5125
5126 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5127 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5128 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5129 return 0;
5130 }
5131
5132 /* Create a new dependency list and link it (in a LIFO manner) to the
5133 CTXP list of type dependency list. */
5134
5135 static void
5136 create_jdep_list (ctxp)
5137 struct parser_ctxt *ctxp;
5138 {
5139 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
5140 new->first = new->last = NULL;
5141 new->next = ctxp->classd_list;
5142 ctxp->classd_list = new;
5143 }
5144
5145 static jdeplist *
5146 reverse_jdep_list (ctxp)
5147 struct parser_ctxt *ctxp;
5148 {
5149 register jdeplist *prev = NULL, *current, *next;
5150 for (current = ctxp->classd_list; current; current = next)
5151 {
5152 next = current->next;
5153 current->next = prev;
5154 prev = current;
5155 }
5156 return prev;
5157 }
5158
5159 /* Create a fake pointer based on the ID stored in
5160 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5161 registered again. */
5162
5163 static tree
5164 obtain_incomplete_type (type_name)
5165 tree type_name;
5166 {
5167 tree ptr = NULL_TREE, name;
5168
5169 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5170 name = EXPR_WFL_NODE (type_name);
5171 else if (INCOMPLETE_TYPE_P (type_name))
5172 name = TYPE_NAME (type_name);
5173 else
5174 abort ();
5175
5176 BUILD_PTR_FROM_NAME (ptr, name);
5177 layout_type (ptr);
5178
5179 return ptr;
5180 }
5181
5182 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5183 non NULL instead of computing a new fake type based on WFL. The new
5184 dependency is inserted in the current type dependency list, in FIFO
5185 manner. */
5186
5187 static tree
5188 register_incomplete_type (kind, wfl, decl, ptr)
5189 int kind;
5190 tree wfl, decl, ptr;
5191 {
5192 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5193
5194 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5195 ptr = obtain_incomplete_type (wfl);
5196
5197 JDEP_KIND (new) = kind;
5198 JDEP_DECL (new) = decl;
5199 JDEP_TO_RESOLVE (new) = ptr;
5200 JDEP_WFL (new) = wfl;
5201 JDEP_CHAIN (new) = NULL;
5202 JDEP_MISC (new) = NULL_TREE;
5203 /* For some dependencies, set the enclosing class of the current
5204 class to be the enclosing context */
5205 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
5206 && GET_ENCLOSING_CPC ())
5207 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5208 else if (kind == JDEP_SUPER)
5209 JDEP_ENCLOSING (new) = (GET_ENCLOSING_CPC () ?
5210 TREE_VALUE (GET_ENCLOSING_CPC ()) : NULL_TREE);
5211 else
5212 JDEP_ENCLOSING (new) = GET_CPC ();
5213 JDEP_GET_PATCH (new) = (tree *)NULL;
5214
5215 JDEP_INSERT (ctxp->classd_list, new);
5216
5217 return ptr;
5218 }
5219
5220 /* This checks for circular references with innerclasses. We start
5221 from SOURCE and should never reach TARGET. Extended/implemented
5222 types in SOURCE have their enclosing context checked not to reach
5223 TARGET. When the last enclosing context of SOURCE is reached, its
5224 extended/implemented types are also checked not to reach TARGET.
5225 In case of error, WFL of the offending type is returned; NULL_TREE
5226 otherwise. */
5227
5228 static tree
5229 check_inner_circular_reference (source, target)
5230 tree source;
5231 tree target;
5232 {
5233 tree basetype_vec = TYPE_BINFO_BASETYPES (source);
5234 tree ctx, cl;
5235 int i;
5236
5237 if (!basetype_vec)
5238 return NULL_TREE;
5239
5240 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5241 {
5242 tree su = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
5243
5244 if (inherits_from_p (su, target))
5245 return lookup_cl (TYPE_NAME (su));
5246
5247 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5248 {
5249 /* An enclosing context shouldn't be TARGET */
5250 if (ctx == TYPE_NAME (target))
5251 return lookup_cl (TYPE_NAME (su));
5252
5253 /* When we reach the enclosing last context, start a check
5254 on it, with the same target */
5255 if (! DECL_CONTEXT (ctx) &&
5256 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5257 return cl;
5258 }
5259 }
5260 return NULL_TREE;
5261 }
5262
5263 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5264 offending type if a circularity is detected. NULL_TREE is returned
5265 otherwise. TYPE can be an interface or a class. */
5266
5267 static tree
5268 check_circular_reference (type)
5269 tree type;
5270 {
5271 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5272 int i;
5273
5274 if (!basetype_vec)
5275 return NULL_TREE;
5276
5277 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5278 {
5279 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5280 return lookup_cl (TYPE_NAME (type));
5281 return NULL_TREE;
5282 }
5283
5284 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5285 {
5286 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5287 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5288 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5289 return lookup_cl (TYPE_NAME (BINFO_TYPE (vec_elt)));
5290 }
5291 return NULL_TREE;
5292 }
5293
5294 void
5295 java_check_circular_reference ()
5296 {
5297 tree current;
5298 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5299 {
5300 tree type = TREE_TYPE (current);
5301 tree cl;
5302
5303 cl = check_circular_reference (type);
5304 if (! cl)
5305 cl = check_inner_circular_reference (type, type);
5306 if (cl)
5307 parse_error_context (cl, "Cyclic class inheritance%s",
5308 (cyclic_inheritance_report ?
5309 cyclic_inheritance_report : ""));
5310 }
5311 }
5312
5313 /* Augment the parameter list PARM with parameters crafted to
5314 initialize outer context locals aliases. Through ARTIFICIAL, a
5315 count is kept of the number of crafted parameters. MODE governs
5316 what eventually gets created: something suitable for a function
5317 creation or a function invocation, either the constructor or
5318 finit$. */
5319
5320 static tree
5321 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5322 int mode;
5323 tree class_type, parm;
5324 int *artificial;
5325 {
5326 tree field;
5327 tree additional_parms = NULL_TREE;
5328
5329 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5330 if (FIELD_LOCAL_ALIAS (field))
5331 {
5332 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5333 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5334 tree mangled_id;
5335
5336 switch (mode)
5337 {
5338 case AIPL_FUNCTION_DECLARATION:
5339 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5340 &buffer [4]);
5341 purpose = build_wfl_node (mangled_id);
5342 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5343 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5344 else
5345 value = TREE_TYPE (field);
5346 break;
5347
5348 case AIPL_FUNCTION_CREATION:
5349 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5350 &buffer [4]);
5351 value = TREE_TYPE (field);
5352 break;
5353
5354 case AIPL_FUNCTION_FINIT_INVOCATION:
5355 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5356 &buffer [4]);
5357 /* Now, this is wrong. purpose should always be the NAME
5358 of something and value its matching value (decl, type,
5359 etc...) FIXME -- but there is a lot to fix. */
5360
5361 /* When invoked for this kind of operation, we already
5362 know whether a field is used or not. */
5363 purpose = TREE_TYPE (field);
5364 value = build_wfl_node (mangled_id);
5365 break;
5366
5367 case AIPL_FUNCTION_CTOR_INVOCATION:
5368 /* There are two case: the constructor invokation happends
5369 outside the local inner, in which case, locales from the outer
5370 context are directly used.
5371
5372 Otherwise, we fold to using the alias directly. */
5373 if (class_type == current_class)
5374 value = field;
5375 else
5376 {
5377 name = get_identifier (&buffer[4]);
5378 value = IDENTIFIER_LOCAL_VALUE (name);
5379 }
5380 break;
5381 }
5382 additional_parms = tree_cons (purpose, value, additional_parms);
5383 if (artificial)
5384 *artificial +=1;
5385 }
5386 if (additional_parms)
5387 {
5388 if (ANONYMOUS_CLASS_P (class_type)
5389 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5390 additional_parms = nreverse (additional_parms);
5391 parm = chainon (additional_parms, parm);
5392 }
5393
5394 return parm;
5395 }
5396
5397 /* Craft a constructor for CLASS_DECL -- what we should do when none
5398 where found. ARGS is non NULL when a special signature must be
5399 enforced. This is the case for anonymous classes. */
5400
5401 static void
5402 craft_constructor (class_decl, args)
5403 tree class_decl, args;
5404 {
5405 tree class_type = TREE_TYPE (class_decl);
5406 tree parm = NULL_TREE;
5407 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5408 ACC_PUBLIC : 0);
5409 int i = 0, artificial = 0;
5410 tree decl, ctor_name;
5411 char buffer [80];
5412
5413 /* The constructor name is <init> unless we're dealing with an
5414 anonymous class, in which case the name will be fixed after having
5415 be expanded. */
5416 if (ANONYMOUS_CLASS_P (class_type))
5417 ctor_name = DECL_NAME (class_decl);
5418 else
5419 ctor_name = init_identifier_node;
5420
5421 /* If we're dealing with an inner class constructor, we hide the
5422 this$<n> decl in the name field of its parameter declaration. */
5423 if (PURE_INNER_CLASS_TYPE_P (class_type))
5424 {
5425 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5426 parm = tree_cons (build_current_thisn (class_type),
5427 build_pointer_type (type), parm);
5428
5429 /* Some more arguments to be hidden here. The values of the local
5430 variables of the outer context that the inner class needs to see. */
5431 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5432 class_type, parm,
5433 &artificial);
5434 }
5435
5436 /* Then if there are any args to be enforced, enforce them now */
5437 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5438 {
5439 sprintf (buffer, "parm%d", i++);
5440 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5441 }
5442
5443 CRAFTED_PARAM_LIST_FIXUP (parm);
5444 decl = create_artificial_method (class_type, flags, void_type_node,
5445 ctor_name, parm);
5446 fix_method_argument_names (parm, decl);
5447 /* Now, mark the artificial parameters. */
5448 DECL_FUNCTION_NAP (decl) = artificial;
5449 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5450 }
5451
5452
5453 /* Fix the constructors. This will be called right after circular
5454 references have been checked. It is necessary to fix constructors
5455 early even if no code generation will take place for that class:
5456 some generated constructor might be required by the class whose
5457 compilation triggered this one to be simply loaded. */
5458
5459 void
5460 java_fix_constructors ()
5461 {
5462 tree current;
5463
5464 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5465 {
5466 tree class_type = TREE_TYPE (current);
5467 int saw_ctor = 0;
5468 tree decl;
5469
5470 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5471 continue;
5472
5473 current_class = class_type;
5474 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5475 {
5476 if (DECL_CONSTRUCTOR_P (decl))
5477 {
5478 fix_constructors (decl);
5479 saw_ctor = 1;
5480 }
5481 }
5482
5483 /* Anonymous class constructor can't be generated that early. */
5484 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5485 craft_constructor (current, NULL_TREE);
5486 }
5487 }
5488
5489 /* safe_layout_class just makes sure that we can load a class without
5490 disrupting the current_class, input_file, lineno, etc, information
5491 about the class processed currently. */
5492
5493 void
5494 safe_layout_class (class)
5495 tree class;
5496 {
5497 tree save_current_class = current_class;
5498 const char *save_input_filename = input_filename;
5499 int save_lineno = lineno;
5500
5501 layout_class (class);
5502
5503 current_class = save_current_class;
5504 input_filename = save_input_filename;
5505 lineno = save_lineno;
5506 }
5507
5508 static tree
5509 jdep_resolve_class (dep)
5510 jdep *dep;
5511 {
5512 tree decl;
5513
5514 if (JDEP_RESOLVED_P (dep))
5515 decl = JDEP_RESOLVED_DECL (dep);
5516 else
5517 {
5518 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5519 JDEP_DECL (dep), JDEP_WFL (dep));
5520 JDEP_RESOLVED (dep, decl);
5521 }
5522
5523 if (!decl)
5524 complete_class_report_errors (dep);
5525 else if (PURE_INNER_CLASS_DECL_P (decl))
5526 {
5527 tree inner = TREE_TYPE (decl);
5528 if (! CLASS_LOADED_P (inner))
5529 {
5530 safe_layout_class (inner);
5531 if (TYPE_SIZE (inner) == error_mark_node)
5532 TYPE_SIZE (inner) = NULL_TREE;
5533 }
5534 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5535 }
5536 return decl;
5537 }
5538
5539 /* Complete unsatisfied class declaration and their dependencies */
5540
5541 void
5542 java_complete_class ()
5543 {
5544 tree cclass;
5545 jdeplist *cclassd;
5546 int error_found;
5547 tree type;
5548
5549 /* Process imports */
5550 process_imports ();
5551
5552 /* Rever things so we have the right order */
5553 ctxp->class_list = nreverse (ctxp->class_list);
5554 ctxp->classd_list = reverse_jdep_list (ctxp);
5555
5556 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5557 cclass && cclassd;
5558 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5559 {
5560 jdep *dep;
5561
5562 /* We keep the compilation unit imports in the class so that
5563 they can be used later to resolve type dependencies that
5564 aren't necessary to solve now. */
5565 TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5566 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5567
5568 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5569 {
5570 tree decl;
5571 if (!(decl = jdep_resolve_class (dep)))
5572 continue;
5573
5574 /* Now it's time to patch */
5575 switch (JDEP_KIND (dep))
5576 {
5577 case JDEP_SUPER:
5578 /* Simply patch super */
5579 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5580 continue;
5581 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5582 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5583 break;
5584
5585 case JDEP_FIELD:
5586 {
5587 /* We do part of the job done in add_field */
5588 tree field_decl = JDEP_DECL (dep);
5589 tree field_type = TREE_TYPE (decl);
5590 if (TREE_CODE (field_type) == RECORD_TYPE)
5591 field_type = promote_type (field_type);
5592 TREE_TYPE (field_decl) = field_type;
5593 DECL_ALIGN (field_decl) = 0;
5594 DECL_USER_ALIGN (field_decl) = 0;
5595 layout_decl (field_decl, 0);
5596 SOURCE_FRONTEND_DEBUG
5597 (("Completed field/var decl `%s' with `%s'",
5598 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5599 IDENTIFIER_POINTER (DECL_NAME (decl))));
5600 break;
5601 }
5602 case JDEP_METHOD: /* We start patching a method */
5603 case JDEP_METHOD_RETURN:
5604 error_found = 0;
5605 while (1)
5606 {
5607 if (decl)
5608 {
5609 type = TREE_TYPE(decl);
5610 if (TREE_CODE (type) == RECORD_TYPE)
5611 type = promote_type (type);
5612 JDEP_APPLY_PATCH (dep, type);
5613 SOURCE_FRONTEND_DEBUG
5614 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5615 "Completing fct `%s' with ret type `%s'":
5616 "Completing arg `%s' with type `%s'"),
5617 IDENTIFIER_POINTER (EXPR_WFL_NODE
5618 (JDEP_DECL_WFL (dep))),
5619 IDENTIFIER_POINTER (DECL_NAME (decl))));
5620 }
5621 else
5622 error_found = 1;
5623 dep = JDEP_CHAIN (dep);
5624 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5625 break;
5626 else
5627 decl = jdep_resolve_class (dep);
5628 }
5629 if (!error_found)
5630 {
5631 tree mdecl = JDEP_DECL (dep), signature;
5632 /* Recompute and reset the signature, check first that
5633 all types are now defined. If they're not,
5634 don't build the signature. */
5635 if (check_method_types_complete (mdecl))
5636 {
5637 signature = build_java_signature (TREE_TYPE (mdecl));
5638 set_java_signature (TREE_TYPE (mdecl), signature);
5639 }
5640 }
5641 else
5642 continue;
5643 break;
5644
5645 case JDEP_INTERFACE:
5646 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5647 JDEP_WFL (dep)))
5648 continue;
5649 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5650 break;
5651
5652 case JDEP_PARM:
5653 case JDEP_VARIABLE:
5654 type = TREE_TYPE(decl);
5655 if (TREE_CODE (type) == RECORD_TYPE)
5656 type = promote_type (type);
5657 JDEP_APPLY_PATCH (dep, type);
5658 break;
5659
5660 case JDEP_TYPE:
5661 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5662 SOURCE_FRONTEND_DEBUG
5663 (("Completing a random type dependency on a '%s' node",
5664 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5665 break;
5666
5667 case JDEP_EXCEPTION:
5668 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5669 SOURCE_FRONTEND_DEBUG
5670 (("Completing `%s' `throws' argument node",
5671 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5672 break;
5673
5674 case JDEP_ANONYMOUS:
5675 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5676 break;
5677
5678 default:
5679 abort ();
5680 }
5681 }
5682 }
5683 return;
5684 }
5685
5686 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5687 array. */
5688
5689 static tree
5690 resolve_class (enclosing, class_type, decl, cl)
5691 tree enclosing, class_type, decl, cl;
5692 {
5693 tree tname = TYPE_NAME (class_type);
5694 tree resolved_type = TREE_TYPE (class_type);
5695 int array_dims = 0;
5696 tree resolved_type_decl;
5697
5698 if (resolved_type != NULL_TREE)
5699 {
5700 tree resolved_type_decl = TYPE_NAME (resolved_type);
5701 if (resolved_type_decl == NULL_TREE
5702 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5703 {
5704 resolved_type_decl = build_decl (TYPE_DECL,
5705 TYPE_NAME (class_type),
5706 resolved_type);
5707 }
5708 return resolved_type_decl;
5709 }
5710
5711 /* 1- Check to see if we have an array. If true, find what we really
5712 want to resolve */
5713 if ((array_dims = build_type_name_from_array_name (tname,
5714 &TYPE_NAME (class_type))))
5715 WFL_STRIP_BRACKET (cl, cl);
5716
5717 /* 2- Resolve the bare type */
5718 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5719 decl, cl)))
5720 return NULL_TREE;
5721 resolved_type = TREE_TYPE (resolved_type_decl);
5722
5723 /* 3- If we have and array, reconstruct the array down to its nesting */
5724 if (array_dims)
5725 {
5726 for (; array_dims; array_dims--)
5727 resolved_type = build_java_array_type (resolved_type, -1);
5728 resolved_type_decl = TYPE_NAME (resolved_type);
5729 }
5730 TREE_TYPE (class_type) = resolved_type;
5731 return resolved_type_decl;
5732 }
5733
5734 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5735 are used to report error messages. Do not try to replace TYPE_NAME
5736 (class_type) by a variable, since it is changed by
5737 find_in_imports{_on_demand} and (but it doesn't really matter)
5738 qualify_and_find. */
5739
5740 tree
5741 do_resolve_class (enclosing, class_type, decl, cl)
5742 tree enclosing, class_type, decl, cl;
5743 {
5744 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5745 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5746 struct hash_table _ht, *circularity_hash = &_ht;
5747
5748 /* This hash table is used to register the classes we're going
5749 through when searching the current class as an inner class, in
5750 order to detect circular references. Remember to free it before
5751 returning the section 0- of this function. */
5752 hash_table_init (circularity_hash, hash_newfunc,
5753 java_hash_hash_tree_node, java_hash_compare_tree_node);
5754
5755 /* 0- Search in the current class as an inner class.
5756 Maybe some code here should be added to load the class or
5757 something, at least if the class isn't an inner class and ended
5758 being loaded from class file. FIXME. */
5759 while (enclosing)
5760 {
5761 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5762 &super, class_type);
5763 if (new_class_decl)
5764 break;
5765
5766 /* If we haven't found anything because SUPER reached Object and
5767 ENCLOSING happens to be an innerclass, try the enclosing context. */
5768 if ((!super || super == object_type_node) &&
5769 enclosing && INNER_CLASS_DECL_P (enclosing))
5770 enclosing = DECL_CONTEXT (enclosing);
5771 else
5772 enclosing = NULL_TREE;
5773 }
5774
5775 hash_table_free (circularity_hash);
5776
5777 if (new_class_decl)
5778 return new_class_decl;
5779
5780 /* 1- Check for the type in single imports. This will change
5781 TYPE_NAME() if something relevant is found */
5782 find_in_imports (saved_enclosing_type, class_type);
5783
5784 /* 2- And check for the type in the current compilation unit */
5785 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5786 {
5787 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5788 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5789 load_class (TYPE_NAME (class_type), 0);
5790 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5791 }
5792
5793 /* 3- Search according to the current package definition */
5794 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5795 {
5796 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5797 TYPE_NAME (class_type))))
5798 return new_class_decl;
5799 }
5800
5801 /* 4- Check the import on demands. Don't allow bar.baz to be
5802 imported from foo.* */
5803 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5804 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5805 return NULL_TREE;
5806
5807 /* If found in find_in_imports_on_demant, the type has already been
5808 loaded. */
5809 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5810 return new_class_decl;
5811
5812 /* 5- Try with a name qualified with the package name we've seen so far */
5813 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5814 {
5815 tree package;
5816
5817 /* If there is a current package (ctxp->package), it's the first
5818 element of package_list and we can skip it. */
5819 for (package = (ctxp->package ?
5820 TREE_CHAIN (package_list) : package_list);
5821 package; package = TREE_CHAIN (package))
5822 if ((new_class_decl = qualify_and_find (class_type,
5823 TREE_PURPOSE (package),
5824 TYPE_NAME (class_type))))
5825 return new_class_decl;
5826 }
5827
5828 /* 5- Check an other compilation unit that bears the name of type */
5829 load_class (TYPE_NAME (class_type), 0);
5830
5831 if (!cl)
5832 cl = lookup_cl (decl);
5833
5834 /* If we don't have a value for CL, then we're being called recursively.
5835 We can't check package access just yet, but it will be taken care of
5836 by the caller. */
5837 if (cl)
5838 {
5839 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true))
5840 return NULL_TREE;
5841 }
5842
5843 /* 6- Last call for a resolution */
5844 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5845 }
5846
5847 static tree
5848 qualify_and_find (class_type, package, name)
5849 tree class_type, package, name;
5850 {
5851 tree new_qualified = merge_qualified_name (package, name);
5852 tree new_class_decl;
5853
5854 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5855 load_class (new_qualified, 0);
5856 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5857 {
5858 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5859 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5860 load_class (new_qualified, 0);
5861 TYPE_NAME (class_type) = new_qualified;
5862 return IDENTIFIER_CLASS_VALUE (new_qualified);
5863 }
5864 return NULL_TREE;
5865 }
5866
5867 /* Resolve NAME and lay it out (if not done and if not the current
5868 parsed class). Return a decl node. This function is meant to be
5869 called when type resolution is necessary during the walk pass. */
5870
5871 static tree
5872 resolve_and_layout (something, cl)
5873 tree something;
5874 tree cl;
5875 {
5876 tree decl, decl_type;
5877
5878 /* Don't do that on the current class */
5879 if (something == current_class)
5880 return TYPE_NAME (current_class);
5881
5882 /* Don't do anything for void and other primitive types */
5883 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5884 return NULL_TREE;
5885
5886 /* Pointer types can be reall pointer types or fake pointers. When
5887 finding a real pointer, recheck for primitive types */
5888 if (TREE_CODE (something) == POINTER_TYPE)
5889 {
5890 if (TREE_TYPE (something))
5891 {
5892 something = TREE_TYPE (something);
5893 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5894 return NULL_TREE;
5895 }
5896 else
5897 something = TYPE_NAME (something);
5898 }
5899
5900 /* Don't do anything for arrays of primitive types */
5901 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5902 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5903 return NULL_TREE;
5904
5905 /* Something might be a WFL */
5906 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5907 something = EXPR_WFL_NODE (something);
5908
5909 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5910 TYPE_DECL or a real TYPE */
5911 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5912 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5913 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5914
5915 if (!(decl = resolve_no_layout (something, cl)))
5916 return NULL_TREE;
5917
5918 /* Resolve and layout if necessary */
5919 decl_type = TREE_TYPE (decl);
5920 layout_class_methods (decl_type);
5921 /* Check methods */
5922 if (CLASS_FROM_SOURCE_P (decl_type))
5923 java_check_methods (decl);
5924 /* Layout the type if necessary */
5925 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5926 safe_layout_class (decl_type);
5927
5928 return decl;
5929 }
5930
5931 /* Resolve a class, returns its decl but doesn't perform any
5932 layout. The current parsing context is saved and restored */
5933
5934 static tree
5935 resolve_no_layout (name, cl)
5936 tree name, cl;
5937 {
5938 tree ptr, decl;
5939 BUILD_PTR_FROM_NAME (ptr, name);
5940 java_parser_context_save_global ();
5941 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5942 java_parser_context_restore_global ();
5943
5944 return decl;
5945 }
5946
5947 /* Called when reporting errors. Skip the '[]'s in a complex array
5948 type description that failed to be resolved. purify_type_name can't
5949 use an identifier tree. */
5950
5951 static const char *
5952 purify_type_name (name)
5953 const char *name;
5954 {
5955 int len = strlen (name);
5956 int bracket_found;
5957
5958 STRING_STRIP_BRACKETS (name, len, bracket_found);
5959 if (bracket_found)
5960 {
5961 char *stripped_name = xmemdup (name, len, len+1);
5962 stripped_name [len] = '\0';
5963 return stripped_name;
5964 }
5965 return name;
5966 }
5967
5968 /* The type CURRENT refers to can't be found. We print error messages. */
5969
5970 static void
5971 complete_class_report_errors (dep)
5972 jdep *dep;
5973 {
5974 const char *name;
5975
5976 if (!JDEP_WFL (dep))
5977 return;
5978
5979 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5980 switch (JDEP_KIND (dep))
5981 {
5982 case JDEP_SUPER:
5983 parse_error_context
5984 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5985 purify_type_name (name),
5986 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5987 break;
5988 case JDEP_FIELD:
5989 parse_error_context
5990 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5991 purify_type_name (name),
5992 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5993 break;
5994 case JDEP_METHOD: /* Covers arguments */
5995 parse_error_context
5996 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5997 purify_type_name (name),
5998 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5999 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6000 break;
6001 case JDEP_METHOD_RETURN: /* Covers return type */
6002 parse_error_context
6003 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
6004 purify_type_name (name),
6005 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6006 break;
6007 case JDEP_INTERFACE:
6008 parse_error_context
6009 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
6010 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6011 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6012 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6013 break;
6014 case JDEP_VARIABLE:
6015 parse_error_context
6016 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
6017 purify_type_name (IDENTIFIER_POINTER
6018 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6019 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6020 break;
6021 case JDEP_EXCEPTION: /* As specified by `throws' */
6022 parse_error_context
6023 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
6024 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6025 break;
6026 default:
6027 /* Fix for -Wall. Just break doing nothing. The error will be
6028 caught later */
6029 break;
6030 }
6031 }
6032
6033 /* Return a static string containing the DECL prototype string. If
6034 DECL is a constructor, use the class name instead of the form
6035 <init> */
6036
6037 static const char *
6038 get_printable_method_name (decl)
6039 tree decl;
6040 {
6041 const char *to_return;
6042 tree name = NULL_TREE;
6043
6044 if (DECL_CONSTRUCTOR_P (decl))
6045 {
6046 name = DECL_NAME (decl);
6047 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6048 }
6049
6050 to_return = lang_printable_name (decl, 0);
6051 if (DECL_CONSTRUCTOR_P (decl))
6052 DECL_NAME (decl) = name;
6053
6054 return to_return;
6055 }
6056
6057 /* Track method being redefined inside the same class. As a side
6058 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6059 function it's a FWL, so we can track errors more accurately.) */
6060
6061 static int
6062 check_method_redefinition (class, method)
6063 tree class, method;
6064 {
6065 tree redef, sig;
6066
6067 /* There's no need to verify <clinit> and finit$ and instinit$ */
6068 if (DECL_CLINIT_P (method)
6069 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6070 return 0;
6071
6072 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6073 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6074 {
6075 if (redef == method)
6076 break;
6077 if (DECL_NAME (redef) == DECL_NAME (method)
6078 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6079 && !DECL_ARTIFICIAL (method))
6080 {
6081 parse_error_context
6082 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6083 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6084 get_printable_method_name (redef));
6085 return 1;
6086 }
6087 }
6088 return 0;
6089 }
6090
6091 /* Return 1 if check went ok, 0 otherwise. */
6092 static int
6093 check_abstract_method_definitions (do_interface, class_decl, type)
6094 int do_interface;
6095 tree class_decl, type;
6096 {
6097 tree class = TREE_TYPE (class_decl);
6098 tree method, end_type;
6099 int ok = 1;
6100
6101 end_type = (do_interface ? object_type_node : type);
6102 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6103 {
6104 tree other_super, other_method, method_sig, method_name;
6105 int found = 0;
6106 int end_type_reached = 0;
6107
6108 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6109 continue;
6110
6111 /* Now verify that somewhere in between TYPE and CLASS,
6112 abstract method METHOD gets a non abstract definition
6113 that is inherited by CLASS. */
6114
6115 method_sig = build_java_signature (TREE_TYPE (method));
6116 method_name = DECL_NAME (method);
6117 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6118 method_name = EXPR_WFL_NODE (method_name);
6119
6120 other_super = class;
6121 do {
6122 if (other_super == end_type)
6123 end_type_reached = 1;
6124
6125 /* Method search */
6126 for (other_method = TYPE_METHODS (other_super); other_method;
6127 other_method = TREE_CHAIN (other_method))
6128 {
6129 tree s = build_java_signature (TREE_TYPE (other_method));
6130 tree other_name = DECL_NAME (other_method);
6131
6132 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6133 other_name = EXPR_WFL_NODE (other_name);
6134 if (!DECL_CLINIT_P (other_method)
6135 && !DECL_CONSTRUCTOR_P (other_method)
6136 && method_name == other_name
6137 && method_sig == s
6138 && !METHOD_ABSTRACT (other_method))
6139 {
6140 found = 1;
6141 break;
6142 }
6143 }
6144 other_super = CLASSTYPE_SUPER (other_super);
6145 } while (!end_type_reached);
6146
6147 /* Report that abstract METHOD didn't find an implementation
6148 that CLASS can use. */
6149 if (!found)
6150 {
6151 char *t = xstrdup (lang_printable_name
6152 (TREE_TYPE (TREE_TYPE (method)), 0));
6153 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6154
6155 parse_error_context
6156 (lookup_cl (class_decl),
6157 "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",
6158 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6159 t, lang_printable_name (method, 0),
6160 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6161 "interface" : "class"),
6162 IDENTIFIER_POINTER (ccn),
6163 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6164 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6165 ok = 0;
6166 free (t);
6167 }
6168 }
6169
6170 if (ok && do_interface)
6171 {
6172 /* Check for implemented interfaces. */
6173 int i;
6174 tree vector = TYPE_BINFO_BASETYPES (type);
6175 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6176 {
6177 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6178 ok = check_abstract_method_definitions (1, class_decl, super);
6179 }
6180 }
6181
6182 return ok;
6183 }
6184
6185 /* Check that CLASS_DECL somehow implements all inherited abstract
6186 methods. */
6187
6188 static void
6189 java_check_abstract_method_definitions (class_decl)
6190 tree class_decl;
6191 {
6192 tree class = TREE_TYPE (class_decl);
6193 tree super, vector;
6194 int i;
6195
6196 if (CLASS_ABSTRACT (class_decl))
6197 return;
6198
6199 /* Check for inherited types */
6200 super = class;
6201 do {
6202 super = CLASSTYPE_SUPER (super);
6203 check_abstract_method_definitions (0, class_decl, super);
6204 } while (super != object_type_node);
6205
6206 /* Check for implemented interfaces. */
6207 vector = TYPE_BINFO_BASETYPES (class);
6208 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6209 {
6210 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6211 check_abstract_method_definitions (1, class_decl, super);
6212 }
6213 }
6214
6215 /* Check all the types method DECL uses and return 1 if all of them
6216 are now complete, 0 otherwise. This is used to check whether its
6217 safe to build a method signature or not. */
6218
6219 static int
6220 check_method_types_complete (decl)
6221 tree decl;
6222 {
6223 tree type = TREE_TYPE (decl);
6224 tree args;
6225
6226 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6227 return 0;
6228
6229 args = TYPE_ARG_TYPES (type);
6230 if (TREE_CODE (type) == METHOD_TYPE)
6231 args = TREE_CHAIN (args);
6232 for (; args != end_params_node; args = TREE_CHAIN (args))
6233 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6234 return 0;
6235
6236 return 1;
6237 }
6238
6239 /* Visible interface to check methods contained in CLASS_DECL */
6240
6241 void
6242 java_check_methods (class_decl)
6243 tree class_decl;
6244 {
6245 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6246 return;
6247
6248 if (CLASS_INTERFACE (class_decl))
6249 java_check_abstract_methods (class_decl);
6250 else
6251 java_check_regular_methods (class_decl);
6252
6253 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6254 }
6255
6256 /* Check all the methods of CLASS_DECL. Methods are first completed
6257 then checked according to regular method existence rules. If no
6258 constructor for CLASS_DECL were encountered, then build its
6259 declaration. */
6260
6261 static void
6262 java_check_regular_methods (class_decl)
6263 tree class_decl;
6264 {
6265 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6266 tree method;
6267 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6268 tree found = NULL_TREE;
6269 tree mthrows;
6270
6271 /* It is not necessary to check methods defined in java.lang.Object */
6272 if (class == object_type_node)
6273 return;
6274
6275 if (!TYPE_NVIRTUALS (class))
6276 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6277
6278 /* Should take interfaces into account. FIXME */
6279 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6280 {
6281 tree sig;
6282 tree method_wfl = DECL_FUNCTION_WFL (method);
6283 int aflags;
6284
6285 /* Check for redefinitions */
6286 if (check_method_redefinition (class, method))
6287 continue;
6288
6289 /* If we see one constructor a mark so we don't generate the
6290 default one. Also skip other verifications: constructors
6291 can't be inherited hence hiden or overriden */
6292 if (DECL_CONSTRUCTOR_P (method))
6293 {
6294 saw_constructor = 1;
6295 continue;
6296 }
6297
6298 /* We verify things thrown by the method. They must inherits from
6299 java.lang.Throwable */
6300 for (mthrows = DECL_FUNCTION_THROWS (method);
6301 mthrows; mthrows = TREE_CHAIN (mthrows))
6302 {
6303 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6304 parse_error_context
6305 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6306 IDENTIFIER_POINTER
6307 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6308 }
6309
6310 sig = build_java_argument_signature (TREE_TYPE (method));
6311 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6312
6313 /* Inner class can't declare static methods */
6314 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6315 {
6316 char *t = xstrdup (lang_printable_name (class, 0));
6317 parse_error_context
6318 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6319 lang_printable_name (method, 0), t);
6320 free (t);
6321 }
6322
6323 /* Nothing overrides or it's a private method. */
6324 if (!found)
6325 continue;
6326 if (METHOD_PRIVATE (found))
6327 {
6328 found = NULL_TREE;
6329 continue;
6330 }
6331
6332 /* If `found' is declared in an interface, make sure the
6333 modifier matches. */
6334 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6335 && clinit_identifier_node != DECL_NAME (found)
6336 && !METHOD_PUBLIC (method))
6337 {
6338 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6339 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6340 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6341 lang_printable_name (method, 0),
6342 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6343 }
6344
6345 /* Can't override a method with the same name and different return
6346 types. */
6347 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6348 {
6349 char *t = xstrdup
6350 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6351 parse_error_context
6352 (method_wfl,
6353 "Method `%s' was defined with return type `%s' in class `%s'",
6354 lang_printable_name (found, 0), t,
6355 IDENTIFIER_POINTER
6356 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6357 free (t);
6358 }
6359
6360 aflags = get_access_flags_from_decl (found);
6361
6362 /* Can't override final. Can't override static. */
6363 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6364 {
6365 /* Static *can* override static */
6366 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6367 continue;
6368 parse_error_context
6369 (method_wfl,
6370 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6371 (METHOD_FINAL (found) ? "Final" : "Static"),
6372 lang_printable_name (found, 0),
6373 (METHOD_FINAL (found) ? "final" : "static"),
6374 IDENTIFIER_POINTER
6375 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6376 continue;
6377 }
6378
6379 /* Static method can't override instance method. */
6380 if (METHOD_STATIC (method))
6381 {
6382 parse_error_context
6383 (method_wfl,
6384 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6385 lang_printable_name (found, 0),
6386 IDENTIFIER_POINTER
6387 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6388 continue;
6389 }
6390
6391 /* - Overriding/hiding public must be public
6392 - Overriding/hiding protected must be protected or public
6393 - If the overriden or hidden method has default (package)
6394 access, then the overriding or hiding method must not be
6395 private; otherwise, a compile-time error occurs. If
6396 `found' belongs to an interface, things have been already
6397 taken care of. */
6398 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6399 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6400 || (METHOD_PROTECTED (found)
6401 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6402 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6403 && METHOD_PRIVATE (method))))
6404 {
6405 parse_error_context
6406 (method_wfl,
6407 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6408 (METHOD_PUBLIC (method) ? "public" :
6409 (METHOD_PRIVATE (method) ? "private" : "protected")),
6410 IDENTIFIER_POINTER (DECL_NAME
6411 (TYPE_NAME (DECL_CONTEXT (found)))));
6412 continue;
6413 }
6414
6415 /* Overriding methods must have compatible `throws' clauses on checked
6416 exceptions, if any */
6417 check_throws_clauses (method, method_wfl, found);
6418
6419 /* Inheriting multiple methods with the same signature. FIXME */
6420 }
6421
6422 if (!TYPE_NVIRTUALS (class))
6423 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6424
6425 /* Search for inherited abstract method not yet implemented in this
6426 class. */
6427 java_check_abstract_method_definitions (class_decl);
6428
6429 if (!saw_constructor)
6430 abort ();
6431 }
6432
6433 /* Return a non zero value if the `throws' clause of METHOD (if any)
6434 is incompatible with the `throws' clause of FOUND (if any). */
6435
6436 static void
6437 check_throws_clauses (method, method_wfl, found)
6438 tree method, method_wfl, found;
6439 {
6440 tree mthrows, fthrows;
6441
6442 /* Can't check these things with class loaded from bytecode. FIXME */
6443 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6444 return;
6445
6446 for (mthrows = DECL_FUNCTION_THROWS (method);
6447 mthrows; mthrows = TREE_CHAIN (mthrows))
6448 {
6449 /* We don't verify unchecked expressions */
6450 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6451 continue;
6452 /* Checked expression must be compatible */
6453 for (fthrows = DECL_FUNCTION_THROWS (found);
6454 fthrows; fthrows = TREE_CHAIN (fthrows))
6455 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6456 break;
6457 if (!fthrows)
6458 {
6459 parse_error_context
6460 (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'",
6461 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6462 lang_printable_name (found, 0),
6463 IDENTIFIER_POINTER
6464 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6465 }
6466 }
6467 }
6468
6469 /* Check abstract method of interface INTERFACE */
6470
6471 static void
6472 java_check_abstract_methods (interface_decl)
6473 tree interface_decl;
6474 {
6475 int i, n;
6476 tree method, basetype_vec, found;
6477 tree interface = TREE_TYPE (interface_decl);
6478
6479 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6480 {
6481 /* 2- Check for double definition inside the defining interface */
6482 if (check_method_redefinition (interface, method))
6483 continue;
6484
6485 /* 3- Overriding is OK as far as we preserve the return type and
6486 the thrown exceptions (FIXME) */
6487 found = lookup_java_interface_method2 (interface, method);
6488 if (found)
6489 {
6490 char *t;
6491 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6492 parse_error_context
6493 (DECL_FUNCTION_WFL (found),
6494 "Method `%s' was defined with return type `%s' in class `%s'",
6495 lang_printable_name (found, 0), t,
6496 IDENTIFIER_POINTER
6497 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6498 free (t);
6499 continue;
6500 }
6501 }
6502
6503 /* 4- Inherited methods can't differ by their returned types */
6504 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6505 return;
6506 n = TREE_VEC_LENGTH (basetype_vec);
6507 for (i = 0; i < n; i++)
6508 {
6509 tree sub_interface_method, sub_interface;
6510 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6511 if (!vec_elt)
6512 continue;
6513 sub_interface = BINFO_TYPE (vec_elt);
6514 for (sub_interface_method = TYPE_METHODS (sub_interface);
6515 sub_interface_method;
6516 sub_interface_method = TREE_CHAIN (sub_interface_method))
6517 {
6518 found = lookup_java_interface_method2 (interface,
6519 sub_interface_method);
6520 if (found && (found != sub_interface_method))
6521 {
6522 parse_error_context
6523 (lookup_cl (sub_interface_method),
6524 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6525 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6526 lang_printable_name (found, 0),
6527 IDENTIFIER_POINTER
6528 (DECL_NAME (TYPE_NAME
6529 (DECL_CONTEXT (sub_interface_method)))),
6530 IDENTIFIER_POINTER
6531 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6532 }
6533 }
6534 }
6535 }
6536
6537 /* Lookup methods in interfaces using their name and partial
6538 signature. Return a matching method only if their types differ. */
6539
6540 static tree
6541 lookup_java_interface_method2 (class, method_decl)
6542 tree class, method_decl;
6543 {
6544 int i, n;
6545 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6546
6547 if (!basetype_vec)
6548 return NULL_TREE;
6549
6550 n = TREE_VEC_LENGTH (basetype_vec);
6551 for (i = 0; i < n; i++)
6552 {
6553 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6554 if ((BINFO_TYPE (vec_elt) != object_type_node)
6555 && (to_return =
6556 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6557 return to_return;
6558 }
6559 for (i = 0; i < n; i++)
6560 {
6561 to_return = lookup_java_interface_method2
6562 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6563 if (to_return)
6564 return to_return;
6565 }
6566
6567 return NULL_TREE;
6568 }
6569
6570 /* Lookup method using their name and partial signature. Return a
6571 matching method only if their types differ. */
6572
6573 static tree
6574 lookup_java_method2 (clas, method_decl, do_interface)
6575 tree clas, method_decl;
6576 int do_interface;
6577 {
6578 tree method, method_signature, method_name, method_type, name;
6579
6580 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6581 name = DECL_NAME (method_decl);
6582 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6583 EXPR_WFL_NODE (name) : name);
6584 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6585
6586 while (clas != NULL_TREE)
6587 {
6588 for (method = TYPE_METHODS (clas);
6589 method != NULL_TREE; method = TREE_CHAIN (method))
6590 {
6591 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6592 tree name = DECL_NAME (method);
6593 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6594 EXPR_WFL_NODE (name) : name) == method_name
6595 && method_sig == method_signature
6596 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6597 return method;
6598 }
6599 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6600 }
6601 return NULL_TREE;
6602 }
6603
6604 /* Return the line that matches DECL line number, and try its best to
6605 position the column number. Used during error reports. */
6606
6607 static tree
6608 lookup_cl (decl)
6609 tree decl;
6610 {
6611 static tree cl = NULL_TREE;
6612 char *line, *found;
6613
6614 if (!decl)
6615 return NULL_TREE;
6616
6617 if (cl == NULL_TREE)
6618 {
6619 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6620 ggc_add_tree_root (&cl, 1);
6621 }
6622
6623 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6624 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6625
6626 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6627 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6628
6629 found = strstr ((const char *)line,
6630 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6631 if (found)
6632 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6633
6634 return cl;
6635 }
6636
6637 /* Look for a simple name in the single-type import list */
6638
6639 static tree
6640 find_name_in_single_imports (name)
6641 tree name;
6642 {
6643 tree node;
6644
6645 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6646 if (TREE_VALUE (node) == name)
6647 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6648
6649 return NULL_TREE;
6650 }
6651
6652 /* Process all single-type import. */
6653
6654 static int
6655 process_imports ()
6656 {
6657 tree import;
6658 int error_found;
6659
6660 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6661 {
6662 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6663 char *original_name;
6664
6665 obstack_grow0 (&temporary_obstack,
6666 IDENTIFIER_POINTER (to_be_found),
6667 IDENTIFIER_LENGTH (to_be_found));
6668 original_name = obstack_finish (&temporary_obstack);
6669
6670 /* Don't load twice something already defined. */
6671 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6672 continue;
6673
6674 while (1)
6675 {
6676 tree left;
6677
6678 QUALIFIED_P (to_be_found) = 1;
6679 load_class (to_be_found, 0);
6680 error_found =
6681 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true);
6682
6683 /* We found it, we can bail out */
6684 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6685 break;
6686
6687 /* We haven't found it. Maybe we're trying to access an
6688 inner class. The only way for us to know is to try again
6689 after having dropped a qualifier. If we can't break it further,
6690 we have an error. */
6691 if (breakdown_qualified (&left, NULL, to_be_found))
6692 break;
6693
6694 to_be_found = left;
6695 }
6696 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6697 {
6698 parse_error_context (TREE_PURPOSE (import),
6699 "Class or interface `%s' not found in import",
6700 original_name);
6701 error_found = 1;
6702 }
6703
6704 obstack_free (&temporary_obstack, original_name);
6705 if (error_found)
6706 return 1;
6707 }
6708 return 0;
6709 }
6710
6711 /* Possibly find and mark a class imported by a single-type import
6712 statement. */
6713
6714 static void
6715 find_in_imports (enclosing_type, class_type)
6716 tree enclosing_type;
6717 tree class_type;
6718 {
6719 tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
6720 ctxp->import_list);
6721 while (import)
6722 {
6723 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6724 {
6725 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6726 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6727 return;
6728 }
6729 import = TREE_CHAIN (import);
6730 }
6731 }
6732
6733 static int
6734 note_possible_classname (name, len)
6735 const char *name;
6736 int len;
6737 {
6738 tree node;
6739 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6740 len = len - 5;
6741 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6742 len = len - 6;
6743 else
6744 return 0;
6745 node = ident_subst (name, len, "", '/', '.', "");
6746 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6747 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6748 return 1;
6749 }
6750
6751 /* Read a import directory, gathering potential match for further type
6752 references. Indifferently reads a filesystem or a ZIP archive
6753 directory. */
6754
6755 static void
6756 read_import_dir (wfl)
6757 tree wfl;
6758 {
6759 tree package_id = EXPR_WFL_NODE (wfl);
6760 const char *package_name = IDENTIFIER_POINTER (package_id);
6761 int package_length = IDENTIFIER_LENGTH (package_id);
6762 DIR *dirp = NULL;
6763 JCF *saved_jcf = current_jcf;
6764
6765 int found = 0;
6766 int k;
6767 void *entry;
6768 struct buffer filename[1];
6769
6770 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6771 return;
6772 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6773
6774 BUFFER_INIT (filename);
6775 buffer_grow (filename, package_length + 100);
6776
6777 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6778 {
6779 const char *entry_name = jcf_path_name (entry);
6780 int entry_length = strlen (entry_name);
6781 if (jcf_path_is_zipfile (entry))
6782 {
6783 ZipFile *zipf;
6784 buffer_grow (filename, entry_length);
6785 memcpy (filename->data, entry_name, entry_length - 1);
6786 filename->data[entry_length-1] = '\0';
6787 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6788 if (zipf == NULL)
6789 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6790 else
6791 {
6792 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6793 BUFFER_RESET (filename);
6794 for (k = 0; k < package_length; k++)
6795 {
6796 char ch = package_name[k];
6797 *filename->ptr++ = ch == '.' ? '/' : ch;
6798 }
6799 *filename->ptr++ = '/';
6800
6801 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6802 {
6803 const char *current_entry = ZIPDIR_FILENAME (zipd);
6804 int current_entry_len = zipd->filename_length;
6805
6806 if (current_entry_len >= BUFFER_LENGTH (filename)
6807 && strncmp (filename->data, current_entry,
6808 BUFFER_LENGTH (filename)) != 0)
6809 continue;
6810 found |= note_possible_classname (current_entry,
6811 current_entry_len);
6812 }
6813 }
6814 }
6815 else
6816 {
6817 BUFFER_RESET (filename);
6818 buffer_grow (filename, entry_length + package_length + 4);
6819 strcpy (filename->data, entry_name);
6820 filename->ptr = filename->data + entry_length;
6821 for (k = 0; k < package_length; k++)
6822 {
6823 char ch = package_name[k];
6824 *filename->ptr++ = ch == '.' ? '/' : ch;
6825 }
6826 *filename->ptr = '\0';
6827
6828 dirp = opendir (filename->data);
6829 if (dirp == NULL)
6830 continue;
6831 *filename->ptr++ = '/';
6832 for (;;)
6833 {
6834 int len;
6835 const char *d_name;
6836 struct dirent *direntp = readdir (dirp);
6837 if (!direntp)
6838 break;
6839 d_name = direntp->d_name;
6840 len = strlen (direntp->d_name);
6841 buffer_grow (filename, len+1);
6842 strcpy (filename->ptr, d_name);
6843 found |= note_possible_classname (filename->data + entry_length,
6844 package_length+len+1);
6845 }
6846 if (dirp)
6847 closedir (dirp);
6848 }
6849 }
6850
6851 free (filename->data);
6852
6853 /* Here we should have a unified way of retrieving an entry, to be
6854 indexed. */
6855 if (!found)
6856 {
6857 static int first = 1;
6858 if (first)
6859 {
6860 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives", package_name);
6861 java_error_count++;
6862 first = 0;
6863 }
6864 else
6865 parse_error_context (wfl, "Package `%s' not found in import",
6866 package_name);
6867 current_jcf = saved_jcf;
6868 return;
6869 }
6870 current_jcf = saved_jcf;
6871 }
6872
6873 /* Possibly find a type in the import on demands specified
6874 types. Returns 1 if an error occurred, 0 otherwise. Run through the
6875 entire list, to detected potential double definitions. */
6876
6877 static int
6878 find_in_imports_on_demand (enclosing_type, class_type)
6879 tree enclosing_type;
6880 tree class_type;
6881 {
6882 tree class_type_name = TYPE_NAME (class_type);
6883 tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
6884 ctxp->import_demand_list);
6885 tree cl = NULL_TREE;
6886 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
6887 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
6888 tree node;
6889
6890 for (; import; import = TREE_CHAIN (import))
6891 {
6892 int saved_lineno = lineno;
6893 int access_check;
6894 const char *id_name;
6895 tree decl, type_name_copy;
6896
6897 obstack_grow (&temporary_obstack,
6898 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6899 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6900 obstack_1grow (&temporary_obstack, '.');
6901 obstack_grow0 (&temporary_obstack,
6902 IDENTIFIER_POINTER (class_type_name),
6903 IDENTIFIER_LENGTH (class_type_name));
6904 id_name = obstack_finish (&temporary_obstack);
6905
6906 if (! (node = maybe_get_identifier (id_name)))
6907 continue;
6908
6909 /* Setup lineno so that it refers to the line of the import (in
6910 case we parse a class file and encounter errors */
6911 lineno = EXPR_WFL_LINENO (TREE_PURPOSE (import));
6912
6913 type_name_copy = TYPE_NAME (class_type);
6914 TYPE_NAME (class_type) = node;
6915 QUALIFIED_P (node) = 1;
6916 decl = IDENTIFIER_CLASS_VALUE (node);
6917 access_check = -1;
6918 /* If there is no DECL set for the class or if the class isn't
6919 loaded and not seen in source yet, then load */
6920 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6921 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6922 {
6923 load_class (node, 0);
6924 decl = IDENTIFIER_CLASS_VALUE (node);
6925 }
6926 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
6927 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
6928 false);
6929 else
6930 /* 6.6.1: Inner classes are subject to member access rules. */
6931 access_check = 0;
6932
6933 lineno = saved_lineno;
6934
6935 /* If the loaded class is not accessible or couldn't be loaded,
6936 we restore the original TYPE_NAME and process the next
6937 import. */
6938 if (access_check || !decl)
6939 {
6940 TYPE_NAME (class_type) = type_name_copy;
6941 continue;
6942 }
6943
6944 /* If the loaded class is accessible, we keep a tab on it to
6945 detect and report multiple inclusions. */
6946 if (IS_A_CLASSFILE_NAME (node))
6947 {
6948 if (seen_once < 0)
6949 {
6950 cl = TREE_PURPOSE (import);
6951 seen_once = 1;
6952 }
6953 else if (seen_once >= 0)
6954 {
6955 tree location = (cl ? cl : TREE_PURPOSE (import));
6956 tree package = (cl ? EXPR_WFL_NODE (cl) :
6957 EXPR_WFL_NODE (TREE_PURPOSE (import)));
6958 seen_once++;
6959 parse_error_context
6960 (location,
6961 "Type `%s' also potentially defined in package `%s'",
6962 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6963 IDENTIFIER_POINTER (package));
6964 }
6965 }
6966 to_return = access_check;
6967 }
6968
6969 if (seen_once == 1)
6970 return to_return;
6971 else
6972 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6973 }
6974
6975 /* Add package NAME to the list of package encountered so far. To
6976 speed up class lookup in do_resolve_class, we make sure a
6977 particular package is added only once. */
6978
6979 static void
6980 register_package (name)
6981 tree name;
6982 {
6983 static struct hash_table _pht, *pht = NULL;
6984
6985 if (!pht)
6986 {
6987 hash_table_init (&_pht, hash_newfunc,
6988 java_hash_hash_tree_node, java_hash_compare_tree_node);
6989 pht = &_pht;
6990 }
6991
6992 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6993 {
6994 package_list = chainon (package_list, build_tree_list (name, NULL));
6995 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6996 }
6997 }
6998
6999 static tree
7000 resolve_package (pkg, next)
7001 tree pkg, *next;
7002 {
7003 tree current, acc;
7004 tree type_name = NULL_TREE;
7005 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
7006
7007 /* The trick is to determine when the package name stops and were
7008 the name of something contained in the package starts. Then we
7009 return a fully qualified name of what we want to get. */
7010
7011 /* Do a quick search on well known package names */
7012 if (!strncmp (name, "java.lang.reflect", 17))
7013 {
7014 *next =
7015 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
7016 type_name = lookup_package_type (name, 17);
7017 }
7018 else if (!strncmp (name, "java.lang", 9))
7019 {
7020 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
7021 type_name = lookup_package_type (name, 9);
7022 }
7023
7024 /* If we found something here, return */
7025 if (type_name)
7026 return type_name;
7027
7028 *next = EXPR_WFL_QUALIFICATION (pkg);
7029
7030 /* Try to progressively construct a type name */
7031 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7032 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
7033 current; current = TREE_CHAIN (current))
7034 {
7035 /* If we don't have what we're expecting, exit now. TYPE_NAME
7036 will be null and the error caught later. */
7037 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7038 break;
7039 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
7040 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
7041 {
7042 type_name = acc;
7043 /* resolve_package should be used in a loop, hence we
7044 point at this one to naturally process the next one at
7045 the next iteration. */
7046 *next = current;
7047 break;
7048 }
7049 }
7050 return type_name;
7051 }
7052
7053 static tree
7054 lookup_package_type (name, from)
7055 const char *name;
7056 int from;
7057 {
7058 char subname [128];
7059 const char *sub = &name[from+1];
7060 while (*sub != '.' && *sub)
7061 sub++;
7062 strncpy (subname, name, sub-name);
7063 subname [sub-name] = '\0';
7064 return get_identifier (subname);
7065 }
7066
7067 /* Check accessibility of inner classes according to member access rules.
7068 DECL is the inner class, ENCLOSING_DECL is the class from which the
7069 access is being attempted. */
7070
7071 static void
7072 check_inner_class_access (decl, enclosing_decl, cl)
7073 tree decl, enclosing_decl, cl;
7074 {
7075 const char *access;
7076 tree enclosing_decl_type;
7077
7078 /* We don't issue an error message when CL is null. CL can be null
7079 as a result of processing a JDEP crafted by source_start_java_method
7080 for the purpose of patching its parm decl. But the error would
7081 have been already trapped when fixing the method's signature.
7082 DECL can also be NULL in case of earlier errors. */
7083 if (!decl || !cl)
7084 return;
7085
7086 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7087
7088 if (CLASS_PRIVATE (decl))
7089 {
7090 /* Access is permitted only within the body of the top-level
7091 class in which DECL is declared. */
7092 tree top_level = decl;
7093 while (DECL_CONTEXT (top_level))
7094 top_level = DECL_CONTEXT (top_level);
7095 while (DECL_CONTEXT (enclosing_decl))
7096 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7097 if (top_level == enclosing_decl)
7098 return;
7099 access = "private";
7100 }
7101 else if (CLASS_PROTECTED (decl))
7102 {
7103 tree decl_context;
7104 /* Access is permitted from within the same package... */
7105 if (in_same_package (decl, enclosing_decl))
7106 return;
7107
7108 /* ... or from within the body of a subtype of the context in which
7109 DECL is declared. */
7110 decl_context = DECL_CONTEXT (decl);
7111 while (enclosing_decl)
7112 {
7113 if (CLASS_INTERFACE (decl))
7114 {
7115 if (interface_of_p (TREE_TYPE (decl_context),
7116 enclosing_decl_type))
7117 return;
7118 }
7119 else
7120 {
7121 /* Eww. The order of the arguments is different!! */
7122 if (inherits_from_p (enclosing_decl_type,
7123 TREE_TYPE (decl_context)))
7124 return;
7125 }
7126 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7127 }
7128 access = "protected";
7129 }
7130 else if (! CLASS_PUBLIC (decl))
7131 {
7132 /* Access is permitted only from within the same package as DECL. */
7133 if (in_same_package (decl, enclosing_decl))
7134 return;
7135 access = "non-public";
7136 }
7137 else
7138 /* Class is public. */
7139 return;
7140
7141 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7142 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7143 lang_printable_name (decl, 0), access);
7144 }
7145
7146 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7147 foreign package, it must be PUBLIC. Return 0 if no access
7148 violations were found, 1 otherwise. If VERBOSE is true and an error
7149 was found, it is reported and accounted for. */
7150
7151 static int
7152 check_pkg_class_access (class_name, cl, verbose)
7153 tree class_name;
7154 tree cl;
7155 bool verbose;
7156 {
7157 tree type;
7158
7159 if (!IDENTIFIER_CLASS_VALUE (class_name))
7160 return 0;
7161
7162 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7163 return 0;
7164
7165 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7166 {
7167 /* Access to a private class within the same package is
7168 allowed. */
7169 tree l, r;
7170 breakdown_qualified (&l, &r, class_name);
7171 if (!QUALIFIED_P (class_name) && !ctxp->package)
7172 /* Both in the empty package. */
7173 return 0;
7174 if (l == ctxp->package)
7175 /* Both in the same package. */
7176 return 0;
7177
7178 if (verbose)
7179 parse_error_context
7180 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7181 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7182 IDENTIFIER_POINTER (class_name));
7183 return 1;
7184 }
7185 return 0;
7186 }
7187
7188 /* Local variable declaration. */
7189
7190 static void
7191 declare_local_variables (modifier, type, vlist)
7192 int modifier;
7193 tree type;
7194 tree vlist;
7195 {
7196 tree decl, current, saved_type;
7197 tree type_wfl = NULL_TREE;
7198 int must_chain = 0;
7199 int final_p = 0;
7200
7201 /* Push a new block if statements were seen between the last time we
7202 pushed a block and now. Keep a count of blocks to close */
7203 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7204 {
7205 tree b = enter_block ();
7206 BLOCK_IS_IMPLICIT (b) = 1;
7207 }
7208
7209 if (modifier)
7210 {
7211 int i;
7212 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
7213 if (modifier == ACC_FINAL)
7214 final_p = 1;
7215 else
7216 {
7217 parse_error_context
7218 (ctxp->modifier_ctx [i],
7219 "Only `final' is allowed as a local variables modifier");
7220 return;
7221 }
7222 }
7223
7224 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7225 hold the TYPE value if a new incomplete has to be created (as
7226 opposed to being found already existing and reused). */
7227 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7228
7229 /* If TYPE is fully resolved and we don't have a reference, make one */
7230 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7231
7232 /* Go through all the declared variables */
7233 for (current = vlist, saved_type = type; current;
7234 current = TREE_CHAIN (current), type = saved_type)
7235 {
7236 tree other, real_type;
7237 tree wfl = TREE_PURPOSE (current);
7238 tree name = EXPR_WFL_NODE (wfl);
7239 tree init = TREE_VALUE (current);
7240
7241 /* Process NAME, as it may specify extra dimension(s) for it */
7242 type = build_array_from_name (type, type_wfl, name, &name);
7243
7244 /* Variable redefinition check */
7245 if ((other = lookup_name_in_blocks (name)))
7246 {
7247 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7248 DECL_SOURCE_LINE (other));
7249 continue;
7250 }
7251
7252 /* Type adjustment. We may have just readjusted TYPE because
7253 the variable specified more dimensions. Make sure we have
7254 a reference if we can and don't have one already. */
7255 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7256
7257 real_type = GET_REAL_TYPE (type);
7258 /* Never layout this decl. This will be done when its scope
7259 will be entered */
7260 decl = build_decl (VAR_DECL, name, real_type);
7261 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7262 DECL_FINAL (decl) = final_p;
7263 BLOCK_CHAIN_DECL (decl);
7264
7265 /* If doing xreferencing, replace the line number with the WFL
7266 compound value */
7267 if (flag_emit_xref)
7268 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7269
7270 /* Don't try to use an INIT statement when an error was found */
7271 if (init && java_error_count)
7272 init = NULL_TREE;
7273
7274 /* Add the initialization function to the current function's code */
7275 if (init)
7276 {
7277 /* Name might have been readjusted */
7278 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7279 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7280 java_method_add_stmt (current_function_decl,
7281 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7282 init));
7283 }
7284
7285 /* Setup dependency the type of the decl */
7286 if (must_chain)
7287 {
7288 jdep *dep;
7289 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7290 dep = CLASSD_LAST (ctxp->classd_list);
7291 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7292 }
7293 }
7294 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7295 }
7296
7297 /* Called during parsing. Build decls from argument list. */
7298
7299 static void
7300 source_start_java_method (fndecl)
7301 tree fndecl;
7302 {
7303 tree tem;
7304 tree parm_decl;
7305 int i;
7306
7307 if (!fndecl)
7308 return;
7309
7310 current_function_decl = fndecl;
7311
7312 /* New scope for the function */
7313 enter_block ();
7314 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7315 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7316 {
7317 tree type = TREE_VALUE (tem);
7318 tree name = TREE_PURPOSE (tem);
7319
7320 /* If type is incomplete. Create an incomplete decl and ask for
7321 the decl to be patched later */
7322 if (INCOMPLETE_TYPE_P (type))
7323 {
7324 jdep *jdep;
7325 tree real_type = GET_REAL_TYPE (type);
7326 parm_decl = build_decl (PARM_DECL, name, real_type);
7327 type = obtain_incomplete_type (type);
7328 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7329 jdep = CLASSD_LAST (ctxp->classd_list);
7330 JDEP_MISC (jdep) = name;
7331 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7332 }
7333 else
7334 parm_decl = build_decl (PARM_DECL, name, type);
7335
7336 /* Remember if a local variable was declared final (via its
7337 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7338 if (ARG_FINAL_P (tem))
7339 {
7340 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7341 DECL_FINAL (parm_decl) = 1;
7342 }
7343
7344 BLOCK_CHAIN_DECL (parm_decl);
7345 }
7346 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7347 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7348 nreverse (tem);
7349 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7350 DECL_MAX_LOCALS (current_function_decl) = i;
7351 }
7352
7353 /* Called during parsing. Creates an artificial method declaration. */
7354
7355 static tree
7356 create_artificial_method (class, flags, type, name, args)
7357 tree class;
7358 int flags;
7359 tree type, name, args;
7360 {
7361 tree mdecl;
7362
7363 java_parser_context_save_global ();
7364 lineno = 0;
7365 mdecl = make_node (FUNCTION_TYPE);
7366 TREE_TYPE (mdecl) = type;
7367 TYPE_ARG_TYPES (mdecl) = args;
7368 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7369 java_parser_context_restore_global ();
7370 DECL_ARTIFICIAL (mdecl) = 1;
7371 return mdecl;
7372 }
7373
7374 /* Starts the body if an artificial method. */
7375
7376 static void
7377 start_artificial_method_body (mdecl)
7378 tree mdecl;
7379 {
7380 DECL_SOURCE_LINE (mdecl) = 1;
7381 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7382 source_start_java_method (mdecl);
7383 enter_block ();
7384 }
7385
7386 static void
7387 end_artificial_method_body (mdecl)
7388 tree mdecl;
7389 {
7390 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7391 It has to be evaluated first. (if mdecl is current_function_decl,
7392 we have an undefined behavior if no temporary variable is used.) */
7393 tree b = exit_block ();
7394 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7395 exit_block ();
7396 }
7397
7398 /* Dump a tree of some kind. This is a convenience wrapper for the
7399 dump_* functions in tree-dump.c. */
7400 static void
7401 dump_java_tree (phase, t)
7402 enum tree_dump_index phase;
7403 tree t;
7404 {
7405 FILE *stream;
7406 int flags;
7407
7408 stream = dump_begin (phase, &flags);
7409 if (stream)
7410 {
7411 dump_node (t, flags, stream);
7412 dump_end (phase, stream);
7413 }
7414 }
7415
7416 /* Terminate a function and expand its body. */
7417
7418 static void
7419 source_end_java_method ()
7420 {
7421 tree fndecl = current_function_decl;
7422
7423 if (!fndecl)
7424 return;
7425
7426 java_parser_context_save_global ();
7427 lineno = ctxp->last_ccb_indent1;
7428
7429 /* Turn function bodies with only a NOP expr null, so they don't get
7430 generated at all and we won't get warnings when using the -W
7431 -Wall flags. */
7432 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7433 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7434
7435 /* We've generated all the trees for this function, and it has been
7436 patched. Dump it to a file if the user requested it. */
7437 dump_java_tree (TDI_original, fndecl);
7438
7439 /* Generate function's code */
7440 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7441 && ! flag_emit_class_files
7442 && ! flag_emit_xref)
7443 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7444
7445 /* pop out of its parameters */
7446 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7447 poplevel (1, 0, 1);
7448 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7449
7450 /* Generate rtl for function exit. */
7451 if (! flag_emit_class_files && ! flag_emit_xref)
7452 {
7453 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7454 expand_function_end (input_filename, lineno, 0);
7455
7456 /* Run the optimizers and output assembler code for this function. */
7457 rest_of_compilation (fndecl);
7458 }
7459
7460 current_function_decl = NULL_TREE;
7461 java_parser_context_restore_global ();
7462 }
7463
7464 /* Record EXPR in the current function block. Complements compound
7465 expression second operand if necessary. */
7466
7467 tree
7468 java_method_add_stmt (fndecl, expr)
7469 tree fndecl, expr;
7470 {
7471 if (!GET_CURRENT_BLOCK (fndecl))
7472 return NULL_TREE;
7473 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7474 }
7475
7476 static tree
7477 add_stmt_to_block (b, type, stmt)
7478 tree b, type, stmt;
7479 {
7480 tree body = BLOCK_EXPR_BODY (b), c;
7481
7482 if (java_error_count)
7483 return body;
7484
7485 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7486 return body;
7487
7488 BLOCK_EXPR_BODY (b) = c;
7489 TREE_SIDE_EFFECTS (c) = 1;
7490 return c;
7491 }
7492
7493 /* Add STMT to EXISTING if possible, otherwise create a new
7494 COMPOUND_EXPR and add STMT to it. */
7495
7496 static tree
7497 add_stmt_to_compound (existing, type, stmt)
7498 tree existing, type, stmt;
7499 {
7500 if (existing)
7501 return build (COMPOUND_EXPR, type, existing, stmt);
7502 else
7503 return stmt;
7504 }
7505
7506 void java_layout_seen_class_methods ()
7507 {
7508 tree previous_list = all_class_list;
7509 tree end = NULL_TREE;
7510 tree current;
7511
7512 while (1)
7513 {
7514 for (current = previous_list;
7515 current != end; current = TREE_CHAIN (current))
7516 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7517
7518 if (previous_list != all_class_list)
7519 {
7520 end = previous_list;
7521 previous_list = all_class_list;
7522 }
7523 else
7524 break;
7525 }
7526 }
7527
7528 void
7529 java_reorder_fields ()
7530 {
7531 static tree stop_reordering = NULL_TREE;
7532 static int initialized_p;
7533 tree current;
7534
7535 /* Register STOP_REORDERING with the garbage collector. */
7536 if (!initialized_p)
7537 {
7538 ggc_add_tree_root (&stop_reordering, 1);
7539 initialized_p = 1;
7540 }
7541
7542 for (current = gclass_list; current; current = TREE_CHAIN (current))
7543 {
7544 current_class = TREE_TYPE (TREE_VALUE (current));
7545
7546 if (current_class == stop_reordering)
7547 break;
7548
7549 /* Reverse the fields, but leave the dummy field in front.
7550 Fields are already ordered for Object and Class */
7551 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7552 && current_class != class_type_node)
7553 {
7554 /* If the dummy field is there, reverse the right fields and
7555 just layout the type for proper fields offset */
7556 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7557 {
7558 tree fields = TYPE_FIELDS (current_class);
7559 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7560 TYPE_SIZE (current_class) = NULL_TREE;
7561 }
7562 /* We don't have a dummy field, we need to layout the class,
7563 after having reversed the fields */
7564 else
7565 {
7566 TYPE_FIELDS (current_class) =
7567 nreverse (TYPE_FIELDS (current_class));
7568 TYPE_SIZE (current_class) = NULL_TREE;
7569 }
7570 }
7571 }
7572 /* There are cases were gclass_list will be empty. */
7573 if (gclass_list)
7574 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7575 }
7576
7577 /* Layout the methods of all classes loaded in one way or another.
7578 Check methods of source parsed classes. Then reorder the
7579 fields and layout the classes or the type of all source parsed
7580 classes */
7581
7582 void
7583 java_layout_classes ()
7584 {
7585 tree current;
7586 int save_error_count = java_error_count;
7587
7588 /* Layout the methods of all classes seen so far */
7589 java_layout_seen_class_methods ();
7590 java_parse_abort_on_error ();
7591 all_class_list = NULL_TREE;
7592
7593 /* Then check the methods of all parsed classes */
7594 for (current = gclass_list; current; current = TREE_CHAIN (current))
7595 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7596 java_check_methods (TREE_VALUE (current));
7597 java_parse_abort_on_error ();
7598
7599 for (current = gclass_list; current; current = TREE_CHAIN (current))
7600 {
7601 current_class = TREE_TYPE (TREE_VALUE (current));
7602 layout_class (current_class);
7603
7604 /* Error reported by the caller */
7605 if (java_error_count)
7606 return;
7607 }
7608
7609 /* We might have reloaded classes durign the process of laying out
7610 classes for code generation. We must layout the methods of those
7611 late additions, as constructor checks might use them */
7612 java_layout_seen_class_methods ();
7613 java_parse_abort_on_error ();
7614 }
7615
7616 /* Expand methods in the current set of classes rememebered for
7617 generation. */
7618
7619 static void
7620 java_complete_expand_classes ()
7621 {
7622 tree current;
7623
7624 do_not_fold = flag_emit_xref;
7625
7626 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7627 if (!INNER_CLASS_DECL_P (current))
7628 java_complete_expand_class (current);
7629 }
7630
7631 /* Expand the methods found in OUTER, starting first by OUTER's inner
7632 classes, if any. */
7633
7634 static void
7635 java_complete_expand_class (outer)
7636 tree outer;
7637 {
7638 tree inner_list;
7639
7640 set_nested_class_simple_name_value (outer, 1); /* Set */
7641
7642 /* We need to go after all inner classes and start expanding them,
7643 starting with most nested ones. We have to do that because nested
7644 classes might add functions to outer classes */
7645
7646 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7647 inner_list; inner_list = TREE_CHAIN (inner_list))
7648 java_complete_expand_class (TREE_PURPOSE (inner_list));
7649
7650 java_complete_expand_methods (outer);
7651 set_nested_class_simple_name_value (outer, 0); /* Reset */
7652 }
7653
7654 /* Expand methods registered in CLASS_DECL. The general idea is that
7655 we expand regular methods first. This allows us get an estimate on
7656 how outer context local alias fields are really used so we can add
7657 to the constructor just enough code to initialize them properly (it
7658 also lets us generate finit$ correctly.) Then we expand the
7659 constructors and then <clinit>. */
7660
7661 static void
7662 java_complete_expand_methods (class_decl)
7663 tree class_decl;
7664 {
7665 tree clinit, decl, first_decl;
7666
7667 current_class = TREE_TYPE (class_decl);
7668
7669 /* Initialize a new constant pool */
7670 init_outgoing_cpool ();
7671
7672 /* Pre-expand <clinit> to figure whether we really need it or
7673 not. If we do need it, we pre-expand the static fields so they're
7674 ready to be used somewhere else. <clinit> will be fully expanded
7675 after we processed the constructors. */
7676 first_decl = TYPE_METHODS (current_class);
7677 clinit = maybe_generate_pre_expand_clinit (current_class);
7678
7679 /* Then generate finit$ (if we need to) because constructors will
7680 try to use it.*/
7681 if (TYPE_FINIT_STMT_LIST (current_class))
7682 java_complete_expand_method (generate_finit (current_class));
7683
7684 /* Then generate instinit$ (if we need to) because constructors will
7685 try to use it. */
7686 if (TYPE_II_STMT_LIST (current_class))
7687 java_complete_expand_method (generate_instinit (current_class));
7688
7689 /* Now do the constructors */
7690 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7691 {
7692 int no_body;
7693
7694 if (!DECL_CONSTRUCTOR_P (decl))
7695 continue;
7696
7697 no_body = !DECL_FUNCTION_BODY (decl);
7698 /* Don't generate debug info on line zero when expanding a
7699 generated constructor. */
7700 if (no_body)
7701 restore_line_number_status (1);
7702
7703 java_complete_expand_method (decl);
7704
7705 if (no_body)
7706 restore_line_number_status (0);
7707 }
7708
7709 /* First, do the ordinary methods. */
7710 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7711 {
7712 /* Ctors aren't part of this batch. */
7713 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7714 continue;
7715
7716 /* Skip abstract or native methods -- but do handle native
7717 methods when generating JNI stubs. */
7718 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7719 {
7720 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7721 continue;
7722 }
7723
7724 if (METHOD_NATIVE (decl))
7725 {
7726 tree body = build_jni_stub (decl);
7727 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7728 }
7729
7730 java_complete_expand_method (decl);
7731 }
7732
7733 /* If there is indeed a <clinit>, fully expand it now */
7734 if (clinit)
7735 {
7736 /* Prevent the use of `this' inside <clinit> */
7737 ctxp->explicit_constructor_p = 1;
7738 java_complete_expand_method (clinit);
7739 ctxp->explicit_constructor_p = 0;
7740 }
7741
7742 /* We might have generated a class$ that we now want to expand */
7743 if (TYPE_DOT_CLASS (current_class))
7744 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7745
7746 /* Now verify constructor circularity (stop after the first one we
7747 prove wrong.) */
7748 if (!CLASS_INTERFACE (class_decl))
7749 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7750 if (DECL_CONSTRUCTOR_P (decl)
7751 && verify_constructor_circularity (decl, decl))
7752 break;
7753
7754 /* Save the constant pool. We'll need to restore it later. */
7755 TYPE_CPOOL (current_class) = outgoing_cpool;
7756 }
7757
7758 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7759 safely used in some other methods/constructors. */
7760
7761 static tree
7762 maybe_generate_pre_expand_clinit (class_type)
7763 tree class_type;
7764 {
7765 tree current, mdecl;
7766
7767 if (!TYPE_CLINIT_STMT_LIST (class_type))
7768 return NULL_TREE;
7769
7770 /* Go through all static fields and pre expand them */
7771 for (current = TYPE_FIELDS (class_type); current;
7772 current = TREE_CHAIN (current))
7773 if (FIELD_STATIC (current))
7774 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7775
7776 /* Then build the <clinit> method */
7777 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7778 clinit_identifier_node, end_params_node);
7779 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7780 mdecl, NULL_TREE);
7781 start_artificial_method_body (mdecl);
7782
7783 /* We process the list of assignment we produced as the result of
7784 the declaration of initialized static field and add them as
7785 statement to the <clinit> method. */
7786 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7787 current = TREE_CHAIN (current))
7788 {
7789 tree stmt = current;
7790 /* We build the assignment expression that will initialize the
7791 field to its value. There are strict rules on static
7792 initializers (8.5). FIXME */
7793 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7794 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7795 java_method_add_stmt (mdecl, stmt);
7796 }
7797
7798 end_artificial_method_body (mdecl);
7799
7800 /* Now we want to place <clinit> as the last method (because we need
7801 it at least for interface so that it doesn't interfere with the
7802 dispatch table based lookup. */
7803 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7804 {
7805 current = TREE_CHAIN (TYPE_METHODS (class_type));
7806 TYPE_METHODS (class_type) = current;
7807
7808 while (TREE_CHAIN (current))
7809 current = TREE_CHAIN (current);
7810
7811 TREE_CHAIN (current) = mdecl;
7812 TREE_CHAIN (mdecl) = NULL_TREE;
7813 }
7814
7815 return mdecl;
7816 }
7817
7818 /* Analyzes a method body and look for something that isn't a
7819 MODIFY_EXPR with a constant value. */
7820
7821 static int
7822 analyze_clinit_body (this_class, bbody)
7823 tree this_class, bbody;
7824 {
7825 while (bbody)
7826 switch (TREE_CODE (bbody))
7827 {
7828 case BLOCK:
7829 bbody = BLOCK_EXPR_BODY (bbody);
7830 break;
7831
7832 case EXPR_WITH_FILE_LOCATION:
7833 bbody = EXPR_WFL_NODE (bbody);
7834 break;
7835
7836 case COMPOUND_EXPR:
7837 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7838 return 1;
7839 bbody = TREE_OPERAND (bbody, 1);
7840 break;
7841
7842 case MODIFY_EXPR:
7843 /* If we're generating to class file and we're dealing with an
7844 array initialization, we return 1 to keep <clinit> */
7845 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7846 && flag_emit_class_files)
7847 return 1;
7848
7849 /* There are a few cases where we're required to keep
7850 <clinit>:
7851 - If this is an assignment whose operand is not constant,
7852 - If this is an assignment to a non-initialized field,
7853 - If this field is not a member of the current class.
7854 */
7855 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
7856 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
7857 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
7858
7859 default:
7860 return 1;
7861 }
7862 return 0;
7863 }
7864
7865
7866 /* See whether we could get rid of <clinit>. Criteria are: all static
7867 final fields have constant initial values and the body of <clinit>
7868 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7869
7870 static int
7871 maybe_yank_clinit (mdecl)
7872 tree mdecl;
7873 {
7874 tree type, current;
7875 tree fbody, bbody;
7876
7877 if (!DECL_CLINIT_P (mdecl))
7878 return 0;
7879
7880 /* If the body isn't empty, then we keep <clinit>. Note that if
7881 we're emitting classfiles, this isn't enough not to rule it
7882 out. */
7883 fbody = DECL_FUNCTION_BODY (mdecl);
7884 bbody = BLOCK_EXPR_BODY (fbody);
7885 if (bbody && bbody != error_mark_node)
7886 bbody = BLOCK_EXPR_BODY (bbody);
7887 else
7888 return 0;
7889 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7890 return 0;
7891
7892 type = DECL_CONTEXT (mdecl);
7893 current = TYPE_FIELDS (type);
7894
7895 for (current = (current ? TREE_CHAIN (current) : current);
7896 current; current = TREE_CHAIN (current))
7897 {
7898 tree f_init;
7899
7900 /* We're not interested in non-static fields. */
7901 if (!FIELD_STATIC (current))
7902 continue;
7903
7904 /* Nor in fields without initializers. */
7905 f_init = DECL_INITIAL (current);
7906 if (f_init == NULL_TREE)
7907 continue;
7908
7909 /* Anything that isn't String or a basic type is ruled out -- or
7910 if we know how to deal with it (when doing things natively) we
7911 should generated an empty <clinit> so that SUID are computed
7912 correctly. */
7913 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7914 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7915 return 0;
7916
7917 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
7918 return 0;
7919 }
7920
7921 /* Now we analyze the method body and look for something that
7922 isn't a MODIFY_EXPR */
7923 if (bbody != empty_stmt_node && analyze_clinit_body (type, bbody))
7924 return 0;
7925
7926 /* Get rid of <clinit> in the class' list of methods */
7927 if (TYPE_METHODS (type) == mdecl)
7928 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7929 else
7930 for (current = TYPE_METHODS (type); current;
7931 current = TREE_CHAIN (current))
7932 if (TREE_CHAIN (current) == mdecl)
7933 {
7934 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7935 break;
7936 }
7937
7938 return 1;
7939 }
7940
7941 /* Install the argument from MDECL. Suitable to completion and
7942 expansion of mdecl's body. */
7943
7944 static void
7945 start_complete_expand_method (mdecl)
7946 tree mdecl;
7947 {
7948 tree tem;
7949
7950 pushlevel (1); /* Prepare for a parameter push */
7951 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7952 DECL_ARGUMENTS (mdecl) = tem;
7953
7954 for (; tem; tem = TREE_CHAIN (tem))
7955 {
7956 /* TREE_CHAIN (tem) will change after pushdecl. */
7957 tree next = TREE_CHAIN (tem);
7958 tree type = TREE_TYPE (tem);
7959 if (PROMOTE_PROTOTYPES
7960 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7961 && INTEGRAL_TYPE_P (type))
7962 type = integer_type_node;
7963 DECL_ARG_TYPE (tem) = type;
7964 layout_decl (tem, 0);
7965 pushdecl (tem);
7966 /* Re-install the next so that the list is kept and the loop
7967 advances. */
7968 TREE_CHAIN (tem) = next;
7969 }
7970 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7971 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7972 build_result_decl (mdecl);
7973 }
7974
7975
7976 /* Complete and expand a method. */
7977
7978 static void
7979 java_complete_expand_method (mdecl)
7980 tree mdecl;
7981 {
7982 tree fbody, block_body, exception_copy;
7983
7984 current_function_decl = mdecl;
7985 /* Fix constructors before expanding them */
7986 if (DECL_CONSTRUCTOR_P (mdecl))
7987 fix_constructors (mdecl);
7988
7989 /* Expand functions that have a body */
7990 if (!DECL_FUNCTION_BODY (mdecl))
7991 return;
7992
7993 fbody = DECL_FUNCTION_BODY (mdecl);
7994 block_body = BLOCK_EXPR_BODY (fbody);
7995 exception_copy = NULL_TREE;
7996
7997 current_function_decl = mdecl;
7998
7999 if (! quiet_flag)
8000 fprintf (stderr, " [%s.",
8001 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8002 announce_function (mdecl);
8003 if (! quiet_flag)
8004 fprintf (stderr, "]");
8005
8006 /* Prepare the function for tree completion */
8007 start_complete_expand_method (mdecl);
8008
8009 /* Install the current this */
8010 current_this = (!METHOD_STATIC (mdecl) ?
8011 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8012
8013 /* Purge the `throws' list of unchecked exceptions (we save a copy
8014 of the list and re-install it later.) */
8015 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8016 purge_unchecked_exceptions (mdecl);
8017
8018 /* Install exceptions thrown with `throws' */
8019 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8020
8021 if (block_body != NULL_TREE)
8022 {
8023 block_body = java_complete_tree (block_body);
8024
8025 /* Before we check initialization, attached all class initialization
8026 variable to the block_body */
8027 hash_traverse (&DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8028 attach_init_test_initialization_flags, block_body);
8029
8030 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8031 {
8032 check_for_initialization (block_body, mdecl);
8033
8034 /* Go through all the flags marking the initialization of
8035 static variables and see whether they're definitively
8036 assigned, in which case the type is remembered as
8037 definitively initialized in MDECL. */
8038 if (STATIC_CLASS_INIT_OPT_P ())
8039 {
8040 /* Always register the context as properly initialized in
8041 MDECL. This used with caution helps removing extra
8042 initialization of self. */
8043 if (METHOD_STATIC (mdecl))
8044 hash_lookup (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8045 (hash_table_key) DECL_CONTEXT (mdecl),
8046 TRUE, NULL);
8047 }
8048 }
8049 ctxp->explicit_constructor_p = 0;
8050 }
8051
8052 BLOCK_EXPR_BODY (fbody) = block_body;
8053
8054 /* If we saw a return but couldn't evaluate it properly, we'll have
8055 an error_mark_node here. */
8056 if (block_body != error_mark_node
8057 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8058 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8059 && !flag_emit_xref)
8060 missing_return_error (current_function_decl);
8061
8062 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8063 maybe_yank_clinit (mdecl);
8064
8065 /* Pop the current level, with special measures if we found errors. */
8066 if (java_error_count)
8067 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8068 poplevel (1, 0, 1);
8069
8070 /* Pop the exceptions and sanity check */
8071 POP_EXCEPTIONS();
8072 if (currently_caught_type_list)
8073 abort ();
8074
8075 /* Restore the copy of the list of exceptions if emitting xrefs. */
8076 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8077 }
8078
8079 /* For with each class for which there's code to generate. */
8080
8081 static void
8082 java_expand_method_bodies (class)
8083 tree class;
8084 {
8085 tree decl;
8086 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8087 {
8088 if (!DECL_FUNCTION_BODY (decl))
8089 continue;
8090
8091 current_function_decl = decl;
8092
8093 /* It's time to assign the variable flagging static class
8094 initialization based on which classes invoked static methods
8095 are definitely initializing. This should be flagged. */
8096 if (STATIC_CLASS_INIT_OPT_P ())
8097 {
8098 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8099 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8100 {
8101 /* Executed for each statement calling a static function.
8102 LIST is a TREE_LIST whose PURPOSE is the called function
8103 and VALUE is a compound whose second operand can be patched
8104 with static class initialization flag assignments. */
8105
8106 tree called_method = TREE_PURPOSE (list);
8107 tree compound = TREE_VALUE (list);
8108 tree assignment_compound_list
8109 = build_tree_list (called_method, NULL);
8110
8111 /* For each class definitely initialized in
8112 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8113 assignment to the class initialization flag. */
8114 hash_traverse (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8115 emit_test_initialization,
8116 assignment_compound_list);
8117
8118 if (TREE_VALUE (assignment_compound_list))
8119 TREE_OPERAND (compound, 1)
8120 = TREE_VALUE (assignment_compound_list);
8121 }
8122 }
8123
8124 /* Prepare the function for RTL expansion */
8125 start_complete_expand_method (decl);
8126
8127 /* Expand function start, generate initialization flag
8128 assignment, and handle synchronized methods. */
8129 complete_start_java_method (decl);
8130
8131 /* Expand the rest of the function body and terminate
8132 expansion. */
8133 source_end_java_method ();
8134 }
8135 }
8136
8137 \f
8138
8139 /* This section of the code deals with accessing enclosing context
8140 fields either directly by using the relevant access to this$<n> or
8141 by invoking an access method crafted for that purpose. */
8142
8143 /* Build the necessary access from an inner class to an outer
8144 class. This routine could be optimized to cache previous result
8145 (decl, current_class and returned access). When an access method
8146 needs to be generated, it always takes the form of a read. It might
8147 be later turned into a write by calling outer_field_access_fix. */
8148
8149 static tree
8150 build_outer_field_access (id, decl)
8151 tree id, decl;
8152 {
8153 tree access = NULL_TREE;
8154 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8155 tree decl_ctx = DECL_CONTEXT (decl);
8156
8157 /* If the immediate enclosing context of the current class is the
8158 field decl's class or inherits from it; build the access as
8159 `this$<n>.<field>'. Note that we will break the `private' barrier
8160 if we're not emitting bytecodes. */
8161 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8162 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8163 {
8164 tree thisn = build_current_thisn (current_class);
8165 access = make_qualified_primary (build_wfl_node (thisn),
8166 id, EXPR_WFL_LINECOL (id));
8167 }
8168 /* Otherwise, generate access methods to outer this and access the
8169 field (either using an access method or by direct access.) */
8170 else
8171 {
8172 int lc = EXPR_WFL_LINECOL (id);
8173
8174 /* Now we chain the required number of calls to the access$0 to
8175 get a hold to the enclosing instance we need, and then we
8176 build the field access. */
8177 access = build_access_to_thisn (current_class, decl_ctx, lc);
8178
8179 /* If the field is private and we're generating bytecode, then
8180 we generate an access method */
8181 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8182 {
8183 tree name = build_outer_field_access_methods (decl);
8184 access = build_outer_field_access_expr (lc, decl_ctx,
8185 name, access, NULL_TREE);
8186 }
8187 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8188 Once again we break the `private' access rule from a foreign
8189 class. */
8190 else
8191 access = make_qualified_primary (access, id, lc);
8192 }
8193 return resolve_expression_name (access, NULL);
8194 }
8195
8196 /* Return a non zero value if NODE describes an outer field inner
8197 access. */
8198
8199 static int
8200 outer_field_access_p (type, decl)
8201 tree type, decl;
8202 {
8203 if (!INNER_CLASS_TYPE_P (type)
8204 || TREE_CODE (decl) != FIELD_DECL
8205 || DECL_CONTEXT (decl) == type)
8206 return 0;
8207
8208 /* If the inner class extends the declaration context of the field
8209 we're try to acces, then this isn't an outer field access */
8210 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8211 return 0;
8212
8213 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8214 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8215 {
8216 if (type == DECL_CONTEXT (decl))
8217 return 1;
8218
8219 if (!DECL_CONTEXT (TYPE_NAME (type)))
8220 {
8221 /* Before we give up, see whether the field is inherited from
8222 the enclosing context we're considering. */
8223 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8224 return 1;
8225 break;
8226 }
8227 }
8228
8229 return 0;
8230 }
8231
8232 /* Return a non zero value if NODE represents an outer field inner
8233 access that was been already expanded. As a side effect, it returns
8234 the name of the field being accessed and the argument passed to the
8235 access function, suitable for a regeneration of the access method
8236 call if necessary. */
8237
8238 static int
8239 outer_field_expanded_access_p (node, name, arg_type, arg)
8240 tree node, *name, *arg_type, *arg;
8241 {
8242 int identified = 0;
8243
8244 if (TREE_CODE (node) != CALL_EXPR)
8245 return 0;
8246
8247 /* Well, gcj generates slightly different tree nodes when compiling
8248 to native or bytecodes. It's the case for function calls. */
8249
8250 if (flag_emit_class_files
8251 && TREE_CODE (node) == CALL_EXPR
8252 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8253 identified = 1;
8254 else if (!flag_emit_class_files)
8255 {
8256 node = TREE_OPERAND (node, 0);
8257
8258 if (node && TREE_OPERAND (node, 0)
8259 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8260 {
8261 node = TREE_OPERAND (node, 0);
8262 if (TREE_OPERAND (node, 0)
8263 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8264 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8265 (DECL_NAME (TREE_OPERAND (node, 0)))))
8266 identified = 1;
8267 }
8268 }
8269
8270 if (identified && name && arg_type && arg)
8271 {
8272 tree argument = TREE_OPERAND (node, 1);
8273 *name = DECL_NAME (TREE_OPERAND (node, 0));
8274 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8275 *arg = TREE_VALUE (argument);
8276 }
8277 return identified;
8278 }
8279
8280 /* Detect in NODE an outer field read access from an inner class and
8281 transform it into a write with RHS as an argument. This function is
8282 called from the java_complete_lhs when an assignment to a LHS can
8283 be identified. */
8284
8285 static tree
8286 outer_field_access_fix (wfl, node, rhs)
8287 tree wfl, node, rhs;
8288 {
8289 tree name, arg_type, arg;
8290
8291 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8292 {
8293 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8294 arg_type, name, arg, rhs);
8295 return java_complete_tree (node);
8296 }
8297 return NULL_TREE;
8298 }
8299
8300 /* Construct the expression that calls an access method:
8301 <type>.access$<n>(<arg1> [, <arg2>]);
8302
8303 ARG2 can be NULL and will be omitted in that case. It will denote a
8304 read access. */
8305
8306 static tree
8307 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8308 int lc;
8309 tree type, access_method_name, arg1, arg2;
8310 {
8311 tree args, cn, access;
8312
8313 args = arg1 ? arg1 :
8314 build_wfl_node (build_current_thisn (current_class));
8315 args = build_tree_list (NULL_TREE, args);
8316
8317 if (arg2)
8318 args = tree_cons (NULL_TREE, arg2, args);
8319
8320 access = build_method_invocation (build_wfl_node (access_method_name), args);
8321 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8322 return make_qualified_primary (cn, access, lc);
8323 }
8324
8325 static tree
8326 build_new_access_id ()
8327 {
8328 static int access_n_counter = 1;
8329 char buffer [128];
8330
8331 sprintf (buffer, "access$%d", access_n_counter++);
8332 return get_identifier (buffer);
8333 }
8334
8335 /* Create the static access functions for the outer field DECL. We define a
8336 read:
8337 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8338 return inst$.field;
8339 }
8340 and a write access:
8341 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8342 TREE_TYPE (<field>) value$) {
8343 return inst$.field = value$;
8344 }
8345 We should have a usage flags on the DECL so we can lazily turn the ones
8346 we're using for code generation. FIXME.
8347 */
8348
8349 static tree
8350 build_outer_field_access_methods (decl)
8351 tree decl;
8352 {
8353 tree id, args, stmt, mdecl;
8354
8355 if (FIELD_INNER_ACCESS_P (decl))
8356 return FIELD_INNER_ACCESS (decl);
8357
8358 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8359
8360 /* Create the identifier and a function named after it. */
8361 id = build_new_access_id ();
8362
8363 /* The identifier is marked as bearing the name of a generated write
8364 access function for outer field accessed from inner classes. */
8365 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8366
8367 /* Create the read access */
8368 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8369 TREE_CHAIN (args) = end_params_node;
8370 stmt = make_qualified_primary (build_wfl_node (inst_id),
8371 build_wfl_node (DECL_NAME (decl)), 0);
8372 stmt = build_return (0, stmt);
8373 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8374 TREE_TYPE (decl), id, args, stmt);
8375 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8376
8377 /* Create the write access method. No write access for final variable */
8378 if (!FIELD_FINAL (decl))
8379 {
8380 args = build_tree_list (inst_id,
8381 build_pointer_type (DECL_CONTEXT (decl)));
8382 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8383 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8384 stmt = make_qualified_primary (build_wfl_node (inst_id),
8385 build_wfl_node (DECL_NAME (decl)), 0);
8386 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8387 build_wfl_node (wpv_id)));
8388 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8389 TREE_TYPE (decl), id,
8390 args, stmt);
8391 }
8392 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8393
8394 /* Return the access name */
8395 return FIELD_INNER_ACCESS (decl) = id;
8396 }
8397
8398 /* Build an field access method NAME. */
8399
8400 static tree
8401 build_outer_field_access_method (class, type, name, args, body)
8402 tree class, type, name, args, body;
8403 {
8404 tree saved_current_function_decl, mdecl;
8405
8406 /* Create the method */
8407 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8408 fix_method_argument_names (args, mdecl);
8409 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8410
8411 /* Attach the method body. */
8412 saved_current_function_decl = current_function_decl;
8413 start_artificial_method_body (mdecl);
8414 java_method_add_stmt (mdecl, body);
8415 end_artificial_method_body (mdecl);
8416 current_function_decl = saved_current_function_decl;
8417
8418 return mdecl;
8419 }
8420
8421 \f
8422 /* This section deals with building access function necessary for
8423 certain kinds of method invocation from inner classes. */
8424
8425 static tree
8426 build_outer_method_access_method (decl)
8427 tree decl;
8428 {
8429 tree saved_current_function_decl, mdecl;
8430 tree args = NULL_TREE, call_args = NULL_TREE;
8431 tree carg, id, body, class;
8432 char buffer [80];
8433 int parm_id_count = 0;
8434
8435 /* Test this abort with an access to a private field */
8436 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8437 abort ();
8438
8439 /* Check the cache first */
8440 if (DECL_FUNCTION_INNER_ACCESS (decl))
8441 return DECL_FUNCTION_INNER_ACCESS (decl);
8442
8443 class = DECL_CONTEXT (decl);
8444
8445 /* Obtain an access identifier and mark it */
8446 id = build_new_access_id ();
8447 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8448
8449 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8450 /* Create the arguments, as much as the original */
8451 for (; carg && carg != end_params_node;
8452 carg = TREE_CHAIN (carg))
8453 {
8454 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8455 args = chainon (args, build_tree_list (get_identifier (buffer),
8456 TREE_VALUE (carg)));
8457 }
8458 args = chainon (args, end_params_node);
8459
8460 /* Create the method */
8461 mdecl = create_artificial_method (class, ACC_STATIC,
8462 TREE_TYPE (TREE_TYPE (decl)), id, args);
8463 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8464 /* There is a potential bug here. We should be able to use
8465 fix_method_argument_names, but then arg names get mixed up and
8466 eventually a constructor will have its this$0 altered and the
8467 outer context won't be assignment properly. The test case is
8468 stub.java FIXME */
8469 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8470
8471 /* Attach the method body. */
8472 saved_current_function_decl = current_function_decl;
8473 start_artificial_method_body (mdecl);
8474
8475 /* The actual method invocation uses the same args. When invoking a
8476 static methods that way, we don't want to skip the first
8477 argument. */
8478 carg = args;
8479 if (!METHOD_STATIC (decl))
8480 carg = TREE_CHAIN (carg);
8481 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8482 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8483 call_args);
8484
8485 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8486 call_args);
8487 if (!METHOD_STATIC (decl))
8488 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8489 body, 0);
8490 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8491 body = build_return (0, body);
8492 java_method_add_stmt (mdecl,body);
8493 end_artificial_method_body (mdecl);
8494 current_function_decl = saved_current_function_decl;
8495
8496 /* Back tag the access function so it know what it accesses */
8497 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8498
8499 /* Tag the current method so it knows it has an access generated */
8500 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8501 }
8502
8503 \f
8504 /* This section of the code deals with building expressions to access
8505 the enclosing instance of an inner class. The enclosing instance is
8506 kept in a generated field called this$<n>, with <n> being the
8507 inner class nesting level (starting from 0.) */
8508
8509 /* Build an access to a given this$<n>, always chaining access call to
8510 others. Access methods to this$<n> are build on the fly if
8511 necessary. This CAN'T be used to solely access this$<n-1> from
8512 this$<n> (which alway yield to special cases and optimization, see
8513 for example build_outer_field_access). */
8514
8515 static tree
8516 build_access_to_thisn (from, to, lc)
8517 tree from, to;
8518 int lc;
8519 {
8520 tree access = NULL_TREE;
8521
8522 while (from != to)
8523 {
8524 if (!access)
8525 {
8526 access = build_current_thisn (from);
8527 access = build_wfl_node (access);
8528 }
8529 else
8530 {
8531 tree access0_wfl, cn;
8532
8533 maybe_build_thisn_access_method (from);
8534 access0_wfl = build_wfl_node (access0_identifier_node);
8535 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8536 EXPR_WFL_LINECOL (access0_wfl) = lc;
8537 access = build_tree_list (NULL_TREE, access);
8538 access = build_method_invocation (access0_wfl, access);
8539 access = make_qualified_primary (cn, access, lc);
8540 }
8541
8542 /* if FROM isn't an inter class, that's fine, we've done
8543 enough. What we're looking for can be accessed from there. */
8544 from = DECL_CONTEXT (TYPE_NAME (from));
8545 if (!from)
8546 break;
8547 from = TREE_TYPE (from);
8548 }
8549 return access;
8550 }
8551
8552 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8553 is returned if nothing needs to be generated. Otherwise, the method
8554 generated and a method decl is returned.
8555
8556 NOTE: These generated methods should be declared in a class file
8557 attribute so that they can't be referred to directly. */
8558
8559 static tree
8560 maybe_build_thisn_access_method (type)
8561 tree type;
8562 {
8563 tree mdecl, args, stmt, rtype;
8564 tree saved_current_function_decl;
8565
8566 /* If TYPE is a top-level class, no access method is required.
8567 If there already is such an access method, bail out. */
8568 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8569 return NULL_TREE;
8570
8571 /* We generate the method. The method looks like:
8572 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8573 */
8574 args = build_tree_list (inst_id, build_pointer_type (type));
8575 TREE_CHAIN (args) = end_params_node;
8576 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8577 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8578 access0_identifier_node, args);
8579 fix_method_argument_names (args, mdecl);
8580 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8581 stmt = build_current_thisn (type);
8582 stmt = make_qualified_primary (build_wfl_node (inst_id),
8583 build_wfl_node (stmt), 0);
8584 stmt = build_return (0, stmt);
8585
8586 saved_current_function_decl = current_function_decl;
8587 start_artificial_method_body (mdecl);
8588 java_method_add_stmt (mdecl, stmt);
8589 end_artificial_method_body (mdecl);
8590 current_function_decl = saved_current_function_decl;
8591
8592 CLASS_ACCESS0_GENERATED_P (type) = 1;
8593
8594 return mdecl;
8595 }
8596
8597 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8598 the first level of innerclassing. this$1 for the next one, etc...
8599 This function can be invoked with TYPE to NULL, available and then
8600 has to count the parser context. */
8601
8602 static tree
8603 build_current_thisn (type)
8604 tree type;
8605 {
8606 static int saved_i = -1;
8607 static tree saved_thisn = NULL_TREE;
8608 static tree saved_type = NULL_TREE;
8609 static int saved_type_i = 0;
8610 static int initialized_p;
8611 tree decl;
8612 char buffer [24];
8613 int i = 0;
8614
8615 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8616 if (!initialized_p)
8617 {
8618 ggc_add_tree_root (&saved_thisn, 1);
8619 ggc_add_tree_root (&saved_type, 1);
8620 initialized_p = 1;
8621 }
8622
8623 if (type)
8624 {
8625 if (type == saved_type)
8626 i = saved_type_i;
8627 else
8628 {
8629 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8630 decl; decl = DECL_CONTEXT (decl), i++)
8631 ;
8632
8633 saved_type = type;
8634 saved_type_i = i;
8635 }
8636 }
8637 else
8638 i = list_length (GET_CPC_LIST ())-2;
8639
8640 if (i == saved_i)
8641 return saved_thisn;
8642
8643 sprintf (buffer, "this$%d", i);
8644 saved_i = i;
8645 saved_thisn = get_identifier (buffer);
8646 return saved_thisn;
8647 }
8648
8649 /* Return the assignement to the hidden enclosing context `this$<n>'
8650 by the second incoming parameter to the innerclass constructor. The
8651 form used is `this.this$<n> = this$<n>;'. */
8652
8653 static tree
8654 build_thisn_assign ()
8655 {
8656 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8657 {
8658 tree thisn = build_current_thisn (current_class);
8659 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8660 build_wfl_node (thisn), 0);
8661 tree rhs = build_wfl_node (thisn);
8662 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8663 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8664 }
8665 return NULL_TREE;
8666 }
8667
8668 \f
8669 /* Building the synthetic `class$' used to implement the `.class' 1.1
8670 extension for non primitive types. This method looks like:
8671
8672 static Class class$(String type) throws NoClassDefFoundError
8673 {
8674 try {return (java.lang.Class.forName (String));}
8675 catch (ClassNotFoundException e) {
8676 throw new NoClassDefFoundError(e.getMessage());}
8677 } */
8678
8679 static tree
8680 build_dot_class_method (class)
8681 tree class;
8682 {
8683 #define BWF(S) build_wfl_node (get_identifier ((S)))
8684 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8685 tree args, tmp, saved_current_function_decl, mdecl;
8686 tree stmt, throw_stmt;
8687
8688 static tree get_message_wfl, type_parm_wfl;
8689
8690 if (!get_message_wfl)
8691 {
8692 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8693 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8694 ggc_add_tree_root (&get_message_wfl, 1);
8695 ggc_add_tree_root (&type_parm_wfl, 1);
8696 }
8697
8698 /* Build the arguments */
8699 args = build_tree_list (get_identifier ("type$"),
8700 build_pointer_type (string_type_node));
8701 TREE_CHAIN (args) = end_params_node;
8702
8703 /* Build the qualified name java.lang.Class.forName */
8704 tmp = MQN (MQN (MQN (BWF ("java"),
8705 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8706 load_class (class_not_found_type_node, 1);
8707 load_class (no_class_def_found_type_node, 1);
8708
8709 /* Create the "class$" function */
8710 mdecl = create_artificial_method (class, ACC_STATIC,
8711 build_pointer_type (class_type_node),
8712 classdollar_identifier_node, args);
8713 DECL_FUNCTION_THROWS (mdecl) =
8714 build_tree_list (NULL_TREE, no_class_def_found_type_node);
8715
8716 /* We start by building the try block. We need to build:
8717 return (java.lang.Class.forName (type)); */
8718 stmt = build_method_invocation (tmp,
8719 build_tree_list (NULL_TREE, type_parm_wfl));
8720 stmt = build_return (0, stmt);
8721
8722 /* Now onto the catch block. We start by building the expression
8723 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8724 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8725 get_message_wfl, 0);
8726 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8727
8728 /* Build new NoClassDefFoundError (_.getMessage) */
8729 throw_stmt = build_new_invocation
8730 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8731 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8732
8733 /* Build the throw, (it's too early to use BUILD_THROW) */
8734 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8735
8736 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8737 stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8738 stmt, throw_stmt);
8739
8740 fix_method_argument_names (args, mdecl);
8741 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8742 saved_current_function_decl = current_function_decl;
8743 start_artificial_method_body (mdecl);
8744 java_method_add_stmt (mdecl, stmt);
8745 end_artificial_method_body (mdecl);
8746 current_function_decl = saved_current_function_decl;
8747 TYPE_DOT_CLASS (class) = mdecl;
8748
8749 return mdecl;
8750 }
8751
8752 static tree
8753 build_dot_class_method_invocation (type)
8754 tree type;
8755 {
8756 tree sig_id, s;
8757
8758 if (TYPE_ARRAY_P (type))
8759 sig_id = build_java_signature (type);
8760 else
8761 sig_id = DECL_NAME (TYPE_NAME (type));
8762
8763 /* Ensure that the proper name separator is used */
8764 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8765 IDENTIFIER_LENGTH (sig_id));
8766
8767 s = build_string (IDENTIFIER_LENGTH (sig_id),
8768 IDENTIFIER_POINTER (sig_id));
8769 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8770 build_tree_list (NULL_TREE, s));
8771 }
8772
8773 /* This section of the code deals with constructor. */
8774
8775 /* Craft a body for default constructor. Patch existing constructor
8776 bodies with call to super() and field initialization statements if
8777 necessary. */
8778
8779 static void
8780 fix_constructors (mdecl)
8781 tree mdecl;
8782 {
8783 tree iii; /* Instance Initializer Invocation */
8784 tree body = DECL_FUNCTION_BODY (mdecl);
8785 tree thisn_assign, compound = NULL_TREE;
8786 tree class_type = DECL_CONTEXT (mdecl);
8787
8788 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8789 return;
8790 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8791
8792 if (!body)
8793 {
8794 /* It is an error for the compiler to generate a default
8795 constructor if the superclass doesn't have a constructor that
8796 takes no argument, or the same args for an anonymous class */
8797 if (verify_constructor_super (mdecl))
8798 {
8799 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8800 tree save = DECL_NAME (mdecl);
8801 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8802 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8803 parse_error_context
8804 (lookup_cl (TYPE_NAME (class_type)),
8805 "No constructor matching `%s' found in class `%s'",
8806 lang_printable_name (mdecl, 0), n);
8807 DECL_NAME (mdecl) = save;
8808 }
8809
8810 /* The constructor body must be crafted by hand. It's the
8811 constructor we defined when we realize we didn't have the
8812 CLASSNAME() constructor */
8813 start_artificial_method_body (mdecl);
8814
8815 /* Insert an assignment to the this$<n> hidden field, if
8816 necessary */
8817 if ((thisn_assign = build_thisn_assign ()))
8818 java_method_add_stmt (mdecl, thisn_assign);
8819
8820 /* We don't generate a super constructor invocation if we're
8821 compiling java.lang.Object. build_super_invocation takes care
8822 of that. */
8823 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8824
8825 /* FIXME */
8826 if ((iii = build_instinit_invocation (class_type)))
8827 java_method_add_stmt (mdecl, iii);
8828
8829 end_artificial_method_body (mdecl);
8830 }
8831 /* Search for an explicit constructor invocation */
8832 else
8833 {
8834 int found = 0;
8835 int invokes_this = 0;
8836 tree found_call = NULL_TREE;
8837 tree main_block = BLOCK_EXPR_BODY (body);
8838
8839 while (body)
8840 switch (TREE_CODE (body))
8841 {
8842 case CALL_EXPR:
8843 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8844 if (CALL_THIS_CONSTRUCTOR_P (body))
8845 invokes_this = 1;
8846 body = NULL_TREE;
8847 break;
8848 case COMPOUND_EXPR:
8849 case EXPR_WITH_FILE_LOCATION:
8850 found_call = body;
8851 body = TREE_OPERAND (body, 0);
8852 break;
8853 case BLOCK:
8854 found_call = body;
8855 body = BLOCK_EXPR_BODY (body);
8856 break;
8857 default:
8858 found = 0;
8859 body = NULL_TREE;
8860 }
8861
8862 /* Generate the assignment to this$<n>, if necessary */
8863 if ((thisn_assign = build_thisn_assign ()))
8864 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8865
8866 /* The constructor is missing an invocation of super() */
8867 if (!found)
8868 compound = add_stmt_to_compound (compound, NULL_TREE,
8869 build_super_invocation (mdecl));
8870 /* Explicit super() invokation should take place before the
8871 instance initializer blocks. */
8872 else
8873 {
8874 compound = add_stmt_to_compound (compound, NULL_TREE,
8875 TREE_OPERAND (found_call, 0));
8876 TREE_OPERAND (found_call, 0) = empty_stmt_node;
8877 }
8878
8879 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
8880
8881 /* Insert the instance initializer block right after. */
8882 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8883 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8884
8885 /* Fix the constructor main block if we're adding extra stmts */
8886 if (compound)
8887 {
8888 compound = add_stmt_to_compound (compound, NULL_TREE,
8889 BLOCK_EXPR_BODY (main_block));
8890 BLOCK_EXPR_BODY (main_block) = compound;
8891 }
8892 }
8893 }
8894
8895 /* Browse constructors in the super class, searching for a constructor
8896 that doesn't take any argument. Return 0 if one is found, 1
8897 otherwise. If the current class is an anonymous inner class, look
8898 for something that has the same signature. */
8899
8900 static int
8901 verify_constructor_super (mdecl)
8902 tree mdecl;
8903 {
8904 tree class = CLASSTYPE_SUPER (current_class);
8905 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8906 tree sdecl;
8907
8908 if (!class)
8909 return 0;
8910
8911 if (ANONYMOUS_CLASS_P (current_class))
8912 {
8913 tree mdecl_arg_type;
8914 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8915 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8916 if (DECL_CONSTRUCTOR_P (sdecl))
8917 {
8918 tree m_arg_type;
8919 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8920 if (super_inner)
8921 arg_type = TREE_CHAIN (arg_type);
8922 for (m_arg_type = mdecl_arg_type;
8923 (arg_type != end_params_node
8924 && m_arg_type != end_params_node);
8925 arg_type = TREE_CHAIN (arg_type),
8926 m_arg_type = TREE_CHAIN (m_arg_type))
8927 if (!valid_method_invocation_conversion_p
8928 (TREE_VALUE (arg_type),
8929 TREE_VALUE (m_arg_type)))
8930 break;
8931
8932 if (arg_type == end_params_node && m_arg_type == end_params_node)
8933 return 0;
8934 }
8935 }
8936 else
8937 {
8938 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8939 {
8940 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8941 if (super_inner)
8942 arg = TREE_CHAIN (arg);
8943 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8944 return 0;
8945 }
8946 }
8947 return 1;
8948 }
8949
8950 /* Generate code for all context remembered for code generation. */
8951
8952 void
8953 java_expand_classes ()
8954 {
8955 int save_error_count = 0;
8956 static struct parser_ctxt *cur_ctxp = NULL;
8957
8958 java_parse_abort_on_error ();
8959 if (!(ctxp = ctxp_for_generation))
8960 return;
8961 java_layout_classes ();
8962 java_parse_abort_on_error ();
8963
8964 cur_ctxp = ctxp_for_generation;
8965 for (; cur_ctxp; cur_ctxp = cur_ctxp->next)
8966 {
8967 ctxp = cur_ctxp;
8968 input_filename = ctxp->filename;
8969 lang_init_source (2); /* Error msgs have method prototypes */
8970 java_complete_expand_classes (); /* Complete and expand classes */
8971 java_parse_abort_on_error ();
8972 }
8973 input_filename = main_input_filename;
8974
8975 /* Find anonymous classes and expand their constructor, now they
8976 have been fixed. */
8977 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8978 {
8979 tree current;
8980 ctxp = cur_ctxp;
8981 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8982 {
8983 current_class = TREE_TYPE (current);
8984 if (ANONYMOUS_CLASS_P (current_class))
8985 {
8986 tree d;
8987 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8988 {
8989 if (DECL_CONSTRUCTOR_P (d))
8990 {
8991 restore_line_number_status (1);
8992 java_complete_expand_method (d);
8993 restore_line_number_status (0);
8994 break; /* We now there are no other ones */
8995 }
8996 }
8997 }
8998 }
8999 }
9000
9001 /* If we've found error at that stage, don't try to generate
9002 anything, unless we're emitting xrefs or checking the syntax only
9003 (but not using -fsyntax-only for the purpose of generating
9004 bytecode. */
9005 if (java_error_count && !flag_emit_xref
9006 && (!flag_syntax_only && !flag_emit_class_files))
9007 return;
9008
9009 /* Now things are stable, go for generation of the class data. */
9010 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9011 {
9012 tree current;
9013 ctxp = cur_ctxp;
9014 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9015 {
9016 current_class = TREE_TYPE (current);
9017 outgoing_cpool = TYPE_CPOOL (current_class);
9018 if (flag_emit_class_files)
9019 write_classfile (current_class);
9020 if (flag_emit_xref)
9021 expand_xref (current_class);
9022 else if (! flag_syntax_only)
9023 {
9024 java_expand_method_bodies (current_class);
9025 finish_class ();
9026 }
9027 }
9028 }
9029 }
9030
9031 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9032 a tree list node containing RIGHT. Fore coming RIGHTs will be
9033 chained to this hook. LOCATION contains the location of the
9034 separating `.' operator. */
9035
9036 static tree
9037 make_qualified_primary (primary, right, location)
9038 tree primary, right;
9039 int location;
9040 {
9041 tree wfl;
9042
9043 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9044 wfl = build_wfl_wrap (primary, location);
9045 else
9046 {
9047 wfl = primary;
9048 /* If wfl wasn't qualified, we build a first anchor */
9049 if (!EXPR_WFL_QUALIFICATION (wfl))
9050 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9051 }
9052
9053 /* And chain them */
9054 EXPR_WFL_LINECOL (right) = location;
9055 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9056 PRIMARY_P (wfl) = 1;
9057 return wfl;
9058 }
9059
9060 /* Simple merge of two name separated by a `.' */
9061
9062 static tree
9063 merge_qualified_name (left, right)
9064 tree left, right;
9065 {
9066 tree node;
9067 if (!left && !right)
9068 return NULL_TREE;
9069
9070 if (!left)
9071 return right;
9072
9073 if (!right)
9074 return left;
9075
9076 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9077 IDENTIFIER_LENGTH (left));
9078 obstack_1grow (&temporary_obstack, '.');
9079 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9080 IDENTIFIER_LENGTH (right));
9081 node = get_identifier (obstack_base (&temporary_obstack));
9082 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9083 QUALIFIED_P (node) = 1;
9084 return node;
9085 }
9086
9087 /* Merge the two parts of a qualified name into LEFT. Set the
9088 location information of the resulting node to LOCATION, usually
9089 inherited from the location information of the `.' operator. */
9090
9091 static tree
9092 make_qualified_name (left, right, location)
9093 tree left, right;
9094 int location;
9095 {
9096 #ifdef USE_COMPONENT_REF
9097 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
9098 EXPR_WFL_LINECOL (node) = location;
9099 return node;
9100 #else
9101 tree left_id = EXPR_WFL_NODE (left);
9102 tree right_id = EXPR_WFL_NODE (right);
9103 tree wfl, merge;
9104
9105 merge = merge_qualified_name (left_id, right_id);
9106
9107 /* Left wasn't qualified and is now qualified */
9108 if (!QUALIFIED_P (left_id))
9109 {
9110 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9111 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9112 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9113 }
9114
9115 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9116 EXPR_WFL_LINECOL (wfl) = location;
9117 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9118
9119 EXPR_WFL_NODE (left) = merge;
9120 return left;
9121 #endif
9122 }
9123
9124 /* Extract the last identifier component of the qualified in WFL. The
9125 last identifier is removed from the linked list */
9126
9127 static tree
9128 cut_identifier_in_qualified (wfl)
9129 tree wfl;
9130 {
9131 tree q;
9132 tree previous = NULL_TREE;
9133 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9134 if (!TREE_CHAIN (q))
9135 {
9136 if (!previous)
9137 /* Operating on a non qualified qualified WFL. */
9138 abort ();
9139
9140 TREE_CHAIN (previous) = NULL_TREE;
9141 return TREE_PURPOSE (q);
9142 }
9143 }
9144
9145 /* Resolve the expression name NAME. Return its decl. */
9146
9147 static tree
9148 resolve_expression_name (id, orig)
9149 tree id;
9150 tree *orig;
9151 {
9152 tree name = EXPR_WFL_NODE (id);
9153 tree decl;
9154
9155 /* 6.5.5.1: Simple expression names */
9156 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9157 {
9158 /* 15.13.1: NAME can appear within the scope of a local variable
9159 declaration */
9160 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9161 return decl;
9162
9163 /* 15.13.1: NAME can appear within a class declaration */
9164 else
9165 {
9166 decl = lookup_field_wrapper (current_class, name);
9167 if (decl)
9168 {
9169 tree access = NULL_TREE;
9170 int fs = FIELD_STATIC (decl);
9171
9172 /* If we're accessing an outer scope local alias, make
9173 sure we change the name of the field we're going to
9174 build access to. */
9175 if (FIELD_LOCAL_ALIAS_USED (decl))
9176 name = DECL_NAME (decl);
9177
9178 /* Instance variable (8.3.1.1) can't appear within
9179 static method, static initializer or initializer for
9180 a static variable. */
9181 if (!fs && METHOD_STATIC (current_function_decl))
9182 {
9183 static_ref_err (id, name, current_class);
9184 return error_mark_node;
9185 }
9186 /* Instance variables can't appear as an argument of
9187 an explicit constructor invocation */
9188 if (!fs && ctxp->explicit_constructor_p
9189 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9190 {
9191 parse_error_context
9192 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9193 return error_mark_node;
9194 }
9195
9196 /* If we're processing an inner class and we're trying
9197 to access a field belonging to an outer class, build
9198 the access to the field */
9199 if (!fs && outer_field_access_p (current_class, decl))
9200 {
9201 if (CLASS_STATIC (TYPE_NAME (current_class)))
9202 {
9203 static_ref_err (id, DECL_NAME (decl), current_class);
9204 return error_mark_node;
9205 }
9206 access = build_outer_field_access (id, decl);
9207 if (orig)
9208 *orig = access;
9209 return access;
9210 }
9211
9212 /* Otherwise build what it takes to access the field */
9213 access = build_field_ref ((fs ? NULL_TREE : current_this),
9214 DECL_CONTEXT (decl), name);
9215 if (fs)
9216 access = maybe_build_class_init_for_field (decl, access);
9217 /* We may be asked to save the real field access node */
9218 if (orig)
9219 *orig = access;
9220 /* And we return what we got */
9221 return access;
9222 }
9223 /* Fall down to error report on undefined variable */
9224 }
9225 }
9226 /* 6.5.5.2 Qualified Expression Names */
9227 else
9228 {
9229 if (orig)
9230 *orig = NULL_TREE;
9231 qualify_ambiguous_name (id);
9232 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9233 /* 15.10.2: Accessing Superclass Members using super */
9234 return resolve_field_access (id, orig, NULL);
9235 }
9236
9237 /* We've got an error here */
9238 if (INNER_CLASS_TYPE_P (current_class))
9239 parse_error_context (id,
9240 "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9241 IDENTIFIER_POINTER (name),
9242 IDENTIFIER_POINTER (DECL_NAME
9243 (TYPE_NAME (current_class))));
9244 else
9245 parse_error_context (id, "Undefined variable `%s'",
9246 IDENTIFIER_POINTER (name));
9247
9248 return error_mark_node;
9249 }
9250
9251 static void
9252 static_ref_err (wfl, field_id, class_type)
9253 tree wfl, field_id, class_type;
9254 {
9255 parse_error_context
9256 (wfl,
9257 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9258 IDENTIFIER_POINTER (field_id),
9259 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9260 }
9261
9262 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9263 We return something suitable to generate the field access. We also
9264 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9265 recipient's address can be null. */
9266
9267 static tree
9268 resolve_field_access (qual_wfl, field_decl, field_type)
9269 tree qual_wfl;
9270 tree *field_decl, *field_type;
9271 {
9272 int is_static = 0;
9273 tree field_ref;
9274 tree decl, where_found, type_found;
9275
9276 if (resolve_qualified_expression_name (qual_wfl, &decl,
9277 &where_found, &type_found))
9278 return error_mark_node;
9279
9280 /* Resolve the LENGTH field of an array here */
9281 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9282 && type_found && TYPE_ARRAY_P (type_found)
9283 && ! flag_emit_class_files && ! flag_emit_xref)
9284 {
9285 tree length = build_java_array_length_access (where_found);
9286 field_ref = length;
9287
9288 /* In case we're dealing with a static array, we need to
9289 initialize its class before the array length can be fetched.
9290 It's also a good time to create a DECL_RTL for the field if
9291 none already exists, otherwise if the field was declared in a
9292 class found in an external file and hasn't been (and won't
9293 be) accessed for its value, none will be created. */
9294 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9295 {
9296 build_static_field_ref (where_found);
9297 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9298 }
9299 }
9300 /* We might have been trying to resolve field.method(). In which
9301 case, the resolution is over and decl is the answer */
9302 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9303 field_ref = decl;
9304 else if (JDECL_P (decl))
9305 {
9306 if (!type_found)
9307 type_found = DECL_CONTEXT (decl);
9308 is_static = FIELD_STATIC (decl);
9309 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9310 NULL_TREE : where_found),
9311 type_found, DECL_NAME (decl));
9312 if (field_ref == error_mark_node)
9313 return error_mark_node;
9314 if (is_static)
9315 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9316 }
9317 else
9318 field_ref = decl;
9319
9320 if (field_decl)
9321 *field_decl = decl;
9322 if (field_type)
9323 *field_type = (QUAL_DECL_TYPE (decl) ?
9324 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9325 return field_ref;
9326 }
9327
9328 /* If NODE is an access to f static field, strip out the class
9329 initialization part and return the field decl, otherwise, return
9330 NODE. */
9331
9332 static tree
9333 strip_out_static_field_access_decl (node)
9334 tree node;
9335 {
9336 if (TREE_CODE (node) == COMPOUND_EXPR)
9337 {
9338 tree op1 = TREE_OPERAND (node, 1);
9339 if (TREE_CODE (op1) == COMPOUND_EXPR)
9340 {
9341 tree call = TREE_OPERAND (op1, 0);
9342 if (TREE_CODE (call) == CALL_EXPR
9343 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9344 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9345 == soft_initclass_node)
9346 return TREE_OPERAND (op1, 1);
9347 }
9348 else if (JDECL_P (op1))
9349 return op1;
9350 }
9351 return node;
9352 }
9353
9354 /* 6.5.5.2: Qualified Expression Names */
9355
9356 static int
9357 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9358 tree wfl;
9359 tree *found_decl, *type_found, *where_found;
9360 {
9361 int from_type = 0; /* Field search initiated from a type */
9362 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9363 int previous_call_static = 0;
9364 int is_static;
9365 tree decl = NULL_TREE, type = NULL_TREE, q;
9366 /* For certain for of inner class instantiation */
9367 tree saved_current, saved_this;
9368 #define RESTORE_THIS_AND_CURRENT_CLASS \
9369 { current_class = saved_current; current_this = saved_this;}
9370
9371 *type_found = *where_found = NULL_TREE;
9372
9373 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9374 {
9375 tree qual_wfl = QUAL_WFL (q);
9376 tree ret_decl; /* for EH checking */
9377 int location; /* for EH checking */
9378
9379 /* 15.10.1 Field Access Using a Primary */
9380 switch (TREE_CODE (qual_wfl))
9381 {
9382 case CALL_EXPR:
9383 case NEW_CLASS_EXPR:
9384 /* If the access to the function call is a non static field,
9385 build the code to access it. */
9386 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9387 {
9388 decl = maybe_access_field (decl, *where_found,
9389 DECL_CONTEXT (decl));
9390 if (decl == error_mark_node)
9391 return 1;
9392 }
9393
9394 /* And code for the function call */
9395 if (complete_function_arguments (qual_wfl))
9396 return 1;
9397
9398 /* We might have to setup a new current class and a new this
9399 for the search of an inner class, relative to the type of
9400 a expression resolved as `decl'. The current values are
9401 saved and restored shortly after */
9402 saved_current = current_class;
9403 saved_this = current_this;
9404 if (decl
9405 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9406 || from_qualified_this))
9407 {
9408 /* If we still have `from_qualified_this', we have the form
9409 <T>.this.f() and we need to build <T>.this */
9410 if (from_qualified_this)
9411 {
9412 decl = build_access_to_thisn (current_class, type, 0);
9413 decl = java_complete_tree (decl);
9414 type = TREE_TYPE (TREE_TYPE (decl));
9415 }
9416 current_class = type;
9417 current_this = decl;
9418 from_qualified_this = 0;
9419 }
9420
9421 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9422 CALL_USING_SUPER (qual_wfl) = 1;
9423 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9424 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9425 *where_found = patch_method_invocation (qual_wfl, decl, type,
9426 from_super,
9427 &is_static, &ret_decl);
9428 if (*where_found == error_mark_node)
9429 {
9430 RESTORE_THIS_AND_CURRENT_CLASS;
9431 return 1;
9432 }
9433 *type_found = type = QUAL_DECL_TYPE (*where_found);
9434
9435 /* If we're creating an inner class instance, check for that
9436 an enclosing instance is in scope */
9437 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9438 && INNER_ENCLOSING_SCOPE_CHECK (type))
9439 {
9440 parse_error_context
9441 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9442 lang_printable_name (type, 0),
9443 (!current_this ? "" :
9444 "; an explicit one must be provided when creating this inner class"));
9445 RESTORE_THIS_AND_CURRENT_CLASS;
9446 return 1;
9447 }
9448
9449 /* In case we had to change then to resolve a inner class
9450 instantiation using a primary qualified by a `new' */
9451 RESTORE_THIS_AND_CURRENT_CLASS;
9452
9453 /* EH check. No check on access$<n> functions */
9454 if (location
9455 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9456 (DECL_NAME (current_function_decl)))
9457 check_thrown_exceptions (location, ret_decl);
9458
9459 /* If the previous call was static and this one is too,
9460 build a compound expression to hold the two (because in
9461 that case, previous function calls aren't transported as
9462 forcoming function's argument. */
9463 if (previous_call_static && is_static)
9464 {
9465 decl = build (COMPOUND_EXPR, TREE_TYPE (*where_found),
9466 decl, *where_found);
9467 TREE_SIDE_EFFECTS (decl) = 1;
9468 }
9469 else
9470 {
9471 previous_call_static = is_static;
9472 decl = *where_found;
9473 }
9474 from_type = 0;
9475 continue;
9476
9477 case NEW_ARRAY_EXPR:
9478 case NEW_ANONYMOUS_ARRAY_EXPR:
9479 *where_found = decl = java_complete_tree (qual_wfl);
9480 if (decl == error_mark_node)
9481 return 1;
9482 *type_found = type = QUAL_DECL_TYPE (decl);
9483 continue;
9484
9485 case CONVERT_EXPR:
9486 *where_found = decl = java_complete_tree (qual_wfl);
9487 if (decl == error_mark_node)
9488 return 1;
9489 *type_found = type = QUAL_DECL_TYPE (decl);
9490 from_cast = 1;
9491 continue;
9492
9493 case CONDITIONAL_EXPR:
9494 case STRING_CST:
9495 case MODIFY_EXPR:
9496 *where_found = decl = java_complete_tree (qual_wfl);
9497 if (decl == error_mark_node)
9498 return 1;
9499 *type_found = type = QUAL_DECL_TYPE (decl);
9500 continue;
9501
9502 case ARRAY_REF:
9503 /* If the access to the function call is a non static field,
9504 build the code to access it. */
9505 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9506 {
9507 decl = maybe_access_field (decl, *where_found, type);
9508 if (decl == error_mark_node)
9509 return 1;
9510 }
9511 /* And code for the array reference expression */
9512 decl = java_complete_tree (qual_wfl);
9513 if (decl == error_mark_node)
9514 return 1;
9515 type = QUAL_DECL_TYPE (decl);
9516 continue;
9517
9518 case PLUS_EXPR:
9519 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9520 return 1;
9521 if ((type = patch_string (decl)))
9522 decl = type;
9523 *where_found = QUAL_RESOLUTION (q) = decl;
9524 *type_found = type = TREE_TYPE (decl);
9525 break;
9526
9527 case CLASS_LITERAL:
9528 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9529 return 1;
9530 *where_found = QUAL_RESOLUTION (q) = decl;
9531 *type_found = type = TREE_TYPE (decl);
9532 break;
9533
9534 default:
9535 /* Fix for -Wall Just go to the next statement. Don't
9536 continue */
9537 break;
9538 }
9539
9540 /* If we fall here, we weren't processing a (static) function call. */
9541 previous_call_static = 0;
9542
9543 /* It can be the keyword THIS */
9544 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9545 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9546 {
9547 if (!current_this)
9548 {
9549 parse_error_context
9550 (wfl, "Keyword `this' used outside allowed context");
9551 return 1;
9552 }
9553 if (ctxp->explicit_constructor_p
9554 && type == current_class)
9555 {
9556 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9557 return 1;
9558 }
9559 /* We have to generate code for intermediate access */
9560 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9561 {
9562 *where_found = decl = current_this;
9563 *type_found = type = QUAL_DECL_TYPE (decl);
9564 }
9565 /* We're trying to access the this from somewhere else. Make sure
9566 it's allowed before doing so. */
9567 else
9568 {
9569 if (!enclosing_context_p (type, current_class))
9570 {
9571 char *p = xstrdup (lang_printable_name (type, 0));
9572 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9573 p, p,
9574 lang_printable_name (current_class, 0));
9575 free (p);
9576 return 1;
9577 }
9578 from_qualified_this = 1;
9579 /* If there's nothing else after that, we need to
9580 produce something now, otherwise, the section of the
9581 code that needs to produce <T>.this will generate
9582 what is necessary. */
9583 if (!TREE_CHAIN (q))
9584 {
9585 decl = build_access_to_thisn (current_class, type, 0);
9586 *where_found = decl = java_complete_tree (decl);
9587 *type_found = type = TREE_TYPE (decl);
9588 }
9589 }
9590
9591 from_type = 0;
9592 continue;
9593 }
9594
9595 /* 15.10.2 Accessing Superclass Members using SUPER */
9596 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9597 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9598 {
9599 tree node;
9600 /* Check on the restricted use of SUPER */
9601 if (METHOD_STATIC (current_function_decl)
9602 || current_class == object_type_node)
9603 {
9604 parse_error_context
9605 (wfl, "Keyword `super' used outside allowed context");
9606 return 1;
9607 }
9608 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9609 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9610 CLASSTYPE_SUPER (current_class),
9611 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9612 *where_found = decl = java_complete_tree (node);
9613 if (decl == error_mark_node)
9614 return 1;
9615 *type_found = type = QUAL_DECL_TYPE (decl);
9616 from_super = from_type = 1;
9617 continue;
9618 }
9619
9620 /* 15.13.1: Can't search for field name in packages, so we
9621 assume a variable/class name was meant. */
9622 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9623 {
9624 tree name = resolve_package (wfl, &q);
9625 if (name)
9626 {
9627 tree list;
9628 *where_found = decl = resolve_no_layout (name, qual_wfl);
9629 /* We want to be absolutely sure that the class is laid
9630 out. We're going to search something inside it. */
9631 *type_found = type = TREE_TYPE (decl);
9632 layout_class (type);
9633 from_type = 1;
9634
9635 /* Fix them all the way down, if any are left. */
9636 if (q)
9637 {
9638 list = TREE_CHAIN (q);
9639 while (list)
9640 {
9641 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9642 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9643 list = TREE_CHAIN (list);
9644 }
9645 }
9646 }
9647 else
9648 {
9649 if (from_super || from_cast)
9650 parse_error_context
9651 ((from_cast ? qual_wfl : wfl),
9652 "No variable `%s' defined in class `%s'",
9653 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9654 lang_printable_name (type, 0));
9655 else
9656 parse_error_context
9657 (qual_wfl, "Undefined variable or class name: `%s'",
9658 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9659 return 1;
9660 }
9661 }
9662
9663 /* We have a type name. It's been already resolved when the
9664 expression was qualified. */
9665 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9666 {
9667 decl = QUAL_RESOLUTION (q);
9668
9669 /* Sneak preview. If next we see a `new', we're facing a
9670 qualification with resulted in a type being selected
9671 instead of a field. Report the error */
9672 if(TREE_CHAIN (q)
9673 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9674 {
9675 parse_error_context (qual_wfl, "Undefined variable `%s'",
9676 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9677 return 1;
9678 }
9679
9680 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9681 {
9682 parse_error_context
9683 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9684 java_accstring_lookup (get_access_flags_from_decl (decl)),
9685 GET_TYPE_NAME (type),
9686 IDENTIFIER_POINTER (DECL_NAME (decl)),
9687 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9688 return 1;
9689 }
9690 check_deprecation (qual_wfl, decl);
9691
9692 type = TREE_TYPE (decl);
9693 from_type = 1;
9694 }
9695 /* We resolve an expression name */
9696 else
9697 {
9698 tree field_decl = NULL_TREE;
9699
9700 /* If there exists an early resolution, use it. That occurs
9701 only once and we know that there are more things to
9702 come. Don't do that when processing something after SUPER
9703 (we need more thing to be put in place below */
9704 if (!from_super && QUAL_RESOLUTION (q))
9705 {
9706 decl = QUAL_RESOLUTION (q);
9707 if (!type)
9708 {
9709 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9710 {
9711 if (current_this)
9712 *where_found = current_this;
9713 else
9714 {
9715 static_ref_err (qual_wfl, DECL_NAME (decl),
9716 current_class);
9717 return 1;
9718 }
9719 if (outer_field_access_p (current_class, decl))
9720 decl = build_outer_field_access (qual_wfl, decl);
9721 }
9722 else
9723 {
9724 *where_found = TREE_TYPE (decl);
9725 if (TREE_CODE (*where_found) == POINTER_TYPE)
9726 *where_found = TREE_TYPE (*where_found);
9727 }
9728 }
9729 }
9730
9731 /* Report and error if we're using a numerical litteral as a
9732 qualifier. It can only be an INTEGER_CST. */
9733 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
9734 {
9735 parse_error_context
9736 (wfl, "Can't use type `%s' as a qualifier",
9737 lang_printable_name (TREE_TYPE (qual_wfl), 0));
9738 return 1;
9739 }
9740
9741 /* We have to search for a field, knowing the type of its
9742 container. The flag FROM_TYPE indicates that we resolved
9743 the last member of the expression as a type name, which
9744 means that for the resolution of this field, we'll look
9745 for other errors than if it was resolved as a member of
9746 an other field. */
9747 else
9748 {
9749 int is_static;
9750 tree field_decl_type; /* For layout */
9751
9752 if (!from_type && !JREFERENCE_TYPE_P (type))
9753 {
9754 parse_error_context
9755 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9756 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9757 lang_printable_name (type, 0),
9758 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9759 return 1;
9760 }
9761
9762 field_decl = lookup_field_wrapper (type,
9763 EXPR_WFL_NODE (qual_wfl));
9764
9765 /* Maybe what we're trying to access to is an inner
9766 class, only if decl is a TYPE_DECL. */
9767 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9768 {
9769 tree ptr, inner_decl;
9770
9771 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9772 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9773 if (inner_decl)
9774 {
9775 check_inner_class_access (inner_decl, decl, qual_wfl);
9776 type = TREE_TYPE (inner_decl);
9777 decl = inner_decl;
9778 from_type = 1;
9779 continue;
9780 }
9781 }
9782
9783 if (field_decl == NULL_TREE)
9784 {
9785 parse_error_context
9786 (qual_wfl, "No variable `%s' defined in type `%s'",
9787 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9788 GET_TYPE_NAME (type));
9789 return 1;
9790 }
9791 if (field_decl == error_mark_node)
9792 return 1;
9793
9794 /* Layout the type of field_decl, since we may need
9795 it. Don't do primitive types or loaded classes. The
9796 situation of non primitive arrays may not handled
9797 properly here. FIXME */
9798 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9799 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9800 else
9801 field_decl_type = TREE_TYPE (field_decl);
9802 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9803 && !CLASS_LOADED_P (field_decl_type)
9804 && !TYPE_ARRAY_P (field_decl_type))
9805 resolve_and_layout (field_decl_type, NULL_TREE);
9806
9807 /* Check on accessibility here */
9808 if (not_accessible_p (current_class, field_decl,
9809 DECL_CONTEXT (field_decl), from_super))
9810 {
9811 parse_error_context
9812 (qual_wfl,
9813 "Can't access %s field `%s.%s' from `%s'",
9814 java_accstring_lookup
9815 (get_access_flags_from_decl (field_decl)),
9816 GET_TYPE_NAME (type),
9817 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9818 IDENTIFIER_POINTER
9819 (DECL_NAME (TYPE_NAME (current_class))));
9820 return 1;
9821 }
9822 check_deprecation (qual_wfl, field_decl);
9823
9824 /* There are things to check when fields are accessed
9825 from type. There are no restrictions on a static
9826 declaration of the field when it is accessed from an
9827 interface */
9828 is_static = FIELD_STATIC (field_decl);
9829 if (!from_super && from_type
9830 && !TYPE_INTERFACE_P (type)
9831 && !is_static
9832 && (current_function_decl
9833 && METHOD_STATIC (current_function_decl)))
9834 {
9835 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9836 return 1;
9837 }
9838 from_cast = from_super = 0;
9839
9840 /* It's an access from a type but it isn't static, we
9841 make it relative to `this'. */
9842 if (!is_static && from_type)
9843 decl = current_this;
9844
9845 /* If we need to generate something to get a proper
9846 handle on what this field is accessed from, do it
9847 now. */
9848 if (!is_static)
9849 {
9850 decl = maybe_access_field (decl, *where_found, *type_found);
9851 if (decl == error_mark_node)
9852 return 1;
9853 }
9854
9855 /* We want to keep the location were found it, and the type
9856 we found. */
9857 *where_found = decl;
9858 *type_found = type;
9859
9860 /* Generate the correct expression for field access from
9861 qualified this */
9862 if (from_qualified_this)
9863 {
9864 field_decl = build_outer_field_access (qual_wfl, field_decl);
9865 from_qualified_this = 0;
9866 }
9867
9868 /* This is the decl found and eventually the next one to
9869 search from */
9870 decl = field_decl;
9871 }
9872 from_type = 0;
9873 type = QUAL_DECL_TYPE (decl);
9874
9875 /* Sneak preview. If decl is qualified by a `new', report
9876 the error here to be accurate on the peculiar construct */
9877 if (TREE_CHAIN (q)
9878 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9879 && !JREFERENCE_TYPE_P (type))
9880 {
9881 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9882 lang_printable_name (type, 0));
9883 return 1;
9884 }
9885 }
9886 /* `q' might have changed due to a after package resolution
9887 re-qualification */
9888 if (!q)
9889 break;
9890 }
9891 *found_decl = decl;
9892 return 0;
9893 }
9894
9895 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9896 can't be accessed from REFERENCE (a record type). If MEMBER
9897 features a protected access, we then use WHERE which, if non null,
9898 holds the type of MEMBER's access that is checked against
9899 6.6.2.1. This function should be used when decl is a field or a
9900 method. */
9901
9902 static int
9903 not_accessible_p (reference, member, where, from_super)
9904 tree reference, member;
9905 tree where;
9906 int from_super;
9907 {
9908 int access_flag = get_access_flags_from_decl (member);
9909
9910 /* Inner classes are processed by check_inner_class_access */
9911 if (INNER_CLASS_TYPE_P (reference))
9912 return 0;
9913
9914 /* Access always granted for members declared public */
9915 if (access_flag & ACC_PUBLIC)
9916 return 0;
9917
9918 /* Check access on protected members */
9919 if (access_flag & ACC_PROTECTED)
9920 {
9921 /* Access granted if it occurs from within the package
9922 containing the class in which the protected member is
9923 declared */
9924 if (class_in_current_package (DECL_CONTEXT (member)))
9925 return 0;
9926
9927 /* If accessed with the form `super.member', then access is granted */
9928 if (from_super)
9929 return 0;
9930
9931 /* If where is active, access was made through a
9932 qualifier. Access is granted if the type of the qualifier is
9933 or is a sublass of the type the access made from (6.6.2.1.) */
9934 if (where && !inherits_from_p (reference, where))
9935 return 1;
9936
9937 /* Otherwise, access is granted if occurring from the class where
9938 member is declared or a subclass of it. Find the right
9939 context to perform the check */
9940 if (PURE_INNER_CLASS_TYPE_P (reference))
9941 {
9942 while (INNER_CLASS_TYPE_P (reference))
9943 {
9944 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9945 return 0;
9946 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9947 }
9948 }
9949 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9950 return 0;
9951 return 1;
9952 }
9953
9954 /* Check access on private members. Access is granted only if it
9955 occurs from within the class in which it is declared -- that does
9956 it for innerclasses too. */
9957 if (access_flag & ACC_PRIVATE)
9958 {
9959 if (reference == DECL_CONTEXT (member))
9960 return 0;
9961 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9962 return 0;
9963 return 1;
9964 }
9965
9966 /* Default access are permitted only when occurring within the
9967 package in which the type (REFERENCE) is declared. In other words,
9968 REFERENCE is defined in the current package */
9969 if (ctxp->package)
9970 return !class_in_current_package (reference);
9971
9972 /* Otherwise, access is granted */
9973 return 0;
9974 }
9975
9976 /* Test deprecated decl access. */
9977 static void
9978 check_deprecation (wfl, decl)
9979 tree wfl, decl;
9980 {
9981 const char *file = DECL_SOURCE_FILE (decl);
9982 /* Complain if the field is deprecated and the file it was defined
9983 in isn't compiled at the same time the file which contains its
9984 use is */
9985 if (DECL_DEPRECATED (decl)
9986 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9987 {
9988 char the [20];
9989 switch (TREE_CODE (decl))
9990 {
9991 case FUNCTION_DECL:
9992 strcpy (the, "method");
9993 break;
9994 case FIELD_DECL:
9995 case VAR_DECL:
9996 strcpy (the, "field");
9997 break;
9998 case TYPE_DECL:
9999 parse_warning_context (wfl, "The class `%s' has been deprecated",
10000 IDENTIFIER_POINTER (DECL_NAME (decl)));
10001 return;
10002 default:
10003 abort ();
10004 }
10005 /* Don't issue a message if the context as been deprecated as a
10006 whole. */
10007 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10008 parse_warning_context
10009 (wfl, "The %s `%s' in class `%s' has been deprecated",
10010 the, lang_printable_name (decl, 0),
10011 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10012 }
10013 }
10014
10015 /* Returns 1 if class was declared in the current package, 0 otherwise */
10016
10017 static int
10018 class_in_current_package (class)
10019 tree class;
10020 {
10021 static tree cache = NULL_TREE;
10022 int qualified_flag;
10023 tree left;
10024
10025 if (cache == class)
10026 return 1;
10027
10028 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10029
10030 /* If the current package is empty and the name of CLASS is
10031 qualified, class isn't in the current package. If there is a
10032 current package and the name of the CLASS is not qualified, class
10033 isn't in the current package */
10034 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10035 return 0;
10036
10037 /* If there is not package and the name of CLASS isn't qualified,
10038 they belong to the same unnamed package */
10039 if (!ctxp->package && !qualified_flag)
10040 return 1;
10041
10042 /* Compare the left part of the name of CLASS with the package name */
10043 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10044 if (ctxp->package == left)
10045 {
10046 static int initialized_p;
10047 /* Register CACHE with the garbage collector. */
10048 if (!initialized_p)
10049 {
10050 ggc_add_tree_root (&cache, 1);
10051 initialized_p = 1;
10052 }
10053
10054 cache = class;
10055 return 1;
10056 }
10057 return 0;
10058 }
10059
10060 /* This function may generate code to access DECL from WHERE. This is
10061 done only if certain conditions meet. */
10062
10063 static tree
10064 maybe_access_field (decl, where, type)
10065 tree decl, where, type;
10066 {
10067 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10068 && !FIELD_STATIC (decl))
10069 decl = build_field_ref (where ? where : current_this,
10070 (type ? type : DECL_CONTEXT (decl)),
10071 DECL_NAME (decl));
10072 return decl;
10073 }
10074
10075 /* Build a method invocation, by patching PATCH. If non NULL
10076 and according to the situation, PRIMARY and WHERE may be
10077 used. IS_STATIC is set to 1 if the invoked function is static. */
10078
10079 static tree
10080 patch_method_invocation (patch, primary, where, from_super,
10081 is_static, ret_decl)
10082 tree patch, primary, where;
10083 int from_super;
10084 int *is_static;
10085 tree *ret_decl;
10086 {
10087 tree wfl = TREE_OPERAND (patch, 0);
10088 tree args = TREE_OPERAND (patch, 1);
10089 tree name = EXPR_WFL_NODE (wfl);
10090 tree list;
10091 int is_static_flag = 0;
10092 int is_super_init = 0;
10093 tree this_arg = NULL_TREE;
10094 int is_array_clone_call = 0;
10095
10096 /* Should be overriden if everything goes well. Otherwise, if
10097 something fails, it should keep this value. It stop the
10098 evaluation of a bogus assignment. See java_complete_tree,
10099 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10100 evaluating an assignment */
10101 TREE_TYPE (patch) = error_mark_node;
10102
10103 /* Since lookup functions are messing with line numbers, save the
10104 context now. */
10105 java_parser_context_save_global ();
10106
10107 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10108
10109 /* Resolution of qualified name, excluding constructors */
10110 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10111 {
10112 tree identifier, identifier_wfl, type, resolved;
10113 /* Extract the last IDENTIFIER of the qualified
10114 expression. This is a wfl and we will use it's location
10115 data during error report. */
10116 identifier_wfl = cut_identifier_in_qualified (wfl);
10117 identifier = EXPR_WFL_NODE (identifier_wfl);
10118
10119 /* Given the context, IDENTIFIER is syntactically qualified
10120 as a MethodName. We need to qualify what's before */
10121 qualify_ambiguous_name (wfl);
10122 resolved = resolve_field_access (wfl, NULL, NULL);
10123
10124 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10125 && FIELD_FINAL (resolved)
10126 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10127 && !flag_emit_class_files && !flag_emit_xref)
10128 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10129
10130 if (resolved == error_mark_node)
10131 PATCH_METHOD_RETURN_ERROR ();
10132
10133 type = GET_SKIP_TYPE (resolved);
10134 resolve_and_layout (type, NULL_TREE);
10135
10136 if (JPRIMITIVE_TYPE_P (type))
10137 {
10138 parse_error_context
10139 (identifier_wfl,
10140 "Can't invoke a method on primitive type `%s'",
10141 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10142 PATCH_METHOD_RETURN_ERROR ();
10143 }
10144
10145 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10146 args = nreverse (args);
10147
10148 /* We're resolving a call from a type */
10149 if (TREE_CODE (resolved) == TYPE_DECL)
10150 {
10151 if (CLASS_INTERFACE (resolved))
10152 {
10153 parse_error_context
10154 (identifier_wfl,
10155 "Can't make static reference to method `%s' in interface `%s'",
10156 IDENTIFIER_POINTER (identifier),
10157 IDENTIFIER_POINTER (name));
10158 PATCH_METHOD_RETURN_ERROR ();
10159 }
10160 if (list && !METHOD_STATIC (list))
10161 {
10162 char *fct_name = xstrdup (lang_printable_name (list, 0));
10163 parse_error_context
10164 (identifier_wfl,
10165 "Can't make static reference to method `%s %s' in class `%s'",
10166 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10167 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10168 free (fct_name);
10169 PATCH_METHOD_RETURN_ERROR ();
10170 }
10171 }
10172 else
10173 this_arg = primary = resolved;
10174
10175 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10176 is_array_clone_call = 1;
10177
10178 /* IDENTIFIER_WFL will be used to report any problem further */
10179 wfl = identifier_wfl;
10180 }
10181 /* Resolution of simple names, names generated after a primary: or
10182 constructors */
10183 else
10184 {
10185 tree class_to_search = NULL_TREE;
10186 int lc; /* Looking for Constructor */
10187
10188 /* We search constructor in their target class */
10189 if (CALL_CONSTRUCTOR_P (patch))
10190 {
10191 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10192 class_to_search = EXPR_WFL_NODE (wfl);
10193 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10194 this_identifier_node)
10195 class_to_search = NULL_TREE;
10196 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10197 super_identifier_node)
10198 {
10199 is_super_init = 1;
10200 if (CLASSTYPE_SUPER (current_class))
10201 class_to_search =
10202 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10203 else
10204 {
10205 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10206 PATCH_METHOD_RETURN_ERROR ();
10207 }
10208 }
10209
10210 /* Class to search is NULL if we're searching the current one */
10211 if (class_to_search)
10212 {
10213 class_to_search = resolve_and_layout (class_to_search, wfl);
10214
10215 if (!class_to_search)
10216 {
10217 parse_error_context
10218 (wfl, "Class `%s' not found in type declaration",
10219 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10220 PATCH_METHOD_RETURN_ERROR ();
10221 }
10222
10223 /* Can't instantiate an abstract class, but we can
10224 invoke it's constructor. It's use within the `new'
10225 context is denied here. */
10226 if (CLASS_ABSTRACT (class_to_search)
10227 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10228 {
10229 parse_error_context
10230 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10231 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10232 PATCH_METHOD_RETURN_ERROR ();
10233 }
10234
10235 class_to_search = TREE_TYPE (class_to_search);
10236 }
10237 else
10238 class_to_search = current_class;
10239 lc = 1;
10240 }
10241 /* This is a regular search in the local class, unless an
10242 alternate class is specified. */
10243 else
10244 {
10245 if (where != NULL_TREE)
10246 class_to_search = where;
10247 else if (QUALIFIED_P (name))
10248 class_to_search = current_class;
10249 else
10250 {
10251 class_to_search = current_class;
10252
10253 for (;;)
10254 {
10255 if (has_method (class_to_search, name))
10256 break;
10257 if (! INNER_CLASS_TYPE_P (class_to_search))
10258 {
10259 parse_error_context (wfl,
10260 "No method named `%s' in scope",
10261 IDENTIFIER_POINTER (name));
10262 PATCH_METHOD_RETURN_ERROR ();
10263 }
10264 class_to_search
10265 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10266 }
10267 }
10268 lc = 0;
10269 }
10270
10271 /* NAME is a simple identifier or comes from a primary. Search
10272 in the class whose declaration contain the method being
10273 invoked. */
10274 resolve_and_layout (class_to_search, NULL_TREE);
10275
10276 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10277 /* Don't continue if no method were found, as the next statement
10278 can't be executed then. */
10279 if (!list)
10280 PATCH_METHOD_RETURN_ERROR ();
10281
10282 if (TYPE_ARRAY_P (class_to_search)
10283 && DECL_NAME (list) == get_identifier ("clone"))
10284 is_array_clone_call = 1;
10285
10286 /* Check for static reference if non static methods */
10287 if (check_for_static_method_reference (wfl, patch, list,
10288 class_to_search, primary))
10289 PATCH_METHOD_RETURN_ERROR ();
10290
10291 /* Check for inner classes creation from illegal contexts */
10292 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10293 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10294 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10295 && !DECL_INIT_P (current_function_decl))
10296 {
10297 parse_error_context
10298 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10299 lang_printable_name (class_to_search, 0),
10300 (!current_this ? "" :
10301 "; an explicit one must be provided when creating this inner class"));
10302 PATCH_METHOD_RETURN_ERROR ();
10303 }
10304
10305 /* Non static methods are called with the current object extra
10306 argument. If patch a `new TYPE()', the argument is the value
10307 returned by the object allocator. If method is resolved as a
10308 primary, use the primary otherwise use the current THIS. */
10309 args = nreverse (args);
10310 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10311 {
10312 this_arg = primary ? primary : current_this;
10313
10314 /* If we're using an access method, things are different.
10315 There are two familly of cases:
10316
10317 1) We're not generating bytecodes:
10318
10319 - LIST is non static. It's invocation is transformed from
10320 x(a1,...,an) into this$<n>.x(a1,....an).
10321 - LIST is static. It's invocation is transformed from
10322 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10323
10324 2) We're generating bytecodes:
10325
10326 - LIST is non static. It's invocation is transformed from
10327 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10328 - LIST is static. It's invocation is transformed from
10329 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10330
10331 Of course, this$<n> can be abitrary complex, ranging from
10332 this$0 (the immediate outer context) to
10333 access$0(access$0(...(this$0))).
10334
10335 maybe_use_access_method returns a non zero value if the
10336 this_arg has to be moved into the (then generated) stub
10337 argument list. In the meantime, the selected function
10338 might have be replaced by a generated stub. */
10339 if (!primary &&
10340 maybe_use_access_method (is_super_init, &list, &this_arg))
10341 {
10342 args = tree_cons (NULL_TREE, this_arg, args);
10343 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10344 }
10345 }
10346 }
10347
10348 /* Merge point of all resolution schemes. If we have nothing, this
10349 is an error, already signaled */
10350 if (!list)
10351 PATCH_METHOD_RETURN_ERROR ();
10352
10353 /* Check accessibility, position the is_static flag, build and
10354 return the call */
10355 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10356 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10357 NULL_TREE), from_super)
10358 /* Calls to clone() on array types are permitted as a special-case. */
10359 && !is_array_clone_call)
10360 {
10361 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10362 const char *const access =
10363 java_accstring_lookup (get_access_flags_from_decl (list));
10364 const char *const klass =
10365 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10366 const char *const refklass =
10367 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10368 const char *const what = (DECL_CONSTRUCTOR_P (list)
10369 ? "constructor" : "method");
10370 /* FIXME: WFL yields the wrong message here but I don't know
10371 what else to use. */
10372 parse_error_context (wfl,
10373 "Can't access %s %s `%s.%s' from `%s'",
10374 access, what, klass, fct_name, refklass);
10375 PATCH_METHOD_RETURN_ERROR ();
10376 }
10377
10378 /* Deprecation check: check whether the method being invoked or the
10379 instance-being-created's type are deprecated. */
10380 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10381 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10382 else
10383 check_deprecation (wfl, list);
10384
10385 /* If invoking a innerclass constructor, there are hidden parameters
10386 to pass */
10387 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10388 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10389 {
10390 /* And make sure we add the accessed local variables to be saved
10391 in field aliases. */
10392 args = build_alias_initializer_parameter_list
10393 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10394
10395 /* Secretly pass the current_this/primary as a second argument */
10396 if (primary || current_this)
10397 {
10398 tree extra_arg;
10399 tree this_type = (current_this ?
10400 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10401 /* Method's (list) enclosing context */
10402 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10403 /* If we have a primary, use it. */
10404 if (primary)
10405 extra_arg = primary;
10406 /* The current `this' is an inner class but isn't a direct
10407 enclosing context for the inner class we're trying to
10408 create. Build an access to the proper enclosing context
10409 and use it. */
10410 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10411 && this_type != TREE_TYPE (mec))
10412 {
10413
10414 extra_arg = build_access_to_thisn (current_class,
10415 TREE_TYPE (mec), 0);
10416 extra_arg = java_complete_tree (extra_arg);
10417 }
10418 /* Otherwise, just use the current `this' as an enclosing
10419 context. */
10420 else
10421 extra_arg = current_this;
10422 args = tree_cons (NULL_TREE, extra_arg, args);
10423 }
10424 else
10425 args = tree_cons (NULL_TREE, integer_zero_node, args);
10426 }
10427
10428 /* This handles the situation where a constructor invocation needs
10429 to have an enclosing context passed as a second parameter (the
10430 constructor is one of an inner class. We extract it from the
10431 current function. */
10432 if ((is_super_init ||
10433 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10434 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10435 {
10436 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10437 tree extra_arg;
10438
10439 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10440 {
10441 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10442 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10443 }
10444 else
10445 {
10446 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10447 extra_arg =
10448 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10449 extra_arg = java_complete_tree (extra_arg);
10450 }
10451 args = tree_cons (NULL_TREE, extra_arg, args);
10452 }
10453
10454 is_static_flag = METHOD_STATIC (list);
10455 if (! is_static_flag && this_arg != NULL_TREE)
10456 args = tree_cons (NULL_TREE, this_arg, args);
10457
10458 /* In the context of an explicit constructor invocation, we can't
10459 invoke any method relying on `this'. Exceptions are: we're
10460 invoking a static function, primary exists and is not the current
10461 this, we're creating a new object. */
10462 if (ctxp->explicit_constructor_p
10463 && !is_static_flag
10464 && (!primary || primary == current_this)
10465 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10466 {
10467 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10468 PATCH_METHOD_RETURN_ERROR ();
10469 }
10470 java_parser_context_restore_global ();
10471 if (is_static)
10472 *is_static = is_static_flag;
10473 /* Sometimes, we want the decl of the selected method. Such as for
10474 EH checking */
10475 if (ret_decl)
10476 *ret_decl = list;
10477 patch = patch_invoke (patch, list, args);
10478
10479 /* Now is a good time to insert the call to finit$ */
10480 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10481 {
10482 tree finit_parms, finit_call;
10483
10484 /* Prepare to pass hidden parameters to finit$, if any. */
10485 finit_parms = build_alias_initializer_parameter_list
10486 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10487
10488 finit_call =
10489 build_method_invocation (build_wfl_node (finit_identifier_node),
10490 finit_parms);
10491
10492 /* Generate the code used to initialize fields declared with an
10493 initialization statement and build a compound statement along
10494 with the super constructor invocation. */
10495 patch = build (COMPOUND_EXPR, void_type_node, patch,
10496 java_complete_tree (finit_call));
10497 CAN_COMPLETE_NORMALLY (patch) = 1;
10498 }
10499 return patch;
10500 }
10501
10502 /* Check that we're not trying to do a static reference to a method in
10503 non static method. Return 1 if it's the case, 0 otherwise. */
10504
10505 static int
10506 check_for_static_method_reference (wfl, node, method, where, primary)
10507 tree wfl, node, method, where, primary;
10508 {
10509 if (METHOD_STATIC (current_function_decl)
10510 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10511 {
10512 char *fct_name = xstrdup (lang_printable_name (method, 0));
10513 parse_error_context
10514 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10515 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10516 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10517 free (fct_name);
10518 return 1;
10519 }
10520 return 0;
10521 }
10522
10523 /* Fix the invocation of *MDECL if necessary in the case of a
10524 invocation from an inner class. *THIS_ARG might be modified
10525 appropriately and an alternative access to *MDECL might be
10526 returned. */
10527
10528 static int
10529 maybe_use_access_method (is_super_init, mdecl, this_arg)
10530 int is_super_init;
10531 tree *mdecl, *this_arg;
10532 {
10533 tree ctx;
10534 tree md = *mdecl, ta = *this_arg;
10535 int to_return = 0;
10536 int non_static_context = !METHOD_STATIC (md);
10537
10538 if (is_super_init
10539 || DECL_CONTEXT (md) == current_class
10540 || !PURE_INNER_CLASS_TYPE_P (current_class)
10541 || DECL_FINIT_P (md)
10542 || DECL_INSTINIT_P (md))
10543 return 0;
10544
10545 /* If we're calling a method found in an enclosing class, generate
10546 what it takes to retrieve the right this. Don't do that if we're
10547 invoking a static method. Note that if MD's type is unrelated to
10548 CURRENT_CLASS, then the current this can be used. */
10549
10550 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10551 {
10552 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10553 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10554 {
10555 ta = build_current_thisn (current_class);
10556 ta = build_wfl_node (ta);
10557 }
10558 else
10559 {
10560 tree type = ctx;
10561 while (type)
10562 {
10563 maybe_build_thisn_access_method (type);
10564 if (inherits_from_p (type, DECL_CONTEXT (md)))
10565 {
10566 ta = build_access_to_thisn (ctx, type, 0);
10567 break;
10568 }
10569 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10570 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10571 }
10572 }
10573 ta = java_complete_tree (ta);
10574 }
10575
10576 /* We might have to use an access method to get to MD. We can
10577 break the method access rule as far as we're not generating
10578 bytecode */
10579 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10580 {
10581 md = build_outer_method_access_method (md);
10582 to_return = 1;
10583 }
10584
10585 *mdecl = md;
10586 *this_arg = ta;
10587
10588 /* Returnin a non zero value indicates we were doing a non static
10589 method invokation that is now a static invocation. It will have
10590 callee displace `this' to insert it in the regular argument
10591 list. */
10592 return (non_static_context && to_return);
10593 }
10594
10595 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10596 mode. */
10597
10598 static tree
10599 patch_invoke (patch, method, args)
10600 tree patch, method, args;
10601 {
10602 tree dtable, func;
10603 tree original_call, t, ta;
10604 tree check = NULL_TREE;
10605
10606 /* Last step for args: convert build-in types. If we're dealing with
10607 a new TYPE() type call, the first argument to the constructor
10608 isn't found in the incoming argument list, but delivered by
10609 `new' */
10610 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10611 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10612 t = TREE_CHAIN (t);
10613 for (ta = args; t != end_params_node && ta;
10614 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10615 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10616 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10617 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10618
10619 /* Resolve unresolved returned type isses */
10620 t = TREE_TYPE (TREE_TYPE (method));
10621 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10622 resolve_and_layout (TREE_TYPE (t), NULL);
10623
10624 if (flag_emit_class_files || flag_emit_xref)
10625 func = method;
10626 else
10627 {
10628 tree signature = build_java_signature (TREE_TYPE (method));
10629 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10630 {
10631 case INVOKE_VIRTUAL:
10632 dtable = invoke_build_dtable (0, args);
10633 func = build_invokevirtual (dtable, method);
10634 break;
10635
10636 case INVOKE_NONVIRTUAL:
10637 /* If the object for the method call is null, we throw an
10638 exception. We don't do this if the object is the current
10639 method's `this'. In other cases we just rely on an
10640 optimization pass to eliminate redundant checks. */
10641 if (TREE_VALUE (args) != current_this)
10642 {
10643 /* We use a save_expr here to make sure we only evaluate
10644 the new `self' expression once. */
10645 tree save_arg = save_expr (TREE_VALUE (args));
10646 TREE_VALUE (args) = save_arg;
10647 check = java_check_reference (save_arg, 1);
10648 }
10649 /* Fall through. */
10650
10651 case INVOKE_SUPER:
10652 case INVOKE_STATIC:
10653 func = build_known_method_ref (method, TREE_TYPE (method),
10654 DECL_CONTEXT (method),
10655 signature, args);
10656 break;
10657
10658 case INVOKE_INTERFACE:
10659 dtable = invoke_build_dtable (1, args);
10660 func = build_invokeinterface (dtable, method);
10661 break;
10662
10663 default:
10664 abort ();
10665 }
10666
10667 /* Ensure self_type is initialized, (invokestatic). FIXME */
10668 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10669 }
10670
10671 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10672 TREE_OPERAND (patch, 0) = func;
10673 TREE_OPERAND (patch, 1) = args;
10674 original_call = patch;
10675
10676 /* We're processing a `new TYPE ()' form. New is called and its
10677 returned value is the first argument to the constructor. We build
10678 a COMPOUND_EXPR and use saved expression so that the overall NEW
10679 expression value is a pointer to a newly created and initialized
10680 class. */
10681 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10682 {
10683 tree class = DECL_CONTEXT (method);
10684 tree c1, saved_new, size, new;
10685 tree alloc_node;
10686
10687 if (flag_emit_class_files || flag_emit_xref)
10688 {
10689 TREE_TYPE (patch) = build_pointer_type (class);
10690 return patch;
10691 }
10692 if (!TYPE_SIZE (class))
10693 safe_layout_class (class);
10694 size = size_in_bytes (class);
10695 alloc_node =
10696 (class_has_finalize_method (class) ? alloc_object_node
10697 : alloc_no_finalizer_node);
10698 new = build (CALL_EXPR, promote_type (class),
10699 build_address_of (alloc_node),
10700 tree_cons (NULL_TREE, build_class_ref (class),
10701 build_tree_list (NULL_TREE,
10702 size_in_bytes (class))),
10703 NULL_TREE);
10704 saved_new = save_expr (new);
10705 c1 = build_tree_list (NULL_TREE, saved_new);
10706 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10707 TREE_OPERAND (original_call, 1) = c1;
10708 TREE_SET_CODE (original_call, CALL_EXPR);
10709 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10710 }
10711
10712 /* If CHECK is set, then we are building a check to see if the object
10713 is NULL. */
10714 if (check != NULL_TREE)
10715 {
10716 patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check, patch);
10717 TREE_SIDE_EFFECTS (patch) = 1;
10718 }
10719
10720 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10721 put it as the first expression of a COMPOUND_EXPR. The second
10722 expression being an empty statement to be later patched if
10723 necessary. We remember a TREE_LIST (the PURPOSE is the method,
10724 the VALUE is the compound) in a hashtable and return a
10725 COMPOUND_EXPR built so that the result of the evaluation of the
10726 original PATCH node is returned. */
10727 if (STATIC_CLASS_INIT_OPT_P ()
10728 && current_function_decl && METHOD_STATIC (method))
10729 {
10730 tree list;
10731 tree fndecl = current_function_decl;
10732 tree save = save_expr (patch);
10733 tree type = TREE_TYPE (patch);
10734
10735 patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10736 list = tree_cons (method, patch,
10737 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
10738
10739 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
10740
10741 patch = build (COMPOUND_EXPR, type, patch, save);
10742 }
10743
10744 return patch;
10745 }
10746
10747 static int
10748 invocation_mode (method, super)
10749 tree method;
10750 int super;
10751 {
10752 int access = get_access_flags_from_decl (method);
10753
10754 if (super)
10755 return INVOKE_SUPER;
10756
10757 if (access & ACC_STATIC)
10758 return INVOKE_STATIC;
10759
10760 /* We have to look for a constructor before we handle nonvirtual
10761 calls; otherwise the constructor will look nonvirtual. */
10762 if (DECL_CONSTRUCTOR_P (method))
10763 return INVOKE_STATIC;
10764
10765 if (access & ACC_FINAL || access & ACC_PRIVATE)
10766 return INVOKE_NONVIRTUAL;
10767
10768 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10769 return INVOKE_NONVIRTUAL;
10770
10771 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10772 return INVOKE_INTERFACE;
10773
10774 return INVOKE_VIRTUAL;
10775 }
10776
10777 /* Retrieve a refined list of matching methods. It covers the step
10778 15.11.2 (Compile-Time Step 2) */
10779
10780 static tree
10781 lookup_method_invoke (lc, cl, class, name, arg_list)
10782 int lc;
10783 tree cl;
10784 tree class, name, arg_list;
10785 {
10786 tree atl = end_params_node; /* Arg Type List */
10787 tree method, signature, list, node;
10788 const char *candidates; /* Used for error report */
10789 char *dup;
10790
10791 /* Fix the arguments */
10792 for (node = arg_list; node; node = TREE_CHAIN (node))
10793 {
10794 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10795 /* Non primitive type may have to be resolved */
10796 if (!JPRIMITIVE_TYPE_P (current_arg))
10797 resolve_and_layout (current_arg, NULL_TREE);
10798 /* And promoted */
10799 if (TREE_CODE (current_arg) == RECORD_TYPE)
10800 current_arg = promote_type (current_arg);
10801 atl = tree_cons (NULL_TREE, current_arg, atl);
10802 }
10803
10804 /* Presto. If we're dealing with an anonymous class and a
10805 constructor call, generate the right constructor now, since we
10806 know the arguments' types. */
10807
10808 if (lc && ANONYMOUS_CLASS_P (class))
10809 craft_constructor (TYPE_NAME (class), atl);
10810
10811 /* Find all candidates and then refine the list, searching for the
10812 most specific method. */
10813 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10814 list = find_most_specific_methods_list (list);
10815 if (list && !TREE_CHAIN (list))
10816 return TREE_VALUE (list);
10817
10818 /* Issue an error. List candidates if any. Candidates are listed
10819 only if accessible (non accessible methods may end-up here for
10820 the sake of a better error report). */
10821 candidates = NULL;
10822 if (list)
10823 {
10824 tree current;
10825 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10826 for (current = list; current; current = TREE_CHAIN (current))
10827 {
10828 tree cm = TREE_VALUE (current);
10829 char string [4096];
10830 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10831 continue;
10832 sprintf
10833 (string, " `%s' in `%s'%s",
10834 get_printable_method_name (cm),
10835 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10836 (TREE_CHAIN (current) ? "\n" : ""));
10837 obstack_grow (&temporary_obstack, string, strlen (string));
10838 }
10839 obstack_1grow (&temporary_obstack, '\0');
10840 candidates = obstack_finish (&temporary_obstack);
10841 }
10842 /* Issue the error message */
10843 method = make_node (FUNCTION_TYPE);
10844 TYPE_ARG_TYPES (method) = atl;
10845 signature = build_java_argument_signature (method);
10846 dup = xstrdup (lang_printable_name (class, 0));
10847 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10848 (lc ? "constructor" : "method"),
10849 (lc ? dup : IDENTIFIER_POINTER (name)),
10850 IDENTIFIER_POINTER (signature), dup,
10851 (candidates ? candidates : ""));
10852 free (dup);
10853 return NULL_TREE;
10854 }
10855
10856 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10857 when we're looking for a constructor. */
10858
10859 static tree
10860 find_applicable_accessible_methods_list (lc, class, name, arglist)
10861 int lc;
10862 tree class, name, arglist;
10863 {
10864 static struct hash_table t, *searched_classes = NULL;
10865 static int search_not_done = 0;
10866 tree list = NULL_TREE, all_list = NULL_TREE;
10867
10868 /* Check the hash table to determine if this class has been searched
10869 already. */
10870 if (searched_classes)
10871 {
10872 if (hash_lookup (searched_classes,
10873 (const hash_table_key) class, FALSE, NULL))
10874 return NULL;
10875 }
10876 else
10877 {
10878 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10879 java_hash_compare_tree_node);
10880 searched_classes = &t;
10881 }
10882
10883 search_not_done++;
10884 hash_lookup (searched_classes,
10885 (const hash_table_key) class, TRUE, NULL);
10886
10887 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10888 {
10889 load_class (class, 1);
10890 safe_layout_class (class);
10891 }
10892
10893 /* Search interfaces */
10894 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10895 && CLASS_INTERFACE (TYPE_NAME (class)))
10896 {
10897 int i, n;
10898 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10899 search_applicable_methods_list (lc, TYPE_METHODS (class),
10900 name, arglist, &list, &all_list);
10901 n = TREE_VEC_LENGTH (basetype_vec);
10902 for (i = 1; i < n; i++)
10903 {
10904 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10905 tree rlist;
10906
10907 rlist = find_applicable_accessible_methods_list (lc, t, name,
10908 arglist);
10909 list = chainon (rlist, list);
10910 }
10911 }
10912 /* Search classes */
10913 else
10914 {
10915 search_applicable_methods_list (lc, TYPE_METHODS (class),
10916 name, arglist, &list, &all_list);
10917
10918 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
10919 that we only search in class. Note that we should have found
10920 something at this point. */
10921 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
10922 {
10923 lc = 1;
10924 if (!list)
10925 abort ();
10926 }
10927
10928 /* We must search all interfaces of this class */
10929 if (!lc)
10930 {
10931 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10932 int n = TREE_VEC_LENGTH (basetype_vec), i;
10933 for (i = 1; i < n; i++)
10934 {
10935 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10936 if (t != object_type_node)
10937 {
10938 tree rlist
10939 = find_applicable_accessible_methods_list (lc, t,
10940 name, arglist);
10941 list = chainon (rlist, list);
10942 }
10943 }
10944 }
10945
10946 /* Search superclass */
10947 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10948 {
10949 tree rlist;
10950 class = CLASSTYPE_SUPER (class);
10951 rlist = find_applicable_accessible_methods_list (lc, class,
10952 name, arglist);
10953 list = chainon (rlist, list);
10954 }
10955 }
10956
10957 search_not_done--;
10958
10959 /* We're done. Reset the searched classes list and finally search
10960 java.lang.Object if it wasn't searched already. */
10961 if (!search_not_done)
10962 {
10963 if (!lc
10964 && TYPE_METHODS (object_type_node)
10965 && !hash_lookup (searched_classes,
10966 (const hash_table_key) object_type_node,
10967 FALSE, NULL))
10968 {
10969 search_applicable_methods_list (lc,
10970 TYPE_METHODS (object_type_node),
10971 name, arglist, &list, &all_list);
10972 }
10973 hash_table_free (searched_classes);
10974 searched_classes = NULL;
10975 }
10976
10977 /* Either return the list obtained or all selected (but
10978 inaccessible) methods for better error report. */
10979 return (!list ? all_list : list);
10980 }
10981
10982 /* Effectively search for the appropriate method in method */
10983
10984 static void
10985 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10986 int lc;
10987 tree method, name, arglist;
10988 tree *list, *all_list;
10989 {
10990 for (; method; method = TREE_CHAIN (method))
10991 {
10992 /* When dealing with constructor, stop here, otherwise search
10993 other classes */
10994 if (lc && !DECL_CONSTRUCTOR_P (method))
10995 continue;
10996 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10997 || (DECL_NAME (method) != name)))
10998 continue;
10999
11000 if (argument_types_convertible (method, arglist))
11001 {
11002 /* Retain accessible methods only */
11003 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11004 method, NULL_TREE, 0))
11005 *list = tree_cons (NULL_TREE, method, *list);
11006 else
11007 /* Also retain all selected method here */
11008 *all_list = tree_cons (NULL_TREE, method, *list);
11009 }
11010 }
11011 }
11012
11013 /* 15.11.2.2 Choose the Most Specific Method */
11014
11015 static tree
11016 find_most_specific_methods_list (list)
11017 tree list;
11018 {
11019 int max = 0;
11020 int abstract, candidates;
11021 tree current, new_list = NULL_TREE;
11022 for (current = list; current; current = TREE_CHAIN (current))
11023 {
11024 tree method;
11025 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11026
11027 for (method = list; method; method = TREE_CHAIN (method))
11028 {
11029 tree method_v, current_v;
11030 /* Don't test a method against itself */
11031 if (method == current)
11032 continue;
11033
11034 method_v = TREE_VALUE (method);
11035 current_v = TREE_VALUE (current);
11036
11037 /* Compare arguments and location where methods where declared */
11038 if (argument_types_convertible (method_v, current_v))
11039 {
11040 if (valid_method_invocation_conversion_p
11041 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11042 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11043 && enclosing_context_p (DECL_CONTEXT (method_v),
11044 DECL_CONTEXT (current_v))))
11045 {
11046 int v = (DECL_SPECIFIC_COUNT (current_v) +=
11047 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11048 max = (v > max ? v : max);
11049 }
11050 }
11051 }
11052 }
11053
11054 /* Review the list and select the maximally specific methods */
11055 for (current = list, abstract = -1, candidates = -1;
11056 current; current = TREE_CHAIN (current))
11057 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11058 {
11059 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11060 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11061 candidates++;
11062 }
11063
11064 /* If we have several and they're all abstract, just pick the
11065 closest one. */
11066 if (candidates > 0 && (candidates == abstract))
11067 {
11068 new_list = nreverse (new_list);
11069 TREE_CHAIN (new_list) = NULL_TREE;
11070 }
11071
11072 /* We have several (we couldn't find a most specific), all but one
11073 are abstract, we pick the only non abstract one. */
11074 if (candidates > 0 && (candidates == abstract+1))
11075 {
11076 for (current = new_list; current; current = TREE_CHAIN (current))
11077 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11078 {
11079 TREE_CHAIN (current) = NULL_TREE;
11080 new_list = current;
11081 }
11082 }
11083
11084 /* If we can't find one, lower expectations and try to gather multiple
11085 maximally specific methods */
11086 while (!new_list && max)
11087 {
11088 while (--max > 0)
11089 {
11090 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11091 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11092 }
11093 }
11094
11095 return new_list;
11096 }
11097
11098 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11099 converted by method invocation conversion (5.3) to the type of the
11100 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11101 to change less often than M1. */
11102
11103 static int
11104 argument_types_convertible (m1, m2_or_arglist)
11105 tree m1, m2_or_arglist;
11106 {
11107 static tree m2_arg_value = NULL_TREE;
11108 static tree m2_arg_cache = NULL_TREE;
11109 static int initialized_p;
11110
11111 register tree m1_arg, m2_arg;
11112
11113 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
11114 collector. */
11115 if (!initialized_p)
11116 {
11117 ggc_add_tree_root (&m2_arg_value, 1);
11118 ggc_add_tree_root (&m2_arg_cache, 1);
11119 initialized_p = 1;
11120 }
11121
11122 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11123
11124 if (m2_arg_value == m2_or_arglist)
11125 m2_arg = m2_arg_cache;
11126 else
11127 {
11128 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11129 argument types */
11130 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11131 {
11132 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11133 if (!METHOD_STATIC (m2_or_arglist))
11134 m2_arg = TREE_CHAIN (m2_arg);
11135 }
11136 else
11137 m2_arg = m2_or_arglist;
11138
11139 m2_arg_value = m2_or_arglist;
11140 m2_arg_cache = m2_arg;
11141 }
11142
11143 while (m1_arg != end_params_node && m2_arg != end_params_node)
11144 {
11145 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11146 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11147 TREE_VALUE (m2_arg)))
11148 break;
11149 m1_arg = TREE_CHAIN (m1_arg);
11150 m2_arg = TREE_CHAIN (m2_arg);
11151 }
11152 return m1_arg == end_params_node && m2_arg == end_params_node;
11153 }
11154
11155 /* Qualification routines */
11156
11157 static void
11158 qualify_ambiguous_name (id)
11159 tree id;
11160 {
11161 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11162 saved_current_class;
11163 int again, super_found = 0, this_found = 0, new_array_found = 0;
11164 int code;
11165
11166 /* We first qualify the first element, then derive qualification of
11167 others based on the first one. If the first element is qualified
11168 by a resolution (field or type), this resolution is stored in the
11169 QUAL_RESOLUTION of the qual element being examined. We need to
11170 save the current_class since the use of SUPER might change the
11171 its value. */
11172 saved_current_class = current_class;
11173 qual = EXPR_WFL_QUALIFICATION (id);
11174 do {
11175
11176 /* Simple qualified expression feature a qual_wfl that is a
11177 WFL. Expression derived from a primary feature more complicated
11178 things like a CALL_EXPR. Expression from primary need to be
11179 worked out to extract the part on which the qualification will
11180 take place. */
11181 qual_wfl = QUAL_WFL (qual);
11182 switch (TREE_CODE (qual_wfl))
11183 {
11184 case CALL_EXPR:
11185 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11186 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11187 {
11188 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11189 qual_wfl = QUAL_WFL (qual);
11190 }
11191 break;
11192 case NEW_ARRAY_EXPR:
11193 case NEW_ANONYMOUS_ARRAY_EXPR:
11194 qual = TREE_CHAIN (qual);
11195 again = new_array_found = 1;
11196 continue;
11197 case CONVERT_EXPR:
11198 break;
11199 case NEW_CLASS_EXPR:
11200 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11201 break;
11202 case ARRAY_REF:
11203 while (TREE_CODE (qual_wfl) == ARRAY_REF)
11204 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11205 break;
11206 case STRING_CST:
11207 qual = TREE_CHAIN (qual);
11208 qual_wfl = QUAL_WFL (qual);
11209 break;
11210 case CLASS_LITERAL:
11211 qual = TREE_CHAIN (qual);
11212 qual_wfl = QUAL_WFL (qual);
11213 break;
11214 default:
11215 /* Fix for -Wall. Just break doing nothing */
11216 break;
11217 }
11218
11219 ptr_type = current_class;
11220 again = 0;
11221 code = TREE_CODE (qual_wfl);
11222
11223 /* Pos evaluation: non WFL leading expression nodes */
11224 if (code == CONVERT_EXPR
11225 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11226 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11227
11228 else if (code == INTEGER_CST)
11229 name = qual_wfl;
11230
11231 else if (code == CONVERT_EXPR &&
11232 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11233 name = TREE_OPERAND (qual_wfl, 0);
11234
11235 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11236 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11237 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11238
11239 else if (code == TREE_LIST)
11240 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11241
11242 else if (code == STRING_CST || code == CONDITIONAL_EXPR
11243 || code == PLUS_EXPR)
11244 {
11245 qual = TREE_CHAIN (qual);
11246 qual_wfl = QUAL_WFL (qual);
11247 again = 1;
11248 }
11249 else
11250 {
11251 name = EXPR_WFL_NODE (qual_wfl);
11252 if (!name)
11253 {
11254 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11255 again = 1;
11256 }
11257 }
11258
11259 /* If we have a THIS (from a primary), we set the context accordingly */
11260 if (name == this_identifier_node)
11261 {
11262 /* This isn't really elegant. One more added irregularity
11263 before I start using COMPONENT_REF (hopefully very soon.) */
11264 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11265 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11266 EXPR_WITH_FILE_LOCATION
11267 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11268 this_identifier_node)
11269 {
11270 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11271 qual = EXPR_WFL_QUALIFICATION (qual);
11272 }
11273 qual = TREE_CHAIN (qual);
11274 qual_wfl = QUAL_WFL (qual);
11275 if (TREE_CODE (qual_wfl) == CALL_EXPR)
11276 again = 1;
11277 else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11278 name = EXPR_WFL_NODE (qual_wfl);
11279 else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11280 name = TREE_OPERAND (qual_wfl, 0);
11281 this_found = 1;
11282 }
11283 /* If we have a SUPER, we set the context accordingly */
11284 if (name == super_identifier_node)
11285 {
11286 current_class = CLASSTYPE_SUPER (ptr_type);
11287 /* Check that there is such a thing as a super class. If not,
11288 return. The error will be caught later on, during the
11289 resolution */
11290 if (!current_class)
11291 {
11292 current_class = saved_current_class;
11293 return;
11294 }
11295 qual = TREE_CHAIN (qual);
11296 /* Do one more interation to set things up */
11297 super_found = again = 1;
11298 }
11299 } while (again);
11300
11301 /* If name appears within the scope of a local variable declaration
11302 or parameter declaration, then it is an expression name. We don't
11303 carry this test out if we're in the context of the use of SUPER
11304 or THIS */
11305 if (!this_found && !super_found
11306 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11307 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11308 {
11309 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11310 QUAL_RESOLUTION (qual) = decl;
11311 }
11312
11313 /* If within the class/interface NAME was found to be used there
11314 exists a (possibly inherited) field named NAME, then this is an
11315 expression name. If we saw a NEW_ARRAY_EXPR before and want to
11316 address length, it is OK. */
11317 else if ((decl = lookup_field_wrapper (ptr_type, name))
11318 || name == length_identifier_node)
11319 {
11320 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11321 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11322 }
11323
11324 /* We reclassify NAME as yielding to a type name resolution if:
11325 - NAME is a class/interface declared within the compilation
11326 unit containing NAME,
11327 - NAME is imported via a single-type-import declaration,
11328 - NAME is declared in an another compilation unit of the package
11329 of the compilation unit containing NAME,
11330 - NAME is declared by exactly on type-import-on-demand declaration
11331 of the compilation unit containing NAME.
11332 - NAME is actually a STRING_CST.
11333 This can't happen if the expression was qualified by `this.' */
11334 else if (! this_found &&
11335 (TREE_CODE (name) == STRING_CST ||
11336 TREE_CODE (name) == INTEGER_CST ||
11337 (decl = resolve_and_layout (name, NULL_TREE))))
11338 {
11339 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11340 QUAL_RESOLUTION (qual) = decl;
11341 }
11342
11343 /* Method call, array references and cast are expression name */
11344 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11345 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11346 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11347 || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11348 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11349
11350 /* Check here that NAME isn't declared by more than one
11351 type-import-on-demand declaration of the compilation unit
11352 containing NAME. FIXME */
11353
11354 /* Otherwise, NAME is reclassified as a package name */
11355 else
11356 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11357
11358 /* Propagate the qualification accross other components of the
11359 qualified name */
11360 for (qual = TREE_CHAIN (qual); qual;
11361 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11362 {
11363 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11364 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11365 else
11366 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11367 }
11368
11369 /* Store the global qualification for the ambiguous part of ID back
11370 into ID fields */
11371 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11372 RESOLVE_EXPRESSION_NAME_P (id) = 1;
11373 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11374 RESOLVE_TYPE_NAME_P (id) = 1;
11375 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11376 RESOLVE_PACKAGE_NAME_P (id) = 1;
11377
11378 /* Restore the current class */
11379 current_class = saved_current_class;
11380 }
11381
11382 static int
11383 breakdown_qualified (left, right, source)
11384 tree *left, *right, source;
11385 {
11386 char *p, *base;
11387 int l = IDENTIFIER_LENGTH (source);
11388
11389 base = alloca (l + 1);
11390 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11391
11392 /* Breakdown NAME into REMAINDER . IDENTIFIER */
11393 p = base + l - 1;
11394 while (*p != '.' && p != base)
11395 p--;
11396
11397 /* We didn't find a '.'. Return an error */
11398 if (p == base)
11399 return 1;
11400
11401 *p = '\0';
11402 if (right)
11403 *right = get_identifier (p+1);
11404 *left = get_identifier (base);
11405
11406 return 0;
11407 }
11408
11409 /* Return TRUE if two classes are from the same package. */
11410
11411 static int
11412 in_same_package (name1, name2)
11413 tree name1, name2;
11414 {
11415 tree tmp;
11416 tree pkg1;
11417 tree pkg2;
11418
11419 if (TREE_CODE (name1) == TYPE_DECL)
11420 name1 = DECL_NAME (name1);
11421 if (TREE_CODE (name2) == TYPE_DECL)
11422 name2 = DECL_NAME (name2);
11423
11424 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11425 /* One in empty package. */
11426 return 0;
11427
11428 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11429 /* Both in empty package. */
11430 return 1;
11431
11432 breakdown_qualified (&pkg1, &tmp, name1);
11433 breakdown_qualified (&pkg2, &tmp, name2);
11434
11435 return (pkg1 == pkg2);
11436 }
11437
11438 /* Patch tree nodes in a function body. When a BLOCK is found, push
11439 local variable decls if present.
11440 Same as java_complete_lhs, but does resolve static finals to values. */
11441
11442 static tree
11443 java_complete_tree (node)
11444 tree node;
11445 {
11446 node = java_complete_lhs (node);
11447 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11448 && DECL_INITIAL (node) != NULL_TREE
11449 && !flag_emit_xref)
11450 {
11451 tree value = DECL_INITIAL (node);
11452 DECL_INITIAL (node) = NULL_TREE;
11453 value = fold_constant_for_init (value, node);
11454 DECL_INITIAL (node) = value;
11455 if (value != NULL_TREE)
11456 {
11457 /* fold_constant_for_init sometimes widens the original type
11458 of the constant (i.e. byte to int). It's not desirable,
11459 especially if NODE is a function argument. */
11460 if ((TREE_CODE (value) == INTEGER_CST
11461 || TREE_CODE (value) == REAL_CST)
11462 && TREE_TYPE (node) != TREE_TYPE (value))
11463 return convert (TREE_TYPE (node), value);
11464 else
11465 return value;
11466 }
11467 }
11468 return node;
11469 }
11470
11471 static tree
11472 java_stabilize_reference (node)
11473 tree node;
11474 {
11475 if (TREE_CODE (node) == COMPOUND_EXPR)
11476 {
11477 tree op0 = TREE_OPERAND (node, 0);
11478 tree op1 = TREE_OPERAND (node, 1);
11479 TREE_OPERAND (node, 0) = save_expr (op0);
11480 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11481 return node;
11482 }
11483 return stabilize_reference (node);
11484 }
11485
11486 /* Patch tree nodes in a function body. When a BLOCK is found, push
11487 local variable decls if present.
11488 Same as java_complete_tree, but does not resolve static finals to values. */
11489
11490 static tree
11491 java_complete_lhs (node)
11492 tree node;
11493 {
11494 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11495 int flag;
11496
11497 /* CONVERT_EXPR always has its type set, even though it needs to be
11498 worked out. */
11499 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11500 return node;
11501
11502 /* The switch block implements cases processing container nodes
11503 first. Contained nodes are always written back. Leaves come
11504 next and return a value. */
11505 switch (TREE_CODE (node))
11506 {
11507 case BLOCK:
11508
11509 /* 1- Block section.
11510 Set the local values on decl names so we can identify them
11511 faster when they're referenced. At that stage, identifiers
11512 are legal so we don't check for declaration errors. */
11513 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11514 {
11515 DECL_CONTEXT (cn) = current_function_decl;
11516 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11517 }
11518 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11519 CAN_COMPLETE_NORMALLY (node) = 1;
11520 else
11521 {
11522 tree stmt = BLOCK_EXPR_BODY (node);
11523 tree *ptr;
11524 int error_seen = 0;
11525 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11526 {
11527 /* Re-order from (((A; B); C); ...; Z) to
11528 (A; (B; (C ; (...; Z)))).
11529 This makes it easier to scan the statements left-to-right
11530 without using recursion (which might overflow the stack
11531 if the block has many statements. */
11532 for (;;)
11533 {
11534 tree left = TREE_OPERAND (stmt, 0);
11535 if (TREE_CODE (left) != COMPOUND_EXPR)
11536 break;
11537 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11538 TREE_OPERAND (left, 1) = stmt;
11539 stmt = left;
11540 }
11541 BLOCK_EXPR_BODY (node) = stmt;
11542 }
11543
11544 /* Now do the actual complete, without deep recursion for
11545 long blocks. */
11546 ptr = &BLOCK_EXPR_BODY (node);
11547 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11548 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11549 {
11550 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11551 tree *next = &TREE_OPERAND (*ptr, 1);
11552 TREE_OPERAND (*ptr, 0) = cur;
11553 if (cur == empty_stmt_node)
11554 {
11555 /* Optimization; makes it easier to detect empty bodies.
11556 Most useful for <clinit> with all-constant initializer. */
11557 *ptr = *next;
11558 continue;
11559 }
11560 if (TREE_CODE (cur) == ERROR_MARK)
11561 error_seen++;
11562 else if (! CAN_COMPLETE_NORMALLY (cur))
11563 {
11564 wfl_op2 = *next;
11565 for (;;)
11566 {
11567 if (TREE_CODE (wfl_op2) == BLOCK)
11568 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11569 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11570 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11571 else
11572 break;
11573 }
11574 if (TREE_CODE (wfl_op2) != CASE_EXPR
11575 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11576 unreachable_stmt_error (*ptr);
11577 }
11578 ptr = next;
11579 }
11580 *ptr = java_complete_tree (*ptr);
11581
11582 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11583 return error_mark_node;
11584 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11585 }
11586 /* Turn local bindings to null */
11587 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11588 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11589
11590 TREE_TYPE (node) = void_type_node;
11591 break;
11592
11593 /* 2- They are expressions but ultimately deal with statements */
11594
11595 case THROW_EXPR:
11596 wfl_op1 = TREE_OPERAND (node, 0);
11597 COMPLETE_CHECK_OP_0 (node);
11598 /* 14.19 A throw statement cannot complete normally. */
11599 CAN_COMPLETE_NORMALLY (node) = 0;
11600 return patch_throw_statement (node, wfl_op1);
11601
11602 case SYNCHRONIZED_EXPR:
11603 wfl_op1 = TREE_OPERAND (node, 0);
11604 return patch_synchronized_statement (node, wfl_op1);
11605
11606 case TRY_EXPR:
11607 return patch_try_statement (node);
11608
11609 case TRY_FINALLY_EXPR:
11610 COMPLETE_CHECK_OP_0 (node);
11611 COMPLETE_CHECK_OP_1 (node);
11612 if (TREE_OPERAND (node, 0) == empty_stmt_node)
11613 return TREE_OPERAND (node, 1);
11614 if (TREE_OPERAND (node, 1) == empty_stmt_node)
11615 return TREE_OPERAND (node, 0);
11616 CAN_COMPLETE_NORMALLY (node)
11617 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11618 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11619 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11620 return node;
11621
11622 case LABELED_BLOCK_EXPR:
11623 PUSH_LABELED_BLOCK (node);
11624 if (LABELED_BLOCK_BODY (node))
11625 COMPLETE_CHECK_OP_1 (node);
11626 TREE_TYPE (node) = void_type_node;
11627 POP_LABELED_BLOCK ();
11628
11629 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11630 {
11631 LABELED_BLOCK_BODY (node) = NULL_TREE;
11632 CAN_COMPLETE_NORMALLY (node) = 1;
11633 }
11634 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11635 CAN_COMPLETE_NORMALLY (node) = 1;
11636 return node;
11637
11638 case EXIT_BLOCK_EXPR:
11639 /* We don't complete operand 1, because it's the return value of
11640 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11641 return patch_bc_statement (node);
11642
11643 case CASE_EXPR:
11644 cn = java_complete_tree (TREE_OPERAND (node, 0));
11645 if (cn == error_mark_node)
11646 return cn;
11647
11648 /* First, the case expression must be constant. Values of final
11649 fields are accepted. */
11650 cn = fold (cn);
11651 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11652 && JDECL_P (TREE_OPERAND (cn, 1))
11653 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11654 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11655 {
11656 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11657 TREE_OPERAND (cn, 1));
11658 }
11659 /* Accept final locals too. */
11660 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn))
11661 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11662
11663 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11664 {
11665 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11666 parse_error_context (node, "Constant expression required");
11667 return error_mark_node;
11668 }
11669
11670 nn = ctxp->current_loop;
11671
11672 /* It must be assignable to the type of the switch expression. */
11673 if (!try_builtin_assignconv (NULL_TREE,
11674 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11675 {
11676 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11677 parse_error_context
11678 (wfl_operator,
11679 "Incompatible type for case. Can't convert `%s' to `int'",
11680 lang_printable_name (TREE_TYPE (cn), 0));
11681 return error_mark_node;
11682 }
11683
11684 cn = fold (convert (int_type_node, cn));
11685 TREE_CONSTANT_OVERFLOW (cn) = 0;
11686 CAN_COMPLETE_NORMALLY (cn) = 1;
11687
11688 /* Save the label on a list so that we can later check for
11689 duplicates. */
11690 case_label_list = tree_cons (node, cn, case_label_list);
11691
11692 /* Multiple instance of a case label bearing the same value is
11693 checked later. The case expression is all right so far. */
11694 if (TREE_CODE (cn) == VAR_DECL)
11695 cn = DECL_INITIAL (cn);
11696 TREE_OPERAND (node, 0) = cn;
11697 TREE_TYPE (node) = void_type_node;
11698 CAN_COMPLETE_NORMALLY (node) = 1;
11699 TREE_SIDE_EFFECTS (node) = 1;
11700 break;
11701
11702 case DEFAULT_EXPR:
11703 nn = ctxp->current_loop;
11704 /* Only one default label is allowed per switch statement */
11705 if (SWITCH_HAS_DEFAULT (nn))
11706 {
11707 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11708 parse_error_context (wfl_operator,
11709 "Duplicate case label: `default'");
11710 return error_mark_node;
11711 }
11712 else
11713 SWITCH_HAS_DEFAULT (nn) = 1;
11714 TREE_TYPE (node) = void_type_node;
11715 TREE_SIDE_EFFECTS (node) = 1;
11716 CAN_COMPLETE_NORMALLY (node) = 1;
11717 break;
11718
11719 case SWITCH_EXPR:
11720 case LOOP_EXPR:
11721 PUSH_LOOP (node);
11722 /* Check whether the loop was enclosed in a labeled
11723 statement. If not, create one, insert the loop in it and
11724 return the node */
11725 nn = patch_loop_statement (node);
11726
11727 /* Anyways, walk the body of the loop */
11728 if (TREE_CODE (node) == LOOP_EXPR)
11729 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11730 /* Switch statement: walk the switch expression and the cases */
11731 else
11732 node = patch_switch_statement (node);
11733
11734 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11735 nn = error_mark_node;
11736 else
11737 {
11738 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11739 /* If we returned something different, that's because we
11740 inserted a label. Pop the label too. */
11741 if (nn != node)
11742 {
11743 if (CAN_COMPLETE_NORMALLY (node))
11744 CAN_COMPLETE_NORMALLY (nn) = 1;
11745 POP_LABELED_BLOCK ();
11746 }
11747 }
11748 POP_LOOP ();
11749 return nn;
11750
11751 case EXIT_EXPR:
11752 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11753 return patch_exit_expr (node);
11754
11755 case COND_EXPR:
11756 /* Condition */
11757 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11758 if (TREE_OPERAND (node, 0) == error_mark_node)
11759 return error_mark_node;
11760 /* then-else branches */
11761 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11762 if (TREE_OPERAND (node, 1) == error_mark_node)
11763 return error_mark_node;
11764 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11765 if (TREE_OPERAND (node, 2) == error_mark_node)
11766 return error_mark_node;
11767 return patch_if_else_statement (node);
11768 break;
11769
11770 case CONDITIONAL_EXPR:
11771 /* Condition */
11772 wfl_op1 = TREE_OPERAND (node, 0);
11773 COMPLETE_CHECK_OP_0 (node);
11774 wfl_op2 = TREE_OPERAND (node, 1);
11775 COMPLETE_CHECK_OP_1 (node);
11776 wfl_op3 = TREE_OPERAND (node, 2);
11777 COMPLETE_CHECK_OP_2 (node);
11778 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11779
11780 /* 3- Expression section */
11781 case COMPOUND_EXPR:
11782 wfl_op2 = TREE_OPERAND (node, 1);
11783 TREE_OPERAND (node, 0) = nn =
11784 java_complete_tree (TREE_OPERAND (node, 0));
11785 if (wfl_op2 == empty_stmt_node)
11786 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11787 else
11788 {
11789 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11790 {
11791 /* An unreachable condition in a do-while statement
11792 is *not* (technically) an unreachable statement. */
11793 nn = wfl_op2;
11794 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11795 nn = EXPR_WFL_NODE (nn);
11796 if (TREE_CODE (nn) != EXIT_EXPR)
11797 {
11798 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11799 parse_error_context (wfl_operator, "Unreachable statement");
11800 }
11801 }
11802 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11803 if (TREE_OPERAND (node, 1) == error_mark_node)
11804 return error_mark_node;
11805 /* Even though we might allow the case where the first
11806 operand doesn't return normally, we still should compute
11807 CAN_COMPLETE_NORMALLY correctly. */
11808 CAN_COMPLETE_NORMALLY (node)
11809 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11810 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11811 }
11812 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11813 break;
11814
11815 case RETURN_EXPR:
11816 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11817 return patch_return (node);
11818
11819 case EXPR_WITH_FILE_LOCATION:
11820 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11821 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11822 {
11823 tree wfl = node;
11824 node = resolve_expression_name (node, NULL);
11825 if (node == error_mark_node)
11826 return node;
11827 /* Keep line number information somewhere were it doesn't
11828 disrupt the completion process. */
11829 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11830 {
11831 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11832 TREE_OPERAND (node, 1) = wfl;
11833 }
11834 CAN_COMPLETE_NORMALLY (node) = 1;
11835 }
11836 else
11837 {
11838 tree body;
11839 int save_lineno = lineno;
11840 lineno = EXPR_WFL_LINENO (node);
11841 body = java_complete_tree (EXPR_WFL_NODE (node));
11842 lineno = save_lineno;
11843 EXPR_WFL_NODE (node) = body;
11844 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11845 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11846 if (body == empty_stmt_node || TREE_CONSTANT (body))
11847 {
11848 /* Makes it easier to constant fold, detect empty bodies. */
11849 return body;
11850 }
11851 if (body == error_mark_node)
11852 {
11853 /* Its important for the evaluation of assignment that
11854 this mark on the TREE_TYPE is propagated. */
11855 TREE_TYPE (node) = error_mark_node;
11856 return error_mark_node;
11857 }
11858 else
11859 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11860
11861 }
11862 break;
11863
11864 case NEW_ARRAY_EXPR:
11865 /* Patch all the dimensions */
11866 flag = 0;
11867 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11868 {
11869 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11870 tree dim = convert (int_type_node,
11871 java_complete_tree (TREE_VALUE (cn)));
11872 if (dim == error_mark_node)
11873 {
11874 flag = 1;
11875 continue;
11876 }
11877 else
11878 {
11879 TREE_VALUE (cn) = dim;
11880 /* Setup the location of the current dimension, for
11881 later error report. */
11882 TREE_PURPOSE (cn) =
11883 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11884 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11885 }
11886 }
11887 /* They complete the array creation expression, if no errors
11888 were found. */
11889 CAN_COMPLETE_NORMALLY (node) = 1;
11890 return (flag ? error_mark_node
11891 : force_evaluation_order (patch_newarray (node)));
11892
11893 case NEW_ANONYMOUS_ARRAY_EXPR:
11894 /* Create the array type if necessary. */
11895 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11896 {
11897 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11898 if (!(type = resolve_type_during_patch (type)))
11899 return error_mark_node;
11900 type = build_array_from_name (type, NULL_TREE,
11901 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11902 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11903 }
11904 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11905 ANONYMOUS_ARRAY_INITIALIZER (node));
11906 if (node == error_mark_node)
11907 return error_mark_node;
11908 CAN_COMPLETE_NORMALLY (node) = 1;
11909 return node;
11910
11911 case NEW_CLASS_EXPR:
11912 case CALL_EXPR:
11913 /* Complete function's argument(s) first */
11914 if (complete_function_arguments (node))
11915 return error_mark_node;
11916 else
11917 {
11918 tree decl, wfl = TREE_OPERAND (node, 0);
11919 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11920 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11921 super_identifier_node);
11922
11923 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11924 from_super, 0, &decl);
11925 if (node == error_mark_node)
11926 return error_mark_node;
11927
11928 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11929 /* If we call this(...), register signature and positions */
11930 if (in_this)
11931 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11932 tree_cons (wfl, decl,
11933 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11934 CAN_COMPLETE_NORMALLY (node) = 1;
11935 return force_evaluation_order (node);
11936 }
11937
11938 case MODIFY_EXPR:
11939 /* Save potential wfls */
11940 wfl_op1 = TREE_OPERAND (node, 0);
11941 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11942
11943 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11944 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11945 && DECL_INITIAL (nn) != NULL_TREE)
11946 {
11947 tree value;
11948
11949 value = fold_constant_for_init (nn, nn);
11950
11951 /* When we have a primitype type, or a string and we're not
11952 emitting a class file, we actually don't want to generate
11953 anything for the assignment. */
11954 if (value != NULL_TREE &&
11955 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
11956 (TREE_TYPE (value) == string_ptr_type_node &&
11957 ! flag_emit_class_files)))
11958 {
11959 /* Prepare node for patch_assignment */
11960 TREE_OPERAND (node, 1) = value;
11961 /* Call patch assignment to verify the assignment */
11962 if (patch_assignment (node, wfl_op1) == error_mark_node)
11963 return error_mark_node;
11964 /* Set DECL_INITIAL properly (a conversion might have
11965 been decided by patch_assignment) and return the
11966 empty statement. */
11967 else
11968 {
11969 tree patched = patch_string (TREE_OPERAND (node, 1));
11970 if (patched)
11971 DECL_INITIAL (nn) = patched;
11972 else
11973 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
11974 DECL_FIELD_FINAL_IUD (nn) = 1;
11975 return empty_stmt_node;
11976 }
11977 }
11978 if (! flag_emit_class_files)
11979 DECL_INITIAL (nn) = NULL_TREE;
11980 }
11981 wfl_op2 = TREE_OPERAND (node, 1);
11982
11983 if (TREE_OPERAND (node, 0) == error_mark_node)
11984 return error_mark_node;
11985
11986 flag = COMPOUND_ASSIGN_P (wfl_op2);
11987 if (flag)
11988 {
11989 /* This might break when accessing outer field from inner
11990 class. TESTME, FIXME */
11991 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11992
11993 /* Hand stabilize the lhs on both places */
11994 TREE_OPERAND (node, 0) = lvalue;
11995 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11996 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11997
11998 /* 15.25.2.a: Left hand is not an array access. FIXME */
11999 /* Now complete the RHS. We write it back later on. */
12000 nn = java_complete_tree (TREE_OPERAND (node, 1));
12001
12002 if ((cn = patch_string (nn)))
12003 nn = cn;
12004
12005 /* The last part of the rewrite for E1 op= E2 is to have
12006 E1 = (T)(E1 op E2), with T being the type of E1. */
12007 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12008 TREE_TYPE (lvalue), nn));
12009
12010 /* If the assignment is compound and has reference type,
12011 then ensure the LHS has type String and nothing else. */
12012 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12013 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12014 parse_error_context (wfl_op2,
12015 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
12016 lang_printable_name (TREE_TYPE (lvalue), 0));
12017
12018 /* 15.25.2.b: Left hand is an array access. FIXME */
12019 }
12020
12021 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12022 function to complete this RHS. Note that a NEW_ARRAY_INIT
12023 might have been already fully expanded if created as a result
12024 of processing an anonymous array initializer. We avoid doing
12025 the operation twice by testing whether the node already bears
12026 a type. */
12027 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12028 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12029 TREE_OPERAND (node, 1));
12030 /* Otherwise we simply complete the RHS */
12031 else
12032 nn = java_complete_tree (TREE_OPERAND (node, 1));
12033
12034 if (nn == error_mark_node)
12035 return error_mark_node;
12036
12037 /* Write back the RHS as we evaluated it. */
12038 TREE_OPERAND (node, 1) = nn;
12039
12040 /* In case we're handling = with a String as a RHS, we need to
12041 produce a String out of the RHS (it might still be a
12042 STRING_CST or a StringBuffer at this stage */
12043 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12044 TREE_OPERAND (node, 1) = nn;
12045
12046 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12047 TREE_OPERAND (node, 1))))
12048 {
12049 /* We return error_mark_node if outer_field_access_fix
12050 detects we write into a final. */
12051 if (nn == error_mark_node)
12052 return error_mark_node;
12053 node = nn;
12054 }
12055 else
12056 {
12057 node = patch_assignment (node, wfl_op1);
12058 /* Reorganize the tree if necessary. */
12059 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12060 || JSTRING_P (TREE_TYPE (node))))
12061 node = java_refold (node);
12062 }
12063
12064 /* Seek to set DECL_INITIAL to a proper value, since it might have
12065 undergone a conversion in patch_assignment. We do that only when
12066 it's necessary to have DECL_INITIAL properly set. */
12067 nn = TREE_OPERAND (node, 0);
12068 if (TREE_CODE (nn) == VAR_DECL
12069 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12070 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12071 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12072 || TREE_TYPE (nn) == string_ptr_type_node))
12073 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12074
12075 CAN_COMPLETE_NORMALLY (node) = 1;
12076 return node;
12077
12078 case MULT_EXPR:
12079 case PLUS_EXPR:
12080 case MINUS_EXPR:
12081 case LSHIFT_EXPR:
12082 case RSHIFT_EXPR:
12083 case URSHIFT_EXPR:
12084 case BIT_AND_EXPR:
12085 case BIT_XOR_EXPR:
12086 case BIT_IOR_EXPR:
12087 case TRUNC_MOD_EXPR:
12088 case TRUNC_DIV_EXPR:
12089 case RDIV_EXPR:
12090 case TRUTH_ANDIF_EXPR:
12091 case TRUTH_ORIF_EXPR:
12092 case EQ_EXPR:
12093 case NE_EXPR:
12094 case GT_EXPR:
12095 case GE_EXPR:
12096 case LT_EXPR:
12097 case LE_EXPR:
12098 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12099 knows how to handle those cases. */
12100 wfl_op1 = TREE_OPERAND (node, 0);
12101 wfl_op2 = TREE_OPERAND (node, 1);
12102
12103 CAN_COMPLETE_NORMALLY (node) = 1;
12104 /* Don't complete string nodes if dealing with the PLUS operand. */
12105 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12106 {
12107 nn = java_complete_tree (wfl_op1);
12108 if (nn == error_mark_node)
12109 return error_mark_node;
12110
12111 TREE_OPERAND (node, 0) = nn;
12112 }
12113 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12114 {
12115 nn = java_complete_tree (wfl_op2);
12116 if (nn == error_mark_node)
12117 return error_mark_node;
12118
12119 TREE_OPERAND (node, 1) = nn;
12120 }
12121 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
12122
12123 case INSTANCEOF_EXPR:
12124 wfl_op1 = TREE_OPERAND (node, 0);
12125 COMPLETE_CHECK_OP_0 (node);
12126 if (flag_emit_xref)
12127 {
12128 TREE_TYPE (node) = boolean_type_node;
12129 return node;
12130 }
12131 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12132
12133 case UNARY_PLUS_EXPR:
12134 case NEGATE_EXPR:
12135 case TRUTH_NOT_EXPR:
12136 case BIT_NOT_EXPR:
12137 case PREDECREMENT_EXPR:
12138 case PREINCREMENT_EXPR:
12139 case POSTDECREMENT_EXPR:
12140 case POSTINCREMENT_EXPR:
12141 case CONVERT_EXPR:
12142 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12143 how to handle those cases. */
12144 wfl_op1 = TREE_OPERAND (node, 0);
12145 CAN_COMPLETE_NORMALLY (node) = 1;
12146 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12147 if (TREE_OPERAND (node, 0) == error_mark_node)
12148 return error_mark_node;
12149 node = patch_unaryop (node, wfl_op1);
12150 CAN_COMPLETE_NORMALLY (node) = 1;
12151 break;
12152
12153 case ARRAY_REF:
12154 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12155 how to handle those cases. */
12156 wfl_op1 = TREE_OPERAND (node, 0);
12157 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12158 if (TREE_OPERAND (node, 0) == error_mark_node)
12159 return error_mark_node;
12160 if (!flag_emit_class_files && !flag_emit_xref)
12161 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12162 /* The same applies to wfl_op2 */
12163 wfl_op2 = TREE_OPERAND (node, 1);
12164 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12165 if (TREE_OPERAND (node, 1) == error_mark_node)
12166 return error_mark_node;
12167 if (!flag_emit_class_files && !flag_emit_xref)
12168 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12169 return patch_array_ref (node);
12170
12171 case RECORD_TYPE:
12172 return node;;
12173
12174 case COMPONENT_REF:
12175 /* The first step in the re-write of qualified name handling. FIXME.
12176 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12177 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12178 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12179 {
12180 tree name = TREE_OPERAND (node, 1);
12181 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12182 if (field == NULL_TREE)
12183 {
12184 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12185 return error_mark_node;
12186 }
12187 if (! FIELD_STATIC (field))
12188 {
12189 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12190 return error_mark_node;
12191 }
12192 return field;
12193 }
12194 else
12195 abort ();
12196 break;
12197
12198 case THIS_EXPR:
12199 /* Can't use THIS in a static environment */
12200 if (!current_this)
12201 {
12202 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12203 parse_error_context (wfl_operator,
12204 "Keyword `this' used outside allowed context");
12205 TREE_TYPE (node) = error_mark_node;
12206 return error_mark_node;
12207 }
12208 if (ctxp->explicit_constructor_p)
12209 {
12210 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12211 parse_error_context
12212 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12213 TREE_TYPE (node) = error_mark_node;
12214 return error_mark_node;
12215 }
12216 return current_this;
12217
12218 case CLASS_LITERAL:
12219 CAN_COMPLETE_NORMALLY (node) = 1;
12220 node = patch_incomplete_class_ref (node);
12221 if (node == error_mark_node)
12222 return error_mark_node;
12223 break;
12224
12225 default:
12226 CAN_COMPLETE_NORMALLY (node) = 1;
12227 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12228 and it's time to turn it into the appropriate String object */
12229 if ((nn = patch_string (node)))
12230 node = nn;
12231 else
12232 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12233 }
12234 return node;
12235 }
12236
12237 /* Complete function call's argument. Return a non zero value is an
12238 error was found. */
12239
12240 static int
12241 complete_function_arguments (node)
12242 tree node;
12243 {
12244 int flag = 0;
12245 tree cn;
12246
12247 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12248 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12249 {
12250 tree wfl = TREE_VALUE (cn), parm, temp;
12251 parm = java_complete_tree (wfl);
12252
12253 if (parm == error_mark_node)
12254 {
12255 flag = 1;
12256 continue;
12257 }
12258 /* If have a string literal that we haven't transformed yet or a
12259 crafted string buffer, as a result of use of the the String
12260 `+' operator. Build `parm.toString()' and expand it. */
12261 if ((temp = patch_string (parm)))
12262 parm = temp;
12263
12264 TREE_VALUE (cn) = parm;
12265 }
12266 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12267 return flag;
12268 }
12269
12270 /* Sometimes (for loops and variable initialized during their
12271 declaration), we want to wrap a statement around a WFL and turn it
12272 debugable. */
12273
12274 static tree
12275 build_debugable_stmt (location, stmt)
12276 int location;
12277 tree stmt;
12278 {
12279 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12280 {
12281 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12282 EXPR_WFL_LINECOL (stmt) = location;
12283 }
12284 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12285 return stmt;
12286 }
12287
12288 static tree
12289 build_expr_block (body, decls)
12290 tree body, decls;
12291 {
12292 tree node = make_node (BLOCK);
12293 BLOCK_EXPR_DECLS (node) = decls;
12294 BLOCK_EXPR_BODY (node) = body;
12295 if (body)
12296 TREE_TYPE (node) = TREE_TYPE (body);
12297 TREE_SIDE_EFFECTS (node) = 1;
12298 return node;
12299 }
12300
12301 /* Create a new function block and link it appropriately to current
12302 function block chain */
12303
12304 static tree
12305 enter_block ()
12306 {
12307 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12308
12309 /* Link block B supercontext to the previous block. The current
12310 function DECL is used as supercontext when enter_a_block is called
12311 for the first time for a given function. The current function body
12312 (DECL_FUNCTION_BODY) is set to be block B. */
12313
12314 tree fndecl = current_function_decl;
12315
12316 if (!fndecl) {
12317 BLOCK_SUPERCONTEXT (b) = current_static_block;
12318 current_static_block = b;
12319 }
12320
12321 else if (!DECL_FUNCTION_BODY (fndecl))
12322 {
12323 BLOCK_SUPERCONTEXT (b) = fndecl;
12324 DECL_FUNCTION_BODY (fndecl) = b;
12325 }
12326 else
12327 {
12328 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12329 DECL_FUNCTION_BODY (fndecl) = b;
12330 }
12331 return b;
12332 }
12333
12334 /* Exit a block by changing the current function body
12335 (DECL_FUNCTION_BODY) to the current block super context, only if
12336 the block being exited isn't the method's top level one. */
12337
12338 static tree
12339 exit_block ()
12340 {
12341 tree b;
12342 if (current_function_decl)
12343 {
12344 b = DECL_FUNCTION_BODY (current_function_decl);
12345 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12346 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12347 }
12348 else
12349 {
12350 b = current_static_block;
12351
12352 if (BLOCK_SUPERCONTEXT (b))
12353 current_static_block = BLOCK_SUPERCONTEXT (b);
12354 }
12355 return b;
12356 }
12357
12358 /* Lookup for NAME in the nested function's blocks, all the way up to
12359 the current toplevel one. It complies with Java's local variable
12360 scoping rules. */
12361
12362 static tree
12363 lookup_name_in_blocks (name)
12364 tree name;
12365 {
12366 tree b = GET_CURRENT_BLOCK (current_function_decl);
12367
12368 while (b != current_function_decl)
12369 {
12370 tree current;
12371
12372 /* Paranoid sanity check. To be removed */
12373 if (TREE_CODE (b) != BLOCK)
12374 abort ();
12375
12376 for (current = BLOCK_EXPR_DECLS (b); current;
12377 current = TREE_CHAIN (current))
12378 if (DECL_NAME (current) == name)
12379 return current;
12380 b = BLOCK_SUPERCONTEXT (b);
12381 }
12382 return NULL_TREE;
12383 }
12384
12385 static void
12386 maybe_absorb_scoping_blocks ()
12387 {
12388 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12389 {
12390 tree b = exit_block ();
12391 java_method_add_stmt (current_function_decl, b);
12392 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12393 }
12394 }
12395
12396 \f
12397 /* This section of the source is reserved to build_* functions that
12398 are building incomplete tree nodes and the patch_* functions that
12399 are completing them. */
12400
12401 /* Wrap a non WFL node around a WFL. */
12402
12403 static tree
12404 build_wfl_wrap (node, location)
12405 tree node;
12406 int location;
12407 {
12408 tree wfl, node_to_insert = node;
12409
12410 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12411 with the way we're processing SUPER. A THIS from a primary as a
12412 different form than a SUPER. Turn THIS into something symbolic */
12413 if (TREE_CODE (node) == THIS_EXPR)
12414 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12415 else
12416 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12417
12418 EXPR_WFL_LINECOL (wfl) = location;
12419 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12420 return wfl;
12421 }
12422
12423 /* Build a super() constructor invocation. Returns empty_stmt_node if
12424 we're currently dealing with the class java.lang.Object. */
12425
12426 static tree
12427 build_super_invocation (mdecl)
12428 tree mdecl;
12429 {
12430 if (DECL_CONTEXT (mdecl) == object_type_node)
12431 return empty_stmt_node;
12432 else
12433 {
12434 tree super_wfl = build_wfl_node (super_identifier_node);
12435 tree a = NULL_TREE, t;
12436 /* If we're dealing with an anonymous class, pass the arguments
12437 of the crafted constructor along. */
12438 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12439 {
12440 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12441 for (; t != end_params_node; t = TREE_CHAIN (t))
12442 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12443 }
12444 return build_method_invocation (super_wfl, a);
12445 }
12446 }
12447
12448 /* Build a SUPER/THIS qualified method invocation. */
12449
12450 static tree
12451 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12452 int use_this;
12453 tree name, args;
12454 int lloc, rloc;
12455 {
12456 tree invok;
12457 tree wfl =
12458 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12459 EXPR_WFL_LINECOL (wfl) = lloc;
12460 invok = build_method_invocation (name, args);
12461 return make_qualified_primary (wfl, invok, rloc);
12462 }
12463
12464 /* Build an incomplete CALL_EXPR node. */
12465
12466 static tree
12467 build_method_invocation (name, args)
12468 tree name;
12469 tree args;
12470 {
12471 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12472 TREE_SIDE_EFFECTS (call) = 1;
12473 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12474 return call;
12475 }
12476
12477 /* Build an incomplete new xxx(...) node. */
12478
12479 static tree
12480 build_new_invocation (name, args)
12481 tree name, args;
12482 {
12483 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12484 TREE_SIDE_EFFECTS (call) = 1;
12485 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12486 return call;
12487 }
12488
12489 /* Build an incomplete assignment expression. */
12490
12491 static tree
12492 build_assignment (op, op_location, lhs, rhs)
12493 int op, op_location;
12494 tree lhs, rhs;
12495 {
12496 tree assignment;
12497 /* Build the corresponding binop if we deal with a Compound
12498 Assignment operator. Mark the binop sub-tree as part of a
12499 Compound Assignment expression */
12500 if (op != ASSIGN_TK)
12501 {
12502 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12503 COMPOUND_ASSIGN_P (rhs) = 1;
12504 }
12505 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12506 TREE_SIDE_EFFECTS (assignment) = 1;
12507 EXPR_WFL_LINECOL (assignment) = op_location;
12508 return assignment;
12509 }
12510
12511 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12512
12513 char *
12514 print_int_node (node)
12515 tree node;
12516 {
12517 static char buffer [80];
12518 if (TREE_CONSTANT_OVERFLOW (node))
12519 sprintf (buffer, "<overflow>");
12520
12521 if (TREE_INT_CST_HIGH (node) == 0)
12522 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12523 TREE_INT_CST_LOW (node));
12524 else if (TREE_INT_CST_HIGH (node) == -1
12525 && TREE_INT_CST_LOW (node) != 0)
12526 {
12527 buffer [0] = '-';
12528 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12529 -TREE_INT_CST_LOW (node));
12530 }
12531 else
12532 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12533 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12534
12535 return buffer;
12536 }
12537
12538 \f
12539 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12540 context. */
12541
12542 /* 15.25 Assignment operators. */
12543
12544 static tree
12545 patch_assignment (node, wfl_op1)
12546 tree node;
12547 tree wfl_op1;
12548 {
12549 tree rhs = TREE_OPERAND (node, 1);
12550 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12551 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12552 int error_found = 0;
12553 int lvalue_from_array = 0;
12554 int is_return = 0;
12555
12556 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12557
12558 /* Lhs can be a named variable */
12559 if (JDECL_P (lvalue))
12560 {
12561 lhs_type = TREE_TYPE (lvalue);
12562 }
12563 /* Or Lhs can be a array access. Should that be lvalue ? FIXME +
12564 comment on reason why */
12565 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12566 {
12567 lhs_type = TREE_TYPE (lvalue);
12568 lvalue_from_array = 1;
12569 }
12570 /* Or a field access */
12571 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12572 lhs_type = TREE_TYPE (lvalue);
12573 /* Or a function return slot */
12574 else if (TREE_CODE (lvalue) == RESULT_DECL)
12575 {
12576 /* If the return type is an integral type, then we create the
12577 RESULT_DECL with a promoted type, but we need to do these
12578 checks against the unpromoted type to ensure type safety. So
12579 here we look at the real type, not the type of the decl we
12580 are modifying. */
12581 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12582 is_return = 1;
12583 }
12584 /* Otherwise, we might want to try to write into an optimized static
12585 final, this is an of a different nature, reported further on. */
12586 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12587 && resolve_expression_name (wfl_op1, &llvalue))
12588 {
12589 lhs_type = TREE_TYPE (lvalue);
12590 }
12591 else
12592 {
12593 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12594 error_found = 1;
12595 }
12596
12597 rhs_type = TREE_TYPE (rhs);
12598
12599 /* 5.1 Try the assignment conversion for builtin type. */
12600 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12601
12602 /* 5.2 If it failed, try a reference conversion */
12603 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12604 lhs_type = promote_type (rhs_type);
12605
12606 /* 15.25.2 If we have a compound assignment, convert RHS into the
12607 type of the LHS */
12608 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12609 new_rhs = convert (lhs_type, rhs);
12610
12611 /* Explicit cast required. This is an error */
12612 if (!new_rhs)
12613 {
12614 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12615 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12616 tree wfl;
12617 char operation [32]; /* Max size known */
12618
12619 /* If the assignment is part of a declaration, we use the WFL of
12620 the declared variable to point out the error and call it a
12621 declaration problem. If the assignment is a genuine =
12622 operator, we call is a operator `=' problem, otherwise we
12623 call it an assignment problem. In both of these last cases,
12624 we use the WFL of the operator to indicate the error. */
12625
12626 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12627 {
12628 wfl = wfl_op1;
12629 strcpy (operation, "declaration");
12630 }
12631 else
12632 {
12633 wfl = wfl_operator;
12634 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12635 strcpy (operation, "assignment");
12636 else if (is_return)
12637 strcpy (operation, "`return'");
12638 else
12639 strcpy (operation, "`='");
12640 }
12641
12642 if (!valid_cast_to_p (rhs_type, lhs_type))
12643 parse_error_context
12644 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12645 operation, t1, t2);
12646 else
12647 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12648 operation, t1, t2);
12649 free (t1); free (t2);
12650 error_found = 1;
12651 }
12652
12653 if (error_found)
12654 return error_mark_node;
12655
12656 /* If we're processing a `return' statement, promote the actual type
12657 to the promoted type. */
12658 if (is_return)
12659 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12660
12661 /* 10.10: Array Store Exception runtime check */
12662 if (!flag_emit_class_files
12663 && !flag_emit_xref
12664 && lvalue_from_array
12665 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12666 {
12667 tree check;
12668 tree base = lvalue;
12669
12670 /* We need to retrieve the right argument for
12671 _Jv_CheckArrayStore. This is somewhat complicated by bounds
12672 and null pointer checks, both of which wrap the operand in
12673 one layer of COMPOUND_EXPR. */
12674 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12675 base = TREE_OPERAND (lvalue, 0);
12676 else
12677 {
12678 tree op = TREE_OPERAND (base, 0);
12679
12680 /* We can have a SAVE_EXPR here when doing String +=. */
12681 if (TREE_CODE (op) == SAVE_EXPR)
12682 op = TREE_OPERAND (op, 0);
12683 /* We can have a COMPOUND_EXPR here when doing bounds check. */
12684 if (TREE_CODE (op) == COMPOUND_EXPR)
12685 op = TREE_OPERAND (op, 1);
12686 base = TREE_OPERAND (op, 0);
12687 /* Strip the last PLUS_EXPR to obtain the base. */
12688 if (TREE_CODE (base) == PLUS_EXPR)
12689 base = TREE_OPERAND (base, 0);
12690 }
12691
12692 /* Build the invocation of _Jv_CheckArrayStore */
12693 new_rhs = save_expr (new_rhs);
12694 check = build (CALL_EXPR, void_type_node,
12695 build_address_of (soft_checkarraystore_node),
12696 tree_cons (NULL_TREE, base,
12697 build_tree_list (NULL_TREE, new_rhs)),
12698 NULL_TREE);
12699 TREE_SIDE_EFFECTS (check) = 1;
12700
12701 /* We have to decide on an insertion point */
12702 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12703 {
12704 tree t;
12705 if (flag_bounds_check)
12706 {
12707 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12708 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12709 build (COMPOUND_EXPR, void_type_node, t, check);
12710 }
12711 else
12712 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12713 check, TREE_OPERAND (lvalue, 1));
12714 }
12715 else if (flag_bounds_check)
12716 {
12717 tree hook = lvalue;
12718 tree compound = TREE_OPERAND (lvalue, 0);
12719 tree bound_check, new_compound;
12720
12721 if (TREE_CODE (compound) == SAVE_EXPR)
12722 {
12723 compound = TREE_OPERAND (compound, 0);
12724 hook = TREE_OPERAND (hook, 0);
12725 }
12726
12727 /* Find the array bound check, hook the original array access. */
12728 bound_check = TREE_OPERAND (compound, 0);
12729 TREE_OPERAND (hook, 0) = TREE_OPERAND (compound, 1);
12730
12731 /* Make sure the bound check will happen before the store check */
12732 new_compound =
12733 build (COMPOUND_EXPR, void_type_node, bound_check, check);
12734
12735 /* Re-assemble the augmented array access. */
12736 lvalue = build (COMPOUND_EXPR, TREE_TYPE (lvalue),
12737 new_compound, lvalue);
12738 }
12739 else
12740 lvalue = build (COMPOUND_EXPR, TREE_TYPE (lvalue), check, lvalue);
12741 }
12742
12743 /* Final locals can be used as case values in switch
12744 statement. Prepare them for this eventuality. */
12745 if (TREE_CODE (lvalue) == VAR_DECL
12746 && DECL_FINAL (lvalue)
12747 && TREE_CONSTANT (new_rhs)
12748 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12749 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12750 )
12751 {
12752 TREE_CONSTANT (lvalue) = 1;
12753 DECL_INITIAL (lvalue) = new_rhs;
12754 }
12755
12756 TREE_OPERAND (node, 0) = lvalue;
12757 TREE_OPERAND (node, 1) = new_rhs;
12758 TREE_TYPE (node) = lhs_type;
12759 return node;
12760 }
12761
12762 /* Check that type SOURCE can be cast into type DEST. If the cast
12763 can't occur at all, return NULL; otherwise, return a possibly
12764 modified rhs. */
12765
12766 static tree
12767 try_reference_assignconv (lhs_type, rhs)
12768 tree lhs_type, rhs;
12769 {
12770 tree new_rhs = NULL_TREE;
12771 tree rhs_type = TREE_TYPE (rhs);
12772
12773 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12774 {
12775 /* `null' may be assigned to any reference type */
12776 if (rhs == null_pointer_node)
12777 new_rhs = null_pointer_node;
12778 /* Try the reference assignment conversion */
12779 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12780 new_rhs = rhs;
12781 /* This is a magic assignment that we process differently */
12782 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12783 new_rhs = rhs;
12784 }
12785 return new_rhs;
12786 }
12787
12788 /* Check that RHS can be converted into LHS_TYPE by the assignment
12789 conversion (5.2), for the cases of RHS being a builtin type. Return
12790 NULL_TREE if the conversion fails or if because RHS isn't of a
12791 builtin type. Return a converted RHS if the conversion is possible. */
12792
12793 static tree
12794 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12795 tree wfl_op1, lhs_type, rhs;
12796 {
12797 tree new_rhs = NULL_TREE;
12798 tree rhs_type = TREE_TYPE (rhs);
12799
12800 /* Handle boolean specially. */
12801 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12802 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12803 {
12804 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12805 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12806 new_rhs = rhs;
12807 }
12808
12809 /* Zero accepted everywhere */
12810 else if (TREE_CODE (rhs) == INTEGER_CST
12811 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12812 && JPRIMITIVE_TYPE_P (rhs_type))
12813 new_rhs = convert (lhs_type, rhs);
12814
12815 /* 5.1.1 Try Identity Conversion,
12816 5.1.2 Try Widening Primitive Conversion */
12817 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12818 new_rhs = convert (lhs_type, rhs);
12819
12820 /* Try a narrowing primitive conversion (5.1.3):
12821 - expression is a constant expression of type int AND
12822 - variable is byte, short or char AND
12823 - The value of the expression is representable in the type of the
12824 variable */
12825 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12826 && (lhs_type == byte_type_node || lhs_type == char_type_node
12827 || lhs_type == short_type_node))
12828 {
12829 if (int_fits_type_p (rhs, lhs_type))
12830 new_rhs = convert (lhs_type, rhs);
12831 else if (wfl_op1) /* Might be called with a NULL */
12832 parse_warning_context
12833 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12834 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12835 /* Reported a warning that will turn into an error further
12836 down, so we don't return */
12837 }
12838
12839 return new_rhs;
12840 }
12841
12842 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12843 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12844 0 is the conversion test fails. This implements parts the method
12845 invocation convertion (5.3). */
12846
12847 static int
12848 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12849 tree lhs_type, rhs_type;
12850 {
12851 /* 5.1.1: This is the identity conversion part. */
12852 if (lhs_type == rhs_type)
12853 return 1;
12854
12855 /* Reject non primitive types and boolean conversions. */
12856 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12857 return 0;
12858
12859 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12860 than a char can't be converted into a char. Short can't too, but
12861 the < test below takes care of that */
12862 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12863 return 0;
12864
12865 /* Accept all promoted type here. Note, we can't use <= in the test
12866 below, because we still need to bounce out assignments of short
12867 to char and the likes */
12868 if (lhs_type == int_type_node
12869 && (rhs_type == promoted_byte_type_node
12870 || rhs_type == promoted_short_type_node
12871 || rhs_type == promoted_char_type_node
12872 || rhs_type == promoted_boolean_type_node))
12873 return 1;
12874
12875 /* From here, an integral is widened if its precision is smaller
12876 than the precision of the LHS or if the LHS is a floating point
12877 type, or the RHS is a float and the RHS a double. */
12878 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12879 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12880 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12881 || (rhs_type == float_type_node && lhs_type == double_type_node))
12882 return 1;
12883
12884 return 0;
12885 }
12886
12887 /* Check that something of SOURCE type can be assigned or cast to
12888 something of DEST type at runtime. Return 1 if the operation is
12889 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12890 were SOURCE is cast into DEST, which borrows a lot of the
12891 assignment check. */
12892
12893 static int
12894 valid_ref_assignconv_cast_p (source, dest, cast)
12895 tree source;
12896 tree dest;
12897 int cast;
12898 {
12899 /* SOURCE or DEST might be null if not from a declared entity. */
12900 if (!source || !dest)
12901 return 0;
12902 if (JNULLP_TYPE_P (source))
12903 return 1;
12904 if (TREE_CODE (source) == POINTER_TYPE)
12905 source = TREE_TYPE (source);
12906 if (TREE_CODE (dest) == POINTER_TYPE)
12907 dest = TREE_TYPE (dest);
12908
12909 /* If source and dest are being compiled from bytecode, they may need to
12910 be loaded. */
12911 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12912 {
12913 load_class (source, 1);
12914 safe_layout_class (source);
12915 }
12916 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12917 {
12918 load_class (dest, 1);
12919 safe_layout_class (dest);
12920 }
12921
12922 /* Case where SOURCE is a class type */
12923 if (TYPE_CLASS_P (source))
12924 {
12925 if (TYPE_CLASS_P (dest))
12926 return (source == dest
12927 || inherits_from_p (source, dest)
12928 || (cast && inherits_from_p (dest, source)));
12929 if (TYPE_INTERFACE_P (dest))
12930 {
12931 /* If doing a cast and SOURCE is final, the operation is
12932 always correct a compile time (because even if SOURCE
12933 does not implement DEST, a subclass of SOURCE might). */
12934 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12935 return 1;
12936 /* Otherwise, SOURCE must implement DEST */
12937 return interface_of_p (dest, source);
12938 }
12939 /* DEST is an array, cast permited if SOURCE is of Object type */
12940 return (cast && source == object_type_node ? 1 : 0);
12941 }
12942 if (TYPE_INTERFACE_P (source))
12943 {
12944 if (TYPE_CLASS_P (dest))
12945 {
12946 /* If not casting, DEST must be the Object type */
12947 if (!cast)
12948 return dest == object_type_node;
12949 /* We're doing a cast. The cast is always valid is class
12950 DEST is not final, otherwise, DEST must implement SOURCE */
12951 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12952 return 1;
12953 else
12954 return interface_of_p (source, dest);
12955 }
12956 if (TYPE_INTERFACE_P (dest))
12957 {
12958 /* If doing a cast, then if SOURCE and DEST contain method
12959 with the same signature but different return type, then
12960 this is a (compile time) error */
12961 if (cast)
12962 {
12963 tree method_source, method_dest;
12964 tree source_type;
12965 tree source_sig;
12966 tree source_name;
12967 for (method_source = TYPE_METHODS (source); method_source;
12968 method_source = TREE_CHAIN (method_source))
12969 {
12970 source_sig =
12971 build_java_argument_signature (TREE_TYPE (method_source));
12972 source_type = TREE_TYPE (TREE_TYPE (method_source));
12973 source_name = DECL_NAME (method_source);
12974 for (method_dest = TYPE_METHODS (dest);
12975 method_dest; method_dest = TREE_CHAIN (method_dest))
12976 if (source_sig ==
12977 build_java_argument_signature (TREE_TYPE (method_dest))
12978 && source_name == DECL_NAME (method_dest)
12979 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12980 return 0;
12981 }
12982 return 1;
12983 }
12984 else
12985 return source == dest || interface_of_p (dest, source);
12986 }
12987 else
12988 {
12989 /* Array */
12990 return (cast
12991 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12992 || (DECL_NAME (TYPE_NAME (source))
12993 == java_io_serializable)));
12994 }
12995 }
12996 if (TYPE_ARRAY_P (source))
12997 {
12998 if (TYPE_CLASS_P (dest))
12999 return dest == object_type_node;
13000 /* Can't cast an array to an interface unless the interface is
13001 java.lang.Cloneable or java.io.Serializable. */
13002 if (TYPE_INTERFACE_P (dest))
13003 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
13004 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
13005 else /* Arrays */
13006 {
13007 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13008 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13009
13010 /* In case of severe errors, they turn out null */
13011 if (!dest_element_type || !source_element_type)
13012 return 0;
13013 if (source_element_type == dest_element_type)
13014 return 1;
13015 return valid_ref_assignconv_cast_p (source_element_type,
13016 dest_element_type, cast);
13017 }
13018 return 0;
13019 }
13020 return 0;
13021 }
13022
13023 static int
13024 valid_cast_to_p (source, dest)
13025 tree source;
13026 tree dest;
13027 {
13028 if (TREE_CODE (source) == POINTER_TYPE)
13029 source = TREE_TYPE (source);
13030 if (TREE_CODE (dest) == POINTER_TYPE)
13031 dest = TREE_TYPE (dest);
13032
13033 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13034 return valid_ref_assignconv_cast_p (source, dest, 1);
13035
13036 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13037 return 1;
13038
13039 else if (TREE_CODE (source) == BOOLEAN_TYPE
13040 && TREE_CODE (dest) == BOOLEAN_TYPE)
13041 return 1;
13042
13043 return 0;
13044 }
13045
13046 static tree
13047 do_unary_numeric_promotion (arg)
13048 tree arg;
13049 {
13050 tree type = TREE_TYPE (arg);
13051 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13052 || TREE_CODE (type) == CHAR_TYPE)
13053 arg = convert (int_type_node, arg);
13054 return arg;
13055 }
13056
13057 /* Return a non zero value if SOURCE can be converted into DEST using
13058 the method invocation conversion rule (5.3). */
13059 static int
13060 valid_method_invocation_conversion_p (dest, source)
13061 tree dest, source;
13062 {
13063 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13064 && valid_builtin_assignconv_identity_widening_p (dest, source))
13065 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13066 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13067 && valid_ref_assignconv_cast_p (source, dest, 0)));
13068 }
13069
13070 /* Build an incomplete binop expression. */
13071
13072 static tree
13073 build_binop (op, op_location, op1, op2)
13074 enum tree_code op;
13075 int op_location;
13076 tree op1, op2;
13077 {
13078 tree binop = build (op, NULL_TREE, op1, op2);
13079 TREE_SIDE_EFFECTS (binop) = 1;
13080 /* Store the location of the operator, for better error report. The
13081 string of the operator will be rebuild based on the OP value. */
13082 EXPR_WFL_LINECOL (binop) = op_location;
13083 return binop;
13084 }
13085
13086 /* Build the string of the operator retained by NODE. If NODE is part
13087 of a compound expression, add an '=' at the end of the string. This
13088 function is called when an error needs to be reported on an
13089 operator. The string is returned as a pointer to a static character
13090 buffer. */
13091
13092 static char *
13093 operator_string (node)
13094 tree node;
13095 {
13096 #define BUILD_OPERATOR_STRING(S) \
13097 { \
13098 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13099 return buffer; \
13100 }
13101
13102 static char buffer [10];
13103 switch (TREE_CODE (node))
13104 {
13105 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13106 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13107 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13108 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13109 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13110 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13111 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13112 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13113 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13114 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13115 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13116 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13117 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13118 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13119 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13120 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13121 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13122 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13123 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13124 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13125 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13126 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13127 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13128 case PREINCREMENT_EXPR: /* Fall through */
13129 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13130 case PREDECREMENT_EXPR: /* Fall through */
13131 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13132 default:
13133 internal_error ("unregistered operator %s",
13134 tree_code_name [TREE_CODE (node)]);
13135 }
13136 return NULL;
13137 #undef BUILD_OPERATOR_STRING
13138 }
13139
13140 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13141
13142 static int
13143 java_decl_equiv (var_acc1, var_acc2)
13144 tree var_acc1, var_acc2;
13145 {
13146 if (JDECL_P (var_acc1))
13147 return (var_acc1 == var_acc2);
13148
13149 return (TREE_CODE (var_acc1) == COMPONENT_REF
13150 && TREE_CODE (var_acc2) == COMPONENT_REF
13151 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13152 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13153 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13154 }
13155
13156 /* Return a non zero value if CODE is one of the operators that can be
13157 used in conjunction with the `=' operator in a compound assignment. */
13158
13159 static int
13160 binop_compound_p (code)
13161 enum tree_code code;
13162 {
13163 int i;
13164 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13165 if (binop_lookup [i] == code)
13166 break;
13167
13168 return i < BINOP_COMPOUND_CANDIDATES;
13169 }
13170
13171 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13172
13173 static tree
13174 java_refold (t)
13175 tree t;
13176 {
13177 tree c, b, ns, decl;
13178
13179 if (TREE_CODE (t) != MODIFY_EXPR)
13180 return t;
13181
13182 c = TREE_OPERAND (t, 1);
13183 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13184 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13185 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13186 return t;
13187
13188 /* Now the left branch of the binary operator. */
13189 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13190 if (! (b && TREE_CODE (b) == NOP_EXPR
13191 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13192 return t;
13193
13194 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13195 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13196 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13197 return t;
13198
13199 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13200 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13201 /* It's got to be the an equivalent decl */
13202 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13203 {
13204 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13205 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13206 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13207 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13208 /* Change the right part of the BINOP_EXPR */
13209 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13210 }
13211
13212 return t;
13213 }
13214
13215 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13216 errors but we modify NODE so that it contains the type computed
13217 according to the expression, when it's fixed. Otherwise, we write
13218 error_mark_node as the type. It allows us to further the analysis
13219 of remaining nodes and detects more errors in certain cases. */
13220
13221 static tree
13222 patch_binop (node, wfl_op1, wfl_op2)
13223 tree node;
13224 tree wfl_op1;
13225 tree wfl_op2;
13226 {
13227 tree op1 = TREE_OPERAND (node, 0);
13228 tree op2 = TREE_OPERAND (node, 1);
13229 tree op1_type = TREE_TYPE (op1);
13230 tree op2_type = TREE_TYPE (op2);
13231 tree prom_type = NULL_TREE, cn;
13232 enum tree_code code = TREE_CODE (node);
13233
13234 /* If 1, tell the routine that we have to return error_mark_node
13235 after checking for the initialization of the RHS */
13236 int error_found = 0;
13237
13238 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13239
13240 /* If either op<n>_type are NULL, this might be early signs of an
13241 error situation, unless it's too early to tell (in case we're
13242 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13243 correctly so the error can be later on reported accurately. */
13244 if (! (code == PLUS_EXPR || code == NE_EXPR
13245 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13246 {
13247 tree n;
13248 if (! op1_type)
13249 {
13250 n = java_complete_tree (op1);
13251 op1_type = TREE_TYPE (n);
13252 }
13253 if (! op2_type)
13254 {
13255 n = java_complete_tree (op2);
13256 op2_type = TREE_TYPE (n);
13257 }
13258 }
13259
13260 switch (code)
13261 {
13262 /* 15.16 Multiplicative operators */
13263 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13264 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13265 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13266 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13267 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13268 {
13269 if (!JNUMERIC_TYPE_P (op1_type))
13270 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13271 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13272 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13273 TREE_TYPE (node) = error_mark_node;
13274 error_found = 1;
13275 break;
13276 }
13277 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13278
13279 /* Detect integral division by zero */
13280 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13281 && TREE_CODE (prom_type) == INTEGER_TYPE
13282 && (op2 == integer_zero_node || op2 == long_zero_node ||
13283 (TREE_CODE (op2) == INTEGER_CST &&
13284 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13285 {
13286 parse_warning_context (wfl_operator, "Evaluating this expression will result in an arithmetic exception being thrown");
13287 TREE_CONSTANT (node) = 0;
13288 }
13289
13290 /* Change the division operator if necessary */
13291 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13292 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13293
13294 /* Before divisions as is disapear, try to simplify and bail if
13295 applicable, otherwise we won't perform even simple
13296 simplifications like (1-1)/3. We can't do that with floating
13297 point number, folds can't handle them at this stage. */
13298 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13299 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13300 {
13301 TREE_TYPE (node) = prom_type;
13302 node = fold (node);
13303 if (TREE_CODE (node) != code)
13304 return node;
13305 }
13306
13307 if (TREE_CODE (prom_type) == INTEGER_TYPE
13308 && flag_use_divide_subroutine
13309 && ! flag_emit_class_files
13310 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13311 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13312
13313 /* This one is more complicated. FLOATs are processed by a
13314 function call to soft_fmod. Duplicate the value of the
13315 COMPOUND_ASSIGN_P flag. */
13316 if (code == TRUNC_MOD_EXPR)
13317 {
13318 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13319 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13320 TREE_SIDE_EFFECTS (mod)
13321 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13322 return mod;
13323 }
13324 break;
13325
13326 /* 15.17 Additive Operators */
13327 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13328
13329 /* Operation is valid if either one argument is a string
13330 constant, a String object or a StringBuffer crafted for the
13331 purpose of the a previous usage of the String concatenation
13332 operator */
13333
13334 if (TREE_CODE (op1) == STRING_CST
13335 || TREE_CODE (op2) == STRING_CST
13336 || JSTRING_TYPE_P (op1_type)
13337 || JSTRING_TYPE_P (op2_type)
13338 || IS_CRAFTED_STRING_BUFFER_P (op1)
13339 || IS_CRAFTED_STRING_BUFFER_P (op2))
13340 return build_string_concatenation (op1, op2);
13341
13342 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13343 Numeric Types */
13344 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13345 {
13346 if (!JNUMERIC_TYPE_P (op1_type))
13347 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13348 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13349 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13350 TREE_TYPE (node) = error_mark_node;
13351 error_found = 1;
13352 break;
13353 }
13354 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13355 break;
13356
13357 /* 15.18 Shift Operators */
13358 case LSHIFT_EXPR:
13359 case RSHIFT_EXPR:
13360 case URSHIFT_EXPR:
13361 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13362 {
13363 if (!JINTEGRAL_TYPE_P (op1_type))
13364 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13365 else
13366 {
13367 if (JNUMERIC_TYPE_P (op2_type))
13368 parse_error_context (wfl_operator,
13369 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13370 operator_string (node),
13371 lang_printable_name (op2_type, 0));
13372 else
13373 parse_error_context (wfl_operator,
13374 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13375 operator_string (node),
13376 lang_printable_name (op2_type, 0));
13377 }
13378 TREE_TYPE (node) = error_mark_node;
13379 error_found = 1;
13380 break;
13381 }
13382
13383 /* Unary numeric promotion (5.6.1) is performed on each operand
13384 separately */
13385 op1 = do_unary_numeric_promotion (op1);
13386 op2 = do_unary_numeric_promotion (op2);
13387
13388 /* The type of the shift expression is the type of the promoted
13389 type of the left-hand operand */
13390 prom_type = TREE_TYPE (op1);
13391
13392 /* Shift int only up to 0x1f and long up to 0x3f */
13393 if (prom_type == int_type_node)
13394 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13395 build_int_2 (0x1f, 0)));
13396 else
13397 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13398 build_int_2 (0x3f, 0)));
13399
13400 /* The >>> operator is a >> operating on unsigned quantities */
13401 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13402 {
13403 tree to_return;
13404 tree utype = unsigned_type (prom_type);
13405 op1 = convert (utype, op1);
13406 TREE_SET_CODE (node, RSHIFT_EXPR);
13407 TREE_OPERAND (node, 0) = op1;
13408 TREE_OPERAND (node, 1) = op2;
13409 TREE_TYPE (node) = utype;
13410 to_return = convert (prom_type, node);
13411 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13412 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13413 TREE_SIDE_EFFECTS (to_return)
13414 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13415 return to_return;
13416 }
13417 break;
13418
13419 /* 15.19.1 Type Comparison Operator instaceof */
13420 case INSTANCEOF_EXPR:
13421
13422 TREE_TYPE (node) = boolean_type_node;
13423
13424 if (!(op2_type = resolve_type_during_patch (op2)))
13425 return error_mark_node;
13426
13427 /* The first operand must be a reference type or the null type */
13428 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13429 error_found = 1; /* Error reported further below */
13430
13431 /* The second operand must be a reference type */
13432 if (!JREFERENCE_TYPE_P (op2_type))
13433 {
13434 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13435 parse_error_context
13436 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13437 lang_printable_name (op2_type, 0));
13438 error_found = 1;
13439 }
13440
13441 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13442 {
13443 /* If the first operand is null, the result is always false */
13444 if (op1 == null_pointer_node)
13445 return boolean_false_node;
13446 else if (flag_emit_class_files)
13447 {
13448 TREE_OPERAND (node, 1) = op2_type;
13449 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13450 return node;
13451 }
13452 /* Otherwise we have to invoke instance of to figure it out */
13453 else
13454 return build_instanceof (op1, op2_type);
13455 }
13456 /* There is no way the expression operand can be an instance of
13457 the type operand. This is a compile time error. */
13458 else
13459 {
13460 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13461 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13462 parse_error_context
13463 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13464 t1, lang_printable_name (op2_type, 0));
13465 free (t1);
13466 error_found = 1;
13467 }
13468
13469 break;
13470
13471 /* 15.21 Bitwise and Logical Operators */
13472 case BIT_AND_EXPR:
13473 case BIT_XOR_EXPR:
13474 case BIT_IOR_EXPR:
13475 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13476 /* Binary numeric promotion is performed on both operand and the
13477 expression retain that type */
13478 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13479
13480 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13481 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13482 /* The type of the bitwise operator expression is BOOLEAN */
13483 prom_type = boolean_type_node;
13484 else
13485 {
13486 if (!JINTEGRAL_TYPE_P (op1_type))
13487 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13488 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13489 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13490 TREE_TYPE (node) = error_mark_node;
13491 error_found = 1;
13492 /* Insert a break here if adding thing before the switch's
13493 break for this case */
13494 }
13495 break;
13496
13497 /* 15.22 Conditional-And Operator */
13498 case TRUTH_ANDIF_EXPR:
13499 /* 15.23 Conditional-Or Operator */
13500 case TRUTH_ORIF_EXPR:
13501 /* Operands must be of BOOLEAN type */
13502 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13503 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13504 {
13505 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13506 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13507 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13508 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13509 TREE_TYPE (node) = boolean_type_node;
13510 error_found = 1;
13511 break;
13512 }
13513 else if (integer_zerop (op1))
13514 {
13515 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13516 }
13517 else if (integer_onep (op1))
13518 {
13519 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13520 }
13521 /* The type of the conditional operators is BOOLEAN */
13522 prom_type = boolean_type_node;
13523 break;
13524
13525 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13526 case LT_EXPR:
13527 case GT_EXPR:
13528 case LE_EXPR:
13529 case GE_EXPR:
13530 /* The type of each of the operands must be a primitive numeric
13531 type */
13532 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13533 {
13534 if (!JNUMERIC_TYPE_P (op1_type))
13535 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13536 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13537 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13538 TREE_TYPE (node) = boolean_type_node;
13539 error_found = 1;
13540 break;
13541 }
13542 /* Binary numeric promotion is performed on the operands */
13543 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13544 /* The type of the relation expression is always BOOLEAN */
13545 prom_type = boolean_type_node;
13546 break;
13547
13548 /* 15.20 Equality Operator */
13549 case EQ_EXPR:
13550 case NE_EXPR:
13551 /* It's time for us to patch the strings. */
13552 if ((cn = patch_string (op1)))
13553 {
13554 op1 = cn;
13555 op1_type = TREE_TYPE (op1);
13556 }
13557 if ((cn = patch_string (op2)))
13558 {
13559 op2 = cn;
13560 op2_type = TREE_TYPE (op2);
13561 }
13562
13563 /* 15.20.1 Numerical Equality Operators == and != */
13564 /* Binary numeric promotion is performed on the operands */
13565 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13566 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13567
13568 /* 15.20.2 Boolean Equality Operators == and != */
13569 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13570 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13571 ; /* Nothing to do here */
13572
13573 /* 15.20.3 Reference Equality Operators == and != */
13574 /* Types have to be either references or the null type. If
13575 they're references, it must be possible to convert either
13576 type to the other by casting conversion. */
13577 else if (op1 == null_pointer_node || op2 == null_pointer_node
13578 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13579 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13580 || valid_ref_assignconv_cast_p (op2_type,
13581 op1_type, 1))))
13582 ; /* Nothing to do here */
13583
13584 /* Else we have an error figure what can't be converted into
13585 what and report the error */
13586 else
13587 {
13588 char *t1;
13589 t1 = xstrdup (lang_printable_name (op1_type, 0));
13590 parse_error_context
13591 (wfl_operator,
13592 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13593 operator_string (node), t1,
13594 lang_printable_name (op2_type, 0));
13595 free (t1);
13596 TREE_TYPE (node) = boolean_type_node;
13597 error_found = 1;
13598 break;
13599 }
13600 prom_type = boolean_type_node;
13601 break;
13602 default:
13603 abort ();
13604 }
13605
13606 if (error_found)
13607 return error_mark_node;
13608
13609 TREE_OPERAND (node, 0) = op1;
13610 TREE_OPERAND (node, 1) = op2;
13611 TREE_TYPE (node) = prom_type;
13612 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13613
13614 if (flag_emit_xref)
13615 return node;
13616
13617 /* fold does not respect side-effect order as required for Java but not C.
13618 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13619 * bytecode.
13620 */
13621 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13622 : ! TREE_SIDE_EFFECTS (node))
13623 node = fold (node);
13624 return node;
13625 }
13626
13627 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13628 zero value, the value of CSTE comes after the valude of STRING */
13629
13630 static tree
13631 do_merge_string_cste (cste, string, string_len, after)
13632 tree cste;
13633 const char *string;
13634 int string_len, after;
13635 {
13636 const char *old = TREE_STRING_POINTER (cste);
13637 int old_len = TREE_STRING_LENGTH (cste);
13638 int len = old_len + string_len;
13639 char *new = alloca (len+1);
13640
13641 if (after)
13642 {
13643 memcpy (new, string, string_len);
13644 memcpy (&new [string_len], old, old_len);
13645 }
13646 else
13647 {
13648 memcpy (new, old, old_len);
13649 memcpy (&new [old_len], string, string_len);
13650 }
13651 new [len] = '\0';
13652 return build_string (len, new);
13653 }
13654
13655 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13656 new STRING_CST on success, NULL_TREE on failure */
13657
13658 static tree
13659 merge_string_cste (op1, op2, after)
13660 tree op1, op2;
13661 int after;
13662 {
13663 /* Handle two string constants right away */
13664 if (TREE_CODE (op2) == STRING_CST)
13665 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13666 TREE_STRING_LENGTH (op2), after);
13667
13668 /* Reasonable integer constant can be treated right away */
13669 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13670 {
13671 static const char *const boolean_true = "true";
13672 static const char *const boolean_false = "false";
13673 static const char *const null_pointer = "null";
13674 char ch[3];
13675 const char *string;
13676
13677 if (op2 == boolean_true_node)
13678 string = boolean_true;
13679 else if (op2 == boolean_false_node)
13680 string = boolean_false;
13681 else if (op2 == null_pointer_node)
13682 string = null_pointer;
13683 else if (TREE_TYPE (op2) == char_type_node)
13684 {
13685 ch[0] = (char )TREE_INT_CST_LOW (op2);
13686 ch[1] = '\0';
13687 string = ch;
13688 }
13689 else
13690 string = print_int_node (op2);
13691
13692 return do_merge_string_cste (op1, string, strlen (string), after);
13693 }
13694 return NULL_TREE;
13695 }
13696
13697 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13698 has to be a STRING_CST and the other part must be a STRING_CST or a
13699 INTEGRAL constant. Return a new STRING_CST if the operation
13700 succeed, NULL_TREE otherwise.
13701
13702 If the case we want to optimize for space, we might want to return
13703 NULL_TREE for each invocation of this routine. FIXME */
13704
13705 static tree
13706 string_constant_concatenation (op1, op2)
13707 tree op1, op2;
13708 {
13709 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13710 {
13711 tree string, rest;
13712 int invert;
13713
13714 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13715 rest = (string == op1 ? op2 : op1);
13716 invert = (string == op1 ? 0 : 1 );
13717
13718 /* Walk REST, only if it looks reasonable */
13719 if (TREE_CODE (rest) != STRING_CST
13720 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13721 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13722 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13723 {
13724 rest = java_complete_tree (rest);
13725 if (rest == error_mark_node)
13726 return error_mark_node;
13727 rest = fold (rest);
13728 }
13729 return merge_string_cste (string, rest, invert);
13730 }
13731 return NULL_TREE;
13732 }
13733
13734 /* Implement the `+' operator. Does static optimization if possible,
13735 otherwise create (if necessary) and append elements to a
13736 StringBuffer. The StringBuffer will be carried around until it is
13737 used for a function call or an assignment. Then toString() will be
13738 called on it to turn it into a String object. */
13739
13740 static tree
13741 build_string_concatenation (op1, op2)
13742 tree op1, op2;
13743 {
13744 tree result;
13745 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13746
13747 if (flag_emit_xref)
13748 return build (PLUS_EXPR, string_type_node, op1, op2);
13749
13750 /* Try to do some static optimization */
13751 if ((result = string_constant_concatenation (op1, op2)))
13752 return result;
13753
13754 /* Discard empty strings on either side of the expression */
13755 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13756 {
13757 op1 = op2;
13758 op2 = NULL_TREE;
13759 }
13760 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13761 op2 = NULL_TREE;
13762
13763 /* If operands are string constant, turn then into object references */
13764 if (TREE_CODE (op1) == STRING_CST)
13765 op1 = patch_string_cst (op1);
13766 if (op2 && TREE_CODE (op2) == STRING_CST)
13767 op2 = patch_string_cst (op2);
13768
13769 /* If either one of the constant is null and the other non null
13770 operand is a String object, return it. */
13771 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13772 return op1;
13773
13774 /* If OP1 isn't already a StringBuffer, create and
13775 initialize a new one */
13776 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13777 {
13778 /* Two solutions here:
13779 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13780 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13781 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13782 op1 = BUILD_STRING_BUFFER (op1);
13783 else
13784 {
13785 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13786 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13787 }
13788 }
13789
13790 if (op2)
13791 {
13792 /* OP1 is no longer the last node holding a crafted StringBuffer */
13793 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13794 /* Create a node for `{new...,xxx}.append (op2)' */
13795 if (op2)
13796 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13797 }
13798
13799 /* Mark the last node holding a crafted StringBuffer */
13800 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13801
13802 TREE_SIDE_EFFECTS (op1) = side_effects;
13803 return op1;
13804 }
13805
13806 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13807 StringBuffer. If no string were found to be patched, return
13808 NULL. */
13809
13810 static tree
13811 patch_string (node)
13812 tree node;
13813 {
13814 if (node == error_mark_node)
13815 return error_mark_node;
13816 if (TREE_CODE (node) == STRING_CST)
13817 return patch_string_cst (node);
13818 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13819 {
13820 int saved = ctxp->explicit_constructor_p;
13821 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13822 tree ret;
13823 /* Temporary disable forbid the use of `this'. */
13824 ctxp->explicit_constructor_p = 0;
13825 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13826 /* String concatenation arguments must be evaluated in order too. */
13827 ret = force_evaluation_order (ret);
13828 /* Restore it at its previous value */
13829 ctxp->explicit_constructor_p = saved;
13830 return ret;
13831 }
13832 return NULL_TREE;
13833 }
13834
13835 /* Build the internal representation of a string constant. */
13836
13837 static tree
13838 patch_string_cst (node)
13839 tree node;
13840 {
13841 int location;
13842 if (! flag_emit_class_files)
13843 {
13844 node = get_identifier (TREE_STRING_POINTER (node));
13845 location = alloc_name_constant (CONSTANT_String, node);
13846 node = build_ref_from_constant_pool (location);
13847 }
13848 TREE_TYPE (node) = string_ptr_type_node;
13849 TREE_CONSTANT (node) = 1;
13850 return node;
13851 }
13852
13853 /* Build an incomplete unary operator expression. */
13854
13855 static tree
13856 build_unaryop (op_token, op_location, op1)
13857 int op_token, op_location;
13858 tree op1;
13859 {
13860 enum tree_code op;
13861 tree unaryop;
13862 switch (op_token)
13863 {
13864 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13865 case MINUS_TK: op = NEGATE_EXPR; break;
13866 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13867 case NOT_TK: op = BIT_NOT_EXPR; break;
13868 default: abort ();
13869 }
13870
13871 unaryop = build1 (op, NULL_TREE, op1);
13872 TREE_SIDE_EFFECTS (unaryop) = 1;
13873 /* Store the location of the operator, for better error report. The
13874 string of the operator will be rebuild based on the OP value. */
13875 EXPR_WFL_LINECOL (unaryop) = op_location;
13876 return unaryop;
13877 }
13878
13879 /* Special case for the ++/-- operators, since they require an extra
13880 argument to build, which is set to NULL and patched
13881 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13882
13883 static tree
13884 build_incdec (op_token, op_location, op1, is_post_p)
13885 int op_token, op_location;
13886 tree op1;
13887 int is_post_p;
13888 {
13889 static const enum tree_code lookup [2][2] =
13890 {
13891 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13892 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13893 };
13894 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13895 NULL_TREE, op1, NULL_TREE);
13896 TREE_SIDE_EFFECTS (node) = 1;
13897 /* Store the location of the operator, for better error report. The
13898 string of the operator will be rebuild based on the OP value. */
13899 EXPR_WFL_LINECOL (node) = op_location;
13900 return node;
13901 }
13902
13903 /* Build an incomplete cast operator, based on the use of the
13904 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13905 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13906 though its type is already set. */
13907
13908 static tree
13909 build_cast (location, type, exp)
13910 int location;
13911 tree type, exp;
13912 {
13913 tree node = build1 (CONVERT_EXPR, type, exp);
13914 EXPR_WFL_LINECOL (node) = location;
13915 return node;
13916 }
13917
13918 /* Build an incomplete class reference operator. */
13919 static tree
13920 build_incomplete_class_ref (location, class_name)
13921 int location;
13922 tree class_name;
13923 {
13924 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13925 EXPR_WFL_LINECOL (node) = location;
13926 return node;
13927 }
13928
13929 /* Complete an incomplete class reference operator. */
13930 static tree
13931 patch_incomplete_class_ref (node)
13932 tree node;
13933 {
13934 tree type = TREE_OPERAND (node, 0);
13935 tree ref_type;
13936
13937 if (!(ref_type = resolve_type_during_patch (type)))
13938 return error_mark_node;
13939
13940 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13941 {
13942 tree dot = build_class_ref (ref_type);
13943 /* A class referenced by `foo.class' is initialized. */
13944 if (!flag_emit_class_files)
13945 dot = build_class_init (ref_type, dot);
13946 return java_complete_tree (dot);
13947 }
13948
13949 /* If we're emitting class files and we have to deal with non
13950 primitive types, we invoke (and consider generating) the
13951 synthetic static method `class$'. */
13952 if (!TYPE_DOT_CLASS (current_class))
13953 build_dot_class_method (current_class);
13954 ref_type = build_dot_class_method_invocation (ref_type);
13955 return java_complete_tree (ref_type);
13956 }
13957
13958 /* 15.14 Unary operators. We return error_mark_node in case of error,
13959 but preserve the type of NODE if the type is fixed. */
13960
13961 static tree
13962 patch_unaryop (node, wfl_op)
13963 tree node;
13964 tree wfl_op;
13965 {
13966 tree op = TREE_OPERAND (node, 0);
13967 tree op_type = TREE_TYPE (op);
13968 tree prom_type = NULL_TREE, value, decl;
13969 int outer_field_flag = 0;
13970 int code = TREE_CODE (node);
13971 int error_found = 0;
13972
13973 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13974
13975 switch (code)
13976 {
13977 /* 15.13.2 Postfix Increment Operator ++ */
13978 case POSTINCREMENT_EXPR:
13979 /* 15.13.3 Postfix Increment Operator -- */
13980 case POSTDECREMENT_EXPR:
13981 /* 15.14.1 Prefix Increment Operator ++ */
13982 case PREINCREMENT_EXPR:
13983 /* 15.14.2 Prefix Decrement Operator -- */
13984 case PREDECREMENT_EXPR:
13985 op = decl = strip_out_static_field_access_decl (op);
13986 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13987 /* We might be trying to change an outer field accessed using
13988 access method. */
13989 if (outer_field_flag)
13990 {
13991 /* Retrieve the decl of the field we're trying to access. We
13992 do that by first retrieving the function we would call to
13993 access the field. It has been already verified that this
13994 field isn't final */
13995 if (flag_emit_class_files)
13996 decl = TREE_OPERAND (op, 0);
13997 else
13998 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13999 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14000 }
14001 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14002 else if (!JDECL_P (decl)
14003 && TREE_CODE (decl) != COMPONENT_REF
14004 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14005 && TREE_CODE (decl) != INDIRECT_REF
14006 && !(TREE_CODE (decl) == COMPOUND_EXPR
14007 && TREE_OPERAND (decl, 1)
14008 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14009 {
14010 TREE_TYPE (node) = error_mark_node;
14011 error_found = 1;
14012 }
14013
14014 /* From now on, we know that op if a variable and that it has a
14015 valid wfl. We use wfl_op to locate errors related to the
14016 ++/-- operand. */
14017 if (!JNUMERIC_TYPE_P (op_type))
14018 {
14019 parse_error_context
14020 (wfl_op, "Invalid argument type `%s' to `%s'",
14021 lang_printable_name (op_type, 0), operator_string (node));
14022 TREE_TYPE (node) = error_mark_node;
14023 error_found = 1;
14024 }
14025 else
14026 {
14027 /* Before the addition, binary numeric promotion is performed on
14028 both operands, if really necessary */
14029 if (JINTEGRAL_TYPE_P (op_type))
14030 {
14031 value = build_int_2 (1, 0);
14032 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
14033 }
14034 else
14035 {
14036 value = build_int_2 (1, 0);
14037 TREE_TYPE (node) =
14038 binary_numeric_promotion (op_type,
14039 TREE_TYPE (value), &op, &value);
14040 }
14041
14042 /* We remember we might be accessing an outer field */
14043 if (outer_field_flag)
14044 {
14045 /* We re-generate an access to the field */
14046 value = build (PLUS_EXPR, TREE_TYPE (op),
14047 build_outer_field_access (wfl_op, decl), value);
14048
14049 /* And we patch the original access$() into a write
14050 with plus_op as a rhs */
14051 return outer_field_access_fix (node, op, value);
14052 }
14053
14054 /* And write back into the node. */
14055 TREE_OPERAND (node, 0) = op;
14056 TREE_OPERAND (node, 1) = value;
14057 /* Convert the overall back into its original type, if
14058 necessary, and return */
14059 if (JINTEGRAL_TYPE_P (op_type))
14060 return fold (node);
14061 else
14062 return fold (convert (op_type, node));
14063 }
14064 break;
14065
14066 /* 15.14.3 Unary Plus Operator + */
14067 case UNARY_PLUS_EXPR:
14068 /* 15.14.4 Unary Minus Operator - */
14069 case NEGATE_EXPR:
14070 if (!JNUMERIC_TYPE_P (op_type))
14071 {
14072 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14073 TREE_TYPE (node) = error_mark_node;
14074 error_found = 1;
14075 }
14076 /* Unary numeric promotion is performed on operand */
14077 else
14078 {
14079 op = do_unary_numeric_promotion (op);
14080 prom_type = TREE_TYPE (op);
14081 if (code == UNARY_PLUS_EXPR)
14082 return fold (op);
14083 }
14084 break;
14085
14086 /* 15.14.5 Bitwise Complement Operator ~ */
14087 case BIT_NOT_EXPR:
14088 if (!JINTEGRAL_TYPE_P (op_type))
14089 {
14090 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14091 TREE_TYPE (node) = error_mark_node;
14092 error_found = 1;
14093 }
14094 else
14095 {
14096 op = do_unary_numeric_promotion (op);
14097 prom_type = TREE_TYPE (op);
14098 }
14099 break;
14100
14101 /* 15.14.6 Logical Complement Operator ! */
14102 case TRUTH_NOT_EXPR:
14103 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14104 {
14105 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14106 /* But the type is known. We will report an error if further
14107 attempt of a assignment is made with this rhs */
14108 TREE_TYPE (node) = boolean_type_node;
14109 error_found = 1;
14110 }
14111 else
14112 prom_type = boolean_type_node;
14113 break;
14114
14115 /* 15.15 Cast Expression */
14116 case CONVERT_EXPR:
14117 value = patch_cast (node, wfl_operator);
14118 if (value == error_mark_node)
14119 {
14120 /* If this cast is part of an assignment, we tell the code
14121 that deals with it not to complain about a mismatch,
14122 because things have been cast, anyways */
14123 TREE_TYPE (node) = error_mark_node;
14124 error_found = 1;
14125 }
14126 else
14127 {
14128 value = fold (value);
14129 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14130 return value;
14131 }
14132 break;
14133 }
14134
14135 if (error_found)
14136 return error_mark_node;
14137
14138 /* There are cases where node has been replaced by something else
14139 and we don't end up returning here: UNARY_PLUS_EXPR,
14140 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14141 TREE_OPERAND (node, 0) = fold (op);
14142 TREE_TYPE (node) = prom_type;
14143 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14144 return fold (node);
14145 }
14146
14147 /* Generic type resolution that sometimes takes place during node
14148 patching. Returned the resolved type or generate an error
14149 message. Return the resolved type or NULL_TREE. */
14150
14151 static tree
14152 resolve_type_during_patch (type)
14153 tree type;
14154 {
14155 if (unresolved_type_p (type, NULL))
14156 {
14157 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14158 if (!type_decl)
14159 {
14160 parse_error_context (type,
14161 "Class `%s' not found in type declaration",
14162 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14163 return NULL_TREE;
14164 }
14165 return TREE_TYPE (type_decl);
14166 }
14167 return type;
14168 }
14169 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14170 found. Otherwise NODE or something meant to replace it is returned. */
14171
14172 static tree
14173 patch_cast (node, wfl_op)
14174 tree node;
14175 tree wfl_op;
14176 {
14177 tree op = TREE_OPERAND (node, 0);
14178 tree cast_type = TREE_TYPE (node);
14179 tree patched, op_type;
14180 char *t1;
14181
14182 /* Some string patching might be necessary at this stage */
14183 if ((patched = patch_string (op)))
14184 TREE_OPERAND (node, 0) = op = patched;
14185 op_type = TREE_TYPE (op);
14186
14187 /* First resolve OP_TYPE if unresolved */
14188 if (!(cast_type = resolve_type_during_patch (cast_type)))
14189 return error_mark_node;
14190
14191 /* Check on cast that are proven correct at compile time */
14192 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14193 {
14194 /* Same type */
14195 if (cast_type == op_type)
14196 return node;
14197
14198 /* float and double type are converted to the original type main
14199 variant and then to the target type. */
14200 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14201 op = convert (integer_type_node, op);
14202
14203 /* Try widening/narowwing convertion. Potentially, things need
14204 to be worked out in gcc so we implement the extreme cases
14205 correctly. fold_convert() needs to be fixed. */
14206 return convert (cast_type, op);
14207 }
14208
14209 /* It's also valid to cast a boolean into a boolean */
14210 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14211 return node;
14212
14213 /* null can be casted to references */
14214 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14215 return build_null_of_type (cast_type);
14216
14217 /* The remaining legal casts involve conversion between reference
14218 types. Check for their compile time correctness. */
14219 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14220 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14221 {
14222 TREE_TYPE (node) = promote_type (cast_type);
14223 /* Now, the case can be determined correct at compile time if
14224 OP_TYPE can be converted into CAST_TYPE by assignment
14225 conversion (5.2) */
14226
14227 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14228 {
14229 TREE_SET_CODE (node, NOP_EXPR);
14230 return node;
14231 }
14232
14233 if (flag_emit_class_files)
14234 {
14235 TREE_SET_CODE (node, CONVERT_EXPR);
14236 return node;
14237 }
14238
14239 /* The cast requires a run-time check */
14240 return build (CALL_EXPR, promote_type (cast_type),
14241 build_address_of (soft_checkcast_node),
14242 tree_cons (NULL_TREE, build_class_ref (cast_type),
14243 build_tree_list (NULL_TREE, op)),
14244 NULL_TREE);
14245 }
14246
14247 /* Any other casts are proven incorrect at compile time */
14248 t1 = xstrdup (lang_printable_name (op_type, 0));
14249 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14250 t1, lang_printable_name (cast_type, 0));
14251 free (t1);
14252 return error_mark_node;
14253 }
14254
14255 /* Build a null constant and give it the type TYPE. */
14256
14257 static tree
14258 build_null_of_type (type)
14259 tree type;
14260 {
14261 tree node = build_int_2 (0, 0);
14262 TREE_TYPE (node) = promote_type (type);
14263 return node;
14264 }
14265
14266 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14267 a list of indices. */
14268 static tree
14269 build_array_ref (location, array, index)
14270 int location;
14271 tree array, index;
14272 {
14273 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14274 EXPR_WFL_LINECOL (node) = location;
14275 return node;
14276 }
14277
14278 /* 15.12 Array Access Expression */
14279
14280 static tree
14281 patch_array_ref (node)
14282 tree node;
14283 {
14284 tree array = TREE_OPERAND (node, 0);
14285 tree array_type = TREE_TYPE (array);
14286 tree index = TREE_OPERAND (node, 1);
14287 tree index_type = TREE_TYPE (index);
14288 int error_found = 0;
14289
14290 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14291
14292 if (TREE_CODE (array_type) == POINTER_TYPE)
14293 array_type = TREE_TYPE (array_type);
14294
14295 /* The array reference must be an array */
14296 if (!TYPE_ARRAY_P (array_type))
14297 {
14298 parse_error_context
14299 (wfl_operator,
14300 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14301 lang_printable_name (array_type, 0));
14302 TREE_TYPE (node) = error_mark_node;
14303 error_found = 1;
14304 }
14305
14306 /* The array index undergoes unary numeric promotion. The promoted
14307 type must be int */
14308 index = do_unary_numeric_promotion (index);
14309 if (TREE_TYPE (index) != int_type_node)
14310 {
14311 if (valid_cast_to_p (index_type, int_type_node))
14312 parse_error_context (wfl_operator,
14313 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14314 lang_printable_name (index_type, 0));
14315 else
14316 parse_error_context (wfl_operator,
14317 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14318 lang_printable_name (index_type, 0));
14319 TREE_TYPE (node) = error_mark_node;
14320 error_found = 1;
14321 }
14322
14323 if (error_found)
14324 return error_mark_node;
14325
14326 array_type = TYPE_ARRAY_ELEMENT (array_type);
14327
14328 if (flag_emit_class_files || flag_emit_xref)
14329 {
14330 TREE_OPERAND (node, 0) = array;
14331 TREE_OPERAND (node, 1) = index;
14332 }
14333 else
14334 node = build_java_arrayaccess (array, array_type, index);
14335 TREE_TYPE (node) = array_type;
14336 return node;
14337 }
14338
14339 /* 15.9 Array Creation Expressions */
14340
14341 static tree
14342 build_newarray_node (type, dims, extra_dims)
14343 tree type;
14344 tree dims;
14345 int extra_dims;
14346 {
14347 tree node =
14348 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14349 build_int_2 (extra_dims, 0));
14350 return node;
14351 }
14352
14353 static tree
14354 patch_newarray (node)
14355 tree node;
14356 {
14357 tree type = TREE_OPERAND (node, 0);
14358 tree dims = TREE_OPERAND (node, 1);
14359 tree cdim, array_type;
14360 int error_found = 0;
14361 int ndims = 0;
14362 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14363
14364 /* Dimension types are verified. It's better for the types to be
14365 verified in order. */
14366 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14367 {
14368 int dim_error = 0;
14369 tree dim = TREE_VALUE (cdim);
14370
14371 /* Dim might have been saved during its evaluation */
14372 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14373
14374 /* The type of each specified dimension must be an integral type. */
14375 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14376 dim_error = 1;
14377
14378 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14379 promoted type must be int. */
14380 else
14381 {
14382 dim = do_unary_numeric_promotion (dim);
14383 if (TREE_TYPE (dim) != int_type_node)
14384 dim_error = 1;
14385 }
14386
14387 /* Report errors on types here */
14388 if (dim_error)
14389 {
14390 parse_error_context
14391 (TREE_PURPOSE (cdim),
14392 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14393 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14394 "Explicit cast needed to" : "Can't"),
14395 lang_printable_name (TREE_TYPE (dim), 0));
14396 error_found = 1;
14397 }
14398
14399 TREE_PURPOSE (cdim) = NULL_TREE;
14400 }
14401
14402 /* Resolve array base type if unresolved */
14403 if (!(type = resolve_type_during_patch (type)))
14404 error_found = 1;
14405
14406 if (error_found)
14407 {
14408 /* We don't want further evaluation of this bogus array creation
14409 operation */
14410 TREE_TYPE (node) = error_mark_node;
14411 return error_mark_node;
14412 }
14413
14414 /* Set array_type to the actual (promoted) array type of the result. */
14415 if (TREE_CODE (type) == RECORD_TYPE)
14416 type = build_pointer_type (type);
14417 while (--xdims >= 0)
14418 {
14419 type = promote_type (build_java_array_type (type, -1));
14420 }
14421 dims = nreverse (dims);
14422 array_type = type;
14423 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14424 {
14425 type = array_type;
14426 array_type
14427 = build_java_array_type (type,
14428 TREE_CODE (cdim) == INTEGER_CST
14429 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14430 : -1);
14431 array_type = promote_type (array_type);
14432 }
14433 dims = nreverse (dims);
14434
14435 /* The node is transformed into a function call. Things are done
14436 differently according to the number of dimensions. If the number
14437 of dimension is equal to 1, then the nature of the base type
14438 (primitive or not) matters. */
14439 if (ndims == 1)
14440 return build_new_array (type, TREE_VALUE (dims));
14441
14442 /* Can't reuse what's already written in expr.c because it uses the
14443 JVM stack representation. Provide a build_multianewarray. FIXME */
14444 return build (CALL_EXPR, array_type,
14445 build_address_of (soft_multianewarray_node),
14446 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14447 tree_cons (NULL_TREE,
14448 build_int_2 (ndims, 0), dims )),
14449 NULL_TREE);
14450 }
14451
14452 /* 10.6 Array initializer. */
14453
14454 /* Build a wfl for array element that don't have one, so we can
14455 pin-point errors. */
14456
14457 static tree
14458 maybe_build_array_element_wfl (node)
14459 tree node;
14460 {
14461 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14462 return build_expr_wfl (NULL_TREE, ctxp->filename,
14463 ctxp->elc.line, ctxp->elc.prev_col);
14464 else
14465 return NULL_TREE;
14466 }
14467
14468 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14469 identification of initialized arrays easier to detect during walk
14470 and expansion. */
14471
14472 static tree
14473 build_new_array_init (location, values)
14474 int location;
14475 tree values;
14476 {
14477 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14478 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14479 EXPR_WFL_LINECOL (to_return) = location;
14480 return to_return;
14481 }
14482
14483 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14484 occurred. Otherwise return NODE after having set its type
14485 appropriately. */
14486
14487 static tree
14488 patch_new_array_init (type, node)
14489 tree type, node;
14490 {
14491 int error_seen = 0;
14492 tree current, element_type;
14493 HOST_WIDE_INT length;
14494 int all_constant = 1;
14495 tree init = TREE_OPERAND (node, 0);
14496
14497 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14498 {
14499 parse_error_context (node,
14500 "Invalid array initializer for non-array type `%s'",
14501 lang_printable_name (type, 1));
14502 return error_mark_node;
14503 }
14504 type = TREE_TYPE (type);
14505 element_type = TYPE_ARRAY_ELEMENT (type);
14506
14507 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14508
14509 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14510 current; length++, current = TREE_CHAIN (current))
14511 {
14512 tree elt = TREE_VALUE (current);
14513 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14514 {
14515 error_seen |= array_constructor_check_entry (element_type, current);
14516 elt = TREE_VALUE (current);
14517 /* When compiling to native code, STRING_CST is converted to
14518 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14519 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14520 all_constant = 0;
14521 }
14522 else
14523 {
14524 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14525 TREE_PURPOSE (current) = NULL_TREE;
14526 all_constant = 0;
14527 }
14528 if (elt && TREE_CODE (elt) == TREE_LIST
14529 && TREE_VALUE (elt) == error_mark_node)
14530 error_seen = 1;
14531 }
14532
14533 if (error_seen)
14534 return error_mark_node;
14535
14536 /* Create a new type. We can't reuse the one we have here by
14537 patching its dimension because it originally is of dimension -1
14538 hence reused by gcc. This would prevent triangular arrays. */
14539 type = build_java_array_type (element_type, length);
14540 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14541 TREE_TYPE (node) = promote_type (type);
14542 TREE_CONSTANT (init) = all_constant;
14543 TREE_CONSTANT (node) = all_constant;
14544 return node;
14545 }
14546
14547 /* Verify that one entry of the initializer element list can be
14548 assigned to the array base type. Report 1 if an error occurred, 0
14549 otherwise. */
14550
14551 static int
14552 array_constructor_check_entry (type, entry)
14553 tree type, entry;
14554 {
14555 char *array_type_string = NULL; /* For error reports */
14556 tree value, type_value, new_value, wfl_value, patched;
14557 int error_seen = 0;
14558
14559 new_value = NULL_TREE;
14560 wfl_value = TREE_VALUE (entry);
14561
14562 value = java_complete_tree (TREE_VALUE (entry));
14563 /* patch_string return error_mark_node if arg is error_mark_node */
14564 if ((patched = patch_string (value)))
14565 value = patched;
14566 if (value == error_mark_node)
14567 return 1;
14568
14569 type_value = TREE_TYPE (value);
14570
14571 /* At anytime, try_builtin_assignconv can report a warning on
14572 constant overflow during narrowing. */
14573 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14574 new_value = try_builtin_assignconv (wfl_operator, type, value);
14575 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14576 type_value = promote_type (type);
14577
14578 /* Check and report errors */
14579 if (!new_value)
14580 {
14581 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14582 "Can't" : "Explicit cast needed to");
14583 if (!array_type_string)
14584 array_type_string = xstrdup (lang_printable_name (type, 1));
14585 parse_error_context
14586 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14587 msg, lang_printable_name (type_value, 1), array_type_string);
14588 error_seen = 1;
14589 }
14590
14591 if (new_value)
14592 TREE_VALUE (entry) = new_value;
14593
14594 if (array_type_string)
14595 free (array_type_string);
14596
14597 TREE_PURPOSE (entry) = NULL_TREE;
14598 return error_seen;
14599 }
14600
14601 static tree
14602 build_this (location)
14603 int location;
14604 {
14605 tree node = build_wfl_node (this_identifier_node);
14606 TREE_SET_CODE (node, THIS_EXPR);
14607 EXPR_WFL_LINECOL (node) = location;
14608 return node;
14609 }
14610
14611 /* 14.15 The return statement. It builds a modify expression that
14612 assigns the returned value to the RESULT_DECL that hold the value
14613 to be returned. */
14614
14615 static tree
14616 build_return (location, op)
14617 int location;
14618 tree op;
14619 {
14620 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14621 EXPR_WFL_LINECOL (node) = location;
14622 node = build_debugable_stmt (location, node);
14623 return node;
14624 }
14625
14626 static tree
14627 patch_return (node)
14628 tree node;
14629 {
14630 tree return_exp = TREE_OPERAND (node, 0);
14631 tree meth = current_function_decl;
14632 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14633 int error_found = 0;
14634
14635 TREE_TYPE (node) = error_mark_node;
14636 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14637
14638 /* It's invalid to have a return value within a function that is
14639 declared with the keyword void or that is a constructor */
14640 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14641 error_found = 1;
14642
14643 /* It's invalid to use a return statement in a static block */
14644 if (DECL_CLINIT_P (current_function_decl))
14645 error_found = 1;
14646
14647 /* It's invalid to have a no return value within a function that
14648 isn't declared with the keyword `void' */
14649 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14650 error_found = 2;
14651
14652 if (DECL_INSTINIT_P (current_function_decl))
14653 error_found = 1;
14654
14655 if (error_found)
14656 {
14657 if (DECL_INSTINIT_P (current_function_decl))
14658 parse_error_context (wfl_operator,
14659 "`return' inside instance initializer");
14660
14661 else if (DECL_CLINIT_P (current_function_decl))
14662 parse_error_context (wfl_operator,
14663 "`return' inside static initializer");
14664
14665 else if (!DECL_CONSTRUCTOR_P (meth))
14666 {
14667 char *t = xstrdup (lang_printable_name (mtype, 0));
14668 parse_error_context (wfl_operator,
14669 "`return' with%s value from `%s %s'",
14670 (error_found == 1 ? "" : "out"),
14671 t, lang_printable_name (meth, 0));
14672 free (t);
14673 }
14674 else
14675 parse_error_context (wfl_operator,
14676 "`return' with value from constructor `%s'",
14677 lang_printable_name (meth, 0));
14678 return error_mark_node;
14679 }
14680
14681 /* If we have a return_exp, build a modify expression and expand
14682 it. Note: at that point, the assignment is declared valid, but we
14683 may want to carry some more hacks */
14684 if (return_exp)
14685 {
14686 tree exp = java_complete_tree (return_exp);
14687 tree modify, patched;
14688
14689 if ((patched = patch_string (exp)))
14690 exp = patched;
14691
14692 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14693 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14694 modify = java_complete_tree (modify);
14695
14696 if (modify != error_mark_node)
14697 {
14698 TREE_SIDE_EFFECTS (modify) = 1;
14699 TREE_OPERAND (node, 0) = modify;
14700 }
14701 else
14702 return error_mark_node;
14703 }
14704 TREE_TYPE (node) = void_type_node;
14705 TREE_SIDE_EFFECTS (node) = 1;
14706 return node;
14707 }
14708
14709 /* 14.8 The if Statement */
14710
14711 static tree
14712 build_if_else_statement (location, expression, if_body, else_body)
14713 int location;
14714 tree expression, if_body, else_body;
14715 {
14716 tree node;
14717 if (!else_body)
14718 else_body = empty_stmt_node;
14719 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14720 EXPR_WFL_LINECOL (node) = location;
14721 node = build_debugable_stmt (location, node);
14722 return node;
14723 }
14724
14725 static tree
14726 patch_if_else_statement (node)
14727 tree node;
14728 {
14729 tree expression = TREE_OPERAND (node, 0);
14730 int can_complete_normally
14731 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14732 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14733
14734 TREE_TYPE (node) = error_mark_node;
14735 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14736
14737 /* The type of expression must be boolean */
14738 if (TREE_TYPE (expression) != boolean_type_node
14739 && TREE_TYPE (expression) != promoted_boolean_type_node)
14740 {
14741 parse_error_context
14742 (wfl_operator,
14743 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14744 lang_printable_name (TREE_TYPE (expression), 0));
14745 return error_mark_node;
14746 }
14747
14748 if (TREE_CODE (expression) == INTEGER_CST)
14749 {
14750 if (integer_zerop (expression))
14751 node = TREE_OPERAND (node, 2);
14752 else
14753 node = TREE_OPERAND (node, 1);
14754 if (CAN_COMPLETE_NORMALLY (node) != can_complete_normally)
14755 {
14756 node = build (COMPOUND_EXPR, void_type_node, node, empty_stmt_node);
14757 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14758 }
14759 return node;
14760 }
14761 TREE_TYPE (node) = void_type_node;
14762 TREE_SIDE_EFFECTS (node) = 1;
14763 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14764 return node;
14765 }
14766
14767 /* 14.6 Labeled Statements */
14768
14769 /* Action taken when a lableled statement is parsed. a new
14770 LABELED_BLOCK_EXPR is created. No statement is attached to the
14771 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14772
14773 static tree
14774 build_labeled_block (location, label)
14775 int location;
14776 tree label;
14777 {
14778 tree label_name ;
14779 tree label_decl, node;
14780 if (label == NULL_TREE || label == continue_identifier_node)
14781 label_name = label;
14782 else
14783 {
14784 label_name = merge_qualified_name (label_id, label);
14785 /* Issue an error if we try to reuse a label that was previously
14786 declared */
14787 if (IDENTIFIER_LOCAL_VALUE (label_name))
14788 {
14789 EXPR_WFL_LINECOL (wfl_operator) = location;
14790 parse_error_context (wfl_operator,
14791 "Declaration of `%s' shadows a previous label declaration",
14792 IDENTIFIER_POINTER (label));
14793 EXPR_WFL_LINECOL (wfl_operator) =
14794 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14795 parse_error_context (wfl_operator,
14796 "This is the location of the previous declaration of label `%s'",
14797 IDENTIFIER_POINTER (label));
14798 java_error_count--;
14799 }
14800 }
14801
14802 label_decl = create_label_decl (label_name);
14803 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14804 EXPR_WFL_LINECOL (node) = location;
14805 TREE_SIDE_EFFECTS (node) = 1;
14806 return node;
14807 }
14808
14809 /* A labeled statement LBE is attached a statement. */
14810
14811 static tree
14812 finish_labeled_statement (lbe, statement)
14813 tree lbe; /* Labeled block expr */
14814 tree statement;
14815 {
14816 /* In anyways, tie the loop to its statement */
14817 LABELED_BLOCK_BODY (lbe) = statement;
14818 pop_labeled_block ();
14819 POP_LABELED_BLOCK ();
14820 return lbe;
14821 }
14822
14823 /* 14.10, 14.11, 14.12 Loop Statements */
14824
14825 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14826 list. */
14827
14828 static tree
14829 build_new_loop (loop_body)
14830 tree loop_body;
14831 {
14832 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14833 TREE_SIDE_EFFECTS (loop) = 1;
14834 PUSH_LOOP (loop);
14835 return loop;
14836 }
14837
14838 /* Create a loop body according to the following structure:
14839 COMPOUND_EXPR
14840 COMPOUND_EXPR (loop main body)
14841 EXIT_EXPR (this order is for while/for loops.
14842 LABELED_BLOCK_EXPR the order is reversed for do loops)
14843 LABEL_DECL (a continue occurring here branches at the
14844 BODY end of this labeled block)
14845 INCREMENT (if any)
14846
14847 REVERSED, if non zero, tells that the loop condition expr comes
14848 after the body, like in the do-while loop.
14849
14850 To obtain a loop, the loop body structure described above is
14851 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14852
14853 LABELED_BLOCK_EXPR
14854 LABEL_DECL (use this label to exit the loop)
14855 LOOP_EXPR
14856 <structure described above> */
14857
14858 static tree
14859 build_loop_body (location, condition, reversed)
14860 int location;
14861 tree condition;
14862 int reversed;
14863 {
14864 tree first, second, body;
14865
14866 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14867 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14868 condition = build_debugable_stmt (location, condition);
14869 TREE_SIDE_EFFECTS (condition) = 1;
14870
14871 body = build_labeled_block (0, continue_identifier_node);
14872 first = (reversed ? body : condition);
14873 second = (reversed ? condition : body);
14874 return
14875 build (COMPOUND_EXPR, NULL_TREE,
14876 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14877 }
14878
14879 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14880 their order) on the current loop. Unlink the current loop from the
14881 loop list. */
14882
14883 static tree
14884 finish_loop_body (location, condition, body, reversed)
14885 int location;
14886 tree condition, body;
14887 int reversed;
14888 {
14889 tree to_return = ctxp->current_loop;
14890 tree loop_body = LOOP_EXPR_BODY (to_return);
14891 if (condition)
14892 {
14893 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14894 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14895 The real EXIT_EXPR is one operand further. */
14896 EXPR_WFL_LINECOL (cnode) = location;
14897 /* This one is for accurate error reports */
14898 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14899 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14900 }
14901 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14902 POP_LOOP ();
14903 return to_return;
14904 }
14905
14906 /* Tailored version of finish_loop_body for FOR loops, when FOR
14907 loops feature the condition part */
14908
14909 static tree
14910 finish_for_loop (location, condition, update, body)
14911 int location;
14912 tree condition, update, body;
14913 {
14914 /* Put the condition and the loop body in place */
14915 tree loop = finish_loop_body (location, condition, body, 0);
14916 /* LOOP is the current loop which has been now popped of the loop
14917 stack. Install the update block */
14918 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14919 return loop;
14920 }
14921
14922 /* Try to find the loop a block might be related to. This comprises
14923 the case where the LOOP_EXPR is found as the second operand of a
14924 COMPOUND_EXPR, because the loop happens to have an initialization
14925 part, then expressed as the first operand of the COMPOUND_EXPR. If
14926 the search finds something, 1 is returned. Otherwise, 0 is
14927 returned. The search is assumed to start from a
14928 LABELED_BLOCK_EXPR's block. */
14929
14930 static tree
14931 search_loop (statement)
14932 tree statement;
14933 {
14934 if (TREE_CODE (statement) == LOOP_EXPR)
14935 return statement;
14936
14937 if (TREE_CODE (statement) == BLOCK)
14938 statement = BLOCK_SUBBLOCKS (statement);
14939 else
14940 return NULL_TREE;
14941
14942 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14943 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14944 statement = TREE_OPERAND (statement, 1);
14945
14946 return (TREE_CODE (statement) == LOOP_EXPR
14947 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14948 }
14949
14950 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14951 returned otherwise. */
14952
14953 static int
14954 labeled_block_contains_loop_p (block, loop)
14955 tree block, loop;
14956 {
14957 if (!block)
14958 return 0;
14959
14960 if (LABELED_BLOCK_BODY (block) == loop)
14961 return 1;
14962
14963 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14964 return 1;
14965
14966 return 0;
14967 }
14968
14969 /* If the loop isn't surrounded by a labeled statement, create one and
14970 insert LOOP as its body. */
14971
14972 static tree
14973 patch_loop_statement (loop)
14974 tree loop;
14975 {
14976 tree loop_label;
14977
14978 TREE_TYPE (loop) = void_type_node;
14979 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14980 return loop;
14981
14982 loop_label = build_labeled_block (0, NULL_TREE);
14983 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14984 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14985 LABELED_BLOCK_BODY (loop_label) = loop;
14986 PUSH_LABELED_BLOCK (loop_label);
14987 return loop_label;
14988 }
14989
14990 /* 14.13, 14.14: break and continue Statements */
14991
14992 /* Build a break or a continue statement. a null NAME indicates an
14993 unlabeled break/continue statement. */
14994
14995 static tree
14996 build_bc_statement (location, is_break, name)
14997 int location, is_break;
14998 tree name;
14999 {
15000 tree break_continue, label_block_expr = NULL_TREE;
15001
15002 if (name)
15003 {
15004 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15005 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15006 /* Null means that we don't have a target for this named
15007 break/continue. In this case, we make the target to be the
15008 label name, so that the error can be reported accuratly in
15009 patch_bc_statement. */
15010 label_block_expr = EXPR_WFL_NODE (name);
15011 }
15012 /* Unlabeled break/continue will be handled during the
15013 break/continue patch operation */
15014 break_continue
15015 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
15016
15017 IS_BREAK_STMT_P (break_continue) = is_break;
15018 TREE_SIDE_EFFECTS (break_continue) = 1;
15019 EXPR_WFL_LINECOL (break_continue) = location;
15020 break_continue = build_debugable_stmt (location, break_continue);
15021 return break_continue;
15022 }
15023
15024 /* Verification of a break/continue statement. */
15025
15026 static tree
15027 patch_bc_statement (node)
15028 tree node;
15029 {
15030 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15031 tree labeled_block = ctxp->current_labeled_block;
15032 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15033
15034 /* Having an identifier here means that the target is unknown. */
15035 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15036 {
15037 parse_error_context (wfl_operator, "No label definition found for `%s'",
15038 IDENTIFIER_POINTER (bc_label));
15039 return error_mark_node;
15040 }
15041 if (! IS_BREAK_STMT_P (node))
15042 {
15043 /* It's a continue statement. */
15044 for (;; labeled_block = TREE_CHAIN (labeled_block))
15045 {
15046 if (labeled_block == NULL_TREE)
15047 {
15048 if (bc_label == NULL_TREE)
15049 parse_error_context (wfl_operator,
15050 "`continue' must be in loop");
15051 else
15052 parse_error_context
15053 (wfl_operator, "continue label `%s' does not name a loop",
15054 IDENTIFIER_POINTER (bc_label));
15055 return error_mark_node;
15056 }
15057 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15058 == continue_identifier_node)
15059 && (bc_label == NULL_TREE
15060 || TREE_CHAIN (labeled_block) == bc_label))
15061 {
15062 bc_label = labeled_block;
15063 break;
15064 }
15065 }
15066 }
15067 else if (!bc_label)
15068 {
15069 for (;; labeled_block = TREE_CHAIN (labeled_block))
15070 {
15071 if (labeled_block == NULL_TREE)
15072 {
15073 parse_error_context (wfl_operator,
15074 "`break' must be in loop or switch");
15075 return error_mark_node;
15076 }
15077 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15078 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15079 || search_loop (target_stmt))
15080 {
15081 bc_label = labeled_block;
15082 break;
15083 }
15084 }
15085 }
15086
15087 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15088 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15089
15090 /* Our break/continue don't return values. */
15091 TREE_TYPE (node) = void_type_node;
15092 /* Encapsulate the break within a compound statement so that it's
15093 expanded all the times by expand_expr (and not clobbered
15094 sometimes, like after a if statement) */
15095 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15096 TREE_SIDE_EFFECTS (node) = 1;
15097 return node;
15098 }
15099
15100 /* Process the exit expression belonging to a loop. Its type must be
15101 boolean. */
15102
15103 static tree
15104 patch_exit_expr (node)
15105 tree node;
15106 {
15107 tree expression = TREE_OPERAND (node, 0);
15108 TREE_TYPE (node) = error_mark_node;
15109 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15110
15111 /* The type of expression must be boolean */
15112 if (TREE_TYPE (expression) != boolean_type_node)
15113 {
15114 parse_error_context
15115 (wfl_operator,
15116 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
15117 lang_printable_name (TREE_TYPE (expression), 0));
15118 return error_mark_node;
15119 }
15120 /* Now we know things are allright, invert the condition, fold and
15121 return */
15122 TREE_OPERAND (node, 0) =
15123 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15124
15125 if (! integer_zerop (TREE_OPERAND (node, 0))
15126 && ctxp->current_loop != NULL_TREE
15127 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15128 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15129 if (! integer_onep (TREE_OPERAND (node, 0)))
15130 CAN_COMPLETE_NORMALLY (node) = 1;
15131
15132
15133 TREE_TYPE (node) = void_type_node;
15134 return node;
15135 }
15136
15137 /* 14.9 Switch statement */
15138
15139 static tree
15140 patch_switch_statement (node)
15141 tree node;
15142 {
15143 tree se = TREE_OPERAND (node, 0), se_type;
15144 tree save, iter;
15145
15146 /* Complete the switch expression */
15147 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15148 se_type = TREE_TYPE (se);
15149 /* The type of the switch expression must be char, byte, short or
15150 int */
15151 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15152 {
15153 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15154 parse_error_context (wfl_operator,
15155 "Incompatible type for `switch'. Can't convert `%s' to `int'",
15156 lang_printable_name (se_type, 0));
15157 /* This is what java_complete_tree will check */
15158 TREE_OPERAND (node, 0) = error_mark_node;
15159 return error_mark_node;
15160 }
15161
15162 /* Save and restore the outer case label list. */
15163 save = case_label_list;
15164 case_label_list = NULL_TREE;
15165
15166 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15167
15168 /* See if we've found a duplicate label. We can't leave this until
15169 code generation, because in `--syntax-only' and `-C' modes we
15170 don't do ordinary code generation. */
15171 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15172 {
15173 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15174 tree subiter;
15175 for (subiter = TREE_CHAIN (iter);
15176 subiter != NULL_TREE;
15177 subiter = TREE_CHAIN (subiter))
15178 {
15179 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15180 if (val == subval)
15181 {
15182 EXPR_WFL_LINECOL (wfl_operator)
15183 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15184 /* The case_label_list is in reverse order, so print the
15185 outer label first. */
15186 parse_error_context (wfl_operator, "duplicate case label: `%d'",
15187 subval);
15188 EXPR_WFL_LINECOL (wfl_operator)
15189 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15190 parse_error_context (wfl_operator, "original label is here");
15191
15192 break;
15193 }
15194 }
15195 }
15196
15197 case_label_list = save;
15198
15199 /* Ready to return */
15200 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15201 {
15202 TREE_TYPE (node) = error_mark_node;
15203 return error_mark_node;
15204 }
15205 TREE_TYPE (node) = void_type_node;
15206 TREE_SIDE_EFFECTS (node) = 1;
15207 CAN_COMPLETE_NORMALLY (node)
15208 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15209 || ! SWITCH_HAS_DEFAULT (node);
15210 return node;
15211 }
15212
15213 /* 14.18 The try/catch statements */
15214
15215 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15216 catches TYPE and executes CATCH_STMTS. */
15217
15218 static tree
15219 encapsulate_with_try_catch (location, type, try_stmts, catch_stmts)
15220 int location;
15221 tree type, try_stmts, catch_stmts;
15222 {
15223 tree try_block, catch_clause_param, catch_block, catch;
15224
15225 /* First build a try block */
15226 try_block = build_expr_block (try_stmts, NULL_TREE);
15227
15228 /* Build a catch block: we need a catch clause parameter */
15229 catch_clause_param = build_decl (VAR_DECL,
15230 wpv_id, build_pointer_type (type));
15231 /* And a block */
15232 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15233
15234 /* Initialize the variable and store in the block */
15235 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15236 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15237 add_stmt_to_block (catch_block, NULL_TREE, catch);
15238
15239 /* Add the catch statements */
15240 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15241
15242 /* Now we can build a CATCH_EXPR */
15243 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15244
15245 return build_try_statement (location, try_block, catch_block);
15246 }
15247
15248 static tree
15249 build_try_statement (location, try_block, catches)
15250 int location;
15251 tree try_block, catches;
15252 {
15253 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15254 EXPR_WFL_LINECOL (node) = location;
15255 return node;
15256 }
15257
15258 static tree
15259 build_try_finally_statement (location, try_block, finally)
15260 int location;
15261 tree try_block, finally;
15262 {
15263 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15264 EXPR_WFL_LINECOL (node) = location;
15265 return node;
15266 }
15267
15268 static tree
15269 patch_try_statement (node)
15270 tree node;
15271 {
15272 int error_found = 0;
15273 tree try = TREE_OPERAND (node, 0);
15274 /* Exception handlers are considered in left to right order */
15275 tree catch = nreverse (TREE_OPERAND (node, 1));
15276 tree current, caught_type_list = NULL_TREE;
15277
15278 /* Check catch clauses, if any. Every time we find an error, we try
15279 to process the next catch clause. We process the catch clause before
15280 the try block so that when processing the try block we can check thrown
15281 exceptions againts the caught type list. */
15282 for (current = catch; current; current = TREE_CHAIN (current))
15283 {
15284 tree carg_decl, carg_type;
15285 tree sub_current, catch_block, catch_clause;
15286 int unreachable;
15287
15288 /* At this point, the structure of the catch clause is
15289 CATCH_EXPR (catch node)
15290 BLOCK (with the decl of the parameter)
15291 COMPOUND_EXPR
15292 MODIFY_EXPR (assignment of the catch parameter)
15293 BLOCK (catch clause block)
15294 */
15295 catch_clause = TREE_OPERAND (current, 0);
15296 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15297 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15298
15299 /* Catch clauses can't have more than one parameter declared,
15300 but it's already enforced by the grammar. Make sure that the
15301 only parameter of the clause statement in of class Throwable
15302 or a subclass of Throwable, but that was done earlier. The
15303 catch clause parameter type has also been resolved. */
15304
15305 /* Just make sure that the catch clause parameter type inherits
15306 from java.lang.Throwable */
15307 if (!inherits_from_p (carg_type, throwable_type_node))
15308 {
15309 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15310 parse_error_context (wfl_operator,
15311 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15312 lang_printable_name (carg_type, 0));
15313 error_found = 1;
15314 continue;
15315 }
15316
15317 /* Partial check for unreachable catch statement: The catch
15318 clause is reachable iff is no earlier catch block A in
15319 the try statement such that the type of the catch
15320 clause's parameter is the same as or a subclass of the
15321 type of A's parameter */
15322 unreachable = 0;
15323 for (sub_current = catch;
15324 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15325 {
15326 tree sub_catch_clause, decl;
15327 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15328 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15329
15330 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15331 {
15332 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15333 parse_error_context
15334 (wfl_operator,
15335 "`catch' not reached because of the catch clause at line %d",
15336 EXPR_WFL_LINENO (sub_current));
15337 unreachable = error_found = 1;
15338 break;
15339 }
15340 }
15341 /* Complete the catch clause block */
15342 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15343 if (catch_block == error_mark_node)
15344 {
15345 error_found = 1;
15346 continue;
15347 }
15348 if (CAN_COMPLETE_NORMALLY (catch_block))
15349 CAN_COMPLETE_NORMALLY (node) = 1;
15350 TREE_OPERAND (current, 0) = catch_block;
15351
15352 if (unreachable)
15353 continue;
15354
15355 /* Things to do here: the exception must be thrown */
15356
15357 /* Link this type to the caught type list */
15358 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15359 }
15360
15361 PUSH_EXCEPTIONS (caught_type_list);
15362 if ((try = java_complete_tree (try)) == error_mark_node)
15363 error_found = 1;
15364 if (CAN_COMPLETE_NORMALLY (try))
15365 CAN_COMPLETE_NORMALLY (node) = 1;
15366 POP_EXCEPTIONS ();
15367
15368 /* Verification ends here */
15369 if (error_found)
15370 return error_mark_node;
15371
15372 TREE_OPERAND (node, 0) = try;
15373 TREE_OPERAND (node, 1) = catch;
15374 TREE_TYPE (node) = void_type_node;
15375 return node;
15376 }
15377
15378 /* 14.17 The synchronized Statement */
15379
15380 static tree
15381 patch_synchronized_statement (node, wfl_op1)
15382 tree node, wfl_op1;
15383 {
15384 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15385 tree block = TREE_OPERAND (node, 1);
15386
15387 tree tmp, enter, exit, expr_decl, assignment;
15388
15389 if (expr == error_mark_node)
15390 {
15391 block = java_complete_tree (block);
15392 return expr;
15393 }
15394
15395 /* We might be trying to synchronize on a STRING_CST */
15396 if ((tmp = patch_string (expr)))
15397 expr = tmp;
15398
15399 /* The TYPE of expr must be a reference type */
15400 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15401 {
15402 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15403 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15404 lang_printable_name (TREE_TYPE (expr), 0));
15405 return error_mark_node;
15406 }
15407
15408 if (flag_emit_xref)
15409 {
15410 TREE_OPERAND (node, 0) = expr;
15411 TREE_OPERAND (node, 1) = java_complete_tree (block);
15412 CAN_COMPLETE_NORMALLY (node) = 1;
15413 return node;
15414 }
15415
15416 /* Generate a try-finally for the synchronized statement, except
15417 that the handler that catches all throw exception calls
15418 _Jv_MonitorExit and then rethrow the exception.
15419 The synchronized statement is then implemented as:
15420 TRY
15421 {
15422 _Jv_MonitorEnter (expression)
15423 synchronized_block
15424 _Jv_MonitorExit (expression)
15425 }
15426 CATCH_ALL
15427 {
15428 e = _Jv_exception_info ();
15429 _Jv_MonitorExit (expression)
15430 Throw (e);
15431 } */
15432
15433 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15434 BUILD_MONITOR_ENTER (enter, expr_decl);
15435 BUILD_MONITOR_EXIT (exit, expr_decl);
15436 CAN_COMPLETE_NORMALLY (enter) = 1;
15437 CAN_COMPLETE_NORMALLY (exit) = 1;
15438 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15439 TREE_SIDE_EFFECTS (assignment) = 1;
15440 node = build (COMPOUND_EXPR, NULL_TREE,
15441 build (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15442 build (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15443 node = build_expr_block (node, expr_decl);
15444
15445 return java_complete_tree (node);
15446 }
15447
15448 /* 14.16 The throw Statement */
15449
15450 static tree
15451 patch_throw_statement (node, wfl_op1)
15452 tree node, wfl_op1;
15453 {
15454 tree expr = TREE_OPERAND (node, 0);
15455 tree type = TREE_TYPE (expr);
15456 int unchecked_ok = 0, tryblock_throws_ok = 0;
15457
15458 /* Thrown expression must be assignable to java.lang.Throwable */
15459 if (!try_reference_assignconv (throwable_type_node, expr))
15460 {
15461 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15462 parse_error_context (wfl_operator,
15463 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15464 lang_printable_name (type, 0));
15465 /* If the thrown expression was a reference, we further the
15466 compile-time check. */
15467 if (!JREFERENCE_TYPE_P (type))
15468 return error_mark_node;
15469 }
15470
15471 /* At least one of the following must be true */
15472
15473 /* The type of the throw expression is a not checked exception,
15474 i.e. is a unchecked expression. */
15475 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15476
15477 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15478 /* An instance can't throw a checked exception unless that exception
15479 is explicitly declared in the `throws' clause of each
15480 constructor. This doesn't apply to anonymous classes, since they
15481 don't have declared constructors. */
15482 if (!unchecked_ok
15483 && DECL_INSTINIT_P (current_function_decl)
15484 && !ANONYMOUS_CLASS_P (current_class))
15485 {
15486 tree current;
15487 for (current = TYPE_METHODS (current_class); current;
15488 current = TREE_CHAIN (current))
15489 if (DECL_CONSTRUCTOR_P (current)
15490 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15491 {
15492 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)",
15493 lang_printable_name (TREE_TYPE (expr), 0));
15494 return error_mark_node;
15495 }
15496 }
15497
15498 /* Throw is contained in a try statement and at least one catch
15499 clause can receive the thrown expression or the current method is
15500 declared to throw such an exception. Or, the throw statement is
15501 contained in a method or constructor declaration and the type of
15502 the Expression is assignable to at least one type listed in the
15503 throws clause the declaration. */
15504 if (!unchecked_ok)
15505 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15506 if (!(unchecked_ok || tryblock_throws_ok))
15507 {
15508 /* If there is a surrounding try block that has no matching
15509 clatch clause, report it first. A surrounding try block exits
15510 only if there is something after the list of checked
15511 exception thrown by the current function (if any). */
15512 if (IN_TRY_BLOCK_P ())
15513 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15514 lang_printable_name (type, 0));
15515 /* If we have no surrounding try statement and the method doesn't have
15516 any throws, report it now. FIXME */
15517
15518 /* We report that the exception can't be throw from a try block
15519 in all circumstances but when the `throw' is inside a static
15520 block. */
15521 else if (!EXCEPTIONS_P (currently_caught_type_list)
15522 && !tryblock_throws_ok)
15523 {
15524 if (DECL_CLINIT_P (current_function_decl))
15525 parse_error_context (wfl_operator,
15526 "Checked exception `%s' can't be thrown in initializer",
15527 lang_printable_name (type, 0));
15528 else
15529 parse_error_context (wfl_operator,
15530 "Checked exception `%s' isn't thrown from a `try' block",
15531 lang_printable_name (type, 0));
15532 }
15533 /* Otherwise, the current method doesn't have the appropriate
15534 throws declaration */
15535 else
15536 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15537 lang_printable_name (type, 0));
15538 return error_mark_node;
15539 }
15540
15541 if (! flag_emit_class_files && ! flag_emit_xref)
15542 BUILD_THROW (node, expr);
15543
15544 /* If doing xrefs, keep the location where the `throw' was seen. */
15545 if (flag_emit_xref)
15546 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15547 return node;
15548 }
15549
15550 /* Check that exception said to be thrown by method DECL can be
15551 effectively caught from where DECL is invoked. */
15552
15553 static void
15554 check_thrown_exceptions (location, decl)
15555 int location;
15556 tree decl;
15557 {
15558 tree throws;
15559 /* For all the unchecked exceptions thrown by DECL */
15560 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15561 throws = TREE_CHAIN (throws))
15562 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15563 {
15564 #if 1
15565 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15566 if (DECL_NAME (decl) == get_identifier ("clone"))
15567 continue;
15568 #endif
15569 EXPR_WFL_LINECOL (wfl_operator) = location;
15570 if (DECL_FINIT_P (current_function_decl))
15571 parse_error_context
15572 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15573 lang_printable_name (TREE_VALUE (throws), 0));
15574 else
15575 {
15576 parse_error_context
15577 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15578 lang_printable_name (TREE_VALUE (throws), 0),
15579 (DECL_INIT_P (current_function_decl) ?
15580 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15581 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15582 }
15583 }
15584 }
15585
15586 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15587 try-catch blocks, OR is listed in the `throws' clause of the
15588 current method. */
15589
15590 static int
15591 check_thrown_exceptions_do (exception)
15592 tree exception;
15593 {
15594 tree list = currently_caught_type_list;
15595 resolve_and_layout (exception, NULL_TREE);
15596 /* First, all the nested try-catch-finally at that stage. The
15597 last element contains `throws' clause exceptions, if any. */
15598 if (IS_UNCHECKED_EXCEPTION_P (exception))
15599 return 1;
15600 while (list)
15601 {
15602 tree caught;
15603 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15604 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15605 return 1;
15606 list = TREE_CHAIN (list);
15607 }
15608 return 0;
15609 }
15610
15611 static void
15612 purge_unchecked_exceptions (mdecl)
15613 tree mdecl;
15614 {
15615 tree throws = DECL_FUNCTION_THROWS (mdecl);
15616 tree new = NULL_TREE;
15617
15618 while (throws)
15619 {
15620 tree next = TREE_CHAIN (throws);
15621 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15622 {
15623 TREE_CHAIN (throws) = new;
15624 new = throws;
15625 }
15626 throws = next;
15627 }
15628 /* List is inverted here, but it doesn't matter */
15629 DECL_FUNCTION_THROWS (mdecl) = new;
15630 }
15631
15632 /* This function goes over all of CLASS_TYPE ctors and checks whether
15633 each of them features at least one unchecked exception in its
15634 `throws' clause. If it's the case, it returns `true', `false'
15635 otherwise. */
15636
15637 static bool
15638 ctors_unchecked_throws_clause_p (class_type)
15639 tree class_type;
15640 {
15641 tree current;
15642
15643 for (current = TYPE_METHODS (class_type); current;
15644 current = TREE_CHAIN (current))
15645 {
15646 bool ctu = false; /* Ctor Throws Unchecked */
15647 if (DECL_CONSTRUCTOR_P (current))
15648 {
15649 tree throws;
15650 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15651 throws = TREE_CHAIN (throws))
15652 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15653 ctu = true;
15654 }
15655 /* We return false as we found one ctor that is unfit. */
15656 if (!ctu && DECL_CONSTRUCTOR_P (current))
15657 return false;
15658 }
15659 /* All ctors feature at least one unchecked exception in their
15660 `throws' clause. */
15661 return true;
15662 }
15663
15664 /* 15.24 Conditional Operator ?: */
15665
15666 static tree
15667 patch_conditional_expr (node, wfl_cond, wfl_op1)
15668 tree node, wfl_cond, wfl_op1;
15669 {
15670 tree cond = TREE_OPERAND (node, 0);
15671 tree op1 = TREE_OPERAND (node, 1);
15672 tree op2 = TREE_OPERAND (node, 2);
15673 tree resulting_type = NULL_TREE;
15674 tree t1, t2, patched;
15675 int error_found = 0;
15676
15677 /* Operands of ?: might be StringBuffers crafted as a result of a
15678 string concatenation. Obtain a descent operand here. */
15679 if ((patched = patch_string (op1)))
15680 TREE_OPERAND (node, 1) = op1 = patched;
15681 if ((patched = patch_string (op2)))
15682 TREE_OPERAND (node, 2) = op2 = patched;
15683
15684 t1 = TREE_TYPE (op1);
15685 t2 = TREE_TYPE (op2);
15686
15687 /* The first expression must be a boolean */
15688 if (TREE_TYPE (cond) != boolean_type_node)
15689 {
15690 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15691 parse_error_context (wfl_operator,
15692 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15693 lang_printable_name (TREE_TYPE (cond), 0));
15694 error_found = 1;
15695 }
15696
15697 /* Second and third can be numeric, boolean (i.e. primitive),
15698 references or null. Anything else results in an error */
15699 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15700 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15701 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15702 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15703 error_found = 1;
15704
15705 /* Determine the type of the conditional expression. Same types are
15706 easy to deal with */
15707 else if (t1 == t2)
15708 resulting_type = t1;
15709
15710 /* There are different rules for numeric types */
15711 else if (JNUMERIC_TYPE_P (t1))
15712 {
15713 /* if byte/short found, the resulting type is short */
15714 if ((t1 == byte_type_node && t2 == short_type_node)
15715 || (t1 == short_type_node && t2 == byte_type_node))
15716 resulting_type = short_type_node;
15717
15718 /* If t1 is a constant int and t2 is of type byte, short or char
15719 and t1's value fits in t2, then the resulting type is t2 */
15720 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15721 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15722 resulting_type = t2;
15723
15724 /* If t2 is a constant int and t1 is of type byte, short or char
15725 and t2's value fits in t1, then the resulting type is t1 */
15726 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15727 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15728 resulting_type = t1;
15729
15730 /* Otherwise, binary numeric promotion is applied and the
15731 resulting type is the promoted type of operand 1 and 2 */
15732 else
15733 resulting_type = binary_numeric_promotion (t1, t2,
15734 &TREE_OPERAND (node, 1),
15735 &TREE_OPERAND (node, 2));
15736 }
15737
15738 /* Cases of a reference and a null type */
15739 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15740 resulting_type = t1;
15741
15742 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15743 resulting_type = t2;
15744
15745 /* Last case: different reference types. If a type can be converted
15746 into the other one by assignment conversion, the latter
15747 determines the type of the expression */
15748 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15749 resulting_type = promote_type (t1);
15750
15751 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15752 resulting_type = promote_type (t2);
15753
15754 /* If we don't have any resulting type, we're in trouble */
15755 if (!resulting_type)
15756 {
15757 char *t = xstrdup (lang_printable_name (t1, 0));
15758 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15759 parse_error_context (wfl_operator,
15760 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15761 t, lang_printable_name (t2, 0));
15762 free (t);
15763 error_found = 1;
15764 }
15765
15766 if (error_found)
15767 {
15768 TREE_TYPE (node) = error_mark_node;
15769 return error_mark_node;
15770 }
15771
15772 TREE_TYPE (node) = resulting_type;
15773 TREE_SET_CODE (node, COND_EXPR);
15774 CAN_COMPLETE_NORMALLY (node) = 1;
15775 return node;
15776 }
15777
15778 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
15779
15780 static tree
15781 maybe_build_class_init_for_field (decl, expr)
15782 tree decl, expr;
15783 {
15784 tree clas = DECL_CONTEXT (decl);
15785 if (flag_emit_class_files || flag_emit_xref)
15786 return expr;
15787
15788 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
15789 && FIELD_FINAL (decl))
15790 {
15791 tree init = DECL_INITIAL (decl);
15792 if (init != NULL_TREE)
15793 init = fold_constant_for_init (init, decl);
15794 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
15795 return expr;
15796 }
15797
15798 return build_class_init (clas, expr);
15799 }
15800
15801 /* Try to constant fold NODE.
15802 If NODE is not a constant expression, return NULL_EXPR.
15803 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15804
15805 static tree
15806 fold_constant_for_init (node, context)
15807 tree node;
15808 tree context;
15809 {
15810 tree op0, op1, val;
15811 enum tree_code code = TREE_CODE (node);
15812
15813 switch (code)
15814 {
15815 case STRING_CST:
15816 case INTEGER_CST:
15817 case REAL_CST:
15818 return node;
15819
15820 case PLUS_EXPR:
15821 case MINUS_EXPR:
15822 case MULT_EXPR:
15823 case TRUNC_MOD_EXPR:
15824 case RDIV_EXPR:
15825 case LSHIFT_EXPR:
15826 case RSHIFT_EXPR:
15827 case URSHIFT_EXPR:
15828 case BIT_AND_EXPR:
15829 case BIT_XOR_EXPR:
15830 case BIT_IOR_EXPR:
15831 case TRUTH_ANDIF_EXPR:
15832 case TRUTH_ORIF_EXPR:
15833 case EQ_EXPR:
15834 case NE_EXPR:
15835 case GT_EXPR:
15836 case GE_EXPR:
15837 case LT_EXPR:
15838 case LE_EXPR:
15839 op0 = TREE_OPERAND (node, 0);
15840 op1 = TREE_OPERAND (node, 1);
15841 val = fold_constant_for_init (op0, context);
15842 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15843 return NULL_TREE;
15844 TREE_OPERAND (node, 0) = val;
15845 val = fold_constant_for_init (op1, context);
15846 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15847 return NULL_TREE;
15848 TREE_OPERAND (node, 1) = val;
15849 return patch_binop (node, op0, op1);
15850
15851 case UNARY_PLUS_EXPR:
15852 case NEGATE_EXPR:
15853 case TRUTH_NOT_EXPR:
15854 case BIT_NOT_EXPR:
15855 case CONVERT_EXPR:
15856 op0 = TREE_OPERAND (node, 0);
15857 val = fold_constant_for_init (op0, context);
15858 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15859 return NULL_TREE;
15860 TREE_OPERAND (node, 0) = val;
15861 return patch_unaryop (node, op0);
15862 break;
15863
15864 case COND_EXPR:
15865 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15866 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15867 return NULL_TREE;
15868 TREE_OPERAND (node, 0) = val;
15869 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15870 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15871 return NULL_TREE;
15872 TREE_OPERAND (node, 1) = val;
15873 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15874 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15875 return NULL_TREE;
15876 TREE_OPERAND (node, 2) = val;
15877 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15878 : TREE_OPERAND (node, 2);
15879
15880 case VAR_DECL:
15881 case FIELD_DECL:
15882 if (! FIELD_FINAL (node)
15883 || DECL_INITIAL (node) == NULL_TREE)
15884 return NULL_TREE;
15885 val = DECL_INITIAL (node);
15886 /* Guard against infinite recursion. */
15887 DECL_INITIAL (node) = NULL_TREE;
15888 val = fold_constant_for_init (val, node);
15889 DECL_INITIAL (node) = val;
15890 return val;
15891
15892 case EXPR_WITH_FILE_LOCATION:
15893 /* Compare java_complete_tree and resolve_expression_name. */
15894 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15895 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15896 {
15897 tree name = EXPR_WFL_NODE (node);
15898 tree decl;
15899 if (PRIMARY_P (node))
15900 return NULL_TREE;
15901 else if (! QUALIFIED_P (name))
15902 {
15903 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15904 if (decl == NULL_TREE
15905 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15906 return NULL_TREE;
15907 return fold_constant_for_init (decl, decl);
15908 }
15909 else
15910 {
15911 /* Install the proper context for the field resolution.
15912 The prior context is restored once the name is
15913 properly qualified. */
15914 tree saved_current_class = current_class;
15915 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15916 current_class = DECL_CONTEXT (context);
15917 qualify_ambiguous_name (node);
15918 current_class = saved_current_class;
15919 if (resolve_field_access (node, &decl, NULL)
15920 && decl != NULL_TREE)
15921 return fold_constant_for_init (decl, decl);
15922 return NULL_TREE;
15923 }
15924 }
15925 else
15926 {
15927 op0 = TREE_OPERAND (node, 0);
15928 val = fold_constant_for_init (op0, context);
15929 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15930 return NULL_TREE;
15931 TREE_OPERAND (node, 0) = val;
15932 return val;
15933 }
15934
15935 #ifdef USE_COMPONENT_REF
15936 case IDENTIFIER:
15937 case COMPONENT_REF:
15938 ?;
15939 #endif
15940
15941 default:
15942 return NULL_TREE;
15943 }
15944 }
15945
15946 #ifdef USE_COMPONENT_REF
15947 /* Context is 'T' for TypeName, 'P' for PackageName,
15948 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15949
15950 tree
15951 resolve_simple_name (name, context)
15952 tree name;
15953 int context;
15954 {
15955 }
15956
15957 tree
15958 resolve_qualified_name (name, context)
15959 tree name;
15960 int context;
15961 {
15962 }
15963 #endif
15964
15965 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15966
15967 static void
15968 mark_parser_ctxt (p)
15969 void *p;
15970 {
15971 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15972 int i;
15973
15974 if (!pc)
15975 return;
15976
15977 #ifndef JC1_LITE
15978 for (i = 0; i < 11; ++i)
15979 ggc_mark_tree (pc->modifier_ctx[i]);
15980 ggc_mark_tree (pc->class_type);
15981 ggc_mark_tree (pc->function_decl);
15982 ggc_mark_tree (pc->package);
15983 ggc_mark_tree (pc->class_list);
15984 ggc_mark_tree (pc->current_parsed_class);
15985 ggc_mark_tree (pc->current_parsed_class_un);
15986 ggc_mark_tree (pc->non_static_initialized);
15987 ggc_mark_tree (pc->static_initialized);
15988 ggc_mark_tree (pc->instance_initializers);
15989 ggc_mark_tree (pc->import_list);
15990 ggc_mark_tree (pc->import_demand_list);
15991 ggc_mark_tree (pc->current_loop);
15992 ggc_mark_tree (pc->current_labeled_block);
15993 #endif /* JC1_LITE */
15994
15995 if (pc->next)
15996 mark_parser_ctxt (&pc->next);
15997 }
15998
15999 void
16000 init_src_parse ()
16001 {
16002 /* Register roots with the garbage collector. */
16003 ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));
16004 }
16005
16006 \f
16007
16008 /* This section deals with the functions that are called when tables
16009 recording class initialization information are traversed. */
16010
16011 /* Attach to PTR (a block) the declaration found in ENTRY. */
16012
16013 static bool
16014 attach_init_test_initialization_flags (entry, ptr)
16015 struct hash_entry *entry;
16016 PTR ptr;
16017 {
16018 tree block = (tree)ptr;
16019 struct init_test_hash_entry *ite = (struct init_test_hash_entry *) entry;
16020
16021 TREE_CHAIN (ite->init_test_decl) = BLOCK_EXPR_DECLS (block);
16022 BLOCK_EXPR_DECLS (block) = ite->init_test_decl;
16023 return true;
16024 }
16025
16026 /* This function is called for each classes that is known definitely
16027 assigned when a given static method was called. This function
16028 augments a compound expression (INFO) storing all assignment to
16029 initialized static class flags if a flag already existed, otherwise
16030 a new one is created. */
16031
16032 static bool
16033 emit_test_initialization (entry, info)
16034 struct hash_entry *entry;
16035 PTR info;
16036 {
16037 tree l = (tree) info;
16038 tree decl, init;
16039
16040 struct init_test_hash_entry *ite = (struct init_test_hash_entry *)
16041 hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
16042 entry->key,
16043 current_function_decl != TREE_PURPOSE (l), NULL);
16044
16045 /* If we haven't found a flag and we're dealing with self registered
16046 with current_function_decl, then don't do anything. Self is
16047 always added as definitely initialized but this information is
16048 valid only if used outside the current function. */
16049 if (! ite)
16050 return true;
16051
16052 /* If we don't have a variable, create one and install it. */
16053 if (! ite->init_test_decl)
16054 {
16055 tree block;
16056
16057 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16058 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16059 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16060 DECL_CONTEXT (decl) = current_function_decl;
16061 DECL_INITIAL (decl) = boolean_true_node;
16062
16063 /* The trick is to find the right context for it. */
16064 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16065 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16066 BLOCK_EXPR_DECLS (block) = decl;
16067 ite->init_test_decl = decl;
16068 }
16069 else
16070 decl = ite->init_test_decl;
16071
16072 /* Now simply augment the compound that holds all the assignments
16073 pertaining to this method invocation. */
16074 init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16075 TREE_SIDE_EFFECTS (init) = 1;
16076 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16077 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16078
16079 return true;
16080 }