]> gcc.gnu.org Git - gcc.git/blob - gcc/java/parse.y
typeck.c (build_java_array_type): Don't try to poke a public `clone' method into...
[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));
103 static void find_in_imports PARAMS ((tree));
104 static void check_static_final_variable_assignment_flag PARAMS ((tree));
105 static void reset_static_final_variable_assignment_flag PARAMS ((tree));
106 static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
107 static void reset_final_variable_local_assignment_flag PARAMS ((tree));
108 static int check_final_variable_indirect_assignment PARAMS ((tree));
109 static void check_final_variable_global_assignment_flag PARAMS ((tree));
110 static void check_inner_class_access PARAMS ((tree, tree, tree));
111 static int check_pkg_class_access PARAMS ((tree, tree));
112 static void register_package PARAMS ((tree));
113 static tree resolve_package PARAMS ((tree, tree *));
114 static tree lookup_package_type PARAMS ((const char *, int));
115 static tree resolve_class PARAMS ((tree, tree, tree, tree));
116 static void declare_local_variables PARAMS ((int, tree, tree));
117 static void source_start_java_method PARAMS ((tree));
118 static void source_end_java_method PARAMS ((void));
119 static void expand_start_java_method PARAMS ((tree));
120 static tree find_name_in_single_imports PARAMS ((tree));
121 static void check_abstract_method_header PARAMS ((tree));
122 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
123 static tree resolve_expression_name PARAMS ((tree, tree *));
124 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
125 static int check_class_interface_creation PARAMS ((int, int, tree,
126 tree, tree, tree));
127 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
128 int *, tree *));
129 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
130 static tree resolve_and_layout PARAMS ((tree, tree));
131 static tree qualify_and_find PARAMS ((tree, tree, tree));
132 static tree resolve_no_layout PARAMS ((tree, tree));
133 static int invocation_mode PARAMS ((tree, int));
134 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
135 tree, tree));
136 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
137 tree *, tree *));
138 static tree find_most_specific_methods_list PARAMS ((tree));
139 static int argument_types_convertible PARAMS ((tree, tree));
140 static tree patch_invoke PARAMS ((tree, tree, tree));
141 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
142 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
143 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
144 static tree obtain_incomplete_type PARAMS ((tree));
145 static tree java_complete_lhs PARAMS ((tree));
146 static tree java_complete_tree PARAMS ((tree));
147 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
148 static int analyze_clinit_body PARAMS ((tree));
149 static int maybe_yank_clinit PARAMS ((tree));
150 static void java_complete_expand_method PARAMS ((tree));
151 static int unresolved_type_p PARAMS ((tree, tree *));
152 static void create_jdep_list PARAMS ((struct parser_ctxt *));
153 static tree build_expr_block PARAMS ((tree, tree));
154 static tree enter_block PARAMS ((void));
155 static tree enter_a_block PARAMS ((tree));
156 static tree exit_block PARAMS ((void));
157 static tree lookup_name_in_blocks PARAMS ((tree));
158 static void maybe_absorb_scoping_blocks PARAMS ((void));
159 static tree build_method_invocation PARAMS ((tree, tree));
160 static tree build_new_invocation PARAMS ((tree, tree));
161 static tree build_assignment PARAMS ((int, int, tree, tree));
162 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
163 static int check_final_assignment PARAMS ((tree ,tree));
164 static tree patch_assignment PARAMS ((tree, tree, tree ));
165 static tree patch_binop PARAMS ((tree, tree, tree));
166 static tree build_unaryop PARAMS ((int, int, tree));
167 static tree build_incdec PARAMS ((int, int, tree, int));
168 static tree patch_unaryop PARAMS ((tree, tree));
169 static tree build_cast PARAMS ((int, tree, tree));
170 static tree build_null_of_type PARAMS ((tree));
171 static tree patch_cast PARAMS ((tree, tree));
172 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
173 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
174 static int valid_cast_to_p PARAMS ((tree, tree));
175 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
176 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
177 static tree try_reference_assignconv PARAMS ((tree, tree));
178 static tree build_unresolved_array_type PARAMS ((tree));
179 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
180 static tree build_array_ref PARAMS ((int, tree, tree));
181 static tree patch_array_ref PARAMS ((tree));
182 static tree make_qualified_name PARAMS ((tree, tree, int));
183 static tree merge_qualified_name PARAMS ((tree, tree));
184 static tree make_qualified_primary PARAMS ((tree, tree, int));
185 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
186 tree *, tree *));
187 static void qualify_ambiguous_name PARAMS ((tree));
188 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
189 static tree build_newarray_node PARAMS ((tree, tree, int));
190 static tree patch_newarray PARAMS ((tree));
191 static tree resolve_type_during_patch PARAMS ((tree));
192 static tree build_this PARAMS ((int));
193 static tree build_wfl_wrap PARAMS ((tree, int));
194 static tree build_return PARAMS ((int, tree));
195 static tree patch_return PARAMS ((tree));
196 static tree maybe_access_field PARAMS ((tree, tree, tree));
197 static int complete_function_arguments PARAMS ((tree));
198 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
199 tree, tree));
200 static int not_accessible_p PARAMS ((tree, tree, tree, int));
201 static void check_deprecation PARAMS ((tree, tree));
202 static int class_in_current_package PARAMS ((tree));
203 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
204 static tree patch_if_else_statement PARAMS ((tree));
205 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
206 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
207 static tree patch_exit_expr PARAMS ((tree));
208 static tree build_labeled_block PARAMS ((int, tree));
209 static tree finish_labeled_statement PARAMS ((tree, tree));
210 static tree build_bc_statement PARAMS ((int, int, tree));
211 static tree patch_bc_statement PARAMS ((tree));
212 static tree patch_loop_statement PARAMS ((tree));
213 static tree build_new_loop PARAMS ((tree));
214 static tree build_loop_body PARAMS ((int, tree, int));
215 static tree finish_loop_body PARAMS ((int, tree, tree, int));
216 static tree build_debugable_stmt PARAMS ((int, tree));
217 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
218 static tree patch_switch_statement PARAMS ((tree));
219 static tree string_constant_concatenation PARAMS ((tree, tree));
220 static tree build_string_concatenation PARAMS ((tree, tree));
221 static tree patch_string_cst PARAMS ((tree));
222 static tree patch_string PARAMS ((tree));
223 static tree build_try_statement PARAMS ((int, tree, tree));
224 static tree build_try_finally_statement PARAMS ((int, tree, tree));
225 static tree patch_try_statement PARAMS ((tree));
226 static tree patch_synchronized_statement PARAMS ((tree, tree));
227 static tree patch_throw_statement PARAMS ((tree, tree));
228 static void check_thrown_exceptions PARAMS ((int, tree));
229 static int check_thrown_exceptions_do PARAMS ((tree));
230 static void purge_unchecked_exceptions 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 void add_instance_initializer PARAMS ((tree));
242 static void fix_constructors PARAMS ((tree));
243 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
244 tree, int *));
245 static void craft_constructor PARAMS ((tree, tree));
246 static int verify_constructor_super PARAMS ((tree));
247 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
248 static void start_artificial_method_body PARAMS ((tree));
249 static void end_artificial_method_body PARAMS ((tree));
250 static int check_method_redefinition PARAMS ((tree, tree));
251 static int check_method_types_complete PARAMS ((tree));
252 static void java_check_regular_methods PARAMS ((tree));
253 static void java_check_abstract_methods PARAMS ((tree));
254 static tree maybe_build_primttype_type_ref PARAMS ((tree, 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 find_as_inner_class PARAMS ((tree, tree, tree));
302 static tree find_as_inner_class_do PARAMS ((tree, tree));
303 static int check_inner_class_redefinition PARAMS ((tree, tree));
304
305 static tree build_thisn_assign PARAMS ((void));
306 static tree build_current_thisn PARAMS ((tree));
307 static tree build_access_to_thisn PARAMS ((tree, tree, int));
308 static tree maybe_build_thisn_access_method PARAMS ((tree));
309
310 static tree build_outer_field_access PARAMS ((tree, tree));
311 static tree build_outer_field_access_methods PARAMS ((tree));
312 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
313 tree, tree));
314 static tree build_outer_method_access_method PARAMS ((tree));
315 static tree build_new_access_id PARAMS ((void));
316 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
317 tree, tree));
318
319 static int outer_field_access_p PARAMS ((tree, tree));
320 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
321 tree *, tree *));
322 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
323 static tree build_incomplete_class_ref PARAMS ((int, tree));
324 static tree patch_incomplete_class_ref PARAMS ((tree));
325 static tree create_anonymous_class PARAMS ((int, tree));
326 static void patch_anonymous_class PARAMS ((tree, tree, tree));
327 static void add_inner_class_fields PARAMS ((tree, tree));
328
329 static tree build_dot_class_method PARAMS ((tree));
330 static tree build_dot_class_method_invocation PARAMS ((tree));
331 static void create_new_parser_context PARAMS ((int));
332 static void mark_parser_ctxt PARAMS ((void *));
333
334 /* Number of error found so far. */
335 int java_error_count;
336 /* Number of warning found so far. */
337 int java_warning_count;
338 /* Tell when not to fold, when doing xrefs */
339 int do_not_fold;
340 /* Cyclic inheritance report, as it can be set by layout_class */
341 const char *cyclic_inheritance_report;
342
343 /* Tell when we're within an instance initializer */
344 static int in_instance_initializer;
345
346 /* The current parser context */
347 struct parser_ctxt *ctxp;
348
349 /* List of things that were analyzed for which code will be generated */
350 static struct parser_ctxt *ctxp_for_generation = NULL;
351
352 /* binop_lookup maps token to tree_code. It is used where binary
353 operations are involved and required by the parser. RDIV_EXPR
354 covers both integral/floating point division. The code is changed
355 once the type of both operator is worked out. */
356
357 static enum tree_code binop_lookup[19] =
358 {
359 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
360 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
361 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
362 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
363 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
364 };
365 #define BINOP_LOOKUP(VALUE) \
366 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
367
368 /* This is the end index for binary operators that can also be used
369 in compound assignements. */
370 #define BINOP_COMPOUND_CANDIDATES 11
371
372 /* The "$L" identifier we use to create labels. */
373 static tree label_id = NULL_TREE;
374
375 /* The "StringBuffer" identifier used for the String `+' operator. */
376 static tree wfl_string_buffer = NULL_TREE;
377
378 /* The "append" identifier used for String `+' operator. */
379 static tree wfl_append = NULL_TREE;
380
381 /* The "toString" identifier used for String `+' operator. */
382 static tree wfl_to_string = NULL_TREE;
383
384 /* The "java.lang" import qualified name. */
385 static tree java_lang_id = NULL_TREE;
386
387 /* The generated `inst$' identifier used for generated enclosing
388 instance/field access functions. */
389 static tree inst_id = NULL_TREE;
390
391 /* The "java.lang.Cloneable" qualified name. */
392 static tree java_lang_cloneable = NULL_TREE;
393
394 /* The "java.io.Serializable" qualified name. */
395 static tree java_io_serializable = NULL_TREE;
396
397 /* Context and flag for static blocks */
398 static tree current_static_block = NULL_TREE;
399
400 /* The generated `write_parm_value$' identifier. */
401 static tree wpv_id;
402
403 /* The list of all packages we've seen so far */
404 static tree package_list = NULL_TREE;
405
406 /* Hold THIS for the scope of the current public method decl. */
407 static tree current_this;
408
409 /* Hold a list of catch clauses list. The first element of this list is
410 the list of the catch clauses of the currently analysed try block. */
411 static tree currently_caught_type_list;
412
413 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
414 line and point it out. */
415 /* Should point out the one that don't fit. ASCII/unicode, going
416 backward. FIXME */
417
418 #define check_modifiers(__message, __value, __mask) do { \
419 if ((__value) & ~(__mask)) \
420 { \
421 int i, remainder = (__value) & ~(__mask); \
422 for (i = 0; i <= 10; i++) \
423 if ((1 << i) & remainder) \
424 parse_error_context (ctxp->modifier_ctx [i], (__message), \
425 java_accstring_lookup (1 << i)); \
426 } \
427 } while (0)
428
429 %}
430
431 %union {
432 tree node;
433 int sub_token;
434 struct {
435 int token;
436 int location;
437 } operator;
438 int value;
439 }
440
441 %{
442 #include "lex.c"
443 %}
444
445 %pure_parser
446
447 /* Things defined here have to match the order of what's in the
448 binop_lookup table. */
449
450 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
451 %token LS_TK SRS_TK ZRS_TK
452 %token AND_TK XOR_TK OR_TK
453 %token BOOL_AND_TK BOOL_OR_TK
454 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
455
456 /* This maps to the same binop_lookup entry than the token above */
457
458 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
459 %token REM_ASSIGN_TK
460 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
461 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
462
463
464 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
465
466 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
467 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
468 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
469 %token PAD_TK ABSTRACT_TK MODIFIER_TK
470 %token STRICT_TK
471
472 /* Keep those two in order, too */
473 %token DECR_TK INCR_TK
474
475 /* From now one, things can be in any order */
476
477 %token DEFAULT_TK IF_TK THROW_TK
478 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
479 %token THROWS_TK BREAK_TK IMPORT_TK
480 %token ELSE_TK INSTANCEOF_TK RETURN_TK
481 %token VOID_TK CATCH_TK INTERFACE_TK
482 %token CASE_TK EXTENDS_TK FINALLY_TK
483 %token SUPER_TK WHILE_TK CLASS_TK
484 %token SWITCH_TK CONST_TK TRY_TK
485 %token FOR_TK NEW_TK CONTINUE_TK
486 %token GOTO_TK PACKAGE_TK THIS_TK
487
488 %token BYTE_TK SHORT_TK INT_TK LONG_TK
489 %token CHAR_TK INTEGRAL_TK
490
491 %token FLOAT_TK DOUBLE_TK FP_TK
492
493 %token ID_TK
494
495 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
496
497 %token ASSIGN_ANY_TK ASSIGN_TK
498 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
499
500 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
501 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
502
503 %type <value> modifiers MODIFIER_TK final synchronized
504
505 %type <node> super ID_TK identifier
506 %type <node> name simple_name qualified_name
507 %type <node> type_declaration compilation_unit
508 field_declaration method_declaration extends_interfaces
509 interfaces interface_type_list
510 class_member_declaration
511 import_declarations package_declaration
512 type_declarations interface_body
513 interface_member_declaration constant_declaration
514 interface_member_declarations interface_type
515 abstract_method_declaration interface_type_list
516 %type <node> class_body_declaration class_member_declaration
517 static_initializer constructor_declaration block
518 %type <node> class_body_declarations constructor_header
519 %type <node> class_or_interface_type class_type class_type_list
520 constructor_declarator explicit_constructor_invocation
521 %type <node> dim_expr dim_exprs this_or_super throws
522
523 %type <node> variable_declarator_id variable_declarator
524 variable_declarators variable_initializer
525 variable_initializers constructor_body
526 array_initializer
527
528 %type <node> class_body block_end constructor_block_end
529 %type <node> statement statement_without_trailing_substatement
530 labeled_statement if_then_statement label_decl
531 if_then_else_statement while_statement for_statement
532 statement_nsi labeled_statement_nsi do_statement
533 if_then_else_statement_nsi while_statement_nsi
534 for_statement_nsi statement_expression_list for_init
535 for_update statement_expression expression_statement
536 primary_no_new_array expression primary
537 array_creation_expression array_type
538 class_instance_creation_expression field_access
539 method_invocation array_access something_dot_new
540 argument_list postfix_expression while_expression
541 post_increment_expression post_decrement_expression
542 unary_expression_not_plus_minus unary_expression
543 pre_increment_expression pre_decrement_expression
544 unary_expression_not_plus_minus cast_expression
545 multiplicative_expression additive_expression
546 shift_expression relational_expression
547 equality_expression and_expression
548 exclusive_or_expression inclusive_or_expression
549 conditional_and_expression conditional_or_expression
550 conditional_expression assignment_expression
551 left_hand_side assignment for_header for_begin
552 constant_expression do_statement_begin empty_statement
553 switch_statement synchronized_statement throw_statement
554 try_statement switch_expression switch_block
555 catches catch_clause catch_clause_parameter finally
556 anonymous_class_creation
557 %type <node> return_statement break_statement continue_statement
558
559 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
560 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
561 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
562 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
563 %type <operator> ASSIGN_ANY_TK assignment_operator
564 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
565 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
566 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
567 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
568 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
569 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
570 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
571 %type <operator> NEW_TK
572
573 %type <node> method_body
574
575 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
576 STRING_LIT_TK NULL_TK VOID_TK
577
578 %type <node> IF_TK WHILE_TK FOR_TK
579
580 %type <node> formal_parameter_list formal_parameter
581 method_declarator method_header
582
583 %type <node> primitive_type reference_type type
584 BOOLEAN_TK INTEGRAL_TK FP_TK
585
586 /* Added or modified JDK 1.1 rule types */
587 %type <node> type_literals
588
589 %%
590 /* 19.2 Production from 2.3: The Syntactic Grammar */
591 goal:
592 {
593 /* Register static variables with the garbage
594 collector. */
595 ggc_add_tree_root (&label_id, 1);
596 ggc_add_tree_root (&wfl_string_buffer, 1);
597 ggc_add_tree_root (&wfl_append, 1);
598 ggc_add_tree_root (&wfl_to_string, 1);
599 ggc_add_tree_root (&java_lang_id, 1);
600 ggc_add_tree_root (&inst_id, 1);
601 ggc_add_tree_root (&java_lang_cloneable, 1);
602 ggc_add_tree_root (&java_io_serializable, 1);
603 ggc_add_tree_root (&current_static_block, 1);
604 ggc_add_tree_root (&wpv_id, 1);
605 ggc_add_tree_root (&package_list, 1);
606 ggc_add_tree_root (&current_this, 1);
607 ggc_add_tree_root (&currently_caught_type_list, 1);
608 ggc_add_root (&ctxp, 1,
609 sizeof (struct parser_ctxt *),
610 mark_parser_ctxt);
611 ggc_add_root (&ctxp_for_generation, 1,
612 sizeof (struct parser_ctxt *),
613 mark_parser_ctxt);
614 }
615 compilation_unit
616 {}
617 ;
618
619 /* 19.3 Productions from 3: Lexical structure */
620 literal:
621 INT_LIT_TK
622 | FP_LIT_TK
623 | BOOL_LIT_TK
624 | CHAR_LIT_TK
625 | STRING_LIT_TK
626 | NULL_TK
627 ;
628
629 /* 19.4 Productions from 4: Types, Values and Variables */
630 type:
631 primitive_type
632 | reference_type
633 ;
634
635 primitive_type:
636 INTEGRAL_TK
637 | FP_TK
638 | BOOLEAN_TK
639 ;
640
641 reference_type:
642 class_or_interface_type
643 | array_type
644 ;
645
646 class_or_interface_type:
647 name
648 ;
649
650 class_type:
651 class_or_interface_type /* Default rule */
652 ;
653
654 interface_type:
655 class_or_interface_type
656 ;
657
658 array_type:
659 primitive_type dims
660 {
661 int osb = pop_current_osb (ctxp);
662 tree t = build_java_array_type (($1), -1);
663 CLASS_LOADED_P (t) = 1;
664 while (--osb)
665 t = build_unresolved_array_type (t);
666 $$ = t;
667 }
668 | name dims
669 {
670 int osb = pop_current_osb (ctxp);
671 tree t = $1;
672 while (osb--)
673 t = build_unresolved_array_type (t);
674 $$ = t;
675 }
676 ;
677
678 /* 19.5 Productions from 6: Names */
679 name:
680 simple_name /* Default rule */
681 | qualified_name /* Default rule */
682 ;
683
684 simple_name:
685 identifier /* Default rule */
686 ;
687
688 qualified_name:
689 name DOT_TK identifier
690 { $$ = make_qualified_name ($1, $3, $2.location); }
691 ;
692
693 identifier:
694 ID_TK
695 ;
696
697 /* 19.6: Production from 7: Packages */
698 compilation_unit:
699 {$$ = NULL;}
700 | package_declaration
701 | import_declarations
702 | type_declarations
703 | package_declaration import_declarations
704 | package_declaration type_declarations
705 | import_declarations type_declarations
706 | package_declaration import_declarations type_declarations
707 ;
708
709 import_declarations:
710 import_declaration
711 {
712 $$ = NULL;
713 }
714 | import_declarations import_declaration
715 {
716 $$ = NULL;
717 }
718 ;
719
720 type_declarations:
721 type_declaration
722 | type_declarations type_declaration
723 ;
724
725 package_declaration:
726 PACKAGE_TK name SC_TK
727 {
728 ctxp->package = EXPR_WFL_NODE ($2);
729 register_package (ctxp->package);
730 }
731 | PACKAGE_TK error
732 {yyerror ("Missing name"); RECOVER;}
733 | PACKAGE_TK name error
734 {yyerror ("';' expected"); RECOVER;}
735 ;
736
737 import_declaration:
738 single_type_import_declaration
739 | type_import_on_demand_declaration
740 ;
741
742 single_type_import_declaration:
743 IMPORT_TK name SC_TK
744 {
745 tree name = EXPR_WFL_NODE ($2), last_name;
746 int i = IDENTIFIER_LENGTH (name)-1;
747 const char *last = &IDENTIFIER_POINTER (name)[i];
748 while (last != IDENTIFIER_POINTER (name))
749 {
750 if (last [0] == '.')
751 break;
752 last--;
753 }
754 last_name = get_identifier (++last);
755 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
756 {
757 tree err = find_name_in_single_imports (last_name);
758 if (err && err != name)
759 parse_error_context
760 ($2, "Ambiguous class: `%s' and `%s'",
761 IDENTIFIER_POINTER (name),
762 IDENTIFIER_POINTER (err));
763 else
764 REGISTER_IMPORT ($2, last_name);
765 }
766 else
767 REGISTER_IMPORT ($2, last_name);
768 }
769 | IMPORT_TK error
770 {yyerror ("Missing name"); RECOVER;}
771 | IMPORT_TK name error
772 {yyerror ("';' expected"); RECOVER;}
773 ;
774
775 type_import_on_demand_declaration:
776 IMPORT_TK name DOT_TK MULT_TK SC_TK
777 {
778 tree name = EXPR_WFL_NODE ($2);
779 /* Don't import java.lang.* twice. */
780 if (name != java_lang_id)
781 {
782 read_import_dir ($2);
783 ctxp->import_demand_list =
784 chainon (ctxp->import_demand_list,
785 build_tree_list ($2, NULL_TREE));
786 }
787 }
788 | IMPORT_TK name DOT_TK error
789 {yyerror ("'*' expected"); RECOVER;}
790 | IMPORT_TK name DOT_TK MULT_TK error
791 {yyerror ("';' expected"); RECOVER;}
792 ;
793
794 type_declaration:
795 class_declaration
796 { end_class_declaration (0); }
797 | interface_declaration
798 { end_class_declaration (0); }
799 | empty_statement
800 | error
801 {
802 YYERROR_NOW;
803 yyerror ("Class or interface declaration expected");
804 }
805 ;
806
807 /* 19.7 Shortened from the original:
808 modifiers: modifier | modifiers modifier
809 modifier: any of public... */
810 modifiers:
811 MODIFIER_TK
812 {
813 $$ = (1 << $1);
814 }
815 | modifiers MODIFIER_TK
816 {
817 int acc = (1 << $2);
818 if ($$ & acc)
819 parse_error_context
820 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
821 java_accstring_lookup (acc));
822 else
823 {
824 $$ |= acc;
825 }
826 }
827 ;
828
829 /* 19.8.1 Production from $8.1: Class Declaration */
830 class_declaration:
831 modifiers CLASS_TK identifier super interfaces
832 { create_class ($1, $3, $4, $5); }
833 class_body
834 | CLASS_TK identifier super interfaces
835 { create_class (0, $2, $3, $4); }
836 class_body
837 | modifiers CLASS_TK error
838 {yyerror ("Missing class name"); RECOVER;}
839 | CLASS_TK error
840 {yyerror ("Missing class name"); RECOVER;}
841 | CLASS_TK identifier error
842 {
843 if (!ctxp->class_err) yyerror ("'{' expected");
844 DRECOVER(class1);
845 }
846 | modifiers CLASS_TK identifier error
847 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
848 ;
849
850 super:
851 { $$ = NULL; }
852 | EXTENDS_TK class_type
853 { $$ = $2; }
854 | EXTENDS_TK class_type error
855 {yyerror ("'{' expected"); ctxp->class_err=1;}
856 | EXTENDS_TK error
857 {yyerror ("Missing super class name"); ctxp->class_err=1;}
858 ;
859
860 interfaces:
861 { $$ = NULL_TREE; }
862 | IMPLEMENTS_TK interface_type_list
863 { $$ = $2; }
864 | IMPLEMENTS_TK error
865 {
866 ctxp->class_err=1;
867 yyerror ("Missing interface name");
868 }
869 ;
870
871 interface_type_list:
872 interface_type
873 {
874 ctxp->interface_number = 1;
875 $$ = build_tree_list ($1, NULL_TREE);
876 }
877 | interface_type_list C_TK interface_type
878 {
879 ctxp->interface_number++;
880 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
881 }
882 | interface_type_list C_TK error
883 {yyerror ("Missing interface name"); RECOVER;}
884 ;
885
886 class_body:
887 OCB_TK CCB_TK
888 {
889 /* Store the location of the `}' when doing xrefs */
890 if (flag_emit_xref)
891 DECL_END_SOURCE_LINE (GET_CPC ()) =
892 EXPR_WFL_ADD_COL ($2.location, 1);
893 $$ = GET_CPC ();
894 }
895 | OCB_TK class_body_declarations CCB_TK
896 {
897 /* Store the location of the `}' when doing xrefs */
898 if (flag_emit_xref)
899 DECL_END_SOURCE_LINE (GET_CPC ()) =
900 EXPR_WFL_ADD_COL ($3.location, 1);
901 $$ = GET_CPC ();
902 }
903 ;
904
905 class_body_declarations:
906 class_body_declaration
907 | class_body_declarations class_body_declaration
908 ;
909
910 class_body_declaration:
911 class_member_declaration
912 | static_initializer
913 | constructor_declaration
914 | block /* Added, JDK1.1, instance initializer */
915 {
916 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
917 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
918 }
919 ;
920
921 class_member_declaration:
922 field_declaration
923 | method_declaration
924 | class_declaration /* Added, JDK1.1 inner classes */
925 { end_class_declaration (1); }
926 | interface_declaration /* Added, JDK1.1 inner interfaces */
927 { end_class_declaration (1); }
928 | empty_statement
929 ;
930
931 /* 19.8.2 Productions from 8.3: Field Declarations */
932 field_declaration:
933 type variable_declarators SC_TK
934 { register_fields (0, $1, $2); }
935 | modifiers type variable_declarators SC_TK
936 {
937 check_modifiers
938 ("Illegal modifier `%s' for field declaration",
939 $1, FIELD_MODIFIERS);
940 check_modifiers_consistency ($1);
941 register_fields ($1, $2, $3);
942 }
943 ;
944
945 variable_declarators:
946 /* Should we use build_decl_list () instead ? FIXME */
947 variable_declarator /* Default rule */
948 | variable_declarators C_TK variable_declarator
949 { $$ = chainon ($1, $3); }
950 | variable_declarators C_TK error
951 {yyerror ("Missing term"); RECOVER;}
952 ;
953
954 variable_declarator:
955 variable_declarator_id
956 { $$ = build_tree_list ($1, NULL_TREE); }
957 | variable_declarator_id ASSIGN_TK variable_initializer
958 {
959 if (java_error_count)
960 $3 = NULL_TREE;
961 $$ = build_tree_list
962 ($1, build_assignment ($2.token, $2.location, $1, $3));
963 }
964 | variable_declarator_id ASSIGN_TK error
965 {
966 yyerror ("Missing variable initializer");
967 $$ = build_tree_list ($1, NULL_TREE);
968 RECOVER;
969 }
970 | variable_declarator_id ASSIGN_TK variable_initializer error
971 {
972 yyerror ("';' expected");
973 $$ = build_tree_list ($1, NULL_TREE);
974 RECOVER;
975 }
976 ;
977
978 variable_declarator_id:
979 identifier
980 | variable_declarator_id OSB_TK CSB_TK
981 { $$ = build_unresolved_array_type ($1); }
982 | identifier error
983 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
984 | variable_declarator_id OSB_TK error
985 {
986 tree node = java_lval.node;
987 if (node && (TREE_CODE (node) == INTEGER_CST
988 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
989 yyerror ("Can't specify array dimension in a declaration");
990 else
991 yyerror ("']' expected");
992 DRECOVER(vdi);
993 }
994 | variable_declarator_id CSB_TK error
995 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
996 ;
997
998 variable_initializer:
999 expression
1000 | array_initializer
1001 ;
1002
1003 /* 19.8.3 Productions from 8.4: Method Declarations */
1004 method_declaration:
1005 method_header
1006 {
1007 current_function_decl = $1;
1008 if (current_function_decl
1009 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1010 source_start_java_method (current_function_decl);
1011 else
1012 current_function_decl = NULL_TREE;
1013 }
1014 method_body
1015 { finish_method_declaration ($3); }
1016 | method_header error
1017 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1018 ;
1019
1020 method_header:
1021 type method_declarator throws
1022 { $$ = method_header (0, $1, $2, $3); }
1023 | VOID_TK method_declarator throws
1024 { $$ = method_header (0, void_type_node, $2, $3); }
1025 | modifiers type method_declarator throws
1026 { $$ = method_header ($1, $2, $3, $4); }
1027 | modifiers VOID_TK method_declarator throws
1028 { $$ = method_header ($1, void_type_node, $3, $4); }
1029 | type error
1030 {
1031 yyerror ("Invalid method declaration, method name required");
1032 RECOVER;
1033 }
1034 | modifiers type error
1035 {RECOVER;}
1036 | VOID_TK error
1037 {yyerror ("Identifier expected"); RECOVER;}
1038 | modifiers VOID_TK error
1039 {yyerror ("Identifier expected"); RECOVER;}
1040 | modifiers error
1041 {
1042 yyerror ("Invalid method declaration, return type required");
1043 RECOVER;
1044 }
1045 ;
1046
1047 method_declarator:
1048 identifier OP_TK CP_TK
1049 {
1050 ctxp->formal_parameter_number = 0;
1051 $$ = method_declarator ($1, NULL_TREE);
1052 }
1053 | identifier OP_TK formal_parameter_list CP_TK
1054 { $$ = method_declarator ($1, $3); }
1055 | method_declarator OSB_TK CSB_TK
1056 {
1057 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1058 TREE_PURPOSE ($1) =
1059 build_unresolved_array_type (TREE_PURPOSE ($1));
1060 parse_warning_context
1061 (wfl_operator,
1062 "Discouraged form of returned type specification");
1063 }
1064 | identifier OP_TK error
1065 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1066 | method_declarator OSB_TK error
1067 {yyerror ("']' expected"); RECOVER;}
1068 ;
1069
1070 formal_parameter_list:
1071 formal_parameter
1072 {
1073 ctxp->formal_parameter_number = 1;
1074 }
1075 | formal_parameter_list C_TK formal_parameter
1076 {
1077 ctxp->formal_parameter_number += 1;
1078 $$ = chainon ($1, $3);
1079 }
1080 | formal_parameter_list C_TK error
1081 { yyerror ("Missing formal parameter term"); RECOVER; }
1082 ;
1083
1084 formal_parameter:
1085 type variable_declarator_id
1086 {
1087 $$ = build_tree_list ($2, $1);
1088 }
1089 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1090 {
1091 $$ = build_tree_list ($3, $2);
1092 ARG_FINAL_P ($$) = 1;
1093 }
1094 | type error
1095 {
1096 yyerror ("Missing identifier"); RECOVER;
1097 $$ = NULL_TREE;
1098 }
1099 | final type error
1100 {
1101 yyerror ("Missing identifier"); RECOVER;
1102 $$ = NULL_TREE;
1103 }
1104 ;
1105
1106 final:
1107 modifiers
1108 {
1109 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1110 $1, ACC_FINAL);
1111 if ($1 != ACC_FINAL)
1112 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1113 }
1114 ;
1115
1116 throws:
1117 { $$ = NULL_TREE; }
1118 | THROWS_TK class_type_list
1119 { $$ = $2; }
1120 | THROWS_TK error
1121 {yyerror ("Missing class type term"); RECOVER;}
1122 ;
1123
1124 class_type_list:
1125 class_type
1126 { $$ = build_tree_list ($1, $1); }
1127 | class_type_list C_TK class_type
1128 { $$ = tree_cons ($3, $3, $1); }
1129 | class_type_list C_TK error
1130 {yyerror ("Missing class type term"); RECOVER;}
1131 ;
1132
1133 method_body:
1134 block
1135 | SC_TK { $$ = NULL_TREE; }
1136 ;
1137
1138 /* 19.8.4 Productions from 8.5: Static Initializers */
1139 static_initializer:
1140 static block
1141 {
1142 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1143 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1144 current_static_block = NULL_TREE;
1145 }
1146 ;
1147
1148 static: /* Test lval.sub_token here */
1149 modifiers
1150 {
1151 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1152 /* Can't have a static initializer in an innerclass */
1153 if ($1 | ACC_STATIC &&
1154 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1155 parse_error_context
1156 (MODIFIER_WFL (STATIC_TK),
1157 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1158 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1159 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1160 }
1161 ;
1162
1163 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1164 constructor_declaration:
1165 constructor_header
1166 {
1167 current_function_decl = $1;
1168 source_start_java_method (current_function_decl);
1169 }
1170 constructor_body
1171 { finish_method_declaration ($3); }
1172 ;
1173
1174 constructor_header:
1175 constructor_declarator throws
1176 { $$ = method_header (0, NULL_TREE, $1, $2); }
1177 | modifiers constructor_declarator throws
1178 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1179 ;
1180
1181 constructor_declarator:
1182 simple_name OP_TK CP_TK
1183 {
1184 ctxp->formal_parameter_number = 0;
1185 $$ = method_declarator ($1, NULL_TREE);
1186 }
1187 | simple_name OP_TK formal_parameter_list CP_TK
1188 { $$ = method_declarator ($1, $3); }
1189 ;
1190
1191 constructor_body:
1192 /* Unlike regular method, we always need a complete (empty)
1193 body so we can safely perform all the required code
1194 addition (super invocation and field initialization) */
1195 block_begin constructor_block_end
1196 {
1197 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1198 $$ = $2;
1199 }
1200 | block_begin explicit_constructor_invocation constructor_block_end
1201 { $$ = $3; }
1202 | block_begin block_statements constructor_block_end
1203 { $$ = $3; }
1204 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1205 { $$ = $4; }
1206 ;
1207
1208 constructor_block_end:
1209 block_end
1210 ;
1211
1212 /* Error recovery for that rule moved down expression_statement: rule. */
1213 explicit_constructor_invocation:
1214 this_or_super OP_TK CP_TK SC_TK
1215 {
1216 $$ = build_method_invocation ($1, NULL_TREE);
1217 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1218 $$ = java_method_add_stmt (current_function_decl, $$);
1219 }
1220 | this_or_super OP_TK argument_list CP_TK SC_TK
1221 {
1222 $$ = build_method_invocation ($1, $3);
1223 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1224 $$ = java_method_add_stmt (current_function_decl, $$);
1225 }
1226 /* Added, JDK1.1 inner classes. Modified because the rule
1227 'primary' couldn't work. */
1228 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1229 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1230 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1231 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1232 ;
1233
1234 this_or_super: /* Added, simplifies error diagnostics */
1235 THIS_TK
1236 {
1237 tree wfl = build_wfl_node (this_identifier_node);
1238 EXPR_WFL_LINECOL (wfl) = $1.location;
1239 $$ = wfl;
1240 }
1241 | SUPER_TK
1242 {
1243 tree wfl = build_wfl_node (super_identifier_node);
1244 EXPR_WFL_LINECOL (wfl) = $1.location;
1245 $$ = wfl;
1246 }
1247 ;
1248
1249 /* 19.9 Productions from 9: Interfaces */
1250 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1251 interface_declaration:
1252 INTERFACE_TK identifier
1253 { create_interface (0, $2, NULL_TREE); }
1254 interface_body
1255 | modifiers INTERFACE_TK identifier
1256 { create_interface ($1, $3, NULL_TREE); }
1257 interface_body
1258 | INTERFACE_TK identifier extends_interfaces
1259 { create_interface (0, $2, $3); }
1260 interface_body
1261 | modifiers INTERFACE_TK identifier extends_interfaces
1262 { create_interface ($1, $3, $4); }
1263 interface_body
1264 | INTERFACE_TK identifier error
1265 {yyerror ("'{' expected"); RECOVER;}
1266 | modifiers INTERFACE_TK identifier error
1267 {yyerror ("'{' expected"); RECOVER;}
1268 ;
1269
1270 extends_interfaces:
1271 EXTENDS_TK interface_type
1272 {
1273 ctxp->interface_number = 1;
1274 $$ = build_tree_list ($2, NULL_TREE);
1275 }
1276 | extends_interfaces C_TK interface_type
1277 {
1278 ctxp->interface_number++;
1279 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1280 }
1281 | EXTENDS_TK error
1282 {yyerror ("Invalid interface type"); RECOVER;}
1283 | extends_interfaces C_TK error
1284 {yyerror ("Missing term"); RECOVER;}
1285 ;
1286
1287 interface_body:
1288 OCB_TK CCB_TK
1289 { $$ = NULL_TREE; }
1290 | OCB_TK interface_member_declarations CCB_TK
1291 { $$ = NULL_TREE; }
1292 ;
1293
1294 interface_member_declarations:
1295 interface_member_declaration
1296 | interface_member_declarations interface_member_declaration
1297 ;
1298
1299 interface_member_declaration:
1300 constant_declaration
1301 | abstract_method_declaration
1302 | class_declaration /* Added, JDK1.1 inner classes */
1303 { end_class_declaration (1); }
1304 | interface_declaration /* Added, JDK1.1 inner interfaces */
1305 { end_class_declaration (1); }
1306 ;
1307
1308 constant_declaration:
1309 field_declaration
1310 ;
1311
1312 abstract_method_declaration:
1313 method_header SC_TK
1314 {
1315 check_abstract_method_header ($1);
1316 current_function_decl = NULL_TREE; /* FIXME ? */
1317 }
1318 | method_header error
1319 {yyerror ("';' expected"); RECOVER;}
1320 ;
1321
1322 /* 19.10 Productions from 10: Arrays */
1323 array_initializer:
1324 OCB_TK CCB_TK
1325 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1326 | OCB_TK variable_initializers CCB_TK
1327 { $$ = build_new_array_init ($1.location, $2); }
1328 | OCB_TK variable_initializers C_TK CCB_TK
1329 { $$ = build_new_array_init ($1.location, $2); }
1330 ;
1331
1332 variable_initializers:
1333 variable_initializer
1334 {
1335 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1336 $1, NULL_TREE);
1337 }
1338 | variable_initializers C_TK variable_initializer
1339 {
1340 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1341 }
1342 | variable_initializers C_TK error
1343 {yyerror ("Missing term"); RECOVER;}
1344 ;
1345
1346 /* 19.11 Production from 14: Blocks and Statements */
1347 block:
1348 OCB_TK CCB_TK
1349 {
1350 /* Store the location of the `}' when doing xrefs */
1351 if (current_function_decl && flag_emit_xref)
1352 DECL_END_SOURCE_LINE (current_function_decl) =
1353 EXPR_WFL_ADD_COL ($2.location, 1);
1354 $$ = empty_stmt_node;
1355 }
1356 | block_begin block_statements block_end
1357 { $$ = $3; }
1358 ;
1359
1360 block_begin:
1361 OCB_TK
1362 { enter_block (); }
1363 ;
1364
1365 block_end:
1366 CCB_TK
1367 {
1368 maybe_absorb_scoping_blocks ();
1369 /* Store the location of the `}' when doing xrefs */
1370 if (current_function_decl && flag_emit_xref)
1371 DECL_END_SOURCE_LINE (current_function_decl) =
1372 EXPR_WFL_ADD_COL ($1.location, 1);
1373 $$ = exit_block ();
1374 if (!BLOCK_SUBBLOCKS ($$))
1375 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1376 }
1377 ;
1378
1379 block_statements:
1380 block_statement
1381 | block_statements block_statement
1382 ;
1383
1384 block_statement:
1385 local_variable_declaration_statement
1386 | statement
1387 { java_method_add_stmt (current_function_decl, $1); }
1388 | class_declaration /* Added, JDK1.1 local classes */
1389 {
1390 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1391 end_class_declaration (1);
1392 }
1393 ;
1394
1395 local_variable_declaration_statement:
1396 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1397 ;
1398
1399 local_variable_declaration:
1400 type variable_declarators
1401 { declare_local_variables (0, $1, $2); }
1402 | final type variable_declarators /* Added, JDK1.1 final locals */
1403 { declare_local_variables ($1, $2, $3); }
1404 ;
1405
1406 statement:
1407 statement_without_trailing_substatement
1408 | labeled_statement
1409 | if_then_statement
1410 | if_then_else_statement
1411 | while_statement
1412 | for_statement
1413 { $$ = exit_block (); }
1414 ;
1415
1416 statement_nsi:
1417 statement_without_trailing_substatement
1418 | labeled_statement_nsi
1419 | if_then_else_statement_nsi
1420 | while_statement_nsi
1421 | for_statement_nsi
1422 { $$ = exit_block (); }
1423 ;
1424
1425 statement_without_trailing_substatement:
1426 block
1427 | empty_statement
1428 | expression_statement
1429 | switch_statement
1430 | do_statement
1431 | break_statement
1432 | continue_statement
1433 | return_statement
1434 | synchronized_statement
1435 | throw_statement
1436 | try_statement
1437 ;
1438
1439 empty_statement:
1440 SC_TK
1441 {
1442 if (flag_extraneous_semicolon)
1443 {
1444 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1445 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1446 }
1447 $$ = empty_stmt_node;
1448 }
1449 ;
1450
1451 label_decl:
1452 identifier REL_CL_TK
1453 {
1454 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1455 EXPR_WFL_NODE ($1));
1456 pushlevel (2);
1457 push_labeled_block ($$);
1458 PUSH_LABELED_BLOCK ($$);
1459 }
1460 ;
1461
1462 labeled_statement:
1463 label_decl statement
1464 { $$ = finish_labeled_statement ($1, $2); }
1465 | identifier error
1466 {yyerror ("':' expected"); RECOVER;}
1467 ;
1468
1469 labeled_statement_nsi:
1470 label_decl statement_nsi
1471 { $$ = finish_labeled_statement ($1, $2); }
1472 ;
1473
1474 /* We concentrate here a bunch of error handling rules that we couldn't write
1475 earlier, because expression_statement catches a missing ';'. */
1476 expression_statement:
1477 statement_expression SC_TK
1478 {
1479 /* We have a statement. Generate a WFL around it so
1480 we can debug it */
1481 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1482 /* We know we have a statement, so set the debug
1483 info to be eventually generate here. */
1484 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1485 }
1486 | error SC_TK
1487 {
1488 YYNOT_TWICE yyerror ("Invalid expression statement");
1489 DRECOVER (expr_stmt);
1490 }
1491 | error OCB_TK
1492 {
1493 YYNOT_TWICE yyerror ("Invalid expression statement");
1494 DRECOVER (expr_stmt);
1495 }
1496 | error CCB_TK
1497 {
1498 YYNOT_TWICE yyerror ("Invalid expression statement");
1499 DRECOVER (expr_stmt);
1500 }
1501 | this_or_super OP_TK error
1502 {yyerror ("')' expected"); RECOVER;}
1503 | this_or_super OP_TK CP_TK error
1504 {
1505 parse_ctor_invocation_error ();
1506 RECOVER;
1507 }
1508 | this_or_super OP_TK argument_list error
1509 {yyerror ("')' expected"); RECOVER;}
1510 | this_or_super OP_TK argument_list CP_TK error
1511 {
1512 parse_ctor_invocation_error ();
1513 RECOVER;
1514 }
1515 | name DOT_TK SUPER_TK error
1516 {yyerror ("'(' expected"); RECOVER;}
1517 | name DOT_TK SUPER_TK OP_TK error
1518 {yyerror ("')' expected"); RECOVER;}
1519 | name DOT_TK SUPER_TK OP_TK argument_list error
1520 {yyerror ("')' expected"); RECOVER;}
1521 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1522 {yyerror ("';' expected"); RECOVER;}
1523 | name DOT_TK SUPER_TK OP_TK CP_TK error
1524 {yyerror ("';' expected"); RECOVER;}
1525 ;
1526
1527 statement_expression:
1528 assignment
1529 | pre_increment_expression
1530 | pre_decrement_expression
1531 | post_increment_expression
1532 | post_decrement_expression
1533 | method_invocation
1534 | class_instance_creation_expression
1535 ;
1536
1537 if_then_statement:
1538 IF_TK OP_TK expression CP_TK statement
1539 {
1540 $$ = build_if_else_statement ($2.location, $3,
1541 $5, NULL_TREE);
1542 }
1543 | IF_TK error
1544 {yyerror ("'(' expected"); RECOVER;}
1545 | IF_TK OP_TK error
1546 {yyerror ("Missing term"); RECOVER;}
1547 | IF_TK OP_TK expression error
1548 {yyerror ("')' expected"); RECOVER;}
1549 ;
1550
1551 if_then_else_statement:
1552 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1553 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1554 ;
1555
1556 if_then_else_statement_nsi:
1557 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1558 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1559 ;
1560
1561 switch_statement:
1562 switch_expression
1563 {
1564 enter_block ();
1565 }
1566 switch_block
1567 {
1568 /* Make into "proper list" of COMPOUND_EXPRs.
1569 I.e. make the last statment also have its own
1570 COMPOUND_EXPR. */
1571 maybe_absorb_scoping_blocks ();
1572 TREE_OPERAND ($1, 1) = exit_block ();
1573 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1574 }
1575 ;
1576
1577 switch_expression:
1578 SWITCH_TK OP_TK expression CP_TK
1579 {
1580 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1581 EXPR_WFL_LINECOL ($$) = $2.location;
1582 }
1583 | SWITCH_TK error
1584 {yyerror ("'(' expected"); RECOVER;}
1585 | SWITCH_TK OP_TK error
1586 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1587 | SWITCH_TK OP_TK expression CP_TK error
1588 {yyerror ("'{' expected"); RECOVER;}
1589 ;
1590
1591 /* Default assignment is there to avoid type node on switch_block
1592 node. */
1593
1594 switch_block:
1595 OCB_TK CCB_TK
1596 { $$ = NULL_TREE; }
1597 | OCB_TK switch_labels CCB_TK
1598 { $$ = NULL_TREE; }
1599 | OCB_TK switch_block_statement_groups CCB_TK
1600 { $$ = NULL_TREE; }
1601 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1602 { $$ = NULL_TREE; }
1603 ;
1604
1605 switch_block_statement_groups:
1606 switch_block_statement_group
1607 | switch_block_statement_groups switch_block_statement_group
1608 ;
1609
1610 switch_block_statement_group:
1611 switch_labels block_statements
1612 ;
1613
1614 switch_labels:
1615 switch_label
1616 | switch_labels switch_label
1617 ;
1618
1619 switch_label:
1620 CASE_TK constant_expression REL_CL_TK
1621 {
1622 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1623 EXPR_WFL_LINECOL (lab) = $1.location;
1624 java_method_add_stmt (current_function_decl, lab);
1625 }
1626 | DEFAULT_TK REL_CL_TK
1627 {
1628 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1629 EXPR_WFL_LINECOL (lab) = $1.location;
1630 java_method_add_stmt (current_function_decl, lab);
1631 }
1632 | CASE_TK error
1633 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1634 | CASE_TK constant_expression error
1635 {yyerror ("':' expected"); RECOVER;}
1636 | DEFAULT_TK error
1637 {yyerror ("':' expected"); RECOVER;}
1638 ;
1639
1640 while_expression:
1641 WHILE_TK OP_TK expression CP_TK
1642 {
1643 tree body = build_loop_body ($2.location, $3, 0);
1644 $$ = build_new_loop (body);
1645 }
1646 ;
1647
1648 while_statement:
1649 while_expression statement
1650 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1651 | WHILE_TK error
1652 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1653 | WHILE_TK OP_TK error
1654 {yyerror ("Missing term and ')' expected"); RECOVER;}
1655 | WHILE_TK OP_TK expression error
1656 {yyerror ("')' expected"); RECOVER;}
1657 ;
1658
1659 while_statement_nsi:
1660 while_expression statement_nsi
1661 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1662 ;
1663
1664 do_statement_begin:
1665 DO_TK
1666 {
1667 tree body = build_loop_body (0, NULL_TREE, 1);
1668 $$ = build_new_loop (body);
1669 }
1670 /* Need error handing here. FIXME */
1671 ;
1672
1673 do_statement:
1674 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1675 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1676 ;
1677
1678 for_statement:
1679 for_begin SC_TK expression SC_TK for_update CP_TK statement
1680 {
1681 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1682 $3 = build_wfl_node ($3);
1683 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1684 }
1685 | for_begin SC_TK SC_TK for_update CP_TK statement
1686 {
1687 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1688 /* We have not condition, so we get rid of the EXIT_EXPR */
1689 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1690 empty_stmt_node;
1691 }
1692 | for_begin SC_TK error
1693 {yyerror ("Invalid control expression"); RECOVER;}
1694 | for_begin SC_TK expression SC_TK error
1695 {yyerror ("Invalid update expression"); RECOVER;}
1696 | for_begin SC_TK SC_TK error
1697 {yyerror ("Invalid update expression"); RECOVER;}
1698 ;
1699
1700 for_statement_nsi:
1701 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1702 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1703 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1704 {
1705 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1706 /* We have not condition, so we get rid of the EXIT_EXPR */
1707 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1708 empty_stmt_node;
1709 }
1710 ;
1711
1712 for_header:
1713 FOR_TK OP_TK
1714 {
1715 /* This scope defined for local variable that may be
1716 defined within the scope of the for loop */
1717 enter_block ();
1718 }
1719 | FOR_TK error
1720 {yyerror ("'(' expected"); DRECOVER(for_1);}
1721 | FOR_TK OP_TK error
1722 {yyerror ("Invalid init statement"); RECOVER;}
1723 ;
1724
1725 for_begin:
1726 for_header for_init
1727 {
1728 /* We now declare the loop body. The loop is
1729 declared as a for loop. */
1730 tree body = build_loop_body (0, NULL_TREE, 0);
1731 $$ = build_new_loop (body);
1732 FOR_LOOP_P ($$) = 1;
1733 /* The loop is added to the current block the for
1734 statement is defined within */
1735 java_method_add_stmt (current_function_decl, $$);
1736 }
1737 ;
1738 for_init: /* Can be empty */
1739 { $$ = empty_stmt_node; }
1740 | statement_expression_list
1741 {
1742 /* Init statement recorded within the previously
1743 defined block scope */
1744 $$ = java_method_add_stmt (current_function_decl, $1);
1745 }
1746 | local_variable_declaration
1747 {
1748 /* Local variable are recorded within the previously
1749 defined block scope */
1750 $$ = NULL_TREE;
1751 }
1752 | statement_expression_list error
1753 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1754 ;
1755
1756 for_update: /* Can be empty */
1757 {$$ = empty_stmt_node;}
1758 | statement_expression_list
1759 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1760 ;
1761
1762 statement_expression_list:
1763 statement_expression
1764 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1765 | statement_expression_list C_TK statement_expression
1766 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1767 | statement_expression_list C_TK error
1768 {yyerror ("Missing term"); RECOVER;}
1769 ;
1770
1771 break_statement:
1772 BREAK_TK SC_TK
1773 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1774 | BREAK_TK identifier SC_TK
1775 { $$ = build_bc_statement ($1.location, 1, $2); }
1776 | BREAK_TK error
1777 {yyerror ("Missing term"); RECOVER;}
1778 | BREAK_TK identifier error
1779 {yyerror ("';' expected"); RECOVER;}
1780 ;
1781
1782 continue_statement:
1783 CONTINUE_TK SC_TK
1784 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1785 | CONTINUE_TK identifier SC_TK
1786 { $$ = build_bc_statement ($1.location, 0, $2); }
1787 | CONTINUE_TK error
1788 {yyerror ("Missing term"); RECOVER;}
1789 | CONTINUE_TK identifier error
1790 {yyerror ("';' expected"); RECOVER;}
1791 ;
1792
1793 return_statement:
1794 RETURN_TK SC_TK
1795 { $$ = build_return ($1.location, NULL_TREE); }
1796 | RETURN_TK expression SC_TK
1797 { $$ = build_return ($1.location, $2); }
1798 | RETURN_TK error
1799 {yyerror ("Missing term"); RECOVER;}
1800 | RETURN_TK expression error
1801 {yyerror ("';' expected"); RECOVER;}
1802 ;
1803
1804 throw_statement:
1805 THROW_TK expression SC_TK
1806 {
1807 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1808 EXPR_WFL_LINECOL ($$) = $1.location;
1809 }
1810 | THROW_TK error
1811 {yyerror ("Missing term"); RECOVER;}
1812 | THROW_TK expression error
1813 {yyerror ("';' expected"); RECOVER;}
1814 ;
1815
1816 synchronized_statement:
1817 synchronized OP_TK expression CP_TK block
1818 {
1819 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1820 EXPR_WFL_LINECOL ($$) =
1821 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1822 }
1823 | synchronized OP_TK expression CP_TK error
1824 {yyerror ("'{' expected"); RECOVER;}
1825 | synchronized error
1826 {yyerror ("'(' expected"); RECOVER;}
1827 | synchronized OP_TK error CP_TK
1828 {yyerror ("Missing term"); RECOVER;}
1829 | synchronized OP_TK error
1830 {yyerror ("Missing term"); RECOVER;}
1831 ;
1832
1833 synchronized:
1834 modifiers
1835 {
1836 check_modifiers (
1837 "Illegal modifier `%s'. Only `synchronized' was expected here",
1838 $1, ACC_SYNCHRONIZED);
1839 if ($1 != ACC_SYNCHRONIZED)
1840 MODIFIER_WFL (SYNCHRONIZED_TK) =
1841 build_wfl_node (NULL_TREE);
1842 }
1843 ;
1844
1845 try_statement:
1846 TRY_TK block catches
1847 { $$ = build_try_statement ($1.location, $2, $3); }
1848 | TRY_TK block finally
1849 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1850 | TRY_TK block catches finally
1851 { $$ = build_try_finally_statement
1852 ($1.location, build_try_statement ($1.location,
1853 $2, $3), $4);
1854 }
1855 | TRY_TK error
1856 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1857 ;
1858
1859 catches:
1860 catch_clause
1861 | catches catch_clause
1862 {
1863 TREE_CHAIN ($2) = $1;
1864 $$ = $2;
1865 }
1866 ;
1867
1868 catch_clause:
1869 catch_clause_parameter block
1870 {
1871 java_method_add_stmt (current_function_decl, $2);
1872 exit_block ();
1873 $$ = $1;
1874 }
1875
1876 catch_clause_parameter:
1877 CATCH_TK OP_TK formal_parameter CP_TK
1878 {
1879 /* We add a block to define a scope for
1880 formal_parameter (CCBP). The formal parameter is
1881 declared initialized by the appropriate function
1882 call */
1883 tree ccpb = enter_block ();
1884 tree init = build_assignment (ASSIGN_TK, $2.location,
1885 TREE_PURPOSE ($3),
1886 soft_exceptioninfo_call_node);
1887 declare_local_variables (0, TREE_VALUE ($3),
1888 build_tree_list (TREE_PURPOSE ($3),
1889 init));
1890 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1891 EXPR_WFL_LINECOL ($$) = $1.location;
1892 }
1893 | CATCH_TK error
1894 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1895 | CATCH_TK OP_TK error
1896 {
1897 yyerror ("Missing term or ')' expected");
1898 RECOVER; $$ = NULL_TREE;
1899 }
1900 | CATCH_TK OP_TK error CP_TK /* That's for () */
1901 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1902 ;
1903
1904 finally:
1905 FINALLY_TK block
1906 { $$ = $2; }
1907 | FINALLY_TK error
1908 {yyerror ("'{' expected"); RECOVER; }
1909 ;
1910
1911 /* 19.12 Production from 15: Expressions */
1912 primary:
1913 primary_no_new_array
1914 | array_creation_expression
1915 ;
1916
1917 primary_no_new_array:
1918 literal
1919 | THIS_TK
1920 { $$ = build_this ($1.location); }
1921 | OP_TK expression CP_TK
1922 {$$ = $2;}
1923 | class_instance_creation_expression
1924 | field_access
1925 | method_invocation
1926 | array_access
1927 | type_literals
1928 /* Added, JDK1.1 inner classes. Documentation is wrong
1929 refering to a 'ClassName' (class_name) rule that doesn't
1930 exist. Used name: instead. */
1931 | name DOT_TK THIS_TK
1932 {
1933 tree wfl = build_wfl_node (this_identifier_node);
1934 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1935 }
1936 | OP_TK expression error
1937 {yyerror ("')' expected"); RECOVER;}
1938 | name DOT_TK error
1939 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1940 | primitive_type DOT_TK error
1941 {yyerror ("'class' expected" ); RECOVER;}
1942 | VOID_TK DOT_TK error
1943 {yyerror ("'class' expected" ); RECOVER;}
1944 ;
1945
1946 type_literals:
1947 name DOT_TK CLASS_TK
1948 { $$ = build_incomplete_class_ref ($2.location, $1); }
1949 | array_type DOT_TK CLASS_TK
1950 { $$ = build_incomplete_class_ref ($2.location, $1); }
1951 | primitive_type DOT_TK CLASS_TK
1952 { $$ = build_class_ref ($1); }
1953 | VOID_TK DOT_TK CLASS_TK
1954 { $$ = build_class_ref (void_type_node); }
1955 ;
1956
1957 class_instance_creation_expression:
1958 NEW_TK class_type OP_TK argument_list CP_TK
1959 { $$ = build_new_invocation ($2, $4); }
1960 | NEW_TK class_type OP_TK CP_TK
1961 { $$ = build_new_invocation ($2, NULL_TREE); }
1962 | anonymous_class_creation
1963 /* Added, JDK1.1 inner classes, modified to use name or
1964 primary instead of primary solely which couldn't work in
1965 all situations. */
1966 | something_dot_new identifier OP_TK CP_TK
1967 {
1968 tree ctor = build_new_invocation ($2, NULL_TREE);
1969 $$ = make_qualified_primary ($1, ctor,
1970 EXPR_WFL_LINECOL ($1));
1971 }
1972 | something_dot_new identifier OP_TK CP_TK class_body
1973 | something_dot_new identifier OP_TK argument_list CP_TK
1974 {
1975 tree ctor = build_new_invocation ($2, $4);
1976 $$ = make_qualified_primary ($1, ctor,
1977 EXPR_WFL_LINECOL ($1));
1978 }
1979 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1980 | NEW_TK error SC_TK
1981 {yyerror ("'(' expected"); DRECOVER(new_1);}
1982 | NEW_TK class_type error
1983 {yyerror ("'(' expected"); RECOVER;}
1984 | NEW_TK class_type OP_TK error
1985 {yyerror ("')' or term expected"); RECOVER;}
1986 | NEW_TK class_type OP_TK argument_list error
1987 {yyerror ("')' expected"); RECOVER;}
1988 | something_dot_new error
1989 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1990 | something_dot_new identifier error
1991 {yyerror ("'(' expected"); RECOVER;}
1992 ;
1993
1994 /* Created after JDK1.1 rules originally added to
1995 class_instance_creation_expression, but modified to use
1996 'class_type' instead of 'TypeName' (type_name) which is mentionned
1997 in the documentation but doesn't exist. */
1998
1999 anonymous_class_creation:
2000 NEW_TK class_type OP_TK argument_list CP_TK
2001 { create_anonymous_class ($1.location, $2); }
2002 class_body
2003 {
2004 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2005 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2006
2007 end_class_declaration (1);
2008
2009 /* Now we can craft the new expression */
2010 $$ = build_new_invocation (id, $4);
2011
2012 /* Note that we can't possibly be here if
2013 `class_type' is an interface (in which case the
2014 anonymous class extends Object and implements
2015 `class_type', hence its constructor can't have
2016 arguments.) */
2017
2018 /* Otherwise, the innerclass must feature a
2019 constructor matching `argument_list'. Anonymous
2020 classes are a bit special: it's impossible to
2021 define constructor for them, hence constructors
2022 must be generated following the hints provided by
2023 the `new' expression. Whether a super constructor
2024 of that nature exists or not is to be verified
2025 later on in verify_constructor_super.
2026
2027 It's during the expansion of a `new' statement
2028 refering to an anonymous class that a ctor will
2029 be generated for the anonymous class, with the
2030 right arguments. */
2031
2032 }
2033 | NEW_TK class_type OP_TK CP_TK
2034 { create_anonymous_class ($1.location, $2); }
2035 class_body
2036 {
2037 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2038 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2039
2040 end_class_declaration (1);
2041
2042 /* Now we can craft the new expression. The
2043 statement doesn't need to be remember so that a
2044 constructor can be generated, since its signature
2045 is already known. */
2046 $$ = build_new_invocation (id, NULL_TREE);
2047 }
2048 ;
2049
2050 something_dot_new: /* Added, not part of the specs. */
2051 name DOT_TK NEW_TK
2052 { $$ = $1; }
2053 | primary DOT_TK NEW_TK
2054 { $$ = $1; }
2055 ;
2056
2057 argument_list:
2058 expression
2059 {
2060 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2061 ctxp->formal_parameter_number = 1;
2062 }
2063 | argument_list C_TK expression
2064 {
2065 ctxp->formal_parameter_number += 1;
2066 $$ = tree_cons (NULL_TREE, $3, $1);
2067 }
2068 | argument_list C_TK error
2069 {yyerror ("Missing term"); RECOVER;}
2070 ;
2071
2072 array_creation_expression:
2073 NEW_TK primitive_type dim_exprs
2074 { $$ = build_newarray_node ($2, $3, 0); }
2075 | NEW_TK class_or_interface_type dim_exprs
2076 { $$ = build_newarray_node ($2, $3, 0); }
2077 | NEW_TK primitive_type dim_exprs dims
2078 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2079 | NEW_TK class_or_interface_type dim_exprs dims
2080 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2081 /* Added, JDK1.1 anonymous array. Initial documentation rule
2082 modified */
2083 | NEW_TK class_or_interface_type dims array_initializer
2084 {
2085 char *sig;
2086 int osb = pop_current_osb (ctxp);
2087 while (osb--)
2088 obstack_1grow (&temporary_obstack, '[');
2089 sig = obstack_finish (&temporary_obstack);
2090 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2091 $2, get_identifier (sig), $4);
2092 }
2093 | NEW_TK primitive_type dims array_initializer
2094 {
2095 int osb = pop_current_osb (ctxp);
2096 tree type = $2;
2097 while (osb--)
2098 type = build_java_array_type (type, -1);
2099 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2100 build_pointer_type (type), NULL_TREE, $4);
2101 }
2102 | NEW_TK error CSB_TK
2103 {yyerror ("'[' expected"); DRECOVER ("]");}
2104 | NEW_TK error OSB_TK
2105 {yyerror ("']' expected"); RECOVER;}
2106 ;
2107
2108 dim_exprs:
2109 dim_expr
2110 { $$ = build_tree_list (NULL_TREE, $1); }
2111 | dim_exprs dim_expr
2112 { $$ = tree_cons (NULL_TREE, $2, $$); }
2113 ;
2114
2115 dim_expr:
2116 OSB_TK expression CSB_TK
2117 {
2118 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2119 {
2120 $2 = build_wfl_node ($2);
2121 TREE_TYPE ($2) = NULL_TREE;
2122 }
2123 EXPR_WFL_LINECOL ($2) = $1.location;
2124 $$ = $2;
2125 }
2126 | OSB_TK expression error
2127 {yyerror ("']' expected"); RECOVER;}
2128 | OSB_TK error
2129 {
2130 yyerror ("Missing term");
2131 yyerror ("']' expected");
2132 RECOVER;
2133 }
2134 ;
2135
2136 dims:
2137 OSB_TK CSB_TK
2138 {
2139 int allocate = 0;
2140 /* If not initialized, allocate memory for the osb
2141 numbers stack */
2142 if (!ctxp->osb_limit)
2143 {
2144 allocate = ctxp->osb_limit = 32;
2145 ctxp->osb_depth = -1;
2146 }
2147 /* If capacity overflown, reallocate a bigger chunk */
2148 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2149 allocate = ctxp->osb_limit << 1;
2150
2151 if (allocate)
2152 {
2153 allocate *= sizeof (int);
2154 if (ctxp->osb_number)
2155 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2156 allocate);
2157 else
2158 ctxp->osb_number = (int *)xmalloc (allocate);
2159 }
2160 ctxp->osb_depth++;
2161 CURRENT_OSB (ctxp) = 1;
2162 }
2163 | dims OSB_TK CSB_TK
2164 { CURRENT_OSB (ctxp)++; }
2165 | dims OSB_TK error
2166 { yyerror ("']' expected"); RECOVER;}
2167 ;
2168
2169 field_access:
2170 primary DOT_TK identifier
2171 { $$ = make_qualified_primary ($1, $3, $2.location); }
2172 /* FIXME - REWRITE TO:
2173 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2174 | SUPER_TK DOT_TK identifier
2175 {
2176 tree super_wfl = build_wfl_node (super_identifier_node);
2177 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2178 $$ = make_qualified_name (super_wfl, $3, $2.location);
2179 }
2180 | SUPER_TK error
2181 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2182 ;
2183
2184 method_invocation:
2185 name OP_TK CP_TK
2186 { $$ = build_method_invocation ($1, NULL_TREE); }
2187 | name OP_TK argument_list CP_TK
2188 { $$ = build_method_invocation ($1, $3); }
2189 | primary DOT_TK identifier OP_TK CP_TK
2190 {
2191 if (TREE_CODE ($1) == THIS_EXPR)
2192 $$ = build_this_super_qualified_invocation
2193 (1, $3, NULL_TREE, 0, $2.location);
2194 else
2195 {
2196 tree invok = build_method_invocation ($3, NULL_TREE);
2197 $$ = make_qualified_primary ($1, invok, $2.location);
2198 }
2199 }
2200 | primary DOT_TK identifier OP_TK argument_list CP_TK
2201 {
2202 if (TREE_CODE ($1) == THIS_EXPR)
2203 $$ = build_this_super_qualified_invocation
2204 (1, $3, $5, 0, $2.location);
2205 else
2206 {
2207 tree invok = build_method_invocation ($3, $5);
2208 $$ = make_qualified_primary ($1, invok, $2.location);
2209 }
2210 }
2211 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2212 {
2213 $$ = build_this_super_qualified_invocation
2214 (0, $3, NULL_TREE, $1.location, $2.location);
2215 }
2216 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2217 {
2218 $$ = build_this_super_qualified_invocation
2219 (0, $3, $5, $1.location, $2.location);
2220 }
2221 /* Screws up thing. I let it here until I'm convinced it can
2222 be removed. FIXME
2223 | primary DOT_TK error
2224 {yyerror ("'(' expected"); DRECOVER(bad);} */
2225 | SUPER_TK DOT_TK error CP_TK
2226 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2227 | SUPER_TK DOT_TK error DOT_TK
2228 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2229 ;
2230
2231 array_access:
2232 name OSB_TK expression CSB_TK
2233 { $$ = build_array_ref ($2.location, $1, $3); }
2234 | primary_no_new_array OSB_TK expression CSB_TK
2235 { $$ = build_array_ref ($2.location, $1, $3); }
2236 | name OSB_TK error
2237 {
2238 yyerror ("Missing term and ']' expected");
2239 DRECOVER(array_access);
2240 }
2241 | name OSB_TK expression error
2242 {
2243 yyerror ("']' expected");
2244 DRECOVER(array_access);
2245 }
2246 | primary_no_new_array OSB_TK error
2247 {
2248 yyerror ("Missing term and ']' expected");
2249 DRECOVER(array_access);
2250 }
2251 | primary_no_new_array OSB_TK expression error
2252 {
2253 yyerror ("']' expected");
2254 DRECOVER(array_access);
2255 }
2256 ;
2257
2258 postfix_expression:
2259 primary
2260 | name
2261 | post_increment_expression
2262 | post_decrement_expression
2263 ;
2264
2265 post_increment_expression:
2266 postfix_expression INCR_TK
2267 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2268 ;
2269
2270 post_decrement_expression:
2271 postfix_expression DECR_TK
2272 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2273 ;
2274
2275 unary_expression:
2276 pre_increment_expression
2277 | pre_decrement_expression
2278 | PLUS_TK unary_expression
2279 {$$ = build_unaryop ($1.token, $1.location, $2); }
2280 | MINUS_TK unary_expression
2281 {$$ = build_unaryop ($1.token, $1.location, $2); }
2282 | unary_expression_not_plus_minus
2283 | PLUS_TK error
2284 {yyerror ("Missing term"); RECOVER}
2285 | MINUS_TK error
2286 {yyerror ("Missing term"); RECOVER}
2287 ;
2288
2289 pre_increment_expression:
2290 INCR_TK unary_expression
2291 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2292 | INCR_TK error
2293 {yyerror ("Missing term"); RECOVER}
2294 ;
2295
2296 pre_decrement_expression:
2297 DECR_TK unary_expression
2298 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2299 | DECR_TK error
2300 {yyerror ("Missing term"); RECOVER}
2301 ;
2302
2303 unary_expression_not_plus_minus:
2304 postfix_expression
2305 | NOT_TK unary_expression
2306 {$$ = build_unaryop ($1.token, $1.location, $2); }
2307 | NEG_TK unary_expression
2308 {$$ = build_unaryop ($1.token, $1.location, $2); }
2309 | cast_expression
2310 | NOT_TK error
2311 {yyerror ("Missing term"); RECOVER}
2312 | NEG_TK error
2313 {yyerror ("Missing term"); RECOVER}
2314 ;
2315
2316 cast_expression: /* Error handling here is potentially weak */
2317 OP_TK primitive_type dims CP_TK unary_expression
2318 {
2319 tree type = $2;
2320 int osb = pop_current_osb (ctxp);
2321 while (osb--)
2322 type = build_java_array_type (type, -1);
2323 $$ = build_cast ($1.location, type, $5);
2324 }
2325 | OP_TK primitive_type CP_TK unary_expression
2326 { $$ = build_cast ($1.location, $2, $4); }
2327 | OP_TK expression CP_TK unary_expression_not_plus_minus
2328 { $$ = build_cast ($1.location, $2, $4); }
2329 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2330 {
2331 const char *ptr;
2332 int osb = pop_current_osb (ctxp);
2333 while (osb--)
2334 obstack_1grow (&temporary_obstack, '[');
2335 obstack_grow0 (&temporary_obstack,
2336 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2337 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2338 ptr = obstack_finish (&temporary_obstack);
2339 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2340 $$ = build_cast ($1.location, $2, $5);
2341 }
2342 | OP_TK primitive_type OSB_TK error
2343 {yyerror ("']' expected, invalid type expression");}
2344 | OP_TK error
2345 {
2346 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2347 RECOVER;
2348 }
2349 | OP_TK primitive_type dims CP_TK error
2350 {yyerror ("Missing term"); RECOVER;}
2351 | OP_TK primitive_type CP_TK error
2352 {yyerror ("Missing term"); RECOVER;}
2353 | OP_TK name dims CP_TK error
2354 {yyerror ("Missing term"); RECOVER;}
2355 ;
2356
2357 multiplicative_expression:
2358 unary_expression
2359 | multiplicative_expression MULT_TK unary_expression
2360 {
2361 $$ = build_binop (BINOP_LOOKUP ($2.token),
2362 $2.location, $1, $3);
2363 }
2364 | multiplicative_expression DIV_TK unary_expression
2365 {
2366 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2367 $1, $3);
2368 }
2369 | multiplicative_expression REM_TK unary_expression
2370 {
2371 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2372 $1, $3);
2373 }
2374 | multiplicative_expression MULT_TK error
2375 {yyerror ("Missing term"); RECOVER;}
2376 | multiplicative_expression DIV_TK error
2377 {yyerror ("Missing term"); RECOVER;}
2378 | multiplicative_expression REM_TK error
2379 {yyerror ("Missing term"); RECOVER;}
2380 ;
2381
2382 additive_expression:
2383 multiplicative_expression
2384 | additive_expression PLUS_TK multiplicative_expression
2385 {
2386 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2387 $1, $3);
2388 }
2389 | additive_expression MINUS_TK multiplicative_expression
2390 {
2391 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2392 $1, $3);
2393 }
2394 | additive_expression PLUS_TK error
2395 {yyerror ("Missing term"); RECOVER;}
2396 | additive_expression MINUS_TK error
2397 {yyerror ("Missing term"); RECOVER;}
2398 ;
2399
2400 shift_expression:
2401 additive_expression
2402 | shift_expression LS_TK additive_expression
2403 {
2404 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2405 $1, $3);
2406 }
2407 | shift_expression SRS_TK additive_expression
2408 {
2409 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2410 $1, $3);
2411 }
2412 | shift_expression ZRS_TK additive_expression
2413 {
2414 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2415 $1, $3);
2416 }
2417 | shift_expression LS_TK error
2418 {yyerror ("Missing term"); RECOVER;}
2419 | shift_expression SRS_TK error
2420 {yyerror ("Missing term"); RECOVER;}
2421 | shift_expression ZRS_TK error
2422 {yyerror ("Missing term"); RECOVER;}
2423 ;
2424
2425 relational_expression:
2426 shift_expression
2427 | relational_expression LT_TK shift_expression
2428 {
2429 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2430 $1, $3);
2431 }
2432 | relational_expression GT_TK shift_expression
2433 {
2434 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2435 $1, $3);
2436 }
2437 | relational_expression LTE_TK shift_expression
2438 {
2439 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2440 $1, $3);
2441 }
2442 | relational_expression GTE_TK shift_expression
2443 {
2444 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2445 $1, $3);
2446 }
2447 | relational_expression INSTANCEOF_TK reference_type
2448 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2449 | relational_expression LT_TK error
2450 {yyerror ("Missing term"); RECOVER;}
2451 | relational_expression GT_TK error
2452 {yyerror ("Missing term"); RECOVER;}
2453 | relational_expression LTE_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2455 | relational_expression GTE_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457 | relational_expression INSTANCEOF_TK error
2458 {yyerror ("Invalid reference type"); RECOVER;}
2459 ;
2460
2461 equality_expression:
2462 relational_expression
2463 | equality_expression EQ_TK relational_expression
2464 {
2465 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2466 $1, $3);
2467 }
2468 | equality_expression NEQ_TK relational_expression
2469 {
2470 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2471 $1, $3);
2472 }
2473 | equality_expression EQ_TK error
2474 {yyerror ("Missing term"); RECOVER;}
2475 | equality_expression NEQ_TK error
2476 {yyerror ("Missing term"); RECOVER;}
2477 ;
2478
2479 and_expression:
2480 equality_expression
2481 | and_expression AND_TK equality_expression
2482 {
2483 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2484 $1, $3);
2485 }
2486 | and_expression AND_TK error
2487 {yyerror ("Missing term"); RECOVER;}
2488 ;
2489
2490 exclusive_or_expression:
2491 and_expression
2492 | exclusive_or_expression XOR_TK and_expression
2493 {
2494 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2495 $1, $3);
2496 }
2497 | exclusive_or_expression XOR_TK error
2498 {yyerror ("Missing term"); RECOVER;}
2499 ;
2500
2501 inclusive_or_expression:
2502 exclusive_or_expression
2503 | inclusive_or_expression OR_TK exclusive_or_expression
2504 {
2505 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2506 $1, $3);
2507 }
2508 | inclusive_or_expression OR_TK error
2509 {yyerror ("Missing term"); RECOVER;}
2510 ;
2511
2512 conditional_and_expression:
2513 inclusive_or_expression
2514 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2515 {
2516 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2517 $1, $3);
2518 }
2519 | conditional_and_expression BOOL_AND_TK error
2520 {yyerror ("Missing term"); RECOVER;}
2521 ;
2522
2523 conditional_or_expression:
2524 conditional_and_expression
2525 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2526 {
2527 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2528 $1, $3);
2529 }
2530 | conditional_or_expression BOOL_OR_TK error
2531 {yyerror ("Missing term"); RECOVER;}
2532 ;
2533
2534 conditional_expression: /* Error handling here is weak */
2535 conditional_or_expression
2536 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2537 {
2538 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2539 EXPR_WFL_LINECOL ($$) = $2.location;
2540 }
2541 | conditional_or_expression REL_QM_TK REL_CL_TK error
2542 {
2543 YYERROR_NOW;
2544 yyerror ("Missing term");
2545 DRECOVER (1);
2546 }
2547 | conditional_or_expression REL_QM_TK error
2548 {yyerror ("Missing term"); DRECOVER (2);}
2549 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2550 {yyerror ("Missing term"); DRECOVER (3);}
2551 ;
2552
2553 assignment_expression:
2554 conditional_expression
2555 | assignment
2556 ;
2557
2558 assignment:
2559 left_hand_side assignment_operator assignment_expression
2560 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2561 | left_hand_side assignment_operator error
2562 {
2563 YYNOT_TWICE yyerror ("Missing term");
2564 DRECOVER (assign);
2565 }
2566 ;
2567
2568 left_hand_side:
2569 name
2570 | field_access
2571 | array_access
2572 ;
2573
2574 assignment_operator:
2575 ASSIGN_ANY_TK
2576 | ASSIGN_TK
2577 ;
2578
2579 expression:
2580 assignment_expression
2581 ;
2582
2583 constant_expression:
2584 expression
2585 ;
2586
2587 %%
2588
2589 /* Helper function to retrieve an OSB count. Should be used when the
2590 `dims:' rule is being used. */
2591
2592 static int
2593 pop_current_osb (ctxp)
2594 struct parser_ctxt *ctxp;
2595 {
2596 int to_return;
2597
2598 if (ctxp->osb_depth < 0)
2599 abort ();
2600
2601 to_return = CURRENT_OSB (ctxp);
2602 ctxp->osb_depth--;
2603
2604 return to_return;
2605 }
2606
2607 \f
2608
2609 /* This section of the code deal with save/restoring parser contexts.
2610 Add mode documentation here. FIXME */
2611
2612 /* Helper function. Create a new parser context. With
2613 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2614 context is copied, otherwise, the new context is zeroed. The newly
2615 created context becomes the current one. */
2616
2617 static void
2618 create_new_parser_context (copy_from_previous)
2619 int copy_from_previous;
2620 {
2621 struct parser_ctxt *new;
2622
2623 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2624 if (copy_from_previous)
2625 {
2626 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2627 new->saved_data_ctx = 1;
2628 }
2629 else
2630 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2631
2632 new->next = ctxp;
2633 ctxp = new;
2634 }
2635
2636 /* Create a new parser context and make it the current one. */
2637
2638 void
2639 java_push_parser_context ()
2640 {
2641 create_new_parser_context (0);
2642 if (ctxp->next)
2643 {
2644 ctxp->incomplete_class = ctxp->next->incomplete_class;
2645 ctxp->gclass_list = ctxp->next->gclass_list;
2646 }
2647 }
2648
2649 void
2650 java_pop_parser_context (generate)
2651 int generate;
2652 {
2653 tree current;
2654 struct parser_ctxt *toFree, *next;
2655
2656 if (!ctxp)
2657 return;
2658
2659 toFree = ctxp;
2660 next = ctxp->next;
2661 if (next)
2662 {
2663 next->incomplete_class = ctxp->incomplete_class;
2664 next->gclass_list = ctxp->gclass_list;
2665 lineno = ctxp->lineno;
2666 current_class = ctxp->class_type;
2667 }
2668
2669 /* If the old and new lexers differ, then free the old one. */
2670 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2671 java_destroy_lexer (ctxp->lexer);
2672
2673 /* Set the single import class file flag to 0 for the current list
2674 of imported things */
2675 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2676 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2677
2678 /* And restore those of the previous context */
2679 if ((ctxp = next)) /* Assignment is really meant here */
2680 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2681 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2682
2683 /* If we pushed a context to parse a class intended to be generated,
2684 we keep it so we can remember the class. What we could actually
2685 do is to just update a list of class names. */
2686 if (generate)
2687 {
2688 toFree->next = ctxp_for_generation;
2689 ctxp_for_generation = toFree;
2690 }
2691 else
2692 free (toFree);
2693 }
2694
2695 /* Create a parser context for the use of saving some global
2696 variables. */
2697
2698 void
2699 java_parser_context_save_global ()
2700 {
2701 if (!ctxp)
2702 {
2703 java_push_parser_context ();
2704 ctxp->saved_data_ctx = 1;
2705 }
2706
2707 /* If this context already stores data, create a new one suitable
2708 for data storage. */
2709 else if (ctxp->saved_data)
2710 create_new_parser_context (1);
2711
2712 ctxp->lineno = lineno;
2713 ctxp->class_type = current_class;
2714 ctxp->filename = input_filename;
2715 ctxp->function_decl = current_function_decl;
2716 ctxp->saved_data = 1;
2717 }
2718
2719 /* Restore some global variables from the previous context. Make the
2720 previous context the current one. */
2721
2722 void
2723 java_parser_context_restore_global ()
2724 {
2725 lineno = ctxp->lineno;
2726 current_class = ctxp->class_type;
2727 input_filename = ctxp->filename;
2728 if (wfl_operator)
2729 {
2730 tree s;
2731 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2732 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2733 }
2734 current_function_decl = ctxp->function_decl;
2735 ctxp->saved_data = 0;
2736 if (ctxp->saved_data_ctx)
2737 java_pop_parser_context (0);
2738 }
2739
2740 /* Suspend vital data for the current class/function being parsed so
2741 that an other class can be parsed. Used to let local/anonymous
2742 classes be parsed. */
2743
2744 static void
2745 java_parser_context_suspend ()
2746 {
2747 /* This makes debugging through java_debug_context easier */
2748 static const char *name = "<inner buffer context>";
2749
2750 /* Duplicate the previous context, use it to save the globals we're
2751 interested in */
2752 create_new_parser_context (1);
2753 ctxp->function_decl = current_function_decl;
2754 ctxp->class_type = current_class;
2755
2756 /* Then create a new context which inherits all data from the
2757 previous one. This will be the new current context */
2758 create_new_parser_context (1);
2759
2760 /* Help debugging */
2761 ctxp->next->filename = name;
2762 }
2763
2764 /* Resume vital data for the current class/function being parsed so
2765 that an other class can be parsed. Used to let local/anonymous
2766 classes be parsed. The trick is the data storing file position
2767 informations must be restored to their current value, so parsing
2768 can resume as if no context was ever saved. */
2769
2770 static void
2771 java_parser_context_resume ()
2772 {
2773 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2774 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2775 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2776
2777 /* We need to inherit the list of classes to complete/generate */
2778 restored->incomplete_class = old->incomplete_class;
2779 restored->gclass_list = old->gclass_list;
2780 restored->classd_list = old->classd_list;
2781 restored->class_list = old->class_list;
2782
2783 /* Restore the current class and function from the saver */
2784 current_class = saver->class_type;
2785 current_function_decl = saver->function_decl;
2786
2787 /* Retrive the restored context */
2788 ctxp = restored;
2789
2790 /* Re-installed the data for the parsing to carry on */
2791 bcopy (&old->marker_begining, &ctxp->marker_begining,
2792 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2793
2794 /* Buffer context can now be discarded */
2795 free (saver);
2796 free (old);
2797 }
2798
2799 /* Add a new anchor node to which all statement(s) initializing static
2800 and non static initialized upon declaration field(s) will be
2801 linked. */
2802
2803 static void
2804 java_parser_context_push_initialized_field ()
2805 {
2806 tree node;
2807
2808 node = build_tree_list (NULL_TREE, NULL_TREE);
2809 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2810 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2811
2812 node = build_tree_list (NULL_TREE, NULL_TREE);
2813 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2814 CPC_INITIALIZER_LIST (ctxp) = node;
2815
2816 node = build_tree_list (NULL_TREE, NULL_TREE);
2817 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2818 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2819 }
2820
2821 /* Pop the lists of initialized field. If this lists aren't empty,
2822 remember them so we can use it to create and populate the finit$
2823 or <clinit> functions. */
2824
2825 static void
2826 java_parser_context_pop_initialized_field ()
2827 {
2828 tree stmts;
2829 tree class_type = TREE_TYPE (GET_CPC ());
2830
2831 if (CPC_INITIALIZER_LIST (ctxp))
2832 {
2833 stmts = CPC_INITIALIZER_STMT (ctxp);
2834 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2835 if (stmts && !java_error_count)
2836 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2837 }
2838
2839 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2840 {
2841 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2842 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2843 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2844 /* Keep initialization in order to enforce 8.5 */
2845 if (stmts && !java_error_count)
2846 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2847 }
2848
2849 /* JDK 1.1 instance initializers */
2850 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2851 {
2852 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2853 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2854 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2855 if (stmts && !java_error_count)
2856 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2857 }
2858 }
2859
2860 static tree
2861 reorder_static_initialized (list)
2862 tree list;
2863 {
2864 /* We have to keep things in order. The alias initializer have to
2865 come first, then the initialized regular field, in reverse to
2866 keep them in lexical order. */
2867 tree marker, previous = NULL_TREE;
2868 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2869 if (TREE_CODE (marker) == TREE_LIST
2870 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2871 break;
2872
2873 /* No static initialized, the list is fine as is */
2874 if (!previous)
2875 list = TREE_CHAIN (marker);
2876
2877 /* No marker? reverse the whole list */
2878 else if (!marker)
2879 list = nreverse (list);
2880
2881 /* Otherwise, reverse what's after the marker and the new reordered
2882 sublist will replace the marker. */
2883 else
2884 {
2885 TREE_CHAIN (previous) = NULL_TREE;
2886 list = nreverse (list);
2887 list = chainon (TREE_CHAIN (marker), list);
2888 }
2889 return list;
2890 }
2891
2892 /* Helper functions to dump the parser context stack. */
2893
2894 #define TAB_CONTEXT(C) \
2895 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2896
2897 static void
2898 java_debug_context_do (tab)
2899 int tab;
2900 {
2901 struct parser_ctxt *copy = ctxp;
2902 while (copy)
2903 {
2904 TAB_CONTEXT (tab);
2905 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2906 TAB_CONTEXT (tab);
2907 fprintf (stderr, "filename: %s\n", copy->filename);
2908 TAB_CONTEXT (tab);
2909 fprintf (stderr, "lineno: %d\n", copy->lineno);
2910 TAB_CONTEXT (tab);
2911 fprintf (stderr, "package: %s\n",
2912 (copy->package ?
2913 IDENTIFIER_POINTER (copy->package) : "<none>"));
2914 TAB_CONTEXT (tab);
2915 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2916 TAB_CONTEXT (tab);
2917 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2918 copy = copy->next;
2919 tab += 2;
2920 }
2921 }
2922
2923 /* Dump the stacked up parser contexts. Intended to be called from a
2924 debugger. */
2925
2926 void
2927 java_debug_context ()
2928 {
2929 java_debug_context_do (0);
2930 }
2931
2932 \f
2933
2934 /* Flag for the error report routine to issue the error the first time
2935 it's called (overriding the default behavior which is to drop the
2936 first invocation and honor the second one, taking advantage of a
2937 richer context. */
2938 static int force_error = 0;
2939
2940 /* Reporting an constructor invocation error. */
2941 static void
2942 parse_ctor_invocation_error ()
2943 {
2944 if (DECL_CONSTRUCTOR_P (current_function_decl))
2945 yyerror ("Constructor invocation must be first thing in a constructor");
2946 else
2947 yyerror ("Only constructors can invoke constructors");
2948 }
2949
2950 /* Reporting JDK1.1 features not implemented. */
2951
2952 static tree
2953 parse_jdk1_1_error (msg)
2954 const char *msg;
2955 {
2956 sorry (": `%s' JDK1.1(TM) feature", msg);
2957 java_error_count++;
2958 return empty_stmt_node;
2959 }
2960
2961 static int do_warning = 0;
2962
2963 void
2964 yyerror (msg)
2965 const char *msg;
2966 {
2967 static java_lc elc;
2968 static int prev_lineno;
2969 static const char *prev_msg;
2970
2971 int save_lineno;
2972 char *remainder, *code_from_source;
2973
2974 if (!force_error && prev_lineno == lineno)
2975 return;
2976
2977 /* Save current error location but report latter, when the context is
2978 richer. */
2979 if (ctxp->java_error_flag == 0)
2980 {
2981 ctxp->java_error_flag = 1;
2982 elc = ctxp->elc;
2983 /* Do something to use the previous line if we're reaching the
2984 end of the file... */
2985 #ifdef VERBOSE_SKELETON
2986 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2987 #endif
2988 return;
2989 }
2990
2991 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2992 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2993 return;
2994
2995 ctxp->java_error_flag = 0;
2996 if (do_warning)
2997 java_warning_count++;
2998 else
2999 java_error_count++;
3000
3001 if (elc.col == 0 && msg && msg[1] == ';')
3002 {
3003 elc.col = ctxp->p_line->char_col-1;
3004 elc.line = ctxp->p_line->lineno;
3005 }
3006
3007 save_lineno = lineno;
3008 prev_lineno = lineno = elc.line;
3009 prev_msg = msg;
3010
3011 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3012 obstack_grow0 (&temporary_obstack,
3013 code_from_source, strlen (code_from_source));
3014 remainder = obstack_finish (&temporary_obstack);
3015 if (do_warning)
3016 warning ("%s.\n%s", msg, remainder);
3017 else
3018 error ("%s.\n%s", msg, remainder);
3019
3020 /* This allow us to cheaply avoid an extra 'Invalid expression
3021 statement' error report when errors have been already reported on
3022 the same line. This occurs when we report an error but don't have
3023 a synchronization point other than ';', which
3024 expression_statement is the only one to take care of. */
3025 ctxp->prevent_ese = lineno = save_lineno;
3026 }
3027
3028 static void
3029 issue_warning_error_from_context (cl, msg, ap)
3030 tree cl;
3031 const char *msg;
3032 va_list ap;
3033 {
3034 const char *saved, *saved_input_filename;
3035 char buffer [4096];
3036 vsprintf (buffer, msg, ap);
3037 force_error = 1;
3038
3039 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3040 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3041 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3042
3043 /* We have a CL, that's a good reason for using it if it contains data */
3044 saved = ctxp->filename;
3045 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3046 ctxp->filename = EXPR_WFL_FILENAME (cl);
3047 saved_input_filename = input_filename;
3048 input_filename = ctxp->filename;
3049 java_error (NULL);
3050 java_error (buffer);
3051 ctxp->filename = saved;
3052 input_filename = saved_input_filename;
3053 force_error = 0;
3054 }
3055
3056 /* Issue an error message at a current source line CL */
3057
3058 void
3059 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3060 {
3061 #ifndef ANSI_PROTOTYPES
3062 tree cl;
3063 const char *msg;
3064 #endif
3065 va_list ap;
3066
3067 VA_START (ap, msg);
3068 #ifndef ANSI_PROTOTYPES
3069 cl = va_arg (ap, tree);
3070 msg = va_arg (ap, const char *);
3071 #endif
3072 issue_warning_error_from_context (cl, msg, ap);
3073 va_end (ap);
3074 }
3075
3076 /* Issue a warning at a current source line CL */
3077
3078 static void
3079 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3080 {
3081 #ifndef ANSI_PROTOTYPES
3082 tree cl;
3083 const char *msg;
3084 #endif
3085 va_list ap;
3086
3087 VA_START (ap, msg);
3088 #ifndef ANSI_PROTOTYPES
3089 cl = va_arg (ap, tree);
3090 msg = va_arg (ap, const char *);
3091 #endif
3092
3093 force_error = do_warning = 1;
3094 issue_warning_error_from_context (cl, msg, ap);
3095 do_warning = force_error = 0;
3096 va_end (ap);
3097 }
3098
3099 static tree
3100 find_expr_with_wfl (node)
3101 tree node;
3102 {
3103 while (node)
3104 {
3105 char code;
3106 tree to_return;
3107
3108 switch (TREE_CODE (node))
3109 {
3110 case BLOCK:
3111 node = BLOCK_EXPR_BODY (node);
3112 continue;
3113
3114 case COMPOUND_EXPR:
3115 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3116 if (to_return)
3117 return to_return;
3118 node = TREE_OPERAND (node, 1);
3119 continue;
3120
3121 case LOOP_EXPR:
3122 node = TREE_OPERAND (node, 0);
3123 continue;
3124
3125 case LABELED_BLOCK_EXPR:
3126 node = TREE_OPERAND (node, 1);
3127 continue;
3128
3129 default:
3130 code = TREE_CODE_CLASS (TREE_CODE (node));
3131 if (((code == '1') || (code == '2') || (code == 'e'))
3132 && EXPR_WFL_LINECOL (node))
3133 return node;
3134 return NULL_TREE;
3135 }
3136 }
3137 return NULL_TREE;
3138 }
3139
3140 /* Issue a missing return statement error. Uses METHOD to figure the
3141 last line of the method the error occurs in. */
3142
3143 static void
3144 missing_return_error (method)
3145 tree method;
3146 {
3147 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3148 parse_error_context (wfl_operator, "Missing return statement");
3149 }
3150
3151 /* Issue an unreachable statement error. From NODE, find the next
3152 statement to report appropriately. */
3153 static void
3154 unreachable_stmt_error (node)
3155 tree node;
3156 {
3157 /* Browse node to find the next expression node that has a WFL. Use
3158 the location to report the error */
3159 if (TREE_CODE (node) == COMPOUND_EXPR)
3160 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3161 else
3162 node = find_expr_with_wfl (node);
3163
3164 if (node)
3165 {
3166 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3167 parse_error_context (wfl_operator, "Unreachable statement");
3168 }
3169 else
3170 abort ();
3171 }
3172
3173 int
3174 java_report_errors ()
3175 {
3176 if (java_error_count)
3177 fprintf (stderr, "%d error%s",
3178 java_error_count, (java_error_count == 1 ? "" : "s"));
3179 if (java_warning_count)
3180 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3181 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3182 if (java_error_count || java_warning_count)
3183 putc ('\n', stderr);
3184 return java_error_count;
3185 }
3186
3187 static char *
3188 java_accstring_lookup (flags)
3189 int flags;
3190 {
3191 static char buffer [80];
3192 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3193
3194 /* Access modifier looked-up first for easier report on forbidden
3195 access. */
3196 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3197 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3198 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3199 if (flags & ACC_STATIC) COPY_RETURN ("static");
3200 if (flags & ACC_FINAL) COPY_RETURN ("final");
3201 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3202 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3203 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3204 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3205 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3206 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3207
3208 buffer [0] = '\0';
3209 return buffer;
3210 #undef COPY_RETURN
3211 }
3212
3213 /* Issuing error messages upon redefinition of classes, interfaces or
3214 variables. */
3215
3216 static void
3217 classitf_redefinition_error (context, id, decl, cl)
3218 const char *context;
3219 tree id, decl, cl;
3220 {
3221 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3222 context, IDENTIFIER_POINTER (id),
3223 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3224 /* Here we should point out where its redefined. It's a unicode. FIXME */
3225 }
3226
3227 static void
3228 variable_redefinition_error (context, name, type, line)
3229 tree context, name, type;
3230 int line;
3231 {
3232 const char *type_name;
3233
3234 /* Figure a proper name for type. We might haven't resolved it */
3235 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3236 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3237 else
3238 type_name = lang_printable_name (type, 0);
3239
3240 parse_error_context (context,
3241 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3242 IDENTIFIER_POINTER (name),
3243 type_name, IDENTIFIER_POINTER (name), line);
3244 }
3245
3246 static tree
3247 build_array_from_name (type, type_wfl, name, ret_name)
3248 tree type, type_wfl, name, *ret_name;
3249 {
3250 int more_dims = 0;
3251 const char *string;
3252
3253 /* Eventually get more dims */
3254 string = IDENTIFIER_POINTER (name);
3255 while (string [more_dims] == '[')
3256 more_dims++;
3257
3258 /* If we have, then craft a new type for this variable */
3259 if (more_dims)
3260 {
3261 name = get_identifier (&string [more_dims]);
3262
3263 /* If we have a pointer, use its type */
3264 if (TREE_CODE (type) == POINTER_TYPE)
3265 type = TREE_TYPE (type);
3266
3267 /* Building the first dimension of a primitive type uses this
3268 function */
3269 if (JPRIMITIVE_TYPE_P (type))
3270 {
3271 type = build_java_array_type (type, -1);
3272 CLASS_LOADED_P (type) = 1;
3273 more_dims--;
3274 }
3275 /* Otherwise, if we have a WFL for this type, use it (the type
3276 is already an array on an unresolved type, and we just keep
3277 on adding dimensions) */
3278 else if (type_wfl)
3279 type = type_wfl;
3280
3281 /* Add all the dimensions */
3282 while (more_dims--)
3283 type = build_unresolved_array_type (type);
3284
3285 /* The type may have been incomplete in the first place */
3286 if (type_wfl)
3287 type = obtain_incomplete_type (type);
3288 }
3289
3290 if (ret_name)
3291 *ret_name = name;
3292 return type;
3293 }
3294
3295 /* Build something that the type identifier resolver will identify as
3296 being an array to an unresolved type. TYPE_WFL is a WFL on a
3297 identifier. */
3298
3299 static tree
3300 build_unresolved_array_type (type_or_wfl)
3301 tree type_or_wfl;
3302 {
3303 const char *ptr;
3304
3305 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3306 just create a array type */
3307 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3308 {
3309 tree type = build_java_array_type (type_or_wfl, -1);
3310 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3311 return type;
3312 }
3313
3314 obstack_1grow (&temporary_obstack, '[');
3315 obstack_grow0 (&temporary_obstack,
3316 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3317 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3318 ptr = obstack_finish (&temporary_obstack);
3319 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3320 return type_or_wfl;
3321 }
3322
3323 static void
3324 parser_add_interface (class_decl, interface_decl, wfl)
3325 tree class_decl, interface_decl, wfl;
3326 {
3327 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3328 parse_error_context (wfl, "Interface `%s' repeated",
3329 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3330 }
3331
3332 /* Bulk of common class/interface checks. Return 1 if an error was
3333 encountered. TAG is 0 for a class, 1 for an interface. */
3334
3335 static int
3336 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3337 int is_interface, flags;
3338 tree raw_name, qualified_name, decl, cl;
3339 {
3340 tree node;
3341 int sca = 0; /* Static class allowed */
3342 int icaf = 0; /* Inner class allowed flags */
3343 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3344
3345 if (!quiet_flag)
3346 fprintf (stderr, " %s%s %s",
3347 (CPC_INNER_P () ? "inner" : ""),
3348 (is_interface ? "interface" : "class"),
3349 IDENTIFIER_POINTER (qualified_name));
3350
3351 /* Scope of an interface/class type name:
3352 - Can't be imported by a single type import
3353 - Can't already exists in the package */
3354 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3355 && (node = find_name_in_single_imports (raw_name))
3356 && !CPC_INNER_P ())
3357 {
3358 parse_error_context
3359 (cl, "%s name `%s' clashes with imported type `%s'",
3360 (is_interface ? "Interface" : "Class"),
3361 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3362 return 1;
3363 }
3364 if (decl && CLASS_COMPLETE_P (decl))
3365 {
3366 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3367 qualified_name, decl, cl);
3368 return 1;
3369 }
3370
3371 if (check_inner_class_redefinition (raw_name, cl))
3372 return 1;
3373
3374 /* If public, file name should match class/interface name, except
3375 when dealing with an inner class */
3376 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3377 {
3378 const char *f;
3379
3380 /* Contains OS dependent assumption on path separator. FIXME */
3381 for (f = &input_filename [strlen (input_filename)];
3382 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3383 f--)
3384 ;
3385 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3386 f++;
3387 if (strncmp (IDENTIFIER_POINTER (raw_name),
3388 f , IDENTIFIER_LENGTH (raw_name)) ||
3389 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3390 parse_error_context
3391 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3392 (is_interface ? "interface" : "class"),
3393 IDENTIFIER_POINTER (qualified_name),
3394 IDENTIFIER_POINTER (raw_name));
3395 }
3396
3397 /* Static classes can be declared only in top level classes. Note:
3398 once static, a inner class is a top level class. */
3399 if (flags & ACC_STATIC)
3400 {
3401 /* Catch the specific error of declaring an class inner class
3402 with no toplevel enclosing class. Prevent check_modifiers from
3403 complaining a second time */
3404 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3405 {
3406 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3407 IDENTIFIER_POINTER (qualified_name));
3408 sca = ACC_STATIC;
3409 }
3410 /* Else, in the context of a top-level class declaration, let
3411 `check_modifiers' do its job, otherwise, give it a go */
3412 else
3413 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3414 }
3415
3416 /* Inner classes can be declared private or protected
3417 within their enclosing classes. */
3418 if (CPC_INNER_P ())
3419 {
3420 /* A class which is local to a block can't be public, private,
3421 protected or static. But it is created final, so allow this
3422 one. */
3423 if (current_function_decl)
3424 icaf = sca = uaaf = ACC_FINAL;
3425 else
3426 {
3427 check_modifiers_consistency (flags);
3428 icaf = ACC_PRIVATE|ACC_PROTECTED;
3429 }
3430 }
3431
3432 if (is_interface)
3433 {
3434 if (CPC_INNER_P ())
3435 uaaf = INTERFACE_INNER_MODIFIERS;
3436 else
3437 uaaf = INTERFACE_MODIFIERS;
3438
3439 check_modifiers ("Illegal modifier `%s' for interface declaration",
3440 flags, uaaf);
3441 }
3442 else
3443 check_modifiers ((current_function_decl ?
3444 "Illegal modifier `%s' for local class declaration" :
3445 "Illegal modifier `%s' for class declaration"),
3446 flags, uaaf|sca|icaf);
3447 return 0;
3448 }
3449
3450 static void
3451 make_nested_class_name (cpc_list)
3452 tree cpc_list;
3453 {
3454 tree name;
3455
3456 if (!cpc_list)
3457 return;
3458 else
3459 make_nested_class_name (TREE_CHAIN (cpc_list));
3460
3461 /* Pick the qualified name when dealing with the first upmost
3462 enclosing class */
3463 name = (TREE_CHAIN (cpc_list) ?
3464 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3465 obstack_grow (&temporary_obstack,
3466 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3467 /* Why is NO_DOLLAR_IN_LABEL defined? */
3468 #if 0
3469 #ifdef NO_DOLLAR_IN_LABEL
3470 internal_error ("Can't use '$' as a separator for inner classes");
3471 #endif
3472 #endif
3473 obstack_1grow (&temporary_obstack, '$');
3474 }
3475
3476 /* Can't redefine a class already defined in an earlier scope. */
3477
3478 static int
3479 check_inner_class_redefinition (raw_name, cl)
3480 tree raw_name, cl;
3481 {
3482 tree scope_list;
3483
3484 for (scope_list = GET_CPC_LIST (); scope_list;
3485 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3486 if (raw_name == GET_CPC_UN_NODE (scope_list))
3487 {
3488 parse_error_context
3489 (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",
3490 IDENTIFIER_POINTER (raw_name));
3491 return 1;
3492 }
3493 return 0;
3494 }
3495
3496 static tree
3497 find_as_inner_class (enclosing, name, cl)
3498 tree enclosing, name, cl;
3499 {
3500 tree qual, to_return;
3501 if (!enclosing)
3502 return NULL_TREE;
3503
3504 name = TYPE_NAME (name);
3505
3506 /* First search: within the scope of `enclosing', search for name */
3507 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3508 qual = EXPR_WFL_QUALIFICATION (cl);
3509 else if (cl)
3510 qual = build_tree_list (cl, NULL_TREE);
3511 else
3512 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3513
3514 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3515 return to_return;
3516
3517 /* We're dealing with a qualified name. Try to resolve thing until
3518 we get something that is an enclosing class. */
3519 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3520 {
3521 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3522
3523 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3524 qual = TREE_CHAIN (qual))
3525 {
3526 acc = merge_qualified_name (acc,
3527 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3528 BUILD_PTR_FROM_NAME (ptr, acc);
3529 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3530 }
3531
3532 /* A NULL qual and a decl means that the search ended
3533 successfully?!? We have to do something then. FIXME */
3534
3535 if (decl)
3536 enclosing = decl;
3537 else
3538 qual = EXPR_WFL_QUALIFICATION (cl);
3539 }
3540 /* Otherwise, create a qual for the other part of the resolution. */
3541 else
3542 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3543
3544 return find_as_inner_class_do (qual, enclosing);
3545 }
3546
3547 /* We go inside the list of sub classes and try to find a way
3548 through. */
3549
3550 static tree
3551 find_as_inner_class_do (qual, enclosing)
3552 tree qual, enclosing;
3553 {
3554 if (!qual)
3555 return NULL_TREE;
3556
3557 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3558 {
3559 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3560 tree next_enclosing = NULL_TREE;
3561 tree inner_list;
3562
3563 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3564 inner_list; inner_list = TREE_CHAIN (inner_list))
3565 {
3566 if (TREE_VALUE (inner_list) == name_to_match)
3567 {
3568 next_enclosing = TREE_PURPOSE (inner_list);
3569 break;
3570 }
3571 }
3572 enclosing = next_enclosing;
3573 }
3574
3575 return (!qual && enclosing ? enclosing : NULL_TREE);
3576 }
3577
3578 /* Reach all inner classes and tie their unqualified name to a
3579 DECL. */
3580
3581 static void
3582 set_nested_class_simple_name_value (outer, set)
3583 tree outer;
3584 int set;
3585 {
3586 tree l;
3587
3588 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3589 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3590 TREE_PURPOSE (l) : NULL_TREE);
3591 }
3592
3593 static void
3594 link_nested_class_to_enclosing ()
3595 {
3596 if (GET_ENCLOSING_CPC ())
3597 {
3598 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3599 DECL_INNER_CLASS_LIST (enclosing) =
3600 tree_cons (GET_CPC (), GET_CPC_UN (),
3601 DECL_INNER_CLASS_LIST (enclosing));
3602 enclosing = enclosing;
3603 }
3604 }
3605
3606 static tree
3607 maybe_make_nested_class_name (name)
3608 tree name;
3609 {
3610 tree id = NULL_TREE;
3611
3612 if (CPC_INNER_P ())
3613 {
3614 make_nested_class_name (GET_CPC_LIST ());
3615 obstack_grow0 (&temporary_obstack,
3616 IDENTIFIER_POINTER (name),
3617 IDENTIFIER_LENGTH (name));
3618 id = get_identifier (obstack_finish (&temporary_obstack));
3619 if (ctxp->package)
3620 QUALIFIED_P (id) = 1;
3621 }
3622 return id;
3623 }
3624
3625 /* If DECL is NULL, create and push a new DECL, record the current
3626 line CL and do other maintenance things. */
3627
3628 static tree
3629 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3630 tree decl, raw_name, qualified_name, cl;
3631 {
3632 if (!decl)
3633 decl = push_class (make_class (), qualified_name);
3634
3635 /* Take care of the file and line business */
3636 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3637 /* If we're emiting xrefs, store the line/col number information */
3638 if (flag_emit_xref)
3639 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3640 else
3641 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3642 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3643 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3644 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3645
3646 PUSH_CPC (decl, raw_name);
3647 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3648
3649 /* Link the declaration to the already seen ones */
3650 TREE_CHAIN (decl) = ctxp->class_list;
3651 ctxp->class_list = decl;
3652
3653 /* Create a new nodes in the global lists */
3654 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
3655 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3656
3657 /* Install a new dependency list element */
3658 create_jdep_list (ctxp);
3659
3660 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3661 IDENTIFIER_POINTER (qualified_name)));
3662 return decl;
3663 }
3664
3665 static void
3666 add_superinterfaces (decl, interface_list)
3667 tree decl, interface_list;
3668 {
3669 tree node;
3670 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3671 takes care of ensuring that:
3672 - This is an accessible interface type,
3673 - Circularity detection.
3674 parser_add_interface is then called. If present but not defined,
3675 the check operation is delayed until the super interface gets
3676 defined. */
3677 for (node = interface_list; node; node = TREE_CHAIN (node))
3678 {
3679 tree current = TREE_PURPOSE (node);
3680 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3681 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3682 {
3683 if (!parser_check_super_interface (idecl, decl, current))
3684 parser_add_interface (decl, idecl, current);
3685 }
3686 else
3687 register_incomplete_type (JDEP_INTERFACE,
3688 current, decl, NULL_TREE);
3689 }
3690 }
3691
3692 /* Create an interface in pass1 and return its decl. Return the
3693 interface's decl in pass 2. */
3694
3695 static tree
3696 create_interface (flags, id, super)
3697 int flags;
3698 tree id, super;
3699 {
3700 tree raw_name = EXPR_WFL_NODE (id);
3701 tree q_name = parser_qualified_classname (raw_name);
3702 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3703
3704 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3705
3706 /* Basic checks: scope, redefinition, modifiers */
3707 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3708 {
3709 PUSH_ERROR ();
3710 return NULL_TREE;
3711 }
3712
3713 /* Suspend the current parsing context if we're parsing an inner
3714 interface */
3715 if (CPC_INNER_P ())
3716 java_parser_context_suspend ();
3717
3718 /* Push a new context for (static) initialized upon declaration fields */
3719 java_parser_context_push_initialized_field ();
3720
3721 /* Interface modifiers check
3722 - public/abstract allowed (already done at that point)
3723 - abstract is obsolete (comes first, it's a warning, or should be)
3724 - Can't use twice the same (checked in the modifier rule) */
3725 if ((flags & ACC_ABSTRACT) && flag_redundant)
3726 parse_warning_context
3727 (MODIFIER_WFL (ABSTRACT_TK),
3728 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3729
3730 /* Create a new decl if DECL is NULL, otherwise fix it */
3731 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3732
3733 /* Set super info and mark the class a complete */
3734 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3735 object_type_node, ctxp->interface_number);
3736 ctxp->interface_number = 0;
3737 CLASS_COMPLETE_P (decl) = 1;
3738 add_superinterfaces (decl, super);
3739
3740 return decl;
3741 }
3742
3743 /* Anonymous class counter. Will be reset to 1 every time a non
3744 anonymous class gets created. */
3745 static int anonymous_class_counter = 1;
3746
3747 /* Patch anonymous class CLASS, by either extending or implementing
3748 DEP. */
3749
3750 static void
3751 patch_anonymous_class (type_decl, class_decl, wfl)
3752 tree type_decl, class_decl, wfl;
3753 {
3754 tree class = TREE_TYPE (class_decl);
3755 tree type = TREE_TYPE (type_decl);
3756 tree binfo = TYPE_BINFO (class);
3757
3758 /* If it's an interface, implement it */
3759 if (CLASS_INTERFACE (type_decl))
3760 {
3761 tree s_binfo;
3762 int length;
3763
3764 if (parser_check_super_interface (type_decl, class_decl, wfl))
3765 return;
3766
3767 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3768 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3769 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3770 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3771 /* And add the interface */
3772 parser_add_interface (class_decl, type_decl, wfl);
3773 }
3774 /* Otherwise, it's a type we want to extend */
3775 else
3776 {
3777 if (parser_check_super (type_decl, class_decl, wfl))
3778 return;
3779 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3780 }
3781 }
3782
3783 static tree
3784 create_anonymous_class (location, type_name)
3785 int location;
3786 tree type_name;
3787 {
3788 char buffer [80];
3789 tree super = NULL_TREE, itf = NULL_TREE;
3790 tree id, type_decl, class;
3791
3792 /* The unqualified name of the anonymous class. It's just a number. */
3793 sprintf (buffer, "%d", anonymous_class_counter++);
3794 id = build_wfl_node (get_identifier (buffer));
3795 EXPR_WFL_LINECOL (id) = location;
3796
3797 /* We know about the type to extend/implement. We go ahead */
3798 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3799 {
3800 /* Create a class which either implements on extends the designated
3801 class. The class bears an innacessible name. */
3802 if (CLASS_INTERFACE (type_decl))
3803 {
3804 /* It's OK to modify it here. It's been already used and
3805 shouldn't be reused */
3806 ctxp->interface_number = 1;
3807 /* Interfaces should presented as a list of WFLs */
3808 itf = build_tree_list (type_name, NULL_TREE);
3809 }
3810 else
3811 super = type_name;
3812 }
3813
3814 class = create_class (ACC_FINAL, id, super, itf);
3815
3816 /* We didn't know anything about the stuff. We register a dependence. */
3817 if (!type_decl)
3818 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3819
3820 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3821 return class;
3822 }
3823
3824 /* Create a class in pass1 and return its decl. Return class
3825 interface's decl in pass 2. */
3826
3827 static tree
3828 create_class (flags, id, super, interfaces)
3829 int flags;
3830 tree id, super, interfaces;
3831 {
3832 tree raw_name = EXPR_WFL_NODE (id);
3833 tree class_id, decl;
3834 tree super_decl_type;
3835
3836 class_id = parser_qualified_classname (raw_name);
3837 decl = IDENTIFIER_CLASS_VALUE (class_id);
3838 EXPR_WFL_NODE (id) = class_id;
3839
3840 /* Basic check: scope, redefinition, modifiers */
3841 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3842 {
3843 PUSH_ERROR ();
3844 return NULL_TREE;
3845 }
3846
3847 /* Suspend the current parsing context if we're parsing an inner
3848 class or an anonymous class. */
3849 if (CPC_INNER_P ())
3850 java_parser_context_suspend ();
3851 /* Push a new context for (static) initialized upon declaration fields */
3852 java_parser_context_push_initialized_field ();
3853
3854 /* Class modifier check:
3855 - Allowed modifier (already done at that point)
3856 - abstract AND final forbidden
3857 - Public classes defined in the correct file */
3858 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3859 parse_error_context
3860 (id, "Class `%s' can't be declared both abstract and final",
3861 IDENTIFIER_POINTER (raw_name));
3862
3863 /* Create a new decl if DECL is NULL, otherwise fix it */
3864 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3865
3866 /* If SUPER exists, use it, otherwise use Object */
3867 if (super)
3868 {
3869 /* Can't extend java.lang.Object */
3870 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3871 {
3872 parse_error_context (id, "Can't extend `java.lang.Object'");
3873 return NULL_TREE;
3874 }
3875
3876 super_decl_type =
3877 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3878 }
3879 else if (TREE_TYPE (decl) != object_type_node)
3880 super_decl_type = object_type_node;
3881 /* We're defining java.lang.Object */
3882 else
3883 super_decl_type = NULL_TREE;
3884
3885 /* A class nested in an interface is implicitly static. */
3886 if (INNER_CLASS_DECL_P (decl)
3887 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3888 {
3889 flags |= ACC_STATIC;
3890 }
3891
3892 /* Set super info and mark the class as complete. */
3893 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3894 ctxp->interface_number);
3895 ctxp->interface_number = 0;
3896 CLASS_COMPLETE_P (decl) = 1;
3897 add_superinterfaces (decl, interfaces);
3898
3899 /* Add the private this$<n> field, Replicate final locals still in
3900 scope as private final fields mangled like val$<local_name>.
3901 This doesn't not occur for top level (static) inner classes. */
3902 if (PURE_INNER_CLASS_DECL_P (decl))
3903 add_inner_class_fields (decl, current_function_decl);
3904
3905 /* If doing xref, store the location at which the inherited class
3906 (if any) was seen. */
3907 if (flag_emit_xref && super)
3908 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3909
3910 /* Eventually sets the @deprecated tag flag */
3911 CHECK_DEPRECATED (decl);
3912
3913 /* Reset the anonymous class counter when declaring non inner classes */
3914 if (!INNER_CLASS_DECL_P (decl))
3915 anonymous_class_counter = 1;
3916
3917 return decl;
3918 }
3919
3920 /* End a class declaration: register the statements used to create
3921 finit$ and <clinit>, pop the current class and resume the prior
3922 parser context if necessary. */
3923
3924 static void
3925 end_class_declaration (resume)
3926 int resume;
3927 {
3928 /* If an error occured, context weren't pushed and won't need to be
3929 popped by a resume. */
3930 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3931
3932 java_parser_context_pop_initialized_field ();
3933 POP_CPC ();
3934 if (resume && no_error_occured)
3935 java_parser_context_resume ();
3936
3937 /* We're ending a class declaration, this is a good time to reset
3938 the interface cout. Note that might have been already done in
3939 create_interface, but if at that time an inner class was being
3940 dealt with, the interface count was reset in a context created
3941 for the sake of handling inner classes declaration. */
3942 ctxp->interface_number = 0;
3943 }
3944
3945 static void
3946 add_inner_class_fields (class_decl, fct_decl)
3947 tree class_decl;
3948 tree fct_decl;
3949 {
3950 tree block, marker, f;
3951
3952 f = add_field (TREE_TYPE (class_decl),
3953 build_current_thisn (TREE_TYPE (class_decl)),
3954 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3955 ACC_PRIVATE);
3956 FIELD_THISN (f) = 1;
3957
3958 if (!fct_decl)
3959 return;
3960
3961 for (block = GET_CURRENT_BLOCK (fct_decl);
3962 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3963 {
3964 tree decl;
3965 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3966 {
3967 tree name, pname;
3968 tree wfl, init, list;
3969
3970 /* Avoid non final arguments. */
3971 if (!LOCAL_FINAL_P (decl))
3972 continue;
3973
3974 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3975 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3976 wfl = build_wfl_node (name);
3977 init = build_wfl_node (pname);
3978 /* Build an initialization for the field: it will be
3979 initialized by a parameter added to finit$, bearing a
3980 mangled name of the field itself (param$<n>.) The
3981 parameter is provided to finit$ by the constructor
3982 invoking it (hence the constructor will also feature a
3983 hidden parameter, set to the value of the outer context
3984 local at the time the inner class is created.)
3985
3986 Note: we take into account all possible locals that can
3987 be accessed by the inner class. It's actually not trivial
3988 to minimize these aliases down to the ones really
3989 used. One way to do that would be to expand all regular
3990 methods first, then finit$ to get a picture of what's
3991 used. It works with the exception that we would have to
3992 go back on all constructor invoked in regular methods to
3993 have their invokation reworked (to include the right amount
3994 of alias initializer parameters.)
3995
3996 The only real way around, I think, is a first pass to
3997 identify locals really used in the inner class. We leave
3998 the flag FIELD_LOCAL_ALIAS_USED around for that future
3999 use.
4000
4001 On the other hand, it only affect local inner classes,
4002 whose constructors (and finit$ call) will be featuring
4003 unecessary arguments. It's easy for a developper to keep
4004 this number of parameter down by using the `final'
4005 keyword only when necessary. For the time being, we can
4006 issue a warning on unecessary finals. FIXME */
4007 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4008 wfl, init);
4009
4010 /* Register the field. The TREE_LIST holding the part
4011 initialized/initializer will be marked ARG_FINAL_P so
4012 that the created field can be marked
4013 FIELD_LOCAL_ALIAS. */
4014 list = build_tree_list (wfl, init);
4015 ARG_FINAL_P (list) = 1;
4016 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4017 }
4018 }
4019
4020 if (!CPC_INITIALIZER_STMT (ctxp))
4021 return;
4022
4023 /* If we ever registered an alias field, insert and marker to
4024 remeber where the list ends. The second part of the list (the one
4025 featuring initialized fields) so it can be later reversed to
4026 enforce 8.5. The marker will be removed during that operation. */
4027 marker = build_tree_list (NULL_TREE, NULL_TREE);
4028 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4029 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4030 }
4031
4032 /* Can't use lookup_field () since we don't want to load the class and
4033 can't set the CLASS_LOADED_P flag */
4034
4035 static tree
4036 find_field (class, name)
4037 tree class;
4038 tree name;
4039 {
4040 tree decl;
4041 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4042 {
4043 if (DECL_NAME (decl) == name)
4044 return decl;
4045 }
4046 return NULL_TREE;
4047 }
4048
4049 /* Wrap around lookup_field that doesn't potentially upset the value
4050 of CLASS */
4051
4052 static tree
4053 lookup_field_wrapper (class, name)
4054 tree class, name;
4055 {
4056 tree type = class;
4057 tree decl = NULL_TREE;
4058 java_parser_context_save_global ();
4059
4060 /* Last chance: if we're within the context of an inner class, we
4061 might be trying to access a local variable defined in an outer
4062 context. We try to look for it now. */
4063 if (INNER_CLASS_TYPE_P (class))
4064 {
4065 tree new_name;
4066 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4067 decl = lookup_field (&type, new_name);
4068 if (decl && decl != error_mark_node)
4069 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4070 }
4071 if (!decl || decl == error_mark_node)
4072 {
4073 type = class;
4074 decl = lookup_field (&type, name);
4075 }
4076
4077 java_parser_context_restore_global ();
4078 return decl == error_mark_node ? NULL : decl;
4079 }
4080
4081 /* Find duplicate field within the same class declarations and report
4082 the error. Returns 1 if a duplicated field was found, 0
4083 otherwise. */
4084
4085 static int
4086 duplicate_declaration_error_p (new_field_name, new_type, cl)
4087 tree new_field_name, new_type, cl;
4088 {
4089 /* This might be modified to work with method decl as well */
4090 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4091 if (decl)
4092 {
4093 char *t1 = xstrdup (purify_type_name
4094 ((TREE_CODE (new_type) == POINTER_TYPE
4095 && TREE_TYPE (new_type) == NULL_TREE) ?
4096 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4097 lang_printable_name (new_type, 1)));
4098 /* The type may not have been completed by the time we report
4099 the error */
4100 char *t2 = xstrdup (purify_type_name
4101 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4102 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4103 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4104 lang_printable_name (TREE_TYPE (decl), 1)));
4105 parse_error_context
4106 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4107 t1, IDENTIFIER_POINTER (new_field_name),
4108 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4109 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4110 free (t1);
4111 free (t2);
4112 return 1;
4113 }
4114 return 0;
4115 }
4116
4117 /* Field registration routine. If TYPE doesn't exist, field
4118 declarations are linked to the undefined TYPE dependency list, to
4119 be later resolved in java_complete_class () */
4120
4121 static void
4122 register_fields (flags, type, variable_list)
4123 int flags;
4124 tree type, variable_list;
4125 {
4126 tree current, saved_type;
4127 tree class_type = NULL_TREE;
4128 int saved_lineno = lineno;
4129 int must_chain = 0;
4130 tree wfl = NULL_TREE;
4131
4132 if (GET_CPC ())
4133 class_type = TREE_TYPE (GET_CPC ());
4134
4135 if (!class_type || class_type == error_mark_node)
4136 return;
4137
4138 /* If we're adding fields to interfaces, those fields are public,
4139 static, final */
4140 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4141 {
4142 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4143 flags, ACC_PUBLIC, "interface field(s)");
4144 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4145 flags, ACC_STATIC, "interface field(s)");
4146 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4147 flags, ACC_FINAL, "interface field(s)");
4148 check_modifiers ("Illegal interface member modifier `%s'", flags,
4149 INTERFACE_FIELD_MODIFIERS);
4150 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4151 }
4152
4153 /* Obtain a suitable type for resolution, if necessary */
4154 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4155
4156 /* If TYPE is fully resolved and we don't have a reference, make one */
4157 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4158
4159 for (current = variable_list, saved_type = type; current;
4160 current = TREE_CHAIN (current), type = saved_type)
4161 {
4162 tree real_type;
4163 tree field_decl;
4164 tree cl = TREE_PURPOSE (current);
4165 tree init = TREE_VALUE (current);
4166 tree current_name = EXPR_WFL_NODE (cl);
4167
4168 /* Can't declare non-final static fields in inner classes */
4169 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4170 && !(flags & ACC_FINAL))
4171 parse_error_context
4172 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4173 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4174 lang_printable_name (class_type, 0));
4175
4176 /* Process NAME, as it may specify extra dimension(s) for it */
4177 type = build_array_from_name (type, wfl, current_name, &current_name);
4178
4179 /* Type adjustment. We may have just readjusted TYPE because
4180 the variable specified more dimensions. Make sure we have
4181 a reference if we can and don't have one already. Also
4182 change the name if we have an init. */
4183 if (type != saved_type)
4184 {
4185 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4186 if (init)
4187 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4188 }
4189
4190 real_type = GET_REAL_TYPE (type);
4191 /* Check for redeclarations */
4192 if (duplicate_declaration_error_p (current_name, real_type, cl))
4193 continue;
4194
4195 /* Set lineno to the line the field was found and create a
4196 declaration for it. Eventually sets the @deprecated tag flag. */
4197 if (flag_emit_xref)
4198 lineno = EXPR_WFL_LINECOL (cl);
4199 else
4200 lineno = EXPR_WFL_LINENO (cl);
4201 field_decl = add_field (class_type, current_name, real_type, flags);
4202 CHECK_DEPRECATED (field_decl);
4203
4204 /* If the field denotes a final instance variable, then we
4205 allocate a LANG_DECL_SPECIFIC part to keep track of its
4206 initialization. We also mark whether the field was
4207 initialized upon it's declaration. We don't do that if the
4208 created field is an alias to a final local. */
4209 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4210 {
4211 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4212 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4213 if ((flags & ACC_STATIC) && init)
4214 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4215 }
4216
4217 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4218 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4219 hide parameters to this inner class finit$ and
4220 constructors. It also means that the field isn't final per
4221 say. */
4222 if (ARG_FINAL_P (current))
4223 {
4224 FIELD_LOCAL_ALIAS (field_decl) = 1;
4225 FIELD_FINAL (field_decl) = 0;
4226 }
4227
4228 /* Check if we must chain. */
4229 if (must_chain)
4230 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4231
4232 /* If we have an initialization value tied to the field */
4233 if (init)
4234 {
4235 /* The field is declared static */
4236 if (flags & ACC_STATIC)
4237 {
4238 /* We include the field and its initialization part into
4239 a list used to generate <clinit>. After <clinit> is
4240 walked, field initializations will be processed and
4241 fields initialized with known constants will be taken
4242 out of <clinit> and have their DECL_INITIAL set
4243 appropriately. */
4244 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4245 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4246 if (TREE_OPERAND (init, 1)
4247 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4248 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4249 }
4250 /* A non-static field declared with an immediate initialization is
4251 to be initialized in <init>, if any. This field is remembered
4252 to be processed at the time of the generation of <init>. */
4253 else
4254 {
4255 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4256 SET_CPC_INITIALIZER_STMT (ctxp, init);
4257 }
4258 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4259 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4260 }
4261 }
4262 lineno = saved_lineno;
4263 }
4264
4265 /* Generate finit$, using the list of initialized fields to populate
4266 its body. finit$'s parameter(s) list is adjusted to include the
4267 one(s) used to initialized the field(s) caching outer context
4268 local(s). */
4269
4270 static tree
4271 generate_finit (class_type)
4272 tree class_type;
4273 {
4274 int count = 0;
4275 tree list = TYPE_FINIT_STMT_LIST (class_type);
4276 tree mdecl, current, parms;
4277
4278 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4279 class_type, NULL_TREE,
4280 &count);
4281 CRAFTED_PARAM_LIST_FIXUP (parms);
4282 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4283 finit_identifier_node, parms);
4284 fix_method_argument_names (parms, mdecl);
4285 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4286 mdecl, NULL_TREE);
4287 DECL_FUNCTION_NAP (mdecl) = count;
4288 start_artificial_method_body (mdecl);
4289
4290 for (current = list; current; current = TREE_CHAIN (current))
4291 java_method_add_stmt (mdecl,
4292 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4293 current));
4294 end_artificial_method_body (mdecl);
4295 return mdecl;
4296 }
4297
4298 static void
4299 add_instance_initializer (mdecl)
4300 tree mdecl;
4301 {
4302 tree current;
4303 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4304 tree compound = NULL_TREE;
4305
4306 if (stmt_list)
4307 {
4308 for (current = stmt_list; current; current = TREE_CHAIN (current))
4309 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4310
4311 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4312 NULL_TREE, compound));
4313 }
4314 }
4315
4316 /* Shared accros method_declarator and method_header to remember the
4317 patch stage that was reached during the declaration of the method.
4318 A method DECL is built differently is there is no patch
4319 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4320 pending on the currently defined method. */
4321
4322 static int patch_stage;
4323
4324 /* Check the method declaration and add the method to its current
4325 class. If the argument list is known to contain incomplete types,
4326 the method is partially added and the registration will be resume
4327 once the method arguments resolved. If TYPE is NULL, we're dealing
4328 with a constructor. */
4329
4330 static tree
4331 method_header (flags, type, mdecl, throws)
4332 int flags;
4333 tree type, mdecl, throws;
4334 {
4335 tree type_wfl = NULL_TREE;
4336 tree meth_name = NULL_TREE;
4337 tree current, orig_arg, this_class = NULL;
4338 tree id, meth;
4339 int saved_lineno;
4340 int constructor_ok = 0, must_chain;
4341 int count;
4342
4343 if (mdecl == error_mark_node)
4344 return error_mark_node;
4345 meth = TREE_VALUE (mdecl);
4346 id = TREE_PURPOSE (mdecl);
4347
4348 check_modifiers_consistency (flags);
4349
4350 if (GET_CPC ())
4351 this_class = TREE_TYPE (GET_CPC ());
4352
4353 if (!this_class || this_class == error_mark_node)
4354 return NULL_TREE;
4355
4356 /* There are some forbidden modifiers for an abstract method and its
4357 class must be abstract as well. */
4358 if (type && (flags & ACC_ABSTRACT))
4359 {
4360 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4361 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4362 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4363 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4364 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4365 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4366 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4367 parse_error_context
4368 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4369 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4370 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4371 }
4372
4373 /* Things to be checked when declaring a constructor */
4374 if (!type)
4375 {
4376 int ec = java_error_count;
4377 /* 8.6: Constructor declarations: we might be trying to define a
4378 method without specifying a return type. */
4379 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4380 parse_error_context
4381 (id, "Invalid method declaration, return type required");
4382 /* 8.6.3: Constructor modifiers */
4383 else
4384 {
4385 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4386 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4387 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4388 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4389 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4390 }
4391 /* If we found error here, we don't consider it's OK to tread
4392 the method definition as a constructor, for the rest of this
4393 function */
4394 if (ec == java_error_count)
4395 constructor_ok = 1;
4396 }
4397
4398 /* Method declared within the scope of an interface are implicitly
4399 abstract and public. Conflicts with other erroneously provided
4400 modifiers are checked right after. */
4401
4402 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4403 {
4404 /* If FLAGS isn't set because of a modifier, turn the
4405 corresponding modifier WFL to NULL so we issue a warning on
4406 the obsolete use of the modifier */
4407 if (!(flags & ACC_PUBLIC))
4408 MODIFIER_WFL (PUBLIC_TK) = NULL;
4409 if (!(flags & ACC_ABSTRACT))
4410 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4411 flags |= ACC_PUBLIC;
4412 flags |= ACC_ABSTRACT;
4413 }
4414
4415 /* Inner class can't declare static methods */
4416 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4417 {
4418 parse_error_context
4419 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4420 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4421 lang_printable_name (this_class, 0));
4422 }
4423
4424 /* Modifiers context reset moved up, so abstract method declaration
4425 modifiers can be later checked. */
4426
4427 /* Set constructor returned type to void and method name to <init>,
4428 unless we found an error identifier the constructor (in which
4429 case we retain the original name) */
4430 if (!type)
4431 {
4432 type = void_type_node;
4433 if (constructor_ok)
4434 meth_name = init_identifier_node;
4435 }
4436 else
4437 meth_name = EXPR_WFL_NODE (id);
4438
4439 /* Do the returned type resolution and registration if necessary */
4440 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4441
4442 if (meth_name)
4443 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4444 EXPR_WFL_NODE (id) = meth_name;
4445 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4446
4447 if (must_chain)
4448 {
4449 patch_stage = JDEP_METHOD_RETURN;
4450 register_incomplete_type (patch_stage, type_wfl, id, type);
4451 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4452 }
4453 else
4454 TREE_TYPE (meth) = type;
4455
4456 saved_lineno = lineno;
4457 /* When defining an abstract or interface method, the curly
4458 bracket at level 1 doesn't exist because there is no function
4459 body */
4460 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4461 EXPR_WFL_LINENO (id));
4462
4463 /* Remember the original argument list */
4464 orig_arg = TYPE_ARG_TYPES (meth);
4465
4466 if (patch_stage) /* includes ret type and/or all args */
4467 {
4468 jdep *jdep;
4469 meth = add_method_1 (this_class, flags, meth_name, meth);
4470 /* Patch for the return type */
4471 if (patch_stage == JDEP_METHOD_RETURN)
4472 {
4473 jdep = CLASSD_LAST (ctxp->classd_list);
4474 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4475 }
4476 /* This is the stop JDEP. METH allows the function's signature
4477 to be computed. */
4478 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4479 }
4480 else
4481 meth = add_method (this_class, flags, meth_name,
4482 build_java_signature (meth));
4483
4484 /* Remember final parameters */
4485 MARK_FINAL_PARMS (meth, orig_arg);
4486
4487 /* Fix the method argument list so we have the argument name
4488 information */
4489 fix_method_argument_names (orig_arg, meth);
4490
4491 /* Register the parameter number and re-install the current line
4492 number */
4493 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4494 lineno = saved_lineno;
4495
4496 /* Register exception specified by the `throws' keyword for
4497 resolution and set the method decl appropriate field to the list.
4498 Note: the grammar ensures that what we get here are class
4499 types. */
4500 if (throws)
4501 {
4502 throws = nreverse (throws);
4503 for (current = throws; current; current = TREE_CHAIN (current))
4504 {
4505 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4506 NULL_TREE, NULL_TREE);
4507 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4508 &TREE_VALUE (current);
4509 }
4510 DECL_FUNCTION_THROWS (meth) = throws;
4511 }
4512
4513 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4514 DECL_FUNCTION_WFL (meth) = id;
4515
4516 /* Set the flag if we correctly processed a constructor */
4517 if (constructor_ok)
4518 {
4519 DECL_CONSTRUCTOR_P (meth) = 1;
4520 /* Compute and store the number of artificial parameters declared
4521 for this constructor */
4522 for (count = 0, current = TYPE_FIELDS (this_class); current;
4523 current = TREE_CHAIN (current))
4524 if (FIELD_LOCAL_ALIAS (current))
4525 count++;
4526 DECL_FUNCTION_NAP (meth) = count;
4527 }
4528
4529 /* Eventually set the @deprecated tag flag */
4530 CHECK_DEPRECATED (meth);
4531
4532 /* If doing xref, store column and line number information instead
4533 of the line number only. */
4534 if (flag_emit_xref)
4535 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4536
4537 return meth;
4538 }
4539
4540 static void
4541 fix_method_argument_names (orig_arg, meth)
4542 tree orig_arg, meth;
4543 {
4544 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4545 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4546 {
4547 TREE_PURPOSE (arg) = this_identifier_node;
4548 arg = TREE_CHAIN (arg);
4549 }
4550 while (orig_arg != end_params_node)
4551 {
4552 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4553 orig_arg = TREE_CHAIN (orig_arg);
4554 arg = TREE_CHAIN (arg);
4555 }
4556 }
4557
4558 /* Complete the method declaration with METHOD_BODY. */
4559
4560 static void
4561 finish_method_declaration (method_body)
4562 tree method_body;
4563 {
4564 int flags;
4565
4566 if (!current_function_decl)
4567 return;
4568
4569 flags = get_access_flags_from_decl (current_function_decl);
4570
4571 /* 8.4.5 Method Body */
4572 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4573 {
4574 tree name = DECL_NAME (current_function_decl);
4575 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4576 "%s method `%s' can't have a body defined",
4577 (METHOD_NATIVE (current_function_decl) ?
4578 "Native" : "Abstract"),
4579 IDENTIFIER_POINTER (name));
4580 method_body = NULL_TREE;
4581 }
4582 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4583 {
4584 tree name = DECL_NAME (current_function_decl);
4585 parse_error_context
4586 (DECL_FUNCTION_WFL (current_function_decl),
4587 "Non native and non abstract method `%s' must have a body defined",
4588 IDENTIFIER_POINTER (name));
4589 method_body = NULL_TREE;
4590 }
4591
4592 if (flag_emit_class_files && method_body
4593 && TREE_CODE (method_body) == NOP_EXPR
4594 && TREE_TYPE (current_function_decl)
4595 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4596 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4597
4598 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4599 maybe_absorb_scoping_blocks ();
4600 /* Exit function's body */
4601 exit_block ();
4602 /* Merge last line of the function with first line, directly in the
4603 function decl. It will be used to emit correct debug info. */
4604 if (!flag_emit_xref)
4605 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4606
4607 /* Since function's argument's list are shared, reset the
4608 ARG_FINAL_P parameter that might have been set on some of this
4609 function parameters. */
4610 UNMARK_FINAL_PARMS (current_function_decl);
4611
4612 /* So we don't have an irrelevant function declaration context for
4613 the next static block we'll see. */
4614 current_function_decl = NULL_TREE;
4615 }
4616
4617 /* Build a an error message for constructor circularity errors. */
4618
4619 static char *
4620 constructor_circularity_msg (from, to)
4621 tree from, to;
4622 {
4623 static char string [4096];
4624 char *t = xstrdup (lang_printable_name (from, 0));
4625 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4626 free (t);
4627 return string;
4628 }
4629
4630 /* Verify a circular call to METH. Return 1 if an error is found, 0
4631 otherwise. */
4632
4633 static int
4634 verify_constructor_circularity (meth, current)
4635 tree meth, current;
4636 {
4637 static tree list = NULL_TREE;
4638 static int initialized_p;
4639 tree c;
4640
4641 /* If we haven't already registered LIST with the garbage collector,
4642 do so now. */
4643 if (!initialized_p)
4644 {
4645 ggc_add_tree_root (&list, 1);
4646 initialized_p = 1;
4647 }
4648
4649 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4650 {
4651 if (TREE_VALUE (c) == meth)
4652 {
4653 char *t;
4654 if (list)
4655 {
4656 tree liste;
4657 list = nreverse (list);
4658 for (liste = list; liste; liste = TREE_CHAIN (liste))
4659 {
4660 parse_error_context
4661 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4662 constructor_circularity_msg
4663 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4664 java_error_count--;
4665 }
4666 }
4667 t = xstrdup (lang_printable_name (meth, 0));
4668 parse_error_context (TREE_PURPOSE (c),
4669 "%s: recursive invocation of constructor `%s'",
4670 constructor_circularity_msg (current, meth), t);
4671 free (t);
4672 list = NULL_TREE;
4673 return 1;
4674 }
4675 }
4676 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4677 {
4678 list = tree_cons (c, current, list);
4679 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4680 return 1;
4681 list = TREE_CHAIN (list);
4682 }
4683 return 0;
4684 }
4685
4686 /* Check modifiers that can be declared but exclusively */
4687
4688 static void
4689 check_modifiers_consistency (flags)
4690 int flags;
4691 {
4692 int acc_count = 0;
4693 tree cl = NULL_TREE;
4694
4695 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4696 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4697 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4698 if (acc_count > 1)
4699 parse_error_context
4700 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4701
4702 acc_count = 0;
4703 cl = NULL_TREE;
4704 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4705 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4706 if (acc_count > 1)
4707 parse_error_context (cl,
4708 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4709 }
4710
4711 /* Check the methode header METH for abstract specifics features */
4712
4713 static void
4714 check_abstract_method_header (meth)
4715 tree meth;
4716 {
4717 int flags = get_access_flags_from_decl (meth);
4718
4719 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4720 ACC_ABSTRACT, "abstract method",
4721 IDENTIFIER_POINTER (DECL_NAME (meth)));
4722 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4723 ACC_PUBLIC, "abstract method",
4724 IDENTIFIER_POINTER (DECL_NAME (meth)));
4725
4726 check_modifiers ("Illegal modifier `%s' for interface method",
4727 flags, INTERFACE_METHOD_MODIFIERS);
4728 }
4729
4730 /* Create a FUNCTION_TYPE node and start augmenting it with the
4731 declared function arguments. Arguments type that can't be resolved
4732 are left as they are, but the returned node is marked as containing
4733 incomplete types. */
4734
4735 static tree
4736 method_declarator (id, list)
4737 tree id, list;
4738 {
4739 tree arg_types = NULL_TREE, current, node;
4740 tree meth = make_node (FUNCTION_TYPE);
4741 jdep *jdep;
4742
4743 patch_stage = JDEP_NO_PATCH;
4744
4745 if (GET_CPC () == error_mark_node)
4746 return error_mark_node;
4747
4748 /* If we're dealing with an inner class constructor, we hide the
4749 this$<n> decl in the name field of its parameter declaration. We
4750 also might have to hide the outer context local alias
4751 initializers. Not done when the class is a toplevel class. */
4752 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4753 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4754 {
4755 tree aliases_list, type, thisn;
4756 /* First the aliases, linked to the regular parameters */
4757 aliases_list =
4758 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4759 TREE_TYPE (GET_CPC ()),
4760 NULL_TREE, NULL);
4761 list = chainon (nreverse (aliases_list), list);
4762
4763 /* Then this$<n> */
4764 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4765 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4766 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4767 list);
4768 }
4769
4770 for (current = list; current; current = TREE_CHAIN (current))
4771 {
4772 int must_chain = 0;
4773 tree wfl_name = TREE_PURPOSE (current);
4774 tree type = TREE_VALUE (current);
4775 tree name = EXPR_WFL_NODE (wfl_name);
4776 tree already, arg_node;
4777 tree type_wfl = NULL_TREE;
4778 tree real_type;
4779
4780 /* Obtain a suitable type for resolution, if necessary */
4781 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4782
4783 /* Process NAME, as it may specify extra dimension(s) for it */
4784 type = build_array_from_name (type, type_wfl, name, &name);
4785 EXPR_WFL_NODE (wfl_name) = name;
4786
4787 real_type = GET_REAL_TYPE (type);
4788 if (TREE_CODE (real_type) == RECORD_TYPE)
4789 {
4790 real_type = promote_type (real_type);
4791 if (TREE_CODE (type) == TREE_LIST)
4792 TREE_PURPOSE (type) = real_type;
4793 }
4794
4795 /* Check redefinition */
4796 for (already = arg_types; already; already = TREE_CHAIN (already))
4797 if (TREE_PURPOSE (already) == name)
4798 {
4799 parse_error_context
4800 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4801 IDENTIFIER_POINTER (name),
4802 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4803 break;
4804 }
4805
4806 /* If we've an incomplete argument type, we know there is a location
4807 to patch when the type get resolved, later. */
4808 jdep = NULL;
4809 if (must_chain)
4810 {
4811 patch_stage = JDEP_METHOD;
4812 type = register_incomplete_type (patch_stage,
4813 type_wfl, wfl_name, type);
4814 jdep = CLASSD_LAST (ctxp->classd_list);
4815 JDEP_MISC (jdep) = id;
4816 }
4817
4818 /* The argument node: a name and a (possibly) incomplete type. */
4819 arg_node = build_tree_list (name, real_type);
4820 /* Remeber arguments declared final. */
4821 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4822
4823 if (jdep)
4824 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4825 TREE_CHAIN (arg_node) = arg_types;
4826 arg_types = arg_node;
4827 }
4828 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4829 node = build_tree_list (id, meth);
4830 return node;
4831 }
4832
4833 static int
4834 unresolved_type_p (wfl, returned)
4835 tree wfl;
4836 tree *returned;
4837
4838 {
4839 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4840 {
4841 if (returned)
4842 {
4843 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4844 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4845 *returned = TREE_TYPE (decl);
4846 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4847 *returned = TREE_TYPE (GET_CPC ());
4848 else
4849 *returned = NULL_TREE;
4850 }
4851 return 1;
4852 }
4853 if (returned)
4854 *returned = wfl;
4855 return 0;
4856 }
4857
4858 /* From NAME, build a qualified identifier node using the
4859 qualification from the current package definition. */
4860
4861 static tree
4862 parser_qualified_classname (name)
4863 tree name;
4864 {
4865 tree nested_class_name;
4866
4867 if ((nested_class_name = maybe_make_nested_class_name (name)))
4868 return nested_class_name;
4869
4870 if (ctxp->package)
4871 return merge_qualified_name (ctxp->package, name);
4872 else
4873 return name;
4874 }
4875
4876 /* Called once the type a interface extends is resolved. Returns 0 if
4877 everything is OK. */
4878
4879 static int
4880 parser_check_super_interface (super_decl, this_decl, this_wfl)
4881 tree super_decl, this_decl, this_wfl;
4882 {
4883 tree super_type = TREE_TYPE (super_decl);
4884
4885 /* Has to be an interface */
4886 if (!CLASS_INTERFACE (super_decl))
4887 {
4888 parse_error_context
4889 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4890 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4891 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4892 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4893 "interface" : "class"),
4894 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4895 return 1;
4896 }
4897
4898 /* Check scope: same package OK, other package: OK if public */
4899 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4900 return 1;
4901
4902 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4903 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4904 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4905 return 0;
4906 }
4907
4908 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4909 0 if everthing is OK. */
4910
4911 static int
4912 parser_check_super (super_decl, this_decl, wfl)
4913 tree super_decl, this_decl, wfl;
4914 {
4915 tree super_type = TREE_TYPE (super_decl);
4916
4917 /* SUPER should be a CLASS (neither an array nor an interface) */
4918 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4919 {
4920 parse_error_context
4921 (wfl, "Class `%s' can't subclass %s `%s'",
4922 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4923 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4924 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4925 return 1;
4926 }
4927
4928 if (CLASS_FINAL (TYPE_NAME (super_type)))
4929 {
4930 parse_error_context (wfl, "Can't subclass final classes: %s",
4931 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4932 return 1;
4933 }
4934
4935 /* Check scope: same package OK, other package: OK if public */
4936 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4937 return 1;
4938
4939 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4940 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4941 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4942 return 0;
4943 }
4944
4945 /* Create a new dependency list and link it (in a LIFO manner) to the
4946 CTXP list of type dependency list. */
4947
4948 static void
4949 create_jdep_list (ctxp)
4950 struct parser_ctxt *ctxp;
4951 {
4952 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4953 new->first = new->last = NULL;
4954 new->next = ctxp->classd_list;
4955 ctxp->classd_list = new;
4956 }
4957
4958 static jdeplist *
4959 reverse_jdep_list (ctxp)
4960 struct parser_ctxt *ctxp;
4961 {
4962 register jdeplist *prev = NULL, *current, *next;
4963 for (current = ctxp->classd_list; current; current = next)
4964 {
4965 next = current->next;
4966 current->next = prev;
4967 prev = current;
4968 }
4969 return prev;
4970 }
4971
4972 /* Create a fake pointer based on the ID stored in
4973 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4974 registered again. */
4975
4976 static tree
4977 obtain_incomplete_type (type_name)
4978 tree type_name;
4979 {
4980 tree ptr, name;
4981
4982 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4983 name = EXPR_WFL_NODE (type_name);
4984 else if (INCOMPLETE_TYPE_P (type_name))
4985 name = TYPE_NAME (type_name);
4986 else
4987 abort ();
4988
4989 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
4990 if (TYPE_NAME (ptr) == name)
4991 break;
4992
4993 if (!ptr)
4994 {
4995 BUILD_PTR_FROM_NAME (ptr, name);
4996 layout_type (ptr);
4997 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4998 ctxp->incomplete_class = ptr;
4999 }
5000
5001 return ptr;
5002 }
5003
5004 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5005 non NULL instead of computing a new fake type based on WFL. The new
5006 dependency is inserted in the current type dependency list, in FIFO
5007 manner. */
5008
5009 static tree
5010 register_incomplete_type (kind, wfl, decl, ptr)
5011 int kind;
5012 tree wfl, decl, ptr;
5013 {
5014 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5015
5016 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5017 ptr = obtain_incomplete_type (wfl);
5018
5019 JDEP_KIND (new) = kind;
5020 JDEP_DECL (new) = decl;
5021 JDEP_SOLV (new) = ptr;
5022 JDEP_WFL (new) = wfl;
5023 JDEP_CHAIN (new) = NULL;
5024 JDEP_MISC (new) = NULL_TREE;
5025 /* For some dependencies, set the enclosing class of the current
5026 class to be the enclosing context */
5027 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5028 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
5029 && GET_ENCLOSING_CPC ())
5030 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5031 else
5032 JDEP_ENCLOSING (new) = GET_CPC ();
5033 JDEP_GET_PATCH (new) = (tree *)NULL;
5034
5035 JDEP_INSERT (ctxp->classd_list, new);
5036
5037 return ptr;
5038 }
5039
5040 void
5041 java_check_circular_reference ()
5042 {
5043 tree current;
5044 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5045 {
5046 tree type = TREE_TYPE (current);
5047 if (CLASS_INTERFACE (current))
5048 {
5049 /* Check all interfaces this class extends */
5050 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5051 int n, i;
5052
5053 if (!basetype_vec)
5054 return;
5055 n = TREE_VEC_LENGTH (basetype_vec);
5056 for (i = 0; i < n; i++)
5057 {
5058 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5059 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5060 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5061 parse_error_context (lookup_cl (current),
5062 "Cyclic interface inheritance");
5063 }
5064 }
5065 else
5066 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5067 parse_error_context (lookup_cl (current),
5068 "Cyclic class inheritance%s",
5069 (cyclic_inheritance_report ?
5070 cyclic_inheritance_report : ""));
5071 }
5072 }
5073
5074 /* Augment the parameter list PARM with parameters crafted to
5075 initialize outer context locals aliases. Through ARTIFICIAL, a
5076 count is kept of the number of crafted parameters. MODE governs
5077 what eventually gets created: something suitable for a function
5078 creation or a function invocation, either the constructor or
5079 finit$. */
5080
5081 static tree
5082 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5083 int mode;
5084 tree class_type, parm;
5085 int *artificial;
5086 {
5087 tree field;
5088 tree additional_parms = NULL_TREE;
5089
5090 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5091 if (FIELD_LOCAL_ALIAS (field))
5092 {
5093 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5094 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5095 tree mangled_id;
5096
5097 switch (mode)
5098 {
5099 case AIPL_FUNCTION_DECLARATION:
5100 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5101 &buffer [4]);
5102 purpose = build_wfl_node (mangled_id);
5103 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5104 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5105 else
5106 value = TREE_TYPE (field);
5107 break;
5108
5109 case AIPL_FUNCTION_CREATION:
5110 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5111 &buffer [4]);
5112 value = TREE_TYPE (field);
5113 break;
5114
5115 case AIPL_FUNCTION_FINIT_INVOCATION:
5116 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5117 &buffer [4]);
5118 /* Now, this is wrong. purpose should always be the NAME
5119 of something and value its matching value (decl, type,
5120 etc...) FIXME -- but there is a lot to fix. */
5121
5122 /* When invoked for this kind of operation, we already
5123 know whether a field is used or not. */
5124 purpose = TREE_TYPE (field);
5125 value = build_wfl_node (mangled_id);
5126 break;
5127
5128 case AIPL_FUNCTION_CTOR_INVOCATION:
5129 /* There are two case: the constructor invokation happends
5130 outside the local inner, in which case, locales from the outer
5131 context are directly used.
5132
5133 Otherwise, we fold to using the alias directly. */
5134 if (class_type == current_class)
5135 value = field;
5136 else
5137 {
5138 name = get_identifier (&buffer[4]);
5139 value = IDENTIFIER_LOCAL_VALUE (name);
5140 }
5141 break;
5142 }
5143 additional_parms = tree_cons (purpose, value, additional_parms);
5144 if (artificial)
5145 *artificial +=1;
5146 }
5147 if (additional_parms)
5148 {
5149 if (ANONYMOUS_CLASS_P (class_type)
5150 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5151 additional_parms = nreverse (additional_parms);
5152 parm = chainon (additional_parms, parm);
5153 }
5154
5155 return parm;
5156 }
5157
5158 /* Craft a constructor for CLASS_DECL -- what we should do when none
5159 where found. ARGS is non NULL when a special signature must be
5160 enforced. This is the case for anonymous classes. */
5161
5162 static void
5163 craft_constructor (class_decl, args)
5164 tree class_decl, args;
5165 {
5166 tree class_type = TREE_TYPE (class_decl);
5167 tree parm = NULL_TREE;
5168 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5169 ACC_PUBLIC : 0);
5170 int i = 0, artificial = 0;
5171 tree decl, ctor_name;
5172 char buffer [80];
5173
5174 /* The constructor name is <init> unless we're dealing with an
5175 anonymous class, in which case the name will be fixed after having
5176 be expanded. */
5177 if (ANONYMOUS_CLASS_P (class_type))
5178 ctor_name = DECL_NAME (class_decl);
5179 else
5180 ctor_name = init_identifier_node;
5181
5182 /* If we're dealing with an inner class constructor, we hide the
5183 this$<n> decl in the name field of its parameter declaration. */
5184 if (PURE_INNER_CLASS_TYPE_P (class_type))
5185 {
5186 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5187 parm = tree_cons (build_current_thisn (class_type),
5188 build_pointer_type (type), parm);
5189
5190 /* Some more arguments to be hidden here. The values of the local
5191 variables of the outer context that the inner class needs to see. */
5192 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5193 class_type, parm,
5194 &artificial);
5195 }
5196
5197 /* Then if there are any args to be enforced, enforce them now */
5198 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5199 {
5200 sprintf (buffer, "parm%d", i++);
5201 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5202 }
5203
5204 CRAFTED_PARAM_LIST_FIXUP (parm);
5205 decl = create_artificial_method (class_type, flags, void_type_node,
5206 ctor_name, parm);
5207 fix_method_argument_names (parm, decl);
5208 /* Now, mark the artificial parameters. */
5209 DECL_FUNCTION_NAP (decl) = artificial;
5210 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5211 }
5212
5213
5214 /* Fix the constructors. This will be called right after circular
5215 references have been checked. It is necessary to fix constructors
5216 early even if no code generation will take place for that class:
5217 some generated constructor might be required by the class whose
5218 compilation triggered this one to be simply loaded. */
5219
5220 void
5221 java_fix_constructors ()
5222 {
5223 tree current;
5224
5225 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5226 {
5227 tree class_type = TREE_TYPE (current);
5228 int saw_ctor = 0;
5229 tree decl;
5230
5231 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5232 continue;
5233
5234 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5235 {
5236 if (DECL_CONSTRUCTOR_P (decl))
5237 {
5238 fix_constructors (decl);
5239 saw_ctor = 1;
5240 }
5241 }
5242
5243 /* Anonymous class constructor can't be generated that early. */
5244 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5245 craft_constructor (current, NULL_TREE);
5246 }
5247 }
5248
5249 /* safe_layout_class just makes sure that we can load a class without
5250 disrupting the current_class, input_file, lineno, etc, information
5251 about the class processed currently. */
5252
5253 void
5254 safe_layout_class (class)
5255 tree class;
5256 {
5257 tree save_current_class = current_class;
5258 const char *save_input_filename = input_filename;
5259 int save_lineno = lineno;
5260
5261 layout_class (class);
5262
5263 current_class = save_current_class;
5264 input_filename = save_input_filename;
5265 lineno = save_lineno;
5266 CLASS_LOADED_P (class) = 1;
5267 }
5268
5269 static tree
5270 jdep_resolve_class (dep)
5271 jdep *dep;
5272 {
5273 tree decl;
5274
5275 if (JDEP_RESOLVED_P (dep))
5276 decl = JDEP_RESOLVED_DECL (dep);
5277 else
5278 {
5279 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5280 JDEP_DECL (dep), JDEP_WFL (dep));
5281 JDEP_RESOLVED (dep, decl);
5282 }
5283
5284 if (!decl)
5285 complete_class_report_errors (dep);
5286 else if (PURE_INNER_CLASS_DECL_P (decl))
5287 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5288 return decl;
5289 }
5290
5291 /* Complete unsatisfied class declaration and their dependencies */
5292
5293 void
5294 java_complete_class ()
5295 {
5296 tree cclass;
5297 jdeplist *cclassd;
5298 int error_found;
5299 tree type;
5300
5301 /* Process imports */
5302 process_imports ();
5303
5304 /* Rever things so we have the right order */
5305 ctxp->class_list = nreverse (ctxp->class_list);
5306 ctxp->classd_list = reverse_jdep_list (ctxp);
5307
5308 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5309 cclass && cclassd;
5310 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5311 {
5312 jdep *dep;
5313 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5314 {
5315 tree decl;
5316 if (!(decl = jdep_resolve_class (dep)))
5317 continue;
5318
5319 /* Now it's time to patch */
5320 switch (JDEP_KIND (dep))
5321 {
5322 case JDEP_SUPER:
5323 /* Simply patch super */
5324 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5325 continue;
5326 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5327 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5328 break;
5329
5330 case JDEP_FIELD:
5331 {
5332 /* We do part of the job done in add_field */
5333 tree field_decl = JDEP_DECL (dep);
5334 tree field_type = TREE_TYPE (decl);
5335 if (TREE_CODE (field_type) == RECORD_TYPE)
5336 field_type = promote_type (field_type);
5337 TREE_TYPE (field_decl) = field_type;
5338 DECL_ALIGN (field_decl) = 0;
5339 DECL_USER_ALIGN (field_decl) = 0;
5340 layout_decl (field_decl, 0);
5341 SOURCE_FRONTEND_DEBUG
5342 (("Completed field/var decl `%s' with `%s'",
5343 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5344 IDENTIFIER_POINTER (DECL_NAME (decl))));
5345 break;
5346 }
5347 case JDEP_METHOD: /* We start patching a method */
5348 case JDEP_METHOD_RETURN:
5349 error_found = 0;
5350 while (1)
5351 {
5352 if (decl)
5353 {
5354 type = TREE_TYPE(decl);
5355 if (TREE_CODE (type) == RECORD_TYPE)
5356 type = promote_type (type);
5357 JDEP_APPLY_PATCH (dep, type);
5358 SOURCE_FRONTEND_DEBUG
5359 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5360 "Completing fct `%s' with ret type `%s'":
5361 "Completing arg `%s' with type `%s'"),
5362 IDENTIFIER_POINTER (EXPR_WFL_NODE
5363 (JDEP_DECL_WFL (dep))),
5364 IDENTIFIER_POINTER (DECL_NAME (decl))));
5365 }
5366 else
5367 error_found = 1;
5368 dep = JDEP_CHAIN (dep);
5369 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5370 break;
5371 else
5372 decl = jdep_resolve_class (dep);
5373 }
5374 if (!error_found)
5375 {
5376 tree mdecl = JDEP_DECL (dep), signature;
5377 /* Recompute and reset the signature, check first that
5378 all types are now defined. If they're not,
5379 dont build the signature. */
5380 if (check_method_types_complete (mdecl))
5381 {
5382 signature = build_java_signature (TREE_TYPE (mdecl));
5383 set_java_signature (TREE_TYPE (mdecl), signature);
5384 }
5385 }
5386 else
5387 continue;
5388 break;
5389
5390 case JDEP_INTERFACE:
5391 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5392 JDEP_WFL (dep)))
5393 continue;
5394 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5395 break;
5396
5397 case JDEP_PARM:
5398 case JDEP_VARIABLE:
5399 type = TREE_TYPE(decl);
5400 if (TREE_CODE (type) == RECORD_TYPE)
5401 type = promote_type (type);
5402 JDEP_APPLY_PATCH (dep, type);
5403 break;
5404
5405 case JDEP_TYPE:
5406 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5407 SOURCE_FRONTEND_DEBUG
5408 (("Completing a random type dependency on a '%s' node",
5409 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5410 break;
5411
5412 case JDEP_EXCEPTION:
5413 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5414 SOURCE_FRONTEND_DEBUG
5415 (("Completing `%s' `throws' argument node",
5416 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5417 break;
5418
5419 case JDEP_ANONYMOUS:
5420 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5421 break;
5422
5423 default:
5424 abort ();
5425 }
5426 }
5427 }
5428 return;
5429 }
5430
5431 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5432 array. */
5433
5434 static tree
5435 resolve_class (enclosing, class_type, decl, cl)
5436 tree enclosing, class_type, decl, cl;
5437 {
5438 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5439 const char *base = name;
5440 tree resolved_type = TREE_TYPE (class_type);
5441 tree resolved_type_decl;
5442
5443 if (resolved_type != NULL_TREE)
5444 {
5445 tree resolved_type_decl = TYPE_NAME (resolved_type);
5446 if (resolved_type_decl == NULL_TREE
5447 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5448 {
5449 resolved_type_decl = build_decl (TYPE_DECL,
5450 TYPE_NAME (class_type),
5451 resolved_type);
5452 }
5453 return resolved_type_decl;
5454 }
5455
5456 /* 1- Check to see if we have an array. If true, find what we really
5457 want to resolve */
5458 while (name[0] == '[')
5459 name++;
5460 if (base != name)
5461 {
5462 TYPE_NAME (class_type) = get_identifier (name);
5463 WFL_STRIP_BRACKET (cl, cl);
5464 }
5465
5466 /* 2- Resolve the bare type */
5467 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5468 decl, cl)))
5469 return NULL_TREE;
5470 resolved_type = TREE_TYPE (resolved_type_decl);
5471
5472 /* 3- If we have and array, reconstruct the array down to its nesting */
5473 if (base != name)
5474 {
5475 while (base != name)
5476 {
5477 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5478 resolved_type = promote_type (resolved_type);
5479 resolved_type = build_java_array_type (resolved_type, -1);
5480 CLASS_LOADED_P (resolved_type) = 1;
5481 name--;
5482 }
5483 /* A TYPE_NAME that is a TYPE_DECL was set in
5484 build_java_array_type, return it. */
5485 resolved_type_decl = TYPE_NAME (resolved_type);
5486 }
5487 TREE_TYPE (class_type) = resolved_type;
5488 return resolved_type_decl;
5489 }
5490
5491 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5492 are used to report error messages. */
5493
5494 tree
5495 do_resolve_class (enclosing, class_type, decl, cl)
5496 tree enclosing, class_type, decl, cl;
5497 {
5498 tree new_class_decl;
5499
5500 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5501 it is changed by find_in_imports{_on_demand} and (but it doesn't
5502 really matter) qualify_and_find */
5503
5504 /* 0- Search in the current class as an inner class */
5505
5506 /* Maybe some code here should be added to load the class or
5507 something, at least if the class isn't an inner class and ended
5508 being loaded from class file. FIXME. */
5509 while (enclosing)
5510 {
5511 tree name;
5512 tree intermediate;
5513
5514 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5515 return new_class_decl;
5516
5517 intermediate = enclosing;
5518 /* Explore enclosing contexts. */
5519 while (INNER_CLASS_DECL_P (intermediate))
5520 {
5521 intermediate = DECL_CONTEXT (intermediate);
5522 if ((new_class_decl = find_as_inner_class (intermediate,
5523 class_type, cl)))
5524 return new_class_decl;
5525 }
5526
5527 /* Now go to the upper classes, bail out if necessary. */
5528 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5529 if (!enclosing || enclosing == object_type_node)
5530 break;
5531
5532 if (TREE_CODE (enclosing) == RECORD_TYPE)
5533 {
5534 enclosing = TYPE_NAME (enclosing);
5535 continue;
5536 }
5537
5538 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5539 BUILD_PTR_FROM_NAME (name, enclosing);
5540 else
5541 name = enclosing;
5542 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5543 }
5544
5545 /* 1- Check for the type in single imports. This will change
5546 TYPE_NAME() if something relevant is found */
5547 find_in_imports (class_type);
5548
5549 /* 2- And check for the type in the current compilation unit */
5550 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5551 {
5552 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5553 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5554 load_class (TYPE_NAME (class_type), 0);
5555 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5556 }
5557
5558 /* 3- Search according to the current package definition */
5559 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5560 {
5561 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5562 TYPE_NAME (class_type))))
5563 return new_class_decl;
5564 }
5565
5566 /* 4- Check the import on demands. Don't allow bar.baz to be
5567 imported from foo.* */
5568 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5569 if (find_in_imports_on_demand (class_type))
5570 return NULL_TREE;
5571
5572 /* If found in find_in_imports_on_demant, the type has already been
5573 loaded. */
5574 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5575 return new_class_decl;
5576
5577 /* 5- Try with a name qualified with the package name we've seen so far */
5578 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5579 {
5580 tree package;
5581
5582 /* If there is a current package (ctxp->package), it's the first
5583 element of package_list and we can skip it. */
5584 for (package = (ctxp->package ?
5585 TREE_CHAIN (package_list) : package_list);
5586 package; package = TREE_CHAIN (package))
5587 if ((new_class_decl = qualify_and_find (class_type,
5588 TREE_PURPOSE (package),
5589 TYPE_NAME (class_type))))
5590 return new_class_decl;
5591 }
5592
5593 /* 5- Check an other compilation unit that bears the name of type */
5594 load_class (TYPE_NAME (class_type), 0);
5595 if (check_pkg_class_access (TYPE_NAME (class_type),
5596 (cl ? cl : lookup_cl (decl))))
5597 return NULL_TREE;
5598
5599 /* 6- Last call for a resolution */
5600 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5601 }
5602
5603 static tree
5604 qualify_and_find (class_type, package, name)
5605 tree class_type, package, name;
5606 {
5607 tree new_qualified = merge_qualified_name (package, name);
5608 tree new_class_decl;
5609
5610 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5611 load_class (new_qualified, 0);
5612 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5613 {
5614 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5615 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5616 load_class (new_qualified, 0);
5617 TYPE_NAME (class_type) = new_qualified;
5618 return IDENTIFIER_CLASS_VALUE (new_qualified);
5619 }
5620 return NULL_TREE;
5621 }
5622
5623 /* Resolve NAME and lay it out (if not done and if not the current
5624 parsed class). Return a decl node. This function is meant to be
5625 called when type resolution is necessary during the walk pass. */
5626
5627 static tree
5628 resolve_and_layout (something, cl)
5629 tree something;
5630 tree cl;
5631 {
5632 tree decl, decl_type;
5633
5634 /* Don't do that on the current class */
5635 if (something == current_class)
5636 return TYPE_NAME (current_class);
5637
5638 /* Don't do anything for void and other primitive types */
5639 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5640 return NULL_TREE;
5641
5642 /* Pointer types can be reall pointer types or fake pointers. When
5643 finding a real pointer, recheck for primitive types */
5644 if (TREE_CODE (something) == POINTER_TYPE)
5645 {
5646 if (TREE_TYPE (something))
5647 {
5648 something = TREE_TYPE (something);
5649 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5650 return NULL_TREE;
5651 }
5652 else
5653 something = TYPE_NAME (something);
5654 }
5655
5656 /* Don't do anything for arrays of primitive types */
5657 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5658 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5659 return NULL_TREE;
5660
5661 /* Something might be a WFL */
5662 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5663 something = EXPR_WFL_NODE (something);
5664
5665 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5666 TYPE_DECL or a real TYPE */
5667 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5668 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5669 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5670
5671 if (!(decl = resolve_no_layout (something, cl)))
5672 return NULL_TREE;
5673
5674 /* Resolve and layout if necessary */
5675 decl_type = TREE_TYPE (decl);
5676 layout_class_methods (decl_type);
5677 /* Check methods */
5678 if (CLASS_FROM_SOURCE_P (decl_type))
5679 java_check_methods (decl);
5680 /* Layout the type if necessary */
5681 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5682 safe_layout_class (decl_type);
5683
5684 return decl;
5685 }
5686
5687 /* Resolve a class, returns its decl but doesn't perform any
5688 layout. The current parsing context is saved and restored */
5689
5690 static tree
5691 resolve_no_layout (name, cl)
5692 tree name, cl;
5693 {
5694 tree ptr, decl;
5695 BUILD_PTR_FROM_NAME (ptr, name);
5696 java_parser_context_save_global ();
5697 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5698 java_parser_context_restore_global ();
5699
5700 return decl;
5701 }
5702
5703 /* Called when reporting errors. Skip leader '[' in a complex array
5704 type description that failed to be resolved. */
5705
5706 static const char *
5707 purify_type_name (name)
5708 const char *name;
5709 {
5710 while (*name && *name == '[')
5711 name++;
5712 return name;
5713 }
5714
5715 /* The type CURRENT refers to can't be found. We print error messages. */
5716
5717 static void
5718 complete_class_report_errors (dep)
5719 jdep *dep;
5720 {
5721 const char *name;
5722
5723 if (!JDEP_WFL (dep))
5724 return;
5725
5726 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5727 switch (JDEP_KIND (dep))
5728 {
5729 case JDEP_SUPER:
5730 parse_error_context
5731 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5732 purify_type_name (name),
5733 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5734 break;
5735 case JDEP_FIELD:
5736 parse_error_context
5737 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5738 purify_type_name (name),
5739 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5740 break;
5741 case JDEP_METHOD: /* Covers arguments */
5742 parse_error_context
5743 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5744 purify_type_name (name),
5745 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5746 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5747 break;
5748 case JDEP_METHOD_RETURN: /* Covers return type */
5749 parse_error_context
5750 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5751 purify_type_name (name),
5752 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5753 break;
5754 case JDEP_INTERFACE:
5755 parse_error_context
5756 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5757 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5758 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5759 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5760 break;
5761 case JDEP_VARIABLE:
5762 parse_error_context
5763 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5764 purify_type_name (IDENTIFIER_POINTER
5765 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5766 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5767 break;
5768 case JDEP_EXCEPTION: /* As specified by `throws' */
5769 parse_error_context
5770 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5771 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5772 break;
5773 default:
5774 /* Fix for -Wall. Just break doing nothing. The error will be
5775 caught later */
5776 break;
5777 }
5778 }
5779
5780 /* Return a static string containing the DECL prototype string. If
5781 DECL is a constructor, use the class name instead of the form
5782 <init> */
5783
5784 static const char *
5785 get_printable_method_name (decl)
5786 tree decl;
5787 {
5788 const char *to_return;
5789 tree name = NULL_TREE;
5790
5791 if (DECL_CONSTRUCTOR_P (decl))
5792 {
5793 name = DECL_NAME (decl);
5794 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5795 }
5796
5797 to_return = lang_printable_name (decl, 0);
5798 if (DECL_CONSTRUCTOR_P (decl))
5799 DECL_NAME (decl) = name;
5800
5801 return to_return;
5802 }
5803
5804 /* Track method being redefined inside the same class. As a side
5805 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5806 function it's a FWL, so we can track errors more accurately.) */
5807
5808 static int
5809 check_method_redefinition (class, method)
5810 tree class, method;
5811 {
5812 tree redef, sig;
5813
5814 /* There's no need to verify <clinit> and finit$ */
5815 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
5816 return 0;
5817
5818 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5819 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5820 {
5821 if (redef == method)
5822 break;
5823 if (DECL_NAME (redef) == DECL_NAME (method)
5824 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5825 && !DECL_ARTIFICIAL (method))
5826 {
5827 parse_error_context
5828 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
5829 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5830 get_printable_method_name (redef));
5831 return 1;
5832 }
5833 }
5834 return 0;
5835 }
5836
5837 /* Return 1 if check went ok, 0 otherwise. */
5838 static int
5839 check_abstract_method_definitions (do_interface, class_decl, type)
5840 int do_interface;
5841 tree class_decl, type;
5842 {
5843 tree class = TREE_TYPE (class_decl);
5844 tree method, end_type;
5845 int ok = 1;
5846
5847 end_type = (do_interface ? object_type_node : type);
5848 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5849 {
5850 tree other_super, other_method, method_sig, method_name;
5851 int found = 0;
5852 int end_type_reached = 0;
5853
5854 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5855 continue;
5856
5857 /* Now verify that somewhere in between TYPE and CLASS,
5858 abstract method METHOD gets a non abstract definition
5859 that is inherited by CLASS. */
5860
5861 method_sig = build_java_signature (TREE_TYPE (method));
5862 method_name = DECL_NAME (method);
5863 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5864 method_name = EXPR_WFL_NODE (method_name);
5865
5866 other_super = class;
5867 do {
5868 if (other_super == end_type)
5869 end_type_reached = 1;
5870
5871 /* Method search */
5872 for (other_method = TYPE_METHODS (other_super); other_method;
5873 other_method = TREE_CHAIN (other_method))
5874 {
5875 tree s = build_java_signature (TREE_TYPE (other_method));
5876 tree other_name = DECL_NAME (other_method);
5877
5878 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5879 other_name = EXPR_WFL_NODE (other_name);
5880 if (!DECL_CLINIT_P (other_method)
5881 && !DECL_CONSTRUCTOR_P (other_method)
5882 && method_name == other_name
5883 && method_sig == s
5884 && !METHOD_ABSTRACT (other_method))
5885 {
5886 found = 1;
5887 break;
5888 }
5889 }
5890 other_super = CLASSTYPE_SUPER (other_super);
5891 } while (!end_type_reached);
5892
5893 /* Report that abstract METHOD didn't find an implementation
5894 that CLASS can use. */
5895 if (!found)
5896 {
5897 char *t = xstrdup (lang_printable_name
5898 (TREE_TYPE (TREE_TYPE (method)), 0));
5899 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5900
5901 parse_error_context
5902 (lookup_cl (class_decl),
5903 "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",
5904 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5905 t, lang_printable_name (method, 0),
5906 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5907 "interface" : "class"),
5908 IDENTIFIER_POINTER (ccn),
5909 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5910 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5911 ok = 0;
5912 free (t);
5913 }
5914 }
5915
5916 if (ok && do_interface)
5917 {
5918 /* Check for implemented interfaces. */
5919 int i;
5920 tree vector = TYPE_BINFO_BASETYPES (type);
5921 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5922 {
5923 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5924 ok = check_abstract_method_definitions (1, class_decl, super);
5925 }
5926 }
5927
5928 return ok;
5929 }
5930
5931 /* Check that CLASS_DECL somehow implements all inherited abstract
5932 methods. */
5933
5934 static void
5935 java_check_abstract_method_definitions (class_decl)
5936 tree class_decl;
5937 {
5938 tree class = TREE_TYPE (class_decl);
5939 tree super, vector;
5940 int i;
5941
5942 if (CLASS_ABSTRACT (class_decl))
5943 return;
5944
5945 /* Check for inherited types */
5946 super = class;
5947 do {
5948 super = CLASSTYPE_SUPER (super);
5949 check_abstract_method_definitions (0, class_decl, super);
5950 } while (super != object_type_node);
5951
5952 /* Check for implemented interfaces. */
5953 vector = TYPE_BINFO_BASETYPES (class);
5954 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5955 {
5956 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5957 check_abstract_method_definitions (1, class_decl, super);
5958 }
5959 }
5960
5961 /* Check all the types method DECL uses and return 1 if all of them
5962 are now complete, 0 otherwise. This is used to check whether its
5963 safe to build a method signature or not. */
5964
5965 static int
5966 check_method_types_complete (decl)
5967 tree decl;
5968 {
5969 tree type = TREE_TYPE (decl);
5970 tree args;
5971
5972 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5973 return 0;
5974
5975 args = TYPE_ARG_TYPES (type);
5976 if (TREE_CODE (type) == METHOD_TYPE)
5977 args = TREE_CHAIN (args);
5978 for (; args != end_params_node; args = TREE_CHAIN (args))
5979 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5980 return 0;
5981
5982 return 1;
5983 }
5984
5985 /* Visible interface to check methods contained in CLASS_DECL */
5986
5987 void
5988 java_check_methods (class_decl)
5989 tree class_decl;
5990 {
5991 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5992 return;
5993
5994 if (CLASS_INTERFACE (class_decl))
5995 java_check_abstract_methods (class_decl);
5996 else
5997 java_check_regular_methods (class_decl);
5998
5999 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6000 }
6001
6002 /* Check all the methods of CLASS_DECL. Methods are first completed
6003 then checked according to regular method existance rules. If no
6004 constructor for CLASS_DECL were encountered, then build its
6005 declaration. */
6006
6007 static void
6008 java_check_regular_methods (class_decl)
6009 tree class_decl;
6010 {
6011 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6012 tree method;
6013 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6014 tree found = NULL_TREE;
6015 tree mthrows;
6016
6017 /* It is not necessary to check methods defined in java.lang.Object */
6018 if (class == object_type_node)
6019 return;
6020
6021 if (!TYPE_NVIRTUALS (class))
6022 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6023
6024 /* Should take interfaces into account. FIXME */
6025 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6026 {
6027 tree sig;
6028 tree method_wfl = DECL_FUNCTION_WFL (method);
6029 int aflags;
6030
6031 /* Check for redefinitions */
6032 if (check_method_redefinition (class, method))
6033 continue;
6034
6035 /* If we see one constructor a mark so we don't generate the
6036 default one. Also skip other verifications: constructors
6037 can't be inherited hence hiden or overriden */
6038 if (DECL_CONSTRUCTOR_P (method))
6039 {
6040 saw_constructor = 1;
6041 continue;
6042 }
6043
6044 /* We verify things thrown by the method. They must inherits from
6045 java.lang.Throwable */
6046 for (mthrows = DECL_FUNCTION_THROWS (method);
6047 mthrows; mthrows = TREE_CHAIN (mthrows))
6048 {
6049 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6050 parse_error_context
6051 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6052 IDENTIFIER_POINTER
6053 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6054 }
6055
6056 sig = build_java_argument_signature (TREE_TYPE (method));
6057 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6058
6059 /* Inner class can't declare static methods */
6060 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6061 {
6062 char *t = xstrdup (lang_printable_name (class, 0));
6063 parse_error_context
6064 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6065 lang_printable_name (method, 0), t);
6066 free (t);
6067 }
6068
6069 /* Nothing overrides or it's a private method. */
6070 if (!found)
6071 continue;
6072 if (METHOD_PRIVATE (found))
6073 {
6074 found = NULL_TREE;
6075 continue;
6076 }
6077
6078 /* If `found' is declared in an interface, make sure the
6079 modifier matches. */
6080 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6081 && clinit_identifier_node != DECL_NAME (found)
6082 && !METHOD_PUBLIC (method))
6083 {
6084 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6085 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6086 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6087 lang_printable_name (method, 0),
6088 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6089 }
6090
6091 /* Can't override a method with the same name and different return
6092 types. */
6093 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6094 {
6095 char *t = xstrdup
6096 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6097 parse_error_context
6098 (method_wfl,
6099 "Method `%s' was defined with return type `%s' in class `%s'",
6100 lang_printable_name (found, 0), t,
6101 IDENTIFIER_POINTER
6102 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6103 free (t);
6104 }
6105
6106 aflags = get_access_flags_from_decl (found);
6107
6108 /* Can't override final. Can't override static. */
6109 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6110 {
6111 /* Static *can* override static */
6112 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6113 continue;
6114 parse_error_context
6115 (method_wfl,
6116 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6117 (METHOD_FINAL (found) ? "Final" : "Static"),
6118 lang_printable_name (found, 0),
6119 (METHOD_FINAL (found) ? "final" : "static"),
6120 IDENTIFIER_POINTER
6121 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6122 continue;
6123 }
6124
6125 /* Static method can't override instance method. */
6126 if (METHOD_STATIC (method))
6127 {
6128 parse_error_context
6129 (method_wfl,
6130 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6131 lang_printable_name (found, 0),
6132 IDENTIFIER_POINTER
6133 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6134 continue;
6135 }
6136
6137 /* - Overriding/hiding public must be public
6138 - Overriding/hiding protected must be protected or public
6139 - If the overriden or hidden method has default (package)
6140 access, then the overriding or hiding method must not be
6141 private; otherwise, a compile-time error occurs. If
6142 `found' belongs to an interface, things have been already
6143 taken care of. */
6144 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6145 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6146 || (METHOD_PROTECTED (found)
6147 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6148 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6149 && METHOD_PRIVATE (method))))
6150 {
6151 parse_error_context
6152 (method_wfl,
6153 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6154 (METHOD_PUBLIC (method) ? "public" :
6155 (METHOD_PRIVATE (method) ? "private" : "protected")),
6156 IDENTIFIER_POINTER (DECL_NAME
6157 (TYPE_NAME (DECL_CONTEXT (found)))));
6158 continue;
6159 }
6160
6161 /* Overriding methods must have compatible `throws' clauses on checked
6162 exceptions, if any */
6163 check_throws_clauses (method, method_wfl, found);
6164
6165 /* Inheriting multiple methods with the same signature. FIXME */
6166 }
6167
6168 if (!TYPE_NVIRTUALS (class))
6169 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6170
6171 /* Search for inherited abstract method not yet implemented in this
6172 class. */
6173 java_check_abstract_method_definitions (class_decl);
6174
6175 if (!saw_constructor)
6176 abort ();
6177 }
6178
6179 /* Return a non zero value if the `throws' clause of METHOD (if any)
6180 is incompatible with the `throws' clause of FOUND (if any). */
6181
6182 static void
6183 check_throws_clauses (method, method_wfl, found)
6184 tree method, method_wfl, found;
6185 {
6186 tree mthrows, fthrows;
6187
6188 /* Can't check these things with class loaded from bytecode. FIXME */
6189 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6190 return;
6191
6192 for (mthrows = DECL_FUNCTION_THROWS (method);
6193 mthrows; mthrows = TREE_CHAIN (mthrows))
6194 {
6195 /* We don't verify unchecked expressions */
6196 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6197 continue;
6198 /* Checked expression must be compatible */
6199 for (fthrows = DECL_FUNCTION_THROWS (found);
6200 fthrows; fthrows = TREE_CHAIN (fthrows))
6201 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6202 break;
6203 if (!fthrows)
6204 {
6205 parse_error_context
6206 (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'",
6207 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6208 lang_printable_name (found, 0),
6209 IDENTIFIER_POINTER
6210 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6211 }
6212 }
6213 }
6214
6215 /* Check abstract method of interface INTERFACE */
6216
6217 static void
6218 java_check_abstract_methods (interface_decl)
6219 tree interface_decl;
6220 {
6221 int i, n;
6222 tree method, basetype_vec, found;
6223 tree interface = TREE_TYPE (interface_decl);
6224
6225 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6226 {
6227 /* 2- Check for double definition inside the defining interface */
6228 if (check_method_redefinition (interface, method))
6229 continue;
6230
6231 /* 3- Overriding is OK as far as we preserve the return type and
6232 the thrown exceptions (FIXME) */
6233 found = lookup_java_interface_method2 (interface, method);
6234 if (found)
6235 {
6236 char *t;
6237 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6238 parse_error_context
6239 (DECL_FUNCTION_WFL (found),
6240 "Method `%s' was defined with return type `%s' in class `%s'",
6241 lang_printable_name (found, 0), t,
6242 IDENTIFIER_POINTER
6243 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6244 free (t);
6245 continue;
6246 }
6247 }
6248
6249 /* 4- Inherited methods can't differ by their returned types */
6250 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6251 return;
6252 n = TREE_VEC_LENGTH (basetype_vec);
6253 for (i = 0; i < n; i++)
6254 {
6255 tree sub_interface_method, sub_interface;
6256 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6257 if (!vec_elt)
6258 continue;
6259 sub_interface = BINFO_TYPE (vec_elt);
6260 for (sub_interface_method = TYPE_METHODS (sub_interface);
6261 sub_interface_method;
6262 sub_interface_method = TREE_CHAIN (sub_interface_method))
6263 {
6264 found = lookup_java_interface_method2 (interface,
6265 sub_interface_method);
6266 if (found && (found != sub_interface_method))
6267 {
6268 parse_error_context
6269 (lookup_cl (sub_interface_method),
6270 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6271 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6272 lang_printable_name (found, 0),
6273 IDENTIFIER_POINTER
6274 (DECL_NAME (TYPE_NAME
6275 (DECL_CONTEXT (sub_interface_method)))),
6276 IDENTIFIER_POINTER
6277 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6278 }
6279 }
6280 }
6281 }
6282
6283 /* Lookup methods in interfaces using their name and partial
6284 signature. Return a matching method only if their types differ. */
6285
6286 static tree
6287 lookup_java_interface_method2 (class, method_decl)
6288 tree class, method_decl;
6289 {
6290 int i, n;
6291 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6292
6293 if (!basetype_vec)
6294 return NULL_TREE;
6295
6296 n = TREE_VEC_LENGTH (basetype_vec);
6297 for (i = 0; i < n; i++)
6298 {
6299 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6300 if ((BINFO_TYPE (vec_elt) != object_type_node)
6301 && (to_return =
6302 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6303 return to_return;
6304 }
6305 for (i = 0; i < n; i++)
6306 {
6307 to_return = lookup_java_interface_method2
6308 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6309 if (to_return)
6310 return to_return;
6311 }
6312
6313 return NULL_TREE;
6314 }
6315
6316 /* Lookup method using their name and partial signature. Return a
6317 matching method only if their types differ. */
6318
6319 static tree
6320 lookup_java_method2 (clas, method_decl, do_interface)
6321 tree clas, method_decl;
6322 int do_interface;
6323 {
6324 tree method, method_signature, method_name, method_type, name;
6325
6326 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6327 name = DECL_NAME (method_decl);
6328 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6329 EXPR_WFL_NODE (name) : name);
6330 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6331
6332 while (clas != NULL_TREE)
6333 {
6334 for (method = TYPE_METHODS (clas);
6335 method != NULL_TREE; method = TREE_CHAIN (method))
6336 {
6337 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6338 tree name = DECL_NAME (method);
6339 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6340 EXPR_WFL_NODE (name) : name) == method_name
6341 && method_sig == method_signature
6342 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6343 return method;
6344 }
6345 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6346 }
6347 return NULL_TREE;
6348 }
6349
6350 /* Return the line that matches DECL line number, and try its best to
6351 position the column number. Used during error reports. */
6352
6353 static tree
6354 lookup_cl (decl)
6355 tree decl;
6356 {
6357 static tree cl = NULL_TREE;
6358 char *line, *found;
6359
6360 if (!decl)
6361 return NULL_TREE;
6362
6363 if (cl == NULL_TREE)
6364 {
6365 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6366 ggc_add_tree_root (&cl, 1);
6367 }
6368
6369 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6370 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6371
6372 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6373 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6374
6375 found = strstr ((const char *)line,
6376 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6377 if (found)
6378 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6379
6380 return cl;
6381 }
6382
6383 /* Look for a simple name in the single-type import list */
6384
6385 static tree
6386 find_name_in_single_imports (name)
6387 tree name;
6388 {
6389 tree node;
6390
6391 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6392 if (TREE_VALUE (node) == name)
6393 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6394
6395 return NULL_TREE;
6396 }
6397
6398 /* Process all single-type import. */
6399
6400 static int
6401 process_imports ()
6402 {
6403 tree import;
6404 int error_found;
6405
6406 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6407 {
6408 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6409 char *original_name;
6410
6411 obstack_grow0 (&temporary_obstack,
6412 IDENTIFIER_POINTER (to_be_found),
6413 IDENTIFIER_LENGTH (to_be_found));
6414 original_name = obstack_finish (&temporary_obstack);
6415
6416 /* Don't load twice something already defined. */
6417 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6418 continue;
6419
6420 while (1)
6421 {
6422 tree left;
6423
6424 QUALIFIED_P (to_be_found) = 1;
6425 load_class (to_be_found, 0);
6426 error_found =
6427 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6428
6429 /* We found it, we can bail out */
6430 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6431 break;
6432
6433 /* We haven't found it. Maybe we're trying to access an
6434 inner class. The only way for us to know is to try again
6435 after having dropped a qualifier. If we can't break it further,
6436 we have an error. */
6437 if (breakdown_qualified (&left, NULL, to_be_found))
6438 break;
6439
6440 to_be_found = left;
6441 }
6442 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6443 {
6444 parse_error_context (TREE_PURPOSE (import),
6445 "Class or interface `%s' not found in import",
6446 original_name);
6447 error_found = 1;
6448 }
6449
6450 obstack_free (&temporary_obstack, original_name);
6451 if (error_found)
6452 return 1;
6453 }
6454 return 0;
6455 }
6456
6457 /* Possibly find and mark a class imported by a single-type import
6458 statement. */
6459
6460 static void
6461 find_in_imports (class_type)
6462 tree class_type;
6463 {
6464 tree import;
6465
6466 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6467 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6468 {
6469 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6470 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6471 }
6472 }
6473
6474 static int
6475 note_possible_classname (name, len)
6476 const char *name;
6477 int len;
6478 {
6479 tree node;
6480 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6481 len = len - 5;
6482 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6483 len = len - 6;
6484 else
6485 return 0;
6486 node = ident_subst (name, len, "", '/', '.', "");
6487 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6488 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6489 return 1;
6490 }
6491
6492 /* Read a import directory, gathering potential match for further type
6493 references. Indifferently reads a filesystem or a ZIP archive
6494 directory. */
6495
6496 static void
6497 read_import_dir (wfl)
6498 tree wfl;
6499 {
6500 tree package_id = EXPR_WFL_NODE (wfl);
6501 const char *package_name = IDENTIFIER_POINTER (package_id);
6502 int package_length = IDENTIFIER_LENGTH (package_id);
6503 DIR *dirp = NULL;
6504 JCF *saved_jcf = current_jcf;
6505
6506 int found = 0;
6507 int k;
6508 void *entry;
6509 struct buffer filename[1];
6510
6511
6512 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6513 return;
6514 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6515
6516 BUFFER_INIT (filename);
6517 buffer_grow (filename, package_length + 100);
6518
6519 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6520 {
6521 const char *entry_name = jcf_path_name (entry);
6522 int entry_length = strlen (entry_name);
6523 if (jcf_path_is_zipfile (entry))
6524 {
6525 ZipFile *zipf;
6526 buffer_grow (filename, entry_length);
6527 memcpy (filename->data, entry_name, entry_length - 1);
6528 filename->data[entry_length-1] = '\0';
6529 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6530 if (zipf == NULL)
6531 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6532 else
6533 {
6534 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6535 BUFFER_RESET (filename);
6536 for (k = 0; k < package_length; k++)
6537 {
6538 char ch = package_name[k];
6539 *filename->ptr++ = ch == '.' ? '/' : ch;
6540 }
6541 *filename->ptr++ = '/';
6542
6543 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6544 {
6545 const char *current_entry = ZIPDIR_FILENAME (zipd);
6546 int current_entry_len = zipd->filename_length;
6547
6548 if (current_entry_len >= BUFFER_LENGTH (filename)
6549 && strncmp (filename->data, current_entry,
6550 BUFFER_LENGTH (filename)) != 0)
6551 continue;
6552 found |= note_possible_classname (current_entry,
6553 current_entry_len);
6554 }
6555 }
6556 }
6557 else
6558 {
6559 BUFFER_RESET (filename);
6560 buffer_grow (filename, entry_length + package_length + 4);
6561 strcpy (filename->data, entry_name);
6562 filename->ptr = filename->data + entry_length;
6563 for (k = 0; k < package_length; k++)
6564 {
6565 char ch = package_name[k];
6566 *filename->ptr++ = ch == '.' ? '/' : ch;
6567 }
6568 *filename->ptr = '\0';
6569
6570 dirp = opendir (filename->data);
6571 if (dirp == NULL)
6572 continue;
6573 *filename->ptr++ = '/';
6574 for (;;)
6575 {
6576 int len;
6577 const char *d_name;
6578 struct dirent *direntp = readdir (dirp);
6579 if (!direntp)
6580 break;
6581 d_name = direntp->d_name;
6582 len = strlen (direntp->d_name);
6583 buffer_grow (filename, len+1);
6584 strcpy (filename->ptr, d_name);
6585 found |= note_possible_classname (filename->data + entry_length,
6586 package_length+len+1);
6587 }
6588 if (dirp)
6589 closedir (dirp);
6590 }
6591 }
6592
6593 free (filename->data);
6594
6595 /* Here we should have a unified way of retrieving an entry, to be
6596 indexed. */
6597 if (!found)
6598 {
6599 static int first = 1;
6600 if (first)
6601 {
6602 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6603 java_error_count++;
6604 first = 0;
6605 }
6606 else
6607 parse_error_context (wfl, "Package `%s' not found in import",
6608 package_name);
6609 current_jcf = saved_jcf;
6610 return;
6611 }
6612 current_jcf = saved_jcf;
6613 }
6614
6615 /* Possibly find a type in the import on demands specified
6616 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6617 entire list, to detected potential double definitions. */
6618
6619 static int
6620 find_in_imports_on_demand (class_type)
6621 tree class_type;
6622 {
6623 tree node, import, node_to_use = NULL_TREE;
6624 int seen_once = -1;
6625 tree cl = NULL_TREE;
6626
6627 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6628 {
6629 const char *id_name;
6630 obstack_grow (&temporary_obstack,
6631 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6632 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6633 obstack_1grow (&temporary_obstack, '.');
6634 obstack_grow0 (&temporary_obstack,
6635 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6636 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6637 id_name = obstack_finish (&temporary_obstack);
6638
6639 node = maybe_get_identifier (id_name);
6640 if (node && IS_A_CLASSFILE_NAME (node))
6641 {
6642 if (seen_once < 0)
6643 {
6644 cl = TREE_PURPOSE (import);
6645 seen_once = 1;
6646 node_to_use = node;
6647 }
6648 else
6649 {
6650 seen_once++;
6651 parse_error_context
6652 (TREE_PURPOSE (import),
6653 "Type `%s' also potentially defined in package `%s'",
6654 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6655 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6656 }
6657 }
6658 }
6659
6660 if (seen_once == 1)
6661 {
6662 /* Setup lineno so that it refers to the line of the import (in
6663 case we parse a class file and encounter errors */
6664 tree decl;
6665 int saved_lineno = lineno;
6666 lineno = EXPR_WFL_LINENO (cl);
6667 TYPE_NAME (class_type) = node_to_use;
6668 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6669 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6670 /* If there is no DECL set for the class or if the class isn't
6671 loaded and not seen in source yet, the load */
6672 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6673 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6674 load_class (node_to_use, 0);
6675 lineno = saved_lineno;
6676 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6677 }
6678 else
6679 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6680 }
6681
6682 /* Add package NAME to the list of package encountered so far. To
6683 speed up class lookup in do_resolve_class, we make sure a
6684 particular package is added only once. */
6685
6686 static void
6687 register_package (name)
6688 tree name;
6689 {
6690 static struct hash_table _pht, *pht = NULL;
6691
6692 if (!pht)
6693 {
6694 hash_table_init (&_pht, hash_newfunc,
6695 java_hash_hash_tree_node, java_hash_compare_tree_node);
6696 pht = &_pht;
6697 }
6698
6699 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6700 {
6701 package_list = chainon (package_list, build_tree_list (name, NULL));
6702 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6703 }
6704 }
6705
6706 static tree
6707 resolve_package (pkg, next)
6708 tree pkg, *next;
6709 {
6710 tree current, acc;
6711 tree type_name = NULL_TREE;
6712 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6713
6714 /* The trick is to determine when the package name stops and were
6715 the name of something contained in the package starts. Then we
6716 return a fully qualified name of what we want to get. */
6717
6718 /* Do a quick search on well known package names */
6719 if (!strncmp (name, "java.lang.reflect", 17))
6720 {
6721 *next =
6722 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6723 type_name = lookup_package_type (name, 17);
6724 }
6725 else if (!strncmp (name, "java.lang", 9))
6726 {
6727 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6728 type_name = lookup_package_type (name, 9);
6729 }
6730
6731 /* If we found something here, return */
6732 if (type_name)
6733 return type_name;
6734
6735 *next = EXPR_WFL_QUALIFICATION (pkg);
6736
6737 /* Try to progressively construct a type name */
6738 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6739 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6740 current; current = TREE_CHAIN (current))
6741 {
6742 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6743 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6744 {
6745 type_name = acc;
6746 /* resolve_package should be used in a loop, hence we
6747 point at this one to naturally process the next one at
6748 the next iteration. */
6749 *next = current;
6750 break;
6751 }
6752 }
6753 return type_name;
6754 }
6755
6756 static tree
6757 lookup_package_type (name, from)
6758 const char *name;
6759 int from;
6760 {
6761 char subname [128];
6762 const char *sub = &name[from+1];
6763 while (*sub != '.' && *sub)
6764 sub++;
6765 strncpy (subname, name, sub-name);
6766 subname [sub-name] = '\0';
6767 return get_identifier (subname);
6768 }
6769
6770 static void
6771 check_inner_class_access (decl, enclosing_decl, cl)
6772 tree decl, enclosing_decl, cl;
6773 {
6774 int access = 0;
6775
6776 /* We don't issue an error message when CL is null. CL can be null
6777 as a result of processing a JDEP crafted by source_start_java_method
6778 for the purpose of patching its parm decl. But the error would
6779 have been already trapped when fixing the method's signature.
6780 DECL can also be NULL in case of earlier errors. */
6781 if (!decl || !cl)
6782 return;
6783
6784 /* We grant access to private and protected inner classes if the
6785 location from where we're trying to access DECL is an enclosing
6786 context for DECL or if both have a common enclosing context. */
6787 if (CLASS_PRIVATE (decl))
6788 access = 1;
6789 if (CLASS_PROTECTED (decl))
6790 access = 2;
6791 if (!access)
6792 return;
6793
6794 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6795 TREE_TYPE (decl))
6796 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6797 TREE_TYPE (decl)))
6798 return;
6799
6800 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6801 (access == 1 ? "private" : "protected"),
6802 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6803 lang_printable_name (decl, 0));
6804 }
6805
6806 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6807 access violations were found, 1 otherwise. */
6808
6809 static int
6810 check_pkg_class_access (class_name, cl)
6811 tree class_name;
6812 tree cl;
6813 {
6814 tree type;
6815
6816 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6817 return 0;
6818
6819 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6820 return 0;
6821
6822 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6823 {
6824 /* Access to a private class within the same package is
6825 allowed. */
6826 tree l, r;
6827 breakdown_qualified (&l, &r, class_name);
6828 if (l == ctxp->package)
6829 return 0;
6830
6831 parse_error_context
6832 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6833 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6834 IDENTIFIER_POINTER (class_name));
6835 return 1;
6836 }
6837 return 0;
6838 }
6839
6840 /* Local variable declaration. */
6841
6842 static void
6843 declare_local_variables (modifier, type, vlist)
6844 int modifier;
6845 tree type;
6846 tree vlist;
6847 {
6848 tree decl, current, saved_type;
6849 tree type_wfl = NULL_TREE;
6850 int must_chain = 0;
6851 int final_p = 0;
6852
6853 /* Push a new block if statements were seen between the last time we
6854 pushed a block and now. Keep a cound of block to close */
6855 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6856 {
6857 tree body = GET_CURRENT_BLOCK (current_function_decl);
6858 tree b = enter_block ();
6859 BLOCK_EXPR_ORIGIN (b) = body;
6860 }
6861
6862 if (modifier)
6863 {
6864 int i;
6865 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6866 if (modifier == ACC_FINAL)
6867 final_p = 1;
6868 else
6869 {
6870 parse_error_context
6871 (ctxp->modifier_ctx [i],
6872 "Only `final' is allowed as a local variables modifier");
6873 return;
6874 }
6875 }
6876
6877 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6878 hold the TYPE value if a new incomplete has to be created (as
6879 opposed to being found already existing and reused). */
6880 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6881
6882 /* If TYPE is fully resolved and we don't have a reference, make one */
6883 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6884
6885 /* Go through all the declared variables */
6886 for (current = vlist, saved_type = type; current;
6887 current = TREE_CHAIN (current), type = saved_type)
6888 {
6889 tree other, real_type;
6890 tree wfl = TREE_PURPOSE (current);
6891 tree name = EXPR_WFL_NODE (wfl);
6892 tree init = TREE_VALUE (current);
6893
6894 /* Process NAME, as it may specify extra dimension(s) for it */
6895 type = build_array_from_name (type, type_wfl, name, &name);
6896
6897 /* Variable redefinition check */
6898 if ((other = lookup_name_in_blocks (name)))
6899 {
6900 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6901 DECL_SOURCE_LINE (other));
6902 continue;
6903 }
6904
6905 /* Type adjustment. We may have just readjusted TYPE because
6906 the variable specified more dimensions. Make sure we have
6907 a reference if we can and don't have one already. */
6908 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6909
6910 real_type = GET_REAL_TYPE (type);
6911 /* Never layout this decl. This will be done when its scope
6912 will be entered */
6913 decl = build_decl (VAR_DECL, name, real_type);
6914 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
6915 LOCAL_FINAL (decl) = final_p;
6916 BLOCK_CHAIN_DECL (decl);
6917
6918 /* If doing xreferencing, replace the line number with the WFL
6919 compound value */
6920 if (flag_emit_xref)
6921 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6922
6923 /* Don't try to use an INIT statement when an error was found */
6924 if (init && java_error_count)
6925 init = NULL_TREE;
6926
6927 /* Add the initialization function to the current function's code */
6928 if (init)
6929 {
6930 /* Name might have been readjusted */
6931 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6932 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6933 java_method_add_stmt (current_function_decl,
6934 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6935 init));
6936 }
6937
6938 /* Setup dependency the type of the decl */
6939 if (must_chain)
6940 {
6941 jdep *dep;
6942 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6943 dep = CLASSD_LAST (ctxp->classd_list);
6944 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
6945 }
6946 }
6947 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6948 }
6949
6950 /* Called during parsing. Build decls from argument list. */
6951
6952 static void
6953 source_start_java_method (fndecl)
6954 tree fndecl;
6955 {
6956 tree tem;
6957 tree parm_decl;
6958 int i;
6959
6960 if (!fndecl)
6961 return;
6962
6963 current_function_decl = fndecl;
6964
6965 /* New scope for the function */
6966 enter_block ();
6967 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
6968 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
6969 {
6970 tree type = TREE_VALUE (tem);
6971 tree name = TREE_PURPOSE (tem);
6972
6973 /* If type is incomplete. Create an incomplete decl and ask for
6974 the decl to be patched later */
6975 if (INCOMPLETE_TYPE_P (type))
6976 {
6977 jdep *jdep;
6978 tree real_type = GET_REAL_TYPE (type);
6979 parm_decl = build_decl (PARM_DECL, name, real_type);
6980 type = obtain_incomplete_type (type);
6981 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
6982 jdep = CLASSD_LAST (ctxp->classd_list);
6983 JDEP_MISC (jdep) = name;
6984 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
6985 }
6986 else
6987 parm_decl = build_decl (PARM_DECL, name, type);
6988
6989 /* Remember if a local variable was declared final (via its
6990 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
6991 if (ARG_FINAL_P (tem))
6992 {
6993 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
6994 LOCAL_FINAL (parm_decl) = 1;
6995 }
6996
6997 BLOCK_CHAIN_DECL (parm_decl);
6998 }
6999 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7000 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7001 nreverse (tem);
7002 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7003 DECL_MAX_LOCALS (current_function_decl) = i;
7004 }
7005
7006 /* Called during parsing. Creates an artificial method declaration. */
7007
7008 static tree
7009 create_artificial_method (class, flags, type, name, args)
7010 tree class;
7011 int flags;
7012 tree type, name, args;
7013 {
7014 tree mdecl;
7015
7016 java_parser_context_save_global ();
7017 lineno = 0;
7018 mdecl = make_node (FUNCTION_TYPE);
7019 TREE_TYPE (mdecl) = type;
7020 TYPE_ARG_TYPES (mdecl) = args;
7021 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7022 java_parser_context_restore_global ();
7023 DECL_ARTIFICIAL (mdecl) = 1;
7024 return mdecl;
7025 }
7026
7027 /* Starts the body if an artifical method. */
7028
7029 static void
7030 start_artificial_method_body (mdecl)
7031 tree mdecl;
7032 {
7033 DECL_SOURCE_LINE (mdecl) = 1;
7034 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7035 source_start_java_method (mdecl);
7036 enter_block ();
7037 }
7038
7039 static void
7040 end_artificial_method_body (mdecl)
7041 tree mdecl;
7042 {
7043 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7044 It has to be evaluated first. (if mdecl is current_function_decl,
7045 we have an undefined behavior if no temporary variable is used.) */
7046 tree b = exit_block ();
7047 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7048 exit_block ();
7049 }
7050
7051 /* Called during expansion. Push decls formerly built from argument
7052 list so they're usable during expansion. */
7053
7054 static void
7055 expand_start_java_method (fndecl)
7056 tree fndecl;
7057 {
7058 tree tem, *ptr;
7059
7060 current_function_decl = fndecl;
7061
7062 if (! quiet_flag)
7063 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
7064 announce_function (fndecl);
7065 if (! quiet_flag)
7066 fprintf (stderr, "]");
7067
7068 pushlevel (1); /* Prepare for a parameter push */
7069 ptr = &DECL_ARGUMENTS (fndecl);
7070 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7071 while (tem)
7072 {
7073 tree next = TREE_CHAIN (tem);
7074 tree type = TREE_TYPE (tem);
7075 if (PROMOTE_PROTOTYPES
7076 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7077 && INTEGRAL_TYPE_P (type))
7078 type = integer_type_node;
7079 DECL_ARG_TYPE (tem) = type;
7080 layout_decl (tem, 0);
7081 pushdecl (tem);
7082 *ptr = tem;
7083 ptr = &TREE_CHAIN (tem);
7084 tem = next;
7085 }
7086 *ptr = NULL_TREE;
7087 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7088 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
7089 }
7090
7091 /* Terminate a function and expand its body. */
7092
7093 static void
7094 source_end_java_method ()
7095 {
7096 tree fndecl = current_function_decl;
7097 int flag_asynchronous_exceptions = asynchronous_exceptions;
7098
7099 if (!fndecl)
7100 return;
7101
7102 java_parser_context_save_global ();
7103 lineno = ctxp->last_ccb_indent1;
7104
7105 /* Set EH language codes */
7106 java_set_exception_lang_code ();
7107
7108 /* Turn function bodies with only a NOP expr null, so they don't get
7109 generated at all and we won't get warnings when using the -W
7110 -Wall flags. */
7111 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7112 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7113
7114 /* Generate function's code */
7115 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7116 && ! flag_emit_class_files
7117 && ! flag_emit_xref)
7118 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7119
7120 /* pop out of its parameters */
7121 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7122 poplevel (1, 0, 1);
7123 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7124
7125 /* Generate rtl for function exit. */
7126 if (! flag_emit_class_files && ! flag_emit_xref)
7127 {
7128 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7129 /* Emit catch-finally clauses */
7130 emit_handlers ();
7131 expand_function_end (input_filename, lineno, 0);
7132
7133 /* FIXME: If the current method contains any exception handlers,
7134 force asynchronous_exceptions: this is necessary because signal
7135 handlers in libjava may throw exceptions. This is far from being
7136 a perfect solution, but it's better than doing nothing at all.*/
7137 if (catch_clauses)
7138 asynchronous_exceptions = 1;
7139
7140 /* Run the optimizers and output assembler code for this function. */
7141 rest_of_compilation (fndecl);
7142 }
7143
7144 current_function_decl = NULL_TREE;
7145 java_parser_context_restore_global ();
7146 asynchronous_exceptions = flag_asynchronous_exceptions;
7147 }
7148
7149 /* Record EXPR in the current function block. Complements compound
7150 expression second operand if necessary. */
7151
7152 tree
7153 java_method_add_stmt (fndecl, expr)
7154 tree fndecl, expr;
7155 {
7156 if (!GET_CURRENT_BLOCK (fndecl))
7157 return NULL_TREE;
7158 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7159 }
7160
7161 static tree
7162 add_stmt_to_block (b, type, stmt)
7163 tree b, type, stmt;
7164 {
7165 tree body = BLOCK_EXPR_BODY (b), c;
7166
7167 if (java_error_count)
7168 return body;
7169
7170 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7171 return body;
7172
7173 BLOCK_EXPR_BODY (b) = c;
7174 TREE_SIDE_EFFECTS (c) = 1;
7175 return c;
7176 }
7177
7178 /* Add STMT to EXISTING if possible, otherwise create a new
7179 COMPOUND_EXPR and add STMT to it. */
7180
7181 static tree
7182 add_stmt_to_compound (existing, type, stmt)
7183 tree existing, type, stmt;
7184 {
7185 if (existing)
7186 return build (COMPOUND_EXPR, type, existing, stmt);
7187 else
7188 return stmt;
7189 }
7190
7191 void java_layout_seen_class_methods ()
7192 {
7193 tree previous_list = all_class_list;
7194 tree end = NULL_TREE;
7195 tree current;
7196
7197 while (1)
7198 {
7199 for (current = previous_list;
7200 current != end; current = TREE_CHAIN (current))
7201 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7202
7203 if (previous_list != all_class_list)
7204 {
7205 end = previous_list;
7206 previous_list = all_class_list;
7207 }
7208 else
7209 break;
7210 }
7211 }
7212
7213 void
7214 java_reorder_fields ()
7215 {
7216 static tree stop_reordering = NULL_TREE;
7217 static int initialized_p;
7218 tree current;
7219
7220 /* Register STOP_REORDERING with the garbage collector. */
7221 if (!initialized_p)
7222 {
7223 ggc_add_tree_root (&stop_reordering, 1);
7224 initialized_p = 1;
7225 }
7226
7227 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7228 {
7229 current_class = TREE_TYPE (TREE_VALUE (current));
7230
7231 if (current_class == stop_reordering)
7232 break;
7233
7234 /* Reverse the fields, but leave the dummy field in front.
7235 Fields are already ordered for Object and Class */
7236 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7237 && current_class != class_type_node)
7238 {
7239 /* If the dummy field is there, reverse the right fields and
7240 just layout the type for proper fields offset */
7241 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7242 {
7243 tree fields = TYPE_FIELDS (current_class);
7244 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7245 TYPE_SIZE (current_class) = NULL_TREE;
7246 }
7247 /* We don't have a dummy field, we need to layout the class,
7248 after having reversed the fields */
7249 else
7250 {
7251 TYPE_FIELDS (current_class) =
7252 nreverse (TYPE_FIELDS (current_class));
7253 TYPE_SIZE (current_class) = NULL_TREE;
7254 }
7255 }
7256 }
7257 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7258 }
7259
7260 /* Layout the methods of all classes loaded in one way on an
7261 other. Check methods of source parsed classes. Then reorder the
7262 fields and layout the classes or the type of all source parsed
7263 classes */
7264
7265 void
7266 java_layout_classes ()
7267 {
7268 tree current;
7269 int save_error_count = java_error_count;
7270
7271 /* Layout the methods of all classes seen so far */
7272 java_layout_seen_class_methods ();
7273 java_parse_abort_on_error ();
7274 all_class_list = NULL_TREE;
7275
7276 /* Then check the methods of all parsed classes */
7277 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7278 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7279 java_check_methods (TREE_VALUE (current));
7280 java_parse_abort_on_error ();
7281
7282 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7283 {
7284 current_class = TREE_TYPE (TREE_VALUE (current));
7285 layout_class (current_class);
7286
7287 /* From now on, the class is considered completely loaded */
7288 CLASS_LOADED_P (current_class) = 1;
7289
7290 /* Error reported by the caller */
7291 if (java_error_count)
7292 return;
7293 }
7294
7295 /* We might have reloaded classes durign the process of laying out
7296 classes for code generation. We must layout the methods of those
7297 late additions, as constructor checks might use them */
7298 java_layout_seen_class_methods ();
7299 java_parse_abort_on_error ();
7300 }
7301
7302 /* Expand methods in the current set of classes rememebered for
7303 generation. */
7304
7305 static void
7306 java_complete_expand_classes ()
7307 {
7308 tree current;
7309
7310 do_not_fold = flag_emit_xref;
7311
7312 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7313 if (!INNER_CLASS_DECL_P (current))
7314 java_complete_expand_class (current);
7315 }
7316
7317 /* Expand the methods found in OUTER, starting first by OUTER's inner
7318 classes, if any. */
7319
7320 static void
7321 java_complete_expand_class (outer)
7322 tree outer;
7323 {
7324 tree inner_list;
7325
7326 set_nested_class_simple_name_value (outer, 1); /* Set */
7327
7328 /* We need to go after all inner classes and start expanding them,
7329 starting with most nested ones. We have to do that because nested
7330 classes might add functions to outer classes */
7331
7332 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7333 inner_list; inner_list = TREE_CHAIN (inner_list))
7334 java_complete_expand_class (TREE_PURPOSE (inner_list));
7335
7336 java_complete_expand_methods (outer);
7337 set_nested_class_simple_name_value (outer, 0); /* Reset */
7338 }
7339
7340 /* Expand methods registered in CLASS_DECL. The general idea is that
7341 we expand regular methods first. This allows us get an estimate on
7342 how outer context local alias fields are really used so we can add
7343 to the constructor just enough code to initialize them properly (it
7344 also lets us generate finit$ correctly.) Then we expand the
7345 constructors and then <clinit>. */
7346
7347 static void
7348 java_complete_expand_methods (class_decl)
7349 tree class_decl;
7350 {
7351 tree clinit, finit, decl, first_decl;
7352
7353 current_class = TREE_TYPE (class_decl);
7354
7355 /* Find whether the class has final variables */
7356 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7357 if (FIELD_FINAL (decl))
7358 {
7359 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7360 break;
7361 }
7362
7363 /* Initialize a new constant pool */
7364 init_outgoing_cpool ();
7365
7366 /* Pre-expand <clinit> to figure whether we really need it or
7367 not. If we do need it, we pre-expand the static fields so they're
7368 ready to be used somewhere else. <clinit> will be fully expanded
7369 after we processed the constructors. */
7370 first_decl = TYPE_METHODS (current_class);
7371 clinit = maybe_generate_pre_expand_clinit (current_class);
7372
7373 /* Then generate finit$ (if we need to) because constructor will
7374 try to use it.*/
7375 if (TYPE_FINIT_STMT_LIST (current_class))
7376 {
7377 finit = generate_finit (current_class);
7378 java_complete_expand_method (finit);
7379 }
7380
7381 /* Now do the constructors */
7382 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7383 {
7384 int no_body;
7385
7386 if (!DECL_CONSTRUCTOR_P (decl))
7387 continue;
7388
7389 no_body = !DECL_FUNCTION_BODY (decl);
7390 /* Don't generate debug info on line zero when expanding a
7391 generated constructor. */
7392 if (no_body)
7393 restore_line_number_status (1);
7394
7395 /* Reset the final local variable assignment flags */
7396 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7397 reset_final_variable_local_assignment_flag (current_class);
7398
7399 java_complete_expand_method (decl);
7400
7401 /* Check for missed out final variable assignment */
7402 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7403 check_final_variable_local_assignment_flag (current_class, decl);
7404
7405 if (no_body)
7406 restore_line_number_status (0);
7407 }
7408
7409 /* First, do the ordinary methods. */
7410 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7411 {
7412 /* Skip abstract or native methods -- but do handle native
7413 methods when generating JNI stubs. */
7414 if (METHOD_ABSTRACT (decl)
7415 || (! flag_jni && METHOD_NATIVE (decl))
7416 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7417 continue;
7418
7419 if (METHOD_NATIVE (decl))
7420 {
7421 tree body = build_jni_stub (decl);
7422 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7423 }
7424
7425 java_complete_expand_method (decl);
7426 }
7427
7428 /* If there is indeed a <clinit>, fully expand it now */
7429 if (clinit)
7430 {
7431 /* Reset the final local variable assignment flags */
7432 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7433 reset_static_final_variable_assignment_flag (current_class);
7434 /* Prevent the use of `this' inside <clinit> */
7435 ctxp->explicit_constructor_p = 1;
7436 java_complete_expand_method (clinit);
7437 ctxp->explicit_constructor_p = 0;
7438 /* Check for missed out static final variable assignment */
7439 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7440 && !CLASS_INTERFACE (class_decl))
7441 check_static_final_variable_assignment_flag (current_class);
7442 }
7443
7444 /* We might have generated a class$ that we now want to expand */
7445 if (TYPE_DOT_CLASS (current_class))
7446 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7447
7448 /* Now verify constructor circularity (stop after the first one we
7449 prove wrong.) */
7450 if (!CLASS_INTERFACE (class_decl))
7451 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7452 if (DECL_CONSTRUCTOR_P (decl)
7453 && verify_constructor_circularity (decl, decl))
7454 break;
7455
7456 /* Final check on the initialization of final variables. */
7457 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7458 {
7459 check_final_variable_global_assignment_flag (current_class);
7460 /* If we have an interface, check for uninitialized fields. */
7461 if (CLASS_INTERFACE (class_decl))
7462 check_static_final_variable_assignment_flag (current_class);
7463 }
7464
7465 /* Save the constant pool. We'll need to restore it later. */
7466 TYPE_CPOOL (current_class) = outgoing_cpool;
7467 }
7468
7469 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7470 safely used in some other methods/constructors. */
7471
7472 static tree
7473 maybe_generate_pre_expand_clinit (class_type)
7474 tree class_type;
7475 {
7476 tree current, mdecl;
7477
7478 if (!TYPE_CLINIT_STMT_LIST (class_type))
7479 return NULL_TREE;
7480
7481 /* Go through all static fields and pre expand them */
7482 for (current = TYPE_FIELDS (class_type); current;
7483 current = TREE_CHAIN (current))
7484 if (FIELD_STATIC (current))
7485 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7486
7487 /* Then build the <clinit> method */
7488 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7489 clinit_identifier_node, end_params_node);
7490 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7491 mdecl, NULL_TREE);
7492 start_artificial_method_body (mdecl);
7493
7494 /* We process the list of assignment we produced as the result of
7495 the declaration of initialized static field and add them as
7496 statement to the <clinit> method. */
7497 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7498 current = TREE_CHAIN (current))
7499 {
7500 tree stmt = current;
7501 /* We build the assignment expression that will initialize the
7502 field to its value. There are strict rules on static
7503 initializers (8.5). FIXME */
7504 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7505 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7506 java_method_add_stmt (mdecl, stmt);
7507 }
7508
7509 end_artificial_method_body (mdecl);
7510
7511 /* Now we want to place <clinit> as the last method (because we need
7512 it at least for interface so that it doesn't interfere with the
7513 dispatch table based lookup. */
7514 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7515 {
7516 current = TREE_CHAIN (TYPE_METHODS (class_type));
7517 TYPE_METHODS (class_type) = current;
7518
7519 while (TREE_CHAIN (current))
7520 current = TREE_CHAIN (current);
7521
7522 TREE_CHAIN (current) = mdecl;
7523 TREE_CHAIN (mdecl) = NULL_TREE;
7524 }
7525
7526 return mdecl;
7527 }
7528
7529 /* Analyzes a method body and look for something that isn't a
7530 MODIFY_EXPR. */
7531
7532 static int
7533 analyze_clinit_body (bbody)
7534 tree bbody;
7535 {
7536 while (bbody)
7537 switch (TREE_CODE (bbody))
7538 {
7539 case BLOCK:
7540 bbody = BLOCK_EXPR_BODY (bbody);
7541 break;
7542
7543 case EXPR_WITH_FILE_LOCATION:
7544 bbody = EXPR_WFL_NODE (bbody);
7545 break;
7546
7547 case COMPOUND_EXPR:
7548 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7549 return 1;
7550 bbody = TREE_OPERAND (bbody, 1);
7551 break;
7552
7553 case MODIFY_EXPR:
7554 bbody = NULL_TREE;
7555 break;
7556
7557 default:
7558 bbody = NULL_TREE;
7559 return 1;
7560 }
7561 return 0;
7562 }
7563
7564
7565 /* See whether we could get rid of <clinit>. Criteria are: all static
7566 final fields have constant initial values and the body of <clinit>
7567 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7568
7569 static int
7570 maybe_yank_clinit (mdecl)
7571 tree mdecl;
7572 {
7573 tree type, current;
7574 tree fbody, bbody;
7575 int found = 0;
7576
7577 if (!DECL_CLINIT_P (mdecl))
7578 return 0;
7579
7580 /* If the body isn't empty, then we keep <clinit>. Note that if
7581 we're emitting classfiles, this isn't enough not to rule it
7582 out. */
7583 fbody = DECL_FUNCTION_BODY (mdecl);
7584 bbody = BLOCK_EXPR_BODY (fbody);
7585 if (bbody && bbody != error_mark_node)
7586 bbody = BLOCK_EXPR_BODY (bbody);
7587 else
7588 return 0;
7589 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7590 return 0;
7591
7592 type = DECL_CONTEXT (mdecl);
7593 current = TYPE_FIELDS (type);
7594
7595 for (current = (current ? TREE_CHAIN (current) : current);
7596 current; current = TREE_CHAIN (current))
7597 {
7598 tree f_init;
7599
7600 /* We're not interested in non static field */
7601 if (!FIELD_STATIC (current))
7602 continue;
7603
7604 /* Anything that isn't String or a basic type is ruled out -- or
7605 if we know how to deal with it (when doing things natively) we
7606 should generated an empty <clinit> so that SUID are computed
7607 correctly. */
7608 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7609 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7610 break;
7611
7612 f_init = DECL_INITIAL (current);
7613 /* If we're emitting native code, we want static final fields to
7614 have constant initializers. If we don't meet these
7615 conditions, we keep <clinit> */
7616 if (!flag_emit_class_files
7617 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7618 break;
7619 /* If we're emitting bytecode, we want static fields to have
7620 constant initializers or no initializer. If we don't meet
7621 these conditions, we keep <clinit> */
7622 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7623 break;
7624 }
7625
7626 /* Now we analyze the method body and look for something that
7627 isn't a MODIFY_EXPR */
7628 if (bbody == empty_stmt_node)
7629 found = 0;
7630 else
7631 found = analyze_clinit_body (bbody);
7632
7633 if (current || found)
7634 return 0;
7635
7636 /* Get rid of <clinit> in the class' list of methods */
7637 if (TYPE_METHODS (type) == mdecl)
7638 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7639 else
7640 for (current = TYPE_METHODS (type); current;
7641 current = TREE_CHAIN (current))
7642 if (TREE_CHAIN (current) == mdecl)
7643 {
7644 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7645 break;
7646 }
7647
7648 return 1;
7649 }
7650
7651
7652 /* Complete and expand a method. */
7653
7654 static void
7655 java_complete_expand_method (mdecl)
7656 tree mdecl;
7657 {
7658 int yank_clinit = 0;
7659
7660 current_function_decl = mdecl;
7661 /* Fix constructors before expanding them */
7662 if (DECL_CONSTRUCTOR_P (mdecl))
7663 fix_constructors (mdecl);
7664
7665 /* Expand functions that have a body */
7666 if (DECL_FUNCTION_BODY (mdecl))
7667 {
7668 tree fbody = DECL_FUNCTION_BODY (mdecl);
7669 tree block_body = BLOCK_EXPR_BODY (fbody);
7670 tree exception_copy = NULL_TREE;
7671 expand_start_java_method (mdecl);
7672 build_result_decl (mdecl);
7673
7674 current_this
7675 = (!METHOD_STATIC (mdecl) ?
7676 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7677
7678 /* Purge the `throws' list of unchecked exceptions. If we're
7679 doing xref, save a copy of the list and re-install it
7680 later. */
7681 if (flag_emit_xref)
7682 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7683
7684 purge_unchecked_exceptions (mdecl);
7685
7686 /* Install exceptions thrown with `throws' */
7687 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7688
7689 if (block_body != NULL_TREE)
7690 {
7691 block_body = java_complete_tree (block_body);
7692
7693 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7694 check_for_initialization (block_body);
7695 ctxp->explicit_constructor_p = 0;
7696 }
7697
7698 BLOCK_EXPR_BODY (fbody) = block_body;
7699
7700 /* If we saw a return but couldn't evaluate it properly, we'll
7701 have an error_mark_node here. */
7702 if (block_body != error_mark_node
7703 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7704 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7705 && !flag_emit_xref)
7706 missing_return_error (current_function_decl);
7707
7708 /* Check wether we could just get rid of clinit, now the picture
7709 is complete. */
7710 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7711 complete_start_java_method (mdecl);
7712
7713 /* Don't go any further if we've found error(s) during the
7714 expansion */
7715 if (!java_error_count && !yank_clinit)
7716 source_end_java_method ();
7717 else
7718 {
7719 if (java_error_count)
7720 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7721 poplevel (1, 0, 1);
7722 }
7723
7724 /* Pop the exceptions and sanity check */
7725 POP_EXCEPTIONS();
7726 if (currently_caught_type_list)
7727 abort ();
7728
7729 if (flag_emit_xref)
7730 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7731 }
7732 }
7733
7734 \f
7735
7736 /* This section of the code deals with accessing enclosing context
7737 fields either directly by using the relevant access to this$<n> or
7738 by invoking an access method crafted for that purpose. */
7739
7740 /* Build the necessary access from an inner class to an outer
7741 class. This routine could be optimized to cache previous result
7742 (decl, current_class and returned access). When an access method
7743 needs to be generated, it always takes the form of a read. It might
7744 be later turned into a write by calling outer_field_access_fix. */
7745
7746 static tree
7747 build_outer_field_access (id, decl)
7748 tree id, decl;
7749 {
7750 tree access = NULL_TREE;
7751 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7752 tree decl_ctx = DECL_CONTEXT (decl);
7753
7754 /* If the immediate enclosing context of the current class is the
7755 field decl's class or inherits from it; build the access as
7756 `this$<n>.<field>'. Note that we will break the `private' barrier
7757 if we're not emitting bytecodes. */
7758 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
7759 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7760 {
7761 tree thisn = build_current_thisn (current_class);
7762 access = make_qualified_primary (build_wfl_node (thisn),
7763 id, EXPR_WFL_LINECOL (id));
7764 }
7765 /* Otherwise, generate access methods to outer this and access the
7766 field (either using an access method or by direct access.) */
7767 else
7768 {
7769 int lc = EXPR_WFL_LINECOL (id);
7770
7771 /* Now we chain the required number of calls to the access$0 to
7772 get a hold to the enclosing instance we need, and then we
7773 build the field access. */
7774 access = build_access_to_thisn (current_class, decl_ctx, lc);
7775
7776 /* If the field is private and we're generating bytecode, then
7777 we generate an access method */
7778 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7779 {
7780 tree name = build_outer_field_access_methods (decl);
7781 access = build_outer_field_access_expr (lc, decl_ctx,
7782 name, access, NULL_TREE);
7783 }
7784 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7785 Once again we break the `private' access rule from a foreign
7786 class. */
7787 else
7788 access = make_qualified_primary (access, id, lc);
7789 }
7790 return resolve_expression_name (access, NULL);
7791 }
7792
7793 /* Return a non zero value if NODE describes an outer field inner
7794 access. */
7795
7796 static int
7797 outer_field_access_p (type, decl)
7798 tree type, decl;
7799 {
7800 if (!INNER_CLASS_TYPE_P (type)
7801 || TREE_CODE (decl) != FIELD_DECL
7802 || DECL_CONTEXT (decl) == type)
7803 return 0;
7804
7805 /* If the inner class extends the declaration context of the field
7806 we're try to acces, then this isn't an outer field access */
7807 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7808 return 0;
7809
7810 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7811 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7812 {
7813 if (type == DECL_CONTEXT (decl))
7814 return 1;
7815
7816 if (!DECL_CONTEXT (TYPE_NAME (type)))
7817 {
7818 /* Before we give up, see whether the field is inherited from
7819 the enclosing context we're considering. */
7820 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7821 return 1;
7822 break;
7823 }
7824 }
7825
7826 return 0;
7827 }
7828
7829 /* Return a non zero value if NODE represents an outer field inner
7830 access that was been already expanded. As a side effect, it returns
7831 the name of the field being accessed and the argument passed to the
7832 access function, suitable for a regeneration of the access method
7833 call if necessary. */
7834
7835 static int
7836 outer_field_expanded_access_p (node, name, arg_type, arg)
7837 tree node, *name, *arg_type, *arg;
7838 {
7839 int identified = 0;
7840
7841 if (TREE_CODE (node) != CALL_EXPR)
7842 return 0;
7843
7844 /* Well, gcj generates slightly different tree nodes when compiling
7845 to native or bytecodes. It's the case for function calls. */
7846
7847 if (flag_emit_class_files
7848 && TREE_CODE (node) == CALL_EXPR
7849 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7850 identified = 1;
7851 else if (!flag_emit_class_files)
7852 {
7853 node = TREE_OPERAND (node, 0);
7854
7855 if (node && TREE_OPERAND (node, 0)
7856 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7857 {
7858 node = TREE_OPERAND (node, 0);
7859 if (TREE_OPERAND (node, 0)
7860 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7861 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7862 (DECL_NAME (TREE_OPERAND (node, 0)))))
7863 identified = 1;
7864 }
7865 }
7866
7867 if (identified && name && arg_type && arg)
7868 {
7869 tree argument = TREE_OPERAND (node, 1);
7870 *name = DECL_NAME (TREE_OPERAND (node, 0));
7871 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7872 *arg = TREE_VALUE (argument);
7873 }
7874 return identified;
7875 }
7876
7877 /* Detect in NODE an outer field read access from an inner class and
7878 transform it into a write with RHS as an argument. This function is
7879 called from the java_complete_lhs when an assignment to a LHS can
7880 be identified. */
7881
7882 static tree
7883 outer_field_access_fix (wfl, node, rhs)
7884 tree wfl, node, rhs;
7885 {
7886 tree name, arg_type, arg;
7887
7888 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7889 {
7890 /* At any rate, check whether we're trying to assign a value to
7891 a final. */
7892 tree accessed = (JDECL_P (node) ? node :
7893 (TREE_CODE (node) == COMPONENT_REF ?
7894 TREE_OPERAND (node, 1) : node));
7895 if (check_final_assignment (accessed, wfl))
7896 return error_mark_node;
7897
7898 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7899 arg_type, name, arg, rhs);
7900 return java_complete_tree (node);
7901 }
7902 return NULL_TREE;
7903 }
7904
7905 /* Construct the expression that calls an access method:
7906 <type>.access$<n>(<arg1> [, <arg2>]);
7907
7908 ARG2 can be NULL and will be omitted in that case. It will denote a
7909 read access. */
7910
7911 static tree
7912 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7913 int lc;
7914 tree type, access_method_name, arg1, arg2;
7915 {
7916 tree args, cn, access;
7917
7918 args = arg1 ? arg1 :
7919 build_wfl_node (build_current_thisn (current_class));
7920 args = build_tree_list (NULL_TREE, args);
7921
7922 if (arg2)
7923 args = tree_cons (NULL_TREE, arg2, args);
7924
7925 access = build_method_invocation (build_wfl_node (access_method_name), args);
7926 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7927 return make_qualified_primary (cn, access, lc);
7928 }
7929
7930 static tree
7931 build_new_access_id ()
7932 {
7933 static int access_n_counter = 1;
7934 char buffer [128];
7935
7936 sprintf (buffer, "access$%d", access_n_counter++);
7937 return get_identifier (buffer);
7938 }
7939
7940 /* Create the static access functions for the outer field DECL. We define a
7941 read:
7942 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7943 return inst$.field;
7944 }
7945 and a write access:
7946 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7947 TREE_TYPE (<field>) value$) {
7948 return inst$.field = value$;
7949 }
7950 We should have a usage flags on the DECL so we can lazily turn the ones
7951 we're using for code generation. FIXME.
7952 */
7953
7954 static tree
7955 build_outer_field_access_methods (decl)
7956 tree decl;
7957 {
7958 tree id, args, stmt, mdecl;
7959
7960 if (FIELD_INNER_ACCESS_P (decl))
7961 return FIELD_INNER_ACCESS (decl);
7962
7963 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7964
7965 /* Create the identifier and a function named after it. */
7966 id = build_new_access_id ();
7967
7968 /* The identifier is marked as bearing the name of a generated write
7969 access function for outer field accessed from inner classes. */
7970 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7971
7972 /* Create the read access */
7973 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7974 TREE_CHAIN (args) = end_params_node;
7975 stmt = make_qualified_primary (build_wfl_node (inst_id),
7976 build_wfl_node (DECL_NAME (decl)), 0);
7977 stmt = build_return (0, stmt);
7978 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7979 TREE_TYPE (decl), id, args, stmt);
7980 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7981
7982 /* Create the write access method. No write access for final variable */
7983 if (!FIELD_FINAL (decl))
7984 {
7985 args = build_tree_list (inst_id,
7986 build_pointer_type (DECL_CONTEXT (decl)));
7987 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7988 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7989 stmt = make_qualified_primary (build_wfl_node (inst_id),
7990 build_wfl_node (DECL_NAME (decl)), 0);
7991 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7992 build_wfl_node (wpv_id)));
7993 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7994 TREE_TYPE (decl), id,
7995 args, stmt);
7996 }
7997 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7998
7999 /* Return the access name */
8000 return FIELD_INNER_ACCESS (decl) = id;
8001 }
8002
8003 /* Build an field access method NAME. */
8004
8005 static tree
8006 build_outer_field_access_method (class, type, name, args, body)
8007 tree class, type, name, args, body;
8008 {
8009 tree saved_current_function_decl, mdecl;
8010
8011 /* Create the method */
8012 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8013 fix_method_argument_names (args, mdecl);
8014 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8015
8016 /* Attach the method body. */
8017 saved_current_function_decl = current_function_decl;
8018 start_artificial_method_body (mdecl);
8019 java_method_add_stmt (mdecl, body);
8020 end_artificial_method_body (mdecl);
8021 current_function_decl = saved_current_function_decl;
8022
8023 return mdecl;
8024 }
8025
8026 \f
8027 /* This section deals with building access function necessary for
8028 certain kinds of method invocation from inner classes. */
8029
8030 static tree
8031 build_outer_method_access_method (decl)
8032 tree decl;
8033 {
8034 tree saved_current_function_decl, mdecl;
8035 tree args = NULL_TREE, call_args = NULL_TREE;
8036 tree carg, id, body, class;
8037 char buffer [80];
8038 int parm_id_count = 0;
8039
8040 /* Test this abort with an access to a private field */
8041 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8042 abort ();
8043
8044 /* Check the cache first */
8045 if (DECL_FUNCTION_INNER_ACCESS (decl))
8046 return DECL_FUNCTION_INNER_ACCESS (decl);
8047
8048 class = DECL_CONTEXT (decl);
8049
8050 /* Obtain an access identifier and mark it */
8051 id = build_new_access_id ();
8052 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8053
8054 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8055 /* Create the arguments, as much as the original */
8056 for (; carg && carg != end_params_node;
8057 carg = TREE_CHAIN (carg))
8058 {
8059 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8060 args = chainon (args, build_tree_list (get_identifier (buffer),
8061 TREE_VALUE (carg)));
8062 }
8063 args = chainon (args, end_params_node);
8064
8065 /* Create the method */
8066 mdecl = create_artificial_method (class, ACC_STATIC,
8067 TREE_TYPE (TREE_TYPE (decl)), id, args);
8068 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8069 /* There is a potential bug here. We should be able to use
8070 fix_method_argument_names, but then arg names get mixed up and
8071 eventually a constructor will have its this$0 altered and the
8072 outer context won't be assignment properly. The test case is
8073 stub.java FIXME */
8074 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8075
8076 /* Attach the method body. */
8077 saved_current_function_decl = current_function_decl;
8078 start_artificial_method_body (mdecl);
8079
8080 /* The actual method invocation uses the same args. When invoking a
8081 static methods that way, we don't want to skip the first
8082 argument. */
8083 carg = args;
8084 if (!METHOD_STATIC (decl))
8085 carg = TREE_CHAIN (carg);
8086 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8087 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8088 call_args);
8089
8090 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8091 call_args);
8092 if (!METHOD_STATIC (decl))
8093 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8094 body, 0);
8095 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8096 body = build_return (0, body);
8097 java_method_add_stmt (mdecl,body);
8098 end_artificial_method_body (mdecl);
8099 current_function_decl = saved_current_function_decl;
8100
8101 /* Back tag the access function so it know what it accesses */
8102 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8103
8104 /* Tag the current method so it knows it has an access generated */
8105 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8106 }
8107
8108 \f
8109 /* This section of the code deals with building expressions to access
8110 the enclosing instance of an inner class. The enclosing instance is
8111 kept in a generated field called this$<n>, with <n> being the
8112 inner class nesting level (starting from 0.) */
8113
8114 /* Build an access to a given this$<n>, always chaining access call to
8115 others. Access methods to this$<n> are build on the fly if
8116 necessary. This CAN'T be used to solely access this$<n-1> from
8117 this$<n> (which alway yield to special cases and optimization, see
8118 for example build_outer_field_access). */
8119
8120 static tree
8121 build_access_to_thisn (from, to, lc)
8122 tree from, to;
8123 int lc;
8124 {
8125 tree access = NULL_TREE;
8126
8127 while (from != to)
8128 {
8129 if (!access)
8130 {
8131 access = build_current_thisn (from);
8132 access = build_wfl_node (access);
8133 }
8134 else
8135 {
8136 tree access0_wfl, cn;
8137
8138 maybe_build_thisn_access_method (from);
8139 access0_wfl = build_wfl_node (access0_identifier_node);
8140 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8141 EXPR_WFL_LINECOL (access0_wfl) = lc;
8142 access = build_tree_list (NULL_TREE, access);
8143 access = build_method_invocation (access0_wfl, access);
8144 access = make_qualified_primary (cn, access, lc);
8145 }
8146
8147 /* if FROM isn't an inter class, that's fine, we've done
8148 enough. What we're looking for can be accessed from there. */
8149 from = DECL_CONTEXT (TYPE_NAME (from));
8150 if (!from)
8151 break;
8152 from = TREE_TYPE (from);
8153 }
8154 return access;
8155 }
8156
8157 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8158 is returned if nothing needs to be generated. Otherwise, the method
8159 generated and a method decl is returned.
8160
8161 NOTE: These generated methods should be declared in a class file
8162 attribute so that they can't be referred to directly. */
8163
8164 static tree
8165 maybe_build_thisn_access_method (type)
8166 tree type;
8167 {
8168 tree mdecl, args, stmt, rtype;
8169 tree saved_current_function_decl;
8170
8171 /* If TYPE is a top-level class, no access method is required.
8172 If there already is such an access method, bail out. */
8173 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8174 return NULL_TREE;
8175
8176 /* We generate the method. The method looks like:
8177 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8178 */
8179 args = build_tree_list (inst_id, build_pointer_type (type));
8180 TREE_CHAIN (args) = end_params_node;
8181 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8182 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8183 access0_identifier_node, args);
8184 fix_method_argument_names (args, mdecl);
8185 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8186 stmt = build_current_thisn (type);
8187 stmt = make_qualified_primary (build_wfl_node (inst_id),
8188 build_wfl_node (stmt), 0);
8189 stmt = build_return (0, stmt);
8190
8191 saved_current_function_decl = current_function_decl;
8192 start_artificial_method_body (mdecl);
8193 java_method_add_stmt (mdecl, stmt);
8194 end_artificial_method_body (mdecl);
8195 current_function_decl = saved_current_function_decl;
8196
8197 CLASS_ACCESS0_GENERATED_P (type) = 1;
8198
8199 return mdecl;
8200 }
8201
8202 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8203 the first level of innerclassing. this$1 for the next one, etc...
8204 This function can be invoked with TYPE to NULL, available and then
8205 has to count the parser context. */
8206
8207 static tree
8208 build_current_thisn (type)
8209 tree type;
8210 {
8211 static int saved_i = -1;
8212 static tree saved_thisn = NULL_TREE;
8213 static tree saved_type = NULL_TREE;
8214 static int saved_type_i = 0;
8215 static int initialized_p;
8216 tree decl;
8217 char buffer [80];
8218 int i = 0;
8219
8220 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8221 if (!initialized_p)
8222 {
8223 ggc_add_tree_root (&saved_thisn, 1);
8224 ggc_add_tree_root (&saved_type, 1);
8225 initialized_p = 1;
8226 }
8227
8228 if (type)
8229 {
8230 if (type == saved_type)
8231 i = saved_type_i;
8232 else
8233 {
8234 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8235 decl; decl = DECL_CONTEXT (decl), i++)
8236 ;
8237
8238 saved_type = type;
8239 saved_type_i = i;
8240 }
8241 }
8242 else
8243 i = list_length (GET_CPC_LIST ())-2;
8244
8245 if (i == saved_i)
8246 return saved_thisn;
8247
8248 sprintf (buffer, "this$%d", i);
8249 saved_i = i;
8250 saved_thisn = get_identifier (buffer);
8251 return saved_thisn;
8252 }
8253
8254 /* Return the assignement to the hidden enclosing context `this$<n>'
8255 by the second incoming parameter to the innerclass constructor. The
8256 form used is `this.this$<n> = this$<n>;'. */
8257
8258 static tree
8259 build_thisn_assign ()
8260 {
8261 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8262 {
8263 tree thisn = build_current_thisn (current_class);
8264 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8265 build_wfl_node (thisn), 0);
8266 tree rhs = build_wfl_node (thisn);
8267 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8268 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8269 }
8270 return NULL_TREE;
8271 }
8272
8273 \f
8274 /* Building the synthetic `class$' used to implement the `.class' 1.1
8275 extension for non primitive types. This method looks like:
8276
8277 static Class class$(String type) throws NoClassDefFoundError
8278 {
8279 try {return (java.lang.Class.forName (String));}
8280 catch (ClassNotFoundException e) {
8281 throw new NoClassDefFoundError(e.getMessage());}
8282 } */
8283
8284 static tree
8285 build_dot_class_method (class)
8286 tree class;
8287 {
8288 #define BWF(S) build_wfl_node (get_identifier ((S)))
8289 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8290 tree args, tmp, saved_current_function_decl, mdecl;
8291 tree stmt, throw_stmt, catch, catch_block, try_block;
8292 tree catch_clause_param;
8293 tree class_not_found_exception, no_class_def_found_error;
8294
8295 static tree get_message_wfl, type_parm_wfl;
8296
8297 if (!get_message_wfl)
8298 {
8299 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8300 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8301 ggc_add_tree_root (&get_message_wfl, 1);
8302 ggc_add_tree_root (&type_parm_wfl, 1);
8303 }
8304
8305 /* Build the arguments */
8306 args = build_tree_list (get_identifier ("type$"),
8307 build_pointer_type (string_type_node));
8308 TREE_CHAIN (args) = end_params_node;
8309
8310 /* Build the qualified name java.lang.Class.forName */
8311 tmp = MQN (MQN (MQN (BWF ("java"),
8312 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8313
8314 /* For things we have to catch and throw */
8315 class_not_found_exception =
8316 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8317 no_class_def_found_error =
8318 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8319 load_class (class_not_found_exception, 1);
8320 load_class (no_class_def_found_error, 1);
8321
8322 /* Create the "class$" function */
8323 mdecl = create_artificial_method (class, ACC_STATIC,
8324 build_pointer_type (class_type_node),
8325 classdollar_identifier_node, args);
8326 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8327 no_class_def_found_error);
8328
8329 /* We start by building the try block. We need to build:
8330 return (java.lang.Class.forName (type)); */
8331 stmt = build_method_invocation (tmp,
8332 build_tree_list (NULL_TREE, type_parm_wfl));
8333 stmt = build_return (0, stmt);
8334 /* Put it in a block. That's the try block */
8335 try_block = build_expr_block (stmt, NULL_TREE);
8336
8337 /* Now onto the catch block. We start by building the expression
8338 throwing a new exception:
8339 throw new NoClassDefFoundError (_.getMessage); */
8340 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8341 get_message_wfl, 0);
8342 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8343
8344 /* Build new NoClassDefFoundError (_.getMessage) */
8345 throw_stmt = build_new_invocation
8346 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8347 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8348
8349 /* Build the throw, (it's too early to use BUILD_THROW) */
8350 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8351
8352 /* Build the catch block to encapsulate all this. We begin by
8353 building an decl for the catch clause parameter and link it to
8354 newly created block, the catch block. */
8355 catch_clause_param =
8356 build_decl (VAR_DECL, wpv_id,
8357 build_pointer_type (class_not_found_exception));
8358 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8359
8360 /* We initialize the variable with the exception handler. */
8361 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8362 soft_exceptioninfo_call_node);
8363 add_stmt_to_block (catch_block, NULL_TREE, catch);
8364
8365 /* We add the statement throwing the new exception */
8366 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8367
8368 /* Build a catch expression for all this */
8369 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8370
8371 /* Build the try/catch sequence */
8372 stmt = build_try_statement (0, try_block, catch_block);
8373
8374 fix_method_argument_names (args, mdecl);
8375 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8376 saved_current_function_decl = current_function_decl;
8377 start_artificial_method_body (mdecl);
8378 java_method_add_stmt (mdecl, stmt);
8379 end_artificial_method_body (mdecl);
8380 current_function_decl = saved_current_function_decl;
8381 TYPE_DOT_CLASS (class) = mdecl;
8382
8383 return mdecl;
8384 }
8385
8386 static tree
8387 build_dot_class_method_invocation (type)
8388 tree type;
8389 {
8390 tree sig_id, s;
8391
8392 if (TYPE_ARRAY_P (type))
8393 sig_id = build_java_signature (type);
8394 else
8395 sig_id = DECL_NAME (TYPE_NAME (type));
8396
8397 s = build_string (IDENTIFIER_LENGTH (sig_id),
8398 IDENTIFIER_POINTER (sig_id));
8399 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8400 build_tree_list (NULL_TREE, s));
8401 }
8402
8403 /* This section of the code deals with constructor. */
8404
8405 /* Craft a body for default constructor. Patch existing constructor
8406 bodies with call to super() and field initialization statements if
8407 necessary. */
8408
8409 static void
8410 fix_constructors (mdecl)
8411 tree mdecl;
8412 {
8413 tree body = DECL_FUNCTION_BODY (mdecl);
8414 tree thisn_assign, compound = NULL_TREE;
8415 tree class_type = DECL_CONTEXT (mdecl);
8416
8417 if (!body)
8418 {
8419 /* It is an error for the compiler to generate a default
8420 constructor if the superclass doesn't have a constructor that
8421 takes no argument, or the same args for an anonymous class */
8422 if (verify_constructor_super (mdecl))
8423 {
8424 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8425 tree save = DECL_NAME (mdecl);
8426 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8427 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8428 parse_error_context
8429 (lookup_cl (TYPE_NAME (class_type)),
8430 "No constructor matching `%s' found in class `%s'",
8431 lang_printable_name (mdecl, 0), n);
8432 DECL_NAME (mdecl) = save;
8433 }
8434
8435 /* The constructor body must be crafted by hand. It's the
8436 constructor we defined when we realize we didn't have the
8437 CLASSNAME() constructor */
8438 start_artificial_method_body (mdecl);
8439
8440 /* Insert an assignment to the this$<n> hidden field, if
8441 necessary */
8442 if ((thisn_assign = build_thisn_assign ()))
8443 java_method_add_stmt (mdecl, thisn_assign);
8444
8445 /* We don't generate a super constructor invocation if we're
8446 compiling java.lang.Object. build_super_invocation takes care
8447 of that. */
8448 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8449
8450 /* Insert the instance initializer block right here, after the
8451 super invocation. */
8452 add_instance_initializer (mdecl);
8453
8454 end_artificial_method_body (mdecl);
8455 }
8456 /* Search for an explicit constructor invocation */
8457 else
8458 {
8459 int found = 0;
8460 tree main_block = BLOCK_EXPR_BODY (body);
8461
8462 while (body)
8463 switch (TREE_CODE (body))
8464 {
8465 case CALL_EXPR:
8466 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8467 body = NULL_TREE;
8468 break;
8469 case COMPOUND_EXPR:
8470 case EXPR_WITH_FILE_LOCATION:
8471 body = TREE_OPERAND (body, 0);
8472 break;
8473 case BLOCK:
8474 body = BLOCK_EXPR_BODY (body);
8475 break;
8476 default:
8477 found = 0;
8478 body = NULL_TREE;
8479 }
8480 /* The constructor is missing an invocation of super() */
8481 if (!found)
8482 compound = add_stmt_to_compound (compound, NULL_TREE,
8483 build_super_invocation (mdecl));
8484
8485 /* Generate the assignment to this$<n>, if necessary */
8486 if ((thisn_assign = build_thisn_assign ()))
8487 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8488
8489 /* Insert the instance initializer block right here, after the
8490 super invocation. */
8491 add_instance_initializer (mdecl);
8492
8493 /* Fix the constructor main block if we're adding extra stmts */
8494 if (compound)
8495 {
8496 compound = add_stmt_to_compound (compound, NULL_TREE,
8497 BLOCK_EXPR_BODY (main_block));
8498 BLOCK_EXPR_BODY (main_block) = compound;
8499 }
8500 }
8501 }
8502
8503 /* Browse constructors in the super class, searching for a constructor
8504 that doesn't take any argument. Return 0 if one is found, 1
8505 otherwise. If the current class is an anonymous inner class, look
8506 for something that has the same signature. */
8507
8508 static int
8509 verify_constructor_super (mdecl)
8510 tree mdecl;
8511 {
8512 tree class = CLASSTYPE_SUPER (current_class);
8513 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8514 tree sdecl;
8515
8516 if (!class)
8517 return 0;
8518
8519 if (ANONYMOUS_CLASS_P (current_class))
8520 {
8521 tree mdecl_arg_type;
8522 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8523 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8524 if (DECL_CONSTRUCTOR_P (sdecl))
8525 {
8526 tree m_arg_type;
8527 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8528 if (super_inner)
8529 arg_type = TREE_CHAIN (arg_type);
8530 for (m_arg_type = mdecl_arg_type;
8531 (arg_type != end_params_node
8532 && m_arg_type != end_params_node);
8533 arg_type = TREE_CHAIN (arg_type),
8534 m_arg_type = TREE_CHAIN (m_arg_type))
8535 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8536 break;
8537
8538 if (arg_type == end_params_node && m_arg_type == end_params_node)
8539 return 0;
8540 }
8541 }
8542 else
8543 {
8544 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8545 {
8546 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8547 if (super_inner)
8548 arg = TREE_CHAIN (arg);
8549 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8550 return 0;
8551 }
8552 }
8553 return 1;
8554 }
8555
8556 /* Generate code for all context remembered for code generation. */
8557
8558 void
8559 java_expand_classes ()
8560 {
8561 int save_error_count = 0;
8562 static struct parser_ctxt *saved_ctxp = NULL;
8563
8564 java_parse_abort_on_error ();
8565 if (!(ctxp = ctxp_for_generation))
8566 return;
8567 java_layout_classes ();
8568 java_parse_abort_on_error ();
8569
8570 saved_ctxp = ctxp_for_generation;
8571 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8572 {
8573 ctxp = ctxp_for_generation;
8574 lang_init_source (2); /* Error msgs have method prototypes */
8575 java_complete_expand_classes (); /* Complete and expand classes */
8576 java_parse_abort_on_error ();
8577 }
8578
8579 /* Find anonymous classes and expand their constructor, now they
8580 have been fixed. */
8581 for (ctxp_for_generation = saved_ctxp;
8582 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8583 {
8584 tree current;
8585 ctxp = ctxp_for_generation;
8586 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8587 {
8588 current_class = TREE_TYPE (current);
8589 if (ANONYMOUS_CLASS_P (current_class))
8590 {
8591 tree d;
8592 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8593 {
8594 if (DECL_CONSTRUCTOR_P (d))
8595 {
8596 restore_line_number_status (1);
8597 java_complete_expand_method (d);
8598 restore_line_number_status (0);
8599 break; /* We now there are no other ones */
8600 }
8601 }
8602 }
8603 }
8604 }
8605
8606 /* If we've found error at that stage, don't try to generate
8607 anything, unless we're emitting xrefs or checking the syntax only
8608 (but not using -fsyntax-only for the purpose of generating
8609 bytecode. */
8610 if (java_error_count && !flag_emit_xref
8611 && (!flag_syntax_only && !flag_emit_class_files))
8612 return;
8613
8614 /* Now things are stable, go for generation of the class data. */
8615 for (ctxp_for_generation = saved_ctxp;
8616 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8617 {
8618 tree current;
8619 ctxp = ctxp_for_generation;
8620 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8621 {
8622 current_class = TREE_TYPE (current);
8623 outgoing_cpool = TYPE_CPOOL (current_class);
8624 if (flag_emit_class_files)
8625 write_classfile (current_class);
8626 if (flag_emit_xref)
8627 expand_xref (current_class);
8628 else if (! flag_syntax_only)
8629 finish_class ();
8630 }
8631 }
8632 }
8633
8634 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8635 a tree list node containing RIGHT. Fore coming RIGHTs will be
8636 chained to this hook. LOCATION contains the location of the
8637 separating `.' operator. */
8638
8639 static tree
8640 make_qualified_primary (primary, right, location)
8641 tree primary, right;
8642 int location;
8643 {
8644 tree wfl;
8645
8646 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8647 wfl = build_wfl_wrap (primary, location);
8648 else
8649 {
8650 wfl = primary;
8651 /* If wfl wasn't qualified, we build a first anchor */
8652 if (!EXPR_WFL_QUALIFICATION (wfl))
8653 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8654 }
8655
8656 /* And chain them */
8657 EXPR_WFL_LINECOL (right) = location;
8658 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8659 PRIMARY_P (wfl) = 1;
8660 return wfl;
8661 }
8662
8663 /* Simple merge of two name separated by a `.' */
8664
8665 static tree
8666 merge_qualified_name (left, right)
8667 tree left, right;
8668 {
8669 tree node;
8670 if (!left && !right)
8671 return NULL_TREE;
8672
8673 if (!left)
8674 return right;
8675
8676 if (!right)
8677 return left;
8678
8679 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8680 IDENTIFIER_LENGTH (left));
8681 obstack_1grow (&temporary_obstack, '.');
8682 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8683 IDENTIFIER_LENGTH (right));
8684 node = get_identifier (obstack_base (&temporary_obstack));
8685 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8686 QUALIFIED_P (node) = 1;
8687 return node;
8688 }
8689
8690 /* Merge the two parts of a qualified name into LEFT. Set the
8691 location information of the resulting node to LOCATION, usually
8692 inherited from the location information of the `.' operator. */
8693
8694 static tree
8695 make_qualified_name (left, right, location)
8696 tree left, right;
8697 int location;
8698 {
8699 #ifdef USE_COMPONENT_REF
8700 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8701 EXPR_WFL_LINECOL (node) = location;
8702 return node;
8703 #else
8704 tree left_id = EXPR_WFL_NODE (left);
8705 tree right_id = EXPR_WFL_NODE (right);
8706 tree wfl, merge;
8707
8708 merge = merge_qualified_name (left_id, right_id);
8709
8710 /* Left wasn't qualified and is now qualified */
8711 if (!QUALIFIED_P (left_id))
8712 {
8713 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8714 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8715 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8716 }
8717
8718 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8719 EXPR_WFL_LINECOL (wfl) = location;
8720 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8721
8722 EXPR_WFL_NODE (left) = merge;
8723 return left;
8724 #endif
8725 }
8726
8727 /* Extract the last identifier component of the qualified in WFL. The
8728 last identifier is removed from the linked list */
8729
8730 static tree
8731 cut_identifier_in_qualified (wfl)
8732 tree wfl;
8733 {
8734 tree q;
8735 tree previous = NULL_TREE;
8736 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8737 if (!TREE_CHAIN (q))
8738 {
8739 if (!previous)
8740 /* Operating on a non qualified qualified WFL. */
8741 abort ();
8742
8743 TREE_CHAIN (previous) = NULL_TREE;
8744 return TREE_PURPOSE (q);
8745 }
8746 }
8747
8748 /* Resolve the expression name NAME. Return its decl. */
8749
8750 static tree
8751 resolve_expression_name (id, orig)
8752 tree id;
8753 tree *orig;
8754 {
8755 tree name = EXPR_WFL_NODE (id);
8756 tree decl;
8757
8758 /* 6.5.5.1: Simple expression names */
8759 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8760 {
8761 /* 15.13.1: NAME can appear within the scope of a local variable
8762 declaration */
8763 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8764 return decl;
8765
8766 /* 15.13.1: NAME can appear within a class declaration */
8767 else
8768 {
8769 decl = lookup_field_wrapper (current_class, name);
8770 if (decl)
8771 {
8772 tree access = NULL_TREE;
8773 int fs = FIELD_STATIC (decl);
8774
8775 /* If we're accessing an outer scope local alias, make
8776 sure we change the name of the field we're going to
8777 build access to. */
8778 if (FIELD_LOCAL_ALIAS_USED (decl))
8779 name = DECL_NAME (decl);
8780
8781 /* Instance variable (8.3.1.1) can't appear within
8782 static method, static initializer or initializer for
8783 a static variable. */
8784 if (!fs && METHOD_STATIC (current_function_decl))
8785 {
8786 static_ref_err (id, name, current_class);
8787 return error_mark_node;
8788 }
8789 /* Instance variables can't appear as an argument of
8790 an explicit constructor invocation */
8791 if (!fs && ctxp->explicit_constructor_p
8792 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
8793 {
8794 parse_error_context
8795 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8796 return error_mark_node;
8797 }
8798
8799 /* If we're processing an inner class and we're trying
8800 to access a field belonging to an outer class, build
8801 the access to the field */
8802 if (!fs && outer_field_access_p (current_class, decl))
8803 {
8804 if (CLASS_STATIC (TYPE_NAME (current_class)))
8805 {
8806 static_ref_err (id, DECL_NAME (decl), current_class);
8807 return error_mark_node;
8808 }
8809 return build_outer_field_access (id, decl);
8810 }
8811
8812 /* Otherwise build what it takes to access the field */
8813 access = build_field_ref ((fs ? NULL_TREE : current_this),
8814 DECL_CONTEXT (decl), name);
8815 if (fs && !flag_emit_class_files && !flag_emit_xref)
8816 access = build_class_init (DECL_CONTEXT (access), access);
8817 /* We may be asked to save the real field access node */
8818 if (orig)
8819 *orig = access;
8820 /* And we return what we got */
8821 return access;
8822 }
8823 /* Fall down to error report on undefined variable */
8824 }
8825 }
8826 /* 6.5.5.2 Qualified Expression Names */
8827 else
8828 {
8829 if (orig)
8830 *orig = NULL_TREE;
8831 qualify_ambiguous_name (id);
8832 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8833 /* 15.10.2: Accessing Superclass Members using super */
8834 return resolve_field_access (id, orig, NULL);
8835 }
8836
8837 /* We've got an error here */
8838 parse_error_context (id, "Undefined variable `%s'",
8839 IDENTIFIER_POINTER (name));
8840
8841 return error_mark_node;
8842 }
8843
8844 static void
8845 static_ref_err (wfl, field_id, class_type)
8846 tree wfl, field_id, class_type;
8847 {
8848 parse_error_context
8849 (wfl,
8850 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8851 IDENTIFIER_POINTER (field_id),
8852 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8853 }
8854
8855 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8856 We return something suitable to generate the field access. We also
8857 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8858 recipient's address can be null. */
8859
8860 static tree
8861 resolve_field_access (qual_wfl, field_decl, field_type)
8862 tree qual_wfl;
8863 tree *field_decl, *field_type;
8864 {
8865 int is_static = 0;
8866 tree field_ref;
8867 tree decl, where_found, type_found;
8868
8869 if (resolve_qualified_expression_name (qual_wfl, &decl,
8870 &where_found, &type_found))
8871 return error_mark_node;
8872
8873 /* Resolve the LENGTH field of an array here */
8874 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8875 && type_found && TYPE_ARRAY_P (type_found)
8876 && ! flag_emit_class_files && ! flag_emit_xref)
8877 {
8878 tree length = build_java_array_length_access (where_found);
8879 field_ref =
8880 build_java_arraynull_check (type_found, length, int_type_node);
8881
8882 /* In case we're dealing with a static array, we need to
8883 initialize its class before the array length can be fetched.
8884 It's also a good time to create a DECL_RTL for the field if
8885 none already exists, otherwise if the field was declared in a
8886 class found in an external file and hasn't been (and won't
8887 be) accessed for its value, none will be created. */
8888 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8889 {
8890 build_static_field_ref (where_found);
8891 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8892 }
8893 }
8894 /* We might have been trying to resolve field.method(). In which
8895 case, the resolution is over and decl is the answer */
8896 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8897 field_ref = decl;
8898 else if (JDECL_P (decl))
8899 {
8900 int static_final_found = 0;
8901 if (!type_found)
8902 type_found = DECL_CONTEXT (decl);
8903 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8904 if (CLASS_FINAL_VARIABLE_P (decl)
8905 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8906 && DECL_INITIAL (decl))
8907 {
8908 /* When called on a FIELD_DECL of the right (primitive)
8909 type, java_complete_tree will try to substitue the decl
8910 for it's initial value. */
8911 field_ref = java_complete_tree (decl);
8912 static_final_found = 1;
8913 }
8914 else
8915 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8916 NULL_TREE : where_found),
8917 type_found, DECL_NAME (decl));
8918 if (field_ref == error_mark_node)
8919 return error_mark_node;
8920 if (is_static && !static_final_found
8921 && !flag_emit_class_files && !flag_emit_xref)
8922 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8923 }
8924 else
8925 field_ref = decl;
8926
8927 if (field_decl)
8928 *field_decl = decl;
8929 if (field_type)
8930 *field_type = (QUAL_DECL_TYPE (decl) ?
8931 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8932 return field_ref;
8933 }
8934
8935 /* If NODE is an access to f static field, strip out the class
8936 initialization part and return the field decl, otherwise, return
8937 NODE. */
8938
8939 static tree
8940 strip_out_static_field_access_decl (node)
8941 tree node;
8942 {
8943 if (TREE_CODE (node) == COMPOUND_EXPR)
8944 {
8945 tree op1 = TREE_OPERAND (node, 1);
8946 if (TREE_CODE (op1) == COMPOUND_EXPR)
8947 {
8948 tree call = TREE_OPERAND (op1, 0);
8949 if (TREE_CODE (call) == CALL_EXPR
8950 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8951 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8952 == soft_initclass_node)
8953 return TREE_OPERAND (op1, 1);
8954 }
8955 else if (JDECL_P (op1))
8956 return op1;
8957 }
8958 return node;
8959 }
8960
8961 /* 6.5.5.2: Qualified Expression Names */
8962
8963 static int
8964 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8965 tree wfl;
8966 tree *found_decl, *type_found, *where_found;
8967 {
8968 int from_type = 0; /* Field search initiated from a type */
8969 int from_super = 0, from_cast = 0, from_qualified_this = 0;
8970 int previous_call_static = 0;
8971 int is_static;
8972 tree decl = NULL_TREE, type = NULL_TREE, q;
8973 /* For certain for of inner class instantiation */
8974 tree saved_current, saved_this;
8975 #define RESTORE_THIS_AND_CURRENT_CLASS \
8976 { current_class = saved_current; current_this = saved_this;}
8977
8978 *type_found = *where_found = NULL_TREE;
8979
8980 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8981 {
8982 tree qual_wfl = QUAL_WFL (q);
8983 tree ret_decl; /* for EH checking */
8984 int location; /* for EH checking */
8985
8986 /* 15.10.1 Field Access Using a Primary */
8987 switch (TREE_CODE (qual_wfl))
8988 {
8989 case CALL_EXPR:
8990 case NEW_CLASS_EXPR:
8991 /* If the access to the function call is a non static field,
8992 build the code to access it. */
8993 if (JDECL_P (decl) && !FIELD_STATIC (decl))
8994 {
8995 decl = maybe_access_field (decl, *where_found,
8996 DECL_CONTEXT (decl));
8997 if (decl == error_mark_node)
8998 return 1;
8999 }
9000
9001 /* And code for the function call */
9002 if (complete_function_arguments (qual_wfl))
9003 return 1;
9004
9005 /* We might have to setup a new current class and a new this
9006 for the search of an inner class, relative to the type of
9007 a expression resolved as `decl'. The current values are
9008 saved and restored shortly after */
9009 saved_current = current_class;
9010 saved_this = current_this;
9011 if (decl
9012 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9013 || from_qualified_this))
9014 {
9015 /* If we still have `from_qualified_this', we have the form
9016 <T>.this.f() and we need to build <T>.this */
9017 if (from_qualified_this)
9018 {
9019 decl = build_access_to_thisn (current_class, type, 0);
9020 decl = java_complete_tree (decl);
9021 type = TREE_TYPE (TREE_TYPE (decl));
9022 }
9023 current_class = type;
9024 current_this = decl;
9025 from_qualified_this = 0;
9026 }
9027
9028 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9029 CALL_USING_SUPER (qual_wfl) = 1;
9030 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9031 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9032 *where_found = patch_method_invocation (qual_wfl, decl, type,
9033 from_super,
9034 &is_static, &ret_decl);
9035 if (*where_found == error_mark_node)
9036 {
9037 RESTORE_THIS_AND_CURRENT_CLASS;
9038 return 1;
9039 }
9040 *type_found = type = QUAL_DECL_TYPE (*where_found);
9041
9042 /* If we're creating an inner class instance, check for that
9043 an enclosing instance is in scope */
9044 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9045 && INNER_ENCLOSING_SCOPE_CHECK (type))
9046 {
9047 parse_error_context
9048 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9049 lang_printable_name (type, 0),
9050 (!current_this ? "" :
9051 "; an explicit one must be provided when creating this inner class"));
9052 RESTORE_THIS_AND_CURRENT_CLASS;
9053 return 1;
9054 }
9055
9056 /* In case we had to change then to resolve a inner class
9057 instantiation using a primary qualified by a `new' */
9058 RESTORE_THIS_AND_CURRENT_CLASS;
9059
9060 /* EH check. No check on access$<n> functions */
9061 if (location
9062 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9063 (DECL_NAME (current_function_decl)))
9064 check_thrown_exceptions (location, ret_decl);
9065
9066 /* If the previous call was static and this one is too,
9067 build a compound expression to hold the two (because in
9068 that case, previous function calls aren't transported as
9069 forcoming function's argument. */
9070 if (previous_call_static && is_static)
9071 {
9072 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9073 TREE_SIDE_EFFECTS (decl) = 1;
9074 }
9075 else
9076 {
9077 previous_call_static = is_static;
9078 decl = *where_found;
9079 }
9080 from_type = 0;
9081 continue;
9082
9083 case NEW_ARRAY_EXPR:
9084 case NEW_ANONYMOUS_ARRAY_EXPR:
9085 *where_found = decl = java_complete_tree (qual_wfl);
9086 if (decl == error_mark_node)
9087 return 1;
9088 *type_found = type = QUAL_DECL_TYPE (decl);
9089 CLASS_LOADED_P (type) = 1;
9090 continue;
9091
9092 case CONVERT_EXPR:
9093 *where_found = decl = java_complete_tree (qual_wfl);
9094 if (decl == error_mark_node)
9095 return 1;
9096 *type_found = type = QUAL_DECL_TYPE (decl);
9097 from_cast = 1;
9098 continue;
9099
9100 case CONDITIONAL_EXPR:
9101 case STRING_CST:
9102 case MODIFY_EXPR:
9103 *where_found = decl = java_complete_tree (qual_wfl);
9104 if (decl == error_mark_node)
9105 return 1;
9106 *type_found = type = QUAL_DECL_TYPE (decl);
9107 continue;
9108
9109 case ARRAY_REF:
9110 /* If the access to the function call is a non static field,
9111 build the code to access it. */
9112 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9113 {
9114 decl = maybe_access_field (decl, *where_found, type);
9115 if (decl == error_mark_node)
9116 return 1;
9117 }
9118 /* And code for the array reference expression */
9119 decl = java_complete_tree (qual_wfl);
9120 if (decl == error_mark_node)
9121 return 1;
9122 type = QUAL_DECL_TYPE (decl);
9123 continue;
9124
9125 case PLUS_EXPR:
9126 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9127 return 1;
9128 if ((type = patch_string (decl)))
9129 decl = type;
9130 *where_found = QUAL_RESOLUTION (q) = decl;
9131 *type_found = type = TREE_TYPE (decl);
9132 break;
9133
9134 case CLASS_LITERAL:
9135 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9136 return 1;
9137 *where_found = QUAL_RESOLUTION (q) = decl;
9138 *type_found = type = TREE_TYPE (decl);
9139 break;
9140
9141 default:
9142 /* Fix for -Wall Just go to the next statement. Don't
9143 continue */
9144 break;
9145 }
9146
9147 /* If we fall here, we weren't processing a (static) function call. */
9148 previous_call_static = 0;
9149
9150 /* It can be the keyword THIS */
9151 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9152 {
9153 if (!current_this)
9154 {
9155 parse_error_context
9156 (wfl, "Keyword `this' used outside allowed context");
9157 return 1;
9158 }
9159 if (ctxp->explicit_constructor_p
9160 && type == current_class)
9161 {
9162 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9163 return 1;
9164 }
9165 /* We have to generate code for intermediate acess */
9166 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9167 {
9168 *where_found = decl = current_this;
9169 *type_found = type = QUAL_DECL_TYPE (decl);
9170 }
9171 /* We're trying to access the this from somewhere else. Make sure
9172 it's allowed before doing so. */
9173 else
9174 {
9175 if (!enclosing_context_p (type, current_class))
9176 {
9177 char *p = xstrdup (lang_printable_name (type, 0));
9178 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9179 p, p,
9180 lang_printable_name (current_class, 0));
9181 free (p);
9182 return 1;
9183 }
9184 from_qualified_this = 1;
9185 /* If there's nothing else after that, we need to
9186 produce something now, otherwise, the section of the
9187 code that needs to produce <T>.this will generate
9188 what is necessary. */
9189 if (!TREE_CHAIN (q))
9190 {
9191 decl = build_access_to_thisn (current_class, type, 0);
9192 *where_found = decl = java_complete_tree (decl);
9193 *type_found = type = TREE_TYPE (decl);
9194 }
9195 }
9196
9197 from_type = 0;
9198 continue;
9199 }
9200
9201 /* 15.10.2 Accessing Superclass Members using SUPER */
9202 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9203 {
9204 tree node;
9205 /* Check on the restricted use of SUPER */
9206 if (METHOD_STATIC (current_function_decl)
9207 || current_class == object_type_node)
9208 {
9209 parse_error_context
9210 (wfl, "Keyword `super' used outside allowed context");
9211 return 1;
9212 }
9213 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9214 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9215 CLASSTYPE_SUPER (current_class),
9216 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9217 *where_found = decl = java_complete_tree (node);
9218 if (decl == error_mark_node)
9219 return 1;
9220 *type_found = type = QUAL_DECL_TYPE (decl);
9221 from_super = from_type = 1;
9222 continue;
9223 }
9224
9225 /* 15.13.1: Can't search for field name in packages, so we
9226 assume a variable/class name was meant. */
9227 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9228 {
9229 tree name = resolve_package (wfl, &q);
9230 if (name)
9231 {
9232 tree list;
9233 *where_found = decl = resolve_no_layout (name, qual_wfl);
9234 /* We want to be absolutely sure that the class is laid
9235 out. We're going to search something inside it. */
9236 *type_found = type = TREE_TYPE (decl);
9237 layout_class (type);
9238 from_type = 1;
9239
9240 /* Fix them all the way down, if any are left. */
9241 if (q)
9242 {
9243 list = TREE_CHAIN (q);
9244 while (list)
9245 {
9246 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9247 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9248 list = TREE_CHAIN (list);
9249 }
9250 }
9251 }
9252 else
9253 {
9254 if (from_super || from_cast)
9255 parse_error_context
9256 ((from_cast ? qual_wfl : wfl),
9257 "No variable `%s' defined in class `%s'",
9258 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9259 lang_printable_name (type, 0));
9260 else
9261 parse_error_context
9262 (qual_wfl, "Undefined variable or class name: `%s'",
9263 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9264 return 1;
9265 }
9266 }
9267
9268 /* We have a type name. It's been already resolved when the
9269 expression was qualified. */
9270 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9271 {
9272 if (!(decl = QUAL_RESOLUTION (q)))
9273 return 1; /* Error reported already */
9274
9275 /* Sneak preview. If next we see a `new', we're facing a
9276 qualification with resulted in a type being selected
9277 instead of a field. Report the error */
9278 if(TREE_CHAIN (q)
9279 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9280 {
9281 parse_error_context (qual_wfl, "Undefined variable `%s'",
9282 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9283 return 1;
9284 }
9285
9286 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9287 {
9288 parse_error_context
9289 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9290 java_accstring_lookup (get_access_flags_from_decl (decl)),
9291 GET_TYPE_NAME (type),
9292 IDENTIFIER_POINTER (DECL_NAME (decl)),
9293 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9294 return 1;
9295 }
9296 check_deprecation (qual_wfl, decl);
9297
9298 type = TREE_TYPE (decl);
9299 from_type = 1;
9300 }
9301 /* We resolve and expression name */
9302 else
9303 {
9304 tree field_decl = NULL_TREE;
9305
9306 /* If there exists an early resolution, use it. That occurs
9307 only once and we know that there are more things to
9308 come. Don't do that when processing something after SUPER
9309 (we need more thing to be put in place below */
9310 if (!from_super && QUAL_RESOLUTION (q))
9311 {
9312 decl = QUAL_RESOLUTION (q);
9313 if (!type)
9314 {
9315 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9316 {
9317 if (current_this)
9318 *where_found = current_this;
9319 else
9320 {
9321 static_ref_err (qual_wfl, DECL_NAME (decl),
9322 current_class);
9323 return 1;
9324 }
9325 if (outer_field_access_p (current_class, decl))
9326 decl = build_outer_field_access (qual_wfl, decl);
9327 }
9328 else
9329 {
9330 *where_found = TREE_TYPE (decl);
9331 if (TREE_CODE (*where_found) == POINTER_TYPE)
9332 *where_found = TREE_TYPE (*where_found);
9333 }
9334 }
9335 }
9336
9337 /* We have to search for a field, knowing the type of its
9338 container. The flag FROM_TYPE indicates that we resolved
9339 the last member of the expression as a type name, which
9340 means that for the resolution of this field, we'll look
9341 for other errors than if it was resolved as a member of
9342 an other field. */
9343 else
9344 {
9345 int is_static;
9346 tree field_decl_type; /* For layout */
9347
9348 if (!from_type && !JREFERENCE_TYPE_P (type))
9349 {
9350 parse_error_context
9351 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9352 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9353 lang_printable_name (type, 0),
9354 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9355 return 1;
9356 }
9357
9358 field_decl = lookup_field_wrapper (type,
9359 EXPR_WFL_NODE (qual_wfl));
9360
9361 /* Maybe what we're trying to access to is an inner
9362 class, only if decl is a TYPE_DECL. */
9363 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9364 {
9365 tree ptr, inner_decl;
9366
9367 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9368 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9369 if (inner_decl)
9370 {
9371 check_inner_class_access (inner_decl, decl, qual_wfl);
9372 type = TREE_TYPE (inner_decl);
9373 decl = inner_decl;
9374 from_type = 1;
9375 continue;
9376 }
9377 }
9378
9379 if (field_decl == NULL_TREE)
9380 {
9381 parse_error_context
9382 (qual_wfl, "No variable `%s' defined in type `%s'",
9383 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9384 GET_TYPE_NAME (type));
9385 return 1;
9386 }
9387 if (field_decl == error_mark_node)
9388 return 1;
9389
9390 /* Layout the type of field_decl, since we may need
9391 it. Don't do primitive types or loaded classes. The
9392 situation of non primitive arrays may not handled
9393 properly here. FIXME */
9394 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9395 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9396 else
9397 field_decl_type = TREE_TYPE (field_decl);
9398 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9399 && !CLASS_LOADED_P (field_decl_type)
9400 && !TYPE_ARRAY_P (field_decl_type))
9401 resolve_and_layout (field_decl_type, NULL_TREE);
9402 if (TYPE_ARRAY_P (field_decl_type))
9403 CLASS_LOADED_P (field_decl_type) = 1;
9404
9405 /* Check on accessibility here */
9406 if (not_accessible_p (current_class, field_decl,
9407 TREE_TYPE (decl), from_super))
9408 {
9409 parse_error_context
9410 (qual_wfl,
9411 "Can't access %s field `%s.%s' from `%s'",
9412 java_accstring_lookup
9413 (get_access_flags_from_decl (field_decl)),
9414 GET_TYPE_NAME (type),
9415 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9416 IDENTIFIER_POINTER
9417 (DECL_NAME (TYPE_NAME (current_class))));
9418 return 1;
9419 }
9420 check_deprecation (qual_wfl, field_decl);
9421
9422 /* There are things to check when fields are accessed
9423 from type. There are no restrictions on a static
9424 declaration of the field when it is accessed from an
9425 interface */
9426 is_static = FIELD_STATIC (field_decl);
9427 if (!from_super && from_type
9428 && !TYPE_INTERFACE_P (type)
9429 && !is_static
9430 && (current_function_decl
9431 && METHOD_STATIC (current_function_decl)))
9432 {
9433 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9434 return 1;
9435 }
9436 from_cast = from_super = 0;
9437
9438 /* It's an access from a type but it isn't static, we
9439 make it relative to `this'. */
9440 if (!is_static && from_type)
9441 decl = current_this;
9442
9443 /* If we need to generate something to get a proper
9444 handle on what this field is accessed from, do it
9445 now. */
9446 if (!is_static)
9447 {
9448 decl = maybe_access_field (decl, *where_found, *type_found);
9449 if (decl == error_mark_node)
9450 return 1;
9451 }
9452
9453 /* We want to keep the location were found it, and the type
9454 we found. */
9455 *where_found = decl;
9456 *type_found = type;
9457
9458 /* Generate the correct expression for field access from
9459 qualified this */
9460 if (from_qualified_this)
9461 {
9462 field_decl = build_outer_field_access (qual_wfl, field_decl);
9463 from_qualified_this = 0;
9464 }
9465
9466 /* This is the decl found and eventually the next one to
9467 search from */
9468 decl = field_decl;
9469 }
9470 from_type = 0;
9471 type = QUAL_DECL_TYPE (decl);
9472
9473 /* Sneak preview. If decl is qualified by a `new', report
9474 the error here to be accurate on the peculiar construct */
9475 if (TREE_CHAIN (q)
9476 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9477 && !JREFERENCE_TYPE_P (type))
9478 {
9479 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9480 lang_printable_name (type, 0));
9481 return 1;
9482 }
9483 }
9484 /* `q' might have changed due to a after package resolution
9485 re-qualification */
9486 if (!q)
9487 break;
9488 }
9489 *found_decl = decl;
9490 return 0;
9491 }
9492
9493 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9494 can't be accessed from REFERENCE (a record type). If MEMBER
9495 features a protected access, we then use WHERE which, if non null,
9496 holds the type of MEMBER's access that is checked against
9497 6.6.2.1. This function should be used when decl is a field or a
9498 method. */
9499
9500 static int
9501 not_accessible_p (reference, member, where, from_super)
9502 tree reference, member;
9503 tree where;
9504 int from_super;
9505 {
9506 int access_flag = get_access_flags_from_decl (member);
9507
9508 /* Inner classes are processed by check_inner_class_access */
9509 if (INNER_CLASS_TYPE_P (reference))
9510 return 0;
9511
9512 /* Access always granted for members declared public */
9513 if (access_flag & ACC_PUBLIC)
9514 return 0;
9515
9516 /* Check access on protected members */
9517 if (access_flag & ACC_PROTECTED)
9518 {
9519 /* Access granted if it occurs from within the package
9520 containing the class in which the protected member is
9521 declared */
9522 if (class_in_current_package (DECL_CONTEXT (member)))
9523 return 0;
9524
9525 /* If accessed with the form `super.member', then access is granted */
9526 if (from_super)
9527 return 0;
9528
9529 /* If where is active, access was made through a
9530 qualifier. Access is granted if the type of the qualifier is
9531 or is a sublass of the type the access made from (6.6.2.1.) */
9532 if (where && !inherits_from_p (where, reference))
9533 return 1;
9534
9535 /* Otherwise, access is granted if occuring from the class where
9536 member is declared or a subclass of it. Find the right
9537 context to perform the check */
9538 if (PURE_INNER_CLASS_TYPE_P (reference))
9539 {
9540 while (INNER_CLASS_TYPE_P (reference))
9541 {
9542 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9543 return 0;
9544 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9545 }
9546 }
9547 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9548 return 0;
9549 return 1;
9550 }
9551
9552 /* Check access on private members. Access is granted only if it
9553 occurs from within the class in which it is declared -- that does
9554 it for innerclasses too. */
9555 if (access_flag & ACC_PRIVATE)
9556 {
9557 if (reference == DECL_CONTEXT (member))
9558 return 0;
9559 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9560 return 0;
9561 return 1;
9562 }
9563
9564 /* Default access are permitted only when occuring within the
9565 package in which the type (REFERENCE) is declared. In other words,
9566 REFERENCE is defined in the current package */
9567 if (ctxp->package)
9568 return !class_in_current_package (reference);
9569
9570 /* Otherwise, access is granted */
9571 return 0;
9572 }
9573
9574 /* Test deprecated decl access. */
9575 static void
9576 check_deprecation (wfl, decl)
9577 tree wfl, decl;
9578 {
9579 const char *file = DECL_SOURCE_FILE (decl);
9580 /* Complain if the field is deprecated and the file it was defined
9581 in isn't compiled at the same time the file which contains its
9582 use is */
9583 if (DECL_DEPRECATED (decl)
9584 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9585 {
9586 char the [20];
9587 switch (TREE_CODE (decl))
9588 {
9589 case FUNCTION_DECL:
9590 strcpy (the, "method");
9591 break;
9592 case FIELD_DECL:
9593 strcpy (the, "field");
9594 break;
9595 case TYPE_DECL:
9596 strcpy (the, "class");
9597 break;
9598 default:
9599 abort ();
9600 }
9601 parse_warning_context
9602 (wfl, "The %s `%s' in class `%s' has been deprecated",
9603 the, lang_printable_name (decl, 0),
9604 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9605 }
9606 }
9607
9608 /* Returns 1 if class was declared in the current package, 0 otherwise */
9609
9610 static int
9611 class_in_current_package (class)
9612 tree class;
9613 {
9614 static tree cache = NULL_TREE;
9615 int qualified_flag;
9616 tree left;
9617
9618 if (cache == class)
9619 return 1;
9620
9621 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9622
9623 /* If the current package is empty and the name of CLASS is
9624 qualified, class isn't in the current package. If there is a
9625 current package and the name of the CLASS is not qualified, class
9626 isn't in the current package */
9627 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9628 return 0;
9629
9630 /* If there is not package and the name of CLASS isn't qualified,
9631 they belong to the same unnamed package */
9632 if (!ctxp->package && !qualified_flag)
9633 return 1;
9634
9635 /* Compare the left part of the name of CLASS with the package name */
9636 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9637 if (ctxp->package == left)
9638 {
9639 static int initialized_p;
9640 /* Register CACHE with the garbage collector. */
9641 if (!initialized_p)
9642 {
9643 ggc_add_tree_root (&cache, 1);
9644 initialized_p = 1;
9645 }
9646
9647 cache = class;
9648 return 1;
9649 }
9650 return 0;
9651 }
9652
9653 /* This function may generate code to access DECL from WHERE. This is
9654 done only if certain conditions meet. */
9655
9656 static tree
9657 maybe_access_field (decl, where, type)
9658 tree decl, where, type;
9659 {
9660 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9661 && !FIELD_STATIC (decl))
9662 decl = build_field_ref (where ? where : current_this,
9663 (type ? type : DECL_CONTEXT (decl)),
9664 DECL_NAME (decl));
9665 return decl;
9666 }
9667
9668 /* Build a method invocation, by patching PATCH. If non NULL
9669 and according to the situation, PRIMARY and WHERE may be
9670 used. IS_STATIC is set to 1 if the invoked function is static. */
9671
9672 static tree
9673 patch_method_invocation (patch, primary, where, from_super,
9674 is_static, ret_decl)
9675 tree patch, primary, where;
9676 int from_super;
9677 int *is_static;
9678 tree *ret_decl;
9679 {
9680 tree wfl = TREE_OPERAND (patch, 0);
9681 tree args = TREE_OPERAND (patch, 1);
9682 tree name = EXPR_WFL_NODE (wfl);
9683 tree list;
9684 int is_static_flag = 0;
9685 int is_super_init = 0;
9686 tree this_arg = NULL_TREE;
9687 int is_array_clone_call = 0;
9688
9689 /* Should be overriden if everything goes well. Otherwise, if
9690 something fails, it should keep this value. It stop the
9691 evaluation of a bogus assignment. See java_complete_tree,
9692 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9693 evaluating an assignment */
9694 TREE_TYPE (patch) = error_mark_node;
9695
9696 /* Since lookup functions are messing with line numbers, save the
9697 context now. */
9698 java_parser_context_save_global ();
9699
9700 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9701
9702 /* Resolution of qualified name, excluding constructors */
9703 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9704 {
9705 tree identifier, identifier_wfl, type, resolved;
9706 /* Extract the last IDENTIFIER of the qualified
9707 expression. This is a wfl and we will use it's location
9708 data during error report. */
9709 identifier_wfl = cut_identifier_in_qualified (wfl);
9710 identifier = EXPR_WFL_NODE (identifier_wfl);
9711
9712 /* Given the context, IDENTIFIER is syntactically qualified
9713 as a MethodName. We need to qualify what's before */
9714 qualify_ambiguous_name (wfl);
9715 resolved = resolve_field_access (wfl, NULL, NULL);
9716
9717 if (resolved == error_mark_node)
9718 PATCH_METHOD_RETURN_ERROR ();
9719
9720 type = GET_SKIP_TYPE (resolved);
9721 resolve_and_layout (type, NULL_TREE);
9722
9723 if (JPRIMITIVE_TYPE_P (type))
9724 {
9725 parse_error_context
9726 (identifier_wfl,
9727 "Can't invoke a method on primitive type `%s'",
9728 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9729 PATCH_METHOD_RETURN_ERROR ();
9730 }
9731
9732 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9733 args = nreverse (args);
9734
9735 /* We're resolving a call from a type */
9736 if (TREE_CODE (resolved) == TYPE_DECL)
9737 {
9738 if (CLASS_INTERFACE (resolved))
9739 {
9740 parse_error_context
9741 (identifier_wfl,
9742 "Can't make static reference to method `%s' in interface `%s'",
9743 IDENTIFIER_POINTER (identifier),
9744 IDENTIFIER_POINTER (name));
9745 PATCH_METHOD_RETURN_ERROR ();
9746 }
9747 if (list && !METHOD_STATIC (list))
9748 {
9749 char *fct_name = xstrdup (lang_printable_name (list, 0));
9750 parse_error_context
9751 (identifier_wfl,
9752 "Can't make static reference to method `%s %s' in class `%s'",
9753 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9754 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9755 free (fct_name);
9756 PATCH_METHOD_RETURN_ERROR ();
9757 }
9758 }
9759 else
9760 this_arg = primary = resolved;
9761
9762 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
9763 is_array_clone_call = 1;
9764
9765 /* IDENTIFIER_WFL will be used to report any problem further */
9766 wfl = identifier_wfl;
9767 }
9768 /* Resolution of simple names, names generated after a primary: or
9769 constructors */
9770 else
9771 {
9772 tree class_to_search = NULL_TREE;
9773 int lc; /* Looking for Constructor */
9774
9775 /* We search constructor in their target class */
9776 if (CALL_CONSTRUCTOR_P (patch))
9777 {
9778 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9779 class_to_search = EXPR_WFL_NODE (wfl);
9780 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9781 this_identifier_node)
9782 class_to_search = NULL_TREE;
9783 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9784 super_identifier_node)
9785 {
9786 is_super_init = 1;
9787 if (CLASSTYPE_SUPER (current_class))
9788 class_to_search =
9789 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9790 else
9791 {
9792 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9793 PATCH_METHOD_RETURN_ERROR ();
9794 }
9795 }
9796
9797 /* Class to search is NULL if we're searching the current one */
9798 if (class_to_search)
9799 {
9800 class_to_search = resolve_and_layout (class_to_search, wfl);
9801
9802 if (!class_to_search)
9803 {
9804 parse_error_context
9805 (wfl, "Class `%s' not found in type declaration",
9806 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9807 PATCH_METHOD_RETURN_ERROR ();
9808 }
9809
9810 /* Can't instantiate an abstract class, but we can
9811 invoke it's constructor. It's use within the `new'
9812 context is denied here. */
9813 if (CLASS_ABSTRACT (class_to_search)
9814 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9815 {
9816 parse_error_context
9817 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9818 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9819 PATCH_METHOD_RETURN_ERROR ();
9820 }
9821
9822 class_to_search = TREE_TYPE (class_to_search);
9823 }
9824 else
9825 class_to_search = current_class;
9826 lc = 1;
9827 }
9828 /* This is a regular search in the local class, unless an
9829 alternate class is specified. */
9830 else
9831 {
9832 class_to_search = (where ? where : current_class);
9833 lc = 0;
9834 }
9835
9836 /* NAME is a simple identifier or comes from a primary. Search
9837 in the class whose declaration contain the method being
9838 invoked. */
9839 resolve_and_layout (class_to_search, NULL_TREE);
9840
9841 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9842 /* Don't continue if no method were found, as the next statement
9843 can't be executed then. */
9844 if (!list)
9845 PATCH_METHOD_RETURN_ERROR ();
9846
9847 if (TYPE_ARRAY_P (class_to_search)
9848 && DECL_NAME (list) == get_identifier ("clone"))
9849 is_array_clone_call = 1;
9850
9851 /* Check for static reference if non static methods */
9852 if (check_for_static_method_reference (wfl, patch, list,
9853 class_to_search, primary))
9854 PATCH_METHOD_RETURN_ERROR ();
9855
9856 /* Check for inner classes creation from illegal contexts */
9857 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9858 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9859 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9860 {
9861 parse_error_context
9862 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9863 lang_printable_name (class_to_search, 0),
9864 (!current_this ? "" :
9865 "; an explicit one must be provided when creating this inner class"));
9866 PATCH_METHOD_RETURN_ERROR ();
9867 }
9868
9869 /* Non static methods are called with the current object extra
9870 argument. If patch a `new TYPE()', the argument is the value
9871 returned by the object allocator. If method is resolved as a
9872 primary, use the primary otherwise use the current THIS. */
9873 args = nreverse (args);
9874 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9875 {
9876 this_arg = primary ? primary : current_this;
9877
9878 /* If we're using an access method, things are different.
9879 There are two familly of cases:
9880
9881 1) We're not generating bytecodes:
9882
9883 - LIST is non static. It's invocation is transformed from
9884 x(a1,...,an) into this$<n>.x(a1,....an).
9885 - LIST is static. It's invocation is transformed from
9886 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9887
9888 2) We're generating bytecodes:
9889
9890 - LIST is non static. It's invocation is transformed from
9891 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9892 - LIST is static. It's invocation is transformed from
9893 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9894
9895 Of course, this$<n> can be abitrary complex, ranging from
9896 this$0 (the immediate outer context) to
9897 access$0(access$0(...(this$0))).
9898
9899 maybe_use_access_method returns a non zero value if the
9900 this_arg has to be moved into the (then generated) stub
9901 argument list. In the meantime, the selected function
9902 might have be replaced by a generated stub. */
9903 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9904 {
9905 args = tree_cons (NULL_TREE, this_arg, args);
9906 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9907 }
9908 }
9909 }
9910
9911 /* Merge point of all resolution schemes. If we have nothing, this
9912 is an error, already signaled */
9913 if (!list)
9914 PATCH_METHOD_RETURN_ERROR ();
9915
9916 /* Check accessibility, position the is_static flag, build and
9917 return the call */
9918 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
9919 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
9920 NULL_TREE), from_super)
9921 /* Calls to clone() on array types are permitted as a special-case. */
9922 && !is_array_clone_call)
9923 {
9924 char *fct_name = (char *) IDENTIFIER_POINTER (DECL_NAME (list));
9925 char *access = java_accstring_lookup (get_access_flags_from_decl (list));
9926 char *klass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
9927 char *refklass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
9928 char *what = (char *) (DECL_CONSTRUCTOR_P (list)
9929 ? "constructor" : "method");
9930 /* FIXME: WFL yields the wrong message here but I don't know
9931 what else to use. */
9932 parse_error_context (wfl,
9933 "Can't access %s %s `%s.%s' from `%s'",
9934 access, what, klass, fct_name, refklass);
9935 PATCH_METHOD_RETURN_ERROR ();
9936 }
9937 check_deprecation (wfl, list);
9938
9939 /* If invoking a innerclass constructor, there are hidden parameters
9940 to pass */
9941 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9942 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9943 {
9944 /* And make sure we add the accessed local variables to be saved
9945 in field aliases. */
9946 args = build_alias_initializer_parameter_list
9947 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9948
9949 /* Secretly pass the current_this/primary as a second argument */
9950 if (primary || current_this)
9951 {
9952 tree extra_arg;
9953 tree this_type = (current_this ?
9954 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
9955 /* Method's (list) enclosing context */
9956 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
9957 /* If we have a primary, use it. */
9958 if (primary)
9959 extra_arg = primary;
9960 /* The current `this' is an inner class but isn't a direct
9961 enclosing context for the inner class we're trying to
9962 create. Build an access to the proper enclosing context
9963 and use it. */
9964 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
9965 && this_type != TREE_TYPE (mec))
9966 {
9967
9968 extra_arg = build_access_to_thisn (current_class,
9969 TREE_TYPE (mec), 0);
9970 extra_arg = java_complete_tree (extra_arg);
9971 }
9972 /* Otherwise, just use the current `this' as an enclosing
9973 context. */
9974 else
9975 extra_arg = current_this;
9976 args = tree_cons (NULL_TREE, extra_arg, args);
9977 }
9978 else
9979 args = tree_cons (NULL_TREE, integer_zero_node, args);
9980 }
9981
9982 /* This handles the situation where a constructor invocation needs
9983 to have an enclosing context passed as a second parameter (the
9984 constructor is one of an inner class. We extract it from the
9985 current function. */
9986 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9987 {
9988 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9989 tree extra_arg;
9990
9991 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9992 {
9993 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9994 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9995 }
9996 else
9997 {
9998 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9999 extra_arg =
10000 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10001 extra_arg = java_complete_tree (extra_arg);
10002 }
10003 args = tree_cons (NULL_TREE, extra_arg, args);
10004 }
10005
10006 is_static_flag = METHOD_STATIC (list);
10007 if (! is_static_flag && this_arg != NULL_TREE)
10008 args = tree_cons (NULL_TREE, this_arg, args);
10009
10010 /* In the context of an explicit constructor invocation, we can't
10011 invoke any method relying on `this'. Exceptions are: we're
10012 invoking a static function, primary exists and is not the current
10013 this, we're creating a new object. */
10014 if (ctxp->explicit_constructor_p
10015 && !is_static_flag
10016 && (!primary || primary == current_this)
10017 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10018 {
10019 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10020 PATCH_METHOD_RETURN_ERROR ();
10021 }
10022 java_parser_context_restore_global ();
10023 if (is_static)
10024 *is_static = is_static_flag;
10025 /* Sometimes, we want the decl of the selected method. Such as for
10026 EH checking */
10027 if (ret_decl)
10028 *ret_decl = list;
10029 patch = patch_invoke (patch, list, args);
10030 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10031 {
10032 tree finit_parms, finit_call;
10033
10034 /* Prepare to pass hidden parameters to finit$, if any. */
10035 finit_parms = build_alias_initializer_parameter_list
10036 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10037
10038 finit_call =
10039 build_method_invocation (build_wfl_node (finit_identifier_node),
10040 finit_parms);
10041
10042 /* Generate the code used to initialize fields declared with an
10043 initialization statement and build a compound statement along
10044 with the super constructor invocation. */
10045 patch = build (COMPOUND_EXPR, void_type_node, patch,
10046 java_complete_tree (finit_call));
10047 CAN_COMPLETE_NORMALLY (patch) = 1;
10048 }
10049 return patch;
10050 }
10051
10052 /* Check that we're not trying to do a static reference to a method in
10053 non static method. Return 1 if it's the case, 0 otherwise. */
10054
10055 static int
10056 check_for_static_method_reference (wfl, node, method, where, primary)
10057 tree wfl, node, method, where, primary;
10058 {
10059 if (METHOD_STATIC (current_function_decl)
10060 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10061 {
10062 char *fct_name = xstrdup (lang_printable_name (method, 0));
10063 parse_error_context
10064 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10065 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10066 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10067 free (fct_name);
10068 return 1;
10069 }
10070 return 0;
10071 }
10072
10073 /* Fix the invocation of *MDECL if necessary in the case of a
10074 invocation from an inner class. *THIS_ARG might be modified
10075 appropriately and an alternative access to *MDECL might be
10076 returned. */
10077
10078 static int
10079 maybe_use_access_method (is_super_init, mdecl, this_arg)
10080 int is_super_init;
10081 tree *mdecl, *this_arg;
10082 {
10083 tree ctx;
10084 tree md = *mdecl, ta = *this_arg;
10085 int to_return = 0;
10086 int non_static_context = !METHOD_STATIC (md);
10087
10088 if (is_super_init
10089 || DECL_CONTEXT (md) == current_class
10090 || !PURE_INNER_CLASS_TYPE_P (current_class)
10091 || DECL_FINIT_P (md))
10092 return 0;
10093
10094 /* If we're calling a method found in an enclosing class, generate
10095 what it takes to retrieve the right this. Don't do that if we're
10096 invoking a static method. Note that if MD's type is unrelated to
10097 CURRENT_CLASS, then the current this can be used. */
10098
10099 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10100 {
10101 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10102 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10103 {
10104 ta = build_current_thisn (current_class);
10105 ta = build_wfl_node (ta);
10106 }
10107 else
10108 {
10109 tree type = ctx;
10110 while (type)
10111 {
10112 maybe_build_thisn_access_method (type);
10113 if (inherits_from_p (type, DECL_CONTEXT (md)))
10114 {
10115 ta = build_access_to_thisn (ctx, type, 0);
10116 break;
10117 }
10118 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10119 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10120 }
10121 }
10122 ta = java_complete_tree (ta);
10123 }
10124
10125 /* We might have to use an access method to get to MD. We can
10126 break the method access rule as far as we're not generating
10127 bytecode */
10128 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10129 {
10130 md = build_outer_method_access_method (md);
10131 to_return = 1;
10132 }
10133
10134 *mdecl = md;
10135 *this_arg = ta;
10136
10137 /* Returnin a non zero value indicates we were doing a non static
10138 method invokation that is now a static invocation. It will have
10139 callee displace `this' to insert it in the regular argument
10140 list. */
10141 return (non_static_context && to_return);
10142 }
10143
10144 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10145 mode. */
10146
10147 static tree
10148 patch_invoke (patch, method, args)
10149 tree patch, method, args;
10150 {
10151 tree dtable, func;
10152 tree original_call, t, ta;
10153 tree cond = NULL_TREE;
10154
10155 /* Last step for args: convert build-in types. If we're dealing with
10156 a new TYPE() type call, the first argument to the constructor
10157 isn't found in the incoming argument list, but delivered by
10158 `new' */
10159 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10160 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10161 t = TREE_CHAIN (t);
10162 for (ta = args; t != end_params_node && ta;
10163 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10164 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10165 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10166 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10167
10168 /* Resolve unresolved returned type isses */
10169 t = TREE_TYPE (TREE_TYPE (method));
10170 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10171 resolve_and_layout (TREE_TYPE (t), NULL);
10172
10173 if (flag_emit_class_files || flag_emit_xref)
10174 func = method;
10175 else
10176 {
10177 tree signature = build_java_signature (TREE_TYPE (method));
10178 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10179 {
10180 case INVOKE_VIRTUAL:
10181 dtable = invoke_build_dtable (0, args);
10182 func = build_invokevirtual (dtable, method);
10183 break;
10184
10185 case INVOKE_NONVIRTUAL:
10186 /* If the object for the method call is null, we throw an
10187 exception. We don't do this if the object is the current
10188 method's `this'. In other cases we just rely on an
10189 optimization pass to eliminate redundant checks. */
10190 if (TREE_VALUE (args) != current_this)
10191 {
10192 /* We use a SAVE_EXPR here to make sure we only evaluate
10193 the new `self' expression once. */
10194 tree save_arg = save_expr (TREE_VALUE (args));
10195 TREE_VALUE (args) = save_arg;
10196 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10197 null_pointer_node);
10198 }
10199 /* Fall through. */
10200
10201 case INVOKE_SUPER:
10202 case INVOKE_STATIC:
10203 func = build_known_method_ref (method, TREE_TYPE (method),
10204 DECL_CONTEXT (method),
10205 signature, args);
10206 break;
10207
10208 case INVOKE_INTERFACE:
10209 dtable = invoke_build_dtable (1, args);
10210 func = build_invokeinterface (dtable, method);
10211 break;
10212
10213 default:
10214 abort ();
10215 }
10216
10217 /* Ensure self_type is initialized, (invokestatic). FIXME */
10218 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10219 }
10220
10221 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10222 TREE_OPERAND (patch, 0) = func;
10223 TREE_OPERAND (patch, 1) = args;
10224 original_call = patch;
10225
10226 /* We're processing a `new TYPE ()' form. New is called and its
10227 returned value is the first argument to the constructor. We build
10228 a COMPOUND_EXPR and use saved expression so that the overall NEW
10229 expression value is a pointer to a newly created and initialized
10230 class. */
10231 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10232 {
10233 tree class = DECL_CONTEXT (method);
10234 tree c1, saved_new, size, new;
10235 if (flag_emit_class_files || flag_emit_xref)
10236 {
10237 TREE_TYPE (patch) = build_pointer_type (class);
10238 return patch;
10239 }
10240 if (!TYPE_SIZE (class))
10241 safe_layout_class (class);
10242 size = size_in_bytes (class);
10243 new = build (CALL_EXPR, promote_type (class),
10244 build_address_of (alloc_object_node),
10245 tree_cons (NULL_TREE, build_class_ref (class),
10246 build_tree_list (NULL_TREE,
10247 size_in_bytes (class))),
10248 NULL_TREE);
10249 saved_new = save_expr (new);
10250 c1 = build_tree_list (NULL_TREE, saved_new);
10251 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10252 TREE_OPERAND (original_call, 1) = c1;
10253 TREE_SET_CODE (original_call, CALL_EXPR);
10254 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10255 }
10256
10257 /* If COND is set, then we are building a check to see if the object
10258 is NULL. */
10259 if (cond != NULL_TREE)
10260 {
10261 /* We have to make the `then' branch a compound expression to
10262 make the types turn out right. This seems bizarre. */
10263 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10264 build (COMPOUND_EXPR, TREE_TYPE (patch),
10265 build (CALL_EXPR, void_type_node,
10266 build_address_of (soft_nullpointer_node),
10267 NULL_TREE, NULL_TREE),
10268 (FLOAT_TYPE_P (TREE_TYPE (patch))
10269 ? build_real (TREE_TYPE (patch), dconst0)
10270 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10271 integer_zero_node))),
10272 patch);
10273 TREE_SIDE_EFFECTS (patch) = 1;
10274 }
10275
10276 return patch;
10277 }
10278
10279 static int
10280 invocation_mode (method, super)
10281 tree method;
10282 int super;
10283 {
10284 int access = get_access_flags_from_decl (method);
10285
10286 if (super)
10287 return INVOKE_SUPER;
10288
10289 if (access & ACC_STATIC)
10290 return INVOKE_STATIC;
10291
10292 /* We have to look for a constructor before we handle nonvirtual
10293 calls; otherwise the constructor will look nonvirtual. */
10294 if (DECL_CONSTRUCTOR_P (method))
10295 return INVOKE_STATIC;
10296
10297 if (access & ACC_FINAL || access & ACC_PRIVATE)
10298 return INVOKE_NONVIRTUAL;
10299
10300 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10301 return INVOKE_NONVIRTUAL;
10302
10303 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10304 return INVOKE_INTERFACE;
10305
10306 return INVOKE_VIRTUAL;
10307 }
10308
10309 /* Retrieve a refined list of matching methods. It covers the step
10310 15.11.2 (Compile-Time Step 2) */
10311
10312 static tree
10313 lookup_method_invoke (lc, cl, class, name, arg_list)
10314 int lc;
10315 tree cl;
10316 tree class, name, arg_list;
10317 {
10318 tree atl = end_params_node; /* Arg Type List */
10319 tree method, signature, list, node;
10320 const char *candidates; /* Used for error report */
10321 char *dup;
10322
10323 /* Fix the arguments */
10324 for (node = arg_list; node; node = TREE_CHAIN (node))
10325 {
10326 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10327 /* Non primitive type may have to be resolved */
10328 if (!JPRIMITIVE_TYPE_P (current_arg))
10329 resolve_and_layout (current_arg, NULL_TREE);
10330 /* And promoted */
10331 if (TREE_CODE (current_arg) == RECORD_TYPE)
10332 current_arg = promote_type (current_arg);
10333 atl = tree_cons (NULL_TREE, current_arg, atl);
10334 }
10335
10336 /* Presto. If we're dealing with an anonymous class and a
10337 constructor call, generate the right constructor now, since we
10338 know the arguments' types. */
10339
10340 if (lc && ANONYMOUS_CLASS_P (class))
10341 craft_constructor (TYPE_NAME (class), atl);
10342
10343 /* Find all candidates and then refine the list, searching for the
10344 most specific method. */
10345 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10346 list = find_most_specific_methods_list (list);
10347 if (list && !TREE_CHAIN (list))
10348 return TREE_VALUE (list);
10349
10350 /* Issue an error. List candidates if any. Candidates are listed
10351 only if accessible (non accessible methods may end-up here for
10352 the sake of a better error report). */
10353 candidates = NULL;
10354 if (list)
10355 {
10356 tree current;
10357 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10358 for (current = list; current; current = TREE_CHAIN (current))
10359 {
10360 tree cm = TREE_VALUE (current);
10361 char string [4096];
10362 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10363 continue;
10364 sprintf
10365 (string, " `%s' in `%s'%s",
10366 get_printable_method_name (cm),
10367 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10368 (TREE_CHAIN (current) ? "\n" : ""));
10369 obstack_grow (&temporary_obstack, string, strlen (string));
10370 }
10371 obstack_1grow (&temporary_obstack, '\0');
10372 candidates = obstack_finish (&temporary_obstack);
10373 }
10374 /* Issue the error message */
10375 method = make_node (FUNCTION_TYPE);
10376 TYPE_ARG_TYPES (method) = atl;
10377 signature = build_java_argument_signature (method);
10378 dup = xstrdup (lang_printable_name (class, 0));
10379 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10380 (lc ? "constructor" : "method"),
10381 (lc ? dup : IDENTIFIER_POINTER (name)),
10382 IDENTIFIER_POINTER (signature), dup,
10383 (candidates ? candidates : ""));
10384 free (dup);
10385 return NULL_TREE;
10386 }
10387
10388 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10389 when we're looking for a constructor. */
10390
10391 static tree
10392 find_applicable_accessible_methods_list (lc, class, name, arglist)
10393 int lc;
10394 tree class, name, arglist;
10395 {
10396 static struct hash_table t, *searched_classes = NULL;
10397 static int search_not_done = 0;
10398 tree list = NULL_TREE, all_list = NULL_TREE;
10399
10400 /* Check the hash table to determine if this class has been searched
10401 already. */
10402 if (searched_classes)
10403 {
10404 if (hash_lookup (searched_classes,
10405 (const hash_table_key) class, FALSE, NULL))
10406 return NULL;
10407 }
10408 else
10409 {
10410 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10411 java_hash_compare_tree_node);
10412 searched_classes = &t;
10413 }
10414
10415 search_not_done++;
10416 hash_lookup (searched_classes,
10417 (const hash_table_key) class, TRUE, NULL);
10418
10419 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10420 {
10421 load_class (class, 1);
10422 safe_layout_class (class);
10423 }
10424
10425 /* Search interfaces */
10426 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10427 && CLASS_INTERFACE (TYPE_NAME (class)))
10428 {
10429 int i, n;
10430 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10431 search_applicable_methods_list (lc, TYPE_METHODS (class),
10432 name, arglist, &list, &all_list);
10433 n = TREE_VEC_LENGTH (basetype_vec);
10434 for (i = 1; i < n; i++)
10435 {
10436 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10437 tree rlist;
10438
10439 rlist = find_applicable_accessible_methods_list (lc, t, name,
10440 arglist);
10441 list = chainon (rlist, list);
10442 }
10443 }
10444 /* Search classes */
10445 else
10446 {
10447 tree sc = class;
10448 int seen_inner_class = 0;
10449 search_applicable_methods_list (lc, TYPE_METHODS (class),
10450 name, arglist, &list, &all_list);
10451
10452 /* When looking finit$ or class$, we turn LC to 1 so that we
10453 only search in class. Note that we should have found
10454 something at this point. */
10455 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name))
10456 {
10457 lc = 1;
10458 if (!list)
10459 abort ();
10460 }
10461
10462 /* We must search all interfaces of this class */
10463 if (!lc)
10464 {
10465 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10466 int n = TREE_VEC_LENGTH (basetype_vec), i;
10467 for (i = 1; i < n; i++)
10468 {
10469 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10470 if (t != object_type_node)
10471 {
10472 tree rlist
10473 = find_applicable_accessible_methods_list (lc, t,
10474 name, arglist);
10475 list = chainon (rlist, list);
10476 }
10477 }
10478 }
10479
10480 /* Search enclosing context of inner classes before looking
10481 ancestors up. */
10482 while (!lc && INNER_CLASS_TYPE_P (class))
10483 {
10484 tree rlist;
10485 seen_inner_class = 1;
10486 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10487 rlist = find_applicable_accessible_methods_list (lc, class,
10488 name, arglist);
10489 list = chainon (rlist, list);
10490 }
10491
10492 if (!lc && seen_inner_class
10493 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10494 class = CLASSTYPE_SUPER (sc);
10495 else
10496 class = sc;
10497
10498 /* Search superclass */
10499 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10500 {
10501 tree rlist;
10502 class = CLASSTYPE_SUPER (class);
10503 rlist = find_applicable_accessible_methods_list (lc, class,
10504 name, arglist);
10505 list = chainon (rlist, list);
10506 }
10507 }
10508
10509 search_not_done--;
10510
10511 /* We're done. Reset the searched classes list and finally search
10512 java.lang.Object if it wasn't searched already. */
10513 if (!search_not_done)
10514 {
10515 if (!lc
10516 && TYPE_METHODS (object_type_node)
10517 && !hash_lookup (searched_classes,
10518 (const hash_table_key) object_type_node,
10519 FALSE, NULL))
10520 {
10521 search_applicable_methods_list (lc,
10522 TYPE_METHODS (object_type_node),
10523 name, arglist, &list, &all_list);
10524 }
10525 hash_table_free (searched_classes);
10526 searched_classes = NULL;
10527 }
10528
10529 /* Either return the list obtained or all selected (but
10530 inaccessible) methods for better error report. */
10531 return (!list ? all_list : list);
10532 }
10533
10534 /* Effectively search for the appropriate method in method */
10535
10536 static void
10537 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10538 int lc;
10539 tree method, name, arglist;
10540 tree *list, *all_list;
10541 {
10542 for (; method; method = TREE_CHAIN (method))
10543 {
10544 /* When dealing with constructor, stop here, otherwise search
10545 other classes */
10546 if (lc && !DECL_CONSTRUCTOR_P (method))
10547 continue;
10548 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10549 || (DECL_NAME (method) != name)))
10550 continue;
10551
10552 if (argument_types_convertible (method, arglist))
10553 {
10554 /* Retain accessible methods only */
10555 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10556 method, NULL_TREE, 0))
10557 *list = tree_cons (NULL_TREE, method, *list);
10558 else
10559 /* Also retain all selected method here */
10560 *all_list = tree_cons (NULL_TREE, method, *list);
10561 }
10562 }
10563 }
10564
10565 /* 15.11.2.2 Choose the Most Specific Method */
10566
10567 static tree
10568 find_most_specific_methods_list (list)
10569 tree list;
10570 {
10571 int max = 0;
10572 int abstract, candidates;
10573 tree current, new_list = NULL_TREE;
10574 for (current = list; current; current = TREE_CHAIN (current))
10575 {
10576 tree method;
10577 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10578
10579 for (method = list; method; method = TREE_CHAIN (method))
10580 {
10581 tree method_v, current_v;
10582 /* Don't test a method against itself */
10583 if (method == current)
10584 continue;
10585
10586 method_v = TREE_VALUE (method);
10587 current_v = TREE_VALUE (current);
10588
10589 /* Compare arguments and location where methods where declared */
10590 if (argument_types_convertible (method_v, current_v))
10591 {
10592 if (valid_method_invocation_conversion_p
10593 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10594 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10595 && enclosing_context_p (DECL_CONTEXT (method_v),
10596 DECL_CONTEXT (current_v))))
10597 {
10598 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10599 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10600 max = (v > max ? v : max);
10601 }
10602 }
10603 }
10604 }
10605
10606 /* Review the list and select the maximally specific methods */
10607 for (current = list, abstract = -1, candidates = -1;
10608 current; current = TREE_CHAIN (current))
10609 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10610 {
10611 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10612 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10613 candidates++;
10614 }
10615
10616 /* If we have several and they're all abstract, just pick the
10617 closest one. */
10618 if (candidates > 0 && (candidates == abstract))
10619 {
10620 new_list = nreverse (new_list);
10621 TREE_CHAIN (new_list) = NULL_TREE;
10622 }
10623
10624 /* We have several (we couldn't find a most specific), all but one
10625 are abstract, we pick the only non abstract one. */
10626 if (candidates > 0 && (candidates == abstract+1))
10627 {
10628 for (current = new_list; current; current = TREE_CHAIN (current))
10629 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10630 {
10631 TREE_CHAIN (current) = NULL_TREE;
10632 new_list = current;
10633 }
10634 }
10635
10636 /* If we can't find one, lower expectations and try to gather multiple
10637 maximally specific methods */
10638 while (!new_list && max)
10639 {
10640 while (--max > 0)
10641 {
10642 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10643 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10644 }
10645 }
10646
10647 return new_list;
10648 }
10649
10650 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10651 converted by method invocation conversion (5.3) to the type of the
10652 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10653 to change less often than M1. */
10654
10655 static int
10656 argument_types_convertible (m1, m2_or_arglist)
10657 tree m1, m2_or_arglist;
10658 {
10659 static tree m2_arg_value = NULL_TREE;
10660 static tree m2_arg_cache = NULL_TREE;
10661 static int initialized_p;
10662
10663 register tree m1_arg, m2_arg;
10664
10665 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10666 collector. */
10667 if (!initialized_p)
10668 {
10669 ggc_add_tree_root (&m2_arg_value, 1);
10670 ggc_add_tree_root (&m2_arg_cache, 1);
10671 initialized_p = 1;
10672 }
10673
10674 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10675
10676 if (m2_arg_value == m2_or_arglist)
10677 m2_arg = m2_arg_cache;
10678 else
10679 {
10680 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10681 argument types */
10682 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10683 {
10684 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10685 if (!METHOD_STATIC (m2_or_arglist))
10686 m2_arg = TREE_CHAIN (m2_arg);
10687 }
10688 else
10689 m2_arg = m2_or_arglist;
10690
10691 m2_arg_value = m2_or_arglist;
10692 m2_arg_cache = m2_arg;
10693 }
10694
10695 while (m1_arg != end_params_node && m2_arg != end_params_node)
10696 {
10697 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10698 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10699 TREE_VALUE (m2_arg)))
10700 break;
10701 m1_arg = TREE_CHAIN (m1_arg);
10702 m2_arg = TREE_CHAIN (m2_arg);
10703 }
10704 return m1_arg == end_params_node && m2_arg == end_params_node;
10705 }
10706
10707 /* Qualification routines */
10708
10709 static void
10710 qualify_ambiguous_name (id)
10711 tree id;
10712 {
10713 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10714 saved_current_class;
10715 int again, super_found = 0, this_found = 0, new_array_found = 0;
10716 int code;
10717
10718 /* We first qualify the first element, then derive qualification of
10719 others based on the first one. If the first element is qualified
10720 by a resolution (field or type), this resolution is stored in the
10721 QUAL_RESOLUTION of the qual element being examined. We need to
10722 save the current_class since the use of SUPER might change the
10723 its value. */
10724 saved_current_class = current_class;
10725 qual = EXPR_WFL_QUALIFICATION (id);
10726 do {
10727
10728 /* Simple qualified expression feature a qual_wfl that is a
10729 WFL. Expression derived from a primary feature more complicated
10730 things like a CALL_EXPR. Expression from primary need to be
10731 worked out to extract the part on which the qualification will
10732 take place. */
10733 qual_wfl = QUAL_WFL (qual);
10734 switch (TREE_CODE (qual_wfl))
10735 {
10736 case CALL_EXPR:
10737 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10738 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10739 {
10740 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10741 qual_wfl = QUAL_WFL (qual);
10742 }
10743 break;
10744 case NEW_ARRAY_EXPR:
10745 case NEW_ANONYMOUS_ARRAY_EXPR:
10746 qual = TREE_CHAIN (qual);
10747 again = new_array_found = 1;
10748 continue;
10749 case CONVERT_EXPR:
10750 break;
10751 case NEW_CLASS_EXPR:
10752 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10753 break;
10754 case ARRAY_REF:
10755 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10756 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10757 break;
10758 case STRING_CST:
10759 qual = TREE_CHAIN (qual);
10760 qual_wfl = QUAL_WFL (qual);
10761 break;
10762 case CLASS_LITERAL:
10763 qual = TREE_CHAIN (qual);
10764 qual_wfl = QUAL_WFL (qual);
10765 break;
10766 default:
10767 /* Fix for -Wall. Just break doing nothing */
10768 break;
10769 }
10770
10771 ptr_type = current_class;
10772 again = 0;
10773 code = TREE_CODE (qual_wfl);
10774
10775 /* Pos evaluation: non WFL leading expression nodes */
10776 if (code == CONVERT_EXPR
10777 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10778 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10779
10780 else if (code == INTEGER_CST)
10781 name = qual_wfl;
10782
10783 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10784 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10785 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10786
10787 else if (code == TREE_LIST)
10788 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10789
10790 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10791 || code == PLUS_EXPR)
10792 {
10793 qual = TREE_CHAIN (qual);
10794 qual_wfl = QUAL_WFL (qual);
10795 again = 1;
10796 }
10797 else
10798 {
10799 name = EXPR_WFL_NODE (qual_wfl);
10800 if (!name)
10801 {
10802 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10803 again = 1;
10804 }
10805 }
10806
10807 /* If we have a THIS (from a primary), we set the context accordingly */
10808 if (name == this_identifier_node)
10809 {
10810 /* This isn't really elegant. One more added irregularity
10811 before I start using COMPONENT_REF (hopefully very soon.) */
10812 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10813 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10814 EXPR_WITH_FILE_LOCATION
10815 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10816 this_identifier_node)
10817 {
10818 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10819 qual = EXPR_WFL_QUALIFICATION (qual);
10820 }
10821 qual = TREE_CHAIN (qual);
10822 qual_wfl = QUAL_WFL (qual);
10823 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10824 again = 1;
10825 else
10826 name = EXPR_WFL_NODE (qual_wfl);
10827 this_found = 1;
10828 }
10829 /* If we have a SUPER, we set the context accordingly */
10830 if (name == super_identifier_node)
10831 {
10832 current_class = CLASSTYPE_SUPER (ptr_type);
10833 /* Check that there is such a thing as a super class. If not,
10834 return. The error will be caught later on, during the
10835 resolution */
10836 if (!current_class)
10837 {
10838 current_class = saved_current_class;
10839 return;
10840 }
10841 qual = TREE_CHAIN (qual);
10842 /* Do one more interation to set things up */
10843 super_found = again = 1;
10844 }
10845 } while (again);
10846
10847 /* If name appears within the scope of a local variable declaration
10848 or parameter declaration, then it is an expression name. We don't
10849 carry this test out if we're in the context of the use of SUPER
10850 or THIS */
10851 if (!this_found && !super_found
10852 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10853 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10854 {
10855 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10856 QUAL_RESOLUTION (qual) = decl;
10857 }
10858
10859 /* If within the class/interface NAME was found to be used there
10860 exists a (possibly inherited) field named NAME, then this is an
10861 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10862 address length, it is OK. */
10863 else if ((decl = lookup_field_wrapper (ptr_type, name))
10864 || (new_array_found && name == length_identifier_node))
10865 {
10866 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10867 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10868 }
10869
10870 /* We reclassify NAME as yielding to a type name resolution if:
10871 - NAME is a class/interface declared within the compilation
10872 unit containing NAME,
10873 - NAME is imported via a single-type-import declaration,
10874 - NAME is declared in an another compilation unit of the package
10875 of the compilation unit containing NAME,
10876 - NAME is declared by exactly on type-import-on-demand declaration
10877 of the compilation unit containing NAME.
10878 - NAME is actually a STRING_CST. */
10879 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10880 || (decl = resolve_and_layout (name, NULL_TREE)))
10881 {
10882 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10883 QUAL_RESOLUTION (qual) = decl;
10884 }
10885
10886 /* Method call, array references and cast are expression name */
10887 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10888 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10889 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10890 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10891
10892 /* Check here that NAME isn't declared by more than one
10893 type-import-on-demand declaration of the compilation unit
10894 containing NAME. FIXME */
10895
10896 /* Otherwise, NAME is reclassified as a package name */
10897 else
10898 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10899
10900 /* Propagate the qualification accross other components of the
10901 qualified name */
10902 for (qual = TREE_CHAIN (qual); qual;
10903 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10904 {
10905 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10906 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10907 else
10908 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10909 }
10910
10911 /* Store the global qualification for the ambiguous part of ID back
10912 into ID fields */
10913 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10914 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10915 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10916 RESOLVE_TYPE_NAME_P (id) = 1;
10917 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10918 RESOLVE_PACKAGE_NAME_P (id) = 1;
10919
10920 /* Restore the current class */
10921 current_class = saved_current_class;
10922 }
10923
10924 static int
10925 breakdown_qualified (left, right, source)
10926 tree *left, *right, source;
10927 {
10928 char *p, *base;
10929 int l = IDENTIFIER_LENGTH (source);
10930
10931 base = alloca (l + 1);
10932 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10933
10934 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10935 p = base + l - 1;
10936 while (*p != '.' && p != base)
10937 p--;
10938
10939 /* We didn't find a '.'. Return an error */
10940 if (p == base)
10941 return 1;
10942
10943 *p = '\0';
10944 if (right)
10945 *right = get_identifier (p+1);
10946 *left = get_identifier (base);
10947
10948 return 0;
10949 }
10950
10951 /* Patch tree nodes in a function body. When a BLOCK is found, push
10952 local variable decls if present.
10953 Same as java_complete_lhs, but does resolve static finals to values. */
10954
10955 static tree
10956 java_complete_tree (node)
10957 tree node;
10958 {
10959 node = java_complete_lhs (node);
10960 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
10961 && DECL_INITIAL (node) != NULL_TREE
10962 && !flag_emit_xref)
10963 {
10964 tree value = DECL_INITIAL (node);
10965 DECL_INITIAL (node) = NULL_TREE;
10966 value = fold_constant_for_init (value, node);
10967 DECL_INITIAL (node) = value;
10968 if (value != NULL_TREE)
10969 {
10970 /* fold_constant_for_init sometimes widen the original type
10971 of the constant (i.e. byte to int.) It's not desirable,
10972 especially if NODE is a function argument. */
10973 if (TREE_CODE (value) == INTEGER_CST
10974 && TREE_TYPE (node) != TREE_TYPE (value))
10975 return convert (TREE_TYPE (node), value);
10976 else
10977 return value;
10978 }
10979 else
10980 DECL_FIELD_FINAL_IUD (node) = 0;
10981 }
10982 return node;
10983 }
10984
10985 static tree
10986 java_stabilize_reference (node)
10987 tree node;
10988 {
10989 if (TREE_CODE (node) == COMPOUND_EXPR)
10990 {
10991 tree op0 = TREE_OPERAND (node, 0);
10992 tree op1 = TREE_OPERAND (node, 1);
10993 TREE_OPERAND (node, 0) = save_expr (op0);
10994 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10995 return node;
10996 }
10997 return stabilize_reference (node);
10998 }
10999
11000 /* Patch tree nodes in a function body. When a BLOCK is found, push
11001 local variable decls if present.
11002 Same as java_complete_tree, but does not resolve static finals to values. */
11003
11004 static tree
11005 java_complete_lhs (node)
11006 tree node;
11007 {
11008 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11009 int flag;
11010
11011 /* CONVERT_EXPR always has its type set, even though it needs to be
11012 worked out. */
11013 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11014 return node;
11015
11016 /* The switch block implements cases processing container nodes
11017 first. Contained nodes are always written back. Leaves come
11018 next and return a value. */
11019 switch (TREE_CODE (node))
11020 {
11021 case BLOCK:
11022
11023 /* 1- Block section.
11024 Set the local values on decl names so we can identify them
11025 faster when they're referenced. At that stage, identifiers
11026 are legal so we don't check for declaration errors. */
11027 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11028 {
11029 DECL_CONTEXT (cn) = current_function_decl;
11030 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11031 }
11032 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11033 CAN_COMPLETE_NORMALLY (node) = 1;
11034 else
11035 {
11036 tree stmt = BLOCK_EXPR_BODY (node);
11037 tree *ptr;
11038 int error_seen = 0;
11039 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11040 {
11041 /* Re-order from (((A; B); C); ...; Z) to
11042 (A; (B; (C ; (...; Z)))).
11043 This makes it easier to scan the statements left-to-right
11044 without using recursion (which might overflow the stack
11045 if the block has many statements. */
11046 for (;;)
11047 {
11048 tree left = TREE_OPERAND (stmt, 0);
11049 if (TREE_CODE (left) != COMPOUND_EXPR)
11050 break;
11051 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11052 TREE_OPERAND (left, 1) = stmt;
11053 stmt = left;
11054 }
11055 BLOCK_EXPR_BODY (node) = stmt;
11056 }
11057
11058 /* Now do the actual complete, without deep recursion for
11059 long blocks. */
11060 ptr = &BLOCK_EXPR_BODY (node);
11061 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11062 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11063 {
11064 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11065 tree *next = &TREE_OPERAND (*ptr, 1);
11066 TREE_OPERAND (*ptr, 0) = cur;
11067 if (cur == empty_stmt_node)
11068 {
11069 /* Optimization; makes it easier to detect empty bodies.
11070 Most useful for <clinit> with all-constant initializer. */
11071 *ptr = *next;
11072 continue;
11073 }
11074 if (TREE_CODE (cur) == ERROR_MARK)
11075 error_seen++;
11076 else if (! CAN_COMPLETE_NORMALLY (cur))
11077 {
11078 wfl_op2 = *next;
11079 for (;;)
11080 {
11081 if (TREE_CODE (wfl_op2) == BLOCK)
11082 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11083 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11084 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11085 else
11086 break;
11087 }
11088 if (TREE_CODE (wfl_op2) != CASE_EXPR
11089 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11090 unreachable_stmt_error (*ptr);
11091 }
11092 ptr = next;
11093 }
11094 *ptr = java_complete_tree (*ptr);
11095
11096 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11097 return error_mark_node;
11098 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11099 }
11100 /* Turn local bindings to null */
11101 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11102 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11103
11104 TREE_TYPE (node) = void_type_node;
11105 break;
11106
11107 /* 2- They are expressions but ultimately deal with statements */
11108
11109 case THROW_EXPR:
11110 wfl_op1 = TREE_OPERAND (node, 0);
11111 COMPLETE_CHECK_OP_0 (node);
11112 /* 14.19 A throw statement cannot complete normally. */
11113 CAN_COMPLETE_NORMALLY (node) = 0;
11114 return patch_throw_statement (node, wfl_op1);
11115
11116 case SYNCHRONIZED_EXPR:
11117 wfl_op1 = TREE_OPERAND (node, 0);
11118 return patch_synchronized_statement (node, wfl_op1);
11119
11120 case TRY_EXPR:
11121 return patch_try_statement (node);
11122
11123 case TRY_FINALLY_EXPR:
11124 COMPLETE_CHECK_OP_0 (node);
11125 COMPLETE_CHECK_OP_1 (node);
11126 CAN_COMPLETE_NORMALLY (node)
11127 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11128 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11129 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11130 return node;
11131
11132 case CLEANUP_POINT_EXPR:
11133 COMPLETE_CHECK_OP_0 (node);
11134 TREE_TYPE (node) = void_type_node;
11135 CAN_COMPLETE_NORMALLY (node) =
11136 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11137 return node;
11138
11139 case WITH_CLEANUP_EXPR:
11140 COMPLETE_CHECK_OP_0 (node);
11141 COMPLETE_CHECK_OP_2 (node);
11142 CAN_COMPLETE_NORMALLY (node) =
11143 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11144 TREE_TYPE (node) = void_type_node;
11145 return node;
11146
11147 case LABELED_BLOCK_EXPR:
11148 PUSH_LABELED_BLOCK (node);
11149 if (LABELED_BLOCK_BODY (node))
11150 COMPLETE_CHECK_OP_1 (node);
11151 TREE_TYPE (node) = void_type_node;
11152 POP_LABELED_BLOCK ();
11153
11154 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11155 {
11156 LABELED_BLOCK_BODY (node) = NULL_TREE;
11157 CAN_COMPLETE_NORMALLY (node) = 1;
11158 }
11159 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11160 CAN_COMPLETE_NORMALLY (node) = 1;
11161 return node;
11162
11163 case EXIT_BLOCK_EXPR:
11164 /* We don't complete operand 1, because it's the return value of
11165 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11166 return patch_bc_statement (node);
11167
11168 case CASE_EXPR:
11169 cn = java_complete_tree (TREE_OPERAND (node, 0));
11170 if (cn == error_mark_node)
11171 return cn;
11172
11173 /* First, the case expression must be constant. Values of final
11174 fields are accepted. */
11175 cn = fold (cn);
11176 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11177 && JDECL_P (TREE_OPERAND (cn, 1))
11178 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11179 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11180 {
11181 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11182 TREE_OPERAND (cn, 1));
11183 }
11184
11185 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11186 {
11187 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11188 parse_error_context (node, "Constant expression required");
11189 return error_mark_node;
11190 }
11191
11192 nn = ctxp->current_loop;
11193
11194 /* It must be assignable to the type of the switch expression. */
11195 if (!try_builtin_assignconv (NULL_TREE,
11196 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11197 {
11198 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11199 parse_error_context
11200 (wfl_operator,
11201 "Incompatible type for case. Can't convert `%s' to `int'",
11202 lang_printable_name (TREE_TYPE (cn), 0));
11203 return error_mark_node;
11204 }
11205
11206 cn = fold (convert (int_type_node, cn));
11207
11208 /* Multiple instance of a case label bearing the same
11209 value is checked during code generation. The case
11210 expression is allright so far. */
11211 if (TREE_CODE (cn) == VAR_DECL)
11212 cn = DECL_INITIAL (cn);
11213 TREE_OPERAND (node, 0) = cn;
11214 TREE_TYPE (node) = void_type_node;
11215 CAN_COMPLETE_NORMALLY (node) = 1;
11216 TREE_SIDE_EFFECTS (node) = 1;
11217 break;
11218
11219 case DEFAULT_EXPR:
11220 nn = ctxp->current_loop;
11221 /* Only one default label is allowed per switch statement */
11222 if (SWITCH_HAS_DEFAULT (nn))
11223 {
11224 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11225 parse_error_context (wfl_operator,
11226 "Duplicate case label: `default'");
11227 return error_mark_node;
11228 }
11229 else
11230 SWITCH_HAS_DEFAULT (nn) = 1;
11231 TREE_TYPE (node) = void_type_node;
11232 TREE_SIDE_EFFECTS (node) = 1;
11233 CAN_COMPLETE_NORMALLY (node) = 1;
11234 break;
11235
11236 case SWITCH_EXPR:
11237 case LOOP_EXPR:
11238 PUSH_LOOP (node);
11239 /* Check whether the loop was enclosed in a labeled
11240 statement. If not, create one, insert the loop in it and
11241 return the node */
11242 nn = patch_loop_statement (node);
11243
11244 /* Anyways, walk the body of the loop */
11245 if (TREE_CODE (node) == LOOP_EXPR)
11246 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11247 /* Switch statement: walk the switch expression and the cases */
11248 else
11249 node = patch_switch_statement (node);
11250
11251 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11252 nn = error_mark_node;
11253 else
11254 {
11255 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11256 /* If we returned something different, that's because we
11257 inserted a label. Pop the label too. */
11258 if (nn != node)
11259 {
11260 if (CAN_COMPLETE_NORMALLY (node))
11261 CAN_COMPLETE_NORMALLY (nn) = 1;
11262 POP_LABELED_BLOCK ();
11263 }
11264 }
11265 POP_LOOP ();
11266 return nn;
11267
11268 case EXIT_EXPR:
11269 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11270 return patch_exit_expr (node);
11271
11272 case COND_EXPR:
11273 /* Condition */
11274 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11275 if (TREE_OPERAND (node, 0) == error_mark_node)
11276 return error_mark_node;
11277 /* then-else branches */
11278 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11279 if (TREE_OPERAND (node, 1) == error_mark_node)
11280 return error_mark_node;
11281 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11282 if (TREE_OPERAND (node, 2) == error_mark_node)
11283 return error_mark_node;
11284 return patch_if_else_statement (node);
11285 break;
11286
11287 case CONDITIONAL_EXPR:
11288 /* Condition */
11289 wfl_op1 = TREE_OPERAND (node, 0);
11290 COMPLETE_CHECK_OP_0 (node);
11291 wfl_op2 = TREE_OPERAND (node, 1);
11292 COMPLETE_CHECK_OP_1 (node);
11293 wfl_op3 = TREE_OPERAND (node, 2);
11294 COMPLETE_CHECK_OP_2 (node);
11295 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11296
11297 /* 3- Expression section */
11298 case COMPOUND_EXPR:
11299 wfl_op2 = TREE_OPERAND (node, 1);
11300 TREE_OPERAND (node, 0) = nn =
11301 java_complete_tree (TREE_OPERAND (node, 0));
11302 if (wfl_op2 == empty_stmt_node)
11303 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11304 else
11305 {
11306 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11307 {
11308 /* An unreachable condition in a do-while statement
11309 is *not* (technically) an unreachable statement. */
11310 nn = wfl_op2;
11311 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11312 nn = EXPR_WFL_NODE (nn);
11313 if (TREE_CODE (nn) != EXIT_EXPR)
11314 {
11315 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11316 parse_error_context (wfl_operator, "Unreachable statement");
11317 }
11318 }
11319 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11320 if (TREE_OPERAND (node, 1) == error_mark_node)
11321 return error_mark_node;
11322 CAN_COMPLETE_NORMALLY (node)
11323 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11324 }
11325 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11326 break;
11327
11328 case RETURN_EXPR:
11329 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11330 return patch_return (node);
11331
11332 case EXPR_WITH_FILE_LOCATION:
11333 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11334 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11335 {
11336 tree wfl = node;
11337 node = resolve_expression_name (node, NULL);
11338 if (node == error_mark_node)
11339 return node;
11340 /* Keep line number information somewhere were it doesn't
11341 disrupt the completion process. */
11342 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11343 {
11344 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11345 TREE_OPERAND (node, 1) = wfl;
11346 }
11347 CAN_COMPLETE_NORMALLY (node) = 1;
11348 }
11349 else
11350 {
11351 tree body;
11352 int save_lineno = lineno;
11353 lineno = EXPR_WFL_LINENO (node);
11354 body = java_complete_tree (EXPR_WFL_NODE (node));
11355 lineno = save_lineno;
11356 EXPR_WFL_NODE (node) = body;
11357 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11358 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11359 if (body == empty_stmt_node || TREE_CONSTANT (body))
11360 {
11361 /* Makes it easier to constant fold, detect empty bodies. */
11362 return body;
11363 }
11364 if (body == error_mark_node)
11365 {
11366 /* Its important for the evaluation of assignment that
11367 this mark on the TREE_TYPE is propagated. */
11368 TREE_TYPE (node) = error_mark_node;
11369 return error_mark_node;
11370 }
11371 else
11372 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11373
11374 }
11375 break;
11376
11377 case NEW_ARRAY_EXPR:
11378 /* Patch all the dimensions */
11379 flag = 0;
11380 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11381 {
11382 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11383 tree dim = convert (int_type_node,
11384 java_complete_tree (TREE_VALUE (cn)));
11385 if (dim == error_mark_node)
11386 {
11387 flag = 1;
11388 continue;
11389 }
11390 else
11391 {
11392 TREE_VALUE (cn) = dim;
11393 /* Setup the location of the current dimension, for
11394 later error report. */
11395 TREE_PURPOSE (cn) =
11396 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11397 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11398 }
11399 }
11400 /* They complete the array creation expression, if no errors
11401 were found. */
11402 CAN_COMPLETE_NORMALLY (node) = 1;
11403 return (flag ? error_mark_node
11404 : force_evaluation_order (patch_newarray (node)));
11405
11406 case NEW_ANONYMOUS_ARRAY_EXPR:
11407 /* Create the array type if necessary. */
11408 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11409 {
11410 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11411 if (!(type = resolve_type_during_patch (type)))
11412 return error_mark_node;
11413 type = build_array_from_name (type, NULL_TREE,
11414 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11415 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11416 }
11417 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11418 ANONYMOUS_ARRAY_INITIALIZER (node));
11419 if (node == error_mark_node)
11420 return error_mark_node;
11421 CAN_COMPLETE_NORMALLY (node) = 1;
11422 return node;
11423
11424 case NEW_CLASS_EXPR:
11425 case CALL_EXPR:
11426 /* Complete function's argument(s) first */
11427 if (complete_function_arguments (node))
11428 return error_mark_node;
11429 else
11430 {
11431 tree decl, wfl = TREE_OPERAND (node, 0);
11432 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11433 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11434 super_identifier_node);
11435
11436 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11437 from_super, 0, &decl);
11438 if (node == error_mark_node)
11439 return error_mark_node;
11440
11441 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11442 /* If we call this(...), register signature and positions */
11443 if (in_this)
11444 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11445 tree_cons (wfl, decl,
11446 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11447 CAN_COMPLETE_NORMALLY (node) = 1;
11448 return force_evaluation_order (node);
11449 }
11450
11451 case MODIFY_EXPR:
11452 /* Save potential wfls */
11453 wfl_op1 = TREE_OPERAND (node, 0);
11454 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11455
11456 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11457 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11458 && DECL_INITIAL (nn) != NULL_TREE)
11459 {
11460 tree value;
11461
11462 value = fold_constant_for_init (nn, nn);
11463
11464 if (value != NULL_TREE)
11465 {
11466 tree type = TREE_TYPE (value);
11467 if (JPRIMITIVE_TYPE_P (type) ||
11468 (type == string_ptr_type_node && ! flag_emit_class_files))
11469 return empty_stmt_node;
11470 }
11471 if (! flag_emit_class_files)
11472 DECL_INITIAL (nn) = NULL_TREE;
11473 if (CLASS_FINAL_VARIABLE_P (nn))
11474 DECL_FIELD_FINAL_IUD (nn) = 0;
11475 }
11476 wfl_op2 = TREE_OPERAND (node, 1);
11477
11478 if (TREE_OPERAND (node, 0) == error_mark_node)
11479 return error_mark_node;
11480
11481 flag = COMPOUND_ASSIGN_P (wfl_op2);
11482 if (flag)
11483 {
11484 /* This might break when accessing outer field from inner
11485 class. TESTME, FIXME */
11486 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11487
11488 /* Hand stabilize the lhs on both places */
11489 TREE_OPERAND (node, 0) = lvalue;
11490 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11491 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11492
11493 /* 15.25.2.a: Left hand is not an array access. FIXME */
11494 /* Now complete the RHS. We write it back later on. */
11495 nn = java_complete_tree (TREE_OPERAND (node, 1));
11496
11497 if ((cn = patch_string (nn)))
11498 nn = cn;
11499
11500 /* The last part of the rewrite for E1 op= E2 is to have
11501 E1 = (T)(E1 op E2), with T being the type of E1. */
11502 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11503 TREE_TYPE (lvalue), nn));
11504
11505 /* If the assignment is compound and has reference type,
11506 then ensure the LHS has type String and nothing else. */
11507 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11508 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11509 parse_error_context (wfl_op2,
11510 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11511 lang_printable_name (TREE_TYPE (lvalue), 0));
11512
11513 /* 15.25.2.b: Left hand is an array access. FIXME */
11514 }
11515
11516 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11517 function to complete this RHS. Note that a NEW_ARRAY_INIT
11518 might have been already fully expanded if created as a result
11519 of processing an anonymous array initializer. We avoid doing
11520 the operation twice by testing whether the node already bears
11521 a type. */
11522 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11523 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11524 TREE_OPERAND (node, 1));
11525 /* Otherwise we simply complete the RHS */
11526 else
11527 nn = java_complete_tree (TREE_OPERAND (node, 1));
11528
11529 if (nn == error_mark_node)
11530 return error_mark_node;
11531
11532 /* Write back the RHS as we evaluated it. */
11533 TREE_OPERAND (node, 1) = nn;
11534
11535 /* In case we're handling = with a String as a RHS, we need to
11536 produce a String out of the RHS (it might still be a
11537 STRING_CST or a StringBuffer at this stage */
11538 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11539 TREE_OPERAND (node, 1) = nn;
11540
11541 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11542 TREE_OPERAND (node, 1))))
11543 {
11544 /* We return error_mark_node if outer_field_access_fix
11545 detects we write into a final. */
11546 if (nn == error_mark_node)
11547 return error_mark_node;
11548 node = nn;
11549 }
11550 else
11551 {
11552 node = patch_assignment (node, wfl_op1, wfl_op2);
11553 /* Reorganize the tree if necessary. */
11554 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11555 || JSTRING_P (TREE_TYPE (node))))
11556 node = java_refold (node);
11557 }
11558
11559 CAN_COMPLETE_NORMALLY (node) = 1;
11560 return node;
11561
11562 case MULT_EXPR:
11563 case PLUS_EXPR:
11564 case MINUS_EXPR:
11565 case LSHIFT_EXPR:
11566 case RSHIFT_EXPR:
11567 case URSHIFT_EXPR:
11568 case BIT_AND_EXPR:
11569 case BIT_XOR_EXPR:
11570 case BIT_IOR_EXPR:
11571 case TRUNC_MOD_EXPR:
11572 case TRUNC_DIV_EXPR:
11573 case RDIV_EXPR:
11574 case TRUTH_ANDIF_EXPR:
11575 case TRUTH_ORIF_EXPR:
11576 case EQ_EXPR:
11577 case NE_EXPR:
11578 case GT_EXPR:
11579 case GE_EXPR:
11580 case LT_EXPR:
11581 case LE_EXPR:
11582 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11583 knows how to handle those cases. */
11584 wfl_op1 = TREE_OPERAND (node, 0);
11585 wfl_op2 = TREE_OPERAND (node, 1);
11586
11587 CAN_COMPLETE_NORMALLY (node) = 1;
11588 /* Don't complete string nodes if dealing with the PLUS operand. */
11589 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11590 {
11591 nn = java_complete_tree (wfl_op1);
11592 if (nn == error_mark_node)
11593 return error_mark_node;
11594
11595 TREE_OPERAND (node, 0) = nn;
11596 }
11597 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11598 {
11599 nn = java_complete_tree (wfl_op2);
11600 if (nn == error_mark_node)
11601 return error_mark_node;
11602
11603 TREE_OPERAND (node, 1) = nn;
11604 }
11605 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11606
11607 case INSTANCEOF_EXPR:
11608 wfl_op1 = TREE_OPERAND (node, 0);
11609 COMPLETE_CHECK_OP_0 (node);
11610 if (flag_emit_xref)
11611 {
11612 TREE_TYPE (node) = boolean_type_node;
11613 return node;
11614 }
11615 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11616
11617 case UNARY_PLUS_EXPR:
11618 case NEGATE_EXPR:
11619 case TRUTH_NOT_EXPR:
11620 case BIT_NOT_EXPR:
11621 case PREDECREMENT_EXPR:
11622 case PREINCREMENT_EXPR:
11623 case POSTDECREMENT_EXPR:
11624 case POSTINCREMENT_EXPR:
11625 case CONVERT_EXPR:
11626 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11627 how to handle those cases. */
11628 wfl_op1 = TREE_OPERAND (node, 0);
11629 CAN_COMPLETE_NORMALLY (node) = 1;
11630 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11631 if (TREE_OPERAND (node, 0) == error_mark_node)
11632 return error_mark_node;
11633 node = patch_unaryop (node, wfl_op1);
11634 CAN_COMPLETE_NORMALLY (node) = 1;
11635 break;
11636
11637 case ARRAY_REF:
11638 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11639 how to handle those cases. */
11640 wfl_op1 = TREE_OPERAND (node, 0);
11641 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11642 if (TREE_OPERAND (node, 0) == error_mark_node)
11643 return error_mark_node;
11644 if (!flag_emit_class_files && !flag_emit_xref)
11645 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11646 /* The same applies to wfl_op2 */
11647 wfl_op2 = TREE_OPERAND (node, 1);
11648 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11649 if (TREE_OPERAND (node, 1) == error_mark_node)
11650 return error_mark_node;
11651 if (!flag_emit_class_files && !flag_emit_xref)
11652 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11653 return patch_array_ref (node);
11654
11655 case RECORD_TYPE:
11656 return node;;
11657
11658 case COMPONENT_REF:
11659 /* The first step in the re-write of qualified name handling. FIXME.
11660 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11661 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11662 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11663 {
11664 tree name = TREE_OPERAND (node, 1);
11665 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11666 if (field == NULL_TREE)
11667 {
11668 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11669 return error_mark_node;
11670 }
11671 if (! FIELD_STATIC (field))
11672 {
11673 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11674 return error_mark_node;
11675 }
11676 return field;
11677 }
11678 else
11679 abort ();
11680 break;
11681
11682 case THIS_EXPR:
11683 /* Can't use THIS in a static environment */
11684 if (!current_this)
11685 {
11686 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11687 parse_error_context (wfl_operator,
11688 "Keyword `this' used outside allowed context");
11689 TREE_TYPE (node) = error_mark_node;
11690 return error_mark_node;
11691 }
11692 if (ctxp->explicit_constructor_p)
11693 {
11694 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11695 parse_error_context
11696 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11697 TREE_TYPE (node) = error_mark_node;
11698 return error_mark_node;
11699 }
11700 return current_this;
11701
11702 case CLASS_LITERAL:
11703 CAN_COMPLETE_NORMALLY (node) = 1;
11704 node = patch_incomplete_class_ref (node);
11705 if (node == error_mark_node)
11706 return error_mark_node;
11707 break;
11708
11709 case INSTANCE_INITIALIZERS_EXPR:
11710 in_instance_initializer++;
11711 node = java_complete_tree (TREE_OPERAND (node, 0));
11712 in_instance_initializer--;
11713 if (node != error_mark_node)
11714 TREE_TYPE (node) = void_type_node;
11715 else
11716 return error_mark_node;
11717 break;
11718
11719 default:
11720 CAN_COMPLETE_NORMALLY (node) = 1;
11721 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11722 and it's time to turn it into the appropriate String object */
11723 if ((nn = patch_string (node)))
11724 node = nn;
11725 else
11726 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
11727 }
11728 return node;
11729 }
11730
11731 /* Complete function call's argument. Return a non zero value is an
11732 error was found. */
11733
11734 static int
11735 complete_function_arguments (node)
11736 tree node;
11737 {
11738 int flag = 0;
11739 tree cn;
11740
11741 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11742 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11743 {
11744 tree wfl = TREE_VALUE (cn), parm, temp;
11745 parm = java_complete_tree (wfl);
11746
11747 if (parm == error_mark_node)
11748 {
11749 flag = 1;
11750 continue;
11751 }
11752 /* If have a string literal that we haven't transformed yet or a
11753 crafted string buffer, as a result of use of the the String
11754 `+' operator. Build `parm.toString()' and expand it. */
11755 if ((temp = patch_string (parm)))
11756 parm = temp;
11757 /* Inline PRIMTYPE.TYPE read access */
11758 parm = maybe_build_primttype_type_ref (parm, wfl);
11759
11760 TREE_VALUE (cn) = parm;
11761 }
11762 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11763 return flag;
11764 }
11765
11766 /* Sometimes (for loops and variable initialized during their
11767 declaration), we want to wrap a statement around a WFL and turn it
11768 debugable. */
11769
11770 static tree
11771 build_debugable_stmt (location, stmt)
11772 int location;
11773 tree stmt;
11774 {
11775 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11776 {
11777 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11778 EXPR_WFL_LINECOL (stmt) = location;
11779 }
11780 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11781 return stmt;
11782 }
11783
11784 static tree
11785 build_expr_block (body, decls)
11786 tree body, decls;
11787 {
11788 tree node = make_node (BLOCK);
11789 BLOCK_EXPR_DECLS (node) = decls;
11790 BLOCK_EXPR_BODY (node) = body;
11791 if (body)
11792 TREE_TYPE (node) = TREE_TYPE (body);
11793 TREE_SIDE_EFFECTS (node) = 1;
11794 return node;
11795 }
11796
11797 /* Create a new function block and link it approriately to current
11798 function block chain */
11799
11800 static tree
11801 enter_block ()
11802 {
11803 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11804 }
11805
11806 /* Link block B supercontext to the previous block. The current
11807 function DECL is used as supercontext when enter_a_block is called
11808 for the first time for a given function. The current function body
11809 (DECL_FUNCTION_BODY) is set to be block B. */
11810
11811 static tree
11812 enter_a_block (b)
11813 tree b;
11814 {
11815 tree fndecl = current_function_decl;
11816
11817 if (!fndecl) {
11818 BLOCK_SUPERCONTEXT (b) = current_static_block;
11819 current_static_block = b;
11820 }
11821
11822 else if (!DECL_FUNCTION_BODY (fndecl))
11823 {
11824 BLOCK_SUPERCONTEXT (b) = fndecl;
11825 DECL_FUNCTION_BODY (fndecl) = b;
11826 }
11827 else
11828 {
11829 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11830 DECL_FUNCTION_BODY (fndecl) = b;
11831 }
11832 return b;
11833 }
11834
11835 /* Exit a block by changing the current function body
11836 (DECL_FUNCTION_BODY) to the current block super context, only if
11837 the block being exited isn't the method's top level one. */
11838
11839 static tree
11840 exit_block ()
11841 {
11842 tree b;
11843 if (current_function_decl)
11844 {
11845 b = DECL_FUNCTION_BODY (current_function_decl);
11846 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11847 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11848 }
11849 else
11850 {
11851 b = current_static_block;
11852
11853 if (BLOCK_SUPERCONTEXT (b))
11854 current_static_block = BLOCK_SUPERCONTEXT (b);
11855 }
11856 return b;
11857 }
11858
11859 /* Lookup for NAME in the nested function's blocks, all the way up to
11860 the current toplevel one. It complies with Java's local variable
11861 scoping rules. */
11862
11863 static tree
11864 lookup_name_in_blocks (name)
11865 tree name;
11866 {
11867 tree b = GET_CURRENT_BLOCK (current_function_decl);
11868
11869 while (b != current_function_decl)
11870 {
11871 tree current;
11872
11873 /* Paranoid sanity check. To be removed */
11874 if (TREE_CODE (b) != BLOCK)
11875 abort ();
11876
11877 for (current = BLOCK_EXPR_DECLS (b); current;
11878 current = TREE_CHAIN (current))
11879 if (DECL_NAME (current) == name)
11880 return current;
11881 b = BLOCK_SUPERCONTEXT (b);
11882 }
11883 return NULL_TREE;
11884 }
11885
11886 static void
11887 maybe_absorb_scoping_blocks ()
11888 {
11889 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
11890 {
11891 tree b = exit_block ();
11892 java_method_add_stmt (current_function_decl, b);
11893 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11894 }
11895 }
11896
11897 \f
11898 /* This section of the source is reserved to build_* functions that
11899 are building incomplete tree nodes and the patch_* functions that
11900 are completing them. */
11901
11902 /* Wrap a non WFL node around a WFL. */
11903 static tree
11904 build_wfl_wrap (node, location)
11905 tree node;
11906 int location;
11907 {
11908 tree wfl, node_to_insert = node;
11909
11910 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11911 with the way we're processing SUPER. A THIS from a primary as a
11912 different form than a SUPER. Turn THIS into something symbolic */
11913 if (TREE_CODE (node) == THIS_EXPR)
11914 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11915 else
11916 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11917
11918 EXPR_WFL_LINECOL (wfl) = location;
11919 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11920 return wfl;
11921 }
11922
11923
11924 /* Build a super() constructor invocation. Returns empty_stmt_node if
11925 we're currently dealing with the class java.lang.Object. */
11926
11927 static tree
11928 build_super_invocation (mdecl)
11929 tree mdecl;
11930 {
11931 if (DECL_CONTEXT (mdecl) == object_type_node)
11932 return empty_stmt_node;
11933 else
11934 {
11935 tree super_wfl = build_wfl_node (super_identifier_node);
11936 tree a = NULL_TREE, t;
11937 /* If we're dealing with an anonymous class, pass the arguments
11938 of the crafted constructor along. */
11939 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11940 {
11941 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11942 for (; t != end_params_node; t = TREE_CHAIN (t))
11943 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11944 }
11945 return build_method_invocation (super_wfl, a);
11946 }
11947 }
11948
11949 /* Build a SUPER/THIS qualified method invocation. */
11950
11951 static tree
11952 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11953 int use_this;
11954 tree name, args;
11955 int lloc, rloc;
11956 {
11957 tree invok;
11958 tree wfl =
11959 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
11960 EXPR_WFL_LINECOL (wfl) = lloc;
11961 invok = build_method_invocation (name, args);
11962 return make_qualified_primary (wfl, invok, rloc);
11963 }
11964
11965 /* Build an incomplete CALL_EXPR node. */
11966
11967 static tree
11968 build_method_invocation (name, args)
11969 tree name;
11970 tree args;
11971 {
11972 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11973 TREE_SIDE_EFFECTS (call) = 1;
11974 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11975 return call;
11976 }
11977
11978 /* Build an incomplete new xxx(...) node. */
11979
11980 static tree
11981 build_new_invocation (name, args)
11982 tree name, args;
11983 {
11984 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11985 TREE_SIDE_EFFECTS (call) = 1;
11986 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11987 return call;
11988 }
11989
11990 /* Build an incomplete assignment expression. */
11991
11992 static tree
11993 build_assignment (op, op_location, lhs, rhs)
11994 int op, op_location;
11995 tree lhs, rhs;
11996 {
11997 tree assignment;
11998 /* Build the corresponding binop if we deal with a Compound
11999 Assignment operator. Mark the binop sub-tree as part of a
12000 Compound Assignment expression */
12001 if (op != ASSIGN_TK)
12002 {
12003 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12004 COMPOUND_ASSIGN_P (rhs) = 1;
12005 }
12006 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12007 TREE_SIDE_EFFECTS (assignment) = 1;
12008 EXPR_WFL_LINECOL (assignment) = op_location;
12009 return assignment;
12010 }
12011
12012 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12013
12014 char *
12015 print_int_node (node)
12016 tree node;
12017 {
12018 static char buffer [80];
12019 if (TREE_CONSTANT_OVERFLOW (node))
12020 sprintf (buffer, "<overflow>");
12021
12022 if (TREE_INT_CST_HIGH (node) == 0)
12023 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12024 TREE_INT_CST_LOW (node));
12025 else if (TREE_INT_CST_HIGH (node) == -1
12026 && TREE_INT_CST_LOW (node) != 0)
12027 {
12028 buffer [0] = '-';
12029 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12030 -TREE_INT_CST_LOW (node));
12031 }
12032 else
12033 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12034 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12035
12036 return buffer;
12037 }
12038
12039 \f
12040
12041 /* This section of the code handle assignment check with FINAL
12042 variables. */
12043
12044 static void
12045 reset_static_final_variable_assignment_flag (class)
12046 tree class;
12047 {
12048 tree field;
12049 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12050 if (CLASS_FINAL_VARIABLE_P (field))
12051 DECL_FIELD_FINAL_LIIC (field) = 0;
12052 }
12053
12054 /* Figure whether all final static variable have been initialized. */
12055
12056 static void
12057 check_static_final_variable_assignment_flag (class)
12058 tree class;
12059 {
12060 tree field;
12061
12062 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12063 if (CLASS_FINAL_VARIABLE_P (field)
12064 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12065 parse_error_context
12066 (DECL_FIELD_FINAL_WFL (field),
12067 "Blank static final variable `%s' may not have been initialized",
12068 IDENTIFIER_POINTER (DECL_NAME (field)));
12069 }
12070
12071 /* This function marks all final variable locally unassigned. */
12072
12073 static void
12074 reset_final_variable_local_assignment_flag (class)
12075 tree class;
12076 {
12077 tree field;
12078 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12079 if (FINAL_VARIABLE_P (field))
12080 DECL_FIELD_FINAL_LIIC (field) = 0;
12081 }
12082
12083 /* Figure whether all final variables have beem initialized in MDECL
12084 and mark MDECL accordingly. */
12085
12086 static void
12087 check_final_variable_local_assignment_flag (class, mdecl)
12088 tree class;
12089 tree mdecl;
12090 {
12091 tree field;
12092 int initialized = 0;
12093 int non_initialized = 0;
12094
12095 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12096 return;
12097
12098 /* First find out whether all final variables or no final variable
12099 are initialized in this ctor. We don't take into account final
12100 variable that have been initialized upon declaration. */
12101 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12102 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12103 {
12104 if (DECL_FIELD_FINAL_LIIC (field))
12105 initialized++;
12106 else
12107 non_initialized++;
12108 }
12109
12110 /* There were no non initialized variable and no initialized variable.
12111 This ctor is fine. */
12112 if (!non_initialized && !initialized)
12113 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12114 /* If no variables have been initialized, that fine. We'll check
12115 later whether this ctor calls a constructor which initializes
12116 them. We mark the ctor as not initializing all its finals. */
12117 else if (initialized == 0)
12118 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12119 /* If we have a mixed bag, then we have a problem. We need to report
12120 all the variables we're not initializing. */
12121 else if (initialized && non_initialized)
12122 {
12123 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12124 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12125 if (FIELD_FINAL (field)
12126 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12127 {
12128 parse_error_context
12129 (lookup_cl (mdecl),
12130 "Blank final variable `%s' may not have been initialized in this constructor",
12131 IDENTIFIER_POINTER (DECL_NAME (field)));
12132 DECL_FIELD_FINAL_IERR (field) = 1;
12133 }
12134 }
12135 /* Otherwise we know this ctor is initializing all its final
12136 variable. We mark it so. */
12137 else
12138 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12139 }
12140
12141 /* This function recurses in a simple what through STMT and stops when
12142 it finds a constructor call. It then verifies that the called
12143 constructor initialized its final properly. Return 1 upon success,
12144 0 or -1 otherwise. */
12145
12146 static int
12147 check_final_variable_indirect_assignment (stmt)
12148 tree stmt;
12149 {
12150 int res;
12151 switch (TREE_CODE (stmt))
12152 {
12153 case EXPR_WITH_FILE_LOCATION:
12154 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12155 case COMPOUND_EXPR:
12156 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12157 if (res)
12158 return res;
12159 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12160 case SAVE_EXPR:
12161 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12162 case CALL_EXPR:
12163 {
12164 tree decl = TREE_OPERAND (stmt, 0);
12165 tree fbody;
12166
12167 if (TREE_CODE (decl) != FUNCTION_DECL)
12168 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12169 if (TREE_CODE (decl) != FUNCTION_DECL)
12170 abort ();
12171 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12172 return 1;
12173 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12174 return -1;
12175 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12176 if (fbody == error_mark_node)
12177 return -1;
12178 fbody = BLOCK_EXPR_BODY (fbody);
12179 return check_final_variable_indirect_assignment (fbody);
12180 }
12181 default:
12182 break;
12183 }
12184 return 0;
12185 }
12186
12187 /* This is the last chance to catch a final variable initialization
12188 problem. This routine will report an error if a final variable was
12189 never (globally) initialized and never reported as not having been
12190 initialized properly. */
12191
12192 static void
12193 check_final_variable_global_assignment_flag (class)
12194 tree class;
12195 {
12196 tree field, mdecl;
12197 int nnctor = 0;
12198
12199 /* We go through all natural ctors and see whether they're
12200 initializing all their final variables or not. */
12201 current_function_decl = NULL_TREE; /* For the error report. */
12202 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12203 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12204 {
12205 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12206 {
12207 /* It doesn't. Maybe it calls a constructor that initializes
12208 them. find out. */
12209 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12210 if (fbody == error_mark_node)
12211 continue;
12212 fbody = BLOCK_EXPR_BODY (fbody);
12213 if (check_final_variable_indirect_assignment (fbody) == 1)
12214 {
12215 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12216 nnctor++;
12217 }
12218 else
12219 parse_error_context
12220 (lookup_cl (mdecl),
12221 "Final variable initialization error in this constructor");
12222 }
12223 else
12224 nnctor++;
12225 }
12226
12227 /* Finally we catch final variables that never were initialized */
12228 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12229 if (FINAL_VARIABLE_P (field)
12230 /* If the field wasn't initialized upon declaration */
12231 && !DECL_FIELD_FINAL_IUD (field)
12232 /* There wasn't natural ctor in which the field could have been
12233 initialized */
12234 && !nnctor
12235 /* If we never reported a problem with this field */
12236 && !DECL_FIELD_FINAL_IERR (field))
12237 {
12238 current_function_decl = NULL;
12239 parse_error_context
12240 (DECL_FIELD_FINAL_WFL (field),
12241 "Final variable `%s' hasn't been initialized upon its declaration",
12242 IDENTIFIER_POINTER (DECL_NAME (field)));
12243 }
12244
12245 }
12246
12247 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12248 context. */
12249
12250 static int
12251 check_final_assignment (lvalue, wfl)
12252 tree lvalue, wfl;
12253 {
12254 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12255 return 0;
12256
12257 if (TREE_CODE (lvalue) == COMPONENT_REF
12258 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12259 lvalue = TREE_OPERAND (lvalue, 1);
12260
12261 if (!FIELD_FINAL (lvalue))
12262 return 0;
12263
12264 /* Now the logic. We can modify a final VARIABLE:
12265 1) in finit$, (its declaration was followed by an initialization,)
12266 2) consistently in each natural ctor, if it wasn't initialized in
12267 finit$ or once in <clinit>. In any other cases, an error should be
12268 reported. */
12269 if (DECL_FINIT_P (current_function_decl))
12270 {
12271 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12272 return 0;
12273 }
12274
12275 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12276 /* Only if it wasn't given a value upon initialization */
12277 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12278 /* If it was never assigned a value in this constructor */
12279 && !DECL_FIELD_FINAL_LIIC (lvalue))
12280 {
12281 /* Turn the locally assigned flag on, it will be checked later
12282 on to point out at discrepancies. */
12283 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12284 if (DECL_CLINIT_P (current_function_decl))
12285 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12286 return 0;
12287 }
12288
12289 /* Other problems should be reported right away. */
12290 parse_error_context
12291 (wfl, "Can't %sassign a value to the final variable `%s'",
12292 (FIELD_STATIC (lvalue) ? "re" : ""),
12293 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12294
12295 /* Note that static field can be initialized once and only once. */
12296 if (FIELD_STATIC (lvalue))
12297 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12298
12299 return 1;
12300 }
12301
12302 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12303 read. This is needed to avoid circularities in the implementation
12304 of these fields in libjava. */
12305
12306 static tree
12307 maybe_build_primttype_type_ref (rhs, wfl)
12308 tree rhs, wfl;
12309 {
12310 tree to_return = NULL_TREE;
12311 tree rhs_type = TREE_TYPE (rhs);
12312 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12313 {
12314 tree n = TREE_OPERAND (rhs, 1);
12315 if (TREE_CODE (n) == VAR_DECL
12316 && DECL_NAME (n) == TYPE_identifier_node
12317 && rhs_type == class_ptr_type
12318 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12319 {
12320 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12321 if (!strncmp (self_name, "java.lang.", 10))
12322 to_return = build_primtype_type_ref (self_name);
12323 }
12324 }
12325 return (to_return ? to_return : rhs );
12326 }
12327
12328 /* 15.25 Assignment operators. */
12329
12330 static tree
12331 patch_assignment (node, wfl_op1, wfl_op2)
12332 tree node;
12333 tree wfl_op1;
12334 tree wfl_op2;
12335 {
12336 tree rhs = TREE_OPERAND (node, 1);
12337 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12338 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12339 int error_found = 0;
12340 int lvalue_from_array = 0;
12341
12342 /* Can't assign to a (blank) final. */
12343 if (check_final_assignment (lvalue, wfl_op1))
12344 error_found = 1;
12345
12346 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12347
12348 /* Lhs can be a named variable */
12349 if (JDECL_P (lvalue))
12350 {
12351 lhs_type = TREE_TYPE (lvalue);
12352 }
12353 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12354 comment on reason why */
12355 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12356 {
12357 lhs_type = TREE_TYPE (lvalue);
12358 lvalue_from_array = 1;
12359 }
12360 /* Or a field access */
12361 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12362 lhs_type = TREE_TYPE (lvalue);
12363 /* Or a function return slot */
12364 else if (TREE_CODE (lvalue) == RESULT_DECL)
12365 lhs_type = TREE_TYPE (lvalue);
12366 /* Otherwise, we might want to try to write into an optimized static
12367 final, this is an of a different nature, reported further on. */
12368 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12369 && resolve_expression_name (wfl_op1, &llvalue))
12370 {
12371 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12372 {
12373 /* What we should do instead is resetting the all the flags
12374 previously set, exchange lvalue for llvalue and continue. */
12375 error_found = 1;
12376 return error_mark_node;
12377 }
12378 else
12379 lhs_type = TREE_TYPE (lvalue);
12380 }
12381 else
12382 {
12383 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12384 error_found = 1;
12385 }
12386
12387 rhs_type = TREE_TYPE (rhs);
12388 /* 5.1 Try the assignment conversion for builtin type. */
12389 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12390
12391 /* 5.2 If it failed, try a reference conversion */
12392 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12393 lhs_type = promote_type (rhs_type);
12394
12395 /* 15.25.2 If we have a compound assignment, convert RHS into the
12396 type of the LHS */
12397 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12398 new_rhs = convert (lhs_type, rhs);
12399
12400 /* Explicit cast required. This is an error */
12401 if (!new_rhs)
12402 {
12403 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12404 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12405 tree wfl;
12406 char operation [32]; /* Max size known */
12407
12408 /* If the assignment is part of a declaration, we use the WFL of
12409 the declared variable to point out the error and call it a
12410 declaration problem. If the assignment is a genuine =
12411 operator, we call is a operator `=' problem, otherwise we
12412 call it an assignment problem. In both of these last cases,
12413 we use the WFL of the operator to indicate the error. */
12414
12415 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12416 {
12417 wfl = wfl_op1;
12418 strcpy (operation, "declaration");
12419 }
12420 else
12421 {
12422 wfl = wfl_operator;
12423 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12424 strcpy (operation, "assignment");
12425 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12426 strcpy (operation, "`return'");
12427 else
12428 strcpy (operation, "`='");
12429 }
12430
12431 if (!valid_cast_to_p (rhs_type, lhs_type))
12432 parse_error_context
12433 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12434 operation, t1, t2);
12435 else
12436 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12437 operation, t1, t2);
12438 free (t1); free (t2);
12439 error_found = 1;
12440 }
12441
12442 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12443 if (new_rhs)
12444 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12445
12446 if (error_found)
12447 return error_mark_node;
12448
12449 /* 10.10: Array Store Exception runtime check */
12450 if (!flag_emit_class_files
12451 && !flag_emit_xref
12452 && lvalue_from_array
12453 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12454 {
12455 tree check;
12456 tree base = lvalue;
12457
12458 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12459 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12460 base = TREE_OPERAND (lvalue, 0);
12461 else
12462 {
12463 if (flag_bounds_check)
12464 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12465 else
12466 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12467 }
12468
12469 /* Build the invocation of _Jv_CheckArrayStore */
12470 new_rhs = save_expr (new_rhs);
12471 check = build (CALL_EXPR, void_type_node,
12472 build_address_of (soft_checkarraystore_node),
12473 tree_cons (NULL_TREE, base,
12474 build_tree_list (NULL_TREE, new_rhs)),
12475 NULL_TREE);
12476 TREE_SIDE_EFFECTS (check) = 1;
12477
12478 /* We have to decide on an insertion point */
12479 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12480 {
12481 tree t;
12482 if (flag_bounds_check)
12483 {
12484 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12485 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12486 build (COMPOUND_EXPR, void_type_node, t, check);
12487 }
12488 else
12489 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12490 check, TREE_OPERAND (lvalue, 1));
12491 }
12492 else
12493 {
12494 /* Make sure the bound check will happen before the store check */
12495 if (flag_bounds_check)
12496 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12497 build (COMPOUND_EXPR, void_type_node,
12498 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12499 else
12500 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12501 }
12502 }
12503
12504 /* Final locals can be used as case values in switch
12505 statement. Prepare them for this eventuality. */
12506 if (TREE_CODE (lvalue) == VAR_DECL
12507 && LOCAL_FINAL_P (lvalue)
12508 && TREE_CONSTANT (new_rhs)
12509 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12510 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12511 )
12512 {
12513 TREE_CONSTANT (lvalue) = 1;
12514 DECL_INITIAL (lvalue) = new_rhs;
12515 }
12516
12517 TREE_OPERAND (node, 0) = lvalue;
12518 TREE_OPERAND (node, 1) = new_rhs;
12519 TREE_TYPE (node) = lhs_type;
12520 return node;
12521 }
12522
12523 /* Check that type SOURCE can be cast into type DEST. If the cast
12524 can't occur at all, return 0 otherwise 1. This function is used to
12525 produce accurate error messages on the reasons why an assignment
12526 failed. */
12527
12528 static tree
12529 try_reference_assignconv (lhs_type, rhs)
12530 tree lhs_type, rhs;
12531 {
12532 tree new_rhs = NULL_TREE;
12533 tree rhs_type = TREE_TYPE (rhs);
12534
12535 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12536 {
12537 /* `null' may be assigned to any reference type */
12538 if (rhs == null_pointer_node)
12539 new_rhs = null_pointer_node;
12540 /* Try the reference assignment conversion */
12541 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12542 new_rhs = rhs;
12543 /* This is a magic assignment that we process differently */
12544 else if (rhs == soft_exceptioninfo_call_node)
12545 new_rhs = rhs;
12546 }
12547 return new_rhs;
12548 }
12549
12550 /* Check that RHS can be converted into LHS_TYPE by the assignment
12551 conversion (5.2), for the cases of RHS being a builtin type. Return
12552 NULL_TREE if the conversion fails or if because RHS isn't of a
12553 builtin type. Return a converted RHS if the conversion is possible. */
12554
12555 static tree
12556 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12557 tree wfl_op1, lhs_type, rhs;
12558 {
12559 tree new_rhs = NULL_TREE;
12560 tree rhs_type = TREE_TYPE (rhs);
12561
12562 /* Handle boolean specially. */
12563 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12564 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12565 {
12566 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12567 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12568 new_rhs = rhs;
12569 }
12570
12571 /* Zero accepted everywhere */
12572 else if (TREE_CODE (rhs) == INTEGER_CST
12573 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12574 && JPRIMITIVE_TYPE_P (rhs_type))
12575 new_rhs = convert (lhs_type, rhs);
12576
12577 /* 5.1.1 Try Identity Conversion,
12578 5.1.2 Try Widening Primitive Conversion */
12579 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12580 new_rhs = convert (lhs_type, rhs);
12581
12582 /* Try a narrowing primitive conversion (5.1.3):
12583 - expression is a constant expression of type int AND
12584 - variable is byte, short or char AND
12585 - The value of the expression is representable in the type of the
12586 variable */
12587 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12588 && (lhs_type == byte_type_node || lhs_type == char_type_node
12589 || lhs_type == short_type_node))
12590 {
12591 if (int_fits_type_p (rhs, lhs_type))
12592 new_rhs = convert (lhs_type, rhs);
12593 else if (wfl_op1) /* Might be called with a NULL */
12594 parse_warning_context
12595 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12596 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12597 /* Reported a warning that will turn into an error further
12598 down, so we don't return */
12599 }
12600
12601 return new_rhs;
12602 }
12603
12604 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12605 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12606 0 is the conversion test fails. This implements parts the method
12607 invocation convertion (5.3). */
12608
12609 static int
12610 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12611 tree lhs_type, rhs_type;
12612 {
12613 /* 5.1.1: This is the identity conversion part. */
12614 if (lhs_type == rhs_type)
12615 return 1;
12616
12617 /* Reject non primitive types and boolean conversions. */
12618 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12619 return 0;
12620
12621 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12622 than a char can't be converted into a char. Short can't too, but
12623 the < test below takes care of that */
12624 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12625 return 0;
12626
12627 /* Accept all promoted type here. Note, we can't use <= in the test
12628 below, because we still need to bounce out assignments of short
12629 to char and the likes */
12630 if (lhs_type == int_type_node
12631 && (rhs_type == promoted_byte_type_node
12632 || rhs_type == promoted_short_type_node
12633 || rhs_type == promoted_char_type_node
12634 || rhs_type == promoted_boolean_type_node))
12635 return 1;
12636
12637 /* From here, an integral is widened if its precision is smaller
12638 than the precision of the LHS or if the LHS is a floating point
12639 type, or the RHS is a float and the RHS a double. */
12640 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12641 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12642 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12643 || (rhs_type == float_type_node && lhs_type == double_type_node))
12644 return 1;
12645
12646 return 0;
12647 }
12648
12649 /* Check that something of SOURCE type can be assigned or cast to
12650 something of DEST type at runtime. Return 1 if the operation is
12651 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12652 were SOURCE is cast into DEST, which borrows a lot of the
12653 assignment check. */
12654
12655 static int
12656 valid_ref_assignconv_cast_p (source, dest, cast)
12657 tree source;
12658 tree dest;
12659 int cast;
12660 {
12661 /* SOURCE or DEST might be null if not from a declared entity. */
12662 if (!source || !dest)
12663 return 0;
12664 if (JNULLP_TYPE_P (source))
12665 return 1;
12666 if (TREE_CODE (source) == POINTER_TYPE)
12667 source = TREE_TYPE (source);
12668 if (TREE_CODE (dest) == POINTER_TYPE)
12669 dest = TREE_TYPE (dest);
12670
12671 /* If source and dest are being compiled from bytecode, they may need to
12672 be loaded. */
12673 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12674 {
12675 load_class (source, 1);
12676 safe_layout_class (source);
12677 }
12678 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12679 {
12680 load_class (dest, 1);
12681 safe_layout_class (dest);
12682 }
12683
12684 /* Case where SOURCE is a class type */
12685 if (TYPE_CLASS_P (source))
12686 {
12687 if (TYPE_CLASS_P (dest))
12688 return (source == dest
12689 || inherits_from_p (source, dest)
12690 || (cast && inherits_from_p (dest, source)));
12691 if (TYPE_INTERFACE_P (dest))
12692 {
12693 /* If doing a cast and SOURCE is final, the operation is
12694 always correct a compile time (because even if SOURCE
12695 does not implement DEST, a subclass of SOURCE might). */
12696 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12697 return 1;
12698 /* Otherwise, SOURCE must implement DEST */
12699 return interface_of_p (dest, source);
12700 }
12701 /* DEST is an array, cast permited if SOURCE is of Object type */
12702 return (cast && source == object_type_node ? 1 : 0);
12703 }
12704 if (TYPE_INTERFACE_P (source))
12705 {
12706 if (TYPE_CLASS_P (dest))
12707 {
12708 /* If not casting, DEST must be the Object type */
12709 if (!cast)
12710 return dest == object_type_node;
12711 /* We're doing a cast. The cast is always valid is class
12712 DEST is not final, otherwise, DEST must implement SOURCE */
12713 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12714 return 1;
12715 else
12716 return interface_of_p (source, dest);
12717 }
12718 if (TYPE_INTERFACE_P (dest))
12719 {
12720 /* If doing a cast, then if SOURCE and DEST contain method
12721 with the same signature but different return type, then
12722 this is a (compile time) error */
12723 if (cast)
12724 {
12725 tree method_source, method_dest;
12726 tree source_type;
12727 tree source_sig;
12728 tree source_name;
12729 for (method_source = TYPE_METHODS (source); method_source;
12730 method_source = TREE_CHAIN (method_source))
12731 {
12732 source_sig =
12733 build_java_argument_signature (TREE_TYPE (method_source));
12734 source_type = TREE_TYPE (TREE_TYPE (method_source));
12735 source_name = DECL_NAME (method_source);
12736 for (method_dest = TYPE_METHODS (dest);
12737 method_dest; method_dest = TREE_CHAIN (method_dest))
12738 if (source_sig ==
12739 build_java_argument_signature (TREE_TYPE (method_dest))
12740 && source_name == DECL_NAME (method_dest)
12741 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12742 return 0;
12743 }
12744 return 1;
12745 }
12746 else
12747 return source == dest || interface_of_p (dest, source);
12748 }
12749 else
12750 {
12751 /* Array */
12752 return (cast
12753 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12754 || (DECL_NAME (TYPE_NAME (source))
12755 == java_io_serializable)));
12756 }
12757 }
12758 if (TYPE_ARRAY_P (source))
12759 {
12760 if (TYPE_CLASS_P (dest))
12761 return dest == object_type_node;
12762 /* Can't cast an array to an interface unless the interface is
12763 java.lang.Cloneable or java.io.Serializable. */
12764 if (TYPE_INTERFACE_P (dest))
12765 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12766 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12767 else /* Arrays */
12768 {
12769 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12770 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12771
12772 /* In case of severe errors, they turn out null */
12773 if (!dest_element_type || !source_element_type)
12774 return 0;
12775 if (source_element_type == dest_element_type)
12776 return 1;
12777 return valid_ref_assignconv_cast_p (source_element_type,
12778 dest_element_type, cast);
12779 }
12780 return 0;
12781 }
12782 return 0;
12783 }
12784
12785 static int
12786 valid_cast_to_p (source, dest)
12787 tree source;
12788 tree dest;
12789 {
12790 if (TREE_CODE (source) == POINTER_TYPE)
12791 source = TREE_TYPE (source);
12792 if (TREE_CODE (dest) == POINTER_TYPE)
12793 dest = TREE_TYPE (dest);
12794
12795 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12796 return valid_ref_assignconv_cast_p (source, dest, 1);
12797
12798 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12799 return 1;
12800
12801 else if (TREE_CODE (source) == BOOLEAN_TYPE
12802 && TREE_CODE (dest) == BOOLEAN_TYPE)
12803 return 1;
12804
12805 return 0;
12806 }
12807
12808 static tree
12809 do_unary_numeric_promotion (arg)
12810 tree arg;
12811 {
12812 tree type = TREE_TYPE (arg);
12813 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12814 || TREE_CODE (type) == CHAR_TYPE)
12815 arg = convert (int_type_node, arg);
12816 return arg;
12817 }
12818
12819 /* Return a non zero value if SOURCE can be converted into DEST using
12820 the method invocation conversion rule (5.3). */
12821 static int
12822 valid_method_invocation_conversion_p (dest, source)
12823 tree dest, source;
12824 {
12825 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12826 && valid_builtin_assignconv_identity_widening_p (dest, source))
12827 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12828 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12829 && valid_ref_assignconv_cast_p (source, dest, 0)));
12830 }
12831
12832 /* Build an incomplete binop expression. */
12833
12834 static tree
12835 build_binop (op, op_location, op1, op2)
12836 enum tree_code op;
12837 int op_location;
12838 tree op1, op2;
12839 {
12840 tree binop = build (op, NULL_TREE, op1, op2);
12841 TREE_SIDE_EFFECTS (binop) = 1;
12842 /* Store the location of the operator, for better error report. The
12843 string of the operator will be rebuild based on the OP value. */
12844 EXPR_WFL_LINECOL (binop) = op_location;
12845 return binop;
12846 }
12847
12848 /* Build the string of the operator retained by NODE. If NODE is part
12849 of a compound expression, add an '=' at the end of the string. This
12850 function is called when an error needs to be reported on an
12851 operator. The string is returned as a pointer to a static character
12852 buffer. */
12853
12854 static char *
12855 operator_string (node)
12856 tree node;
12857 {
12858 #define BUILD_OPERATOR_STRING(S) \
12859 { \
12860 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12861 return buffer; \
12862 }
12863
12864 static char buffer [10];
12865 switch (TREE_CODE (node))
12866 {
12867 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12868 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12869 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12870 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12871 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12872 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12873 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12874 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12875 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12876 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12877 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12878 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12879 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12880 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12881 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12882 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12883 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12884 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12885 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12886 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12887 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12888 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12889 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12890 case PREINCREMENT_EXPR: /* Fall through */
12891 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12892 case PREDECREMENT_EXPR: /* Fall through */
12893 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12894 default:
12895 internal_error ("unregistered operator %s",
12896 tree_code_name [TREE_CODE (node)]);
12897 }
12898 return NULL;
12899 #undef BUILD_OPERATOR_STRING
12900 }
12901
12902 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12903
12904 static int
12905 java_decl_equiv (var_acc1, var_acc2)
12906 tree var_acc1, var_acc2;
12907 {
12908 if (JDECL_P (var_acc1))
12909 return (var_acc1 == var_acc2);
12910
12911 return (TREE_CODE (var_acc1) == COMPONENT_REF
12912 && TREE_CODE (var_acc2) == COMPONENT_REF
12913 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12914 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12915 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12916 }
12917
12918 /* Return a non zero value if CODE is one of the operators that can be
12919 used in conjunction with the `=' operator in a compound assignment. */
12920
12921 static int
12922 binop_compound_p (code)
12923 enum tree_code code;
12924 {
12925 int i;
12926 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12927 if (binop_lookup [i] == code)
12928 break;
12929
12930 return i < BINOP_COMPOUND_CANDIDATES;
12931 }
12932
12933 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12934
12935 static tree
12936 java_refold (t)
12937 tree t;
12938 {
12939 tree c, b, ns, decl;
12940
12941 if (TREE_CODE (t) != MODIFY_EXPR)
12942 return t;
12943
12944 c = TREE_OPERAND (t, 1);
12945 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12946 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12947 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12948 return t;
12949
12950 /* Now the left branch of the binary operator. */
12951 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12952 if (! (b && TREE_CODE (b) == NOP_EXPR
12953 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12954 return t;
12955
12956 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12957 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12958 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12959 return t;
12960
12961 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12962 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12963 /* It's got to be the an equivalent decl */
12964 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12965 {
12966 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12967 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12968 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12969 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12970 /* Change the right part of the BINOP_EXPR */
12971 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12972 }
12973
12974 return t;
12975 }
12976
12977 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
12978 errors but we modify NODE so that it contains the type computed
12979 according to the expression, when it's fixed. Otherwise, we write
12980 error_mark_node as the type. It allows us to further the analysis
12981 of remaining nodes and detects more errors in certain cases. */
12982
12983 static tree
12984 patch_binop (node, wfl_op1, wfl_op2)
12985 tree node;
12986 tree wfl_op1;
12987 tree wfl_op2;
12988 {
12989 tree op1 = TREE_OPERAND (node, 0);
12990 tree op2 = TREE_OPERAND (node, 1);
12991 tree op1_type = TREE_TYPE (op1);
12992 tree op2_type = TREE_TYPE (op2);
12993 tree prom_type = NULL_TREE, cn;
12994 int code = TREE_CODE (node);
12995
12996 /* If 1, tell the routine that we have to return error_mark_node
12997 after checking for the initialization of the RHS */
12998 int error_found = 0;
12999
13000 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13001
13002 /* If either op<n>_type are NULL, this might be early signs of an
13003 error situation, unless it's too early to tell (in case we're
13004 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13005 correctly so the error can be later on reported accurately. */
13006 if (! (code == PLUS_EXPR || code == NE_EXPR
13007 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13008 {
13009 tree n;
13010 if (! op1_type)
13011 {
13012 n = java_complete_tree (op1);
13013 op1_type = TREE_TYPE (n);
13014 }
13015 if (! op2_type)
13016 {
13017 n = java_complete_tree (op2);
13018 op2_type = TREE_TYPE (n);
13019 }
13020 }
13021
13022 switch (code)
13023 {
13024 /* 15.16 Multiplicative operators */
13025 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13026 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13027 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13028 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13029 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13030 {
13031 if (!JNUMERIC_TYPE_P (op1_type))
13032 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13033 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13034 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13035 TREE_TYPE (node) = error_mark_node;
13036 error_found = 1;
13037 break;
13038 }
13039 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13040 /* Change the division operator if necessary */
13041 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13042 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13043
13044 if (TREE_CODE (prom_type) == INTEGER_TYPE
13045 && flag_use_divide_subroutine
13046 && ! flag_emit_class_files
13047 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13048 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13049
13050 /* This one is more complicated. FLOATs are processed by a
13051 function call to soft_fmod. Duplicate the value of the
13052 COMPOUND_ASSIGN_P flag. */
13053 if (code == TRUNC_MOD_EXPR)
13054 {
13055 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13056 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13057 TREE_SIDE_EFFECTS (mod)
13058 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13059 return mod;
13060 }
13061 break;
13062
13063 /* 15.17 Additive Operators */
13064 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13065
13066 /* Operation is valid if either one argument is a string
13067 constant, a String object or a StringBuffer crafted for the
13068 purpose of the a previous usage of the String concatenation
13069 operator */
13070
13071 if (TREE_CODE (op1) == STRING_CST
13072 || TREE_CODE (op2) == STRING_CST
13073 || JSTRING_TYPE_P (op1_type)
13074 || JSTRING_TYPE_P (op2_type)
13075 || IS_CRAFTED_STRING_BUFFER_P (op1)
13076 || IS_CRAFTED_STRING_BUFFER_P (op2))
13077 return build_string_concatenation (op1, op2);
13078
13079 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13080 Numeric Types */
13081 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13082 {
13083 if (!JNUMERIC_TYPE_P (op1_type))
13084 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13085 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13086 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13087 TREE_TYPE (node) = error_mark_node;
13088 error_found = 1;
13089 break;
13090 }
13091 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13092 break;
13093
13094 /* 15.18 Shift Operators */
13095 case LSHIFT_EXPR:
13096 case RSHIFT_EXPR:
13097 case URSHIFT_EXPR:
13098 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13099 {
13100 if (!JINTEGRAL_TYPE_P (op1_type))
13101 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13102 else
13103 {
13104 if (JNUMERIC_TYPE_P (op2_type))
13105 parse_error_context (wfl_operator,
13106 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13107 operator_string (node),
13108 lang_printable_name (op2_type, 0));
13109 else
13110 parse_error_context (wfl_operator,
13111 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13112 operator_string (node),
13113 lang_printable_name (op2_type, 0));
13114 }
13115 TREE_TYPE (node) = error_mark_node;
13116 error_found = 1;
13117 break;
13118 }
13119
13120 /* Unary numeric promotion (5.6.1) is performed on each operand
13121 separatly */
13122 op1 = do_unary_numeric_promotion (op1);
13123 op2 = do_unary_numeric_promotion (op2);
13124
13125 /* The type of the shift expression is the type of the promoted
13126 type of the left-hand operand */
13127 prom_type = TREE_TYPE (op1);
13128
13129 /* Shift int only up to 0x1f and long up to 0x3f */
13130 if (prom_type == int_type_node)
13131 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13132 build_int_2 (0x1f, 0)));
13133 else
13134 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13135 build_int_2 (0x3f, 0)));
13136
13137 /* The >>> operator is a >> operating on unsigned quantities */
13138 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13139 {
13140 tree to_return;
13141 tree utype = unsigned_type (prom_type);
13142 op1 = convert (utype, op1);
13143 TREE_SET_CODE (node, RSHIFT_EXPR);
13144 TREE_OPERAND (node, 0) = op1;
13145 TREE_OPERAND (node, 1) = op2;
13146 TREE_TYPE (node) = utype;
13147 to_return = convert (prom_type, node);
13148 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13149 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13150 TREE_SIDE_EFFECTS (to_return)
13151 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13152 return to_return;
13153 }
13154 break;
13155
13156 /* 15.19.1 Type Comparison Operator instaceof */
13157 case INSTANCEOF_EXPR:
13158
13159 TREE_TYPE (node) = boolean_type_node;
13160
13161 if (!(op2_type = resolve_type_during_patch (op2)))
13162 return error_mark_node;
13163
13164 /* The first operand must be a reference type or the null type */
13165 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13166 error_found = 1; /* Error reported further below */
13167
13168 /* The second operand must be a reference type */
13169 if (!JREFERENCE_TYPE_P (op2_type))
13170 {
13171 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13172 parse_error_context
13173 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13174 lang_printable_name (op2_type, 0));
13175 error_found = 1;
13176 }
13177
13178 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13179 {
13180 /* If the first operand is null, the result is always false */
13181 if (op1 == null_pointer_node)
13182 return boolean_false_node;
13183 else if (flag_emit_class_files)
13184 {
13185 TREE_OPERAND (node, 1) = op2_type;
13186 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13187 return node;
13188 }
13189 /* Otherwise we have to invoke instance of to figure it out */
13190 else
13191 return build_instanceof (op1, op2_type);
13192 }
13193 /* There is no way the expression operand can be an instance of
13194 the type operand. This is a compile time error. */
13195 else
13196 {
13197 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13198 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13199 parse_error_context
13200 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13201 t1, lang_printable_name (op2_type, 0));
13202 free (t1);
13203 error_found = 1;
13204 }
13205
13206 break;
13207
13208 /* 15.21 Bitwise and Logical Operators */
13209 case BIT_AND_EXPR:
13210 case BIT_XOR_EXPR:
13211 case BIT_IOR_EXPR:
13212 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13213 /* Binary numeric promotion is performed on both operand and the
13214 expression retain that type */
13215 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13216
13217 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13218 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13219 /* The type of the bitwise operator expression is BOOLEAN */
13220 prom_type = boolean_type_node;
13221 else
13222 {
13223 if (!JINTEGRAL_TYPE_P (op1_type))
13224 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13225 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13226 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13227 TREE_TYPE (node) = error_mark_node;
13228 error_found = 1;
13229 /* Insert a break here if adding thing before the switch's
13230 break for this case */
13231 }
13232 break;
13233
13234 /* 15.22 Conditional-And Operator */
13235 case TRUTH_ANDIF_EXPR:
13236 /* 15.23 Conditional-Or Operator */
13237 case TRUTH_ORIF_EXPR:
13238 /* Operands must be of BOOLEAN type */
13239 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13240 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13241 {
13242 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13243 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13244 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13245 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13246 TREE_TYPE (node) = boolean_type_node;
13247 error_found = 1;
13248 break;
13249 }
13250 /* The type of the conditional operators is BOOLEAN */
13251 prom_type = boolean_type_node;
13252 break;
13253
13254 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13255 case LT_EXPR:
13256 case GT_EXPR:
13257 case LE_EXPR:
13258 case GE_EXPR:
13259 /* The type of each of the operands must be a primitive numeric
13260 type */
13261 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13262 {
13263 if (!JNUMERIC_TYPE_P (op1_type))
13264 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13265 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13266 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13267 TREE_TYPE (node) = boolean_type_node;
13268 error_found = 1;
13269 break;
13270 }
13271 /* Binary numeric promotion is performed on the operands */
13272 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13273 /* The type of the relation expression is always BOOLEAN */
13274 prom_type = boolean_type_node;
13275 break;
13276
13277 /* 15.20 Equality Operator */
13278 case EQ_EXPR:
13279 case NE_EXPR:
13280 /* It's time for us to patch the strings. */
13281 if ((cn = patch_string (op1)))
13282 {
13283 op1 = cn;
13284 op1_type = TREE_TYPE (op1);
13285 }
13286 if ((cn = patch_string (op2)))
13287 {
13288 op2 = cn;
13289 op2_type = TREE_TYPE (op2);
13290 }
13291
13292 /* 15.20.1 Numerical Equality Operators == and != */
13293 /* Binary numeric promotion is performed on the operands */
13294 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13295 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13296
13297 /* 15.20.2 Boolean Equality Operators == and != */
13298 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13299 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13300 ; /* Nothing to do here */
13301
13302 /* 15.20.3 Reference Equality Operators == and != */
13303 /* Types have to be either references or the null type. If
13304 they're references, it must be possible to convert either
13305 type to the other by casting conversion. */
13306 else if (op1 == null_pointer_node || op2 == null_pointer_node
13307 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13308 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13309 || valid_ref_assignconv_cast_p (op2_type,
13310 op1_type, 1))))
13311 ; /* Nothing to do here */
13312
13313 /* Else we have an error figure what can't be converted into
13314 what and report the error */
13315 else
13316 {
13317 char *t1;
13318 t1 = xstrdup (lang_printable_name (op1_type, 0));
13319 parse_error_context
13320 (wfl_operator,
13321 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13322 operator_string (node), t1,
13323 lang_printable_name (op2_type, 0));
13324 free (t1);
13325 TREE_TYPE (node) = boolean_type_node;
13326 error_found = 1;
13327 break;
13328 }
13329 prom_type = boolean_type_node;
13330 break;
13331 }
13332
13333 if (error_found)
13334 return error_mark_node;
13335
13336 TREE_OPERAND (node, 0) = op1;
13337 TREE_OPERAND (node, 1) = op2;
13338 TREE_TYPE (node) = prom_type;
13339 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13340
13341 if (flag_emit_xref)
13342 return node;
13343
13344 /* fold does not respect side-effect order as required for Java but not C.
13345 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13346 * bytecode.
13347 */
13348 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13349 : ! TREE_SIDE_EFFECTS (node))
13350 node = fold (node);
13351 return node;
13352 }
13353
13354 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13355 zero value, the value of CSTE comes after the valude of STRING */
13356
13357 static tree
13358 do_merge_string_cste (cste, string, string_len, after)
13359 tree cste;
13360 const char *string;
13361 int string_len, after;
13362 {
13363 const char *old = TREE_STRING_POINTER (cste);
13364 int old_len = TREE_STRING_LENGTH (cste);
13365 int len = old_len + string_len;
13366 char *new = alloca (len+1);
13367
13368 if (after)
13369 {
13370 memcpy (new, string, string_len);
13371 memcpy (&new [string_len], old, old_len);
13372 }
13373 else
13374 {
13375 memcpy (new, old, old_len);
13376 memcpy (&new [old_len], string, string_len);
13377 }
13378 new [len] = '\0';
13379 return build_string (len, new);
13380 }
13381
13382 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13383 new STRING_CST on success, NULL_TREE on failure */
13384
13385 static tree
13386 merge_string_cste (op1, op2, after)
13387 tree op1, op2;
13388 int after;
13389 {
13390 /* Handle two string constants right away */
13391 if (TREE_CODE (op2) == STRING_CST)
13392 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13393 TREE_STRING_LENGTH (op2), after);
13394
13395 /* Reasonable integer constant can be treated right away */
13396 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13397 {
13398 static const char *boolean_true = "true";
13399 static const char *boolean_false = "false";
13400 static const char *null_pointer = "null";
13401 char ch[3];
13402 const char *string;
13403
13404 if (op2 == boolean_true_node)
13405 string = boolean_true;
13406 else if (op2 == boolean_false_node)
13407 string = boolean_false;
13408 else if (op2 == null_pointer_node)
13409 string = null_pointer;
13410 else if (TREE_TYPE (op2) == char_type_node)
13411 {
13412 ch[0] = (char )TREE_INT_CST_LOW (op2);
13413 ch[1] = '\0';
13414 string = ch;
13415 }
13416 else
13417 string = print_int_node (op2);
13418
13419 return do_merge_string_cste (op1, string, strlen (string), after);
13420 }
13421 return NULL_TREE;
13422 }
13423
13424 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13425 has to be a STRING_CST and the other part must be a STRING_CST or a
13426 INTEGRAL constant. Return a new STRING_CST if the operation
13427 succeed, NULL_TREE otherwise.
13428
13429 If the case we want to optimize for space, we might want to return
13430 NULL_TREE for each invocation of this routine. FIXME */
13431
13432 static tree
13433 string_constant_concatenation (op1, op2)
13434 tree op1, op2;
13435 {
13436 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13437 {
13438 tree string, rest;
13439 int invert;
13440
13441 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13442 rest = (string == op1 ? op2 : op1);
13443 invert = (string == op1 ? 0 : 1 );
13444
13445 /* Walk REST, only if it looks reasonable */
13446 if (TREE_CODE (rest) != STRING_CST
13447 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13448 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13449 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13450 {
13451 rest = java_complete_tree (rest);
13452 if (rest == error_mark_node)
13453 return error_mark_node;
13454 rest = fold (rest);
13455 }
13456 return merge_string_cste (string, rest, invert);
13457 }
13458 return NULL_TREE;
13459 }
13460
13461 /* Implement the `+' operator. Does static optimization if possible,
13462 otherwise create (if necessary) and append elements to a
13463 StringBuffer. The StringBuffer will be carried around until it is
13464 used for a function call or an assignment. Then toString() will be
13465 called on it to turn it into a String object. */
13466
13467 static tree
13468 build_string_concatenation (op1, op2)
13469 tree op1, op2;
13470 {
13471 tree result;
13472 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13473
13474 if (flag_emit_xref)
13475 return build (PLUS_EXPR, string_type_node, op1, op2);
13476
13477 /* Try to do some static optimization */
13478 if ((result = string_constant_concatenation (op1, op2)))
13479 return result;
13480
13481 /* Discard empty strings on either side of the expression */
13482 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13483 {
13484 op1 = op2;
13485 op2 = NULL_TREE;
13486 }
13487 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13488 op2 = NULL_TREE;
13489
13490 /* If operands are string constant, turn then into object references */
13491 if (TREE_CODE (op1) == STRING_CST)
13492 op1 = patch_string_cst (op1);
13493 if (op2 && TREE_CODE (op2) == STRING_CST)
13494 op2 = patch_string_cst (op2);
13495
13496 /* If either one of the constant is null and the other non null
13497 operand is a String object, return it. */
13498 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13499 return op1;
13500
13501 /* If OP1 isn't already a StringBuffer, create and
13502 initialize a new one */
13503 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13504 {
13505 /* Two solutions here:
13506 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13507 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13508 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13509 op1 = BUILD_STRING_BUFFER (op1);
13510 else
13511 {
13512 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13513 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13514 }
13515 }
13516
13517 if (op2)
13518 {
13519 /* OP1 is no longer the last node holding a crafted StringBuffer */
13520 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13521 /* Create a node for `{new...,xxx}.append (op2)' */
13522 if (op2)
13523 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13524 }
13525
13526 /* Mark the last node holding a crafted StringBuffer */
13527 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13528
13529 TREE_SIDE_EFFECTS (op1) = side_effects;
13530 return op1;
13531 }
13532
13533 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13534 StringBuffer. If no string were found to be patched, return
13535 NULL. */
13536
13537 static tree
13538 patch_string (node)
13539 tree node;
13540 {
13541 if (node == error_mark_node)
13542 return error_mark_node;
13543 if (TREE_CODE (node) == STRING_CST)
13544 return patch_string_cst (node);
13545 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13546 {
13547 int saved = ctxp->explicit_constructor_p;
13548 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13549 tree ret;
13550 /* Temporary disable forbid the use of `this'. */
13551 ctxp->explicit_constructor_p = 0;
13552 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13553 /* String concatenation arguments must be evaluated in order too. */
13554 ret = force_evaluation_order (ret);
13555 /* Restore it at its previous value */
13556 ctxp->explicit_constructor_p = saved;
13557 return ret;
13558 }
13559 return NULL_TREE;
13560 }
13561
13562 /* Build the internal representation of a string constant. */
13563
13564 static tree
13565 patch_string_cst (node)
13566 tree node;
13567 {
13568 int location;
13569 if (! flag_emit_class_files)
13570 {
13571 node = get_identifier (TREE_STRING_POINTER (node));
13572 location = alloc_name_constant (CONSTANT_String, node);
13573 node = build_ref_from_constant_pool (location);
13574 }
13575 TREE_TYPE (node) = string_ptr_type_node;
13576 TREE_CONSTANT (node) = 1;
13577 return node;
13578 }
13579
13580 /* Build an incomplete unary operator expression. */
13581
13582 static tree
13583 build_unaryop (op_token, op_location, op1)
13584 int op_token, op_location;
13585 tree op1;
13586 {
13587 enum tree_code op;
13588 tree unaryop;
13589 switch (op_token)
13590 {
13591 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13592 case MINUS_TK: op = NEGATE_EXPR; break;
13593 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13594 case NOT_TK: op = BIT_NOT_EXPR; break;
13595 default: abort ();
13596 }
13597
13598 unaryop = build1 (op, NULL_TREE, op1);
13599 TREE_SIDE_EFFECTS (unaryop) = 1;
13600 /* Store the location of the operator, for better error report. The
13601 string of the operator will be rebuild based on the OP value. */
13602 EXPR_WFL_LINECOL (unaryop) = op_location;
13603 return unaryop;
13604 }
13605
13606 /* Special case for the ++/-- operators, since they require an extra
13607 argument to build, which is set to NULL and patched
13608 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13609
13610 static tree
13611 build_incdec (op_token, op_location, op1, is_post_p)
13612 int op_token, op_location;
13613 tree op1;
13614 int is_post_p;
13615 {
13616 static enum tree_code lookup [2][2] =
13617 {
13618 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13619 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13620 };
13621 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13622 NULL_TREE, op1, NULL_TREE);
13623 TREE_SIDE_EFFECTS (node) = 1;
13624 /* Store the location of the operator, for better error report. The
13625 string of the operator will be rebuild based on the OP value. */
13626 EXPR_WFL_LINECOL (node) = op_location;
13627 return node;
13628 }
13629
13630 /* Build an incomplete cast operator, based on the use of the
13631 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13632 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13633 though its type is already set. */
13634
13635 static tree
13636 build_cast (location, type, exp)
13637 int location;
13638 tree type, exp;
13639 {
13640 tree node = build1 (CONVERT_EXPR, type, exp);
13641 EXPR_WFL_LINECOL (node) = location;
13642 return node;
13643 }
13644
13645 /* Build an incomplete class reference operator. */
13646 static tree
13647 build_incomplete_class_ref (location, class_name)
13648 int location;
13649 tree class_name;
13650 {
13651 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13652 EXPR_WFL_LINECOL (node) = location;
13653 return node;
13654 }
13655
13656 /* Complete an incomplete class reference operator. */
13657 static tree
13658 patch_incomplete_class_ref (node)
13659 tree node;
13660 {
13661 tree type = TREE_OPERAND (node, 0);
13662 tree ref_type;
13663
13664 if (!(ref_type = resolve_type_during_patch (type)))
13665 return error_mark_node;
13666
13667 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13668 {
13669 /* A class referenced by `foo.class' is initialized. */
13670 return build_class_init (ref_type, build_class_ref (ref_type));
13671 }
13672
13673 /* If we're emitting class files and we have to deal with non
13674 primitive types, we invoke (and consider generating) the
13675 synthetic static method `class$'. */
13676 if (!TYPE_DOT_CLASS (current_class))
13677 build_dot_class_method (current_class);
13678 ref_type = build_dot_class_method_invocation (ref_type);
13679 return java_complete_tree (ref_type);
13680 }
13681
13682 /* 15.14 Unary operators. We return error_mark_node in case of error,
13683 but preserve the type of NODE if the type is fixed. */
13684
13685 static tree
13686 patch_unaryop (node, wfl_op)
13687 tree node;
13688 tree wfl_op;
13689 {
13690 tree op = TREE_OPERAND (node, 0);
13691 tree op_type = TREE_TYPE (op);
13692 tree prom_type = NULL_TREE, value, decl;
13693 int outer_field_flag = 0;
13694 int code = TREE_CODE (node);
13695 int error_found = 0;
13696
13697 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13698
13699 switch (code)
13700 {
13701 /* 15.13.2 Postfix Increment Operator ++ */
13702 case POSTINCREMENT_EXPR:
13703 /* 15.13.3 Postfix Increment Operator -- */
13704 case POSTDECREMENT_EXPR:
13705 /* 15.14.1 Prefix Increment Operator ++ */
13706 case PREINCREMENT_EXPR:
13707 /* 15.14.2 Prefix Decrement Operator -- */
13708 case PREDECREMENT_EXPR:
13709 op = decl = strip_out_static_field_access_decl (op);
13710 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13711 /* We might be trying to change an outer field accessed using
13712 access method. */
13713 if (outer_field_flag)
13714 {
13715 /* Retrieve the decl of the field we're trying to access. We
13716 do that by first retrieving the function we would call to
13717 access the field. It has been already verified that this
13718 field isn't final */
13719 if (flag_emit_class_files)
13720 decl = TREE_OPERAND (op, 0);
13721 else
13722 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13723 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13724 }
13725 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13726 else if (!JDECL_P (decl)
13727 && TREE_CODE (decl) != COMPONENT_REF
13728 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13729 && TREE_CODE (decl) != INDIRECT_REF
13730 && !(TREE_CODE (decl) == COMPOUND_EXPR
13731 && TREE_OPERAND (decl, 1)
13732 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13733 {
13734 tree lvalue;
13735 /* Before screaming, check that we're not in fact trying to
13736 increment a optimized static final access, in which case
13737 we issue an different error message. */
13738 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13739 && resolve_expression_name (wfl_op, &lvalue)
13740 && check_final_assignment (lvalue, wfl_op)))
13741 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13742 operator_string (node));
13743 TREE_TYPE (node) = error_mark_node;
13744 error_found = 1;
13745 }
13746
13747 if (check_final_assignment (op, wfl_op))
13748 error_found = 1;
13749
13750 /* From now on, we know that op if a variable and that it has a
13751 valid wfl. We use wfl_op to locate errors related to the
13752 ++/-- operand. */
13753 else if (!JNUMERIC_TYPE_P (op_type))
13754 {
13755 parse_error_context
13756 (wfl_op, "Invalid argument type `%s' to `%s'",
13757 lang_printable_name (op_type, 0), operator_string (node));
13758 TREE_TYPE (node) = error_mark_node;
13759 error_found = 1;
13760 }
13761 else
13762 {
13763 /* Before the addition, binary numeric promotion is performed on
13764 both operands, if really necessary */
13765 if (JINTEGRAL_TYPE_P (op_type))
13766 {
13767 value = build_int_2 (1, 0);
13768 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13769 }
13770 else
13771 {
13772 value = build_int_2 (1, 0);
13773 TREE_TYPE (node) =
13774 binary_numeric_promotion (op_type,
13775 TREE_TYPE (value), &op, &value);
13776 }
13777
13778 /* We remember we might be accessing an outer field */
13779 if (outer_field_flag)
13780 {
13781 /* We re-generate an access to the field */
13782 value = build (PLUS_EXPR, TREE_TYPE (op),
13783 build_outer_field_access (wfl_op, decl), value);
13784
13785 /* And we patch the original access$() into a write
13786 with plus_op as a rhs */
13787 return outer_field_access_fix (node, op, value);
13788 }
13789
13790 /* And write back into the node. */
13791 TREE_OPERAND (node, 0) = op;
13792 TREE_OPERAND (node, 1) = value;
13793 /* Convert the overall back into its original type, if
13794 necessary, and return */
13795 if (JINTEGRAL_TYPE_P (op_type))
13796 return fold (node);
13797 else
13798 return fold (convert (op_type, node));
13799 }
13800 break;
13801
13802 /* 15.14.3 Unary Plus Operator + */
13803 case UNARY_PLUS_EXPR:
13804 /* 15.14.4 Unary Minus Operator - */
13805 case NEGATE_EXPR:
13806 if (!JNUMERIC_TYPE_P (op_type))
13807 {
13808 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13809 TREE_TYPE (node) = error_mark_node;
13810 error_found = 1;
13811 }
13812 /* Unary numeric promotion is performed on operand */
13813 else
13814 {
13815 op = do_unary_numeric_promotion (op);
13816 prom_type = TREE_TYPE (op);
13817 if (code == UNARY_PLUS_EXPR)
13818 return fold (op);
13819 }
13820 break;
13821
13822 /* 15.14.5 Bitwise Complement Operator ~ */
13823 case BIT_NOT_EXPR:
13824 if (!JINTEGRAL_TYPE_P (op_type))
13825 {
13826 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13827 TREE_TYPE (node) = error_mark_node;
13828 error_found = 1;
13829 }
13830 else
13831 {
13832 op = do_unary_numeric_promotion (op);
13833 prom_type = TREE_TYPE (op);
13834 }
13835 break;
13836
13837 /* 15.14.6 Logical Complement Operator ! */
13838 case TRUTH_NOT_EXPR:
13839 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13840 {
13841 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13842 /* But the type is known. We will report an error if further
13843 attempt of a assignment is made with this rhs */
13844 TREE_TYPE (node) = boolean_type_node;
13845 error_found = 1;
13846 }
13847 else
13848 prom_type = boolean_type_node;
13849 break;
13850
13851 /* 15.15 Cast Expression */
13852 case CONVERT_EXPR:
13853 value = patch_cast (node, wfl_operator);
13854 if (value == error_mark_node)
13855 {
13856 /* If this cast is part of an assignment, we tell the code
13857 that deals with it not to complain about a mismatch,
13858 because things have been cast, anyways */
13859 TREE_TYPE (node) = error_mark_node;
13860 error_found = 1;
13861 }
13862 else
13863 {
13864 value = fold (value);
13865 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13866 return value;
13867 }
13868 break;
13869 }
13870
13871 if (error_found)
13872 return error_mark_node;
13873
13874 /* There are cases where node has been replaced by something else
13875 and we don't end up returning here: UNARY_PLUS_EXPR,
13876 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13877 TREE_OPERAND (node, 0) = fold (op);
13878 TREE_TYPE (node) = prom_type;
13879 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13880 return fold (node);
13881 }
13882
13883 /* Generic type resolution that sometimes takes place during node
13884 patching. Returned the resolved type or generate an error
13885 message. Return the resolved type or NULL_TREE. */
13886
13887 static tree
13888 resolve_type_during_patch (type)
13889 tree type;
13890 {
13891 if (unresolved_type_p (type, NULL))
13892 {
13893 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
13894 if (!type_decl)
13895 {
13896 parse_error_context (type,
13897 "Class `%s' not found in type declaration",
13898 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13899 return NULL_TREE;
13900 }
13901 else
13902 {
13903 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13904 return TREE_TYPE (type_decl);
13905 }
13906 }
13907 return type;
13908 }
13909 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13910 found. Otherwise NODE or something meant to replace it is returned. */
13911
13912 static tree
13913 patch_cast (node, wfl_op)
13914 tree node;
13915 tree wfl_op;
13916 {
13917 tree op = TREE_OPERAND (node, 0);
13918 tree op_type = TREE_TYPE (op);
13919 tree cast_type = TREE_TYPE (node);
13920 char *t1;
13921
13922 /* First resolve OP_TYPE if unresolved */
13923 if (!(cast_type = resolve_type_during_patch (cast_type)))
13924 return error_mark_node;
13925
13926 /* Check on cast that are proven correct at compile time */
13927 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13928 {
13929 /* Same type */
13930 if (cast_type == op_type)
13931 return node;
13932
13933 /* float and double type are converted to the original type main
13934 variant and then to the target type. */
13935 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13936 op = convert (integer_type_node, op);
13937
13938 /* Try widening/narowwing convertion. Potentially, things need
13939 to be worked out in gcc so we implement the extreme cases
13940 correctly. fold_convert() needs to be fixed. */
13941 return convert (cast_type, op);
13942 }
13943
13944 /* It's also valid to cast a boolean into a boolean */
13945 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13946 return node;
13947
13948 /* null can be casted to references */
13949 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13950 return build_null_of_type (cast_type);
13951
13952 /* The remaining legal casts involve conversion between reference
13953 types. Check for their compile time correctness. */
13954 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
13955 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
13956 {
13957 TREE_TYPE (node) = promote_type (cast_type);
13958 /* Now, the case can be determined correct at compile time if
13959 OP_TYPE can be converted into CAST_TYPE by assignment
13960 conversion (5.2) */
13961
13962 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
13963 {
13964 TREE_SET_CODE (node, NOP_EXPR);
13965 return node;
13966 }
13967
13968 if (flag_emit_class_files)
13969 {
13970 TREE_SET_CODE (node, CONVERT_EXPR);
13971 return node;
13972 }
13973
13974 /* The cast requires a run-time check */
13975 return build (CALL_EXPR, promote_type (cast_type),
13976 build_address_of (soft_checkcast_node),
13977 tree_cons (NULL_TREE, build_class_ref (cast_type),
13978 build_tree_list (NULL_TREE, op)),
13979 NULL_TREE);
13980 }
13981
13982 /* Any other casts are proven incorrect at compile time */
13983 t1 = xstrdup (lang_printable_name (op_type, 0));
13984 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
13985 t1, lang_printable_name (cast_type, 0));
13986 free (t1);
13987 return error_mark_node;
13988 }
13989
13990 /* Build a null constant and give it the type TYPE. */
13991
13992 static tree
13993 build_null_of_type (type)
13994 tree type;
13995 {
13996 tree node = build_int_2 (0, 0);
13997 TREE_TYPE (node) = promote_type (type);
13998 return node;
13999 }
14000
14001 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14002 a list of indices. */
14003 static tree
14004 build_array_ref (location, array, index)
14005 int location;
14006 tree array, index;
14007 {
14008 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14009 EXPR_WFL_LINECOL (node) = location;
14010 return node;
14011 }
14012
14013 /* 15.12 Array Access Expression */
14014
14015 static tree
14016 patch_array_ref (node)
14017 tree node;
14018 {
14019 tree array = TREE_OPERAND (node, 0);
14020 tree array_type = TREE_TYPE (array);
14021 tree index = TREE_OPERAND (node, 1);
14022 tree index_type = TREE_TYPE (index);
14023 int error_found = 0;
14024
14025 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14026
14027 if (TREE_CODE (array_type) == POINTER_TYPE)
14028 array_type = TREE_TYPE (array_type);
14029
14030 /* The array reference must be an array */
14031 if (!TYPE_ARRAY_P (array_type))
14032 {
14033 parse_error_context
14034 (wfl_operator,
14035 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14036 lang_printable_name (array_type, 0));
14037 TREE_TYPE (node) = error_mark_node;
14038 error_found = 1;
14039 }
14040
14041 /* The array index undergoes unary numeric promotion. The promoted
14042 type must be int */
14043 index = do_unary_numeric_promotion (index);
14044 if (TREE_TYPE (index) != int_type_node)
14045 {
14046 if (valid_cast_to_p (index_type, int_type_node))
14047 parse_error_context (wfl_operator,
14048 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14049 lang_printable_name (index_type, 0));
14050 else
14051 parse_error_context (wfl_operator,
14052 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14053 lang_printable_name (index_type, 0));
14054 TREE_TYPE (node) = error_mark_node;
14055 error_found = 1;
14056 }
14057
14058 if (error_found)
14059 return error_mark_node;
14060
14061 array_type = TYPE_ARRAY_ELEMENT (array_type);
14062
14063 if (flag_emit_class_files || flag_emit_xref)
14064 {
14065 TREE_OPERAND (node, 0) = array;
14066 TREE_OPERAND (node, 1) = index;
14067 }
14068 else
14069 {
14070 /* The save_expr is for correct evaluation order. It would be cleaner
14071 to use force_evaluation_order (see comment there), but that is
14072 difficult when we also have to deal with bounds checking. */
14073 if (TREE_SIDE_EFFECTS (index))
14074 array = save_expr (array);
14075 node = build_java_arrayaccess (array, array_type, index);
14076 if (TREE_SIDE_EFFECTS (index))
14077 node = build (COMPOUND_EXPR, array_type, array, node);
14078 }
14079 TREE_TYPE (node) = array_type;
14080 return node;
14081 }
14082
14083 /* 15.9 Array Creation Expressions */
14084
14085 static tree
14086 build_newarray_node (type, dims, extra_dims)
14087 tree type;
14088 tree dims;
14089 int extra_dims;
14090 {
14091 tree node =
14092 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14093 build_int_2 (extra_dims, 0));
14094 return node;
14095 }
14096
14097 static tree
14098 patch_newarray (node)
14099 tree node;
14100 {
14101 tree type = TREE_OPERAND (node, 0);
14102 tree dims = TREE_OPERAND (node, 1);
14103 tree cdim, array_type;
14104 int error_found = 0;
14105 int ndims = 0;
14106 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14107
14108 /* Dimension types are verified. It's better for the types to be
14109 verified in order. */
14110 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14111 {
14112 int dim_error = 0;
14113 tree dim = TREE_VALUE (cdim);
14114
14115 /* Dim might have been saved during its evaluation */
14116 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14117
14118 /* The type of each specified dimension must be an integral type. */
14119 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14120 dim_error = 1;
14121
14122 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14123 promoted type must be int. */
14124 else
14125 {
14126 dim = do_unary_numeric_promotion (dim);
14127 if (TREE_TYPE (dim) != int_type_node)
14128 dim_error = 1;
14129 }
14130
14131 /* Report errors on types here */
14132 if (dim_error)
14133 {
14134 parse_error_context
14135 (TREE_PURPOSE (cdim),
14136 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14137 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14138 "Explicit cast needed to" : "Can't"),
14139 lang_printable_name (TREE_TYPE (dim), 0));
14140 error_found = 1;
14141 }
14142
14143 TREE_PURPOSE (cdim) = NULL_TREE;
14144 }
14145
14146 /* Resolve array base type if unresolved */
14147 if (!(type = resolve_type_during_patch (type)))
14148 error_found = 1;
14149
14150 if (error_found)
14151 {
14152 /* We don't want further evaluation of this bogus array creation
14153 operation */
14154 TREE_TYPE (node) = error_mark_node;
14155 return error_mark_node;
14156 }
14157
14158 /* Set array_type to the actual (promoted) array type of the result. */
14159 if (TREE_CODE (type) == RECORD_TYPE)
14160 type = build_pointer_type (type);
14161 while (--xdims >= 0)
14162 {
14163 type = promote_type (build_java_array_type (type, -1));
14164 }
14165 dims = nreverse (dims);
14166 array_type = type;
14167 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14168 {
14169 type = array_type;
14170 array_type
14171 = build_java_array_type (type,
14172 TREE_CODE (cdim) == INTEGER_CST
14173 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14174 : -1);
14175 array_type = promote_type (array_type);
14176 }
14177 dims = nreverse (dims);
14178
14179 /* The node is transformed into a function call. Things are done
14180 differently according to the number of dimensions. If the number
14181 of dimension is equal to 1, then the nature of the base type
14182 (primitive or not) matters. */
14183 if (ndims == 1)
14184 return build_new_array (type, TREE_VALUE (dims));
14185
14186 /* Can't reuse what's already written in expr.c because it uses the
14187 JVM stack representation. Provide a build_multianewarray. FIXME */
14188 return build (CALL_EXPR, array_type,
14189 build_address_of (soft_multianewarray_node),
14190 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14191 tree_cons (NULL_TREE,
14192 build_int_2 (ndims, 0), dims )),
14193 NULL_TREE);
14194 }
14195
14196 /* 10.6 Array initializer. */
14197
14198 /* Build a wfl for array element that don't have one, so we can
14199 pin-point errors. */
14200
14201 static tree
14202 maybe_build_array_element_wfl (node)
14203 tree node;
14204 {
14205 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14206 return build_expr_wfl (NULL_TREE, ctxp->filename,
14207 ctxp->elc.line, ctxp->elc.prev_col);
14208 else
14209 return NULL_TREE;
14210 }
14211
14212 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14213 identification of initialized arrays easier to detect during walk
14214 and expansion. */
14215
14216 static tree
14217 build_new_array_init (location, values)
14218 int location;
14219 tree values;
14220 {
14221 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14222 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14223 EXPR_WFL_LINECOL (to_return) = location;
14224 return to_return;
14225 }
14226
14227 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14228 occurred. Otherwise return NODE after having set its type
14229 appropriately. */
14230
14231 static tree
14232 patch_new_array_init (type, node)
14233 tree type, node;
14234 {
14235 int error_seen = 0;
14236 tree current, element_type;
14237 HOST_WIDE_INT length;
14238 int all_constant = 1;
14239 tree init = TREE_OPERAND (node, 0);
14240
14241 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14242 {
14243 parse_error_context (node,
14244 "Invalid array initializer for non-array type `%s'",
14245 lang_printable_name (type, 1));
14246 return error_mark_node;
14247 }
14248 type = TREE_TYPE (type);
14249 element_type = TYPE_ARRAY_ELEMENT (type);
14250
14251 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14252
14253 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14254 current; length++, current = TREE_CHAIN (current))
14255 {
14256 tree elt = TREE_VALUE (current);
14257 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14258 {
14259 error_seen |= array_constructor_check_entry (element_type, current);
14260 elt = TREE_VALUE (current);
14261 /* When compiling to native code, STRING_CST is converted to
14262 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14263 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14264 all_constant = 0;
14265 }
14266 else
14267 {
14268 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14269 TREE_PURPOSE (current) = NULL_TREE;
14270 all_constant = 0;
14271 }
14272 if (elt && TREE_CODE (elt) == TREE_LIST
14273 && TREE_VALUE (elt) == error_mark_node)
14274 error_seen = 1;
14275 }
14276
14277 if (error_seen)
14278 return error_mark_node;
14279
14280 /* Create a new type. We can't reuse the one we have here by
14281 patching its dimension because it originally is of dimension -1
14282 hence reused by gcc. This would prevent triangular arrays. */
14283 type = build_java_array_type (element_type, length);
14284 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14285 TREE_TYPE (node) = promote_type (type);
14286 TREE_CONSTANT (init) = all_constant;
14287 TREE_CONSTANT (node) = all_constant;
14288 return node;
14289 }
14290
14291 /* Verify that one entry of the initializer element list can be
14292 assigned to the array base type. Report 1 if an error occurred, 0
14293 otherwise. */
14294
14295 static int
14296 array_constructor_check_entry (type, entry)
14297 tree type, entry;
14298 {
14299 char *array_type_string = NULL; /* For error reports */
14300 tree value, type_value, new_value, wfl_value, patched;
14301 int error_seen = 0;
14302
14303 new_value = NULL_TREE;
14304 wfl_value = TREE_VALUE (entry);
14305
14306 value = java_complete_tree (TREE_VALUE (entry));
14307 /* patch_string return error_mark_node if arg is error_mark_node */
14308 if ((patched = patch_string (value)))
14309 value = patched;
14310 if (value == error_mark_node)
14311 return 1;
14312
14313 type_value = TREE_TYPE (value);
14314
14315 /* At anytime, try_builtin_assignconv can report a warning on
14316 constant overflow during narrowing. */
14317 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14318 new_value = try_builtin_assignconv (wfl_operator, type, value);
14319 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14320 type_value = promote_type (type);
14321
14322 /* Check and report errors */
14323 if (!new_value)
14324 {
14325 const char *msg = (!valid_cast_to_p (type_value, type) ?
14326 "Can't" : "Explicit cast needed to");
14327 if (!array_type_string)
14328 array_type_string = xstrdup (lang_printable_name (type, 1));
14329 parse_error_context
14330 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14331 msg, lang_printable_name (type_value, 1), array_type_string);
14332 error_seen = 1;
14333 }
14334
14335 if (new_value)
14336 {
14337 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14338 TREE_VALUE (entry) = new_value;
14339 }
14340
14341 if (array_type_string)
14342 free (array_type_string);
14343
14344 TREE_PURPOSE (entry) = NULL_TREE;
14345 return error_seen;
14346 }
14347
14348 static tree
14349 build_this (location)
14350 int location;
14351 {
14352 tree node = build_wfl_node (this_identifier_node);
14353 TREE_SET_CODE (node, THIS_EXPR);
14354 EXPR_WFL_LINECOL (node) = location;
14355 return node;
14356 }
14357
14358 /* 14.15 The return statement. It builds a modify expression that
14359 assigns the returned value to the RESULT_DECL that hold the value
14360 to be returned. */
14361
14362 static tree
14363 build_return (location, op)
14364 int location;
14365 tree op;
14366 {
14367 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14368 EXPR_WFL_LINECOL (node) = location;
14369 node = build_debugable_stmt (location, node);
14370 return node;
14371 }
14372
14373 static tree
14374 patch_return (node)
14375 tree node;
14376 {
14377 tree return_exp = TREE_OPERAND (node, 0);
14378 tree meth = current_function_decl;
14379 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14380 int error_found = 0;
14381
14382 TREE_TYPE (node) = error_mark_node;
14383 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14384
14385 /* It's invalid to have a return value within a function that is
14386 declared with the keyword void or that is a constructor */
14387 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14388 error_found = 1;
14389
14390 /* It's invalid to use a return statement in a static block */
14391 if (DECL_CLINIT_P (current_function_decl))
14392 error_found = 1;
14393
14394 /* It's invalid to have a no return value within a function that
14395 isn't declared with the keyword `void' */
14396 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14397 error_found = 2;
14398
14399 if (in_instance_initializer)
14400 error_found = 1;
14401
14402 if (error_found)
14403 {
14404 if (in_instance_initializer)
14405 parse_error_context (wfl_operator,
14406 "`return' inside instance initializer");
14407
14408 else if (DECL_CLINIT_P (current_function_decl))
14409 parse_error_context (wfl_operator,
14410 "`return' inside static initializer");
14411
14412 else if (!DECL_CONSTRUCTOR_P (meth))
14413 {
14414 char *t = xstrdup (lang_printable_name (mtype, 0));
14415 parse_error_context (wfl_operator,
14416 "`return' with%s value from `%s %s'",
14417 (error_found == 1 ? "" : "out"),
14418 t, lang_printable_name (meth, 0));
14419 free (t);
14420 }
14421 else
14422 parse_error_context (wfl_operator,
14423 "`return' with value from constructor `%s'",
14424 lang_printable_name (meth, 0));
14425 return error_mark_node;
14426 }
14427
14428 /* If we have a return_exp, build a modify expression and expand
14429 it. Note: at that point, the assignment is declared valid, but we
14430 may want to carry some more hacks */
14431 if (return_exp)
14432 {
14433 tree exp = java_complete_tree (return_exp);
14434 tree modify, patched;
14435
14436 /* If the function returned value and EXP are booleans, EXP has
14437 to be converted into the type of DECL_RESULT, which is integer
14438 (see complete_start_java_method) */
14439 if (TREE_TYPE (exp) == boolean_type_node &&
14440 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14441 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14442
14443 /* `null' can be assigned to a function returning a reference */
14444 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14445 exp == null_pointer_node)
14446 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14447
14448 if ((patched = patch_string (exp)))
14449 exp = patched;
14450
14451 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14452 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14453 modify = java_complete_tree (modify);
14454
14455 if (modify != error_mark_node)
14456 {
14457 TREE_SIDE_EFFECTS (modify) = 1;
14458 TREE_OPERAND (node, 0) = modify;
14459 }
14460 else
14461 return error_mark_node;
14462 }
14463 TREE_TYPE (node) = void_type_node;
14464 TREE_SIDE_EFFECTS (node) = 1;
14465 return node;
14466 }
14467
14468 /* 14.8 The if Statement */
14469
14470 static tree
14471 build_if_else_statement (location, expression, if_body, else_body)
14472 int location;
14473 tree expression, if_body, else_body;
14474 {
14475 tree node;
14476 if (!else_body)
14477 else_body = empty_stmt_node;
14478 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14479 EXPR_WFL_LINECOL (node) = location;
14480 node = build_debugable_stmt (location, node);
14481 return node;
14482 }
14483
14484 static tree
14485 patch_if_else_statement (node)
14486 tree node;
14487 {
14488 tree expression = TREE_OPERAND (node, 0);
14489
14490 TREE_TYPE (node) = error_mark_node;
14491 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14492
14493 /* The type of expression must be boolean */
14494 if (TREE_TYPE (expression) != boolean_type_node
14495 && TREE_TYPE (expression) != promoted_boolean_type_node)
14496 {
14497 parse_error_context
14498 (wfl_operator,
14499 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14500 lang_printable_name (TREE_TYPE (expression), 0));
14501 return error_mark_node;
14502 }
14503
14504 TREE_TYPE (node) = void_type_node;
14505 TREE_SIDE_EFFECTS (node) = 1;
14506 CAN_COMPLETE_NORMALLY (node)
14507 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14508 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14509 return node;
14510 }
14511
14512 /* 14.6 Labeled Statements */
14513
14514 /* Action taken when a lableled statement is parsed. a new
14515 LABELED_BLOCK_EXPR is created. No statement is attached to the
14516 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14517
14518 static tree
14519 build_labeled_block (location, label)
14520 int location;
14521 tree label;
14522 {
14523 tree label_name ;
14524 tree label_decl, node;
14525 if (label == NULL_TREE || label == continue_identifier_node)
14526 label_name = label;
14527 else
14528 {
14529 label_name = merge_qualified_name (label_id, label);
14530 /* Issue an error if we try to reuse a label that was previously
14531 declared */
14532 if (IDENTIFIER_LOCAL_VALUE (label_name))
14533 {
14534 EXPR_WFL_LINECOL (wfl_operator) = location;
14535 parse_error_context (wfl_operator,
14536 "Declaration of `%s' shadows a previous label declaration",
14537 IDENTIFIER_POINTER (label));
14538 EXPR_WFL_LINECOL (wfl_operator) =
14539 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14540 parse_error_context (wfl_operator,
14541 "This is the location of the previous declaration of label `%s'",
14542 IDENTIFIER_POINTER (label));
14543 java_error_count--;
14544 }
14545 }
14546
14547 label_decl = create_label_decl (label_name);
14548 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14549 EXPR_WFL_LINECOL (node) = location;
14550 TREE_SIDE_EFFECTS (node) = 1;
14551 return node;
14552 }
14553
14554 /* A labeled statement LBE is attached a statement. */
14555
14556 static tree
14557 finish_labeled_statement (lbe, statement)
14558 tree lbe; /* Labeled block expr */
14559 tree statement;
14560 {
14561 /* In anyways, tie the loop to its statement */
14562 LABELED_BLOCK_BODY (lbe) = statement;
14563 pop_labeled_block ();
14564 POP_LABELED_BLOCK ();
14565 return lbe;
14566 }
14567
14568 /* 14.10, 14.11, 14.12 Loop Statements */
14569
14570 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14571 list. */
14572
14573 static tree
14574 build_new_loop (loop_body)
14575 tree loop_body;
14576 {
14577 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14578 TREE_SIDE_EFFECTS (loop) = 1;
14579 PUSH_LOOP (loop);
14580 return loop;
14581 }
14582
14583 /* Create a loop body according to the following structure:
14584 COMPOUND_EXPR
14585 COMPOUND_EXPR (loop main body)
14586 EXIT_EXPR (this order is for while/for loops.
14587 LABELED_BLOCK_EXPR the order is reversed for do loops)
14588 LABEL_DECL (a continue occuring here branches at the
14589 BODY end of this labeled block)
14590 INCREMENT (if any)
14591
14592 REVERSED, if non zero, tells that the loop condition expr comes
14593 after the body, like in the do-while loop.
14594
14595 To obtain a loop, the loop body structure described above is
14596 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14597
14598 LABELED_BLOCK_EXPR
14599 LABEL_DECL (use this label to exit the loop)
14600 LOOP_EXPR
14601 <structure described above> */
14602
14603 static tree
14604 build_loop_body (location, condition, reversed)
14605 int location;
14606 tree condition;
14607 int reversed;
14608 {
14609 tree first, second, body;
14610
14611 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14612 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14613 condition = build_debugable_stmt (location, condition);
14614 TREE_SIDE_EFFECTS (condition) = 1;
14615
14616 body = build_labeled_block (0, continue_identifier_node);
14617 first = (reversed ? body : condition);
14618 second = (reversed ? condition : body);
14619 return
14620 build (COMPOUND_EXPR, NULL_TREE,
14621 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14622 }
14623
14624 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14625 their order) on the current loop. Unlink the current loop from the
14626 loop list. */
14627
14628 static tree
14629 finish_loop_body (location, condition, body, reversed)
14630 int location;
14631 tree condition, body;
14632 int reversed;
14633 {
14634 tree to_return = ctxp->current_loop;
14635 tree loop_body = LOOP_EXPR_BODY (to_return);
14636 if (condition)
14637 {
14638 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14639 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14640 The real EXIT_EXPR is one operand further. */
14641 EXPR_WFL_LINECOL (cnode) = location;
14642 /* This one is for accurate error reports */
14643 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14644 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14645 }
14646 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14647 POP_LOOP ();
14648 return to_return;
14649 }
14650
14651 /* Tailored version of finish_loop_body for FOR loops, when FOR
14652 loops feature the condition part */
14653
14654 static tree
14655 finish_for_loop (location, condition, update, body)
14656 int location;
14657 tree condition, update, body;
14658 {
14659 /* Put the condition and the loop body in place */
14660 tree loop = finish_loop_body (location, condition, body, 0);
14661 /* LOOP is the current loop which has been now popped of the loop
14662 stack. Install the update block */
14663 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14664 return loop;
14665 }
14666
14667 /* Try to find the loop a block might be related to. This comprises
14668 the case where the LOOP_EXPR is found as the second operand of a
14669 COMPOUND_EXPR, because the loop happens to have an initialization
14670 part, then expressed as the first operand of the COMPOUND_EXPR. If
14671 the search finds something, 1 is returned. Otherwise, 0 is
14672 returned. The search is assumed to start from a
14673 LABELED_BLOCK_EXPR's block. */
14674
14675 static tree
14676 search_loop (statement)
14677 tree statement;
14678 {
14679 if (TREE_CODE (statement) == LOOP_EXPR)
14680 return statement;
14681
14682 if (TREE_CODE (statement) == BLOCK)
14683 statement = BLOCK_SUBBLOCKS (statement);
14684 else
14685 return NULL_TREE;
14686
14687 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14688 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14689 statement = TREE_OPERAND (statement, 1);
14690
14691 return (TREE_CODE (statement) == LOOP_EXPR
14692 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14693 }
14694
14695 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14696 returned otherwise. */
14697
14698 static int
14699 labeled_block_contains_loop_p (block, loop)
14700 tree block, loop;
14701 {
14702 if (!block)
14703 return 0;
14704
14705 if (LABELED_BLOCK_BODY (block) == loop)
14706 return 1;
14707
14708 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14709 return 1;
14710
14711 return 0;
14712 }
14713
14714 /* If the loop isn't surrounded by a labeled statement, create one and
14715 insert LOOP as its body. */
14716
14717 static tree
14718 patch_loop_statement (loop)
14719 tree loop;
14720 {
14721 tree loop_label;
14722
14723 TREE_TYPE (loop) = void_type_node;
14724 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14725 return loop;
14726
14727 loop_label = build_labeled_block (0, NULL_TREE);
14728 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14729 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14730 LABELED_BLOCK_BODY (loop_label) = loop;
14731 PUSH_LABELED_BLOCK (loop_label);
14732 return loop_label;
14733 }
14734
14735 /* 14.13, 14.14: break and continue Statements */
14736
14737 /* Build a break or a continue statement. a null NAME indicates an
14738 unlabeled break/continue statement. */
14739
14740 static tree
14741 build_bc_statement (location, is_break, name)
14742 int location, is_break;
14743 tree name;
14744 {
14745 tree break_continue, label_block_expr = NULL_TREE;
14746
14747 if (name)
14748 {
14749 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14750 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14751 /* Null means that we don't have a target for this named
14752 break/continue. In this case, we make the target to be the
14753 label name, so that the error can be reported accuratly in
14754 patch_bc_statement. */
14755 label_block_expr = EXPR_WFL_NODE (name);
14756 }
14757 /* Unlabeled break/continue will be handled during the
14758 break/continue patch operation */
14759 break_continue
14760 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14761
14762 IS_BREAK_STMT_P (break_continue) = is_break;
14763 TREE_SIDE_EFFECTS (break_continue) = 1;
14764 EXPR_WFL_LINECOL (break_continue) = location;
14765 break_continue = build_debugable_stmt (location, break_continue);
14766 return break_continue;
14767 }
14768
14769 /* Verification of a break/continue statement. */
14770
14771 static tree
14772 patch_bc_statement (node)
14773 tree node;
14774 {
14775 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14776 tree labeled_block = ctxp->current_labeled_block;
14777 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14778
14779 /* Having an identifier here means that the target is unknown. */
14780 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14781 {
14782 parse_error_context (wfl_operator, "No label definition found for `%s'",
14783 IDENTIFIER_POINTER (bc_label));
14784 return error_mark_node;
14785 }
14786 if (! IS_BREAK_STMT_P (node))
14787 {
14788 /* It's a continue statement. */
14789 for (;; labeled_block = TREE_CHAIN (labeled_block))
14790 {
14791 if (labeled_block == NULL_TREE)
14792 {
14793 if (bc_label == NULL_TREE)
14794 parse_error_context (wfl_operator,
14795 "`continue' must be in loop");
14796 else
14797 parse_error_context
14798 (wfl_operator, "continue label `%s' does not name a loop",
14799 IDENTIFIER_POINTER (bc_label));
14800 return error_mark_node;
14801 }
14802 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14803 == continue_identifier_node)
14804 && (bc_label == NULL_TREE
14805 || TREE_CHAIN (labeled_block) == bc_label))
14806 {
14807 bc_label = labeled_block;
14808 break;
14809 }
14810 }
14811 }
14812 else if (!bc_label)
14813 {
14814 for (;; labeled_block = TREE_CHAIN (labeled_block))
14815 {
14816 if (labeled_block == NULL_TREE)
14817 {
14818 parse_error_context (wfl_operator,
14819 "`break' must be in loop or switch");
14820 return error_mark_node;
14821 }
14822 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14823 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14824 || search_loop (target_stmt))
14825 {
14826 bc_label = labeled_block;
14827 break;
14828 }
14829 }
14830 }
14831
14832 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14833 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14834
14835 /* Our break/continue don't return values. */
14836 TREE_TYPE (node) = void_type_node;
14837 /* Encapsulate the break within a compound statement so that it's
14838 expanded all the times by expand_expr (and not clobbered
14839 sometimes, like after a if statement) */
14840 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14841 TREE_SIDE_EFFECTS (node) = 1;
14842 return node;
14843 }
14844
14845 /* Process the exit expression belonging to a loop. Its type must be
14846 boolean. */
14847
14848 static tree
14849 patch_exit_expr (node)
14850 tree node;
14851 {
14852 tree expression = TREE_OPERAND (node, 0);
14853 TREE_TYPE (node) = error_mark_node;
14854 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14855
14856 /* The type of expression must be boolean */
14857 if (TREE_TYPE (expression) != boolean_type_node)
14858 {
14859 parse_error_context
14860 (wfl_operator,
14861 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14862 lang_printable_name (TREE_TYPE (expression), 0));
14863 return error_mark_node;
14864 }
14865 /* Now we know things are allright, invert the condition, fold and
14866 return */
14867 TREE_OPERAND (node, 0) =
14868 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14869
14870 if (! integer_zerop (TREE_OPERAND (node, 0))
14871 && ctxp->current_loop != NULL_TREE
14872 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14873 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14874 if (! integer_onep (TREE_OPERAND (node, 0)))
14875 CAN_COMPLETE_NORMALLY (node) = 1;
14876
14877
14878 TREE_TYPE (node) = void_type_node;
14879 return node;
14880 }
14881
14882 /* 14.9 Switch statement */
14883
14884 static tree
14885 patch_switch_statement (node)
14886 tree node;
14887 {
14888 tree se = TREE_OPERAND (node, 0), se_type;
14889
14890 /* Complete the switch expression */
14891 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14892 se_type = TREE_TYPE (se);
14893 /* The type of the switch expression must be char, byte, short or
14894 int */
14895 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14896 {
14897 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14898 parse_error_context (wfl_operator,
14899 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14900 lang_printable_name (se_type, 0));
14901 /* This is what java_complete_tree will check */
14902 TREE_OPERAND (node, 0) = error_mark_node;
14903 return error_mark_node;
14904 }
14905
14906 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14907
14908 /* Ready to return */
14909 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14910 {
14911 TREE_TYPE (node) = error_mark_node;
14912 return error_mark_node;
14913 }
14914 TREE_TYPE (node) = void_type_node;
14915 TREE_SIDE_EFFECTS (node) = 1;
14916 CAN_COMPLETE_NORMALLY (node)
14917 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14918 || ! SWITCH_HAS_DEFAULT (node);
14919 return node;
14920 }
14921
14922 /* 14.18 The try/catch statements */
14923
14924 static tree
14925 build_try_statement (location, try_block, catches)
14926 int location;
14927 tree try_block, catches;
14928 {
14929 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14930 EXPR_WFL_LINECOL (node) = location;
14931 return node;
14932 }
14933
14934 static tree
14935 build_try_finally_statement (location, try_block, finally)
14936 int location;
14937 tree try_block, finally;
14938 {
14939 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14940 EXPR_WFL_LINECOL (node) = location;
14941 return node;
14942 }
14943
14944 static tree
14945 patch_try_statement (node)
14946 tree node;
14947 {
14948 int error_found = 0;
14949 tree try = TREE_OPERAND (node, 0);
14950 /* Exception handlers are considered in left to right order */
14951 tree catch = nreverse (TREE_OPERAND (node, 1));
14952 tree current, caught_type_list = NULL_TREE;
14953
14954 /* Check catch clauses, if any. Every time we find an error, we try
14955 to process the next catch clause. We process the catch clause before
14956 the try block so that when processing the try block we can check thrown
14957 exceptions againts the caught type list. */
14958 for (current = catch; current; current = TREE_CHAIN (current))
14959 {
14960 tree carg_decl, carg_type;
14961 tree sub_current, catch_block, catch_clause;
14962 int unreachable;
14963
14964 /* At this point, the structure of the catch clause is
14965 CATCH_EXPR (catch node)
14966 BLOCK (with the decl of the parameter)
14967 COMPOUND_EXPR
14968 MODIFY_EXPR (assignment of the catch parameter)
14969 BLOCK (catch clause block)
14970 */
14971 catch_clause = TREE_OPERAND (current, 0);
14972 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14973 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14974
14975 /* Catch clauses can't have more than one parameter declared,
14976 but it's already enforced by the grammar. Make sure that the
14977 only parameter of the clause statement in of class Throwable
14978 or a subclass of Throwable, but that was done earlier. The
14979 catch clause parameter type has also been resolved. */
14980
14981 /* Just make sure that the catch clause parameter type inherits
14982 from java.lang.Throwable */
14983 if (!inherits_from_p (carg_type, throwable_type_node))
14984 {
14985 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14986 parse_error_context (wfl_operator,
14987 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
14988 lang_printable_name (carg_type, 0));
14989 error_found = 1;
14990 continue;
14991 }
14992
14993 /* Partial check for unreachable catch statement: The catch
14994 clause is reachable iff is no earlier catch block A in
14995 the try statement such that the type of the catch
14996 clause's parameter is the same as or a subclass of the
14997 type of A's parameter */
14998 unreachable = 0;
14999 for (sub_current = catch;
15000 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15001 {
15002 tree sub_catch_clause, decl;
15003 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15004 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15005
15006 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15007 {
15008 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15009 parse_error_context
15010 (wfl_operator,
15011 "`catch' not reached because of the catch clause at line %d",
15012 EXPR_WFL_LINENO (sub_current));
15013 unreachable = error_found = 1;
15014 break;
15015 }
15016 }
15017 /* Complete the catch clause block */
15018 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15019 if (catch_block == error_mark_node)
15020 {
15021 error_found = 1;
15022 continue;
15023 }
15024 if (CAN_COMPLETE_NORMALLY (catch_block))
15025 CAN_COMPLETE_NORMALLY (node) = 1;
15026 TREE_OPERAND (current, 0) = catch_block;
15027
15028 if (unreachable)
15029 continue;
15030
15031 /* Things to do here: the exception must be thrown */
15032
15033 /* Link this type to the caught type list */
15034 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15035 }
15036
15037 PUSH_EXCEPTIONS (caught_type_list);
15038 if ((try = java_complete_tree (try)) == error_mark_node)
15039 error_found = 1;
15040 if (CAN_COMPLETE_NORMALLY (try))
15041 CAN_COMPLETE_NORMALLY (node) = 1;
15042 POP_EXCEPTIONS ();
15043
15044 /* Verification ends here */
15045 if (error_found)
15046 return error_mark_node;
15047
15048 TREE_OPERAND (node, 0) = try;
15049 TREE_OPERAND (node, 1) = catch;
15050 TREE_TYPE (node) = void_type_node;
15051 return node;
15052 }
15053
15054 /* 14.17 The synchronized Statement */
15055
15056 static tree
15057 patch_synchronized_statement (node, wfl_op1)
15058 tree node, wfl_op1;
15059 {
15060 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15061 tree block = TREE_OPERAND (node, 1);
15062
15063 tree tmp, enter, exit, expr_decl, assignment;
15064
15065 if (expr == error_mark_node)
15066 {
15067 block = java_complete_tree (block);
15068 return expr;
15069 }
15070
15071 /* We might be trying to synchronize on a STRING_CST */
15072 if ((tmp = patch_string (expr)))
15073 expr = tmp;
15074
15075 /* The TYPE of expr must be a reference type */
15076 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15077 {
15078 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15079 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15080 lang_printable_name (TREE_TYPE (expr), 0));
15081 return error_mark_node;
15082 }
15083
15084 if (flag_emit_xref)
15085 {
15086 TREE_OPERAND (node, 0) = expr;
15087 TREE_OPERAND (node, 1) = java_complete_tree (block);
15088 CAN_COMPLETE_NORMALLY (node) = 1;
15089 return node;
15090 }
15091
15092 /* Generate a try-finally for the synchronized statement, except
15093 that the handler that catches all throw exception calls
15094 _Jv_MonitorExit and then rethrow the exception.
15095 The synchronized statement is then implemented as:
15096 TRY
15097 {
15098 _Jv_MonitorEnter (expression)
15099 synchronized_block
15100 _Jv_MonitorExit (expression)
15101 }
15102 CATCH_ALL
15103 {
15104 e = _Jv_exception_info ();
15105 _Jv_MonitorExit (expression)
15106 Throw (e);
15107 } */
15108
15109 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15110 BUILD_MONITOR_ENTER (enter, expr_decl);
15111 BUILD_MONITOR_EXIT (exit, expr_decl);
15112 CAN_COMPLETE_NORMALLY (enter) = 1;
15113 CAN_COMPLETE_NORMALLY (exit) = 1;
15114 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15115 TREE_SIDE_EFFECTS (assignment) = 1;
15116 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15117 build (COMPOUND_EXPR, NULL_TREE,
15118 build (WITH_CLEANUP_EXPR, NULL_TREE,
15119 build (COMPOUND_EXPR, NULL_TREE,
15120 assignment, enter),
15121 NULL_TREE, exit),
15122 block));
15123 node = build_expr_block (node, expr_decl);
15124
15125 return java_complete_tree (node);
15126 }
15127
15128 /* 14.16 The throw Statement */
15129
15130 static tree
15131 patch_throw_statement (node, wfl_op1)
15132 tree node, wfl_op1;
15133 {
15134 tree expr = TREE_OPERAND (node, 0);
15135 tree type = TREE_TYPE (expr);
15136 int unchecked_ok = 0, tryblock_throws_ok = 0;
15137
15138 /* Thrown expression must be assignable to java.lang.Throwable */
15139 if (!try_reference_assignconv (throwable_type_node, expr))
15140 {
15141 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15142 parse_error_context (wfl_operator,
15143 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15144 lang_printable_name (type, 0));
15145 /* If the thrown expression was a reference, we further the
15146 compile-time check. */
15147 if (!JREFERENCE_TYPE_P (type))
15148 return error_mark_node;
15149 }
15150
15151 /* At least one of the following must be true */
15152
15153 /* The type of the throw expression is a not checked exception,
15154 i.e. is a unchecked expression. */
15155 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15156
15157 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15158 /* An instance can't throw a checked excetion unless that exception
15159 is explicitely declared in the `throws' clause of each
15160 constructor. This doesn't apply to anonymous classes, since they
15161 don't have declared constructors. */
15162 if (!unchecked_ok
15163 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15164 {
15165 tree current;
15166 for (current = TYPE_METHODS (current_class); current;
15167 current = TREE_CHAIN (current))
15168 if (DECL_CONSTRUCTOR_P (current)
15169 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15170 {
15171 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)",
15172 lang_printable_name (TREE_TYPE (expr), 0));
15173 return error_mark_node;
15174 }
15175 }
15176
15177 /* Throw is contained in a try statement and at least one catch
15178 clause can receive the thrown expression or the current method is
15179 declared to throw such an exception. Or, the throw statement is
15180 contained in a method or constructor declaration and the type of
15181 the Expression is assignable to at least one type listed in the
15182 throws clause the declaration. */
15183 if (!unchecked_ok)
15184 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15185 if (!(unchecked_ok || tryblock_throws_ok))
15186 {
15187 /* If there is a surrounding try block that has no matching
15188 clatch clause, report it first. A surrounding try block exits
15189 only if there is something after the list of checked
15190 exception thrown by the current function (if any). */
15191 if (IN_TRY_BLOCK_P ())
15192 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15193 lang_printable_name (type, 0));
15194 /* If we have no surrounding try statement and the method doesn't have
15195 any throws, report it now. FIXME */
15196
15197 /* We report that the exception can't be throw from a try block
15198 in all circumstances but when the `throw' is inside a static
15199 block. */
15200 else if (!EXCEPTIONS_P (currently_caught_type_list)
15201 && !tryblock_throws_ok)
15202 {
15203 if (DECL_CLINIT_P (current_function_decl))
15204 parse_error_context (wfl_operator,
15205 "Checked exception `%s' can't be thrown in initializer",
15206 lang_printable_name (type, 0));
15207 else
15208 parse_error_context (wfl_operator,
15209 "Checked exception `%s' isn't thrown from a `try' block",
15210 lang_printable_name (type, 0));
15211 }
15212 /* Otherwise, the current method doesn't have the appropriate
15213 throws declaration */
15214 else
15215 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15216 lang_printable_name (type, 0));
15217 return error_mark_node;
15218 }
15219
15220 if (! flag_emit_class_files && ! flag_emit_xref)
15221 BUILD_THROW (node, expr);
15222
15223 /* If doing xrefs, keep the location where the `throw' was seen. */
15224 if (flag_emit_xref)
15225 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15226 return node;
15227 }
15228
15229 /* Check that exception said to be thrown by method DECL can be
15230 effectively caught from where DECL is invoked. */
15231
15232 static void
15233 check_thrown_exceptions (location, decl)
15234 int location;
15235 tree decl;
15236 {
15237 tree throws;
15238 /* For all the unchecked exceptions thrown by DECL */
15239 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15240 throws = TREE_CHAIN (throws))
15241 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15242 {
15243 #if 1
15244 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15245 if (DECL_NAME (decl) == get_identifier ("clone"))
15246 continue;
15247 #endif
15248 EXPR_WFL_LINECOL (wfl_operator) = location;
15249 if (DECL_FINIT_P (current_function_decl))
15250 parse_error_context
15251 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15252 lang_printable_name (TREE_VALUE (throws), 0));
15253 else
15254 {
15255 parse_error_context
15256 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15257 lang_printable_name (TREE_VALUE (throws), 0),
15258 (DECL_INIT_P (current_function_decl) ?
15259 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15260 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15261 }
15262 }
15263 }
15264
15265 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15266 try-catch blocks, OR is listed in the `throws' clause of the
15267 current method. */
15268
15269 static int
15270 check_thrown_exceptions_do (exception)
15271 tree exception;
15272 {
15273 tree list = currently_caught_type_list;
15274 resolve_and_layout (exception, NULL_TREE);
15275 /* First, all the nested try-catch-finally at that stage. The
15276 last element contains `throws' clause exceptions, if any. */
15277 if (IS_UNCHECKED_EXCEPTION_P (exception))
15278 return 1;
15279 while (list)
15280 {
15281 tree caught;
15282 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15283 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15284 return 1;
15285 list = TREE_CHAIN (list);
15286 }
15287 return 0;
15288 }
15289
15290 static void
15291 purge_unchecked_exceptions (mdecl)
15292 tree mdecl;
15293 {
15294 tree throws = DECL_FUNCTION_THROWS (mdecl);
15295 tree new = NULL_TREE;
15296
15297 while (throws)
15298 {
15299 tree next = TREE_CHAIN (throws);
15300 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15301 {
15302 TREE_CHAIN (throws) = new;
15303 new = throws;
15304 }
15305 throws = next;
15306 }
15307 /* List is inverted here, but it doesn't matter */
15308 DECL_FUNCTION_THROWS (mdecl) = new;
15309 }
15310
15311 /* 15.24 Conditional Operator ?: */
15312
15313 static tree
15314 patch_conditional_expr (node, wfl_cond, wfl_op1)
15315 tree node, wfl_cond, wfl_op1;
15316 {
15317 tree cond = TREE_OPERAND (node, 0);
15318 tree op1 = TREE_OPERAND (node, 1);
15319 tree op2 = TREE_OPERAND (node, 2);
15320 tree resulting_type = NULL_TREE;
15321 tree t1, t2, patched;
15322 int error_found = 0;
15323
15324 /* Operands of ?: might be StringBuffers crafted as a result of a
15325 string concatenation. Obtain a descent operand here. */
15326 if ((patched = patch_string (op1)))
15327 TREE_OPERAND (node, 1) = op1 = patched;
15328 if ((patched = patch_string (op2)))
15329 TREE_OPERAND (node, 2) = op2 = patched;
15330
15331 t1 = TREE_TYPE (op1);
15332 t2 = TREE_TYPE (op2);
15333
15334 /* The first expression must be a boolean */
15335 if (TREE_TYPE (cond) != boolean_type_node)
15336 {
15337 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15338 parse_error_context (wfl_operator,
15339 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15340 lang_printable_name (TREE_TYPE (cond), 0));
15341 error_found = 1;
15342 }
15343
15344 /* Second and third can be numeric, boolean (i.e. primitive),
15345 references or null. Anything else results in an error */
15346 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15347 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15348 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15349 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15350 error_found = 1;
15351
15352 /* Determine the type of the conditional expression. Same types are
15353 easy to deal with */
15354 else if (t1 == t2)
15355 resulting_type = t1;
15356
15357 /* There are different rules for numeric types */
15358 else if (JNUMERIC_TYPE_P (t1))
15359 {
15360 /* if byte/short found, the resulting type is short */
15361 if ((t1 == byte_type_node && t2 == short_type_node)
15362 || (t1 == short_type_node && t2 == byte_type_node))
15363 resulting_type = short_type_node;
15364
15365 /* If t1 is a constant int and t2 is of type byte, short or char
15366 and t1's value fits in t2, then the resulting type is t2 */
15367 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15368 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15369 resulting_type = t2;
15370
15371 /* If t2 is a constant int and t1 is of type byte, short or char
15372 and t2's value fits in t1, then the resulting type is t1 */
15373 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15374 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15375 resulting_type = t1;
15376
15377 /* Otherwise, binary numeric promotion is applied and the
15378 resulting type is the promoted type of operand 1 and 2 */
15379 else
15380 resulting_type = binary_numeric_promotion (t1, t2,
15381 &TREE_OPERAND (node, 1),
15382 &TREE_OPERAND (node, 2));
15383 }
15384
15385 /* Cases of a reference and a null type */
15386 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15387 resulting_type = t1;
15388
15389 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15390 resulting_type = t2;
15391
15392 /* Last case: different reference types. If a type can be converted
15393 into the other one by assignment conversion, the latter
15394 determines the type of the expression */
15395 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15396 resulting_type = promote_type (t1);
15397
15398 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15399 resulting_type = promote_type (t2);
15400
15401 /* If we don't have any resulting type, we're in trouble */
15402 if (!resulting_type)
15403 {
15404 char *t = xstrdup (lang_printable_name (t1, 0));
15405 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15406 parse_error_context (wfl_operator,
15407 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15408 t, lang_printable_name (t2, 0));
15409 free (t);
15410 error_found = 1;
15411 }
15412
15413 if (error_found)
15414 {
15415 TREE_TYPE (node) = error_mark_node;
15416 return error_mark_node;
15417 }
15418
15419 TREE_TYPE (node) = resulting_type;
15420 TREE_SET_CODE (node, COND_EXPR);
15421 CAN_COMPLETE_NORMALLY (node) = 1;
15422 return node;
15423 }
15424
15425 /* Try to constant fold NODE.
15426 If NODE is not a constant expression, return NULL_EXPR.
15427 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15428
15429 static tree
15430 fold_constant_for_init (node, context)
15431 tree node;
15432 tree context;
15433 {
15434 tree op0, op1, val;
15435 enum tree_code code = TREE_CODE (node);
15436
15437 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
15438 return node;
15439
15440 switch (code)
15441 {
15442 case PLUS_EXPR:
15443 case MINUS_EXPR:
15444 case MULT_EXPR:
15445 case TRUNC_MOD_EXPR:
15446 case RDIV_EXPR:
15447 case LSHIFT_EXPR:
15448 case RSHIFT_EXPR:
15449 case URSHIFT_EXPR:
15450 case BIT_AND_EXPR:
15451 case BIT_XOR_EXPR:
15452 case BIT_IOR_EXPR:
15453 case TRUTH_ANDIF_EXPR:
15454 case TRUTH_ORIF_EXPR:
15455 case EQ_EXPR:
15456 case NE_EXPR:
15457 case GT_EXPR:
15458 case GE_EXPR:
15459 case LT_EXPR:
15460 case LE_EXPR:
15461 op0 = TREE_OPERAND (node, 0);
15462 op1 = TREE_OPERAND (node, 1);
15463 val = fold_constant_for_init (op0, context);
15464 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15465 return NULL_TREE;
15466 TREE_OPERAND (node, 0) = val;
15467 val = fold_constant_for_init (op1, context);
15468 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15469 return NULL_TREE;
15470 TREE_OPERAND (node, 1) = val;
15471 return patch_binop (node, op0, op1);
15472
15473 case UNARY_PLUS_EXPR:
15474 case NEGATE_EXPR:
15475 case TRUTH_NOT_EXPR:
15476 case BIT_NOT_EXPR:
15477 case CONVERT_EXPR:
15478 op0 = TREE_OPERAND (node, 0);
15479 val = fold_constant_for_init (op0, context);
15480 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15481 return NULL_TREE;
15482 TREE_OPERAND (node, 0) = val;
15483 return patch_unaryop (node, op0);
15484 break;
15485
15486 case COND_EXPR:
15487 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15488 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15489 return NULL_TREE;
15490 TREE_OPERAND (node, 0) = val;
15491 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15492 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15493 return NULL_TREE;
15494 TREE_OPERAND (node, 1) = val;
15495 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15496 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15497 return NULL_TREE;
15498 TREE_OPERAND (node, 2) = val;
15499 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15500 : TREE_OPERAND (node, 2);
15501
15502 case VAR_DECL:
15503 case FIELD_DECL:
15504 if (! FIELD_FINAL (node)
15505 || DECL_INITIAL (node) == NULL_TREE)
15506 return NULL_TREE;
15507 val = DECL_INITIAL (node);
15508 /* Guard against infinite recursion. */
15509 DECL_INITIAL (node) = NULL_TREE;
15510 val = fold_constant_for_init (val, node);
15511 DECL_INITIAL (node) = val;
15512 if (!val && CLASS_FINAL_VARIABLE_P (node))
15513 DECL_FIELD_FINAL_IUD (node) = 0;
15514 return val;
15515
15516 case EXPR_WITH_FILE_LOCATION:
15517 /* Compare java_complete_tree and resolve_expression_name. */
15518 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15519 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15520 {
15521 tree name = EXPR_WFL_NODE (node);
15522 tree decl;
15523 if (PRIMARY_P (node))
15524 return NULL_TREE;
15525 else if (! QUALIFIED_P (name))
15526 {
15527 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15528 if (decl == NULL_TREE
15529 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15530 return NULL_TREE;
15531 return fold_constant_for_init (decl, decl);
15532 }
15533 else
15534 {
15535 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15536 qualify_ambiguous_name (node);
15537 if (resolve_field_access (node, &decl, NULL)
15538 && decl != NULL_TREE)
15539 return fold_constant_for_init (decl, decl);
15540 return NULL_TREE;
15541 }
15542 }
15543 else
15544 {
15545 op0 = TREE_OPERAND (node, 0);
15546 val = fold_constant_for_init (op0, context);
15547 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15548 return NULL_TREE;
15549 TREE_OPERAND (node, 0) = val;
15550 return val;
15551 }
15552
15553 #ifdef USE_COMPONENT_REF
15554 case IDENTIFIER:
15555 case COMPONENT_REF:
15556 ?;
15557 #endif
15558
15559 default:
15560 return NULL_TREE;
15561 }
15562 }
15563
15564 #ifdef USE_COMPONENT_REF
15565 /* Context is 'T' for TypeName, 'P' for PackageName,
15566 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15567
15568 tree
15569 resolve_simple_name (name, context)
15570 tree name;
15571 int context;
15572 {
15573 }
15574
15575 tree
15576 resolve_qualified_name (name, context)
15577 tree name;
15578 int context;
15579 {
15580 }
15581 #endif
15582
15583 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15584
15585 static void
15586 mark_parser_ctxt (p)
15587 void *p;
15588 {
15589 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15590 int i;
15591
15592 if (!pc)
15593 return;
15594
15595 #ifndef JC1_LITE
15596 for (i = 0; i < 11; ++i)
15597 ggc_mark_tree (pc->modifier_ctx[i]);
15598 ggc_mark_tree (pc->class_type);
15599 ggc_mark_tree (pc->function_decl);
15600 ggc_mark_tree (pc->package);
15601 ggc_mark_tree (pc->incomplete_class);
15602 ggc_mark_tree (pc->gclass_list);
15603 ggc_mark_tree (pc->class_list);
15604 ggc_mark_tree (pc->current_parsed_class);
15605 ggc_mark_tree (pc->current_parsed_class_un);
15606 ggc_mark_tree (pc->non_static_initialized);
15607 ggc_mark_tree (pc->static_initialized);
15608 ggc_mark_tree (pc->instance_initializers);
15609 ggc_mark_tree (pc->import_list);
15610 ggc_mark_tree (pc->import_demand_list);
15611 ggc_mark_tree (pc->current_loop);
15612 ggc_mark_tree (pc->current_labeled_block);
15613 #endif /* JC1_LITE */
15614
15615 if (pc->next)
15616 mark_parser_ctxt (&pc->next);
15617 }
This page took 0.744353 seconds and 5 git commands to generate.