OSDN Git Service

b54917cde63d5e01ecdcb81ea8bdccbd442c872d
[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, 2005
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, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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 "function.h"
71 #include "except.h"
72 #include "ggc.h"
73 #include "debug.h"
74 #include "tree-inline.h"
75 #include "cgraph.h"
76 #include "target.h"
77
78 /* Local function prototypes */
79 static char *java_accstring_lookup (int);
80 static const char *accessibility_string (int);
81 static void  classitf_redefinition_error (const char *,tree, tree, tree);
82 static void  variable_redefinition_error (tree, tree, tree, int);
83 static tree  create_class (int, tree, tree, tree);
84 static tree  create_interface (int, tree, tree);
85 static void  end_class_declaration (int);
86 static tree  find_field (tree, tree);
87 static tree lookup_field_wrapper (tree, tree);
88 static int   duplicate_declaration_error_p (tree, tree, tree);
89 static void  register_fields (int, tree, tree);
90 static tree parser_qualified_classname (tree);
91 static int  parser_check_super (tree, tree, tree);
92 static int  parser_check_super_interface (tree, tree, tree);
93 static void check_modifiers_consistency (int);
94 static tree lookup_cl (tree);
95 static tree lookup_java_method2 (tree, tree, int);
96 static tree method_header (int, tree, tree, tree);
97 static void fix_method_argument_names (tree ,tree);
98 static tree method_declarator (tree, tree);
99 static void parse_warning_context (tree cl, const char *gmsgid, ...) ATTRIBUTE_GCC_DIAG(2,3);
100 #ifdef USE_MAPPED_LOCATION
101 static void issue_warning_error_from_context
102   (source_location, const char *gmsgid, va_list *);
103 #else
104 static void issue_warning_error_from_context
105   (tree, const char *gmsgid, va_list *);
106 #endif
107 static void parse_ctor_invocation_error (void);
108 static tree parse_jdk1_1_error (const char *);
109 static void complete_class_report_errors (jdep *);
110 static int process_imports (void);
111 static void read_import_dir (tree);
112 static int find_in_imports_on_demand (tree, tree);
113 static void find_in_imports (tree, tree);
114 static void check_inner_class_access (tree, tree, tree);
115 static int check_pkg_class_access (tree, tree, bool, tree);
116 static tree resolve_package (tree, tree *, tree *);
117 static tree resolve_class (tree, tree, tree, tree);
118 static void declare_local_variables (int, tree, tree);
119 static void dump_java_tree (enum tree_dump_index, tree);
120 static void source_start_java_method (tree);
121 static void source_end_java_method (void);
122 static tree find_name_in_single_imports (tree);
123 static void check_abstract_method_header (tree);
124 static tree lookup_java_interface_method2 (tree, tree);
125 static tree resolve_expression_name (tree, tree *);
126 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
127 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
128 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
129 static tree resolve_and_layout (tree, tree);
130 static tree qualify_and_find (tree, tree, tree);
131 static tree resolve_no_layout (tree, tree);
132 static int invocation_mode (tree, int);
133 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
134 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
135 static tree find_most_specific_methods_list (tree);
136 static int argument_types_convertible (tree, tree);
137 static tree patch_invoke (tree, tree, tree);
138 static int maybe_use_access_method (int, tree *, tree *);
139 static tree lookup_method_invoke (int, tree, tree, tree, tree);
140 static tree register_incomplete_type (int, tree, tree, tree);
141 static tree check_inner_circular_reference (tree, tree);
142 static tree check_circular_reference (tree);
143 static tree obtain_incomplete_type (tree);
144 static tree java_complete_lhs (tree);
145 static tree java_complete_tree (tree);
146 static tree maybe_generate_pre_expand_clinit (tree);
147 static int analyze_clinit_body (tree, tree);
148 static int maybe_yank_clinit (tree);
149 static void start_complete_expand_method (tree);
150 static void java_complete_expand_method (tree);
151 static void java_expand_method_bodies (tree);
152 static int  unresolved_type_p (tree, tree *);
153 static void create_jdep_list (struct parser_ctxt *);
154 static tree build_expr_block (tree, tree);
155 static tree enter_block (void);
156 static tree exit_block (void);
157 static tree lookup_name_in_blocks (tree);
158 static void maybe_absorb_scoping_blocks (void);
159 static tree build_method_invocation (tree, tree);
160 static tree build_new_invocation (tree, tree);
161 static tree build_assignment (int, int, tree, tree);
162 static tree build_binop (enum tree_code, int, tree, tree);
163 static tree patch_assignment (tree, tree);
164 static tree patch_binop (tree, tree, tree, int);
165 static tree build_unaryop (int, int, tree);
166 static tree build_incdec (int, int, tree, int);
167 static tree patch_unaryop (tree, tree);
168 static tree build_cast (int, tree, tree);
169 static tree build_null_of_type (tree);
170 static tree patch_cast (tree, tree);
171 static int valid_ref_assignconv_cast_p (tree, tree, int);
172 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
173 static int valid_cast_to_p (tree, tree);
174 static int valid_method_invocation_conversion_p (tree, tree);
175 static tree try_builtin_assignconv (tree, tree, tree);
176 static tree try_reference_assignconv (tree, tree);
177 static tree build_unresolved_array_type (tree);
178 static int build_type_name_from_array_name (tree, tree *);
179 static tree build_array_from_name (tree, tree, tree, tree *);
180 static tree build_array_ref (int, tree, tree);
181 static tree patch_array_ref (tree);
182 #ifdef USE_MAPPED_LOCATION
183 static tree make_qualified_name (tree, tree, source_location);
184 #else
185 static tree make_qualified_name (tree, tree, int);
186 #endif
187 static tree merge_qualified_name (tree, tree);
188 static tree make_qualified_primary (tree, tree, int);
189 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
190 static void qualify_ambiguous_name (tree);
191 static tree resolve_field_access (tree, tree *, tree *);
192 static tree build_newarray_node (tree, tree, int);
193 static tree patch_newarray (tree);
194 static tree resolve_type_during_patch (tree);
195 static tree build_this (int);
196 static tree build_wfl_wrap (tree, int);
197 static tree build_return (int, tree);
198 static tree patch_return (tree);
199 static tree maybe_access_field (tree, tree, tree);
200 static int complete_function_arguments (tree);
201 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
202 static int not_accessible_p (tree, tree, tree, int);
203 static void check_deprecation (tree, tree);
204 static int class_in_current_package (tree);
205 static tree build_if_else_statement (int, tree, tree, tree);
206 static tree patch_if_else_statement (tree);
207 static tree add_stmt_to_block (tree, tree, tree);
208 static tree patch_exit_expr (tree);
209 static tree build_labeled_block (int, tree);
210 static tree finish_labeled_statement (tree, tree);
211 static tree build_bc_statement (int, int, tree);
212 static tree patch_bc_statement (tree);
213 static tree patch_loop_statement (tree);
214 static tree build_new_loop (tree);
215 static tree build_loop_body (int, tree, int);
216 static tree finish_loop_body (int, tree, tree, int);
217 static tree build_debugable_stmt (int, tree);
218 static tree finish_for_loop (int, tree, tree, tree);
219 static tree patch_switch_statement (tree);
220 static tree string_constant_concatenation (tree, tree);
221 static tree build_string_concatenation (tree, tree);
222 static tree patch_string_cst (tree);
223 static tree patch_string (tree);
224 static tree encapsulate_with_try_catch (int, tree, tree, tree);
225 #ifdef USE_MAPPED_LOCATION
226 static tree build_assertion (source_location, tree, tree);
227 #else
228 static tree build_assertion (int, tree, tree);
229 #endif
230 static tree build_try_statement (int, tree, tree);
231 static tree build_try_finally_statement (int, tree, tree);
232 static tree patch_try_statement (tree);
233 static tree patch_synchronized_statement (tree, tree);
234 static tree patch_throw_statement (tree, tree);
235 #ifdef USE_MAPPED_LOCATION
236 static void check_thrown_exceptions (source_location, tree, tree);
237 #else
238 static void check_thrown_exceptions (int, tree, tree);
239 #endif
240 static int check_thrown_exceptions_do (tree);
241 static void purge_unchecked_exceptions (tree);
242 static bool ctors_unchecked_throws_clause_p (tree);
243 static void check_concrete_throws_clauses (tree, tree, tree, tree);
244 static void check_throws_clauses (tree, tree, tree);
245 static void finish_method_declaration (tree);
246 static tree build_super_invocation (tree);
247 static int verify_constructor_circularity (tree, tree);
248 static char *constructor_circularity_msg (tree, tree);
249 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
250 static const char *get_printable_method_name (tree);
251 static tree patch_conditional_expr (tree, tree, tree);
252 static tree generate_finit (tree);
253 static tree generate_instinit (tree);
254 static tree build_instinit_invocation (tree);
255 static void fix_constructors (tree);
256 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
257 static tree craft_constructor (tree, tree);
258 static int verify_constructor_super (tree);
259 static tree create_artificial_method (tree, int, tree, tree, tree);
260 static void start_artificial_method_body (tree);
261 static void end_artificial_method_body (tree);
262 static int check_method_redefinition (tree, tree);
263 static int check_method_types_complete (tree);
264 static bool hack_is_accessible_p (tree, tree);
265 static void java_check_regular_methods (tree);
266 static void check_interface_throws_clauses (tree, tree);
267 static void java_check_abstract_methods (tree);
268 static void unreachable_stmt_error (tree);
269 static int not_accessible_field_error (tree, tree);
270 static tree find_expr_with_wfl (tree);
271 static void missing_return_error (tree);
272 static tree build_new_array_init (int, tree);
273 static tree patch_new_array_init (tree, tree);
274 static tree maybe_build_array_element_wfl (tree);
275 static int array_constructor_check_entry (tree, tree);
276 static const char *purify_type_name (const char *);
277 static tree fold_constant_for_init (tree, tree);
278 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
279 static void static_ref_err (tree, tree, tree);
280 static void parser_add_interface (tree, tree, tree);
281 static void add_superinterfaces (tree, tree);
282 static tree jdep_resolve_class (jdep *);
283 static int note_possible_classname (const char *, int);
284 static void java_complete_expand_classes (void);
285 static void java_complete_expand_class (tree);
286 static void java_complete_expand_methods (tree);
287 static tree cut_identifier_in_qualified (tree);
288 static tree java_stabilize_reference (tree);
289 static tree do_unary_numeric_promotion (tree);
290 static char * operator_string (tree);
291 static tree do_merge_string_cste (tree, const char *, int, int);
292 static tree merge_string_cste (tree, tree, int);
293 static tree java_refold (tree);
294 static int java_decl_equiv (tree, tree);
295 static int binop_compound_p (enum tree_code);
296 static tree search_loop (tree);
297 static int labeled_block_contains_loop_p (tree, tree);
298 static int check_abstract_method_definitions (int, tree, tree);
299 static void java_check_abstract_method_definitions (tree);
300 static void java_debug_context_do (int);
301 static void java_parser_context_push_initialized_field (void);
302 static void java_parser_context_pop_initialized_field (void);
303 static tree reorder_static_initialized (tree);
304 static void java_parser_context_suspend (void);
305 static void java_parser_context_resume (void);
306 static int pop_current_osb (struct parser_ctxt *);
307
308 /* JDK 1.1 work. FIXME */
309
310 static tree maybe_make_nested_class_name (tree);
311 static int make_nested_class_name (tree);
312 static void link_nested_class_to_enclosing (void);
313 static tree resolve_inner_class (htab_t, tree, tree *, tree *, tree);
314 static tree find_as_inner_class (tree, tree, tree);
315 static tree find_as_inner_class_do (tree, tree);
316 static int check_inner_class_redefinition (tree, tree);
317
318 static tree build_thisn_assign (void);
319 static tree build_current_thisn (tree);
320 static tree build_access_to_thisn (tree, tree, int);
321 static tree maybe_build_thisn_access_method (tree);
322
323 static tree build_nested_field_access (tree, tree);
324 static tree build_nested_field_access_methods (tree);
325 static tree build_nested_field_access_method (tree, tree, tree, tree, tree);
326 static tree build_nested_field_access_expr (int, tree, tree, tree, tree);
327 static tree build_outer_method_access_method (tree);
328 static tree build_new_access_id (void);
329
330 static int nested_field_access_p (tree, tree);
331 static int nested_field_expanded_access_p (tree, tree *, tree *, tree *);
332 static tree nested_field_access_fix (tree, tree, tree);
333
334 static tree build_incomplete_class_ref (int, tree);
335 static tree patch_incomplete_class_ref (tree);
336 static tree create_anonymous_class (tree);
337 static void patch_anonymous_class (tree, tree, tree);
338 static void add_inner_class_fields (tree, tree);
339
340 static tree build_dot_class_method (tree);
341 static tree build_dot_class_method_invocation (tree, tree);
342 static void create_new_parser_context (int);
343 static tree maybe_build_class_init_for_field (tree, tree);
344
345 static int emit_test_initialization (void **, void *);
346
347 static char *string_convert_int_cst (tree);
348
349 /* Number of error found so far. */
350 int java_error_count;
351 /* Number of warning found so far. */
352 int java_warning_count;
353 /* Cyclic inheritance report, as it can be set by layout_class */
354 const char *cyclic_inheritance_report;
355
356 /* The current parser context */
357 struct parser_ctxt *ctxp;
358
359 /* List of things that were analyzed for which code will be generated */
360 struct parser_ctxt *ctxp_for_generation = NULL;
361 struct parser_ctxt *ctxp_for_generation_last = NULL;
362
363 /* binop_lookup maps token to tree_code. It is used where binary
364    operations are involved and required by the parser. RDIV_EXPR
365    covers both integral/floating point division. The code is changed
366    once the type of both operator is worked out.  */
367
368 static const enum tree_code binop_lookup[19] =
369   {
370     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
371     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
372     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
373     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
374     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
375    };
376 #define BINOP_LOOKUP(VALUE)                                             \
377   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
378
379 /* This is the end index for binary operators that can also be used
380    in compound assignments. */
381 #define BINOP_COMPOUND_CANDIDATES 11
382
383 /* The "$L" identifier we use to create labels.  */
384 static GTY(()) tree label_id;
385
386 /* The "StringBuffer" identifier used for the String `+' operator. */
387 static GTY(()) tree wfl_string_buffer;
388
389 /* The "append" identifier used for String `+' operator.  */
390 static GTY(()) tree wfl_append;
391
392 /* The "toString" identifier used for String `+' operator. */
393 static GTY(()) tree wfl_to_string;
394
395 /* The "java.lang" import qualified name.  */
396 static GTY(()) tree java_lang_id;
397
398 /* The generated `inst$' identifier used for generated enclosing
399    instance/field access functions.  */
400 static GTY(()) tree inst_id;
401
402 /* Context and flag for static blocks */
403 static GTY(()) tree current_static_block;
404
405 /* The generated `write_parm_value$' identifier.  */
406 static GTY(()) tree wpv_id;
407
408 /* Hold THIS for the scope of the current method decl.  */
409 static GTY(()) tree current_this;
410
411 /* Hold a list of catch clauses list. The first element of this list is
412    the list of the catch clauses of the currently analyzed try block. */
413 static GTY(()) tree currently_caught_type_list;
414
415 /* This holds a linked list of all the case labels for the current
416    switch statement.  It is only used when checking to see if there
417    are duplicate labels.  FIXME: probably this should just be attached
418    to the switch itself; then it could be referenced via
419    `ctxp->current_loop'.  */
420 static GTY(()) tree case_label_list;
421
422 /* Anonymous class counter. Will be reset to 1 every time a non
423    anonymous class gets created. */
424 static int anonymous_class_counter = 1;
425
426 static GTY(()) tree src_parse_roots[1];
427
428 /* All classes seen from source code */
429 #define gclass_list src_parse_roots[0]
430
431 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
432    line and point it out.  */
433 /* Should point out the one that don't fit. ASCII/unicode, going
434    backward. FIXME */
435
436 #define check_modifiers(__message, __value, __mask) do {        \
437   if ((__value) & ~(__mask))                                    \
438     {                                                           \
439       size_t i, remainder = (__value) & ~(__mask);              \
440       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)     \
441         if ((1 << i) & remainder)                               \
442           parse_error_context (ctxp->modifier_ctx [i], (__message), \
443                                java_accstring_lookup (1 << i)); \
444     }                                                           \
445 } while (0)
446
447 %}
448
449 %union {
450   tree node;
451   int sub_token;
452   struct {
453     int token;
454 #ifdef USE_MAPPED_LOCATION
455     source_location location;
456 #else
457     int location;
458 #endif
459   } operator;
460   int value;
461 }
462
463 %{
464 #ifdef USE_MAPPED_LOCATION
465 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
466   SET_EXPR_LOCATION(EXPR, (TOKEN).location)
467 #else
468 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
469   (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
470 #endif
471
472 #include "lex.c"
473 %}
474
475 %pure_parser
476
477 /* Things defined here have to match the order of what's in the
478    binop_lookup table.  */
479
480 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
481 %token   LS_TK           SRS_TK          ZRS_TK
482 %token   AND_TK          XOR_TK          OR_TK
483 %token   BOOL_AND_TK BOOL_OR_TK
484 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
485
486 /* This maps to the same binop_lookup entry than the token above */
487
488 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
489 %token   REM_ASSIGN_TK
490 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
491 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
492
493
494 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
495
496 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
497 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
498 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
499 %token   PAD_TK          ABSTRACT_TK        STRICT_TK
500 %token   MODIFIER_TK
501
502 /* Keep those two in order, too */
503 %token   DECR_TK INCR_TK
504
505 /* From now one, things can be in any order */
506
507 %token   DEFAULT_TK      IF_TK              THROW_TK
508 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
509 %token   THROWS_TK       BREAK_TK           IMPORT_TK
510 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
511 %token   VOID_TK         CATCH_TK           INTERFACE_TK
512 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
513 %token   SUPER_TK        WHILE_TK           CLASS_TK
514 %token   SWITCH_TK       CONST_TK           TRY_TK
515 %token   FOR_TK          NEW_TK             CONTINUE_TK
516 %token   GOTO_TK         PACKAGE_TK         THIS_TK
517 %token   ASSERT_TK
518
519 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
520 %token   CHAR_TK         INTEGRAL_TK
521
522 %token   FLOAT_TK        DOUBLE_TK          FP_TK
523
524 %token   ID_TK
525
526 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
527
528 %token   ASSIGN_ANY_TK   ASSIGN_TK
529 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
530
531 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
532 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
533
534 %type    <value>        modifiers MODIFIER_TK final synchronized
535
536 %type    <node>         super ID_TK identifier
537 %type    <node>         name simple_name qualified_name
538 %type    <node>         type_declaration compilation_unit
539                         field_declaration method_declaration extends_interfaces
540                         interfaces interface_type_list
541                         import_declarations package_declaration
542                         type_declarations interface_body
543                         interface_member_declaration constant_declaration
544                         interface_member_declarations interface_type
545                         abstract_method_declaration
546 %type    <node>         class_body_declaration class_member_declaration
547                         static_initializer constructor_declaration block
548 %type    <node>         class_body_declarations constructor_header
549 %type    <node>         class_or_interface_type class_type class_type_list
550                         constructor_declarator explicit_constructor_invocation
551 %type    <node>         dim_expr dim_exprs this_or_super throws
552
553 %type    <node>         variable_declarator_id variable_declarator
554                         variable_declarators variable_initializer
555                         variable_initializers constructor_body
556                         array_initializer
557
558 %type    <node>         class_body block_end constructor_block_end
559 %type    <node>         statement statement_without_trailing_substatement
560                         labeled_statement if_then_statement label_decl
561                         if_then_else_statement while_statement for_statement
562                         statement_nsi labeled_statement_nsi do_statement
563                         if_then_else_statement_nsi while_statement_nsi
564                         for_statement_nsi statement_expression_list for_init
565                         for_update statement_expression expression_statement
566                         primary_no_new_array expression primary array_type
567                         array_creation_initialized array_creation_uninitialized
568                         class_instance_creation_expression field_access
569                         method_invocation array_access something_dot_new
570                         argument_list postfix_expression while_expression
571                         post_increment_expression post_decrement_expression
572                         unary_expression_not_plus_minus unary_expression
573                         pre_increment_expression pre_decrement_expression
574                         cast_expression
575                         multiplicative_expression additive_expression
576                         shift_expression relational_expression
577                         equality_expression and_expression
578                         exclusive_or_expression inclusive_or_expression
579                         conditional_and_expression conditional_or_expression
580                         conditional_expression assignment_expression
581                         left_hand_side assignment for_header for_begin
582                         constant_expression do_statement_begin empty_statement
583                         switch_statement synchronized_statement throw_statement
584                         try_statement assert_statement
585                         switch_expression switch_block
586                         catches catch_clause catch_clause_parameter finally
587                         anonymous_class_creation trap_overflow_corner_case
588 %type    <node>         return_statement break_statement continue_statement
589
590 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK
591 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
592 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
593 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
594 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
595 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
596 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
597 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
598 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
599 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
600 %type    <operator>     THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
601 %type    <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
602 %type    <operator>     NEW_TK ASSERT_TK
603
604 %type    <node>         method_body
605
606 %type    <node>         literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
607                         STRING_LIT_TK NULL_TK VOID_TK
608
609 %type    <node>         IF_TK WHILE_TK FOR_TK
610
611 %type    <node>         formal_parameter_list formal_parameter
612                         method_declarator method_header
613
614 %type    <node>         primitive_type reference_type type
615                         BOOLEAN_TK INTEGRAL_TK FP_TK
616
617 /* Added or modified JDK 1.1 rule types  */
618 %type    <node>         type_literals
619
620 %%
621 /* 19.2 Production from 2.3: The Syntactic Grammar  */
622 goal:  compilation_unit
623                 {}
624 ;
625
626 /* 19.3 Productions from 3: Lexical structure  */
627 literal:
628         INT_LIT_TK
629 |       FP_LIT_TK
630 |       BOOL_LIT_TK
631 |       CHAR_LIT_TK
632 |       STRING_LIT_TK
633 |       NULL_TK
634 ;
635
636 /* 19.4 Productions from 4: Types, Values and Variables  */
637 type:
638         primitive_type
639 |       reference_type
640 ;
641
642 primitive_type:
643         INTEGRAL_TK
644 |       FP_TK
645 |       BOOLEAN_TK
646 ;
647
648 reference_type:
649         class_or_interface_type
650 |       array_type
651 ;
652
653 class_or_interface_type:
654         name
655 ;
656
657 class_type:
658         class_or_interface_type /* Default rule */
659 ;
660
661 interface_type:
662          class_or_interface_type
663 ;
664
665 array_type:
666         primitive_type dims
667                 {
668                   int osb = pop_current_osb (ctxp);
669                   tree t = build_java_array_type (($1), -1);
670                   while (--osb)
671                     t = build_unresolved_array_type (t);
672                   $$ = t;
673                 }
674 |       name dims
675                 {
676                   int osb = pop_current_osb (ctxp);
677                   tree t = $1;
678                   while (osb--)
679                     t = build_unresolved_array_type (t);
680                   $$ = t;
681                 }
682 ;
683
684 /* 19.5 Productions from 6: Names  */
685 name:
686         simple_name             /* Default rule */
687 |       qualified_name          /* Default rule */
688 ;
689
690 simple_name:
691         identifier              /* Default rule */
692 ;
693
694 qualified_name:
695         name DOT_TK identifier
696                 { $$ = make_qualified_name ($1, $3, $2.location); }
697 ;
698
699 identifier:
700         ID_TK
701 ;
702
703 /* 19.6: Production from 7: Packages  */
704 compilation_unit:
705                 {$$ = NULL;}
706 |       package_declaration
707 |       import_declarations
708 |       type_declarations
709 |       package_declaration import_declarations
710 |       package_declaration type_declarations
711 |       import_declarations type_declarations
712 |       package_declaration import_declarations type_declarations
713 ;
714
715 import_declarations:
716         import_declaration
717                 {
718                   $$ = NULL;
719                 }
720 |       import_declarations import_declaration
721                 {
722                   $$ = NULL;
723                 }
724 ;
725
726 type_declarations:
727         type_declaration
728 |       type_declarations type_declaration
729 ;
730
731 package_declaration:
732         PACKAGE_TK name SC_TK
733                 {
734                   ctxp->package = EXPR_WFL_NODE ($2);
735                 }
736 |       PACKAGE_TK error
737                 {yyerror ("Missing name"); RECOVER;}
738 |       PACKAGE_TK name error
739                 {yyerror ("';' expected"); RECOVER;}
740 ;
741
742 import_declaration:
743         single_type_import_declaration
744 |       type_import_on_demand_declaration
745 ;
746
747 single_type_import_declaration:
748         IMPORT_TK name SC_TK
749                 {
750                   tree name = EXPR_WFL_NODE ($2), last_name;
751                   int   i = IDENTIFIER_LENGTH (name)-1;
752                   const char *last = &IDENTIFIER_POINTER (name)[i];
753                   while (last != IDENTIFIER_POINTER (name))
754                     {
755                       if (last [0] == '.')
756                         break;
757                       last--;
758                     }
759                   last_name = get_identifier (++last);
760                   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
761                     {
762                       tree err = find_name_in_single_imports (last_name);
763                       if (err && err != name)
764                         parse_error_context
765                           ($2, "Ambiguous class: %qs and %qs",
766                            IDENTIFIER_POINTER (name),
767                            IDENTIFIER_POINTER (err));
768                       else
769                         REGISTER_IMPORT ($2, last_name);
770                     }
771                   else
772                     REGISTER_IMPORT ($2, last_name);
773                 }
774 |       IMPORT_TK error
775                 {yyerror ("Missing name"); RECOVER;}
776 |       IMPORT_TK name error
777                 {yyerror ("';' expected"); RECOVER;}
778 ;
779
780 type_import_on_demand_declaration:
781         IMPORT_TK name DOT_TK MULT_TK SC_TK
782                 {
783                   tree name = EXPR_WFL_NODE ($2);
784                   tree it;
785                   /* Search for duplicates. */
786                   for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
787                     if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
788                       break;
789                   /* Don't import the same thing more than once, just ignore
790                      duplicates (7.5.2) */
791                   if (! it)
792                     {
793                       read_import_dir ($2);
794                       ctxp->import_demand_list =
795                         chainon (ctxp->import_demand_list,
796                                  build_tree_list ($2, NULL_TREE));
797                     }
798                 }
799 |       IMPORT_TK name DOT_TK error
800                 {yyerror ("'*' expected"); RECOVER;}
801 |       IMPORT_TK name DOT_TK MULT_TK error
802                 {yyerror ("';' expected"); RECOVER;}
803 ;
804
805 type_declaration:
806         class_declaration
807                 { end_class_declaration (0); }
808 |       interface_declaration
809                 { end_class_declaration (0); }
810 |       empty_statement
811 |       error
812                 {
813                   YYERROR_NOW;
814                   yyerror ("Class or interface declaration expected");
815                 }
816 ;
817
818 /* 19.7 Shortened from the original:
819    modifiers: modifier | modifiers modifier
820    modifier: any of public...  */
821 modifiers:
822         MODIFIER_TK
823                 {
824                   $$ = (1 << $1);
825                 }
826 |       modifiers MODIFIER_TK
827                 {
828                   int acc = (1 << $2);
829                   if ($$ & acc)
830                     parse_error_context
831                       (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
832                        java_accstring_lookup (acc));
833                   else
834                     {
835                       $$ |= acc;
836                     }
837                 }
838 ;
839
840 /* 19.8.1 Production from $8.1: Class Declaration */
841 class_declaration:
842         modifiers CLASS_TK identifier super interfaces
843                 { create_class ($1, $3, $4, $5); }
844         class_body
845                 {;}
846 |       CLASS_TK identifier super interfaces
847                 { create_class (0, $2, $3, $4); }
848         class_body
849                 {;}
850 |       modifiers CLASS_TK error
851                 { yyerror ("Missing class name"); RECOVER; }
852 |       CLASS_TK error
853                 { yyerror ("Missing class name"); RECOVER; }
854 |       CLASS_TK identifier error
855                 {
856                   if (!ctxp->class_err) yyerror ("'{' expected");
857                   DRECOVER(class1);
858                 }
859 |       modifiers CLASS_TK identifier error
860                 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
861 ;
862
863 super:
864                 { $$ = NULL; }
865 |       EXTENDS_TK class_type
866                 { $$ = $2; }
867 |       EXTENDS_TK class_type error
868                 {yyerror ("'{' expected"); ctxp->class_err=1;}
869 |       EXTENDS_TK error
870                 {yyerror ("Missing super class name"); ctxp->class_err=1;}
871 ;
872
873 interfaces:
874                 { $$ = NULL_TREE; }
875 |       IMPLEMENTS_TK interface_type_list
876                 { $$ = $2; }
877 |       IMPLEMENTS_TK error
878                 {
879                   ctxp->class_err=1;
880                   yyerror ("Missing interface name");
881                 }
882 ;
883
884 interface_type_list:
885         interface_type
886                 {
887                   ctxp->interface_number = 1;
888                   $$ = build_tree_list ($1, NULL_TREE);
889                 }
890 |       interface_type_list C_TK interface_type
891                 {
892                   ctxp->interface_number++;
893                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
894                 }
895 |       interface_type_list C_TK error
896                 {yyerror ("Missing interface name"); RECOVER;}
897 ;
898
899 class_body:
900         OCB_TK CCB_TK
901                 {
902                   $$ = GET_CPC ();
903                 }
904 |       OCB_TK class_body_declarations CCB_TK
905                 {
906                   $$ = GET_CPC ();
907                 }
908 ;
909
910 class_body_declarations:
911         class_body_declaration
912 |       class_body_declarations class_body_declaration
913 ;
914
915 class_body_declaration:
916         class_member_declaration
917 |       static_initializer
918 |       constructor_declaration
919 |       block                   /* Added, JDK1.1, instance initializer */
920                 {
921                   if (!IS_EMPTY_STMT ($1))
922                     {
923                       TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
924                       SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
925                     }
926                 }
927 ;
928
929 class_member_declaration:
930         field_declaration
931 |       method_declaration
932 |       class_declaration       /* Added, JDK1.1 inner classes */
933                 { end_class_declaration (1); }
934 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
935                 { end_class_declaration (1); }
936 |       empty_statement
937 ;
938
939 /* 19.8.2 Productions from 8.3: Field Declarations  */
940 field_declaration:
941         type variable_declarators SC_TK
942                 { register_fields (0, $1, $2); }
943 |       modifiers type variable_declarators SC_TK
944                 {
945                   check_modifiers
946                     ("Illegal modifier %qs for field declaration",
947                      $1, FIELD_MODIFIERS);
948                   check_modifiers_consistency ($1);
949                   register_fields ($1, $2, $3);
950                 }
951 ;
952
953 variable_declarators:
954         /* Should we use build_decl_list () instead ? FIXME */
955         variable_declarator     /* Default rule */
956 |       variable_declarators C_TK variable_declarator
957                 { $$ = chainon ($1, $3); }
958 |       variable_declarators C_TK error
959                 {yyerror ("Missing term"); RECOVER;}
960 ;
961
962 variable_declarator:
963         variable_declarator_id
964                 { $$ = build_tree_list ($1, NULL_TREE); }
965 |       variable_declarator_id ASSIGN_TK variable_initializer
966                 {
967                   if (java_error_count)
968                     $3 = NULL_TREE;
969                   $$ = build_tree_list
970                     ($1, build_assignment ($2.token, $2.location, $1, $3));
971                 }
972 |       variable_declarator_id ASSIGN_TK error
973                 {
974                   yyerror ("Missing variable initializer");
975                   $$ = build_tree_list ($1, NULL_TREE);
976                   RECOVER;
977                 }
978 |       variable_declarator_id ASSIGN_TK variable_initializer error
979                 {
980                   yyerror ("';' expected");
981                   $$ = build_tree_list ($1, NULL_TREE);
982                   RECOVER;
983                 }
984 ;
985
986 variable_declarator_id:
987         identifier
988 |       variable_declarator_id OSB_TK CSB_TK
989                 { $$ = build_unresolved_array_type ($1); }
990 |       identifier error
991                 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
992 |       variable_declarator_id OSB_TK error
993                 {
994                   yyerror ("']' expected");
995                   DRECOVER(vdi);
996                 }
997 |       variable_declarator_id CSB_TK error
998                 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
999 ;
1000
1001 variable_initializer:
1002         expression
1003 |       array_initializer
1004 ;
1005
1006 /* 19.8.3 Productions from 8.4: Method Declarations  */
1007 method_declaration:
1008         method_header
1009                 {
1010                   current_function_decl = $1;
1011                   if (current_function_decl
1012                       && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1013                     source_start_java_method (current_function_decl);
1014                   else
1015                     current_function_decl = NULL_TREE;
1016                 }
1017         method_body
1018                 { finish_method_declaration ($3); }
1019 |       method_header error
1020                 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1021 ;
1022
1023 method_header:
1024         type method_declarator throws
1025                 { $$ = method_header (0, $1, $2, $3); }
1026 |       VOID_TK method_declarator throws
1027                 { $$ = method_header (0, void_type_node, $2, $3); }
1028 |       modifiers type method_declarator throws
1029                 { $$ = method_header ($1, $2, $3, $4); }
1030 |       modifiers VOID_TK method_declarator throws
1031                 { $$ = method_header ($1, void_type_node, $3, $4); }
1032 |       type error
1033                 {
1034                   yyerror ("Invalid method declaration, method name required");
1035                   RECOVER;
1036                 }
1037 |       modifiers type error
1038                 {
1039                   yyerror ("Identifier expected");
1040                   RECOVER;
1041                 }
1042 |       VOID_TK error
1043                 {
1044                   yyerror ("Identifier expected");
1045                   RECOVER;
1046                 }
1047 |       modifiers VOID_TK error
1048                 {
1049                   yyerror ("Identifier expected");
1050                   RECOVER;
1051                 }
1052 |       modifiers error
1053                 {
1054                   yyerror ("Invalid method declaration, return type required");
1055                   RECOVER;
1056                 }
1057 ;
1058
1059 method_declarator:
1060         identifier OP_TK CP_TK
1061                 {
1062                   ctxp->formal_parameter_number = 0;
1063                   $$ = method_declarator ($1, NULL_TREE);
1064                 }
1065 |       identifier OP_TK formal_parameter_list CP_TK
1066                 { $$ = method_declarator ($1, $3); }
1067 |       method_declarator OSB_TK CSB_TK
1068                 {
1069                   SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1070                   TREE_PURPOSE ($1) =
1071                     build_unresolved_array_type (TREE_PURPOSE ($1));
1072                   parse_warning_context
1073                     (wfl_operator,
1074                      "Discouraged form of returned type specification");
1075                 }
1076 |       identifier OP_TK error
1077                 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1078 |       method_declarator OSB_TK error
1079                 {yyerror ("']' expected"); RECOVER;}
1080 ;
1081
1082 formal_parameter_list:
1083         formal_parameter
1084                 {
1085                   ctxp->formal_parameter_number = 1;
1086                 }
1087 |       formal_parameter_list C_TK formal_parameter
1088                 {
1089                   ctxp->formal_parameter_number += 1;
1090                   $$ = chainon ($1, $3);
1091                 }
1092 |       formal_parameter_list C_TK error
1093                 { yyerror ("Missing formal parameter term"); RECOVER; }
1094 ;
1095
1096 formal_parameter:
1097         type variable_declarator_id
1098                 {
1099                   $$ = build_tree_list ($2, $1);
1100                 }
1101 |       final type variable_declarator_id /* Added, JDK1.1 final parms */
1102                 {
1103                   $$ = build_tree_list ($3, $2);
1104                   ARG_FINAL_P ($$) = 1;
1105                 }
1106 |       type error
1107                 {
1108                   yyerror ("Missing identifier"); RECOVER;
1109                   $$ = NULL_TREE;
1110                 }
1111 |       final type error
1112                 {
1113                   yyerror ("Missing identifier"); RECOVER;
1114                   $$ = NULL_TREE;
1115                 }
1116 ;
1117
1118 final:
1119         modifiers
1120                 {
1121                   check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1122                                    $1, ACC_FINAL);
1123                   if ($1 != ACC_FINAL)
1124                     MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1125                 }
1126 ;
1127
1128 throws:
1129                 { $$ = NULL_TREE; }
1130 |       THROWS_TK class_type_list
1131                 { $$ = $2; }
1132 |       THROWS_TK error
1133                 {yyerror ("Missing class type term"); RECOVER;}
1134 ;
1135
1136 class_type_list:
1137         class_type
1138                 { $$ = build_tree_list ($1, $1); }
1139 |       class_type_list C_TK class_type
1140                 { $$ = tree_cons ($3, $3, $1); }
1141 |       class_type_list C_TK error
1142                 {yyerror ("Missing class type term"); RECOVER;}
1143 ;
1144
1145 method_body:
1146         block
1147 |       SC_TK { $$ = NULL_TREE; }
1148 ;
1149
1150 /* 19.8.4 Productions from 8.5: Static Initializers  */
1151 static_initializer:
1152         static block
1153                 {
1154                   TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1155                   SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1156                   current_static_block = NULL_TREE;
1157                 }
1158 ;
1159
1160 static:                         /* Test lval.sub_token here */
1161         modifiers
1162                 {
1163                   check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1164                   /* Can't have a static initializer in an innerclass */
1165                   if ($1 | ACC_STATIC &&
1166                       GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1167                     parse_error_context
1168                       (MODIFIER_WFL (STATIC_TK),
1169                        "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1170                        IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1171                   SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1172                 }
1173 ;
1174
1175 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1176 constructor_declaration:
1177         constructor_header
1178                 {
1179                   current_function_decl = $1;
1180                   source_start_java_method (current_function_decl);
1181                 }
1182         constructor_body
1183                 { finish_method_declaration ($3); }
1184 ;
1185
1186 constructor_header:
1187         constructor_declarator throws
1188                 { $$ = method_header (0, NULL_TREE, $1, $2); }
1189 |       modifiers constructor_declarator throws
1190                 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1191 ;
1192
1193 constructor_declarator:
1194         simple_name OP_TK CP_TK
1195                 {
1196                   ctxp->formal_parameter_number = 0;
1197                   $$ = method_declarator ($1, NULL_TREE);
1198                 }
1199 |       simple_name OP_TK formal_parameter_list CP_TK
1200                 { $$ = method_declarator ($1, $3); }
1201 ;
1202
1203 constructor_body:
1204         /* Unlike regular method, we always need a complete (empty)
1205            body so we can safely perform all the required code
1206            addition (super invocation and field initialization) */
1207         block_begin constructor_block_end
1208                 {
1209                   BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1210                   $$ = $2;
1211                 }
1212 |       block_begin explicit_constructor_invocation constructor_block_end
1213                 { $$ = $3; }
1214 |       block_begin block_statements constructor_block_end
1215                 { $$ = $3; }
1216 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1217                 { $$ = $4; }
1218 ;
1219
1220 constructor_block_end:
1221         block_end
1222 ;
1223
1224 /* Error recovery for that rule moved down expression_statement: rule.  */
1225 explicit_constructor_invocation:
1226         this_or_super OP_TK CP_TK SC_TK
1227                 {
1228                   $$ = build_method_invocation ($1, NULL_TREE);
1229                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1230                   $$ = java_method_add_stmt (current_function_decl, $$);
1231                 }
1232 |       this_or_super OP_TK argument_list CP_TK SC_TK
1233                 {
1234                   $$ = build_method_invocation ($1, $3);
1235                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1236                   $$ = java_method_add_stmt (current_function_decl, $$);
1237                 }
1238         /* Added, JDK1.1 inner classes. Modified because the rule
1239            'primary' couldn't work.  */
1240 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1241                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1242 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1243                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1244 ;
1245
1246 this_or_super:                  /* Added, simplifies error diagnostics */
1247         THIS_TK
1248                 {
1249                   tree wfl = build_wfl_node (this_identifier_node);
1250                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1251                   $$ = wfl;
1252                 }
1253 |       SUPER_TK
1254                 {
1255                   tree wfl = build_wfl_node (super_identifier_node);
1256                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1257                   $$ = wfl;
1258                 }
1259 ;
1260
1261 /* 19.9 Productions from 9: Interfaces  */
1262 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1263 interface_declaration:
1264         INTERFACE_TK identifier
1265                 { create_interface (0, $2, NULL_TREE); }
1266         interface_body
1267                 { ; }
1268 |       modifiers INTERFACE_TK identifier
1269                 { create_interface ($1, $3, NULL_TREE); }
1270         interface_body
1271                 { ; }
1272 |       INTERFACE_TK identifier extends_interfaces
1273                 { create_interface (0, $2, $3); }
1274         interface_body
1275                 { ; }
1276 |       modifiers INTERFACE_TK identifier extends_interfaces
1277                 { create_interface ($1, $3, $4); }
1278         interface_body
1279                 { ; }
1280 |       INTERFACE_TK identifier error
1281                 { yyerror ("'{' expected"); RECOVER; }
1282 |       modifiers INTERFACE_TK identifier error
1283                 { yyerror ("'{' expected"); RECOVER; }
1284 ;
1285
1286 extends_interfaces:
1287         EXTENDS_TK interface_type
1288                 {
1289                   ctxp->interface_number = 1;
1290                   $$ = build_tree_list ($2, NULL_TREE);
1291                 }
1292 |       extends_interfaces C_TK interface_type
1293                 {
1294                   ctxp->interface_number++;
1295                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1296                 }
1297 |       EXTENDS_TK error
1298                 {yyerror ("Invalid interface type"); RECOVER;}
1299 |       extends_interfaces C_TK error
1300                 {yyerror ("Missing term"); RECOVER;}
1301 ;
1302
1303 interface_body:
1304         OCB_TK CCB_TK
1305                 { $$ = NULL_TREE; }
1306 |       OCB_TK interface_member_declarations CCB_TK
1307                 { $$ = NULL_TREE; }
1308 ;
1309
1310 interface_member_declarations:
1311         interface_member_declaration
1312 |       interface_member_declarations interface_member_declaration
1313 ;
1314
1315 interface_member_declaration:
1316         constant_declaration
1317 |       abstract_method_declaration
1318 |       class_declaration       /* Added, JDK1.1 inner classes */
1319                 { end_class_declaration (1); }
1320 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
1321                 { end_class_declaration (1); }
1322 ;
1323
1324 constant_declaration:
1325         field_declaration
1326 ;
1327
1328 abstract_method_declaration:
1329         method_header SC_TK
1330                 {
1331                   check_abstract_method_header ($1);
1332                   current_function_decl = NULL_TREE; /* FIXME ? */
1333                 }
1334 |       method_header error
1335                 {yyerror ("';' expected"); RECOVER;}
1336 ;
1337
1338 /* 19.10 Productions from 10: Arrays  */
1339 array_initializer:
1340         OCB_TK CCB_TK
1341                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1342 |       OCB_TK C_TK CCB_TK
1343                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1344 |       OCB_TK variable_initializers CCB_TK
1345                 { $$ = build_new_array_init ($1.location, $2); }
1346 |       OCB_TK variable_initializers C_TK CCB_TK
1347                 { $$ = build_new_array_init ($1.location, $2); }
1348 ;
1349
1350 variable_initializers:
1351         variable_initializer
1352                 {
1353                   $$ = tree_cons (maybe_build_array_element_wfl ($1),
1354                                   $1, NULL_TREE);
1355                 }
1356 |       variable_initializers C_TK variable_initializer
1357                 {
1358                   $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1359                 }
1360 |       variable_initializers C_TK error
1361                 {yyerror ("Missing term"); RECOVER;}
1362 ;
1363
1364 /* 19.11 Production from 14: Blocks and Statements  */
1365 block:
1366         block_begin block_end
1367                 { $$ = $2; }
1368 |       block_begin block_statements block_end
1369                 { $$ = $3; }
1370 ;
1371
1372 block_begin:
1373         OCB_TK
1374                 { enter_block (); }
1375 ;
1376
1377 block_end:
1378         CCB_TK
1379                 {
1380                   maybe_absorb_scoping_blocks ();
1381                   $$ = exit_block ();
1382                   if (!BLOCK_SUBBLOCKS ($$))
1383                     BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1384                 }
1385 ;
1386
1387 block_statements:
1388         block_statement
1389 |       block_statements block_statement
1390 ;
1391
1392 block_statement:
1393         local_variable_declaration_statement
1394 |       statement
1395                 { java_method_add_stmt (current_function_decl, $1); }
1396 |       class_declaration       /* Added, JDK1.1 local classes */
1397                 {
1398                   LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1399                   end_class_declaration (1);
1400                 }
1401 ;
1402
1403 local_variable_declaration_statement:
1404         local_variable_declaration SC_TK /* Can't catch missing ';' here */
1405 ;
1406
1407 local_variable_declaration:
1408         type variable_declarators
1409                 { declare_local_variables (0, $1, $2); }
1410 |       final type variable_declarators /* Added, JDK1.1 final locals */
1411                 { declare_local_variables ($1, $2, $3); }
1412 ;
1413
1414 statement:
1415         statement_without_trailing_substatement
1416 |       labeled_statement
1417 |       if_then_statement
1418 |       if_then_else_statement
1419 |       while_statement
1420 |       for_statement
1421                 { $$ = exit_block (); }
1422 ;
1423
1424 statement_nsi:
1425         statement_without_trailing_substatement
1426 |       labeled_statement_nsi
1427 |       if_then_else_statement_nsi
1428 |       while_statement_nsi
1429 |       for_statement_nsi
1430                 { $$ = exit_block (); }
1431 ;
1432
1433 statement_without_trailing_substatement:
1434         block
1435 |       empty_statement
1436 |       expression_statement
1437 |       switch_statement
1438 |       do_statement
1439 |       break_statement
1440 |       continue_statement
1441 |       return_statement
1442 |       synchronized_statement
1443 |       throw_statement
1444 |       try_statement
1445 |       assert_statement
1446 ;
1447
1448 empty_statement:
1449         SC_TK
1450                 {
1451                   if (flag_extraneous_semicolon
1452                       && ! current_static_block
1453                       && (! current_function_decl ||
1454                           /* Verify we're not in a inner class declaration */
1455                           (GET_CPC () != TYPE_NAME
1456                            (DECL_CONTEXT (current_function_decl)))))
1457
1458                     {
1459 #ifdef USE_MAPPED_LOCATION
1460                       SET_EXPR_LOCATION (wfl_operator, input_location);
1461 #else
1462                       EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1463 #endif
1464                       parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1465                     }
1466                   $$ = build_java_empty_stmt ();
1467                 }
1468 ;
1469
1470 label_decl:
1471         identifier REL_CL_TK
1472                 {
1473                   $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1474                                             EXPR_WFL_NODE ($1));
1475                   pushlevel (2);
1476                   push_labeled_block ($$);
1477                   PUSH_LABELED_BLOCK ($$);
1478                 }
1479 ;
1480
1481 labeled_statement:
1482         label_decl statement
1483                 { $$ = finish_labeled_statement ($1, $2); }
1484 |       identifier error
1485                 {yyerror ("':' expected"); RECOVER;}
1486 ;
1487
1488 labeled_statement_nsi:
1489         label_decl statement_nsi
1490                 { $$ = finish_labeled_statement ($1, $2); }
1491 ;
1492
1493 /* We concentrate here a bunch of error handling rules that we couldn't write
1494    earlier, because expression_statement catches a missing ';'.  */
1495 expression_statement:
1496         statement_expression SC_TK
1497                 {
1498                   /* We have a statement. Generate a WFL around it so
1499                      we can debug it */
1500 #ifdef USE_MAPPED_LOCATION
1501                   $$ = expr_add_location ($1, input_location, 1);
1502 #else
1503                   $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1504                   JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1505 #endif
1506                   /* We know we have a statement, so set the debug
1507                      info to be eventually generate here. */
1508                 }
1509 |       error SC_TK
1510                 {
1511                   YYNOT_TWICE yyerror ("Invalid expression statement");
1512                   DRECOVER (expr_stmt);
1513                 }
1514 |       error OCB_TK
1515                 {
1516                   YYNOT_TWICE yyerror ("Invalid expression statement");
1517                   DRECOVER (expr_stmt);
1518                 }
1519 |       error CCB_TK
1520                 {
1521                   YYNOT_TWICE yyerror ("Invalid expression statement");
1522                   DRECOVER (expr_stmt);
1523                 }
1524 |       this_or_super OP_TK error
1525                 {yyerror ("')' expected"); RECOVER;}
1526 |       this_or_super OP_TK CP_TK error
1527                 {
1528                   parse_ctor_invocation_error ();
1529                   RECOVER;
1530                 }
1531 |       this_or_super OP_TK argument_list error
1532                 {yyerror ("')' expected"); RECOVER;}
1533 |       this_or_super OP_TK argument_list CP_TK error
1534                 {
1535                   parse_ctor_invocation_error ();
1536                   RECOVER;
1537                 }
1538 |       name DOT_TK SUPER_TK error
1539                 {yyerror ("'(' expected"); RECOVER;}
1540 |       name DOT_TK SUPER_TK OP_TK error
1541                 {yyerror ("')' expected"); RECOVER;}
1542 |       name DOT_TK SUPER_TK OP_TK argument_list error
1543                 {yyerror ("')' expected"); RECOVER;}
1544 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1545                 {yyerror ("';' expected"); RECOVER;}
1546 |       name DOT_TK SUPER_TK OP_TK CP_TK error
1547                 {yyerror ("';' expected"); RECOVER;}
1548 ;
1549
1550 statement_expression:
1551         assignment
1552 |       pre_increment_expression
1553 |       pre_decrement_expression
1554 |       post_increment_expression
1555 |       post_decrement_expression
1556 |       method_invocation
1557 |       class_instance_creation_expression
1558 ;
1559
1560 if_then_statement:
1561         IF_TK OP_TK expression CP_TK statement
1562                 {
1563                   $$ = build_if_else_statement ($2.location, $3,
1564                                                 $5, NULL_TREE);
1565                 }
1566 |       IF_TK error
1567                 {yyerror ("'(' expected"); RECOVER;}
1568 |       IF_TK OP_TK error
1569                 {yyerror ("Missing term"); RECOVER;}
1570 |       IF_TK OP_TK expression error
1571                 {yyerror ("')' expected"); RECOVER;}
1572 ;
1573
1574 if_then_else_statement:
1575         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1576                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1577 ;
1578
1579 if_then_else_statement_nsi:
1580         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1581                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1582 ;
1583
1584 switch_statement:
1585         switch_expression
1586                 {
1587                   enter_block ();
1588                 }
1589         switch_block
1590                 {
1591                   /* Make into "proper list" of COMPOUND_EXPRs.
1592                      I.e. make the last statement also have its own
1593                      COMPOUND_EXPR. */
1594                   maybe_absorb_scoping_blocks ();
1595                   TREE_OPERAND ($1, 1) = exit_block ();
1596                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1597                 }
1598 ;
1599
1600 switch_expression:
1601         SWITCH_TK OP_TK expression CP_TK
1602                 {
1603                   $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1604                                NULL_TREE, NULL_TREE);
1605                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1606                 }
1607 |       SWITCH_TK error
1608                 {yyerror ("'(' expected"); RECOVER;}
1609 |       SWITCH_TK OP_TK error
1610                 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1611 |       SWITCH_TK OP_TK expression CP_TK error
1612                 {yyerror ("'{' expected"); RECOVER;}
1613 ;
1614
1615 /* Default assignment is there to avoid type node on switch_block
1616    node. */
1617
1618 switch_block:
1619         OCB_TK CCB_TK
1620                 { $$ = NULL_TREE; }
1621 |       OCB_TK switch_labels CCB_TK
1622                 { $$ = NULL_TREE; }
1623 |       OCB_TK switch_block_statement_groups CCB_TK
1624                 { $$ = NULL_TREE; }
1625 |       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1626                 { $$ = NULL_TREE; }
1627 ;
1628
1629 switch_block_statement_groups:
1630         switch_block_statement_group
1631 |       switch_block_statement_groups switch_block_statement_group
1632 ;
1633
1634 switch_block_statement_group:
1635         switch_labels block_statements
1636 ;
1637
1638 switch_labels:
1639         switch_label
1640 |       switch_labels switch_label
1641 ;
1642
1643 switch_label:
1644         CASE_TK constant_expression REL_CL_TK
1645                 {
1646                   tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1647                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1648                   java_method_add_stmt (current_function_decl, lab);
1649                 }
1650 |       DEFAULT_TK REL_CL_TK
1651                 {
1652                   tree lab = make_node (DEFAULT_EXPR);
1653                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1654                   java_method_add_stmt (current_function_decl, lab);
1655                 }
1656 |       CASE_TK error
1657                 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1658 |       CASE_TK constant_expression error
1659                 {yyerror ("':' expected"); RECOVER;}
1660 |       DEFAULT_TK error
1661                 {yyerror ("':' expected"); RECOVER;}
1662 ;
1663
1664 while_expression:
1665         WHILE_TK OP_TK expression CP_TK
1666                 {
1667                   tree body = build_loop_body ($2.location, $3, 0);
1668                   $$ = build_new_loop (body);
1669                 }
1670 ;
1671
1672 while_statement:
1673         while_expression statement
1674                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1675 |       WHILE_TK error
1676                 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1677 |       WHILE_TK OP_TK error
1678                 {yyerror ("Missing term and ')' expected"); RECOVER;}
1679 |       WHILE_TK OP_TK expression error
1680                 {yyerror ("')' expected"); RECOVER;}
1681 ;
1682
1683 while_statement_nsi:
1684         while_expression statement_nsi
1685                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1686 ;
1687
1688 do_statement_begin:
1689         DO_TK
1690                 {
1691                   tree body = build_loop_body (0, NULL_TREE, 1);
1692                   $$ = build_new_loop (body);
1693                 }
1694         /* Need error handing here. FIXME */
1695 ;
1696
1697 do_statement:
1698         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1699                 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1700 ;
1701
1702 for_statement:
1703         for_begin SC_TK expression SC_TK for_update CP_TK statement
1704                 {
1705                   if (CONSTANT_CLASS_P ($3))
1706                     $3 = build_wfl_node ($3);
1707                   $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1708                 }
1709 |       for_begin SC_TK SC_TK for_update CP_TK statement
1710                 {
1711                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1712                   /* We have not condition, so we get rid of the EXIT_EXPR */
1713                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1714                     build_java_empty_stmt ();
1715                 }
1716 |       for_begin SC_TK error
1717                 {yyerror ("Invalid control expression"); RECOVER;}
1718 |       for_begin SC_TK expression SC_TK error
1719                 {yyerror ("Invalid update expression"); RECOVER;}
1720 |       for_begin SC_TK SC_TK error
1721                 {yyerror ("Invalid update expression"); RECOVER;}
1722 ;
1723
1724 for_statement_nsi:
1725         for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1726                 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1727 |       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1728                 {
1729                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1730                   /* We have not condition, so we get rid of the EXIT_EXPR */
1731                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1732                     build_java_empty_stmt ();
1733                 }
1734 ;
1735
1736 for_header:
1737         FOR_TK OP_TK
1738                 {
1739                   /* This scope defined for local variable that may be
1740                      defined within the scope of the for loop */
1741                   enter_block ();
1742                 }
1743 |       FOR_TK error
1744                 {yyerror ("'(' expected"); DRECOVER(for_1);}
1745 |       FOR_TK OP_TK error
1746                 {yyerror ("Invalid init statement"); RECOVER;}
1747 ;
1748
1749 for_begin:
1750         for_header for_init
1751                 {
1752                   /* We now declare the loop body. The loop is
1753                      declared as a for loop. */
1754                   tree body = build_loop_body (0, NULL_TREE, 0);
1755                   $$ =  build_new_loop (body);
1756                   FOR_LOOP_P ($$) = 1;
1757                   /* The loop is added to the current block the for
1758                      statement is defined within */
1759                   java_method_add_stmt (current_function_decl, $$);
1760                 }
1761 ;
1762 for_init:                       /* Can be empty */
1763                 { $$ = build_java_empty_stmt (); }
1764 |       statement_expression_list
1765                 {
1766                   /* Init statement recorded within the previously
1767                      defined block scope */
1768                   $$ = java_method_add_stmt (current_function_decl, $1);
1769                 }
1770 |       local_variable_declaration
1771                 {
1772                   /* Local variable are recorded within the previously
1773                      defined block scope */
1774                   $$ = NULL_TREE;
1775                 }
1776 |       statement_expression_list error
1777                 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1778 ;
1779
1780 for_update:                     /* Can be empty */
1781                 {$$ = build_java_empty_stmt ();}
1782 |       statement_expression_list
1783                 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1784 ;
1785
1786 statement_expression_list:
1787         statement_expression
1788                 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1789 |       statement_expression_list C_TK statement_expression
1790                 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1791 |       statement_expression_list C_TK error
1792                 {yyerror ("Missing term"); RECOVER;}
1793 ;
1794
1795 break_statement:
1796         BREAK_TK SC_TK
1797                 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1798 |       BREAK_TK identifier SC_TK
1799                 { $$ = build_bc_statement ($1.location, 1, $2); }
1800 |       BREAK_TK error
1801                 {yyerror ("Missing term"); RECOVER;}
1802 |       BREAK_TK identifier error
1803                 {yyerror ("';' expected"); RECOVER;}
1804 ;
1805
1806 continue_statement:
1807         CONTINUE_TK SC_TK
1808                 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1809 |       CONTINUE_TK identifier SC_TK
1810                 { $$ = build_bc_statement ($1.location, 0, $2); }
1811 |       CONTINUE_TK error
1812                 {yyerror ("Missing term"); RECOVER;}
1813 |       CONTINUE_TK identifier error
1814                 {yyerror ("';' expected"); RECOVER;}
1815 ;
1816
1817 return_statement:
1818         RETURN_TK SC_TK
1819                 { $$ = build_return ($1.location, NULL_TREE); }
1820 |       RETURN_TK expression SC_TK
1821                 { $$ = build_return ($1.location, $2); }
1822 |       RETURN_TK error
1823                 {yyerror ("Missing term"); RECOVER;}
1824 |       RETURN_TK expression error
1825                 {yyerror ("';' expected"); RECOVER;}
1826 ;
1827
1828 throw_statement:
1829         THROW_TK expression SC_TK
1830                 {
1831                   $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1832                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1833                 }
1834 |       THROW_TK error
1835                 {yyerror ("Missing term"); RECOVER;}
1836 |       THROW_TK expression error
1837                 {yyerror ("';' expected"); RECOVER;}
1838 ;
1839
1840 assert_statement:
1841         ASSERT_TK expression REL_CL_TK expression SC_TK
1842                 {
1843                   $$ = build_assertion ($1.location, $2, $4);
1844                 }
1845 |       ASSERT_TK expression SC_TK
1846                 {
1847                   $$ = build_assertion ($1.location, $2, NULL_TREE);
1848                 }
1849 |       ASSERT_TK error
1850                 {yyerror ("Missing term"); RECOVER;}
1851 |       ASSERT_TK expression error
1852                 {yyerror ("';' expected"); RECOVER;}
1853 ;
1854
1855 synchronized_statement:
1856         synchronized OP_TK expression CP_TK block
1857                 {
1858                   $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1859                   EXPR_WFL_LINECOL ($$) =
1860                     EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1861                 }
1862 |       synchronized OP_TK expression CP_TK error
1863                 {yyerror ("'{' expected"); RECOVER;}
1864 |       synchronized error
1865                 {yyerror ("'(' expected"); RECOVER;}
1866 |       synchronized OP_TK error CP_TK
1867                 {yyerror ("Missing term"); RECOVER;}
1868 |       synchronized OP_TK error
1869                 {yyerror ("Missing term"); RECOVER;}
1870 ;
1871
1872 synchronized:
1873         modifiers
1874                 {
1875                   check_modifiers (
1876              "Illegal modifier %qs. Only %<synchronized%> was expected here",
1877                                    $1, ACC_SYNCHRONIZED);
1878                   if ($1 != ACC_SYNCHRONIZED)
1879                     MODIFIER_WFL (SYNCHRONIZED_TK) =
1880                       build_wfl_node (NULL_TREE);
1881                 }
1882 ;
1883
1884 try_statement:
1885         TRY_TK block catches
1886                 { $$ = build_try_statement ($1.location, $2, $3); }
1887 |       TRY_TK block finally
1888                 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1889 |       TRY_TK block catches finally
1890                 { $$ = build_try_finally_statement
1891                     ($1.location, build_try_statement ($1.location,
1892                                                        $2, $3), $4);
1893                 }
1894 |       TRY_TK error
1895                 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1896 ;
1897
1898 catches:
1899         catch_clause
1900 |       catches catch_clause
1901                 {
1902                   TREE_CHAIN ($2) = $1;
1903                   $$ = $2;
1904                 }
1905 ;
1906
1907 catch_clause:
1908         catch_clause_parameter block
1909                 {
1910                   java_method_add_stmt (current_function_decl, $2);
1911                   exit_block ();
1912                   $$ = $1;
1913                 }
1914 ;
1915
1916 catch_clause_parameter:
1917         CATCH_TK OP_TK formal_parameter CP_TK
1918                 {
1919                   /* We add a block to define a scope for
1920                      formal_parameter (CCBP). The formal parameter is
1921                      declared initialized by the appropriate function
1922                      call */
1923                   tree ccpb;
1924                   tree init;
1925                   if ($3)
1926                     {
1927                       ccpb = enter_block ();
1928                       init = build_assignment
1929                         (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1930                          build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1931                       declare_local_variables (0, TREE_VALUE ($3),
1932                                                build_tree_list 
1933                                                (TREE_PURPOSE ($3), init));
1934                       $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1935                       SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1936                     }
1937                   else
1938                     {
1939                       $$ = error_mark_node;
1940                     }
1941                 }
1942 |       CATCH_TK error
1943                 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1944 |       CATCH_TK OP_TK error
1945                 {
1946                   yyerror ("Missing term or ')' expected");
1947                   RECOVER; $$ = NULL_TREE;
1948                 }
1949 |       CATCH_TK OP_TK error CP_TK /* That's for () */
1950                 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1951 ;
1952
1953 finally:
1954         FINALLY_TK block
1955                 { $$ = $2; }
1956 |       FINALLY_TK error
1957                 {yyerror ("'{' expected"); RECOVER; }
1958 ;
1959
1960 /* 19.12 Production from 15: Expressions  */
1961 primary:
1962         primary_no_new_array
1963 |       array_creation_uninitialized
1964 |       array_creation_initialized
1965 ;
1966
1967 primary_no_new_array:
1968         literal
1969 |       THIS_TK
1970                 { $$ = build_this ($1.location); }
1971 |       OP_TK expression CP_TK
1972                 {$$ = $2;}
1973 |       class_instance_creation_expression
1974 |       field_access
1975 |       method_invocation
1976 |       array_access
1977 |       type_literals
1978         /* Added, JDK1.1 inner classes. Documentation is wrong
1979            referring to a 'ClassName' (class_name) rule that doesn't
1980            exist. Used name: instead.  */
1981 |       name DOT_TK THIS_TK
1982                 {
1983                   tree wfl = build_wfl_node (this_identifier_node);
1984                   $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1985                 }
1986 |       OP_TK expression error
1987                 {yyerror ("')' expected"); RECOVER;}
1988 |       name DOT_TK error
1989                 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1990 |       primitive_type DOT_TK error
1991                 {yyerror ("'class' expected" ); RECOVER;}
1992 |       VOID_TK DOT_TK error
1993                 {yyerror ("'class' expected" ); RECOVER;}
1994 ;
1995
1996 type_literals:
1997         name DOT_TK CLASS_TK
1998                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1999 |       array_type DOT_TK CLASS_TK
2000                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2001 |       primitive_type DOT_TK CLASS_TK
2002                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2003 |       VOID_TK DOT_TK CLASS_TK
2004                 {
2005                    $$ = build_incomplete_class_ref ($2.location,
2006                                                    void_type_node);
2007                 }
2008 ;
2009
2010 class_instance_creation_expression:
2011         NEW_TK class_type OP_TK argument_list CP_TK
2012                 { $$ = build_new_invocation ($2, $4); }
2013 |       NEW_TK class_type OP_TK CP_TK
2014                 { $$ = build_new_invocation ($2, NULL_TREE); }
2015 |       anonymous_class_creation
2016         /* Added, JDK1.1 inner classes, modified to use name or
2017            primary instead of primary solely which couldn't work in
2018            all situations.  */
2019 |       something_dot_new identifier OP_TK CP_TK
2020                 {
2021                   tree ctor = build_new_invocation ($2, NULL_TREE);
2022                   $$ = make_qualified_primary ($1, ctor,
2023                                                EXPR_WFL_LINECOL ($1));
2024                 }
2025 |       something_dot_new identifier OP_TK CP_TK class_body
2026 |       something_dot_new identifier OP_TK argument_list CP_TK
2027                 {
2028                   tree ctor = build_new_invocation ($2, $4);
2029                   $$ = make_qualified_primary ($1, ctor,
2030                                                EXPR_WFL_LINECOL ($1));
2031                 }
2032 |       something_dot_new identifier OP_TK argument_list CP_TK class_body
2033 |       NEW_TK error SC_TK
2034                 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2035 |       NEW_TK class_type error
2036                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2037 |       NEW_TK class_type OP_TK error
2038                 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2039 |       NEW_TK class_type OP_TK argument_list error
2040                 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2041 |       something_dot_new error
2042                 {
2043                   $$ = NULL_TREE;
2044                   YYERROR_NOW;
2045                   yyerror ("Identifier expected");
2046                   RECOVER;
2047                 }
2048 |       something_dot_new identifier error
2049                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2050 ;
2051
2052 /* Created after JDK1.1 rules originally added to
2053    class_instance_creation_expression, but modified to use
2054    'class_type' instead of 'TypeName' (type_name) which is mentioned
2055    in the documentation but doesn't exist. */
2056
2057 anonymous_class_creation:
2058         NEW_TK class_type OP_TK argument_list CP_TK
2059                 { create_anonymous_class ($2); }
2060         class_body
2061                 {
2062                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2063                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2064
2065                   end_class_declaration (1);
2066
2067                   /* Now we can craft the new expression */
2068                   $$ = build_new_invocation (id, $4);
2069
2070                   /* Note that we can't possibly be here if
2071                      `class_type' is an interface (in which case the
2072                      anonymous class extends Object and implements
2073                      `class_type', hence its constructor can't have
2074                      arguments.) */
2075
2076                   /* Otherwise, the innerclass must feature a
2077                      constructor matching `argument_list'. Anonymous
2078                      classes are a bit special: it's impossible to
2079                      define constructor for them, hence constructors
2080                      must be generated following the hints provided by
2081                      the `new' expression. Whether a super constructor
2082                      of that nature exists or not is to be verified
2083                      later on in verify_constructor_super.
2084
2085                      It's during the expansion of a `new' statement
2086                      referring to an anonymous class that a ctor will
2087                      be generated for the anonymous class, with the
2088                      right arguments. */
2089
2090                 }
2091 |       NEW_TK class_type OP_TK CP_TK
2092                 { create_anonymous_class ($2); }
2093         class_body
2094                 {
2095                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2096                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2097
2098                   end_class_declaration (1);
2099
2100                   /* Now we can craft the new expression. The
2101                      statement doesn't need to be remember so that a
2102                      constructor can be generated, since its signature
2103                      is already known. */
2104                   $$ = build_new_invocation (id, NULL_TREE);
2105                 }
2106 ;
2107
2108 something_dot_new:              /* Added, not part of the specs. */
2109         name DOT_TK NEW_TK
2110                 { $$ = $1; }
2111 |       primary DOT_TK NEW_TK
2112                 { $$ = $1; }
2113 ;
2114
2115 argument_list:
2116         expression
2117                 {
2118                   $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2119                   ctxp->formal_parameter_number = 1;
2120                 }
2121 |       argument_list C_TK expression
2122                 {
2123                   ctxp->formal_parameter_number += 1;
2124                   $$ = tree_cons (NULL_TREE, $3, $1);
2125                 }
2126 |       argument_list C_TK error
2127                 {yyerror ("Missing term"); RECOVER;}
2128 ;
2129
2130 array_creation_uninitialized:
2131         NEW_TK primitive_type dim_exprs
2132                 { $$ = build_newarray_node ($2, $3, 0); }
2133 |       NEW_TK class_or_interface_type dim_exprs
2134                 { $$ = build_newarray_node ($2, $3, 0); }
2135 |       NEW_TK primitive_type dim_exprs dims
2136                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2137 |       NEW_TK class_or_interface_type dim_exprs dims
2138                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2139 |       NEW_TK error CSB_TK
2140                 {yyerror ("'[' expected"); DRECOVER ("]");}
2141 |       NEW_TK error OSB_TK
2142                 {yyerror ("']' expected"); RECOVER;}
2143 ;
2144
2145 array_creation_initialized:
2146         /* Added, JDK1.1 anonymous array. Initial documentation rule
2147            modified */
2148         NEW_TK class_or_interface_type dims array_initializer
2149                 {
2150                   char *sig;
2151                   int osb = pop_current_osb (ctxp);
2152                   while (osb--)
2153                     obstack_grow (&temporary_obstack, "[]", 2);
2154                   obstack_1grow (&temporary_obstack, '\0');
2155                   sig = obstack_finish (&temporary_obstack);
2156                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2157                                $2, get_identifier (sig), $4);
2158                 }
2159 |       NEW_TK primitive_type dims array_initializer
2160                 {
2161                   int osb = pop_current_osb (ctxp);
2162                   tree type = $2;
2163                   while (osb--)
2164                     type = build_java_array_type (type, -1);
2165                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2166                                build_pointer_type (type), NULL_TREE, $4);
2167                 }
2168 |       NEW_TK error CSB_TK
2169                 {yyerror ("'[' expected"); DRECOVER ("]");}
2170 |       NEW_TK error OSB_TK
2171                 {yyerror ("']' expected"); RECOVER;}
2172 ;
2173
2174 dim_exprs:
2175         dim_expr
2176                 { $$ = build_tree_list (NULL_TREE, $1); }
2177 |       dim_exprs dim_expr
2178                 { $$ = tree_cons (NULL_TREE, $2, $$); }
2179 ;
2180
2181 dim_expr:
2182         OSB_TK expression CSB_TK
2183                 {
2184                   if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2185                     {
2186                       $2 = build_wfl_node ($2);
2187                       TREE_TYPE ($2) = NULL_TREE;
2188                     }
2189                   EXPR_WFL_LINECOL ($2) = $1.location;
2190                   $$ = $2;
2191                 }
2192 |       OSB_TK expression error
2193                 {yyerror ("']' expected"); RECOVER;}
2194 |       OSB_TK error
2195                 {
2196                   yyerror ("Missing term");
2197                   yyerror ("']' expected");
2198                   RECOVER;
2199                 }
2200 ;
2201
2202 dims:
2203         OSB_TK CSB_TK
2204                 {
2205                   int allocate = 0;
2206                   /* If not initialized, allocate memory for the osb
2207                      numbers stack */
2208                   if (!ctxp->osb_limit)
2209                     {
2210                       allocate = ctxp->osb_limit = 32;
2211                       ctxp->osb_depth = -1;
2212                     }
2213                   /* If capacity overflown, reallocate a bigger chunk */
2214                   else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2215                     allocate = ctxp->osb_limit << 1;
2216
2217                   if (allocate)
2218                     {
2219                       allocate *= sizeof (int);
2220                       if (ctxp->osb_number)
2221                         ctxp->osb_number = xrealloc (ctxp->osb_number,
2222                                                      allocate);
2223                       else
2224                         ctxp->osb_number = xmalloc (allocate);
2225                     }
2226                   ctxp->osb_depth++;
2227                   CURRENT_OSB (ctxp) = 1;
2228                 }
2229 |       dims OSB_TK CSB_TK
2230                 { CURRENT_OSB (ctxp)++; }
2231 |       dims OSB_TK error
2232                 { yyerror ("']' expected"); RECOVER;}
2233 ;
2234
2235 field_access:
2236         primary DOT_TK identifier
2237                 { $$ = make_qualified_primary ($1, $3, $2.location); }
2238                 /*  FIXME - REWRITE TO:
2239                 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2240 |       SUPER_TK DOT_TK identifier
2241                 {
2242                   tree super_wfl = build_wfl_node (super_identifier_node);
2243                   SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2244                   $$ = make_qualified_name (super_wfl, $3, $2.location);
2245                 }
2246 |       SUPER_TK error
2247                 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2248 ;
2249
2250 method_invocation:
2251         name OP_TK CP_TK
2252                 { $$ = build_method_invocation ($1, NULL_TREE); }
2253 |       name OP_TK argument_list CP_TK
2254                 { $$ = build_method_invocation ($1, $3); }
2255 |       primary DOT_TK identifier OP_TK CP_TK
2256                 {
2257                   if (TREE_CODE ($1) == THIS_EXPR)
2258                     $$ = build_this_super_qualified_invocation
2259                       (1, $3, NULL_TREE, 0, $2.location);
2260                   else
2261                     {
2262                       tree invok = build_method_invocation ($3, NULL_TREE);
2263                       $$ = make_qualified_primary ($1, invok, $2.location);
2264                     }
2265                 }
2266 |       primary DOT_TK identifier OP_TK argument_list CP_TK
2267                 {
2268                   if (TREE_CODE ($1) == THIS_EXPR)
2269                     $$ = build_this_super_qualified_invocation
2270                       (1, $3, $5, 0, $2.location);
2271                   else
2272                     {
2273                       tree invok = build_method_invocation ($3, $5);
2274                       $$ = make_qualified_primary ($1, invok, $2.location);
2275                     }
2276                 }
2277 |       SUPER_TK DOT_TK identifier OP_TK CP_TK
2278                 {
2279                   $$ = build_this_super_qualified_invocation
2280                     (0, $3, NULL_TREE, $1.location, $2.location);
2281                 }
2282 |       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2283                 {
2284                   $$ = build_this_super_qualified_invocation
2285                     (0, $3, $5, $1.location, $2.location);
2286                 }
2287         /* Screws up thing. I let it here until I'm convinced it can
2288            be removed. FIXME
2289 |       primary DOT_TK error
2290                 {yyerror ("'(' expected"); DRECOVER(bad);} */
2291 |       SUPER_TK DOT_TK error CP_TK
2292                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2293 |       SUPER_TK DOT_TK error DOT_TK
2294                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2295 ;
2296
2297 array_access:
2298         name OSB_TK expression CSB_TK
2299                 { $$ = build_array_ref ($2.location, $1, $3); }
2300 |       primary_no_new_array OSB_TK expression CSB_TK
2301                 { $$ = build_array_ref ($2.location, $1, $3); }
2302 |       array_creation_initialized OSB_TK expression CSB_TK
2303                 { $$ = build_array_ref ($2.location, $1, $3); }
2304 |       name OSB_TK error
2305                 {
2306                   yyerror ("Missing term and ']' expected");
2307                   DRECOVER(array_access);
2308                 }
2309 |       name OSB_TK expression error
2310                 {
2311                   yyerror ("']' expected");
2312                   DRECOVER(array_access);
2313                 }
2314 |       primary_no_new_array OSB_TK error
2315                 {
2316                   yyerror ("Missing term and ']' expected");
2317                   DRECOVER(array_access);
2318                 }
2319 |       primary_no_new_array OSB_TK expression error
2320                 {
2321                   yyerror ("']' expected");
2322                   DRECOVER(array_access);
2323                 }
2324 |       array_creation_initialized OSB_TK error
2325                 {
2326                   yyerror ("Missing term and ']' expected");
2327                   DRECOVER(array_access);
2328                 }
2329 |       array_creation_initialized OSB_TK expression error
2330                 {
2331                   yyerror ("']' expected");
2332                   DRECOVER(array_access);
2333                 }
2334 ;
2335
2336 postfix_expression:
2337         primary
2338 |       name
2339 |       post_increment_expression
2340 |       post_decrement_expression
2341 ;
2342
2343 post_increment_expression:
2344         postfix_expression INCR_TK
2345                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2346 ;
2347
2348 post_decrement_expression:
2349         postfix_expression DECR_TK
2350                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2351 ;
2352
2353 trap_overflow_corner_case:
2354         pre_increment_expression
2355 |       pre_decrement_expression
2356 |       PLUS_TK unary_expression
2357                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2358 |       unary_expression_not_plus_minus
2359 |       PLUS_TK error
2360                 {yyerror ("Missing term"); RECOVER}
2361 ;
2362
2363 unary_expression:
2364         trap_overflow_corner_case
2365                 {
2366                   if ($1)
2367                     error_if_numeric_overflow ($1);
2368                   $$ = $1;
2369                 }
2370 |       MINUS_TK trap_overflow_corner_case
2371                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2372 |       MINUS_TK error
2373                 {yyerror ("Missing term"); RECOVER}
2374 ;
2375
2376 pre_increment_expression:
2377         INCR_TK unary_expression
2378                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2379 |       INCR_TK error
2380                 {yyerror ("Missing term"); RECOVER}
2381 ;
2382
2383 pre_decrement_expression:
2384         DECR_TK unary_expression
2385                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2386 |       DECR_TK error
2387                 {yyerror ("Missing term"); RECOVER}
2388 ;
2389
2390 unary_expression_not_plus_minus:
2391         postfix_expression
2392 |       NOT_TK unary_expression
2393                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2394 |       NEG_TK unary_expression
2395                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2396 |       cast_expression
2397 |       NOT_TK error
2398                 {yyerror ("Missing term"); RECOVER}
2399 |       NEG_TK error
2400                 {yyerror ("Missing term"); RECOVER}
2401 ;
2402
2403 cast_expression:                /* Error handling here is potentially weak */
2404         OP_TK primitive_type dims CP_TK unary_expression
2405                 {
2406                   tree type = $2;
2407                   int osb = pop_current_osb (ctxp);
2408                   while (osb--)
2409                     type = build_java_array_type (type, -1);
2410                   $$ = build_cast ($1.location, type, $5);
2411                 }
2412 |       OP_TK primitive_type CP_TK unary_expression
2413                 { $$ = build_cast ($1.location, $2, $4); }
2414 |       OP_TK expression CP_TK unary_expression_not_plus_minus
2415                 { $$ = build_cast ($1.location, $2, $4); }
2416 |       OP_TK name dims CP_TK unary_expression_not_plus_minus
2417                 {
2418                   const char *ptr;
2419                   int osb = pop_current_osb (ctxp);
2420                   obstack_grow (&temporary_obstack,
2421                                 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2422                                 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2423                   while (osb--)
2424                     obstack_grow (&temporary_obstack, "[]", 2);
2425                   obstack_1grow (&temporary_obstack, '\0');
2426                   ptr = obstack_finish (&temporary_obstack);
2427                   EXPR_WFL_NODE ($2) = get_identifier (ptr);
2428                   $$ = build_cast ($1.location, $2, $5);
2429                 }
2430 |       OP_TK primitive_type OSB_TK error
2431                 {yyerror ("']' expected, invalid type expression");}
2432 |       OP_TK error
2433                 {
2434                   YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2435                   RECOVER;
2436                 }
2437 |       OP_TK primitive_type dims CP_TK error
2438                 {yyerror ("Missing term"); RECOVER;}
2439 |       OP_TK primitive_type CP_TK error
2440                 {yyerror ("Missing term"); RECOVER;}
2441 |       OP_TK name dims CP_TK error
2442                 {yyerror ("Missing term"); RECOVER;}
2443 ;
2444
2445 multiplicative_expression:
2446         unary_expression
2447 |       multiplicative_expression MULT_TK unary_expression
2448                 {
2449                   $$ = build_binop (BINOP_LOOKUP ($2.token),
2450                                     $2.location, $1, $3);
2451                 }
2452 |       multiplicative_expression DIV_TK unary_expression
2453                 {
2454                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2455                                     $1, $3);
2456                 }
2457 |       multiplicative_expression REM_TK unary_expression
2458                 {
2459                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2460                                     $1, $3);
2461                 }
2462 |       multiplicative_expression MULT_TK error
2463                 {yyerror ("Missing term"); RECOVER;}
2464 |       multiplicative_expression DIV_TK error
2465                 {yyerror ("Missing term"); RECOVER;}
2466 |       multiplicative_expression REM_TK error
2467                 {yyerror ("Missing term"); RECOVER;}
2468 ;
2469
2470 additive_expression:
2471         multiplicative_expression
2472 |       additive_expression PLUS_TK multiplicative_expression
2473                 {
2474                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2475                                     $1, $3);
2476                 }
2477 |       additive_expression MINUS_TK multiplicative_expression
2478                 {
2479                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2480                                     $1, $3);
2481                 }
2482 |       additive_expression PLUS_TK error
2483                 {yyerror ("Missing term"); RECOVER;}
2484 |       additive_expression MINUS_TK error
2485                 {yyerror ("Missing term"); RECOVER;}
2486 ;
2487
2488 shift_expression:
2489         additive_expression
2490 |       shift_expression LS_TK additive_expression
2491                 {
2492                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2493                                     $1, $3);
2494                 }
2495 |       shift_expression SRS_TK additive_expression
2496                 {
2497                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2498                                     $1, $3);
2499                 }
2500 |       shift_expression ZRS_TK additive_expression
2501                 {
2502                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2503                                     $1, $3);
2504                 }
2505 |       shift_expression LS_TK error
2506                 {yyerror ("Missing term"); RECOVER;}
2507 |       shift_expression SRS_TK error
2508                 {yyerror ("Missing term"); RECOVER;}
2509 |       shift_expression ZRS_TK error
2510                 {yyerror ("Missing term"); RECOVER;}
2511 ;
2512
2513 relational_expression:
2514         shift_expression
2515 |       relational_expression LT_TK shift_expression
2516                 {
2517                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2518                                     $1, $3);
2519                 }
2520 |       relational_expression GT_TK shift_expression
2521                 {
2522                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2523                                     $1, $3);
2524                 }
2525 |       relational_expression LTE_TK shift_expression
2526                 {
2527                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2528                                     $1, $3);
2529                 }
2530 |       relational_expression GTE_TK shift_expression
2531                 {
2532                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2533                                     $1, $3);
2534                 }
2535 |       relational_expression INSTANCEOF_TK reference_type
2536                 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2537 |       relational_expression LT_TK error
2538                 {yyerror ("Missing term"); RECOVER;}
2539 |       relational_expression GT_TK error
2540                 {yyerror ("Missing term"); RECOVER;}
2541 |       relational_expression LTE_TK error
2542                 {yyerror ("Missing term"); RECOVER;}
2543 |       relational_expression GTE_TK error
2544                 {yyerror ("Missing term"); RECOVER;}
2545 |       relational_expression INSTANCEOF_TK error
2546                 {yyerror ("Invalid reference type"); RECOVER;}
2547 ;
2548
2549 equality_expression:
2550         relational_expression
2551 |       equality_expression EQ_TK relational_expression
2552                 {
2553                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2554                                     $1, $3);
2555                 }
2556 |       equality_expression NEQ_TK relational_expression
2557                 {
2558                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2559                                     $1, $3);
2560                 }
2561 |       equality_expression EQ_TK error
2562                 {yyerror ("Missing term"); RECOVER;}
2563 |       equality_expression NEQ_TK error
2564                 {yyerror ("Missing term"); RECOVER;}
2565 ;
2566
2567 and_expression:
2568         equality_expression
2569 |       and_expression AND_TK equality_expression
2570                 {
2571                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2572                                     $1, $3);
2573                 }
2574 |       and_expression AND_TK error
2575                 {yyerror ("Missing term"); RECOVER;}
2576 ;
2577
2578 exclusive_or_expression:
2579         and_expression
2580 |       exclusive_or_expression XOR_TK and_expression
2581                 {
2582                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2583                                     $1, $3);
2584                 }
2585 |       exclusive_or_expression XOR_TK error
2586                 {yyerror ("Missing term"); RECOVER;}
2587 ;
2588
2589 inclusive_or_expression:
2590         exclusive_or_expression
2591 |       inclusive_or_expression OR_TK exclusive_or_expression
2592                 {
2593                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2594                                     $1, $3);
2595                 }
2596 |       inclusive_or_expression OR_TK error
2597                 {yyerror ("Missing term"); RECOVER;}
2598 ;
2599
2600 conditional_and_expression:
2601         inclusive_or_expression
2602 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2603                 {
2604                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2605                                     $1, $3);
2606                 }
2607 |       conditional_and_expression BOOL_AND_TK error
2608                 {yyerror ("Missing term"); RECOVER;}
2609 ;
2610
2611 conditional_or_expression:
2612         conditional_and_expression
2613 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
2614                 {
2615                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2616                                     $1, $3);
2617                 }
2618 |       conditional_or_expression BOOL_OR_TK error
2619                 {yyerror ("Missing term"); RECOVER;}
2620 ;
2621
2622 conditional_expression:         /* Error handling here is weak */
2623         conditional_or_expression
2624 |       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2625                 {
2626                   $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2627                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2628                 }
2629 |       conditional_or_expression REL_QM_TK REL_CL_TK error
2630                 {
2631                   YYERROR_NOW;
2632                   yyerror ("Missing term");
2633                   DRECOVER (1);
2634                 }
2635 |       conditional_or_expression REL_QM_TK error
2636                 {yyerror ("Missing term"); DRECOVER (2);}
2637 |       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2638                 {yyerror ("Missing term"); DRECOVER (3);}
2639 ;
2640
2641 assignment_expression:
2642         conditional_expression
2643 |       assignment
2644 ;
2645
2646 assignment:
2647         left_hand_side assignment_operator assignment_expression
2648                 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2649 |       left_hand_side assignment_operator error
2650                 {
2651                   YYNOT_TWICE yyerror ("Missing term");
2652                   DRECOVER (assign);
2653                 }
2654 ;
2655
2656 left_hand_side:
2657         name
2658 |       field_access
2659 |       array_access
2660 ;
2661
2662 assignment_operator:
2663         ASSIGN_ANY_TK
2664 |       ASSIGN_TK
2665 ;
2666
2667 expression:
2668         assignment_expression
2669 ;
2670
2671 constant_expression:
2672         expression
2673 ;
2674
2675 %%
2676
2677 /* Helper function to retrieve an OSB count. Should be used when the
2678    `dims:' rule is being used.  */
2679
2680 static int
2681 pop_current_osb (struct parser_ctxt *ctxp)
2682 {
2683   int to_return;
2684
2685   if (ctxp->osb_depth < 0)
2686     abort ();
2687
2688   to_return = CURRENT_OSB (ctxp);
2689   ctxp->osb_depth--;
2690
2691   return to_return;
2692 }
2693
2694 \f
2695
2696 /* This section of the code deal with save/restoring parser contexts.
2697    Add mode documentation here. FIXME */
2698
2699 /* Helper function. Create a new parser context. With
2700    COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2701    context is copied, otherwise, the new context is zeroed. The newly
2702    created context becomes the current one.  */
2703
2704 static void
2705 create_new_parser_context (int copy_from_previous)
2706 {
2707   struct parser_ctxt *new;
2708
2709   new = ggc_alloc (sizeof (struct parser_ctxt));
2710   if (copy_from_previous)
2711     {
2712       memcpy (new, ctxp, sizeof (struct parser_ctxt));
2713       /* This flag, indicating the context saves global values,
2714          should only be set by java_parser_context_save_global.  */
2715       new->saved_data_ctx = 0;
2716     }
2717   else
2718     memset (new, 0, sizeof (struct parser_ctxt));
2719
2720   new->next = ctxp;
2721   ctxp = new;
2722 }
2723
2724 /* Create a new parser context and make it the current one. */
2725
2726 void
2727 java_push_parser_context (void)
2728 {
2729   create_new_parser_context (0);
2730 }
2731
2732 void
2733 java_pop_parser_context (int generate)
2734 {
2735   tree current;
2736   struct parser_ctxt *next;
2737
2738   if (!ctxp)
2739     return;
2740
2741   next = ctxp->next;
2742   if (next)
2743     {
2744       input_location = ctxp->save_location;
2745       current_class = ctxp->class_type;
2746     }
2747
2748   /* If the old and new lexers differ, then free the old one.  */
2749   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2750     java_destroy_lexer (ctxp->lexer);
2751
2752   /* Set the single import class file flag to 0 for the current list
2753      of imported things */
2754   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2755     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2756
2757   /* If we pushed a context to parse a class intended to be generated,
2758      we keep it so we can remember the class. What we could actually
2759      do is to just update a list of class names.  */
2760   if (generate)
2761     {
2762       if (ctxp_for_generation_last == NULL)
2763         ctxp_for_generation = ctxp;
2764       else
2765         ctxp_for_generation_last->next = ctxp;
2766       ctxp->next = NULL;
2767       ctxp_for_generation_last = ctxp;
2768     }
2769
2770   /* And restore those of the previous context */
2771   if ((ctxp = next))            /* Assignment is really meant here */
2772     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2773       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2774 }
2775
2776 /* Create a parser context for the use of saving some global
2777    variables.  */
2778
2779 void
2780 java_parser_context_save_global (void)
2781 {
2782   if (!ctxp)
2783     {
2784       java_push_parser_context ();
2785       ctxp->saved_data_ctx = 1;
2786     }
2787
2788   /* If this context already stores data, create a new one suitable
2789      for data storage. */
2790   else if (ctxp->saved_data)
2791     {
2792       create_new_parser_context (1);
2793       ctxp->saved_data_ctx = 1;
2794     }
2795
2796   ctxp->save_location = input_location;
2797   ctxp->class_type = current_class;
2798   ctxp->function_decl = current_function_decl;
2799   ctxp->saved_data = 1;
2800 }
2801
2802 /* Restore some global variables from the previous context. Make the
2803    previous context the current one.  */
2804
2805 void
2806 java_parser_context_restore_global (void)
2807 {
2808   input_location = ctxp->save_location;
2809   current_class = ctxp->class_type;
2810   if (wfl_operator)
2811 #ifdef USE_MAPPED_LOCATION
2812     SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2813 #else
2814     EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2815 #endif
2816   current_function_decl = ctxp->function_decl;
2817   ctxp->saved_data = 0;
2818   if (ctxp->saved_data_ctx)
2819     java_pop_parser_context (0);
2820 }
2821
2822 /* Suspend vital data for the current class/function being parsed so
2823    that an other class can be parsed. Used to let local/anonymous
2824    classes be parsed.  */
2825
2826 static void
2827 java_parser_context_suspend (void)
2828 {
2829   /* This makes debugging through java_debug_context easier */
2830   static const char *const name = "<inner buffer context>";
2831
2832   /* Duplicate the previous context, use it to save the globals we're
2833      interested in */
2834   create_new_parser_context (1);
2835   ctxp->function_decl = current_function_decl;
2836   ctxp->class_type = current_class;
2837
2838   /* Then create a new context which inherits all data from the
2839      previous one. This will be the new current context  */
2840   create_new_parser_context (1);
2841
2842   /* Help debugging */
2843   ctxp->next->filename = name;
2844 }
2845
2846 /* Resume vital data for the current class/function being parsed so
2847    that an other class can be parsed. Used to let local/anonymous
2848    classes be parsed.  The trick is the data storing file position
2849    informations must be restored to their current value, so parsing
2850    can resume as if no context was ever saved. */
2851
2852 static void
2853 java_parser_context_resume (void)
2854 {
2855   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2856   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2857   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2858
2859   /* We need to inherit the list of classes to complete/generate */
2860   restored->classd_list = old->classd_list;
2861   restored->class_list = old->class_list;
2862
2863   /* Restore the current class and function from the saver */
2864   current_class = saver->class_type;
2865   current_function_decl = saver->function_decl;
2866
2867   /* Retrieve the restored context */
2868   ctxp = restored;
2869
2870   /* Re-installed the data for the parsing to carry on */
2871   memcpy (&ctxp->marker_begining, &old->marker_begining,
2872           (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2873 }
2874
2875 /* Add a new anchor node to which all statement(s) initializing static
2876    and non static initialized upon declaration field(s) will be
2877    linked.  */
2878
2879 static void
2880 java_parser_context_push_initialized_field (void)
2881 {
2882   tree node;
2883
2884   node = build_tree_list (NULL_TREE, NULL_TREE);
2885   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2886   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2887
2888   node = build_tree_list (NULL_TREE, NULL_TREE);
2889   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2890   CPC_INITIALIZER_LIST (ctxp) = node;
2891
2892   node = build_tree_list (NULL_TREE, NULL_TREE);
2893   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2894   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2895 }
2896
2897 /* Pop the lists of initialized field. If this lists aren't empty,
2898    remember them so we can use it to create and populate the finit$
2899    or <clinit> functions. */
2900
2901 static void
2902 java_parser_context_pop_initialized_field (void)
2903 {
2904   tree stmts;
2905   tree class_type = TREE_TYPE (GET_CPC ());
2906
2907   if (CPC_INITIALIZER_LIST (ctxp))
2908     {
2909       stmts = CPC_INITIALIZER_STMT (ctxp);
2910       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2911       if (stmts && !java_error_count)
2912         TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2913     }
2914
2915   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2916     {
2917       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2918       CPC_STATIC_INITIALIZER_LIST (ctxp) =
2919         TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2920       /* Keep initialization in order to enforce 8.5 */
2921       if (stmts && !java_error_count)
2922         TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2923     }
2924
2925   /* JDK 1.1 instance initializers */
2926   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2927     {
2928       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2929       CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2930         TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2931       if (stmts && !java_error_count)
2932         TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2933     }
2934 }
2935
2936 static tree
2937 reorder_static_initialized (tree list)
2938 {
2939   /* We have to keep things in order. The alias initializer have to
2940      come first, then the initialized regular field, in reverse to
2941      keep them in lexical order. */
2942   tree marker, previous = NULL_TREE;
2943   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2944     if (TREE_CODE (marker) == TREE_LIST
2945         && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2946       break;
2947
2948   /* No static initialized, the list is fine as is */
2949   if (!previous)
2950     list = TREE_CHAIN (marker);
2951
2952   /* No marker? reverse the whole list */
2953   else if (!marker)
2954     list = nreverse (list);
2955
2956   /* Otherwise, reverse what's after the marker and the new reordered
2957      sublist will replace the marker. */
2958   else
2959     {
2960       TREE_CHAIN (previous) = NULL_TREE;
2961       list = nreverse (list);
2962       list = chainon (TREE_CHAIN (marker), list);
2963     }
2964   return list;
2965 }
2966
2967 /* Helper functions to dump the parser context stack.  */
2968
2969 #define TAB_CONTEXT(C) \
2970   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2971
2972 static void
2973 java_debug_context_do (int tab)
2974 {
2975   struct parser_ctxt *copy = ctxp;
2976   while (copy)
2977     {
2978       TAB_CONTEXT (tab);
2979       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2980       TAB_CONTEXT (tab);
2981       fprintf (stderr, "filename: %s\n", copy->filename);
2982       TAB_CONTEXT (tab);
2983       fprintf (stderr, "package: %s\n",
2984                (copy->package ?
2985                 IDENTIFIER_POINTER (copy->package) : "<none>"));
2986       TAB_CONTEXT (tab);
2987       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2988       TAB_CONTEXT (tab);
2989       fprintf (stderr, "saved data: %d\n", copy->saved_data);
2990       copy = copy->next;
2991       tab += 2;
2992     }
2993 }
2994
2995 /* Dump the stacked up parser contexts. Intended to be called from a
2996    debugger.  */
2997
2998 void
2999 java_debug_context (void)
3000 {
3001   java_debug_context_do (0);
3002 }
3003
3004 \f
3005
3006 /* Flag for the error report routine to issue the error the first time
3007    it's called (overriding the default behavior which is to drop the
3008    first invocation and honor the second one, taking advantage of a
3009    richer context.  */
3010 static int force_error = 0;
3011
3012 /* Reporting an constructor invocation error.  */
3013 static void
3014 parse_ctor_invocation_error (void)
3015 {
3016   if (DECL_CONSTRUCTOR_P (current_function_decl))
3017     yyerror ("Constructor invocation must be first thing in a constructor");
3018   else
3019     yyerror ("Only constructors can invoke constructors");
3020 }
3021
3022 /* Reporting JDK1.1 features not implemented.  */
3023
3024 static tree
3025 parse_jdk1_1_error (const char *msg)
3026 {
3027   sorry (": %qs JDK1.1(TM) feature", msg);
3028   java_error_count++;
3029   return build_java_empty_stmt ();
3030 }
3031
3032 static int do_warning = 0;
3033
3034 void
3035 yyerror (const char *msgid)
3036 {
3037 #ifdef USE_MAPPED_LOCATION
3038   static source_location elc;
3039   expanded_location xloc = expand_location (input_location);
3040   int current_line = xloc.line;
3041 #else
3042   static java_lc elc;
3043   int save_lineno;
3044   int current_line = input_line;
3045 #endif
3046   static int prev_lineno;
3047   static const char *prev_msg;
3048
3049   char *remainder, *code_from_source;
3050
3051   if (!force_error && prev_lineno == current_line)
3052     return;
3053 #ifndef USE_MAPPED_LOCATION
3054   current_line = ctxp->lexer->token_start.line;
3055 #endif
3056
3057   /* Save current error location but report latter, when the context is
3058      richer.  */
3059   if (ctxp->java_error_flag == 0)
3060     {
3061       ctxp->java_error_flag = 1;
3062 #ifdef USE_MAPPED_LOCATION
3063       elc = input_location;
3064 #else
3065       elc = ctxp->lexer->token_start;
3066 #endif
3067       /* Do something to use the previous line if we're reaching the
3068          end of the file... */
3069 #ifdef VERBOSE_SKELETON
3070       printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3071 #endif
3072       return;
3073     }
3074
3075   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3076   if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3077     return;
3078
3079   ctxp->java_error_flag = 0;
3080   if (do_warning)
3081     java_warning_count++;
3082   else
3083     java_error_count++;
3084
3085 #if 0 /* FIXME */
3086   if (elc.col == 0 && msgid && msgid[1] == ';')
3087     elc = ctxp->prev_line_end;
3088 #endif
3089
3090   prev_msg = msgid;
3091
3092 #ifdef USE_MAPPED_LOCATION
3093   prev_lineno = current_line;
3094   code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3095 #else
3096   save_lineno = input_line;
3097   prev_lineno = input_line = current_line;
3098   code_from_source = java_get_line_col (input_filename, current_line,
3099                                         ctxp->lexer->token_start.col);
3100 #endif
3101
3102
3103   obstack_grow0 (&temporary_obstack,
3104                  code_from_source, strlen (code_from_source));
3105   remainder = obstack_finish (&temporary_obstack);
3106   if (do_warning)
3107     warning (0, "%s.\n%s", msgid, remainder);
3108   else
3109     error ("%s.\n%s", msgid, remainder);
3110
3111   /* This allow us to cheaply avoid an extra 'Invalid expression
3112      statement' error report when errors have been already reported on
3113      the same line. This occurs when we report an error but don't have
3114      a synchronization point other than ';', which
3115      expression_statement is the only one to take care of.  */
3116 #ifndef USE_MAPPED_LOCATION
3117   input_line = save_lineno;
3118 #endif
3119   ctxp->prevent_ese = input_line;
3120 }
3121
3122 static void
3123 issue_warning_error_from_context (
3124 #ifdef USE_MAPPED_LOCATION
3125                                   source_location cl,
3126 #else
3127                                   tree cl,
3128 #endif
3129                                   const char *gmsgid, va_list *ap)
3130 {
3131 #ifdef USE_MAPPED_LOCATION
3132   source_location saved_location = input_location;
3133   expanded_location xloc = expand_location (cl);
3134 #else
3135   java_lc save_lc = ctxp->lexer->token_start;
3136   const char *saved = ctxp->filename, *saved_input_filename;
3137 #endif
3138   char buffer [4096];
3139   text_info text;
3140
3141   text.err_no = errno;
3142   text.args_ptr = ap;
3143   text.format_spec = gmsgid;
3144   pp_format (global_dc->printer, &text);
3145   pp_output_formatted_text (global_dc->printer);
3146   strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3147   buffer[sizeof (buffer) - 1] = '\0';
3148   pp_clear_output_area (global_dc->printer);
3149
3150   force_error = 1;
3151
3152 #ifdef USE_MAPPED_LOCATION
3153   if (xloc.file != NULL)
3154     {
3155       ctxp->filename = xloc.file;
3156       input_location = cl;
3157     }
3158 #else
3159   ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3160   ctxp->lexer->token_start.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3161                                    : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3162                                    : EXPR_WFL_COLNO (cl));
3163
3164   /* We have a CL, that's a good reason for using it if it contains data */
3165   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3166     ctxp->filename = EXPR_WFL_FILENAME (cl);
3167   saved_input_filename = input_filename;
3168   input_filename = ctxp->filename;
3169 #endif
3170   java_error (NULL);
3171   java_error (buffer);
3172 #ifdef USE_MAPPED_LOCATION
3173   input_location = saved_location;
3174 #else
3175   ctxp->filename = saved;
3176   input_filename = saved_input_filename;
3177   ctxp->lexer->token_start = save_lc;
3178 #endif
3179   force_error = 0;
3180 }
3181
3182 /* Issue an error message at a current source line CL.
3183    FUTURE/FIXME:  change cl to be a source_location. */
3184
3185 void
3186 parse_error_context (tree cl, const char *gmsgid, ...)
3187 {
3188   va_list ap;
3189   va_start (ap, gmsgid);
3190 #ifdef USE_MAPPED_LOCATION
3191   issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3192 #else
3193   issue_warning_error_from_context (cl, gmsgid, &ap);
3194 #endif
3195   va_end (ap);
3196 }
3197
3198 /* Issue a warning at a current source line CL.
3199    FUTURE/FIXME:  change cl to be a source_location. */
3200
3201 static void
3202 parse_warning_context (tree cl, const char *gmsgid, ...)
3203 {
3204   va_list ap;
3205   va_start (ap, gmsgid);
3206
3207   do_warning = 1;
3208 #ifdef USE_MAPPED_LOCATION
3209   issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3210 #else
3211   issue_warning_error_from_context (cl, gmsgid, &ap);
3212 #endif
3213   do_warning = 0;
3214   va_end (ap);
3215 }
3216
3217 static tree
3218 find_expr_with_wfl (tree node)
3219 {
3220   while (node)
3221     {
3222       enum tree_code_class code;
3223       tree to_return;
3224
3225       switch (TREE_CODE (node))
3226         {
3227         case BLOCK:
3228           node = BLOCK_EXPR_BODY (node);
3229           continue;
3230
3231         case COMPOUND_EXPR:
3232           to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3233           if (to_return)
3234             return to_return;
3235           node = TREE_OPERAND (node, 1);
3236           continue;
3237
3238         case LOOP_EXPR:
3239           node = TREE_OPERAND (node, 0);
3240           continue;
3241
3242         case LABELED_BLOCK_EXPR:
3243           node = LABELED_BLOCK_BODY (node);
3244           continue;
3245
3246         default:
3247           code = TREE_CODE_CLASS (TREE_CODE (node));
3248           if (((code == tcc_unary) || (code == tcc_binary)
3249                || (code == tcc_expression))
3250               && EXPR_WFL_LINECOL (node))
3251             return node;
3252           return NULL_TREE;
3253         }
3254     }
3255   return NULL_TREE;
3256 }
3257
3258 /* Issue a missing return statement error. Uses METHOD to figure the
3259    last line of the method the error occurs in.  */
3260
3261 static void
3262 missing_return_error (tree method)
3263 {
3264 #ifdef USE_MAPPED_LOCATION
3265   SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3266 #else
3267   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3268 #endif
3269   parse_error_context (wfl_operator, "Missing return statement");
3270 }
3271
3272 /* Issue an unreachable statement error. From NODE, find the next
3273    statement to report appropriately.  */
3274 static void
3275 unreachable_stmt_error (tree node)
3276 {
3277   /* Browse node to find the next expression node that has a WFL. Use
3278      the location to report the error */
3279   if (TREE_CODE (node) == COMPOUND_EXPR)
3280     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3281   else
3282     node = find_expr_with_wfl (node);
3283
3284   if (node)
3285     {
3286 #ifdef USE_MAPPED_LOCATION
3287       SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3288 #else
3289       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3290 #endif
3291       parse_error_context (wfl_operator, "Unreachable statement");
3292     }
3293   else
3294     abort ();
3295 }
3296
3297 static int
3298 not_accessible_field_error (tree wfl, tree decl)
3299 {
3300   parse_error_context 
3301     (wfl, "Can't access %s field %<%s.%s%> from %qs",
3302      accessibility_string (get_access_flags_from_decl (decl)),
3303      GET_TYPE_NAME (DECL_CONTEXT (decl)),
3304      IDENTIFIER_POINTER (DECL_NAME (decl)),
3305      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3306   return 1;
3307 }
3308
3309 int
3310 java_report_errors (void)
3311 {
3312   if (java_error_count)
3313     fprintf (stderr, "%d error%s",
3314              java_error_count, (java_error_count == 1 ? "" : "s"));
3315   if (java_warning_count)
3316     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3317              java_warning_count, (java_warning_count == 1 ? "" : "s"));
3318   if (java_error_count || java_warning_count)
3319     putc ('\n', stderr);
3320   return java_error_count;
3321 }
3322
3323 static char *
3324 java_accstring_lookup (int flags)
3325 {
3326   static char buffer [80];
3327 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3328
3329   /* Access modifier looked-up first for easier report on forbidden
3330      access. */
3331   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3332   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3333   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3334   if (flags & ACC_STATIC) COPY_RETURN ("static");
3335   if (flags & ACC_FINAL) COPY_RETURN ("final");
3336   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3337   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3338   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3339   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3340   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3341   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3342
3343   buffer [0] = '\0';
3344   return buffer;
3345 #undef COPY_RETURN
3346 }
3347
3348 /* Returns a string denoting the accessibility of a class or a member as
3349    indicated by FLAGS.  We need a separate function from
3350    java_accstring_lookup, as the latter can return spurious "static", etc.
3351    if package-private access is defined (in which case none of the
3352    relevant access control bits in FLAGS is set).  */
3353
3354 static const char *
3355 accessibility_string (int flags)
3356 {
3357   if (flags & ACC_PRIVATE) return "private";
3358   if (flags & ACC_PROTECTED) return "protected";
3359   if (flags & ACC_PUBLIC) return "public";
3360
3361   return "package-private";
3362 }
3363
3364 /* Issuing error messages upon redefinition of classes, interfaces or
3365    variables. */
3366
3367 static void
3368 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3369 {
3370   parse_error_context (cl, "%s %qs already defined in %s:%d",
3371                        context, IDENTIFIER_POINTER (id),
3372                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3373   /* Here we should point out where its redefined. It's a unicode. FIXME */
3374 }
3375
3376 static void
3377 variable_redefinition_error (tree context, tree name, tree type, int line)
3378 {
3379   const char *type_name;
3380
3381   /* Figure a proper name for type. We might haven't resolved it */
3382   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3383     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3384   else
3385     type_name = lang_printable_name (type, 0);
3386
3387   parse_error_context (context,
3388                        "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3389                        IDENTIFIER_POINTER (name),
3390                        type_name, IDENTIFIER_POINTER (name), line);
3391 }
3392
3393 /* If ANAME is terminated with `[]', it indicates an array. This
3394    function returns the number of `[]' found and if this number is
3395    greater than zero, it extracts the array type name and places it in
3396    the node pointed to by TRIMMED unless TRIMMED is null.  */
3397
3398 static int
3399 build_type_name_from_array_name (tree aname, tree *trimmed)
3400 {
3401   const char *name = IDENTIFIER_POINTER (aname);
3402   int len = IDENTIFIER_LENGTH (aname);
3403   int array_dims;
3404
3405   STRING_STRIP_BRACKETS (name, len, array_dims);
3406
3407   if (array_dims && trimmed)
3408     *trimmed = get_identifier_with_length (name, len);
3409
3410   return array_dims;
3411 }
3412
3413 static tree
3414 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3415 {
3416   int more_dims = 0;
3417
3418   /* Eventually get more dims */
3419   more_dims = build_type_name_from_array_name (name, &name);
3420
3421   /* If we have, then craft a new type for this variable */
3422   if (more_dims)
3423     {
3424       tree save = type;
3425
3426       /* If we have a pointer, use its type */
3427       if (TREE_CODE (type) == POINTER_TYPE)
3428         type = TREE_TYPE (type);
3429
3430       /* Building the first dimension of a primitive type uses this
3431          function */
3432       if (JPRIMITIVE_TYPE_P (type))
3433         {
3434           type = build_java_array_type (type, -1);
3435           more_dims--;
3436         }
3437       /* Otherwise, if we have a WFL for this type, use it (the type
3438          is already an array on an unresolved type, and we just keep
3439          on adding dimensions) */
3440       else if (type_wfl)
3441         {
3442           type = type_wfl;
3443           more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3444                                                         NULL);
3445         }
3446
3447       /* Add all the dimensions */
3448       while (more_dims--)
3449         type = build_unresolved_array_type (type);
3450
3451       /* The type may have been incomplete in the first place */
3452       if (type_wfl)
3453         type = obtain_incomplete_type (type);
3454     }
3455
3456   if (ret_name)
3457     *ret_name = name;
3458   return type;
3459 }
3460
3461 /* Build something that the type identifier resolver will identify as
3462    being an array to an unresolved type. TYPE_WFL is a WFL on a
3463    identifier. */
3464
3465 static tree
3466 build_unresolved_array_type (tree type_or_wfl)
3467 {
3468   const char *ptr;
3469   tree wfl;
3470
3471   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3472      just create a array type */
3473   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3474     return build_java_array_type (type_or_wfl, -1);
3475
3476   obstack_grow (&temporary_obstack,
3477                  IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3478                  IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3479   obstack_grow0 (&temporary_obstack, "[]", 2);
3480   ptr = obstack_finish (&temporary_obstack);
3481 #ifdef USE_MAPPED_LOCATION
3482   wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3483 #else
3484   wfl = build_expr_wfl (get_identifier (ptr),
3485                         EXPR_WFL_FILENAME (type_or_wfl),
3486                         EXPR_WFL_LINENO (type_or_wfl),
3487                         EXPR_WFL_COLNO (type_or_wfl));
3488 #endif
3489   /* Re-install the existing qualifications so that the type can be
3490      resolved properly. */
3491   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3492   return wfl;
3493 }
3494
3495 static void
3496 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3497 {
3498   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3499     parse_error_context (wfl, "Interface %qs repeated",
3500                          IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3501 }
3502
3503 /* Bulk of common class/interface checks. Return 1 if an error was
3504    encountered. TAG is 0 for a class, 1 for an interface.  */
3505
3506 static int
3507 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3508                                 tree qualified_name, tree decl, tree cl)
3509 {
3510   tree node;
3511   int sca = 0;                  /* Static class allowed */
3512   int icaf = 0;                 /* Inner class allowed flags */
3513   int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3514
3515   if (!quiet_flag)
3516     fprintf (stderr, " %s%s %s",
3517              (CPC_INNER_P () ? "inner" : ""),
3518              (is_interface ? "interface" : "class"),
3519              IDENTIFIER_POINTER (qualified_name));
3520
3521   /* Scope of an interface/class type name:
3522        - Can't be imported by a single type import
3523        - Can't already exists in the package */
3524   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3525       && (node = find_name_in_single_imports (raw_name))
3526       && !CPC_INNER_P ())
3527     {
3528       parse_error_context
3529         (cl, "%s name %qs clashes with imported type %qs",
3530          (is_interface ? "Interface" : "Class"),
3531          IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3532       return 1;
3533     }
3534   if (decl && CLASS_COMPLETE_P (decl))
3535     {
3536       classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3537                                    qualified_name, decl, cl);
3538       return 1;
3539     }
3540
3541   if (check_inner_class_redefinition (raw_name, cl))
3542     return 1;
3543
3544   /* If public, file name should match class/interface name, except
3545      when dealing with an inner class */
3546   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3547     {
3548       const char *fname = input_filename;
3549       const char *f;
3550
3551       for (f = fname + strlen (fname);
3552            f != fname && ! IS_DIR_SEPARATOR (*f);
3553            f--)
3554         ;
3555       if (IS_DIR_SEPARATOR (*f))
3556         f++;
3557       if (strncmp (IDENTIFIER_POINTER (raw_name),
3558                    f , IDENTIFIER_LENGTH (raw_name)) ||
3559           f [IDENTIFIER_LENGTH (raw_name)] != '.')
3560         parse_error_context
3561           (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3562                              (is_interface ? "interface" : "class"),
3563                              IDENTIFIER_POINTER (qualified_name),
3564                              IDENTIFIER_POINTER (raw_name));
3565     }
3566
3567   /* Static classes can be declared only in top level classes. Note:
3568      once static, a inner class is a top level class. */
3569   if (flags & ACC_STATIC)
3570     {
3571       /* Catch the specific error of declaring an class inner class
3572          with no toplevel enclosing class. Prevent check_modifiers from
3573          complaining a second time */
3574       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3575         {
3576           parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3577                                IDENTIFIER_POINTER (qualified_name));
3578           sca = ACC_STATIC;
3579         }
3580       /* Else, in the context of a top-level class declaration, let
3581          `check_modifiers' do its job, otherwise, give it a go */
3582       else
3583         sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3584     }
3585
3586   /* Inner classes can be declared private or protected
3587      within their enclosing classes. */
3588   if (CPC_INNER_P ())
3589     {
3590       /* A class which is local to a block can't be public, private,
3591          protected or static. But it is created final, so allow this
3592          one. */
3593       if (current_function_decl)
3594         icaf = sca = uaaf = ACC_FINAL;
3595       else
3596         {
3597           check_modifiers_consistency (flags);
3598           icaf = ACC_PROTECTED;
3599           if (! CLASS_INTERFACE (GET_CPC ()))
3600             icaf |= ACC_PRIVATE;
3601         }
3602     }
3603
3604   if (is_interface)
3605     {
3606       if (CPC_INNER_P ())
3607         uaaf = INTERFACE_INNER_MODIFIERS;
3608       else
3609         uaaf = INTERFACE_MODIFIERS;
3610
3611       check_modifiers ("Illegal modifier %qs for interface declaration",
3612                        flags, uaaf);
3613     }
3614   else
3615     check_modifiers ((current_function_decl ?
3616                       "Illegal modifier %qs for local class declaration" :
3617                       "Illegal modifier %qs for class declaration"),
3618                      flags, uaaf|sca|icaf);
3619   return 0;
3620 }
3621
3622 /* Construct a nested class name.  If the final component starts with
3623    a digit, return true.  Otherwise return false.  */
3624 static int
3625 make_nested_class_name (tree cpc_list)
3626 {
3627   tree name;
3628
3629   if (!cpc_list)
3630     return 0;
3631
3632   make_nested_class_name (TREE_CHAIN (cpc_list));
3633
3634   /* Pick the qualified name when dealing with the first upmost
3635      enclosing class */
3636   name = (TREE_CHAIN (cpc_list)
3637           ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3638   obstack_grow (&temporary_obstack,
3639                 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3640   obstack_1grow (&temporary_obstack, '$');
3641
3642   return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3643 }
3644
3645 /* Can't redefine a class already defined in an earlier scope. */
3646
3647 static int
3648 check_inner_class_redefinition (tree raw_name, tree cl)
3649 {
3650   tree scope_list;
3651
3652   for (scope_list = GET_CPC_LIST (); scope_list;
3653        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3654     if (raw_name == GET_CPC_UN_NODE (scope_list))
3655       {
3656         parse_error_context
3657           (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",
3658            IDENTIFIER_POINTER (raw_name));
3659         return 1;
3660       }
3661   return 0;
3662 }
3663
3664 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3665    we remember ENCLOSING and SUPER.  */
3666
3667 static tree
3668 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3669                      tree *super, tree class_type)
3670 {
3671   tree local_enclosing = *enclosing;
3672   tree local_super = NULL_TREE;
3673
3674   while (local_enclosing)
3675     {
3676       tree intermediate, decl;
3677
3678       *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3679         local_enclosing;
3680
3681       if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3682         return decl;
3683
3684       intermediate = local_enclosing;
3685       /* Explore enclosing contexts. */
3686       while (INNER_CLASS_DECL_P (intermediate))
3687         {
3688           intermediate = DECL_CONTEXT (intermediate);
3689           if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3690             return decl;
3691         }
3692
3693       /* Now go to the upper classes, bail out if necessary.  We will
3694          analyze the returned SUPER and act accordingly (see
3695          do_resolve_class).  */
3696       if (JPRIMITIVE_TYPE_P (TREE_TYPE (local_enclosing))
3697           || TREE_TYPE (local_enclosing) == void_type_node)
3698         {
3699           parse_error_context (cl, "Qualifier must be a reference");
3700           local_enclosing = NULL_TREE;
3701           break;
3702         }
3703       local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3704       if (!local_super || local_super == object_type_node)
3705         break;
3706
3707       if (TREE_CODE (local_super) == POINTER_TYPE)
3708         local_super = do_resolve_class (NULL, NULL, local_super, NULL, NULL);
3709       else
3710         local_super = TYPE_NAME (local_super);
3711
3712       /* We may not have checked for circular inheritance yet, so do so
3713          here to prevent an infinite loop. */
3714       if (htab_find (circularity_hash, local_super) != NULL)
3715         {
3716           if (!cl)
3717             cl = lookup_cl (local_enclosing);
3718
3719           parse_error_context
3720             (cl, "Cyclic inheritance involving %s",
3721              IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3722           local_enclosing = NULL_TREE;
3723         }
3724       else
3725         local_enclosing = local_super;
3726     }
3727
3728   /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3729   *super = local_super;
3730   *enclosing = local_enclosing;
3731
3732   return NULL_TREE;
3733 }
3734
3735 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3736    qualified. */
3737
3738 static tree
3739 find_as_inner_class (tree enclosing, tree name, tree cl)
3740 {
3741   tree qual, to_return;
3742   if (!enclosing)
3743     return NULL_TREE;
3744
3745   name = TYPE_NAME (name);
3746
3747   /* First search: within the scope of `enclosing', search for name */
3748   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3749     qual = EXPR_WFL_QUALIFICATION (cl);
3750   else if (cl)
3751     qual = build_tree_list (cl, NULL_TREE);
3752   else
3753     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3754
3755   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3756     return to_return;
3757
3758   /* We're dealing with a qualified name. Try to resolve thing until
3759      we get something that is an enclosing class. */
3760   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3761     {
3762       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3763
3764       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3765            qual = TREE_CHAIN (qual))
3766         {
3767           acc = merge_qualified_name (acc,
3768                                       EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3769           BUILD_PTR_FROM_NAME (ptr, acc);
3770           decl = do_resolve_class (NULL_TREE, NULL_TREE, ptr, NULL_TREE, cl);
3771         }
3772
3773       /* A NULL qual and a decl means that the search ended
3774          successfully?!? We have to do something then. FIXME */
3775
3776       if (decl)
3777         enclosing = decl;
3778       else
3779         qual = EXPR_WFL_QUALIFICATION (cl);
3780     }
3781   /* Otherwise, create a qual for the other part of the resolution. */
3782   else
3783     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3784
3785   return find_as_inner_class_do (qual, enclosing);
3786 }
3787
3788 /* We go inside the list of sub classes and try to find a way
3789    through. */
3790
3791 static tree
3792 find_as_inner_class_do (tree qual, tree enclosing)
3793 {
3794   if (!qual)
3795     return NULL_TREE;
3796
3797   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3798     {
3799       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3800       tree next_enclosing = NULL_TREE;
3801       tree inner_list;
3802
3803       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3804            inner_list; inner_list = TREE_CHAIN (inner_list))
3805         {
3806           if (TREE_VALUE (inner_list) == name_to_match)
3807             {
3808               next_enclosing = TREE_PURPOSE (inner_list);
3809               break;
3810             }
3811         }
3812       enclosing = next_enclosing;
3813     }
3814
3815   return (!qual && enclosing ? enclosing : NULL_TREE);
3816 }
3817
3818 static void
3819 link_nested_class_to_enclosing (void)
3820 {
3821   if (GET_ENCLOSING_CPC ())
3822     {
3823       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3824       DECL_INNER_CLASS_LIST (enclosing) =
3825         tree_cons (GET_CPC (), GET_CPC_UN (),
3826                    DECL_INNER_CLASS_LIST (enclosing));
3827     }
3828 }
3829
3830 static tree
3831 maybe_make_nested_class_name (tree name)
3832 {
3833   tree id = NULL_TREE;
3834
3835   if (CPC_INNER_P ())
3836     {
3837       /* If we're in a function, we must append a number to create the
3838          nested class name.  However, we don't do this if the class we
3839          are constructing is anonymous, because in that case we'll
3840          already have a number as the class name.  */
3841       if (! make_nested_class_name (GET_CPC_LIST ())
3842           && current_function_decl != NULL_TREE
3843           && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3844         {
3845           char buf[10];
3846           sprintf (buf, "%d", anonymous_class_counter);
3847           ++anonymous_class_counter;
3848           obstack_grow (&temporary_obstack, buf, strlen (buf));
3849           obstack_1grow (&temporary_obstack, '$');
3850         }
3851       obstack_grow0 (&temporary_obstack,
3852                      IDENTIFIER_POINTER (name),
3853                      IDENTIFIER_LENGTH (name));
3854       id = get_identifier (obstack_finish (&temporary_obstack));
3855       if (ctxp->package)
3856         QUALIFIED_P (id) = 1;
3857     }
3858   return id;
3859 }
3860
3861 /* If DECL is NULL, create and push a new DECL, record the current
3862    line CL and do other maintenance things.  */
3863
3864 static tree
3865 maybe_create_class_interface_decl (tree decl, tree raw_name,
3866                                    tree qualified_name, tree cl)
3867 {
3868   if (!decl)
3869     decl = push_class (make_class (), qualified_name);
3870
3871   /* Take care of the file and line business */
3872 #ifdef USE_MAPPED_LOCATION
3873   DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3874 #else
3875   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3876   DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3877 #endif
3878   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3879   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3880 #ifdef USE_MAPPED_LOCATION
3881   {
3882     tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3883     CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3884       tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3885   }
3886 #else
3887   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3888     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3889 #endif
3890
3891   PUSH_CPC (decl, raw_name);
3892   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3893
3894   /* Link the declaration to the already seen ones */
3895   TREE_CHAIN (decl) = ctxp->class_list;
3896   ctxp->class_list = decl;
3897
3898   /* Create a new nodes in the global lists */
3899   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3900   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3901
3902   /* Install a new dependency list element */
3903   create_jdep_list (ctxp);
3904
3905   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3906                           IDENTIFIER_POINTER (qualified_name)));
3907   return decl;
3908 }
3909
3910 static void
3911 add_superinterfaces (tree decl, tree interface_list)
3912 {
3913   tree node;
3914   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3915      takes care of ensuring that:
3916        - This is an accessible interface type,
3917        - Circularity detection.
3918    parser_add_interface is then called. If present but not defined,
3919    the check operation is delayed until the super interface gets
3920    defined.  */
3921   for (node = interface_list; node; node = TREE_CHAIN (node))
3922     {
3923       tree current = TREE_PURPOSE (node);
3924       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3925       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3926         {
3927           if (!parser_check_super_interface (idecl, decl, current))
3928             parser_add_interface (decl, idecl, current);
3929         }
3930       else
3931         register_incomplete_type (JDEP_INTERFACE,
3932                                   current, decl, NULL_TREE);
3933     }
3934 }
3935
3936 /* Create an interface in pass1 and return its decl. Return the
3937    interface's decl in pass 2.  */
3938
3939 static tree
3940 create_interface (int flags, tree id, tree super)
3941 {
3942   tree raw_name = EXPR_WFL_NODE (id);
3943   tree q_name = parser_qualified_classname (raw_name);
3944   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3945
3946   /* Certain syntax errors are making SUPER be like ID. Avoid this
3947      case. */
3948   if (ctxp->class_err && id == super)
3949     super = NULL;
3950
3951   EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3952
3953   /* Basic checks: scope, redefinition, modifiers */
3954   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3955     {
3956       PUSH_ERROR ();
3957       return NULL_TREE;
3958     }
3959
3960   /* Suspend the current parsing context if we're parsing an inner
3961      interface */
3962   if (CPC_INNER_P ())
3963     {
3964       java_parser_context_suspend ();
3965       /* Interface members are public. */
3966       if (CLASS_INTERFACE (GET_CPC ()))
3967         flags |= ACC_PUBLIC;
3968     }
3969
3970   /* Push a new context for (static) initialized upon declaration fields */
3971   java_parser_context_push_initialized_field ();
3972
3973   /* Interface modifiers check
3974        - public/abstract allowed (already done at that point)
3975        - abstract is obsolete (comes first, it's a warning, or should be)
3976        - Can't use twice the same (checked in the modifier rule) */
3977   if ((flags & ACC_ABSTRACT) && flag_redundant)
3978     parse_warning_context
3979       (MODIFIER_WFL (ABSTRACT_TK),
3980        "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3981
3982   /* Create a new decl if DECL is NULL, otherwise fix it */
3983   decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3984
3985   /* Interfaces are always abstract. */
3986   flags |= ACC_ABSTRACT;
3987
3988   /* Inner interfaces are always static.  */
3989   if (INNER_CLASS_DECL_P (decl))
3990     flags |= ACC_STATIC;
3991
3992   /* Set super info and mark the class a complete */
3993   set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3994                   object_type_node, ctxp->interface_number);
3995   ctxp->interface_number = 0;
3996   CLASS_COMPLETE_P (decl) = 1;
3997   add_superinterfaces (decl, super);
3998
3999   /* Eventually sets the @deprecated tag flag */
4000   CHECK_DEPRECATED (decl);
4001
4002   return decl;
4003 }
4004
4005 /* Patch anonymous class CLASS, by either extending or implementing
4006    DEP.  */
4007
4008 static void
4009 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4010 {
4011   tree class = TREE_TYPE (class_decl);
4012   tree type =  TREE_TYPE (type_decl);
4013   tree binfo = TYPE_BINFO (class);
4014
4015   /* If it's an interface, implement it */
4016   if (CLASS_INTERFACE (type_decl))
4017     {
4018       if (parser_check_super_interface (type_decl, class_decl, wfl))
4019         return;
4020
4021       if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4022         {
4023            /* Extend the binfo - by reallocating and copying it. */
4024           tree new_binfo;
4025           tree base_binfo;
4026           int i;
4027           
4028           new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4029           for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4030             BINFO_BASE_APPEND (new_binfo, base_binfo);
4031           CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4032           BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4033           TYPE_BINFO (class) = new_binfo;
4034         }
4035       
4036       /* And add the interface */
4037       parser_add_interface (class_decl, type_decl, wfl);
4038     }
4039   /* Otherwise, it's a type we want to extend */
4040   else
4041     {
4042       if (parser_check_super (type_decl, class_decl, wfl))
4043         return;
4044       BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4045     }
4046 }
4047
4048 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4049    its decl.  */
4050
4051 static tree
4052 create_anonymous_class (tree type_name)
4053 {
4054   char buffer [80];
4055   tree super = NULL_TREE, itf = NULL_TREE;
4056   tree id, type_decl, class;
4057
4058   /* The unqualified name of the anonymous class. It's just a number. */
4059   sprintf (buffer, "%d", anonymous_class_counter++);
4060   id = build_wfl_node (get_identifier (buffer));
4061   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4062
4063   /* We know about the type to extend/implement. We go ahead */
4064   if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4065     {
4066       /* Create a class which either implements on extends the designated
4067          class. The class bears an inaccessible name. */
4068       if (CLASS_INTERFACE (type_decl))
4069         {
4070           /* It's OK to modify it here. It's been already used and
4071              shouldn't be reused */
4072           ctxp->interface_number = 1;
4073           /* Interfaces should presented as a list of WFLs */
4074           itf = build_tree_list (type_name, NULL_TREE);
4075         }
4076       else
4077         super = type_name;
4078     }
4079
4080   class = create_class (ACC_FINAL, id, super, itf);
4081
4082   /* We didn't know anything about the stuff. We register a dependence. */
4083   if (!type_decl)
4084     register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4085
4086   ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4087   return class;
4088 }
4089
4090 /* Create a class in pass1 and return its decl. Return class
4091    interface's decl in pass 2.  */
4092
4093 static tree
4094 create_class (int flags, tree id, tree super, tree interfaces)
4095 {
4096   tree raw_name = EXPR_WFL_NODE (id);
4097   tree class_id, decl;
4098   tree super_decl_type;
4099
4100   /* Certain syntax errors are making SUPER be like ID. Avoid this
4101      case. */
4102   if (ctxp->class_err && id == super)
4103     super = NULL;
4104
4105   class_id = parser_qualified_classname (raw_name);
4106   decl = IDENTIFIER_CLASS_VALUE (class_id);
4107   EXPR_WFL_NODE (id) = class_id;
4108
4109   /* Basic check: scope, redefinition, modifiers */
4110   if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4111     {
4112       PUSH_ERROR ();
4113       return NULL_TREE;
4114     }
4115
4116   /* Suspend the current parsing context if we're parsing an inner
4117      class or an anonymous class. */
4118   if (CPC_INNER_P ())
4119     {
4120       java_parser_context_suspend ();
4121       /* Interface members are public. */
4122       if (CLASS_INTERFACE (GET_CPC ()))
4123         flags |= ACC_PUBLIC;
4124     }
4125
4126   /* Push a new context for (static) initialized upon declaration fields */
4127   java_parser_context_push_initialized_field ();
4128
4129   /* Class modifier check:
4130        - Allowed modifier (already done at that point)
4131        - abstract AND final forbidden
4132        - Public classes defined in the correct file */
4133   if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4134     parse_error_context
4135       (id, "Class %qs can't be declared both abstract and final",
4136        IDENTIFIER_POINTER (raw_name));
4137
4138   /* Create a new decl if DECL is NULL, otherwise fix it */
4139   decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4140
4141   /* If SUPER exists, use it, otherwise use Object */
4142   if (super)
4143     {
4144       /* java.lang.Object can't extend anything.  */
4145       if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4146         {
4147           parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4148           return NULL_TREE;
4149         }
4150
4151       super_decl_type =
4152         register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4153     }
4154   else if (TREE_TYPE (decl) != object_type_node)
4155     super_decl_type = object_type_node;
4156   /* We're defining java.lang.Object */
4157   else
4158     super_decl_type = NULL_TREE;
4159
4160   /* A class nested in an interface is implicitly static. */
4161   if (INNER_CLASS_DECL_P (decl)
4162       && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4163     {
4164       flags |= ACC_STATIC;
4165     }
4166
4167   /* Set super info and mark the class as complete. */
4168   set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4169                   ctxp->interface_number);
4170   ctxp->interface_number = 0;
4171   CLASS_COMPLETE_P (decl) = 1;
4172   add_superinterfaces (decl, interfaces);
4173
4174   /* TYPE_VFIELD' is a compiler-generated field used to point to
4175      virtual function tables.  In gcj, every class has a common base
4176      virtual function table in java.lang.object.  */
4177   TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4178
4179   /* We keep the compilation unit imports in the class so that
4180      they can be used later to resolve type dependencies that
4181      aren't necessary to solve now. */
4182   TYPE_IMPORT_LIST (TREE_TYPE (decl)) = ctxp->import_list;
4183   TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (decl)) = ctxp->import_demand_list;
4184
4185   /* Add the private this$<n> field, Replicate final locals still in
4186      scope as private final fields mangled like val$<local_name>.
4187      This does not occur for top level (static) inner classes. */
4188   if (PURE_INNER_CLASS_DECL_P (decl))
4189     add_inner_class_fields (decl, current_function_decl);
4190
4191   /* Eventually sets the @deprecated tag flag */
4192   CHECK_DEPRECATED (decl);
4193
4194   /* Reset the anonymous class counter when declaring non inner classes */
4195   if (!INNER_CLASS_DECL_P (decl))
4196     anonymous_class_counter = 1;
4197
4198   return decl;
4199 }
4200
4201 /* End a class declaration: register the statements used to create
4202    finit$ and <clinit>, pop the current class and resume the prior
4203    parser context if necessary.  */
4204
4205 static void
4206 end_class_declaration (int resume)
4207 {
4208   /* If an error occurred, context weren't pushed and won't need to be
4209      popped by a resume. */
4210   int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4211
4212   if (GET_CPC () != error_mark_node)
4213     dump_java_tree (TDI_class, GET_CPC ());
4214
4215   java_parser_context_pop_initialized_field ();
4216   POP_CPC ();
4217   if (resume && no_error_occurred)
4218     java_parser_context_resume ();
4219
4220   /* We're ending a class declaration, this is a good time to reset
4221      the interface cout. Note that might have been already done in
4222      create_interface, but if at that time an inner class was being
4223      dealt with, the interface count was reset in a context created
4224      for the sake of handling inner classes declaration. */
4225   ctxp->interface_number = 0;
4226 }
4227
4228 static void
4229 add_inner_class_fields (tree class_decl, tree fct_decl)
4230 {
4231   tree block, marker, f;
4232
4233   f = add_field (TREE_TYPE (class_decl),
4234                  build_current_thisn (TREE_TYPE (class_decl)),
4235                  build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4236                  ACC_PRIVATE);
4237   FIELD_THISN (f) = 1;
4238
4239   if (!fct_decl)
4240     return;
4241
4242   for (block = GET_CURRENT_BLOCK (fct_decl);
4243        block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4244     {
4245       tree decl;
4246       for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4247         {
4248           tree name, pname;
4249           tree wfl, init, list;
4250
4251           /* Avoid non final arguments. */
4252           if (!LOCAL_FINAL_P (decl))
4253             continue;
4254
4255           MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4256           MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4257           wfl = build_wfl_node (name);
4258           init = build_wfl_node (pname);
4259           /* Build an initialization for the field: it will be
4260              initialized by a parameter added to finit$, bearing a
4261              mangled name of the field itself (param$<n>.) The
4262              parameter is provided to finit$ by the constructor
4263              invoking it (hence the constructor will also feature a
4264              hidden parameter, set to the value of the outer context
4265              local at the time the inner class is created.)
4266
4267              Note: we take into account all possible locals that can
4268              be accessed by the inner class. It's actually not trivial
4269              to minimize these aliases down to the ones really
4270              used. One way to do that would be to expand all regular
4271              methods first, then finit$ to get a picture of what's
4272              used.  It works with the exception that we would have to
4273              go back on all constructor invoked in regular methods to
4274              have their invocation reworked (to include the right amount
4275              of alias initializer parameters.)
4276
4277              The only real way around, I think, is a first pass to
4278              identify locals really used in the inner class. We leave
4279              the flag FIELD_LOCAL_ALIAS_USED around for that future
4280              use.
4281
4282              On the other hand, it only affect local inner classes,
4283              whose constructors (and finit$ call) will be featuring
4284              unnecessary arguments. It's easy for a developer to keep
4285              this number of parameter down by using the `final'
4286              keyword only when necessary. For the time being, we can
4287              issue a warning on unnecessary finals. FIXME */
4288           init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4289                                    wfl, init);
4290
4291           /* Register the field. The TREE_LIST holding the part
4292              initialized/initializer will be marked ARG_FINAL_P so
4293              that the created field can be marked
4294              FIELD_LOCAL_ALIAS. */
4295           list = build_tree_list (wfl, init);
4296           ARG_FINAL_P (list) = 1;
4297           register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4298         }
4299     }
4300
4301   if (!CPC_INITIALIZER_STMT (ctxp))
4302     return;
4303
4304   /* If we ever registered an alias field, insert and marker to
4305      remember where the list ends. The second part of the list (the one
4306      featuring initialized fields) so it can be later reversed to
4307      enforce 8.5. The marker will be removed during that operation. */
4308   marker = build_tree_list (NULL_TREE, NULL_TREE);
4309   TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4310   SET_CPC_INITIALIZER_STMT (ctxp, marker);
4311 }
4312
4313 /* Can't use lookup_field () since we don't want to load the class and
4314    can't set the CLASS_LOADED_P flag */
4315
4316 static tree
4317 find_field (tree class, tree name)
4318 {
4319   tree decl;
4320   for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4321     {
4322       if (DECL_NAME (decl) == name)
4323         return decl;
4324     }
4325   return NULL_TREE;
4326 }
4327
4328 /* Wrap around lookup_field that doesn't potentially upset the value
4329    of CLASS */
4330
4331 static tree
4332 lookup_field_wrapper (tree class, tree name)
4333 {
4334   tree type = class;
4335   tree decl = NULL_TREE;
4336   java_parser_context_save_global ();
4337
4338   /* Last chance: if we're within the context of an inner class, we
4339      might be trying to access a local variable defined in an outer
4340      context. We try to look for it now. */
4341   if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4342     {
4343       tree new_name;
4344       MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4345       decl = lookup_field (&type, new_name);
4346       if (decl && decl != error_mark_node)
4347         FIELD_LOCAL_ALIAS_USED (decl) = 1;
4348     }
4349   if (!decl || decl == error_mark_node)
4350     {
4351       type = class;
4352       decl = lookup_field (&type, name);
4353     }
4354
4355   /* If the field still hasn't been found, try the next enclosing context. */
4356   if (!decl && INNER_CLASS_TYPE_P (class))
4357     {
4358       tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4359       decl = lookup_field_wrapper (outer_type, name);
4360     }
4361
4362   java_parser_context_restore_global ();
4363   return decl == error_mark_node ? NULL : decl;
4364 }
4365
4366 /* Find duplicate field within the same class declarations and report
4367    the error. Returns 1 if a duplicated field was found, 0
4368    otherwise.  */
4369
4370 static int
4371 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4372 {
4373   /* This might be modified to work with method decl as well */
4374   tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4375   if (decl)
4376     {
4377       char *t1 = xstrdup (purify_type_name
4378                          ((TREE_CODE (new_type) == POINTER_TYPE
4379                            && TREE_TYPE (new_type) == NULL_TREE) ?
4380                           IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4381                           lang_printable_name (new_type, 1)));
4382       /* The type may not have been completed by the time we report
4383          the error */
4384       char *t2 = xstrdup (purify_type_name
4385                          ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4386                            && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4387                           IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4388                           lang_printable_name (TREE_TYPE (decl), 1)));
4389       parse_error_context
4390         (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4391          t1, IDENTIFIER_POINTER (new_field_name),
4392          t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4393          DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4394       free (t1);
4395       free (t2);
4396       return 1;
4397     }
4398   return 0;
4399 }
4400
4401 /* Field registration routine. If TYPE doesn't exist, field
4402    declarations are linked to the undefined TYPE dependency list, to
4403    be later resolved in java_complete_class () */
4404
4405 static void
4406 register_fields (int flags, tree type, tree variable_list)
4407 {
4408   tree current, saved_type;
4409   tree class_type = NULL_TREE;
4410   location_t saved_location = input_location;
4411   int must_chain = 0;
4412   tree wfl = NULL_TREE;
4413
4414   if (GET_CPC ())
4415     class_type = TREE_TYPE (GET_CPC ());
4416
4417   if (!class_type || class_type == error_mark_node)
4418     return;
4419
4420   /* If we're adding fields to interfaces, those fields are public,
4421      static, final */
4422   if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4423     {
4424       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4425                                  flags, ACC_PUBLIC, "interface field(s)");
4426       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4427                                  flags, ACC_STATIC, "interface field(s)");
4428       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4429                                  flags, ACC_FINAL, "interface field(s)");
4430       check_modifiers ("Illegal interface member modifier %qs", flags,
4431                        INTERFACE_FIELD_MODIFIERS);
4432       flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4433     }
4434
4435   /* Obtain a suitable type for resolution, if necessary */
4436   SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4437
4438   /* If TYPE is fully resolved and we don't have a reference, make one */
4439   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4440
4441   for (current = variable_list, saved_type = type; current;
4442        current = TREE_CHAIN (current), type = saved_type)
4443     {
4444       tree real_type;
4445       tree field_decl;
4446       tree cl = TREE_PURPOSE (current);
4447       tree init = TREE_VALUE (current);
4448       tree current_name = EXPR_WFL_NODE (cl);
4449
4450       /* Can't declare non-final static fields in inner classes */
4451       if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4452           && !(flags & ACC_FINAL))
4453         parse_error_context
4454           (cl, "Field %qs can't be static in inner class %qs unless it is final",
4455            IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4456            lang_printable_name (class_type, 0));
4457
4458       /* Process NAME, as it may specify extra dimension(s) for it */
4459       type = build_array_from_name (type, wfl, current_name, &current_name);
4460
4461       /* Type adjustment. We may have just readjusted TYPE because
4462          the variable specified more dimensions. Make sure we have
4463          a reference if we can and don't have one already. Also
4464          change the name if we have an init. */
4465       if (type != saved_type)
4466         {
4467           PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4468           if (init)
4469             EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4470         }
4471
4472       real_type = GET_REAL_TYPE (type);
4473       /* Check for redeclarations */
4474       if (duplicate_declaration_error_p (current_name, real_type, cl))
4475         continue;
4476
4477       /* Set input_line to the line the field was found and create a
4478          declaration for it. Eventually sets the @deprecated tag flag. */
4479 #ifdef USE_MAPPED_LOCATION
4480       input_location = EXPR_LOCATION (cl);
4481 #else
4482       input_line = EXPR_WFL_LINENO (cl);
4483 #endif
4484       field_decl = add_field (class_type, current_name, real_type, flags);
4485       CHECK_DEPRECATED_NO_RESET (field_decl);
4486
4487       /* If the field denotes a final instance variable, then we
4488          allocate a LANG_DECL_SPECIFIC part to keep track of its
4489          initialization. We also mark whether the field was
4490          initialized upon its declaration. We don't do that if the
4491          created field is an alias to a final local. */
4492       if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4493         {
4494           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4495           DECL_FIELD_FINAL_WFL (field_decl) = cl;
4496         }
4497
4498       /* If the couple initializer/initialized is marked ARG_FINAL_P,
4499          we mark the created field FIELD_LOCAL_ALIAS, so that we can
4500          hide parameters to this inner class finit$ and
4501          constructors. It also means that the field isn't final per
4502          say. */
4503       if (ARG_FINAL_P (current))
4504         {
4505           FIELD_LOCAL_ALIAS (field_decl) = 1;
4506           FIELD_FINAL (field_decl) = 0;
4507         }
4508
4509       /* Check if we must chain. */
4510       if (must_chain)
4511         register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4512
4513       /* If we have an initialization value tied to the field */
4514       if (init)
4515         {
4516           /* The field is declared static */
4517           if (flags & ACC_STATIC)
4518             {
4519               /* We include the field and its initialization part into
4520                  a list used to generate <clinit>. After <clinit> is
4521                  walked, field initializations will be processed and
4522                  fields initialized with known constants will be taken
4523                  out of <clinit> and have their DECL_INITIAL set
4524                  appropriately. */
4525               TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4526               SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4527               if (TREE_OPERAND (init, 1)
4528                   && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4529                 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4530             }
4531           /* A non-static field declared with an immediate initialization is
4532              to be initialized in <init>, if any.  This field is remembered
4533              to be processed at the time of the generation of <init>. */
4534           else
4535             {
4536               TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4537               SET_CPC_INITIALIZER_STMT (ctxp, init);
4538             }
4539           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4540           DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4541         }
4542     }
4543
4544   CLEAR_DEPRECATED;
4545   input_location = saved_location;
4546 }
4547
4548 /* Generate finit$, using the list of initialized fields to populate
4549    its body. finit$'s parameter(s) list is adjusted to include the
4550    one(s) used to initialized the field(s) caching outer context
4551    local(s).  */
4552
4553 static tree
4554 generate_finit (tree class_type)
4555 {
4556   int count = 0;
4557   tree list = TYPE_FINIT_STMT_LIST (class_type);
4558   tree mdecl, current, parms;
4559
4560   parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4561                                                   class_type, NULL_TREE,
4562                                                   &count);
4563   CRAFTED_PARAM_LIST_FIXUP (parms);
4564   mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4565                                     finit_identifier_node, parms);
4566   fix_method_argument_names (parms, mdecl);
4567   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4568                        mdecl, NULL_TREE);
4569   DECL_FUNCTION_NAP (mdecl) = count;
4570   start_artificial_method_body (mdecl);
4571
4572   for (current = list; current; current = TREE_CHAIN (current))
4573     java_method_add_stmt (mdecl,
4574                           build_debugable_stmt (EXPR_WFL_LINECOL (current),
4575                                                 current));
4576   end_artificial_method_body (mdecl);
4577   return mdecl;
4578 }
4579
4580 /* Generate a function to run the instance initialization code. The
4581    private method is called `instinit$'. Unless we're dealing with an
4582    anonymous class, we determine whether all ctors of CLASS_TYPE
4583    declare a checked exception in their `throws' clause in order to
4584    see whether it's necessary to encapsulate the instance initializer
4585    statements in a try/catch/rethrow sequence.  */
4586
4587 static tree
4588 generate_instinit (tree class_type)
4589 {
4590   tree current;
4591   tree compound = NULL_TREE;
4592   tree parms = tree_cons (this_identifier_node,
4593                           build_pointer_type (class_type), end_params_node);
4594   tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4595                                          void_type_node,
4596                                          instinit_identifier_node, parms);
4597
4598   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4599                        mdecl, NULL_TREE);
4600
4601   /* Gather all the statements in a compound */
4602   for (current = TYPE_II_STMT_LIST (class_type);
4603        current; current = TREE_CHAIN (current))
4604     compound = add_stmt_to_compound (compound, NULL_TREE, current);
4605
4606   /* We need to encapsulate COMPOUND by a try/catch statement to
4607      rethrow exceptions that might occur in the instance initializer.
4608      We do that only if all ctors of CLASS_TYPE are set to catch a
4609      checked exception. This doesn't apply to anonymous classes (since
4610      they don't have declared ctors.) */
4611   if (!ANONYMOUS_CLASS_P (class_type) &&
4612       ctors_unchecked_throws_clause_p (class_type))
4613     {
4614       compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4615                                              build1 (THROW_EXPR, NULL_TREE,
4616                                                      build_wfl_node (wpv_id)));
4617       DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4618                                                       exception_type_node);
4619     }
4620
4621   start_artificial_method_body (mdecl);
4622   java_method_add_stmt (mdecl, compound);
4623   end_artificial_method_body (mdecl);
4624
4625   return mdecl;
4626 }
4627
4628 /* FIXME */
4629 static tree
4630 build_instinit_invocation (tree class_type)
4631 {
4632   tree to_return = NULL_TREE;
4633
4634   if (TYPE_II_STMT_LIST (class_type))
4635     {
4636       tree parm = build_tree_list (NULL_TREE,
4637                                    build_wfl_node (this_identifier_node));
4638       to_return =
4639         build_method_invocation (build_wfl_node (instinit_identifier_node),
4640                                  parm);
4641     }
4642   return to_return;
4643 }
4644
4645 /* Shared across method_declarator and method_header to remember the
4646    patch stage that was reached during the declaration of the method.
4647    A method DECL is built differently is there is no patch
4648    (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4649    pending on the currently defined method.  */
4650
4651 static int patch_stage;
4652
4653 /* Check the method declaration and add the method to its current
4654    class.  If the argument list is known to contain incomplete types,
4655    the method is partially added and the registration will be resume
4656    once the method arguments resolved. If TYPE is NULL, we're dealing
4657    with a constructor.  */
4658
4659 static tree
4660 method_header (int flags, tree type, tree mdecl, tree throws)
4661 {
4662   tree type_wfl = NULL_TREE;
4663   tree meth_name = NULL_TREE;
4664   tree current, orig_arg, this_class = NULL;
4665   tree id, meth;
4666   location_t saved_location;
4667   int constructor_ok = 0, must_chain;
4668   int count;
4669
4670   if (mdecl == error_mark_node)
4671     return error_mark_node;
4672   meth = TREE_VALUE (mdecl);
4673   id = TREE_PURPOSE (mdecl);
4674
4675   check_modifiers_consistency (flags);
4676
4677   if (GET_CPC ())
4678     this_class = TREE_TYPE (GET_CPC ());
4679
4680   if (!this_class || this_class == error_mark_node)
4681     return NULL_TREE;
4682
4683   /* There are some forbidden modifiers for an abstract method and its
4684      class must be abstract as well.  */
4685   if (type && (flags & ACC_ABSTRACT))
4686     {
4687       ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4688       ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4689       ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4690       ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4691       ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4692       ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4693       if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4694           && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4695         parse_error_context
4696           (id,
4697            "Class %qs must be declared abstract to define abstract method %qs",
4698            IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4699            IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4700     }
4701
4702   /* A native method can't be strictfp.  */
4703   if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4704     parse_error_context (id, "native method %qs can't be strictfp",
4705                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4706   /* No such thing as a transient or volatile method.  */
4707   if ((flags & ACC_TRANSIENT))
4708     parse_error_context (id, "method %qs can't be transient",
4709                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4710   if ((flags & ACC_VOLATILE))
4711     parse_error_context (id, "method %qs can't be volatile",
4712                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4713
4714   /* Things to be checked when declaring a constructor */
4715   if (!type)
4716     {
4717       int ec = java_error_count;
4718       /* 8.6: Constructor declarations: we might be trying to define a
4719          method without specifying a return type. */
4720       if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4721         parse_error_context
4722           (id, "Invalid method declaration, return type required");
4723       /* 8.6.3: Constructor modifiers */
4724       else
4725         {
4726           JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4727           JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4728           JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4729           JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4730           JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4731           JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4732         }
4733       /* If we found error here, we don't consider it's OK to tread
4734          the method definition as a constructor, for the rest of this
4735          function */
4736       if (ec == java_error_count)
4737         constructor_ok = 1;
4738     }
4739
4740   /* Method declared within the scope of an interface are implicitly
4741      abstract and public. Conflicts with other erroneously provided
4742      modifiers are checked right after. */
4743
4744   if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4745     {
4746       /* If FLAGS isn't set because of a modifier, turn the
4747          corresponding modifier WFL to NULL so we issue a warning on
4748          the obsolete use of the modifier */
4749       if (!(flags & ACC_PUBLIC))
4750         MODIFIER_WFL (PUBLIC_TK) = NULL;
4751       if (!(flags & ACC_ABSTRACT))
4752         MODIFIER_WFL (ABSTRACT_TK) = NULL;
4753       flags |= ACC_PUBLIC;
4754       flags |= ACC_ABSTRACT;
4755     }
4756
4757   /* Inner class can't declare static methods */
4758   if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4759     {
4760       parse_error_context
4761         (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4762          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4763          lang_printable_name (this_class, 0));
4764     }
4765
4766   /* Modifiers context reset moved up, so abstract method declaration
4767      modifiers can be later checked.  */
4768
4769   /* Set constructor returned type to void and method name to <init>,
4770      unless we found an error identifier the constructor (in which
4771      case we retain the original name) */
4772   if (!type)
4773     {
4774       type = void_type_node;
4775       if (constructor_ok)
4776         meth_name = init_identifier_node;
4777     }
4778   else
4779     meth_name = EXPR_WFL_NODE (id);
4780
4781   /* Do the returned type resolution and registration if necessary */
4782   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4783
4784   if (meth_name)
4785     type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4786   EXPR_WFL_NODE (id) = meth_name;
4787   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4788
4789   if (must_chain)
4790     {
4791       patch_stage = JDEP_METHOD_RETURN;
4792       register_incomplete_type (patch_stage, type_wfl, id, type);
4793       TREE_TYPE (meth) = GET_REAL_TYPE (type);
4794     }
4795   else
4796     TREE_TYPE (meth) = type;
4797
4798   saved_location = input_location;
4799   /* When defining an abstract or interface method, the curly
4800      bracket at level 1 doesn't exist because there is no function
4801      body */
4802 #ifdef USE_MAPPED_LOCATION
4803   input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4804                     EXPR_LOCATION (id));
4805 #else
4806   input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4807                 EXPR_WFL_LINENO (id));
4808 #endif
4809
4810   /* Remember the original argument list */
4811   orig_arg = TYPE_ARG_TYPES (meth);
4812
4813   if (patch_stage)              /* includes ret type and/or all args */
4814     {
4815       jdep *jdep;
4816       meth = add_method_1 (this_class, flags, meth_name, meth);
4817       /* Patch for the return type */
4818       if (patch_stage == JDEP_METHOD_RETURN)
4819         {
4820           jdep = CLASSD_LAST (ctxp->classd_list);
4821           JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4822         }
4823       /* This is the stop JDEP. METH allows the function's signature
4824          to be computed. */
4825       register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4826     }
4827   else
4828     meth = add_method (this_class, flags, meth_name,
4829                        build_java_signature (meth));
4830
4831   /* Remember final parameters */
4832   MARK_FINAL_PARMS (meth, orig_arg);
4833
4834   /* Fix the method argument list so we have the argument name
4835      information */
4836   fix_method_argument_names (orig_arg, meth);
4837
4838   /* Register the parameter number and re-install the current line
4839      number */
4840   DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4841   input_location = saved_location;
4842
4843   /* Register exception specified by the `throws' keyword for
4844      resolution and set the method decl appropriate field to the list.
4845      Note: the grammar ensures that what we get here are class
4846      types. */
4847   if (throws)
4848     {
4849       throws = nreverse (throws);
4850       for (current = throws; current; current = TREE_CHAIN (current))
4851         {
4852           register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4853                                     NULL_TREE, NULL_TREE);
4854           JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4855             &TREE_VALUE (current);
4856         }
4857       DECL_FUNCTION_THROWS (meth) = throws;
4858     }
4859
4860   if (TREE_TYPE (GET_CPC ()) != object_type_node)
4861     DECL_FUNCTION_WFL (meth) = id;
4862
4863   /* Set the flag if we correctly processed a constructor */
4864   if (constructor_ok)
4865     {
4866       DECL_CONSTRUCTOR_P (meth) = 1;
4867       /* Compute and store the number of artificial parameters declared
4868          for this constructor */
4869       for (count = 0, current = TYPE_FIELDS (this_class); current;
4870            current = TREE_CHAIN (current))
4871         if (FIELD_LOCAL_ALIAS (current))
4872           count++;
4873       DECL_FUNCTION_NAP (meth) = count;
4874     }
4875
4876   /* Eventually set the @deprecated tag flag */
4877   CHECK_DEPRECATED (meth);
4878
4879   return meth;
4880 }
4881
4882 static void
4883 fix_method_argument_names (tree orig_arg, tree meth)
4884 {
4885   tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4886   if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4887     {
4888       TREE_PURPOSE (arg) = this_identifier_node;
4889       arg = TREE_CHAIN (arg);
4890     }
4891   while (orig_arg != end_params_node)
4892     {
4893       TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4894       orig_arg = TREE_CHAIN (orig_arg);
4895       arg = TREE_CHAIN (arg);
4896     }
4897 }
4898
4899 /* Complete the method declaration with METHOD_BODY.  */
4900
4901 static void
4902 finish_method_declaration (tree method_body)
4903 {
4904   int flags;
4905
4906   if (!current_function_decl)
4907     return;
4908
4909   flags = get_access_flags_from_decl (current_function_decl);
4910
4911   /* 8.4.5 Method Body */
4912   if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4913     {
4914       tree name = DECL_NAME (current_function_decl);
4915       parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4916                            "%s method %qs can't have a body defined",
4917                            (METHOD_NATIVE (current_function_decl) ?
4918                             "Native" : "Abstract"),
4919                            IDENTIFIER_POINTER (name));
4920       method_body = NULL_TREE;
4921     }
4922   else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4923     {
4924       tree name = DECL_NAME (current_function_decl);
4925       parse_error_context
4926         (DECL_FUNCTION_WFL (current_function_decl),
4927          "Non native and non abstract method %qs must have a body defined",
4928          IDENTIFIER_POINTER (name));
4929       method_body = NULL_TREE;
4930     }
4931
4932   if (flag_emit_class_files && method_body
4933       && TREE_CODE (method_body) == NOP_EXPR
4934       && TREE_TYPE (current_function_decl)
4935       && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4936     method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4937
4938   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4939   maybe_absorb_scoping_blocks ();
4940   /* Exit function's body */
4941   exit_block ();
4942   /* Merge last line of the function with first line, directly in the
4943      function decl. It will be used to emit correct debug info. */
4944   DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4945
4946   /* Since function's argument's list are shared, reset the
4947      ARG_FINAL_P parameter that might have been set on some of this
4948      function parameters. */
4949   UNMARK_FINAL_PARMS (current_function_decl);
4950
4951   /* So we don't have an irrelevant function declaration context for
4952      the next static block we'll see. */
4953   current_function_decl = NULL_TREE;
4954 }
4955
4956 /* Build a an error message for constructor circularity errors.  */
4957
4958 static char *
4959 constructor_circularity_msg (tree from, tree to)
4960 {
4961   static char string [4096];
4962   char *t = xstrdup (lang_printable_name (from, 2));
4963   sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4964   free (t);
4965   return string;
4966 }
4967
4968 /* Verify a circular call to METH. Return 1 if an error is found, 0
4969    otherwise.  */
4970
4971 static GTY(()) tree vcc_list;
4972 static int
4973 verify_constructor_circularity (tree meth, tree current)
4974 {
4975   tree c;
4976
4977   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4978     {
4979       if (TREE_VALUE (c) == meth)
4980         {
4981           char *t;
4982           if (vcc_list)
4983             {
4984               tree liste;
4985               vcc_list = nreverse (vcc_list);
4986               for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4987                 {
4988                   parse_error_context
4989                     (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4990                      constructor_circularity_msg
4991                       (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4992                   java_error_count--;
4993                 }
4994             }
4995           t = xstrdup (lang_printable_name (meth, 2));
4996           parse_error_context (TREE_PURPOSE (c),
4997                                "%s: recursive invocation of constructor %qs",
4998                                constructor_circularity_msg (current, meth), t);
4999           free (t);
5000           vcc_list = NULL_TREE;
5001           return 1;
5002         }
5003     }
5004   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5005     {
5006       vcc_list = tree_cons (c, current, vcc_list);
5007       if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5008         return 1;
5009       vcc_list = TREE_CHAIN (vcc_list);
5010     }
5011   return 0;
5012 }
5013
5014 /* Check modifiers that can be declared but exclusively */
5015
5016 static void
5017 check_modifiers_consistency (int flags)
5018 {
5019   int acc_count = 0;
5020   tree cl = NULL_TREE;
5021
5022   THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5023   THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5024   THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5025   if (acc_count > 1)
5026     parse_error_context
5027       (cl, "Inconsistent member declaration.  At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5028
5029   acc_count = 0;
5030   cl = NULL_TREE;
5031   THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5032   THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5033   if (acc_count > 1)
5034     parse_error_context (cl,
5035                          "Inconsistent member declaration.  At most one of %<final%> or %<volatile%> may be specified");
5036 }
5037
5038 /* Check the methode header METH for abstract specifics features */
5039
5040 static void
5041 check_abstract_method_header (tree meth)
5042 {
5043   int flags = get_access_flags_from_decl (meth);
5044
5045   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5046                               ACC_ABSTRACT, "abstract method",
5047                               IDENTIFIER_POINTER (DECL_NAME (meth)));
5048   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5049                               ACC_PUBLIC, "abstract method",
5050                               IDENTIFIER_POINTER (DECL_NAME (meth)));
5051
5052   check_modifiers ("Illegal modifier %qs for interface method",
5053                   flags, INTERFACE_METHOD_MODIFIERS);
5054 }
5055
5056 /* Create a FUNCTION_TYPE node and start augmenting it with the
5057    declared function arguments. Arguments type that can't be resolved
5058    are left as they are, but the returned node is marked as containing
5059    incomplete types.  */
5060
5061 static tree
5062 method_declarator (tree id, tree list)
5063 {
5064   tree arg_types = NULL_TREE, current, node;
5065   tree meth = make_node (FUNCTION_TYPE);
5066   jdep *jdep;
5067
5068   patch_stage = JDEP_NO_PATCH;
5069
5070   if (GET_CPC () == error_mark_node)
5071     return error_mark_node;
5072
5073   /* If we're dealing with an inner class constructor, we hide the
5074      this$<n> decl in the name field of its parameter declaration.  We
5075      also might have to hide the outer context local alias
5076      initializers. Not done when the class is a toplevel class. */
5077   if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5078       && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5079     {
5080       tree aliases_list, type, thisn;
5081       /* First the aliases, linked to the regular parameters */
5082       aliases_list =
5083         build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5084                                                 TREE_TYPE (GET_CPC ()),
5085                                                 NULL_TREE, NULL);
5086       list = chainon (nreverse (aliases_list), list);
5087
5088       /* Then this$<n> */
5089       type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5090       thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5091       list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5092                         list);
5093     }
5094
5095   for (current = list; current; current = TREE_CHAIN (current))
5096     {
5097       int must_chain = 0;
5098       tree wfl_name = TREE_PURPOSE (current);
5099       tree type = TREE_VALUE (current);
5100       tree name = EXPR_WFL_NODE (wfl_name);
5101       tree already, arg_node;
5102       tree type_wfl = NULL_TREE;
5103       tree real_type;
5104
5105       /* Obtain a suitable type for resolution, if necessary */
5106       SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5107
5108       /* Process NAME, as it may specify extra dimension(s) for it */
5109       type = build_array_from_name (type, type_wfl, name, &name);
5110       EXPR_WFL_NODE (wfl_name) = name;
5111
5112       real_type = GET_REAL_TYPE (type);
5113       if (TREE_CODE (real_type) == RECORD_TYPE)
5114         {
5115           real_type = promote_type (real_type);
5116           if (TREE_CODE (type) == TREE_LIST)
5117             TREE_PURPOSE (type) = real_type;
5118         }
5119
5120       /* Check redefinition */
5121       for (already = arg_types; already; already = TREE_CHAIN (already))
5122         if (TREE_PURPOSE (already) == name)
5123           {
5124             parse_error_context
5125               (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5126                IDENTIFIER_POINTER (name),
5127                IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5128             break;
5129           }
5130
5131       /* If we've an incomplete argument type, we know there is a location
5132          to patch when the type get resolved, later.  */
5133       jdep = NULL;
5134       if (must_chain)
5135         {
5136           patch_stage = JDEP_METHOD;
5137           type = register_incomplete_type (patch_stage,
5138                                            type_wfl, wfl_name, type);
5139           jdep = CLASSD_LAST (ctxp->classd_list);
5140           JDEP_MISC (jdep) = id;
5141         }
5142
5143       /* The argument node: a name and a (possibly) incomplete type.  */
5144       arg_node = build_tree_list (name, real_type);
5145       /* Remember arguments declared final. */
5146       ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5147
5148       if (jdep)
5149         JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5150       TREE_CHAIN (arg_node) = arg_types;
5151       arg_types = arg_node;
5152     }
5153   TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5154   node = build_tree_list (id, meth);
5155   return node;
5156 }
5157
5158 static int
5159 unresolved_type_p (tree wfl, tree *returned)
5160 {
5161   if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5162     {
5163       if (returned)
5164         {
5165           tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5166           if (decl && current_class && (decl == TYPE_NAME (current_class)))
5167             *returned = TREE_TYPE (decl);
5168           else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5169             *returned = TREE_TYPE (GET_CPC ());
5170           else
5171             *returned = NULL_TREE;
5172         }
5173       return 1;
5174     }
5175   if (returned)
5176     *returned = wfl;
5177   return 0;
5178 }
5179
5180 /* From NAME, build a qualified identifier node using the
5181    qualification from the current package definition. */
5182
5183 static tree
5184 parser_qualified_classname (tree name)
5185 {
5186   tree nested_class_name;
5187
5188   if ((nested_class_name = maybe_make_nested_class_name (name)))
5189     return nested_class_name;
5190
5191   if (ctxp->package)
5192     return merge_qualified_name (ctxp->package, name);
5193   else
5194     return name;
5195 }
5196
5197 /* Called once the type a interface extends is resolved. Returns 0 if
5198    everything is OK.  */
5199
5200 static int
5201 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5202 {
5203   tree super_type = TREE_TYPE (super_decl);
5204
5205   /* Has to be an interface */
5206   if (!CLASS_INTERFACE (super_decl))
5207     {
5208       parse_error_context
5209         (this_wfl, "%s %qs can't implement/extend %s %qs",
5210          (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5211           "Interface" : "Class"),
5212          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5213          (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5214          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5215       return 1;
5216     }
5217
5218   /* Check top-level interface access. Inner classes are subject to member
5219      access rules (6.6.1). */
5220   if (! INNER_CLASS_P (super_type)
5221       && check_pkg_class_access (DECL_NAME (super_decl),
5222                                  NULL_TREE, true, this_decl))
5223     return 1;
5224
5225   SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5226                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5227                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5228   return 0;
5229 }
5230
5231 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5232    0 if everything is OK.  */
5233
5234 static int
5235 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5236 {
5237   tree super_type = TREE_TYPE (super_decl);
5238
5239   /* SUPER should be a CLASS (neither an array nor an interface) */
5240   if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5241     {
5242       parse_error_context
5243         (wfl, "Class %qs can't subclass %s %qs",
5244          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5245          (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5246          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5247       return 1;
5248     }
5249
5250   if (CLASS_FINAL (TYPE_NAME (super_type)))
5251     {
5252       parse_error_context (wfl, "Can't subclass final classes: %s",
5253                            IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5254       return 1;
5255     }
5256
5257   /* Check top-level class scope. Inner classes are subject to member access
5258      rules (6.6.1). */
5259   if (! INNER_CLASS_P (super_type)
5260       && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5261     return 1;
5262
5263   SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5264                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5265                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5266   return 0;
5267 }
5268
5269 /* Create a new dependency list and link it (in a LIFO manner) to the
5270    CTXP list of type dependency list.  */
5271
5272 static void
5273 create_jdep_list (struct parser_ctxt *ctxp)
5274 {
5275   jdeplist *new = xmalloc (sizeof (jdeplist));
5276   new->first = new->last = NULL;
5277   new->next = ctxp->classd_list;
5278   ctxp->classd_list = new;
5279 }
5280
5281 static jdeplist *
5282 reverse_jdep_list (struct parser_ctxt *ctxp)
5283 {
5284   jdeplist *prev = NULL, *current, *next;
5285   for (current = ctxp->classd_list; current; current = next)
5286     {
5287       next = current->next;
5288       current->next = prev;
5289       prev = current;
5290     }
5291   return prev;
5292 }
5293
5294 /* Create a fake pointer based on the ID stored in
5295    TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5296    registered again. */
5297
5298 static tree
5299 obtain_incomplete_type (tree type_name)
5300 {
5301   tree ptr = NULL_TREE, name;
5302
5303   if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5304     name = EXPR_WFL_NODE (type_name);
5305   else if (INCOMPLETE_TYPE_P (type_name))
5306     name = TYPE_NAME (type_name);
5307   else
5308     abort ();
5309
5310   /* Workaround from build_pointer_type for incomplete types.  */
5311   BUILD_PTR_FROM_NAME (ptr, name);
5312   TYPE_MODE (ptr) = ptr_mode;
5313   layout_type (ptr);
5314
5315   return ptr;
5316 }
5317
5318 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5319    non NULL instead of computing a new fake type based on WFL. The new
5320    dependency is inserted in the current type dependency list, in FIFO
5321    manner.  */
5322
5323 static tree
5324 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5325 {
5326   jdep *new = xmalloc (sizeof (jdep));
5327
5328   if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5329     ptr = obtain_incomplete_type (wfl);
5330
5331   JDEP_KIND (new) = kind;
5332   JDEP_DECL (new) = decl;
5333   JDEP_TO_RESOLVE (new) = ptr;
5334   JDEP_WFL (new) = wfl;
5335   JDEP_CHAIN (new) = NULL;
5336   JDEP_MISC (new) = NULL_TREE;
5337   /* For some dependencies, set the enclosing class of the current
5338      class to be the enclosing context */
5339   if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5340       && GET_ENCLOSING_CPC ())
5341     JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5342   else
5343     JDEP_ENCLOSING (new) = GET_CPC ();
5344   JDEP_GET_PATCH (new) = (tree *)NULL;
5345
5346   JDEP_INSERT (ctxp->classd_list, new);
5347
5348   return ptr;
5349 }
5350
5351 /* This checks for circular references with innerclasses. We start
5352    from SOURCE and should never reach TARGET. Extended/implemented
5353    types in SOURCE have their enclosing context checked not to reach
5354    TARGET. When the last enclosing context of SOURCE is reached, its
5355    extended/implemented types are also checked not to reach TARGET.
5356    In case of error, WFL of the offending type is returned; NULL_TREE
5357    otherwise.  */
5358
5359 static tree
5360 check_inner_circular_reference (tree source, tree target)
5361 {
5362   tree base_binfo;
5363   tree ctx, cl;
5364   int i;
5365
5366   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5367     {
5368       tree su;
5369
5370       /* We can end up with a NULL_TREE or an incomplete type here if
5371          we encountered previous type resolution errors. It's safe to
5372          simply ignore these cases.  */
5373       su = BINFO_TYPE (base_binfo);
5374       if (INCOMPLETE_TYPE_P (su))
5375         continue;
5376
5377       if (inherits_from_p (su, target))
5378         return lookup_cl (TYPE_NAME (su));
5379
5380       for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5381         {
5382           /* An enclosing context shouldn't be TARGET */
5383           if (ctx == TYPE_NAME (target))
5384             return lookup_cl (TYPE_NAME (su));
5385
5386           /* When we reach the enclosing last context, start a check
5387              on it, with the same target */
5388           if (! DECL_CONTEXT (ctx) &&
5389               (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5390             return cl;
5391         }
5392     }
5393   return NULL_TREE;
5394 }
5395
5396 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5397    offending type if a circularity is detected. NULL_TREE is returned
5398    otherwise. TYPE can be an interface or a class.   */
5399
5400 static tree
5401 check_circular_reference (tree type)
5402 {
5403   tree base_binfo;
5404   int i;
5405
5406   if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5407     return NULL_TREE;
5408
5409   if (! CLASS_INTERFACE (TYPE_NAME (type)))
5410     {
5411       if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5412         return lookup_cl (TYPE_NAME (type));
5413       return NULL_TREE;
5414     }
5415
5416   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5417     {
5418       if (BINFO_TYPE (base_binfo) != object_type_node
5419           && interface_of_p (type, BINFO_TYPE (base_binfo)))
5420         return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5421     }
5422   return NULL_TREE;
5423 }
5424
5425 void
5426 java_check_circular_reference (void)
5427 {
5428   tree current;
5429   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5430     {
5431       tree type = TREE_TYPE (current);
5432       tree cl;
5433
5434       cl = check_circular_reference (type);
5435       if (! cl)
5436         cl = check_inner_circular_reference (type, type);
5437       if (cl)
5438         parse_error_context (cl, "Cyclic class inheritance%s",
5439                              (cyclic_inheritance_report ?
5440                               cyclic_inheritance_report : ""));
5441     }
5442 }
5443
5444 /* Augment the parameter list PARM with parameters crafted to
5445    initialize outer context locals aliases. Through ARTIFICIAL, a
5446    count is kept of the number of crafted parameters. MODE governs
5447    what eventually gets created: something suitable for a function
5448    creation or a function invocation, either the constructor or
5449    finit$.  */
5450
5451 static tree
5452 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5453                                         int *artificial)
5454 {
5455   tree field;
5456   tree additional_parms = NULL_TREE;
5457
5458   for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5459     if (FIELD_LOCAL_ALIAS (field))
5460       {
5461         const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5462         tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5463         tree mangled_id;
5464
5465         switch (mode)
5466           {
5467           case AIPL_FUNCTION_DECLARATION:
5468             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5469                                                          &buffer [4]);
5470             purpose = build_wfl_node (mangled_id);
5471             if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5472               value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5473             else
5474               value = TREE_TYPE (field);
5475             break;
5476
5477           case AIPL_FUNCTION_CREATION:
5478             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5479                                                          &buffer [4]);
5480             value = TREE_TYPE (field);
5481             break;
5482
5483           case AIPL_FUNCTION_FINIT_INVOCATION:
5484             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5485                                                          &buffer [4]);
5486             /* Now, this is wrong. purpose should always be the NAME
5487                of something and value its matching value (decl, type,
5488                etc...) FIXME -- but there is a lot to fix. */
5489
5490             /* When invoked for this kind of operation, we already
5491                know whether a field is used or not. */
5492             purpose = TREE_TYPE (field);
5493             value = build_wfl_node (mangled_id);
5494             break;
5495
5496           case AIPL_FUNCTION_CTOR_INVOCATION:
5497             /* There are two case: the constructor invocation happens
5498                outside the local inner, in which case, locales from the outer
5499                context are directly used.
5500
5501                Otherwise, we fold to using the alias directly. */
5502             if (class_type == current_class)
5503               value = field;
5504             else
5505               {
5506                 name = get_identifier (&buffer[4]);
5507                 value = IDENTIFIER_LOCAL_VALUE (name);
5508               }
5509             break;
5510           }
5511         additional_parms = tree_cons (purpose, value, additional_parms);
5512         if (artificial)
5513           *artificial +=1;
5514       }
5515   if (additional_parms)
5516     {
5517       if (ANONYMOUS_CLASS_P (class_type)
5518           && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5519         additional_parms = nreverse (additional_parms);
5520       parm = chainon (additional_parms, parm);
5521     }
5522
5523    return parm;
5524 }
5525
5526 /* Craft a constructor for CLASS_DECL -- what we should do when none
5527    where found. ARGS is non NULL when a special signature must be
5528    enforced. This is the case for anonymous classes.  */
5529
5530 static tree
5531 craft_constructor (tree class_decl, tree args)
5532 {
5533   tree class_type = TREE_TYPE (class_decl);
5534   tree parm = NULL_TREE;
5535   /* Inherit access flags for the constructor from its enclosing class. */
5536   int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5537   int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5538   int i = 0, artificial = 0;
5539   tree decl, ctor_name;
5540   char buffer [80];
5541
5542   ctor_name = init_identifier_node;
5543
5544   /* If we're dealing with an inner class constructor, we hide the
5545      this$<n> decl in the name field of its parameter declaration. */
5546   if (PURE_INNER_CLASS_TYPE_P (class_type))
5547     {
5548       tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5549       parm = tree_cons (build_current_thisn (class_type),
5550                         build_pointer_type (type), parm);
5551
5552       /* Some more arguments to be hidden here. The values of the local
5553          variables of the outer context that the inner class needs to see. */
5554       parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5555                                                      class_type, parm,
5556                                                      &artificial);
5557     }
5558
5559   /* Then if there are any args to be enforced, enforce them now */
5560   for (; args && args != end_params_node; args = TREE_CHAIN (args))
5561     {
5562       /* If we see a `void *', we need to change it to Object.  */
5563       if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5564         TREE_VALUE (args) = object_ptr_type_node;
5565
5566       sprintf (buffer, "parm%d", i++);
5567       parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5568     }
5569
5570   CRAFTED_PARAM_LIST_FIXUP (parm);
5571   decl = create_artificial_method (class_type, flags, void_type_node,
5572                                    ctor_name, parm);
5573   fix_method_argument_names (parm, decl);
5574   /* Now, mark the artificial parameters. */
5575   DECL_FUNCTION_NAP (decl) = artificial;
5576   DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5577   DECL_INLINE (decl) = 1;
5578   return decl;
5579 }
5580
5581
5582 /* Fix the constructors. This will be called right after circular
5583    references have been checked. It is necessary to fix constructors
5584    early even if no code generation will take place for that class:
5585    some generated constructor might be required by the class whose
5586    compilation triggered this one to be simply loaded.  */
5587
5588 void
5589 java_fix_constructors (void)
5590 {
5591   tree current;
5592
5593   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5594     {
5595       tree class_type = TREE_TYPE (current);
5596       int saw_ctor = 0;
5597       tree decl;
5598
5599       if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5600         continue;
5601
5602       output_class = current_class = class_type;
5603       for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5604         {
5605           if (DECL_CONSTRUCTOR_P (decl))
5606             {
5607               fix_constructors (decl);
5608               saw_ctor = 1;
5609             }
5610         }
5611
5612       /* Anonymous class constructor can't be generated that early. */
5613       if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5614         craft_constructor (current, NULL_TREE);
5615     }
5616 }
5617
5618 /* safe_layout_class just makes sure that we can load a class without
5619    disrupting the current_class, input_file, input_line, etc, information
5620    about the class processed currently.  */
5621
5622 void
5623 safe_layout_class (tree class)
5624 {
5625   tree save_current_class = current_class;
5626   location_t save_location = input_location;
5627
5628   layout_class (class);
5629
5630   current_class = save_current_class;
5631   input_location = save_location;
5632 }
5633
5634 static tree
5635 jdep_resolve_class (jdep *dep)
5636 {
5637   tree decl;
5638
5639   if (JDEP_RESOLVED_P (dep))
5640     decl = JDEP_RESOLVED_DECL (dep);
5641   else
5642     {
5643       decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5644                             JDEP_DECL (dep), JDEP_WFL (dep));
5645       JDEP_RESOLVED (dep, decl);
5646       /* If there is no WFL, that's ok.  We generate this warning
5647          elsewhere.  */
5648       if (decl && JDEP_WFL (dep) != NULL_TREE)
5649         check_deprecation (JDEP_WFL (dep), decl);
5650     }
5651
5652   if (!decl)
5653     complete_class_report_errors (dep);
5654   else if (INNER_CLASS_DECL_P (decl))
5655     {
5656       tree inner = TREE_TYPE (decl);
5657       if (! CLASS_LOADED_P (inner))
5658         {
5659           safe_layout_class (inner);
5660           if (TYPE_SIZE (inner) == error_mark_node)
5661             TYPE_SIZE (inner) = NULL_TREE;
5662         }
5663       check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5664     }
5665   return decl;
5666 }
5667
5668 /* Complete unsatisfied class declaration and their dependencies */
5669
5670 void
5671 java_complete_class (void)
5672 {
5673   tree cclass;
5674   jdeplist *cclassd;
5675   int error_found;
5676   tree type;
5677
5678   /* Process imports */
5679   process_imports ();
5680
5681   /* Reverse things so we have the right order */
5682   ctxp->class_list = nreverse (ctxp->class_list);
5683   ctxp->classd_list = reverse_jdep_list (ctxp);
5684
5685   for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5686        cclass && cclassd;
5687        cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5688     {
5689       jdep *dep;
5690
5691       for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5692         {
5693           tree decl;
5694           if (!(decl = jdep_resolve_class (dep)))
5695             continue;
5696
5697           /* Now it's time to patch */
5698           switch (JDEP_KIND (dep))
5699             {
5700             case JDEP_SUPER:
5701               /* Simply patch super */
5702               if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5703                 continue;
5704               BINFO_TYPE (BINFO_BASE_BINFO
5705                           (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5706                 = TREE_TYPE (decl);
5707               break;
5708
5709             case JDEP_FIELD:
5710               {
5711                 /* We do part of the job done in add_field */
5712                 tree field_decl = JDEP_DECL (dep);
5713                 tree field_type = TREE_TYPE (decl);
5714                 if (TREE_CODE (field_type) == RECORD_TYPE)
5715                   field_type = promote_type (field_type);
5716                 TREE_TYPE (field_decl) = field_type;
5717                 DECL_ALIGN (field_decl) = 0;
5718                 DECL_USER_ALIGN (field_decl) = 0;
5719                 layout_decl (field_decl, 0);
5720                 SOURCE_FRONTEND_DEBUG
5721                   (("Completed field/var decl '%s' with '%s'",
5722                     IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5723                     IDENTIFIER_POINTER (DECL_NAME (decl))));
5724                 break;
5725               }
5726             case JDEP_METHOD:   /* We start patching a method */
5727             case JDEP_METHOD_RETURN:
5728               error_found = 0;
5729               while (1)
5730                 {
5731                   if (decl)
5732                     {
5733                       type = TREE_TYPE(decl);
5734                       if (TREE_CODE (type) == RECORD_TYPE)
5735                         type = promote_type (type);
5736                       JDEP_APPLY_PATCH (dep, type);
5737                       SOURCE_FRONTEND_DEBUG
5738                         (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5739                            "Completing fct '%s' with ret type '%s'":
5740                            "Completing arg '%s' with type '%s'"),
5741                           IDENTIFIER_POINTER (EXPR_WFL_NODE
5742                                               (JDEP_DECL_WFL (dep))),
5743                           IDENTIFIER_POINTER (DECL_NAME (decl))));
5744                     }
5745                   else
5746                     error_found = 1;
5747                   dep = JDEP_CHAIN (dep);
5748                   if (JDEP_KIND (dep) == JDEP_METHOD_END)
5749                     break;
5750                   else
5751                     decl = jdep_resolve_class (dep);
5752                 }
5753               if (!error_found)
5754                 {
5755                   tree mdecl = JDEP_DECL (dep), signature;
5756                   /* Recompute and reset the signature, check first that
5757                      all types are now defined. If they're not,
5758                      don't build the signature. */
5759                   if (check_method_types_complete (mdecl))
5760                     {
5761                       signature = build_java_signature (TREE_TYPE (mdecl));
5762                       set_java_signature (TREE_TYPE (mdecl), signature);
5763                     }
5764                 }
5765               else
5766                 continue;
5767               break;
5768
5769             case JDEP_INTERFACE:
5770               if (parser_check_super_interface (decl, JDEP_DECL (dep),
5771                                                 JDEP_WFL (dep)))
5772                 continue;
5773               parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5774               break;
5775
5776             case JDEP_PARM:
5777             case JDEP_VARIABLE:
5778               type = TREE_TYPE(decl);
5779               if (TREE_CODE (type) == RECORD_TYPE)
5780                 type = promote_type (type);
5781               JDEP_APPLY_PATCH (dep, type);
5782               break;
5783
5784             case JDEP_TYPE:
5785               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5786               SOURCE_FRONTEND_DEBUG
5787                 (("Completing a random type dependency on a '%s' node",
5788                   tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5789               break;
5790
5791             case JDEP_EXCEPTION:
5792               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5793               SOURCE_FRONTEND_DEBUG
5794                 (("Completing '%s' 'throws' argument node",
5795                   IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5796               break;
5797
5798             case JDEP_ANONYMOUS:
5799               patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5800               break;
5801
5802             default:
5803               abort ();
5804             }
5805         }
5806     }
5807   return;
5808 }
5809
5810 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5811    array.  */
5812
5813 static tree
5814 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5815 {
5816   tree tname = TYPE_NAME (class_type);
5817   tree resolved_type = TREE_TYPE (class_type);
5818   int array_dims = 0;
5819   tree resolved_type_decl;
5820
5821   if (resolved_type != NULL_TREE)
5822     {
5823       tree resolved_type_decl = TYPE_NAME (resolved_type);
5824       if (resolved_type_decl == NULL_TREE
5825           || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5826         {
5827           resolved_type_decl = build_decl (TYPE_DECL,
5828                                            TYPE_NAME (class_type),
5829                                            resolved_type);
5830         }
5831       return resolved_type_decl;
5832     }
5833
5834   /* 1- Check to see if we have an array. If true, find what we really
5835      want to resolve  */
5836   if ((array_dims = build_type_name_from_array_name (tname,
5837                                                      &TYPE_NAME (class_type))))
5838     WFL_STRIP_BRACKET (cl, cl);
5839
5840   /* 2- Resolve the bare type */
5841   if (!(resolved_type_decl = do_resolve_class (enclosing, NULL_TREE, class_type,
5842                                                decl, cl)))
5843     return NULL_TREE;
5844   resolved_type = TREE_TYPE (resolved_type_decl);
5845
5846   /* 3- If we have an array, reconstruct the array down to its nesting */
5847   if (array_dims)
5848     {
5849       for (; array_dims; array_dims--)
5850         resolved_type = build_java_array_type (resolved_type, -1);
5851       resolved_type_decl = TYPE_NAME (resolved_type);
5852     }
5853   TREE_TYPE (class_type) = resolved_type;
5854   return resolved_type_decl;
5855 }
5856
5857 /* Effectively perform the resolution of class CLASS_TYPE.  DECL or CL
5858    are used to report error messages; CL must either be NULL_TREE or a
5859    WFL wrapping a class.  Do not try to replace TYPE_NAME (class_type)
5860    by a variable, since it is changed by find_in_imports{_on_demand}
5861    and (but it doesn't really matter) qualify_and_find.  */
5862
5863 tree
5864 do_resolve_class (tree enclosing, tree import_type, tree class_type, tree decl,
5865                   tree cl)
5866 {
5867   tree new_class_decl = NULL_TREE, super = NULL_TREE;
5868   tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5869   tree decl_result;
5870   htab_t circularity_hash;
5871
5872   if (QUALIFIED_P (TYPE_NAME (class_type)))
5873     {
5874       /* If the type name is of the form `Q . Id', then Q is either a
5875          package name or a class name.  First we try to find Q as a
5876          class and then treat Id as a member type.  If we can't find Q
5877          as a class then we fall through.  */
5878       tree q, left, left_type, right;
5879       if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5880         {
5881           BUILD_PTR_FROM_NAME (left_type, left);
5882           q = do_resolve_class (enclosing, import_type, left_type, decl, cl);
5883           if (q)
5884             {
5885               enclosing = q;
5886               saved_enclosing_type = TREE_TYPE (q);
5887               BUILD_PTR_FROM_NAME (class_type, right);
5888             }
5889         }
5890     }
5891
5892   if (enclosing)
5893     {
5894       /* This hash table is used to register the classes we're going
5895          through when searching the current class as an inner class, in
5896          order to detect circular references. Remember to free it before
5897          returning the section 0- of this function. */
5898       circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5899                                       NULL);
5900
5901       /* 0- Search in the current class as an inner class.
5902          Maybe some code here should be added to load the class or
5903          something, at least if the class isn't an inner class and ended
5904          being loaded from class file. FIXME. */
5905       while (enclosing)
5906         {
5907           new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5908                                                 &super, class_type);
5909           if (new_class_decl)
5910             break;
5911
5912           /* If we haven't found anything because SUPER reached Object and
5913              ENCLOSING happens to be an innerclass, try the enclosing context. */
5914           if ((!super || super == object_type_node) &&
5915               enclosing && INNER_CLASS_DECL_P (enclosing))
5916             enclosing = DECL_CONTEXT (enclosing);
5917           else
5918             enclosing = NULL_TREE;
5919         }
5920
5921       htab_delete (circularity_hash);
5922
5923       if (new_class_decl)
5924         return new_class_decl;
5925     }
5926
5927   /* 1- Check for the type in single imports.  Look at enclosing classes and,
5928      if we're laying out a superclass, at the import list for the subclass.
5929      This will change TYPE_NAME() if something relevant is found. */
5930   if (import_type && TYPE_IMPORT_LIST (import_type))
5931     find_in_imports (import_type, class_type);
5932   find_in_imports (saved_enclosing_type, class_type);
5933
5934   /* 2- And check for the type in the current compilation unit */
5935   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5936     {
5937       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5938         load_class (TYPE_NAME (class_type), 0);
5939       return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5940     }
5941
5942   /* 3- Search according to the current package definition */
5943   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5944     {
5945       if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5946                                              TYPE_NAME (class_type))))
5947         return new_class_decl;
5948     }
5949
5950   /* 4- Check the import on demands. Don't allow bar.baz to be
5951      imported from foo.* */
5952   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5953     {
5954       if (import_type
5955           && TYPE_IMPORT_DEMAND_LIST (import_type)
5956           && find_in_imports_on_demand (import_type, class_type))
5957         return NULL_TREE;
5958       if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5959         return NULL_TREE;
5960     }
5961
5962   /* If found in find_in_imports_on_demand, the type has already been
5963      loaded. */
5964   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5965     return new_class_decl;
5966
5967   /* 5- Check another compilation unit that bears the name of type */
5968   load_class (TYPE_NAME (class_type), 0);
5969
5970   if (!cl)
5971     cl = lookup_cl (decl);
5972
5973   /* If we don't have a value for CL, then we're being called recursively.
5974      We can't check package access just yet, but it will be taken care of
5975      by the caller. */
5976   if (cl)
5977     {
5978       if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
5979         return NULL_TREE;
5980     }
5981
5982   /* 6- Last call for a resolution */
5983   decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5984
5985   /* The final lookup might have registered a.b.c into a.b$c If we
5986      failed at the first lookup, progressively change the name if
5987      applicable and use the matching DECL instead. */
5988   if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5989     {
5990       char *separator;
5991       tree name = TYPE_NAME (class_type);
5992       char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5993
5994       strcpy (namebuffer, IDENTIFIER_POINTER (name));
5995
5996       do {
5997
5998        /* Reach the last '.', and if applicable, replace it by a `$' and
5999           see if this exists as a type. */
6000        if ((separator = strrchr (namebuffer, '.')))
6001          {
6002            *separator = '$';
6003            name = get_identifier (namebuffer);
6004            decl_result = IDENTIFIER_CLASS_VALUE (name);
6005          }
6006       } while (!decl_result && separator);
6007     }
6008   return decl_result;
6009 }
6010
6011 static tree
6012 qualify_and_find (tree class_type, tree package, tree name)
6013 {
6014   tree new_qualified = merge_qualified_name (package, name);
6015   tree new_class_decl;
6016
6017   if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6018     load_class (new_qualified, 0);
6019   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6020     {
6021       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6022         load_class (TREE_TYPE (new_class_decl), 0);
6023       TYPE_NAME (class_type) = new_qualified;
6024       return IDENTIFIER_CLASS_VALUE (new_qualified);
6025     }
6026   return NULL_TREE;
6027 }
6028
6029 /* Resolve NAME and lay it out (if not done and if not the current
6030    parsed class). Return a decl node. This function is meant to be
6031    called when type resolution is necessary during the walk pass.  */
6032
6033 static tree
6034 resolve_and_layout (tree something, tree cl)
6035 {
6036   tree decl, decl_type;
6037
6038   /* Don't do that on the current class */
6039   if (something == current_class)
6040     return TYPE_NAME (current_class);
6041
6042   /* Don't do anything for void and other primitive types */
6043   if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6044     return NULL_TREE;
6045
6046   /* Pointer types can be reall pointer types or fake pointers. When
6047      finding a real pointer, recheck for primitive types */
6048   if (TREE_CODE (something) == POINTER_TYPE)
6049     {
6050       if (TREE_TYPE (something))
6051         {
6052           something = TREE_TYPE (something);
6053           if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6054             return NULL_TREE;
6055         }
6056       else
6057         something = TYPE_NAME (something);
6058     }
6059
6060   /* Don't do anything for arrays of primitive types */
6061   if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6062       && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6063     return NULL_TREE;
6064
6065   /* Something might be a WFL */
6066   if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6067     something = EXPR_WFL_NODE (something);
6068
6069   /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6070      TYPE_DECL or a real TYPE */
6071   else if (TREE_CODE (something) != IDENTIFIER_NODE)
6072     something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6073             DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6074
6075   if (!(decl = resolve_no_layout (something, cl)))
6076     return NULL_TREE;
6077
6078   /* Resolve and layout if necessary */
6079   decl_type = TREE_TYPE (decl);
6080   layout_class_methods (decl_type);
6081   /* Check methods */
6082   if (CLASS_FROM_SOURCE_P (decl_type))
6083     java_check_methods (decl);
6084   /* Layout the type if necessary */
6085   if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6086     safe_layout_class (decl_type);
6087
6088   return decl;
6089 }
6090
6091 /* Resolve a class, returns its decl but doesn't perform any
6092    layout. The current parsing context is saved and restored */
6093
6094 static tree
6095 resolve_no_layout (tree name, tree cl)
6096 {
6097   tree ptr, decl;
6098   BUILD_PTR_FROM_NAME (ptr, name);
6099   java_parser_context_save_global ();
6100   decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6101   java_parser_context_restore_global ();
6102
6103   return decl;
6104 }
6105
6106 /* Called when reporting errors. Skip the '[]'s in a complex array
6107    type description that failed to be resolved. purify_type_name can't
6108    use an identifier tree.  */
6109
6110 static const char *
6111 purify_type_name (const char *name)
6112 {
6113   int len = strlen (name);
6114   int bracket_found;
6115
6116   STRING_STRIP_BRACKETS (name, len, bracket_found);
6117   if (bracket_found)
6118     {
6119       char *stripped_name = xmemdup (name, len, len+1);
6120       stripped_name [len] = '\0';
6121       return stripped_name;
6122     }
6123   return name;
6124 }
6125
6126 /* The type CURRENT refers to can't be found. We print error messages.  */
6127
6128 static void
6129 complete_class_report_errors (jdep *dep)
6130 {
6131   const char *name;
6132
6133   if (!JDEP_WFL (dep))
6134     return;
6135
6136   name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6137   switch (JDEP_KIND (dep))
6138     {
6139     case JDEP_SUPER:
6140       parse_error_context
6141         (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6142          purify_type_name (name),
6143          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6144       break;
6145     case JDEP_FIELD:
6146       parse_error_context
6147         (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6148          purify_type_name (name),
6149          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6150       break;
6151     case JDEP_METHOD:           /* Covers arguments */
6152       parse_error_context
6153         (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6154          purify_type_name (name),
6155          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6156          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6157       break;
6158     case JDEP_METHOD_RETURN:    /* Covers return type */
6159       parse_error_context
6160         (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6161          purify_type_name (name),
6162          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6163       break;
6164     case JDEP_INTERFACE:
6165       parse_error_context
6166         (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6167          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6168          (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6169          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6170       break;
6171     case JDEP_VARIABLE:
6172       parse_error_context
6173         (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6174          purify_type_name (IDENTIFIER_POINTER
6175                            (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6176          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6177       break;
6178     case JDEP_EXCEPTION:        /* As specified by `throws' */
6179       parse_error_context
6180           (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6181          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6182       break;
6183     default:
6184       /* Fix for -Wall. Just break doing nothing. The error will be
6185          caught later */
6186       break;
6187     }
6188 }
6189
6190 /* Return a static string containing the DECL prototype string. If
6191    DECL is a constructor, use the class name instead of the form
6192    <init> */
6193
6194 static const char *
6195 get_printable_method_name (tree decl)
6196 {
6197   const char *to_return;
6198   tree name = NULL_TREE;
6199
6200   if (DECL_CONSTRUCTOR_P (decl))
6201     {
6202       name = DECL_NAME (decl);
6203       DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6204     }
6205
6206   to_return = lang_printable_name (decl, 2);
6207   if (DECL_CONSTRUCTOR_P (decl))
6208     DECL_NAME (decl) = name;
6209
6210   return to_return;
6211 }
6212
6213 /* Track method being redefined inside the same class. As a side
6214    effect, set DECL_NAME to an IDENTIFIER (prior entering this
6215    function it's a FWL, so we can track errors more accurately.)  */
6216
6217 static int
6218 check_method_redefinition (tree class, tree method)
6219 {
6220   tree redef, sig;
6221
6222   /* There's no need to verify <clinit> and finit$ and instinit$ */
6223   if (DECL_CLINIT_P (method)
6224       || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6225     return 0;
6226
6227   sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6228   for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6229     {
6230       if (redef == method)
6231         break;
6232       if (DECL_NAME (redef) == DECL_NAME (method)
6233           && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6234           && !DECL_ARTIFICIAL (method))
6235         {
6236           parse_error_context
6237             (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6238              (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6239              get_printable_method_name (redef));
6240           return 1;
6241         }
6242     }
6243   return 0;
6244 }
6245
6246 /* Return 1 if check went ok, 0 otherwise.  */
6247 static int
6248 check_abstract_method_definitions (int do_interface, tree class_decl,
6249                                    tree type)
6250 {
6251   tree class = TREE_TYPE (class_decl);
6252   tree method, end_type;
6253   int ok = 1;
6254
6255   end_type = (do_interface ? object_type_node : type);
6256   for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6257     {
6258       tree other_super, other_method, method_sig, method_name;
6259       int found = 0;
6260       int end_type_reached = 0;
6261
6262       if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6263         continue;
6264
6265       /* Now verify that somewhere in between TYPE and CLASS,
6266          abstract method METHOD gets a non abstract definition
6267          that is inherited by CLASS.  */
6268
6269       method_sig = build_java_signature (TREE_TYPE (method));
6270       method_name = DECL_NAME (method);
6271       if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6272         method_name = EXPR_WFL_NODE (method_name);
6273
6274       other_super = class;
6275       do {
6276         if (other_super == end_type)
6277           end_type_reached = 1;
6278
6279         /* Method search */
6280         for (other_method = TYPE_METHODS (other_super); other_method;
6281             other_method = TREE_CHAIN (other_method))
6282           {
6283             tree s = build_java_signature (TREE_TYPE (other_method));
6284             tree other_name = DECL_NAME (other_method);
6285
6286             if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6287               other_name = EXPR_WFL_NODE (other_name);
6288             if (!DECL_CLINIT_P (other_method)
6289                 && !DECL_CONSTRUCTOR_P (other_method)
6290                 && method_name == other_name
6291                 && method_sig == s
6292                 && !METHOD_ABSTRACT (other_method))
6293              {
6294                found = 1;
6295                break;
6296              }
6297           }
6298         other_super = CLASSTYPE_SUPER (other_super);
6299       } while (!end_type_reached);
6300
6301       /* Report that abstract METHOD didn't find an implementation
6302          that CLASS can use. */
6303       if (!found)
6304         {
6305           char *t = xstrdup (lang_printable_name
6306                             (TREE_TYPE (TREE_TYPE (method)), 0));
6307           tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6308
6309           parse_error_context
6310             (lookup_cl (class_decl),
6311              "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",
6312              IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6313              t, lang_printable_name (method, 2),
6314              (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6315               "interface" : "class"),
6316              IDENTIFIER_POINTER (ccn),
6317              (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6318              IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6319           ok = 0;
6320           free (t);
6321         }
6322     }
6323
6324   if (ok && do_interface)
6325     {
6326       /* Check for implemented interfaces. */
6327       int i;
6328       tree base_binfo;
6329       
6330       for (i = 1;
6331            ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6332            i++)
6333         ok = check_abstract_method_definitions (1, class_decl,
6334                                                 BINFO_TYPE (base_binfo));
6335     }
6336
6337   return ok;
6338 }
6339
6340 /* Check that CLASS_DECL somehow implements all inherited abstract
6341    methods.  */
6342
6343 static void
6344 java_check_abstract_method_definitions (tree class_decl)
6345 {
6346   tree class = TREE_TYPE (class_decl);
6347   tree super, base_binfo;
6348   int i;
6349
6350   if (CLASS_ABSTRACT (class_decl))
6351     return;
6352
6353   /* Check for inherited types */
6354   super = class;
6355   do {
6356     super = CLASSTYPE_SUPER (super);
6357     check_abstract_method_definitions (0, class_decl, super);
6358   } while (super != object_type_node);
6359
6360   /* Check for implemented interfaces. */
6361   for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6362     check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6363 }
6364
6365 /* Check all the types method DECL uses and return 1 if all of them
6366    are now complete, 0 otherwise. This is used to check whether its
6367    safe to build a method signature or not.  */
6368
6369 static int
6370 check_method_types_complete (tree decl)
6371 {
6372   tree type = TREE_TYPE (decl);
6373   tree args;
6374
6375   if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6376     return 0;
6377
6378   args = TYPE_ARG_TYPES (type);
6379   if (TREE_CODE (type) == METHOD_TYPE)
6380     args = TREE_CHAIN (args);
6381   for (; args != end_params_node; args = TREE_CHAIN (args))
6382     if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6383       return 0;
6384
6385   return 1;
6386 }
6387
6388 /* Visible interface to check methods contained in CLASS_DECL */
6389
6390 void
6391 java_check_methods (tree class_decl)
6392 {
6393   if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6394     return;
6395
6396   if (CLASS_INTERFACE (class_decl))
6397     java_check_abstract_methods (class_decl);
6398   else
6399     java_check_regular_methods (class_decl);
6400
6401   CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6402 }
6403
6404 /* Like not_accessible_p, but doesn't refer to the current class at
6405    all.  */
6406 static bool
6407 hack_is_accessible_p (tree member, tree from_where)
6408 {
6409   int flags = get_access_flags_from_decl (member);
6410
6411   if (from_where == DECL_CONTEXT (member)
6412       || (flags & ACC_PUBLIC))
6413     return true;
6414
6415   if ((flags & ACC_PROTECTED))
6416     {
6417       if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6418         return true;
6419     }
6420
6421   if ((flags & ACC_PRIVATE))
6422     return false;
6423
6424   /* Package private, or protected.  */
6425   return in_same_package (TYPE_NAME (from_where),
6426                           TYPE_NAME (DECL_CONTEXT (member)));
6427 }
6428
6429 /* Check all the methods of CLASS_DECL. Methods are first completed
6430    then checked according to regular method existence rules.  If no
6431    constructor for CLASS_DECL were encountered, then build its
6432    declaration.  */
6433 static void
6434 java_check_regular_methods (tree class_decl)
6435 {
6436   int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6437   tree method;
6438   tree class = TREE_TYPE (class_decl);
6439   tree found = NULL_TREE;
6440   tree mthrows;
6441
6442   /* It is not necessary to check methods defined in java.lang.Object */
6443   if (class == object_type_node)
6444     return;
6445
6446   if (!TYPE_NVIRTUALS (class))
6447     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6448
6449   /* Should take interfaces into account. FIXME */
6450   for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6451     {
6452       tree sig;
6453       tree method_wfl = DECL_FUNCTION_WFL (method);
6454       int aflags;
6455
6456       /* Check for redefinitions */
6457       if (check_method_redefinition (class, method))
6458         continue;
6459
6460       /* We verify things thrown by the method.  They must inherit from
6461          java.lang.Throwable.  */
6462       for (mthrows = DECL_FUNCTION_THROWS (method);
6463            mthrows; mthrows = TREE_CHAIN (mthrows))
6464         {
6465           if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6466             parse_error_context
6467               (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6468                IDENTIFIER_POINTER
6469                  (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6470         }
6471
6472       /* If we see one constructor a mark so we don't generate the
6473          default one.  Also skip other verifications: constructors
6474          can't be inherited hence hidden or overridden.  */
6475       if (DECL_CONSTRUCTOR_P (method))
6476         {
6477           saw_constructor = 1;
6478           continue;
6479         }
6480
6481       sig = build_java_argument_signature (TREE_TYPE (method));
6482       found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6483                                               SEARCH_SUPER | SEARCH_INTERFACE);
6484
6485       /* Inner class can't declare static methods */
6486       if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6487         {
6488           char *t = xstrdup (lang_printable_name (class, 0));
6489           parse_error_context
6490             (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6491              lang_printable_name (method, 2), t);
6492           free (t);
6493         }
6494
6495       /* Nothing overrides or it's a private method. */
6496       if (!found)
6497         continue;
6498       if (METHOD_PRIVATE (found))
6499         {
6500           found = NULL_TREE;
6501           continue;
6502         }
6503
6504       /* If `found' is declared in an interface, make sure the
6505          modifier matches. */
6506       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6507           && clinit_identifier_node != DECL_NAME (found)
6508           && !METHOD_PUBLIC (method))
6509         {
6510           tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6511           parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6512                                IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6513                                lang_printable_name (method, 2),
6514                                IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6515         }
6516
6517       /* Can't override a method with the same name and different return
6518          types. */
6519       if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6520         {
6521           char *t = xstrdup
6522             (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6523           parse_error_context
6524             (method_wfl,
6525              "Method %qs was defined with return type %qs in class %qs",
6526              lang_printable_name (found, 2), t,
6527              IDENTIFIER_POINTER
6528                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6529           free (t);
6530         }
6531
6532       aflags = get_access_flags_from_decl (found);
6533
6534       /* Can't override final. Can't override static. */
6535       if (METHOD_FINAL (found) || METHOD_STATIC (found))
6536         {
6537           /* Static *can* override static */
6538           if (METHOD_STATIC (found) && METHOD_STATIC (method))
6539             continue;
6540           parse_error_context
6541             (method_wfl,
6542              "%s methods can't be overridden. Method %qs is %s in class %qs",
6543              (METHOD_FINAL (found) ? "Final" : "Static"),
6544              lang_printable_name (found, 2),
6545              (METHOD_FINAL (found) ? "final" : "static"),
6546              IDENTIFIER_POINTER
6547                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6548           continue;
6549         }
6550
6551       /* Static method can't override instance method. */
6552       if (METHOD_STATIC (method))
6553         {
6554           parse_error_context
6555             (method_wfl,
6556              "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6557              lang_printable_name (found, 2),
6558              IDENTIFIER_POINTER
6559                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6560           continue;
6561         }
6562
6563       /* - Overriding/hiding public must be public
6564          - Overriding/hiding protected must be protected or public
6565          - If the overridden or hidden method has default (package)
6566            access, then the overriding or hiding method must not be
6567            private; otherwise, a compile-time error occurs.  If
6568            `found' belongs to an interface, things have been already
6569            taken care of.  */
6570       if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6571           && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6572               || (METHOD_PROTECTED (found)
6573                   && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6574               || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6575                   && METHOD_PRIVATE (method))))
6576         {
6577           parse_error_context
6578             (method_wfl,
6579              "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6580              (METHOD_PUBLIC (method) ? "public" :
6581               (METHOD_PRIVATE (method) ? "private" : "protected")),
6582              IDENTIFIER_POINTER (DECL_NAME
6583                                  (TYPE_NAME (DECL_CONTEXT (found)))));
6584           continue;
6585         }
6586
6587       /* Check this method against all the other implementations it
6588          overrides.  Here we only check the class hierarchy; the rest
6589          of the checking is done later.  If this method is just a
6590          Miranda method, we can skip the check.  */
6591       if (! METHOD_INVISIBLE (method))
6592         check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6593     }
6594
6595   /* The above throws clause check only looked at superclasses.  Now
6596      we must also make sure that all methods declared in interfaces
6597      have compatible throws clauses.  FIXME: there are more efficient
6598      ways to organize this checking; we should implement one.  */
6599   check_interface_throws_clauses (class, class);
6600
6601   if (!TYPE_NVIRTUALS (class))
6602     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6603
6604   /* Search for inherited abstract method not yet implemented in this
6605      class.  */
6606   java_check_abstract_method_definitions (class_decl);
6607
6608   if (!saw_constructor)
6609     abort ();
6610 }
6611
6612 /* Check to make sure that all the methods in all the interfaces
6613    implemented by CLASS_DECL are compatible with the concrete
6614    implementations available in CHECK_CLASS_DECL.  */
6615 static void
6616 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6617 {
6618   for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6619     {
6620       int i;
6621
6622       if (! CLASS_LOADED_P (class_decl))
6623         {
6624           if (CLASS_FROM_SOURCE_P (class_decl))
6625             safe_layout_class (class_decl);
6626           else
6627             load_class (class_decl, 1);
6628         }
6629
6630       for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6631         {
6632           tree interface
6633             = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6634           tree iface_method;
6635
6636           for (iface_method = TYPE_METHODS (interface);
6637                iface_method != NULL_TREE;
6638                iface_method = TREE_CHAIN (iface_method))
6639             {
6640               tree sig, method;
6641
6642               /* First look for a concrete method implemented or
6643                  inherited by this class.  No need to search
6644                  interfaces here, since we're already looking through
6645                  all of them.  */
6646               sig = build_java_argument_signature (TREE_TYPE (iface_method));
6647               method
6648                 = lookup_argument_method_generic (check_class_decl,
6649                                                   DECL_NAME (iface_method),
6650                                                   sig, SEARCH_VISIBLE);
6651               /* If we don't find an implementation, that is ok.  Any
6652                  potential errors from that are diagnosed elsewhere.
6653                  Also, multiple inheritance with conflicting throws
6654                  clauses is fine in the absence of a concrete
6655                  implementation.  */
6656               if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6657                   && !METHOD_INVISIBLE (iface_method))
6658                 {
6659                   tree method_wfl = DECL_FUNCTION_WFL (method);
6660                   check_throws_clauses (method, method_wfl, iface_method);
6661                 }
6662             }
6663
6664           /* Now check superinterfaces.  */
6665           check_interface_throws_clauses (check_class_decl, interface);
6666         }
6667     }
6668 }
6669
6670 /* Check throws clauses of a method against the clauses of all the
6671    methods it overrides.  We do this by searching up the class
6672    hierarchy, examining all matching accessible methods.  */
6673 static void
6674 check_concrete_throws_clauses (tree class, tree self_method,
6675                                tree name, tree signature)
6676 {
6677   tree method = lookup_argument_method_generic (class, name, signature,
6678                                                 SEARCH_SUPER | SEARCH_VISIBLE);
6679   while (method != NULL_TREE)
6680     {
6681       if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6682         check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6683                               method);
6684
6685       method = lookup_argument_method_generic (DECL_CONTEXT (method),
6686                                                name, signature,
6687                                                SEARCH_SUPER | SEARCH_VISIBLE);
6688     }
6689 }
6690
6691 /* Generate an error if the `throws' clause of METHOD (if any) is
6692    incompatible with the `throws' clause of FOUND (if any).  */
6693 static void
6694 check_throws_clauses (tree method, tree method_wfl, tree found)
6695 {
6696   tree mthrows;
6697
6698   for (mthrows = DECL_FUNCTION_THROWS (method);
6699        mthrows; mthrows = TREE_CHAIN (mthrows))
6700     {
6701       tree fthrows;
6702
6703       /* We don't verify unchecked expressions */
6704       if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6705         continue;
6706       /* Checked expression must be compatible */
6707       for (fthrows = DECL_FUNCTION_THROWS (found);
6708            fthrows; fthrows = TREE_CHAIN (fthrows))
6709         {
6710           if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6711             break;
6712         }
6713       if (!fthrows)
6714         {
6715           parse_error_context
6716             (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",
6717              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6718              lang_printable_name (found, 2),
6719              IDENTIFIER_POINTER
6720              (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6721         }
6722     }
6723 }
6724
6725 /* Check abstract method of interface INTERFACE */
6726 static void
6727 java_check_abstract_methods (tree interface_decl)
6728 {
6729   int i;
6730   tree method, found;
6731   tree interface = TREE_TYPE (interface_decl);
6732   tree base_binfo;
6733
6734   for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6735     {
6736       /* 2- Check for double definition inside the defining interface */
6737       if (check_method_redefinition (interface, method))
6738         continue;
6739
6740       /* 3- Overriding is OK as far as we preserve the return type.  */
6741       found = lookup_java_interface_method2 (interface, method);
6742       if (found)
6743         {
6744           char *t;
6745           t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6746           parse_error_context
6747             (DECL_FUNCTION_WFL (found),
6748              "Method %qs was defined with return type %qs in class %qs",
6749              lang_printable_name (found, 2), t,
6750              IDENTIFIER_POINTER
6751                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6752           free (t);
6753           continue;
6754         }
6755     }
6756
6757   /* 4- Inherited methods can't differ by their returned types */
6758   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6759     {
6760       tree sub_interface_method, sub_interface;
6761
6762       sub_interface = BINFO_TYPE (base_binfo);
6763       for (sub_interface_method = TYPE_METHODS (sub_interface);
6764            sub_interface_method;
6765            sub_interface_method = TREE_CHAIN (sub_interface_method))
6766         {
6767           found = lookup_java_interface_method2 (interface,
6768                                                  sub_interface_method);
6769           if (found && (found != sub_interface_method))
6770             {
6771               parse_error_context
6772                 (lookup_cl (sub_interface_method),
6773                  "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6774                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6775                  lang_printable_name (found, 2),
6776                  IDENTIFIER_POINTER
6777                    (DECL_NAME (TYPE_NAME
6778                                (DECL_CONTEXT (sub_interface_method)))),
6779                  IDENTIFIER_POINTER
6780                    (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6781             }
6782         }
6783     }
6784 }
6785
6786 /* Lookup methods in interfaces using their name and partial
6787    signature. Return a matching method only if their types differ.  */
6788
6789 static tree
6790 lookup_java_interface_method2 (tree class, tree method_decl)
6791 {
6792   int i;
6793   tree base_binfo;
6794   tree to_return;
6795
6796   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6797     {
6798       if ((BINFO_TYPE (base_binfo) != object_type_node)
6799           && (to_return =
6800               lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6801         return to_return;
6802     }
6803   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6804     {
6805       to_return = lookup_java_interface_method2
6806         (BINFO_TYPE (base_binfo), method_decl);
6807       if (to_return)
6808         return to_return;
6809     }
6810
6811   return NULL_TREE;
6812 }
6813
6814 /* Lookup method using their name and partial signature. Return a
6815    matching method only if their types differ.  */
6816
6817 static tree
6818 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6819 {
6820   tree method, method_signature, method_name, method_type, name;
6821
6822   method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6823   name = DECL_NAME (method_decl);
6824   method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6825                  EXPR_WFL_NODE (name) : name);
6826   method_type = TREE_TYPE (TREE_TYPE (method_decl));
6827
6828   while (clas != NULL_TREE)
6829     {
6830       for (method = TYPE_METHODS (clas);
6831            method != NULL_TREE;  method = TREE_CHAIN (method))
6832         {
6833           tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6834           tree name = DECL_NAME (method);
6835           if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6836                EXPR_WFL_NODE (name) : name) == method_name
6837               && method_sig == method_signature
6838               && TREE_TYPE (TREE_TYPE (method)) != method_type)
6839             return method;
6840         }
6841       clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6842     }
6843   return NULL_TREE;
6844 }
6845
6846 /* Return the line that matches DECL line number, and try its best to
6847    position the column number. Used during error reports.
6848    FUTURE/FIXME: return source_location instead of node. */
6849
6850 static GTY(()) tree cl_v;
6851 static tree
6852 lookup_cl (tree decl)
6853 {
6854 #ifndef USE_MAPPED_LOCATION
6855   char *line, *found;
6856 #endif
6857
6858   if (!decl)
6859     return NULL_TREE;
6860
6861   if (cl_v == NULL_TREE)
6862     {
6863       cl_v = build_unknown_wfl (NULL_TREE);
6864     }
6865
6866 #ifdef USE_MAPPED_LOCATION
6867   SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6868 #else
6869   EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6870   EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6871
6872   line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6873                             EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6874
6875   found = strstr ((const char *)line,
6876                   (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6877   if (found)
6878     EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6879 #endif
6880
6881   return cl_v;
6882 }
6883
6884 /* Look for a simple name in the single-type import list */
6885
6886 static tree
6887 find_name_in_single_imports (tree name)
6888 {
6889   tree node;
6890
6891   for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6892     if (TREE_VALUE (node) == name)
6893       return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6894
6895   return NULL_TREE;
6896 }
6897
6898 /* Process all single-type import. */
6899
6900 static int
6901 process_imports (void)
6902 {
6903   tree import;
6904   int error_found;
6905
6906   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6907     {
6908       tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6909       char *original_name;
6910
6911       /* Don't load twice something already defined. */
6912       if (IDENTIFIER_CLASS_VALUE (to_be_found))
6913         continue;
6914
6915       original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6916                                IDENTIFIER_LENGTH (to_be_found),
6917                                IDENTIFIER_LENGTH (to_be_found) + 1);
6918
6919       while (1)
6920         {
6921           tree left;
6922
6923           QUALIFIED_P (to_be_found) = 1;
6924           load_class (to_be_found, 0);
6925           error_found =
6926             check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6927
6928           /* We found it, we can bail out */
6929           if (IDENTIFIER_CLASS_VALUE (to_be_found))
6930             {
6931               check_deprecation (TREE_PURPOSE (import),
6932                                  IDENTIFIER_CLASS_VALUE (to_be_found));
6933               break;
6934             }
6935
6936           /* We haven't found it. Maybe we're trying to access an
6937              inner class.  The only way for us to know is to try again
6938              after having dropped a qualifier. If we can't break it further,
6939              we have an error. */
6940           if (split_qualified_name (&left, NULL, to_be_found))
6941             break;
6942
6943           to_be_found = left;
6944         }
6945       if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6946         {
6947           parse_error_context (TREE_PURPOSE (import),
6948                                "Class or interface %qs not found in import",
6949                                original_name);
6950           error_found = 1;
6951         }
6952
6953       free (original_name);
6954       if (error_found)
6955         return 1;
6956     }
6957   return 0;
6958 }
6959
6960 /* Possibly find and mark a class imported by a single-type import
6961    statement.  */
6962
6963 static void
6964 find_in_imports (tree enclosing_type, tree class_type)
6965 {
6966   tree import;
6967   if (enclosing_type && TYPE_IMPORT_LIST (enclosing_type))
6968     import = TYPE_IMPORT_LIST (enclosing_type);
6969   else
6970     import = ctxp->import_list;
6971
6972   while (import)
6973     {
6974       if (TREE_VALUE (import) == TYPE_NAME (class_type))
6975         {
6976           TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6977           QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6978           return;
6979         }
6980       import = TREE_CHAIN (import);
6981     }
6982 }
6983
6984 static int
6985 note_possible_classname (const char *name, int len)
6986 {
6987   tree node;
6988   if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6989     len = len - 5;
6990   else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6991     len = len - 6;
6992   else
6993     return 0;
6994   node = ident_subst (name, len, "", '/', '.', "");
6995   IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6996   QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6997   return 1;
6998 }
6999
7000 /* Read a import directory, gathering potential match for further type
7001    references. Indifferently reads a filesystem or a ZIP archive
7002    directory.  */
7003
7004 static void
7005 read_import_dir (tree wfl)
7006 {
7007   tree package_id = EXPR_WFL_NODE (wfl);
7008   const char *package_name = IDENTIFIER_POINTER (package_id);
7009   int package_length = IDENTIFIER_LENGTH (package_id);
7010   DIR *dirp = NULL;
7011   JCF *saved_jcf = current_jcf;
7012
7013   int found = 0;
7014   int k;
7015   void *entry;
7016   struct buffer filename[1];
7017
7018   if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7019     return;
7020   IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7021
7022   BUFFER_INIT (filename);
7023   buffer_grow (filename, package_length + 100);
7024
7025   for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7026     {
7027       const char *entry_name = jcf_path_name (entry);
7028       int entry_length = strlen (entry_name);
7029       if (jcf_path_is_zipfile (entry))
7030         {
7031           ZipFile *zipf;
7032           buffer_grow (filename, entry_length);
7033           memcpy (filename->data, entry_name, entry_length - 1);
7034           filename->data[entry_length-1] = '\0';
7035           zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7036           if (zipf == NULL)
7037             error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7038           else
7039             {
7040               ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7041               BUFFER_RESET (filename);
7042               for (k = 0; k < package_length; k++)
7043                 {
7044                   char ch = package_name[k];
7045                   *filename->ptr++ = ch == '.' ? '/' : ch;
7046                 }
7047               *filename->ptr++ = '/';
7048
7049               for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
7050                 {
7051                   const char *current_entry = ZIPDIR_FILENAME (zipd);
7052                   int current_entry_len = zipd->filename_length;
7053
7054                   if (current_entry_len >= BUFFER_LENGTH (filename)
7055                       && strncmp ((const char *) filename->data, current_entry,
7056                                   BUFFER_LENGTH (filename)) != 0)
7057                     continue;
7058                   found |= note_possible_classname (current_entry,
7059                                                     current_entry_len);
7060                 }
7061             }
7062         }
7063       else
7064         {
7065           BUFFER_RESET (filename);
7066           buffer_grow (filename, entry_length + package_length + 4);
7067           strcpy ((char *) filename->data, entry_name);
7068           filename->ptr = filename->data + entry_length;
7069           for (k = 0; k < package_length; k++)
7070             {
7071               char ch = package_name[k];
7072               *filename->ptr++ = ch == '.' ? '/' : ch;
7073             }
7074           *filename->ptr = '\0';
7075
7076           dirp = opendir ((const char *) filename->data);
7077           if (dirp == NULL)
7078             continue;
7079           *filename->ptr++ = '/';
7080           for (;;)
7081             {
7082               int len;
7083               const char *d_name;
7084               struct dirent *direntp = readdir (dirp);
7085               if (!direntp)
7086                 break;
7087               d_name = direntp->d_name;
7088               len = strlen (direntp->d_name);
7089               buffer_grow (filename, len+1);
7090               strcpy ((char *) filename->ptr, d_name);
7091               found |= note_possible_classname ((const char *) filename->data + entry_length,
7092                                                 package_length+len+1);
7093             }
7094           if (dirp)
7095             closedir (dirp);
7096         }
7097     }
7098
7099   free (filename->data);
7100
7101   /* Here we should have a unified way of retrieving an entry, to be
7102      indexed. */
7103   if (!found)
7104     {
7105       static int first = 1;
7106       if (first)
7107         {
7108           error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7109           java_error_count++;
7110           first = 0;
7111         }
7112       else
7113         parse_error_context (wfl, "Package %qs not found in import",
7114                              package_name);
7115       current_jcf = saved_jcf;
7116       return;
7117     }
7118   current_jcf = saved_jcf;
7119 }
7120
7121 /* Possibly find a type in the import on demands specified
7122    types. Returns 1 if an error occurred, 0 otherwise. Run through the
7123    entire list, to detected potential double definitions.  */
7124
7125 static int
7126 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7127 {
7128   tree class_type_name = TYPE_NAME (class_type);
7129   tree cl = NULL_TREE;
7130   int seen_once = -1;   /* -1 when not set, 1 if seen once, >1 otherwise. */
7131   int to_return = -1;   /* -1 when not set, 0 or 1 otherwise */
7132   tree node;
7133   tree import;
7134
7135   if (enclosing_type && TYPE_IMPORT_DEMAND_LIST (enclosing_type))
7136     import = TYPE_IMPORT_DEMAND_LIST (enclosing_type);
7137   else
7138     import = ctxp->import_demand_list;
7139
7140   for (; import; import = TREE_CHAIN (import))
7141     {
7142       location_t saved_location = input_location;
7143       int access_check;
7144       const char *id_name;
7145       tree decl, type_name_copy;
7146
7147       obstack_grow (&temporary_obstack,
7148                     IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7149                     IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7150       obstack_1grow (&temporary_obstack, '.');
7151       obstack_grow0 (&temporary_obstack,
7152                      IDENTIFIER_POINTER (class_type_name),
7153                      IDENTIFIER_LENGTH (class_type_name));
7154       id_name = obstack_finish (&temporary_obstack);
7155
7156       if (! (node = maybe_get_identifier (id_name)))
7157         continue;
7158
7159       /* Setup input_line so that it refers to the line of the import (in
7160          case we parse a class file and encounter errors */
7161 #ifdef USE_MAPPED_LOCATION
7162       input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7163 #else
7164       input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7165 #endif
7166
7167       type_name_copy = TYPE_NAME (class_type);
7168       TYPE_NAME (class_type) = node;
7169       QUALIFIED_P (node) = 1;
7170       decl = IDENTIFIER_CLASS_VALUE (node);
7171       access_check = -1;
7172       /* If there is no DECL set for the class or if the class isn't
7173          loaded and not seen in source yet, then load */
7174       if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7175         {
7176           load_class (node, 0);
7177           decl = IDENTIFIER_CLASS_VALUE (node);
7178         }
7179       if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7180         access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7181                                                false, NULL_TREE);
7182       else
7183         /* 6.6.1: Inner classes are subject to member access rules. */
7184         access_check = 0;
7185
7186       input_location = saved_location;
7187
7188       /* If the loaded class is not accessible or couldn't be loaded,
7189          we restore the original TYPE_NAME and process the next
7190          import. */
7191       if (access_check || !decl)
7192         {
7193           TYPE_NAME (class_type) = type_name_copy;
7194           continue;
7195         }
7196
7197       /* If the loaded class is accessible, we keep a tab on it to
7198          detect and report multiple inclusions. */
7199       if (IS_A_CLASSFILE_NAME (node))
7200         {
7201           if (seen_once < 0)
7202             {
7203               cl = TREE_PURPOSE (import);
7204               seen_once = 1;
7205             }
7206           else if (seen_once >= 0)
7207             {
7208               tree location = (cl ? cl : TREE_PURPOSE (import));
7209               tree package = (cl ? EXPR_WFL_NODE (cl) :
7210                               EXPR_WFL_NODE (TREE_PURPOSE (import)));
7211               seen_once++;
7212               parse_error_context
7213                 (location,
7214                  "Type %qs also potentially defined in package %qs",
7215                  IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7216                  IDENTIFIER_POINTER (package));
7217             }
7218         }
7219       to_return = access_check;
7220     }
7221
7222   if (seen_once == 1)
7223     return to_return;
7224   else
7225     return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7226 }
7227
7228 static tree
7229 resolve_package (tree pkg, tree *next, tree *type_name)
7230 {
7231   tree current;
7232   tree decl = NULL_TREE;
7233   *type_name = NULL_TREE;
7234
7235   /* The trick is to determine when the package name stops and were
7236      the name of something contained in the package starts. Then we
7237      return a fully qualified name of what we want to get. */
7238
7239   *next = EXPR_WFL_QUALIFICATION (pkg);
7240
7241   /* Try to progressively construct a type name */
7242   if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7243     for (current = EXPR_WFL_QUALIFICATION (pkg);
7244          current; current = TREE_CHAIN (current))
7245       {
7246         /* If we don't have what we're expecting, exit now. TYPE_NAME
7247            will be null and the error caught later. */
7248         if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7249           break;
7250         *type_name =
7251           merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7252         if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7253           {
7254             /* resolve_package should be used in a loop, hence we
7255                point at this one to naturally process the next one at
7256                the next iteration. */
7257             *next = current;
7258             break;
7259           }
7260       }
7261   return decl;
7262 }
7263
7264
7265 /* Check accessibility of inner classes according to member access rules.
7266    DECL is the inner class, ENCLOSING_DECL is the class from which the
7267    access is being attempted. */
7268
7269 static void
7270 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7271 {
7272   const char *access;
7273   tree enclosing_decl_type;
7274
7275   /* We don't issue an error message when CL is null. CL can be null
7276      as a result of processing a JDEP crafted by source_start_java_method
7277      for the purpose of patching its parm decl. But the error would
7278      have been already trapped when fixing the method's signature.
7279      DECL can also be NULL in case of earlier errors. */
7280   if (!decl || !cl)
7281     return;
7282
7283   enclosing_decl_type = TREE_TYPE (enclosing_decl);
7284
7285   if (CLASS_PRIVATE (decl))
7286     {
7287       /* Access is permitted only within the body of the top-level
7288          class in which DECL is declared. */
7289       tree top_level = decl;
7290       while (DECL_CONTEXT (top_level))
7291         top_level = DECL_CONTEXT (top_level);
7292       while (DECL_CONTEXT (enclosing_decl))
7293         enclosing_decl = DECL_CONTEXT (enclosing_decl);
7294       if (top_level == enclosing_decl)
7295         return;
7296       access = "private";
7297     }
7298   else if (CLASS_PROTECTED (decl))
7299     {
7300       tree decl_context;
7301       /* Access is permitted from within the same package... */
7302       if (in_same_package (decl, enclosing_decl))
7303         return;
7304
7305       /* ... or from within the body of a subtype of the context in which
7306          DECL is declared. */
7307       decl_context = DECL_CONTEXT (decl);
7308       while (enclosing_decl)
7309         {
7310           if (CLASS_INTERFACE (decl))
7311             {
7312               if (interface_of_p (TREE_TYPE (decl_context),
7313                                   enclosing_decl_type))
7314                 return;
7315             }
7316           else
7317             {
7318               /* Eww. The order of the arguments is different!! */
7319               if (inherits_from_p (enclosing_decl_type,
7320                                    TREE_TYPE (decl_context)))
7321                 return;
7322             }
7323           enclosing_decl = DECL_CONTEXT (enclosing_decl);
7324         }
7325       access = "protected";
7326     }
7327   else if (! CLASS_PUBLIC (decl))
7328     {
7329       /* Access is permitted only from within the same package as DECL. */
7330       if (in_same_package (decl, enclosing_decl))
7331         return;
7332       access = "non-public";
7333     }
7334   else
7335     /* Class is public. */
7336     return;
7337
7338   parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7339                        (CLASS_INTERFACE (decl) ? "interface" : "class"),
7340                        lang_printable_name (decl, 2), access);
7341 }
7342
7343 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7344    foreign package, it must be PUBLIC. Return 0 if no access
7345    violations were found, 1 otherwise. If VERBOSE is true and an error
7346    was found, it is reported and accounted for.  If CL is NULL then 
7347    look it up with THIS_DECL.  */
7348
7349 static int
7350 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7351 {
7352   tree type;
7353
7354   if (!IDENTIFIER_CLASS_VALUE (class_name))
7355     return 0;
7356
7357   if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7358     return 0;
7359
7360   if (!CLASS_PUBLIC (TYPE_NAME (type)))
7361     {
7362       /* Access to a private class within the same package is
7363          allowed. */
7364       tree l, r;
7365       split_qualified_name (&l, &r, class_name);
7366       if (!QUALIFIED_P (class_name) && !ctxp->package)
7367         /* Both in the empty package. */
7368         return 0;
7369       if (l == ctxp->package)
7370         /* Both in the same package. */
7371         return 0;
7372
7373       if (verbose)
7374         parse_error_context
7375           (cl == NULL ? lookup_cl (this_decl): cl,
7376            "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7377            (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7378            IDENTIFIER_POINTER (class_name));
7379       return 1;
7380     }
7381   return 0;
7382 }
7383
7384 /* Local variable declaration. */
7385
7386 static void
7387 declare_local_variables (int modifier, tree type, tree vlist)
7388 {
7389   tree decl, current, saved_type;
7390   tree type_wfl = NULL_TREE;
7391   int must_chain = 0;
7392   int final_p = 0;
7393
7394   /* Push a new block if statements were seen between the last time we
7395      pushed a block and now. Keep a count of blocks to close */
7396   if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7397     {
7398       tree b = enter_block ();
7399       BLOCK_IS_IMPLICIT (b) = 1;
7400     }
7401
7402   if (modifier)
7403     {
7404       size_t i;
7405       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7406         if (1 << i & modifier)
7407           break;
7408       if (modifier == ACC_FINAL)
7409         final_p = 1;
7410       else
7411         {
7412           parse_error_context
7413             (ctxp->modifier_ctx [i],
7414              "Only %<final%> is allowed as a local variables modifier");
7415           return;
7416         }
7417     }
7418
7419   /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7420      hold the TYPE value if a new incomplete has to be created (as
7421      opposed to being found already existing and reused). */
7422   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7423
7424   /* If TYPE is fully resolved and we don't have a reference, make one */
7425   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7426
7427   /* Go through all the declared variables */
7428   for (current = vlist, saved_type = type; current;
7429        current = TREE_CHAIN (current), type = saved_type)
7430     {
7431       tree other, real_type;
7432       tree wfl  = TREE_PURPOSE (current);
7433       tree name = EXPR_WFL_NODE (wfl);
7434       tree init = TREE_VALUE (current);
7435
7436       /* Process NAME, as it may specify extra dimension(s) for it */
7437       type = build_array_from_name (type, type_wfl, name, &name);
7438
7439       /* Variable redefinition check */
7440       if ((other = lookup_name_in_blocks (name)))
7441         {
7442           variable_redefinition_error (wfl, name, TREE_TYPE (other),
7443                                        DECL_SOURCE_LINE (other));
7444           continue;
7445         }
7446
7447       /* Type adjustment. We may have just readjusted TYPE because
7448          the variable specified more dimensions. Make sure we have
7449          a reference if we can and don't have one already. */
7450       PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7451
7452       real_type = GET_REAL_TYPE (type);
7453       /* Never layout this decl. This will be done when its scope
7454          will be entered */
7455       decl = build_decl (VAR_DECL, name, real_type);
7456       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7457       DECL_FINAL (decl) = final_p;
7458       BLOCK_CHAIN_DECL (decl);
7459
7460       /* Don't try to use an INIT statement when an error was found */
7461       if (init && java_error_count)
7462         init = NULL_TREE;
7463
7464       /* Remember it if this is an initialized-upon-declaration final
7465          variable.  */
7466       if (init && final_p)
7467         {
7468           DECL_LOCAL_FINAL_IUD (decl) = 1;
7469         }
7470
7471       /* Add the initialization function to the current function's code */
7472       if (init)
7473         {
7474           /* Name might have been readjusted */
7475           EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7476           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7477           java_method_add_stmt (current_function_decl,
7478                                 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7479                                                       init));
7480         }
7481
7482       /* Setup dependency the type of the decl */
7483       if (must_chain)
7484         {
7485           jdep *dep;
7486           register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7487           dep = CLASSD_LAST (ctxp->classd_list);
7488           JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7489         }
7490     }
7491   SOURCE_FRONTEND_DEBUG (("Defined locals"));
7492 }
7493
7494 /* Called during parsing. Build decls from argument list.  */
7495
7496 static void
7497 source_start_java_method (tree fndecl)
7498 {
7499   tree tem;
7500   tree parm_decl;
7501   int i;
7502
7503   if (!fndecl)
7504     return;
7505
7506   current_function_decl = fndecl;
7507
7508   /* New scope for the function */
7509   enter_block ();
7510   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7511        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7512     {
7513       tree type = TREE_VALUE (tem);
7514       tree name = TREE_PURPOSE (tem);
7515
7516       /* If type is incomplete. Create an incomplete decl and ask for
7517          the decl to be patched later */
7518       if (INCOMPLETE_TYPE_P (type))
7519         {
7520           jdep *jdep;
7521           tree real_type = GET_REAL_TYPE (type);
7522           parm_decl = build_decl (PARM_DECL, name, real_type);
7523           type = obtain_incomplete_type (type);
7524           register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7525           jdep = CLASSD_LAST (ctxp->classd_list);
7526           JDEP_MISC (jdep) = name;
7527           JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7528         }
7529       else
7530         parm_decl = build_decl (PARM_DECL, name, type);
7531
7532       /* Remember if a local variable was declared final (via its
7533          TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7534       if (ARG_FINAL_P (tem))
7535         {
7536           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7537           DECL_FINAL (parm_decl) = 1;
7538         }
7539
7540       BLOCK_CHAIN_DECL (parm_decl);
7541     }
7542   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7543   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7544     nreverse (tem);
7545   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7546   DECL_MAX_LOCALS (current_function_decl) = i;
7547 }
7548
7549 /* Called during parsing. Creates an artificial method declaration.  */
7550
7551 static tree
7552 create_artificial_method (tree class, int flags, tree type,
7553                           tree name, tree args)
7554 {
7555   tree mdecl;
7556   location_t save_location = input_location;
7557
7558   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7559   mdecl = make_node (FUNCTION_TYPE);
7560   TREE_TYPE (mdecl) = type;
7561   TYPE_ARG_TYPES (mdecl) = args;
7562   /* We used to compute the signature of MDECL here and then use
7563      add_method(), but that failed because our caller might modify
7564      the type of the returned method, which trashes the cache in
7565      get_type_from_signature().  */
7566   mdecl = add_method_1 (class, flags, name, mdecl);
7567   input_location = save_location;
7568   DECL_ARTIFICIAL (mdecl) = 1;
7569   return mdecl;
7570 }
7571
7572 /* Starts the body if an artificial method.  */
7573
7574 static void
7575 start_artificial_method_body (tree mdecl)
7576 {
7577 #ifdef USE_MAPPED_LOCATION
7578   DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7579   DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7580 #else
7581   DECL_SOURCE_LINE (mdecl) = 1;
7582   DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7583 #endif
7584   source_start_java_method (mdecl);
7585   enter_block ();
7586 }
7587
7588 static void
7589 end_artificial_method_body (tree mdecl)
7590 {
7591   /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7592      It has to be evaluated first. (if mdecl is current_function_decl,
7593      we have an undefined behavior if no temporary variable is used.) */
7594   tree b = exit_block ();
7595   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7596   exit_block ();
7597 }
7598
7599 /* Dump a tree of some kind.  This is a convenience wrapper for the
7600    dump_* functions in tree-dump.c.  */
7601 static void
7602 dump_java_tree (enum tree_dump_index phase, tree t)
7603 {
7604   FILE *stream;
7605   int flags;
7606
7607   stream = dump_begin (phase, &flags);
7608   flags |= TDF_SLIM;
7609   if (stream)
7610     {
7611       dump_node (t, flags, stream);
7612       dump_end (phase, stream);
7613     }
7614 }
7615
7616 /* Terminate a function and expand its body.  */
7617
7618 static void
7619 source_end_java_method (void)
7620 {
7621   tree fndecl = current_function_decl;
7622
7623   if (!fndecl)
7624     return;
7625
7626   java_parser_context_save_global ();
7627 #ifdef USE_MAPPED_LOCATION
7628   input_location = ctxp->last_ccb_indent1;
7629 #else
7630   input_line = ctxp->last_ccb_indent1;
7631 #endif
7632
7633   /* Turn function bodies with only a NOP expr null, so they don't get
7634      generated at all and we won't get warnings when using the -W
7635      -Wall flags. */
7636   if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7637     BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7638
7639   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7640       && ! flag_emit_class_files)
7641     finish_method (fndecl);
7642
7643   current_function_decl = NULL_TREE;
7644   java_parser_context_restore_global ();
7645   current_function_decl = NULL_TREE;
7646 }
7647
7648 /* Record EXPR in the current function block. Complements compound
7649    expression second operand if necessary.  */
7650
7651 tree
7652 java_method_add_stmt (tree fndecl, tree expr)
7653 {
7654   if (!GET_CURRENT_BLOCK (fndecl))
7655     return NULL_TREE;
7656   return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7657 }
7658
7659 static tree
7660 add_stmt_to_block (tree b, tree type, tree stmt)
7661 {
7662   tree body = BLOCK_EXPR_BODY (b), c;
7663
7664   if (java_error_count)
7665     return body;
7666
7667   if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7668     return body;
7669
7670   BLOCK_EXPR_BODY (b) = c;
7671   TREE_SIDE_EFFECTS (c) = 1;
7672   return c;
7673 }
7674
7675 /* Lays out the methods for the classes seen so far.  */
7676
7677 void
7678 java_layout_seen_class_methods (void)
7679 {
7680   tree previous_list = all_class_list;
7681   tree end = NULL_TREE;
7682   tree current;
7683
7684   while (1)
7685     {
7686       for (current = previous_list;
7687            current != end; current = TREE_CHAIN (current))
7688         {
7689           tree decl = TREE_VALUE (current);
7690           tree cls = TREE_TYPE (decl);
7691
7692           input_location = DECL_SOURCE_LOCATION (decl);
7693
7694           if (! CLASS_LOADED_P (cls))
7695             load_class (cls, 0);
7696
7697           layout_class_methods (cls);
7698         }
7699
7700       /* Note that new classes might have been added while laying out
7701          methods, changing the value of all_class_list.  */
7702
7703       if (previous_list != all_class_list)
7704         {
7705           end = previous_list;
7706           previous_list = all_class_list;
7707         }
7708       else
7709         break;
7710     }
7711 }
7712
7713 static GTY(()) tree stop_reordering;
7714 void
7715 java_reorder_fields (void)
7716 {
7717   tree current;
7718
7719   for (current = gclass_list; current; current = TREE_CHAIN (current))
7720     {
7721       output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7722
7723       if (current_class == stop_reordering)
7724         break;
7725
7726       /* Reverse the fields, but leave the dummy field in front.
7727          Fields are already ordered for Object and Class */
7728       if (TYPE_FIELDS (current_class) && current_class != object_type_node
7729           && current_class != class_type_node)
7730       {
7731         /* If the dummy field is there, reverse the right fields and
7732            just layout the type for proper fields offset */
7733         if (!DECL_NAME (TYPE_FIELDS (current_class)))
7734           {
7735             tree fields = TYPE_FIELDS (current_class);
7736             TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7737             TYPE_SIZE (current_class) = NULL_TREE;
7738           }
7739         /* We don't have a dummy field, we need to layout the class,
7740            after having reversed the fields */
7741         else
7742           {
7743             TYPE_FIELDS (current_class) =
7744               nreverse (TYPE_FIELDS (current_class));
7745             TYPE_SIZE (current_class) = NULL_TREE;
7746           }
7747       }
7748     }
7749   /* There are cases were gclass_list will be empty. */
7750   if (gclass_list)
7751     stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7752 }
7753
7754 /* Layout the methods of all classes loaded in one way or another.
7755    Check methods of source parsed classes. Then reorder the
7756    fields and layout the classes or the type of all source parsed
7757    classes */
7758
7759 void
7760 java_layout_classes (void)
7761 {
7762   tree current;
7763   int save_error_count = java_error_count;
7764
7765   /* Layout the methods of all classes seen so far */
7766   java_layout_seen_class_methods ();
7767   java_parse_abort_on_error ();
7768   all_class_list = NULL_TREE;
7769
7770   /* Then check the methods of all parsed classes */
7771   for (current = gclass_list; current; current = TREE_CHAIN (current))
7772     if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7773       java_check_methods (TREE_VALUE (current));
7774   java_parse_abort_on_error ();
7775
7776   for (current = gclass_list; current; current = TREE_CHAIN (current))
7777     {
7778       output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7779       layout_class (current_class);
7780
7781       /* Error reported by the caller */
7782       if (java_error_count)
7783         return;
7784     }
7785
7786   /* We might have reloaded classes durign the process of laying out
7787      classes for code generation. We must layout the methods of those
7788      late additions, as constructor checks might use them */
7789   java_layout_seen_class_methods ();
7790   java_parse_abort_on_error ();
7791 }
7792
7793 /* Expand methods in the current set of classes remembered for
7794    generation.  */
7795
7796 static void
7797 java_complete_expand_classes (void)
7798 {
7799   tree current;
7800
7801   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7802     if (!INNER_CLASS_DECL_P (current))
7803       java_complete_expand_class (current);
7804 }
7805
7806 /* Expand the methods found in OUTER, starting first by OUTER's inner
7807    classes, if any.  */
7808
7809 static void
7810 java_complete_expand_class (tree outer)
7811 {
7812   tree inner_list;
7813
7814   /* We need to go after all inner classes and start expanding them,
7815      starting with most nested ones. We have to do that because nested
7816      classes might add functions to outer classes */
7817
7818   for (inner_list = DECL_INNER_CLASS_LIST (outer);
7819        inner_list; inner_list = TREE_CHAIN (inner_list))
7820     java_complete_expand_class (TREE_PURPOSE (inner_list));
7821
7822   java_complete_expand_methods (outer);
7823 }
7824
7825 /* Expand methods registered in CLASS_DECL. The general idea is that
7826    we expand regular methods first. This allows us get an estimate on
7827    how outer context local alias fields are really used so we can add
7828    to the constructor just enough code to initialize them properly (it
7829    also lets us generate finit$ correctly.) Then we expand the
7830    constructors and then <clinit>.  */
7831
7832 static void
7833 java_complete_expand_methods (tree class_decl)
7834 {
7835   tree clinit, decl, first_decl;
7836
7837   output_class = current_class = TREE_TYPE (class_decl);
7838
7839   /* Pre-expand <clinit> to figure whether we really need it or
7840      not. If we do need it, we pre-expand the static fields so they're
7841      ready to be used somewhere else. <clinit> will be fully expanded
7842      after we processed the constructors. */
7843   first_decl = TYPE_METHODS (current_class);
7844   clinit = maybe_generate_pre_expand_clinit (current_class);
7845
7846   /* Then generate finit$ (if we need to) because constructors will
7847    try to use it.*/
7848   if (TYPE_FINIT_STMT_LIST (current_class))
7849     java_complete_expand_method (generate_finit (current_class));
7850
7851   /* Then generate instinit$ (if we need to) because constructors will
7852      try to use it. */
7853   if (TYPE_II_STMT_LIST (current_class))
7854     java_complete_expand_method (generate_instinit (current_class));
7855
7856   /* Now do the constructors */
7857   for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7858     {
7859       if (!DECL_CONSTRUCTOR_P (decl))
7860         continue;
7861       java_complete_expand_method (decl);
7862     }
7863
7864   /* First, do the ordinary methods. */
7865   for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7866     {
7867       /* Ctors aren't part of this batch. */
7868       if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7869         continue;
7870
7871       /* Skip abstract or native methods -- but do handle native
7872          methods when generating JNI stubs.  */
7873       if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7874         {
7875           DECL_FUNCTION_BODY (decl) = NULL_TREE;
7876           continue;
7877         }
7878
7879       if (METHOD_NATIVE (decl))
7880         {
7881           tree body;
7882           current_function_decl = decl;
7883           body = build_jni_stub (decl);
7884           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7885         }
7886
7887       java_complete_expand_method (decl);
7888     }
7889
7890   /* If there is indeed a <clinit>, fully expand it now */
7891   if (clinit)
7892     {
7893       /* Prevent the use of `this' inside <clinit> */
7894       ctxp->explicit_constructor_p = 1;
7895       java_complete_expand_method (clinit);
7896       ctxp->explicit_constructor_p = 0;
7897     }
7898
7899   /* We might have generated a class$ that we now want to expand */
7900   if (TYPE_DOT_CLASS (current_class))
7901     java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7902
7903   /* Now verify constructor circularity (stop after the first one we
7904      prove wrong.) */
7905   if (!CLASS_INTERFACE (class_decl))
7906     for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7907       if (DECL_CONSTRUCTOR_P (decl)
7908           && verify_constructor_circularity (decl, decl))
7909         break;
7910 }
7911
7912 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7913    safely used in some other methods/constructors.  */
7914
7915 static tree
7916 maybe_generate_pre_expand_clinit (tree class_type)
7917 {
7918   tree current, mdecl;
7919
7920   if (!TYPE_CLINIT_STMT_LIST (class_type))
7921     return NULL_TREE;
7922
7923   /* Go through all static fields and pre expand them */
7924   for (current = TYPE_FIELDS (class_type); current;
7925        current = TREE_CHAIN (current))
7926     if (FIELD_STATIC (current))
7927       build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7928
7929   /* Then build the <clinit> method */
7930   mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7931                                     clinit_identifier_node, end_params_node);
7932   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7933                        mdecl, NULL_TREE);
7934   start_artificial_method_body (mdecl);
7935
7936   /* We process the list of assignment we produced as the result of
7937      the declaration of initialized static field and add them as
7938      statement to the <clinit> method. */
7939   for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7940        current = TREE_CHAIN (current))
7941     {
7942       tree stmt = current;
7943       /* We build the assignment expression that will initialize the
7944          field to its value. There are strict rules on static
7945          initializers (8.5). FIXME */
7946       if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
7947         stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7948       java_method_add_stmt (mdecl, stmt);
7949     }
7950
7951   end_artificial_method_body (mdecl);
7952
7953   /* Now we want to place <clinit> as the last method (because we need
7954      it at least for interface so that it doesn't interfere with the
7955      dispatch table based lookup. */
7956   if (TREE_CHAIN (TYPE_METHODS (class_type)))
7957     {
7958       current = TREE_CHAIN (TYPE_METHODS (class_type));
7959       TYPE_METHODS (class_type) = current;
7960
7961       while (TREE_CHAIN (current))
7962         current = TREE_CHAIN (current);
7963
7964       TREE_CHAIN (current) = mdecl;
7965       TREE_CHAIN (mdecl) = NULL_TREE;
7966     }
7967
7968   return mdecl;
7969 }
7970
7971 /* Analyzes a method body and look for something that isn't a
7972    MODIFY_EXPR with a constant value.  */
7973
7974 static int
7975 analyze_clinit_body (tree this_class, tree bbody)
7976 {
7977   while (bbody)
7978     switch (TREE_CODE (bbody))
7979       {
7980       case BLOCK:
7981         bbody = BLOCK_EXPR_BODY (bbody);
7982         break;
7983
7984       case EXPR_WITH_FILE_LOCATION:
7985         bbody = EXPR_WFL_NODE (bbody);
7986         break;
7987
7988       case COMPOUND_EXPR:
7989         if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7990           return 1;
7991         bbody = TREE_OPERAND (bbody, 1);
7992         break;
7993
7994       case MODIFY_EXPR:
7995         /* If we're generating to class file and we're dealing with an
7996            array initialization, we return 1 to keep <clinit> */
7997         if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7998             && flag_emit_class_files)
7999           return 1;
8000
8001         /* There are a few cases where we're required to keep
8002            <clinit>:
8003            - If this is an assignment whose operand is not constant,
8004            - If this is an assignment to a non-initialized field,
8005            - If this field is not a member of the current class.
8006         */
8007         return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8008                 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8009                 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8010
8011       default:
8012         return 1;
8013       }
8014   return 0;
8015 }
8016
8017
8018 /* See whether we could get rid of <clinit>. Criteria are: all static
8019    final fields have constant initial values and the body of <clinit>
8020    is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8021
8022 static int
8023 maybe_yank_clinit (tree mdecl)
8024 {
8025   tree type, current;
8026   tree fbody, bbody;
8027
8028   if (!DECL_CLINIT_P (mdecl))
8029     return 0;
8030
8031   /* If the body isn't empty, then we keep <clinit>. Note that if
8032      we're emitting classfiles, this isn't enough not to rule it
8033      out. */
8034   fbody = DECL_FUNCTION_BODY (mdecl);
8035   bbody = BLOCK_EXPR_BODY (fbody);
8036   if (bbody && bbody != error_mark_node)
8037     bbody = BLOCK_EXPR_BODY (bbody);
8038   else
8039     return 0;
8040   if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8041     return 0;
8042
8043   type = DECL_CONTEXT (mdecl);
8044   current = TYPE_FIELDS (type);
8045
8046   for (current = (current ? TREE_CHAIN (current) : current);
8047        current; current = TREE_CHAIN (current))
8048     {
8049       tree f_init;
8050
8051       /* We're not interested in non-static fields.  */
8052       if (!FIELD_STATIC (current))
8053         continue;
8054
8055       /* Nor in fields without initializers. */
8056       f_init = DECL_INITIAL (current);
8057       if (f_init == NULL_TREE)
8058         continue;
8059
8060       /* Anything that isn't String or a basic type is ruled out -- or
8061          if we know how to deal with it (when doing things natively) we
8062          should generated an empty <clinit> so that SUID are computed
8063          correctly. */
8064       if (! JSTRING_TYPE_P (TREE_TYPE (current))
8065           && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8066         return 0;
8067
8068       if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8069         return 0;
8070     }
8071
8072   /* Now we analyze the method body and look for something that
8073      isn't a MODIFY_EXPR */
8074   if (bbody && !IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8075     return 0;
8076
8077   /* Get rid of <clinit> in the class' list of methods */
8078   if (TYPE_METHODS (type) == mdecl)
8079     TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8080   else
8081     for (current = TYPE_METHODS (type); current;
8082          current = TREE_CHAIN (current))
8083       if (TREE_CHAIN (current) == mdecl)
8084         {
8085           TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8086           break;
8087         }
8088
8089   return 1;
8090 }
8091
8092 /* Install the argument from MDECL. Suitable to completion and
8093    expansion of mdecl's body.  */
8094
8095 void
8096 start_complete_expand_method (tree mdecl)
8097 {
8098   tree tem;
8099
8100   pushlevel (1);                /* Prepare for a parameter push */
8101   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8102   DECL_ARGUMENTS (mdecl) = tem;
8103
8104   for (; tem; tem = TREE_CHAIN (tem))
8105     {
8106       /* TREE_CHAIN (tem) will change after pushdecl. */
8107       tree next = TREE_CHAIN (tem);
8108       tree type = TREE_TYPE (tem);
8109       if (targetm.calls.promote_prototypes (type)
8110           && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8111           && INTEGRAL_TYPE_P (type))
8112         type = integer_type_node;
8113       DECL_ARG_TYPE (tem) = type;
8114       layout_decl (tem, 0);
8115       pushdecl (tem);
8116       /* Re-install the next so that the list is kept and the loop
8117          advances. */
8118       TREE_CHAIN (tem) = next;
8119     }
8120   pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8121   input_location = DECL_SOURCE_LOCATION (mdecl);
8122   build_result_decl (mdecl);
8123 }
8124
8125
8126 /* Complete and expand a method.  */
8127
8128 static void
8129 java_complete_expand_method (tree mdecl)
8130 {
8131   tree fbody, block_body, exception_copy;
8132
8133   current_function_decl = mdecl;
8134   /* Fix constructors before expanding them */
8135   if (DECL_CONSTRUCTOR_P (mdecl))
8136     fix_constructors (mdecl);
8137
8138   /* Expand functions that have a body */
8139   if (!DECL_FUNCTION_BODY (mdecl))
8140     return;
8141
8142   fbody = DECL_FUNCTION_BODY (mdecl);
8143   block_body = BLOCK_EXPR_BODY (fbody);
8144   exception_copy = NULL_TREE;
8145
8146   current_function_decl = mdecl;
8147
8148   if (! quiet_flag)
8149     fprintf (stderr, " [%s.",
8150              lang_printable_name (DECL_CONTEXT (mdecl), 0));
8151   announce_function (mdecl);
8152   if (! quiet_flag)
8153     fprintf (stderr, "]");
8154
8155   /* Prepare the function for tree completion */
8156   start_complete_expand_method (mdecl);
8157
8158   /* Install the current this */
8159   current_this = (!METHOD_STATIC (mdecl) ?
8160                   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8161
8162   /* Purge the `throws' list of unchecked exceptions (we save a copy
8163      of the list and re-install it later.) */
8164   exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8165   purge_unchecked_exceptions (mdecl);
8166
8167   /* Install exceptions thrown with `throws' */
8168   PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8169
8170   if (block_body != NULL_TREE)
8171     {
8172       block_body = java_complete_tree (block_body);
8173
8174       /* Before we check initialization, attached all class initialization
8175          variable to the block_body */
8176       htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8177                      attach_init_test_initialization_flags, block_body);
8178
8179       if (! METHOD_NATIVE (mdecl))
8180         {
8181           check_for_initialization (block_body, mdecl);
8182
8183           /* Go through all the flags marking the initialization of
8184              static variables and see whether they're definitively
8185              assigned, in which case the type is remembered as
8186              definitively initialized in MDECL. */
8187           if (STATIC_CLASS_INIT_OPT_P ())
8188             {
8189               /* Always register the context as properly initialized in
8190                  MDECL. This used with caution helps removing extra
8191                  initialization of self. */
8192               if (METHOD_STATIC (mdecl))
8193                 {
8194                   *(htab_find_slot
8195                     (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8196                      DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8197                 }
8198             }
8199         }
8200       ctxp->explicit_constructor_p = 0;
8201     }
8202
8203   BLOCK_EXPR_BODY (fbody) = block_body;
8204
8205   /* If we saw a return but couldn't evaluate it properly, we'll have
8206      an error_mark_node here. */
8207   if (block_body != error_mark_node
8208       && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8209       && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
8210     missing_return_error (current_function_decl);
8211
8212   /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8213   maybe_yank_clinit (mdecl);
8214
8215   /* Pop the current level, with special measures if we found errors. */
8216   if (java_error_count)
8217     pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8218   poplevel (1, 0, 1);
8219
8220   /* Pop the exceptions and sanity check */
8221   POP_EXCEPTIONS();
8222   if (currently_caught_type_list)
8223     abort ();
8224
8225   /* Restore the copy of the list of exceptions. */
8226   DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8227 }
8228
8229 /* For with each class for which there's code to generate. */
8230
8231 static void
8232 java_expand_method_bodies (tree class)
8233 {
8234   tree decl;
8235   for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8236     {
8237       tree block;
8238
8239       if (! DECL_FUNCTION_BODY (decl))
8240         continue;
8241
8242       current_function_decl = decl;
8243
8244       block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8245
8246       /* Save the function body for gimplify and inlining.  */
8247       DECL_SAVED_TREE (decl) = block;
8248
8249       /* It's time to assign the variable flagging static class
8250          initialization based on which classes invoked static methods
8251          are definitely initializing. This should be flagged. */
8252       if (STATIC_CLASS_INIT_OPT_P ())
8253         {
8254           tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8255           for (; list != NULL_TREE;  list = TREE_CHAIN (list))
8256             {
8257               /* Executed for each statement calling a static function.
8258                  LIST is a TREE_LIST whose PURPOSE is the called function
8259                  and VALUE is a compound whose second operand can be patched
8260                  with static class initialization flag assignments.  */
8261
8262               tree called_method = TREE_PURPOSE (list);
8263               tree compound = TREE_VALUE (list);
8264               tree assignment_compound_list
8265                 = build_tree_list (called_method, NULL);
8266
8267               /* For each class definitely initialized in
8268                  CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8269                  assignment to the class initialization flag. */
8270               htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8271                              emit_test_initialization,
8272                              assignment_compound_list);
8273
8274               if (TREE_VALUE (assignment_compound_list))
8275                 TREE_OPERAND (compound, 1)
8276                   = TREE_VALUE (assignment_compound_list);
8277             }
8278         }
8279
8280       /* Expand the function body.  */
8281       source_end_java_method ();
8282     }
8283 }
8284
8285 \f
8286
8287 /* This section of the code deals with accessing enclosing context
8288    fields either directly by using the relevant access to this$<n> or
8289    by invoking an access method crafted for that purpose.  */
8290
8291 /* Build the necessary access across nested class boundaries.
8292    This routine could be optimized to cache previous result
8293    (decl, current_class and returned access).  When an access method
8294    needs to be generated, it always takes the form of a read.  It might
8295    be later turned into a write by calling nested_field_access_fix.  */
8296
8297 static tree
8298 build_nested_field_access (tree id, tree decl)
8299 {
8300   tree access = NULL_TREE;
8301   tree ctx = NULL_TREE;
8302   tree decl_ctx = DECL_CONTEXT (decl);
8303   bool is_static = FIELD_STATIC (decl);
8304
8305   if (DECL_CONTEXT (TYPE_NAME (current_class)))
8306     ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8307
8308   /* For non-static fields, if the immediate enclosing context of the
8309      current class is the field decl's class or inherits from it,
8310      build the access as `this$<n>.<field>'.  Note that we will break
8311      the `private' barrier if we're not emitting bytecodes.  */
8312   if (!is_static
8313       && ctx
8314       && (ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8315       && (!FIELD_PRIVATE (decl) || !flag_emit_class_files))
8316     {
8317       tree thisn = build_current_thisn (current_class);
8318       access = make_qualified_primary (build_wfl_node (thisn),
8319                                        id, EXPR_WFL_LINECOL (id));
8320     }
8321   /* Otherwise, generate and use accessor methods for the field as
8322      needed.  */
8323   else
8324     {
8325       int lc = EXPR_WFL_LINECOL (id);
8326
8327       /* Now we chain the required number of calls to the access$0 to
8328          get a hold to the enclosing instance we need for a non-static
8329          field, and then we build the field access. */
8330       if (!is_static)
8331         access = build_access_to_thisn (current_class, decl_ctx, lc);
8332
8333       /* If the field is private and we're generating bytecode, then
8334          we generate an access method.  */
8335       if (FIELD_PRIVATE (decl) && flag_emit_class_files)
8336         {
8337           tree name = build_nested_field_access_methods (decl);
8338           access = build_nested_field_access_expr (lc, decl_ctx,
8339                                                    name, access, NULL_TREE);
8340         }
8341       /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'
8342          for non-static fields.
8343          Once again we break the `private' access rule from a foreign
8344          class.  */
8345       else if (is_static)
8346         {
8347           tree class_name = DECL_NAME (TYPE_NAME (decl_ctx));
8348           access
8349             = make_qualified_primary (build_wfl_node (class_name), id, lc);
8350         }
8351       else
8352         access = make_qualified_primary (access, id, lc);
8353     }
8354
8355   return resolve_expression_name (access, NULL);
8356 }
8357
8358 /* Return a nonzero value if DECL describes a field access across nested
8359    class boundaries.  That is, DECL is in a class that either encloses,
8360    is enclosed by or shares a common enclosing class with, the class
8361    TYPE.  */
8362
8363 static int
8364 nested_field_access_p (tree type, tree decl)
8365 {
8366   bool is_static = false;
8367   tree decl_type = DECL_CONTEXT (decl);
8368   tree type_root, decl_type_root;
8369
8370   if (decl_type == type
8371       || (TREE_CODE (decl) != FIELD_DECL && TREE_CODE (decl) != VAR_DECL))
8372     return 0;
8373   
8374   if (!INNER_CLASS_TYPE_P (type)
8375       && !(TREE_CODE (decl_type) == RECORD_TYPE
8376            && INNER_CLASS_TYPE_P (decl_type)))
8377     return 0;
8378
8379   is_static = FIELD_STATIC (decl);
8380
8381   /* If TYPE extends the declaration context of the non-static
8382      field we're trying to access, then this isn't a nested field
8383      access we need to worry about.  */
8384   if (!is_static && inherits_from_p (type, decl_type))
8385     return 0;
8386
8387   for (type_root = type;
8388        DECL_CONTEXT (TYPE_NAME (type_root));
8389        type_root = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type_root))))
8390     {
8391       if (type_root == decl_type)
8392         return 1;
8393     }
8394
8395   if (TREE_CODE (decl_type) == RECORD_TYPE
8396       && INNER_CLASS_TYPE_P (decl_type))
8397     {
8398       for (decl_type_root = decl_type;
8399            DECL_CONTEXT (TYPE_NAME (decl_type_root));
8400            decl_type_root
8401              = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (decl_type_root))))
8402         {
8403           if (decl_type_root == type)
8404             return 1;
8405         }
8406     }
8407   else
8408     decl_type_root = decl_type;
8409     
8410   if (type_root == decl_type_root)
8411     return 1;
8412
8413   /* Before we give up, see whether it is a non-static field
8414      inherited from the enclosing context we are considering.  */
8415   if (!DECL_CONTEXT (TYPE_NAME (type_root))
8416       && !is_static
8417       && inherits_from_p (type_root, decl_type))
8418     return 1;
8419
8420   return 0;
8421 }
8422
8423 /* Return a nonzero value if NODE represents a cross-nested-class 
8424    access that has already been expanded.  As a side effect, it returns
8425    the name of the field being accessed and the argument passed to the
8426    access function, suitable for a regeneration of the access method
8427    call if necessary.  */
8428
8429 static int
8430 nested_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8431                                 tree *arg)
8432 {
8433   int identified = 0;
8434
8435   if (TREE_CODE (node) != CALL_EXPR)
8436     return 0;
8437
8438   /* Well, GCJ generates slightly different tree nodes when compiling
8439      to native or bytecodes.  It's the case for function calls.  */
8440
8441   if (flag_emit_class_files
8442       && TREE_CODE (node) == CALL_EXPR
8443       && NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8444     identified = 1;
8445   else if (!flag_emit_class_files)
8446     {
8447       node = TREE_OPERAND (node, 0);
8448
8449       if (node && TREE_OPERAND (node, 0)
8450           && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8451         {
8452           node = TREE_OPERAND (node, 0);
8453           if (TREE_OPERAND (node, 0)
8454               && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8455               && (NESTED_FIELD_ACCESS_IDENTIFIER_P
8456                   (DECL_NAME (TREE_OPERAND (node, 0)))))
8457             identified = 1;
8458         }
8459     }
8460
8461   if (identified && name && arg_type && arg)
8462     {
8463       tree argument = TREE_OPERAND (node, 1);
8464       *name = DECL_NAME (TREE_OPERAND (node, 0));
8465
8466       /* The accessors for static fields do not take in a this$<n> argument,
8467          so we take the class name from the accessor's context instead.  */
8468       if (argument)
8469         {
8470           *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8471           *arg = TREE_VALUE (argument);
8472         }
8473       else
8474         {
8475           *arg_type = DECL_CONTEXT (TREE_OPERAND (node, 0));
8476           *arg = NULL_TREE;
8477         }
8478     }
8479   return identified;
8480 }
8481
8482 /* Detect in NODE cross-nested-class field read access and
8483    transform it into a write with RHS as an argument.  This function
8484    is called from the java_complete_lhs when an assignment to a LHS can
8485    be identified.  */
8486
8487 static tree
8488 nested_field_access_fix (tree wfl, tree node, tree rhs)
8489 {
8490   tree name, arg_type, arg;
8491
8492   if (nested_field_expanded_access_p (node, &name, &arg_type, &arg))
8493     {
8494       node = build_nested_field_access_expr (EXPR_WFL_LINECOL (wfl),
8495                                              arg_type, name, arg, rhs);
8496       return java_complete_tree (node);
8497     }
8498   return NULL_TREE;
8499 }
8500
8501 /* Construct the expression that calls an access method:
8502      <type>.access$<n>(<arg1> [, <arg2>]);
8503
8504    ARG2 can be NULL and will be omitted in that case. It will denote a
8505    read access.  */
8506
8507 static tree
8508 build_nested_field_access_expr (int lc, tree type, tree access_method_name,
8509                                 tree arg1, tree arg2)
8510 {
8511   tree args, cn, access;
8512
8513   if (arg1)
8514     args = build_tree_list (NULL_TREE, arg1);
8515   else
8516     args = NULL_TREE;
8517
8518   if (arg2)
8519     {
8520       if (args)
8521         args = tree_cons (NULL_TREE, arg2, args);
8522       else
8523         args = build_tree_list (NULL_TREE, arg2);
8524     }
8525
8526   access
8527     = build_method_invocation (build_wfl_node (access_method_name), args);
8528   cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8529
8530   return make_qualified_primary (cn, access, lc);
8531 }
8532
8533 /* Build the name of a synthetic accessor used to access class members
8534    across nested class boundaries.  */
8535
8536 static tree
8537 build_new_access_id (void)
8538 {
8539   static int access_n_counter = 1;
8540   char buffer [128];
8541
8542   sprintf (buffer, "access$%d", access_n_counter++);
8543   return get_identifier (buffer);
8544 }
8545
8546 /* Create the static access functions for the cross-nested-class field DECL.
8547    We define a read:
8548      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8549        return inst$.field;
8550      }
8551    and a write access:
8552      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8553                                      TREE_TYPE (<field>) value$) {
8554        return inst$.field = value$;
8555      }
8556    For static fields, these methods are generated without the instance
8557    parameter.
8558    We should have a usage flag on the DECL so we can lazily turn the ones
8559    we're using for code generation.  FIXME.
8560 */
8561
8562 static tree
8563 build_nested_field_access_methods (tree decl)
8564 {
8565   tree id, args, stmt, mdecl, class_name = NULL_TREE;
8566   bool is_static = FIELD_STATIC (decl);
8567
8568   if (FIELD_NESTED_ACCESS_P (decl))
8569     return FIELD_NESTED_ACCESS (decl);
8570
8571   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8572
8573   /* Create the identifier and a function named after it.  */
8574   id = build_new_access_id ();
8575
8576   /* The identifier is marked as bearing the name of a generated write
8577      access function for outer field accessed from inner classes.  */
8578   NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8579
8580   /* Create the read access.  */
8581   if (!is_static)
8582     {
8583       args = build_tree_list (inst_id,
8584                               build_pointer_type (DECL_CONTEXT (decl)));
8585       TREE_CHAIN (args) = end_params_node;
8586       stmt = make_qualified_primary (build_wfl_node (inst_id),
8587                                      build_wfl_node (DECL_NAME (decl)), 0);
8588     }
8589   else
8590     {
8591       args = end_params_node;
8592       class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
8593       stmt = make_qualified_primary (build_wfl_node (class_name),
8594                                      build_wfl_node (DECL_NAME (decl)), 0);
8595     }
8596   stmt = build_return (0, stmt);
8597   mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8598                                             TREE_TYPE (decl), id, args, stmt);
8599   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8600
8601   /* Create the write access method.  No write access for final variable */
8602   if (!FIELD_FINAL (decl))
8603     {
8604       if (!is_static)
8605         {
8606           args = build_tree_list (inst_id,
8607                                   build_pointer_type (DECL_CONTEXT (decl)));
8608           TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8609           TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8610           stmt = make_qualified_primary (build_wfl_node (inst_id),
8611                                          build_wfl_node (DECL_NAME (decl)),
8612                                          0);
8613         }
8614       else
8615         {
8616           args = build_tree_list (wpv_id, TREE_TYPE (decl));
8617           TREE_CHAIN (args) = end_params_node;
8618           stmt = make_qualified_primary (build_wfl_node (class_name),
8619                                          build_wfl_node (DECL_NAME (decl)),
8620                                          0);
8621         }
8622       stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8623                                                 build_wfl_node (wpv_id)));
8624       mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8625                                                 TREE_TYPE (decl), id,
8626                                                 args, stmt);
8627     }
8628   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8629
8630   /* Return the access name */
8631   return FIELD_NESTED_ACCESS (decl) = id;
8632 }
8633
8634 /* Build a field access method NAME.  */
8635
8636 static tree
8637 build_nested_field_access_method (tree class, tree type, tree name,
8638                                   tree args, tree body)
8639 {
8640   tree saved_current_function_decl, mdecl;
8641
8642   /* Create the method */
8643   mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8644   fix_method_argument_names (args, mdecl);
8645   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8646
8647   /* Attach the method body. */
8648   saved_current_function_decl = current_function_decl;
8649   start_artificial_method_body (mdecl);
8650   java_method_add_stmt (mdecl, body);
8651   end_artificial_method_body (mdecl);
8652   current_function_decl = saved_current_function_decl;
8653
8654   return mdecl;
8655 }
8656
8657 \f
8658 /* This section deals with building access function necessary for
8659    certain kinds of method invocation from inner classes.  */
8660
8661 static tree
8662 build_outer_method_access_method (tree decl)
8663 {
8664   tree saved_current_function_decl, mdecl;
8665   tree args = NULL_TREE, call_args = NULL_TREE;
8666   tree carg, id, body, class;
8667   char buffer [80];
8668   int parm_id_count = 0;
8669
8670   /* Test this abort with an access to a private field */
8671   if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8672     abort ();
8673
8674   /* Check the cache first */
8675   if (DECL_FUNCTION_INNER_ACCESS (decl))
8676     return DECL_FUNCTION_INNER_ACCESS (decl);
8677
8678   class = DECL_CONTEXT (decl);
8679
8680   /* Obtain an access identifier and mark it */
8681   id = build_new_access_id ();
8682   NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8683
8684   carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8685   /* Create the arguments, as much as the original */
8686   for (; carg && carg != end_params_node;
8687        carg = TREE_CHAIN (carg))
8688     {
8689       sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8690       args = chainon (args, build_tree_list (get_identifier (buffer),
8691                                              TREE_VALUE (carg)));
8692     }
8693   args = chainon (args, end_params_node);
8694
8695   /* Create the method */
8696   mdecl = create_artificial_method (class, ACC_STATIC,
8697                                     TREE_TYPE (TREE_TYPE (decl)), id, args);
8698   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8699   /* There is a potential bug here. We should be able to use
8700      fix_method_argument_names, but then arg names get mixed up and
8701      eventually a constructor will have its this$0 altered and the
8702      outer context won't be assignment properly. The testcase is
8703      stub.java FIXME */
8704   TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8705
8706   /* Attach the method body. */
8707   saved_current_function_decl = current_function_decl;
8708   start_artificial_method_body (mdecl);
8709
8710   /* The actual method invocation uses the same args. When invoking a
8711      static methods that way, we don't want to skip the first
8712      argument. */
8713   carg = args;
8714   if (!METHOD_STATIC (decl))
8715     carg = TREE_CHAIN (carg);
8716   for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8717     call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8718                            call_args);
8719
8720   body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8721                                   call_args);
8722   if (!METHOD_STATIC (decl))
8723     body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8724                                    body, 0);
8725   if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8726     body = build_return (0, body);
8727   java_method_add_stmt (mdecl,body);
8728   end_artificial_method_body (mdecl);
8729   current_function_decl = saved_current_function_decl;
8730
8731   /* Back tag the access function so it know what it accesses */
8732   DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8733
8734   /* Tag the current method so it knows it has an access generated */
8735   return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8736 }
8737
8738 \f
8739 /* This section of the code deals with building expressions to access
8740    the enclosing instance of an inner class. The enclosing instance is
8741    kept in a generated field called this$<n>, with <n> being the
8742    inner class nesting level (starting from 0.)  */
8743
8744 /* Build an access to a given this$<n>, always chaining access call to
8745    others. Access methods to this$<n> are build on the fly if
8746    necessary. This CAN'T be used to solely access this$<n-1> from
8747    this$<n> (which alway yield to special cases and optimization, see
8748    for example build_nested_field_access).  */
8749
8750 static tree
8751 build_access_to_thisn (tree from, tree to, int lc)
8752 {
8753   tree access = NULL_TREE;
8754
8755   while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8756     {
8757       if (!access)
8758         {
8759           access = build_current_thisn (from);
8760           access = build_wfl_node (access);
8761         }
8762       else
8763         {
8764           tree access0_wfl, cn;
8765
8766           maybe_build_thisn_access_method (from);
8767           access0_wfl = build_wfl_node (access0_identifier_node);
8768           cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8769           EXPR_WFL_LINECOL (access0_wfl) = lc;
8770           access = build_tree_list (NULL_TREE, access);
8771           access = build_method_invocation (access0_wfl, access);
8772           access = make_qualified_primary (cn, access, lc);
8773         }
8774
8775       /* If FROM isn't an inner class, that's fine, we've done enough.
8776          What we're looking for can be accessed from there.  */
8777       from = DECL_CONTEXT (TYPE_NAME (from));
8778       if (!from)
8779         break;
8780       from = TREE_TYPE (from);
8781     }
8782   return access;
8783 }
8784
8785 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8786    is returned if nothing needs to be generated. Otherwise, the method
8787    generated and a method decl is returned.
8788
8789    NOTE: These generated methods should be declared in a class file
8790    attribute so that they can't be referred to directly.  */
8791
8792 static tree
8793 maybe_build_thisn_access_method (tree type)
8794 {
8795   tree mdecl, args, stmt, rtype;
8796   tree saved_current_function_decl;
8797
8798   /* If TYPE is a top-level class, no access method is required.
8799      If there already is such an access method, bail out. */
8800   if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8801     return NULL_TREE;
8802
8803   /* We generate the method. The method looks like:
8804      static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8805   */
8806   args = build_tree_list (inst_id, build_pointer_type (type));
8807   TREE_CHAIN (args) = end_params_node;
8808   rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8809   mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8810                                     access0_identifier_node, args);
8811   fix_method_argument_names (args, mdecl);
8812   layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8813   stmt = build_current_thisn (type);
8814   stmt = make_qualified_primary (build_wfl_node (inst_id),
8815                                  build_wfl_node (stmt), 0);
8816   stmt = build_return (0, stmt);
8817
8818   saved_current_function_decl = current_function_decl;
8819   start_artificial_method_body (mdecl);
8820   java_method_add_stmt (mdecl, stmt);
8821   end_artificial_method_body (mdecl);
8822   current_function_decl = saved_current_function_decl;
8823
8824   CLASS_ACCESS0_GENERATED_P (type) = 1;
8825
8826   return mdecl;
8827 }
8828
8829 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8830    the first level of innerclassing. this$1 for the next one, etc...
8831    This function can be invoked with TYPE to NULL, available and then
8832    has to count the parser context.  */
8833
8834 static GTY(()) tree saved_thisn;
8835 static GTY(()) tree saved_type;
8836
8837 static tree
8838 build_current_thisn (tree type)
8839 {
8840   static int saved_i = -1;
8841   static int saved_type_i = 0;
8842   tree decl;
8843   char buffer [24];
8844   int i = 0;
8845
8846   if (type)
8847     {
8848       if (type == saved_type)
8849         i = saved_type_i;
8850       else
8851         {
8852           for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8853                decl; decl = DECL_CONTEXT (decl), i++)
8854             ;
8855
8856           saved_type = type;
8857           saved_type_i = i;
8858         }
8859     }
8860   else
8861     i = list_length (GET_CPC_LIST ())-2;
8862
8863   if (i == saved_i)
8864     return saved_thisn;
8865
8866   sprintf (buffer, "this$%d", i);
8867   saved_i = i;
8868   saved_thisn = get_identifier (buffer);
8869   return saved_thisn;
8870 }
8871
8872 /* Return the assignment to the hidden enclosing context `this$<n>'
8873    by the second incoming parameter to the innerclass constructor. The
8874    form used is `this.this$<n> = this$<n>;'.  */
8875
8876 static tree
8877 build_thisn_assign (void)
8878 {
8879   if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8880     {
8881       tree thisn = build_current_thisn (current_class);
8882       tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8883                                          build_wfl_node (thisn), 0);
8884       tree rhs = build_wfl_node (thisn);
8885 #ifdef USE_MAPPED_LOCATION
8886       SET_EXPR_LOCATION (lhs, input_location);
8887 #else
8888       EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8889 #endif
8890       return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8891     }
8892   return NULL_TREE;
8893 }
8894
8895 \f
8896 /* Building the synthetic `class$' used to implement the `.class' 1.1
8897    extension for non primitive types. This method looks like:
8898
8899     static Class class$(String type) throws NoClassDefFoundError
8900     {
8901       try {return (java.lang.Class.forName (String));}
8902       catch (ClassNotFoundException e) {
8903         throw new NoClassDefFoundError(e.getMessage());}
8904     } */
8905
8906 static GTY(()) tree get_message_wfl;
8907 static GTY(()) tree type_parm_wfl;
8908
8909 static tree
8910 build_dot_class_method (tree class)
8911 {
8912 #define BWF(S) build_wfl_node (get_identifier ((S)))
8913 #ifdef USE_MAPPED_LOCATION
8914 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8915 #else
8916 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8917 #endif
8918   tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8919   tree stmt, throw_stmt;
8920
8921   if (!get_message_wfl)
8922     {
8923       get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8924       type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8925     }
8926
8927   /* Build the arguments */
8928   args = build_tree_list (get_identifier ("type$"),
8929                           build_pointer_type (string_type_node));
8930   TREE_CHAIN (args) = end_params_node;
8931
8932   /* Build the qualified name java.lang.Class.forName */
8933   tmp = MQN (MQN (MQN (BWF ("java"),
8934                        BWF ("lang")), BWF ("Class")), BWF ("forName"));
8935
8936   /* Create the "class$" function */
8937   mdecl = create_artificial_method (class, ACC_STATIC,
8938                                     build_pointer_type (class_type_node),
8939                                     classdollar_identifier_node, args);
8940   qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8941                    BWF ("NoClassDefFoundError"));
8942   DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8943   register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8944   JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8945     &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8946
8947   /* We start by building the try block. We need to build:
8948        return (java.lang.Class.forName (type)); */
8949   stmt = build_method_invocation (tmp,
8950                                   build_tree_list (NULL_TREE, type_parm_wfl));
8951   stmt = build_return (0, stmt);
8952
8953   /* Now onto the catch block. We start by building the expression
8954      throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8955 #ifdef USE_MAPPED_LOCATION
8956   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8957                                     get_message_wfl, UNKNOWN_LOCATION);
8958 #else
8959   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8960                                     get_message_wfl, 0);
8961 #endif
8962   throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8963
8964   /* Build new NoClassDefFoundError (_.getMessage) */
8965   throw_stmt = build_new_invocation
8966     (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8967      build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8968
8969   /* Build the throw, (it's too early to use BUILD_THROW) */
8970   throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8971
8972   /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8973   qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8974                    BWF ("ClassNotFoundException"));
8975   stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8976
8977   fix_method_argument_names (args, mdecl);
8978   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8979   saved_current_function_decl = current_function_decl;
8980   start_artificial_method_body (mdecl);
8981   java_method_add_stmt (mdecl, stmt);
8982   end_artificial_method_body (mdecl);
8983   current_function_decl = saved_current_function_decl;
8984   TYPE_DOT_CLASS (class) = mdecl;
8985
8986   return mdecl;
8987 }
8988
8989 static tree
8990 build_dot_class_method_invocation (tree this_class, tree type)
8991 {
8992   tree dot_class_method = TYPE_DOT_CLASS (this_class);
8993   tree sig_id, s, t;
8994
8995   if (TYPE_ARRAY_P (type))
8996     sig_id = build_java_signature (type);
8997   else
8998     sig_id = DECL_NAME (TYPE_NAME (type));
8999
9000   /* Ensure that the proper name separator is used */
9001   sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
9002                                IDENTIFIER_LENGTH (sig_id));
9003
9004   s = build_string (IDENTIFIER_LENGTH (sig_id),
9005                     IDENTIFIER_POINTER (sig_id));
9006   t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
9007                                build_tree_list (NULL_TREE, s));
9008   if (DECL_CONTEXT (dot_class_method) != this_class)
9009     {
9010       tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
9011       t = make_qualified_primary (build_wfl_node (class_name), t, 0);
9012     }
9013   return t;
9014 }
9015
9016 /* This section of the code deals with constructor.  */
9017
9018 /* Craft a body for default constructor. Patch existing constructor
9019    bodies with call to super() and field initialization statements if
9020    necessary.  */
9021
9022 static void
9023 fix_constructors (tree mdecl)
9024 {
9025   tree iii;                     /* Instance Initializer Invocation */
9026   tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9027   tree thisn_assign, compound = NULL_TREE;
9028   tree class_type = DECL_CONTEXT (mdecl);
9029
9030   if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9031     return;
9032   DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9033
9034   if (!*bodyp)
9035     {
9036       /* It is an error for the compiler to generate a default
9037          constructor if the superclass doesn't have a constructor that
9038          takes no argument, or the same args for an anonymous class */
9039       if (verify_constructor_super (mdecl))
9040         {
9041           tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9042           tree save = DECL_NAME (mdecl);
9043           const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9044           DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9045           parse_error_context
9046             (lookup_cl (TYPE_NAME (class_type)),
9047              "No constructor matching %qs found in class %qs",
9048              lang_printable_name (mdecl, 2), n);
9049           DECL_NAME (mdecl) = save;
9050         }
9051
9052       /* The constructor body must be crafted by hand. It's the
9053          constructor we defined when we realize we didn't have the
9054          CLASSNAME() constructor */
9055       start_artificial_method_body (mdecl);
9056
9057       /* Insert an assignment to the this$<n> hidden field, if
9058          necessary */
9059       if ((thisn_assign = build_thisn_assign ()))
9060         java_method_add_stmt (mdecl, thisn_assign);
9061
9062       /* We don't generate a super constructor invocation if we're
9063          compiling java.lang.Object. build_super_invocation takes care
9064          of that. */
9065       java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9066
9067       /* FIXME */
9068       if ((iii = build_instinit_invocation (class_type)))
9069         java_method_add_stmt (mdecl, iii);
9070
9071       end_artificial_method_body (mdecl);
9072     }
9073   /* Search for an explicit constructor invocation */
9074   else
9075     {
9076       int found = 0;
9077       int invokes_this = 0;
9078       tree main_block = BLOCK_EXPR_BODY (*bodyp);
9079
9080       while (*bodyp)
9081         {
9082           tree body = *bodyp;
9083           switch (TREE_CODE (body))
9084             {
9085             case CALL_EXPR:
9086               found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9087               if (CALL_THIS_CONSTRUCTOR_P (body))
9088                 invokes_this = 1;
9089               break;
9090             case COMPOUND_EXPR:
9091             case EXPR_WITH_FILE_LOCATION:
9092               bodyp = &TREE_OPERAND (body, 0);
9093               continue;
9094             case BLOCK:
9095               bodyp = &BLOCK_EXPR_BODY (body);
9096               continue;
9097             default:
9098               break;
9099             }
9100           break;
9101         }
9102
9103       /* Generate the assignment to this$<n>, if necessary */
9104       if ((thisn_assign = build_thisn_assign ()))
9105         compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9106
9107       /* The constructor is missing an invocation of super() */
9108       if (!found)
9109         compound = add_stmt_to_compound (compound, NULL_TREE,
9110                                          build_super_invocation (mdecl));
9111       /* Explicit super() invocation should take place before the
9112          instance initializer blocks. */
9113       else
9114         {
9115           compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9116           *bodyp = build_java_empty_stmt ();
9117         }
9118
9119       DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9120
9121       /* Insert the instance initializer block right after. */
9122       if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9123         compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9124
9125       /* Fix the constructor main block if we're adding extra stmts */
9126       if (compound)
9127         {
9128           compound = add_stmt_to_compound (compound, NULL_TREE,
9129                                            BLOCK_EXPR_BODY (main_block));
9130           BLOCK_EXPR_BODY (main_block) = compound;
9131         }
9132     }
9133 }
9134
9135 /* Browse constructors in the super class, searching for a constructor
9136    that doesn't take any argument. Return 0 if one is found, 1
9137    otherwise.  If the current class is an anonymous inner class, look
9138    for something that has the same signature. */
9139
9140 static int
9141 verify_constructor_super (tree mdecl)
9142 {
9143   tree class = CLASSTYPE_SUPER (current_class);
9144   int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9145   tree sdecl;
9146
9147   if (!class)
9148     return 0;
9149
9150   if (ANONYMOUS_CLASS_P (current_class))
9151     {
9152       tree mdecl_arg_type;
9153       SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9154       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9155         if (DECL_CONSTRUCTOR_P (sdecl))
9156           {
9157             tree m_arg_type;
9158             tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9159             if (super_inner)
9160               arg_type = TREE_CHAIN (arg_type);
9161             for (m_arg_type = mdecl_arg_type;
9162                  (arg_type != end_params_node
9163                   && m_arg_type != end_params_node);
9164                  arg_type = TREE_CHAIN (arg_type),
9165                    m_arg_type = TREE_CHAIN (m_arg_type))
9166               if (!valid_method_invocation_conversion_p
9167                      (TREE_VALUE (arg_type),
9168                       TREE_VALUE (m_arg_type)))
9169                 break;
9170
9171             if (arg_type == end_params_node && m_arg_type == end_params_node)
9172               return 0;
9173           }
9174     }
9175   else
9176     {
9177       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9178         {
9179           tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9180           if (super_inner)
9181             arg = TREE_CHAIN (arg);
9182           if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9183             return 0;
9184         }
9185     }
9186   return 1;
9187 }
9188
9189 /* Generate code for all context remembered for code generation.  */
9190
9191 static GTY(()) tree reversed_class_list;
9192 void
9193 java_expand_classes (void)
9194 {
9195   int save_error_count = 0;
9196   static struct parser_ctxt *cur_ctxp = NULL;
9197   location_t save_location;
9198
9199   java_parse_abort_on_error ();
9200   if (!(ctxp = ctxp_for_generation))
9201     return;
9202   java_layout_classes ();
9203   java_parse_abort_on_error ();
9204   save_location = input_location;
9205
9206   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9207     {
9208       tree current;
9209       for (current = cur_ctxp->class_list; 
9210            current; 
9211            current = TREE_CHAIN (current))
9212         gen_indirect_dispatch_tables (TREE_TYPE (current));
9213     }
9214   
9215   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9216     {
9217       ctxp = cur_ctxp;
9218       input_location = ctxp->file_start_location;
9219       lang_init_source (2);            /* Error msgs have method prototypes */
9220       java_complete_expand_classes (); /* Complete and expand classes */
9221       java_parse_abort_on_error ();
9222     }
9223   input_location = save_location;
9224
9225   /* Find anonymous classes and expand their constructor. This extra pass is
9226      necessary because the constructor itself is only generated when the
9227      method in which it is defined is expanded. */
9228   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9229     {
9230       tree current;
9231       ctxp = cur_ctxp;
9232       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9233         {
9234           output_class = current_class = TREE_TYPE (current);
9235           if (ANONYMOUS_CLASS_P (current_class))
9236             {
9237               tree d;
9238               for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9239                 {
9240                   if (DECL_CONSTRUCTOR_P (d))
9241                     {
9242                       java_complete_expand_method (d);
9243                       break;    /* There is only one constructor. */
9244                     }
9245                 }
9246             }
9247         }
9248     }
9249
9250   /* Expanding the constructors of anonymous classes generates access
9251      methods.  Scan all the methods looking for null DECL_RESULTs --
9252      this will be the case if a method hasn't been expanded.  */
9253   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9254     {
9255       tree current;
9256       ctxp = cur_ctxp;
9257       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9258         {
9259           tree d;
9260           output_class = current_class = TREE_TYPE (current);
9261           for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9262             {
9263               if (DECL_RESULT (d) == NULL_TREE)
9264                 java_complete_expand_method (d);
9265             }
9266         }
9267     }
9268
9269   /* ???  Instead of all this we could iterate around the list of
9270      classes until there were no more un-expanded methods.  It would
9271      take a little longer -- one pass over the whole list of methods
9272      -- but it would be simpler.  Like this:  */
9273 #if 0
9274     {
9275       int something_changed;
9276     
9277       do
9278         {
9279           something_changed = 0;
9280           for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9281             {
9282               tree current;
9283               ctxp = cur_ctxp;
9284               for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9285                 {
9286                   tree d;
9287                   output_class = current_class = TREE_TYPE (current);
9288                   for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9289                     {
9290                       if (DECL_RESULT (d) == NULL_TREE)
9291                         {
9292                           something_changed = 1;
9293                           java_complete_expand_method (d);
9294                         }
9295                     }
9296                 }
9297             }
9298         }
9299       while (something_changed);
9300     }
9301 #endif
9302
9303   /* If we've found error at that stage, don't try to generate
9304      anything, unless we're checking the syntax only
9305      (but not using -fsyntax-only for the purpose of generating
9306      bytecode).  */
9307   if (java_error_count
9308       && (!flag_syntax_only && !flag_emit_class_files))
9309     return;
9310
9311   /* Now things are stable, go for generation of the class data. */
9312
9313   /* We pessimistically marked all methods and fields external until
9314      we knew what set of classes we were planning to compile.  Now mark
9315      those that will be generated locally as not external.  */
9316   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9317     {
9318       tree current;
9319       ctxp = cur_ctxp;
9320       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9321         java_mark_class_local (TREE_TYPE (current));
9322     }
9323
9324   /* Compile the classes.  */
9325   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9326     {
9327       tree current;
9328       reversed_class_list = NULL;
9329
9330       ctxp = cur_ctxp;
9331
9332       /* We write out the classes in reverse order.  This ensures that
9333          inner classes are written before their containing classes,
9334          which is important for parallel builds.  Otherwise, the
9335          class file for the outer class may be found, but the class
9336          file for the inner class may not be present.  In that
9337          situation, the compiler cannot fall back to the original
9338          source, having already read the outer class, so we must
9339          prevent that situation.  */
9340       for (current = ctxp->class_list;
9341            current;
9342            current = TREE_CHAIN (current))
9343         reversed_class_list
9344           = tree_cons (NULL_TREE, current, reversed_class_list);
9345
9346       for (current = reversed_class_list;
9347            current;
9348            current = TREE_CHAIN (current))
9349         {
9350           output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9351           if (flag_emit_class_files)
9352             write_classfile (current_class);
9353           else if (! flag_syntax_only)
9354             java_expand_method_bodies (current_class);
9355         }
9356     }
9357 }
9358
9359 void
9360 java_finish_classes (void)
9361 {
9362   static struct parser_ctxt *cur_ctxp = NULL;
9363   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9364     {
9365       tree current;
9366       ctxp = cur_ctxp;
9367       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9368         {
9369           output_class = current_class = TREE_TYPE (current);
9370           finish_class ();
9371         }
9372     }
9373 }
9374
9375 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9376    a tree list node containing RIGHT. Fore coming RIGHTs will be
9377    chained to this hook. LOCATION contains the location of the
9378    separating `.' operator.  */
9379
9380 static tree
9381 make_qualified_primary (tree primary, tree right, int location)
9382 {
9383   tree wfl;
9384
9385   if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9386     wfl = build_wfl_wrap (primary, location);
9387   else
9388     {
9389       wfl = primary;
9390       /* If wfl wasn't qualified, we build a first anchor */
9391       if (!EXPR_WFL_QUALIFICATION (wfl))
9392         EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9393     }
9394
9395   /* And chain them */
9396   EXPR_WFL_LINECOL (right) = location;
9397   chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9398   PRIMARY_P (wfl) =  1;
9399   return wfl;
9400 }
9401
9402 /* Simple merge of two name separated by a `.' */
9403
9404 static tree
9405 merge_qualified_name (tree left, tree right)
9406 {
9407   tree node;
9408   if (!left && !right)
9409     return NULL_TREE;
9410
9411   if (!left)
9412     return right;
9413
9414   if (!right)
9415     return left;
9416
9417   obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9418                 IDENTIFIER_LENGTH (left));
9419   obstack_1grow (&temporary_obstack, '.');
9420   obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9421                  IDENTIFIER_LENGTH (right));
9422   node =  get_identifier (obstack_base (&temporary_obstack));
9423   obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9424   QUALIFIED_P (node) = 1;
9425   return node;
9426 }
9427
9428 /* Merge the two parts of a qualified name into LEFT.  Set the
9429    location information of the resulting node to LOCATION, usually
9430    inherited from the location information of the `.' operator. */
9431
9432 static tree
9433 make_qualified_name (tree left, tree right,
9434 #ifdef USE_MAPPED_LOCATION
9435                      source_location location
9436 #else
9437                      int location
9438 #endif
9439                      )
9440 {
9441 #ifdef USE_COMPONENT_REF
9442   tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9443   SET_EXPR_LOCATION (node, location);
9444   return node;
9445 #else
9446   tree left_id = EXPR_WFL_NODE (left);
9447   tree right_id = EXPR_WFL_NODE (right);
9448   tree wfl, merge;
9449
9450   merge = merge_qualified_name (left_id, right_id);
9451
9452   /* Left wasn't qualified and is now qualified */
9453 #ifdef USE_MAPPED_LOCATION
9454   if (!QUALIFIED_P (left_id))
9455     {
9456       tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9457       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9458     }
9459
9460   wfl = build_expr_wfl (right_id, location);
9461 #else
9462   if (!QUALIFIED_P (left_id))
9463     {
9464       tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9465       EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9466       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9467     }
9468
9469   wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9470   EXPR_WFL_LINECOL (wfl) = location;
9471 #endif
9472   chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9473   EXPR_WFL_NODE (left) = merge;
9474   return left;
9475 #endif
9476 }
9477
9478 /* Extract the last identifier component of the qualified in WFL. The
9479    last identifier is removed from the linked list */
9480
9481 static tree
9482 cut_identifier_in_qualified (tree wfl)
9483 {
9484   tree q;
9485   tree previous = NULL_TREE;
9486   for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9487     if (!TREE_CHAIN (q))
9488       {
9489         if (!previous)
9490           /* Operating on a non qualified qualified WFL.  */
9491           abort ();
9492
9493         TREE_CHAIN (previous) = NULL_TREE;
9494         return TREE_PURPOSE (q);
9495       }
9496 }
9497
9498 /* Resolve the expression name NAME. Return its decl.  */
9499
9500 static tree
9501 resolve_expression_name (tree id, tree *orig)
9502 {
9503   tree name = EXPR_WFL_NODE (id);
9504   tree decl;
9505
9506   /* 6.5.5.1: Simple expression names */
9507   if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9508     {
9509       /* 15.13.1: NAME can appear within the scope of a local variable
9510          declaration */
9511       if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9512         return decl;
9513
9514       /* 15.13.1: NAME can appear within a class declaration */
9515       else
9516         {
9517           decl = lookup_field_wrapper (current_class, name);
9518           if (decl)
9519             {
9520               tree access = NULL_TREE;
9521               int fs = FIELD_STATIC (decl);
9522
9523               /* If we're accessing an outer scope local alias, make
9524                  sure we change the name of the field we're going to
9525                  build access to. */
9526               if (FIELD_LOCAL_ALIAS_USED (decl))
9527                 name = DECL_NAME (decl);
9528
9529               check_deprecation (id, decl);
9530
9531               /* Instance variable (8.3.1.1) can't appear within
9532                  static method, static initializer or initializer for
9533                  a static variable. */
9534               if (!fs && METHOD_STATIC (current_function_decl))
9535                 {
9536                   static_ref_err (id, name, current_class);
9537                   return error_mark_node;
9538                 }
9539               /* Instance variables can't appear as an argument of
9540                  an explicit constructor invocation */
9541               if (!fs && ctxp->explicit_constructor_p
9542                   && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9543                 {
9544                   parse_error_context
9545                     (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9546                   return error_mark_node;
9547                 }
9548
9549               /* If we're processing an inner class and we're trying
9550                  to access a field belonging to an outer class, build
9551                  the access to the field.  */
9552               if (nested_field_access_p (current_class, decl))
9553                 {
9554                   if (!fs && CLASS_STATIC (TYPE_NAME (current_class)))
9555                     {
9556                       static_ref_err (id, DECL_NAME (decl), current_class);
9557                       return error_mark_node;
9558                     }
9559                   access = build_nested_field_access (id, decl);
9560                   if (orig)
9561                     *orig = access;
9562                   return access;
9563                 }
9564
9565               /* Otherwise build what it takes to access the field */
9566               access = build_field_ref ((fs ? NULL_TREE : current_this),
9567                                         DECL_CONTEXT (decl), name);
9568               if (fs)
9569                 access = maybe_build_class_init_for_field (decl, access);
9570               /* We may be asked to save the real field access node */
9571               if (orig)
9572                 *orig = access;
9573               /* Last check: can we access the field? */
9574               if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9575                 {
9576                   not_accessible_field_error (id, decl);
9577                   return error_mark_node;
9578                 }
9579               /* And we return what we got */
9580               return access;
9581             }
9582           /* Fall down to error report on undefined variable */
9583         }
9584     }
9585   /* 6.5.5.2 Qualified Expression Names */
9586   else
9587     {
9588       if (orig)
9589         *orig = NULL_TREE;
9590       qualify_ambiguous_name (id);
9591       /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9592       /* 15.10.2: Accessing Superclass Members using super */
9593       return resolve_field_access (id, orig, NULL);
9594     }
9595
9596   /* We've got an error here */
9597   if (INNER_CLASS_TYPE_P (current_class))
9598     parse_error_context (id,
9599                          "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9600                          IDENTIFIER_POINTER (name),
9601                          IDENTIFIER_POINTER (DECL_NAME
9602                                              (TYPE_NAME (current_class))));
9603   else
9604     parse_error_context (id, "Undefined variable %qs",
9605                          IDENTIFIER_POINTER (name));
9606
9607   return error_mark_node;
9608 }
9609
9610 static void
9611 static_ref_err (tree wfl, tree field_id, tree class_type)
9612 {
9613   parse_error_context
9614     (wfl,
9615      "Can't make a static reference to nonstatic variable %qs in class %qs",
9616      IDENTIFIER_POINTER (field_id),
9617      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9618 }
9619
9620 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9621    We return something suitable to generate the field access. We also
9622    return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
9623    recipient's address can be null. */
9624
9625 static tree
9626 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9627 {
9628   int is_static = 0;
9629   tree field_ref;
9630   tree decl = NULL_TREE, where_found, type_found;
9631
9632   if (resolve_qualified_expression_name (qual_wfl, &decl,
9633                                          &where_found, &type_found))
9634     return error_mark_node;
9635
9636   /* Resolve the LENGTH field of an array here */
9637   if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9638       && type_found && TYPE_ARRAY_P (type_found)
9639       && ! flag_emit_class_files)
9640     {
9641       tree length = build_java_array_length_access (where_found);
9642       field_ref = length;
9643
9644       /* In case we're dealing with a static array, we need to
9645          initialize its class before the array length can be fetched.  */
9646       if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9647         {
9648           build_static_field_ref (where_found);
9649           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9650         }
9651     }
9652   /* We might have been trying to resolve field.method(). In which
9653      case, the resolution is over and decl is the answer */
9654   else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9655     field_ref = decl;
9656   else if (JDECL_P (decl))
9657     {
9658       if (!type_found)
9659         type_found = DECL_CONTEXT (decl);
9660       is_static = FIELD_STATIC (decl);
9661       field_ref = build_field_ref ((is_static ?
9662                                     NULL_TREE : where_found),
9663                                    type_found, DECL_NAME (decl));
9664       if (field_ref == error_mark_node)
9665         return error_mark_node;
9666       if (is_static)
9667         field_ref = maybe_build_class_init_for_field (decl, field_ref);
9668
9669       /* If we're looking at a static field, we may need to generate a
9670          class initialization for it.  This can happen when the access
9671          looks like `field.ref', where `field' is a static field in an
9672          interface we implement.  */
9673       if (!flag_emit_class_files
9674           && TREE_CODE (where_found) == VAR_DECL
9675           && FIELD_STATIC (where_found))
9676         {
9677           build_static_field_ref (where_found);
9678           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9679         }
9680     }
9681   else
9682     field_ref = decl;
9683
9684   if (field_decl)
9685     *field_decl = decl;
9686   if (field_type)
9687     *field_type = (QUAL_DECL_TYPE (decl) ?
9688                    QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9689   return field_ref;
9690 }
9691
9692 /* If NODE is an access to a static field, strip out the class
9693    initialization part and return the field decl, otherwise, return
9694    NODE. */
9695
9696 tree
9697 extract_field_decl (tree node)
9698 {
9699   if (TREE_CODE (node) == COMPOUND_EXPR)
9700     {
9701       tree op1 = TREE_OPERAND (node, 1);
9702       if (TREE_CODE (op1) == COMPOUND_EXPR)
9703          {
9704            tree call = TREE_OPERAND (op1, 0);
9705            if (TREE_CODE (call) == CALL_EXPR
9706                && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9707                && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9708                    == soft_initclass_node))
9709              return TREE_OPERAND (op1, 1);
9710          }
9711       else if (JDECL_P (op1))
9712         return op1;
9713     }
9714   return node;
9715 }
9716
9717 /* 6.5.5.2: Qualified Expression Names */
9718
9719 static int
9720 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9721                                    tree *where_found, tree *type_found)
9722 {
9723   int from_type = 0;            /* Field search initiated from a type */
9724   int from_super = 0, from_cast = 0, from_qualified_this = 0;
9725   int previous_call_static = 0;
9726   int is_static;
9727   tree decl = NULL_TREE, type = NULL_TREE, q;
9728   /* For certain for of inner class instantiation */
9729   tree saved_current, saved_this;
9730 #define RESTORE_THIS_AND_CURRENT_CLASS                          \
9731   { current_class = saved_current; current_this = saved_this;}
9732
9733   *type_found = *where_found = NULL_TREE;
9734
9735   for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9736     {
9737       tree qual_wfl = QUAL_WFL (q);
9738       tree ret_decl;            /* for EH checking */
9739 #ifdef USE_MAPPED_LOCATION
9740       source_location location;  /* for EH checking */
9741 #else
9742       int location;             /* for EH checking */
9743 #endif
9744
9745       /* 15.10.1 Field Access Using a Primary */
9746       switch (TREE_CODE (qual_wfl))
9747         {
9748         case CALL_EXPR:
9749         case NEW_CLASS_EXPR:
9750           /* If the access to the function call is a non static field,
9751              build the code to access it. */
9752           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9753             {
9754               decl = maybe_access_field (decl, *where_found,
9755                                          DECL_CONTEXT (decl));
9756               if (decl == error_mark_node)
9757                 return 1;
9758             }
9759
9760           /* And code for the function call */
9761           if (complete_function_arguments (qual_wfl))
9762             return 1;
9763
9764           /* We might have to setup a new current class and a new this
9765              for the search of an inner class, relative to the type of
9766              a expression resolved as `decl'. The current values are
9767              saved and restored shortly after */
9768           saved_current = current_class;
9769           saved_this = current_this;
9770           if (decl
9771               && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9772                   || from_qualified_this))
9773             {
9774               /* If we still have `from_qualified_this', we have the form
9775                  <T>.this.f() and we need to build <T>.this */
9776               if (from_qualified_this)
9777                 {
9778                   decl = build_access_to_thisn (current_class, type, 0);
9779                   decl = java_complete_tree (decl);
9780                   type = TREE_TYPE (TREE_TYPE (decl));
9781                 }
9782               current_class = type;
9783               current_this = decl;
9784               from_qualified_this = 0;
9785             }
9786
9787           if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9788             CALL_USING_SUPER (qual_wfl) = 1;
9789 #ifdef USE_MAPPED_LOCATION
9790           location = (TREE_CODE (qual_wfl) == CALL_EXPR
9791                       ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9792                       : UNKNOWN_LOCATION);
9793 #else
9794           location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9795                       EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9796 #endif
9797           *where_found = patch_method_invocation (qual_wfl, decl, type,
9798                                                   from_super,
9799                                                   &is_static, &ret_decl);
9800           from_super = 0;
9801           if (*where_found == error_mark_node)
9802             {
9803               RESTORE_THIS_AND_CURRENT_CLASS;
9804               return 1;
9805             }
9806           *type_found = type = QUAL_DECL_TYPE (*where_found);
9807
9808           *where_found = force_evaluation_order (*where_found);
9809
9810           /* If we're creating an inner class instance, check for that
9811              an enclosing instance is in scope */
9812           if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9813               && INNER_ENCLOSING_SCOPE_CHECK (type))
9814             {
9815               parse_error_context
9816                 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9817                  lang_printable_name (type, 0),
9818                  (!current_this ? "" :
9819                   "; an explicit one must be provided when creating this inner class"));
9820               RESTORE_THIS_AND_CURRENT_CLASS;
9821               return 1;
9822             }
9823
9824           /* In case we had to change then to resolve a inner class
9825              instantiation using a primary qualified by a `new' */
9826           RESTORE_THIS_AND_CURRENT_CLASS;
9827
9828 #ifdef USE_MAPPED_LOCATION
9829           if (location != UNKNOWN_LOCATION)
9830 #else
9831           if (location)
9832 #endif
9833             {
9834               tree arguments = NULL_TREE;
9835               if (TREE_CODE (qual_wfl) == CALL_EXPR
9836                   && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9837                 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9838               check_thrown_exceptions (location, ret_decl, arguments);
9839             }
9840
9841           /* If the previous call was static and this one is too,
9842              build a compound expression to hold the two (because in
9843              that case, previous function calls aren't transported as
9844              forcoming function's argument. */
9845           if (previous_call_static && is_static)
9846             {
9847               /* We must set CAN_COMPLETE_NORMALLY for the first call
9848                  since it is done nowhere else.  */
9849               CAN_COMPLETE_NORMALLY (decl) = 1;
9850               decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9851                              decl, *where_found);
9852               TREE_SIDE_EFFECTS (decl) = 1;
9853             }
9854           else
9855             {
9856               previous_call_static = is_static;
9857               decl = *where_found;
9858             }
9859           from_type = 0;
9860           continue;
9861
9862         case NEW_ARRAY_EXPR:
9863         case NEW_ANONYMOUS_ARRAY_EXPR:
9864           *where_found = decl = java_complete_tree (qual_wfl);
9865           if (decl == error_mark_node)
9866             return 1;
9867           *type_found = type = QUAL_DECL_TYPE (decl);
9868           continue;
9869
9870         case CONVERT_EXPR:
9871           *where_found = decl = java_complete_tree (qual_wfl);
9872           if (decl == error_mark_node)
9873             return 1;
9874           *type_found = type = QUAL_DECL_TYPE (decl);
9875           from_cast = 1;
9876           continue;
9877
9878         case CONDITIONAL_EXPR:
9879         case STRING_CST:
9880         case MODIFY_EXPR:
9881           *where_found = decl = java_complete_tree (qual_wfl);
9882           if (decl == error_mark_node)
9883             return 1;
9884           *type_found = type = QUAL_DECL_TYPE (decl);
9885           continue;
9886
9887         case ARRAY_REF:
9888           /* If the access to the function call is a non static field,
9889              build the code to access it. */
9890           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9891             {
9892               decl = maybe_access_field (decl, *where_found, type);
9893               if (decl == error_mark_node)
9894                 return 1;
9895             }
9896           /* And code for the array reference expression */
9897           decl = java_complete_tree (qual_wfl);
9898           if (decl == error_mark_node)
9899             return 1;
9900           type = QUAL_DECL_TYPE (decl);
9901           continue;
9902
9903         case PLUS_EXPR:
9904           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9905             return 1;
9906           if ((type = patch_string (decl)))
9907             decl = type;
9908           *where_found = QUAL_RESOLUTION (q) = decl;
9909           *type_found = type = TREE_TYPE (decl);
9910           break;
9911
9912         case CLASS_LITERAL:
9913           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9914             return 1;
9915           *where_found = QUAL_RESOLUTION (q) = decl;
9916           *type_found = type = TREE_TYPE (decl);
9917           break;
9918
9919         default:
9920           /* Fix for -Wall Just go to the next statement. Don't
9921              continue */
9922           break;
9923         }
9924
9925       /* If we fall here, we weren't processing a (static) function call. */
9926       previous_call_static = 0;
9927
9928       /* It can be the keyword THIS */
9929       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9930           && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9931         {
9932           if (!current_this)
9933             {
9934               parse_error_context
9935                 (wfl, "Keyword %<this%> used outside allowed context");
9936               return 1;
9937             }
9938           if (ctxp->explicit_constructor_p
9939               && type == current_class)
9940             {
9941               parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9942               return 1;
9943             }
9944           /* We have to generate code for intermediate access */
9945           if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9946             {
9947               *where_found = decl = current_this;
9948               *type_found = type = QUAL_DECL_TYPE (decl);
9949             }
9950           /* We're trying to access the this from somewhere else. Make sure
9951              it's allowed before doing so. */
9952           else
9953             {
9954               if (!enclosing_context_p (type, current_class))
9955                 {
9956                   char *p  = xstrdup (lang_printable_name (type, 0));
9957                   parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9958                                        p, p,
9959                                        lang_printable_name (current_class, 0));
9960                   free (p);
9961                   return 1;
9962                 }
9963               from_qualified_this = 1;
9964               /* If there's nothing else after that, we need to
9965                  produce something now, otherwise, the section of the
9966                  code that needs to produce <T>.this will generate
9967                  what is necessary. */
9968               if (!TREE_CHAIN (q))
9969                 {
9970                   decl = build_access_to_thisn (current_class, type, 0);
9971                   *where_found = decl = java_complete_tree (decl);
9972                   *type_found = type = TREE_TYPE (decl);
9973                 }
9974             }
9975
9976           from_type = 0;
9977           continue;
9978         }
9979
9980       /* 15.10.2 Accessing Superclass Members using SUPER */
9981       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9982           && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9983         {
9984           tree node;
9985           /* Check on the restricted use of SUPER */
9986           if (METHOD_STATIC (current_function_decl)
9987               || current_class == object_type_node)
9988             {
9989               parse_error_context
9990                 (wfl, "Keyword %<super%> used outside allowed context");
9991               return 1;
9992             }
9993           /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9994           node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9995                              CLASSTYPE_SUPER (current_class),
9996                              build_this (EXPR_WFL_LINECOL (qual_wfl)));
9997           *where_found = decl = java_complete_tree (node);
9998           if (decl == error_mark_node)
9999             return 1;
10000           *type_found = type = QUAL_DECL_TYPE (decl);
10001           from_super = from_type = 1;
10002           continue;
10003         }
10004
10005       /* 15.13.1: Can't search for field name in packages, so we
10006          assume a variable/class name was meant. */
10007       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10008         {
10009           tree name;
10010           if ((decl = resolve_package (wfl, &q, &name)))
10011             {
10012               tree list;
10013               *where_found = decl;
10014
10015               check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10016
10017               /* We want to be absolutely sure that the class is laid
10018                  out. We're going to search something inside it. */
10019               *type_found = type = TREE_TYPE (decl);
10020               layout_class (type);
10021               from_type = 1;
10022
10023               /* Fix them all the way down, if any are left. */
10024               if (q)
10025                 {
10026                   list = TREE_CHAIN (q);
10027                   while (list)
10028                     {
10029                       RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10030                       list = TREE_CHAIN (list);
10031                     }
10032                 }
10033             }
10034           else
10035             {
10036               if (from_super || from_cast)
10037                 parse_error_context
10038                   ((from_cast ? qual_wfl : wfl),
10039                    "No variable %qs defined in class %qs",
10040                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10041                    lang_printable_name (type, 0));
10042               else
10043                 parse_error_context
10044                   (qual_wfl, "Undefined variable or class name: %qs",
10045                    IDENTIFIER_POINTER (name));
10046               return 1;
10047             }
10048         }
10049
10050       /* We have a type name. It's been already resolved when the
10051          expression was qualified. */
10052       else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10053         {
10054           decl = QUAL_RESOLUTION (q);
10055
10056           /* Sneak preview. If next we see a `new', we're facing a
10057              qualification which resulted in a type being selected
10058              instead of a field.  Report the error.  */
10059           if(TREE_CHAIN (q)
10060              && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10061             {
10062               parse_error_context (qual_wfl, "Undefined variable %qs",
10063                                    IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10064               return 1;
10065             }
10066
10067           check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10068           
10069           check_deprecation (qual_wfl, decl);
10070
10071           type = TREE_TYPE (decl);
10072           from_type = 1;
10073         }
10074       /* We resolve an expression name */
10075       else
10076         {
10077           tree field_decl = NULL_TREE;
10078
10079           /* If there exists an early resolution, use it. That occurs
10080              only once and we know that there are more things to
10081              come. Don't do that when processing something after SUPER
10082              (we need more thing to be put in place below */
10083           if (!from_super && QUAL_RESOLUTION (q))
10084             {
10085               decl = QUAL_RESOLUTION (q);
10086               if (!type)
10087                 {
10088                   if (TREE_CODE (decl) == FIELD_DECL
10089                       || TREE_CODE (decl) == VAR_DECL)
10090                     {
10091                       if (TREE_CODE (decl) == FIELD_DECL
10092                           && !FIELD_STATIC (decl))
10093                         {
10094                           if (current_this)
10095                             *where_found = current_this;
10096                           else
10097                             {
10098                               static_ref_err (qual_wfl, DECL_NAME (decl),
10099                                               current_class);
10100                               return 1;
10101                             }
10102                         }
10103                       else
10104                         {
10105                           *where_found = TREE_TYPE (decl);
10106                           if (TREE_CODE (*where_found) == POINTER_TYPE)
10107                             *where_found = TREE_TYPE (*where_found);
10108                         }
10109                       if (nested_field_access_p (current_class, decl))
10110                         decl = build_nested_field_access (qual_wfl, decl);
10111                     }
10112                   else
10113                     {
10114                       *where_found = TREE_TYPE (decl);
10115                       if (TREE_CODE (*where_found) == POINTER_TYPE)
10116                         *where_found = TREE_TYPE (*where_found);
10117                     }
10118                 }
10119             }
10120
10121           /* Report and error if we're using a numerical literal as a
10122              qualifier. It can only be an INTEGER_CST. */
10123           else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10124             {
10125               parse_error_context
10126                 (wfl, "Can't use type %qs as a qualifier",
10127                  lang_printable_name (TREE_TYPE (qual_wfl), 0));
10128               return 1;
10129             }
10130
10131           /* We have to search for a field, knowing the type of its
10132              container. The flag FROM_TYPE indicates that we resolved
10133              the last member of the expression as a type name, which
10134              means that for the resolution of this field, we'll look
10135              for other errors than if it was resolved as a member of
10136              an other field. */
10137           else
10138             {
10139               int is_static;
10140               tree field_decl_type; /* For layout */
10141
10142               if (!from_type && !JREFERENCE_TYPE_P (type))
10143                 {
10144                   parse_error_context
10145                     (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10146                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10147                      lang_printable_name (type, 0),
10148                      IDENTIFIER_POINTER (DECL_NAME (decl)));
10149                   return 1;
10150                 }
10151
10152               field_decl = lookup_field_wrapper (type,
10153                                                  EXPR_WFL_NODE (qual_wfl));
10154
10155               /* Maybe what we're trying to access to is an inner
10156                  class, only if decl is a TYPE_DECL. */
10157               if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10158                 {
10159                   tree ptr, inner_decl;
10160
10161                   BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10162                   inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10163                   if (inner_decl)
10164                     {
10165                       check_inner_class_access (inner_decl, decl, qual_wfl);
10166                       type = TREE_TYPE (inner_decl);
10167                       decl = inner_decl;
10168                       from_type = 1;
10169                       continue;
10170                     }
10171                 }
10172
10173               if (field_decl == NULL_TREE)
10174                 {
10175                   parse_error_context
10176                     (qual_wfl, "No variable %qs defined in type %qs",
10177                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10178                      GET_TYPE_NAME (type));
10179                   return 1;
10180                 }
10181               if (field_decl == error_mark_node)
10182                 return 1;
10183
10184               /* Layout the type of field_decl, since we may need
10185                  it. Don't do primitive types or loaded classes. The
10186                  situation of non primitive arrays may not handled
10187                  properly here. FIXME */
10188               if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10189                 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10190               else
10191                 field_decl_type = TREE_TYPE (field_decl);
10192               if (!JPRIMITIVE_TYPE_P (field_decl_type)
10193                   && !CLASS_LOADED_P (field_decl_type)
10194                   && !TYPE_ARRAY_P (field_decl_type))
10195                 resolve_and_layout (field_decl_type, NULL_TREE);
10196
10197               /* Check on accessibility here */
10198               if (not_accessible_p (current_class, field_decl,
10199                                     *type_found, from_super))
10200                 return not_accessible_field_error (qual_wfl,field_decl);    
10201               check_deprecation (qual_wfl, field_decl);
10202
10203               /* There are things to check when fields are accessed
10204                  from type. There are no restrictions on a static
10205                  declaration of the field when it is accessed from an
10206                  interface */
10207               is_static = FIELD_STATIC (field_decl);
10208               if (!from_super && from_type
10209                   && !TYPE_INTERFACE_P (type)
10210                   && !is_static
10211                   && (current_function_decl
10212                       && METHOD_STATIC (current_function_decl)))
10213                 {
10214                   static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10215                   return 1;
10216                 }
10217               from_cast = from_super = 0;
10218
10219               /* If it's an access from a type but isn't static, we
10220                  make it relative to `this'. */
10221               if (!is_static && from_type)
10222                 decl = current_this;
10223
10224               /* If we need to generate something to get a proper
10225                  handle on what this field is accessed from, do it
10226                  now. */
10227               if (!is_static)
10228                 {
10229                   decl = maybe_access_field (decl, *where_found, *type_found);
10230                   if (decl == error_mark_node)
10231                     return 1;
10232                 }
10233
10234               /* We want to keep the location where we found it, and the
10235                  type we found.  */
10236               *where_found = decl;
10237               *type_found = type;
10238
10239               /* Generate the correct expression for field access from
10240                  qualified this */
10241               if (from_qualified_this)
10242                 {
10243                   field_decl
10244                     = build_nested_field_access (qual_wfl, field_decl);
10245                   from_qualified_this = 0;
10246                 }
10247
10248               /* If needed, generate accessors for static field access.  */
10249               if (is_static
10250                   && FIELD_PRIVATE (field_decl)
10251                   && flag_emit_class_files
10252                   && nested_field_access_p (current_class, field_decl))
10253                 field_decl = build_nested_field_access (qual_wfl, field_decl);
10254
10255               /* This is the decl found and eventually the next one to
10256                  search from */
10257               decl = field_decl;
10258             }
10259           from_type = 0;
10260           type = QUAL_DECL_TYPE (decl);
10261
10262           /* Sneak preview. If decl is qualified by a `new', report
10263              the error here to be accurate on the peculiar construct */
10264           if (TREE_CHAIN (q)
10265               && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10266               && !JREFERENCE_TYPE_P (type))
10267             {
10268               parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10269                                    lang_printable_name (type, 0));
10270               return 1;
10271             }
10272         }
10273       /* `q' might have changed due to a after package resolution
10274          re-qualification */
10275       if (!q)
10276         break;
10277     }
10278   *found_decl = decl;
10279   return 0;
10280 }
10281
10282 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10283    can't be accessed from REFERENCE (a record type). If MEMBER
10284    features a protected access, we then use WHERE which, if non null,
10285    holds the type of MEMBER's access that is checked against
10286    6.6.2.1. This function should be used when decl is a field or a
10287    method.  */
10288
10289 static int
10290 not_accessible_p (tree reference, tree member, tree where, int from_super)
10291 {
10292   int access_flag = get_access_flags_from_decl (member);
10293   bool is_static = false;
10294  
10295   if (TREE_CODE (member) == FIELD_DECL ||
10296       TREE_CODE (member) == VAR_DECL)
10297     is_static = FIELD_STATIC (member);
10298   else
10299     is_static = METHOD_STATIC (member);
10300
10301   /* Access always granted for members declared public */
10302   if (access_flag & ACC_PUBLIC)
10303     return 0;
10304
10305   /* Check access on protected members */
10306   if (access_flag & ACC_PROTECTED)
10307     {
10308       /* Access granted if it occurs from within the package
10309          containing the class in which the protected member is
10310          declared */
10311       if (class_in_current_package (DECL_CONTEXT (member)))
10312         return 0;
10313
10314       /* If accessed with the form `super.member', then access is granted */
10315       if (from_super)
10316         return 0;
10317
10318       /* If WHERE is active, access was made through a qualifier. For 
10319          non-static members, access is granted if the type of the qualifier 
10320          is or is a sublass of the type the access is made from (6.6.2.1.)  */
10321       if (where && !is_static)
10322         {
10323           while (reference)
10324             {
10325               if (inherits_from_p (where, reference))
10326                 return 0;
10327               if (INNER_CLASS_TYPE_P (reference))
10328                 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10329               else
10330                 break;
10331             }
10332           return 1;
10333         }
10334
10335       /* Otherwise, access is granted if occurring from within the class
10336          where member is declared, or a subclass of it.  */
10337       while (reference)
10338         {
10339           if (inherits_from_p (reference, DECL_CONTEXT (member)))
10340             return 0;
10341           if (INNER_CLASS_TYPE_P (reference))
10342             reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10343           else
10344             break;
10345         }
10346       return 1;
10347     }
10348
10349   /* Check access on private members. Access is granted only if it
10350      occurs from within the class in which it is declared -- that does
10351      it for innerclasses too. */
10352   if (access_flag & ACC_PRIVATE)
10353     {
10354       if (reference == DECL_CONTEXT (member) ||
10355           common_enclosing_context_p (DECL_CONTEXT (member), reference))
10356         return 0;
10357       return 1;
10358     }
10359
10360   /* Default access is permitted only when occurring from within the
10361      package in which the context (MEMBER) is declared.  */
10362   return !class_in_current_package (DECL_CONTEXT (member));
10363 }
10364
10365 /* Test deprecated decl access.  */
10366 static void
10367 check_deprecation (tree wfl, tree decl)
10368 {
10369   const char *file;
10370   tree elt;
10371
10372   if (! warn_deprecated)
10373     return;
10374
10375   /* We want to look at the element type of arrays here, so we strip
10376      all surrounding array types.  */
10377   if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10378     {
10379       elt = TREE_TYPE (decl);
10380       while (TYPE_ARRAY_P (elt))
10381         elt = TYPE_ARRAY_ELEMENT (elt);
10382       /* We'll end up with a pointer type, so we use TREE_TYPE to go
10383          to the record.  */
10384       decl = TYPE_NAME (TREE_TYPE (elt));
10385     }
10386   file = DECL_SOURCE_FILE (decl);
10387
10388   /* Complain if the field is deprecated and the file it was defined
10389      in isn't compiled at the same time the file which contains its
10390      use is */
10391   if (DECL_DEPRECATED (decl)
10392       && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10393     {
10394       const char *the;
10395       switch (TREE_CODE (decl))
10396         {
10397         case FUNCTION_DECL:
10398           the = "method";
10399           break;
10400         case FIELD_DECL:
10401         case VAR_DECL:
10402           the = "field";
10403           break;
10404         case TYPE_DECL:
10405           parse_warning_context (wfl, "The class %qs has been deprecated",
10406                                  IDENTIFIER_POINTER (DECL_NAME (decl)));
10407           return;
10408         default:
10409           abort ();
10410         }
10411       /* Don't issue a message if the context as been deprecated as a
10412          whole. */
10413       if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10414         parse_warning_context
10415           (wfl, "The %s %qs in class %qs has been deprecated",
10416            the, lang_printable_name (decl, 0),
10417            IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10418     }
10419 }
10420
10421 /* Returns 1 if class was declared in the current package, 0 otherwise */
10422
10423 static GTY(()) tree cicp_cache;
10424 static int
10425 class_in_current_package (tree class)
10426 {
10427   int qualified_flag;
10428   tree left;
10429
10430   if (cicp_cache == class)
10431     return 1;
10432
10433   qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10434
10435   /* If the current package is empty and the name of CLASS is
10436      qualified, class isn't in the current package.  If there is a
10437      current package and the name of the CLASS is not qualified, class
10438      isn't in the current package */
10439   if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10440     return 0;
10441
10442   /* If there is not package and the name of CLASS isn't qualified,
10443      they belong to the same unnamed package */
10444   if (!ctxp->package && !qualified_flag)
10445     return 1;
10446
10447   /* Compare the left part of the name of CLASS with the package name */
10448   split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10449   if (ctxp->package == left)
10450     {
10451       cicp_cache = class;
10452       return 1;
10453     }
10454   return 0;
10455 }
10456
10457 /* This function may generate code to access DECL from WHERE. This is
10458    done only if certain conditions meet.  */
10459
10460 static tree
10461 maybe_access_field (tree decl, tree where, tree type)
10462 {
10463   if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10464       && !FIELD_STATIC (decl))
10465     decl = build_field_ref (where ? where : current_this,
10466                             (type ? type : DECL_CONTEXT (decl)),
10467                             DECL_NAME (decl));
10468   return decl;
10469 }
10470
10471 /* Build a method invocation, by patching PATCH. If non NULL
10472    and according to the situation, PRIMARY and WHERE may be
10473    used. IS_STATIC is set to 1 if the invoked function is static. */
10474
10475 static tree
10476 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10477                          int *is_static, tree *ret_decl)
10478 {
10479   tree wfl = TREE_OPERAND (patch, 0);
10480   tree args = TREE_OPERAND (patch, 1);
10481   tree name = EXPR_WFL_NODE (wfl);
10482   tree list;
10483   int is_static_flag = 0;
10484   int is_super_init = 0;
10485   tree this_arg = NULL_TREE;
10486   int is_array_clone_call = 0;
10487
10488   /* Should be overridden if everything goes well. Otherwise, if
10489      something fails, it should keep this value. It stop the
10490      evaluation of a bogus assignment. See java_complete_tree,
10491      MODIFY_EXPR: for the reasons why we sometimes want to keep on
10492      evaluating an assignment */
10493   TREE_TYPE (patch) = error_mark_node;
10494
10495   /* Since lookup functions are messing with line numbers, save the
10496      context now.  */
10497   java_parser_context_save_global ();
10498
10499   /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10500
10501   /* Resolution of qualified name, excluding constructors */
10502   if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10503     {
10504       tree identifier, identifier_wfl, type, resolved;
10505       /* Extract the last IDENTIFIER of the qualified
10506          expression. This is a wfl and we will use it's location
10507          data during error report. */
10508       identifier_wfl = cut_identifier_in_qualified (wfl);
10509       identifier = EXPR_WFL_NODE (identifier_wfl);
10510
10511       /* Given the context, IDENTIFIER is syntactically qualified
10512          as a MethodName. We need to qualify what's before */
10513       qualify_ambiguous_name (wfl);
10514       resolved = resolve_field_access (wfl, NULL, NULL);
10515
10516       if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10517          && FIELD_FINAL (resolved)
10518          && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10519          && !flag_emit_class_files)
10520        resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10521
10522       if (resolved == error_mark_node)
10523         PATCH_METHOD_RETURN_ERROR ();
10524
10525       type = GET_SKIP_TYPE (resolved);
10526       resolve_and_layout (type, NULL_TREE);
10527
10528       if (JPRIMITIVE_TYPE_P (type))
10529         {
10530           parse_error_context
10531             (identifier_wfl,
10532              "Can't invoke a method on primitive type %qs",
10533              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10534           PATCH_METHOD_RETURN_ERROR ();
10535         }
10536
10537       list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10538       args = nreverse (args);
10539
10540       /* We're resolving a call from a type */
10541       if (TREE_CODE (resolved) == TYPE_DECL)
10542         {
10543           if (CLASS_INTERFACE (resolved))
10544             {
10545               parse_error_context
10546                 (identifier_wfl,
10547                 "Can't make static reference to method %qs in interface %qs",
10548                  IDENTIFIER_POINTER (identifier),
10549                  IDENTIFIER_POINTER (name));
10550               PATCH_METHOD_RETURN_ERROR ();
10551             }
10552           if (list && !METHOD_STATIC (list))
10553             {
10554               char *fct_name = xstrdup (lang_printable_name (list, 2));
10555               parse_error_context
10556                 (identifier_wfl,
10557                  "Can't make static reference to method %<%s %s%> in class %qs",
10558                  lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10559                  fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10560               free (fct_name);
10561               PATCH_METHOD_RETURN_ERROR ();
10562             }
10563         }
10564       else
10565         this_arg = primary = resolved;
10566
10567       if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10568         is_array_clone_call = 1;
10569
10570       /* IDENTIFIER_WFL will be used to report any problem further */
10571       wfl = identifier_wfl;
10572     }
10573   /* Resolution of simple names, names generated after a primary: or
10574      constructors */
10575   else
10576     {
10577       tree class_to_search = NULL_TREE;
10578       int lc;                   /* Looking for Constructor */
10579
10580       /* We search constructor in their target class */
10581       if (CALL_CONSTRUCTOR_P (patch))
10582         {
10583           if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10584             class_to_search = EXPR_WFL_NODE (wfl);
10585           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10586                    this_identifier_node)
10587             class_to_search = NULL_TREE;
10588           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10589                    super_identifier_node)
10590             {
10591               is_super_init = 1;
10592               if (CLASSTYPE_SUPER (current_class))
10593                 class_to_search =
10594                   DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10595               else
10596                 {
10597                   parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10598                   PATCH_METHOD_RETURN_ERROR ();
10599                 }
10600             }
10601
10602           /* Class to search is NULL if we're searching the current one */
10603           if (class_to_search)
10604             {
10605               class_to_search = resolve_and_layout (class_to_search, wfl);
10606
10607               if (!class_to_search)
10608                 {
10609                   parse_error_context
10610                     (wfl, "Class %qs not found in type declaration",
10611                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10612                   PATCH_METHOD_RETURN_ERROR ();
10613                 }
10614
10615               /* Can't instantiate an abstract class, but we can
10616                  invoke it's constructor. It's use within the `new'
10617                  context is denied here. */
10618               if (CLASS_ABSTRACT (class_to_search)
10619                   && TREE_CODE (patch) == NEW_CLASS_EXPR)
10620                 {
10621                   parse_error_context
10622                     (wfl, "Class %qs is an abstract class. It can't be instantiated",
10623                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10624                   PATCH_METHOD_RETURN_ERROR ();
10625                 }
10626
10627               class_to_search = TREE_TYPE (class_to_search);
10628             }
10629           else
10630             class_to_search = current_class;
10631           lc = 1;
10632         }
10633       /* This is a regular search in the local class, unless an
10634          alternate class is specified. */
10635       else
10636         {
10637           if (where != NULL_TREE)
10638             class_to_search = where;
10639           else if (QUALIFIED_P (name))
10640             class_to_search = current_class;
10641           else
10642             {
10643               class_to_search = current_class;
10644
10645               for (;;)
10646                 {
10647                   if (has_method (class_to_search, name))
10648                     break;
10649                   if (! INNER_CLASS_TYPE_P (class_to_search))
10650                     {
10651                       parse_error_context (wfl,
10652                                            "No method named %qs in scope",
10653                                            IDENTIFIER_POINTER (name));
10654                       PATCH_METHOD_RETURN_ERROR ();
10655                     }
10656                   class_to_search
10657                     = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10658                 }
10659             }
10660           lc = 0;
10661         }
10662
10663       /* NAME is a simple identifier or comes from a primary. Search
10664          in the class whose declaration contain the method being
10665          invoked. */
10666       resolve_and_layout (class_to_search, NULL_TREE);
10667
10668       list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10669       /* Don't continue if no method were found, as the next statement
10670          can't be executed then. */
10671       if (!list)
10672         PATCH_METHOD_RETURN_ERROR ();
10673
10674       if (TYPE_ARRAY_P (class_to_search)
10675           && DECL_NAME (list) == get_identifier ("clone"))
10676         is_array_clone_call = 1;
10677
10678       /* Check for static reference if non static methods */
10679       if (check_for_static_method_reference (wfl, patch, list,
10680                                              class_to_search, primary))
10681         PATCH_METHOD_RETURN_ERROR ();
10682
10683       /* Check for inner classes creation from illegal contexts */
10684       if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10685                  && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10686           && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10687           && !DECL_INIT_P (current_function_decl))
10688         {
10689           parse_error_context
10690             (wfl, "No enclosing instance for inner class %qs is in scope%s",
10691              lang_printable_name (class_to_search, 0),
10692              (!current_this ? "" :
10693               "; an explicit one must be provided when creating this inner class"));
10694           PATCH_METHOD_RETURN_ERROR ();
10695         }
10696
10697       /* Non static methods are called with the current object extra
10698          argument. If patch a `new TYPE()', the argument is the value
10699          returned by the object allocator. If method is resolved as a
10700          primary, use the primary otherwise use the current THIS. */
10701       args = nreverse (args);
10702       if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10703         {
10704           this_arg = primary ? primary : current_this;
10705
10706           /* If we're using an access method, things are different.
10707              There are two family of cases:
10708
10709              1) We're not generating bytecodes:
10710
10711              - LIST is non static. It's invocation is transformed from
10712                x(a1,...,an) into this$<n>.x(a1,....an).
10713              - LIST is static. It's invocation is transformed from
10714                x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10715
10716              2) We're generating bytecodes:
10717
10718              - LIST is non static. It's invocation is transformed from
10719                x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10720              - LIST is static. It's invocation is transformed from
10721                x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10722
10723              Of course, this$<n> can be arbitrarily complex, ranging from
10724              this$0 (the immediate outer context) to
10725              access$0(access$0(...(this$0))).
10726
10727              maybe_use_access_method returns a nonzero value if the
10728              this_arg has to be moved into the (then generated) stub
10729              argument list. In the meantime, the selected function
10730              might have be replaced by a generated stub. */
10731           if (!primary &&
10732               maybe_use_access_method (is_super_init, &list, &this_arg))
10733             {
10734               args = tree_cons (NULL_TREE, this_arg, args);
10735               this_arg = NULL_TREE; /* So it doesn't get chained twice */
10736             }
10737         }
10738     }
10739
10740   /* Merge point of all resolution schemes. If we have nothing, this
10741      is an error, already signaled */
10742   if (!list)
10743     PATCH_METHOD_RETURN_ERROR ();
10744
10745   /* Check accessibility, position the is_static flag, build and
10746      return the call */
10747   if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10748                         (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10749                          NULL_TREE), from_super)
10750       /* Calls to clone() on array types are permitted as a special-case. */
10751       && !is_array_clone_call)
10752     {
10753       const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10754       const char *const access =
10755         accessibility_string (get_access_flags_from_decl (list));
10756       const char *const klass =
10757         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10758       const char *const refklass =
10759         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10760       const char *const what = (DECL_CONSTRUCTOR_P (list)
10761                                 ? "constructor" : "method");
10762       parse_error_context (wfl,
10763                            "Can't access %s %s %<%s.%s%> from %qs",
10764                            access, what, klass, fct_name, refklass);
10765       PATCH_METHOD_RETURN_ERROR ();
10766     }
10767
10768   /* Deprecation check: check whether the method being invoked or the
10769      instance-being-created's type are deprecated.  */
10770   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10771     check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10772   check_deprecation (wfl, list);
10773
10774   /* If invoking a innerclass constructor, there are hidden parameters
10775      to pass */
10776   if (TREE_CODE (patch) == NEW_CLASS_EXPR
10777       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10778     {
10779       /* And make sure we add the accessed local variables to be saved
10780          in field aliases. */
10781       args = build_alias_initializer_parameter_list
10782         (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10783
10784       /* Secretly pass the current_this/primary as a second argument */
10785       if (primary || current_this)
10786         {
10787           tree extra_arg;
10788           tree this_type = (current_this ?
10789                             TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10790           /* Method's (list) enclosing context */
10791           tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10792           /* If we have a primary, use it. */
10793           if (primary)
10794             extra_arg = primary;
10795           /* The current `this' is an inner class but isn't a direct
10796              enclosing context for the inner class we're trying to
10797              create. Build an access to the proper enclosing context
10798              and use it. */
10799           else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10800                    && this_type != TREE_TYPE (mec))
10801             {
10802
10803               extra_arg = build_access_to_thisn (current_class,
10804                                                  TREE_TYPE (mec), 0);
10805               extra_arg = java_complete_tree (extra_arg);
10806             }
10807           /* Otherwise, just use the current `this' as an enclosing
10808              context. */
10809           else
10810             extra_arg = current_this;
10811           args = tree_cons (NULL_TREE, extra_arg, args);
10812         }
10813       else
10814         args = tree_cons (NULL_TREE, integer_zero_node, args);
10815     }
10816
10817   /* This handles the situation where a constructor invocation needs
10818      to have an enclosing context passed as a second parameter (the
10819      constructor is one of an inner class). */
10820   if ((is_super_init ||
10821        (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10822       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10823     {
10824       tree dest = TYPE_NAME (DECL_CONTEXT (list));
10825       tree extra_arg =
10826         build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10827       extra_arg = java_complete_tree (extra_arg);
10828       args = tree_cons (NULL_TREE, extra_arg, args);
10829     }
10830
10831   is_static_flag = METHOD_STATIC (list);
10832   if (! is_static_flag && this_arg != NULL_TREE)
10833     args = tree_cons (NULL_TREE, this_arg, args);
10834
10835   /* In the context of an explicit constructor invocation, we can't
10836      invoke any method relying on `this'. Exceptions are: we're
10837      invoking a static function, primary exists and is not the current
10838      this, we're creating a new object. */
10839   if (ctxp->explicit_constructor_p
10840       && !is_static_flag
10841       && (!primary || primary == current_this)
10842       && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10843     {
10844       parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10845       PATCH_METHOD_RETURN_ERROR ();
10846     }
10847   java_parser_context_restore_global ();
10848   if (is_static)
10849     *is_static = is_static_flag;
10850   /* Sometimes, we want the decl of the selected method. Such as for
10851      EH checking */
10852   if (ret_decl)
10853     *ret_decl = list;
10854   patch = patch_invoke (patch, list, args);
10855
10856   /* Now is a good time to insert the call to finit$ */
10857   if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10858     {
10859       tree finit_parms, finit_call;
10860
10861       /* Prepare to pass hidden parameters to finit$, if any. */
10862       finit_parms = build_alias_initializer_parameter_list
10863         (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10864
10865       finit_call =
10866         build_method_invocation (build_wfl_node (finit_identifier_node),
10867                                  finit_parms);
10868
10869       /* Generate the code used to initialize fields declared with an
10870          initialization statement and build a compound statement along
10871          with the super constructor invocation. */
10872       CAN_COMPLETE_NORMALLY (patch) = 1;
10873       patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10874                       java_complete_tree (finit_call));
10875     }
10876   return patch;
10877 }
10878
10879 /* Check that we're not trying to do a static reference to a method in
10880    non static method. Return 1 if it's the case, 0 otherwise. */
10881
10882 static int
10883 check_for_static_method_reference (tree wfl, tree node, tree method,
10884                                    tree where, tree primary)
10885 {
10886   if (METHOD_STATIC (current_function_decl)
10887       && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10888     {
10889       char *fct_name = xstrdup (lang_printable_name (method, 0));
10890       parse_error_context
10891         (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10892          lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10893          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10894       free (fct_name);
10895       return 1;
10896     }
10897   return 0;
10898 }
10899
10900 /* Fix the invocation of *MDECL if necessary in the case of a
10901    invocation from an inner class. *THIS_ARG might be modified
10902    appropriately and an alternative access to *MDECL might be
10903    returned.  */
10904
10905 static int
10906 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10907 {
10908   tree ctx;
10909   tree md = *mdecl, ta = *this_arg;
10910   int to_return = 0;
10911   int non_static_context = !METHOD_STATIC (md);
10912
10913   if (is_super_init
10914       || DECL_CONTEXT (md) == current_class
10915       || !PURE_INNER_CLASS_TYPE_P (current_class)
10916       || DECL_FINIT_P (md)
10917       || DECL_INSTINIT_P (md))
10918     return 0;
10919
10920   /* If we're calling a method found in an enclosing class, generate
10921      what it takes to retrieve the right this. Don't do that if we're
10922      invoking a static method. Note that if MD's type is unrelated to
10923      CURRENT_CLASS, then the current this can be used. */
10924
10925   if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10926     {
10927       ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10928       if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10929         {
10930           ta = build_current_thisn (current_class);
10931           ta = build_wfl_node (ta);
10932         }
10933       else
10934         {
10935           tree type = ctx;
10936           while (type)
10937             {
10938               maybe_build_thisn_access_method (type);
10939               if (inherits_from_p (type, DECL_CONTEXT (md)))
10940                 {
10941                   ta = build_access_to_thisn (ctx, type, 0);
10942                   break;
10943                 }
10944               type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10945                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10946             }
10947         }
10948       ta = java_complete_tree (ta);
10949     }
10950
10951   /* We might have to use an access method to get to MD. We can
10952      break the method access rule as far as we're not generating
10953      bytecode */
10954   if (METHOD_PRIVATE (md) && flag_emit_class_files)
10955     {
10956       md = build_outer_method_access_method (md);
10957       to_return = 1;
10958     }
10959
10960   *mdecl = md;
10961   *this_arg = ta;
10962
10963   /* Returning a nonzero value indicates we were doing a non static
10964      method invocation that is now a static invocation. It will have
10965      callee displace `this' to insert it in the regular argument
10966      list. */
10967   return (non_static_context && to_return);
10968 }
10969
10970 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10971    mode.  */
10972
10973 static tree
10974 patch_invoke (tree patch, tree method, tree args)
10975 {
10976   tree dtable, func;
10977   tree original_call, t, ta;
10978   tree check = NULL_TREE;
10979
10980   /* Last step for args: convert build-in types. If we're dealing with
10981      a new TYPE() type call, the first argument to the constructor
10982      isn't found in the incoming argument list, but delivered by
10983      `new' */
10984   t = TYPE_ARG_TYPES (TREE_TYPE (method));
10985   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10986     t = TREE_CHAIN (t);
10987   for (ta = args; t != end_params_node && ta;
10988        t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10989     if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10990         TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10991       TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10992
10993   /* Resolve unresolved returned type issues */
10994   t = TREE_TYPE (TREE_TYPE (method));
10995   if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10996     resolve_and_layout (TREE_TYPE (t), NULL);
10997
10998   if (flag_emit_class_files)
10999     func = method;
11000   else
11001     {
11002       switch (invocation_mode (method, CALL_USING_SUPER (patch)))
11003         {
11004         case INVOKE_VIRTUAL:
11005           dtable = invoke_build_dtable (0, args);
11006           func = build_invokevirtual (dtable, method);
11007           break;
11008
11009         case INVOKE_NONVIRTUAL:
11010           /* If the object for the method call is null, we throw an
11011              exception.  We don't do this if the object is the current
11012              method's `this'.  In other cases we just rely on an
11013              optimization pass to eliminate redundant checks.  */
11014           if (TREE_VALUE (args) != current_this)
11015             {
11016               /* We use a save_expr here to make sure we only evaluate
11017                  the new `self' expression once.  */
11018               tree save_arg = save_expr (TREE_VALUE (args));
11019               TREE_VALUE (args) = save_arg;
11020               check = java_check_reference (save_arg, 1);
11021             }
11022           /* Fall through.  */
11023
11024         case INVOKE_SUPER:
11025         case INVOKE_STATIC:
11026           {
11027             tree signature = build_java_signature (TREE_TYPE (method));
11028             func = build_known_method_ref (method, TREE_TYPE (method),
11029                                            DECL_CONTEXT (method),
11030                                            signature, args);
11031           }
11032           break;
11033
11034         case INVOKE_INTERFACE:
11035           dtable = invoke_build_dtable (1, args);
11036           func = build_invokeinterface (dtable, method);
11037           break;
11038
11039         default:
11040           abort ();
11041         }
11042
11043       /* Ensure self_type is initialized, (invokestatic). FIXME */
11044       func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11045     }
11046
11047   TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11048   TREE_OPERAND (patch, 0) = func;
11049   TREE_OPERAND (patch, 1) = args;
11050   patch = check_for_builtin (method, patch);
11051   original_call = patch;
11052
11053   /* We're processing a `new TYPE ()' form. New is called and its
11054      returned value is the first argument to the constructor. We build
11055      a COMPOUND_EXPR and use saved expression so that the overall NEW
11056      expression value is a pointer to a newly created and initialized
11057      class. */
11058   if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11059     {
11060       tree class = DECL_CONTEXT (method);
11061       tree c1, saved_new, new;
11062       tree alloc_node;
11063
11064       if (flag_emit_class_files)
11065         {
11066           TREE_TYPE (patch) = build_pointer_type (class);
11067           return patch;
11068         }
11069       if (!TYPE_SIZE (class))
11070         safe_layout_class (class);
11071       alloc_node =
11072         (class_has_finalize_method (class) ? alloc_object_node
11073                                            : alloc_no_finalizer_node);
11074       new = build3 (CALL_EXPR, promote_type (class),
11075                     build_address_of (alloc_node),
11076                     build_tree_list (NULL_TREE, build_class_ref (class)),
11077                     NULL_TREE);
11078       saved_new = save_expr (new);
11079       c1 = build_tree_list (NULL_TREE, saved_new);
11080       TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11081       TREE_OPERAND (original_call, 1) = c1;
11082       TREE_SET_CODE (original_call, CALL_EXPR);
11083       patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11084     }
11085
11086   /* If CHECK is set, then we are building a check to see if the object
11087      is NULL.  */
11088   if (check != NULL_TREE)
11089     {
11090       /* We have to call force_evaluation_order now because creating a
11091          COMPOUND_EXPR wraps the arg list in a way that makes it
11092          unrecognizable by force_evaluation_order later.  Yuk.  */
11093       patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check, 
11094                       force_evaluation_order (patch));
11095       TREE_SIDE_EFFECTS (patch) = 1;
11096     }
11097
11098   /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11099      put it as the first expression of a COMPOUND_EXPR. The second
11100      expression being an empty statement to be later patched if
11101      necessary. We remember a TREE_LIST (the PURPOSE is the method,
11102      the VALUE is the compound) in a hashtable and return a
11103      COMPOUND_EXPR built so that the result of the evaluation of the
11104      original PATCH node is returned. */
11105   if (STATIC_CLASS_INIT_OPT_P ()
11106       && current_function_decl && METHOD_STATIC (method))
11107     {
11108       tree list;
11109       tree fndecl = current_function_decl;
11110       /* We have to call force_evaluation_order now because creating a
11111          COMPOUND_EXPR wraps the arg list in a way that makes it
11112          unrecognizable by force_evaluation_order later.  Yuk.  */
11113       tree save = force_evaluation_order (patch);
11114       tree type = TREE_TYPE (patch);
11115
11116       patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11117       list = tree_cons (method, patch,
11118                         DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11119
11120       DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11121
11122       patch = build2 (COMPOUND_EXPR, type, patch, save);
11123     }
11124
11125   return patch;
11126 }
11127
11128 static int
11129 invocation_mode (tree method, int super)
11130 {
11131   int access = get_access_flags_from_decl (method);
11132
11133   if (super)
11134     return INVOKE_SUPER;
11135
11136   if (access & ACC_STATIC)
11137     return INVOKE_STATIC;
11138
11139   /* We have to look for a constructor before we handle nonvirtual
11140      calls; otherwise the constructor will look nonvirtual.  */
11141   if (DECL_CONSTRUCTOR_P (method))
11142     return INVOKE_STATIC;
11143
11144   if (access & ACC_PRIVATE)
11145     return INVOKE_NONVIRTUAL;
11146
11147   /* Binary compatibility: just because it's final today, that doesn't
11148      mean it'll be final tomorrow.  */
11149   if (! flag_indirect_dispatch  
11150       || DECL_CONTEXT (method) == object_type_node)
11151     {
11152       if (access & ACC_FINAL)
11153         return INVOKE_NONVIRTUAL;
11154
11155       if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11156         return INVOKE_NONVIRTUAL;
11157     }
11158
11159   if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11160     return INVOKE_INTERFACE;
11161
11162   return INVOKE_VIRTUAL;
11163 }
11164
11165 /* Retrieve a refined list of matching methods. It covers the step
11166    15.11.2 (Compile-Time Step 2) */
11167
11168 static tree
11169 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11170 {
11171   tree atl = end_params_node;           /* Arg Type List */
11172   tree method, signature, list, node;
11173   const char *candidates;               /* Used for error report */
11174   char *dup;
11175
11176   /* Fix the arguments */
11177   for (node = arg_list; node; node = TREE_CHAIN (node))
11178     {
11179       tree current_arg = TREE_TYPE (TREE_VALUE (node));
11180       /* Non primitive type may have to be resolved */
11181       if (!JPRIMITIVE_TYPE_P (current_arg))
11182         resolve_and_layout (current_arg, NULL_TREE);
11183       /* And promoted */
11184       if (TREE_CODE (current_arg) == RECORD_TYPE)
11185         current_arg = promote_type (current_arg);
11186       atl = tree_cons (NULL_TREE, current_arg, atl);
11187     }
11188
11189   /* Presto. If we're dealing with an anonymous class and a
11190      constructor call, generate the right constructor now, since we
11191      know the arguments' types. */
11192
11193   if (lc && ANONYMOUS_CLASS_P (class))
11194     {
11195       tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11196       /* The anonymous class may have already been laid out, so make sure
11197          the new constructor is laid out here.  */
11198       layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11199     }
11200
11201   /* Find all candidates and then refine the list, searching for the
11202      most specific method. */
11203   list = find_applicable_accessible_methods_list (lc, class, name, atl);
11204   list = find_most_specific_methods_list (list);
11205   if (list && !TREE_CHAIN (list))
11206     return TREE_VALUE (list);
11207
11208   /* Issue an error. List candidates if any. Candidates are listed
11209      only if accessible (non accessible methods may end-up here for
11210      the sake of a better error report). */
11211   candidates = NULL;
11212   if (list)
11213     {
11214       tree current;
11215       obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11216       for (current = list; current; current = TREE_CHAIN (current))
11217         {
11218           tree cm = TREE_VALUE (current);
11219           char string [4096];
11220           if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11221             continue;
11222           sprintf
11223             (string, "  '%s' in '%s'%s",
11224              get_printable_method_name (cm),
11225              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11226              (TREE_CHAIN (current) ? "\n" : ""));
11227           obstack_grow (&temporary_obstack, string, strlen (string));
11228         }
11229       obstack_1grow (&temporary_obstack, '\0');
11230       candidates = obstack_finish (&temporary_obstack);
11231     }
11232   /* Issue the error message */
11233   method = make_node (FUNCTION_TYPE);
11234   TYPE_ARG_TYPES (method) = atl;
11235   signature = build_java_argument_signature (method);
11236   dup = xstrdup (lang_printable_name (class, 0));
11237   parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11238                        (lc ? "constructor" : "method"),
11239                        (lc ? dup : IDENTIFIER_POINTER (name)),
11240                        IDENTIFIER_POINTER (signature), dup,
11241                        (candidates ? candidates : ""));
11242   free (dup);
11243   return NULL_TREE;
11244 }
11245
11246 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11247    when we're looking for a constructor. */
11248
11249 static tree
11250 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11251                                          tree arglist)
11252 {
11253   static htab_t searched_classes;
11254   static int search_not_done = 0;
11255   tree list = NULL_TREE, all_list = NULL_TREE;
11256   tree base_binfo;
11257   int i;
11258
11259   /* Check the hash table to determine if this class has been searched
11260      already. */
11261   if (searched_classes)
11262     {
11263       if (htab_find (searched_classes, class) != NULL)
11264         return NULL;
11265     }
11266   else
11267     {
11268       searched_classes = htab_create (10, htab_hash_pointer,
11269                                       htab_eq_pointer, NULL);
11270     }
11271
11272   search_not_done++;
11273   *htab_find_slot (searched_classes, class, INSERT) = class;
11274
11275   if (!CLASS_LOADED_P (class))
11276     {
11277       load_class (class, 1);
11278       safe_layout_class (class);
11279     }
11280
11281   /* Search interfaces */
11282   if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11283       && CLASS_INTERFACE (TYPE_NAME (class)))
11284     {
11285       search_applicable_methods_list (lc, TYPE_METHODS (class),
11286                                       name, arglist, &list, &all_list);
11287       for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11288         {
11289           tree t = BINFO_TYPE (base_binfo);
11290           tree rlist;
11291           
11292           rlist = find_applicable_accessible_methods_list (lc,  t, name,
11293                                                            arglist);
11294           list = chainon (rlist, list);
11295         }
11296     }
11297   /* Search classes */
11298   else
11299     {
11300       search_applicable_methods_list (lc, TYPE_METHODS (class),
11301                                       name, arglist, &list, &all_list);
11302
11303       /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11304          that we only search in class. Note that we should have found
11305          something at this point. */
11306       if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11307         {
11308           lc = 1;
11309           if (!list)
11310             abort ();
11311         }
11312
11313       /* We must search all interfaces of this class */
11314       if (!lc)
11315         {
11316           for (i = 1;
11317                BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11318             {
11319               tree t = BINFO_TYPE (base_binfo);
11320               if (t != object_type_node)
11321                 {
11322                   tree rlist
11323                     = find_applicable_accessible_methods_list (lc, t,
11324                                                                name, arglist);
11325                   list = chainon (rlist, list);
11326                 }
11327             }
11328         }
11329
11330       /* Search superclass */
11331       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11332         {
11333           tree rlist;
11334           class = CLASSTYPE_SUPER (class);
11335           rlist = find_applicable_accessible_methods_list (lc, class,
11336                                                            name, arglist);
11337           list = chainon (rlist, list);
11338         }
11339     }
11340
11341   search_not_done--;
11342
11343   /* We're done. Reset the searched classes list and finally search
11344      java.lang.Object if it wasn't searched already. */
11345   if (!search_not_done)
11346     {
11347       if (!lc
11348           && TYPE_METHODS (object_type_node)
11349           && htab_find (searched_classes, object_type_node) == NULL)
11350         {
11351           search_applicable_methods_list (lc,
11352                                           TYPE_METHODS (object_type_node),
11353                                           name, arglist, &list, &all_list);
11354         }
11355       htab_delete (searched_classes);
11356       searched_classes = NULL;
11357     }
11358
11359   /* Either return the list obtained or all selected (but
11360      inaccessible) methods for better error report. */
11361   return (!list ? all_list : list);
11362 }
11363
11364 /* Effectively search for the appropriate method in method */
11365
11366 static void
11367 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11368                                 tree *list, tree *all_list)
11369 {
11370   for (; method; method = TREE_CHAIN (method))
11371     {
11372       /* When dealing with constructor, stop here, otherwise search
11373          other classes */
11374       if (lc && !DECL_CONSTRUCTOR_P (method))
11375         continue;
11376       else if (!lc && (DECL_CONSTRUCTOR_P (method)
11377                        || (DECL_NAME (method) != name)))
11378         continue;
11379
11380       if (argument_types_convertible (method, arglist))
11381         {
11382           /* Retain accessible methods only */
11383           if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11384                                  method, NULL_TREE, 0))
11385             *list = tree_cons (NULL_TREE, method, *list);
11386           else
11387             /* Also retain all selected method here */
11388             *all_list = tree_cons (NULL_TREE, method, *list);
11389         }
11390     }
11391 }
11392
11393 /* 15.11.2.2 Choose the Most Specific Method */
11394
11395 static tree
11396 find_most_specific_methods_list (tree list)
11397 {
11398   int max = 0;
11399   int abstract, candidates;
11400   tree current, new_list = NULL_TREE;
11401   for (current = list; current; current = TREE_CHAIN (current))
11402     {
11403       tree method;
11404       DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11405
11406       for (method = list; method; method = TREE_CHAIN (method))
11407         {
11408           tree method_v, current_v;
11409           /* Don't test a method against itself */
11410           if (method == current)
11411             continue;
11412
11413           method_v = TREE_VALUE (method);
11414           current_v = TREE_VALUE (current);
11415
11416           /* Compare arguments and location where methods where declared */
11417           if (argument_types_convertible (method_v, current_v))
11418             {
11419               if (valid_method_invocation_conversion_p
11420                   (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v)))
11421                 {
11422                   int v = (DECL_SPECIFIC_COUNT (current_v) += 1);
11423                   max = (v > max ? v : max);
11424                 }
11425             }
11426         }
11427     }
11428
11429   /* Review the list and select the maximally specific methods */
11430   for (current = list, abstract = -1, candidates = -1;
11431        current; current = TREE_CHAIN (current))
11432     if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11433       {
11434         new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11435         abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11436         candidates++;
11437       }
11438
11439   /* If we have several and they're all abstract, just pick the
11440      closest one. */
11441   if (candidates > 0 && candidates == abstract)
11442     {
11443       /* FIXME: merge the throws clauses.  There is no convenient way
11444          to do this in gcj right now, since ideally we'd like to
11445          introduce a new METHOD_DECL here, but that is really not
11446          possible.  */
11447       new_list = nreverse (new_list);
11448       TREE_CHAIN (new_list) = NULL_TREE;
11449       return new_list;
11450     }
11451
11452   /* We have several (we couldn't find a most specific), all but one
11453      are abstract, we pick the only non abstract one. */
11454   if (candidates > 0 && (candidates == abstract+1))
11455     {
11456       for (current = new_list; current; current = TREE_CHAIN (current))
11457         if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11458           {
11459             TREE_CHAIN (current) = NULL_TREE;
11460             new_list = current;
11461           }
11462     }
11463
11464   /* If we can't find one, lower expectations and try to gather multiple
11465      maximally specific methods */
11466   while (!new_list && max)
11467     {
11468       while (--max > 0)
11469         {
11470           if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11471             new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11472         }
11473     }
11474
11475   return new_list;
11476 }
11477
11478 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11479    converted by method invocation conversion (5.3) to the type of the
11480    corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11481    to change less often than M1. */
11482
11483 static GTY(()) tree m2_arg_value;
11484 static GTY(()) tree m2_arg_cache;
11485
11486 static int
11487 argument_types_convertible (tree m1, tree m2_or_arglist)
11488 {
11489   tree m1_arg, m2_arg;
11490
11491   SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11492
11493   if (m2_arg_value == m2_or_arglist)
11494     m2_arg = m2_arg_cache;
11495   else
11496     {
11497       /* M2_OR_ARGLIST can be a function DECL or a raw list of
11498          argument types */
11499       if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11500         {
11501           m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11502           if (!METHOD_STATIC (m2_or_arglist))
11503             m2_arg = TREE_CHAIN (m2_arg);
11504         }
11505       else
11506         m2_arg = m2_or_arglist;
11507
11508       m2_arg_value = m2_or_arglist;
11509       m2_arg_cache = m2_arg;
11510     }
11511
11512   while (m1_arg != end_params_node && m2_arg != end_params_node)
11513     {
11514       resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11515       if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11516                                                  TREE_VALUE (m2_arg)))
11517         break;
11518       m1_arg = TREE_CHAIN (m1_arg);
11519       m2_arg = TREE_CHAIN (m2_arg);
11520     }
11521   return m1_arg == end_params_node && m2_arg == end_params_node;
11522 }
11523
11524 /* Qualification routines */
11525
11526 /* Given a name x.y.z, look up x locally.  If it's found, save the
11527    decl.  If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11528    so that we later try and load the appropriate classes.  */
11529 static void
11530 qualify_ambiguous_name (tree id)
11531 {
11532   tree name, decl;
11533
11534   /* We inspect the first item of the qualification list.  As a sanity
11535      check, make sure that it is an identfier node.  */
11536   tree qual = EXPR_WFL_QUALIFICATION (id);
11537   tree qual_wfl = QUAL_WFL (qual);
11538
11539   if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11540     return;
11541
11542   name = EXPR_WFL_NODE (qual_wfl);
11543
11544   /* If we don't have an identifier, or we have a 'this' or 'super',
11545      then field access processing is all we need : there is nothing
11546      for us to do.  */
11547   if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11548       name == this_identifier_node ||
11549       name == super_identifier_node)
11550     return;
11551
11552   /* If name appears within the scope of a local variable declaration
11553      or parameter declaration, or is a field within an enclosing
11554      class, then it is an expression name.  Save the decl and let
11555      resolve_field_access do it's work.  */
11556   if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11557       (decl = lookup_field_wrapper (current_class, name)))
11558     {
11559       QUAL_RESOLUTION (qual) = decl;
11560       return;
11561     }
11562
11563   /* If name is a known class name (either declared or imported), mark
11564      us as a type name.  */
11565   if ((decl = resolve_and_layout (name, NULL_TREE)))
11566     {
11567       RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11568       QUAL_RESOLUTION (qual) = decl;
11569     }
11570
11571   /* Check here that NAME isn't declared by more than one
11572      type-import-on-demand declaration of the compilation unit
11573      containing NAME. FIXME */
11574
11575   /* We couldn't find a declaration for the name.  Assume for now that
11576      we have a qualified class name that needs to be loaded from an
11577      external class file.  */
11578   else
11579     RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11580
11581   /* Propagate the qualification across other components of the
11582      qualified name */
11583   for (qual = TREE_CHAIN (qual); qual;
11584        qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11585     {
11586       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11587         RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11588     }
11589
11590   /* Store the global qualification for the ambiguous part of ID back
11591      into ID fields */
11592   if (RESOLVE_TYPE_NAME_P (qual_wfl))
11593     RESOLVE_TYPE_NAME_P (id) = 1;
11594   else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11595     RESOLVE_PACKAGE_NAME_P (id) = 1;
11596 }
11597
11598 /* Patch tree nodes in a function body. When a BLOCK is found, push
11599    local variable decls if present.
11600    Same as java_complete_lhs, but does resolve static finals to values. */
11601
11602 static tree
11603 java_complete_tree (tree node)
11604 {
11605   node = java_complete_lhs (node);
11606   if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11607       && DECL_INITIAL (node) != NULL_TREE)
11608     {
11609       tree value = fold_constant_for_init (node, node);
11610       if (value != NULL_TREE)
11611         return value;
11612     }
11613   return node;
11614 }
11615
11616 static tree
11617 java_stabilize_reference (tree node)
11618 {
11619   if (TREE_CODE (node) == COMPOUND_EXPR)
11620     {
11621       tree op0 = TREE_OPERAND (node, 0);
11622       tree op1 = TREE_OPERAND (node, 1);
11623       TREE_OPERAND (node, 0) = save_expr (op0);
11624       TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11625       return node;
11626     }
11627   return stabilize_reference (node);
11628 }
11629
11630 /* Patch tree nodes in a function body. When a BLOCK is found, push
11631    local variable decls if present.
11632    Same as java_complete_tree, but does not resolve static finals to values. */
11633
11634 static tree
11635 java_complete_lhs (tree node)
11636 {
11637   tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11638   int flag;
11639
11640   /* CONVERT_EXPR always has its type set, even though it needs to be
11641      worked out. */
11642   if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11643     return node;
11644
11645   /* The switch block implements cases processing container nodes
11646      first.  Contained nodes are always written back. Leaves come
11647      next and return a value. */
11648   switch (TREE_CODE (node))
11649     {
11650     case BLOCK:
11651
11652       /* 1- Block section.
11653          Set the local values on decl names so we can identify them
11654          faster when they're referenced. At that stage, identifiers
11655          are legal so we don't check for declaration errors. */
11656       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11657         {
11658           DECL_CONTEXT (cn) = current_function_decl;
11659           IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11660         }
11661       if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11662           CAN_COMPLETE_NORMALLY (node) = 1;
11663       else
11664         {
11665           tree stmt = BLOCK_EXPR_BODY (node);
11666           tree *ptr;
11667           int error_seen = 0;
11668           if (TREE_CODE (stmt) == COMPOUND_EXPR)
11669             {
11670               /* Re-order from (((A; B); C); ...; Z) to
11671                  (A; (B; (C ; (...; Z)))).
11672                  This makes it easier to scan the statements left-to-right
11673                  without using recursion (which might overflow the stack
11674                  if the block has many statements. */
11675               for (;;)
11676                 {
11677                   tree left = TREE_OPERAND (stmt, 0);
11678                   if (TREE_CODE (left) != COMPOUND_EXPR)
11679                     break;
11680                   TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11681                   TREE_OPERAND (left, 1) = stmt;
11682                   stmt = left;
11683                 }
11684               BLOCK_EXPR_BODY (node) = stmt;
11685             }
11686
11687           /* Now do the actual complete, without deep recursion for
11688              long blocks. */
11689           ptr = &BLOCK_EXPR_BODY (node);
11690           while (TREE_CODE (*ptr) == COMPOUND_EXPR
11691                  && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11692             {
11693               tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11694               tree *next = &TREE_OPERAND (*ptr, 1);
11695               TREE_OPERAND (*ptr, 0) = cur;
11696               if (IS_EMPTY_STMT (cur))
11697                 {
11698                   /* Optimization;  makes it easier to detect empty bodies.
11699                      Most useful for <clinit> with all-constant initializer. */
11700                   *ptr = *next;
11701                   continue;
11702                 }
11703               if (TREE_CODE (cur) == ERROR_MARK)
11704                 error_seen++;
11705               else if (! CAN_COMPLETE_NORMALLY (cur))
11706                 {
11707                   wfl_op2 = *next;
11708                   for (;;)
11709                     {
11710                       if (TREE_CODE (wfl_op2) == BLOCK)
11711                         wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11712                       else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11713                         wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11714                       else
11715                         break;
11716                     }
11717                   if (TREE_CODE (wfl_op2) != CASE_EXPR
11718                       && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11719                     unreachable_stmt_error (*ptr);
11720                 }
11721               if (TREE_TYPE (*ptr) == NULL_TREE)
11722                 TREE_TYPE (*ptr) = void_type_node;
11723               ptr = next;
11724             }
11725           *ptr = java_complete_tree (*ptr);
11726
11727           if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11728             return error_mark_node;
11729           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11730         }
11731       /* Turn local bindings to null */
11732       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11733         IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11734
11735       TREE_TYPE (node) = void_type_node;
11736       break;
11737
11738       /* 2- They are expressions but ultimately deal with statements */
11739
11740     case THROW_EXPR:
11741       wfl_op1 = TREE_OPERAND (node, 0);
11742       COMPLETE_CHECK_OP_0 (node);
11743       /* 14.19 A throw statement cannot complete normally. */
11744       CAN_COMPLETE_NORMALLY (node) = 0;
11745       return patch_throw_statement (node, wfl_op1);
11746
11747     case SYNCHRONIZED_EXPR:
11748       wfl_op1 = TREE_OPERAND (node, 0);
11749       return patch_synchronized_statement (node, wfl_op1);
11750
11751     case TRY_EXPR:
11752       return patch_try_statement (node);
11753
11754     case TRY_FINALLY_EXPR:
11755       COMPLETE_CHECK_OP_0 (node);
11756       COMPLETE_CHECK_OP_1 (node);
11757       if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11758         /* Reduce try/finally nodes with an empty try block.  */
11759         return TREE_OPERAND (node, 1);
11760       if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11761         /* Likewise for an empty finally block.  */
11762         return TREE_OPERAND (node, 0);
11763       CAN_COMPLETE_NORMALLY (node)
11764         = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11765            && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11766       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11767       return node;
11768
11769     case LABELED_BLOCK_EXPR:
11770       PUSH_LABELED_BLOCK (node);
11771       if (LABELED_BLOCK_BODY (node))
11772         COMPLETE_CHECK_OP_1 (node);
11773       TREE_TYPE (node) = void_type_node;
11774       POP_LABELED_BLOCK ();
11775
11776       if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11777         {
11778           LABELED_BLOCK_BODY (node) = NULL_TREE;
11779           CAN_COMPLETE_NORMALLY (node) = 1;
11780         }
11781       else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11782         CAN_COMPLETE_NORMALLY (node) = 1;
11783       return node;
11784
11785     case EXIT_BLOCK_EXPR:
11786       return patch_bc_statement (node);
11787
11788     case CASE_EXPR:
11789       cn = java_complete_tree (TREE_OPERAND (node, 0));
11790       if (cn == error_mark_node)
11791         return cn;
11792
11793       /* First, the case expression must be constant. Values of final
11794          fields are accepted. */
11795       nn = fold_constant_for_init (cn, NULL_TREE);
11796       if (nn != NULL_TREE)
11797         cn = nn;
11798
11799       cn = fold (cn);
11800       if ((TREE_CODE (cn) == COMPOUND_EXPR
11801            || TREE_CODE (cn) == COMPONENT_REF)
11802           && JDECL_P (TREE_OPERAND (cn, 1))
11803           && FIELD_FINAL (TREE_OPERAND (cn, 1))
11804           && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11805         {
11806           cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11807                                        TREE_OPERAND (cn, 1));
11808         }
11809       /* Accept final locals too. */
11810       else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn) 
11811                && DECL_INITIAL (cn))
11812         cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11813
11814       if (!TREE_CONSTANT (cn))
11815         {
11816           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11817           parse_error_context (node, "Constant expression required");
11818           return error_mark_node;
11819         }
11820
11821       nn = ctxp->current_loop;
11822
11823       /* It must be assignable to the type of the switch expression. */
11824       if (!try_builtin_assignconv (NULL_TREE,
11825                                    TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11826         {
11827           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11828           parse_error_context
11829             (wfl_operator,
11830              "Incompatible type for case. Can't convert %qs to %<int%>",
11831              lang_printable_name (TREE_TYPE (cn), 0));
11832           return error_mark_node;
11833         }
11834
11835       cn = fold (convert (int_type_node, cn));
11836       TREE_CONSTANT_OVERFLOW (cn) = 0;
11837       CAN_COMPLETE_NORMALLY (cn) = 1;
11838
11839       /* Save the label on a list so that we can later check for
11840          duplicates.  */
11841       case_label_list = tree_cons (node, cn, case_label_list);
11842
11843       /* Multiple instance of a case label bearing the same value is
11844          checked later. The case expression is all right so far. */
11845       if (TREE_CODE (cn) == VAR_DECL)
11846         cn = DECL_INITIAL (cn);
11847       TREE_OPERAND (node, 0) = cn;
11848       TREE_TYPE (node) = void_type_node;
11849       CAN_COMPLETE_NORMALLY (node) = 1;
11850       TREE_SIDE_EFFECTS (node) = 1;
11851       break;
11852
11853     case DEFAULT_EXPR:
11854       nn = ctxp->current_loop;
11855       /* Only one default label is allowed per switch statement */
11856       if (SWITCH_HAS_DEFAULT (nn))
11857         {
11858 #ifdef USE_MAPPED_LOCATION
11859           SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11860 #else
11861           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11862 #endif
11863           parse_error_context (wfl_operator,
11864                                "Duplicate case label: %<default%>");
11865           return error_mark_node;
11866         }
11867       else
11868         SWITCH_HAS_DEFAULT (nn) = 1;
11869       TREE_TYPE (node) = void_type_node;
11870       TREE_SIDE_EFFECTS (node) = 1;
11871       CAN_COMPLETE_NORMALLY (node) = 1;
11872       break;
11873
11874     case SWITCH_EXPR:
11875     case LOOP_EXPR:
11876       PUSH_LOOP (node);
11877       /* Check whether the loop was enclosed in a labeled
11878          statement. If not, create one, insert the loop in it and
11879          return the node */
11880       nn = patch_loop_statement (node);
11881
11882       /* Anyways, walk the body of the loop */
11883       if (TREE_CODE (node) == LOOP_EXPR)
11884         TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11885       /* Switch statement: walk the switch expression and the cases */
11886       else
11887         node = patch_switch_statement (node);
11888
11889       if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11890         nn = error_mark_node;
11891       else
11892         {
11893           TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11894           /* If we returned something different, that's because we
11895              inserted a label. Pop the label too. */
11896           if (nn != node)
11897             {
11898               if (CAN_COMPLETE_NORMALLY (node))
11899                 CAN_COMPLETE_NORMALLY (nn) = 1;
11900               POP_LABELED_BLOCK ();
11901             }
11902         }
11903       POP_LOOP ();
11904       return nn;
11905
11906     case EXIT_EXPR:
11907       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11908       return patch_exit_expr (node);
11909
11910     case COND_EXPR:
11911       /* Condition */
11912       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11913       if (TREE_OPERAND (node, 0) == error_mark_node)
11914         return error_mark_node;
11915       /* then-else branches */
11916       TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11917       if (TREE_OPERAND (node, 1) == error_mark_node)
11918         return error_mark_node;
11919       {
11920         /* This is a special case due to build_assertion().  When
11921            assertions are disabled we build a COND_EXPR in which
11922            Operand 1 is the body of the assertion.  If that happens to
11923            be a string concatenation we'll need to patch it here.  */
11924         tree patched = patch_string (TREE_OPERAND (node, 1));
11925         if (patched)
11926           TREE_OPERAND (node, 1) = patched;
11927       }
11928      TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11929       if (TREE_OPERAND (node, 2) == error_mark_node)
11930         return error_mark_node;
11931       return patch_if_else_statement (node);
11932       break;
11933
11934     case CONDITIONAL_EXPR:
11935       /* Condition */
11936       wfl_op1 = TREE_OPERAND (node, 0);
11937       COMPLETE_CHECK_OP_0 (node);
11938       wfl_op2 = TREE_OPERAND (node, 1);
11939       COMPLETE_CHECK_OP_1 (node);
11940       wfl_op3 = TREE_OPERAND (node, 2);
11941       COMPLETE_CHECK_OP_2 (node);
11942       return patch_conditional_expr (node, wfl_op1, wfl_op2);
11943
11944       /* 3- Expression section */
11945     case COMPOUND_EXPR:
11946       wfl_op2 = TREE_OPERAND (node, 1);
11947       TREE_OPERAND (node, 0) = nn =
11948         java_complete_tree (TREE_OPERAND (node, 0));
11949       if (IS_EMPTY_STMT (wfl_op2))
11950         CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11951       else
11952         {
11953           if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11954             {
11955               /* An unreachable condition in a do-while statement
11956                  is *not* (technically) an unreachable statement. */
11957               nn = wfl_op2;
11958               if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11959                 nn = EXPR_WFL_NODE (nn);
11960               /* NN can be NULL_TREE exactly when UPDATE is, in
11961                  finish_for_loop.  */
11962               if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11963                 {
11964                   SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11965                   if (SUPPRESS_UNREACHABLE_ERROR (nn))
11966                     {
11967                       /* Perhaps this warning should have an
11968                          associated flag.  The code being compiled is
11969                          pedantically correct, but useless.  */
11970                       parse_warning_context (wfl_operator,
11971                                              "Unreachable statement");
11972                     }
11973                   else
11974                     parse_error_context (wfl_operator,
11975                                          "Unreachable statement");
11976                 }
11977             }
11978           TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11979           if (TREE_OPERAND (node, 1) == error_mark_node)
11980             return error_mark_node;
11981           /* Even though we might allow the case where the first
11982              operand doesn't return normally, we still should compute
11983              CAN_COMPLETE_NORMALLY correctly.  */
11984           CAN_COMPLETE_NORMALLY (node)
11985             = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11986                && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11987         }
11988       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11989       break;
11990
11991     case RETURN_EXPR:
11992       /* CAN_COMPLETE_NORMALLY (node) = 0; */
11993       return patch_return (node);
11994
11995     case EXPR_WITH_FILE_LOCATION:
11996       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11997           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11998         {
11999           node = resolve_expression_name (node, NULL);
12000           if (node == error_mark_node)
12001             return node;
12002           CAN_COMPLETE_NORMALLY (node) = 1;
12003         }
12004       else
12005         {
12006           tree body;
12007           location_t save_location = input_location;
12008 #ifdef USE_MAPPED_LOCATION
12009           input_location = EXPR_LOCATION (node);
12010           if (input_location == UNKNOWN_LOCATION)
12011             input_location = save_location;
12012 #else
12013           input_line = EXPR_WFL_LINENO (node);
12014 #endif
12015           body = java_complete_tree (EXPR_WFL_NODE (node));
12016           input_location = save_location;
12017           EXPR_WFL_NODE (node) = body;
12018           TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
12019           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
12020           if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
12021             {
12022               /* Makes it easier to constant fold, detect empty bodies. */
12023               return body;
12024             }
12025           if (body == error_mark_node)
12026             {
12027               /* Its important for the evaluation of assignment that
12028                  this mark on the TREE_TYPE is propagated. */
12029               TREE_TYPE (node) = error_mark_node;
12030               return error_mark_node;
12031             }
12032           else
12033             TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12034
12035         }
12036       break;
12037
12038     case NEW_ARRAY_EXPR:
12039       /* Patch all the dimensions */
12040       flag = 0;
12041       for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12042         {
12043           int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12044           tree dim = convert (int_type_node,
12045                               java_complete_tree (TREE_VALUE (cn)));
12046           if (dim == error_mark_node)
12047             {
12048               flag = 1;
12049               continue;
12050             }
12051           else
12052             {
12053               TREE_VALUE (cn) = dim;
12054               /* Setup the location of the current dimension, for
12055                  later error report. */
12056 #ifdef USE_MAPPED_LOCATION
12057               TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12058 #else
12059               TREE_PURPOSE (cn) =
12060                 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12061               EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12062 #endif
12063             }
12064         }
12065       /* They complete the array creation expression, if no errors
12066          were found. */
12067       CAN_COMPLETE_NORMALLY (node) = 1;
12068       return (flag ? error_mark_node
12069               : force_evaluation_order (patch_newarray (node)));
12070
12071     case NEW_ANONYMOUS_ARRAY_EXPR:
12072       /* Create the array type if necessary. */
12073       if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12074         {
12075           tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12076           if (!(type = resolve_type_during_patch (type)))
12077             return error_mark_node;
12078           type = build_array_from_name (type, NULL_TREE,
12079                                         ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12080           ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12081         }
12082       node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12083                                    ANONYMOUS_ARRAY_INITIALIZER (node));
12084       if (node == error_mark_node)
12085         return error_mark_node;
12086       CAN_COMPLETE_NORMALLY (node) = 1;
12087       return node;
12088
12089     case NEW_CLASS_EXPR:
12090     case CALL_EXPR:
12091       /* Complete function's argument(s) first */
12092       if (complete_function_arguments (node))
12093         return error_mark_node;
12094       else
12095         {
12096           tree decl, wfl = TREE_OPERAND (node, 0);
12097           int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12098           int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12099                            super_identifier_node);
12100           tree arguments;
12101 #ifdef USE_MAPPED_LOCATION
12102           source_location location = EXPR_LOCATION (node);
12103 #else
12104           int location = EXPR_WFL_LINECOL (node);
12105 #endif
12106
12107           node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12108                                           from_super, 0, &decl);
12109           if (node == error_mark_node)
12110             return error_mark_node;
12111
12112           if (TREE_CODE (node) == CALL_EXPR
12113               && TREE_OPERAND (node, 1) != NULL_TREE)
12114             arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12115           else
12116             arguments = NULL_TREE;
12117           check_thrown_exceptions (location, decl, arguments);
12118           /* If we call this(...), register signature and positions */
12119           if (in_this)
12120             DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12121               tree_cons (wfl, decl,
12122                          DECL_CONSTRUCTOR_CALLS (current_function_decl));
12123           CAN_COMPLETE_NORMALLY (node) = 1;
12124           return force_evaluation_order (node);
12125         }
12126
12127     case MODIFY_EXPR:
12128       /* Save potential wfls */
12129       wfl_op1 = TREE_OPERAND (node, 0);
12130       TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12131
12132       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12133           && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12134           && DECL_INITIAL (nn) != NULL_TREE)
12135         {
12136           tree value;
12137
12138           value = fold_constant_for_init (nn, nn);
12139
12140           /* When we have a primitype type, or a string and we're not
12141              emitting a class file, we actually don't want to generate
12142              anything for the assignment. */
12143           if (value != NULL_TREE && 
12144               (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) || 
12145                (TREE_TYPE (value) == string_ptr_type_node &&
12146                 ! flag_emit_class_files)))
12147             {
12148               /* Prepare node for patch_assignment */
12149               TREE_OPERAND (node, 1) = value;
12150               /* Call patch assignment to verify the assignment */
12151               if (patch_assignment (node, wfl_op1) == error_mark_node)
12152                 return error_mark_node;
12153               /* Set DECL_INITIAL properly (a conversion might have
12154                  been decided by patch_assignment) and return the
12155                  empty statement. */
12156               else
12157                 {
12158                   tree patched = patch_string (TREE_OPERAND (node, 1));
12159                   if (patched)
12160                     DECL_INITIAL (nn) = patched;
12161                   else
12162                     DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12163                   DECL_FIELD_FINAL_IUD (nn) = 1;
12164                   return build_java_empty_stmt ();
12165                 }
12166             }
12167           if (! flag_emit_class_files)
12168             DECL_INITIAL (nn) = NULL_TREE;
12169         }
12170       wfl_op2 = TREE_OPERAND (node, 1);
12171
12172       if (TREE_OPERAND (node, 0) == error_mark_node)
12173         return error_mark_node;
12174
12175       flag = COMPOUND_ASSIGN_P (wfl_op2);
12176       if (flag)
12177         {
12178           /* This might break when accessing outer field from inner
12179              class. TESTME, FIXME */
12180           tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12181
12182           /* Hand stabilize the lhs on both places */
12183           TREE_OPERAND (node, 0) = lvalue;
12184           TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12185             (flag_emit_class_files ? lvalue : save_expr (lvalue));
12186
12187           /* 15.25.2.a: Left hand is not an array access. FIXME */
12188           /* Now complete the RHS. We write it back later on. */
12189           nn = java_complete_tree (TREE_OPERAND (node, 1));
12190
12191           if ((cn = patch_string (nn)))
12192             nn = cn;
12193
12194           /* The last part of the rewrite for E1 op= E2 is to have
12195              E1 = (T)(E1 op E2), with T being the type of E1. */
12196           nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12197                                                TREE_TYPE (lvalue), nn));
12198
12199           /* If the assignment is compound and has reference type,
12200              then ensure the LHS has type String and nothing else.  */
12201           if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12202               && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12203             parse_error_context (wfl_op2,
12204                                  "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12205                                  lang_printable_name (TREE_TYPE (lvalue), 0));
12206
12207           /* 15.25.2.b: Left hand is an array access. FIXME */
12208         }
12209
12210       /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12211          function to complete this RHS. Note that a NEW_ARRAY_INIT
12212          might have been already fully expanded if created as a result
12213          of processing an anonymous array initializer. We avoid doing
12214          the operation twice by testing whether the node already bears
12215          a type. */
12216       else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12217         nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12218                                    TREE_OPERAND (node, 1));
12219       /* Otherwise we simply complete the RHS */
12220       else
12221         nn = java_complete_tree (TREE_OPERAND (node, 1));
12222
12223       if (nn == error_mark_node)
12224         return error_mark_node;
12225
12226       /* Write back the RHS as we evaluated it. */
12227       TREE_OPERAND (node, 1) = nn;
12228
12229       /* In case we're handling = with a String as a RHS, we need to
12230          produce a String out of the RHS (it might still be a
12231          STRING_CST or a StringBuffer at this stage */
12232       if ((nn = patch_string (TREE_OPERAND (node, 1))))
12233         TREE_OPERAND (node, 1) = nn;
12234
12235       if ((nn = nested_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12236                                          TREE_OPERAND (node, 1))))
12237         {
12238           /* We return error_mark_node if nested_field_access_fix
12239              detects we write into a final. */
12240           if (nn == error_mark_node)
12241             return error_mark_node;
12242           node = nn;
12243         }
12244       else
12245         {
12246           node = patch_assignment (node, wfl_op1);
12247           if (node == error_mark_node)
12248             return error_mark_node;
12249           /* Reorganize the tree if necessary. */
12250           if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12251                        || JSTRING_P (TREE_TYPE (node))))
12252             node = java_refold (node);
12253         }
12254
12255       /* Seek to set DECL_INITIAL to a proper value, since it might have
12256          undergone a conversion in patch_assignment. We do that only when
12257          it's necessary to have DECL_INITIAL properly set. */
12258       nn = TREE_OPERAND (node, 0);
12259       if (TREE_CODE (nn) == VAR_DECL
12260           && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12261           && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12262           && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12263               || TREE_TYPE (nn) == string_ptr_type_node))
12264         DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12265
12266       CAN_COMPLETE_NORMALLY (node) = 1;
12267       return node;
12268
12269     case MULT_EXPR:
12270     case PLUS_EXPR:
12271     case MINUS_EXPR:
12272     case LSHIFT_EXPR:
12273     case RSHIFT_EXPR:
12274     case URSHIFT_EXPR:
12275     case BIT_AND_EXPR:
12276     case BIT_XOR_EXPR:
12277     case BIT_IOR_EXPR:
12278     case TRUNC_MOD_EXPR:
12279     case TRUNC_DIV_EXPR:
12280     case RDIV_EXPR:
12281     case TRUTH_ANDIF_EXPR:
12282     case TRUTH_ORIF_EXPR:
12283     case EQ_EXPR:
12284     case NE_EXPR:
12285     case GT_EXPR:
12286     case GE_EXPR:
12287     case LT_EXPR:
12288     case LE_EXPR:
12289       /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12290          knows how to handle those cases. */
12291       wfl_op1 = TREE_OPERAND (node, 0);
12292       wfl_op2 = TREE_OPERAND (node, 1);
12293
12294       CAN_COMPLETE_NORMALLY (node) = 1;
12295       /* Don't complete string nodes if dealing with the PLUS operand. */
12296       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12297         {
12298           nn = java_complete_tree (wfl_op1);
12299           if (nn == error_mark_node)
12300             return error_mark_node;
12301
12302           TREE_OPERAND (node, 0) = nn;
12303         }
12304       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12305         {
12306           nn = java_complete_tree (wfl_op2);
12307           if (nn == error_mark_node)
12308             return error_mark_node;
12309
12310           TREE_OPERAND (node, 1) = nn;
12311         }
12312       return patch_binop (node, wfl_op1, wfl_op2, 0);
12313
12314     case INSTANCEOF_EXPR:
12315       wfl_op1 = TREE_OPERAND (node, 0);
12316       COMPLETE_CHECK_OP_0 (node);
12317       return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1), 0);
12318
12319     case UNARY_PLUS_EXPR:
12320     case NEGATE_EXPR:
12321     case TRUTH_NOT_EXPR:
12322     case BIT_NOT_EXPR:
12323     case PREDECREMENT_EXPR:
12324     case PREINCREMENT_EXPR:
12325     case POSTDECREMENT_EXPR:
12326     case POSTINCREMENT_EXPR:
12327     case CONVERT_EXPR:
12328       /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12329          how to handle those cases. */
12330       wfl_op1 = TREE_OPERAND (node, 0);
12331       CAN_COMPLETE_NORMALLY (node) = 1;
12332       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12333       if (TREE_OPERAND (node, 0) == error_mark_node)
12334         return error_mark_node;
12335       node = patch_unaryop (node, wfl_op1);
12336       CAN_COMPLETE_NORMALLY (node) = 1;
12337       break;
12338
12339     case ARRAY_REF:
12340       /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12341          how to handle those cases. */
12342       wfl_op1 = TREE_OPERAND (node, 0);
12343       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12344       if (TREE_OPERAND (node, 0) == error_mark_node)
12345         return error_mark_node;
12346       if (!flag_emit_class_files)
12347         TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12348       /* The same applies to wfl_op2 */
12349       wfl_op2 = TREE_OPERAND (node, 1);
12350       TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12351       if (TREE_OPERAND (node, 1) == error_mark_node)
12352         return error_mark_node;
12353       if (!flag_emit_class_files)
12354         TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12355       return patch_array_ref (node);
12356
12357     case RECORD_TYPE:
12358       return node;;
12359
12360     case COMPONENT_REF:
12361       /* The first step in the re-write of qualified name handling.  FIXME.
12362          So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12363       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12364       if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12365         {
12366           tree name = TREE_OPERAND (node, 1);
12367           tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12368           if (field == NULL_TREE)
12369             {
12370               error ("missing static field %qs", IDENTIFIER_POINTER (name));
12371               return error_mark_node;
12372             }
12373           if (! FIELD_STATIC (field))
12374             {
12375               error ("not a static field %qs", IDENTIFIER_POINTER (name));
12376               return error_mark_node;
12377             }
12378           return field;
12379         }
12380       else
12381         abort ();
12382       break;
12383
12384     case THIS_EXPR:
12385       /* Can't use THIS in a static environment */
12386       if (!current_this)
12387         {
12388           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12389           parse_error_context (wfl_operator,
12390                                "Keyword %<this%> used outside allowed context");
12391           TREE_TYPE (node) = error_mark_node;
12392           return error_mark_node;
12393         }
12394       if (ctxp->explicit_constructor_p)
12395         {
12396           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12397           parse_error_context
12398             (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12399           TREE_TYPE (node) = error_mark_node;
12400           return error_mark_node;
12401         }
12402       return current_this;
12403
12404     case CLASS_LITERAL:
12405       CAN_COMPLETE_NORMALLY (node) = 1;
12406       node = patch_incomplete_class_ref (node);
12407       if (node == error_mark_node)
12408         return error_mark_node;
12409       break;
12410
12411     default:
12412       CAN_COMPLETE_NORMALLY (node) = 1;
12413       /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12414          and it's time to turn it into the appropriate String object */
12415       if ((nn = patch_string (node)))
12416         node = nn;
12417       else
12418         internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12419     }
12420   return node;
12421 }
12422
12423 /* Complete function call's argument. Return a nonzero value is an
12424    error was found.  */
12425
12426 static int
12427 complete_function_arguments (tree node)
12428 {
12429   int flag = 0;
12430   tree cn;
12431
12432   ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12433   for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12434     {
12435       tree wfl = TREE_VALUE (cn), parm, temp;
12436       parm = java_complete_tree (wfl);
12437
12438       if (parm == error_mark_node)
12439         {
12440           flag = 1;
12441           continue;
12442         }
12443       /* If we have a string literal that we haven't transformed yet or a
12444          crafted string buffer, as a result of the use of the String
12445          `+' operator. Build `parm.toString()' and expand it. */
12446       if ((temp = patch_string (parm)))
12447         parm = temp;
12448
12449       TREE_VALUE (cn) = parm;
12450     }
12451   ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12452   return flag;
12453 }
12454
12455 /* Sometimes (for loops and variable initialized during their
12456    declaration), we want to wrap a statement around a WFL and turn it
12457    debugable.  */
12458
12459 static tree
12460 build_debugable_stmt (int location, tree stmt)
12461 {
12462   if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12463     {
12464 #ifdef USE_MAPPED_LOCATION
12465       stmt = expr_add_location (stmt, location, 1);
12466 #else
12467       stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12468       EXPR_WFL_LINECOL (stmt) = location;
12469       JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12470 #endif
12471     }
12472   return stmt;
12473 }
12474
12475 static tree
12476 build_expr_block (tree body, tree decls)
12477 {
12478   tree node = make_node (BLOCK);
12479   BLOCK_EXPR_DECLS (node) = decls;
12480   BLOCK_EXPR_BODY (node) = body;
12481   if (body)
12482     TREE_TYPE (node) = TREE_TYPE (body);
12483   TREE_SIDE_EFFECTS (node) = 1;
12484   return node;
12485 }
12486
12487 /* Create a new function block and link it appropriately to current
12488    function block chain */
12489
12490 static tree
12491 enter_block (void)
12492 {
12493   tree b = build_expr_block (NULL_TREE, NULL_TREE);
12494
12495   /* Link block B supercontext to the previous block. The current
12496      function DECL is used as supercontext when enter_a_block is called
12497      for the first time for a given function. The current function body
12498      (DECL_FUNCTION_BODY) is set to be block B.  */
12499
12500   tree fndecl = current_function_decl;
12501
12502   if (!fndecl) {
12503     BLOCK_SUPERCONTEXT (b) = current_static_block;
12504     current_static_block = b;
12505   }
12506
12507   else if (!DECL_FUNCTION_BODY (fndecl))
12508     {
12509       BLOCK_SUPERCONTEXT (b) = fndecl;
12510       DECL_FUNCTION_BODY (fndecl) = b;
12511     }
12512   else
12513     {
12514       BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12515       DECL_FUNCTION_BODY (fndecl) = b;
12516     }
12517   return b;
12518 }
12519
12520 /* Exit a block by changing the current function body
12521    (DECL_FUNCTION_BODY) to the current block super context, only if
12522    the block being exited isn't the method's top level one.  */
12523
12524 static tree
12525 exit_block (void)
12526 {
12527   tree b;
12528   if (current_function_decl)
12529     {
12530       b = DECL_FUNCTION_BODY (current_function_decl);
12531       if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12532         DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12533     }
12534   else
12535     {
12536       b = current_static_block;
12537
12538       if (BLOCK_SUPERCONTEXT (b))
12539         current_static_block = BLOCK_SUPERCONTEXT (b);
12540     }
12541   return b;
12542 }
12543
12544 /* Lookup for NAME in the nested function's blocks, all the way up to
12545    the current toplevel one. It complies with Java's local variable
12546    scoping rules.  */
12547
12548 static tree
12549 lookup_name_in_blocks (tree name)
12550 {
12551   tree b = GET_CURRENT_BLOCK (current_function_decl);
12552
12553   while (b != current_function_decl)
12554     {
12555       tree current;
12556
12557       /* Paranoid sanity check. To be removed */
12558       if (TREE_CODE (b) != BLOCK)
12559         abort ();
12560
12561       for (current = BLOCK_EXPR_DECLS (b); current;
12562            current = TREE_CHAIN (current))
12563         if (DECL_NAME (current) == name)
12564           return current;
12565       b = BLOCK_SUPERCONTEXT (b);
12566     }
12567   return NULL_TREE;
12568 }
12569
12570 static void
12571 maybe_absorb_scoping_blocks (void)
12572 {
12573   while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12574     {
12575       tree b = exit_block ();
12576       java_method_add_stmt (current_function_decl, b);
12577       SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12578     }
12579 }
12580
12581 \f
12582 /* This section of the source is reserved to build_* functions that
12583    are building incomplete tree nodes and the patch_* functions that
12584    are completing them.  */
12585
12586 /* Wrap a non WFL node around a WFL.  */
12587
12588 static tree
12589 build_wfl_wrap (tree node, int location)
12590 {
12591   tree wfl, node_to_insert = node;
12592
12593   /* We want to process THIS . xxx symbolically, to keep it consistent
12594      with the way we're processing SUPER. A THIS from a primary as a
12595      different form than a SUPER. Turn THIS into something symbolic */
12596   if (TREE_CODE (node) == THIS_EXPR)
12597     node_to_insert = wfl = build_wfl_node (this_identifier_node);
12598   else
12599 #ifdef USE_MAPPED_LOCATION
12600     wfl = build_unknown_wfl (NULL_TREE);
12601
12602   SET_EXPR_LOCATION (wfl, location);
12603 #else
12604     wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12605
12606   EXPR_WFL_LINECOL (wfl) = location;
12607 #endif
12608   EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12609   return wfl;
12610 }
12611
12612 /* Build a super() constructor invocation. Returns an empty statement if
12613    we're currently dealing with the class java.lang.Object. */
12614
12615 static tree
12616 build_super_invocation (tree mdecl)
12617 {
12618   if (DECL_CONTEXT (mdecl) == object_type_node)
12619     return build_java_empty_stmt ();
12620   else
12621     {
12622       tree super_wfl = build_wfl_node (super_identifier_node);
12623       tree a = NULL_TREE, t;
12624
12625       /* This is called after parsing is done, so the parser context
12626          won't be accurate. Set location info from current_class decl. */
12627       tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12628       EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12629
12630       /* If we're dealing with an anonymous class, pass the arguments
12631          of the crafted constructor along. */
12632       if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12633         {
12634           SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12635           for (; t != end_params_node; t = TREE_CHAIN (t))
12636             a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12637         }
12638       return build_method_invocation (super_wfl, a);
12639     }
12640 }
12641
12642 /* Build a SUPER/THIS qualified method invocation.  */
12643
12644 static tree
12645 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12646                                        int lloc, int rloc)
12647 {
12648   tree invok;
12649   tree wfl =
12650     build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12651   EXPR_WFL_LINECOL (wfl) = lloc;
12652   invok = build_method_invocation (name, args);
12653   return make_qualified_primary (wfl, invok, rloc);
12654 }
12655
12656 /* Build an incomplete CALL_EXPR node. */
12657
12658 static tree
12659 build_method_invocation (tree name, tree args)
12660 {
12661   tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12662   TREE_SIDE_EFFECTS (call) = 1;
12663   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12664   return call;
12665 }
12666
12667 /* Build an incomplete new xxx(...) node. */
12668
12669 static tree
12670 build_new_invocation (tree name, tree args)
12671 {
12672   tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12673   TREE_SIDE_EFFECTS (call) = 1;
12674   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12675   return call;
12676 }
12677
12678 /* Build an incomplete assignment expression. */
12679
12680 static tree
12681 build_assignment (int op, int op_location, tree lhs, tree rhs)
12682 {
12683   tree assignment;
12684   /* Build the corresponding binop if we deal with a Compound
12685      Assignment operator. Mark the binop sub-tree as part of a
12686      Compound Assignment expression */
12687   if (op != ASSIGN_TK)
12688     {
12689       rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12690       COMPOUND_ASSIGN_P (rhs) = 1;
12691     }
12692   assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12693   TREE_SIDE_EFFECTS (assignment) = 1;
12694   EXPR_WFL_LINECOL (assignment) = op_location;
12695   return assignment;
12696 }
12697
12698 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12699    the buffer.  This is used only for string conversion.  */
12700 static char *
12701 string_convert_int_cst (tree node)
12702 {
12703   /* Long.MIN_VALUE is -9223372036854775808, 20 characters.  */
12704   static char buffer[21];
12705
12706   unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12707   unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12708   char *p = buffer + sizeof (buffer);
12709   int neg = 0;
12710
12711   unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12712                                   << (HOST_BITS_PER_WIDE_INT - 1));
12713
12714   *--p = '\0';
12715
12716   /* If negative, note the fact and negate the value.  */
12717   if ((hi & hibit))
12718     {
12719       lo = ~lo;
12720       hi = ~hi;
12721       if (++lo == 0)
12722         ++hi;
12723       neg = 1;
12724     }
12725
12726   /* Divide by 10 until there are no bits left.  */
12727   do
12728     {
12729       unsigned HOST_WIDE_INT acc = 0;
12730       unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12731       unsigned int i;
12732
12733       /* Use long division to compute the result and the remainder.  */
12734       for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12735         {
12736           /* Shift a bit into accumulator.  */
12737           acc <<= 1;
12738           if ((hi & hibit))
12739             acc |= 1;
12740
12741           /* Shift the value.  */
12742           hi <<= 1;
12743           if ((lo & hibit))
12744             hi |= 1;
12745           lo <<= 1;
12746
12747           /* Shift the correct bit into the result.  */
12748           outhi <<= 1;
12749           if ((outlo & hibit))
12750             outhi |= 1;
12751           outlo <<= 1;
12752           if (acc >= 10)
12753             {
12754               acc -= 10;
12755               outlo |= 1;
12756             }
12757         }
12758
12759       /* '0' == 060 in Java, but might not be here (think EBCDIC).  */
12760       *--p = '\060' + acc;
12761
12762       hi = outhi;
12763       lo = outlo;
12764     }
12765   while (hi || lo);
12766
12767   if (neg)
12768     *--p = '\055'; /* '-' == 055 in Java, but might not be here.  */
12769
12770   return p;
12771 }
12772
12773 /* Print an INTEGER_CST node in a static buffer, and return the
12774    buffer.  This is used only for error handling.  */
12775 char *
12776 print_int_node (tree node)
12777 {
12778   static char buffer [80];
12779   if (TREE_CONSTANT_OVERFLOW (node))
12780     sprintf (buffer, "<overflow>");
12781
12782   if (TREE_INT_CST_HIGH (node) == 0)
12783     sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12784              TREE_INT_CST_LOW (node));
12785   else if (TREE_INT_CST_HIGH (node) == -1
12786            && TREE_INT_CST_LOW (node) != 0)
12787     sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12788              -TREE_INT_CST_LOW (node));
12789   else
12790     sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12791              TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12792
12793   return buffer;
12794 }
12795
12796 \f
12797 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12798    context.  */
12799
12800 /* 15.25 Assignment operators. */
12801
12802 static tree
12803 patch_assignment (tree node, tree wfl_op1)
12804 {
12805   tree rhs = TREE_OPERAND (node, 1);
12806   tree lvalue = TREE_OPERAND (node, 0), llvalue;
12807   tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12808   int error_found = 0;
12809   int lvalue_from_array = 0;
12810   int is_return = 0;
12811
12812   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12813
12814   /* Lhs can be a named variable */
12815   if (JDECL_P (lvalue))
12816     {
12817       lhs_type = TREE_TYPE (lvalue);
12818     }
12819   /* Or Lhs can be an array access. */
12820   else if (TREE_CODE (lvalue) == ARRAY_REF)
12821     {
12822       lhs_type = TREE_TYPE (lvalue);
12823       lvalue_from_array = 1;
12824     }
12825   /* Or a field access */
12826   else if (TREE_CODE (lvalue) == COMPONENT_REF)
12827     lhs_type = TREE_TYPE (lvalue);
12828   /* Or a function return slot */
12829   else if (TREE_CODE (lvalue) == RESULT_DECL)
12830     {
12831       /* If the return type is an integral type, then we create the
12832          RESULT_DECL with a promoted type, but we need to do these
12833          checks against the unpromoted type to ensure type safety.  So
12834          here we look at the real type, not the type of the decl we
12835          are modifying.  */
12836       lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12837       is_return = 1;
12838     }
12839   /* Otherwise, we might want to try to write into an optimized static
12840      final, this is an of a different nature, reported further on. */
12841   else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12842            && resolve_expression_name (wfl_op1, &llvalue))
12843     {
12844       lhs_type = TREE_TYPE (lvalue);
12845     }
12846   else
12847     {
12848       parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12849       error_found = 1;
12850     }
12851
12852   rhs_type = TREE_TYPE (rhs);
12853
12854   /* 5.1 Try the assignment conversion for builtin type. */
12855   new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12856
12857   /* 5.2 If it failed, try a reference conversion */
12858   if (!new_rhs)
12859     new_rhs = try_reference_assignconv (lhs_type, rhs);
12860
12861   /* 15.25.2 If we have a compound assignment, convert RHS into the
12862      type of the LHS */
12863   else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12864     new_rhs = convert (lhs_type, rhs);
12865
12866   /* Explicit cast required. This is an error */
12867   if (!new_rhs)
12868     {
12869       char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12870       char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12871       tree wfl;
12872       char operation [32];      /* Max size known */
12873
12874       /* If the assignment is part of a declaration, we use the WFL of
12875          the declared variable to point out the error and call it a
12876          declaration problem. If the assignment is a genuine =
12877          operator, we call is a operator `=' problem, otherwise we
12878          call it an assignment problem. In both of these last cases,
12879          we use the WFL of the operator to indicate the error. */
12880
12881       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12882         {
12883           wfl = wfl_op1;
12884           strcpy (operation, "declaration");
12885         }
12886       else
12887         {
12888           wfl = wfl_operator;
12889           if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12890             strcpy (operation, "assignment");
12891           else if (is_return)
12892             strcpy (operation, "'return'");
12893           else
12894             strcpy (operation, "'='");
12895         }
12896
12897       if (!valid_cast_to_p (rhs_type, lhs_type))
12898         parse_error_context
12899           (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12900            operation, t1, t2);
12901       else
12902         parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12903                              operation, t1, t2);
12904       free (t1); free (t2);
12905       error_found = 1;
12906     }
12907
12908   if (error_found)
12909     return error_mark_node;
12910
12911   /* If we're processing a `return' statement, promote the actual type
12912      to the promoted type.  */
12913   if (is_return)
12914     new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12915
12916   /* 10.10: Array Store Exception runtime check */
12917   if (!flag_emit_class_files
12918       && lvalue_from_array
12919       && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12920     {
12921       tree array, store_check, base, index_expr;
12922
12923       /* Save RHS so that it doesn't get re-evaluated by the store check. */
12924       new_rhs = save_expr (new_rhs);
12925
12926       /* Get the INDIRECT_REF. */
12927       array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12928       /* Get the array pointer expr. */
12929       array = TREE_OPERAND (array, 0);
12930       store_check = build_java_arraystore_check (array, new_rhs);
12931
12932       index_expr = TREE_OPERAND (lvalue, 1);
12933
12934       if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12935         {
12936           /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12937              happen before the store check, so prepare to insert the store
12938              check within the second operand of the existing COMPOUND_EXPR. */
12939           base = index_expr;
12940         }
12941       else
12942         base = lvalue;
12943
12944       index_expr = TREE_OPERAND (base, 1);
12945       TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12946                                        store_check, index_expr);
12947     }
12948
12949   /* Final locals can be used as case values in switch
12950      statement. Prepare them for this eventuality. */
12951   if (TREE_CODE (lvalue) == VAR_DECL
12952       && DECL_FINAL (lvalue)
12953       && TREE_CONSTANT (new_rhs)
12954       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12955       && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12956       )
12957     {
12958       TREE_CONSTANT (lvalue) = 1;
12959       TREE_INVARIANT (lvalue) = 1;
12960       DECL_INITIAL (lvalue) = new_rhs;
12961     }
12962
12963   /* Copy the rhs if it's a reference.  */
12964   if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12965     {
12966       switch (TREE_CODE (new_rhs))
12967         {
12968         case ARRAY_REF:
12969         case INDIRECT_REF:
12970         case COMPONENT_REF:
12971           /* Transform a = foo.bar 
12972              into a = ({int tmp; tmp = foo.bar;}).
12973              We need to ensure that if a read from memory fails
12974              because of a NullPointerException, a destination variable
12975              will remain unchanged.  An explicit temporary does what
12976              we need.  
12977
12978              If flag_check_references is set, this is unnecessary
12979              because we'll check each reference before doing any
12980              reads.  If optimize is not set the result will never be
12981              written to a stack slot that contains the LHS.  */
12982           {
12983             tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"), 
12984                                    TREE_TYPE (new_rhs));
12985             tree block = make_node (BLOCK);
12986             tree assignment 
12987               = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12988             DECL_CONTEXT (tmp) = current_function_decl;
12989             TREE_TYPE (block) = TREE_TYPE (new_rhs);
12990             BLOCK_VARS (block) = tmp;
12991             BLOCK_EXPR_BODY (block) = assignment;
12992             TREE_SIDE_EFFECTS (block) = 1;
12993             new_rhs = block;
12994           }
12995           break;
12996         default:
12997           break;
12998         }
12999     }
13000
13001   TREE_OPERAND (node, 0) = lvalue;
13002   TREE_OPERAND (node, 1) = new_rhs;
13003   TREE_TYPE (node) = lhs_type;
13004   return node;
13005 }
13006
13007 /* Check that type SOURCE can be cast into type DEST. If the cast
13008    can't occur at all, return NULL; otherwise, return a possibly
13009    modified rhs.  */
13010
13011 static tree
13012 try_reference_assignconv (tree lhs_type, tree rhs)
13013 {
13014   tree new_rhs = NULL_TREE;
13015   tree rhs_type = TREE_TYPE (rhs);
13016
13017   if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13018     {
13019       /* `null' may be assigned to any reference type */
13020       if (rhs == null_pointer_node)
13021         new_rhs = null_pointer_node;
13022       /* Try the reference assignment conversion */
13023       else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13024         new_rhs = rhs;
13025       /* This is a magic assignment that we process differently */
13026       else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13027         new_rhs = rhs;
13028     }
13029   return new_rhs;
13030 }
13031
13032 /* Check that RHS can be converted into LHS_TYPE by the assignment
13033    conversion (5.2), for the cases of RHS being a builtin type. Return
13034    NULL_TREE if the conversion fails or if because RHS isn't of a
13035    builtin type. Return a converted RHS if the conversion is possible.  */
13036
13037 static tree
13038 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13039 {
13040   tree new_rhs = NULL_TREE;
13041   tree rhs_type = TREE_TYPE (rhs);
13042
13043   /* Handle boolean specially.  */
13044   if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13045       || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13046     {
13047       if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13048           && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13049         new_rhs = rhs;
13050     }
13051
13052   /* 5.1.1 Try Identity Conversion,
13053      5.1.2 Try Widening Primitive Conversion */
13054   else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13055     new_rhs = convert (lhs_type, rhs);
13056
13057   /* Try a narrowing primitive conversion (5.1.3):
13058        - expression is a constant expression of type byte, short, char,
13059          or int, AND
13060        - variable is byte, short or char AND
13061        - The value of the expression is representable in the type of the
13062          variable */
13063   else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13064             || rhs_type == char_type_node || rhs_type == int_type_node)
13065             && TREE_CONSTANT (rhs)
13066            && (lhs_type == byte_type_node || lhs_type == char_type_node
13067                || lhs_type == short_type_node))
13068     {
13069       if (int_fits_type_p (rhs, lhs_type))
13070         new_rhs = convert (lhs_type, rhs);
13071       else if (wfl_op1)         /* Might be called with a NULL */
13072         parse_warning_context
13073           (wfl_op1,
13074            "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13075            print_int_node (rhs), lang_printable_name (lhs_type, 0));
13076       /* Reported a warning that will turn into an error further
13077          down, so we don't return */
13078     }
13079
13080   return new_rhs;
13081 }
13082
13083 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13084    conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
13085    0 is the conversion test fails.  This implements parts the method
13086    invocation conversion (5.3).  */
13087
13088 static int
13089 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13090 {
13091   /* 5.1.1: This is the identity conversion part. */
13092   if (lhs_type == rhs_type)
13093     return 1;
13094
13095   /* Reject non primitive types and boolean conversions.  */
13096   if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13097     return 0;
13098
13099   /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13100      than a char can't be converted into a char. Short can't too, but
13101      the < test below takes care of that */
13102   if (lhs_type == char_type_node && rhs_type == byte_type_node)
13103     return 0;
13104
13105   /* Accept all promoted type here. Note, we can't use <= in the test
13106      below, because we still need to bounce out assignments of short
13107      to char and the likes */
13108   if (lhs_type == int_type_node
13109       && (rhs_type == promoted_byte_type_node
13110           || rhs_type == promoted_short_type_node
13111           || rhs_type == promoted_char_type_node
13112           || rhs_type == promoted_boolean_type_node))
13113     return 1;
13114
13115   /* From here, an integral is widened if its precision is smaller
13116      than the precision of the LHS or if the LHS is a floating point
13117      type, or the RHS is a float and the RHS a double. */
13118   if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13119        && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13120       || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13121       || (rhs_type == float_type_node && lhs_type == double_type_node))
13122     return 1;
13123
13124   return 0;
13125 }
13126
13127 /* Check that something of SOURCE type can be assigned or cast to
13128    something of DEST type at runtime. Return 1 if the operation is
13129    valid, 0 otherwise. If CAST is set to 1, we're treating the case
13130    were SOURCE is cast into DEST, which borrows a lot of the
13131    assignment check. */
13132
13133 static int
13134 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13135 {
13136   /* SOURCE or DEST might be null if not from a declared entity. */
13137   if (!source || !dest)
13138     return 0;
13139   if (JNULLP_TYPE_P (source))
13140     return 1;
13141   if (TREE_CODE (source) == POINTER_TYPE)
13142     source = TREE_TYPE (source);
13143   if (TREE_CODE (dest) == POINTER_TYPE)
13144     dest = TREE_TYPE (dest);
13145
13146   /* If source and dest are being compiled from bytecode, they may need to
13147      be loaded. */
13148   if (CLASS_P (source) && !CLASS_LOADED_P (source))
13149     {
13150       load_class (source, 1);
13151       safe_layout_class (source);
13152     }
13153   if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13154     {
13155       load_class (dest, 1);
13156       safe_layout_class (dest);
13157     }
13158
13159   /* Case where SOURCE is a class type */
13160   if (TYPE_CLASS_P (source))
13161     {
13162       if (TYPE_CLASS_P (dest))
13163         return  (source == dest
13164                  || inherits_from_p (source, dest)
13165                  || (cast && inherits_from_p (dest, source)));
13166       if (TYPE_INTERFACE_P (dest))
13167         {
13168           /* If doing a cast and SOURCE is final, the operation is
13169              always correct a compile time (because even if SOURCE
13170              does not implement DEST, a subclass of SOURCE might). */
13171           if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13172             return 1;
13173           /* Otherwise, SOURCE must implement DEST */
13174           return interface_of_p (dest, source);
13175         }
13176       /* DEST is an array, cast permitted if SOURCE is of Object type */
13177       return (cast && source == object_type_node ? 1 : 0);
13178     }
13179   if (TYPE_INTERFACE_P (source))
13180     {
13181       if (TYPE_CLASS_P (dest))
13182         {
13183           /* If not casting, DEST must be the Object type */
13184           if (!cast)
13185             return dest == object_type_node;
13186           /* We're doing a cast. The cast is always valid is class
13187              DEST is not final, otherwise, DEST must implement SOURCE */
13188           else if (!CLASS_FINAL (TYPE_NAME (dest)))
13189             return 1;
13190           else
13191             return interface_of_p (source, dest);
13192         }
13193       if (TYPE_INTERFACE_P (dest))
13194         {
13195           /* If doing a cast, then if SOURCE and DEST contain method
13196              with the same signature but different return type, then
13197              this is a (compile time) error */
13198           if (cast)
13199             {
13200               tree method_source, method_dest;
13201               tree source_type;
13202               tree source_sig;
13203               tree source_name;
13204               for (method_source = TYPE_METHODS (source); method_source;
13205                    method_source = TREE_CHAIN (method_source))
13206                 {
13207                   source_sig =
13208                     build_java_argument_signature (TREE_TYPE (method_source));
13209                   source_type = TREE_TYPE (TREE_TYPE (method_source));
13210                   source_name = DECL_NAME (method_source);
13211                   for (method_dest = TYPE_METHODS (dest);
13212                        method_dest; method_dest = TREE_CHAIN (method_dest))
13213                     if (source_sig ==
13214                         build_java_argument_signature (TREE_TYPE (method_dest))
13215                         && source_name == DECL_NAME (method_dest)
13216                         && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13217                       return 0;
13218                 }
13219               return 1;
13220             }
13221           else
13222             return source == dest || interface_of_p (dest, source);
13223         }
13224       else
13225         {
13226           /* Array */
13227           return (cast
13228                   && (DECL_NAME (TYPE_NAME (source))
13229                       == java_lang_cloneable_identifier_node
13230                       || (DECL_NAME (TYPE_NAME (source))
13231                           == java_io_serializable_identifier_node)));
13232         }
13233     }
13234   if (TYPE_ARRAY_P (source))
13235     {
13236       if (TYPE_CLASS_P (dest))
13237         return dest == object_type_node;
13238       /* Can't cast an array to an interface unless the interface is
13239          java.lang.Cloneable or java.io.Serializable.  */
13240       if (TYPE_INTERFACE_P (dest))
13241         return (DECL_NAME (TYPE_NAME (dest))
13242                 == java_lang_cloneable_identifier_node
13243                 || (DECL_NAME (TYPE_NAME (dest))
13244                     == java_io_serializable_identifier_node));
13245       else                      /* Arrays */
13246         {
13247           tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13248           tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13249
13250           /* In case of severe errors, they turn out null */
13251           if (!dest_element_type || !source_element_type)
13252             return 0;
13253           if (source_element_type == dest_element_type)
13254             return 1;
13255           return valid_ref_assignconv_cast_p (source_element_type,
13256                                               dest_element_type, cast);
13257         }
13258       return 0;
13259     }
13260   return 0;
13261 }
13262
13263 static int
13264 valid_cast_to_p (tree source, tree dest)
13265 {
13266   if (TREE_CODE (source) == POINTER_TYPE)
13267     source = TREE_TYPE (source);
13268   if (TREE_CODE (dest) == POINTER_TYPE)
13269     dest = TREE_TYPE (dest);
13270
13271   if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13272     return valid_ref_assignconv_cast_p (source, dest, 1);
13273
13274   else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13275     return 1;
13276
13277   else if (TREE_CODE (source) == BOOLEAN_TYPE
13278            && TREE_CODE (dest) == BOOLEAN_TYPE)
13279     return 1;
13280
13281   return 0;
13282 }
13283
13284 static tree
13285 do_unary_numeric_promotion (tree arg)
13286 {
13287   tree type = TREE_TYPE (arg);
13288   if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13289       || TREE_CODE (type) == CHAR_TYPE)
13290     arg = convert (int_type_node, arg);
13291   return arg;
13292 }
13293
13294 /* Return a nonzero value if SOURCE can be converted into DEST using
13295    the method invocation conversion rule (5.3).  */
13296 static int
13297 valid_method_invocation_conversion_p (tree dest, tree source)
13298 {
13299   return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13300            && valid_builtin_assignconv_identity_widening_p (dest, source))
13301           || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13302               && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13303               && valid_ref_assignconv_cast_p (source, dest, 0)));
13304 }
13305
13306 /* Build an incomplete binop expression. */
13307
13308 static tree
13309 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13310 {
13311   tree binop = build2 (op, NULL_TREE, op1, op2);
13312   TREE_SIDE_EFFECTS (binop) = 1;
13313   /* Store the location of the operator, for better error report. The
13314      string of the operator will be rebuild based on the OP value. */
13315   EXPR_WFL_LINECOL (binop) = op_location;
13316   return binop;
13317 }
13318
13319 /* Build the string of the operator retained by NODE. If NODE is part
13320    of a compound expression, add an '=' at the end of the string. This
13321    function is called when an error needs to be reported on an
13322    operator. The string is returned as a pointer to a static character
13323    buffer. */
13324
13325 static char *
13326 operator_string (tree node)
13327 {
13328 #define BUILD_OPERATOR_STRING(S)                                        \
13329   {                                                                     \
13330     sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13331     return buffer;                                                      \
13332   }
13333
13334   static char buffer [10];
13335   switch (TREE_CODE (node))
13336     {
13337     case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13338     case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13339     case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13340     case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13341     case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13342     case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13343     case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13344     case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13345     case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13346     case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13347     case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13348     case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13349     case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13350     case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13351     case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13352     case GT_EXPR: BUILD_OPERATOR_STRING (">");
13353     case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13354     case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13355     case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13356     case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13357     case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13358     case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13359     case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13360     case PREINCREMENT_EXPR:     /* Fall through */
13361     case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13362     case PREDECREMENT_EXPR:     /* Fall through */
13363     case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13364     default:
13365       internal_error ("unregistered operator %s",
13366                       tree_code_name [TREE_CODE (node)]);
13367     }
13368   return NULL;
13369 #undef BUILD_OPERATOR_STRING
13370 }
13371
13372 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13373
13374 static int
13375 java_decl_equiv (tree var_acc1, tree var_acc2)
13376 {
13377   if (JDECL_P (var_acc1))
13378     return (var_acc1 == var_acc2);
13379
13380   return (TREE_CODE (var_acc1) == COMPONENT_REF
13381           && TREE_CODE (var_acc2) == COMPONENT_REF
13382           && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13383              == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13384           && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13385 }
13386
13387 /* Return a nonzero value if CODE is one of the operators that can be
13388    used in conjunction with the `=' operator in a compound assignment.  */
13389
13390 static int
13391 binop_compound_p (enum tree_code code)
13392 {
13393   int i;
13394   for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13395     if (binop_lookup [i] == code)
13396       break;
13397
13398   return i < BINOP_COMPOUND_CANDIDATES;
13399 }
13400
13401 /* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13402
13403 static tree
13404 java_refold (tree t)
13405 {
13406   tree c, b, ns, decl;
13407
13408   if (TREE_CODE (t) != MODIFY_EXPR)
13409     return t;
13410
13411   c = TREE_OPERAND (t, 1);
13412   if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13413          && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13414          && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13415     return t;
13416
13417   /* Now the left branch of the binary operator. */
13418   b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13419   if (! (b && TREE_CODE (b) == NOP_EXPR
13420          && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13421     return t;
13422
13423   ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13424   if (! (ns && TREE_CODE (ns) == NOP_EXPR
13425          && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13426     return t;
13427
13428   decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13429   if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13430       /* It's got to be the an equivalent decl */
13431       && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13432     {
13433       /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13434       TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13435       /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13436       TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13437       /* Change the right part of the BINOP_EXPR */
13438       TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13439     }
13440
13441   return t;
13442 }
13443
13444 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13445    errors but we modify NODE so that it contains the type computed
13446    according to the expression, when it's fixed. Otherwise, we write
13447    error_mark_node as the type. It allows us to further the analysis
13448    of remaining nodes and detects more errors in certain cases.  */
13449
13450 static tree
13451 patch_binop (tree node, tree wfl_op1, tree wfl_op2, int folding)
13452 {
13453   tree op1 = TREE_OPERAND (node, 0);
13454   tree op2 = TREE_OPERAND (node, 1);
13455   tree op1_type = TREE_TYPE (op1);
13456   tree op2_type = TREE_TYPE (op2);
13457   tree prom_type = NULL_TREE, cn;
13458   enum tree_code code = TREE_CODE (node);
13459
13460   /* If 1, tell the routine that we have to return error_mark_node
13461      after checking for the initialization of the RHS */
13462   int error_found = 0;
13463
13464   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13465
13466   /* If either op<n>_type are NULL, this might be early signs of an
13467      error situation, unless it's too early to tell (in case we're
13468      handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13469      correctly so the error can be later on reported accurately. */
13470   if (! (code == PLUS_EXPR || code == NE_EXPR
13471          || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13472     {
13473       tree n;
13474       if (! op1_type)
13475         {
13476           n = java_complete_tree (op1);
13477           op1_type = TREE_TYPE (n);
13478         }
13479       if (! op2_type)
13480         {
13481           n = java_complete_tree (op2);
13482           op2_type = TREE_TYPE (n);
13483         }
13484     }
13485
13486   switch (code)
13487     {
13488     /* 15.16 Multiplicative operators */
13489     case MULT_EXPR:             /* 15.16.1 Multiplication Operator * */
13490     case RDIV_EXPR:             /* 15.16.2 Division Operator / */
13491     case TRUNC_DIV_EXPR:        /* 15.16.2 Integral type Division Operator / */
13492     case TRUNC_MOD_EXPR:        /* 15.16.3 Remainder operator % */
13493       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13494         {
13495           if (!JNUMERIC_TYPE_P (op1_type))
13496             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13497           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13498             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13499           TREE_TYPE (node) = error_mark_node;
13500           error_found = 1;
13501           break;
13502         }
13503       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13504
13505       /* Detect integral division by zero */
13506       if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13507           && TREE_CODE (prom_type) == INTEGER_TYPE
13508           && (op2 == integer_zero_node || op2 == long_zero_node ||
13509               (TREE_CODE (op2) == INTEGER_CST &&
13510                ! TREE_INT_CST_LOW (op2)  && ! TREE_INT_CST_HIGH (op2))))
13511         {
13512           parse_warning_context
13513             (wfl_operator,
13514              "Evaluating this expression will result in an arithmetic exception being thrown");
13515           TREE_CONSTANT (node) = 0;
13516           TREE_INVARIANT (node) = 0;
13517         }
13518
13519       /* Change the division operator if necessary */
13520       if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13521         TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13522
13523       /* Before divisions as is disappear, try to simplify and bail if
13524          applicable, otherwise we won't perform even simple
13525          simplifications like (1-1)/3. We can't do that with floating
13526          point number, folds can't handle them at this stage. */
13527       if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13528           && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13529         {
13530           TREE_TYPE (node) = prom_type;
13531           node = fold (node);
13532           if (TREE_CODE (node) != code)
13533             return node;
13534         }
13535
13536       if (TREE_CODE (prom_type) == INTEGER_TYPE
13537           && flag_use_divide_subroutine
13538           && ! flag_emit_class_files
13539           && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13540         return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13541
13542       /* This one is more complicated. FLOATs are processed by a
13543          function call to soft_fmod. Duplicate the value of the
13544          COMPOUND_ASSIGN_P flag. */
13545       if (code == TRUNC_MOD_EXPR)
13546         {
13547           tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13548           COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13549           return mod;
13550         }
13551       break;
13552
13553     /* 15.17 Additive Operators */
13554     case PLUS_EXPR:             /* 15.17.1 String Concatenation Operator + */
13555
13556       /* Operation is valid if either one argument is a string
13557          constant, a String object or a StringBuffer crafted for the
13558          purpose of the a previous usage of the String concatenation
13559          operator */
13560
13561       if (TREE_CODE (op1) == STRING_CST
13562           || TREE_CODE (op2) == STRING_CST
13563           || JSTRING_TYPE_P (op1_type)
13564           || JSTRING_TYPE_P (op2_type)
13565           || IS_CRAFTED_STRING_BUFFER_P (op1)
13566           || IS_CRAFTED_STRING_BUFFER_P (op2))
13567         return build_string_concatenation (op1, op2);
13568
13569     case MINUS_EXPR:            /* 15.17.2 Additive Operators (+ and -) for
13570                                    Numeric Types */
13571       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13572         {
13573           if (!JNUMERIC_TYPE_P (op1_type))
13574             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13575           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13576             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13577           TREE_TYPE (node) = error_mark_node;
13578           error_found = 1;
13579           break;
13580         }
13581       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13582       break;
13583
13584     /* 15.18 Shift Operators */
13585     case LSHIFT_EXPR:
13586     case RSHIFT_EXPR:
13587     case URSHIFT_EXPR:
13588       if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13589         {
13590           if (!JINTEGRAL_TYPE_P (op1_type))
13591             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13592           else
13593             {
13594               if (JNUMERIC_TYPE_P (op2_type))
13595                 parse_error_context (wfl_operator,
13596                                      "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13597                                      operator_string (node),
13598                                      lang_printable_name (op2_type, 0));
13599               else
13600                 parse_error_context (wfl_operator,
13601                                      "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13602                                      operator_string (node),
13603                                      lang_printable_name (op2_type, 0));
13604             }
13605           TREE_TYPE (node) = error_mark_node;
13606           error_found = 1;
13607           break;
13608         }
13609
13610       /* Unary numeric promotion (5.6.1) is performed on each operand
13611          separately */
13612       op1 = do_unary_numeric_promotion (op1);
13613       op2 = do_unary_numeric_promotion (op2);
13614
13615       /* If the right hand side is of type `long', first cast it to
13616          `int'.  */
13617       if (TREE_TYPE (op2) == long_type_node)
13618         op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13619
13620       /* The type of the shift expression is the type of the promoted
13621          type of the left-hand operand */
13622       prom_type = TREE_TYPE (op1);
13623
13624       /* Shift int only up to 0x1f and long up to 0x3f */
13625       if (prom_type == int_type_node)
13626         op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13627                             build_int_cst (NULL_TREE, 0x1f)));
13628       else
13629         op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13630                             build_int_cst (NULL_TREE, 0x3f)));
13631
13632       /* The >>> operator is a >> operating on unsigned quantities */
13633       if (code == URSHIFT_EXPR && (folding || ! flag_emit_class_files))
13634         {
13635           tree to_return;
13636           tree utype = java_unsigned_type (prom_type);
13637           op1 = convert (utype, op1);
13638
13639           to_return = fold_build2 (RSHIFT_EXPR, utype, op1, op2);
13640           to_return = convert (prom_type, to_return);
13641           /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13642           COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13643           TREE_SIDE_EFFECTS (to_return)
13644             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13645           return to_return;
13646         }
13647       break;
13648
13649       /* 15.19.1 Type Comparison Operator instanceof */
13650     case INSTANCEOF_EXPR:
13651
13652       TREE_TYPE (node) = boolean_type_node;
13653
13654       /* OP1_TYPE might be NULL when OP1 is a string constant.  */
13655       if ((cn = patch_string (op1)))
13656         {
13657           op1 = cn;
13658           op1_type = TREE_TYPE (op1);
13659         }
13660       if (op1_type == NULL_TREE)
13661         abort ();
13662
13663       if (!(op2_type = resolve_type_during_patch (op2)))
13664         return error_mark_node;
13665
13666       /* The first operand must be a reference type or the null type */
13667       if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13668         error_found = 1;        /* Error reported further below */
13669
13670       /* The second operand must be a reference type */
13671       if (!JREFERENCE_TYPE_P (op2_type))
13672         {
13673           SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13674           parse_error_context
13675             (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13676              lang_printable_name (op2_type, 0));
13677           error_found = 1;
13678         }
13679
13680       if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13681         {
13682           /* If the first operand is null, the result is always false */
13683           if (op1 == null_pointer_node)
13684             return boolean_false_node;
13685           else if (flag_emit_class_files)
13686             {
13687               TREE_OPERAND (node, 1) = op2_type;
13688               TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13689               return node;
13690             }
13691           /* Otherwise we have to invoke instance of to figure it out */
13692           else
13693             return build_instanceof (op1, op2_type);
13694         }
13695       /* There is no way the expression operand can be an instance of
13696          the type operand. This is a compile time error. */
13697       else
13698         {
13699           char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13700           SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13701           parse_error_context
13702             (wfl_operator, "Impossible for %qs to be instance of %qs",
13703              t1, lang_printable_name (op2_type, 0));
13704           free (t1);
13705           error_found = 1;
13706         }
13707
13708       break;
13709
13710       /* 15.21 Bitwise and Logical Operators */
13711     case BIT_AND_EXPR:
13712     case BIT_XOR_EXPR:
13713     case BIT_IOR_EXPR:
13714       if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13715         /* Binary numeric promotion is performed on both operand and the
13716            expression retain that type */
13717         prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13718
13719       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13720                && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13721         /* The type of the bitwise operator expression is BOOLEAN */
13722         prom_type = boolean_type_node;
13723       else
13724         {
13725           if (!JINTEGRAL_TYPE_P (op1_type))
13726             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13727           if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13728             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13729           TREE_TYPE (node) = error_mark_node;
13730           error_found = 1;
13731           /* Insert a break here if adding thing before the switch's
13732              break for this case */
13733         }
13734       break;
13735
13736       /* 15.22 Conditional-And Operator */
13737     case TRUTH_ANDIF_EXPR:
13738       /* 15.23 Conditional-Or Operator */
13739     case TRUTH_ORIF_EXPR:
13740       /* Operands must be of BOOLEAN type */
13741       if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13742           TREE_CODE (op2_type) != BOOLEAN_TYPE)
13743         {
13744           if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13745             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13746           if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13747             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13748           TREE_TYPE (node) = boolean_type_node;
13749           error_found = 1;
13750           break;
13751         }
13752       else if (integer_zerop (op1))
13753         {
13754           return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13755         }
13756       else if (integer_onep (op1))
13757         {
13758           return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13759         }
13760       /* The type of the conditional operators is BOOLEAN */
13761       prom_type = boolean_type_node;
13762       break;
13763
13764       /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13765     case LT_EXPR:
13766     case GT_EXPR:
13767     case LE_EXPR:
13768     case GE_EXPR:
13769       /* The type of each of the operands must be a primitive numeric
13770          type */
13771       if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13772         {
13773           if (!JNUMERIC_TYPE_P (op1_type))
13774             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13775           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13776             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13777           TREE_TYPE (node) = boolean_type_node;
13778           error_found = 1;
13779           break;
13780         }
13781       /* Binary numeric promotion is performed on the operands */
13782       binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13783       /* The type of the relation expression is always BOOLEAN */
13784       prom_type = boolean_type_node;
13785       break;
13786
13787       /* 15.20 Equality Operator */
13788     case EQ_EXPR:
13789     case NE_EXPR:
13790       /* It's time for us to patch the strings. */
13791       if ((cn = patch_string (op1)))
13792        {
13793          op1 = cn;
13794          op1_type = TREE_TYPE (op1);
13795        }
13796       if ((cn = patch_string (op2)))
13797        {
13798          op2 = cn;
13799          op2_type = TREE_TYPE (op2);
13800        }
13801
13802       /* 15.20.1 Numerical Equality Operators == and != */
13803       /* Binary numeric promotion is performed on the operands */
13804       if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13805         binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13806
13807       /* 15.20.2 Boolean Equality Operators == and != */
13808       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13809           TREE_CODE (op2_type) == BOOLEAN_TYPE)
13810         ;                       /* Nothing to do here */
13811
13812       /* 15.20.3 Reference Equality Operators == and != */
13813       /* Types have to be either references or the null type. If
13814          they're references, it must be possible to convert either
13815          type to the other by casting conversion. */
13816       else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13817                || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13818                || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13819                || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13820                    && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13821                        || valid_ref_assignconv_cast_p (op2_type,
13822                                                        op1_type, 1))))
13823         ;                       /* Nothing to do here */
13824
13825       /* Else we have an error figure what can't be converted into
13826          what and report the error */
13827       else
13828         {
13829           char *t1;
13830           t1 = xstrdup (lang_printable_name (op1_type, 0));
13831           parse_error_context
13832             (wfl_operator,
13833              "Incompatible type for %qs. Can't convert %qs to %qs",
13834              operator_string (node), t1,
13835              lang_printable_name (op2_type, 0));
13836           free (t1);
13837           TREE_TYPE (node) = boolean_type_node;
13838           error_found = 1;
13839           break;
13840         }
13841       prom_type = boolean_type_node;
13842       break;
13843     default:
13844       abort ();
13845     }
13846
13847   if (error_found)
13848     return error_mark_node;
13849
13850   TREE_OPERAND (node, 0) = op1;
13851   TREE_OPERAND (node, 1) = op2;
13852   TREE_TYPE (node) = prom_type;
13853   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13854
13855   /* fold does not respect side-effect order as required for Java but not C.
13856    * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13857    * bytecode.
13858    */
13859   if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13860       : ! TREE_SIDE_EFFECTS (node))
13861     node = fold (node);
13862   return node;
13863 }
13864
13865 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13866    zero value, the value of CSTE comes after the valude of STRING */
13867
13868 static tree
13869 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13870 {
13871   const char *old = TREE_STRING_POINTER (cste);
13872   int old_len = TREE_STRING_LENGTH (cste);
13873   int len = old_len + string_len;
13874   char *new = alloca (len+1);
13875
13876   if (after)
13877     {
13878       memcpy (new, string, string_len);
13879       memcpy (&new [string_len], old, old_len);
13880     }
13881   else
13882     {
13883       memcpy (new, old, old_len);
13884       memcpy (&new [old_len], string, string_len);
13885     }
13886   new [len] = '\0';
13887   return build_string (len, new);
13888 }
13889
13890 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13891    new STRING_CST on success, NULL_TREE on failure.  */
13892
13893 static tree
13894 merge_string_cste (tree op1, tree op2, int after)
13895 {
13896   /* Handle two string constants right away.  */
13897   if (TREE_CODE (op2) == STRING_CST)
13898     return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13899                                  TREE_STRING_LENGTH (op2), after);
13900
13901   /* Reasonable integer constant can be treated right away.  */
13902   if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13903     {
13904       static const char *const boolean_true = "true";
13905       static const char *const boolean_false = "false";
13906       static const char *const null_pointer = "null";
13907       char ch[4];
13908       const char *string;
13909
13910       if (op2 == boolean_true_node)
13911         string = boolean_true;
13912       else if (op2 == boolean_false_node)
13913         string = boolean_false;
13914       else if (op2 == null_pointer_node
13915                || (integer_zerop (op2)
13916                    && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13917         /* FIXME: null is not a compile-time constant, so it is only safe to
13918            merge if the overall expression is non-constant. However, this
13919            code always merges without checking the overall expression.  */
13920         string = null_pointer;
13921       else if (TREE_TYPE (op2) == char_type_node)
13922         {
13923           /* Convert the character into UTF-8.  */
13924           unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13925           unsigned char *p = (unsigned char *) ch;
13926           if (0x01 <= c && c <= 0x7f)
13927             *p++ = (unsigned char) c;
13928           else if (c < 0x7ff)
13929             {
13930               *p++ = (unsigned char) (c >> 6 | 0xc0);
13931               *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13932             }
13933           else
13934             {
13935               *p++ = (unsigned char) (c >> 12 | 0xe0);
13936               *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13937               *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13938             }
13939           *p = '\0';
13940
13941           string = ch;
13942         }
13943       else
13944         string = string_convert_int_cst (op2);
13945
13946       return do_merge_string_cste (op1, string, strlen (string), after);
13947     }
13948   return NULL_TREE;
13949 }
13950
13951 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13952    has to be a STRING_CST and the other part must be a STRING_CST or a
13953    INTEGRAL constant. Return a new STRING_CST if the operation
13954    succeed, NULL_TREE otherwise.
13955
13956    If the case we want to optimize for space, we might want to return
13957    NULL_TREE for each invocation of this routine. FIXME */
13958
13959 static tree
13960 string_constant_concatenation (tree op1, tree op2)
13961 {
13962   if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13963     {
13964       tree string, rest;
13965       int invert;
13966
13967       string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13968       rest   = (string == op1 ? op2 : op1);
13969       invert = (string == op1 ? 0 : 1 );
13970
13971       /* Walk REST, only if it looks reasonable */
13972       if (TREE_CODE (rest) != STRING_CST
13973           && !IS_CRAFTED_STRING_BUFFER_P (rest)
13974           && !JSTRING_TYPE_P (TREE_TYPE (rest))
13975           && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13976         {
13977           rest = java_complete_tree (rest);
13978           if (rest == error_mark_node)
13979             return error_mark_node;
13980           rest = fold (rest);
13981         }
13982       return merge_string_cste (string, rest, invert);
13983     }
13984   return NULL_TREE;
13985 }
13986
13987 /* Implement the `+' operator. Does static optimization if possible,
13988    otherwise create (if necessary) and append elements to a
13989    StringBuffer. The StringBuffer will be carried around until it is
13990    used for a function call or an assignment. Then toString() will be
13991    called on it to turn it into a String object. */
13992
13993 static tree
13994 build_string_concatenation (tree op1, tree op2)
13995 {
13996   tree result;
13997   int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13998
13999   /* Try to do some static optimization */
14000   if ((result = string_constant_concatenation (op1, op2)))
14001     return result;
14002
14003   /* Discard empty strings on either side of the expression */
14004   if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
14005     {
14006       op1 = op2;
14007       op2 = NULL_TREE;
14008     }
14009   else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14010     op2 = NULL_TREE;
14011
14012   /* If operands are string constant, turn then into object references */
14013   if (TREE_CODE (op1) == STRING_CST)
14014     op1 = patch_string_cst (op1);
14015   if (op2 && TREE_CODE (op2) == STRING_CST)
14016     op2 = patch_string_cst (op2);
14017
14018   /* If either one of the constant is null and the other non null
14019      operand is a String constant, return it. */
14020   if ((TREE_CODE (op1) == STRING_CST) && !op2)
14021     return op1;
14022
14023   /* If OP1 isn't already a StringBuffer, create and
14024      initialize a new one */
14025   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14026     {
14027       /* Two solutions here:
14028          1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14029          2) OP1 is something else, we call new StringBuffer().append(OP1).  */
14030       if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14031         op1 = BUILD_STRING_BUFFER (op1);
14032       else
14033         {
14034           tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14035           op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14036         }
14037     }
14038
14039   if (op2)
14040     {
14041       /* OP1 is no longer the last node holding a crafted StringBuffer */
14042       IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14043       /* Create a node for `{new...,xxx}.append (op2)' */
14044       op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14045     }
14046
14047   /* Mark the last node holding a crafted StringBuffer */
14048   IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14049
14050   TREE_SIDE_EFFECTS (op1) = side_effects;
14051   return op1;
14052 }
14053
14054 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14055    StringBuffer. If no string were found to be patched, return
14056    NULL. */
14057
14058 static tree
14059 patch_string (tree node)
14060 {
14061   if (node == error_mark_node)
14062     return error_mark_node;
14063   if (TREE_CODE (node) == STRING_CST)
14064     return patch_string_cst (node);
14065   else if (IS_CRAFTED_STRING_BUFFER_P (node))
14066     {
14067       int saved = ctxp->explicit_constructor_p;
14068       tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14069       tree ret;
14070       /* Temporary disable forbid the use of `this'. */
14071       ctxp->explicit_constructor_p = 0;
14072       ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14073       /* String concatenation arguments must be evaluated in order too. */
14074       ret = force_evaluation_order (ret);
14075       /* Restore it at its previous value */
14076       ctxp->explicit_constructor_p = saved;
14077       return ret;
14078     }
14079   return NULL_TREE;
14080 }
14081
14082 /* Build the internal representation of a string constant.  */
14083
14084 static tree
14085 patch_string_cst (tree node)
14086 {
14087   int location;
14088   if (! flag_emit_class_files)
14089     {
14090       node = get_identifier (TREE_STRING_POINTER (node));
14091       location = alloc_name_constant (CONSTANT_String, node);
14092       node = build_ref_from_constant_pool (location);
14093     }
14094   TREE_CONSTANT (node) = 1;
14095   TREE_INVARIANT (node) = 1;
14096
14097   /* ??? Guessing that the class file code can't handle casts.  */
14098   if (! flag_emit_class_files)
14099     node = convert (string_ptr_type_node, node);
14100   else
14101     TREE_TYPE (node) = string_ptr_type_node;
14102
14103   return node;
14104 }
14105
14106 /* Build an incomplete unary operator expression. */
14107
14108 static tree
14109 build_unaryop (int op_token, int op_location, tree op1)
14110 {
14111   enum tree_code op;
14112   tree unaryop;
14113   switch (op_token)
14114     {
14115     case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14116     case MINUS_TK: op = NEGATE_EXPR; break;
14117     case NEG_TK: op = TRUTH_NOT_EXPR; break;
14118     case NOT_TK: op = BIT_NOT_EXPR; break;
14119     default: abort ();
14120     }
14121
14122   unaryop = build1 (op, NULL_TREE, op1);
14123   TREE_SIDE_EFFECTS (unaryop) = 1;
14124   /* Store the location of the operator, for better error report. The
14125      string of the operator will be rebuild based on the OP value. */
14126   EXPR_WFL_LINECOL (unaryop) = op_location;
14127   return unaryop;
14128 }
14129
14130 /* Special case for the ++/-- operators, since they require an extra
14131    argument to build, which is set to NULL and patched
14132    later. IS_POST_P is 1 if the operator, 0 otherwise.  */
14133
14134 static tree
14135 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14136 {
14137   static const enum tree_code lookup [2][2] =
14138     {
14139       { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14140       { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14141     };
14142   tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14143                       NULL_TREE, op1, NULL_TREE);
14144   TREE_SIDE_EFFECTS (node) = 1;
14145   /* Store the location of the operator, for better error report. The
14146      string of the operator will be rebuild based on the OP value. */
14147   EXPR_WFL_LINECOL (node) = op_location;
14148   return node;
14149 }
14150
14151 /* Build an incomplete cast operator, based on the use of the
14152    CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14153    set. java_complete_tree is trained to walk a CONVERT_EXPR even
14154    though its type is already set.  */
14155
14156 static tree
14157 build_cast (int location, tree type, tree exp)
14158 {
14159   tree node = build1 (CONVERT_EXPR, type, exp);
14160   EXPR_WFL_LINECOL (node) = location;
14161   return node;
14162 }
14163
14164 /* Build an incomplete class reference operator.  */
14165 static tree
14166 build_incomplete_class_ref (int location, tree class_name)
14167 {
14168   tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14169   tree class_decl = GET_CPC ();
14170   tree this_class = TREE_TYPE (class_decl);
14171
14172   /* Generate the synthetic static method `class$'.  (Previously we
14173      deferred this, causing different method tables to be emitted
14174      for native code and bytecode.)  */
14175   if (!TYPE_DOT_CLASS (this_class)
14176       && !JPRIMITIVE_TYPE_P (class_name)
14177       && !(TREE_CODE (class_name) == VOID_TYPE))
14178     {
14179       tree cpc_list = GET_CPC_LIST();
14180       tree cpc = cpc_list;
14181       tree target_class;
14182
14183       /* For inner classes, add a 'class$' method to their outermost
14184          context, creating it if necessary.  */
14185       
14186       while (GET_NEXT_ENCLOSING_CPC(cpc))
14187         cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14188       class_decl = TREE_VALUE (cpc);
14189
14190       target_class = TREE_TYPE (class_decl);
14191
14192       if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14193         {
14194           /* For interfaces, adding a static 'class$' method directly 
14195              is illegal.  So create an inner class to contain the new
14196              method.  Empirically this matches the behavior of javac.  */
14197           tree t, inner;
14198           /* We want the generated inner class inside the outermost class. */
14199           GET_CPC_LIST() = cpc;
14200           t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14201           inner = create_anonymous_class (t);
14202           target_class = TREE_TYPE (inner);
14203           end_class_declaration (1);
14204           GET_CPC_LIST() = cpc_list;
14205         }
14206
14207       if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14208         build_dot_class_method (target_class);
14209
14210       if (this_class != target_class)
14211         TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14212     }
14213
14214   EXPR_WFL_LINECOL (node) = location;
14215   return node;
14216 }
14217
14218 /* Complete an incomplete class reference operator.  */
14219 static tree
14220 patch_incomplete_class_ref (tree node)
14221 {
14222   tree type = TREE_OPERAND (node, 0);
14223   tree ref_type;
14224
14225   if (!(ref_type = resolve_type_during_patch (type)))
14226     return error_mark_node;
14227
14228   /* If we're not emitting class files and we know ref_type is a
14229      compiled class, build a direct reference.  */
14230   if ((! flag_emit_class_files && is_compiled_class (ref_type))
14231       || JPRIMITIVE_TYPE_P (ref_type)
14232       || TREE_CODE (ref_type) == VOID_TYPE)
14233     {
14234       tree dot = build_class_ref (ref_type);
14235       /* A class referenced by `foo.class' is initialized.  */
14236       if (!flag_emit_class_files)
14237        dot = build_class_init (ref_type, dot);
14238       return java_complete_tree (dot);
14239     }
14240
14241   /* If we're emitting class files and we have to deal with non
14242      primitive types, we invoke the synthetic static method `class$'.  */
14243   ref_type = build_dot_class_method_invocation (current_class, ref_type);
14244   return java_complete_tree (ref_type);
14245 }
14246
14247 /* 15.14 Unary operators. We return error_mark_node in case of error,
14248    but preserve the type of NODE if the type is fixed.  */
14249
14250 static tree
14251 patch_unaryop (tree node, tree wfl_op)
14252 {
14253   tree op = TREE_OPERAND (node, 0);
14254   tree op_type = TREE_TYPE (op);
14255   tree prom_type = NULL_TREE, value, decl;
14256   int nested_field_flag = 0;
14257   int code = TREE_CODE (node);
14258   int error_found = 0;
14259
14260   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14261
14262   switch (code)
14263     {
14264       /* 15.13.2 Postfix Increment Operator ++ */
14265     case POSTINCREMENT_EXPR:
14266       /* 15.13.3 Postfix Increment Operator -- */
14267     case POSTDECREMENT_EXPR:
14268       /* 15.14.1 Prefix Increment Operator ++ */
14269     case PREINCREMENT_EXPR:
14270       /* 15.14.2 Prefix Decrement Operator -- */
14271     case PREDECREMENT_EXPR:
14272       op = decl = extract_field_decl (op);
14273       nested_field_flag
14274         = nested_field_expanded_access_p (op, NULL, NULL, NULL);
14275       /* We might be trying to change an outer field accessed using
14276          access method. */
14277       if (nested_field_flag)
14278         {
14279           /* Retrieve the decl of the field we're trying to access. We
14280              do that by first retrieving the function we would call to
14281              access the field. It has been already verified that this
14282              field isn't final */
14283           if (flag_emit_class_files)
14284             decl = TREE_OPERAND (op, 0);
14285           else
14286             decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14287           decl = DECL_FUNCTION_ACCESS_DECL (decl);
14288         }
14289       /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14290       else if (!JDECL_P (decl)
14291           && TREE_CODE (decl) != COMPONENT_REF
14292           && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14293           && TREE_CODE (decl) != INDIRECT_REF
14294           && !(TREE_CODE (decl) == COMPOUND_EXPR
14295                && TREE_OPERAND (decl, 1)
14296                && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14297         {
14298           TREE_TYPE (node) = error_mark_node;
14299           error_found = 1;
14300         }
14301
14302       /* From now on, we know that op if a variable and that it has a
14303          valid wfl. We use wfl_op to locate errors related to the
14304          ++/-- operand. */
14305       if (!JNUMERIC_TYPE_P (op_type))
14306         {
14307           parse_error_context
14308             (wfl_op, "Invalid argument type %qs to %qs",
14309              lang_printable_name (op_type, 0), operator_string (node));
14310           TREE_TYPE (node) = error_mark_node;
14311           error_found = 1;
14312         }
14313       else
14314         {
14315           /* Before the addition, binary numeric promotion is performed on
14316              both operands, if really necessary */
14317           if (JINTEGRAL_TYPE_P (op_type))
14318             {
14319               value = build_int_cst (op_type, 1);
14320               TREE_TYPE (node) = op_type;
14321             }
14322           else
14323             {
14324               value = build_int_cst (NULL_TREE, 1);
14325               TREE_TYPE (node) =
14326                 binary_numeric_promotion (op_type,
14327                                           TREE_TYPE (value), &op, &value);
14328             }
14329
14330           /* We remember we might be accessing an outer field */
14331           if (nested_field_flag)
14332             {
14333               /* We re-generate an access to the field */
14334               value = build2 (PLUS_EXPR, TREE_TYPE (op),
14335                               build_nested_field_access (wfl_op, decl), value);
14336
14337               /* And we patch the original access$() into a write
14338                  with plus_op as a rhs */
14339               return nested_field_access_fix (node, op, value);
14340             }
14341
14342           /* And write back into the node. */
14343           TREE_OPERAND (node, 0) = op;
14344           TREE_OPERAND (node, 1) = value;
14345           /* Convert the overall back into its original type, if
14346              necessary, and return */
14347           if (JINTEGRAL_TYPE_P (op_type))
14348             return fold (node);
14349           else
14350             return fold (convert (op_type, node));
14351         }
14352       break;
14353
14354       /* 15.14.3 Unary Plus Operator + */
14355     case UNARY_PLUS_EXPR:
14356       /* 15.14.4 Unary Minus Operator - */
14357     case NEGATE_EXPR:
14358       if (!JNUMERIC_TYPE_P (op_type))
14359         {
14360           ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14361           TREE_TYPE (node) = error_mark_node;
14362           error_found = 1;
14363         }
14364       /* Unary numeric promotion is performed on operand */
14365       else
14366         {
14367           op = do_unary_numeric_promotion (op);
14368           prom_type = TREE_TYPE (op);
14369           if (code == UNARY_PLUS_EXPR)
14370             return fold (op);
14371         }
14372       break;
14373
14374       /* 15.14.5 Bitwise Complement Operator ~ */
14375     case BIT_NOT_EXPR:
14376       if (!JINTEGRAL_TYPE_P (op_type))
14377         {
14378           ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14379           TREE_TYPE (node) = error_mark_node;
14380           error_found = 1;
14381         }
14382       else
14383         {
14384           op = do_unary_numeric_promotion (op);
14385           prom_type = TREE_TYPE (op);
14386         }
14387       break;
14388
14389       /* 15.14.6 Logical Complement Operator ! */
14390     case TRUTH_NOT_EXPR:
14391       if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14392         {
14393           ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14394           /* But the type is known. We will report an error if further
14395              attempt of a assignment is made with this rhs */
14396           TREE_TYPE (node) = boolean_type_node;
14397           error_found = 1;
14398         }
14399       else
14400         prom_type = boolean_type_node;
14401       break;
14402
14403       /* 15.15 Cast Expression */
14404     case CONVERT_EXPR:
14405       value = patch_cast (node, wfl_operator);
14406       if (value == error_mark_node)
14407         {
14408           /* If this cast is part of an assignment, we tell the code
14409              that deals with it not to complain about a mismatch,
14410              because things have been cast, anyways */
14411           TREE_TYPE (node) = error_mark_node;
14412           error_found = 1;
14413         }
14414       else
14415         {
14416           value = fold (value);
14417           return value;
14418         }
14419       break;
14420
14421     case NOP_EXPR:
14422       /* This can only happen when the type is already known.  */
14423       gcc_assert (TREE_TYPE (node) != NULL_TREE);
14424       prom_type = TREE_TYPE (node);
14425       break;
14426     }
14427
14428   if (error_found)
14429     return error_mark_node;
14430
14431   /* There are cases where node has been replaced by something else
14432      and we don't end up returning here: UNARY_PLUS_EXPR,
14433      CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14434   TREE_OPERAND (node, 0) = fold (op);
14435   TREE_TYPE (node) = prom_type;
14436   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14437   return fold (node);
14438 }
14439
14440 /* Generic type resolution that sometimes takes place during node
14441    patching. Returned the resolved type or generate an error
14442    message. Return the resolved type or NULL_TREE.  */
14443
14444 static tree
14445 resolve_type_during_patch (tree type)
14446 {
14447   if (unresolved_type_p (type, NULL))
14448     {
14449       tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14450       if (!type_decl)
14451         {
14452           parse_error_context (type,
14453                                "Class %qs not found in type declaration",
14454                                IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14455           return NULL_TREE;
14456         }
14457
14458       check_deprecation (type, type_decl);
14459
14460       return TREE_TYPE (type_decl);
14461     }
14462   return type;
14463 }
14464
14465 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14466    found. Otherwise NODE or something meant to replace it is returned.  */
14467
14468 static tree
14469 patch_cast (tree node, tree wfl_op)
14470 {
14471   tree op = TREE_OPERAND (node, 0);
14472   tree cast_type = TREE_TYPE (node);
14473   tree patched, op_type;
14474   char *t1;
14475
14476   /* Some string patching might be necessary at this stage */
14477   if ((patched = patch_string (op)))
14478     TREE_OPERAND (node, 0) = op = patched;
14479   op_type = TREE_TYPE (op);
14480
14481   /* First resolve OP_TYPE if unresolved */
14482   if (!(cast_type = resolve_type_during_patch (cast_type)))
14483     return error_mark_node;
14484
14485   /* Check on cast that are proven correct at compile time */
14486   if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14487     {
14488       /* Same type */
14489       if (cast_type == op_type)
14490         return node;
14491
14492       /* A narrowing conversion from a floating-point number to an
14493          integral type requires special handling (5.1.3).  */
14494       if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14495         if (cast_type != long_type_node)
14496           op = convert (integer_type_node, op);
14497
14498       /* Try widening/narrowing conversion.  Potentially, things need
14499          to be worked out in gcc so we implement the extreme cases
14500          correctly.  fold_convert() needs to be fixed.  */
14501       return convert (cast_type, op);
14502     }
14503
14504   /* It's also valid to cast a boolean into a boolean */
14505   if (op_type == boolean_type_node && cast_type == boolean_type_node)
14506     return node;
14507
14508   /* null can be casted to references */
14509   if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14510     return build_null_of_type (cast_type);
14511
14512   /* The remaining legal casts involve conversion between reference
14513      types. Check for their compile time correctness. */
14514   if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14515       && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14516     {
14517       TREE_TYPE (node) = promote_type (cast_type);
14518       /* Now, the case can be determined correct at compile time if
14519          OP_TYPE can be converted into CAST_TYPE by assignment
14520          conversion (5.2) */
14521
14522       if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14523         {
14524           TREE_SET_CODE (node, NOP_EXPR);
14525           return node;
14526         }
14527
14528       if (flag_emit_class_files)
14529         {
14530           TREE_SET_CODE (node, CONVERT_EXPR);
14531           return node;
14532         }
14533
14534       /* The cast requires a run-time check */
14535       return build3 (CALL_EXPR, promote_type (cast_type),
14536                      build_address_of (soft_checkcast_node),
14537                      tree_cons (NULL_TREE, build_class_ref (cast_type),
14538                                 build_tree_list (NULL_TREE, op)),
14539                      NULL_TREE);
14540     }
14541
14542   /* Any other casts are proven incorrect at compile time */
14543   t1 = xstrdup (lang_printable_name (op_type, 0));
14544   parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14545                        t1, lang_printable_name (cast_type, 0));
14546   free (t1);
14547   return error_mark_node;
14548 }
14549
14550 /* Build a null constant and give it the type TYPE.  */
14551
14552 static tree
14553 build_null_of_type (tree type)
14554 {
14555   tree node = build_int_cst (promote_type (type), 0);
14556   return node;
14557 }
14558
14559 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14560    a list of indices. */
14561 static tree
14562 build_array_ref (int location, tree array, tree index)
14563 {
14564   tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14565                       NULL_TREE, NULL_TREE);
14566   EXPR_WFL_LINECOL (node) = location;
14567   return node;
14568 }
14569
14570 /* 15.12 Array Access Expression */
14571
14572 static tree
14573 patch_array_ref (tree node)
14574 {
14575   tree array = TREE_OPERAND (node, 0);
14576   tree array_type  = TREE_TYPE (array);
14577   tree index = TREE_OPERAND (node, 1);
14578   tree index_type = TREE_TYPE (index);
14579   int error_found = 0;
14580
14581   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14582
14583   if (TREE_CODE (array_type) == POINTER_TYPE)
14584     array_type = TREE_TYPE (array_type);
14585
14586   /* The array reference must be an array */
14587   if (!TYPE_ARRAY_P (array_type))
14588     {
14589       parse_error_context
14590         (wfl_operator,
14591          "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14592          lang_printable_name (array_type, 0));
14593       TREE_TYPE (node) = error_mark_node;
14594       error_found = 1;
14595     }
14596
14597   /* The array index undergoes unary numeric promotion. The promoted
14598      type must be int */
14599   index = do_unary_numeric_promotion (index);
14600   if (TREE_TYPE (index) != int_type_node)
14601     {
14602       if (valid_cast_to_p (index_type, int_type_node))
14603         parse_error_context (wfl_operator,
14604    "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14605                              lang_printable_name (index_type, 0));
14606       else
14607         parse_error_context (wfl_operator,
14608           "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14609                              lang_printable_name (index_type, 0));
14610       TREE_TYPE (node) = error_mark_node;
14611       error_found = 1;
14612     }
14613
14614   if (error_found)
14615     return error_mark_node;
14616
14617   array_type = TYPE_ARRAY_ELEMENT (array_type);
14618
14619   if (flag_emit_class_files)
14620     {
14621       TREE_OPERAND (node, 0) = array;
14622       TREE_OPERAND (node, 1) = index;
14623     }
14624   else
14625     node = build_java_arrayaccess (array, array_type, index);
14626   TREE_TYPE (node) = array_type;
14627   return node;
14628 }
14629
14630 /* 15.9 Array Creation Expressions */
14631
14632 static tree
14633 build_newarray_node (tree type, tree dims, int extra_dims)
14634 {
14635   tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14636                       nreverse (dims),
14637                       build_int_cst (NULL_TREE, extra_dims));
14638   return node;
14639 }
14640
14641 static tree
14642 patch_newarray (tree node)
14643 {
14644   tree type = TREE_OPERAND (node, 0);
14645   tree dims = TREE_OPERAND (node, 1);
14646   tree cdim, array_type;
14647   int error_found = 0;
14648   int ndims = 0;
14649   int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14650
14651   /* Dimension types are verified. It's better for the types to be
14652      verified in order. */
14653   for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14654     {
14655       int dim_error = 0;
14656       tree dim = TREE_VALUE (cdim);
14657
14658       /* Dim might have been saved during its evaluation */
14659       dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14660
14661       /* The type of each specified dimension must be an integral type. */
14662       if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14663         dim_error = 1;
14664
14665       /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14666          promoted type must be int. */
14667       else
14668         {
14669           dim = do_unary_numeric_promotion (dim);
14670           if (TREE_TYPE (dim) != int_type_node)
14671             dim_error = 1;
14672         }
14673
14674       /* Report errors on types here */
14675       if (dim_error)
14676         {
14677           parse_error_context
14678             (TREE_PURPOSE (cdim),
14679              "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14680              (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14681               "Explicit cast needed to" : "Can't"),
14682              lang_printable_name (TREE_TYPE (dim), 0));
14683           error_found = 1;
14684         }
14685
14686       TREE_PURPOSE (cdim) = NULL_TREE;
14687     }
14688
14689   /* Resolve array base type if unresolved */
14690   if (!(type = resolve_type_during_patch (type)))
14691     error_found = 1;
14692
14693   if (error_found)
14694     {
14695       /* We don't want further evaluation of this bogus array creation
14696          operation */
14697       TREE_TYPE (node) = error_mark_node;
14698       return error_mark_node;
14699     }
14700
14701   /* Set array_type to the actual (promoted) array type of the result. */
14702   if (TREE_CODE (type) == RECORD_TYPE)
14703     type = build_pointer_type (type);
14704   while (--xdims >= 0)
14705     {
14706       type = promote_type (build_java_array_type (type, -1));
14707     }
14708   dims = nreverse (dims);
14709   array_type = type;
14710   for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14711     {
14712       type = array_type;
14713       array_type
14714         = build_java_array_type (type,
14715                                  TREE_CODE (cdim) == INTEGER_CST
14716                                  ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14717                                  : -1);
14718       array_type = promote_type (array_type);
14719     }
14720   dims = nreverse (dims);
14721
14722   /* The node is transformed into a function call. Things are done
14723      differently according to the number of dimensions. If the number
14724      of dimension is equal to 1, then the nature of the base type
14725      (primitive or not) matters. */
14726   if (ndims == 1)
14727     return build_new_array (type, TREE_VALUE (dims));
14728
14729   /* Can't reuse what's already written in expr.c because it uses the
14730      JVM stack representation. Provide a build_multianewarray. FIXME */
14731   return build3 (CALL_EXPR, array_type,
14732                  build_address_of (soft_multianewarray_node),
14733                  tree_cons (NULL_TREE,
14734                             build_class_ref (TREE_TYPE (array_type)),
14735                             tree_cons (NULL_TREE,
14736                                        build_int_cst (NULL_TREE, ndims),
14737                                        dims)),
14738                  NULL_TREE);
14739 }
14740
14741 /* 10.6 Array initializer.  */
14742
14743 /* Build a wfl for array element that don't have one, so we can
14744    pin-point errors.  */
14745
14746 static tree
14747 maybe_build_array_element_wfl (tree node)
14748 {
14749   if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14750     {
14751       /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14752       return build_expr_wfl (NULL_TREE,
14753 #ifdef USE_MAPPED_LOCATION
14754                              input_location
14755 #else
14756                              ctxp->filename,
14757                              ctxp->lexer->token_start.line,
14758                              ctxp->lexer->token_start.col
14759 #endif
14760                              );
14761     }
14762   else
14763     return NULL_TREE;
14764 }
14765
14766 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14767    identification of initialized arrays easier to detect during walk
14768    and expansion.  */
14769
14770 static tree
14771 build_new_array_init (int location, tree values)
14772 {
14773   tree constructor = build_constructor (NULL_TREE, values);
14774   tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14775   EXPR_WFL_LINECOL (to_return) = location;
14776   return to_return;
14777 }
14778
14779 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14780    occurred.  Otherwise return NODE after having set its type
14781    appropriately.  */
14782
14783 static tree
14784 patch_new_array_init (tree type, tree node)
14785 {
14786   int error_seen = 0;
14787   tree current, element_type;
14788   HOST_WIDE_INT length;
14789   int all_constant = 1;
14790   tree init = TREE_OPERAND (node, 0);
14791
14792   if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14793     {
14794       parse_error_context (node,
14795                            "Invalid array initializer for non-array type %qs",
14796                            lang_printable_name (type, 1));
14797       return error_mark_node;
14798     }
14799   type = TREE_TYPE (type);
14800   element_type = TYPE_ARRAY_ELEMENT (type);
14801
14802   CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14803
14804   for (length = 0, current = CONSTRUCTOR_ELTS (init);
14805        current;  length++, current = TREE_CHAIN (current))
14806     {
14807       tree elt = TREE_VALUE (current);
14808       if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14809         {
14810           error_seen |= array_constructor_check_entry (element_type, current);
14811           elt = TREE_VALUE (current);
14812           /* When compiling to native code, STRING_CST is converted to
14813              INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14814           if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14815             all_constant = 0;
14816         }
14817       else
14818         {
14819           TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14820           TREE_PURPOSE (current) = NULL_TREE;
14821           all_constant = 0;
14822         }
14823       if (elt && TREE_CODE (elt) == TREE_LIST
14824           && TREE_VALUE (elt) == error_mark_node)
14825         error_seen = 1;
14826     }
14827
14828   if (error_seen)
14829     return error_mark_node;
14830
14831   /* Create a new type. We can't reuse the one we have here by
14832      patching its dimension because it originally is of dimension -1
14833      hence reused by gcc. This would prevent triangular arrays. */
14834   type = build_java_array_type (element_type, length);
14835   TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14836   TREE_TYPE (node) = promote_type (type);
14837   TREE_CONSTANT (init) = all_constant;
14838   TREE_INVARIANT (init) = all_constant;
14839   TREE_CONSTANT (node) = all_constant;
14840   TREE_INVARIANT (node) = all_constant;
14841   return node;
14842 }
14843
14844 /* Verify that one entry of the initializer element list can be
14845    assigned to the array base type. Report 1 if an error occurred, 0
14846    otherwise.  */
14847
14848 static int
14849 array_constructor_check_entry (tree type, tree entry)
14850 {
14851   char *array_type_string = NULL;       /* For error reports */
14852   tree value, type_value, new_value, wfl_value, patched;
14853   int error_seen = 0;
14854
14855   new_value = NULL_TREE;
14856   wfl_value = TREE_VALUE (entry);
14857
14858   value = java_complete_tree (TREE_VALUE (entry));
14859   /* patch_string return error_mark_node if arg is error_mark_node */
14860   if ((patched = patch_string (value)))
14861     value = patched;
14862   if (value == error_mark_node)
14863     return 1;
14864
14865   type_value = TREE_TYPE (value);
14866
14867   /* At anytime, try_builtin_assignconv can report a warning on
14868      constant overflow during narrowing. */
14869   SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14870   new_value = try_builtin_assignconv (wfl_operator, type, value);
14871   if (!new_value && (new_value = try_reference_assignconv (type, value)))
14872     type_value = promote_type (type);
14873
14874   /* Check and report errors */
14875   if (!new_value)
14876     {
14877       const char *const msg = (!valid_cast_to_p (type_value, type) ?
14878                    "Can't" : "Explicit cast needed to");
14879       if (!array_type_string)
14880         array_type_string = xstrdup (lang_printable_name (type, 1));
14881       parse_error_context
14882         (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14883          msg, lang_printable_name (type_value, 1), array_type_string);
14884       error_seen = 1;
14885     }
14886
14887   if (new_value)
14888     TREE_VALUE (entry) = new_value;
14889
14890   if (array_type_string)
14891     free (array_type_string);
14892
14893   TREE_PURPOSE (entry) = NULL_TREE;
14894   return error_seen;
14895 }
14896
14897 static tree
14898 build_this (int location)
14899 {
14900   tree node = build_wfl_node (this_identifier_node);
14901   TREE_SET_CODE (node, THIS_EXPR);
14902   EXPR_WFL_LINECOL (node) = location;
14903   return node;
14904 }
14905
14906 /* 14.15 The return statement. It builds a modify expression that
14907    assigns the returned value to the RESULT_DECL that hold the value
14908    to be returned. */
14909
14910 static tree
14911 build_return (int location, tree op)
14912 {
14913   tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14914   EXPR_WFL_LINECOL (node) = location;
14915   node = build_debugable_stmt (location, node);
14916   return node;
14917 }
14918
14919 static tree
14920 patch_return (tree node)
14921 {
14922   tree return_exp = TREE_OPERAND (node, 0);
14923   tree meth = current_function_decl;
14924   tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14925   int error_found = 0;
14926
14927   TREE_TYPE (node) = error_mark_node;
14928   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14929
14930   /* It's invalid to have a return value within a function that is
14931      declared with the keyword void or that is a constructor */
14932   if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14933     error_found = 1;
14934
14935   /* It's invalid to use a return statement in a static block */
14936   if (DECL_CLINIT_P (current_function_decl))
14937     error_found = 1;
14938
14939   /* It's invalid to have a no return value within a function that
14940      isn't declared with the keyword `void' */
14941   if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14942     error_found = 2;
14943
14944   if (DECL_INSTINIT_P (current_function_decl))
14945     error_found = 1;
14946
14947   if (error_found)
14948     {
14949       if (DECL_INSTINIT_P (current_function_decl))
14950         parse_error_context (wfl_operator,
14951                              "%<return%> inside instance initializer");
14952
14953       else if (DECL_CLINIT_P (current_function_decl))
14954         parse_error_context (wfl_operator,
14955                              "%<return%> inside static initializer");
14956
14957       else if (!DECL_CONSTRUCTOR_P (meth))
14958         {
14959           char *t = xstrdup (lang_printable_name (mtype, 0));
14960           parse_error_context (wfl_operator,
14961                                "%<return%> with%s value from %<%s %s%>",
14962                                (error_found == 1 ? "" : "out"),
14963                                t, lang_printable_name (meth, 2));
14964           free (t);
14965         }
14966       else
14967         parse_error_context (wfl_operator,
14968                              "%<return%> with value from constructor %qs",
14969                              lang_printable_name (meth, 2));
14970       return error_mark_node;
14971     }
14972
14973   /* If we have a return_exp, build a modify expression and expand
14974      it. Note: at that point, the assignment is declared valid, but we
14975      may want to carry some more hacks */
14976   if (return_exp)
14977     {
14978       tree exp = java_complete_tree (return_exp);
14979       tree modify, patched;
14980
14981       if ((patched = patch_string (exp)))
14982         exp = patched;
14983
14984       modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14985       EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14986       modify = java_complete_tree (modify);
14987
14988       if (modify != error_mark_node)
14989         {
14990           TREE_SIDE_EFFECTS (modify) = 1;
14991           TREE_OPERAND (node, 0) = modify;
14992         }
14993       else
14994         return error_mark_node;
14995     }
14996   TREE_TYPE (node) = void_type_node;
14997   TREE_SIDE_EFFECTS (node) = 1;
14998   return node;
14999 }
15000
15001 /* 14.8 The if Statement */
15002
15003 static tree
15004 build_if_else_statement (int location, tree expression, tree if_body,
15005                          tree else_body)
15006 {
15007   tree node;
15008   if (!else_body)
15009     else_body = build_java_empty_stmt ();
15010   node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
15011   EXPR_WFL_LINECOL (node) = location;
15012   node = build_debugable_stmt (location, node);
15013   return node;
15014 }
15015
15016 static tree
15017 patch_if_else_statement (tree node)
15018 {
15019   tree expression = TREE_OPERAND (node, 0);
15020   int can_complete_normally
15021     = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15022        | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15023
15024   TREE_TYPE (node) = error_mark_node;
15025   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15026
15027   /* The type of expression must be boolean */
15028   if (TREE_TYPE (expression) != boolean_type_node
15029       && TREE_TYPE (expression) != promoted_boolean_type_node)
15030     {
15031       parse_error_context
15032         (wfl_operator,
15033          "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
15034          lang_printable_name (TREE_TYPE (expression), 0));
15035       return error_mark_node;
15036     }
15037
15038   TREE_TYPE (node) = void_type_node;
15039   TREE_SIDE_EFFECTS (node) = 1;
15040   CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15041   return node;
15042 }
15043
15044 /* 14.6 Labeled Statements */
15045
15046 /* Action taken when a labeled statement is parsed. a new
15047    LABELED_BLOCK_EXPR is created. No statement is attached to the
15048    label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
15049
15050 static tree
15051 build_labeled_block (int location, tree label)
15052 {
15053   tree label_name ;
15054   tree label_decl, node;
15055   if (label == NULL_TREE || label == continue_identifier_node)
15056     label_name = label;
15057   else
15058     {
15059       label_name = merge_qualified_name (label_id, label);
15060       /* Issue an error if we try to reuse a label that was previously
15061          declared */
15062       if (IDENTIFIER_LOCAL_VALUE (label_name))
15063         {
15064           EXPR_WFL_LINECOL (wfl_operator) = location;
15065           parse_error_context (wfl_operator,
15066             "Declaration of %qs shadows a previous label declaration",
15067                                IDENTIFIER_POINTER (label));
15068           EXPR_WFL_LINECOL (wfl_operator) =
15069             EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15070           parse_error_context (wfl_operator,
15071             "This is the location of the previous declaration of label %qs",
15072                                IDENTIFIER_POINTER (label));
15073           java_error_count--;
15074         }
15075     }
15076
15077   label_decl = create_label_decl (label_name);
15078   node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15079   EXPR_WFL_LINECOL (node) = location;
15080   TREE_SIDE_EFFECTS (node) = 1;
15081   return node;
15082 }
15083
15084 /* A labeled statement LBE is attached a statement.  */
15085
15086 static tree
15087 finish_labeled_statement (tree lbe, /* Labeled block expr */
15088                           tree statement)
15089 {
15090   /* In anyways, tie the loop to its statement */
15091   LABELED_BLOCK_BODY (lbe) = statement;
15092   pop_labeled_block ();
15093   POP_LABELED_BLOCK ();
15094   return lbe;
15095 }
15096
15097 /* 14.10, 14.11, 14.12 Loop Statements */
15098
15099 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15100    list. */
15101
15102 static tree
15103 build_new_loop (tree loop_body)
15104 {
15105   tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15106   TREE_SIDE_EFFECTS (loop) = 1;
15107   PUSH_LOOP (loop);
15108   return loop;
15109 }
15110
15111 /* Create a loop body according to the following structure:
15112      COMPOUND_EXPR
15113        COMPOUND_EXPR            (loop main body)
15114          EXIT_EXPR              (this order is for while/for loops.
15115          LABELED_BLOCK_EXPR      the order is reversed for do loops)
15116            LABEL_DECL           (a continue occurring here branches at the
15117            BODY                  end of this labeled block)
15118        INCREMENT                (if any)
15119
15120   REVERSED, if nonzero, tells that the loop condition expr comes
15121   after the body, like in the do-while loop.
15122
15123   To obtain a loop, the loop body structure described above is
15124   encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15125
15126    LABELED_BLOCK_EXPR
15127      LABEL_DECL                   (use this label to exit the loop)
15128      LOOP_EXPR
15129        <structure described above> */
15130
15131 static tree
15132 build_loop_body (int location, tree condition, int reversed)
15133 {
15134   tree first, second, body;
15135
15136   condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15137   EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15138   condition = build_debugable_stmt (location, condition);
15139   TREE_SIDE_EFFECTS (condition) = 1;
15140
15141   body = build_labeled_block (0, continue_identifier_node);
15142   first = (reversed ? body : condition);
15143   second = (reversed ? condition : body);
15144   return build2 (COMPOUND_EXPR, NULL_TREE,
15145                  build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15146                  build_java_empty_stmt ());
15147 }
15148
15149 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15150    their order) on the current loop. Unlink the current loop from the
15151    loop list.  */
15152
15153 static tree
15154 finish_loop_body (int location, tree condition, tree body, int reversed)
15155 {
15156   tree to_return = ctxp->current_loop;
15157   tree loop_body = LOOP_EXPR_BODY (to_return);
15158   if (condition)
15159     {
15160       tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15161       /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15162          The real EXIT_EXPR is one operand further. */
15163       EXPR_WFL_LINECOL (cnode) = location;
15164       if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15165         {
15166           cnode = EXPR_WFL_NODE (cnode);
15167           /* This one is for accurate error reports */
15168           EXPR_WFL_LINECOL (cnode) = location;
15169         }
15170       TREE_OPERAND (cnode, 0) = condition;
15171     }
15172   LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15173   POP_LOOP ();
15174   return to_return;
15175 }
15176
15177 /* Tailored version of finish_loop_body for FOR loops, when FOR
15178    loops feature the condition part */
15179
15180 static tree
15181 finish_for_loop (int location, tree condition, tree update, tree body)
15182 {
15183   /* Put the condition and the loop body in place */
15184   tree loop = finish_loop_body (location, condition, body, 0);
15185   /* LOOP is the current loop which has been now popped of the loop
15186      stack.  Mark the update block as reachable and install it.  We do
15187      this because the (current interpretation of the) JLS requires
15188      that the update expression be considered reachable even if the
15189      for loop's body doesn't complete normally.  */
15190   if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15191     {
15192       tree up2 = update;
15193       if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15194         up2 = EXPR_WFL_NODE (up2);
15195       /* It is possible for the update expression to be an
15196          EXPR_WFL_NODE wrapping nothing.  */
15197       if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15198         {
15199           /* Try to detect constraint violations.  These would be
15200              programming errors somewhere.  */
15201           if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15202             abort ();
15203           SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15204         }
15205     }
15206   LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15207   return loop;
15208 }
15209
15210 /* Try to find the loop a block might be related to. This comprises
15211    the case where the LOOP_EXPR is found as the second operand of a
15212    COMPOUND_EXPR, because the loop happens to have an initialization
15213    part, then expressed as the first operand of the COMPOUND_EXPR. If
15214    the search finds something, 1 is returned. Otherwise, 0 is
15215    returned. The search is assumed to start from a
15216    LABELED_BLOCK_EXPR's block.  */
15217
15218 static tree
15219 search_loop (tree statement)
15220 {
15221   if (TREE_CODE (statement) == LOOP_EXPR)
15222     return statement;
15223
15224   if (TREE_CODE (statement) == BLOCK)
15225     statement = BLOCK_SUBBLOCKS (statement);
15226   else
15227     return NULL_TREE;
15228
15229   if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15230     while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15231       statement = TREE_OPERAND (statement, 1);
15232
15233   return (TREE_CODE (statement) == LOOP_EXPR
15234           && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15235 }
15236
15237 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15238    returned otherwise.  */
15239
15240 static int
15241 labeled_block_contains_loop_p (tree block, tree loop)
15242 {
15243   if (!block)
15244     return 0;
15245
15246   if (LABELED_BLOCK_BODY (block) == loop)
15247     return 1;
15248
15249   if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15250     return 1;
15251
15252   return 0;
15253 }
15254
15255 /* If the loop isn't surrounded by a labeled statement, create one and
15256    insert LOOP as its body.  */
15257
15258 static tree
15259 patch_loop_statement (tree loop)
15260 {
15261   tree loop_label;
15262
15263   TREE_TYPE (loop) = void_type_node;
15264   if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15265     return loop;
15266
15267   loop_label = build_labeled_block (0, NULL_TREE);
15268   /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15269      that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15270   LABELED_BLOCK_BODY (loop_label) = loop;
15271   PUSH_LABELED_BLOCK (loop_label);
15272   return loop_label;
15273 }
15274
15275 /* 14.13, 14.14: break and continue Statements */
15276
15277 /* Build a break or a continue statement. a null NAME indicates an
15278    unlabeled break/continue statement.  */
15279
15280 static tree
15281 build_bc_statement (int location, int is_break, tree name)
15282 {
15283   tree break_continue, label_block_expr = NULL_TREE;
15284
15285   if (name)
15286     {
15287       if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15288             (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15289         /* Null means that we don't have a target for this named
15290            break/continue. In this case, we make the target to be the
15291            label name, so that the error can be reported accurately in
15292            patch_bc_statement. */
15293         label_block_expr = EXPR_WFL_NODE (name);
15294     }
15295   /* Unlabeled break/continue will be handled during the
15296      break/continue patch operation */
15297   break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15298
15299   IS_BREAK_STMT_P (break_continue) = is_break;
15300   TREE_SIDE_EFFECTS (break_continue) = 1;
15301   EXPR_WFL_LINECOL (break_continue) = location;
15302   break_continue = build_debugable_stmt (location, break_continue);
15303   return break_continue;
15304 }
15305
15306 /* Verification of a break/continue statement. */
15307
15308 static tree
15309 patch_bc_statement (tree node)
15310 {
15311   tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15312   tree labeled_block = ctxp->current_labeled_block;
15313   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15314
15315   /* Having an identifier here means that the target is unknown. */
15316   if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15317     {
15318       parse_error_context (wfl_operator, "No label definition found for %qs",
15319                            IDENTIFIER_POINTER (bc_label));
15320       return error_mark_node;
15321     }
15322   if (! IS_BREAK_STMT_P (node))
15323     {
15324       /* It's a continue statement. */
15325       for (;; labeled_block = TREE_CHAIN (labeled_block))
15326         {
15327           if (labeled_block == NULL_TREE)
15328             {
15329               if (bc_label == NULL_TREE)
15330                 parse_error_context (wfl_operator,
15331                                      "%<continue%> must be in loop");
15332               else
15333                 parse_error_context
15334                   (wfl_operator, "continue label %qs does not name a loop",
15335                    IDENTIFIER_POINTER (bc_label));
15336               return error_mark_node;
15337             }
15338           if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15339                == continue_identifier_node)
15340               && (bc_label == NULL_TREE
15341                   || TREE_CHAIN (labeled_block) == bc_label))
15342             {
15343               bc_label = labeled_block;
15344               break;
15345             }
15346         }
15347     }
15348   else if (!bc_label)
15349     {
15350       for (;; labeled_block = TREE_CHAIN (labeled_block))
15351         {
15352           if (labeled_block == NULL_TREE)
15353             {
15354               parse_error_context (wfl_operator,
15355                                      "%<break%> must be in loop or switch");
15356               return error_mark_node;
15357             }
15358           target_stmt = LABELED_BLOCK_BODY (labeled_block);
15359           if (TREE_CODE (target_stmt) == SWITCH_EXPR
15360               || search_loop (target_stmt))
15361             {
15362               bc_label = labeled_block;
15363               break;
15364             }
15365         }
15366     }
15367
15368   EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15369   CAN_COMPLETE_NORMALLY (bc_label) = 1;
15370
15371   /* Our break/continue don't return values. */
15372   TREE_TYPE (node) = void_type_node;
15373   /* Encapsulate the break within a compound statement so that it's
15374      expanded all the times by expand_expr (and not clobbered
15375      sometimes, like after a if statement) */
15376   node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15377   TREE_SIDE_EFFECTS (node) = 1;
15378   return node;
15379 }
15380
15381 /* Process the exit expression belonging to a loop. Its type must be
15382    boolean.  */
15383
15384 static tree
15385 patch_exit_expr (tree node)
15386 {
15387   tree expression = TREE_OPERAND (node, 0);
15388   TREE_TYPE (node) = error_mark_node;
15389   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15390
15391   /* The type of expression must be boolean */
15392   if (TREE_TYPE (expression) != boolean_type_node)
15393     {
15394       parse_error_context
15395         (wfl_operator,
15396     "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15397          lang_printable_name (TREE_TYPE (expression), 0));
15398       return error_mark_node;
15399     }
15400   /* Now we know things are allright, invert the condition, fold and
15401      return */
15402   TREE_OPERAND (node, 0) =
15403     fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15404
15405   if (! integer_zerop (TREE_OPERAND (node, 0))
15406       && ctxp->current_loop != NULL_TREE
15407       && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15408     CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15409   if (! integer_onep (TREE_OPERAND (node, 0)))
15410     CAN_COMPLETE_NORMALLY (node) = 1;
15411
15412
15413   TREE_TYPE (node) = void_type_node;
15414   return node;
15415 }
15416
15417 /* 14.9 Switch statement */
15418
15419 static tree
15420 patch_switch_statement (tree node)
15421 {
15422   tree se = TREE_OPERAND (node, 0), se_type;
15423   tree save, iter;
15424
15425   /* Complete the switch expression */
15426   se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15427   se_type = TREE_TYPE (se);
15428   /* The type of the switch expression must be char, byte, short or
15429      int */
15430   if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15431     {
15432       EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15433       parse_error_context (wfl_operator,
15434           "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15435                            lang_printable_name (se_type, 0));
15436       /* This is what java_complete_tree will check */
15437       TREE_OPERAND (node, 0) = error_mark_node;
15438       return error_mark_node;
15439     }
15440
15441   /* Save and restore the outer case label list.  */
15442   save = case_label_list;
15443   case_label_list = NULL_TREE;
15444
15445   TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15446
15447   /* See if we've found a duplicate label.  We can't leave this until
15448      code generation, because in `--syntax-only' and `-C' modes we
15449      don't do ordinary code generation.  */
15450   for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15451     {
15452       HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15453       tree subiter;
15454       for (subiter = TREE_CHAIN (iter);
15455            subiter != NULL_TREE;
15456            subiter = TREE_CHAIN (subiter))
15457         {
15458           HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15459           if (val == subval)
15460             {
15461               EXPR_WFL_LINECOL (wfl_operator)
15462                 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15463               /* The case_label_list is in reverse order, so print the
15464                  outer label first.  */
15465               parse_error_context (wfl_operator, "duplicate case label: %<"
15466                                    HOST_WIDE_INT_PRINT_DEC "%>", subval);
15467               EXPR_WFL_LINECOL (wfl_operator)
15468                 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15469               parse_error_context (wfl_operator, "original label is here");
15470
15471               break;
15472             }
15473         }
15474     }
15475
15476   case_label_list = save;
15477
15478   /* Ready to return */
15479   if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15480     {
15481       TREE_TYPE (node) = error_mark_node;
15482       return error_mark_node;
15483     }
15484   TREE_TYPE (node) = void_type_node;
15485   TREE_SIDE_EFFECTS (node) = 1;
15486   CAN_COMPLETE_NORMALLY (node)
15487     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15488       || ! SWITCH_HAS_DEFAULT (node);
15489   return node;
15490 }
15491
15492 /* Assertions.  */
15493
15494 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15495    might be NULL_TREE.  */
15496 static tree
15497 build_assertion (
15498 #ifdef USE_MAPPED_LOCATION
15499                  source_location location,
15500 #else
15501                  int location,
15502 #endif
15503                  tree condition, tree value)
15504 {
15505   tree node;
15506   tree klass = GET_CPC ();
15507
15508   if (! enable_assertions (klass))
15509     {
15510       condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15511                           boolean_false_node, condition);
15512       if (value == NULL_TREE)
15513         value = build_java_empty_stmt ();
15514       return build_if_else_statement (location, condition,
15515                                       value, NULL_TREE);
15516     }
15517
15518   if (! CLASS_USES_ASSERTIONS (klass))
15519     {
15520       tree field, classdollar, id, call;
15521       tree class_type = TREE_TYPE (klass);
15522
15523       field = add_field (class_type,
15524                          get_identifier ("$assertionsDisabled"),
15525                          boolean_type_node,
15526                          ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15527       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15528       FIELD_SYNTHETIC (field) = 1;
15529
15530       classdollar = build_incomplete_class_ref (location, class_type);
15531
15532       /* Call CLASS.desiredAssertionStatus().  */
15533       id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15534       call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15535       call = make_qualified_primary (classdollar, call, location);
15536       TREE_SIDE_EFFECTS (call) = 1;
15537
15538       /* Invert to obtain !CLASS.desiredAssertionStatus().  This may
15539          seem odd, but we do it to generate code identical to that of
15540          the JDK.  */
15541       call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15542       TREE_SIDE_EFFECTS (call) = 1;
15543       DECL_INITIAL (field) = call;
15544
15545       /* Record the initializer in the initializer statement list.  */
15546       call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15547       TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15548       SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15549       MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15550
15551       CLASS_USES_ASSERTIONS (klass) = 1;
15552     }
15553
15554   if (value != NULL_TREE)
15555     value = tree_cons (NULL_TREE, value, NULL_TREE);
15556
15557   node = build_wfl_node (get_identifier ("java"));
15558   node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15559                               location);
15560   node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15561                               location);
15562
15563   node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15564   TREE_SIDE_EFFECTS (node) = 1;
15565   /* It is too early to use BUILD_THROW.  */
15566   node = build1 (THROW_EXPR, NULL_TREE, node);
15567   TREE_SIDE_EFFECTS (node) = 1;
15568
15569   /* We invert the condition; if we just put NODE as the `else' part
15570      then we generate weird-looking bytecode.  */
15571   condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15572   /* Check $assertionsDisabled.  */
15573   condition
15574     = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15575               build1 (TRUTH_NOT_EXPR, NULL_TREE,
15576                       build_wfl_node (get_identifier ("$assertionsDisabled"))),
15577               condition);
15578   node = build_if_else_statement (location, condition, node, NULL_TREE);
15579   return node;
15580 }
15581
15582 /* 14.18 The try/catch statements */
15583
15584 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15585    catches TYPE and executes CATCH_STMTS.  */
15586
15587 static tree
15588 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15589                             tree catch_stmts)
15590 {
15591   tree try_block, catch_clause_param, catch_block, catch;
15592
15593   /* First build a try block */
15594   try_block = build_expr_block (try_stmts, NULL_TREE);
15595
15596   /* Build a catch block: we need a catch clause parameter */
15597   if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15598     {
15599       tree catch_type = obtain_incomplete_type (type_or_name);
15600       jdep *dep;
15601       catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15602       register_incomplete_type (JDEP_VARIABLE, type_or_name,
15603                                 catch_clause_param, catch_type);
15604       dep = CLASSD_LAST (ctxp->classd_list);
15605       JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15606     }
15607   else
15608     catch_clause_param = build_decl (VAR_DECL, wpv_id,
15609                                      build_pointer_type (type_or_name));
15610
15611   /* And a block */
15612   catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15613
15614   /* Initialize the variable and store in the block */
15615   catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15616                   build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15617   add_stmt_to_block (catch_block, NULL_TREE, catch);
15618
15619   /* Add the catch statements */
15620   add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15621
15622   /* Now we can build a JAVA_CATCH_EXPR */
15623   catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15624
15625   return build_try_statement (location, try_block, catch_block);
15626 }
15627
15628 static tree
15629 build_try_statement (int location, tree try_block, tree catches)
15630 {
15631   tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15632   EXPR_WFL_LINECOL (node) = location;
15633   return node;
15634 }
15635
15636 static tree
15637 build_try_finally_statement (int location, tree try_block, tree finally)
15638 {
15639   tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15640   EXPR_WFL_LINECOL (node) = location;
15641   return node;
15642 }
15643
15644 static tree
15645 patch_try_statement (tree node)
15646 {
15647   int error_found = 0;
15648   tree try = TREE_OPERAND (node, 0);
15649   /* Exception handlers are considered in left to right order */
15650   tree catch = nreverse (TREE_OPERAND (node, 1));
15651   tree current, caught_type_list = NULL_TREE;
15652
15653   /* Check catch clauses, if any. Every time we find an error, we try
15654      to process the next catch clause. We process the catch clause before
15655      the try block so that when processing the try block we can check thrown
15656      exceptions against the caught type list. */
15657   for (current = catch; current; current = TREE_CHAIN (current))
15658     {
15659       tree carg_decl, carg_type;
15660       tree sub_current, catch_block, catch_clause;
15661       int unreachable;
15662
15663       /* At this point, the structure of the catch clause is
15664            JAVA_CATCH_EXPR              (catch node)
15665              BLOCK              (with the decl of the parameter)
15666                COMPOUND_EXPR
15667                  MODIFY_EXPR   (assignment of the catch parameter)
15668                  BLOCK          (catch clause block)
15669        */
15670       catch_clause = TREE_OPERAND (current, 0);
15671       carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15672       carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15673
15674       /* Catch clauses can't have more than one parameter declared,
15675          but it's already enforced by the grammar. Make sure that the
15676          only parameter of the clause statement in of class Throwable
15677          or a subclass of Throwable, but that was done earlier. The
15678          catch clause parameter type has also been resolved. */
15679
15680       /* Just make sure that the catch clause parameter type inherits
15681          from java.lang.Throwable */
15682       if (!inherits_from_p (carg_type, throwable_type_node))
15683         {
15684           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15685           parse_error_context (wfl_operator,
15686                                "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15687                                lang_printable_name (carg_type, 0));
15688           error_found = 1;
15689           continue;
15690         }
15691
15692       /* Partial check for unreachable catch statement: The catch
15693          clause is reachable iff is no earlier catch block A in
15694          the try statement such that the type of the catch
15695          clause's parameter is the same as or a subclass of the
15696          type of A's parameter */
15697       unreachable = 0;
15698       for (sub_current = catch;
15699            sub_current != current; sub_current = TREE_CHAIN (sub_current))
15700         {
15701           tree sub_catch_clause, decl;
15702           sub_catch_clause = TREE_OPERAND (sub_current, 0);
15703           decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15704
15705           if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15706             {
15707               EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15708               parse_error_context
15709                 (wfl_operator,
15710                  "%<catch%> not reached because of the catch clause at line %d",
15711                  EXPR_WFL_LINENO (sub_current));
15712               unreachable = error_found = 1;
15713               break;
15714             }
15715         }
15716       /* Complete the catch clause block */
15717       catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15718       if (catch_block == error_mark_node)
15719         {
15720           error_found = 1;
15721           continue;
15722         }
15723       if (CAN_COMPLETE_NORMALLY (catch_block))
15724         CAN_COMPLETE_NORMALLY (node) = 1;
15725       TREE_OPERAND (current, 0) = catch_block;
15726
15727       if (unreachable)
15728         continue;
15729
15730       /* Things to do here: the exception must be thrown */
15731
15732       /* Link this type to the caught type list */
15733       caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15734     }
15735
15736   PUSH_EXCEPTIONS (caught_type_list);
15737   if ((try = java_complete_tree (try)) == error_mark_node)
15738     error_found = 1;
15739   if (CAN_COMPLETE_NORMALLY (try))
15740     CAN_COMPLETE_NORMALLY (node) = 1;
15741   POP_EXCEPTIONS ();
15742
15743   /* Verification ends here */
15744   if (error_found)
15745     return error_mark_node;
15746
15747   TREE_OPERAND (node, 0) = try;
15748   TREE_OPERAND (node, 1) = catch;
15749   TREE_TYPE (node) = void_type_node;
15750   return node;
15751 }
15752
15753 /* 14.17 The synchronized Statement */
15754
15755 static tree
15756 patch_synchronized_statement (tree node, tree wfl_op1)
15757 {
15758   tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15759   tree block = TREE_OPERAND (node, 1);
15760
15761   tree tmp, enter, exit, expr_decl, assignment;
15762
15763   if (expr == error_mark_node)
15764     {
15765       block = java_complete_tree (block);
15766       return expr;
15767     }
15768
15769   /* We might be trying to synchronize on a STRING_CST */
15770   if ((tmp = patch_string (expr)))
15771     expr = tmp;
15772
15773   /* The TYPE of expr must be a reference type */
15774   if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15775     {
15776       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15777       parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15778                            lang_printable_name (TREE_TYPE (expr), 0));
15779       return error_mark_node;
15780     }
15781
15782   /* Generate a try-finally for the synchronized statement, except
15783      that the handler that catches all throw exception calls
15784      _Jv_MonitorExit and then rethrow the exception.
15785      The synchronized statement is then implemented as:
15786      TRY
15787        {
15788          _Jv_MonitorEnter (expression)
15789          synchronized_block
15790          _Jv_MonitorExit (expression)
15791        }
15792      CATCH_ALL
15793        {
15794          e = _Jv_exception_info ();
15795          _Jv_MonitorExit (expression)
15796          Throw (e);
15797        } */
15798
15799   expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15800   BUILD_MONITOR_ENTER (enter, expr_decl);
15801   BUILD_MONITOR_EXIT (exit, expr_decl);
15802   CAN_COMPLETE_NORMALLY (enter) = 1;
15803   CAN_COMPLETE_NORMALLY (exit) = 1;
15804   assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15805   TREE_SIDE_EFFECTS (assignment) = 1;
15806   node = build2 (COMPOUND_EXPR, NULL_TREE,
15807                  build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15808                  build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15809   node = build_expr_block (node, expr_decl);
15810
15811   return java_complete_tree (node);
15812 }
15813
15814 /* 14.16 The throw Statement */
15815
15816 static tree
15817 patch_throw_statement (tree node, tree wfl_op1)
15818 {
15819   tree expr = TREE_OPERAND (node, 0);
15820   tree type = TREE_TYPE (expr);
15821   int unchecked_ok = 0, tryblock_throws_ok = 0;
15822
15823   /* Thrown expression must be assignable to java.lang.Throwable */
15824   if (!try_reference_assignconv (throwable_type_node, expr))
15825     {
15826       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15827       parse_error_context (wfl_operator,
15828     "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15829                            lang_printable_name (type, 0));
15830       /* If the thrown expression was a reference, we further the
15831          compile-time check. */
15832       if (!JREFERENCE_TYPE_P (type))
15833         return error_mark_node;
15834     }
15835
15836   /* At least one of the following must be true */
15837
15838   /* The type of the throw expression is a not checked exception,
15839      i.e. is a unchecked expression. */
15840   unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15841
15842   SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15843   /* An instance can't throw a checked exception unless that exception
15844      is explicitly declared in the `throws' clause of each
15845      constructor. This doesn't apply to anonymous classes, since they
15846      don't have declared constructors. */
15847   if (!unchecked_ok
15848       && DECL_INSTINIT_P (current_function_decl)
15849       && !ANONYMOUS_CLASS_P (current_class))
15850     {
15851       tree current;
15852       for (current = TYPE_METHODS (current_class); current;
15853            current = TREE_CHAIN (current))
15854         if (DECL_CONSTRUCTOR_P (current)
15855             && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15856           {
15857             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)",
15858                                  lang_printable_name (TREE_TYPE (expr), 0));
15859             return error_mark_node;
15860           }
15861     }
15862
15863   /* Throw is contained in a try statement and at least one catch
15864      clause can receive the thrown expression or the current method is
15865      declared to throw such an exception. Or, the throw statement is
15866      contained in a method or constructor declaration and the type of
15867      the Expression is assignable to at least one type listed in the
15868      throws clause the declaration. */
15869   if (!unchecked_ok)
15870     tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15871   if (!(unchecked_ok || tryblock_throws_ok))
15872     {
15873       /* If there is a surrounding try block that has no matching
15874          clatch clause, report it first. A surrounding try block exits
15875          only if there is something after the list of checked
15876          exception thrown by the current function (if any). */
15877       if (IN_TRY_BLOCK_P ())
15878         parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15879                              lang_printable_name (type, 0));
15880       /* If we have no surrounding try statement and the method doesn't have
15881          any throws, report it now. FIXME */
15882
15883       /* We report that the exception can't be throw from a try block
15884          in all circumstances but when the `throw' is inside a static
15885          block. */
15886       else if (!EXCEPTIONS_P (currently_caught_type_list)
15887                && !tryblock_throws_ok)
15888         {
15889           if (DECL_CLINIT_P (current_function_decl))
15890             parse_error_context (wfl_operator,
15891                    "Checked exception %qs can't be thrown in initializer",
15892                                  lang_printable_name (type, 0));
15893           else
15894             parse_error_context (wfl_operator,
15895                    "Checked exception %qs isn't thrown from a %<try%> block",
15896                                  lang_printable_name (type, 0));
15897         }
15898       /* Otherwise, the current method doesn't have the appropriate
15899          throws declaration */
15900       else
15901         parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15902                              lang_printable_name (type, 0));
15903       return error_mark_node;
15904     }
15905
15906   if (! flag_emit_class_files)
15907     BUILD_THROW (node, expr);
15908
15909   return node;
15910 }
15911
15912 /* Check that exception said to be thrown by method DECL can be
15913    effectively caught from where DECL is invoked.  THIS_EXPR is the
15914    expression that computes `this' for the method call.  */
15915 static void
15916 check_thrown_exceptions (
15917 #ifdef USE_MAPPED_LOCATION
15918                          source_location location,
15919 #else
15920
15921                          int location,
15922 #endif
15923                          tree decl, tree this_expr)
15924 {
15925   tree throws;
15926   int is_array_call = 0;
15927
15928   /* Skip check within generated methods, such as access$<n>.  */
15929   if (NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15930     return;
15931
15932   if (this_expr != NULL_TREE
15933       && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15934       && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15935     is_array_call = 1;
15936
15937   /* For all the unchecked exceptions thrown by DECL.  */
15938   for (throws = DECL_FUNCTION_THROWS (decl); throws;
15939        throws = TREE_CHAIN (throws))
15940     if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15941       {
15942         /* Suppress errors about cloning arrays.  */
15943         if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
15944           continue;
15945
15946 #ifdef USE_MAPPED_LOCATION
15947         SET_EXPR_LOCATION (wfl_operator, location);
15948 #else
15949         EXPR_WFL_LINECOL (wfl_operator) = location;
15950 #endif
15951         if (DECL_FINIT_P (current_function_decl))
15952           parse_error_context
15953             (wfl_operator, "Exception %qs can't be thrown in initializer",
15954              lang_printable_name (TREE_VALUE (throws), 0));
15955         else
15956           {
15957             parse_error_context
15958               (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
15959                lang_printable_name (TREE_VALUE (throws), 0),
15960                (DECL_INIT_P (current_function_decl) ?
15961                 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15962                 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15963           }
15964       }
15965 }
15966
15967 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15968    try-catch blocks, OR is listed in the `throws' clause of the
15969    current method.  */
15970
15971 static int
15972 check_thrown_exceptions_do (tree exception)
15973 {
15974   tree list = currently_caught_type_list;
15975   resolve_and_layout (exception, NULL_TREE);
15976   /* First, all the nested try-catch-finally at that stage. The
15977      last element contains `throws' clause exceptions, if any. */
15978   if (IS_UNCHECKED_EXCEPTION_P (exception))
15979     return 1;
15980   while (list)
15981     {
15982       tree caught;
15983       for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15984         if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15985           return 1;
15986       list = TREE_CHAIN (list);
15987     }
15988   return 0;
15989 }
15990
15991 static void
15992 purge_unchecked_exceptions (tree mdecl)
15993 {
15994   tree throws = DECL_FUNCTION_THROWS (mdecl);
15995   tree new = NULL_TREE;
15996
15997   while (throws)
15998     {
15999       tree next = TREE_CHAIN (throws);
16000       if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
16001         {
16002           TREE_CHAIN (throws) = new;
16003           new = throws;
16004         }
16005       throws = next;
16006     }
16007   /* List is inverted here, but it doesn't matter */
16008   DECL_FUNCTION_THROWS (mdecl) = new;
16009 }
16010
16011 /* This function goes over all of CLASS_TYPE ctors and checks whether
16012    each of them features at least one unchecked exception in its
16013    `throws' clause. If it's the case, it returns `true', `false'
16014    otherwise.  */
16015
16016 static bool
16017 ctors_unchecked_throws_clause_p (tree class_type)
16018 {
16019   tree current;
16020
16021   for (current = TYPE_METHODS (class_type); current;
16022        current = TREE_CHAIN (current))
16023     {
16024       bool ctu = false; /* Ctor Throws Unchecked */
16025       if (DECL_CONSTRUCTOR_P (current))
16026         {
16027           tree throws;
16028           for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16029                throws = TREE_CHAIN (throws))
16030             if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16031               ctu = true;
16032         }
16033       /* We return false as we found one ctor that is unfit. */
16034       if (!ctu && DECL_CONSTRUCTOR_P (current))
16035         return false;
16036     }
16037   /* All ctors feature at least one unchecked exception in their
16038      `throws' clause. */
16039   return true;
16040 }
16041
16042 /* 15.24 Conditional Operator ?: */
16043
16044 static tree
16045 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16046 {
16047   tree cond = TREE_OPERAND (node, 0);
16048   tree op1 = TREE_OPERAND (node, 1);
16049   tree op2 = TREE_OPERAND (node, 2);
16050   tree resulting_type = NULL_TREE;
16051   tree t1, t2, patched;
16052   int error_found = 0;
16053
16054   /* The condition and operands of ?: might be StringBuffers crafted
16055      as a result of a string concatenation.  Obtain decent ones here.  */
16056   if ((patched = patch_string (cond)))
16057     TREE_OPERAND (node, 0) = cond = patched;
16058   if ((patched = patch_string (op1)))
16059     TREE_OPERAND (node, 1) = op1 = patched;
16060   if ((patched = patch_string (op2)))
16061     TREE_OPERAND (node, 2) = op2 = patched;
16062
16063   t1 = TREE_TYPE (op1);
16064   t2 = TREE_TYPE (op2);
16065
16066   /* The first expression must be a boolean */
16067   if (TREE_TYPE (cond) != boolean_type_node)
16068     {
16069       SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16070       parse_error_context (wfl_operator,
16071                "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16072                            lang_printable_name (TREE_TYPE (cond), 0));
16073       error_found = 1;
16074     }
16075
16076   /* Second and third can be numeric, boolean (i.e. primitive),
16077      references or null. Anything else results in an error */
16078   if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16079         || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16080             && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16081         || (t1 == boolean_type_node && t2 == boolean_type_node)))
16082     error_found = 1;
16083
16084   /* Determine the type of the conditional expression. Same types are
16085      easy to deal with */
16086   else if (t1 == t2)
16087     resulting_type = t1;
16088
16089   /* There are different rules for numeric types */
16090   else if (JNUMERIC_TYPE_P (t1))
16091     {
16092       /* if byte/short found, the resulting type is short */
16093       if ((t1 == byte_type_node && t2 == short_type_node)
16094           || (t1 == short_type_node && t2 == byte_type_node))
16095         resulting_type = short_type_node;
16096
16097       /* If t1 is a constant int and t2 is of type byte, short or char
16098          and t1's value fits in t2, then the resulting type is t2 */
16099       else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16100           && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16101         resulting_type = t2;
16102
16103       /* If t2 is a constant int and t1 is of type byte, short or char
16104          and t2's value fits in t1, then the resulting type is t1 */
16105       else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16106           && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16107         resulting_type = t1;
16108
16109       /* Otherwise, binary numeric promotion is applied and the
16110          resulting type is the promoted type of operand 1 and 2 */
16111       else
16112         resulting_type = binary_numeric_promotion (t1, t2,
16113                                                    &TREE_OPERAND (node, 1),
16114                                                    &TREE_OPERAND (node, 2));
16115     }
16116
16117   /* Cases of a reference and a null type */
16118   else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16119     resulting_type = t1;
16120
16121   else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16122     resulting_type = t2;
16123
16124   /* Last case: different reference types. If a type can be converted
16125      into the other one by assignment conversion, the latter
16126      determines the type of the expression */
16127   else if ((resulting_type = try_reference_assignconv (t1, op2)))
16128     resulting_type = promote_type (t1);
16129
16130   else if ((resulting_type = try_reference_assignconv (t2, op1)))
16131     resulting_type = promote_type (t2);
16132
16133   /* If we don't have any resulting type, we're in trouble */
16134   if (!resulting_type)
16135     {
16136       char *t = xstrdup (lang_printable_name (t1, 0));
16137       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16138       parse_error_context (wfl_operator,
16139                  "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16140                            t, lang_printable_name (t2, 0));
16141       free (t);
16142       error_found = 1;
16143     }
16144
16145   if (error_found)
16146     {
16147       TREE_TYPE (node) = error_mark_node;
16148       return error_mark_node;
16149     }
16150
16151   TREE_TYPE (node) = resulting_type;
16152   TREE_SET_CODE (node, COND_EXPR);
16153   CAN_COMPLETE_NORMALLY (node) = 1;
16154   return node;
16155 }
16156
16157 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16158
16159 static tree
16160 maybe_build_class_init_for_field (tree decl, tree expr)
16161 {
16162   tree clas = DECL_CONTEXT (decl);
16163   if (flag_emit_class_files)
16164     return expr;
16165
16166   if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16167       && FIELD_FINAL (decl))
16168     {
16169       tree init = DECL_INITIAL (decl);
16170       if (init != NULL_TREE)
16171         init = fold_constant_for_init (init, decl);
16172       if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16173         return expr;
16174     }
16175
16176   return build_class_init (clas, expr);
16177 }
16178
16179 /* Try to constant fold NODE.
16180    If NODE is not a constant expression, return NULL_EXPR.
16181    CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16182
16183 static tree
16184 fold_constant_for_init (tree node, tree context)
16185 {
16186   tree op0, op1, val;
16187   enum tree_code code = TREE_CODE (node);
16188
16189   switch (code)
16190     {
16191     case INTEGER_CST:
16192       if (node == null_pointer_node)
16193         return NULL_TREE;
16194     case STRING_CST:
16195     case REAL_CST:
16196       return node;
16197
16198     case PLUS_EXPR:
16199     case MINUS_EXPR:
16200     case MULT_EXPR:
16201     case TRUNC_MOD_EXPR:
16202     case RDIV_EXPR:
16203     case LSHIFT_EXPR:
16204     case RSHIFT_EXPR:
16205     case URSHIFT_EXPR:
16206     case BIT_AND_EXPR:
16207     case BIT_XOR_EXPR:
16208     case BIT_IOR_EXPR:
16209     case TRUTH_ANDIF_EXPR:
16210     case TRUTH_ORIF_EXPR:
16211     case EQ_EXPR:
16212     case NE_EXPR:
16213     case GT_EXPR:
16214     case GE_EXPR:
16215     case LT_EXPR:
16216     case LE_EXPR:
16217       op0 = TREE_OPERAND (node, 0);
16218       op1 = TREE_OPERAND (node, 1);
16219       val = fold_constant_for_init (op0, context);
16220       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16221         return NULL_TREE;
16222       TREE_OPERAND (node, 0) = val;
16223       val = fold_constant_for_init (op1, context);
16224       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16225         return NULL_TREE;
16226       TREE_OPERAND (node, 1) = val;
16227       return patch_binop (node, op0, op1, 1);
16228
16229     case UNARY_PLUS_EXPR:
16230     case NEGATE_EXPR:
16231     case TRUTH_NOT_EXPR:
16232     case BIT_NOT_EXPR:
16233     case CONVERT_EXPR:
16234     case NOP_EXPR:
16235       op0 = TREE_OPERAND (node, 0);
16236       val = fold_constant_for_init (op0, context);
16237       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16238         return NULL_TREE;
16239       TREE_OPERAND (node, 0) = val;
16240       val = patch_unaryop (node, op0);
16241       if (! TREE_CONSTANT (val))
16242         return NULL_TREE;
16243       return val;
16244
16245       break;
16246
16247     case COND_EXPR:
16248       val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16249       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16250         return NULL_TREE;
16251       TREE_OPERAND (node, 0) = val;
16252       val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16253       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16254         return NULL_TREE;
16255       TREE_OPERAND (node, 1) = val;
16256       val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16257       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16258         return NULL_TREE;
16259       TREE_OPERAND (node, 2) = val;
16260       return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 2)
16261         : TREE_OPERAND (node, 1);
16262
16263     case VAR_DECL:
16264     case FIELD_DECL:
16265       if (! FIELD_FINAL (node)
16266           || DECL_INITIAL (node) == NULL_TREE)
16267         return NULL_TREE;
16268       val = DECL_INITIAL (node);
16269       /* Guard against infinite recursion. */
16270       DECL_INITIAL (node) = NULL_TREE;
16271       val = fold_constant_for_init (val, node);
16272       if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16273         val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16274       DECL_INITIAL (node) = val;
16275       return val;
16276
16277     case EXPR_WITH_FILE_LOCATION:
16278       /* Compare java_complete_tree and resolve_expression_name. */
16279       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16280           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16281         {
16282           tree name = EXPR_WFL_NODE (node);
16283           tree decl;
16284           if (PRIMARY_P (node))
16285             return NULL_TREE;
16286           else if (! QUALIFIED_P (name))
16287             {
16288               decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16289               if (decl == NULL_TREE
16290                   || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16291                 return NULL_TREE;
16292               return fold_constant_for_init (decl, decl);
16293             }
16294           else
16295             {
16296               tree r = NULL_TREE;
16297               /* Install the proper context for the field resolution.  */
16298               tree saved_current_class = current_class;
16299               /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
16300               current_class = DECL_CONTEXT (context);
16301               qualify_ambiguous_name (node);
16302               r = resolve_field_access (node, &decl, NULL);
16303               /* Restore prior context.  */
16304               current_class = saved_current_class;
16305               if (r != error_mark_node && decl != NULL_TREE)
16306                 return fold_constant_for_init (decl, decl);
16307               return NULL_TREE;
16308             }
16309         }
16310       else
16311         {
16312           op0 = TREE_OPERAND (node, 0);
16313           val = fold_constant_for_init (op0, context);
16314           if (val == NULL_TREE || ! TREE_CONSTANT (val))
16315             return NULL_TREE;
16316           TREE_OPERAND (node, 0) = val;
16317           return val;
16318         }
16319
16320 #ifdef USE_COMPONENT_REF
16321     case IDENTIFIER:
16322     case COMPONENT_REF:
16323       ?;
16324 #endif
16325
16326     default:
16327       return NULL_TREE;
16328     }
16329 }
16330
16331 #ifdef USE_COMPONENT_REF
16332 /* Context is 'T' for TypeName, 'P' for PackageName,
16333    'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16334
16335 tree
16336 resolve_simple_name (tree name, int context)
16337 {
16338 }
16339
16340 tree
16341 resolve_qualified_name (tree name, int context)
16342 {
16343 }
16344 #endif
16345
16346 void
16347 init_src_parse (void)
16348 {
16349   /* Sanity check; we've been bit by this before.  */
16350   if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16351     abort ();
16352 }
16353
16354 \f
16355
16356 /* This section deals with the functions that are called when tables
16357    recording class initialization information are traversed.  */
16358
16359 /* This function is called for each class that is known definitely
16360    initialized when a given static method was called. This function
16361    augments a compound expression (INFO) storing all assignment to
16362    initialized static class flags if a flag already existed, otherwise
16363    a new one is created.  */
16364
16365 static int
16366 emit_test_initialization (void **entry_p, void *info)
16367 {
16368   tree l = (tree) info;
16369   tree decl, init;
16370   tree key = (tree) *entry_p;
16371   tree *ite;
16372   htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16373
16374   /* If we haven't found a flag and we're dealing with self registered
16375      with current_function_decl, then don't do anything. Self is
16376      always added as definitely initialized but this information is
16377      valid only if used outside the current function. */
16378   if (current_function_decl == TREE_PURPOSE (l)
16379       && java_treetreehash_find (cf_ht, key) == NULL)
16380     return true;
16381
16382   ite = java_treetreehash_new (cf_ht, key);
16383
16384   /* If we don't have a variable, create one and install it. */
16385   if (*ite == NULL)
16386     {
16387       tree block;
16388
16389       decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16390       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16391       LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16392       DECL_CONTEXT (decl) = current_function_decl;
16393       DECL_INITIAL (decl) = boolean_true_node;
16394       /* Don't emit any symbolic debugging info for this decl.  */
16395       DECL_IGNORED_P (decl) = 1;
16396
16397       /* The trick is to find the right context for it. */
16398       block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16399       TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16400       BLOCK_EXPR_DECLS (block) = decl;
16401       *ite = decl;
16402     }
16403   else
16404     decl = *ite;
16405
16406   /* Now simply augment the compound that holds all the assignments
16407      pertaining to this method invocation. */
16408   init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16409   TREE_SIDE_EFFECTS (init) = 1;
16410   TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16411   TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16412
16413   return true;
16414 }
16415
16416 #ifdef __XGETTEXT__
16417 /* Depending on the version of Bison used to compile this grammar,
16418    it may issue generic diagnostics spelled "syntax error" or
16419    "parse error".  To prevent this from changing the translation
16420    template randomly, we list all the variants of this particular
16421    diagnostic here.  Translators: there is no fine distinction
16422    between diagnostics with "syntax error" in them, and diagnostics
16423    with "parse error" in them.  It's okay to give them both the same
16424    translation.  */
16425 const char d1[] = N_("syntax error");
16426 const char d2[] = N_("parse error");
16427 const char d3[] = N_("syntax error; also virtual memory exhausted");
16428 const char d4[] = N_("parse error; also virtual memory exhausted");
16429 const char d5[] = N_("syntax error: cannot back up");
16430 const char d6[] = N_("parse error: cannot back up");
16431 #endif
16432
16433 #include "gt-java-parse.h"
16434 #include "gtype-java.h"