OSDN Git Service

363d07452189121294d7b6d6d955309111dc737e
[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 "tree-dump.h"
76 #include "cgraph.h"
77 #include "target.h"
78
79 /* Local function prototypes */
80 static char *java_accstring_lookup (int);
81 static const char *accessibility_string (int);
82 static void  classitf_redefinition_error (const char *,tree, tree, tree);
83 static void  variable_redefinition_error (tree, tree, tree, int);
84 static tree  create_class (int, tree, tree, tree);
85 static tree  create_interface (int, tree, tree);
86 static void  end_class_declaration (int);
87 static tree  find_field (tree, tree);
88 static tree lookup_field_wrapper (tree, tree);
89 static int   duplicate_declaration_error_p (tree, tree, tree);
90 static void  register_fields (int, tree, tree);
91 static tree parser_qualified_classname (tree);
92 static int  parser_check_super (tree, tree, tree);
93 static int  parser_check_super_interface (tree, tree, tree);
94 static void check_modifiers_consistency (int);
95 static tree lookup_cl (tree);
96 static tree lookup_java_method2 (tree, tree, int);
97 static tree method_header (int, tree, tree, tree);
98 static void fix_method_argument_names (tree ,tree);
99 static tree method_declarator (tree, tree);
100 static void parse_warning_context (tree cl, const char *gmsgid, ...) ATTRIBUTE_GCC_DIAG(2,3);
101 #ifdef USE_MAPPED_LOCATION
102 static void issue_warning_error_from_context
103   (source_location, const char *gmsgid, va_list *);
104 #else
105 static void issue_warning_error_from_context
106   (tree, const char *gmsgid, va_list *);
107 #endif
108 static void parse_ctor_invocation_error (void);
109 static tree parse_jdk1_1_error (const char *);
110 static void complete_class_report_errors (jdep *);
111 static int process_imports (void);
112 static void read_import_dir (tree);
113 static int find_in_imports_on_demand (tree, tree);
114 static void find_in_imports (tree, tree);
115 static bool inner_class_accessible (tree, tree);
116 static void check_inner_class_access (tree, tree, tree);
117 static int check_pkg_class_access (tree, tree, bool, tree);
118 static tree resolve_package (tree, tree *, tree *);
119 static tree resolve_class (tree, tree, tree, tree);
120 static void declare_local_variables (int, tree, tree);
121 static void dump_java_tree (enum tree_dump_index, tree);
122 static void source_start_java_method (tree);
123 static void source_end_java_method (void);
124 static tree find_name_in_single_imports (tree);
125 static void check_abstract_method_header (tree);
126 static tree lookup_java_interface_method2 (tree, tree);
127 static tree resolve_expression_name (tree, tree *);
128 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
129 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
130 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
131 static tree resolve_and_layout (tree, tree);
132 static tree qualify_and_find (tree, tree, tree);
133 static tree resolve_no_layout (tree, tree);
134 static int invocation_mode (tree, int);
135 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
136 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
137 static tree find_most_specific_methods_list (tree);
138 static int argument_types_convertible (tree, tree);
139 static tree patch_invoke (tree, tree, tree);
140 static int maybe_use_access_method (int, tree *, tree *);
141 static tree lookup_method_invoke (int, tree, tree, tree, tree);
142 static tree register_incomplete_type (int, tree, tree, tree);
143 static tree check_inner_circular_reference (tree, tree);
144 static tree check_circular_reference (tree);
145 static tree obtain_incomplete_type (tree);
146 static tree java_complete_lhs (tree);
147 static tree java_complete_tree (tree);
148 static tree maybe_generate_pre_expand_clinit (tree);
149 static int analyze_clinit_body (tree, tree);
150 static int maybe_yank_clinit (tree);
151 static void start_complete_expand_method (tree);
152 static void java_complete_expand_method (tree);
153 static void java_expand_method_bodies (tree);
154 static int  unresolved_type_p (tree, tree *);
155 static void create_jdep_list (struct parser_ctxt *);
156 static tree build_expr_block (tree, tree);
157 static tree enter_block (void);
158 static tree exit_block (void);
159 static tree lookup_name_in_blocks (tree);
160 static void maybe_absorb_scoping_blocks (void);
161 static tree build_method_invocation (tree, tree);
162 static tree build_new_invocation (tree, tree);
163 static tree build_assignment (int, int, tree, tree);
164 static tree build_binop (enum tree_code, int, tree, tree);
165 static tree patch_assignment (tree, tree);
166 static tree patch_binop (tree, tree, tree, int);
167 static tree build_unaryop (int, int, tree);
168 static tree build_incdec (int, int, tree, int);
169 static tree patch_unaryop (tree, tree);
170 static tree build_cast (int, tree, tree);
171 static tree build_null_of_type (tree);
172 static tree patch_cast (tree, tree);
173 static int valid_ref_assignconv_cast_p (tree, tree, int);
174 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
175 static int valid_cast_to_p (tree, tree);
176 static int valid_method_invocation_conversion_p (tree, tree);
177 static tree try_builtin_assignconv (tree, tree, tree);
178 static tree try_reference_assignconv (tree, tree);
179 static tree build_unresolved_array_type (tree);
180 static int build_type_name_from_array_name (tree, tree *);
181 static tree build_array_from_name (tree, tree, tree, tree *);
182 static tree build_array_ref (int, tree, tree);
183 static tree patch_array_ref (tree);
184 #ifdef USE_MAPPED_LOCATION
185 static tree make_qualified_name (tree, tree, source_location);
186 #else
187 static tree make_qualified_name (tree, tree, int);
188 #endif
189 static tree merge_qualified_name (tree, tree);
190 static tree make_qualified_primary (tree, tree, int);
191 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
192 static void qualify_ambiguous_name (tree);
193 static tree resolve_field_access (tree, tree *, tree *);
194 static tree build_newarray_node (tree, tree, int);
195 static tree patch_newarray (tree);
196 static tree resolve_type_during_patch (tree);
197 static tree build_this (int);
198 static tree build_wfl_wrap (tree, int);
199 static tree build_return (int, tree);
200 static tree patch_return (tree);
201 static tree maybe_access_field (tree, tree, tree);
202 static int complete_function_arguments (tree);
203 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
204 static int not_accessible_p (tree, tree, tree, int);
205 static void check_deprecation (tree, tree);
206 static int class_in_current_package (tree);
207 static tree build_if_else_statement (int, tree, tree, tree);
208 static tree patch_if_else_statement (tree);
209 static tree add_stmt_to_block (tree, tree, tree);
210 static tree patch_exit_expr (tree);
211 static tree build_labeled_block (int, tree);
212 static tree finish_labeled_statement (tree, tree);
213 static tree build_bc_statement (int, int, tree);
214 static tree patch_bc_statement (tree);
215 static tree patch_loop_statement (tree);
216 static tree build_new_loop (tree);
217 static tree build_loop_body (int, tree, int);
218 static tree finish_loop_body (int, tree, tree, int);
219 static tree build_debugable_stmt (int, tree);
220 static tree finish_for_loop (int, tree, tree, tree);
221 static tree patch_switch_statement (tree);
222 static tree string_constant_concatenation (tree, tree);
223 static tree build_string_concatenation (tree, tree);
224 static tree patch_string_cst (tree);
225 static tree patch_string (tree);
226 static tree encapsulate_with_try_catch (int, tree, tree, tree);
227 #ifdef USE_MAPPED_LOCATION
228 static tree build_assertion (source_location, tree, tree);
229 #else
230 static tree build_assertion (int, tree, tree);
231 #endif
232 static tree build_try_statement (int, tree, tree);
233 static tree build_try_finally_statement (int, tree, tree);
234 static tree patch_try_statement (tree);
235 static tree patch_synchronized_statement (tree, tree);
236 static tree patch_throw_statement (tree, tree);
237 static void add_exception_to_throws (tree, tree);
238 #ifdef USE_MAPPED_LOCATION
239 static void check_thrown_exceptions (source_location, tree, tree);
240 #else
241 static void check_thrown_exceptions (int, tree, tree);
242 #endif
243 static int check_thrown_exceptions_do (tree);
244 static bool ctors_unchecked_throws_clause_p (tree);
245 static void check_concrete_throws_clauses (tree, tree, tree, tree);
246 static void check_throws_clauses (tree, tree, tree);
247 static void finish_method_declaration (tree);
248 static tree build_super_invocation (tree);
249 static int verify_constructor_circularity (tree, tree);
250 static char *constructor_circularity_msg (tree, tree);
251 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
252 static const char *get_printable_method_name (tree);
253 static tree patch_conditional_expr (tree, tree, tree);
254 static tree generate_finit (tree);
255 static tree generate_instinit (tree);
256 static tree build_instinit_invocation (tree);
257 static void fix_constructors (tree);
258 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
259 static tree craft_constructor (tree, tree);
260 static tree get_constructor_super (tree);
261 static tree create_artificial_method (tree, int, tree, tree, tree);
262 static void start_artificial_method_body (tree);
263 static void end_artificial_method_body (tree);
264 static int check_method_redefinition (tree, tree);
265 static int check_method_types_complete (tree);
266 static bool hack_is_accessible_p (tree, tree);
267 static void java_check_regular_methods (tree);
268 static void check_interface_throws_clauses (tree, tree);
269 static void java_check_abstract_methods (tree);
270 static void unreachable_stmt_error (tree);
271 static int not_accessible_field_error (tree, tree);
272 static tree find_expr_with_wfl (tree);
273 static void missing_return_error (tree);
274 static tree build_new_array_init (int, tree);
275 static tree patch_new_array_init (tree, tree);
276 static tree maybe_build_array_element_wfl (tree);
277 static int array_constructor_check_entry (tree, constructor_elt *);
278 static const char *purify_type_name (const char *);
279 static tree fold_constant_for_init (tree, tree);
280 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
281 static void static_ref_err (tree, tree, tree);
282 static void parser_add_interface (tree, tree, tree);
283 static void add_superinterfaces (tree, tree);
284 static tree jdep_resolve_class (jdep *);
285 static int note_possible_classname (const char *, int);
286 static void java_complete_expand_classes (void);
287 static void java_complete_expand_class (tree);
288 static void java_complete_expand_methods (tree);
289 static tree cut_identifier_in_qualified (tree);
290 static tree java_stabilize_reference (tree);
291 static tree do_unary_numeric_promotion (tree);
292 static char * operator_string (tree);
293 static tree do_merge_string_cste (tree, const char *, int, int);
294 static tree merge_string_cste (tree, tree, int);
295 static tree java_refold (tree);
296 static int java_decl_equiv (tree, tree);
297 static int binop_compound_p (enum tree_code);
298 static tree search_loop (tree);
299 static int labeled_block_contains_loop_p (tree, tree);
300 static int check_abstract_method_definitions (int, tree, tree);
301 static void java_check_abstract_method_definitions (tree);
302 static void java_debug_context_do (int);
303 static void java_parser_context_push_initialized_field (void);
304 static void java_parser_context_pop_initialized_field (void);
305 static tree reorder_static_initialized (tree);
306 static void java_parser_context_suspend (void);
307 static void java_parser_context_resume (void);
308 static int pop_current_osb (struct parser_ctxt *);
309
310 /* JDK 1.1 work. FIXME */
311
312 static tree maybe_make_nested_class_name (tree);
313 static int make_nested_class_name (tree);
314 static void link_nested_class_to_enclosing (void);
315 static tree resolve_inner_class (tree, tree, tree, tree);
316 static tree find_as_inner_class (tree, tree, tree);
317 static tree find_as_inner_class_do (tree, tree);
318 static int check_inner_class_redefinition (tree, tree);
319
320 static tree build_thisn_assign (void);
321 static tree build_current_thisn (tree);
322 static tree build_access_to_thisn (tree, tree, int);
323 static tree maybe_build_thisn_access_method (tree);
324
325 static tree build_nested_field_access (tree, tree);
326 static tree build_nested_field_access_methods (tree);
327 static tree build_nested_field_access_method (tree, tree, tree, tree, tree);
328 static tree build_nested_field_access_expr (int, tree, tree, tree, tree);
329 static tree build_outer_method_access_method (tree);
330 static tree build_new_access_id (void);
331
332 static int nested_field_access_p (tree, tree);
333 static int nested_field_expanded_access_p (tree, tree *, tree *, tree *);
334 static tree nested_field_access_fix (tree, tree, tree);
335
336 static tree build_incomplete_class_ref (int, tree);
337 static tree patch_incomplete_class_ref (tree);
338 static tree create_anonymous_class (tree);
339 static void patch_anonymous_class (tree, tree, tree);
340 static void add_inner_class_fields (tree, tree);
341
342 static tree build_dot_class_method (tree);
343 static tree build_dot_class_method_invocation (tree, tree);
344 static void create_new_parser_context (int);
345 static tree maybe_build_class_init_for_field (tree, tree);
346
347 static int emit_test_initialization (void **, void *);
348
349 static char *string_convert_int_cst (tree);
350
351 /* Number of error found so far. */
352 int java_error_count;
353 /* Number of warning found so far. */
354 int java_warning_count;
355 /* Cyclic inheritance report, as it can be set by layout_class */
356 const char *cyclic_inheritance_report;
357
358 /* The current parser context */
359 struct parser_ctxt *ctxp;
360
361 /* List of things that were analyzed for which code will be generated */
362 struct parser_ctxt *ctxp_for_generation = NULL;
363 struct parser_ctxt *ctxp_for_generation_last = NULL;
364
365 /* binop_lookup maps token to tree_code. It is used where binary
366    operations are involved and required by the parser. RDIV_EXPR
367    covers both integral/floating point division. The code is changed
368    once the type of both operator is worked out.  */
369
370 static const enum tree_code binop_lookup[19] =
371   {
372     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
373     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
374     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
375     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
376     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
377    };
378 #define BINOP_LOOKUP(VALUE)                                             \
379   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
380
381 /* This is the end index for binary operators that can also be used
382    in compound assignments. */
383 #define BINOP_COMPOUND_CANDIDATES 11
384
385 /* The "$L" identifier we use to create labels.  */
386 static GTY(()) tree label_id;
387
388 /* The "StringBuffer" identifier used for the String `+' operator. */
389 static GTY(()) tree wfl_string_buffer;
390
391 /* The "append" identifier used for String `+' operator.  */
392 static GTY(()) tree wfl_append;
393
394 /* The "toString" identifier used for String `+' operator. */
395 static GTY(()) tree wfl_to_string;
396
397 /* The "java.lang" import qualified name.  */
398 static GTY(()) tree java_lang_id;
399
400 /* The generated `inst$' identifier used for generated enclosing
401    instance/field access functions.  */
402 static GTY(()) tree inst_id;
403
404 /* Context and flag for static blocks */
405 static GTY(()) tree current_static_block;
406
407 /* The generated `write_parm_value$' identifier.  */
408 static GTY(()) tree wpv_id;
409
410 /* Hold THIS for the scope of the current method decl.  */
411 static GTY(()) tree current_this;
412
413 /* Hold a list of catch clauses list. The first element of this list is
414    the list of the catch clauses of the currently analyzed try block. */
415 static GTY(()) tree currently_caught_type_list;
416
417 /* This holds a linked list of all the case labels for the current
418    switch statement.  It is only used when checking to see if there
419    are duplicate labels.  FIXME: probably this should just be attached
420    to the switch itself; then it could be referenced via
421    `ctxp->current_loop'.  */
422 static GTY(()) tree case_label_list;
423
424 /* Anonymous class counter. Will be reset to 1 every time a non
425    anonymous class gets created. */
426 static int anonymous_class_counter = 1;
427
428 static GTY(()) tree src_parse_roots[1];
429
430 /* All classes seen from source code */
431 #define gclass_list src_parse_roots[0]
432
433 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
434    line and point it out.  */
435 /* Should point out the one that don't fit. ASCII/unicode, going
436    backward. FIXME */
437
438 #define check_modifiers(__message, __value, __mask) do {        \
439   if ((__value) & ~(__mask))                                    \
440     {                                                           \
441       size_t i, remainder = (__value) & ~(__mask);              \
442       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)     \
443         if ((1 << i) & remainder)                               \
444           parse_error_context (ctxp->modifier_ctx [i], (__message), \
445                                java_accstring_lookup (1 << i)); \
446     }                                                           \
447 } while (0)
448
449 %}
450
451 %union {
452   tree node;
453   int sub_token;
454   struct {
455     int token;
456 #ifdef USE_MAPPED_LOCATION
457     source_location location;
458 #else
459     int location;
460 #endif
461   } operator;
462   int value;
463 }
464
465 %{
466 #ifdef USE_MAPPED_LOCATION
467 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
468   SET_EXPR_LOCATION(EXPR, (TOKEN).location)
469 #else
470 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
471   (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
472 #endif
473
474 #include "lex.c"
475 %}
476
477 %pure_parser
478
479 /* Things defined here have to match the order of what's in the
480    binop_lookup table.  */
481
482 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
483 %token   LS_TK           SRS_TK          ZRS_TK
484 %token   AND_TK          XOR_TK          OR_TK
485 %token   BOOL_AND_TK BOOL_OR_TK
486 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
487
488 /* This maps to the same binop_lookup entry than the token above */
489
490 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
491 %token   REM_ASSIGN_TK
492 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
493 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
494
495
496 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
497
498 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
499 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
500 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
501 %token   PAD_TK          ABSTRACT_TK        STRICT_TK
502 %token   MODIFIER_TK
503
504 /* Keep those two in order, too */
505 %token   DECR_TK INCR_TK
506
507 /* From now one, things can be in any order */
508
509 %token   DEFAULT_TK      IF_TK              THROW_TK
510 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
511 %token   THROWS_TK       BREAK_TK           IMPORT_TK
512 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
513 %token   VOID_TK         CATCH_TK           INTERFACE_TK
514 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
515 %token   SUPER_TK        WHILE_TK           CLASS_TK
516 %token   SWITCH_TK       CONST_TK           TRY_TK
517 %token   FOR_TK          NEW_TK             CONTINUE_TK
518 %token   GOTO_TK         PACKAGE_TK         THIS_TK
519 %token   ASSERT_TK
520
521 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
522 %token   CHAR_TK         INTEGRAL_TK
523
524 %token   FLOAT_TK        DOUBLE_TK          FP_TK
525
526 %token   ID_TK
527
528 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
529
530 %token   ASSIGN_ANY_TK   ASSIGN_TK
531 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
532
533 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
534 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
535
536 %type    <value>        modifiers MODIFIER_TK final synchronized
537
538 %type    <node>         super ID_TK identifier
539 %type    <node>         name simple_name qualified_name
540 %type    <node>         type_declaration compilation_unit
541                         field_declaration method_declaration extends_interfaces
542                         interfaces interface_type_list
543                         import_declarations package_declaration
544                         type_declarations interface_body
545                         interface_member_declaration constant_declaration
546                         interface_member_declarations interface_type
547                         abstract_method_declaration
548 %type    <node>         class_body_declaration class_member_declaration
549                         static_initializer constructor_declaration block
550 %type    <node>         class_body_declarations constructor_header
551 %type    <node>         class_or_interface_type class_type class_type_list
552                         constructor_declarator explicit_constructor_invocation
553 %type    <node>         dim_expr dim_exprs this_or_super throws
554
555 %type    <node>         variable_declarator_id variable_declarator
556                         variable_declarators variable_initializer
557                         variable_initializers constructor_body
558                         array_initializer
559
560 %type    <node>         class_body block_end constructor_block_end
561 %type    <node>         statement statement_without_trailing_substatement
562                         labeled_statement if_then_statement label_decl
563                         if_then_else_statement while_statement for_statement
564                         statement_nsi labeled_statement_nsi do_statement
565                         if_then_else_statement_nsi while_statement_nsi
566                         for_statement_nsi statement_expression_list for_init
567                         for_update statement_expression expression_statement
568                         primary_no_new_array expression primary array_type
569                         array_creation_initialized array_creation_uninitialized
570                         class_instance_creation_expression field_access
571                         method_invocation array_access something_dot_new
572                         argument_list postfix_expression while_expression
573                         post_increment_expression post_decrement_expression
574                         unary_expression_not_plus_minus unary_expression
575                         pre_increment_expression pre_decrement_expression
576                         cast_expression
577                         multiplicative_expression additive_expression
578                         shift_expression relational_expression
579                         equality_expression and_expression
580                         exclusive_or_expression inclusive_or_expression
581                         conditional_and_expression conditional_or_expression
582                         conditional_expression assignment_expression
583                         left_hand_side assignment for_header for_begin
584                         constant_expression do_statement_begin empty_statement
585                         switch_statement synchronized_statement throw_statement
586                         try_statement assert_statement
587                         switch_expression switch_block
588                         catches catch_clause catch_clause_parameter finally
589                         anonymous_class_creation trap_overflow_corner_case
590 %type    <node>         return_statement break_statement continue_statement
591
592 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK
593 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
594 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
595 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
596 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
597 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
598 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
599 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
600 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
601 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
602 %type    <operator>     THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
603 %type    <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
604 %type    <operator>     NEW_TK ASSERT_TK
605
606 %type    <node>         method_body
607
608 %type    <node>         literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
609                         STRING_LIT_TK NULL_TK VOID_TK
610
611 %type    <node>         IF_TK WHILE_TK FOR_TK
612
613 %type    <node>         formal_parameter_list formal_parameter
614                         method_declarator method_header
615
616 %type    <node>         primitive_type reference_type type
617                         BOOLEAN_TK INTEGRAL_TK FP_TK
618
619 /* Added or modified JDK 1.1 rule types  */
620 %type    <node>         type_literals
621
622 %%
623 /* 19.2 Production from 2.3: The Syntactic Grammar  */
624 goal:  compilation_unit
625                 {}
626 ;
627
628 /* 19.3 Productions from 3: Lexical structure  */
629 literal:
630         INT_LIT_TK
631 |       FP_LIT_TK
632 |       BOOL_LIT_TK
633 |       CHAR_LIT_TK
634 |       STRING_LIT_TK
635 |       NULL_TK
636 ;
637
638 /* 19.4 Productions from 4: Types, Values and Variables  */
639 type:
640         primitive_type
641 |       reference_type
642 ;
643
644 primitive_type:
645         INTEGRAL_TK
646 |       FP_TK
647 |       BOOLEAN_TK
648 ;
649
650 reference_type:
651         class_or_interface_type
652 |       array_type
653 ;
654
655 class_or_interface_type:
656         name
657 ;
658
659 class_type:
660         class_or_interface_type /* Default rule */
661 ;
662
663 interface_type:
664          class_or_interface_type
665 ;
666
667 array_type:
668         primitive_type dims
669                 {
670                   int osb = pop_current_osb (ctxp);
671                   tree t = build_java_array_type (($1), -1);
672                   while (--osb)
673                     t = build_unresolved_array_type (t);
674                   $$ = t;
675                 }
676 |       name dims
677                 {
678                   int osb = pop_current_osb (ctxp);
679                   tree t = $1;
680                   while (osb--)
681                     t = build_unresolved_array_type (t);
682                   $$ = t;
683                 }
684 ;
685
686 /* 19.5 Productions from 6: Names  */
687 name:
688         simple_name             /* Default rule */
689 |       qualified_name          /* Default rule */
690 ;
691
692 simple_name:
693         identifier              /* Default rule */
694 ;
695
696 qualified_name:
697         name DOT_TK identifier
698                 { $$ = make_qualified_name ($1, $3, $2.location); }
699 ;
700
701 identifier:
702         ID_TK
703 ;
704
705 /* 19.6: Production from 7: Packages  */
706 compilation_unit:
707                 {$$ = NULL;}
708 |       package_declaration
709 |       import_declarations
710 |       type_declarations
711 |       package_declaration import_declarations
712 |       package_declaration type_declarations
713 |       import_declarations type_declarations
714 |       package_declaration import_declarations type_declarations
715 ;
716
717 import_declarations:
718         import_declaration
719                 {
720                   $$ = NULL;
721                 }
722 |       import_declarations import_declaration
723                 {
724                   $$ = NULL;
725                 }
726 ;
727
728 type_declarations:
729         type_declaration
730 |       type_declarations type_declaration
731 ;
732
733 package_declaration:
734         PACKAGE_TK name SC_TK
735                 {
736                   ctxp->package = EXPR_WFL_NODE ($2);
737                 }
738 |       PACKAGE_TK error
739                 {yyerror ("Missing name"); RECOVER;}
740 |       PACKAGE_TK name error
741                 {yyerror ("';' expected"); RECOVER;}
742 ;
743
744 import_declaration:
745         single_type_import_declaration
746 |       type_import_on_demand_declaration
747 ;
748
749 single_type_import_declaration:
750         IMPORT_TK name SC_TK
751                 {
752                   tree name = EXPR_WFL_NODE ($2), last_name;
753                   int   i = IDENTIFIER_LENGTH (name)-1;
754                   const char *last = &IDENTIFIER_POINTER (name)[i];
755                   while (last != IDENTIFIER_POINTER (name))
756                     {
757                       if (last [0] == '.')
758                         break;
759                       last--;
760                     }
761                   last_name = get_identifier (++last);
762                   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
763                     {
764                       tree err = find_name_in_single_imports (last_name);
765                       if (err && err != name)
766                         parse_error_context
767                           ($2, "Ambiguous class: %qs and %qs",
768                            IDENTIFIER_POINTER (name),
769                            IDENTIFIER_POINTER (err));
770                       else
771                         REGISTER_IMPORT ($2, last_name);
772                     }
773                   else
774                     REGISTER_IMPORT ($2, last_name);
775                 }
776 |       IMPORT_TK error
777                 {yyerror ("Missing name"); RECOVER;}
778 |       IMPORT_TK name error
779                 {yyerror ("';' expected"); RECOVER;}
780 ;
781
782 type_import_on_demand_declaration:
783         IMPORT_TK name DOT_TK MULT_TK SC_TK
784                 {
785                   tree name = EXPR_WFL_NODE ($2);
786                   tree it;
787                   /* Search for duplicates. */
788                   for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
789                     if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
790                       break;
791                   /* Don't import the same thing more than once, just ignore
792                      duplicates (7.5.2) */
793                   if (! it)
794                     {
795                       read_import_dir ($2);
796                       ctxp->import_demand_list =
797                         chainon (ctxp->import_demand_list,
798                                  build_tree_list ($2, NULL_TREE));
799                     }
800                 }
801 |       IMPORT_TK name DOT_TK error
802                 {yyerror ("'*' expected"); RECOVER;}
803 |       IMPORT_TK name DOT_TK MULT_TK error
804                 {yyerror ("';' expected"); RECOVER;}
805 ;
806
807 type_declaration:
808         class_declaration
809                 { end_class_declaration (0); }
810 |       interface_declaration
811                 { end_class_declaration (0); }
812 |       empty_statement
813 |       error
814                 {
815                   YYERROR_NOW;
816                   yyerror ("Class or interface declaration expected");
817                 }
818 ;
819
820 /* 19.7 Shortened from the original:
821    modifiers: modifier | modifiers modifier
822    modifier: any of public...  */
823 modifiers:
824         MODIFIER_TK
825                 {
826                   $$ = (1 << $1);
827                 }
828 |       modifiers MODIFIER_TK
829                 {
830                   int acc = (1 << $2);
831                   if ($$ & acc)
832                     parse_error_context
833                       (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
834                        java_accstring_lookup (acc));
835                   else
836                     {
837                       $$ |= acc;
838                     }
839                 }
840 ;
841
842 /* 19.8.1 Production from $8.1: Class Declaration */
843 class_declaration:
844         modifiers CLASS_TK identifier super interfaces
845                 { create_class ($1, $3, $4, $5); }
846         class_body
847                 {;}
848 |       CLASS_TK identifier super interfaces
849                 { create_class (0, $2, $3, $4); }
850         class_body
851                 {;}
852 |       modifiers CLASS_TK error
853                 { yyerror ("Missing class name"); RECOVER; }
854 |       CLASS_TK error
855                 { yyerror ("Missing class name"); RECOVER; }
856 |       CLASS_TK identifier error
857                 {
858                   if (!ctxp->class_err) yyerror ("'{' expected");
859                   DRECOVER(class1);
860                 }
861 |       modifiers CLASS_TK identifier error
862                 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
863 ;
864
865 super:
866                 { $$ = NULL; }
867 |       EXTENDS_TK class_type
868                 { $$ = $2; }
869 |       EXTENDS_TK class_type error
870                 {yyerror ("'{' expected"); ctxp->class_err=1;}
871 |       EXTENDS_TK error
872                 {yyerror ("Missing super class name"); ctxp->class_err=1;}
873 ;
874
875 interfaces:
876                 { $$ = NULL_TREE; }
877 |       IMPLEMENTS_TK interface_type_list
878                 { $$ = $2; }
879 |       IMPLEMENTS_TK error
880                 {
881                   ctxp->class_err=1;
882                   yyerror ("Missing interface name");
883                 }
884 ;
885
886 interface_type_list:
887         interface_type
888                 {
889                   ctxp->interface_number = 1;
890                   $$ = build_tree_list ($1, NULL_TREE);
891                 }
892 |       interface_type_list C_TK interface_type
893                 {
894                   ctxp->interface_number++;
895                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
896                 }
897 |       interface_type_list C_TK error
898                 {yyerror ("Missing interface name"); RECOVER;}
899 ;
900
901 class_body:
902         OCB_TK CCB_TK
903                 {
904                   $$ = GET_CPC ();
905                 }
906 |       OCB_TK class_body_declarations CCB_TK
907                 {
908                   $$ = GET_CPC ();
909                 }
910 ;
911
912 class_body_declarations:
913         class_body_declaration
914 |       class_body_declarations class_body_declaration
915 ;
916
917 class_body_declaration:
918         class_member_declaration
919 |       static_initializer
920 |       constructor_declaration
921 |       block                   /* Added, JDK1.1, instance initializer */
922                 {
923                   if (!IS_EMPTY_STMT ($1))
924                     {
925                       TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
926                       SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
927                     }
928                 }
929 ;
930
931 class_member_declaration:
932         field_declaration
933 |       method_declaration
934 |       class_declaration       /* Added, JDK1.1 inner classes */
935                 { end_class_declaration (1); }
936 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
937                 { end_class_declaration (1); }
938 |       empty_statement
939 ;
940
941 /* 19.8.2 Productions from 8.3: Field Declarations  */
942 field_declaration:
943         type variable_declarators SC_TK
944                 { register_fields (0, $1, $2); }
945 |       modifiers type variable_declarators SC_TK
946                 {
947                   check_modifiers
948                     ("Illegal modifier %qs for field declaration",
949                      $1, FIELD_MODIFIERS);
950                   check_modifiers_consistency ($1);
951                   register_fields ($1, $2, $3);
952                 }
953 ;
954
955 variable_declarators:
956         /* Should we use build_decl_list () instead ? FIXME */
957         variable_declarator     /* Default rule */
958 |       variable_declarators C_TK variable_declarator
959                 { $$ = chainon ($1, $3); }
960 |       variable_declarators C_TK error
961                 {yyerror ("Missing term"); RECOVER;}
962 ;
963
964 variable_declarator:
965         variable_declarator_id
966                 { $$ = build_tree_list ($1, NULL_TREE); }
967 |       variable_declarator_id ASSIGN_TK variable_initializer
968                 {
969                   if (java_error_count)
970                     $3 = NULL_TREE;
971                   $$ = build_tree_list
972                     ($1, build_assignment ($2.token, $2.location, $1, $3));
973                 }
974 |       variable_declarator_id ASSIGN_TK error
975                 {
976                   yyerror ("Missing variable initializer");
977                   $$ = build_tree_list ($1, NULL_TREE);
978                   RECOVER;
979                 }
980 |       variable_declarator_id ASSIGN_TK variable_initializer error
981                 {
982                   yyerror ("';' expected");
983                   $$ = build_tree_list ($1, NULL_TREE);
984                   RECOVER;
985                 }
986 ;
987
988 variable_declarator_id:
989         identifier
990 |       variable_declarator_id OSB_TK CSB_TK
991                 { $$ = build_unresolved_array_type ($1); }
992 |       identifier error
993                 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
994 |       variable_declarator_id OSB_TK error
995                 {
996                   yyerror ("']' expected");
997                   DRECOVER(vdi);
998                 }
999 |       variable_declarator_id CSB_TK error
1000                 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1001 ;
1002
1003 variable_initializer:
1004         expression
1005 |       array_initializer
1006 ;
1007
1008 /* 19.8.3 Productions from 8.4: Method Declarations  */
1009 method_declaration:
1010         method_header
1011                 {
1012                   current_function_decl = $1;
1013                   if (current_function_decl
1014                       && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1015                     source_start_java_method (current_function_decl);
1016                   else
1017                     current_function_decl = NULL_TREE;
1018                 }
1019         method_body
1020                 { finish_method_declaration ($3); }
1021 |       method_header error
1022                 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1023 ;
1024
1025 method_header:
1026         type method_declarator throws
1027                 { $$ = method_header (0, $1, $2, $3); }
1028 |       VOID_TK method_declarator throws
1029                 { $$ = method_header (0, void_type_node, $2, $3); }
1030 |       modifiers type method_declarator throws
1031                 { $$ = method_header ($1, $2, $3, $4); }
1032 |       modifiers VOID_TK method_declarator throws
1033                 { $$ = method_header ($1, void_type_node, $3, $4); }
1034 |       type error
1035                 {
1036                   yyerror ("Invalid method declaration, method name required");
1037                   RECOVER;
1038                 }
1039 |       modifiers type error
1040                 {
1041                   yyerror ("Identifier expected");
1042                   RECOVER;
1043                 }
1044 |       VOID_TK error
1045                 {
1046                   yyerror ("Identifier expected");
1047                   RECOVER;
1048                 }
1049 |       modifiers VOID_TK error
1050                 {
1051                   yyerror ("Identifier expected");
1052                   RECOVER;
1053                 }
1054 |       modifiers error
1055                 {
1056                   yyerror ("Invalid method declaration, return type required");
1057                   RECOVER;
1058                 }
1059 ;
1060
1061 method_declarator:
1062         identifier OP_TK CP_TK
1063                 {
1064                   ctxp->formal_parameter_number = 0;
1065                   $$ = method_declarator ($1, NULL_TREE);
1066                 }
1067 |       identifier OP_TK formal_parameter_list CP_TK
1068                 { $$ = method_declarator ($1, $3); }
1069 |       method_declarator OSB_TK CSB_TK
1070                 {
1071                   SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1072                   TREE_PURPOSE ($1) =
1073                     build_unresolved_array_type (TREE_PURPOSE ($1));
1074                   parse_warning_context
1075                     (wfl_operator,
1076                      "Discouraged form of returned type specification");
1077                 }
1078 |       identifier OP_TK error
1079                 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1080 |       method_declarator OSB_TK error
1081                 {yyerror ("']' expected"); RECOVER;}
1082 ;
1083
1084 formal_parameter_list:
1085         formal_parameter
1086                 {
1087                   ctxp->formal_parameter_number = 1;
1088                 }
1089 |       formal_parameter_list C_TK formal_parameter
1090                 {
1091                   ctxp->formal_parameter_number += 1;
1092                   $$ = chainon ($1, $3);
1093                 }
1094 |       formal_parameter_list C_TK error
1095                 { yyerror ("Missing formal parameter term"); RECOVER; }
1096 ;
1097
1098 formal_parameter:
1099         type variable_declarator_id
1100                 {
1101                   $$ = build_tree_list ($2, $1);
1102                 }
1103 |       final type variable_declarator_id /* Added, JDK1.1 final parms */
1104                 {
1105                   $$ = build_tree_list ($3, $2);
1106                   ARG_FINAL_P ($$) = 1;
1107                 }
1108 |       type error
1109                 {
1110                   yyerror ("Missing identifier"); RECOVER;
1111                   $$ = NULL_TREE;
1112                 }
1113 |       final type error
1114                 {
1115                   yyerror ("Missing identifier"); RECOVER;
1116                   $$ = NULL_TREE;
1117                 }
1118 ;
1119
1120 final:
1121         modifiers
1122                 {
1123                   check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1124                                    $1, ACC_FINAL);
1125                   if ($1 != ACC_FINAL)
1126                     MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1127                 }
1128 ;
1129
1130 throws:
1131                 { $$ = NULL_TREE; }
1132 |       THROWS_TK class_type_list
1133                 { $$ = $2; }
1134 |       THROWS_TK error
1135                 {yyerror ("Missing class type term"); RECOVER;}
1136 ;
1137
1138 class_type_list:
1139         class_type
1140                 { $$ = build_tree_list ($1, $1); }
1141 |       class_type_list C_TK class_type
1142                 { $$ = tree_cons ($3, $3, $1); }
1143 |       class_type_list C_TK error
1144                 {yyerror ("Missing class type term"); RECOVER;}
1145 ;
1146
1147 method_body:
1148         block
1149 |       SC_TK { $$ = NULL_TREE; }
1150 ;
1151
1152 /* 19.8.4 Productions from 8.5: Static Initializers  */
1153 static_initializer:
1154         static block
1155                 {
1156                   TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1157                   SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1158                   current_static_block = NULL_TREE;
1159                 }
1160 ;
1161
1162 static:                         /* Test lval.sub_token here */
1163         modifiers
1164                 {
1165                   check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1166                   /* Can't have a static initializer in an innerclass */
1167                   if ($1 | ACC_STATIC &&
1168                       GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1169                     parse_error_context
1170                       (MODIFIER_WFL (STATIC_TK),
1171                        "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1172                        IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1173                   SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1174                 }
1175 ;
1176
1177 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1178 constructor_declaration:
1179         constructor_header
1180                 {
1181                   current_function_decl = $1;
1182                   source_start_java_method (current_function_decl);
1183                 }
1184         constructor_body
1185                 { finish_method_declaration ($3); }
1186 ;
1187
1188 constructor_header:
1189         constructor_declarator throws
1190                 { $$ = method_header (0, NULL_TREE, $1, $2); }
1191 |       modifiers constructor_declarator throws
1192                 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1193 ;
1194
1195 constructor_declarator:
1196         simple_name OP_TK CP_TK
1197                 {
1198                   ctxp->formal_parameter_number = 0;
1199                   $$ = method_declarator ($1, NULL_TREE);
1200                 }
1201 |       simple_name OP_TK formal_parameter_list CP_TK
1202                 { $$ = method_declarator ($1, $3); }
1203 ;
1204
1205 constructor_body:
1206         /* Unlike regular method, we always need a complete (empty)
1207            body so we can safely perform all the required code
1208            addition (super invocation and field initialization) */
1209         block_begin constructor_block_end
1210                 {
1211                   BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1212                   $$ = $2;
1213                 }
1214 |       block_begin explicit_constructor_invocation constructor_block_end
1215                 { $$ = $3; }
1216 |       block_begin block_statements constructor_block_end
1217                 { $$ = $3; }
1218 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1219                 { $$ = $4; }
1220 ;
1221
1222 constructor_block_end:
1223         block_end
1224 ;
1225
1226 /* Error recovery for that rule moved down expression_statement: rule.  */
1227 explicit_constructor_invocation:
1228         this_or_super OP_TK CP_TK SC_TK
1229                 {
1230                   $$ = build_method_invocation ($1, NULL_TREE);
1231                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1232                   $$ = java_method_add_stmt (current_function_decl, $$);
1233                 }
1234 |       this_or_super OP_TK argument_list CP_TK SC_TK
1235                 {
1236                   $$ = build_method_invocation ($1, $3);
1237                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1238                   $$ = java_method_add_stmt (current_function_decl, $$);
1239                 }
1240         /* Added, JDK1.1 inner classes. Modified because the rule
1241            'primary' couldn't work.  */
1242 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1243                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1244 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1245                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1246 ;
1247
1248 this_or_super:                  /* Added, simplifies error diagnostics */
1249         THIS_TK
1250                 {
1251                   tree wfl = build_wfl_node (this_identifier_node);
1252                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1253                   $$ = wfl;
1254                 }
1255 |       SUPER_TK
1256                 {
1257                   tree wfl = build_wfl_node (super_identifier_node);
1258                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1259                   $$ = wfl;
1260                 }
1261 ;
1262
1263 /* 19.9 Productions from 9: Interfaces  */
1264 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1265 interface_declaration:
1266         INTERFACE_TK identifier
1267                 { create_interface (0, $2, NULL_TREE); }
1268         interface_body
1269                 { ; }
1270 |       modifiers INTERFACE_TK identifier
1271                 { create_interface ($1, $3, NULL_TREE); }
1272         interface_body
1273                 { ; }
1274 |       INTERFACE_TK identifier extends_interfaces
1275                 { create_interface (0, $2, $3); }
1276         interface_body
1277                 { ; }
1278 |       modifiers INTERFACE_TK identifier extends_interfaces
1279                 { create_interface ($1, $3, $4); }
1280         interface_body
1281                 { ; }
1282 |       INTERFACE_TK identifier error
1283                 { yyerror ("'{' expected"); RECOVER; }
1284 |       modifiers INTERFACE_TK identifier error
1285                 { yyerror ("'{' expected"); RECOVER; }
1286 ;
1287
1288 extends_interfaces:
1289         EXTENDS_TK interface_type
1290                 {
1291                   ctxp->interface_number = 1;
1292                   $$ = build_tree_list ($2, NULL_TREE);
1293                 }
1294 |       extends_interfaces C_TK interface_type
1295                 {
1296                   ctxp->interface_number++;
1297                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1298                 }
1299 |       EXTENDS_TK error
1300                 {yyerror ("Invalid interface type"); RECOVER;}
1301 |       extends_interfaces C_TK error
1302                 {yyerror ("Missing term"); RECOVER;}
1303 ;
1304
1305 interface_body:
1306         OCB_TK CCB_TK
1307                 { $$ = NULL_TREE; }
1308 |       OCB_TK interface_member_declarations CCB_TK
1309                 { $$ = NULL_TREE; }
1310 ;
1311
1312 interface_member_declarations:
1313         interface_member_declaration
1314 |       interface_member_declarations interface_member_declaration
1315 ;
1316
1317 interface_member_declaration:
1318         constant_declaration
1319 |       abstract_method_declaration
1320 |       class_declaration       /* Added, JDK1.1 inner classes */
1321                 { end_class_declaration (1); }
1322 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
1323                 { end_class_declaration (1); }
1324 |       empty_statement
1325 ;
1326
1327 constant_declaration:
1328         field_declaration
1329 ;
1330
1331 abstract_method_declaration:
1332         method_header SC_TK
1333                 {
1334                   check_abstract_method_header ($1);
1335                   current_function_decl = NULL_TREE; /* FIXME ? */
1336                 }
1337 |       method_header error
1338                 {yyerror ("';' expected"); RECOVER;}
1339 ;
1340
1341 /* 19.10 Productions from 10: Arrays  */
1342 array_initializer:
1343         OCB_TK CCB_TK
1344                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1345 |       OCB_TK C_TK CCB_TK
1346                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1347 |       OCB_TK variable_initializers CCB_TK
1348                 { $$ = build_new_array_init ($1.location, $2); }
1349 |       OCB_TK variable_initializers C_TK CCB_TK
1350                 { $$ = build_new_array_init ($1.location, $2); }
1351 ;
1352
1353 variable_initializers:
1354         variable_initializer
1355                 {
1356                   $$ = tree_cons (maybe_build_array_element_wfl ($1),
1357                                   $1, NULL_TREE);
1358                 }
1359 |       variable_initializers C_TK variable_initializer
1360                 {
1361                   $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1362                 }
1363 |       variable_initializers C_TK error
1364                 {yyerror ("Missing term"); RECOVER;}
1365 ;
1366
1367 /* 19.11 Production from 14: Blocks and Statements  */
1368 block:
1369         block_begin block_end
1370                 { $$ = $2; }
1371 |       block_begin block_statements block_end
1372                 { $$ = $3; }
1373 ;
1374
1375 block_begin:
1376         OCB_TK
1377                 { enter_block (); }
1378 ;
1379
1380 block_end:
1381         CCB_TK
1382                 {
1383                   maybe_absorb_scoping_blocks ();
1384                   $$ = exit_block ();
1385                   if (!BLOCK_SUBBLOCKS ($$))
1386                     BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1387                 }
1388 ;
1389
1390 block_statements:
1391         block_statement
1392 |       block_statements block_statement
1393 ;
1394
1395 block_statement:
1396         local_variable_declaration_statement
1397 |       statement
1398                 { java_method_add_stmt (current_function_decl, $1); }
1399 |       class_declaration       /* Added, JDK1.1 local classes */
1400                 {
1401                   LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1402                   end_class_declaration (1);
1403                 }
1404 ;
1405
1406 local_variable_declaration_statement:
1407         local_variable_declaration SC_TK /* Can't catch missing ';' here */
1408 ;
1409
1410 local_variable_declaration:
1411         type variable_declarators
1412                 { declare_local_variables (0, $1, $2); }
1413 |       final type variable_declarators /* Added, JDK1.1 final locals */
1414                 { declare_local_variables ($1, $2, $3); }
1415 ;
1416
1417 statement:
1418         statement_without_trailing_substatement
1419 |       labeled_statement
1420 |       if_then_statement
1421 |       if_then_else_statement
1422 |       while_statement
1423 |       for_statement
1424                 { $$ = exit_block (); }
1425 ;
1426
1427 statement_nsi:
1428         statement_without_trailing_substatement
1429 |       labeled_statement_nsi
1430 |       if_then_else_statement_nsi
1431 |       while_statement_nsi
1432 |       for_statement_nsi
1433                 { $$ = exit_block (); }
1434 ;
1435
1436 statement_without_trailing_substatement:
1437         block
1438 |       empty_statement
1439 |       expression_statement
1440 |       switch_statement
1441 |       do_statement
1442 |       break_statement
1443 |       continue_statement
1444 |       return_statement
1445 |       synchronized_statement
1446 |       throw_statement
1447 |       try_statement
1448 |       assert_statement
1449 ;
1450
1451 empty_statement:
1452         SC_TK
1453                 {
1454                   if (flag_extraneous_semicolon
1455                       && ! current_static_block
1456                       && (! current_function_decl ||
1457                           /* Verify we're not in a inner class declaration */
1458                           (GET_CPC () != TYPE_NAME
1459                            (DECL_CONTEXT (current_function_decl)))))
1460
1461                     {
1462 #ifdef USE_MAPPED_LOCATION
1463                       SET_EXPR_LOCATION (wfl_operator, input_location);
1464 #else
1465                       EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1466 #endif
1467                       parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1468                     }
1469                   $$ = build_java_empty_stmt ();
1470                 }
1471 ;
1472
1473 label_decl:
1474         identifier REL_CL_TK
1475                 {
1476                   $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1477                                             EXPR_WFL_NODE ($1));
1478                   pushlevel (2);
1479                   push_labeled_block ($$);
1480                   PUSH_LABELED_BLOCK ($$);
1481                 }
1482 ;
1483
1484 labeled_statement:
1485         label_decl statement
1486                 { $$ = finish_labeled_statement ($1, $2); }
1487 |       identifier error
1488                 {yyerror ("':' expected"); RECOVER;}
1489 ;
1490
1491 labeled_statement_nsi:
1492         label_decl statement_nsi
1493                 { $$ = finish_labeled_statement ($1, $2); }
1494 ;
1495
1496 /* We concentrate here a bunch of error handling rules that we couldn't write
1497    earlier, because expression_statement catches a missing ';'.  */
1498 expression_statement:
1499         statement_expression SC_TK
1500                 {
1501                   /* We have a statement. Generate a WFL around it so
1502                      we can debug it */
1503 #ifdef USE_MAPPED_LOCATION
1504                   $$ = expr_add_location ($1, input_location, 1);
1505 #else
1506                   $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1507                   JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1508 #endif
1509                   /* We know we have a statement, so set the debug
1510                      info to be eventually generate here. */
1511                 }
1512 |       error SC_TK
1513                 {
1514                   YYNOT_TWICE yyerror ("Invalid expression statement");
1515                   DRECOVER (expr_stmt);
1516                 }
1517 |       error OCB_TK
1518                 {
1519                   YYNOT_TWICE yyerror ("Invalid expression statement");
1520                   DRECOVER (expr_stmt);
1521                 }
1522 |       error CCB_TK
1523                 {
1524                   YYNOT_TWICE yyerror ("Invalid expression statement");
1525                   DRECOVER (expr_stmt);
1526                 }
1527 |       this_or_super OP_TK error
1528                 {yyerror ("')' expected"); RECOVER;}
1529 |       this_or_super OP_TK CP_TK error
1530                 {
1531                   parse_ctor_invocation_error ();
1532                   RECOVER;
1533                 }
1534 |       this_or_super OP_TK argument_list error
1535                 {yyerror ("')' expected"); RECOVER;}
1536 |       this_or_super OP_TK argument_list CP_TK error
1537                 {
1538                   parse_ctor_invocation_error ();
1539                   RECOVER;
1540                 }
1541 |       name DOT_TK SUPER_TK error
1542                 {yyerror ("'(' expected"); RECOVER;}
1543 |       name DOT_TK SUPER_TK OP_TK error
1544                 {yyerror ("')' expected"); RECOVER;}
1545 |       name DOT_TK SUPER_TK OP_TK argument_list error
1546                 {yyerror ("')' expected"); RECOVER;}
1547 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1548                 {yyerror ("';' expected"); RECOVER;}
1549 |       name DOT_TK SUPER_TK OP_TK CP_TK error
1550                 {yyerror ("';' expected"); RECOVER;}
1551 ;
1552
1553 statement_expression:
1554         assignment
1555 |       pre_increment_expression
1556 |       pre_decrement_expression
1557 |       post_increment_expression
1558 |       post_decrement_expression
1559 |       method_invocation
1560 |       class_instance_creation_expression
1561 ;
1562
1563 if_then_statement:
1564         IF_TK OP_TK expression CP_TK statement
1565                 {
1566                   $$ = build_if_else_statement ($2.location, $3,
1567                                                 $5, NULL_TREE);
1568                 }
1569 |       IF_TK error
1570                 {yyerror ("'(' expected"); RECOVER;}
1571 |       IF_TK OP_TK error
1572                 {yyerror ("Missing term"); RECOVER;}
1573 |       IF_TK OP_TK expression error
1574                 {yyerror ("')' expected"); RECOVER;}
1575 ;
1576
1577 if_then_else_statement:
1578         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1579                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1580 ;
1581
1582 if_then_else_statement_nsi:
1583         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1584                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1585 ;
1586
1587 switch_statement:
1588         switch_expression
1589                 {
1590                   enter_block ();
1591                 }
1592         switch_block
1593                 {
1594                   /* Make into "proper list" of COMPOUND_EXPRs.
1595                      I.e. make the last statement also have its own
1596                      COMPOUND_EXPR. */
1597                   maybe_absorb_scoping_blocks ();
1598                   TREE_OPERAND ($1, 1) = exit_block ();
1599                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1600                 }
1601 ;
1602
1603 switch_expression:
1604         SWITCH_TK OP_TK expression CP_TK
1605                 {
1606                   $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1607                                NULL_TREE, NULL_TREE);
1608                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1609                 }
1610 |       SWITCH_TK error
1611                 {yyerror ("'(' expected"); RECOVER;}
1612 |       SWITCH_TK OP_TK error
1613                 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1614 |       SWITCH_TK OP_TK expression CP_TK error
1615                 {yyerror ("'{' expected"); RECOVER;}
1616 ;
1617
1618 /* Default assignment is there to avoid type node on switch_block
1619    node. */
1620
1621 switch_block:
1622         OCB_TK CCB_TK
1623                 { $$ = NULL_TREE; }
1624 |       OCB_TK switch_labels CCB_TK
1625                 { $$ = NULL_TREE; }
1626 |       OCB_TK switch_block_statement_groups CCB_TK
1627                 { $$ = NULL_TREE; }
1628 |       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1629                 { $$ = NULL_TREE; }
1630 ;
1631
1632 switch_block_statement_groups:
1633         switch_block_statement_group
1634 |       switch_block_statement_groups switch_block_statement_group
1635 ;
1636
1637 switch_block_statement_group:
1638         switch_labels block_statements
1639 ;
1640
1641 switch_labels:
1642         switch_label
1643 |       switch_labels switch_label
1644 ;
1645
1646 switch_label:
1647         CASE_TK constant_expression REL_CL_TK
1648                 {
1649                   tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1650                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1651                   java_method_add_stmt (current_function_decl, lab);
1652                 }
1653 |       DEFAULT_TK REL_CL_TK
1654                 {
1655                   tree lab = make_node (DEFAULT_EXPR);
1656                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1657                   java_method_add_stmt (current_function_decl, lab);
1658                 }
1659 |       CASE_TK error
1660                 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1661 |       CASE_TK constant_expression error
1662                 {yyerror ("':' expected"); RECOVER;}
1663 |       DEFAULT_TK error
1664                 {yyerror ("':' expected"); RECOVER;}
1665 ;
1666
1667 while_expression:
1668         WHILE_TK OP_TK expression CP_TK
1669                 {
1670                   tree body = build_loop_body ($2.location, $3, 0);
1671                   $$ = build_new_loop (body);
1672                 }
1673 ;
1674
1675 while_statement:
1676         while_expression statement
1677                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1678 |       WHILE_TK error
1679                 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1680 |       WHILE_TK OP_TK error
1681                 {yyerror ("Missing term and ')' expected"); RECOVER;}
1682 |       WHILE_TK OP_TK expression error
1683                 {yyerror ("')' expected"); RECOVER;}
1684 ;
1685
1686 while_statement_nsi:
1687         while_expression statement_nsi
1688                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1689 ;
1690
1691 do_statement_begin:
1692         DO_TK
1693                 {
1694                   tree body = build_loop_body (0, NULL_TREE, 1);
1695                   $$ = build_new_loop (body);
1696                 }
1697         /* Need error handing here. FIXME */
1698 ;
1699
1700 do_statement:
1701         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1702                 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1703 ;
1704
1705 for_statement:
1706         for_begin SC_TK expression SC_TK for_update CP_TK statement
1707                 {
1708                   if (CONSTANT_CLASS_P ($3))
1709                     $3 = build_wfl_node ($3);
1710                   $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1711                 }
1712 |       for_begin SC_TK SC_TK for_update CP_TK statement
1713                 {
1714                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1715                   /* We have not condition, so we get rid of the EXIT_EXPR */
1716                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1717                     build_java_empty_stmt ();
1718                 }
1719 |       for_begin SC_TK error
1720                 {yyerror ("Invalid control expression"); RECOVER;}
1721 |       for_begin SC_TK expression SC_TK error
1722                 {yyerror ("Invalid update expression"); RECOVER;}
1723 |       for_begin SC_TK SC_TK error
1724                 {yyerror ("Invalid update expression"); RECOVER;}
1725 ;
1726
1727 for_statement_nsi:
1728         for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1729                 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1730 |       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1731                 {
1732                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1733                   /* We have not condition, so we get rid of the EXIT_EXPR */
1734                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1735                     build_java_empty_stmt ();
1736                 }
1737 ;
1738
1739 for_header:
1740         FOR_TK OP_TK
1741                 {
1742                   /* This scope defined for local variable that may be
1743                      defined within the scope of the for loop */
1744                   enter_block ();
1745                 }
1746 |       FOR_TK error
1747                 {yyerror ("'(' expected"); DRECOVER(for_1);}
1748 |       FOR_TK OP_TK error
1749                 {yyerror ("Invalid init statement"); RECOVER;}
1750 ;
1751
1752 for_begin:
1753         for_header for_init
1754                 {
1755                   /* We now declare the loop body. The loop is
1756                      declared as a for loop. */
1757                   tree body = build_loop_body (0, NULL_TREE, 0);
1758                   $$ =  build_new_loop (body);
1759                   FOR_LOOP_P ($$) = 1;
1760                   /* The loop is added to the current block the for
1761                      statement is defined within */
1762                   java_method_add_stmt (current_function_decl, $$);
1763                 }
1764 ;
1765 for_init:                       /* Can be empty */
1766                 { $$ = build_java_empty_stmt (); }
1767 |       statement_expression_list
1768                 {
1769                   /* Init statement recorded within the previously
1770                      defined block scope */
1771                   $$ = java_method_add_stmt (current_function_decl, $1);
1772                 }
1773 |       local_variable_declaration
1774                 {
1775                   /* Local variable are recorded within the previously
1776                      defined block scope */
1777                   $$ = NULL_TREE;
1778                 }
1779 |       statement_expression_list error
1780                 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1781 ;
1782
1783 for_update:                     /* Can be empty */
1784                 {$$ = build_java_empty_stmt ();}
1785 |       statement_expression_list
1786                 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1787 ;
1788
1789 statement_expression_list:
1790         statement_expression
1791                 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1792 |       statement_expression_list C_TK statement_expression
1793                 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1794 |       statement_expression_list C_TK error
1795                 {yyerror ("Missing term"); RECOVER;}
1796 ;
1797
1798 break_statement:
1799         BREAK_TK SC_TK
1800                 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1801 |       BREAK_TK identifier SC_TK
1802                 { $$ = build_bc_statement ($1.location, 1, $2); }
1803 |       BREAK_TK error
1804                 {yyerror ("Missing term"); RECOVER;}
1805 |       BREAK_TK identifier error
1806                 {yyerror ("';' expected"); RECOVER;}
1807 ;
1808
1809 continue_statement:
1810         CONTINUE_TK SC_TK
1811                 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1812 |       CONTINUE_TK identifier SC_TK
1813                 { $$ = build_bc_statement ($1.location, 0, $2); }
1814 |       CONTINUE_TK error
1815                 {yyerror ("Missing term"); RECOVER;}
1816 |       CONTINUE_TK identifier error
1817                 {yyerror ("';' expected"); RECOVER;}
1818 ;
1819
1820 return_statement:
1821         RETURN_TK SC_TK
1822                 { $$ = build_return ($1.location, NULL_TREE); }
1823 |       RETURN_TK expression SC_TK
1824                 { $$ = build_return ($1.location, $2); }
1825 |       RETURN_TK error
1826                 {yyerror ("Missing term"); RECOVER;}
1827 |       RETURN_TK expression error
1828                 {yyerror ("';' expected"); RECOVER;}
1829 ;
1830
1831 throw_statement:
1832         THROW_TK expression SC_TK
1833                 {
1834                   $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1835                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1836                 }
1837 |       THROW_TK error
1838                 {yyerror ("Missing term"); RECOVER;}
1839 |       THROW_TK expression error
1840                 {yyerror ("';' expected"); RECOVER;}
1841 ;
1842
1843 assert_statement:
1844         ASSERT_TK expression REL_CL_TK expression SC_TK
1845                 {
1846                   $$ = build_assertion ($1.location, $2, $4);
1847                 }
1848 |       ASSERT_TK expression SC_TK
1849                 {
1850                   $$ = build_assertion ($1.location, $2, NULL_TREE);
1851                 }
1852 |       ASSERT_TK error
1853                 {yyerror ("Missing term"); RECOVER;}
1854 |       ASSERT_TK expression error
1855                 {yyerror ("';' expected"); RECOVER;}
1856 ;
1857
1858 synchronized_statement:
1859         synchronized OP_TK expression CP_TK block
1860                 {
1861                   $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1862                   EXPR_WFL_LINECOL ($$) =
1863                     EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1864                 }
1865 |       synchronized OP_TK expression CP_TK error
1866                 {yyerror ("'{' expected"); RECOVER;}
1867 |       synchronized error
1868                 {yyerror ("'(' expected"); RECOVER;}
1869 |       synchronized OP_TK error CP_TK
1870                 {yyerror ("Missing term"); RECOVER;}
1871 |       synchronized OP_TK error
1872                 {yyerror ("Missing term"); RECOVER;}
1873 ;
1874
1875 synchronized:
1876         modifiers
1877                 {
1878                   check_modifiers (
1879              "Illegal modifier %qs. Only %<synchronized%> was expected here",
1880                                    $1, ACC_SYNCHRONIZED);
1881                   if ($1 != ACC_SYNCHRONIZED)
1882                     MODIFIER_WFL (SYNCHRONIZED_TK) =
1883                       build_wfl_node (NULL_TREE);
1884                 }
1885 ;
1886
1887 try_statement:
1888         TRY_TK block catches
1889                 { $$ = build_try_statement ($1.location, $2, $3); }
1890 |       TRY_TK block finally
1891                 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1892 |       TRY_TK block catches finally
1893                 { $$ = build_try_finally_statement
1894                     ($1.location, build_try_statement ($1.location,
1895                                                        $2, $3), $4);
1896                 }
1897 |       TRY_TK error
1898                 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1899 ;
1900
1901 catches:
1902         catch_clause
1903 |       catches catch_clause
1904                 {
1905                   TREE_CHAIN ($2) = $1;
1906                   $$ = $2;
1907                 }
1908 ;
1909
1910 catch_clause:
1911         catch_clause_parameter block
1912                 {
1913                   java_method_add_stmt (current_function_decl, $2);
1914                   exit_block ();
1915                   $$ = $1;
1916                 }
1917 ;
1918
1919 catch_clause_parameter:
1920         CATCH_TK OP_TK formal_parameter CP_TK
1921                 {
1922                   /* We add a block to define a scope for
1923                      formal_parameter (CCBP). The formal parameter is
1924                      declared initialized by the appropriate function
1925                      call */
1926                   tree ccpb;
1927                   tree init;
1928                   if ($3)
1929                     {
1930                       ccpb = enter_block ();
1931                       init = build_assignment
1932                         (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1933                          build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1934                       declare_local_variables (0, TREE_VALUE ($3),
1935                                                build_tree_list 
1936                                                (TREE_PURPOSE ($3), init));
1937                       $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1938                       SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1939                     }
1940                   else
1941                     {
1942                       $$ = error_mark_node;
1943                     }
1944                 }
1945 |       CATCH_TK error
1946                 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1947 |       CATCH_TK OP_TK error
1948                 {
1949                   yyerror ("Missing term or ')' expected");
1950                   RECOVER; $$ = NULL_TREE;
1951                 }
1952 |       CATCH_TK OP_TK error CP_TK /* That's for () */
1953                 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1954 ;
1955
1956 finally:
1957         FINALLY_TK block
1958                 { $$ = $2; }
1959 |       FINALLY_TK error
1960                 {yyerror ("'{' expected"); RECOVER; }
1961 ;
1962
1963 /* 19.12 Production from 15: Expressions  */
1964 primary:
1965         primary_no_new_array
1966 |       array_creation_uninitialized
1967 |       array_creation_initialized
1968 ;
1969
1970 primary_no_new_array:
1971         literal
1972 |       THIS_TK
1973                 { $$ = build_this ($1.location); }
1974 |       OP_TK expression CP_TK
1975                 {$$ = $2;}
1976 |       class_instance_creation_expression
1977 |       field_access
1978 |       method_invocation
1979 |       array_access
1980 |       type_literals
1981         /* Added, JDK1.1 inner classes. Documentation is wrong
1982            referring to a 'ClassName' (class_name) rule that doesn't
1983            exist. Used name: instead.  */
1984 |       name DOT_TK THIS_TK
1985                 {
1986                   tree wfl = build_wfl_node (this_identifier_node);
1987                   $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1988                 }
1989 |       OP_TK expression error
1990                 {yyerror ("')' expected"); RECOVER;}
1991 |       name DOT_TK error
1992                 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1993 |       primitive_type DOT_TK error
1994                 {yyerror ("'class' expected" ); RECOVER;}
1995 |       VOID_TK DOT_TK error
1996                 {yyerror ("'class' expected" ); RECOVER;}
1997 ;
1998
1999 type_literals:
2000         name DOT_TK CLASS_TK
2001                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2002 |       array_type DOT_TK CLASS_TK
2003                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2004 |       primitive_type DOT_TK CLASS_TK
2005                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2006 |       VOID_TK DOT_TK CLASS_TK
2007                 {
2008                    $$ = build_incomplete_class_ref ($2.location,
2009                                                    void_type_node);
2010                 }
2011 ;
2012
2013 class_instance_creation_expression:
2014         NEW_TK class_type OP_TK argument_list CP_TK
2015                 { $$ = build_new_invocation ($2, $4); }
2016 |       NEW_TK class_type OP_TK CP_TK
2017                 { $$ = build_new_invocation ($2, NULL_TREE); }
2018 |       anonymous_class_creation
2019         /* Added, JDK1.1 inner classes, modified to use name or
2020            primary instead of primary solely which couldn't work in
2021            all situations.  */
2022 |       something_dot_new identifier OP_TK CP_TK
2023                 {
2024                   tree ctor = build_new_invocation ($2, NULL_TREE);
2025                   $$ = make_qualified_primary ($1, ctor,
2026                                                EXPR_WFL_LINECOL ($1));
2027                 }
2028 |       something_dot_new identifier OP_TK CP_TK class_body
2029 |       something_dot_new identifier OP_TK argument_list CP_TK
2030                 {
2031                   tree ctor = build_new_invocation ($2, $4);
2032                   $$ = make_qualified_primary ($1, ctor,
2033                                                EXPR_WFL_LINECOL ($1));
2034                 }
2035 |       something_dot_new identifier OP_TK argument_list CP_TK class_body
2036 |       NEW_TK error SC_TK
2037                 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2038 |       NEW_TK class_type error
2039                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2040 |       NEW_TK class_type OP_TK error
2041                 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2042 |       NEW_TK class_type OP_TK argument_list error
2043                 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2044 |       something_dot_new error
2045                 {
2046                   $$ = NULL_TREE;
2047                   YYERROR_NOW;
2048                   yyerror ("Identifier expected");
2049                   RECOVER;
2050                 }
2051 |       something_dot_new identifier error
2052                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2053 ;
2054
2055 /* Created after JDK1.1 rules originally added to
2056    class_instance_creation_expression, but modified to use
2057    'class_type' instead of 'TypeName' (type_name) which is mentioned
2058    in the documentation but doesn't exist. */
2059
2060 anonymous_class_creation:
2061         NEW_TK class_type OP_TK argument_list CP_TK
2062                 { create_anonymous_class ($2); }
2063         class_body
2064                 {
2065                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2066                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2067
2068                   end_class_declaration (1);
2069
2070                   /* Now we can craft the new expression */
2071                   $$ = build_new_invocation (id, $4);
2072
2073                   /* Note that we can't possibly be here if
2074                      `class_type' is an interface (in which case the
2075                      anonymous class extends Object and implements
2076                      `class_type', hence its constructor can't have
2077                      arguments.) */
2078
2079                   /* Otherwise, the innerclass must feature a
2080                      constructor matching `argument_list'. Anonymous
2081                      classes are a bit special: it's impossible to
2082                      define constructor for them, hence constructors
2083                      must be generated following the hints provided by
2084                      the `new' expression. Whether a super constructor
2085                      of that nature exists or not is to be verified
2086                      later on in get_constructor_super.
2087
2088                      It's during the expansion of a `new' statement
2089                      referring to an anonymous class that a ctor will
2090                      be generated for the anonymous class, with the
2091                      right arguments. */
2092
2093                 }
2094 |       NEW_TK class_type OP_TK CP_TK
2095                 { create_anonymous_class ($2); }
2096         class_body
2097                 {
2098                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2099                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2100
2101                   end_class_declaration (1);
2102
2103                   /* Now we can craft the new expression. The
2104                      statement doesn't need to be remember so that a
2105                      constructor can be generated, since its signature
2106                      is already known. */
2107                   $$ = build_new_invocation (id, NULL_TREE);
2108                 }
2109 ;
2110
2111 something_dot_new:              /* Added, not part of the specs. */
2112         name DOT_TK NEW_TK
2113                 { $$ = $1; }
2114 |       primary DOT_TK NEW_TK
2115                 { $$ = $1; }
2116 ;
2117
2118 argument_list:
2119         expression
2120                 {
2121                   $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2122                   ctxp->formal_parameter_number = 1;
2123                 }
2124 |       argument_list C_TK expression
2125                 {
2126                   ctxp->formal_parameter_number += 1;
2127                   $$ = tree_cons (NULL_TREE, $3, $1);
2128                 }
2129 |       argument_list C_TK error
2130                 {yyerror ("Missing term"); RECOVER;}
2131 ;
2132
2133 array_creation_uninitialized:
2134         NEW_TK primitive_type dim_exprs
2135                 { $$ = build_newarray_node ($2, $3, 0); }
2136 |       NEW_TK class_or_interface_type dim_exprs
2137                 { $$ = build_newarray_node ($2, $3, 0); }
2138 |       NEW_TK primitive_type dim_exprs dims
2139                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2140 |       NEW_TK class_or_interface_type dim_exprs dims
2141                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2142 |       NEW_TK error CSB_TK
2143                 {yyerror ("'[' expected"); DRECOVER ("]");}
2144 |       NEW_TK error OSB_TK
2145                 {yyerror ("']' expected"); RECOVER;}
2146 ;
2147
2148 array_creation_initialized:
2149         /* Added, JDK1.1 anonymous array. Initial documentation rule
2150            modified */
2151         NEW_TK class_or_interface_type dims array_initializer
2152                 {
2153                   char *sig;
2154                   int osb = pop_current_osb (ctxp);
2155                   while (osb--)
2156                     obstack_grow (&temporary_obstack, "[]", 2);
2157                   obstack_1grow (&temporary_obstack, '\0');
2158                   sig = obstack_finish (&temporary_obstack);
2159                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2160                                $2, get_identifier (sig), $4);
2161                 }
2162 |       NEW_TK primitive_type dims array_initializer
2163                 {
2164                   int osb = pop_current_osb (ctxp);
2165                   tree type = $2;
2166                   while (osb--)
2167                     type = build_java_array_type (type, -1);
2168                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2169                                build_pointer_type (type), NULL_TREE, $4);
2170                 }
2171 |       NEW_TK error CSB_TK
2172                 {yyerror ("'[' expected"); DRECOVER ("]");}
2173 |       NEW_TK error OSB_TK
2174                 {yyerror ("']' expected"); RECOVER;}
2175 ;
2176
2177 dim_exprs:
2178         dim_expr
2179                 { $$ = build_tree_list (NULL_TREE, $1); }
2180 |       dim_exprs dim_expr
2181                 { $$ = tree_cons (NULL_TREE, $2, $$); }
2182 ;
2183
2184 dim_expr:
2185         OSB_TK expression CSB_TK
2186                 {
2187                   if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2188                     {
2189                       $2 = build_wfl_node ($2);
2190                       TREE_TYPE ($2) = NULL_TREE;
2191                     }
2192                   EXPR_WFL_LINECOL ($2) = $1.location;
2193                   $$ = $2;
2194                 }
2195 |       OSB_TK expression error
2196                 {yyerror ("']' expected"); RECOVER;}
2197 |       OSB_TK error
2198                 {
2199                   yyerror ("Missing term");
2200                   yyerror ("']' expected");
2201                   RECOVER;
2202                 }
2203 ;
2204
2205 dims:
2206         OSB_TK CSB_TK
2207                 {
2208                   int allocate = 0;
2209                   /* If not initialized, allocate memory for the osb
2210                      numbers stack */
2211                   if (!ctxp->osb_limit)
2212                     {
2213                       allocate = ctxp->osb_limit = 32;
2214                       ctxp->osb_depth = -1;
2215                     }
2216                   /* If capacity overflown, reallocate a bigger chunk */
2217                   else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2218                     allocate = ctxp->osb_limit << 1;
2219
2220                   if (allocate)
2221                     {
2222                       allocate *= sizeof (int);
2223                       if (ctxp->osb_number)
2224                         ctxp->osb_number = xrealloc (ctxp->osb_number,
2225                                                      allocate);
2226                       else
2227                         ctxp->osb_number = xmalloc (allocate);
2228                     }
2229                   ctxp->osb_depth++;
2230                   CURRENT_OSB (ctxp) = 1;
2231                 }
2232 |       dims OSB_TK CSB_TK
2233                 { CURRENT_OSB (ctxp)++; }
2234 |       dims OSB_TK error
2235                 { yyerror ("']' expected"); RECOVER;}
2236 ;
2237
2238 field_access:
2239         primary DOT_TK identifier
2240                 { $$ = make_qualified_primary ($1, $3, $2.location); }
2241                 /*  FIXME - REWRITE TO:
2242                 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2243 |       SUPER_TK DOT_TK identifier
2244                 {
2245                   tree super_wfl = build_wfl_node (super_identifier_node);
2246                   SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2247                   $$ = make_qualified_name (super_wfl, $3, $2.location);
2248                 }
2249 |       SUPER_TK error
2250                 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2251 ;
2252
2253 method_invocation:
2254         name OP_TK CP_TK
2255                 { $$ = build_method_invocation ($1, NULL_TREE); }
2256 |       name OP_TK argument_list CP_TK
2257                 { $$ = build_method_invocation ($1, $3); }
2258 |       primary DOT_TK identifier OP_TK CP_TK
2259                 {
2260                   if (TREE_CODE ($1) == THIS_EXPR)
2261                     $$ = build_this_super_qualified_invocation
2262                       (1, $3, NULL_TREE, 0, $2.location);
2263                   else
2264                     {
2265                       tree invok = build_method_invocation ($3, NULL_TREE);
2266                       $$ = make_qualified_primary ($1, invok, $2.location);
2267                     }
2268                 }
2269 |       primary DOT_TK identifier OP_TK argument_list CP_TK
2270                 {
2271                   if (TREE_CODE ($1) == THIS_EXPR)
2272                     $$ = build_this_super_qualified_invocation
2273                       (1, $3, $5, 0, $2.location);
2274                   else
2275                     {
2276                       tree invok = build_method_invocation ($3, $5);
2277                       $$ = make_qualified_primary ($1, invok, $2.location);
2278                     }
2279                 }
2280 |       SUPER_TK DOT_TK identifier OP_TK CP_TK
2281                 {
2282                   $$ = build_this_super_qualified_invocation
2283                     (0, $3, NULL_TREE, $1.location, $2.location);
2284                 }
2285 |       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2286                 {
2287                   $$ = build_this_super_qualified_invocation
2288                     (0, $3, $5, $1.location, $2.location);
2289                 }
2290         /* Screws up thing. I let it here until I'm convinced it can
2291            be removed. FIXME
2292 |       primary DOT_TK error
2293                 {yyerror ("'(' expected"); DRECOVER(bad);} */
2294 |       SUPER_TK DOT_TK error CP_TK
2295                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2296 |       SUPER_TK DOT_TK error DOT_TK
2297                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2298 ;
2299
2300 array_access:
2301         name OSB_TK expression CSB_TK
2302                 { $$ = build_array_ref ($2.location, $1, $3); }
2303 |       primary_no_new_array OSB_TK expression CSB_TK
2304                 { $$ = build_array_ref ($2.location, $1, $3); }
2305 |       array_creation_initialized OSB_TK expression CSB_TK
2306                 { $$ = build_array_ref ($2.location, $1, $3); }
2307 |       name OSB_TK error
2308                 {
2309                   yyerror ("Missing term and ']' expected");
2310                   DRECOVER(array_access);
2311                 }
2312 |       name OSB_TK expression error
2313                 {
2314                   yyerror ("']' expected");
2315                   DRECOVER(array_access);
2316                 }
2317 |       primary_no_new_array OSB_TK error
2318                 {
2319                   yyerror ("Missing term and ']' expected");
2320                   DRECOVER(array_access);
2321                 }
2322 |       primary_no_new_array OSB_TK expression error
2323                 {
2324                   yyerror ("']' expected");
2325                   DRECOVER(array_access);
2326                 }
2327 |       array_creation_initialized OSB_TK error
2328                 {
2329                   yyerror ("Missing term and ']' expected");
2330                   DRECOVER(array_access);
2331                 }
2332 |       array_creation_initialized OSB_TK expression error
2333                 {
2334                   yyerror ("']' expected");
2335                   DRECOVER(array_access);
2336                 }
2337 ;
2338
2339 postfix_expression:
2340         primary
2341 |       name
2342 |       post_increment_expression
2343 |       post_decrement_expression
2344 ;
2345
2346 post_increment_expression:
2347         postfix_expression INCR_TK
2348                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2349 ;
2350
2351 post_decrement_expression:
2352         postfix_expression DECR_TK
2353                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2354 ;
2355
2356 trap_overflow_corner_case:
2357         pre_increment_expression
2358 |       pre_decrement_expression
2359 |       PLUS_TK unary_expression
2360                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2361 |       unary_expression_not_plus_minus
2362 |       PLUS_TK error
2363                 {yyerror ("Missing term"); RECOVER}
2364 ;
2365
2366 unary_expression:
2367         trap_overflow_corner_case
2368                 {
2369                   if ($1)
2370                     error_if_numeric_overflow ($1);
2371                   $$ = $1;
2372                 }
2373 |       MINUS_TK trap_overflow_corner_case
2374                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2375 |       MINUS_TK error
2376                 {yyerror ("Missing term"); RECOVER}
2377 ;
2378
2379 pre_increment_expression:
2380         INCR_TK unary_expression
2381                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2382 |       INCR_TK error
2383                 {yyerror ("Missing term"); RECOVER}
2384 ;
2385
2386 pre_decrement_expression:
2387         DECR_TK unary_expression
2388                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2389 |       DECR_TK error
2390                 {yyerror ("Missing term"); RECOVER}
2391 ;
2392
2393 unary_expression_not_plus_minus:
2394         postfix_expression
2395 |       NOT_TK unary_expression
2396                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2397 |       NEG_TK unary_expression
2398                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2399 |       cast_expression
2400 |       NOT_TK error
2401                 {yyerror ("Missing term"); RECOVER}
2402 |       NEG_TK error
2403                 {yyerror ("Missing term"); RECOVER}
2404 ;
2405
2406 cast_expression:                /* Error handling here is potentially weak */
2407         OP_TK primitive_type dims CP_TK unary_expression
2408                 {
2409                   tree type = $2;
2410                   int osb = pop_current_osb (ctxp);
2411                   while (osb--)
2412                     type = build_java_array_type (type, -1);
2413                   $$ = build_cast ($1.location, type, $5);
2414                 }
2415 |       OP_TK primitive_type CP_TK unary_expression
2416                 { $$ = build_cast ($1.location, $2, $4); }
2417 |       OP_TK expression CP_TK unary_expression_not_plus_minus
2418                 { $$ = build_cast ($1.location, $2, $4); }
2419 |       OP_TK name dims CP_TK unary_expression_not_plus_minus
2420                 {
2421                   const char *ptr;
2422                   int osb = pop_current_osb (ctxp);
2423                   obstack_grow (&temporary_obstack,
2424                                 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2425                                 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2426                   while (osb--)
2427                     obstack_grow (&temporary_obstack, "[]", 2);
2428                   obstack_1grow (&temporary_obstack, '\0');
2429                   ptr = obstack_finish (&temporary_obstack);
2430                   EXPR_WFL_NODE ($2) = get_identifier (ptr);
2431                   $$ = build_cast ($1.location, $2, $5);
2432                 }
2433 |       OP_TK primitive_type OSB_TK error
2434                 {yyerror ("']' expected, invalid type expression");}
2435 |       OP_TK error
2436                 {
2437                   YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2438                   RECOVER;
2439                 }
2440 |       OP_TK primitive_type dims CP_TK error
2441                 {yyerror ("Missing term"); RECOVER;}
2442 |       OP_TK primitive_type CP_TK error
2443                 {yyerror ("Missing term"); RECOVER;}
2444 |       OP_TK name dims CP_TK error
2445                 {yyerror ("Missing term"); RECOVER;}
2446 ;
2447
2448 multiplicative_expression:
2449         unary_expression
2450 |       multiplicative_expression MULT_TK unary_expression
2451                 {
2452                   $$ = build_binop (BINOP_LOOKUP ($2.token),
2453                                     $2.location, $1, $3);
2454                 }
2455 |       multiplicative_expression DIV_TK unary_expression
2456                 {
2457                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2458                                     $1, $3);
2459                 }
2460 |       multiplicative_expression REM_TK unary_expression
2461                 {
2462                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2463                                     $1, $3);
2464                 }
2465 |       multiplicative_expression MULT_TK error
2466                 {yyerror ("Missing term"); RECOVER;}
2467 |       multiplicative_expression DIV_TK error
2468                 {yyerror ("Missing term"); RECOVER;}
2469 |       multiplicative_expression REM_TK error
2470                 {yyerror ("Missing term"); RECOVER;}
2471 ;
2472
2473 additive_expression:
2474         multiplicative_expression
2475 |       additive_expression PLUS_TK multiplicative_expression
2476                 {
2477                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478                                     $1, $3);
2479                 }
2480 |       additive_expression MINUS_TK multiplicative_expression
2481                 {
2482                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2483                                     $1, $3);
2484                 }
2485 |       additive_expression PLUS_TK error
2486                 {yyerror ("Missing term"); RECOVER;}
2487 |       additive_expression MINUS_TK error
2488                 {yyerror ("Missing term"); RECOVER;}
2489 ;
2490
2491 shift_expression:
2492         additive_expression
2493 |       shift_expression LS_TK additive_expression
2494                 {
2495                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2496                                     $1, $3);
2497                 }
2498 |       shift_expression SRS_TK additive_expression
2499                 {
2500                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2501                                     $1, $3);
2502                 }
2503 |       shift_expression ZRS_TK additive_expression
2504                 {
2505                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2506                                     $1, $3);
2507                 }
2508 |       shift_expression LS_TK error
2509                 {yyerror ("Missing term"); RECOVER;}
2510 |       shift_expression SRS_TK error
2511                 {yyerror ("Missing term"); RECOVER;}
2512 |       shift_expression ZRS_TK error
2513                 {yyerror ("Missing term"); RECOVER;}
2514 ;
2515
2516 relational_expression:
2517         shift_expression
2518 |       relational_expression LT_TK shift_expression
2519                 {
2520                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2521                                     $1, $3);
2522                 }
2523 |       relational_expression GT_TK shift_expression
2524                 {
2525                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2526                                     $1, $3);
2527                 }
2528 |       relational_expression LTE_TK shift_expression
2529                 {
2530                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2531                                     $1, $3);
2532                 }
2533 |       relational_expression GTE_TK shift_expression
2534                 {
2535                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2536                                     $1, $3);
2537                 }
2538 |       relational_expression INSTANCEOF_TK reference_type
2539                 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2540 |       relational_expression LT_TK error
2541                 {yyerror ("Missing term"); RECOVER;}
2542 |       relational_expression GT_TK error
2543                 {yyerror ("Missing term"); RECOVER;}
2544 |       relational_expression LTE_TK error
2545                 {yyerror ("Missing term"); RECOVER;}
2546 |       relational_expression GTE_TK error
2547                 {yyerror ("Missing term"); RECOVER;}
2548 |       relational_expression INSTANCEOF_TK error
2549                 {yyerror ("Invalid reference type"); RECOVER;}
2550 ;
2551
2552 equality_expression:
2553         relational_expression
2554 |       equality_expression EQ_TK relational_expression
2555                 {
2556                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2557                                     $1, $3);
2558                 }
2559 |       equality_expression NEQ_TK relational_expression
2560                 {
2561                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2562                                     $1, $3);
2563                 }
2564 |       equality_expression EQ_TK error
2565                 {yyerror ("Missing term"); RECOVER;}
2566 |       equality_expression NEQ_TK error
2567                 {yyerror ("Missing term"); RECOVER;}
2568 ;
2569
2570 and_expression:
2571         equality_expression
2572 |       and_expression AND_TK equality_expression
2573                 {
2574                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2575                                     $1, $3);
2576                 }
2577 |       and_expression AND_TK error
2578                 {yyerror ("Missing term"); RECOVER;}
2579 ;
2580
2581 exclusive_or_expression:
2582         and_expression
2583 |       exclusive_or_expression XOR_TK and_expression
2584                 {
2585                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2586                                     $1, $3);
2587                 }
2588 |       exclusive_or_expression XOR_TK error
2589                 {yyerror ("Missing term"); RECOVER;}
2590 ;
2591
2592 inclusive_or_expression:
2593         exclusive_or_expression
2594 |       inclusive_or_expression OR_TK exclusive_or_expression
2595                 {
2596                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2597                                     $1, $3);
2598                 }
2599 |       inclusive_or_expression OR_TK error
2600                 {yyerror ("Missing term"); RECOVER;}
2601 ;
2602
2603 conditional_and_expression:
2604         inclusive_or_expression
2605 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2606                 {
2607                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2608                                     $1, $3);
2609                 }
2610 |       conditional_and_expression BOOL_AND_TK error
2611                 {yyerror ("Missing term"); RECOVER;}
2612 ;
2613
2614 conditional_or_expression:
2615         conditional_and_expression
2616 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
2617                 {
2618                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2619                                     $1, $3);
2620                 }
2621 |       conditional_or_expression BOOL_OR_TK error
2622                 {yyerror ("Missing term"); RECOVER;}
2623 ;
2624
2625 conditional_expression:         /* Error handling here is weak */
2626         conditional_or_expression
2627 |       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2628                 {
2629                   $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2630                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2631                 }
2632 |       conditional_or_expression REL_QM_TK REL_CL_TK error
2633                 {
2634                   YYERROR_NOW;
2635                   yyerror ("Missing term");
2636                   DRECOVER (1);
2637                 }
2638 |       conditional_or_expression REL_QM_TK error
2639                 {yyerror ("Missing term"); DRECOVER (2);}
2640 |       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2641                 {yyerror ("Missing term"); DRECOVER (3);}
2642 ;
2643
2644 assignment_expression:
2645         conditional_expression
2646 |       assignment
2647 ;
2648
2649 assignment:
2650         left_hand_side assignment_operator assignment_expression
2651                 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2652 |       left_hand_side assignment_operator error
2653                 {
2654                   YYNOT_TWICE yyerror ("Missing term");
2655                   DRECOVER (assign);
2656                 }
2657 ;
2658
2659 left_hand_side:
2660         name
2661 |       field_access
2662 |       array_access
2663 ;
2664
2665 assignment_operator:
2666         ASSIGN_ANY_TK
2667 |       ASSIGN_TK
2668 ;
2669
2670 expression:
2671         assignment_expression
2672 ;
2673
2674 constant_expression:
2675         expression
2676 ;
2677
2678 %%
2679
2680 /* Helper function to retrieve an OSB count. Should be used when the
2681    `dims:' rule is being used.  */
2682
2683 static int
2684 pop_current_osb (struct parser_ctxt *ctxp)
2685 {
2686   int to_return;
2687
2688   if (ctxp->osb_depth < 0)
2689     abort ();
2690
2691   to_return = CURRENT_OSB (ctxp);
2692   ctxp->osb_depth--;
2693
2694   return to_return;
2695 }
2696
2697 \f
2698
2699 /* This section of the code deal with save/restoring parser contexts.
2700    Add mode documentation here. FIXME */
2701
2702 /* Helper function. Create a new parser context. With
2703    COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2704    context is copied, otherwise, the new context is zeroed. The newly
2705    created context becomes the current one.  */
2706
2707 static void
2708 create_new_parser_context (int copy_from_previous)
2709 {
2710   struct parser_ctxt *new;
2711
2712   new = ggc_alloc (sizeof (struct parser_ctxt));
2713   if (copy_from_previous)
2714     {
2715       memcpy (new, ctxp, sizeof (struct parser_ctxt));
2716       /* This flag, indicating the context saves global values,
2717          should only be set by java_parser_context_save_global.  */
2718       new->saved_data_ctx = 0;
2719     }
2720   else
2721     memset (new, 0, sizeof (struct parser_ctxt));
2722
2723   new->next = ctxp;
2724   ctxp = new;
2725 }
2726
2727 /* Create a new parser context and make it the current one. */
2728
2729 void
2730 java_push_parser_context (void)
2731 {
2732   create_new_parser_context (0);
2733 }
2734
2735 void
2736 java_pop_parser_context (int generate)
2737 {
2738   tree current;
2739   struct parser_ctxt *next;
2740
2741   if (!ctxp)
2742     return;
2743
2744   next = ctxp->next;
2745   if (next)
2746     {
2747       input_location = ctxp->save_location;
2748       current_class = ctxp->class_type;
2749     }
2750
2751   /* If the old and new lexers differ, then free the old one.  */
2752   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2753     java_destroy_lexer (ctxp->lexer);
2754
2755   /* Set the single import class file flag to 0 for the current list
2756      of imported things */
2757   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2758     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2759
2760   /* If we pushed a context to parse a class intended to be generated,
2761      we keep it so we can remember the class. What we could actually
2762      do is to just update a list of class names.  */
2763   if (generate)
2764     {
2765       if (ctxp_for_generation_last == NULL)
2766         ctxp_for_generation = ctxp;
2767       else
2768         ctxp_for_generation_last->next = ctxp;
2769       ctxp->next = NULL;
2770       ctxp_for_generation_last = ctxp;
2771     }
2772
2773   /* And restore those of the previous context */
2774   if ((ctxp = next))            /* Assignment is really meant here */
2775     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2776       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2777 }
2778
2779 /* Create a parser context for the use of saving some global
2780    variables.  */
2781
2782 void
2783 java_parser_context_save_global (void)
2784 {
2785   if (!ctxp)
2786     {
2787       java_push_parser_context ();
2788       ctxp->saved_data_ctx = 1;
2789     }
2790
2791   /* If this context already stores data, create a new one suitable
2792      for data storage. */
2793   else if (ctxp->saved_data)
2794     {
2795       create_new_parser_context (1);
2796       ctxp->saved_data_ctx = 1;
2797     }
2798
2799   ctxp->save_location = input_location;
2800   ctxp->class_type = current_class;
2801   ctxp->function_decl = current_function_decl;
2802   ctxp->saved_data = 1;
2803 }
2804
2805 /* Restore some global variables from the previous context. Make the
2806    previous context the current one.  */
2807
2808 void
2809 java_parser_context_restore_global (void)
2810 {
2811   input_location = ctxp->save_location;
2812   current_class = ctxp->class_type;
2813   if (wfl_operator)
2814 #ifdef USE_MAPPED_LOCATION
2815     SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2816 #else
2817     EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2818 #endif
2819   current_function_decl = ctxp->function_decl;
2820   ctxp->saved_data = 0;
2821   if (ctxp->saved_data_ctx)
2822     java_pop_parser_context (0);
2823 }
2824
2825 /* Suspend vital data for the current class/function being parsed so
2826    that an other class can be parsed. Used to let local/anonymous
2827    classes be parsed.  */
2828
2829 static void
2830 java_parser_context_suspend (void)
2831 {
2832   /* This makes debugging through java_debug_context easier */
2833   static const char *const name = "<inner buffer context>";
2834
2835   /* Duplicate the previous context, use it to save the globals we're
2836      interested in */
2837   create_new_parser_context (1);
2838   ctxp->function_decl = current_function_decl;
2839   ctxp->class_type = current_class;
2840
2841   /* Then create a new context which inherits all data from the
2842      previous one. This will be the new current context  */
2843   create_new_parser_context (1);
2844
2845   /* Help debugging */
2846   ctxp->next->filename = name;
2847 }
2848
2849 /* Resume vital data for the current class/function being parsed so
2850    that an other class can be parsed. Used to let local/anonymous
2851    classes be parsed.  The trick is the data storing file position
2852    informations must be restored to their current value, so parsing
2853    can resume as if no context was ever saved. */
2854
2855 static void
2856 java_parser_context_resume (void)
2857 {
2858   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2859   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2860   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2861
2862   /* We need to inherit the list of classes to complete/generate */
2863   restored->classd_list = old->classd_list;
2864   restored->class_list = old->class_list;
2865
2866   /* Restore the current class and function from the saver */
2867   current_class = saver->class_type;
2868   current_function_decl = saver->function_decl;
2869
2870   /* Retrieve the restored context */
2871   ctxp = restored;
2872
2873   /* Re-installed the data for the parsing to carry on */
2874   memcpy (&ctxp->marker_begining, &old->marker_begining,
2875           (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2876 }
2877
2878 /* Add a new anchor node to which all statement(s) initializing static
2879    and non static initialized upon declaration field(s) will be
2880    linked.  */
2881
2882 static void
2883 java_parser_context_push_initialized_field (void)
2884 {
2885   tree node;
2886
2887   node = build_tree_list (NULL_TREE, NULL_TREE);
2888   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2889   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2890
2891   node = build_tree_list (NULL_TREE, NULL_TREE);
2892   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2893   CPC_INITIALIZER_LIST (ctxp) = node;
2894
2895   node = build_tree_list (NULL_TREE, NULL_TREE);
2896   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2897   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2898 }
2899
2900 /* Pop the lists of initialized field. If this lists aren't empty,
2901    remember them so we can use it to create and populate the finit$
2902    or <clinit> functions. */
2903
2904 static void
2905 java_parser_context_pop_initialized_field (void)
2906 {
2907   tree stmts;
2908   tree class_type = TREE_TYPE (GET_CPC ());
2909
2910   if (CPC_INITIALIZER_LIST (ctxp))
2911     {
2912       stmts = CPC_INITIALIZER_STMT (ctxp);
2913       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2914       if (stmts && !java_error_count)
2915         TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2916     }
2917
2918   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2919     {
2920       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2921       CPC_STATIC_INITIALIZER_LIST (ctxp) =
2922         TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2923       /* Keep initialization in order to enforce 8.5 */
2924       if (stmts && !java_error_count)
2925         TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2926     }
2927
2928   /* JDK 1.1 instance initializers */
2929   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2930     {
2931       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2932       CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2933         TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2934       if (stmts && !java_error_count)
2935         TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2936     }
2937 }
2938
2939 static tree
2940 reorder_static_initialized (tree list)
2941 {
2942   /* We have to keep things in order. The alias initializer have to
2943      come first, then the initialized regular field, in reverse to
2944      keep them in lexical order. */
2945   tree marker, previous = NULL_TREE;
2946   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2947     if (TREE_CODE (marker) == TREE_LIST
2948         && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2949       break;
2950
2951   /* No static initialized, the list is fine as is */
2952   if (!previous)
2953     list = TREE_CHAIN (marker);
2954
2955   /* No marker? reverse the whole list */
2956   else if (!marker)
2957     list = nreverse (list);
2958
2959   /* Otherwise, reverse what's after the marker and the new reordered
2960      sublist will replace the marker. */
2961   else
2962     {
2963       TREE_CHAIN (previous) = NULL_TREE;
2964       list = nreverse (list);
2965       list = chainon (TREE_CHAIN (marker), list);
2966     }
2967   return list;
2968 }
2969
2970 /* Helper functions to dump the parser context stack.  */
2971
2972 #define TAB_CONTEXT(C) \
2973   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2974
2975 static void
2976 java_debug_context_do (int tab)
2977 {
2978   struct parser_ctxt *copy = ctxp;
2979   while (copy)
2980     {
2981       TAB_CONTEXT (tab);
2982       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2983       TAB_CONTEXT (tab);
2984       fprintf (stderr, "filename: %s\n", copy->filename);
2985       TAB_CONTEXT (tab);
2986       fprintf (stderr, "package: %s\n",
2987                (copy->package ?
2988                 IDENTIFIER_POINTER (copy->package) : "<none>"));
2989       TAB_CONTEXT (tab);
2990       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2991       TAB_CONTEXT (tab);
2992       fprintf (stderr, "saved data: %d\n", copy->saved_data);
2993       copy = copy->next;
2994       tab += 2;
2995     }
2996 }
2997
2998 /* Dump the stacked up parser contexts. Intended to be called from a
2999    debugger.  */
3000
3001 void
3002 java_debug_context (void)
3003 {
3004   java_debug_context_do (0);
3005 }
3006
3007 \f
3008
3009 /* Flag for the error report routine to issue the error the first time
3010    it's called (overriding the default behavior which is to drop the
3011    first invocation and honor the second one, taking advantage of a
3012    richer context.  */
3013 static int force_error = 0;
3014
3015 /* Reporting an constructor invocation error.  */
3016 static void
3017 parse_ctor_invocation_error (void)
3018 {
3019   if (DECL_CONSTRUCTOR_P (current_function_decl))
3020     yyerror ("Constructor invocation must be first thing in a constructor");
3021   else
3022     yyerror ("Only constructors can invoke constructors");
3023 }
3024
3025 /* Reporting JDK1.1 features not implemented.  */
3026
3027 static tree
3028 parse_jdk1_1_error (const char *msg)
3029 {
3030   sorry (": %qs JDK1.1(TM) feature", msg);
3031   java_error_count++;
3032   return build_java_empty_stmt ();
3033 }
3034
3035 static int do_warning = 0;
3036
3037 void
3038 yyerror (const char *msgid)
3039 {
3040 #ifdef USE_MAPPED_LOCATION
3041   static source_location elc;
3042   expanded_location xloc = expand_location (input_location);
3043   int current_line = xloc.line;
3044 #else
3045   static java_lc elc;
3046   int save_lineno;
3047   int current_line = input_line;
3048 #endif
3049   static int prev_lineno;
3050   static const char *prev_msg;
3051
3052   char *remainder, *code_from_source;
3053
3054   if (!force_error && prev_lineno == current_line)
3055     return;
3056 #ifndef USE_MAPPED_LOCATION
3057   current_line = ctxp->lexer->token_start.line;
3058 #endif
3059
3060   /* Save current error location but report latter, when the context is
3061      richer.  */
3062   if (ctxp->java_error_flag == 0)
3063     {
3064       ctxp->java_error_flag = 1;
3065 #ifdef USE_MAPPED_LOCATION
3066       elc = input_location;
3067 #else
3068       elc = ctxp->lexer->token_start;
3069 #endif
3070       /* Do something to use the previous line if we're reaching the
3071          end of the file... */
3072 #ifdef VERBOSE_SKELETON
3073       printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3074 #endif
3075       return;
3076     }
3077
3078   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3079   if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3080     return;
3081
3082   ctxp->java_error_flag = 0;
3083   if (do_warning)
3084     java_warning_count++;
3085   else
3086     java_error_count++;
3087
3088 #if 0 /* FIXME */
3089   if (elc.col == 0 && msgid && msgid[1] == ';')
3090     elc = ctxp->prev_line_end;
3091 #endif
3092
3093   prev_msg = msgid;
3094
3095 #ifdef USE_MAPPED_LOCATION
3096   prev_lineno = current_line;
3097   code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3098 #else
3099   save_lineno = input_line;
3100   prev_lineno = input_line = current_line;
3101   code_from_source = java_get_line_col (input_filename, current_line,
3102                                         ctxp->lexer->token_start.col);
3103 #endif
3104
3105
3106   obstack_grow0 (&temporary_obstack,
3107                  code_from_source, strlen (code_from_source));
3108   remainder = obstack_finish (&temporary_obstack);
3109   if (do_warning)
3110     warning (0, "%s.\n%s", msgid, remainder);
3111   else
3112     error ("%s.\n%s", msgid, remainder);
3113
3114   /* This allow us to cheaply avoid an extra 'Invalid expression
3115      statement' error report when errors have been already reported on
3116      the same line. This occurs when we report an error but don't have
3117      a synchronization point other than ';', which
3118      expression_statement is the only one to take care of.  */
3119 #ifndef USE_MAPPED_LOCATION
3120   input_line = save_lineno;
3121 #endif
3122   ctxp->prevent_ese = input_line;
3123 }
3124
3125 static void
3126 issue_warning_error_from_context (
3127 #ifdef USE_MAPPED_LOCATION
3128                                   source_location cl,
3129 #else
3130                                   tree cl,
3131 #endif
3132                                   const char *gmsgid, va_list *ap)
3133 {
3134 #ifdef USE_MAPPED_LOCATION
3135   source_location saved_location = input_location;
3136   expanded_location xloc = expand_location (cl);
3137 #else
3138   java_lc save_lc = ctxp->lexer->token_start;
3139   const char *saved = ctxp->filename, *saved_input_filename;
3140 #endif
3141   char buffer [4096];
3142   text_info text;
3143
3144   text.err_no = errno;
3145   text.args_ptr = ap;
3146   text.format_spec = gmsgid;
3147   pp_format (global_dc->printer, &text);
3148   pp_output_formatted_text (global_dc->printer);
3149   strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3150   buffer[sizeof (buffer) - 1] = '\0';
3151   pp_clear_output_area (global_dc->printer);
3152
3153   force_error = 1;
3154
3155 #ifdef USE_MAPPED_LOCATION
3156   if (xloc.file != NULL)
3157     {
3158       ctxp->filename = xloc.file;
3159       input_location = cl;
3160     }
3161 #else
3162   ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3163   ctxp->lexer->token_start.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3164                                    : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3165                                    : EXPR_WFL_COLNO (cl));
3166
3167   /* We have a CL, that's a good reason for using it if it contains data */
3168   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3169     ctxp->filename = EXPR_WFL_FILENAME (cl);
3170   saved_input_filename = input_filename;
3171   input_filename = ctxp->filename;
3172 #endif
3173   java_error (NULL);
3174   java_error (buffer);
3175 #ifdef USE_MAPPED_LOCATION
3176   input_location = saved_location;
3177 #else
3178   ctxp->filename = saved;
3179   input_filename = saved_input_filename;
3180   ctxp->lexer->token_start = save_lc;
3181 #endif
3182   force_error = 0;
3183 }
3184
3185 /* Issue an error message at a current source line CL.
3186    FUTURE/FIXME:  change cl to be a source_location. */
3187
3188 void
3189 parse_error_context (tree cl, const char *gmsgid, ...)
3190 {
3191   va_list ap;
3192   va_start (ap, gmsgid);
3193 #ifdef USE_MAPPED_LOCATION
3194   issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3195 #else
3196   issue_warning_error_from_context (cl, gmsgid, &ap);
3197 #endif
3198   va_end (ap);
3199 }
3200
3201 /* Issue a warning at a current source line CL.
3202    FUTURE/FIXME:  change cl to be a source_location. */
3203
3204 static void
3205 parse_warning_context (tree cl, const char *gmsgid, ...)
3206 {
3207   va_list ap;
3208   va_start (ap, gmsgid);
3209
3210   do_warning = 1;
3211 #ifdef USE_MAPPED_LOCATION
3212   issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3213 #else
3214   issue_warning_error_from_context (cl, gmsgid, &ap);
3215 #endif
3216   do_warning = 0;
3217   va_end (ap);
3218 }
3219
3220 static tree
3221 find_expr_with_wfl (tree node)
3222 {
3223   while (node)
3224     {
3225       enum tree_code_class code;
3226       tree to_return;
3227
3228       switch (TREE_CODE (node))
3229         {
3230         case BLOCK:
3231           node = BLOCK_EXPR_BODY (node);
3232           continue;
3233
3234         case COMPOUND_EXPR:
3235           to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3236           if (to_return)
3237             return to_return;
3238           node = TREE_OPERAND (node, 1);
3239           continue;
3240
3241         case LOOP_EXPR:
3242           node = TREE_OPERAND (node, 0);
3243           continue;
3244
3245         case LABELED_BLOCK_EXPR:
3246           node = LABELED_BLOCK_BODY (node);
3247           continue;
3248
3249         default:
3250           code = TREE_CODE_CLASS (TREE_CODE (node));
3251           if (((code == tcc_unary) || (code == tcc_binary)
3252                || (code == tcc_expression))
3253               && EXPR_WFL_LINECOL (node))
3254             return node;
3255           return NULL_TREE;
3256         }
3257     }
3258   return NULL_TREE;
3259 }
3260
3261 /* Issue a missing return statement error. Uses METHOD to figure the
3262    last line of the method the error occurs in.  */
3263
3264 static void
3265 missing_return_error (tree method)
3266 {
3267 #ifdef USE_MAPPED_LOCATION
3268   SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3269 #else
3270   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3271 #endif
3272   parse_error_context (wfl_operator, "Missing return statement");
3273 }
3274
3275 /* Issue an unreachable statement error. From NODE, find the next
3276    statement to report appropriately.  */
3277 static void
3278 unreachable_stmt_error (tree node)
3279 {
3280   /* Browse node to find the next expression node that has a WFL. Use
3281      the location to report the error */
3282   if (TREE_CODE (node) == COMPOUND_EXPR)
3283     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3284   else
3285     node = find_expr_with_wfl (node);
3286
3287   if (node)
3288     {
3289 #ifdef USE_MAPPED_LOCATION
3290       SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3291 #else
3292       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3293 #endif
3294       parse_error_context (wfl_operator, "Unreachable statement");
3295     }
3296   else
3297     abort ();
3298 }
3299
3300 static int
3301 not_accessible_field_error (tree wfl, tree decl)
3302 {
3303   parse_error_context 
3304     (wfl, "Can't access %s field %<%s.%s%> from %qs",
3305      accessibility_string (get_access_flags_from_decl (decl)),
3306      GET_TYPE_NAME (DECL_CONTEXT (decl)),
3307      IDENTIFIER_POINTER (DECL_NAME (decl)),
3308      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3309   return 1;
3310 }
3311
3312 int
3313 java_report_errors (void)
3314 {
3315   if (java_error_count)
3316     fprintf (stderr, "%d error%s",
3317              java_error_count, (java_error_count == 1 ? "" : "s"));
3318   if (java_warning_count)
3319     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3320              java_warning_count, (java_warning_count == 1 ? "" : "s"));
3321   if (java_error_count || java_warning_count)
3322     putc ('\n', stderr);
3323   return java_error_count;
3324 }
3325
3326 static char *
3327 java_accstring_lookup (int flags)
3328 {
3329   static char buffer [80];
3330 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3331
3332   /* Access modifier looked-up first for easier report on forbidden
3333      access. */
3334   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3335   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3336   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3337   if (flags & ACC_STATIC) COPY_RETURN ("static");
3338   if (flags & ACC_FINAL) COPY_RETURN ("final");
3339   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3340   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3341   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3342   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3343   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3344   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3345
3346   buffer [0] = '\0';
3347   return buffer;
3348 #undef COPY_RETURN
3349 }
3350
3351 /* Returns a string denoting the accessibility of a class or a member as
3352    indicated by FLAGS.  We need a separate function from
3353    java_accstring_lookup, as the latter can return spurious "static", etc.
3354    if package-private access is defined (in which case none of the
3355    relevant access control bits in FLAGS is set).  */
3356
3357 static const char *
3358 accessibility_string (int flags)
3359 {
3360   if (flags & ACC_PRIVATE) return "private";
3361   if (flags & ACC_PROTECTED) return "protected";
3362   if (flags & ACC_PUBLIC) return "public";
3363
3364   return "package-private";
3365 }
3366
3367 /* Issuing error messages upon redefinition of classes, interfaces or
3368    variables. */
3369
3370 static void
3371 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3372 {
3373   parse_error_context (cl, "%s %qs already defined in %s:%d",
3374                        context, IDENTIFIER_POINTER (id),
3375                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3376   /* Here we should point out where its redefined. It's a unicode. FIXME */
3377 }
3378
3379 static void
3380 variable_redefinition_error (tree context, tree name, tree type, int line)
3381 {
3382   const char *type_name;
3383
3384   /* Figure a proper name for type. We might haven't resolved it */
3385   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3386     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3387   else
3388     type_name = lang_printable_name (type, 0);
3389
3390   parse_error_context (context,
3391                        "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3392                        IDENTIFIER_POINTER (name),
3393                        type_name, IDENTIFIER_POINTER (name), line);
3394 }
3395
3396 /* If ANAME is terminated with `[]', it indicates an array. This
3397    function returns the number of `[]' found and if this number is
3398    greater than zero, it extracts the array type name and places it in
3399    the node pointed to by TRIMMED unless TRIMMED is null.  */
3400
3401 static int
3402 build_type_name_from_array_name (tree aname, tree *trimmed)
3403 {
3404   const char *name = IDENTIFIER_POINTER (aname);
3405   int len = IDENTIFIER_LENGTH (aname);
3406   int array_dims;
3407
3408   STRING_STRIP_BRACKETS (name, len, array_dims);
3409
3410   if (array_dims && trimmed)
3411     *trimmed = get_identifier_with_length (name, len);
3412
3413   return array_dims;
3414 }
3415
3416 static tree
3417 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3418 {
3419   int more_dims = 0;
3420
3421   /* Eventually get more dims */
3422   more_dims = build_type_name_from_array_name (name, &name);
3423
3424   /* If we have, then craft a new type for this variable */
3425   if (more_dims)
3426     {
3427       tree save = type;
3428
3429       /* If we have a pointer, use its type */
3430       if (TREE_CODE (type) == POINTER_TYPE)
3431         type = TREE_TYPE (type);
3432
3433       /* Building the first dimension of a primitive type uses this
3434          function */
3435       if (JPRIMITIVE_TYPE_P (type))
3436         {
3437           type = build_java_array_type (type, -1);
3438           more_dims--;
3439         }
3440       /* Otherwise, if we have a WFL for this type, use it (the type
3441          is already an array on an unresolved type, and we just keep
3442          on adding dimensions) */
3443       else if (type_wfl)
3444         {
3445           type = type_wfl;
3446           more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3447                                                         NULL);
3448         }
3449
3450       /* Add all the dimensions */
3451       while (more_dims--)
3452         type = build_unresolved_array_type (type);
3453
3454       /* The type may have been incomplete in the first place */
3455       if (type_wfl)
3456         type = obtain_incomplete_type (type);
3457     }
3458
3459   if (ret_name)
3460     *ret_name = name;
3461   return type;
3462 }
3463
3464 /* Build something that the type identifier resolver will identify as
3465    being an array to an unresolved type. TYPE_WFL is a WFL on a
3466    identifier. */
3467
3468 static tree
3469 build_unresolved_array_type (tree type_or_wfl)
3470 {
3471   const char *ptr;
3472   tree wfl;
3473
3474   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3475      just create a array type */
3476   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3477     return build_java_array_type (type_or_wfl, -1);
3478
3479   obstack_grow (&temporary_obstack,
3480                  IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3481                  IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3482   obstack_grow0 (&temporary_obstack, "[]", 2);
3483   ptr = obstack_finish (&temporary_obstack);
3484 #ifdef USE_MAPPED_LOCATION
3485   wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3486 #else
3487   wfl = build_expr_wfl (get_identifier (ptr),
3488                         EXPR_WFL_FILENAME (type_or_wfl),
3489                         EXPR_WFL_LINENO (type_or_wfl),
3490                         EXPR_WFL_COLNO (type_or_wfl));
3491 #endif
3492   /* Re-install the existing qualifications so that the type can be
3493      resolved properly. */
3494   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3495   return wfl;
3496 }
3497
3498 static void
3499 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3500 {
3501   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3502     parse_error_context (wfl, "Interface %qs repeated",
3503                          IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3504 }
3505
3506 /* Bulk of common class/interface checks. Return 1 if an error was
3507    encountered. TAG is 0 for a class, 1 for an interface.  */
3508
3509 static int
3510 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3511                                 tree qualified_name, tree decl, tree cl)
3512 {
3513   tree node;
3514   int sca = 0;                  /* Static class allowed */
3515   int icaf = 0;                 /* Inner class allowed flags */
3516   int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3517
3518   if (!quiet_flag)
3519     fprintf (stderr, " %s%s %s",
3520              (CPC_INNER_P () ? "inner" : ""),
3521              (is_interface ? "interface" : "class"),
3522              IDENTIFIER_POINTER (qualified_name));
3523
3524   /* Scope of an interface/class type name:
3525        - Can't be imported by a single type import
3526        - Can't already exists in the package */
3527   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3528       && (node = find_name_in_single_imports (raw_name))
3529       && !CPC_INNER_P ())
3530     {
3531       parse_error_context
3532         (cl, "%s name %qs clashes with imported type %qs",
3533          (is_interface ? "Interface" : "Class"),
3534          IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3535       return 1;
3536     }
3537   if (decl && CLASS_COMPLETE_P (decl))
3538     {
3539       classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3540                                    qualified_name, decl, cl);
3541       return 1;
3542     }
3543
3544   if (check_inner_class_redefinition (raw_name, cl))
3545     return 1;
3546
3547   /* If public, file name should match class/interface name, except
3548      when dealing with an inner class */
3549   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3550     {
3551       const char *fname = input_filename;
3552       const char *f;
3553
3554       for (f = fname + strlen (fname);
3555            f != fname && ! IS_DIR_SEPARATOR (*f);
3556            f--)
3557         ;
3558       if (IS_DIR_SEPARATOR (*f))
3559         f++;
3560       if (strncmp (IDENTIFIER_POINTER (raw_name),
3561                    f , IDENTIFIER_LENGTH (raw_name)) ||
3562           f [IDENTIFIER_LENGTH (raw_name)] != '.')
3563         parse_error_context
3564           (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3565                              (is_interface ? "interface" : "class"),
3566                              IDENTIFIER_POINTER (qualified_name),
3567                              IDENTIFIER_POINTER (raw_name));
3568     }
3569
3570   /* Static classes can be declared only in top level classes. Note:
3571      once static, a inner class is a top level class. */
3572   if (flags & ACC_STATIC)
3573     {
3574       /* Catch the specific error of declaring an class inner class
3575          with no toplevel enclosing class. Prevent check_modifiers from
3576          complaining a second time */
3577       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3578         {
3579           parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3580                                IDENTIFIER_POINTER (qualified_name));
3581           sca = ACC_STATIC;
3582         }
3583       /* Else, in the context of a top-level class declaration, let
3584          `check_modifiers' do its job, otherwise, give it a go */
3585       else
3586         sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3587     }
3588
3589   /* Inner classes can be declared private or protected
3590      within their enclosing classes. */
3591   if (CPC_INNER_P ())
3592     {
3593       /* A class which is local to a block can't be public, private,
3594          protected or static. But it is created final, so allow this
3595          one. */
3596       if (current_function_decl)
3597         icaf = sca = uaaf = ACC_FINAL;
3598       else
3599         {
3600           check_modifiers_consistency (flags);
3601           icaf = ACC_PROTECTED;
3602           if (! CLASS_INTERFACE (GET_CPC ()))
3603             icaf |= ACC_PRIVATE;
3604         }
3605     }
3606
3607   if (is_interface)
3608     {
3609       if (CPC_INNER_P ())
3610         uaaf = INTERFACE_INNER_MODIFIERS;
3611       else
3612         uaaf = INTERFACE_MODIFIERS;
3613
3614       check_modifiers ("Illegal modifier %qs for interface declaration",
3615                        flags, uaaf);
3616     }
3617   else
3618     check_modifiers ((current_function_decl ?
3619                       "Illegal modifier %qs for local class declaration" :
3620                       "Illegal modifier %qs for class declaration"),
3621                      flags, uaaf|sca|icaf);
3622   return 0;
3623 }
3624
3625 /* Construct a nested class name.  If the final component starts with
3626    a digit, return true.  Otherwise return false.  */
3627 static int
3628 make_nested_class_name (tree cpc_list)
3629 {
3630   tree name;
3631
3632   if (!cpc_list)
3633     return 0;
3634
3635   make_nested_class_name (TREE_CHAIN (cpc_list));
3636
3637   /* Pick the qualified name when dealing with the first upmost
3638      enclosing class */
3639   name = (TREE_CHAIN (cpc_list)
3640           ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3641   obstack_grow (&temporary_obstack,
3642                 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3643   obstack_1grow (&temporary_obstack, '$');
3644
3645   return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3646 }
3647
3648 /* Can't redefine a class already defined in an earlier scope. */
3649
3650 static int
3651 check_inner_class_redefinition (tree raw_name, tree cl)
3652 {
3653   tree scope_list;
3654
3655   for (scope_list = GET_CPC_LIST (); scope_list;
3656        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3657     if (raw_name == GET_CPC_UN_NODE (scope_list))
3658       {
3659         parse_error_context
3660           (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",
3661            IDENTIFIER_POINTER (raw_name));
3662         return 1;
3663       }
3664   return 0;
3665 }
3666
3667 /* Tries to find a decl for CLASS_TYPE within ENCLOSING.  May return an
3668    invisible/non-accessible matching decl when an accessible one could not be 
3669    found, in order to give a better error message when accessibility is 
3670    checked later.  */
3671
3672 static tree
3673 resolve_inner_class (tree context, tree cl, tree enclosing, tree class_type)
3674 {
3675   tree local_super = NULL_TREE;
3676   tree candidate = NULL_TREE;
3677
3678   /* This hash table is used to register the classes we're going
3679      through when searching the current class as an inner class, in
3680      order to detect circular references.  */
3681   htab_t circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
3682                                          NULL);
3683
3684   while (enclosing)
3685     {
3686       tree decl;
3687
3688       *htab_find_slot (circularity_hash, enclosing, INSERT) = enclosing;
3689
3690       if ((decl = find_as_inner_class (enclosing, class_type, cl)))
3691         {
3692           if (inner_class_accessible (decl, context))
3693             {
3694               candidate = decl;
3695               break;
3696             }
3697           else
3698             if (candidate == NULL_TREE)
3699               candidate = decl;
3700         }       
3701
3702       /* Now go to the upper classes, bail out if necessary.  We will
3703          analyze the returned SUPER and act accordingly (see
3704          do_resolve_class).  */
3705       if (JPRIMITIVE_TYPE_P (TREE_TYPE (enclosing))
3706           || TREE_TYPE (enclosing) == void_type_node)
3707         {
3708           parse_error_context (cl, "Qualifier must be a reference");
3709           enclosing = NULL_TREE;
3710           break;
3711         }
3712       local_super = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
3713       if (!local_super || local_super == object_type_node)
3714         break;
3715
3716       if (TREE_CODE (local_super) == POINTER_TYPE)
3717         local_super = do_resolve_class (NULL, NULL, local_super, NULL, NULL);
3718       else
3719         local_super = TYPE_NAME (local_super);
3720
3721       /* We may not have checked for circular inheritance yet, so do so
3722          here to prevent an infinite loop. */
3723       if (htab_find (circularity_hash, local_super) != NULL)
3724         {
3725           if (!cl)
3726             cl = lookup_cl (enclosing);
3727
3728           parse_error_context
3729             (cl, "Cyclic inheritance involving %s",
3730              IDENTIFIER_POINTER (DECL_NAME (enclosing)));
3731           enclosing = NULL_TREE;
3732         }
3733       else
3734         enclosing = local_super;
3735     }
3736
3737   htab_delete (circularity_hash);
3738
3739   /* We failed, but we might have found a matching class that wasn't 
3740      accessible.  Return that to get a better error message.  */
3741   return candidate;
3742 }
3743
3744 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3745    qualified. */
3746
3747 static tree
3748 find_as_inner_class (tree enclosing, tree name, tree cl)
3749 {
3750   tree qual, to_return;
3751   if (!enclosing)
3752     return NULL_TREE;
3753
3754   name = TYPE_NAME (name);
3755
3756   /* First search: within the scope of `enclosing', search for name */
3757   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3758     qual = EXPR_WFL_QUALIFICATION (cl);
3759   else if (cl)
3760     qual = build_tree_list (cl, NULL_TREE);
3761   else
3762     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3763
3764   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3765     return to_return;
3766
3767   /* We're dealing with a qualified name. Try to resolve thing until
3768      we get something that is an enclosing class. */
3769   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3770     {
3771       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3772
3773       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3774            qual = TREE_CHAIN (qual))
3775         {
3776           acc = merge_qualified_name (acc,
3777                                       EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3778           BUILD_PTR_FROM_NAME (ptr, acc);
3779           decl = do_resolve_class (NULL_TREE, NULL_TREE, ptr, NULL_TREE, cl);
3780         }
3781
3782       /* A NULL qual and a decl means that the search ended
3783          successfully?!? We have to do something then. FIXME */
3784
3785       if (decl)
3786         enclosing = decl;
3787       else
3788         qual = EXPR_WFL_QUALIFICATION (cl);
3789     }
3790   /* Otherwise, create a qual for the other part of the resolution. */
3791   else
3792     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3793
3794   return find_as_inner_class_do (qual, enclosing);
3795 }
3796
3797 /* We go inside the list of sub classes and try to find a way
3798    through. */
3799
3800 static tree
3801 find_as_inner_class_do (tree qual, tree enclosing)
3802 {
3803   if (!qual)
3804     return NULL_TREE;
3805
3806   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3807     {
3808       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3809       tree next_enclosing = NULL_TREE;
3810       tree inner_list;
3811
3812       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3813            inner_list; inner_list = TREE_CHAIN (inner_list))
3814         {
3815           if (TREE_VALUE (inner_list) == name_to_match)
3816             {
3817               next_enclosing = TREE_PURPOSE (inner_list);
3818               break;
3819             }
3820         }
3821       enclosing = next_enclosing;
3822     }
3823
3824   return (!qual && enclosing ? enclosing : NULL_TREE);
3825 }
3826
3827 static void
3828 link_nested_class_to_enclosing (void)
3829 {
3830   if (GET_ENCLOSING_CPC ())
3831     {
3832       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3833       DECL_INNER_CLASS_LIST (enclosing) =
3834         tree_cons (GET_CPC (), GET_CPC_UN (),
3835                    DECL_INNER_CLASS_LIST (enclosing));
3836     }
3837 }
3838
3839 static tree
3840 maybe_make_nested_class_name (tree name)
3841 {
3842   tree id = NULL_TREE;
3843
3844   if (CPC_INNER_P ())
3845     {
3846       /* If we're in a function, we must append a number to create the
3847          nested class name.  However, we don't do this if the class we
3848          are constructing is anonymous, because in that case we'll
3849          already have a number as the class name.  */
3850       if (! make_nested_class_name (GET_CPC_LIST ())
3851           && current_function_decl != NULL_TREE
3852           && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3853         {
3854           char buf[10];
3855           sprintf (buf, "%d", anonymous_class_counter);
3856           ++anonymous_class_counter;
3857           obstack_grow (&temporary_obstack, buf, strlen (buf));
3858           obstack_1grow (&temporary_obstack, '$');
3859         }
3860       obstack_grow0 (&temporary_obstack,
3861                      IDENTIFIER_POINTER (name),
3862                      IDENTIFIER_LENGTH (name));
3863       id = get_identifier (obstack_finish (&temporary_obstack));
3864       if (ctxp->package)
3865         QUALIFIED_P (id) = 1;
3866     }
3867   return id;
3868 }
3869
3870 /* If DECL is NULL, create and push a new DECL, record the current
3871    line CL and do other maintenance things.  */
3872
3873 static tree
3874 maybe_create_class_interface_decl (tree decl, tree raw_name,
3875                                    tree qualified_name, tree cl)
3876 {
3877   if (!decl)
3878     decl = push_class (make_class (), qualified_name);
3879
3880   /* Take care of the file and line business */
3881 #ifdef USE_MAPPED_LOCATION
3882   DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3883 #else
3884   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3885   DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3886 #endif
3887   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3888   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3889 #ifdef USE_MAPPED_LOCATION
3890   {
3891     tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3892     CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3893       tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3894   }
3895 #else
3896   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3897     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3898 #endif
3899
3900   PUSH_CPC (decl, raw_name);
3901   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3902
3903   /* Link the declaration to the already seen ones */
3904   TREE_CHAIN (decl) = ctxp->class_list;
3905   ctxp->class_list = decl;
3906
3907   /* Create a new nodes in the global lists */
3908   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3909   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3910
3911   /* Install a new dependency list element */
3912   create_jdep_list (ctxp);
3913
3914   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3915                           IDENTIFIER_POINTER (qualified_name)));
3916   return decl;
3917 }
3918
3919 static void
3920 add_superinterfaces (tree decl, tree interface_list)
3921 {
3922   tree node;
3923   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3924      takes care of ensuring that:
3925        - This is an accessible interface type,
3926        - Circularity detection.
3927    parser_add_interface is then called. If present but not defined,
3928    the check operation is delayed until the super interface gets
3929    defined.  */
3930   for (node = interface_list; node; node = TREE_CHAIN (node))
3931     {
3932       tree current = TREE_PURPOSE (node);
3933       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3934       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3935         {
3936           if (!parser_check_super_interface (idecl, decl, current))
3937             parser_add_interface (decl, idecl, current);
3938         }
3939       else
3940         register_incomplete_type (JDEP_INTERFACE,
3941                                   current, decl, NULL_TREE);
3942     }
3943 }
3944
3945 /* Create an interface in pass1 and return its decl. Return the
3946    interface's decl in pass 2.  */
3947
3948 static tree
3949 create_interface (int flags, tree id, tree super)
3950 {
3951   tree raw_name = EXPR_WFL_NODE (id);
3952   tree q_name = parser_qualified_classname (raw_name);
3953   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3954
3955   /* Certain syntax errors are making SUPER be like ID. Avoid this
3956      case. */
3957   if (ctxp->class_err && id == super)
3958     super = NULL;
3959
3960   EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3961
3962   /* Basic checks: scope, redefinition, modifiers */
3963   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3964     {
3965       PUSH_ERROR ();
3966       return NULL_TREE;
3967     }
3968
3969   /* Suspend the current parsing context if we're parsing an inner
3970      interface */
3971   if (CPC_INNER_P ())
3972     {
3973       java_parser_context_suspend ();
3974       /* Interface members are public. */
3975       if (CLASS_INTERFACE (GET_CPC ()))
3976         flags |= ACC_PUBLIC;
3977     }
3978
3979   /* Push a new context for (static) initialized upon declaration fields */
3980   java_parser_context_push_initialized_field ();
3981
3982   /* Interface modifiers check
3983        - public/abstract allowed (already done at that point)
3984        - abstract is obsolete (comes first, it's a warning, or should be)
3985        - Can't use twice the same (checked in the modifier rule) */
3986   if ((flags & ACC_ABSTRACT) && flag_redundant)
3987     parse_warning_context
3988       (MODIFIER_WFL (ABSTRACT_TK),
3989        "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3990
3991   /* Create a new decl if DECL is NULL, otherwise fix it */
3992   decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3993
3994   /* Interfaces are always abstract. */
3995   flags |= ACC_ABSTRACT;
3996
3997   /* Inner interfaces are always static.  */
3998   if (INNER_CLASS_DECL_P (decl))
3999     flags |= ACC_STATIC;
4000
4001   /* Set super info and mark the class a complete */
4002   set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
4003                   object_type_node, ctxp->interface_number);
4004   ctxp->interface_number = 0;
4005   CLASS_COMPLETE_P (decl) = 1;
4006   add_superinterfaces (decl, super);
4007
4008   /* Eventually sets the @deprecated tag flag */
4009   CHECK_DEPRECATED (decl);
4010
4011   return decl;
4012 }
4013
4014 /* Patch anonymous class CLASS, by either extending or implementing
4015    DEP.  */
4016
4017 static void
4018 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4019 {
4020   tree class = TREE_TYPE (class_decl);
4021   tree type =  TREE_TYPE (type_decl);
4022   tree binfo = TYPE_BINFO (class);
4023
4024   /* If it's an interface, implement it */
4025   if (CLASS_INTERFACE (type_decl))
4026     {
4027       if (parser_check_super_interface (type_decl, class_decl, wfl))
4028         return;
4029
4030       if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4031         {
4032            /* Extend the binfo - by reallocating and copying it. */
4033           tree new_binfo;
4034           tree base_binfo;
4035           int i;
4036           
4037           new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4038           for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4039             BINFO_BASE_APPEND (new_binfo, base_binfo);
4040           CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4041           BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4042           TYPE_BINFO (class) = new_binfo;
4043         }
4044       
4045       /* And add the interface */
4046       parser_add_interface (class_decl, type_decl, wfl);
4047     }
4048   /* Otherwise, it's a type we want to extend */
4049   else
4050     {
4051       if (parser_check_super (type_decl, class_decl, wfl))
4052         return;
4053       BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4054     }
4055 }
4056
4057 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4058    its decl.  */
4059
4060 static tree
4061 create_anonymous_class (tree type_name)
4062 {
4063   char buffer [80];
4064   tree super = NULL_TREE, itf = NULL_TREE;
4065   tree id, type_decl, class;
4066
4067   /* The unqualified name of the anonymous class. It's just a number. */
4068   sprintf (buffer, "%d", anonymous_class_counter++);
4069   id = build_wfl_node (get_identifier (buffer));
4070   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4071
4072   /* We know about the type to extend/implement. We go ahead */
4073   if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4074     {
4075       /* Create a class which either implements on extends the designated
4076          class. The class bears an inaccessible name. */
4077       if (CLASS_INTERFACE (type_decl))
4078         {
4079           /* It's OK to modify it here. It's been already used and
4080              shouldn't be reused */
4081           ctxp->interface_number = 1;
4082           /* Interfaces should presented as a list of WFLs */
4083           itf = build_tree_list (type_name, NULL_TREE);
4084         }
4085       else
4086         super = type_name;
4087     }
4088
4089   class = create_class (ACC_FINAL, id, super, itf);
4090
4091   /* We didn't know anything about the stuff. We register a dependence. */
4092   if (!type_decl)
4093     register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4094
4095   ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4096   return class;
4097 }
4098
4099 /* Create a class in pass1 and return its decl. Return class
4100    interface's decl in pass 2.  */
4101
4102 static tree
4103 create_class (int flags, tree id, tree super, tree interfaces)
4104 {
4105   tree raw_name = EXPR_WFL_NODE (id);
4106   tree class_id, decl;
4107   tree super_decl_type;
4108
4109   /* Certain syntax errors are making SUPER be like ID. Avoid this
4110      case. */
4111   if (ctxp->class_err && id == super)
4112     super = NULL;
4113
4114   class_id = parser_qualified_classname (raw_name);
4115   decl = IDENTIFIER_CLASS_VALUE (class_id);
4116   EXPR_WFL_NODE (id) = class_id;
4117
4118   /* Basic check: scope, redefinition, modifiers */
4119   if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4120     {
4121       PUSH_ERROR ();
4122       return NULL_TREE;
4123     }
4124
4125   /* Suspend the current parsing context if we're parsing an inner
4126      class or an anonymous class. */
4127   if (CPC_INNER_P ())
4128     {
4129       java_parser_context_suspend ();
4130       /* Interface members are public. */
4131       if (CLASS_INTERFACE (GET_CPC ()))
4132         flags |= ACC_PUBLIC;
4133     }
4134
4135   /* Push a new context for (static) initialized upon declaration fields */
4136   java_parser_context_push_initialized_field ();
4137
4138   /* Class modifier check:
4139        - Allowed modifier (already done at that point)
4140        - abstract AND final forbidden
4141        - Public classes defined in the correct file */
4142   if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4143     parse_error_context
4144       (id, "Class %qs can't be declared both abstract and final",
4145        IDENTIFIER_POINTER (raw_name));
4146
4147   /* Create a new decl if DECL is NULL, otherwise fix it */
4148   decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4149
4150   /* If SUPER exists, use it, otherwise use Object */
4151   if (super)
4152     {
4153       /* java.lang.Object can't extend anything.  */
4154       if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4155         {
4156           parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4157           return NULL_TREE;
4158         }
4159
4160       super_decl_type =
4161         register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4162     }
4163   else if (TREE_TYPE (decl) != object_type_node)
4164     super_decl_type = object_type_node;
4165   /* We're defining java.lang.Object */
4166   else
4167     super_decl_type = NULL_TREE;
4168
4169   /* A class nested in an interface is implicitly static. */
4170   if (INNER_CLASS_DECL_P (decl)
4171       && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4172     {
4173       flags |= ACC_STATIC;
4174     }
4175
4176   /* Set super info and mark the class as complete. */
4177   set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4178                   ctxp->interface_number);
4179   ctxp->interface_number = 0;
4180   CLASS_COMPLETE_P (decl) = 1;
4181   add_superinterfaces (decl, interfaces);
4182
4183   /* TYPE_VFIELD' is a compiler-generated field used to point to
4184      virtual function tables.  In gcj, every class has a common base
4185      virtual function table in java.lang.object.  */
4186   TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4187
4188   /* We keep the compilation unit imports in the class so that
4189      they can be used later to resolve type dependencies that
4190      aren't necessary to solve now. */
4191   TYPE_IMPORT_LIST (TREE_TYPE (decl)) = ctxp->import_list;
4192   TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (decl)) = ctxp->import_demand_list;
4193
4194   /* Add the private this$<n> field, Replicate final locals still in
4195      scope as private final fields mangled like val$<local_name>.
4196      This does not occur for top level (static) inner classes. */
4197   if (PURE_INNER_CLASS_DECL_P (decl))
4198     add_inner_class_fields (decl, current_function_decl);
4199
4200   /* Eventually sets the @deprecated tag flag */
4201   CHECK_DEPRECATED (decl);
4202
4203   /* Reset the anonymous class counter when declaring non inner classes */
4204   if (!INNER_CLASS_DECL_P (decl))
4205     anonymous_class_counter = 1;
4206
4207   return decl;
4208 }
4209
4210 /* End a class declaration: register the statements used to create
4211    finit$ and <clinit>, pop the current class and resume the prior
4212    parser context if necessary.  */
4213
4214 static void
4215 end_class_declaration (int resume)
4216 {
4217   /* If an error occurred, context weren't pushed and won't need to be
4218      popped by a resume. */
4219   int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4220
4221   if (GET_CPC () != error_mark_node)
4222     dump_java_tree (TDI_class, GET_CPC ());
4223
4224   java_parser_context_pop_initialized_field ();
4225   POP_CPC ();
4226   if (resume && no_error_occurred)
4227     java_parser_context_resume ();
4228
4229   /* We're ending a class declaration, this is a good time to reset
4230      the interface cout. Note that might have been already done in
4231      create_interface, but if at that time an inner class was being
4232      dealt with, the interface count was reset in a context created
4233      for the sake of handling inner classes declaration. */
4234   ctxp->interface_number = 0;
4235 }
4236
4237 static void
4238 add_inner_class_fields (tree class_decl, tree fct_decl)
4239 {
4240   tree block, marker, f;
4241
4242   f = add_field (TREE_TYPE (class_decl),
4243                  build_current_thisn (TREE_TYPE (class_decl)),
4244                  build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4245                  ACC_PRIVATE);
4246   FIELD_THISN (f) = 1;
4247
4248   if (!fct_decl)
4249     return;
4250
4251   for (block = GET_CURRENT_BLOCK (fct_decl);
4252        block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4253     {
4254       tree decl;
4255       for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4256         {
4257           tree name, pname;
4258           tree wfl, init, list;
4259
4260           /* Avoid non final arguments. */
4261           if (!LOCAL_FINAL_P (decl))
4262             continue;
4263
4264           MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4265           MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4266           wfl = build_wfl_node (name);
4267           init = build_wfl_node (pname);
4268           /* Build an initialization for the field: it will be
4269              initialized by a parameter added to finit$, bearing a
4270              mangled name of the field itself (param$<n>.) The
4271              parameter is provided to finit$ by the constructor
4272              invoking it (hence the constructor will also feature a
4273              hidden parameter, set to the value of the outer context
4274              local at the time the inner class is created.)
4275
4276              Note: we take into account all possible locals that can
4277              be accessed by the inner class. It's actually not trivial
4278              to minimize these aliases down to the ones really
4279              used. One way to do that would be to expand all regular
4280              methods first, then finit$ to get a picture of what's
4281              used.  It works with the exception that we would have to
4282              go back on all constructor invoked in regular methods to
4283              have their invocation reworked (to include the right amount
4284              of alias initializer parameters.)
4285
4286              The only real way around, I think, is a first pass to
4287              identify locals really used in the inner class. We leave
4288              the flag FIELD_LOCAL_ALIAS_USED around for that future
4289              use.
4290
4291              On the other hand, it only affect local inner classes,
4292              whose constructors (and finit$ call) will be featuring
4293              unnecessary arguments. It's easy for a developer to keep
4294              this number of parameter down by using the `final'
4295              keyword only when necessary. For the time being, we can
4296              issue a warning on unnecessary finals. FIXME */
4297           init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4298                                    wfl, init);
4299
4300           /* Register the field. The TREE_LIST holding the part
4301              initialized/initializer will be marked ARG_FINAL_P so
4302              that the created field can be marked
4303              FIELD_LOCAL_ALIAS. */
4304           list = build_tree_list (wfl, init);
4305           ARG_FINAL_P (list) = 1;
4306           register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4307         }
4308     }
4309
4310   if (!CPC_INITIALIZER_STMT (ctxp))
4311     return;
4312
4313   /* If we ever registered an alias field, insert and marker to
4314      remember where the list ends. The second part of the list (the one
4315      featuring initialized fields) so it can be later reversed to
4316      enforce 8.5. The marker will be removed during that operation. */
4317   marker = build_tree_list (NULL_TREE, NULL_TREE);
4318   TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4319   SET_CPC_INITIALIZER_STMT (ctxp, marker);
4320 }
4321
4322 /* Can't use lookup_field () since we don't want to load the class and
4323    can't set the CLASS_LOADED_P flag */
4324
4325 static tree
4326 find_field (tree class, tree name)
4327 {
4328   tree decl;
4329   for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4330     {
4331       if (DECL_NAME (decl) == name)
4332         return decl;
4333     }
4334   return NULL_TREE;
4335 }
4336
4337 /* Wrap around lookup_field that doesn't potentially upset the value
4338    of CLASS */
4339
4340 static tree
4341 lookup_field_wrapper (tree class, tree name)
4342 {
4343   tree type = class;
4344   tree decl = NULL_TREE;
4345   java_parser_context_save_global ();
4346
4347   /* Last chance: if we're within the context of an inner class, we
4348      might be trying to access a local variable defined in an outer
4349      context. We try to look for it now. */
4350   if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4351     {
4352       tree new_name;
4353       MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4354       decl = lookup_field (&type, new_name);
4355       if (decl && decl != error_mark_node)
4356         FIELD_LOCAL_ALIAS_USED (decl) = 1;
4357     }
4358   if (!decl || decl == error_mark_node)
4359     {
4360       type = class;
4361       decl = lookup_field (&type, name);
4362     }
4363
4364   /* If the field still hasn't been found, try the next enclosing context. */
4365   if (!decl && INNER_CLASS_TYPE_P (class))
4366     {
4367       tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4368       decl = lookup_field_wrapper (outer_type, name);
4369     }
4370
4371   java_parser_context_restore_global ();
4372   return decl == error_mark_node ? NULL : decl;
4373 }
4374
4375 /* Find duplicate field within the same class declarations and report
4376    the error. Returns 1 if a duplicated field was found, 0
4377    otherwise.  */
4378
4379 static int
4380 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4381 {
4382   /* This might be modified to work with method decl as well */
4383   tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4384   if (decl)
4385     {
4386       char *t1 = xstrdup (purify_type_name
4387                          ((TREE_CODE (new_type) == POINTER_TYPE
4388                            && TREE_TYPE (new_type) == NULL_TREE) ?
4389                           IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4390                           lang_printable_name (new_type, 1)));
4391       /* The type may not have been completed by the time we report
4392          the error */
4393       char *t2 = xstrdup (purify_type_name
4394                          ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4395                            && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4396                           IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4397                           lang_printable_name (TREE_TYPE (decl), 1)));
4398       parse_error_context
4399         (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4400          t1, IDENTIFIER_POINTER (new_field_name),
4401          t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4402          DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4403       free (t1);
4404       free (t2);
4405       return 1;
4406     }
4407   return 0;
4408 }
4409
4410 /* Field registration routine. If TYPE doesn't exist, field
4411    declarations are linked to the undefined TYPE dependency list, to
4412    be later resolved in java_complete_class () */
4413
4414 static void
4415 register_fields (int flags, tree type, tree variable_list)
4416 {
4417   tree current, saved_type;
4418   tree class_type = NULL_TREE;
4419   location_t saved_location = input_location;
4420   int must_chain = 0;
4421   tree wfl = NULL_TREE;
4422
4423   if (GET_CPC ())
4424     class_type = TREE_TYPE (GET_CPC ());
4425
4426   if (!class_type || class_type == error_mark_node)
4427     return;
4428
4429   /* If we're adding fields to interfaces, those fields are public,
4430      static, final */
4431   if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4432     {
4433       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4434                                  flags, ACC_PUBLIC, "interface field(s)");
4435       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4436                                  flags, ACC_STATIC, "interface field(s)");
4437       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4438                                  flags, ACC_FINAL, "interface field(s)");
4439       check_modifiers ("Illegal interface member modifier %qs", flags,
4440                        INTERFACE_FIELD_MODIFIERS);
4441       flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4442     }
4443
4444   /* Obtain a suitable type for resolution, if necessary */
4445   SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4446
4447   /* If TYPE is fully resolved and we don't have a reference, make one */
4448   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4449
4450   for (current = variable_list, saved_type = type; current;
4451        current = TREE_CHAIN (current), type = saved_type)
4452     {
4453       tree real_type;
4454       tree field_decl;
4455       tree cl = TREE_PURPOSE (current);
4456       tree init = TREE_VALUE (current);
4457       tree current_name = EXPR_WFL_NODE (cl);
4458
4459       /* Can't declare non-final static fields in inner classes */
4460       if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4461           && !(flags & ACC_FINAL))
4462         parse_error_context
4463           (cl, "Field %qs can't be static in inner class %qs unless it is final",
4464            IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4465            lang_printable_name (class_type, 0));
4466
4467       /* Process NAME, as it may specify extra dimension(s) for it */
4468       type = build_array_from_name (type, wfl, current_name, &current_name);
4469
4470       /* Type adjustment. We may have just readjusted TYPE because
4471          the variable specified more dimensions. Make sure we have
4472          a reference if we can and don't have one already. Also
4473          change the name if we have an init. */
4474       if (type != saved_type)
4475         {
4476           PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4477           if (init)
4478             EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4479         }
4480
4481       real_type = GET_REAL_TYPE (type);
4482       /* Check for redeclarations */
4483       if (duplicate_declaration_error_p (current_name, real_type, cl))
4484         continue;
4485
4486       /* Set input_line to the line the field was found and create a
4487          declaration for it. Eventually sets the @deprecated tag flag. */
4488 #ifdef USE_MAPPED_LOCATION
4489       input_location = EXPR_LOCATION (cl);
4490 #else
4491       input_line = EXPR_WFL_LINENO (cl);
4492 #endif
4493       field_decl = add_field (class_type, current_name, real_type, flags);
4494       CHECK_DEPRECATED_NO_RESET (field_decl);
4495
4496       /* If the field denotes a final instance variable, then we
4497          allocate a LANG_DECL_SPECIFIC part to keep track of its
4498          initialization. We also mark whether the field was
4499          initialized upon its declaration. We don't do that if the
4500          created field is an alias to a final local. */
4501       if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4502         {
4503           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4504           DECL_FIELD_FINAL_WFL (field_decl) = cl;
4505         }
4506
4507       /* If the couple initializer/initialized is marked ARG_FINAL_P,
4508          we mark the created field FIELD_LOCAL_ALIAS, so that we can
4509          hide parameters to this inner class finit$ and
4510          constructors. It also means that the field isn't final per
4511          say. */
4512       if (ARG_FINAL_P (current))
4513         {
4514           FIELD_LOCAL_ALIAS (field_decl) = 1;
4515           FIELD_FINAL (field_decl) = 0;
4516         }
4517
4518       /* Check if we must chain. */
4519       if (must_chain)
4520         register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4521
4522       /* If we have an initialization value tied to the field */
4523       if (init)
4524         {
4525           /* The field is declared static */
4526           if (flags & ACC_STATIC)
4527             {
4528               /* We include the field and its initialization part into
4529                  a list used to generate <clinit>. After <clinit> is
4530                  walked, field initializations will be processed and
4531                  fields initialized with known constants will be taken
4532                  out of <clinit> and have their DECL_INITIAL set
4533                  appropriately. */
4534               TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4535               SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4536               if (TREE_OPERAND (init, 1)
4537                   && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4538                 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4539             }
4540           /* A non-static field declared with an immediate initialization is
4541              to be initialized in <init>, if any.  This field is remembered
4542              to be processed at the time of the generation of <init>. */
4543           else
4544             {
4545               TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4546               SET_CPC_INITIALIZER_STMT (ctxp, init);
4547             }
4548           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4549           DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4550         }
4551     }
4552
4553   CLEAR_DEPRECATED;
4554   input_location = saved_location;
4555 }
4556
4557 /* Generate finit$, using the list of initialized fields to populate
4558    its body. finit$'s parameter(s) list is adjusted to include the
4559    one(s) used to initialized the field(s) caching outer context
4560    local(s).  */
4561
4562 static tree
4563 generate_finit (tree class_type)
4564 {
4565   int count = 0;
4566   tree list = TYPE_FINIT_STMT_LIST (class_type);
4567   tree mdecl, current, parms;
4568
4569   parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4570                                                   class_type, NULL_TREE,
4571                                                   &count);
4572   CRAFTED_PARAM_LIST_FIXUP (parms);
4573   mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4574                                     finit_identifier_node, parms);
4575   fix_method_argument_names (parms, mdecl);
4576   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4577                        mdecl, NULL_TREE);
4578   DECL_FUNCTION_NAP (mdecl) = count;
4579   start_artificial_method_body (mdecl);
4580
4581   for (current = list; current; current = TREE_CHAIN (current))
4582     java_method_add_stmt (mdecl,
4583                           build_debugable_stmt (EXPR_WFL_LINECOL (current),
4584                                                 current));
4585   end_artificial_method_body (mdecl);
4586   return mdecl;
4587 }
4588
4589 /* Generate a function to run the instance initialization code. The
4590    private method is called `instinit$'. Unless we're dealing with an
4591    anonymous class, we determine whether all ctors of CLASS_TYPE
4592    declare a checked exception in their `throws' clause in order to
4593    see whether it's necessary to encapsulate the instance initializer
4594    statements in a try/catch/rethrow sequence.  */
4595
4596 static tree
4597 generate_instinit (tree class_type)
4598 {
4599   tree current;
4600   tree compound = NULL_TREE;
4601   tree parms = tree_cons (this_identifier_node,
4602                           build_pointer_type (class_type), end_params_node);
4603   tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4604                                          void_type_node,
4605                                          instinit_identifier_node, parms);
4606
4607   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4608                        mdecl, NULL_TREE);
4609
4610   /* Gather all the statements in a compound */
4611   for (current = TYPE_II_STMT_LIST (class_type);
4612        current; current = TREE_CHAIN (current))
4613     compound = add_stmt_to_compound (compound, NULL_TREE, current);
4614
4615   /* We need to encapsulate COMPOUND by a try/catch statement to
4616      rethrow exceptions that might occur in the instance initializer.
4617      We do that only if all ctors of CLASS_TYPE are set to catch a
4618      checked exception. This doesn't apply to anonymous classes (since
4619      they don't have declared ctors.) */
4620   if (!ANONYMOUS_CLASS_P (class_type) &&
4621       ctors_unchecked_throws_clause_p (class_type))
4622     {
4623       compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4624                                              build1 (THROW_EXPR, NULL_TREE,
4625                                                      build_wfl_node (wpv_id)));
4626       DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4627                                                       exception_type_node);
4628     }
4629
4630   start_artificial_method_body (mdecl);
4631   java_method_add_stmt (mdecl, compound);
4632   end_artificial_method_body (mdecl);
4633
4634   return mdecl;
4635 }
4636
4637 /* FIXME */
4638 static tree
4639 build_instinit_invocation (tree class_type)
4640 {
4641   tree to_return = NULL_TREE;
4642
4643   if (TYPE_II_STMT_LIST (class_type))
4644     {
4645       tree parm = build_tree_list (NULL_TREE,
4646                                    build_wfl_node (this_identifier_node));
4647       to_return =
4648         build_method_invocation (build_wfl_node (instinit_identifier_node),
4649                                  parm);
4650     }
4651   return to_return;
4652 }
4653
4654 /* Shared across method_declarator and method_header to remember the
4655    patch stage that was reached during the declaration of the method.
4656    A method DECL is built differently is there is no patch
4657    (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4658    pending on the currently defined method.  */
4659
4660 static int patch_stage;
4661
4662 /* Check the method declaration and add the method to its current
4663    class.  If the argument list is known to contain incomplete types,
4664    the method is partially added and the registration will be resume
4665    once the method arguments resolved. If TYPE is NULL, we're dealing
4666    with a constructor.  */
4667
4668 static tree
4669 method_header (int flags, tree type, tree mdecl, tree throws)
4670 {
4671   tree type_wfl = NULL_TREE;
4672   tree meth_name = NULL_TREE;
4673   tree current, orig_arg, this_class = NULL;
4674   tree id, meth;
4675   location_t saved_location;
4676   int constructor_ok = 0, must_chain;
4677   int count;
4678
4679   if (mdecl == error_mark_node)
4680     return error_mark_node;
4681   meth = TREE_VALUE (mdecl);
4682   id = TREE_PURPOSE (mdecl);
4683
4684   check_modifiers_consistency (flags);
4685
4686   if (GET_CPC ())
4687     this_class = TREE_TYPE (GET_CPC ());
4688
4689   if (!this_class || this_class == error_mark_node)
4690     return NULL_TREE;
4691
4692   /* There are some forbidden modifiers for an abstract method and its
4693      class must be abstract as well.  */
4694   if (type && (flags & ACC_ABSTRACT))
4695     {
4696       ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4697       ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4698       ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4699       ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4700       ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4701       ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4702       if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4703           && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4704         parse_error_context
4705           (id,
4706            "Class %qs must be declared abstract to define abstract method %qs",
4707            IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4708            IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4709     }
4710
4711   /* A native method can't be strictfp.  */
4712   if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4713     parse_error_context (id, "native method %qs can't be strictfp",
4714                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4715   /* No such thing as a transient or volatile method.  */
4716   if ((flags & ACC_TRANSIENT))
4717     parse_error_context (id, "method %qs can't be transient",
4718                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4719   if ((flags & ACC_VOLATILE))
4720     parse_error_context (id, "method %qs can't be volatile",
4721                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4722
4723   /* Things to be checked when declaring a constructor */
4724   if (!type)
4725     {
4726       int ec = java_error_count;
4727       /* 8.6: Constructor declarations: we might be trying to define a
4728          method without specifying a return type. */
4729       if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4730         parse_error_context
4731           (id, "Invalid method declaration, return type required");
4732       /* 8.6.3: Constructor modifiers */
4733       else
4734         {
4735           JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4736           JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4737           JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4738           JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4739           JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4740           JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4741         }
4742       /* If we found error here, we don't consider it's OK to tread
4743          the method definition as a constructor, for the rest of this
4744          function */
4745       if (ec == java_error_count)
4746         constructor_ok = 1;
4747     }
4748
4749   /* Method declared within the scope of an interface are implicitly
4750      abstract and public. Conflicts with other erroneously provided
4751      modifiers are checked right after. */
4752
4753   if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4754     {
4755       /* If FLAGS isn't set because of a modifier, turn the
4756          corresponding modifier WFL to NULL so we issue a warning on
4757          the obsolete use of the modifier */
4758       if (!(flags & ACC_PUBLIC))
4759         MODIFIER_WFL (PUBLIC_TK) = NULL;
4760       if (!(flags & ACC_ABSTRACT))
4761         MODIFIER_WFL (ABSTRACT_TK) = NULL;
4762       flags |= ACC_PUBLIC;
4763       flags |= ACC_ABSTRACT;
4764     }
4765
4766   /* Inner class can't declare static methods */
4767   if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4768     {
4769       parse_error_context
4770         (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4771          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4772          lang_printable_name (this_class, 0));
4773     }
4774
4775   /* Modifiers context reset moved up, so abstract method declaration
4776      modifiers can be later checked.  */
4777
4778   /* Set constructor returned type to void and method name to <init>,
4779      unless we found an error identifier the constructor (in which
4780      case we retain the original name) */
4781   if (!type)
4782     {
4783       type = void_type_node;
4784       if (constructor_ok)
4785         meth_name = init_identifier_node;
4786     }
4787   else
4788     meth_name = EXPR_WFL_NODE (id);
4789
4790   /* Do the returned type resolution and registration if necessary */
4791   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4792
4793   if (meth_name)
4794     type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4795   EXPR_WFL_NODE (id) = meth_name;
4796   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4797
4798   if (must_chain)
4799     {
4800       patch_stage = JDEP_METHOD_RETURN;
4801       register_incomplete_type (patch_stage, type_wfl, id, type);
4802       TREE_TYPE (meth) = GET_REAL_TYPE (type);
4803     }
4804   else
4805     TREE_TYPE (meth) = type;
4806
4807   saved_location = input_location;
4808   /* When defining an abstract or interface method, the curly
4809      bracket at level 1 doesn't exist because there is no function
4810      body */
4811 #ifdef USE_MAPPED_LOCATION
4812   input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4813                     EXPR_LOCATION (id));
4814 #else
4815   input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4816                 EXPR_WFL_LINENO (id));
4817 #endif
4818
4819   /* Remember the original argument list */
4820   orig_arg = TYPE_ARG_TYPES (meth);
4821
4822   if (patch_stage)              /* includes ret type and/or all args */
4823     {
4824       jdep *jdep;
4825       meth = add_method_1 (this_class, flags, meth_name, meth);
4826       /* Patch for the return type */
4827       if (patch_stage == JDEP_METHOD_RETURN)
4828         {
4829           jdep = CLASSD_LAST (ctxp->classd_list);
4830           JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4831         }
4832       /* This is the stop JDEP. METH allows the function's signature
4833          to be computed. */
4834       register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4835     }
4836   else
4837     meth = add_method (this_class, flags, meth_name,
4838                        build_java_signature (meth));
4839
4840   /* Remember final parameters */
4841   MARK_FINAL_PARMS (meth, orig_arg);
4842
4843   /* Fix the method argument list so we have the argument name
4844      information */
4845   fix_method_argument_names (orig_arg, meth);
4846
4847   /* Register the parameter number and re-install the current line
4848      number */
4849   DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4850   input_location = saved_location;
4851
4852   /* Register exception specified by the `throws' keyword for
4853      resolution and set the method decl appropriate field to the list.
4854      Note: the grammar ensures that what we get here are class
4855      types. */
4856   if (throws)
4857     {
4858       throws = nreverse (throws);
4859       for (current = throws; current; current = TREE_CHAIN (current))
4860         {
4861           register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4862                                     NULL_TREE, NULL_TREE);
4863           JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4864             &TREE_VALUE (current);
4865         }
4866       DECL_FUNCTION_THROWS (meth) = throws;
4867     }
4868
4869   if (TREE_TYPE (GET_CPC ()) != object_type_node)
4870     DECL_FUNCTION_WFL (meth) = id;
4871
4872   /* Set the flag if we correctly processed a constructor */
4873   if (constructor_ok)
4874     {
4875       DECL_CONSTRUCTOR_P (meth) = 1;
4876       /* Compute and store the number of artificial parameters declared
4877          for this constructor */
4878       for (count = 0, current = TYPE_FIELDS (this_class); current;
4879            current = TREE_CHAIN (current))
4880         if (FIELD_LOCAL_ALIAS (current))
4881           count++;
4882       DECL_FUNCTION_NAP (meth) = count;
4883     }
4884
4885   /* Eventually set the @deprecated tag flag */
4886   CHECK_DEPRECATED (meth);
4887
4888   return meth;
4889 }
4890
4891 static void
4892 fix_method_argument_names (tree orig_arg, tree meth)
4893 {
4894   tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4895   if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4896     {
4897       TREE_PURPOSE (arg) = this_identifier_node;
4898       arg = TREE_CHAIN (arg);
4899     }
4900   while (orig_arg != end_params_node)
4901     {
4902       TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4903       orig_arg = TREE_CHAIN (orig_arg);
4904       arg = TREE_CHAIN (arg);
4905     }
4906 }
4907
4908 /* Complete the method declaration with METHOD_BODY.  */
4909
4910 static void
4911 finish_method_declaration (tree method_body)
4912 {
4913   int flags;
4914
4915   if (!current_function_decl)
4916     return;
4917
4918   flags = get_access_flags_from_decl (current_function_decl);
4919
4920   /* 8.4.5 Method Body */
4921   if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4922     {
4923       tree name = DECL_NAME (current_function_decl);
4924       parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4925                            "%s method %qs can't have a body defined",
4926                            (METHOD_NATIVE (current_function_decl) ?
4927                             "Native" : "Abstract"),
4928                            IDENTIFIER_POINTER (name));
4929       method_body = NULL_TREE;
4930     }
4931   else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4932     {
4933       tree name = DECL_NAME (current_function_decl);
4934       parse_error_context
4935         (DECL_FUNCTION_WFL (current_function_decl),
4936          "Non native and non abstract method %qs must have a body defined",
4937          IDENTIFIER_POINTER (name));
4938       method_body = NULL_TREE;
4939     }
4940
4941   if (flag_emit_class_files && method_body
4942       && TREE_CODE (method_body) == NOP_EXPR
4943       && TREE_TYPE (current_function_decl)
4944       && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4945     method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4946
4947   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4948   maybe_absorb_scoping_blocks ();
4949   /* Exit function's body */
4950   exit_block ();
4951   /* Merge last line of the function with first line, directly in the
4952      function decl. It will be used to emit correct debug info. */
4953   DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4954
4955   /* Since function's argument's list are shared, reset the
4956      ARG_FINAL_P parameter that might have been set on some of this
4957      function parameters. */
4958   UNMARK_FINAL_PARMS (current_function_decl);
4959
4960   /* So we don't have an irrelevant function declaration context for
4961      the next static block we'll see. */
4962   current_function_decl = NULL_TREE;
4963 }
4964
4965 /* Build a an error message for constructor circularity errors.  */
4966
4967 static char *
4968 constructor_circularity_msg (tree from, tree to)
4969 {
4970   static char string [4096];
4971   char *t = xstrdup (lang_printable_name (from, 2));
4972   sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4973   free (t);
4974   return string;
4975 }
4976
4977 /* Verify a circular call to METH. Return 1 if an error is found, 0
4978    otherwise.  */
4979
4980 static GTY(()) tree vcc_list;
4981 static int
4982 verify_constructor_circularity (tree meth, tree current)
4983 {
4984   tree c;
4985
4986   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4987     {
4988       if (TREE_VALUE (c) == meth)
4989         {
4990           char *t;
4991           if (vcc_list)
4992             {
4993               tree liste;
4994               vcc_list = nreverse (vcc_list);
4995               for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4996                 {
4997                   parse_error_context
4998                     (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4999                      constructor_circularity_msg
5000                       (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
5001                   java_error_count--;
5002                 }
5003             }
5004           t = xstrdup (lang_printable_name (meth, 2));
5005           parse_error_context (TREE_PURPOSE (c),
5006                                "%s: recursive invocation of constructor %qs",
5007                                constructor_circularity_msg (current, meth), t);
5008           free (t);
5009           vcc_list = NULL_TREE;
5010           return 1;
5011         }
5012     }
5013   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5014     {
5015       vcc_list = tree_cons (c, current, vcc_list);
5016       if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5017         return 1;
5018       vcc_list = TREE_CHAIN (vcc_list);
5019     }
5020   return 0;
5021 }
5022
5023 /* Check modifiers that can be declared but exclusively */
5024
5025 static void
5026 check_modifiers_consistency (int flags)
5027 {
5028   int acc_count = 0;
5029   tree cl = NULL_TREE;
5030
5031   THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5032   THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5033   THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5034   if (acc_count > 1)
5035     parse_error_context
5036       (cl, "Inconsistent member declaration.  At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5037
5038   acc_count = 0;
5039   cl = NULL_TREE;
5040   THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5041   THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5042   if (acc_count > 1)
5043     parse_error_context (cl,
5044                          "Inconsistent member declaration.  At most one of %<final%> or %<volatile%> may be specified");
5045 }
5046
5047 /* Check the methode header METH for abstract specifics features */
5048
5049 static void
5050 check_abstract_method_header (tree meth)
5051 {
5052   int flags = get_access_flags_from_decl (meth);
5053
5054   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5055                               ACC_ABSTRACT, "abstract method",
5056                               IDENTIFIER_POINTER (DECL_NAME (meth)));
5057   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5058                               ACC_PUBLIC, "abstract method",
5059                               IDENTIFIER_POINTER (DECL_NAME (meth)));
5060
5061   check_modifiers ("Illegal modifier %qs for interface method",
5062                   flags, INTERFACE_METHOD_MODIFIERS);
5063 }
5064
5065 /* Create a FUNCTION_TYPE node and start augmenting it with the
5066    declared function arguments. Arguments type that can't be resolved
5067    are left as they are, but the returned node is marked as containing
5068    incomplete types.  */
5069
5070 static tree
5071 method_declarator (tree id, tree list)
5072 {
5073   tree arg_types = NULL_TREE, current, node;
5074   tree meth = make_node (FUNCTION_TYPE);
5075   jdep *jdep;
5076
5077   patch_stage = JDEP_NO_PATCH;
5078
5079   if (GET_CPC () == error_mark_node)
5080     return error_mark_node;
5081
5082   /* If we're dealing with an inner class constructor, we hide the
5083      this$<n> decl in the name field of its parameter declaration.  We
5084      also might have to hide the outer context local alias
5085      initializers. Not done when the class is a toplevel class. */
5086   if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5087       && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5088     {
5089       tree aliases_list, type, thisn;
5090       /* First the aliases, linked to the regular parameters */
5091       aliases_list =
5092         build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5093                                                 TREE_TYPE (GET_CPC ()),
5094                                                 NULL_TREE, NULL);
5095       list = chainon (nreverse (aliases_list), list);
5096
5097       /* Then this$<n> */
5098       type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5099       thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5100       list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5101                         list);
5102     }
5103
5104   for (current = list; current; current = TREE_CHAIN (current))
5105     {
5106       int must_chain = 0;
5107       tree wfl_name = TREE_PURPOSE (current);
5108       tree type = TREE_VALUE (current);
5109       tree name = EXPR_WFL_NODE (wfl_name);
5110       tree already, arg_node;
5111       tree type_wfl = NULL_TREE;
5112       tree real_type;
5113
5114       /* Obtain a suitable type for resolution, if necessary */
5115       SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5116
5117       /* Process NAME, as it may specify extra dimension(s) for it */
5118       type = build_array_from_name (type, type_wfl, name, &name);
5119       EXPR_WFL_NODE (wfl_name) = name;
5120
5121       real_type = GET_REAL_TYPE (type);
5122       if (TREE_CODE (real_type) == RECORD_TYPE)
5123         {
5124           real_type = promote_type (real_type);
5125           if (TREE_CODE (type) == TREE_LIST)
5126             TREE_PURPOSE (type) = real_type;
5127         }
5128
5129       /* Check redefinition */
5130       for (already = arg_types; already; already = TREE_CHAIN (already))
5131         if (TREE_PURPOSE (already) == name)
5132           {
5133             parse_error_context
5134               (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5135                IDENTIFIER_POINTER (name),
5136                IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5137             break;
5138           }
5139
5140       /* If we've an incomplete argument type, we know there is a location
5141          to patch when the type get resolved, later.  */
5142       jdep = NULL;
5143       if (must_chain)
5144         {
5145           patch_stage = JDEP_METHOD;
5146           type = register_incomplete_type (patch_stage,
5147                                            type_wfl, wfl_name, type);
5148           jdep = CLASSD_LAST (ctxp->classd_list);
5149           JDEP_MISC (jdep) = id;
5150         }
5151
5152       /* The argument node: a name and a (possibly) incomplete type.  */
5153       arg_node = build_tree_list (name, real_type);
5154       /* Remember arguments declared final. */
5155       ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5156
5157       if (jdep)
5158         JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5159       TREE_CHAIN (arg_node) = arg_types;
5160       arg_types = arg_node;
5161     }
5162   TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5163   node = build_tree_list (id, meth);
5164   return node;
5165 }
5166
5167 static int
5168 unresolved_type_p (tree wfl, tree *returned)
5169 {
5170   if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5171     {
5172       if (returned)
5173         {
5174           tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5175           if (decl && current_class && (decl == TYPE_NAME (current_class)))
5176             *returned = TREE_TYPE (decl);
5177           else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5178             *returned = TREE_TYPE (GET_CPC ());
5179           else
5180             *returned = NULL_TREE;
5181         }
5182       return 1;
5183     }
5184   if (returned)
5185     *returned = wfl;
5186   return 0;
5187 }
5188
5189 /* From NAME, build a qualified identifier node using the
5190    qualification from the current package definition. */
5191
5192 static tree
5193 parser_qualified_classname (tree name)
5194 {
5195   tree nested_class_name;
5196
5197   if ((nested_class_name = maybe_make_nested_class_name (name)))
5198     return nested_class_name;
5199
5200   if (ctxp->package)
5201     return merge_qualified_name (ctxp->package, name);
5202   else
5203     return name;
5204 }
5205
5206 /* Called once the type a interface extends is resolved. Returns 0 if
5207    everything is OK.  */
5208
5209 static int
5210 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5211 {
5212   tree super_type = TREE_TYPE (super_decl);
5213
5214   /* Has to be an interface */
5215   if (!CLASS_INTERFACE (super_decl))
5216     {
5217       parse_error_context
5218         (this_wfl, "%s %qs can't implement/extend %s %qs",
5219          (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5220           "Interface" : "Class"),
5221          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5222          (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5223          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5224       return 1;
5225     }
5226
5227   /* Check top-level interface access. Inner classes are subject to member
5228      access rules (6.6.1). */
5229   if (! INNER_CLASS_P (super_type)
5230       && check_pkg_class_access (DECL_NAME (super_decl),
5231                                  NULL_TREE, true, this_decl))
5232     return 1;
5233
5234   SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5235                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5236                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5237   return 0;
5238 }
5239
5240 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5241    0 if everything is OK.  */
5242
5243 static int
5244 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5245 {
5246   tree super_type = TREE_TYPE (super_decl);
5247
5248   /* SUPER should be a CLASS (neither an array nor an interface) */
5249   if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5250     {
5251       parse_error_context
5252         (wfl, "Class %qs can't subclass %s %qs",
5253          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5254          (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5255          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5256       return 1;
5257     }
5258
5259   if (CLASS_FINAL (TYPE_NAME (super_type)))
5260     {
5261       parse_error_context (wfl, "Can't subclass final classes: %s",
5262                            IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5263       return 1;
5264     }
5265
5266   /* Check top-level class scope. Inner classes are subject to member access
5267      rules (6.6.1). */
5268   if (! INNER_CLASS_P (super_type)
5269       && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5270     return 1;
5271
5272   SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5273                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5274                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5275   return 0;
5276 }
5277
5278 /* Create a new dependency list and link it (in a LIFO manner) to the
5279    CTXP list of type dependency list.  */
5280
5281 static void
5282 create_jdep_list (struct parser_ctxt *ctxp)
5283 {
5284   jdeplist *new = xmalloc (sizeof (jdeplist));
5285   new->first = new->last = NULL;
5286   new->next = ctxp->classd_list;
5287   ctxp->classd_list = new;
5288 }
5289
5290 static jdeplist *
5291 reverse_jdep_list (struct parser_ctxt *ctxp)
5292 {
5293   jdeplist *prev = NULL, *current, *next;
5294   for (current = ctxp->classd_list; current; current = next)
5295     {
5296       next = current->next;
5297       current->next = prev;
5298       prev = current;
5299     }
5300   return prev;
5301 }
5302
5303 /* Create a fake pointer based on the ID stored in
5304    TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5305    registered again. */
5306
5307 static tree
5308 obtain_incomplete_type (tree type_name)
5309 {
5310   tree ptr = NULL_TREE, name;
5311
5312   if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5313     name = EXPR_WFL_NODE (type_name);
5314   else if (INCOMPLETE_TYPE_P (type_name))
5315     name = TYPE_NAME (type_name);
5316   else
5317     abort ();
5318
5319   /* Workaround from build_pointer_type for incomplete types.  */
5320   BUILD_PTR_FROM_NAME (ptr, name);
5321   TYPE_MODE (ptr) = ptr_mode;
5322   layout_type (ptr);
5323
5324   return ptr;
5325 }
5326
5327 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5328    non NULL instead of computing a new fake type based on WFL. The new
5329    dependency is inserted in the current type dependency list, in FIFO
5330    manner.  */
5331
5332 static tree
5333 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5334 {
5335   jdep *new = xmalloc (sizeof (jdep));
5336
5337   if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5338     ptr = obtain_incomplete_type (wfl);
5339
5340   JDEP_KIND (new) = kind;
5341   JDEP_DECL (new) = decl;
5342   JDEP_TO_RESOLVE (new) = ptr;
5343   JDEP_WFL (new) = wfl;
5344   JDEP_CHAIN (new) = NULL;
5345   JDEP_MISC (new) = NULL_TREE;
5346   /* For some dependencies, set the enclosing class of the current
5347      class to be the enclosing context */
5348   if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5349       && GET_ENCLOSING_CPC ())
5350     JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5351   else
5352     JDEP_ENCLOSING (new) = GET_CPC ();
5353   JDEP_GET_PATCH (new) = (tree *)NULL;
5354
5355   JDEP_INSERT (ctxp->classd_list, new);
5356
5357   return ptr;
5358 }
5359
5360 /* This checks for circular references with innerclasses. We start
5361    from SOURCE and should never reach TARGET. Extended/implemented
5362    types in SOURCE have their enclosing context checked not to reach
5363    TARGET. When the last enclosing context of SOURCE is reached, its
5364    extended/implemented types are also checked not to reach TARGET.
5365    In case of error, WFL of the offending type is returned; NULL_TREE
5366    otherwise.  */
5367
5368 static tree
5369 check_inner_circular_reference (tree source, tree target)
5370 {
5371   tree base_binfo;
5372   tree ctx, cl;
5373   int i;
5374
5375   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5376     {
5377       tree su;
5378
5379       /* We can end up with a NULL_TREE or an incomplete type here if
5380          we encountered previous type resolution errors. It's safe to
5381          simply ignore these cases.  */
5382       su = BINFO_TYPE (base_binfo);
5383       if (INCOMPLETE_TYPE_P (su))
5384         continue;
5385
5386       if (inherits_from_p (su, target))
5387         return lookup_cl (TYPE_NAME (su));
5388
5389       for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5390         {
5391           /* An enclosing context shouldn't be TARGET */
5392           if (ctx == TYPE_NAME (target))
5393             return lookup_cl (TYPE_NAME (su));
5394
5395           /* When we reach the enclosing last context, start a check
5396              on it, with the same target */
5397           if (! DECL_CONTEXT (ctx) &&
5398               (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5399             return cl;
5400         }
5401     }
5402   return NULL_TREE;
5403 }
5404
5405 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5406    offending type if a circularity is detected. NULL_TREE is returned
5407    otherwise. TYPE can be an interface or a class.   */
5408
5409 static tree
5410 check_circular_reference (tree type)
5411 {
5412   tree base_binfo;
5413   int i;
5414
5415   if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5416     return NULL_TREE;
5417
5418   if (! CLASS_INTERFACE (TYPE_NAME (type)))
5419     {
5420       if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5421         return lookup_cl (TYPE_NAME (type));
5422       return NULL_TREE;
5423     }
5424
5425   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5426     {
5427       if (BINFO_TYPE (base_binfo) != object_type_node
5428           && interface_of_p (type, BINFO_TYPE (base_binfo)))
5429         return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5430     }
5431   return NULL_TREE;
5432 }
5433
5434 void
5435 java_check_circular_reference (void)
5436 {
5437   tree current;
5438   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5439     {
5440       tree type = TREE_TYPE (current);
5441       tree cl;
5442
5443       cl = check_circular_reference (type);
5444       if (! cl)
5445         cl = check_inner_circular_reference (type, type);
5446       if (cl)
5447         parse_error_context (cl, "Cyclic class inheritance%s",
5448                              (cyclic_inheritance_report ?
5449                               cyclic_inheritance_report : ""));
5450     }
5451 }
5452
5453 /* Augment the parameter list PARM with parameters crafted to
5454    initialize outer context locals aliases. Through ARTIFICIAL, a
5455    count is kept of the number of crafted parameters. MODE governs
5456    what eventually gets created: something suitable for a function
5457    creation or a function invocation, either the constructor or
5458    finit$.  */
5459
5460 static tree
5461 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5462                                         int *artificial)
5463 {
5464   tree field;
5465   tree additional_parms = NULL_TREE;
5466
5467   for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5468     if (FIELD_LOCAL_ALIAS (field))
5469       {
5470         const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5471         tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5472         tree mangled_id;
5473
5474         switch (mode)
5475           {
5476           case AIPL_FUNCTION_DECLARATION:
5477             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5478                                                          &buffer [4]);
5479             purpose = build_wfl_node (mangled_id);
5480             if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5481               value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5482             else
5483               value = TREE_TYPE (field);
5484             break;
5485
5486           case AIPL_FUNCTION_CREATION:
5487             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5488                                                          &buffer [4]);
5489             value = TREE_TYPE (field);
5490             break;
5491
5492           case AIPL_FUNCTION_FINIT_INVOCATION:
5493             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5494                                                          &buffer [4]);
5495             /* Now, this is wrong. purpose should always be the NAME
5496                of something and value its matching value (decl, type,
5497                etc...) FIXME -- but there is a lot to fix. */
5498
5499             /* When invoked for this kind of operation, we already
5500                know whether a field is used or not. */
5501             purpose = TREE_TYPE (field);
5502             value = build_wfl_node (mangled_id);
5503             break;
5504
5505           case AIPL_FUNCTION_CTOR_INVOCATION:
5506             /* There are two case: the constructor invocation happens
5507                outside the local inner, in which case, locales from the outer
5508                context are directly used.
5509
5510                Otherwise, we fold to using the alias directly. */
5511             if (class_type == current_class)
5512               value = field;
5513             else
5514               {
5515                 name = get_identifier (&buffer[4]);
5516                 value = IDENTIFIER_LOCAL_VALUE (name);
5517               }
5518             break;
5519           }
5520         additional_parms = tree_cons (purpose, value, additional_parms);
5521         if (artificial)
5522           *artificial +=1;
5523       }
5524   if (additional_parms)
5525     {
5526       if (ANONYMOUS_CLASS_P (class_type)
5527           && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5528         additional_parms = nreverse (additional_parms);
5529       parm = chainon (additional_parms, parm);
5530     }
5531
5532    return parm;
5533 }
5534
5535 /* Craft a constructor for CLASS_DECL -- what we should do when none
5536    where found. ARGS is non NULL when a special signature must be
5537    enforced. This is the case for anonymous classes.  */
5538
5539 static tree
5540 craft_constructor (tree class_decl, tree args)
5541 {
5542   tree class_type = TREE_TYPE (class_decl);
5543   tree parm = NULL_TREE;
5544   /* Inherit access flags for the constructor from its enclosing class. */
5545   int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5546   int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5547   int i = 0, artificial = 0;
5548   tree decl, ctor_name;
5549   char buffer [80];
5550
5551   ctor_name = init_identifier_node;
5552
5553   /* If we're dealing with an inner class constructor, we hide the
5554      this$<n> decl in the name field of its parameter declaration. */
5555   if (PURE_INNER_CLASS_TYPE_P (class_type))
5556     {
5557       tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5558       parm = tree_cons (build_current_thisn (class_type),
5559                         build_pointer_type (type), parm);
5560
5561       /* Some more arguments to be hidden here. The values of the local
5562          variables of the outer context that the inner class needs to see. */
5563       parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5564                                                      class_type, parm,
5565                                                      &artificial);
5566     }
5567
5568   /* Then if there are any args to be enforced, enforce them now */
5569   for (; args && args != end_params_node; args = TREE_CHAIN (args))
5570     {
5571       /* If we see a `void *', we need to change it to Object.  */
5572       if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5573         TREE_VALUE (args) = object_ptr_type_node;
5574
5575       sprintf (buffer, "parm%d", i++);
5576       parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5577     }
5578
5579   CRAFTED_PARAM_LIST_FIXUP (parm);
5580   decl = create_artificial_method (class_type, flags, void_type_node,
5581                                    ctor_name, parm);
5582   fix_method_argument_names (parm, decl);
5583   /* Now, mark the artificial parameters. */
5584   DECL_FUNCTION_NAP (decl) = artificial;
5585   DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5586   DECL_INLINE (decl) = 1;
5587   return decl;
5588 }
5589
5590
5591 /* Fix the constructors. This will be called right after circular
5592    references have been checked. It is necessary to fix constructors
5593    early even if no code generation will take place for that class:
5594    some generated constructor might be required by the class whose
5595    compilation triggered this one to be simply loaded.  */
5596
5597 void
5598 java_fix_constructors (void)
5599 {
5600   tree current;
5601
5602   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5603     {
5604       tree class_type = TREE_TYPE (current);
5605       int saw_ctor = 0;
5606       tree decl;
5607
5608       if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5609         continue;
5610
5611       output_class = current_class = class_type;
5612       for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5613         {
5614           if (DECL_CONSTRUCTOR_P (decl))
5615             {
5616               fix_constructors (decl);
5617               saw_ctor = 1;
5618             }
5619         }
5620
5621       /* Anonymous class constructor can't be generated that early. */
5622       if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5623         craft_constructor (current, NULL_TREE);
5624     }
5625 }
5626
5627 /* safe_layout_class just makes sure that we can load a class without
5628    disrupting the current_class, input_file, input_line, etc, information
5629    about the class processed currently.  */
5630
5631 void
5632 safe_layout_class (tree class)
5633 {
5634   tree save_current_class = current_class;
5635   location_t save_location = input_location;
5636
5637   layout_class (class);
5638
5639   current_class = save_current_class;
5640   input_location = save_location;
5641 }
5642
5643 static tree
5644 jdep_resolve_class (jdep *dep)
5645 {
5646   tree decl;
5647
5648   if (JDEP_RESOLVED_P (dep))
5649     decl = JDEP_RESOLVED_DECL (dep);
5650   else
5651     {
5652       decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5653                             JDEP_DECL (dep), JDEP_WFL (dep));
5654       JDEP_RESOLVED (dep, decl);
5655       /* If there is no WFL, that's ok.  We generate this warning
5656          elsewhere.  */
5657       if (decl && JDEP_WFL (dep) != NULL_TREE)
5658         check_deprecation (JDEP_WFL (dep), decl);
5659     }
5660
5661   if (!decl)
5662     complete_class_report_errors (dep);
5663   else if (INNER_CLASS_DECL_P (decl))
5664     {
5665       tree inner = TREE_TYPE (decl);
5666       if (! CLASS_LOADED_P (inner))
5667         {
5668           safe_layout_class (inner);
5669           if (TYPE_SIZE (inner) == error_mark_node)
5670             TYPE_SIZE (inner) = NULL_TREE;
5671         }
5672       check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5673     }
5674   return decl;
5675 }
5676
5677 /* Complete unsatisfied class declaration and their dependencies */
5678
5679 void
5680 java_complete_class (void)
5681 {
5682   tree cclass;
5683   jdeplist *cclassd;
5684   int error_found;
5685   tree type;
5686
5687   /* Process imports */
5688   process_imports ();
5689
5690   /* Reverse things so we have the right order */
5691   ctxp->class_list = nreverse (ctxp->class_list);
5692   ctxp->classd_list = reverse_jdep_list (ctxp);
5693
5694   for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5695        cclass && cclassd;
5696        cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5697     {
5698       jdep *dep;
5699
5700       for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5701         {
5702           tree decl;
5703           if (!(decl = jdep_resolve_class (dep)))
5704             continue;
5705
5706           /* Now it's time to patch */
5707           switch (JDEP_KIND (dep))
5708             {
5709             case JDEP_SUPER:
5710               /* Simply patch super */
5711               if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5712                 continue;
5713               BINFO_TYPE (BINFO_BASE_BINFO
5714                           (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5715                 = TREE_TYPE (decl);
5716               break;
5717
5718             case JDEP_FIELD:
5719               {
5720                 /* We do part of the job done in add_field */
5721                 tree field_decl = JDEP_DECL (dep);
5722                 tree field_type = TREE_TYPE (decl);
5723                 if (TREE_CODE (field_type) == RECORD_TYPE)
5724                   field_type = promote_type (field_type);
5725                 TREE_TYPE (field_decl) = field_type;
5726                 DECL_ALIGN (field_decl) = 0;
5727                 DECL_USER_ALIGN (field_decl) = 0;
5728                 layout_decl (field_decl, 0);
5729                 SOURCE_FRONTEND_DEBUG
5730                   (("Completed field/var decl '%s' with '%s'",
5731                     IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5732                     IDENTIFIER_POINTER (DECL_NAME (decl))));
5733                 break;
5734               }
5735             case JDEP_METHOD:   /* We start patching a method */
5736             case JDEP_METHOD_RETURN:
5737               error_found = 0;
5738               while (1)
5739                 {
5740                   if (decl)
5741                     {
5742                       type = TREE_TYPE(decl);
5743                       if (TREE_CODE (type) == RECORD_TYPE)
5744                         type = promote_type (type);
5745                       JDEP_APPLY_PATCH (dep, type);
5746                       SOURCE_FRONTEND_DEBUG
5747                         (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5748                            "Completing fct '%s' with ret type '%s'":
5749                            "Completing arg '%s' with type '%s'"),
5750                           IDENTIFIER_POINTER (EXPR_WFL_NODE
5751                                               (JDEP_DECL_WFL (dep))),
5752                           IDENTIFIER_POINTER (DECL_NAME (decl))));
5753                     }
5754                   else
5755                     error_found = 1;
5756                   dep = JDEP_CHAIN (dep);
5757                   if (JDEP_KIND (dep) == JDEP_METHOD_END)
5758                     break;
5759                   else
5760                     decl = jdep_resolve_class (dep);
5761                 }
5762               if (!error_found)
5763                 {
5764                   tree mdecl = JDEP_DECL (dep), signature;
5765                   /* Recompute and reset the signature, check first that
5766                      all types are now defined. If they're not,
5767                      don't build the signature. */
5768                   if (check_method_types_complete (mdecl))
5769                     {
5770                       signature = build_java_signature (TREE_TYPE (mdecl));
5771                       set_java_signature (TREE_TYPE (mdecl), signature);
5772                     }
5773                 }
5774               else
5775                 continue;
5776               break;
5777
5778             case JDEP_INTERFACE:
5779               if (parser_check_super_interface (decl, JDEP_DECL (dep),
5780                                                 JDEP_WFL (dep)))
5781                 continue;
5782               parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5783               break;
5784
5785             case JDEP_PARM:
5786             case JDEP_VARIABLE:
5787               type = TREE_TYPE(decl);
5788               if (TREE_CODE (type) == RECORD_TYPE)
5789                 type = promote_type (type);
5790               JDEP_APPLY_PATCH (dep, type);
5791               break;
5792
5793             case JDEP_TYPE:
5794               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5795               SOURCE_FRONTEND_DEBUG
5796                 (("Completing a random type dependency on a '%s' node",
5797                   tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5798               break;
5799
5800             case JDEP_EXCEPTION:
5801               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5802               SOURCE_FRONTEND_DEBUG
5803                 (("Completing '%s' 'throws' argument node",
5804                   IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5805               break;
5806
5807             case JDEP_ANONYMOUS:
5808               patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5809               break;
5810
5811             default:
5812               abort ();
5813             }
5814         }
5815     }
5816   return;
5817 }
5818
5819 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5820    array.  */
5821
5822 static tree
5823 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5824 {
5825   tree tname = TYPE_NAME (class_type);
5826   tree resolved_type = TREE_TYPE (class_type);
5827   int array_dims = 0;
5828   tree resolved_type_decl;
5829
5830   if (resolved_type != NULL_TREE)
5831     {
5832       tree resolved_type_decl = TYPE_NAME (resolved_type);
5833       if (resolved_type_decl == NULL_TREE
5834           || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5835         {
5836           resolved_type_decl = build_decl (TYPE_DECL,
5837                                            TYPE_NAME (class_type),
5838                                            resolved_type);
5839         }
5840       return resolved_type_decl;
5841     }
5842
5843   /* 1- Check to see if we have an array. If true, find what we really
5844      want to resolve  */
5845   if ((array_dims = build_type_name_from_array_name (tname,
5846                                                      &TYPE_NAME (class_type))))
5847     WFL_STRIP_BRACKET (cl, cl);
5848
5849   /* 2- Resolve the bare type */
5850   if (!(resolved_type_decl = do_resolve_class (enclosing, NULL_TREE, class_type,
5851                                                decl, cl)))
5852     return NULL_TREE;
5853   resolved_type = TREE_TYPE (resolved_type_decl);
5854
5855   /* 3- If we have an array, reconstruct the array down to its nesting */
5856   if (array_dims)
5857     {
5858       for (; array_dims; array_dims--)
5859         resolved_type = build_java_array_type (resolved_type, -1);
5860       resolved_type_decl = TYPE_NAME (resolved_type);
5861     }
5862   TREE_TYPE (class_type) = resolved_type;
5863   return resolved_type_decl;
5864 }
5865
5866 /* Effectively perform the resolution of class CLASS_TYPE.  DECL or CL
5867    are used to report error messages; CL must either be NULL_TREE or a
5868    WFL wrapping a class.  Do not try to replace TYPE_NAME (class_type)
5869    by a variable, since it is changed by find_in_imports{_on_demand}
5870    and (but it doesn't really matter) qualify_and_find.  */
5871
5872 tree
5873 do_resolve_class (tree enclosing, tree import_type, tree class_type, tree decl,
5874                   tree cl)
5875 {
5876   tree new_class_decl = NULL_TREE;
5877   tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5878   tree candidate = NULL_TREE;
5879   tree decl_result;
5880
5881   if (QUALIFIED_P (TYPE_NAME (class_type)))
5882     {
5883       /* If the type name is of the form `Q . Id', then Q is either a
5884          package name or a class name.  First we try to find Q as a
5885          class and then treat Id as a member type.  If we can't find Q
5886          as a class then we fall through.  */
5887       tree q, left, left_type, right;
5888       if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5889         {
5890           BUILD_PTR_FROM_NAME (left_type, left);
5891           q = do_resolve_class (enclosing, import_type, left_type, decl, cl);
5892           if (q)
5893             {
5894               enclosing = q;
5895               saved_enclosing_type = TREE_TYPE (q);
5896               BUILD_PTR_FROM_NAME (class_type, right);
5897             }
5898         }
5899     }
5900
5901   if (enclosing)
5902     {
5903       tree context = enclosing;
5904
5905       /* 0- Search in the current class as an inner class.
5906          Maybe some code here should be added to load the class or
5907          something, at least if the class isn't an inner class and ended
5908          being loaded from class file. FIXME. */
5909       while (enclosing)
5910         {
5911           new_class_decl = resolve_inner_class (context, cl, enclosing, class_type);
5912           
5913           if (new_class_decl)
5914             {
5915               if (inner_class_accessible (new_class_decl, context))
5916                 break;
5917               else
5918                 if (candidate == NULL_TREE)
5919                   candidate = new_class_decl;
5920                 new_class_decl = NULL_TREE;
5921             }
5922
5923           /* Now that we've looked through all superclasses, try the enclosing
5924              context. */
5925           enclosing = DECL_CONTEXT (enclosing);
5926         }
5927
5928       if (new_class_decl)
5929         return new_class_decl;
5930     }
5931
5932   /* 1- Check for the type in single imports.  Look at enclosing classes and,
5933      if we're laying out a superclass, at the import list for the subclass.
5934      This will change TYPE_NAME() if something relevant is found. */
5935   if (import_type && TYPE_IMPORT_LIST (import_type))
5936     find_in_imports (import_type, class_type);
5937   find_in_imports (saved_enclosing_type, class_type);
5938
5939   /* 2- And check for the type in the current compilation unit */
5940   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5941     {
5942       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5943         load_class (TYPE_NAME (class_type), 0);
5944       return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5945     }
5946
5947   /* 3- Search according to the current package definition */
5948   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5949     {
5950       if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5951                                              TYPE_NAME (class_type))))
5952         return new_class_decl;
5953     }
5954
5955   /* 4- Check the import on demands. Don't allow bar.baz to be
5956      imported from foo.* */
5957   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5958     {
5959       if (import_type
5960           && TYPE_IMPORT_DEMAND_LIST (import_type)
5961           && find_in_imports_on_demand (import_type, class_type))
5962         return NULL_TREE;
5963       if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5964         return NULL_TREE;
5965     }
5966
5967   /* If found in find_in_imports_on_demand, the type has already been
5968      loaded. */
5969   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5970     return new_class_decl;
5971
5972   /* 5- Check another compilation unit that bears the name of type */
5973   load_class (TYPE_NAME (class_type), 0);
5974
5975   if (!cl)
5976     cl = lookup_cl (decl);
5977
5978   /* If we don't have a value for CL, then we're being called recursively.
5979      We can't check package access just yet, but it will be taken care of
5980      by the caller. */
5981   if (cl)
5982     {
5983       if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
5984         return NULL_TREE;
5985     }
5986
5987   /* 6- Last call for a resolution */
5988   decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5989
5990   /* The final lookup might have registered a.b.c into a.b$c If we
5991      failed at the first lookup, progressively change the name if
5992      applicable and use the matching DECL instead. */
5993   if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5994     {
5995       char *separator;
5996       tree name = TYPE_NAME (class_type);
5997       char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5998
5999       strcpy (namebuffer, IDENTIFIER_POINTER (name));
6000
6001       do {
6002
6003        /* Reach the last '.', and if applicable, replace it by a `$' and
6004           see if this exists as a type. */
6005        if ((separator = strrchr (namebuffer, '.')))
6006          {
6007            *separator = '$';
6008            name = get_identifier (namebuffer);
6009            decl_result = IDENTIFIER_CLASS_VALUE (name);
6010          }
6011       } while (!decl_result && separator);
6012     }
6013   if (decl_result)
6014     return decl_result;
6015   else
6016     return candidate;
6017 }
6018
6019 static tree
6020 qualify_and_find (tree class_type, tree package, tree name)
6021 {
6022   tree new_qualified = merge_qualified_name (package, name);
6023   tree new_class_decl;
6024
6025   if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6026     load_class (new_qualified, 0);
6027   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6028     {
6029       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6030         load_class (TREE_TYPE (new_class_decl), 0);
6031       TYPE_NAME (class_type) = new_qualified;
6032       return IDENTIFIER_CLASS_VALUE (new_qualified);
6033     }
6034   return NULL_TREE;
6035 }
6036
6037 /* Resolve NAME and lay it out (if not done and if not the current
6038    parsed class). Return a decl node. This function is meant to be
6039    called when type resolution is necessary during the walk pass.  */
6040
6041 static tree
6042 resolve_and_layout (tree something, tree cl)
6043 {
6044   tree decl, decl_type;
6045
6046   /* Don't do that on the current class */
6047   if (something == current_class)
6048     return TYPE_NAME (current_class);
6049
6050   /* Don't do anything for void and other primitive types */
6051   if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6052     return NULL_TREE;
6053
6054   /* Pointer types can be reall pointer types or fake pointers. When
6055      finding a real pointer, recheck for primitive types */
6056   if (TREE_CODE (something) == POINTER_TYPE)
6057     {
6058       if (TREE_TYPE (something))
6059         {
6060           something = TREE_TYPE (something);
6061           if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6062             return NULL_TREE;
6063         }
6064       else
6065         something = TYPE_NAME (something);
6066     }
6067
6068   /* Don't do anything for arrays of primitive types */
6069   if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6070       && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6071     return NULL_TREE;
6072
6073   /* Something might be a WFL */
6074   if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6075     something = EXPR_WFL_NODE (something);
6076
6077   /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6078      TYPE_DECL or a real TYPE */
6079   else if (TREE_CODE (something) != IDENTIFIER_NODE)
6080     something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6081             DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6082
6083   if (!(decl = resolve_no_layout (something, cl)))
6084     return NULL_TREE;
6085
6086   /* Resolve and layout if necessary */
6087   decl_type = TREE_TYPE (decl);
6088   layout_class_methods (decl_type);
6089   /* Check methods */
6090   if (CLASS_FROM_SOURCE_P (decl_type))
6091     java_check_methods (decl);
6092   /* Layout the type if necessary */
6093   if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6094     safe_layout_class (decl_type);
6095
6096   return decl;
6097 }
6098
6099 /* Resolve a class, returns its decl but doesn't perform any
6100    layout. The current parsing context is saved and restored */
6101
6102 static tree
6103 resolve_no_layout (tree name, tree cl)
6104 {
6105   tree ptr, decl;
6106   BUILD_PTR_FROM_NAME (ptr, name);
6107   java_parser_context_save_global ();
6108   decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6109   java_parser_context_restore_global ();
6110
6111   return decl;
6112 }
6113
6114 /* Called when reporting errors. Skip the '[]'s in a complex array
6115    type description that failed to be resolved. purify_type_name can't
6116    use an identifier tree.  */
6117
6118 static const char *
6119 purify_type_name (const char *name)
6120 {
6121   int len = strlen (name);
6122   int bracket_found;
6123
6124   STRING_STRIP_BRACKETS (name, len, bracket_found);
6125   if (bracket_found)
6126     {
6127       char *stripped_name = xmemdup (name, len, len+1);
6128       stripped_name [len] = '\0';
6129       return stripped_name;
6130     }
6131   return name;
6132 }
6133
6134 /* The type CURRENT refers to can't be found. We print error messages.  */
6135
6136 static void
6137 complete_class_report_errors (jdep *dep)
6138 {
6139   const char *name;
6140
6141   if (!JDEP_WFL (dep))
6142     return;
6143
6144   name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6145   switch (JDEP_KIND (dep))
6146     {
6147     case JDEP_SUPER:
6148       parse_error_context
6149         (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6150          purify_type_name (name),
6151          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6152       break;
6153     case JDEP_FIELD:
6154       parse_error_context
6155         (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6156          purify_type_name (name),
6157          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6158       break;
6159     case JDEP_METHOD:           /* Covers arguments */
6160       parse_error_context
6161         (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6162          purify_type_name (name),
6163          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6164          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6165       break;
6166     case JDEP_METHOD_RETURN:    /* Covers return type */
6167       parse_error_context
6168         (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6169          purify_type_name (name),
6170          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6171       break;
6172     case JDEP_INTERFACE:
6173       parse_error_context
6174         (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6175          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6176          (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6177          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6178       break;
6179     case JDEP_VARIABLE:
6180       parse_error_context
6181         (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6182          purify_type_name (IDENTIFIER_POINTER
6183                            (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6184          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6185       break;
6186     case JDEP_EXCEPTION:        /* As specified by `throws' */
6187       parse_error_context
6188           (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6189          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6190       break;
6191     default:
6192       /* Fix for -Wall. Just break doing nothing. The error will be
6193          caught later */
6194       break;
6195     }
6196 }
6197
6198 /* Return a static string containing the DECL prototype string. If
6199    DECL is a constructor, use the class name instead of the form
6200    <init> */
6201
6202 static const char *
6203 get_printable_method_name (tree decl)
6204 {
6205   const char *to_return;
6206   tree name = NULL_TREE;
6207
6208   if (DECL_CONSTRUCTOR_P (decl))
6209     {
6210       name = DECL_NAME (decl);
6211       DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6212     }
6213
6214   to_return = lang_printable_name (decl, 2);
6215   if (DECL_CONSTRUCTOR_P (decl))
6216     DECL_NAME (decl) = name;
6217
6218   return to_return;
6219 }
6220
6221 /* Track method being redefined inside the same class. As a side
6222    effect, set DECL_NAME to an IDENTIFIER (prior entering this
6223    function it's a FWL, so we can track errors more accurately.)  */
6224
6225 static int
6226 check_method_redefinition (tree class, tree method)
6227 {
6228   tree redef, sig;
6229
6230   /* There's no need to verify <clinit> and finit$ and instinit$ */
6231   if (DECL_CLINIT_P (method)
6232       || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6233     return 0;
6234
6235   sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6236   for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6237     {
6238       if (redef == method)
6239         break;
6240       if (DECL_NAME (redef) == DECL_NAME (method)
6241           && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6242           && !DECL_ARTIFICIAL (method))
6243         {
6244           parse_error_context
6245             (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6246              (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6247              get_printable_method_name (redef));
6248           return 1;
6249         }
6250     }
6251   return 0;
6252 }
6253
6254 /* Return 1 if check went ok, 0 otherwise.  */
6255 static int
6256 check_abstract_method_definitions (int do_interface, tree class_decl,
6257                                    tree type)
6258 {
6259   tree class = TREE_TYPE (class_decl);
6260   tree method, end_type;
6261   int ok = 1;
6262
6263   end_type = (do_interface ? object_type_node : type);
6264   for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6265     {
6266       tree other_super, other_method, method_sig, method_name;
6267       int found = 0;
6268       int end_type_reached = 0;
6269
6270       if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6271         continue;
6272
6273       /* Now verify that somewhere in between TYPE and CLASS,
6274          abstract method METHOD gets a non abstract definition
6275          that is inherited by CLASS.  */
6276
6277       method_sig = build_java_signature (TREE_TYPE (method));
6278       method_name = DECL_NAME (method);
6279       if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6280         method_name = EXPR_WFL_NODE (method_name);
6281
6282       other_super = class;
6283       do {
6284         if (other_super == end_type)
6285           end_type_reached = 1;
6286
6287         /* Method search */
6288         for (other_method = TYPE_METHODS (other_super); other_method;
6289             other_method = TREE_CHAIN (other_method))
6290           {
6291             tree s = build_java_signature (TREE_TYPE (other_method));
6292             tree other_name = DECL_NAME (other_method);
6293
6294             if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6295               other_name = EXPR_WFL_NODE (other_name);
6296             if (!DECL_CLINIT_P (other_method)
6297                 && !DECL_CONSTRUCTOR_P (other_method)
6298                 && method_name == other_name
6299                 && method_sig == s
6300                 && !METHOD_ABSTRACT (other_method))
6301              {
6302                found = 1;
6303                break;
6304              }
6305           }
6306         other_super = CLASSTYPE_SUPER (other_super);
6307       } while (!end_type_reached);
6308
6309       /* Report that abstract METHOD didn't find an implementation
6310          that CLASS can use. */
6311       if (!found)
6312         {
6313           char *t = xstrdup (lang_printable_name
6314                             (TREE_TYPE (TREE_TYPE (method)), 0));
6315           tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6316
6317           parse_error_context
6318             (lookup_cl (class_decl),
6319              "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",
6320              IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6321              t, lang_printable_name (method, 2),
6322              (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6323               "interface" : "class"),
6324              IDENTIFIER_POINTER (ccn),
6325              (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6326              IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6327           ok = 0;
6328           free (t);
6329         }
6330     }
6331
6332   if (ok && do_interface)
6333     {
6334       /* Check for implemented interfaces. */
6335       int i;
6336       tree base_binfo;
6337       
6338       for (i = 1;
6339            ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6340            i++)
6341         ok = check_abstract_method_definitions (1, class_decl,
6342                                                 BINFO_TYPE (base_binfo));
6343     }
6344
6345   return ok;
6346 }
6347
6348 /* Check that CLASS_DECL somehow implements all inherited abstract
6349    methods.  */
6350
6351 static void
6352 java_check_abstract_method_definitions (tree class_decl)
6353 {
6354   tree class = TREE_TYPE (class_decl);
6355   tree super, base_binfo;
6356   int i;
6357
6358   if (CLASS_ABSTRACT (class_decl))
6359     return;
6360
6361   /* Check for inherited types */
6362   super = class;
6363   do {
6364     super = CLASSTYPE_SUPER (super);
6365     check_abstract_method_definitions (0, class_decl, super);
6366   } while (super != object_type_node);
6367
6368   /* Check for implemented interfaces. */
6369   for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6370     check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6371 }
6372
6373 /* Check all the types method DECL uses and return 1 if all of them
6374    are now complete, 0 otherwise. This is used to check whether its
6375    safe to build a method signature or not.  */
6376
6377 static int
6378 check_method_types_complete (tree decl)
6379 {
6380   tree type = TREE_TYPE (decl);
6381   tree args;
6382
6383   if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6384     return 0;
6385
6386   args = TYPE_ARG_TYPES (type);
6387   if (TREE_CODE (type) == METHOD_TYPE)
6388     args = TREE_CHAIN (args);
6389   for (; args != end_params_node; args = TREE_CHAIN (args))
6390     if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6391       return 0;
6392
6393   return 1;
6394 }
6395
6396 /* Visible interface to check methods contained in CLASS_DECL */
6397
6398 void
6399 java_check_methods (tree class_decl)
6400 {
6401   if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6402     return;
6403
6404   if (CLASS_INTERFACE (class_decl))
6405     java_check_abstract_methods (class_decl);
6406   else
6407     java_check_regular_methods (class_decl);
6408
6409   CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6410 }
6411
6412 /* Like not_accessible_p, but doesn't refer to the current class at
6413    all.  */
6414 static bool
6415 hack_is_accessible_p (tree member, tree from_where)
6416 {
6417   int flags = get_access_flags_from_decl (member);
6418
6419   if (from_where == DECL_CONTEXT (member)
6420       || (flags & ACC_PUBLIC))
6421     return true;
6422
6423   if ((flags & ACC_PROTECTED))
6424     {
6425       if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6426         return true;
6427     }
6428
6429   if ((flags & ACC_PRIVATE))
6430     return false;
6431
6432   /* Package private, or protected.  */
6433   return in_same_package (TYPE_NAME (from_where),
6434                           TYPE_NAME (DECL_CONTEXT (member)));
6435 }
6436
6437 /* Check all the methods of CLASS_DECL. Methods are first completed
6438    then checked according to regular method existence rules.  If no
6439    constructor for CLASS_DECL were encountered, then build its
6440    declaration.  */
6441 static void
6442 java_check_regular_methods (tree class_decl)
6443 {
6444   int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6445   tree method;
6446   tree class = TREE_TYPE (class_decl);
6447   tree found = NULL_TREE;
6448   tree mthrows;
6449
6450   /* It is not necessary to check methods defined in java.lang.Object */
6451   if (class == object_type_node)
6452     return;
6453
6454   if (!TYPE_NVIRTUALS (class))
6455     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6456
6457   /* Should take interfaces into account. FIXME */
6458   for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6459     {
6460       tree sig;
6461       tree method_wfl = DECL_FUNCTION_WFL (method);
6462       int aflags;
6463
6464       /* Check for redefinitions */
6465       if (check_method_redefinition (class, method))
6466         continue;
6467
6468       /* We verify things thrown by the method.  They must inherit from
6469          java.lang.Throwable.  */
6470       for (mthrows = DECL_FUNCTION_THROWS (method);
6471            mthrows; mthrows = TREE_CHAIN (mthrows))
6472         {
6473           if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6474             parse_error_context
6475               (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6476                IDENTIFIER_POINTER
6477                  (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6478         }
6479
6480       /* If we see one constructor a mark so we don't generate the
6481          default one.  Also skip other verifications: constructors
6482          can't be inherited hence hidden or overridden.  */
6483       if (DECL_CONSTRUCTOR_P (method))
6484         {
6485           saw_constructor = 1;
6486           continue;
6487         }
6488
6489       sig = build_java_argument_signature (TREE_TYPE (method));
6490       found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6491                                               SEARCH_SUPER | SEARCH_INTERFACE);
6492
6493       /* Inner class can't declare static methods */
6494       if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6495         {
6496           char *t = xstrdup (lang_printable_name (class, 0));
6497           parse_error_context
6498             (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6499              lang_printable_name (method, 2), t);
6500           free (t);
6501         }
6502
6503       /* Nothing overrides or it's a private method. */
6504       if (!found)
6505         continue;
6506       if (METHOD_PRIVATE (found))
6507         {
6508           found = NULL_TREE;
6509           continue;
6510         }
6511
6512       /* If `found' is declared in an interface, make sure the
6513          modifier matches. */
6514       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6515           && clinit_identifier_node != DECL_NAME (found)
6516           && !METHOD_PUBLIC (method))
6517         {
6518           tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6519           parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6520                                IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6521                                lang_printable_name (method, 2),
6522                                IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6523         }
6524
6525       /* Can't override a method with the same name and different return
6526          types. */
6527       if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6528         {
6529           char *t = xstrdup
6530             (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6531           parse_error_context
6532             (method_wfl,
6533              "Method %qs was defined with return type %qs in class %qs",
6534              lang_printable_name (found, 2), t,
6535              IDENTIFIER_POINTER
6536                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6537           free (t);
6538         }
6539
6540       aflags = get_access_flags_from_decl (found);
6541
6542       /* Can't override final. Can't override static. */
6543       if (METHOD_FINAL (found) || METHOD_STATIC (found))
6544         {
6545           /* Static *can* override static */
6546           if (METHOD_STATIC (found) && METHOD_STATIC (method))
6547             continue;
6548           parse_error_context
6549             (method_wfl,
6550              "%s methods can't be overridden. Method %qs is %s in class %qs",
6551              (METHOD_FINAL (found) ? "Final" : "Static"),
6552              lang_printable_name (found, 2),
6553              (METHOD_FINAL (found) ? "final" : "static"),
6554              IDENTIFIER_POINTER
6555                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6556           continue;
6557         }
6558
6559       /* Static method can't override instance method. */
6560       if (METHOD_STATIC (method))
6561         {
6562           parse_error_context
6563             (method_wfl,
6564              "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6565              lang_printable_name (found, 2),
6566              IDENTIFIER_POINTER
6567                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6568           continue;
6569         }
6570
6571       /* - Overriding/hiding public must be public
6572          - Overriding/hiding protected must be protected or public
6573          - If the overridden or hidden method has default (package)
6574            access, then the overriding or hiding method must not be
6575            private; otherwise, a compile-time error occurs.  If
6576            `found' belongs to an interface, things have been already
6577            taken care of.  */
6578       if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6579           && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6580               || (METHOD_PROTECTED (found)
6581                   && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6582               || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6583                   && METHOD_PRIVATE (method))))
6584         {
6585           parse_error_context
6586             (method_wfl,
6587              "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6588              (METHOD_PUBLIC (method) ? "public" :
6589               (METHOD_PRIVATE (method) ? "private" : "protected")),
6590              IDENTIFIER_POINTER (DECL_NAME
6591                                  (TYPE_NAME (DECL_CONTEXT (found)))));
6592           continue;
6593         }
6594
6595       /* Check this method against all the other implementations it
6596          overrides.  Here we only check the class hierarchy; the rest
6597          of the checking is done later.  If this method is just a
6598          Miranda method, we can skip the check.  */
6599       if (! METHOD_INVISIBLE (method))
6600         check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6601     }
6602
6603   /* The above throws clause check only looked at superclasses.  Now
6604      we must also make sure that all methods declared in interfaces
6605      have compatible throws clauses.  FIXME: there are more efficient
6606      ways to organize this checking; we should implement one.  */
6607   check_interface_throws_clauses (class, class);
6608
6609   if (!TYPE_NVIRTUALS (class))
6610     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6611
6612   /* Search for inherited abstract method not yet implemented in this
6613      class.  */
6614   java_check_abstract_method_definitions (class_decl);
6615
6616   if (!saw_constructor)
6617     abort ();
6618 }
6619
6620 /* Check to make sure that all the methods in all the interfaces
6621    implemented by CLASS_DECL are compatible with the concrete
6622    implementations available in CHECK_CLASS_DECL.  */
6623 static void
6624 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6625 {
6626   for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6627     {
6628       int i;
6629
6630       if (! CLASS_LOADED_P (class_decl))
6631         {
6632           if (CLASS_FROM_SOURCE_P (class_decl))
6633             safe_layout_class (class_decl);
6634           else
6635             load_class (class_decl, 1);
6636         }
6637
6638       for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6639         {
6640           tree interface
6641             = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6642           tree iface_method;
6643
6644           for (iface_method = TYPE_METHODS (interface);
6645                iface_method != NULL_TREE;
6646                iface_method = TREE_CHAIN (iface_method))
6647             {
6648               tree sig, method;
6649
6650               /* First look for a concrete method implemented or
6651                  inherited by this class.  No need to search
6652                  interfaces here, since we're already looking through
6653                  all of them.  */
6654               sig = build_java_argument_signature (TREE_TYPE (iface_method));
6655               method
6656                 = lookup_argument_method_generic (check_class_decl,
6657                                                   DECL_NAME (iface_method),
6658                                                   sig, SEARCH_VISIBLE);
6659               /* If we don't find an implementation, that is ok.  Any
6660                  potential errors from that are diagnosed elsewhere.
6661                  Also, multiple inheritance with conflicting throws
6662                  clauses is fine in the absence of a concrete
6663                  implementation.  */
6664               if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6665                   && !METHOD_INVISIBLE (iface_method))
6666                 {
6667                   tree method_wfl = DECL_FUNCTION_WFL (method);
6668                   check_throws_clauses (method, method_wfl, iface_method);
6669                 }
6670             }
6671
6672           /* Now check superinterfaces.  */
6673           check_interface_throws_clauses (check_class_decl, interface);
6674         }
6675     }
6676 }
6677
6678 /* Check throws clauses of a method against the clauses of all the
6679    methods it overrides.  We do this by searching up the class
6680    hierarchy, examining all matching accessible methods.  */
6681 static void
6682 check_concrete_throws_clauses (tree class, tree self_method,
6683                                tree name, tree signature)
6684 {
6685   tree method = lookup_argument_method_generic (class, name, signature,
6686                                                 SEARCH_SUPER | SEARCH_VISIBLE);
6687   while (method != NULL_TREE)
6688     {
6689       if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6690         check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6691                               method);
6692
6693       method = lookup_argument_method_generic (DECL_CONTEXT (method),
6694                                                name, signature,
6695                                                SEARCH_SUPER | SEARCH_VISIBLE);
6696     }
6697 }
6698
6699 /* Generate an error if the `throws' clause of METHOD (if any) is
6700    incompatible with the `throws' clause of FOUND (if any).  */
6701 static void
6702 check_throws_clauses (tree method, tree method_wfl, tree found)
6703 {
6704   tree mthrows;
6705
6706   for (mthrows = DECL_FUNCTION_THROWS (method);
6707        mthrows; mthrows = TREE_CHAIN (mthrows))
6708     {
6709       tree fthrows;
6710
6711       /* We don't verify unchecked expressions */
6712       if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6713         continue;
6714       /* Checked expression must be compatible */
6715       for (fthrows = DECL_FUNCTION_THROWS (found);
6716            fthrows; fthrows = TREE_CHAIN (fthrows))
6717         {
6718           if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6719             break;
6720         }
6721       if (!fthrows)
6722         {
6723           parse_error_context
6724             (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",
6725              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6726              lang_printable_name (found, 2),
6727              IDENTIFIER_POINTER
6728              (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6729         }
6730     }
6731 }
6732
6733 /* Check abstract method of interface INTERFACE */
6734 static void
6735 java_check_abstract_methods (tree interface_decl)
6736 {
6737   int i;
6738   tree method, found;
6739   tree interface = TREE_TYPE (interface_decl);
6740   tree base_binfo;
6741
6742   for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6743     {
6744       /* 2- Check for double definition inside the defining interface */
6745       if (check_method_redefinition (interface, method))
6746         continue;
6747
6748       /* 3- Overriding is OK as far as we preserve the return type.  */
6749       found = lookup_java_interface_method2 (interface, method);
6750       if (found)
6751         {
6752           char *t;
6753           t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6754           parse_error_context
6755             (DECL_FUNCTION_WFL (found),
6756              "Method %qs was defined with return type %qs in class %qs",
6757              lang_printable_name (found, 2), t,
6758              IDENTIFIER_POINTER
6759                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6760           free (t);
6761           continue;
6762         }
6763     }
6764
6765   /* 4- Inherited methods can't differ by their returned types */
6766   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6767     {
6768       tree sub_interface_method, sub_interface;
6769
6770       sub_interface = BINFO_TYPE (base_binfo);
6771       for (sub_interface_method = TYPE_METHODS (sub_interface);
6772            sub_interface_method;
6773            sub_interface_method = TREE_CHAIN (sub_interface_method))
6774         {
6775           found = lookup_java_interface_method2 (interface,
6776                                                  sub_interface_method);
6777           if (found && (found != sub_interface_method))
6778             {
6779               parse_error_context
6780                 (lookup_cl (sub_interface_method),
6781                  "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6782                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6783                  lang_printable_name (found, 2),
6784                  IDENTIFIER_POINTER
6785                    (DECL_NAME (TYPE_NAME
6786                                (DECL_CONTEXT (sub_interface_method)))),
6787                  IDENTIFIER_POINTER
6788                    (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6789             }
6790         }
6791     }
6792 }
6793
6794 /* Lookup methods in interfaces using their name and partial
6795    signature. Return a matching method only if their types differ.  */
6796
6797 static tree
6798 lookup_java_interface_method2 (tree class, tree method_decl)
6799 {
6800   int i;
6801   tree base_binfo;
6802   tree to_return;
6803
6804   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6805     {
6806       if ((BINFO_TYPE (base_binfo) != object_type_node)
6807           && (to_return =
6808               lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6809         return to_return;
6810     }
6811   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6812     {
6813       to_return = lookup_java_interface_method2
6814         (BINFO_TYPE (base_binfo), method_decl);
6815       if (to_return)
6816         return to_return;
6817     }
6818
6819   return NULL_TREE;
6820 }
6821
6822 /* Lookup method using their name and partial signature. Return a
6823    matching method only if their types differ.  */
6824
6825 static tree
6826 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6827 {
6828   tree method, method_signature, method_name, method_type, name;
6829
6830   method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6831   name = DECL_NAME (method_decl);
6832   method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6833                  EXPR_WFL_NODE (name) : name);
6834   method_type = TREE_TYPE (TREE_TYPE (method_decl));
6835
6836   while (clas != NULL_TREE)
6837     {
6838       for (method = TYPE_METHODS (clas);
6839            method != NULL_TREE;  method = TREE_CHAIN (method))
6840         {
6841           tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6842           tree name = DECL_NAME (method);
6843           if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6844                EXPR_WFL_NODE (name) : name) == method_name
6845               && method_sig == method_signature
6846               && TREE_TYPE (TREE_TYPE (method)) != method_type)
6847             return method;
6848         }
6849       clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6850     }
6851   return NULL_TREE;
6852 }
6853
6854 /* Return the line that matches DECL line number, and try its best to
6855    position the column number. Used during error reports.
6856    FUTURE/FIXME: return source_location instead of node. */
6857
6858 static GTY(()) tree cl_v;
6859 static tree
6860 lookup_cl (tree decl)
6861 {
6862 #ifndef USE_MAPPED_LOCATION
6863   char *line, *found;
6864 #endif
6865
6866   if (!decl)
6867     return NULL_TREE;
6868
6869   if (cl_v == NULL_TREE)
6870     {
6871       cl_v = build_unknown_wfl (NULL_TREE);
6872     }
6873
6874 #ifdef USE_MAPPED_LOCATION
6875   SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6876 #else
6877   EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6878   EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6879
6880   line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6881                             EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6882
6883   found = strstr ((const char *)line,
6884                   (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6885   if (found)
6886     EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6887 #endif
6888
6889   return cl_v;
6890 }
6891
6892 /* Look for a simple name in the single-type import list */
6893
6894 static tree
6895 find_name_in_single_imports (tree name)
6896 {
6897   tree node;
6898
6899   for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6900     if (TREE_VALUE (node) == name)
6901       return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6902
6903   return NULL_TREE;
6904 }
6905
6906 /* Process all single-type import. */
6907
6908 static int
6909 process_imports (void)
6910 {
6911   tree import;
6912   int error_found;
6913
6914   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6915     {
6916       tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6917       char *original_name;
6918
6919       /* Don't load twice something already defined. */
6920       if (IDENTIFIER_CLASS_VALUE (to_be_found))
6921         continue;
6922
6923       original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6924                                IDENTIFIER_LENGTH (to_be_found),
6925                                IDENTIFIER_LENGTH (to_be_found) + 1);
6926
6927       while (1)
6928         {
6929           tree left;
6930
6931           QUALIFIED_P (to_be_found) = 1;
6932           load_class (to_be_found, 0);
6933           error_found =
6934             check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6935
6936           /* We found it, we can bail out */
6937           if (IDENTIFIER_CLASS_VALUE (to_be_found))
6938             {
6939               check_deprecation (TREE_PURPOSE (import),
6940                                  IDENTIFIER_CLASS_VALUE (to_be_found));
6941               break;
6942             }
6943
6944           /* We haven't found it. Maybe we're trying to access an
6945              inner class.  The only way for us to know is to try again
6946              after having dropped a qualifier. If we can't break it further,
6947              we have an error. */
6948           if (split_qualified_name (&left, NULL, to_be_found))
6949             break;
6950
6951           to_be_found = left;
6952         }
6953       if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6954         {
6955           parse_error_context (TREE_PURPOSE (import),
6956                                "Class or interface %qs not found in import",
6957                                original_name);
6958           error_found = 1;
6959         }
6960
6961       free (original_name);
6962       if (error_found)
6963         return 1;
6964     }
6965   return 0;
6966 }
6967
6968 /* Possibly find and mark a class imported by a single-type import
6969    statement.  */
6970
6971 static void
6972 find_in_imports (tree enclosing_type, tree class_type)
6973 {
6974   tree import;
6975   if (enclosing_type && TYPE_IMPORT_LIST (enclosing_type))
6976     import = TYPE_IMPORT_LIST (enclosing_type);
6977   else
6978     import = ctxp->import_list;
6979
6980   while (import)
6981     {
6982       if (TREE_VALUE (import) == TYPE_NAME (class_type))
6983         {
6984           TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6985           QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6986           return;
6987         }
6988       import = TREE_CHAIN (import);
6989     }
6990 }
6991
6992 static int
6993 note_possible_classname (const char *name, int len)
6994 {
6995   tree node;
6996   if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6997     len = len - 5;
6998   else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6999     len = len - 6;
7000   else
7001     return 0;
7002   node = ident_subst (name, len, "", '/', '.', "");
7003   IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
7004   QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
7005   return 1;
7006 }
7007
7008 /* Read a import directory, gathering potential match for further type
7009    references. Indifferently reads a filesystem or a ZIP archive
7010    directory.  */
7011
7012 static void
7013 read_import_dir (tree wfl)
7014 {
7015   tree package_id = EXPR_WFL_NODE (wfl);
7016   const char *package_name = IDENTIFIER_POINTER (package_id);
7017   int package_length = IDENTIFIER_LENGTH (package_id);
7018   DIR *dirp = NULL;
7019   JCF *saved_jcf = current_jcf;
7020
7021   int found = 0;
7022   int k;
7023   void *entry;
7024   struct buffer filename[1];
7025
7026   if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7027     return;
7028   IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7029
7030   BUFFER_INIT (filename);
7031   buffer_grow (filename, package_length + 100);
7032
7033   for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7034     {
7035       const char *entry_name = jcf_path_name (entry);
7036       int entry_length = strlen (entry_name);
7037       if (jcf_path_is_zipfile (entry))
7038         {
7039           ZipFile *zipf;
7040           buffer_grow (filename, entry_length);
7041           memcpy (filename->data, entry_name, entry_length - 1);
7042           filename->data[entry_length-1] = '\0';
7043           zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7044           if (zipf == NULL)
7045             error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7046           else
7047             {
7048               ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7049               BUFFER_RESET (filename);
7050               for (k = 0; k < package_length; k++)
7051                 {
7052                   char ch = package_name[k];
7053                   *filename->ptr++ = ch == '.' ? '/' : ch;
7054                 }
7055               *filename->ptr++ = '/';
7056
7057               for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
7058                 {
7059                   const char *current_entry = ZIPDIR_FILENAME (zipd);
7060                   int current_entry_len = zipd->filename_length;
7061
7062                   if (current_entry_len >= BUFFER_LENGTH (filename)
7063                       && strncmp ((const char *) filename->data, current_entry,
7064                                   BUFFER_LENGTH (filename)) != 0)
7065                     continue;
7066                   found |= note_possible_classname (current_entry,
7067                                                     current_entry_len);
7068                 }
7069             }
7070         }
7071       else
7072         {
7073           BUFFER_RESET (filename);
7074           buffer_grow (filename, entry_length + package_length + 4);
7075           strcpy ((char *) filename->data, entry_name);
7076           filename->ptr = filename->data + entry_length;
7077           for (k = 0; k < package_length; k++)
7078             {
7079               char ch = package_name[k];
7080               *filename->ptr++ = ch == '.' ? '/' : ch;
7081             }
7082           *filename->ptr = '\0';
7083
7084           dirp = opendir ((const char *) filename->data);
7085           if (dirp == NULL)
7086             continue;
7087           *filename->ptr++ = '/';
7088           for (;;)
7089             {
7090               int len;
7091               const char *d_name;
7092               struct dirent *direntp = readdir (dirp);
7093               if (!direntp)
7094                 break;
7095               d_name = direntp->d_name;
7096               len = strlen (direntp->d_name);
7097               buffer_grow (filename, len+1);
7098               strcpy ((char *) filename->ptr, d_name);
7099               found |= note_possible_classname ((const char *) filename->data + entry_length,
7100                                                 package_length+len+1);
7101             }
7102           if (dirp)
7103             closedir (dirp);
7104         }
7105     }
7106
7107   free (filename->data);
7108
7109   /* Here we should have a unified way of retrieving an entry, to be
7110      indexed. */
7111   if (!found)
7112     {
7113       static int first = 1;
7114       if (first)
7115         {
7116           error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7117           java_error_count++;
7118           first = 0;
7119         }
7120       else
7121         parse_error_context (wfl, "Package %qs not found in import",
7122                              package_name);
7123       current_jcf = saved_jcf;
7124       return;
7125     }
7126   current_jcf = saved_jcf;
7127 }
7128
7129 /* Possibly find a type in the import on demands specified
7130    types. Returns 1 if an error occurred, 0 otherwise. Run through the
7131    entire list, to detected potential double definitions.  */
7132
7133 static int
7134 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7135 {
7136   tree class_type_name = TYPE_NAME (class_type);
7137   tree cl = NULL_TREE;
7138   int seen_once = -1;   /* -1 when not set, 1 if seen once, >1 otherwise. */
7139   int to_return = -1;   /* -1 when not set, 0 or 1 otherwise */
7140   tree node;
7141   tree import;
7142
7143   if (enclosing_type && TYPE_IMPORT_DEMAND_LIST (enclosing_type))
7144     import = TYPE_IMPORT_DEMAND_LIST (enclosing_type);
7145   else
7146     import = ctxp->import_demand_list;
7147
7148   for (; import; import = TREE_CHAIN (import))
7149     {
7150       location_t saved_location = input_location;
7151       int access_check;
7152       const char *id_name;
7153       tree decl, type_name_copy;
7154
7155       obstack_grow (&temporary_obstack,
7156                     IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7157                     IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7158       obstack_1grow (&temporary_obstack, '.');
7159       obstack_grow0 (&temporary_obstack,
7160                      IDENTIFIER_POINTER (class_type_name),
7161                      IDENTIFIER_LENGTH (class_type_name));
7162       id_name = obstack_finish (&temporary_obstack);
7163
7164       if (! (node = maybe_get_identifier (id_name)))
7165         continue;
7166
7167       /* Setup input_line so that it refers to the line of the import (in
7168          case we parse a class file and encounter errors */
7169 #ifdef USE_MAPPED_LOCATION
7170       input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7171 #else
7172       input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7173 #endif
7174
7175       type_name_copy = TYPE_NAME (class_type);
7176       TYPE_NAME (class_type) = node;
7177       QUALIFIED_P (node) = 1;
7178       decl = IDENTIFIER_CLASS_VALUE (node);
7179       access_check = -1;
7180       /* If there is no DECL set for the class or if the class isn't
7181          loaded and not seen in source yet, then load */
7182       if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7183         {
7184           load_class (node, 0);
7185           decl = IDENTIFIER_CLASS_VALUE (node);
7186         }
7187       if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7188         access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7189                                                false, NULL_TREE);
7190       else
7191         /* 6.6.1: Inner classes are subject to member access rules. */
7192         access_check = 0;
7193
7194       input_location = saved_location;
7195
7196       /* If the loaded class is not accessible or couldn't be loaded,
7197          we restore the original TYPE_NAME and process the next
7198          import. */
7199       if (access_check || !decl)
7200         {
7201           TYPE_NAME (class_type) = type_name_copy;
7202           continue;
7203         }
7204
7205       /* If the loaded class is accessible, we keep a tab on it to
7206          detect and report multiple inclusions. */
7207       if (IS_A_CLASSFILE_NAME (node))
7208         {
7209           if (seen_once < 0)
7210             {
7211               cl = TREE_PURPOSE (import);
7212               seen_once = 1;
7213             }
7214           else if (seen_once >= 0)
7215             {
7216               tree location = (cl ? cl : TREE_PURPOSE (import));
7217               tree package = (cl ? EXPR_WFL_NODE (cl) :
7218                               EXPR_WFL_NODE (TREE_PURPOSE (import)));
7219               seen_once++;
7220               parse_error_context
7221                 (location,
7222                  "Type %qs also potentially defined in package %qs",
7223                  IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7224                  IDENTIFIER_POINTER (package));
7225             }
7226         }
7227       to_return = access_check;
7228     }
7229
7230   if (seen_once == 1)
7231     return to_return;
7232   else
7233     return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7234 }
7235
7236 static tree
7237 resolve_package (tree pkg, tree *next, tree *type_name)
7238 {
7239   tree current;
7240   tree decl = NULL_TREE;
7241   *type_name = NULL_TREE;
7242
7243   /* The trick is to determine when the package name stops and were
7244      the name of something contained in the package starts. Then we
7245      return a fully qualified name of what we want to get. */
7246
7247   *next = EXPR_WFL_QUALIFICATION (pkg);
7248
7249   /* Try to progressively construct a type name */
7250   if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7251     for (current = EXPR_WFL_QUALIFICATION (pkg);
7252          current; current = TREE_CHAIN (current))
7253       {
7254         /* If we don't have what we're expecting, exit now. TYPE_NAME
7255            will be null and the error caught later. */
7256         if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7257           break;
7258         *type_name =
7259           merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7260         if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7261           {
7262             /* resolve_package should be used in a loop, hence we
7263                point at this one to naturally process the next one at
7264                the next iteration. */
7265             *next = current;
7266             break;
7267           }
7268       }
7269   return decl;
7270 }
7271
7272 /* Check accessibility of inner class DECL, from the context ENCLOSING_DECL,
7273    according to member access rules.  */
7274
7275 static bool
7276 inner_class_accessible (tree decl, tree enclosing_decl)
7277 {
7278   tree enclosing_decl_type;
7279
7280   enclosing_decl_type = TREE_TYPE (enclosing_decl);
7281
7282   if (CLASS_PRIVATE (decl))
7283     {
7284       /* Access is permitted only within the body of the top-level
7285          class in which DECL is declared. */
7286       tree top_level = decl;
7287       while (DECL_CONTEXT (top_level))
7288         top_level = DECL_CONTEXT (top_level);
7289       while (DECL_CONTEXT (enclosing_decl))
7290         enclosing_decl = DECL_CONTEXT (enclosing_decl);
7291       if (top_level == enclosing_decl)
7292         return true;
7293     }
7294   else if (CLASS_PROTECTED (decl))
7295     {
7296       tree decl_context;
7297       /* Access is permitted from within the same package... */
7298       if (in_same_package (decl, enclosing_decl))
7299         return true;
7300
7301       /* ... or from within the body of a subtype of the context in which
7302          DECL is declared. */
7303       decl_context = DECL_CONTEXT (decl);
7304       while (enclosing_decl)
7305         {
7306           if (CLASS_INTERFACE (decl))
7307             {
7308               if (interface_of_p (TREE_TYPE (decl_context),
7309                                   enclosing_decl_type))
7310                 return true;
7311             }
7312           else
7313             {
7314               /* Eww. The order of the arguments is different!! */
7315               if (inherits_from_p (enclosing_decl_type,
7316                                    TREE_TYPE (decl_context)))
7317                 return true;
7318             }
7319           enclosing_decl = DECL_CONTEXT (enclosing_decl);
7320         }
7321     }
7322   else if (! CLASS_PUBLIC (decl))
7323     {
7324       /* Access is permitted only from within the same package as DECL. */
7325       if (in_same_package (decl, enclosing_decl))
7326         return true;
7327     }
7328   else
7329     /* Class is public. */
7330     return true;
7331
7332   return false;
7333 }
7334
7335 /* Check accessibility of inner classes according to member access rules.
7336    DECL is the inner class, ENCLOSING_DECL is the class from which the
7337    access is being attempted. */
7338
7339 static void
7340 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7341 {
7342   const char *access;
7343
7344   /* We don't issue an error message when CL is null. CL can be null
7345      as a result of processing a JDEP crafted by source_start_java_method
7346      for the purpose of patching its parm decl. But the error would
7347      have been already trapped when fixing the method's signature.
7348      DECL can also be NULL in case of earlier errors. */
7349   if (!decl || !cl)
7350     return;
7351
7352   if (inner_class_accessible (decl, enclosing_decl))
7353     return;
7354
7355   if (CLASS_PRIVATE (decl))
7356       access = "private";
7357   else if (CLASS_PROTECTED (decl))
7358       access = "protected";
7359   else
7360       access = "non-public";
7361
7362   parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7363                        (CLASS_INTERFACE (decl) ? "interface" : "class"),
7364                        lang_printable_name (decl, 2), access);
7365 }
7366
7367 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7368    foreign package, it must be PUBLIC. Return 0 if no access
7369    violations were found, 1 otherwise. If VERBOSE is true and an error
7370    was found, it is reported and accounted for.  If CL is NULL then 
7371    look it up with THIS_DECL.  */
7372
7373 static int
7374 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7375 {
7376   tree type;
7377
7378   if (!IDENTIFIER_CLASS_VALUE (class_name))
7379     return 0;
7380
7381   if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7382     return 0;
7383
7384   if (!CLASS_PUBLIC (TYPE_NAME (type)))
7385     {
7386       /* Access to a private class within the same package is
7387          allowed. */
7388       tree l, r;
7389       split_qualified_name (&l, &r, class_name);
7390       if (!QUALIFIED_P (class_name) && !ctxp->package)
7391         /* Both in the empty package. */
7392         return 0;
7393       if (l == ctxp->package)
7394         /* Both in the same package. */
7395         return 0;
7396
7397       if (verbose)
7398         parse_error_context
7399           (cl == NULL ? lookup_cl (this_decl): cl,
7400            "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7401            (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7402            IDENTIFIER_POINTER (class_name));
7403       return 1;
7404     }
7405   return 0;
7406 }
7407
7408 /* Local variable declaration. */
7409
7410 static void
7411 declare_local_variables (int modifier, tree type, tree vlist)
7412 {
7413   tree decl, current, saved_type;
7414   tree type_wfl = NULL_TREE;
7415   int must_chain = 0;
7416   int final_p = 0;
7417
7418   /* Push a new block if statements were seen between the last time we
7419      pushed a block and now. Keep a count of blocks to close */
7420   if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7421     {
7422       tree b = enter_block ();
7423       BLOCK_IS_IMPLICIT (b) = 1;
7424     }
7425
7426   if (modifier)
7427     {
7428       size_t i;
7429       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7430         if (1 << i & modifier)
7431           break;
7432       if (modifier == ACC_FINAL)
7433         final_p = 1;
7434       else
7435         {
7436           parse_error_context
7437             (ctxp->modifier_ctx [i],
7438              "Only %<final%> is allowed as a local variables modifier");
7439           return;
7440         }
7441     }
7442
7443   /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7444      hold the TYPE value if a new incomplete has to be created (as
7445      opposed to being found already existing and reused). */
7446   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7447
7448   /* If TYPE is fully resolved and we don't have a reference, make one */
7449   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7450
7451   /* Go through all the declared variables */
7452   for (current = vlist, saved_type = type; current;
7453        current = TREE_CHAIN (current), type = saved_type)
7454     {
7455       tree other, real_type;
7456       tree wfl  = TREE_PURPOSE (current);
7457       tree name = EXPR_WFL_NODE (wfl);
7458       tree init = TREE_VALUE (current);
7459
7460       /* Process NAME, as it may specify extra dimension(s) for it */
7461       type = build_array_from_name (type, type_wfl, name, &name);
7462
7463       /* Variable redefinition check */
7464       if ((other = lookup_name_in_blocks (name)))
7465         {
7466           variable_redefinition_error (wfl, name, TREE_TYPE (other),
7467                                        DECL_SOURCE_LINE (other));
7468           continue;
7469         }
7470
7471       /* Type adjustment. We may have just readjusted TYPE because
7472          the variable specified more dimensions. Make sure we have
7473          a reference if we can and don't have one already. */
7474       PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7475
7476       real_type = GET_REAL_TYPE (type);
7477       /* Never layout this decl. This will be done when its scope
7478          will be entered */
7479       decl = build_decl (VAR_DECL, name, real_type);
7480       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7481       DECL_FINAL (decl) = final_p;
7482       BLOCK_CHAIN_DECL (decl);
7483
7484       /* Don't try to use an INIT statement when an error was found */
7485       if (init && java_error_count)
7486         init = NULL_TREE;
7487
7488       /* Remember it if this is an initialized-upon-declaration final
7489          variable.  */
7490       if (init && final_p)
7491         {
7492           DECL_LOCAL_FINAL_IUD (decl) = 1;
7493         }
7494
7495       /* Add the initialization function to the current function's code */
7496       if (init)
7497         {
7498           /* Name might have been readjusted */
7499           EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7500           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7501           java_method_add_stmt (current_function_decl,
7502                                 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7503                                                       init));
7504         }
7505
7506       /* Setup dependency the type of the decl */
7507       if (must_chain)
7508         {
7509           jdep *dep;
7510           register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7511           dep = CLASSD_LAST (ctxp->classd_list);
7512           JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7513         }
7514     }
7515   SOURCE_FRONTEND_DEBUG (("Defined locals"));
7516 }
7517
7518 /* Called during parsing. Build decls from argument list.  */
7519
7520 static void
7521 source_start_java_method (tree fndecl)
7522 {
7523   tree tem;
7524   tree parm_decl;
7525   int i;
7526
7527   if (!fndecl)
7528     return;
7529
7530   current_function_decl = fndecl;
7531
7532   /* New scope for the function */
7533   enter_block ();
7534   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7535        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7536     {
7537       tree type = TREE_VALUE (tem);
7538       tree name = TREE_PURPOSE (tem);
7539
7540       /* If type is incomplete. Create an incomplete decl and ask for
7541          the decl to be patched later */
7542       if (INCOMPLETE_TYPE_P (type))
7543         {
7544           jdep *jdep;
7545           tree real_type = GET_REAL_TYPE (type);
7546           parm_decl = build_decl (PARM_DECL, name, real_type);
7547           type = obtain_incomplete_type (type);
7548           register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7549           jdep = CLASSD_LAST (ctxp->classd_list);
7550           JDEP_MISC (jdep) = name;
7551           JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7552         }
7553       else
7554         parm_decl = build_decl (PARM_DECL, name, type);
7555
7556       /* Remember if a local variable was declared final (via its
7557          TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7558       if (ARG_FINAL_P (tem))
7559         {
7560           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7561           DECL_FINAL (parm_decl) = 1;
7562         }
7563
7564       BLOCK_CHAIN_DECL (parm_decl);
7565     }
7566   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7567   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7568     nreverse (tem);
7569   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7570   DECL_MAX_LOCALS (current_function_decl) = i;
7571 }
7572
7573 /* Called during parsing. Creates an artificial method declaration.  */
7574
7575 static tree
7576 create_artificial_method (tree class, int flags, tree type,
7577                           tree name, tree args)
7578 {
7579   tree mdecl;
7580   location_t save_location = input_location;
7581
7582   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7583   mdecl = make_node (FUNCTION_TYPE);
7584   TREE_TYPE (mdecl) = type;
7585   TYPE_ARG_TYPES (mdecl) = args;
7586   /* We used to compute the signature of MDECL here and then use
7587      add_method(), but that failed because our caller might modify
7588      the type of the returned method, which trashes the cache in
7589      get_type_from_signature().  */
7590   mdecl = add_method_1 (class, flags, name, mdecl);
7591   input_location = save_location;
7592   DECL_ARTIFICIAL (mdecl) = 1;
7593   return mdecl;
7594 }
7595
7596 /* Starts the body if an artificial method.  */
7597
7598 static void
7599 start_artificial_method_body (tree mdecl)
7600 {
7601 #ifdef USE_MAPPED_LOCATION
7602   DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7603   DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7604 #else
7605   DECL_SOURCE_LINE (mdecl) = 1;
7606   DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7607 #endif
7608   source_start_java_method (mdecl);
7609   enter_block ();
7610 }
7611
7612 static void
7613 end_artificial_method_body (tree mdecl)
7614 {
7615   /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7616      It has to be evaluated first. (if mdecl is current_function_decl,
7617      we have an undefined behavior if no temporary variable is used.) */
7618   tree b = exit_block ();
7619   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7620   exit_block ();
7621 }
7622
7623 /* Dump a tree of some kind.  This is a convenience wrapper for the
7624    dump_* functions in tree-dump.c.  */
7625 static void
7626 dump_java_tree (enum tree_dump_index phase, tree t)
7627 {
7628   FILE *stream;
7629   int flags;
7630
7631   stream = dump_begin (phase, &flags);
7632   flags |= TDF_SLIM;
7633   if (stream)
7634     {
7635       dump_node (t, flags, stream);
7636       dump_end (phase, stream);
7637     }
7638 }
7639
7640 /* Terminate a function and expand its body.  */
7641
7642 static void
7643 source_end_java_method (void)
7644 {
7645   tree fndecl = current_function_decl;
7646
7647   if (!fndecl)
7648     return;
7649
7650   java_parser_context_save_global ();
7651 #ifdef USE_MAPPED_LOCATION
7652   input_location = ctxp->last_ccb_indent1;
7653 #else
7654   input_line = ctxp->last_ccb_indent1;
7655 #endif
7656
7657   /* Turn function bodies with only a NOP expr null, so they don't get
7658      generated at all and we won't get warnings when using the -W
7659      -Wall flags. */
7660   if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7661     BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7662
7663   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7664       && ! flag_emit_class_files)
7665     finish_method (fndecl);
7666
7667   current_function_decl = NULL_TREE;
7668   java_parser_context_restore_global ();
7669   current_function_decl = NULL_TREE;
7670 }
7671
7672 /* Record EXPR in the current function block. Complements compound
7673    expression second operand if necessary.  */
7674
7675 tree
7676 java_method_add_stmt (tree fndecl, tree expr)
7677 {
7678   if (!GET_CURRENT_BLOCK (fndecl))
7679     return NULL_TREE;
7680   return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7681 }
7682
7683 static tree
7684 add_stmt_to_block (tree b, tree type, tree stmt)
7685 {
7686   tree body = BLOCK_EXPR_BODY (b), c;
7687
7688   if (java_error_count)
7689     return body;
7690
7691   if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7692     return body;
7693
7694   BLOCK_EXPR_BODY (b) = c;
7695   TREE_SIDE_EFFECTS (c) = 1;
7696   return c;
7697 }
7698
7699 /* Lays out the methods for the classes seen so far.  */
7700
7701 void
7702 java_layout_seen_class_methods (void)
7703 {
7704   tree previous_list = all_class_list;
7705   tree end = NULL_TREE;
7706   tree current;
7707
7708   while (1)
7709     {
7710       for (current = previous_list;
7711            current != end; current = TREE_CHAIN (current))
7712         {
7713           tree decl = TREE_VALUE (current);
7714           tree cls = TREE_TYPE (decl);
7715
7716           input_location = DECL_SOURCE_LOCATION (decl);
7717
7718           if (! CLASS_LOADED_P (cls))
7719             load_class (cls, 0);
7720
7721           layout_class_methods (cls);
7722         }
7723
7724       /* Note that new classes might have been added while laying out
7725          methods, changing the value of all_class_list.  */
7726
7727       if (previous_list != all_class_list)
7728         {
7729           end = previous_list;
7730           previous_list = all_class_list;
7731         }
7732       else
7733         break;
7734     }
7735 }
7736
7737 static GTY(()) tree stop_reordering;
7738 void
7739 java_reorder_fields (void)
7740 {
7741   tree current;
7742
7743   for (current = gclass_list; current; current = TREE_CHAIN (current))
7744     {
7745       output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7746
7747       if (current_class == stop_reordering)
7748         break;
7749
7750       /* Reverse the fields, but leave the dummy field in front.
7751          Fields are already ordered for Object and Class */
7752       if (TYPE_FIELDS (current_class) && current_class != object_type_node
7753           && current_class != class_type_node)
7754       {
7755         /* If the dummy field is there, reverse the right fields and
7756            just layout the type for proper fields offset */
7757         if (!DECL_NAME (TYPE_FIELDS (current_class)))
7758           {
7759             tree fields = TYPE_FIELDS (current_class);
7760             TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7761             TYPE_SIZE (current_class) = NULL_TREE;
7762           }
7763         /* We don't have a dummy field, we need to layout the class,
7764            after having reversed the fields */
7765         else
7766           {
7767             TYPE_FIELDS (current_class) =
7768               nreverse (TYPE_FIELDS (current_class));
7769             TYPE_SIZE (current_class) = NULL_TREE;
7770           }
7771       }
7772     }
7773   /* There are cases were gclass_list will be empty. */
7774   if (gclass_list)
7775     stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7776 }
7777
7778 /* Layout the methods of all classes loaded in one way or another.
7779    Check methods of source parsed classes. Then reorder the
7780    fields and layout the classes or the type of all source parsed
7781    classes */
7782
7783 void
7784 java_layout_classes (void)
7785 {
7786   tree current;
7787   int save_error_count = java_error_count;
7788
7789   /* Layout the methods of all classes seen so far */
7790   java_layout_seen_class_methods ();
7791   java_parse_abort_on_error ();
7792   all_class_list = NULL_TREE;
7793
7794   /* Then check the methods of all parsed classes */
7795   for (current = gclass_list; current; current = TREE_CHAIN (current))
7796     if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7797       java_check_methods (TREE_VALUE (current));
7798   java_parse_abort_on_error ();
7799
7800   for (current = gclass_list; current; current = TREE_CHAIN (current))
7801     {
7802       output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7803       layout_class (current_class);
7804
7805       /* Error reported by the caller */
7806       if (java_error_count)
7807         return;
7808     }
7809
7810   /* We might have reloaded classes durign the process of laying out
7811      classes for code generation. We must layout the methods of those
7812      late additions, as constructor checks might use them */
7813   java_layout_seen_class_methods ();
7814   java_parse_abort_on_error ();
7815 }
7816
7817 /* Expand methods in the current set of classes remembered for
7818    generation.  */
7819
7820 static void
7821 java_complete_expand_classes (void)
7822 {
7823   tree current;
7824
7825   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7826     if (!INNER_CLASS_DECL_P (current))
7827       java_complete_expand_class (current);
7828 }
7829
7830 /* Expand the methods found in OUTER, starting first by OUTER's inner
7831    classes, if any.  */
7832
7833 static void
7834 java_complete_expand_class (tree outer)
7835 {
7836   tree inner_list;
7837
7838   /* We need to go after all inner classes and start expanding them,
7839      starting with most nested ones. We have to do that because nested
7840      classes might add functions to outer classes */
7841
7842   for (inner_list = DECL_INNER_CLASS_LIST (outer);
7843        inner_list; inner_list = TREE_CHAIN (inner_list))
7844     java_complete_expand_class (TREE_PURPOSE (inner_list));
7845
7846   java_complete_expand_methods (outer);
7847 }
7848
7849 /* Expand methods registered in CLASS_DECL. The general idea is that
7850    we expand regular methods first. This allows us get an estimate on
7851    how outer context local alias fields are really used so we can add
7852    to the constructor just enough code to initialize them properly (it
7853    also lets us generate finit$ correctly.) Then we expand the
7854    constructors and then <clinit>.  */
7855
7856 static void
7857 java_complete_expand_methods (tree class_decl)
7858 {
7859   tree clinit, decl, first_decl;
7860
7861   output_class = current_class = TREE_TYPE (class_decl);
7862
7863   /* Pre-expand <clinit> to figure whether we really need it or
7864      not. If we do need it, we pre-expand the static fields so they're
7865      ready to be used somewhere else. <clinit> will be fully expanded
7866      after we processed the constructors. */
7867   first_decl = TYPE_METHODS (current_class);
7868   clinit = maybe_generate_pre_expand_clinit (current_class);
7869
7870   /* Then generate finit$ (if we need to) because constructors will
7871    try to use it.*/
7872   if (TYPE_FINIT_STMT_LIST (current_class))
7873     java_complete_expand_method (generate_finit (current_class));
7874
7875   /* Then generate instinit$ (if we need to) because constructors will
7876      try to use it. */
7877   if (TYPE_II_STMT_LIST (current_class))
7878     java_complete_expand_method (generate_instinit (current_class));
7879
7880   /* Now do the constructors */
7881   for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7882     {
7883       if (!DECL_CONSTRUCTOR_P (decl))
7884         continue;
7885       java_complete_expand_method (decl);
7886     }
7887
7888   /* First, do the ordinary methods. */
7889   for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7890     {
7891       /* Ctors aren't part of this batch. */
7892       if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7893         continue;
7894
7895       /* Skip abstract or native methods -- but do handle native
7896          methods when generating JNI stubs.  */
7897       if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7898         {
7899           DECL_FUNCTION_BODY (decl) = NULL_TREE;
7900           continue;
7901         }
7902
7903       if (METHOD_NATIVE (decl))
7904         {
7905           tree body;
7906           current_function_decl = decl;
7907           body = build_jni_stub (decl);
7908           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7909         }
7910
7911       java_complete_expand_method (decl);
7912     }
7913
7914   /* If there is indeed a <clinit>, fully expand it now */
7915   if (clinit)
7916     {
7917       /* Prevent the use of `this' inside <clinit> */
7918       ctxp->explicit_constructor_p = 1;
7919       java_complete_expand_method (clinit);
7920       ctxp->explicit_constructor_p = 0;
7921     }
7922
7923   /* We might have generated a class$ that we now want to expand */
7924   if (TYPE_DOT_CLASS (current_class))
7925     java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7926
7927   /* Now verify constructor circularity (stop after the first one we
7928      prove wrong.) */
7929   if (!CLASS_INTERFACE (class_decl))
7930     for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7931       if (DECL_CONSTRUCTOR_P (decl)
7932           && verify_constructor_circularity (decl, decl))
7933         break;
7934 }
7935
7936 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7937    safely used in some other methods/constructors.  */
7938
7939 static tree
7940 maybe_generate_pre_expand_clinit (tree class_type)
7941 {
7942   tree current, mdecl;
7943
7944   if (!TYPE_CLINIT_STMT_LIST (class_type))
7945     return NULL_TREE;
7946
7947   /* Go through all static fields and pre expand them */
7948   for (current = TYPE_FIELDS (class_type); current;
7949        current = TREE_CHAIN (current))
7950     if (FIELD_STATIC (current))
7951       build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7952
7953   /* Then build the <clinit> method */
7954   mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7955                                     clinit_identifier_node, end_params_node);
7956   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7957                        mdecl, NULL_TREE);
7958   start_artificial_method_body (mdecl);
7959
7960   /* We process the list of assignment we produced as the result of
7961      the declaration of initialized static field and add them as
7962      statement to the <clinit> method. */
7963   for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7964        current = TREE_CHAIN (current))
7965     {
7966       tree stmt = current;
7967       /* We build the assignment expression that will initialize the
7968          field to its value. There are strict rules on static
7969          initializers (8.5). FIXME */
7970       if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
7971         stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7972       java_method_add_stmt (mdecl, stmt);
7973     }
7974
7975   end_artificial_method_body (mdecl);
7976
7977   /* Now we want to place <clinit> as the last method (because we need
7978      it at least for interface so that it doesn't interfere with the
7979      dispatch table based lookup. */
7980   if (TREE_CHAIN (TYPE_METHODS (class_type)))
7981     {
7982       current = TREE_CHAIN (TYPE_METHODS (class_type));
7983       TYPE_METHODS (class_type) = current;
7984
7985       while (TREE_CHAIN (current))
7986         current = TREE_CHAIN (current);
7987
7988       TREE_CHAIN (current) = mdecl;
7989       TREE_CHAIN (mdecl) = NULL_TREE;
7990     }
7991
7992   return mdecl;
7993 }
7994
7995 /* Analyzes a method body and look for something that isn't a
7996    MODIFY_EXPR with a constant value.  */
7997
7998 static int
7999 analyze_clinit_body (tree this_class, tree bbody)
8000 {
8001   while (bbody)
8002     switch (TREE_CODE (bbody))
8003       {
8004       case BLOCK:
8005         bbody = BLOCK_EXPR_BODY (bbody);
8006         break;
8007
8008       case EXPR_WITH_FILE_LOCATION:
8009         bbody = EXPR_WFL_NODE (bbody);
8010         break;
8011
8012       case COMPOUND_EXPR:
8013         if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
8014           return 1;
8015         bbody = TREE_OPERAND (bbody, 1);
8016         break;
8017
8018       case MODIFY_EXPR:
8019         /* If we're generating to class file and we're dealing with an
8020            array initialization, we return 1 to keep <clinit> */
8021         if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
8022             && flag_emit_class_files)
8023           return 1;
8024
8025         /* There are a few cases where we're required to keep
8026            <clinit>:
8027            - If this is an assignment whose operand is not constant,
8028            - If this is an assignment to a non-initialized field,
8029            - If this field is not a member of the current class.
8030         */
8031         return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8032                 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8033                 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8034
8035       default:
8036         return 1;
8037       }
8038   return 0;
8039 }
8040
8041
8042 /* See whether we could get rid of <clinit>. Criteria are: all static
8043    final fields have constant initial values and the body of <clinit>
8044    is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8045
8046 static int
8047 maybe_yank_clinit (tree mdecl)
8048 {
8049   tree type, current;
8050   tree fbody, bbody;
8051
8052   if (!DECL_CLINIT_P (mdecl))
8053     return 0;
8054
8055   /* If the body isn't empty, then we keep <clinit>. Note that if
8056      we're emitting classfiles, this isn't enough not to rule it
8057      out. */
8058   fbody = DECL_FUNCTION_BODY (mdecl);
8059   bbody = BLOCK_EXPR_BODY (fbody);
8060   if (bbody && bbody != error_mark_node)
8061     bbody = BLOCK_EXPR_BODY (bbody);
8062   else
8063     return 0;
8064   if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8065     return 0;
8066
8067   type = DECL_CONTEXT (mdecl);
8068   current = TYPE_FIELDS (type);
8069
8070   for (current = (current ? TREE_CHAIN (current) : current);
8071        current; current = TREE_CHAIN (current))
8072     {
8073       tree f_init;
8074
8075       /* We're not interested in non-static fields.  */
8076       if (!FIELD_STATIC (current))
8077         continue;
8078
8079       /* Nor in fields without initializers. */
8080       f_init = DECL_INITIAL (current);
8081       if (f_init == NULL_TREE)
8082         continue;
8083
8084       /* Anything that isn't String or a basic type is ruled out -- or
8085          if we know how to deal with it (when doing things natively) we
8086          should generated an empty <clinit> so that SUID are computed
8087          correctly. */
8088       if (! JSTRING_TYPE_P (TREE_TYPE (current))
8089           && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8090         return 0;
8091
8092       if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8093         return 0;
8094     }
8095
8096   /* Now we analyze the method body and look for something that
8097      isn't a MODIFY_EXPR */
8098   if (bbody && !IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8099     return 0;
8100
8101   /* Get rid of <clinit> in the class' list of methods */
8102   if (TYPE_METHODS (type) == mdecl)
8103     TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8104   else
8105     for (current = TYPE_METHODS (type); current;
8106          current = TREE_CHAIN (current))
8107       if (TREE_CHAIN (current) == mdecl)
8108         {
8109           TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8110           break;
8111         }
8112
8113   return 1;
8114 }
8115
8116 /* Install the argument from MDECL. Suitable to completion and
8117    expansion of mdecl's body.  */
8118
8119 void
8120 start_complete_expand_method (tree mdecl)
8121 {
8122   tree tem;
8123
8124   pushlevel (1);                /* Prepare for a parameter push */
8125   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8126   DECL_ARGUMENTS (mdecl) = tem;
8127
8128   for (; tem; tem = TREE_CHAIN (tem))
8129     {
8130       /* TREE_CHAIN (tem) will change after pushdecl. */
8131       tree next = TREE_CHAIN (tem);
8132       tree type = TREE_TYPE (tem);
8133       if (targetm.calls.promote_prototypes (type)
8134           && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8135           && INTEGRAL_TYPE_P (type))
8136         type = integer_type_node;
8137       DECL_ARG_TYPE (tem) = type;
8138       layout_decl (tem, 0);
8139       pushdecl (tem);
8140       /* Re-install the next so that the list is kept and the loop
8141          advances. */
8142       TREE_CHAIN (tem) = next;
8143     }
8144   pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8145   input_location = DECL_SOURCE_LOCATION (mdecl);
8146   build_result_decl (mdecl);
8147 }
8148
8149
8150 /* Complete and expand a method.  */
8151
8152 static void
8153 java_complete_expand_method (tree mdecl)
8154 {
8155   tree fbody, block_body, exception_copy;
8156
8157   current_function_decl = mdecl;
8158   /* Fix constructors before expanding them */
8159   if (DECL_CONSTRUCTOR_P (mdecl))
8160     fix_constructors (mdecl);
8161
8162   /* Expand functions that have a body */
8163   if (!DECL_FUNCTION_BODY (mdecl))
8164     return;
8165
8166   fbody = DECL_FUNCTION_BODY (mdecl);
8167   block_body = BLOCK_EXPR_BODY (fbody);
8168   exception_copy = NULL_TREE;
8169
8170   current_function_decl = mdecl;
8171
8172   if (! quiet_flag)
8173     fprintf (stderr, " [%s.",
8174              lang_printable_name (DECL_CONTEXT (mdecl), 0));
8175   announce_function (mdecl);
8176   if (! quiet_flag)
8177     fprintf (stderr, "]");
8178
8179   /* Prepare the function for tree completion */
8180   start_complete_expand_method (mdecl);
8181
8182   /* Install the current this */
8183   current_this = (!METHOD_STATIC (mdecl) ?
8184                   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8185
8186   /* Install exceptions thrown with `throws' */
8187   PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8188
8189   if (block_body != NULL_TREE)
8190     {
8191       block_body = java_complete_tree (block_body);
8192
8193       /* Before we check initialization, attached all class initialization
8194          variable to the block_body */
8195       htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8196                      attach_init_test_initialization_flags, block_body);
8197
8198       if (! METHOD_NATIVE (mdecl))
8199         {
8200           check_for_initialization (block_body, mdecl);
8201
8202           /* Go through all the flags marking the initialization of
8203              static variables and see whether they're definitively
8204              assigned, in which case the type is remembered as
8205              definitively initialized in MDECL. */
8206           if (STATIC_CLASS_INIT_OPT_P ())
8207             {
8208               /* Always register the context as properly initialized in
8209                  MDECL. This used with caution helps removing extra
8210                  initialization of self. */
8211               if (METHOD_STATIC (mdecl))
8212                 {
8213                   *(htab_find_slot
8214                     (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8215                      DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8216                 }
8217             }
8218         }
8219       ctxp->explicit_constructor_p = 0;
8220     }
8221
8222   BLOCK_EXPR_BODY (fbody) = block_body;
8223
8224   /* If we saw a return but couldn't evaluate it properly, we'll have
8225      an error_mark_node here. */
8226   if (block_body != error_mark_node
8227       && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8228       && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
8229     missing_return_error (current_function_decl);
8230
8231   /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8232   maybe_yank_clinit (mdecl);
8233
8234   /* Pop the current level, with special measures if we found errors. */
8235   if (java_error_count)
8236     pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8237   poplevel (1, 0, 1);
8238
8239   /* Pop the exceptions and sanity check */
8240   POP_EXCEPTIONS();
8241   if (currently_caught_type_list)
8242     abort ();
8243 }
8244
8245 /* For with each class for which there's code to generate. */
8246
8247 static void
8248 java_expand_method_bodies (tree class)
8249 {
8250   tree decl;
8251   for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8252     {
8253       tree block;
8254
8255       if (! DECL_FUNCTION_BODY (decl))
8256         continue;
8257
8258       current_function_decl = decl;
8259
8260       block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8261
8262       /* Save the function body for gimplify and inlining.  */
8263       DECL_SAVED_TREE (decl) = block;
8264
8265       /* It's time to assign the variable flagging static class
8266          initialization based on which classes invoked static methods
8267          are definitely initializing. This should be flagged. */
8268       if (STATIC_CLASS_INIT_OPT_P ())
8269         {
8270           tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8271           for (; list != NULL_TREE;  list = TREE_CHAIN (list))
8272             {
8273               /* Executed for each statement calling a static function.
8274                  LIST is a TREE_LIST whose PURPOSE is the called function
8275                  and VALUE is a compound whose second operand can be patched
8276                  with static class initialization flag assignments.  */
8277
8278               tree called_method = TREE_PURPOSE (list);
8279               tree compound = TREE_VALUE (list);
8280               tree assignment_compound_list
8281                 = build_tree_list (called_method, NULL);
8282
8283               /* For each class definitely initialized in
8284                  CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8285                  assignment to the class initialization flag. */
8286               htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8287                              emit_test_initialization,
8288                              assignment_compound_list);
8289
8290               if (TREE_VALUE (assignment_compound_list))
8291                 TREE_OPERAND (compound, 1)
8292                   = TREE_VALUE (assignment_compound_list);
8293             }
8294         }
8295
8296       /* Expand the function body.  */
8297       source_end_java_method ();
8298     }
8299 }
8300
8301 \f
8302
8303 /* This section of the code deals with accessing enclosing context
8304    fields either directly by using the relevant access to this$<n> or
8305    by invoking an access method crafted for that purpose.  */
8306
8307 /* Build the necessary access across nested class boundaries.
8308    This routine could be optimized to cache previous result
8309    (decl, current_class and returned access).  When an access method
8310    needs to be generated, it always takes the form of a read.  It might
8311    be later turned into a write by calling nested_field_access_fix.  */
8312
8313 static tree
8314 build_nested_field_access (tree id, tree decl)
8315 {
8316   tree access = NULL_TREE;
8317   tree ctx = NULL_TREE;
8318   tree decl_ctx = DECL_CONTEXT (decl);
8319   bool is_static = FIELD_STATIC (decl);
8320
8321   if (DECL_CONTEXT (TYPE_NAME (current_class)))
8322     ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8323
8324   /* For non-static fields, if the immediate enclosing context of the
8325      current class is the field decl's class or inherits from it,
8326      build the access as `this$<n>.<field>'.  Note that we will break
8327      the `private' barrier if we're not emitting bytecodes.  */
8328   if (!is_static
8329       && ctx
8330       && (ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8331       && (!FIELD_PRIVATE (decl) || !flag_emit_class_files))
8332     {
8333       tree thisn = build_current_thisn (current_class);
8334       access = make_qualified_primary (build_wfl_node (thisn),
8335                                        id, EXPR_WFL_LINECOL (id));
8336     }
8337   /* Otherwise, generate and use accessor methods for the field as
8338      needed.  */
8339   else
8340     {
8341       int lc = EXPR_WFL_LINECOL (id);
8342
8343       /* Now we chain the required number of calls to the access$0 to
8344          get a hold to the enclosing instance we need for a non-static
8345          field, and then we build the field access. */
8346       if (!is_static)
8347         access = build_access_to_thisn (current_class, decl_ctx, lc);
8348
8349       /* If the field is private and we're generating bytecode, then
8350          we generate an access method.  */
8351       if (FIELD_PRIVATE (decl) && flag_emit_class_files)
8352         {
8353           tree name = build_nested_field_access_methods (decl);
8354           access = build_nested_field_access_expr (lc, decl_ctx,
8355                                                    name, access, NULL_TREE);
8356         }
8357       /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'
8358          for non-static fields.
8359          Once again we break the `private' access rule from a foreign
8360          class.  */
8361       else if (is_static)
8362         {
8363           tree class_name = DECL_NAME (TYPE_NAME (decl_ctx));
8364           access
8365             = make_qualified_primary (build_wfl_node (class_name), id, lc);
8366         }
8367       else
8368         access = make_qualified_primary (access, id, lc);
8369     }
8370
8371   return resolve_expression_name (access, NULL);
8372 }
8373
8374 /* Return a nonzero value if DECL describes a field access across nested
8375    class boundaries.  That is, DECL is in a class that either encloses,
8376    is enclosed by or shares a common enclosing class with, the class
8377    TYPE.  */
8378
8379 static int
8380 nested_field_access_p (tree type, tree decl)
8381 {
8382   bool is_static = false;
8383   tree decl_type = DECL_CONTEXT (decl);
8384   tree type_root, decl_type_root;
8385
8386   if (decl_type == type
8387       || (TREE_CODE (decl) != FIELD_DECL && TREE_CODE (decl) != VAR_DECL))
8388     return 0;
8389   
8390   if (!INNER_CLASS_TYPE_P (type)
8391       && !(TREE_CODE (decl_type) == RECORD_TYPE
8392            && INNER_CLASS_TYPE_P (decl_type)))
8393     return 0;
8394
8395   is_static = FIELD_STATIC (decl);
8396
8397   /* If TYPE extends the declaration context of the non-static
8398      field we're trying to access, then this isn't a nested field
8399      access we need to worry about.  */
8400   if (!is_static && inherits_from_p (type, decl_type))
8401     return 0;
8402
8403   for (type_root = type;
8404        DECL_CONTEXT (TYPE_NAME (type_root));
8405        type_root = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type_root))))
8406     {
8407       if (type_root == decl_type)
8408         return 1;
8409     }
8410
8411   if (TREE_CODE (decl_type) == RECORD_TYPE
8412       && INNER_CLASS_TYPE_P (decl_type))
8413     {
8414       for (decl_type_root = decl_type;
8415            DECL_CONTEXT (TYPE_NAME (decl_type_root));
8416            decl_type_root
8417              = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (decl_type_root))))
8418         {
8419           if (decl_type_root == type)
8420             return 1;
8421         }
8422     }
8423   else
8424     decl_type_root = decl_type;
8425     
8426   if (type_root == decl_type_root)
8427     return 1;
8428
8429   /* Before we give up, see whether it is a non-static field
8430      inherited from the enclosing context we are considering.  */
8431   if (!DECL_CONTEXT (TYPE_NAME (type_root))
8432       && !is_static
8433       && inherits_from_p (type_root, decl_type))
8434     return 1;
8435
8436   return 0;
8437 }
8438
8439 /* Return a nonzero value if NODE represents a cross-nested-class 
8440    access that has already been expanded.  As a side effect, it returns
8441    the name of the field being accessed and the argument passed to the
8442    access function, suitable for a regeneration of the access method
8443    call if necessary.  */
8444
8445 static int
8446 nested_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8447                                 tree *arg)
8448 {
8449   int identified = 0;
8450
8451   if (TREE_CODE (node) != CALL_EXPR)
8452     return 0;
8453
8454   /* Well, GCJ generates slightly different tree nodes when compiling
8455      to native or bytecodes.  It's the case for function calls.  */
8456
8457   if (flag_emit_class_files
8458       && TREE_CODE (node) == CALL_EXPR
8459       && NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8460     identified = 1;
8461   else if (!flag_emit_class_files)
8462     {
8463       node = TREE_OPERAND (node, 0);
8464
8465       if (node && TREE_OPERAND (node, 0)
8466           && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8467         {
8468           node = TREE_OPERAND (node, 0);
8469           if (TREE_OPERAND (node, 0)
8470               && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8471               && (NESTED_FIELD_ACCESS_IDENTIFIER_P
8472                   (DECL_NAME (TREE_OPERAND (node, 0)))))
8473             identified = 1;
8474         }
8475     }
8476
8477   if (identified && name && arg_type && arg)
8478     {
8479       tree argument = TREE_OPERAND (node, 1);
8480       *name = DECL_NAME (TREE_OPERAND (node, 0));
8481
8482       /* The accessors for static fields do not take in a this$<n> argument,
8483          so we take the class name from the accessor's context instead.  */
8484       if (argument)
8485         {
8486           *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8487           *arg = TREE_VALUE (argument);
8488         }
8489       else
8490         {
8491           *arg_type = DECL_CONTEXT (TREE_OPERAND (node, 0));
8492           *arg = NULL_TREE;
8493         }
8494     }
8495   return identified;
8496 }
8497
8498 /* Detect in NODE cross-nested-class field read access and
8499    transform it into a write with RHS as an argument.  This function
8500    is called from the java_complete_lhs when an assignment to a LHS can
8501    be identified.  */
8502
8503 static tree
8504 nested_field_access_fix (tree wfl, tree node, tree rhs)
8505 {
8506   tree name, arg_type, arg;
8507
8508   if (nested_field_expanded_access_p (node, &name, &arg_type, &arg))
8509     {
8510       node = build_nested_field_access_expr (EXPR_WFL_LINECOL (wfl),
8511                                              arg_type, name, arg, rhs);
8512       return java_complete_tree (node);
8513     }
8514   return NULL_TREE;
8515 }
8516
8517 /* Construct the expression that calls an access method:
8518      <type>.access$<n>(<arg1> [, <arg2>]);
8519
8520    ARG2 can be NULL and will be omitted in that case. It will denote a
8521    read access.  */
8522
8523 static tree
8524 build_nested_field_access_expr (int lc, tree type, tree access_method_name,
8525                                 tree arg1, tree arg2)
8526 {
8527   tree args, cn, access;
8528
8529   if (arg1)
8530     args = build_tree_list (NULL_TREE, arg1);
8531   else
8532     args = NULL_TREE;
8533
8534   if (arg2)
8535     {
8536       if (args)
8537         args = tree_cons (NULL_TREE, arg2, args);
8538       else
8539         args = build_tree_list (NULL_TREE, arg2);
8540     }
8541
8542   access
8543     = build_method_invocation (build_wfl_node (access_method_name), args);
8544   cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8545
8546   return make_qualified_primary (cn, access, lc);
8547 }
8548
8549 /* Build the name of a synthetic accessor used to access class members
8550    across nested class boundaries.  */
8551
8552 static tree
8553 build_new_access_id (void)
8554 {
8555   static int access_n_counter = 1;
8556   char buffer [128];
8557
8558   sprintf (buffer, "access$%d", access_n_counter++);
8559   return get_identifier (buffer);
8560 }
8561
8562 /* Create the static access functions for the cross-nested-class field DECL.
8563    We define a read:
8564      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8565        return inst$.field;
8566      }
8567    and a write access:
8568      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8569                                      TREE_TYPE (<field>) value$) {
8570        return inst$.field = value$;
8571      }
8572    For static fields, these methods are generated without the instance
8573    parameter.
8574    We should have a usage flag on the DECL so we can lazily turn the ones
8575    we're using for code generation.  FIXME.
8576 */
8577
8578 static tree
8579 build_nested_field_access_methods (tree decl)
8580 {
8581   tree id, args, stmt, mdecl, class_name = NULL_TREE;
8582   bool is_static = FIELD_STATIC (decl);
8583
8584   if (FIELD_NESTED_ACCESS_P (decl))
8585     return FIELD_NESTED_ACCESS (decl);
8586
8587   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8588
8589   /* Create the identifier and a function named after it.  */
8590   id = build_new_access_id ();
8591
8592   /* The identifier is marked as bearing the name of a generated write
8593      access function for outer field accessed from inner classes.  */
8594   NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8595
8596   /* Create the read access.  */
8597   if (!is_static)
8598     {
8599       args = build_tree_list (inst_id,
8600                               build_pointer_type (DECL_CONTEXT (decl)));
8601       TREE_CHAIN (args) = end_params_node;
8602       stmt = make_qualified_primary (build_wfl_node (inst_id),
8603                                      build_wfl_node (DECL_NAME (decl)), 0);
8604     }
8605   else
8606     {
8607       args = end_params_node;
8608       class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
8609       stmt = make_qualified_primary (build_wfl_node (class_name),
8610                                      build_wfl_node (DECL_NAME (decl)), 0);
8611     }
8612   stmt = build_return (0, stmt);
8613   mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8614                                             TREE_TYPE (decl), id, args, stmt);
8615   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8616
8617   /* Create the write access method.  No write access for final variable */
8618   if (!FIELD_FINAL (decl))
8619     {
8620       if (!is_static)
8621         {
8622           args = build_tree_list (inst_id,
8623                                   build_pointer_type (DECL_CONTEXT (decl)));
8624           TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8625           TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8626           stmt = make_qualified_primary (build_wfl_node (inst_id),
8627                                          build_wfl_node (DECL_NAME (decl)),
8628                                          0);
8629         }
8630       else
8631         {
8632           args = build_tree_list (wpv_id, TREE_TYPE (decl));
8633           TREE_CHAIN (args) = end_params_node;
8634           stmt = make_qualified_primary (build_wfl_node (class_name),
8635                                          build_wfl_node (DECL_NAME (decl)),
8636                                          0);
8637         }
8638       stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8639                                                 build_wfl_node (wpv_id)));
8640       mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8641                                                 TREE_TYPE (decl), id,
8642                                                 args, stmt);
8643     }
8644   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8645
8646   /* Return the access name */
8647   return FIELD_NESTED_ACCESS (decl) = id;
8648 }
8649
8650 /* Build a field access method NAME.  */
8651
8652 static tree
8653 build_nested_field_access_method (tree class, tree type, tree name,
8654                                   tree args, tree body)
8655 {
8656   tree saved_current_function_decl, mdecl;
8657
8658   /* Create the method */
8659   mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8660   fix_method_argument_names (args, mdecl);
8661   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8662
8663   /* Attach the method body. */
8664   saved_current_function_decl = current_function_decl;
8665   start_artificial_method_body (mdecl);
8666   java_method_add_stmt (mdecl, body);
8667   end_artificial_method_body (mdecl);
8668   current_function_decl = saved_current_function_decl;
8669
8670   return mdecl;
8671 }
8672
8673 \f
8674 /* This section deals with building access function necessary for
8675    certain kinds of method invocation from inner classes.  */
8676
8677 static tree
8678 build_outer_method_access_method (tree decl)
8679 {
8680   tree saved_current_function_decl, mdecl;
8681   tree args = NULL_TREE, call_args = NULL_TREE;
8682   tree carg, id, body, class;
8683   char buffer [80];
8684   int parm_id_count = 0;
8685
8686   /* Test this abort with an access to a private field */
8687   if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8688     abort ();
8689
8690   /* Check the cache first */
8691   if (DECL_FUNCTION_INNER_ACCESS (decl))
8692     return DECL_FUNCTION_INNER_ACCESS (decl);
8693
8694   class = DECL_CONTEXT (decl);
8695
8696   /* Obtain an access identifier and mark it */
8697   id = build_new_access_id ();
8698   NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8699
8700   carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8701   /* Create the arguments, as much as the original */
8702   for (; carg && carg != end_params_node;
8703        carg = TREE_CHAIN (carg))
8704     {
8705       sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8706       args = chainon (args, build_tree_list (get_identifier (buffer),
8707                                              TREE_VALUE (carg)));
8708     }
8709   args = chainon (args, end_params_node);
8710
8711   /* Create the method */
8712   mdecl = create_artificial_method (class, ACC_STATIC,
8713                                     TREE_TYPE (TREE_TYPE (decl)), id, args);
8714   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8715   /* There is a potential bug here. We should be able to use
8716      fix_method_argument_names, but then arg names get mixed up and
8717      eventually a constructor will have its this$0 altered and the
8718      outer context won't be assignment properly. The testcase is
8719      stub.java FIXME */
8720   TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8721
8722   /* Attach the method body. */
8723   saved_current_function_decl = current_function_decl;
8724   start_artificial_method_body (mdecl);
8725
8726   /* The actual method invocation uses the same args. When invoking a
8727      static methods that way, we don't want to skip the first
8728      argument. */
8729   carg = args;
8730   if (!METHOD_STATIC (decl))
8731     carg = TREE_CHAIN (carg);
8732   for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8733     call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8734                            call_args);
8735
8736   body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8737                                   call_args);
8738   if (!METHOD_STATIC (decl))
8739     body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8740                                    body, 0);
8741   if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8742     body = build_return (0, body);
8743   java_method_add_stmt (mdecl,body);
8744   end_artificial_method_body (mdecl);
8745   current_function_decl = saved_current_function_decl;
8746
8747   /* Back tag the access function so it know what it accesses */
8748   DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8749
8750   /* Tag the current method so it knows it has an access generated */
8751   return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8752 }
8753
8754 \f
8755 /* This section of the code deals with building expressions to access
8756    the enclosing instance of an inner class. The enclosing instance is
8757    kept in a generated field called this$<n>, with <n> being the
8758    inner class nesting level (starting from 0.)  */
8759
8760 /* Build an access to a given this$<n>, always chaining access call to
8761    others. Access methods to this$<n> are build on the fly if
8762    necessary. This CAN'T be used to solely access this$<n-1> from
8763    this$<n> (which alway yield to special cases and optimization, see
8764    for example build_nested_field_access).  */
8765
8766 static tree
8767 build_access_to_thisn (tree from, tree to, int lc)
8768 {
8769   tree access = NULL_TREE;
8770
8771   while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8772     {
8773       if (!access)
8774         {
8775           access = build_current_thisn (from);
8776           access = build_wfl_node (access);
8777         }
8778       else
8779         {
8780           tree access0_wfl, cn;
8781
8782           maybe_build_thisn_access_method (from);
8783           access0_wfl = build_wfl_node (access0_identifier_node);
8784           cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8785           EXPR_WFL_LINECOL (access0_wfl) = lc;
8786           access = build_tree_list (NULL_TREE, access);
8787           access = build_method_invocation (access0_wfl, access);
8788           access = make_qualified_primary (cn, access, lc);
8789         }
8790
8791       /* If FROM isn't an inner class, that's fine, we've done enough.
8792          What we're looking for can be accessed from there.  */
8793       from = DECL_CONTEXT (TYPE_NAME (from));
8794       if (!from)
8795         break;
8796       from = TREE_TYPE (from);
8797     }
8798   return access;
8799 }
8800
8801 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8802    is returned if nothing needs to be generated. Otherwise, the method
8803    generated and a method decl is returned.
8804
8805    NOTE: These generated methods should be declared in a class file
8806    attribute so that they can't be referred to directly.  */
8807
8808 static tree
8809 maybe_build_thisn_access_method (tree type)
8810 {
8811   tree mdecl, args, stmt, rtype;
8812   tree saved_current_function_decl;
8813
8814   /* If TYPE is a top-level class, no access method is required.
8815      If there already is such an access method, bail out. */
8816   if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8817     return NULL_TREE;
8818
8819   /* We generate the method. The method looks like:
8820      static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8821   */
8822   args = build_tree_list (inst_id, build_pointer_type (type));
8823   TREE_CHAIN (args) = end_params_node;
8824   rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8825   mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8826                                     access0_identifier_node, args);
8827   fix_method_argument_names (args, mdecl);
8828   layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8829   stmt = build_current_thisn (type);
8830   stmt = make_qualified_primary (build_wfl_node (inst_id),
8831                                  build_wfl_node (stmt), 0);
8832   stmt = build_return (0, stmt);
8833
8834   saved_current_function_decl = current_function_decl;
8835   start_artificial_method_body (mdecl);
8836   java_method_add_stmt (mdecl, stmt);
8837   end_artificial_method_body (mdecl);
8838   current_function_decl = saved_current_function_decl;
8839
8840   CLASS_ACCESS0_GENERATED_P (type) = 1;
8841
8842   return mdecl;
8843 }
8844
8845 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8846    the first level of innerclassing. this$1 for the next one, etc...
8847    This function can be invoked with TYPE to NULL, available and then
8848    has to count the parser context.  */
8849
8850 static GTY(()) tree saved_thisn;
8851 static GTY(()) tree saved_type;
8852
8853 static tree
8854 build_current_thisn (tree type)
8855 {
8856   static int saved_i = -1;
8857   static int saved_type_i = 0;
8858   tree decl;
8859   char buffer [24];
8860   int i = 0;
8861
8862   if (type)
8863     {
8864       if (type == saved_type)
8865         i = saved_type_i;
8866       else
8867         {
8868           for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8869                decl; decl = DECL_CONTEXT (decl), i++)
8870             ;
8871
8872           saved_type = type;
8873           saved_type_i = i;
8874         }
8875     }
8876   else
8877     i = list_length (GET_CPC_LIST ())-2;
8878
8879   if (i == saved_i)
8880     return saved_thisn;
8881
8882   sprintf (buffer, "this$%d", i);
8883   saved_i = i;
8884   saved_thisn = get_identifier (buffer);
8885   return saved_thisn;
8886 }
8887
8888 /* Return the assignment to the hidden enclosing context `this$<n>'
8889    by the second incoming parameter to the innerclass constructor. The
8890    form used is `this.this$<n> = this$<n>;'.  */
8891
8892 static tree
8893 build_thisn_assign (void)
8894 {
8895   if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8896     {
8897       tree thisn = build_current_thisn (current_class);
8898       tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8899                                          build_wfl_node (thisn), 0);
8900       tree rhs = build_wfl_node (thisn);
8901 #ifdef USE_MAPPED_LOCATION
8902       SET_EXPR_LOCATION (lhs, input_location);
8903 #else
8904       EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8905 #endif
8906       return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8907     }
8908   return NULL_TREE;
8909 }
8910
8911 \f
8912 /* Building the synthetic `class$' used to implement the `.class' 1.1
8913    extension for non primitive types. This method looks like:
8914
8915     static Class class$(String type) throws NoClassDefFoundError
8916     {
8917       try {return (java.lang.Class.forName (String));}
8918       catch (ClassNotFoundException e) {
8919         throw new NoClassDefFoundError(e.getMessage());}
8920     } */
8921
8922 static GTY(()) tree get_message_wfl;
8923 static GTY(()) tree type_parm_wfl;
8924
8925 static tree
8926 build_dot_class_method (tree class)
8927 {
8928 #define BWF(S) build_wfl_node (get_identifier ((S)))
8929 #ifdef USE_MAPPED_LOCATION
8930 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8931 #else
8932 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8933 #endif
8934   tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8935   tree stmt, throw_stmt;
8936
8937   if (!get_message_wfl)
8938     {
8939       get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8940       type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8941     }
8942
8943   /* Build the arguments */
8944   args = build_tree_list (get_identifier ("type$"),
8945                           build_pointer_type (string_type_node));
8946   TREE_CHAIN (args) = end_params_node;
8947
8948   /* Build the qualified name java.lang.Class.forName */
8949   tmp = MQN (MQN (MQN (BWF ("java"),
8950                        BWF ("lang")), BWF ("Class")), BWF ("forName"));
8951
8952   /* Create the "class$" function */
8953   mdecl = create_artificial_method (class, ACC_STATIC,
8954                                     build_pointer_type (class_type_node),
8955                                     classdollar_identifier_node, args);
8956   qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8957                    BWF ("NoClassDefFoundError"));
8958   DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8959   register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8960   JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8961     &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8962
8963   /* We start by building the try block. We need to build:
8964        return (java.lang.Class.forName (type)); */
8965   stmt = build_method_invocation (tmp,
8966                                   build_tree_list (NULL_TREE, type_parm_wfl));
8967   stmt = build_return (0, stmt);
8968
8969   /* Now onto the catch block. We start by building the expression
8970      throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8971 #ifdef USE_MAPPED_LOCATION
8972   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8973                                     get_message_wfl, UNKNOWN_LOCATION);
8974 #else
8975   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8976                                     get_message_wfl, 0);
8977 #endif
8978   throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8979
8980   /* Build new NoClassDefFoundError (_.getMessage) */
8981   throw_stmt = build_new_invocation
8982     (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8983      build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8984
8985   /* Build the throw, (it's too early to use BUILD_THROW) */
8986   throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8987
8988   /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8989   qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8990                    BWF ("ClassNotFoundException"));
8991   stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8992
8993   fix_method_argument_names (args, mdecl);
8994   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8995   saved_current_function_decl = current_function_decl;
8996   start_artificial_method_body (mdecl);
8997   java_method_add_stmt (mdecl, stmt);
8998   end_artificial_method_body (mdecl);
8999   current_function_decl = saved_current_function_decl;
9000   TYPE_DOT_CLASS (class) = mdecl;
9001
9002   return mdecl;
9003 }
9004
9005 static tree
9006 build_dot_class_method_invocation (tree this_class, tree type)
9007 {
9008   tree dot_class_method = TYPE_DOT_CLASS (this_class);
9009   tree sig_id, s, t;
9010
9011   if (TYPE_ARRAY_P (type))
9012     sig_id = build_java_signature (type);
9013   else
9014     sig_id = DECL_NAME (TYPE_NAME (type));
9015
9016   /* Ensure that the proper name separator is used */
9017   sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
9018                                IDENTIFIER_LENGTH (sig_id));
9019
9020   s = build_string (IDENTIFIER_LENGTH (sig_id),
9021                     IDENTIFIER_POINTER (sig_id));
9022   t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
9023                                build_tree_list (NULL_TREE, s));
9024   if (DECL_CONTEXT (dot_class_method) != this_class)
9025     {
9026       tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
9027       t = make_qualified_primary (build_wfl_node (class_name), t, 0);
9028     }
9029   return t;
9030 }
9031
9032 /* This section of the code deals with constructor.  */
9033
9034 /* Craft a body for default constructor. Patch existing constructor
9035    bodies with call to super() and field initialization statements if
9036    necessary.  */
9037
9038 static void
9039 fix_constructors (tree mdecl)
9040 {
9041   tree iii;                     /* Instance Initializer Invocation */
9042   tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9043   tree thisn_assign, compound = NULL_TREE;
9044   tree class_type = DECL_CONTEXT (mdecl);
9045
9046   if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9047     return;
9048   DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9049
9050   if (!*bodyp)
9051     {
9052       /* It is an error for the compiler to generate a default
9053          constructor if the superclass doesn't have a constructor that
9054          takes no argument, or the same args for an anonymous class */
9055       tree sdecl = get_constructor_super (mdecl);
9056       if (sdecl == NULL_TREE)
9057         {
9058           tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9059           tree save = DECL_NAME (mdecl);
9060           const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9061           DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9062           parse_error_context
9063             (lookup_cl (TYPE_NAME (class_type)),
9064              "No constructor matching %qs found in class %qs",
9065              lang_printable_name (mdecl, 2), n);
9066           DECL_NAME (mdecl) = save;
9067         }
9068
9069       if (ANONYMOUS_CLASS_P (class_type))
9070         {
9071           /* Copy throws clause from the super constructor.  */
9072           tree throws = DECL_FUNCTION_THROWS (sdecl);
9073           DECL_FUNCTION_THROWS (mdecl) = copy_list (throws);
9074         }
9075
9076       /* The constructor body must be crafted by hand. It's the
9077          constructor we defined when we realize we didn't have the
9078          CLASSNAME() constructor */
9079       start_artificial_method_body (mdecl);
9080
9081       /* Insert an assignment to the this$<n> hidden field, if
9082          necessary */
9083       if ((thisn_assign = build_thisn_assign ()))
9084         java_method_add_stmt (mdecl, thisn_assign);
9085
9086       /* We don't generate a super constructor invocation if we're
9087          compiling java.lang.Object. build_super_invocation takes care
9088          of that. */
9089       java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9090
9091       /* FIXME */
9092       if ((iii = build_instinit_invocation (class_type)))
9093         java_method_add_stmt (mdecl, iii);
9094
9095       end_artificial_method_body (mdecl);
9096     }
9097   /* Search for an explicit constructor invocation */
9098   else
9099     {
9100       int found = 0;
9101       int invokes_this = 0;
9102       tree main_block = BLOCK_EXPR_BODY (*bodyp);
9103
9104       while (*bodyp)
9105         {
9106           tree body = *bodyp;
9107           switch (TREE_CODE (body))
9108             {
9109             case CALL_EXPR:
9110               found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9111               if (CALL_THIS_CONSTRUCTOR_P (body))
9112                 invokes_this = 1;
9113               break;
9114             case COMPOUND_EXPR:
9115             case EXPR_WITH_FILE_LOCATION:
9116               bodyp = &TREE_OPERAND (body, 0);
9117               continue;
9118             case BLOCK:
9119               bodyp = &BLOCK_EXPR_BODY (body);
9120               continue;
9121             default:
9122               break;
9123             }
9124           break;
9125         }
9126
9127       /* Generate the assignment to this$<n>, if necessary */
9128       if ((thisn_assign = build_thisn_assign ()))
9129         compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9130
9131       /* The constructor is missing an invocation of super() */
9132       if (!found)
9133         compound = add_stmt_to_compound (compound, NULL_TREE,
9134                                          build_super_invocation (mdecl));
9135       /* Explicit super() invocation should take place before the
9136          instance initializer blocks. */
9137       else
9138         {
9139           compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9140           *bodyp = build_java_empty_stmt ();
9141         }
9142
9143       DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9144
9145       /* Insert the instance initializer block right after. */
9146       if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9147         compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9148
9149       /* Fix the constructor main block if we're adding extra stmts */
9150       if (compound)
9151         {
9152           compound = add_stmt_to_compound (compound, NULL_TREE,
9153                                            BLOCK_EXPR_BODY (main_block));
9154           BLOCK_EXPR_BODY (main_block) = compound;
9155         }
9156     }
9157 }
9158
9159 /* Browse constructors in the super class, searching for a constructor
9160    that doesn't take any argument. Return the constructor if one is found, 
9161    NULL_TREE otherwise.  If the current class is an anonymous inner class, 
9162    look for something that has the same signature. */
9163 static tree
9164 get_constructor_super (tree mdecl)
9165 {
9166   tree class = CLASSTYPE_SUPER (current_class);
9167   int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9168   tree sdecl;
9169
9170   if (!class)
9171     return NULL_TREE;
9172
9173   if (ANONYMOUS_CLASS_P (current_class))
9174     {
9175       tree mdecl_arg_type;
9176       SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9177       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9178         if (DECL_CONSTRUCTOR_P (sdecl))
9179           {
9180             tree m_arg_type;
9181             tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9182             if (super_inner)
9183               arg_type = TREE_CHAIN (arg_type);
9184             for (m_arg_type = mdecl_arg_type;
9185                  (arg_type != end_params_node
9186                   && m_arg_type != end_params_node);
9187                  arg_type = TREE_CHAIN (arg_type),
9188                    m_arg_type = TREE_CHAIN (m_arg_type))
9189               if (!valid_method_invocation_conversion_p
9190                      (TREE_VALUE (arg_type),
9191                       TREE_VALUE (m_arg_type)))
9192                 break;
9193
9194             if (arg_type == end_params_node && m_arg_type == end_params_node)
9195               return sdecl;
9196           }
9197     }
9198   else
9199     {
9200       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9201         {
9202           tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9203           if (super_inner)
9204             arg = TREE_CHAIN (arg);
9205           if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9206             return sdecl;
9207         }
9208     }
9209   return NULL_TREE;
9210 }
9211
9212 /* Generate code for all context remembered for code generation.  */
9213
9214 static GTY(()) tree reversed_class_list;
9215 void
9216 java_expand_classes (void)
9217 {
9218   int save_error_count = 0;
9219   static struct parser_ctxt *cur_ctxp = NULL;
9220   location_t save_location;
9221
9222   java_parse_abort_on_error ();
9223   if (!(ctxp = ctxp_for_generation))
9224     return;
9225   java_layout_classes ();
9226   java_parse_abort_on_error ();
9227   save_location = input_location;
9228
9229   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9230     {
9231       tree current;
9232       for (current = cur_ctxp->class_list; 
9233            current; 
9234            current = TREE_CHAIN (current))
9235         gen_indirect_dispatch_tables (TREE_TYPE (current));
9236     }
9237   
9238   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9239     {
9240       ctxp = cur_ctxp;
9241       input_location = ctxp->file_start_location;
9242       lang_init_source (2);            /* Error msgs have method prototypes */
9243       java_complete_expand_classes (); /* Complete and expand classes */
9244       java_parse_abort_on_error ();
9245     }
9246   input_location = save_location;
9247
9248   /* Find anonymous classes and expand their constructor. This extra pass is
9249      necessary because the constructor itself is only generated when the
9250      method in which it is defined is expanded. */
9251   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9252     {
9253       tree current;
9254       ctxp = cur_ctxp;
9255       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9256         {
9257           output_class = current_class = TREE_TYPE (current);
9258           if (ANONYMOUS_CLASS_P (current_class))
9259             {
9260               tree d;
9261               for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9262                 {
9263                   if (DECL_CONSTRUCTOR_P (d))
9264                     {
9265                       java_complete_expand_method (d);
9266                       break;    /* There is only one constructor. */
9267                     }
9268                 }
9269             }
9270         }
9271     }
9272
9273   /* Expanding the constructors of anonymous classes generates access
9274      methods.  Scan all the methods looking for null DECL_RESULTs --
9275      this will be the case if a method hasn't been expanded.  */
9276   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9277     {
9278       tree current;
9279       ctxp = cur_ctxp;
9280       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9281         {
9282           tree d;
9283           output_class = current_class = TREE_TYPE (current);
9284           for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9285             {
9286               if (DECL_RESULT (d) == NULL_TREE)
9287                 java_complete_expand_method (d);
9288             }
9289         }
9290     }
9291
9292   /* ???  Instead of all this we could iterate around the list of
9293      classes until there were no more un-expanded methods.  It would
9294      take a little longer -- one pass over the whole list of methods
9295      -- but it would be simpler.  Like this:  */
9296 #if 0
9297     {
9298       int something_changed;
9299     
9300       do
9301         {
9302           something_changed = 0;
9303           for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9304             {
9305               tree current;
9306               ctxp = cur_ctxp;
9307               for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9308                 {
9309                   tree d;
9310                   output_class = current_class = TREE_TYPE (current);
9311                   for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9312                     {
9313                       if (DECL_RESULT (d) == NULL_TREE)
9314                         {
9315                           something_changed = 1;
9316                           java_complete_expand_method (d);
9317                         }
9318                     }
9319                 }
9320             }
9321         }
9322       while (something_changed);
9323     }
9324 #endif
9325
9326   /* If we've found error at that stage, don't try to generate
9327      anything, unless we're checking the syntax only
9328      (but not using -fsyntax-only for the purpose of generating
9329      bytecode).  */
9330   if (java_error_count
9331       && (!flag_syntax_only && !flag_emit_class_files))
9332     return;
9333
9334   /* Now things are stable, go for generation of the class data. */
9335
9336   /* We pessimistically marked all methods and fields external until
9337      we knew what set of classes we were planning to compile.  Now mark
9338      those that will be generated locally as not external.  */
9339   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9340     {
9341       tree current;
9342       ctxp = cur_ctxp;
9343       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9344         java_mark_class_local (TREE_TYPE (current));
9345     }
9346
9347   /* Compile the classes.  */
9348   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9349     {
9350       tree current;
9351       reversed_class_list = NULL;
9352
9353       ctxp = cur_ctxp;
9354
9355       /* We write out the classes in reverse order.  This ensures that
9356          inner classes are written before their containing classes,
9357          which is important for parallel builds.  Otherwise, the
9358          class file for the outer class may be found, but the class
9359          file for the inner class may not be present.  In that
9360          situation, the compiler cannot fall back to the original
9361          source, having already read the outer class, so we must
9362          prevent that situation.  */
9363       for (current = ctxp->class_list;
9364            current;
9365            current = TREE_CHAIN (current))
9366         reversed_class_list
9367           = tree_cons (NULL_TREE, current, reversed_class_list);
9368
9369       for (current = reversed_class_list;
9370            current;
9371            current = TREE_CHAIN (current))
9372         {
9373           output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9374           if (flag_emit_class_files)
9375             write_classfile (current_class);
9376           else if (! flag_syntax_only)
9377             java_expand_method_bodies (current_class);
9378         }
9379     }
9380 }
9381
9382 void
9383 java_finish_classes (void)
9384 {
9385   static struct parser_ctxt *cur_ctxp = NULL;
9386   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9387     {
9388       tree current;
9389       ctxp = cur_ctxp;
9390       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9391         {
9392           output_class = current_class = TREE_TYPE (current);
9393           finish_class ();
9394         }
9395     }
9396 }
9397
9398 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9399    a tree list node containing RIGHT. Fore coming RIGHTs will be
9400    chained to this hook. LOCATION contains the location of the
9401    separating `.' operator.  */
9402
9403 static tree
9404 make_qualified_primary (tree primary, tree right, int location)
9405 {
9406   tree wfl;
9407
9408   if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9409     wfl = build_wfl_wrap (primary, location);
9410   else
9411     {
9412       wfl = primary;
9413       /* If wfl wasn't qualified, we build a first anchor */
9414       if (!EXPR_WFL_QUALIFICATION (wfl))
9415         EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9416     }
9417
9418   /* And chain them */
9419   EXPR_WFL_LINECOL (right) = location;
9420   chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9421   PRIMARY_P (wfl) =  1;
9422   return wfl;
9423 }
9424
9425 /* Simple merge of two name separated by a `.' */
9426
9427 static tree
9428 merge_qualified_name (tree left, tree right)
9429 {
9430   tree node;
9431   if (!left && !right)
9432     return NULL_TREE;
9433
9434   if (!left)
9435     return right;
9436
9437   if (!right)
9438     return left;
9439
9440   obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9441                 IDENTIFIER_LENGTH (left));
9442   obstack_1grow (&temporary_obstack, '.');
9443   obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9444                  IDENTIFIER_LENGTH (right));
9445   node =  get_identifier (obstack_base (&temporary_obstack));
9446   obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9447   QUALIFIED_P (node) = 1;
9448   return node;
9449 }
9450
9451 /* Merge the two parts of a qualified name into LEFT.  Set the
9452    location information of the resulting node to LOCATION, usually
9453    inherited from the location information of the `.' operator. */
9454
9455 static tree
9456 make_qualified_name (tree left, tree right,
9457 #ifdef USE_MAPPED_LOCATION
9458                      source_location location
9459 #else
9460                      int location
9461 #endif
9462                      )
9463 {
9464 #ifdef USE_COMPONENT_REF
9465   tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9466   SET_EXPR_LOCATION (node, location);
9467   return node;
9468 #else
9469   tree left_id = EXPR_WFL_NODE (left);
9470   tree right_id = EXPR_WFL_NODE (right);
9471   tree wfl, merge;
9472
9473   merge = merge_qualified_name (left_id, right_id);
9474
9475   /* Left wasn't qualified and is now qualified */
9476 #ifdef USE_MAPPED_LOCATION
9477   if (!QUALIFIED_P (left_id))
9478     {
9479       tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9480       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9481     }
9482
9483   wfl = build_expr_wfl (right_id, location);
9484 #else
9485   if (!QUALIFIED_P (left_id))
9486     {
9487       tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9488       EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9489       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9490     }
9491
9492   wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9493   EXPR_WFL_LINECOL (wfl) = location;
9494 #endif
9495   chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9496   EXPR_WFL_NODE (left) = merge;
9497   return left;
9498 #endif
9499 }
9500
9501 /* Extract the last identifier component of the qualified in WFL. The
9502    last identifier is removed from the linked list */
9503
9504 static tree
9505 cut_identifier_in_qualified (tree wfl)
9506 {
9507   tree q;
9508   tree previous = NULL_TREE;
9509   for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9510     if (!TREE_CHAIN (q))
9511       {
9512         if (!previous)
9513           /* Operating on a non qualified qualified WFL.  */
9514           abort ();
9515
9516         TREE_CHAIN (previous) = NULL_TREE;
9517         return TREE_PURPOSE (q);
9518       }
9519 }
9520
9521 /* Resolve the expression name NAME. Return its decl.  */
9522
9523 static tree
9524 resolve_expression_name (tree id, tree *orig)
9525 {
9526   tree name = EXPR_WFL_NODE (id);
9527   tree decl;
9528
9529   /* 6.5.5.1: Simple expression names */
9530   if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9531     {
9532       /* 15.13.1: NAME can appear within the scope of a local variable
9533          declaration */
9534       if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9535         return decl;
9536
9537       /* 15.13.1: NAME can appear within a class declaration */
9538       else
9539         {
9540           decl = lookup_field_wrapper (current_class, name);
9541           if (decl)
9542             {
9543               tree access = NULL_TREE;
9544               int fs = FIELD_STATIC (decl);
9545
9546               /* If we're accessing an outer scope local alias, make
9547                  sure we change the name of the field we're going to
9548                  build access to. */
9549               if (FIELD_LOCAL_ALIAS_USED (decl))
9550                 name = DECL_NAME (decl);
9551
9552               check_deprecation (id, decl);
9553
9554               /* Instance variable (8.3.1.1) can't appear within
9555                  static method, static initializer or initializer for
9556                  a static variable. */
9557               if (!fs && METHOD_STATIC (current_function_decl))
9558                 {
9559                   static_ref_err (id, name, current_class);
9560                   return error_mark_node;
9561                 }
9562               /* Instance variables can't appear as an argument of
9563                  an explicit constructor invocation */
9564               if (!fs && ctxp->explicit_constructor_p
9565                   && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9566                 {
9567                   parse_error_context
9568                     (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9569                   return error_mark_node;
9570                 }
9571
9572               /* If we're processing an inner class and we're trying
9573                  to access a field belonging to an outer class, build
9574                  the access to the field.  */
9575               if (nested_field_access_p (current_class, decl))
9576                 {
9577                   if (!fs && CLASS_STATIC (TYPE_NAME (current_class)))
9578                     {
9579                       static_ref_err (id, DECL_NAME (decl), current_class);
9580                       return error_mark_node;
9581                     }
9582                   access = build_nested_field_access (id, decl);
9583                   if (orig)
9584                     *orig = access;
9585                   return access;
9586                 }
9587
9588               /* Otherwise build what it takes to access the field */
9589               access = build_field_ref ((fs ? NULL_TREE : current_this),
9590                                         DECL_CONTEXT (decl), name);
9591               if (fs)
9592                 access = maybe_build_class_init_for_field (decl, access);
9593               /* We may be asked to save the real field access node */
9594               if (orig)
9595                 *orig = access;
9596               /* Last check: can we access the field? */
9597               if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9598                 {
9599                   not_accessible_field_error (id, decl);
9600                   return error_mark_node;
9601                 }
9602               /* And we return what we got */
9603               return access;
9604             }
9605           /* Fall down to error report on undefined variable */
9606         }
9607     }
9608   /* 6.5.5.2 Qualified Expression Names */
9609   else
9610     {
9611       if (orig)
9612         *orig = NULL_TREE;
9613       qualify_ambiguous_name (id);
9614       /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9615       /* 15.10.2: Accessing Superclass Members using super */
9616       return resolve_field_access (id, orig, NULL);
9617     }
9618
9619   /* We've got an error here */
9620   if (INNER_CLASS_TYPE_P (current_class))
9621     parse_error_context (id,
9622                          "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9623                          IDENTIFIER_POINTER (name),
9624                          IDENTIFIER_POINTER (DECL_NAME
9625                                              (TYPE_NAME (current_class))));
9626   else
9627     parse_error_context (id, "Undefined variable %qs",
9628                          IDENTIFIER_POINTER (name));
9629
9630   return error_mark_node;
9631 }
9632
9633 static void
9634 static_ref_err (tree wfl, tree field_id, tree class_type)
9635 {
9636   parse_error_context
9637     (wfl,
9638      "Can't make a static reference to nonstatic variable %qs in class %qs",
9639      IDENTIFIER_POINTER (field_id),
9640      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9641 }
9642
9643 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9644    We return something suitable to generate the field access. We also
9645    return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
9646    recipient's address can be null. */
9647
9648 static tree
9649 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9650 {
9651   int is_static = 0;
9652   tree field_ref;
9653   tree decl = NULL_TREE, where_found, type_found;
9654
9655   if (resolve_qualified_expression_name (qual_wfl, &decl,
9656                                          &where_found, &type_found))
9657     return error_mark_node;
9658
9659   /* Resolve the LENGTH field of an array here */
9660   if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9661       && type_found && TYPE_ARRAY_P (type_found)
9662       && ! flag_emit_class_files)
9663     {
9664       tree length = build_java_array_length_access (where_found);
9665       field_ref = length;
9666
9667       /* In case we're dealing with a static array, we need to
9668          initialize its class before the array length can be fetched.  */
9669       if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9670         {
9671           build_static_field_ref (where_found);
9672           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9673         }
9674     }
9675   /* We might have been trying to resolve field.method(). In which
9676      case, the resolution is over and decl is the answer */
9677   else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9678     field_ref = decl;
9679   else if (JDECL_P (decl))
9680     {
9681       if (!type_found)
9682         type_found = DECL_CONTEXT (decl);
9683       is_static = FIELD_STATIC (decl);
9684       field_ref = build_field_ref ((is_static ?
9685                                     NULL_TREE : where_found),
9686                                    type_found, DECL_NAME (decl));
9687       if (field_ref == error_mark_node)
9688         return error_mark_node;
9689       if (is_static)
9690         field_ref = maybe_build_class_init_for_field (decl, field_ref);
9691
9692       /* If we're looking at a static field, we may need to generate a
9693          class initialization for it.  This can happen when the access
9694          looks like `field.ref', where `field' is a static field in an
9695          interface we implement.  */
9696       if (!flag_emit_class_files
9697           && TREE_CODE (where_found) == VAR_DECL
9698           && FIELD_STATIC (where_found))
9699         {
9700           build_static_field_ref (where_found);
9701           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9702         }
9703     }
9704   else
9705     field_ref = decl;
9706
9707   if (field_decl)
9708     *field_decl = decl;
9709   if (field_type)
9710     *field_type = (QUAL_DECL_TYPE (decl) ?
9711                    QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9712   return field_ref;
9713 }
9714
9715 /* If NODE is an access to a static field, strip out the class
9716    initialization part and return the field decl, otherwise, return
9717    NODE. */
9718
9719 tree
9720 extract_field_decl (tree node)
9721 {
9722   if (TREE_CODE (node) == COMPOUND_EXPR)
9723     {
9724       tree op1 = TREE_OPERAND (node, 1);
9725       if (TREE_CODE (op1) == COMPOUND_EXPR)
9726          {
9727            tree call = TREE_OPERAND (op1, 0);
9728            if (TREE_CODE (call) == CALL_EXPR
9729                && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9730                && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9731                    == soft_initclass_node))
9732              return TREE_OPERAND (op1, 1);
9733          }
9734       else if (JDECL_P (op1))
9735         return op1;
9736     }
9737   return node;
9738 }
9739
9740 /* 6.5.5.2: Qualified Expression Names */
9741
9742 static int
9743 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9744                                    tree *where_found, tree *type_found)
9745 {
9746   int from_type = 0;            /* Field search initiated from a type */
9747   int from_super = 0, from_cast = 0, from_qualified_this = 0;
9748   int previous_call_static = 0;
9749   int is_static;
9750   tree decl = NULL_TREE, type = NULL_TREE, q;
9751   /* For certain for of inner class instantiation */
9752   tree saved_current, saved_this;
9753 #define RESTORE_THIS_AND_CURRENT_CLASS                          \
9754   { current_class = saved_current; current_this = saved_this;}
9755
9756   *type_found = *where_found = NULL_TREE;
9757
9758   for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9759     {
9760       tree qual_wfl = QUAL_WFL (q);
9761       tree ret_decl;            /* for EH checking */
9762 #ifdef USE_MAPPED_LOCATION
9763       source_location location;  /* for EH checking */
9764 #else
9765       int location;             /* for EH checking */
9766 #endif
9767
9768       /* 15.10.1 Field Access Using a Primary */
9769       switch (TREE_CODE (qual_wfl))
9770         {
9771         case CALL_EXPR:
9772         case NEW_CLASS_EXPR:
9773           /* If the access to the function call is a non static field,
9774              build the code to access it. */
9775           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9776             {
9777               decl = maybe_access_field (decl, *where_found,
9778                                          DECL_CONTEXT (decl));
9779               if (decl == error_mark_node)
9780                 return 1;
9781             }
9782
9783           /* And code for the function call */
9784           if (complete_function_arguments (qual_wfl))
9785             return 1;
9786
9787           /* We might have to setup a new current class and a new this
9788              for the search of an inner class, relative to the type of
9789              a expression resolved as `decl'. The current values are
9790              saved and restored shortly after */
9791           saved_current = current_class;
9792           saved_this = current_this;
9793           if (decl
9794               && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9795                   || from_qualified_this))
9796             {
9797               /* If we still have `from_qualified_this', we have the form
9798                  <T>.this.f() and we need to build <T>.this */
9799               if (from_qualified_this)
9800                 {
9801                   decl = build_access_to_thisn (current_class, type, 0);
9802                   decl = java_complete_tree (decl);
9803                   type = TREE_TYPE (TREE_TYPE (decl));
9804                 }
9805               current_class = type;
9806               current_this = decl;
9807               from_qualified_this = 0;
9808             }
9809
9810           if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9811             CALL_USING_SUPER (qual_wfl) = 1;
9812 #ifdef USE_MAPPED_LOCATION
9813           location = (TREE_CODE (qual_wfl) == CALL_EXPR
9814                       ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9815                       : UNKNOWN_LOCATION);
9816 #else
9817           location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9818                       EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9819 #endif
9820           *where_found = patch_method_invocation (qual_wfl, decl, type,
9821                                                   from_super,
9822                                                   &is_static, &ret_decl);
9823           from_super = 0;
9824           if (*where_found == error_mark_node)
9825             {
9826               RESTORE_THIS_AND_CURRENT_CLASS;
9827               return 1;
9828             }
9829           *type_found = type = QUAL_DECL_TYPE (*where_found);
9830
9831           *where_found = force_evaluation_order (*where_found);
9832
9833           /* If we're creating an inner class instance, check for that
9834              an enclosing instance is in scope */
9835           if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9836               && INNER_ENCLOSING_SCOPE_CHECK (type))
9837             {
9838               parse_error_context
9839                 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9840                  lang_printable_name (type, 0),
9841                  (!current_this ? "" :
9842                   "; an explicit one must be provided when creating this inner class"));
9843               RESTORE_THIS_AND_CURRENT_CLASS;
9844               return 1;
9845             }
9846
9847           /* In case we had to change then to resolve a inner class
9848              instantiation using a primary qualified by a `new' */
9849           RESTORE_THIS_AND_CURRENT_CLASS;
9850
9851 #ifdef USE_MAPPED_LOCATION
9852           if (location != UNKNOWN_LOCATION)
9853 #else
9854           if (location)
9855 #endif
9856             {
9857               tree arguments = NULL_TREE;
9858               if (TREE_CODE (qual_wfl) == CALL_EXPR
9859                   && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9860                 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9861               check_thrown_exceptions (location, ret_decl, arguments);
9862             }
9863
9864           /* If the previous call was static and this one is too,
9865              build a compound expression to hold the two (because in
9866              that case, previous function calls aren't transported as
9867              forcoming function's argument. */
9868           if (previous_call_static && is_static)
9869             {
9870               /* We must set CAN_COMPLETE_NORMALLY for the first call
9871                  since it is done nowhere else.  */
9872               CAN_COMPLETE_NORMALLY (decl) = 1;
9873               decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9874                              decl, *where_found);
9875               TREE_SIDE_EFFECTS (decl) = 1;
9876             }
9877           else
9878             {
9879               previous_call_static = is_static;
9880               decl = *where_found;
9881             }
9882           from_type = 0;
9883           continue;
9884
9885         case NEW_ARRAY_EXPR:
9886         case NEW_ANONYMOUS_ARRAY_EXPR:
9887           *where_found = decl = java_complete_tree (qual_wfl);
9888           if (decl == error_mark_node)
9889             return 1;
9890           *type_found = type = QUAL_DECL_TYPE (decl);
9891           continue;
9892
9893         case CONVERT_EXPR:
9894           *where_found = decl = java_complete_tree (qual_wfl);
9895           if (decl == error_mark_node)
9896             return 1;
9897           *type_found = type = QUAL_DECL_TYPE (decl);
9898           from_cast = 1;
9899           continue;
9900
9901         case CONDITIONAL_EXPR:
9902         case STRING_CST:
9903         case MODIFY_EXPR:
9904           *where_found = decl = java_complete_tree (qual_wfl);
9905           if (decl == error_mark_node)
9906             return 1;
9907           *type_found = type = QUAL_DECL_TYPE (decl);
9908           continue;
9909
9910         case ARRAY_REF:
9911           /* If the access to the function call is a non static field,
9912              build the code to access it. */
9913           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9914             {
9915               decl = maybe_access_field (decl, *where_found, type);
9916               if (decl == error_mark_node)
9917                 return 1;
9918             }
9919           /* And code for the array reference expression */
9920           decl = java_complete_tree (qual_wfl);
9921           if (decl == error_mark_node)
9922             return 1;
9923           type = QUAL_DECL_TYPE (decl);
9924           continue;
9925
9926         case PLUS_EXPR:
9927           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9928             return 1;
9929           if ((type = patch_string (decl)))
9930             decl = type;
9931           *where_found = QUAL_RESOLUTION (q) = decl;
9932           *type_found = type = TREE_TYPE (decl);
9933           break;
9934
9935         case CLASS_LITERAL:
9936           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9937             return 1;
9938           *where_found = QUAL_RESOLUTION (q) = decl;
9939           *type_found = type = TREE_TYPE (decl);
9940           break;
9941
9942         default:
9943           /* Fix for -Wall Just go to the next statement. Don't
9944              continue */
9945           break;
9946         }
9947
9948       /* If we fall here, we weren't processing a (static) function call. */
9949       previous_call_static = 0;
9950
9951       /* It can be the keyword THIS */
9952       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9953           && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9954         {
9955           if (!current_this)
9956             {
9957               parse_error_context
9958                 (wfl, "Keyword %<this%> used outside allowed context");
9959               return 1;
9960             }
9961           if (ctxp->explicit_constructor_p
9962               && type == current_class)
9963             {
9964               parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9965               return 1;
9966             }
9967           /* We have to generate code for intermediate access */
9968           if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9969             {
9970               *where_found = decl = current_this;
9971               *type_found = type = QUAL_DECL_TYPE (decl);
9972             }
9973           /* We're trying to access the this from somewhere else. Make sure
9974              it's allowed before doing so. */
9975           else
9976             {
9977               if (!enclosing_context_p (type, current_class))
9978                 {
9979                   char *p  = xstrdup (lang_printable_name (type, 0));
9980                   parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9981                                        p, p,
9982                                        lang_printable_name (current_class, 0));
9983                   free (p);
9984                   return 1;
9985                 }
9986               from_qualified_this = 1;
9987               /* If there's nothing else after that, we need to
9988                  produce something now, otherwise, the section of the
9989                  code that needs to produce <T>.this will generate
9990                  what is necessary. */
9991               if (!TREE_CHAIN (q))
9992                 {
9993                   decl = build_access_to_thisn (current_class, type, 0);
9994                   *where_found = decl = java_complete_tree (decl);
9995                   *type_found = type = TREE_TYPE (decl);
9996                 }
9997             }
9998
9999           from_type = 0;
10000           continue;
10001         }
10002
10003       /* 15.10.2 Accessing Superclass Members using SUPER */
10004       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
10005           && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
10006         {
10007           tree node;
10008           /* Check on the restricted use of SUPER */
10009           if (METHOD_STATIC (current_function_decl)
10010               || current_class == object_type_node)
10011             {
10012               parse_error_context
10013                 (wfl, "Keyword %<super%> used outside allowed context");
10014               return 1;
10015             }
10016           /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
10017           node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
10018                              CLASSTYPE_SUPER (current_class),
10019                              build_this (EXPR_WFL_LINECOL (qual_wfl)));
10020           *where_found = decl = java_complete_tree (node);
10021           if (decl == error_mark_node)
10022             return 1;
10023           *type_found = type = QUAL_DECL_TYPE (decl);
10024           from_super = from_type = 1;
10025           continue;
10026         }
10027
10028       /* 15.13.1: Can't search for field name in packages, so we
10029          assume a variable/class name was meant. */
10030       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10031         {
10032           tree name;
10033           if ((decl = resolve_package (wfl, &q, &name)))
10034             {
10035               tree list;
10036               *where_found = decl;
10037
10038               check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10039
10040               /* We want to be absolutely sure that the class is laid
10041                  out. We're going to search something inside it. */
10042               *type_found = type = TREE_TYPE (decl);
10043               layout_class (type);
10044               from_type = 1;
10045
10046               /* Fix them all the way down, if any are left. */
10047               if (q)
10048                 {
10049                   list = TREE_CHAIN (q);
10050                   while (list)
10051                     {
10052                       RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10053                       list = TREE_CHAIN (list);
10054                     }
10055                 }
10056             }
10057           else
10058             {
10059               if (from_super || from_cast)
10060                 parse_error_context
10061                   ((from_cast ? qual_wfl : wfl),
10062                    "No variable %qs defined in class %qs",
10063                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10064                    lang_printable_name (type, 0));
10065               else
10066                 parse_error_context
10067                   (qual_wfl, "Undefined variable or class name: %qs",
10068                    IDENTIFIER_POINTER (name));
10069               return 1;
10070             }
10071         }
10072
10073       /* We have a type name. It's been already resolved when the
10074          expression was qualified. */
10075       else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10076         {
10077           decl = QUAL_RESOLUTION (q);
10078
10079           /* Sneak preview. If next we see a `new', we're facing a
10080              qualification which resulted in a type being selected
10081              instead of a field.  Report the error.  */
10082           if(TREE_CHAIN (q)
10083              && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10084             {
10085               parse_error_context (qual_wfl, "Undefined variable %qs",
10086                                    IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10087               return 1;
10088             }
10089
10090           check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10091           
10092           check_deprecation (qual_wfl, decl);
10093
10094           type = TREE_TYPE (decl);
10095           from_type = 1;
10096         }
10097       /* We resolve an expression name */
10098       else
10099         {
10100           tree field_decl = NULL_TREE;
10101
10102           /* If there exists an early resolution, use it. That occurs
10103              only once and we know that there are more things to
10104              come. Don't do that when processing something after SUPER
10105              (we need more thing to be put in place below */
10106           if (!from_super && QUAL_RESOLUTION (q))
10107             {
10108               decl = QUAL_RESOLUTION (q);
10109               if (!type)
10110                 {
10111                   if (TREE_CODE (decl) == FIELD_DECL
10112                       || TREE_CODE (decl) == VAR_DECL)
10113                     {
10114                       if (TREE_CODE (decl) == FIELD_DECL
10115                           && !FIELD_STATIC (decl))
10116                         {
10117                           if (current_this)
10118                             *where_found = current_this;
10119                           else
10120                             {
10121                               static_ref_err (qual_wfl, DECL_NAME (decl),
10122                                               current_class);
10123                               return 1;
10124                             }
10125                         }
10126                       else
10127                         {
10128                           *where_found = TREE_TYPE (decl);
10129                           if (TREE_CODE (*where_found) == POINTER_TYPE)
10130                             *where_found = TREE_TYPE (*where_found);
10131                         }
10132                       if (nested_field_access_p (current_class, decl))
10133                         decl = build_nested_field_access (qual_wfl, decl);
10134                     }
10135                   else
10136                     {
10137                       *where_found = TREE_TYPE (decl);
10138                       if (TREE_CODE (*where_found) == POINTER_TYPE)
10139                         *where_found = TREE_TYPE (*where_found);
10140                     }
10141                 }
10142             }
10143
10144           /* Report and error if we're using a numerical literal as a
10145              qualifier. It can only be an INTEGER_CST. */
10146           else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10147             {
10148               parse_error_context
10149                 (wfl, "Can't use type %qs as a qualifier",
10150                  lang_printable_name (TREE_TYPE (qual_wfl), 0));
10151               return 1;
10152             }
10153
10154           /* We have to search for a field, knowing the type of its
10155              container. The flag FROM_TYPE indicates that we resolved
10156              the last member of the expression as a type name, which
10157              means that for the resolution of this field, we'll look
10158              for other errors than if it was resolved as a member of
10159              an other field. */
10160           else
10161             {
10162               int is_static;
10163               tree field_decl_type; /* For layout */
10164
10165               if (!from_type && !JREFERENCE_TYPE_P (type))
10166                 {
10167                   parse_error_context
10168                     (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10169                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10170                      lang_printable_name (type, 0),
10171                      IDENTIFIER_POINTER (DECL_NAME (decl)));
10172                   return 1;
10173                 }
10174
10175               field_decl = lookup_field_wrapper (type,
10176                                                  EXPR_WFL_NODE (qual_wfl));
10177
10178               /* Maybe what we're trying to access to is an inner
10179                  class, only if decl is a TYPE_DECL. */
10180               if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10181                 {
10182                   tree ptr, inner_decl;
10183
10184                   BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10185                   inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10186                   if (inner_decl)
10187                     {
10188                       check_inner_class_access (inner_decl, decl, qual_wfl);
10189                       type = TREE_TYPE (inner_decl);
10190                       decl = inner_decl;
10191                       from_type = 1;
10192                       continue;
10193                     }
10194                 }
10195
10196               if (field_decl == NULL_TREE)
10197                 {
10198                   parse_error_context
10199                     (qual_wfl, "No variable %qs defined in type %qs",
10200                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10201                      GET_TYPE_NAME (type));
10202                   return 1;
10203                 }
10204               if (field_decl == error_mark_node)
10205                 return 1;
10206
10207               /* Layout the type of field_decl, since we may need
10208                  it. Don't do primitive types or loaded classes. The
10209                  situation of non primitive arrays may not handled
10210                  properly here. FIXME */
10211               if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10212                 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10213               else
10214                 field_decl_type = TREE_TYPE (field_decl);
10215               if (!JPRIMITIVE_TYPE_P (field_decl_type)
10216                   && !CLASS_LOADED_P (field_decl_type)
10217                   && !TYPE_ARRAY_P (field_decl_type))
10218                 resolve_and_layout (field_decl_type, NULL_TREE);
10219
10220               /* Check on accessibility here */
10221               if (not_accessible_p (current_class, field_decl,
10222                                     *type_found, from_super))
10223                 return not_accessible_field_error (qual_wfl,field_decl);    
10224               check_deprecation (qual_wfl, field_decl);
10225
10226               /* There are things to check when fields are accessed
10227                  from type. There are no restrictions on a static
10228                  declaration of the field when it is accessed from an
10229                  interface */
10230               is_static = FIELD_STATIC (field_decl);
10231               if (!from_super && from_type
10232                   && !TYPE_INTERFACE_P (type)
10233                   && !is_static
10234                   && (current_function_decl
10235                       && METHOD_STATIC (current_function_decl)))
10236                 {
10237                   static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10238                   return 1;
10239                 }
10240               from_cast = from_super = 0;
10241
10242               /* If it's an access from a type but isn't static, we
10243                  make it relative to `this'. */
10244               if (!is_static && from_type)
10245                 decl = current_this;
10246
10247               /* If we need to generate something to get a proper
10248                  handle on what this field is accessed from, do it
10249                  now. */
10250               if (!is_static)
10251                 {
10252                   decl = maybe_access_field (decl, *where_found, *type_found);
10253                   if (decl == error_mark_node)
10254                     return 1;
10255                 }
10256
10257               /* We want to keep the location where we found it, and the
10258                  type we found.  */
10259               *where_found = decl;
10260               *type_found = type;
10261
10262               /* Generate the correct expression for field access from
10263                  qualified this */
10264               if (from_qualified_this)
10265                 {
10266                   field_decl
10267                     = build_nested_field_access (qual_wfl, field_decl);
10268                   from_qualified_this = 0;
10269                 }
10270
10271               /* If needed, generate accessors for static field access.  */
10272               if (is_static
10273                   && FIELD_PRIVATE (field_decl)
10274                   && flag_emit_class_files
10275                   && nested_field_access_p (current_class, field_decl))
10276                 field_decl = build_nested_field_access (qual_wfl, field_decl);
10277
10278               /* This is the decl found and eventually the next one to
10279                  search from */
10280               decl = field_decl;
10281             }
10282           from_type = 0;
10283           type = QUAL_DECL_TYPE (decl);
10284
10285           /* Sneak preview. If decl is qualified by a `new', report
10286              the error here to be accurate on the peculiar construct */
10287           if (TREE_CHAIN (q)
10288               && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10289               && !JREFERENCE_TYPE_P (type))
10290             {
10291               parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10292                                    lang_printable_name (type, 0));
10293               return 1;
10294             }
10295         }
10296       /* `q' might have changed due to a after package resolution
10297          re-qualification */
10298       if (!q)
10299         break;
10300     }
10301   *found_decl = decl;
10302   return 0;
10303 }
10304
10305 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10306    can't be accessed from REFERENCE (a record type). If MEMBER
10307    features a protected access, we then use WHERE which, if non null,
10308    holds the type of MEMBER's access that is checked against
10309    6.6.2.1. This function should be used when decl is a field or a
10310    method.  */
10311
10312 static int
10313 not_accessible_p (tree reference, tree member, tree where, int from_super)
10314 {
10315   int access_flag = get_access_flags_from_decl (member);
10316   bool is_static = false;
10317  
10318   if (TREE_CODE (member) == FIELD_DECL ||
10319       TREE_CODE (member) == VAR_DECL)
10320     is_static = FIELD_STATIC (member);
10321   else
10322     is_static = METHOD_STATIC (member);
10323
10324   /* Access always granted for members declared public */
10325   if (access_flag & ACC_PUBLIC)
10326     return 0;
10327
10328   /* Check access on protected members */
10329   if (access_flag & ACC_PROTECTED)
10330     {
10331       /* Access granted if it occurs from within the package
10332          containing the class in which the protected member is
10333          declared */
10334       if (class_in_current_package (DECL_CONTEXT (member)))
10335         return 0;
10336
10337       /* If accessed with the form `super.member', then access is granted */
10338       if (from_super)
10339         return 0;
10340
10341       /* If WHERE is active, access was made through a qualifier. For 
10342          non-static members, access is granted if the type of the qualifier 
10343          is or is a sublass of the type the access is made from (6.6.2.1.)  */
10344       if (where && !is_static)
10345         {
10346           while (reference)
10347             {
10348               if (inherits_from_p (where, reference))
10349                 return 0;
10350               if (INNER_CLASS_TYPE_P (reference))
10351                 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10352               else
10353                 break;
10354             }
10355           return 1;
10356         }
10357
10358       /* Otherwise, access is granted if occurring from within the class
10359          where member is declared, or a subclass of it.  */
10360       while (reference)
10361         {
10362           if (inherits_from_p (reference, DECL_CONTEXT (member)))
10363             return 0;
10364           if (INNER_CLASS_TYPE_P (reference))
10365             reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10366           else
10367             break;
10368         }
10369       return 1;
10370     }
10371
10372   /* Check access on private members. Access is granted only if it
10373      occurs from within the class in which it is declared -- that does
10374      it for innerclasses too. */
10375   if (access_flag & ACC_PRIVATE)
10376     {
10377       if (reference == DECL_CONTEXT (member) ||
10378           common_enclosing_context_p (DECL_CONTEXT (member), reference))
10379         return 0;
10380       return 1;
10381     }
10382
10383   /* Default access is permitted only when occurring from within the
10384      package in which the context (MEMBER) is declared.  */
10385   return !class_in_current_package (DECL_CONTEXT (member));
10386 }
10387
10388 /* Test deprecated decl access.  */
10389 static void
10390 check_deprecation (tree wfl, tree decl)
10391 {
10392   const char *file;
10393   tree elt;
10394
10395   if (! warn_deprecated)
10396     return;
10397
10398   /* We want to look at the element type of arrays here, so we strip
10399      all surrounding array types.  */
10400   if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10401     {
10402       elt = TREE_TYPE (decl);
10403       while (TYPE_ARRAY_P (elt))
10404         elt = TYPE_ARRAY_ELEMENT (elt);
10405       /* We'll end up with a pointer type, so we use TREE_TYPE to go
10406          to the record.  */
10407       decl = TYPE_NAME (TREE_TYPE (elt));
10408     }
10409   file = DECL_SOURCE_FILE (decl);
10410
10411   /* Complain if the field is deprecated and the file it was defined
10412      in isn't compiled at the same time the file which contains its
10413      use is */
10414   if (DECL_DEPRECATED (decl)
10415       && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10416     {
10417       const char *the;
10418       switch (TREE_CODE (decl))
10419         {
10420         case FUNCTION_DECL:
10421           the = "method";
10422           break;
10423         case FIELD_DECL:
10424         case VAR_DECL:
10425           the = "field";
10426           break;
10427         case TYPE_DECL:
10428           parse_warning_context (wfl, "The class %qs has been deprecated",
10429                                  IDENTIFIER_POINTER (DECL_NAME (decl)));
10430           return;
10431         default:
10432           abort ();
10433         }
10434       /* Don't issue a message if the context as been deprecated as a
10435          whole. */
10436       if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10437         parse_warning_context
10438           (wfl, "The %s %qs in class %qs has been deprecated",
10439            the, lang_printable_name (decl, 0),
10440            IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10441     }
10442 }
10443
10444 /* Returns 1 if class was declared in the current package, 0 otherwise */
10445
10446 static GTY(()) tree cicp_cache;
10447 static int
10448 class_in_current_package (tree class)
10449 {
10450   int qualified_flag;
10451   tree left;
10452
10453   if (cicp_cache == class)
10454     return 1;
10455
10456   qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10457
10458   /* If the current package is empty and the name of CLASS is
10459      qualified, class isn't in the current package.  If there is a
10460      current package and the name of the CLASS is not qualified, class
10461      isn't in the current package */
10462   if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10463     return 0;
10464
10465   /* If there is not package and the name of CLASS isn't qualified,
10466      they belong to the same unnamed package */
10467   if (!ctxp->package && !qualified_flag)
10468     return 1;
10469
10470   /* Compare the left part of the name of CLASS with the package name */
10471   split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10472   if (ctxp->package == left)
10473     {
10474       cicp_cache = class;
10475       return 1;
10476     }
10477   return 0;
10478 }
10479
10480 /* This function may generate code to access DECL from WHERE. This is
10481    done only if certain conditions meet.  */
10482
10483 static tree
10484 maybe_access_field (tree decl, tree where, tree type)
10485 {
10486   if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10487       && !FIELD_STATIC (decl))
10488     decl = build_field_ref (where ? where : current_this,
10489                             (type ? type : DECL_CONTEXT (decl)),
10490                             DECL_NAME (decl));
10491   return decl;
10492 }
10493
10494 /* Build a method invocation, by patching PATCH. If non NULL
10495    and according to the situation, PRIMARY and WHERE may be
10496    used. IS_STATIC is set to 1 if the invoked function is static. */
10497
10498 static tree
10499 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10500                          int *is_static, tree *ret_decl)
10501 {
10502   tree wfl = TREE_OPERAND (patch, 0);
10503   tree args = TREE_OPERAND (patch, 1);
10504   tree name = EXPR_WFL_NODE (wfl);
10505   tree list;
10506   int is_static_flag = 0;
10507   int is_super_init = 0;
10508   tree this_arg = NULL_TREE;
10509   int is_array_clone_call = 0;
10510
10511   /* Should be overridden if everything goes well. Otherwise, if
10512      something fails, it should keep this value. It stop the
10513      evaluation of a bogus assignment. See java_complete_tree,
10514      MODIFY_EXPR: for the reasons why we sometimes want to keep on
10515      evaluating an assignment */
10516   TREE_TYPE (patch) = error_mark_node;
10517
10518   /* Since lookup functions are messing with line numbers, save the
10519      context now.  */
10520   java_parser_context_save_global ();
10521
10522   /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10523
10524   /* Resolution of qualified name, excluding constructors */
10525   if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10526     {
10527       tree identifier, identifier_wfl, type, resolved;
10528       /* Extract the last IDENTIFIER of the qualified
10529          expression. This is a wfl and we will use it's location
10530          data during error report. */
10531       identifier_wfl = cut_identifier_in_qualified (wfl);
10532       identifier = EXPR_WFL_NODE (identifier_wfl);
10533
10534       /* Given the context, IDENTIFIER is syntactically qualified
10535          as a MethodName. We need to qualify what's before */
10536       qualify_ambiguous_name (wfl);
10537       resolved = resolve_field_access (wfl, NULL, NULL);
10538
10539       if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10540          && FIELD_FINAL (resolved)
10541          && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10542          && !flag_emit_class_files)
10543        resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10544
10545       if (resolved == error_mark_node)
10546         PATCH_METHOD_RETURN_ERROR ();
10547
10548       type = GET_SKIP_TYPE (resolved);
10549       resolve_and_layout (type, NULL_TREE);
10550
10551       if (JPRIMITIVE_TYPE_P (type))
10552         {
10553           parse_error_context
10554             (identifier_wfl,
10555              "Can't invoke a method on primitive type %qs",
10556              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10557           PATCH_METHOD_RETURN_ERROR ();
10558         }
10559
10560       list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10561       args = nreverse (args);
10562
10563       /* We're resolving a call from a type */
10564       if (TREE_CODE (resolved) == TYPE_DECL)
10565         {
10566           if (CLASS_INTERFACE (resolved))
10567             {
10568               parse_error_context
10569                 (identifier_wfl,
10570                 "Can't make static reference to method %qs in interface %qs",
10571                  IDENTIFIER_POINTER (identifier),
10572                  IDENTIFIER_POINTER (name));
10573               PATCH_METHOD_RETURN_ERROR ();
10574             }
10575           if (list && !METHOD_STATIC (list))
10576             {
10577               char *fct_name = xstrdup (lang_printable_name (list, 2));
10578               parse_error_context
10579                 (identifier_wfl,
10580                  "Can't make static reference to method %<%s %s%> in class %qs",
10581                  lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10582                  fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10583               free (fct_name);
10584               PATCH_METHOD_RETURN_ERROR ();
10585             }
10586         }
10587       else
10588         this_arg = primary = resolved;
10589
10590       if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10591         is_array_clone_call = 1;
10592
10593       /* IDENTIFIER_WFL will be used to report any problem further */
10594       wfl = identifier_wfl;
10595     }
10596   /* Resolution of simple names, names generated after a primary: or
10597      constructors */
10598   else
10599     {
10600       tree class_to_search = NULL_TREE;
10601       int lc;                   /* Looking for Constructor */
10602
10603       /* We search constructor in their target class */
10604       if (CALL_CONSTRUCTOR_P (patch))
10605         {
10606           if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10607             class_to_search = EXPR_WFL_NODE (wfl);
10608           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10609                    this_identifier_node)
10610             class_to_search = NULL_TREE;
10611           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10612                    super_identifier_node)
10613             {
10614               is_super_init = 1;
10615               if (CLASSTYPE_SUPER (current_class))
10616                 class_to_search =
10617                   DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10618               else
10619                 {
10620                   parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10621                   PATCH_METHOD_RETURN_ERROR ();
10622                 }
10623             }
10624
10625           /* Class to search is NULL if we're searching the current one */
10626           if (class_to_search)
10627             {
10628               class_to_search = resolve_and_layout (class_to_search, wfl);
10629
10630               if (!class_to_search)
10631                 {
10632                   parse_error_context
10633                     (wfl, "Class %qs not found in type declaration",
10634                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10635                   PATCH_METHOD_RETURN_ERROR ();
10636                 }
10637
10638               /* Can't instantiate an abstract class, but we can
10639                  invoke it's constructor. It's use within the `new'
10640                  context is denied here. */
10641               if (CLASS_ABSTRACT (class_to_search)
10642                   && TREE_CODE (patch) == NEW_CLASS_EXPR)
10643                 {
10644                   parse_error_context
10645                     (wfl, "Class %qs is an abstract class. It can't be instantiated",
10646                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10647                   PATCH_METHOD_RETURN_ERROR ();
10648                 }
10649
10650               class_to_search = TREE_TYPE (class_to_search);
10651             }
10652           else
10653             class_to_search = current_class;
10654           lc = 1;
10655         }
10656       /* This is a regular search in the local class, unless an
10657          alternate class is specified. */
10658       else
10659         {
10660           if (where != NULL_TREE)
10661             class_to_search = where;
10662           else if (QUALIFIED_P (name))
10663             class_to_search = current_class;
10664           else
10665             {
10666               class_to_search = current_class;
10667
10668               for (;;)
10669                 {
10670                   if (has_method (class_to_search, name))
10671                     break;
10672                   if (! INNER_CLASS_TYPE_P (class_to_search))
10673                     {
10674                       parse_error_context (wfl,
10675                                            "No method named %qs in scope",
10676                                            IDENTIFIER_POINTER (name));
10677                       PATCH_METHOD_RETURN_ERROR ();
10678                     }
10679                   class_to_search
10680                     = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10681                 }
10682             }
10683           lc = 0;
10684         }
10685
10686       /* NAME is a simple identifier or comes from a primary. Search
10687          in the class whose declaration contain the method being
10688          invoked. */
10689       resolve_and_layout (class_to_search, NULL_TREE);
10690
10691       list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10692       /* Don't continue if no method were found, as the next statement
10693          can't be executed then. */
10694       if (!list)
10695         PATCH_METHOD_RETURN_ERROR ();
10696
10697       if (TYPE_ARRAY_P (class_to_search)
10698           && DECL_NAME (list) == get_identifier ("clone"))
10699         is_array_clone_call = 1;
10700
10701       /* Check for static reference if non static methods */
10702       if (check_for_static_method_reference (wfl, patch, list,
10703                                              class_to_search, primary))
10704         PATCH_METHOD_RETURN_ERROR ();
10705
10706       /* Check for inner classes creation from illegal contexts */
10707       if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10708                  && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10709           && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10710           && !DECL_INIT_P (current_function_decl))
10711         {
10712           parse_error_context
10713             (wfl, "No enclosing instance for inner class %qs is in scope%s",
10714              lang_printable_name (class_to_search, 0),
10715              (!current_this ? "" :
10716               "; an explicit one must be provided when creating this inner class"));
10717           PATCH_METHOD_RETURN_ERROR ();
10718         }
10719
10720       /* Non static methods are called with the current object extra
10721          argument. If patch a `new TYPE()', the argument is the value
10722          returned by the object allocator. If method is resolved as a
10723          primary, use the primary otherwise use the current THIS. */
10724       args = nreverse (args);
10725       if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10726         {
10727           this_arg = primary ? primary : current_this;
10728
10729           /* If we're using an access method, things are different.
10730              There are two family of cases:
10731
10732              1) We're not generating bytecodes:
10733
10734              - LIST is non static. It's invocation is transformed from
10735                x(a1,...,an) into this$<n>.x(a1,....an).
10736              - LIST is static. It's invocation is transformed from
10737                x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10738
10739              2) We're generating bytecodes:
10740
10741              - LIST is non static. It's invocation is transformed from
10742                x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10743              - LIST is static. It's invocation is transformed from
10744                x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10745
10746              Of course, this$<n> can be arbitrarily complex, ranging from
10747              this$0 (the immediate outer context) to
10748              access$0(access$0(...(this$0))).
10749
10750              maybe_use_access_method returns a nonzero value if the
10751              this_arg has to be moved into the (then generated) stub
10752              argument list. In the meantime, the selected function
10753              might have be replaced by a generated stub. */
10754           if (!primary &&
10755               maybe_use_access_method (is_super_init, &list, &this_arg))
10756             {
10757               args = tree_cons (NULL_TREE, this_arg, args);
10758               this_arg = NULL_TREE; /* So it doesn't get chained twice */
10759             }
10760         }
10761     }
10762
10763   /* Merge point of all resolution schemes. If we have nothing, this
10764      is an error, already signaled */
10765   if (!list)
10766     PATCH_METHOD_RETURN_ERROR ();
10767
10768   /* Check accessibility, position the is_static flag, build and
10769      return the call */
10770   if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10771                         (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10772                          NULL_TREE), from_super)
10773       /* Calls to clone() on array types are permitted as a special-case. */
10774       && !is_array_clone_call)
10775     {
10776       const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10777       const char *const access =
10778         accessibility_string (get_access_flags_from_decl (list));
10779       const char *const klass =
10780         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10781       const char *const refklass =
10782         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10783       const char *const what = (DECL_CONSTRUCTOR_P (list)
10784                                 ? "constructor" : "method");
10785       parse_error_context (wfl,
10786                            "Can't access %s %s %<%s.%s%> from %qs",
10787                            access, what, klass, fct_name, refklass);
10788       PATCH_METHOD_RETURN_ERROR ();
10789     }
10790
10791   /* Deprecation check: check whether the method being invoked or the
10792      instance-being-created's type are deprecated.  */
10793   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10794     check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10795   check_deprecation (wfl, list);
10796
10797   /* If invoking a innerclass constructor, there are hidden parameters
10798      to pass */
10799   if (TREE_CODE (patch) == NEW_CLASS_EXPR
10800       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10801     {
10802       /* And make sure we add the accessed local variables to be saved
10803          in field aliases. */
10804       args = build_alias_initializer_parameter_list
10805         (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10806
10807       /* Secretly pass the current_this/primary as a second argument */
10808       if (primary || current_this)
10809         {
10810           tree extra_arg;
10811           tree this_type = (current_this ?
10812                             TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10813           /* Method's (list) enclosing context */
10814           tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10815           /* If we have a primary, use it. */
10816           if (primary)
10817             extra_arg = primary;
10818           /* The current `this' is an inner class but isn't a direct
10819              enclosing context for the inner class we're trying to
10820              create. Build an access to the proper enclosing context
10821              and use it. */
10822           else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10823                    && this_type != TREE_TYPE (mec))
10824             {
10825
10826               extra_arg = build_access_to_thisn (current_class,
10827                                                  TREE_TYPE (mec), 0);
10828               extra_arg = java_complete_tree (extra_arg);
10829             }
10830           /* Otherwise, just use the current `this' as an enclosing
10831              context. */
10832           else
10833             extra_arg = current_this;
10834           args = tree_cons (NULL_TREE, extra_arg, args);
10835         }
10836       else
10837         args = tree_cons (NULL_TREE, integer_zero_node, args);
10838     }
10839
10840   /* This handles the situation where a constructor invocation needs
10841      to have an enclosing context passed as a second parameter (the
10842      constructor is one of an inner class). */
10843   if ((is_super_init ||
10844        (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10845       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10846     {
10847       tree dest = TYPE_NAME (DECL_CONTEXT (list));
10848       tree extra_arg =
10849         build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10850       extra_arg = java_complete_tree (extra_arg);
10851       args = tree_cons (NULL_TREE, extra_arg, args);
10852     }
10853
10854   is_static_flag = METHOD_STATIC (list);
10855   if (! is_static_flag && this_arg != NULL_TREE)
10856     args = tree_cons (NULL_TREE, this_arg, args);
10857
10858   /* In the context of an explicit constructor invocation, we can't
10859      invoke any method relying on `this'. Exceptions are: we're
10860      invoking a static function, primary exists and is not the current
10861      this, we're creating a new object. */
10862   if (ctxp->explicit_constructor_p
10863       && !is_static_flag
10864       && (!primary || primary == current_this)
10865       && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10866     {
10867       parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10868       PATCH_METHOD_RETURN_ERROR ();
10869     }
10870   java_parser_context_restore_global ();
10871   if (is_static)
10872     *is_static = is_static_flag;
10873   /* Sometimes, we want the decl of the selected method. Such as for
10874      EH checking */
10875   if (ret_decl)
10876     *ret_decl = list;
10877   patch = patch_invoke (patch, list, args);
10878
10879   /* Now is a good time to insert the call to finit$ */
10880   if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10881     {
10882       tree finit_parms, finit_call;
10883
10884       /* Prepare to pass hidden parameters to finit$, if any. */
10885       finit_parms = build_alias_initializer_parameter_list
10886         (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10887
10888       finit_call =
10889         build_method_invocation (build_wfl_node (finit_identifier_node),
10890                                  finit_parms);
10891
10892       /* Generate the code used to initialize fields declared with an
10893          initialization statement and build a compound statement along
10894          with the super constructor invocation. */
10895       CAN_COMPLETE_NORMALLY (patch) = 1;
10896       patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10897                       java_complete_tree (finit_call));
10898     }
10899   return patch;
10900 }
10901
10902 /* Check that we're not trying to do a static reference to a method in
10903    non static method. Return 1 if it's the case, 0 otherwise. */
10904
10905 static int
10906 check_for_static_method_reference (tree wfl, tree node, tree method,
10907                                    tree where, tree primary)
10908 {
10909   if (METHOD_STATIC (current_function_decl)
10910       && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10911     {
10912       char *fct_name = xstrdup (lang_printable_name (method, 0));
10913       parse_error_context
10914         (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10915          lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10916          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10917       free (fct_name);
10918       return 1;
10919     }
10920   return 0;
10921 }
10922
10923 /* Fix the invocation of *MDECL if necessary in the case of a
10924    invocation from an inner class. *THIS_ARG might be modified
10925    appropriately and an alternative access to *MDECL might be
10926    returned.  */
10927
10928 static int
10929 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10930 {
10931   tree ctx;
10932   tree md = *mdecl, ta = *this_arg;
10933   int to_return = 0;
10934   int non_static_context = !METHOD_STATIC (md);
10935
10936   if (is_super_init
10937       || DECL_CONTEXT (md) == current_class
10938       || !PURE_INNER_CLASS_TYPE_P (current_class)
10939       || DECL_FINIT_P (md)
10940       || DECL_INSTINIT_P (md))
10941     return 0;
10942
10943   /* If we're calling a method found in an enclosing class, generate
10944      what it takes to retrieve the right this. Don't do that if we're
10945      invoking a static method. Note that if MD's type is unrelated to
10946      CURRENT_CLASS, then the current this can be used. */
10947
10948   if (non_static_context 
10949       && !inherits_from_p (current_class, DECL_CONTEXT (md)))
10950     {
10951       ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10952       if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10953         {
10954           ta = build_current_thisn (current_class);
10955           ta = build_wfl_node (ta);
10956         }
10957       else
10958         {
10959           tree type = ctx;
10960           while (type)
10961             {
10962               maybe_build_thisn_access_method (type);
10963               if (inherits_from_p (type, DECL_CONTEXT (md)))
10964                 {
10965                   ta = build_access_to_thisn (ctx, type, 0);
10966                   break;
10967                 }
10968               type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10969                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10970             }
10971         }
10972       ta = java_complete_tree (ta);
10973     }
10974
10975   /* We might have to use an access method to get to MD. We can
10976      break the method access rule as far as we're not generating
10977      bytecode */
10978   if (METHOD_PRIVATE (md) && flag_emit_class_files)
10979     {
10980       md = build_outer_method_access_method (md);
10981       to_return = 1;
10982     }
10983
10984   *mdecl = md;
10985   *this_arg = ta;
10986
10987   /* Returning a nonzero value indicates we were doing a non static
10988      method invocation that is now a static invocation. It will have
10989      callee displace `this' to insert it in the regular argument
10990      list. */
10991   return (non_static_context && to_return);
10992 }
10993
10994 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10995    mode.  */
10996
10997 static tree
10998 patch_invoke (tree patch, tree method, tree args)
10999 {
11000   tree dtable, func;
11001   tree original_call, t, ta;
11002   tree check = NULL_TREE;
11003
11004   /* Last step for args: convert build-in types. If we're dealing with
11005      a new TYPE() type call, the first argument to the constructor
11006      isn't found in the incoming argument list, but delivered by
11007      `new' */
11008   t = TYPE_ARG_TYPES (TREE_TYPE (method));
11009   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
11010     t = TREE_CHAIN (t);
11011   for (ta = args; t != end_params_node && ta;
11012        t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
11013     if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
11014         TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
11015       TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
11016
11017   /* Resolve unresolved returned type issues */
11018   t = TREE_TYPE (TREE_TYPE (method));
11019   if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
11020     resolve_and_layout (TREE_TYPE (t), NULL);
11021
11022   if (flag_emit_class_files)
11023     func = method;
11024   else
11025     {
11026       switch (invocation_mode (method, CALL_USING_SUPER (patch)))
11027         {
11028         case INVOKE_VIRTUAL:
11029           dtable = invoke_build_dtable (0, args);
11030           func = build_invokevirtual (dtable, method);
11031           break;
11032
11033         case INVOKE_NONVIRTUAL:
11034           /* If the object for the method call is null, we throw an
11035              exception.  We don't do this if the object is the current
11036              method's `this'.  In other cases we just rely on an
11037              optimization pass to eliminate redundant checks.  */
11038           if (TREE_VALUE (args) != current_this)
11039             {
11040               /* We use a save_expr here to make sure we only evaluate
11041                  the new `self' expression once.  */
11042               tree save_arg = save_expr (TREE_VALUE (args));
11043               TREE_VALUE (args) = save_arg;
11044               check = java_check_reference (save_arg, 1);
11045             }
11046           /* Fall through.  */
11047
11048         case INVOKE_SUPER:
11049         case INVOKE_STATIC:
11050           {
11051             tree signature = build_java_signature (TREE_TYPE (method));
11052             func = build_known_method_ref (method, TREE_TYPE (method),
11053                                            DECL_CONTEXT (method),
11054                                            signature, args);
11055           }
11056           break;
11057
11058         case INVOKE_INTERFACE:
11059           dtable = invoke_build_dtable (1, args);
11060           func = build_invokeinterface (dtable, method);
11061           break;
11062
11063         default:
11064           abort ();
11065         }
11066
11067       /* Ensure self_type is initialized, (invokestatic). FIXME */
11068       func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11069     }
11070
11071   TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11072   TREE_OPERAND (patch, 0) = func;
11073   TREE_OPERAND (patch, 1) = args;
11074   patch = check_for_builtin (method, patch);
11075   original_call = patch;
11076
11077   /* We're processing a `new TYPE ()' form. New is called and its
11078      returned value is the first argument to the constructor. We build
11079      a COMPOUND_EXPR and use saved expression so that the overall NEW
11080      expression value is a pointer to a newly created and initialized
11081      class. */
11082   if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11083     {
11084       tree class = DECL_CONTEXT (method);
11085       tree c1, saved_new, new;
11086       tree alloc_node;
11087
11088       if (flag_emit_class_files)
11089         {
11090           TREE_TYPE (patch) = build_pointer_type (class);
11091           return patch;
11092         }
11093       if (!TYPE_SIZE (class))
11094         safe_layout_class (class);
11095       alloc_node =
11096         (class_has_finalize_method (class) ? alloc_object_node
11097                                            : alloc_no_finalizer_node);
11098       new = build3 (CALL_EXPR, promote_type (class),
11099                     build_address_of (alloc_node),
11100                     build_tree_list (NULL_TREE, build_class_ref (class)),
11101                     NULL_TREE);
11102       saved_new = save_expr (new);
11103       c1 = build_tree_list (NULL_TREE, saved_new);
11104       TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11105       TREE_OPERAND (original_call, 1) = c1;
11106       TREE_SET_CODE (original_call, CALL_EXPR);
11107       patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11108     }
11109
11110   /* If CHECK is set, then we are building a check to see if the object
11111      is NULL.  */
11112   if (check != NULL_TREE)
11113     {
11114       /* We have to call force_evaluation_order now because creating a
11115          COMPOUND_EXPR wraps the arg list in a way that makes it
11116          unrecognizable by force_evaluation_order later.  Yuk.  */
11117       patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check, 
11118                       force_evaluation_order (patch));
11119       TREE_SIDE_EFFECTS (patch) = 1;
11120     }
11121
11122   /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11123      put it as the first expression of a COMPOUND_EXPR. The second
11124      expression being an empty statement to be later patched if
11125      necessary. We remember a TREE_LIST (the PURPOSE is the method,
11126      the VALUE is the compound) in a hashtable and return a
11127      COMPOUND_EXPR built so that the result of the evaluation of the
11128      original PATCH node is returned. */
11129   if (STATIC_CLASS_INIT_OPT_P ()
11130       && current_function_decl && METHOD_STATIC (method))
11131     {
11132       tree list;
11133       tree fndecl = current_function_decl;
11134       /* We have to call force_evaluation_order now because creating a
11135          COMPOUND_EXPR wraps the arg list in a way that makes it
11136          unrecognizable by force_evaluation_order later.  Yuk.  */
11137       tree save = force_evaluation_order (patch);
11138       tree type = TREE_TYPE (patch);
11139
11140       patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11141       list = tree_cons (method, patch,
11142                         DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11143
11144       DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11145
11146       patch = build2 (COMPOUND_EXPR, type, patch, save);
11147     }
11148
11149   return patch;
11150 }
11151
11152 static int
11153 invocation_mode (tree method, int super)
11154 {
11155   int access = get_access_flags_from_decl (method);
11156
11157   if (super)
11158     return INVOKE_SUPER;
11159
11160   if (access & ACC_STATIC)
11161     return INVOKE_STATIC;
11162
11163   /* We have to look for a constructor before we handle nonvirtual
11164      calls; otherwise the constructor will look nonvirtual.  */
11165   if (DECL_CONSTRUCTOR_P (method))
11166     return INVOKE_STATIC;
11167
11168   if (access & ACC_PRIVATE)
11169     return INVOKE_NONVIRTUAL;
11170
11171   /* Binary compatibility: just because it's final today, that doesn't
11172      mean it'll be final tomorrow.  */
11173   if (! flag_indirect_dispatch  
11174       || DECL_CONTEXT (method) == object_type_node)
11175     {
11176       if (access & ACC_FINAL)
11177         return INVOKE_NONVIRTUAL;
11178
11179       if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11180         return INVOKE_NONVIRTUAL;
11181     }
11182
11183   if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11184     return INVOKE_INTERFACE;
11185
11186   return INVOKE_VIRTUAL;
11187 }
11188
11189 /* Retrieve a refined list of matching methods. It covers the step
11190    15.11.2 (Compile-Time Step 2) */
11191
11192 static tree
11193 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11194 {
11195   tree atl = end_params_node;           /* Arg Type List */
11196   tree method, signature, list, node;
11197   const char *candidates;               /* Used for error report */
11198   char *dup;
11199
11200   /* Fix the arguments */
11201   for (node = arg_list; node; node = TREE_CHAIN (node))
11202     {
11203       tree current_arg = TREE_TYPE (TREE_VALUE (node));
11204       /* Non primitive type may have to be resolved */
11205       if (!JPRIMITIVE_TYPE_P (current_arg))
11206         resolve_and_layout (current_arg, NULL_TREE);
11207       /* And promoted */
11208       if (TREE_CODE (current_arg) == RECORD_TYPE)
11209         current_arg = promote_type (current_arg);
11210       atl = tree_cons (NULL_TREE, current_arg, atl);
11211     }
11212
11213   /* Presto. If we're dealing with an anonymous class and a
11214      constructor call, generate the right constructor now, since we
11215      know the arguments' types. */
11216
11217   if (lc && ANONYMOUS_CLASS_P (class))
11218     {
11219       tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11220       /* The anonymous class may have already been laid out, so make sure
11221          the new constructor is laid out here.  */
11222       layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11223     }
11224
11225   /* Find all candidates and then refine the list, searching for the
11226      most specific method. */
11227   list = find_applicable_accessible_methods_list (lc, class, name, atl);
11228   list = find_most_specific_methods_list (list);
11229   if (list && !TREE_CHAIN (list))
11230     return TREE_VALUE (list);
11231
11232   /* Issue an error. List candidates if any. Candidates are listed
11233      only if accessible (non accessible methods may end-up here for
11234      the sake of a better error report). */
11235   candidates = NULL;
11236   if (list)
11237     {
11238       tree current;
11239       obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11240       for (current = list; current; current = TREE_CHAIN (current))
11241         {
11242           tree cm = TREE_VALUE (current);
11243           char string [4096];
11244           if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11245             continue;
11246           sprintf
11247             (string, "  '%s' in '%s'%s",
11248              get_printable_method_name (cm),
11249              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11250              (TREE_CHAIN (current) ? "\n" : ""));
11251           obstack_grow (&temporary_obstack, string, strlen (string));
11252         }
11253       obstack_1grow (&temporary_obstack, '\0');
11254       candidates = obstack_finish (&temporary_obstack);
11255     }
11256   /* Issue the error message */
11257   method = make_node (FUNCTION_TYPE);
11258   TYPE_ARG_TYPES (method) = atl;
11259   signature = build_java_argument_signature (method);
11260   dup = xstrdup (lang_printable_name (class, 0));
11261   parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11262                        (lc ? "constructor" : "method"),
11263                        (lc ? dup : IDENTIFIER_POINTER (name)),
11264                        IDENTIFIER_POINTER (signature), dup,
11265                        (candidates ? candidates : ""));
11266   free (dup);
11267   return NULL_TREE;
11268 }
11269
11270 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11271    when we're looking for a constructor. */
11272
11273 static tree
11274 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11275                                          tree arglist)
11276 {
11277   static htab_t searched_classes;
11278   static int search_not_done = 0;
11279   tree list = NULL_TREE, all_list = NULL_TREE;
11280   tree base_binfo;
11281   int i;
11282
11283   /* Check the hash table to determine if this class has been searched
11284      already. */
11285   if (searched_classes)
11286     {
11287       if (htab_find (searched_classes, class) != NULL)
11288         return NULL;
11289     }
11290   else
11291     {
11292       searched_classes = htab_create (10, htab_hash_pointer,
11293                                       htab_eq_pointer, NULL);
11294     }
11295
11296   search_not_done++;
11297   *htab_find_slot (searched_classes, class, INSERT) = class;
11298
11299   if (!CLASS_LOADED_P (class))
11300     {
11301       load_class (class, 1);
11302       safe_layout_class (class);
11303     }
11304
11305   /* Search interfaces */
11306   if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11307       && CLASS_INTERFACE (TYPE_NAME (class)))
11308     {
11309       search_applicable_methods_list (lc, TYPE_METHODS (class),
11310                                       name, arglist, &list, &all_list);
11311       for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11312         {
11313           tree t = BINFO_TYPE (base_binfo);
11314           tree rlist;
11315           
11316           rlist = find_applicable_accessible_methods_list (lc,  t, name,
11317                                                            arglist);
11318           list = chainon (rlist, list);
11319         }
11320     }
11321   /* Search classes */
11322   else
11323     {
11324       search_applicable_methods_list (lc, TYPE_METHODS (class),
11325                                       name, arglist, &list, &all_list);
11326
11327       /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11328          that we only search in class. Note that we should have found
11329          something at this point. */
11330       if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11331         {
11332           lc = 1;
11333           if (!list)
11334             abort ();
11335         }
11336
11337       /* We must search all interfaces of this class */
11338       if (!lc)
11339         {
11340           for (i = 1;
11341                BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11342             {
11343               tree t = BINFO_TYPE (base_binfo);
11344               if (t != object_type_node)
11345                 {
11346                   tree rlist
11347                     = find_applicable_accessible_methods_list (lc, t,
11348                                                                name, arglist);
11349                   list = chainon (rlist, list);
11350                 }
11351             }
11352         }
11353
11354       /* Search superclass */
11355       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11356         {
11357           tree rlist;
11358           class = CLASSTYPE_SUPER (class);
11359           rlist = find_applicable_accessible_methods_list (lc, class,
11360                                                            name, arglist);
11361           list = chainon (rlist, list);
11362         }
11363     }
11364
11365   search_not_done--;
11366
11367   /* We're done. Reset the searched classes list and finally search
11368      java.lang.Object if it wasn't searched already. */
11369   if (!search_not_done)
11370     {
11371       if (!lc
11372           && TYPE_METHODS (object_type_node)
11373           && htab_find (searched_classes, object_type_node) == NULL)
11374         {
11375           search_applicable_methods_list (lc,
11376                                           TYPE_METHODS (object_type_node),
11377                                           name, arglist, &list, &all_list);
11378         }
11379       htab_delete (searched_classes);
11380       searched_classes = NULL;
11381     }
11382
11383   /* Either return the list obtained or all selected (but
11384      inaccessible) methods for better error report. */
11385   return (!list ? all_list : list);
11386 }
11387
11388 /* Effectively search for the appropriate method in method */
11389
11390 static void
11391 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11392                                 tree *list, tree *all_list)
11393 {
11394   for (; method; method = TREE_CHAIN (method))
11395     {
11396       /* When dealing with constructor, stop here, otherwise search
11397          other classes */
11398       if (lc && !DECL_CONSTRUCTOR_P (method))
11399         continue;
11400       else if (!lc && (DECL_CONSTRUCTOR_P (method)
11401                        || (DECL_NAME (method) != name)))
11402         continue;
11403
11404       if (argument_types_convertible (method, arglist))
11405         {
11406           /* Retain accessible methods only */
11407           if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11408                                  method, NULL_TREE, 0))
11409             *list = tree_cons (NULL_TREE, method, *list);
11410           else
11411             /* Also retain all selected method here */
11412             *all_list = tree_cons (NULL_TREE, method, *list);
11413         }
11414     }
11415 }
11416
11417 /* 15.11.2.2 Choose the Most Specific Method */
11418
11419 static tree
11420 find_most_specific_methods_list (tree list)
11421 {
11422   int max = 0;
11423   int abstract, candidates;
11424   tree current, new_list = NULL_TREE;
11425   for (current = list; current; current = TREE_CHAIN (current))
11426     {
11427       tree method;
11428       DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11429
11430       for (method = list; method; method = TREE_CHAIN (method))
11431         {
11432           tree method_v, current_v;
11433           /* Don't test a method against itself */
11434           if (method == current)
11435             continue;
11436
11437           method_v = TREE_VALUE (method);
11438           current_v = TREE_VALUE (current);
11439
11440           /* Compare arguments and location where methods where declared */
11441           if (argument_types_convertible (method_v, current_v))
11442             {
11443               if (valid_method_invocation_conversion_p
11444                   (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v)))
11445                 {
11446                   int v = (DECL_SPECIFIC_COUNT (current_v) += 1);
11447                   max = (v > max ? v : max);
11448                 }
11449             }
11450         }
11451     }
11452
11453   /* Review the list and select the maximally specific methods */
11454   for (current = list, abstract = -1, candidates = -1;
11455        current; current = TREE_CHAIN (current))
11456     if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11457       {
11458         new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11459         abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11460         candidates++;
11461       }
11462
11463   /* If we have several and they're all abstract, just pick the
11464      closest one. */
11465   if (candidates > 0 && candidates == abstract)
11466     {
11467       /* FIXME: merge the throws clauses.  There is no convenient way
11468          to do this in gcj right now, since ideally we'd like to
11469          introduce a new METHOD_DECL here, but that is really not
11470          possible.  */
11471       new_list = nreverse (new_list);
11472       TREE_CHAIN (new_list) = NULL_TREE;
11473       return new_list;
11474     }
11475
11476   /* We have several (we couldn't find a most specific), all but one
11477      are abstract, we pick the only non abstract one. */
11478   if (candidates > 0 && (candidates == abstract+1))
11479     {
11480       for (current = new_list; current; current = TREE_CHAIN (current))
11481         if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11482           {
11483             TREE_CHAIN (current) = NULL_TREE;
11484             new_list = current;
11485           }
11486     }
11487
11488   /* If we can't find one, lower expectations and try to gather multiple
11489      maximally specific methods */
11490   while (!new_list && max)
11491     {
11492       while (--max > 0)
11493         {
11494           if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11495             new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11496         }
11497     }
11498
11499   return new_list;
11500 }
11501
11502 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11503    converted by method invocation conversion (5.3) to the type of the
11504    corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11505    to change less often than M1. */
11506
11507 static GTY(()) tree m2_arg_value;
11508 static GTY(()) tree m2_arg_cache;
11509
11510 static int
11511 argument_types_convertible (tree m1, tree m2_or_arglist)
11512 {
11513   tree m1_arg, m2_arg;
11514
11515   SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11516
11517   if (m2_arg_value == m2_or_arglist)
11518     m2_arg = m2_arg_cache;
11519   else
11520     {
11521       /* M2_OR_ARGLIST can be a function DECL or a raw list of
11522          argument types */
11523       if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11524         {
11525           m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11526           if (!METHOD_STATIC (m2_or_arglist))
11527             m2_arg = TREE_CHAIN (m2_arg);
11528         }
11529       else
11530         m2_arg = m2_or_arglist;
11531
11532       m2_arg_value = m2_or_arglist;
11533       m2_arg_cache = m2_arg;
11534     }
11535
11536   while (m1_arg != end_params_node && m2_arg != end_params_node)
11537     {
11538       resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11539       if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11540                                                  TREE_VALUE (m2_arg)))
11541         break;
11542       m1_arg = TREE_CHAIN (m1_arg);
11543       m2_arg = TREE_CHAIN (m2_arg);
11544     }
11545   return m1_arg == end_params_node && m2_arg == end_params_node;
11546 }
11547
11548 /* Qualification routines */
11549
11550 /* Given a name x.y.z, look up x locally.  If it's found, save the
11551    decl.  If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11552    so that we later try and load the appropriate classes.  */
11553 static void
11554 qualify_ambiguous_name (tree id)
11555 {
11556   tree name, decl;
11557
11558   /* We inspect the first item of the qualification list.  As a sanity
11559      check, make sure that it is an identfier node.  */
11560   tree qual = EXPR_WFL_QUALIFICATION (id);
11561   tree qual_wfl = QUAL_WFL (qual);
11562
11563   if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11564     return;
11565
11566   name = EXPR_WFL_NODE (qual_wfl);
11567
11568   /* If we don't have an identifier, or we have a 'this' or 'super',
11569      then field access processing is all we need : there is nothing
11570      for us to do.  */
11571   if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11572       name == this_identifier_node ||
11573       name == super_identifier_node)
11574     return;
11575
11576   /* If name appears within the scope of a local variable declaration
11577      or parameter declaration, or is a field within an enclosing
11578      class, then it is an expression name.  Save the decl and let
11579      resolve_field_access do it's work.  */
11580   if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11581       (decl = lookup_field_wrapper (current_class, name)))
11582     {
11583       QUAL_RESOLUTION (qual) = decl;
11584       return;
11585     }
11586
11587   /* If name is a known class name (either declared or imported), mark
11588      us as a type name.  */
11589   if ((decl = resolve_and_layout (name, NULL_TREE)))
11590     {
11591       RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11592       QUAL_RESOLUTION (qual) = decl;
11593     }
11594
11595   /* Check here that NAME isn't declared by more than one
11596      type-import-on-demand declaration of the compilation unit
11597      containing NAME. FIXME */
11598
11599   /* We couldn't find a declaration for the name.  Assume for now that
11600      we have a qualified class name that needs to be loaded from an
11601      external class file.  */
11602   else
11603     RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11604
11605   /* Propagate the qualification across other components of the
11606      qualified name */
11607   for (qual = TREE_CHAIN (qual); qual;
11608        qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11609     {
11610       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11611         RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11612     }
11613
11614   /* Store the global qualification for the ambiguous part of ID back
11615      into ID fields */
11616   if (RESOLVE_TYPE_NAME_P (qual_wfl))
11617     RESOLVE_TYPE_NAME_P (id) = 1;
11618   else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11619     RESOLVE_PACKAGE_NAME_P (id) = 1;
11620 }
11621
11622 /* Patch tree nodes in a function body. When a BLOCK is found, push
11623    local variable decls if present.
11624    Same as java_complete_lhs, but does resolve static finals to values. */
11625
11626 static tree
11627 java_complete_tree (tree node)
11628 {
11629   node = java_complete_lhs (node);
11630   if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11631       && DECL_INITIAL (node) != NULL_TREE)
11632     {
11633       tree value = fold_constant_for_init (node, node);
11634       if (value != NULL_TREE)
11635         return value;
11636     }
11637   return node;
11638 }
11639
11640 static tree
11641 java_stabilize_reference (tree node)
11642 {
11643   if (TREE_CODE (node) == COMPOUND_EXPR)
11644     {
11645       tree op0 = TREE_OPERAND (node, 0);
11646       tree op1 = TREE_OPERAND (node, 1);
11647       TREE_OPERAND (node, 0) = save_expr (op0);
11648       TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11649       return node;
11650     }
11651   return stabilize_reference (node);
11652 }
11653
11654 /* Patch tree nodes in a function body. When a BLOCK is found, push
11655    local variable decls if present.
11656    Same as java_complete_tree, but does not resolve static finals to values. */
11657
11658 static tree
11659 java_complete_lhs (tree node)
11660 {
11661   tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11662   int flag;
11663
11664   /* CONVERT_EXPR always has its type set, even though it needs to be
11665      worked out. */
11666   if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11667     return node;
11668
11669   /* The switch block implements cases processing container nodes
11670      first.  Contained nodes are always written back. Leaves come
11671      next and return a value. */
11672   switch (TREE_CODE (node))
11673     {
11674     case BLOCK:
11675
11676       /* 1- Block section.
11677          Set the local values on decl names so we can identify them
11678          faster when they're referenced. At that stage, identifiers
11679          are legal so we don't check for declaration errors. */
11680       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11681         {
11682           DECL_CONTEXT (cn) = current_function_decl;
11683           IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11684         }
11685       if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11686           CAN_COMPLETE_NORMALLY (node) = 1;
11687       else
11688         {
11689           tree stmt = BLOCK_EXPR_BODY (node);
11690           tree *ptr;
11691           int error_seen = 0;
11692           if (TREE_CODE (stmt) == COMPOUND_EXPR)
11693             {
11694               /* Re-order from (((A; B); C); ...; Z) to
11695                  (A; (B; (C ; (...; Z)))).
11696                  This makes it easier to scan the statements left-to-right
11697                  without using recursion (which might overflow the stack
11698                  if the block has many statements. */
11699               for (;;)
11700                 {
11701                   tree left = TREE_OPERAND (stmt, 0);
11702                   if (TREE_CODE (left) != COMPOUND_EXPR)
11703                     break;
11704                   TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11705                   TREE_OPERAND (left, 1) = stmt;
11706                   stmt = left;
11707                 }
11708               BLOCK_EXPR_BODY (node) = stmt;
11709             }
11710
11711           /* Now do the actual complete, without deep recursion for
11712              long blocks. */
11713           ptr = &BLOCK_EXPR_BODY (node);
11714           while (TREE_CODE (*ptr) == COMPOUND_EXPR
11715                  && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11716             {
11717               tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11718               tree *next = &TREE_OPERAND (*ptr, 1);
11719               TREE_OPERAND (*ptr, 0) = cur;
11720               if (IS_EMPTY_STMT (cur))
11721                 {
11722                   /* Optimization;  makes it easier to detect empty bodies.
11723                      Most useful for <clinit> with all-constant initializer. */
11724                   *ptr = *next;
11725                   continue;
11726                 }
11727               if (TREE_CODE (cur) == ERROR_MARK)
11728                 error_seen++;
11729               else if (! CAN_COMPLETE_NORMALLY (cur))
11730                 {
11731                   wfl_op2 = *next;
11732                   for (;;)
11733                     {
11734                       if (TREE_CODE (wfl_op2) == BLOCK)
11735                         wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11736                       else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11737                         wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11738                       else
11739                         break;
11740                     }
11741                   if (TREE_CODE (wfl_op2) != CASE_EXPR
11742                       && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11743                     unreachable_stmt_error (*ptr);
11744                 }
11745               if (TREE_TYPE (*ptr) == NULL_TREE)
11746                 TREE_TYPE (*ptr) = void_type_node;
11747               ptr = next;
11748             }
11749           *ptr = java_complete_tree (*ptr);
11750
11751           if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11752             return error_mark_node;
11753           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11754         }
11755       /* Turn local bindings to null */
11756       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11757         IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11758
11759       TREE_TYPE (node) = void_type_node;
11760       break;
11761
11762       /* 2- They are expressions but ultimately deal with statements */
11763
11764     case THROW_EXPR:
11765       wfl_op1 = TREE_OPERAND (node, 0);
11766       COMPLETE_CHECK_OP_0 (node);
11767       /* 14.19 A throw statement cannot complete normally. */
11768       CAN_COMPLETE_NORMALLY (node) = 0;
11769       return patch_throw_statement (node, wfl_op1);
11770
11771     case SYNCHRONIZED_EXPR:
11772       wfl_op1 = TREE_OPERAND (node, 0);
11773       return patch_synchronized_statement (node, wfl_op1);
11774
11775     case TRY_EXPR:
11776       return patch_try_statement (node);
11777
11778     case TRY_FINALLY_EXPR:
11779       COMPLETE_CHECK_OP_0 (node);
11780       COMPLETE_CHECK_OP_1 (node);
11781       if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11782         /* Reduce try/finally nodes with an empty try block.  */
11783         return TREE_OPERAND (node, 1);
11784       if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11785         /* Likewise for an empty finally block.  */
11786         return TREE_OPERAND (node, 0);
11787       CAN_COMPLETE_NORMALLY (node)
11788         = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11789            && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11790       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11791       return node;
11792
11793     case LABELED_BLOCK_EXPR:
11794       PUSH_LABELED_BLOCK (node);
11795       if (LABELED_BLOCK_BODY (node))
11796         COMPLETE_CHECK_OP_1 (node);
11797       TREE_TYPE (node) = void_type_node;
11798       POP_LABELED_BLOCK ();
11799
11800       if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11801         {
11802           LABELED_BLOCK_BODY (node) = NULL_TREE;
11803           CAN_COMPLETE_NORMALLY (node) = 1;
11804         }
11805       else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11806         CAN_COMPLETE_NORMALLY (node) = 1;
11807       return node;
11808
11809     case EXIT_BLOCK_EXPR:
11810       return patch_bc_statement (node);
11811
11812     case CASE_EXPR:
11813       cn = java_complete_tree (TREE_OPERAND (node, 0));
11814       if (cn == error_mark_node)
11815         return cn;
11816
11817       /* First, the case expression must be constant. Values of final
11818          fields are accepted. */
11819       nn = fold_constant_for_init (cn, NULL_TREE);
11820       if (nn != NULL_TREE)
11821         cn = nn;
11822
11823       cn = fold (cn);
11824       if ((TREE_CODE (cn) == COMPOUND_EXPR
11825            || TREE_CODE (cn) == COMPONENT_REF)
11826           && JDECL_P (TREE_OPERAND (cn, 1))
11827           && FIELD_FINAL (TREE_OPERAND (cn, 1))
11828           && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11829         {
11830           cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11831                                        TREE_OPERAND (cn, 1));
11832         }
11833       /* Accept final locals too. */
11834       else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn) 
11835                && DECL_INITIAL (cn))
11836         cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11837
11838       if (!TREE_CONSTANT (cn))
11839         {
11840           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11841           parse_error_context (node, "Constant expression required");
11842           return error_mark_node;
11843         }
11844
11845       nn = ctxp->current_loop;
11846
11847       /* It must be assignable to the type of the switch expression. */
11848       if (!try_builtin_assignconv (NULL_TREE,
11849                                    TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11850         {
11851           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11852           parse_error_context
11853             (wfl_operator,
11854              "Incompatible type for case. Can't convert %qs to %<int%>",
11855              lang_printable_name (TREE_TYPE (cn), 0));
11856           return error_mark_node;
11857         }
11858
11859       cn = fold (convert (int_type_node, cn));
11860       TREE_CONSTANT_OVERFLOW (cn) = 0;
11861       CAN_COMPLETE_NORMALLY (cn) = 1;
11862
11863       /* Save the label on a list so that we can later check for
11864          duplicates.  */
11865       case_label_list = tree_cons (node, cn, case_label_list);
11866
11867       /* Multiple instance of a case label bearing the same value is
11868          checked later. The case expression is all right so far. */
11869       if (TREE_CODE (cn) == VAR_DECL)
11870         cn = DECL_INITIAL (cn);
11871       TREE_OPERAND (node, 0) = cn;
11872       TREE_TYPE (node) = void_type_node;
11873       CAN_COMPLETE_NORMALLY (node) = 1;
11874       TREE_SIDE_EFFECTS (node) = 1;
11875       break;
11876
11877     case DEFAULT_EXPR:
11878       nn = ctxp->current_loop;
11879       /* Only one default label is allowed per switch statement */
11880       if (SWITCH_HAS_DEFAULT (nn))
11881         {
11882 #ifdef USE_MAPPED_LOCATION
11883           SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11884 #else
11885           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11886 #endif
11887           parse_error_context (wfl_operator,
11888                                "Duplicate case label: %<default%>");
11889           return error_mark_node;
11890         }
11891       else
11892         SWITCH_HAS_DEFAULT (nn) = 1;
11893       TREE_TYPE (node) = void_type_node;
11894       TREE_SIDE_EFFECTS (node) = 1;
11895       CAN_COMPLETE_NORMALLY (node) = 1;
11896       break;
11897
11898     case SWITCH_EXPR:
11899     case LOOP_EXPR:
11900       PUSH_LOOP (node);
11901       /* Check whether the loop was enclosed in a labeled
11902          statement. If not, create one, insert the loop in it and
11903          return the node */
11904       nn = patch_loop_statement (node);
11905
11906       /* Anyways, walk the body of the loop */
11907       if (TREE_CODE (node) == LOOP_EXPR)
11908         TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11909       /* Switch statement: walk the switch expression and the cases */
11910       else
11911         node = patch_switch_statement (node);
11912
11913       if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11914         nn = error_mark_node;
11915       else
11916         {
11917           TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11918           /* If we returned something different, that's because we
11919              inserted a label. Pop the label too. */
11920           if (nn != node)
11921             {
11922               if (CAN_COMPLETE_NORMALLY (node))
11923                 CAN_COMPLETE_NORMALLY (nn) = 1;
11924               POP_LABELED_BLOCK ();
11925             }
11926         }
11927       POP_LOOP ();
11928       return nn;
11929
11930     case EXIT_EXPR:
11931       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11932       return patch_exit_expr (node);
11933
11934     case COND_EXPR:
11935       /* Condition */
11936       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11937       if (TREE_OPERAND (node, 0) == error_mark_node)
11938         return error_mark_node;
11939       /* then-else branches */
11940       TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11941       if (TREE_OPERAND (node, 1) == error_mark_node)
11942         return error_mark_node;
11943       {
11944         /* This is a special case due to build_assertion().  When
11945            assertions are disabled we build a COND_EXPR in which
11946            Operand 1 is the body of the assertion.  If that happens to
11947            be a string concatenation we'll need to patch it here.  */
11948         tree patched = patch_string (TREE_OPERAND (node, 1));
11949         if (patched)
11950           TREE_OPERAND (node, 1) = patched;
11951       }
11952      TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11953       if (TREE_OPERAND (node, 2) == error_mark_node)
11954         return error_mark_node;
11955       return patch_if_else_statement (node);
11956       break;
11957
11958     case CONDITIONAL_EXPR:
11959       /* Condition */
11960       wfl_op1 = TREE_OPERAND (node, 0);
11961       COMPLETE_CHECK_OP_0 (node);
11962       wfl_op2 = TREE_OPERAND (node, 1);
11963       COMPLETE_CHECK_OP_1 (node);
11964       wfl_op3 = TREE_OPERAND (node, 2);
11965       COMPLETE_CHECK_OP_2 (node);
11966       return patch_conditional_expr (node, wfl_op1, wfl_op2);
11967
11968       /* 3- Expression section */
11969     case COMPOUND_EXPR:
11970       wfl_op2 = TREE_OPERAND (node, 1);
11971       TREE_OPERAND (node, 0) = nn =
11972         java_complete_tree (TREE_OPERAND (node, 0));
11973       if (IS_EMPTY_STMT (wfl_op2))
11974         CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11975       else
11976         {
11977           if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11978             {
11979               /* An unreachable condition in a do-while statement
11980                  is *not* (technically) an unreachable statement. */
11981               nn = wfl_op2;
11982               if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11983                 nn = EXPR_WFL_NODE (nn);
11984               /* NN can be NULL_TREE exactly when UPDATE is, in
11985                  finish_for_loop.  */
11986               if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11987                 {
11988                   SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11989                   if (SUPPRESS_UNREACHABLE_ERROR (nn))
11990                     {
11991                       /* Perhaps this warning should have an
11992                          associated flag.  The code being compiled is
11993                          pedantically correct, but useless.  */
11994                       parse_warning_context (wfl_operator,
11995                                              "Unreachable statement");
11996                     }
11997                   else
11998                     parse_error_context (wfl_operator,
11999                                          "Unreachable statement");
12000                 }
12001             }
12002           TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
12003           if (TREE_OPERAND (node, 1) == error_mark_node)
12004             return error_mark_node;
12005           /* Even though we might allow the case where the first
12006              operand doesn't return normally, we still should compute
12007              CAN_COMPLETE_NORMALLY correctly.  */
12008           CAN_COMPLETE_NORMALLY (node)
12009             = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
12010                && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
12011         }
12012       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
12013       break;
12014
12015     case RETURN_EXPR:
12016       /* CAN_COMPLETE_NORMALLY (node) = 0; */
12017       return patch_return (node);
12018
12019     case EXPR_WITH_FILE_LOCATION:
12020       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
12021           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
12022         {
12023           node = resolve_expression_name (node, NULL);
12024           if (node == error_mark_node)
12025             return node;
12026           CAN_COMPLETE_NORMALLY (node) = 1;
12027         }
12028       else
12029         {
12030           tree body;
12031           location_t save_location = input_location;
12032 #ifdef USE_MAPPED_LOCATION
12033           input_location = EXPR_LOCATION (node);
12034           if (input_location == UNKNOWN_LOCATION)
12035             input_location = save_location;
12036 #else
12037           input_line = EXPR_WFL_LINENO (node);
12038 #endif
12039           body = java_complete_tree (EXPR_WFL_NODE (node));
12040           input_location = save_location;
12041           EXPR_WFL_NODE (node) = body;
12042           TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
12043           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
12044           if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
12045             {
12046               /* Makes it easier to constant fold, detect empty bodies. */
12047               return body;
12048             }
12049           if (body == error_mark_node)
12050             {
12051               /* Its important for the evaluation of assignment that
12052                  this mark on the TREE_TYPE is propagated. */
12053               TREE_TYPE (node) = error_mark_node;
12054               return error_mark_node;
12055             }
12056           else
12057             TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12058
12059         }
12060       break;
12061
12062     case NEW_ARRAY_EXPR:
12063       /* Patch all the dimensions */
12064       flag = 0;
12065       for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12066         {
12067           int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12068           tree dim = convert (int_type_node,
12069                               java_complete_tree (TREE_VALUE (cn)));
12070           if (dim == error_mark_node)
12071             {
12072               flag = 1;
12073               continue;
12074             }
12075           else
12076             {
12077               TREE_VALUE (cn) = dim;
12078               /* Setup the location of the current dimension, for
12079                  later error report. */
12080 #ifdef USE_MAPPED_LOCATION
12081               TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12082 #else
12083               TREE_PURPOSE (cn) =
12084                 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12085               EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12086 #endif
12087             }
12088         }
12089       /* They complete the array creation expression, if no errors
12090          were found. */
12091       CAN_COMPLETE_NORMALLY (node) = 1;
12092       return (flag ? error_mark_node
12093               : force_evaluation_order (patch_newarray (node)));
12094
12095     case NEW_ANONYMOUS_ARRAY_EXPR:
12096       /* Create the array type if necessary. */
12097       if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12098         {
12099           tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12100           if (!(type = resolve_type_during_patch (type)))
12101             return error_mark_node;
12102           type = build_array_from_name (type, NULL_TREE,
12103                                         ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12104           ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12105         }
12106       node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12107                                    ANONYMOUS_ARRAY_INITIALIZER (node));
12108       if (node == error_mark_node)
12109         return error_mark_node;
12110       CAN_COMPLETE_NORMALLY (node) = 1;
12111       return node;
12112
12113     case NEW_CLASS_EXPR:
12114     case CALL_EXPR:
12115       /* Complete function's argument(s) first */
12116       if (complete_function_arguments (node))
12117         return error_mark_node;
12118       else
12119         {
12120           tree decl, wfl = TREE_OPERAND (node, 0);
12121           int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12122           int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12123                            super_identifier_node);
12124           tree arguments;
12125 #ifdef USE_MAPPED_LOCATION
12126           source_location location = EXPR_LOCATION (node);
12127 #else
12128           int location = EXPR_WFL_LINECOL (node);
12129 #endif
12130
12131           node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12132                                           from_super, 0, &decl);
12133           if (node == error_mark_node)
12134             return error_mark_node;
12135
12136           if (TREE_CODE (node) == CALL_EXPR
12137               && TREE_OPERAND (node, 1) != NULL_TREE)
12138             arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12139           else
12140             arguments = NULL_TREE;
12141           check_thrown_exceptions (location, decl, arguments);
12142           /* If we call this(...), register signature and positions */
12143           if (in_this)
12144             DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12145               tree_cons (wfl, decl,
12146                          DECL_CONSTRUCTOR_CALLS (current_function_decl));
12147           CAN_COMPLETE_NORMALLY (node) = 1;
12148           return force_evaluation_order (node);
12149         }
12150
12151     case MODIFY_EXPR:
12152       /* Save potential wfls */
12153       wfl_op1 = TREE_OPERAND (node, 0);
12154       TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12155
12156       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12157           && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12158           && DECL_INITIAL (nn) != NULL_TREE)
12159         {
12160           tree value;
12161
12162           value = fold_constant_for_init (nn, nn);
12163
12164           /* When we have a primitype type, or a string and we're not
12165              emitting a class file, we actually don't want to generate
12166              anything for the assignment. */
12167           if (value != NULL_TREE && 
12168               (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) || 
12169                (TREE_TYPE (value) == string_ptr_type_node &&
12170                 ! flag_emit_class_files)))
12171             {
12172               /* Prepare node for patch_assignment */
12173               TREE_OPERAND (node, 1) = value;
12174               /* Call patch assignment to verify the assignment */
12175               if (patch_assignment (node, wfl_op1) == error_mark_node)
12176                 return error_mark_node;
12177               /* Set DECL_INITIAL properly (a conversion might have
12178                  been decided by patch_assignment) and return the
12179                  empty statement. */
12180               else
12181                 {
12182                   tree patched = patch_string (TREE_OPERAND (node, 1));
12183                   if (patched)
12184                     DECL_INITIAL (nn) = patched;
12185                   else
12186                     DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12187                   DECL_FIELD_FINAL_IUD (nn) = 1;
12188                   return build_java_empty_stmt ();
12189                 }
12190             }
12191           if (! flag_emit_class_files)
12192             DECL_INITIAL (nn) = NULL_TREE;
12193         }
12194       wfl_op2 = TREE_OPERAND (node, 1);
12195
12196       if (TREE_OPERAND (node, 0) == error_mark_node)
12197         return error_mark_node;
12198
12199       flag = COMPOUND_ASSIGN_P (wfl_op2);
12200       if (flag)
12201         {
12202           /* This might break when accessing outer field from inner
12203              class. TESTME, FIXME */
12204           tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12205
12206           /* Hand stabilize the lhs on both places */
12207           TREE_OPERAND (node, 0) = lvalue;
12208           TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12209             (flag_emit_class_files ? lvalue : save_expr (lvalue));
12210
12211           /* 15.25.2.a: Left hand is not an array access. FIXME */
12212           /* Now complete the RHS. We write it back later on. */
12213           nn = java_complete_tree (TREE_OPERAND (node, 1));
12214
12215           if ((cn = patch_string (nn)))
12216             nn = cn;
12217
12218           /* The last part of the rewrite for E1 op= E2 is to have
12219              E1 = (T)(E1 op E2), with T being the type of E1. */
12220           nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12221                                                TREE_TYPE (lvalue), nn));
12222
12223           /* If the assignment is compound and has reference type,
12224              then ensure the LHS has type String and nothing else.  */
12225           if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12226               && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12227             parse_error_context (wfl_op2,
12228                                  "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12229                                  lang_printable_name (TREE_TYPE (lvalue), 0));
12230
12231           /* 15.25.2.b: Left hand is an array access. FIXME */
12232         }
12233
12234       /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12235          function to complete this RHS. Note that a NEW_ARRAY_INIT
12236          might have been already fully expanded if created as a result
12237          of processing an anonymous array initializer. We avoid doing
12238          the operation twice by testing whether the node already bears
12239          a type. */
12240       else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12241         nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12242                                    TREE_OPERAND (node, 1));
12243       /* Otherwise we simply complete the RHS */
12244       else
12245         nn = java_complete_tree (TREE_OPERAND (node, 1));
12246
12247       if (nn == error_mark_node)
12248         return error_mark_node;
12249
12250       /* Write back the RHS as we evaluated it. */
12251       TREE_OPERAND (node, 1) = nn;
12252
12253       /* In case we're handling = with a String as a RHS, we need to
12254          produce a String out of the RHS (it might still be a
12255          STRING_CST or a StringBuffer at this stage */
12256       if ((nn = patch_string (TREE_OPERAND (node, 1))))
12257         TREE_OPERAND (node, 1) = nn;
12258
12259       if ((nn = nested_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12260                                          TREE_OPERAND (node, 1))))
12261         {
12262           /* We return error_mark_node if nested_field_access_fix
12263              detects we write into a final. */
12264           if (nn == error_mark_node)
12265             return error_mark_node;
12266           node = nn;
12267         }
12268       else
12269         {
12270           node = patch_assignment (node, wfl_op1);
12271           if (node == error_mark_node)
12272             return error_mark_node;
12273           /* Reorganize the tree if necessary. */
12274           if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12275                        || JSTRING_P (TREE_TYPE (node))))
12276             node = java_refold (node);
12277         }
12278
12279       /* Seek to set DECL_INITIAL to a proper value, since it might have
12280          undergone a conversion in patch_assignment. We do that only when
12281          it's necessary to have DECL_INITIAL properly set. */
12282       nn = TREE_OPERAND (node, 0);
12283       if (TREE_CODE (nn) == VAR_DECL
12284           && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12285           && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12286           && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12287               || TREE_TYPE (nn) == string_ptr_type_node))
12288         DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12289
12290       CAN_COMPLETE_NORMALLY (node) = 1;
12291       return node;
12292
12293     case MULT_EXPR:
12294     case PLUS_EXPR:
12295     case MINUS_EXPR:
12296     case LSHIFT_EXPR:
12297     case RSHIFT_EXPR:
12298     case URSHIFT_EXPR:
12299     case BIT_AND_EXPR:
12300     case BIT_XOR_EXPR:
12301     case BIT_IOR_EXPR:
12302     case TRUNC_MOD_EXPR:
12303     case TRUNC_DIV_EXPR:
12304     case RDIV_EXPR:
12305     case TRUTH_ANDIF_EXPR:
12306     case TRUTH_ORIF_EXPR:
12307     case EQ_EXPR:
12308     case NE_EXPR:
12309     case GT_EXPR:
12310     case GE_EXPR:
12311     case LT_EXPR:
12312     case LE_EXPR:
12313       /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12314          knows how to handle those cases. */
12315       wfl_op1 = TREE_OPERAND (node, 0);
12316       wfl_op2 = TREE_OPERAND (node, 1);
12317
12318       CAN_COMPLETE_NORMALLY (node) = 1;
12319       /* Don't complete string nodes if dealing with the PLUS operand. */
12320       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12321         {
12322           nn = java_complete_tree (wfl_op1);
12323           if (nn == error_mark_node)
12324             return error_mark_node;
12325
12326           TREE_OPERAND (node, 0) = nn;
12327         }
12328       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12329         {
12330           nn = java_complete_tree (wfl_op2);
12331           if (nn == error_mark_node)
12332             return error_mark_node;
12333
12334           TREE_OPERAND (node, 1) = nn;
12335         }
12336       return patch_binop (node, wfl_op1, wfl_op2, 0);
12337
12338     case INSTANCEOF_EXPR:
12339       wfl_op1 = TREE_OPERAND (node, 0);
12340       COMPLETE_CHECK_OP_0 (node);
12341       return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1), 0);
12342
12343     case UNARY_PLUS_EXPR:
12344     case NEGATE_EXPR:
12345     case TRUTH_NOT_EXPR:
12346     case BIT_NOT_EXPR:
12347     case PREDECREMENT_EXPR:
12348     case PREINCREMENT_EXPR:
12349     case POSTDECREMENT_EXPR:
12350     case POSTINCREMENT_EXPR:
12351     case CONVERT_EXPR:
12352       /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12353          how to handle those cases. */
12354       wfl_op1 = TREE_OPERAND (node, 0);
12355       CAN_COMPLETE_NORMALLY (node) = 1;
12356       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12357       if (TREE_OPERAND (node, 0) == error_mark_node)
12358         return error_mark_node;
12359       node = patch_unaryop (node, wfl_op1);
12360       CAN_COMPLETE_NORMALLY (node) = 1;
12361       break;
12362
12363     case ARRAY_REF:
12364       /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12365          how to handle those cases. */
12366       wfl_op1 = TREE_OPERAND (node, 0);
12367       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12368       if (TREE_OPERAND (node, 0) == error_mark_node)
12369         return error_mark_node;
12370       if (!flag_emit_class_files)
12371         TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12372       /* The same applies to wfl_op2 */
12373       wfl_op2 = TREE_OPERAND (node, 1);
12374       TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12375       if (TREE_OPERAND (node, 1) == error_mark_node)
12376         return error_mark_node;
12377       if (!flag_emit_class_files)
12378         TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12379       return patch_array_ref (node);
12380
12381     case RECORD_TYPE:
12382       return node;;
12383
12384     case COMPONENT_REF:
12385       /* The first step in the re-write of qualified name handling.  FIXME.
12386          So far, this is only to support PRIMTYPE.class ->
12387          PRIMCLASS.TYPE. */
12388       {
12389         tree prim_class = TREE_OPERAND (node, 0);
12390         tree name = TREE_OPERAND (node, 1);
12391         tree field;
12392         
12393         gcc_assert (TREE_CODE (prim_class) == NOP_EXPR);
12394         prim_class = java_complete_tree (TREE_TYPE (prim_class));
12395         gcc_assert (TREE_CODE (prim_class) == RECORD_TYPE);
12396         field = lookup_field_wrapper (prim_class, name);
12397         
12398         if (field == NULL_TREE)
12399           {
12400             error ("missing static field %qs", IDENTIFIER_POINTER (name));
12401             return error_mark_node;
12402           }
12403         if (! FIELD_STATIC (field))
12404           {
12405             error ("not a static field %qs", IDENTIFIER_POINTER (name));
12406             return error_mark_node;
12407           }
12408         return field;
12409       }
12410       break;
12411
12412     case THIS_EXPR:
12413       /* Can't use THIS in a static environment */
12414       if (!current_this)
12415         {
12416           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12417           parse_error_context (wfl_operator,
12418                                "Keyword %<this%> used outside allowed context");
12419           TREE_TYPE (node) = error_mark_node;
12420           return error_mark_node;
12421         }
12422       if (ctxp->explicit_constructor_p)
12423         {
12424           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12425           parse_error_context
12426             (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12427           TREE_TYPE (node) = error_mark_node;
12428           return error_mark_node;
12429         }
12430       return current_this;
12431
12432     case CLASS_LITERAL:
12433       CAN_COMPLETE_NORMALLY (node) = 1;
12434       node = patch_incomplete_class_ref (node);
12435       if (node == error_mark_node)
12436         return error_mark_node;
12437       break;
12438
12439     default:
12440       CAN_COMPLETE_NORMALLY (node) = 1;
12441       /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12442          and it's time to turn it into the appropriate String object */
12443       if ((nn = patch_string (node)))
12444         node = nn;
12445       else
12446         internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12447     }
12448   return node;
12449 }
12450
12451 /* Complete function call's argument. Return a nonzero value is an
12452    error was found.  */
12453
12454 static int
12455 complete_function_arguments (tree node)
12456 {
12457   int flag = 0;
12458   tree cn;
12459
12460   ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12461   for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12462     {
12463       tree wfl = TREE_VALUE (cn), parm, temp;
12464       parm = java_complete_tree (wfl);
12465
12466       if (parm == error_mark_node)
12467         {
12468           flag = 1;
12469           continue;
12470         }
12471       /* If we have a string literal that we haven't transformed yet or a
12472          crafted string buffer, as a result of the use of the String
12473          `+' operator. Build `parm.toString()' and expand it. */
12474       if ((temp = patch_string (parm)))
12475         parm = temp;
12476
12477       TREE_VALUE (cn) = parm;
12478     }
12479   ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12480   return flag;
12481 }
12482
12483 /* Sometimes (for loops and variable initialized during their
12484    declaration), we want to wrap a statement around a WFL and turn it
12485    debugable.  */
12486
12487 static tree
12488 build_debugable_stmt (int location, tree stmt)
12489 {
12490   if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12491     {
12492 #ifdef USE_MAPPED_LOCATION
12493       stmt = expr_add_location (stmt, location, 1);
12494 #else
12495       stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12496       EXPR_WFL_LINECOL (stmt) = location;
12497       JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12498 #endif
12499     }
12500   return stmt;
12501 }
12502
12503 static tree
12504 build_expr_block (tree body, tree decls)
12505 {
12506   tree node = make_node (BLOCK);
12507   BLOCK_EXPR_DECLS (node) = decls;
12508   BLOCK_EXPR_BODY (node) = body;
12509   if (body)
12510     TREE_TYPE (node) = TREE_TYPE (body);
12511   TREE_SIDE_EFFECTS (node) = 1;
12512   return node;
12513 }
12514
12515 /* Create a new function block and link it appropriately to current
12516    function block chain */
12517
12518 static tree
12519 enter_block (void)
12520 {
12521   tree b = build_expr_block (NULL_TREE, NULL_TREE);
12522
12523   /* Link block B supercontext to the previous block. The current
12524      function DECL is used as supercontext when enter_a_block is called
12525      for the first time for a given function. The current function body
12526      (DECL_FUNCTION_BODY) is set to be block B.  */
12527
12528   tree fndecl = current_function_decl;
12529
12530   if (!fndecl) {
12531     BLOCK_SUPERCONTEXT (b) = current_static_block;
12532     current_static_block = b;
12533   }
12534
12535   else if (!DECL_FUNCTION_BODY (fndecl))
12536     {
12537       BLOCK_SUPERCONTEXT (b) = fndecl;
12538       DECL_FUNCTION_BODY (fndecl) = b;
12539     }
12540   else
12541     {
12542       BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12543       DECL_FUNCTION_BODY (fndecl) = b;
12544     }
12545   return b;
12546 }
12547
12548 /* Exit a block by changing the current function body
12549    (DECL_FUNCTION_BODY) to the current block super context, only if
12550    the block being exited isn't the method's top level one.  */
12551
12552 static tree
12553 exit_block (void)
12554 {
12555   tree b;
12556   if (current_function_decl)
12557     {
12558       b = DECL_FUNCTION_BODY (current_function_decl);
12559       if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12560         DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12561     }
12562   else
12563     {
12564       b = current_static_block;
12565
12566       if (BLOCK_SUPERCONTEXT (b))
12567         current_static_block = BLOCK_SUPERCONTEXT (b);
12568     }
12569   return b;
12570 }
12571
12572 /* Lookup for NAME in the nested function's blocks, all the way up to
12573    the current toplevel one. It complies with Java's local variable
12574    scoping rules.  */
12575
12576 static tree
12577 lookup_name_in_blocks (tree name)
12578 {
12579   tree b = GET_CURRENT_BLOCK (current_function_decl);
12580
12581   while (b != current_function_decl)
12582     {
12583       tree current;
12584
12585       /* Paranoid sanity check. To be removed */
12586       if (TREE_CODE (b) != BLOCK)
12587         abort ();
12588
12589       for (current = BLOCK_EXPR_DECLS (b); current;
12590            current = TREE_CHAIN (current))
12591         if (DECL_NAME (current) == name)
12592           return current;
12593       b = BLOCK_SUPERCONTEXT (b);
12594     }
12595   return NULL_TREE;
12596 }
12597
12598 static void
12599 maybe_absorb_scoping_blocks (void)
12600 {
12601   while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12602     {
12603       tree b = exit_block ();
12604       java_method_add_stmt (current_function_decl, b);
12605       SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12606     }
12607 }
12608
12609 \f
12610 /* This section of the source is reserved to build_* functions that
12611    are building incomplete tree nodes and the patch_* functions that
12612    are completing them.  */
12613
12614 /* Wrap a non WFL node around a WFL.  */
12615
12616 static tree
12617 build_wfl_wrap (tree node, int location)
12618 {
12619   tree wfl, node_to_insert = node;
12620
12621   /* We want to process THIS . xxx symbolically, to keep it consistent
12622      with the way we're processing SUPER. A THIS from a primary as a
12623      different form than a SUPER. Turn THIS into something symbolic */
12624   if (TREE_CODE (node) == THIS_EXPR)
12625     node_to_insert = wfl = build_wfl_node (this_identifier_node);
12626   else
12627 #ifdef USE_MAPPED_LOCATION
12628     wfl = build_unknown_wfl (NULL_TREE);
12629
12630   SET_EXPR_LOCATION (wfl, location);
12631 #else
12632     wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12633
12634   EXPR_WFL_LINECOL (wfl) = location;
12635 #endif
12636   EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12637   return wfl;
12638 }
12639
12640 /* Build a super() constructor invocation. Returns an empty statement if
12641    we're currently dealing with the class java.lang.Object. */
12642
12643 static tree
12644 build_super_invocation (tree mdecl)
12645 {
12646   if (DECL_CONTEXT (mdecl) == object_type_node)
12647     return build_java_empty_stmt ();
12648   else
12649     {
12650       tree super_wfl = build_wfl_node (super_identifier_node);
12651       tree a = NULL_TREE, t;
12652
12653       /* This is called after parsing is done, so the parser context
12654          won't be accurate. Set location info from current_class decl. */
12655       tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12656       EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12657
12658       /* If we're dealing with an anonymous class, pass the arguments
12659          of the crafted constructor along. */
12660       if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12661         {
12662           SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12663           for (; t != end_params_node; t = TREE_CHAIN (t))
12664             a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12665         }
12666       return build_method_invocation (super_wfl, a);
12667     }
12668 }
12669
12670 /* Build a SUPER/THIS qualified method invocation.  */
12671
12672 static tree
12673 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12674                                        int lloc, int rloc)
12675 {
12676   tree invok;
12677   tree wfl =
12678     build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12679   EXPR_WFL_LINECOL (wfl) = lloc;
12680   invok = build_method_invocation (name, args);
12681   return make_qualified_primary (wfl, invok, rloc);
12682 }
12683
12684 /* Build an incomplete CALL_EXPR node. */
12685
12686 static tree
12687 build_method_invocation (tree name, tree args)
12688 {
12689   tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12690   TREE_SIDE_EFFECTS (call) = 1;
12691   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12692   return call;
12693 }
12694
12695 /* Build an incomplete new xxx(...) node. */
12696
12697 static tree
12698 build_new_invocation (tree name, tree args)
12699 {
12700   tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12701   TREE_SIDE_EFFECTS (call) = 1;
12702   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12703   return call;
12704 }
12705
12706 /* Build an incomplete assignment expression. */
12707
12708 static tree
12709 build_assignment (int op, int op_location, tree lhs, tree rhs)
12710 {
12711   tree assignment;
12712   /* Build the corresponding binop if we deal with a Compound
12713      Assignment operator. Mark the binop sub-tree as part of a
12714      Compound Assignment expression */
12715   if (op != ASSIGN_TK)
12716     {
12717       rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12718       COMPOUND_ASSIGN_P (rhs) = 1;
12719     }
12720   assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12721   TREE_SIDE_EFFECTS (assignment) = 1;
12722   EXPR_WFL_LINECOL (assignment) = op_location;
12723   return assignment;
12724 }
12725
12726 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12727    the buffer.  This is used only for string conversion.  */
12728 static char *
12729 string_convert_int_cst (tree node)
12730 {
12731   /* Long.MIN_VALUE is -9223372036854775808, 20 characters.  */
12732   static char buffer[21];
12733
12734   unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12735   unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12736   char *p = buffer + sizeof (buffer);
12737   int neg = 0;
12738
12739   unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12740                                   << (HOST_BITS_PER_WIDE_INT - 1));
12741
12742   *--p = '\0';
12743
12744   /* If negative, note the fact and negate the value.  */
12745   if ((hi & hibit))
12746     {
12747       lo = ~lo;
12748       hi = ~hi;
12749       if (++lo == 0)
12750         ++hi;
12751       neg = 1;
12752     }
12753
12754   /* Divide by 10 until there are no bits left.  */
12755   do
12756     {
12757       unsigned HOST_WIDE_INT acc = 0;
12758       unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12759       unsigned int i;
12760
12761       /* Use long division to compute the result and the remainder.  */
12762       for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12763         {
12764           /* Shift a bit into accumulator.  */
12765           acc <<= 1;
12766           if ((hi & hibit))
12767             acc |= 1;
12768
12769           /* Shift the value.  */
12770           hi <<= 1;
12771           if ((lo & hibit))
12772             hi |= 1;
12773           lo <<= 1;
12774
12775           /* Shift the correct bit into the result.  */
12776           outhi <<= 1;
12777           if ((outlo & hibit))
12778             outhi |= 1;
12779           outlo <<= 1;
12780           if (acc >= 10)
12781             {
12782               acc -= 10;
12783               outlo |= 1;
12784             }
12785         }
12786
12787       /* '0' == 060 in Java, but might not be here (think EBCDIC).  */
12788       *--p = '\060' + acc;
12789
12790       hi = outhi;
12791       lo = outlo;
12792     }
12793   while (hi || lo);
12794
12795   if (neg)
12796     *--p = '\055'; /* '-' == 055 in Java, but might not be here.  */
12797
12798   return p;
12799 }
12800
12801 /* Print an INTEGER_CST node in a static buffer, and return the
12802    buffer.  This is used only for error handling.  */
12803 char *
12804 print_int_node (tree node)
12805 {
12806   static char buffer [80];
12807   if (TREE_CONSTANT_OVERFLOW (node))
12808     sprintf (buffer, "<overflow>");
12809
12810   if (TREE_INT_CST_HIGH (node) == 0)
12811     sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12812              TREE_INT_CST_LOW (node));
12813   else if (TREE_INT_CST_HIGH (node) == -1
12814            && TREE_INT_CST_LOW (node) != 0)
12815     sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12816              -TREE_INT_CST_LOW (node));
12817   else
12818     sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12819              TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12820
12821   return buffer;
12822 }
12823
12824 \f
12825 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12826    context.  */
12827
12828 /* 15.25 Assignment operators. */
12829
12830 static tree
12831 patch_assignment (tree node, tree wfl_op1)
12832 {
12833   tree rhs = TREE_OPERAND (node, 1);
12834   tree lvalue = TREE_OPERAND (node, 0), llvalue;
12835   tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12836   int error_found = 0;
12837   int lvalue_from_array = 0;
12838   int is_return = 0;
12839
12840   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12841
12842   /* Lhs can be a named variable */
12843   if (JDECL_P (lvalue))
12844     {
12845       lhs_type = TREE_TYPE (lvalue);
12846     }
12847   /* Or Lhs can be an array access. */
12848   else if (TREE_CODE (lvalue) == ARRAY_REF)
12849     {
12850       lhs_type = TREE_TYPE (lvalue);
12851       lvalue_from_array = 1;
12852     }
12853   /* Or a field access */
12854   else if (TREE_CODE (lvalue) == COMPONENT_REF)
12855     lhs_type = TREE_TYPE (lvalue);
12856   /* Or a function return slot */
12857   else if (TREE_CODE (lvalue) == RESULT_DECL)
12858     {
12859       /* If the return type is an integral type, then we create the
12860          RESULT_DECL with a promoted type, but we need to do these
12861          checks against the unpromoted type to ensure type safety.  So
12862          here we look at the real type, not the type of the decl we
12863          are modifying.  */
12864       lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12865       is_return = 1;
12866     }
12867   /* Otherwise, we might want to try to write into an optimized static
12868      final, this is an of a different nature, reported further on. */
12869   else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12870            && resolve_expression_name (wfl_op1, &llvalue))
12871     {
12872       lhs_type = TREE_TYPE (lvalue);
12873     }
12874   else
12875     {
12876       parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12877       error_found = 1;
12878     }
12879
12880   rhs_type = TREE_TYPE (rhs);
12881
12882   /* 5.1 Try the assignment conversion for builtin type. */
12883   new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12884
12885   /* 5.2 If it failed, try a reference conversion */
12886   if (!new_rhs)
12887     new_rhs = try_reference_assignconv (lhs_type, rhs);
12888
12889   /* 15.25.2 If we have a compound assignment, convert RHS into the
12890      type of the LHS */
12891   else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12892     new_rhs = convert (lhs_type, rhs);
12893
12894   /* Explicit cast required. This is an error */
12895   if (!new_rhs)
12896     {
12897       char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12898       char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12899       tree wfl;
12900       char operation [32];      /* Max size known */
12901
12902       /* If the assignment is part of a declaration, we use the WFL of
12903          the declared variable to point out the error and call it a
12904          declaration problem. If the assignment is a genuine =
12905          operator, we call is a operator `=' problem, otherwise we
12906          call it an assignment problem. In both of these last cases,
12907          we use the WFL of the operator to indicate the error. */
12908
12909       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12910         {
12911           wfl = wfl_op1;
12912           strcpy (operation, "declaration");
12913         }
12914       else
12915         {
12916           wfl = wfl_operator;
12917           if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12918             strcpy (operation, "assignment");
12919           else if (is_return)
12920             strcpy (operation, "'return'");
12921           else
12922             strcpy (operation, "'='");
12923         }
12924
12925       if (!valid_cast_to_p (rhs_type, lhs_type))
12926         parse_error_context
12927           (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12928            operation, t1, t2);
12929       else
12930         parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12931                              operation, t1, t2);
12932       free (t1); free (t2);
12933       error_found = 1;
12934     }
12935
12936   if (error_found)
12937     return error_mark_node;
12938
12939   /* If we're processing a `return' statement, promote the actual type
12940      to the promoted type.  */
12941   if (is_return)
12942     new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12943
12944   /* 10.10: Array Store Exception runtime check */
12945   if (!flag_emit_class_files
12946       && lvalue_from_array
12947       && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12948     {
12949       tree array, store_check, base, index_expr;
12950
12951       /* Save RHS so that it doesn't get re-evaluated by the store check. */
12952       new_rhs = save_expr (new_rhs);
12953
12954       /* Get the INDIRECT_REF. */
12955       array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12956       /* Get the array pointer expr. */
12957       array = TREE_OPERAND (array, 0);
12958       store_check = build_java_arraystore_check (array, new_rhs);
12959
12960       index_expr = TREE_OPERAND (lvalue, 1);
12961
12962       if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12963         {
12964           /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12965              happen before the store check, so prepare to insert the store
12966              check within the second operand of the existing COMPOUND_EXPR. */
12967           base = index_expr;
12968         }
12969       else
12970         base = lvalue;
12971
12972       index_expr = TREE_OPERAND (base, 1);
12973       TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12974                                        store_check, index_expr);
12975     }
12976
12977   /* Final locals can be used as case values in switch
12978      statement. Prepare them for this eventuality. */
12979   if (TREE_CODE (lvalue) == VAR_DECL
12980       && DECL_FINAL (lvalue)
12981       && TREE_CONSTANT (new_rhs)
12982       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12983       && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12984       )
12985     {
12986       TREE_CONSTANT (lvalue) = 1;
12987       TREE_INVARIANT (lvalue) = 1;
12988       DECL_INITIAL (lvalue) = new_rhs;
12989     }
12990
12991   /* Copy the rhs if it's a reference.  */
12992   if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12993     {
12994       switch (TREE_CODE (new_rhs))
12995         {
12996         case ARRAY_REF:
12997         case INDIRECT_REF:
12998         case COMPONENT_REF:
12999           /* Transform a = foo.bar 
13000              into a = ({int tmp; tmp = foo.bar;}).
13001              We need to ensure that if a read from memory fails
13002              because of a NullPointerException, a destination variable
13003              will remain unchanged.  An explicit temporary does what
13004              we need.  
13005
13006              If flag_check_references is set, this is unnecessary
13007              because we'll check each reference before doing any
13008              reads.  If optimize is not set the result will never be
13009              written to a stack slot that contains the LHS.  */
13010           {
13011             tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"), 
13012                                    TREE_TYPE (new_rhs));
13013             tree block = make_node (BLOCK);
13014             tree assignment 
13015               = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
13016             DECL_CONTEXT (tmp) = current_function_decl;
13017             TREE_TYPE (block) = TREE_TYPE (new_rhs);
13018             BLOCK_VARS (block) = tmp;
13019             BLOCK_EXPR_BODY (block) = assignment;
13020             TREE_SIDE_EFFECTS (block) = 1;
13021             new_rhs = block;
13022           }
13023           break;
13024         default:
13025           break;
13026         }
13027     }
13028
13029   TREE_OPERAND (node, 0) = lvalue;
13030   TREE_OPERAND (node, 1) = new_rhs;
13031   TREE_TYPE (node) = lhs_type;
13032   return node;
13033 }
13034
13035 /* Check that type SOURCE can be cast into type DEST. If the cast
13036    can't occur at all, return NULL; otherwise, return a possibly
13037    modified rhs.  */
13038
13039 static tree
13040 try_reference_assignconv (tree lhs_type, tree rhs)
13041 {
13042   tree new_rhs = NULL_TREE;
13043   tree rhs_type = TREE_TYPE (rhs);
13044
13045   if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13046     {
13047       /* `null' may be assigned to any reference type */
13048       if (rhs == null_pointer_node)
13049         new_rhs = null_pointer_node;
13050       /* Try the reference assignment conversion */
13051       else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13052         new_rhs = rhs;
13053       /* This is a magic assignment that we process differently */
13054       else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13055         new_rhs = rhs;
13056     }
13057   return new_rhs;
13058 }
13059
13060 /* Check that RHS can be converted into LHS_TYPE by the assignment
13061    conversion (5.2), for the cases of RHS being a builtin type. Return
13062    NULL_TREE if the conversion fails or if because RHS isn't of a
13063    builtin type. Return a converted RHS if the conversion is possible.  */
13064
13065 static tree
13066 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13067 {
13068   tree new_rhs = NULL_TREE;
13069   tree rhs_type = TREE_TYPE (rhs);
13070
13071   /* Handle boolean specially.  */
13072   if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13073       || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13074     {
13075       if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13076           && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13077         new_rhs = rhs;
13078     }
13079
13080   /* 5.1.1 Try Identity Conversion,
13081      5.1.2 Try Widening Primitive Conversion */
13082   else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13083     new_rhs = convert (lhs_type, rhs);
13084
13085   /* Try a narrowing primitive conversion (5.1.3):
13086        - expression is a constant expression of type byte, short, char,
13087          or int, AND
13088        - variable is byte, short or char AND
13089        - The value of the expression is representable in the type of the
13090          variable */
13091   else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13092             || rhs_type == char_type_node || rhs_type == int_type_node)
13093             && TREE_CONSTANT (rhs)
13094            && (lhs_type == byte_type_node || lhs_type == char_type_node
13095                || lhs_type == short_type_node))
13096     {
13097       if (int_fits_type_p (rhs, lhs_type))
13098         new_rhs = convert (lhs_type, rhs);
13099       else if (wfl_op1)         /* Might be called with a NULL */
13100         parse_warning_context
13101           (wfl_op1,
13102            "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13103            print_int_node (rhs), lang_printable_name (lhs_type, 0));
13104       /* Reported a warning that will turn into an error further
13105          down, so we don't return */
13106     }
13107
13108   return new_rhs;
13109 }
13110
13111 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13112    conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
13113    0 is the conversion test fails.  This implements parts the method
13114    invocation conversion (5.3).  */
13115
13116 static int
13117 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13118 {
13119   /* 5.1.1: This is the identity conversion part. */
13120   if (lhs_type == rhs_type)
13121     return 1;
13122
13123   /* Reject non primitive types and boolean conversions.  */
13124   if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13125     return 0;
13126
13127   /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13128      than a char can't be converted into a char. Short can't too, but
13129      the < test below takes care of that */
13130   if (lhs_type == char_type_node && rhs_type == byte_type_node)
13131     return 0;
13132
13133   /* Accept all promoted type here. Note, we can't use <= in the test
13134      below, because we still need to bounce out assignments of short
13135      to char and the likes */
13136   if (lhs_type == int_type_node
13137       && (rhs_type == promoted_byte_type_node
13138           || rhs_type == promoted_short_type_node
13139           || rhs_type == promoted_char_type_node
13140           || rhs_type == promoted_boolean_type_node))
13141     return 1;
13142
13143   /* From here, an integral is widened if its precision is smaller
13144      than the precision of the LHS or if the LHS is a floating point
13145      type, or the RHS is a float and the RHS a double. */
13146   if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13147        && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13148       || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13149       || (rhs_type == float_type_node && lhs_type == double_type_node))
13150     return 1;
13151
13152   return 0;
13153 }
13154
13155 /* Check that something of SOURCE type can be assigned or cast to
13156    something of DEST type at runtime. Return 1 if the operation is
13157    valid, 0 otherwise. If CAST is set to 1, we're treating the case
13158    were SOURCE is cast into DEST, which borrows a lot of the
13159    assignment check. */
13160
13161 static int
13162 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13163 {
13164   /* SOURCE or DEST might be null if not from a declared entity. */
13165   if (!source || !dest)
13166     return 0;
13167   if (JNULLP_TYPE_P (source))
13168     return 1;
13169   if (TREE_CODE (source) == POINTER_TYPE)
13170     source = TREE_TYPE (source);
13171   if (TREE_CODE (dest) == POINTER_TYPE)
13172     dest = TREE_TYPE (dest);
13173
13174   /* If source and dest are being compiled from bytecode, they may need to
13175      be loaded. */
13176   if (CLASS_P (source) && !CLASS_LOADED_P (source))
13177     {
13178       load_class (source, 1);
13179       safe_layout_class (source);
13180     }
13181   if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13182     {
13183       load_class (dest, 1);
13184       safe_layout_class (dest);
13185     }
13186
13187   /* Case where SOURCE is a class type */
13188   if (TYPE_CLASS_P (source))
13189     {
13190       if (TYPE_CLASS_P (dest))
13191         return  (source == dest
13192                  || inherits_from_p (source, dest)
13193                  || (cast && inherits_from_p (dest, source)));
13194       if (TYPE_INTERFACE_P (dest))
13195         {
13196           /* If doing a cast and SOURCE is final, the operation is
13197              always correct a compile time (because even if SOURCE
13198              does not implement DEST, a subclass of SOURCE might). */
13199           if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13200             return 1;
13201           /* Otherwise, SOURCE must implement DEST */
13202           return interface_of_p (dest, source);
13203         }
13204       /* DEST is an array, cast permitted if SOURCE is of Object type */
13205       return (cast && source == object_type_node ? 1 : 0);
13206     }
13207   if (TYPE_INTERFACE_P (source))
13208     {
13209       if (TYPE_CLASS_P (dest))
13210         {
13211           /* If not casting, DEST must be the Object type */
13212           if (!cast)
13213             return dest == object_type_node;
13214           /* We're doing a cast. The cast is always valid is class
13215              DEST is not final, otherwise, DEST must implement SOURCE */
13216           else if (!CLASS_FINAL (TYPE_NAME (dest)))
13217             return 1;
13218           else
13219             return interface_of_p (source, dest);
13220         }
13221       if (TYPE_INTERFACE_P (dest))
13222         {
13223           /* If doing a cast, then if SOURCE and DEST contain method
13224              with the same signature but different return type, then
13225              this is a (compile time) error */
13226           if (cast)
13227             {
13228               tree method_source, method_dest;
13229               tree source_type;
13230               tree source_sig;
13231               tree source_name;
13232               for (method_source = TYPE_METHODS (source); method_source;
13233                    method_source = TREE_CHAIN (method_source))
13234                 {
13235                   source_sig =
13236                     build_java_argument_signature (TREE_TYPE (method_source));
13237                   source_type = TREE_TYPE (TREE_TYPE (method_source));
13238                   source_name = DECL_NAME (method_source);
13239                   for (method_dest = TYPE_METHODS (dest);
13240                        method_dest; method_dest = TREE_CHAIN (method_dest))
13241                     if (source_sig ==
13242                         build_java_argument_signature (TREE_TYPE (method_dest))
13243                         && source_name == DECL_NAME (method_dest)
13244                         && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13245                       return 0;
13246                 }
13247               return 1;
13248             }
13249           else
13250             return source == dest || interface_of_p (dest, source);
13251         }
13252       else
13253         {
13254           /* Array */
13255           return (cast
13256                   && (DECL_NAME (TYPE_NAME (source))
13257                       == java_lang_cloneable_identifier_node
13258                       || (DECL_NAME (TYPE_NAME (source))
13259                           == java_io_serializable_identifier_node)));
13260         }
13261     }
13262   if (TYPE_ARRAY_P (source))
13263     {
13264       if (TYPE_CLASS_P (dest))
13265         return dest == object_type_node;
13266       /* Can't cast an array to an interface unless the interface is
13267          java.lang.Cloneable or java.io.Serializable.  */
13268       if (TYPE_INTERFACE_P (dest))
13269         return (DECL_NAME (TYPE_NAME (dest))
13270                 == java_lang_cloneable_identifier_node
13271                 || (DECL_NAME (TYPE_NAME (dest))
13272                     == java_io_serializable_identifier_node));
13273       else                      /* Arrays */
13274         {
13275           tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13276           tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13277
13278           /* In case of severe errors, they turn out null */
13279           if (!dest_element_type || !source_element_type)
13280             return 0;
13281           if (source_element_type == dest_element_type)
13282             return 1;
13283           return valid_ref_assignconv_cast_p (source_element_type,
13284                                               dest_element_type, cast);
13285         }
13286       return 0;
13287     }
13288   return 0;
13289 }
13290
13291 static int
13292 valid_cast_to_p (tree source, tree dest)
13293 {
13294   if (TREE_CODE (source) == POINTER_TYPE)
13295     source = TREE_TYPE (source);
13296   if (TREE_CODE (dest) == POINTER_TYPE)
13297     dest = TREE_TYPE (dest);
13298
13299   if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13300     return valid_ref_assignconv_cast_p (source, dest, 1);
13301
13302   else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13303     return 1;
13304
13305   else if (TREE_CODE (source) == BOOLEAN_TYPE
13306            && TREE_CODE (dest) == BOOLEAN_TYPE)
13307     return 1;
13308
13309   return 0;
13310 }
13311
13312 static tree
13313 do_unary_numeric_promotion (tree arg)
13314 {
13315   tree type = TREE_TYPE (arg);
13316   if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13317       || TREE_CODE (type) == CHAR_TYPE)
13318     arg = convert (int_type_node, arg);
13319   return arg;
13320 }
13321
13322 /* Return a nonzero value if SOURCE can be converted into DEST using
13323    the method invocation conversion rule (5.3).  */
13324 static int
13325 valid_method_invocation_conversion_p (tree dest, tree source)
13326 {
13327   return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13328            && valid_builtin_assignconv_identity_widening_p (dest, source))
13329           || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13330               && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13331               && valid_ref_assignconv_cast_p (source, dest, 0)));
13332 }
13333
13334 /* Build an incomplete binop expression. */
13335
13336 static tree
13337 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13338 {
13339   tree binop = build2 (op, NULL_TREE, op1, op2);
13340   TREE_SIDE_EFFECTS (binop) = 1;
13341   /* Store the location of the operator, for better error report. The
13342      string of the operator will be rebuild based on the OP value. */
13343   EXPR_WFL_LINECOL (binop) = op_location;
13344   return binop;
13345 }
13346
13347 /* Build the string of the operator retained by NODE. If NODE is part
13348    of a compound expression, add an '=' at the end of the string. This
13349    function is called when an error needs to be reported on an
13350    operator. The string is returned as a pointer to a static character
13351    buffer. */
13352
13353 static char *
13354 operator_string (tree node)
13355 {
13356 #define BUILD_OPERATOR_STRING(S)                                        \
13357   {                                                                     \
13358     sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13359     return buffer;                                                      \
13360   }
13361
13362   static char buffer [10];
13363   switch (TREE_CODE (node))
13364     {
13365     case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13366     case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13367     case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13368     case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13369     case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13370     case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13371     case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13372     case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13373     case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13374     case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13375     case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13376     case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13377     case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13378     case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13379     case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13380     case GT_EXPR: BUILD_OPERATOR_STRING (">");
13381     case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13382     case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13383     case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13384     case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13385     case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13386     case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13387     case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13388     case PREINCREMENT_EXPR:     /* Fall through */
13389     case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13390     case PREDECREMENT_EXPR:     /* Fall through */
13391     case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13392     default:
13393       internal_error ("unregistered operator %s",
13394                       tree_code_name [TREE_CODE (node)]);
13395     }
13396   return NULL;
13397 #undef BUILD_OPERATOR_STRING
13398 }
13399
13400 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13401
13402 static int
13403 java_decl_equiv (tree var_acc1, tree var_acc2)
13404 {
13405   if (JDECL_P (var_acc1))
13406     return (var_acc1 == var_acc2);
13407
13408   return (TREE_CODE (var_acc1) == COMPONENT_REF
13409           && TREE_CODE (var_acc2) == COMPONENT_REF
13410           && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13411              == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13412           && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13413 }
13414
13415 /* Return a nonzero value if CODE is one of the operators that can be
13416    used in conjunction with the `=' operator in a compound assignment.  */
13417
13418 static int
13419 binop_compound_p (enum tree_code code)
13420 {
13421   int i;
13422   for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13423     if (binop_lookup [i] == code)
13424       break;
13425
13426   return i < BINOP_COMPOUND_CANDIDATES;
13427 }
13428
13429 /* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13430
13431 static tree
13432 java_refold (tree t)
13433 {
13434   tree c, b, ns, decl;
13435
13436   if (TREE_CODE (t) != MODIFY_EXPR)
13437     return t;
13438
13439   c = TREE_OPERAND (t, 1);
13440   if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13441          && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13442          && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13443     return t;
13444
13445   /* Now the left branch of the binary operator. */
13446   b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13447   if (! (b && TREE_CODE (b) == NOP_EXPR
13448          && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13449     return t;
13450
13451   ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13452   if (! (ns && TREE_CODE (ns) == NOP_EXPR
13453          && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13454     return t;
13455
13456   decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13457   if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13458       /* It's got to be the an equivalent decl */
13459       && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13460     {
13461       /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13462       TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13463       /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13464       TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13465       /* Change the right part of the BINOP_EXPR */
13466       TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13467     }
13468
13469   return t;
13470 }
13471
13472 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13473    errors but we modify NODE so that it contains the type computed
13474    according to the expression, when it's fixed. Otherwise, we write
13475    error_mark_node as the type. It allows us to further the analysis
13476    of remaining nodes and detects more errors in certain cases.  */
13477
13478 static tree
13479 patch_binop (tree node, tree wfl_op1, tree wfl_op2, int folding)
13480 {
13481   tree op1 = TREE_OPERAND (node, 0);
13482   tree op2 = TREE_OPERAND (node, 1);
13483   tree op1_type = TREE_TYPE (op1);
13484   tree op2_type = TREE_TYPE (op2);
13485   tree prom_type = NULL_TREE, cn;
13486   enum tree_code code = TREE_CODE (node);
13487
13488   /* If 1, tell the routine that we have to return error_mark_node
13489      after checking for the initialization of the RHS */
13490   int error_found = 0;
13491
13492   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13493
13494   /* If either op<n>_type are NULL, this might be early signs of an
13495      error situation, unless it's too early to tell (in case we're
13496      handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13497      correctly so the error can be later on reported accurately. */
13498   if (! (code == PLUS_EXPR || code == NE_EXPR
13499          || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13500     {
13501       tree n;
13502       if (! op1_type)
13503         {
13504           n = java_complete_tree (op1);
13505           op1_type = TREE_TYPE (n);
13506         }
13507       if (! op2_type)
13508         {
13509           n = java_complete_tree (op2);
13510           op2_type = TREE_TYPE (n);
13511         }
13512     }
13513
13514   switch (code)
13515     {
13516     /* 15.16 Multiplicative operators */
13517     case MULT_EXPR:             /* 15.16.1 Multiplication Operator * */
13518     case RDIV_EXPR:             /* 15.16.2 Division Operator / */
13519     case TRUNC_DIV_EXPR:        /* 15.16.2 Integral type Division Operator / */
13520     case TRUNC_MOD_EXPR:        /* 15.16.3 Remainder operator % */
13521       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13522         {
13523           if (!JNUMERIC_TYPE_P (op1_type))
13524             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13525           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13526             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13527           TREE_TYPE (node) = error_mark_node;
13528           error_found = 1;
13529           break;
13530         }
13531       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13532
13533       /* Detect integral division by zero */
13534       if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13535           && TREE_CODE (prom_type) == INTEGER_TYPE
13536           && (op2 == integer_zero_node || op2 == long_zero_node ||
13537               (TREE_CODE (op2) == INTEGER_CST &&
13538                ! TREE_INT_CST_LOW (op2)  && ! TREE_INT_CST_HIGH (op2))))
13539         {
13540           parse_warning_context
13541             (wfl_operator,
13542              "Evaluating this expression will result in an arithmetic exception being thrown");
13543           TREE_CONSTANT (node) = 0;
13544           TREE_INVARIANT (node) = 0;
13545         }
13546
13547       /* Change the division operator if necessary */
13548       if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13549         TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13550
13551       /* Before divisions as is disappear, try to simplify and bail if
13552          applicable, otherwise we won't perform even simple
13553          simplifications like (1-1)/3. We can't do that with floating
13554          point number, folds can't handle them at this stage. */
13555       if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13556           && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13557         {
13558           TREE_TYPE (node) = prom_type;
13559           node = fold (node);
13560           if (TREE_CODE (node) != code)
13561             return node;
13562         }
13563
13564       if (TREE_CODE (prom_type) == INTEGER_TYPE
13565           && flag_use_divide_subroutine
13566           && ! flag_emit_class_files
13567           && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13568         return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13569
13570       /* This one is more complicated. FLOATs are processed by a
13571          function call to soft_fmod. Duplicate the value of the
13572          COMPOUND_ASSIGN_P flag. */
13573       if (code == TRUNC_MOD_EXPR)
13574         {
13575           tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13576           COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13577           return mod;
13578         }
13579       break;
13580
13581     /* 15.17 Additive Operators */
13582     case PLUS_EXPR:             /* 15.17.1 String Concatenation Operator + */
13583
13584       /* Operation is valid if either one argument is a string
13585          constant, a String object or a StringBuffer crafted for the
13586          purpose of the a previous usage of the String concatenation
13587          operator */
13588
13589       if (TREE_CODE (op1) == STRING_CST
13590           || TREE_CODE (op2) == STRING_CST
13591           || JSTRING_TYPE_P (op1_type)
13592           || JSTRING_TYPE_P (op2_type)
13593           || IS_CRAFTED_STRING_BUFFER_P (op1)
13594           || IS_CRAFTED_STRING_BUFFER_P (op2))
13595         return build_string_concatenation (op1, op2);
13596
13597     case MINUS_EXPR:            /* 15.17.2 Additive Operators (+ and -) for
13598                                    Numeric Types */
13599       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13600         {
13601           if (!JNUMERIC_TYPE_P (op1_type))
13602             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13603           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13604             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13605           TREE_TYPE (node) = error_mark_node;
13606           error_found = 1;
13607           break;
13608         }
13609       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13610       break;
13611
13612     /* 15.18 Shift Operators */
13613     case LSHIFT_EXPR:
13614     case RSHIFT_EXPR:
13615     case URSHIFT_EXPR:
13616       if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13617         {
13618           if (!JINTEGRAL_TYPE_P (op1_type))
13619             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13620           else
13621             {
13622               if (JNUMERIC_TYPE_P (op2_type))
13623                 parse_error_context (wfl_operator,
13624                                      "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13625                                      operator_string (node),
13626                                      lang_printable_name (op2_type, 0));
13627               else
13628                 parse_error_context (wfl_operator,
13629                                      "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13630                                      operator_string (node),
13631                                      lang_printable_name (op2_type, 0));
13632             }
13633           TREE_TYPE (node) = error_mark_node;
13634           error_found = 1;
13635           break;
13636         }
13637
13638       /* Unary numeric promotion (5.6.1) is performed on each operand
13639          separately */
13640       op1 = do_unary_numeric_promotion (op1);
13641       op2 = do_unary_numeric_promotion (op2);
13642
13643       /* If the right hand side is of type `long', first cast it to
13644          `int'.  */
13645       if (TREE_TYPE (op2) == long_type_node)
13646         op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13647
13648       /* The type of the shift expression is the type of the promoted
13649          type of the left-hand operand */
13650       prom_type = TREE_TYPE (op1);
13651
13652       /* Shift int only up to 0x1f and long up to 0x3f */
13653       if (prom_type == int_type_node)
13654         op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13655                             build_int_cst (NULL_TREE, 0x1f)));
13656       else
13657         op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13658                             build_int_cst (NULL_TREE, 0x3f)));
13659
13660       /* The >>> operator is a >> operating on unsigned quantities */
13661       if (code == URSHIFT_EXPR && (folding || ! flag_emit_class_files))
13662         {
13663           tree to_return;
13664           tree utype = java_unsigned_type (prom_type);
13665           op1 = convert (utype, op1);
13666
13667           to_return = fold_build2 (RSHIFT_EXPR, utype, op1, op2);
13668           to_return = convert (prom_type, to_return);
13669           /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13670           COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13671           TREE_SIDE_EFFECTS (to_return)
13672             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13673           return to_return;
13674         }
13675       break;
13676
13677       /* 15.19.1 Type Comparison Operator instanceof */
13678     case INSTANCEOF_EXPR:
13679
13680       TREE_TYPE (node) = boolean_type_node;
13681
13682       /* OP1_TYPE might be NULL when OP1 is a string constant.  */
13683       if ((cn = patch_string (op1)))
13684         {
13685           op1 = cn;
13686           op1_type = TREE_TYPE (op1);
13687         }
13688       if (op1_type == NULL_TREE)
13689         abort ();
13690
13691       if (!(op2_type = resolve_type_during_patch (op2)))
13692         return error_mark_node;
13693
13694       /* The first operand must be a reference type or the null type */
13695       if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13696         error_found = 1;        /* Error reported further below */
13697
13698       /* The second operand must be a reference type */
13699       if (!JREFERENCE_TYPE_P (op2_type))
13700         {
13701           SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13702           parse_error_context
13703             (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13704              lang_printable_name (op2_type, 0));
13705           error_found = 1;
13706         }
13707
13708       if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13709         {
13710           /* If the first operand is null, the result is always false */
13711           if (op1 == null_pointer_node)
13712             return boolean_false_node;
13713           else if (flag_emit_class_files)
13714             {
13715               TREE_OPERAND (node, 1) = op2_type;
13716               TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13717               return node;
13718             }
13719           /* Otherwise we have to invoke instance of to figure it out */
13720           else
13721             return build_instanceof (op1, op2_type);
13722         }
13723       /* There is no way the expression operand can be an instance of
13724          the type operand. This is a compile time error. */
13725       else
13726         {
13727           char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13728           SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13729           parse_error_context
13730             (wfl_operator, "Impossible for %qs to be instance of %qs",
13731              t1, lang_printable_name (op2_type, 0));
13732           free (t1);
13733           error_found = 1;
13734         }
13735
13736       break;
13737
13738       /* 15.21 Bitwise and Logical Operators */
13739     case BIT_AND_EXPR:
13740     case BIT_XOR_EXPR:
13741     case BIT_IOR_EXPR:
13742       if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13743         /* Binary numeric promotion is performed on both operand and the
13744            expression retain that type */
13745         prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13746
13747       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13748                && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13749         /* The type of the bitwise operator expression is BOOLEAN */
13750         prom_type = boolean_type_node;
13751       else
13752         {
13753           if (!JINTEGRAL_TYPE_P (op1_type))
13754             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13755           if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13756             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13757           TREE_TYPE (node) = error_mark_node;
13758           error_found = 1;
13759           /* Insert a break here if adding thing before the switch's
13760              break for this case */
13761         }
13762       break;
13763
13764       /* 15.22 Conditional-And Operator */
13765     case TRUTH_ANDIF_EXPR:
13766       /* 15.23 Conditional-Or Operator */
13767     case TRUTH_ORIF_EXPR:
13768       /* Operands must be of BOOLEAN type */
13769       if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13770           TREE_CODE (op2_type) != BOOLEAN_TYPE)
13771         {
13772           if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13773             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13774           if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13775             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13776           TREE_TYPE (node) = boolean_type_node;
13777           error_found = 1;
13778           break;
13779         }
13780       else if (integer_zerop (op1))
13781         {
13782           return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13783         }
13784       else if (integer_onep (op1))
13785         {
13786           return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13787         }
13788       /* The type of the conditional operators is BOOLEAN */
13789       prom_type = boolean_type_node;
13790       break;
13791
13792       /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13793     case LT_EXPR:
13794     case GT_EXPR:
13795     case LE_EXPR:
13796     case GE_EXPR:
13797       /* The type of each of the operands must be a primitive numeric
13798          type */
13799       if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13800         {
13801           if (!JNUMERIC_TYPE_P (op1_type))
13802             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13803           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13804             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13805           TREE_TYPE (node) = boolean_type_node;
13806           error_found = 1;
13807           break;
13808         }
13809       /* Binary numeric promotion is performed on the operands */
13810       binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13811       /* The type of the relation expression is always BOOLEAN */
13812       prom_type = boolean_type_node;
13813       break;
13814
13815       /* 15.20 Equality Operator */
13816     case EQ_EXPR:
13817     case NE_EXPR:
13818       /* It's time for us to patch the strings. */
13819       if ((cn = patch_string (op1)))
13820        {
13821          op1 = cn;
13822          op1_type = TREE_TYPE (op1);
13823        }
13824       if ((cn = patch_string (op2)))
13825        {
13826          op2 = cn;
13827          op2_type = TREE_TYPE (op2);
13828        }
13829
13830       /* 15.20.1 Numerical Equality Operators == and != */
13831       /* Binary numeric promotion is performed on the operands */
13832       if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13833         binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13834
13835       /* 15.20.2 Boolean Equality Operators == and != */
13836       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13837           TREE_CODE (op2_type) == BOOLEAN_TYPE)
13838         ;                       /* Nothing to do here */
13839
13840       /* 15.20.3 Reference Equality Operators == and != */
13841       /* Types have to be either references or the null type. If
13842          they're references, it must be possible to convert either
13843          type to the other by casting conversion. */
13844       else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13845                || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13846                || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13847                || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13848                    && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13849                        || valid_ref_assignconv_cast_p (op2_type,
13850                                                        op1_type, 1))))
13851         ;                       /* Nothing to do here */
13852
13853       /* Else we have an error figure what can't be converted into
13854          what and report the error */
13855       else
13856         {
13857           char *t1;
13858           t1 = xstrdup (lang_printable_name (op1_type, 0));
13859           parse_error_context
13860             (wfl_operator,
13861              "Incompatible type for %qs. Can't convert %qs to %qs",
13862              operator_string (node), t1,
13863              lang_printable_name (op2_type, 0));
13864           free (t1);
13865           TREE_TYPE (node) = boolean_type_node;
13866           error_found = 1;
13867           break;
13868         }
13869       prom_type = boolean_type_node;
13870       break;
13871     default:
13872       abort ();
13873     }
13874
13875   if (error_found)
13876     return error_mark_node;
13877
13878   TREE_OPERAND (node, 0) = op1;
13879   TREE_OPERAND (node, 1) = op2;
13880   TREE_TYPE (node) = prom_type;
13881   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13882
13883   /* fold does not respect side-effect order as required for Java but not C.
13884    * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13885    * bytecode.
13886    */
13887   if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13888       : ! TREE_SIDE_EFFECTS (node))
13889     node = fold (node);
13890   return node;
13891 }
13892
13893 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13894    zero value, the value of CSTE comes after the valude of STRING */
13895
13896 static tree
13897 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13898 {
13899   const char *old = TREE_STRING_POINTER (cste);
13900   int old_len = TREE_STRING_LENGTH (cste);
13901   int len = old_len + string_len;
13902   char *new = alloca (len+1);
13903
13904   if (after)
13905     {
13906       memcpy (new, string, string_len);
13907       memcpy (&new [string_len], old, old_len);
13908     }
13909   else
13910     {
13911       memcpy (new, old, old_len);
13912       memcpy (&new [old_len], string, string_len);
13913     }
13914   new [len] = '\0';
13915   return build_string (len, new);
13916 }
13917
13918 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13919    new STRING_CST on success, NULL_TREE on failure.  */
13920
13921 static tree
13922 merge_string_cste (tree op1, tree op2, int after)
13923 {
13924   /* Handle two string constants right away.  */
13925   if (TREE_CODE (op2) == STRING_CST)
13926     return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13927                                  TREE_STRING_LENGTH (op2), after);
13928
13929   /* Reasonable integer constant can be treated right away.  */
13930   if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13931     {
13932       static const char *const boolean_true = "true";
13933       static const char *const boolean_false = "false";
13934       static const char *const null_pointer = "null";
13935       char ch[4];
13936       const char *string;
13937
13938       if (op2 == boolean_true_node)
13939         string = boolean_true;
13940       else if (op2 == boolean_false_node)
13941         string = boolean_false;
13942       else if (op2 == null_pointer_node
13943                || (integer_zerop (op2)
13944                    && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13945         /* FIXME: null is not a compile-time constant, so it is only safe to
13946            merge if the overall expression is non-constant. However, this
13947            code always merges without checking the overall expression.  */
13948         string = null_pointer;
13949       else if (TREE_TYPE (op2) == char_type_node)
13950         {
13951           /* Convert the character into UTF-8.  */
13952           unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13953           unsigned char *p = (unsigned char *) ch;
13954           if (0x01 <= c && c <= 0x7f)
13955             *p++ = (unsigned char) c;
13956           else if (c < 0x7ff)
13957             {
13958               *p++ = (unsigned char) (c >> 6 | 0xc0);
13959               *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13960             }
13961           else
13962             {
13963               *p++ = (unsigned char) (c >> 12 | 0xe0);
13964               *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13965               *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13966             }
13967           *p = '\0';
13968
13969           string = ch;
13970         }
13971       else
13972         string = string_convert_int_cst (op2);
13973
13974       return do_merge_string_cste (op1, string, strlen (string), after);
13975     }
13976   return NULL_TREE;
13977 }
13978
13979 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13980    has to be a STRING_CST and the other part must be a STRING_CST or a
13981    INTEGRAL constant. Return a new STRING_CST if the operation
13982    succeed, NULL_TREE otherwise.
13983
13984    If the case we want to optimize for space, we might want to return
13985    NULL_TREE for each invocation of this routine. FIXME */
13986
13987 static tree
13988 string_constant_concatenation (tree op1, tree op2)
13989 {
13990   if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13991     {
13992       tree string, rest;
13993       int invert;
13994
13995       string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13996       rest   = (string == op1 ? op2 : op1);
13997       invert = (string == op1 ? 0 : 1 );
13998
13999       /* Walk REST, only if it looks reasonable */
14000       if (TREE_CODE (rest) != STRING_CST
14001           && !IS_CRAFTED_STRING_BUFFER_P (rest)
14002           && !JSTRING_TYPE_P (TREE_TYPE (rest))
14003           && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
14004         {
14005           rest = java_complete_tree (rest);
14006           if (rest == error_mark_node)
14007             return error_mark_node;
14008           rest = fold (rest);
14009         }
14010       return merge_string_cste (string, rest, invert);
14011     }
14012   return NULL_TREE;
14013 }
14014
14015 /* Implement the `+' operator. Does static optimization if possible,
14016    otherwise create (if necessary) and append elements to a
14017    StringBuffer. The StringBuffer will be carried around until it is
14018    used for a function call or an assignment. Then toString() will be
14019    called on it to turn it into a String object. */
14020
14021 static tree
14022 build_string_concatenation (tree op1, tree op2)
14023 {
14024   tree result;
14025   int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
14026
14027   /* Try to do some static optimization */
14028   if ((result = string_constant_concatenation (op1, op2)))
14029     return result;
14030
14031   /* Discard empty strings on either side of the expression */
14032   if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
14033     {
14034       op1 = op2;
14035       op2 = NULL_TREE;
14036     }
14037   else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14038     op2 = NULL_TREE;
14039
14040   /* If operands are string constant, turn then into object references */
14041   if (TREE_CODE (op1) == STRING_CST)
14042     op1 = patch_string_cst (op1);
14043   if (op2 && TREE_CODE (op2) == STRING_CST)
14044     op2 = patch_string_cst (op2);
14045
14046   /* If either one of the constant is null and the other non null
14047      operand is a String constant, return it. */
14048   if ((TREE_CODE (op1) == STRING_CST) && !op2)
14049     return op1;
14050
14051   /* If OP1 isn't already a StringBuffer, create and
14052      initialize a new one */
14053   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14054     {
14055       /* Two solutions here:
14056          1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14057          2) OP1 is something else, we call new StringBuffer().append(OP1).  */
14058       if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14059         op1 = BUILD_STRING_BUFFER (op1);
14060       else
14061         {
14062           tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14063           op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14064         }
14065     }
14066
14067   if (op2)
14068     {
14069       /* OP1 is no longer the last node holding a crafted StringBuffer */
14070       IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14071       /* Create a node for `{new...,xxx}.append (op2)' */
14072       op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14073     }
14074
14075   /* Mark the last node holding a crafted StringBuffer */
14076   IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14077
14078   TREE_SIDE_EFFECTS (op1) = side_effects;
14079   return op1;
14080 }
14081
14082 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14083    StringBuffer. If no string were found to be patched, return
14084    NULL. */
14085
14086 static tree
14087 patch_string (tree node)
14088 {
14089   if (node == error_mark_node)
14090     return error_mark_node;
14091   if (TREE_CODE (node) == STRING_CST)
14092     return patch_string_cst (node);
14093   else if (IS_CRAFTED_STRING_BUFFER_P (node))
14094     {
14095       int saved = ctxp->explicit_constructor_p;
14096       tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14097       tree ret;
14098       /* Temporary disable forbid the use of `this'. */
14099       ctxp->explicit_constructor_p = 0;
14100       ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14101       /* String concatenation arguments must be evaluated in order too. */
14102       ret = force_evaluation_order (ret);
14103       /* Restore it at its previous value */
14104       ctxp->explicit_constructor_p = saved;
14105       return ret;
14106     }
14107   return NULL_TREE;
14108 }
14109
14110 /* Build the internal representation of a string constant.  */
14111
14112 static tree
14113 patch_string_cst (tree node)
14114 {
14115   int location;
14116   if (! flag_emit_class_files)
14117     {
14118       node = get_identifier (TREE_STRING_POINTER (node));
14119       location = alloc_name_constant (CONSTANT_String, node);
14120       node = build_ref_from_constant_pool (location);
14121     }
14122   TREE_CONSTANT (node) = 1;
14123   TREE_INVARIANT (node) = 1;
14124
14125   /* ??? Guessing that the class file code can't handle casts.  */
14126   if (! flag_emit_class_files)
14127     node = convert (string_ptr_type_node, node);
14128   else
14129     TREE_TYPE (node) = string_ptr_type_node;
14130
14131   return node;
14132 }
14133
14134 /* Build an incomplete unary operator expression. */
14135
14136 static tree
14137 build_unaryop (int op_token, int op_location, tree op1)
14138 {
14139   enum tree_code op;
14140   tree unaryop;
14141   switch (op_token)
14142     {
14143     case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14144     case MINUS_TK: op = NEGATE_EXPR; break;
14145     case NEG_TK: op = TRUTH_NOT_EXPR; break;
14146     case NOT_TK: op = BIT_NOT_EXPR; break;
14147     default: abort ();
14148     }
14149
14150   unaryop = build1 (op, NULL_TREE, op1);
14151   TREE_SIDE_EFFECTS (unaryop) = 1;
14152   /* Store the location of the operator, for better error report. The
14153      string of the operator will be rebuild based on the OP value. */
14154   EXPR_WFL_LINECOL (unaryop) = op_location;
14155   return unaryop;
14156 }
14157
14158 /* Special case for the ++/-- operators, since they require an extra
14159    argument to build, which is set to NULL and patched
14160    later. IS_POST_P is 1 if the operator, 0 otherwise.  */
14161
14162 static tree
14163 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14164 {
14165   static const enum tree_code lookup [2][2] =
14166     {
14167       { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14168       { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14169     };
14170   tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14171                       NULL_TREE, op1, NULL_TREE);
14172   TREE_SIDE_EFFECTS (node) = 1;
14173   /* Store the location of the operator, for better error report. The
14174      string of the operator will be rebuild based on the OP value. */
14175   EXPR_WFL_LINECOL (node) = op_location;
14176   return node;
14177 }
14178
14179 /* Build an incomplete cast operator, based on the use of the
14180    CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14181    set. java_complete_tree is trained to walk a CONVERT_EXPR even
14182    though its type is already set.  */
14183
14184 static tree
14185 build_cast (int location, tree type, tree exp)
14186 {
14187   tree node = build1 (CONVERT_EXPR, type, exp);
14188   EXPR_WFL_LINECOL (node) = location;
14189   return node;
14190 }
14191
14192 /* Build an incomplete class reference operator.  */
14193 static tree
14194 build_incomplete_class_ref (int location, tree class_name)
14195 {
14196   tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14197   tree class_decl = GET_CPC ();
14198   tree this_class = TREE_TYPE (class_decl);
14199
14200   /* Generate the synthetic static method `class$'.  (Previously we
14201      deferred this, causing different method tables to be emitted
14202      for native code and bytecode.)  */
14203   if (!TYPE_DOT_CLASS (this_class)
14204       && !JPRIMITIVE_TYPE_P (class_name)
14205       && !(TREE_CODE (class_name) == VOID_TYPE))
14206     {
14207       tree cpc_list = GET_CPC_LIST();
14208       tree cpc = cpc_list;
14209       tree target_class;
14210
14211       /* For inner classes, add a 'class$' method to their outermost
14212          context, creating it if necessary.  */
14213       
14214       while (GET_NEXT_ENCLOSING_CPC(cpc))
14215         cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14216       class_decl = TREE_VALUE (cpc);
14217
14218       target_class = TREE_TYPE (class_decl);
14219
14220       if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14221         {
14222           /* For interfaces, adding a static 'class$' method directly 
14223              is illegal.  So create an inner class to contain the new
14224              method.  Empirically this matches the behavior of javac.  */
14225           tree t, inner;
14226           /* We want the generated inner class inside the outermost class. */
14227           GET_CPC_LIST() = cpc;
14228           t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14229           inner = create_anonymous_class (t);
14230           target_class = TREE_TYPE (inner);
14231           end_class_declaration (1);
14232           GET_CPC_LIST() = cpc_list;
14233         }
14234
14235       if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14236         build_dot_class_method (target_class);
14237
14238       if (this_class != target_class)
14239         TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14240     }
14241
14242   EXPR_WFL_LINECOL (node) = location;
14243   return node;
14244 }
14245
14246 /* Complete an incomplete class reference operator.  */
14247 static tree
14248 patch_incomplete_class_ref (tree node)
14249 {
14250   tree type = TREE_OPERAND (node, 0);
14251   tree ref_type;
14252
14253   if (!(ref_type = resolve_type_during_patch (type)))
14254     return error_mark_node;
14255
14256   /* If we're not emitting class files and we know ref_type is a
14257      compiled class, build a direct reference.  */
14258   if ((! flag_emit_class_files && is_compiled_class (ref_type))
14259       || JPRIMITIVE_TYPE_P (ref_type)
14260       || TREE_CODE (ref_type) == VOID_TYPE)
14261     {
14262       tree dot = build_class_ref (ref_type);
14263       /* A class referenced by `foo.class' is initialized.  */
14264       if (!flag_emit_class_files)
14265        dot = build_class_init (ref_type, dot);
14266       return java_complete_tree (dot);
14267     }
14268
14269   /* If we're emitting class files and we have to deal with non
14270      primitive types, we invoke the synthetic static method `class$'.  */
14271   ref_type = build_dot_class_method_invocation (current_class, ref_type);
14272   return java_complete_tree (ref_type);
14273 }
14274
14275 /* 15.14 Unary operators. We return error_mark_node in case of error,
14276    but preserve the type of NODE if the type is fixed.  */
14277
14278 static tree
14279 patch_unaryop (tree node, tree wfl_op)
14280 {
14281   tree op = TREE_OPERAND (node, 0);
14282   tree op_type = TREE_TYPE (op);
14283   tree prom_type = NULL_TREE, value, decl;
14284   int nested_field_flag = 0;
14285   int code = TREE_CODE (node);
14286   int error_found = 0;
14287
14288   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14289
14290   switch (code)
14291     {
14292       /* 15.13.2 Postfix Increment Operator ++ */
14293     case POSTINCREMENT_EXPR:
14294       /* 15.13.3 Postfix Increment Operator -- */
14295     case POSTDECREMENT_EXPR:
14296       /* 15.14.1 Prefix Increment Operator ++ */
14297     case PREINCREMENT_EXPR:
14298       /* 15.14.2 Prefix Decrement Operator -- */
14299     case PREDECREMENT_EXPR:
14300       op = decl = extract_field_decl (op);
14301       nested_field_flag
14302         = nested_field_expanded_access_p (op, NULL, NULL, NULL);
14303       /* We might be trying to change an outer field accessed using
14304          access method. */
14305       if (nested_field_flag)
14306         {
14307           /* Retrieve the decl of the field we're trying to access. We
14308              do that by first retrieving the function we would call to
14309              access the field. It has been already verified that this
14310              field isn't final */
14311           if (flag_emit_class_files)
14312             decl = TREE_OPERAND (op, 0);
14313           else
14314             decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14315           decl = DECL_FUNCTION_ACCESS_DECL (decl);
14316         }
14317       /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14318       else if (!JDECL_P (decl)
14319           && TREE_CODE (decl) != COMPONENT_REF
14320           && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14321           && TREE_CODE (decl) != INDIRECT_REF
14322           && !(TREE_CODE (decl) == COMPOUND_EXPR
14323                && TREE_OPERAND (decl, 1)
14324                && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14325         {
14326           TREE_TYPE (node) = error_mark_node;
14327           error_found = 1;
14328         }
14329
14330       /* From now on, we know that op if a variable and that it has a
14331          valid wfl. We use wfl_op to locate errors related to the
14332          ++/-- operand. */
14333       if (!JNUMERIC_TYPE_P (op_type))
14334         {
14335           parse_error_context
14336             (wfl_op, "Invalid argument type %qs to %qs",
14337              lang_printable_name (op_type, 0), operator_string (node));
14338           TREE_TYPE (node) = error_mark_node;
14339           error_found = 1;
14340         }
14341       else
14342         {
14343           /* Before the addition, binary numeric promotion is performed on
14344              both operands, if really necessary */
14345           if (JINTEGRAL_TYPE_P (op_type))
14346             {
14347               value = build_int_cst (op_type, 1);
14348               TREE_TYPE (node) = op_type;
14349             }
14350           else
14351             {
14352               value = build_int_cst (NULL_TREE, 1);
14353               TREE_TYPE (node) =
14354                 binary_numeric_promotion (op_type,
14355                                           TREE_TYPE (value), &op, &value);
14356             }
14357
14358           /* We remember we might be accessing an outer field */
14359           if (nested_field_flag)
14360             {
14361               /* We re-generate an access to the field */
14362               value = build2 (PLUS_EXPR, TREE_TYPE (op),
14363                               build_nested_field_access (wfl_op, decl), value);
14364
14365               /* And we patch the original access$() into a write
14366                  with plus_op as a rhs */
14367               return nested_field_access_fix (node, op, value);
14368             }
14369
14370           /* And write back into the node. */
14371           TREE_OPERAND (node, 0) = op;
14372           TREE_OPERAND (node, 1) = value;
14373           /* Convert the overall back into its original type, if
14374              necessary, and return */
14375           if (JINTEGRAL_TYPE_P (op_type))
14376             return fold (node);
14377           else
14378             return fold (convert (op_type, node));
14379         }
14380       break;
14381
14382       /* 15.14.3 Unary Plus Operator + */
14383     case UNARY_PLUS_EXPR:
14384       /* 15.14.4 Unary Minus Operator - */
14385     case NEGATE_EXPR:
14386       if (!JNUMERIC_TYPE_P (op_type))
14387         {
14388           ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14389           TREE_TYPE (node) = error_mark_node;
14390           error_found = 1;
14391         }
14392       /* Unary numeric promotion is performed on operand */
14393       else
14394         {
14395           op = do_unary_numeric_promotion (op);
14396           prom_type = TREE_TYPE (op);
14397           if (code == UNARY_PLUS_EXPR)
14398             return fold (op);
14399         }
14400       break;
14401
14402       /* 15.14.5 Bitwise Complement Operator ~ */
14403     case BIT_NOT_EXPR:
14404       if (!JINTEGRAL_TYPE_P (op_type))
14405         {
14406           ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14407           TREE_TYPE (node) = error_mark_node;
14408           error_found = 1;
14409         }
14410       else
14411         {
14412           op = do_unary_numeric_promotion (op);
14413           prom_type = TREE_TYPE (op);
14414         }
14415       break;
14416
14417       /* 15.14.6 Logical Complement Operator ! */
14418     case TRUTH_NOT_EXPR:
14419       if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14420         {
14421           ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14422           /* But the type is known. We will report an error if further
14423              attempt of a assignment is made with this rhs */
14424           TREE_TYPE (node) = boolean_type_node;
14425           error_found = 1;
14426         }
14427       else
14428         prom_type = boolean_type_node;
14429       break;
14430
14431       /* 15.15 Cast Expression */
14432     case CONVERT_EXPR:
14433       value = patch_cast (node, wfl_operator);
14434       if (value == error_mark_node)
14435         {
14436           /* If this cast is part of an assignment, we tell the code
14437              that deals with it not to complain about a mismatch,
14438              because things have been cast, anyways */
14439           TREE_TYPE (node) = error_mark_node;
14440           error_found = 1;
14441         }
14442       else
14443         {
14444           value = fold (value);
14445           return value;
14446         }
14447       break;
14448
14449     case NOP_EXPR:
14450       /* This can only happen when the type is already known.  */
14451       gcc_assert (TREE_TYPE (node) != NULL_TREE);
14452       prom_type = TREE_TYPE (node);
14453       break;
14454     }
14455
14456   if (error_found)
14457     return error_mark_node;
14458
14459   /* There are cases where node has been replaced by something else
14460      and we don't end up returning here: UNARY_PLUS_EXPR,
14461      CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14462   TREE_OPERAND (node, 0) = fold (op);
14463   TREE_TYPE (node) = prom_type;
14464   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14465   return fold (node);
14466 }
14467
14468 /* Generic type resolution that sometimes takes place during node
14469    patching. Returned the resolved type or generate an error
14470    message. Return the resolved type or NULL_TREE.  */
14471
14472 static tree
14473 resolve_type_during_patch (tree type)
14474 {
14475   if (unresolved_type_p (type, NULL))
14476     {
14477       tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14478       if (!type_decl)
14479         {
14480           parse_error_context (type,
14481                                "Class %qs not found in type declaration",
14482                                IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14483           return NULL_TREE;
14484         }
14485
14486       check_deprecation (type, type_decl);
14487
14488       return TREE_TYPE (type_decl);
14489     }
14490   return type;
14491 }
14492
14493 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14494    found. Otherwise NODE or something meant to replace it is returned.  */
14495
14496 static tree
14497 patch_cast (tree node, tree wfl_op)
14498 {
14499   tree op = TREE_OPERAND (node, 0);
14500   tree cast_type = TREE_TYPE (node);
14501   tree patched, op_type;
14502   char *t1;
14503
14504   /* Some string patching might be necessary at this stage */
14505   if ((patched = patch_string (op)))
14506     TREE_OPERAND (node, 0) = op = patched;
14507   op_type = TREE_TYPE (op);
14508
14509   /* First resolve OP_TYPE if unresolved */
14510   if (!(cast_type = resolve_type_during_patch (cast_type)))
14511     return error_mark_node;
14512
14513   /* Check on cast that are proven correct at compile time */
14514   if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14515     {
14516       /* Same type */
14517       if (cast_type == op_type)
14518         return node;
14519
14520       /* A narrowing conversion from a floating-point number to an
14521          integral type requires special handling (5.1.3).  */
14522       if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14523         if (cast_type != long_type_node)
14524           op = convert (integer_type_node, op);
14525
14526       /* Try widening/narrowing conversion.  Potentially, things need
14527          to be worked out in gcc so we implement the extreme cases
14528          correctly.  fold_convert() needs to be fixed.  */
14529       return convert (cast_type, op);
14530     }
14531
14532   /* It's also valid to cast a boolean into a boolean */
14533   if (op_type == boolean_type_node && cast_type == boolean_type_node)
14534     return node;
14535
14536   /* null can be casted to references */
14537   if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14538     return build_null_of_type (cast_type);
14539
14540   /* The remaining legal casts involve conversion between reference
14541      types. Check for their compile time correctness. */
14542   if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14543       && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14544     {
14545       TREE_TYPE (node) = promote_type (cast_type);
14546       /* Now, the case can be determined correct at compile time if
14547          OP_TYPE can be converted into CAST_TYPE by assignment
14548          conversion (5.2) */
14549
14550       if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14551         {
14552           TREE_SET_CODE (node, NOP_EXPR);
14553           return node;
14554         }
14555
14556       if (flag_emit_class_files)
14557         {
14558           TREE_SET_CODE (node, CONVERT_EXPR);
14559           return node;
14560         }
14561
14562       /* The cast requires a run-time check */
14563       return build3 (CALL_EXPR, promote_type (cast_type),
14564                      build_address_of (soft_checkcast_node),
14565                      tree_cons (NULL_TREE, build_class_ref (cast_type),
14566                                 build_tree_list (NULL_TREE, op)),
14567                      NULL_TREE);
14568     }
14569
14570   /* Any other casts are proven incorrect at compile time */
14571   t1 = xstrdup (lang_printable_name (op_type, 0));
14572   parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14573                        t1, lang_printable_name (cast_type, 0));
14574   free (t1);
14575   return error_mark_node;
14576 }
14577
14578 /* Build a null constant and give it the type TYPE.  */
14579
14580 static tree
14581 build_null_of_type (tree type)
14582 {
14583   tree node = build_int_cst (promote_type (type), 0);
14584   return node;
14585 }
14586
14587 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14588    a list of indices. */
14589 static tree
14590 build_array_ref (int location, tree array, tree index)
14591 {
14592   tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14593                       NULL_TREE, NULL_TREE);
14594   EXPR_WFL_LINECOL (node) = location;
14595   return node;
14596 }
14597
14598 /* 15.12 Array Access Expression */
14599
14600 static tree
14601 patch_array_ref (tree node)
14602 {
14603   tree array = TREE_OPERAND (node, 0);
14604   tree array_type  = TREE_TYPE (array);
14605   tree index = TREE_OPERAND (node, 1);
14606   tree index_type = TREE_TYPE (index);
14607   int error_found = 0;
14608
14609   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14610
14611   if (TREE_CODE (array_type) == POINTER_TYPE)
14612     array_type = TREE_TYPE (array_type);
14613
14614   /* The array reference must be an array */
14615   if (!TYPE_ARRAY_P (array_type))
14616     {
14617       parse_error_context
14618         (wfl_operator,
14619          "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14620          lang_printable_name (array_type, 0));
14621       TREE_TYPE (node) = error_mark_node;
14622       error_found = 1;
14623     }
14624
14625   /* The array index undergoes unary numeric promotion. The promoted
14626      type must be int */
14627   index = do_unary_numeric_promotion (index);
14628   if (TREE_TYPE (index) != int_type_node)
14629     {
14630       if (valid_cast_to_p (index_type, int_type_node))
14631         parse_error_context (wfl_operator,
14632    "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14633                              lang_printable_name (index_type, 0));
14634       else
14635         parse_error_context (wfl_operator,
14636           "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14637                              lang_printable_name (index_type, 0));
14638       TREE_TYPE (node) = error_mark_node;
14639       error_found = 1;
14640     }
14641
14642   if (error_found)
14643     return error_mark_node;
14644
14645   array_type = TYPE_ARRAY_ELEMENT (array_type);
14646
14647   if (flag_emit_class_files)
14648     {
14649       TREE_OPERAND (node, 0) = array;
14650       TREE_OPERAND (node, 1) = index;
14651     }
14652   else
14653     node = build_java_arrayaccess (array, array_type, index);
14654   TREE_TYPE (node) = array_type;
14655   return node;
14656 }
14657
14658 /* 15.9 Array Creation Expressions */
14659
14660 static tree
14661 build_newarray_node (tree type, tree dims, int extra_dims)
14662 {
14663   tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14664                       nreverse (dims),
14665                       build_int_cst (NULL_TREE, extra_dims));
14666   return node;
14667 }
14668
14669 static tree
14670 patch_newarray (tree node)
14671 {
14672   tree type = TREE_OPERAND (node, 0);
14673   tree dims = TREE_OPERAND (node, 1);
14674   tree cdim, array_type;
14675   int error_found = 0;
14676   int ndims = 0;
14677   int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14678
14679   /* Dimension types are verified. It's better for the types to be
14680      verified in order. */
14681   for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14682     {
14683       int dim_error = 0;
14684       tree dim = TREE_VALUE (cdim);
14685
14686       /* Dim might have been saved during its evaluation */
14687       dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14688
14689       /* The type of each specified dimension must be an integral type. */
14690       if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14691         dim_error = 1;
14692
14693       /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14694          promoted type must be int. */
14695       else
14696         {
14697           dim = do_unary_numeric_promotion (dim);
14698           if (TREE_TYPE (dim) != int_type_node)
14699             dim_error = 1;
14700         }
14701
14702       /* Report errors on types here */
14703       if (dim_error)
14704         {
14705           parse_error_context
14706             (TREE_PURPOSE (cdim),
14707              "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14708              (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14709               "Explicit cast needed to" : "Can't"),
14710              lang_printable_name (TREE_TYPE (dim), 0));
14711           error_found = 1;
14712         }
14713
14714       TREE_PURPOSE (cdim) = NULL_TREE;
14715     }
14716
14717   /* Resolve array base type if unresolved */
14718   if (!(type = resolve_type_during_patch (type)))
14719     error_found = 1;
14720
14721   if (error_found)
14722     {
14723       /* We don't want further evaluation of this bogus array creation
14724          operation */
14725       TREE_TYPE (node) = error_mark_node;
14726       return error_mark_node;
14727     }
14728
14729   /* Set array_type to the actual (promoted) array type of the result. */
14730   if (TREE_CODE (type) == RECORD_TYPE)
14731     type = build_pointer_type (type);
14732   while (--xdims >= 0)
14733     {
14734       type = promote_type (build_java_array_type (type, -1));
14735     }
14736   dims = nreverse (dims);
14737   array_type = type;
14738   for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14739     {
14740       type = array_type;
14741       array_type
14742         = build_java_array_type (type,
14743                                  TREE_CODE (cdim) == INTEGER_CST
14744                                  ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14745                                  : -1);
14746       array_type = promote_type (array_type);
14747     }
14748   dims = nreverse (dims);
14749
14750   /* The node is transformed into a function call. Things are done
14751      differently according to the number of dimensions. If the number
14752      of dimension is equal to 1, then the nature of the base type
14753      (primitive or not) matters. */
14754   if (ndims == 1)
14755     return build_new_array (type, TREE_VALUE (dims));
14756
14757   /* Can't reuse what's already written in expr.c because it uses the
14758      JVM stack representation. Provide a build_multianewarray. FIXME */
14759   return build3 (CALL_EXPR, array_type,
14760                  build_address_of (soft_multianewarray_node),
14761                  tree_cons (NULL_TREE,
14762                             build_class_ref (TREE_TYPE (array_type)),
14763                             tree_cons (NULL_TREE,
14764                                        build_int_cst (NULL_TREE, ndims),
14765                                        dims)),
14766                  NULL_TREE);
14767 }
14768
14769 /* 10.6 Array initializer.  */
14770
14771 /* Build a wfl for array element that don't have one, so we can
14772    pin-point errors.  */
14773
14774 static tree
14775 maybe_build_array_element_wfl (tree node)
14776 {
14777   if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14778     {
14779       /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14780       return build_expr_wfl (NULL_TREE,
14781 #ifdef USE_MAPPED_LOCATION
14782                              input_location
14783 #else
14784                              ctxp->filename,
14785                              ctxp->lexer->token_start.line,
14786                              ctxp->lexer->token_start.col
14787 #endif
14788                              );
14789     }
14790   else
14791     return NULL_TREE;
14792 }
14793
14794 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14795    identification of initialized arrays easier to detect during walk
14796    and expansion.  */
14797
14798 static tree
14799 build_new_array_init (int location, tree values)
14800 {
14801   tree constructor = build_constructor_from_list (NULL_TREE,
14802                                                   nreverse (values));
14803   tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14804   EXPR_WFL_LINECOL (to_return) = location;
14805   return to_return;
14806 }
14807
14808 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14809    occurred.  Otherwise return NODE after having set its type
14810    appropriately.  */
14811
14812 static tree
14813 patch_new_array_init (tree type, tree node)
14814 {
14815   int error_seen = 0;
14816   tree element_type;
14817   unsigned HOST_WIDE_INT length;
14818   constructor_elt *current;
14819   int all_constant = 1;
14820   tree init = TREE_OPERAND (node, 0);
14821
14822   if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14823     {
14824       parse_error_context (node,
14825                            "Invalid array initializer for non-array type %qs",
14826                            lang_printable_name (type, 1));
14827       return error_mark_node;
14828     }
14829   type = TREE_TYPE (type);
14830   element_type = TYPE_ARRAY_ELEMENT (type);
14831
14832   for (length = 0;
14833        VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init),
14834                     length, current);
14835        length++)
14836     {
14837       tree elt = current->value;
14838       if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14839         {
14840           error_seen |= array_constructor_check_entry (element_type, current);
14841           elt = current->value;
14842           /* When compiling to native code, STRING_CST is converted to
14843              INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14844           if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14845             all_constant = 0;
14846         }
14847       else
14848         {
14849           current->value = patch_new_array_init (element_type, elt);
14850           current->index = NULL_TREE;
14851           all_constant = 0;
14852         }
14853       if (elt && TREE_CODE (elt) == TREE_LIST
14854           && TREE_VALUE (elt) == error_mark_node)
14855         error_seen = 1;
14856     }
14857
14858   if (error_seen)
14859     return error_mark_node;
14860
14861   /* Create a new type. We can't reuse the one we have here by
14862      patching its dimension because it originally is of dimension -1
14863      hence reused by gcc. This would prevent triangular arrays. */
14864   type = build_java_array_type (element_type, length);
14865   TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14866   TREE_TYPE (node) = promote_type (type);
14867   TREE_CONSTANT (init) = all_constant;
14868   TREE_INVARIANT (init) = all_constant;
14869   TREE_CONSTANT (node) = all_constant;
14870   TREE_INVARIANT (node) = all_constant;
14871   return node;
14872 }
14873
14874 /* Verify that one entry of the initializer element list can be
14875    assigned to the array base type. Report 1 if an error occurred, 0
14876    otherwise.  */
14877
14878 static int
14879 array_constructor_check_entry (tree type, constructor_elt *entry)
14880 {
14881   char *array_type_string = NULL;       /* For error reports */
14882   tree value, type_value, new_value, wfl_value, patched;
14883   int error_seen = 0;
14884
14885   new_value = NULL_TREE;
14886   wfl_value = entry->value;
14887
14888   value = java_complete_tree (entry->value);
14889   /* patch_string return error_mark_node if arg is error_mark_node */
14890   if ((patched = patch_string (value)))
14891     value = patched;
14892   if (value == error_mark_node)
14893     return 1;
14894
14895   type_value = TREE_TYPE (value);
14896
14897   /* At anytime, try_builtin_assignconv can report a warning on
14898      constant overflow during narrowing. */
14899   SET_WFL_OPERATOR (wfl_operator, entry->index, wfl_value);
14900   new_value = try_builtin_assignconv (wfl_operator, type, value);
14901   if (!new_value && (new_value = try_reference_assignconv (type, value)))
14902     type_value = promote_type (type);
14903
14904   /* Check and report errors */
14905   if (!new_value)
14906     {
14907       const char *const msg = (!valid_cast_to_p (type_value, type) ?
14908                    "Can't" : "Explicit cast needed to");
14909       if (!array_type_string)
14910         array_type_string = xstrdup (lang_printable_name (type, 1));
14911       parse_error_context
14912         (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14913          msg, lang_printable_name (type_value, 1), array_type_string);
14914       error_seen = 1;
14915     }
14916
14917   if (new_value)
14918     entry->value = new_value;
14919
14920   if (array_type_string)
14921     free (array_type_string);
14922
14923   entry->index = NULL_TREE;
14924   return error_seen;
14925 }
14926
14927 static tree
14928 build_this (int location)
14929 {
14930   tree node = build_wfl_node (this_identifier_node);
14931   TREE_SET_CODE (node, THIS_EXPR);
14932   EXPR_WFL_LINECOL (node) = location;
14933   return node;
14934 }
14935
14936 /* 14.15 The return statement. It builds a modify expression that
14937    assigns the returned value to the RESULT_DECL that hold the value
14938    to be returned. */
14939
14940 static tree
14941 build_return (int location, tree op)
14942 {
14943   tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14944   EXPR_WFL_LINECOL (node) = location;
14945   node = build_debugable_stmt (location, node);
14946   return node;
14947 }
14948
14949 static tree
14950 patch_return (tree node)
14951 {
14952   tree return_exp = TREE_OPERAND (node, 0);
14953   tree meth = current_function_decl;
14954   tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14955   int error_found = 0;
14956
14957   TREE_TYPE (node) = error_mark_node;
14958   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14959
14960   /* It's invalid to have a return value within a function that is
14961      declared with the keyword void or that is a constructor */
14962   if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14963     error_found = 1;
14964
14965   /* It's invalid to use a return statement in a static block */
14966   if (DECL_CLINIT_P (current_function_decl))
14967     error_found = 1;
14968
14969   /* It's invalid to have a no return value within a function that
14970      isn't declared with the keyword `void' */
14971   if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14972     error_found = 2;
14973
14974   if (DECL_INSTINIT_P (current_function_decl))
14975     error_found = 1;
14976
14977   if (error_found)
14978     {
14979       if (DECL_INSTINIT_P (current_function_decl))
14980         parse_error_context (wfl_operator,
14981                              "%<return%> inside instance initializer");
14982
14983       else if (DECL_CLINIT_P (current_function_decl))
14984         parse_error_context (wfl_operator,
14985                              "%<return%> inside static initializer");
14986
14987       else if (!DECL_CONSTRUCTOR_P (meth))
14988         {
14989           char *t = xstrdup (lang_printable_name (mtype, 0));
14990           parse_error_context (wfl_operator,
14991                                "%<return%> with%s value from %<%s %s%>",
14992                                (error_found == 1 ? "" : "out"),
14993                                t, lang_printable_name (meth, 2));
14994           free (t);
14995         }
14996       else
14997         parse_error_context (wfl_operator,
14998                              "%<return%> with value from constructor %qs",
14999                              lang_printable_name (meth, 2));
15000       return error_mark_node;
15001     }
15002
15003   /* If we have a return_exp, build a modify expression and expand
15004      it. Note: at that point, the assignment is declared valid, but we
15005      may want to carry some more hacks */
15006   if (return_exp)
15007     {
15008       tree exp = java_complete_tree (return_exp);
15009       tree modify, patched;
15010
15011       if ((patched = patch_string (exp)))
15012         exp = patched;
15013
15014       modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
15015       EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
15016       modify = java_complete_tree (modify);
15017
15018       if (modify != error_mark_node)
15019         {
15020           TREE_SIDE_EFFECTS (modify) = 1;
15021           TREE_OPERAND (node, 0) = modify;
15022         }
15023       else
15024         return error_mark_node;
15025     }
15026   TREE_TYPE (node) = void_type_node;
15027   TREE_SIDE_EFFECTS (node) = 1;
15028   return node;
15029 }
15030
15031 /* 14.8 The if Statement */
15032
15033 static tree
15034 build_if_else_statement (int location, tree expression, tree if_body,
15035                          tree else_body)
15036 {
15037   tree node;
15038   if (!else_body)
15039     else_body = build_java_empty_stmt ();
15040   node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
15041   EXPR_WFL_LINECOL (node) = location;
15042   node = build_debugable_stmt (location, node);
15043   return node;
15044 }
15045
15046 static tree
15047 patch_if_else_statement (tree node)
15048 {
15049   tree expression = TREE_OPERAND (node, 0);
15050   int can_complete_normally
15051     = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15052        | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15053
15054   TREE_TYPE (node) = error_mark_node;
15055   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15056
15057   /* The type of expression must be boolean */
15058   if (TREE_TYPE (expression) != boolean_type_node
15059       && TREE_TYPE (expression) != promoted_boolean_type_node)
15060     {
15061       parse_error_context
15062         (wfl_operator,
15063          "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
15064          lang_printable_name (TREE_TYPE (expression), 0));
15065       return error_mark_node;
15066     }
15067
15068   TREE_TYPE (node) = void_type_node;
15069   TREE_SIDE_EFFECTS (node) = 1;
15070   CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15071   return node;
15072 }
15073
15074 /* 14.6 Labeled Statements */
15075
15076 /* Action taken when a labeled statement is parsed. a new
15077    LABELED_BLOCK_EXPR is created. No statement is attached to the
15078    label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
15079
15080 static tree
15081 build_labeled_block (int location, tree label)
15082 {
15083   tree label_name ;
15084   tree label_decl, node;
15085   if (label == NULL_TREE || label == continue_identifier_node)
15086     label_name = label;
15087   else
15088     {
15089       label_name = merge_qualified_name (label_id, label);
15090       /* Issue an error if we try to reuse a label that was previously
15091          declared */
15092       if (IDENTIFIER_LOCAL_VALUE (label_name))
15093         {
15094           EXPR_WFL_LINECOL (wfl_operator) = location;
15095           parse_error_context (wfl_operator,
15096             "Declaration of %qs shadows a previous label declaration",
15097                                IDENTIFIER_POINTER (label));
15098           EXPR_WFL_LINECOL (wfl_operator) =
15099             EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15100           parse_error_context (wfl_operator,
15101             "This is the location of the previous declaration of label %qs",
15102                                IDENTIFIER_POINTER (label));
15103           java_error_count--;
15104         }
15105     }
15106
15107   label_decl = create_label_decl (label_name);
15108   node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15109   EXPR_WFL_LINECOL (node) = location;
15110   TREE_SIDE_EFFECTS (node) = 1;
15111   return node;
15112 }
15113
15114 /* A labeled statement LBE is attached a statement.  */
15115
15116 static tree
15117 finish_labeled_statement (tree lbe, /* Labeled block expr */
15118                           tree statement)
15119 {
15120   /* In anyways, tie the loop to its statement */
15121   LABELED_BLOCK_BODY (lbe) = statement;
15122   pop_labeled_block ();
15123   POP_LABELED_BLOCK ();
15124   return lbe;
15125 }
15126
15127 /* 14.10, 14.11, 14.12 Loop Statements */
15128
15129 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15130    list. */
15131
15132 static tree
15133 build_new_loop (tree loop_body)
15134 {
15135   tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15136   TREE_SIDE_EFFECTS (loop) = 1;
15137   PUSH_LOOP (loop);
15138   return loop;
15139 }
15140
15141 /* Create a loop body according to the following structure:
15142      COMPOUND_EXPR
15143        COMPOUND_EXPR            (loop main body)
15144          EXIT_EXPR              (this order is for while/for loops.
15145          LABELED_BLOCK_EXPR      the order is reversed for do loops)
15146            LABEL_DECL           (a continue occurring here branches at the
15147            BODY                  end of this labeled block)
15148        INCREMENT                (if any)
15149
15150   REVERSED, if nonzero, tells that the loop condition expr comes
15151   after the body, like in the do-while loop.
15152
15153   To obtain a loop, the loop body structure described above is
15154   encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15155
15156    LABELED_BLOCK_EXPR
15157      LABEL_DECL                   (use this label to exit the loop)
15158      LOOP_EXPR
15159        <structure described above> */
15160
15161 static tree
15162 build_loop_body (int location, tree condition, int reversed)
15163 {
15164   tree first, second, body;
15165
15166   condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15167   EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15168   condition = build_debugable_stmt (location, condition);
15169   TREE_SIDE_EFFECTS (condition) = 1;
15170
15171   body = build_labeled_block (0, continue_identifier_node);
15172   first = (reversed ? body : condition);
15173   second = (reversed ? condition : body);
15174   return build2 (COMPOUND_EXPR, NULL_TREE,
15175                  build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15176                  build_java_empty_stmt ());
15177 }
15178
15179 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15180    their order) on the current loop. Unlink the current loop from the
15181    loop list.  */
15182
15183 static tree
15184 finish_loop_body (int location, tree condition, tree body, int reversed)
15185 {
15186   tree to_return = ctxp->current_loop;
15187   tree loop_body = LOOP_EXPR_BODY (to_return);
15188   if (condition)
15189     {
15190       tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15191       /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15192          The real EXIT_EXPR is one operand further. */
15193       EXPR_WFL_LINECOL (cnode) = location;
15194       if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15195         {
15196           cnode = EXPR_WFL_NODE (cnode);
15197           /* This one is for accurate error reports */
15198           EXPR_WFL_LINECOL (cnode) = location;
15199         }
15200       TREE_OPERAND (cnode, 0) = condition;
15201     }
15202   LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15203   POP_LOOP ();
15204   return to_return;
15205 }
15206
15207 /* Tailored version of finish_loop_body for FOR loops, when FOR
15208    loops feature the condition part */
15209
15210 static tree
15211 finish_for_loop (int location, tree condition, tree update, tree body)
15212 {
15213   /* Put the condition and the loop body in place */
15214   tree loop = finish_loop_body (location, condition, body, 0);
15215   /* LOOP is the current loop which has been now popped of the loop
15216      stack.  Mark the update block as reachable and install it.  We do
15217      this because the (current interpretation of the) JLS requires
15218      that the update expression be considered reachable even if the
15219      for loop's body doesn't complete normally.  */
15220   if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15221     {
15222       tree up2 = update;
15223       if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15224         up2 = EXPR_WFL_NODE (up2);
15225       /* It is possible for the update expression to be an
15226          EXPR_WFL_NODE wrapping nothing.  */
15227       if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15228         {
15229           /* Try to detect constraint violations.  These would be
15230              programming errors somewhere.  */
15231           if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15232             abort ();
15233           SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15234         }
15235     }
15236   LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15237   return loop;
15238 }
15239
15240 /* Try to find the loop a block might be related to. This comprises
15241    the case where the LOOP_EXPR is found as the second operand of a
15242    COMPOUND_EXPR, because the loop happens to have an initialization
15243    part, then expressed as the first operand of the COMPOUND_EXPR. If
15244    the search finds something, 1 is returned. Otherwise, 0 is
15245    returned. The search is assumed to start from a
15246    LABELED_BLOCK_EXPR's block.  */
15247
15248 static tree
15249 search_loop (tree statement)
15250 {
15251   if (TREE_CODE (statement) == LOOP_EXPR)
15252     return statement;
15253
15254   if (TREE_CODE (statement) == BLOCK)
15255     statement = BLOCK_SUBBLOCKS (statement);
15256   else
15257     return NULL_TREE;
15258
15259   if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15260     while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15261       statement = TREE_OPERAND (statement, 1);
15262
15263   return (TREE_CODE (statement) == LOOP_EXPR
15264           && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15265 }
15266
15267 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15268    returned otherwise.  */
15269
15270 static int
15271 labeled_block_contains_loop_p (tree block, tree loop)
15272 {
15273   if (!block)
15274     return 0;
15275
15276   if (LABELED_BLOCK_BODY (block) == loop)
15277     return 1;
15278
15279   if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15280     return 1;
15281
15282   return 0;
15283 }
15284
15285 /* If the loop isn't surrounded by a labeled statement, create one and
15286    insert LOOP as its body.  */
15287
15288 static tree
15289 patch_loop_statement (tree loop)
15290 {
15291   tree loop_label;
15292
15293   TREE_TYPE (loop) = void_type_node;
15294   if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15295     return loop;
15296
15297   loop_label = build_labeled_block (0, NULL_TREE);
15298   /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15299      that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15300   LABELED_BLOCK_BODY (loop_label) = loop;
15301   PUSH_LABELED_BLOCK (loop_label);
15302   return loop_label;
15303 }
15304
15305 /* 14.13, 14.14: break and continue Statements */
15306
15307 /* Build a break or a continue statement. a null NAME indicates an
15308    unlabeled break/continue statement.  */
15309
15310 static tree
15311 build_bc_statement (int location, int is_break, tree name)
15312 {
15313   tree break_continue, label_block_expr = NULL_TREE;
15314
15315   if (name)
15316     {
15317       if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15318             (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15319         /* Null means that we don't have a target for this named
15320            break/continue. In this case, we make the target to be the
15321            label name, so that the error can be reported accurately in
15322            patch_bc_statement. */
15323         label_block_expr = EXPR_WFL_NODE (name);
15324     }
15325   /* Unlabeled break/continue will be handled during the
15326      break/continue patch operation */
15327   break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15328
15329   IS_BREAK_STMT_P (break_continue) = is_break;
15330   TREE_SIDE_EFFECTS (break_continue) = 1;
15331   EXPR_WFL_LINECOL (break_continue) = location;
15332   break_continue = build_debugable_stmt (location, break_continue);
15333   return break_continue;
15334 }
15335
15336 /* Verification of a break/continue statement. */
15337
15338 static tree
15339 patch_bc_statement (tree node)
15340 {
15341   tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15342   tree labeled_block = ctxp->current_labeled_block;
15343   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15344
15345   /* Having an identifier here means that the target is unknown. */
15346   if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15347     {
15348       parse_error_context (wfl_operator, "No label definition found for %qs",
15349                            IDENTIFIER_POINTER (bc_label));
15350       return error_mark_node;
15351     }
15352   if (! IS_BREAK_STMT_P (node))
15353     {
15354       /* It's a continue statement. */
15355       for (;; labeled_block = TREE_CHAIN (labeled_block))
15356         {
15357           if (labeled_block == NULL_TREE)
15358             {
15359               if (bc_label == NULL_TREE)
15360                 parse_error_context (wfl_operator,
15361                                      "%<continue%> must be in loop");
15362               else
15363                 parse_error_context
15364                   (wfl_operator, "continue label %qs does not name a loop",
15365                    IDENTIFIER_POINTER (bc_label));
15366               return error_mark_node;
15367             }
15368           if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15369                == continue_identifier_node)
15370               && (bc_label == NULL_TREE
15371                   || TREE_CHAIN (labeled_block) == bc_label))
15372             {
15373               bc_label = labeled_block;
15374               break;
15375             }
15376         }
15377     }
15378   else if (!bc_label)
15379     {
15380       for (;; labeled_block = TREE_CHAIN (labeled_block))
15381         {
15382           if (labeled_block == NULL_TREE)
15383             {
15384               parse_error_context (wfl_operator,
15385                                      "%<break%> must be in loop or switch");
15386               return error_mark_node;
15387             }
15388           target_stmt = LABELED_BLOCK_BODY (labeled_block);
15389           if (TREE_CODE (target_stmt) == SWITCH_EXPR
15390               || search_loop (target_stmt))
15391             {
15392               bc_label = labeled_block;
15393               break;
15394             }
15395         }
15396     }
15397
15398   EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15399   CAN_COMPLETE_NORMALLY (bc_label) = 1;
15400
15401   /* Our break/continue don't return values. */
15402   TREE_TYPE (node) = void_type_node;
15403   /* Encapsulate the break within a compound statement so that it's
15404      expanded all the times by expand_expr (and not clobbered
15405      sometimes, like after a if statement) */
15406   node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15407   TREE_SIDE_EFFECTS (node) = 1;
15408   return node;
15409 }
15410
15411 /* Process the exit expression belonging to a loop. Its type must be
15412    boolean.  */
15413
15414 static tree
15415 patch_exit_expr (tree node)
15416 {
15417   tree expression = TREE_OPERAND (node, 0);
15418   TREE_TYPE (node) = error_mark_node;
15419   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15420
15421   /* The type of expression must be boolean */
15422   if (TREE_TYPE (expression) != boolean_type_node)
15423     {
15424       parse_error_context
15425         (wfl_operator,
15426     "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15427          lang_printable_name (TREE_TYPE (expression), 0));
15428       return error_mark_node;
15429     }
15430   /* Now we know things are allright, invert the condition, fold and
15431      return */
15432   TREE_OPERAND (node, 0) =
15433     fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15434
15435   if (! integer_zerop (TREE_OPERAND (node, 0))
15436       && ctxp->current_loop != NULL_TREE
15437       && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15438     CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15439   if (! integer_onep (TREE_OPERAND (node, 0)))
15440     CAN_COMPLETE_NORMALLY (node) = 1;
15441
15442
15443   TREE_TYPE (node) = void_type_node;
15444   return node;
15445 }
15446
15447 /* 14.9 Switch statement */
15448
15449 static tree
15450 patch_switch_statement (tree node)
15451 {
15452   tree se = TREE_OPERAND (node, 0), se_type;
15453   tree save, iter;
15454
15455   /* Complete the switch expression */
15456   se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15457   se_type = TREE_TYPE (se);
15458   /* The type of the switch expression must be char, byte, short or
15459      int */
15460   if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15461     {
15462       EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15463       parse_error_context (wfl_operator,
15464           "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15465                            lang_printable_name (se_type, 0));
15466       /* This is what java_complete_tree will check */
15467       TREE_OPERAND (node, 0) = error_mark_node;
15468       return error_mark_node;
15469     }
15470
15471   /* Save and restore the outer case label list.  */
15472   save = case_label_list;
15473   case_label_list = NULL_TREE;
15474
15475   TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15476
15477   /* See if we've found a duplicate label.  We can't leave this until
15478      code generation, because in `--syntax-only' and `-C' modes we
15479      don't do ordinary code generation.  */
15480   for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15481     {
15482       HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15483       tree subiter;
15484       for (subiter = TREE_CHAIN (iter);
15485            subiter != NULL_TREE;
15486            subiter = TREE_CHAIN (subiter))
15487         {
15488           HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15489           if (val == subval)
15490             {
15491               EXPR_WFL_LINECOL (wfl_operator)
15492                 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15493               /* The case_label_list is in reverse order, so print the
15494                  outer label first.  */
15495               parse_error_context (wfl_operator, "duplicate case label: %<"
15496                                    HOST_WIDE_INT_PRINT_DEC "%>", subval);
15497               EXPR_WFL_LINECOL (wfl_operator)
15498                 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15499               parse_error_context (wfl_operator, "original label is here");
15500
15501               break;
15502             }
15503         }
15504     }
15505
15506   case_label_list = save;
15507
15508   /* Ready to return */
15509   if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15510     {
15511       TREE_TYPE (node) = error_mark_node;
15512       return error_mark_node;
15513     }
15514   TREE_TYPE (node) = void_type_node;
15515   TREE_SIDE_EFFECTS (node) = 1;
15516   CAN_COMPLETE_NORMALLY (node)
15517     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15518       || ! SWITCH_HAS_DEFAULT (node);
15519   return node;
15520 }
15521
15522 /* Assertions.  */
15523
15524 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15525    might be NULL_TREE.  */
15526 static tree
15527 build_assertion (
15528 #ifdef USE_MAPPED_LOCATION
15529                  source_location location,
15530 #else
15531                  int location,
15532 #endif
15533                  tree condition, tree value)
15534 {
15535   tree node;
15536   tree klass = GET_CPC ();
15537
15538   if (! enable_assertions (klass))
15539     {
15540       condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15541                           boolean_false_node, condition);
15542       if (value == NULL_TREE)
15543         value = build_java_empty_stmt ();
15544       return build_if_else_statement (location, condition,
15545                                       value, NULL_TREE);
15546     }
15547
15548   if (! CLASS_USES_ASSERTIONS (klass))
15549     {
15550       tree field, classdollar, id, call;
15551       tree class_type = TREE_TYPE (klass);
15552
15553       field = add_field (class_type,
15554                          get_identifier ("$assertionsDisabled"),
15555                          boolean_type_node,
15556                          ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15557       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15558       FIELD_SYNTHETIC (field) = 1;
15559
15560       classdollar = build_incomplete_class_ref (location, class_type);
15561
15562       /* Call CLASS.desiredAssertionStatus().  */
15563       id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15564       call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15565       call = make_qualified_primary (classdollar, call, location);
15566       TREE_SIDE_EFFECTS (call) = 1;
15567
15568       /* Invert to obtain !CLASS.desiredAssertionStatus().  This may
15569          seem odd, but we do it to generate code identical to that of
15570          the JDK.  */
15571       call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15572       TREE_SIDE_EFFECTS (call) = 1;
15573       DECL_INITIAL (field) = call;
15574
15575       /* Record the initializer in the initializer statement list.  */
15576       call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15577       TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15578       SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15579       MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15580
15581       CLASS_USES_ASSERTIONS (klass) = 1;
15582     }
15583
15584   if (value != NULL_TREE)
15585     value = tree_cons (NULL_TREE, value, NULL_TREE);
15586
15587   node = build_wfl_node (get_identifier ("java"));
15588   node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15589                               location);
15590   node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15591                               location);
15592
15593   node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15594   TREE_SIDE_EFFECTS (node) = 1;
15595   /* It is too early to use BUILD_THROW.  */
15596   node = build1 (THROW_EXPR, NULL_TREE, node);
15597   TREE_SIDE_EFFECTS (node) = 1;
15598
15599   /* We invert the condition; if we just put NODE as the `else' part
15600      then we generate weird-looking bytecode.  */
15601   condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15602   /* Check $assertionsDisabled.  */
15603   condition
15604     = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15605               build1 (TRUTH_NOT_EXPR, NULL_TREE,
15606                       build_wfl_node (get_identifier ("$assertionsDisabled"))),
15607               condition);
15608   node = build_if_else_statement (location, condition, node, NULL_TREE);
15609   return node;
15610 }
15611
15612 /* 14.18 The try/catch statements */
15613
15614 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15615    catches TYPE and executes CATCH_STMTS.  */
15616
15617 static tree
15618 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15619                             tree catch_stmts)
15620 {
15621   tree try_block, catch_clause_param, catch_block, catch;
15622
15623   /* First build a try block */
15624   try_block = build_expr_block (try_stmts, NULL_TREE);
15625
15626   /* Build a catch block: we need a catch clause parameter */
15627   if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15628     {
15629       tree catch_type = obtain_incomplete_type (type_or_name);
15630       jdep *dep;
15631       catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15632       register_incomplete_type (JDEP_VARIABLE, type_or_name,
15633                                 catch_clause_param, catch_type);
15634       dep = CLASSD_LAST (ctxp->classd_list);
15635       JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15636     }
15637   else
15638     catch_clause_param = build_decl (VAR_DECL, wpv_id,
15639                                      build_pointer_type (type_or_name));
15640
15641   /* And a block */
15642   catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15643
15644   /* Initialize the variable and store in the block */
15645   catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15646                   build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15647   add_stmt_to_block (catch_block, NULL_TREE, catch);
15648
15649   /* Add the catch statements */
15650   add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15651
15652   /* Now we can build a JAVA_CATCH_EXPR */
15653   catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15654
15655   return build_try_statement (location, try_block, catch_block);
15656 }
15657
15658 static tree
15659 build_try_statement (int location, tree try_block, tree catches)
15660 {
15661   tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15662   EXPR_WFL_LINECOL (node) = location;
15663   return node;
15664 }
15665
15666 static tree
15667 build_try_finally_statement (int location, tree try_block, tree finally)
15668 {
15669   tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15670   EXPR_WFL_LINECOL (node) = location;
15671   return node;
15672 }
15673
15674 static tree
15675 patch_try_statement (tree node)
15676 {
15677   int error_found = 0;
15678   tree try = TREE_OPERAND (node, 0);
15679   /* Exception handlers are considered in left to right order */
15680   tree catch = nreverse (TREE_OPERAND (node, 1));
15681   tree current, caught_type_list = NULL_TREE;
15682
15683   /* Check catch clauses, if any. Every time we find an error, we try
15684      to process the next catch clause. We process the catch clause before
15685      the try block so that when processing the try block we can check thrown
15686      exceptions against the caught type list. */
15687   for (current = catch; current; current = TREE_CHAIN (current))
15688     {
15689       tree carg_decl, carg_type;
15690       tree sub_current, catch_block, catch_clause;
15691       int unreachable;
15692
15693       /* At this point, the structure of the catch clause is
15694            JAVA_CATCH_EXPR              (catch node)
15695              BLOCK              (with the decl of the parameter)
15696                COMPOUND_EXPR
15697                  MODIFY_EXPR   (assignment of the catch parameter)
15698                  BLOCK          (catch clause block)
15699        */
15700       catch_clause = TREE_OPERAND (current, 0);
15701       carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15702       carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15703
15704       /* Catch clauses can't have more than one parameter declared,
15705          but it's already enforced by the grammar. Make sure that the
15706          only parameter of the clause statement in of class Throwable
15707          or a subclass of Throwable, but that was done earlier. The
15708          catch clause parameter type has also been resolved. */
15709
15710       /* Just make sure that the catch clause parameter type inherits
15711          from java.lang.Throwable */
15712       if (!inherits_from_p (carg_type, throwable_type_node))
15713         {
15714           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15715           parse_error_context (wfl_operator,
15716                                "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15717                                lang_printable_name (carg_type, 0));
15718           error_found = 1;
15719           continue;
15720         }
15721
15722       /* Partial check for unreachable catch statement: The catch
15723          clause is reachable iff is no earlier catch block A in
15724          the try statement such that the type of the catch
15725          clause's parameter is the same as or a subclass of the
15726          type of A's parameter */
15727       unreachable = 0;
15728       for (sub_current = catch;
15729            sub_current != current; sub_current = TREE_CHAIN (sub_current))
15730         {
15731           tree sub_catch_clause, decl;
15732           sub_catch_clause = TREE_OPERAND (sub_current, 0);
15733           decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15734
15735           if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15736             {
15737               EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15738               parse_error_context
15739                 (wfl_operator,
15740                  "%<catch%> not reached because of the catch clause at line %d",
15741                  EXPR_WFL_LINENO (sub_current));
15742               unreachable = error_found = 1;
15743               break;
15744             }
15745         }
15746       /* Complete the catch clause block */
15747       catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15748       if (catch_block == error_mark_node)
15749         {
15750           error_found = 1;
15751           continue;
15752         }
15753       if (CAN_COMPLETE_NORMALLY (catch_block))
15754         CAN_COMPLETE_NORMALLY (node) = 1;
15755       TREE_OPERAND (current, 0) = catch_block;
15756
15757       if (unreachable)
15758         continue;
15759
15760       /* Things to do here: the exception must be thrown */
15761
15762       /* Link this type to the caught type list */
15763       caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15764     }
15765
15766   PUSH_EXCEPTIONS (caught_type_list);
15767   if ((try = java_complete_tree (try)) == error_mark_node)
15768     error_found = 1;
15769   if (CAN_COMPLETE_NORMALLY (try))
15770     CAN_COMPLETE_NORMALLY (node) = 1;
15771   POP_EXCEPTIONS ();
15772
15773   /* Verification ends here */
15774   if (error_found)
15775     return error_mark_node;
15776
15777   TREE_OPERAND (node, 0) = try;
15778   TREE_OPERAND (node, 1) = catch;
15779   TREE_TYPE (node) = void_type_node;
15780   return node;
15781 }
15782
15783 /* 14.17 The synchronized Statement */
15784
15785 static tree
15786 patch_synchronized_statement (tree node, tree wfl_op1)
15787 {
15788   tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15789   tree block = TREE_OPERAND (node, 1);
15790
15791   tree tmp, enter, exit, expr_decl, assignment;
15792
15793   if (expr == error_mark_node)
15794     {
15795       block = java_complete_tree (block);
15796       return expr;
15797     }
15798
15799   /* We might be trying to synchronize on a STRING_CST */
15800   if ((tmp = patch_string (expr)))
15801     expr = tmp;
15802
15803   /* The TYPE of expr must be a reference type */
15804   if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15805     {
15806       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15807       parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15808                            lang_printable_name (TREE_TYPE (expr), 0));
15809       return error_mark_node;
15810     }
15811
15812   /* Generate a try-finally for the synchronized statement, except
15813      that the handler that catches all throw exception calls
15814      _Jv_MonitorExit and then rethrow the exception.
15815      The synchronized statement is then implemented as:
15816      TRY
15817        {
15818          _Jv_MonitorEnter (expression)
15819          synchronized_block
15820          _Jv_MonitorExit (expression)
15821        }
15822      CATCH_ALL
15823        {
15824          e = _Jv_exception_info ();
15825          _Jv_MonitorExit (expression)
15826          Throw (e);
15827        } */
15828
15829   expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15830   BUILD_MONITOR_ENTER (enter, expr_decl);
15831   BUILD_MONITOR_EXIT (exit, expr_decl);
15832   CAN_COMPLETE_NORMALLY (enter) = 1;
15833   CAN_COMPLETE_NORMALLY (exit) = 1;
15834   assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15835   TREE_SIDE_EFFECTS (assignment) = 1;
15836   node = build2 (COMPOUND_EXPR, NULL_TREE,
15837                  build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15838                  build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15839   node = build_expr_block (node, expr_decl);
15840
15841   return java_complete_tree (node);
15842 }
15843
15844 /* 14.16 The throw Statement */
15845
15846 static tree
15847 patch_throw_statement (tree node, tree wfl_op1)
15848 {
15849   tree expr = TREE_OPERAND (node, 0);
15850   tree type = TREE_TYPE (expr);
15851   int unchecked_ok = 0, tryblock_throws_ok = 0;
15852
15853   /* Thrown expression must be assignable to java.lang.Throwable */
15854   if (!try_reference_assignconv (throwable_type_node, expr))
15855     {
15856       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15857       parse_error_context (wfl_operator,
15858     "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15859                            lang_printable_name (type, 0));
15860       /* If the thrown expression was a reference, we further the
15861          compile-time check. */
15862       if (!JREFERENCE_TYPE_P (type))
15863         return error_mark_node;
15864     }
15865
15866   /* At least one of the following must be true */
15867
15868   /* The type of the throw expression is a not checked exception,
15869      i.e. is a unchecked expression. */
15870   unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15871
15872   SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15873   /* An instance can't throw a checked exception unless that exception
15874      is explicitly declared in the `throws' clause of each
15875      constructor. This doesn't apply to anonymous classes, since they
15876      don't have declared constructors. */
15877   if (!unchecked_ok
15878       && DECL_INSTINIT_P (current_function_decl)
15879       && !ANONYMOUS_CLASS_P (current_class))
15880     {
15881       tree current;
15882       for (current = TYPE_METHODS (current_class); current;
15883            current = TREE_CHAIN (current))
15884         if (DECL_CONSTRUCTOR_P (current)
15885             && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15886           {
15887             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)",
15888                                  lang_printable_name (TREE_TYPE (expr), 0));
15889             return error_mark_node;
15890           }
15891     }
15892
15893   /* Throw is contained in a try statement and at least one catch
15894      clause can receive the thrown expression or the current method is
15895      declared to throw such an exception. Or, the throw statement is
15896      contained in a method or constructor declaration and the type of
15897      the Expression is assignable to at least one type listed in the
15898      throws clause the declaration. */
15899   if (!unchecked_ok)
15900     tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15901   if (!(unchecked_ok || tryblock_throws_ok))
15902     {
15903       /* If there is a surrounding try block that has no matching
15904          clatch clause, report it first. A surrounding try block exits
15905          only if there is something after the list of checked
15906          exception thrown by the current function (if any). */
15907       if (IN_TRY_BLOCK_P ())
15908         parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15909                              lang_printable_name (type, 0));
15910       /* If we have no surrounding try statement and the method doesn't have
15911          any throws, report it now. FIXME */
15912
15913       /* We report that the exception can't be throw from a try block
15914          in all circumstances but when the `throw' is inside a static
15915          block. */
15916       else if (!EXCEPTIONS_P (currently_caught_type_list)
15917                && !tryblock_throws_ok)
15918         {
15919           if (DECL_CLINIT_P (current_function_decl))
15920             parse_error_context (wfl_operator,
15921                    "Checked exception %qs can't be thrown in initializer",
15922                                  lang_printable_name (type, 0));
15923           else
15924             parse_error_context (wfl_operator,
15925                    "Checked exception %qs isn't thrown from a %<try%> block",
15926                                  lang_printable_name (type, 0));
15927         }
15928       /* Otherwise, the current method doesn't have the appropriate
15929          throws declaration */
15930       else
15931         parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15932                              lang_printable_name (type, 0));
15933       return error_mark_node;
15934     }
15935
15936   if (! flag_emit_class_files)
15937     BUILD_THROW (node, expr);
15938
15939   return node;
15940 }
15941
15942 /* Add EXCEPTION to the throws clause of MDECL.  If MDECL already throws
15943    a super-class of EXCEPTION, keep the superclass instead.  If MDECL already
15944    throws a sub-class of EXCEPTION, replace the sub-class with EXCEPTION.  */
15945 static void
15946 add_exception_to_throws (tree mdecl, tree exception)
15947 {
15948   tree mthrows;
15949   
15950   /* Ignore unchecked exceptions. */
15951   if (IS_UNCHECKED_EXCEPTION_P (exception))
15952     return;
15953
15954   for (mthrows = DECL_FUNCTION_THROWS (mdecl);
15955        mthrows; mthrows = TREE_CHAIN (mthrows))
15956     {
15957       if (inherits_from_p (exception, TREE_VALUE (mthrows)))
15958         return;
15959       if (inherits_from_p (TREE_VALUE (mthrows), exception))
15960         {
15961           TREE_VALUE (mthrows) = exception;
15962           return;
15963         }
15964     }
15965   
15966   mthrows = DECL_FUNCTION_THROWS (mdecl);
15967   DECL_FUNCTION_THROWS (mdecl) = build_tree_list (mthrows, exception);
15968 }
15969
15970 /* Check that exception said to be thrown by method DECL can be
15971    effectively caught from where DECL is invoked.  THIS_EXPR is the
15972    expression that computes `this' for the method call.  */
15973 static void
15974 check_thrown_exceptions (
15975 #ifdef USE_MAPPED_LOCATION
15976                          source_location location,
15977 #else
15978
15979                          int location,
15980 #endif
15981                          tree decl, tree this_expr)
15982 {
15983   tree throws;
15984   int is_array_call = 0;
15985
15986   /* Skip check within generated methods, such as access$<n>.  */
15987   if (NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15988     return;
15989
15990   if (this_expr != NULL_TREE
15991       && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15992       && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15993     is_array_call = 1;
15994
15995   /* For all the unchecked exceptions thrown by DECL.  */
15996   for (throws = DECL_FUNCTION_THROWS (decl); throws;
15997        throws = TREE_CHAIN (throws))
15998     if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15999       {
16000         /* Suppress errors about cloning arrays.  */
16001         if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
16002           continue;
16003
16004 #ifdef USE_MAPPED_LOCATION
16005         SET_EXPR_LOCATION (wfl_operator, location);
16006 #else
16007         EXPR_WFL_LINECOL (wfl_operator) = location;
16008 #endif
16009         if (ANONYMOUS_CLASS_P (DECL_CONTEXT (current_function_decl))
16010             && (DECL_FINIT_P (current_function_decl)
16011                 || DECL_INIT_P (current_function_decl)
16012                 || DECL_CONSTRUCTOR_P (current_function_decl)))
16013           {
16014             /* Add "throws" to the initializer's exception list */
16015             tree exception = TREE_VALUE (throws);
16016             add_exception_to_throws (current_function_decl, exception);   
16017           }
16018         else if (DECL_FINIT_P (current_function_decl))
16019           {
16020             parse_error_context
16021               (wfl_operator, "Exception %qs can't be thrown in initializer",
16022                lang_printable_name (TREE_VALUE (throws), 0));
16023           }
16024         else
16025           {
16026             parse_error_context
16027               (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
16028                lang_printable_name (TREE_VALUE (throws), 0),
16029                (DECL_INIT_P (current_function_decl) ?
16030                 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
16031                 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
16032           }
16033       }
16034 }
16035
16036 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
16037    try-catch blocks, OR is listed in the `throws' clause of the
16038    current method.  */
16039
16040 static int
16041 check_thrown_exceptions_do (tree exception)
16042 {
16043   tree list = currently_caught_type_list;
16044   resolve_and_layout (exception, NULL_TREE);
16045   /* First, all the nested try-catch-finally at that stage. The
16046      last element contains `throws' clause exceptions, if any. */
16047   if (IS_UNCHECKED_EXCEPTION_P (exception))
16048     return 1;
16049   while (list)
16050     {
16051       tree caught;
16052       for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
16053         if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
16054           return 1;
16055       list = TREE_CHAIN (list);
16056     }
16057   return 0;
16058 }
16059
16060 /* This function goes over all of CLASS_TYPE ctors and checks whether
16061    each of them features at least one unchecked exception in its
16062    `throws' clause. If it's the case, it returns `true', `false'
16063    otherwise.  */
16064
16065 static bool
16066 ctors_unchecked_throws_clause_p (tree class_type)
16067 {
16068   tree current;
16069
16070   for (current = TYPE_METHODS (class_type); current;
16071        current = TREE_CHAIN (current))
16072     {
16073       bool ctu = false; /* Ctor Throws Unchecked */
16074       if (DECL_CONSTRUCTOR_P (current))
16075         {
16076           tree throws;
16077           for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16078                throws = TREE_CHAIN (throws))
16079             if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16080               ctu = true;
16081         }
16082       /* We return false as we found one ctor that is unfit. */
16083       if (!ctu && DECL_CONSTRUCTOR_P (current))
16084         return false;
16085     }
16086   /* All ctors feature at least one unchecked exception in their
16087      `throws' clause. */
16088   return true;
16089 }
16090
16091 /* 15.24 Conditional Operator ?: */
16092
16093 static tree
16094 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16095 {
16096   tree cond = TREE_OPERAND (node, 0);
16097   tree op1 = TREE_OPERAND (node, 1);
16098   tree op2 = TREE_OPERAND (node, 2);
16099   tree resulting_type = NULL_TREE;
16100   tree t1, t2, patched;
16101   int error_found = 0;
16102
16103   /* The condition and operands of ?: might be StringBuffers crafted
16104      as a result of a string concatenation.  Obtain decent ones here.  */
16105   if ((patched = patch_string (cond)))
16106     TREE_OPERAND (node, 0) = cond = patched;
16107   if ((patched = patch_string (op1)))
16108     TREE_OPERAND (node, 1) = op1 = patched;
16109   if ((patched = patch_string (op2)))
16110     TREE_OPERAND (node, 2) = op2 = patched;
16111
16112   t1 = TREE_TYPE (op1);
16113   t2 = TREE_TYPE (op2);
16114
16115   /* The first expression must be a boolean */
16116   if (TREE_TYPE (cond) != boolean_type_node)
16117     {
16118       SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16119       parse_error_context (wfl_operator,
16120                "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16121                            lang_printable_name (TREE_TYPE (cond), 0));
16122       error_found = 1;
16123     }
16124
16125   /* Second and third can be numeric, boolean (i.e. primitive),
16126      references or null. Anything else results in an error */
16127   if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16128         || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16129             && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16130         || (t1 == boolean_type_node && t2 == boolean_type_node)))
16131     error_found = 1;
16132
16133   /* Determine the type of the conditional expression. Same types are
16134      easy to deal with */
16135   else if (t1 == t2)
16136     resulting_type = t1;
16137
16138   /* There are different rules for numeric types */
16139   else if (JNUMERIC_TYPE_P (t1))
16140     {
16141       /* if byte/short found, the resulting type is short */
16142       if ((t1 == byte_type_node && t2 == short_type_node)
16143           || (t1 == short_type_node && t2 == byte_type_node))
16144         resulting_type = short_type_node;
16145
16146       /* If t1 is a constant int and t2 is of type byte, short or char
16147          and t1's value fits in t2, then the resulting type is t2 */
16148       else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16149           && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16150         resulting_type = t2;
16151
16152       /* If t2 is a constant int and t1 is of type byte, short or char
16153          and t2's value fits in t1, then the resulting type is t1 */
16154       else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16155           && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16156         resulting_type = t1;
16157
16158       /* Otherwise, binary numeric promotion is applied and the
16159          resulting type is the promoted type of operand 1 and 2 */
16160       else
16161         resulting_type = binary_numeric_promotion (t1, t2,
16162                                                    &TREE_OPERAND (node, 1),
16163                                                    &TREE_OPERAND (node, 2));
16164     }
16165
16166   /* Cases of a reference and a null type */
16167   else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16168     resulting_type = t1;
16169
16170   else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16171     resulting_type = t2;
16172
16173   /* Last case: different reference types. If a type can be converted
16174      into the other one by assignment conversion, the latter
16175      determines the type of the expression */
16176   else if ((resulting_type = try_reference_assignconv (t1, op2)))
16177     resulting_type = promote_type (t1);
16178
16179   else if ((resulting_type = try_reference_assignconv (t2, op1)))
16180     resulting_type = promote_type (t2);
16181
16182   /* If we don't have any resulting type, we're in trouble */
16183   if (!resulting_type)
16184     {
16185       char *t = xstrdup (lang_printable_name (t1, 0));
16186       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16187       parse_error_context (wfl_operator,
16188                  "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16189                            t, lang_printable_name (t2, 0));
16190       free (t);
16191       error_found = 1;
16192     }
16193
16194   if (error_found)
16195     {
16196       TREE_TYPE (node) = error_mark_node;
16197       return error_mark_node;
16198     }
16199
16200   TREE_TYPE (node) = resulting_type;
16201   TREE_SET_CODE (node, COND_EXPR);
16202   CAN_COMPLETE_NORMALLY (node) = 1;
16203   return node;
16204 }
16205
16206 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16207
16208 static tree
16209 maybe_build_class_init_for_field (tree decl, tree expr)
16210 {
16211   tree clas = DECL_CONTEXT (decl);
16212   if (flag_emit_class_files)
16213     return expr;
16214
16215   if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16216       && FIELD_FINAL (decl))
16217     {
16218       tree init = DECL_INITIAL (decl);
16219       if (init != NULL_TREE)
16220         init = fold_constant_for_init (init, decl);
16221       if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16222         return expr;
16223     }
16224
16225   return build_class_init (clas, expr);
16226 }
16227
16228 /* Try to constant fold NODE.
16229    If NODE is not a constant expression, return NULL_EXPR.
16230    CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16231
16232 static tree
16233 fold_constant_for_init (tree node, tree context)
16234 {
16235   tree op0, op1, val;
16236   enum tree_code code = TREE_CODE (node);
16237
16238   switch (code)
16239     {
16240     case INTEGER_CST:
16241       if (node == null_pointer_node)
16242         return NULL_TREE;
16243     case STRING_CST:
16244     case REAL_CST:
16245       return node;
16246
16247     case PLUS_EXPR:
16248     case MINUS_EXPR:
16249     case MULT_EXPR:
16250     case TRUNC_MOD_EXPR:
16251     case RDIV_EXPR:
16252     case LSHIFT_EXPR:
16253     case RSHIFT_EXPR:
16254     case URSHIFT_EXPR:
16255     case BIT_AND_EXPR:
16256     case BIT_XOR_EXPR:
16257     case BIT_IOR_EXPR:
16258     case TRUTH_ANDIF_EXPR:
16259     case TRUTH_ORIF_EXPR:
16260     case EQ_EXPR:
16261     case NE_EXPR:
16262     case GT_EXPR:
16263     case GE_EXPR:
16264     case LT_EXPR:
16265     case LE_EXPR:
16266       op0 = TREE_OPERAND (node, 0);
16267       op1 = TREE_OPERAND (node, 1);
16268       val = fold_constant_for_init (op0, context);
16269       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16270         return NULL_TREE;
16271       TREE_OPERAND (node, 0) = val;
16272       val = fold_constant_for_init (op1, context);
16273       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16274         return NULL_TREE;
16275       TREE_OPERAND (node, 1) = val;
16276       return patch_binop (node, op0, op1, 1);
16277
16278     case UNARY_PLUS_EXPR:
16279     case NEGATE_EXPR:
16280     case TRUTH_NOT_EXPR:
16281     case BIT_NOT_EXPR:
16282     case CONVERT_EXPR:
16283     case NOP_EXPR:
16284       op0 = TREE_OPERAND (node, 0);
16285       val = fold_constant_for_init (op0, context);
16286       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16287         return NULL_TREE;
16288       TREE_OPERAND (node, 0) = val;
16289       val = patch_unaryop (node, op0);
16290       if (! TREE_CONSTANT (val))
16291         return NULL_TREE;
16292       return val;
16293
16294       break;
16295
16296     case COND_EXPR:
16297       val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16298       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16299         return NULL_TREE;
16300       TREE_OPERAND (node, 0) = val;
16301       val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16302       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16303         return NULL_TREE;
16304       TREE_OPERAND (node, 1) = val;
16305       val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16306       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16307         return NULL_TREE;
16308       TREE_OPERAND (node, 2) = val;
16309       return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 2)
16310         : TREE_OPERAND (node, 1);
16311
16312     case VAR_DECL:
16313     case FIELD_DECL:
16314       if (! FIELD_FINAL (node)
16315           || DECL_INITIAL (node) == NULL_TREE)
16316         return NULL_TREE;
16317       val = DECL_INITIAL (node);
16318       /* Guard against infinite recursion. */
16319       DECL_INITIAL (node) = NULL_TREE;
16320       val = fold_constant_for_init (val, node);
16321       if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16322         val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16323       DECL_INITIAL (node) = val;
16324       return val;
16325
16326     case EXPR_WITH_FILE_LOCATION:
16327       /* Compare java_complete_tree and resolve_expression_name. */
16328       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16329           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16330         {
16331           tree name = EXPR_WFL_NODE (node);
16332           tree decl;
16333           if (PRIMARY_P (node))
16334             return NULL_TREE;
16335           else if (! QUALIFIED_P (name))
16336             {
16337               decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16338               if (decl == NULL_TREE
16339                   || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16340                 return NULL_TREE;
16341               return fold_constant_for_init (decl, decl);
16342             }
16343           else
16344             {
16345               tree r = NULL_TREE;
16346               /* Install the proper context for the field resolution.  */
16347               tree saved_current_class = current_class;
16348               /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
16349               current_class = DECL_CONTEXT (context);
16350               qualify_ambiguous_name (node);
16351               r = resolve_field_access (node, &decl, NULL);
16352               /* Restore prior context.  */
16353               current_class = saved_current_class;
16354               if (r != error_mark_node && decl != NULL_TREE)
16355                 return fold_constant_for_init (decl, decl);
16356               return NULL_TREE;
16357             }
16358         }
16359       else
16360         {
16361           op0 = TREE_OPERAND (node, 0);
16362           val = fold_constant_for_init (op0, context);
16363           if (val == NULL_TREE || ! TREE_CONSTANT (val))
16364             return NULL_TREE;
16365           TREE_OPERAND (node, 0) = val;
16366           return val;
16367         }
16368
16369 #ifdef USE_COMPONENT_REF
16370     case IDENTIFIER:
16371     case COMPONENT_REF:
16372       ?;
16373 #endif
16374
16375     default:
16376       return NULL_TREE;
16377     }
16378 }
16379
16380 #ifdef USE_COMPONENT_REF
16381 /* Context is 'T' for TypeName, 'P' for PackageName,
16382    'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16383
16384 tree
16385 resolve_simple_name (tree name, int context)
16386 {
16387 }
16388
16389 tree
16390 resolve_qualified_name (tree name, int context)
16391 {
16392 }
16393 #endif
16394
16395 void
16396 init_src_parse (void)
16397 {
16398   /* Sanity check; we've been bit by this before.  */
16399   if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16400     abort ();
16401 }
16402
16403 \f
16404
16405 /* This section deals with the functions that are called when tables
16406    recording class initialization information are traversed.  */
16407
16408 /* This function is called for each class that is known definitely
16409    initialized when a given static method was called. This function
16410    augments a compound expression (INFO) storing all assignment to
16411    initialized static class flags if a flag already existed, otherwise
16412    a new one is created.  */
16413
16414 static int
16415 emit_test_initialization (void **entry_p, void *info)
16416 {
16417   tree l = (tree) info;
16418   tree decl, init;
16419   tree key = (tree) *entry_p;
16420   tree *ite;
16421   htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16422
16423   /* If we haven't found a flag and we're dealing with self registered
16424      with current_function_decl, then don't do anything. Self is
16425      always added as definitely initialized but this information is
16426      valid only if used outside the current function. */
16427   if (current_function_decl == TREE_PURPOSE (l)
16428       && java_treetreehash_find (cf_ht, key) == NULL)
16429     return true;
16430
16431   ite = java_treetreehash_new (cf_ht, key);
16432
16433   /* If we don't have a variable, create one and install it. */
16434   if (*ite == NULL)
16435     {
16436       tree block;
16437
16438       decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16439       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16440       LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16441       DECL_CONTEXT (decl) = current_function_decl;
16442       DECL_INITIAL (decl) = boolean_true_node;
16443       /* Don't emit any symbolic debugging info for this decl.  */
16444       DECL_IGNORED_P (decl) = 1;
16445
16446       /* The trick is to find the right context for it. */
16447       block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16448       TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16449       BLOCK_EXPR_DECLS (block) = decl;
16450       *ite = decl;
16451     }
16452   else
16453     decl = *ite;
16454
16455   /* Now simply augment the compound that holds all the assignments
16456      pertaining to this method invocation. */
16457   init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16458   TREE_SIDE_EFFECTS (init) = 1;
16459   TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16460   TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16461
16462   return true;
16463 }
16464
16465 #ifdef __XGETTEXT__
16466 /* Depending on the version of Bison used to compile this grammar,
16467    it may issue generic diagnostics spelled "syntax error" or
16468    "parse error".  To prevent this from changing the translation
16469    template randomly, we list all the variants of this particular
16470    diagnostic here.  Translators: there is no fine distinction
16471    between diagnostics with "syntax error" in them, and diagnostics
16472    with "parse error" in them.  It's okay to give them both the same
16473    translation.  */
16474 const char d1[] = N_("syntax error");
16475 const char d2[] = N_("parse error");
16476 const char d3[] = N_("syntax error; also virtual memory exhausted");
16477 const char d4[] = N_("parse error; also virtual memory exhausted");
16478 const char d5[] = N_("syntax error: cannot back up");
16479 const char d6[] = N_("parse error: cannot back up");
16480 #endif
16481
16482 #include "gt-java-parse.h"
16483 #include "gtype-java.h"