OSDN Git Service

PR java/16675:
[pf3gnuchains/gcc-fork.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, 2002, 2003, 2004
4    Free Software Foundation, Inc.
5    Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
23
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
27
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
30 language).
31
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
35
36 The following modifications were brought to the original grammar:
37
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
40
41 Note: All the extra rules described above should go away when the
42       empty_statement rule will work.
43
44 statement_nsi: 'nsi' should be read no_short_if.
45
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions.  */
48
49 %{
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include <dirent.h>
55 #include "tree.h"
56 #include "rtl.h"
57 #include "real.h"
58 #include "obstack.h"
59 #include "toplev.h"
60 #include "pretty-print.h"
61 #include "diagnostic.h"
62 #include "flags.h"
63 #include "java-tree.h"
64 #include "jcf.h"
65 #include "lex.h"
66 #include "parse.h"
67 #include "zipfile.h"
68 #include "convert.h"
69 #include "buffer.h"
70 #include "xref.h"
71 #include "function.h"
72 #include "except.h"
73 #include "ggc.h"
74 #include "debug.h"
75 #include "tree-inline.h"
76 #include "cgraph.h"
77 #include "target.h"
78
79 /* Local function prototypes */
80 static char *java_accstring_lookup (int);
81 static const char *accessibility_string (int);
82 static void  classitf_redefinition_error (const char *,tree, tree, tree);
83 static void  variable_redefinition_error (tree, tree, tree, int);
84 static tree  create_class (int, tree, tree, tree);
85 static tree  create_interface (int, tree, tree);
86 static void  end_class_declaration (int);
87 static tree  find_field (tree, tree);
88 static tree lookup_field_wrapper (tree, tree);
89 static int   duplicate_declaration_error_p (tree, tree, tree);
90 static void  register_fields (int, tree, tree);
91 static tree parser_qualified_classname (tree);
92 static int  parser_check_super (tree, tree, tree);
93 static int  parser_check_super_interface (tree, tree, tree);
94 static void check_modifiers_consistency (int);
95 static tree lookup_cl (tree);
96 static tree lookup_java_method2 (tree, tree, int);
97 static tree method_header (int, tree, tree, tree);
98 static void fix_method_argument_names (tree ,tree);
99 static tree method_declarator (tree, tree);
100 static void parse_warning_context (tree cl, const char *msgid, ...);
101 #ifdef USE_MAPPED_LOCATION
102 static void issue_warning_error_from_context
103   (source_location, const char *msgid, va_list *);
104 #else
105 static void issue_warning_error_from_context
106   (tree, const char *msgid, va_list *);
107 #endif
108 static void parse_ctor_invocation_error (void);
109 static tree parse_jdk1_1_error (const char *);
110 static void complete_class_report_errors (jdep *);
111 static int process_imports (void);
112 static void read_import_dir (tree);
113 static int find_in_imports_on_demand (tree, tree);
114 static void find_in_imports (tree, tree);
115 static void check_inner_class_access (tree, tree, tree);
116 static int check_pkg_class_access (tree, tree, bool, tree);
117 static void register_package (tree);
118 static tree resolve_package (tree, tree *, tree *);
119 static tree resolve_class (tree, tree, tree, tree);
120 static void declare_local_variables (int, tree, tree);
121 static void dump_java_tree (enum tree_dump_index, tree);
122 static void source_start_java_method (tree);
123 static void source_end_java_method (void);
124 static tree find_name_in_single_imports (tree);
125 static void check_abstract_method_header (tree);
126 static tree lookup_java_interface_method2 (tree, tree);
127 static tree resolve_expression_name (tree, tree *);
128 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
129 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
130 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
131 static tree resolve_and_layout (tree, tree);
132 static tree qualify_and_find (tree, tree, tree);
133 static tree resolve_no_layout (tree, tree);
134 static int invocation_mode (tree, int);
135 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
136 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
137 static tree find_most_specific_methods_list (tree);
138 static int argument_types_convertible (tree, tree);
139 static tree patch_invoke (tree, tree, tree);
140 static int maybe_use_access_method (int, tree *, tree *);
141 static tree lookup_method_invoke (int, tree, tree, tree, tree);
142 static tree register_incomplete_type (int, tree, tree, tree);
143 static tree check_inner_circular_reference (tree, tree);
144 static tree check_circular_reference (tree);
145 static tree obtain_incomplete_type (tree);
146 static tree java_complete_lhs (tree);
147 static tree java_complete_tree (tree);
148 static tree maybe_generate_pre_expand_clinit (tree);
149 static int analyze_clinit_body (tree, tree);
150 static int maybe_yank_clinit (tree);
151 static void start_complete_expand_method (tree);
152 static void java_complete_expand_method (tree);
153 static void java_expand_method_bodies (tree);
154 static int  unresolved_type_p (tree, tree *);
155 static void create_jdep_list (struct parser_ctxt *);
156 static tree build_expr_block (tree, tree);
157 static tree enter_block (void);
158 static tree exit_block (void);
159 static tree lookup_name_in_blocks (tree);
160 static void maybe_absorb_scoping_blocks (void);
161 static tree build_method_invocation (tree, tree);
162 static tree build_new_invocation (tree, tree);
163 static tree build_assignment (int, int, tree, tree);
164 static tree build_binop (enum tree_code, int, tree, tree);
165 static tree patch_assignment (tree, tree);
166 static tree patch_binop (tree, tree, tree);
167 static tree build_unaryop (int, int, tree);
168 static tree build_incdec (int, int, tree, int);
169 static tree patch_unaryop (tree, tree);
170 static tree build_cast (int, tree, tree);
171 static tree build_null_of_type (tree);
172 static tree patch_cast (tree, tree);
173 static int valid_ref_assignconv_cast_p (tree, tree, int);
174 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
175 static int valid_cast_to_p (tree, tree);
176 static int valid_method_invocation_conversion_p (tree, tree);
177 static tree try_builtin_assignconv (tree, tree, tree);
178 static tree try_reference_assignconv (tree, tree);
179 static tree build_unresolved_array_type (tree);
180 static int build_type_name_from_array_name (tree, tree *);
181 static tree build_array_from_name (tree, tree, tree, tree *);
182 static tree build_array_ref (int, tree, tree);
183 static tree patch_array_ref (tree);
184 #ifdef USE_MAPPED_LOCATION
185 static tree make_qualified_name (tree, tree, source_location);
186 #else
187 static tree make_qualified_name (tree, tree, int);
188 #endif
189 static tree merge_qualified_name (tree, tree);
190 static tree make_qualified_primary (tree, tree, int);
191 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
192 static void qualify_ambiguous_name (tree);
193 static tree resolve_field_access (tree, tree *, tree *);
194 static tree build_newarray_node (tree, tree, int);
195 static tree patch_newarray (tree);
196 static tree resolve_type_during_patch (tree);
197 static tree build_this (int);
198 static tree build_wfl_wrap (tree, int);
199 static tree build_return (int, tree);
200 static tree patch_return (tree);
201 static tree maybe_access_field (tree, tree, tree);
202 static int complete_function_arguments (tree);
203 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
204 static int not_accessible_p (tree, tree, tree, int);
205 static void check_deprecation (tree, tree);
206 static int class_in_current_package (tree);
207 static tree build_if_else_statement (int, tree, tree, tree);
208 static tree patch_if_else_statement (tree);
209 static tree add_stmt_to_block (tree, tree, tree);
210 static tree patch_exit_expr (tree);
211 static tree build_labeled_block (int, tree);
212 static tree finish_labeled_statement (tree, tree);
213 static tree build_bc_statement (int, int, tree);
214 static tree patch_bc_statement (tree);
215 static tree patch_loop_statement (tree);
216 static tree build_new_loop (tree);
217 static tree build_loop_body (int, tree, int);
218 static tree finish_loop_body (int, tree, tree, int);
219 static tree build_debugable_stmt (int, tree);
220 static tree finish_for_loop (int, tree, tree, tree);
221 static tree patch_switch_statement (tree);
222 static tree string_constant_concatenation (tree, tree);
223 static tree build_string_concatenation (tree, tree);
224 static tree patch_string_cst (tree);
225 static tree patch_string (tree);
226 static tree encapsulate_with_try_catch (int, tree, tree, tree);
227 #ifdef USE_MAPPED_LOCATION
228 static tree build_assertion (source_location, tree, tree);
229 #else
230 static tree build_assertion (int, tree, tree);
231 #endif
232 static tree build_try_statement (int, tree, tree);
233 static tree build_try_finally_statement (int, tree, tree);
234 static tree patch_try_statement (tree);
235 static tree patch_synchronized_statement (tree, tree);
236 static tree patch_throw_statement (tree, tree);
237 #ifdef USE_MAPPED_LOCATION
238 static void check_thrown_exceptions (source_location, tree, tree);
239 #else
240 static void check_thrown_exceptions (int, tree, tree);
241 #endif
242 static int check_thrown_exceptions_do (tree);
243 static void purge_unchecked_exceptions (tree);
244 static bool ctors_unchecked_throws_clause_p (tree);
245 static void check_concrete_throws_clauses (tree, tree, tree, tree);
246 static void check_throws_clauses (tree, tree, tree);
247 static void finish_method_declaration (tree);
248 static tree build_super_invocation (tree);
249 static int verify_constructor_circularity (tree, tree);
250 static char *constructor_circularity_msg (tree, tree);
251 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
252 static const char *get_printable_method_name (tree);
253 static tree patch_conditional_expr (tree, tree, tree);
254 static tree generate_finit (tree);
255 static tree generate_instinit (tree);
256 static tree build_instinit_invocation (tree);
257 static void fix_constructors (tree);
258 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
259 static tree craft_constructor (tree, tree);
260 static int verify_constructor_super (tree);
261 static tree create_artificial_method (tree, int, tree, tree, tree);
262 static void start_artificial_method_body (tree);
263 static void end_artificial_method_body (tree);
264 static int check_method_redefinition (tree, tree);
265 static int check_method_types_complete (tree);
266 static bool hack_is_accessible_p (tree, tree);
267 static void java_check_regular_methods (tree);
268 static void check_interface_throws_clauses (tree, tree);
269 static void java_check_abstract_methods (tree);
270 static void unreachable_stmt_error (tree);
271 static int not_accessible_field_error (tree, tree);
272 static tree find_expr_with_wfl (tree);
273 static void missing_return_error (tree);
274 static tree build_new_array_init (int, tree);
275 static tree patch_new_array_init (tree, tree);
276 static tree maybe_build_array_element_wfl (tree);
277 static int array_constructor_check_entry (tree, tree);
278 static const char *purify_type_name (const char *);
279 static tree fold_constant_for_init (tree, tree);
280 static tree strip_out_static_field_access_decl (tree);
281 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
282 static void static_ref_err (tree, tree, tree);
283 static void parser_add_interface (tree, tree, tree);
284 static void add_superinterfaces (tree, tree);
285 static tree jdep_resolve_class (jdep *);
286 static int note_possible_classname (const char *, int);
287 static void java_complete_expand_classes (void);
288 static void java_complete_expand_class (tree);
289 static void java_complete_expand_methods (tree);
290 static tree cut_identifier_in_qualified (tree);
291 static tree java_stabilize_reference (tree);
292 static tree do_unary_numeric_promotion (tree);
293 static char * operator_string (tree);
294 static tree do_merge_string_cste (tree, const char *, int, int);
295 static tree merge_string_cste (tree, tree, int);
296 static tree java_refold (tree);
297 static int java_decl_equiv (tree, tree);
298 static int binop_compound_p (enum tree_code);
299 static tree search_loop (tree);
300 static int labeled_block_contains_loop_p (tree, tree);
301 static int check_abstract_method_definitions (int, tree, tree);
302 static void java_check_abstract_method_definitions (tree);
303 static void java_debug_context_do (int);
304 static void java_parser_context_push_initialized_field (void);
305 static void java_parser_context_pop_initialized_field (void);
306 static tree reorder_static_initialized (tree);
307 static void java_parser_context_suspend (void);
308 static void java_parser_context_resume (void);
309 static int pop_current_osb (struct parser_ctxt *);
310
311 /* JDK 1.1 work. FIXME */
312
313 static tree maybe_make_nested_class_name (tree);
314 static int make_nested_class_name (tree);
315 static void link_nested_class_to_enclosing (void);
316 static tree resolve_inner_class (htab_t, tree, tree *, tree *, tree);
317 static tree find_as_inner_class (tree, tree, tree);
318 static tree find_as_inner_class_do (tree, tree);
319 static int check_inner_class_redefinition (tree, tree);
320
321 static tree build_thisn_assign (void);
322 static tree build_current_thisn (tree);
323 static tree build_access_to_thisn (tree, tree, int);
324 static tree maybe_build_thisn_access_method (tree);
325
326 static tree build_outer_field_access (tree, tree);
327 static tree build_outer_field_access_methods (tree);
328 static tree build_outer_field_access_expr (int, tree, tree,
329                                                   tree, tree);
330 static tree build_outer_method_access_method (tree);
331 static tree build_new_access_id (void);
332 static tree build_outer_field_access_method (tree, tree, tree,
333                                                     tree, tree);
334
335 static int outer_field_access_p (tree, tree);
336 static int outer_field_expanded_access_p (tree, tree *,
337                                                  tree *, tree *);
338 static tree outer_field_access_fix (tree, tree, tree);
339 static tree build_incomplete_class_ref (int, tree);
340 static tree patch_incomplete_class_ref (tree);
341 static tree create_anonymous_class (tree);
342 static void patch_anonymous_class (tree, tree, tree);
343 static void add_inner_class_fields (tree, tree);
344
345 static tree build_dot_class_method (tree);
346 static tree build_dot_class_method_invocation (tree, tree);
347 static void create_new_parser_context (int);
348 static tree maybe_build_class_init_for_field (tree, tree);
349
350 static int emit_test_initialization (void **, void *);
351
352 static char *string_convert_int_cst (tree);
353
354 /* Number of error found so far. */
355 int java_error_count;
356 /* Number of warning found so far. */
357 int java_warning_count;
358 /* Tell when not to fold, when doing xrefs */
359 int do_not_fold;
360 /* Cyclic inheritance report, as it can be set by layout_class */
361 const char *cyclic_inheritance_report;
362
363 /* The current parser context */
364 struct parser_ctxt *ctxp;
365
366 /* List of things that were analyzed for which code will be generated */
367 struct parser_ctxt *ctxp_for_generation = NULL;
368
369 /* binop_lookup maps token to tree_code. It is used where binary
370    operations are involved and required by the parser. RDIV_EXPR
371    covers both integral/floating point division. The code is changed
372    once the type of both operator is worked out.  */
373
374 static const enum tree_code binop_lookup[19] =
375   {
376     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
377     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
378     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
379     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
380     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
381    };
382 #define BINOP_LOOKUP(VALUE)                                             \
383   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
384
385 /* This is the end index for binary operators that can also be used
386    in compound assignments. */
387 #define BINOP_COMPOUND_CANDIDATES 11
388
389 /* The "$L" identifier we use to create labels.  */
390 static GTY(()) tree label_id;
391
392 /* The "StringBuffer" identifier used for the String `+' operator. */
393 static GTY(()) tree wfl_string_buffer;
394
395 /* The "append" identifier used for String `+' operator.  */
396 static GTY(()) tree wfl_append;
397
398 /* The "toString" identifier used for String `+' operator. */
399 static GTY(()) tree wfl_to_string;
400
401 /* The "java.lang" import qualified name.  */
402 static GTY(()) tree java_lang_id;
403
404 /* The generated `inst$' identifier used for generated enclosing
405    instance/field access functions.  */
406 static GTY(()) tree inst_id;
407
408 /* Context and flag for static blocks */
409 static GTY(()) tree current_static_block;
410
411 /* The generated `write_parm_value$' identifier.  */
412 static GTY(()) tree wpv_id;
413
414 /* The list of all packages we've seen so far */
415 static GTY(()) tree package_list;
416
417 /* Hold THIS for the scope of the current method decl.  */
418 static GTY(()) tree current_this;
419
420 /* Hold a list of catch clauses list. The first element of this list is
421    the list of the catch clauses of the currently analyzed try block. */
422 static GTY(()) tree currently_caught_type_list;
423
424 /* This holds a linked list of all the case labels for the current
425    switch statement.  It is only used when checking to see if there
426    are duplicate labels.  FIXME: probably this should just be attached
427    to the switch itself; then it could be referenced via
428    `ctxp->current_loop'.  */
429 static GTY(()) tree case_label_list;
430
431 /* Anonymous class counter. Will be reset to 1 every time a non
432    anonymous class gets created. */
433 static int anonymous_class_counter = 1;
434
435 static GTY(()) tree src_parse_roots[1];
436
437 /* All classes seen from source code */
438 #define gclass_list src_parse_roots[0]
439
440 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
441    line and point it out.  */
442 /* Should point out the one that don't fit. ASCII/unicode, going
443    backward. FIXME */
444
445 #define check_modifiers(__message, __value, __mask) do {        \
446   if ((__value) & ~(__mask))                                    \
447     {                                                           \
448       size_t i, remainder = (__value) & ~(__mask);              \
449       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)     \
450         if ((1 << i) & remainder)                               \
451           parse_error_context (ctxp->modifier_ctx [i], (__message), \
452                                java_accstring_lookup (1 << i)); \
453     }                                                           \
454 } while (0)
455
456 %}
457
458 %union {
459   tree node;
460   int sub_token;
461   struct {
462     int token;
463 #ifdef USE_MAPPED_LOCATION
464     source_location location;
465 #else
466     int location;
467 #endif
468   } operator;
469   int value;
470 }
471
472 %{
473 #ifdef USE_MAPPED_LOCATION
474 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
475   SET_EXPR_LOCATION(EXPR, (TOKEN).location)
476 #else
477 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
478   (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
479 #endif
480
481 #include "lex.c"
482 %}
483
484 %pure_parser
485
486 /* Things defined here have to match the order of what's in the
487    binop_lookup table.  */
488
489 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
490 %token   LS_TK           SRS_TK          ZRS_TK
491 %token   AND_TK          XOR_TK          OR_TK
492 %token   BOOL_AND_TK BOOL_OR_TK
493 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
494
495 /* This maps to the same binop_lookup entry than the token above */
496
497 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
498 %token   REM_ASSIGN_TK
499 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
500 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
501
502
503 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
504
505 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
506 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
507 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
508 %token   PAD_TK          ABSTRACT_TK        STRICT_TK
509 %token   MODIFIER_TK
510
511 /* Keep those two in order, too */
512 %token   DECR_TK INCR_TK
513
514 /* From now one, things can be in any order */
515
516 %token   DEFAULT_TK      IF_TK              THROW_TK
517 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
518 %token   THROWS_TK       BREAK_TK           IMPORT_TK
519 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
520 %token   VOID_TK         CATCH_TK           INTERFACE_TK
521 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
522 %token   SUPER_TK        WHILE_TK           CLASS_TK
523 %token   SWITCH_TK       CONST_TK           TRY_TK
524 %token   FOR_TK          NEW_TK             CONTINUE_TK
525 %token   GOTO_TK         PACKAGE_TK         THIS_TK
526 %token   ASSERT_TK
527
528 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
529 %token   CHAR_TK         INTEGRAL_TK
530
531 %token   FLOAT_TK        DOUBLE_TK          FP_TK
532
533 %token   ID_TK
534
535 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
536
537 %token   ASSIGN_ANY_TK   ASSIGN_TK
538 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
539
540 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
541 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
542
543 %type    <value>        modifiers MODIFIER_TK final synchronized
544
545 %type    <node>         super ID_TK identifier
546 %type    <node>         name simple_name qualified_name
547 %type    <node>         type_declaration compilation_unit
548                         field_declaration method_declaration extends_interfaces
549                         interfaces interface_type_list
550                         import_declarations package_declaration
551                         type_declarations interface_body
552                         interface_member_declaration constant_declaration
553                         interface_member_declarations interface_type
554                         abstract_method_declaration
555 %type    <node>         class_body_declaration class_member_declaration
556                         static_initializer constructor_declaration block
557 %type    <node>         class_body_declarations constructor_header
558 %type    <node>         class_or_interface_type class_type class_type_list
559                         constructor_declarator explicit_constructor_invocation
560 %type    <node>         dim_expr dim_exprs this_or_super throws
561
562 %type    <node>         variable_declarator_id variable_declarator
563                         variable_declarators variable_initializer
564                         variable_initializers constructor_body
565                         array_initializer
566
567 %type    <node>         class_body block_end constructor_block_end
568 %type    <node>         statement statement_without_trailing_substatement
569                         labeled_statement if_then_statement label_decl
570                         if_then_else_statement while_statement for_statement
571                         statement_nsi labeled_statement_nsi do_statement
572                         if_then_else_statement_nsi while_statement_nsi
573                         for_statement_nsi statement_expression_list for_init
574                         for_update statement_expression expression_statement
575                         primary_no_new_array expression primary array_type
576                         array_creation_initialized array_creation_uninitialized
577                         class_instance_creation_expression field_access
578                         method_invocation array_access something_dot_new
579                         argument_list postfix_expression while_expression
580                         post_increment_expression post_decrement_expression
581                         unary_expression_not_plus_minus unary_expression
582                         pre_increment_expression pre_decrement_expression
583                         cast_expression
584                         multiplicative_expression additive_expression
585                         shift_expression relational_expression
586                         equality_expression and_expression
587                         exclusive_or_expression inclusive_or_expression
588                         conditional_and_expression conditional_or_expression
589                         conditional_expression assignment_expression
590                         left_hand_side assignment for_header for_begin
591                         constant_expression do_statement_begin empty_statement
592                         switch_statement synchronized_statement throw_statement
593                         try_statement assert_statement
594                         switch_expression switch_block
595                         catches catch_clause catch_clause_parameter finally
596                         anonymous_class_creation trap_overflow_corner_case
597 %type    <node>         return_statement break_statement continue_statement
598
599 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK
600 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
601 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
602 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
603 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
604 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
605 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
606 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
607 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
608 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
609 %type    <operator>     THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
610 %type    <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
611 %type    <operator>     NEW_TK ASSERT_TK
612
613 %type    <node>         method_body
614
615 %type    <node>         literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
616                         STRING_LIT_TK NULL_TK VOID_TK
617
618 %type    <node>         IF_TK WHILE_TK FOR_TK
619
620 %type    <node>         formal_parameter_list formal_parameter
621                         method_declarator method_header
622
623 %type    <node>         primitive_type reference_type type
624                         BOOLEAN_TK INTEGRAL_TK FP_TK
625
626 /* Added or modified JDK 1.1 rule types  */
627 %type    <node>         type_literals
628
629 %%
630 /* 19.2 Production from 2.3: The Syntactic Grammar  */
631 goal:  compilation_unit
632                 {}
633 ;
634
635 /* 19.3 Productions from 3: Lexical structure  */
636 literal:
637         INT_LIT_TK
638 |       FP_LIT_TK
639 |       BOOL_LIT_TK
640 |       CHAR_LIT_TK
641 |       STRING_LIT_TK
642 |       NULL_TK
643 ;
644
645 /* 19.4 Productions from 4: Types, Values and Variables  */
646 type:
647         primitive_type
648 |       reference_type
649 ;
650
651 primitive_type:
652         INTEGRAL_TK
653 |       FP_TK
654 |       BOOLEAN_TK
655 ;
656
657 reference_type:
658         class_or_interface_type
659 |       array_type
660 ;
661
662 class_or_interface_type:
663         name
664 ;
665
666 class_type:
667         class_or_interface_type /* Default rule */
668 ;
669
670 interface_type:
671          class_or_interface_type
672 ;
673
674 array_type:
675         primitive_type dims
676                 {
677                   int osb = pop_current_osb (ctxp);
678                   tree t = build_java_array_type (($1), -1);
679                   while (--osb)
680                     t = build_unresolved_array_type (t);
681                   $$ = t;
682                 }
683 |       name dims
684                 {
685                   int osb = pop_current_osb (ctxp);
686                   tree t = $1;
687                   while (osb--)
688                     t = build_unresolved_array_type (t);
689                   $$ = t;
690                 }
691 ;
692
693 /* 19.5 Productions from 6: Names  */
694 name:
695         simple_name             /* Default rule */
696 |       qualified_name          /* Default rule */
697 ;
698
699 simple_name:
700         identifier              /* Default rule */
701 ;
702
703 qualified_name:
704         name DOT_TK identifier
705                 { $$ = make_qualified_name ($1, $3, $2.location); }
706 ;
707
708 identifier:
709         ID_TK
710 ;
711
712 /* 19.6: Production from 7: Packages  */
713 compilation_unit:
714                 {$$ = NULL;}
715 |       package_declaration
716 |       import_declarations
717 |       type_declarations
718 |       package_declaration import_declarations
719 |       package_declaration type_declarations
720 |       import_declarations type_declarations
721 |       package_declaration import_declarations type_declarations
722 ;
723
724 import_declarations:
725         import_declaration
726                 {
727                   $$ = NULL;
728                 }
729 |       import_declarations import_declaration
730                 {
731                   $$ = NULL;
732                 }
733 ;
734
735 type_declarations:
736         type_declaration
737 |       type_declarations type_declaration
738 ;
739
740 package_declaration:
741         PACKAGE_TK name SC_TK
742                 {
743                   ctxp->package = EXPR_WFL_NODE ($2);
744                   register_package (ctxp->package);
745                 }
746 |       PACKAGE_TK error
747                 {yyerror ("Missing name"); RECOVER;}
748 |       PACKAGE_TK name error
749                 {yyerror ("';' expected"); RECOVER;}
750 ;
751
752 import_declaration:
753         single_type_import_declaration
754 |       type_import_on_demand_declaration
755 ;
756
757 single_type_import_declaration:
758         IMPORT_TK name SC_TK
759                 {
760                   tree name = EXPR_WFL_NODE ($2), last_name;
761                   int   i = IDENTIFIER_LENGTH (name)-1;
762                   const char *last = &IDENTIFIER_POINTER (name)[i];
763                   while (last != IDENTIFIER_POINTER (name))
764                     {
765                       if (last [0] == '.')
766                         break;
767                       last--;
768                     }
769                   last_name = get_identifier (++last);
770                   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
771                     {
772                       tree err = find_name_in_single_imports (last_name);
773                       if (err && err != name)
774                         parse_error_context
775                           ($2, "Ambiguous class: %qs and %qs",
776                            IDENTIFIER_POINTER (name),
777                            IDENTIFIER_POINTER (err));
778                       else
779                         REGISTER_IMPORT ($2, last_name);
780                     }
781                   else
782                     REGISTER_IMPORT ($2, last_name);
783                 }
784 |       IMPORT_TK error
785                 {yyerror ("Missing name"); RECOVER;}
786 |       IMPORT_TK name error
787                 {yyerror ("';' expected"); RECOVER;}
788 ;
789
790 type_import_on_demand_declaration:
791         IMPORT_TK name DOT_TK MULT_TK SC_TK
792                 {
793                   tree name = EXPR_WFL_NODE ($2);
794                   tree it;
795                   /* Search for duplicates. */
796                   for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
797                     if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
798                       break;
799                   /* Don't import the same thing more than once, just ignore
800                      duplicates (7.5.2) */
801                   if (! it)
802                     {
803                       read_import_dir ($2);
804                       ctxp->import_demand_list =
805                         chainon (ctxp->import_demand_list,
806                                  build_tree_list ($2, NULL_TREE));
807                     }
808                 }
809 |       IMPORT_TK name DOT_TK error
810                 {yyerror ("'*' expected"); RECOVER;}
811 |       IMPORT_TK name DOT_TK MULT_TK error
812                 {yyerror ("';' expected"); RECOVER;}
813 ;
814
815 type_declaration:
816         class_declaration
817                 { end_class_declaration (0); }
818 |       interface_declaration
819                 { end_class_declaration (0); }
820 |       empty_statement
821 |       error
822                 {
823                   YYERROR_NOW;
824                   yyerror ("Class or interface declaration expected");
825                 }
826 ;
827
828 /* 19.7 Shortened from the original:
829    modifiers: modifier | modifiers modifier
830    modifier: any of public...  */
831 modifiers:
832         MODIFIER_TK
833                 {
834                   $$ = (1 << $1);
835                 }
836 |       modifiers MODIFIER_TK
837                 {
838                   int acc = (1 << $2);
839                   if ($$ & acc)
840                     parse_error_context
841                       (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
842                        java_accstring_lookup (acc));
843                   else
844                     {
845                       $$ |= acc;
846                     }
847                 }
848 ;
849
850 /* 19.8.1 Production from $8.1: Class Declaration */
851 class_declaration:
852         modifiers CLASS_TK identifier super interfaces
853                 { create_class ($1, $3, $4, $5); }
854         class_body
855                 {;}
856 |       CLASS_TK identifier super interfaces
857                 { create_class (0, $2, $3, $4); }
858         class_body
859                 {;}
860 |       modifiers CLASS_TK error
861                 { yyerror ("Missing class name"); RECOVER; }
862 |       CLASS_TK error
863                 { yyerror ("Missing class name"); RECOVER; }
864 |       CLASS_TK identifier error
865                 {
866                   if (!ctxp->class_err) yyerror ("'{' expected");
867                   DRECOVER(class1);
868                 }
869 |       modifiers CLASS_TK identifier error
870                 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
871 ;
872
873 super:
874                 { $$ = NULL; }
875 |       EXTENDS_TK class_type
876                 { $$ = $2; }
877 |       EXTENDS_TK class_type error
878                 {yyerror ("'{' expected"); ctxp->class_err=1;}
879 |       EXTENDS_TK error
880                 {yyerror ("Missing super class name"); ctxp->class_err=1;}
881 ;
882
883 interfaces:
884                 { $$ = NULL_TREE; }
885 |       IMPLEMENTS_TK interface_type_list
886                 { $$ = $2; }
887 |       IMPLEMENTS_TK error
888                 {
889                   ctxp->class_err=1;
890                   yyerror ("Missing interface name");
891                 }
892 ;
893
894 interface_type_list:
895         interface_type
896                 {
897                   ctxp->interface_number = 1;
898                   $$ = build_tree_list ($1, NULL_TREE);
899                 }
900 |       interface_type_list C_TK interface_type
901                 {
902                   ctxp->interface_number++;
903                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
904                 }
905 |       interface_type_list C_TK error
906                 {yyerror ("Missing interface name"); RECOVER;}
907 ;
908
909 class_body:
910         OCB_TK CCB_TK
911                 {
912                   /* Store the location of the `}' when doing xrefs */
913                   if (flag_emit_xref)
914                     DECL_END_SOURCE_LINE (GET_CPC ()) = $2.location;
915                   $$ = GET_CPC ();
916                 }
917 |       OCB_TK class_body_declarations CCB_TK
918                 {
919                   /* Store the location of the `}' when doing xrefs */
920                   if (flag_emit_xref)
921                     DECL_END_SOURCE_LINE (GET_CPC ()) = $3.location;
922                   $$ = GET_CPC ();
923                 }
924 ;
925
926 class_body_declarations:
927         class_body_declaration
928 |       class_body_declarations class_body_declaration
929 ;
930
931 class_body_declaration:
932         class_member_declaration
933 |       static_initializer
934 |       constructor_declaration
935 |       block                   /* Added, JDK1.1, instance initializer */
936                 {
937                   if (!IS_EMPTY_STMT ($1))
938                     {
939                       TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
940                       SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
941                     }
942                 }
943 ;
944
945 class_member_declaration:
946         field_declaration
947 |       method_declaration
948 |       class_declaration       /* Added, JDK1.1 inner classes */
949                 { end_class_declaration (1); }
950 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
951                 { end_class_declaration (1); }
952 |       empty_statement
953 ;
954
955 /* 19.8.2 Productions from 8.3: Field Declarations  */
956 field_declaration:
957         type variable_declarators SC_TK
958                 { register_fields (0, $1, $2); }
959 |       modifiers type variable_declarators SC_TK
960                 {
961                   check_modifiers
962                     ("Illegal modifier %qs for field declaration",
963                      $1, FIELD_MODIFIERS);
964                   check_modifiers_consistency ($1);
965                   register_fields ($1, $2, $3);
966                 }
967 ;
968
969 variable_declarators:
970         /* Should we use build_decl_list () instead ? FIXME */
971         variable_declarator     /* Default rule */
972 |       variable_declarators C_TK variable_declarator
973                 { $$ = chainon ($1, $3); }
974 |       variable_declarators C_TK error
975                 {yyerror ("Missing term"); RECOVER;}
976 ;
977
978 variable_declarator:
979         variable_declarator_id
980                 { $$ = build_tree_list ($1, NULL_TREE); }
981 |       variable_declarator_id ASSIGN_TK variable_initializer
982                 {
983                   if (java_error_count)
984                     $3 = NULL_TREE;
985                   $$ = build_tree_list
986                     ($1, build_assignment ($2.token, $2.location, $1, $3));
987                 }
988 |       variable_declarator_id ASSIGN_TK error
989                 {
990                   yyerror ("Missing variable initializer");
991                   $$ = build_tree_list ($1, NULL_TREE);
992                   RECOVER;
993                 }
994 |       variable_declarator_id ASSIGN_TK variable_initializer error
995                 {
996                   yyerror ("';' expected");
997                   $$ = build_tree_list ($1, NULL_TREE);
998                   RECOVER;
999                 }
1000 ;
1001
1002 variable_declarator_id:
1003         identifier
1004 |       variable_declarator_id OSB_TK CSB_TK
1005                 { $$ = build_unresolved_array_type ($1); }
1006 |       identifier error
1007                 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
1008 |       variable_declarator_id OSB_TK error
1009                 {
1010                   yyerror ("']' expected");
1011                   DRECOVER(vdi);
1012                 }
1013 |       variable_declarator_id CSB_TK error
1014                 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1015 ;
1016
1017 variable_initializer:
1018         expression
1019 |       array_initializer
1020 ;
1021
1022 /* 19.8.3 Productions from 8.4: Method Declarations  */
1023 method_declaration:
1024         method_header
1025                 {
1026                   current_function_decl = $1;
1027                   if (current_function_decl
1028                       && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1029                     source_start_java_method (current_function_decl);
1030                   else
1031                     current_function_decl = NULL_TREE;
1032                 }
1033         method_body
1034                 { finish_method_declaration ($3); }
1035 |       method_header error
1036                 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1037 ;
1038
1039 method_header:
1040         type method_declarator throws
1041                 { $$ = method_header (0, $1, $2, $3); }
1042 |       VOID_TK method_declarator throws
1043                 { $$ = method_header (0, void_type_node, $2, $3); }
1044 |       modifiers type method_declarator throws
1045                 { $$ = method_header ($1, $2, $3, $4); }
1046 |       modifiers VOID_TK method_declarator throws
1047                 { $$ = method_header ($1, void_type_node, $3, $4); }
1048 |       type error
1049                 {
1050                   yyerror ("Invalid method declaration, method name required");
1051                   RECOVER;
1052                 }
1053 |       modifiers type error
1054                 {
1055                   yyerror ("Identifier expected");
1056                   RECOVER;
1057                 }
1058 |       VOID_TK error
1059                 {
1060                   yyerror ("Identifier expected");
1061                   RECOVER;
1062                 }
1063 |       modifiers VOID_TK error
1064                 {
1065                   yyerror ("Identifier expected");
1066                   RECOVER;
1067                 }
1068 |       modifiers error
1069                 {
1070                   yyerror ("Invalid method declaration, return type required");
1071                   RECOVER;
1072                 }
1073 ;
1074
1075 method_declarator:
1076         identifier OP_TK CP_TK
1077                 {
1078                   ctxp->formal_parameter_number = 0;
1079                   $$ = method_declarator ($1, NULL_TREE);
1080                 }
1081 |       identifier OP_TK formal_parameter_list CP_TK
1082                 { $$ = method_declarator ($1, $3); }
1083 |       method_declarator OSB_TK CSB_TK
1084                 {
1085                   SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1086                   TREE_PURPOSE ($1) =
1087                     build_unresolved_array_type (TREE_PURPOSE ($1));
1088                   parse_warning_context
1089                     (wfl_operator,
1090                      "Discouraged form of returned type specification");
1091                 }
1092 |       identifier OP_TK error
1093                 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1094 |       method_declarator OSB_TK error
1095                 {yyerror ("']' expected"); RECOVER;}
1096 ;
1097
1098 formal_parameter_list:
1099         formal_parameter
1100                 {
1101                   ctxp->formal_parameter_number = 1;
1102                 }
1103 |       formal_parameter_list C_TK formal_parameter
1104                 {
1105                   ctxp->formal_parameter_number += 1;
1106                   $$ = chainon ($1, $3);
1107                 }
1108 |       formal_parameter_list C_TK error
1109                 { yyerror ("Missing formal parameter term"); RECOVER; }
1110 ;
1111
1112 formal_parameter:
1113         type variable_declarator_id
1114                 {
1115                   $$ = build_tree_list ($2, $1);
1116                 }
1117 |       final type variable_declarator_id /* Added, JDK1.1 final parms */
1118                 {
1119                   $$ = build_tree_list ($3, $2);
1120                   ARG_FINAL_P ($$) = 1;
1121                 }
1122 |       type error
1123                 {
1124                   yyerror ("Missing identifier"); RECOVER;
1125                   $$ = NULL_TREE;
1126                 }
1127 |       final type error
1128                 {
1129                   yyerror ("Missing identifier"); RECOVER;
1130                   $$ = NULL_TREE;
1131                 }
1132 ;
1133
1134 final:
1135         modifiers
1136                 {
1137                   check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1138                                    $1, ACC_FINAL);
1139                   if ($1 != ACC_FINAL)
1140                     MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1141                 }
1142 ;
1143
1144 throws:
1145                 { $$ = NULL_TREE; }
1146 |       THROWS_TK class_type_list
1147                 { $$ = $2; }
1148 |       THROWS_TK error
1149                 {yyerror ("Missing class type term"); RECOVER;}
1150 ;
1151
1152 class_type_list:
1153         class_type
1154                 { $$ = build_tree_list ($1, $1); }
1155 |       class_type_list C_TK class_type
1156                 { $$ = tree_cons ($3, $3, $1); }
1157 |       class_type_list C_TK error
1158                 {yyerror ("Missing class type term"); RECOVER;}
1159 ;
1160
1161 method_body:
1162         block
1163 |       SC_TK { $$ = NULL_TREE; }
1164 ;
1165
1166 /* 19.8.4 Productions from 8.5: Static Initializers  */
1167 static_initializer:
1168         static block
1169                 {
1170                   TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1171                   SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1172                   current_static_block = NULL_TREE;
1173                 }
1174 ;
1175
1176 static:                         /* Test lval.sub_token here */
1177         modifiers
1178                 {
1179                   check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1180                   /* Can't have a static initializer in an innerclass */
1181                   if ($1 | ACC_STATIC &&
1182                       GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1183                     parse_error_context
1184                       (MODIFIER_WFL (STATIC_TK),
1185                        "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1186                        IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1187                   SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1188                 }
1189 ;
1190
1191 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1192 constructor_declaration:
1193         constructor_header
1194                 {
1195                   current_function_decl = $1;
1196                   source_start_java_method (current_function_decl);
1197                 }
1198         constructor_body
1199                 { finish_method_declaration ($3); }
1200 ;
1201
1202 constructor_header:
1203         constructor_declarator throws
1204                 { $$ = method_header (0, NULL_TREE, $1, $2); }
1205 |       modifiers constructor_declarator throws
1206                 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1207 ;
1208
1209 constructor_declarator:
1210         simple_name OP_TK CP_TK
1211                 {
1212                   ctxp->formal_parameter_number = 0;
1213                   $$ = method_declarator ($1, NULL_TREE);
1214                 }
1215 |       simple_name OP_TK formal_parameter_list CP_TK
1216                 { $$ = method_declarator ($1, $3); }
1217 ;
1218
1219 constructor_body:
1220         /* Unlike regular method, we always need a complete (empty)
1221            body so we can safely perform all the required code
1222            addition (super invocation and field initialization) */
1223         block_begin constructor_block_end
1224                 {
1225                   BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1226                   $$ = $2;
1227                 }
1228 |       block_begin explicit_constructor_invocation constructor_block_end
1229                 { $$ = $3; }
1230 |       block_begin block_statements constructor_block_end
1231                 { $$ = $3; }
1232 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1233                 { $$ = $4; }
1234 ;
1235
1236 constructor_block_end:
1237         block_end
1238 ;
1239
1240 /* Error recovery for that rule moved down expression_statement: rule.  */
1241 explicit_constructor_invocation:
1242         this_or_super OP_TK CP_TK SC_TK
1243                 {
1244                   $$ = build_method_invocation ($1, NULL_TREE);
1245                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1246                   $$ = java_method_add_stmt (current_function_decl, $$);
1247                 }
1248 |       this_or_super OP_TK argument_list CP_TK SC_TK
1249                 {
1250                   $$ = build_method_invocation ($1, $3);
1251                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1252                   $$ = java_method_add_stmt (current_function_decl, $$);
1253                 }
1254         /* Added, JDK1.1 inner classes. Modified because the rule
1255            'primary' couldn't work.  */
1256 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1257                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1258 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1259                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1260 ;
1261
1262 this_or_super:                  /* Added, simplifies error diagnostics */
1263         THIS_TK
1264                 {
1265                   tree wfl = build_wfl_node (this_identifier_node);
1266                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1267                   $$ = wfl;
1268                 }
1269 |       SUPER_TK
1270                 {
1271                   tree wfl = build_wfl_node (super_identifier_node);
1272                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1273                   $$ = wfl;
1274                 }
1275 ;
1276
1277 /* 19.9 Productions from 9: Interfaces  */
1278 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1279 interface_declaration:
1280         INTERFACE_TK identifier
1281                 { create_interface (0, $2, NULL_TREE); }
1282         interface_body
1283                 { ; }
1284 |       modifiers INTERFACE_TK identifier
1285                 { create_interface ($1, $3, NULL_TREE); }
1286         interface_body
1287                 { ; }
1288 |       INTERFACE_TK identifier extends_interfaces
1289                 { create_interface (0, $2, $3); }
1290         interface_body
1291                 { ; }
1292 |       modifiers INTERFACE_TK identifier extends_interfaces
1293                 { create_interface ($1, $3, $4); }
1294         interface_body
1295                 { ; }
1296 |       INTERFACE_TK identifier error
1297                 { yyerror ("'{' expected"); RECOVER; }
1298 |       modifiers INTERFACE_TK identifier error
1299                 { yyerror ("'{' expected"); RECOVER; }
1300 ;
1301
1302 extends_interfaces:
1303         EXTENDS_TK interface_type
1304                 {
1305                   ctxp->interface_number = 1;
1306                   $$ = build_tree_list ($2, NULL_TREE);
1307                 }
1308 |       extends_interfaces C_TK interface_type
1309                 {
1310                   ctxp->interface_number++;
1311                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1312                 }
1313 |       EXTENDS_TK error
1314                 {yyerror ("Invalid interface type"); RECOVER;}
1315 |       extends_interfaces C_TK error
1316                 {yyerror ("Missing term"); RECOVER;}
1317 ;
1318
1319 interface_body:
1320         OCB_TK CCB_TK
1321                 { $$ = NULL_TREE; }
1322 |       OCB_TK interface_member_declarations CCB_TK
1323                 { $$ = NULL_TREE; }
1324 ;
1325
1326 interface_member_declarations:
1327         interface_member_declaration
1328 |       interface_member_declarations interface_member_declaration
1329 ;
1330
1331 interface_member_declaration:
1332         constant_declaration
1333 |       abstract_method_declaration
1334 |       class_declaration       /* Added, JDK1.1 inner classes */
1335                 { end_class_declaration (1); }
1336 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
1337                 { end_class_declaration (1); }
1338 ;
1339
1340 constant_declaration:
1341         field_declaration
1342 ;
1343
1344 abstract_method_declaration:
1345         method_header SC_TK
1346                 {
1347                   check_abstract_method_header ($1);
1348                   current_function_decl = NULL_TREE; /* FIXME ? */
1349                 }
1350 |       method_header error
1351                 {yyerror ("';' expected"); RECOVER;}
1352 ;
1353
1354 /* 19.10 Productions from 10: Arrays  */
1355 array_initializer:
1356         OCB_TK CCB_TK
1357                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1358 |       OCB_TK C_TK CCB_TK
1359                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1360 |       OCB_TK variable_initializers CCB_TK
1361                 { $$ = build_new_array_init ($1.location, $2); }
1362 |       OCB_TK variable_initializers C_TK CCB_TK
1363                 { $$ = build_new_array_init ($1.location, $2); }
1364 ;
1365
1366 variable_initializers:
1367         variable_initializer
1368                 {
1369                   $$ = tree_cons (maybe_build_array_element_wfl ($1),
1370                                   $1, NULL_TREE);
1371                 }
1372 |       variable_initializers C_TK variable_initializer
1373                 {
1374                   $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1375                 }
1376 |       variable_initializers C_TK error
1377                 {yyerror ("Missing term"); RECOVER;}
1378 ;
1379
1380 /* 19.11 Production from 14: Blocks and Statements  */
1381 block:
1382         block_begin block_end
1383                 { $$ = $2; }
1384 |       block_begin block_statements block_end
1385                 { $$ = $3; }
1386 ;
1387
1388 block_begin:
1389         OCB_TK
1390                 { enter_block (); }
1391 ;
1392
1393 block_end:
1394         CCB_TK
1395                 {
1396                   maybe_absorb_scoping_blocks ();
1397                   /* Store the location of the `}' when doing xrefs */
1398                   if (current_function_decl && flag_emit_xref)
1399                     DECL_END_SOURCE_LINE (current_function_decl) = $1.location;
1400                   $$ = exit_block ();
1401                   if (!BLOCK_SUBBLOCKS ($$))
1402                     BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1403                 }
1404 ;
1405
1406 block_statements:
1407         block_statement
1408 |       block_statements block_statement
1409 ;
1410
1411 block_statement:
1412         local_variable_declaration_statement
1413 |       statement
1414                 { java_method_add_stmt (current_function_decl, $1); }
1415 |       class_declaration       /* Added, JDK1.1 local classes */
1416                 {
1417                   LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1418                   end_class_declaration (1);
1419                 }
1420 ;
1421
1422 local_variable_declaration_statement:
1423         local_variable_declaration SC_TK /* Can't catch missing ';' here */
1424 ;
1425
1426 local_variable_declaration:
1427         type variable_declarators
1428                 { declare_local_variables (0, $1, $2); }
1429 |       final type variable_declarators /* Added, JDK1.1 final locals */
1430                 { declare_local_variables ($1, $2, $3); }
1431 ;
1432
1433 statement:
1434         statement_without_trailing_substatement
1435 |       labeled_statement
1436 |       if_then_statement
1437 |       if_then_else_statement
1438 |       while_statement
1439 |       for_statement
1440                 { $$ = exit_block (); }
1441 ;
1442
1443 statement_nsi:
1444         statement_without_trailing_substatement
1445 |       labeled_statement_nsi
1446 |       if_then_else_statement_nsi
1447 |       while_statement_nsi
1448 |       for_statement_nsi
1449                 { $$ = exit_block (); }
1450 ;
1451
1452 statement_without_trailing_substatement:
1453         block
1454 |       empty_statement
1455 |       expression_statement
1456 |       switch_statement
1457 |       do_statement
1458 |       break_statement
1459 |       continue_statement
1460 |       return_statement
1461 |       synchronized_statement
1462 |       throw_statement
1463 |       try_statement
1464 |       assert_statement
1465 ;
1466
1467 empty_statement:
1468         SC_TK
1469                 {
1470                   if (flag_extraneous_semicolon
1471                       && ! current_static_block
1472                       && (! current_function_decl ||
1473                           /* Verify we're not in a inner class declaration */
1474                           (GET_CPC () != TYPE_NAME
1475                            (DECL_CONTEXT (current_function_decl)))))
1476
1477                     {
1478 #ifdef USE_MAPPED_LOCATION
1479                       SET_EXPR_LOCATION (wfl_operator, input_location);
1480 #else
1481                       EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1482 #endif
1483                       parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1484                     }
1485                   $$ = build_java_empty_stmt ();
1486                 }
1487 ;
1488
1489 label_decl:
1490         identifier REL_CL_TK
1491                 {
1492                   $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1493                                             EXPR_WFL_NODE ($1));
1494                   pushlevel (2);
1495                   push_labeled_block ($$);
1496                   PUSH_LABELED_BLOCK ($$);
1497                 }
1498 ;
1499
1500 labeled_statement:
1501         label_decl statement
1502                 { $$ = finish_labeled_statement ($1, $2); }
1503 |       identifier error
1504                 {yyerror ("':' expected"); RECOVER;}
1505 ;
1506
1507 labeled_statement_nsi:
1508         label_decl statement_nsi
1509                 { $$ = finish_labeled_statement ($1, $2); }
1510 ;
1511
1512 /* We concentrate here a bunch of error handling rules that we couldn't write
1513    earlier, because expression_statement catches a missing ';'.  */
1514 expression_statement:
1515         statement_expression SC_TK
1516                 {
1517                   /* We have a statement. Generate a WFL around it so
1518                      we can debug it */
1519 #ifdef USE_MAPPED_LOCATION
1520                   $$ = expr_add_location ($1, input_location, 1);
1521 #else
1522                   $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1523                   JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1524 #endif
1525                   /* We know we have a statement, so set the debug
1526                      info to be eventually generate here. */
1527                 }
1528 |       error SC_TK
1529                 {
1530                   YYNOT_TWICE yyerror ("Invalid expression statement");
1531                   DRECOVER (expr_stmt);
1532                 }
1533 |       error OCB_TK
1534                 {
1535                   YYNOT_TWICE yyerror ("Invalid expression statement");
1536                   DRECOVER (expr_stmt);
1537                 }
1538 |       error CCB_TK
1539                 {
1540                   YYNOT_TWICE yyerror ("Invalid expression statement");
1541                   DRECOVER (expr_stmt);
1542                 }
1543 |       this_or_super OP_TK error
1544                 {yyerror ("')' expected"); RECOVER;}
1545 |       this_or_super OP_TK CP_TK error
1546                 {
1547                   parse_ctor_invocation_error ();
1548                   RECOVER;
1549                 }
1550 |       this_or_super OP_TK argument_list error
1551                 {yyerror ("')' expected"); RECOVER;}
1552 |       this_or_super OP_TK argument_list CP_TK error
1553                 {
1554                   parse_ctor_invocation_error ();
1555                   RECOVER;
1556                 }
1557 |       name DOT_TK SUPER_TK error
1558                 {yyerror ("'(' expected"); RECOVER;}
1559 |       name DOT_TK SUPER_TK OP_TK error
1560                 {yyerror ("')' expected"); RECOVER;}
1561 |       name DOT_TK SUPER_TK OP_TK argument_list error
1562                 {yyerror ("')' expected"); RECOVER;}
1563 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1564                 {yyerror ("';' expected"); RECOVER;}
1565 |       name DOT_TK SUPER_TK OP_TK CP_TK error
1566                 {yyerror ("';' expected"); RECOVER;}
1567 ;
1568
1569 statement_expression:
1570         assignment
1571 |       pre_increment_expression
1572 |       pre_decrement_expression
1573 |       post_increment_expression
1574 |       post_decrement_expression
1575 |       method_invocation
1576 |       class_instance_creation_expression
1577 ;
1578
1579 if_then_statement:
1580         IF_TK OP_TK expression CP_TK statement
1581                 {
1582                   $$ = build_if_else_statement ($2.location, $3,
1583                                                 $5, NULL_TREE);
1584                 }
1585 |       IF_TK error
1586                 {yyerror ("'(' expected"); RECOVER;}
1587 |       IF_TK OP_TK error
1588                 {yyerror ("Missing term"); RECOVER;}
1589 |       IF_TK OP_TK expression error
1590                 {yyerror ("')' expected"); RECOVER;}
1591 ;
1592
1593 if_then_else_statement:
1594         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1595                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1596 ;
1597
1598 if_then_else_statement_nsi:
1599         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1600                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1601 ;
1602
1603 switch_statement:
1604         switch_expression
1605                 {
1606                   enter_block ();
1607                 }
1608         switch_block
1609                 {
1610                   /* Make into "proper list" of COMPOUND_EXPRs.
1611                      I.e. make the last statement also have its own
1612                      COMPOUND_EXPR. */
1613                   maybe_absorb_scoping_blocks ();
1614                   TREE_OPERAND ($1, 1) = exit_block ();
1615                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1616                 }
1617 ;
1618
1619 switch_expression:
1620         SWITCH_TK OP_TK expression CP_TK
1621                 {
1622                   $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1623                                NULL_TREE, NULL_TREE);
1624                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1625                 }
1626 |       SWITCH_TK error
1627                 {yyerror ("'(' expected"); RECOVER;}
1628 |       SWITCH_TK OP_TK error
1629                 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1630 |       SWITCH_TK OP_TK expression CP_TK error
1631                 {yyerror ("'{' expected"); RECOVER;}
1632 ;
1633
1634 /* Default assignment is there to avoid type node on switch_block
1635    node. */
1636
1637 switch_block:
1638         OCB_TK CCB_TK
1639                 { $$ = NULL_TREE; }
1640 |       OCB_TK switch_labels CCB_TK
1641                 { $$ = NULL_TREE; }
1642 |       OCB_TK switch_block_statement_groups CCB_TK
1643                 { $$ = NULL_TREE; }
1644 |       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1645                 { $$ = NULL_TREE; }
1646 ;
1647
1648 switch_block_statement_groups:
1649         switch_block_statement_group
1650 |       switch_block_statement_groups switch_block_statement_group
1651 ;
1652
1653 switch_block_statement_group:
1654         switch_labels block_statements
1655 ;
1656
1657 switch_labels:
1658         switch_label
1659 |       switch_labels switch_label
1660 ;
1661
1662 switch_label:
1663         CASE_TK constant_expression REL_CL_TK
1664                 {
1665                   tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1666                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1667                   java_method_add_stmt (current_function_decl, lab);
1668                 }
1669 |       DEFAULT_TK REL_CL_TK
1670                 {
1671                   tree lab = make_node (DEFAULT_EXPR);
1672                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1673                   java_method_add_stmt (current_function_decl, lab);
1674                 }
1675 |       CASE_TK error
1676                 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1677 |       CASE_TK constant_expression error
1678                 {yyerror ("':' expected"); RECOVER;}
1679 |       DEFAULT_TK error
1680                 {yyerror ("':' expected"); RECOVER;}
1681 ;
1682
1683 while_expression:
1684         WHILE_TK OP_TK expression CP_TK
1685                 {
1686                   tree body = build_loop_body ($2.location, $3, 0);
1687                   $$ = build_new_loop (body);
1688                 }
1689 ;
1690
1691 while_statement:
1692         while_expression statement
1693                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1694 |       WHILE_TK error
1695                 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1696 |       WHILE_TK OP_TK error
1697                 {yyerror ("Missing term and ')' expected"); RECOVER;}
1698 |       WHILE_TK OP_TK expression error
1699                 {yyerror ("')' expected"); RECOVER;}
1700 ;
1701
1702 while_statement_nsi:
1703         while_expression statement_nsi
1704                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1705 ;
1706
1707 do_statement_begin:
1708         DO_TK
1709                 {
1710                   tree body = build_loop_body (0, NULL_TREE, 1);
1711                   $$ = build_new_loop (body);
1712                 }
1713         /* Need error handing here. FIXME */
1714 ;
1715
1716 do_statement:
1717         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1718                 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1719 ;
1720
1721 for_statement:
1722         for_begin SC_TK expression SC_TK for_update CP_TK statement
1723                 {
1724                   if (CONSTANT_CLASS_P ($3))
1725                     $3 = build_wfl_node ($3);
1726                   $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1727                 }
1728 |       for_begin SC_TK SC_TK for_update CP_TK statement
1729                 {
1730                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1731                   /* We have not condition, so we get rid of the EXIT_EXPR */
1732                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1733                     build_java_empty_stmt ();
1734                 }
1735 |       for_begin SC_TK error
1736                 {yyerror ("Invalid control expression"); RECOVER;}
1737 |       for_begin SC_TK expression SC_TK error
1738                 {yyerror ("Invalid update expression"); RECOVER;}
1739 |       for_begin SC_TK SC_TK error
1740                 {yyerror ("Invalid update expression"); RECOVER;}
1741 ;
1742
1743 for_statement_nsi:
1744         for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1745                 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1746 |       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1747                 {
1748                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1749                   /* We have not condition, so we get rid of the EXIT_EXPR */
1750                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1751                     build_java_empty_stmt ();
1752                 }
1753 ;
1754
1755 for_header:
1756         FOR_TK OP_TK
1757                 {
1758                   /* This scope defined for local variable that may be
1759                      defined within the scope of the for loop */
1760                   enter_block ();
1761                 }
1762 |       FOR_TK error
1763                 {yyerror ("'(' expected"); DRECOVER(for_1);}
1764 |       FOR_TK OP_TK error
1765                 {yyerror ("Invalid init statement"); RECOVER;}
1766 ;
1767
1768 for_begin:
1769         for_header for_init
1770                 {
1771                   /* We now declare the loop body. The loop is
1772                      declared as a for loop. */
1773                   tree body = build_loop_body (0, NULL_TREE, 0);
1774                   $$ =  build_new_loop (body);
1775                   FOR_LOOP_P ($$) = 1;
1776                   /* The loop is added to the current block the for
1777                      statement is defined within */
1778                   java_method_add_stmt (current_function_decl, $$);
1779                 }
1780 ;
1781 for_init:                       /* Can be empty */
1782                 { $$ = build_java_empty_stmt (); }
1783 |       statement_expression_list
1784                 {
1785                   /* Init statement recorded within the previously
1786                      defined block scope */
1787                   $$ = java_method_add_stmt (current_function_decl, $1);
1788                 }
1789 |       local_variable_declaration
1790                 {
1791                   /* Local variable are recorded within the previously
1792                      defined block scope */
1793                   $$ = NULL_TREE;
1794                 }
1795 |       statement_expression_list error
1796                 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1797 ;
1798
1799 for_update:                     /* Can be empty */
1800                 {$$ = build_java_empty_stmt ();}
1801 |       statement_expression_list
1802                 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1803 ;
1804
1805 statement_expression_list:
1806         statement_expression
1807                 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1808 |       statement_expression_list C_TK statement_expression
1809                 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1810 |       statement_expression_list C_TK error
1811                 {yyerror ("Missing term"); RECOVER;}
1812 ;
1813
1814 break_statement:
1815         BREAK_TK SC_TK
1816                 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1817 |       BREAK_TK identifier SC_TK
1818                 { $$ = build_bc_statement ($1.location, 1, $2); }
1819 |       BREAK_TK error
1820                 {yyerror ("Missing term"); RECOVER;}
1821 |       BREAK_TK identifier error
1822                 {yyerror ("';' expected"); RECOVER;}
1823 ;
1824
1825 continue_statement:
1826         CONTINUE_TK SC_TK
1827                 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1828 |       CONTINUE_TK identifier SC_TK
1829                 { $$ = build_bc_statement ($1.location, 0, $2); }
1830 |       CONTINUE_TK error
1831                 {yyerror ("Missing term"); RECOVER;}
1832 |       CONTINUE_TK identifier error
1833                 {yyerror ("';' expected"); RECOVER;}
1834 ;
1835
1836 return_statement:
1837         RETURN_TK SC_TK
1838                 { $$ = build_return ($1.location, NULL_TREE); }
1839 |       RETURN_TK expression SC_TK
1840                 { $$ = build_return ($1.location, $2); }
1841 |       RETURN_TK error
1842                 {yyerror ("Missing term"); RECOVER;}
1843 |       RETURN_TK expression error
1844                 {yyerror ("';' expected"); RECOVER;}
1845 ;
1846
1847 throw_statement:
1848         THROW_TK expression SC_TK
1849                 {
1850                   $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1851                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1852                 }
1853 |       THROW_TK error
1854                 {yyerror ("Missing term"); RECOVER;}
1855 |       THROW_TK expression error
1856                 {yyerror ("';' expected"); RECOVER;}
1857 ;
1858
1859 assert_statement:
1860         ASSERT_TK expression REL_CL_TK expression SC_TK
1861                 {
1862                   $$ = build_assertion ($1.location, $2, $4);
1863                 }
1864 |       ASSERT_TK expression SC_TK
1865                 {
1866                   $$ = build_assertion ($1.location, $2, NULL_TREE);
1867                 }
1868 |       ASSERT_TK error
1869                 {yyerror ("Missing term"); RECOVER;}
1870 |       ASSERT_TK expression error
1871                 {yyerror ("';' expected"); RECOVER;}
1872 ;
1873
1874 synchronized_statement:
1875         synchronized OP_TK expression CP_TK block
1876                 {
1877                   $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1878                   EXPR_WFL_LINECOL ($$) =
1879                     EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1880                 }
1881 |       synchronized OP_TK expression CP_TK error
1882                 {yyerror ("'{' expected"); RECOVER;}
1883 |       synchronized error
1884                 {yyerror ("'(' expected"); RECOVER;}
1885 |       synchronized OP_TK error CP_TK
1886                 {yyerror ("Missing term"); RECOVER;}
1887 |       synchronized OP_TK error
1888                 {yyerror ("Missing term"); RECOVER;}
1889 ;
1890
1891 synchronized:
1892         modifiers
1893                 {
1894                   check_modifiers (
1895              "Illegal modifier %qs. Only %<synchronized%> was expected here",
1896                                    $1, ACC_SYNCHRONIZED);
1897                   if ($1 != ACC_SYNCHRONIZED)
1898                     MODIFIER_WFL (SYNCHRONIZED_TK) =
1899                       build_wfl_node (NULL_TREE);
1900                 }
1901 ;
1902
1903 try_statement:
1904         TRY_TK block catches
1905                 { $$ = build_try_statement ($1.location, $2, $3); }
1906 |       TRY_TK block finally
1907                 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1908 |       TRY_TK block catches finally
1909                 { $$ = build_try_finally_statement
1910                     ($1.location, build_try_statement ($1.location,
1911                                                        $2, $3), $4);
1912                 }
1913 |       TRY_TK error
1914                 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1915 ;
1916
1917 catches:
1918         catch_clause
1919 |       catches catch_clause
1920                 {
1921                   TREE_CHAIN ($2) = $1;
1922                   $$ = $2;
1923                 }
1924 ;
1925
1926 catch_clause:
1927         catch_clause_parameter block
1928                 {
1929                   java_method_add_stmt (current_function_decl, $2);
1930                   exit_block ();
1931                   $$ = $1;
1932                 }
1933 ;
1934
1935 catch_clause_parameter:
1936         CATCH_TK OP_TK formal_parameter CP_TK
1937                 {
1938                   /* We add a block to define a scope for
1939                      formal_parameter (CCBP). The formal parameter is
1940                      declared initialized by the appropriate function
1941                      call */
1942                   tree ccpb;
1943                   tree init;
1944                   if ($3)
1945                     {
1946                       ccpb = enter_block ();
1947                       init = build_assignment
1948                         (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1949                          build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1950                       declare_local_variables (0, TREE_VALUE ($3),
1951                                                build_tree_list 
1952                                                (TREE_PURPOSE ($3), init));
1953                       $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1954                       SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1955                     }
1956                   else
1957                     {
1958                       $$ = error_mark_node;
1959                     }
1960                 }
1961 |       CATCH_TK error
1962                 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1963 |       CATCH_TK OP_TK error
1964                 {
1965                   yyerror ("Missing term or ')' expected");
1966                   RECOVER; $$ = NULL_TREE;
1967                 }
1968 |       CATCH_TK OP_TK error CP_TK /* That's for () */
1969                 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1970 ;
1971
1972 finally:
1973         FINALLY_TK block
1974                 { $$ = $2; }
1975 |       FINALLY_TK error
1976                 {yyerror ("'{' expected"); RECOVER; }
1977 ;
1978
1979 /* 19.12 Production from 15: Expressions  */
1980 primary:
1981         primary_no_new_array
1982 |       array_creation_uninitialized
1983 |       array_creation_initialized
1984 ;
1985
1986 primary_no_new_array:
1987         literal
1988 |       THIS_TK
1989                 { $$ = build_this ($1.location); }
1990 |       OP_TK expression CP_TK
1991                 {$$ = $2;}
1992 |       class_instance_creation_expression
1993 |       field_access
1994 |       method_invocation
1995 |       array_access
1996 |       type_literals
1997         /* Added, JDK1.1 inner classes. Documentation is wrong
1998            referring to a 'ClassName' (class_name) rule that doesn't
1999            exist. Used name: instead.  */
2000 |       name DOT_TK THIS_TK
2001                 {
2002                   tree wfl = build_wfl_node (this_identifier_node);
2003                   $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
2004                 }
2005 |       OP_TK expression error
2006                 {yyerror ("')' expected"); RECOVER;}
2007 |       name DOT_TK error
2008                 {yyerror ("'class' or 'this' expected" ); RECOVER;}
2009 |       primitive_type DOT_TK error
2010                 {yyerror ("'class' expected" ); RECOVER;}
2011 |       VOID_TK DOT_TK error
2012                 {yyerror ("'class' expected" ); RECOVER;}
2013 ;
2014
2015 type_literals:
2016         name DOT_TK CLASS_TK
2017                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2018 |       array_type DOT_TK CLASS_TK
2019                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2020 |       primitive_type DOT_TK CLASS_TK
2021                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2022 |       VOID_TK DOT_TK CLASS_TK
2023                 {
2024                    $$ = build_incomplete_class_ref ($2.location,
2025                                                    void_type_node);
2026                 }
2027 ;
2028
2029 class_instance_creation_expression:
2030         NEW_TK class_type OP_TK argument_list CP_TK
2031                 { $$ = build_new_invocation ($2, $4); }
2032 |       NEW_TK class_type OP_TK CP_TK
2033                 { $$ = build_new_invocation ($2, NULL_TREE); }
2034 |       anonymous_class_creation
2035         /* Added, JDK1.1 inner classes, modified to use name or
2036            primary instead of primary solely which couldn't work in
2037            all situations.  */
2038 |       something_dot_new identifier OP_TK CP_TK
2039                 {
2040                   tree ctor = build_new_invocation ($2, NULL_TREE);
2041                   $$ = make_qualified_primary ($1, ctor,
2042                                                EXPR_WFL_LINECOL ($1));
2043                 }
2044 |       something_dot_new identifier OP_TK CP_TK class_body
2045 |       something_dot_new identifier OP_TK argument_list CP_TK
2046                 {
2047                   tree ctor = build_new_invocation ($2, $4);
2048                   $$ = make_qualified_primary ($1, ctor,
2049                                                EXPR_WFL_LINECOL ($1));
2050                 }
2051 |       something_dot_new identifier OP_TK argument_list CP_TK class_body
2052 |       NEW_TK error SC_TK
2053                 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2054 |       NEW_TK class_type error
2055                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2056 |       NEW_TK class_type OP_TK error
2057                 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2058 |       NEW_TK class_type OP_TK argument_list error
2059                 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2060 |       something_dot_new error
2061                 {
2062                   $$ = NULL_TREE;
2063                   YYERROR_NOW;
2064                   yyerror ("Identifier expected");
2065                   RECOVER;
2066                 }
2067 |       something_dot_new identifier error
2068                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2069 ;
2070
2071 /* Created after JDK1.1 rules originally added to
2072    class_instance_creation_expression, but modified to use
2073    'class_type' instead of 'TypeName' (type_name) which is mentioned
2074    in the documentation but doesn't exist. */
2075
2076 anonymous_class_creation:
2077         NEW_TK class_type OP_TK argument_list CP_TK
2078                 { create_anonymous_class ($2); }
2079         class_body
2080                 {
2081                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2082                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2083
2084                   end_class_declaration (1);
2085
2086                   /* Now we can craft the new expression */
2087                   $$ = build_new_invocation (id, $4);
2088
2089                   /* Note that we can't possibly be here if
2090                      `class_type' is an interface (in which case the
2091                      anonymous class extends Object and implements
2092                      `class_type', hence its constructor can't have
2093                      arguments.) */
2094
2095                   /* Otherwise, the innerclass must feature a
2096                      constructor matching `argument_list'. Anonymous
2097                      classes are a bit special: it's impossible to
2098                      define constructor for them, hence constructors
2099                      must be generated following the hints provided by
2100                      the `new' expression. Whether a super constructor
2101                      of that nature exists or not is to be verified
2102                      later on in verify_constructor_super.
2103
2104                      It's during the expansion of a `new' statement
2105                      referring to an anonymous class that a ctor will
2106                      be generated for the anonymous class, with the
2107                      right arguments. */
2108
2109                 }
2110 |       NEW_TK class_type OP_TK CP_TK
2111                 { create_anonymous_class ($2); }
2112         class_body
2113                 {
2114                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2115                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2116
2117                   end_class_declaration (1);
2118
2119                   /* Now we can craft the new expression. The
2120                      statement doesn't need to be remember so that a
2121                      constructor can be generated, since its signature
2122                      is already known. */
2123                   $$ = build_new_invocation (id, NULL_TREE);
2124                 }
2125 ;
2126
2127 something_dot_new:              /* Added, not part of the specs. */
2128         name DOT_TK NEW_TK
2129                 { $$ = $1; }
2130 |       primary DOT_TK NEW_TK
2131                 { $$ = $1; }
2132 ;
2133
2134 argument_list:
2135         expression
2136                 {
2137                   $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2138                   ctxp->formal_parameter_number = 1;
2139                 }
2140 |       argument_list C_TK expression
2141                 {
2142                   ctxp->formal_parameter_number += 1;
2143                   $$ = tree_cons (NULL_TREE, $3, $1);
2144                 }
2145 |       argument_list C_TK error
2146                 {yyerror ("Missing term"); RECOVER;}
2147 ;
2148
2149 array_creation_uninitialized:
2150         NEW_TK primitive_type dim_exprs
2151                 { $$ = build_newarray_node ($2, $3, 0); }
2152 |       NEW_TK class_or_interface_type dim_exprs
2153                 { $$ = build_newarray_node ($2, $3, 0); }
2154 |       NEW_TK primitive_type dim_exprs dims
2155                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2156 |       NEW_TK class_or_interface_type dim_exprs dims
2157                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2158 |       NEW_TK error CSB_TK
2159                 {yyerror ("'[' expected"); DRECOVER ("]");}
2160 |       NEW_TK error OSB_TK
2161                 {yyerror ("']' expected"); RECOVER;}
2162 ;
2163
2164 array_creation_initialized:
2165         /* Added, JDK1.1 anonymous array. Initial documentation rule
2166            modified */
2167         NEW_TK class_or_interface_type dims array_initializer
2168                 {
2169                   char *sig;
2170                   int osb = pop_current_osb (ctxp);
2171                   while (osb--)
2172                     obstack_grow (&temporary_obstack, "[]", 2);
2173                   obstack_1grow (&temporary_obstack, '\0');
2174                   sig = obstack_finish (&temporary_obstack);
2175                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2176                                $2, get_identifier (sig), $4);
2177                 }
2178 |       NEW_TK primitive_type dims array_initializer
2179                 {
2180                   int osb = pop_current_osb (ctxp);
2181                   tree type = $2;
2182                   while (osb--)
2183                     type = build_java_array_type (type, -1);
2184                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2185                                build_pointer_type (type), NULL_TREE, $4);
2186                 }
2187 |       NEW_TK error CSB_TK
2188                 {yyerror ("'[' expected"); DRECOVER ("]");}
2189 |       NEW_TK error OSB_TK
2190                 {yyerror ("']' expected"); RECOVER;}
2191 ;
2192
2193 dim_exprs:
2194         dim_expr
2195                 { $$ = build_tree_list (NULL_TREE, $1); }
2196 |       dim_exprs dim_expr
2197                 { $$ = tree_cons (NULL_TREE, $2, $$); }
2198 ;
2199
2200 dim_expr:
2201         OSB_TK expression CSB_TK
2202                 {
2203                   if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2204                     {
2205                       $2 = build_wfl_node ($2);
2206                       TREE_TYPE ($2) = NULL_TREE;
2207                     }
2208                   EXPR_WFL_LINECOL ($2) = $1.location;
2209                   $$ = $2;
2210                 }
2211 |       OSB_TK expression error
2212                 {yyerror ("']' expected"); RECOVER;}
2213 |       OSB_TK error
2214                 {
2215                   yyerror ("Missing term");
2216                   yyerror ("']' expected");
2217                   RECOVER;
2218                 }
2219 ;
2220
2221 dims:
2222         OSB_TK CSB_TK
2223                 {
2224                   int allocate = 0;
2225                   /* If not initialized, allocate memory for the osb
2226                      numbers stack */
2227                   if (!ctxp->osb_limit)
2228                     {
2229                       allocate = ctxp->osb_limit = 32;
2230                       ctxp->osb_depth = -1;
2231                     }
2232                   /* If capacity overflown, reallocate a bigger chunk */
2233                   else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2234                     allocate = ctxp->osb_limit << 1;
2235
2236                   if (allocate)
2237                     {
2238                       allocate *= sizeof (int);
2239                       if (ctxp->osb_number)
2240                         ctxp->osb_number = xrealloc (ctxp->osb_number,
2241                                                      allocate);
2242                       else
2243                         ctxp->osb_number = xmalloc (allocate);
2244                     }
2245                   ctxp->osb_depth++;
2246                   CURRENT_OSB (ctxp) = 1;
2247                 }
2248 |       dims OSB_TK CSB_TK
2249                 { CURRENT_OSB (ctxp)++; }
2250 |       dims OSB_TK error
2251                 { yyerror ("']' expected"); RECOVER;}
2252 ;
2253
2254 field_access:
2255         primary DOT_TK identifier
2256                 { $$ = make_qualified_primary ($1, $3, $2.location); }
2257                 /*  FIXME - REWRITE TO:
2258                 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2259 |       SUPER_TK DOT_TK identifier
2260                 {
2261                   tree super_wfl = build_wfl_node (super_identifier_node);
2262                   SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2263                   $$ = make_qualified_name (super_wfl, $3, $2.location);
2264                 }
2265 |       SUPER_TK error
2266                 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2267 ;
2268
2269 method_invocation:
2270         name OP_TK CP_TK
2271                 { $$ = build_method_invocation ($1, NULL_TREE); }
2272 |       name OP_TK argument_list CP_TK
2273                 { $$ = build_method_invocation ($1, $3); }
2274 |       primary DOT_TK identifier OP_TK CP_TK
2275                 {
2276                   if (TREE_CODE ($1) == THIS_EXPR)
2277                     $$ = build_this_super_qualified_invocation
2278                       (1, $3, NULL_TREE, 0, $2.location);
2279                   else
2280                     {
2281                       tree invok = build_method_invocation ($3, NULL_TREE);
2282                       $$ = make_qualified_primary ($1, invok, $2.location);
2283                     }
2284                 }
2285 |       primary DOT_TK identifier OP_TK argument_list CP_TK
2286                 {
2287                   if (TREE_CODE ($1) == THIS_EXPR)
2288                     $$ = build_this_super_qualified_invocation
2289                       (1, $3, $5, 0, $2.location);
2290                   else
2291                     {
2292                       tree invok = build_method_invocation ($3, $5);
2293                       $$ = make_qualified_primary ($1, invok, $2.location);
2294                     }
2295                 }
2296 |       SUPER_TK DOT_TK identifier OP_TK CP_TK
2297                 {
2298                   $$ = build_this_super_qualified_invocation
2299                     (0, $3, NULL_TREE, $1.location, $2.location);
2300                 }
2301 |       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2302                 {
2303                   $$ = build_this_super_qualified_invocation
2304                     (0, $3, $5, $1.location, $2.location);
2305                 }
2306         /* Screws up thing. I let it here until I'm convinced it can
2307            be removed. FIXME
2308 |       primary DOT_TK error
2309                 {yyerror ("'(' expected"); DRECOVER(bad);} */
2310 |       SUPER_TK DOT_TK error CP_TK
2311                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2312 |       SUPER_TK DOT_TK error DOT_TK
2313                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2314 ;
2315
2316 array_access:
2317         name OSB_TK expression CSB_TK
2318                 { $$ = build_array_ref ($2.location, $1, $3); }
2319 |       primary_no_new_array OSB_TK expression CSB_TK
2320                 { $$ = build_array_ref ($2.location, $1, $3); }
2321 |       array_creation_initialized OSB_TK expression CSB_TK
2322                 { $$ = build_array_ref ($2.location, $1, $3); }
2323 |       name OSB_TK error
2324                 {
2325                   yyerror ("Missing term and ']' expected");
2326                   DRECOVER(array_access);
2327                 }
2328 |       name OSB_TK expression error
2329                 {
2330                   yyerror ("']' expected");
2331                   DRECOVER(array_access);
2332                 }
2333 |       primary_no_new_array OSB_TK error
2334                 {
2335                   yyerror ("Missing term and ']' expected");
2336                   DRECOVER(array_access);
2337                 }
2338 |       primary_no_new_array OSB_TK expression error
2339                 {
2340                   yyerror ("']' expected");
2341                   DRECOVER(array_access);
2342                 }
2343 |       array_creation_initialized OSB_TK error
2344                 {
2345                   yyerror ("Missing term and ']' expected");
2346                   DRECOVER(array_access);
2347                 }
2348 |       array_creation_initialized OSB_TK expression error
2349                 {
2350                   yyerror ("']' expected");
2351                   DRECOVER(array_access);
2352                 }
2353 ;
2354
2355 postfix_expression:
2356         primary
2357 |       name
2358 |       post_increment_expression
2359 |       post_decrement_expression
2360 ;
2361
2362 post_increment_expression:
2363         postfix_expression INCR_TK
2364                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2365 ;
2366
2367 post_decrement_expression:
2368         postfix_expression DECR_TK
2369                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2370 ;
2371
2372 trap_overflow_corner_case:
2373         pre_increment_expression
2374 |       pre_decrement_expression
2375 |       PLUS_TK unary_expression
2376                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2377 |       unary_expression_not_plus_minus
2378 |       PLUS_TK error
2379                 {yyerror ("Missing term"); RECOVER}
2380 ;
2381
2382 unary_expression:
2383         trap_overflow_corner_case
2384                 {
2385                   if ($1)
2386                     error_if_numeric_overflow ($1);
2387                   $$ = $1;
2388                 }
2389 |       MINUS_TK trap_overflow_corner_case
2390                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2391 |       MINUS_TK error
2392                 {yyerror ("Missing term"); RECOVER}
2393 ;
2394
2395 pre_increment_expression:
2396         INCR_TK unary_expression
2397                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2398 |       INCR_TK error
2399                 {yyerror ("Missing term"); RECOVER}
2400 ;
2401
2402 pre_decrement_expression:
2403         DECR_TK unary_expression
2404                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2405 |       DECR_TK error
2406                 {yyerror ("Missing term"); RECOVER}
2407 ;
2408
2409 unary_expression_not_plus_minus:
2410         postfix_expression
2411 |       NOT_TK unary_expression
2412                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2413 |       NEG_TK unary_expression
2414                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2415 |       cast_expression
2416 |       NOT_TK error
2417                 {yyerror ("Missing term"); RECOVER}
2418 |       NEG_TK error
2419                 {yyerror ("Missing term"); RECOVER}
2420 ;
2421
2422 cast_expression:                /* Error handling here is potentially weak */
2423         OP_TK primitive_type dims CP_TK unary_expression
2424                 {
2425                   tree type = $2;
2426                   int osb = pop_current_osb (ctxp);
2427                   while (osb--)
2428                     type = build_java_array_type (type, -1);
2429                   $$ = build_cast ($1.location, type, $5);
2430                 }
2431 |       OP_TK primitive_type CP_TK unary_expression
2432                 { $$ = build_cast ($1.location, $2, $4); }
2433 |       OP_TK expression CP_TK unary_expression_not_plus_minus
2434                 { $$ = build_cast ($1.location, $2, $4); }
2435 |       OP_TK name dims CP_TK unary_expression_not_plus_minus
2436                 {
2437                   const char *ptr;
2438                   int osb = pop_current_osb (ctxp);
2439                   obstack_grow (&temporary_obstack,
2440                                 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2441                                 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2442                   while (osb--)
2443                     obstack_grow (&temporary_obstack, "[]", 2);
2444                   obstack_1grow (&temporary_obstack, '\0');
2445                   ptr = obstack_finish (&temporary_obstack);
2446                   EXPR_WFL_NODE ($2) = get_identifier (ptr);
2447                   $$ = build_cast ($1.location, $2, $5);
2448                 }
2449 |       OP_TK primitive_type OSB_TK error
2450                 {yyerror ("']' expected, invalid type expression");}
2451 |       OP_TK error
2452                 {
2453                   YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2454                   RECOVER;
2455                 }
2456 |       OP_TK primitive_type dims CP_TK error
2457                 {yyerror ("Missing term"); RECOVER;}
2458 |       OP_TK primitive_type CP_TK error
2459                 {yyerror ("Missing term"); RECOVER;}
2460 |       OP_TK name dims CP_TK error
2461                 {yyerror ("Missing term"); RECOVER;}
2462 ;
2463
2464 multiplicative_expression:
2465         unary_expression
2466 |       multiplicative_expression MULT_TK unary_expression
2467                 {
2468                   $$ = build_binop (BINOP_LOOKUP ($2.token),
2469                                     $2.location, $1, $3);
2470                 }
2471 |       multiplicative_expression DIV_TK unary_expression
2472                 {
2473                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2474                                     $1, $3);
2475                 }
2476 |       multiplicative_expression REM_TK unary_expression
2477                 {
2478                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2479                                     $1, $3);
2480                 }
2481 |       multiplicative_expression MULT_TK error
2482                 {yyerror ("Missing term"); RECOVER;}
2483 |       multiplicative_expression DIV_TK error
2484                 {yyerror ("Missing term"); RECOVER;}
2485 |       multiplicative_expression REM_TK error
2486                 {yyerror ("Missing term"); RECOVER;}
2487 ;
2488
2489 additive_expression:
2490         multiplicative_expression
2491 |       additive_expression PLUS_TK multiplicative_expression
2492                 {
2493                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2494                                     $1, $3);
2495                 }
2496 |       additive_expression MINUS_TK multiplicative_expression
2497                 {
2498                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2499                                     $1, $3);
2500                 }
2501 |       additive_expression PLUS_TK error
2502                 {yyerror ("Missing term"); RECOVER;}
2503 |       additive_expression MINUS_TK error
2504                 {yyerror ("Missing term"); RECOVER;}
2505 ;
2506
2507 shift_expression:
2508         additive_expression
2509 |       shift_expression LS_TK additive_expression
2510                 {
2511                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2512                                     $1, $3);
2513                 }
2514 |       shift_expression SRS_TK additive_expression
2515                 {
2516                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2517                                     $1, $3);
2518                 }
2519 |       shift_expression ZRS_TK additive_expression
2520                 {
2521                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2522                                     $1, $3);
2523                 }
2524 |       shift_expression LS_TK error
2525                 {yyerror ("Missing term"); RECOVER;}
2526 |       shift_expression SRS_TK error
2527                 {yyerror ("Missing term"); RECOVER;}
2528 |       shift_expression ZRS_TK error
2529                 {yyerror ("Missing term"); RECOVER;}
2530 ;
2531
2532 relational_expression:
2533         shift_expression
2534 |       relational_expression LT_TK shift_expression
2535                 {
2536                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2537                                     $1, $3);
2538                 }
2539 |       relational_expression GT_TK shift_expression
2540                 {
2541                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2542                                     $1, $3);
2543                 }
2544 |       relational_expression LTE_TK shift_expression
2545                 {
2546                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2547                                     $1, $3);
2548                 }
2549 |       relational_expression GTE_TK shift_expression
2550                 {
2551                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2552                                     $1, $3);
2553                 }
2554 |       relational_expression INSTANCEOF_TK reference_type
2555                 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2556 |       relational_expression LT_TK error
2557                 {yyerror ("Missing term"); RECOVER;}
2558 |       relational_expression GT_TK error
2559                 {yyerror ("Missing term"); RECOVER;}
2560 |       relational_expression LTE_TK error
2561                 {yyerror ("Missing term"); RECOVER;}
2562 |       relational_expression GTE_TK error
2563                 {yyerror ("Missing term"); RECOVER;}
2564 |       relational_expression INSTANCEOF_TK error
2565                 {yyerror ("Invalid reference type"); RECOVER;}
2566 ;
2567
2568 equality_expression:
2569         relational_expression
2570 |       equality_expression EQ_TK relational_expression
2571                 {
2572                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2573                                     $1, $3);
2574                 }
2575 |       equality_expression NEQ_TK relational_expression
2576                 {
2577                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2578                                     $1, $3);
2579                 }
2580 |       equality_expression EQ_TK error
2581                 {yyerror ("Missing term"); RECOVER;}
2582 |       equality_expression NEQ_TK error
2583                 {yyerror ("Missing term"); RECOVER;}
2584 ;
2585
2586 and_expression:
2587         equality_expression
2588 |       and_expression AND_TK equality_expression
2589                 {
2590                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2591                                     $1, $3);
2592                 }
2593 |       and_expression AND_TK error
2594                 {yyerror ("Missing term"); RECOVER;}
2595 ;
2596
2597 exclusive_or_expression:
2598         and_expression
2599 |       exclusive_or_expression XOR_TK and_expression
2600                 {
2601                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2602                                     $1, $3);
2603                 }
2604 |       exclusive_or_expression XOR_TK error
2605                 {yyerror ("Missing term"); RECOVER;}
2606 ;
2607
2608 inclusive_or_expression:
2609         exclusive_or_expression
2610 |       inclusive_or_expression OR_TK exclusive_or_expression
2611                 {
2612                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2613                                     $1, $3);
2614                 }
2615 |       inclusive_or_expression OR_TK error
2616                 {yyerror ("Missing term"); RECOVER;}
2617 ;
2618
2619 conditional_and_expression:
2620         inclusive_or_expression
2621 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2622                 {
2623                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2624                                     $1, $3);
2625                 }
2626 |       conditional_and_expression BOOL_AND_TK error
2627                 {yyerror ("Missing term"); RECOVER;}
2628 ;
2629
2630 conditional_or_expression:
2631         conditional_and_expression
2632 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
2633                 {
2634                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2635                                     $1, $3);
2636                 }
2637 |       conditional_or_expression BOOL_OR_TK error
2638                 {yyerror ("Missing term"); RECOVER;}
2639 ;
2640
2641 conditional_expression:         /* Error handling here is weak */
2642         conditional_or_expression
2643 |       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2644                 {
2645                   $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2646                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2647                 }
2648 |       conditional_or_expression REL_QM_TK REL_CL_TK error
2649                 {
2650                   YYERROR_NOW;
2651                   yyerror ("Missing term");
2652                   DRECOVER (1);
2653                 }
2654 |       conditional_or_expression REL_QM_TK error
2655                 {yyerror ("Missing term"); DRECOVER (2);}
2656 |       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2657                 {yyerror ("Missing term"); DRECOVER (3);}
2658 ;
2659
2660 assignment_expression:
2661         conditional_expression
2662 |       assignment
2663 ;
2664
2665 assignment:
2666         left_hand_side assignment_operator assignment_expression
2667                 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2668 |       left_hand_side assignment_operator error
2669                 {
2670                   YYNOT_TWICE yyerror ("Missing term");
2671                   DRECOVER (assign);
2672                 }
2673 ;
2674
2675 left_hand_side:
2676         name
2677 |       field_access
2678 |       array_access
2679 ;
2680
2681 assignment_operator:
2682         ASSIGN_ANY_TK
2683 |       ASSIGN_TK
2684 ;
2685
2686 expression:
2687         assignment_expression
2688 ;
2689
2690 constant_expression:
2691         expression
2692 ;
2693
2694 %%
2695
2696 /* Helper function to retrieve an OSB count. Should be used when the
2697    `dims:' rule is being used.  */
2698
2699 static int
2700 pop_current_osb (struct parser_ctxt *ctxp)
2701 {
2702   int to_return;
2703
2704   if (ctxp->osb_depth < 0)
2705     abort ();
2706
2707   to_return = CURRENT_OSB (ctxp);
2708   ctxp->osb_depth--;
2709
2710   return to_return;
2711 }
2712
2713 \f
2714
2715 /* This section of the code deal with save/restoring parser contexts.
2716    Add mode documentation here. FIXME */
2717
2718 /* Helper function. Create a new parser context. With
2719    COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2720    context is copied, otherwise, the new context is zeroed. The newly
2721    created context becomes the current one.  */
2722
2723 static void
2724 create_new_parser_context (int copy_from_previous)
2725 {
2726   struct parser_ctxt *new;
2727
2728   new = ggc_alloc (sizeof (struct parser_ctxt));
2729   if (copy_from_previous)
2730     {
2731       memcpy (new, ctxp, sizeof (struct parser_ctxt));
2732       /* This flag, indicating the context saves global values,
2733          should only be set by java_parser_context_save_global.  */
2734       new->saved_data_ctx = 0;
2735     }
2736   else
2737     memset (new, 0, sizeof (struct parser_ctxt));
2738
2739   new->next = ctxp;
2740   ctxp = new;
2741 }
2742
2743 /* Create a new parser context and make it the current one. */
2744
2745 void
2746 java_push_parser_context (void)
2747 {
2748   create_new_parser_context (0);
2749 }
2750
2751 void
2752 java_pop_parser_context (int generate)
2753 {
2754   tree current;
2755   struct parser_ctxt *next;
2756
2757   if (!ctxp)
2758     return;
2759
2760   next = ctxp->next;
2761   if (next)
2762     {
2763       input_location = ctxp->save_location;
2764       current_class = ctxp->class_type;
2765     }
2766
2767   /* If the old and new lexers differ, then free the old one.  */
2768   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2769     java_destroy_lexer (ctxp->lexer);
2770
2771   /* Set the single import class file flag to 0 for the current list
2772      of imported things */
2773   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2774     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2775
2776   /* If we pushed a context to parse a class intended to be generated,
2777      we keep it so we can remember the class. What we could actually
2778      do is to just update a list of class names.  */
2779   if (generate)
2780     {
2781       ctxp->next = ctxp_for_generation;
2782       ctxp_for_generation = ctxp;
2783     }
2784
2785   /* And restore those of the previous context */
2786   if ((ctxp = next))            /* Assignment is really meant here */
2787     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2788       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2789 }
2790
2791 /* Create a parser context for the use of saving some global
2792    variables.  */
2793
2794 void
2795 java_parser_context_save_global (void)
2796 {
2797   if (!ctxp)
2798     {
2799       java_push_parser_context ();
2800       ctxp->saved_data_ctx = 1;
2801     }
2802
2803   /* If this context already stores data, create a new one suitable
2804      for data storage. */
2805   else if (ctxp->saved_data)
2806     {
2807       create_new_parser_context (1);
2808       ctxp->saved_data_ctx = 1;
2809     }
2810
2811   ctxp->save_location = input_location;
2812   ctxp->class_type = current_class;
2813   ctxp->function_decl = current_function_decl;
2814   ctxp->saved_data = 1;
2815 }
2816
2817 /* Restore some global variables from the previous context. Make the
2818    previous context the current one.  */
2819
2820 void
2821 java_parser_context_restore_global (void)
2822 {
2823   input_location = ctxp->save_location;
2824   current_class = ctxp->class_type;
2825   if (wfl_operator)
2826 #ifdef USE_MAPPED_LOCATION
2827     SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2828 #else
2829     EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2830 #endif
2831   current_function_decl = ctxp->function_decl;
2832   ctxp->saved_data = 0;
2833   if (ctxp->saved_data_ctx)
2834     java_pop_parser_context (0);
2835 }
2836
2837 /* Suspend vital data for the current class/function being parsed so
2838    that an other class can be parsed. Used to let local/anonymous
2839    classes be parsed.  */
2840
2841 static void
2842 java_parser_context_suspend (void)
2843 {
2844   /* This makes debugging through java_debug_context easier */
2845   static const char *const name = "<inner buffer context>";
2846
2847   /* Duplicate the previous context, use it to save the globals we're
2848      interested in */
2849   create_new_parser_context (1);
2850   ctxp->function_decl = current_function_decl;
2851   ctxp->class_type = current_class;
2852
2853   /* Then create a new context which inherits all data from the
2854      previous one. This will be the new current context  */
2855   create_new_parser_context (1);
2856
2857   /* Help debugging */
2858   ctxp->next->filename = name;
2859 }
2860
2861 /* Resume vital data for the current class/function being parsed so
2862    that an other class can be parsed. Used to let local/anonymous
2863    classes be parsed.  The trick is the data storing file position
2864    informations must be restored to their current value, so parsing
2865    can resume as if no context was ever saved. */
2866
2867 static void
2868 java_parser_context_resume (void)
2869 {
2870   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2871   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2872   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2873
2874   /* We need to inherit the list of classes to complete/generate */
2875   restored->classd_list = old->classd_list;
2876   restored->class_list = old->class_list;
2877
2878   /* Restore the current class and function from the saver */
2879   current_class = saver->class_type;
2880   current_function_decl = saver->function_decl;
2881
2882   /* Retrieve the restored context */
2883   ctxp = restored;
2884
2885   /* Re-installed the data for the parsing to carry on */
2886   memcpy (&ctxp->marker_begining, &old->marker_begining,
2887           (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2888 }
2889
2890 /* Add a new anchor node to which all statement(s) initializing static
2891    and non static initialized upon declaration field(s) will be
2892    linked.  */
2893
2894 static void
2895 java_parser_context_push_initialized_field (void)
2896 {
2897   tree node;
2898
2899   node = build_tree_list (NULL_TREE, NULL_TREE);
2900   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2901   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2902
2903   node = build_tree_list (NULL_TREE, NULL_TREE);
2904   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2905   CPC_INITIALIZER_LIST (ctxp) = node;
2906
2907   node = build_tree_list (NULL_TREE, NULL_TREE);
2908   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2909   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2910 }
2911
2912 /* Pop the lists of initialized field. If this lists aren't empty,
2913    remember them so we can use it to create and populate the finit$
2914    or <clinit> functions. */
2915
2916 static void
2917 java_parser_context_pop_initialized_field (void)
2918 {
2919   tree stmts;
2920   tree class_type = TREE_TYPE (GET_CPC ());
2921
2922   if (CPC_INITIALIZER_LIST (ctxp))
2923     {
2924       stmts = CPC_INITIALIZER_STMT (ctxp);
2925       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2926       if (stmts && !java_error_count)
2927         TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2928     }
2929
2930   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2931     {
2932       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2933       CPC_STATIC_INITIALIZER_LIST (ctxp) =
2934         TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2935       /* Keep initialization in order to enforce 8.5 */
2936       if (stmts && !java_error_count)
2937         TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2938     }
2939
2940   /* JDK 1.1 instance initializers */
2941   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2942     {
2943       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2944       CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2945         TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2946       if (stmts && !java_error_count)
2947         TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2948     }
2949 }
2950
2951 static tree
2952 reorder_static_initialized (tree list)
2953 {
2954   /* We have to keep things in order. The alias initializer have to
2955      come first, then the initialized regular field, in reverse to
2956      keep them in lexical order. */
2957   tree marker, previous = NULL_TREE;
2958   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2959     if (TREE_CODE (marker) == TREE_LIST
2960         && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2961       break;
2962
2963   /* No static initialized, the list is fine as is */
2964   if (!previous)
2965     list = TREE_CHAIN (marker);
2966
2967   /* No marker? reverse the whole list */
2968   else if (!marker)
2969     list = nreverse (list);
2970
2971   /* Otherwise, reverse what's after the marker and the new reordered
2972      sublist will replace the marker. */
2973   else
2974     {
2975       TREE_CHAIN (previous) = NULL_TREE;
2976       list = nreverse (list);
2977       list = chainon (TREE_CHAIN (marker), list);
2978     }
2979   return list;
2980 }
2981
2982 /* Helper functions to dump the parser context stack.  */
2983
2984 #define TAB_CONTEXT(C) \
2985   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2986
2987 static void
2988 java_debug_context_do (int tab)
2989 {
2990   struct parser_ctxt *copy = ctxp;
2991   while (copy)
2992     {
2993       TAB_CONTEXT (tab);
2994       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2995       TAB_CONTEXT (tab);
2996       fprintf (stderr, "filename: %s\n", copy->filename);
2997       TAB_CONTEXT (tab);
2998       fprintf (stderr, "package: %s\n",
2999                (copy->package ?
3000                 IDENTIFIER_POINTER (copy->package) : "<none>"));
3001       TAB_CONTEXT (tab);
3002       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
3003       TAB_CONTEXT (tab);
3004       fprintf (stderr, "saved data: %d\n", copy->saved_data);
3005       copy = copy->next;
3006       tab += 2;
3007     }
3008 }
3009
3010 /* Dump the stacked up parser contexts. Intended to be called from a
3011    debugger.  */
3012
3013 void
3014 java_debug_context (void)
3015 {
3016   java_debug_context_do (0);
3017 }
3018
3019 \f
3020
3021 /* Flag for the error report routine to issue the error the first time
3022    it's called (overriding the default behavior which is to drop the
3023    first invocation and honor the second one, taking advantage of a
3024    richer context.  */
3025 static int force_error = 0;
3026
3027 /* Reporting an constructor invocation error.  */
3028 static void
3029 parse_ctor_invocation_error (void)
3030 {
3031   if (DECL_CONSTRUCTOR_P (current_function_decl))
3032     yyerror ("Constructor invocation must be first thing in a constructor");
3033   else
3034     yyerror ("Only constructors can invoke constructors");
3035 }
3036
3037 /* Reporting JDK1.1 features not implemented.  */
3038
3039 static tree
3040 parse_jdk1_1_error (const char *msg)
3041 {
3042   sorry (": %qs JDK1.1(TM) feature", msg);
3043   java_error_count++;
3044   return build_java_empty_stmt ();
3045 }
3046
3047 static int do_warning = 0;
3048
3049 void
3050 yyerror (const char *msgid)
3051 {
3052 #ifdef USE_MAPPED_LOCATION
3053   static source_location elc;
3054   expanded_location xloc = expand_location (input_location);
3055   int current_line = xloc.line;
3056 #else
3057   static java_lc elc;
3058   int save_lineno;
3059   int current_line = input_line;
3060 #endif
3061   static int prev_lineno;
3062   static const char *prev_msg;
3063
3064   char *remainder, *code_from_source;
3065
3066   if (!force_error && prev_lineno == current_line)
3067     return;
3068 #ifndef USE_MAPPED_LOCATION
3069   current_line = ctxp->lexer->token_start.line;
3070 #endif
3071
3072   /* Save current error location but report latter, when the context is
3073      richer.  */
3074   if (ctxp->java_error_flag == 0)
3075     {
3076       ctxp->java_error_flag = 1;
3077 #ifdef USE_MAPPED_LOCATION
3078       elc = input_location;
3079 #else
3080       elc = ctxp->lexer->token_start;
3081 #endif
3082       /* Do something to use the previous line if we're reaching the
3083          end of the file... */
3084 #ifdef VERBOSE_SKELETON
3085       printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3086 #endif
3087       return;
3088     }
3089
3090   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3091   if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3092     return;
3093
3094   ctxp->java_error_flag = 0;
3095   if (do_warning)
3096     java_warning_count++;
3097   else
3098     java_error_count++;
3099
3100 #if 0 /* FIXME */
3101   if (elc.col == 0 && msgid && msgid[1] == ';')
3102     elc = ctxp->prev_line_end;
3103 #endif
3104
3105   prev_msg = msgid;
3106
3107 #ifdef USE_MAPPED_LOCATION
3108   prev_lineno = current_line;
3109   code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3110 #else
3111   save_lineno = input_line;
3112   prev_lineno = input_line = current_line;
3113   code_from_source = java_get_line_col (input_filename, current_line,
3114                                         ctxp->lexer->token_start.col);
3115 #endif
3116
3117
3118   obstack_grow0 (&temporary_obstack,
3119                  code_from_source, strlen (code_from_source));
3120   remainder = obstack_finish (&temporary_obstack);
3121   if (do_warning)
3122     warning ("%s.\n%s", msgid, remainder);
3123   else
3124     error ("%s.\n%s", msgid, remainder);
3125
3126   /* This allow us to cheaply avoid an extra 'Invalid expression
3127      statement' error report when errors have been already reported on
3128      the same line. This occurs when we report an error but don't have
3129      a synchronization point other than ';', which
3130      expression_statement is the only one to take care of.  */
3131 #ifndef USE_MAPPED_LOCATION
3132   input_line = save_lineno;
3133 #endif
3134   ctxp->prevent_ese = input_line;
3135 }
3136
3137 static void
3138 issue_warning_error_from_context (
3139 #ifdef USE_MAPPED_LOCATION
3140                                   source_location cl,
3141 #else
3142                                   tree cl,
3143 #endif
3144                                   const char *msgid, va_list *ap)
3145 {
3146 #ifdef USE_MAPPED_LOCATION
3147   source_location saved_location = input_location;
3148   expanded_location xloc = expand_location (cl);
3149 #else
3150   java_lc save_lc = ctxp->lexer->token_start;
3151   const char *saved = ctxp->filename, *saved_input_filename;
3152 #endif
3153   char buffer [4096];
3154   text_info text;
3155
3156   text.err_no = errno;
3157   text.args_ptr = ap;
3158   text.format_spec = msgid;
3159   pp_format_text (global_dc->printer, &text);
3160   strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3161   buffer[sizeof (buffer) - 1] = '\0';
3162   pp_clear_output_area (global_dc->printer);
3163
3164   force_error = 1;
3165
3166 #ifdef USE_MAPPED_LOCATION
3167   if (xloc.file != NULL)
3168     {
3169       ctxp->filename = xloc.file;
3170       input_location = cl;
3171     }
3172 #else
3173   ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3174   ctxp->lexer->token_start.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3175                                    : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3176                                    : EXPR_WFL_COLNO (cl));
3177
3178   /* We have a CL, that's a good reason for using it if it contains data */
3179   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3180     ctxp->filename = EXPR_WFL_FILENAME (cl);
3181   saved_input_filename = input_filename;
3182   input_filename = ctxp->filename;
3183 #endif
3184   java_error (NULL);
3185   java_error (buffer);
3186 #ifdef USE_MAPPED_LOCATION
3187   input_location = saved_location;
3188 #else
3189   ctxp->filename = saved;
3190   input_filename = saved_input_filename;
3191   ctxp->lexer->token_start = save_lc;
3192 #endif
3193   force_error = 0;
3194 }
3195
3196 /* Issue an error message at a current source line CL.
3197    FUTURE/FIXME:  change cl to be a source_location. */
3198
3199 void
3200 parse_error_context (tree cl, const char *msgid, ...)
3201 {
3202   va_list ap;
3203   va_start (ap, msgid);
3204 #ifdef USE_MAPPED_LOCATION
3205   issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3206 #else
3207   issue_warning_error_from_context (cl, msgid, &ap);
3208 #endif
3209   va_end (ap);
3210 }
3211
3212 /* Issue a warning at a current source line CL.
3213    FUTURE/FIXME:  change cl to be a source_location. */
3214
3215 static void
3216 parse_warning_context (tree cl, const char *msgid, ...)
3217 {
3218   va_list ap;
3219   va_start (ap, msgid);
3220
3221   do_warning = 1;
3222 #ifdef USE_MAPPED_LOCATION
3223   issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3224 #else
3225   issue_warning_error_from_context (cl, msgid, &ap);
3226 #endif
3227   do_warning = 0;
3228   va_end (ap);
3229 }
3230
3231 static tree
3232 find_expr_with_wfl (tree node)
3233 {
3234   while (node)
3235     {
3236       enum tree_code_class code;
3237       tree to_return;
3238
3239       switch (TREE_CODE (node))
3240         {
3241         case BLOCK:
3242           node = BLOCK_EXPR_BODY (node);
3243           continue;
3244
3245         case COMPOUND_EXPR:
3246           to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3247           if (to_return)
3248             return to_return;
3249           node = TREE_OPERAND (node, 1);
3250           continue;
3251
3252         case LOOP_EXPR:
3253           node = TREE_OPERAND (node, 0);
3254           continue;
3255
3256         case LABELED_BLOCK_EXPR:
3257           node = LABELED_BLOCK_BODY (node);
3258           continue;
3259
3260         default:
3261           code = TREE_CODE_CLASS (TREE_CODE (node));
3262           if (((code == tcc_unary) || (code == tcc_binary)
3263                || (code == tcc_expression))
3264               && EXPR_WFL_LINECOL (node))
3265             return node;
3266           return NULL_TREE;
3267         }
3268     }
3269   return NULL_TREE;
3270 }
3271
3272 /* Issue a missing return statement error. Uses METHOD to figure the
3273    last line of the method the error occurs in.  */
3274
3275 static void
3276 missing_return_error (tree method)
3277 {
3278 #ifdef USE_MAPPED_LOCATION
3279   SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3280 #else
3281   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3282 #endif
3283   parse_error_context (wfl_operator, "Missing return statement");
3284 }
3285
3286 /* Issue an unreachable statement error. From NODE, find the next
3287    statement to report appropriately.  */
3288 static void
3289 unreachable_stmt_error (tree node)
3290 {
3291   /* Browse node to find the next expression node that has a WFL. Use
3292      the location to report the error */
3293   if (TREE_CODE (node) == COMPOUND_EXPR)
3294     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3295   else
3296     node = find_expr_with_wfl (node);
3297
3298   if (node)
3299     {
3300 #ifdef USE_MAPPED_LOCATION
3301       SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3302 #else
3303       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3304 #endif
3305       parse_error_context (wfl_operator, "Unreachable statement");
3306     }
3307   else
3308     abort ();
3309 }
3310
3311 static int
3312 not_accessible_field_error (tree wfl, tree decl)
3313 {
3314   parse_error_context 
3315     (wfl, "Can't access %s field %<%s.%s%> from %qs",
3316      accessibility_string (get_access_flags_from_decl (decl)),
3317      GET_TYPE_NAME (DECL_CONTEXT (decl)),
3318      IDENTIFIER_POINTER (DECL_NAME (decl)),
3319      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3320   return 1;
3321 }
3322
3323 int
3324 java_report_errors (void)
3325 {
3326   if (java_error_count)
3327     fprintf (stderr, "%d error%s",
3328              java_error_count, (java_error_count == 1 ? "" : "s"));
3329   if (java_warning_count)
3330     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3331              java_warning_count, (java_warning_count == 1 ? "" : "s"));
3332   if (java_error_count || java_warning_count)
3333     putc ('\n', stderr);
3334   return java_error_count;
3335 }
3336
3337 static char *
3338 java_accstring_lookup (int flags)
3339 {
3340   static char buffer [80];
3341 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3342
3343   /* Access modifier looked-up first for easier report on forbidden
3344      access. */
3345   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3346   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3347   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3348   if (flags & ACC_STATIC) COPY_RETURN ("static");
3349   if (flags & ACC_FINAL) COPY_RETURN ("final");
3350   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3351   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3352   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3353   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3354   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3355   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3356
3357   buffer [0] = '\0';
3358   return buffer;
3359 #undef COPY_RETURN
3360 }
3361
3362 /* Returns a string denoting the accessibility of a class or a member as
3363    indicated by FLAGS.  We need a separate function from
3364    java_accstring_lookup, as the latter can return spurious "static", etc.
3365    if package-private access is defined (in which case none of the
3366    relevant access control bits in FLAGS is set).  */
3367
3368 static const char *
3369 accessibility_string (int flags)
3370 {
3371   if (flags & ACC_PRIVATE) return "private";
3372   if (flags & ACC_PROTECTED) return "protected";
3373   if (flags & ACC_PUBLIC) return "public";
3374
3375   return "package-private";
3376 }
3377
3378 /* Issuing error messages upon redefinition of classes, interfaces or
3379    variables. */
3380
3381 static void
3382 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3383 {
3384   parse_error_context (cl, "%s %qs already defined in %s:%d",
3385                        context, IDENTIFIER_POINTER (id),
3386                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3387   /* Here we should point out where its redefined. It's a unicode. FIXME */
3388 }
3389
3390 static void
3391 variable_redefinition_error (tree context, tree name, tree type, int line)
3392 {
3393   const char *type_name;
3394
3395   /* Figure a proper name for type. We might haven't resolved it */
3396   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3397     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3398   else
3399     type_name = lang_printable_name (type, 0);
3400
3401   parse_error_context (context,
3402                        "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3403                        IDENTIFIER_POINTER (name),
3404                        type_name, IDENTIFIER_POINTER (name), line);
3405 }
3406
3407 /* If ANAME is terminated with `[]', it indicates an array. This
3408    function returns the number of `[]' found and if this number is
3409    greater than zero, it extracts the array type name and places it in
3410    the node pointed to by TRIMMED unless TRIMMED is null.  */
3411
3412 static int
3413 build_type_name_from_array_name (tree aname, tree *trimmed)
3414 {
3415   const char *name = IDENTIFIER_POINTER (aname);
3416   int len = IDENTIFIER_LENGTH (aname);
3417   int array_dims;
3418
3419   STRING_STRIP_BRACKETS (name, len, array_dims);
3420
3421   if (array_dims && trimmed)
3422     *trimmed = get_identifier_with_length (name, len);
3423
3424   return array_dims;
3425 }
3426
3427 static tree
3428 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3429 {
3430   int more_dims = 0;
3431
3432   /* Eventually get more dims */
3433   more_dims = build_type_name_from_array_name (name, &name);
3434
3435   /* If we have, then craft a new type for this variable */
3436   if (more_dims)
3437     {
3438       tree save = type;
3439
3440       /* If we have a pointer, use its type */
3441       if (TREE_CODE (type) == POINTER_TYPE)
3442         type = TREE_TYPE (type);
3443
3444       /* Building the first dimension of a primitive type uses this
3445          function */
3446       if (JPRIMITIVE_TYPE_P (type))
3447         {
3448           type = build_java_array_type (type, -1);
3449           more_dims--;
3450         }
3451       /* Otherwise, if we have a WFL for this type, use it (the type
3452          is already an array on an unresolved type, and we just keep
3453          on adding dimensions) */
3454       else if (type_wfl)
3455         {
3456           type = type_wfl;
3457           more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3458                                                         NULL);
3459         }
3460
3461       /* Add all the dimensions */
3462       while (more_dims--)
3463         type = build_unresolved_array_type (type);
3464
3465       /* The type may have been incomplete in the first place */
3466       if (type_wfl)
3467         type = obtain_incomplete_type (type);
3468     }
3469
3470   if (ret_name)
3471     *ret_name = name;
3472   return type;
3473 }
3474
3475 /* Build something that the type identifier resolver will identify as
3476    being an array to an unresolved type. TYPE_WFL is a WFL on a
3477    identifier. */
3478
3479 static tree
3480 build_unresolved_array_type (tree type_or_wfl)
3481 {
3482   const char *ptr;
3483   tree wfl;
3484
3485   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3486      just create a array type */
3487   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3488     return build_java_array_type (type_or_wfl, -1);
3489
3490   obstack_grow (&temporary_obstack,
3491                  IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3492                  IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3493   obstack_grow0 (&temporary_obstack, "[]", 2);
3494   ptr = obstack_finish (&temporary_obstack);
3495 #ifdef USE_MAPPED_LOCATION
3496   wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3497 #else
3498   wfl = build_expr_wfl (get_identifier (ptr),
3499                         EXPR_WFL_FILENAME (type_or_wfl),
3500                         EXPR_WFL_LINENO (type_or_wfl),
3501                         EXPR_WFL_COLNO (type_or_wfl));
3502 #endif
3503   /* Re-install the existing qualifications so that the type can be
3504      resolved properly. */
3505   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3506   return wfl;
3507 }
3508
3509 static void
3510 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3511 {
3512   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3513     parse_error_context (wfl, "Interface %qs repeated",
3514                          IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3515 }
3516
3517 /* Bulk of common class/interface checks. Return 1 if an error was
3518    encountered. TAG is 0 for a class, 1 for an interface.  */
3519
3520 static int
3521 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3522                                 tree qualified_name, tree decl, tree cl)
3523 {
3524   tree node;
3525   int sca = 0;                  /* Static class allowed */
3526   int icaf = 0;                 /* Inner class allowed flags */
3527   int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3528
3529   if (!quiet_flag)
3530     fprintf (stderr, " %s%s %s",
3531              (CPC_INNER_P () ? "inner" : ""),
3532              (is_interface ? "interface" : "class"),
3533              IDENTIFIER_POINTER (qualified_name));
3534
3535   /* Scope of an interface/class type name:
3536        - Can't be imported by a single type import
3537        - Can't already exists in the package */
3538   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3539       && (node = find_name_in_single_imports (raw_name))
3540       && !CPC_INNER_P ())
3541     {
3542       parse_error_context
3543         (cl, "%s name %qs clashes with imported type %qs",
3544          (is_interface ? "Interface" : "Class"),
3545          IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3546       return 1;
3547     }
3548   if (decl && CLASS_COMPLETE_P (decl))
3549     {
3550       classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3551                                    qualified_name, decl, cl);
3552       return 1;
3553     }
3554
3555   if (check_inner_class_redefinition (raw_name, cl))
3556     return 1;
3557
3558   /* If public, file name should match class/interface name, except
3559      when dealing with an inner class */
3560   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3561     {
3562       const char *fname = input_filename;
3563       const char *f;
3564
3565       for (f = fname + strlen (fname);
3566            f != fname && ! IS_DIR_SEPARATOR (*f);
3567            f--)
3568         ;
3569       if (IS_DIR_SEPARATOR (*f))
3570         f++;
3571       if (strncmp (IDENTIFIER_POINTER (raw_name),
3572                    f , IDENTIFIER_LENGTH (raw_name)) ||
3573           f [IDENTIFIER_LENGTH (raw_name)] != '.')
3574         parse_error_context
3575           (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3576                              (is_interface ? "interface" : "class"),
3577                              IDENTIFIER_POINTER (qualified_name),
3578                              IDENTIFIER_POINTER (raw_name));
3579     }
3580
3581   /* Static classes can be declared only in top level classes. Note:
3582      once static, a inner class is a top level class. */
3583   if (flags & ACC_STATIC)
3584     {
3585       /* Catch the specific error of declaring an class inner class
3586          with no toplevel enclosing class. Prevent check_modifiers from
3587          complaining a second time */
3588       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3589         {
3590           parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3591                                IDENTIFIER_POINTER (qualified_name));
3592           sca = ACC_STATIC;
3593         }
3594       /* Else, in the context of a top-level class declaration, let
3595          `check_modifiers' do its job, otherwise, give it a go */
3596       else
3597         sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3598     }
3599
3600   /* Inner classes can be declared private or protected
3601      within their enclosing classes. */
3602   if (CPC_INNER_P ())
3603     {
3604       /* A class which is local to a block can't be public, private,
3605          protected or static. But it is created final, so allow this
3606          one. */
3607       if (current_function_decl)
3608         icaf = sca = uaaf = ACC_FINAL;
3609       else
3610         {
3611           check_modifiers_consistency (flags);
3612           icaf = ACC_PROTECTED;
3613           if (! CLASS_INTERFACE (GET_CPC ()))
3614             icaf |= ACC_PRIVATE;
3615         }
3616     }
3617
3618   if (is_interface)
3619     {
3620       if (CPC_INNER_P ())
3621         uaaf = INTERFACE_INNER_MODIFIERS;
3622       else
3623         uaaf = INTERFACE_MODIFIERS;
3624
3625       check_modifiers ("Illegal modifier %qs for interface declaration",
3626                        flags, uaaf);
3627     }
3628   else
3629     check_modifiers ((current_function_decl ?
3630                       "Illegal modifier %qs for local class declaration" :
3631                       "Illegal modifier %qs for class declaration"),
3632                      flags, uaaf|sca|icaf);
3633   return 0;
3634 }
3635
3636 /* Construct a nested class name.  If the final component starts with
3637    a digit, return true.  Otherwise return false.  */
3638 static int
3639 make_nested_class_name (tree cpc_list)
3640 {
3641   tree name;
3642
3643   if (!cpc_list)
3644     return 0;
3645
3646   make_nested_class_name (TREE_CHAIN (cpc_list));
3647
3648   /* Pick the qualified name when dealing with the first upmost
3649      enclosing class */
3650   name = (TREE_CHAIN (cpc_list)
3651           ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3652   obstack_grow (&temporary_obstack,
3653                 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3654   obstack_1grow (&temporary_obstack, '$');
3655
3656   return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3657 }
3658
3659 /* Can't redefine a class already defined in an earlier scope. */
3660
3661 static int
3662 check_inner_class_redefinition (tree raw_name, tree cl)
3663 {
3664   tree scope_list;
3665
3666   for (scope_list = GET_CPC_LIST (); scope_list;
3667        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3668     if (raw_name == GET_CPC_UN_NODE (scope_list))
3669       {
3670         parse_error_context
3671           (cl, "The class name %qs is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3672            IDENTIFIER_POINTER (raw_name));
3673         return 1;
3674       }
3675   return 0;
3676 }
3677
3678 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3679    we remember ENCLOSING and SUPER.  */
3680
3681 static tree
3682 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3683                      tree *super, tree class_type)
3684 {
3685   tree local_enclosing = *enclosing;
3686   tree local_super = NULL_TREE;
3687
3688   while (local_enclosing)
3689     {
3690       tree intermediate, decl;
3691
3692       *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3693         local_enclosing;
3694
3695       if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3696         return decl;
3697
3698       intermediate = local_enclosing;
3699       /* Explore enclosing contexts. */
3700       while (INNER_CLASS_DECL_P (intermediate))
3701         {
3702           intermediate = DECL_CONTEXT (intermediate);
3703           if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3704             return decl;
3705         }
3706
3707       /* Now go to the upper classes, bail out if necessary.  We will
3708          analyze the returned SUPER and act accordingly (see
3709          do_resolve_class).  */
3710       if (JPRIMITIVE_TYPE_P (TREE_TYPE (local_enclosing))
3711           || TREE_TYPE (local_enclosing) == void_type_node)
3712         {
3713           parse_error_context (cl, "Qualifier must be a reference");
3714           local_enclosing = NULL_TREE;
3715           break;
3716         }
3717       local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3718       if (!local_super || local_super == object_type_node)
3719         break;
3720
3721       if (TREE_CODE (local_super) == POINTER_TYPE)
3722         local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3723       else
3724         local_super = TYPE_NAME (local_super);
3725
3726       /* We may not have checked for circular inheritance yet, so do so
3727          here to prevent an infinite loop. */
3728       if (htab_find (circularity_hash, local_super) != NULL)
3729         {
3730           if (!cl)
3731             cl = lookup_cl (local_enclosing);
3732
3733           parse_error_context
3734             (cl, "Cyclic inheritance involving %s",
3735              IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3736           local_enclosing = NULL_TREE;
3737         }
3738       else
3739         local_enclosing = local_super;
3740     }
3741
3742   /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3743   *super = local_super;
3744   *enclosing = local_enclosing;
3745
3746   return NULL_TREE;
3747 }
3748
3749 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3750    qualified. */
3751
3752 static tree
3753 find_as_inner_class (tree enclosing, tree name, tree cl)
3754 {
3755   tree qual, to_return;
3756   if (!enclosing)
3757     return NULL_TREE;
3758
3759   name = TYPE_NAME (name);
3760
3761   /* First search: within the scope of `enclosing', search for name */
3762   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3763     qual = EXPR_WFL_QUALIFICATION (cl);
3764   else if (cl)
3765     qual = build_tree_list (cl, NULL_TREE);
3766   else
3767     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3768
3769   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3770     return to_return;
3771
3772   /* We're dealing with a qualified name. Try to resolve thing until
3773      we get something that is an enclosing class. */
3774   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3775     {
3776       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3777
3778       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3779            qual = TREE_CHAIN (qual))
3780         {
3781           acc = merge_qualified_name (acc,
3782                                       EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3783           BUILD_PTR_FROM_NAME (ptr, acc);
3784           decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3785         }
3786
3787       /* A NULL qual and a decl means that the search ended
3788          successfully?!? We have to do something then. FIXME */
3789
3790       if (decl)
3791         enclosing = decl;
3792       else
3793         qual = EXPR_WFL_QUALIFICATION (cl);
3794     }
3795   /* Otherwise, create a qual for the other part of the resolution. */
3796   else
3797     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3798
3799   return find_as_inner_class_do (qual, enclosing);
3800 }
3801
3802 /* We go inside the list of sub classes and try to find a way
3803    through. */
3804
3805 static tree
3806 find_as_inner_class_do (tree qual, tree enclosing)
3807 {
3808   if (!qual)
3809     return NULL_TREE;
3810
3811   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3812     {
3813       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3814       tree next_enclosing = NULL_TREE;
3815       tree inner_list;
3816
3817       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3818            inner_list; inner_list = TREE_CHAIN (inner_list))
3819         {
3820           if (TREE_VALUE (inner_list) == name_to_match)
3821             {
3822               next_enclosing = TREE_PURPOSE (inner_list);
3823               break;
3824             }
3825         }
3826       enclosing = next_enclosing;
3827     }
3828
3829   return (!qual && enclosing ? enclosing : NULL_TREE);
3830 }
3831
3832 static void
3833 link_nested_class_to_enclosing (void)
3834 {
3835   if (GET_ENCLOSING_CPC ())
3836     {
3837       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3838       DECL_INNER_CLASS_LIST (enclosing) =
3839         tree_cons (GET_CPC (), GET_CPC_UN (),
3840                    DECL_INNER_CLASS_LIST (enclosing));
3841     }
3842 }
3843
3844 static tree
3845 maybe_make_nested_class_name (tree name)
3846 {
3847   tree id = NULL_TREE;
3848
3849   if (CPC_INNER_P ())
3850     {
3851       /* If we're in a function, we must append a number to create the
3852          nested class name.  However, we don't do this if the class we
3853          are constructing is anonymous, because in that case we'll
3854          already have a number as the class name.  */
3855       if (! make_nested_class_name (GET_CPC_LIST ())
3856           && current_function_decl != NULL_TREE
3857           && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3858         {
3859           char buf[10];
3860           sprintf (buf, "%d", anonymous_class_counter);
3861           ++anonymous_class_counter;
3862           obstack_grow (&temporary_obstack, buf, strlen (buf));
3863           obstack_1grow (&temporary_obstack, '$');
3864         }
3865       obstack_grow0 (&temporary_obstack,
3866                      IDENTIFIER_POINTER (name),
3867                      IDENTIFIER_LENGTH (name));
3868       id = get_identifier (obstack_finish (&temporary_obstack));
3869       if (ctxp->package)
3870         QUALIFIED_P (id) = 1;
3871     }
3872   return id;
3873 }
3874
3875 /* If DECL is NULL, create and push a new DECL, record the current
3876    line CL and do other maintenance things.  */
3877
3878 static tree
3879 maybe_create_class_interface_decl (tree decl, tree raw_name,
3880                                    tree qualified_name, tree cl)
3881 {
3882   if (!decl)
3883     decl = push_class (make_class (), qualified_name);
3884
3885   /* Take care of the file and line business */
3886 #ifdef USE_MAPPED_LOCATION
3887   DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3888 #else
3889   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3890   /* If we're emitting xrefs, store the line/col number information */
3891   if (flag_emit_xref)
3892     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3893   else
3894     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3895 #endif
3896   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3897   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3898 #ifdef USE_MAPPED_LOCATION
3899   {
3900     tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3901     CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3902       tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3903   }
3904 #else
3905   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3906     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3907 #endif
3908
3909   PUSH_CPC (decl, raw_name);
3910   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3911
3912   /* Link the declaration to the already seen ones */
3913   TREE_CHAIN (decl) = ctxp->class_list;
3914   ctxp->class_list = decl;
3915
3916   /* Create a new nodes in the global lists */
3917   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3918   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3919
3920   /* Install a new dependency list element */
3921   create_jdep_list (ctxp);
3922
3923   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3924                           IDENTIFIER_POINTER (qualified_name)));
3925   return decl;
3926 }
3927
3928 static void
3929 add_superinterfaces (tree decl, tree interface_list)
3930 {
3931   tree node;
3932   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3933      takes care of ensuring that:
3934        - This is an accessible interface type,
3935        - Circularity detection.
3936    parser_add_interface is then called. If present but not defined,
3937    the check operation is delayed until the super interface gets
3938    defined.  */
3939   for (node = interface_list; node; node = TREE_CHAIN (node))
3940     {
3941       tree current = TREE_PURPOSE (node);
3942       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3943       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3944         {
3945           if (!parser_check_super_interface (idecl, decl, current))
3946             parser_add_interface (decl, idecl, current);
3947         }
3948       else
3949         register_incomplete_type (JDEP_INTERFACE,
3950                                   current, decl, NULL_TREE);
3951     }
3952 }
3953
3954 /* Create an interface in pass1 and return its decl. Return the
3955    interface's decl in pass 2.  */
3956
3957 static tree
3958 create_interface (int flags, tree id, tree super)
3959 {
3960   tree raw_name = EXPR_WFL_NODE (id);
3961   tree q_name = parser_qualified_classname (raw_name);
3962   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3963
3964   /* Certain syntax errors are making SUPER be like ID. Avoid this
3965      case. */
3966   if (ctxp->class_err && id == super)
3967     super = NULL;
3968
3969   EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3970
3971   /* Basic checks: scope, redefinition, modifiers */
3972   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3973     {
3974       PUSH_ERROR ();
3975       return NULL_TREE;
3976     }
3977
3978   /* Suspend the current parsing context if we're parsing an inner
3979      interface */
3980   if (CPC_INNER_P ())
3981     {
3982       java_parser_context_suspend ();
3983       /* Interface members are public. */
3984       if (CLASS_INTERFACE (GET_CPC ()))
3985         flags |= ACC_PUBLIC;
3986     }
3987
3988   /* Push a new context for (static) initialized upon declaration fields */
3989   java_parser_context_push_initialized_field ();
3990
3991   /* Interface modifiers check
3992        - public/abstract allowed (already done at that point)
3993        - abstract is obsolete (comes first, it's a warning, or should be)
3994        - Can't use twice the same (checked in the modifier rule) */
3995   if ((flags & ACC_ABSTRACT) && flag_redundant)
3996     parse_warning_context
3997       (MODIFIER_WFL (ABSTRACT_TK),
3998        "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3999
4000   /* Create a new decl if DECL is NULL, otherwise fix it */
4001   decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
4002
4003   /* Interfaces are always abstract. */
4004   flags |= ACC_ABSTRACT;
4005
4006   /* Inner interfaces are always static.  */
4007   if (INNER_CLASS_DECL_P (decl))
4008     flags |= ACC_STATIC;
4009
4010   /* Set super info and mark the class a complete */
4011   set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
4012                   object_type_node, ctxp->interface_number);
4013   ctxp->interface_number = 0;
4014   CLASS_COMPLETE_P (decl) = 1;
4015   add_superinterfaces (decl, super);
4016
4017   /* Eventually sets the @deprecated tag flag */
4018   CHECK_DEPRECATED (decl);
4019
4020   return decl;
4021 }
4022
4023 /* Patch anonymous class CLASS, by either extending or implementing
4024    DEP.  */
4025
4026 static void
4027 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4028 {
4029   tree class = TREE_TYPE (class_decl);
4030   tree type =  TREE_TYPE (type_decl);
4031   tree binfo = TYPE_BINFO (class);
4032
4033   /* If it's an interface, implement it */
4034   if (CLASS_INTERFACE (type_decl))
4035     {
4036       if (parser_check_super_interface (type_decl, class_decl, wfl))
4037         return;
4038
4039       if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4040         {
4041            /* Extend the binfo - by reallocating and copying it. */
4042           tree new_binfo;
4043           tree base_binfo;
4044           int i;
4045           
4046           new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4047           for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4048             BINFO_BASE_APPEND (new_binfo, base_binfo);
4049           CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4050           BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4051           TYPE_BINFO (class) = new_binfo;
4052         }
4053       
4054       /* And add the interface */
4055       parser_add_interface (class_decl, type_decl, wfl);
4056     }
4057   /* Otherwise, it's a type we want to extend */
4058   else
4059     {
4060       if (parser_check_super (type_decl, class_decl, wfl))
4061         return;
4062       BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4063     }
4064 }
4065
4066 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4067    its decl.  */
4068
4069 static tree
4070 create_anonymous_class (tree type_name)
4071 {
4072   char buffer [80];
4073   tree super = NULL_TREE, itf = NULL_TREE;
4074   tree id, type_decl, class;
4075
4076   /* The unqualified name of the anonymous class. It's just a number. */
4077   sprintf (buffer, "%d", anonymous_class_counter++);
4078   id = build_wfl_node (get_identifier (buffer));
4079   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4080
4081   /* We know about the type to extend/implement. We go ahead */
4082   if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4083     {
4084       /* Create a class which either implements on extends the designated
4085          class. The class bears an inaccessible name. */
4086       if (CLASS_INTERFACE (type_decl))
4087         {
4088           /* It's OK to modify it here. It's been already used and
4089              shouldn't be reused */
4090           ctxp->interface_number = 1;
4091           /* Interfaces should presented as a list of WFLs */
4092           itf = build_tree_list (type_name, NULL_TREE);
4093         }
4094       else
4095         super = type_name;
4096     }
4097
4098   class = create_class (ACC_FINAL, id, super, itf);
4099
4100   /* We didn't know anything about the stuff. We register a dependence. */
4101   if (!type_decl)
4102     register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4103
4104   ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4105   return class;
4106 }
4107
4108 /* Create a class in pass1 and return its decl. Return class
4109    interface's decl in pass 2.  */
4110
4111 static tree
4112 create_class (int flags, tree id, tree super, tree interfaces)
4113 {
4114   tree raw_name = EXPR_WFL_NODE (id);
4115   tree class_id, decl;
4116   tree super_decl_type;
4117
4118   /* Certain syntax errors are making SUPER be like ID. Avoid this
4119      case. */
4120   if (ctxp->class_err && id == super)
4121     super = NULL;
4122
4123   class_id = parser_qualified_classname (raw_name);
4124   decl = IDENTIFIER_CLASS_VALUE (class_id);
4125   EXPR_WFL_NODE (id) = class_id;
4126
4127   /* Basic check: scope, redefinition, modifiers */
4128   if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4129     {
4130       PUSH_ERROR ();
4131       return NULL_TREE;
4132     }
4133
4134   /* Suspend the current parsing context if we're parsing an inner
4135      class or an anonymous class. */
4136   if (CPC_INNER_P ())
4137     {
4138       java_parser_context_suspend ();
4139       /* Interface members are public. */
4140       if (CLASS_INTERFACE (GET_CPC ()))
4141         flags |= ACC_PUBLIC;
4142     }
4143
4144   /* Push a new context for (static) initialized upon declaration fields */
4145   java_parser_context_push_initialized_field ();
4146
4147   /* Class modifier check:
4148        - Allowed modifier (already done at that point)
4149        - abstract AND final forbidden
4150        - Public classes defined in the correct file */
4151   if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4152     parse_error_context
4153       (id, "Class %qs can't be declared both abstract and final",
4154        IDENTIFIER_POINTER (raw_name));
4155
4156   /* Create a new decl if DECL is NULL, otherwise fix it */
4157   decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4158
4159   /* If SUPER exists, use it, otherwise use Object */
4160   if (super)
4161     {
4162       /* java.lang.Object can't extend anything.  */
4163       if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4164         {
4165           parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4166           return NULL_TREE;
4167         }
4168
4169       super_decl_type =
4170         register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4171     }
4172   else if (TREE_TYPE (decl) != object_type_node)
4173     super_decl_type = object_type_node;
4174   /* We're defining java.lang.Object */
4175   else
4176     super_decl_type = NULL_TREE;
4177
4178   /* A class nested in an interface is implicitly static. */
4179   if (INNER_CLASS_DECL_P (decl)
4180       && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4181     {
4182       flags |= ACC_STATIC;
4183     }
4184
4185   /* Set super info and mark the class as complete. */
4186   set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4187                   ctxp->interface_number);
4188   ctxp->interface_number = 0;
4189   CLASS_COMPLETE_P (decl) = 1;
4190   add_superinterfaces (decl, interfaces);
4191
4192   /* TYPE_VFIELD' is a compiler-generated field used to point to
4193      virtual function tables.  In gcj, every class has a common base
4194      virtual function table in java.lang.object.  */
4195   TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4196
4197   /* Add the private this$<n> field, Replicate final locals still in
4198      scope as private final fields mangled like val$<local_name>.
4199      This does not occur for top level (static) inner classes. */
4200   if (PURE_INNER_CLASS_DECL_P (decl))
4201     add_inner_class_fields (decl, current_function_decl);
4202
4203   /* If doing xref, store the location at which the inherited class
4204      (if any) was seen. */
4205   if (flag_emit_xref && super)
4206     DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4207
4208   /* Eventually sets the @deprecated tag flag */
4209   CHECK_DEPRECATED (decl);
4210
4211   /* Reset the anonymous class counter when declaring non inner classes */
4212   if (!INNER_CLASS_DECL_P (decl))
4213     anonymous_class_counter = 1;
4214
4215   return decl;
4216 }
4217
4218 /* End a class declaration: register the statements used to create
4219    finit$ and <clinit>, pop the current class and resume the prior
4220    parser context if necessary.  */
4221
4222 static void
4223 end_class_declaration (int resume)
4224 {
4225   /* If an error occurred, context weren't pushed and won't need to be
4226      popped by a resume. */
4227   int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4228
4229   if (GET_CPC () != error_mark_node)
4230     dump_java_tree (TDI_class, GET_CPC ());
4231
4232   java_parser_context_pop_initialized_field ();
4233   POP_CPC ();
4234   if (resume && no_error_occurred)
4235     java_parser_context_resume ();
4236
4237   /* We're ending a class declaration, this is a good time to reset
4238      the interface cout. Note that might have been already done in
4239      create_interface, but if at that time an inner class was being
4240      dealt with, the interface count was reset in a context created
4241      for the sake of handling inner classes declaration. */
4242   ctxp->interface_number = 0;
4243 }
4244
4245 static void
4246 add_inner_class_fields (tree class_decl, tree fct_decl)
4247 {
4248   tree block, marker, f;
4249
4250   f = add_field (TREE_TYPE (class_decl),
4251                  build_current_thisn (TREE_TYPE (class_decl)),
4252                  build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4253                  ACC_PRIVATE);
4254   FIELD_THISN (f) = 1;
4255
4256   if (!fct_decl)
4257     return;
4258
4259   for (block = GET_CURRENT_BLOCK (fct_decl);
4260        block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4261     {
4262       tree decl;
4263       for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4264         {
4265           tree name, pname;
4266           tree wfl, init, list;
4267
4268           /* Avoid non final arguments. */
4269           if (!LOCAL_FINAL_P (decl))
4270             continue;
4271
4272           MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4273           MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4274           wfl = build_wfl_node (name);
4275           init = build_wfl_node (pname);
4276           /* Build an initialization for the field: it will be
4277              initialized by a parameter added to finit$, bearing a
4278              mangled name of the field itself (param$<n>.) The
4279              parameter is provided to finit$ by the constructor
4280              invoking it (hence the constructor will also feature a
4281              hidden parameter, set to the value of the outer context
4282              local at the time the inner class is created.)
4283
4284              Note: we take into account all possible locals that can
4285              be accessed by the inner class. It's actually not trivial
4286              to minimize these aliases down to the ones really
4287              used. One way to do that would be to expand all regular
4288              methods first, then finit$ to get a picture of what's
4289              used.  It works with the exception that we would have to
4290              go back on all constructor invoked in regular methods to
4291              have their invocation reworked (to include the right amount
4292              of alias initializer parameters.)
4293
4294              The only real way around, I think, is a first pass to
4295              identify locals really used in the inner class. We leave
4296              the flag FIELD_LOCAL_ALIAS_USED around for that future
4297              use.
4298
4299              On the other hand, it only affect local inner classes,
4300              whose constructors (and finit$ call) will be featuring
4301              unnecessary arguments. It's easy for a developer to keep
4302              this number of parameter down by using the `final'
4303              keyword only when necessary. For the time being, we can
4304              issue a warning on unnecessary finals. FIXME */
4305           init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4306                                    wfl, init);
4307
4308           /* Register the field. The TREE_LIST holding the part
4309              initialized/initializer will be marked ARG_FINAL_P so
4310              that the created field can be marked
4311              FIELD_LOCAL_ALIAS. */
4312           list = build_tree_list (wfl, init);
4313           ARG_FINAL_P (list) = 1;
4314           register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4315         }
4316     }
4317
4318   if (!CPC_INITIALIZER_STMT (ctxp))
4319     return;
4320
4321   /* If we ever registered an alias field, insert and marker to
4322      remember where the list ends. The second part of the list (the one
4323      featuring initialized fields) so it can be later reversed to
4324      enforce 8.5. The marker will be removed during that operation. */
4325   marker = build_tree_list (NULL_TREE, NULL_TREE);
4326   TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4327   SET_CPC_INITIALIZER_STMT (ctxp, marker);
4328 }
4329
4330 /* Can't use lookup_field () since we don't want to load the class and
4331    can't set the CLASS_LOADED_P flag */
4332
4333 static tree
4334 find_field (tree class, tree name)
4335 {
4336   tree decl;
4337   for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4338     {
4339       if (DECL_NAME (decl) == name)
4340         return decl;
4341     }
4342   return NULL_TREE;
4343 }
4344
4345 /* Wrap around lookup_field that doesn't potentially upset the value
4346    of CLASS */
4347
4348 static tree
4349 lookup_field_wrapper (tree class, tree name)
4350 {
4351   tree type = class;
4352   tree decl = NULL_TREE;
4353   java_parser_context_save_global ();
4354
4355   /* Last chance: if we're within the context of an inner class, we
4356      might be trying to access a local variable defined in an outer
4357      context. We try to look for it now. */
4358   if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4359     {
4360       tree new_name;
4361       MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4362       decl = lookup_field (&type, new_name);
4363       if (decl && decl != error_mark_node)
4364         FIELD_LOCAL_ALIAS_USED (decl) = 1;
4365     }
4366   if (!decl || decl == error_mark_node)
4367     {
4368       type = class;
4369       decl = lookup_field (&type, name);
4370     }
4371
4372   /* If the field still hasn't been found, try the next enclosing context. */
4373   if (!decl && INNER_CLASS_TYPE_P (class))
4374     {
4375       tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4376       decl = lookup_field_wrapper (outer_type, name);
4377     }
4378
4379   java_parser_context_restore_global ();
4380   return decl == error_mark_node ? NULL : decl;
4381 }
4382
4383 /* Find duplicate field within the same class declarations and report
4384    the error. Returns 1 if a duplicated field was found, 0
4385    otherwise.  */
4386
4387 static int
4388 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4389 {
4390   /* This might be modified to work with method decl as well */
4391   tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4392   if (decl)
4393     {
4394       char *t1 = xstrdup (purify_type_name
4395                          ((TREE_CODE (new_type) == POINTER_TYPE
4396                            && TREE_TYPE (new_type) == NULL_TREE) ?
4397                           IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4398                           lang_printable_name (new_type, 1)));
4399       /* The type may not have been completed by the time we report
4400          the error */
4401       char *t2 = xstrdup (purify_type_name
4402                          ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4403                            && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4404                           IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4405                           lang_printable_name (TREE_TYPE (decl), 1)));
4406       parse_error_context
4407         (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4408          t1, IDENTIFIER_POINTER (new_field_name),
4409          t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4410          DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4411       free (t1);
4412       free (t2);
4413       return 1;
4414     }
4415   return 0;
4416 }
4417
4418 /* Field registration routine. If TYPE doesn't exist, field
4419    declarations are linked to the undefined TYPE dependency list, to
4420    be later resolved in java_complete_class () */
4421
4422 static void
4423 register_fields (int flags, tree type, tree variable_list)
4424 {
4425   tree current, saved_type;
4426   tree class_type = NULL_TREE;
4427   location_t saved_location = input_location;
4428   int must_chain = 0;
4429   tree wfl = NULL_TREE;
4430
4431   if (GET_CPC ())
4432     class_type = TREE_TYPE (GET_CPC ());
4433
4434   if (!class_type || class_type == error_mark_node)
4435     return;
4436
4437   /* If we're adding fields to interfaces, those fields are public,
4438      static, final */
4439   if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4440     {
4441       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4442                                  flags, ACC_PUBLIC, "interface field(s)");
4443       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4444                                  flags, ACC_STATIC, "interface field(s)");
4445       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4446                                  flags, ACC_FINAL, "interface field(s)");
4447       check_modifiers ("Illegal interface member modifier %qs", flags,
4448                        INTERFACE_FIELD_MODIFIERS);
4449       flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4450     }
4451
4452   /* Obtain a suitable type for resolution, if necessary */
4453   SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4454
4455   /* If TYPE is fully resolved and we don't have a reference, make one */
4456   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4457
4458   for (current = variable_list, saved_type = type; current;
4459        current = TREE_CHAIN (current), type = saved_type)
4460     {
4461       tree real_type;
4462       tree field_decl;
4463       tree cl = TREE_PURPOSE (current);
4464       tree init = TREE_VALUE (current);
4465       tree current_name = EXPR_WFL_NODE (cl);
4466
4467       /* Can't declare non-final static fields in inner classes */
4468       if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4469           && !(flags & ACC_FINAL))
4470         parse_error_context
4471           (cl, "Field %qs can't be static in inner class %qs unless it is final",
4472            IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4473            lang_printable_name (class_type, 0));
4474
4475       /* Process NAME, as it may specify extra dimension(s) for it */
4476       type = build_array_from_name (type, wfl, current_name, &current_name);
4477
4478       /* Type adjustment. We may have just readjusted TYPE because
4479          the variable specified more dimensions. Make sure we have
4480          a reference if we can and don't have one already. Also
4481          change the name if we have an init. */
4482       if (type != saved_type)
4483         {
4484           PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4485           if (init)
4486             EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4487         }
4488
4489       real_type = GET_REAL_TYPE (type);
4490       /* Check for redeclarations */
4491       if (duplicate_declaration_error_p (current_name, real_type, cl))
4492         continue;
4493
4494       /* Set input_line to the line the field was found and create a
4495          declaration for it. Eventually sets the @deprecated tag flag. */
4496 #ifdef USE_MAPPED_LOCATION
4497       input_location = EXPR_LOCATION (cl);
4498 #else
4499       if (flag_emit_xref)
4500         input_line = EXPR_WFL_LINECOL (cl);
4501       else
4502         input_line = EXPR_WFL_LINENO (cl);
4503 #endif
4504       field_decl = add_field (class_type, current_name, real_type, flags);
4505       CHECK_DEPRECATED_NO_RESET (field_decl);
4506
4507       /* If the field denotes a final instance variable, then we
4508          allocate a LANG_DECL_SPECIFIC part to keep track of its
4509          initialization. We also mark whether the field was
4510          initialized upon its declaration. We don't do that if the
4511          created field is an alias to a final local. */
4512       if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4513         {
4514           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4515           DECL_FIELD_FINAL_WFL (field_decl) = cl;
4516         }
4517
4518       /* If the couple initializer/initialized is marked ARG_FINAL_P,
4519          we mark the created field FIELD_LOCAL_ALIAS, so that we can
4520          hide parameters to this inner class finit$ and
4521          constructors. It also means that the field isn't final per
4522          say. */
4523       if (ARG_FINAL_P (current))
4524         {
4525           FIELD_LOCAL_ALIAS (field_decl) = 1;
4526           FIELD_FINAL (field_decl) = 0;
4527         }
4528
4529       /* Check if we must chain. */
4530       if (must_chain)
4531         register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4532
4533       /* If we have an initialization value tied to the field */
4534       if (init)
4535         {
4536           /* The field is declared static */
4537           if (flags & ACC_STATIC)
4538             {
4539               /* We include the field and its initialization part into
4540                  a list used to generate <clinit>. After <clinit> is
4541                  walked, field initializations will be processed and
4542                  fields initialized with known constants will be taken
4543                  out of <clinit> and have their DECL_INITIAL set
4544                  appropriately. */
4545               TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4546               SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4547               if (TREE_OPERAND (init, 1)
4548                   && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4549                 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4550             }
4551           /* A non-static field declared with an immediate initialization is
4552              to be initialized in <init>, if any.  This field is remembered
4553              to be processed at the time of the generation of <init>. */
4554           else
4555             {
4556               TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4557               SET_CPC_INITIALIZER_STMT (ctxp, init);
4558             }
4559           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4560           DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4561         }
4562     }
4563
4564   CLEAR_DEPRECATED;
4565   input_location = saved_location;
4566 }
4567
4568 /* Generate finit$, using the list of initialized fields to populate
4569    its body. finit$'s parameter(s) list is adjusted to include the
4570    one(s) used to initialized the field(s) caching outer context
4571    local(s).  */
4572
4573 static tree
4574 generate_finit (tree class_type)
4575 {
4576   int count = 0;
4577   tree list = TYPE_FINIT_STMT_LIST (class_type);
4578   tree mdecl, current, parms;
4579
4580   parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4581                                                   class_type, NULL_TREE,
4582                                                   &count);
4583   CRAFTED_PARAM_LIST_FIXUP (parms);
4584   mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4585                                     finit_identifier_node, parms);
4586   fix_method_argument_names (parms, mdecl);
4587   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4588                        mdecl, NULL_TREE);
4589   DECL_FUNCTION_NAP (mdecl) = count;
4590   start_artificial_method_body (mdecl);
4591
4592   for (current = list; current; current = TREE_CHAIN (current))
4593     java_method_add_stmt (mdecl,
4594                           build_debugable_stmt (EXPR_WFL_LINECOL (current),
4595                                                 current));
4596   end_artificial_method_body (mdecl);
4597   return mdecl;
4598 }
4599
4600 /* Generate a function to run the instance initialization code. The
4601    private method is called `instinit$'. Unless we're dealing with an
4602    anonymous class, we determine whether all ctors of CLASS_TYPE
4603    declare a checked exception in their `throws' clause in order to
4604    see whether it's necessary to encapsulate the instance initializer
4605    statements in a try/catch/rethrow sequence.  */
4606
4607 static tree
4608 generate_instinit (tree class_type)
4609 {
4610   tree current;
4611   tree compound = NULL_TREE;
4612   tree parms = tree_cons (this_identifier_node,
4613                           build_pointer_type (class_type), end_params_node);
4614   tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4615                                          void_type_node,
4616                                          instinit_identifier_node, parms);
4617
4618   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4619                        mdecl, NULL_TREE);
4620
4621   /* Gather all the statements in a compound */
4622   for (current = TYPE_II_STMT_LIST (class_type);
4623        current; current = TREE_CHAIN (current))
4624     compound = add_stmt_to_compound (compound, NULL_TREE, current);
4625
4626   /* We need to encapsulate COMPOUND by a try/catch statement to
4627      rethrow exceptions that might occur in the instance initializer.
4628      We do that only if all ctors of CLASS_TYPE are set to catch a
4629      checked exception. This doesn't apply to anonymous classes (since
4630      they don't have declared ctors.) */
4631   if (!ANONYMOUS_CLASS_P (class_type) &&
4632       ctors_unchecked_throws_clause_p (class_type))
4633     {
4634       compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4635                                              build1 (THROW_EXPR, NULL_TREE,
4636                                                      build_wfl_node (wpv_id)));
4637       DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4638                                                       exception_type_node);
4639     }
4640
4641   start_artificial_method_body (mdecl);
4642   java_method_add_stmt (mdecl, compound);
4643   end_artificial_method_body (mdecl);
4644
4645   return mdecl;
4646 }
4647
4648 /* FIXME */
4649 static tree
4650 build_instinit_invocation (tree class_type)
4651 {
4652   tree to_return = NULL_TREE;
4653
4654   if (TYPE_II_STMT_LIST (class_type))
4655     {
4656       tree parm = build_tree_list (NULL_TREE,
4657                                    build_wfl_node (this_identifier_node));
4658       to_return =
4659         build_method_invocation (build_wfl_node (instinit_identifier_node),
4660                                  parm);
4661     }
4662   return to_return;
4663 }
4664
4665 /* Shared across method_declarator and method_header to remember the
4666    patch stage that was reached during the declaration of the method.
4667    A method DECL is built differently is there is no patch
4668    (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4669    pending on the currently defined method.  */
4670
4671 static int patch_stage;
4672
4673 /* Check the method declaration and add the method to its current
4674    class.  If the argument list is known to contain incomplete types,
4675    the method is partially added and the registration will be resume
4676    once the method arguments resolved. If TYPE is NULL, we're dealing
4677    with a constructor.  */
4678
4679 static tree
4680 method_header (int flags, tree type, tree mdecl, tree throws)
4681 {
4682   tree type_wfl = NULL_TREE;
4683   tree meth_name = NULL_TREE;
4684   tree current, orig_arg, this_class = NULL;
4685   tree id, meth;
4686   location_t saved_location;
4687   int constructor_ok = 0, must_chain;
4688   int count;
4689
4690   if (mdecl == error_mark_node)
4691     return error_mark_node;
4692   meth = TREE_VALUE (mdecl);
4693   id = TREE_PURPOSE (mdecl);
4694
4695   check_modifiers_consistency (flags);
4696
4697   if (GET_CPC ())
4698     this_class = TREE_TYPE (GET_CPC ());
4699
4700   if (!this_class || this_class == error_mark_node)
4701     return NULL_TREE;
4702
4703   /* There are some forbidden modifiers for an abstract method and its
4704      class must be abstract as well.  */
4705   if (type && (flags & ACC_ABSTRACT))
4706     {
4707       ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4708       ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4709       ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4710       ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4711       ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4712       ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4713       if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4714           && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4715         parse_error_context
4716           (id,
4717            "Class %qs must be declared abstract to define abstract method %qs",
4718            IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4719            IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4720     }
4721
4722   /* A native method can't be strictfp.  */
4723   if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4724     parse_error_context (id, "native method %qs can't be strictfp",
4725                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4726   /* No such thing as a transient or volatile method.  */
4727   if ((flags & ACC_TRANSIENT))
4728     parse_error_context (id, "method %qs can't be transient",
4729                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4730   if ((flags & ACC_VOLATILE))
4731     parse_error_context (id, "method %qs can't be volatile",
4732                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4733
4734   /* Things to be checked when declaring a constructor */
4735   if (!type)
4736     {
4737       int ec = java_error_count;
4738       /* 8.6: Constructor declarations: we might be trying to define a
4739          method without specifying a return type. */
4740       if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4741         parse_error_context
4742           (id, "Invalid method declaration, return type required");
4743       /* 8.6.3: Constructor modifiers */
4744       else
4745         {
4746           JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4747           JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4748           JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4749           JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4750           JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4751           JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4752         }
4753       /* If we found error here, we don't consider it's OK to tread
4754          the method definition as a constructor, for the rest of this
4755          function */
4756       if (ec == java_error_count)
4757         constructor_ok = 1;
4758     }
4759
4760   /* Method declared within the scope of an interface are implicitly
4761      abstract and public. Conflicts with other erroneously provided
4762      modifiers are checked right after. */
4763
4764   if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4765     {
4766       /* If FLAGS isn't set because of a modifier, turn the
4767          corresponding modifier WFL to NULL so we issue a warning on
4768          the obsolete use of the modifier */
4769       if (!(flags & ACC_PUBLIC))
4770         MODIFIER_WFL (PUBLIC_TK) = NULL;
4771       if (!(flags & ACC_ABSTRACT))
4772         MODIFIER_WFL (ABSTRACT_TK) = NULL;
4773       flags |= ACC_PUBLIC;
4774       flags |= ACC_ABSTRACT;
4775     }
4776
4777   /* Inner class can't declare static methods */
4778   if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4779     {
4780       parse_error_context
4781         (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4782          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4783          lang_printable_name (this_class, 0));
4784     }
4785
4786   /* Modifiers context reset moved up, so abstract method declaration
4787      modifiers can be later checked.  */
4788
4789   /* Set constructor returned type to void and method name to <init>,
4790      unless we found an error identifier the constructor (in which
4791      case we retain the original name) */
4792   if (!type)
4793     {
4794       type = void_type_node;
4795       if (constructor_ok)
4796         meth_name = init_identifier_node;
4797     }
4798   else
4799     meth_name = EXPR_WFL_NODE (id);
4800
4801   /* Do the returned type resolution and registration if necessary */
4802   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4803
4804   if (meth_name)
4805     type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4806   EXPR_WFL_NODE (id) = meth_name;
4807   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4808
4809   if (must_chain)
4810     {
4811       patch_stage = JDEP_METHOD_RETURN;
4812       register_incomplete_type (patch_stage, type_wfl, id, type);
4813       TREE_TYPE (meth) = GET_REAL_TYPE (type);
4814     }
4815   else
4816     TREE_TYPE (meth) = type;
4817
4818   saved_location = input_location;
4819   /* When defining an abstract or interface method, the curly
4820      bracket at level 1 doesn't exist because there is no function
4821      body */
4822 #ifdef USE_MAPPED_LOCATION
4823   input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4824                     EXPR_LOCATION (id));
4825 #else
4826   input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4827                 EXPR_WFL_LINENO (id));
4828 #endif
4829
4830   /* Remember the original argument list */
4831   orig_arg = TYPE_ARG_TYPES (meth);
4832
4833   if (patch_stage)              /* includes ret type and/or all args */
4834     {
4835       jdep *jdep;
4836       meth = add_method_1 (this_class, flags, meth_name, meth);
4837       /* Patch for the return type */
4838       if (patch_stage == JDEP_METHOD_RETURN)
4839         {
4840           jdep = CLASSD_LAST (ctxp->classd_list);
4841           JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4842         }
4843       /* This is the stop JDEP. METH allows the function's signature
4844          to be computed. */
4845       register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4846     }
4847   else
4848     meth = add_method (this_class, flags, meth_name,
4849                        build_java_signature (meth));
4850
4851   /* Remember final parameters */
4852   MARK_FINAL_PARMS (meth, orig_arg);
4853
4854   /* Fix the method argument list so we have the argument name
4855      information */
4856   fix_method_argument_names (orig_arg, meth);
4857
4858   /* Register the parameter number and re-install the current line
4859      number */
4860   DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4861   input_location = saved_location;
4862
4863   /* Register exception specified by the `throws' keyword for
4864      resolution and set the method decl appropriate field to the list.
4865      Note: the grammar ensures that what we get here are class
4866      types. */
4867   if (throws)
4868     {
4869       throws = nreverse (throws);
4870       for (current = throws; current; current = TREE_CHAIN (current))
4871         {
4872           register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4873                                     NULL_TREE, NULL_TREE);
4874           JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4875             &TREE_VALUE (current);
4876         }
4877       DECL_FUNCTION_THROWS (meth) = throws;
4878     }
4879
4880   if (TREE_TYPE (GET_CPC ()) != object_type_node)
4881     DECL_FUNCTION_WFL (meth) = id;
4882
4883   /* Set the flag if we correctly processed a constructor */
4884   if (constructor_ok)
4885     {
4886       DECL_CONSTRUCTOR_P (meth) = 1;
4887       /* Compute and store the number of artificial parameters declared
4888          for this constructor */
4889       for (count = 0, current = TYPE_FIELDS (this_class); current;
4890            current = TREE_CHAIN (current))
4891         if (FIELD_LOCAL_ALIAS (current))
4892           count++;
4893       DECL_FUNCTION_NAP (meth) = count;
4894     }
4895
4896   /* Eventually set the @deprecated tag flag */
4897   CHECK_DEPRECATED (meth);
4898
4899   /* If doing xref, store column and line number information instead
4900      of the line number only. */
4901   if (flag_emit_xref)
4902     {
4903 #ifdef USE_MAPPED_LOCATION
4904       DECL_SOURCE_LOCATION (meth) = EXPR_LOCATION (id);
4905 #else
4906       DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4907 #endif
4908     }
4909
4910   return meth;
4911 }
4912
4913 static void
4914 fix_method_argument_names (tree orig_arg, tree meth)
4915 {
4916   tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4917   if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4918     {
4919       TREE_PURPOSE (arg) = this_identifier_node;
4920       arg = TREE_CHAIN (arg);
4921     }
4922   while (orig_arg != end_params_node)
4923     {
4924       TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4925       orig_arg = TREE_CHAIN (orig_arg);
4926       arg = TREE_CHAIN (arg);
4927     }
4928 }
4929
4930 /* Complete the method declaration with METHOD_BODY.  */
4931
4932 static void
4933 finish_method_declaration (tree method_body)
4934 {
4935   int flags;
4936
4937   if (!current_function_decl)
4938     return;
4939
4940   flags = get_access_flags_from_decl (current_function_decl);
4941
4942   /* 8.4.5 Method Body */
4943   if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4944     {
4945       tree name = DECL_NAME (current_function_decl);
4946       parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4947                            "%s method %qs can't have a body defined",
4948                            (METHOD_NATIVE (current_function_decl) ?
4949                             "Native" : "Abstract"),
4950                            IDENTIFIER_POINTER (name));
4951       method_body = NULL_TREE;
4952     }
4953   else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4954     {
4955       tree name = DECL_NAME (current_function_decl);
4956       parse_error_context
4957         (DECL_FUNCTION_WFL (current_function_decl),
4958          "Non native and non abstract method %qs must have a body defined",
4959          IDENTIFIER_POINTER (name));
4960       method_body = NULL_TREE;
4961     }
4962
4963   if (flag_emit_class_files && method_body
4964       && TREE_CODE (method_body) == NOP_EXPR
4965       && TREE_TYPE (current_function_decl)
4966       && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4967     method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4968
4969   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4970   maybe_absorb_scoping_blocks ();
4971   /* Exit function's body */
4972   exit_block ();
4973   /* Merge last line of the function with first line, directly in the
4974      function decl. It will be used to emit correct debug info. */
4975   if (!flag_emit_xref)
4976     DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4977
4978   /* Since function's argument's list are shared, reset the
4979      ARG_FINAL_P parameter that might have been set on some of this
4980      function parameters. */
4981   UNMARK_FINAL_PARMS (current_function_decl);
4982
4983   /* So we don't have an irrelevant function declaration context for
4984      the next static block we'll see. */
4985   current_function_decl = NULL_TREE;
4986 }
4987
4988 /* Build a an error message for constructor circularity errors.  */
4989
4990 static char *
4991 constructor_circularity_msg (tree from, tree to)
4992 {
4993   static char string [4096];
4994   char *t = xstrdup (lang_printable_name (from, 2));
4995   sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4996   free (t);
4997   return string;
4998 }
4999
5000 /* Verify a circular call to METH. Return 1 if an error is found, 0
5001    otherwise.  */
5002
5003 static GTY(()) tree vcc_list;
5004 static int
5005 verify_constructor_circularity (tree meth, tree current)
5006 {
5007   tree c;
5008
5009   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5010     {
5011       if (TREE_VALUE (c) == meth)
5012         {
5013           char *t;
5014           if (vcc_list)
5015             {
5016               tree liste;
5017               vcc_list = nreverse (vcc_list);
5018               for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
5019                 {
5020                   parse_error_context
5021                     (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
5022                      constructor_circularity_msg
5023                       (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
5024                   java_error_count--;
5025                 }
5026             }
5027           t = xstrdup (lang_printable_name (meth, 2));
5028           parse_error_context (TREE_PURPOSE (c),
5029                                "%s: recursive invocation of constructor %qs",
5030                                constructor_circularity_msg (current, meth), t);
5031           free (t);
5032           vcc_list = NULL_TREE;
5033           return 1;
5034         }
5035     }
5036   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5037     {
5038       vcc_list = tree_cons (c, current, vcc_list);
5039       if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5040         return 1;
5041       vcc_list = TREE_CHAIN (vcc_list);
5042     }
5043   return 0;
5044 }
5045
5046 /* Check modifiers that can be declared but exclusively */
5047
5048 static void
5049 check_modifiers_consistency (int flags)
5050 {
5051   int acc_count = 0;
5052   tree cl = NULL_TREE;
5053
5054   THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5055   THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5056   THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5057   if (acc_count > 1)
5058     parse_error_context
5059       (cl, "Inconsistent member declaration.  At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5060
5061   acc_count = 0;
5062   cl = NULL_TREE;
5063   THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5064   THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5065   if (acc_count > 1)
5066     parse_error_context (cl,
5067                          "Inconsistent member declaration.  At most one of %<final%> or %<volatile%> may be specified");
5068 }
5069
5070 /* Check the methode header METH for abstract specifics features */
5071
5072 static void
5073 check_abstract_method_header (tree meth)
5074 {
5075   int flags = get_access_flags_from_decl (meth);
5076
5077   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5078                               ACC_ABSTRACT, "abstract method",
5079                               IDENTIFIER_POINTER (DECL_NAME (meth)));
5080   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5081                               ACC_PUBLIC, "abstract method",
5082                               IDENTIFIER_POINTER (DECL_NAME (meth)));
5083
5084   check_modifiers ("Illegal modifier %qs for interface method",
5085                   flags, INTERFACE_METHOD_MODIFIERS);
5086 }
5087
5088 /* Create a FUNCTION_TYPE node and start augmenting it with the
5089    declared function arguments. Arguments type that can't be resolved
5090    are left as they are, but the returned node is marked as containing
5091    incomplete types.  */
5092
5093 static tree
5094 method_declarator (tree id, tree list)
5095 {
5096   tree arg_types = NULL_TREE, current, node;
5097   tree meth = make_node (FUNCTION_TYPE);
5098   jdep *jdep;
5099
5100   patch_stage = JDEP_NO_PATCH;
5101
5102   if (GET_CPC () == error_mark_node)
5103     return error_mark_node;
5104
5105   /* If we're dealing with an inner class constructor, we hide the
5106      this$<n> decl in the name field of its parameter declaration.  We
5107      also might have to hide the outer context local alias
5108      initializers. Not done when the class is a toplevel class. */
5109   if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5110       && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5111     {
5112       tree aliases_list, type, thisn;
5113       /* First the aliases, linked to the regular parameters */
5114       aliases_list =
5115         build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5116                                                 TREE_TYPE (GET_CPC ()),
5117                                                 NULL_TREE, NULL);
5118       list = chainon (nreverse (aliases_list), list);
5119
5120       /* Then this$<n> */
5121       type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5122       thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5123       list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5124                         list);
5125     }
5126
5127   for (current = list; current; current = TREE_CHAIN (current))
5128     {
5129       int must_chain = 0;
5130       tree wfl_name = TREE_PURPOSE (current);
5131       tree type = TREE_VALUE (current);
5132       tree name = EXPR_WFL_NODE (wfl_name);
5133       tree already, arg_node;
5134       tree type_wfl = NULL_TREE;
5135       tree real_type;
5136
5137       /* Obtain a suitable type for resolution, if necessary */
5138       SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5139
5140       /* Process NAME, as it may specify extra dimension(s) for it */
5141       type = build_array_from_name (type, type_wfl, name, &name);
5142       EXPR_WFL_NODE (wfl_name) = name;
5143
5144       real_type = GET_REAL_TYPE (type);
5145       if (TREE_CODE (real_type) == RECORD_TYPE)
5146         {
5147           real_type = promote_type (real_type);
5148           if (TREE_CODE (type) == TREE_LIST)
5149             TREE_PURPOSE (type) = real_type;
5150         }
5151
5152       /* Check redefinition */
5153       for (already = arg_types; already; already = TREE_CHAIN (already))
5154         if (TREE_PURPOSE (already) == name)
5155           {
5156             parse_error_context
5157               (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5158                IDENTIFIER_POINTER (name),
5159                IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5160             break;
5161           }
5162
5163       /* If we've an incomplete argument type, we know there is a location
5164          to patch when the type get resolved, later.  */
5165       jdep = NULL;
5166       if (must_chain)
5167         {
5168           patch_stage = JDEP_METHOD;
5169           type = register_incomplete_type (patch_stage,
5170                                            type_wfl, wfl_name, type);
5171           jdep = CLASSD_LAST (ctxp->classd_list);
5172           JDEP_MISC (jdep) = id;
5173         }
5174
5175       /* The argument node: a name and a (possibly) incomplete type.  */
5176       arg_node = build_tree_list (name, real_type);
5177       /* Remember arguments declared final. */
5178       ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5179
5180       if (jdep)
5181         JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5182       TREE_CHAIN (arg_node) = arg_types;
5183       arg_types = arg_node;
5184     }
5185   TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5186   node = build_tree_list (id, meth);
5187   return node;
5188 }
5189
5190 static int
5191 unresolved_type_p (tree wfl, tree *returned)
5192 {
5193   if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5194     {
5195       if (returned)
5196         {
5197           tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5198           if (decl && current_class && (decl == TYPE_NAME (current_class)))
5199             *returned = TREE_TYPE (decl);
5200           else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5201             *returned = TREE_TYPE (GET_CPC ());
5202           else
5203             *returned = NULL_TREE;
5204         }
5205       return 1;
5206     }
5207   if (returned)
5208     *returned = wfl;
5209   return 0;
5210 }
5211
5212 /* From NAME, build a qualified identifier node using the
5213    qualification from the current package definition. */
5214
5215 static tree
5216 parser_qualified_classname (tree name)
5217 {
5218   tree nested_class_name;
5219
5220   if ((nested_class_name = maybe_make_nested_class_name (name)))
5221     return nested_class_name;
5222
5223   if (ctxp->package)
5224     return merge_qualified_name (ctxp->package, name);
5225   else
5226     return name;
5227 }
5228
5229 /* Called once the type a interface extends is resolved. Returns 0 if
5230    everything is OK.  */
5231
5232 static int
5233 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5234 {
5235   tree super_type = TREE_TYPE (super_decl);
5236
5237   /* Has to be an interface */
5238   if (!CLASS_INTERFACE (super_decl))
5239     {
5240       parse_error_context
5241         (this_wfl, "%s %qs can't implement/extend %s %qs",
5242          (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5243           "Interface" : "Class"),
5244          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5245          (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5246          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5247       return 1;
5248     }
5249
5250   /* Check top-level interface access. Inner classes are subject to member
5251      access rules (6.6.1). */
5252   if (! INNER_CLASS_P (super_type)
5253       && check_pkg_class_access (DECL_NAME (super_decl),
5254                                  NULL_TREE, true, this_decl))
5255     return 1;
5256
5257   SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5258                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5259                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5260   return 0;
5261 }
5262
5263 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5264    0 if everything is OK.  */
5265
5266 static int
5267 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5268 {
5269   tree super_type = TREE_TYPE (super_decl);
5270
5271   /* SUPER should be a CLASS (neither an array nor an interface) */
5272   if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5273     {
5274       parse_error_context
5275         (wfl, "Class %qs can't subclass %s %qs",
5276          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5277          (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5278          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5279       return 1;
5280     }
5281
5282   if (CLASS_FINAL (TYPE_NAME (super_type)))
5283     {
5284       parse_error_context (wfl, "Can't subclass final classes: %s",
5285                            IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5286       return 1;
5287     }
5288
5289   /* Check top-level class scope. Inner classes are subject to member access
5290      rules (6.6.1). */
5291   if (! INNER_CLASS_P (super_type)
5292       && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5293     return 1;
5294
5295   SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5296                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5297                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5298   return 0;
5299 }
5300
5301 /* Create a new dependency list and link it (in a LIFO manner) to the
5302    CTXP list of type dependency list.  */
5303
5304 static void
5305 create_jdep_list (struct parser_ctxt *ctxp)
5306 {
5307   jdeplist *new = xmalloc (sizeof (jdeplist));
5308   new->first = new->last = NULL;
5309   new->next = ctxp->classd_list;
5310   ctxp->classd_list = new;
5311 }
5312
5313 static jdeplist *
5314 reverse_jdep_list (struct parser_ctxt *ctxp)
5315 {
5316   jdeplist *prev = NULL, *current, *next;
5317   for (current = ctxp->classd_list; current; current = next)
5318     {
5319       next = current->next;
5320       current->next = prev;
5321       prev = current;
5322     }
5323   return prev;
5324 }
5325
5326 /* Create a fake pointer based on the ID stored in
5327    TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5328    registered again. */
5329
5330 static tree
5331 obtain_incomplete_type (tree type_name)
5332 {
5333   tree ptr = NULL_TREE, name;
5334
5335   if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5336     name = EXPR_WFL_NODE (type_name);
5337   else if (INCOMPLETE_TYPE_P (type_name))
5338     name = TYPE_NAME (type_name);
5339   else
5340     abort ();
5341
5342   /* Workaround from build_pointer_type for incomplete types.  */
5343   BUILD_PTR_FROM_NAME (ptr, name);
5344   TYPE_MODE (ptr) = ptr_mode;
5345   layout_type (ptr);
5346
5347   return ptr;
5348 }
5349
5350 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5351    non NULL instead of computing a new fake type based on WFL. The new
5352    dependency is inserted in the current type dependency list, in FIFO
5353    manner.  */
5354
5355 static tree
5356 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5357 {
5358   jdep *new = xmalloc (sizeof (jdep));
5359
5360   if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5361     ptr = obtain_incomplete_type (wfl);
5362
5363   JDEP_KIND (new) = kind;
5364   JDEP_DECL (new) = decl;
5365   JDEP_TO_RESOLVE (new) = ptr;
5366   JDEP_WFL (new) = wfl;
5367   JDEP_CHAIN (new) = NULL;
5368   JDEP_MISC (new) = NULL_TREE;
5369   /* For some dependencies, set the enclosing class of the current
5370      class to be the enclosing context */
5371   if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5372       && GET_ENCLOSING_CPC ())
5373     JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5374   else
5375     JDEP_ENCLOSING (new) = GET_CPC ();
5376   JDEP_GET_PATCH (new) = (tree *)NULL;
5377
5378   JDEP_INSERT (ctxp->classd_list, new);
5379
5380   return ptr;
5381 }
5382
5383 /* This checks for circular references with innerclasses. We start
5384    from SOURCE and should never reach TARGET. Extended/implemented
5385    types in SOURCE have their enclosing context checked not to reach
5386    TARGET. When the last enclosing context of SOURCE is reached, its
5387    extended/implemented types are also checked not to reach TARGET.
5388    In case of error, WFL of the offending type is returned; NULL_TREE
5389    otherwise.  */
5390
5391 static tree
5392 check_inner_circular_reference (tree source, tree target)
5393 {
5394   tree base_binfo;
5395   tree ctx, cl;
5396   int i;
5397
5398   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5399     {
5400       tree su;
5401
5402       /* We can end up with a NULL_TREE or an incomplete type here if
5403          we encountered previous type resolution errors. It's safe to
5404          simply ignore these cases.  */
5405       su = BINFO_TYPE (base_binfo);
5406       if (INCOMPLETE_TYPE_P (su))
5407         continue;
5408
5409       if (inherits_from_p (su, target))
5410         return lookup_cl (TYPE_NAME (su));
5411
5412       for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5413         {
5414           /* An enclosing context shouldn't be TARGET */
5415           if (ctx == TYPE_NAME (target))
5416             return lookup_cl (TYPE_NAME (su));
5417
5418           /* When we reach the enclosing last context, start a check
5419              on it, with the same target */
5420           if (! DECL_CONTEXT (ctx) &&
5421               (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5422             return cl;
5423         }
5424     }
5425   return NULL_TREE;
5426 }
5427
5428 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5429    offending type if a circularity is detected. NULL_TREE is returned
5430    otherwise. TYPE can be an interface or a class.   */
5431
5432 static tree
5433 check_circular_reference (tree type)
5434 {
5435   tree base_binfo;
5436   int i;
5437
5438   if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5439     return NULL_TREE;
5440
5441   if (! CLASS_INTERFACE (TYPE_NAME (type)))
5442     {
5443       if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5444         return lookup_cl (TYPE_NAME (type));
5445       return NULL_TREE;
5446     }
5447
5448   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5449     {
5450       if (BINFO_TYPE (base_binfo) != object_type_node
5451           && interface_of_p (type, BINFO_TYPE (base_binfo)))
5452         return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5453     }
5454   return NULL_TREE;
5455 }
5456
5457 void
5458 java_check_circular_reference (void)
5459 {
5460   tree current;
5461   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5462     {
5463       tree type = TREE_TYPE (current);
5464       tree cl;
5465
5466       cl = check_circular_reference (type);
5467       if (! cl)
5468         cl = check_inner_circular_reference (type, type);
5469       if (cl)
5470         parse_error_context (cl, "Cyclic class inheritance%s",
5471                              (cyclic_inheritance_report ?
5472                               cyclic_inheritance_report : ""));
5473     }
5474 }
5475
5476 /* Augment the parameter list PARM with parameters crafted to
5477    initialize outer context locals aliases. Through ARTIFICIAL, a
5478    count is kept of the number of crafted parameters. MODE governs
5479    what eventually gets created: something suitable for a function
5480    creation or a function invocation, either the constructor or
5481    finit$.  */
5482
5483 static tree
5484 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5485                                         int *artificial)
5486 {
5487   tree field;
5488   tree additional_parms = NULL_TREE;
5489
5490   for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5491     if (FIELD_LOCAL_ALIAS (field))
5492       {
5493         const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5494         tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5495         tree mangled_id;
5496
5497         switch (mode)
5498           {
5499           case AIPL_FUNCTION_DECLARATION:
5500             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5501                                                          &buffer [4]);
5502             purpose = build_wfl_node (mangled_id);
5503             if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5504               value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5505             else
5506               value = TREE_TYPE (field);
5507             break;
5508
5509           case AIPL_FUNCTION_CREATION:
5510             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5511                                                          &buffer [4]);
5512             value = TREE_TYPE (field);
5513             break;
5514
5515           case AIPL_FUNCTION_FINIT_INVOCATION:
5516             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5517                                                          &buffer [4]);
5518             /* Now, this is wrong. purpose should always be the NAME
5519                of something and value its matching value (decl, type,
5520                etc...) FIXME -- but there is a lot to fix. */
5521
5522             /* When invoked for this kind of operation, we already
5523                know whether a field is used or not. */
5524             purpose = TREE_TYPE (field);
5525             value = build_wfl_node (mangled_id);
5526             break;
5527
5528           case AIPL_FUNCTION_CTOR_INVOCATION:
5529             /* There are two case: the constructor invocation happens
5530                outside the local inner, in which case, locales from the outer
5531                context are directly used.
5532
5533                Otherwise, we fold to using the alias directly. */
5534             if (class_type == current_class)
5535               value = field;
5536             else
5537               {
5538                 name = get_identifier (&buffer[4]);
5539                 value = IDENTIFIER_LOCAL_VALUE (name);
5540               }
5541             break;
5542           }
5543         additional_parms = tree_cons (purpose, value, additional_parms);
5544         if (artificial)
5545           *artificial +=1;
5546       }
5547   if (additional_parms)
5548     {
5549       if (ANONYMOUS_CLASS_P (class_type)
5550           && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5551         additional_parms = nreverse (additional_parms);
5552       parm = chainon (additional_parms, parm);
5553     }
5554
5555    return parm;
5556 }
5557
5558 /* Craft a constructor for CLASS_DECL -- what we should do when none
5559    where found. ARGS is non NULL when a special signature must be
5560    enforced. This is the case for anonymous classes.  */
5561
5562 static tree
5563 craft_constructor (tree class_decl, tree args)
5564 {
5565   tree class_type = TREE_TYPE (class_decl);
5566   tree parm = NULL_TREE;
5567   /* Inherit access flags for the constructor from its enclosing class. */
5568   int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5569   int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5570   int i = 0, artificial = 0;
5571   tree decl, ctor_name;
5572   char buffer [80];
5573
5574   ctor_name = init_identifier_node;
5575
5576   /* If we're dealing with an inner class constructor, we hide the
5577      this$<n> decl in the name field of its parameter declaration. */
5578   if (PURE_INNER_CLASS_TYPE_P (class_type))
5579     {
5580       tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5581       parm = tree_cons (build_current_thisn (class_type),
5582                         build_pointer_type (type), parm);
5583
5584       /* Some more arguments to be hidden here. The values of the local
5585          variables of the outer context that the inner class needs to see. */
5586       parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5587                                                      class_type, parm,
5588                                                      &artificial);
5589     }
5590
5591   /* Then if there are any args to be enforced, enforce them now */
5592   for (; args && args != end_params_node; args = TREE_CHAIN (args))
5593     {
5594       /* If we see a `void *', we need to change it to Object.  */
5595       if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5596         TREE_VALUE (args) = object_ptr_type_node;
5597
5598       sprintf (buffer, "parm%d", i++);
5599       parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5600     }
5601
5602   CRAFTED_PARAM_LIST_FIXUP (parm);
5603   decl = create_artificial_method (class_type, flags, void_type_node,
5604                                    ctor_name, parm);
5605   fix_method_argument_names (parm, decl);
5606   /* Now, mark the artificial parameters. */
5607   DECL_FUNCTION_NAP (decl) = artificial;
5608   DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5609   DECL_INLINE (decl) = 1;
5610   return decl;
5611 }
5612
5613
5614 /* Fix the constructors. This will be called right after circular
5615    references have been checked. It is necessary to fix constructors
5616    early even if no code generation will take place for that class:
5617    some generated constructor might be required by the class whose
5618    compilation triggered this one to be simply loaded.  */
5619
5620 void
5621 java_fix_constructors (void)
5622 {
5623   tree current;
5624
5625   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5626     {
5627       tree class_type = TREE_TYPE (current);
5628       int saw_ctor = 0;
5629       tree decl;
5630
5631       if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5632         continue;
5633
5634       output_class = current_class = class_type;
5635       for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5636         {
5637           if (DECL_CONSTRUCTOR_P (decl))
5638             {
5639               fix_constructors (decl);
5640               saw_ctor = 1;
5641             }
5642         }
5643
5644       /* Anonymous class constructor can't be generated that early. */
5645       if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5646         craft_constructor (current, NULL_TREE);
5647     }
5648 }
5649
5650 /* safe_layout_class just makes sure that we can load a class without
5651    disrupting the current_class, input_file, input_line, etc, information
5652    about the class processed currently.  */
5653
5654 void
5655 safe_layout_class (tree class)
5656 {
5657   tree save_current_class = current_class;
5658   location_t save_location = input_location;
5659
5660   layout_class (class);
5661
5662   current_class = save_current_class;
5663   input_location = save_location;
5664 }
5665
5666 static tree
5667 jdep_resolve_class (jdep *dep)
5668 {
5669   tree decl;
5670
5671   if (JDEP_RESOLVED_P (dep))
5672     decl = JDEP_RESOLVED_DECL (dep);
5673   else
5674     {
5675       decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5676                             JDEP_DECL (dep), JDEP_WFL (dep));
5677       JDEP_RESOLVED (dep, decl);
5678       /* If there is no WFL, that's ok.  We generate this warning
5679          elsewhere.  */
5680       if (decl && JDEP_WFL (dep) != NULL_TREE)
5681         check_deprecation (JDEP_WFL (dep), decl);
5682     }
5683
5684   if (!decl)
5685     complete_class_report_errors (dep);
5686   else if (INNER_CLASS_DECL_P (decl))
5687     {
5688       tree inner = TREE_TYPE (decl);
5689       if (! CLASS_LOADED_P (inner))
5690         {
5691           safe_layout_class (inner);
5692           if (TYPE_SIZE (inner) == error_mark_node)
5693             TYPE_SIZE (inner) = NULL_TREE;
5694         }
5695       check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5696     }
5697   return decl;
5698 }
5699
5700 /* Complete unsatisfied class declaration and their dependencies */
5701
5702 void
5703 java_complete_class (void)
5704 {
5705   tree cclass;
5706   jdeplist *cclassd;
5707   int error_found;
5708   tree type;
5709
5710   /* Process imports */
5711   process_imports ();
5712
5713   /* Reverse things so we have the right order */
5714   ctxp->class_list = nreverse (ctxp->class_list);
5715   ctxp->classd_list = reverse_jdep_list (ctxp);
5716
5717   for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5718        cclass && cclassd;
5719        cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5720     {
5721       jdep *dep;
5722
5723       /* We keep the compilation unit imports in the class so that
5724          they can be used later to resolve type dependencies that
5725          aren't necessary to solve now. */
5726       TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5727       TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5728
5729       for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5730         {
5731           tree decl;
5732           if (!(decl = jdep_resolve_class (dep)))
5733             continue;
5734
5735           /* Now it's time to patch */
5736           switch (JDEP_KIND (dep))
5737             {
5738             case JDEP_SUPER:
5739               /* Simply patch super */
5740               if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5741                 continue;
5742               BINFO_TYPE (BINFO_BASE_BINFO
5743                           (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5744                 = TREE_TYPE (decl);
5745               break;
5746
5747             case JDEP_FIELD:
5748               {
5749                 /* We do part of the job done in add_field */
5750                 tree field_decl = JDEP_DECL (dep);
5751                 tree field_type = TREE_TYPE (decl);
5752                 if (TREE_CODE (field_type) == RECORD_TYPE)
5753                   field_type = promote_type (field_type);
5754                 TREE_TYPE (field_decl) = field_type;
5755                 DECL_ALIGN (field_decl) = 0;
5756                 DECL_USER_ALIGN (field_decl) = 0;
5757                 layout_decl (field_decl, 0);
5758                 SOURCE_FRONTEND_DEBUG
5759                   (("Completed field/var decl '%s' with '%s'",
5760                     IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5761                     IDENTIFIER_POINTER (DECL_NAME (decl))));
5762                 break;
5763               }
5764             case JDEP_METHOD:   /* We start patching a method */
5765             case JDEP_METHOD_RETURN:
5766               error_found = 0;
5767               while (1)
5768                 {
5769                   if (decl)
5770                     {
5771                       type = TREE_TYPE(decl);
5772                       if (TREE_CODE (type) == RECORD_TYPE)
5773                         type = promote_type (type);
5774                       JDEP_APPLY_PATCH (dep, type);
5775                       SOURCE_FRONTEND_DEBUG
5776                         (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5777                            "Completing fct '%s' with ret type '%s'":
5778                            "Completing arg '%s' with type '%s'"),
5779                           IDENTIFIER_POINTER (EXPR_WFL_NODE
5780                                               (JDEP_DECL_WFL (dep))),
5781                           IDENTIFIER_POINTER (DECL_NAME (decl))));
5782                     }
5783                   else
5784                     error_found = 1;
5785                   dep = JDEP_CHAIN (dep);
5786                   if (JDEP_KIND (dep) == JDEP_METHOD_END)
5787                     break;
5788                   else
5789                     decl = jdep_resolve_class (dep);
5790                 }
5791               if (!error_found)
5792                 {
5793                   tree mdecl = JDEP_DECL (dep), signature;
5794                   /* Recompute and reset the signature, check first that
5795                      all types are now defined. If they're not,
5796                      don't build the signature. */
5797                   if (check_method_types_complete (mdecl))
5798                     {
5799                       signature = build_java_signature (TREE_TYPE (mdecl));
5800                       set_java_signature (TREE_TYPE (mdecl), signature);
5801                     }
5802                 }
5803               else
5804                 continue;
5805               break;
5806
5807             case JDEP_INTERFACE:
5808               if (parser_check_super_interface (decl, JDEP_DECL (dep),
5809                                                 JDEP_WFL (dep)))
5810                 continue;
5811               parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5812               break;
5813
5814             case JDEP_PARM:
5815             case JDEP_VARIABLE:
5816               type = TREE_TYPE(decl);
5817               if (TREE_CODE (type) == RECORD_TYPE)
5818                 type = promote_type (type);
5819               JDEP_APPLY_PATCH (dep, type);
5820               break;
5821
5822             case JDEP_TYPE:
5823               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5824               SOURCE_FRONTEND_DEBUG
5825                 (("Completing a random type dependency on a '%s' node",
5826                   tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5827               break;
5828
5829             case JDEP_EXCEPTION:
5830               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5831               SOURCE_FRONTEND_DEBUG
5832                 (("Completing '%s' 'throws' argument node",
5833                   IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5834               break;
5835
5836             case JDEP_ANONYMOUS:
5837               patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5838               break;
5839
5840             default:
5841               abort ();
5842             }
5843         }
5844     }
5845   return;
5846 }
5847
5848 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5849    array.  */
5850
5851 static tree
5852 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5853 {
5854   tree tname = TYPE_NAME (class_type);
5855   tree resolved_type = TREE_TYPE (class_type);
5856   int array_dims = 0;
5857   tree resolved_type_decl;
5858
5859   if (resolved_type != NULL_TREE)
5860     {
5861       tree resolved_type_decl = TYPE_NAME (resolved_type);
5862       if (resolved_type_decl == NULL_TREE
5863           || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5864         {
5865           resolved_type_decl = build_decl (TYPE_DECL,
5866                                            TYPE_NAME (class_type),
5867                                            resolved_type);
5868         }
5869       return resolved_type_decl;
5870     }
5871
5872   /* 1- Check to see if we have an array. If true, find what we really
5873      want to resolve  */
5874   if ((array_dims = build_type_name_from_array_name (tname,
5875                                                      &TYPE_NAME (class_type))))
5876     WFL_STRIP_BRACKET (cl, cl);
5877
5878   /* 2- Resolve the bare type */
5879   if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5880                                                decl, cl)))
5881     return NULL_TREE;
5882   resolved_type = TREE_TYPE (resolved_type_decl);
5883
5884   /* 3- If we have an array, reconstruct the array down to its nesting */
5885   if (array_dims)
5886     {
5887       for (; array_dims; array_dims--)
5888         resolved_type = build_java_array_type (resolved_type, -1);
5889       resolved_type_decl = TYPE_NAME (resolved_type);
5890     }
5891   TREE_TYPE (class_type) = resolved_type;
5892   return resolved_type_decl;
5893 }
5894
5895 /* Effectively perform the resolution of class CLASS_TYPE.  DECL or CL
5896    are used to report error messages; CL must either be NULL_TREE or a
5897    WFL wrapping a class.  Do not try to replace TYPE_NAME (class_type)
5898    by a variable, since it is changed by find_in_imports{_on_demand}
5899    and (but it doesn't really matter) qualify_and_find.  */
5900
5901 tree
5902 do_resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5903 {
5904   tree new_class_decl = NULL_TREE, super = NULL_TREE;
5905   tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5906   tree decl_result;
5907   htab_t circularity_hash;
5908
5909   if (QUALIFIED_P (TYPE_NAME (class_type)))
5910     {
5911       /* If the type name is of the form `Q . Id', then Q is either a
5912          package name or a class name.  First we try to find Q as a
5913          class and then treat Id as a member type.  If we can't find Q
5914          as a class then we fall through.  */
5915       tree q, left, left_type, right;
5916       if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5917         {
5918           BUILD_PTR_FROM_NAME (left_type, left);
5919           q = do_resolve_class (enclosing, left_type, decl, cl);
5920           if (q)
5921             {
5922               enclosing = q;
5923               saved_enclosing_type = TREE_TYPE (q);
5924               BUILD_PTR_FROM_NAME (class_type, right);
5925             }
5926         }
5927     }
5928
5929   if (enclosing)
5930     {
5931       /* This hash table is used to register the classes we're going
5932          through when searching the current class as an inner class, in
5933          order to detect circular references. Remember to free it before
5934          returning the section 0- of this function. */
5935       circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5936                                       NULL);
5937
5938       /* 0- Search in the current class as an inner class.
5939          Maybe some code here should be added to load the class or
5940          something, at least if the class isn't an inner class and ended
5941          being loaded from class file. FIXME. */
5942       while (enclosing)
5943         {
5944           new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5945                                                 &super, class_type);
5946           if (new_class_decl)
5947             break;
5948
5949           /* If we haven't found anything because SUPER reached Object and
5950              ENCLOSING happens to be an innerclass, try the enclosing context. */
5951           if ((!super || super == object_type_node) &&
5952               enclosing && INNER_CLASS_DECL_P (enclosing))
5953             enclosing = DECL_CONTEXT (enclosing);
5954           else
5955             enclosing = NULL_TREE;
5956         }
5957
5958       htab_delete (circularity_hash);
5959
5960       if (new_class_decl)
5961         return new_class_decl;
5962     }
5963
5964   /* 1- Check for the type in single imports. This will change
5965      TYPE_NAME() if something relevant is found */
5966   find_in_imports (saved_enclosing_type, class_type);
5967
5968   /* 2- And check for the type in the current compilation unit */
5969   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5970     {
5971       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5972         load_class (TYPE_NAME (class_type), 0);
5973       return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5974     }
5975
5976   /* 3- Search according to the current package definition */
5977   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5978     {
5979       if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5980                                              TYPE_NAME (class_type))))
5981         return new_class_decl;
5982     }
5983
5984   /* 4- Check the import on demands. Don't allow bar.baz to be
5985      imported from foo.* */
5986   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5987     if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5988       return NULL_TREE;
5989
5990   /* If found in find_in_imports_on_demand, the type has already been
5991      loaded. */
5992   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5993     return new_class_decl;
5994
5995   /* 5- Try with a name qualified with the package name we've seen so far */
5996   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5997     {
5998       tree package;
5999
6000       /* If there is a current package (ctxp->package), it's the first
6001          element of package_list and we can skip it. */
6002       for (package = (ctxp->package ?
6003                       TREE_CHAIN (package_list) : package_list);
6004            package; package = TREE_CHAIN (package))
6005         if ((new_class_decl = qualify_and_find (class_type,
6006                                                TREE_PURPOSE (package),
6007                                                TYPE_NAME (class_type))))
6008           return new_class_decl;
6009     }
6010
6011   /* 5- Check another compilation unit that bears the name of type */
6012   load_class (TYPE_NAME (class_type), 0);
6013
6014   if (!cl)
6015     cl = lookup_cl (decl);
6016
6017   /* If we don't have a value for CL, then we're being called recursively.
6018      We can't check package access just yet, but it will be taken care of
6019      by the caller. */
6020   if (cl)
6021     {
6022       if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
6023         return NULL_TREE;
6024     }
6025
6026   /* 6- Last call for a resolution */
6027   decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6028
6029   /* The final lookup might have registered a.b.c into a.b$c If we
6030      failed at the first lookup, progressively change the name if
6031      applicable and use the matching DECL instead. */
6032   if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
6033     {
6034       char *separator;
6035       tree name = TYPE_NAME (class_type);
6036       char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
6037
6038       strcpy (namebuffer, IDENTIFIER_POINTER (name));
6039
6040       do {
6041
6042        /* Reach the last '.', and if applicable, replace it by a `$' and
6043           see if this exists as a type. */
6044        if ((separator = strrchr (namebuffer, '.')))
6045          {
6046            *separator = '$';
6047            name = get_identifier (namebuffer);
6048            decl_result = IDENTIFIER_CLASS_VALUE (name);
6049          }
6050       } while (!decl_result && separator);
6051     }
6052   return decl_result;
6053 }
6054
6055 static tree
6056 qualify_and_find (tree class_type, tree package, tree name)
6057 {
6058   tree new_qualified = merge_qualified_name (package, name);
6059   tree new_class_decl;
6060
6061   if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6062     load_class (new_qualified, 0);
6063   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6064     {
6065       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6066         load_class (TREE_TYPE (new_class_decl), 0);
6067       TYPE_NAME (class_type) = new_qualified;
6068       return IDENTIFIER_CLASS_VALUE (new_qualified);
6069     }
6070   return NULL_TREE;
6071 }
6072
6073 /* Resolve NAME and lay it out (if not done and if not the current
6074    parsed class). Return a decl node. This function is meant to be
6075    called when type resolution is necessary during the walk pass.  */
6076
6077 static tree
6078 resolve_and_layout (tree something, tree cl)
6079 {
6080   tree decl, decl_type;
6081
6082   /* Don't do that on the current class */
6083   if (something == current_class)
6084     return TYPE_NAME (current_class);
6085
6086   /* Don't do anything for void and other primitive types */
6087   if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6088     return NULL_TREE;
6089
6090   /* Pointer types can be reall pointer types or fake pointers. When
6091      finding a real pointer, recheck for primitive types */
6092   if (TREE_CODE (something) == POINTER_TYPE)
6093     {
6094       if (TREE_TYPE (something))
6095         {
6096           something = TREE_TYPE (something);
6097           if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6098             return NULL_TREE;
6099         }
6100       else
6101         something = TYPE_NAME (something);
6102     }
6103
6104   /* Don't do anything for arrays of primitive types */
6105   if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6106       && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6107     return NULL_TREE;
6108
6109   /* Something might be a WFL */
6110   if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6111     something = EXPR_WFL_NODE (something);
6112
6113   /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6114      TYPE_DECL or a real TYPE */
6115   else if (TREE_CODE (something) != IDENTIFIER_NODE)
6116     something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6117             DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6118
6119   if (!(decl = resolve_no_layout (something, cl)))
6120     return NULL_TREE;
6121
6122   /* Resolve and layout if necessary */
6123   decl_type = TREE_TYPE (decl);
6124   layout_class_methods (decl_type);
6125   /* Check methods */
6126   if (CLASS_FROM_SOURCE_P (decl_type))
6127     java_check_methods (decl);
6128   /* Layout the type if necessary */
6129   if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6130     safe_layout_class (decl_type);
6131
6132   return decl;
6133 }
6134
6135 /* Resolve a class, returns its decl but doesn't perform any
6136    layout. The current parsing context is saved and restored */
6137
6138 static tree
6139 resolve_no_layout (tree name, tree cl)
6140 {
6141   tree ptr, decl;
6142   BUILD_PTR_FROM_NAME (ptr, name);
6143   java_parser_context_save_global ();
6144   decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6145   java_parser_context_restore_global ();
6146
6147   return decl;
6148 }
6149
6150 /* Called when reporting errors. Skip the '[]'s in a complex array
6151    type description that failed to be resolved. purify_type_name can't
6152    use an identifier tree.  */
6153
6154 static const char *
6155 purify_type_name (const char *name)
6156 {
6157   int len = strlen (name);
6158   int bracket_found;
6159
6160   STRING_STRIP_BRACKETS (name, len, bracket_found);
6161   if (bracket_found)
6162     {
6163       char *stripped_name = xmemdup (name, len, len+1);
6164       stripped_name [len] = '\0';
6165       return stripped_name;
6166     }
6167   return name;
6168 }
6169
6170 /* The type CURRENT refers to can't be found. We print error messages.  */
6171
6172 static void
6173 complete_class_report_errors (jdep *dep)
6174 {
6175   const char *name;
6176
6177   if (!JDEP_WFL (dep))
6178     return;
6179
6180   name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6181   switch (JDEP_KIND (dep))
6182     {
6183     case JDEP_SUPER:
6184       parse_error_context
6185         (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6186          purify_type_name (name),
6187          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6188       break;
6189     case JDEP_FIELD:
6190       parse_error_context
6191         (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6192          purify_type_name (name),
6193          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6194       break;
6195     case JDEP_METHOD:           /* Covers arguments */
6196       parse_error_context
6197         (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6198          purify_type_name (name),
6199          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6200          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6201       break;
6202     case JDEP_METHOD_RETURN:    /* Covers return type */
6203       parse_error_context
6204         (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6205          purify_type_name (name),
6206          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6207       break;
6208     case JDEP_INTERFACE:
6209       parse_error_context
6210         (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6211          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6212          (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6213          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6214       break;
6215     case JDEP_VARIABLE:
6216       parse_error_context
6217         (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6218          purify_type_name (IDENTIFIER_POINTER
6219                            (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6220          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6221       break;
6222     case JDEP_EXCEPTION:        /* As specified by `throws' */
6223       parse_error_context
6224           (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6225          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6226       break;
6227     default:
6228       /* Fix for -Wall. Just break doing nothing. The error will be
6229          caught later */
6230       break;
6231     }
6232 }
6233
6234 /* Return a static string containing the DECL prototype string. If
6235    DECL is a constructor, use the class name instead of the form
6236    <init> */
6237
6238 static const char *
6239 get_printable_method_name (tree decl)
6240 {
6241   const char *to_return;
6242   tree name = NULL_TREE;
6243
6244   if (DECL_CONSTRUCTOR_P (decl))
6245     {
6246       name = DECL_NAME (decl);
6247       DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6248     }
6249
6250   to_return = lang_printable_name (decl, 2);
6251   if (DECL_CONSTRUCTOR_P (decl))
6252     DECL_NAME (decl) = name;
6253
6254   return to_return;
6255 }
6256
6257 /* Track method being redefined inside the same class. As a side
6258    effect, set DECL_NAME to an IDENTIFIER (prior entering this
6259    function it's a FWL, so we can track errors more accurately.)  */
6260
6261 static int
6262 check_method_redefinition (tree class, tree method)
6263 {
6264   tree redef, sig;
6265
6266   /* There's no need to verify <clinit> and finit$ and instinit$ */
6267   if (DECL_CLINIT_P (method)
6268       || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6269     return 0;
6270
6271   sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6272   for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6273     {
6274       if (redef == method)
6275         break;
6276       if (DECL_NAME (redef) == DECL_NAME (method)
6277           && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6278           && !DECL_ARTIFICIAL (method))
6279         {
6280           parse_error_context
6281             (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6282              (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6283              get_printable_method_name (redef));
6284           return 1;
6285         }
6286     }
6287   return 0;
6288 }
6289
6290 /* Return 1 if check went ok, 0 otherwise.  */
6291 static int
6292 check_abstract_method_definitions (int do_interface, tree class_decl,
6293                                    tree type)
6294 {
6295   tree class = TREE_TYPE (class_decl);
6296   tree method, end_type;
6297   int ok = 1;
6298
6299   end_type = (do_interface ? object_type_node : type);
6300   for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6301     {
6302       tree other_super, other_method, method_sig, method_name;
6303       int found = 0;
6304       int end_type_reached = 0;
6305
6306       if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6307         continue;
6308
6309       /* Now verify that somewhere in between TYPE and CLASS,
6310          abstract method METHOD gets a non abstract definition
6311          that is inherited by CLASS.  */
6312
6313       method_sig = build_java_signature (TREE_TYPE (method));
6314       method_name = DECL_NAME (method);
6315       if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6316         method_name = EXPR_WFL_NODE (method_name);
6317
6318       other_super = class;
6319       do {
6320         if (other_super == end_type)
6321           end_type_reached = 1;
6322
6323         /* Method search */
6324         for (other_method = TYPE_METHODS (other_super); other_method;
6325             other_method = TREE_CHAIN (other_method))
6326           {
6327             tree s = build_java_signature (TREE_TYPE (other_method));
6328             tree other_name = DECL_NAME (other_method);
6329
6330             if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6331               other_name = EXPR_WFL_NODE (other_name);
6332             if (!DECL_CLINIT_P (other_method)
6333                 && !DECL_CONSTRUCTOR_P (other_method)
6334                 && method_name == other_name
6335                 && method_sig == s
6336                 && !METHOD_ABSTRACT (other_method))
6337              {
6338                found = 1;
6339                break;
6340              }
6341           }
6342         other_super = CLASSTYPE_SUPER (other_super);
6343       } while (!end_type_reached);
6344
6345       /* Report that abstract METHOD didn't find an implementation
6346          that CLASS can use. */
6347       if (!found)
6348         {
6349           char *t = xstrdup (lang_printable_name
6350                             (TREE_TYPE (TREE_TYPE (method)), 0));
6351           tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6352
6353           parse_error_context
6354             (lookup_cl (class_decl),
6355              "Class %qs doesn't define the abstract method %<%s %s%> from %s %<%s%>. This method must be defined or %s %qs must be declared abstract",
6356              IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6357              t, lang_printable_name (method, 2),
6358              (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6359               "interface" : "class"),
6360              IDENTIFIER_POINTER (ccn),
6361              (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6362              IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6363           ok = 0;
6364           free (t);
6365         }
6366     }
6367
6368   if (ok && do_interface)
6369     {
6370       /* Check for implemented interfaces. */
6371       int i;
6372       tree base_binfo;
6373       
6374       for (i = 1;
6375            ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6376            i++)
6377         ok = check_abstract_method_definitions (1, class_decl,
6378                                                 BINFO_TYPE (base_binfo));
6379     }
6380
6381   return ok;
6382 }
6383
6384 /* Check that CLASS_DECL somehow implements all inherited abstract
6385    methods.  */
6386
6387 static void
6388 java_check_abstract_method_definitions (tree class_decl)
6389 {
6390   tree class = TREE_TYPE (class_decl);
6391   tree super, base_binfo;
6392   int i;
6393
6394   if (CLASS_ABSTRACT (class_decl))
6395     return;
6396
6397   /* Check for inherited types */
6398   super = class;
6399   do {
6400     super = CLASSTYPE_SUPER (super);
6401     check_abstract_method_definitions (0, class_decl, super);
6402   } while (super != object_type_node);
6403
6404   /* Check for implemented interfaces. */
6405   for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6406     check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6407 }
6408
6409 /* Check all the types method DECL uses and return 1 if all of them
6410    are now complete, 0 otherwise. This is used to check whether its
6411    safe to build a method signature or not.  */
6412
6413 static int
6414 check_method_types_complete (tree decl)
6415 {
6416   tree type = TREE_TYPE (decl);
6417   tree args;
6418
6419   if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6420     return 0;
6421
6422   args = TYPE_ARG_TYPES (type);
6423   if (TREE_CODE (type) == METHOD_TYPE)
6424     args = TREE_CHAIN (args);
6425   for (; args != end_params_node; args = TREE_CHAIN (args))
6426     if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6427       return 0;
6428
6429   return 1;
6430 }
6431
6432 /* Visible interface to check methods contained in CLASS_DECL */
6433
6434 void
6435 java_check_methods (tree class_decl)
6436 {
6437   if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6438     return;
6439
6440   if (CLASS_INTERFACE (class_decl))
6441     java_check_abstract_methods (class_decl);
6442   else
6443     java_check_regular_methods (class_decl);
6444
6445   CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6446 }
6447
6448 /* Like not_accessible_p, but doesn't refer to the current class at
6449    all.  */
6450 static bool
6451 hack_is_accessible_p (tree member, tree from_where)
6452 {
6453   int flags = get_access_flags_from_decl (member);
6454
6455   if (from_where == DECL_CONTEXT (member)
6456       || (flags & ACC_PUBLIC))
6457     return true;
6458
6459   if ((flags & ACC_PROTECTED))
6460     {
6461       if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6462         return true;
6463     }
6464
6465   if ((flags & ACC_PRIVATE))
6466     return false;
6467
6468   /* Package private, or protected.  */
6469   return in_same_package (TYPE_NAME (from_where),
6470                           TYPE_NAME (DECL_CONTEXT (member)));
6471 }
6472
6473 /* Check all the methods of CLASS_DECL. Methods are first completed
6474    then checked according to regular method existence rules.  If no
6475    constructor for CLASS_DECL were encountered, then build its
6476    declaration.  */
6477 static void
6478 java_check_regular_methods (tree class_decl)
6479 {
6480   int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6481   tree method;
6482   tree class = TREE_TYPE (class_decl);
6483   tree found = NULL_TREE;
6484   tree mthrows;
6485
6486   /* It is not necessary to check methods defined in java.lang.Object */
6487   if (class == object_type_node)
6488     return;
6489
6490   if (!TYPE_NVIRTUALS (class))
6491     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6492
6493   /* Should take interfaces into account. FIXME */
6494   for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6495     {
6496       tree sig;
6497       tree method_wfl = DECL_FUNCTION_WFL (method);
6498       int aflags;
6499
6500       /* Check for redefinitions */
6501       if (check_method_redefinition (class, method))
6502         continue;
6503
6504       /* We verify things thrown by the method.  They must inherit from
6505          java.lang.Throwable.  */
6506       for (mthrows = DECL_FUNCTION_THROWS (method);
6507            mthrows; mthrows = TREE_CHAIN (mthrows))
6508         {
6509           if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6510             parse_error_context
6511               (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6512                IDENTIFIER_POINTER
6513                  (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6514         }
6515
6516       /* If we see one constructor a mark so we don't generate the
6517          default one.  Also skip other verifications: constructors
6518          can't be inherited hence hidden or overridden.  */
6519       if (DECL_CONSTRUCTOR_P (method))
6520         {
6521           saw_constructor = 1;
6522           continue;
6523         }
6524
6525       sig = build_java_argument_signature (TREE_TYPE (method));
6526       found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6527                                               SEARCH_SUPER | SEARCH_INTERFACE);
6528
6529       /* Inner class can't declare static methods */
6530       if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6531         {
6532           char *t = xstrdup (lang_printable_name (class, 0));
6533           parse_error_context
6534             (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6535              lang_printable_name (method, 2), t);
6536           free (t);
6537         }
6538
6539       /* Nothing overrides or it's a private method. */
6540       if (!found)
6541         continue;
6542       if (METHOD_PRIVATE (found))
6543         {
6544           found = NULL_TREE;
6545           continue;
6546         }
6547
6548       /* If `found' is declared in an interface, make sure the
6549          modifier matches. */
6550       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6551           && clinit_identifier_node != DECL_NAME (found)
6552           && !METHOD_PUBLIC (method))
6553         {
6554           tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6555           parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6556                                IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6557                                lang_printable_name (method, 2),
6558                                IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6559         }
6560
6561       /* Can't override a method with the same name and different return
6562          types. */
6563       if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6564         {
6565           char *t = xstrdup
6566             (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6567           parse_error_context
6568             (method_wfl,
6569              "Method %qs was defined with return type %qs in class %qs",
6570              lang_printable_name (found, 2), t,
6571              IDENTIFIER_POINTER
6572                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6573           free (t);
6574         }
6575
6576       aflags = get_access_flags_from_decl (found);
6577
6578       /* Can't override final. Can't override static. */
6579       if (METHOD_FINAL (found) || METHOD_STATIC (found))
6580         {
6581           /* Static *can* override static */
6582           if (METHOD_STATIC (found) && METHOD_STATIC (method))
6583             continue;
6584           parse_error_context
6585             (method_wfl,
6586              "%s methods can't be overridden. Method %qs is %s in class %qs",
6587              (METHOD_FINAL (found) ? "Final" : "Static"),
6588              lang_printable_name (found, 2),
6589              (METHOD_FINAL (found) ? "final" : "static"),
6590              IDENTIFIER_POINTER
6591                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6592           continue;
6593         }
6594
6595       /* Static method can't override instance method. */
6596       if (METHOD_STATIC (method))
6597         {
6598           parse_error_context
6599             (method_wfl,
6600              "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6601              lang_printable_name (found, 2),
6602              IDENTIFIER_POINTER
6603                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6604           continue;
6605         }
6606
6607       /* - Overriding/hiding public must be public
6608          - Overriding/hiding protected must be protected or public
6609          - If the overridden or hidden method has default (package)
6610            access, then the overriding or hiding method must not be
6611            private; otherwise, a compile-time error occurs.  If
6612            `found' belongs to an interface, things have been already
6613            taken care of.  */
6614       if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6615           && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6616               || (METHOD_PROTECTED (found)
6617                   && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6618               || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6619                   && METHOD_PRIVATE (method))))
6620         {
6621           parse_error_context
6622             (method_wfl,
6623              "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6624              (METHOD_PUBLIC (method) ? "public" :
6625               (METHOD_PRIVATE (method) ? "private" : "protected")),
6626              IDENTIFIER_POINTER (DECL_NAME
6627                                  (TYPE_NAME (DECL_CONTEXT (found)))));
6628           continue;
6629         }
6630
6631       /* Check this method against all the other implementations it
6632          overrides.  Here we only check the class hierarchy; the rest
6633          of the checking is done later.  If this method is just a
6634          Miranda method, we can skip the check.  */
6635       if (! METHOD_INVISIBLE (method))
6636         check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6637     }
6638
6639   /* The above throws clause check only looked at superclasses.  Now
6640      we must also make sure that all methods declared in interfaces
6641      have compatible throws clauses.  FIXME: there are more efficient
6642      ways to organize this checking; we should implement one.  */
6643   check_interface_throws_clauses (class, class);
6644
6645   if (!TYPE_NVIRTUALS (class))
6646     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6647
6648   /* Search for inherited abstract method not yet implemented in this
6649      class.  */
6650   java_check_abstract_method_definitions (class_decl);
6651
6652   if (!saw_constructor)
6653     abort ();
6654 }
6655
6656 /* Check to make sure that all the methods in all the interfaces
6657    implemented by CLASS_DECL are compatible with the concrete
6658    implementations available in CHECK_CLASS_DECL.  */
6659 static void
6660 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6661 {
6662   for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6663     {
6664       int i;
6665
6666       if (! CLASS_LOADED_P (class_decl))
6667         {
6668           if (CLASS_FROM_SOURCE_P (class_decl))
6669             safe_layout_class (class_decl);
6670           else
6671             load_class (class_decl, 1);
6672         }
6673
6674       for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6675         {
6676           tree interface
6677             = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6678           tree iface_method;
6679
6680           for (iface_method = TYPE_METHODS (interface);
6681                iface_method != NULL_TREE;
6682                iface_method = TREE_CHAIN (iface_method))
6683             {
6684               tree sig, method;
6685
6686               /* First look for a concrete method implemented or
6687                  inherited by this class.  No need to search
6688                  interfaces here, since we're already looking through
6689                  all of them.  */
6690               sig = build_java_argument_signature (TREE_TYPE (iface_method));
6691               method
6692                 = lookup_argument_method_generic (check_class_decl,
6693                                                   DECL_NAME (iface_method),
6694                                                   sig, SEARCH_VISIBLE);
6695               /* If we don't find an implementation, that is ok.  Any
6696                  potential errors from that are diagnosed elsewhere.
6697                  Also, multiple inheritance with conflicting throws
6698                  clauses is fine in the absence of a concrete
6699                  implementation.  */
6700               if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6701                   && !METHOD_INVISIBLE (iface_method))
6702                 {
6703                   tree method_wfl = DECL_FUNCTION_WFL (method);
6704                   check_throws_clauses (method, method_wfl, iface_method);
6705                 }
6706             }
6707
6708           /* Now check superinterfaces.  */
6709           check_interface_throws_clauses (check_class_decl, interface);
6710         }
6711     }
6712 }
6713
6714 /* Check throws clauses of a method against the clauses of all the
6715    methods it overrides.  We do this by searching up the class
6716    hierarchy, examining all matching accessible methods.  */
6717 static void
6718 check_concrete_throws_clauses (tree class, tree self_method,
6719                                tree name, tree signature)
6720 {
6721   tree method = lookup_argument_method_generic (class, name, signature,
6722                                                 SEARCH_SUPER | SEARCH_VISIBLE);
6723   while (method != NULL_TREE)
6724     {
6725       if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6726         check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6727                               method);
6728
6729       method = lookup_argument_method_generic (DECL_CONTEXT (method),
6730                                                name, signature,
6731                                                SEARCH_SUPER | SEARCH_VISIBLE);
6732     }
6733 }
6734
6735 /* Generate an error if the `throws' clause of METHOD (if any) is
6736    incompatible with the `throws' clause of FOUND (if any).  */
6737 static void
6738 check_throws_clauses (tree method, tree method_wfl, tree found)
6739 {
6740   tree mthrows;
6741
6742   /* Can't check these things with class loaded from bytecode. FIXME */
6743   if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6744     return;
6745
6746   for (mthrows = DECL_FUNCTION_THROWS (method);
6747        mthrows; mthrows = TREE_CHAIN (mthrows))
6748     {
6749       tree fthrows;
6750
6751       /* We don't verify unchecked expressions */
6752       if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6753         continue;
6754       /* Checked expression must be compatible */
6755       for (fthrows = DECL_FUNCTION_THROWS (found);
6756            fthrows; fthrows = TREE_CHAIN (fthrows))
6757         {
6758           if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6759             break;
6760         }
6761       if (!fthrows)
6762         {
6763           parse_error_context
6764             (method_wfl, "Invalid checked exception class %qs in %<throws%> clause.  The exception must be a subclass of an exception thrown by %qs from class %qs",
6765              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6766              lang_printable_name (found, 2),
6767              IDENTIFIER_POINTER
6768              (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6769         }
6770     }
6771 }
6772
6773 /* Check abstract method of interface INTERFACE */
6774 static void
6775 java_check_abstract_methods (tree interface_decl)
6776 {
6777   int i;
6778   tree method, found;
6779   tree interface = TREE_TYPE (interface_decl);
6780   tree base_binfo;
6781
6782   for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6783     {
6784       /* 2- Check for double definition inside the defining interface */
6785       if (check_method_redefinition (interface, method))
6786         continue;
6787
6788       /* 3- Overriding is OK as far as we preserve the return type.  */
6789       found = lookup_java_interface_method2 (interface, method);
6790       if (found)
6791         {
6792           char *t;
6793           t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6794           parse_error_context
6795             (DECL_FUNCTION_WFL (found),
6796              "Method %qs was defined with return type %qs in class %qs",
6797              lang_printable_name (found, 2), t,
6798              IDENTIFIER_POINTER
6799                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6800           free (t);
6801           continue;
6802         }
6803     }
6804
6805   /* 4- Inherited methods can't differ by their returned types */
6806   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6807     {
6808       tree sub_interface_method, sub_interface;
6809
6810       sub_interface = BINFO_TYPE (base_binfo);
6811       for (sub_interface_method = TYPE_METHODS (sub_interface);
6812            sub_interface_method;
6813            sub_interface_method = TREE_CHAIN (sub_interface_method))
6814         {
6815           found = lookup_java_interface_method2 (interface,
6816                                                  sub_interface_method);
6817           if (found && (found != sub_interface_method))
6818             {
6819               parse_error_context
6820                 (lookup_cl (sub_interface_method),
6821                  "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6822                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6823                  lang_printable_name (found, 2),
6824                  IDENTIFIER_POINTER
6825                    (DECL_NAME (TYPE_NAME
6826                                (DECL_CONTEXT (sub_interface_method)))),
6827                  IDENTIFIER_POINTER
6828                    (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6829             }
6830         }
6831     }
6832 }
6833
6834 /* Lookup methods in interfaces using their name and partial
6835    signature. Return a matching method only if their types differ.  */
6836
6837 static tree
6838 lookup_java_interface_method2 (tree class, tree method_decl)
6839 {
6840   int i;
6841   tree base_binfo;
6842   tree to_return;
6843
6844   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6845     {
6846       if ((BINFO_TYPE (base_binfo) != object_type_node)
6847           && (to_return =
6848               lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6849         return to_return;
6850     }
6851   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6852     {
6853       to_return = lookup_java_interface_method2
6854         (BINFO_TYPE (base_binfo), method_decl);
6855       if (to_return)
6856         return to_return;
6857     }
6858
6859   return NULL_TREE;
6860 }
6861
6862 /* Lookup method using their name and partial signature. Return a
6863    matching method only if their types differ.  */
6864
6865 static tree
6866 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6867 {
6868   tree method, method_signature, method_name, method_type, name;
6869
6870   method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6871   name = DECL_NAME (method_decl);
6872   method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6873                  EXPR_WFL_NODE (name) : name);
6874   method_type = TREE_TYPE (TREE_TYPE (method_decl));
6875
6876   while (clas != NULL_TREE)
6877     {
6878       for (method = TYPE_METHODS (clas);
6879            method != NULL_TREE;  method = TREE_CHAIN (method))
6880         {
6881           tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6882           tree name = DECL_NAME (method);
6883           if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6884                EXPR_WFL_NODE (name) : name) == method_name
6885               && method_sig == method_signature
6886               && TREE_TYPE (TREE_TYPE (method)) != method_type)
6887             return method;
6888         }
6889       clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6890     }
6891   return NULL_TREE;
6892 }
6893
6894 /* Return the line that matches DECL line number, and try its best to
6895    position the column number. Used during error reports.
6896    FUTURE/FIXME: return source_location instead of node. */
6897
6898 static GTY(()) tree cl_v;
6899 static tree
6900 lookup_cl (tree decl)
6901 {
6902 #ifndef USE_MAPPED_LOCATION
6903   char *line, *found;
6904 #endif
6905
6906   if (!decl)
6907     return NULL_TREE;
6908
6909   if (cl_v == NULL_TREE)
6910     {
6911       cl_v = build_unknown_wfl (NULL_TREE);
6912     }
6913
6914 #ifdef USE_MAPPED_LOCATION
6915   SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6916 #else
6917   EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6918   EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6919
6920   line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6921                             EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6922
6923   found = strstr ((const char *)line,
6924                   (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6925   if (found)
6926     EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6927 #endif
6928
6929   return cl_v;
6930 }
6931
6932 /* Look for a simple name in the single-type import list */
6933
6934 static tree
6935 find_name_in_single_imports (tree name)
6936 {
6937   tree node;
6938
6939   for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6940     if (TREE_VALUE (node) == name)
6941       return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6942
6943   return NULL_TREE;
6944 }
6945
6946 /* Process all single-type import. */
6947
6948 static int
6949 process_imports (void)
6950 {
6951   tree import;
6952   int error_found;
6953
6954   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6955     {
6956       tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6957       char *original_name;
6958
6959       original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6960                                IDENTIFIER_LENGTH (to_be_found),
6961                                IDENTIFIER_LENGTH (to_be_found) + 1);
6962
6963       /* Don't load twice something already defined. */
6964       if (IDENTIFIER_CLASS_VALUE (to_be_found))
6965         continue;
6966
6967       while (1)
6968         {
6969           tree left;
6970
6971           QUALIFIED_P (to_be_found) = 1;
6972           load_class (to_be_found, 0);
6973           error_found =
6974             check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6975
6976           /* We found it, we can bail out */
6977           if (IDENTIFIER_CLASS_VALUE (to_be_found))
6978             {
6979               check_deprecation (TREE_PURPOSE (import),
6980                                  IDENTIFIER_CLASS_VALUE (to_be_found));
6981               break;
6982             }
6983
6984           /* We haven't found it. Maybe we're trying to access an
6985              inner class.  The only way for us to know is to try again
6986              after having dropped a qualifier. If we can't break it further,
6987              we have an error. */
6988           if (split_qualified_name (&left, NULL, to_be_found))
6989             break;
6990
6991           to_be_found = left;
6992         }
6993       if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6994         {
6995           parse_error_context (TREE_PURPOSE (import),
6996                                "Class or interface %qs not found in import",
6997                                original_name);
6998           error_found = 1;
6999         }
7000
7001       free (original_name);
7002       if (error_found)
7003         return 1;
7004     }
7005   return 0;
7006 }
7007
7008 /* Possibly find and mark a class imported by a single-type import
7009    statement.  */
7010
7011 static void
7012 find_in_imports (tree enclosing_type, tree class_type)
7013 {
7014   tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
7015                  ctxp->import_list);
7016   while (import)
7017     {
7018       if (TREE_VALUE (import) == TYPE_NAME (class_type))
7019         {
7020           TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
7021           QUALIFIED_P (TYPE_NAME (class_type)) = 1;
7022           return;
7023         }
7024       import = TREE_CHAIN (import);
7025     }
7026 }
7027
7028 static int
7029 note_possible_classname (const char *name, int len)
7030 {
7031   tree node;
7032   if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
7033     len = len - 5;
7034   else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
7035     len = len - 6;
7036   else
7037     return 0;
7038   node = ident_subst (name, len, "", '/', '.', "");
7039   IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
7040   QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
7041   return 1;
7042 }
7043
7044 /* Read a import directory, gathering potential match for further type
7045    references. Indifferently reads a filesystem or a ZIP archive
7046    directory.  */
7047
7048 static void
7049 read_import_dir (tree wfl)
7050 {
7051   tree package_id = EXPR_WFL_NODE (wfl);
7052   const char *package_name = IDENTIFIER_POINTER (package_id);
7053   int package_length = IDENTIFIER_LENGTH (package_id);
7054   DIR *dirp = NULL;
7055   JCF *saved_jcf = current_jcf;
7056
7057   int found = 0;
7058   int k;
7059   void *entry;
7060   struct buffer filename[1];
7061
7062   if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7063     return;
7064   IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7065
7066   BUFFER_INIT (filename);
7067   buffer_grow (filename, package_length + 100);
7068
7069   for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7070     {
7071       const char *entry_name = jcf_path_name (entry);
7072       int entry_length = strlen (entry_name);
7073       if (jcf_path_is_zipfile (entry))
7074         {
7075           ZipFile *zipf;
7076           buffer_grow (filename, entry_length);
7077           memcpy (filename->data, entry_name, entry_length - 1);
7078           filename->data[entry_length-1] = '\0';
7079           zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7080           if (zipf == NULL)
7081             error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7082           else
7083             {
7084               ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7085               BUFFER_RESET (filename);
7086               for (k = 0; k < package_length; k++)
7087                 {
7088                   char ch = package_name[k];
7089                   *filename->ptr++ = ch == '.' ? '/' : ch;
7090                 }
7091               *filename->ptr++ = '/';
7092
7093               for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
7094                 {
7095                   const char *current_entry = ZIPDIR_FILENAME (zipd);
7096                   int current_entry_len = zipd->filename_length;
7097
7098                   if (current_entry_len >= BUFFER_LENGTH (filename)
7099                       && strncmp ((const char *) filename->data, current_entry,
7100                                   BUFFER_LENGTH (filename)) != 0)
7101                     continue;
7102                   found |= note_possible_classname (current_entry,
7103                                                     current_entry_len);
7104                 }
7105             }
7106         }
7107       else
7108         {
7109           BUFFER_RESET (filename);
7110           buffer_grow (filename, entry_length + package_length + 4);
7111           strcpy ((char *) filename->data, entry_name);
7112           filename->ptr = filename->data + entry_length;
7113           for (k = 0; k < package_length; k++)
7114             {
7115               char ch = package_name[k];
7116               *filename->ptr++ = ch == '.' ? '/' : ch;
7117             }
7118           *filename->ptr = '\0';
7119
7120           dirp = opendir ((const char *) filename->data);
7121           if (dirp == NULL)
7122             continue;
7123           *filename->ptr++ = '/';
7124           for (;;)
7125             {
7126               int len;
7127               const char *d_name;
7128               struct dirent *direntp = readdir (dirp);
7129               if (!direntp)
7130                 break;
7131               d_name = direntp->d_name;
7132               len = strlen (direntp->d_name);
7133               buffer_grow (filename, len+1);
7134               strcpy ((char *) filename->ptr, d_name);
7135               found |= note_possible_classname ((const char *) filename->data + entry_length,
7136                                                 package_length+len+1);
7137             }
7138           if (dirp)
7139             closedir (dirp);
7140         }
7141     }
7142
7143   free (filename->data);
7144
7145   /* Here we should have a unified way of retrieving an entry, to be
7146      indexed. */
7147   if (!found)
7148     {
7149       static int first = 1;
7150       if (first)
7151         {
7152           error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7153           java_error_count++;
7154           first = 0;
7155         }
7156       else
7157         parse_error_context (wfl, "Package %qs not found in import",
7158                              package_name);
7159       current_jcf = saved_jcf;
7160       return;
7161     }
7162   current_jcf = saved_jcf;
7163 }
7164
7165 /* Possibly find a type in the import on demands specified
7166    types. Returns 1 if an error occurred, 0 otherwise. Run through the
7167    entire list, to detected potential double definitions.  */
7168
7169 static int
7170 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7171 {
7172   tree class_type_name = TYPE_NAME (class_type);
7173   tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
7174                   ctxp->import_demand_list);
7175   tree cl = NULL_TREE;
7176   int seen_once = -1;   /* -1 when not set, 1 if seen once, >1 otherwise. */
7177   int to_return = -1;   /* -1 when not set, 0 or 1 otherwise */
7178   tree node;
7179
7180   for (; import; import = TREE_CHAIN (import))
7181     {
7182       location_t saved_location = input_location;
7183       int access_check;
7184       const char *id_name;
7185       tree decl, type_name_copy;
7186
7187       obstack_grow (&temporary_obstack,
7188                     IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7189                     IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7190       obstack_1grow (&temporary_obstack, '.');
7191       obstack_grow0 (&temporary_obstack,
7192                      IDENTIFIER_POINTER (class_type_name),
7193                      IDENTIFIER_LENGTH (class_type_name));
7194       id_name = obstack_finish (&temporary_obstack);
7195
7196       if (! (node = maybe_get_identifier (id_name)))
7197         continue;
7198
7199       /* Setup input_line so that it refers to the line of the import (in
7200          case we parse a class file and encounter errors */
7201 #ifdef USE_MAPPED_LOCATION
7202       input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7203 #else
7204       input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7205 #endif
7206
7207       type_name_copy = TYPE_NAME (class_type);
7208       TYPE_NAME (class_type) = node;
7209       QUALIFIED_P (node) = 1;
7210       decl = IDENTIFIER_CLASS_VALUE (node);
7211       access_check = -1;
7212       /* If there is no DECL set for the class or if the class isn't
7213          loaded and not seen in source yet, then load */
7214       if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7215         {
7216           load_class (node, 0);
7217           decl = IDENTIFIER_CLASS_VALUE (node);
7218         }
7219       if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7220         access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7221                                                false, NULL_TREE);
7222       else
7223         /* 6.6.1: Inner classes are subject to member access rules. */
7224         access_check = 0;
7225
7226       input_location = saved_location;
7227
7228       /* If the loaded class is not accessible or couldn't be loaded,
7229          we restore the original TYPE_NAME and process the next
7230          import. */
7231       if (access_check || !decl)
7232         {
7233           TYPE_NAME (class_type) = type_name_copy;
7234           continue;
7235         }
7236
7237       /* If the loaded class is accessible, we keep a tab on it to
7238          detect and report multiple inclusions. */
7239       if (IS_A_CLASSFILE_NAME (node))
7240         {
7241           if (seen_once < 0)
7242             {
7243               cl = TREE_PURPOSE (import);
7244               seen_once = 1;
7245             }
7246           else if (seen_once >= 0)
7247             {
7248               tree location = (cl ? cl : TREE_PURPOSE (import));
7249               tree package = (cl ? EXPR_WFL_NODE (cl) :
7250                               EXPR_WFL_NODE (TREE_PURPOSE (import)));
7251               seen_once++;
7252               parse_error_context
7253                 (location,
7254                  "Type %qs also potentially defined in package %qs",
7255                  IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7256                  IDENTIFIER_POINTER (package));
7257             }
7258         }
7259       to_return = access_check;
7260     }
7261
7262   if (seen_once == 1)
7263     return to_return;
7264   else
7265     return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7266 }
7267
7268 /* Add package NAME to the list of packages encountered so far. To
7269    speed up class lookup in do_resolve_class, we make sure a
7270    particular package is added only once.  */
7271
7272 static void
7273 register_package (tree name)
7274 {
7275   static htab_t pht;
7276   void **e;
7277
7278   if (pht == NULL)
7279     pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
7280
7281   e = htab_find_slot (pht, name, INSERT);
7282   if (*e == NULL)
7283     {
7284       package_list = chainon (package_list, build_tree_list (name, NULL));
7285       *e = name;
7286     }
7287 }
7288
7289 static tree
7290 resolve_package (tree pkg, tree *next, tree *type_name)
7291 {
7292   tree current;
7293   tree decl = NULL_TREE;
7294   *type_name = NULL_TREE;
7295
7296   /* The trick is to determine when the package name stops and were
7297      the name of something contained in the package starts. Then we
7298      return a fully qualified name of what we want to get. */
7299
7300   *next = EXPR_WFL_QUALIFICATION (pkg);
7301
7302   /* Try to progressively construct a type name */
7303   if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7304     for (current = EXPR_WFL_QUALIFICATION (pkg);
7305          current; current = TREE_CHAIN (current))
7306       {
7307         /* If we don't have what we're expecting, exit now. TYPE_NAME
7308            will be null and the error caught later. */
7309         if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7310           break;
7311         *type_name =
7312           merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7313         if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7314           {
7315             /* resolve_package should be used in a loop, hence we
7316                point at this one to naturally process the next one at
7317                the next iteration. */
7318             *next = current;
7319             break;
7320           }
7321       }
7322   return decl;
7323 }
7324
7325
7326 /* Check accessibility of inner classes according to member access rules.
7327    DECL is the inner class, ENCLOSING_DECL is the class from which the
7328    access is being attempted. */
7329
7330 static void
7331 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7332 {
7333   const char *access;
7334   tree enclosing_decl_type;
7335
7336   /* We don't issue an error message when CL is null. CL can be null
7337      as a result of processing a JDEP crafted by source_start_java_method
7338      for the purpose of patching its parm decl. But the error would
7339      have been already trapped when fixing the method's signature.
7340      DECL can also be NULL in case of earlier errors. */
7341   if (!decl || !cl)
7342     return;
7343
7344   enclosing_decl_type = TREE_TYPE (enclosing_decl);
7345
7346   if (CLASS_PRIVATE (decl))
7347     {
7348       /* Access is permitted only within the body of the top-level
7349          class in which DECL is declared. */
7350       tree top_level = decl;
7351       while (DECL_CONTEXT (top_level))
7352         top_level = DECL_CONTEXT (top_level);
7353       while (DECL_CONTEXT (enclosing_decl))
7354         enclosing_decl = DECL_CONTEXT (enclosing_decl);
7355       if (top_level == enclosing_decl)
7356         return;
7357       access = "private";
7358     }
7359   else if (CLASS_PROTECTED (decl))
7360     {
7361       tree decl_context;
7362       /* Access is permitted from within the same package... */
7363       if (in_same_package (decl, enclosing_decl))
7364         return;
7365
7366       /* ... or from within the body of a subtype of the context in which
7367          DECL is declared. */
7368       decl_context = DECL_CONTEXT (decl);
7369       while (enclosing_decl)
7370         {
7371           if (CLASS_INTERFACE (decl))
7372             {
7373               if (interface_of_p (TREE_TYPE (decl_context),
7374                                   enclosing_decl_type))
7375                 return;
7376             }
7377           else
7378             {
7379               /* Eww. The order of the arguments is different!! */
7380               if (inherits_from_p (enclosing_decl_type,
7381                                    TREE_TYPE (decl_context)))
7382                 return;
7383             }
7384           enclosing_decl = DECL_CONTEXT (enclosing_decl);
7385         }
7386       access = "protected";
7387     }
7388   else if (! CLASS_PUBLIC (decl))
7389     {
7390       /* Access is permitted only from within the same package as DECL. */
7391       if (in_same_package (decl, enclosing_decl))
7392         return;
7393       access = "non-public";
7394     }
7395   else
7396     /* Class is public. */
7397     return;
7398
7399   parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7400                        (CLASS_INTERFACE (decl) ? "interface" : "class"),
7401                        lang_printable_name (decl, 2), access);
7402 }
7403
7404 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7405    foreign package, it must be PUBLIC. Return 0 if no access
7406    violations were found, 1 otherwise. If VERBOSE is true and an error
7407    was found, it is reported and accounted for.  If CL is NULL then 
7408    look it up with THIS_DECL.  */
7409
7410 static int
7411 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7412 {
7413   tree type;
7414
7415   if (!IDENTIFIER_CLASS_VALUE (class_name))
7416     return 0;
7417
7418   if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7419     return 0;
7420
7421   if (!CLASS_PUBLIC (TYPE_NAME (type)))
7422     {
7423       /* Access to a private class within the same package is
7424          allowed. */
7425       tree l, r;
7426       split_qualified_name (&l, &r, class_name);
7427       if (!QUALIFIED_P (class_name) && !ctxp->package)
7428         /* Both in the empty package. */
7429         return 0;
7430       if (l == ctxp->package)
7431         /* Both in the same package. */
7432         return 0;
7433
7434       if (verbose)
7435         parse_error_context
7436           (cl == NULL ? lookup_cl (this_decl): cl,
7437            "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7438            (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7439            IDENTIFIER_POINTER (class_name));
7440       return 1;
7441     }
7442   return 0;
7443 }
7444
7445 /* Local variable declaration. */
7446
7447 static void
7448 declare_local_variables (int modifier, tree type, tree vlist)
7449 {
7450   tree decl, current, saved_type;
7451   tree type_wfl = NULL_TREE;
7452   int must_chain = 0;
7453   int final_p = 0;
7454
7455   /* Push a new block if statements were seen between the last time we
7456      pushed a block and now. Keep a count of blocks to close */
7457   if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7458     {
7459       tree b = enter_block ();
7460       BLOCK_IS_IMPLICIT (b) = 1;
7461     }
7462
7463   if (modifier)
7464     {
7465       size_t i;
7466       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7467         if (1 << i & modifier)
7468           break;
7469       if (modifier == ACC_FINAL)
7470         final_p = 1;
7471       else
7472         {
7473           parse_error_context
7474             (ctxp->modifier_ctx [i],
7475              "Only %<final%> is allowed as a local variables modifier");
7476           return;
7477         }
7478     }
7479
7480   /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7481      hold the TYPE value if a new incomplete has to be created (as
7482      opposed to being found already existing and reused). */
7483   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7484
7485   /* If TYPE is fully resolved and we don't have a reference, make one */
7486   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7487
7488   /* Go through all the declared variables */
7489   for (current = vlist, saved_type = type; current;
7490        current = TREE_CHAIN (current), type = saved_type)
7491     {
7492       tree other, real_type;
7493       tree wfl  = TREE_PURPOSE (current);
7494       tree name = EXPR_WFL_NODE (wfl);
7495       tree init = TREE_VALUE (current);
7496
7497       /* Process NAME, as it may specify extra dimension(s) for it */
7498       type = build_array_from_name (type, type_wfl, name, &name);
7499
7500       /* Variable redefinition check */
7501       if ((other = lookup_name_in_blocks (name)))
7502         {
7503           variable_redefinition_error (wfl, name, TREE_TYPE (other),
7504                                        DECL_SOURCE_LINE (other));
7505           continue;
7506         }
7507
7508       /* Type adjustment. We may have just readjusted TYPE because
7509          the variable specified more dimensions. Make sure we have
7510          a reference if we can and don't have one already. */
7511       PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7512
7513       real_type = GET_REAL_TYPE (type);
7514       /* Never layout this decl. This will be done when its scope
7515          will be entered */
7516       decl = build_decl (VAR_DECL, name, real_type);
7517       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7518       DECL_FINAL (decl) = final_p;
7519       BLOCK_CHAIN_DECL (decl);
7520
7521       /* If doing xreferencing, replace the line number with the WFL
7522          compound value */
7523 #ifdef USE_MAPPED_LOCATION
7524       if (flag_emit_xref)
7525         DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (wfl);
7526 #else
7527       if (flag_emit_xref)
7528         DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7529 #endif
7530
7531       /* Don't try to use an INIT statement when an error was found */
7532       if (init && java_error_count)
7533         init = NULL_TREE;
7534
7535       /* Remember it if this is an initialized-upon-declaration final
7536          variable.  */
7537       if (init && final_p)
7538         {
7539           DECL_LOCAL_FINAL_IUD (decl) = 1;
7540         }
7541
7542       /* Add the initialization function to the current function's code */
7543       if (init)
7544         {
7545           /* Name might have been readjusted */
7546           EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7547           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7548           java_method_add_stmt (current_function_decl,
7549                                 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7550                                                       init));
7551         }
7552
7553       /* Setup dependency the type of the decl */
7554       if (must_chain)
7555         {
7556           jdep *dep;
7557           register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7558           dep = CLASSD_LAST (ctxp->classd_list);
7559           JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7560         }
7561     }
7562   SOURCE_FRONTEND_DEBUG (("Defined locals"));
7563 }
7564
7565 /* Called during parsing. Build decls from argument list.  */
7566
7567 static void
7568 source_start_java_method (tree fndecl)
7569 {
7570   tree tem;
7571   tree parm_decl;
7572   int i;
7573
7574   if (!fndecl)
7575     return;
7576
7577   current_function_decl = fndecl;
7578
7579   /* New scope for the function */
7580   enter_block ();
7581   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7582        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7583     {
7584       tree type = TREE_VALUE (tem);
7585       tree name = TREE_PURPOSE (tem);
7586
7587       /* If type is incomplete. Create an incomplete decl and ask for
7588          the decl to be patched later */
7589       if (INCOMPLETE_TYPE_P (type))
7590         {
7591           jdep *jdep;
7592           tree real_type = GET_REAL_TYPE (type);
7593           parm_decl = build_decl (PARM_DECL, name, real_type);
7594           type = obtain_incomplete_type (type);
7595           register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7596           jdep = CLASSD_LAST (ctxp->classd_list);
7597           JDEP_MISC (jdep) = name;
7598           JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7599         }
7600       else
7601         parm_decl = build_decl (PARM_DECL, name, type);
7602
7603       /* Remember if a local variable was declared final (via its
7604          TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7605       if (ARG_FINAL_P (tem))
7606         {
7607           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7608           DECL_FINAL (parm_decl) = 1;
7609         }
7610
7611       BLOCK_CHAIN_DECL (parm_decl);
7612     }
7613   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7614   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7615     nreverse (tem);
7616   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7617   DECL_MAX_LOCALS (current_function_decl) = i;
7618 }
7619
7620 /* Called during parsing. Creates an artificial method declaration.  */
7621
7622 static tree
7623 create_artificial_method (tree class, int flags, tree type,
7624                           tree name, tree args)
7625 {
7626   tree mdecl;
7627   location_t save_location = input_location;
7628
7629   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7630   mdecl = make_node (FUNCTION_TYPE);
7631   TREE_TYPE (mdecl) = type;
7632   TYPE_ARG_TYPES (mdecl) = args;
7633   /* We used to compute the signature of MDECL here and then use
7634      add_method(), but that failed because our caller might modify
7635      the type of the returned method, which trashes the cache in
7636      get_type_from_signature().  */
7637   mdecl = add_method_1 (class, flags, name, mdecl);
7638   input_location = save_location;
7639   DECL_ARTIFICIAL (mdecl) = 1;
7640   return mdecl;
7641 }
7642
7643 /* Starts the body if an artificial method.  */
7644
7645 static void
7646 start_artificial_method_body (tree mdecl)
7647 {
7648 #ifdef USE_MAPPED_LOCATION
7649   DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7650   DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7651 #else
7652   DECL_SOURCE_LINE (mdecl) = 1;
7653   DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7654 #endif
7655   source_start_java_method (mdecl);
7656   enter_block ();
7657 }
7658
7659 static void
7660 end_artificial_method_body (tree mdecl)
7661 {
7662   /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7663      It has to be evaluated first. (if mdecl is current_function_decl,
7664      we have an undefined behavior if no temporary variable is used.) */
7665   tree b = exit_block ();
7666   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7667   exit_block ();
7668 }
7669
7670 /* Dump a tree of some kind.  This is a convenience wrapper for the
7671    dump_* functions in tree-dump.c.  */
7672 static void
7673 dump_java_tree (enum tree_dump_index phase, tree t)
7674 {
7675   FILE *stream;
7676   int flags;
7677
7678   stream = dump_begin (phase, &flags);
7679   flags |= TDF_SLIM;
7680   if (stream)
7681     {
7682       dump_node (t, flags, stream);
7683       dump_end (phase, stream);
7684     }
7685 }
7686
7687 /* Terminate a function and expand its body.  */
7688
7689 static void
7690 source_end_java_method (void)
7691 {
7692   tree fndecl = current_function_decl;
7693
7694   if (!fndecl)
7695     return;
7696
7697   java_parser_context_save_global ();
7698 #ifdef USE_MAPPED_LOCATION
7699   input_location = ctxp->last_ccb_indent1;
7700 #else
7701   input_line = ctxp->last_ccb_indent1;
7702 #endif
7703
7704   /* Turn function bodies with only a NOP expr null, so they don't get
7705      generated at all and we won't get warnings when using the -W
7706      -Wall flags. */
7707   if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7708     BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7709
7710   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7711       && ! flag_emit_class_files
7712       && ! flag_emit_xref)
7713     finish_method (fndecl);
7714
7715   current_function_decl = NULL_TREE;
7716   java_parser_context_restore_global ();
7717   current_function_decl = NULL_TREE;
7718 }
7719
7720 /* Record EXPR in the current function block. Complements compound
7721    expression second operand if necessary.  */
7722
7723 tree
7724 java_method_add_stmt (tree fndecl, tree expr)
7725 {
7726   if (!GET_CURRENT_BLOCK (fndecl))
7727     return NULL_TREE;
7728   return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7729 }
7730
7731 static tree
7732 add_stmt_to_block (tree b, tree type, tree stmt)
7733 {
7734   tree body = BLOCK_EXPR_BODY (b), c;
7735
7736   if (java_error_count)
7737     return body;
7738
7739   if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7740     return body;
7741
7742   BLOCK_EXPR_BODY (b) = c;
7743   TREE_SIDE_EFFECTS (c) = 1;
7744   return c;
7745 }
7746
7747 /* Lays out the methods for the classes seen so far.  */
7748
7749 void
7750 java_layout_seen_class_methods (void)
7751 {
7752   tree previous_list = all_class_list;
7753   tree end = NULL_TREE;
7754   tree current;
7755
7756   while (1)
7757     {
7758       for (current = previous_list;
7759            current != end; current = TREE_CHAIN (current))
7760         {
7761           tree decl = TREE_VALUE (current);
7762           tree cls = TREE_TYPE (decl);
7763
7764           input_location = DECL_SOURCE_LOCATION (decl);
7765
7766           if (! CLASS_LOADED_P (cls))
7767             load_class (cls, 0);
7768
7769           layout_class_methods (cls);
7770         }
7771
7772       /* Note that new classes might have been added while laying out
7773          methods, changing the value of all_class_list.  */
7774
7775       if (previous_list != all_class_list)
7776         {
7777           end = previous_list;
7778           previous_list = all_class_list;
7779         }
7780       else
7781         break;
7782     }
7783 }
7784
7785 static GTY(()) tree stop_reordering;
7786 void
7787 java_reorder_fields (void)
7788 {
7789   tree current;
7790
7791   for (current = gclass_list; current; current = TREE_CHAIN (current))
7792     {
7793       output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7794
7795       if (current_class == stop_reordering)
7796         break;
7797
7798       /* Reverse the fields, but leave the dummy field in front.
7799          Fields are already ordered for Object and Class */
7800       if (TYPE_FIELDS (current_class) && current_class != object_type_node
7801           && current_class != class_type_node)
7802       {
7803         /* If the dummy field is there, reverse the right fields and
7804            just layout the type for proper fields offset */
7805         if (!DECL_NAME (TYPE_FIELDS (current_class)))
7806           {
7807             tree fields = TYPE_FIELDS (current_class);
7808             TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7809             TYPE_SIZE (current_class) = NULL_TREE;
7810           }
7811         /* We don't have a dummy field, we need to layout the class,
7812            after having reversed the fields */
7813         else
7814           {
7815             TYPE_FIELDS (current_class) =
7816               nreverse (TYPE_FIELDS (current_class));
7817             TYPE_SIZE (current_class) = NULL_TREE;
7818           }
7819       }
7820     }
7821   /* There are cases were gclass_list will be empty. */
7822   if (gclass_list)
7823     stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7824 }
7825
7826 /* Layout the methods of all classes loaded in one way or another.
7827    Check methods of source parsed classes. Then reorder the
7828    fields and layout the classes or the type of all source parsed
7829    classes */
7830
7831 void
7832 java_layout_classes (void)
7833 {
7834   tree current;
7835   int save_error_count = java_error_count;
7836
7837   /* Layout the methods of all classes seen so far */
7838   java_layout_seen_class_methods ();
7839   java_parse_abort_on_error ();
7840   all_class_list = NULL_TREE;
7841
7842   /* Then check the methods of all parsed classes */
7843   for (current = gclass_list; current; current = TREE_CHAIN (current))
7844     if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7845       java_check_methods (TREE_VALUE (current));
7846   java_parse_abort_on_error ();
7847
7848   for (current = gclass_list; current; current = TREE_CHAIN (current))
7849     {
7850       output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7851       layout_class (current_class);
7852
7853       /* Error reported by the caller */
7854       if (java_error_count)
7855         return;
7856     }
7857
7858   /* We might have reloaded classes durign the process of laying out
7859      classes for code generation. We must layout the methods of those
7860      late additions, as constructor checks might use them */
7861   java_layout_seen_class_methods ();
7862   java_parse_abort_on_error ();
7863 }
7864
7865 /* Expand methods in the current set of classes remembered for
7866    generation.  */
7867
7868 static void
7869 java_complete_expand_classes (void)
7870 {
7871   tree current;
7872
7873   do_not_fold = flag_emit_xref;
7874
7875   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7876     if (!INNER_CLASS_DECL_P (current))
7877       java_complete_expand_class (current);
7878 }
7879
7880 /* Expand the methods found in OUTER, starting first by OUTER's inner
7881    classes, if any.  */
7882
7883 static void
7884 java_complete_expand_class (tree outer)
7885 {
7886   tree inner_list;
7887
7888   /* We need to go after all inner classes and start expanding them,
7889      starting with most nested ones. We have to do that because nested
7890      classes might add functions to outer classes */
7891
7892   for (inner_list = DECL_INNER_CLASS_LIST (outer);
7893        inner_list; inner_list = TREE_CHAIN (inner_list))
7894     java_complete_expand_class (TREE_PURPOSE (inner_list));
7895
7896   java_complete_expand_methods (outer);
7897 }
7898
7899 /* Expand methods registered in CLASS_DECL. The general idea is that
7900    we expand regular methods first. This allows us get an estimate on
7901    how outer context local alias fields are really used so we can add
7902    to the constructor just enough code to initialize them properly (it
7903    also lets us generate finit$ correctly.) Then we expand the
7904    constructors and then <clinit>.  */
7905
7906 static void
7907 java_complete_expand_methods (tree class_decl)
7908 {
7909   tree clinit, decl, first_decl;
7910
7911   output_class = current_class = TREE_TYPE (class_decl);
7912
7913   /* Pre-expand <clinit> to figure whether we really need it or
7914      not. If we do need it, we pre-expand the static fields so they're
7915      ready to be used somewhere else. <clinit> will be fully expanded
7916      after we processed the constructors. */
7917   first_decl = TYPE_METHODS (current_class);
7918   clinit = maybe_generate_pre_expand_clinit (current_class);
7919
7920   /* Then generate finit$ (if we need to) because constructors will
7921    try to use it.*/
7922   if (TYPE_FINIT_STMT_LIST (current_class))
7923     java_complete_expand_method (generate_finit (current_class));
7924
7925   /* Then generate instinit$ (if we need to) because constructors will
7926      try to use it. */
7927   if (TYPE_II_STMT_LIST (current_class))
7928     java_complete_expand_method (generate_instinit (current_class));
7929
7930   /* Now do the constructors */
7931   for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7932     {
7933       if (!DECL_CONSTRUCTOR_P (decl))
7934         continue;
7935       java_complete_expand_method (decl);
7936     }
7937
7938   /* First, do the ordinary methods. */
7939   for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7940     {
7941       /* Ctors aren't part of this batch. */
7942       if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7943         continue;
7944
7945       /* Skip abstract or native methods -- but do handle native
7946          methods when generating JNI stubs.  */
7947       if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7948         {
7949           DECL_FUNCTION_BODY (decl) = NULL_TREE;
7950           continue;
7951         }
7952
7953       if (METHOD_NATIVE (decl))
7954         {
7955           tree body;
7956           current_function_decl = decl;
7957           body = build_jni_stub (decl);
7958           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7959         }
7960
7961       java_complete_expand_method (decl);
7962     }
7963
7964   /* If there is indeed a <clinit>, fully expand it now */
7965   if (clinit)
7966     {
7967       /* Prevent the use of `this' inside <clinit> */
7968       ctxp->explicit_constructor_p = 1;
7969       java_complete_expand_method (clinit);
7970       ctxp->explicit_constructor_p = 0;
7971     }
7972
7973   /* We might have generated a class$ that we now want to expand */
7974   if (TYPE_DOT_CLASS (current_class))
7975     java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7976
7977   /* Now verify constructor circularity (stop after the first one we
7978      prove wrong.) */
7979   if (!CLASS_INTERFACE (class_decl))
7980     for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7981       if (DECL_CONSTRUCTOR_P (decl)
7982           && verify_constructor_circularity (decl, decl))
7983         break;
7984 }
7985
7986 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7987    safely used in some other methods/constructors.  */
7988
7989 static tree
7990 maybe_generate_pre_expand_clinit (tree class_type)
7991 {
7992   tree current, mdecl;
7993
7994   if (!TYPE_CLINIT_STMT_LIST (class_type))
7995     return NULL_TREE;
7996
7997   /* Go through all static fields and pre expand them */
7998   for (current = TYPE_FIELDS (class_type); current;
7999        current = TREE_CHAIN (current))
8000     if (FIELD_STATIC (current))
8001       build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
8002
8003   /* Then build the <clinit> method */
8004   mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
8005                                     clinit_identifier_node, end_params_node);
8006   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
8007                        mdecl, NULL_TREE);
8008   start_artificial_method_body (mdecl);
8009
8010   /* We process the list of assignment we produced as the result of
8011      the declaration of initialized static field and add them as
8012      statement to the <clinit> method. */
8013   for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
8014        current = TREE_CHAIN (current))
8015     {
8016       tree stmt = current;
8017       /* We build the assignment expression that will initialize the
8018          field to its value. There are strict rules on static
8019          initializers (8.5). FIXME */
8020       if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
8021         stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
8022       java_method_add_stmt (mdecl, stmt);
8023     }
8024
8025   end_artificial_method_body (mdecl);
8026
8027   /* Now we want to place <clinit> as the last method (because we need
8028      it at least for interface so that it doesn't interfere with the
8029      dispatch table based lookup. */
8030   if (TREE_CHAIN (TYPE_METHODS (class_type)))
8031     {
8032       current = TREE_CHAIN (TYPE_METHODS (class_type));
8033       TYPE_METHODS (class_type) = current;
8034
8035       while (TREE_CHAIN (current))
8036         current = TREE_CHAIN (current);
8037
8038       TREE_CHAIN (current) = mdecl;
8039       TREE_CHAIN (mdecl) = NULL_TREE;
8040     }
8041
8042   return mdecl;
8043 }
8044
8045 /* Analyzes a method body and look for something that isn't a
8046    MODIFY_EXPR with a constant value.  */
8047
8048 static int
8049 analyze_clinit_body (tree this_class, tree bbody)
8050 {
8051   while (bbody)
8052     switch (TREE_CODE (bbody))
8053       {
8054       case BLOCK:
8055         bbody = BLOCK_EXPR_BODY (bbody);
8056         break;
8057
8058       case EXPR_WITH_FILE_LOCATION:
8059         bbody = EXPR_WFL_NODE (bbody);
8060         break;
8061
8062       case COMPOUND_EXPR:
8063         if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
8064           return 1;
8065         bbody = TREE_OPERAND (bbody, 1);
8066         break;
8067
8068       case MODIFY_EXPR:
8069         /* If we're generating to class file and we're dealing with an
8070            array initialization, we return 1 to keep <clinit> */
8071         if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
8072             && flag_emit_class_files)
8073           return 1;
8074
8075         /* There are a few cases where we're required to keep
8076            <clinit>:
8077            - If this is an assignment whose operand is not constant,
8078            - If this is an assignment to a non-initialized field,
8079            - If this field is not a member of the current class.
8080         */
8081         return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8082                 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8083                 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8084
8085       default:
8086         return 1;
8087       }
8088   return 0;
8089 }
8090
8091
8092 /* See whether we could get rid of <clinit>. Criteria are: all static
8093    final fields have constant initial values and the body of <clinit>
8094    is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8095
8096 static int
8097 maybe_yank_clinit (tree mdecl)
8098 {
8099   tree type, current;
8100   tree fbody, bbody;
8101
8102   if (!DECL_CLINIT_P (mdecl))
8103     return 0;
8104
8105   /* If the body isn't empty, then we keep <clinit>. Note that if
8106      we're emitting classfiles, this isn't enough not to rule it
8107      out. */
8108   fbody = DECL_FUNCTION_BODY (mdecl);
8109   bbody = BLOCK_EXPR_BODY (fbody);
8110   if (bbody && bbody != error_mark_node)
8111     bbody = BLOCK_EXPR_BODY (bbody);
8112   else
8113     return 0;
8114   if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8115     return 0;
8116
8117   type = DECL_CONTEXT (mdecl);
8118   current = TYPE_FIELDS (type);
8119
8120   for (current = (current ? TREE_CHAIN (current) : current);
8121        current; current = TREE_CHAIN (current))
8122     {
8123       tree f_init;
8124
8125       /* We're not interested in non-static fields.  */
8126       if (!FIELD_STATIC (current))
8127         continue;
8128
8129       /* Nor in fields without initializers. */
8130       f_init = DECL_INITIAL (current);
8131       if (f_init == NULL_TREE)
8132         continue;
8133
8134       /* Anything that isn't String or a basic type is ruled out -- or
8135          if we know how to deal with it (when doing things natively) we
8136          should generated an empty <clinit> so that SUID are computed
8137          correctly. */
8138       if (! JSTRING_TYPE_P (TREE_TYPE (current))
8139           && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8140         return 0;
8141
8142       if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8143         return 0;
8144     }
8145
8146   /* Now we analyze the method body and look for something that
8147      isn't a MODIFY_EXPR */
8148   if (!IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8149     return 0;
8150
8151   /* Get rid of <clinit> in the class' list of methods */
8152   if (TYPE_METHODS (type) == mdecl)
8153     TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8154   else
8155     for (current = TYPE_METHODS (type); current;
8156          current = TREE_CHAIN (current))
8157       if (TREE_CHAIN (current) == mdecl)
8158         {
8159           TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8160           break;
8161         }
8162
8163   return 1;
8164 }
8165
8166 /* Install the argument from MDECL. Suitable to completion and
8167    expansion of mdecl's body.  */
8168
8169 void
8170 start_complete_expand_method (tree mdecl)
8171 {
8172   tree tem;
8173
8174   pushlevel (1);                /* Prepare for a parameter push */
8175   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8176   DECL_ARGUMENTS (mdecl) = tem;
8177
8178   for (; tem; tem = TREE_CHAIN (tem))
8179     {
8180       /* TREE_CHAIN (tem) will change after pushdecl. */
8181       tree next = TREE_CHAIN (tem);
8182       tree type = TREE_TYPE (tem);
8183       if (targetm.calls.promote_prototypes (type)
8184           && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8185           && INTEGRAL_TYPE_P (type))
8186         type = integer_type_node;
8187       DECL_ARG_TYPE (tem) = type;
8188       layout_decl (tem, 0);
8189       pushdecl (tem);
8190       /* Re-install the next so that the list is kept and the loop
8191          advances. */
8192       TREE_CHAIN (tem) = next;
8193     }
8194   pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8195   input_location = DECL_SOURCE_LOCATION (mdecl);
8196   build_result_decl (mdecl);
8197 }
8198
8199
8200 /* Complete and expand a method.  */
8201
8202 static void
8203 java_complete_expand_method (tree mdecl)
8204 {
8205   tree fbody, block_body, exception_copy;
8206
8207   current_function_decl = mdecl;
8208   /* Fix constructors before expanding them */
8209   if (DECL_CONSTRUCTOR_P (mdecl))
8210     fix_constructors (mdecl);
8211
8212   /* Expand functions that have a body */
8213   if (!DECL_FUNCTION_BODY (mdecl))
8214     return;
8215
8216   fbody = DECL_FUNCTION_BODY (mdecl);
8217   block_body = BLOCK_EXPR_BODY (fbody);
8218   exception_copy = NULL_TREE;
8219
8220   current_function_decl = mdecl;
8221
8222   if (! quiet_flag)
8223     fprintf (stderr, " [%s.",
8224              lang_printable_name (DECL_CONTEXT (mdecl), 0));
8225   announce_function (mdecl);
8226   if (! quiet_flag)
8227     fprintf (stderr, "]");
8228
8229   /* Prepare the function for tree completion */
8230   start_complete_expand_method (mdecl);
8231
8232   /* Install the current this */
8233   current_this = (!METHOD_STATIC (mdecl) ?
8234                   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8235
8236   /* Purge the `throws' list of unchecked exceptions (we save a copy
8237      of the list and re-install it later.) */
8238   exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8239   purge_unchecked_exceptions (mdecl);
8240
8241   /* Install exceptions thrown with `throws' */
8242   PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8243
8244   if (block_body != NULL_TREE)
8245     {
8246       block_body = java_complete_tree (block_body);
8247
8248       /* Before we check initialization, attached all class initialization
8249          variable to the block_body */
8250       htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8251                      attach_init_test_initialization_flags, block_body);
8252
8253       if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8254         {
8255           check_for_initialization (block_body, mdecl);
8256
8257           /* Go through all the flags marking the initialization of
8258              static variables and see whether they're definitively
8259              assigned, in which case the type is remembered as
8260              definitively initialized in MDECL. */
8261           if (STATIC_CLASS_INIT_OPT_P ())
8262             {
8263               /* Always register the context as properly initialized in
8264                  MDECL. This used with caution helps removing extra
8265                  initialization of self. */
8266               if (METHOD_STATIC (mdecl))
8267                 {
8268                   *(htab_find_slot
8269                     (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8270                      DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8271                 }
8272             }
8273         }
8274       ctxp->explicit_constructor_p = 0;
8275     }
8276
8277   BLOCK_EXPR_BODY (fbody) = block_body;
8278
8279   /* If we saw a return but couldn't evaluate it properly, we'll have
8280      an error_mark_node here. */
8281   if (block_body != error_mark_node
8282       && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8283       && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8284       && !flag_emit_xref)
8285     missing_return_error (current_function_decl);
8286
8287   /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8288   maybe_yank_clinit (mdecl);
8289
8290   /* Pop the current level, with special measures if we found errors. */
8291   if (java_error_count)
8292     pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8293   poplevel (1, 0, 1);
8294
8295   /* Pop the exceptions and sanity check */
8296   POP_EXCEPTIONS();
8297   if (currently_caught_type_list)
8298     abort ();
8299
8300   /* Restore the copy of the list of exceptions if emitting xrefs. */
8301   DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8302 }
8303
8304 /* For with each class for which there's code to generate. */
8305
8306 static void
8307 java_expand_method_bodies (tree class)
8308 {
8309   tree decl;
8310   for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8311     {
8312       tree block;
8313
8314       if (! DECL_FUNCTION_BODY (decl))
8315         continue;
8316
8317       current_function_decl = decl;
8318
8319       block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8320
8321       /* Save the function body for gimplify and inlining.  */
8322       DECL_SAVED_TREE (decl) = block;
8323
8324       /* It's time to assign the variable flagging static class
8325          initialization based on which classes invoked static methods
8326          are definitely initializing. This should be flagged. */
8327       if (STATIC_CLASS_INIT_OPT_P ())
8328         {
8329           tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8330           for (; list != NULL_TREE;  list = TREE_CHAIN (list))
8331             {
8332               /* Executed for each statement calling a static function.
8333                  LIST is a TREE_LIST whose PURPOSE is the called function
8334                  and VALUE is a compound whose second operand can be patched
8335                  with static class initialization flag assignments.  */
8336
8337               tree called_method = TREE_PURPOSE (list);
8338               tree compound = TREE_VALUE (list);
8339               tree assignment_compound_list
8340                 = build_tree_list (called_method, NULL);
8341
8342               /* For each class definitely initialized in
8343                  CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8344                  assignment to the class initialization flag. */
8345               htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8346                              emit_test_initialization,
8347                              assignment_compound_list);
8348
8349               if (TREE_VALUE (assignment_compound_list))
8350                 TREE_OPERAND (compound, 1)
8351                   = TREE_VALUE (assignment_compound_list);
8352             }
8353         }
8354
8355       /* Expand the function body.  */
8356       source_end_java_method ();
8357     }
8358 }
8359
8360 \f
8361
8362 /* This section of the code deals with accessing enclosing context
8363    fields either directly by using the relevant access to this$<n> or
8364    by invoking an access method crafted for that purpose.  */
8365
8366 /* Build the necessary access from an inner class to an outer
8367    class. This routine could be optimized to cache previous result
8368    (decl, current_class and returned access).  When an access method
8369    needs to be generated, it always takes the form of a read. It might
8370    be later turned into a write by calling outer_field_access_fix.  */
8371
8372 static tree
8373 build_outer_field_access (tree id, tree decl)
8374 {
8375   tree access = NULL_TREE;
8376   tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8377   tree decl_ctx = DECL_CONTEXT (decl);
8378
8379   /* If the immediate enclosing context of the current class is the
8380      field decl's class or inherits from it; build the access as
8381      `this$<n>.<field>'. Note that we will break the `private' barrier
8382      if we're not emitting bytecodes. */
8383   if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8384       && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8385     {
8386       tree thisn = build_current_thisn (current_class);
8387       access = make_qualified_primary (build_wfl_node (thisn),
8388                                        id, EXPR_WFL_LINECOL (id));
8389     }
8390   /* Otherwise, generate access methods to outer this and access the
8391      field (either using an access method or by direct access.) */
8392   else
8393     {
8394       int lc = EXPR_WFL_LINECOL (id);
8395
8396       /* Now we chain the required number of calls to the access$0 to
8397          get a hold to the enclosing instance we need, and then we
8398          build the field access. */
8399       access = build_access_to_thisn (current_class, decl_ctx, lc);
8400
8401       /* If the field is private and we're generating bytecode, then
8402          we generate an access method */
8403       if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8404         {
8405           tree name = build_outer_field_access_methods (decl);
8406           access = build_outer_field_access_expr (lc, decl_ctx,
8407                                                   name, access, NULL_TREE);
8408         }
8409       /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8410          Once again we break the `private' access rule from a foreign
8411          class. */
8412       else
8413         access = make_qualified_primary (access, id, lc);
8414     }
8415   return resolve_expression_name (access, NULL);
8416 }
8417
8418 /* Return a nonzero value if NODE describes an outer field inner
8419    access.  */
8420
8421 static int
8422 outer_field_access_p (tree type, tree decl)
8423 {
8424   if (!INNER_CLASS_TYPE_P (type)
8425       || TREE_CODE (decl) != FIELD_DECL
8426       || DECL_CONTEXT (decl) == type)
8427     return 0;
8428
8429   /* If the inner class extends the declaration context of the field
8430      we're trying to access, then this isn't an outer field access */
8431   if (inherits_from_p (type, DECL_CONTEXT (decl)))
8432     return 0;
8433
8434   for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8435        type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8436     {
8437       if (type == DECL_CONTEXT (decl))
8438         return 1;
8439
8440       if (!DECL_CONTEXT (TYPE_NAME (type)))
8441         {
8442           /* Before we give up, see whether the field is inherited from
8443              the enclosing context we're considering. */
8444           if (inherits_from_p (type, DECL_CONTEXT (decl)))
8445             return 1;
8446           break;
8447         }
8448     }
8449
8450   return 0;
8451 }
8452
8453 /* Return a nonzero value if NODE represents an outer field inner
8454    access that was been already expanded. As a side effect, it returns
8455    the name of the field being accessed and the argument passed to the
8456    access function, suitable for a regeneration of the access method
8457    call if necessary. */
8458
8459 static int
8460 outer_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8461                                tree *arg)
8462 {
8463   int identified = 0;
8464
8465   if (TREE_CODE (node) != CALL_EXPR)
8466     return 0;
8467
8468   /* Well, gcj generates slightly different tree nodes when compiling
8469      to native or bytecodes. It's the case for function calls. */
8470
8471   if (flag_emit_class_files
8472       && TREE_CODE (node) == CALL_EXPR
8473       && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8474     identified = 1;
8475   else if (!flag_emit_class_files)
8476     {
8477       node = TREE_OPERAND (node, 0);
8478
8479       if (node && TREE_OPERAND (node, 0)
8480           && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8481         {
8482           node = TREE_OPERAND (node, 0);
8483           if (TREE_OPERAND (node, 0)
8484               && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8485               && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8486                   (DECL_NAME (TREE_OPERAND (node, 0)))))
8487             identified = 1;
8488         }
8489     }
8490
8491   if (identified && name && arg_type && arg)
8492     {
8493       tree argument = TREE_OPERAND (node, 1);
8494       *name = DECL_NAME (TREE_OPERAND (node, 0));
8495       *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8496       *arg = TREE_VALUE (argument);
8497     }
8498   return identified;
8499 }
8500
8501 /* Detect in NODE an outer field read access from an inner class and
8502    transform it into a write with RHS as an argument. This function is
8503    called from the java_complete_lhs when an assignment to a LHS can
8504    be identified. */
8505
8506 static tree
8507 outer_field_access_fix (tree wfl, tree node, tree rhs)
8508 {
8509   tree name, arg_type, arg;
8510
8511   if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8512     {
8513       node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8514                                             arg_type, name, arg, rhs);
8515       return java_complete_tree (node);
8516     }
8517   return NULL_TREE;
8518 }
8519
8520 /* Construct the expression that calls an access method:
8521      <type>.access$<n>(<arg1> [, <arg2>]);
8522
8523    ARG2 can be NULL and will be omitted in that case. It will denote a
8524    read access.  */
8525
8526 static tree
8527 build_outer_field_access_expr (int lc, tree type, tree access_method_name,
8528                                tree arg1, tree arg2)
8529 {
8530   tree args, cn, access;
8531
8532   args = arg1 ? arg1 :
8533     build_wfl_node (build_current_thisn (current_class));
8534   args = build_tree_list (NULL_TREE, args);
8535
8536   if (arg2)
8537     args = tree_cons (NULL_TREE, arg2, args);
8538
8539   access = build_method_invocation (build_wfl_node (access_method_name), args);
8540   cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8541   return make_qualified_primary (cn, access, lc);
8542 }
8543
8544 static tree
8545 build_new_access_id (void)
8546 {
8547   static int access_n_counter = 1;
8548   char buffer [128];
8549
8550   sprintf (buffer, "access$%d", access_n_counter++);
8551   return get_identifier (buffer);
8552 }
8553
8554 /* Create the static access functions for the outer field DECL. We define a
8555    read:
8556      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8557        return inst$.field;
8558      }
8559    and a write access:
8560      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8561                                      TREE_TYPE (<field>) value$) {
8562        return inst$.field = value$;
8563      }
8564    We should have a usage flags on the DECL so we can lazily turn the ones
8565    we're using for code generation. FIXME.
8566 */
8567
8568 static tree
8569 build_outer_field_access_methods (tree decl)
8570 {
8571   tree id, args, stmt, mdecl;
8572
8573   if (FIELD_INNER_ACCESS_P (decl))
8574     return FIELD_INNER_ACCESS (decl);
8575
8576   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8577
8578   /* Create the identifier and a function named after it. */
8579   id = build_new_access_id ();
8580
8581   /* The identifier is marked as bearing the name of a generated write
8582      access function for outer field accessed from inner classes. */
8583   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8584
8585   /* Create the read access */
8586   args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8587   TREE_CHAIN (args) = end_params_node;
8588   stmt = make_qualified_primary (build_wfl_node (inst_id),
8589                                  build_wfl_node (DECL_NAME (decl)), 0);
8590   stmt = build_return (0, stmt);
8591   mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8592                                            TREE_TYPE (decl), id, args, stmt);
8593   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8594
8595   /* Create the write access method. No write access for final variable */
8596   if (!FIELD_FINAL (decl))
8597     {
8598       args = build_tree_list (inst_id,
8599                               build_pointer_type (DECL_CONTEXT (decl)));
8600       TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8601       TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8602       stmt = make_qualified_primary (build_wfl_node (inst_id),
8603                                      build_wfl_node (DECL_NAME (decl)), 0);
8604       stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8605                                                 build_wfl_node (wpv_id)));
8606       mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8607                                                TREE_TYPE (decl), id,
8608                                                args, stmt);
8609     }
8610   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8611
8612   /* Return the access name */
8613   return FIELD_INNER_ACCESS (decl) = id;
8614 }
8615
8616 /* Build an field access method NAME.  */
8617
8618 static tree
8619 build_outer_field_access_method (tree class, tree type, tree name,
8620                                  tree args, tree body)
8621 {
8622   tree saved_current_function_decl, mdecl;
8623
8624   /* Create the method */
8625   mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8626   fix_method_argument_names (args, mdecl);
8627   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8628
8629   /* Attach the method body. */
8630   saved_current_function_decl = current_function_decl;
8631   start_artificial_method_body (mdecl);
8632   java_method_add_stmt (mdecl, body);
8633   end_artificial_method_body (mdecl);
8634   current_function_decl = saved_current_function_decl;
8635
8636   return mdecl;
8637 }
8638
8639 \f
8640 /* This section deals with building access function necessary for
8641    certain kinds of method invocation from inner classes.  */
8642
8643 static tree
8644 build_outer_method_access_method (tree decl)
8645 {
8646   tree saved_current_function_decl, mdecl;
8647   tree args = NULL_TREE, call_args = NULL_TREE;
8648   tree carg, id, body, class;
8649   char buffer [80];
8650   int parm_id_count = 0;
8651
8652   /* Test this abort with an access to a private field */
8653   if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8654     abort ();
8655
8656   /* Check the cache first */
8657   if (DECL_FUNCTION_INNER_ACCESS (decl))
8658     return DECL_FUNCTION_INNER_ACCESS (decl);
8659
8660   class = DECL_CONTEXT (decl);
8661
8662   /* Obtain an access identifier and mark it */
8663   id = build_new_access_id ();
8664   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8665
8666   carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8667   /* Create the arguments, as much as the original */
8668   for (; carg && carg != end_params_node;
8669        carg = TREE_CHAIN (carg))
8670     {
8671       sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8672       args = chainon (args, build_tree_list (get_identifier (buffer),
8673                                              TREE_VALUE (carg)));
8674     }
8675   args = chainon (args, end_params_node);
8676
8677   /* Create the method */
8678   mdecl = create_artificial_method (class, ACC_STATIC,
8679                                     TREE_TYPE (TREE_TYPE (decl)), id, args);
8680   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8681   /* There is a potential bug here. We should be able to use
8682      fix_method_argument_names, but then arg names get mixed up and
8683      eventually a constructor will have its this$0 altered and the
8684      outer context won't be assignment properly. The testcase is
8685      stub.java FIXME */
8686   TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8687
8688   /* Attach the method body. */
8689   saved_current_function_decl = current_function_decl;
8690   start_artificial_method_body (mdecl);
8691
8692   /* The actual method invocation uses the same args. When invoking a
8693      static methods that way, we don't want to skip the first
8694      argument. */
8695   carg = args;
8696   if (!METHOD_STATIC (decl))
8697     carg = TREE_CHAIN (carg);
8698   for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8699     call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8700                            call_args);
8701
8702   body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8703                                   call_args);
8704   if (!METHOD_STATIC (decl))
8705     body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8706                                    body, 0);
8707   if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8708     body = build_return (0, body);
8709   java_method_add_stmt (mdecl,body);
8710   end_artificial_method_body (mdecl);
8711   current_function_decl = saved_current_function_decl;
8712
8713   /* Back tag the access function so it know what it accesses */
8714   DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8715
8716   /* Tag the current method so it knows it has an access generated */
8717   return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8718 }
8719
8720 \f
8721 /* This section of the code deals with building expressions to access
8722    the enclosing instance of an inner class. The enclosing instance is
8723    kept in a generated field called this$<n>, with <n> being the
8724    inner class nesting level (starting from 0.)  */
8725
8726 /* Build an access to a given this$<n>, always chaining access call to
8727    others. Access methods to this$<n> are build on the fly if
8728    necessary. This CAN'T be used to solely access this$<n-1> from
8729    this$<n> (which alway yield to special cases and optimization, see
8730    for example build_outer_field_access).  */
8731
8732 static tree
8733 build_access_to_thisn (tree from, tree to, int lc)
8734 {
8735   tree access = NULL_TREE;
8736
8737   while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8738     {
8739       if (!access)
8740         {
8741           access = build_current_thisn (from);
8742           access = build_wfl_node (access);
8743         }
8744       else
8745         {
8746           tree access0_wfl, cn;
8747
8748           maybe_build_thisn_access_method (from);
8749           access0_wfl = build_wfl_node (access0_identifier_node);
8750           cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8751           EXPR_WFL_LINECOL (access0_wfl) = lc;
8752           access = build_tree_list (NULL_TREE, access);
8753           access = build_method_invocation (access0_wfl, access);
8754           access = make_qualified_primary (cn, access, lc);
8755         }
8756
8757       /* If FROM isn't an inner class, that's fine, we've done enough.
8758          What we're looking for can be accessed from there.  */
8759       from = DECL_CONTEXT (TYPE_NAME (from));
8760       if (!from)
8761         break;
8762       from = TREE_TYPE (from);
8763     }
8764   return access;
8765 }
8766
8767 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8768    is returned if nothing needs to be generated. Otherwise, the method
8769    generated and a method decl is returned.
8770
8771    NOTE: These generated methods should be declared in a class file
8772    attribute so that they can't be referred to directly.  */
8773
8774 static tree
8775 maybe_build_thisn_access_method (tree type)
8776 {
8777   tree mdecl, args, stmt, rtype;
8778   tree saved_current_function_decl;
8779
8780   /* If TYPE is a top-level class, no access method is required.
8781      If there already is such an access method, bail out. */
8782   if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8783     return NULL_TREE;
8784
8785   /* We generate the method. The method looks like:
8786      static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8787   */
8788   args = build_tree_list (inst_id, build_pointer_type (type));
8789   TREE_CHAIN (args) = end_params_node;
8790   rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8791   mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8792                                     access0_identifier_node, args);
8793   fix_method_argument_names (args, mdecl);
8794   layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8795   stmt = build_current_thisn (type);
8796   stmt = make_qualified_primary (build_wfl_node (inst_id),
8797                                  build_wfl_node (stmt), 0);
8798   stmt = build_return (0, stmt);
8799
8800   saved_current_function_decl = current_function_decl;
8801   start_artificial_method_body (mdecl);
8802   java_method_add_stmt (mdecl, stmt);
8803   end_artificial_method_body (mdecl);
8804   current_function_decl = saved_current_function_decl;
8805
8806   CLASS_ACCESS0_GENERATED_P (type) = 1;
8807
8808   return mdecl;
8809 }
8810
8811 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8812    the first level of innerclassing. this$1 for the next one, etc...
8813    This function can be invoked with TYPE to NULL, available and then
8814    has to count the parser context.  */
8815
8816 static GTY(()) tree saved_thisn;
8817 static GTY(()) tree saved_type;
8818
8819 static tree
8820 build_current_thisn (tree type)
8821 {
8822   static int saved_i = -1;
8823   static int saved_type_i = 0;
8824   tree decl;
8825   char buffer [24];
8826   int i = 0;
8827
8828   if (type)
8829     {
8830       if (type == saved_type)
8831         i = saved_type_i;
8832       else
8833         {
8834           for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8835                decl; decl = DECL_CONTEXT (decl), i++)
8836             ;
8837
8838           saved_type = type;
8839           saved_type_i = i;
8840         }
8841     }
8842   else
8843     i = list_length (GET_CPC_LIST ())-2;
8844
8845   if (i == saved_i)
8846     return saved_thisn;
8847
8848   sprintf (buffer, "this$%d", i);
8849   saved_i = i;
8850   saved_thisn = get_identifier (buffer);
8851   return saved_thisn;
8852 }
8853
8854 /* Return the assignment to the hidden enclosing context `this$<n>'
8855    by the second incoming parameter to the innerclass constructor. The
8856    form used is `this.this$<n> = this$<n>;'.  */
8857
8858 static tree
8859 build_thisn_assign (void)
8860 {
8861   if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8862     {
8863       tree thisn = build_current_thisn (current_class);
8864       tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8865                                          build_wfl_node (thisn), 0);
8866       tree rhs = build_wfl_node (thisn);
8867 #ifdef USE_MAPPED_LOCATION
8868       SET_EXPR_LOCATION (lhs, input_location);
8869 #else
8870       EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8871 #endif
8872       return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8873     }
8874   return NULL_TREE;
8875 }
8876
8877 \f
8878 /* Building the synthetic `class$' used to implement the `.class' 1.1
8879    extension for non primitive types. This method looks like:
8880
8881     static Class class$(String type) throws NoClassDefFoundError
8882     {
8883       try {return (java.lang.Class.forName (String));}
8884       catch (ClassNotFoundException e) {
8885         throw new NoClassDefFoundError(e.getMessage());}
8886     } */
8887
8888 static GTY(()) tree get_message_wfl;
8889 static GTY(()) tree type_parm_wfl;
8890
8891 static tree
8892 build_dot_class_method (tree class)
8893 {
8894 #define BWF(S) build_wfl_node (get_identifier ((S)))
8895 #ifdef USE_MAPPED_LOCATION
8896 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8897 #else
8898 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8899 #endif
8900   tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8901   tree stmt, throw_stmt;
8902
8903   if (!get_message_wfl)
8904     {
8905       get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8906       type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8907     }
8908
8909   /* Build the arguments */
8910   args = build_tree_list (get_identifier ("type$"),
8911                           build_pointer_type (string_type_node));
8912   TREE_CHAIN (args) = end_params_node;
8913
8914   /* Build the qualified name java.lang.Class.forName */
8915   tmp = MQN (MQN (MQN (BWF ("java"),
8916                        BWF ("lang")), BWF ("Class")), BWF ("forName"));
8917
8918   /* Create the "class$" function */
8919   mdecl = create_artificial_method (class, ACC_STATIC,
8920                                     build_pointer_type (class_type_node),
8921                                     classdollar_identifier_node, args);
8922   qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8923                    BWF ("NoClassDefFoundError"));
8924   DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8925   register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8926   JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8927     &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8928
8929   /* We start by building the try block. We need to build:
8930        return (java.lang.Class.forName (type)); */
8931   stmt = build_method_invocation (tmp,
8932                                   build_tree_list (NULL_TREE, type_parm_wfl));
8933   stmt = build_return (0, stmt);
8934
8935   /* Now onto the catch block. We start by building the expression
8936      throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8937 #ifdef USE_MAPPED_LOCATION
8938   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8939                                     get_message_wfl, UNKNOWN_LOCATION);
8940 #else
8941   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8942                                     get_message_wfl, 0);
8943 #endif
8944   throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8945
8946   /* Build new NoClassDefFoundError (_.getMessage) */
8947   throw_stmt = build_new_invocation
8948     (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8949      build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8950
8951   /* Build the throw, (it's too early to use BUILD_THROW) */
8952   throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8953
8954   /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8955   qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8956                    BWF ("ClassNotFoundException"));
8957   stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8958
8959   fix_method_argument_names (args, mdecl);
8960   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8961   saved_current_function_decl = current_function_decl;
8962   start_artificial_method_body (mdecl);
8963   java_method_add_stmt (mdecl, stmt);
8964   end_artificial_method_body (mdecl);
8965   current_function_decl = saved_current_function_decl;
8966   TYPE_DOT_CLASS (class) = mdecl;
8967
8968   return mdecl;
8969 }
8970
8971 static tree
8972 build_dot_class_method_invocation (tree this_class, tree type)
8973 {
8974   tree dot_class_method = TYPE_DOT_CLASS (this_class);
8975   tree sig_id, s, t;
8976
8977   if (TYPE_ARRAY_P (type))
8978     sig_id = build_java_signature (type);
8979   else
8980     sig_id = DECL_NAME (TYPE_NAME (type));
8981
8982   /* Ensure that the proper name separator is used */
8983   sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8984                                IDENTIFIER_LENGTH (sig_id));
8985
8986   s = build_string (IDENTIFIER_LENGTH (sig_id),
8987                     IDENTIFIER_POINTER (sig_id));
8988   t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
8989                                build_tree_list (NULL_TREE, s));
8990   if (DECL_CONTEXT (dot_class_method) != this_class)
8991     {
8992       tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
8993       t = make_qualified_primary (build_wfl_node (class_name), t, 0);
8994     }
8995   return t;
8996 }
8997
8998 /* This section of the code deals with constructor.  */
8999
9000 /* Craft a body for default constructor. Patch existing constructor
9001    bodies with call to super() and field initialization statements if
9002    necessary.  */
9003
9004 static void
9005 fix_constructors (tree mdecl)
9006 {
9007   tree iii;                     /* Instance Initializer Invocation */
9008   tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9009   tree thisn_assign, compound = NULL_TREE;
9010   tree class_type = DECL_CONTEXT (mdecl);
9011
9012   if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9013     return;
9014   DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9015
9016   if (!*bodyp)
9017     {
9018       /* It is an error for the compiler to generate a default
9019          constructor if the superclass doesn't have a constructor that
9020          takes no argument, or the same args for an anonymous class */
9021       if (verify_constructor_super (mdecl))
9022         {
9023           tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9024           tree save = DECL_NAME (mdecl);
9025           const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9026           DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9027           parse_error_context
9028             (lookup_cl (TYPE_NAME (class_type)),
9029              "No constructor matching %qs found in class %qs",
9030              lang_printable_name (mdecl, 2), n);
9031           DECL_NAME (mdecl) = save;
9032         }
9033
9034       /* The constructor body must be crafted by hand. It's the
9035          constructor we defined when we realize we didn't have the
9036          CLASSNAME() constructor */
9037       start_artificial_method_body (mdecl);
9038
9039       /* Insert an assignment to the this$<n> hidden field, if
9040          necessary */
9041       if ((thisn_assign = build_thisn_assign ()))
9042         java_method_add_stmt (mdecl, thisn_assign);
9043
9044       /* We don't generate a super constructor invocation if we're
9045          compiling java.lang.Object. build_super_invocation takes care
9046          of that. */
9047       java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9048
9049       /* FIXME */
9050       if ((iii = build_instinit_invocation (class_type)))
9051         java_method_add_stmt (mdecl, iii);
9052
9053       end_artificial_method_body (mdecl);
9054     }
9055   /* Search for an explicit constructor invocation */
9056   else
9057     {
9058       int found = 0;
9059       int invokes_this = 0;
9060       tree main_block = BLOCK_EXPR_BODY (*bodyp);
9061
9062       while (*bodyp)
9063         {
9064           tree body = *bodyp;
9065           switch (TREE_CODE (body))
9066             {
9067             case CALL_EXPR:
9068               found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9069               if (CALL_THIS_CONSTRUCTOR_P (body))
9070                 invokes_this = 1;
9071               break;
9072             case COMPOUND_EXPR:
9073             case EXPR_WITH_FILE_LOCATION:
9074               bodyp = &TREE_OPERAND (body, 0);
9075               continue;
9076             case BLOCK:
9077               bodyp = &BLOCK_EXPR_BODY (body);
9078               continue;
9079             default:
9080               break;
9081             }
9082           break;
9083         }
9084
9085       /* Generate the assignment to this$<n>, if necessary */
9086       if ((thisn_assign = build_thisn_assign ()))
9087         compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9088
9089       /* The constructor is missing an invocation of super() */
9090       if (!found)
9091         compound = add_stmt_to_compound (compound, NULL_TREE,
9092                                          build_super_invocation (mdecl));
9093       /* Explicit super() invocation should take place before the
9094          instance initializer blocks. */
9095       else
9096         {
9097           compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9098           *bodyp = build_java_empty_stmt ();
9099         }
9100
9101       DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9102
9103       /* Insert the instance initializer block right after. */
9104       if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9105         compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9106
9107       /* Fix the constructor main block if we're adding extra stmts */
9108       if (compound)
9109         {
9110           compound = add_stmt_to_compound (compound, NULL_TREE,
9111                                            BLOCK_EXPR_BODY (main_block));
9112           BLOCK_EXPR_BODY (main_block) = compound;
9113         }
9114     }
9115 }
9116
9117 /* Browse constructors in the super class, searching for a constructor
9118    that doesn't take any argument. Return 0 if one is found, 1
9119    otherwise.  If the current class is an anonymous inner class, look
9120    for something that has the same signature. */
9121
9122 static int
9123 verify_constructor_super (tree mdecl)
9124 {
9125   tree class = CLASSTYPE_SUPER (current_class);
9126   int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9127   tree sdecl;
9128
9129   if (!class)
9130     return 0;
9131
9132   if (ANONYMOUS_CLASS_P (current_class))
9133     {
9134       tree mdecl_arg_type;
9135       SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9136       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9137         if (DECL_CONSTRUCTOR_P (sdecl))
9138           {
9139             tree m_arg_type;
9140             tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9141             if (super_inner)
9142               arg_type = TREE_CHAIN (arg_type);
9143             for (m_arg_type = mdecl_arg_type;
9144                  (arg_type != end_params_node
9145                   && m_arg_type != end_params_node);
9146                  arg_type = TREE_CHAIN (arg_type),
9147                    m_arg_type = TREE_CHAIN (m_arg_type))
9148               if (!valid_method_invocation_conversion_p
9149                      (TREE_VALUE (arg_type),
9150                       TREE_VALUE (m_arg_type)))
9151                 break;
9152
9153             if (arg_type == end_params_node && m_arg_type == end_params_node)
9154               return 0;
9155           }
9156     }
9157   else
9158     {
9159       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9160         {
9161           tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9162           if (super_inner)
9163             arg = TREE_CHAIN (arg);
9164           if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9165             return 0;
9166         }
9167     }
9168   return 1;
9169 }
9170
9171 /* Generate code for all context remembered for code generation.  */
9172
9173 static GTY(()) tree reversed_class_list;
9174 void
9175 java_expand_classes (void)
9176 {
9177   int save_error_count = 0;
9178   static struct parser_ctxt *cur_ctxp = NULL;
9179   location_t save_location;
9180
9181   java_parse_abort_on_error ();
9182   if (!(ctxp = ctxp_for_generation))
9183     return;
9184   java_layout_classes ();
9185   java_parse_abort_on_error ();
9186   save_location = input_location;
9187
9188   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9189     {
9190       tree current;
9191       for (current = cur_ctxp->class_list; 
9192            current; 
9193            current = TREE_CHAIN (current))
9194         gen_indirect_dispatch_tables (TREE_TYPE (current));
9195     }
9196   
9197   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9198     {
9199       ctxp = cur_ctxp;
9200       input_location = ctxp->file_start_location;
9201       lang_init_source (2);            /* Error msgs have method prototypes */
9202       java_complete_expand_classes (); /* Complete and expand classes */
9203       java_parse_abort_on_error ();
9204     }
9205   input_location = save_location;
9206
9207   /* Find anonymous classes and expand their constructor. This extra pass is
9208      necessary because the constructor itself is only generated when the
9209      method in which it is defined is expanded. */
9210   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9211     {
9212       tree current;
9213       ctxp = cur_ctxp;
9214       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9215         {
9216           output_class = current_class = TREE_TYPE (current);
9217           if (ANONYMOUS_CLASS_P (current_class))
9218             {
9219               tree d;
9220               for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9221                 {
9222                   if (DECL_CONSTRUCTOR_P (d))
9223                     {
9224                       java_complete_expand_method (d);
9225                       break;    /* There is only one constructor. */
9226                     }
9227                 }
9228             }
9229         }
9230     }
9231
9232   /* Expanding the constructors of anonymous classes generates access
9233      methods.  Scan all the methods looking for null DECL_RESULTs --
9234      this will be the case if a method hasn't been expanded.  */
9235   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9236     {
9237       tree current;
9238       ctxp = cur_ctxp;
9239       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9240         {
9241           tree d;
9242           output_class = current_class = TREE_TYPE (current);
9243           for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9244             {
9245               if (DECL_RESULT (d) == NULL_TREE)
9246                 java_complete_expand_method (d);
9247             }
9248         }
9249     }
9250
9251   /* ???  Instead of all this we could iterate around the list of
9252      classes until there were no more un-expanded methods.  It would
9253      take a little longer -- one pass over the whole list of methods
9254      -- but it would be simpler.  Like this:  */
9255 #if 0
9256     {
9257       int something_changed;
9258     
9259       do
9260         {
9261           something_changed = 0;
9262           for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9263             {
9264               tree current;
9265               ctxp = cur_ctxp;
9266               for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9267                 {
9268                   tree d;
9269                   output_class = current_class = TREE_TYPE (current);
9270                   for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9271                     {
9272                       if (DECL_RESULT (d) == NULL_TREE)
9273                         {
9274                           something_changed = 1;
9275                           java_complete_expand_method (d);
9276                         }
9277                     }
9278                 }
9279             }
9280         }
9281       while (something_changed);
9282     }
9283 #endif
9284
9285   /* If we've found error at that stage, don't try to generate
9286      anything, unless we're emitting xrefs or checking the syntax only
9287      (but not using -fsyntax-only for the purpose of generating
9288      bytecode. */
9289   if (java_error_count && !flag_emit_xref
9290       && (!flag_syntax_only && !flag_emit_class_files))
9291     return;
9292
9293   /* Now things are stable, go for generation of the class data. */
9294
9295   /* We pessimistically marked all methods and fields external until
9296      we knew what set of classes we were planning to compile.  Now mark
9297      those that will be generated locally as not external.  */
9298   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9299     {
9300       tree current;
9301       ctxp = cur_ctxp;
9302       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9303         java_mark_class_local (TREE_TYPE (current));
9304     }
9305
9306   /* Compile the classes.  */
9307   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9308     {
9309       tree current;
9310       reversed_class_list = NULL;
9311
9312       ctxp = cur_ctxp;
9313
9314       /* We write out the classes in reverse order.  This ensures that
9315          inner classes are written before their containing classes,
9316          which is important for parallel builds.  Otherwise, the
9317          class file for the outer class may be found, but the class
9318          file for the inner class may not be present.  In that
9319          situation, the compiler cannot fall back to the original
9320          source, having already read the outer class, so we must
9321          prevent that situation.  */
9322       for (current = ctxp->class_list;
9323            current;
9324            current = TREE_CHAIN (current))
9325         reversed_class_list
9326           = tree_cons (NULL_TREE, current, reversed_class_list);
9327
9328       for (current = reversed_class_list;
9329            current;
9330            current = TREE_CHAIN (current))
9331         {
9332           output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9333           if (flag_emit_class_files)
9334             write_classfile (current_class);
9335           if (flag_emit_xref)
9336             expand_xref (current_class);
9337           else if (! flag_syntax_only)
9338             java_expand_method_bodies (current_class);
9339         }
9340     }
9341 }
9342
9343 void
9344 java_finish_classes (void)
9345 {
9346   static struct parser_ctxt *cur_ctxp = NULL;
9347   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9348     {
9349       tree current;
9350       ctxp = cur_ctxp;
9351       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9352         {
9353           output_class = current_class = TREE_TYPE (current);
9354           finish_class ();
9355         }
9356     }
9357 }
9358
9359 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9360    a tree list node containing RIGHT. Fore coming RIGHTs will be
9361    chained to this hook. LOCATION contains the location of the
9362    separating `.' operator.  */
9363
9364 static tree
9365 make_qualified_primary (tree primary, tree right, int location)
9366 {
9367   tree wfl;
9368
9369   if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9370     wfl = build_wfl_wrap (primary, location);
9371   else
9372     {
9373       wfl = primary;
9374       /* If wfl wasn't qualified, we build a first anchor */
9375       if (!EXPR_WFL_QUALIFICATION (wfl))
9376         EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9377     }
9378
9379   /* And chain them */
9380   EXPR_WFL_LINECOL (right) = location;
9381   chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9382   PRIMARY_P (wfl) =  1;
9383   return wfl;
9384 }
9385
9386 /* Simple merge of two name separated by a `.' */
9387
9388 static tree
9389 merge_qualified_name (tree left, tree right)
9390 {
9391   tree node;
9392   if (!left && !right)
9393     return NULL_TREE;
9394
9395   if (!left)
9396     return right;
9397
9398   if (!right)
9399     return left;
9400
9401   obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9402                 IDENTIFIER_LENGTH (left));
9403   obstack_1grow (&temporary_obstack, '.');
9404   obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9405                  IDENTIFIER_LENGTH (right));
9406   node =  get_identifier (obstack_base (&temporary_obstack));
9407   obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9408   QUALIFIED_P (node) = 1;
9409   return node;
9410 }
9411
9412 /* Merge the two parts of a qualified name into LEFT.  Set the
9413    location information of the resulting node to LOCATION, usually
9414    inherited from the location information of the `.' operator. */
9415
9416 static tree
9417 make_qualified_name (tree left, tree right,
9418 #ifdef USE_MAPPED_LOCATION
9419                      source_location location
9420 #else
9421                      int location
9422 #endif
9423                      )
9424 {
9425 #ifdef USE_COMPONENT_REF
9426   tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9427   SET_EXPR_LOCATION (node, location);
9428   return node;
9429 #else
9430   tree left_id = EXPR_WFL_NODE (left);
9431   tree right_id = EXPR_WFL_NODE (right);
9432   tree wfl, merge;
9433
9434   merge = merge_qualified_name (left_id, right_id);
9435
9436   /* Left wasn't qualified and is now qualified */
9437 #ifdef USE_MAPPED_LOCATION
9438   if (!QUALIFIED_P (left_id))
9439     {
9440       tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9441       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9442     }
9443
9444   wfl = build_expr_wfl (right_id, location);
9445 #else
9446   if (!QUALIFIED_P (left_id))
9447     {
9448       tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9449       EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9450       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9451     }
9452
9453   wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9454   EXPR_WFL_LINECOL (wfl) = location;
9455 #endif
9456   chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9457   EXPR_WFL_NODE (left) = merge;
9458   return left;
9459 #endif
9460 }
9461
9462 /* Extract the last identifier component of the qualified in WFL. The
9463    last identifier is removed from the linked list */
9464
9465 static tree
9466 cut_identifier_in_qualified (tree wfl)
9467 {
9468   tree q;
9469   tree previous = NULL_TREE;
9470   for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9471     if (!TREE_CHAIN (q))
9472       {
9473         if (!previous)
9474           /* Operating on a non qualified qualified WFL.  */
9475           abort ();
9476
9477         TREE_CHAIN (previous) = NULL_TREE;
9478         return TREE_PURPOSE (q);
9479       }
9480 }
9481
9482 /* Resolve the expression name NAME. Return its decl.  */
9483
9484 static tree
9485 resolve_expression_name (tree id, tree *orig)
9486 {
9487   tree name = EXPR_WFL_NODE (id);
9488   tree decl;
9489
9490   /* 6.5.5.1: Simple expression names */
9491   if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9492     {
9493       /* 15.13.1: NAME can appear within the scope of a local variable
9494          declaration */
9495       if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9496         return decl;
9497
9498       /* 15.13.1: NAME can appear within a class declaration */
9499       else
9500         {
9501           decl = lookup_field_wrapper (current_class, name);
9502           if (decl)
9503             {
9504               tree access = NULL_TREE;
9505               int fs = FIELD_STATIC (decl);
9506
9507               /* If we're accessing an outer scope local alias, make
9508                  sure we change the name of the field we're going to
9509                  build access to. */
9510               if (FIELD_LOCAL_ALIAS_USED (decl))
9511                 name = DECL_NAME (decl);
9512
9513               check_deprecation (id, decl);
9514
9515               /* Instance variable (8.3.1.1) can't appear within
9516                  static method, static initializer or initializer for
9517                  a static variable. */
9518               if (!fs && METHOD_STATIC (current_function_decl))
9519                 {
9520                   static_ref_err (id, name, current_class);
9521                   return error_mark_node;
9522                 }
9523               /* Instance variables can't appear as an argument of
9524                  an explicit constructor invocation */
9525               if (!fs && ctxp->explicit_constructor_p
9526                   && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9527                 {
9528                   parse_error_context
9529                     (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9530                   return error_mark_node;
9531                 }
9532
9533               /* If we're processing an inner class and we're trying
9534                  to access a field belonging to an outer class, build
9535                  the access to the field */
9536               if (!fs && outer_field_access_p (current_class, decl))
9537                 {
9538                   if (CLASS_STATIC (TYPE_NAME (current_class)))
9539                     {
9540                       static_ref_err (id, DECL_NAME (decl), current_class);
9541                       return error_mark_node;
9542                     }
9543                   access = build_outer_field_access (id, decl);
9544                   if (orig)
9545                     *orig = access;
9546                   return access;
9547                 }
9548
9549               /* Otherwise build what it takes to access the field */
9550               access = build_field_ref ((fs ? NULL_TREE : current_this),
9551                                         DECL_CONTEXT (decl), name);
9552               if (fs)
9553                 access = maybe_build_class_init_for_field (decl, access);
9554               /* We may be asked to save the real field access node */
9555               if (orig)
9556                 *orig = access;
9557               /* Last check: can we access the field? */
9558               if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9559                 {
9560                   not_accessible_field_error (id, decl);
9561                   return error_mark_node;
9562                 }
9563               /* And we return what we got */
9564               return access;
9565             }
9566           /* Fall down to error report on undefined variable */
9567         }
9568     }
9569   /* 6.5.5.2 Qualified Expression Names */
9570   else
9571     {
9572       if (orig)
9573         *orig = NULL_TREE;
9574       qualify_ambiguous_name (id);
9575       /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9576       /* 15.10.2: Accessing Superclass Members using super */
9577       return resolve_field_access (id, orig, NULL);
9578     }
9579
9580   /* We've got an error here */
9581   if (INNER_CLASS_TYPE_P (current_class))
9582     parse_error_context (id,
9583                          "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9584                          IDENTIFIER_POINTER (name),
9585                          IDENTIFIER_POINTER (DECL_NAME
9586                                              (TYPE_NAME (current_class))));
9587   else
9588     parse_error_context (id, "Undefined variable %qs",
9589                          IDENTIFIER_POINTER (name));
9590
9591   return error_mark_node;
9592 }
9593
9594 static void
9595 static_ref_err (tree wfl, tree field_id, tree class_type)
9596 {
9597   parse_error_context
9598     (wfl,
9599      "Can't make a static reference to nonstatic variable %qs in class %qs",
9600      IDENTIFIER_POINTER (field_id),
9601      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9602 }
9603
9604 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9605    We return something suitable to generate the field access. We also
9606    return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
9607    recipient's address can be null. */
9608
9609 static tree
9610 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9611 {
9612   int is_static = 0;
9613   tree field_ref;
9614   tree decl = NULL_TREE, where_found, type_found;
9615
9616   if (resolve_qualified_expression_name (qual_wfl, &decl,
9617                                          &where_found, &type_found))
9618     return error_mark_node;
9619
9620   /* Resolve the LENGTH field of an array here */
9621   if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9622       && type_found && TYPE_ARRAY_P (type_found)
9623       && ! flag_emit_class_files && ! flag_emit_xref)
9624     {
9625       tree length = build_java_array_length_access (where_found);
9626       field_ref = length;
9627
9628       /* In case we're dealing with a static array, we need to
9629          initialize its class before the array length can be fetched.
9630          It's also a good time to create a DECL_RTL for the field if
9631          none already exists, otherwise if the field was declared in a
9632          class found in an external file and hasn't been (and won't
9633          be) accessed for its value, none will be created. */
9634       if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9635         {
9636           build_static_field_ref (where_found);
9637           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9638         }
9639     }
9640   /* We might have been trying to resolve field.method(). In which
9641      case, the resolution is over and decl is the answer */
9642   else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9643     field_ref = decl;
9644   else if (JDECL_P (decl))
9645     {
9646       if (!type_found)
9647         type_found = DECL_CONTEXT (decl);
9648       is_static = FIELD_STATIC (decl);
9649       field_ref = build_field_ref ((is_static && !flag_emit_xref?
9650                                     NULL_TREE : where_found),
9651                                    type_found, DECL_NAME (decl));
9652       if (field_ref == error_mark_node)
9653         return error_mark_node;
9654       if (is_static)
9655         field_ref = maybe_build_class_init_for_field (decl, field_ref);
9656
9657       /* If we're looking at a static field, we may need to generate a
9658          class initialization for it.  This can happen when the access
9659          looks like `field.ref', where `field' is a static field in an
9660          interface we implement.  */
9661       if (!flag_emit_class_files
9662           && !flag_emit_xref
9663           && TREE_CODE (where_found) == VAR_DECL
9664           && FIELD_STATIC (where_found))
9665         {
9666           build_static_field_ref (where_found);
9667           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9668         }
9669     }
9670   else
9671     field_ref = decl;
9672
9673   if (field_decl)
9674     *field_decl = decl;
9675   if (field_type)
9676     *field_type = (QUAL_DECL_TYPE (decl) ?
9677                    QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9678   return field_ref;
9679 }
9680
9681 /* If NODE is an access to f static field, strip out the class
9682    initialization part and return the field decl, otherwise, return
9683    NODE. */
9684
9685 static tree
9686 strip_out_static_field_access_decl (tree node)
9687 {
9688   if (TREE_CODE (node) == COMPOUND_EXPR)
9689     {
9690       tree op1 = TREE_OPERAND (node, 1);
9691       if (TREE_CODE (op1) == COMPOUND_EXPR)
9692          {
9693            tree call = TREE_OPERAND (op1, 0);
9694            if (TREE_CODE (call) == CALL_EXPR
9695                && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9696                && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9697                    == soft_initclass_node))
9698              return TREE_OPERAND (op1, 1);
9699          }
9700       else if (JDECL_P (op1))
9701         return op1;
9702     }
9703   return node;
9704 }
9705
9706 /* 6.5.5.2: Qualified Expression Names */
9707
9708 static int
9709 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9710                                    tree *where_found, tree *type_found)
9711 {
9712   int from_type = 0;            /* Field search initiated from a type */
9713   int from_super = 0, from_cast = 0, from_qualified_this = 0;
9714   int previous_call_static = 0;
9715   int is_static;
9716   tree decl = NULL_TREE, type = NULL_TREE, q;
9717   /* For certain for of inner class instantiation */
9718   tree saved_current, saved_this;
9719 #define RESTORE_THIS_AND_CURRENT_CLASS                          \
9720   { current_class = saved_current; current_this = saved_this;}
9721
9722   *type_found = *where_found = NULL_TREE;
9723
9724   for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9725     {
9726       tree qual_wfl = QUAL_WFL (q);
9727       tree ret_decl;            /* for EH checking */
9728 #ifdef USE_MAPPED_LOCATION
9729       source_location location;  /* for EH checking */
9730 #else
9731       int location;             /* for EH checking */
9732 #endif
9733
9734       /* 15.10.1 Field Access Using a Primary */
9735       switch (TREE_CODE (qual_wfl))
9736         {
9737         case CALL_EXPR:
9738         case NEW_CLASS_EXPR:
9739           /* If the access to the function call is a non static field,
9740              build the code to access it. */
9741           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9742             {
9743               decl = maybe_access_field (decl, *where_found,
9744                                          DECL_CONTEXT (decl));
9745               if (decl == error_mark_node)
9746                 return 1;
9747             }
9748
9749           /* And code for the function call */
9750           if (complete_function_arguments (qual_wfl))
9751             return 1;
9752
9753           /* We might have to setup a new current class and a new this
9754              for the search of an inner class, relative to the type of
9755              a expression resolved as `decl'. The current values are
9756              saved and restored shortly after */
9757           saved_current = current_class;
9758           saved_this = current_this;
9759           if (decl
9760               && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9761                   || from_qualified_this))
9762             {
9763               /* If we still have `from_qualified_this', we have the form
9764                  <T>.this.f() and we need to build <T>.this */
9765               if (from_qualified_this)
9766                 {
9767                   decl = build_access_to_thisn (current_class, type, 0);
9768                   decl = java_complete_tree (decl);
9769                   type = TREE_TYPE (TREE_TYPE (decl));
9770                 }
9771               current_class = type;
9772               current_this = decl;
9773               from_qualified_this = 0;
9774             }
9775
9776           if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9777             CALL_USING_SUPER (qual_wfl) = 1;
9778 #ifdef USE_MAPPED_LOCATION
9779           location = (TREE_CODE (qual_wfl) == CALL_EXPR
9780                       ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9781                       : UNKNOWN_LOCATION);
9782 #else
9783           location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9784                       EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9785 #endif
9786           *where_found = patch_method_invocation (qual_wfl, decl, type,
9787                                                   from_super,
9788                                                   &is_static, &ret_decl);
9789           from_super = 0;
9790           if (*where_found == error_mark_node)
9791             {
9792               RESTORE_THIS_AND_CURRENT_CLASS;
9793               return 1;
9794             }
9795           *type_found = type = QUAL_DECL_TYPE (*where_found);
9796
9797           *where_found = force_evaluation_order (*where_found);
9798
9799           /* If we're creating an inner class instance, check for that
9800              an enclosing instance is in scope */
9801           if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9802               && INNER_ENCLOSING_SCOPE_CHECK (type))
9803             {
9804               parse_error_context
9805                 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9806                  lang_printable_name (type, 0),
9807                  (!current_this ? "" :
9808                   "; an explicit one must be provided when creating this inner class"));
9809               RESTORE_THIS_AND_CURRENT_CLASS;
9810               return 1;
9811             }
9812
9813           /* In case we had to change then to resolve a inner class
9814              instantiation using a primary qualified by a `new' */
9815           RESTORE_THIS_AND_CURRENT_CLASS;
9816
9817 #ifdef USE_MAPPED_LOCATION
9818           if (location != UNKNOWN_LOCATION)
9819 #else
9820           if (location)
9821 #endif
9822             {
9823               tree arguments = NULL_TREE;
9824               if (TREE_CODE (qual_wfl) == CALL_EXPR
9825                   && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9826                 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9827               check_thrown_exceptions (location, ret_decl, arguments);
9828             }
9829
9830           /* If the previous call was static and this one is too,
9831              build a compound expression to hold the two (because in
9832              that case, previous function calls aren't transported as
9833              forcoming function's argument. */
9834           if (previous_call_static && is_static)
9835             {
9836               /* We must set CAN_COMPLETE_NORMALLY for the first call
9837                  since it is done nowhere else.  */
9838               CAN_COMPLETE_NORMALLY (decl) = 1;
9839               decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9840                              decl, *where_found);
9841               TREE_SIDE_EFFECTS (decl) = 1;
9842             }
9843           else
9844             {
9845               previous_call_static = is_static;
9846               decl = *where_found;
9847             }
9848           from_type = 0;
9849           continue;
9850
9851         case NEW_ARRAY_EXPR:
9852         case NEW_ANONYMOUS_ARRAY_EXPR:
9853           *where_found = decl = java_complete_tree (qual_wfl);
9854           if (decl == error_mark_node)
9855             return 1;
9856           *type_found = type = QUAL_DECL_TYPE (decl);
9857           continue;
9858
9859         case CONVERT_EXPR:
9860           *where_found = decl = java_complete_tree (qual_wfl);
9861           if (decl == error_mark_node)
9862             return 1;
9863           *type_found = type = QUAL_DECL_TYPE (decl);
9864           from_cast = 1;
9865           continue;
9866
9867         case CONDITIONAL_EXPR:
9868         case STRING_CST:
9869         case MODIFY_EXPR:
9870           *where_found = decl = java_complete_tree (qual_wfl);
9871           if (decl == error_mark_node)
9872             return 1;
9873           *type_found = type = QUAL_DECL_TYPE (decl);
9874           continue;
9875
9876         case ARRAY_REF:
9877           /* If the access to the function call is a non static field,
9878              build the code to access it. */
9879           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9880             {
9881               decl = maybe_access_field (decl, *where_found, type);
9882               if (decl == error_mark_node)
9883                 return 1;
9884             }
9885           /* And code for the array reference expression */
9886           decl = java_complete_tree (qual_wfl);
9887           if (decl == error_mark_node)
9888             return 1;
9889           type = QUAL_DECL_TYPE (decl);
9890           continue;
9891
9892         case PLUS_EXPR:
9893           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9894             return 1;
9895           if ((type = patch_string (decl)))
9896             decl = type;
9897           *where_found = QUAL_RESOLUTION (q) = decl;
9898           *type_found = type = TREE_TYPE (decl);
9899           break;
9900
9901         case CLASS_LITERAL:
9902           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9903             return 1;
9904           *where_found = QUAL_RESOLUTION (q) = decl;
9905           *type_found = type = TREE_TYPE (decl);
9906           break;
9907
9908         default:
9909           /* Fix for -Wall Just go to the next statement. Don't
9910              continue */
9911           break;
9912         }
9913
9914       /* If we fall here, we weren't processing a (static) function call. */
9915       previous_call_static = 0;
9916
9917       /* It can be the keyword THIS */
9918       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9919           && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9920         {
9921           if (!current_this)
9922             {
9923               parse_error_context
9924                 (wfl, "Keyword %<this%> used outside allowed context");
9925               return 1;
9926             }
9927           if (ctxp->explicit_constructor_p
9928               && type == current_class)
9929             {
9930               parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9931               return 1;
9932             }
9933           /* We have to generate code for intermediate access */
9934           if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9935             {
9936               *where_found = decl = current_this;
9937               *type_found = type = QUAL_DECL_TYPE (decl);
9938             }
9939           /* We're trying to access the this from somewhere else. Make sure
9940              it's allowed before doing so. */
9941           else
9942             {
9943               if (!enclosing_context_p (type, current_class))
9944                 {
9945                   char *p  = xstrdup (lang_printable_name (type, 0));
9946                   parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9947                                        p, p,
9948                                        lang_printable_name (current_class, 0));
9949                   free (p);
9950                   return 1;
9951                 }
9952               from_qualified_this = 1;
9953               /* If there's nothing else after that, we need to
9954                  produce something now, otherwise, the section of the
9955                  code that needs to produce <T>.this will generate
9956                  what is necessary. */
9957               if (!TREE_CHAIN (q))
9958                 {
9959                   decl = build_access_to_thisn (current_class, type, 0);
9960                   *where_found = decl = java_complete_tree (decl);
9961                   *type_found = type = TREE_TYPE (decl);
9962                 }
9963             }
9964
9965           from_type = 0;
9966           continue;
9967         }
9968
9969       /* 15.10.2 Accessing Superclass Members using SUPER */
9970       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9971           && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9972         {
9973           tree node;
9974           /* Check on the restricted use of SUPER */
9975           if (METHOD_STATIC (current_function_decl)
9976               || current_class == object_type_node)
9977             {
9978               parse_error_context
9979                 (wfl, "Keyword %<super%> used outside allowed context");
9980               return 1;
9981             }
9982           /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9983           node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9984                              CLASSTYPE_SUPER (current_class),
9985                              build_this (EXPR_WFL_LINECOL (qual_wfl)));
9986           *where_found = decl = java_complete_tree (node);
9987           if (decl == error_mark_node)
9988             return 1;
9989           *type_found = type = QUAL_DECL_TYPE (decl);
9990           from_super = from_type = 1;
9991           continue;
9992         }
9993
9994       /* 15.13.1: Can't search for field name in packages, so we
9995          assume a variable/class name was meant. */
9996       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9997         {
9998           tree name;
9999           if ((decl = resolve_package (wfl, &q, &name)))
10000             {
10001               tree list;
10002               *where_found = decl;
10003
10004               check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10005
10006               /* We want to be absolutely sure that the class is laid
10007                  out. We're going to search something inside it. */
10008               *type_found = type = TREE_TYPE (decl);
10009               layout_class (type);
10010               from_type = 1;
10011
10012               /* Fix them all the way down, if any are left. */
10013               if (q)
10014                 {
10015                   list = TREE_CHAIN (q);
10016                   while (list)
10017                     {
10018                       RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10019                       list = TREE_CHAIN (list);
10020                     }
10021                 }
10022             }
10023           else
10024             {
10025               if (from_super || from_cast)
10026                 parse_error_context
10027                   ((from_cast ? qual_wfl : wfl),
10028                    "No variable %qs defined in class %qs",
10029                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10030                    lang_printable_name (type, 0));
10031               else
10032                 parse_error_context
10033                   (qual_wfl, "Undefined variable or class name: %qs",
10034                    IDENTIFIER_POINTER (name));
10035               return 1;
10036             }
10037         }
10038
10039       /* We have a type name. It's been already resolved when the
10040          expression was qualified. */
10041       else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10042         {
10043           decl = QUAL_RESOLUTION (q);
10044
10045           /* Sneak preview. If next we see a `new', we're facing a
10046              qualification which resulted in a type being selected
10047              instead of a field.  Report the error.  */
10048           if(TREE_CHAIN (q)
10049              && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10050             {
10051               parse_error_context (qual_wfl, "Undefined variable %qs",
10052                                    IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10053               return 1;
10054             }
10055
10056           check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10057           
10058           check_deprecation (qual_wfl, decl);
10059
10060           type = TREE_TYPE (decl);
10061           from_type = 1;
10062         }
10063       /* We resolve an expression name */
10064       else
10065         {
10066           tree field_decl = NULL_TREE;
10067
10068           /* If there exists an early resolution, use it. That occurs
10069              only once and we know that there are more things to
10070              come. Don't do that when processing something after SUPER
10071              (we need more thing to be put in place below */
10072           if (!from_super && QUAL_RESOLUTION (q))
10073             {
10074               decl = QUAL_RESOLUTION (q);
10075               if (!type)
10076                 {
10077                   if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
10078                     {
10079                       if (current_this)
10080                         *where_found = current_this;
10081                       else
10082                         {
10083                           static_ref_err (qual_wfl, DECL_NAME (decl),
10084                                           current_class);
10085                           return 1;
10086                         }
10087                       if (outer_field_access_p (current_class, decl))
10088                         decl = build_outer_field_access (qual_wfl, decl);
10089                     }
10090                   else
10091                     {
10092                       *where_found = TREE_TYPE (decl);
10093                       if (TREE_CODE (*where_found) == POINTER_TYPE)
10094                         *where_found = TREE_TYPE (*where_found);
10095                     }
10096                 }
10097             }
10098
10099           /* Report and error if we're using a numerical literal as a
10100              qualifier. It can only be an INTEGER_CST. */
10101           else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10102             {
10103               parse_error_context
10104                 (wfl, "Can't use type %qs as a qualifier",
10105                  lang_printable_name (TREE_TYPE (qual_wfl), 0));
10106               return 1;
10107             }
10108
10109           /* We have to search for a field, knowing the type of its
10110              container. The flag FROM_TYPE indicates that we resolved
10111              the last member of the expression as a type name, which
10112              means that for the resolution of this field, we'll look
10113              for other errors than if it was resolved as a member of
10114              an other field. */
10115           else
10116             {
10117               int is_static;
10118               tree field_decl_type; /* For layout */
10119
10120               if (!from_type && !JREFERENCE_TYPE_P (type))
10121                 {
10122                   parse_error_context
10123                     (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10124                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10125                      lang_printable_name (type, 0),
10126                      IDENTIFIER_POINTER (DECL_NAME (decl)));
10127                   return 1;
10128                 }
10129
10130               field_decl = lookup_field_wrapper (type,
10131                                                  EXPR_WFL_NODE (qual_wfl));
10132
10133               /* Maybe what we're trying to access to is an inner
10134                  class, only if decl is a TYPE_DECL. */
10135               if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10136                 {
10137                   tree ptr, inner_decl;
10138
10139                   BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10140                   inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10141                   if (inner_decl)
10142                     {
10143                       check_inner_class_access (inner_decl, decl, qual_wfl);
10144                       type = TREE_TYPE (inner_decl);
10145                       decl = inner_decl;
10146                       from_type = 1;
10147                       continue;
10148                     }
10149                 }
10150
10151               if (field_decl == NULL_TREE)
10152                 {
10153                   parse_error_context
10154                     (qual_wfl, "No variable %qs defined in type %qs",
10155                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10156                      GET_TYPE_NAME (type));
10157                   return 1;
10158                 }
10159               if (field_decl == error_mark_node)
10160                 return 1;
10161
10162               /* Layout the type of field_decl, since we may need
10163                  it. Don't do primitive types or loaded classes. The
10164                  situation of non primitive arrays may not handled
10165                  properly here. FIXME */
10166               if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10167                 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10168               else
10169                 field_decl_type = TREE_TYPE (field_decl);
10170               if (!JPRIMITIVE_TYPE_P (field_decl_type)
10171                   && !CLASS_LOADED_P (field_decl_type)
10172                   && !TYPE_ARRAY_P (field_decl_type))
10173                 resolve_and_layout (field_decl_type, NULL_TREE);
10174
10175               /* Check on accessibility here */
10176               if (not_accessible_p (current_class, field_decl,
10177                                     *type_found, from_super))
10178                 return not_accessible_field_error (qual_wfl,field_decl);    
10179               check_deprecation (qual_wfl, field_decl);
10180
10181               /* There are things to check when fields are accessed
10182                  from type. There are no restrictions on a static
10183                  declaration of the field when it is accessed from an
10184                  interface */
10185               is_static = FIELD_STATIC (field_decl);
10186               if (!from_super && from_type
10187                   && !TYPE_INTERFACE_P (type)
10188                   && !is_static
10189                   && (current_function_decl
10190                       && METHOD_STATIC (current_function_decl)))
10191                 {
10192                   static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10193                   return 1;
10194                 }
10195               from_cast = from_super = 0;
10196
10197               /* It's an access from a type but it isn't static, we
10198                  make it relative to `this'. */
10199               if (!is_static && from_type)
10200                 decl = current_this;
10201
10202               /* If we need to generate something to get a proper
10203                  handle on what this field is accessed from, do it
10204                  now. */
10205               if (!is_static)
10206                 {
10207                   decl = maybe_access_field (decl, *where_found, *type_found);
10208                   if (decl == error_mark_node)
10209                     return 1;
10210                 }
10211
10212               /* We want to keep the location were found it, and the type
10213                  we found. */
10214               *where_found = decl;
10215               *type_found = type;
10216
10217               /* Generate the correct expression for field access from
10218                  qualified this */
10219               if (from_qualified_this)
10220                 {
10221                   field_decl = build_outer_field_access (qual_wfl, field_decl);
10222                   from_qualified_this = 0;
10223                 }
10224
10225               /* This is the decl found and eventually the next one to
10226                  search from */
10227               decl = field_decl;
10228             }
10229           from_type = 0;
10230           type = QUAL_DECL_TYPE (decl);
10231
10232           /* Sneak preview. If decl is qualified by a `new', report
10233              the error here to be accurate on the peculiar construct */
10234           if (TREE_CHAIN (q)
10235               && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10236               && !JREFERENCE_TYPE_P (type))
10237             {
10238               parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10239                                    lang_printable_name (type, 0));
10240               return 1;
10241             }
10242         }
10243       /* `q' might have changed due to a after package resolution
10244          re-qualification */
10245       if (!q)
10246         break;
10247     }
10248   *found_decl = decl;
10249   return 0;
10250 }
10251
10252 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10253    can't be accessed from REFERENCE (a record type). If MEMBER
10254    features a protected access, we then use WHERE which, if non null,
10255    holds the type of MEMBER's access that is checked against
10256    6.6.2.1. This function should be used when decl is a field or a
10257    method.  */
10258
10259 static int
10260 not_accessible_p (tree reference, tree member, tree where, int from_super)
10261 {
10262   int access_flag = get_access_flags_from_decl (member);
10263   bool is_static = false;
10264  
10265   if (TREE_CODE (member) == FIELD_DECL ||
10266       TREE_CODE (member) == VAR_DECL)
10267     is_static = FIELD_STATIC (member);
10268   else
10269     is_static = METHOD_STATIC (member);
10270
10271   /* Access always granted for members declared public */
10272   if (access_flag & ACC_PUBLIC)
10273     return 0;
10274
10275   /* Check access on protected members */
10276   if (access_flag & ACC_PROTECTED)
10277     {
10278       /* Access granted if it occurs from within the package
10279          containing the class in which the protected member is
10280          declared */
10281       if (class_in_current_package (DECL_CONTEXT (member)))
10282         return 0;
10283
10284       /* If accessed with the form `super.member', then access is granted */
10285       if (from_super)
10286         return 0;
10287
10288       /* If WHERE is active, access was made through a qualifier. For 
10289          non-static members, access is granted if the type of the qualifier 
10290          is or is a sublass of the type the access is made from (6.6.2.1.)  */
10291       if (where && !is_static)
10292         {
10293           while (reference)
10294             {
10295               if (inherits_from_p (where, reference))
10296                 return 0;
10297               if (INNER_CLASS_TYPE_P (reference))
10298                 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10299               else
10300                 break;
10301             }
10302           return 1;
10303         }
10304
10305       /* Otherwise, access is granted if occurring from within the class
10306          where member is declared, or a subclass of it.  */
10307       while (reference)
10308         {
10309           if (inherits_from_p (reference, DECL_CONTEXT (member)))
10310             return 0;
10311           if (INNER_CLASS_TYPE_P (reference))
10312             reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10313           else
10314             break;
10315         }
10316       return 1;
10317     }
10318
10319   /* Check access on private members. Access is granted only if it
10320      occurs from within the class in which it is declared -- that does
10321      it for innerclasses too. */
10322   if (access_flag & ACC_PRIVATE)
10323     {
10324       if (reference == DECL_CONTEXT (member) ||
10325           common_enclosing_context_p (DECL_CONTEXT (member), reference))
10326         return 0;
10327       return 1;
10328     }
10329
10330   /* Default access is permitted only when occurring from within the
10331      package in which the context (MEMBER) is declared.  */
10332   return !class_in_current_package (DECL_CONTEXT (member));
10333 }
10334
10335 /* Test deprecated decl access.  */
10336 static void
10337 check_deprecation (tree wfl, tree decl)
10338 {
10339   const char *file;
10340   tree elt;
10341
10342   if (! warn_deprecated)
10343     return;
10344
10345   /* We want to look at the element type of arrays here, so we strip
10346      all surrounding array types.  */
10347   if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10348     {
10349       elt = TREE_TYPE (decl);
10350       while (TYPE_ARRAY_P (elt))
10351         elt = TYPE_ARRAY_ELEMENT (elt);
10352       /* We'll end up with a pointer type, so we use TREE_TYPE to go
10353          to the record.  */
10354       decl = TYPE_NAME (TREE_TYPE (elt));
10355     }
10356   file = DECL_SOURCE_FILE (decl);
10357
10358   /* Complain if the field is deprecated and the file it was defined
10359      in isn't compiled at the same time the file which contains its
10360      use is */
10361   if (DECL_DEPRECATED (decl)
10362       && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10363     {
10364       const char *the;
10365       switch (TREE_CODE (decl))
10366         {
10367         case FUNCTION_DECL:
10368           the = "method";
10369           break;
10370         case FIELD_DECL:
10371         case VAR_DECL:
10372           the = "field";
10373           break;
10374         case TYPE_DECL:
10375           parse_warning_context (wfl, "The class %qs has been deprecated",
10376                                  IDENTIFIER_POINTER (DECL_NAME (decl)));
10377           return;
10378         default:
10379           abort ();
10380         }
10381       /* Don't issue a message if the context as been deprecated as a
10382          whole. */
10383       if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10384         parse_warning_context
10385           (wfl, "The %s %qs in class %qs has been deprecated",
10386            the, lang_printable_name (decl, 0),
10387            IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10388     }
10389 }
10390
10391 /* Returns 1 if class was declared in the current package, 0 otherwise */
10392
10393 static GTY(()) tree cicp_cache;
10394 static int
10395 class_in_current_package (tree class)
10396 {
10397   int qualified_flag;
10398   tree left;
10399
10400   if (cicp_cache == class)
10401     return 1;
10402
10403   qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10404
10405   /* If the current package is empty and the name of CLASS is
10406      qualified, class isn't in the current package.  If there is a
10407      current package and the name of the CLASS is not qualified, class
10408      isn't in the current package */
10409   if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10410     return 0;
10411
10412   /* If there is not package and the name of CLASS isn't qualified,
10413      they belong to the same unnamed package */
10414   if (!ctxp->package && !qualified_flag)
10415     return 1;
10416
10417   /* Compare the left part of the name of CLASS with the package name */
10418   split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10419   if (ctxp->package == left)
10420     {
10421       cicp_cache = class;
10422       return 1;
10423     }
10424   return 0;
10425 }
10426
10427 /* This function may generate code to access DECL from WHERE. This is
10428    done only if certain conditions meet.  */
10429
10430 static tree
10431 maybe_access_field (tree decl, tree where, tree type)
10432 {
10433   if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10434       && !FIELD_STATIC (decl))
10435     decl = build_field_ref (where ? where : current_this,
10436                             (type ? type : DECL_CONTEXT (decl)),
10437                             DECL_NAME (decl));
10438   return decl;
10439 }
10440
10441 /* Build a method invocation, by patching PATCH. If non NULL
10442    and according to the situation, PRIMARY and WHERE may be
10443    used. IS_STATIC is set to 1 if the invoked function is static. */
10444
10445 static tree
10446 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10447                          int *is_static, tree *ret_decl)
10448 {
10449   tree wfl = TREE_OPERAND (patch, 0);
10450   tree args = TREE_OPERAND (patch, 1);
10451   tree name = EXPR_WFL_NODE (wfl);
10452   tree list;
10453   int is_static_flag = 0;
10454   int is_super_init = 0;
10455   tree this_arg = NULL_TREE;
10456   int is_array_clone_call = 0;
10457
10458   /* Should be overridden if everything goes well. Otherwise, if
10459      something fails, it should keep this value. It stop the
10460      evaluation of a bogus assignment. See java_complete_tree,
10461      MODIFY_EXPR: for the reasons why we sometimes want to keep on
10462      evaluating an assignment */
10463   TREE_TYPE (patch) = error_mark_node;
10464
10465   /* Since lookup functions are messing with line numbers, save the
10466      context now.  */
10467   java_parser_context_save_global ();
10468
10469   /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10470
10471   /* Resolution of qualified name, excluding constructors */
10472   if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10473     {
10474       tree identifier, identifier_wfl, type, resolved;
10475       /* Extract the last IDENTIFIER of the qualified
10476          expression. This is a wfl and we will use it's location
10477          data during error report. */
10478       identifier_wfl = cut_identifier_in_qualified (wfl);
10479       identifier = EXPR_WFL_NODE (identifier_wfl);
10480
10481       /* Given the context, IDENTIFIER is syntactically qualified
10482          as a MethodName. We need to qualify what's before */
10483       qualify_ambiguous_name (wfl);
10484       resolved = resolve_field_access (wfl, NULL, NULL);
10485
10486       if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10487          && FIELD_FINAL (resolved)
10488          && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10489          && !flag_emit_class_files && !flag_emit_xref)
10490        resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10491
10492       if (resolved == error_mark_node)
10493         PATCH_METHOD_RETURN_ERROR ();
10494
10495       type = GET_SKIP_TYPE (resolved);
10496       resolve_and_layout (type, NULL_TREE);
10497
10498       if (JPRIMITIVE_TYPE_P (type))
10499         {
10500           parse_error_context
10501             (identifier_wfl,
10502              "Can't invoke a method on primitive type %qs",
10503              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10504           PATCH_METHOD_RETURN_ERROR ();
10505         }
10506
10507       list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10508       args = nreverse (args);
10509
10510       /* We're resolving a call from a type */
10511       if (TREE_CODE (resolved) == TYPE_DECL)
10512         {
10513           if (CLASS_INTERFACE (resolved))
10514             {
10515               parse_error_context
10516                 (identifier_wfl,
10517                 "Can't make static reference to method %qs in interface %qs",
10518                  IDENTIFIER_POINTER (identifier),
10519                  IDENTIFIER_POINTER (name));
10520               PATCH_METHOD_RETURN_ERROR ();
10521             }
10522           if (list && !METHOD_STATIC (list))
10523             {
10524               char *fct_name = xstrdup (lang_printable_name (list, 2));
10525               parse_error_context
10526                 (identifier_wfl,
10527                  "Can't make static reference to method %<%s %s%> in class %qs",
10528                  lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10529                  fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10530               free (fct_name);
10531               PATCH_METHOD_RETURN_ERROR ();
10532             }
10533         }
10534       else
10535         this_arg = primary = resolved;
10536
10537       if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10538         is_array_clone_call = 1;
10539
10540       /* IDENTIFIER_WFL will be used to report any problem further */
10541       wfl = identifier_wfl;
10542     }
10543   /* Resolution of simple names, names generated after a primary: or
10544      constructors */
10545   else
10546     {
10547       tree class_to_search = NULL_TREE;
10548       int lc;                   /* Looking for Constructor */
10549
10550       /* We search constructor in their target class */
10551       if (CALL_CONSTRUCTOR_P (patch))
10552         {
10553           if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10554             class_to_search = EXPR_WFL_NODE (wfl);
10555           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10556                    this_identifier_node)
10557             class_to_search = NULL_TREE;
10558           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10559                    super_identifier_node)
10560             {
10561               is_super_init = 1;
10562               if (CLASSTYPE_SUPER (current_class))
10563                 class_to_search =
10564                   DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10565               else
10566                 {
10567                   parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10568                   PATCH_METHOD_RETURN_ERROR ();
10569                 }
10570             }
10571
10572           /* Class to search is NULL if we're searching the current one */
10573           if (class_to_search)
10574             {
10575               class_to_search = resolve_and_layout (class_to_search, wfl);
10576
10577               if (!class_to_search)
10578                 {
10579                   parse_error_context
10580                     (wfl, "Class %qs not found in type declaration",
10581                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10582                   PATCH_METHOD_RETURN_ERROR ();
10583                 }
10584
10585               /* Can't instantiate an abstract class, but we can
10586                  invoke it's constructor. It's use within the `new'
10587                  context is denied here. */
10588               if (CLASS_ABSTRACT (class_to_search)
10589                   && TREE_CODE (patch) == NEW_CLASS_EXPR)
10590                 {
10591                   parse_error_context
10592                     (wfl, "Class %qs is an abstract class. It can't be instantiated",
10593                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10594                   PATCH_METHOD_RETURN_ERROR ();
10595                 }
10596
10597               class_to_search = TREE_TYPE (class_to_search);
10598             }
10599           else
10600             class_to_search = current_class;
10601           lc = 1;
10602         }
10603       /* This is a regular search in the local class, unless an
10604          alternate class is specified. */
10605       else
10606         {
10607           if (where != NULL_TREE)
10608             class_to_search = where;
10609           else if (QUALIFIED_P (name))
10610             class_to_search = current_class;
10611           else
10612             {
10613               class_to_search = current_class;
10614
10615               for (;;)
10616                 {
10617                   if (has_method (class_to_search, name))
10618                     break;
10619                   if (! INNER_CLASS_TYPE_P (class_to_search))
10620                     {
10621                       parse_error_context (wfl,
10622                                            "No method named %qs in scope",
10623                                            IDENTIFIER_POINTER (name));
10624                       PATCH_METHOD_RETURN_ERROR ();
10625                     }
10626                   class_to_search
10627                     = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10628                 }
10629             }
10630           lc = 0;
10631         }
10632
10633       /* NAME is a simple identifier or comes from a primary. Search
10634          in the class whose declaration contain the method being
10635          invoked. */
10636       resolve_and_layout (class_to_search, NULL_TREE);
10637
10638       list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10639       /* Don't continue if no method were found, as the next statement
10640          can't be executed then. */
10641       if (!list)
10642         PATCH_METHOD_RETURN_ERROR ();
10643
10644       if (TYPE_ARRAY_P (class_to_search)
10645           && DECL_NAME (list) == get_identifier ("clone"))
10646         is_array_clone_call = 1;
10647
10648       /* Check for static reference if non static methods */
10649       if (check_for_static_method_reference (wfl, patch, list,
10650                                              class_to_search, primary))
10651         PATCH_METHOD_RETURN_ERROR ();
10652
10653       /* Check for inner classes creation from illegal contexts */
10654       if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10655                  && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10656           && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10657           && !DECL_INIT_P (current_function_decl))
10658         {
10659           parse_error_context
10660             (wfl, "No enclosing instance for inner class %qs is in scope%s",
10661              lang_printable_name (class_to_search, 0),
10662              (!current_this ? "" :
10663               "; an explicit one must be provided when creating this inner class"));
10664           PATCH_METHOD_RETURN_ERROR ();
10665         }
10666
10667       /* Non static methods are called with the current object extra
10668          argument. If patch a `new TYPE()', the argument is the value
10669          returned by the object allocator. If method is resolved as a
10670          primary, use the primary otherwise use the current THIS. */
10671       args = nreverse (args);
10672       if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10673         {
10674           this_arg = primary ? primary : current_this;
10675
10676           /* If we're using an access method, things are different.
10677              There are two family of cases:
10678
10679              1) We're not generating bytecodes:
10680
10681              - LIST is non static. It's invocation is transformed from
10682                x(a1,...,an) into this$<n>.x(a1,....an).
10683              - LIST is static. It's invocation is transformed from
10684                x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10685
10686              2) We're generating bytecodes:
10687
10688              - LIST is non static. It's invocation is transformed from
10689                x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10690              - LIST is static. It's invocation is transformed from
10691                x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10692
10693              Of course, this$<n> can be arbitrarily complex, ranging from
10694              this$0 (the immediate outer context) to
10695              access$0(access$0(...(this$0))).
10696
10697              maybe_use_access_method returns a nonzero value if the
10698              this_arg has to be moved into the (then generated) stub
10699              argument list. In the meantime, the selected function
10700              might have be replaced by a generated stub. */
10701           if (!primary &&
10702               maybe_use_access_method (is_super_init, &list, &this_arg))
10703             {
10704               args = tree_cons (NULL_TREE, this_arg, args);
10705               this_arg = NULL_TREE; /* So it doesn't get chained twice */
10706             }
10707         }
10708     }
10709
10710   /* Merge point of all resolution schemes. If we have nothing, this
10711      is an error, already signaled */
10712   if (!list)
10713     PATCH_METHOD_RETURN_ERROR ();
10714
10715   /* Check accessibility, position the is_static flag, build and
10716      return the call */
10717   if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10718                         (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10719                          NULL_TREE), from_super)
10720       /* Calls to clone() on array types are permitted as a special-case. */
10721       && !is_array_clone_call)
10722     {
10723       const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10724       const char *const access =
10725         accessibility_string (get_access_flags_from_decl (list));
10726       const char *const klass =
10727         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10728       const char *const refklass =
10729         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10730       const char *const what = (DECL_CONSTRUCTOR_P (list)
10731                                 ? "constructor" : "method");
10732       parse_error_context (wfl,
10733                            "Can't access %s %s %<%s.%s%> from %qs",
10734                            access, what, klass, fct_name, refklass);
10735       PATCH_METHOD_RETURN_ERROR ();
10736     }
10737
10738   /* Deprecation check: check whether the method being invoked or the
10739      instance-being-created's type are deprecated.  */
10740   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10741     check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10742   check_deprecation (wfl, list);
10743
10744   /* If invoking a innerclass constructor, there are hidden parameters
10745      to pass */
10746   if (TREE_CODE (patch) == NEW_CLASS_EXPR
10747       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10748     {
10749       /* And make sure we add the accessed local variables to be saved
10750          in field aliases. */
10751       args = build_alias_initializer_parameter_list
10752         (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10753
10754       /* Secretly pass the current_this/primary as a second argument */
10755       if (primary || current_this)
10756         {
10757           tree extra_arg;
10758           tree this_type = (current_this ?
10759                             TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10760           /* Method's (list) enclosing context */
10761           tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10762           /* If we have a primary, use it. */
10763           if (primary)
10764             extra_arg = primary;
10765           /* The current `this' is an inner class but isn't a direct
10766              enclosing context for the inner class we're trying to
10767              create. Build an access to the proper enclosing context
10768              and use it. */
10769           else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10770                    && this_type != TREE_TYPE (mec))
10771             {
10772
10773               extra_arg = build_access_to_thisn (current_class,
10774                                                  TREE_TYPE (mec), 0);
10775               extra_arg = java_complete_tree (extra_arg);
10776             }
10777           /* Otherwise, just use the current `this' as an enclosing
10778              context. */
10779           else
10780             extra_arg = current_this;
10781           args = tree_cons (NULL_TREE, extra_arg, args);
10782         }
10783       else
10784         args = tree_cons (NULL_TREE, integer_zero_node, args);
10785     }
10786
10787   /* This handles the situation where a constructor invocation needs
10788      to have an enclosing context passed as a second parameter (the
10789      constructor is one of an inner class). */
10790   if ((is_super_init ||
10791        (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10792       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10793     {
10794       tree dest = TYPE_NAME (DECL_CONTEXT (list));
10795       tree extra_arg =
10796         build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10797       extra_arg = java_complete_tree (extra_arg);
10798       args = tree_cons (NULL_TREE, extra_arg, args);
10799     }
10800
10801   is_static_flag = METHOD_STATIC (list);
10802   if (! is_static_flag && this_arg != NULL_TREE)
10803     args = tree_cons (NULL_TREE, this_arg, args);
10804
10805   /* In the context of an explicit constructor invocation, we can't
10806      invoke any method relying on `this'. Exceptions are: we're
10807      invoking a static function, primary exists and is not the current
10808      this, we're creating a new object. */
10809   if (ctxp->explicit_constructor_p
10810       && !is_static_flag
10811       && (!primary || primary == current_this)
10812       && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10813     {
10814       parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10815       PATCH_METHOD_RETURN_ERROR ();
10816     }
10817   java_parser_context_restore_global ();
10818   if (is_static)
10819     *is_static = is_static_flag;
10820   /* Sometimes, we want the decl of the selected method. Such as for
10821      EH checking */
10822   if (ret_decl)
10823     *ret_decl = list;
10824   patch = patch_invoke (patch, list, args);
10825
10826   /* Now is a good time to insert the call to finit$ */
10827   if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10828     {
10829       tree finit_parms, finit_call;
10830
10831       /* Prepare to pass hidden parameters to finit$, if any. */
10832       finit_parms = build_alias_initializer_parameter_list
10833         (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10834
10835       finit_call =
10836         build_method_invocation (build_wfl_node (finit_identifier_node),
10837                                  finit_parms);
10838
10839       /* Generate the code used to initialize fields declared with an
10840          initialization statement and build a compound statement along
10841          with the super constructor invocation. */
10842       CAN_COMPLETE_NORMALLY (patch) = 1;
10843       patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10844                       java_complete_tree (finit_call));
10845     }
10846   return patch;
10847 }
10848
10849 /* Check that we're not trying to do a static reference to a method in
10850    non static method. Return 1 if it's the case, 0 otherwise. */
10851
10852 static int
10853 check_for_static_method_reference (tree wfl, tree node, tree method,
10854                                    tree where, tree primary)
10855 {
10856   if (METHOD_STATIC (current_function_decl)
10857       && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10858     {
10859       char *fct_name = xstrdup (lang_printable_name (method, 0));
10860       parse_error_context
10861         (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10862          lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10863          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10864       free (fct_name);
10865       return 1;
10866     }
10867   return 0;
10868 }
10869
10870 /* Fix the invocation of *MDECL if necessary in the case of a
10871    invocation from an inner class. *THIS_ARG might be modified
10872    appropriately and an alternative access to *MDECL might be
10873    returned.  */
10874
10875 static int
10876 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10877 {
10878   tree ctx;
10879   tree md = *mdecl, ta = *this_arg;
10880   int to_return = 0;
10881   int non_static_context = !METHOD_STATIC (md);
10882
10883   if (is_super_init
10884       || DECL_CONTEXT (md) == current_class
10885       || !PURE_INNER_CLASS_TYPE_P (current_class)
10886       || DECL_FINIT_P (md)
10887       || DECL_INSTINIT_P (md))
10888     return 0;
10889
10890   /* If we're calling a method found in an enclosing class, generate
10891      what it takes to retrieve the right this. Don't do that if we're
10892      invoking a static method. Note that if MD's type is unrelated to
10893      CURRENT_CLASS, then the current this can be used. */
10894
10895   if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10896     {
10897       ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10898       if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10899         {
10900           ta = build_current_thisn (current_class);
10901           ta = build_wfl_node (ta);
10902         }
10903       else
10904         {
10905           tree type = ctx;
10906           while (type)
10907             {
10908               maybe_build_thisn_access_method (type);
10909               if (inherits_from_p (type, DECL_CONTEXT (md)))
10910                 {
10911                   ta = build_access_to_thisn (ctx, type, 0);
10912                   break;
10913                 }
10914               type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10915                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10916             }
10917         }
10918       ta = java_complete_tree (ta);
10919     }
10920
10921   /* We might have to use an access method to get to MD. We can
10922      break the method access rule as far as we're not generating
10923      bytecode */
10924   if (METHOD_PRIVATE (md) && flag_emit_class_files)
10925     {
10926       md = build_outer_method_access_method (md);
10927       to_return = 1;
10928     }
10929
10930   *mdecl = md;
10931   *this_arg = ta;
10932
10933   /* Returning a nonzero value indicates we were doing a non static
10934      method invocation that is now a static invocation. It will have
10935      callee displace `this' to insert it in the regular argument
10936      list. */
10937   return (non_static_context && to_return);
10938 }
10939
10940 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10941    mode.  */
10942
10943 static tree
10944 patch_invoke (tree patch, tree method, tree args)
10945 {
10946   tree dtable, func;
10947   tree original_call, t, ta;
10948   tree check = NULL_TREE;
10949
10950   /* Last step for args: convert build-in types. If we're dealing with
10951      a new TYPE() type call, the first argument to the constructor
10952      isn't found in the incoming argument list, but delivered by
10953      `new' */
10954   t = TYPE_ARG_TYPES (TREE_TYPE (method));
10955   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10956     t = TREE_CHAIN (t);
10957   for (ta = args; t != end_params_node && ta;
10958        t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10959     if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10960         TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10961       TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10962
10963   /* Resolve unresolved returned type issues */
10964   t = TREE_TYPE (TREE_TYPE (method));
10965   if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10966     resolve_and_layout (TREE_TYPE (t), NULL);
10967
10968   if (flag_emit_class_files || flag_emit_xref)
10969     func = method;
10970   else
10971     {
10972       switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10973         {
10974         case INVOKE_VIRTUAL:
10975           dtable = invoke_build_dtable (0, args);
10976           func = build_invokevirtual (dtable, method);
10977           break;
10978
10979         case INVOKE_NONVIRTUAL:
10980           /* If the object for the method call is null, we throw an
10981              exception.  We don't do this if the object is the current
10982              method's `this'.  In other cases we just rely on an
10983              optimization pass to eliminate redundant checks.  */
10984           if (TREE_VALUE (args) != current_this)
10985             {
10986               /* We use a save_expr here to make sure we only evaluate
10987                  the new `self' expression once.  */
10988               tree save_arg = save_expr (TREE_VALUE (args));
10989               TREE_VALUE (args) = save_arg;
10990               check = java_check_reference (save_arg, 1);
10991             }
10992           /* Fall through.  */
10993
10994         case INVOKE_SUPER:
10995         case INVOKE_STATIC:
10996           {
10997             tree signature = build_java_signature (TREE_TYPE (method));
10998             func = build_known_method_ref (method, TREE_TYPE (method),
10999                                            DECL_CONTEXT (method),
11000                                            signature, args);
11001           }
11002           break;
11003
11004         case INVOKE_INTERFACE:
11005           dtable = invoke_build_dtable (1, args);
11006           func = build_invokeinterface (dtable, method);
11007           break;
11008
11009         default:
11010           abort ();
11011         }
11012
11013       /* Ensure self_type is initialized, (invokestatic). FIXME */
11014       func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11015     }
11016
11017   TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11018   TREE_OPERAND (patch, 0) = func;
11019   TREE_OPERAND (patch, 1) = args;
11020   patch = check_for_builtin (method, patch);
11021   original_call = patch;
11022
11023   /* We're processing a `new TYPE ()' form. New is called and its
11024      returned value is the first argument to the constructor. We build
11025      a COMPOUND_EXPR and use saved expression so that the overall NEW
11026      expression value is a pointer to a newly created and initialized
11027      class. */
11028   if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11029     {
11030       tree class = DECL_CONTEXT (method);
11031       tree c1, saved_new, new;
11032       tree alloc_node;
11033
11034       if (flag_emit_class_files || flag_emit_xref)
11035         {
11036           TREE_TYPE (patch) = build_pointer_type (class);
11037           return patch;
11038         }
11039       if (!TYPE_SIZE (class))
11040         safe_layout_class (class);
11041       alloc_node =
11042         (class_has_finalize_method (class) ? alloc_object_node
11043                                            : alloc_no_finalizer_node);
11044       new = build3 (CALL_EXPR, promote_type (class),
11045                     build_address_of (alloc_node),
11046                     build_tree_list (NULL_TREE, build_class_ref (class)),
11047                     NULL_TREE);
11048       saved_new = save_expr (new);
11049       c1 = build_tree_list (NULL_TREE, saved_new);
11050       TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11051       TREE_OPERAND (original_call, 1) = c1;
11052       TREE_SET_CODE (original_call, CALL_EXPR);
11053       patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11054     }
11055
11056   /* If CHECK is set, then we are building a check to see if the object
11057      is NULL.  */
11058   if (check != NULL_TREE)
11059     {
11060       /* We have to call force_evaluation_order now because creating a
11061          COMPOUND_EXPR wraps the arg list in a way that makes it
11062          unrecognizable by force_evaluation_order later.  Yuk.  */
11063       patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check, 
11064                       force_evaluation_order (patch));
11065       TREE_SIDE_EFFECTS (patch) = 1;
11066     }
11067
11068   /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11069      put it as the first expression of a COMPOUND_EXPR. The second
11070      expression being an empty statement to be later patched if
11071      necessary. We remember a TREE_LIST (the PURPOSE is the method,
11072      the VALUE is the compound) in a hashtable and return a
11073      COMPOUND_EXPR built so that the result of the evaluation of the
11074      original PATCH node is returned. */
11075   if (STATIC_CLASS_INIT_OPT_P ()
11076       && current_function_decl && METHOD_STATIC (method))
11077     {
11078       tree list;
11079       tree fndecl = current_function_decl;
11080       /* We have to call force_evaluation_order now because creating a
11081          COMPOUND_EXPR wraps the arg list in a way that makes it
11082          unrecognizable by force_evaluation_order later.  Yuk.  */
11083       tree save = force_evaluation_order (patch);
11084       tree type = TREE_TYPE (patch);
11085
11086       patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11087       list = tree_cons (method, patch,
11088                         DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11089
11090       DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11091
11092       patch = build2 (COMPOUND_EXPR, type, patch, save);
11093     }
11094
11095   return patch;
11096 }
11097
11098 static int
11099 invocation_mode (tree method, int super)
11100 {
11101   int access = get_access_flags_from_decl (method);
11102
11103   if (super)
11104     return INVOKE_SUPER;
11105
11106   if (access & ACC_STATIC)
11107     return INVOKE_STATIC;
11108
11109   /* We have to look for a constructor before we handle nonvirtual
11110      calls; otherwise the constructor will look nonvirtual.  */
11111   if (DECL_CONSTRUCTOR_P (method))
11112     return INVOKE_STATIC;
11113
11114   if (access & ACC_PRIVATE)
11115     return INVOKE_NONVIRTUAL;
11116
11117   /* Binary compatibility: just because it's final today, that doesn't
11118      mean it'll be final tomorrow.  */
11119   if (! flag_indirect_dispatch  
11120       || DECL_CONTEXT (method) == object_type_node)
11121     {
11122       if (access & ACC_FINAL)
11123         return INVOKE_NONVIRTUAL;
11124
11125       if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11126         return INVOKE_NONVIRTUAL;
11127     }
11128
11129   if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11130     return INVOKE_INTERFACE;
11131
11132   return INVOKE_VIRTUAL;
11133 }
11134
11135 /* Retrieve a refined list of matching methods. It covers the step
11136    15.11.2 (Compile-Time Step 2) */
11137
11138 static tree
11139 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11140 {
11141   tree atl = end_params_node;           /* Arg Type List */
11142   tree method, signature, list, node;
11143   const char *candidates;               /* Used for error report */
11144   char *dup;
11145
11146   /* Fix the arguments */
11147   for (node = arg_list; node; node = TREE_CHAIN (node))
11148     {
11149       tree current_arg = TREE_TYPE (TREE_VALUE (node));
11150       /* Non primitive type may have to be resolved */
11151       if (!JPRIMITIVE_TYPE_P (current_arg))
11152         resolve_and_layout (current_arg, NULL_TREE);
11153       /* And promoted */
11154       if (TREE_CODE (current_arg) == RECORD_TYPE)
11155         current_arg = promote_type (current_arg);
11156       atl = tree_cons (NULL_TREE, current_arg, atl);
11157     }
11158
11159   /* Presto. If we're dealing with an anonymous class and a
11160      constructor call, generate the right constructor now, since we
11161      know the arguments' types. */
11162
11163   if (lc && ANONYMOUS_CLASS_P (class))
11164     {
11165       tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11166       /* The anonymous class may have already been laid out, so make sure
11167          the new constructor is laid out here.  */
11168       layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11169     }
11170
11171   /* Find all candidates and then refine the list, searching for the
11172      most specific method. */
11173   list = find_applicable_accessible_methods_list (lc, class, name, atl);
11174   list = find_most_specific_methods_list (list);
11175   if (list && !TREE_CHAIN (list))
11176     return TREE_VALUE (list);
11177
11178   /* Issue an error. List candidates if any. Candidates are listed
11179      only if accessible (non accessible methods may end-up here for
11180      the sake of a better error report). */
11181   candidates = NULL;
11182   if (list)
11183     {
11184       tree current;
11185       obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11186       for (current = list; current; current = TREE_CHAIN (current))
11187         {
11188           tree cm = TREE_VALUE (current);
11189           char string [4096];
11190           if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11191             continue;
11192           sprintf
11193             (string, "  '%s' in '%s'%s",
11194              get_printable_method_name (cm),
11195              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11196              (TREE_CHAIN (current) ? "\n" : ""));
11197           obstack_grow (&temporary_obstack, string, strlen (string));
11198         }
11199       obstack_1grow (&temporary_obstack, '\0');
11200       candidates = obstack_finish (&temporary_obstack);
11201     }
11202   /* Issue the error message */
11203   method = make_node (FUNCTION_TYPE);
11204   TYPE_ARG_TYPES (method) = atl;
11205   signature = build_java_argument_signature (method);
11206   dup = xstrdup (lang_printable_name (class, 0));
11207   parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11208                        (lc ? "constructor" : "method"),
11209                        (lc ? dup : IDENTIFIER_POINTER (name)),
11210                        IDENTIFIER_POINTER (signature), dup,
11211                        (candidates ? candidates : ""));
11212   free (dup);
11213   return NULL_TREE;
11214 }
11215
11216 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11217    when we're looking for a constructor. */
11218
11219 static tree
11220 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11221                                          tree arglist)
11222 {
11223   static htab_t searched_classes;
11224   static int search_not_done = 0;
11225   tree list = NULL_TREE, all_list = NULL_TREE;
11226   tree base_binfo;
11227   int i;
11228
11229   /* Check the hash table to determine if this class has been searched
11230      already. */
11231   if (searched_classes)
11232     {
11233       if (htab_find (searched_classes, class) != NULL)
11234         return NULL;
11235     }
11236   else
11237     {
11238       searched_classes = htab_create (10, htab_hash_pointer,
11239                                       htab_eq_pointer, NULL);
11240     }
11241
11242   search_not_done++;
11243   *htab_find_slot (searched_classes, class, INSERT) = class;
11244
11245   if (!CLASS_LOADED_P (class))
11246     {
11247       load_class (class, 1);
11248       safe_layout_class (class);
11249     }
11250
11251   /* Search interfaces */
11252   if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11253       && CLASS_INTERFACE (TYPE_NAME (class)))
11254     {
11255       search_applicable_methods_list (lc, TYPE_METHODS (class),
11256                                       name, arglist, &list, &all_list);
11257       for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11258         {
11259           tree t = BINFO_TYPE (base_binfo);
11260           tree rlist;
11261           
11262           rlist = find_applicable_accessible_methods_list (lc,  t, name,
11263                                                            arglist);
11264           list = chainon (rlist, list);
11265         }
11266     }
11267   /* Search classes */
11268   else
11269     {
11270       search_applicable_methods_list (lc, TYPE_METHODS (class),
11271                                       name, arglist, &list, &all_list);
11272
11273       /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11274          that we only search in class. Note that we should have found
11275          something at this point. */
11276       if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11277         {
11278           lc = 1;
11279           if (!list)
11280             abort ();
11281         }
11282
11283       /* We must search all interfaces of this class */
11284       if (!lc)
11285         {
11286           for (i = 1;
11287                BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11288             {
11289               tree t = BINFO_TYPE (base_binfo);
11290               if (t != object_type_node)
11291                 {
11292                   tree rlist
11293                     = find_applicable_accessible_methods_list (lc, t,
11294                                                                name, arglist);
11295                   list = chainon (rlist, list);
11296                 }
11297             }
11298         }
11299
11300       /* Search superclass */
11301       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11302         {
11303           tree rlist;
11304           class = CLASSTYPE_SUPER (class);
11305           rlist = find_applicable_accessible_methods_list (lc, class,
11306                                                            name, arglist);
11307           list = chainon (rlist, list);
11308         }
11309     }
11310
11311   search_not_done--;
11312
11313   /* We're done. Reset the searched classes list and finally search
11314      java.lang.Object if it wasn't searched already. */
11315   if (!search_not_done)
11316     {
11317       if (!lc
11318           && TYPE_METHODS (object_type_node)
11319           && htab_find (searched_classes, object_type_node) == NULL)
11320         {
11321           search_applicable_methods_list (lc,
11322                                           TYPE_METHODS (object_type_node),
11323                                           name, arglist, &list, &all_list);
11324         }
11325       htab_delete (searched_classes);
11326       searched_classes = NULL;
11327     }
11328
11329   /* Either return the list obtained or all selected (but
11330      inaccessible) methods for better error report. */
11331   return (!list ? all_list : list);
11332 }
11333
11334 /* Effectively search for the appropriate method in method */
11335
11336 static void
11337 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11338                                 tree *list, tree *all_list)
11339 {
11340   for (; method; method = TREE_CHAIN (method))
11341     {
11342       /* When dealing with constructor, stop here, otherwise search
11343          other classes */
11344       if (lc && !DECL_CONSTRUCTOR_P (method))
11345         continue;
11346       else if (!lc && (DECL_CONSTRUCTOR_P (method)
11347                        || (DECL_NAME (method) != name)))
11348         continue;
11349
11350       if (argument_types_convertible (method, arglist))
11351         {
11352           /* Retain accessible methods only */
11353           if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11354                                  method, NULL_TREE, 0))
11355             *list = tree_cons (NULL_TREE, method, *list);
11356           else
11357             /* Also retain all selected method here */
11358             *all_list = tree_cons (NULL_TREE, method, *list);
11359         }
11360     }
11361 }
11362
11363 /* 15.11.2.2 Choose the Most Specific Method */
11364
11365 static tree
11366 find_most_specific_methods_list (tree list)
11367 {
11368   int max = 0;
11369   int abstract, candidates;
11370   tree current, new_list = NULL_TREE;
11371   for (current = list; current; current = TREE_CHAIN (current))
11372     {
11373       tree method;
11374       DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11375
11376       for (method = list; method; method = TREE_CHAIN (method))
11377         {
11378           tree method_v, current_v;
11379           /* Don't test a method against itself */
11380           if (method == current)
11381             continue;
11382
11383           method_v = TREE_VALUE (method);
11384           current_v = TREE_VALUE (current);
11385
11386           /* Compare arguments and location where methods where declared */
11387           if (argument_types_convertible (method_v, current_v))
11388             {
11389               if (valid_method_invocation_conversion_p
11390                   (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11391                   || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11392                       && enclosing_context_p (DECL_CONTEXT (method_v),
11393                                               DECL_CONTEXT (current_v))))
11394                 {
11395                   int v = (DECL_SPECIFIC_COUNT (current_v) +=
11396                     (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11397                   max = (v > max ? v : max);
11398                 }
11399             }
11400         }
11401     }
11402
11403   /* Review the list and select the maximally specific methods */
11404   for (current = list, abstract = -1, candidates = -1;
11405        current; current = TREE_CHAIN (current))
11406     if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11407       {
11408         new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11409         abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11410         candidates++;
11411       }
11412
11413   /* If we have several and they're all abstract, just pick the
11414      closest one. */
11415   if (candidates > 0 && candidates == abstract)
11416     {
11417       /* FIXME: merge the throws clauses.  There is no convenient way
11418          to do this in gcj right now, since ideally we'd like to
11419          introduce a new METHOD_DECL here, but that is really not
11420          possible.  */
11421       new_list = nreverse (new_list);
11422       TREE_CHAIN (new_list) = NULL_TREE;
11423       return new_list;
11424     }
11425
11426   /* We have several (we couldn't find a most specific), all but one
11427      are abstract, we pick the only non abstract one. */
11428   if (candidates > 0 && (candidates == abstract+1))
11429     {
11430       for (current = new_list; current; current = TREE_CHAIN (current))
11431         if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11432           {
11433             TREE_CHAIN (current) = NULL_TREE;
11434             new_list = current;
11435           }
11436     }
11437
11438   /* If we can't find one, lower expectations and try to gather multiple
11439      maximally specific methods */
11440   while (!new_list && max)
11441     {
11442       while (--max > 0)
11443         {
11444           if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11445             new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11446         }
11447     }
11448
11449   return new_list;
11450 }
11451
11452 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11453    converted by method invocation conversion (5.3) to the type of the
11454    corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11455    to change less often than M1. */
11456
11457 static GTY(()) tree m2_arg_value;
11458 static GTY(()) tree m2_arg_cache;
11459
11460 static int
11461 argument_types_convertible (tree m1, tree m2_or_arglist)
11462 {
11463   tree m1_arg, m2_arg;
11464
11465   SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11466
11467   if (m2_arg_value == m2_or_arglist)
11468     m2_arg = m2_arg_cache;
11469   else
11470     {
11471       /* M2_OR_ARGLIST can be a function DECL or a raw list of
11472          argument types */
11473       if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11474         {
11475           m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11476           if (!METHOD_STATIC (m2_or_arglist))
11477             m2_arg = TREE_CHAIN (m2_arg);
11478         }
11479       else
11480         m2_arg = m2_or_arglist;
11481
11482       m2_arg_value = m2_or_arglist;
11483       m2_arg_cache = m2_arg;
11484     }
11485
11486   while (m1_arg != end_params_node && m2_arg != end_params_node)
11487     {
11488       resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11489       if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11490                                                  TREE_VALUE (m2_arg)))
11491         break;
11492       m1_arg = TREE_CHAIN (m1_arg);
11493       m2_arg = TREE_CHAIN (m2_arg);
11494     }
11495   return m1_arg == end_params_node && m2_arg == end_params_node;
11496 }
11497
11498 /* Qualification routines */
11499
11500 /* Given a name x.y.z, look up x locally.  If it's found, save the
11501    decl.  If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11502    so that we later try and load the appropriate classes.  */
11503 static void
11504 qualify_ambiguous_name (tree id)
11505 {
11506   tree name, decl;
11507
11508   /* We inspect the first item of the qualification list.  As a sanity
11509      check, make sure that it is an identfier node.  */
11510   tree qual = EXPR_WFL_QUALIFICATION (id);
11511   tree qual_wfl = QUAL_WFL (qual);
11512
11513   if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11514     return;
11515
11516   name = EXPR_WFL_NODE (qual_wfl);
11517
11518   /* If we don't have an identifier, or we have a 'this' or 'super',
11519      then field access processing is all we need : there is nothing
11520      for us to do.  */
11521   if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11522       name == this_identifier_node ||
11523       name == super_identifier_node)
11524     return;
11525
11526   /* If name appears within the scope of a local variable declaration
11527      or parameter declaration, or is a field within an enclosing
11528      class, then it is an expression name.  Save the decl and let
11529      resolve_field_access do it's work.  */
11530   if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11531       (decl = lookup_field_wrapper (current_class, name)))
11532     {
11533       QUAL_RESOLUTION (qual) = decl;
11534       return;
11535     }
11536
11537   /* If name is a known class name (either declared or imported), mark
11538      us as a type name.  */
11539   if ((decl = resolve_and_layout (name, NULL_TREE)))
11540     {
11541       RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11542       QUAL_RESOLUTION (qual) = decl;
11543     }
11544
11545   /* Check here that NAME isn't declared by more than one
11546      type-import-on-demand declaration of the compilation unit
11547      containing NAME. FIXME */
11548
11549   /* We couldn't find a declaration for the name.  Assume for now that
11550      we have a qualified class name that needs to be loaded from an
11551      external class file.  */
11552   else
11553     RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11554
11555   /* Propagate the qualification across other components of the
11556      qualified name */
11557   for (qual = TREE_CHAIN (qual); qual;
11558        qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11559     {
11560       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11561         RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11562     }
11563
11564   /* Store the global qualification for the ambiguous part of ID back
11565      into ID fields */
11566   if (RESOLVE_TYPE_NAME_P (qual_wfl))
11567     RESOLVE_TYPE_NAME_P (id) = 1;
11568   else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11569     RESOLVE_PACKAGE_NAME_P (id) = 1;
11570 }
11571
11572 /* Patch tree nodes in a function body. When a BLOCK is found, push
11573    local variable decls if present.
11574    Same as java_complete_lhs, but does resolve static finals to values. */
11575
11576 static tree
11577 java_complete_tree (tree node)
11578 {
11579   node = java_complete_lhs (node);
11580   if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11581       && DECL_INITIAL (node) != NULL_TREE
11582       && !flag_emit_xref)
11583     {
11584       tree value = fold_constant_for_init (node, node);
11585       if (value != NULL_TREE)
11586         return value;
11587     }
11588   return node;
11589 }
11590
11591 static tree
11592 java_stabilize_reference (tree node)
11593 {
11594   if (TREE_CODE (node) == COMPOUND_EXPR)
11595     {
11596       tree op0 = TREE_OPERAND (node, 0);
11597       tree op1 = TREE_OPERAND (node, 1);
11598       TREE_OPERAND (node, 0) = save_expr (op0);
11599       TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11600       return node;
11601     }
11602   return stabilize_reference (node);
11603 }
11604
11605 /* Patch tree nodes in a function body. When a BLOCK is found, push
11606    local variable decls if present.
11607    Same as java_complete_tree, but does not resolve static finals to values. */
11608
11609 static tree
11610 java_complete_lhs (tree node)
11611 {
11612   tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11613   int flag;
11614
11615   /* CONVERT_EXPR always has its type set, even though it needs to be
11616      worked out. */
11617   if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11618     return node;
11619
11620   /* The switch block implements cases processing container nodes
11621      first.  Contained nodes are always written back. Leaves come
11622      next and return a value. */
11623   switch (TREE_CODE (node))
11624     {
11625     case BLOCK:
11626
11627       /* 1- Block section.
11628          Set the local values on decl names so we can identify them
11629          faster when they're referenced. At that stage, identifiers
11630          are legal so we don't check for declaration errors. */
11631       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11632         {
11633           DECL_CONTEXT (cn) = current_function_decl;
11634           IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11635         }
11636       if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11637           CAN_COMPLETE_NORMALLY (node) = 1;
11638       else
11639         {
11640           tree stmt = BLOCK_EXPR_BODY (node);
11641           tree *ptr;
11642           int error_seen = 0;
11643           if (TREE_CODE (stmt) == COMPOUND_EXPR)
11644             {
11645               /* Re-order from (((A; B); C); ...; Z) to
11646                  (A; (B; (C ; (...; Z)))).
11647                  This makes it easier to scan the statements left-to-right
11648                  without using recursion (which might overflow the stack
11649                  if the block has many statements. */
11650               for (;;)
11651                 {
11652                   tree left = TREE_OPERAND (stmt, 0);
11653                   if (TREE_CODE (left) != COMPOUND_EXPR)
11654                     break;
11655                   TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11656                   TREE_OPERAND (left, 1) = stmt;
11657                   stmt = left;
11658                 }
11659               BLOCK_EXPR_BODY (node) = stmt;
11660             }
11661
11662           /* Now do the actual complete, without deep recursion for
11663              long blocks. */
11664           ptr = &BLOCK_EXPR_BODY (node);
11665           while (TREE_CODE (*ptr) == COMPOUND_EXPR
11666                  && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11667             {
11668               tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11669               tree *next = &TREE_OPERAND (*ptr, 1);
11670               TREE_OPERAND (*ptr, 0) = cur;
11671               if (IS_EMPTY_STMT (cur))
11672                 {
11673                   /* Optimization;  makes it easier to detect empty bodies.
11674                      Most useful for <clinit> with all-constant initializer. */
11675                   *ptr = *next;
11676                   continue;
11677                 }
11678               if (TREE_CODE (cur) == ERROR_MARK)
11679                 error_seen++;
11680               else if (! CAN_COMPLETE_NORMALLY (cur))
11681                 {
11682                   wfl_op2 = *next;
11683                   for (;;)
11684                     {
11685                       if (TREE_CODE (wfl_op2) == BLOCK)
11686                         wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11687                       else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11688                         wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11689                       else
11690                         break;
11691                     }
11692                   if (TREE_CODE (wfl_op2) != CASE_EXPR
11693                       && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11694                     unreachable_stmt_error (*ptr);
11695                 }
11696               if (TREE_TYPE (*ptr) == NULL_TREE)
11697                 TREE_TYPE (*ptr) = void_type_node;
11698               ptr = next;
11699             }
11700           *ptr = java_complete_tree (*ptr);
11701
11702           if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11703             return error_mark_node;
11704           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11705         }
11706       /* Turn local bindings to null */
11707       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11708         IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11709
11710       TREE_TYPE (node) = void_type_node;
11711       break;
11712
11713       /* 2- They are expressions but ultimately deal with statements */
11714
11715     case THROW_EXPR:
11716       wfl_op1 = TREE_OPERAND (node, 0);
11717       COMPLETE_CHECK_OP_0 (node);
11718       /* 14.19 A throw statement cannot complete normally. */
11719       CAN_COMPLETE_NORMALLY (node) = 0;
11720       return patch_throw_statement (node, wfl_op1);
11721
11722     case SYNCHRONIZED_EXPR:
11723       wfl_op1 = TREE_OPERAND (node, 0);
11724       return patch_synchronized_statement (node, wfl_op1);
11725
11726     case TRY_EXPR:
11727       return patch_try_statement (node);
11728
11729     case TRY_FINALLY_EXPR:
11730       COMPLETE_CHECK_OP_0 (node);
11731       COMPLETE_CHECK_OP_1 (node);
11732       if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11733         /* Reduce try/finally nodes with an empty try block.  */
11734         return TREE_OPERAND (node, 1);
11735       if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11736         /* Likewise for an empty finally block.  */
11737         return TREE_OPERAND (node, 0);
11738       CAN_COMPLETE_NORMALLY (node)
11739         = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11740            && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11741       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11742       return node;
11743
11744     case LABELED_BLOCK_EXPR:
11745       PUSH_LABELED_BLOCK (node);
11746       if (LABELED_BLOCK_BODY (node))
11747         COMPLETE_CHECK_OP_1 (node);
11748       TREE_TYPE (node) = void_type_node;
11749       POP_LABELED_BLOCK ();
11750
11751       if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11752         {
11753           LABELED_BLOCK_BODY (node) = NULL_TREE;
11754           CAN_COMPLETE_NORMALLY (node) = 1;
11755         }
11756       else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11757         CAN_COMPLETE_NORMALLY (node) = 1;
11758       return node;
11759
11760     case EXIT_BLOCK_EXPR:
11761       return patch_bc_statement (node);
11762
11763     case CASE_EXPR:
11764       cn = java_complete_tree (TREE_OPERAND (node, 0));
11765       if (cn == error_mark_node)
11766         return cn;
11767
11768       /* First, the case expression must be constant. Values of final
11769          fields are accepted. */
11770       cn = fold (cn);
11771       if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11772           && JDECL_P (TREE_OPERAND (cn, 1))
11773           && FIELD_FINAL (TREE_OPERAND (cn, 1))
11774           && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11775         {
11776           cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11777                                        TREE_OPERAND (cn, 1));
11778         }
11779       /* Accept final locals too. */
11780       else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn) 
11781                && DECL_INITIAL (cn))
11782         cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11783
11784       if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11785         {
11786           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11787           parse_error_context (node, "Constant expression required");
11788           return error_mark_node;
11789         }
11790
11791       nn = ctxp->current_loop;
11792
11793       /* It must be assignable to the type of the switch expression. */
11794       if (!try_builtin_assignconv (NULL_TREE,
11795                                    TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11796         {
11797           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11798           parse_error_context
11799             (wfl_operator,
11800              "Incompatible type for case. Can't convert %qs to %<int%>",
11801              lang_printable_name (TREE_TYPE (cn), 0));
11802           return error_mark_node;
11803         }
11804
11805       cn = fold (convert (int_type_node, cn));
11806       TREE_CONSTANT_OVERFLOW (cn) = 0;
11807       CAN_COMPLETE_NORMALLY (cn) = 1;
11808
11809       /* Save the label on a list so that we can later check for
11810          duplicates.  */
11811       case_label_list = tree_cons (node, cn, case_label_list);
11812
11813       /* Multiple instance of a case label bearing the same value is
11814          checked later. The case expression is all right so far. */
11815       if (TREE_CODE (cn) == VAR_DECL)
11816         cn = DECL_INITIAL (cn);
11817       TREE_OPERAND (node, 0) = cn;
11818       TREE_TYPE (node) = void_type_node;
11819       CAN_COMPLETE_NORMALLY (node) = 1;
11820       TREE_SIDE_EFFECTS (node) = 1;
11821       break;
11822
11823     case DEFAULT_EXPR:
11824       nn = ctxp->current_loop;
11825       /* Only one default label is allowed per switch statement */
11826       if (SWITCH_HAS_DEFAULT (nn))
11827         {
11828 #ifdef USE_MAPPED_LOCATION
11829           SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11830 #else
11831           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11832 #endif
11833           parse_error_context (wfl_operator,
11834                                "Duplicate case label: %<default%>");
11835           return error_mark_node;
11836         }
11837       else
11838         SWITCH_HAS_DEFAULT (nn) = 1;
11839       TREE_TYPE (node) = void_type_node;
11840       TREE_SIDE_EFFECTS (node) = 1;
11841       CAN_COMPLETE_NORMALLY (node) = 1;
11842       break;
11843
11844     case SWITCH_EXPR:
11845     case LOOP_EXPR:
11846       PUSH_LOOP (node);
11847       /* Check whether the loop was enclosed in a labeled
11848          statement. If not, create one, insert the loop in it and
11849          return the node */
11850       nn = patch_loop_statement (node);
11851
11852       /* Anyways, walk the body of the loop */
11853       if (TREE_CODE (node) == LOOP_EXPR)
11854         TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11855       /* Switch statement: walk the switch expression and the cases */
11856       else
11857         node = patch_switch_statement (node);
11858
11859       if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11860         nn = error_mark_node;
11861       else
11862         {
11863           TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11864           /* If we returned something different, that's because we
11865              inserted a label. Pop the label too. */
11866           if (nn != node)
11867             {
11868               if (CAN_COMPLETE_NORMALLY (node))
11869                 CAN_COMPLETE_NORMALLY (nn) = 1;
11870               POP_LABELED_BLOCK ();
11871             }
11872         }
11873       POP_LOOP ();
11874       return nn;
11875
11876     case EXIT_EXPR:
11877       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11878       return patch_exit_expr (node);
11879
11880     case COND_EXPR:
11881       /* Condition */
11882       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11883       if (TREE_OPERAND (node, 0) == error_mark_node)
11884         return error_mark_node;
11885       /* then-else branches */
11886       TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11887       if (TREE_OPERAND (node, 1) == error_mark_node)
11888         return error_mark_node;
11889       {
11890         /* This is a special case due to build_assertion().  When
11891            assertions are disabled we build a COND_EXPR in which
11892            Operand 1 is the body of the assertion.  If that happens to
11893            be a string concatenation we'll need to patch it here.  */
11894         tree patched = patch_string (TREE_OPERAND (node, 1));
11895         if (patched)
11896           TREE_OPERAND (node, 1) = patched;
11897       }
11898      TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11899       if (TREE_OPERAND (node, 2) == error_mark_node)
11900         return error_mark_node;
11901       return patch_if_else_statement (node);
11902       break;
11903
11904     case CONDITIONAL_EXPR:
11905       /* Condition */
11906       wfl_op1 = TREE_OPERAND (node, 0);
11907       COMPLETE_CHECK_OP_0 (node);
11908       wfl_op2 = TREE_OPERAND (node, 1);
11909       COMPLETE_CHECK_OP_1 (node);
11910       wfl_op3 = TREE_OPERAND (node, 2);
11911       COMPLETE_CHECK_OP_2 (node);
11912       return patch_conditional_expr (node, wfl_op1, wfl_op2);
11913
11914       /* 3- Expression section */
11915     case COMPOUND_EXPR:
11916       wfl_op2 = TREE_OPERAND (node, 1);
11917       TREE_OPERAND (node, 0) = nn =
11918         java_complete_tree (TREE_OPERAND (node, 0));
11919       if (IS_EMPTY_STMT (wfl_op2))
11920         CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11921       else
11922         {
11923           if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11924             {
11925               /* An unreachable condition in a do-while statement
11926                  is *not* (technically) an unreachable statement. */
11927               nn = wfl_op2;
11928               if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11929                 nn = EXPR_WFL_NODE (nn);
11930               /* NN can be NULL_TREE exactly when UPDATE is, in
11931                  finish_for_loop.  */
11932               if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11933                 {
11934                   SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11935                   if (SUPPRESS_UNREACHABLE_ERROR (nn))
11936                     {
11937                       /* Perhaps this warning should have an
11938                          associated flag.  The code being compiled is
11939                          pedantically correct, but useless.  */
11940                       parse_warning_context (wfl_operator,
11941                                              "Unreachable statement");
11942                     }
11943                   else
11944                     parse_error_context (wfl_operator,
11945                                          "Unreachable statement");
11946                 }
11947             }
11948           TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11949           if (TREE_OPERAND (node, 1) == error_mark_node)
11950             return error_mark_node;
11951           /* Even though we might allow the case where the first
11952              operand doesn't return normally, we still should compute
11953              CAN_COMPLETE_NORMALLY correctly.  */
11954           CAN_COMPLETE_NORMALLY (node)
11955             = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11956                && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11957         }
11958       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11959       break;
11960
11961     case RETURN_EXPR:
11962       /* CAN_COMPLETE_NORMALLY (node) = 0; */
11963       return patch_return (node);
11964
11965     case EXPR_WITH_FILE_LOCATION:
11966       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11967           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11968         {
11969           tree wfl = node;
11970           node = resolve_expression_name (node, NULL);
11971           if (node == error_mark_node)
11972             return node;
11973           /* Keep line number information somewhere were it doesn't
11974              disrupt the completion process. */
11975           if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11976             {
11977               EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11978               TREE_OPERAND (node, 1) = wfl;
11979             }
11980           CAN_COMPLETE_NORMALLY (node) = 1;
11981         }
11982       else
11983         {
11984           tree body;
11985           location_t save_location = input_location;
11986 #ifdef USE_MAPPED_LOCATION
11987           input_location = EXPR_LOCATION (node);
11988           if (input_location == UNKNOWN_LOCATION)
11989             input_location = save_location;
11990 #else
11991           input_line = EXPR_WFL_LINENO (node);
11992 #endif
11993           body = java_complete_tree (EXPR_WFL_NODE (node));
11994           input_location = save_location;
11995           EXPR_WFL_NODE (node) = body;
11996           TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11997           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11998           if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
11999             {
12000               /* Makes it easier to constant fold, detect empty bodies. */
12001               return body;
12002             }
12003           if (body == error_mark_node)
12004             {
12005               /* Its important for the evaluation of assignment that
12006                  this mark on the TREE_TYPE is propagated. */
12007               TREE_TYPE (node) = error_mark_node;
12008               return error_mark_node;
12009             }
12010           else
12011             TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12012
12013         }
12014       break;
12015
12016     case NEW_ARRAY_EXPR:
12017       /* Patch all the dimensions */
12018       flag = 0;
12019       for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12020         {
12021           int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12022           tree dim = convert (int_type_node,
12023                               java_complete_tree (TREE_VALUE (cn)));
12024           if (dim == error_mark_node)
12025             {
12026               flag = 1;
12027               continue;
12028             }
12029           else
12030             {
12031               TREE_VALUE (cn) = dim;
12032               /* Setup the location of the current dimension, for
12033                  later error report. */
12034 #ifdef USE_MAPPED_LOCATION
12035               TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12036 #else
12037               TREE_PURPOSE (cn) =
12038                 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12039               EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12040 #endif
12041             }
12042         }
12043       /* They complete the array creation expression, if no errors
12044          were found. */
12045       CAN_COMPLETE_NORMALLY (node) = 1;
12046       return (flag ? error_mark_node
12047               : force_evaluation_order (patch_newarray (node)));
12048
12049     case NEW_ANONYMOUS_ARRAY_EXPR:
12050       /* Create the array type if necessary. */
12051       if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12052         {
12053           tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12054           if (!(type = resolve_type_during_patch (type)))
12055             return error_mark_node;
12056           type = build_array_from_name (type, NULL_TREE,
12057                                         ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12058           ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12059         }
12060       node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12061                                    ANONYMOUS_ARRAY_INITIALIZER (node));
12062       if (node == error_mark_node)
12063         return error_mark_node;
12064       CAN_COMPLETE_NORMALLY (node) = 1;
12065       return node;
12066
12067     case NEW_CLASS_EXPR:
12068     case CALL_EXPR:
12069       /* Complete function's argument(s) first */
12070       if (complete_function_arguments (node))
12071         return error_mark_node;
12072       else
12073         {
12074           tree decl, wfl = TREE_OPERAND (node, 0);
12075           int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12076           int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12077                            super_identifier_node);
12078           tree arguments;
12079 #ifdef USE_MAPPED_LOCATION
12080           source_location location = EXPR_LOCATION (node);
12081 #else
12082           int location = EXPR_WFL_LINECOL (node);
12083 #endif
12084
12085           node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12086                                           from_super, 0, &decl);
12087           if (node == error_mark_node)
12088             return error_mark_node;
12089
12090           if (TREE_CODE (node) == CALL_EXPR
12091               && TREE_OPERAND (node, 1) != NULL_TREE)
12092             arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12093           else
12094             arguments = NULL_TREE;
12095           check_thrown_exceptions (location, decl, arguments);
12096           /* If we call this(...), register signature and positions */
12097           if (in_this)
12098             DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12099               tree_cons (wfl, decl,
12100                          DECL_CONSTRUCTOR_CALLS (current_function_decl));
12101           CAN_COMPLETE_NORMALLY (node) = 1;
12102           return force_evaluation_order (node);
12103         }
12104
12105     case MODIFY_EXPR:
12106       /* Save potential wfls */
12107       wfl_op1 = TREE_OPERAND (node, 0);
12108       TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12109
12110       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12111           && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12112           && DECL_INITIAL (nn) != NULL_TREE)
12113         {
12114           tree value;
12115
12116           value = fold_constant_for_init (nn, nn);
12117
12118           /* When we have a primitype type, or a string and we're not
12119              emitting a class file, we actually don't want to generate
12120              anything for the assignment. */
12121           if (value != NULL_TREE && 
12122               (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) || 
12123                (TREE_TYPE (value) == string_ptr_type_node &&
12124                 ! flag_emit_class_files)))
12125             {
12126               /* Prepare node for patch_assignment */
12127               TREE_OPERAND (node, 1) = value;
12128               /* Call patch assignment to verify the assignment */
12129               if (patch_assignment (node, wfl_op1) == error_mark_node)
12130                 return error_mark_node;
12131               /* Set DECL_INITIAL properly (a conversion might have
12132                  been decided by patch_assignment) and return the
12133                  empty statement. */
12134               else
12135                 {
12136                   tree patched = patch_string (TREE_OPERAND (node, 1));
12137                   if (patched)
12138                     DECL_INITIAL (nn) = patched;
12139                   else
12140                     DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12141                   DECL_FIELD_FINAL_IUD (nn) = 1;
12142                   return build_java_empty_stmt ();
12143                 }
12144             }
12145           if (! flag_emit_class_files)
12146             DECL_INITIAL (nn) = NULL_TREE;
12147         }
12148       wfl_op2 = TREE_OPERAND (node, 1);
12149
12150       if (TREE_OPERAND (node, 0) == error_mark_node)
12151         return error_mark_node;
12152
12153       flag = COMPOUND_ASSIGN_P (wfl_op2);
12154       if (flag)
12155         {
12156           /* This might break when accessing outer field from inner
12157              class. TESTME, FIXME */
12158           tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12159
12160           /* Hand stabilize the lhs on both places */
12161           TREE_OPERAND (node, 0) = lvalue;
12162           TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12163             (flag_emit_class_files ? lvalue : save_expr (lvalue));
12164
12165           /* 15.25.2.a: Left hand is not an array access. FIXME */
12166           /* Now complete the RHS. We write it back later on. */
12167           nn = java_complete_tree (TREE_OPERAND (node, 1));
12168
12169           if ((cn = patch_string (nn)))
12170             nn = cn;
12171
12172           /* The last part of the rewrite for E1 op= E2 is to have
12173              E1 = (T)(E1 op E2), with T being the type of E1. */
12174           nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12175                                                TREE_TYPE (lvalue), nn));
12176
12177           /* If the assignment is compound and has reference type,
12178              then ensure the LHS has type String and nothing else.  */
12179           if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12180               && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12181             parse_error_context (wfl_op2,
12182                                  "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12183                                  lang_printable_name (TREE_TYPE (lvalue), 0));
12184
12185           /* 15.25.2.b: Left hand is an array access. FIXME */
12186         }
12187
12188       /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12189          function to complete this RHS. Note that a NEW_ARRAY_INIT
12190          might have been already fully expanded if created as a result
12191          of processing an anonymous array initializer. We avoid doing
12192          the operation twice by testing whether the node already bears
12193          a type. */
12194       else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12195         nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12196                                    TREE_OPERAND (node, 1));
12197       /* Otherwise we simply complete the RHS */
12198       else
12199         nn = java_complete_tree (TREE_OPERAND (node, 1));
12200
12201       if (nn == error_mark_node)
12202         return error_mark_node;
12203
12204       /* Write back the RHS as we evaluated it. */
12205       TREE_OPERAND (node, 1) = nn;
12206
12207       /* In case we're handling = with a String as a RHS, we need to
12208          produce a String out of the RHS (it might still be a
12209          STRING_CST or a StringBuffer at this stage */
12210       if ((nn = patch_string (TREE_OPERAND (node, 1))))
12211         TREE_OPERAND (node, 1) = nn;
12212
12213       if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12214                                         TREE_OPERAND (node, 1))))
12215         {
12216           /* We return error_mark_node if outer_field_access_fix
12217              detects we write into a final. */
12218           if (nn == error_mark_node)
12219             return error_mark_node;
12220           node = nn;
12221         }
12222       else
12223         {
12224           node = patch_assignment (node, wfl_op1);
12225           if (node == error_mark_node)
12226             return error_mark_node;
12227           /* Reorganize the tree if necessary. */
12228           if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12229                        || JSTRING_P (TREE_TYPE (node))))
12230             node = java_refold (node);
12231         }
12232
12233       /* Seek to set DECL_INITIAL to a proper value, since it might have
12234          undergone a conversion in patch_assignment. We do that only when
12235          it's necessary to have DECL_INITIAL properly set. */
12236       nn = TREE_OPERAND (node, 0);
12237       if (TREE_CODE (nn) == VAR_DECL
12238           && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12239           && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12240           && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12241               || TREE_TYPE (nn) == string_ptr_type_node))
12242         DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12243
12244       CAN_COMPLETE_NORMALLY (node) = 1;
12245       return node;
12246
12247     case MULT_EXPR:
12248     case PLUS_EXPR:
12249     case MINUS_EXPR:
12250     case LSHIFT_EXPR:
12251     case RSHIFT_EXPR:
12252     case URSHIFT_EXPR:
12253     case BIT_AND_EXPR:
12254     case BIT_XOR_EXPR:
12255     case BIT_IOR_EXPR:
12256     case TRUNC_MOD_EXPR:
12257     case TRUNC_DIV_EXPR:
12258     case RDIV_EXPR:
12259     case TRUTH_ANDIF_EXPR:
12260     case TRUTH_ORIF_EXPR:
12261     case EQ_EXPR:
12262     case NE_EXPR:
12263     case GT_EXPR:
12264     case GE_EXPR:
12265     case LT_EXPR:
12266     case LE_EXPR:
12267       /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12268          knows how to handle those cases. */
12269       wfl_op1 = TREE_OPERAND (node, 0);
12270       wfl_op2 = TREE_OPERAND (node, 1);
12271
12272       CAN_COMPLETE_NORMALLY (node) = 1;
12273       /* Don't complete string nodes if dealing with the PLUS operand. */
12274       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12275         {
12276           nn = java_complete_tree (wfl_op1);
12277           if (nn == error_mark_node)
12278             return error_mark_node;
12279
12280           TREE_OPERAND (node, 0) = nn;
12281         }
12282       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12283         {
12284           nn = java_complete_tree (wfl_op2);
12285           if (nn == error_mark_node)
12286             return error_mark_node;
12287
12288           TREE_OPERAND (node, 1) = nn;
12289         }
12290       return patch_binop (node, wfl_op1, wfl_op2);
12291
12292     case INSTANCEOF_EXPR:
12293       wfl_op1 = TREE_OPERAND (node, 0);
12294       COMPLETE_CHECK_OP_0 (node);
12295       if (flag_emit_xref)
12296         {
12297           TREE_TYPE (node) = boolean_type_node;
12298           return node;
12299         }
12300       return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12301
12302     case UNARY_PLUS_EXPR:
12303     case NEGATE_EXPR:
12304     case TRUTH_NOT_EXPR:
12305     case BIT_NOT_EXPR:
12306     case PREDECREMENT_EXPR:
12307     case PREINCREMENT_EXPR:
12308     case POSTDECREMENT_EXPR:
12309     case POSTINCREMENT_EXPR:
12310     case CONVERT_EXPR:
12311       /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12312          how to handle those cases. */
12313       wfl_op1 = TREE_OPERAND (node, 0);
12314       CAN_COMPLETE_NORMALLY (node) = 1;
12315       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12316       if (TREE_OPERAND (node, 0) == error_mark_node)
12317         return error_mark_node;
12318       node = patch_unaryop (node, wfl_op1);
12319       CAN_COMPLETE_NORMALLY (node) = 1;
12320       break;
12321
12322     case ARRAY_REF:
12323       /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12324          how to handle those cases. */
12325       wfl_op1 = TREE_OPERAND (node, 0);
12326       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12327       if (TREE_OPERAND (node, 0) == error_mark_node)
12328         return error_mark_node;
12329       if (!flag_emit_class_files && !flag_emit_xref)
12330         TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12331       /* The same applies to wfl_op2 */
12332       wfl_op2 = TREE_OPERAND (node, 1);
12333       TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12334       if (TREE_OPERAND (node, 1) == error_mark_node)
12335         return error_mark_node;
12336       if (!flag_emit_class_files && !flag_emit_xref)
12337         TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12338       return patch_array_ref (node);
12339
12340     case RECORD_TYPE:
12341       return node;;
12342
12343     case COMPONENT_REF:
12344       /* The first step in the re-write of qualified name handling.  FIXME.
12345          So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12346       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12347       if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12348         {
12349           tree name = TREE_OPERAND (node, 1);
12350           tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12351           if (field == NULL_TREE)
12352             {
12353               error ("missing static field %qs", IDENTIFIER_POINTER (name));
12354               return error_mark_node;
12355             }
12356           if (! FIELD_STATIC (field))
12357             {
12358               error ("not a static field %qs", IDENTIFIER_POINTER (name));
12359               return error_mark_node;
12360             }
12361           return field;
12362         }
12363       else
12364         abort ();
12365       break;
12366
12367     case THIS_EXPR:
12368       /* Can't use THIS in a static environment */
12369       if (!current_this)
12370         {
12371           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12372           parse_error_context (wfl_operator,
12373                                "Keyword %<this%> used outside allowed context");
12374           TREE_TYPE (node) = error_mark_node;
12375           return error_mark_node;
12376         }
12377       if (ctxp->explicit_constructor_p)
12378         {
12379           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12380           parse_error_context
12381             (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12382           TREE_TYPE (node) = error_mark_node;
12383           return error_mark_node;
12384         }
12385       return current_this;
12386
12387     case CLASS_LITERAL:
12388       CAN_COMPLETE_NORMALLY (node) = 1;
12389       node = patch_incomplete_class_ref (node);
12390       if (node == error_mark_node)
12391         return error_mark_node;
12392       break;
12393
12394     default:
12395       CAN_COMPLETE_NORMALLY (node) = 1;
12396       /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12397          and it's time to turn it into the appropriate String object */
12398       if ((nn = patch_string (node)))
12399         node = nn;
12400       else
12401         internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12402     }
12403   return node;
12404 }
12405
12406 /* Complete function call's argument. Return a nonzero value is an
12407    error was found.  */
12408
12409 static int
12410 complete_function_arguments (tree node)
12411 {
12412   int flag = 0;
12413   tree cn;
12414
12415   ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12416   for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12417     {
12418       tree wfl = TREE_VALUE (cn), parm, temp;
12419       parm = java_complete_tree (wfl);
12420
12421       if (parm == error_mark_node)
12422         {
12423           flag = 1;
12424           continue;
12425         }
12426       /* If we have a string literal that we haven't transformed yet or a
12427          crafted string buffer, as a result of the use of the String
12428          `+' operator. Build `parm.toString()' and expand it. */
12429       if ((temp = patch_string (parm)))
12430         parm = temp;
12431
12432       TREE_VALUE (cn) = parm;
12433     }
12434   ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12435   return flag;
12436 }
12437
12438 /* Sometimes (for loops and variable initialized during their
12439    declaration), we want to wrap a statement around a WFL and turn it
12440    debugable.  */
12441
12442 static tree
12443 build_debugable_stmt (int location, tree stmt)
12444 {
12445   if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12446     {
12447 #ifdef USE_MAPPED_LOCATION
12448       stmt = expr_add_location (stmt, location, 1);
12449 #else
12450       stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12451       EXPR_WFL_LINECOL (stmt) = location;
12452       JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12453 #endif
12454     }
12455   return stmt;
12456 }
12457
12458 static tree
12459 build_expr_block (tree body, tree decls)
12460 {
12461   tree node = make_node (BLOCK);
12462   BLOCK_EXPR_DECLS (node) = decls;
12463   BLOCK_EXPR_BODY (node) = body;
12464   if (body)
12465     TREE_TYPE (node) = TREE_TYPE (body);
12466   TREE_SIDE_EFFECTS (node) = 1;
12467   return node;
12468 }
12469
12470 /* Create a new function block and link it appropriately to current
12471    function block chain */
12472
12473 static tree
12474 enter_block (void)
12475 {
12476   tree b = build_expr_block (NULL_TREE, NULL_TREE);
12477
12478   /* Link block B supercontext to the previous block. The current
12479      function DECL is used as supercontext when enter_a_block is called
12480      for the first time for a given function. The current function body
12481      (DECL_FUNCTION_BODY) is set to be block B.  */
12482
12483   tree fndecl = current_function_decl;
12484
12485   if (!fndecl) {
12486     BLOCK_SUPERCONTEXT (b) = current_static_block;
12487     current_static_block = b;
12488   }
12489
12490   else if (!DECL_FUNCTION_BODY (fndecl))
12491     {
12492       BLOCK_SUPERCONTEXT (b) = fndecl;
12493       DECL_FUNCTION_BODY (fndecl) = b;
12494     }
12495   else
12496     {
12497       BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12498       DECL_FUNCTION_BODY (fndecl) = b;
12499     }
12500   return b;
12501 }
12502
12503 /* Exit a block by changing the current function body
12504    (DECL_FUNCTION_BODY) to the current block super context, only if
12505    the block being exited isn't the method's top level one.  */
12506
12507 static tree
12508 exit_block (void)
12509 {
12510   tree b;
12511   if (current_function_decl)
12512     {
12513       b = DECL_FUNCTION_BODY (current_function_decl);
12514       if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12515         DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12516     }
12517   else
12518     {
12519       b = current_static_block;
12520
12521       if (BLOCK_SUPERCONTEXT (b))
12522         current_static_block = BLOCK_SUPERCONTEXT (b);
12523     }
12524   return b;
12525 }
12526
12527 /* Lookup for NAME in the nested function's blocks, all the way up to
12528    the current toplevel one. It complies with Java's local variable
12529    scoping rules.  */
12530
12531 static tree
12532 lookup_name_in_blocks (tree name)
12533 {
12534   tree b = GET_CURRENT_BLOCK (current_function_decl);
12535
12536   while (b != current_function_decl)
12537     {
12538       tree current;
12539
12540       /* Paranoid sanity check. To be removed */
12541       if (TREE_CODE (b) != BLOCK)
12542         abort ();
12543
12544       for (current = BLOCK_EXPR_DECLS (b); current;
12545            current = TREE_CHAIN (current))
12546         if (DECL_NAME (current) == name)
12547           return current;
12548       b = BLOCK_SUPERCONTEXT (b);
12549     }
12550   return NULL_TREE;
12551 }
12552
12553 static void
12554 maybe_absorb_scoping_blocks (void)
12555 {
12556   while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12557     {
12558       tree b = exit_block ();
12559       java_method_add_stmt (current_function_decl, b);
12560       SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12561     }
12562 }
12563
12564 \f
12565 /* This section of the source is reserved to build_* functions that
12566    are building incomplete tree nodes and the patch_* functions that
12567    are completing them.  */
12568
12569 /* Wrap a non WFL node around a WFL.  */
12570
12571 static tree
12572 build_wfl_wrap (tree node, int location)
12573 {
12574   tree wfl, node_to_insert = node;
12575
12576   /* We want to process THIS . xxx symbolically, to keep it consistent
12577      with the way we're processing SUPER. A THIS from a primary as a
12578      different form than a SUPER. Turn THIS into something symbolic */
12579   if (TREE_CODE (node) == THIS_EXPR)
12580     node_to_insert = wfl = build_wfl_node (this_identifier_node);
12581   else
12582 #ifdef USE_MAPPED_LOCATION
12583     wfl = build_unknown_wfl (NULL_TREE);
12584
12585   SET_EXPR_LOCATION (wfl, location);
12586 #else
12587     wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12588
12589   EXPR_WFL_LINECOL (wfl) = location;
12590 #endif
12591   EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12592   return wfl;
12593 }
12594
12595 /* Build a super() constructor invocation. Returns an empty statement if
12596    we're currently dealing with the class java.lang.Object. */
12597
12598 static tree
12599 build_super_invocation (tree mdecl)
12600 {
12601   if (DECL_CONTEXT (mdecl) == object_type_node)
12602     return build_java_empty_stmt ();
12603   else
12604     {
12605       tree super_wfl = build_wfl_node (super_identifier_node);
12606       tree a = NULL_TREE, t;
12607
12608       /* This is called after parsing is done, so the parser context
12609          won't be accurate. Set location info from current_class decl. */
12610       tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12611       EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12612
12613       /* If we're dealing with an anonymous class, pass the arguments
12614          of the crafted constructor along. */
12615       if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12616         {
12617           SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12618           for (; t != end_params_node; t = TREE_CHAIN (t))
12619             a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12620         }
12621       return build_method_invocation (super_wfl, a);
12622     }
12623 }
12624
12625 /* Build a SUPER/THIS qualified method invocation.  */
12626
12627 static tree
12628 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12629                                        int lloc, int rloc)
12630 {
12631   tree invok;
12632   tree wfl =
12633     build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12634   EXPR_WFL_LINECOL (wfl) = lloc;
12635   invok = build_method_invocation (name, args);
12636   return make_qualified_primary (wfl, invok, rloc);
12637 }
12638
12639 /* Build an incomplete CALL_EXPR node. */
12640
12641 static tree
12642 build_method_invocation (tree name, tree args)
12643 {
12644   tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12645   TREE_SIDE_EFFECTS (call) = 1;
12646   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12647   return call;
12648 }
12649
12650 /* Build an incomplete new xxx(...) node. */
12651
12652 static tree
12653 build_new_invocation (tree name, tree args)
12654 {
12655   tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12656   TREE_SIDE_EFFECTS (call) = 1;
12657   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12658   return call;
12659 }
12660
12661 /* Build an incomplete assignment expression. */
12662
12663 static tree
12664 build_assignment (int op, int op_location, tree lhs, tree rhs)
12665 {
12666   tree assignment;
12667   /* Build the corresponding binop if we deal with a Compound
12668      Assignment operator. Mark the binop sub-tree as part of a
12669      Compound Assignment expression */
12670   if (op != ASSIGN_TK)
12671     {
12672       rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12673       COMPOUND_ASSIGN_P (rhs) = 1;
12674     }
12675   assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12676   TREE_SIDE_EFFECTS (assignment) = 1;
12677   EXPR_WFL_LINECOL (assignment) = op_location;
12678   return assignment;
12679 }
12680
12681 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12682    the buffer.  This is used only for string conversion.  */
12683 static char *
12684 string_convert_int_cst (tree node)
12685 {
12686   /* Long.MIN_VALUE is -9223372036854775808, 20 characters.  */
12687   static char buffer[21];
12688
12689   unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12690   unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12691   char *p = buffer + sizeof (buffer);
12692   int neg = 0;
12693
12694   unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12695                                   << (HOST_BITS_PER_WIDE_INT - 1));
12696
12697   *--p = '\0';
12698
12699   /* If negative, note the fact and negate the value.  */
12700   if ((hi & hibit))
12701     {
12702       lo = ~lo;
12703       hi = ~hi;
12704       if (++lo == 0)
12705         ++hi;
12706       neg = 1;
12707     }
12708
12709   /* Divide by 10 until there are no bits left.  */
12710   do
12711     {
12712       unsigned HOST_WIDE_INT acc = 0;
12713       unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12714       unsigned int i;
12715
12716       /* Use long division to compute the result and the remainder.  */
12717       for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12718         {
12719           /* Shift a bit into accumulator.  */
12720           acc <<= 1;
12721           if ((hi & hibit))
12722             acc |= 1;
12723
12724           /* Shift the value.  */
12725           hi <<= 1;
12726           if ((lo & hibit))
12727             hi |= 1;
12728           lo <<= 1;
12729
12730           /* Shift the correct bit into the result.  */
12731           outhi <<= 1;
12732           if ((outlo & hibit))
12733             outhi |= 1;
12734           outlo <<= 1;
12735           if (acc >= 10)
12736             {
12737               acc -= 10;
12738               outlo |= 1;
12739             }
12740         }
12741
12742       /* '0' == 060 in Java, but might not be here (think EBCDIC).  */
12743       *--p = '\060' + acc;
12744
12745       hi = outhi;
12746       lo = outlo;
12747     }
12748   while (hi || lo);
12749
12750   if (neg)
12751     *--p = '\055'; /* '-' == 055 in Java, but might not be here.  */
12752
12753   return p;
12754 }
12755
12756 /* Print an INTEGER_CST node in a static buffer, and return the
12757    buffer.  This is used only for error handling.  */
12758 char *
12759 print_int_node (tree node)
12760 {
12761   static char buffer [80];
12762   if (TREE_CONSTANT_OVERFLOW (node))
12763     sprintf (buffer, "<overflow>");
12764
12765   if (TREE_INT_CST_HIGH (node) == 0)
12766     sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12767              TREE_INT_CST_LOW (node));
12768   else if (TREE_INT_CST_HIGH (node) == -1
12769            && TREE_INT_CST_LOW (node) != 0)
12770     sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12771              -TREE_INT_CST_LOW (node));
12772   else
12773     sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12774              TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12775
12776   return buffer;
12777 }
12778
12779 \f
12780 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12781    context.  */
12782
12783 /* 15.25 Assignment operators. */
12784
12785 static tree
12786 patch_assignment (tree node, tree wfl_op1)
12787 {
12788   tree rhs = TREE_OPERAND (node, 1);
12789   tree lvalue = TREE_OPERAND (node, 0), llvalue;
12790   tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12791   int error_found = 0;
12792   int lvalue_from_array = 0;
12793   int is_return = 0;
12794
12795   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12796
12797   /* Lhs can be a named variable */
12798   if (JDECL_P (lvalue))
12799     {
12800       lhs_type = TREE_TYPE (lvalue);
12801     }
12802   /* Or Lhs can be an array access. */
12803   else if (TREE_CODE (lvalue) == ARRAY_REF)
12804     {
12805       lhs_type = TREE_TYPE (lvalue);
12806       lvalue_from_array = 1;
12807     }
12808   /* Or a field access */
12809   else if (TREE_CODE (lvalue) == COMPONENT_REF)
12810     lhs_type = TREE_TYPE (lvalue);
12811   /* Or a function return slot */
12812   else if (TREE_CODE (lvalue) == RESULT_DECL)
12813     {
12814       /* If the return type is an integral type, then we create the
12815          RESULT_DECL with a promoted type, but we need to do these
12816          checks against the unpromoted type to ensure type safety.  So
12817          here we look at the real type, not the type of the decl we
12818          are modifying.  */
12819       lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12820       is_return = 1;
12821     }
12822   /* Otherwise, we might want to try to write into an optimized static
12823      final, this is an of a different nature, reported further on. */
12824   else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12825            && resolve_expression_name (wfl_op1, &llvalue))
12826     {
12827       lhs_type = TREE_TYPE (lvalue);
12828     }
12829   else
12830     {
12831       parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12832       error_found = 1;
12833     }
12834
12835   rhs_type = TREE_TYPE (rhs);
12836
12837   /* 5.1 Try the assignment conversion for builtin type. */
12838   new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12839
12840   /* 5.2 If it failed, try a reference conversion */
12841   if (!new_rhs)
12842     new_rhs = try_reference_assignconv (lhs_type, rhs);
12843
12844   /* 15.25.2 If we have a compound assignment, convert RHS into the
12845      type of the LHS */
12846   else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12847     new_rhs = convert (lhs_type, rhs);
12848
12849   /* Explicit cast required. This is an error */
12850   if (!new_rhs)
12851     {
12852       char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12853       char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12854       tree wfl;
12855       char operation [32];      /* Max size known */
12856
12857       /* If the assignment is part of a declaration, we use the WFL of
12858          the declared variable to point out the error and call it a
12859          declaration problem. If the assignment is a genuine =
12860          operator, we call is a operator `=' problem, otherwise we
12861          call it an assignment problem. In both of these last cases,
12862          we use the WFL of the operator to indicate the error. */
12863
12864       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12865         {
12866           wfl = wfl_op1;
12867           strcpy (operation, "declaration");
12868         }
12869       else
12870         {
12871           wfl = wfl_operator;
12872           if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12873             strcpy (operation, "assignment");
12874           else if (is_return)
12875             strcpy (operation, "'return'");
12876           else
12877             strcpy (operation, "'='");
12878         }
12879
12880       if (!valid_cast_to_p (rhs_type, lhs_type))
12881         parse_error_context
12882           (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12883            operation, t1, t2);
12884       else
12885         parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12886                              operation, t1, t2);
12887       free (t1); free (t2);
12888       error_found = 1;
12889     }
12890
12891   if (error_found)
12892     return error_mark_node;
12893
12894   /* If we're processing a `return' statement, promote the actual type
12895      to the promoted type.  */
12896   if (is_return)
12897     new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12898
12899   /* 10.10: Array Store Exception runtime check */
12900   if (!flag_emit_class_files
12901       && !flag_emit_xref
12902       && lvalue_from_array
12903       && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12904     {
12905       tree array, store_check, base, index_expr;
12906
12907       /* Save RHS so that it doesn't get re-evaluated by the store check. */
12908       new_rhs = save_expr (new_rhs);
12909
12910       /* Get the INDIRECT_REF. */
12911       array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12912       /* Get the array pointer expr. */
12913       array = TREE_OPERAND (array, 0);
12914       store_check = build_java_arraystore_check (array, new_rhs);
12915
12916       index_expr = TREE_OPERAND (lvalue, 1);
12917
12918       if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12919         {
12920           /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12921              happen before the store check, so prepare to insert the store
12922              check within the second operand of the existing COMPOUND_EXPR. */
12923           base = index_expr;
12924         }
12925       else
12926         base = lvalue;
12927
12928       index_expr = TREE_OPERAND (base, 1);
12929       TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12930                                        store_check, index_expr);
12931     }
12932
12933   /* Final locals can be used as case values in switch
12934      statement. Prepare them for this eventuality. */
12935   if (TREE_CODE (lvalue) == VAR_DECL
12936       && DECL_FINAL (lvalue)
12937       && TREE_CONSTANT (new_rhs)
12938       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12939       && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12940       )
12941     {
12942       TREE_CONSTANT (lvalue) = 1;
12943       TREE_INVARIANT (lvalue) = 1;
12944       DECL_INITIAL (lvalue) = new_rhs;
12945     }
12946
12947   /* Copy the rhs if it's a reference.  */
12948   if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12949     {
12950       switch (TREE_CODE (new_rhs))
12951         {
12952         case ARRAY_REF:
12953         case INDIRECT_REF:
12954         case COMPONENT_REF:
12955           /* Transform a = foo.bar 
12956              into a = ({int tmp; tmp = foo.bar;}).
12957              We need to ensure that if a read from memory fails
12958              because of a NullPointerException, a destination variable
12959              will remain unchanged.  An explicit temporary does what
12960              we need.  
12961
12962              If flag_check_references is set, this is unnecessary
12963              because we'll check each reference before doing any
12964              reads.  If optimize is not set the result will never be
12965              written to a stack slot that contains the LHS.  */
12966           {
12967             tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"), 
12968                                    TREE_TYPE (new_rhs));
12969             tree block = make_node (BLOCK);
12970             tree assignment 
12971               = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12972             DECL_CONTEXT (tmp) = current_function_decl;
12973             TREE_TYPE (block) = TREE_TYPE (new_rhs);
12974             BLOCK_VARS (block) = tmp;
12975             BLOCK_EXPR_BODY (block) = assignment;
12976             TREE_SIDE_EFFECTS (block) = 1;
12977             new_rhs = block;
12978           }
12979           break;
12980         default:
12981           break;
12982         }
12983     }
12984
12985   TREE_OPERAND (node, 0) = lvalue;
12986   TREE_OPERAND (node, 1) = new_rhs;
12987   TREE_TYPE (node) = lhs_type;
12988   return node;
12989 }
12990
12991 /* Check that type SOURCE can be cast into type DEST. If the cast
12992    can't occur at all, return NULL; otherwise, return a possibly
12993    modified rhs.  */
12994
12995 static tree
12996 try_reference_assignconv (tree lhs_type, tree rhs)
12997 {
12998   tree new_rhs = NULL_TREE;
12999   tree rhs_type = TREE_TYPE (rhs);
13000
13001   if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13002     {
13003       /* `null' may be assigned to any reference type */
13004       if (rhs == null_pointer_node)
13005         new_rhs = null_pointer_node;
13006       /* Try the reference assignment conversion */
13007       else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13008         new_rhs = rhs;
13009       /* This is a magic assignment that we process differently */
13010       else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13011         new_rhs = rhs;
13012     }
13013   return new_rhs;
13014 }
13015
13016 /* Check that RHS can be converted into LHS_TYPE by the assignment
13017    conversion (5.2), for the cases of RHS being a builtin type. Return
13018    NULL_TREE if the conversion fails or if because RHS isn't of a
13019    builtin type. Return a converted RHS if the conversion is possible.  */
13020
13021 static tree
13022 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13023 {
13024   tree new_rhs = NULL_TREE;
13025   tree rhs_type = TREE_TYPE (rhs);
13026
13027   /* Handle boolean specially.  */
13028   if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13029       || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13030     {
13031       if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13032           && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13033         new_rhs = rhs;
13034     }
13035
13036   /* 5.1.1 Try Identity Conversion,
13037      5.1.2 Try Widening Primitive Conversion */
13038   else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13039     new_rhs = convert (lhs_type, rhs);
13040
13041   /* Try a narrowing primitive conversion (5.1.3):
13042        - expression is a constant expression of type byte, short, char,
13043          or int, AND
13044        - variable is byte, short or char AND
13045        - The value of the expression is representable in the type of the
13046          variable */
13047   else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13048             || rhs_type == char_type_node || rhs_type == int_type_node)
13049             && TREE_CONSTANT (rhs)
13050            && (lhs_type == byte_type_node || lhs_type == char_type_node
13051                || lhs_type == short_type_node))
13052     {
13053       if (int_fits_type_p (rhs, lhs_type))
13054         new_rhs = convert (lhs_type, rhs);
13055       else if (wfl_op1)         /* Might be called with a NULL */
13056         parse_warning_context
13057           (wfl_op1,
13058            "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13059            print_int_node (rhs), lang_printable_name (lhs_type, 0));
13060       /* Reported a warning that will turn into an error further
13061          down, so we don't return */
13062     }
13063
13064   return new_rhs;
13065 }
13066
13067 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13068    conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
13069    0 is the conversion test fails.  This implements parts the method
13070    invocation conversion (5.3).  */
13071
13072 static int
13073 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13074 {
13075   /* 5.1.1: This is the identity conversion part. */
13076   if (lhs_type == rhs_type)
13077     return 1;
13078
13079   /* Reject non primitive types and boolean conversions.  */
13080   if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13081     return 0;
13082
13083   /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13084      than a char can't be converted into a char. Short can't too, but
13085      the < test below takes care of that */
13086   if (lhs_type == char_type_node && rhs_type == byte_type_node)
13087     return 0;
13088
13089   /* Accept all promoted type here. Note, we can't use <= in the test
13090      below, because we still need to bounce out assignments of short
13091      to char and the likes */
13092   if (lhs_type == int_type_node
13093       && (rhs_type == promoted_byte_type_node
13094           || rhs_type == promoted_short_type_node
13095           || rhs_type == promoted_char_type_node
13096           || rhs_type == promoted_boolean_type_node))
13097     return 1;
13098
13099   /* From here, an integral is widened if its precision is smaller
13100      than the precision of the LHS or if the LHS is a floating point
13101      type, or the RHS is a float and the RHS a double. */
13102   if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13103        && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13104       || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13105       || (rhs_type == float_type_node && lhs_type == double_type_node))
13106     return 1;
13107
13108   return 0;
13109 }
13110
13111 /* Check that something of SOURCE type can be assigned or cast to
13112    something of DEST type at runtime. Return 1 if the operation is
13113    valid, 0 otherwise. If CAST is set to 1, we're treating the case
13114    were SOURCE is cast into DEST, which borrows a lot of the
13115    assignment check. */
13116
13117 static int
13118 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13119 {
13120   /* SOURCE or DEST might be null if not from a declared entity. */
13121   if (!source || !dest)
13122     return 0;
13123   if (JNULLP_TYPE_P (source))
13124     return 1;
13125   if (TREE_CODE (source) == POINTER_TYPE)
13126     source = TREE_TYPE (source);
13127   if (TREE_CODE (dest) == POINTER_TYPE)
13128     dest = TREE_TYPE (dest);
13129
13130   /* If source and dest are being compiled from bytecode, they may need to
13131      be loaded. */
13132   if (CLASS_P (source) && !CLASS_LOADED_P (source))
13133     {
13134       load_class (source, 1);
13135       safe_layout_class (source);
13136     }
13137   if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13138     {
13139       load_class (dest, 1);
13140       safe_layout_class (dest);
13141     }
13142
13143   /* Case where SOURCE is a class type */
13144   if (TYPE_CLASS_P (source))
13145     {
13146       if (TYPE_CLASS_P (dest))
13147         return  (source == dest
13148                  || inherits_from_p (source, dest)
13149                  || (cast && inherits_from_p (dest, source)));
13150       if (TYPE_INTERFACE_P (dest))
13151         {
13152           /* If doing a cast and SOURCE is final, the operation is
13153              always correct a compile time (because even if SOURCE
13154              does not implement DEST, a subclass of SOURCE might). */
13155           if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13156             return 1;
13157           /* Otherwise, SOURCE must implement DEST */
13158           return interface_of_p (dest, source);
13159         }
13160       /* DEST is an array, cast permitted if SOURCE is of Object type */
13161       return (cast && source == object_type_node ? 1 : 0);
13162     }
13163   if (TYPE_INTERFACE_P (source))
13164     {
13165       if (TYPE_CLASS_P (dest))
13166         {
13167           /* If not casting, DEST must be the Object type */
13168           if (!cast)
13169             return dest == object_type_node;
13170           /* We're doing a cast. The cast is always valid is class
13171              DEST is not final, otherwise, DEST must implement SOURCE */
13172           else if (!CLASS_FINAL (TYPE_NAME (dest)))
13173             return 1;
13174           else
13175             return interface_of_p (source, dest);
13176         }
13177       if (TYPE_INTERFACE_P (dest))
13178         {
13179           /* If doing a cast, then if SOURCE and DEST contain method
13180              with the same signature but different return type, then
13181              this is a (compile time) error */
13182           if (cast)
13183             {
13184               tree method_source, method_dest;
13185               tree source_type;
13186               tree source_sig;
13187               tree source_name;
13188               for (method_source = TYPE_METHODS (source); method_source;
13189                    method_source = TREE_CHAIN (method_source))
13190                 {
13191                   source_sig =
13192                     build_java_argument_signature (TREE_TYPE (method_source));
13193                   source_type = TREE_TYPE (TREE_TYPE (method_source));
13194                   source_name = DECL_NAME (method_source);
13195                   for (method_dest = TYPE_METHODS (dest);
13196                        method_dest; method_dest = TREE_CHAIN (method_dest))
13197                     if (source_sig ==
13198                         build_java_argument_signature (TREE_TYPE (method_dest))
13199                         && source_name == DECL_NAME (method_dest)
13200                         && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13201                       return 0;
13202                 }
13203               return 1;
13204             }
13205           else
13206             return source == dest || interface_of_p (dest, source);
13207         }
13208       else
13209         {
13210           /* Array */
13211           return (cast
13212                   && (DECL_NAME (TYPE_NAME (source))
13213                       == java_lang_cloneable_identifier_node
13214                       || (DECL_NAME (TYPE_NAME (source))
13215                           == java_io_serializable_identifier_node)));
13216         }
13217     }
13218   if (TYPE_ARRAY_P (source))
13219     {
13220       if (TYPE_CLASS_P (dest))
13221         return dest == object_type_node;
13222       /* Can't cast an array to an interface unless the interface is
13223          java.lang.Cloneable or java.io.Serializable.  */
13224       if (TYPE_INTERFACE_P (dest))
13225         return (DECL_NAME (TYPE_NAME (dest))
13226                 == java_lang_cloneable_identifier_node
13227                 || (DECL_NAME (TYPE_NAME (dest))
13228                     == java_io_serializable_identifier_node));
13229       else                      /* Arrays */
13230         {
13231           tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13232           tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13233
13234           /* In case of severe errors, they turn out null */
13235           if (!dest_element_type || !source_element_type)
13236             return 0;
13237           if (source_element_type == dest_element_type)
13238             return 1;
13239           return valid_ref_assignconv_cast_p (source_element_type,
13240                                               dest_element_type, cast);
13241         }
13242       return 0;
13243     }
13244   return 0;
13245 }
13246
13247 static int
13248 valid_cast_to_p (tree source, tree dest)
13249 {
13250   if (TREE_CODE (source) == POINTER_TYPE)
13251     source = TREE_TYPE (source);
13252   if (TREE_CODE (dest) == POINTER_TYPE)
13253     dest = TREE_TYPE (dest);
13254
13255   if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13256     return valid_ref_assignconv_cast_p (source, dest, 1);
13257
13258   else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13259     return 1;
13260
13261   else if (TREE_CODE (source) == BOOLEAN_TYPE
13262            && TREE_CODE (dest) == BOOLEAN_TYPE)
13263     return 1;
13264
13265   return 0;
13266 }
13267
13268 static tree
13269 do_unary_numeric_promotion (tree arg)
13270 {
13271   tree type = TREE_TYPE (arg);
13272   if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13273       || TREE_CODE (type) == CHAR_TYPE)
13274     arg = convert (int_type_node, arg);
13275   return arg;
13276 }
13277
13278 /* Return a nonzero value if SOURCE can be converted into DEST using
13279    the method invocation conversion rule (5.3).  */
13280 static int
13281 valid_method_invocation_conversion_p (tree dest, tree source)
13282 {
13283   return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13284            && valid_builtin_assignconv_identity_widening_p (dest, source))
13285           || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13286               && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13287               && valid_ref_assignconv_cast_p (source, dest, 0)));
13288 }
13289
13290 /* Build an incomplete binop expression. */
13291
13292 static tree
13293 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13294 {
13295   tree binop = build2 (op, NULL_TREE, op1, op2);
13296   TREE_SIDE_EFFECTS (binop) = 1;
13297   /* Store the location of the operator, for better error report. The
13298      string of the operator will be rebuild based on the OP value. */
13299   EXPR_WFL_LINECOL (binop) = op_location;
13300   return binop;
13301 }
13302
13303 /* Build the string of the operator retained by NODE. If NODE is part
13304    of a compound expression, add an '=' at the end of the string. This
13305    function is called when an error needs to be reported on an
13306    operator. The string is returned as a pointer to a static character
13307    buffer. */
13308
13309 static char *
13310 operator_string (tree node)
13311 {
13312 #define BUILD_OPERATOR_STRING(S)                                        \
13313   {                                                                     \
13314     sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13315     return buffer;                                                      \
13316   }
13317
13318   static char buffer [10];
13319   switch (TREE_CODE (node))
13320     {
13321     case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13322     case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13323     case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13324     case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13325     case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13326     case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13327     case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13328     case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13329     case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13330     case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13331     case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13332     case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13333     case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13334     case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13335     case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13336     case GT_EXPR: BUILD_OPERATOR_STRING (">");
13337     case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13338     case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13339     case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13340     case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13341     case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13342     case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13343     case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13344     case PREINCREMENT_EXPR:     /* Fall through */
13345     case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13346     case PREDECREMENT_EXPR:     /* Fall through */
13347     case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13348     default:
13349       internal_error ("unregistered operator %s",
13350                       tree_code_name [TREE_CODE (node)]);
13351     }
13352   return NULL;
13353 #undef BUILD_OPERATOR_STRING
13354 }
13355
13356 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13357
13358 static int
13359 java_decl_equiv (tree var_acc1, tree var_acc2)
13360 {
13361   if (JDECL_P (var_acc1))
13362     return (var_acc1 == var_acc2);
13363
13364   return (TREE_CODE (var_acc1) == COMPONENT_REF
13365           && TREE_CODE (var_acc2) == COMPONENT_REF
13366           && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13367              == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13368           && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13369 }
13370
13371 /* Return a nonzero value if CODE is one of the operators that can be
13372    used in conjunction with the `=' operator in a compound assignment.  */
13373
13374 static int
13375 binop_compound_p (enum tree_code code)
13376 {
13377   int i;
13378   for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13379     if (binop_lookup [i] == code)
13380       break;
13381
13382   return i < BINOP_COMPOUND_CANDIDATES;
13383 }
13384
13385 /* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13386
13387 static tree
13388 java_refold (tree t)
13389 {
13390   tree c, b, ns, decl;
13391
13392   if (TREE_CODE (t) != MODIFY_EXPR)
13393     return t;
13394
13395   c = TREE_OPERAND (t, 1);
13396   if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13397          && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13398          && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13399     return t;
13400
13401   /* Now the left branch of the binary operator. */
13402   b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13403   if (! (b && TREE_CODE (b) == NOP_EXPR
13404          && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13405     return t;
13406
13407   ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13408   if (! (ns && TREE_CODE (ns) == NOP_EXPR
13409          && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13410     return t;
13411
13412   decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13413   if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13414       /* It's got to be the an equivalent decl */
13415       && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13416     {
13417       /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13418       TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13419       /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13420       TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13421       /* Change the right part of the BINOP_EXPR */
13422       TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13423     }
13424
13425   return t;
13426 }
13427
13428 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13429    errors but we modify NODE so that it contains the type computed
13430    according to the expression, when it's fixed. Otherwise, we write
13431    error_mark_node as the type. It allows us to further the analysis
13432    of remaining nodes and detects more errors in certain cases.  */
13433
13434 static tree
13435 patch_binop (tree node, tree wfl_op1, tree wfl_op2)
13436 {
13437   tree op1 = TREE_OPERAND (node, 0);
13438   tree op2 = TREE_OPERAND (node, 1);
13439   tree op1_type = TREE_TYPE (op1);
13440   tree op2_type = TREE_TYPE (op2);
13441   tree prom_type = NULL_TREE, cn;
13442   enum tree_code code = TREE_CODE (node);
13443
13444   /* If 1, tell the routine that we have to return error_mark_node
13445      after checking for the initialization of the RHS */
13446   int error_found = 0;
13447
13448   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13449
13450   /* If either op<n>_type are NULL, this might be early signs of an
13451      error situation, unless it's too early to tell (in case we're
13452      handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13453      correctly so the error can be later on reported accurately. */
13454   if (! (code == PLUS_EXPR || code == NE_EXPR
13455          || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13456     {
13457       tree n;
13458       if (! op1_type)
13459         {
13460           n = java_complete_tree (op1);
13461           op1_type = TREE_TYPE (n);
13462         }
13463       if (! op2_type)
13464         {
13465           n = java_complete_tree (op2);
13466           op2_type = TREE_TYPE (n);
13467         }
13468     }
13469
13470   switch (code)
13471     {
13472     /* 15.16 Multiplicative operators */
13473     case MULT_EXPR:             /* 15.16.1 Multiplication Operator * */
13474     case RDIV_EXPR:             /* 15.16.2 Division Operator / */
13475     case TRUNC_DIV_EXPR:        /* 15.16.2 Integral type Division Operator / */
13476     case TRUNC_MOD_EXPR:        /* 15.16.3 Remainder operator % */
13477       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13478         {
13479           if (!JNUMERIC_TYPE_P (op1_type))
13480             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13481           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13482             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13483           TREE_TYPE (node) = error_mark_node;
13484           error_found = 1;
13485           break;
13486         }
13487       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13488
13489       /* Detect integral division by zero */
13490       if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13491           && TREE_CODE (prom_type) == INTEGER_TYPE
13492           && (op2 == integer_zero_node || op2 == long_zero_node ||
13493               (TREE_CODE (op2) == INTEGER_CST &&
13494                ! TREE_INT_CST_LOW (op2)  && ! TREE_INT_CST_HIGH (op2))))
13495         {
13496           parse_warning_context
13497             (wfl_operator,
13498              "Evaluating this expression will result in an arithmetic exception being thrown");
13499           TREE_CONSTANT (node) = 0;
13500           TREE_INVARIANT (node) = 0;
13501         }
13502
13503       /* Change the division operator if necessary */
13504       if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13505         TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13506
13507       /* Before divisions as is disappear, try to simplify and bail if
13508          applicable, otherwise we won't perform even simple
13509          simplifications like (1-1)/3. We can't do that with floating
13510          point number, folds can't handle them at this stage. */
13511       if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13512           && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13513         {
13514           TREE_TYPE (node) = prom_type;
13515           node = fold (node);
13516           if (TREE_CODE (node) != code)
13517             return node;
13518         }
13519
13520       if (TREE_CODE (prom_type) == INTEGER_TYPE
13521           && flag_use_divide_subroutine
13522           && ! flag_emit_class_files
13523           && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13524         return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13525
13526       /* This one is more complicated. FLOATs are processed by a
13527          function call to soft_fmod. Duplicate the value of the
13528          COMPOUND_ASSIGN_P flag. */
13529       if (code == TRUNC_MOD_EXPR)
13530         {
13531           tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13532           COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13533           return mod;
13534         }
13535       break;
13536
13537     /* 15.17 Additive Operators */
13538     case PLUS_EXPR:             /* 15.17.1 String Concatenation Operator + */
13539
13540       /* Operation is valid if either one argument is a string
13541          constant, a String object or a StringBuffer crafted for the
13542          purpose of the a previous usage of the String concatenation
13543          operator */
13544
13545       if (TREE_CODE (op1) == STRING_CST
13546           || TREE_CODE (op2) == STRING_CST
13547           || JSTRING_TYPE_P (op1_type)
13548           || JSTRING_TYPE_P (op2_type)
13549           || IS_CRAFTED_STRING_BUFFER_P (op1)
13550           || IS_CRAFTED_STRING_BUFFER_P (op2))
13551         return build_string_concatenation (op1, op2);
13552
13553     case MINUS_EXPR:            /* 15.17.2 Additive Operators (+ and -) for
13554                                    Numeric Types */
13555       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13556         {
13557           if (!JNUMERIC_TYPE_P (op1_type))
13558             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13559           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13560             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13561           TREE_TYPE (node) = error_mark_node;
13562           error_found = 1;
13563           break;
13564         }
13565       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13566       break;
13567
13568     /* 15.18 Shift Operators */
13569     case LSHIFT_EXPR:
13570     case RSHIFT_EXPR:
13571     case URSHIFT_EXPR:
13572       if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13573         {
13574           if (!JINTEGRAL_TYPE_P (op1_type))
13575             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13576           else
13577             {
13578               if (JNUMERIC_TYPE_P (op2_type))
13579                 parse_error_context (wfl_operator,
13580                                      "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13581                                      operator_string (node),
13582                                      lang_printable_name (op2_type, 0));
13583               else
13584                 parse_error_context (wfl_operator,
13585                                      "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13586                                      operator_string (node),
13587                                      lang_printable_name (op2_type, 0));
13588             }
13589           TREE_TYPE (node) = error_mark_node;
13590           error_found = 1;
13591           break;
13592         }
13593
13594       /* Unary numeric promotion (5.6.1) is performed on each operand
13595          separately */
13596       op1 = do_unary_numeric_promotion (op1);
13597       op2 = do_unary_numeric_promotion (op2);
13598
13599       /* If the right hand side is of type `long', first cast it to
13600          `int'.  */
13601       if (TREE_TYPE (op2) == long_type_node)
13602         op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13603
13604       /* The type of the shift expression is the type of the promoted
13605          type of the left-hand operand */
13606       prom_type = TREE_TYPE (op1);
13607
13608       /* Shift int only up to 0x1f and long up to 0x3f */
13609       if (prom_type == int_type_node)
13610         op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13611                             build_int_cst (NULL_TREE, 0x1f)));
13612       else
13613         op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13614                             build_int_cst (NULL_TREE, 0x3f)));
13615
13616       /* The >>> operator is a >> operating on unsigned quantities */
13617       if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13618         {
13619           tree to_return;
13620           tree utype = java_unsigned_type (prom_type);
13621           op1 = convert (utype, op1);
13622           TREE_SET_CODE (node, RSHIFT_EXPR);
13623           TREE_OPERAND (node, 0) = op1;
13624           TREE_OPERAND (node, 1) = op2;
13625           TREE_TYPE (node) = utype;
13626           to_return = convert (prom_type, node);
13627           /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13628           COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13629           TREE_SIDE_EFFECTS (to_return)
13630             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13631           return to_return;
13632         }
13633       break;
13634
13635       /* 15.19.1 Type Comparison Operator instanceof */
13636     case INSTANCEOF_EXPR:
13637
13638       TREE_TYPE (node) = boolean_type_node;
13639
13640       /* OP1_TYPE might be NULL when OP1 is a string constant.  */
13641       if ((cn = patch_string (op1)))
13642         {
13643           op1 = cn;
13644           op1_type = TREE_TYPE (op1);
13645         }
13646       if (op1_type == NULL_TREE)
13647         abort ();
13648
13649       if (!(op2_type = resolve_type_during_patch (op2)))
13650         return error_mark_node;
13651
13652       /* The first operand must be a reference type or the null type */
13653       if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13654         error_found = 1;        /* Error reported further below */
13655
13656       /* The second operand must be a reference type */
13657       if (!JREFERENCE_TYPE_P (op2_type))
13658         {
13659           SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13660           parse_error_context
13661             (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13662              lang_printable_name (op2_type, 0));
13663           error_found = 1;
13664         }
13665
13666       if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13667         {
13668           /* If the first operand is null, the result is always false */
13669           if (op1 == null_pointer_node)
13670             return boolean_false_node;
13671           else if (flag_emit_class_files)
13672             {
13673               TREE_OPERAND (node, 1) = op2_type;
13674               TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13675               return node;
13676             }
13677           /* Otherwise we have to invoke instance of to figure it out */
13678           else
13679             return build_instanceof (op1, op2_type);
13680         }
13681       /* There is no way the expression operand can be an instance of
13682          the type operand. This is a compile time error. */
13683       else
13684         {
13685           char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13686           SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13687           parse_error_context
13688             (wfl_operator, "Impossible for %qs to be instance of %qs",
13689              t1, lang_printable_name (op2_type, 0));
13690           free (t1);
13691           error_found = 1;
13692         }
13693
13694       break;
13695
13696       /* 15.21 Bitwise and Logical Operators */
13697     case BIT_AND_EXPR:
13698     case BIT_XOR_EXPR:
13699     case BIT_IOR_EXPR:
13700       if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13701         /* Binary numeric promotion is performed on both operand and the
13702            expression retain that type */
13703         prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13704
13705       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13706                && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13707         /* The type of the bitwise operator expression is BOOLEAN */
13708         prom_type = boolean_type_node;
13709       else
13710         {
13711           if (!JINTEGRAL_TYPE_P (op1_type))
13712             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13713           if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13714             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13715           TREE_TYPE (node) = error_mark_node;
13716           error_found = 1;
13717           /* Insert a break here if adding thing before the switch's
13718              break for this case */
13719         }
13720       break;
13721
13722       /* 15.22 Conditional-And Operator */
13723     case TRUTH_ANDIF_EXPR:
13724       /* 15.23 Conditional-Or Operator */
13725     case TRUTH_ORIF_EXPR:
13726       /* Operands must be of BOOLEAN type */
13727       if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13728           TREE_CODE (op2_type) != BOOLEAN_TYPE)
13729         {
13730           if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13731             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13732           if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13733             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13734           TREE_TYPE (node) = boolean_type_node;
13735           error_found = 1;
13736           break;
13737         }
13738       else if (integer_zerop (op1))
13739         {
13740           return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13741         }
13742       else if (integer_onep (op1))
13743         {
13744           return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13745         }
13746       /* The type of the conditional operators is BOOLEAN */
13747       prom_type = boolean_type_node;
13748       break;
13749
13750       /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13751     case LT_EXPR:
13752     case GT_EXPR:
13753     case LE_EXPR:
13754     case GE_EXPR:
13755       /* The type of each of the operands must be a primitive numeric
13756          type */
13757       if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13758         {
13759           if (!JNUMERIC_TYPE_P (op1_type))
13760             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13761           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13762             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13763           TREE_TYPE (node) = boolean_type_node;
13764           error_found = 1;
13765           break;
13766         }
13767       /* Binary numeric promotion is performed on the operands */
13768       binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13769       /* The type of the relation expression is always BOOLEAN */
13770       prom_type = boolean_type_node;
13771       break;
13772
13773       /* 15.20 Equality Operator */
13774     case EQ_EXPR:
13775     case NE_EXPR:
13776       /* It's time for us to patch the strings. */
13777       if ((cn = patch_string (op1)))
13778        {
13779          op1 = cn;
13780          op1_type = TREE_TYPE (op1);
13781        }
13782       if ((cn = patch_string (op2)))
13783        {
13784          op2 = cn;
13785          op2_type = TREE_TYPE (op2);
13786        }
13787
13788       /* 15.20.1 Numerical Equality Operators == and != */
13789       /* Binary numeric promotion is performed on the operands */
13790       if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13791         binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13792
13793       /* 15.20.2 Boolean Equality Operators == and != */
13794       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13795           TREE_CODE (op2_type) == BOOLEAN_TYPE)
13796         ;                       /* Nothing to do here */
13797
13798       /* 15.20.3 Reference Equality Operators == and != */
13799       /* Types have to be either references or the null type. If
13800          they're references, it must be possible to convert either
13801          type to the other by casting conversion. */
13802       else if (op1 == null_pointer_node || op2 == null_pointer_node
13803                || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13804                    && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13805                        || valid_ref_assignconv_cast_p (op2_type,
13806                                                        op1_type, 1))))
13807         ;                       /* Nothing to do here */
13808
13809       /* Else we have an error figure what can't be converted into
13810          what and report the error */
13811       else
13812         {
13813           char *t1;
13814           t1 = xstrdup (lang_printable_name (op1_type, 0));
13815           parse_error_context
13816             (wfl_operator,
13817              "Incompatible type for %qs. Can't convert %qs to %qs",
13818              operator_string (node), t1,
13819              lang_printable_name (op2_type, 0));
13820           free (t1);
13821           TREE_TYPE (node) = boolean_type_node;
13822           error_found = 1;
13823           break;
13824         }
13825       prom_type = boolean_type_node;
13826       break;
13827     default:
13828       abort ();
13829     }
13830
13831   if (error_found)
13832     return error_mark_node;
13833
13834   TREE_OPERAND (node, 0) = op1;
13835   TREE_OPERAND (node, 1) = op2;
13836   TREE_TYPE (node) = prom_type;
13837   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13838
13839   if (flag_emit_xref)
13840     return node;
13841
13842   /* fold does not respect side-effect order as required for Java but not C.
13843    * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13844    * bytecode.
13845    */
13846   if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13847       : ! TREE_SIDE_EFFECTS (node))
13848     node = fold (node);
13849   return node;
13850 }
13851
13852 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13853    zero value, the value of CSTE comes after the valude of STRING */
13854
13855 static tree
13856 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13857 {
13858   const char *old = TREE_STRING_POINTER (cste);
13859   int old_len = TREE_STRING_LENGTH (cste);
13860   int len = old_len + string_len;
13861   char *new = alloca (len+1);
13862
13863   if (after)
13864     {
13865       memcpy (new, string, string_len);
13866       memcpy (&new [string_len], old, old_len);
13867     }
13868   else
13869     {
13870       memcpy (new, old, old_len);
13871       memcpy (&new [old_len], string, string_len);
13872     }
13873   new [len] = '\0';
13874   return build_string (len, new);
13875 }
13876
13877 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13878    new STRING_CST on success, NULL_TREE on failure.  */
13879
13880 static tree
13881 merge_string_cste (tree op1, tree op2, int after)
13882 {
13883   /* Handle two string constants right away.  */
13884   if (TREE_CODE (op2) == STRING_CST)
13885     return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13886                                  TREE_STRING_LENGTH (op2), after);
13887
13888   /* Reasonable integer constant can be treated right away.  */
13889   if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13890     {
13891       static const char *const boolean_true = "true";
13892       static const char *const boolean_false = "false";
13893       static const char *const null_pointer = "null";
13894       char ch[4];
13895       const char *string;
13896
13897       if (op2 == boolean_true_node)
13898         string = boolean_true;
13899       else if (op2 == boolean_false_node)
13900         string = boolean_false;
13901       else if (op2 == null_pointer_node
13902                || (integer_zerop (op2)
13903                    && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13904         /* FIXME: null is not a compile-time constant, so it is only safe to
13905            merge if the overall expression is non-constant. However, this
13906            code always merges without checking the overall expression.  */
13907         string = null_pointer;
13908       else if (TREE_TYPE (op2) == char_type_node)
13909         {
13910           /* Convert the character into UTF-8.  */
13911           unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13912           unsigned char *p = (unsigned char *) ch;
13913           if (0x01 <= c && c <= 0x7f)
13914             *p++ = (unsigned char) c;
13915           else if (c < 0x7ff)
13916             {
13917               *p++ = (unsigned char) (c >> 6 | 0xc0);
13918               *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13919             }
13920           else
13921             {
13922               *p++ = (unsigned char) (c >> 12 | 0xe0);
13923               *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13924               *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13925             }
13926           *p = '\0';
13927
13928           string = ch;
13929         }
13930       else
13931         string = string_convert_int_cst (op2);
13932
13933       return do_merge_string_cste (op1, string, strlen (string), after);
13934     }
13935   return NULL_TREE;
13936 }
13937
13938 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13939    has to be a STRING_CST and the other part must be a STRING_CST or a
13940    INTEGRAL constant. Return a new STRING_CST if the operation
13941    succeed, NULL_TREE otherwise.
13942
13943    If the case we want to optimize for space, we might want to return
13944    NULL_TREE for each invocation of this routine. FIXME */
13945
13946 static tree
13947 string_constant_concatenation (tree op1, tree op2)
13948 {
13949   if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13950     {
13951       tree string, rest;
13952       int invert;
13953
13954       string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13955       rest   = (string == op1 ? op2 : op1);
13956       invert = (string == op1 ? 0 : 1 );
13957
13958       /* Walk REST, only if it looks reasonable */
13959       if (TREE_CODE (rest) != STRING_CST
13960           && !IS_CRAFTED_STRING_BUFFER_P (rest)
13961           && !JSTRING_TYPE_P (TREE_TYPE (rest))
13962           && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13963         {
13964           rest = java_complete_tree (rest);
13965           if (rest == error_mark_node)
13966             return error_mark_node;
13967           rest = fold (rest);
13968         }
13969       return merge_string_cste (string, rest, invert);
13970     }
13971   return NULL_TREE;
13972 }
13973
13974 /* Implement the `+' operator. Does static optimization if possible,
13975    otherwise create (if necessary) and append elements to a
13976    StringBuffer. The StringBuffer will be carried around until it is
13977    used for a function call or an assignment. Then toString() will be
13978    called on it to turn it into a String object. */
13979
13980 static tree
13981 build_string_concatenation (tree op1, tree op2)
13982 {
13983   tree result;
13984   int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13985
13986   if (flag_emit_xref)
13987     return build2 (PLUS_EXPR, string_type_node, op1, op2);
13988
13989   /* Try to do some static optimization */
13990   if ((result = string_constant_concatenation (op1, op2)))
13991     return result;
13992
13993   /* Discard empty strings on either side of the expression */
13994   if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13995     {
13996       op1 = op2;
13997       op2 = NULL_TREE;
13998     }
13999   else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14000     op2 = NULL_TREE;
14001
14002   /* If operands are string constant, turn then into object references */
14003   if (TREE_CODE (op1) == STRING_CST)
14004     op1 = patch_string_cst (op1);
14005   if (op2 && TREE_CODE (op2) == STRING_CST)
14006     op2 = patch_string_cst (op2);
14007
14008   /* If either one of the constant is null and the other non null
14009      operand is a String constant, return it. */
14010   if ((TREE_CODE (op1) == STRING_CST) && !op2)
14011     return op1;
14012
14013   /* If OP1 isn't already a StringBuffer, create and
14014      initialize a new one */
14015   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14016     {
14017       /* Two solutions here:
14018          1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14019          2) OP1 is something else, we call new StringBuffer().append(OP1).  */
14020       if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14021         op1 = BUILD_STRING_BUFFER (op1);
14022       else
14023         {
14024           tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14025           op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14026         }
14027     }
14028
14029   if (op2)
14030     {
14031       /* OP1 is no longer the last node holding a crafted StringBuffer */
14032       IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14033       /* Create a node for `{new...,xxx}.append (op2)' */
14034       if (op2)
14035         op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14036     }
14037
14038   /* Mark the last node holding a crafted StringBuffer */
14039   IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14040
14041   TREE_SIDE_EFFECTS (op1) = side_effects;
14042   return op1;
14043 }
14044
14045 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14046    StringBuffer. If no string were found to be patched, return
14047    NULL. */
14048
14049 static tree
14050 patch_string (tree node)
14051 {
14052   if (node == error_mark_node)
14053     return error_mark_node;
14054   if (TREE_CODE (node) == STRING_CST)
14055     return patch_string_cst (node);
14056   else if (IS_CRAFTED_STRING_BUFFER_P (node))
14057     {
14058       int saved = ctxp->explicit_constructor_p;
14059       tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14060       tree ret;
14061       /* Temporary disable forbid the use of `this'. */
14062       ctxp->explicit_constructor_p = 0;
14063       ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14064       /* String concatenation arguments must be evaluated in order too. */
14065       ret = force_evaluation_order (ret);
14066       /* Restore it at its previous value */
14067       ctxp->explicit_constructor_p = saved;
14068       return ret;
14069     }
14070   return NULL_TREE;
14071 }
14072
14073 /* Build the internal representation of a string constant.  */
14074
14075 static tree
14076 patch_string_cst (tree node)
14077 {
14078   int location;
14079   if (! flag_emit_class_files)
14080     {
14081       node = get_identifier (TREE_STRING_POINTER (node));
14082       location = alloc_name_constant (CONSTANT_String, node);
14083       node = build_ref_from_constant_pool (location);
14084     }
14085   TREE_CONSTANT (node) = 1;
14086   TREE_INVARIANT (node) = 1;
14087
14088   /* ??? Guessing that the class file code can't handle casts.  */
14089   if (! flag_emit_class_files)
14090     node = convert (string_ptr_type_node, node);
14091   else
14092     TREE_TYPE (node) = string_ptr_type_node;
14093
14094   return node;
14095 }
14096
14097 /* Build an incomplete unary operator expression. */
14098
14099 static tree
14100 build_unaryop (int op_token, int op_location, tree op1)
14101 {
14102   enum tree_code op;
14103   tree unaryop;
14104   switch (op_token)
14105     {
14106     case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14107     case MINUS_TK: op = NEGATE_EXPR; break;
14108     case NEG_TK: op = TRUTH_NOT_EXPR; break;
14109     case NOT_TK: op = BIT_NOT_EXPR; break;
14110     default: abort ();
14111     }
14112
14113   unaryop = build1 (op, NULL_TREE, op1);
14114   TREE_SIDE_EFFECTS (unaryop) = 1;
14115   /* Store the location of the operator, for better error report. The
14116      string of the operator will be rebuild based on the OP value. */
14117   EXPR_WFL_LINECOL (unaryop) = op_location;
14118   return unaryop;
14119 }
14120
14121 /* Special case for the ++/-- operators, since they require an extra
14122    argument to build, which is set to NULL and patched
14123    later. IS_POST_P is 1 if the operator, 0 otherwise.  */
14124
14125 static tree
14126 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14127 {
14128   static const enum tree_code lookup [2][2] =
14129     {
14130       { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14131       { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14132     };
14133   tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14134                       NULL_TREE, op1, NULL_TREE);
14135   TREE_SIDE_EFFECTS (node) = 1;
14136   /* Store the location of the operator, for better error report. The
14137      string of the operator will be rebuild based on the OP value. */
14138   EXPR_WFL_LINECOL (node) = op_location;
14139   return node;
14140 }
14141
14142 /* Build an incomplete cast operator, based on the use of the
14143    CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14144    set. java_complete_tree is trained to walk a CONVERT_EXPR even
14145    though its type is already set.  */
14146
14147 static tree
14148 build_cast (int location, tree type, tree exp)
14149 {
14150   tree node = build1 (CONVERT_EXPR, type, exp);
14151   EXPR_WFL_LINECOL (node) = location;
14152   return node;
14153 }
14154
14155 /* Build an incomplete class reference operator.  */
14156 static tree
14157 build_incomplete_class_ref (int location, tree class_name)
14158 {
14159   tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14160   tree class_decl = GET_CPC ();
14161   tree this_class = TREE_TYPE (class_decl);
14162
14163   /* Generate the synthetic static method `class$'.  (Previously we
14164      deferred this, causing different method tables to be emitted
14165      for native code and bytecode.)  */
14166   if (!TYPE_DOT_CLASS (this_class)
14167       && !JPRIMITIVE_TYPE_P (class_name)
14168       && !(TREE_CODE (class_name) == VOID_TYPE))
14169     {
14170       tree cpc_list = GET_CPC_LIST();
14171       tree cpc = cpc_list;
14172       tree target_class;
14173
14174       /* For inner classes, add a 'class$' method to their outermost
14175          context, creating it if necessary.  */
14176       
14177       while (GET_NEXT_ENCLOSING_CPC(cpc))
14178         cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14179       class_decl = TREE_VALUE (cpc);
14180
14181       target_class = TREE_TYPE (class_decl);
14182
14183       if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14184         {
14185           /* For interfaces, adding a static 'class$' method directly 
14186              is illegal.  So create an inner class to contain the new
14187              method.  Empirically this matches the behavior of javac.  */
14188           tree t, inner;
14189           /* We want the generated inner class inside the outermost class. */
14190           GET_CPC_LIST() = cpc;
14191           t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14192           inner = create_anonymous_class (t);
14193           target_class = TREE_TYPE (inner);
14194           end_class_declaration (1);
14195           GET_CPC_LIST() = cpc_list;
14196         }
14197
14198       if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14199         build_dot_class_method (target_class);
14200
14201       if (this_class != target_class)
14202         TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14203     }
14204
14205   EXPR_WFL_LINECOL (node) = location;
14206   return node;
14207 }
14208
14209 /* Complete an incomplete class reference operator.  */
14210 static tree
14211 patch_incomplete_class_ref (tree node)
14212 {
14213   tree type = TREE_OPERAND (node, 0);
14214   tree ref_type;
14215
14216   if (!(ref_type = resolve_type_during_patch (type)))
14217     return error_mark_node;
14218
14219   /* If we're not emitting class files and we know ref_type is a
14220      compiled class, build a direct reference.  */
14221   if ((! flag_emit_class_files && is_compiled_class (ref_type))
14222       || JPRIMITIVE_TYPE_P (ref_type)
14223       || TREE_CODE (ref_type) == VOID_TYPE)
14224     {
14225       tree dot = build_class_ref (ref_type);
14226       /* A class referenced by `foo.class' is initialized.  */
14227       if (!flag_emit_class_files)
14228        dot = build_class_init (ref_type, dot);
14229       return java_complete_tree (dot);
14230     }
14231
14232   /* If we're emitting class files and we have to deal with non
14233      primitive types, we invoke the synthetic static method `class$'.  */
14234   ref_type = build_dot_class_method_invocation (current_class, ref_type);
14235   return java_complete_tree (ref_type);
14236 }
14237
14238 /* 15.14 Unary operators. We return error_mark_node in case of error,
14239    but preserve the type of NODE if the type is fixed.  */
14240
14241 static tree
14242 patch_unaryop (tree node, tree wfl_op)
14243 {
14244   tree op = TREE_OPERAND (node, 0);
14245   tree op_type = TREE_TYPE (op);
14246   tree prom_type = NULL_TREE, value, decl;
14247   int outer_field_flag = 0;
14248   int code = TREE_CODE (node);
14249   int error_found = 0;
14250
14251   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14252
14253   switch (code)
14254     {
14255       /* 15.13.2 Postfix Increment Operator ++ */
14256     case POSTINCREMENT_EXPR:
14257       /* 15.13.3 Postfix Increment Operator -- */
14258     case POSTDECREMENT_EXPR:
14259       /* 15.14.1 Prefix Increment Operator ++ */
14260     case PREINCREMENT_EXPR:
14261       /* 15.14.2 Prefix Decrement Operator -- */
14262     case PREDECREMENT_EXPR:
14263       op = decl = strip_out_static_field_access_decl (op);
14264       outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14265       /* We might be trying to change an outer field accessed using
14266          access method. */
14267       if (outer_field_flag)
14268         {
14269           /* Retrieve the decl of the field we're trying to access. We
14270              do that by first retrieving the function we would call to
14271              access the field. It has been already verified that this
14272              field isn't final */
14273           if (flag_emit_class_files)
14274             decl = TREE_OPERAND (op, 0);
14275           else
14276             decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14277           decl = DECL_FUNCTION_ACCESS_DECL (decl);
14278         }
14279       /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14280       else if (!JDECL_P (decl)
14281           && TREE_CODE (decl) != COMPONENT_REF
14282           && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14283           && TREE_CODE (decl) != INDIRECT_REF
14284           && !(TREE_CODE (decl) == COMPOUND_EXPR
14285                && TREE_OPERAND (decl, 1)
14286                && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14287         {
14288           TREE_TYPE (node) = error_mark_node;
14289           error_found = 1;
14290         }
14291
14292       /* From now on, we know that op if a variable and that it has a
14293          valid wfl. We use wfl_op to locate errors related to the
14294          ++/-- operand. */
14295       if (!JNUMERIC_TYPE_P (op_type))
14296         {
14297           parse_error_context
14298             (wfl_op, "Invalid argument type %qs to %qs",
14299              lang_printable_name (op_type, 0), operator_string (node));
14300           TREE_TYPE (node) = error_mark_node;
14301           error_found = 1;
14302         }
14303       else
14304         {
14305           /* Before the addition, binary numeric promotion is performed on
14306              both operands, if really necessary */
14307           if (JINTEGRAL_TYPE_P (op_type))
14308             {
14309               value = build_int_cst (op_type, 1);
14310               TREE_TYPE (node) = op_type;
14311             }
14312           else
14313             {
14314               value = build_int_cst (NULL_TREE, 1);
14315               TREE_TYPE (node) =
14316                 binary_numeric_promotion (op_type,
14317                                           TREE_TYPE (value), &op, &value);
14318             }
14319
14320           /* We remember we might be accessing an outer field */
14321           if (outer_field_flag)
14322             {
14323               /* We re-generate an access to the field */
14324               value = build2 (PLUS_EXPR, TREE_TYPE (op),
14325                               build_outer_field_access (wfl_op, decl), value);
14326
14327               /* And we patch the original access$() into a write
14328                  with plus_op as a rhs */
14329               return outer_field_access_fix (node, op, value);
14330             }
14331
14332           /* And write back into the node. */
14333           TREE_OPERAND (node, 0) = op;
14334           TREE_OPERAND (node, 1) = value;
14335           /* Convert the overall back into its original type, if
14336              necessary, and return */
14337           if (JINTEGRAL_TYPE_P (op_type))
14338             return fold (node);
14339           else
14340             return fold (convert (op_type, node));
14341         }
14342       break;
14343
14344       /* 15.14.3 Unary Plus Operator + */
14345     case UNARY_PLUS_EXPR:
14346       /* 15.14.4 Unary Minus Operator - */
14347     case NEGATE_EXPR:
14348       if (!JNUMERIC_TYPE_P (op_type))
14349         {
14350           ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14351           TREE_TYPE (node) = error_mark_node;
14352           error_found = 1;
14353         }
14354       /* Unary numeric promotion is performed on operand */
14355       else
14356         {
14357           op = do_unary_numeric_promotion (op);
14358           prom_type = TREE_TYPE (op);
14359           if (code == UNARY_PLUS_EXPR)
14360             return fold (op);
14361         }
14362       break;
14363
14364       /* 15.14.5 Bitwise Complement Operator ~ */
14365     case BIT_NOT_EXPR:
14366       if (!JINTEGRAL_TYPE_P (op_type))
14367         {
14368           ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14369           TREE_TYPE (node) = error_mark_node;
14370           error_found = 1;
14371         }
14372       else
14373         {
14374           op = do_unary_numeric_promotion (op);
14375           prom_type = TREE_TYPE (op);
14376         }
14377       break;
14378
14379       /* 15.14.6 Logical Complement Operator ! */
14380     case TRUTH_NOT_EXPR:
14381       if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14382         {
14383           ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14384           /* But the type is known. We will report an error if further
14385              attempt of a assignment is made with this rhs */
14386           TREE_TYPE (node) = boolean_type_node;
14387           error_found = 1;
14388         }
14389       else
14390         prom_type = boolean_type_node;
14391       break;
14392
14393       /* 15.15 Cast Expression */
14394     case CONVERT_EXPR:
14395       value = patch_cast (node, wfl_operator);
14396       if (value == error_mark_node)
14397         {
14398           /* If this cast is part of an assignment, we tell the code
14399              that deals with it not to complain about a mismatch,
14400              because things have been cast, anyways */
14401           TREE_TYPE (node) = error_mark_node;
14402           error_found = 1;
14403         }
14404       else
14405         {
14406           value = fold (value);
14407           return value;
14408         }
14409       break;
14410     }
14411
14412   if (error_found)
14413     return error_mark_node;
14414
14415   /* There are cases where node has been replaced by something else
14416      and we don't end up returning here: UNARY_PLUS_EXPR,
14417      CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14418   TREE_OPERAND (node, 0) = fold (op);
14419   TREE_TYPE (node) = prom_type;
14420   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14421   return fold (node);
14422 }
14423
14424 /* Generic type resolution that sometimes takes place during node
14425    patching. Returned the resolved type or generate an error
14426    message. Return the resolved type or NULL_TREE.  */
14427
14428 static tree
14429 resolve_type_during_patch (tree type)
14430 {
14431   if (unresolved_type_p (type, NULL))
14432     {
14433       tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14434       if (!type_decl)
14435         {
14436           parse_error_context (type,
14437                                "Class %qs not found in type declaration",
14438                                IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14439           return NULL_TREE;
14440         }
14441
14442       check_deprecation (type, type_decl);
14443
14444       return TREE_TYPE (type_decl);
14445     }
14446   return type;
14447 }
14448
14449 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14450    found. Otherwise NODE or something meant to replace it is returned.  */
14451
14452 static tree
14453 patch_cast (tree node, tree wfl_op)
14454 {
14455   tree op = TREE_OPERAND (node, 0);
14456   tree cast_type = TREE_TYPE (node);
14457   tree patched, op_type;
14458   char *t1;
14459
14460   /* Some string patching might be necessary at this stage */
14461   if ((patched = patch_string (op)))
14462     TREE_OPERAND (node, 0) = op = patched;
14463   op_type = TREE_TYPE (op);
14464
14465   /* First resolve OP_TYPE if unresolved */
14466   if (!(cast_type = resolve_type_during_patch (cast_type)))
14467     return error_mark_node;
14468
14469   /* Check on cast that are proven correct at compile time */
14470   if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14471     {
14472       /* Same type */
14473       if (cast_type == op_type)
14474         return node;
14475
14476       /* A narrowing conversion from a floating-point number to an
14477          integral type requires special handling (5.1.3).  */
14478       if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14479         if (cast_type != long_type_node)
14480           op = convert (integer_type_node, op);
14481
14482       /* Try widening/narrowing conversion.  Potentially, things need
14483          to be worked out in gcc so we implement the extreme cases
14484          correctly.  fold_convert() needs to be fixed.  */
14485       return convert (cast_type, op);
14486     }
14487
14488   /* It's also valid to cast a boolean into a boolean */
14489   if (op_type == boolean_type_node && cast_type == boolean_type_node)
14490     return node;
14491
14492   /* null can be casted to references */
14493   if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14494     return build_null_of_type (cast_type);
14495
14496   /* The remaining legal casts involve conversion between reference
14497      types. Check for their compile time correctness. */
14498   if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14499       && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14500     {
14501       TREE_TYPE (node) = promote_type (cast_type);
14502       /* Now, the case can be determined correct at compile time if
14503          OP_TYPE can be converted into CAST_TYPE by assignment
14504          conversion (5.2) */
14505
14506       if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14507         {
14508           TREE_SET_CODE (node, NOP_EXPR);
14509           return node;
14510         }
14511
14512       if (flag_emit_class_files)
14513         {
14514           TREE_SET_CODE (node, CONVERT_EXPR);
14515           return node;
14516         }
14517
14518       /* The cast requires a run-time check */
14519       return build3 (CALL_EXPR, promote_type (cast_type),
14520                      build_address_of (soft_checkcast_node),
14521                      tree_cons (NULL_TREE, build_class_ref (cast_type),
14522                                 build_tree_list (NULL_TREE, op)),
14523                      NULL_TREE);
14524     }
14525
14526   /* Any other casts are proven incorrect at compile time */
14527   t1 = xstrdup (lang_printable_name (op_type, 0));
14528   parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14529                        t1, lang_printable_name (cast_type, 0));
14530   free (t1);
14531   return error_mark_node;
14532 }
14533
14534 /* Build a null constant and give it the type TYPE.  */
14535
14536 static tree
14537 build_null_of_type (tree type)
14538 {
14539   tree node = build_int_cst (promote_type (type), 0);
14540   return node;
14541 }
14542
14543 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14544    a list of indices. */
14545 static tree
14546 build_array_ref (int location, tree array, tree index)
14547 {
14548   tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14549                       NULL_TREE, NULL_TREE);
14550   EXPR_WFL_LINECOL (node) = location;
14551   return node;
14552 }
14553
14554 /* 15.12 Array Access Expression */
14555
14556 static tree
14557 patch_array_ref (tree node)
14558 {
14559   tree array = TREE_OPERAND (node, 0);
14560   tree array_type  = TREE_TYPE (array);
14561   tree index = TREE_OPERAND (node, 1);
14562   tree index_type = TREE_TYPE (index);
14563   int error_found = 0;
14564
14565   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14566
14567   if (TREE_CODE (array_type) == POINTER_TYPE)
14568     array_type = TREE_TYPE (array_type);
14569
14570   /* The array reference must be an array */
14571   if (!TYPE_ARRAY_P (array_type))
14572     {
14573       parse_error_context
14574         (wfl_operator,
14575          "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14576          lang_printable_name (array_type, 0));
14577       TREE_TYPE (node) = error_mark_node;
14578       error_found = 1;
14579     }
14580
14581   /* The array index undergoes unary numeric promotion. The promoted
14582      type must be int */
14583   index = do_unary_numeric_promotion (index);
14584   if (TREE_TYPE (index) != int_type_node)
14585     {
14586       if (valid_cast_to_p (index_type, int_type_node))
14587         parse_error_context (wfl_operator,
14588    "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14589                              lang_printable_name (index_type, 0));
14590       else
14591         parse_error_context (wfl_operator,
14592           "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14593                              lang_printable_name (index_type, 0));
14594       TREE_TYPE (node) = error_mark_node;
14595       error_found = 1;
14596     }
14597
14598   if (error_found)
14599     return error_mark_node;
14600
14601   array_type = TYPE_ARRAY_ELEMENT (array_type);
14602
14603   if (flag_emit_class_files || flag_emit_xref)
14604     {
14605       TREE_OPERAND (node, 0) = array;
14606       TREE_OPERAND (node, 1) = index;
14607     }
14608   else
14609     node = build_java_arrayaccess (array, array_type, index);
14610   TREE_TYPE (node) = array_type;
14611   return node;
14612 }
14613
14614 /* 15.9 Array Creation Expressions */
14615
14616 static tree
14617 build_newarray_node (tree type, tree dims, int extra_dims)
14618 {
14619   tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14620                       nreverse (dims),
14621                       build_int_cst (NULL_TREE, extra_dims));
14622   return node;
14623 }
14624
14625 static tree
14626 patch_newarray (tree node)
14627 {
14628   tree type = TREE_OPERAND (node, 0);
14629   tree dims = TREE_OPERAND (node, 1);
14630   tree cdim, array_type;
14631   int error_found = 0;
14632   int ndims = 0;
14633   int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14634
14635   /* Dimension types are verified. It's better for the types to be
14636      verified in order. */
14637   for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14638     {
14639       int dim_error = 0;
14640       tree dim = TREE_VALUE (cdim);
14641
14642       /* Dim might have been saved during its evaluation */
14643       dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14644
14645       /* The type of each specified dimension must be an integral type. */
14646       if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14647         dim_error = 1;
14648
14649       /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14650          promoted type must be int. */
14651       else
14652         {
14653           dim = do_unary_numeric_promotion (dim);
14654           if (TREE_TYPE (dim) != int_type_node)
14655             dim_error = 1;
14656         }
14657
14658       /* Report errors on types here */
14659       if (dim_error)
14660         {
14661           parse_error_context
14662             (TREE_PURPOSE (cdim),
14663              "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14664              (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14665               "Explicit cast needed to" : "Can't"),
14666              lang_printable_name (TREE_TYPE (dim), 0));
14667           error_found = 1;
14668         }
14669
14670       TREE_PURPOSE (cdim) = NULL_TREE;
14671     }
14672
14673   /* Resolve array base type if unresolved */
14674   if (!(type = resolve_type_during_patch (type)))
14675     error_found = 1;
14676
14677   if (error_found)
14678     {
14679       /* We don't want further evaluation of this bogus array creation
14680          operation */
14681       TREE_TYPE (node) = error_mark_node;
14682       return error_mark_node;
14683     }
14684
14685   /* Set array_type to the actual (promoted) array type of the result. */
14686   if (TREE_CODE (type) == RECORD_TYPE)
14687     type = build_pointer_type (type);
14688   while (--xdims >= 0)
14689     {
14690       type = promote_type (build_java_array_type (type, -1));
14691     }
14692   dims = nreverse (dims);
14693   array_type = type;
14694   for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14695     {
14696       type = array_type;
14697       array_type
14698         = build_java_array_type (type,
14699                                  TREE_CODE (cdim) == INTEGER_CST
14700                                  ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14701                                  : -1);
14702       array_type = promote_type (array_type);
14703     }
14704   dims = nreverse (dims);
14705
14706   /* The node is transformed into a function call. Things are done
14707      differently according to the number of dimensions. If the number
14708      of dimension is equal to 1, then the nature of the base type
14709      (primitive or not) matters. */
14710   if (ndims == 1)
14711     return build_new_array (type, TREE_VALUE (dims));
14712
14713   /* Can't reuse what's already written in expr.c because it uses the
14714      JVM stack representation. Provide a build_multianewarray. FIXME */
14715   return build3 (CALL_EXPR, array_type,
14716                  build_address_of (soft_multianewarray_node),
14717                  tree_cons (NULL_TREE,
14718                             build_class_ref (TREE_TYPE (array_type)),
14719                             tree_cons (NULL_TREE,
14720                                        build_int_cst (NULL_TREE, ndims),
14721                                        dims)),
14722                  NULL_TREE);
14723 }
14724
14725 /* 10.6 Array initializer.  */
14726
14727 /* Build a wfl for array element that don't have one, so we can
14728    pin-point errors.  */
14729
14730 static tree
14731 maybe_build_array_element_wfl (tree node)
14732 {
14733   if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14734     {
14735       /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14736       return build_expr_wfl (NULL_TREE,
14737 #ifdef USE_MAPPED_LOCATION
14738                              input_location
14739 #else
14740                              ctxp->filename,
14741                              ctxp->lexer->token_start.line,
14742                              ctxp->lexer->token_start.col
14743 #endif
14744                              );
14745     }
14746   else
14747     return NULL_TREE;
14748 }
14749
14750 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14751    identification of initialized arrays easier to detect during walk
14752    and expansion.  */
14753
14754 static tree
14755 build_new_array_init (int location, tree values)
14756 {
14757   tree constructor = build_constructor (NULL_TREE, values);
14758   tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14759   EXPR_WFL_LINECOL (to_return) = location;
14760   return to_return;
14761 }
14762
14763 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14764    occurred.  Otherwise return NODE after having set its type
14765    appropriately.  */
14766
14767 static tree
14768 patch_new_array_init (tree type, tree node)
14769 {
14770   int error_seen = 0;
14771   tree current, element_type;
14772   HOST_WIDE_INT length;
14773   int all_constant = 1;
14774   tree init = TREE_OPERAND (node, 0);
14775
14776   if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14777     {
14778       parse_error_context (node,
14779                            "Invalid array initializer for non-array type %qs",
14780                            lang_printable_name (type, 1));
14781       return error_mark_node;
14782     }
14783   type = TREE_TYPE (type);
14784   element_type = TYPE_ARRAY_ELEMENT (type);
14785
14786   CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14787
14788   for (length = 0, current = CONSTRUCTOR_ELTS (init);
14789        current;  length++, current = TREE_CHAIN (current))
14790     {
14791       tree elt = TREE_VALUE (current);
14792       if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14793         {
14794           error_seen |= array_constructor_check_entry (element_type, current);
14795           elt = TREE_VALUE (current);
14796           /* When compiling to native code, STRING_CST is converted to
14797              INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14798           if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14799             all_constant = 0;
14800         }
14801       else
14802         {
14803           TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14804           TREE_PURPOSE (current) = NULL_TREE;
14805           all_constant = 0;
14806         }
14807       if (elt && TREE_CODE (elt) == TREE_LIST
14808           && TREE_VALUE (elt) == error_mark_node)
14809         error_seen = 1;
14810     }
14811
14812   if (error_seen)
14813     return error_mark_node;
14814
14815   /* Create a new type. We can't reuse the one we have here by
14816      patching its dimension because it originally is of dimension -1
14817      hence reused by gcc. This would prevent triangular arrays. */
14818   type = build_java_array_type (element_type, length);
14819   TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14820   TREE_TYPE (node) = promote_type (type);
14821   TREE_CONSTANT (init) = all_constant;
14822   TREE_INVARIANT (init) = all_constant;
14823   TREE_CONSTANT (node) = all_constant;
14824   TREE_INVARIANT (node) = all_constant;
14825   return node;
14826 }
14827
14828 /* Verify that one entry of the initializer element list can be
14829    assigned to the array base type. Report 1 if an error occurred, 0
14830    otherwise.  */
14831
14832 static int
14833 array_constructor_check_entry (tree type, tree entry)
14834 {
14835   char *array_type_string = NULL;       /* For error reports */
14836   tree value, type_value, new_value, wfl_value, patched;
14837   int error_seen = 0;
14838
14839   new_value = NULL_TREE;
14840   wfl_value = TREE_VALUE (entry);
14841
14842   value = java_complete_tree (TREE_VALUE (entry));
14843   /* patch_string return error_mark_node if arg is error_mark_node */
14844   if ((patched = patch_string (value)))
14845     value = patched;
14846   if (value == error_mark_node)
14847     return 1;
14848
14849   type_value = TREE_TYPE (value);
14850
14851   /* At anytime, try_builtin_assignconv can report a warning on
14852      constant overflow during narrowing. */
14853   SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14854   new_value = try_builtin_assignconv (wfl_operator, type, value);
14855   if (!new_value && (new_value = try_reference_assignconv (type, value)))
14856     type_value = promote_type (type);
14857
14858   /* Check and report errors */
14859   if (!new_value)
14860     {
14861       const char *const msg = (!valid_cast_to_p (type_value, type) ?
14862                    "Can't" : "Explicit cast needed to");
14863       if (!array_type_string)
14864         array_type_string = xstrdup (lang_printable_name (type, 1));
14865       parse_error_context
14866         (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14867          msg, lang_printable_name (type_value, 1), array_type_string);
14868       error_seen = 1;
14869     }
14870
14871   if (new_value)
14872     TREE_VALUE (entry) = new_value;
14873
14874   if (array_type_string)
14875     free (array_type_string);
14876
14877   TREE_PURPOSE (entry) = NULL_TREE;
14878   return error_seen;
14879 }
14880
14881 static tree
14882 build_this (int location)
14883 {
14884   tree node = build_wfl_node (this_identifier_node);
14885   TREE_SET_CODE (node, THIS_EXPR);
14886   EXPR_WFL_LINECOL (node) = location;
14887   return node;
14888 }
14889
14890 /* 14.15 The return statement. It builds a modify expression that
14891    assigns the returned value to the RESULT_DECL that hold the value
14892    to be returned. */
14893
14894 static tree
14895 build_return (int location, tree op)
14896 {
14897   tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14898   EXPR_WFL_LINECOL (node) = location;
14899   node = build_debugable_stmt (location, node);
14900   return node;
14901 }
14902
14903 static tree
14904 patch_return (tree node)
14905 {
14906   tree return_exp = TREE_OPERAND (node, 0);
14907   tree meth = current_function_decl;
14908   tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14909   int error_found = 0;
14910
14911   TREE_TYPE (node) = error_mark_node;
14912   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14913
14914   /* It's invalid to have a return value within a function that is
14915      declared with the keyword void or that is a constructor */
14916   if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14917     error_found = 1;
14918
14919   /* It's invalid to use a return statement in a static block */
14920   if (DECL_CLINIT_P (current_function_decl))
14921     error_found = 1;
14922
14923   /* It's invalid to have a no return value within a function that
14924      isn't declared with the keyword `void' */
14925   if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14926     error_found = 2;
14927
14928   if (DECL_INSTINIT_P (current_function_decl))
14929     error_found = 1;
14930
14931   if (error_found)
14932     {
14933       if (DECL_INSTINIT_P (current_function_decl))
14934         parse_error_context (wfl_operator,
14935                              "%<return%> inside instance initializer");
14936
14937       else if (DECL_CLINIT_P (current_function_decl))
14938         parse_error_context (wfl_operator,
14939                              "%<return%> inside static initializer");
14940
14941       else if (!DECL_CONSTRUCTOR_P (meth))
14942         {
14943           char *t = xstrdup (lang_printable_name (mtype, 0));
14944           parse_error_context (wfl_operator,
14945                                "%<return%> with%s value from %<%s %s%>",
14946                                (error_found == 1 ? "" : "out"),
14947                                t, lang_printable_name (meth, 2));
14948           free (t);
14949         }
14950       else
14951         parse_error_context (wfl_operator,
14952                              "%<return%> with value from constructor %qs",
14953                              lang_printable_name (meth, 2));
14954       return error_mark_node;
14955     }
14956
14957   /* If we have a return_exp, build a modify expression and expand
14958      it. Note: at that point, the assignment is declared valid, but we
14959      may want to carry some more hacks */
14960   if (return_exp)
14961     {
14962       tree exp = java_complete_tree (return_exp);
14963       tree modify, patched;
14964
14965       if ((patched = patch_string (exp)))
14966         exp = patched;
14967
14968       modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14969       EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14970       modify = java_complete_tree (modify);
14971
14972       if (modify != error_mark_node)
14973         {
14974           TREE_SIDE_EFFECTS (modify) = 1;
14975           TREE_OPERAND (node, 0) = modify;
14976         }
14977       else
14978         return error_mark_node;
14979     }
14980   TREE_TYPE (node) = void_type_node;
14981   TREE_SIDE_EFFECTS (node) = 1;
14982   return node;
14983 }
14984
14985 /* 14.8 The if Statement */
14986
14987 static tree
14988 build_if_else_statement (int location, tree expression, tree if_body,
14989                          tree else_body)
14990 {
14991   tree node;
14992   if (!else_body)
14993     else_body = build_java_empty_stmt ();
14994   node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14995   EXPR_WFL_LINECOL (node) = location;
14996   node = build_debugable_stmt (location, node);
14997   return node;
14998 }
14999
15000 static tree
15001 patch_if_else_statement (tree node)
15002 {
15003   tree expression = TREE_OPERAND (node, 0);
15004   int can_complete_normally
15005     = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15006        | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15007
15008   TREE_TYPE (node) = error_mark_node;
15009   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15010
15011   /* The type of expression must be boolean */
15012   if (TREE_TYPE (expression) != boolean_type_node
15013       && TREE_TYPE (expression) != promoted_boolean_type_node)
15014     {
15015       parse_error_context
15016         (wfl_operator,
15017          "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
15018          lang_printable_name (TREE_TYPE (expression), 0));
15019       return error_mark_node;
15020     }
15021
15022   TREE_TYPE (node) = void_type_node;
15023   TREE_SIDE_EFFECTS (node) = 1;
15024   CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15025   return node;
15026 }
15027
15028 /* 14.6 Labeled Statements */
15029
15030 /* Action taken when a labeled statement is parsed. a new
15031    LABELED_BLOCK_EXPR is created. No statement is attached to the
15032    label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
15033
15034 static tree
15035 build_labeled_block (int location, tree label)
15036 {
15037   tree label_name ;
15038   tree label_decl, node;
15039   if (label == NULL_TREE || label == continue_identifier_node)
15040     label_name = label;
15041   else
15042     {
15043       label_name = merge_qualified_name (label_id, label);
15044       /* Issue an error if we try to reuse a label that was previously
15045          declared */
15046       if (IDENTIFIER_LOCAL_VALUE (label_name))
15047         {
15048           EXPR_WFL_LINECOL (wfl_operator) = location;
15049           parse_error_context (wfl_operator,
15050             "Declaration of %qs shadows a previous label declaration",
15051                                IDENTIFIER_POINTER (label));
15052           EXPR_WFL_LINECOL (wfl_operator) =
15053             EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15054           parse_error_context (wfl_operator,
15055             "This is the location of the previous declaration of label %qs",
15056                                IDENTIFIER_POINTER (label));
15057           java_error_count--;
15058         }
15059     }
15060
15061   label_decl = create_label_decl (label_name);
15062   node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15063   EXPR_WFL_LINECOL (node) = location;
15064   TREE_SIDE_EFFECTS (node) = 1;
15065   return node;
15066 }
15067
15068 /* A labeled statement LBE is attached a statement.  */
15069
15070 static tree
15071 finish_labeled_statement (tree lbe, /* Labeled block expr */
15072                           tree statement)
15073 {
15074   /* In anyways, tie the loop to its statement */
15075   LABELED_BLOCK_BODY (lbe) = statement;
15076   pop_labeled_block ();
15077   POP_LABELED_BLOCK ();
15078   return lbe;
15079 }
15080
15081 /* 14.10, 14.11, 14.12 Loop Statements */
15082
15083 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15084    list. */
15085
15086 static tree
15087 build_new_loop (tree loop_body)
15088 {
15089   tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15090   TREE_SIDE_EFFECTS (loop) = 1;
15091   PUSH_LOOP (loop);
15092   return loop;
15093 }
15094
15095 /* Create a loop body according to the following structure:
15096      COMPOUND_EXPR
15097        COMPOUND_EXPR            (loop main body)
15098          EXIT_EXPR              (this order is for while/for loops.
15099          LABELED_BLOCK_EXPR      the order is reversed for do loops)
15100            LABEL_DECL           (a continue occurring here branches at the
15101            BODY                  end of this labeled block)
15102        INCREMENT                (if any)
15103
15104   REVERSED, if nonzero, tells that the loop condition expr comes
15105   after the body, like in the do-while loop.
15106
15107   To obtain a loop, the loop body structure described above is
15108   encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15109
15110    LABELED_BLOCK_EXPR
15111      LABEL_DECL                   (use this label to exit the loop)
15112      LOOP_EXPR
15113        <structure described above> */
15114
15115 static tree
15116 build_loop_body (int location, tree condition, int reversed)
15117 {
15118   tree first, second, body;
15119
15120   condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15121   EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15122   condition = build_debugable_stmt (location, condition);
15123   TREE_SIDE_EFFECTS (condition) = 1;
15124
15125   body = build_labeled_block (0, continue_identifier_node);
15126   first = (reversed ? body : condition);
15127   second = (reversed ? condition : body);
15128   return build2 (COMPOUND_EXPR, NULL_TREE,
15129                  build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15130                  build_java_empty_stmt ());
15131 }
15132
15133 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15134    their order) on the current loop. Unlink the current loop from the
15135    loop list.  */
15136
15137 static tree
15138 finish_loop_body (int location, tree condition, tree body, int reversed)
15139 {
15140   tree to_return = ctxp->current_loop;
15141   tree loop_body = LOOP_EXPR_BODY (to_return);
15142   if (condition)
15143     {
15144       tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15145       /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15146          The real EXIT_EXPR is one operand further. */
15147       EXPR_WFL_LINECOL (cnode) = location;
15148       if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15149         {
15150           cnode = EXPR_WFL_NODE (cnode);
15151           /* This one is for accurate error reports */
15152           EXPR_WFL_LINECOL (cnode) = location;
15153         }
15154       TREE_OPERAND (cnode, 0) = condition;
15155     }
15156   LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15157   POP_LOOP ();
15158   return to_return;
15159 }
15160
15161 /* Tailored version of finish_loop_body for FOR loops, when FOR
15162    loops feature the condition part */
15163
15164 static tree
15165 finish_for_loop (int location, tree condition, tree update, tree body)
15166 {
15167   /* Put the condition and the loop body in place */
15168   tree loop = finish_loop_body (location, condition, body, 0);
15169   /* LOOP is the current loop which has been now popped of the loop
15170      stack.  Mark the update block as reachable and install it.  We do
15171      this because the (current interpretation of the) JLS requires
15172      that the update expression be considered reachable even if the
15173      for loop's body doesn't complete normally.  */
15174   if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15175     {
15176       tree up2 = update;
15177       if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15178         up2 = EXPR_WFL_NODE (up2);
15179       /* It is possible for the update expression to be an
15180          EXPR_WFL_NODE wrapping nothing.  */
15181       if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15182         {
15183           /* Try to detect constraint violations.  These would be
15184              programming errors somewhere.  */
15185           if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15186             abort ();
15187           SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15188         }
15189     }
15190   LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15191   return loop;
15192 }
15193
15194 /* Try to find the loop a block might be related to. This comprises
15195    the case where the LOOP_EXPR is found as the second operand of a
15196    COMPOUND_EXPR, because the loop happens to have an initialization
15197    part, then expressed as the first operand of the COMPOUND_EXPR. If
15198    the search finds something, 1 is returned. Otherwise, 0 is
15199    returned. The search is assumed to start from a
15200    LABELED_BLOCK_EXPR's block.  */
15201
15202 static tree
15203 search_loop (tree statement)
15204 {
15205   if (TREE_CODE (statement) == LOOP_EXPR)
15206     return statement;
15207
15208   if (TREE_CODE (statement) == BLOCK)
15209     statement = BLOCK_SUBBLOCKS (statement);
15210   else
15211     return NULL_TREE;
15212
15213   if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15214     while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15215       statement = TREE_OPERAND (statement, 1);
15216
15217   return (TREE_CODE (statement) == LOOP_EXPR
15218           && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15219 }
15220
15221 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15222    returned otherwise.  */
15223
15224 static int
15225 labeled_block_contains_loop_p (tree block, tree loop)
15226 {
15227   if (!block)
15228     return 0;
15229
15230   if (LABELED_BLOCK_BODY (block) == loop)
15231     return 1;
15232
15233   if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15234     return 1;
15235
15236   return 0;
15237 }
15238
15239 /* If the loop isn't surrounded by a labeled statement, create one and
15240    insert LOOP as its body.  */
15241
15242 static tree
15243 patch_loop_statement (tree loop)
15244 {
15245   tree loop_label;
15246
15247   TREE_TYPE (loop) = void_type_node;
15248   if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15249     return loop;
15250
15251   loop_label = build_labeled_block (0, NULL_TREE);
15252   /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15253      that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15254   LABELED_BLOCK_BODY (loop_label) = loop;
15255   PUSH_LABELED_BLOCK (loop_label);
15256   return loop_label;
15257 }
15258
15259 /* 14.13, 14.14: break and continue Statements */
15260
15261 /* Build a break or a continue statement. a null NAME indicates an
15262    unlabeled break/continue statement.  */
15263
15264 static tree
15265 build_bc_statement (int location, int is_break, tree name)
15266 {
15267   tree break_continue, label_block_expr = NULL_TREE;
15268
15269   if (name)
15270     {
15271       if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15272             (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15273         /* Null means that we don't have a target for this named
15274            break/continue. In this case, we make the target to be the
15275            label name, so that the error can be reported accurately in
15276            patch_bc_statement. */
15277         label_block_expr = EXPR_WFL_NODE (name);
15278     }
15279   /* Unlabeled break/continue will be handled during the
15280      break/continue patch operation */
15281   break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15282
15283   IS_BREAK_STMT_P (break_continue) = is_break;
15284   TREE_SIDE_EFFECTS (break_continue) = 1;
15285   EXPR_WFL_LINECOL (break_continue) = location;
15286   break_continue = build_debugable_stmt (location, break_continue);
15287   return break_continue;
15288 }
15289
15290 /* Verification of a break/continue statement. */
15291
15292 static tree
15293 patch_bc_statement (tree node)
15294 {
15295   tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15296   tree labeled_block = ctxp->current_labeled_block;
15297   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15298
15299   /* Having an identifier here means that the target is unknown. */
15300   if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15301     {
15302       parse_error_context (wfl_operator, "No label definition found for %qs",
15303                            IDENTIFIER_POINTER (bc_label));
15304       return error_mark_node;
15305     }
15306   if (! IS_BREAK_STMT_P (node))
15307     {
15308       /* It's a continue statement. */
15309       for (;; labeled_block = TREE_CHAIN (labeled_block))
15310         {
15311           if (labeled_block == NULL_TREE)
15312             {
15313               if (bc_label == NULL_TREE)
15314                 parse_error_context (wfl_operator,
15315                                      "%<continue%> must be in loop");
15316               else
15317                 parse_error_context
15318                   (wfl_operator, "continue label %qs does not name a loop",
15319                    IDENTIFIER_POINTER (bc_label));
15320               return error_mark_node;
15321             }
15322           if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15323                == continue_identifier_node)
15324               && (bc_label == NULL_TREE
15325                   || TREE_CHAIN (labeled_block) == bc_label))
15326             {
15327               bc_label = labeled_block;
15328               break;
15329             }
15330         }
15331     }
15332   else if (!bc_label)
15333     {
15334       for (;; labeled_block = TREE_CHAIN (labeled_block))
15335         {
15336           if (labeled_block == NULL_TREE)
15337             {
15338               parse_error_context (wfl_operator,
15339                                      "%<break%> must be in loop or switch");
15340               return error_mark_node;
15341             }
15342           target_stmt = LABELED_BLOCK_BODY (labeled_block);
15343           if (TREE_CODE (target_stmt) == SWITCH_EXPR
15344               || search_loop (target_stmt))
15345             {
15346               bc_label = labeled_block;
15347               break;
15348             }
15349         }
15350     }
15351
15352   EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15353   CAN_COMPLETE_NORMALLY (bc_label) = 1;
15354
15355   /* Our break/continue don't return values. */
15356   TREE_TYPE (node) = void_type_node;
15357   /* Encapsulate the break within a compound statement so that it's
15358      expanded all the times by expand_expr (and not clobbered
15359      sometimes, like after a if statement) */
15360   node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15361   TREE_SIDE_EFFECTS (node) = 1;
15362   return node;
15363 }
15364
15365 /* Process the exit expression belonging to a loop. Its type must be
15366    boolean.  */
15367
15368 static tree
15369 patch_exit_expr (tree node)
15370 {
15371   tree expression = TREE_OPERAND (node, 0);
15372   TREE_TYPE (node) = error_mark_node;
15373   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15374
15375   /* The type of expression must be boolean */
15376   if (TREE_TYPE (expression) != boolean_type_node)
15377     {
15378       parse_error_context
15379         (wfl_operator,
15380     "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15381          lang_printable_name (TREE_TYPE (expression), 0));
15382       return error_mark_node;
15383     }
15384   /* Now we know things are allright, invert the condition, fold and
15385      return */
15386   TREE_OPERAND (node, 0) =
15387     fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15388
15389   if (! integer_zerop (TREE_OPERAND (node, 0))
15390       && ctxp->current_loop != NULL_TREE
15391       && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15392     CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15393   if (! integer_onep (TREE_OPERAND (node, 0)))
15394     CAN_COMPLETE_NORMALLY (node) = 1;
15395
15396
15397   TREE_TYPE (node) = void_type_node;
15398   return node;
15399 }
15400
15401 /* 14.9 Switch statement */
15402
15403 static tree
15404 patch_switch_statement (tree node)
15405 {
15406   tree se = TREE_OPERAND (node, 0), se_type;
15407   tree save, iter;
15408
15409   /* Complete the switch expression */
15410   se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15411   se_type = TREE_TYPE (se);
15412   /* The type of the switch expression must be char, byte, short or
15413      int */
15414   if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15415     {
15416       EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15417       parse_error_context (wfl_operator,
15418           "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15419                            lang_printable_name (se_type, 0));
15420       /* This is what java_complete_tree will check */
15421       TREE_OPERAND (node, 0) = error_mark_node;
15422       return error_mark_node;
15423     }
15424
15425   /* Save and restore the outer case label list.  */
15426   save = case_label_list;
15427   case_label_list = NULL_TREE;
15428
15429   TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15430
15431   /* See if we've found a duplicate label.  We can't leave this until
15432      code generation, because in `--syntax-only' and `-C' modes we
15433      don't do ordinary code generation.  */
15434   for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15435     {
15436       HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15437       tree subiter;
15438       for (subiter = TREE_CHAIN (iter);
15439            subiter != NULL_TREE;
15440            subiter = TREE_CHAIN (subiter))
15441         {
15442           HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15443           if (val == subval)
15444             {
15445               EXPR_WFL_LINECOL (wfl_operator)
15446                 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15447               /* The case_label_list is in reverse order, so print the
15448                  outer label first.  */
15449               parse_error_context (wfl_operator, "duplicate case label: %<"
15450                                    HOST_WIDE_INT_PRINT_DEC "%>", subval);
15451               EXPR_WFL_LINECOL (wfl_operator)
15452                 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15453               parse_error_context (wfl_operator, "original label is here");
15454
15455               break;
15456             }
15457         }
15458     }
15459
15460   case_label_list = save;
15461
15462   /* Ready to return */
15463   if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15464     {
15465       TREE_TYPE (node) = error_mark_node;
15466       return error_mark_node;
15467     }
15468   TREE_TYPE (node) = void_type_node;
15469   TREE_SIDE_EFFECTS (node) = 1;
15470   CAN_COMPLETE_NORMALLY (node)
15471     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15472       || ! SWITCH_HAS_DEFAULT (node);
15473   return node;
15474 }
15475
15476 /* Assertions.  */
15477
15478 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15479    might be NULL_TREE.  */
15480 static tree
15481 build_assertion (
15482 #ifdef USE_MAPPED_LOCATION
15483                  source_location location,
15484 #else
15485                  int location,
15486 #endif
15487                  tree condition, tree value)
15488 {
15489   tree node;
15490   tree klass = GET_CPC ();
15491
15492   if (! enable_assertions (klass))
15493     {
15494       condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15495                           boolean_false_node, condition);
15496       if (value == NULL_TREE)
15497         value = build_java_empty_stmt ();
15498       return build_if_else_statement (location, condition,
15499                                       value, NULL_TREE);
15500     }
15501
15502   if (! CLASS_USES_ASSERTIONS (klass))
15503     {
15504       tree field, classdollar, id, call;
15505       tree class_type = TREE_TYPE (klass);
15506
15507       field = add_field (class_type,
15508                          get_identifier ("$assertionsDisabled"),
15509                          boolean_type_node,
15510                          ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15511       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15512       FIELD_SYNTHETIC (field) = 1;
15513
15514       classdollar = build_incomplete_class_ref (location, class_type);
15515
15516       /* Call CLASS.desiredAssertionStatus().  */
15517       id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15518       call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15519       call = make_qualified_primary (classdollar, call, location);
15520       TREE_SIDE_EFFECTS (call) = 1;
15521
15522       /* Invert to obtain !CLASS.desiredAssertionStatus().  This may
15523          seem odd, but we do it to generate code identical to that of
15524          the JDK.  */
15525       call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15526       TREE_SIDE_EFFECTS (call) = 1;
15527       DECL_INITIAL (field) = call;
15528
15529       /* Record the initializer in the initializer statement list.  */
15530       call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15531       TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15532       SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15533       MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15534
15535       CLASS_USES_ASSERTIONS (klass) = 1;
15536     }
15537
15538   if (value != NULL_TREE)
15539     value = tree_cons (NULL_TREE, value, NULL_TREE);
15540
15541   node = build_wfl_node (get_identifier ("java"));
15542   node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15543                               location);
15544   node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15545                               location);
15546
15547   node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15548   TREE_SIDE_EFFECTS (node) = 1;
15549   /* It is too early to use BUILD_THROW.  */
15550   node = build1 (THROW_EXPR, NULL_TREE, node);
15551   TREE_SIDE_EFFECTS (node) = 1;
15552
15553   /* We invert the condition; if we just put NODE as the `else' part
15554      then we generate weird-looking bytecode.  */
15555   condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15556   /* Check $assertionsDisabled.  */
15557   condition
15558     = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15559               build1 (TRUTH_NOT_EXPR, NULL_TREE,
15560                       build_wfl_node (get_identifier ("$assertionsDisabled"))),
15561               condition);
15562   node = build_if_else_statement (location, condition, node, NULL_TREE);
15563   return node;
15564 }
15565
15566 /* 14.18 The try/catch statements */
15567
15568 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15569    catches TYPE and executes CATCH_STMTS.  */
15570
15571 static tree
15572 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15573                             tree catch_stmts)
15574 {
15575   tree try_block, catch_clause_param, catch_block, catch;
15576
15577   /* First build a try block */
15578   try_block = build_expr_block (try_stmts, NULL_TREE);
15579
15580   /* Build a catch block: we need a catch clause parameter */
15581   if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15582     {
15583       tree catch_type = obtain_incomplete_type (type_or_name);
15584       jdep *dep;
15585       catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15586       register_incomplete_type (JDEP_VARIABLE, type_or_name,
15587                                 catch_clause_param, catch_type);
15588       dep = CLASSD_LAST (ctxp->classd_list);
15589       JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15590     }
15591   else
15592     catch_clause_param = build_decl (VAR_DECL, wpv_id,
15593                                      build_pointer_type (type_or_name));
15594
15595   /* And a block */
15596   catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15597
15598   /* Initialize the variable and store in the block */
15599   catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15600                   build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15601   add_stmt_to_block (catch_block, NULL_TREE, catch);
15602
15603   /* Add the catch statements */
15604   add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15605
15606   /* Now we can build a JAVA_CATCH_EXPR */
15607   catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15608
15609   return build_try_statement (location, try_block, catch_block);
15610 }
15611
15612 static tree
15613 build_try_statement (int location, tree try_block, tree catches)
15614 {
15615   tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15616   EXPR_WFL_LINECOL (node) = location;
15617   return node;
15618 }
15619
15620 static tree
15621 build_try_finally_statement (int location, tree try_block, tree finally)
15622 {
15623   tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15624   EXPR_WFL_LINECOL (node) = location;
15625   return node;
15626 }
15627
15628 static tree
15629 patch_try_statement (tree node)
15630 {
15631   int error_found = 0;
15632   tree try = TREE_OPERAND (node, 0);
15633   /* Exception handlers are considered in left to right order */
15634   tree catch = nreverse (TREE_OPERAND (node, 1));
15635   tree current, caught_type_list = NULL_TREE;
15636
15637   /* Check catch clauses, if any. Every time we find an error, we try
15638      to process the next catch clause. We process the catch clause before
15639      the try block so that when processing the try block we can check thrown
15640      exceptions against the caught type list. */
15641   for (current = catch; current; current = TREE_CHAIN (current))
15642     {
15643       tree carg_decl, carg_type;
15644       tree sub_current, catch_block, catch_clause;
15645       int unreachable;
15646
15647       /* At this point, the structure of the catch clause is
15648            JAVA_CATCH_EXPR              (catch node)
15649              BLOCK              (with the decl of the parameter)
15650                COMPOUND_EXPR
15651                  MODIFY_EXPR   (assignment of the catch parameter)
15652                  BLOCK          (catch clause block)
15653        */
15654       catch_clause = TREE_OPERAND (current, 0);
15655       carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15656       carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15657
15658       /* Catch clauses can't have more than one parameter declared,
15659          but it's already enforced by the grammar. Make sure that the
15660          only parameter of the clause statement in of class Throwable
15661          or a subclass of Throwable, but that was done earlier. The
15662          catch clause parameter type has also been resolved. */
15663
15664       /* Just make sure that the catch clause parameter type inherits
15665          from java.lang.Throwable */
15666       if (!inherits_from_p (carg_type, throwable_type_node))
15667         {
15668           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15669           parse_error_context (wfl_operator,
15670                                "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15671                                lang_printable_name (carg_type, 0));
15672           error_found = 1;
15673           continue;
15674         }
15675
15676       /* Partial check for unreachable catch statement: The catch
15677          clause is reachable iff is no earlier catch block A in
15678          the try statement such that the type of the catch
15679          clause's parameter is the same as or a subclass of the
15680          type of A's parameter */
15681       unreachable = 0;
15682       for (sub_current = catch;
15683            sub_current != current; sub_current = TREE_CHAIN (sub_current))
15684         {
15685           tree sub_catch_clause, decl;
15686           sub_catch_clause = TREE_OPERAND (sub_current, 0);
15687           decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15688
15689           if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15690             {
15691               EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15692               parse_error_context
15693                 (wfl_operator,
15694                  "%<catch%> not reached because of the catch clause at line %d",
15695                  EXPR_WFL_LINENO (sub_current));
15696               unreachable = error_found = 1;
15697               break;
15698             }
15699         }
15700       /* Complete the catch clause block */
15701       catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15702       if (catch_block == error_mark_node)
15703         {
15704           error_found = 1;
15705           continue;
15706         }
15707       if (CAN_COMPLETE_NORMALLY (catch_block))
15708         CAN_COMPLETE_NORMALLY (node) = 1;
15709       TREE_OPERAND (current, 0) = catch_block;
15710
15711       if (unreachable)
15712         continue;
15713
15714       /* Things to do here: the exception must be thrown */
15715
15716       /* Link this type to the caught type list */
15717       caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15718     }
15719
15720   PUSH_EXCEPTIONS (caught_type_list);
15721   if ((try = java_complete_tree (try)) == error_mark_node)
15722     error_found = 1;
15723   if (CAN_COMPLETE_NORMALLY (try))
15724     CAN_COMPLETE_NORMALLY (node) = 1;
15725   POP_EXCEPTIONS ();
15726
15727   /* Verification ends here */
15728   if (error_found)
15729     return error_mark_node;
15730
15731   TREE_OPERAND (node, 0) = try;
15732   TREE_OPERAND (node, 1) = catch;
15733   TREE_TYPE (node) = void_type_node;
15734   return node;
15735 }
15736
15737 /* 14.17 The synchronized Statement */
15738
15739 static tree
15740 patch_synchronized_statement (tree node, tree wfl_op1)
15741 {
15742   tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15743   tree block = TREE_OPERAND (node, 1);
15744
15745   tree tmp, enter, exit, expr_decl, assignment;
15746
15747   if (expr == error_mark_node)
15748     {
15749       block = java_complete_tree (block);
15750       return expr;
15751     }
15752
15753   /* We might be trying to synchronize on a STRING_CST */
15754   if ((tmp = patch_string (expr)))
15755     expr = tmp;
15756
15757   /* The TYPE of expr must be a reference type */
15758   if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15759     {
15760       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15761       parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15762                            lang_printable_name (TREE_TYPE (expr), 0));
15763       return error_mark_node;
15764     }
15765
15766   if (flag_emit_xref)
15767     {
15768       TREE_OPERAND (node, 0) = expr;
15769       TREE_OPERAND (node, 1) = java_complete_tree (block);
15770       CAN_COMPLETE_NORMALLY (node) = 1;
15771       return node;
15772     }
15773
15774   /* Generate a try-finally for the synchronized statement, except
15775      that the handler that catches all throw exception calls
15776      _Jv_MonitorExit and then rethrow the exception.
15777      The synchronized statement is then implemented as:
15778      TRY
15779        {
15780          _Jv_MonitorEnter (expression)
15781          synchronized_block
15782          _Jv_MonitorExit (expression)
15783        }
15784      CATCH_ALL
15785        {
15786          e = _Jv_exception_info ();
15787          _Jv_MonitorExit (expression)
15788          Throw (e);
15789        } */
15790
15791   expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15792   BUILD_MONITOR_ENTER (enter, expr_decl);
15793   BUILD_MONITOR_EXIT (exit, expr_decl);
15794   CAN_COMPLETE_NORMALLY (enter) = 1;
15795   CAN_COMPLETE_NORMALLY (exit) = 1;
15796   assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15797   TREE_SIDE_EFFECTS (assignment) = 1;
15798   node = build2 (COMPOUND_EXPR, NULL_TREE,
15799                  build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15800                  build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15801   node = build_expr_block (node, expr_decl);
15802
15803   return java_complete_tree (node);
15804 }
15805
15806 /* 14.16 The throw Statement */
15807
15808 static tree
15809 patch_throw_statement (tree node, tree wfl_op1)
15810 {
15811   tree expr = TREE_OPERAND (node, 0);
15812   tree type = TREE_TYPE (expr);
15813   int unchecked_ok = 0, tryblock_throws_ok = 0;
15814
15815   /* Thrown expression must be assignable to java.lang.Throwable */
15816   if (!try_reference_assignconv (throwable_type_node, expr))
15817     {
15818       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15819       parse_error_context (wfl_operator,
15820     "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15821                            lang_printable_name (type, 0));
15822       /* If the thrown expression was a reference, we further the
15823          compile-time check. */
15824       if (!JREFERENCE_TYPE_P (type))
15825         return error_mark_node;
15826     }
15827
15828   /* At least one of the following must be true */
15829
15830   /* The type of the throw expression is a not checked exception,
15831      i.e. is a unchecked expression. */
15832   unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15833
15834   SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15835   /* An instance can't throw a checked exception unless that exception
15836      is explicitly declared in the `throws' clause of each
15837      constructor. This doesn't apply to anonymous classes, since they
15838      don't have declared constructors. */
15839   if (!unchecked_ok
15840       && DECL_INSTINIT_P (current_function_decl)
15841       && !ANONYMOUS_CLASS_P (current_class))
15842     {
15843       tree current;
15844       for (current = TYPE_METHODS (current_class); current;
15845            current = TREE_CHAIN (current))
15846         if (DECL_CONSTRUCTOR_P (current)
15847             && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15848           {
15849             parse_error_context (wfl_operator, "Checked exception %qs can't be thrown in instance initializer (not all declared constructor are declaring it in their %<throws%> clause)",
15850                                  lang_printable_name (TREE_TYPE (expr), 0));
15851             return error_mark_node;
15852           }
15853     }
15854
15855   /* Throw is contained in a try statement and at least one catch
15856      clause can receive the thrown expression or the current method is
15857      declared to throw such an exception. Or, the throw statement is
15858      contained in a method or constructor declaration and the type of
15859      the Expression is assignable to at least one type listed in the
15860      throws clause the declaration. */
15861   if (!unchecked_ok)
15862     tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15863   if (!(unchecked_ok || tryblock_throws_ok))
15864     {
15865       /* If there is a surrounding try block that has no matching
15866          clatch clause, report it first. A surrounding try block exits
15867          only if there is something after the list of checked
15868          exception thrown by the current function (if any). */
15869       if (IN_TRY_BLOCK_P ())
15870         parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15871                              lang_printable_name (type, 0));
15872       /* If we have no surrounding try statement and the method doesn't have
15873          any throws, report it now. FIXME */
15874
15875       /* We report that the exception can't be throw from a try block
15876          in all circumstances but when the `throw' is inside a static
15877          block. */
15878       else if (!EXCEPTIONS_P (currently_caught_type_list)
15879                && !tryblock_throws_ok)
15880         {
15881           if (DECL_CLINIT_P (current_function_decl))
15882             parse_error_context (wfl_operator,
15883                    "Checked exception %qs can't be thrown in initializer",
15884                                  lang_printable_name (type, 0));
15885           else
15886             parse_error_context (wfl_operator,
15887                    "Checked exception %qs isn't thrown from a %<try%> block",
15888                                  lang_printable_name (type, 0));
15889         }
15890       /* Otherwise, the current method doesn't have the appropriate
15891          throws declaration */
15892       else
15893         parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15894                              lang_printable_name (type, 0));
15895       return error_mark_node;
15896     }
15897
15898   if (! flag_emit_class_files && ! flag_emit_xref)
15899     BUILD_THROW (node, expr);
15900
15901   /* If doing xrefs, keep the location where the `throw' was seen. */
15902   if (flag_emit_xref)
15903     EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15904   return node;
15905 }
15906
15907 /* Check that exception said to be thrown by method DECL can be
15908    effectively caught from where DECL is invoked.  THIS_EXPR is the
15909    expression that computes `this' for the method call.  */
15910 static void
15911 check_thrown_exceptions (
15912 #ifdef USE_MAPPED_LOCATION
15913                          source_location location,
15914 #else
15915
15916                          int location,
15917 #endif
15918                          tree decl, tree this_expr)
15919 {
15920   tree throws;
15921   int is_array_call = 0;
15922
15923   /* Skip check within generated methods, such as access$<n>.  */
15924   if (OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15925     return;
15926
15927   if (this_expr != NULL_TREE
15928       && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15929       && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15930     is_array_call = 1;
15931
15932   /* For all the unchecked exceptions thrown by DECL.  */
15933   for (throws = DECL_FUNCTION_THROWS (decl); throws;
15934        throws = TREE_CHAIN (throws))
15935     if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15936       {
15937         /* Suppress errors about cloning arrays.  */
15938         if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
15939           continue;
15940
15941 #ifdef USE_MAPPED_LOCATION
15942         SET_EXPR_LOCATION (wfl_operator, location);
15943 #else
15944         EXPR_WFL_LINECOL (wfl_operator) = location;
15945 #endif
15946         if (DECL_FINIT_P (current_function_decl))
15947           parse_error_context
15948             (wfl_operator, "Exception %qs can't be thrown in initializer",
15949              lang_printable_name (TREE_VALUE (throws), 0));
15950         else
15951           {
15952             parse_error_context
15953               (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
15954                lang_printable_name (TREE_VALUE (throws), 0),
15955                (DECL_INIT_P (current_function_decl) ?
15956                 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15957                 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15958           }
15959       }
15960 }
15961
15962 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15963    try-catch blocks, OR is listed in the `throws' clause of the
15964    current method.  */
15965
15966 static int
15967 check_thrown_exceptions_do (tree exception)
15968 {
15969   tree list = currently_caught_type_list;
15970   resolve_and_layout (exception, NULL_TREE);
15971   /* First, all the nested try-catch-finally at that stage. The
15972      last element contains `throws' clause exceptions, if any. */
15973   if (IS_UNCHECKED_EXCEPTION_P (exception))
15974     return 1;
15975   while (list)
15976     {
15977       tree caught;
15978       for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15979         if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15980           return 1;
15981       list = TREE_CHAIN (list);
15982     }
15983   return 0;
15984 }
15985
15986 static void
15987 purge_unchecked_exceptions (tree mdecl)
15988 {
15989   tree throws = DECL_FUNCTION_THROWS (mdecl);
15990   tree new = NULL_TREE;
15991
15992   while (throws)
15993     {
15994       tree next = TREE_CHAIN (throws);
15995       if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15996         {
15997           TREE_CHAIN (throws) = new;
15998           new = throws;
15999         }
16000       throws = next;
16001     }
16002   /* List is inverted here, but it doesn't matter */
16003   DECL_FUNCTION_THROWS (mdecl) = new;
16004 }
16005
16006 /* This function goes over all of CLASS_TYPE ctors and checks whether
16007    each of them features at least one unchecked exception in its
16008    `throws' clause. If it's the case, it returns `true', `false'
16009    otherwise.  */
16010
16011 static bool
16012 ctors_unchecked_throws_clause_p (tree class_type)
16013 {
16014   tree current;
16015
16016   for (current = TYPE_METHODS (class_type); current;
16017        current = TREE_CHAIN (current))
16018     {
16019       bool ctu = false; /* Ctor Throws Unchecked */
16020       if (DECL_CONSTRUCTOR_P (current))
16021         {
16022           tree throws;
16023           for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16024                throws = TREE_CHAIN (throws))
16025             if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16026               ctu = true;
16027         }
16028       /* We return false as we found one ctor that is unfit. */
16029       if (!ctu && DECL_CONSTRUCTOR_P (current))
16030         return false;
16031     }
16032   /* All ctors feature at least one unchecked exception in their
16033      `throws' clause. */
16034   return true;
16035 }
16036
16037 /* 15.24 Conditional Operator ?: */
16038
16039 static tree
16040 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16041 {
16042   tree cond = TREE_OPERAND (node, 0);
16043   tree op1 = TREE_OPERAND (node, 1);
16044   tree op2 = TREE_OPERAND (node, 2);
16045   tree resulting_type = NULL_TREE;
16046   tree t1, t2, patched;
16047   int error_found = 0;
16048
16049   /* Operands of ?: might be StringBuffers crafted as a result of a
16050      string concatenation. Obtain a descent operand here.  */
16051   if ((patched = patch_string (op1)))
16052     TREE_OPERAND (node, 1) = op1 = patched;
16053   if ((patched = patch_string (op2)))
16054     TREE_OPERAND (node, 2) = op2 = patched;
16055
16056   t1 = TREE_TYPE (op1);
16057   t2 = TREE_TYPE (op2);
16058
16059   /* The first expression must be a boolean */
16060   if (TREE_TYPE (cond) != boolean_type_node)
16061     {
16062       SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16063       parse_error_context (wfl_operator,
16064                "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16065                            lang_printable_name (TREE_TYPE (cond), 0));
16066       error_found = 1;
16067     }
16068
16069   /* Second and third can be numeric, boolean (i.e. primitive),
16070      references or null. Anything else results in an error */
16071   if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16072         || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16073             && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16074         || (t1 == boolean_type_node && t2 == boolean_type_node)))
16075     error_found = 1;
16076
16077   /* Determine the type of the conditional expression. Same types are
16078      easy to deal with */
16079   else if (t1 == t2)
16080     resulting_type = t1;
16081
16082   /* There are different rules for numeric types */
16083   else if (JNUMERIC_TYPE_P (t1))
16084     {
16085       /* if byte/short found, the resulting type is short */
16086       if ((t1 == byte_type_node && t2 == short_type_node)
16087           || (t1 == short_type_node && t2 == byte_type_node))
16088         resulting_type = short_type_node;
16089
16090       /* If t1 is a constant int and t2 is of type byte, short or char
16091          and t1's value fits in t2, then the resulting type is t2 */
16092       else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16093           && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16094         resulting_type = t2;
16095
16096       /* If t2 is a constant int and t1 is of type byte, short or char
16097          and t2's value fits in t1, then the resulting type is t1 */
16098       else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16099           && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16100         resulting_type = t1;
16101
16102       /* Otherwise, binary numeric promotion is applied and the
16103          resulting type is the promoted type of operand 1 and 2 */
16104       else
16105         resulting_type = binary_numeric_promotion (t1, t2,
16106                                                    &TREE_OPERAND (node, 1),
16107                                                    &TREE_OPERAND (node, 2));
16108     }
16109
16110   /* Cases of a reference and a null type */
16111   else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16112     resulting_type = t1;
16113
16114   else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16115     resulting_type = t2;
16116
16117   /* Last case: different reference types. If a type can be converted
16118      into the other one by assignment conversion, the latter
16119      determines the type of the expression */
16120   else if ((resulting_type = try_reference_assignconv (t1, op2)))
16121     resulting_type = promote_type (t1);
16122
16123   else if ((resulting_type = try_reference_assignconv (t2, op1)))
16124     resulting_type = promote_type (t2);
16125
16126   /* If we don't have any resulting type, we're in trouble */
16127   if (!resulting_type)
16128     {
16129       char *t = xstrdup (lang_printable_name (t1, 0));
16130       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16131       parse_error_context (wfl_operator,
16132                  "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16133                            t, lang_printable_name (t2, 0));
16134       free (t);
16135       error_found = 1;
16136     }
16137
16138   if (error_found)
16139     {
16140       TREE_TYPE (node) = error_mark_node;
16141       return error_mark_node;
16142     }
16143
16144   TREE_TYPE (node) = resulting_type;
16145   TREE_SET_CODE (node, COND_EXPR);
16146   CAN_COMPLETE_NORMALLY (node) = 1;
16147   return node;
16148 }
16149
16150 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16151
16152 static tree
16153 maybe_build_class_init_for_field (tree decl, tree expr)
16154 {
16155   tree clas = DECL_CONTEXT (decl);
16156   if (flag_emit_class_files || flag_emit_xref)
16157     return expr;
16158
16159   if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16160       && FIELD_FINAL (decl))
16161     {
16162       tree init = DECL_INITIAL (decl);
16163       if (init != NULL_TREE)
16164         init = fold_constant_for_init (init, decl);
16165       if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16166         return expr;
16167     }
16168
16169   return build_class_init (clas, expr);
16170 }
16171
16172 /* Try to constant fold NODE.
16173    If NODE is not a constant expression, return NULL_EXPR.
16174    CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16175
16176 static tree
16177 fold_constant_for_init (tree node, tree context)
16178 {
16179   tree op0, op1, val;
16180   enum tree_code code = TREE_CODE (node);
16181
16182   switch (code)
16183     {
16184     case INTEGER_CST:
16185       if (node == null_pointer_node)
16186         return NULL_TREE;
16187     case STRING_CST:
16188     case REAL_CST:
16189       return node;
16190
16191     case PLUS_EXPR:
16192     case MINUS_EXPR:
16193     case MULT_EXPR:
16194     case TRUNC_MOD_EXPR:
16195     case RDIV_EXPR:
16196     case LSHIFT_EXPR:
16197     case RSHIFT_EXPR:
16198     case URSHIFT_EXPR:
16199     case BIT_AND_EXPR:
16200     case BIT_XOR_EXPR:
16201     case BIT_IOR_EXPR:
16202     case TRUTH_ANDIF_EXPR:
16203     case TRUTH_ORIF_EXPR:
16204     case EQ_EXPR:
16205     case NE_EXPR:
16206     case GT_EXPR:
16207     case GE_EXPR:
16208     case LT_EXPR:
16209     case LE_EXPR:
16210       op0 = TREE_OPERAND (node, 0);
16211       op1 = TREE_OPERAND (node, 1);
16212       val = fold_constant_for_init (op0, context);
16213       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16214         return NULL_TREE;
16215       TREE_OPERAND (node, 0) = val;
16216       val = fold_constant_for_init (op1, context);
16217       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16218         return NULL_TREE;
16219       TREE_OPERAND (node, 1) = val;
16220       return patch_binop (node, op0, op1);
16221
16222     case UNARY_PLUS_EXPR:
16223     case NEGATE_EXPR:
16224     case TRUTH_NOT_EXPR:
16225     case BIT_NOT_EXPR:
16226     case CONVERT_EXPR:
16227       op0 = TREE_OPERAND (node, 0);
16228       val = fold_constant_for_init (op0, context);
16229       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16230         return NULL_TREE;
16231       TREE_OPERAND (node, 0) = val;
16232       val = patch_unaryop (node, op0);
16233       if (! TREE_CONSTANT (val))
16234         return NULL_TREE;
16235       return val;
16236
16237       break;
16238
16239     case COND_EXPR:
16240       val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16241       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16242         return NULL_TREE;
16243       TREE_OPERAND (node, 0) = val;
16244       val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16245       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16246         return NULL_TREE;
16247       TREE_OPERAND (node, 1) = val;
16248       val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16249       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16250         return NULL_TREE;
16251       TREE_OPERAND (node, 2) = val;
16252       return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16253         : TREE_OPERAND (node, 2);
16254
16255     case VAR_DECL:
16256     case FIELD_DECL:
16257       if (! FIELD_FINAL (node)
16258           || DECL_INITIAL (node) == NULL_TREE)
16259         return NULL_TREE;
16260       val = DECL_INITIAL (node);
16261       /* Guard against infinite recursion. */
16262       DECL_INITIAL (node) = NULL_TREE;
16263       val = fold_constant_for_init (val, node);
16264       if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16265         val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16266       DECL_INITIAL (node) = val;
16267       return val;
16268
16269     case EXPR_WITH_FILE_LOCATION:
16270       /* Compare java_complete_tree and resolve_expression_name. */
16271       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16272           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16273         {
16274           tree name = EXPR_WFL_NODE (node);
16275           tree decl;
16276           if (PRIMARY_P (node))
16277             return NULL_TREE;
16278           else if (! QUALIFIED_P (name))
16279             {
16280               decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16281               if (decl == NULL_TREE
16282                   || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16283                 return NULL_TREE;
16284               return fold_constant_for_init (decl, decl);
16285             }
16286           else
16287             {
16288               tree r = NULL_TREE;
16289               /* Install the proper context for the field resolution.  */
16290               tree saved_current_class = current_class;
16291               /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
16292               current_class = DECL_CONTEXT (context);
16293               qualify_ambiguous_name (node);
16294               r = resolve_field_access (node, &decl, NULL);
16295               /* Restore prior context.  */
16296               current_class = saved_current_class;
16297               if (r != error_mark_node && decl != NULL_TREE)
16298                 return fold_constant_for_init (decl, decl);
16299               return NULL_TREE;
16300             }
16301         }
16302       else
16303         {
16304           op0 = TREE_OPERAND (node, 0);
16305           val = fold_constant_for_init (op0, context);
16306           if (val == NULL_TREE || ! TREE_CONSTANT (val))
16307             return NULL_TREE;
16308           TREE_OPERAND (node, 0) = val;
16309           return val;
16310         }
16311
16312 #ifdef USE_COMPONENT_REF
16313     case IDENTIFIER:
16314     case COMPONENT_REF:
16315       ?;
16316 #endif
16317
16318     default:
16319       return NULL_TREE;
16320     }
16321 }
16322
16323 #ifdef USE_COMPONENT_REF
16324 /* Context is 'T' for TypeName, 'P' for PackageName,
16325    'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16326
16327 tree
16328 resolve_simple_name (tree name, int context)
16329 {
16330 }
16331
16332 tree
16333 resolve_qualified_name (tree name, int context)
16334 {
16335 }
16336 #endif
16337
16338 void
16339 init_src_parse (void)
16340 {
16341   /* Sanity check; we've been bit by this before.  */
16342   if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16343     abort ();
16344 }
16345
16346 \f
16347
16348 /* This section deals with the functions that are called when tables
16349    recording class initialization information are traversed.  */
16350
16351 /* This function is called for each class that is known definitely
16352    initialized when a given static method was called. This function
16353    augments a compound expression (INFO) storing all assignment to
16354    initialized static class flags if a flag already existed, otherwise
16355    a new one is created.  */
16356
16357 static int
16358 emit_test_initialization (void **entry_p, void *info)
16359 {
16360   tree l = (tree) info;
16361   tree decl, init;
16362   tree key = (tree) *entry_p;
16363   tree *ite;
16364   htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16365
16366   /* If we haven't found a flag and we're dealing with self registered
16367      with current_function_decl, then don't do anything. Self is
16368      always added as definitely initialized but this information is
16369      valid only if used outside the current function. */
16370   if (current_function_decl == TREE_PURPOSE (l)
16371       && java_treetreehash_find (cf_ht, key) == NULL)
16372     return true;
16373
16374   ite = java_treetreehash_new (cf_ht, key);
16375
16376   /* If we don't have a variable, create one and install it. */
16377   if (*ite == NULL)
16378     {
16379       tree block;
16380
16381       decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16382       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16383       LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16384       DECL_CONTEXT (decl) = current_function_decl;
16385       DECL_INITIAL (decl) = boolean_true_node;
16386       /* Don't emit any symbolic debugging info for this decl.  */
16387       DECL_IGNORED_P (decl) = 1;
16388
16389       /* The trick is to find the right context for it. */
16390       block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16391       TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16392       BLOCK_EXPR_DECLS (block) = decl;
16393       *ite = decl;
16394     }
16395   else
16396     decl = *ite;
16397
16398   /* Now simply augment the compound that holds all the assignments
16399      pertaining to this method invocation. */
16400   init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16401   TREE_SIDE_EFFECTS (init) = 1;
16402   TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16403   TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16404
16405   return true;
16406 }
16407
16408 #ifdef __XGETTEXT__
16409 /* Depending on the version of Bison used to compile this grammar,
16410    it may issue generic diagnostics spelled "syntax error" or
16411    "parse error".  To prevent this from changing the translation
16412    template randomly, we list all the variants of this particular
16413    diagnostic here.  Translators: there is no fine distinction
16414    between diagnostics with "syntax error" in them, and diagnostics
16415    with "parse error" in them.  It's okay to give them both the same
16416    translation.  */
16417 const char d1[] = N_("syntax error");
16418 const char d2[] = N_("parse error");
16419 const char d3[] = N_("syntax error; also virtual memory exhausted");
16420 const char d4[] = N_("parse error; also virtual memory exhausted");
16421 const char d5[] = N_("syntax error: cannot back up");
16422 const char d6[] = N_("parse error: cannot back up");
16423 #endif
16424
16425 #include "gt-java-parse.h"
16426 #include "gtype-java.h"