]> gcc.gnu.org Git - gcc.git/blob - gcc/java/parse.y
parse.h (JDEP_SOLV): Removed.
[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 tree find_name_in_single_imports PARAMS ((tree));
120 static void check_abstract_method_header PARAMS ((tree));
121 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
122 static tree resolve_expression_name PARAMS ((tree, tree *));
123 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
124 static int check_class_interface_creation PARAMS ((int, int, tree,
125 tree, tree, tree));
126 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
127 int *, tree *));
128 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
129 static int in_same_package PARAMS ((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 exit_block PARAMS ((void));
156 static tree lookup_name_in_blocks PARAMS ((tree));
157 static void maybe_absorb_scoping_blocks PARAMS ((void));
158 static tree build_method_invocation PARAMS ((tree, tree));
159 static tree build_new_invocation PARAMS ((tree, tree));
160 static tree build_assignment PARAMS ((int, int, tree, tree));
161 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
162 static int check_final_assignment PARAMS ((tree ,tree));
163 static tree patch_assignment PARAMS ((tree, tree, tree ));
164 static tree patch_binop PARAMS ((tree, tree, tree));
165 static tree build_unaryop PARAMS ((int, int, tree));
166 static tree build_incdec PARAMS ((int, int, tree, int));
167 static tree patch_unaryop PARAMS ((tree, tree));
168 static tree build_cast PARAMS ((int, tree, tree));
169 static tree build_null_of_type PARAMS ((tree));
170 static tree patch_cast PARAMS ((tree, tree));
171 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
172 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
173 static int valid_cast_to_p PARAMS ((tree, tree));
174 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
175 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
176 static tree try_reference_assignconv PARAMS ((tree, tree));
177 static tree build_unresolved_array_type PARAMS ((tree));
178 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
179 static tree build_array_ref PARAMS ((int, tree, tree));
180 static tree patch_array_ref PARAMS ((tree));
181 static tree make_qualified_name PARAMS ((tree, tree, int));
182 static tree merge_qualified_name PARAMS ((tree, tree));
183 static tree make_qualified_primary PARAMS ((tree, tree, int));
184 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
185 tree *, tree *));
186 static void qualify_ambiguous_name PARAMS ((tree));
187 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
188 static tree build_newarray_node PARAMS ((tree, tree, int));
189 static tree patch_newarray PARAMS ((tree));
190 static tree resolve_type_during_patch PARAMS ((tree));
191 static tree build_this PARAMS ((int));
192 static tree build_wfl_wrap PARAMS ((tree, int));
193 static tree build_return PARAMS ((int, tree));
194 static tree patch_return PARAMS ((tree));
195 static tree maybe_access_field PARAMS ((tree, tree, tree));
196 static int complete_function_arguments PARAMS ((tree));
197 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
198 tree, tree));
199 static int not_accessible_p PARAMS ((tree, tree, tree, int));
200 static void check_deprecation PARAMS ((tree, tree));
201 static int class_in_current_package PARAMS ((tree));
202 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
203 static tree patch_if_else_statement PARAMS ((tree));
204 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
205 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
206 static tree patch_exit_expr PARAMS ((tree));
207 static tree build_labeled_block PARAMS ((int, tree));
208 static tree finish_labeled_statement PARAMS ((tree, tree));
209 static tree build_bc_statement PARAMS ((int, int, tree));
210 static tree patch_bc_statement PARAMS ((tree));
211 static tree patch_loop_statement PARAMS ((tree));
212 static tree build_new_loop PARAMS ((tree));
213 static tree build_loop_body PARAMS ((int, tree, int));
214 static tree finish_loop_body PARAMS ((int, tree, tree, int));
215 static tree build_debugable_stmt PARAMS ((int, tree));
216 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
217 static tree patch_switch_statement PARAMS ((tree));
218 static tree string_constant_concatenation PARAMS ((tree, tree));
219 static tree build_string_concatenation PARAMS ((tree, tree));
220 static tree patch_string_cst PARAMS ((tree));
221 static tree patch_string PARAMS ((tree));
222 static tree build_try_statement PARAMS ((int, tree, tree));
223 static tree build_try_finally_statement PARAMS ((int, tree, tree));
224 static tree patch_try_statement PARAMS ((tree));
225 static tree patch_synchronized_statement PARAMS ((tree, tree));
226 static tree patch_throw_statement PARAMS ((tree, tree));
227 static void check_thrown_exceptions PARAMS ((int, tree));
228 static int check_thrown_exceptions_do PARAMS ((tree));
229 static void purge_unchecked_exceptions PARAMS ((tree));
230 static void check_throws_clauses PARAMS ((tree, tree, tree));
231 static void finish_method_declaration PARAMS ((tree));
232 static tree build_super_invocation PARAMS ((tree));
233 static int verify_constructor_circularity PARAMS ((tree, tree));
234 static char *constructor_circularity_msg PARAMS ((tree, tree));
235 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
236 int, int));
237 static const char *get_printable_method_name PARAMS ((tree));
238 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
239 static tree generate_finit PARAMS ((tree));
240 static void add_instance_initializer PARAMS ((tree));
241 static void fix_constructors PARAMS ((tree));
242 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
243 tree, int *));
244 static void craft_constructor PARAMS ((tree, tree));
245 static int verify_constructor_super PARAMS ((tree));
246 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
247 static void start_artificial_method_body PARAMS ((tree));
248 static void end_artificial_method_body PARAMS ((tree));
249 static int check_method_redefinition PARAMS ((tree, tree));
250 static int check_method_types_complete PARAMS ((tree));
251 static void java_check_regular_methods PARAMS ((tree));
252 static void java_check_abstract_methods PARAMS ((tree));
253 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
254 static void unreachable_stmt_error PARAMS ((tree));
255 static tree find_expr_with_wfl PARAMS ((tree));
256 static void missing_return_error PARAMS ((tree));
257 static tree build_new_array_init PARAMS ((int, tree));
258 static tree patch_new_array_init PARAMS ((tree, tree));
259 static tree maybe_build_array_element_wfl PARAMS ((tree));
260 static int array_constructor_check_entry PARAMS ((tree, tree));
261 static const char *purify_type_name PARAMS ((const char *));
262 static tree fold_constant_for_init PARAMS ((tree, tree));
263 static tree strip_out_static_field_access_decl PARAMS ((tree));
264 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
265 static void static_ref_err PARAMS ((tree, tree, tree));
266 static void parser_add_interface PARAMS ((tree, tree, tree));
267 static void add_superinterfaces PARAMS ((tree, tree));
268 static tree jdep_resolve_class PARAMS ((jdep *));
269 static int note_possible_classname PARAMS ((const char *, int));
270 static void java_complete_expand_classes PARAMS ((void));
271 static void java_complete_expand_class PARAMS ((tree));
272 static void java_complete_expand_methods PARAMS ((tree));
273 static tree cut_identifier_in_qualified PARAMS ((tree));
274 static tree java_stabilize_reference PARAMS ((tree));
275 static tree do_unary_numeric_promotion PARAMS ((tree));
276 static char * operator_string PARAMS ((tree));
277 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
278 static tree merge_string_cste PARAMS ((tree, tree, int));
279 static tree java_refold PARAMS ((tree));
280 static int java_decl_equiv PARAMS ((tree, tree));
281 static int binop_compound_p PARAMS ((enum tree_code));
282 static tree search_loop PARAMS ((tree));
283 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
284 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
285 static void java_check_abstract_method_definitions PARAMS ((tree));
286 static void java_debug_context_do PARAMS ((int));
287 static void java_parser_context_push_initialized_field PARAMS ((void));
288 static void java_parser_context_pop_initialized_field PARAMS ((void));
289 static tree reorder_static_initialized PARAMS ((tree));
290 static void java_parser_context_suspend PARAMS ((void));
291 static void java_parser_context_resume PARAMS ((void));
292 static int pop_current_osb PARAMS ((struct parser_ctxt *));
293
294 /* JDK 1.1 work. FIXME */
295
296 static tree maybe_make_nested_class_name PARAMS ((tree));
297 static void make_nested_class_name PARAMS ((tree));
298 static void set_nested_class_simple_name_value PARAMS ((tree, int));
299 static void link_nested_class_to_enclosing PARAMS ((void));
300 static tree find_as_inner_class PARAMS ((tree, tree, tree));
301 static tree find_as_inner_class_do PARAMS ((tree, tree));
302 static int check_inner_class_redefinition PARAMS ((tree, tree));
303
304 static tree build_thisn_assign PARAMS ((void));
305 static tree build_current_thisn PARAMS ((tree));
306 static tree build_access_to_thisn PARAMS ((tree, tree, int));
307 static tree maybe_build_thisn_access_method PARAMS ((tree));
308
309 static tree build_outer_field_access PARAMS ((tree, tree));
310 static tree build_outer_field_access_methods PARAMS ((tree));
311 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
312 tree, tree));
313 static tree build_outer_method_access_method PARAMS ((tree));
314 static tree build_new_access_id PARAMS ((void));
315 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
316 tree, tree));
317
318 static int outer_field_access_p PARAMS ((tree, tree));
319 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
320 tree *, tree *));
321 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
322 static tree build_incomplete_class_ref PARAMS ((int, tree));
323 static tree patch_incomplete_class_ref PARAMS ((tree));
324 static tree create_anonymous_class PARAMS ((int, tree));
325 static void patch_anonymous_class PARAMS ((tree, tree, tree));
326 static void add_inner_class_fields PARAMS ((tree, tree));
327
328 static tree build_dot_class_method PARAMS ((tree));
329 static tree build_dot_class_method_invocation PARAMS ((tree));
330 static void create_new_parser_context PARAMS ((int));
331 static void mark_parser_ctxt PARAMS ((void *));
332
333 /* Number of error found so far. */
334 int java_error_count;
335 /* Number of warning found so far. */
336 int java_warning_count;
337 /* Tell when not to fold, when doing xrefs */
338 int do_not_fold;
339 /* Cyclic inheritance report, as it can be set by layout_class */
340 const char *cyclic_inheritance_report;
341
342 /* Tell when we're within an instance initializer */
343 static int in_instance_initializer;
344
345 /* The current parser context */
346 struct parser_ctxt *ctxp;
347
348 /* List of things that were analyzed for which code will be generated */
349 struct parser_ctxt *ctxp_for_generation = NULL;
350
351 /* binop_lookup maps token to tree_code. It is used where binary
352 operations are involved and required by the parser. RDIV_EXPR
353 covers both integral/floating point division. The code is changed
354 once the type of both operator is worked out. */
355
356 static enum tree_code binop_lookup[19] =
357 {
358 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
359 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
360 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
361 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
362 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
363 };
364 #define BINOP_LOOKUP(VALUE) \
365 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
366
367 /* This is the end index for binary operators that can also be used
368 in compound assignements. */
369 #define BINOP_COMPOUND_CANDIDATES 11
370
371 /* The "$L" identifier we use to create labels. */
372 static tree label_id = NULL_TREE;
373
374 /* The "StringBuffer" identifier used for the String `+' operator. */
375 static tree wfl_string_buffer = NULL_TREE;
376
377 /* The "append" identifier used for String `+' operator. */
378 static tree wfl_append = NULL_TREE;
379
380 /* The "toString" identifier used for String `+' operator. */
381 static tree wfl_to_string = NULL_TREE;
382
383 /* The "java.lang" import qualified name. */
384 static tree java_lang_id = NULL_TREE;
385
386 /* The generated `inst$' identifier used for generated enclosing
387 instance/field access functions. */
388 static tree inst_id = NULL_TREE;
389
390 /* The "java.lang.Cloneable" qualified name. */
391 static tree java_lang_cloneable = NULL_TREE;
392
393 /* The "java.io.Serializable" qualified name. */
394 static tree java_io_serializable = NULL_TREE;
395
396 /* Context and flag for static blocks */
397 static tree current_static_block = NULL_TREE;
398
399 /* The generated `write_parm_value$' identifier. */
400 static tree wpv_id;
401
402 /* The list of all packages we've seen so far */
403 static tree package_list = NULL_TREE;
404
405 /* Hold THIS for the scope of the current public method decl. */
406 static tree current_this;
407
408 /* Hold a list of catch clauses list. The first element of this list is
409 the list of the catch clauses of the currently analysed try block. */
410 static tree currently_caught_type_list;
411
412 static tree src_parse_roots[1] = { NULL_TREE };
413
414 /* All classes seen from source code */
415 #define gclass_list src_parse_roots[0]
416
417 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
418 line and point it out. */
419 /* Should point out the one that don't fit. ASCII/unicode, going
420 backward. FIXME */
421
422 #define check_modifiers(__message, __value, __mask) do { \
423 if ((__value) & ~(__mask)) \
424 { \
425 int i, remainder = (__value) & ~(__mask); \
426 for (i = 0; i <= 10; i++) \
427 if ((1 << i) & remainder) \
428 parse_error_context (ctxp->modifier_ctx [i], (__message), \
429 java_accstring_lookup (1 << i)); \
430 } \
431 } while (0)
432
433 %}
434
435 %union {
436 tree node;
437 int sub_token;
438 struct {
439 int token;
440 int location;
441 } operator;
442 int value;
443 }
444
445 %{
446 #include "lex.c"
447 %}
448
449 %pure_parser
450
451 /* Things defined here have to match the order of what's in the
452 binop_lookup table. */
453
454 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
455 %token LS_TK SRS_TK ZRS_TK
456 %token AND_TK XOR_TK OR_TK
457 %token BOOL_AND_TK BOOL_OR_TK
458 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
459
460 /* This maps to the same binop_lookup entry than the token above */
461
462 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
463 %token REM_ASSIGN_TK
464 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
465 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
466
467
468 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
469
470 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
471 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
472 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
473 %token PAD_TK ABSTRACT_TK MODIFIER_TK
474 %token STRICT_TK
475
476 /* Keep those two in order, too */
477 %token DECR_TK INCR_TK
478
479 /* From now one, things can be in any order */
480
481 %token DEFAULT_TK IF_TK THROW_TK
482 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
483 %token THROWS_TK BREAK_TK IMPORT_TK
484 %token ELSE_TK INSTANCEOF_TK RETURN_TK
485 %token VOID_TK CATCH_TK INTERFACE_TK
486 %token CASE_TK EXTENDS_TK FINALLY_TK
487 %token SUPER_TK WHILE_TK CLASS_TK
488 %token SWITCH_TK CONST_TK TRY_TK
489 %token FOR_TK NEW_TK CONTINUE_TK
490 %token GOTO_TK PACKAGE_TK THIS_TK
491
492 %token BYTE_TK SHORT_TK INT_TK LONG_TK
493 %token CHAR_TK INTEGRAL_TK
494
495 %token FLOAT_TK DOUBLE_TK FP_TK
496
497 %token ID_TK
498
499 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
500
501 %token ASSIGN_ANY_TK ASSIGN_TK
502 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
503
504 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
505 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
506
507 %type <value> modifiers MODIFIER_TK final synchronized
508
509 %type <node> super ID_TK identifier
510 %type <node> name simple_name qualified_name
511 %type <node> type_declaration compilation_unit
512 field_declaration method_declaration extends_interfaces
513 interfaces interface_type_list
514 class_member_declaration
515 import_declarations package_declaration
516 type_declarations interface_body
517 interface_member_declaration constant_declaration
518 interface_member_declarations interface_type
519 abstract_method_declaration interface_type_list
520 %type <node> class_body_declaration class_member_declaration
521 static_initializer constructor_declaration block
522 %type <node> class_body_declarations constructor_header
523 %type <node> class_or_interface_type class_type class_type_list
524 constructor_declarator explicit_constructor_invocation
525 %type <node> dim_expr dim_exprs this_or_super throws
526
527 %type <node> variable_declarator_id variable_declarator
528 variable_declarators variable_initializer
529 variable_initializers constructor_body
530 array_initializer
531
532 %type <node> class_body block_end constructor_block_end
533 %type <node> statement statement_without_trailing_substatement
534 labeled_statement if_then_statement label_decl
535 if_then_else_statement while_statement for_statement
536 statement_nsi labeled_statement_nsi do_statement
537 if_then_else_statement_nsi while_statement_nsi
538 for_statement_nsi statement_expression_list for_init
539 for_update statement_expression expression_statement
540 primary_no_new_array expression primary
541 array_creation_expression array_type
542 class_instance_creation_expression field_access
543 method_invocation array_access something_dot_new
544 argument_list postfix_expression while_expression
545 post_increment_expression post_decrement_expression
546 unary_expression_not_plus_minus unary_expression
547 pre_increment_expression pre_decrement_expression
548 unary_expression_not_plus_minus cast_expression
549 multiplicative_expression additive_expression
550 shift_expression relational_expression
551 equality_expression and_expression
552 exclusive_or_expression inclusive_or_expression
553 conditional_and_expression conditional_or_expression
554 conditional_expression assignment_expression
555 left_hand_side assignment for_header for_begin
556 constant_expression do_statement_begin empty_statement
557 switch_statement synchronized_statement throw_statement
558 try_statement switch_expression switch_block
559 catches catch_clause catch_clause_parameter finally
560 anonymous_class_creation
561 %type <node> return_statement break_statement continue_statement
562
563 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
564 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
565 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
566 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
567 %type <operator> ASSIGN_ANY_TK assignment_operator
568 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
569 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
570 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
571 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
572 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
573 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
574 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
575 %type <operator> NEW_TK
576
577 %type <node> method_body
578
579 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
580 STRING_LIT_TK NULL_TK VOID_TK
581
582 %type <node> IF_TK WHILE_TK FOR_TK
583
584 %type <node> formal_parameter_list formal_parameter
585 method_declarator method_header
586
587 %type <node> primitive_type reference_type type
588 BOOLEAN_TK INTEGRAL_TK FP_TK
589
590 /* Added or modified JDK 1.1 rule types */
591 %type <node> type_literals
592
593 %%
594 /* 19.2 Production from 2.3: The Syntactic Grammar */
595 goal:
596 {
597 /* Register static variables with the garbage
598 collector. */
599 ggc_add_tree_root (&label_id, 1);
600 ggc_add_tree_root (&wfl_string_buffer, 1);
601 ggc_add_tree_root (&wfl_append, 1);
602 ggc_add_tree_root (&wfl_to_string, 1);
603 ggc_add_tree_root (&java_lang_id, 1);
604 ggc_add_tree_root (&inst_id, 1);
605 ggc_add_tree_root (&java_lang_cloneable, 1);
606 ggc_add_tree_root (&java_io_serializable, 1);
607 ggc_add_tree_root (&current_static_block, 1);
608 ggc_add_tree_root (&wpv_id, 1);
609 ggc_add_tree_root (&package_list, 1);
610 ggc_add_tree_root (&current_this, 1);
611 ggc_add_tree_root (&currently_caught_type_list, 1);
612 ggc_add_root (&ctxp, 1,
613 sizeof (struct parser_ctxt *),
614 mark_parser_ctxt);
615 ggc_add_root (&ctxp_for_generation, 1,
616 sizeof (struct parser_ctxt *),
617 mark_parser_ctxt);
618 }
619 compilation_unit
620 {}
621 ;
622
623 /* 19.3 Productions from 3: Lexical structure */
624 literal:
625 INT_LIT_TK
626 | FP_LIT_TK
627 | BOOL_LIT_TK
628 | CHAR_LIT_TK
629 | STRING_LIT_TK
630 | NULL_TK
631 ;
632
633 /* 19.4 Productions from 4: Types, Values and Variables */
634 type:
635 primitive_type
636 | reference_type
637 ;
638
639 primitive_type:
640 INTEGRAL_TK
641 | FP_TK
642 | BOOLEAN_TK
643 ;
644
645 reference_type:
646 class_or_interface_type
647 | array_type
648 ;
649
650 class_or_interface_type:
651 name
652 ;
653
654 class_type:
655 class_or_interface_type /* Default rule */
656 ;
657
658 interface_type:
659 class_or_interface_type
660 ;
661
662 array_type:
663 primitive_type dims
664 {
665 int osb = pop_current_osb (ctxp);
666 tree t = build_java_array_type (($1), -1);
667 CLASS_LOADED_P (t) = 1;
668 while (--osb)
669 t = build_unresolved_array_type (t);
670 $$ = t;
671 }
672 | name dims
673 {
674 int osb = pop_current_osb (ctxp);
675 tree t = $1;
676 while (osb--)
677 t = build_unresolved_array_type (t);
678 $$ = t;
679 }
680 ;
681
682 /* 19.5 Productions from 6: Names */
683 name:
684 simple_name /* Default rule */
685 | qualified_name /* Default rule */
686 ;
687
688 simple_name:
689 identifier /* Default rule */
690 ;
691
692 qualified_name:
693 name DOT_TK identifier
694 { $$ = make_qualified_name ($1, $3, $2.location); }
695 ;
696
697 identifier:
698 ID_TK
699 ;
700
701 /* 19.6: Production from 7: Packages */
702 compilation_unit:
703 {$$ = NULL;}
704 | package_declaration
705 | import_declarations
706 | type_declarations
707 | package_declaration import_declarations
708 | package_declaration type_declarations
709 | import_declarations type_declarations
710 | package_declaration import_declarations type_declarations
711 ;
712
713 import_declarations:
714 import_declaration
715 {
716 $$ = NULL;
717 }
718 | import_declarations import_declaration
719 {
720 $$ = NULL;
721 }
722 ;
723
724 type_declarations:
725 type_declaration
726 | type_declarations type_declaration
727 ;
728
729 package_declaration:
730 PACKAGE_TK name SC_TK
731 {
732 ctxp->package = EXPR_WFL_NODE ($2);
733 register_package (ctxp->package);
734 }
735 | PACKAGE_TK error
736 {yyerror ("Missing name"); RECOVER;}
737 | PACKAGE_TK name error
738 {yyerror ("';' expected"); RECOVER;}
739 ;
740
741 import_declaration:
742 single_type_import_declaration
743 | type_import_on_demand_declaration
744 ;
745
746 single_type_import_declaration:
747 IMPORT_TK name SC_TK
748 {
749 tree name = EXPR_WFL_NODE ($2), last_name;
750 int i = IDENTIFIER_LENGTH (name)-1;
751 const char *last = &IDENTIFIER_POINTER (name)[i];
752 while (last != IDENTIFIER_POINTER (name))
753 {
754 if (last [0] == '.')
755 break;
756 last--;
757 }
758 last_name = get_identifier (++last);
759 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
760 {
761 tree err = find_name_in_single_imports (last_name);
762 if (err && err != name)
763 parse_error_context
764 ($2, "Ambiguous class: `%s' and `%s'",
765 IDENTIFIER_POINTER (name),
766 IDENTIFIER_POINTER (err));
767 else
768 REGISTER_IMPORT ($2, last_name);
769 }
770 else
771 REGISTER_IMPORT ($2, last_name);
772 }
773 | IMPORT_TK error
774 {yyerror ("Missing name"); RECOVER;}
775 | IMPORT_TK name error
776 {yyerror ("';' expected"); RECOVER;}
777 ;
778
779 type_import_on_demand_declaration:
780 IMPORT_TK name DOT_TK MULT_TK SC_TK
781 {
782 tree name = EXPR_WFL_NODE ($2);
783 /* Don't import java.lang.* twice. */
784 if (name != java_lang_id)
785 {
786 read_import_dir ($2);
787 ctxp->import_demand_list =
788 chainon (ctxp->import_demand_list,
789 build_tree_list ($2, NULL_TREE));
790 }
791 }
792 | IMPORT_TK name DOT_TK error
793 {yyerror ("'*' expected"); RECOVER;}
794 | IMPORT_TK name DOT_TK MULT_TK error
795 {yyerror ("';' expected"); RECOVER;}
796 ;
797
798 type_declaration:
799 class_declaration
800 { end_class_declaration (0); }
801 | interface_declaration
802 { end_class_declaration (0); }
803 | empty_statement
804 | error
805 {
806 YYERROR_NOW;
807 yyerror ("Class or interface declaration expected");
808 }
809 ;
810
811 /* 19.7 Shortened from the original:
812 modifiers: modifier | modifiers modifier
813 modifier: any of public... */
814 modifiers:
815 MODIFIER_TK
816 {
817 $$ = (1 << $1);
818 }
819 | modifiers MODIFIER_TK
820 {
821 int acc = (1 << $2);
822 if ($$ & acc)
823 parse_error_context
824 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
825 java_accstring_lookup (acc));
826 else
827 {
828 $$ |= acc;
829 }
830 }
831 ;
832
833 /* 19.8.1 Production from $8.1: Class Declaration */
834 class_declaration:
835 modifiers CLASS_TK identifier super interfaces
836 { create_class ($1, $3, $4, $5); }
837 class_body
838 | CLASS_TK identifier super interfaces
839 { create_class (0, $2, $3, $4); }
840 class_body
841 | modifiers CLASS_TK error
842 {yyerror ("Missing class name"); RECOVER;}
843 | CLASS_TK error
844 {yyerror ("Missing class name"); RECOVER;}
845 | CLASS_TK identifier error
846 {
847 if (!ctxp->class_err) yyerror ("'{' expected");
848 DRECOVER(class1);
849 }
850 | modifiers CLASS_TK identifier error
851 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
852 ;
853
854 super:
855 { $$ = NULL; }
856 | EXTENDS_TK class_type
857 { $$ = $2; }
858 | EXTENDS_TK class_type error
859 {yyerror ("'{' expected"); ctxp->class_err=1;}
860 | EXTENDS_TK error
861 {yyerror ("Missing super class name"); ctxp->class_err=1;}
862 ;
863
864 interfaces:
865 { $$ = NULL_TREE; }
866 | IMPLEMENTS_TK interface_type_list
867 { $$ = $2; }
868 | IMPLEMENTS_TK error
869 {
870 ctxp->class_err=1;
871 yyerror ("Missing interface name");
872 }
873 ;
874
875 interface_type_list:
876 interface_type
877 {
878 ctxp->interface_number = 1;
879 $$ = build_tree_list ($1, NULL_TREE);
880 }
881 | interface_type_list C_TK interface_type
882 {
883 ctxp->interface_number++;
884 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
885 }
886 | interface_type_list C_TK error
887 {yyerror ("Missing interface name"); RECOVER;}
888 ;
889
890 class_body:
891 OCB_TK CCB_TK
892 {
893 /* Store the location of the `}' when doing xrefs */
894 if (flag_emit_xref)
895 DECL_END_SOURCE_LINE (GET_CPC ()) =
896 EXPR_WFL_ADD_COL ($2.location, 1);
897 $$ = GET_CPC ();
898 }
899 | OCB_TK class_body_declarations CCB_TK
900 {
901 /* Store the location of the `}' when doing xrefs */
902 if (flag_emit_xref)
903 DECL_END_SOURCE_LINE (GET_CPC ()) =
904 EXPR_WFL_ADD_COL ($3.location, 1);
905 $$ = GET_CPC ();
906 }
907 ;
908
909 class_body_declarations:
910 class_body_declaration
911 | class_body_declarations class_body_declaration
912 ;
913
914 class_body_declaration:
915 class_member_declaration
916 | static_initializer
917 | constructor_declaration
918 | block /* Added, JDK1.1, instance initializer */
919 {
920 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
921 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
922 }
923 ;
924
925 class_member_declaration:
926 field_declaration
927 | method_declaration
928 | class_declaration /* Added, JDK1.1 inner classes */
929 { end_class_declaration (1); }
930 | interface_declaration /* Added, JDK1.1 inner interfaces */
931 { end_class_declaration (1); }
932 | empty_statement
933 ;
934
935 /* 19.8.2 Productions from 8.3: Field Declarations */
936 field_declaration:
937 type variable_declarators SC_TK
938 { register_fields (0, $1, $2); }
939 | modifiers type variable_declarators SC_TK
940 {
941 check_modifiers
942 ("Illegal modifier `%s' for field declaration",
943 $1, FIELD_MODIFIERS);
944 check_modifiers_consistency ($1);
945 register_fields ($1, $2, $3);
946 }
947 ;
948
949 variable_declarators:
950 /* Should we use build_decl_list () instead ? FIXME */
951 variable_declarator /* Default rule */
952 | variable_declarators C_TK variable_declarator
953 { $$ = chainon ($1, $3); }
954 | variable_declarators C_TK error
955 {yyerror ("Missing term"); RECOVER;}
956 ;
957
958 variable_declarator:
959 variable_declarator_id
960 { $$ = build_tree_list ($1, NULL_TREE); }
961 | variable_declarator_id ASSIGN_TK variable_initializer
962 {
963 if (java_error_count)
964 $3 = NULL_TREE;
965 $$ = build_tree_list
966 ($1, build_assignment ($2.token, $2.location, $1, $3));
967 }
968 | variable_declarator_id ASSIGN_TK error
969 {
970 yyerror ("Missing variable initializer");
971 $$ = build_tree_list ($1, NULL_TREE);
972 RECOVER;
973 }
974 | variable_declarator_id ASSIGN_TK variable_initializer error
975 {
976 yyerror ("';' expected");
977 $$ = build_tree_list ($1, NULL_TREE);
978 RECOVER;
979 }
980 ;
981
982 variable_declarator_id:
983 identifier
984 | variable_declarator_id OSB_TK CSB_TK
985 { $$ = build_unresolved_array_type ($1); }
986 | identifier error
987 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
988 | variable_declarator_id OSB_TK error
989 {
990 tree node = java_lval.node;
991 if (node && (TREE_CODE (node) == INTEGER_CST
992 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
993 yyerror ("Can't specify array dimension in a declaration");
994 else
995 yyerror ("']' expected");
996 DRECOVER(vdi);
997 }
998 | variable_declarator_id CSB_TK error
999 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1000 ;
1001
1002 variable_initializer:
1003 expression
1004 | array_initializer
1005 ;
1006
1007 /* 19.8.3 Productions from 8.4: Method Declarations */
1008 method_declaration:
1009 method_header
1010 {
1011 current_function_decl = $1;
1012 if (current_function_decl
1013 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1014 source_start_java_method (current_function_decl);
1015 else
1016 current_function_decl = NULL_TREE;
1017 }
1018 method_body
1019 { finish_method_declaration ($3); }
1020 | method_header error
1021 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1022 ;
1023
1024 method_header:
1025 type method_declarator throws
1026 { $$ = method_header (0, $1, $2, $3); }
1027 | VOID_TK method_declarator throws
1028 { $$ = method_header (0, void_type_node, $2, $3); }
1029 | modifiers type method_declarator throws
1030 { $$ = method_header ($1, $2, $3, $4); }
1031 | modifiers VOID_TK method_declarator throws
1032 { $$ = method_header ($1, void_type_node, $3, $4); }
1033 | type error
1034 {
1035 yyerror ("Invalid method declaration, method name required");
1036 RECOVER;
1037 }
1038 | modifiers type error
1039 {RECOVER;}
1040 | VOID_TK error
1041 {yyerror ("Identifier expected"); RECOVER;}
1042 | modifiers VOID_TK error
1043 {yyerror ("Identifier expected"); RECOVER;}
1044 | modifiers error
1045 {
1046 yyerror ("Invalid method declaration, return type required");
1047 RECOVER;
1048 }
1049 ;
1050
1051 method_declarator:
1052 identifier OP_TK CP_TK
1053 {
1054 ctxp->formal_parameter_number = 0;
1055 $$ = method_declarator ($1, NULL_TREE);
1056 }
1057 | identifier OP_TK formal_parameter_list CP_TK
1058 { $$ = method_declarator ($1, $3); }
1059 | method_declarator OSB_TK CSB_TK
1060 {
1061 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1062 TREE_PURPOSE ($1) =
1063 build_unresolved_array_type (TREE_PURPOSE ($1));
1064 parse_warning_context
1065 (wfl_operator,
1066 "Discouraged form of returned type specification");
1067 }
1068 | identifier OP_TK error
1069 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1070 | method_declarator OSB_TK error
1071 {yyerror ("']' expected"); RECOVER;}
1072 ;
1073
1074 formal_parameter_list:
1075 formal_parameter
1076 {
1077 ctxp->formal_parameter_number = 1;
1078 }
1079 | formal_parameter_list C_TK formal_parameter
1080 {
1081 ctxp->formal_parameter_number += 1;
1082 $$ = chainon ($1, $3);
1083 }
1084 | formal_parameter_list C_TK error
1085 { yyerror ("Missing formal parameter term"); RECOVER; }
1086 ;
1087
1088 formal_parameter:
1089 type variable_declarator_id
1090 {
1091 $$ = build_tree_list ($2, $1);
1092 }
1093 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1094 {
1095 $$ = build_tree_list ($3, $2);
1096 ARG_FINAL_P ($$) = 1;
1097 }
1098 | type error
1099 {
1100 yyerror ("Missing identifier"); RECOVER;
1101 $$ = NULL_TREE;
1102 }
1103 | final type error
1104 {
1105 yyerror ("Missing identifier"); RECOVER;
1106 $$ = NULL_TREE;
1107 }
1108 ;
1109
1110 final:
1111 modifiers
1112 {
1113 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1114 $1, ACC_FINAL);
1115 if ($1 != ACC_FINAL)
1116 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1117 }
1118 ;
1119
1120 throws:
1121 { $$ = NULL_TREE; }
1122 | THROWS_TK class_type_list
1123 { $$ = $2; }
1124 | THROWS_TK error
1125 {yyerror ("Missing class type term"); RECOVER;}
1126 ;
1127
1128 class_type_list:
1129 class_type
1130 { $$ = build_tree_list ($1, $1); }
1131 | class_type_list C_TK class_type
1132 { $$ = tree_cons ($3, $3, $1); }
1133 | class_type_list C_TK error
1134 {yyerror ("Missing class type term"); RECOVER;}
1135 ;
1136
1137 method_body:
1138 block
1139 | SC_TK { $$ = NULL_TREE; }
1140 ;
1141
1142 /* 19.8.4 Productions from 8.5: Static Initializers */
1143 static_initializer:
1144 static block
1145 {
1146 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1147 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1148 current_static_block = NULL_TREE;
1149 }
1150 ;
1151
1152 static: /* Test lval.sub_token here */
1153 modifiers
1154 {
1155 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1156 /* Can't have a static initializer in an innerclass */
1157 if ($1 | ACC_STATIC &&
1158 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1159 parse_error_context
1160 (MODIFIER_WFL (STATIC_TK),
1161 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1162 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1163 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1164 }
1165 ;
1166
1167 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1168 constructor_declaration:
1169 constructor_header
1170 {
1171 current_function_decl = $1;
1172 source_start_java_method (current_function_decl);
1173 }
1174 constructor_body
1175 { finish_method_declaration ($3); }
1176 ;
1177
1178 constructor_header:
1179 constructor_declarator throws
1180 { $$ = method_header (0, NULL_TREE, $1, $2); }
1181 | modifiers constructor_declarator throws
1182 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1183 ;
1184
1185 constructor_declarator:
1186 simple_name OP_TK CP_TK
1187 {
1188 ctxp->formal_parameter_number = 0;
1189 $$ = method_declarator ($1, NULL_TREE);
1190 }
1191 | simple_name OP_TK formal_parameter_list CP_TK
1192 { $$ = method_declarator ($1, $3); }
1193 ;
1194
1195 constructor_body:
1196 /* Unlike regular method, we always need a complete (empty)
1197 body so we can safely perform all the required code
1198 addition (super invocation and field initialization) */
1199 block_begin constructor_block_end
1200 {
1201 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1202 $$ = $2;
1203 }
1204 | block_begin explicit_constructor_invocation constructor_block_end
1205 { $$ = $3; }
1206 | block_begin block_statements constructor_block_end
1207 { $$ = $3; }
1208 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1209 { $$ = $4; }
1210 ;
1211
1212 constructor_block_end:
1213 block_end
1214 ;
1215
1216 /* Error recovery for that rule moved down expression_statement: rule. */
1217 explicit_constructor_invocation:
1218 this_or_super OP_TK CP_TK SC_TK
1219 {
1220 $$ = build_method_invocation ($1, NULL_TREE);
1221 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1222 $$ = java_method_add_stmt (current_function_decl, $$);
1223 }
1224 | this_or_super OP_TK argument_list CP_TK SC_TK
1225 {
1226 $$ = build_method_invocation ($1, $3);
1227 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1228 $$ = java_method_add_stmt (current_function_decl, $$);
1229 }
1230 /* Added, JDK1.1 inner classes. Modified because the rule
1231 'primary' couldn't work. */
1232 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1233 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1234 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1235 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1236 ;
1237
1238 this_or_super: /* Added, simplifies error diagnostics */
1239 THIS_TK
1240 {
1241 tree wfl = build_wfl_node (this_identifier_node);
1242 EXPR_WFL_LINECOL (wfl) = $1.location;
1243 $$ = wfl;
1244 }
1245 | SUPER_TK
1246 {
1247 tree wfl = build_wfl_node (super_identifier_node);
1248 EXPR_WFL_LINECOL (wfl) = $1.location;
1249 $$ = wfl;
1250 }
1251 ;
1252
1253 /* 19.9 Productions from 9: Interfaces */
1254 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1255 interface_declaration:
1256 INTERFACE_TK identifier
1257 { create_interface (0, $2, NULL_TREE); }
1258 interface_body
1259 | modifiers INTERFACE_TK identifier
1260 { create_interface ($1, $3, NULL_TREE); }
1261 interface_body
1262 | INTERFACE_TK identifier extends_interfaces
1263 { create_interface (0, $2, $3); }
1264 interface_body
1265 | modifiers INTERFACE_TK identifier extends_interfaces
1266 { create_interface ($1, $3, $4); }
1267 interface_body
1268 | INTERFACE_TK identifier error
1269 {yyerror ("'{' expected"); RECOVER;}
1270 | modifiers INTERFACE_TK identifier error
1271 {yyerror ("'{' expected"); RECOVER;}
1272 ;
1273
1274 extends_interfaces:
1275 EXTENDS_TK interface_type
1276 {
1277 ctxp->interface_number = 1;
1278 $$ = build_tree_list ($2, NULL_TREE);
1279 }
1280 | extends_interfaces C_TK interface_type
1281 {
1282 ctxp->interface_number++;
1283 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1284 }
1285 | EXTENDS_TK error
1286 {yyerror ("Invalid interface type"); RECOVER;}
1287 | extends_interfaces C_TK error
1288 {yyerror ("Missing term"); RECOVER;}
1289 ;
1290
1291 interface_body:
1292 OCB_TK CCB_TK
1293 { $$ = NULL_TREE; }
1294 | OCB_TK interface_member_declarations CCB_TK
1295 { $$ = NULL_TREE; }
1296 ;
1297
1298 interface_member_declarations:
1299 interface_member_declaration
1300 | interface_member_declarations interface_member_declaration
1301 ;
1302
1303 interface_member_declaration:
1304 constant_declaration
1305 | abstract_method_declaration
1306 | class_declaration /* Added, JDK1.1 inner classes */
1307 { end_class_declaration (1); }
1308 | interface_declaration /* Added, JDK1.1 inner interfaces */
1309 { end_class_declaration (1); }
1310 ;
1311
1312 constant_declaration:
1313 field_declaration
1314 ;
1315
1316 abstract_method_declaration:
1317 method_header SC_TK
1318 {
1319 check_abstract_method_header ($1);
1320 current_function_decl = NULL_TREE; /* FIXME ? */
1321 }
1322 | method_header error
1323 {yyerror ("';' expected"); RECOVER;}
1324 ;
1325
1326 /* 19.10 Productions from 10: Arrays */
1327 array_initializer:
1328 OCB_TK CCB_TK
1329 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1330 | OCB_TK variable_initializers CCB_TK
1331 { $$ = build_new_array_init ($1.location, $2); }
1332 | OCB_TK variable_initializers C_TK CCB_TK
1333 { $$ = build_new_array_init ($1.location, $2); }
1334 ;
1335
1336 variable_initializers:
1337 variable_initializer
1338 {
1339 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1340 $1, NULL_TREE);
1341 }
1342 | variable_initializers C_TK variable_initializer
1343 {
1344 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1345 }
1346 | variable_initializers C_TK error
1347 {yyerror ("Missing term"); RECOVER;}
1348 ;
1349
1350 /* 19.11 Production from 14: Blocks and Statements */
1351 block:
1352 OCB_TK CCB_TK
1353 {
1354 /* Store the location of the `}' when doing xrefs */
1355 if (current_function_decl && flag_emit_xref)
1356 DECL_END_SOURCE_LINE (current_function_decl) =
1357 EXPR_WFL_ADD_COL ($2.location, 1);
1358 $$ = empty_stmt_node;
1359 }
1360 | block_begin block_statements block_end
1361 { $$ = $3; }
1362 ;
1363
1364 block_begin:
1365 OCB_TK
1366 { enter_block (); }
1367 ;
1368
1369 block_end:
1370 CCB_TK
1371 {
1372 maybe_absorb_scoping_blocks ();
1373 /* Store the location of the `}' when doing xrefs */
1374 if (current_function_decl && flag_emit_xref)
1375 DECL_END_SOURCE_LINE (current_function_decl) =
1376 EXPR_WFL_ADD_COL ($1.location, 1);
1377 $$ = exit_block ();
1378 if (!BLOCK_SUBBLOCKS ($$))
1379 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1380 }
1381 ;
1382
1383 block_statements:
1384 block_statement
1385 | block_statements block_statement
1386 ;
1387
1388 block_statement:
1389 local_variable_declaration_statement
1390 | statement
1391 { java_method_add_stmt (current_function_decl, $1); }
1392 | class_declaration /* Added, JDK1.1 local classes */
1393 {
1394 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1395 end_class_declaration (1);
1396 }
1397 ;
1398
1399 local_variable_declaration_statement:
1400 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1401 ;
1402
1403 local_variable_declaration:
1404 type variable_declarators
1405 { declare_local_variables (0, $1, $2); }
1406 | final type variable_declarators /* Added, JDK1.1 final locals */
1407 { declare_local_variables ($1, $2, $3); }
1408 ;
1409
1410 statement:
1411 statement_without_trailing_substatement
1412 | labeled_statement
1413 | if_then_statement
1414 | if_then_else_statement
1415 | while_statement
1416 | for_statement
1417 { $$ = exit_block (); }
1418 ;
1419
1420 statement_nsi:
1421 statement_without_trailing_substatement
1422 | labeled_statement_nsi
1423 | if_then_else_statement_nsi
1424 | while_statement_nsi
1425 | for_statement_nsi
1426 { $$ = exit_block (); }
1427 ;
1428
1429 statement_without_trailing_substatement:
1430 block
1431 | empty_statement
1432 | expression_statement
1433 | switch_statement
1434 | do_statement
1435 | break_statement
1436 | continue_statement
1437 | return_statement
1438 | synchronized_statement
1439 | throw_statement
1440 | try_statement
1441 ;
1442
1443 empty_statement:
1444 SC_TK
1445 {
1446 if (flag_extraneous_semicolon)
1447 {
1448 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1449 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1450 }
1451 $$ = empty_stmt_node;
1452 }
1453 ;
1454
1455 label_decl:
1456 identifier REL_CL_TK
1457 {
1458 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1459 EXPR_WFL_NODE ($1));
1460 pushlevel (2);
1461 push_labeled_block ($$);
1462 PUSH_LABELED_BLOCK ($$);
1463 }
1464 ;
1465
1466 labeled_statement:
1467 label_decl statement
1468 { $$ = finish_labeled_statement ($1, $2); }
1469 | identifier error
1470 {yyerror ("':' expected"); RECOVER;}
1471 ;
1472
1473 labeled_statement_nsi:
1474 label_decl statement_nsi
1475 { $$ = finish_labeled_statement ($1, $2); }
1476 ;
1477
1478 /* We concentrate here a bunch of error handling rules that we couldn't write
1479 earlier, because expression_statement catches a missing ';'. */
1480 expression_statement:
1481 statement_expression SC_TK
1482 {
1483 /* We have a statement. Generate a WFL around it so
1484 we can debug it */
1485 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1486 /* We know we have a statement, so set the debug
1487 info to be eventually generate here. */
1488 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1489 }
1490 | error SC_TK
1491 {
1492 YYNOT_TWICE yyerror ("Invalid expression statement");
1493 DRECOVER (expr_stmt);
1494 }
1495 | error OCB_TK
1496 {
1497 YYNOT_TWICE yyerror ("Invalid expression statement");
1498 DRECOVER (expr_stmt);
1499 }
1500 | error CCB_TK
1501 {
1502 YYNOT_TWICE yyerror ("Invalid expression statement");
1503 DRECOVER (expr_stmt);
1504 }
1505 | this_or_super OP_TK error
1506 {yyerror ("')' expected"); RECOVER;}
1507 | this_or_super OP_TK CP_TK error
1508 {
1509 parse_ctor_invocation_error ();
1510 RECOVER;
1511 }
1512 | this_or_super OP_TK argument_list error
1513 {yyerror ("')' expected"); RECOVER;}
1514 | this_or_super OP_TK argument_list CP_TK error
1515 {
1516 parse_ctor_invocation_error ();
1517 RECOVER;
1518 }
1519 | name DOT_TK SUPER_TK error
1520 {yyerror ("'(' expected"); RECOVER;}
1521 | name DOT_TK SUPER_TK OP_TK error
1522 {yyerror ("')' expected"); RECOVER;}
1523 | name DOT_TK SUPER_TK OP_TK argument_list error
1524 {yyerror ("')' expected"); RECOVER;}
1525 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1526 {yyerror ("';' expected"); RECOVER;}
1527 | name DOT_TK SUPER_TK OP_TK CP_TK error
1528 {yyerror ("';' expected"); RECOVER;}
1529 ;
1530
1531 statement_expression:
1532 assignment
1533 | pre_increment_expression
1534 | pre_decrement_expression
1535 | post_increment_expression
1536 | post_decrement_expression
1537 | method_invocation
1538 | class_instance_creation_expression
1539 ;
1540
1541 if_then_statement:
1542 IF_TK OP_TK expression CP_TK statement
1543 {
1544 $$ = build_if_else_statement ($2.location, $3,
1545 $5, NULL_TREE);
1546 }
1547 | IF_TK error
1548 {yyerror ("'(' expected"); RECOVER;}
1549 | IF_TK OP_TK error
1550 {yyerror ("Missing term"); RECOVER;}
1551 | IF_TK OP_TK expression error
1552 {yyerror ("')' expected"); RECOVER;}
1553 ;
1554
1555 if_then_else_statement:
1556 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1557 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1558 ;
1559
1560 if_then_else_statement_nsi:
1561 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1562 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1563 ;
1564
1565 switch_statement:
1566 switch_expression
1567 {
1568 enter_block ();
1569 }
1570 switch_block
1571 {
1572 /* Make into "proper list" of COMPOUND_EXPRs.
1573 I.e. make the last statment also have its own
1574 COMPOUND_EXPR. */
1575 maybe_absorb_scoping_blocks ();
1576 TREE_OPERAND ($1, 1) = exit_block ();
1577 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1578 }
1579 ;
1580
1581 switch_expression:
1582 SWITCH_TK OP_TK expression CP_TK
1583 {
1584 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1585 EXPR_WFL_LINECOL ($$) = $2.location;
1586 }
1587 | SWITCH_TK error
1588 {yyerror ("'(' expected"); RECOVER;}
1589 | SWITCH_TK OP_TK error
1590 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1591 | SWITCH_TK OP_TK expression CP_TK error
1592 {yyerror ("'{' expected"); RECOVER;}
1593 ;
1594
1595 /* Default assignment is there to avoid type node on switch_block
1596 node. */
1597
1598 switch_block:
1599 OCB_TK CCB_TK
1600 { $$ = NULL_TREE; }
1601 | OCB_TK switch_labels CCB_TK
1602 { $$ = NULL_TREE; }
1603 | OCB_TK switch_block_statement_groups CCB_TK
1604 { $$ = NULL_TREE; }
1605 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1606 { $$ = NULL_TREE; }
1607 ;
1608
1609 switch_block_statement_groups:
1610 switch_block_statement_group
1611 | switch_block_statement_groups switch_block_statement_group
1612 ;
1613
1614 switch_block_statement_group:
1615 switch_labels block_statements
1616 ;
1617
1618 switch_labels:
1619 switch_label
1620 | switch_labels switch_label
1621 ;
1622
1623 switch_label:
1624 CASE_TK constant_expression REL_CL_TK
1625 {
1626 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1627 EXPR_WFL_LINECOL (lab) = $1.location;
1628 java_method_add_stmt (current_function_decl, lab);
1629 }
1630 | DEFAULT_TK REL_CL_TK
1631 {
1632 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1633 EXPR_WFL_LINECOL (lab) = $1.location;
1634 java_method_add_stmt (current_function_decl, lab);
1635 }
1636 | CASE_TK error
1637 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1638 | CASE_TK constant_expression error
1639 {yyerror ("':' expected"); RECOVER;}
1640 | DEFAULT_TK error
1641 {yyerror ("':' expected"); RECOVER;}
1642 ;
1643
1644 while_expression:
1645 WHILE_TK OP_TK expression CP_TK
1646 {
1647 tree body = build_loop_body ($2.location, $3, 0);
1648 $$ = build_new_loop (body);
1649 }
1650 ;
1651
1652 while_statement:
1653 while_expression statement
1654 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1655 | WHILE_TK error
1656 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1657 | WHILE_TK OP_TK error
1658 {yyerror ("Missing term and ')' expected"); RECOVER;}
1659 | WHILE_TK OP_TK expression error
1660 {yyerror ("')' expected"); RECOVER;}
1661 ;
1662
1663 while_statement_nsi:
1664 while_expression statement_nsi
1665 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1666 ;
1667
1668 do_statement_begin:
1669 DO_TK
1670 {
1671 tree body = build_loop_body (0, NULL_TREE, 1);
1672 $$ = build_new_loop (body);
1673 }
1674 /* Need error handing here. FIXME */
1675 ;
1676
1677 do_statement:
1678 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1679 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1680 ;
1681
1682 for_statement:
1683 for_begin SC_TK expression SC_TK for_update CP_TK statement
1684 {
1685 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1686 $3 = build_wfl_node ($3);
1687 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1688 }
1689 | for_begin SC_TK SC_TK for_update CP_TK statement
1690 {
1691 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1692 /* We have not condition, so we get rid of the EXIT_EXPR */
1693 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1694 empty_stmt_node;
1695 }
1696 | for_begin SC_TK error
1697 {yyerror ("Invalid control expression"); RECOVER;}
1698 | for_begin SC_TK expression SC_TK error
1699 {yyerror ("Invalid update expression"); RECOVER;}
1700 | for_begin SC_TK SC_TK error
1701 {yyerror ("Invalid update expression"); RECOVER;}
1702 ;
1703
1704 for_statement_nsi:
1705 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1706 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1707 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1708 {
1709 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1710 /* We have not condition, so we get rid of the EXIT_EXPR */
1711 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1712 empty_stmt_node;
1713 }
1714 ;
1715
1716 for_header:
1717 FOR_TK OP_TK
1718 {
1719 /* This scope defined for local variable that may be
1720 defined within the scope of the for loop */
1721 enter_block ();
1722 }
1723 | FOR_TK error
1724 {yyerror ("'(' expected"); DRECOVER(for_1);}
1725 | FOR_TK OP_TK error
1726 {yyerror ("Invalid init statement"); RECOVER;}
1727 ;
1728
1729 for_begin:
1730 for_header for_init
1731 {
1732 /* We now declare the loop body. The loop is
1733 declared as a for loop. */
1734 tree body = build_loop_body (0, NULL_TREE, 0);
1735 $$ = build_new_loop (body);
1736 FOR_LOOP_P ($$) = 1;
1737 /* The loop is added to the current block the for
1738 statement is defined within */
1739 java_method_add_stmt (current_function_decl, $$);
1740 }
1741 ;
1742 for_init: /* Can be empty */
1743 { $$ = empty_stmt_node; }
1744 | statement_expression_list
1745 {
1746 /* Init statement recorded within the previously
1747 defined block scope */
1748 $$ = java_method_add_stmt (current_function_decl, $1);
1749 }
1750 | local_variable_declaration
1751 {
1752 /* Local variable are recorded within the previously
1753 defined block scope */
1754 $$ = NULL_TREE;
1755 }
1756 | statement_expression_list error
1757 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1758 ;
1759
1760 for_update: /* Can be empty */
1761 {$$ = empty_stmt_node;}
1762 | statement_expression_list
1763 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1764 ;
1765
1766 statement_expression_list:
1767 statement_expression
1768 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1769 | statement_expression_list C_TK statement_expression
1770 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1771 | statement_expression_list C_TK error
1772 {yyerror ("Missing term"); RECOVER;}
1773 ;
1774
1775 break_statement:
1776 BREAK_TK SC_TK
1777 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1778 | BREAK_TK identifier SC_TK
1779 { $$ = build_bc_statement ($1.location, 1, $2); }
1780 | BREAK_TK error
1781 {yyerror ("Missing term"); RECOVER;}
1782 | BREAK_TK identifier error
1783 {yyerror ("';' expected"); RECOVER;}
1784 ;
1785
1786 continue_statement:
1787 CONTINUE_TK SC_TK
1788 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1789 | CONTINUE_TK identifier SC_TK
1790 { $$ = build_bc_statement ($1.location, 0, $2); }
1791 | CONTINUE_TK error
1792 {yyerror ("Missing term"); RECOVER;}
1793 | CONTINUE_TK identifier error
1794 {yyerror ("';' expected"); RECOVER;}
1795 ;
1796
1797 return_statement:
1798 RETURN_TK SC_TK
1799 { $$ = build_return ($1.location, NULL_TREE); }
1800 | RETURN_TK expression SC_TK
1801 { $$ = build_return ($1.location, $2); }
1802 | RETURN_TK error
1803 {yyerror ("Missing term"); RECOVER;}
1804 | RETURN_TK expression error
1805 {yyerror ("';' expected"); RECOVER;}
1806 ;
1807
1808 throw_statement:
1809 THROW_TK expression SC_TK
1810 {
1811 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1812 EXPR_WFL_LINECOL ($$) = $1.location;
1813 }
1814 | THROW_TK error
1815 {yyerror ("Missing term"); RECOVER;}
1816 | THROW_TK expression error
1817 {yyerror ("';' expected"); RECOVER;}
1818 ;
1819
1820 synchronized_statement:
1821 synchronized OP_TK expression CP_TK block
1822 {
1823 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1824 EXPR_WFL_LINECOL ($$) =
1825 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1826 }
1827 | synchronized OP_TK expression CP_TK error
1828 {yyerror ("'{' expected"); RECOVER;}
1829 | synchronized error
1830 {yyerror ("'(' expected"); RECOVER;}
1831 | synchronized OP_TK error CP_TK
1832 {yyerror ("Missing term"); RECOVER;}
1833 | synchronized OP_TK error
1834 {yyerror ("Missing term"); RECOVER;}
1835 ;
1836
1837 synchronized:
1838 modifiers
1839 {
1840 check_modifiers (
1841 "Illegal modifier `%s'. Only `synchronized' was expected here",
1842 $1, ACC_SYNCHRONIZED);
1843 if ($1 != ACC_SYNCHRONIZED)
1844 MODIFIER_WFL (SYNCHRONIZED_TK) =
1845 build_wfl_node (NULL_TREE);
1846 }
1847 ;
1848
1849 try_statement:
1850 TRY_TK block catches
1851 { $$ = build_try_statement ($1.location, $2, $3); }
1852 | TRY_TK block finally
1853 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1854 | TRY_TK block catches finally
1855 { $$ = build_try_finally_statement
1856 ($1.location, build_try_statement ($1.location,
1857 $2, $3), $4);
1858 }
1859 | TRY_TK error
1860 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1861 ;
1862
1863 catches:
1864 catch_clause
1865 | catches catch_clause
1866 {
1867 TREE_CHAIN ($2) = $1;
1868 $$ = $2;
1869 }
1870 ;
1871
1872 catch_clause:
1873 catch_clause_parameter block
1874 {
1875 java_method_add_stmt (current_function_decl, $2);
1876 exit_block ();
1877 $$ = $1;
1878 }
1879
1880 catch_clause_parameter:
1881 CATCH_TK OP_TK formal_parameter CP_TK
1882 {
1883 /* We add a block to define a scope for
1884 formal_parameter (CCBP). The formal parameter is
1885 declared initialized by the appropriate function
1886 call */
1887 tree ccpb = enter_block ();
1888 tree init = build_assignment (ASSIGN_TK, $2.location,
1889 TREE_PURPOSE ($3),
1890 soft_exceptioninfo_call_node);
1891 declare_local_variables (0, TREE_VALUE ($3),
1892 build_tree_list (TREE_PURPOSE ($3),
1893 init));
1894 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1895 EXPR_WFL_LINECOL ($$) = $1.location;
1896 }
1897 | CATCH_TK error
1898 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1899 | CATCH_TK OP_TK error
1900 {
1901 yyerror ("Missing term or ')' expected");
1902 RECOVER; $$ = NULL_TREE;
1903 }
1904 | CATCH_TK OP_TK error CP_TK /* That's for () */
1905 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1906 ;
1907
1908 finally:
1909 FINALLY_TK block
1910 { $$ = $2; }
1911 | FINALLY_TK error
1912 {yyerror ("'{' expected"); RECOVER; }
1913 ;
1914
1915 /* 19.12 Production from 15: Expressions */
1916 primary:
1917 primary_no_new_array
1918 | array_creation_expression
1919 ;
1920
1921 primary_no_new_array:
1922 literal
1923 | THIS_TK
1924 { $$ = build_this ($1.location); }
1925 | OP_TK expression CP_TK
1926 {$$ = $2;}
1927 | class_instance_creation_expression
1928 | field_access
1929 | method_invocation
1930 | array_access
1931 | type_literals
1932 /* Added, JDK1.1 inner classes. Documentation is wrong
1933 refering to a 'ClassName' (class_name) rule that doesn't
1934 exist. Used name: instead. */
1935 | name DOT_TK THIS_TK
1936 {
1937 tree wfl = build_wfl_node (this_identifier_node);
1938 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1939 }
1940 | OP_TK expression error
1941 {yyerror ("')' expected"); RECOVER;}
1942 | name DOT_TK error
1943 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1944 | primitive_type DOT_TK error
1945 {yyerror ("'class' expected" ); RECOVER;}
1946 | VOID_TK DOT_TK error
1947 {yyerror ("'class' expected" ); RECOVER;}
1948 ;
1949
1950 type_literals:
1951 name DOT_TK CLASS_TK
1952 { $$ = build_incomplete_class_ref ($2.location, $1); }
1953 | array_type DOT_TK CLASS_TK
1954 { $$ = build_incomplete_class_ref ($2.location, $1); }
1955 | primitive_type DOT_TK CLASS_TK
1956 { $$ = build_class_ref ($1); }
1957 | VOID_TK DOT_TK CLASS_TK
1958 { $$ = build_class_ref (void_type_node); }
1959 ;
1960
1961 class_instance_creation_expression:
1962 NEW_TK class_type OP_TK argument_list CP_TK
1963 { $$ = build_new_invocation ($2, $4); }
1964 | NEW_TK class_type OP_TK CP_TK
1965 { $$ = build_new_invocation ($2, NULL_TREE); }
1966 | anonymous_class_creation
1967 /* Added, JDK1.1 inner classes, modified to use name or
1968 primary instead of primary solely which couldn't work in
1969 all situations. */
1970 | something_dot_new identifier OP_TK CP_TK
1971 {
1972 tree ctor = build_new_invocation ($2, NULL_TREE);
1973 $$ = make_qualified_primary ($1, ctor,
1974 EXPR_WFL_LINECOL ($1));
1975 }
1976 | something_dot_new identifier OP_TK CP_TK class_body
1977 | something_dot_new identifier OP_TK argument_list CP_TK
1978 {
1979 tree ctor = build_new_invocation ($2, $4);
1980 $$ = make_qualified_primary ($1, ctor,
1981 EXPR_WFL_LINECOL ($1));
1982 }
1983 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1984 | NEW_TK error SC_TK
1985 {yyerror ("'(' expected"); DRECOVER(new_1);}
1986 | NEW_TK class_type error
1987 {yyerror ("'(' expected"); RECOVER;}
1988 | NEW_TK class_type OP_TK error
1989 {yyerror ("')' or term expected"); RECOVER;}
1990 | NEW_TK class_type OP_TK argument_list error
1991 {yyerror ("')' expected"); RECOVER;}
1992 | something_dot_new error
1993 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1994 | something_dot_new identifier error
1995 {yyerror ("'(' expected"); RECOVER;}
1996 ;
1997
1998 /* Created after JDK1.1 rules originally added to
1999 class_instance_creation_expression, but modified to use
2000 'class_type' instead of 'TypeName' (type_name) which is mentionned
2001 in the documentation but doesn't exist. */
2002
2003 anonymous_class_creation:
2004 NEW_TK class_type OP_TK argument_list CP_TK
2005 { create_anonymous_class ($1.location, $2); }
2006 class_body
2007 {
2008 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2009 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2010
2011 end_class_declaration (1);
2012
2013 /* Now we can craft the new expression */
2014 $$ = build_new_invocation (id, $4);
2015
2016 /* Note that we can't possibly be here if
2017 `class_type' is an interface (in which case the
2018 anonymous class extends Object and implements
2019 `class_type', hence its constructor can't have
2020 arguments.) */
2021
2022 /* Otherwise, the innerclass must feature a
2023 constructor matching `argument_list'. Anonymous
2024 classes are a bit special: it's impossible to
2025 define constructor for them, hence constructors
2026 must be generated following the hints provided by
2027 the `new' expression. Whether a super constructor
2028 of that nature exists or not is to be verified
2029 later on in verify_constructor_super.
2030
2031 It's during the expansion of a `new' statement
2032 refering to an anonymous class that a ctor will
2033 be generated for the anonymous class, with the
2034 right arguments. */
2035
2036 }
2037 | NEW_TK class_type OP_TK CP_TK
2038 { create_anonymous_class ($1.location, $2); }
2039 class_body
2040 {
2041 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2042 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2043
2044 end_class_declaration (1);
2045
2046 /* Now we can craft the new expression. The
2047 statement doesn't need to be remember so that a
2048 constructor can be generated, since its signature
2049 is already known. */
2050 $$ = build_new_invocation (id, NULL_TREE);
2051 }
2052 ;
2053
2054 something_dot_new: /* Added, not part of the specs. */
2055 name DOT_TK NEW_TK
2056 { $$ = $1; }
2057 | primary DOT_TK NEW_TK
2058 { $$ = $1; }
2059 ;
2060
2061 argument_list:
2062 expression
2063 {
2064 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2065 ctxp->formal_parameter_number = 1;
2066 }
2067 | argument_list C_TK expression
2068 {
2069 ctxp->formal_parameter_number += 1;
2070 $$ = tree_cons (NULL_TREE, $3, $1);
2071 }
2072 | argument_list C_TK error
2073 {yyerror ("Missing term"); RECOVER;}
2074 ;
2075
2076 array_creation_expression:
2077 NEW_TK primitive_type dim_exprs
2078 { $$ = build_newarray_node ($2, $3, 0); }
2079 | NEW_TK class_or_interface_type dim_exprs
2080 { $$ = build_newarray_node ($2, $3, 0); }
2081 | NEW_TK primitive_type dim_exprs dims
2082 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2083 | NEW_TK class_or_interface_type dim_exprs dims
2084 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2085 /* Added, JDK1.1 anonymous array. Initial documentation rule
2086 modified */
2087 | NEW_TK class_or_interface_type dims array_initializer
2088 {
2089 char *sig;
2090 int osb = pop_current_osb (ctxp);
2091 while (osb--)
2092 obstack_1grow (&temporary_obstack, '[');
2093 sig = obstack_finish (&temporary_obstack);
2094 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2095 $2, get_identifier (sig), $4);
2096 }
2097 | NEW_TK primitive_type dims array_initializer
2098 {
2099 int osb = pop_current_osb (ctxp);
2100 tree type = $2;
2101 while (osb--)
2102 type = build_java_array_type (type, -1);
2103 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2104 build_pointer_type (type), NULL_TREE, $4);
2105 }
2106 | NEW_TK error CSB_TK
2107 {yyerror ("'[' expected"); DRECOVER ("]");}
2108 | NEW_TK error OSB_TK
2109 {yyerror ("']' expected"); RECOVER;}
2110 ;
2111
2112 dim_exprs:
2113 dim_expr
2114 { $$ = build_tree_list (NULL_TREE, $1); }
2115 | dim_exprs dim_expr
2116 { $$ = tree_cons (NULL_TREE, $2, $$); }
2117 ;
2118
2119 dim_expr:
2120 OSB_TK expression CSB_TK
2121 {
2122 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2123 {
2124 $2 = build_wfl_node ($2);
2125 TREE_TYPE ($2) = NULL_TREE;
2126 }
2127 EXPR_WFL_LINECOL ($2) = $1.location;
2128 $$ = $2;
2129 }
2130 | OSB_TK expression error
2131 {yyerror ("']' expected"); RECOVER;}
2132 | OSB_TK error
2133 {
2134 yyerror ("Missing term");
2135 yyerror ("']' expected");
2136 RECOVER;
2137 }
2138 ;
2139
2140 dims:
2141 OSB_TK CSB_TK
2142 {
2143 int allocate = 0;
2144 /* If not initialized, allocate memory for the osb
2145 numbers stack */
2146 if (!ctxp->osb_limit)
2147 {
2148 allocate = ctxp->osb_limit = 32;
2149 ctxp->osb_depth = -1;
2150 }
2151 /* If capacity overflown, reallocate a bigger chunk */
2152 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2153 allocate = ctxp->osb_limit << 1;
2154
2155 if (allocate)
2156 {
2157 allocate *= sizeof (int);
2158 if (ctxp->osb_number)
2159 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2160 allocate);
2161 else
2162 ctxp->osb_number = (int *)xmalloc (allocate);
2163 }
2164 ctxp->osb_depth++;
2165 CURRENT_OSB (ctxp) = 1;
2166 }
2167 | dims OSB_TK CSB_TK
2168 { CURRENT_OSB (ctxp)++; }
2169 | dims OSB_TK error
2170 { yyerror ("']' expected"); RECOVER;}
2171 ;
2172
2173 field_access:
2174 primary DOT_TK identifier
2175 { $$ = make_qualified_primary ($1, $3, $2.location); }
2176 /* FIXME - REWRITE TO:
2177 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2178 | SUPER_TK DOT_TK identifier
2179 {
2180 tree super_wfl = build_wfl_node (super_identifier_node);
2181 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2182 $$ = make_qualified_name (super_wfl, $3, $2.location);
2183 }
2184 | SUPER_TK error
2185 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2186 ;
2187
2188 method_invocation:
2189 name OP_TK CP_TK
2190 { $$ = build_method_invocation ($1, NULL_TREE); }
2191 | name OP_TK argument_list CP_TK
2192 { $$ = build_method_invocation ($1, $3); }
2193 | primary DOT_TK identifier OP_TK CP_TK
2194 {
2195 if (TREE_CODE ($1) == THIS_EXPR)
2196 $$ = build_this_super_qualified_invocation
2197 (1, $3, NULL_TREE, 0, $2.location);
2198 else
2199 {
2200 tree invok = build_method_invocation ($3, NULL_TREE);
2201 $$ = make_qualified_primary ($1, invok, $2.location);
2202 }
2203 }
2204 | primary DOT_TK identifier OP_TK argument_list CP_TK
2205 {
2206 if (TREE_CODE ($1) == THIS_EXPR)
2207 $$ = build_this_super_qualified_invocation
2208 (1, $3, $5, 0, $2.location);
2209 else
2210 {
2211 tree invok = build_method_invocation ($3, $5);
2212 $$ = make_qualified_primary ($1, invok, $2.location);
2213 }
2214 }
2215 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2216 {
2217 $$ = build_this_super_qualified_invocation
2218 (0, $3, NULL_TREE, $1.location, $2.location);
2219 }
2220 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2221 {
2222 $$ = build_this_super_qualified_invocation
2223 (0, $3, $5, $1.location, $2.location);
2224 }
2225 /* Screws up thing. I let it here until I'm convinced it can
2226 be removed. FIXME
2227 | primary DOT_TK error
2228 {yyerror ("'(' expected"); DRECOVER(bad);} */
2229 | SUPER_TK DOT_TK error CP_TK
2230 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2231 | SUPER_TK DOT_TK error DOT_TK
2232 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2233 ;
2234
2235 array_access:
2236 name OSB_TK expression CSB_TK
2237 { $$ = build_array_ref ($2.location, $1, $3); }
2238 | primary_no_new_array OSB_TK expression CSB_TK
2239 { $$ = build_array_ref ($2.location, $1, $3); }
2240 | name OSB_TK error
2241 {
2242 yyerror ("Missing term and ']' expected");
2243 DRECOVER(array_access);
2244 }
2245 | name OSB_TK expression error
2246 {
2247 yyerror ("']' expected");
2248 DRECOVER(array_access);
2249 }
2250 | primary_no_new_array OSB_TK error
2251 {
2252 yyerror ("Missing term and ']' expected");
2253 DRECOVER(array_access);
2254 }
2255 | primary_no_new_array OSB_TK expression error
2256 {
2257 yyerror ("']' expected");
2258 DRECOVER(array_access);
2259 }
2260 ;
2261
2262 postfix_expression:
2263 primary
2264 | name
2265 | post_increment_expression
2266 | post_decrement_expression
2267 ;
2268
2269 post_increment_expression:
2270 postfix_expression INCR_TK
2271 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2272 ;
2273
2274 post_decrement_expression:
2275 postfix_expression DECR_TK
2276 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2277 ;
2278
2279 unary_expression:
2280 pre_increment_expression
2281 | pre_decrement_expression
2282 | PLUS_TK unary_expression
2283 {$$ = build_unaryop ($1.token, $1.location, $2); }
2284 | MINUS_TK unary_expression
2285 {$$ = build_unaryop ($1.token, $1.location, $2); }
2286 | unary_expression_not_plus_minus
2287 | PLUS_TK error
2288 {yyerror ("Missing term"); RECOVER}
2289 | MINUS_TK error
2290 {yyerror ("Missing term"); RECOVER}
2291 ;
2292
2293 pre_increment_expression:
2294 INCR_TK unary_expression
2295 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2296 | INCR_TK error
2297 {yyerror ("Missing term"); RECOVER}
2298 ;
2299
2300 pre_decrement_expression:
2301 DECR_TK unary_expression
2302 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2303 | DECR_TK error
2304 {yyerror ("Missing term"); RECOVER}
2305 ;
2306
2307 unary_expression_not_plus_minus:
2308 postfix_expression
2309 | NOT_TK unary_expression
2310 {$$ = build_unaryop ($1.token, $1.location, $2); }
2311 | NEG_TK unary_expression
2312 {$$ = build_unaryop ($1.token, $1.location, $2); }
2313 | cast_expression
2314 | NOT_TK error
2315 {yyerror ("Missing term"); RECOVER}
2316 | NEG_TK error
2317 {yyerror ("Missing term"); RECOVER}
2318 ;
2319
2320 cast_expression: /* Error handling here is potentially weak */
2321 OP_TK primitive_type dims CP_TK unary_expression
2322 {
2323 tree type = $2;
2324 int osb = pop_current_osb (ctxp);
2325 while (osb--)
2326 type = build_java_array_type (type, -1);
2327 $$ = build_cast ($1.location, type, $5);
2328 }
2329 | OP_TK primitive_type CP_TK unary_expression
2330 { $$ = build_cast ($1.location, $2, $4); }
2331 | OP_TK expression CP_TK unary_expression_not_plus_minus
2332 { $$ = build_cast ($1.location, $2, $4); }
2333 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2334 {
2335 const char *ptr;
2336 int osb = pop_current_osb (ctxp);
2337 while (osb--)
2338 obstack_1grow (&temporary_obstack, '[');
2339 obstack_grow0 (&temporary_obstack,
2340 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2341 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2342 ptr = obstack_finish (&temporary_obstack);
2343 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2344 $$ = build_cast ($1.location, $2, $5);
2345 }
2346 | OP_TK primitive_type OSB_TK error
2347 {yyerror ("']' expected, invalid type expression");}
2348 | OP_TK error
2349 {
2350 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2351 RECOVER;
2352 }
2353 | OP_TK primitive_type dims CP_TK error
2354 {yyerror ("Missing term"); RECOVER;}
2355 | OP_TK primitive_type CP_TK error
2356 {yyerror ("Missing term"); RECOVER;}
2357 | OP_TK name dims CP_TK error
2358 {yyerror ("Missing term"); RECOVER;}
2359 ;
2360
2361 multiplicative_expression:
2362 unary_expression
2363 | multiplicative_expression MULT_TK unary_expression
2364 {
2365 $$ = build_binop (BINOP_LOOKUP ($2.token),
2366 $2.location, $1, $3);
2367 }
2368 | multiplicative_expression DIV_TK unary_expression
2369 {
2370 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2371 $1, $3);
2372 }
2373 | multiplicative_expression REM_TK unary_expression
2374 {
2375 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2376 $1, $3);
2377 }
2378 | multiplicative_expression MULT_TK error
2379 {yyerror ("Missing term"); RECOVER;}
2380 | multiplicative_expression DIV_TK error
2381 {yyerror ("Missing term"); RECOVER;}
2382 | multiplicative_expression REM_TK error
2383 {yyerror ("Missing term"); RECOVER;}
2384 ;
2385
2386 additive_expression:
2387 multiplicative_expression
2388 | additive_expression PLUS_TK multiplicative_expression
2389 {
2390 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2391 $1, $3);
2392 }
2393 | additive_expression MINUS_TK multiplicative_expression
2394 {
2395 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2396 $1, $3);
2397 }
2398 | additive_expression PLUS_TK error
2399 {yyerror ("Missing term"); RECOVER;}
2400 | additive_expression MINUS_TK error
2401 {yyerror ("Missing term"); RECOVER;}
2402 ;
2403
2404 shift_expression:
2405 additive_expression
2406 | shift_expression LS_TK additive_expression
2407 {
2408 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2409 $1, $3);
2410 }
2411 | shift_expression SRS_TK additive_expression
2412 {
2413 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2414 $1, $3);
2415 }
2416 | shift_expression ZRS_TK additive_expression
2417 {
2418 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2419 $1, $3);
2420 }
2421 | shift_expression LS_TK error
2422 {yyerror ("Missing term"); RECOVER;}
2423 | shift_expression SRS_TK error
2424 {yyerror ("Missing term"); RECOVER;}
2425 | shift_expression ZRS_TK error
2426 {yyerror ("Missing term"); RECOVER;}
2427 ;
2428
2429 relational_expression:
2430 shift_expression
2431 | relational_expression LT_TK shift_expression
2432 {
2433 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2434 $1, $3);
2435 }
2436 | relational_expression GT_TK shift_expression
2437 {
2438 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2439 $1, $3);
2440 }
2441 | relational_expression LTE_TK shift_expression
2442 {
2443 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2444 $1, $3);
2445 }
2446 | relational_expression GTE_TK shift_expression
2447 {
2448 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2449 $1, $3);
2450 }
2451 | relational_expression INSTANCEOF_TK reference_type
2452 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2453 | relational_expression LT_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2455 | relational_expression GT_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457 | relational_expression LTE_TK error
2458 {yyerror ("Missing term"); RECOVER;}
2459 | relational_expression GTE_TK error
2460 {yyerror ("Missing term"); RECOVER;}
2461 | relational_expression INSTANCEOF_TK error
2462 {yyerror ("Invalid reference type"); RECOVER;}
2463 ;
2464
2465 equality_expression:
2466 relational_expression
2467 | equality_expression EQ_TK relational_expression
2468 {
2469 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2470 $1, $3);
2471 }
2472 | equality_expression NEQ_TK relational_expression
2473 {
2474 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2475 $1, $3);
2476 }
2477 | equality_expression EQ_TK error
2478 {yyerror ("Missing term"); RECOVER;}
2479 | equality_expression NEQ_TK error
2480 {yyerror ("Missing term"); RECOVER;}
2481 ;
2482
2483 and_expression:
2484 equality_expression
2485 | and_expression AND_TK equality_expression
2486 {
2487 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2488 $1, $3);
2489 }
2490 | and_expression AND_TK error
2491 {yyerror ("Missing term"); RECOVER;}
2492 ;
2493
2494 exclusive_or_expression:
2495 and_expression
2496 | exclusive_or_expression XOR_TK and_expression
2497 {
2498 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2499 $1, $3);
2500 }
2501 | exclusive_or_expression XOR_TK error
2502 {yyerror ("Missing term"); RECOVER;}
2503 ;
2504
2505 inclusive_or_expression:
2506 exclusive_or_expression
2507 | inclusive_or_expression OR_TK exclusive_or_expression
2508 {
2509 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2510 $1, $3);
2511 }
2512 | inclusive_or_expression OR_TK error
2513 {yyerror ("Missing term"); RECOVER;}
2514 ;
2515
2516 conditional_and_expression:
2517 inclusive_or_expression
2518 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2519 {
2520 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2521 $1, $3);
2522 }
2523 | conditional_and_expression BOOL_AND_TK error
2524 {yyerror ("Missing term"); RECOVER;}
2525 ;
2526
2527 conditional_or_expression:
2528 conditional_and_expression
2529 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2530 {
2531 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2532 $1, $3);
2533 }
2534 | conditional_or_expression BOOL_OR_TK error
2535 {yyerror ("Missing term"); RECOVER;}
2536 ;
2537
2538 conditional_expression: /* Error handling here is weak */
2539 conditional_or_expression
2540 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2541 {
2542 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2543 EXPR_WFL_LINECOL ($$) = $2.location;
2544 }
2545 | conditional_or_expression REL_QM_TK REL_CL_TK error
2546 {
2547 YYERROR_NOW;
2548 yyerror ("Missing term");
2549 DRECOVER (1);
2550 }
2551 | conditional_or_expression REL_QM_TK error
2552 {yyerror ("Missing term"); DRECOVER (2);}
2553 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2554 {yyerror ("Missing term"); DRECOVER (3);}
2555 ;
2556
2557 assignment_expression:
2558 conditional_expression
2559 | assignment
2560 ;
2561
2562 assignment:
2563 left_hand_side assignment_operator assignment_expression
2564 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2565 | left_hand_side assignment_operator error
2566 {
2567 YYNOT_TWICE yyerror ("Missing term");
2568 DRECOVER (assign);
2569 }
2570 ;
2571
2572 left_hand_side:
2573 name
2574 | field_access
2575 | array_access
2576 ;
2577
2578 assignment_operator:
2579 ASSIGN_ANY_TK
2580 | ASSIGN_TK
2581 ;
2582
2583 expression:
2584 assignment_expression
2585 ;
2586
2587 constant_expression:
2588 expression
2589 ;
2590
2591 %%
2592
2593 /* Helper function to retrieve an OSB count. Should be used when the
2594 `dims:' rule is being used. */
2595
2596 static int
2597 pop_current_osb (ctxp)
2598 struct parser_ctxt *ctxp;
2599 {
2600 int to_return;
2601
2602 if (ctxp->osb_depth < 0)
2603 abort ();
2604
2605 to_return = CURRENT_OSB (ctxp);
2606 ctxp->osb_depth--;
2607
2608 return to_return;
2609 }
2610
2611 \f
2612
2613 /* This section of the code deal with save/restoring parser contexts.
2614 Add mode documentation here. FIXME */
2615
2616 /* Helper function. Create a new parser context. With
2617 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2618 context is copied, otherwise, the new context is zeroed. The newly
2619 created context becomes the current one. */
2620
2621 static void
2622 create_new_parser_context (copy_from_previous)
2623 int copy_from_previous;
2624 {
2625 struct parser_ctxt *new;
2626
2627 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2628 if (copy_from_previous)
2629 {
2630 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2631 new->saved_data_ctx = 1;
2632 }
2633 else
2634 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2635
2636 new->next = ctxp;
2637 ctxp = new;
2638 }
2639
2640 /* Create a new parser context and make it the current one. */
2641
2642 void
2643 java_push_parser_context ()
2644 {
2645 create_new_parser_context (0);
2646 }
2647
2648 void
2649 java_pop_parser_context (generate)
2650 int generate;
2651 {
2652 tree current;
2653 struct parser_ctxt *toFree, *next;
2654
2655 if (!ctxp)
2656 return;
2657
2658 toFree = ctxp;
2659 next = ctxp->next;
2660 if (next)
2661 {
2662 lineno = ctxp->lineno;
2663 current_class = ctxp->class_type;
2664 }
2665
2666 /* If the old and new lexers differ, then free the old one. */
2667 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2668 java_destroy_lexer (ctxp->lexer);
2669
2670 /* Set the single import class file flag to 0 for the current list
2671 of imported things */
2672 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2673 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2674
2675 /* And restore those of the previous context */
2676 if ((ctxp = next)) /* Assignment is really meant here */
2677 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2678 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2679
2680 /* If we pushed a context to parse a class intended to be generated,
2681 we keep it so we can remember the class. What we could actually
2682 do is to just update a list of class names. */
2683 if (generate)
2684 {
2685 toFree->next = ctxp_for_generation;
2686 ctxp_for_generation = toFree;
2687 }
2688 else
2689 free (toFree);
2690 }
2691
2692 /* Create a parser context for the use of saving some global
2693 variables. */
2694
2695 void
2696 java_parser_context_save_global ()
2697 {
2698 if (!ctxp)
2699 {
2700 java_push_parser_context ();
2701 ctxp->saved_data_ctx = 1;
2702 }
2703
2704 /* If this context already stores data, create a new one suitable
2705 for data storage. */
2706 else if (ctxp->saved_data)
2707 create_new_parser_context (1);
2708
2709 ctxp->lineno = lineno;
2710 ctxp->class_type = current_class;
2711 ctxp->filename = input_filename;
2712 ctxp->function_decl = current_function_decl;
2713 ctxp->saved_data = 1;
2714 }
2715
2716 /* Restore some global variables from the previous context. Make the
2717 previous context the current one. */
2718
2719 void
2720 java_parser_context_restore_global ()
2721 {
2722 lineno = ctxp->lineno;
2723 current_class = ctxp->class_type;
2724 input_filename = ctxp->filename;
2725 if (wfl_operator)
2726 {
2727 tree s;
2728 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2729 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2730 }
2731 current_function_decl = ctxp->function_decl;
2732 ctxp->saved_data = 0;
2733 if (ctxp->saved_data_ctx)
2734 java_pop_parser_context (0);
2735 }
2736
2737 /* Suspend vital data for the current class/function being parsed so
2738 that an other class can be parsed. Used to let local/anonymous
2739 classes be parsed. */
2740
2741 static void
2742 java_parser_context_suspend ()
2743 {
2744 /* This makes debugging through java_debug_context easier */
2745 static const char *name = "<inner buffer context>";
2746
2747 /* Duplicate the previous context, use it to save the globals we're
2748 interested in */
2749 create_new_parser_context (1);
2750 ctxp->function_decl = current_function_decl;
2751 ctxp->class_type = current_class;
2752
2753 /* Then create a new context which inherits all data from the
2754 previous one. This will be the new current context */
2755 create_new_parser_context (1);
2756
2757 /* Help debugging */
2758 ctxp->next->filename = name;
2759 }
2760
2761 /* Resume vital data for the current class/function being parsed so
2762 that an other class can be parsed. Used to let local/anonymous
2763 classes be parsed. The trick is the data storing file position
2764 informations must be restored to their current value, so parsing
2765 can resume as if no context was ever saved. */
2766
2767 static void
2768 java_parser_context_resume ()
2769 {
2770 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2771 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2772 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2773
2774 /* We need to inherit the list of classes to complete/generate */
2775 restored->classd_list = old->classd_list;
2776 restored->class_list = old->class_list;
2777
2778 /* Restore the current class and function from the saver */
2779 current_class = saver->class_type;
2780 current_function_decl = saver->function_decl;
2781
2782 /* Retrive the restored context */
2783 ctxp = restored;
2784
2785 /* Re-installed the data for the parsing to carry on */
2786 bcopy (&old->marker_begining, &ctxp->marker_begining,
2787 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2788
2789 /* Buffer context can now be discarded */
2790 free (saver);
2791 free (old);
2792 }
2793
2794 /* Add a new anchor node to which all statement(s) initializing static
2795 and non static initialized upon declaration field(s) will be
2796 linked. */
2797
2798 static void
2799 java_parser_context_push_initialized_field ()
2800 {
2801 tree node;
2802
2803 node = build_tree_list (NULL_TREE, NULL_TREE);
2804 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2805 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2806
2807 node = build_tree_list (NULL_TREE, NULL_TREE);
2808 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2809 CPC_INITIALIZER_LIST (ctxp) = node;
2810
2811 node = build_tree_list (NULL_TREE, NULL_TREE);
2812 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2813 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2814 }
2815
2816 /* Pop the lists of initialized field. If this lists aren't empty,
2817 remember them so we can use it to create and populate the finit$
2818 or <clinit> functions. */
2819
2820 static void
2821 java_parser_context_pop_initialized_field ()
2822 {
2823 tree stmts;
2824 tree class_type = TREE_TYPE (GET_CPC ());
2825
2826 if (CPC_INITIALIZER_LIST (ctxp))
2827 {
2828 stmts = CPC_INITIALIZER_STMT (ctxp);
2829 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2830 if (stmts && !java_error_count)
2831 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2832 }
2833
2834 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2835 {
2836 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2837 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2838 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2839 /* Keep initialization in order to enforce 8.5 */
2840 if (stmts && !java_error_count)
2841 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2842 }
2843
2844 /* JDK 1.1 instance initializers */
2845 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2846 {
2847 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2848 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2849 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2850 if (stmts && !java_error_count)
2851 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2852 }
2853 }
2854
2855 static tree
2856 reorder_static_initialized (list)
2857 tree list;
2858 {
2859 /* We have to keep things in order. The alias initializer have to
2860 come first, then the initialized regular field, in reverse to
2861 keep them in lexical order. */
2862 tree marker, previous = NULL_TREE;
2863 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2864 if (TREE_CODE (marker) == TREE_LIST
2865 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2866 break;
2867
2868 /* No static initialized, the list is fine as is */
2869 if (!previous)
2870 list = TREE_CHAIN (marker);
2871
2872 /* No marker? reverse the whole list */
2873 else if (!marker)
2874 list = nreverse (list);
2875
2876 /* Otherwise, reverse what's after the marker and the new reordered
2877 sublist will replace the marker. */
2878 else
2879 {
2880 TREE_CHAIN (previous) = NULL_TREE;
2881 list = nreverse (list);
2882 list = chainon (TREE_CHAIN (marker), list);
2883 }
2884 return list;
2885 }
2886
2887 /* Helper functions to dump the parser context stack. */
2888
2889 #define TAB_CONTEXT(C) \
2890 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2891
2892 static void
2893 java_debug_context_do (tab)
2894 int tab;
2895 {
2896 struct parser_ctxt *copy = ctxp;
2897 while (copy)
2898 {
2899 TAB_CONTEXT (tab);
2900 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2901 TAB_CONTEXT (tab);
2902 fprintf (stderr, "filename: %s\n", copy->filename);
2903 TAB_CONTEXT (tab);
2904 fprintf (stderr, "lineno: %d\n", copy->lineno);
2905 TAB_CONTEXT (tab);
2906 fprintf (stderr, "package: %s\n",
2907 (copy->package ?
2908 IDENTIFIER_POINTER (copy->package) : "<none>"));
2909 TAB_CONTEXT (tab);
2910 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2911 TAB_CONTEXT (tab);
2912 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2913 copy = copy->next;
2914 tab += 2;
2915 }
2916 }
2917
2918 /* Dump the stacked up parser contexts. Intended to be called from a
2919 debugger. */
2920
2921 void
2922 java_debug_context ()
2923 {
2924 java_debug_context_do (0);
2925 }
2926
2927 \f
2928
2929 /* Flag for the error report routine to issue the error the first time
2930 it's called (overriding the default behavior which is to drop the
2931 first invocation and honor the second one, taking advantage of a
2932 richer context. */
2933 static int force_error = 0;
2934
2935 /* Reporting an constructor invocation error. */
2936 static void
2937 parse_ctor_invocation_error ()
2938 {
2939 if (DECL_CONSTRUCTOR_P (current_function_decl))
2940 yyerror ("Constructor invocation must be first thing in a constructor");
2941 else
2942 yyerror ("Only constructors can invoke constructors");
2943 }
2944
2945 /* Reporting JDK1.1 features not implemented. */
2946
2947 static tree
2948 parse_jdk1_1_error (msg)
2949 const char *msg;
2950 {
2951 sorry (": `%s' JDK1.1(TM) feature", msg);
2952 java_error_count++;
2953 return empty_stmt_node;
2954 }
2955
2956 static int do_warning = 0;
2957
2958 void
2959 yyerror (msg)
2960 const char *msg;
2961 {
2962 static java_lc elc;
2963 static int prev_lineno;
2964 static const char *prev_msg;
2965
2966 int save_lineno;
2967 char *remainder, *code_from_source;
2968
2969 if (!force_error && prev_lineno == lineno)
2970 return;
2971
2972 /* Save current error location but report latter, when the context is
2973 richer. */
2974 if (ctxp->java_error_flag == 0)
2975 {
2976 ctxp->java_error_flag = 1;
2977 elc = ctxp->elc;
2978 /* Do something to use the previous line if we're reaching the
2979 end of the file... */
2980 #ifdef VERBOSE_SKELETON
2981 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2982 #endif
2983 return;
2984 }
2985
2986 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2987 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2988 return;
2989
2990 ctxp->java_error_flag = 0;
2991 if (do_warning)
2992 java_warning_count++;
2993 else
2994 java_error_count++;
2995
2996 if (elc.col == 0 && msg && msg[1] == ';')
2997 {
2998 elc.col = ctxp->p_line->char_col-1;
2999 elc.line = ctxp->p_line->lineno;
3000 }
3001
3002 save_lineno = lineno;
3003 prev_lineno = lineno = elc.line;
3004 prev_msg = msg;
3005
3006 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3007 obstack_grow0 (&temporary_obstack,
3008 code_from_source, strlen (code_from_source));
3009 remainder = obstack_finish (&temporary_obstack);
3010 if (do_warning)
3011 warning ("%s.\n%s", msg, remainder);
3012 else
3013 error ("%s.\n%s", msg, remainder);
3014
3015 /* This allow us to cheaply avoid an extra 'Invalid expression
3016 statement' error report when errors have been already reported on
3017 the same line. This occurs when we report an error but don't have
3018 a synchronization point other than ';', which
3019 expression_statement is the only one to take care of. */
3020 ctxp->prevent_ese = lineno = save_lineno;
3021 }
3022
3023 static void
3024 issue_warning_error_from_context (cl, msg, ap)
3025 tree cl;
3026 const char *msg;
3027 va_list ap;
3028 {
3029 const char *saved, *saved_input_filename;
3030 char buffer [4096];
3031 vsprintf (buffer, msg, ap);
3032 force_error = 1;
3033
3034 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3035 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3036 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3037
3038 /* We have a CL, that's a good reason for using it if it contains data */
3039 saved = ctxp->filename;
3040 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3041 ctxp->filename = EXPR_WFL_FILENAME (cl);
3042 saved_input_filename = input_filename;
3043 input_filename = ctxp->filename;
3044 java_error (NULL);
3045 java_error (buffer);
3046 ctxp->filename = saved;
3047 input_filename = saved_input_filename;
3048 force_error = 0;
3049 }
3050
3051 /* Issue an error message at a current source line CL */
3052
3053 void
3054 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3055 {
3056 #ifndef ANSI_PROTOTYPES
3057 tree cl;
3058 const char *msg;
3059 #endif
3060 va_list ap;
3061
3062 VA_START (ap, msg);
3063 #ifndef ANSI_PROTOTYPES
3064 cl = va_arg (ap, tree);
3065 msg = va_arg (ap, const char *);
3066 #endif
3067 issue_warning_error_from_context (cl, msg, ap);
3068 va_end (ap);
3069 }
3070
3071 /* Issue a warning at a current source line CL */
3072
3073 static void
3074 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3075 {
3076 #ifndef ANSI_PROTOTYPES
3077 tree cl;
3078 const char *msg;
3079 #endif
3080 va_list ap;
3081
3082 VA_START (ap, msg);
3083 #ifndef ANSI_PROTOTYPES
3084 cl = va_arg (ap, tree);
3085 msg = va_arg (ap, const char *);
3086 #endif
3087
3088 force_error = do_warning = 1;
3089 issue_warning_error_from_context (cl, msg, ap);
3090 do_warning = force_error = 0;
3091 va_end (ap);
3092 }
3093
3094 static tree
3095 find_expr_with_wfl (node)
3096 tree node;
3097 {
3098 while (node)
3099 {
3100 char code;
3101 tree to_return;
3102
3103 switch (TREE_CODE (node))
3104 {
3105 case BLOCK:
3106 node = BLOCK_EXPR_BODY (node);
3107 continue;
3108
3109 case COMPOUND_EXPR:
3110 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3111 if (to_return)
3112 return to_return;
3113 node = TREE_OPERAND (node, 1);
3114 continue;
3115
3116 case LOOP_EXPR:
3117 node = TREE_OPERAND (node, 0);
3118 continue;
3119
3120 case LABELED_BLOCK_EXPR:
3121 node = TREE_OPERAND (node, 1);
3122 continue;
3123
3124 default:
3125 code = TREE_CODE_CLASS (TREE_CODE (node));
3126 if (((code == '1') || (code == '2') || (code == 'e'))
3127 && EXPR_WFL_LINECOL (node))
3128 return node;
3129 return NULL_TREE;
3130 }
3131 }
3132 return NULL_TREE;
3133 }
3134
3135 /* Issue a missing return statement error. Uses METHOD to figure the
3136 last line of the method the error occurs in. */
3137
3138 static void
3139 missing_return_error (method)
3140 tree method;
3141 {
3142 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3143 parse_error_context (wfl_operator, "Missing return statement");
3144 }
3145
3146 /* Issue an unreachable statement error. From NODE, find the next
3147 statement to report appropriately. */
3148 static void
3149 unreachable_stmt_error (node)
3150 tree node;
3151 {
3152 /* Browse node to find the next expression node that has a WFL. Use
3153 the location to report the error */
3154 if (TREE_CODE (node) == COMPOUND_EXPR)
3155 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3156 else
3157 node = find_expr_with_wfl (node);
3158
3159 if (node)
3160 {
3161 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3162 parse_error_context (wfl_operator, "Unreachable statement");
3163 }
3164 else
3165 abort ();
3166 }
3167
3168 int
3169 java_report_errors ()
3170 {
3171 if (java_error_count)
3172 fprintf (stderr, "%d error%s",
3173 java_error_count, (java_error_count == 1 ? "" : "s"));
3174 if (java_warning_count)
3175 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3176 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3177 if (java_error_count || java_warning_count)
3178 putc ('\n', stderr);
3179 return java_error_count;
3180 }
3181
3182 static char *
3183 java_accstring_lookup (flags)
3184 int flags;
3185 {
3186 static char buffer [80];
3187 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3188
3189 /* Access modifier looked-up first for easier report on forbidden
3190 access. */
3191 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3192 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3193 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3194 if (flags & ACC_STATIC) COPY_RETURN ("static");
3195 if (flags & ACC_FINAL) COPY_RETURN ("final");
3196 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3197 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3198 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3199 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3200 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3201 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3202
3203 buffer [0] = '\0';
3204 return buffer;
3205 #undef COPY_RETURN
3206 }
3207
3208 /* Issuing error messages upon redefinition of classes, interfaces or
3209 variables. */
3210
3211 static void
3212 classitf_redefinition_error (context, id, decl, cl)
3213 const char *context;
3214 tree id, decl, cl;
3215 {
3216 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3217 context, IDENTIFIER_POINTER (id),
3218 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3219 /* Here we should point out where its redefined. It's a unicode. FIXME */
3220 }
3221
3222 static void
3223 variable_redefinition_error (context, name, type, line)
3224 tree context, name, type;
3225 int line;
3226 {
3227 const char *type_name;
3228
3229 /* Figure a proper name for type. We might haven't resolved it */
3230 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3231 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3232 else
3233 type_name = lang_printable_name (type, 0);
3234
3235 parse_error_context (context,
3236 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3237 IDENTIFIER_POINTER (name),
3238 type_name, IDENTIFIER_POINTER (name), line);
3239 }
3240
3241 static tree
3242 build_array_from_name (type, type_wfl, name, ret_name)
3243 tree type, type_wfl, name, *ret_name;
3244 {
3245 int more_dims = 0;
3246 const char *string;
3247
3248 /* Eventually get more dims */
3249 string = IDENTIFIER_POINTER (name);
3250 while (string [more_dims] == '[')
3251 more_dims++;
3252
3253 /* If we have, then craft a new type for this variable */
3254 if (more_dims)
3255 {
3256 name = get_identifier (&string [more_dims]);
3257
3258 /* If we have a pointer, use its type */
3259 if (TREE_CODE (type) == POINTER_TYPE)
3260 type = TREE_TYPE (type);
3261
3262 /* Building the first dimension of a primitive type uses this
3263 function */
3264 if (JPRIMITIVE_TYPE_P (type))
3265 {
3266 type = build_java_array_type (type, -1);
3267 CLASS_LOADED_P (type) = 1;
3268 more_dims--;
3269 }
3270 /* Otherwise, if we have a WFL for this type, use it (the type
3271 is already an array on an unresolved type, and we just keep
3272 on adding dimensions) */
3273 else if (type_wfl)
3274 type = type_wfl;
3275
3276 /* Add all the dimensions */
3277 while (more_dims--)
3278 type = build_unresolved_array_type (type);
3279
3280 /* The type may have been incomplete in the first place */
3281 if (type_wfl)
3282 type = obtain_incomplete_type (type);
3283 }
3284
3285 if (ret_name)
3286 *ret_name = name;
3287 return type;
3288 }
3289
3290 /* Build something that the type identifier resolver will identify as
3291 being an array to an unresolved type. TYPE_WFL is a WFL on a
3292 identifier. */
3293
3294 static tree
3295 build_unresolved_array_type (type_or_wfl)
3296 tree type_or_wfl;
3297 {
3298 const char *ptr;
3299
3300 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3301 just create a array type */
3302 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3303 {
3304 tree type = build_java_array_type (type_or_wfl, -1);
3305 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3306 return type;
3307 }
3308
3309 obstack_1grow (&temporary_obstack, '[');
3310 obstack_grow0 (&temporary_obstack,
3311 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3312 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3313 ptr = obstack_finish (&temporary_obstack);
3314 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3315 return type_or_wfl;
3316 }
3317
3318 static void
3319 parser_add_interface (class_decl, interface_decl, wfl)
3320 tree class_decl, interface_decl, wfl;
3321 {
3322 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3323 parse_error_context (wfl, "Interface `%s' repeated",
3324 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3325 }
3326
3327 /* Bulk of common class/interface checks. Return 1 if an error was
3328 encountered. TAG is 0 for a class, 1 for an interface. */
3329
3330 static int
3331 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3332 int is_interface, flags;
3333 tree raw_name, qualified_name, decl, cl;
3334 {
3335 tree node;
3336 int sca = 0; /* Static class allowed */
3337 int icaf = 0; /* Inner class allowed flags */
3338 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3339
3340 if (!quiet_flag)
3341 fprintf (stderr, " %s%s %s",
3342 (CPC_INNER_P () ? "inner" : ""),
3343 (is_interface ? "interface" : "class"),
3344 IDENTIFIER_POINTER (qualified_name));
3345
3346 /* Scope of an interface/class type name:
3347 - Can't be imported by a single type import
3348 - Can't already exists in the package */
3349 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3350 && (node = find_name_in_single_imports (raw_name))
3351 && !CPC_INNER_P ())
3352 {
3353 parse_error_context
3354 (cl, "%s name `%s' clashes with imported type `%s'",
3355 (is_interface ? "Interface" : "Class"),
3356 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3357 return 1;
3358 }
3359 if (decl && CLASS_COMPLETE_P (decl))
3360 {
3361 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3362 qualified_name, decl, cl);
3363 return 1;
3364 }
3365
3366 if (check_inner_class_redefinition (raw_name, cl))
3367 return 1;
3368
3369 /* If public, file name should match class/interface name, except
3370 when dealing with an inner class */
3371 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3372 {
3373 const char *f;
3374
3375 /* Contains OS dependent assumption on path separator. FIXME */
3376 for (f = &input_filename [strlen (input_filename)];
3377 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3378 f--)
3379 ;
3380 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3381 f++;
3382 if (strncmp (IDENTIFIER_POINTER (raw_name),
3383 f , IDENTIFIER_LENGTH (raw_name)) ||
3384 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3385 parse_error_context
3386 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3387 (is_interface ? "interface" : "class"),
3388 IDENTIFIER_POINTER (qualified_name),
3389 IDENTIFIER_POINTER (raw_name));
3390 }
3391
3392 /* Static classes can be declared only in top level classes. Note:
3393 once static, a inner class is a top level class. */
3394 if (flags & ACC_STATIC)
3395 {
3396 /* Catch the specific error of declaring an class inner class
3397 with no toplevel enclosing class. Prevent check_modifiers from
3398 complaining a second time */
3399 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3400 {
3401 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3402 IDENTIFIER_POINTER (qualified_name));
3403 sca = ACC_STATIC;
3404 }
3405 /* Else, in the context of a top-level class declaration, let
3406 `check_modifiers' do its job, otherwise, give it a go */
3407 else
3408 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3409 }
3410
3411 /* Inner classes can be declared private or protected
3412 within their enclosing classes. */
3413 if (CPC_INNER_P ())
3414 {
3415 /* A class which is local to a block can't be public, private,
3416 protected or static. But it is created final, so allow this
3417 one. */
3418 if (current_function_decl)
3419 icaf = sca = uaaf = ACC_FINAL;
3420 else
3421 {
3422 check_modifiers_consistency (flags);
3423 icaf = ACC_PRIVATE|ACC_PROTECTED;
3424 }
3425 }
3426
3427 if (is_interface)
3428 {
3429 if (CPC_INNER_P ())
3430 uaaf = INTERFACE_INNER_MODIFIERS;
3431 else
3432 uaaf = INTERFACE_MODIFIERS;
3433
3434 check_modifiers ("Illegal modifier `%s' for interface declaration",
3435 flags, uaaf);
3436 }
3437 else
3438 check_modifiers ((current_function_decl ?
3439 "Illegal modifier `%s' for local class declaration" :
3440 "Illegal modifier `%s' for class declaration"),
3441 flags, uaaf|sca|icaf);
3442 return 0;
3443 }
3444
3445 static void
3446 make_nested_class_name (cpc_list)
3447 tree cpc_list;
3448 {
3449 tree name;
3450
3451 if (!cpc_list)
3452 return;
3453 else
3454 make_nested_class_name (TREE_CHAIN (cpc_list));
3455
3456 /* Pick the qualified name when dealing with the first upmost
3457 enclosing class */
3458 name = (TREE_CHAIN (cpc_list) ?
3459 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3460 obstack_grow (&temporary_obstack,
3461 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3462 /* Why is NO_DOLLAR_IN_LABEL defined? */
3463 #if 0
3464 #ifdef NO_DOLLAR_IN_LABEL
3465 internal_error ("Can't use '$' as a separator for inner classes");
3466 #endif
3467 #endif
3468 obstack_1grow (&temporary_obstack, '$');
3469 }
3470
3471 /* Can't redefine a class already defined in an earlier scope. */
3472
3473 static int
3474 check_inner_class_redefinition (raw_name, cl)
3475 tree raw_name, cl;
3476 {
3477 tree scope_list;
3478
3479 for (scope_list = GET_CPC_LIST (); scope_list;
3480 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3481 if (raw_name == GET_CPC_UN_NODE (scope_list))
3482 {
3483 parse_error_context
3484 (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",
3485 IDENTIFIER_POINTER (raw_name));
3486 return 1;
3487 }
3488 return 0;
3489 }
3490
3491 static tree
3492 find_as_inner_class (enclosing, name, cl)
3493 tree enclosing, name, cl;
3494 {
3495 tree qual, to_return;
3496 if (!enclosing)
3497 return NULL_TREE;
3498
3499 name = TYPE_NAME (name);
3500
3501 /* First search: within the scope of `enclosing', search for name */
3502 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3503 qual = EXPR_WFL_QUALIFICATION (cl);
3504 else if (cl)
3505 qual = build_tree_list (cl, NULL_TREE);
3506 else
3507 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3508
3509 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3510 return to_return;
3511
3512 /* We're dealing with a qualified name. Try to resolve thing until
3513 we get something that is an enclosing class. */
3514 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3515 {
3516 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3517
3518 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3519 qual = TREE_CHAIN (qual))
3520 {
3521 acc = merge_qualified_name (acc,
3522 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3523 BUILD_PTR_FROM_NAME (ptr, acc);
3524 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3525 }
3526
3527 /* A NULL qual and a decl means that the search ended
3528 successfully?!? We have to do something then. FIXME */
3529
3530 if (decl)
3531 enclosing = decl;
3532 else
3533 qual = EXPR_WFL_QUALIFICATION (cl);
3534 }
3535 /* Otherwise, create a qual for the other part of the resolution. */
3536 else
3537 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3538
3539 return find_as_inner_class_do (qual, enclosing);
3540 }
3541
3542 /* We go inside the list of sub classes and try to find a way
3543 through. */
3544
3545 static tree
3546 find_as_inner_class_do (qual, enclosing)
3547 tree qual, enclosing;
3548 {
3549 if (!qual)
3550 return NULL_TREE;
3551
3552 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3553 {
3554 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3555 tree next_enclosing = NULL_TREE;
3556 tree inner_list;
3557
3558 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3559 inner_list; inner_list = TREE_CHAIN (inner_list))
3560 {
3561 if (TREE_VALUE (inner_list) == name_to_match)
3562 {
3563 next_enclosing = TREE_PURPOSE (inner_list);
3564 break;
3565 }
3566 }
3567 enclosing = next_enclosing;
3568 }
3569
3570 return (!qual && enclosing ? enclosing : NULL_TREE);
3571 }
3572
3573 /* Reach all inner classes and tie their unqualified name to a
3574 DECL. */
3575
3576 static void
3577 set_nested_class_simple_name_value (outer, set)
3578 tree outer;
3579 int set;
3580 {
3581 tree l;
3582
3583 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3584 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3585 TREE_PURPOSE (l) : NULL_TREE);
3586 }
3587
3588 static void
3589 link_nested_class_to_enclosing ()
3590 {
3591 if (GET_ENCLOSING_CPC ())
3592 {
3593 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3594 DECL_INNER_CLASS_LIST (enclosing) =
3595 tree_cons (GET_CPC (), GET_CPC_UN (),
3596 DECL_INNER_CLASS_LIST (enclosing));
3597 enclosing = enclosing;
3598 }
3599 }
3600
3601 static tree
3602 maybe_make_nested_class_name (name)
3603 tree name;
3604 {
3605 tree id = NULL_TREE;
3606
3607 if (CPC_INNER_P ())
3608 {
3609 make_nested_class_name (GET_CPC_LIST ());
3610 obstack_grow0 (&temporary_obstack,
3611 IDENTIFIER_POINTER (name),
3612 IDENTIFIER_LENGTH (name));
3613 id = get_identifier (obstack_finish (&temporary_obstack));
3614 if (ctxp->package)
3615 QUALIFIED_P (id) = 1;
3616 }
3617 return id;
3618 }
3619
3620 /* If DECL is NULL, create and push a new DECL, record the current
3621 line CL and do other maintenance things. */
3622
3623 static tree
3624 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3625 tree decl, raw_name, qualified_name, cl;
3626 {
3627 if (!decl)
3628 decl = push_class (make_class (), qualified_name);
3629
3630 /* Take care of the file and line business */
3631 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3632 /* If we're emiting xrefs, store the line/col number information */
3633 if (flag_emit_xref)
3634 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3635 else
3636 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3637 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3638 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3639 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3640
3641 PUSH_CPC (decl, raw_name);
3642 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3643
3644 /* Link the declaration to the already seen ones */
3645 TREE_CHAIN (decl) = ctxp->class_list;
3646 ctxp->class_list = decl;
3647
3648 /* Create a new nodes in the global lists */
3649 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3650 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3651
3652 /* Install a new dependency list element */
3653 create_jdep_list (ctxp);
3654
3655 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3656 IDENTIFIER_POINTER (qualified_name)));
3657 return decl;
3658 }
3659
3660 static void
3661 add_superinterfaces (decl, interface_list)
3662 tree decl, interface_list;
3663 {
3664 tree node;
3665 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3666 takes care of ensuring that:
3667 - This is an accessible interface type,
3668 - Circularity detection.
3669 parser_add_interface is then called. If present but not defined,
3670 the check operation is delayed until the super interface gets
3671 defined. */
3672 for (node = interface_list; node; node = TREE_CHAIN (node))
3673 {
3674 tree current = TREE_PURPOSE (node);
3675 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3676 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3677 {
3678 if (!parser_check_super_interface (idecl, decl, current))
3679 parser_add_interface (decl, idecl, current);
3680 }
3681 else
3682 register_incomplete_type (JDEP_INTERFACE,
3683 current, decl, NULL_TREE);
3684 }
3685 }
3686
3687 /* Create an interface in pass1 and return its decl. Return the
3688 interface's decl in pass 2. */
3689
3690 static tree
3691 create_interface (flags, id, super)
3692 int flags;
3693 tree id, super;
3694 {
3695 tree raw_name = EXPR_WFL_NODE (id);
3696 tree q_name = parser_qualified_classname (raw_name);
3697 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3698
3699 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3700
3701 /* Basic checks: scope, redefinition, modifiers */
3702 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3703 {
3704 PUSH_ERROR ();
3705 return NULL_TREE;
3706 }
3707
3708 /* Suspend the current parsing context if we're parsing an inner
3709 interface */
3710 if (CPC_INNER_P ())
3711 java_parser_context_suspend ();
3712
3713 /* Push a new context for (static) initialized upon declaration fields */
3714 java_parser_context_push_initialized_field ();
3715
3716 /* Interface modifiers check
3717 - public/abstract allowed (already done at that point)
3718 - abstract is obsolete (comes first, it's a warning, or should be)
3719 - Can't use twice the same (checked in the modifier rule) */
3720 if ((flags & ACC_ABSTRACT) && flag_redundant)
3721 parse_warning_context
3722 (MODIFIER_WFL (ABSTRACT_TK),
3723 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3724
3725 /* Create a new decl if DECL is NULL, otherwise fix it */
3726 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3727
3728 /* Set super info and mark the class a complete */
3729 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3730 object_type_node, ctxp->interface_number);
3731 ctxp->interface_number = 0;
3732 CLASS_COMPLETE_P (decl) = 1;
3733 add_superinterfaces (decl, super);
3734
3735 return decl;
3736 }
3737
3738 /* Anonymous class counter. Will be reset to 1 every time a non
3739 anonymous class gets created. */
3740 static int anonymous_class_counter = 1;
3741
3742 /* Patch anonymous class CLASS, by either extending or implementing
3743 DEP. */
3744
3745 static void
3746 patch_anonymous_class (type_decl, class_decl, wfl)
3747 tree type_decl, class_decl, wfl;
3748 {
3749 tree class = TREE_TYPE (class_decl);
3750 tree type = TREE_TYPE (type_decl);
3751 tree binfo = TYPE_BINFO (class);
3752
3753 /* If it's an interface, implement it */
3754 if (CLASS_INTERFACE (type_decl))
3755 {
3756 tree s_binfo;
3757 int length;
3758
3759 if (parser_check_super_interface (type_decl, class_decl, wfl))
3760 return;
3761
3762 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3763 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3764 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3765 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3766 /* And add the interface */
3767 parser_add_interface (class_decl, type_decl, wfl);
3768 }
3769 /* Otherwise, it's a type we want to extend */
3770 else
3771 {
3772 if (parser_check_super (type_decl, class_decl, wfl))
3773 return;
3774 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3775 }
3776 }
3777
3778 static tree
3779 create_anonymous_class (location, type_name)
3780 int location;
3781 tree type_name;
3782 {
3783 char buffer [80];
3784 tree super = NULL_TREE, itf = NULL_TREE;
3785 tree id, type_decl, class;
3786
3787 /* The unqualified name of the anonymous class. It's just a number. */
3788 sprintf (buffer, "%d", anonymous_class_counter++);
3789 id = build_wfl_node (get_identifier (buffer));
3790 EXPR_WFL_LINECOL (id) = location;
3791
3792 /* We know about the type to extend/implement. We go ahead */
3793 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3794 {
3795 /* Create a class which either implements on extends the designated
3796 class. The class bears an innacessible name. */
3797 if (CLASS_INTERFACE (type_decl))
3798 {
3799 /* It's OK to modify it here. It's been already used and
3800 shouldn't be reused */
3801 ctxp->interface_number = 1;
3802 /* Interfaces should presented as a list of WFLs */
3803 itf = build_tree_list (type_name, NULL_TREE);
3804 }
3805 else
3806 super = type_name;
3807 }
3808
3809 class = create_class (ACC_FINAL, id, super, itf);
3810
3811 /* We didn't know anything about the stuff. We register a dependence. */
3812 if (!type_decl)
3813 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3814
3815 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3816 return class;
3817 }
3818
3819 /* Create a class in pass1 and return its decl. Return class
3820 interface's decl in pass 2. */
3821
3822 static tree
3823 create_class (flags, id, super, interfaces)
3824 int flags;
3825 tree id, super, interfaces;
3826 {
3827 tree raw_name = EXPR_WFL_NODE (id);
3828 tree class_id, decl;
3829 tree super_decl_type;
3830
3831 class_id = parser_qualified_classname (raw_name);
3832 decl = IDENTIFIER_CLASS_VALUE (class_id);
3833 EXPR_WFL_NODE (id) = class_id;
3834
3835 /* Basic check: scope, redefinition, modifiers */
3836 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3837 {
3838 PUSH_ERROR ();
3839 return NULL_TREE;
3840 }
3841
3842 /* Suspend the current parsing context if we're parsing an inner
3843 class or an anonymous class. */
3844 if (CPC_INNER_P ())
3845 java_parser_context_suspend ();
3846 /* Push a new context for (static) initialized upon declaration fields */
3847 java_parser_context_push_initialized_field ();
3848
3849 /* Class modifier check:
3850 - Allowed modifier (already done at that point)
3851 - abstract AND final forbidden
3852 - Public classes defined in the correct file */
3853 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3854 parse_error_context
3855 (id, "Class `%s' can't be declared both abstract and final",
3856 IDENTIFIER_POINTER (raw_name));
3857
3858 /* Create a new decl if DECL is NULL, otherwise fix it */
3859 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3860
3861 /* If SUPER exists, use it, otherwise use Object */
3862 if (super)
3863 {
3864 /* Can't extend java.lang.Object */
3865 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3866 {
3867 parse_error_context (id, "Can't extend `java.lang.Object'");
3868 return NULL_TREE;
3869 }
3870
3871 super_decl_type =
3872 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3873 }
3874 else if (TREE_TYPE (decl) != object_type_node)
3875 super_decl_type = object_type_node;
3876 /* We're defining java.lang.Object */
3877 else
3878 super_decl_type = NULL_TREE;
3879
3880 /* A class nested in an interface is implicitly static. */
3881 if (INNER_CLASS_DECL_P (decl)
3882 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3883 {
3884 flags |= ACC_STATIC;
3885 }
3886
3887 /* Set super info and mark the class as complete. */
3888 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3889 ctxp->interface_number);
3890 ctxp->interface_number = 0;
3891 CLASS_COMPLETE_P (decl) = 1;
3892 add_superinterfaces (decl, interfaces);
3893
3894 /* Add the private this$<n> field, Replicate final locals still in
3895 scope as private final fields mangled like val$<local_name>.
3896 This doesn't not occur for top level (static) inner classes. */
3897 if (PURE_INNER_CLASS_DECL_P (decl))
3898 add_inner_class_fields (decl, current_function_decl);
3899
3900 /* If doing xref, store the location at which the inherited class
3901 (if any) was seen. */
3902 if (flag_emit_xref && super)
3903 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3904
3905 /* Eventually sets the @deprecated tag flag */
3906 CHECK_DEPRECATED (decl);
3907
3908 /* Reset the anonymous class counter when declaring non inner classes */
3909 if (!INNER_CLASS_DECL_P (decl))
3910 anonymous_class_counter = 1;
3911
3912 return decl;
3913 }
3914
3915 /* End a class declaration: register the statements used to create
3916 finit$ and <clinit>, pop the current class and resume the prior
3917 parser context if necessary. */
3918
3919 static void
3920 end_class_declaration (resume)
3921 int resume;
3922 {
3923 /* If an error occured, context weren't pushed and won't need to be
3924 popped by a resume. */
3925 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3926
3927 java_parser_context_pop_initialized_field ();
3928 POP_CPC ();
3929 if (resume && no_error_occured)
3930 java_parser_context_resume ();
3931
3932 /* We're ending a class declaration, this is a good time to reset
3933 the interface cout. Note that might have been already done in
3934 create_interface, but if at that time an inner class was being
3935 dealt with, the interface count was reset in a context created
3936 for the sake of handling inner classes declaration. */
3937 ctxp->interface_number = 0;
3938 }
3939
3940 static void
3941 add_inner_class_fields (class_decl, fct_decl)
3942 tree class_decl;
3943 tree fct_decl;
3944 {
3945 tree block, marker, f;
3946
3947 f = add_field (TREE_TYPE (class_decl),
3948 build_current_thisn (TREE_TYPE (class_decl)),
3949 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3950 ACC_PRIVATE);
3951 FIELD_THISN (f) = 1;
3952
3953 if (!fct_decl)
3954 return;
3955
3956 for (block = GET_CURRENT_BLOCK (fct_decl);
3957 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3958 {
3959 tree decl;
3960 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3961 {
3962 tree name, pname;
3963 tree wfl, init, list;
3964
3965 /* Avoid non final arguments. */
3966 if (!LOCAL_FINAL_P (decl))
3967 continue;
3968
3969 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3970 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3971 wfl = build_wfl_node (name);
3972 init = build_wfl_node (pname);
3973 /* Build an initialization for the field: it will be
3974 initialized by a parameter added to finit$, bearing a
3975 mangled name of the field itself (param$<n>.) The
3976 parameter is provided to finit$ by the constructor
3977 invoking it (hence the constructor will also feature a
3978 hidden parameter, set to the value of the outer context
3979 local at the time the inner class is created.)
3980
3981 Note: we take into account all possible locals that can
3982 be accessed by the inner class. It's actually not trivial
3983 to minimize these aliases down to the ones really
3984 used. One way to do that would be to expand all regular
3985 methods first, then finit$ to get a picture of what's
3986 used. It works with the exception that we would have to
3987 go back on all constructor invoked in regular methods to
3988 have their invokation reworked (to include the right amount
3989 of alias initializer parameters.)
3990
3991 The only real way around, I think, is a first pass to
3992 identify locals really used in the inner class. We leave
3993 the flag FIELD_LOCAL_ALIAS_USED around for that future
3994 use.
3995
3996 On the other hand, it only affect local inner classes,
3997 whose constructors (and finit$ call) will be featuring
3998 unecessary arguments. It's easy for a developper to keep
3999 this number of parameter down by using the `final'
4000 keyword only when necessary. For the time being, we can
4001 issue a warning on unecessary finals. FIXME */
4002 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4003 wfl, init);
4004
4005 /* Register the field. The TREE_LIST holding the part
4006 initialized/initializer will be marked ARG_FINAL_P so
4007 that the created field can be marked
4008 FIELD_LOCAL_ALIAS. */
4009 list = build_tree_list (wfl, init);
4010 ARG_FINAL_P (list) = 1;
4011 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4012 }
4013 }
4014
4015 if (!CPC_INITIALIZER_STMT (ctxp))
4016 return;
4017
4018 /* If we ever registered an alias field, insert and marker to
4019 remeber where the list ends. The second part of the list (the one
4020 featuring initialized fields) so it can be later reversed to
4021 enforce 8.5. The marker will be removed during that operation. */
4022 marker = build_tree_list (NULL_TREE, NULL_TREE);
4023 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4024 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4025 }
4026
4027 /* Can't use lookup_field () since we don't want to load the class and
4028 can't set the CLASS_LOADED_P flag */
4029
4030 static tree
4031 find_field (class, name)
4032 tree class;
4033 tree name;
4034 {
4035 tree decl;
4036 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4037 {
4038 if (DECL_NAME (decl) == name)
4039 return decl;
4040 }
4041 return NULL_TREE;
4042 }
4043
4044 /* Wrap around lookup_field that doesn't potentially upset the value
4045 of CLASS */
4046
4047 static tree
4048 lookup_field_wrapper (class, name)
4049 tree class, name;
4050 {
4051 tree type = class;
4052 tree decl = NULL_TREE;
4053 java_parser_context_save_global ();
4054
4055 /* Last chance: if we're within the context of an inner class, we
4056 might be trying to access a local variable defined in an outer
4057 context. We try to look for it now. */
4058 if (INNER_CLASS_TYPE_P (class))
4059 {
4060 tree new_name;
4061 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4062 decl = lookup_field (&type, new_name);
4063 if (decl && decl != error_mark_node)
4064 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4065 }
4066 if (!decl || decl == error_mark_node)
4067 {
4068 type = class;
4069 decl = lookup_field (&type, name);
4070 }
4071
4072 java_parser_context_restore_global ();
4073 return decl == error_mark_node ? NULL : decl;
4074 }
4075
4076 /* Find duplicate field within the same class declarations and report
4077 the error. Returns 1 if a duplicated field was found, 0
4078 otherwise. */
4079
4080 static int
4081 duplicate_declaration_error_p (new_field_name, new_type, cl)
4082 tree new_field_name, new_type, cl;
4083 {
4084 /* This might be modified to work with method decl as well */
4085 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4086 if (decl)
4087 {
4088 char *t1 = xstrdup (purify_type_name
4089 ((TREE_CODE (new_type) == POINTER_TYPE
4090 && TREE_TYPE (new_type) == NULL_TREE) ?
4091 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4092 lang_printable_name (new_type, 1)));
4093 /* The type may not have been completed by the time we report
4094 the error */
4095 char *t2 = xstrdup (purify_type_name
4096 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4097 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4098 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4099 lang_printable_name (TREE_TYPE (decl), 1)));
4100 parse_error_context
4101 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4102 t1, IDENTIFIER_POINTER (new_field_name),
4103 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4104 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4105 free (t1);
4106 free (t2);
4107 return 1;
4108 }
4109 return 0;
4110 }
4111
4112 /* Field registration routine. If TYPE doesn't exist, field
4113 declarations are linked to the undefined TYPE dependency list, to
4114 be later resolved in java_complete_class () */
4115
4116 static void
4117 register_fields (flags, type, variable_list)
4118 int flags;
4119 tree type, variable_list;
4120 {
4121 tree current, saved_type;
4122 tree class_type = NULL_TREE;
4123 int saved_lineno = lineno;
4124 int must_chain = 0;
4125 tree wfl = NULL_TREE;
4126
4127 if (GET_CPC ())
4128 class_type = TREE_TYPE (GET_CPC ());
4129
4130 if (!class_type || class_type == error_mark_node)
4131 return;
4132
4133 /* If we're adding fields to interfaces, those fields are public,
4134 static, final */
4135 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4136 {
4137 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4138 flags, ACC_PUBLIC, "interface field(s)");
4139 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4140 flags, ACC_STATIC, "interface field(s)");
4141 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4142 flags, ACC_FINAL, "interface field(s)");
4143 check_modifiers ("Illegal interface member modifier `%s'", flags,
4144 INTERFACE_FIELD_MODIFIERS);
4145 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4146 }
4147
4148 /* Obtain a suitable type for resolution, if necessary */
4149 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4150
4151 /* If TYPE is fully resolved and we don't have a reference, make one */
4152 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4153
4154 for (current = variable_list, saved_type = type; current;
4155 current = TREE_CHAIN (current), type = saved_type)
4156 {
4157 tree real_type;
4158 tree field_decl;
4159 tree cl = TREE_PURPOSE (current);
4160 tree init = TREE_VALUE (current);
4161 tree current_name = EXPR_WFL_NODE (cl);
4162
4163 /* Can't declare non-final static fields in inner classes */
4164 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4165 && !(flags & ACC_FINAL))
4166 parse_error_context
4167 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4168 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4169 lang_printable_name (class_type, 0));
4170
4171 /* Process NAME, as it may specify extra dimension(s) for it */
4172 type = build_array_from_name (type, wfl, current_name, &current_name);
4173
4174 /* Type adjustment. We may have just readjusted TYPE because
4175 the variable specified more dimensions. Make sure we have
4176 a reference if we can and don't have one already. Also
4177 change the name if we have an init. */
4178 if (type != saved_type)
4179 {
4180 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4181 if (init)
4182 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4183 }
4184
4185 real_type = GET_REAL_TYPE (type);
4186 /* Check for redeclarations */
4187 if (duplicate_declaration_error_p (current_name, real_type, cl))
4188 continue;
4189
4190 /* Set lineno to the line the field was found and create a
4191 declaration for it. Eventually sets the @deprecated tag flag. */
4192 if (flag_emit_xref)
4193 lineno = EXPR_WFL_LINECOL (cl);
4194 else
4195 lineno = EXPR_WFL_LINENO (cl);
4196 field_decl = add_field (class_type, current_name, real_type, flags);
4197 CHECK_DEPRECATED (field_decl);
4198
4199 /* If the field denotes a final instance variable, then we
4200 allocate a LANG_DECL_SPECIFIC part to keep track of its
4201 initialization. We also mark whether the field was
4202 initialized upon it's declaration. We don't do that if the
4203 created field is an alias to a final local. */
4204 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4205 {
4206 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4207 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4208 if ((flags & ACC_STATIC) && init)
4209 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4210 }
4211
4212 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4213 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4214 hide parameters to this inner class finit$ and
4215 constructors. It also means that the field isn't final per
4216 say. */
4217 if (ARG_FINAL_P (current))
4218 {
4219 FIELD_LOCAL_ALIAS (field_decl) = 1;
4220 FIELD_FINAL (field_decl) = 0;
4221 }
4222
4223 /* Check if we must chain. */
4224 if (must_chain)
4225 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4226
4227 /* If we have an initialization value tied to the field */
4228 if (init)
4229 {
4230 /* The field is declared static */
4231 if (flags & ACC_STATIC)
4232 {
4233 /* We include the field and its initialization part into
4234 a list used to generate <clinit>. After <clinit> is
4235 walked, field initializations will be processed and
4236 fields initialized with known constants will be taken
4237 out of <clinit> and have their DECL_INITIAL set
4238 appropriately. */
4239 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4240 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4241 if (TREE_OPERAND (init, 1)
4242 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4243 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4244 }
4245 /* A non-static field declared with an immediate initialization is
4246 to be initialized in <init>, if any. This field is remembered
4247 to be processed at the time of the generation of <init>. */
4248 else
4249 {
4250 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4251 SET_CPC_INITIALIZER_STMT (ctxp, init);
4252 }
4253 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4254 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4255 }
4256 }
4257 lineno = saved_lineno;
4258 }
4259
4260 /* Generate finit$, using the list of initialized fields to populate
4261 its body. finit$'s parameter(s) list is adjusted to include the
4262 one(s) used to initialized the field(s) caching outer context
4263 local(s). */
4264
4265 static tree
4266 generate_finit (class_type)
4267 tree class_type;
4268 {
4269 int count = 0;
4270 tree list = TYPE_FINIT_STMT_LIST (class_type);
4271 tree mdecl, current, parms;
4272
4273 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4274 class_type, NULL_TREE,
4275 &count);
4276 CRAFTED_PARAM_LIST_FIXUP (parms);
4277 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4278 finit_identifier_node, parms);
4279 fix_method_argument_names (parms, mdecl);
4280 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4281 mdecl, NULL_TREE);
4282 DECL_FUNCTION_NAP (mdecl) = count;
4283 start_artificial_method_body (mdecl);
4284
4285 for (current = list; current; current = TREE_CHAIN (current))
4286 java_method_add_stmt (mdecl,
4287 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4288 current));
4289 end_artificial_method_body (mdecl);
4290 return mdecl;
4291 }
4292
4293 static void
4294 add_instance_initializer (mdecl)
4295 tree mdecl;
4296 {
4297 tree current;
4298 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4299 tree compound = NULL_TREE;
4300
4301 if (stmt_list)
4302 {
4303 for (current = stmt_list; current; current = TREE_CHAIN (current))
4304 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4305
4306 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4307 NULL_TREE, compound));
4308 }
4309 }
4310
4311 /* Shared accros method_declarator and method_header to remember the
4312 patch stage that was reached during the declaration of the method.
4313 A method DECL is built differently is there is no patch
4314 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4315 pending on the currently defined method. */
4316
4317 static int patch_stage;
4318
4319 /* Check the method declaration and add the method to its current
4320 class. If the argument list is known to contain incomplete types,
4321 the method is partially added and the registration will be resume
4322 once the method arguments resolved. If TYPE is NULL, we're dealing
4323 with a constructor. */
4324
4325 static tree
4326 method_header (flags, type, mdecl, throws)
4327 int flags;
4328 tree type, mdecl, throws;
4329 {
4330 tree type_wfl = NULL_TREE;
4331 tree meth_name = NULL_TREE;
4332 tree current, orig_arg, this_class = NULL;
4333 tree id, meth;
4334 int saved_lineno;
4335 int constructor_ok = 0, must_chain;
4336 int count;
4337
4338 if (mdecl == error_mark_node)
4339 return error_mark_node;
4340 meth = TREE_VALUE (mdecl);
4341 id = TREE_PURPOSE (mdecl);
4342
4343 check_modifiers_consistency (flags);
4344
4345 if (GET_CPC ())
4346 this_class = TREE_TYPE (GET_CPC ());
4347
4348 if (!this_class || this_class == error_mark_node)
4349 return NULL_TREE;
4350
4351 /* There are some forbidden modifiers for an abstract method and its
4352 class must be abstract as well. */
4353 if (type && (flags & ACC_ABSTRACT))
4354 {
4355 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4356 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4357 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4358 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4359 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4360 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4361 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4362 parse_error_context
4363 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4364 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4365 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4366 }
4367
4368 /* Things to be checked when declaring a constructor */
4369 if (!type)
4370 {
4371 int ec = java_error_count;
4372 /* 8.6: Constructor declarations: we might be trying to define a
4373 method without specifying a return type. */
4374 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4375 parse_error_context
4376 (id, "Invalid method declaration, return type required");
4377 /* 8.6.3: Constructor modifiers */
4378 else
4379 {
4380 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4381 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4382 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4383 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4384 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4385 }
4386 /* If we found error here, we don't consider it's OK to tread
4387 the method definition as a constructor, for the rest of this
4388 function */
4389 if (ec == java_error_count)
4390 constructor_ok = 1;
4391 }
4392
4393 /* Method declared within the scope of an interface are implicitly
4394 abstract and public. Conflicts with other erroneously provided
4395 modifiers are checked right after. */
4396
4397 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4398 {
4399 /* If FLAGS isn't set because of a modifier, turn the
4400 corresponding modifier WFL to NULL so we issue a warning on
4401 the obsolete use of the modifier */
4402 if (!(flags & ACC_PUBLIC))
4403 MODIFIER_WFL (PUBLIC_TK) = NULL;
4404 if (!(flags & ACC_ABSTRACT))
4405 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4406 flags |= ACC_PUBLIC;
4407 flags |= ACC_ABSTRACT;
4408 }
4409
4410 /* Inner class can't declare static methods */
4411 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4412 {
4413 parse_error_context
4414 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4415 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4416 lang_printable_name (this_class, 0));
4417 }
4418
4419 /* Modifiers context reset moved up, so abstract method declaration
4420 modifiers can be later checked. */
4421
4422 /* Set constructor returned type to void and method name to <init>,
4423 unless we found an error identifier the constructor (in which
4424 case we retain the original name) */
4425 if (!type)
4426 {
4427 type = void_type_node;
4428 if (constructor_ok)
4429 meth_name = init_identifier_node;
4430 }
4431 else
4432 meth_name = EXPR_WFL_NODE (id);
4433
4434 /* Do the returned type resolution and registration if necessary */
4435 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4436
4437 if (meth_name)
4438 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4439 EXPR_WFL_NODE (id) = meth_name;
4440 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4441
4442 if (must_chain)
4443 {
4444 patch_stage = JDEP_METHOD_RETURN;
4445 register_incomplete_type (patch_stage, type_wfl, id, type);
4446 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4447 }
4448 else
4449 TREE_TYPE (meth) = type;
4450
4451 saved_lineno = lineno;
4452 /* When defining an abstract or interface method, the curly
4453 bracket at level 1 doesn't exist because there is no function
4454 body */
4455 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4456 EXPR_WFL_LINENO (id));
4457
4458 /* Remember the original argument list */
4459 orig_arg = TYPE_ARG_TYPES (meth);
4460
4461 if (patch_stage) /* includes ret type and/or all args */
4462 {
4463 jdep *jdep;
4464 meth = add_method_1 (this_class, flags, meth_name, meth);
4465 /* Patch for the return type */
4466 if (patch_stage == JDEP_METHOD_RETURN)
4467 {
4468 jdep = CLASSD_LAST (ctxp->classd_list);
4469 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4470 }
4471 /* This is the stop JDEP. METH allows the function's signature
4472 to be computed. */
4473 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4474 }
4475 else
4476 meth = add_method (this_class, flags, meth_name,
4477 build_java_signature (meth));
4478
4479 /* Remember final parameters */
4480 MARK_FINAL_PARMS (meth, orig_arg);
4481
4482 /* Fix the method argument list so we have the argument name
4483 information */
4484 fix_method_argument_names (orig_arg, meth);
4485
4486 /* Register the parameter number and re-install the current line
4487 number */
4488 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4489 lineno = saved_lineno;
4490
4491 /* Register exception specified by the `throws' keyword for
4492 resolution and set the method decl appropriate field to the list.
4493 Note: the grammar ensures that what we get here are class
4494 types. */
4495 if (throws)
4496 {
4497 throws = nreverse (throws);
4498 for (current = throws; current; current = TREE_CHAIN (current))
4499 {
4500 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4501 NULL_TREE, NULL_TREE);
4502 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4503 &TREE_VALUE (current);
4504 }
4505 DECL_FUNCTION_THROWS (meth) = throws;
4506 }
4507
4508 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4509 DECL_FUNCTION_WFL (meth) = id;
4510
4511 /* Set the flag if we correctly processed a constructor */
4512 if (constructor_ok)
4513 {
4514 DECL_CONSTRUCTOR_P (meth) = 1;
4515 /* Compute and store the number of artificial parameters declared
4516 for this constructor */
4517 for (count = 0, current = TYPE_FIELDS (this_class); current;
4518 current = TREE_CHAIN (current))
4519 if (FIELD_LOCAL_ALIAS (current))
4520 count++;
4521 DECL_FUNCTION_NAP (meth) = count;
4522 }
4523
4524 /* Eventually set the @deprecated tag flag */
4525 CHECK_DEPRECATED (meth);
4526
4527 /* If doing xref, store column and line number information instead
4528 of the line number only. */
4529 if (flag_emit_xref)
4530 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4531
4532 return meth;
4533 }
4534
4535 static void
4536 fix_method_argument_names (orig_arg, meth)
4537 tree orig_arg, meth;
4538 {
4539 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4540 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4541 {
4542 TREE_PURPOSE (arg) = this_identifier_node;
4543 arg = TREE_CHAIN (arg);
4544 }
4545 while (orig_arg != end_params_node)
4546 {
4547 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4548 orig_arg = TREE_CHAIN (orig_arg);
4549 arg = TREE_CHAIN (arg);
4550 }
4551 }
4552
4553 /* Complete the method declaration with METHOD_BODY. */
4554
4555 static void
4556 finish_method_declaration (method_body)
4557 tree method_body;
4558 {
4559 int flags;
4560
4561 if (!current_function_decl)
4562 return;
4563
4564 flags = get_access_flags_from_decl (current_function_decl);
4565
4566 /* 8.4.5 Method Body */
4567 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4568 {
4569 tree name = DECL_NAME (current_function_decl);
4570 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4571 "%s method `%s' can't have a body defined",
4572 (METHOD_NATIVE (current_function_decl) ?
4573 "Native" : "Abstract"),
4574 IDENTIFIER_POINTER (name));
4575 method_body = NULL_TREE;
4576 }
4577 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4578 {
4579 tree name = DECL_NAME (current_function_decl);
4580 parse_error_context
4581 (DECL_FUNCTION_WFL (current_function_decl),
4582 "Non native and non abstract method `%s' must have a body defined",
4583 IDENTIFIER_POINTER (name));
4584 method_body = NULL_TREE;
4585 }
4586
4587 if (flag_emit_class_files && method_body
4588 && TREE_CODE (method_body) == NOP_EXPR
4589 && TREE_TYPE (current_function_decl)
4590 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4591 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4592
4593 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4594 maybe_absorb_scoping_blocks ();
4595 /* Exit function's body */
4596 exit_block ();
4597 /* Merge last line of the function with first line, directly in the
4598 function decl. It will be used to emit correct debug info. */
4599 if (!flag_emit_xref)
4600 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4601
4602 /* Since function's argument's list are shared, reset the
4603 ARG_FINAL_P parameter that might have been set on some of this
4604 function parameters. */
4605 UNMARK_FINAL_PARMS (current_function_decl);
4606
4607 /* So we don't have an irrelevant function declaration context for
4608 the next static block we'll see. */
4609 current_function_decl = NULL_TREE;
4610 }
4611
4612 /* Build a an error message for constructor circularity errors. */
4613
4614 static char *
4615 constructor_circularity_msg (from, to)
4616 tree from, to;
4617 {
4618 static char string [4096];
4619 char *t = xstrdup (lang_printable_name (from, 0));
4620 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4621 free (t);
4622 return string;
4623 }
4624
4625 /* Verify a circular call to METH. Return 1 if an error is found, 0
4626 otherwise. */
4627
4628 static int
4629 verify_constructor_circularity (meth, current)
4630 tree meth, current;
4631 {
4632 static tree list = NULL_TREE;
4633 static int initialized_p;
4634 tree c;
4635
4636 /* If we haven't already registered LIST with the garbage collector,
4637 do so now. */
4638 if (!initialized_p)
4639 {
4640 ggc_add_tree_root (&list, 1);
4641 initialized_p = 1;
4642 }
4643
4644 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4645 {
4646 if (TREE_VALUE (c) == meth)
4647 {
4648 char *t;
4649 if (list)
4650 {
4651 tree liste;
4652 list = nreverse (list);
4653 for (liste = list; liste; liste = TREE_CHAIN (liste))
4654 {
4655 parse_error_context
4656 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4657 constructor_circularity_msg
4658 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4659 java_error_count--;
4660 }
4661 }
4662 t = xstrdup (lang_printable_name (meth, 0));
4663 parse_error_context (TREE_PURPOSE (c),
4664 "%s: recursive invocation of constructor `%s'",
4665 constructor_circularity_msg (current, meth), t);
4666 free (t);
4667 list = NULL_TREE;
4668 return 1;
4669 }
4670 }
4671 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4672 {
4673 list = tree_cons (c, current, list);
4674 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4675 return 1;
4676 list = TREE_CHAIN (list);
4677 }
4678 return 0;
4679 }
4680
4681 /* Check modifiers that can be declared but exclusively */
4682
4683 static void
4684 check_modifiers_consistency (flags)
4685 int flags;
4686 {
4687 int acc_count = 0;
4688 tree cl = NULL_TREE;
4689
4690 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4691 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4692 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4693 if (acc_count > 1)
4694 parse_error_context
4695 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4696
4697 acc_count = 0;
4698 cl = NULL_TREE;
4699 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4700 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4701 if (acc_count > 1)
4702 parse_error_context (cl,
4703 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4704 }
4705
4706 /* Check the methode header METH for abstract specifics features */
4707
4708 static void
4709 check_abstract_method_header (meth)
4710 tree meth;
4711 {
4712 int flags = get_access_flags_from_decl (meth);
4713
4714 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4715 ACC_ABSTRACT, "abstract method",
4716 IDENTIFIER_POINTER (DECL_NAME (meth)));
4717 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4718 ACC_PUBLIC, "abstract method",
4719 IDENTIFIER_POINTER (DECL_NAME (meth)));
4720
4721 check_modifiers ("Illegal modifier `%s' for interface method",
4722 flags, INTERFACE_METHOD_MODIFIERS);
4723 }
4724
4725 /* Create a FUNCTION_TYPE node and start augmenting it with the
4726 declared function arguments. Arguments type that can't be resolved
4727 are left as they are, but the returned node is marked as containing
4728 incomplete types. */
4729
4730 static tree
4731 method_declarator (id, list)
4732 tree id, list;
4733 {
4734 tree arg_types = NULL_TREE, current, node;
4735 tree meth = make_node (FUNCTION_TYPE);
4736 jdep *jdep;
4737
4738 patch_stage = JDEP_NO_PATCH;
4739
4740 if (GET_CPC () == error_mark_node)
4741 return error_mark_node;
4742
4743 /* If we're dealing with an inner class constructor, we hide the
4744 this$<n> decl in the name field of its parameter declaration. We
4745 also might have to hide the outer context local alias
4746 initializers. Not done when the class is a toplevel class. */
4747 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4748 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4749 {
4750 tree aliases_list, type, thisn;
4751 /* First the aliases, linked to the regular parameters */
4752 aliases_list =
4753 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4754 TREE_TYPE (GET_CPC ()),
4755 NULL_TREE, NULL);
4756 list = chainon (nreverse (aliases_list), list);
4757
4758 /* Then this$<n> */
4759 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4760 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4761 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4762 list);
4763 }
4764
4765 for (current = list; current; current = TREE_CHAIN (current))
4766 {
4767 int must_chain = 0;
4768 tree wfl_name = TREE_PURPOSE (current);
4769 tree type = TREE_VALUE (current);
4770 tree name = EXPR_WFL_NODE (wfl_name);
4771 tree already, arg_node;
4772 tree type_wfl = NULL_TREE;
4773 tree real_type;
4774
4775 /* Obtain a suitable type for resolution, if necessary */
4776 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4777
4778 /* Process NAME, as it may specify extra dimension(s) for it */
4779 type = build_array_from_name (type, type_wfl, name, &name);
4780 EXPR_WFL_NODE (wfl_name) = name;
4781
4782 real_type = GET_REAL_TYPE (type);
4783 if (TREE_CODE (real_type) == RECORD_TYPE)
4784 {
4785 real_type = promote_type (real_type);
4786 if (TREE_CODE (type) == TREE_LIST)
4787 TREE_PURPOSE (type) = real_type;
4788 }
4789
4790 /* Check redefinition */
4791 for (already = arg_types; already; already = TREE_CHAIN (already))
4792 if (TREE_PURPOSE (already) == name)
4793 {
4794 parse_error_context
4795 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4796 IDENTIFIER_POINTER (name),
4797 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4798 break;
4799 }
4800
4801 /* If we've an incomplete argument type, we know there is a location
4802 to patch when the type get resolved, later. */
4803 jdep = NULL;
4804 if (must_chain)
4805 {
4806 patch_stage = JDEP_METHOD;
4807 type = register_incomplete_type (patch_stage,
4808 type_wfl, wfl_name, type);
4809 jdep = CLASSD_LAST (ctxp->classd_list);
4810 JDEP_MISC (jdep) = id;
4811 }
4812
4813 /* The argument node: a name and a (possibly) incomplete type. */
4814 arg_node = build_tree_list (name, real_type);
4815 /* Remeber arguments declared final. */
4816 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4817
4818 if (jdep)
4819 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4820 TREE_CHAIN (arg_node) = arg_types;
4821 arg_types = arg_node;
4822 }
4823 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4824 node = build_tree_list (id, meth);
4825 return node;
4826 }
4827
4828 static int
4829 unresolved_type_p (wfl, returned)
4830 tree wfl;
4831 tree *returned;
4832
4833 {
4834 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4835 {
4836 if (returned)
4837 {
4838 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4839 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4840 *returned = TREE_TYPE (decl);
4841 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4842 *returned = TREE_TYPE (GET_CPC ());
4843 else
4844 *returned = NULL_TREE;
4845 }
4846 return 1;
4847 }
4848 if (returned)
4849 *returned = wfl;
4850 return 0;
4851 }
4852
4853 /* From NAME, build a qualified identifier node using the
4854 qualification from the current package definition. */
4855
4856 static tree
4857 parser_qualified_classname (name)
4858 tree name;
4859 {
4860 tree nested_class_name;
4861
4862 if ((nested_class_name = maybe_make_nested_class_name (name)))
4863 return nested_class_name;
4864
4865 if (ctxp->package)
4866 return merge_qualified_name (ctxp->package, name);
4867 else
4868 return name;
4869 }
4870
4871 /* Called once the type a interface extends is resolved. Returns 0 if
4872 everything is OK. */
4873
4874 static int
4875 parser_check_super_interface (super_decl, this_decl, this_wfl)
4876 tree super_decl, this_decl, this_wfl;
4877 {
4878 tree super_type = TREE_TYPE (super_decl);
4879
4880 /* Has to be an interface */
4881 if (!CLASS_INTERFACE (super_decl))
4882 {
4883 parse_error_context
4884 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4885 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4886 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4887 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4888 "interface" : "class"),
4889 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4890 return 1;
4891 }
4892
4893 /* Check top-level interface access. Inner classes are subject to member
4894 access rules (6.6.1). */
4895 if (! INNER_CLASS_P (super_type)
4896 && check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4897 return 1;
4898
4899 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4900 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4901 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4902 return 0;
4903 }
4904
4905 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4906 0 if everthing is OK. */
4907
4908 static int
4909 parser_check_super (super_decl, this_decl, wfl)
4910 tree super_decl, this_decl, wfl;
4911 {
4912 tree super_type = TREE_TYPE (super_decl);
4913
4914 /* SUPER should be a CLASS (neither an array nor an interface) */
4915 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4916 {
4917 parse_error_context
4918 (wfl, "Class `%s' can't subclass %s `%s'",
4919 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4920 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4921 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4922 return 1;
4923 }
4924
4925 if (CLASS_FINAL (TYPE_NAME (super_type)))
4926 {
4927 parse_error_context (wfl, "Can't subclass final classes: %s",
4928 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4929 return 1;
4930 }
4931
4932 /* Check top-level class scope. Inner classes are subject to member access
4933 rules (6.6.1). */
4934 if (! INNER_CLASS_P (super_type)
4935 && (check_pkg_class_access (DECL_NAME (super_decl), wfl)))
4936 return 1;
4937
4938 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4939 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4940 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4941 return 0;
4942 }
4943
4944 /* Create a new dependency list and link it (in a LIFO manner) to the
4945 CTXP list of type dependency list. */
4946
4947 static void
4948 create_jdep_list (ctxp)
4949 struct parser_ctxt *ctxp;
4950 {
4951 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4952 new->first = new->last = NULL;
4953 new->next = ctxp->classd_list;
4954 ctxp->classd_list = new;
4955 }
4956
4957 static jdeplist *
4958 reverse_jdep_list (ctxp)
4959 struct parser_ctxt *ctxp;
4960 {
4961 register jdeplist *prev = NULL, *current, *next;
4962 for (current = ctxp->classd_list; current; current = next)
4963 {
4964 next = current->next;
4965 current->next = prev;
4966 prev = current;
4967 }
4968 return prev;
4969 }
4970
4971 /* Create a fake pointer based on the ID stored in
4972 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4973 registered again. */
4974
4975 static tree
4976 obtain_incomplete_type (type_name)
4977 tree type_name;
4978 {
4979 tree ptr = NULL_TREE, name;
4980
4981 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4982 name = EXPR_WFL_NODE (type_name);
4983 else if (INCOMPLETE_TYPE_P (type_name))
4984 name = TYPE_NAME (type_name);
4985 else
4986 abort ();
4987
4988 BUILD_PTR_FROM_NAME (ptr, name);
4989 layout_type (ptr);
4990
4991 return ptr;
4992 }
4993
4994 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4995 non NULL instead of computing a new fake type based on WFL. The new
4996 dependency is inserted in the current type dependency list, in FIFO
4997 manner. */
4998
4999 static tree
5000 register_incomplete_type (kind, wfl, decl, ptr)
5001 int kind;
5002 tree wfl, decl, ptr;
5003 {
5004 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5005
5006 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5007 ptr = obtain_incomplete_type (wfl);
5008
5009 JDEP_KIND (new) = kind;
5010 JDEP_DECL (new) = decl;
5011 JDEP_TO_RESOLVE (new) = ptr;
5012 JDEP_WFL (new) = wfl;
5013 JDEP_CHAIN (new) = NULL;
5014 JDEP_MISC (new) = NULL_TREE;
5015 /* For some dependencies, set the enclosing class of the current
5016 class to be the enclosing context */
5017 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5018 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
5019 && GET_ENCLOSING_CPC ())
5020 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5021 else
5022 JDEP_ENCLOSING (new) = GET_CPC ();
5023 JDEP_GET_PATCH (new) = (tree *)NULL;
5024
5025 JDEP_INSERT (ctxp->classd_list, new);
5026
5027 return ptr;
5028 }
5029
5030 void
5031 java_check_circular_reference ()
5032 {
5033 tree current;
5034 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5035 {
5036 tree type = TREE_TYPE (current);
5037 if (CLASS_INTERFACE (current))
5038 {
5039 /* Check all interfaces this class extends */
5040 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5041 int n, i;
5042
5043 if (!basetype_vec)
5044 return;
5045 n = TREE_VEC_LENGTH (basetype_vec);
5046 for (i = 0; i < n; i++)
5047 {
5048 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5049 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5050 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5051 parse_error_context (lookup_cl (current),
5052 "Cyclic interface inheritance");
5053 }
5054 }
5055 else
5056 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5057 parse_error_context (lookup_cl (current),
5058 "Cyclic class inheritance%s",
5059 (cyclic_inheritance_report ?
5060 cyclic_inheritance_report : ""));
5061 }
5062 }
5063
5064 /* Augment the parameter list PARM with parameters crafted to
5065 initialize outer context locals aliases. Through ARTIFICIAL, a
5066 count is kept of the number of crafted parameters. MODE governs
5067 what eventually gets created: something suitable for a function
5068 creation or a function invocation, either the constructor or
5069 finit$. */
5070
5071 static tree
5072 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5073 int mode;
5074 tree class_type, parm;
5075 int *artificial;
5076 {
5077 tree field;
5078 tree additional_parms = NULL_TREE;
5079
5080 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5081 if (FIELD_LOCAL_ALIAS (field))
5082 {
5083 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5084 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5085 tree mangled_id;
5086
5087 switch (mode)
5088 {
5089 case AIPL_FUNCTION_DECLARATION:
5090 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5091 &buffer [4]);
5092 purpose = build_wfl_node (mangled_id);
5093 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5094 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5095 else
5096 value = TREE_TYPE (field);
5097 break;
5098
5099 case AIPL_FUNCTION_CREATION:
5100 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5101 &buffer [4]);
5102 value = TREE_TYPE (field);
5103 break;
5104
5105 case AIPL_FUNCTION_FINIT_INVOCATION:
5106 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5107 &buffer [4]);
5108 /* Now, this is wrong. purpose should always be the NAME
5109 of something and value its matching value (decl, type,
5110 etc...) FIXME -- but there is a lot to fix. */
5111
5112 /* When invoked for this kind of operation, we already
5113 know whether a field is used or not. */
5114 purpose = TREE_TYPE (field);
5115 value = build_wfl_node (mangled_id);
5116 break;
5117
5118 case AIPL_FUNCTION_CTOR_INVOCATION:
5119 /* There are two case: the constructor invokation happends
5120 outside the local inner, in which case, locales from the outer
5121 context are directly used.
5122
5123 Otherwise, we fold to using the alias directly. */
5124 if (class_type == current_class)
5125 value = field;
5126 else
5127 {
5128 name = get_identifier (&buffer[4]);
5129 value = IDENTIFIER_LOCAL_VALUE (name);
5130 }
5131 break;
5132 }
5133 additional_parms = tree_cons (purpose, value, additional_parms);
5134 if (artificial)
5135 *artificial +=1;
5136 }
5137 if (additional_parms)
5138 {
5139 if (ANONYMOUS_CLASS_P (class_type)
5140 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5141 additional_parms = nreverse (additional_parms);
5142 parm = chainon (additional_parms, parm);
5143 }
5144
5145 return parm;
5146 }
5147
5148 /* Craft a constructor for CLASS_DECL -- what we should do when none
5149 where found. ARGS is non NULL when a special signature must be
5150 enforced. This is the case for anonymous classes. */
5151
5152 static void
5153 craft_constructor (class_decl, args)
5154 tree class_decl, args;
5155 {
5156 tree class_type = TREE_TYPE (class_decl);
5157 tree parm = NULL_TREE;
5158 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5159 ACC_PUBLIC : 0);
5160 int i = 0, artificial = 0;
5161 tree decl, ctor_name;
5162 char buffer [80];
5163
5164 /* The constructor name is <init> unless we're dealing with an
5165 anonymous class, in which case the name will be fixed after having
5166 be expanded. */
5167 if (ANONYMOUS_CLASS_P (class_type))
5168 ctor_name = DECL_NAME (class_decl);
5169 else
5170 ctor_name = init_identifier_node;
5171
5172 /* If we're dealing with an inner class constructor, we hide the
5173 this$<n> decl in the name field of its parameter declaration. */
5174 if (PURE_INNER_CLASS_TYPE_P (class_type))
5175 {
5176 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5177 parm = tree_cons (build_current_thisn (class_type),
5178 build_pointer_type (type), parm);
5179
5180 /* Some more arguments to be hidden here. The values of the local
5181 variables of the outer context that the inner class needs to see. */
5182 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5183 class_type, parm,
5184 &artificial);
5185 }
5186
5187 /* Then if there are any args to be enforced, enforce them now */
5188 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5189 {
5190 sprintf (buffer, "parm%d", i++);
5191 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5192 }
5193
5194 CRAFTED_PARAM_LIST_FIXUP (parm);
5195 decl = create_artificial_method (class_type, flags, void_type_node,
5196 ctor_name, parm);
5197 fix_method_argument_names (parm, decl);
5198 /* Now, mark the artificial parameters. */
5199 DECL_FUNCTION_NAP (decl) = artificial;
5200 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5201 }
5202
5203
5204 /* Fix the constructors. This will be called right after circular
5205 references have been checked. It is necessary to fix constructors
5206 early even if no code generation will take place for that class:
5207 some generated constructor might be required by the class whose
5208 compilation triggered this one to be simply loaded. */
5209
5210 void
5211 java_fix_constructors ()
5212 {
5213 tree current;
5214
5215 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5216 {
5217 tree class_type = TREE_TYPE (current);
5218 int saw_ctor = 0;
5219 tree decl;
5220
5221 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5222 continue;
5223
5224 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5225 {
5226 if (DECL_CONSTRUCTOR_P (decl))
5227 {
5228 fix_constructors (decl);
5229 saw_ctor = 1;
5230 }
5231 }
5232
5233 /* Anonymous class constructor can't be generated that early. */
5234 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5235 craft_constructor (current, NULL_TREE);
5236 }
5237 }
5238
5239 /* safe_layout_class just makes sure that we can load a class without
5240 disrupting the current_class, input_file, lineno, etc, information
5241 about the class processed currently. */
5242
5243 void
5244 safe_layout_class (class)
5245 tree class;
5246 {
5247 tree save_current_class = current_class;
5248 const char *save_input_filename = input_filename;
5249 int save_lineno = lineno;
5250
5251 layout_class (class);
5252
5253 current_class = save_current_class;
5254 input_filename = save_input_filename;
5255 lineno = save_lineno;
5256 CLASS_LOADED_P (class) = 1;
5257 }
5258
5259 static tree
5260 jdep_resolve_class (dep)
5261 jdep *dep;
5262 {
5263 tree decl;
5264
5265 if (JDEP_RESOLVED_P (dep))
5266 decl = JDEP_RESOLVED_DECL (dep);
5267 else
5268 {
5269 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5270 JDEP_DECL (dep), JDEP_WFL (dep));
5271 JDEP_RESOLVED (dep, decl);
5272 }
5273
5274 if (!decl)
5275 complete_class_report_errors (dep);
5276 else if (PURE_INNER_CLASS_DECL_P (decl))
5277 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5278 return decl;
5279 }
5280
5281 /* Complete unsatisfied class declaration and their dependencies */
5282
5283 void
5284 java_complete_class ()
5285 {
5286 tree cclass;
5287 jdeplist *cclassd;
5288 int error_found;
5289 tree type;
5290
5291 /* Process imports */
5292 process_imports ();
5293
5294 /* Rever things so we have the right order */
5295 ctxp->class_list = nreverse (ctxp->class_list);
5296 ctxp->classd_list = reverse_jdep_list (ctxp);
5297
5298 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5299 cclass && cclassd;
5300 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5301 {
5302 jdep *dep;
5303 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5304 {
5305 tree decl;
5306 if (!(decl = jdep_resolve_class (dep)))
5307 continue;
5308
5309 /* Now it's time to patch */
5310 switch (JDEP_KIND (dep))
5311 {
5312 case JDEP_SUPER:
5313 /* Simply patch super */
5314 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5315 continue;
5316 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5317 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5318 break;
5319
5320 case JDEP_FIELD:
5321 {
5322 /* We do part of the job done in add_field */
5323 tree field_decl = JDEP_DECL (dep);
5324 tree field_type = TREE_TYPE (decl);
5325 if (TREE_CODE (field_type) == RECORD_TYPE)
5326 field_type = promote_type (field_type);
5327 TREE_TYPE (field_decl) = field_type;
5328 DECL_ALIGN (field_decl) = 0;
5329 DECL_USER_ALIGN (field_decl) = 0;
5330 layout_decl (field_decl, 0);
5331 SOURCE_FRONTEND_DEBUG
5332 (("Completed field/var decl `%s' with `%s'",
5333 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5334 IDENTIFIER_POINTER (DECL_NAME (decl))));
5335 break;
5336 }
5337 case JDEP_METHOD: /* We start patching a method */
5338 case JDEP_METHOD_RETURN:
5339 error_found = 0;
5340 while (1)
5341 {
5342 if (decl)
5343 {
5344 type = TREE_TYPE(decl);
5345 if (TREE_CODE (type) == RECORD_TYPE)
5346 type = promote_type (type);
5347 JDEP_APPLY_PATCH (dep, type);
5348 SOURCE_FRONTEND_DEBUG
5349 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5350 "Completing fct `%s' with ret type `%s'":
5351 "Completing arg `%s' with type `%s'"),
5352 IDENTIFIER_POINTER (EXPR_WFL_NODE
5353 (JDEP_DECL_WFL (dep))),
5354 IDENTIFIER_POINTER (DECL_NAME (decl))));
5355 }
5356 else
5357 error_found = 1;
5358 dep = JDEP_CHAIN (dep);
5359 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5360 break;
5361 else
5362 decl = jdep_resolve_class (dep);
5363 }
5364 if (!error_found)
5365 {
5366 tree mdecl = JDEP_DECL (dep), signature;
5367 /* Recompute and reset the signature, check first that
5368 all types are now defined. If they're not,
5369 dont build the signature. */
5370 if (check_method_types_complete (mdecl))
5371 {
5372 signature = build_java_signature (TREE_TYPE (mdecl));
5373 set_java_signature (TREE_TYPE (mdecl), signature);
5374 }
5375 }
5376 else
5377 continue;
5378 break;
5379
5380 case JDEP_INTERFACE:
5381 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5382 JDEP_WFL (dep)))
5383 continue;
5384 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5385 break;
5386
5387 case JDEP_PARM:
5388 case JDEP_VARIABLE:
5389 type = TREE_TYPE(decl);
5390 if (TREE_CODE (type) == RECORD_TYPE)
5391 type = promote_type (type);
5392 JDEP_APPLY_PATCH (dep, type);
5393 break;
5394
5395 case JDEP_TYPE:
5396 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5397 SOURCE_FRONTEND_DEBUG
5398 (("Completing a random type dependency on a '%s' node",
5399 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5400 break;
5401
5402 case JDEP_EXCEPTION:
5403 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5404 SOURCE_FRONTEND_DEBUG
5405 (("Completing `%s' `throws' argument node",
5406 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5407 break;
5408
5409 case JDEP_ANONYMOUS:
5410 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5411 break;
5412
5413 default:
5414 abort ();
5415 }
5416 }
5417 }
5418 return;
5419 }
5420
5421 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5422 array. */
5423
5424 static tree
5425 resolve_class (enclosing, class_type, decl, cl)
5426 tree enclosing, class_type, decl, cl;
5427 {
5428 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5429 const char *base = name;
5430 tree resolved_type = TREE_TYPE (class_type);
5431 tree resolved_type_decl;
5432
5433 if (resolved_type != NULL_TREE)
5434 {
5435 tree resolved_type_decl = TYPE_NAME (resolved_type);
5436 if (resolved_type_decl == NULL_TREE
5437 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5438 {
5439 resolved_type_decl = build_decl (TYPE_DECL,
5440 TYPE_NAME (class_type),
5441 resolved_type);
5442 }
5443 return resolved_type_decl;
5444 }
5445
5446 /* 1- Check to see if we have an array. If true, find what we really
5447 want to resolve */
5448 while (name[0] == '[')
5449 name++;
5450 if (base != name)
5451 {
5452 TYPE_NAME (class_type) = get_identifier (name);
5453 WFL_STRIP_BRACKET (cl, cl);
5454 }
5455
5456 /* 2- Resolve the bare type */
5457 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5458 decl, cl)))
5459 return NULL_TREE;
5460 resolved_type = TREE_TYPE (resolved_type_decl);
5461
5462 /* 3- If we have and array, reconstruct the array down to its nesting */
5463 if (base != name)
5464 {
5465 while (base != name)
5466 {
5467 resolved_type = build_java_array_type (resolved_type, -1);
5468 CLASS_LOADED_P (resolved_type) = 1;
5469 name--;
5470 }
5471 /* A TYPE_NAME that is a TYPE_DECL was set in
5472 build_java_array_type, return it. */
5473 resolved_type_decl = TYPE_NAME (resolved_type);
5474 }
5475 TREE_TYPE (class_type) = resolved_type;
5476 return resolved_type_decl;
5477 }
5478
5479 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5480 are used to report error messages. */
5481
5482 tree
5483 do_resolve_class (enclosing, class_type, decl, cl)
5484 tree enclosing, class_type, decl, cl;
5485 {
5486 tree new_class_decl;
5487
5488 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5489 it is changed by find_in_imports{_on_demand} and (but it doesn't
5490 really matter) qualify_and_find */
5491
5492 /* 0- Search in the current class as an inner class */
5493
5494 /* Maybe some code here should be added to load the class or
5495 something, at least if the class isn't an inner class and ended
5496 being loaded from class file. FIXME. */
5497 while (enclosing)
5498 {
5499 tree intermediate;
5500
5501 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5502 return new_class_decl;
5503
5504 intermediate = enclosing;
5505 /* Explore enclosing contexts. */
5506 while (INNER_CLASS_DECL_P (intermediate))
5507 {
5508 intermediate = DECL_CONTEXT (intermediate);
5509 if ((new_class_decl = find_as_inner_class (intermediate,
5510 class_type, cl)))
5511 return new_class_decl;
5512 }
5513
5514 /* Now go to the upper classes, bail out if necessary. */
5515 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5516 if (!enclosing || enclosing == object_type_node)
5517 break;
5518
5519 if (TREE_CODE (enclosing) == POINTER_TYPE)
5520 enclosing = do_resolve_class (NULL, enclosing, NULL, NULL);
5521 else
5522 enclosing = TYPE_NAME (enclosing);
5523 }
5524
5525 /* 1- Check for the type in single imports. This will change
5526 TYPE_NAME() if something relevant is found */
5527 find_in_imports (class_type);
5528
5529 /* 2- And check for the type in the current compilation unit */
5530 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5531 {
5532 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5533 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5534 load_class (TYPE_NAME (class_type), 0);
5535 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5536 }
5537
5538 /* 3- Search according to the current package definition */
5539 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5540 {
5541 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5542 TYPE_NAME (class_type))))
5543 return new_class_decl;
5544 }
5545
5546 /* 4- Check the import on demands. Don't allow bar.baz to be
5547 imported from foo.* */
5548 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5549 if (find_in_imports_on_demand (class_type))
5550 return NULL_TREE;
5551
5552 /* If found in find_in_imports_on_demant, the type has already been
5553 loaded. */
5554 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5555 return new_class_decl;
5556
5557 /* 5- Try with a name qualified with the package name we've seen so far */
5558 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5559 {
5560 tree package;
5561
5562 /* If there is a current package (ctxp->package), it's the first
5563 element of package_list and we can skip it. */
5564 for (package = (ctxp->package ?
5565 TREE_CHAIN (package_list) : package_list);
5566 package; package = TREE_CHAIN (package))
5567 if ((new_class_decl = qualify_and_find (class_type,
5568 TREE_PURPOSE (package),
5569 TYPE_NAME (class_type))))
5570 return new_class_decl;
5571 }
5572
5573 /* 5- Check an other compilation unit that bears the name of type */
5574 load_class (TYPE_NAME (class_type), 0);
5575
5576 if (!cl)
5577 cl = lookup_cl (decl);
5578
5579 /* If we don't have a value for CL, then we're being called recursively.
5580 We can't check package access just yet, but it will be taken care of
5581 by the caller. */
5582 if (cl)
5583 {
5584 if (check_pkg_class_access (TYPE_NAME (class_type), cl))
5585 return NULL_TREE;
5586 }
5587
5588 /* 6- Last call for a resolution */
5589 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5590 }
5591
5592 static tree
5593 qualify_and_find (class_type, package, name)
5594 tree class_type, package, name;
5595 {
5596 tree new_qualified = merge_qualified_name (package, name);
5597 tree new_class_decl;
5598
5599 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5600 load_class (new_qualified, 0);
5601 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5602 {
5603 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5604 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5605 load_class (new_qualified, 0);
5606 TYPE_NAME (class_type) = new_qualified;
5607 return IDENTIFIER_CLASS_VALUE (new_qualified);
5608 }
5609 return NULL_TREE;
5610 }
5611
5612 /* Resolve NAME and lay it out (if not done and if not the current
5613 parsed class). Return a decl node. This function is meant to be
5614 called when type resolution is necessary during the walk pass. */
5615
5616 static tree
5617 resolve_and_layout (something, cl)
5618 tree something;
5619 tree cl;
5620 {
5621 tree decl, decl_type;
5622
5623 /* Don't do that on the current class */
5624 if (something == current_class)
5625 return TYPE_NAME (current_class);
5626
5627 /* Don't do anything for void and other primitive types */
5628 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5629 return NULL_TREE;
5630
5631 /* Pointer types can be reall pointer types or fake pointers. When
5632 finding a real pointer, recheck for primitive types */
5633 if (TREE_CODE (something) == POINTER_TYPE)
5634 {
5635 if (TREE_TYPE (something))
5636 {
5637 something = TREE_TYPE (something);
5638 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5639 return NULL_TREE;
5640 }
5641 else
5642 something = TYPE_NAME (something);
5643 }
5644
5645 /* Don't do anything for arrays of primitive types */
5646 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5647 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5648 return NULL_TREE;
5649
5650 /* Something might be a WFL */
5651 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5652 something = EXPR_WFL_NODE (something);
5653
5654 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5655 TYPE_DECL or a real TYPE */
5656 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5657 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5658 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5659
5660 if (!(decl = resolve_no_layout (something, cl)))
5661 return NULL_TREE;
5662
5663 /* Resolve and layout if necessary */
5664 decl_type = TREE_TYPE (decl);
5665 layout_class_methods (decl_type);
5666 /* Check methods */
5667 if (CLASS_FROM_SOURCE_P (decl_type))
5668 java_check_methods (decl);
5669 /* Layout the type if necessary */
5670 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5671 safe_layout_class (decl_type);
5672
5673 return decl;
5674 }
5675
5676 /* Resolve a class, returns its decl but doesn't perform any
5677 layout. The current parsing context is saved and restored */
5678
5679 static tree
5680 resolve_no_layout (name, cl)
5681 tree name, cl;
5682 {
5683 tree ptr, decl;
5684 BUILD_PTR_FROM_NAME (ptr, name);
5685 java_parser_context_save_global ();
5686 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5687 java_parser_context_restore_global ();
5688
5689 return decl;
5690 }
5691
5692 /* Called when reporting errors. Skip leader '[' in a complex array
5693 type description that failed to be resolved. */
5694
5695 static const char *
5696 purify_type_name (name)
5697 const char *name;
5698 {
5699 while (*name && *name == '[')
5700 name++;
5701 return name;
5702 }
5703
5704 /* The type CURRENT refers to can't be found. We print error messages. */
5705
5706 static void
5707 complete_class_report_errors (dep)
5708 jdep *dep;
5709 {
5710 const char *name;
5711
5712 if (!JDEP_WFL (dep))
5713 return;
5714
5715 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5716 switch (JDEP_KIND (dep))
5717 {
5718 case JDEP_SUPER:
5719 parse_error_context
5720 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5721 purify_type_name (name),
5722 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5723 break;
5724 case JDEP_FIELD:
5725 parse_error_context
5726 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5727 purify_type_name (name),
5728 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5729 break;
5730 case JDEP_METHOD: /* Covers arguments */
5731 parse_error_context
5732 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5733 purify_type_name (name),
5734 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5735 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5736 break;
5737 case JDEP_METHOD_RETURN: /* Covers return type */
5738 parse_error_context
5739 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5740 purify_type_name (name),
5741 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5742 break;
5743 case JDEP_INTERFACE:
5744 parse_error_context
5745 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5746 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5747 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5748 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5749 break;
5750 case JDEP_VARIABLE:
5751 parse_error_context
5752 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5753 purify_type_name (IDENTIFIER_POINTER
5754 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5755 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5756 break;
5757 case JDEP_EXCEPTION: /* As specified by `throws' */
5758 parse_error_context
5759 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5760 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5761 break;
5762 default:
5763 /* Fix for -Wall. Just break doing nothing. The error will be
5764 caught later */
5765 break;
5766 }
5767 }
5768
5769 /* Return a static string containing the DECL prototype string. If
5770 DECL is a constructor, use the class name instead of the form
5771 <init> */
5772
5773 static const char *
5774 get_printable_method_name (decl)
5775 tree decl;
5776 {
5777 const char *to_return;
5778 tree name = NULL_TREE;
5779
5780 if (DECL_CONSTRUCTOR_P (decl))
5781 {
5782 name = DECL_NAME (decl);
5783 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5784 }
5785
5786 to_return = lang_printable_name (decl, 0);
5787 if (DECL_CONSTRUCTOR_P (decl))
5788 DECL_NAME (decl) = name;
5789
5790 return to_return;
5791 }
5792
5793 /* Track method being redefined inside the same class. As a side
5794 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5795 function it's a FWL, so we can track errors more accurately.) */
5796
5797 static int
5798 check_method_redefinition (class, method)
5799 tree class, method;
5800 {
5801 tree redef, sig;
5802
5803 /* There's no need to verify <clinit> and finit$ */
5804 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
5805 return 0;
5806
5807 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5808 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5809 {
5810 if (redef == method)
5811 break;
5812 if (DECL_NAME (redef) == DECL_NAME (method)
5813 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5814 && !DECL_ARTIFICIAL (method))
5815 {
5816 parse_error_context
5817 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
5818 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5819 get_printable_method_name (redef));
5820 return 1;
5821 }
5822 }
5823 return 0;
5824 }
5825
5826 /* Return 1 if check went ok, 0 otherwise. */
5827 static int
5828 check_abstract_method_definitions (do_interface, class_decl, type)
5829 int do_interface;
5830 tree class_decl, type;
5831 {
5832 tree class = TREE_TYPE (class_decl);
5833 tree method, end_type;
5834 int ok = 1;
5835
5836 end_type = (do_interface ? object_type_node : type);
5837 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5838 {
5839 tree other_super, other_method, method_sig, method_name;
5840 int found = 0;
5841 int end_type_reached = 0;
5842
5843 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5844 continue;
5845
5846 /* Now verify that somewhere in between TYPE and CLASS,
5847 abstract method METHOD gets a non abstract definition
5848 that is inherited by CLASS. */
5849
5850 method_sig = build_java_signature (TREE_TYPE (method));
5851 method_name = DECL_NAME (method);
5852 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5853 method_name = EXPR_WFL_NODE (method_name);
5854
5855 other_super = class;
5856 do {
5857 if (other_super == end_type)
5858 end_type_reached = 1;
5859
5860 /* Method search */
5861 for (other_method = TYPE_METHODS (other_super); other_method;
5862 other_method = TREE_CHAIN (other_method))
5863 {
5864 tree s = build_java_signature (TREE_TYPE (other_method));
5865 tree other_name = DECL_NAME (other_method);
5866
5867 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5868 other_name = EXPR_WFL_NODE (other_name);
5869 if (!DECL_CLINIT_P (other_method)
5870 && !DECL_CONSTRUCTOR_P (other_method)
5871 && method_name == other_name
5872 && method_sig == s
5873 && !METHOD_ABSTRACT (other_method))
5874 {
5875 found = 1;
5876 break;
5877 }
5878 }
5879 other_super = CLASSTYPE_SUPER (other_super);
5880 } while (!end_type_reached);
5881
5882 /* Report that abstract METHOD didn't find an implementation
5883 that CLASS can use. */
5884 if (!found)
5885 {
5886 char *t = xstrdup (lang_printable_name
5887 (TREE_TYPE (TREE_TYPE (method)), 0));
5888 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5889
5890 parse_error_context
5891 (lookup_cl (class_decl),
5892 "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",
5893 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5894 t, lang_printable_name (method, 0),
5895 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5896 "interface" : "class"),
5897 IDENTIFIER_POINTER (ccn),
5898 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5899 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5900 ok = 0;
5901 free (t);
5902 }
5903 }
5904
5905 if (ok && do_interface)
5906 {
5907 /* Check for implemented interfaces. */
5908 int i;
5909 tree vector = TYPE_BINFO_BASETYPES (type);
5910 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5911 {
5912 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5913 ok = check_abstract_method_definitions (1, class_decl, super);
5914 }
5915 }
5916
5917 return ok;
5918 }
5919
5920 /* Check that CLASS_DECL somehow implements all inherited abstract
5921 methods. */
5922
5923 static void
5924 java_check_abstract_method_definitions (class_decl)
5925 tree class_decl;
5926 {
5927 tree class = TREE_TYPE (class_decl);
5928 tree super, vector;
5929 int i;
5930
5931 if (CLASS_ABSTRACT (class_decl))
5932 return;
5933
5934 /* Check for inherited types */
5935 super = class;
5936 do {
5937 super = CLASSTYPE_SUPER (super);
5938 check_abstract_method_definitions (0, class_decl, super);
5939 } while (super != object_type_node);
5940
5941 /* Check for implemented interfaces. */
5942 vector = TYPE_BINFO_BASETYPES (class);
5943 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5944 {
5945 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5946 check_abstract_method_definitions (1, class_decl, super);
5947 }
5948 }
5949
5950 /* Check all the types method DECL uses and return 1 if all of them
5951 are now complete, 0 otherwise. This is used to check whether its
5952 safe to build a method signature or not. */
5953
5954 static int
5955 check_method_types_complete (decl)
5956 tree decl;
5957 {
5958 tree type = TREE_TYPE (decl);
5959 tree args;
5960
5961 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5962 return 0;
5963
5964 args = TYPE_ARG_TYPES (type);
5965 if (TREE_CODE (type) == METHOD_TYPE)
5966 args = TREE_CHAIN (args);
5967 for (; args != end_params_node; args = TREE_CHAIN (args))
5968 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5969 return 0;
5970
5971 return 1;
5972 }
5973
5974 /* Visible interface to check methods contained in CLASS_DECL */
5975
5976 void
5977 java_check_methods (class_decl)
5978 tree class_decl;
5979 {
5980 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5981 return;
5982
5983 if (CLASS_INTERFACE (class_decl))
5984 java_check_abstract_methods (class_decl);
5985 else
5986 java_check_regular_methods (class_decl);
5987
5988 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
5989 }
5990
5991 /* Check all the methods of CLASS_DECL. Methods are first completed
5992 then checked according to regular method existance rules. If no
5993 constructor for CLASS_DECL were encountered, then build its
5994 declaration. */
5995
5996 static void
5997 java_check_regular_methods (class_decl)
5998 tree class_decl;
5999 {
6000 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6001 tree method;
6002 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6003 tree found = NULL_TREE;
6004 tree mthrows;
6005
6006 /* It is not necessary to check methods defined in java.lang.Object */
6007 if (class == object_type_node)
6008 return;
6009
6010 if (!TYPE_NVIRTUALS (class))
6011 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6012
6013 /* Should take interfaces into account. FIXME */
6014 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6015 {
6016 tree sig;
6017 tree method_wfl = DECL_FUNCTION_WFL (method);
6018 int aflags;
6019
6020 /* Check for redefinitions */
6021 if (check_method_redefinition (class, method))
6022 continue;
6023
6024 /* If we see one constructor a mark so we don't generate the
6025 default one. Also skip other verifications: constructors
6026 can't be inherited hence hiden or overriden */
6027 if (DECL_CONSTRUCTOR_P (method))
6028 {
6029 saw_constructor = 1;
6030 continue;
6031 }
6032
6033 /* We verify things thrown by the method. They must inherits from
6034 java.lang.Throwable */
6035 for (mthrows = DECL_FUNCTION_THROWS (method);
6036 mthrows; mthrows = TREE_CHAIN (mthrows))
6037 {
6038 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6039 parse_error_context
6040 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6041 IDENTIFIER_POINTER
6042 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6043 }
6044
6045 sig = build_java_argument_signature (TREE_TYPE (method));
6046 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6047
6048 /* Inner class can't declare static methods */
6049 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6050 {
6051 char *t = xstrdup (lang_printable_name (class, 0));
6052 parse_error_context
6053 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6054 lang_printable_name (method, 0), t);
6055 free (t);
6056 }
6057
6058 /* Nothing overrides or it's a private method. */
6059 if (!found)
6060 continue;
6061 if (METHOD_PRIVATE (found))
6062 {
6063 found = NULL_TREE;
6064 continue;
6065 }
6066
6067 /* If `found' is declared in an interface, make sure the
6068 modifier matches. */
6069 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6070 && clinit_identifier_node != DECL_NAME (found)
6071 && !METHOD_PUBLIC (method))
6072 {
6073 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6074 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6075 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6076 lang_printable_name (method, 0),
6077 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6078 }
6079
6080 /* Can't override a method with the same name and different return
6081 types. */
6082 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6083 {
6084 char *t = xstrdup
6085 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6086 parse_error_context
6087 (method_wfl,
6088 "Method `%s' was defined with return type `%s' in class `%s'",
6089 lang_printable_name (found, 0), t,
6090 IDENTIFIER_POINTER
6091 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6092 free (t);
6093 }
6094
6095 aflags = get_access_flags_from_decl (found);
6096
6097 /* Can't override final. Can't override static. */
6098 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6099 {
6100 /* Static *can* override static */
6101 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6102 continue;
6103 parse_error_context
6104 (method_wfl,
6105 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6106 (METHOD_FINAL (found) ? "Final" : "Static"),
6107 lang_printable_name (found, 0),
6108 (METHOD_FINAL (found) ? "final" : "static"),
6109 IDENTIFIER_POINTER
6110 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6111 continue;
6112 }
6113
6114 /* Static method can't override instance method. */
6115 if (METHOD_STATIC (method))
6116 {
6117 parse_error_context
6118 (method_wfl,
6119 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6120 lang_printable_name (found, 0),
6121 IDENTIFIER_POINTER
6122 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6123 continue;
6124 }
6125
6126 /* - Overriding/hiding public must be public
6127 - Overriding/hiding protected must be protected or public
6128 - If the overriden or hidden method has default (package)
6129 access, then the overriding or hiding method must not be
6130 private; otherwise, a compile-time error occurs. If
6131 `found' belongs to an interface, things have been already
6132 taken care of. */
6133 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6134 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6135 || (METHOD_PROTECTED (found)
6136 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6137 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6138 && METHOD_PRIVATE (method))))
6139 {
6140 parse_error_context
6141 (method_wfl,
6142 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6143 (METHOD_PUBLIC (method) ? "public" :
6144 (METHOD_PRIVATE (method) ? "private" : "protected")),
6145 IDENTIFIER_POINTER (DECL_NAME
6146 (TYPE_NAME (DECL_CONTEXT (found)))));
6147 continue;
6148 }
6149
6150 /* Overriding methods must have compatible `throws' clauses on checked
6151 exceptions, if any */
6152 check_throws_clauses (method, method_wfl, found);
6153
6154 /* Inheriting multiple methods with the same signature. FIXME */
6155 }
6156
6157 if (!TYPE_NVIRTUALS (class))
6158 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6159
6160 /* Search for inherited abstract method not yet implemented in this
6161 class. */
6162 java_check_abstract_method_definitions (class_decl);
6163
6164 if (!saw_constructor)
6165 abort ();
6166 }
6167
6168 /* Return a non zero value if the `throws' clause of METHOD (if any)
6169 is incompatible with the `throws' clause of FOUND (if any). */
6170
6171 static void
6172 check_throws_clauses (method, method_wfl, found)
6173 tree method, method_wfl, found;
6174 {
6175 tree mthrows, fthrows;
6176
6177 /* Can't check these things with class loaded from bytecode. FIXME */
6178 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6179 return;
6180
6181 for (mthrows = DECL_FUNCTION_THROWS (method);
6182 mthrows; mthrows = TREE_CHAIN (mthrows))
6183 {
6184 /* We don't verify unchecked expressions */
6185 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6186 continue;
6187 /* Checked expression must be compatible */
6188 for (fthrows = DECL_FUNCTION_THROWS (found);
6189 fthrows; fthrows = TREE_CHAIN (fthrows))
6190 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6191 break;
6192 if (!fthrows)
6193 {
6194 parse_error_context
6195 (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'",
6196 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6197 lang_printable_name (found, 0),
6198 IDENTIFIER_POINTER
6199 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6200 }
6201 }
6202 }
6203
6204 /* Check abstract method of interface INTERFACE */
6205
6206 static void
6207 java_check_abstract_methods (interface_decl)
6208 tree interface_decl;
6209 {
6210 int i, n;
6211 tree method, basetype_vec, found;
6212 tree interface = TREE_TYPE (interface_decl);
6213
6214 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6215 {
6216 /* 2- Check for double definition inside the defining interface */
6217 if (check_method_redefinition (interface, method))
6218 continue;
6219
6220 /* 3- Overriding is OK as far as we preserve the return type and
6221 the thrown exceptions (FIXME) */
6222 found = lookup_java_interface_method2 (interface, method);
6223 if (found)
6224 {
6225 char *t;
6226 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6227 parse_error_context
6228 (DECL_FUNCTION_WFL (found),
6229 "Method `%s' was defined with return type `%s' in class `%s'",
6230 lang_printable_name (found, 0), t,
6231 IDENTIFIER_POINTER
6232 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6233 free (t);
6234 continue;
6235 }
6236 }
6237
6238 /* 4- Inherited methods can't differ by their returned types */
6239 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6240 return;
6241 n = TREE_VEC_LENGTH (basetype_vec);
6242 for (i = 0; i < n; i++)
6243 {
6244 tree sub_interface_method, sub_interface;
6245 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6246 if (!vec_elt)
6247 continue;
6248 sub_interface = BINFO_TYPE (vec_elt);
6249 for (sub_interface_method = TYPE_METHODS (sub_interface);
6250 sub_interface_method;
6251 sub_interface_method = TREE_CHAIN (sub_interface_method))
6252 {
6253 found = lookup_java_interface_method2 (interface,
6254 sub_interface_method);
6255 if (found && (found != sub_interface_method))
6256 {
6257 parse_error_context
6258 (lookup_cl (sub_interface_method),
6259 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6260 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6261 lang_printable_name (found, 0),
6262 IDENTIFIER_POINTER
6263 (DECL_NAME (TYPE_NAME
6264 (DECL_CONTEXT (sub_interface_method)))),
6265 IDENTIFIER_POINTER
6266 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6267 }
6268 }
6269 }
6270 }
6271
6272 /* Lookup methods in interfaces using their name and partial
6273 signature. Return a matching method only if their types differ. */
6274
6275 static tree
6276 lookup_java_interface_method2 (class, method_decl)
6277 tree class, method_decl;
6278 {
6279 int i, n;
6280 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6281
6282 if (!basetype_vec)
6283 return NULL_TREE;
6284
6285 n = TREE_VEC_LENGTH (basetype_vec);
6286 for (i = 0; i < n; i++)
6287 {
6288 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6289 if ((BINFO_TYPE (vec_elt) != object_type_node)
6290 && (to_return =
6291 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6292 return to_return;
6293 }
6294 for (i = 0; i < n; i++)
6295 {
6296 to_return = lookup_java_interface_method2
6297 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6298 if (to_return)
6299 return to_return;
6300 }
6301
6302 return NULL_TREE;
6303 }
6304
6305 /* Lookup method using their name and partial signature. Return a
6306 matching method only if their types differ. */
6307
6308 static tree
6309 lookup_java_method2 (clas, method_decl, do_interface)
6310 tree clas, method_decl;
6311 int do_interface;
6312 {
6313 tree method, method_signature, method_name, method_type, name;
6314
6315 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6316 name = DECL_NAME (method_decl);
6317 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6318 EXPR_WFL_NODE (name) : name);
6319 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6320
6321 while (clas != NULL_TREE)
6322 {
6323 for (method = TYPE_METHODS (clas);
6324 method != NULL_TREE; method = TREE_CHAIN (method))
6325 {
6326 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6327 tree name = DECL_NAME (method);
6328 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6329 EXPR_WFL_NODE (name) : name) == method_name
6330 && method_sig == method_signature
6331 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6332 return method;
6333 }
6334 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6335 }
6336 return NULL_TREE;
6337 }
6338
6339 /* Return the line that matches DECL line number, and try its best to
6340 position the column number. Used during error reports. */
6341
6342 static tree
6343 lookup_cl (decl)
6344 tree decl;
6345 {
6346 static tree cl = NULL_TREE;
6347 char *line, *found;
6348
6349 if (!decl)
6350 return NULL_TREE;
6351
6352 if (cl == NULL_TREE)
6353 {
6354 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6355 ggc_add_tree_root (&cl, 1);
6356 }
6357
6358 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6359 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6360
6361 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6362 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6363
6364 found = strstr ((const char *)line,
6365 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6366 if (found)
6367 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6368
6369 return cl;
6370 }
6371
6372 /* Look for a simple name in the single-type import list */
6373
6374 static tree
6375 find_name_in_single_imports (name)
6376 tree name;
6377 {
6378 tree node;
6379
6380 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6381 if (TREE_VALUE (node) == name)
6382 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6383
6384 return NULL_TREE;
6385 }
6386
6387 /* Process all single-type import. */
6388
6389 static int
6390 process_imports ()
6391 {
6392 tree import;
6393 int error_found;
6394
6395 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6396 {
6397 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6398 char *original_name;
6399
6400 obstack_grow0 (&temporary_obstack,
6401 IDENTIFIER_POINTER (to_be_found),
6402 IDENTIFIER_LENGTH (to_be_found));
6403 original_name = obstack_finish (&temporary_obstack);
6404
6405 /* Don't load twice something already defined. */
6406 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6407 continue;
6408
6409 while (1)
6410 {
6411 tree left;
6412
6413 QUALIFIED_P (to_be_found) = 1;
6414 load_class (to_be_found, 0);
6415 error_found =
6416 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6417
6418 /* We found it, we can bail out */
6419 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6420 break;
6421
6422 /* We haven't found it. Maybe we're trying to access an
6423 inner class. The only way for us to know is to try again
6424 after having dropped a qualifier. If we can't break it further,
6425 we have an error. */
6426 if (breakdown_qualified (&left, NULL, to_be_found))
6427 break;
6428
6429 to_be_found = left;
6430 }
6431 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6432 {
6433 parse_error_context (TREE_PURPOSE (import),
6434 "Class or interface `%s' not found in import",
6435 original_name);
6436 error_found = 1;
6437 }
6438
6439 obstack_free (&temporary_obstack, original_name);
6440 if (error_found)
6441 return 1;
6442 }
6443 return 0;
6444 }
6445
6446 /* Possibly find and mark a class imported by a single-type import
6447 statement. */
6448
6449 static void
6450 find_in_imports (class_type)
6451 tree class_type;
6452 {
6453 tree import;
6454
6455 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6456 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6457 {
6458 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6459 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6460 }
6461 }
6462
6463 static int
6464 note_possible_classname (name, len)
6465 const char *name;
6466 int len;
6467 {
6468 tree node;
6469 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6470 len = len - 5;
6471 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6472 len = len - 6;
6473 else
6474 return 0;
6475 node = ident_subst (name, len, "", '/', '.', "");
6476 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6477 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6478 return 1;
6479 }
6480
6481 /* Read a import directory, gathering potential match for further type
6482 references. Indifferently reads a filesystem or a ZIP archive
6483 directory. */
6484
6485 static void
6486 read_import_dir (wfl)
6487 tree wfl;
6488 {
6489 tree package_id = EXPR_WFL_NODE (wfl);
6490 const char *package_name = IDENTIFIER_POINTER (package_id);
6491 int package_length = IDENTIFIER_LENGTH (package_id);
6492 DIR *dirp = NULL;
6493 JCF *saved_jcf = current_jcf;
6494
6495 int found = 0;
6496 int k;
6497 void *entry;
6498 struct buffer filename[1];
6499
6500
6501 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6502 return;
6503 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6504
6505 BUFFER_INIT (filename);
6506 buffer_grow (filename, package_length + 100);
6507
6508 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6509 {
6510 const char *entry_name = jcf_path_name (entry);
6511 int entry_length = strlen (entry_name);
6512 if (jcf_path_is_zipfile (entry))
6513 {
6514 ZipFile *zipf;
6515 buffer_grow (filename, entry_length);
6516 memcpy (filename->data, entry_name, entry_length - 1);
6517 filename->data[entry_length-1] = '\0';
6518 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6519 if (zipf == NULL)
6520 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6521 else
6522 {
6523 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6524 BUFFER_RESET (filename);
6525 for (k = 0; k < package_length; k++)
6526 {
6527 char ch = package_name[k];
6528 *filename->ptr++ = ch == '.' ? '/' : ch;
6529 }
6530 *filename->ptr++ = '/';
6531
6532 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6533 {
6534 const char *current_entry = ZIPDIR_FILENAME (zipd);
6535 int current_entry_len = zipd->filename_length;
6536
6537 if (current_entry_len >= BUFFER_LENGTH (filename)
6538 && strncmp (filename->data, current_entry,
6539 BUFFER_LENGTH (filename)) != 0)
6540 continue;
6541 found |= note_possible_classname (current_entry,
6542 current_entry_len);
6543 }
6544 }
6545 }
6546 else
6547 {
6548 BUFFER_RESET (filename);
6549 buffer_grow (filename, entry_length + package_length + 4);
6550 strcpy (filename->data, entry_name);
6551 filename->ptr = filename->data + entry_length;
6552 for (k = 0; k < package_length; k++)
6553 {
6554 char ch = package_name[k];
6555 *filename->ptr++ = ch == '.' ? '/' : ch;
6556 }
6557 *filename->ptr = '\0';
6558
6559 dirp = opendir (filename->data);
6560 if (dirp == NULL)
6561 continue;
6562 *filename->ptr++ = '/';
6563 for (;;)
6564 {
6565 int len;
6566 const char *d_name;
6567 struct dirent *direntp = readdir (dirp);
6568 if (!direntp)
6569 break;
6570 d_name = direntp->d_name;
6571 len = strlen (direntp->d_name);
6572 buffer_grow (filename, len+1);
6573 strcpy (filename->ptr, d_name);
6574 found |= note_possible_classname (filename->data + entry_length,
6575 package_length+len+1);
6576 }
6577 if (dirp)
6578 closedir (dirp);
6579 }
6580 }
6581
6582 free (filename->data);
6583
6584 /* Here we should have a unified way of retrieving an entry, to be
6585 indexed. */
6586 if (!found)
6587 {
6588 static int first = 1;
6589 if (first)
6590 {
6591 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6592 java_error_count++;
6593 first = 0;
6594 }
6595 else
6596 parse_error_context (wfl, "Package `%s' not found in import",
6597 package_name);
6598 current_jcf = saved_jcf;
6599 return;
6600 }
6601 current_jcf = saved_jcf;
6602 }
6603
6604 /* Possibly find a type in the import on demands specified
6605 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6606 entire list, to detected potential double definitions. */
6607
6608 static int
6609 find_in_imports_on_demand (class_type)
6610 tree class_type;
6611 {
6612 tree node, import, node_to_use = NULL_TREE;
6613 int seen_once = -1;
6614 tree cl = NULL_TREE;
6615
6616 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6617 {
6618 const char *id_name;
6619 obstack_grow (&temporary_obstack,
6620 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6621 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6622 obstack_1grow (&temporary_obstack, '.');
6623 obstack_grow0 (&temporary_obstack,
6624 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6625 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6626 id_name = obstack_finish (&temporary_obstack);
6627
6628 node = maybe_get_identifier (id_name);
6629 if (node && IS_A_CLASSFILE_NAME (node))
6630 {
6631 if (seen_once < 0)
6632 {
6633 cl = TREE_PURPOSE (import);
6634 seen_once = 1;
6635 node_to_use = node;
6636 }
6637 else
6638 {
6639 seen_once++;
6640 parse_error_context
6641 (TREE_PURPOSE (import),
6642 "Type `%s' also potentially defined in package `%s'",
6643 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6644 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6645 }
6646 }
6647 }
6648
6649 if (seen_once == 1)
6650 {
6651 /* Setup lineno so that it refers to the line of the import (in
6652 case we parse a class file and encounter errors */
6653 tree decl;
6654 int saved_lineno = lineno;
6655 lineno = EXPR_WFL_LINENO (cl);
6656 TYPE_NAME (class_type) = node_to_use;
6657 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6658 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6659 /* If there is no DECL set for the class or if the class isn't
6660 loaded and not seen in source yet, the load */
6661 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6662 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6663 {
6664 load_class (node_to_use, 0);
6665 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6666 }
6667 lineno = saved_lineno;
6668 if (! INNER_CLASS_P (TREE_TYPE (decl)))
6669 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6670 else
6671 /* 6.6.1: Inner classes are subject to member access rules. */
6672 return 0;
6673 }
6674 else
6675 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6676 }
6677
6678 /* Add package NAME to the list of package encountered so far. To
6679 speed up class lookup in do_resolve_class, we make sure a
6680 particular package is added only once. */
6681
6682 static void
6683 register_package (name)
6684 tree name;
6685 {
6686 static struct hash_table _pht, *pht = NULL;
6687
6688 if (!pht)
6689 {
6690 hash_table_init (&_pht, hash_newfunc,
6691 java_hash_hash_tree_node, java_hash_compare_tree_node);
6692 pht = &_pht;
6693 }
6694
6695 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6696 {
6697 package_list = chainon (package_list, build_tree_list (name, NULL));
6698 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6699 }
6700 }
6701
6702 static tree
6703 resolve_package (pkg, next)
6704 tree pkg, *next;
6705 {
6706 tree current, acc;
6707 tree type_name = NULL_TREE;
6708 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6709
6710 /* The trick is to determine when the package name stops and were
6711 the name of something contained in the package starts. Then we
6712 return a fully qualified name of what we want to get. */
6713
6714 /* Do a quick search on well known package names */
6715 if (!strncmp (name, "java.lang.reflect", 17))
6716 {
6717 *next =
6718 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6719 type_name = lookup_package_type (name, 17);
6720 }
6721 else if (!strncmp (name, "java.lang", 9))
6722 {
6723 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6724 type_name = lookup_package_type (name, 9);
6725 }
6726
6727 /* If we found something here, return */
6728 if (type_name)
6729 return type_name;
6730
6731 *next = EXPR_WFL_QUALIFICATION (pkg);
6732
6733 /* Try to progressively construct a type name */
6734 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6735 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6736 current; current = TREE_CHAIN (current))
6737 {
6738 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6739 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6740 {
6741 type_name = acc;
6742 /* resolve_package should be used in a loop, hence we
6743 point at this one to naturally process the next one at
6744 the next iteration. */
6745 *next = current;
6746 break;
6747 }
6748 }
6749 return type_name;
6750 }
6751
6752 static tree
6753 lookup_package_type (name, from)
6754 const char *name;
6755 int from;
6756 {
6757 char subname [128];
6758 const char *sub = &name[from+1];
6759 while (*sub != '.' && *sub)
6760 sub++;
6761 strncpy (subname, name, sub-name);
6762 subname [sub-name] = '\0';
6763 return get_identifier (subname);
6764 }
6765
6766 /* Check accessibility of inner classes according to member access rules.
6767 DECL is the inner class, ENCLOSING_DECL is the class from which the
6768 access is being attempted. */
6769
6770 static void
6771 check_inner_class_access (decl, enclosing_decl, cl)
6772 tree decl, enclosing_decl, cl;
6773 {
6774 const char *access;
6775 tree enclosing_decl_type;
6776
6777 /* We don't issue an error message when CL is null. CL can be null
6778 as a result of processing a JDEP crafted by source_start_java_method
6779 for the purpose of patching its parm decl. But the error would
6780 have been already trapped when fixing the method's signature.
6781 DECL can also be NULL in case of earlier errors. */
6782 if (!decl || !cl)
6783 return;
6784
6785 enclosing_decl_type = TREE_TYPE (enclosing_decl);
6786
6787 if (CLASS_PRIVATE (decl))
6788 {
6789 /* Access is permitted only within the body of the top-level
6790 class in which DECL is declared. */
6791 tree top_level = decl;
6792 while (DECL_CONTEXT (top_level))
6793 top_level = DECL_CONTEXT (top_level);
6794 while (DECL_CONTEXT (enclosing_decl))
6795 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6796 if (top_level == enclosing_decl)
6797 return;
6798 access = "private";
6799 }
6800 else if (CLASS_PROTECTED (decl))
6801 {
6802 tree decl_context;
6803 /* Access is permitted from within the same package... */
6804 if (in_same_package (decl, enclosing_decl))
6805 return;
6806
6807 /* ... or from within the body of a subtype of the context in which
6808 DECL is declared. */
6809 decl_context = DECL_CONTEXT (decl);
6810 while (enclosing_decl)
6811 {
6812 if (CLASS_INTERFACE (decl))
6813 {
6814 if (interface_of_p (TREE_TYPE (decl_context),
6815 enclosing_decl_type))
6816 return;
6817 }
6818 else
6819 {
6820 /* Eww. The order of the arguments is different!! */
6821 if (inherits_from_p (enclosing_decl_type,
6822 TREE_TYPE (decl_context)))
6823 return;
6824 }
6825 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6826 }
6827 access = "protected";
6828 }
6829 else if (! CLASS_PUBLIC (decl))
6830 {
6831 /* Access is permitted only from within the same package as DECL. */
6832 if (in_same_package (decl, enclosing_decl))
6833 return;
6834 access = "non-public";
6835 }
6836 else
6837 /* Class is public. */
6838 return;
6839
6840 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
6841 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6842 lang_printable_name (decl, 0), access);
6843 }
6844
6845 /* Accessibility check for top-level classes. If CLASS_NAME is in a foreign
6846 package, it must be PUBLIC. Return 0 if no access violations were found,
6847 1 otherwise. */
6848
6849 static int
6850 check_pkg_class_access (class_name, cl)
6851 tree class_name;
6852 tree cl;
6853 {
6854 tree type;
6855
6856 if (!IDENTIFIER_CLASS_VALUE (class_name))
6857 return 0;
6858
6859 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6860 return 0;
6861
6862 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6863 {
6864 /* Access to a private class within the same package is
6865 allowed. */
6866 tree l, r;
6867 breakdown_qualified (&l, &r, class_name);
6868 if (!QUALIFIED_P (class_name) && !ctxp->package)
6869 /* Both in the empty package. */
6870 return 0;
6871 if (l == ctxp->package)
6872 /* Both in the same package. */
6873 return 0;
6874
6875 parse_error_context
6876 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6877 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6878 IDENTIFIER_POINTER (class_name));
6879 return 1;
6880 }
6881 return 0;
6882 }
6883
6884 /* Local variable declaration. */
6885
6886 static void
6887 declare_local_variables (modifier, type, vlist)
6888 int modifier;
6889 tree type;
6890 tree vlist;
6891 {
6892 tree decl, current, saved_type;
6893 tree type_wfl = NULL_TREE;
6894 int must_chain = 0;
6895 int final_p = 0;
6896
6897 /* Push a new block if statements were seen between the last time we
6898 pushed a block and now. Keep a count of blocks to close */
6899 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6900 {
6901 tree b = enter_block ();
6902 BLOCK_IS_IMPLICIT (b) = 1;
6903 }
6904
6905 if (modifier)
6906 {
6907 int i;
6908 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6909 if (modifier == ACC_FINAL)
6910 final_p = 1;
6911 else
6912 {
6913 parse_error_context
6914 (ctxp->modifier_ctx [i],
6915 "Only `final' is allowed as a local variables modifier");
6916 return;
6917 }
6918 }
6919
6920 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6921 hold the TYPE value if a new incomplete has to be created (as
6922 opposed to being found already existing and reused). */
6923 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6924
6925 /* If TYPE is fully resolved and we don't have a reference, make one */
6926 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6927
6928 /* Go through all the declared variables */
6929 for (current = vlist, saved_type = type; current;
6930 current = TREE_CHAIN (current), type = saved_type)
6931 {
6932 tree other, real_type;
6933 tree wfl = TREE_PURPOSE (current);
6934 tree name = EXPR_WFL_NODE (wfl);
6935 tree init = TREE_VALUE (current);
6936
6937 /* Process NAME, as it may specify extra dimension(s) for it */
6938 type = build_array_from_name (type, type_wfl, name, &name);
6939
6940 /* Variable redefinition check */
6941 if ((other = lookup_name_in_blocks (name)))
6942 {
6943 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6944 DECL_SOURCE_LINE (other));
6945 continue;
6946 }
6947
6948 /* Type adjustment. We may have just readjusted TYPE because
6949 the variable specified more dimensions. Make sure we have
6950 a reference if we can and don't have one already. */
6951 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6952
6953 real_type = GET_REAL_TYPE (type);
6954 /* Never layout this decl. This will be done when its scope
6955 will be entered */
6956 decl = build_decl (VAR_DECL, name, real_type);
6957 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
6958 LOCAL_FINAL (decl) = final_p;
6959 BLOCK_CHAIN_DECL (decl);
6960
6961 /* If doing xreferencing, replace the line number with the WFL
6962 compound value */
6963 if (flag_emit_xref)
6964 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6965
6966 /* Don't try to use an INIT statement when an error was found */
6967 if (init && java_error_count)
6968 init = NULL_TREE;
6969
6970 /* Add the initialization function to the current function's code */
6971 if (init)
6972 {
6973 /* Name might have been readjusted */
6974 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6975 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6976 java_method_add_stmt (current_function_decl,
6977 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6978 init));
6979 }
6980
6981 /* Setup dependency the type of the decl */
6982 if (must_chain)
6983 {
6984 jdep *dep;
6985 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6986 dep = CLASSD_LAST (ctxp->classd_list);
6987 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
6988 }
6989 }
6990 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6991 }
6992
6993 /* Called during parsing. Build decls from argument list. */
6994
6995 static void
6996 source_start_java_method (fndecl)
6997 tree fndecl;
6998 {
6999 tree tem;
7000 tree parm_decl;
7001 int i;
7002
7003 if (!fndecl)
7004 return;
7005
7006 current_function_decl = fndecl;
7007
7008 /* New scope for the function */
7009 enter_block ();
7010 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7011 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7012 {
7013 tree type = TREE_VALUE (tem);
7014 tree name = TREE_PURPOSE (tem);
7015
7016 /* If type is incomplete. Create an incomplete decl and ask for
7017 the decl to be patched later */
7018 if (INCOMPLETE_TYPE_P (type))
7019 {
7020 jdep *jdep;
7021 tree real_type = GET_REAL_TYPE (type);
7022 parm_decl = build_decl (PARM_DECL, name, real_type);
7023 type = obtain_incomplete_type (type);
7024 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7025 jdep = CLASSD_LAST (ctxp->classd_list);
7026 JDEP_MISC (jdep) = name;
7027 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7028 }
7029 else
7030 parm_decl = build_decl (PARM_DECL, name, type);
7031
7032 /* Remember if a local variable was declared final (via its
7033 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7034 if (ARG_FINAL_P (tem))
7035 {
7036 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7037 LOCAL_FINAL (parm_decl) = 1;
7038 }
7039
7040 BLOCK_CHAIN_DECL (parm_decl);
7041 }
7042 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7043 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7044 nreverse (tem);
7045 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7046 DECL_MAX_LOCALS (current_function_decl) = i;
7047 }
7048
7049 /* Called during parsing. Creates an artificial method declaration. */
7050
7051 static tree
7052 create_artificial_method (class, flags, type, name, args)
7053 tree class;
7054 int flags;
7055 tree type, name, args;
7056 {
7057 tree mdecl;
7058
7059 java_parser_context_save_global ();
7060 lineno = 0;
7061 mdecl = make_node (FUNCTION_TYPE);
7062 TREE_TYPE (mdecl) = type;
7063 TYPE_ARG_TYPES (mdecl) = args;
7064 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7065 java_parser_context_restore_global ();
7066 DECL_ARTIFICIAL (mdecl) = 1;
7067 return mdecl;
7068 }
7069
7070 /* Starts the body if an artifical method. */
7071
7072 static void
7073 start_artificial_method_body (mdecl)
7074 tree mdecl;
7075 {
7076 DECL_SOURCE_LINE (mdecl) = 1;
7077 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7078 source_start_java_method (mdecl);
7079 enter_block ();
7080 }
7081
7082 static void
7083 end_artificial_method_body (mdecl)
7084 tree mdecl;
7085 {
7086 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7087 It has to be evaluated first. (if mdecl is current_function_decl,
7088 we have an undefined behavior if no temporary variable is used.) */
7089 tree b = exit_block ();
7090 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7091 exit_block ();
7092 }
7093
7094 /* Terminate a function and expand its body. */
7095
7096 static void
7097 source_end_java_method ()
7098 {
7099 tree fndecl = current_function_decl;
7100 int flag_asynchronous_exceptions = asynchronous_exceptions;
7101
7102 if (!fndecl)
7103 return;
7104
7105 java_parser_context_save_global ();
7106 lineno = ctxp->last_ccb_indent1;
7107
7108 /* Set EH language codes */
7109 java_set_exception_lang_code ();
7110
7111 /* Turn function bodies with only a NOP expr null, so they don't get
7112 generated at all and we won't get warnings when using the -W
7113 -Wall flags. */
7114 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7115 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7116
7117 /* Generate function's code */
7118 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7119 && ! flag_emit_class_files
7120 && ! flag_emit_xref)
7121 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7122
7123 /* pop out of its parameters */
7124 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7125 poplevel (1, 0, 1);
7126 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7127
7128 /* Generate rtl for function exit. */
7129 if (! flag_emit_class_files && ! flag_emit_xref)
7130 {
7131 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7132 /* Emit catch-finally clauses */
7133 emit_handlers ();
7134 expand_function_end (input_filename, lineno, 0);
7135
7136 /* FIXME: If the current method contains any exception handlers,
7137 force asynchronous_exceptions: this is necessary because signal
7138 handlers in libjava may throw exceptions. This is far from being
7139 a perfect solution, but it's better than doing nothing at all.*/
7140 if (catch_clauses)
7141 asynchronous_exceptions = 1;
7142
7143 /* Run the optimizers and output assembler code for this function. */
7144 rest_of_compilation (fndecl);
7145 }
7146
7147 current_function_decl = NULL_TREE;
7148 java_parser_context_restore_global ();
7149 asynchronous_exceptions = flag_asynchronous_exceptions;
7150 }
7151
7152 /* Record EXPR in the current function block. Complements compound
7153 expression second operand if necessary. */
7154
7155 tree
7156 java_method_add_stmt (fndecl, expr)
7157 tree fndecl, expr;
7158 {
7159 if (!GET_CURRENT_BLOCK (fndecl))
7160 return NULL_TREE;
7161 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7162 }
7163
7164 static tree
7165 add_stmt_to_block (b, type, stmt)
7166 tree b, type, stmt;
7167 {
7168 tree body = BLOCK_EXPR_BODY (b), c;
7169
7170 if (java_error_count)
7171 return body;
7172
7173 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7174 return body;
7175
7176 BLOCK_EXPR_BODY (b) = c;
7177 TREE_SIDE_EFFECTS (c) = 1;
7178 return c;
7179 }
7180
7181 /* Add STMT to EXISTING if possible, otherwise create a new
7182 COMPOUND_EXPR and add STMT to it. */
7183
7184 static tree
7185 add_stmt_to_compound (existing, type, stmt)
7186 tree existing, type, stmt;
7187 {
7188 if (existing)
7189 return build (COMPOUND_EXPR, type, existing, stmt);
7190 else
7191 return stmt;
7192 }
7193
7194 void java_layout_seen_class_methods ()
7195 {
7196 tree previous_list = all_class_list;
7197 tree end = NULL_TREE;
7198 tree current;
7199
7200 while (1)
7201 {
7202 for (current = previous_list;
7203 current != end; current = TREE_CHAIN (current))
7204 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7205
7206 if (previous_list != all_class_list)
7207 {
7208 end = previous_list;
7209 previous_list = all_class_list;
7210 }
7211 else
7212 break;
7213 }
7214 }
7215
7216 void
7217 java_reorder_fields ()
7218 {
7219 static tree stop_reordering = NULL_TREE;
7220 static int initialized_p;
7221 tree current;
7222
7223 /* Register STOP_REORDERING with the garbage collector. */
7224 if (!initialized_p)
7225 {
7226 ggc_add_tree_root (&stop_reordering, 1);
7227 initialized_p = 1;
7228 }
7229
7230 for (current = gclass_list; current; current = TREE_CHAIN (current))
7231 {
7232 current_class = TREE_TYPE (TREE_VALUE (current));
7233
7234 if (current_class == stop_reordering)
7235 break;
7236
7237 /* Reverse the fields, but leave the dummy field in front.
7238 Fields are already ordered for Object and Class */
7239 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7240 && current_class != class_type_node)
7241 {
7242 /* If the dummy field is there, reverse the right fields and
7243 just layout the type for proper fields offset */
7244 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7245 {
7246 tree fields = TYPE_FIELDS (current_class);
7247 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7248 TYPE_SIZE (current_class) = NULL_TREE;
7249 }
7250 /* We don't have a dummy field, we need to layout the class,
7251 after having reversed the fields */
7252 else
7253 {
7254 TYPE_FIELDS (current_class) =
7255 nreverse (TYPE_FIELDS (current_class));
7256 TYPE_SIZE (current_class) = NULL_TREE;
7257 }
7258 }
7259 }
7260 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7261 }
7262
7263 /* Layout the methods of all classes loaded in one way or another.
7264 Check methods of source parsed classes. Then reorder the
7265 fields and layout the classes or the type of all source parsed
7266 classes */
7267
7268 void
7269 java_layout_classes ()
7270 {
7271 tree current;
7272 int save_error_count = java_error_count;
7273
7274 /* Layout the methods of all classes seen so far */
7275 java_layout_seen_class_methods ();
7276 java_parse_abort_on_error ();
7277 all_class_list = NULL_TREE;
7278
7279 /* Then check the methods of all parsed classes */
7280 for (current = gclass_list; current; current = TREE_CHAIN (current))
7281 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7282 java_check_methods (TREE_VALUE (current));
7283 java_parse_abort_on_error ();
7284
7285 for (current = gclass_list; current; current = TREE_CHAIN (current))
7286 {
7287 current_class = TREE_TYPE (TREE_VALUE (current));
7288 layout_class (current_class);
7289
7290 /* From now on, the class is considered completely loaded */
7291 CLASS_LOADED_P (current_class) = 1;
7292
7293 /* Error reported by the caller */
7294 if (java_error_count)
7295 return;
7296 }
7297
7298 /* We might have reloaded classes durign the process of laying out
7299 classes for code generation. We must layout the methods of those
7300 late additions, as constructor checks might use them */
7301 java_layout_seen_class_methods ();
7302 java_parse_abort_on_error ();
7303 }
7304
7305 /* Expand methods in the current set of classes rememebered for
7306 generation. */
7307
7308 static void
7309 java_complete_expand_classes ()
7310 {
7311 tree current;
7312
7313 do_not_fold = flag_emit_xref;
7314
7315 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7316 if (!INNER_CLASS_DECL_P (current))
7317 java_complete_expand_class (current);
7318 }
7319
7320 /* Expand the methods found in OUTER, starting first by OUTER's inner
7321 classes, if any. */
7322
7323 static void
7324 java_complete_expand_class (outer)
7325 tree outer;
7326 {
7327 tree inner_list;
7328
7329 set_nested_class_simple_name_value (outer, 1); /* Set */
7330
7331 /* We need to go after all inner classes and start expanding them,
7332 starting with most nested ones. We have to do that because nested
7333 classes might add functions to outer classes */
7334
7335 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7336 inner_list; inner_list = TREE_CHAIN (inner_list))
7337 java_complete_expand_class (TREE_PURPOSE (inner_list));
7338
7339 java_complete_expand_methods (outer);
7340 set_nested_class_simple_name_value (outer, 0); /* Reset */
7341 }
7342
7343 /* Expand methods registered in CLASS_DECL. The general idea is that
7344 we expand regular methods first. This allows us get an estimate on
7345 how outer context local alias fields are really used so we can add
7346 to the constructor just enough code to initialize them properly (it
7347 also lets us generate finit$ correctly.) Then we expand the
7348 constructors and then <clinit>. */
7349
7350 static void
7351 java_complete_expand_methods (class_decl)
7352 tree class_decl;
7353 {
7354 tree clinit, finit, decl, first_decl;
7355
7356 current_class = TREE_TYPE (class_decl);
7357
7358 /* Find whether the class has final variables */
7359 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7360 if (FIELD_FINAL (decl))
7361 {
7362 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7363 break;
7364 }
7365
7366 /* Initialize a new constant pool */
7367 init_outgoing_cpool ();
7368
7369 /* Pre-expand <clinit> to figure whether we really need it or
7370 not. If we do need it, we pre-expand the static fields so they're
7371 ready to be used somewhere else. <clinit> will be fully expanded
7372 after we processed the constructors. */
7373 first_decl = TYPE_METHODS (current_class);
7374 clinit = maybe_generate_pre_expand_clinit (current_class);
7375
7376 /* Then generate finit$ (if we need to) because constructor will
7377 try to use it.*/
7378 if (TYPE_FINIT_STMT_LIST (current_class))
7379 {
7380 finit = generate_finit (current_class);
7381 java_complete_expand_method (finit);
7382 }
7383
7384 /* Now do the constructors */
7385 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7386 {
7387 int no_body;
7388
7389 if (!DECL_CONSTRUCTOR_P (decl))
7390 continue;
7391
7392 no_body = !DECL_FUNCTION_BODY (decl);
7393 /* Don't generate debug info on line zero when expanding a
7394 generated constructor. */
7395 if (no_body)
7396 restore_line_number_status (1);
7397
7398 /* Reset the final local variable assignment flags */
7399 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7400 reset_final_variable_local_assignment_flag (current_class);
7401
7402 java_complete_expand_method (decl);
7403
7404 /* Check for missed out final variable assignment */
7405 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7406 check_final_variable_local_assignment_flag (current_class, decl);
7407
7408 if (no_body)
7409 restore_line_number_status (0);
7410 }
7411
7412 /* First, do the ordinary methods. */
7413 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7414 {
7415 /* Skip abstract or native methods -- but do handle native
7416 methods when generating JNI stubs. */
7417 if (METHOD_ABSTRACT (decl)
7418 || (! flag_jni && METHOD_NATIVE (decl))
7419 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7420 continue;
7421
7422 if (METHOD_NATIVE (decl))
7423 {
7424 tree body = build_jni_stub (decl);
7425 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7426 }
7427
7428 java_complete_expand_method (decl);
7429 }
7430
7431 /* If there is indeed a <clinit>, fully expand it now */
7432 if (clinit)
7433 {
7434 /* Reset the final local variable assignment flags */
7435 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7436 reset_static_final_variable_assignment_flag (current_class);
7437 /* Prevent the use of `this' inside <clinit> */
7438 ctxp->explicit_constructor_p = 1;
7439 java_complete_expand_method (clinit);
7440 ctxp->explicit_constructor_p = 0;
7441 /* Check for missed out static final variable assignment */
7442 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7443 && !CLASS_INTERFACE (class_decl))
7444 check_static_final_variable_assignment_flag (current_class);
7445 }
7446
7447 /* We might have generated a class$ that we now want to expand */
7448 if (TYPE_DOT_CLASS (current_class))
7449 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7450
7451 /* Now verify constructor circularity (stop after the first one we
7452 prove wrong.) */
7453 if (!CLASS_INTERFACE (class_decl))
7454 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7455 if (DECL_CONSTRUCTOR_P (decl)
7456 && verify_constructor_circularity (decl, decl))
7457 break;
7458
7459 /* Final check on the initialization of final variables. */
7460 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7461 {
7462 check_final_variable_global_assignment_flag (current_class);
7463 /* If we have an interface, check for uninitialized fields. */
7464 if (CLASS_INTERFACE (class_decl))
7465 check_static_final_variable_assignment_flag (current_class);
7466 }
7467
7468 /* Save the constant pool. We'll need to restore it later. */
7469 TYPE_CPOOL (current_class) = outgoing_cpool;
7470 }
7471
7472 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7473 safely used in some other methods/constructors. */
7474
7475 static tree
7476 maybe_generate_pre_expand_clinit (class_type)
7477 tree class_type;
7478 {
7479 tree current, mdecl;
7480
7481 if (!TYPE_CLINIT_STMT_LIST (class_type))
7482 return NULL_TREE;
7483
7484 /* Go through all static fields and pre expand them */
7485 for (current = TYPE_FIELDS (class_type); current;
7486 current = TREE_CHAIN (current))
7487 if (FIELD_STATIC (current))
7488 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7489
7490 /* Then build the <clinit> method */
7491 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7492 clinit_identifier_node, end_params_node);
7493 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7494 mdecl, NULL_TREE);
7495 start_artificial_method_body (mdecl);
7496
7497 /* We process the list of assignment we produced as the result of
7498 the declaration of initialized static field and add them as
7499 statement to the <clinit> method. */
7500 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7501 current = TREE_CHAIN (current))
7502 {
7503 tree stmt = current;
7504 /* We build the assignment expression that will initialize the
7505 field to its value. There are strict rules on static
7506 initializers (8.5). FIXME */
7507 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7508 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7509 java_method_add_stmt (mdecl, stmt);
7510 }
7511
7512 end_artificial_method_body (mdecl);
7513
7514 /* Now we want to place <clinit> as the last method (because we need
7515 it at least for interface so that it doesn't interfere with the
7516 dispatch table based lookup. */
7517 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7518 {
7519 current = TREE_CHAIN (TYPE_METHODS (class_type));
7520 TYPE_METHODS (class_type) = current;
7521
7522 while (TREE_CHAIN (current))
7523 current = TREE_CHAIN (current);
7524
7525 TREE_CHAIN (current) = mdecl;
7526 TREE_CHAIN (mdecl) = NULL_TREE;
7527 }
7528
7529 return mdecl;
7530 }
7531
7532 /* Analyzes a method body and look for something that isn't a
7533 MODIFY_EXPR. */
7534
7535 static int
7536 analyze_clinit_body (bbody)
7537 tree bbody;
7538 {
7539 while (bbody)
7540 switch (TREE_CODE (bbody))
7541 {
7542 case BLOCK:
7543 bbody = BLOCK_EXPR_BODY (bbody);
7544 break;
7545
7546 case EXPR_WITH_FILE_LOCATION:
7547 bbody = EXPR_WFL_NODE (bbody);
7548 break;
7549
7550 case COMPOUND_EXPR:
7551 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7552 return 1;
7553 bbody = TREE_OPERAND (bbody, 1);
7554 break;
7555
7556 case MODIFY_EXPR:
7557 bbody = NULL_TREE;
7558 break;
7559
7560 default:
7561 bbody = NULL_TREE;
7562 return 1;
7563 }
7564 return 0;
7565 }
7566
7567
7568 /* See whether we could get rid of <clinit>. Criteria are: all static
7569 final fields have constant initial values and the body of <clinit>
7570 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7571
7572 static int
7573 maybe_yank_clinit (mdecl)
7574 tree mdecl;
7575 {
7576 tree type, current;
7577 tree fbody, bbody;
7578 int found = 0;
7579
7580 if (!DECL_CLINIT_P (mdecl))
7581 return 0;
7582
7583 /* If the body isn't empty, then we keep <clinit>. Note that if
7584 we're emitting classfiles, this isn't enough not to rule it
7585 out. */
7586 fbody = DECL_FUNCTION_BODY (mdecl);
7587 bbody = BLOCK_EXPR_BODY (fbody);
7588 if (bbody && bbody != error_mark_node)
7589 bbody = BLOCK_EXPR_BODY (bbody);
7590 else
7591 return 0;
7592 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7593 return 0;
7594
7595 type = DECL_CONTEXT (mdecl);
7596 current = TYPE_FIELDS (type);
7597
7598 for (current = (current ? TREE_CHAIN (current) : current);
7599 current; current = TREE_CHAIN (current))
7600 {
7601 tree f_init;
7602
7603 /* We're not interested in non static field */
7604 if (!FIELD_STATIC (current))
7605 continue;
7606
7607 /* Anything that isn't String or a basic type is ruled out -- or
7608 if we know how to deal with it (when doing things natively) we
7609 should generated an empty <clinit> so that SUID are computed
7610 correctly. */
7611 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7612 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7613 break;
7614
7615 f_init = DECL_INITIAL (current);
7616 /* If we're emitting native code, we want static final fields to
7617 have constant initializers. If we don't meet these
7618 conditions, we keep <clinit> */
7619 if (!flag_emit_class_files
7620 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7621 break;
7622 /* If we're emitting bytecode, we want static fields to have
7623 constant initializers or no initializer. If we don't meet
7624 these conditions, we keep <clinit> */
7625 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7626 break;
7627 }
7628
7629 /* Now we analyze the method body and look for something that
7630 isn't a MODIFY_EXPR */
7631 if (bbody == empty_stmt_node)
7632 found = 0;
7633 else
7634 found = analyze_clinit_body (bbody);
7635
7636 if (current || found)
7637 return 0;
7638
7639 /* Get rid of <clinit> in the class' list of methods */
7640 if (TYPE_METHODS (type) == mdecl)
7641 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7642 else
7643 for (current = TYPE_METHODS (type); current;
7644 current = TREE_CHAIN (current))
7645 if (TREE_CHAIN (current) == mdecl)
7646 {
7647 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7648 break;
7649 }
7650
7651 return 1;
7652 }
7653
7654
7655 /* Complete and expand a method. */
7656
7657 static void
7658 java_complete_expand_method (mdecl)
7659 tree mdecl;
7660 {
7661 int yank_clinit = 0;
7662
7663 current_function_decl = mdecl;
7664 /* Fix constructors before expanding them */
7665 if (DECL_CONSTRUCTOR_P (mdecl))
7666 fix_constructors (mdecl);
7667
7668 /* Expand functions that have a body */
7669 if (DECL_FUNCTION_BODY (mdecl))
7670 {
7671 tree fbody = DECL_FUNCTION_BODY (mdecl);
7672 tree block_body = BLOCK_EXPR_BODY (fbody);
7673 tree exception_copy = NULL_TREE;
7674 tree tem, *ptr;
7675
7676 current_function_decl = mdecl;
7677
7678 if (! quiet_flag)
7679 fprintf (stderr, " [%s.",
7680 lang_printable_name (DECL_CONTEXT (mdecl), 0));
7681 announce_function (mdecl);
7682 if (! quiet_flag)
7683 fprintf (stderr, "]");
7684
7685 pushlevel (1); /* Prepare for a parameter push */
7686 ptr = &DECL_ARGUMENTS (mdecl);
7687 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7688 while (tem)
7689 {
7690 tree next = TREE_CHAIN (tem);
7691 tree type = TREE_TYPE (tem);
7692 if (PROMOTE_PROTOTYPES
7693 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7694 && INTEGRAL_TYPE_P (type))
7695 type = integer_type_node;
7696 DECL_ARG_TYPE (tem) = type;
7697 layout_decl (tem, 0);
7698 pushdecl (tem);
7699 *ptr = tem;
7700 ptr = &TREE_CHAIN (tem);
7701 tem = next;
7702 }
7703 *ptr = NULL_TREE;
7704 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7705 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7706
7707 build_result_decl (mdecl);
7708
7709 current_this
7710 = (!METHOD_STATIC (mdecl) ?
7711 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7712
7713 /* Purge the `throws' list of unchecked exceptions. If we're
7714 doing xref, save a copy of the list and re-install it
7715 later. */
7716 if (flag_emit_xref)
7717 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7718
7719 purge_unchecked_exceptions (mdecl);
7720
7721 /* Install exceptions thrown with `throws' */
7722 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7723
7724 if (block_body != NULL_TREE)
7725 {
7726 block_body = java_complete_tree (block_body);
7727
7728 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7729 check_for_initialization (block_body);
7730 ctxp->explicit_constructor_p = 0;
7731 }
7732
7733 BLOCK_EXPR_BODY (fbody) = block_body;
7734
7735 /* If we saw a return but couldn't evaluate it properly, we'll
7736 have an error_mark_node here. */
7737 if (block_body != error_mark_node
7738 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7739 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7740 && !flag_emit_xref)
7741 missing_return_error (current_function_decl);
7742
7743 /* Check wether we could just get rid of clinit, now the picture
7744 is complete. */
7745 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7746 complete_start_java_method (mdecl);
7747
7748 /* Don't go any further if we've found error(s) during the
7749 expansion */
7750 if (!java_error_count && !yank_clinit)
7751 source_end_java_method ();
7752 else
7753 {
7754 if (java_error_count)
7755 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7756 poplevel (1, 0, 1);
7757 }
7758
7759 /* Pop the exceptions and sanity check */
7760 POP_EXCEPTIONS();
7761 if (currently_caught_type_list)
7762 abort ();
7763
7764 if (flag_emit_xref)
7765 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7766 }
7767 }
7768
7769 \f
7770
7771 /* This section of the code deals with accessing enclosing context
7772 fields either directly by using the relevant access to this$<n> or
7773 by invoking an access method crafted for that purpose. */
7774
7775 /* Build the necessary access from an inner class to an outer
7776 class. This routine could be optimized to cache previous result
7777 (decl, current_class and returned access). When an access method
7778 needs to be generated, it always takes the form of a read. It might
7779 be later turned into a write by calling outer_field_access_fix. */
7780
7781 static tree
7782 build_outer_field_access (id, decl)
7783 tree id, decl;
7784 {
7785 tree access = NULL_TREE;
7786 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7787 tree decl_ctx = DECL_CONTEXT (decl);
7788
7789 /* If the immediate enclosing context of the current class is the
7790 field decl's class or inherits from it; build the access as
7791 `this$<n>.<field>'. Note that we will break the `private' barrier
7792 if we're not emitting bytecodes. */
7793 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
7794 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7795 {
7796 tree thisn = build_current_thisn (current_class);
7797 access = make_qualified_primary (build_wfl_node (thisn),
7798 id, EXPR_WFL_LINECOL (id));
7799 }
7800 /* Otherwise, generate access methods to outer this and access the
7801 field (either using an access method or by direct access.) */
7802 else
7803 {
7804 int lc = EXPR_WFL_LINECOL (id);
7805
7806 /* Now we chain the required number of calls to the access$0 to
7807 get a hold to the enclosing instance we need, and then we
7808 build the field access. */
7809 access = build_access_to_thisn (current_class, decl_ctx, lc);
7810
7811 /* If the field is private and we're generating bytecode, then
7812 we generate an access method */
7813 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7814 {
7815 tree name = build_outer_field_access_methods (decl);
7816 access = build_outer_field_access_expr (lc, decl_ctx,
7817 name, access, NULL_TREE);
7818 }
7819 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7820 Once again we break the `private' access rule from a foreign
7821 class. */
7822 else
7823 access = make_qualified_primary (access, id, lc);
7824 }
7825 return resolve_expression_name (access, NULL);
7826 }
7827
7828 /* Return a non zero value if NODE describes an outer field inner
7829 access. */
7830
7831 static int
7832 outer_field_access_p (type, decl)
7833 tree type, decl;
7834 {
7835 if (!INNER_CLASS_TYPE_P (type)
7836 || TREE_CODE (decl) != FIELD_DECL
7837 || DECL_CONTEXT (decl) == type)
7838 return 0;
7839
7840 /* If the inner class extends the declaration context of the field
7841 we're try to acces, then this isn't an outer field access */
7842 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7843 return 0;
7844
7845 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7846 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7847 {
7848 if (type == DECL_CONTEXT (decl))
7849 return 1;
7850
7851 if (!DECL_CONTEXT (TYPE_NAME (type)))
7852 {
7853 /* Before we give up, see whether the field is inherited from
7854 the enclosing context we're considering. */
7855 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7856 return 1;
7857 break;
7858 }
7859 }
7860
7861 return 0;
7862 }
7863
7864 /* Return a non zero value if NODE represents an outer field inner
7865 access that was been already expanded. As a side effect, it returns
7866 the name of the field being accessed and the argument passed to the
7867 access function, suitable for a regeneration of the access method
7868 call if necessary. */
7869
7870 static int
7871 outer_field_expanded_access_p (node, name, arg_type, arg)
7872 tree node, *name, *arg_type, *arg;
7873 {
7874 int identified = 0;
7875
7876 if (TREE_CODE (node) != CALL_EXPR)
7877 return 0;
7878
7879 /* Well, gcj generates slightly different tree nodes when compiling
7880 to native or bytecodes. It's the case for function calls. */
7881
7882 if (flag_emit_class_files
7883 && TREE_CODE (node) == CALL_EXPR
7884 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7885 identified = 1;
7886 else if (!flag_emit_class_files)
7887 {
7888 node = TREE_OPERAND (node, 0);
7889
7890 if (node && TREE_OPERAND (node, 0)
7891 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7892 {
7893 node = TREE_OPERAND (node, 0);
7894 if (TREE_OPERAND (node, 0)
7895 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7896 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7897 (DECL_NAME (TREE_OPERAND (node, 0)))))
7898 identified = 1;
7899 }
7900 }
7901
7902 if (identified && name && arg_type && arg)
7903 {
7904 tree argument = TREE_OPERAND (node, 1);
7905 *name = DECL_NAME (TREE_OPERAND (node, 0));
7906 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7907 *arg = TREE_VALUE (argument);
7908 }
7909 return identified;
7910 }
7911
7912 /* Detect in NODE an outer field read access from an inner class and
7913 transform it into a write with RHS as an argument. This function is
7914 called from the java_complete_lhs when an assignment to a LHS can
7915 be identified. */
7916
7917 static tree
7918 outer_field_access_fix (wfl, node, rhs)
7919 tree wfl, node, rhs;
7920 {
7921 tree name, arg_type, arg;
7922
7923 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7924 {
7925 /* At any rate, check whether we're trying to assign a value to
7926 a final. */
7927 tree accessed = (JDECL_P (node) ? node :
7928 (TREE_CODE (node) == COMPONENT_REF ?
7929 TREE_OPERAND (node, 1) : node));
7930 if (check_final_assignment (accessed, wfl))
7931 return error_mark_node;
7932
7933 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7934 arg_type, name, arg, rhs);
7935 return java_complete_tree (node);
7936 }
7937 return NULL_TREE;
7938 }
7939
7940 /* Construct the expression that calls an access method:
7941 <type>.access$<n>(<arg1> [, <arg2>]);
7942
7943 ARG2 can be NULL and will be omitted in that case. It will denote a
7944 read access. */
7945
7946 static tree
7947 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7948 int lc;
7949 tree type, access_method_name, arg1, arg2;
7950 {
7951 tree args, cn, access;
7952
7953 args = arg1 ? arg1 :
7954 build_wfl_node (build_current_thisn (current_class));
7955 args = build_tree_list (NULL_TREE, args);
7956
7957 if (arg2)
7958 args = tree_cons (NULL_TREE, arg2, args);
7959
7960 access = build_method_invocation (build_wfl_node (access_method_name), args);
7961 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7962 return make_qualified_primary (cn, access, lc);
7963 }
7964
7965 static tree
7966 build_new_access_id ()
7967 {
7968 static int access_n_counter = 1;
7969 char buffer [128];
7970
7971 sprintf (buffer, "access$%d", access_n_counter++);
7972 return get_identifier (buffer);
7973 }
7974
7975 /* Create the static access functions for the outer field DECL. We define a
7976 read:
7977 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7978 return inst$.field;
7979 }
7980 and a write access:
7981 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7982 TREE_TYPE (<field>) value$) {
7983 return inst$.field = value$;
7984 }
7985 We should have a usage flags on the DECL so we can lazily turn the ones
7986 we're using for code generation. FIXME.
7987 */
7988
7989 static tree
7990 build_outer_field_access_methods (decl)
7991 tree decl;
7992 {
7993 tree id, args, stmt, mdecl;
7994
7995 if (FIELD_INNER_ACCESS_P (decl))
7996 return FIELD_INNER_ACCESS (decl);
7997
7998 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7999
8000 /* Create the identifier and a function named after it. */
8001 id = build_new_access_id ();
8002
8003 /* The identifier is marked as bearing the name of a generated write
8004 access function for outer field accessed from inner classes. */
8005 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8006
8007 /* Create the read access */
8008 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8009 TREE_CHAIN (args) = end_params_node;
8010 stmt = make_qualified_primary (build_wfl_node (inst_id),
8011 build_wfl_node (DECL_NAME (decl)), 0);
8012 stmt = build_return (0, stmt);
8013 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8014 TREE_TYPE (decl), id, args, stmt);
8015 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8016
8017 /* Create the write access method. No write access for final variable */
8018 if (!FIELD_FINAL (decl))
8019 {
8020 args = build_tree_list (inst_id,
8021 build_pointer_type (DECL_CONTEXT (decl)));
8022 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8023 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8024 stmt = make_qualified_primary (build_wfl_node (inst_id),
8025 build_wfl_node (DECL_NAME (decl)), 0);
8026 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8027 build_wfl_node (wpv_id)));
8028 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8029 TREE_TYPE (decl), id,
8030 args, stmt);
8031 }
8032 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8033
8034 /* Return the access name */
8035 return FIELD_INNER_ACCESS (decl) = id;
8036 }
8037
8038 /* Build an field access method NAME. */
8039
8040 static tree
8041 build_outer_field_access_method (class, type, name, args, body)
8042 tree class, type, name, args, body;
8043 {
8044 tree saved_current_function_decl, mdecl;
8045
8046 /* Create the method */
8047 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8048 fix_method_argument_names (args, mdecl);
8049 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8050
8051 /* Attach the method body. */
8052 saved_current_function_decl = current_function_decl;
8053 start_artificial_method_body (mdecl);
8054 java_method_add_stmt (mdecl, body);
8055 end_artificial_method_body (mdecl);
8056 current_function_decl = saved_current_function_decl;
8057
8058 return mdecl;
8059 }
8060
8061 \f
8062 /* This section deals with building access function necessary for
8063 certain kinds of method invocation from inner classes. */
8064
8065 static tree
8066 build_outer_method_access_method (decl)
8067 tree decl;
8068 {
8069 tree saved_current_function_decl, mdecl;
8070 tree args = NULL_TREE, call_args = NULL_TREE;
8071 tree carg, id, body, class;
8072 char buffer [80];
8073 int parm_id_count = 0;
8074
8075 /* Test this abort with an access to a private field */
8076 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8077 abort ();
8078
8079 /* Check the cache first */
8080 if (DECL_FUNCTION_INNER_ACCESS (decl))
8081 return DECL_FUNCTION_INNER_ACCESS (decl);
8082
8083 class = DECL_CONTEXT (decl);
8084
8085 /* Obtain an access identifier and mark it */
8086 id = build_new_access_id ();
8087 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8088
8089 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8090 /* Create the arguments, as much as the original */
8091 for (; carg && carg != end_params_node;
8092 carg = TREE_CHAIN (carg))
8093 {
8094 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8095 args = chainon (args, build_tree_list (get_identifier (buffer),
8096 TREE_VALUE (carg)));
8097 }
8098 args = chainon (args, end_params_node);
8099
8100 /* Create the method */
8101 mdecl = create_artificial_method (class, ACC_STATIC,
8102 TREE_TYPE (TREE_TYPE (decl)), id, args);
8103 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8104 /* There is a potential bug here. We should be able to use
8105 fix_method_argument_names, but then arg names get mixed up and
8106 eventually a constructor will have its this$0 altered and the
8107 outer context won't be assignment properly. The test case is
8108 stub.java FIXME */
8109 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8110
8111 /* Attach the method body. */
8112 saved_current_function_decl = current_function_decl;
8113 start_artificial_method_body (mdecl);
8114
8115 /* The actual method invocation uses the same args. When invoking a
8116 static methods that way, we don't want to skip the first
8117 argument. */
8118 carg = args;
8119 if (!METHOD_STATIC (decl))
8120 carg = TREE_CHAIN (carg);
8121 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8122 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8123 call_args);
8124
8125 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8126 call_args);
8127 if (!METHOD_STATIC (decl))
8128 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8129 body, 0);
8130 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8131 body = build_return (0, body);
8132 java_method_add_stmt (mdecl,body);
8133 end_artificial_method_body (mdecl);
8134 current_function_decl = saved_current_function_decl;
8135
8136 /* Back tag the access function so it know what it accesses */
8137 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8138
8139 /* Tag the current method so it knows it has an access generated */
8140 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8141 }
8142
8143 \f
8144 /* This section of the code deals with building expressions to access
8145 the enclosing instance of an inner class. The enclosing instance is
8146 kept in a generated field called this$<n>, with <n> being the
8147 inner class nesting level (starting from 0.) */
8148
8149 /* Build an access to a given this$<n>, always chaining access call to
8150 others. Access methods to this$<n> are build on the fly if
8151 necessary. This CAN'T be used to solely access this$<n-1> from
8152 this$<n> (which alway yield to special cases and optimization, see
8153 for example build_outer_field_access). */
8154
8155 static tree
8156 build_access_to_thisn (from, to, lc)
8157 tree from, to;
8158 int lc;
8159 {
8160 tree access = NULL_TREE;
8161
8162 while (from != to)
8163 {
8164 if (!access)
8165 {
8166 access = build_current_thisn (from);
8167 access = build_wfl_node (access);
8168 }
8169 else
8170 {
8171 tree access0_wfl, cn;
8172
8173 maybe_build_thisn_access_method (from);
8174 access0_wfl = build_wfl_node (access0_identifier_node);
8175 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8176 EXPR_WFL_LINECOL (access0_wfl) = lc;
8177 access = build_tree_list (NULL_TREE, access);
8178 access = build_method_invocation (access0_wfl, access);
8179 access = make_qualified_primary (cn, access, lc);
8180 }
8181
8182 /* if FROM isn't an inter class, that's fine, we've done
8183 enough. What we're looking for can be accessed from there. */
8184 from = DECL_CONTEXT (TYPE_NAME (from));
8185 if (!from)
8186 break;
8187 from = TREE_TYPE (from);
8188 }
8189 return access;
8190 }
8191
8192 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8193 is returned if nothing needs to be generated. Otherwise, the method
8194 generated and a method decl is returned.
8195
8196 NOTE: These generated methods should be declared in a class file
8197 attribute so that they can't be referred to directly. */
8198
8199 static tree
8200 maybe_build_thisn_access_method (type)
8201 tree type;
8202 {
8203 tree mdecl, args, stmt, rtype;
8204 tree saved_current_function_decl;
8205
8206 /* If TYPE is a top-level class, no access method is required.
8207 If there already is such an access method, bail out. */
8208 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8209 return NULL_TREE;
8210
8211 /* We generate the method. The method looks like:
8212 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8213 */
8214 args = build_tree_list (inst_id, build_pointer_type (type));
8215 TREE_CHAIN (args) = end_params_node;
8216 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8217 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8218 access0_identifier_node, args);
8219 fix_method_argument_names (args, mdecl);
8220 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8221 stmt = build_current_thisn (type);
8222 stmt = make_qualified_primary (build_wfl_node (inst_id),
8223 build_wfl_node (stmt), 0);
8224 stmt = build_return (0, stmt);
8225
8226 saved_current_function_decl = current_function_decl;
8227 start_artificial_method_body (mdecl);
8228 java_method_add_stmt (mdecl, stmt);
8229 end_artificial_method_body (mdecl);
8230 current_function_decl = saved_current_function_decl;
8231
8232 CLASS_ACCESS0_GENERATED_P (type) = 1;
8233
8234 return mdecl;
8235 }
8236
8237 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8238 the first level of innerclassing. this$1 for the next one, etc...
8239 This function can be invoked with TYPE to NULL, available and then
8240 has to count the parser context. */
8241
8242 static tree
8243 build_current_thisn (type)
8244 tree type;
8245 {
8246 static int saved_i = -1;
8247 static tree saved_thisn = NULL_TREE;
8248 static tree saved_type = NULL_TREE;
8249 static int saved_type_i = 0;
8250 static int initialized_p;
8251 tree decl;
8252 char buffer [80];
8253 int i = 0;
8254
8255 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8256 if (!initialized_p)
8257 {
8258 ggc_add_tree_root (&saved_thisn, 1);
8259 ggc_add_tree_root (&saved_type, 1);
8260 initialized_p = 1;
8261 }
8262
8263 if (type)
8264 {
8265 if (type == saved_type)
8266 i = saved_type_i;
8267 else
8268 {
8269 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8270 decl; decl = DECL_CONTEXT (decl), i++)
8271 ;
8272
8273 saved_type = type;
8274 saved_type_i = i;
8275 }
8276 }
8277 else
8278 i = list_length (GET_CPC_LIST ())-2;
8279
8280 if (i == saved_i)
8281 return saved_thisn;
8282
8283 sprintf (buffer, "this$%d", i);
8284 saved_i = i;
8285 saved_thisn = get_identifier (buffer);
8286 return saved_thisn;
8287 }
8288
8289 /* Return the assignement to the hidden enclosing context `this$<n>'
8290 by the second incoming parameter to the innerclass constructor. The
8291 form used is `this.this$<n> = this$<n>;'. */
8292
8293 static tree
8294 build_thisn_assign ()
8295 {
8296 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8297 {
8298 tree thisn = build_current_thisn (current_class);
8299 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8300 build_wfl_node (thisn), 0);
8301 tree rhs = build_wfl_node (thisn);
8302 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8303 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8304 }
8305 return NULL_TREE;
8306 }
8307
8308 \f
8309 /* Building the synthetic `class$' used to implement the `.class' 1.1
8310 extension for non primitive types. This method looks like:
8311
8312 static Class class$(String type) throws NoClassDefFoundError
8313 {
8314 try {return (java.lang.Class.forName (String));}
8315 catch (ClassNotFoundException e) {
8316 throw new NoClassDefFoundError(e.getMessage());}
8317 } */
8318
8319 static tree
8320 build_dot_class_method (class)
8321 tree class;
8322 {
8323 #define BWF(S) build_wfl_node (get_identifier ((S)))
8324 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8325 tree args, tmp, saved_current_function_decl, mdecl;
8326 tree stmt, throw_stmt, catch, catch_block, try_block;
8327 tree catch_clause_param;
8328 tree class_not_found_exception, no_class_def_found_error;
8329
8330 static tree get_message_wfl, type_parm_wfl;
8331
8332 if (!get_message_wfl)
8333 {
8334 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8335 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8336 ggc_add_tree_root (&get_message_wfl, 1);
8337 ggc_add_tree_root (&type_parm_wfl, 1);
8338 }
8339
8340 /* Build the arguments */
8341 args = build_tree_list (get_identifier ("type$"),
8342 build_pointer_type (string_type_node));
8343 TREE_CHAIN (args) = end_params_node;
8344
8345 /* Build the qualified name java.lang.Class.forName */
8346 tmp = MQN (MQN (MQN (BWF ("java"),
8347 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8348
8349 /* For things we have to catch and throw */
8350 class_not_found_exception =
8351 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8352 no_class_def_found_error =
8353 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8354 load_class (class_not_found_exception, 1);
8355 load_class (no_class_def_found_error, 1);
8356
8357 /* Create the "class$" function */
8358 mdecl = create_artificial_method (class, ACC_STATIC,
8359 build_pointer_type (class_type_node),
8360 classdollar_identifier_node, args);
8361 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8362 no_class_def_found_error);
8363
8364 /* We start by building the try block. We need to build:
8365 return (java.lang.Class.forName (type)); */
8366 stmt = build_method_invocation (tmp,
8367 build_tree_list (NULL_TREE, type_parm_wfl));
8368 stmt = build_return (0, stmt);
8369 /* Put it in a block. That's the try block */
8370 try_block = build_expr_block (stmt, NULL_TREE);
8371
8372 /* Now onto the catch block. We start by building the expression
8373 throwing a new exception:
8374 throw new NoClassDefFoundError (_.getMessage); */
8375 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8376 get_message_wfl, 0);
8377 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8378
8379 /* Build new NoClassDefFoundError (_.getMessage) */
8380 throw_stmt = build_new_invocation
8381 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8382 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8383
8384 /* Build the throw, (it's too early to use BUILD_THROW) */
8385 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8386
8387 /* Build the catch block to encapsulate all this. We begin by
8388 building an decl for the catch clause parameter and link it to
8389 newly created block, the catch block. */
8390 catch_clause_param =
8391 build_decl (VAR_DECL, wpv_id,
8392 build_pointer_type (class_not_found_exception));
8393 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8394
8395 /* We initialize the variable with the exception handler. */
8396 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8397 soft_exceptioninfo_call_node);
8398 add_stmt_to_block (catch_block, NULL_TREE, catch);
8399
8400 /* We add the statement throwing the new exception */
8401 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8402
8403 /* Build a catch expression for all this */
8404 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8405
8406 /* Build the try/catch sequence */
8407 stmt = build_try_statement (0, try_block, catch_block);
8408
8409 fix_method_argument_names (args, mdecl);
8410 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8411 saved_current_function_decl = current_function_decl;
8412 start_artificial_method_body (mdecl);
8413 java_method_add_stmt (mdecl, stmt);
8414 end_artificial_method_body (mdecl);
8415 current_function_decl = saved_current_function_decl;
8416 TYPE_DOT_CLASS (class) = mdecl;
8417
8418 return mdecl;
8419 }
8420
8421 static tree
8422 build_dot_class_method_invocation (type)
8423 tree type;
8424 {
8425 tree sig_id, s;
8426
8427 if (TYPE_ARRAY_P (type))
8428 sig_id = build_java_signature (type);
8429 else
8430 sig_id = DECL_NAME (TYPE_NAME (type));
8431
8432 s = build_string (IDENTIFIER_LENGTH (sig_id),
8433 IDENTIFIER_POINTER (sig_id));
8434 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8435 build_tree_list (NULL_TREE, s));
8436 }
8437
8438 /* This section of the code deals with constructor. */
8439
8440 /* Craft a body for default constructor. Patch existing constructor
8441 bodies with call to super() and field initialization statements if
8442 necessary. */
8443
8444 static void
8445 fix_constructors (mdecl)
8446 tree mdecl;
8447 {
8448 tree body = DECL_FUNCTION_BODY (mdecl);
8449 tree thisn_assign, compound = NULL_TREE;
8450 tree class_type = DECL_CONTEXT (mdecl);
8451
8452 if (!body)
8453 {
8454 /* It is an error for the compiler to generate a default
8455 constructor if the superclass doesn't have a constructor that
8456 takes no argument, or the same args for an anonymous class */
8457 if (verify_constructor_super (mdecl))
8458 {
8459 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8460 tree save = DECL_NAME (mdecl);
8461 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8462 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8463 parse_error_context
8464 (lookup_cl (TYPE_NAME (class_type)),
8465 "No constructor matching `%s' found in class `%s'",
8466 lang_printable_name (mdecl, 0), n);
8467 DECL_NAME (mdecl) = save;
8468 }
8469
8470 /* The constructor body must be crafted by hand. It's the
8471 constructor we defined when we realize we didn't have the
8472 CLASSNAME() constructor */
8473 start_artificial_method_body (mdecl);
8474
8475 /* Insert an assignment to the this$<n> hidden field, if
8476 necessary */
8477 if ((thisn_assign = build_thisn_assign ()))
8478 java_method_add_stmt (mdecl, thisn_assign);
8479
8480 /* We don't generate a super constructor invocation if we're
8481 compiling java.lang.Object. build_super_invocation takes care
8482 of that. */
8483 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8484
8485 /* Insert the instance initializer block right here, after the
8486 super invocation. */
8487 add_instance_initializer (mdecl);
8488
8489 end_artificial_method_body (mdecl);
8490 }
8491 /* Search for an explicit constructor invocation */
8492 else
8493 {
8494 int found = 0;
8495 tree main_block = BLOCK_EXPR_BODY (body);
8496
8497 while (body)
8498 switch (TREE_CODE (body))
8499 {
8500 case CALL_EXPR:
8501 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8502 body = NULL_TREE;
8503 break;
8504 case COMPOUND_EXPR:
8505 case EXPR_WITH_FILE_LOCATION:
8506 body = TREE_OPERAND (body, 0);
8507 break;
8508 case BLOCK:
8509 body = BLOCK_EXPR_BODY (body);
8510 break;
8511 default:
8512 found = 0;
8513 body = NULL_TREE;
8514 }
8515 /* The constructor is missing an invocation of super() */
8516 if (!found)
8517 compound = add_stmt_to_compound (compound, NULL_TREE,
8518 build_super_invocation (mdecl));
8519
8520 /* Generate the assignment to this$<n>, if necessary */
8521 if ((thisn_assign = build_thisn_assign ()))
8522 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8523
8524 /* Insert the instance initializer block right here, after the
8525 super invocation. */
8526 add_instance_initializer (mdecl);
8527
8528 /* Fix the constructor main block if we're adding extra stmts */
8529 if (compound)
8530 {
8531 compound = add_stmt_to_compound (compound, NULL_TREE,
8532 BLOCK_EXPR_BODY (main_block));
8533 BLOCK_EXPR_BODY (main_block) = compound;
8534 }
8535 }
8536 }
8537
8538 /* Browse constructors in the super class, searching for a constructor
8539 that doesn't take any argument. Return 0 if one is found, 1
8540 otherwise. If the current class is an anonymous inner class, look
8541 for something that has the same signature. */
8542
8543 static int
8544 verify_constructor_super (mdecl)
8545 tree mdecl;
8546 {
8547 tree class = CLASSTYPE_SUPER (current_class);
8548 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8549 tree sdecl;
8550
8551 if (!class)
8552 return 0;
8553
8554 if (ANONYMOUS_CLASS_P (current_class))
8555 {
8556 tree mdecl_arg_type;
8557 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8558 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8559 if (DECL_CONSTRUCTOR_P (sdecl))
8560 {
8561 tree m_arg_type;
8562 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8563 if (super_inner)
8564 arg_type = TREE_CHAIN (arg_type);
8565 for (m_arg_type = mdecl_arg_type;
8566 (arg_type != end_params_node
8567 && m_arg_type != end_params_node);
8568 arg_type = TREE_CHAIN (arg_type),
8569 m_arg_type = TREE_CHAIN (m_arg_type))
8570 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8571 break;
8572
8573 if (arg_type == end_params_node && m_arg_type == end_params_node)
8574 return 0;
8575 }
8576 }
8577 else
8578 {
8579 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8580 {
8581 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8582 if (super_inner)
8583 arg = TREE_CHAIN (arg);
8584 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8585 return 0;
8586 }
8587 }
8588 return 1;
8589 }
8590
8591 /* Generate code for all context remembered for code generation. */
8592
8593 void
8594 java_expand_classes ()
8595 {
8596 int save_error_count = 0;
8597 static struct parser_ctxt *saved_ctxp = NULL;
8598
8599 java_parse_abort_on_error ();
8600 if (!(ctxp = ctxp_for_generation))
8601 return;
8602 java_layout_classes ();
8603 java_parse_abort_on_error ();
8604
8605 saved_ctxp = ctxp_for_generation;
8606 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8607 {
8608 ctxp = ctxp_for_generation;
8609 input_filename = ctxp->filename;
8610 lang_init_source (2); /* Error msgs have method prototypes */
8611 java_complete_expand_classes (); /* Complete and expand classes */
8612 java_parse_abort_on_error ();
8613 }
8614 input_filename = main_input_filename;
8615
8616 /* Find anonymous classes and expand their constructor, now they
8617 have been fixed. */
8618 for (ctxp_for_generation = saved_ctxp;
8619 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8620 {
8621 tree current;
8622 ctxp = ctxp_for_generation;
8623 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8624 {
8625 current_class = TREE_TYPE (current);
8626 if (ANONYMOUS_CLASS_P (current_class))
8627 {
8628 tree d;
8629 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8630 {
8631 if (DECL_CONSTRUCTOR_P (d))
8632 {
8633 restore_line_number_status (1);
8634 java_complete_expand_method (d);
8635 restore_line_number_status (0);
8636 break; /* We now there are no other ones */
8637 }
8638 }
8639 }
8640 }
8641 }
8642
8643 /* If we've found error at that stage, don't try to generate
8644 anything, unless we're emitting xrefs or checking the syntax only
8645 (but not using -fsyntax-only for the purpose of generating
8646 bytecode. */
8647 if (java_error_count && !flag_emit_xref
8648 && (!flag_syntax_only && !flag_emit_class_files))
8649 return;
8650
8651 /* Now things are stable, go for generation of the class data. */
8652 for (ctxp_for_generation = saved_ctxp;
8653 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8654 {
8655 tree current;
8656 ctxp = ctxp_for_generation;
8657 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8658 {
8659 current_class = TREE_TYPE (current);
8660 outgoing_cpool = TYPE_CPOOL (current_class);
8661 if (flag_emit_class_files)
8662 write_classfile (current_class);
8663 if (flag_emit_xref)
8664 expand_xref (current_class);
8665 else if (! flag_syntax_only)
8666 finish_class ();
8667 }
8668 }
8669 }
8670
8671 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8672 a tree list node containing RIGHT. Fore coming RIGHTs will be
8673 chained to this hook. LOCATION contains the location of the
8674 separating `.' operator. */
8675
8676 static tree
8677 make_qualified_primary (primary, right, location)
8678 tree primary, right;
8679 int location;
8680 {
8681 tree wfl;
8682
8683 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8684 wfl = build_wfl_wrap (primary, location);
8685 else
8686 {
8687 wfl = primary;
8688 /* If wfl wasn't qualified, we build a first anchor */
8689 if (!EXPR_WFL_QUALIFICATION (wfl))
8690 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8691 }
8692
8693 /* And chain them */
8694 EXPR_WFL_LINECOL (right) = location;
8695 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8696 PRIMARY_P (wfl) = 1;
8697 return wfl;
8698 }
8699
8700 /* Simple merge of two name separated by a `.' */
8701
8702 static tree
8703 merge_qualified_name (left, right)
8704 tree left, right;
8705 {
8706 tree node;
8707 if (!left && !right)
8708 return NULL_TREE;
8709
8710 if (!left)
8711 return right;
8712
8713 if (!right)
8714 return left;
8715
8716 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8717 IDENTIFIER_LENGTH (left));
8718 obstack_1grow (&temporary_obstack, '.');
8719 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8720 IDENTIFIER_LENGTH (right));
8721 node = get_identifier (obstack_base (&temporary_obstack));
8722 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8723 QUALIFIED_P (node) = 1;
8724 return node;
8725 }
8726
8727 /* Merge the two parts of a qualified name into LEFT. Set the
8728 location information of the resulting node to LOCATION, usually
8729 inherited from the location information of the `.' operator. */
8730
8731 static tree
8732 make_qualified_name (left, right, location)
8733 tree left, right;
8734 int location;
8735 {
8736 #ifdef USE_COMPONENT_REF
8737 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8738 EXPR_WFL_LINECOL (node) = location;
8739 return node;
8740 #else
8741 tree left_id = EXPR_WFL_NODE (left);
8742 tree right_id = EXPR_WFL_NODE (right);
8743 tree wfl, merge;
8744
8745 merge = merge_qualified_name (left_id, right_id);
8746
8747 /* Left wasn't qualified and is now qualified */
8748 if (!QUALIFIED_P (left_id))
8749 {
8750 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8751 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8752 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8753 }
8754
8755 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8756 EXPR_WFL_LINECOL (wfl) = location;
8757 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8758
8759 EXPR_WFL_NODE (left) = merge;
8760 return left;
8761 #endif
8762 }
8763
8764 /* Extract the last identifier component of the qualified in WFL. The
8765 last identifier is removed from the linked list */
8766
8767 static tree
8768 cut_identifier_in_qualified (wfl)
8769 tree wfl;
8770 {
8771 tree q;
8772 tree previous = NULL_TREE;
8773 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8774 if (!TREE_CHAIN (q))
8775 {
8776 if (!previous)
8777 /* Operating on a non qualified qualified WFL. */
8778 abort ();
8779
8780 TREE_CHAIN (previous) = NULL_TREE;
8781 return TREE_PURPOSE (q);
8782 }
8783 }
8784
8785 /* Resolve the expression name NAME. Return its decl. */
8786
8787 static tree
8788 resolve_expression_name (id, orig)
8789 tree id;
8790 tree *orig;
8791 {
8792 tree name = EXPR_WFL_NODE (id);
8793 tree decl;
8794
8795 /* 6.5.5.1: Simple expression names */
8796 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8797 {
8798 /* 15.13.1: NAME can appear within the scope of a local variable
8799 declaration */
8800 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8801 return decl;
8802
8803 /* 15.13.1: NAME can appear within a class declaration */
8804 else
8805 {
8806 decl = lookup_field_wrapper (current_class, name);
8807 if (decl)
8808 {
8809 tree access = NULL_TREE;
8810 int fs = FIELD_STATIC (decl);
8811
8812 /* If we're accessing an outer scope local alias, make
8813 sure we change the name of the field we're going to
8814 build access to. */
8815 if (FIELD_LOCAL_ALIAS_USED (decl))
8816 name = DECL_NAME (decl);
8817
8818 /* Instance variable (8.3.1.1) can't appear within
8819 static method, static initializer or initializer for
8820 a static variable. */
8821 if (!fs && METHOD_STATIC (current_function_decl))
8822 {
8823 static_ref_err (id, name, current_class);
8824 return error_mark_node;
8825 }
8826 /* Instance variables can't appear as an argument of
8827 an explicit constructor invocation */
8828 if (!fs && ctxp->explicit_constructor_p
8829 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
8830 {
8831 parse_error_context
8832 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8833 return error_mark_node;
8834 }
8835
8836 /* If we're processing an inner class and we're trying
8837 to access a field belonging to an outer class, build
8838 the access to the field */
8839 if (!fs && outer_field_access_p (current_class, decl))
8840 {
8841 if (CLASS_STATIC (TYPE_NAME (current_class)))
8842 {
8843 static_ref_err (id, DECL_NAME (decl), current_class);
8844 return error_mark_node;
8845 }
8846 return build_outer_field_access (id, decl);
8847 }
8848
8849 /* Otherwise build what it takes to access the field */
8850 access = build_field_ref ((fs ? NULL_TREE : current_this),
8851 DECL_CONTEXT (decl), name);
8852 if (fs && !flag_emit_class_files && !flag_emit_xref)
8853 access = build_class_init (DECL_CONTEXT (access), access);
8854 /* We may be asked to save the real field access node */
8855 if (orig)
8856 *orig = access;
8857 /* And we return what we got */
8858 return access;
8859 }
8860 /* Fall down to error report on undefined variable */
8861 }
8862 }
8863 /* 6.5.5.2 Qualified Expression Names */
8864 else
8865 {
8866 if (orig)
8867 *orig = NULL_TREE;
8868 qualify_ambiguous_name (id);
8869 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8870 /* 15.10.2: Accessing Superclass Members using super */
8871 return resolve_field_access (id, orig, NULL);
8872 }
8873
8874 /* We've got an error here */
8875 parse_error_context (id, "Undefined variable `%s'",
8876 IDENTIFIER_POINTER (name));
8877
8878 return error_mark_node;
8879 }
8880
8881 static void
8882 static_ref_err (wfl, field_id, class_type)
8883 tree wfl, field_id, class_type;
8884 {
8885 parse_error_context
8886 (wfl,
8887 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8888 IDENTIFIER_POINTER (field_id),
8889 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8890 }
8891
8892 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8893 We return something suitable to generate the field access. We also
8894 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8895 recipient's address can be null. */
8896
8897 static tree
8898 resolve_field_access (qual_wfl, field_decl, field_type)
8899 tree qual_wfl;
8900 tree *field_decl, *field_type;
8901 {
8902 int is_static = 0;
8903 tree field_ref;
8904 tree decl, where_found, type_found;
8905
8906 if (resolve_qualified_expression_name (qual_wfl, &decl,
8907 &where_found, &type_found))
8908 return error_mark_node;
8909
8910 /* Resolve the LENGTH field of an array here */
8911 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8912 && type_found && TYPE_ARRAY_P (type_found)
8913 && ! flag_emit_class_files && ! flag_emit_xref)
8914 {
8915 tree length = build_java_array_length_access (where_found);
8916 field_ref =
8917 build_java_arraynull_check (type_found, length, int_type_node);
8918
8919 /* In case we're dealing with a static array, we need to
8920 initialize its class before the array length can be fetched.
8921 It's also a good time to create a DECL_RTL for the field if
8922 none already exists, otherwise if the field was declared in a
8923 class found in an external file and hasn't been (and won't
8924 be) accessed for its value, none will be created. */
8925 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8926 {
8927 build_static_field_ref (where_found);
8928 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8929 }
8930 }
8931 /* We might have been trying to resolve field.method(). In which
8932 case, the resolution is over and decl is the answer */
8933 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8934 field_ref = decl;
8935 else if (JDECL_P (decl))
8936 {
8937 int static_final_found = 0;
8938 if (!type_found)
8939 type_found = DECL_CONTEXT (decl);
8940 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8941 if (CLASS_FINAL_VARIABLE_P (decl)
8942 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8943 && DECL_INITIAL (decl))
8944 {
8945 /* When called on a FIELD_DECL of the right (primitive)
8946 type, java_complete_tree will try to substitue the decl
8947 for it's initial value. */
8948 field_ref = java_complete_tree (decl);
8949 static_final_found = 1;
8950 }
8951 else
8952 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8953 NULL_TREE : where_found),
8954 type_found, DECL_NAME (decl));
8955 if (field_ref == error_mark_node)
8956 return error_mark_node;
8957 if (is_static && !static_final_found
8958 && !flag_emit_class_files && !flag_emit_xref)
8959 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8960 }
8961 else
8962 field_ref = decl;
8963
8964 if (field_decl)
8965 *field_decl = decl;
8966 if (field_type)
8967 *field_type = (QUAL_DECL_TYPE (decl) ?
8968 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8969 return field_ref;
8970 }
8971
8972 /* If NODE is an access to f static field, strip out the class
8973 initialization part and return the field decl, otherwise, return
8974 NODE. */
8975
8976 static tree
8977 strip_out_static_field_access_decl (node)
8978 tree node;
8979 {
8980 if (TREE_CODE (node) == COMPOUND_EXPR)
8981 {
8982 tree op1 = TREE_OPERAND (node, 1);
8983 if (TREE_CODE (op1) == COMPOUND_EXPR)
8984 {
8985 tree call = TREE_OPERAND (op1, 0);
8986 if (TREE_CODE (call) == CALL_EXPR
8987 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8988 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8989 == soft_initclass_node)
8990 return TREE_OPERAND (op1, 1);
8991 }
8992 else if (JDECL_P (op1))
8993 return op1;
8994 }
8995 return node;
8996 }
8997
8998 /* 6.5.5.2: Qualified Expression Names */
8999
9000 static int
9001 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9002 tree wfl;
9003 tree *found_decl, *type_found, *where_found;
9004 {
9005 int from_type = 0; /* Field search initiated from a type */
9006 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9007 int previous_call_static = 0;
9008 int is_static;
9009 tree decl = NULL_TREE, type = NULL_TREE, q;
9010 /* For certain for of inner class instantiation */
9011 tree saved_current, saved_this;
9012 #define RESTORE_THIS_AND_CURRENT_CLASS \
9013 { current_class = saved_current; current_this = saved_this;}
9014
9015 *type_found = *where_found = NULL_TREE;
9016
9017 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9018 {
9019 tree qual_wfl = QUAL_WFL (q);
9020 tree ret_decl; /* for EH checking */
9021 int location; /* for EH checking */
9022
9023 /* 15.10.1 Field Access Using a Primary */
9024 switch (TREE_CODE (qual_wfl))
9025 {
9026 case CALL_EXPR:
9027 case NEW_CLASS_EXPR:
9028 /* If the access to the function call is a non static field,
9029 build the code to access it. */
9030 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9031 {
9032 decl = maybe_access_field (decl, *where_found,
9033 DECL_CONTEXT (decl));
9034 if (decl == error_mark_node)
9035 return 1;
9036 }
9037
9038 /* And code for the function call */
9039 if (complete_function_arguments (qual_wfl))
9040 return 1;
9041
9042 /* We might have to setup a new current class and a new this
9043 for the search of an inner class, relative to the type of
9044 a expression resolved as `decl'. The current values are
9045 saved and restored shortly after */
9046 saved_current = current_class;
9047 saved_this = current_this;
9048 if (decl
9049 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9050 || from_qualified_this))
9051 {
9052 /* If we still have `from_qualified_this', we have the form
9053 <T>.this.f() and we need to build <T>.this */
9054 if (from_qualified_this)
9055 {
9056 decl = build_access_to_thisn (current_class, type, 0);
9057 decl = java_complete_tree (decl);
9058 type = TREE_TYPE (TREE_TYPE (decl));
9059 }
9060 current_class = type;
9061 current_this = decl;
9062 from_qualified_this = 0;
9063 }
9064
9065 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9066 CALL_USING_SUPER (qual_wfl) = 1;
9067 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9068 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9069 *where_found = patch_method_invocation (qual_wfl, decl, type,
9070 from_super,
9071 &is_static, &ret_decl);
9072 if (*where_found == error_mark_node)
9073 {
9074 RESTORE_THIS_AND_CURRENT_CLASS;
9075 return 1;
9076 }
9077 *type_found = type = QUAL_DECL_TYPE (*where_found);
9078
9079 /* If we're creating an inner class instance, check for that
9080 an enclosing instance is in scope */
9081 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9082 && INNER_ENCLOSING_SCOPE_CHECK (type))
9083 {
9084 parse_error_context
9085 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9086 lang_printable_name (type, 0),
9087 (!current_this ? "" :
9088 "; an explicit one must be provided when creating this inner class"));
9089 RESTORE_THIS_AND_CURRENT_CLASS;
9090 return 1;
9091 }
9092
9093 /* In case we had to change then to resolve a inner class
9094 instantiation using a primary qualified by a `new' */
9095 RESTORE_THIS_AND_CURRENT_CLASS;
9096
9097 /* EH check. No check on access$<n> functions */
9098 if (location
9099 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9100 (DECL_NAME (current_function_decl)))
9101 check_thrown_exceptions (location, ret_decl);
9102
9103 /* If the previous call was static and this one is too,
9104 build a compound expression to hold the two (because in
9105 that case, previous function calls aren't transported as
9106 forcoming function's argument. */
9107 if (previous_call_static && is_static)
9108 {
9109 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9110 TREE_SIDE_EFFECTS (decl) = 1;
9111 }
9112 else
9113 {
9114 previous_call_static = is_static;
9115 decl = *where_found;
9116 }
9117 from_type = 0;
9118 continue;
9119
9120 case NEW_ARRAY_EXPR:
9121 case NEW_ANONYMOUS_ARRAY_EXPR:
9122 *where_found = decl = java_complete_tree (qual_wfl);
9123 if (decl == error_mark_node)
9124 return 1;
9125 *type_found = type = QUAL_DECL_TYPE (decl);
9126 CLASS_LOADED_P (type) = 1;
9127 continue;
9128
9129 case CONVERT_EXPR:
9130 *where_found = decl = java_complete_tree (qual_wfl);
9131 if (decl == error_mark_node)
9132 return 1;
9133 *type_found = type = QUAL_DECL_TYPE (decl);
9134 from_cast = 1;
9135 continue;
9136
9137 case CONDITIONAL_EXPR:
9138 case STRING_CST:
9139 case MODIFY_EXPR:
9140 *where_found = decl = java_complete_tree (qual_wfl);
9141 if (decl == error_mark_node)
9142 return 1;
9143 *type_found = type = QUAL_DECL_TYPE (decl);
9144 continue;
9145
9146 case ARRAY_REF:
9147 /* If the access to the function call is a non static field,
9148 build the code to access it. */
9149 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9150 {
9151 decl = maybe_access_field (decl, *where_found, type);
9152 if (decl == error_mark_node)
9153 return 1;
9154 }
9155 /* And code for the array reference expression */
9156 decl = java_complete_tree (qual_wfl);
9157 if (decl == error_mark_node)
9158 return 1;
9159 type = QUAL_DECL_TYPE (decl);
9160 continue;
9161
9162 case PLUS_EXPR:
9163 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9164 return 1;
9165 if ((type = patch_string (decl)))
9166 decl = type;
9167 *where_found = QUAL_RESOLUTION (q) = decl;
9168 *type_found = type = TREE_TYPE (decl);
9169 break;
9170
9171 case CLASS_LITERAL:
9172 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9173 return 1;
9174 *where_found = QUAL_RESOLUTION (q) = decl;
9175 *type_found = type = TREE_TYPE (decl);
9176 break;
9177
9178 default:
9179 /* Fix for -Wall Just go to the next statement. Don't
9180 continue */
9181 break;
9182 }
9183
9184 /* If we fall here, we weren't processing a (static) function call. */
9185 previous_call_static = 0;
9186
9187 /* It can be the keyword THIS */
9188 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9189 {
9190 if (!current_this)
9191 {
9192 parse_error_context
9193 (wfl, "Keyword `this' used outside allowed context");
9194 return 1;
9195 }
9196 if (ctxp->explicit_constructor_p
9197 && type == current_class)
9198 {
9199 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9200 return 1;
9201 }
9202 /* We have to generate code for intermediate acess */
9203 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9204 {
9205 *where_found = decl = current_this;
9206 *type_found = type = QUAL_DECL_TYPE (decl);
9207 }
9208 /* We're trying to access the this from somewhere else. Make sure
9209 it's allowed before doing so. */
9210 else
9211 {
9212 if (!enclosing_context_p (type, current_class))
9213 {
9214 char *p = xstrdup (lang_printable_name (type, 0));
9215 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9216 p, p,
9217 lang_printable_name (current_class, 0));
9218 free (p);
9219 return 1;
9220 }
9221 from_qualified_this = 1;
9222 /* If there's nothing else after that, we need to
9223 produce something now, otherwise, the section of the
9224 code that needs to produce <T>.this will generate
9225 what is necessary. */
9226 if (!TREE_CHAIN (q))
9227 {
9228 decl = build_access_to_thisn (current_class, type, 0);
9229 *where_found = decl = java_complete_tree (decl);
9230 *type_found = type = TREE_TYPE (decl);
9231 }
9232 }
9233
9234 from_type = 0;
9235 continue;
9236 }
9237
9238 /* 15.10.2 Accessing Superclass Members using SUPER */
9239 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9240 {
9241 tree node;
9242 /* Check on the restricted use of SUPER */
9243 if (METHOD_STATIC (current_function_decl)
9244 || current_class == object_type_node)
9245 {
9246 parse_error_context
9247 (wfl, "Keyword `super' used outside allowed context");
9248 return 1;
9249 }
9250 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9251 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9252 CLASSTYPE_SUPER (current_class),
9253 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9254 *where_found = decl = java_complete_tree (node);
9255 if (decl == error_mark_node)
9256 return 1;
9257 *type_found = type = QUAL_DECL_TYPE (decl);
9258 from_super = from_type = 1;
9259 continue;
9260 }
9261
9262 /* 15.13.1: Can't search for field name in packages, so we
9263 assume a variable/class name was meant. */
9264 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9265 {
9266 tree name = resolve_package (wfl, &q);
9267 if (name)
9268 {
9269 tree list;
9270 *where_found = decl = resolve_no_layout (name, qual_wfl);
9271 /* We want to be absolutely sure that the class is laid
9272 out. We're going to search something inside it. */
9273 *type_found = type = TREE_TYPE (decl);
9274 layout_class (type);
9275 from_type = 1;
9276
9277 /* Fix them all the way down, if any are left. */
9278 if (q)
9279 {
9280 list = TREE_CHAIN (q);
9281 while (list)
9282 {
9283 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9284 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9285 list = TREE_CHAIN (list);
9286 }
9287 }
9288 }
9289 else
9290 {
9291 if (from_super || from_cast)
9292 parse_error_context
9293 ((from_cast ? qual_wfl : wfl),
9294 "No variable `%s' defined in class `%s'",
9295 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9296 lang_printable_name (type, 0));
9297 else
9298 parse_error_context
9299 (qual_wfl, "Undefined variable or class name: `%s'",
9300 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9301 return 1;
9302 }
9303 }
9304
9305 /* We have a type name. It's been already resolved when the
9306 expression was qualified. */
9307 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9308 {
9309 if (!(decl = QUAL_RESOLUTION (q)))
9310 return 1; /* Error reported already */
9311
9312 /* Sneak preview. If next we see a `new', we're facing a
9313 qualification with resulted in a type being selected
9314 instead of a field. Report the error */
9315 if(TREE_CHAIN (q)
9316 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9317 {
9318 parse_error_context (qual_wfl, "Undefined variable `%s'",
9319 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9320 return 1;
9321 }
9322
9323 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9324 {
9325 parse_error_context
9326 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9327 java_accstring_lookup (get_access_flags_from_decl (decl)),
9328 GET_TYPE_NAME (type),
9329 IDENTIFIER_POINTER (DECL_NAME (decl)),
9330 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9331 return 1;
9332 }
9333 check_deprecation (qual_wfl, decl);
9334
9335 type = TREE_TYPE (decl);
9336 from_type = 1;
9337 }
9338 /* We resolve and expression name */
9339 else
9340 {
9341 tree field_decl = NULL_TREE;
9342
9343 /* If there exists an early resolution, use it. That occurs
9344 only once and we know that there are more things to
9345 come. Don't do that when processing something after SUPER
9346 (we need more thing to be put in place below */
9347 if (!from_super && QUAL_RESOLUTION (q))
9348 {
9349 decl = QUAL_RESOLUTION (q);
9350 if (!type)
9351 {
9352 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9353 {
9354 if (current_this)
9355 *where_found = current_this;
9356 else
9357 {
9358 static_ref_err (qual_wfl, DECL_NAME (decl),
9359 current_class);
9360 return 1;
9361 }
9362 if (outer_field_access_p (current_class, decl))
9363 decl = build_outer_field_access (qual_wfl, decl);
9364 }
9365 else
9366 {
9367 *where_found = TREE_TYPE (decl);
9368 if (TREE_CODE (*where_found) == POINTER_TYPE)
9369 *where_found = TREE_TYPE (*where_found);
9370 }
9371 }
9372 }
9373
9374 /* We have to search for a field, knowing the type of its
9375 container. The flag FROM_TYPE indicates that we resolved
9376 the last member of the expression as a type name, which
9377 means that for the resolution of this field, we'll look
9378 for other errors than if it was resolved as a member of
9379 an other field. */
9380 else
9381 {
9382 int is_static;
9383 tree field_decl_type; /* For layout */
9384
9385 if (!from_type && !JREFERENCE_TYPE_P (type))
9386 {
9387 parse_error_context
9388 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9389 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9390 lang_printable_name (type, 0),
9391 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9392 return 1;
9393 }
9394
9395 field_decl = lookup_field_wrapper (type,
9396 EXPR_WFL_NODE (qual_wfl));
9397
9398 /* Maybe what we're trying to access to is an inner
9399 class, only if decl is a TYPE_DECL. */
9400 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9401 {
9402 tree ptr, inner_decl;
9403
9404 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9405 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9406 if (inner_decl)
9407 {
9408 check_inner_class_access (inner_decl, decl, qual_wfl);
9409 type = TREE_TYPE (inner_decl);
9410 decl = inner_decl;
9411 from_type = 1;
9412 continue;
9413 }
9414 }
9415
9416 if (field_decl == NULL_TREE)
9417 {
9418 parse_error_context
9419 (qual_wfl, "No variable `%s' defined in type `%s'",
9420 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9421 GET_TYPE_NAME (type));
9422 return 1;
9423 }
9424 if (field_decl == error_mark_node)
9425 return 1;
9426
9427 /* Layout the type of field_decl, since we may need
9428 it. Don't do primitive types or loaded classes. The
9429 situation of non primitive arrays may not handled
9430 properly here. FIXME */
9431 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9432 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9433 else
9434 field_decl_type = TREE_TYPE (field_decl);
9435 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9436 && !CLASS_LOADED_P (field_decl_type)
9437 && !TYPE_ARRAY_P (field_decl_type))
9438 resolve_and_layout (field_decl_type, NULL_TREE);
9439 if (TYPE_ARRAY_P (field_decl_type))
9440 CLASS_LOADED_P (field_decl_type) = 1;
9441
9442 /* Check on accessibility here */
9443 if (not_accessible_p (current_class, field_decl,
9444 TREE_TYPE (decl), from_super))
9445 {
9446 parse_error_context
9447 (qual_wfl,
9448 "Can't access %s field `%s.%s' from `%s'",
9449 java_accstring_lookup
9450 (get_access_flags_from_decl (field_decl)),
9451 GET_TYPE_NAME (type),
9452 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9453 IDENTIFIER_POINTER
9454 (DECL_NAME (TYPE_NAME (current_class))));
9455 return 1;
9456 }
9457 check_deprecation (qual_wfl, field_decl);
9458
9459 /* There are things to check when fields are accessed
9460 from type. There are no restrictions on a static
9461 declaration of the field when it is accessed from an
9462 interface */
9463 is_static = FIELD_STATIC (field_decl);
9464 if (!from_super && from_type
9465 && !TYPE_INTERFACE_P (type)
9466 && !is_static
9467 && (current_function_decl
9468 && METHOD_STATIC (current_function_decl)))
9469 {
9470 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9471 return 1;
9472 }
9473 from_cast = from_super = 0;
9474
9475 /* It's an access from a type but it isn't static, we
9476 make it relative to `this'. */
9477 if (!is_static && from_type)
9478 decl = current_this;
9479
9480 /* If we need to generate something to get a proper
9481 handle on what this field is accessed from, do it
9482 now. */
9483 if (!is_static)
9484 {
9485 decl = maybe_access_field (decl, *where_found, *type_found);
9486 if (decl == error_mark_node)
9487 return 1;
9488 }
9489
9490 /* We want to keep the location were found it, and the type
9491 we found. */
9492 *where_found = decl;
9493 *type_found = type;
9494
9495 /* Generate the correct expression for field access from
9496 qualified this */
9497 if (from_qualified_this)
9498 {
9499 field_decl = build_outer_field_access (qual_wfl, field_decl);
9500 from_qualified_this = 0;
9501 }
9502
9503 /* This is the decl found and eventually the next one to
9504 search from */
9505 decl = field_decl;
9506 }
9507 from_type = 0;
9508 type = QUAL_DECL_TYPE (decl);
9509
9510 /* Sneak preview. If decl is qualified by a `new', report
9511 the error here to be accurate on the peculiar construct */
9512 if (TREE_CHAIN (q)
9513 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9514 && !JREFERENCE_TYPE_P (type))
9515 {
9516 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9517 lang_printable_name (type, 0));
9518 return 1;
9519 }
9520 }
9521 /* `q' might have changed due to a after package resolution
9522 re-qualification */
9523 if (!q)
9524 break;
9525 }
9526 *found_decl = decl;
9527 return 0;
9528 }
9529
9530 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9531 can't be accessed from REFERENCE (a record type). If MEMBER
9532 features a protected access, we then use WHERE which, if non null,
9533 holds the type of MEMBER's access that is checked against
9534 6.6.2.1. This function should be used when decl is a field or a
9535 method. */
9536
9537 static int
9538 not_accessible_p (reference, member, where, from_super)
9539 tree reference, member;
9540 tree where;
9541 int from_super;
9542 {
9543 int access_flag = get_access_flags_from_decl (member);
9544
9545 /* Inner classes are processed by check_inner_class_access */
9546 if (INNER_CLASS_TYPE_P (reference))
9547 return 0;
9548
9549 /* Access always granted for members declared public */
9550 if (access_flag & ACC_PUBLIC)
9551 return 0;
9552
9553 /* Check access on protected members */
9554 if (access_flag & ACC_PROTECTED)
9555 {
9556 /* Access granted if it occurs from within the package
9557 containing the class in which the protected member is
9558 declared */
9559 if (class_in_current_package (DECL_CONTEXT (member)))
9560 return 0;
9561
9562 /* If accessed with the form `super.member', then access is granted */
9563 if (from_super)
9564 return 0;
9565
9566 /* If where is active, access was made through a
9567 qualifier. Access is granted if the type of the qualifier is
9568 or is a sublass of the type the access made from (6.6.2.1.) */
9569 if (where && !inherits_from_p (where, reference))
9570 return 1;
9571
9572 /* Otherwise, access is granted if occuring from the class where
9573 member is declared or a subclass of it. Find the right
9574 context to perform the check */
9575 if (PURE_INNER_CLASS_TYPE_P (reference))
9576 {
9577 while (INNER_CLASS_TYPE_P (reference))
9578 {
9579 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9580 return 0;
9581 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9582 }
9583 }
9584 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9585 return 0;
9586 return 1;
9587 }
9588
9589 /* Check access on private members. Access is granted only if it
9590 occurs from within the class in which it is declared -- that does
9591 it for innerclasses too. */
9592 if (access_flag & ACC_PRIVATE)
9593 {
9594 if (reference == DECL_CONTEXT (member))
9595 return 0;
9596 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9597 return 0;
9598 return 1;
9599 }
9600
9601 /* Default access are permitted only when occuring within the
9602 package in which the type (REFERENCE) is declared. In other words,
9603 REFERENCE is defined in the current package */
9604 if (ctxp->package)
9605 return !class_in_current_package (reference);
9606
9607 /* Otherwise, access is granted */
9608 return 0;
9609 }
9610
9611 /* Test deprecated decl access. */
9612 static void
9613 check_deprecation (wfl, decl)
9614 tree wfl, decl;
9615 {
9616 const char *file = DECL_SOURCE_FILE (decl);
9617 /* Complain if the field is deprecated and the file it was defined
9618 in isn't compiled at the same time the file which contains its
9619 use is */
9620 if (DECL_DEPRECATED (decl)
9621 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9622 {
9623 char the [20];
9624 switch (TREE_CODE (decl))
9625 {
9626 case FUNCTION_DECL:
9627 strcpy (the, "method");
9628 break;
9629 case FIELD_DECL:
9630 strcpy (the, "field");
9631 break;
9632 case TYPE_DECL:
9633 strcpy (the, "class");
9634 break;
9635 default:
9636 abort ();
9637 }
9638 parse_warning_context
9639 (wfl, "The %s `%s' in class `%s' has been deprecated",
9640 the, lang_printable_name (decl, 0),
9641 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9642 }
9643 }
9644
9645 /* Returns 1 if class was declared in the current package, 0 otherwise */
9646
9647 static int
9648 class_in_current_package (class)
9649 tree class;
9650 {
9651 static tree cache = NULL_TREE;
9652 int qualified_flag;
9653 tree left;
9654
9655 if (cache == class)
9656 return 1;
9657
9658 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9659
9660 /* If the current package is empty and the name of CLASS is
9661 qualified, class isn't in the current package. If there is a
9662 current package and the name of the CLASS is not qualified, class
9663 isn't in the current package */
9664 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9665 return 0;
9666
9667 /* If there is not package and the name of CLASS isn't qualified,
9668 they belong to the same unnamed package */
9669 if (!ctxp->package && !qualified_flag)
9670 return 1;
9671
9672 /* Compare the left part of the name of CLASS with the package name */
9673 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9674 if (ctxp->package == left)
9675 {
9676 static int initialized_p;
9677 /* Register CACHE with the garbage collector. */
9678 if (!initialized_p)
9679 {
9680 ggc_add_tree_root (&cache, 1);
9681 initialized_p = 1;
9682 }
9683
9684 cache = class;
9685 return 1;
9686 }
9687 return 0;
9688 }
9689
9690 /* This function may generate code to access DECL from WHERE. This is
9691 done only if certain conditions meet. */
9692
9693 static tree
9694 maybe_access_field (decl, where, type)
9695 tree decl, where, type;
9696 {
9697 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9698 && !FIELD_STATIC (decl))
9699 decl = build_field_ref (where ? where : current_this,
9700 (type ? type : DECL_CONTEXT (decl)),
9701 DECL_NAME (decl));
9702 return decl;
9703 }
9704
9705 /* Build a method invocation, by patching PATCH. If non NULL
9706 and according to the situation, PRIMARY and WHERE may be
9707 used. IS_STATIC is set to 1 if the invoked function is static. */
9708
9709 static tree
9710 patch_method_invocation (patch, primary, where, from_super,
9711 is_static, ret_decl)
9712 tree patch, primary, where;
9713 int from_super;
9714 int *is_static;
9715 tree *ret_decl;
9716 {
9717 tree wfl = TREE_OPERAND (patch, 0);
9718 tree args = TREE_OPERAND (patch, 1);
9719 tree name = EXPR_WFL_NODE (wfl);
9720 tree list;
9721 int is_static_flag = 0;
9722 int is_super_init = 0;
9723 tree this_arg = NULL_TREE;
9724 int is_array_clone_call = 0;
9725
9726 /* Should be overriden if everything goes well. Otherwise, if
9727 something fails, it should keep this value. It stop the
9728 evaluation of a bogus assignment. See java_complete_tree,
9729 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9730 evaluating an assignment */
9731 TREE_TYPE (patch) = error_mark_node;
9732
9733 /* Since lookup functions are messing with line numbers, save the
9734 context now. */
9735 java_parser_context_save_global ();
9736
9737 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9738
9739 /* Resolution of qualified name, excluding constructors */
9740 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9741 {
9742 tree identifier, identifier_wfl, type, resolved;
9743 /* Extract the last IDENTIFIER of the qualified
9744 expression. This is a wfl and we will use it's location
9745 data during error report. */
9746 identifier_wfl = cut_identifier_in_qualified (wfl);
9747 identifier = EXPR_WFL_NODE (identifier_wfl);
9748
9749 /* Given the context, IDENTIFIER is syntactically qualified
9750 as a MethodName. We need to qualify what's before */
9751 qualify_ambiguous_name (wfl);
9752 resolved = resolve_field_access (wfl, NULL, NULL);
9753
9754 if (resolved == error_mark_node)
9755 PATCH_METHOD_RETURN_ERROR ();
9756
9757 type = GET_SKIP_TYPE (resolved);
9758 resolve_and_layout (type, NULL_TREE);
9759
9760 if (JPRIMITIVE_TYPE_P (type))
9761 {
9762 parse_error_context
9763 (identifier_wfl,
9764 "Can't invoke a method on primitive type `%s'",
9765 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9766 PATCH_METHOD_RETURN_ERROR ();
9767 }
9768
9769 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9770 args = nreverse (args);
9771
9772 /* We're resolving a call from a type */
9773 if (TREE_CODE (resolved) == TYPE_DECL)
9774 {
9775 if (CLASS_INTERFACE (resolved))
9776 {
9777 parse_error_context
9778 (identifier_wfl,
9779 "Can't make static reference to method `%s' in interface `%s'",
9780 IDENTIFIER_POINTER (identifier),
9781 IDENTIFIER_POINTER (name));
9782 PATCH_METHOD_RETURN_ERROR ();
9783 }
9784 if (list && !METHOD_STATIC (list))
9785 {
9786 char *fct_name = xstrdup (lang_printable_name (list, 0));
9787 parse_error_context
9788 (identifier_wfl,
9789 "Can't make static reference to method `%s %s' in class `%s'",
9790 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9791 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9792 free (fct_name);
9793 PATCH_METHOD_RETURN_ERROR ();
9794 }
9795 }
9796 else
9797 this_arg = primary = resolved;
9798
9799 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
9800 is_array_clone_call = 1;
9801
9802 /* IDENTIFIER_WFL will be used to report any problem further */
9803 wfl = identifier_wfl;
9804 }
9805 /* Resolution of simple names, names generated after a primary: or
9806 constructors */
9807 else
9808 {
9809 tree class_to_search = NULL_TREE;
9810 int lc; /* Looking for Constructor */
9811
9812 /* We search constructor in their target class */
9813 if (CALL_CONSTRUCTOR_P (patch))
9814 {
9815 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9816 class_to_search = EXPR_WFL_NODE (wfl);
9817 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9818 this_identifier_node)
9819 class_to_search = NULL_TREE;
9820 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9821 super_identifier_node)
9822 {
9823 is_super_init = 1;
9824 if (CLASSTYPE_SUPER (current_class))
9825 class_to_search =
9826 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9827 else
9828 {
9829 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9830 PATCH_METHOD_RETURN_ERROR ();
9831 }
9832 }
9833
9834 /* Class to search is NULL if we're searching the current one */
9835 if (class_to_search)
9836 {
9837 class_to_search = resolve_and_layout (class_to_search, wfl);
9838
9839 if (!class_to_search)
9840 {
9841 parse_error_context
9842 (wfl, "Class `%s' not found in type declaration",
9843 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9844 PATCH_METHOD_RETURN_ERROR ();
9845 }
9846
9847 /* Can't instantiate an abstract class, but we can
9848 invoke it's constructor. It's use within the `new'
9849 context is denied here. */
9850 if (CLASS_ABSTRACT (class_to_search)
9851 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9852 {
9853 parse_error_context
9854 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9855 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9856 PATCH_METHOD_RETURN_ERROR ();
9857 }
9858
9859 class_to_search = TREE_TYPE (class_to_search);
9860 }
9861 else
9862 class_to_search = current_class;
9863 lc = 1;
9864 }
9865 /* This is a regular search in the local class, unless an
9866 alternate class is specified. */
9867 else
9868 {
9869 class_to_search = (where ? where : current_class);
9870 lc = 0;
9871 }
9872
9873 /* NAME is a simple identifier or comes from a primary. Search
9874 in the class whose declaration contain the method being
9875 invoked. */
9876 resolve_and_layout (class_to_search, NULL_TREE);
9877
9878 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9879 /* Don't continue if no method were found, as the next statement
9880 can't be executed then. */
9881 if (!list)
9882 PATCH_METHOD_RETURN_ERROR ();
9883
9884 if (TYPE_ARRAY_P (class_to_search)
9885 && DECL_NAME (list) == get_identifier ("clone"))
9886 is_array_clone_call = 1;
9887
9888 /* Check for static reference if non static methods */
9889 if (check_for_static_method_reference (wfl, patch, list,
9890 class_to_search, primary))
9891 PATCH_METHOD_RETURN_ERROR ();
9892
9893 /* Check for inner classes creation from illegal contexts */
9894 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9895 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9896 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9897 {
9898 parse_error_context
9899 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9900 lang_printable_name (class_to_search, 0),
9901 (!current_this ? "" :
9902 "; an explicit one must be provided when creating this inner class"));
9903 PATCH_METHOD_RETURN_ERROR ();
9904 }
9905
9906 /* Non static methods are called with the current object extra
9907 argument. If patch a `new TYPE()', the argument is the value
9908 returned by the object allocator. If method is resolved as a
9909 primary, use the primary otherwise use the current THIS. */
9910 args = nreverse (args);
9911 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9912 {
9913 this_arg = primary ? primary : current_this;
9914
9915 /* If we're using an access method, things are different.
9916 There are two familly of cases:
9917
9918 1) We're not generating bytecodes:
9919
9920 - LIST is non static. It's invocation is transformed from
9921 x(a1,...,an) into this$<n>.x(a1,....an).
9922 - LIST is static. It's invocation is transformed from
9923 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9924
9925 2) We're generating bytecodes:
9926
9927 - LIST is non static. It's invocation is transformed from
9928 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9929 - LIST is static. It's invocation is transformed from
9930 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9931
9932 Of course, this$<n> can be abitrary complex, ranging from
9933 this$0 (the immediate outer context) to
9934 access$0(access$0(...(this$0))).
9935
9936 maybe_use_access_method returns a non zero value if the
9937 this_arg has to be moved into the (then generated) stub
9938 argument list. In the meantime, the selected function
9939 might have be replaced by a generated stub. */
9940 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9941 {
9942 args = tree_cons (NULL_TREE, this_arg, args);
9943 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9944 }
9945 }
9946 }
9947
9948 /* Merge point of all resolution schemes. If we have nothing, this
9949 is an error, already signaled */
9950 if (!list)
9951 PATCH_METHOD_RETURN_ERROR ();
9952
9953 /* Check accessibility, position the is_static flag, build and
9954 return the call */
9955 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
9956 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
9957 NULL_TREE), from_super)
9958 /* Calls to clone() on array types are permitted as a special-case. */
9959 && !is_array_clone_call)
9960 {
9961 char *fct_name = (char *) IDENTIFIER_POINTER (DECL_NAME (list));
9962 char *access = java_accstring_lookup (get_access_flags_from_decl (list));
9963 char *klass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
9964 char *refklass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
9965 char *what = (char *) (DECL_CONSTRUCTOR_P (list)
9966 ? "constructor" : "method");
9967 /* FIXME: WFL yields the wrong message here but I don't know
9968 what else to use. */
9969 parse_error_context (wfl,
9970 "Can't access %s %s `%s.%s' from `%s'",
9971 access, what, klass, fct_name, refklass);
9972 PATCH_METHOD_RETURN_ERROR ();
9973 }
9974 check_deprecation (wfl, list);
9975
9976 /* If invoking a innerclass constructor, there are hidden parameters
9977 to pass */
9978 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9979 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9980 {
9981 /* And make sure we add the accessed local variables to be saved
9982 in field aliases. */
9983 args = build_alias_initializer_parameter_list
9984 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9985
9986 /* Secretly pass the current_this/primary as a second argument */
9987 if (primary || current_this)
9988 {
9989 tree extra_arg;
9990 tree this_type = (current_this ?
9991 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
9992 /* Method's (list) enclosing context */
9993 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
9994 /* If we have a primary, use it. */
9995 if (primary)
9996 extra_arg = primary;
9997 /* The current `this' is an inner class but isn't a direct
9998 enclosing context for the inner class we're trying to
9999 create. Build an access to the proper enclosing context
10000 and use it. */
10001 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10002 && this_type != TREE_TYPE (mec))
10003 {
10004
10005 extra_arg = build_access_to_thisn (current_class,
10006 TREE_TYPE (mec), 0);
10007 extra_arg = java_complete_tree (extra_arg);
10008 }
10009 /* Otherwise, just use the current `this' as an enclosing
10010 context. */
10011 else
10012 extra_arg = current_this;
10013 args = tree_cons (NULL_TREE, extra_arg, args);
10014 }
10015 else
10016 args = tree_cons (NULL_TREE, integer_zero_node, args);
10017 }
10018
10019 /* This handles the situation where a constructor invocation needs
10020 to have an enclosing context passed as a second parameter (the
10021 constructor is one of an inner class. We extract it from the
10022 current function. */
10023 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10024 {
10025 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10026 tree extra_arg;
10027
10028 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10029 {
10030 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10031 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10032 }
10033 else
10034 {
10035 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10036 extra_arg =
10037 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10038 extra_arg = java_complete_tree (extra_arg);
10039 }
10040 args = tree_cons (NULL_TREE, extra_arg, args);
10041 }
10042
10043 is_static_flag = METHOD_STATIC (list);
10044 if (! is_static_flag && this_arg != NULL_TREE)
10045 args = tree_cons (NULL_TREE, this_arg, args);
10046
10047 /* In the context of an explicit constructor invocation, we can't
10048 invoke any method relying on `this'. Exceptions are: we're
10049 invoking a static function, primary exists and is not the current
10050 this, we're creating a new object. */
10051 if (ctxp->explicit_constructor_p
10052 && !is_static_flag
10053 && (!primary || primary == current_this)
10054 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10055 {
10056 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10057 PATCH_METHOD_RETURN_ERROR ();
10058 }
10059 java_parser_context_restore_global ();
10060 if (is_static)
10061 *is_static = is_static_flag;
10062 /* Sometimes, we want the decl of the selected method. Such as for
10063 EH checking */
10064 if (ret_decl)
10065 *ret_decl = list;
10066 patch = patch_invoke (patch, list, args);
10067 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10068 {
10069 tree finit_parms, finit_call;
10070
10071 /* Prepare to pass hidden parameters to finit$, if any. */
10072 finit_parms = build_alias_initializer_parameter_list
10073 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10074
10075 finit_call =
10076 build_method_invocation (build_wfl_node (finit_identifier_node),
10077 finit_parms);
10078
10079 /* Generate the code used to initialize fields declared with an
10080 initialization statement and build a compound statement along
10081 with the super constructor invocation. */
10082 patch = build (COMPOUND_EXPR, void_type_node, patch,
10083 java_complete_tree (finit_call));
10084 CAN_COMPLETE_NORMALLY (patch) = 1;
10085 }
10086 return patch;
10087 }
10088
10089 /* Check that we're not trying to do a static reference to a method in
10090 non static method. Return 1 if it's the case, 0 otherwise. */
10091
10092 static int
10093 check_for_static_method_reference (wfl, node, method, where, primary)
10094 tree wfl, node, method, where, primary;
10095 {
10096 if (METHOD_STATIC (current_function_decl)
10097 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10098 {
10099 char *fct_name = xstrdup (lang_printable_name (method, 0));
10100 parse_error_context
10101 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10102 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10103 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10104 free (fct_name);
10105 return 1;
10106 }
10107 return 0;
10108 }
10109
10110 /* Fix the invocation of *MDECL if necessary in the case of a
10111 invocation from an inner class. *THIS_ARG might be modified
10112 appropriately and an alternative access to *MDECL might be
10113 returned. */
10114
10115 static int
10116 maybe_use_access_method (is_super_init, mdecl, this_arg)
10117 int is_super_init;
10118 tree *mdecl, *this_arg;
10119 {
10120 tree ctx;
10121 tree md = *mdecl, ta = *this_arg;
10122 int to_return = 0;
10123 int non_static_context = !METHOD_STATIC (md);
10124
10125 if (is_super_init
10126 || DECL_CONTEXT (md) == current_class
10127 || !PURE_INNER_CLASS_TYPE_P (current_class)
10128 || DECL_FINIT_P (md))
10129 return 0;
10130
10131 /* If we're calling a method found in an enclosing class, generate
10132 what it takes to retrieve the right this. Don't do that if we're
10133 invoking a static method. Note that if MD's type is unrelated to
10134 CURRENT_CLASS, then the current this can be used. */
10135
10136 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10137 {
10138 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10139 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10140 {
10141 ta = build_current_thisn (current_class);
10142 ta = build_wfl_node (ta);
10143 }
10144 else
10145 {
10146 tree type = ctx;
10147 while (type)
10148 {
10149 maybe_build_thisn_access_method (type);
10150 if (inherits_from_p (type, DECL_CONTEXT (md)))
10151 {
10152 ta = build_access_to_thisn (ctx, type, 0);
10153 break;
10154 }
10155 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10156 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10157 }
10158 }
10159 ta = java_complete_tree (ta);
10160 }
10161
10162 /* We might have to use an access method to get to MD. We can
10163 break the method access rule as far as we're not generating
10164 bytecode */
10165 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10166 {
10167 md = build_outer_method_access_method (md);
10168 to_return = 1;
10169 }
10170
10171 *mdecl = md;
10172 *this_arg = ta;
10173
10174 /* Returnin a non zero value indicates we were doing a non static
10175 method invokation that is now a static invocation. It will have
10176 callee displace `this' to insert it in the regular argument
10177 list. */
10178 return (non_static_context && to_return);
10179 }
10180
10181 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10182 mode. */
10183
10184 static tree
10185 patch_invoke (patch, method, args)
10186 tree patch, method, args;
10187 {
10188 tree dtable, func;
10189 tree original_call, t, ta;
10190 tree cond = NULL_TREE;
10191
10192 /* Last step for args: convert build-in types. If we're dealing with
10193 a new TYPE() type call, the first argument to the constructor
10194 isn't found in the incoming argument list, but delivered by
10195 `new' */
10196 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10197 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10198 t = TREE_CHAIN (t);
10199 for (ta = args; t != end_params_node && ta;
10200 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10201 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10202 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10203 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10204
10205 /* Resolve unresolved returned type isses */
10206 t = TREE_TYPE (TREE_TYPE (method));
10207 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10208 resolve_and_layout (TREE_TYPE (t), NULL);
10209
10210 if (flag_emit_class_files || flag_emit_xref)
10211 func = method;
10212 else
10213 {
10214 tree signature = build_java_signature (TREE_TYPE (method));
10215 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10216 {
10217 case INVOKE_VIRTUAL:
10218 dtable = invoke_build_dtable (0, args);
10219 func = build_invokevirtual (dtable, method);
10220 break;
10221
10222 case INVOKE_NONVIRTUAL:
10223 /* If the object for the method call is null, we throw an
10224 exception. We don't do this if the object is the current
10225 method's `this'. In other cases we just rely on an
10226 optimization pass to eliminate redundant checks. */
10227 if (TREE_VALUE (args) != current_this)
10228 {
10229 /* We use a SAVE_EXPR here to make sure we only evaluate
10230 the new `self' expression once. */
10231 tree save_arg = save_expr (TREE_VALUE (args));
10232 TREE_VALUE (args) = save_arg;
10233 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10234 null_pointer_node);
10235 }
10236 /* Fall through. */
10237
10238 case INVOKE_SUPER:
10239 case INVOKE_STATIC:
10240 func = build_known_method_ref (method, TREE_TYPE (method),
10241 DECL_CONTEXT (method),
10242 signature, args);
10243 break;
10244
10245 case INVOKE_INTERFACE:
10246 dtable = invoke_build_dtable (1, args);
10247 func = build_invokeinterface (dtable, method);
10248 break;
10249
10250 default:
10251 abort ();
10252 }
10253
10254 /* Ensure self_type is initialized, (invokestatic). FIXME */
10255 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10256 }
10257
10258 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10259 TREE_OPERAND (patch, 0) = func;
10260 TREE_OPERAND (patch, 1) = args;
10261 original_call = patch;
10262
10263 /* We're processing a `new TYPE ()' form. New is called and its
10264 returned value is the first argument to the constructor. We build
10265 a COMPOUND_EXPR and use saved expression so that the overall NEW
10266 expression value is a pointer to a newly created and initialized
10267 class. */
10268 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10269 {
10270 tree class = DECL_CONTEXT (method);
10271 tree c1, saved_new, size, new;
10272 if (flag_emit_class_files || flag_emit_xref)
10273 {
10274 TREE_TYPE (patch) = build_pointer_type (class);
10275 return patch;
10276 }
10277 if (!TYPE_SIZE (class))
10278 safe_layout_class (class);
10279 size = size_in_bytes (class);
10280 new = build (CALL_EXPR, promote_type (class),
10281 build_address_of (alloc_object_node),
10282 tree_cons (NULL_TREE, build_class_ref (class),
10283 build_tree_list (NULL_TREE,
10284 size_in_bytes (class))),
10285 NULL_TREE);
10286 saved_new = save_expr (new);
10287 c1 = build_tree_list (NULL_TREE, saved_new);
10288 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10289 TREE_OPERAND (original_call, 1) = c1;
10290 TREE_SET_CODE (original_call, CALL_EXPR);
10291 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10292 }
10293
10294 /* If COND is set, then we are building a check to see if the object
10295 is NULL. */
10296 if (cond != NULL_TREE)
10297 {
10298 /* We have to make the `then' branch a compound expression to
10299 make the types turn out right. This seems bizarre. */
10300 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10301 build (COMPOUND_EXPR, TREE_TYPE (patch),
10302 build (CALL_EXPR, void_type_node,
10303 build_address_of (soft_nullpointer_node),
10304 NULL_TREE, NULL_TREE),
10305 (FLOAT_TYPE_P (TREE_TYPE (patch))
10306 ? build_real (TREE_TYPE (patch), dconst0)
10307 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10308 integer_zero_node))),
10309 patch);
10310 TREE_SIDE_EFFECTS (patch) = 1;
10311 }
10312
10313 return patch;
10314 }
10315
10316 static int
10317 invocation_mode (method, super)
10318 tree method;
10319 int super;
10320 {
10321 int access = get_access_flags_from_decl (method);
10322
10323 if (super)
10324 return INVOKE_SUPER;
10325
10326 if (access & ACC_STATIC)
10327 return INVOKE_STATIC;
10328
10329 /* We have to look for a constructor before we handle nonvirtual
10330 calls; otherwise the constructor will look nonvirtual. */
10331 if (DECL_CONSTRUCTOR_P (method))
10332 return INVOKE_STATIC;
10333
10334 if (access & ACC_FINAL || access & ACC_PRIVATE)
10335 return INVOKE_NONVIRTUAL;
10336
10337 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10338 return INVOKE_NONVIRTUAL;
10339
10340 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10341 return INVOKE_INTERFACE;
10342
10343 return INVOKE_VIRTUAL;
10344 }
10345
10346 /* Retrieve a refined list of matching methods. It covers the step
10347 15.11.2 (Compile-Time Step 2) */
10348
10349 static tree
10350 lookup_method_invoke (lc, cl, class, name, arg_list)
10351 int lc;
10352 tree cl;
10353 tree class, name, arg_list;
10354 {
10355 tree atl = end_params_node; /* Arg Type List */
10356 tree method, signature, list, node;
10357 const char *candidates; /* Used for error report */
10358 char *dup;
10359
10360 /* Fix the arguments */
10361 for (node = arg_list; node; node = TREE_CHAIN (node))
10362 {
10363 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10364 /* Non primitive type may have to be resolved */
10365 if (!JPRIMITIVE_TYPE_P (current_arg))
10366 resolve_and_layout (current_arg, NULL_TREE);
10367 /* And promoted */
10368 if (TREE_CODE (current_arg) == RECORD_TYPE)
10369 current_arg = promote_type (current_arg);
10370 atl = tree_cons (NULL_TREE, current_arg, atl);
10371 }
10372
10373 /* Presto. If we're dealing with an anonymous class and a
10374 constructor call, generate the right constructor now, since we
10375 know the arguments' types. */
10376
10377 if (lc && ANONYMOUS_CLASS_P (class))
10378 craft_constructor (TYPE_NAME (class), atl);
10379
10380 /* Find all candidates and then refine the list, searching for the
10381 most specific method. */
10382 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10383 list = find_most_specific_methods_list (list);
10384 if (list && !TREE_CHAIN (list))
10385 return TREE_VALUE (list);
10386
10387 /* Issue an error. List candidates if any. Candidates are listed
10388 only if accessible (non accessible methods may end-up here for
10389 the sake of a better error report). */
10390 candidates = NULL;
10391 if (list)
10392 {
10393 tree current;
10394 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10395 for (current = list; current; current = TREE_CHAIN (current))
10396 {
10397 tree cm = TREE_VALUE (current);
10398 char string [4096];
10399 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10400 continue;
10401 sprintf
10402 (string, " `%s' in `%s'%s",
10403 get_printable_method_name (cm),
10404 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10405 (TREE_CHAIN (current) ? "\n" : ""));
10406 obstack_grow (&temporary_obstack, string, strlen (string));
10407 }
10408 obstack_1grow (&temporary_obstack, '\0');
10409 candidates = obstack_finish (&temporary_obstack);
10410 }
10411 /* Issue the error message */
10412 method = make_node (FUNCTION_TYPE);
10413 TYPE_ARG_TYPES (method) = atl;
10414 signature = build_java_argument_signature (method);
10415 dup = xstrdup (lang_printable_name (class, 0));
10416 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10417 (lc ? "constructor" : "method"),
10418 (lc ? dup : IDENTIFIER_POINTER (name)),
10419 IDENTIFIER_POINTER (signature), dup,
10420 (candidates ? candidates : ""));
10421 free (dup);
10422 return NULL_TREE;
10423 }
10424
10425 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10426 when we're looking for a constructor. */
10427
10428 static tree
10429 find_applicable_accessible_methods_list (lc, class, name, arglist)
10430 int lc;
10431 tree class, name, arglist;
10432 {
10433 static struct hash_table t, *searched_classes = NULL;
10434 static int search_not_done = 0;
10435 tree list = NULL_TREE, all_list = NULL_TREE;
10436
10437 /* Check the hash table to determine if this class has been searched
10438 already. */
10439 if (searched_classes)
10440 {
10441 if (hash_lookup (searched_classes,
10442 (const hash_table_key) class, FALSE, NULL))
10443 return NULL;
10444 }
10445 else
10446 {
10447 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10448 java_hash_compare_tree_node);
10449 searched_classes = &t;
10450 }
10451
10452 search_not_done++;
10453 hash_lookup (searched_classes,
10454 (const hash_table_key) class, TRUE, NULL);
10455
10456 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10457 {
10458 load_class (class, 1);
10459 safe_layout_class (class);
10460 }
10461
10462 /* Search interfaces */
10463 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10464 && CLASS_INTERFACE (TYPE_NAME (class)))
10465 {
10466 int i, n;
10467 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10468 search_applicable_methods_list (lc, TYPE_METHODS (class),
10469 name, arglist, &list, &all_list);
10470 n = TREE_VEC_LENGTH (basetype_vec);
10471 for (i = 1; i < n; i++)
10472 {
10473 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10474 tree rlist;
10475
10476 rlist = find_applicable_accessible_methods_list (lc, t, name,
10477 arglist);
10478 list = chainon (rlist, list);
10479 }
10480 }
10481 /* Search classes */
10482 else
10483 {
10484 tree sc = class;
10485 int seen_inner_class = 0;
10486 search_applicable_methods_list (lc, TYPE_METHODS (class),
10487 name, arglist, &list, &all_list);
10488
10489 /* When looking finit$ or class$, we turn LC to 1 so that we
10490 only search in class. Note that we should have found
10491 something at this point. */
10492 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name))
10493 {
10494 lc = 1;
10495 if (!list)
10496 abort ();
10497 }
10498
10499 /* We must search all interfaces of this class */
10500 if (!lc)
10501 {
10502 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10503 int n = TREE_VEC_LENGTH (basetype_vec), i;
10504 for (i = 1; i < n; i++)
10505 {
10506 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10507 if (t != object_type_node)
10508 {
10509 tree rlist
10510 = find_applicable_accessible_methods_list (lc, t,
10511 name, arglist);
10512 list = chainon (rlist, list);
10513 }
10514 }
10515 }
10516
10517 /* Search enclosing context of inner classes before looking
10518 ancestors up. */
10519 while (!lc && INNER_CLASS_TYPE_P (class))
10520 {
10521 tree rlist;
10522 seen_inner_class = 1;
10523 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10524 rlist = find_applicable_accessible_methods_list (lc, class,
10525 name, arglist);
10526 list = chainon (rlist, list);
10527 }
10528
10529 if (!lc && seen_inner_class
10530 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10531 class = CLASSTYPE_SUPER (sc);
10532 else
10533 class = sc;
10534
10535 /* Search superclass */
10536 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10537 {
10538 tree rlist;
10539 class = CLASSTYPE_SUPER (class);
10540 rlist = find_applicable_accessible_methods_list (lc, class,
10541 name, arglist);
10542 list = chainon (rlist, list);
10543 }
10544 }
10545
10546 search_not_done--;
10547
10548 /* We're done. Reset the searched classes list and finally search
10549 java.lang.Object if it wasn't searched already. */
10550 if (!search_not_done)
10551 {
10552 if (!lc
10553 && TYPE_METHODS (object_type_node)
10554 && !hash_lookup (searched_classes,
10555 (const hash_table_key) object_type_node,
10556 FALSE, NULL))
10557 {
10558 search_applicable_methods_list (lc,
10559 TYPE_METHODS (object_type_node),
10560 name, arglist, &list, &all_list);
10561 }
10562 hash_table_free (searched_classes);
10563 searched_classes = NULL;
10564 }
10565
10566 /* Either return the list obtained or all selected (but
10567 inaccessible) methods for better error report. */
10568 return (!list ? all_list : list);
10569 }
10570
10571 /* Effectively search for the appropriate method in method */
10572
10573 static void
10574 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10575 int lc;
10576 tree method, name, arglist;
10577 tree *list, *all_list;
10578 {
10579 for (; method; method = TREE_CHAIN (method))
10580 {
10581 /* When dealing with constructor, stop here, otherwise search
10582 other classes */
10583 if (lc && !DECL_CONSTRUCTOR_P (method))
10584 continue;
10585 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10586 || (DECL_NAME (method) != name)))
10587 continue;
10588
10589 if (argument_types_convertible (method, arglist))
10590 {
10591 /* Retain accessible methods only */
10592 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10593 method, NULL_TREE, 0))
10594 *list = tree_cons (NULL_TREE, method, *list);
10595 else
10596 /* Also retain all selected method here */
10597 *all_list = tree_cons (NULL_TREE, method, *list);
10598 }
10599 }
10600 }
10601
10602 /* 15.11.2.2 Choose the Most Specific Method */
10603
10604 static tree
10605 find_most_specific_methods_list (list)
10606 tree list;
10607 {
10608 int max = 0;
10609 int abstract, candidates;
10610 tree current, new_list = NULL_TREE;
10611 for (current = list; current; current = TREE_CHAIN (current))
10612 {
10613 tree method;
10614 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10615
10616 for (method = list; method; method = TREE_CHAIN (method))
10617 {
10618 tree method_v, current_v;
10619 /* Don't test a method against itself */
10620 if (method == current)
10621 continue;
10622
10623 method_v = TREE_VALUE (method);
10624 current_v = TREE_VALUE (current);
10625
10626 /* Compare arguments and location where methods where declared */
10627 if (argument_types_convertible (method_v, current_v))
10628 {
10629 if (valid_method_invocation_conversion_p
10630 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10631 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10632 && enclosing_context_p (DECL_CONTEXT (method_v),
10633 DECL_CONTEXT (current_v))))
10634 {
10635 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10636 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10637 max = (v > max ? v : max);
10638 }
10639 }
10640 }
10641 }
10642
10643 /* Review the list and select the maximally specific methods */
10644 for (current = list, abstract = -1, candidates = -1;
10645 current; current = TREE_CHAIN (current))
10646 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10647 {
10648 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10649 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10650 candidates++;
10651 }
10652
10653 /* If we have several and they're all abstract, just pick the
10654 closest one. */
10655 if (candidates > 0 && (candidates == abstract))
10656 {
10657 new_list = nreverse (new_list);
10658 TREE_CHAIN (new_list) = NULL_TREE;
10659 }
10660
10661 /* We have several (we couldn't find a most specific), all but one
10662 are abstract, we pick the only non abstract one. */
10663 if (candidates > 0 && (candidates == abstract+1))
10664 {
10665 for (current = new_list; current; current = TREE_CHAIN (current))
10666 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10667 {
10668 TREE_CHAIN (current) = NULL_TREE;
10669 new_list = current;
10670 }
10671 }
10672
10673 /* If we can't find one, lower expectations and try to gather multiple
10674 maximally specific methods */
10675 while (!new_list && max)
10676 {
10677 while (--max > 0)
10678 {
10679 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10680 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10681 }
10682 }
10683
10684 return new_list;
10685 }
10686
10687 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10688 converted by method invocation conversion (5.3) to the type of the
10689 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10690 to change less often than M1. */
10691
10692 static int
10693 argument_types_convertible (m1, m2_or_arglist)
10694 tree m1, m2_or_arglist;
10695 {
10696 static tree m2_arg_value = NULL_TREE;
10697 static tree m2_arg_cache = NULL_TREE;
10698 static int initialized_p;
10699
10700 register tree m1_arg, m2_arg;
10701
10702 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10703 collector. */
10704 if (!initialized_p)
10705 {
10706 ggc_add_tree_root (&m2_arg_value, 1);
10707 ggc_add_tree_root (&m2_arg_cache, 1);
10708 initialized_p = 1;
10709 }
10710
10711 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10712
10713 if (m2_arg_value == m2_or_arglist)
10714 m2_arg = m2_arg_cache;
10715 else
10716 {
10717 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10718 argument types */
10719 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10720 {
10721 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10722 if (!METHOD_STATIC (m2_or_arglist))
10723 m2_arg = TREE_CHAIN (m2_arg);
10724 }
10725 else
10726 m2_arg = m2_or_arglist;
10727
10728 m2_arg_value = m2_or_arglist;
10729 m2_arg_cache = m2_arg;
10730 }
10731
10732 while (m1_arg != end_params_node && m2_arg != end_params_node)
10733 {
10734 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10735 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10736 TREE_VALUE (m2_arg)))
10737 break;
10738 m1_arg = TREE_CHAIN (m1_arg);
10739 m2_arg = TREE_CHAIN (m2_arg);
10740 }
10741 return m1_arg == end_params_node && m2_arg == end_params_node;
10742 }
10743
10744 /* Qualification routines */
10745
10746 static void
10747 qualify_ambiguous_name (id)
10748 tree id;
10749 {
10750 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10751 saved_current_class;
10752 int again, super_found = 0, this_found = 0, new_array_found = 0;
10753 int code;
10754
10755 /* We first qualify the first element, then derive qualification of
10756 others based on the first one. If the first element is qualified
10757 by a resolution (field or type), this resolution is stored in the
10758 QUAL_RESOLUTION of the qual element being examined. We need to
10759 save the current_class since the use of SUPER might change the
10760 its value. */
10761 saved_current_class = current_class;
10762 qual = EXPR_WFL_QUALIFICATION (id);
10763 do {
10764
10765 /* Simple qualified expression feature a qual_wfl that is a
10766 WFL. Expression derived from a primary feature more complicated
10767 things like a CALL_EXPR. Expression from primary need to be
10768 worked out to extract the part on which the qualification will
10769 take place. */
10770 qual_wfl = QUAL_WFL (qual);
10771 switch (TREE_CODE (qual_wfl))
10772 {
10773 case CALL_EXPR:
10774 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10775 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10776 {
10777 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10778 qual_wfl = QUAL_WFL (qual);
10779 }
10780 break;
10781 case NEW_ARRAY_EXPR:
10782 case NEW_ANONYMOUS_ARRAY_EXPR:
10783 qual = TREE_CHAIN (qual);
10784 again = new_array_found = 1;
10785 continue;
10786 case CONVERT_EXPR:
10787 break;
10788 case NEW_CLASS_EXPR:
10789 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10790 break;
10791 case ARRAY_REF:
10792 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10793 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10794 break;
10795 case STRING_CST:
10796 qual = TREE_CHAIN (qual);
10797 qual_wfl = QUAL_WFL (qual);
10798 break;
10799 case CLASS_LITERAL:
10800 qual = TREE_CHAIN (qual);
10801 qual_wfl = QUAL_WFL (qual);
10802 break;
10803 default:
10804 /* Fix for -Wall. Just break doing nothing */
10805 break;
10806 }
10807
10808 ptr_type = current_class;
10809 again = 0;
10810 code = TREE_CODE (qual_wfl);
10811
10812 /* Pos evaluation: non WFL leading expression nodes */
10813 if (code == CONVERT_EXPR
10814 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10815 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10816
10817 else if (code == INTEGER_CST)
10818 name = qual_wfl;
10819
10820 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10821 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10822 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10823
10824 else if (code == TREE_LIST)
10825 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10826
10827 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10828 || code == PLUS_EXPR)
10829 {
10830 qual = TREE_CHAIN (qual);
10831 qual_wfl = QUAL_WFL (qual);
10832 again = 1;
10833 }
10834 else
10835 {
10836 name = EXPR_WFL_NODE (qual_wfl);
10837 if (!name)
10838 {
10839 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10840 again = 1;
10841 }
10842 }
10843
10844 /* If we have a THIS (from a primary), we set the context accordingly */
10845 if (name == this_identifier_node)
10846 {
10847 /* This isn't really elegant. One more added irregularity
10848 before I start using COMPONENT_REF (hopefully very soon.) */
10849 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10850 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10851 EXPR_WITH_FILE_LOCATION
10852 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10853 this_identifier_node)
10854 {
10855 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10856 qual = EXPR_WFL_QUALIFICATION (qual);
10857 }
10858 qual = TREE_CHAIN (qual);
10859 qual_wfl = QUAL_WFL (qual);
10860 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10861 again = 1;
10862 else
10863 name = EXPR_WFL_NODE (qual_wfl);
10864 this_found = 1;
10865 }
10866 /* If we have a SUPER, we set the context accordingly */
10867 if (name == super_identifier_node)
10868 {
10869 current_class = CLASSTYPE_SUPER (ptr_type);
10870 /* Check that there is such a thing as a super class. If not,
10871 return. The error will be caught later on, during the
10872 resolution */
10873 if (!current_class)
10874 {
10875 current_class = saved_current_class;
10876 return;
10877 }
10878 qual = TREE_CHAIN (qual);
10879 /* Do one more interation to set things up */
10880 super_found = again = 1;
10881 }
10882 } while (again);
10883
10884 /* If name appears within the scope of a local variable declaration
10885 or parameter declaration, then it is an expression name. We don't
10886 carry this test out if we're in the context of the use of SUPER
10887 or THIS */
10888 if (!this_found && !super_found
10889 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10890 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10891 {
10892 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10893 QUAL_RESOLUTION (qual) = decl;
10894 }
10895
10896 /* If within the class/interface NAME was found to be used there
10897 exists a (possibly inherited) field named NAME, then this is an
10898 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10899 address length, it is OK. */
10900 else if ((decl = lookup_field_wrapper (ptr_type, name))
10901 || (new_array_found && name == length_identifier_node))
10902 {
10903 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10904 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10905 }
10906
10907 /* We reclassify NAME as yielding to a type name resolution if:
10908 - NAME is a class/interface declared within the compilation
10909 unit containing NAME,
10910 - NAME is imported via a single-type-import declaration,
10911 - NAME is declared in an another compilation unit of the package
10912 of the compilation unit containing NAME,
10913 - NAME is declared by exactly on type-import-on-demand declaration
10914 of the compilation unit containing NAME.
10915 - NAME is actually a STRING_CST. */
10916 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10917 || (decl = resolve_and_layout (name, NULL_TREE)))
10918 {
10919 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10920 QUAL_RESOLUTION (qual) = decl;
10921 }
10922
10923 /* Method call, array references and cast are expression name */
10924 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10925 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10926 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10927 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10928
10929 /* Check here that NAME isn't declared by more than one
10930 type-import-on-demand declaration of the compilation unit
10931 containing NAME. FIXME */
10932
10933 /* Otherwise, NAME is reclassified as a package name */
10934 else
10935 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10936
10937 /* Propagate the qualification accross other components of the
10938 qualified name */
10939 for (qual = TREE_CHAIN (qual); qual;
10940 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10941 {
10942 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10943 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10944 else
10945 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10946 }
10947
10948 /* Store the global qualification for the ambiguous part of ID back
10949 into ID fields */
10950 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10951 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10952 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10953 RESOLVE_TYPE_NAME_P (id) = 1;
10954 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10955 RESOLVE_PACKAGE_NAME_P (id) = 1;
10956
10957 /* Restore the current class */
10958 current_class = saved_current_class;
10959 }
10960
10961 static int
10962 breakdown_qualified (left, right, source)
10963 tree *left, *right, source;
10964 {
10965 char *p, *base;
10966 int l = IDENTIFIER_LENGTH (source);
10967
10968 base = alloca (l + 1);
10969 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10970
10971 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10972 p = base + l - 1;
10973 while (*p != '.' && p != base)
10974 p--;
10975
10976 /* We didn't find a '.'. Return an error */
10977 if (p == base)
10978 return 1;
10979
10980 *p = '\0';
10981 if (right)
10982 *right = get_identifier (p+1);
10983 *left = get_identifier (base);
10984
10985 return 0;
10986 }
10987
10988 /* Return TRUE if two classes are from the same package. */
10989
10990 static int
10991 in_same_package (name1, name2)
10992 tree name1, name2;
10993 {
10994 tree tmp;
10995 tree pkg1;
10996 tree pkg2;
10997
10998 if (TREE_CODE (name1) == TYPE_DECL)
10999 name1 = DECL_NAME (name1);
11000 if (TREE_CODE (name2) == TYPE_DECL)
11001 name2 = DECL_NAME (name2);
11002
11003 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11004 /* One in empty package. */
11005 return 0;
11006
11007 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11008 /* Both in empty package. */
11009 return 1;
11010
11011 breakdown_qualified (&pkg1, &tmp, name1);
11012 breakdown_qualified (&pkg2, &tmp, name2);
11013
11014 return (pkg1 == pkg2);
11015 }
11016
11017 /* Patch tree nodes in a function body. When a BLOCK is found, push
11018 local variable decls if present.
11019 Same as java_complete_lhs, but does resolve static finals to values. */
11020
11021 static tree
11022 java_complete_tree (node)
11023 tree node;
11024 {
11025 node = java_complete_lhs (node);
11026 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11027 && DECL_INITIAL (node) != NULL_TREE
11028 && !flag_emit_xref)
11029 {
11030 tree value = DECL_INITIAL (node);
11031 DECL_INITIAL (node) = NULL_TREE;
11032 value = fold_constant_for_init (value, node);
11033 DECL_INITIAL (node) = value;
11034 if (value != NULL_TREE)
11035 {
11036 /* fold_constant_for_init sometimes widen the original type
11037 of the constant (i.e. byte to int.) It's not desirable,
11038 especially if NODE is a function argument. */
11039 if (TREE_CODE (value) == INTEGER_CST
11040 && TREE_TYPE (node) != TREE_TYPE (value))
11041 return convert (TREE_TYPE (node), value);
11042 else
11043 return value;
11044 }
11045 else
11046 DECL_FIELD_FINAL_IUD (node) = 0;
11047 }
11048 return node;
11049 }
11050
11051 static tree
11052 java_stabilize_reference (node)
11053 tree node;
11054 {
11055 if (TREE_CODE (node) == COMPOUND_EXPR)
11056 {
11057 tree op0 = TREE_OPERAND (node, 0);
11058 tree op1 = TREE_OPERAND (node, 1);
11059 TREE_OPERAND (node, 0) = save_expr (op0);
11060 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11061 return node;
11062 }
11063 return stabilize_reference (node);
11064 }
11065
11066 /* Patch tree nodes in a function body. When a BLOCK is found, push
11067 local variable decls if present.
11068 Same as java_complete_tree, but does not resolve static finals to values. */
11069
11070 static tree
11071 java_complete_lhs (node)
11072 tree node;
11073 {
11074 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11075 int flag;
11076
11077 /* CONVERT_EXPR always has its type set, even though it needs to be
11078 worked out. */
11079 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11080 return node;
11081
11082 /* The switch block implements cases processing container nodes
11083 first. Contained nodes are always written back. Leaves come
11084 next and return a value. */
11085 switch (TREE_CODE (node))
11086 {
11087 case BLOCK:
11088
11089 /* 1- Block section.
11090 Set the local values on decl names so we can identify them
11091 faster when they're referenced. At that stage, identifiers
11092 are legal so we don't check for declaration errors. */
11093 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11094 {
11095 DECL_CONTEXT (cn) = current_function_decl;
11096 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11097 }
11098 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11099 CAN_COMPLETE_NORMALLY (node) = 1;
11100 else
11101 {
11102 tree stmt = BLOCK_EXPR_BODY (node);
11103 tree *ptr;
11104 int error_seen = 0;
11105 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11106 {
11107 /* Re-order from (((A; B); C); ...; Z) to
11108 (A; (B; (C ; (...; Z)))).
11109 This makes it easier to scan the statements left-to-right
11110 without using recursion (which might overflow the stack
11111 if the block has many statements. */
11112 for (;;)
11113 {
11114 tree left = TREE_OPERAND (stmt, 0);
11115 if (TREE_CODE (left) != COMPOUND_EXPR)
11116 break;
11117 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11118 TREE_OPERAND (left, 1) = stmt;
11119 stmt = left;
11120 }
11121 BLOCK_EXPR_BODY (node) = stmt;
11122 }
11123
11124 /* Now do the actual complete, without deep recursion for
11125 long blocks. */
11126 ptr = &BLOCK_EXPR_BODY (node);
11127 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11128 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11129 {
11130 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11131 tree *next = &TREE_OPERAND (*ptr, 1);
11132 TREE_OPERAND (*ptr, 0) = cur;
11133 if (cur == empty_stmt_node)
11134 {
11135 /* Optimization; makes it easier to detect empty bodies.
11136 Most useful for <clinit> with all-constant initializer. */
11137 *ptr = *next;
11138 continue;
11139 }
11140 if (TREE_CODE (cur) == ERROR_MARK)
11141 error_seen++;
11142 else if (! CAN_COMPLETE_NORMALLY (cur))
11143 {
11144 wfl_op2 = *next;
11145 for (;;)
11146 {
11147 if (TREE_CODE (wfl_op2) == BLOCK)
11148 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11149 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11150 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11151 else
11152 break;
11153 }
11154 if (TREE_CODE (wfl_op2) != CASE_EXPR
11155 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11156 unreachable_stmt_error (*ptr);
11157 }
11158 ptr = next;
11159 }
11160 *ptr = java_complete_tree (*ptr);
11161
11162 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11163 return error_mark_node;
11164 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11165 }
11166 /* Turn local bindings to null */
11167 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11168 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11169
11170 TREE_TYPE (node) = void_type_node;
11171 break;
11172
11173 /* 2- They are expressions but ultimately deal with statements */
11174
11175 case THROW_EXPR:
11176 wfl_op1 = TREE_OPERAND (node, 0);
11177 COMPLETE_CHECK_OP_0 (node);
11178 /* 14.19 A throw statement cannot complete normally. */
11179 CAN_COMPLETE_NORMALLY (node) = 0;
11180 return patch_throw_statement (node, wfl_op1);
11181
11182 case SYNCHRONIZED_EXPR:
11183 wfl_op1 = TREE_OPERAND (node, 0);
11184 return patch_synchronized_statement (node, wfl_op1);
11185
11186 case TRY_EXPR:
11187 return patch_try_statement (node);
11188
11189 case TRY_FINALLY_EXPR:
11190 COMPLETE_CHECK_OP_0 (node);
11191 COMPLETE_CHECK_OP_1 (node);
11192 CAN_COMPLETE_NORMALLY (node)
11193 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11194 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11195 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11196 return node;
11197
11198 case CLEANUP_POINT_EXPR:
11199 COMPLETE_CHECK_OP_0 (node);
11200 TREE_TYPE (node) = void_type_node;
11201 CAN_COMPLETE_NORMALLY (node) =
11202 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11203 return node;
11204
11205 case WITH_CLEANUP_EXPR:
11206 COMPLETE_CHECK_OP_0 (node);
11207 COMPLETE_CHECK_OP_2 (node);
11208 CAN_COMPLETE_NORMALLY (node) =
11209 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11210 TREE_TYPE (node) = void_type_node;
11211 return node;
11212
11213 case LABELED_BLOCK_EXPR:
11214 PUSH_LABELED_BLOCK (node);
11215 if (LABELED_BLOCK_BODY (node))
11216 COMPLETE_CHECK_OP_1 (node);
11217 TREE_TYPE (node) = void_type_node;
11218 POP_LABELED_BLOCK ();
11219
11220 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11221 {
11222 LABELED_BLOCK_BODY (node) = NULL_TREE;
11223 CAN_COMPLETE_NORMALLY (node) = 1;
11224 }
11225 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11226 CAN_COMPLETE_NORMALLY (node) = 1;
11227 return node;
11228
11229 case EXIT_BLOCK_EXPR:
11230 /* We don't complete operand 1, because it's the return value of
11231 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11232 return patch_bc_statement (node);
11233
11234 case CASE_EXPR:
11235 cn = java_complete_tree (TREE_OPERAND (node, 0));
11236 if (cn == error_mark_node)
11237 return cn;
11238
11239 /* First, the case expression must be constant. Values of final
11240 fields are accepted. */
11241 cn = fold (cn);
11242 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11243 && JDECL_P (TREE_OPERAND (cn, 1))
11244 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11245 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11246 {
11247 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11248 TREE_OPERAND (cn, 1));
11249 }
11250
11251 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11252 {
11253 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11254 parse_error_context (node, "Constant expression required");
11255 return error_mark_node;
11256 }
11257
11258 nn = ctxp->current_loop;
11259
11260 /* It must be assignable to the type of the switch expression. */
11261 if (!try_builtin_assignconv (NULL_TREE,
11262 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11263 {
11264 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11265 parse_error_context
11266 (wfl_operator,
11267 "Incompatible type for case. Can't convert `%s' to `int'",
11268 lang_printable_name (TREE_TYPE (cn), 0));
11269 return error_mark_node;
11270 }
11271
11272 cn = fold (convert (int_type_node, cn));
11273
11274 /* Multiple instance of a case label bearing the same
11275 value is checked during code generation. The case
11276 expression is allright so far. */
11277 if (TREE_CODE (cn) == VAR_DECL)
11278 cn = DECL_INITIAL (cn);
11279 TREE_OPERAND (node, 0) = cn;
11280 TREE_TYPE (node) = void_type_node;
11281 CAN_COMPLETE_NORMALLY (node) = 1;
11282 TREE_SIDE_EFFECTS (node) = 1;
11283 break;
11284
11285 case DEFAULT_EXPR:
11286 nn = ctxp->current_loop;
11287 /* Only one default label is allowed per switch statement */
11288 if (SWITCH_HAS_DEFAULT (nn))
11289 {
11290 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11291 parse_error_context (wfl_operator,
11292 "Duplicate case label: `default'");
11293 return error_mark_node;
11294 }
11295 else
11296 SWITCH_HAS_DEFAULT (nn) = 1;
11297 TREE_TYPE (node) = void_type_node;
11298 TREE_SIDE_EFFECTS (node) = 1;
11299 CAN_COMPLETE_NORMALLY (node) = 1;
11300 break;
11301
11302 case SWITCH_EXPR:
11303 case LOOP_EXPR:
11304 PUSH_LOOP (node);
11305 /* Check whether the loop was enclosed in a labeled
11306 statement. If not, create one, insert the loop in it and
11307 return the node */
11308 nn = patch_loop_statement (node);
11309
11310 /* Anyways, walk the body of the loop */
11311 if (TREE_CODE (node) == LOOP_EXPR)
11312 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11313 /* Switch statement: walk the switch expression and the cases */
11314 else
11315 node = patch_switch_statement (node);
11316
11317 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11318 nn = error_mark_node;
11319 else
11320 {
11321 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11322 /* If we returned something different, that's because we
11323 inserted a label. Pop the label too. */
11324 if (nn != node)
11325 {
11326 if (CAN_COMPLETE_NORMALLY (node))
11327 CAN_COMPLETE_NORMALLY (nn) = 1;
11328 POP_LABELED_BLOCK ();
11329 }
11330 }
11331 POP_LOOP ();
11332 return nn;
11333
11334 case EXIT_EXPR:
11335 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11336 return patch_exit_expr (node);
11337
11338 case COND_EXPR:
11339 /* Condition */
11340 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11341 if (TREE_OPERAND (node, 0) == error_mark_node)
11342 return error_mark_node;
11343 /* then-else branches */
11344 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11345 if (TREE_OPERAND (node, 1) == error_mark_node)
11346 return error_mark_node;
11347 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11348 if (TREE_OPERAND (node, 2) == error_mark_node)
11349 return error_mark_node;
11350 return patch_if_else_statement (node);
11351 break;
11352
11353 case CONDITIONAL_EXPR:
11354 /* Condition */
11355 wfl_op1 = TREE_OPERAND (node, 0);
11356 COMPLETE_CHECK_OP_0 (node);
11357 wfl_op2 = TREE_OPERAND (node, 1);
11358 COMPLETE_CHECK_OP_1 (node);
11359 wfl_op3 = TREE_OPERAND (node, 2);
11360 COMPLETE_CHECK_OP_2 (node);
11361 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11362
11363 /* 3- Expression section */
11364 case COMPOUND_EXPR:
11365 wfl_op2 = TREE_OPERAND (node, 1);
11366 TREE_OPERAND (node, 0) = nn =
11367 java_complete_tree (TREE_OPERAND (node, 0));
11368 if (wfl_op2 == empty_stmt_node)
11369 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11370 else
11371 {
11372 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11373 {
11374 /* An unreachable condition in a do-while statement
11375 is *not* (technically) an unreachable statement. */
11376 nn = wfl_op2;
11377 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11378 nn = EXPR_WFL_NODE (nn);
11379 if (TREE_CODE (nn) != EXIT_EXPR)
11380 {
11381 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11382 parse_error_context (wfl_operator, "Unreachable statement");
11383 }
11384 }
11385 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11386 if (TREE_OPERAND (node, 1) == error_mark_node)
11387 return error_mark_node;
11388 CAN_COMPLETE_NORMALLY (node)
11389 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11390 }
11391 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11392 break;
11393
11394 case RETURN_EXPR:
11395 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11396 return patch_return (node);
11397
11398 case EXPR_WITH_FILE_LOCATION:
11399 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11400 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11401 {
11402 tree wfl = node;
11403 node = resolve_expression_name (node, NULL);
11404 if (node == error_mark_node)
11405 return node;
11406 /* Keep line number information somewhere were it doesn't
11407 disrupt the completion process. */
11408 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11409 {
11410 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11411 TREE_OPERAND (node, 1) = wfl;
11412 }
11413 CAN_COMPLETE_NORMALLY (node) = 1;
11414 }
11415 else
11416 {
11417 tree body;
11418 int save_lineno = lineno;
11419 lineno = EXPR_WFL_LINENO (node);
11420 body = java_complete_tree (EXPR_WFL_NODE (node));
11421 lineno = save_lineno;
11422 EXPR_WFL_NODE (node) = body;
11423 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11424 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11425 if (body == empty_stmt_node || TREE_CONSTANT (body))
11426 {
11427 /* Makes it easier to constant fold, detect empty bodies. */
11428 return body;
11429 }
11430 if (body == error_mark_node)
11431 {
11432 /* Its important for the evaluation of assignment that
11433 this mark on the TREE_TYPE is propagated. */
11434 TREE_TYPE (node) = error_mark_node;
11435 return error_mark_node;
11436 }
11437 else
11438 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11439
11440 }
11441 break;
11442
11443 case NEW_ARRAY_EXPR:
11444 /* Patch all the dimensions */
11445 flag = 0;
11446 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11447 {
11448 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11449 tree dim = convert (int_type_node,
11450 java_complete_tree (TREE_VALUE (cn)));
11451 if (dim == error_mark_node)
11452 {
11453 flag = 1;
11454 continue;
11455 }
11456 else
11457 {
11458 TREE_VALUE (cn) = dim;
11459 /* Setup the location of the current dimension, for
11460 later error report. */
11461 TREE_PURPOSE (cn) =
11462 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11463 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11464 }
11465 }
11466 /* They complete the array creation expression, if no errors
11467 were found. */
11468 CAN_COMPLETE_NORMALLY (node) = 1;
11469 return (flag ? error_mark_node
11470 : force_evaluation_order (patch_newarray (node)));
11471
11472 case NEW_ANONYMOUS_ARRAY_EXPR:
11473 /* Create the array type if necessary. */
11474 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11475 {
11476 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11477 if (!(type = resolve_type_during_patch (type)))
11478 return error_mark_node;
11479 type = build_array_from_name (type, NULL_TREE,
11480 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11481 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11482 }
11483 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11484 ANONYMOUS_ARRAY_INITIALIZER (node));
11485 if (node == error_mark_node)
11486 return error_mark_node;
11487 CAN_COMPLETE_NORMALLY (node) = 1;
11488 return node;
11489
11490 case NEW_CLASS_EXPR:
11491 case CALL_EXPR:
11492 /* Complete function's argument(s) first */
11493 if (complete_function_arguments (node))
11494 return error_mark_node;
11495 else
11496 {
11497 tree decl, wfl = TREE_OPERAND (node, 0);
11498 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11499 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11500 super_identifier_node);
11501
11502 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11503 from_super, 0, &decl);
11504 if (node == error_mark_node)
11505 return error_mark_node;
11506
11507 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11508 /* If we call this(...), register signature and positions */
11509 if (in_this)
11510 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11511 tree_cons (wfl, decl,
11512 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11513 CAN_COMPLETE_NORMALLY (node) = 1;
11514 return force_evaluation_order (node);
11515 }
11516
11517 case MODIFY_EXPR:
11518 /* Save potential wfls */
11519 wfl_op1 = TREE_OPERAND (node, 0);
11520 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11521
11522 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11523 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11524 && DECL_INITIAL (nn) != NULL_TREE)
11525 {
11526 tree value;
11527
11528 value = fold_constant_for_init (nn, nn);
11529
11530 if (value != NULL_TREE)
11531 {
11532 tree type = TREE_TYPE (value);
11533 if (JPRIMITIVE_TYPE_P (type) ||
11534 (type == string_ptr_type_node && ! flag_emit_class_files))
11535 return empty_stmt_node;
11536 }
11537 if (! flag_emit_class_files)
11538 DECL_INITIAL (nn) = NULL_TREE;
11539 if (CLASS_FINAL_VARIABLE_P (nn))
11540 DECL_FIELD_FINAL_IUD (nn) = 0;
11541 }
11542 wfl_op2 = TREE_OPERAND (node, 1);
11543
11544 if (TREE_OPERAND (node, 0) == error_mark_node)
11545 return error_mark_node;
11546
11547 flag = COMPOUND_ASSIGN_P (wfl_op2);
11548 if (flag)
11549 {
11550 /* This might break when accessing outer field from inner
11551 class. TESTME, FIXME */
11552 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11553
11554 /* Hand stabilize the lhs on both places */
11555 TREE_OPERAND (node, 0) = lvalue;
11556 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11557 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11558
11559 /* 15.25.2.a: Left hand is not an array access. FIXME */
11560 /* Now complete the RHS. We write it back later on. */
11561 nn = java_complete_tree (TREE_OPERAND (node, 1));
11562
11563 if ((cn = patch_string (nn)))
11564 nn = cn;
11565
11566 /* The last part of the rewrite for E1 op= E2 is to have
11567 E1 = (T)(E1 op E2), with T being the type of E1. */
11568 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11569 TREE_TYPE (lvalue), nn));
11570
11571 /* If the assignment is compound and has reference type,
11572 then ensure the LHS has type String and nothing else. */
11573 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11574 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11575 parse_error_context (wfl_op2,
11576 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11577 lang_printable_name (TREE_TYPE (lvalue), 0));
11578
11579 /* 15.25.2.b: Left hand is an array access. FIXME */
11580 }
11581
11582 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11583 function to complete this RHS. Note that a NEW_ARRAY_INIT
11584 might have been already fully expanded if created as a result
11585 of processing an anonymous array initializer. We avoid doing
11586 the operation twice by testing whether the node already bears
11587 a type. */
11588 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11589 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11590 TREE_OPERAND (node, 1));
11591 /* Otherwise we simply complete the RHS */
11592 else
11593 nn = java_complete_tree (TREE_OPERAND (node, 1));
11594
11595 if (nn == error_mark_node)
11596 return error_mark_node;
11597
11598 /* Write back the RHS as we evaluated it. */
11599 TREE_OPERAND (node, 1) = nn;
11600
11601 /* In case we're handling = with a String as a RHS, we need to
11602 produce a String out of the RHS (it might still be a
11603 STRING_CST or a StringBuffer at this stage */
11604 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11605 TREE_OPERAND (node, 1) = nn;
11606
11607 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11608 TREE_OPERAND (node, 1))))
11609 {
11610 /* We return error_mark_node if outer_field_access_fix
11611 detects we write into a final. */
11612 if (nn == error_mark_node)
11613 return error_mark_node;
11614 node = nn;
11615 }
11616 else
11617 {
11618 node = patch_assignment (node, wfl_op1, wfl_op2);
11619 /* Reorganize the tree if necessary. */
11620 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11621 || JSTRING_P (TREE_TYPE (node))))
11622 node = java_refold (node);
11623 }
11624
11625 CAN_COMPLETE_NORMALLY (node) = 1;
11626 return node;
11627
11628 case MULT_EXPR:
11629 case PLUS_EXPR:
11630 case MINUS_EXPR:
11631 case LSHIFT_EXPR:
11632 case RSHIFT_EXPR:
11633 case URSHIFT_EXPR:
11634 case BIT_AND_EXPR:
11635 case BIT_XOR_EXPR:
11636 case BIT_IOR_EXPR:
11637 case TRUNC_MOD_EXPR:
11638 case TRUNC_DIV_EXPR:
11639 case RDIV_EXPR:
11640 case TRUTH_ANDIF_EXPR:
11641 case TRUTH_ORIF_EXPR:
11642 case EQ_EXPR:
11643 case NE_EXPR:
11644 case GT_EXPR:
11645 case GE_EXPR:
11646 case LT_EXPR:
11647 case LE_EXPR:
11648 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11649 knows how to handle those cases. */
11650 wfl_op1 = TREE_OPERAND (node, 0);
11651 wfl_op2 = TREE_OPERAND (node, 1);
11652
11653 CAN_COMPLETE_NORMALLY (node) = 1;
11654 /* Don't complete string nodes if dealing with the PLUS operand. */
11655 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11656 {
11657 nn = java_complete_tree (wfl_op1);
11658 if (nn == error_mark_node)
11659 return error_mark_node;
11660
11661 TREE_OPERAND (node, 0) = nn;
11662 }
11663 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11664 {
11665 nn = java_complete_tree (wfl_op2);
11666 if (nn == error_mark_node)
11667 return error_mark_node;
11668
11669 TREE_OPERAND (node, 1) = nn;
11670 }
11671 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11672
11673 case INSTANCEOF_EXPR:
11674 wfl_op1 = TREE_OPERAND (node, 0);
11675 COMPLETE_CHECK_OP_0 (node);
11676 if (flag_emit_xref)
11677 {
11678 TREE_TYPE (node) = boolean_type_node;
11679 return node;
11680 }
11681 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11682
11683 case UNARY_PLUS_EXPR:
11684 case NEGATE_EXPR:
11685 case TRUTH_NOT_EXPR:
11686 case BIT_NOT_EXPR:
11687 case PREDECREMENT_EXPR:
11688 case PREINCREMENT_EXPR:
11689 case POSTDECREMENT_EXPR:
11690 case POSTINCREMENT_EXPR:
11691 case CONVERT_EXPR:
11692 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11693 how to handle those cases. */
11694 wfl_op1 = TREE_OPERAND (node, 0);
11695 CAN_COMPLETE_NORMALLY (node) = 1;
11696 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11697 if (TREE_OPERAND (node, 0) == error_mark_node)
11698 return error_mark_node;
11699 node = patch_unaryop (node, wfl_op1);
11700 CAN_COMPLETE_NORMALLY (node) = 1;
11701 break;
11702
11703 case ARRAY_REF:
11704 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11705 how to handle those cases. */
11706 wfl_op1 = TREE_OPERAND (node, 0);
11707 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11708 if (TREE_OPERAND (node, 0) == error_mark_node)
11709 return error_mark_node;
11710 if (!flag_emit_class_files && !flag_emit_xref)
11711 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11712 /* The same applies to wfl_op2 */
11713 wfl_op2 = TREE_OPERAND (node, 1);
11714 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11715 if (TREE_OPERAND (node, 1) == error_mark_node)
11716 return error_mark_node;
11717 if (!flag_emit_class_files && !flag_emit_xref)
11718 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11719 return patch_array_ref (node);
11720
11721 case RECORD_TYPE:
11722 return node;;
11723
11724 case COMPONENT_REF:
11725 /* The first step in the re-write of qualified name handling. FIXME.
11726 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11727 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11728 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11729 {
11730 tree name = TREE_OPERAND (node, 1);
11731 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11732 if (field == NULL_TREE)
11733 {
11734 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11735 return error_mark_node;
11736 }
11737 if (! FIELD_STATIC (field))
11738 {
11739 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11740 return error_mark_node;
11741 }
11742 return field;
11743 }
11744 else
11745 abort ();
11746 break;
11747
11748 case THIS_EXPR:
11749 /* Can't use THIS in a static environment */
11750 if (!current_this)
11751 {
11752 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11753 parse_error_context (wfl_operator,
11754 "Keyword `this' used outside allowed context");
11755 TREE_TYPE (node) = error_mark_node;
11756 return error_mark_node;
11757 }
11758 if (ctxp->explicit_constructor_p)
11759 {
11760 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11761 parse_error_context
11762 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11763 TREE_TYPE (node) = error_mark_node;
11764 return error_mark_node;
11765 }
11766 return current_this;
11767
11768 case CLASS_LITERAL:
11769 CAN_COMPLETE_NORMALLY (node) = 1;
11770 node = patch_incomplete_class_ref (node);
11771 if (node == error_mark_node)
11772 return error_mark_node;
11773 break;
11774
11775 case INSTANCE_INITIALIZERS_EXPR:
11776 in_instance_initializer++;
11777 node = java_complete_tree (TREE_OPERAND (node, 0));
11778 in_instance_initializer--;
11779 if (node != error_mark_node)
11780 TREE_TYPE (node) = void_type_node;
11781 else
11782 return error_mark_node;
11783 break;
11784
11785 default:
11786 CAN_COMPLETE_NORMALLY (node) = 1;
11787 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11788 and it's time to turn it into the appropriate String object */
11789 if ((nn = patch_string (node)))
11790 node = nn;
11791 else
11792 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
11793 }
11794 return node;
11795 }
11796
11797 /* Complete function call's argument. Return a non zero value is an
11798 error was found. */
11799
11800 static int
11801 complete_function_arguments (node)
11802 tree node;
11803 {
11804 int flag = 0;
11805 tree cn;
11806
11807 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11808 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11809 {
11810 tree wfl = TREE_VALUE (cn), parm, temp;
11811 parm = java_complete_tree (wfl);
11812
11813 if (parm == error_mark_node)
11814 {
11815 flag = 1;
11816 continue;
11817 }
11818 /* If have a string literal that we haven't transformed yet or a
11819 crafted string buffer, as a result of use of the the String
11820 `+' operator. Build `parm.toString()' and expand it. */
11821 if ((temp = patch_string (parm)))
11822 parm = temp;
11823 /* Inline PRIMTYPE.TYPE read access */
11824 parm = maybe_build_primttype_type_ref (parm, wfl);
11825
11826 TREE_VALUE (cn) = parm;
11827 }
11828 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11829 return flag;
11830 }
11831
11832 /* Sometimes (for loops and variable initialized during their
11833 declaration), we want to wrap a statement around a WFL and turn it
11834 debugable. */
11835
11836 static tree
11837 build_debugable_stmt (location, stmt)
11838 int location;
11839 tree stmt;
11840 {
11841 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11842 {
11843 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11844 EXPR_WFL_LINECOL (stmt) = location;
11845 }
11846 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11847 return stmt;
11848 }
11849
11850 static tree
11851 build_expr_block (body, decls)
11852 tree body, decls;
11853 {
11854 tree node = make_node (BLOCK);
11855 BLOCK_EXPR_DECLS (node) = decls;
11856 BLOCK_EXPR_BODY (node) = body;
11857 if (body)
11858 TREE_TYPE (node) = TREE_TYPE (body);
11859 TREE_SIDE_EFFECTS (node) = 1;
11860 return node;
11861 }
11862
11863 /* Create a new function block and link it approriately to current
11864 function block chain */
11865
11866 static tree
11867 enter_block ()
11868 {
11869 tree b = build_expr_block (NULL_TREE, NULL_TREE);
11870
11871 /* Link block B supercontext to the previous block. The current
11872 function DECL is used as supercontext when enter_a_block is called
11873 for the first time for a given function. The current function body
11874 (DECL_FUNCTION_BODY) is set to be block B. */
11875
11876 tree fndecl = current_function_decl;
11877
11878 if (!fndecl) {
11879 BLOCK_SUPERCONTEXT (b) = current_static_block;
11880 current_static_block = b;
11881 }
11882
11883 else if (!DECL_FUNCTION_BODY (fndecl))
11884 {
11885 BLOCK_SUPERCONTEXT (b) = fndecl;
11886 DECL_FUNCTION_BODY (fndecl) = b;
11887 }
11888 else
11889 {
11890 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11891 DECL_FUNCTION_BODY (fndecl) = b;
11892 }
11893 return b;
11894 }
11895
11896 /* Exit a block by changing the current function body
11897 (DECL_FUNCTION_BODY) to the current block super context, only if
11898 the block being exited isn't the method's top level one. */
11899
11900 static tree
11901 exit_block ()
11902 {
11903 tree b;
11904 if (current_function_decl)
11905 {
11906 b = DECL_FUNCTION_BODY (current_function_decl);
11907 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11908 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11909 }
11910 else
11911 {
11912 b = current_static_block;
11913
11914 if (BLOCK_SUPERCONTEXT (b))
11915 current_static_block = BLOCK_SUPERCONTEXT (b);
11916 }
11917 return b;
11918 }
11919
11920 /* Lookup for NAME in the nested function's blocks, all the way up to
11921 the current toplevel one. It complies with Java's local variable
11922 scoping rules. */
11923
11924 static tree
11925 lookup_name_in_blocks (name)
11926 tree name;
11927 {
11928 tree b = GET_CURRENT_BLOCK (current_function_decl);
11929
11930 while (b != current_function_decl)
11931 {
11932 tree current;
11933
11934 /* Paranoid sanity check. To be removed */
11935 if (TREE_CODE (b) != BLOCK)
11936 abort ();
11937
11938 for (current = BLOCK_EXPR_DECLS (b); current;
11939 current = TREE_CHAIN (current))
11940 if (DECL_NAME (current) == name)
11941 return current;
11942 b = BLOCK_SUPERCONTEXT (b);
11943 }
11944 return NULL_TREE;
11945 }
11946
11947 static void
11948 maybe_absorb_scoping_blocks ()
11949 {
11950 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
11951 {
11952 tree b = exit_block ();
11953 java_method_add_stmt (current_function_decl, b);
11954 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11955 }
11956 }
11957
11958 \f
11959 /* This section of the source is reserved to build_* functions that
11960 are building incomplete tree nodes and the patch_* functions that
11961 are completing them. */
11962
11963 /* Wrap a non WFL node around a WFL. */
11964 static tree
11965 build_wfl_wrap (node, location)
11966 tree node;
11967 int location;
11968 {
11969 tree wfl, node_to_insert = node;
11970
11971 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11972 with the way we're processing SUPER. A THIS from a primary as a
11973 different form than a SUPER. Turn THIS into something symbolic */
11974 if (TREE_CODE (node) == THIS_EXPR)
11975 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11976 else
11977 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11978
11979 EXPR_WFL_LINECOL (wfl) = location;
11980 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11981 return wfl;
11982 }
11983
11984
11985 /* Build a super() constructor invocation. Returns empty_stmt_node if
11986 we're currently dealing with the class java.lang.Object. */
11987
11988 static tree
11989 build_super_invocation (mdecl)
11990 tree mdecl;
11991 {
11992 if (DECL_CONTEXT (mdecl) == object_type_node)
11993 return empty_stmt_node;
11994 else
11995 {
11996 tree super_wfl = build_wfl_node (super_identifier_node);
11997 tree a = NULL_TREE, t;
11998 /* If we're dealing with an anonymous class, pass the arguments
11999 of the crafted constructor along. */
12000 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12001 {
12002 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12003 for (; t != end_params_node; t = TREE_CHAIN (t))
12004 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12005 }
12006 return build_method_invocation (super_wfl, a);
12007 }
12008 }
12009
12010 /* Build a SUPER/THIS qualified method invocation. */
12011
12012 static tree
12013 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12014 int use_this;
12015 tree name, args;
12016 int lloc, rloc;
12017 {
12018 tree invok;
12019 tree wfl =
12020 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12021 EXPR_WFL_LINECOL (wfl) = lloc;
12022 invok = build_method_invocation (name, args);
12023 return make_qualified_primary (wfl, invok, rloc);
12024 }
12025
12026 /* Build an incomplete CALL_EXPR node. */
12027
12028 static tree
12029 build_method_invocation (name, args)
12030 tree name;
12031 tree args;
12032 {
12033 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12034 TREE_SIDE_EFFECTS (call) = 1;
12035 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12036 return call;
12037 }
12038
12039 /* Build an incomplete new xxx(...) node. */
12040
12041 static tree
12042 build_new_invocation (name, args)
12043 tree name, args;
12044 {
12045 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12046 TREE_SIDE_EFFECTS (call) = 1;
12047 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12048 return call;
12049 }
12050
12051 /* Build an incomplete assignment expression. */
12052
12053 static tree
12054 build_assignment (op, op_location, lhs, rhs)
12055 int op, op_location;
12056 tree lhs, rhs;
12057 {
12058 tree assignment;
12059 /* Build the corresponding binop if we deal with a Compound
12060 Assignment operator. Mark the binop sub-tree as part of a
12061 Compound Assignment expression */
12062 if (op != ASSIGN_TK)
12063 {
12064 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12065 COMPOUND_ASSIGN_P (rhs) = 1;
12066 }
12067 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12068 TREE_SIDE_EFFECTS (assignment) = 1;
12069 EXPR_WFL_LINECOL (assignment) = op_location;
12070 return assignment;
12071 }
12072
12073 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12074
12075 char *
12076 print_int_node (node)
12077 tree node;
12078 {
12079 static char buffer [80];
12080 if (TREE_CONSTANT_OVERFLOW (node))
12081 sprintf (buffer, "<overflow>");
12082
12083 if (TREE_INT_CST_HIGH (node) == 0)
12084 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12085 TREE_INT_CST_LOW (node));
12086 else if (TREE_INT_CST_HIGH (node) == -1
12087 && TREE_INT_CST_LOW (node) != 0)
12088 {
12089 buffer [0] = '-';
12090 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12091 -TREE_INT_CST_LOW (node));
12092 }
12093 else
12094 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12095 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12096
12097 return buffer;
12098 }
12099
12100 \f
12101
12102 /* This section of the code handle assignment check with FINAL
12103 variables. */
12104
12105 static void
12106 reset_static_final_variable_assignment_flag (class)
12107 tree class;
12108 {
12109 tree field;
12110 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12111 if (CLASS_FINAL_VARIABLE_P (field))
12112 DECL_FIELD_FINAL_LIIC (field) = 0;
12113 }
12114
12115 /* Figure whether all final static variable have been initialized. */
12116
12117 static void
12118 check_static_final_variable_assignment_flag (class)
12119 tree class;
12120 {
12121 tree field;
12122
12123 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12124 if (CLASS_FINAL_VARIABLE_P (field)
12125 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12126 parse_error_context
12127 (DECL_FIELD_FINAL_WFL (field),
12128 "Blank static final variable `%s' may not have been initialized",
12129 IDENTIFIER_POINTER (DECL_NAME (field)));
12130 }
12131
12132 /* This function marks all final variable locally unassigned. */
12133
12134 static void
12135 reset_final_variable_local_assignment_flag (class)
12136 tree class;
12137 {
12138 tree field;
12139 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12140 if (FINAL_VARIABLE_P (field))
12141 DECL_FIELD_FINAL_LIIC (field) = 0;
12142 }
12143
12144 /* Figure whether all final variables have beem initialized in MDECL
12145 and mark MDECL accordingly. */
12146
12147 static void
12148 check_final_variable_local_assignment_flag (class, mdecl)
12149 tree class;
12150 tree mdecl;
12151 {
12152 tree field;
12153 int initialized = 0;
12154 int non_initialized = 0;
12155
12156 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12157 return;
12158
12159 /* First find out whether all final variables or no final variable
12160 are initialized in this ctor. We don't take into account final
12161 variable that have been initialized upon declaration. */
12162 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12163 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12164 {
12165 if (DECL_FIELD_FINAL_LIIC (field))
12166 initialized++;
12167 else
12168 non_initialized++;
12169 }
12170
12171 /* There were no non initialized variable and no initialized variable.
12172 This ctor is fine. */
12173 if (!non_initialized && !initialized)
12174 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12175 /* If no variables have been initialized, that fine. We'll check
12176 later whether this ctor calls a constructor which initializes
12177 them. We mark the ctor as not initializing all its finals. */
12178 else if (initialized == 0)
12179 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12180 /* If we have a mixed bag, then we have a problem. We need to report
12181 all the variables we're not initializing. */
12182 else if (initialized && non_initialized)
12183 {
12184 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12185 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12186 if (FIELD_FINAL (field)
12187 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12188 {
12189 parse_error_context
12190 (lookup_cl (mdecl),
12191 "Blank final variable `%s' may not have been initialized in this constructor",
12192 IDENTIFIER_POINTER (DECL_NAME (field)));
12193 DECL_FIELD_FINAL_IERR (field) = 1;
12194 }
12195 }
12196 /* Otherwise we know this ctor is initializing all its final
12197 variable. We mark it so. */
12198 else
12199 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12200 }
12201
12202 /* This function recurses in a simple what through STMT and stops when
12203 it finds a constructor call. It then verifies that the called
12204 constructor initialized its final properly. Return 1 upon success,
12205 0 or -1 otherwise. */
12206
12207 static int
12208 check_final_variable_indirect_assignment (stmt)
12209 tree stmt;
12210 {
12211 int res;
12212 switch (TREE_CODE (stmt))
12213 {
12214 case EXPR_WITH_FILE_LOCATION:
12215 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12216 case COMPOUND_EXPR:
12217 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12218 if (res)
12219 return res;
12220 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12221 case SAVE_EXPR:
12222 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12223 case CALL_EXPR:
12224 {
12225 tree decl = TREE_OPERAND (stmt, 0);
12226 tree fbody;
12227
12228 if (TREE_CODE (decl) != FUNCTION_DECL)
12229 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12230 if (TREE_CODE (decl) != FUNCTION_DECL)
12231 abort ();
12232 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12233 return 1;
12234 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12235 return -1;
12236 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12237 if (fbody == error_mark_node)
12238 return -1;
12239 fbody = BLOCK_EXPR_BODY (fbody);
12240 return check_final_variable_indirect_assignment (fbody);
12241 }
12242 default:
12243 break;
12244 }
12245 return 0;
12246 }
12247
12248 /* This is the last chance to catch a final variable initialization
12249 problem. This routine will report an error if a final variable was
12250 never (globally) initialized and never reported as not having been
12251 initialized properly. */
12252
12253 static void
12254 check_final_variable_global_assignment_flag (class)
12255 tree class;
12256 {
12257 tree field, mdecl;
12258 int nnctor = 0;
12259
12260 /* We go through all natural ctors and see whether they're
12261 initializing all their final variables or not. */
12262 current_function_decl = NULL_TREE; /* For the error report. */
12263 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12264 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12265 {
12266 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12267 {
12268 /* It doesn't. Maybe it calls a constructor that initializes
12269 them. find out. */
12270 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12271 if (fbody == error_mark_node)
12272 continue;
12273 fbody = BLOCK_EXPR_BODY (fbody);
12274 if (check_final_variable_indirect_assignment (fbody) == 1)
12275 {
12276 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12277 nnctor++;
12278 }
12279 else
12280 parse_error_context
12281 (lookup_cl (mdecl),
12282 "Final variable initialization error in this constructor");
12283 }
12284 else
12285 nnctor++;
12286 }
12287
12288 /* Finally we catch final variables that never were initialized */
12289 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12290 if (FINAL_VARIABLE_P (field)
12291 /* If the field wasn't initialized upon declaration */
12292 && !DECL_FIELD_FINAL_IUD (field)
12293 /* There wasn't natural ctor in which the field could have been
12294 initialized */
12295 && !nnctor
12296 /* If we never reported a problem with this field */
12297 && !DECL_FIELD_FINAL_IERR (field))
12298 {
12299 current_function_decl = NULL;
12300 parse_error_context
12301 (DECL_FIELD_FINAL_WFL (field),
12302 "Final variable `%s' hasn't been initialized upon its declaration",
12303 IDENTIFIER_POINTER (DECL_NAME (field)));
12304 }
12305
12306 }
12307
12308 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12309 context. */
12310
12311 static int
12312 check_final_assignment (lvalue, wfl)
12313 tree lvalue, wfl;
12314 {
12315 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12316 return 0;
12317
12318 if (TREE_CODE (lvalue) == COMPONENT_REF
12319 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12320 lvalue = TREE_OPERAND (lvalue, 1);
12321
12322 if (!FIELD_FINAL (lvalue))
12323 return 0;
12324
12325 /* Now the logic. We can modify a final VARIABLE:
12326 1) in finit$, (its declaration was followed by an initialization,)
12327 2) consistently in each natural ctor, if it wasn't initialized in
12328 finit$ or once in <clinit>. In any other cases, an error should be
12329 reported. */
12330 if (DECL_FINIT_P (current_function_decl))
12331 {
12332 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12333 return 0;
12334 }
12335
12336 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12337 /* Only if it wasn't given a value upon initialization */
12338 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12339 /* If it was never assigned a value in this constructor */
12340 && !DECL_FIELD_FINAL_LIIC (lvalue))
12341 {
12342 /* Turn the locally assigned flag on, it will be checked later
12343 on to point out at discrepancies. */
12344 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12345 if (DECL_CLINIT_P (current_function_decl))
12346 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12347 return 0;
12348 }
12349
12350 /* Other problems should be reported right away. */
12351 parse_error_context
12352 (wfl, "Can't %sassign a value to the final variable `%s'",
12353 (FIELD_STATIC (lvalue) ? "re" : ""),
12354 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12355
12356 /* Note that static field can be initialized once and only once. */
12357 if (FIELD_STATIC (lvalue))
12358 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12359
12360 return 1;
12361 }
12362
12363 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12364 read. This is needed to avoid circularities in the implementation
12365 of these fields in libjava. */
12366
12367 static tree
12368 maybe_build_primttype_type_ref (rhs, wfl)
12369 tree rhs, wfl;
12370 {
12371 tree to_return = NULL_TREE;
12372 tree rhs_type = TREE_TYPE (rhs);
12373 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12374 {
12375 tree n = TREE_OPERAND (rhs, 1);
12376 if (TREE_CODE (n) == VAR_DECL
12377 && DECL_NAME (n) == TYPE_identifier_node
12378 && rhs_type == class_ptr_type
12379 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12380 {
12381 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12382 if (!strncmp (self_name, "java.lang.", 10))
12383 to_return = build_primtype_type_ref (self_name);
12384 }
12385 }
12386 return (to_return ? to_return : rhs );
12387 }
12388
12389 /* 15.25 Assignment operators. */
12390
12391 static tree
12392 patch_assignment (node, wfl_op1, wfl_op2)
12393 tree node;
12394 tree wfl_op1;
12395 tree wfl_op2;
12396 {
12397 tree rhs = TREE_OPERAND (node, 1);
12398 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12399 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12400 int error_found = 0;
12401 int lvalue_from_array = 0;
12402
12403 /* Can't assign to a (blank) final. */
12404 if (check_final_assignment (lvalue, wfl_op1))
12405 error_found = 1;
12406
12407 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12408
12409 /* Lhs can be a named variable */
12410 if (JDECL_P (lvalue))
12411 {
12412 lhs_type = TREE_TYPE (lvalue);
12413 }
12414 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12415 comment on reason why */
12416 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12417 {
12418 lhs_type = TREE_TYPE (lvalue);
12419 lvalue_from_array = 1;
12420 }
12421 /* Or a field access */
12422 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12423 lhs_type = TREE_TYPE (lvalue);
12424 /* Or a function return slot */
12425 else if (TREE_CODE (lvalue) == RESULT_DECL)
12426 lhs_type = TREE_TYPE (lvalue);
12427 /* Otherwise, we might want to try to write into an optimized static
12428 final, this is an of a different nature, reported further on. */
12429 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12430 && resolve_expression_name (wfl_op1, &llvalue))
12431 {
12432 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12433 {
12434 /* What we should do instead is resetting the all the flags
12435 previously set, exchange lvalue for llvalue and continue. */
12436 error_found = 1;
12437 return error_mark_node;
12438 }
12439 else
12440 lhs_type = TREE_TYPE (lvalue);
12441 }
12442 else
12443 {
12444 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12445 error_found = 1;
12446 }
12447
12448 rhs_type = TREE_TYPE (rhs);
12449 /* 5.1 Try the assignment conversion for builtin type. */
12450 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12451
12452 /* 5.2 If it failed, try a reference conversion */
12453 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12454 lhs_type = promote_type (rhs_type);
12455
12456 /* 15.25.2 If we have a compound assignment, convert RHS into the
12457 type of the LHS */
12458 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12459 new_rhs = convert (lhs_type, rhs);
12460
12461 /* Explicit cast required. This is an error */
12462 if (!new_rhs)
12463 {
12464 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12465 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12466 tree wfl;
12467 char operation [32]; /* Max size known */
12468
12469 /* If the assignment is part of a declaration, we use the WFL of
12470 the declared variable to point out the error and call it a
12471 declaration problem. If the assignment is a genuine =
12472 operator, we call is a operator `=' problem, otherwise we
12473 call it an assignment problem. In both of these last cases,
12474 we use the WFL of the operator to indicate the error. */
12475
12476 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12477 {
12478 wfl = wfl_op1;
12479 strcpy (operation, "declaration");
12480 }
12481 else
12482 {
12483 wfl = wfl_operator;
12484 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12485 strcpy (operation, "assignment");
12486 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12487 strcpy (operation, "`return'");
12488 else
12489 strcpy (operation, "`='");
12490 }
12491
12492 if (!valid_cast_to_p (rhs_type, lhs_type))
12493 parse_error_context
12494 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12495 operation, t1, t2);
12496 else
12497 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12498 operation, t1, t2);
12499 free (t1); free (t2);
12500 error_found = 1;
12501 }
12502
12503 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12504 if (new_rhs)
12505 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12506
12507 if (error_found)
12508 return error_mark_node;
12509
12510 /* 10.10: Array Store Exception runtime check */
12511 if (!flag_emit_class_files
12512 && !flag_emit_xref
12513 && lvalue_from_array
12514 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12515 {
12516 tree check;
12517 tree base = lvalue;
12518
12519 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12520 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12521 base = TREE_OPERAND (lvalue, 0);
12522 else
12523 {
12524 if (flag_bounds_check)
12525 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12526 else
12527 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12528 }
12529
12530 /* Build the invocation of _Jv_CheckArrayStore */
12531 new_rhs = save_expr (new_rhs);
12532 check = build (CALL_EXPR, void_type_node,
12533 build_address_of (soft_checkarraystore_node),
12534 tree_cons (NULL_TREE, base,
12535 build_tree_list (NULL_TREE, new_rhs)),
12536 NULL_TREE);
12537 TREE_SIDE_EFFECTS (check) = 1;
12538
12539 /* We have to decide on an insertion point */
12540 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12541 {
12542 tree t;
12543 if (flag_bounds_check)
12544 {
12545 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12546 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12547 build (COMPOUND_EXPR, void_type_node, t, check);
12548 }
12549 else
12550 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12551 check, TREE_OPERAND (lvalue, 1));
12552 }
12553 else
12554 {
12555 /* Make sure the bound check will happen before the store check */
12556 if (flag_bounds_check)
12557 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12558 build (COMPOUND_EXPR, void_type_node,
12559 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12560 else
12561 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12562 }
12563 }
12564
12565 /* Final locals can be used as case values in switch
12566 statement. Prepare them for this eventuality. */
12567 if (TREE_CODE (lvalue) == VAR_DECL
12568 && LOCAL_FINAL_P (lvalue)
12569 && TREE_CONSTANT (new_rhs)
12570 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12571 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12572 )
12573 {
12574 TREE_CONSTANT (lvalue) = 1;
12575 DECL_INITIAL (lvalue) = new_rhs;
12576 }
12577
12578 TREE_OPERAND (node, 0) = lvalue;
12579 TREE_OPERAND (node, 1) = new_rhs;
12580 TREE_TYPE (node) = lhs_type;
12581 return node;
12582 }
12583
12584 /* Check that type SOURCE can be cast into type DEST. If the cast
12585 can't occur at all, return 0 otherwise 1. This function is used to
12586 produce accurate error messages on the reasons why an assignment
12587 failed. */
12588
12589 static tree
12590 try_reference_assignconv (lhs_type, rhs)
12591 tree lhs_type, rhs;
12592 {
12593 tree new_rhs = NULL_TREE;
12594 tree rhs_type = TREE_TYPE (rhs);
12595
12596 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12597 {
12598 /* `null' may be assigned to any reference type */
12599 if (rhs == null_pointer_node)
12600 new_rhs = null_pointer_node;
12601 /* Try the reference assignment conversion */
12602 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12603 new_rhs = rhs;
12604 /* This is a magic assignment that we process differently */
12605 else if (rhs == soft_exceptioninfo_call_node)
12606 new_rhs = rhs;
12607 }
12608 return new_rhs;
12609 }
12610
12611 /* Check that RHS can be converted into LHS_TYPE by the assignment
12612 conversion (5.2), for the cases of RHS being a builtin type. Return
12613 NULL_TREE if the conversion fails or if because RHS isn't of a
12614 builtin type. Return a converted RHS if the conversion is possible. */
12615
12616 static tree
12617 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12618 tree wfl_op1, lhs_type, rhs;
12619 {
12620 tree new_rhs = NULL_TREE;
12621 tree rhs_type = TREE_TYPE (rhs);
12622
12623 /* Handle boolean specially. */
12624 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12625 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12626 {
12627 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12628 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12629 new_rhs = rhs;
12630 }
12631
12632 /* Zero accepted everywhere */
12633 else if (TREE_CODE (rhs) == INTEGER_CST
12634 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12635 && JPRIMITIVE_TYPE_P (rhs_type))
12636 new_rhs = convert (lhs_type, rhs);
12637
12638 /* 5.1.1 Try Identity Conversion,
12639 5.1.2 Try Widening Primitive Conversion */
12640 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12641 new_rhs = convert (lhs_type, rhs);
12642
12643 /* Try a narrowing primitive conversion (5.1.3):
12644 - expression is a constant expression of type int AND
12645 - variable is byte, short or char AND
12646 - The value of the expression is representable in the type of the
12647 variable */
12648 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12649 && (lhs_type == byte_type_node || lhs_type == char_type_node
12650 || lhs_type == short_type_node))
12651 {
12652 if (int_fits_type_p (rhs, lhs_type))
12653 new_rhs = convert (lhs_type, rhs);
12654 else if (wfl_op1) /* Might be called with a NULL */
12655 parse_warning_context
12656 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12657 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12658 /* Reported a warning that will turn into an error further
12659 down, so we don't return */
12660 }
12661
12662 return new_rhs;
12663 }
12664
12665 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12666 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12667 0 is the conversion test fails. This implements parts the method
12668 invocation convertion (5.3). */
12669
12670 static int
12671 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12672 tree lhs_type, rhs_type;
12673 {
12674 /* 5.1.1: This is the identity conversion part. */
12675 if (lhs_type == rhs_type)
12676 return 1;
12677
12678 /* Reject non primitive types and boolean conversions. */
12679 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12680 return 0;
12681
12682 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12683 than a char can't be converted into a char. Short can't too, but
12684 the < test below takes care of that */
12685 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12686 return 0;
12687
12688 /* Accept all promoted type here. Note, we can't use <= in the test
12689 below, because we still need to bounce out assignments of short
12690 to char and the likes */
12691 if (lhs_type == int_type_node
12692 && (rhs_type == promoted_byte_type_node
12693 || rhs_type == promoted_short_type_node
12694 || rhs_type == promoted_char_type_node
12695 || rhs_type == promoted_boolean_type_node))
12696 return 1;
12697
12698 /* From here, an integral is widened if its precision is smaller
12699 than the precision of the LHS or if the LHS is a floating point
12700 type, or the RHS is a float and the RHS a double. */
12701 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12702 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12703 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12704 || (rhs_type == float_type_node && lhs_type == double_type_node))
12705 return 1;
12706
12707 return 0;
12708 }
12709
12710 /* Check that something of SOURCE type can be assigned or cast to
12711 something of DEST type at runtime. Return 1 if the operation is
12712 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12713 were SOURCE is cast into DEST, which borrows a lot of the
12714 assignment check. */
12715
12716 static int
12717 valid_ref_assignconv_cast_p (source, dest, cast)
12718 tree source;
12719 tree dest;
12720 int cast;
12721 {
12722 /* SOURCE or DEST might be null if not from a declared entity. */
12723 if (!source || !dest)
12724 return 0;
12725 if (JNULLP_TYPE_P (source))
12726 return 1;
12727 if (TREE_CODE (source) == POINTER_TYPE)
12728 source = TREE_TYPE (source);
12729 if (TREE_CODE (dest) == POINTER_TYPE)
12730 dest = TREE_TYPE (dest);
12731
12732 /* If source and dest are being compiled from bytecode, they may need to
12733 be loaded. */
12734 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12735 {
12736 load_class (source, 1);
12737 safe_layout_class (source);
12738 }
12739 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12740 {
12741 load_class (dest, 1);
12742 safe_layout_class (dest);
12743 }
12744
12745 /* Case where SOURCE is a class type */
12746 if (TYPE_CLASS_P (source))
12747 {
12748 if (TYPE_CLASS_P (dest))
12749 return (source == dest
12750 || inherits_from_p (source, dest)
12751 || (cast && inherits_from_p (dest, source)));
12752 if (TYPE_INTERFACE_P (dest))
12753 {
12754 /* If doing a cast and SOURCE is final, the operation is
12755 always correct a compile time (because even if SOURCE
12756 does not implement DEST, a subclass of SOURCE might). */
12757 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12758 return 1;
12759 /* Otherwise, SOURCE must implement DEST */
12760 return interface_of_p (dest, source);
12761 }
12762 /* DEST is an array, cast permited if SOURCE is of Object type */
12763 return (cast && source == object_type_node ? 1 : 0);
12764 }
12765 if (TYPE_INTERFACE_P (source))
12766 {
12767 if (TYPE_CLASS_P (dest))
12768 {
12769 /* If not casting, DEST must be the Object type */
12770 if (!cast)
12771 return dest == object_type_node;
12772 /* We're doing a cast. The cast is always valid is class
12773 DEST is not final, otherwise, DEST must implement SOURCE */
12774 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12775 return 1;
12776 else
12777 return interface_of_p (source, dest);
12778 }
12779 if (TYPE_INTERFACE_P (dest))
12780 {
12781 /* If doing a cast, then if SOURCE and DEST contain method
12782 with the same signature but different return type, then
12783 this is a (compile time) error */
12784 if (cast)
12785 {
12786 tree method_source, method_dest;
12787 tree source_type;
12788 tree source_sig;
12789 tree source_name;
12790 for (method_source = TYPE_METHODS (source); method_source;
12791 method_source = TREE_CHAIN (method_source))
12792 {
12793 source_sig =
12794 build_java_argument_signature (TREE_TYPE (method_source));
12795 source_type = TREE_TYPE (TREE_TYPE (method_source));
12796 source_name = DECL_NAME (method_source);
12797 for (method_dest = TYPE_METHODS (dest);
12798 method_dest; method_dest = TREE_CHAIN (method_dest))
12799 if (source_sig ==
12800 build_java_argument_signature (TREE_TYPE (method_dest))
12801 && source_name == DECL_NAME (method_dest)
12802 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12803 return 0;
12804 }
12805 return 1;
12806 }
12807 else
12808 return source == dest || interface_of_p (dest, source);
12809 }
12810 else
12811 {
12812 /* Array */
12813 return (cast
12814 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12815 || (DECL_NAME (TYPE_NAME (source))
12816 == java_io_serializable)));
12817 }
12818 }
12819 if (TYPE_ARRAY_P (source))
12820 {
12821 if (TYPE_CLASS_P (dest))
12822 return dest == object_type_node;
12823 /* Can't cast an array to an interface unless the interface is
12824 java.lang.Cloneable or java.io.Serializable. */
12825 if (TYPE_INTERFACE_P (dest))
12826 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12827 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12828 else /* Arrays */
12829 {
12830 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12831 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12832
12833 /* In case of severe errors, they turn out null */
12834 if (!dest_element_type || !source_element_type)
12835 return 0;
12836 if (source_element_type == dest_element_type)
12837 return 1;
12838 return valid_ref_assignconv_cast_p (source_element_type,
12839 dest_element_type, cast);
12840 }
12841 return 0;
12842 }
12843 return 0;
12844 }
12845
12846 static int
12847 valid_cast_to_p (source, dest)
12848 tree source;
12849 tree dest;
12850 {
12851 if (TREE_CODE (source) == POINTER_TYPE)
12852 source = TREE_TYPE (source);
12853 if (TREE_CODE (dest) == POINTER_TYPE)
12854 dest = TREE_TYPE (dest);
12855
12856 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12857 return valid_ref_assignconv_cast_p (source, dest, 1);
12858
12859 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12860 return 1;
12861
12862 else if (TREE_CODE (source) == BOOLEAN_TYPE
12863 && TREE_CODE (dest) == BOOLEAN_TYPE)
12864 return 1;
12865
12866 return 0;
12867 }
12868
12869 static tree
12870 do_unary_numeric_promotion (arg)
12871 tree arg;
12872 {
12873 tree type = TREE_TYPE (arg);
12874 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12875 || TREE_CODE (type) == CHAR_TYPE)
12876 arg = convert (int_type_node, arg);
12877 return arg;
12878 }
12879
12880 /* Return a non zero value if SOURCE can be converted into DEST using
12881 the method invocation conversion rule (5.3). */
12882 static int
12883 valid_method_invocation_conversion_p (dest, source)
12884 tree dest, source;
12885 {
12886 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12887 && valid_builtin_assignconv_identity_widening_p (dest, source))
12888 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12889 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12890 && valid_ref_assignconv_cast_p (source, dest, 0)));
12891 }
12892
12893 /* Build an incomplete binop expression. */
12894
12895 static tree
12896 build_binop (op, op_location, op1, op2)
12897 enum tree_code op;
12898 int op_location;
12899 tree op1, op2;
12900 {
12901 tree binop = build (op, NULL_TREE, op1, op2);
12902 TREE_SIDE_EFFECTS (binop) = 1;
12903 /* Store the location of the operator, for better error report. The
12904 string of the operator will be rebuild based on the OP value. */
12905 EXPR_WFL_LINECOL (binop) = op_location;
12906 return binop;
12907 }
12908
12909 /* Build the string of the operator retained by NODE. If NODE is part
12910 of a compound expression, add an '=' at the end of the string. This
12911 function is called when an error needs to be reported on an
12912 operator. The string is returned as a pointer to a static character
12913 buffer. */
12914
12915 static char *
12916 operator_string (node)
12917 tree node;
12918 {
12919 #define BUILD_OPERATOR_STRING(S) \
12920 { \
12921 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12922 return buffer; \
12923 }
12924
12925 static char buffer [10];
12926 switch (TREE_CODE (node))
12927 {
12928 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12929 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12930 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12931 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12932 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12933 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12934 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12935 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12936 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12937 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12938 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12939 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12940 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12941 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12942 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12943 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12944 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12945 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12946 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12947 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12948 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12949 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12950 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12951 case PREINCREMENT_EXPR: /* Fall through */
12952 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12953 case PREDECREMENT_EXPR: /* Fall through */
12954 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12955 default:
12956 internal_error ("unregistered operator %s",
12957 tree_code_name [TREE_CODE (node)]);
12958 }
12959 return NULL;
12960 #undef BUILD_OPERATOR_STRING
12961 }
12962
12963 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12964
12965 static int
12966 java_decl_equiv (var_acc1, var_acc2)
12967 tree var_acc1, var_acc2;
12968 {
12969 if (JDECL_P (var_acc1))
12970 return (var_acc1 == var_acc2);
12971
12972 return (TREE_CODE (var_acc1) == COMPONENT_REF
12973 && TREE_CODE (var_acc2) == COMPONENT_REF
12974 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12975 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12976 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12977 }
12978
12979 /* Return a non zero value if CODE is one of the operators that can be
12980 used in conjunction with the `=' operator in a compound assignment. */
12981
12982 static int
12983 binop_compound_p (code)
12984 enum tree_code code;
12985 {
12986 int i;
12987 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12988 if (binop_lookup [i] == code)
12989 break;
12990
12991 return i < BINOP_COMPOUND_CANDIDATES;
12992 }
12993
12994 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12995
12996 static tree
12997 java_refold (t)
12998 tree t;
12999 {
13000 tree c, b, ns, decl;
13001
13002 if (TREE_CODE (t) != MODIFY_EXPR)
13003 return t;
13004
13005 c = TREE_OPERAND (t, 1);
13006 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13007 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13008 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13009 return t;
13010
13011 /* Now the left branch of the binary operator. */
13012 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13013 if (! (b && TREE_CODE (b) == NOP_EXPR
13014 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13015 return t;
13016
13017 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13018 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13019 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13020 return t;
13021
13022 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13023 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13024 /* It's got to be the an equivalent decl */
13025 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13026 {
13027 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13028 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13029 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13030 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13031 /* Change the right part of the BINOP_EXPR */
13032 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13033 }
13034
13035 return t;
13036 }
13037
13038 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13039 errors but we modify NODE so that it contains the type computed
13040 according to the expression, when it's fixed. Otherwise, we write
13041 error_mark_node as the type. It allows us to further the analysis
13042 of remaining nodes and detects more errors in certain cases. */
13043
13044 static tree
13045 patch_binop (node, wfl_op1, wfl_op2)
13046 tree node;
13047 tree wfl_op1;
13048 tree wfl_op2;
13049 {
13050 tree op1 = TREE_OPERAND (node, 0);
13051 tree op2 = TREE_OPERAND (node, 1);
13052 tree op1_type = TREE_TYPE (op1);
13053 tree op2_type = TREE_TYPE (op2);
13054 tree prom_type = NULL_TREE, cn;
13055 int code = TREE_CODE (node);
13056
13057 /* If 1, tell the routine that we have to return error_mark_node
13058 after checking for the initialization of the RHS */
13059 int error_found = 0;
13060
13061 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13062
13063 /* If either op<n>_type are NULL, this might be early signs of an
13064 error situation, unless it's too early to tell (in case we're
13065 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13066 correctly so the error can be later on reported accurately. */
13067 if (! (code == PLUS_EXPR || code == NE_EXPR
13068 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13069 {
13070 tree n;
13071 if (! op1_type)
13072 {
13073 n = java_complete_tree (op1);
13074 op1_type = TREE_TYPE (n);
13075 }
13076 if (! op2_type)
13077 {
13078 n = java_complete_tree (op2);
13079 op2_type = TREE_TYPE (n);
13080 }
13081 }
13082
13083 switch (code)
13084 {
13085 /* 15.16 Multiplicative operators */
13086 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13087 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13088 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13089 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13090 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13091 {
13092 if (!JNUMERIC_TYPE_P (op1_type))
13093 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13094 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13095 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13096 TREE_TYPE (node) = error_mark_node;
13097 error_found = 1;
13098 break;
13099 }
13100 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13101 /* Change the division operator if necessary */
13102 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13103 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13104
13105 if (TREE_CODE (prom_type) == INTEGER_TYPE
13106 && flag_use_divide_subroutine
13107 && ! flag_emit_class_files
13108 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13109 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13110
13111 /* This one is more complicated. FLOATs are processed by a
13112 function call to soft_fmod. Duplicate the value of the
13113 COMPOUND_ASSIGN_P flag. */
13114 if (code == TRUNC_MOD_EXPR)
13115 {
13116 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13117 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13118 TREE_SIDE_EFFECTS (mod)
13119 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13120 return mod;
13121 }
13122 break;
13123
13124 /* 15.17 Additive Operators */
13125 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13126
13127 /* Operation is valid if either one argument is a string
13128 constant, a String object or a StringBuffer crafted for the
13129 purpose of the a previous usage of the String concatenation
13130 operator */
13131
13132 if (TREE_CODE (op1) == STRING_CST
13133 || TREE_CODE (op2) == STRING_CST
13134 || JSTRING_TYPE_P (op1_type)
13135 || JSTRING_TYPE_P (op2_type)
13136 || IS_CRAFTED_STRING_BUFFER_P (op1)
13137 || IS_CRAFTED_STRING_BUFFER_P (op2))
13138 return build_string_concatenation (op1, op2);
13139
13140 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13141 Numeric Types */
13142 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13143 {
13144 if (!JNUMERIC_TYPE_P (op1_type))
13145 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13146 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13147 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13148 TREE_TYPE (node) = error_mark_node;
13149 error_found = 1;
13150 break;
13151 }
13152 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13153 break;
13154
13155 /* 15.18 Shift Operators */
13156 case LSHIFT_EXPR:
13157 case RSHIFT_EXPR:
13158 case URSHIFT_EXPR:
13159 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13160 {
13161 if (!JINTEGRAL_TYPE_P (op1_type))
13162 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13163 else
13164 {
13165 if (JNUMERIC_TYPE_P (op2_type))
13166 parse_error_context (wfl_operator,
13167 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13168 operator_string (node),
13169 lang_printable_name (op2_type, 0));
13170 else
13171 parse_error_context (wfl_operator,
13172 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13173 operator_string (node),
13174 lang_printable_name (op2_type, 0));
13175 }
13176 TREE_TYPE (node) = error_mark_node;
13177 error_found = 1;
13178 break;
13179 }
13180
13181 /* Unary numeric promotion (5.6.1) is performed on each operand
13182 separatly */
13183 op1 = do_unary_numeric_promotion (op1);
13184 op2 = do_unary_numeric_promotion (op2);
13185
13186 /* The type of the shift expression is the type of the promoted
13187 type of the left-hand operand */
13188 prom_type = TREE_TYPE (op1);
13189
13190 /* Shift int only up to 0x1f and long up to 0x3f */
13191 if (prom_type == int_type_node)
13192 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13193 build_int_2 (0x1f, 0)));
13194 else
13195 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13196 build_int_2 (0x3f, 0)));
13197
13198 /* The >>> operator is a >> operating on unsigned quantities */
13199 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13200 {
13201 tree to_return;
13202 tree utype = unsigned_type (prom_type);
13203 op1 = convert (utype, op1);
13204 TREE_SET_CODE (node, RSHIFT_EXPR);
13205 TREE_OPERAND (node, 0) = op1;
13206 TREE_OPERAND (node, 1) = op2;
13207 TREE_TYPE (node) = utype;
13208 to_return = convert (prom_type, node);
13209 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13210 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13211 TREE_SIDE_EFFECTS (to_return)
13212 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13213 return to_return;
13214 }
13215 break;
13216
13217 /* 15.19.1 Type Comparison Operator instaceof */
13218 case INSTANCEOF_EXPR:
13219
13220 TREE_TYPE (node) = boolean_type_node;
13221
13222 if (!(op2_type = resolve_type_during_patch (op2)))
13223 return error_mark_node;
13224
13225 /* The first operand must be a reference type or the null type */
13226 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13227 error_found = 1; /* Error reported further below */
13228
13229 /* The second operand must be a reference type */
13230 if (!JREFERENCE_TYPE_P (op2_type))
13231 {
13232 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13233 parse_error_context
13234 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13235 lang_printable_name (op2_type, 0));
13236 error_found = 1;
13237 }
13238
13239 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13240 {
13241 /* If the first operand is null, the result is always false */
13242 if (op1 == null_pointer_node)
13243 return boolean_false_node;
13244 else if (flag_emit_class_files)
13245 {
13246 TREE_OPERAND (node, 1) = op2_type;
13247 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13248 return node;
13249 }
13250 /* Otherwise we have to invoke instance of to figure it out */
13251 else
13252 return build_instanceof (op1, op2_type);
13253 }
13254 /* There is no way the expression operand can be an instance of
13255 the type operand. This is a compile time error. */
13256 else
13257 {
13258 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13259 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13260 parse_error_context
13261 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13262 t1, lang_printable_name (op2_type, 0));
13263 free (t1);
13264 error_found = 1;
13265 }
13266
13267 break;
13268
13269 /* 15.21 Bitwise and Logical Operators */
13270 case BIT_AND_EXPR:
13271 case BIT_XOR_EXPR:
13272 case BIT_IOR_EXPR:
13273 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13274 /* Binary numeric promotion is performed on both operand and the
13275 expression retain that type */
13276 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13277
13278 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13279 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13280 /* The type of the bitwise operator expression is BOOLEAN */
13281 prom_type = boolean_type_node;
13282 else
13283 {
13284 if (!JINTEGRAL_TYPE_P (op1_type))
13285 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13286 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13287 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13288 TREE_TYPE (node) = error_mark_node;
13289 error_found = 1;
13290 /* Insert a break here if adding thing before the switch's
13291 break for this case */
13292 }
13293 break;
13294
13295 /* 15.22 Conditional-And Operator */
13296 case TRUTH_ANDIF_EXPR:
13297 /* 15.23 Conditional-Or Operator */
13298 case TRUTH_ORIF_EXPR:
13299 /* Operands must be of BOOLEAN type */
13300 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13301 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13302 {
13303 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13304 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13305 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13306 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13307 TREE_TYPE (node) = boolean_type_node;
13308 error_found = 1;
13309 break;
13310 }
13311 /* The type of the conditional operators is BOOLEAN */
13312 prom_type = boolean_type_node;
13313 break;
13314
13315 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13316 case LT_EXPR:
13317 case GT_EXPR:
13318 case LE_EXPR:
13319 case GE_EXPR:
13320 /* The type of each of the operands must be a primitive numeric
13321 type */
13322 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13323 {
13324 if (!JNUMERIC_TYPE_P (op1_type))
13325 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13326 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13327 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13328 TREE_TYPE (node) = boolean_type_node;
13329 error_found = 1;
13330 break;
13331 }
13332 /* Binary numeric promotion is performed on the operands */
13333 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13334 /* The type of the relation expression is always BOOLEAN */
13335 prom_type = boolean_type_node;
13336 break;
13337
13338 /* 15.20 Equality Operator */
13339 case EQ_EXPR:
13340 case NE_EXPR:
13341 /* It's time for us to patch the strings. */
13342 if ((cn = patch_string (op1)))
13343 {
13344 op1 = cn;
13345 op1_type = TREE_TYPE (op1);
13346 }
13347 if ((cn = patch_string (op2)))
13348 {
13349 op2 = cn;
13350 op2_type = TREE_TYPE (op2);
13351 }
13352
13353 /* 15.20.1 Numerical Equality Operators == and != */
13354 /* Binary numeric promotion is performed on the operands */
13355 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13356 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13357
13358 /* 15.20.2 Boolean Equality Operators == and != */
13359 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13360 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13361 ; /* Nothing to do here */
13362
13363 /* 15.20.3 Reference Equality Operators == and != */
13364 /* Types have to be either references or the null type. If
13365 they're references, it must be possible to convert either
13366 type to the other by casting conversion. */
13367 else if (op1 == null_pointer_node || op2 == null_pointer_node
13368 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13369 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13370 || valid_ref_assignconv_cast_p (op2_type,
13371 op1_type, 1))))
13372 ; /* Nothing to do here */
13373
13374 /* Else we have an error figure what can't be converted into
13375 what and report the error */
13376 else
13377 {
13378 char *t1;
13379 t1 = xstrdup (lang_printable_name (op1_type, 0));
13380 parse_error_context
13381 (wfl_operator,
13382 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13383 operator_string (node), t1,
13384 lang_printable_name (op2_type, 0));
13385 free (t1);
13386 TREE_TYPE (node) = boolean_type_node;
13387 error_found = 1;
13388 break;
13389 }
13390 prom_type = boolean_type_node;
13391 break;
13392 }
13393
13394 if (error_found)
13395 return error_mark_node;
13396
13397 TREE_OPERAND (node, 0) = op1;
13398 TREE_OPERAND (node, 1) = op2;
13399 TREE_TYPE (node) = prom_type;
13400 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13401
13402 if (flag_emit_xref)
13403 return node;
13404
13405 /* fold does not respect side-effect order as required for Java but not C.
13406 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13407 * bytecode.
13408 */
13409 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13410 : ! TREE_SIDE_EFFECTS (node))
13411 node = fold (node);
13412 return node;
13413 }
13414
13415 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13416 zero value, the value of CSTE comes after the valude of STRING */
13417
13418 static tree
13419 do_merge_string_cste (cste, string, string_len, after)
13420 tree cste;
13421 const char *string;
13422 int string_len, after;
13423 {
13424 const char *old = TREE_STRING_POINTER (cste);
13425 int old_len = TREE_STRING_LENGTH (cste);
13426 int len = old_len + string_len;
13427 char *new = alloca (len+1);
13428
13429 if (after)
13430 {
13431 memcpy (new, string, string_len);
13432 memcpy (&new [string_len], old, old_len);
13433 }
13434 else
13435 {
13436 memcpy (new, old, old_len);
13437 memcpy (&new [old_len], string, string_len);
13438 }
13439 new [len] = '\0';
13440 return build_string (len, new);
13441 }
13442
13443 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13444 new STRING_CST on success, NULL_TREE on failure */
13445
13446 static tree
13447 merge_string_cste (op1, op2, after)
13448 tree op1, op2;
13449 int after;
13450 {
13451 /* Handle two string constants right away */
13452 if (TREE_CODE (op2) == STRING_CST)
13453 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13454 TREE_STRING_LENGTH (op2), after);
13455
13456 /* Reasonable integer constant can be treated right away */
13457 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13458 {
13459 static const char *boolean_true = "true";
13460 static const char *boolean_false = "false";
13461 static const char *null_pointer = "null";
13462 char ch[3];
13463 const char *string;
13464
13465 if (op2 == boolean_true_node)
13466 string = boolean_true;
13467 else if (op2 == boolean_false_node)
13468 string = boolean_false;
13469 else if (op2 == null_pointer_node)
13470 string = null_pointer;
13471 else if (TREE_TYPE (op2) == char_type_node)
13472 {
13473 ch[0] = (char )TREE_INT_CST_LOW (op2);
13474 ch[1] = '\0';
13475 string = ch;
13476 }
13477 else
13478 string = print_int_node (op2);
13479
13480 return do_merge_string_cste (op1, string, strlen (string), after);
13481 }
13482 return NULL_TREE;
13483 }
13484
13485 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13486 has to be a STRING_CST and the other part must be a STRING_CST or a
13487 INTEGRAL constant. Return a new STRING_CST if the operation
13488 succeed, NULL_TREE otherwise.
13489
13490 If the case we want to optimize for space, we might want to return
13491 NULL_TREE for each invocation of this routine. FIXME */
13492
13493 static tree
13494 string_constant_concatenation (op1, op2)
13495 tree op1, op2;
13496 {
13497 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13498 {
13499 tree string, rest;
13500 int invert;
13501
13502 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13503 rest = (string == op1 ? op2 : op1);
13504 invert = (string == op1 ? 0 : 1 );
13505
13506 /* Walk REST, only if it looks reasonable */
13507 if (TREE_CODE (rest) != STRING_CST
13508 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13509 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13510 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13511 {
13512 rest = java_complete_tree (rest);
13513 if (rest == error_mark_node)
13514 return error_mark_node;
13515 rest = fold (rest);
13516 }
13517 return merge_string_cste (string, rest, invert);
13518 }
13519 return NULL_TREE;
13520 }
13521
13522 /* Implement the `+' operator. Does static optimization if possible,
13523 otherwise create (if necessary) and append elements to a
13524 StringBuffer. The StringBuffer will be carried around until it is
13525 used for a function call or an assignment. Then toString() will be
13526 called on it to turn it into a String object. */
13527
13528 static tree
13529 build_string_concatenation (op1, op2)
13530 tree op1, op2;
13531 {
13532 tree result;
13533 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13534
13535 if (flag_emit_xref)
13536 return build (PLUS_EXPR, string_type_node, op1, op2);
13537
13538 /* Try to do some static optimization */
13539 if ((result = string_constant_concatenation (op1, op2)))
13540 return result;
13541
13542 /* Discard empty strings on either side of the expression */
13543 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13544 {
13545 op1 = op2;
13546 op2 = NULL_TREE;
13547 }
13548 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13549 op2 = NULL_TREE;
13550
13551 /* If operands are string constant, turn then into object references */
13552 if (TREE_CODE (op1) == STRING_CST)
13553 op1 = patch_string_cst (op1);
13554 if (op2 && TREE_CODE (op2) == STRING_CST)
13555 op2 = patch_string_cst (op2);
13556
13557 /* If either one of the constant is null and the other non null
13558 operand is a String object, return it. */
13559 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13560 return op1;
13561
13562 /* If OP1 isn't already a StringBuffer, create and
13563 initialize a new one */
13564 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13565 {
13566 /* Two solutions here:
13567 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13568 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13569 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13570 op1 = BUILD_STRING_BUFFER (op1);
13571 else
13572 {
13573 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13574 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13575 }
13576 }
13577
13578 if (op2)
13579 {
13580 /* OP1 is no longer the last node holding a crafted StringBuffer */
13581 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13582 /* Create a node for `{new...,xxx}.append (op2)' */
13583 if (op2)
13584 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13585 }
13586
13587 /* Mark the last node holding a crafted StringBuffer */
13588 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13589
13590 TREE_SIDE_EFFECTS (op1) = side_effects;
13591 return op1;
13592 }
13593
13594 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13595 StringBuffer. If no string were found to be patched, return
13596 NULL. */
13597
13598 static tree
13599 patch_string (node)
13600 tree node;
13601 {
13602 if (node == error_mark_node)
13603 return error_mark_node;
13604 if (TREE_CODE (node) == STRING_CST)
13605 return patch_string_cst (node);
13606 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13607 {
13608 int saved = ctxp->explicit_constructor_p;
13609 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13610 tree ret;
13611 /* Temporary disable forbid the use of `this'. */
13612 ctxp->explicit_constructor_p = 0;
13613 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13614 /* String concatenation arguments must be evaluated in order too. */
13615 ret = force_evaluation_order (ret);
13616 /* Restore it at its previous value */
13617 ctxp->explicit_constructor_p = saved;
13618 return ret;
13619 }
13620 return NULL_TREE;
13621 }
13622
13623 /* Build the internal representation of a string constant. */
13624
13625 static tree
13626 patch_string_cst (node)
13627 tree node;
13628 {
13629 int location;
13630 if (! flag_emit_class_files)
13631 {
13632 node = get_identifier (TREE_STRING_POINTER (node));
13633 location = alloc_name_constant (CONSTANT_String, node);
13634 node = build_ref_from_constant_pool (location);
13635 }
13636 TREE_TYPE (node) = string_ptr_type_node;
13637 TREE_CONSTANT (node) = 1;
13638 return node;
13639 }
13640
13641 /* Build an incomplete unary operator expression. */
13642
13643 static tree
13644 build_unaryop (op_token, op_location, op1)
13645 int op_token, op_location;
13646 tree op1;
13647 {
13648 enum tree_code op;
13649 tree unaryop;
13650 switch (op_token)
13651 {
13652 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13653 case MINUS_TK: op = NEGATE_EXPR; break;
13654 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13655 case NOT_TK: op = BIT_NOT_EXPR; break;
13656 default: abort ();
13657 }
13658
13659 unaryop = build1 (op, NULL_TREE, op1);
13660 TREE_SIDE_EFFECTS (unaryop) = 1;
13661 /* Store the location of the operator, for better error report. The
13662 string of the operator will be rebuild based on the OP value. */
13663 EXPR_WFL_LINECOL (unaryop) = op_location;
13664 return unaryop;
13665 }
13666
13667 /* Special case for the ++/-- operators, since they require an extra
13668 argument to build, which is set to NULL and patched
13669 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13670
13671 static tree
13672 build_incdec (op_token, op_location, op1, is_post_p)
13673 int op_token, op_location;
13674 tree op1;
13675 int is_post_p;
13676 {
13677 static enum tree_code lookup [2][2] =
13678 {
13679 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13680 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13681 };
13682 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13683 NULL_TREE, op1, NULL_TREE);
13684 TREE_SIDE_EFFECTS (node) = 1;
13685 /* Store the location of the operator, for better error report. The
13686 string of the operator will be rebuild based on the OP value. */
13687 EXPR_WFL_LINECOL (node) = op_location;
13688 return node;
13689 }
13690
13691 /* Build an incomplete cast operator, based on the use of the
13692 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13693 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13694 though its type is already set. */
13695
13696 static tree
13697 build_cast (location, type, exp)
13698 int location;
13699 tree type, exp;
13700 {
13701 tree node = build1 (CONVERT_EXPR, type, exp);
13702 EXPR_WFL_LINECOL (node) = location;
13703 return node;
13704 }
13705
13706 /* Build an incomplete class reference operator. */
13707 static tree
13708 build_incomplete_class_ref (location, class_name)
13709 int location;
13710 tree class_name;
13711 {
13712 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13713 EXPR_WFL_LINECOL (node) = location;
13714 return node;
13715 }
13716
13717 /* Complete an incomplete class reference operator. */
13718 static tree
13719 patch_incomplete_class_ref (node)
13720 tree node;
13721 {
13722 tree type = TREE_OPERAND (node, 0);
13723 tree ref_type;
13724
13725 if (!(ref_type = resolve_type_during_patch (type)))
13726 return error_mark_node;
13727
13728 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13729 {
13730 /* A class referenced by `foo.class' is initialized. */
13731 return build_class_init (ref_type, build_class_ref (ref_type));
13732 }
13733
13734 /* If we're emitting class files and we have to deal with non
13735 primitive types, we invoke (and consider generating) the
13736 synthetic static method `class$'. */
13737 if (!TYPE_DOT_CLASS (current_class))
13738 build_dot_class_method (current_class);
13739 ref_type = build_dot_class_method_invocation (ref_type);
13740 return java_complete_tree (ref_type);
13741 }
13742
13743 /* 15.14 Unary operators. We return error_mark_node in case of error,
13744 but preserve the type of NODE if the type is fixed. */
13745
13746 static tree
13747 patch_unaryop (node, wfl_op)
13748 tree node;
13749 tree wfl_op;
13750 {
13751 tree op = TREE_OPERAND (node, 0);
13752 tree op_type = TREE_TYPE (op);
13753 tree prom_type = NULL_TREE, value, decl;
13754 int outer_field_flag = 0;
13755 int code = TREE_CODE (node);
13756 int error_found = 0;
13757
13758 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13759
13760 switch (code)
13761 {
13762 /* 15.13.2 Postfix Increment Operator ++ */
13763 case POSTINCREMENT_EXPR:
13764 /* 15.13.3 Postfix Increment Operator -- */
13765 case POSTDECREMENT_EXPR:
13766 /* 15.14.1 Prefix Increment Operator ++ */
13767 case PREINCREMENT_EXPR:
13768 /* 15.14.2 Prefix Decrement Operator -- */
13769 case PREDECREMENT_EXPR:
13770 op = decl = strip_out_static_field_access_decl (op);
13771 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13772 /* We might be trying to change an outer field accessed using
13773 access method. */
13774 if (outer_field_flag)
13775 {
13776 /* Retrieve the decl of the field we're trying to access. We
13777 do that by first retrieving the function we would call to
13778 access the field. It has been already verified that this
13779 field isn't final */
13780 if (flag_emit_class_files)
13781 decl = TREE_OPERAND (op, 0);
13782 else
13783 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13784 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13785 }
13786 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13787 else if (!JDECL_P (decl)
13788 && TREE_CODE (decl) != COMPONENT_REF
13789 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13790 && TREE_CODE (decl) != INDIRECT_REF
13791 && !(TREE_CODE (decl) == COMPOUND_EXPR
13792 && TREE_OPERAND (decl, 1)
13793 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13794 {
13795 tree lvalue;
13796 /* Before screaming, check that we're not in fact trying to
13797 increment a optimized static final access, in which case
13798 we issue an different error message. */
13799 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13800 && resolve_expression_name (wfl_op, &lvalue)
13801 && check_final_assignment (lvalue, wfl_op)))
13802 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13803 operator_string (node));
13804 TREE_TYPE (node) = error_mark_node;
13805 error_found = 1;
13806 }
13807
13808 if (check_final_assignment (op, wfl_op))
13809 error_found = 1;
13810
13811 /* From now on, we know that op if a variable and that it has a
13812 valid wfl. We use wfl_op to locate errors related to the
13813 ++/-- operand. */
13814 else if (!JNUMERIC_TYPE_P (op_type))
13815 {
13816 parse_error_context
13817 (wfl_op, "Invalid argument type `%s' to `%s'",
13818 lang_printable_name (op_type, 0), operator_string (node));
13819 TREE_TYPE (node) = error_mark_node;
13820 error_found = 1;
13821 }
13822 else
13823 {
13824 /* Before the addition, binary numeric promotion is performed on
13825 both operands, if really necessary */
13826 if (JINTEGRAL_TYPE_P (op_type))
13827 {
13828 value = build_int_2 (1, 0);
13829 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13830 }
13831 else
13832 {
13833 value = build_int_2 (1, 0);
13834 TREE_TYPE (node) =
13835 binary_numeric_promotion (op_type,
13836 TREE_TYPE (value), &op, &value);
13837 }
13838
13839 /* We remember we might be accessing an outer field */
13840 if (outer_field_flag)
13841 {
13842 /* We re-generate an access to the field */
13843 value = build (PLUS_EXPR, TREE_TYPE (op),
13844 build_outer_field_access (wfl_op, decl), value);
13845
13846 /* And we patch the original access$() into a write
13847 with plus_op as a rhs */
13848 return outer_field_access_fix (node, op, value);
13849 }
13850
13851 /* And write back into the node. */
13852 TREE_OPERAND (node, 0) = op;
13853 TREE_OPERAND (node, 1) = value;
13854 /* Convert the overall back into its original type, if
13855 necessary, and return */
13856 if (JINTEGRAL_TYPE_P (op_type))
13857 return fold (node);
13858 else
13859 return fold (convert (op_type, node));
13860 }
13861 break;
13862
13863 /* 15.14.3 Unary Plus Operator + */
13864 case UNARY_PLUS_EXPR:
13865 /* 15.14.4 Unary Minus Operator - */
13866 case NEGATE_EXPR:
13867 if (!JNUMERIC_TYPE_P (op_type))
13868 {
13869 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13870 TREE_TYPE (node) = error_mark_node;
13871 error_found = 1;
13872 }
13873 /* Unary numeric promotion is performed on operand */
13874 else
13875 {
13876 op = do_unary_numeric_promotion (op);
13877 prom_type = TREE_TYPE (op);
13878 if (code == UNARY_PLUS_EXPR)
13879 return fold (op);
13880 }
13881 break;
13882
13883 /* 15.14.5 Bitwise Complement Operator ~ */
13884 case BIT_NOT_EXPR:
13885 if (!JINTEGRAL_TYPE_P (op_type))
13886 {
13887 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13888 TREE_TYPE (node) = error_mark_node;
13889 error_found = 1;
13890 }
13891 else
13892 {
13893 op = do_unary_numeric_promotion (op);
13894 prom_type = TREE_TYPE (op);
13895 }
13896 break;
13897
13898 /* 15.14.6 Logical Complement Operator ! */
13899 case TRUTH_NOT_EXPR:
13900 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13901 {
13902 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13903 /* But the type is known. We will report an error if further
13904 attempt of a assignment is made with this rhs */
13905 TREE_TYPE (node) = boolean_type_node;
13906 error_found = 1;
13907 }
13908 else
13909 prom_type = boolean_type_node;
13910 break;
13911
13912 /* 15.15 Cast Expression */
13913 case CONVERT_EXPR:
13914 value = patch_cast (node, wfl_operator);
13915 if (value == error_mark_node)
13916 {
13917 /* If this cast is part of an assignment, we tell the code
13918 that deals with it not to complain about a mismatch,
13919 because things have been cast, anyways */
13920 TREE_TYPE (node) = error_mark_node;
13921 error_found = 1;
13922 }
13923 else
13924 {
13925 value = fold (value);
13926 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13927 return value;
13928 }
13929 break;
13930 }
13931
13932 if (error_found)
13933 return error_mark_node;
13934
13935 /* There are cases where node has been replaced by something else
13936 and we don't end up returning here: UNARY_PLUS_EXPR,
13937 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13938 TREE_OPERAND (node, 0) = fold (op);
13939 TREE_TYPE (node) = prom_type;
13940 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13941 return fold (node);
13942 }
13943
13944 /* Generic type resolution that sometimes takes place during node
13945 patching. Returned the resolved type or generate an error
13946 message. Return the resolved type or NULL_TREE. */
13947
13948 static tree
13949 resolve_type_during_patch (type)
13950 tree type;
13951 {
13952 if (unresolved_type_p (type, NULL))
13953 {
13954 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
13955 if (!type_decl)
13956 {
13957 parse_error_context (type,
13958 "Class `%s' not found in type declaration",
13959 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13960 return NULL_TREE;
13961 }
13962 else
13963 {
13964 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13965 return TREE_TYPE (type_decl);
13966 }
13967 }
13968 return type;
13969 }
13970 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13971 found. Otherwise NODE or something meant to replace it is returned. */
13972
13973 static tree
13974 patch_cast (node, wfl_op)
13975 tree node;
13976 tree wfl_op;
13977 {
13978 tree op = TREE_OPERAND (node, 0);
13979 tree op_type = TREE_TYPE (op);
13980 tree cast_type = TREE_TYPE (node);
13981 char *t1;
13982
13983 /* First resolve OP_TYPE if unresolved */
13984 if (!(cast_type = resolve_type_during_patch (cast_type)))
13985 return error_mark_node;
13986
13987 /* Check on cast that are proven correct at compile time */
13988 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13989 {
13990 /* Same type */
13991 if (cast_type == op_type)
13992 return node;
13993
13994 /* float and double type are converted to the original type main
13995 variant and then to the target type. */
13996 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13997 op = convert (integer_type_node, op);
13998
13999 /* Try widening/narowwing convertion. Potentially, things need
14000 to be worked out in gcc so we implement the extreme cases
14001 correctly. fold_convert() needs to be fixed. */
14002 return convert (cast_type, op);
14003 }
14004
14005 /* It's also valid to cast a boolean into a boolean */
14006 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14007 return node;
14008
14009 /* null can be casted to references */
14010 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14011 return build_null_of_type (cast_type);
14012
14013 /* The remaining legal casts involve conversion between reference
14014 types. Check for their compile time correctness. */
14015 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14016 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14017 {
14018 TREE_TYPE (node) = promote_type (cast_type);
14019 /* Now, the case can be determined correct at compile time if
14020 OP_TYPE can be converted into CAST_TYPE by assignment
14021 conversion (5.2) */
14022
14023 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14024 {
14025 TREE_SET_CODE (node, NOP_EXPR);
14026 return node;
14027 }
14028
14029 if (flag_emit_class_files)
14030 {
14031 TREE_SET_CODE (node, CONVERT_EXPR);
14032 return node;
14033 }
14034
14035 /* The cast requires a run-time check */
14036 return build (CALL_EXPR, promote_type (cast_type),
14037 build_address_of (soft_checkcast_node),
14038 tree_cons (NULL_TREE, build_class_ref (cast_type),
14039 build_tree_list (NULL_TREE, op)),
14040 NULL_TREE);
14041 }
14042
14043 /* Any other casts are proven incorrect at compile time */
14044 t1 = xstrdup (lang_printable_name (op_type, 0));
14045 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14046 t1, lang_printable_name (cast_type, 0));
14047 free (t1);
14048 return error_mark_node;
14049 }
14050
14051 /* Build a null constant and give it the type TYPE. */
14052
14053 static tree
14054 build_null_of_type (type)
14055 tree type;
14056 {
14057 tree node = build_int_2 (0, 0);
14058 TREE_TYPE (node) = promote_type (type);
14059 return node;
14060 }
14061
14062 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14063 a list of indices. */
14064 static tree
14065 build_array_ref (location, array, index)
14066 int location;
14067 tree array, index;
14068 {
14069 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14070 EXPR_WFL_LINECOL (node) = location;
14071 return node;
14072 }
14073
14074 /* 15.12 Array Access Expression */
14075
14076 static tree
14077 patch_array_ref (node)
14078 tree node;
14079 {
14080 tree array = TREE_OPERAND (node, 0);
14081 tree array_type = TREE_TYPE (array);
14082 tree index = TREE_OPERAND (node, 1);
14083 tree index_type = TREE_TYPE (index);
14084 int error_found = 0;
14085
14086 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14087
14088 if (TREE_CODE (array_type) == POINTER_TYPE)
14089 array_type = TREE_TYPE (array_type);
14090
14091 /* The array reference must be an array */
14092 if (!TYPE_ARRAY_P (array_type))
14093 {
14094 parse_error_context
14095 (wfl_operator,
14096 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14097 lang_printable_name (array_type, 0));
14098 TREE_TYPE (node) = error_mark_node;
14099 error_found = 1;
14100 }
14101
14102 /* The array index undergoes unary numeric promotion. The promoted
14103 type must be int */
14104 index = do_unary_numeric_promotion (index);
14105 if (TREE_TYPE (index) != int_type_node)
14106 {
14107 if (valid_cast_to_p (index_type, int_type_node))
14108 parse_error_context (wfl_operator,
14109 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14110 lang_printable_name (index_type, 0));
14111 else
14112 parse_error_context (wfl_operator,
14113 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14114 lang_printable_name (index_type, 0));
14115 TREE_TYPE (node) = error_mark_node;
14116 error_found = 1;
14117 }
14118
14119 if (error_found)
14120 return error_mark_node;
14121
14122 array_type = TYPE_ARRAY_ELEMENT (array_type);
14123
14124 if (flag_emit_class_files || flag_emit_xref)
14125 {
14126 TREE_OPERAND (node, 0) = array;
14127 TREE_OPERAND (node, 1) = index;
14128 }
14129 else
14130 {
14131 /* The save_expr is for correct evaluation order. It would be cleaner
14132 to use force_evaluation_order (see comment there), but that is
14133 difficult when we also have to deal with bounds checking. */
14134 if (TREE_SIDE_EFFECTS (index))
14135 array = save_expr (array);
14136 node = build_java_arrayaccess (array, array_type, index);
14137 if (TREE_SIDE_EFFECTS (index))
14138 node = build (COMPOUND_EXPR, array_type, array, node);
14139 }
14140 TREE_TYPE (node) = array_type;
14141 return node;
14142 }
14143
14144 /* 15.9 Array Creation Expressions */
14145
14146 static tree
14147 build_newarray_node (type, dims, extra_dims)
14148 tree type;
14149 tree dims;
14150 int extra_dims;
14151 {
14152 tree node =
14153 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14154 build_int_2 (extra_dims, 0));
14155 return node;
14156 }
14157
14158 static tree
14159 patch_newarray (node)
14160 tree node;
14161 {
14162 tree type = TREE_OPERAND (node, 0);
14163 tree dims = TREE_OPERAND (node, 1);
14164 tree cdim, array_type;
14165 int error_found = 0;
14166 int ndims = 0;
14167 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14168
14169 /* Dimension types are verified. It's better for the types to be
14170 verified in order. */
14171 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14172 {
14173 int dim_error = 0;
14174 tree dim = TREE_VALUE (cdim);
14175
14176 /* Dim might have been saved during its evaluation */
14177 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14178
14179 /* The type of each specified dimension must be an integral type. */
14180 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14181 dim_error = 1;
14182
14183 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14184 promoted type must be int. */
14185 else
14186 {
14187 dim = do_unary_numeric_promotion (dim);
14188 if (TREE_TYPE (dim) != int_type_node)
14189 dim_error = 1;
14190 }
14191
14192 /* Report errors on types here */
14193 if (dim_error)
14194 {
14195 parse_error_context
14196 (TREE_PURPOSE (cdim),
14197 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14198 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14199 "Explicit cast needed to" : "Can't"),
14200 lang_printable_name (TREE_TYPE (dim), 0));
14201 error_found = 1;
14202 }
14203
14204 TREE_PURPOSE (cdim) = NULL_TREE;
14205 }
14206
14207 /* Resolve array base type if unresolved */
14208 if (!(type = resolve_type_during_patch (type)))
14209 error_found = 1;
14210
14211 if (error_found)
14212 {
14213 /* We don't want further evaluation of this bogus array creation
14214 operation */
14215 TREE_TYPE (node) = error_mark_node;
14216 return error_mark_node;
14217 }
14218
14219 /* Set array_type to the actual (promoted) array type of the result. */
14220 if (TREE_CODE (type) == RECORD_TYPE)
14221 type = build_pointer_type (type);
14222 while (--xdims >= 0)
14223 {
14224 type = promote_type (build_java_array_type (type, -1));
14225 }
14226 dims = nreverse (dims);
14227 array_type = type;
14228 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14229 {
14230 type = array_type;
14231 array_type
14232 = build_java_array_type (type,
14233 TREE_CODE (cdim) == INTEGER_CST
14234 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14235 : -1);
14236 array_type = promote_type (array_type);
14237 }
14238 dims = nreverse (dims);
14239
14240 /* The node is transformed into a function call. Things are done
14241 differently according to the number of dimensions. If the number
14242 of dimension is equal to 1, then the nature of the base type
14243 (primitive or not) matters. */
14244 if (ndims == 1)
14245 return build_new_array (type, TREE_VALUE (dims));
14246
14247 /* Can't reuse what's already written in expr.c because it uses the
14248 JVM stack representation. Provide a build_multianewarray. FIXME */
14249 return build (CALL_EXPR, array_type,
14250 build_address_of (soft_multianewarray_node),
14251 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14252 tree_cons (NULL_TREE,
14253 build_int_2 (ndims, 0), dims )),
14254 NULL_TREE);
14255 }
14256
14257 /* 10.6 Array initializer. */
14258
14259 /* Build a wfl for array element that don't have one, so we can
14260 pin-point errors. */
14261
14262 static tree
14263 maybe_build_array_element_wfl (node)
14264 tree node;
14265 {
14266 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14267 return build_expr_wfl (NULL_TREE, ctxp->filename,
14268 ctxp->elc.line, ctxp->elc.prev_col);
14269 else
14270 return NULL_TREE;
14271 }
14272
14273 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14274 identification of initialized arrays easier to detect during walk
14275 and expansion. */
14276
14277 static tree
14278 build_new_array_init (location, values)
14279 int location;
14280 tree values;
14281 {
14282 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14283 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14284 EXPR_WFL_LINECOL (to_return) = location;
14285 return to_return;
14286 }
14287
14288 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14289 occurred. Otherwise return NODE after having set its type
14290 appropriately. */
14291
14292 static tree
14293 patch_new_array_init (type, node)
14294 tree type, node;
14295 {
14296 int error_seen = 0;
14297 tree current, element_type;
14298 HOST_WIDE_INT length;
14299 int all_constant = 1;
14300 tree init = TREE_OPERAND (node, 0);
14301
14302 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14303 {
14304 parse_error_context (node,
14305 "Invalid array initializer for non-array type `%s'",
14306 lang_printable_name (type, 1));
14307 return error_mark_node;
14308 }
14309 type = TREE_TYPE (type);
14310 element_type = TYPE_ARRAY_ELEMENT (type);
14311
14312 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14313
14314 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14315 current; length++, current = TREE_CHAIN (current))
14316 {
14317 tree elt = TREE_VALUE (current);
14318 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14319 {
14320 error_seen |= array_constructor_check_entry (element_type, current);
14321 elt = TREE_VALUE (current);
14322 /* When compiling to native code, STRING_CST is converted to
14323 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14324 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14325 all_constant = 0;
14326 }
14327 else
14328 {
14329 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14330 TREE_PURPOSE (current) = NULL_TREE;
14331 all_constant = 0;
14332 }
14333 if (elt && TREE_CODE (elt) == TREE_LIST
14334 && TREE_VALUE (elt) == error_mark_node)
14335 error_seen = 1;
14336 }
14337
14338 if (error_seen)
14339 return error_mark_node;
14340
14341 /* Create a new type. We can't reuse the one we have here by
14342 patching its dimension because it originally is of dimension -1
14343 hence reused by gcc. This would prevent triangular arrays. */
14344 type = build_java_array_type (element_type, length);
14345 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14346 TREE_TYPE (node) = promote_type (type);
14347 TREE_CONSTANT (init) = all_constant;
14348 TREE_CONSTANT (node) = all_constant;
14349 return node;
14350 }
14351
14352 /* Verify that one entry of the initializer element list can be
14353 assigned to the array base type. Report 1 if an error occurred, 0
14354 otherwise. */
14355
14356 static int
14357 array_constructor_check_entry (type, entry)
14358 tree type, entry;
14359 {
14360 char *array_type_string = NULL; /* For error reports */
14361 tree value, type_value, new_value, wfl_value, patched;
14362 int error_seen = 0;
14363
14364 new_value = NULL_TREE;
14365 wfl_value = TREE_VALUE (entry);
14366
14367 value = java_complete_tree (TREE_VALUE (entry));
14368 /* patch_string return error_mark_node if arg is error_mark_node */
14369 if ((patched = patch_string (value)))
14370 value = patched;
14371 if (value == error_mark_node)
14372 return 1;
14373
14374 type_value = TREE_TYPE (value);
14375
14376 /* At anytime, try_builtin_assignconv can report a warning on
14377 constant overflow during narrowing. */
14378 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14379 new_value = try_builtin_assignconv (wfl_operator, type, value);
14380 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14381 type_value = promote_type (type);
14382
14383 /* Check and report errors */
14384 if (!new_value)
14385 {
14386 const char *msg = (!valid_cast_to_p (type_value, type) ?
14387 "Can't" : "Explicit cast needed to");
14388 if (!array_type_string)
14389 array_type_string = xstrdup (lang_printable_name (type, 1));
14390 parse_error_context
14391 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14392 msg, lang_printable_name (type_value, 1), array_type_string);
14393 error_seen = 1;
14394 }
14395
14396 if (new_value)
14397 {
14398 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14399 TREE_VALUE (entry) = new_value;
14400 }
14401
14402 if (array_type_string)
14403 free (array_type_string);
14404
14405 TREE_PURPOSE (entry) = NULL_TREE;
14406 return error_seen;
14407 }
14408
14409 static tree
14410 build_this (location)
14411 int location;
14412 {
14413 tree node = build_wfl_node (this_identifier_node);
14414 TREE_SET_CODE (node, THIS_EXPR);
14415 EXPR_WFL_LINECOL (node) = location;
14416 return node;
14417 }
14418
14419 /* 14.15 The return statement. It builds a modify expression that
14420 assigns the returned value to the RESULT_DECL that hold the value
14421 to be returned. */
14422
14423 static tree
14424 build_return (location, op)
14425 int location;
14426 tree op;
14427 {
14428 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14429 EXPR_WFL_LINECOL (node) = location;
14430 node = build_debugable_stmt (location, node);
14431 return node;
14432 }
14433
14434 static tree
14435 patch_return (node)
14436 tree node;
14437 {
14438 tree return_exp = TREE_OPERAND (node, 0);
14439 tree meth = current_function_decl;
14440 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14441 int error_found = 0;
14442
14443 TREE_TYPE (node) = error_mark_node;
14444 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14445
14446 /* It's invalid to have a return value within a function that is
14447 declared with the keyword void or that is a constructor */
14448 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14449 error_found = 1;
14450
14451 /* It's invalid to use a return statement in a static block */
14452 if (DECL_CLINIT_P (current_function_decl))
14453 error_found = 1;
14454
14455 /* It's invalid to have a no return value within a function that
14456 isn't declared with the keyword `void' */
14457 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14458 error_found = 2;
14459
14460 if (in_instance_initializer)
14461 error_found = 1;
14462
14463 if (error_found)
14464 {
14465 if (in_instance_initializer)
14466 parse_error_context (wfl_operator,
14467 "`return' inside instance initializer");
14468
14469 else if (DECL_CLINIT_P (current_function_decl))
14470 parse_error_context (wfl_operator,
14471 "`return' inside static initializer");
14472
14473 else if (!DECL_CONSTRUCTOR_P (meth))
14474 {
14475 char *t = xstrdup (lang_printable_name (mtype, 0));
14476 parse_error_context (wfl_operator,
14477 "`return' with%s value from `%s %s'",
14478 (error_found == 1 ? "" : "out"),
14479 t, lang_printable_name (meth, 0));
14480 free (t);
14481 }
14482 else
14483 parse_error_context (wfl_operator,
14484 "`return' with value from constructor `%s'",
14485 lang_printable_name (meth, 0));
14486 return error_mark_node;
14487 }
14488
14489 /* If we have a return_exp, build a modify expression and expand
14490 it. Note: at that point, the assignment is declared valid, but we
14491 may want to carry some more hacks */
14492 if (return_exp)
14493 {
14494 tree exp = java_complete_tree (return_exp);
14495 tree modify, patched;
14496
14497 /* If the function returned value and EXP are booleans, EXP has
14498 to be converted into the type of DECL_RESULT, which is integer
14499 (see complete_start_java_method) */
14500 if (TREE_TYPE (exp) == boolean_type_node &&
14501 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14502 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14503
14504 /* `null' can be assigned to a function returning a reference */
14505 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14506 exp == null_pointer_node)
14507 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14508
14509 if ((patched = patch_string (exp)))
14510 exp = patched;
14511
14512 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14513 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14514 modify = java_complete_tree (modify);
14515
14516 if (modify != error_mark_node)
14517 {
14518 TREE_SIDE_EFFECTS (modify) = 1;
14519 TREE_OPERAND (node, 0) = modify;
14520 }
14521 else
14522 return error_mark_node;
14523 }
14524 TREE_TYPE (node) = void_type_node;
14525 TREE_SIDE_EFFECTS (node) = 1;
14526 return node;
14527 }
14528
14529 /* 14.8 The if Statement */
14530
14531 static tree
14532 build_if_else_statement (location, expression, if_body, else_body)
14533 int location;
14534 tree expression, if_body, else_body;
14535 {
14536 tree node;
14537 if (!else_body)
14538 else_body = empty_stmt_node;
14539 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14540 EXPR_WFL_LINECOL (node) = location;
14541 node = build_debugable_stmt (location, node);
14542 return node;
14543 }
14544
14545 static tree
14546 patch_if_else_statement (node)
14547 tree node;
14548 {
14549 tree expression = TREE_OPERAND (node, 0);
14550
14551 TREE_TYPE (node) = error_mark_node;
14552 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14553
14554 /* The type of expression must be boolean */
14555 if (TREE_TYPE (expression) != boolean_type_node
14556 && TREE_TYPE (expression) != promoted_boolean_type_node)
14557 {
14558 parse_error_context
14559 (wfl_operator,
14560 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14561 lang_printable_name (TREE_TYPE (expression), 0));
14562 return error_mark_node;
14563 }
14564
14565 TREE_TYPE (node) = void_type_node;
14566 TREE_SIDE_EFFECTS (node) = 1;
14567 CAN_COMPLETE_NORMALLY (node)
14568 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14569 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14570 return node;
14571 }
14572
14573 /* 14.6 Labeled Statements */
14574
14575 /* Action taken when a lableled statement is parsed. a new
14576 LABELED_BLOCK_EXPR is created. No statement is attached to the
14577 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14578
14579 static tree
14580 build_labeled_block (location, label)
14581 int location;
14582 tree label;
14583 {
14584 tree label_name ;
14585 tree label_decl, node;
14586 if (label == NULL_TREE || label == continue_identifier_node)
14587 label_name = label;
14588 else
14589 {
14590 label_name = merge_qualified_name (label_id, label);
14591 /* Issue an error if we try to reuse a label that was previously
14592 declared */
14593 if (IDENTIFIER_LOCAL_VALUE (label_name))
14594 {
14595 EXPR_WFL_LINECOL (wfl_operator) = location;
14596 parse_error_context (wfl_operator,
14597 "Declaration of `%s' shadows a previous label declaration",
14598 IDENTIFIER_POINTER (label));
14599 EXPR_WFL_LINECOL (wfl_operator) =
14600 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14601 parse_error_context (wfl_operator,
14602 "This is the location of the previous declaration of label `%s'",
14603 IDENTIFIER_POINTER (label));
14604 java_error_count--;
14605 }
14606 }
14607
14608 label_decl = create_label_decl (label_name);
14609 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14610 EXPR_WFL_LINECOL (node) = location;
14611 TREE_SIDE_EFFECTS (node) = 1;
14612 return node;
14613 }
14614
14615 /* A labeled statement LBE is attached a statement. */
14616
14617 static tree
14618 finish_labeled_statement (lbe, statement)
14619 tree lbe; /* Labeled block expr */
14620 tree statement;
14621 {
14622 /* In anyways, tie the loop to its statement */
14623 LABELED_BLOCK_BODY (lbe) = statement;
14624 pop_labeled_block ();
14625 POP_LABELED_BLOCK ();
14626 return lbe;
14627 }
14628
14629 /* 14.10, 14.11, 14.12 Loop Statements */
14630
14631 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14632 list. */
14633
14634 static tree
14635 build_new_loop (loop_body)
14636 tree loop_body;
14637 {
14638 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14639 TREE_SIDE_EFFECTS (loop) = 1;
14640 PUSH_LOOP (loop);
14641 return loop;
14642 }
14643
14644 /* Create a loop body according to the following structure:
14645 COMPOUND_EXPR
14646 COMPOUND_EXPR (loop main body)
14647 EXIT_EXPR (this order is for while/for loops.
14648 LABELED_BLOCK_EXPR the order is reversed for do loops)
14649 LABEL_DECL (a continue occuring here branches at the
14650 BODY end of this labeled block)
14651 INCREMENT (if any)
14652
14653 REVERSED, if non zero, tells that the loop condition expr comes
14654 after the body, like in the do-while loop.
14655
14656 To obtain a loop, the loop body structure described above is
14657 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14658
14659 LABELED_BLOCK_EXPR
14660 LABEL_DECL (use this label to exit the loop)
14661 LOOP_EXPR
14662 <structure described above> */
14663
14664 static tree
14665 build_loop_body (location, condition, reversed)
14666 int location;
14667 tree condition;
14668 int reversed;
14669 {
14670 tree first, second, body;
14671
14672 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14673 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14674 condition = build_debugable_stmt (location, condition);
14675 TREE_SIDE_EFFECTS (condition) = 1;
14676
14677 body = build_labeled_block (0, continue_identifier_node);
14678 first = (reversed ? body : condition);
14679 second = (reversed ? condition : body);
14680 return
14681 build (COMPOUND_EXPR, NULL_TREE,
14682 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14683 }
14684
14685 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14686 their order) on the current loop. Unlink the current loop from the
14687 loop list. */
14688
14689 static tree
14690 finish_loop_body (location, condition, body, reversed)
14691 int location;
14692 tree condition, body;
14693 int reversed;
14694 {
14695 tree to_return = ctxp->current_loop;
14696 tree loop_body = LOOP_EXPR_BODY (to_return);
14697 if (condition)
14698 {
14699 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14700 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14701 The real EXIT_EXPR is one operand further. */
14702 EXPR_WFL_LINECOL (cnode) = location;
14703 /* This one is for accurate error reports */
14704 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14705 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14706 }
14707 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14708 POP_LOOP ();
14709 return to_return;
14710 }
14711
14712 /* Tailored version of finish_loop_body for FOR loops, when FOR
14713 loops feature the condition part */
14714
14715 static tree
14716 finish_for_loop (location, condition, update, body)
14717 int location;
14718 tree condition, update, body;
14719 {
14720 /* Put the condition and the loop body in place */
14721 tree loop = finish_loop_body (location, condition, body, 0);
14722 /* LOOP is the current loop which has been now popped of the loop
14723 stack. Install the update block */
14724 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14725 return loop;
14726 }
14727
14728 /* Try to find the loop a block might be related to. This comprises
14729 the case where the LOOP_EXPR is found as the second operand of a
14730 COMPOUND_EXPR, because the loop happens to have an initialization
14731 part, then expressed as the first operand of the COMPOUND_EXPR. If
14732 the search finds something, 1 is returned. Otherwise, 0 is
14733 returned. The search is assumed to start from a
14734 LABELED_BLOCK_EXPR's block. */
14735
14736 static tree
14737 search_loop (statement)
14738 tree statement;
14739 {
14740 if (TREE_CODE (statement) == LOOP_EXPR)
14741 return statement;
14742
14743 if (TREE_CODE (statement) == BLOCK)
14744 statement = BLOCK_SUBBLOCKS (statement);
14745 else
14746 return NULL_TREE;
14747
14748 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14749 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14750 statement = TREE_OPERAND (statement, 1);
14751
14752 return (TREE_CODE (statement) == LOOP_EXPR
14753 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14754 }
14755
14756 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14757 returned otherwise. */
14758
14759 static int
14760 labeled_block_contains_loop_p (block, loop)
14761 tree block, loop;
14762 {
14763 if (!block)
14764 return 0;
14765
14766 if (LABELED_BLOCK_BODY (block) == loop)
14767 return 1;
14768
14769 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14770 return 1;
14771
14772 return 0;
14773 }
14774
14775 /* If the loop isn't surrounded by a labeled statement, create one and
14776 insert LOOP as its body. */
14777
14778 static tree
14779 patch_loop_statement (loop)
14780 tree loop;
14781 {
14782 tree loop_label;
14783
14784 TREE_TYPE (loop) = void_type_node;
14785 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14786 return loop;
14787
14788 loop_label = build_labeled_block (0, NULL_TREE);
14789 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14790 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14791 LABELED_BLOCK_BODY (loop_label) = loop;
14792 PUSH_LABELED_BLOCK (loop_label);
14793 return loop_label;
14794 }
14795
14796 /* 14.13, 14.14: break and continue Statements */
14797
14798 /* Build a break or a continue statement. a null NAME indicates an
14799 unlabeled break/continue statement. */
14800
14801 static tree
14802 build_bc_statement (location, is_break, name)
14803 int location, is_break;
14804 tree name;
14805 {
14806 tree break_continue, label_block_expr = NULL_TREE;
14807
14808 if (name)
14809 {
14810 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14811 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14812 /* Null means that we don't have a target for this named
14813 break/continue. In this case, we make the target to be the
14814 label name, so that the error can be reported accuratly in
14815 patch_bc_statement. */
14816 label_block_expr = EXPR_WFL_NODE (name);
14817 }
14818 /* Unlabeled break/continue will be handled during the
14819 break/continue patch operation */
14820 break_continue
14821 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14822
14823 IS_BREAK_STMT_P (break_continue) = is_break;
14824 TREE_SIDE_EFFECTS (break_continue) = 1;
14825 EXPR_WFL_LINECOL (break_continue) = location;
14826 break_continue = build_debugable_stmt (location, break_continue);
14827 return break_continue;
14828 }
14829
14830 /* Verification of a break/continue statement. */
14831
14832 static tree
14833 patch_bc_statement (node)
14834 tree node;
14835 {
14836 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14837 tree labeled_block = ctxp->current_labeled_block;
14838 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14839
14840 /* Having an identifier here means that the target is unknown. */
14841 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14842 {
14843 parse_error_context (wfl_operator, "No label definition found for `%s'",
14844 IDENTIFIER_POINTER (bc_label));
14845 return error_mark_node;
14846 }
14847 if (! IS_BREAK_STMT_P (node))
14848 {
14849 /* It's a continue statement. */
14850 for (;; labeled_block = TREE_CHAIN (labeled_block))
14851 {
14852 if (labeled_block == NULL_TREE)
14853 {
14854 if (bc_label == NULL_TREE)
14855 parse_error_context (wfl_operator,
14856 "`continue' must be in loop");
14857 else
14858 parse_error_context
14859 (wfl_operator, "continue label `%s' does not name a loop",
14860 IDENTIFIER_POINTER (bc_label));
14861 return error_mark_node;
14862 }
14863 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14864 == continue_identifier_node)
14865 && (bc_label == NULL_TREE
14866 || TREE_CHAIN (labeled_block) == bc_label))
14867 {
14868 bc_label = labeled_block;
14869 break;
14870 }
14871 }
14872 }
14873 else if (!bc_label)
14874 {
14875 for (;; labeled_block = TREE_CHAIN (labeled_block))
14876 {
14877 if (labeled_block == NULL_TREE)
14878 {
14879 parse_error_context (wfl_operator,
14880 "`break' must be in loop or switch");
14881 return error_mark_node;
14882 }
14883 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14884 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14885 || search_loop (target_stmt))
14886 {
14887 bc_label = labeled_block;
14888 break;
14889 }
14890 }
14891 }
14892
14893 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14894 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14895
14896 /* Our break/continue don't return values. */
14897 TREE_TYPE (node) = void_type_node;
14898 /* Encapsulate the break within a compound statement so that it's
14899 expanded all the times by expand_expr (and not clobbered
14900 sometimes, like after a if statement) */
14901 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14902 TREE_SIDE_EFFECTS (node) = 1;
14903 return node;
14904 }
14905
14906 /* Process the exit expression belonging to a loop. Its type must be
14907 boolean. */
14908
14909 static tree
14910 patch_exit_expr (node)
14911 tree node;
14912 {
14913 tree expression = TREE_OPERAND (node, 0);
14914 TREE_TYPE (node) = error_mark_node;
14915 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14916
14917 /* The type of expression must be boolean */
14918 if (TREE_TYPE (expression) != boolean_type_node)
14919 {
14920 parse_error_context
14921 (wfl_operator,
14922 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14923 lang_printable_name (TREE_TYPE (expression), 0));
14924 return error_mark_node;
14925 }
14926 /* Now we know things are allright, invert the condition, fold and
14927 return */
14928 TREE_OPERAND (node, 0) =
14929 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14930
14931 if (! integer_zerop (TREE_OPERAND (node, 0))
14932 && ctxp->current_loop != NULL_TREE
14933 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14934 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14935 if (! integer_onep (TREE_OPERAND (node, 0)))
14936 CAN_COMPLETE_NORMALLY (node) = 1;
14937
14938
14939 TREE_TYPE (node) = void_type_node;
14940 return node;
14941 }
14942
14943 /* 14.9 Switch statement */
14944
14945 static tree
14946 patch_switch_statement (node)
14947 tree node;
14948 {
14949 tree se = TREE_OPERAND (node, 0), se_type;
14950
14951 /* Complete the switch expression */
14952 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14953 se_type = TREE_TYPE (se);
14954 /* The type of the switch expression must be char, byte, short or
14955 int */
14956 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14957 {
14958 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14959 parse_error_context (wfl_operator,
14960 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14961 lang_printable_name (se_type, 0));
14962 /* This is what java_complete_tree will check */
14963 TREE_OPERAND (node, 0) = error_mark_node;
14964 return error_mark_node;
14965 }
14966
14967 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14968
14969 /* Ready to return */
14970 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14971 {
14972 TREE_TYPE (node) = error_mark_node;
14973 return error_mark_node;
14974 }
14975 TREE_TYPE (node) = void_type_node;
14976 TREE_SIDE_EFFECTS (node) = 1;
14977 CAN_COMPLETE_NORMALLY (node)
14978 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14979 || ! SWITCH_HAS_DEFAULT (node);
14980 return node;
14981 }
14982
14983 /* 14.18 The try/catch statements */
14984
14985 static tree
14986 build_try_statement (location, try_block, catches)
14987 int location;
14988 tree try_block, catches;
14989 {
14990 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14991 EXPR_WFL_LINECOL (node) = location;
14992 return node;
14993 }
14994
14995 static tree
14996 build_try_finally_statement (location, try_block, finally)
14997 int location;
14998 tree try_block, finally;
14999 {
15000 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15001 EXPR_WFL_LINECOL (node) = location;
15002 return node;
15003 }
15004
15005 static tree
15006 patch_try_statement (node)
15007 tree node;
15008 {
15009 int error_found = 0;
15010 tree try = TREE_OPERAND (node, 0);
15011 /* Exception handlers are considered in left to right order */
15012 tree catch = nreverse (TREE_OPERAND (node, 1));
15013 tree current, caught_type_list = NULL_TREE;
15014
15015 /* Check catch clauses, if any. Every time we find an error, we try
15016 to process the next catch clause. We process the catch clause before
15017 the try block so that when processing the try block we can check thrown
15018 exceptions againts the caught type list. */
15019 for (current = catch; current; current = TREE_CHAIN (current))
15020 {
15021 tree carg_decl, carg_type;
15022 tree sub_current, catch_block, catch_clause;
15023 int unreachable;
15024
15025 /* At this point, the structure of the catch clause is
15026 CATCH_EXPR (catch node)
15027 BLOCK (with the decl of the parameter)
15028 COMPOUND_EXPR
15029 MODIFY_EXPR (assignment of the catch parameter)
15030 BLOCK (catch clause block)
15031 */
15032 catch_clause = TREE_OPERAND (current, 0);
15033 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15034 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15035
15036 /* Catch clauses can't have more than one parameter declared,
15037 but it's already enforced by the grammar. Make sure that the
15038 only parameter of the clause statement in of class Throwable
15039 or a subclass of Throwable, but that was done earlier. The
15040 catch clause parameter type has also been resolved. */
15041
15042 /* Just make sure that the catch clause parameter type inherits
15043 from java.lang.Throwable */
15044 if (!inherits_from_p (carg_type, throwable_type_node))
15045 {
15046 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15047 parse_error_context (wfl_operator,
15048 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15049 lang_printable_name (carg_type, 0));
15050 error_found = 1;
15051 continue;
15052 }
15053
15054 /* Partial check for unreachable catch statement: The catch
15055 clause is reachable iff is no earlier catch block A in
15056 the try statement such that the type of the catch
15057 clause's parameter is the same as or a subclass of the
15058 type of A's parameter */
15059 unreachable = 0;
15060 for (sub_current = catch;
15061 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15062 {
15063 tree sub_catch_clause, decl;
15064 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15065 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15066
15067 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15068 {
15069 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15070 parse_error_context
15071 (wfl_operator,
15072 "`catch' not reached because of the catch clause at line %d",
15073 EXPR_WFL_LINENO (sub_current));
15074 unreachable = error_found = 1;
15075 break;
15076 }
15077 }
15078 /* Complete the catch clause block */
15079 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15080 if (catch_block == error_mark_node)
15081 {
15082 error_found = 1;
15083 continue;
15084 }
15085 if (CAN_COMPLETE_NORMALLY (catch_block))
15086 CAN_COMPLETE_NORMALLY (node) = 1;
15087 TREE_OPERAND (current, 0) = catch_block;
15088
15089 if (unreachable)
15090 continue;
15091
15092 /* Things to do here: the exception must be thrown */
15093
15094 /* Link this type to the caught type list */
15095 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15096 }
15097
15098 PUSH_EXCEPTIONS (caught_type_list);
15099 if ((try = java_complete_tree (try)) == error_mark_node)
15100 error_found = 1;
15101 if (CAN_COMPLETE_NORMALLY (try))
15102 CAN_COMPLETE_NORMALLY (node) = 1;
15103 POP_EXCEPTIONS ();
15104
15105 /* Verification ends here */
15106 if (error_found)
15107 return error_mark_node;
15108
15109 TREE_OPERAND (node, 0) = try;
15110 TREE_OPERAND (node, 1) = catch;
15111 TREE_TYPE (node) = void_type_node;
15112 return node;
15113 }
15114
15115 /* 14.17 The synchronized Statement */
15116
15117 static tree
15118 patch_synchronized_statement (node, wfl_op1)
15119 tree node, wfl_op1;
15120 {
15121 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15122 tree block = TREE_OPERAND (node, 1);
15123
15124 tree tmp, enter, exit, expr_decl, assignment;
15125
15126 if (expr == error_mark_node)
15127 {
15128 block = java_complete_tree (block);
15129 return expr;
15130 }
15131
15132 /* We might be trying to synchronize on a STRING_CST */
15133 if ((tmp = patch_string (expr)))
15134 expr = tmp;
15135
15136 /* The TYPE of expr must be a reference type */
15137 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15138 {
15139 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15140 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15141 lang_printable_name (TREE_TYPE (expr), 0));
15142 return error_mark_node;
15143 }
15144
15145 if (flag_emit_xref)
15146 {
15147 TREE_OPERAND (node, 0) = expr;
15148 TREE_OPERAND (node, 1) = java_complete_tree (block);
15149 CAN_COMPLETE_NORMALLY (node) = 1;
15150 return node;
15151 }
15152
15153 /* Generate a try-finally for the synchronized statement, except
15154 that the handler that catches all throw exception calls
15155 _Jv_MonitorExit and then rethrow the exception.
15156 The synchronized statement is then implemented as:
15157 TRY
15158 {
15159 _Jv_MonitorEnter (expression)
15160 synchronized_block
15161 _Jv_MonitorExit (expression)
15162 }
15163 CATCH_ALL
15164 {
15165 e = _Jv_exception_info ();
15166 _Jv_MonitorExit (expression)
15167 Throw (e);
15168 } */
15169
15170 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15171 BUILD_MONITOR_ENTER (enter, expr_decl);
15172 BUILD_MONITOR_EXIT (exit, expr_decl);
15173 CAN_COMPLETE_NORMALLY (enter) = 1;
15174 CAN_COMPLETE_NORMALLY (exit) = 1;
15175 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15176 TREE_SIDE_EFFECTS (assignment) = 1;
15177 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15178 build (COMPOUND_EXPR, NULL_TREE,
15179 build (WITH_CLEANUP_EXPR, NULL_TREE,
15180 build (COMPOUND_EXPR, NULL_TREE,
15181 assignment, enter),
15182 NULL_TREE, exit),
15183 block));
15184 node = build_expr_block (node, expr_decl);
15185
15186 return java_complete_tree (node);
15187 }
15188
15189 /* 14.16 The throw Statement */
15190
15191 static tree
15192 patch_throw_statement (node, wfl_op1)
15193 tree node, wfl_op1;
15194 {
15195 tree expr = TREE_OPERAND (node, 0);
15196 tree type = TREE_TYPE (expr);
15197 int unchecked_ok = 0, tryblock_throws_ok = 0;
15198
15199 /* Thrown expression must be assignable to java.lang.Throwable */
15200 if (!try_reference_assignconv (throwable_type_node, expr))
15201 {
15202 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15203 parse_error_context (wfl_operator,
15204 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15205 lang_printable_name (type, 0));
15206 /* If the thrown expression was a reference, we further the
15207 compile-time check. */
15208 if (!JREFERENCE_TYPE_P (type))
15209 return error_mark_node;
15210 }
15211
15212 /* At least one of the following must be true */
15213
15214 /* The type of the throw expression is a not checked exception,
15215 i.e. is a unchecked expression. */
15216 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15217
15218 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15219 /* An instance can't throw a checked excetion unless that exception
15220 is explicitely declared in the `throws' clause of each
15221 constructor. This doesn't apply to anonymous classes, since they
15222 don't have declared constructors. */
15223 if (!unchecked_ok
15224 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15225 {
15226 tree current;
15227 for (current = TYPE_METHODS (current_class); current;
15228 current = TREE_CHAIN (current))
15229 if (DECL_CONSTRUCTOR_P (current)
15230 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15231 {
15232 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)",
15233 lang_printable_name (TREE_TYPE (expr), 0));
15234 return error_mark_node;
15235 }
15236 }
15237
15238 /* Throw is contained in a try statement and at least one catch
15239 clause can receive the thrown expression or the current method is
15240 declared to throw such an exception. Or, the throw statement is
15241 contained in a method or constructor declaration and the type of
15242 the Expression is assignable to at least one type listed in the
15243 throws clause the declaration. */
15244 if (!unchecked_ok)
15245 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15246 if (!(unchecked_ok || tryblock_throws_ok))
15247 {
15248 /* If there is a surrounding try block that has no matching
15249 clatch clause, report it first. A surrounding try block exits
15250 only if there is something after the list of checked
15251 exception thrown by the current function (if any). */
15252 if (IN_TRY_BLOCK_P ())
15253 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15254 lang_printable_name (type, 0));
15255 /* If we have no surrounding try statement and the method doesn't have
15256 any throws, report it now. FIXME */
15257
15258 /* We report that the exception can't be throw from a try block
15259 in all circumstances but when the `throw' is inside a static
15260 block. */
15261 else if (!EXCEPTIONS_P (currently_caught_type_list)
15262 && !tryblock_throws_ok)
15263 {
15264 if (DECL_CLINIT_P (current_function_decl))
15265 parse_error_context (wfl_operator,
15266 "Checked exception `%s' can't be thrown in initializer",
15267 lang_printable_name (type, 0));
15268 else
15269 parse_error_context (wfl_operator,
15270 "Checked exception `%s' isn't thrown from a `try' block",
15271 lang_printable_name (type, 0));
15272 }
15273 /* Otherwise, the current method doesn't have the appropriate
15274 throws declaration */
15275 else
15276 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15277 lang_printable_name (type, 0));
15278 return error_mark_node;
15279 }
15280
15281 if (! flag_emit_class_files && ! flag_emit_xref)
15282 BUILD_THROW (node, expr);
15283
15284 /* If doing xrefs, keep the location where the `throw' was seen. */
15285 if (flag_emit_xref)
15286 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15287 return node;
15288 }
15289
15290 /* Check that exception said to be thrown by method DECL can be
15291 effectively caught from where DECL is invoked. */
15292
15293 static void
15294 check_thrown_exceptions (location, decl)
15295 int location;
15296 tree decl;
15297 {
15298 tree throws;
15299 /* For all the unchecked exceptions thrown by DECL */
15300 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15301 throws = TREE_CHAIN (throws))
15302 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15303 {
15304 #if 1
15305 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15306 if (DECL_NAME (decl) == get_identifier ("clone"))
15307 continue;
15308 #endif
15309 EXPR_WFL_LINECOL (wfl_operator) = location;
15310 if (DECL_FINIT_P (current_function_decl))
15311 parse_error_context
15312 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15313 lang_printable_name (TREE_VALUE (throws), 0));
15314 else
15315 {
15316 parse_error_context
15317 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15318 lang_printable_name (TREE_VALUE (throws), 0),
15319 (DECL_INIT_P (current_function_decl) ?
15320 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15321 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15322 }
15323 }
15324 }
15325
15326 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15327 try-catch blocks, OR is listed in the `throws' clause of the
15328 current method. */
15329
15330 static int
15331 check_thrown_exceptions_do (exception)
15332 tree exception;
15333 {
15334 tree list = currently_caught_type_list;
15335 resolve_and_layout (exception, NULL_TREE);
15336 /* First, all the nested try-catch-finally at that stage. The
15337 last element contains `throws' clause exceptions, if any. */
15338 if (IS_UNCHECKED_EXCEPTION_P (exception))
15339 return 1;
15340 while (list)
15341 {
15342 tree caught;
15343 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15344 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15345 return 1;
15346 list = TREE_CHAIN (list);
15347 }
15348 return 0;
15349 }
15350
15351 static void
15352 purge_unchecked_exceptions (mdecl)
15353 tree mdecl;
15354 {
15355 tree throws = DECL_FUNCTION_THROWS (mdecl);
15356 tree new = NULL_TREE;
15357
15358 while (throws)
15359 {
15360 tree next = TREE_CHAIN (throws);
15361 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15362 {
15363 TREE_CHAIN (throws) = new;
15364 new = throws;
15365 }
15366 throws = next;
15367 }
15368 /* List is inverted here, but it doesn't matter */
15369 DECL_FUNCTION_THROWS (mdecl) = new;
15370 }
15371
15372 /* 15.24 Conditional Operator ?: */
15373
15374 static tree
15375 patch_conditional_expr (node, wfl_cond, wfl_op1)
15376 tree node, wfl_cond, wfl_op1;
15377 {
15378 tree cond = TREE_OPERAND (node, 0);
15379 tree op1 = TREE_OPERAND (node, 1);
15380 tree op2 = TREE_OPERAND (node, 2);
15381 tree resulting_type = NULL_TREE;
15382 tree t1, t2, patched;
15383 int error_found = 0;
15384
15385 /* Operands of ?: might be StringBuffers crafted as a result of a
15386 string concatenation. Obtain a descent operand here. */
15387 if ((patched = patch_string (op1)))
15388 TREE_OPERAND (node, 1) = op1 = patched;
15389 if ((patched = patch_string (op2)))
15390 TREE_OPERAND (node, 2) = op2 = patched;
15391
15392 t1 = TREE_TYPE (op1);
15393 t2 = TREE_TYPE (op2);
15394
15395 /* The first expression must be a boolean */
15396 if (TREE_TYPE (cond) != boolean_type_node)
15397 {
15398 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15399 parse_error_context (wfl_operator,
15400 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15401 lang_printable_name (TREE_TYPE (cond), 0));
15402 error_found = 1;
15403 }
15404
15405 /* Second and third can be numeric, boolean (i.e. primitive),
15406 references or null. Anything else results in an error */
15407 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15408 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15409 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15410 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15411 error_found = 1;
15412
15413 /* Determine the type of the conditional expression. Same types are
15414 easy to deal with */
15415 else if (t1 == t2)
15416 resulting_type = t1;
15417
15418 /* There are different rules for numeric types */
15419 else if (JNUMERIC_TYPE_P (t1))
15420 {
15421 /* if byte/short found, the resulting type is short */
15422 if ((t1 == byte_type_node && t2 == short_type_node)
15423 || (t1 == short_type_node && t2 == byte_type_node))
15424 resulting_type = short_type_node;
15425
15426 /* If t1 is a constant int and t2 is of type byte, short or char
15427 and t1's value fits in t2, then the resulting type is t2 */
15428 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15429 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15430 resulting_type = t2;
15431
15432 /* If t2 is a constant int and t1 is of type byte, short or char
15433 and t2's value fits in t1, then the resulting type is t1 */
15434 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15435 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15436 resulting_type = t1;
15437
15438 /* Otherwise, binary numeric promotion is applied and the
15439 resulting type is the promoted type of operand 1 and 2 */
15440 else
15441 resulting_type = binary_numeric_promotion (t1, t2,
15442 &TREE_OPERAND (node, 1),
15443 &TREE_OPERAND (node, 2));
15444 }
15445
15446 /* Cases of a reference and a null type */
15447 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15448 resulting_type = t1;
15449
15450 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15451 resulting_type = t2;
15452
15453 /* Last case: different reference types. If a type can be converted
15454 into the other one by assignment conversion, the latter
15455 determines the type of the expression */
15456 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15457 resulting_type = promote_type (t1);
15458
15459 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15460 resulting_type = promote_type (t2);
15461
15462 /* If we don't have any resulting type, we're in trouble */
15463 if (!resulting_type)
15464 {
15465 char *t = xstrdup (lang_printable_name (t1, 0));
15466 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15467 parse_error_context (wfl_operator,
15468 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15469 t, lang_printable_name (t2, 0));
15470 free (t);
15471 error_found = 1;
15472 }
15473
15474 if (error_found)
15475 {
15476 TREE_TYPE (node) = error_mark_node;
15477 return error_mark_node;
15478 }
15479
15480 TREE_TYPE (node) = resulting_type;
15481 TREE_SET_CODE (node, COND_EXPR);
15482 CAN_COMPLETE_NORMALLY (node) = 1;
15483 return node;
15484 }
15485
15486 /* Try to constant fold NODE.
15487 If NODE is not a constant expression, return NULL_EXPR.
15488 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15489
15490 static tree
15491 fold_constant_for_init (node, context)
15492 tree node;
15493 tree context;
15494 {
15495 tree op0, op1, val;
15496 enum tree_code code = TREE_CODE (node);
15497
15498 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
15499 return node;
15500
15501 switch (code)
15502 {
15503 case PLUS_EXPR:
15504 case MINUS_EXPR:
15505 case MULT_EXPR:
15506 case TRUNC_MOD_EXPR:
15507 case RDIV_EXPR:
15508 case LSHIFT_EXPR:
15509 case RSHIFT_EXPR:
15510 case URSHIFT_EXPR:
15511 case BIT_AND_EXPR:
15512 case BIT_XOR_EXPR:
15513 case BIT_IOR_EXPR:
15514 case TRUTH_ANDIF_EXPR:
15515 case TRUTH_ORIF_EXPR:
15516 case EQ_EXPR:
15517 case NE_EXPR:
15518 case GT_EXPR:
15519 case GE_EXPR:
15520 case LT_EXPR:
15521 case LE_EXPR:
15522 op0 = TREE_OPERAND (node, 0);
15523 op1 = TREE_OPERAND (node, 1);
15524 val = fold_constant_for_init (op0, context);
15525 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15526 return NULL_TREE;
15527 TREE_OPERAND (node, 0) = val;
15528 val = fold_constant_for_init (op1, context);
15529 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15530 return NULL_TREE;
15531 TREE_OPERAND (node, 1) = val;
15532 return patch_binop (node, op0, op1);
15533
15534 case UNARY_PLUS_EXPR:
15535 case NEGATE_EXPR:
15536 case TRUTH_NOT_EXPR:
15537 case BIT_NOT_EXPR:
15538 case CONVERT_EXPR:
15539 op0 = TREE_OPERAND (node, 0);
15540 val = fold_constant_for_init (op0, context);
15541 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15542 return NULL_TREE;
15543 TREE_OPERAND (node, 0) = val;
15544 return patch_unaryop (node, op0);
15545 break;
15546
15547 case COND_EXPR:
15548 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15549 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15550 return NULL_TREE;
15551 TREE_OPERAND (node, 0) = val;
15552 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15553 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15554 return NULL_TREE;
15555 TREE_OPERAND (node, 1) = val;
15556 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15557 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15558 return NULL_TREE;
15559 TREE_OPERAND (node, 2) = val;
15560 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15561 : TREE_OPERAND (node, 2);
15562
15563 case VAR_DECL:
15564 case FIELD_DECL:
15565 if (! FIELD_FINAL (node)
15566 || DECL_INITIAL (node) == NULL_TREE)
15567 return NULL_TREE;
15568 val = DECL_INITIAL (node);
15569 /* Guard against infinite recursion. */
15570 DECL_INITIAL (node) = NULL_TREE;
15571 val = fold_constant_for_init (val, node);
15572 DECL_INITIAL (node) = val;
15573 if (!val && CLASS_FINAL_VARIABLE_P (node))
15574 DECL_FIELD_FINAL_IUD (node) = 0;
15575 return val;
15576
15577 case EXPR_WITH_FILE_LOCATION:
15578 /* Compare java_complete_tree and resolve_expression_name. */
15579 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15580 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15581 {
15582 tree name = EXPR_WFL_NODE (node);
15583 tree decl;
15584 if (PRIMARY_P (node))
15585 return NULL_TREE;
15586 else if (! QUALIFIED_P (name))
15587 {
15588 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15589 if (decl == NULL_TREE
15590 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15591 return NULL_TREE;
15592 return fold_constant_for_init (decl, decl);
15593 }
15594 else
15595 {
15596 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15597 qualify_ambiguous_name (node);
15598 if (resolve_field_access (node, &decl, NULL)
15599 && decl != NULL_TREE)
15600 return fold_constant_for_init (decl, decl);
15601 return NULL_TREE;
15602 }
15603 }
15604 else
15605 {
15606 op0 = TREE_OPERAND (node, 0);
15607 val = fold_constant_for_init (op0, context);
15608 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15609 return NULL_TREE;
15610 TREE_OPERAND (node, 0) = val;
15611 return val;
15612 }
15613
15614 #ifdef USE_COMPONENT_REF
15615 case IDENTIFIER:
15616 case COMPONENT_REF:
15617 ?;
15618 #endif
15619
15620 default:
15621 return NULL_TREE;
15622 }
15623 }
15624
15625 #ifdef USE_COMPONENT_REF
15626 /* Context is 'T' for TypeName, 'P' for PackageName,
15627 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15628
15629 tree
15630 resolve_simple_name (name, context)
15631 tree name;
15632 int context;
15633 {
15634 }
15635
15636 tree
15637 resolve_qualified_name (name, context)
15638 tree name;
15639 int context;
15640 {
15641 }
15642 #endif
15643
15644 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15645
15646 static void
15647 mark_parser_ctxt (p)
15648 void *p;
15649 {
15650 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15651 int i;
15652
15653 if (!pc)
15654 return;
15655
15656 #ifndef JC1_LITE
15657 for (i = 0; i < 11; ++i)
15658 ggc_mark_tree (pc->modifier_ctx[i]);
15659 ggc_mark_tree (pc->class_type);
15660 ggc_mark_tree (pc->function_decl);
15661 ggc_mark_tree (pc->package);
15662 ggc_mark_tree (pc->class_list);
15663 ggc_mark_tree (pc->current_parsed_class);
15664 ggc_mark_tree (pc->current_parsed_class_un);
15665 ggc_mark_tree (pc->non_static_initialized);
15666 ggc_mark_tree (pc->static_initialized);
15667 ggc_mark_tree (pc->instance_initializers);
15668 ggc_mark_tree (pc->import_list);
15669 ggc_mark_tree (pc->import_demand_list);
15670 ggc_mark_tree (pc->current_loop);
15671 ggc_mark_tree (pc->current_labeled_block);
15672 #endif /* JC1_LITE */
15673
15674 if (pc->next)
15675 mark_parser_ctxt (&pc->next);
15676 }
15677
15678 void
15679 init_src_parse ()
15680 {
15681 /* Register roots with the garbage collector. */
15682 ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));
15683 }
This page took 0.737822 seconds and 6 git commands to generate.