OSDN Git Service

PR java/19070
[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, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
23
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
27
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
30 language).
31
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
35
36 The following modifications were brought to the original grammar:
37
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
40
41 Note: All the extra rules described above should go away when the
42       empty_statement rule will work.
43
44 statement_nsi: 'nsi' should be read no_short_if.
45
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions.  */
48
49 %{
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include <dirent.h>
55 #include "tree.h"
56 #include "rtl.h"
57 #include "real.h"
58 #include "obstack.h"
59 #include "toplev.h"
60 #include "pretty-print.h"
61 #include "diagnostic.h"
62 #include "flags.h"
63 #include "java-tree.h"
64 #include "jcf.h"
65 #include "lex.h"
66 #include "parse.h"
67 #include "zipfile.h"
68 #include "convert.h"
69 #include "buffer.h"
70 #include "xref.h"
71 #include "function.h"
72 #include "except.h"
73 #include "ggc.h"
74 #include "debug.h"
75 #include "tree-inline.h"
76 #include "cgraph.h"
77 #include "target.h"
78
79 /* Local function prototypes */
80 static char *java_accstring_lookup (int);
81 static const char *accessibility_string (int);
82 static void  classitf_redefinition_error (const char *,tree, tree, tree);
83 static void  variable_redefinition_error (tree, tree, tree, int);
84 static tree  create_class (int, tree, tree, tree);
85 static tree  create_interface (int, tree, tree);
86 static void  end_class_declaration (int);
87 static tree  find_field (tree, tree);
88 static tree lookup_field_wrapper (tree, tree);
89 static int   duplicate_declaration_error_p (tree, tree, tree);
90 static void  register_fields (int, tree, tree);
91 static tree parser_qualified_classname (tree);
92 static int  parser_check_super (tree, tree, tree);
93 static int  parser_check_super_interface (tree, tree, tree);
94 static void check_modifiers_consistency (int);
95 static tree lookup_cl (tree);
96 static tree lookup_java_method2 (tree, tree, int);
97 static tree method_header (int, tree, tree, tree);
98 static void fix_method_argument_names (tree ,tree);
99 static tree method_declarator (tree, tree);
100 static void parse_warning_context (tree cl, const char *msgid, ...);
101 #ifdef USE_MAPPED_LOCATION
102 static void issue_warning_error_from_context
103   (source_location, const char *msgid, va_list *);
104 #else
105 static void issue_warning_error_from_context
106   (tree, const char *msgid, va_list *);
107 #endif
108 static void parse_ctor_invocation_error (void);
109 static tree parse_jdk1_1_error (const char *);
110 static void complete_class_report_errors (jdep *);
111 static int process_imports (void);
112 static void read_import_dir (tree);
113 static int find_in_imports_on_demand (tree, tree);
114 static void find_in_imports (tree, tree);
115 static void check_inner_class_access (tree, tree, tree);
116 static int check_pkg_class_access (tree, tree, bool, tree);
117 static void register_package (tree);
118 static tree resolve_package (tree, tree *, tree *);
119 static tree resolve_class (tree, tree, tree, tree);
120 static void declare_local_variables (int, tree, tree);
121 static void dump_java_tree (enum tree_dump_index, tree);
122 static void source_start_java_method (tree);
123 static void source_end_java_method (void);
124 static tree find_name_in_single_imports (tree);
125 static void check_abstract_method_header (tree);
126 static tree lookup_java_interface_method2 (tree, tree);
127 static tree resolve_expression_name (tree, tree *);
128 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
129 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
130 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
131 static tree resolve_and_layout (tree, tree);
132 static tree qualify_and_find (tree, tree, tree);
133 static tree resolve_no_layout (tree, tree);
134 static int invocation_mode (tree, int);
135 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
136 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
137 static tree find_most_specific_methods_list (tree);
138 static int argument_types_convertible (tree, tree);
139 static tree patch_invoke (tree, tree, tree);
140 static int maybe_use_access_method (int, tree *, tree *);
141 static tree lookup_method_invoke (int, tree, tree, tree, tree);
142 static tree register_incomplete_type (int, tree, tree, tree);
143 static tree check_inner_circular_reference (tree, tree);
144 static tree check_circular_reference (tree);
145 static tree obtain_incomplete_type (tree);
146 static tree java_complete_lhs (tree);
147 static tree java_complete_tree (tree);
148 static tree maybe_generate_pre_expand_clinit (tree);
149 static int analyze_clinit_body (tree, tree);
150 static int maybe_yank_clinit (tree);
151 static void start_complete_expand_method (tree);
152 static void java_complete_expand_method (tree);
153 static void java_expand_method_bodies (tree);
154 static int  unresolved_type_p (tree, tree *);
155 static void create_jdep_list (struct parser_ctxt *);
156 static tree build_expr_block (tree, tree);
157 static tree enter_block (void);
158 static tree exit_block (void);
159 static tree lookup_name_in_blocks (tree);
160 static void maybe_absorb_scoping_blocks (void);
161 static tree build_method_invocation (tree, tree);
162 static tree build_new_invocation (tree, tree);
163 static tree build_assignment (int, int, tree, tree);
164 static tree build_binop (enum tree_code, int, tree, tree);
165 static tree patch_assignment (tree, tree);
166 static tree patch_binop (tree, tree, tree);
167 static tree build_unaryop (int, int, tree);
168 static tree build_incdec (int, int, tree, int);
169 static tree patch_unaryop (tree, tree);
170 static tree build_cast (int, tree, tree);
171 static tree build_null_of_type (tree);
172 static tree patch_cast (tree, tree);
173 static int valid_ref_assignconv_cast_p (tree, tree, int);
174 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
175 static int valid_cast_to_p (tree, tree);
176 static int valid_method_invocation_conversion_p (tree, tree);
177 static tree try_builtin_assignconv (tree, tree, tree);
178 static tree try_reference_assignconv (tree, tree);
179 static tree build_unresolved_array_type (tree);
180 static int build_type_name_from_array_name (tree, tree *);
181 static tree build_array_from_name (tree, tree, tree, tree *);
182 static tree build_array_ref (int, tree, tree);
183 static tree patch_array_ref (tree);
184 #ifdef USE_MAPPED_LOCATION
185 static tree make_qualified_name (tree, tree, source_location);
186 #else
187 static tree make_qualified_name (tree, tree, int);
188 #endif
189 static tree merge_qualified_name (tree, tree);
190 static tree make_qualified_primary (tree, tree, int);
191 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
192 static void qualify_ambiguous_name (tree);
193 static tree resolve_field_access (tree, tree *, tree *);
194 static tree build_newarray_node (tree, tree, int);
195 static tree patch_newarray (tree);
196 static tree resolve_type_during_patch (tree);
197 static tree build_this (int);
198 static tree build_wfl_wrap (tree, int);
199 static tree build_return (int, tree);
200 static tree patch_return (tree);
201 static tree maybe_access_field (tree, tree, tree);
202 static int complete_function_arguments (tree);
203 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
204 static int not_accessible_p (tree, tree, tree, int);
205 static void check_deprecation (tree, tree);
206 static int class_in_current_package (tree);
207 static tree build_if_else_statement (int, tree, tree, tree);
208 static tree patch_if_else_statement (tree);
209 static tree add_stmt_to_block (tree, tree, tree);
210 static tree patch_exit_expr (tree);
211 static tree build_labeled_block (int, tree);
212 static tree finish_labeled_statement (tree, tree);
213 static tree build_bc_statement (int, int, tree);
214 static tree patch_bc_statement (tree);
215 static tree patch_loop_statement (tree);
216 static tree build_new_loop (tree);
217 static tree build_loop_body (int, tree, int);
218 static tree finish_loop_body (int, tree, tree, int);
219 static tree build_debugable_stmt (int, tree);
220 static tree finish_for_loop (int, tree, tree, tree);
221 static tree patch_switch_statement (tree);
222 static tree string_constant_concatenation (tree, tree);
223 static tree build_string_concatenation (tree, tree);
224 static tree patch_string_cst (tree);
225 static tree patch_string (tree);
226 static tree encapsulate_with_try_catch (int, tree, tree, tree);
227 #ifdef USE_MAPPED_LOCATION
228 static tree build_assertion (source_location, tree, tree);
229 #else
230 static tree build_assertion (int, tree, tree);
231 #endif
232 static tree build_try_statement (int, tree, tree);
233 static tree build_try_finally_statement (int, tree, tree);
234 static tree patch_try_statement (tree);
235 static tree patch_synchronized_statement (tree, tree);
236 static tree patch_throw_statement (tree, tree);
237 #ifdef USE_MAPPED_LOCATION
238 static void check_thrown_exceptions (source_location, tree, tree);
239 #else
240 static void check_thrown_exceptions (int, tree, tree);
241 #endif
242 static int check_thrown_exceptions_do (tree);
243 static void purge_unchecked_exceptions (tree);
244 static bool ctors_unchecked_throws_clause_p (tree);
245 static void check_concrete_throws_clauses (tree, tree, tree, tree);
246 static void check_throws_clauses (tree, tree, tree);
247 static void finish_method_declaration (tree);
248 static tree build_super_invocation (tree);
249 static int verify_constructor_circularity (tree, tree);
250 static char *constructor_circularity_msg (tree, tree);
251 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
252 static const char *get_printable_method_name (tree);
253 static tree patch_conditional_expr (tree, tree, tree);
254 static tree generate_finit (tree);
255 static tree generate_instinit (tree);
256 static tree build_instinit_invocation (tree);
257 static void fix_constructors (tree);
258 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
259 static tree craft_constructor (tree, tree);
260 static int verify_constructor_super (tree);
261 static tree create_artificial_method (tree, int, tree, tree, tree);
262 static void start_artificial_method_body (tree);
263 static void end_artificial_method_body (tree);
264 static int check_method_redefinition (tree, tree);
265 static int check_method_types_complete (tree);
266 static bool hack_is_accessible_p (tree, tree);
267 static void java_check_regular_methods (tree);
268 static void check_interface_throws_clauses (tree, tree);
269 static void java_check_abstract_methods (tree);
270 static void unreachable_stmt_error (tree);
271 static int not_accessible_field_error (tree, tree);
272 static tree find_expr_with_wfl (tree);
273 static void missing_return_error (tree);
274 static tree build_new_array_init (int, tree);
275 static tree patch_new_array_init (tree, tree);
276 static tree maybe_build_array_element_wfl (tree);
277 static int array_constructor_check_entry (tree, tree);
278 static const char *purify_type_name (const char *);
279 static tree fold_constant_for_init (tree, tree);
280 static 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 (htab_t, 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_outer_field_access (tree, tree);
326 static tree build_outer_field_access_methods (tree);
327 static tree build_outer_field_access_expr (int, tree, tree,
328                                                   tree, tree);
329 static tree build_outer_method_access_method (tree);
330 static tree build_new_access_id (void);
331 static tree build_outer_field_access_method (tree, tree, tree,
332                                                     tree, tree);
333
334 static int outer_field_access_p (tree, tree);
335 static int outer_field_expanded_access_p (tree, tree *,
336                                                  tree *, tree *);
337 static tree outer_field_access_fix (tree, tree, tree);
338 static tree build_incomplete_class_ref (int, tree);
339 static tree patch_incomplete_class_ref (tree);
340 static tree create_anonymous_class (tree);
341 static void patch_anonymous_class (tree, tree, tree);
342 static void add_inner_class_fields (tree, tree);
343
344 static tree build_dot_class_method (tree);
345 static tree build_dot_class_method_invocation (tree, tree);
346 static void create_new_parser_context (int);
347 static tree maybe_build_class_init_for_field (tree, tree);
348
349 static int emit_test_initialization (void **, void *);
350
351 static char *string_convert_int_cst (tree);
352
353 /* Number of error found so far. */
354 int java_error_count;
355 /* Number of warning found so far. */
356 int java_warning_count;
357 /* Tell when not to fold, when doing xrefs */
358 int do_not_fold;
359 /* Cyclic inheritance report, as it can be set by layout_class */
360 const char *cyclic_inheritance_report;
361
362 /* The current parser context */
363 struct parser_ctxt *ctxp;
364
365 /* List of things that were analyzed for which code will be generated */
366 struct parser_ctxt *ctxp_for_generation = NULL;
367
368 /* binop_lookup maps token to tree_code. It is used where binary
369    operations are involved and required by the parser. RDIV_EXPR
370    covers both integral/floating point division. The code is changed
371    once the type of both operator is worked out.  */
372
373 static const enum tree_code binop_lookup[19] =
374   {
375     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
376     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
377     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
378     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
379     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
380    };
381 #define BINOP_LOOKUP(VALUE)                                             \
382   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
383
384 /* This is the end index for binary operators that can also be used
385    in compound assignments. */
386 #define BINOP_COMPOUND_CANDIDATES 11
387
388 /* The "$L" identifier we use to create labels.  */
389 static GTY(()) tree label_id;
390
391 /* The "StringBuffer" identifier used for the String `+' operator. */
392 static GTY(()) tree wfl_string_buffer;
393
394 /* The "append" identifier used for String `+' operator.  */
395 static GTY(()) tree wfl_append;
396
397 /* The "toString" identifier used for String `+' operator. */
398 static GTY(()) tree wfl_to_string;
399
400 /* The "java.lang" import qualified name.  */
401 static GTY(()) tree java_lang_id;
402
403 /* The generated `inst$' identifier used for generated enclosing
404    instance/field access functions.  */
405 static GTY(()) tree inst_id;
406
407 /* Context and flag for static blocks */
408 static GTY(()) tree current_static_block;
409
410 /* The generated `write_parm_value$' identifier.  */
411 static GTY(()) tree wpv_id;
412
413 /* The list of all packages we've seen so far */
414 static GTY(()) tree package_list;
415
416 /* Hold THIS for the scope of the current method decl.  */
417 static GTY(()) tree current_this;
418
419 /* Hold a list of catch clauses list. The first element of this list is
420    the list of the catch clauses of the currently analyzed try block. */
421 static GTY(()) tree currently_caught_type_list;
422
423 /* This holds a linked list of all the case labels for the current
424    switch statement.  It is only used when checking to see if there
425    are duplicate labels.  FIXME: probably this should just be attached
426    to the switch itself; then it could be referenced via
427    `ctxp->current_loop'.  */
428 static GTY(()) tree case_label_list;
429
430 /* Anonymous class counter. Will be reset to 1 every time a non
431    anonymous class gets created. */
432 static int anonymous_class_counter = 1;
433
434 static GTY(()) tree src_parse_roots[1];
435
436 /* All classes seen from source code */
437 #define gclass_list src_parse_roots[0]
438
439 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
440    line and point it out.  */
441 /* Should point out the one that don't fit. ASCII/unicode, going
442    backward. FIXME */
443
444 #define check_modifiers(__message, __value, __mask) do {        \
445   if ((__value) & ~(__mask))                                    \
446     {                                                           \
447       size_t i, remainder = (__value) & ~(__mask);              \
448       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)     \
449         if ((1 << i) & remainder)                               \
450           parse_error_context (ctxp->modifier_ctx [i], (__message), \
451                                java_accstring_lookup (1 << i)); \
452     }                                                           \
453 } while (0)
454
455 %}
456
457 %union {
458   tree node;
459   int sub_token;
460   struct {
461     int token;
462 #ifdef USE_MAPPED_LOCATION
463     source_location location;
464 #else
465     int location;
466 #endif
467   } operator;
468   int value;
469 }
470
471 %{
472 #ifdef USE_MAPPED_LOCATION
473 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
474   SET_EXPR_LOCATION(EXPR, (TOKEN).location)
475 #else
476 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
477   (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
478 #endif
479
480 #include "lex.c"
481 %}
482
483 %pure_parser
484
485 /* Things defined here have to match the order of what's in the
486    binop_lookup table.  */
487
488 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
489 %token   LS_TK           SRS_TK          ZRS_TK
490 %token   AND_TK          XOR_TK          OR_TK
491 %token   BOOL_AND_TK BOOL_OR_TK
492 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
493
494 /* This maps to the same binop_lookup entry than the token above */
495
496 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
497 %token   REM_ASSIGN_TK
498 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
499 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
500
501
502 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
503
504 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
505 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
506 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
507 %token   PAD_TK          ABSTRACT_TK        STRICT_TK
508 %token   MODIFIER_TK
509
510 /* Keep those two in order, too */
511 %token   DECR_TK INCR_TK
512
513 /* From now one, things can be in any order */
514
515 %token   DEFAULT_TK      IF_TK              THROW_TK
516 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
517 %token   THROWS_TK       BREAK_TK           IMPORT_TK
518 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
519 %token   VOID_TK         CATCH_TK           INTERFACE_TK
520 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
521 %token   SUPER_TK        WHILE_TK           CLASS_TK
522 %token   SWITCH_TK       CONST_TK           TRY_TK
523 %token   FOR_TK          NEW_TK             CONTINUE_TK
524 %token   GOTO_TK         PACKAGE_TK         THIS_TK
525 %token   ASSERT_TK
526
527 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
528 %token   CHAR_TK         INTEGRAL_TK
529
530 %token   FLOAT_TK        DOUBLE_TK          FP_TK
531
532 %token   ID_TK
533
534 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
535
536 %token   ASSIGN_ANY_TK   ASSIGN_TK
537 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
538
539 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
540 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
541
542 %type    <value>        modifiers MODIFIER_TK final synchronized
543
544 %type    <node>         super ID_TK identifier
545 %type    <node>         name simple_name qualified_name
546 %type    <node>         type_declaration compilation_unit
547                         field_declaration method_declaration extends_interfaces
548                         interfaces interface_type_list
549                         import_declarations package_declaration
550                         type_declarations interface_body
551                         interface_member_declaration constant_declaration
552                         interface_member_declarations interface_type
553                         abstract_method_declaration
554 %type    <node>         class_body_declaration class_member_declaration
555                         static_initializer constructor_declaration block
556 %type    <node>         class_body_declarations constructor_header
557 %type    <node>         class_or_interface_type class_type class_type_list
558                         constructor_declarator explicit_constructor_invocation
559 %type    <node>         dim_expr dim_exprs this_or_super throws
560
561 %type    <node>         variable_declarator_id variable_declarator
562                         variable_declarators variable_initializer
563                         variable_initializers constructor_body
564                         array_initializer
565
566 %type    <node>         class_body block_end constructor_block_end
567 %type    <node>         statement statement_without_trailing_substatement
568                         labeled_statement if_then_statement label_decl
569                         if_then_else_statement while_statement for_statement
570                         statement_nsi labeled_statement_nsi do_statement
571                         if_then_else_statement_nsi while_statement_nsi
572                         for_statement_nsi statement_expression_list for_init
573                         for_update statement_expression expression_statement
574                         primary_no_new_array expression primary array_type
575                         array_creation_initialized array_creation_uninitialized
576                         class_instance_creation_expression field_access
577                         method_invocation array_access something_dot_new
578                         argument_list postfix_expression while_expression
579                         post_increment_expression post_decrement_expression
580                         unary_expression_not_plus_minus unary_expression
581                         pre_increment_expression pre_decrement_expression
582                         cast_expression
583                         multiplicative_expression additive_expression
584                         shift_expression relational_expression
585                         equality_expression and_expression
586                         exclusive_or_expression inclusive_or_expression
587                         conditional_and_expression conditional_or_expression
588                         conditional_expression assignment_expression
589                         left_hand_side assignment for_header for_begin
590                         constant_expression do_statement_begin empty_statement
591                         switch_statement synchronized_statement throw_statement
592                         try_statement assert_statement
593                         switch_expression switch_block
594                         catches catch_clause catch_clause_parameter finally
595                         anonymous_class_creation trap_overflow_corner_case
596 %type    <node>         return_statement break_statement continue_statement
597
598 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK
599 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
600 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
601 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
602 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
603 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
604 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
605 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
606 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
607 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
608 %type    <operator>     THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
609 %type    <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
610 %type    <operator>     NEW_TK ASSERT_TK
611
612 %type    <node>         method_body
613
614 %type    <node>         literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
615                         STRING_LIT_TK NULL_TK VOID_TK
616
617 %type    <node>         IF_TK WHILE_TK FOR_TK
618
619 %type    <node>         formal_parameter_list formal_parameter
620                         method_declarator method_header
621
622 %type    <node>         primitive_type reference_type type
623                         BOOLEAN_TK INTEGRAL_TK FP_TK
624
625 /* Added or modified JDK 1.1 rule types  */
626 %type    <node>         type_literals
627
628 %%
629 /* 19.2 Production from 2.3: The Syntactic Grammar  */
630 goal:  compilation_unit
631                 {}
632 ;
633
634 /* 19.3 Productions from 3: Lexical structure  */
635 literal:
636         INT_LIT_TK
637 |       FP_LIT_TK
638 |       BOOL_LIT_TK
639 |       CHAR_LIT_TK
640 |       STRING_LIT_TK
641 |       NULL_TK
642 ;
643
644 /* 19.4 Productions from 4: Types, Values and Variables  */
645 type:
646         primitive_type
647 |       reference_type
648 ;
649
650 primitive_type:
651         INTEGRAL_TK
652 |       FP_TK
653 |       BOOLEAN_TK
654 ;
655
656 reference_type:
657         class_or_interface_type
658 |       array_type
659 ;
660
661 class_or_interface_type:
662         name
663 ;
664
665 class_type:
666         class_or_interface_type /* Default rule */
667 ;
668
669 interface_type:
670          class_or_interface_type
671 ;
672
673 array_type:
674         primitive_type dims
675                 {
676                   int osb = pop_current_osb (ctxp);
677                   tree t = build_java_array_type (($1), -1);
678                   while (--osb)
679                     t = build_unresolved_array_type (t);
680                   $$ = t;
681                 }
682 |       name dims
683                 {
684                   int osb = pop_current_osb (ctxp);
685                   tree t = $1;
686                   while (osb--)
687                     t = build_unresolved_array_type (t);
688                   $$ = t;
689                 }
690 ;
691
692 /* 19.5 Productions from 6: Names  */
693 name:
694         simple_name             /* Default rule */
695 |       qualified_name          /* Default rule */
696 ;
697
698 simple_name:
699         identifier              /* Default rule */
700 ;
701
702 qualified_name:
703         name DOT_TK identifier
704                 { $$ = make_qualified_name ($1, $3, $2.location); }
705 ;
706
707 identifier:
708         ID_TK
709 ;
710
711 /* 19.6: Production from 7: Packages  */
712 compilation_unit:
713                 {$$ = NULL;}
714 |       package_declaration
715 |       import_declarations
716 |       type_declarations
717 |       package_declaration import_declarations
718 |       package_declaration type_declarations
719 |       import_declarations type_declarations
720 |       package_declaration import_declarations type_declarations
721 ;
722
723 import_declarations:
724         import_declaration
725                 {
726                   $$ = NULL;
727                 }
728 |       import_declarations import_declaration
729                 {
730                   $$ = NULL;
731                 }
732 ;
733
734 type_declarations:
735         type_declaration
736 |       type_declarations type_declaration
737 ;
738
739 package_declaration:
740         PACKAGE_TK name SC_TK
741                 {
742                   ctxp->package = EXPR_WFL_NODE ($2);
743                   register_package (ctxp->package);
744                 }
745 |       PACKAGE_TK error
746                 {yyerror ("Missing name"); RECOVER;}
747 |       PACKAGE_TK name error
748                 {yyerror ("';' expected"); RECOVER;}
749 ;
750
751 import_declaration:
752         single_type_import_declaration
753 |       type_import_on_demand_declaration
754 ;
755
756 single_type_import_declaration:
757         IMPORT_TK name SC_TK
758                 {
759                   tree name = EXPR_WFL_NODE ($2), last_name;
760                   int   i = IDENTIFIER_LENGTH (name)-1;
761                   const char *last = &IDENTIFIER_POINTER (name)[i];
762                   while (last != IDENTIFIER_POINTER (name))
763                     {
764                       if (last [0] == '.')
765                         break;
766                       last--;
767                     }
768                   last_name = get_identifier (++last);
769                   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
770                     {
771                       tree err = find_name_in_single_imports (last_name);
772                       if (err && err != name)
773                         parse_error_context
774                           ($2, "Ambiguous class: %qs and %qs",
775                            IDENTIFIER_POINTER (name),
776                            IDENTIFIER_POINTER (err));
777                       else
778                         REGISTER_IMPORT ($2, last_name);
779                     }
780                   else
781                     REGISTER_IMPORT ($2, last_name);
782                 }
783 |       IMPORT_TK error
784                 {yyerror ("Missing name"); RECOVER;}
785 |       IMPORT_TK name error
786                 {yyerror ("';' expected"); RECOVER;}
787 ;
788
789 type_import_on_demand_declaration:
790         IMPORT_TK name DOT_TK MULT_TK SC_TK
791                 {
792                   tree name = EXPR_WFL_NODE ($2);
793                   tree it;
794                   /* Search for duplicates. */
795                   for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
796                     if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
797                       break;
798                   /* Don't import the same thing more than once, just ignore
799                      duplicates (7.5.2) */
800                   if (! it)
801                     {
802                       read_import_dir ($2);
803                       ctxp->import_demand_list =
804                         chainon (ctxp->import_demand_list,
805                                  build_tree_list ($2, NULL_TREE));
806                     }
807                 }
808 |       IMPORT_TK name DOT_TK error
809                 {yyerror ("'*' expected"); RECOVER;}
810 |       IMPORT_TK name DOT_TK MULT_TK error
811                 {yyerror ("';' expected"); RECOVER;}
812 ;
813
814 type_declaration:
815         class_declaration
816                 { end_class_declaration (0); }
817 |       interface_declaration
818                 { end_class_declaration (0); }
819 |       empty_statement
820 |       error
821                 {
822                   YYERROR_NOW;
823                   yyerror ("Class or interface declaration expected");
824                 }
825 ;
826
827 /* 19.7 Shortened from the original:
828    modifiers: modifier | modifiers modifier
829    modifier: any of public...  */
830 modifiers:
831         MODIFIER_TK
832                 {
833                   $$ = (1 << $1);
834                 }
835 |       modifiers MODIFIER_TK
836                 {
837                   int acc = (1 << $2);
838                   if ($$ & acc)
839                     parse_error_context
840                       (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
841                        java_accstring_lookup (acc));
842                   else
843                     {
844                       $$ |= acc;
845                     }
846                 }
847 ;
848
849 /* 19.8.1 Production from $8.1: Class Declaration */
850 class_declaration:
851         modifiers CLASS_TK identifier super interfaces
852                 { create_class ($1, $3, $4, $5); }
853         class_body
854                 {;}
855 |       CLASS_TK identifier super interfaces
856                 { create_class (0, $2, $3, $4); }
857         class_body
858                 {;}
859 |       modifiers CLASS_TK error
860                 { yyerror ("Missing class name"); RECOVER; }
861 |       CLASS_TK error
862                 { yyerror ("Missing class name"); RECOVER; }
863 |       CLASS_TK identifier error
864                 {
865                   if (!ctxp->class_err) yyerror ("'{' expected");
866                   DRECOVER(class1);
867                 }
868 |       modifiers CLASS_TK identifier error
869                 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
870 ;
871
872 super:
873                 { $$ = NULL; }
874 |       EXTENDS_TK class_type
875                 { $$ = $2; }
876 |       EXTENDS_TK class_type error
877                 {yyerror ("'{' expected"); ctxp->class_err=1;}
878 |       EXTENDS_TK error
879                 {yyerror ("Missing super class name"); ctxp->class_err=1;}
880 ;
881
882 interfaces:
883                 { $$ = NULL_TREE; }
884 |       IMPLEMENTS_TK interface_type_list
885                 { $$ = $2; }
886 |       IMPLEMENTS_TK error
887                 {
888                   ctxp->class_err=1;
889                   yyerror ("Missing interface name");
890                 }
891 ;
892
893 interface_type_list:
894         interface_type
895                 {
896                   ctxp->interface_number = 1;
897                   $$ = build_tree_list ($1, NULL_TREE);
898                 }
899 |       interface_type_list C_TK interface_type
900                 {
901                   ctxp->interface_number++;
902                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
903                 }
904 |       interface_type_list C_TK error
905                 {yyerror ("Missing interface name"); RECOVER;}
906 ;
907
908 class_body:
909         OCB_TK CCB_TK
910                 {
911                   /* Store the location of the `}' when doing xrefs */
912                   if (flag_emit_xref)
913                     DECL_END_SOURCE_LINE (GET_CPC ()) = $2.location;
914                   $$ = GET_CPC ();
915                 }
916 |       OCB_TK class_body_declarations CCB_TK
917                 {
918                   /* Store the location of the `}' when doing xrefs */
919                   if (flag_emit_xref)
920                     DECL_END_SOURCE_LINE (GET_CPC ()) = $3.location;
921                   $$ = GET_CPC ();
922                 }
923 ;
924
925 class_body_declarations:
926         class_body_declaration
927 |       class_body_declarations class_body_declaration
928 ;
929
930 class_body_declaration:
931         class_member_declaration
932 |       static_initializer
933 |       constructor_declaration
934 |       block                   /* Added, JDK1.1, instance initializer */
935                 {
936                   if (!IS_EMPTY_STMT ($1))
937                     {
938                       TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
939                       SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
940                     }
941                 }
942 ;
943
944 class_member_declaration:
945         field_declaration
946 |       method_declaration
947 |       class_declaration       /* Added, JDK1.1 inner classes */
948                 { end_class_declaration (1); }
949 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
950                 { end_class_declaration (1); }
951 |       empty_statement
952 ;
953
954 /* 19.8.2 Productions from 8.3: Field Declarations  */
955 field_declaration:
956         type variable_declarators SC_TK
957                 { register_fields (0, $1, $2); }
958 |       modifiers type variable_declarators SC_TK
959                 {
960                   check_modifiers
961                     ("Illegal modifier %qs for field declaration",
962                      $1, FIELD_MODIFIERS);
963                   check_modifiers_consistency ($1);
964                   register_fields ($1, $2, $3);
965                 }
966 ;
967
968 variable_declarators:
969         /* Should we use build_decl_list () instead ? FIXME */
970         variable_declarator     /* Default rule */
971 |       variable_declarators C_TK variable_declarator
972                 { $$ = chainon ($1, $3); }
973 |       variable_declarators C_TK error
974                 {yyerror ("Missing term"); RECOVER;}
975 ;
976
977 variable_declarator:
978         variable_declarator_id
979                 { $$ = build_tree_list ($1, NULL_TREE); }
980 |       variable_declarator_id ASSIGN_TK variable_initializer
981                 {
982                   if (java_error_count)
983                     $3 = NULL_TREE;
984                   $$ = build_tree_list
985                     ($1, build_assignment ($2.token, $2.location, $1, $3));
986                 }
987 |       variable_declarator_id ASSIGN_TK error
988                 {
989                   yyerror ("Missing variable initializer");
990                   $$ = build_tree_list ($1, NULL_TREE);
991                   RECOVER;
992                 }
993 |       variable_declarator_id ASSIGN_TK variable_initializer error
994                 {
995                   yyerror ("';' expected");
996                   $$ = build_tree_list ($1, NULL_TREE);
997                   RECOVER;
998                 }
999 ;
1000
1001 variable_declarator_id:
1002         identifier
1003 |       variable_declarator_id OSB_TK CSB_TK
1004                 { $$ = build_unresolved_array_type ($1); }
1005 |       identifier error
1006                 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
1007 |       variable_declarator_id OSB_TK error
1008                 {
1009                   yyerror ("']' expected");
1010                   DRECOVER(vdi);
1011                 }
1012 |       variable_declarator_id CSB_TK error
1013                 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1014 ;
1015
1016 variable_initializer:
1017         expression
1018 |       array_initializer
1019 ;
1020
1021 /* 19.8.3 Productions from 8.4: Method Declarations  */
1022 method_declaration:
1023         method_header
1024                 {
1025                   current_function_decl = $1;
1026                   if (current_function_decl
1027                       && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1028                     source_start_java_method (current_function_decl);
1029                   else
1030                     current_function_decl = NULL_TREE;
1031                 }
1032         method_body
1033                 { finish_method_declaration ($3); }
1034 |       method_header error
1035                 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1036 ;
1037
1038 method_header:
1039         type method_declarator throws
1040                 { $$ = method_header (0, $1, $2, $3); }
1041 |       VOID_TK method_declarator throws
1042                 { $$ = method_header (0, void_type_node, $2, $3); }
1043 |       modifiers type method_declarator throws
1044                 { $$ = method_header ($1, $2, $3, $4); }
1045 |       modifiers VOID_TK method_declarator throws
1046                 { $$ = method_header ($1, void_type_node, $3, $4); }
1047 |       type error
1048                 {
1049                   yyerror ("Invalid method declaration, method name required");
1050                   RECOVER;
1051                 }
1052 |       modifiers type error
1053                 {
1054                   yyerror ("Identifier expected");
1055                   RECOVER;
1056                 }
1057 |       VOID_TK error
1058                 {
1059                   yyerror ("Identifier expected");
1060                   RECOVER;
1061                 }
1062 |       modifiers VOID_TK error
1063                 {
1064                   yyerror ("Identifier expected");
1065                   RECOVER;
1066                 }
1067 |       modifiers error
1068                 {
1069                   yyerror ("Invalid method declaration, return type required");
1070                   RECOVER;
1071                 }
1072 ;
1073
1074 method_declarator:
1075         identifier OP_TK CP_TK
1076                 {
1077                   ctxp->formal_parameter_number = 0;
1078                   $$ = method_declarator ($1, NULL_TREE);
1079                 }
1080 |       identifier OP_TK formal_parameter_list CP_TK
1081                 { $$ = method_declarator ($1, $3); }
1082 |       method_declarator OSB_TK CSB_TK
1083                 {
1084                   SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1085                   TREE_PURPOSE ($1) =
1086                     build_unresolved_array_type (TREE_PURPOSE ($1));
1087                   parse_warning_context
1088                     (wfl_operator,
1089                      "Discouraged form of returned type specification");
1090                 }
1091 |       identifier OP_TK error
1092                 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1093 |       method_declarator OSB_TK error
1094                 {yyerror ("']' expected"); RECOVER;}
1095 ;
1096
1097 formal_parameter_list:
1098         formal_parameter
1099                 {
1100                   ctxp->formal_parameter_number = 1;
1101                 }
1102 |       formal_parameter_list C_TK formal_parameter
1103                 {
1104                   ctxp->formal_parameter_number += 1;
1105                   $$ = chainon ($1, $3);
1106                 }
1107 |       formal_parameter_list C_TK error
1108                 { yyerror ("Missing formal parameter term"); RECOVER; }
1109 ;
1110
1111 formal_parameter:
1112         type variable_declarator_id
1113                 {
1114                   $$ = build_tree_list ($2, $1);
1115                 }
1116 |       final type variable_declarator_id /* Added, JDK1.1 final parms */
1117                 {
1118                   $$ = build_tree_list ($3, $2);
1119                   ARG_FINAL_P ($$) = 1;
1120                 }
1121 |       type error
1122                 {
1123                   yyerror ("Missing identifier"); RECOVER;
1124                   $$ = NULL_TREE;
1125                 }
1126 |       final type error
1127                 {
1128                   yyerror ("Missing identifier"); RECOVER;
1129                   $$ = NULL_TREE;
1130                 }
1131 ;
1132
1133 final:
1134         modifiers
1135                 {
1136                   check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1137                                    $1, ACC_FINAL);
1138                   if ($1 != ACC_FINAL)
1139                     MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1140                 }
1141 ;
1142
1143 throws:
1144                 { $$ = NULL_TREE; }
1145 |       THROWS_TK class_type_list
1146                 { $$ = $2; }
1147 |       THROWS_TK error
1148                 {yyerror ("Missing class type term"); RECOVER;}
1149 ;
1150
1151 class_type_list:
1152         class_type
1153                 { $$ = build_tree_list ($1, $1); }
1154 |       class_type_list C_TK class_type
1155                 { $$ = tree_cons ($3, $3, $1); }
1156 |       class_type_list C_TK error
1157                 {yyerror ("Missing class type term"); RECOVER;}
1158 ;
1159
1160 method_body:
1161         block
1162 |       SC_TK { $$ = NULL_TREE; }
1163 ;
1164
1165 /* 19.8.4 Productions from 8.5: Static Initializers  */
1166 static_initializer:
1167         static block
1168                 {
1169                   TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1170                   SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1171                   current_static_block = NULL_TREE;
1172                 }
1173 ;
1174
1175 static:                         /* Test lval.sub_token here */
1176         modifiers
1177                 {
1178                   check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1179                   /* Can't have a static initializer in an innerclass */
1180                   if ($1 | ACC_STATIC &&
1181                       GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1182                     parse_error_context
1183                       (MODIFIER_WFL (STATIC_TK),
1184                        "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1185                        IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1186                   SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1187                 }
1188 ;
1189
1190 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1191 constructor_declaration:
1192         constructor_header
1193                 {
1194                   current_function_decl = $1;
1195                   source_start_java_method (current_function_decl);
1196                 }
1197         constructor_body
1198                 { finish_method_declaration ($3); }
1199 ;
1200
1201 constructor_header:
1202         constructor_declarator throws
1203                 { $$ = method_header (0, NULL_TREE, $1, $2); }
1204 |       modifiers constructor_declarator throws
1205                 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1206 ;
1207
1208 constructor_declarator:
1209         simple_name OP_TK CP_TK
1210                 {
1211                   ctxp->formal_parameter_number = 0;
1212                   $$ = method_declarator ($1, NULL_TREE);
1213                 }
1214 |       simple_name OP_TK formal_parameter_list CP_TK
1215                 { $$ = method_declarator ($1, $3); }
1216 ;
1217
1218 constructor_body:
1219         /* Unlike regular method, we always need a complete (empty)
1220            body so we can safely perform all the required code
1221            addition (super invocation and field initialization) */
1222         block_begin constructor_block_end
1223                 {
1224                   BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1225                   $$ = $2;
1226                 }
1227 |       block_begin explicit_constructor_invocation constructor_block_end
1228                 { $$ = $3; }
1229 |       block_begin block_statements constructor_block_end
1230                 { $$ = $3; }
1231 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1232                 { $$ = $4; }
1233 ;
1234
1235 constructor_block_end:
1236         block_end
1237 ;
1238
1239 /* Error recovery for that rule moved down expression_statement: rule.  */
1240 explicit_constructor_invocation:
1241         this_or_super OP_TK CP_TK SC_TK
1242                 {
1243                   $$ = build_method_invocation ($1, NULL_TREE);
1244                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1245                   $$ = java_method_add_stmt (current_function_decl, $$);
1246                 }
1247 |       this_or_super OP_TK argument_list CP_TK SC_TK
1248                 {
1249                   $$ = build_method_invocation ($1, $3);
1250                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1251                   $$ = java_method_add_stmt (current_function_decl, $$);
1252                 }
1253         /* Added, JDK1.1 inner classes. Modified because the rule
1254            'primary' couldn't work.  */
1255 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1256                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1257 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1258                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1259 ;
1260
1261 this_or_super:                  /* Added, simplifies error diagnostics */
1262         THIS_TK
1263                 {
1264                   tree wfl = build_wfl_node (this_identifier_node);
1265                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1266                   $$ = wfl;
1267                 }
1268 |       SUPER_TK
1269                 {
1270                   tree wfl = build_wfl_node (super_identifier_node);
1271                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1272                   $$ = wfl;
1273                 }
1274 ;
1275
1276 /* 19.9 Productions from 9: Interfaces  */
1277 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1278 interface_declaration:
1279         INTERFACE_TK identifier
1280                 { create_interface (0, $2, NULL_TREE); }
1281         interface_body
1282                 { ; }
1283 |       modifiers INTERFACE_TK identifier
1284                 { create_interface ($1, $3, NULL_TREE); }
1285         interface_body
1286                 { ; }
1287 |       INTERFACE_TK identifier extends_interfaces
1288                 { create_interface (0, $2, $3); }
1289         interface_body
1290                 { ; }
1291 |       modifiers INTERFACE_TK identifier extends_interfaces
1292                 { create_interface ($1, $3, $4); }
1293         interface_body
1294                 { ; }
1295 |       INTERFACE_TK identifier error
1296                 { yyerror ("'{' expected"); RECOVER; }
1297 |       modifiers INTERFACE_TK identifier error
1298                 { yyerror ("'{' expected"); RECOVER; }
1299 ;
1300
1301 extends_interfaces:
1302         EXTENDS_TK interface_type
1303                 {
1304                   ctxp->interface_number = 1;
1305                   $$ = build_tree_list ($2, NULL_TREE);
1306                 }
1307 |       extends_interfaces C_TK interface_type
1308                 {
1309                   ctxp->interface_number++;
1310                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1311                 }
1312 |       EXTENDS_TK error
1313                 {yyerror ("Invalid interface type"); RECOVER;}
1314 |       extends_interfaces C_TK error
1315                 {yyerror ("Missing term"); RECOVER;}
1316 ;
1317
1318 interface_body:
1319         OCB_TK CCB_TK
1320                 { $$ = NULL_TREE; }
1321 |       OCB_TK interface_member_declarations CCB_TK
1322                 { $$ = NULL_TREE; }
1323 ;
1324
1325 interface_member_declarations:
1326         interface_member_declaration
1327 |       interface_member_declarations interface_member_declaration
1328 ;
1329
1330 interface_member_declaration:
1331         constant_declaration
1332 |       abstract_method_declaration
1333 |       class_declaration       /* Added, JDK1.1 inner classes */
1334                 { end_class_declaration (1); }
1335 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
1336                 { end_class_declaration (1); }
1337 ;
1338
1339 constant_declaration:
1340         field_declaration
1341 ;
1342
1343 abstract_method_declaration:
1344         method_header SC_TK
1345                 {
1346                   check_abstract_method_header ($1);
1347                   current_function_decl = NULL_TREE; /* FIXME ? */
1348                 }
1349 |       method_header error
1350                 {yyerror ("';' expected"); RECOVER;}
1351 ;
1352
1353 /* 19.10 Productions from 10: Arrays  */
1354 array_initializer:
1355         OCB_TK CCB_TK
1356                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1357 |       OCB_TK C_TK CCB_TK
1358                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1359 |       OCB_TK variable_initializers CCB_TK
1360                 { $$ = build_new_array_init ($1.location, $2); }
1361 |       OCB_TK variable_initializers C_TK CCB_TK
1362                 { $$ = build_new_array_init ($1.location, $2); }
1363 ;
1364
1365 variable_initializers:
1366         variable_initializer
1367                 {
1368                   $$ = tree_cons (maybe_build_array_element_wfl ($1),
1369                                   $1, NULL_TREE);
1370                 }
1371 |       variable_initializers C_TK variable_initializer
1372                 {
1373                   $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1374                 }
1375 |       variable_initializers C_TK error
1376                 {yyerror ("Missing term"); RECOVER;}
1377 ;
1378
1379 /* 19.11 Production from 14: Blocks and Statements  */
1380 block:
1381         block_begin block_end
1382                 { $$ = $2; }
1383 |       block_begin block_statements block_end
1384                 { $$ = $3; }
1385 ;
1386
1387 block_begin:
1388         OCB_TK
1389                 { enter_block (); }
1390 ;
1391
1392 block_end:
1393         CCB_TK
1394                 {
1395                   maybe_absorb_scoping_blocks ();
1396                   /* Store the location of the `}' when doing xrefs */
1397                   if (current_function_decl && flag_emit_xref)
1398                     DECL_END_SOURCE_LINE (current_function_decl) = $1.location;
1399                   $$ = exit_block ();
1400                   if (!BLOCK_SUBBLOCKS ($$))
1401                     BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1402                 }
1403 ;
1404
1405 block_statements:
1406         block_statement
1407 |       block_statements block_statement
1408 ;
1409
1410 block_statement:
1411         local_variable_declaration_statement
1412 |       statement
1413                 { java_method_add_stmt (current_function_decl, $1); }
1414 |       class_declaration       /* Added, JDK1.1 local classes */
1415                 {
1416                   LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1417                   end_class_declaration (1);
1418                 }
1419 ;
1420
1421 local_variable_declaration_statement:
1422         local_variable_declaration SC_TK /* Can't catch missing ';' here */
1423 ;
1424
1425 local_variable_declaration:
1426         type variable_declarators
1427                 { declare_local_variables (0, $1, $2); }
1428 |       final type variable_declarators /* Added, JDK1.1 final locals */
1429                 { declare_local_variables ($1, $2, $3); }
1430 ;
1431
1432 statement:
1433         statement_without_trailing_substatement
1434 |       labeled_statement
1435 |       if_then_statement
1436 |       if_then_else_statement
1437 |       while_statement
1438 |       for_statement
1439                 { $$ = exit_block (); }
1440 ;
1441
1442 statement_nsi:
1443         statement_without_trailing_substatement
1444 |       labeled_statement_nsi
1445 |       if_then_else_statement_nsi
1446 |       while_statement_nsi
1447 |       for_statement_nsi
1448                 { $$ = exit_block (); }
1449 ;
1450
1451 statement_without_trailing_substatement:
1452         block
1453 |       empty_statement
1454 |       expression_statement
1455 |       switch_statement
1456 |       do_statement
1457 |       break_statement
1458 |       continue_statement
1459 |       return_statement
1460 |       synchronized_statement
1461 |       throw_statement
1462 |       try_statement
1463 |       assert_statement
1464 ;
1465
1466 empty_statement:
1467         SC_TK
1468                 {
1469                   if (flag_extraneous_semicolon
1470                       && ! current_static_block
1471                       && (! current_function_decl ||
1472                           /* Verify we're not in a inner class declaration */
1473                           (GET_CPC () != TYPE_NAME
1474                            (DECL_CONTEXT (current_function_decl)))))
1475
1476                     {
1477 #ifdef USE_MAPPED_LOCATION
1478                       SET_EXPR_LOCATION (wfl_operator, input_location);
1479 #else
1480                       EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1481 #endif
1482                       parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1483                     }
1484                   $$ = build_java_empty_stmt ();
1485                 }
1486 ;
1487
1488 label_decl:
1489         identifier REL_CL_TK
1490                 {
1491                   $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1492                                             EXPR_WFL_NODE ($1));
1493                   pushlevel (2);
1494                   push_labeled_block ($$);
1495                   PUSH_LABELED_BLOCK ($$);
1496                 }
1497 ;
1498
1499 labeled_statement:
1500         label_decl statement
1501                 { $$ = finish_labeled_statement ($1, $2); }
1502 |       identifier error
1503                 {yyerror ("':' expected"); RECOVER;}
1504 ;
1505
1506 labeled_statement_nsi:
1507         label_decl statement_nsi
1508                 { $$ = finish_labeled_statement ($1, $2); }
1509 ;
1510
1511 /* We concentrate here a bunch of error handling rules that we couldn't write
1512    earlier, because expression_statement catches a missing ';'.  */
1513 expression_statement:
1514         statement_expression SC_TK
1515                 {
1516                   /* We have a statement. Generate a WFL around it so
1517                      we can debug it */
1518 #ifdef USE_MAPPED_LOCATION
1519                   $$ = expr_add_location ($1, input_location, 1);
1520 #else
1521                   $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1522                   JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1523 #endif
1524                   /* We know we have a statement, so set the debug
1525                      info to be eventually generate here. */
1526                 }
1527 |       error SC_TK
1528                 {
1529                   YYNOT_TWICE yyerror ("Invalid expression statement");
1530                   DRECOVER (expr_stmt);
1531                 }
1532 |       error OCB_TK
1533                 {
1534                   YYNOT_TWICE yyerror ("Invalid expression statement");
1535                   DRECOVER (expr_stmt);
1536                 }
1537 |       error CCB_TK
1538                 {
1539                   YYNOT_TWICE yyerror ("Invalid expression statement");
1540                   DRECOVER (expr_stmt);
1541                 }
1542 |       this_or_super OP_TK error
1543                 {yyerror ("')' expected"); RECOVER;}
1544 |       this_or_super OP_TK CP_TK error
1545                 {
1546                   parse_ctor_invocation_error ();
1547                   RECOVER;
1548                 }
1549 |       this_or_super OP_TK argument_list error
1550                 {yyerror ("')' expected"); RECOVER;}
1551 |       this_or_super OP_TK argument_list CP_TK error
1552                 {
1553                   parse_ctor_invocation_error ();
1554                   RECOVER;
1555                 }
1556 |       name DOT_TK SUPER_TK error
1557                 {yyerror ("'(' expected"); RECOVER;}
1558 |       name DOT_TK SUPER_TK OP_TK error
1559                 {yyerror ("')' expected"); RECOVER;}
1560 |       name DOT_TK SUPER_TK OP_TK argument_list error
1561                 {yyerror ("')' expected"); RECOVER;}
1562 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1563                 {yyerror ("';' expected"); RECOVER;}
1564 |       name DOT_TK SUPER_TK OP_TK CP_TK error
1565                 {yyerror ("';' expected"); RECOVER;}
1566 ;
1567
1568 statement_expression:
1569         assignment
1570 |       pre_increment_expression
1571 |       pre_decrement_expression
1572 |       post_increment_expression
1573 |       post_decrement_expression
1574 |       method_invocation
1575 |       class_instance_creation_expression
1576 ;
1577
1578 if_then_statement:
1579         IF_TK OP_TK expression CP_TK statement
1580                 {
1581                   $$ = build_if_else_statement ($2.location, $3,
1582                                                 $5, NULL_TREE);
1583                 }
1584 |       IF_TK error
1585                 {yyerror ("'(' expected"); RECOVER;}
1586 |       IF_TK OP_TK error
1587                 {yyerror ("Missing term"); RECOVER;}
1588 |       IF_TK OP_TK expression error
1589                 {yyerror ("')' expected"); RECOVER;}
1590 ;
1591
1592 if_then_else_statement:
1593         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1594                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1595 ;
1596
1597 if_then_else_statement_nsi:
1598         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1599                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1600 ;
1601
1602 switch_statement:
1603         switch_expression
1604                 {
1605                   enter_block ();
1606                 }
1607         switch_block
1608                 {
1609                   /* Make into "proper list" of COMPOUND_EXPRs.
1610                      I.e. make the last statement also have its own
1611                      COMPOUND_EXPR. */
1612                   maybe_absorb_scoping_blocks ();
1613                   TREE_OPERAND ($1, 1) = exit_block ();
1614                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1615                 }
1616 ;
1617
1618 switch_expression:
1619         SWITCH_TK OP_TK expression CP_TK
1620                 {
1621                   $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1622                                NULL_TREE, NULL_TREE);
1623                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1624                 }
1625 |       SWITCH_TK error
1626                 {yyerror ("'(' expected"); RECOVER;}
1627 |       SWITCH_TK OP_TK error
1628                 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1629 |       SWITCH_TK OP_TK expression CP_TK error
1630                 {yyerror ("'{' expected"); RECOVER;}
1631 ;
1632
1633 /* Default assignment is there to avoid type node on switch_block
1634    node. */
1635
1636 switch_block:
1637         OCB_TK CCB_TK
1638                 { $$ = NULL_TREE; }
1639 |       OCB_TK switch_labels CCB_TK
1640                 { $$ = NULL_TREE; }
1641 |       OCB_TK switch_block_statement_groups CCB_TK
1642                 { $$ = NULL_TREE; }
1643 |       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1644                 { $$ = NULL_TREE; }
1645 ;
1646
1647 switch_block_statement_groups:
1648         switch_block_statement_group
1649 |       switch_block_statement_groups switch_block_statement_group
1650 ;
1651
1652 switch_block_statement_group:
1653         switch_labels block_statements
1654 ;
1655
1656 switch_labels:
1657         switch_label
1658 |       switch_labels switch_label
1659 ;
1660
1661 switch_label:
1662         CASE_TK constant_expression REL_CL_TK
1663                 {
1664                   tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1665                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1666                   java_method_add_stmt (current_function_decl, lab);
1667                 }
1668 |       DEFAULT_TK REL_CL_TK
1669                 {
1670                   tree lab = make_node (DEFAULT_EXPR);
1671                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1672                   java_method_add_stmt (current_function_decl, lab);
1673                 }
1674 |       CASE_TK error
1675                 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1676 |       CASE_TK constant_expression error
1677                 {yyerror ("':' expected"); RECOVER;}
1678 |       DEFAULT_TK error
1679                 {yyerror ("':' expected"); RECOVER;}
1680 ;
1681
1682 while_expression:
1683         WHILE_TK OP_TK expression CP_TK
1684                 {
1685                   tree body = build_loop_body ($2.location, $3, 0);
1686                   $$ = build_new_loop (body);
1687                 }
1688 ;
1689
1690 while_statement:
1691         while_expression statement
1692                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1693 |       WHILE_TK error
1694                 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1695 |       WHILE_TK OP_TK error
1696                 {yyerror ("Missing term and ')' expected"); RECOVER;}
1697 |       WHILE_TK OP_TK expression error
1698                 {yyerror ("')' expected"); RECOVER;}
1699 ;
1700
1701 while_statement_nsi:
1702         while_expression statement_nsi
1703                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1704 ;
1705
1706 do_statement_begin:
1707         DO_TK
1708                 {
1709                   tree body = build_loop_body (0, NULL_TREE, 1);
1710                   $$ = build_new_loop (body);
1711                 }
1712         /* Need error handing here. FIXME */
1713 ;
1714
1715 do_statement:
1716         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1717                 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1718 ;
1719
1720 for_statement:
1721         for_begin SC_TK expression SC_TK for_update CP_TK statement
1722                 {
1723                   if (CONSTANT_CLASS_P ($3))
1724                     $3 = build_wfl_node ($3);
1725                   $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1726                 }
1727 |       for_begin SC_TK SC_TK for_update CP_TK statement
1728                 {
1729                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1730                   /* We have not condition, so we get rid of the EXIT_EXPR */
1731                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1732                     build_java_empty_stmt ();
1733                 }
1734 |       for_begin SC_TK error
1735                 {yyerror ("Invalid control expression"); RECOVER;}
1736 |       for_begin SC_TK expression SC_TK error
1737                 {yyerror ("Invalid update expression"); RECOVER;}
1738 |       for_begin SC_TK SC_TK error
1739                 {yyerror ("Invalid update expression"); RECOVER;}
1740 ;
1741
1742 for_statement_nsi:
1743         for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1744                 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1745 |       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1746                 {
1747                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1748                   /* We have not condition, so we get rid of the EXIT_EXPR */
1749                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1750                     build_java_empty_stmt ();
1751                 }
1752 ;
1753
1754 for_header:
1755         FOR_TK OP_TK
1756                 {
1757                   /* This scope defined for local variable that may be
1758                      defined within the scope of the for loop */
1759                   enter_block ();
1760                 }
1761 |       FOR_TK error
1762                 {yyerror ("'(' expected"); DRECOVER(for_1);}
1763 |       FOR_TK OP_TK error
1764                 {yyerror ("Invalid init statement"); RECOVER;}
1765 ;
1766
1767 for_begin:
1768         for_header for_init
1769                 {
1770                   /* We now declare the loop body. The loop is
1771                      declared as a for loop. */
1772                   tree body = build_loop_body (0, NULL_TREE, 0);
1773                   $$ =  build_new_loop (body);
1774                   FOR_LOOP_P ($$) = 1;
1775                   /* The loop is added to the current block the for
1776                      statement is defined within */
1777                   java_method_add_stmt (current_function_decl, $$);
1778                 }
1779 ;
1780 for_init:                       /* Can be empty */
1781                 { $$ = build_java_empty_stmt (); }
1782 |       statement_expression_list
1783                 {
1784                   /* Init statement recorded within the previously
1785                      defined block scope */
1786                   $$ = java_method_add_stmt (current_function_decl, $1);
1787                 }
1788 |       local_variable_declaration
1789                 {
1790                   /* Local variable are recorded within the previously
1791                      defined block scope */
1792                   $$ = NULL_TREE;
1793                 }
1794 |       statement_expression_list error
1795                 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1796 ;
1797
1798 for_update:                     /* Can be empty */
1799                 {$$ = build_java_empty_stmt ();}
1800 |       statement_expression_list
1801                 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1802 ;
1803
1804 statement_expression_list:
1805         statement_expression
1806                 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1807 |       statement_expression_list C_TK statement_expression
1808                 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1809 |       statement_expression_list C_TK error
1810                 {yyerror ("Missing term"); RECOVER;}
1811 ;
1812
1813 break_statement:
1814         BREAK_TK SC_TK
1815                 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1816 |       BREAK_TK identifier SC_TK
1817                 { $$ = build_bc_statement ($1.location, 1, $2); }
1818 |       BREAK_TK error
1819                 {yyerror ("Missing term"); RECOVER;}
1820 |       BREAK_TK identifier error
1821                 {yyerror ("';' expected"); RECOVER;}
1822 ;
1823
1824 continue_statement:
1825         CONTINUE_TK SC_TK
1826                 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1827 |       CONTINUE_TK identifier SC_TK
1828                 { $$ = build_bc_statement ($1.location, 0, $2); }
1829 |       CONTINUE_TK error
1830                 {yyerror ("Missing term"); RECOVER;}
1831 |       CONTINUE_TK identifier error
1832                 {yyerror ("';' expected"); RECOVER;}
1833 ;
1834
1835 return_statement:
1836         RETURN_TK SC_TK
1837                 { $$ = build_return ($1.location, NULL_TREE); }
1838 |       RETURN_TK expression SC_TK
1839                 { $$ = build_return ($1.location, $2); }
1840 |       RETURN_TK error
1841                 {yyerror ("Missing term"); RECOVER;}
1842 |       RETURN_TK expression error
1843                 {yyerror ("';' expected"); RECOVER;}
1844 ;
1845
1846 throw_statement:
1847         THROW_TK expression SC_TK
1848                 {
1849                   $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1850                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1851                 }
1852 |       THROW_TK error
1853                 {yyerror ("Missing term"); RECOVER;}
1854 |       THROW_TK expression error
1855                 {yyerror ("';' expected"); RECOVER;}
1856 ;
1857
1858 assert_statement:
1859         ASSERT_TK expression REL_CL_TK expression SC_TK
1860                 {
1861                   $$ = build_assertion ($1.location, $2, $4);
1862                 }
1863 |       ASSERT_TK expression SC_TK
1864                 {
1865                   $$ = build_assertion ($1.location, $2, NULL_TREE);
1866                 }
1867 |       ASSERT_TK error
1868                 {yyerror ("Missing term"); RECOVER;}
1869 |       ASSERT_TK expression error
1870                 {yyerror ("';' expected"); RECOVER;}
1871 ;
1872
1873 synchronized_statement:
1874         synchronized OP_TK expression CP_TK block
1875                 {
1876                   $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1877                   EXPR_WFL_LINECOL ($$) =
1878                     EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1879                 }
1880 |       synchronized OP_TK expression CP_TK error
1881                 {yyerror ("'{' expected"); RECOVER;}
1882 |       synchronized error
1883                 {yyerror ("'(' expected"); RECOVER;}
1884 |       synchronized OP_TK error CP_TK
1885                 {yyerror ("Missing term"); RECOVER;}
1886 |       synchronized OP_TK error
1887                 {yyerror ("Missing term"); RECOVER;}
1888 ;
1889
1890 synchronized:
1891         modifiers
1892                 {
1893                   check_modifiers (
1894              "Illegal modifier %qs. Only %<synchronized%> was expected here",
1895                                    $1, ACC_SYNCHRONIZED);
1896                   if ($1 != ACC_SYNCHRONIZED)
1897                     MODIFIER_WFL (SYNCHRONIZED_TK) =
1898                       build_wfl_node (NULL_TREE);
1899                 }
1900 ;
1901
1902 try_statement:
1903         TRY_TK block catches
1904                 { $$ = build_try_statement ($1.location, $2, $3); }
1905 |       TRY_TK block finally
1906                 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1907 |       TRY_TK block catches finally
1908                 { $$ = build_try_finally_statement
1909                     ($1.location, build_try_statement ($1.location,
1910                                                        $2, $3), $4);
1911                 }
1912 |       TRY_TK error
1913                 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1914 ;
1915
1916 catches:
1917         catch_clause
1918 |       catches catch_clause
1919                 {
1920                   TREE_CHAIN ($2) = $1;
1921                   $$ = $2;
1922                 }
1923 ;
1924
1925 catch_clause:
1926         catch_clause_parameter block
1927                 {
1928                   java_method_add_stmt (current_function_decl, $2);
1929                   exit_block ();
1930                   $$ = $1;
1931                 }
1932 ;
1933
1934 catch_clause_parameter:
1935         CATCH_TK OP_TK formal_parameter CP_TK
1936                 {
1937                   /* We add a block to define a scope for
1938                      formal_parameter (CCBP). The formal parameter is
1939                      declared initialized by the appropriate function
1940                      call */
1941                   tree ccpb;
1942                   tree init;
1943                   if ($3)
1944                     {
1945                       ccpb = enter_block ();
1946                       init = build_assignment
1947                         (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1948                          build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1949                       declare_local_variables (0, TREE_VALUE ($3),
1950                                                build_tree_list 
1951                                                (TREE_PURPOSE ($3), init));
1952                       $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1953                       SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1954                     }
1955                   else
1956                     {
1957                       $$ = error_mark_node;
1958                     }
1959                 }
1960 |       CATCH_TK error
1961                 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1962 |       CATCH_TK OP_TK error
1963                 {
1964                   yyerror ("Missing term or ')' expected");
1965                   RECOVER; $$ = NULL_TREE;
1966                 }
1967 |       CATCH_TK OP_TK error CP_TK /* That's for () */
1968                 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1969 ;
1970
1971 finally:
1972         FINALLY_TK block
1973                 { $$ = $2; }
1974 |       FINALLY_TK error
1975                 {yyerror ("'{' expected"); RECOVER; }
1976 ;
1977
1978 /* 19.12 Production from 15: Expressions  */
1979 primary:
1980         primary_no_new_array
1981 |       array_creation_uninitialized
1982 |       array_creation_initialized
1983 ;
1984
1985 primary_no_new_array:
1986         literal
1987 |       THIS_TK
1988                 { $$ = build_this ($1.location); }
1989 |       OP_TK expression CP_TK
1990                 {$$ = $2;}
1991 |       class_instance_creation_expression
1992 |       field_access
1993 |       method_invocation
1994 |       array_access
1995 |       type_literals
1996         /* Added, JDK1.1 inner classes. Documentation is wrong
1997            referring to a 'ClassName' (class_name) rule that doesn't
1998            exist. Used name: instead.  */
1999 |       name DOT_TK THIS_TK
2000                 {
2001                   tree wfl = build_wfl_node (this_identifier_node);
2002                   $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
2003                 }
2004 |       OP_TK expression error
2005                 {yyerror ("')' expected"); RECOVER;}
2006 |       name DOT_TK error
2007                 {yyerror ("'class' or 'this' expected" ); RECOVER;}
2008 |       primitive_type DOT_TK error
2009                 {yyerror ("'class' expected" ); RECOVER;}
2010 |       VOID_TK DOT_TK error
2011                 {yyerror ("'class' expected" ); RECOVER;}
2012 ;
2013
2014 type_literals:
2015         name DOT_TK CLASS_TK
2016                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2017 |       array_type DOT_TK CLASS_TK
2018                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2019 |       primitive_type DOT_TK CLASS_TK
2020                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2021 |       VOID_TK DOT_TK CLASS_TK
2022                 {
2023                    $$ = build_incomplete_class_ref ($2.location,
2024                                                    void_type_node);
2025                 }
2026 ;
2027
2028 class_instance_creation_expression:
2029         NEW_TK class_type OP_TK argument_list CP_TK
2030                 { $$ = build_new_invocation ($2, $4); }
2031 |       NEW_TK class_type OP_TK CP_TK
2032                 { $$ = build_new_invocation ($2, NULL_TREE); }
2033 |       anonymous_class_creation
2034         /* Added, JDK1.1 inner classes, modified to use name or
2035            primary instead of primary solely which couldn't work in
2036            all situations.  */
2037 |       something_dot_new identifier OP_TK CP_TK
2038                 {
2039                   tree ctor = build_new_invocation ($2, NULL_TREE);
2040                   $$ = make_qualified_primary ($1, ctor,
2041                                                EXPR_WFL_LINECOL ($1));
2042                 }
2043 |       something_dot_new identifier OP_TK CP_TK class_body
2044 |       something_dot_new identifier OP_TK argument_list CP_TK
2045                 {
2046                   tree ctor = build_new_invocation ($2, $4);
2047                   $$ = make_qualified_primary ($1, ctor,
2048                                                EXPR_WFL_LINECOL ($1));
2049                 }
2050 |       something_dot_new identifier OP_TK argument_list CP_TK class_body
2051 |       NEW_TK error SC_TK
2052                 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2053 |       NEW_TK class_type error
2054                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2055 |       NEW_TK class_type OP_TK error
2056                 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2057 |       NEW_TK class_type OP_TK argument_list error
2058                 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2059 |       something_dot_new error
2060                 {
2061                   $$ = NULL_TREE;
2062                   YYERROR_NOW;
2063                   yyerror ("Identifier expected");
2064                   RECOVER;
2065                 }
2066 |       something_dot_new identifier error
2067                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2068 ;
2069
2070 /* Created after JDK1.1 rules originally added to
2071    class_instance_creation_expression, but modified to use
2072    'class_type' instead of 'TypeName' (type_name) which is mentioned
2073    in the documentation but doesn't exist. */
2074
2075 anonymous_class_creation:
2076         NEW_TK class_type OP_TK argument_list CP_TK
2077                 { create_anonymous_class ($2); }
2078         class_body
2079                 {
2080                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2081                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2082
2083                   end_class_declaration (1);
2084
2085                   /* Now we can craft the new expression */
2086                   $$ = build_new_invocation (id, $4);
2087
2088                   /* Note that we can't possibly be here if
2089                      `class_type' is an interface (in which case the
2090                      anonymous class extends Object and implements
2091                      `class_type', hence its constructor can't have
2092                      arguments.) */
2093
2094                   /* Otherwise, the innerclass must feature a
2095                      constructor matching `argument_list'. Anonymous
2096                      classes are a bit special: it's impossible to
2097                      define constructor for them, hence constructors
2098                      must be generated following the hints provided by
2099                      the `new' expression. Whether a super constructor
2100                      of that nature exists or not is to be verified
2101                      later on in verify_constructor_super.
2102
2103                      It's during the expansion of a `new' statement
2104                      referring to an anonymous class that a ctor will
2105                      be generated for the anonymous class, with the
2106                      right arguments. */
2107
2108                 }
2109 |       NEW_TK class_type OP_TK CP_TK
2110                 { create_anonymous_class ($2); }
2111         class_body
2112                 {
2113                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2114                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2115
2116                   end_class_declaration (1);
2117
2118                   /* Now we can craft the new expression. The
2119                      statement doesn't need to be remember so that a
2120                      constructor can be generated, since its signature
2121                      is already known. */
2122                   $$ = build_new_invocation (id, NULL_TREE);
2123                 }
2124 ;
2125
2126 something_dot_new:              /* Added, not part of the specs. */
2127         name DOT_TK NEW_TK
2128                 { $$ = $1; }
2129 |       primary DOT_TK NEW_TK
2130                 { $$ = $1; }
2131 ;
2132
2133 argument_list:
2134         expression
2135                 {
2136                   $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2137                   ctxp->formal_parameter_number = 1;
2138                 }
2139 |       argument_list C_TK expression
2140                 {
2141                   ctxp->formal_parameter_number += 1;
2142                   $$ = tree_cons (NULL_TREE, $3, $1);
2143                 }
2144 |       argument_list C_TK error
2145                 {yyerror ("Missing term"); RECOVER;}
2146 ;
2147
2148 array_creation_uninitialized:
2149         NEW_TK primitive_type dim_exprs
2150                 { $$ = build_newarray_node ($2, $3, 0); }
2151 |       NEW_TK class_or_interface_type dim_exprs
2152                 { $$ = build_newarray_node ($2, $3, 0); }
2153 |       NEW_TK primitive_type dim_exprs dims
2154                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2155 |       NEW_TK class_or_interface_type dim_exprs dims
2156                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2157 |       NEW_TK error CSB_TK
2158                 {yyerror ("'[' expected"); DRECOVER ("]");}
2159 |       NEW_TK error OSB_TK
2160                 {yyerror ("']' expected"); RECOVER;}
2161 ;
2162
2163 array_creation_initialized:
2164         /* Added, JDK1.1 anonymous array. Initial documentation rule
2165            modified */
2166         NEW_TK class_or_interface_type dims array_initializer
2167                 {
2168                   char *sig;
2169                   int osb = pop_current_osb (ctxp);
2170                   while (osb--)
2171                     obstack_grow (&temporary_obstack, "[]", 2);
2172                   obstack_1grow (&temporary_obstack, '\0');
2173                   sig = obstack_finish (&temporary_obstack);
2174                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2175                                $2, get_identifier (sig), $4);
2176                 }
2177 |       NEW_TK primitive_type dims array_initializer
2178                 {
2179                   int osb = pop_current_osb (ctxp);
2180                   tree type = $2;
2181                   while (osb--)
2182                     type = build_java_array_type (type, -1);
2183                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2184                                build_pointer_type (type), NULL_TREE, $4);
2185                 }
2186 |       NEW_TK error CSB_TK
2187                 {yyerror ("'[' expected"); DRECOVER ("]");}
2188 |       NEW_TK error OSB_TK
2189                 {yyerror ("']' expected"); RECOVER;}
2190 ;
2191
2192 dim_exprs:
2193         dim_expr
2194                 { $$ = build_tree_list (NULL_TREE, $1); }
2195 |       dim_exprs dim_expr
2196                 { $$ = tree_cons (NULL_TREE, $2, $$); }
2197 ;
2198
2199 dim_expr:
2200         OSB_TK expression CSB_TK
2201                 {
2202                   if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2203                     {
2204                       $2 = build_wfl_node ($2);
2205                       TREE_TYPE ($2) = NULL_TREE;
2206                     }
2207                   EXPR_WFL_LINECOL ($2) = $1.location;
2208                   $$ = $2;
2209                 }
2210 |       OSB_TK expression error
2211                 {yyerror ("']' expected"); RECOVER;}
2212 |       OSB_TK error
2213                 {
2214                   yyerror ("Missing term");
2215                   yyerror ("']' expected");
2216                   RECOVER;
2217                 }
2218 ;
2219
2220 dims:
2221         OSB_TK CSB_TK
2222                 {
2223                   int allocate = 0;
2224                   /* If not initialized, allocate memory for the osb
2225                      numbers stack */
2226                   if (!ctxp->osb_limit)
2227                     {
2228                       allocate = ctxp->osb_limit = 32;
2229                       ctxp->osb_depth = -1;
2230                     }
2231                   /* If capacity overflown, reallocate a bigger chunk */
2232                   else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2233                     allocate = ctxp->osb_limit << 1;
2234
2235                   if (allocate)
2236                     {
2237                       allocate *= sizeof (int);
2238                       if (ctxp->osb_number)
2239                         ctxp->osb_number = xrealloc (ctxp->osb_number,
2240                                                      allocate);
2241                       else
2242                         ctxp->osb_number = xmalloc (allocate);
2243                     }
2244                   ctxp->osb_depth++;
2245                   CURRENT_OSB (ctxp) = 1;
2246                 }
2247 |       dims OSB_TK CSB_TK
2248                 { CURRENT_OSB (ctxp)++; }
2249 |       dims OSB_TK error
2250                 { yyerror ("']' expected"); RECOVER;}
2251 ;
2252
2253 field_access:
2254         primary DOT_TK identifier
2255                 { $$ = make_qualified_primary ($1, $3, $2.location); }
2256                 /*  FIXME - REWRITE TO:
2257                 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2258 |       SUPER_TK DOT_TK identifier
2259                 {
2260                   tree super_wfl = build_wfl_node (super_identifier_node);
2261                   SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2262                   $$ = make_qualified_name (super_wfl, $3, $2.location);
2263                 }
2264 |       SUPER_TK error
2265                 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2266 ;
2267
2268 method_invocation:
2269         name OP_TK CP_TK
2270                 { $$ = build_method_invocation ($1, NULL_TREE); }
2271 |       name OP_TK argument_list CP_TK
2272                 { $$ = build_method_invocation ($1, $3); }
2273 |       primary DOT_TK identifier OP_TK CP_TK
2274                 {
2275                   if (TREE_CODE ($1) == THIS_EXPR)
2276                     $$ = build_this_super_qualified_invocation
2277                       (1, $3, NULL_TREE, 0, $2.location);
2278                   else
2279                     {
2280                       tree invok = build_method_invocation ($3, NULL_TREE);
2281                       $$ = make_qualified_primary ($1, invok, $2.location);
2282                     }
2283                 }
2284 |       primary DOT_TK identifier OP_TK argument_list CP_TK
2285                 {
2286                   if (TREE_CODE ($1) == THIS_EXPR)
2287                     $$ = build_this_super_qualified_invocation
2288                       (1, $3, $5, 0, $2.location);
2289                   else
2290                     {
2291                       tree invok = build_method_invocation ($3, $5);
2292                       $$ = make_qualified_primary ($1, invok, $2.location);
2293                     }
2294                 }
2295 |       SUPER_TK DOT_TK identifier OP_TK CP_TK
2296                 {
2297                   $$ = build_this_super_qualified_invocation
2298                     (0, $3, NULL_TREE, $1.location, $2.location);
2299                 }
2300 |       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2301                 {
2302                   $$ = build_this_super_qualified_invocation
2303                     (0, $3, $5, $1.location, $2.location);
2304                 }
2305         /* Screws up thing. I let it here until I'm convinced it can
2306            be removed. FIXME
2307 |       primary DOT_TK error
2308                 {yyerror ("'(' expected"); DRECOVER(bad);} */
2309 |       SUPER_TK DOT_TK error CP_TK
2310                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2311 |       SUPER_TK DOT_TK error DOT_TK
2312                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2313 ;
2314
2315 array_access:
2316         name OSB_TK expression CSB_TK
2317                 { $$ = build_array_ref ($2.location, $1, $3); }
2318 |       primary_no_new_array OSB_TK expression CSB_TK
2319                 { $$ = build_array_ref ($2.location, $1, $3); }
2320 |       array_creation_initialized OSB_TK expression CSB_TK
2321                 { $$ = build_array_ref ($2.location, $1, $3); }
2322 |       name OSB_TK error
2323                 {
2324                   yyerror ("Missing term and ']' expected");
2325                   DRECOVER(array_access);
2326                 }
2327 |       name OSB_TK expression error
2328                 {
2329                   yyerror ("']' expected");
2330                   DRECOVER(array_access);
2331                 }
2332 |       primary_no_new_array OSB_TK error
2333                 {
2334                   yyerror ("Missing term and ']' expected");
2335                   DRECOVER(array_access);
2336                 }
2337 |       primary_no_new_array OSB_TK expression error
2338                 {
2339                   yyerror ("']' expected");
2340                   DRECOVER(array_access);
2341                 }
2342 |       array_creation_initialized OSB_TK error
2343                 {
2344                   yyerror ("Missing term and ']' expected");
2345                   DRECOVER(array_access);
2346                 }
2347 |       array_creation_initialized OSB_TK expression error
2348                 {
2349                   yyerror ("']' expected");
2350                   DRECOVER(array_access);
2351                 }
2352 ;
2353
2354 postfix_expression:
2355         primary
2356 |       name
2357 |       post_increment_expression
2358 |       post_decrement_expression
2359 ;
2360
2361 post_increment_expression:
2362         postfix_expression INCR_TK
2363                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2364 ;
2365
2366 post_decrement_expression:
2367         postfix_expression DECR_TK
2368                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2369 ;
2370
2371 trap_overflow_corner_case:
2372         pre_increment_expression
2373 |       pre_decrement_expression
2374 |       PLUS_TK unary_expression
2375                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2376 |       unary_expression_not_plus_minus
2377 |       PLUS_TK error
2378                 {yyerror ("Missing term"); RECOVER}
2379 ;
2380
2381 unary_expression:
2382         trap_overflow_corner_case
2383                 {
2384                   if ($1)
2385                     error_if_numeric_overflow ($1);
2386                   $$ = $1;
2387                 }
2388 |       MINUS_TK trap_overflow_corner_case
2389                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2390 |       MINUS_TK error
2391                 {yyerror ("Missing term"); RECOVER}
2392 ;
2393
2394 pre_increment_expression:
2395         INCR_TK unary_expression
2396                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2397 |       INCR_TK error
2398                 {yyerror ("Missing term"); RECOVER}
2399 ;
2400
2401 pre_decrement_expression:
2402         DECR_TK unary_expression
2403                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2404 |       DECR_TK error
2405                 {yyerror ("Missing term"); RECOVER}
2406 ;
2407
2408 unary_expression_not_plus_minus:
2409         postfix_expression
2410 |       NOT_TK unary_expression
2411                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2412 |       NEG_TK unary_expression
2413                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2414 |       cast_expression
2415 |       NOT_TK error
2416                 {yyerror ("Missing term"); RECOVER}
2417 |       NEG_TK error
2418                 {yyerror ("Missing term"); RECOVER}
2419 ;
2420
2421 cast_expression:                /* Error handling here is potentially weak */
2422         OP_TK primitive_type dims CP_TK unary_expression
2423                 {
2424                   tree type = $2;
2425                   int osb = pop_current_osb (ctxp);
2426                   while (osb--)
2427                     type = build_java_array_type (type, -1);
2428                   $$ = build_cast ($1.location, type, $5);
2429                 }
2430 |       OP_TK primitive_type CP_TK unary_expression
2431                 { $$ = build_cast ($1.location, $2, $4); }
2432 |       OP_TK expression CP_TK unary_expression_not_plus_minus
2433                 { $$ = build_cast ($1.location, $2, $4); }
2434 |       OP_TK name dims CP_TK unary_expression_not_plus_minus
2435                 {
2436                   const char *ptr;
2437                   int osb = pop_current_osb (ctxp);
2438                   obstack_grow (&temporary_obstack,
2439                                 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2440                                 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2441                   while (osb--)
2442                     obstack_grow (&temporary_obstack, "[]", 2);
2443                   obstack_1grow (&temporary_obstack, '\0');
2444                   ptr = obstack_finish (&temporary_obstack);
2445                   EXPR_WFL_NODE ($2) = get_identifier (ptr);
2446                   $$ = build_cast ($1.location, $2, $5);
2447                 }
2448 |       OP_TK primitive_type OSB_TK error
2449                 {yyerror ("']' expected, invalid type expression");}
2450 |       OP_TK error
2451                 {
2452                   YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2453                   RECOVER;
2454                 }
2455 |       OP_TK primitive_type dims CP_TK error
2456                 {yyerror ("Missing term"); RECOVER;}
2457 |       OP_TK primitive_type CP_TK error
2458                 {yyerror ("Missing term"); RECOVER;}
2459 |       OP_TK name dims CP_TK error
2460                 {yyerror ("Missing term"); RECOVER;}
2461 ;
2462
2463 multiplicative_expression:
2464         unary_expression
2465 |       multiplicative_expression MULT_TK unary_expression
2466                 {
2467                   $$ = build_binop (BINOP_LOOKUP ($2.token),
2468                                     $2.location, $1, $3);
2469                 }
2470 |       multiplicative_expression DIV_TK unary_expression
2471                 {
2472                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2473                                     $1, $3);
2474                 }
2475 |       multiplicative_expression REM_TK unary_expression
2476                 {
2477                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478                                     $1, $3);
2479                 }
2480 |       multiplicative_expression MULT_TK error
2481                 {yyerror ("Missing term"); RECOVER;}
2482 |       multiplicative_expression DIV_TK error
2483                 {yyerror ("Missing term"); RECOVER;}
2484 |       multiplicative_expression REM_TK error
2485                 {yyerror ("Missing term"); RECOVER;}
2486 ;
2487
2488 additive_expression:
2489         multiplicative_expression
2490 |       additive_expression PLUS_TK multiplicative_expression
2491                 {
2492                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2493                                     $1, $3);
2494                 }
2495 |       additive_expression MINUS_TK multiplicative_expression
2496                 {
2497                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2498                                     $1, $3);
2499                 }
2500 |       additive_expression PLUS_TK error
2501                 {yyerror ("Missing term"); RECOVER;}
2502 |       additive_expression MINUS_TK error
2503                 {yyerror ("Missing term"); RECOVER;}
2504 ;
2505
2506 shift_expression:
2507         additive_expression
2508 |       shift_expression LS_TK additive_expression
2509                 {
2510                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2511                                     $1, $3);
2512                 }
2513 |       shift_expression SRS_TK additive_expression
2514                 {
2515                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2516                                     $1, $3);
2517                 }
2518 |       shift_expression ZRS_TK additive_expression
2519                 {
2520                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2521                                     $1, $3);
2522                 }
2523 |       shift_expression LS_TK error
2524                 {yyerror ("Missing term"); RECOVER;}
2525 |       shift_expression SRS_TK error
2526                 {yyerror ("Missing term"); RECOVER;}
2527 |       shift_expression ZRS_TK error
2528                 {yyerror ("Missing term"); RECOVER;}
2529 ;
2530
2531 relational_expression:
2532         shift_expression
2533 |       relational_expression LT_TK shift_expression
2534                 {
2535                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2536                                     $1, $3);
2537                 }
2538 |       relational_expression GT_TK shift_expression
2539                 {
2540                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2541                                     $1, $3);
2542                 }
2543 |       relational_expression LTE_TK shift_expression
2544                 {
2545                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2546                                     $1, $3);
2547                 }
2548 |       relational_expression GTE_TK shift_expression
2549                 {
2550                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2551                                     $1, $3);
2552                 }
2553 |       relational_expression INSTANCEOF_TK reference_type
2554                 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2555 |       relational_expression LT_TK error
2556                 {yyerror ("Missing term"); RECOVER;}
2557 |       relational_expression GT_TK error
2558                 {yyerror ("Missing term"); RECOVER;}
2559 |       relational_expression LTE_TK error
2560                 {yyerror ("Missing term"); RECOVER;}
2561 |       relational_expression GTE_TK error
2562                 {yyerror ("Missing term"); RECOVER;}
2563 |       relational_expression INSTANCEOF_TK error
2564                 {yyerror ("Invalid reference type"); RECOVER;}
2565 ;
2566
2567 equality_expression:
2568         relational_expression
2569 |       equality_expression EQ_TK relational_expression
2570                 {
2571                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2572                                     $1, $3);
2573                 }
2574 |       equality_expression NEQ_TK relational_expression
2575                 {
2576                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2577                                     $1, $3);
2578                 }
2579 |       equality_expression EQ_TK error
2580                 {yyerror ("Missing term"); RECOVER;}
2581 |       equality_expression NEQ_TK error
2582                 {yyerror ("Missing term"); RECOVER;}
2583 ;
2584
2585 and_expression:
2586         equality_expression
2587 |       and_expression AND_TK equality_expression
2588                 {
2589                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2590                                     $1, $3);
2591                 }
2592 |       and_expression AND_TK error
2593                 {yyerror ("Missing term"); RECOVER;}
2594 ;
2595
2596 exclusive_or_expression:
2597         and_expression
2598 |       exclusive_or_expression XOR_TK and_expression
2599                 {
2600                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2601                                     $1, $3);
2602                 }
2603 |       exclusive_or_expression XOR_TK error
2604                 {yyerror ("Missing term"); RECOVER;}
2605 ;
2606
2607 inclusive_or_expression:
2608         exclusive_or_expression
2609 |       inclusive_or_expression OR_TK exclusive_or_expression
2610                 {
2611                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2612                                     $1, $3);
2613                 }
2614 |       inclusive_or_expression OR_TK error
2615                 {yyerror ("Missing term"); RECOVER;}
2616 ;
2617
2618 conditional_and_expression:
2619         inclusive_or_expression
2620 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2621                 {
2622                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2623                                     $1, $3);
2624                 }
2625 |       conditional_and_expression BOOL_AND_TK error
2626                 {yyerror ("Missing term"); RECOVER;}
2627 ;
2628
2629 conditional_or_expression:
2630         conditional_and_expression
2631 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
2632                 {
2633                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2634                                     $1, $3);
2635                 }
2636 |       conditional_or_expression BOOL_OR_TK error
2637                 {yyerror ("Missing term"); RECOVER;}
2638 ;
2639
2640 conditional_expression:         /* Error handling here is weak */
2641         conditional_or_expression
2642 |       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2643                 {
2644                   $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2645                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2646                 }
2647 |       conditional_or_expression REL_QM_TK REL_CL_TK error
2648                 {
2649                   YYERROR_NOW;
2650                   yyerror ("Missing term");
2651                   DRECOVER (1);
2652                 }
2653 |       conditional_or_expression REL_QM_TK error
2654                 {yyerror ("Missing term"); DRECOVER (2);}
2655 |       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2656                 {yyerror ("Missing term"); DRECOVER (3);}
2657 ;
2658
2659 assignment_expression:
2660         conditional_expression
2661 |       assignment
2662 ;
2663
2664 assignment:
2665         left_hand_side assignment_operator assignment_expression
2666                 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2667 |       left_hand_side assignment_operator error
2668                 {
2669                   YYNOT_TWICE yyerror ("Missing term");
2670                   DRECOVER (assign);
2671                 }
2672 ;
2673
2674 left_hand_side:
2675         name
2676 |       field_access
2677 |       array_access
2678 ;
2679
2680 assignment_operator:
2681         ASSIGN_ANY_TK
2682 |       ASSIGN_TK
2683 ;
2684
2685 expression:
2686         assignment_expression
2687 ;
2688
2689 constant_expression:
2690         expression
2691 ;
2692
2693 %%
2694
2695 /* Helper function to retrieve an OSB count. Should be used when the
2696    `dims:' rule is being used.  */
2697
2698 static int
2699 pop_current_osb (struct parser_ctxt *ctxp)
2700 {
2701   int to_return;
2702
2703   if (ctxp->osb_depth < 0)
2704     abort ();
2705
2706   to_return = CURRENT_OSB (ctxp);
2707   ctxp->osb_depth--;
2708
2709   return to_return;
2710 }
2711
2712 \f
2713
2714 /* This section of the code deal with save/restoring parser contexts.
2715    Add mode documentation here. FIXME */
2716
2717 /* Helper function. Create a new parser context. With
2718    COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2719    context is copied, otherwise, the new context is zeroed. The newly
2720    created context becomes the current one.  */
2721
2722 static void
2723 create_new_parser_context (int copy_from_previous)
2724 {
2725   struct parser_ctxt *new;
2726
2727   new = ggc_alloc (sizeof (struct parser_ctxt));
2728   if (copy_from_previous)
2729     {
2730       memcpy (new, ctxp, sizeof (struct parser_ctxt));
2731       /* This flag, indicating the context saves global values,
2732          should only be set by java_parser_context_save_global.  */
2733       new->saved_data_ctx = 0;
2734     }
2735   else
2736     memset (new, 0, sizeof (struct parser_ctxt));
2737
2738   new->next = ctxp;
2739   ctxp = new;
2740 }
2741
2742 /* Create a new parser context and make it the current one. */
2743
2744 void
2745 java_push_parser_context (void)
2746 {
2747   create_new_parser_context (0);
2748 }
2749
2750 void
2751 java_pop_parser_context (int generate)
2752 {
2753   tree current;
2754   struct parser_ctxt *next;
2755
2756   if (!ctxp)
2757     return;
2758
2759   next = ctxp->next;
2760   if (next)
2761     {
2762       input_location = ctxp->save_location;
2763       current_class = ctxp->class_type;
2764     }
2765
2766   /* If the old and new lexers differ, then free the old one.  */
2767   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2768     java_destroy_lexer (ctxp->lexer);
2769
2770   /* Set the single import class file flag to 0 for the current list
2771      of imported things */
2772   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2773     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2774
2775   /* If we pushed a context to parse a class intended to be generated,
2776      we keep it so we can remember the class. What we could actually
2777      do is to just update a list of class names.  */
2778   if (generate)
2779     {
2780       ctxp->next = ctxp_for_generation;
2781       ctxp_for_generation = ctxp;
2782     }
2783
2784   /* And restore those of the previous context */
2785   if ((ctxp = next))            /* Assignment is really meant here */
2786     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2787       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2788 }
2789
2790 /* Create a parser context for the use of saving some global
2791    variables.  */
2792
2793 void
2794 java_parser_context_save_global (void)
2795 {
2796   if (!ctxp)
2797     {
2798       java_push_parser_context ();
2799       ctxp->saved_data_ctx = 1;
2800     }
2801
2802   /* If this context already stores data, create a new one suitable
2803      for data storage. */
2804   else if (ctxp->saved_data)
2805     {
2806       create_new_parser_context (1);
2807       ctxp->saved_data_ctx = 1;
2808     }
2809
2810   ctxp->save_location = input_location;
2811   ctxp->class_type = current_class;
2812   ctxp->function_decl = current_function_decl;
2813   ctxp->saved_data = 1;
2814 }
2815
2816 /* Restore some global variables from the previous context. Make the
2817    previous context the current one.  */
2818
2819 void
2820 java_parser_context_restore_global (void)
2821 {
2822   input_location = ctxp->save_location;
2823   current_class = ctxp->class_type;
2824   if (wfl_operator)
2825 #ifdef USE_MAPPED_LOCATION
2826     SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2827 #else
2828     EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2829 #endif
2830   current_function_decl = ctxp->function_decl;
2831   ctxp->saved_data = 0;
2832   if (ctxp->saved_data_ctx)
2833     java_pop_parser_context (0);
2834 }
2835
2836 /* Suspend vital data for the current class/function being parsed so
2837    that an other class can be parsed. Used to let local/anonymous
2838    classes be parsed.  */
2839
2840 static void
2841 java_parser_context_suspend (void)
2842 {
2843   /* This makes debugging through java_debug_context easier */
2844   static const char *const name = "<inner buffer context>";
2845
2846   /* Duplicate the previous context, use it to save the globals we're
2847      interested in */
2848   create_new_parser_context (1);
2849   ctxp->function_decl = current_function_decl;
2850   ctxp->class_type = current_class;
2851
2852   /* Then create a new context which inherits all data from the
2853      previous one. This will be the new current context  */
2854   create_new_parser_context (1);
2855
2856   /* Help debugging */
2857   ctxp->next->filename = name;
2858 }
2859
2860 /* Resume vital data for the current class/function being parsed so
2861    that an other class can be parsed. Used to let local/anonymous
2862    classes be parsed.  The trick is the data storing file position
2863    informations must be restored to their current value, so parsing
2864    can resume as if no context was ever saved. */
2865
2866 static void
2867 java_parser_context_resume (void)
2868 {
2869   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2870   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2871   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2872
2873   /* We need to inherit the list of classes to complete/generate */
2874   restored->classd_list = old->classd_list;
2875   restored->class_list = old->class_list;
2876
2877   /* Restore the current class and function from the saver */
2878   current_class = saver->class_type;
2879   current_function_decl = saver->function_decl;
2880
2881   /* Retrieve the restored context */
2882   ctxp = restored;
2883
2884   /* Re-installed the data for the parsing to carry on */
2885   memcpy (&ctxp->marker_begining, &old->marker_begining,
2886           (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2887 }
2888
2889 /* Add a new anchor node to which all statement(s) initializing static
2890    and non static initialized upon declaration field(s) will be
2891    linked.  */
2892
2893 static void
2894 java_parser_context_push_initialized_field (void)
2895 {
2896   tree node;
2897
2898   node = build_tree_list (NULL_TREE, NULL_TREE);
2899   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2900   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2901
2902   node = build_tree_list (NULL_TREE, NULL_TREE);
2903   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2904   CPC_INITIALIZER_LIST (ctxp) = node;
2905
2906   node = build_tree_list (NULL_TREE, NULL_TREE);
2907   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2908   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2909 }
2910
2911 /* Pop the lists of initialized field. If this lists aren't empty,
2912    remember them so we can use it to create and populate the finit$
2913    or <clinit> functions. */
2914
2915 static void
2916 java_parser_context_pop_initialized_field (void)
2917 {
2918   tree stmts;
2919   tree class_type = TREE_TYPE (GET_CPC ());
2920
2921   if (CPC_INITIALIZER_LIST (ctxp))
2922     {
2923       stmts = CPC_INITIALIZER_STMT (ctxp);
2924       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2925       if (stmts && !java_error_count)
2926         TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2927     }
2928
2929   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2930     {
2931       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2932       CPC_STATIC_INITIALIZER_LIST (ctxp) =
2933         TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2934       /* Keep initialization in order to enforce 8.5 */
2935       if (stmts && !java_error_count)
2936         TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2937     }
2938
2939   /* JDK 1.1 instance initializers */
2940   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2941     {
2942       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2943       CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2944         TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2945       if (stmts && !java_error_count)
2946         TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2947     }
2948 }
2949
2950 static tree
2951 reorder_static_initialized (tree list)
2952 {
2953   /* We have to keep things in order. The alias initializer have to
2954      come first, then the initialized regular field, in reverse to
2955      keep them in lexical order. */
2956   tree marker, previous = NULL_TREE;
2957   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2958     if (TREE_CODE (marker) == TREE_LIST
2959         && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2960       break;
2961
2962   /* No static initialized, the list is fine as is */
2963   if (!previous)
2964     list = TREE_CHAIN (marker);
2965
2966   /* No marker? reverse the whole list */
2967   else if (!marker)
2968     list = nreverse (list);
2969
2970   /* Otherwise, reverse what's after the marker and the new reordered
2971      sublist will replace the marker. */
2972   else
2973     {
2974       TREE_CHAIN (previous) = NULL_TREE;
2975       list = nreverse (list);
2976       list = chainon (TREE_CHAIN (marker), list);
2977     }
2978   return list;
2979 }
2980
2981 /* Helper functions to dump the parser context stack.  */
2982
2983 #define TAB_CONTEXT(C) \
2984   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2985
2986 static void
2987 java_debug_context_do (int tab)
2988 {
2989   struct parser_ctxt *copy = ctxp;
2990   while (copy)
2991     {
2992       TAB_CONTEXT (tab);
2993       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2994       TAB_CONTEXT (tab);
2995       fprintf (stderr, "filename: %s\n", copy->filename);
2996       TAB_CONTEXT (tab);
2997       fprintf (stderr, "package: %s\n",
2998                (copy->package ?
2999                 IDENTIFIER_POINTER (copy->package) : "<none>"));
3000       TAB_CONTEXT (tab);
3001       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
3002       TAB_CONTEXT (tab);
3003       fprintf (stderr, "saved data: %d\n", copy->saved_data);
3004       copy = copy->next;
3005       tab += 2;
3006     }
3007 }
3008
3009 /* Dump the stacked up parser contexts. Intended to be called from a
3010    debugger.  */
3011
3012 void
3013 java_debug_context (void)
3014 {
3015   java_debug_context_do (0);
3016 }
3017
3018 \f
3019
3020 /* Flag for the error report routine to issue the error the first time
3021    it's called (overriding the default behavior which is to drop the
3022    first invocation and honor the second one, taking advantage of a
3023    richer context.  */
3024 static int force_error = 0;
3025
3026 /* Reporting an constructor invocation error.  */
3027 static void
3028 parse_ctor_invocation_error (void)
3029 {
3030   if (DECL_CONSTRUCTOR_P (current_function_decl))
3031     yyerror ("Constructor invocation must be first thing in a constructor");
3032   else
3033     yyerror ("Only constructors can invoke constructors");
3034 }
3035
3036 /* Reporting JDK1.1 features not implemented.  */
3037
3038 static tree
3039 parse_jdk1_1_error (const char *msg)
3040 {
3041   sorry (": %qs JDK1.1(TM) feature", msg);
3042   java_error_count++;
3043   return build_java_empty_stmt ();
3044 }
3045
3046 static int do_warning = 0;
3047
3048 void
3049 yyerror (const char *msgid)
3050 {
3051 #ifdef USE_MAPPED_LOCATION
3052   static source_location elc;
3053   expanded_location xloc = expand_location (input_location);
3054   int current_line = xloc.line;
3055 #else
3056   static java_lc elc;
3057   int save_lineno;
3058   int current_line = input_line;
3059 #endif
3060   static int prev_lineno;
3061   static const char *prev_msg;
3062
3063   char *remainder, *code_from_source;
3064
3065   if (!force_error && prev_lineno == current_line)
3066     return;
3067 #ifndef USE_MAPPED_LOCATION
3068   current_line = ctxp->lexer->token_start.line;
3069 #endif
3070
3071   /* Save current error location but report latter, when the context is
3072      richer.  */
3073   if (ctxp->java_error_flag == 0)
3074     {
3075       ctxp->java_error_flag = 1;
3076 #ifdef USE_MAPPED_LOCATION
3077       elc = input_location;
3078 #else
3079       elc = ctxp->lexer->token_start;
3080 #endif
3081       /* Do something to use the previous line if we're reaching the
3082          end of the file... */
3083 #ifdef VERBOSE_SKELETON
3084       printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3085 #endif
3086       return;
3087     }
3088
3089   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3090   if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3091     return;
3092
3093   ctxp->java_error_flag = 0;
3094   if (do_warning)
3095     java_warning_count++;
3096   else
3097     java_error_count++;
3098
3099 #if 0 /* FIXME */
3100   if (elc.col == 0 && msgid && msgid[1] == ';')
3101     elc = ctxp->prev_line_end;
3102 #endif
3103
3104   prev_msg = msgid;
3105
3106 #ifdef USE_MAPPED_LOCATION
3107   prev_lineno = current_line;
3108   code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3109 #else
3110   save_lineno = input_line;
3111   prev_lineno = input_line = current_line;
3112   code_from_source = java_get_line_col (input_filename, current_line,
3113                                         ctxp->lexer->token_start.col);
3114 #endif
3115
3116
3117   obstack_grow0 (&temporary_obstack,
3118                  code_from_source, strlen (code_from_source));
3119   remainder = obstack_finish (&temporary_obstack);
3120   if (do_warning)
3121     warning ("%s.\n%s", msgid, remainder);
3122   else
3123     error ("%s.\n%s", msgid, remainder);
3124
3125   /* This allow us to cheaply avoid an extra 'Invalid expression
3126      statement' error report when errors have been already reported on
3127      the same line. This occurs when we report an error but don't have
3128      a synchronization point other than ';', which
3129      expression_statement is the only one to take care of.  */
3130 #ifndef USE_MAPPED_LOCATION
3131   input_line = save_lineno;
3132 #endif
3133   ctxp->prevent_ese = input_line;
3134 }
3135
3136 static void
3137 issue_warning_error_from_context (
3138 #ifdef USE_MAPPED_LOCATION
3139                                   source_location cl,
3140 #else
3141                                   tree cl,
3142 #endif
3143                                   const char *msgid, va_list *ap)
3144 {
3145 #ifdef USE_MAPPED_LOCATION
3146   source_location saved_location = input_location;
3147   expanded_location xloc = expand_location (cl);
3148 #else
3149   java_lc save_lc = ctxp->lexer->token_start;
3150   const char *saved = ctxp->filename, *saved_input_filename;
3151 #endif
3152   char buffer [4096];
3153   text_info text;
3154
3155   text.err_no = errno;
3156   text.args_ptr = ap;
3157   text.format_spec = msgid;
3158   pp_format_text (global_dc->printer, &text);
3159   strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3160   buffer[sizeof (buffer) - 1] = '\0';
3161   pp_clear_output_area (global_dc->printer);
3162
3163   force_error = 1;
3164
3165 #ifdef USE_MAPPED_LOCATION
3166   if (xloc.file != NULL)
3167     {
3168       ctxp->filename = xloc.file;
3169       input_location = cl;
3170     }
3171 #else
3172   ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3173   ctxp->lexer->token_start.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3174                                    : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3175                                    : EXPR_WFL_COLNO (cl));
3176
3177   /* We have a CL, that's a good reason for using it if it contains data */
3178   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3179     ctxp->filename = EXPR_WFL_FILENAME (cl);
3180   saved_input_filename = input_filename;
3181   input_filename = ctxp->filename;
3182 #endif
3183   java_error (NULL);
3184   java_error (buffer);
3185 #ifdef USE_MAPPED_LOCATION
3186   input_location = saved_location;
3187 #else
3188   ctxp->filename = saved;
3189   input_filename = saved_input_filename;
3190   ctxp->lexer->token_start = save_lc;
3191 #endif
3192   force_error = 0;
3193 }
3194
3195 /* Issue an error message at a current source line CL.
3196    FUTURE/FIXME:  change cl to be a source_location. */
3197
3198 void
3199 parse_error_context (tree cl, const char *msgid, ...)
3200 {
3201   va_list ap;
3202   va_start (ap, msgid);
3203 #ifdef USE_MAPPED_LOCATION
3204   issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3205 #else
3206   issue_warning_error_from_context (cl, msgid, &ap);
3207 #endif
3208   va_end (ap);
3209 }
3210
3211 /* Issue a warning at a current source line CL.
3212    FUTURE/FIXME:  change cl to be a source_location. */
3213
3214 static void
3215 parse_warning_context (tree cl, const char *msgid, ...)
3216 {
3217   va_list ap;
3218   va_start (ap, msgid);
3219
3220   do_warning = 1;
3221 #ifdef USE_MAPPED_LOCATION
3222   issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3223 #else
3224   issue_warning_error_from_context (cl, msgid, &ap);
3225 #endif
3226   do_warning = 0;
3227   va_end (ap);
3228 }
3229
3230 static tree
3231 find_expr_with_wfl (tree node)
3232 {
3233   while (node)
3234     {
3235       enum tree_code_class code;
3236       tree to_return;
3237
3238       switch (TREE_CODE (node))
3239         {
3240         case BLOCK:
3241           node = BLOCK_EXPR_BODY (node);
3242           continue;
3243
3244         case COMPOUND_EXPR:
3245           to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3246           if (to_return)
3247             return to_return;
3248           node = TREE_OPERAND (node, 1);
3249           continue;
3250
3251         case LOOP_EXPR:
3252           node = TREE_OPERAND (node, 0);
3253           continue;
3254
3255         case LABELED_BLOCK_EXPR:
3256           node = LABELED_BLOCK_BODY (node);
3257           continue;
3258
3259         default:
3260           code = TREE_CODE_CLASS (TREE_CODE (node));
3261           if (((code == tcc_unary) || (code == tcc_binary)
3262                || (code == tcc_expression))
3263               && EXPR_WFL_LINECOL (node))
3264             return node;
3265           return NULL_TREE;
3266         }
3267     }
3268   return NULL_TREE;
3269 }
3270
3271 /* Issue a missing return statement error. Uses METHOD to figure the
3272    last line of the method the error occurs in.  */
3273
3274 static void
3275 missing_return_error (tree method)
3276 {
3277 #ifdef USE_MAPPED_LOCATION
3278   SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3279 #else
3280   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3281 #endif
3282   parse_error_context (wfl_operator, "Missing return statement");
3283 }
3284
3285 /* Issue an unreachable statement error. From NODE, find the next
3286    statement to report appropriately.  */
3287 static void
3288 unreachable_stmt_error (tree node)
3289 {
3290   /* Browse node to find the next expression node that has a WFL. Use
3291      the location to report the error */
3292   if (TREE_CODE (node) == COMPOUND_EXPR)
3293     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3294   else
3295     node = find_expr_with_wfl (node);
3296
3297   if (node)
3298     {
3299 #ifdef USE_MAPPED_LOCATION
3300       SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3301 #else
3302       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3303 #endif
3304       parse_error_context (wfl_operator, "Unreachable statement");
3305     }
3306   else
3307     abort ();
3308 }
3309
3310 static int
3311 not_accessible_field_error (tree wfl, tree decl)
3312 {
3313   parse_error_context 
3314     (wfl, "Can't access %s field %<%s.%s%> from %qs",
3315      accessibility_string (get_access_flags_from_decl (decl)),
3316      GET_TYPE_NAME (DECL_CONTEXT (decl)),
3317      IDENTIFIER_POINTER (DECL_NAME (decl)),
3318      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3319   return 1;
3320 }
3321
3322 int
3323 java_report_errors (void)
3324 {
3325   if (java_error_count)
3326     fprintf (stderr, "%d error%s",
3327              java_error_count, (java_error_count == 1 ? "" : "s"));
3328   if (java_warning_count)
3329     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3330              java_warning_count, (java_warning_count == 1 ? "" : "s"));
3331   if (java_error_count || java_warning_count)
3332     putc ('\n', stderr);
3333   return java_error_count;
3334 }
3335
3336 static char *
3337 java_accstring_lookup (int flags)
3338 {
3339   static char buffer [80];
3340 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3341
3342   /* Access modifier looked-up first for easier report on forbidden
3343      access. */
3344   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3345   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3346   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3347   if (flags & ACC_STATIC) COPY_RETURN ("static");
3348   if (flags & ACC_FINAL) COPY_RETURN ("final");
3349   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3350   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3351   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3352   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3353   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3354   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3355
3356   buffer [0] = '\0';
3357   return buffer;
3358 #undef COPY_RETURN
3359 }
3360
3361 /* Returns a string denoting the accessibility of a class or a member as
3362    indicated by FLAGS.  We need a separate function from
3363    java_accstring_lookup, as the latter can return spurious "static", etc.
3364    if package-private access is defined (in which case none of the
3365    relevant access control bits in FLAGS is set).  */
3366
3367 static const char *
3368 accessibility_string (int flags)
3369 {
3370   if (flags & ACC_PRIVATE) return "private";
3371   if (flags & ACC_PROTECTED) return "protected";
3372   if (flags & ACC_PUBLIC) return "public";
3373
3374   return "package-private";
3375 }
3376
3377 /* Issuing error messages upon redefinition of classes, interfaces or
3378    variables. */
3379
3380 static void
3381 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3382 {
3383   parse_error_context (cl, "%s %qs already defined in %s:%d",
3384                        context, IDENTIFIER_POINTER (id),
3385                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3386   /* Here we should point out where its redefined. It's a unicode. FIXME */
3387 }
3388
3389 static void
3390 variable_redefinition_error (tree context, tree name, tree type, int line)
3391 {
3392   const char *type_name;
3393
3394   /* Figure a proper name for type. We might haven't resolved it */
3395   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3396     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3397   else
3398     type_name = lang_printable_name (type, 0);
3399
3400   parse_error_context (context,
3401                        "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3402                        IDENTIFIER_POINTER (name),
3403                        type_name, IDENTIFIER_POINTER (name), line);
3404 }
3405
3406 /* If ANAME is terminated with `[]', it indicates an array. This
3407    function returns the number of `[]' found and if this number is
3408    greater than zero, it extracts the array type name and places it in
3409    the node pointed to by TRIMMED unless TRIMMED is null.  */
3410
3411 static int
3412 build_type_name_from_array_name (tree aname, tree *trimmed)
3413 {
3414   const char *name = IDENTIFIER_POINTER (aname);
3415   int len = IDENTIFIER_LENGTH (aname);
3416   int array_dims;
3417
3418   STRING_STRIP_BRACKETS (name, len, array_dims);
3419
3420   if (array_dims && trimmed)
3421     *trimmed = get_identifier_with_length (name, len);
3422
3423   return array_dims;
3424 }
3425
3426 static tree
3427 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3428 {
3429   int more_dims = 0;
3430
3431   /* Eventually get more dims */
3432   more_dims = build_type_name_from_array_name (name, &name);
3433
3434   /* If we have, then craft a new type for this variable */
3435   if (more_dims)
3436     {
3437       tree save = type;
3438
3439       /* If we have a pointer, use its type */
3440       if (TREE_CODE (type) == POINTER_TYPE)
3441         type = TREE_TYPE (type);
3442
3443       /* Building the first dimension of a primitive type uses this
3444          function */
3445       if (JPRIMITIVE_TYPE_P (type))
3446         {
3447           type = build_java_array_type (type, -1);
3448           more_dims--;
3449         }
3450       /* Otherwise, if we have a WFL for this type, use it (the type
3451          is already an array on an unresolved type, and we just keep
3452          on adding dimensions) */
3453       else if (type_wfl)
3454         {
3455           type = type_wfl;
3456           more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3457                                                         NULL);
3458         }
3459
3460       /* Add all the dimensions */
3461       while (more_dims--)
3462         type = build_unresolved_array_type (type);
3463
3464       /* The type may have been incomplete in the first place */
3465       if (type_wfl)
3466         type = obtain_incomplete_type (type);
3467     }
3468
3469   if (ret_name)
3470     *ret_name = name;
3471   return type;
3472 }
3473
3474 /* Build something that the type identifier resolver will identify as
3475    being an array to an unresolved type. TYPE_WFL is a WFL on a
3476    identifier. */
3477
3478 static tree
3479 build_unresolved_array_type (tree type_or_wfl)
3480 {
3481   const char *ptr;
3482   tree wfl;
3483
3484   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3485      just create a array type */
3486   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3487     return build_java_array_type (type_or_wfl, -1);
3488
3489   obstack_grow (&temporary_obstack,
3490                  IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3491                  IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3492   obstack_grow0 (&temporary_obstack, "[]", 2);
3493   ptr = obstack_finish (&temporary_obstack);
3494 #ifdef USE_MAPPED_LOCATION
3495   wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3496 #else
3497   wfl = build_expr_wfl (get_identifier (ptr),
3498                         EXPR_WFL_FILENAME (type_or_wfl),
3499                         EXPR_WFL_LINENO (type_or_wfl),
3500                         EXPR_WFL_COLNO (type_or_wfl));
3501 #endif
3502   /* Re-install the existing qualifications so that the type can be
3503      resolved properly. */
3504   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3505   return wfl;
3506 }
3507
3508 static void
3509 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3510 {
3511   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3512     parse_error_context (wfl, "Interface %qs repeated",
3513                          IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3514 }
3515
3516 /* Bulk of common class/interface checks. Return 1 if an error was
3517    encountered. TAG is 0 for a class, 1 for an interface.  */
3518
3519 static int
3520 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3521                                 tree qualified_name, tree decl, tree cl)
3522 {
3523   tree node;
3524   int sca = 0;                  /* Static class allowed */
3525   int icaf = 0;                 /* Inner class allowed flags */
3526   int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3527
3528   if (!quiet_flag)
3529     fprintf (stderr, " %s%s %s",
3530              (CPC_INNER_P () ? "inner" : ""),
3531              (is_interface ? "interface" : "class"),
3532              IDENTIFIER_POINTER (qualified_name));
3533
3534   /* Scope of an interface/class type name:
3535        - Can't be imported by a single type import
3536        - Can't already exists in the package */
3537   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3538       && (node = find_name_in_single_imports (raw_name))
3539       && !CPC_INNER_P ())
3540     {
3541       parse_error_context
3542         (cl, "%s name %qs clashes with imported type %qs",
3543          (is_interface ? "Interface" : "Class"),
3544          IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3545       return 1;
3546     }
3547   if (decl && CLASS_COMPLETE_P (decl))
3548     {
3549       classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3550                                    qualified_name, decl, cl);
3551       return 1;
3552     }
3553
3554   if (check_inner_class_redefinition (raw_name, cl))
3555     return 1;
3556
3557   /* If public, file name should match class/interface name, except
3558      when dealing with an inner class */
3559   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3560     {
3561       const char *fname = input_filename;
3562       const char *f;
3563
3564       for (f = fname + strlen (fname);
3565            f != fname && ! IS_DIR_SEPARATOR (*f);
3566            f--)
3567         ;
3568       if (IS_DIR_SEPARATOR (*f))
3569         f++;
3570       if (strncmp (IDENTIFIER_POINTER (raw_name),
3571                    f , IDENTIFIER_LENGTH (raw_name)) ||
3572           f [IDENTIFIER_LENGTH (raw_name)] != '.')
3573         parse_error_context
3574           (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3575                              (is_interface ? "interface" : "class"),
3576                              IDENTIFIER_POINTER (qualified_name),
3577                              IDENTIFIER_POINTER (raw_name));
3578     }
3579
3580   /* Static classes can be declared only in top level classes. Note:
3581      once static, a inner class is a top level class. */
3582   if (flags & ACC_STATIC)
3583     {
3584       /* Catch the specific error of declaring an class inner class
3585          with no toplevel enclosing class. Prevent check_modifiers from
3586          complaining a second time */
3587       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3588         {
3589           parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3590                                IDENTIFIER_POINTER (qualified_name));
3591           sca = ACC_STATIC;
3592         }
3593       /* Else, in the context of a top-level class declaration, let
3594          `check_modifiers' do its job, otherwise, give it a go */
3595       else
3596         sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3597     }
3598
3599   /* Inner classes can be declared private or protected
3600      within their enclosing classes. */
3601   if (CPC_INNER_P ())
3602     {
3603       /* A class which is local to a block can't be public, private,
3604          protected or static. But it is created final, so allow this
3605          one. */
3606       if (current_function_decl)
3607         icaf = sca = uaaf = ACC_FINAL;
3608       else
3609         {
3610           check_modifiers_consistency (flags);
3611           icaf = ACC_PROTECTED;
3612           if (! CLASS_INTERFACE (GET_CPC ()))
3613             icaf |= ACC_PRIVATE;
3614         }
3615     }
3616
3617   if (is_interface)
3618     {
3619       if (CPC_INNER_P ())
3620         uaaf = INTERFACE_INNER_MODIFIERS;
3621       else
3622         uaaf = INTERFACE_MODIFIERS;
3623
3624       check_modifiers ("Illegal modifier %qs for interface declaration",
3625                        flags, uaaf);
3626     }
3627   else
3628     check_modifiers ((current_function_decl ?
3629                       "Illegal modifier %qs for local class declaration" :
3630                       "Illegal modifier %qs for class declaration"),
3631                      flags, uaaf|sca|icaf);
3632   return 0;
3633 }
3634
3635 /* Construct a nested class name.  If the final component starts with
3636    a digit, return true.  Otherwise return false.  */
3637 static int
3638 make_nested_class_name (tree cpc_list)
3639 {
3640   tree name;
3641
3642   if (!cpc_list)
3643     return 0;
3644
3645   make_nested_class_name (TREE_CHAIN (cpc_list));
3646
3647   /* Pick the qualified name when dealing with the first upmost
3648      enclosing class */
3649   name = (TREE_CHAIN (cpc_list)
3650           ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3651   obstack_grow (&temporary_obstack,
3652                 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3653   obstack_1grow (&temporary_obstack, '$');
3654
3655   return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3656 }
3657
3658 /* Can't redefine a class already defined in an earlier scope. */
3659
3660 static int
3661 check_inner_class_redefinition (tree raw_name, tree cl)
3662 {
3663   tree scope_list;
3664
3665   for (scope_list = GET_CPC_LIST (); scope_list;
3666        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3667     if (raw_name == GET_CPC_UN_NODE (scope_list))
3668       {
3669         parse_error_context
3670           (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",
3671            IDENTIFIER_POINTER (raw_name));
3672         return 1;
3673       }
3674   return 0;
3675 }
3676
3677 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3678    we remember ENCLOSING and SUPER.  */
3679
3680 static tree
3681 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3682                      tree *super, tree class_type)
3683 {
3684   tree local_enclosing = *enclosing;
3685   tree local_super = NULL_TREE;
3686
3687   while (local_enclosing)
3688     {
3689       tree intermediate, decl;
3690
3691       *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3692         local_enclosing;
3693
3694       if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3695         return decl;
3696
3697       intermediate = local_enclosing;
3698       /* Explore enclosing contexts. */
3699       while (INNER_CLASS_DECL_P (intermediate))
3700         {
3701           intermediate = DECL_CONTEXT (intermediate);
3702           if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3703             return decl;
3704         }
3705
3706       /* Now go to the upper classes, bail out if necessary.  We will
3707          analyze the returned SUPER and act accordingly (see
3708          do_resolve_class).  */
3709       if (JPRIMITIVE_TYPE_P (TREE_TYPE (local_enclosing))
3710           || TREE_TYPE (local_enclosing) == void_type_node)
3711         {
3712           parse_error_context (cl, "Qualifier must be a reference");
3713           local_enclosing = NULL_TREE;
3714           break;
3715         }
3716       local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3717       if (!local_super || local_super == object_type_node)
3718         break;
3719
3720       if (TREE_CODE (local_super) == POINTER_TYPE)
3721         local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3722       else
3723         local_super = TYPE_NAME (local_super);
3724
3725       /* We may not have checked for circular inheritance yet, so do so
3726          here to prevent an infinite loop. */
3727       if (htab_find (circularity_hash, local_super) != NULL)
3728         {
3729           if (!cl)
3730             cl = lookup_cl (local_enclosing);
3731
3732           parse_error_context
3733             (cl, "Cyclic inheritance involving %s",
3734              IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3735           local_enclosing = NULL_TREE;
3736         }
3737       else
3738         local_enclosing = local_super;
3739     }
3740
3741   /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3742   *super = local_super;
3743   *enclosing = local_enclosing;
3744
3745   return NULL_TREE;
3746 }
3747
3748 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3749    qualified. */
3750
3751 static tree
3752 find_as_inner_class (tree enclosing, tree name, tree cl)
3753 {
3754   tree qual, to_return;
3755   if (!enclosing)
3756     return NULL_TREE;
3757
3758   name = TYPE_NAME (name);
3759
3760   /* First search: within the scope of `enclosing', search for name */
3761   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3762     qual = EXPR_WFL_QUALIFICATION (cl);
3763   else if (cl)
3764     qual = build_tree_list (cl, NULL_TREE);
3765   else
3766     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3767
3768   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3769     return to_return;
3770
3771   /* We're dealing with a qualified name. Try to resolve thing until
3772      we get something that is an enclosing class. */
3773   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3774     {
3775       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3776
3777       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3778            qual = TREE_CHAIN (qual))
3779         {
3780           acc = merge_qualified_name (acc,
3781                                       EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3782           BUILD_PTR_FROM_NAME (ptr, acc);
3783           decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3784         }
3785
3786       /* A NULL qual and a decl means that the search ended
3787          successfully?!? We have to do something then. FIXME */
3788
3789       if (decl)
3790         enclosing = decl;
3791       else
3792         qual = EXPR_WFL_QUALIFICATION (cl);
3793     }
3794   /* Otherwise, create a qual for the other part of the resolution. */
3795   else
3796     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3797
3798   return find_as_inner_class_do (qual, enclosing);
3799 }
3800
3801 /* We go inside the list of sub classes and try to find a way
3802    through. */
3803
3804 static tree
3805 find_as_inner_class_do (tree qual, tree enclosing)
3806 {
3807   if (!qual)
3808     return NULL_TREE;
3809
3810   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3811     {
3812       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3813       tree next_enclosing = NULL_TREE;
3814       tree inner_list;
3815
3816       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3817            inner_list; inner_list = TREE_CHAIN (inner_list))
3818         {
3819           if (TREE_VALUE (inner_list) == name_to_match)
3820             {
3821               next_enclosing = TREE_PURPOSE (inner_list);
3822               break;
3823             }
3824         }
3825       enclosing = next_enclosing;
3826     }
3827
3828   return (!qual && enclosing ? enclosing : NULL_TREE);
3829 }
3830
3831 static void
3832 link_nested_class_to_enclosing (void)
3833 {
3834   if (GET_ENCLOSING_CPC ())
3835     {
3836       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3837       DECL_INNER_CLASS_LIST (enclosing) =
3838         tree_cons (GET_CPC (), GET_CPC_UN (),
3839                    DECL_INNER_CLASS_LIST (enclosing));
3840     }
3841 }
3842
3843 static tree
3844 maybe_make_nested_class_name (tree name)
3845 {
3846   tree id = NULL_TREE;
3847
3848   if (CPC_INNER_P ())
3849     {
3850       /* If we're in a function, we must append a number to create the
3851          nested class name.  However, we don't do this if the class we
3852          are constructing is anonymous, because in that case we'll
3853          already have a number as the class name.  */
3854       if (! make_nested_class_name (GET_CPC_LIST ())
3855           && current_function_decl != NULL_TREE
3856           && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3857         {
3858           char buf[10];
3859           sprintf (buf, "%d", anonymous_class_counter);
3860           ++anonymous_class_counter;
3861           obstack_grow (&temporary_obstack, buf, strlen (buf));
3862           obstack_1grow (&temporary_obstack, '$');
3863         }
3864       obstack_grow0 (&temporary_obstack,
3865                      IDENTIFIER_POINTER (name),
3866                      IDENTIFIER_LENGTH (name));
3867       id = get_identifier (obstack_finish (&temporary_obstack));
3868       if (ctxp->package)
3869         QUALIFIED_P (id) = 1;
3870     }
3871   return id;
3872 }
3873
3874 /* If DECL is NULL, create and push a new DECL, record the current
3875    line CL and do other maintenance things.  */
3876
3877 static tree
3878 maybe_create_class_interface_decl (tree decl, tree raw_name,
3879                                    tree qualified_name, tree cl)
3880 {
3881   if (!decl)
3882     decl = push_class (make_class (), qualified_name);
3883
3884   /* Take care of the file and line business */
3885 #ifdef USE_MAPPED_LOCATION
3886   DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3887 #else
3888   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3889   /* If we're emitting xrefs, store the line/col number information */
3890   if (flag_emit_xref)
3891     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3892   else
3893     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3894 #endif
3895   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3896   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3897 #ifdef USE_MAPPED_LOCATION
3898   {
3899     tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3900     CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3901       tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3902   }
3903 #else
3904   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3905     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3906 #endif
3907
3908   PUSH_CPC (decl, raw_name);
3909   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3910
3911   /* Link the declaration to the already seen ones */
3912   TREE_CHAIN (decl) = ctxp->class_list;
3913   ctxp->class_list = decl;
3914
3915   /* Create a new nodes in the global lists */
3916   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3917   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3918
3919   /* Install a new dependency list element */
3920   create_jdep_list (ctxp);
3921
3922   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3923                           IDENTIFIER_POINTER (qualified_name)));
3924   return decl;
3925 }
3926
3927 static void
3928 add_superinterfaces (tree decl, tree interface_list)
3929 {
3930   tree node;
3931   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3932      takes care of ensuring that:
3933        - This is an accessible interface type,
3934        - Circularity detection.
3935    parser_add_interface is then called. If present but not defined,
3936    the check operation is delayed until the super interface gets
3937    defined.  */
3938   for (node = interface_list; node; node = TREE_CHAIN (node))
3939     {
3940       tree current = TREE_PURPOSE (node);
3941       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3942       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3943         {
3944           if (!parser_check_super_interface (idecl, decl, current))
3945             parser_add_interface (decl, idecl, current);
3946         }
3947       else
3948         register_incomplete_type (JDEP_INTERFACE,
3949                                   current, decl, NULL_TREE);
3950     }
3951 }
3952
3953 /* Create an interface in pass1 and return its decl. Return the
3954    interface's decl in pass 2.  */
3955
3956 static tree
3957 create_interface (int flags, tree id, tree super)
3958 {
3959   tree raw_name = EXPR_WFL_NODE (id);
3960   tree q_name = parser_qualified_classname (raw_name);
3961   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3962
3963   /* Certain syntax errors are making SUPER be like ID. Avoid this
3964      case. */
3965   if (ctxp->class_err && id == super)
3966     super = NULL;
3967
3968   EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3969
3970   /* Basic checks: scope, redefinition, modifiers */
3971   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3972     {
3973       PUSH_ERROR ();
3974       return NULL_TREE;
3975     }
3976
3977   /* Suspend the current parsing context if we're parsing an inner
3978      interface */
3979   if (CPC_INNER_P ())
3980     {
3981       java_parser_context_suspend ();
3982       /* Interface members are public. */
3983       if (CLASS_INTERFACE (GET_CPC ()))
3984         flags |= ACC_PUBLIC;
3985     }
3986
3987   /* Push a new context for (static) initialized upon declaration fields */
3988   java_parser_context_push_initialized_field ();
3989
3990   /* Interface modifiers check
3991        - public/abstract allowed (already done at that point)
3992        - abstract is obsolete (comes first, it's a warning, or should be)
3993        - Can't use twice the same (checked in the modifier rule) */
3994   if ((flags & ACC_ABSTRACT) && flag_redundant)
3995     parse_warning_context
3996       (MODIFIER_WFL (ABSTRACT_TK),
3997        "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3998
3999   /* Create a new decl if DECL is NULL, otherwise fix it */
4000   decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
4001
4002   /* Interfaces are always abstract. */
4003   flags |= ACC_ABSTRACT;
4004
4005   /* Inner interfaces are always static.  */
4006   if (INNER_CLASS_DECL_P (decl))
4007     flags |= ACC_STATIC;
4008
4009   /* Set super info and mark the class a complete */
4010   set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
4011                   object_type_node, ctxp->interface_number);
4012   ctxp->interface_number = 0;
4013   CLASS_COMPLETE_P (decl) = 1;
4014   add_superinterfaces (decl, super);
4015
4016   /* Eventually sets the @deprecated tag flag */
4017   CHECK_DEPRECATED (decl);
4018
4019   return decl;
4020 }
4021
4022 /* Patch anonymous class CLASS, by either extending or implementing
4023    DEP.  */
4024
4025 static void
4026 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4027 {
4028   tree class = TREE_TYPE (class_decl);
4029   tree type =  TREE_TYPE (type_decl);
4030   tree binfo = TYPE_BINFO (class);
4031
4032   /* If it's an interface, implement it */
4033   if (CLASS_INTERFACE (type_decl))
4034     {
4035       if (parser_check_super_interface (type_decl, class_decl, wfl))
4036         return;
4037
4038       if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4039         {
4040            /* Extend the binfo - by reallocating and copying it. */
4041           tree new_binfo;
4042           tree base_binfo;
4043           int i;
4044           
4045           new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4046           for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4047             BINFO_BASE_APPEND (new_binfo, base_binfo);
4048           CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4049           BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4050           TYPE_BINFO (class) = new_binfo;
4051         }
4052       
4053       /* And add the interface */
4054       parser_add_interface (class_decl, type_decl, wfl);
4055     }
4056   /* Otherwise, it's a type we want to extend */
4057   else
4058     {
4059       if (parser_check_super (type_decl, class_decl, wfl))
4060         return;
4061       BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4062     }
4063 }
4064
4065 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4066    its decl.  */
4067
4068 static tree
4069 create_anonymous_class (tree type_name)
4070 {
4071   char buffer [80];
4072   tree super = NULL_TREE, itf = NULL_TREE;
4073   tree id, type_decl, class;
4074
4075   /* The unqualified name of the anonymous class. It's just a number. */
4076   sprintf (buffer, "%d", anonymous_class_counter++);
4077   id = build_wfl_node (get_identifier (buffer));
4078   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4079
4080   /* We know about the type to extend/implement. We go ahead */
4081   if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4082     {
4083       /* Create a class which either implements on extends the designated
4084          class. The class bears an inaccessible name. */
4085       if (CLASS_INTERFACE (type_decl))
4086         {
4087           /* It's OK to modify it here. It's been already used and
4088              shouldn't be reused */
4089           ctxp->interface_number = 1;
4090           /* Interfaces should presented as a list of WFLs */
4091           itf = build_tree_list (type_name, NULL_TREE);
4092         }
4093       else
4094         super = type_name;
4095     }
4096
4097   class = create_class (ACC_FINAL, id, super, itf);
4098
4099   /* We didn't know anything about the stuff. We register a dependence. */
4100   if (!type_decl)
4101     register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4102
4103   ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4104   return class;
4105 }
4106
4107 /* Create a class in pass1 and return its decl. Return class
4108    interface's decl in pass 2.  */
4109
4110 static tree
4111 create_class (int flags, tree id, tree super, tree interfaces)
4112 {
4113   tree raw_name = EXPR_WFL_NODE (id);
4114   tree class_id, decl;
4115   tree super_decl_type;
4116
4117   /* Certain syntax errors are making SUPER be like ID. Avoid this
4118      case. */
4119   if (ctxp->class_err && id == super)
4120     super = NULL;
4121
4122   class_id = parser_qualified_classname (raw_name);
4123   decl = IDENTIFIER_CLASS_VALUE (class_id);
4124   EXPR_WFL_NODE (id) = class_id;
4125
4126   /* Basic check: scope, redefinition, modifiers */
4127   if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4128     {
4129       PUSH_ERROR ();
4130       return NULL_TREE;
4131     }
4132
4133   /* Suspend the current parsing context if we're parsing an inner
4134      class or an anonymous class. */
4135   if (CPC_INNER_P ())
4136     {
4137       java_parser_context_suspend ();
4138       /* Interface members are public. */
4139       if (CLASS_INTERFACE (GET_CPC ()))
4140         flags |= ACC_PUBLIC;
4141     }
4142
4143   /* Push a new context for (static) initialized upon declaration fields */
4144   java_parser_context_push_initialized_field ();
4145
4146   /* Class modifier check:
4147        - Allowed modifier (already done at that point)
4148        - abstract AND final forbidden
4149        - Public classes defined in the correct file */
4150   if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4151     parse_error_context
4152       (id, "Class %qs can't be declared both abstract and final",
4153        IDENTIFIER_POINTER (raw_name));
4154
4155   /* Create a new decl if DECL is NULL, otherwise fix it */
4156   decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4157
4158   /* If SUPER exists, use it, otherwise use Object */
4159   if (super)
4160     {
4161       /* java.lang.Object can't extend anything.  */
4162       if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4163         {
4164           parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4165           return NULL_TREE;
4166         }
4167
4168       super_decl_type =
4169         register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4170     }
4171   else if (TREE_TYPE (decl) != object_type_node)
4172     super_decl_type = object_type_node;
4173   /* We're defining java.lang.Object */
4174   else
4175     super_decl_type = NULL_TREE;
4176
4177   /* A class nested in an interface is implicitly static. */
4178   if (INNER_CLASS_DECL_P (decl)
4179       && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4180     {
4181       flags |= ACC_STATIC;
4182     }
4183
4184   /* Set super info and mark the class as complete. */
4185   set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4186                   ctxp->interface_number);
4187   ctxp->interface_number = 0;
4188   CLASS_COMPLETE_P (decl) = 1;
4189   add_superinterfaces (decl, interfaces);
4190
4191   /* TYPE_VFIELD' is a compiler-generated field used to point to
4192      virtual function tables.  In gcj, every class has a common base
4193      virtual function table in java.lang.object.  */
4194   TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4195
4196   /* Add the private this$<n> field, Replicate final locals still in
4197      scope as private final fields mangled like val$<local_name>.
4198      This does not occur for top level (static) inner classes. */
4199   if (PURE_INNER_CLASS_DECL_P (decl))
4200     add_inner_class_fields (decl, current_function_decl);
4201
4202   /* If doing xref, store the location at which the inherited class
4203      (if any) was seen. */
4204   if (flag_emit_xref && super)
4205     DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4206
4207   /* Eventually sets the @deprecated tag flag */
4208   CHECK_DEPRECATED (decl);
4209
4210   /* Reset the anonymous class counter when declaring non inner classes */
4211   if (!INNER_CLASS_DECL_P (decl))
4212     anonymous_class_counter = 1;
4213
4214   return decl;
4215 }
4216
4217 /* End a class declaration: register the statements used to create
4218    finit$ and <clinit>, pop the current class and resume the prior
4219    parser context if necessary.  */
4220
4221 static void
4222 end_class_declaration (int resume)
4223 {
4224   /* If an error occurred, context weren't pushed and won't need to be
4225      popped by a resume. */
4226   int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4227
4228   if (GET_CPC () != error_mark_node)
4229     dump_java_tree (TDI_class, GET_CPC ());
4230
4231   java_parser_context_pop_initialized_field ();
4232   POP_CPC ();
4233   if (resume && no_error_occurred)
4234     java_parser_context_resume ();
4235
4236   /* We're ending a class declaration, this is a good time to reset
4237      the interface cout. Note that might have been already done in
4238      create_interface, but if at that time an inner class was being
4239      dealt with, the interface count was reset in a context created
4240      for the sake of handling inner classes declaration. */
4241   ctxp->interface_number = 0;
4242 }
4243
4244 static void
4245 add_inner_class_fields (tree class_decl, tree fct_decl)
4246 {
4247   tree block, marker, f;
4248
4249   f = add_field (TREE_TYPE (class_decl),
4250                  build_current_thisn (TREE_TYPE (class_decl)),
4251                  build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4252                  ACC_PRIVATE);
4253   FIELD_THISN (f) = 1;
4254
4255   if (!fct_decl)
4256     return;
4257
4258   for (block = GET_CURRENT_BLOCK (fct_decl);
4259        block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4260     {
4261       tree decl;
4262       for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4263         {
4264           tree name, pname;
4265           tree wfl, init, list;
4266
4267           /* Avoid non final arguments. */
4268           if (!LOCAL_FINAL_P (decl))
4269             continue;
4270
4271           MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4272           MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4273           wfl = build_wfl_node (name);
4274           init = build_wfl_node (pname);
4275           /* Build an initialization for the field: it will be
4276              initialized by a parameter added to finit$, bearing a
4277              mangled name of the field itself (param$<n>.) The
4278              parameter is provided to finit$ by the constructor
4279              invoking it (hence the constructor will also feature a
4280              hidden parameter, set to the value of the outer context
4281              local at the time the inner class is created.)
4282
4283              Note: we take into account all possible locals that can
4284              be accessed by the inner class. It's actually not trivial
4285              to minimize these aliases down to the ones really
4286              used. One way to do that would be to expand all regular
4287              methods first, then finit$ to get a picture of what's
4288              used.  It works with the exception that we would have to
4289              go back on all constructor invoked in regular methods to
4290              have their invocation reworked (to include the right amount
4291              of alias initializer parameters.)
4292
4293              The only real way around, I think, is a first pass to
4294              identify locals really used in the inner class. We leave
4295              the flag FIELD_LOCAL_ALIAS_USED around for that future
4296              use.
4297
4298              On the other hand, it only affect local inner classes,
4299              whose constructors (and finit$ call) will be featuring
4300              unnecessary arguments. It's easy for a developer to keep
4301              this number of parameter down by using the `final'
4302              keyword only when necessary. For the time being, we can
4303              issue a warning on unnecessary finals. FIXME */
4304           init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4305                                    wfl, init);
4306
4307           /* Register the field. The TREE_LIST holding the part
4308              initialized/initializer will be marked ARG_FINAL_P so
4309              that the created field can be marked
4310              FIELD_LOCAL_ALIAS. */
4311           list = build_tree_list (wfl, init);
4312           ARG_FINAL_P (list) = 1;
4313           register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4314         }
4315     }
4316
4317   if (!CPC_INITIALIZER_STMT (ctxp))
4318     return;
4319
4320   /* If we ever registered an alias field, insert and marker to
4321      remember where the list ends. The second part of the list (the one
4322      featuring initialized fields) so it can be later reversed to
4323      enforce 8.5. The marker will be removed during that operation. */
4324   marker = build_tree_list (NULL_TREE, NULL_TREE);
4325   TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4326   SET_CPC_INITIALIZER_STMT (ctxp, marker);
4327 }
4328
4329 /* Can't use lookup_field () since we don't want to load the class and
4330    can't set the CLASS_LOADED_P flag */
4331
4332 static tree
4333 find_field (tree class, tree name)
4334 {
4335   tree decl;
4336   for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4337     {
4338       if (DECL_NAME (decl) == name)
4339         return decl;
4340     }
4341   return NULL_TREE;
4342 }
4343
4344 /* Wrap around lookup_field that doesn't potentially upset the value
4345    of CLASS */
4346
4347 static tree
4348 lookup_field_wrapper (tree class, tree name)
4349 {
4350   tree type = class;
4351   tree decl = NULL_TREE;
4352   java_parser_context_save_global ();
4353
4354   /* Last chance: if we're within the context of an inner class, we
4355      might be trying to access a local variable defined in an outer
4356      context. We try to look for it now. */
4357   if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4358     {
4359       tree new_name;
4360       MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4361       decl = lookup_field (&type, new_name);
4362       if (decl && decl != error_mark_node)
4363         FIELD_LOCAL_ALIAS_USED (decl) = 1;
4364     }
4365   if (!decl || decl == error_mark_node)
4366     {
4367       type = class;
4368       decl = lookup_field (&type, name);
4369     }
4370
4371   /* If the field still hasn't been found, try the next enclosing context. */
4372   if (!decl && INNER_CLASS_TYPE_P (class))
4373     {
4374       tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4375       decl = lookup_field_wrapper (outer_type, name);
4376     }
4377
4378   java_parser_context_restore_global ();
4379   return decl == error_mark_node ? NULL : decl;
4380 }
4381
4382 /* Find duplicate field within the same class declarations and report
4383    the error. Returns 1 if a duplicated field was found, 0
4384    otherwise.  */
4385
4386 static int
4387 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4388 {
4389   /* This might be modified to work with method decl as well */
4390   tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4391   if (decl)
4392     {
4393       char *t1 = xstrdup (purify_type_name
4394                          ((TREE_CODE (new_type) == POINTER_TYPE
4395                            && TREE_TYPE (new_type) == NULL_TREE) ?
4396                           IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4397                           lang_printable_name (new_type, 1)));
4398       /* The type may not have been completed by the time we report
4399          the error */
4400       char *t2 = xstrdup (purify_type_name
4401                          ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4402                            && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4403                           IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4404                           lang_printable_name (TREE_TYPE (decl), 1)));
4405       parse_error_context
4406         (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4407          t1, IDENTIFIER_POINTER (new_field_name),
4408          t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4409          DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4410       free (t1);
4411       free (t2);
4412       return 1;
4413     }
4414   return 0;
4415 }
4416
4417 /* Field registration routine. If TYPE doesn't exist, field
4418    declarations are linked to the undefined TYPE dependency list, to
4419    be later resolved in java_complete_class () */
4420
4421 static void
4422 register_fields (int flags, tree type, tree variable_list)
4423 {
4424   tree current, saved_type;
4425   tree class_type = NULL_TREE;
4426   location_t saved_location = input_location;
4427   int must_chain = 0;
4428   tree wfl = NULL_TREE;
4429
4430   if (GET_CPC ())
4431     class_type = TREE_TYPE (GET_CPC ());
4432
4433   if (!class_type || class_type == error_mark_node)
4434     return;
4435
4436   /* If we're adding fields to interfaces, those fields are public,
4437      static, final */
4438   if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4439     {
4440       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4441                                  flags, ACC_PUBLIC, "interface field(s)");
4442       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4443                                  flags, ACC_STATIC, "interface field(s)");
4444       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4445                                  flags, ACC_FINAL, "interface field(s)");
4446       check_modifiers ("Illegal interface member modifier %qs", flags,
4447                        INTERFACE_FIELD_MODIFIERS);
4448       flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4449     }
4450
4451   /* Obtain a suitable type for resolution, if necessary */
4452   SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4453
4454   /* If TYPE is fully resolved and we don't have a reference, make one */
4455   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4456
4457   for (current = variable_list, saved_type = type; current;
4458        current = TREE_CHAIN (current), type = saved_type)
4459     {
4460       tree real_type;
4461       tree field_decl;
4462       tree cl = TREE_PURPOSE (current);
4463       tree init = TREE_VALUE (current);
4464       tree current_name = EXPR_WFL_NODE (cl);
4465
4466       /* Can't declare non-final static fields in inner classes */
4467       if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4468           && !(flags & ACC_FINAL))
4469         parse_error_context
4470           (cl, "Field %qs can't be static in inner class %qs unless it is final",
4471            IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4472            lang_printable_name (class_type, 0));
4473
4474       /* Process NAME, as it may specify extra dimension(s) for it */
4475       type = build_array_from_name (type, wfl, current_name, &current_name);
4476
4477       /* Type adjustment. We may have just readjusted TYPE because
4478          the variable specified more dimensions. Make sure we have
4479          a reference if we can and don't have one already. Also
4480          change the name if we have an init. */
4481       if (type != saved_type)
4482         {
4483           PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4484           if (init)
4485             EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4486         }
4487
4488       real_type = GET_REAL_TYPE (type);
4489       /* Check for redeclarations */
4490       if (duplicate_declaration_error_p (current_name, real_type, cl))
4491         continue;
4492
4493       /* Set input_line to the line the field was found and create a
4494          declaration for it. Eventually sets the @deprecated tag flag. */
4495 #ifdef USE_MAPPED_LOCATION
4496       input_location = EXPR_LOCATION (cl);
4497 #else
4498       if (flag_emit_xref)
4499         input_line = EXPR_WFL_LINECOL (cl);
4500       else
4501         input_line = EXPR_WFL_LINENO (cl);
4502 #endif
4503       field_decl = add_field (class_type, current_name, real_type, flags);
4504       CHECK_DEPRECATED_NO_RESET (field_decl);
4505
4506       /* If the field denotes a final instance variable, then we
4507          allocate a LANG_DECL_SPECIFIC part to keep track of its
4508          initialization. We also mark whether the field was
4509          initialized upon its declaration. We don't do that if the
4510          created field is an alias to a final local. */
4511       if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4512         {
4513           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4514           DECL_FIELD_FINAL_WFL (field_decl) = cl;
4515         }
4516
4517       /* If the couple initializer/initialized is marked ARG_FINAL_P,
4518          we mark the created field FIELD_LOCAL_ALIAS, so that we can
4519          hide parameters to this inner class finit$ and
4520          constructors. It also means that the field isn't final per
4521          say. */
4522       if (ARG_FINAL_P (current))
4523         {
4524           FIELD_LOCAL_ALIAS (field_decl) = 1;
4525           FIELD_FINAL (field_decl) = 0;
4526         }
4527
4528       /* Check if we must chain. */
4529       if (must_chain)
4530         register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4531
4532       /* If we have an initialization value tied to the field */
4533       if (init)
4534         {
4535           /* The field is declared static */
4536           if (flags & ACC_STATIC)
4537             {
4538               /* We include the field and its initialization part into
4539                  a list used to generate <clinit>. After <clinit> is
4540                  walked, field initializations will be processed and
4541                  fields initialized with known constants will be taken
4542                  out of <clinit> and have their DECL_INITIAL set
4543                  appropriately. */
4544               TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4545               SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4546               if (TREE_OPERAND (init, 1)
4547                   && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4548                 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4549             }
4550           /* A non-static field declared with an immediate initialization is
4551              to be initialized in <init>, if any.  This field is remembered
4552              to be processed at the time of the generation of <init>. */
4553           else
4554             {
4555               TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4556               SET_CPC_INITIALIZER_STMT (ctxp, init);
4557             }
4558           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4559           DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4560         }
4561     }
4562
4563   CLEAR_DEPRECATED;
4564   input_location = saved_location;
4565 }
4566
4567 /* Generate finit$, using the list of initialized fields to populate
4568    its body. finit$'s parameter(s) list is adjusted to include the
4569    one(s) used to initialized the field(s) caching outer context
4570    local(s).  */
4571
4572 static tree
4573 generate_finit (tree class_type)
4574 {
4575   int count = 0;
4576   tree list = TYPE_FINIT_STMT_LIST (class_type);
4577   tree mdecl, current, parms;
4578
4579   parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4580                                                   class_type, NULL_TREE,
4581                                                   &count);
4582   CRAFTED_PARAM_LIST_FIXUP (parms);
4583   mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4584                                     finit_identifier_node, parms);
4585   fix_method_argument_names (parms, mdecl);
4586   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4587                        mdecl, NULL_TREE);
4588   DECL_FUNCTION_NAP (mdecl) = count;
4589   start_artificial_method_body (mdecl);
4590
4591   for (current = list; current; current = TREE_CHAIN (current))
4592     java_method_add_stmt (mdecl,
4593                           build_debugable_stmt (EXPR_WFL_LINECOL (current),
4594                                                 current));
4595   end_artificial_method_body (mdecl);
4596   return mdecl;
4597 }
4598
4599 /* Generate a function to run the instance initialization code. The
4600    private method is called `instinit$'. Unless we're dealing with an
4601    anonymous class, we determine whether all ctors of CLASS_TYPE
4602    declare a checked exception in their `throws' clause in order to
4603    see whether it's necessary to encapsulate the instance initializer
4604    statements in a try/catch/rethrow sequence.  */
4605
4606 static tree
4607 generate_instinit (tree class_type)
4608 {
4609   tree current;
4610   tree compound = NULL_TREE;
4611   tree parms = tree_cons (this_identifier_node,
4612                           build_pointer_type (class_type), end_params_node);
4613   tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4614                                          void_type_node,
4615                                          instinit_identifier_node, parms);
4616
4617   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4618                        mdecl, NULL_TREE);
4619
4620   /* Gather all the statements in a compound */
4621   for (current = TYPE_II_STMT_LIST (class_type);
4622        current; current = TREE_CHAIN (current))
4623     compound = add_stmt_to_compound (compound, NULL_TREE, current);
4624
4625   /* We need to encapsulate COMPOUND by a try/catch statement to
4626      rethrow exceptions that might occur in the instance initializer.
4627      We do that only if all ctors of CLASS_TYPE are set to catch a
4628      checked exception. This doesn't apply to anonymous classes (since
4629      they don't have declared ctors.) */
4630   if (!ANONYMOUS_CLASS_P (class_type) &&
4631       ctors_unchecked_throws_clause_p (class_type))
4632     {
4633       compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4634                                              build1 (THROW_EXPR, NULL_TREE,
4635                                                      build_wfl_node (wpv_id)));
4636       DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4637                                                       exception_type_node);
4638     }
4639
4640   start_artificial_method_body (mdecl);
4641   java_method_add_stmt (mdecl, compound);
4642   end_artificial_method_body (mdecl);
4643
4644   return mdecl;
4645 }
4646
4647 /* FIXME */
4648 static tree
4649 build_instinit_invocation (tree class_type)
4650 {
4651   tree to_return = NULL_TREE;
4652
4653   if (TYPE_II_STMT_LIST (class_type))
4654     {
4655       tree parm = build_tree_list (NULL_TREE,
4656                                    build_wfl_node (this_identifier_node));
4657       to_return =
4658         build_method_invocation (build_wfl_node (instinit_identifier_node),
4659                                  parm);
4660     }
4661   return to_return;
4662 }
4663
4664 /* Shared across method_declarator and method_header to remember the
4665    patch stage that was reached during the declaration of the method.
4666    A method DECL is built differently is there is no patch
4667    (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4668    pending on the currently defined method.  */
4669
4670 static int patch_stage;
4671
4672 /* Check the method declaration and add the method to its current
4673    class.  If the argument list is known to contain incomplete types,
4674    the method is partially added and the registration will be resume
4675    once the method arguments resolved. If TYPE is NULL, we're dealing
4676    with a constructor.  */
4677
4678 static tree
4679 method_header (int flags, tree type, tree mdecl, tree throws)
4680 {
4681   tree type_wfl = NULL_TREE;
4682   tree meth_name = NULL_TREE;
4683   tree current, orig_arg, this_class = NULL;
4684   tree id, meth;
4685   location_t saved_location;
4686   int constructor_ok = 0, must_chain;
4687   int count;
4688
4689   if (mdecl == error_mark_node)
4690     return error_mark_node;
4691   meth = TREE_VALUE (mdecl);
4692   id = TREE_PURPOSE (mdecl);
4693
4694   check_modifiers_consistency (flags);
4695
4696   if (GET_CPC ())
4697     this_class = TREE_TYPE (GET_CPC ());
4698
4699   if (!this_class || this_class == error_mark_node)
4700     return NULL_TREE;
4701
4702   /* There are some forbidden modifiers for an abstract method and its
4703      class must be abstract as well.  */
4704   if (type && (flags & ACC_ABSTRACT))
4705     {
4706       ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4707       ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4708       ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4709       ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4710       ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4711       ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4712       if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4713           && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4714         parse_error_context
4715           (id,
4716            "Class %qs must be declared abstract to define abstract method %qs",
4717            IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4718            IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4719     }
4720
4721   /* A native method can't be strictfp.  */
4722   if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4723     parse_error_context (id, "native method %qs can't be strictfp",
4724                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4725   /* No such thing as a transient or volatile method.  */
4726   if ((flags & ACC_TRANSIENT))
4727     parse_error_context (id, "method %qs can't be transient",
4728                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4729   if ((flags & ACC_VOLATILE))
4730     parse_error_context (id, "method %qs can't be volatile",
4731                          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4732
4733   /* Things to be checked when declaring a constructor */
4734   if (!type)
4735     {
4736       int ec = java_error_count;
4737       /* 8.6: Constructor declarations: we might be trying to define a
4738          method without specifying a return type. */
4739       if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4740         parse_error_context
4741           (id, "Invalid method declaration, return type required");
4742       /* 8.6.3: Constructor modifiers */
4743       else
4744         {
4745           JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4746           JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4747           JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4748           JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4749           JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4750           JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4751         }
4752       /* If we found error here, we don't consider it's OK to tread
4753          the method definition as a constructor, for the rest of this
4754          function */
4755       if (ec == java_error_count)
4756         constructor_ok = 1;
4757     }
4758
4759   /* Method declared within the scope of an interface are implicitly
4760      abstract and public. Conflicts with other erroneously provided
4761      modifiers are checked right after. */
4762
4763   if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4764     {
4765       /* If FLAGS isn't set because of a modifier, turn the
4766          corresponding modifier WFL to NULL so we issue a warning on
4767          the obsolete use of the modifier */
4768       if (!(flags & ACC_PUBLIC))
4769         MODIFIER_WFL (PUBLIC_TK) = NULL;
4770       if (!(flags & ACC_ABSTRACT))
4771         MODIFIER_WFL (ABSTRACT_TK) = NULL;
4772       flags |= ACC_PUBLIC;
4773       flags |= ACC_ABSTRACT;
4774     }
4775
4776   /* Inner class can't declare static methods */
4777   if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4778     {
4779       parse_error_context
4780         (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4781          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4782          lang_printable_name (this_class, 0));
4783     }
4784
4785   /* Modifiers context reset moved up, so abstract method declaration
4786      modifiers can be later checked.  */
4787
4788   /* Set constructor returned type to void and method name to <init>,
4789      unless we found an error identifier the constructor (in which
4790      case we retain the original name) */
4791   if (!type)
4792     {
4793       type = void_type_node;
4794       if (constructor_ok)
4795         meth_name = init_identifier_node;
4796     }
4797   else
4798     meth_name = EXPR_WFL_NODE (id);
4799
4800   /* Do the returned type resolution and registration if necessary */
4801   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4802
4803   if (meth_name)
4804     type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4805   EXPR_WFL_NODE (id) = meth_name;
4806   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4807
4808   if (must_chain)
4809     {
4810       patch_stage = JDEP_METHOD_RETURN;
4811       register_incomplete_type (patch_stage, type_wfl, id, type);
4812       TREE_TYPE (meth) = GET_REAL_TYPE (type);
4813     }
4814   else
4815     TREE_TYPE (meth) = type;
4816
4817   saved_location = input_location;
4818   /* When defining an abstract or interface method, the curly
4819      bracket at level 1 doesn't exist because there is no function
4820      body */
4821 #ifdef USE_MAPPED_LOCATION
4822   input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4823                     EXPR_LOCATION (id));
4824 #else
4825   input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4826                 EXPR_WFL_LINENO (id));
4827 #endif
4828
4829   /* Remember the original argument list */
4830   orig_arg = TYPE_ARG_TYPES (meth);
4831
4832   if (patch_stage)              /* includes ret type and/or all args */
4833     {
4834       jdep *jdep;
4835       meth = add_method_1 (this_class, flags, meth_name, meth);
4836       /* Patch for the return type */
4837       if (patch_stage == JDEP_METHOD_RETURN)
4838         {
4839           jdep = CLASSD_LAST (ctxp->classd_list);
4840           JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4841         }
4842       /* This is the stop JDEP. METH allows the function's signature
4843          to be computed. */
4844       register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4845     }
4846   else
4847     meth = add_method (this_class, flags, meth_name,
4848                        build_java_signature (meth));
4849
4850   /* Remember final parameters */
4851   MARK_FINAL_PARMS (meth, orig_arg);
4852
4853   /* Fix the method argument list so we have the argument name
4854      information */
4855   fix_method_argument_names (orig_arg, meth);
4856
4857   /* Register the parameter number and re-install the current line
4858      number */
4859   DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4860   input_location = saved_location;
4861
4862   /* Register exception specified by the `throws' keyword for
4863      resolution and set the method decl appropriate field to the list.
4864      Note: the grammar ensures that what we get here are class
4865      types. */
4866   if (throws)
4867     {
4868       throws = nreverse (throws);
4869       for (current = throws; current; current = TREE_CHAIN (current))
4870         {
4871           register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4872                                     NULL_TREE, NULL_TREE);
4873           JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4874             &TREE_VALUE (current);
4875         }
4876       DECL_FUNCTION_THROWS (meth) = throws;
4877     }
4878
4879   if (TREE_TYPE (GET_CPC ()) != object_type_node)
4880     DECL_FUNCTION_WFL (meth) = id;
4881
4882   /* Set the flag if we correctly processed a constructor */
4883   if (constructor_ok)
4884     {
4885       DECL_CONSTRUCTOR_P (meth) = 1;
4886       /* Compute and store the number of artificial parameters declared
4887          for this constructor */
4888       for (count = 0, current = TYPE_FIELDS (this_class); current;
4889            current = TREE_CHAIN (current))
4890         if (FIELD_LOCAL_ALIAS (current))
4891           count++;
4892       DECL_FUNCTION_NAP (meth) = count;
4893     }
4894
4895   /* Eventually set the @deprecated tag flag */
4896   CHECK_DEPRECATED (meth);
4897
4898   /* If doing xref, store column and line number information instead
4899      of the line number only. */
4900   if (flag_emit_xref)
4901     {
4902 #ifdef USE_MAPPED_LOCATION
4903       DECL_SOURCE_LOCATION (meth) = EXPR_LOCATION (id);
4904 #else
4905       DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4906 #endif
4907     }
4908
4909   return meth;
4910 }
4911
4912 static void
4913 fix_method_argument_names (tree orig_arg, tree meth)
4914 {
4915   tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4916   if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4917     {
4918       TREE_PURPOSE (arg) = this_identifier_node;
4919       arg = TREE_CHAIN (arg);
4920     }
4921   while (orig_arg != end_params_node)
4922     {
4923       TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4924       orig_arg = TREE_CHAIN (orig_arg);
4925       arg = TREE_CHAIN (arg);
4926     }
4927 }
4928
4929 /* Complete the method declaration with METHOD_BODY.  */
4930
4931 static void
4932 finish_method_declaration (tree method_body)
4933 {
4934   int flags;
4935
4936   if (!current_function_decl)
4937     return;
4938
4939   flags = get_access_flags_from_decl (current_function_decl);
4940
4941   /* 8.4.5 Method Body */
4942   if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4943     {
4944       tree name = DECL_NAME (current_function_decl);
4945       parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4946                            "%s method %qs can't have a body defined",
4947                            (METHOD_NATIVE (current_function_decl) ?
4948                             "Native" : "Abstract"),
4949                            IDENTIFIER_POINTER (name));
4950       method_body = NULL_TREE;
4951     }
4952   else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4953     {
4954       tree name = DECL_NAME (current_function_decl);
4955       parse_error_context
4956         (DECL_FUNCTION_WFL (current_function_decl),
4957          "Non native and non abstract method %qs must have a body defined",
4958          IDENTIFIER_POINTER (name));
4959       method_body = NULL_TREE;
4960     }
4961
4962   if (flag_emit_class_files && method_body
4963       && TREE_CODE (method_body) == NOP_EXPR
4964       && TREE_TYPE (current_function_decl)
4965       && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4966     method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4967
4968   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4969   maybe_absorb_scoping_blocks ();
4970   /* Exit function's body */
4971   exit_block ();
4972   /* Merge last line of the function with first line, directly in the
4973      function decl. It will be used to emit correct debug info. */
4974   if (!flag_emit_xref)
4975     DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4976
4977   /* Since function's argument's list are shared, reset the
4978      ARG_FINAL_P parameter that might have been set on some of this
4979      function parameters. */
4980   UNMARK_FINAL_PARMS (current_function_decl);
4981
4982   /* So we don't have an irrelevant function declaration context for
4983      the next static block we'll see. */
4984   current_function_decl = NULL_TREE;
4985 }
4986
4987 /* Build a an error message for constructor circularity errors.  */
4988
4989 static char *
4990 constructor_circularity_msg (tree from, tree to)
4991 {
4992   static char string [4096];
4993   char *t = xstrdup (lang_printable_name (from, 2));
4994   sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4995   free (t);
4996   return string;
4997 }
4998
4999 /* Verify a circular call to METH. Return 1 if an error is found, 0
5000    otherwise.  */
5001
5002 static GTY(()) tree vcc_list;
5003 static int
5004 verify_constructor_circularity (tree meth, tree current)
5005 {
5006   tree c;
5007
5008   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5009     {
5010       if (TREE_VALUE (c) == meth)
5011         {
5012           char *t;
5013           if (vcc_list)
5014             {
5015               tree liste;
5016               vcc_list = nreverse (vcc_list);
5017               for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
5018                 {
5019                   parse_error_context
5020                     (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
5021                      constructor_circularity_msg
5022                       (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
5023                   java_error_count--;
5024                 }
5025             }
5026           t = xstrdup (lang_printable_name (meth, 2));
5027           parse_error_context (TREE_PURPOSE (c),
5028                                "%s: recursive invocation of constructor %qs",
5029                                constructor_circularity_msg (current, meth), t);
5030           free (t);
5031           vcc_list = NULL_TREE;
5032           return 1;
5033         }
5034     }
5035   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5036     {
5037       vcc_list = tree_cons (c, current, vcc_list);
5038       if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5039         return 1;
5040       vcc_list = TREE_CHAIN (vcc_list);
5041     }
5042   return 0;
5043 }
5044
5045 /* Check modifiers that can be declared but exclusively */
5046
5047 static void
5048 check_modifiers_consistency (int flags)
5049 {
5050   int acc_count = 0;
5051   tree cl = NULL_TREE;
5052
5053   THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5054   THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5055   THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5056   if (acc_count > 1)
5057     parse_error_context
5058       (cl, "Inconsistent member declaration.  At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5059
5060   acc_count = 0;
5061   cl = NULL_TREE;
5062   THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5063   THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5064   if (acc_count > 1)
5065     parse_error_context (cl,
5066                          "Inconsistent member declaration.  At most one of %<final%> or %<volatile%> may be specified");
5067 }
5068
5069 /* Check the methode header METH for abstract specifics features */
5070
5071 static void
5072 check_abstract_method_header (tree meth)
5073 {
5074   int flags = get_access_flags_from_decl (meth);
5075
5076   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5077                               ACC_ABSTRACT, "abstract method",
5078                               IDENTIFIER_POINTER (DECL_NAME (meth)));
5079   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5080                               ACC_PUBLIC, "abstract method",
5081                               IDENTIFIER_POINTER (DECL_NAME (meth)));
5082
5083   check_modifiers ("Illegal modifier %qs for interface method",
5084                   flags, INTERFACE_METHOD_MODIFIERS);
5085 }
5086
5087 /* Create a FUNCTION_TYPE node and start augmenting it with the
5088    declared function arguments. Arguments type that can't be resolved
5089    are left as they are, but the returned node is marked as containing
5090    incomplete types.  */
5091
5092 static tree
5093 method_declarator (tree id, tree list)
5094 {
5095   tree arg_types = NULL_TREE, current, node;
5096   tree meth = make_node (FUNCTION_TYPE);
5097   jdep *jdep;
5098
5099   patch_stage = JDEP_NO_PATCH;
5100
5101   if (GET_CPC () == error_mark_node)
5102     return error_mark_node;
5103
5104   /* If we're dealing with an inner class constructor, we hide the
5105      this$<n> decl in the name field of its parameter declaration.  We
5106      also might have to hide the outer context local alias
5107      initializers. Not done when the class is a toplevel class. */
5108   if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5109       && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5110     {
5111       tree aliases_list, type, thisn;
5112       /* First the aliases, linked to the regular parameters */
5113       aliases_list =
5114         build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5115                                                 TREE_TYPE (GET_CPC ()),
5116                                                 NULL_TREE, NULL);
5117       list = chainon (nreverse (aliases_list), list);
5118
5119       /* Then this$<n> */
5120       type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5121       thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5122       list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5123                         list);
5124     }
5125
5126   for (current = list; current; current = TREE_CHAIN (current))
5127     {
5128       int must_chain = 0;
5129       tree wfl_name = TREE_PURPOSE (current);
5130       tree type = TREE_VALUE (current);
5131       tree name = EXPR_WFL_NODE (wfl_name);
5132       tree already, arg_node;
5133       tree type_wfl = NULL_TREE;
5134       tree real_type;
5135
5136       /* Obtain a suitable type for resolution, if necessary */
5137       SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5138
5139       /* Process NAME, as it may specify extra dimension(s) for it */
5140       type = build_array_from_name (type, type_wfl, name, &name);
5141       EXPR_WFL_NODE (wfl_name) = name;
5142
5143       real_type = GET_REAL_TYPE (type);
5144       if (TREE_CODE (real_type) == RECORD_TYPE)
5145         {
5146           real_type = promote_type (real_type);
5147           if (TREE_CODE (type) == TREE_LIST)
5148             TREE_PURPOSE (type) = real_type;
5149         }
5150
5151       /* Check redefinition */
5152       for (already = arg_types; already; already = TREE_CHAIN (already))
5153         if (TREE_PURPOSE (already) == name)
5154           {
5155             parse_error_context
5156               (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5157                IDENTIFIER_POINTER (name),
5158                IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5159             break;
5160           }
5161
5162       /* If we've an incomplete argument type, we know there is a location
5163          to patch when the type get resolved, later.  */
5164       jdep = NULL;
5165       if (must_chain)
5166         {
5167           patch_stage = JDEP_METHOD;
5168           type = register_incomplete_type (patch_stage,
5169                                            type_wfl, wfl_name, type);
5170           jdep = CLASSD_LAST (ctxp->classd_list);
5171           JDEP_MISC (jdep) = id;
5172         }
5173
5174       /* The argument node: a name and a (possibly) incomplete type.  */
5175       arg_node = build_tree_list (name, real_type);
5176       /* Remember arguments declared final. */
5177       ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5178
5179       if (jdep)
5180         JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5181       TREE_CHAIN (arg_node) = arg_types;
5182       arg_types = arg_node;
5183     }
5184   TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5185   node = build_tree_list (id, meth);
5186   return node;
5187 }
5188
5189 static int
5190 unresolved_type_p (tree wfl, tree *returned)
5191 {
5192   if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5193     {
5194       if (returned)
5195         {
5196           tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5197           if (decl && current_class && (decl == TYPE_NAME (current_class)))
5198             *returned = TREE_TYPE (decl);
5199           else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5200             *returned = TREE_TYPE (GET_CPC ());
5201           else
5202             *returned = NULL_TREE;
5203         }
5204       return 1;
5205     }
5206   if (returned)
5207     *returned = wfl;
5208   return 0;
5209 }
5210
5211 /* From NAME, build a qualified identifier node using the
5212    qualification from the current package definition. */
5213
5214 static tree
5215 parser_qualified_classname (tree name)
5216 {
5217   tree nested_class_name;
5218
5219   if ((nested_class_name = maybe_make_nested_class_name (name)))
5220     return nested_class_name;
5221
5222   if (ctxp->package)
5223     return merge_qualified_name (ctxp->package, name);
5224   else
5225     return name;
5226 }
5227
5228 /* Called once the type a interface extends is resolved. Returns 0 if
5229    everything is OK.  */
5230
5231 static int
5232 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5233 {
5234   tree super_type = TREE_TYPE (super_decl);
5235
5236   /* Has to be an interface */
5237   if (!CLASS_INTERFACE (super_decl))
5238     {
5239       parse_error_context
5240         (this_wfl, "%s %qs can't implement/extend %s %qs",
5241          (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5242           "Interface" : "Class"),
5243          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5244          (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5245          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5246       return 1;
5247     }
5248
5249   /* Check top-level interface access. Inner classes are subject to member
5250      access rules (6.6.1). */
5251   if (! INNER_CLASS_P (super_type)
5252       && check_pkg_class_access (DECL_NAME (super_decl),
5253                                  NULL_TREE, true, this_decl))
5254     return 1;
5255
5256   SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5257                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5258                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5259   return 0;
5260 }
5261
5262 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5263    0 if everything is OK.  */
5264
5265 static int
5266 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5267 {
5268   tree super_type = TREE_TYPE (super_decl);
5269
5270   /* SUPER should be a CLASS (neither an array nor an interface) */
5271   if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5272     {
5273       parse_error_context
5274         (wfl, "Class %qs can't subclass %s %qs",
5275          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5276          (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5277          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5278       return 1;
5279     }
5280
5281   if (CLASS_FINAL (TYPE_NAME (super_type)))
5282     {
5283       parse_error_context (wfl, "Can't subclass final classes: %s",
5284                            IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5285       return 1;
5286     }
5287
5288   /* Check top-level class scope. Inner classes are subject to member access
5289      rules (6.6.1). */
5290   if (! INNER_CLASS_P (super_type)
5291       && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5292     return 1;
5293
5294   SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5295                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5296                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5297   return 0;
5298 }
5299
5300 /* Create a new dependency list and link it (in a LIFO manner) to the
5301    CTXP list of type dependency list.  */
5302
5303 static void
5304 create_jdep_list (struct parser_ctxt *ctxp)
5305 {
5306   jdeplist *new = xmalloc (sizeof (jdeplist));
5307   new->first = new->last = NULL;
5308   new->next = ctxp->classd_list;
5309   ctxp->classd_list = new;
5310 }
5311
5312 static jdeplist *
5313 reverse_jdep_list (struct parser_ctxt *ctxp)
5314 {
5315   jdeplist *prev = NULL, *current, *next;
5316   for (current = ctxp->classd_list; current; current = next)
5317     {
5318       next = current->next;
5319       current->next = prev;
5320       prev = current;
5321     }
5322   return prev;
5323 }
5324
5325 /* Create a fake pointer based on the ID stored in
5326    TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5327    registered again. */
5328
5329 static tree
5330 obtain_incomplete_type (tree type_name)
5331 {
5332   tree ptr = NULL_TREE, name;
5333
5334   if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5335     name = EXPR_WFL_NODE (type_name);
5336   else if (INCOMPLETE_TYPE_P (type_name))
5337     name = TYPE_NAME (type_name);
5338   else
5339     abort ();
5340
5341   /* Workaround from build_pointer_type for incomplete types.  */
5342   BUILD_PTR_FROM_NAME (ptr, name);
5343   TYPE_MODE (ptr) = ptr_mode;
5344   layout_type (ptr);
5345
5346   return ptr;
5347 }
5348
5349 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5350    non NULL instead of computing a new fake type based on WFL. The new
5351    dependency is inserted in the current type dependency list, in FIFO
5352    manner.  */
5353
5354 static tree
5355 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5356 {
5357   jdep *new = xmalloc (sizeof (jdep));
5358
5359   if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5360     ptr = obtain_incomplete_type (wfl);
5361
5362   JDEP_KIND (new) = kind;
5363   JDEP_DECL (new) = decl;
5364   JDEP_TO_RESOLVE (new) = ptr;
5365   JDEP_WFL (new) = wfl;
5366   JDEP_CHAIN (new) = NULL;
5367   JDEP_MISC (new) = NULL_TREE;
5368   /* For some dependencies, set the enclosing class of the current
5369      class to be the enclosing context */
5370   if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5371       && GET_ENCLOSING_CPC ())
5372     JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5373   else
5374     JDEP_ENCLOSING (new) = GET_CPC ();
5375   JDEP_GET_PATCH (new) = (tree *)NULL;
5376
5377   JDEP_INSERT (ctxp->classd_list, new);
5378
5379   return ptr;
5380 }
5381
5382 /* This checks for circular references with innerclasses. We start
5383    from SOURCE and should never reach TARGET. Extended/implemented
5384    types in SOURCE have their enclosing context checked not to reach
5385    TARGET. When the last enclosing context of SOURCE is reached, its
5386    extended/implemented types are also checked not to reach TARGET.
5387    In case of error, WFL of the offending type is returned; NULL_TREE
5388    otherwise.  */
5389
5390 static tree
5391 check_inner_circular_reference (tree source, tree target)
5392 {
5393   tree base_binfo;
5394   tree ctx, cl;
5395   int i;
5396
5397   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5398     {
5399       tree su;
5400
5401       /* We can end up with a NULL_TREE or an incomplete type here if
5402          we encountered previous type resolution errors. It's safe to
5403          simply ignore these cases.  */
5404       su = BINFO_TYPE (base_binfo);
5405       if (INCOMPLETE_TYPE_P (su))
5406         continue;
5407
5408       if (inherits_from_p (su, target))
5409         return lookup_cl (TYPE_NAME (su));
5410
5411       for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5412         {
5413           /* An enclosing context shouldn't be TARGET */
5414           if (ctx == TYPE_NAME (target))
5415             return lookup_cl (TYPE_NAME (su));
5416
5417           /* When we reach the enclosing last context, start a check
5418              on it, with the same target */
5419           if (! DECL_CONTEXT (ctx) &&
5420               (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5421             return cl;
5422         }
5423     }
5424   return NULL_TREE;
5425 }
5426
5427 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5428    offending type if a circularity is detected. NULL_TREE is returned
5429    otherwise. TYPE can be an interface or a class.   */
5430
5431 static tree
5432 check_circular_reference (tree type)
5433 {
5434   tree base_binfo;
5435   int i;
5436
5437   if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5438     return NULL_TREE;
5439
5440   if (! CLASS_INTERFACE (TYPE_NAME (type)))
5441     {
5442       if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5443         return lookup_cl (TYPE_NAME (type));
5444       return NULL_TREE;
5445     }
5446
5447   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5448     {
5449       if (BINFO_TYPE (base_binfo) != object_type_node
5450           && interface_of_p (type, BINFO_TYPE (base_binfo)))
5451         return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5452     }
5453   return NULL_TREE;
5454 }
5455
5456 void
5457 java_check_circular_reference (void)
5458 {
5459   tree current;
5460   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5461     {
5462       tree type = TREE_TYPE (current);
5463       tree cl;
5464
5465       cl = check_circular_reference (type);
5466       if (! cl)
5467         cl = check_inner_circular_reference (type, type);
5468       if (cl)
5469         parse_error_context (cl, "Cyclic class inheritance%s",
5470                              (cyclic_inheritance_report ?
5471                               cyclic_inheritance_report : ""));
5472     }
5473 }
5474
5475 /* Augment the parameter list PARM with parameters crafted to
5476    initialize outer context locals aliases. Through ARTIFICIAL, a
5477    count is kept of the number of crafted parameters. MODE governs
5478    what eventually gets created: something suitable for a function
5479    creation or a function invocation, either the constructor or
5480    finit$.  */
5481
5482 static tree
5483 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5484                                         int *artificial)
5485 {
5486   tree field;
5487   tree additional_parms = NULL_TREE;
5488
5489   for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5490     if (FIELD_LOCAL_ALIAS (field))
5491       {
5492         const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5493         tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5494         tree mangled_id;
5495
5496         switch (mode)
5497           {
5498           case AIPL_FUNCTION_DECLARATION:
5499             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5500                                                          &buffer [4]);
5501             purpose = build_wfl_node (mangled_id);
5502             if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5503               value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5504             else
5505               value = TREE_TYPE (field);
5506             break;
5507
5508           case AIPL_FUNCTION_CREATION:
5509             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5510                                                          &buffer [4]);
5511             value = TREE_TYPE (field);
5512             break;
5513
5514           case AIPL_FUNCTION_FINIT_INVOCATION:
5515             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5516                                                          &buffer [4]);
5517             /* Now, this is wrong. purpose should always be the NAME
5518                of something and value its matching value (decl, type,
5519                etc...) FIXME -- but there is a lot to fix. */
5520
5521             /* When invoked for this kind of operation, we already
5522                know whether a field is used or not. */
5523             purpose = TREE_TYPE (field);
5524             value = build_wfl_node (mangled_id);
5525             break;
5526
5527           case AIPL_FUNCTION_CTOR_INVOCATION:
5528             /* There are two case: the constructor invocation happens
5529                outside the local inner, in which case, locales from the outer
5530                context are directly used.
5531
5532                Otherwise, we fold to using the alias directly. */
5533             if (class_type == current_class)
5534               value = field;
5535             else
5536               {
5537                 name = get_identifier (&buffer[4]);
5538                 value = IDENTIFIER_LOCAL_VALUE (name);
5539               }
5540             break;
5541           }
5542         additional_parms = tree_cons (purpose, value, additional_parms);
5543         if (artificial)
5544           *artificial +=1;
5545       }
5546   if (additional_parms)
5547     {
5548       if (ANONYMOUS_CLASS_P (class_type)
5549           && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5550         additional_parms = nreverse (additional_parms);
5551       parm = chainon (additional_parms, parm);
5552     }
5553
5554    return parm;
5555 }
5556
5557 /* Craft a constructor for CLASS_DECL -- what we should do when none
5558    where found. ARGS is non NULL when a special signature must be
5559    enforced. This is the case for anonymous classes.  */
5560
5561 static tree
5562 craft_constructor (tree class_decl, tree args)
5563 {
5564   tree class_type = TREE_TYPE (class_decl);
5565   tree parm = NULL_TREE;
5566   /* Inherit access flags for the constructor from its enclosing class. */
5567   int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5568   int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5569   int i = 0, artificial = 0;
5570   tree decl, ctor_name;
5571   char buffer [80];
5572
5573   ctor_name = init_identifier_node;
5574
5575   /* If we're dealing with an inner class constructor, we hide the
5576      this$<n> decl in the name field of its parameter declaration. */
5577   if (PURE_INNER_CLASS_TYPE_P (class_type))
5578     {
5579       tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5580       parm = tree_cons (build_current_thisn (class_type),
5581                         build_pointer_type (type), parm);
5582
5583       /* Some more arguments to be hidden here. The values of the local
5584          variables of the outer context that the inner class needs to see. */
5585       parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5586                                                      class_type, parm,
5587                                                      &artificial);
5588     }
5589
5590   /* Then if there are any args to be enforced, enforce them now */
5591   for (; args && args != end_params_node; args = TREE_CHAIN (args))
5592     {
5593       /* If we see a `void *', we need to change it to Object.  */
5594       if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5595         TREE_VALUE (args) = object_ptr_type_node;
5596
5597       sprintf (buffer, "parm%d", i++);
5598       parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5599     }
5600
5601   CRAFTED_PARAM_LIST_FIXUP (parm);
5602   decl = create_artificial_method (class_type, flags, void_type_node,
5603                                    ctor_name, parm);
5604   fix_method_argument_names (parm, decl);
5605   /* Now, mark the artificial parameters. */
5606   DECL_FUNCTION_NAP (decl) = artificial;
5607   DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5608   DECL_INLINE (decl) = 1;
5609   return decl;
5610 }
5611
5612
5613 /* Fix the constructors. This will be called right after circular
5614    references have been checked. It is necessary to fix constructors
5615    early even if no code generation will take place for that class:
5616    some generated constructor might be required by the class whose
5617    compilation triggered this one to be simply loaded.  */
5618
5619 void
5620 java_fix_constructors (void)
5621 {
5622   tree current;
5623
5624   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5625     {
5626       tree class_type = TREE_TYPE (current);
5627       int saw_ctor = 0;
5628       tree decl;
5629
5630       if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5631         continue;
5632
5633       output_class = current_class = class_type;
5634       for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5635         {
5636           if (DECL_CONSTRUCTOR_P (decl))
5637             {
5638               fix_constructors (decl);
5639               saw_ctor = 1;
5640             }
5641         }
5642
5643       /* Anonymous class constructor can't be generated that early. */
5644       if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5645         craft_constructor (current, NULL_TREE);
5646     }
5647 }
5648
5649 /* safe_layout_class just makes sure that we can load a class without
5650    disrupting the current_class, input_file, input_line, etc, information
5651    about the class processed currently.  */
5652
5653 void
5654 safe_layout_class (tree class)
5655 {
5656   tree save_current_class = current_class;
5657   location_t save_location = input_location;
5658
5659   layout_class (class);
5660
5661   current_class = save_current_class;
5662   input_location = save_location;
5663 }
5664
5665 static tree
5666 jdep_resolve_class (jdep *dep)
5667 {
5668   tree decl;
5669
5670   if (JDEP_RESOLVED_P (dep))
5671     decl = JDEP_RESOLVED_DECL (dep);
5672   else
5673     {
5674       decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5675                             JDEP_DECL (dep), JDEP_WFL (dep));
5676       JDEP_RESOLVED (dep, decl);
5677       /* If there is no WFL, that's ok.  We generate this warning
5678          elsewhere.  */
5679       if (decl && JDEP_WFL (dep) != NULL_TREE)
5680         check_deprecation (JDEP_WFL (dep), decl);
5681     }
5682
5683   if (!decl)
5684     complete_class_report_errors (dep);
5685   else if (INNER_CLASS_DECL_P (decl))
5686     {
5687       tree inner = TREE_TYPE (decl);
5688       if (! CLASS_LOADED_P (inner))
5689         {
5690           safe_layout_class (inner);
5691           if (TYPE_SIZE (inner) == error_mark_node)
5692             TYPE_SIZE (inner) = NULL_TREE;
5693         }
5694       check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5695     }
5696   return decl;
5697 }
5698
5699 /* Complete unsatisfied class declaration and their dependencies */
5700
5701 void
5702 java_complete_class (void)
5703 {
5704   tree cclass;
5705   jdeplist *cclassd;
5706   int error_found;
5707   tree type;
5708
5709   /* Process imports */
5710   process_imports ();
5711
5712   /* Reverse things so we have the right order */
5713   ctxp->class_list = nreverse (ctxp->class_list);
5714   ctxp->classd_list = reverse_jdep_list (ctxp);
5715
5716   for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5717        cclass && cclassd;
5718        cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5719     {
5720       jdep *dep;
5721
5722       /* We keep the compilation unit imports in the class so that
5723          they can be used later to resolve type dependencies that
5724          aren't necessary to solve now. */
5725       TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5726       TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5727
5728       for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5729         {
5730           tree decl;
5731           if (!(decl = jdep_resolve_class (dep)))
5732             continue;
5733
5734           /* Now it's time to patch */
5735           switch (JDEP_KIND (dep))
5736             {
5737             case JDEP_SUPER:
5738               /* Simply patch super */
5739               if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5740                 continue;
5741               BINFO_TYPE (BINFO_BASE_BINFO
5742                           (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5743                 = TREE_TYPE (decl);
5744               break;
5745
5746             case JDEP_FIELD:
5747               {
5748                 /* We do part of the job done in add_field */
5749                 tree field_decl = JDEP_DECL (dep);
5750                 tree field_type = TREE_TYPE (decl);
5751                 if (TREE_CODE (field_type) == RECORD_TYPE)
5752                   field_type = promote_type (field_type);
5753                 TREE_TYPE (field_decl) = field_type;
5754                 DECL_ALIGN (field_decl) = 0;
5755                 DECL_USER_ALIGN (field_decl) = 0;
5756                 layout_decl (field_decl, 0);
5757                 SOURCE_FRONTEND_DEBUG
5758                   (("Completed field/var decl '%s' with '%s'",
5759                     IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5760                     IDENTIFIER_POINTER (DECL_NAME (decl))));
5761                 break;
5762               }
5763             case JDEP_METHOD:   /* We start patching a method */
5764             case JDEP_METHOD_RETURN:
5765               error_found = 0;
5766               while (1)
5767                 {
5768                   if (decl)
5769                     {
5770                       type = TREE_TYPE(decl);
5771                       if (TREE_CODE (type) == RECORD_TYPE)
5772                         type = promote_type (type);
5773                       JDEP_APPLY_PATCH (dep, type);
5774                       SOURCE_FRONTEND_DEBUG
5775                         (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5776                            "Completing fct '%s' with ret type '%s'":
5777                            "Completing arg '%s' with type '%s'"),
5778                           IDENTIFIER_POINTER (EXPR_WFL_NODE
5779                                               (JDEP_DECL_WFL (dep))),
5780                           IDENTIFIER_POINTER (DECL_NAME (decl))));
5781                     }
5782                   else
5783                     error_found = 1;
5784                   dep = JDEP_CHAIN (dep);
5785                   if (JDEP_KIND (dep) == JDEP_METHOD_END)
5786                     break;
5787                   else
5788                     decl = jdep_resolve_class (dep);
5789                 }
5790               if (!error_found)
5791                 {
5792                   tree mdecl = JDEP_DECL (dep), signature;
5793                   /* Recompute and reset the signature, check first that
5794                      all types are now defined. If they're not,
5795                      don't build the signature. */
5796                   if (check_method_types_complete (mdecl))
5797                     {
5798                       signature = build_java_signature (TREE_TYPE (mdecl));
5799                       set_java_signature (TREE_TYPE (mdecl), signature);
5800                     }
5801                 }
5802               else
5803                 continue;
5804               break;
5805
5806             case JDEP_INTERFACE:
5807               if (parser_check_super_interface (decl, JDEP_DECL (dep),
5808                                                 JDEP_WFL (dep)))
5809                 continue;
5810               parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5811               break;
5812
5813             case JDEP_PARM:
5814             case JDEP_VARIABLE:
5815               type = TREE_TYPE(decl);
5816               if (TREE_CODE (type) == RECORD_TYPE)
5817                 type = promote_type (type);
5818               JDEP_APPLY_PATCH (dep, type);
5819               break;
5820
5821             case JDEP_TYPE:
5822               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5823               SOURCE_FRONTEND_DEBUG
5824                 (("Completing a random type dependency on a '%s' node",
5825                   tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5826               break;
5827
5828             case JDEP_EXCEPTION:
5829               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5830               SOURCE_FRONTEND_DEBUG
5831                 (("Completing '%s' 'throws' argument node",
5832                   IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5833               break;
5834
5835             case JDEP_ANONYMOUS:
5836               patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5837               break;
5838
5839             default:
5840               abort ();
5841             }
5842         }
5843     }
5844   return;
5845 }
5846
5847 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5848    array.  */
5849
5850 static tree
5851 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5852 {
5853   tree tname = TYPE_NAME (class_type);
5854   tree resolved_type = TREE_TYPE (class_type);
5855   int array_dims = 0;
5856   tree resolved_type_decl;
5857
5858   if (resolved_type != NULL_TREE)
5859     {
5860       tree resolved_type_decl = TYPE_NAME (resolved_type);
5861       if (resolved_type_decl == NULL_TREE
5862           || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5863         {
5864           resolved_type_decl = build_decl (TYPE_DECL,
5865                                            TYPE_NAME (class_type),
5866                                            resolved_type);
5867         }
5868       return resolved_type_decl;
5869     }
5870
5871   /* 1- Check to see if we have an array. If true, find what we really
5872      want to resolve  */
5873   if ((array_dims = build_type_name_from_array_name (tname,
5874                                                      &TYPE_NAME (class_type))))
5875     WFL_STRIP_BRACKET (cl, cl);
5876
5877   /* 2- Resolve the bare type */
5878   if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5879                                                decl, cl)))
5880     return NULL_TREE;
5881   resolved_type = TREE_TYPE (resolved_type_decl);
5882
5883   /* 3- If we have an array, reconstruct the array down to its nesting */
5884   if (array_dims)
5885     {
5886       for (; array_dims; array_dims--)
5887         resolved_type = build_java_array_type (resolved_type, -1);
5888       resolved_type_decl = TYPE_NAME (resolved_type);
5889     }
5890   TREE_TYPE (class_type) = resolved_type;
5891   return resolved_type_decl;
5892 }
5893
5894 /* Effectively perform the resolution of class CLASS_TYPE.  DECL or CL
5895    are used to report error messages; CL must either be NULL_TREE or a
5896    WFL wrapping a class.  Do not try to replace TYPE_NAME (class_type)
5897    by a variable, since it is changed by find_in_imports{_on_demand}
5898    and (but it doesn't really matter) qualify_and_find.  */
5899
5900 tree
5901 do_resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5902 {
5903   tree new_class_decl = NULL_TREE, super = NULL_TREE;
5904   tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5905   tree decl_result;
5906   htab_t circularity_hash;
5907
5908   if (QUALIFIED_P (TYPE_NAME (class_type)))
5909     {
5910       /* If the type name is of the form `Q . Id', then Q is either a
5911          package name or a class name.  First we try to find Q as a
5912          class and then treat Id as a member type.  If we can't find Q
5913          as a class then we fall through.  */
5914       tree q, left, left_type, right;
5915       if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5916         {
5917           BUILD_PTR_FROM_NAME (left_type, left);
5918           q = do_resolve_class (enclosing, left_type, decl, cl);
5919           if (q)
5920             {
5921               enclosing = q;
5922               saved_enclosing_type = TREE_TYPE (q);
5923               BUILD_PTR_FROM_NAME (class_type, right);
5924             }
5925         }
5926     }
5927
5928   if (enclosing)
5929     {
5930       /* This hash table is used to register the classes we're going
5931          through when searching the current class as an inner class, in
5932          order to detect circular references. Remember to free it before
5933          returning the section 0- of this function. */
5934       circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5935                                       NULL);
5936
5937       /* 0- Search in the current class as an inner class.
5938          Maybe some code here should be added to load the class or
5939          something, at least if the class isn't an inner class and ended
5940          being loaded from class file. FIXME. */
5941       while (enclosing)
5942         {
5943           new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5944                                                 &super, class_type);
5945           if (new_class_decl)
5946             break;
5947
5948           /* If we haven't found anything because SUPER reached Object and
5949              ENCLOSING happens to be an innerclass, try the enclosing context. */
5950           if ((!super || super == object_type_node) &&
5951               enclosing && INNER_CLASS_DECL_P (enclosing))
5952             enclosing = DECL_CONTEXT (enclosing);
5953           else
5954             enclosing = NULL_TREE;
5955         }
5956
5957       htab_delete (circularity_hash);
5958
5959       if (new_class_decl)
5960         return new_class_decl;
5961     }
5962
5963   /* 1- Check for the type in single imports. This will change
5964      TYPE_NAME() if something relevant is found */
5965   find_in_imports (saved_enclosing_type, class_type);
5966
5967   /* 2- And check for the type in the current compilation unit */
5968   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5969     {
5970       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5971         load_class (TYPE_NAME (class_type), 0);
5972       return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5973     }
5974
5975   /* 3- Search according to the current package definition */
5976   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5977     {
5978       if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5979                                              TYPE_NAME (class_type))))
5980         return new_class_decl;
5981     }
5982
5983   /* 4- Check the import on demands. Don't allow bar.baz to be
5984      imported from foo.* */
5985   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5986     if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5987       return NULL_TREE;
5988
5989   /* If found in find_in_imports_on_demand, the type has already been
5990      loaded. */
5991   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5992     return new_class_decl;
5993
5994   /* 5- Try with a name qualified with the package name we've seen so far */
5995   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5996     {
5997       tree package;
5998
5999       /* If there is a current package (ctxp->package), it's the first
6000          element of package_list and we can skip it. */
6001       for (package = (ctxp->package ?
6002                       TREE_CHAIN (package_list) : package_list);
6003            package; package = TREE_CHAIN (package))
6004         if ((new_class_decl = qualify_and_find (class_type,
6005                                                TREE_PURPOSE (package),
6006                                                TYPE_NAME (class_type))))
6007           return new_class_decl;
6008     }
6009
6010   /* 5- Check another compilation unit that bears the name of type */
6011   load_class (TYPE_NAME (class_type), 0);
6012
6013   if (!cl)
6014     cl = lookup_cl (decl);
6015
6016   /* If we don't have a value for CL, then we're being called recursively.
6017      We can't check package access just yet, but it will be taken care of
6018      by the caller. */
6019   if (cl)
6020     {
6021       if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
6022         return NULL_TREE;
6023     }
6024
6025   /* 6- Last call for a resolution */
6026   decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6027
6028   /* The final lookup might have registered a.b.c into a.b$c If we
6029      failed at the first lookup, progressively change the name if
6030      applicable and use the matching DECL instead. */
6031   if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
6032     {
6033       char *separator;
6034       tree name = TYPE_NAME (class_type);
6035       char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
6036
6037       strcpy (namebuffer, IDENTIFIER_POINTER (name));
6038
6039       do {
6040
6041        /* Reach the last '.', and if applicable, replace it by a `$' and
6042           see if this exists as a type. */
6043        if ((separator = strrchr (namebuffer, '.')))
6044          {
6045            *separator = '$';
6046            name = get_identifier (namebuffer);
6047            decl_result = IDENTIFIER_CLASS_VALUE (name);
6048          }
6049       } while (!decl_result && separator);
6050     }
6051   return decl_result;
6052 }
6053
6054 static tree
6055 qualify_and_find (tree class_type, tree package, tree name)
6056 {
6057   tree new_qualified = merge_qualified_name (package, name);
6058   tree new_class_decl;
6059
6060   if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6061     load_class (new_qualified, 0);
6062   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6063     {
6064       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6065         load_class (TREE_TYPE (new_class_decl), 0);
6066       TYPE_NAME (class_type) = new_qualified;
6067       return IDENTIFIER_CLASS_VALUE (new_qualified);
6068     }
6069   return NULL_TREE;
6070 }
6071
6072 /* Resolve NAME and lay it out (if not done and if not the current
6073    parsed class). Return a decl node. This function is meant to be
6074    called when type resolution is necessary during the walk pass.  */
6075
6076 static tree
6077 resolve_and_layout (tree something, tree cl)
6078 {
6079   tree decl, decl_type;
6080
6081   /* Don't do that on the current class */
6082   if (something == current_class)
6083     return TYPE_NAME (current_class);
6084
6085   /* Don't do anything for void and other primitive types */
6086   if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6087     return NULL_TREE;
6088
6089   /* Pointer types can be reall pointer types or fake pointers. When
6090      finding a real pointer, recheck for primitive types */
6091   if (TREE_CODE (something) == POINTER_TYPE)
6092     {
6093       if (TREE_TYPE (something))
6094         {
6095           something = TREE_TYPE (something);
6096           if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6097             return NULL_TREE;
6098         }
6099       else
6100         something = TYPE_NAME (something);
6101     }
6102
6103   /* Don't do anything for arrays of primitive types */
6104   if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6105       && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6106     return NULL_TREE;
6107
6108   /* Something might be a WFL */
6109   if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6110     something = EXPR_WFL_NODE (something);
6111
6112   /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6113      TYPE_DECL or a real TYPE */
6114   else if (TREE_CODE (something) != IDENTIFIER_NODE)
6115     something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6116             DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6117
6118   if (!(decl = resolve_no_layout (something, cl)))
6119     return NULL_TREE;
6120
6121   /* Resolve and layout if necessary */
6122   decl_type = TREE_TYPE (decl);
6123   layout_class_methods (decl_type);
6124   /* Check methods */
6125   if (CLASS_FROM_SOURCE_P (decl_type))
6126     java_check_methods (decl);
6127   /* Layout the type if necessary */
6128   if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6129     safe_layout_class (decl_type);
6130
6131   return decl;
6132 }
6133
6134 /* Resolve a class, returns its decl but doesn't perform any
6135    layout. The current parsing context is saved and restored */
6136
6137 static tree
6138 resolve_no_layout (tree name, tree cl)
6139 {
6140   tree ptr, decl;
6141   BUILD_PTR_FROM_NAME (ptr, name);
6142   java_parser_context_save_global ();
6143   decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6144   java_parser_context_restore_global ();
6145
6146   return decl;
6147 }
6148
6149 /* Called when reporting errors. Skip the '[]'s in a complex array
6150    type description that failed to be resolved. purify_type_name can't
6151    use an identifier tree.  */
6152
6153 static const char *
6154 purify_type_name (const char *name)
6155 {
6156   int len = strlen (name);
6157   int bracket_found;
6158
6159   STRING_STRIP_BRACKETS (name, len, bracket_found);
6160   if (bracket_found)
6161     {
6162       char *stripped_name = xmemdup (name, len, len+1);
6163       stripped_name [len] = '\0';
6164       return stripped_name;
6165     }
6166   return name;
6167 }
6168
6169 /* The type CURRENT refers to can't be found. We print error messages.  */
6170
6171 static void
6172 complete_class_report_errors (jdep *dep)
6173 {
6174   const char *name;
6175
6176   if (!JDEP_WFL (dep))
6177     return;
6178
6179   name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6180   switch (JDEP_KIND (dep))
6181     {
6182     case JDEP_SUPER:
6183       parse_error_context
6184         (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6185          purify_type_name (name),
6186          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6187       break;
6188     case JDEP_FIELD:
6189       parse_error_context
6190         (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6191          purify_type_name (name),
6192          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6193       break;
6194     case JDEP_METHOD:           /* Covers arguments */
6195       parse_error_context
6196         (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6197          purify_type_name (name),
6198          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6199          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6200       break;
6201     case JDEP_METHOD_RETURN:    /* Covers return type */
6202       parse_error_context
6203         (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6204          purify_type_name (name),
6205          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6206       break;
6207     case JDEP_INTERFACE:
6208       parse_error_context
6209         (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6210          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6211          (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6212          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6213       break;
6214     case JDEP_VARIABLE:
6215       parse_error_context
6216         (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6217          purify_type_name (IDENTIFIER_POINTER
6218                            (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6219          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6220       break;
6221     case JDEP_EXCEPTION:        /* As specified by `throws' */
6222       parse_error_context
6223           (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6224          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6225       break;
6226     default:
6227       /* Fix for -Wall. Just break doing nothing. The error will be
6228          caught later */
6229       break;
6230     }
6231 }
6232
6233 /* Return a static string containing the DECL prototype string. If
6234    DECL is a constructor, use the class name instead of the form
6235    <init> */
6236
6237 static const char *
6238 get_printable_method_name (tree decl)
6239 {
6240   const char *to_return;
6241   tree name = NULL_TREE;
6242
6243   if (DECL_CONSTRUCTOR_P (decl))
6244     {
6245       name = DECL_NAME (decl);
6246       DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6247     }
6248
6249   to_return = lang_printable_name (decl, 2);
6250   if (DECL_CONSTRUCTOR_P (decl))
6251     DECL_NAME (decl) = name;
6252
6253   return to_return;
6254 }
6255
6256 /* Track method being redefined inside the same class. As a side
6257    effect, set DECL_NAME to an IDENTIFIER (prior entering this
6258    function it's a FWL, so we can track errors more accurately.)  */
6259
6260 static int
6261 check_method_redefinition (tree class, tree method)
6262 {
6263   tree redef, sig;
6264
6265   /* There's no need to verify <clinit> and finit$ and instinit$ */
6266   if (DECL_CLINIT_P (method)
6267       || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6268     return 0;
6269
6270   sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6271   for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6272     {
6273       if (redef == method)
6274         break;
6275       if (DECL_NAME (redef) == DECL_NAME (method)
6276           && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6277           && !DECL_ARTIFICIAL (method))
6278         {
6279           parse_error_context
6280             (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6281              (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6282              get_printable_method_name (redef));
6283           return 1;
6284         }
6285     }
6286   return 0;
6287 }
6288
6289 /* Return 1 if check went ok, 0 otherwise.  */
6290 static int
6291 check_abstract_method_definitions (int do_interface, tree class_decl,
6292                                    tree type)
6293 {
6294   tree class = TREE_TYPE (class_decl);
6295   tree method, end_type;
6296   int ok = 1;
6297
6298   end_type = (do_interface ? object_type_node : type);
6299   for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6300     {
6301       tree other_super, other_method, method_sig, method_name;
6302       int found = 0;
6303       int end_type_reached = 0;
6304
6305       if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6306         continue;
6307
6308       /* Now verify that somewhere in between TYPE and CLASS,
6309          abstract method METHOD gets a non abstract definition
6310          that is inherited by CLASS.  */
6311
6312       method_sig = build_java_signature (TREE_TYPE (method));
6313       method_name = DECL_NAME (method);
6314       if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6315         method_name = EXPR_WFL_NODE (method_name);
6316
6317       other_super = class;
6318       do {
6319         if (other_super == end_type)
6320           end_type_reached = 1;
6321
6322         /* Method search */
6323         for (other_method = TYPE_METHODS (other_super); other_method;
6324             other_method = TREE_CHAIN (other_method))
6325           {
6326             tree s = build_java_signature (TREE_TYPE (other_method));
6327             tree other_name = DECL_NAME (other_method);
6328
6329             if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6330               other_name = EXPR_WFL_NODE (other_name);
6331             if (!DECL_CLINIT_P (other_method)
6332                 && !DECL_CONSTRUCTOR_P (other_method)
6333                 && method_name == other_name
6334                 && method_sig == s
6335                 && !METHOD_ABSTRACT (other_method))
6336              {
6337                found = 1;
6338                break;
6339              }
6340           }
6341         other_super = CLASSTYPE_SUPER (other_super);
6342       } while (!end_type_reached);
6343
6344       /* Report that abstract METHOD didn't find an implementation
6345          that CLASS can use. */
6346       if (!found)
6347         {
6348           char *t = xstrdup (lang_printable_name
6349                             (TREE_TYPE (TREE_TYPE (method)), 0));
6350           tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6351
6352           parse_error_context
6353             (lookup_cl (class_decl),
6354              "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",
6355              IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6356              t, lang_printable_name (method, 2),
6357              (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6358               "interface" : "class"),
6359              IDENTIFIER_POINTER (ccn),
6360              (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6361              IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6362           ok = 0;
6363           free (t);
6364         }
6365     }
6366
6367   if (ok && do_interface)
6368     {
6369       /* Check for implemented interfaces. */
6370       int i;
6371       tree base_binfo;
6372       
6373       for (i = 1;
6374            ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6375            i++)
6376         ok = check_abstract_method_definitions (1, class_decl,
6377                                                 BINFO_TYPE (base_binfo));
6378     }
6379
6380   return ok;
6381 }
6382
6383 /* Check that CLASS_DECL somehow implements all inherited abstract
6384    methods.  */
6385
6386 static void
6387 java_check_abstract_method_definitions (tree class_decl)
6388 {
6389   tree class = TREE_TYPE (class_decl);
6390   tree super, base_binfo;
6391   int i;
6392
6393   if (CLASS_ABSTRACT (class_decl))
6394     return;
6395
6396   /* Check for inherited types */
6397   super = class;
6398   do {
6399     super = CLASSTYPE_SUPER (super);
6400     check_abstract_method_definitions (0, class_decl, super);
6401   } while (super != object_type_node);
6402
6403   /* Check for implemented interfaces. */
6404   for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6405     check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6406 }
6407
6408 /* Check all the types method DECL uses and return 1 if all of them
6409    are now complete, 0 otherwise. This is used to check whether its
6410    safe to build a method signature or not.  */
6411
6412 static int
6413 check_method_types_complete (tree decl)
6414 {
6415   tree type = TREE_TYPE (decl);
6416   tree args;
6417
6418   if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6419     return 0;
6420
6421   args = TYPE_ARG_TYPES (type);
6422   if (TREE_CODE (type) == METHOD_TYPE)
6423     args = TREE_CHAIN (args);
6424   for (; args != end_params_node; args = TREE_CHAIN (args))
6425     if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6426       return 0;
6427
6428   return 1;
6429 }
6430
6431 /* Visible interface to check methods contained in CLASS_DECL */
6432
6433 void
6434 java_check_methods (tree class_decl)
6435 {
6436   if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6437     return;
6438
6439   if (CLASS_INTERFACE (class_decl))
6440     java_check_abstract_methods (class_decl);
6441   else
6442     java_check_regular_methods (class_decl);
6443
6444   CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6445 }
6446
6447 /* Like not_accessible_p, but doesn't refer to the current class at
6448    all.  */
6449 static bool
6450 hack_is_accessible_p (tree member, tree from_where)
6451 {
6452   int flags = get_access_flags_from_decl (member);
6453
6454   if (from_where == DECL_CONTEXT (member)
6455       || (flags & ACC_PUBLIC))
6456     return true;
6457
6458   if ((flags & ACC_PROTECTED))
6459     {
6460       if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6461         return true;
6462     }
6463
6464   if ((flags & ACC_PRIVATE))
6465     return false;
6466
6467   /* Package private, or protected.  */
6468   return in_same_package (TYPE_NAME (from_where),
6469                           TYPE_NAME (DECL_CONTEXT (member)));
6470 }
6471
6472 /* Check all the methods of CLASS_DECL. Methods are first completed
6473    then checked according to regular method existence rules.  If no
6474    constructor for CLASS_DECL were encountered, then build its
6475    declaration.  */
6476 static void
6477 java_check_regular_methods (tree class_decl)
6478 {
6479   int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6480   tree method;
6481   tree class = TREE_TYPE (class_decl);
6482   tree found = NULL_TREE;
6483   tree mthrows;
6484
6485   /* It is not necessary to check methods defined in java.lang.Object */
6486   if (class == object_type_node)
6487     return;
6488
6489   if (!TYPE_NVIRTUALS (class))
6490     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6491
6492   /* Should take interfaces into account. FIXME */
6493   for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6494     {
6495       tree sig;
6496       tree method_wfl = DECL_FUNCTION_WFL (method);
6497       int aflags;
6498
6499       /* Check for redefinitions */
6500       if (check_method_redefinition (class, method))
6501         continue;
6502
6503       /* We verify things thrown by the method.  They must inherit from
6504          java.lang.Throwable.  */
6505       for (mthrows = DECL_FUNCTION_THROWS (method);
6506            mthrows; mthrows = TREE_CHAIN (mthrows))
6507         {
6508           if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6509             parse_error_context
6510               (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6511                IDENTIFIER_POINTER
6512                  (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6513         }
6514
6515       /* If we see one constructor a mark so we don't generate the
6516          default one.  Also skip other verifications: constructors
6517          can't be inherited hence hidden or overridden.  */
6518       if (DECL_CONSTRUCTOR_P (method))
6519         {
6520           saw_constructor = 1;
6521           continue;
6522         }
6523
6524       sig = build_java_argument_signature (TREE_TYPE (method));
6525       found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6526                                               SEARCH_SUPER | SEARCH_INTERFACE);
6527
6528       /* Inner class can't declare static methods */
6529       if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6530         {
6531           char *t = xstrdup (lang_printable_name (class, 0));
6532           parse_error_context
6533             (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6534              lang_printable_name (method, 2), t);
6535           free (t);
6536         }
6537
6538       /* Nothing overrides or it's a private method. */
6539       if (!found)
6540         continue;
6541       if (METHOD_PRIVATE (found))
6542         {
6543           found = NULL_TREE;
6544           continue;
6545         }
6546
6547       /* If `found' is declared in an interface, make sure the
6548          modifier matches. */
6549       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6550           && clinit_identifier_node != DECL_NAME (found)
6551           && !METHOD_PUBLIC (method))
6552         {
6553           tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6554           parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6555                                IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6556                                lang_printable_name (method, 2),
6557                                IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6558         }
6559
6560       /* Can't override a method with the same name and different return
6561          types. */
6562       if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6563         {
6564           char *t = xstrdup
6565             (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6566           parse_error_context
6567             (method_wfl,
6568              "Method %qs was defined with return type %qs in class %qs",
6569              lang_printable_name (found, 2), t,
6570              IDENTIFIER_POINTER
6571                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6572           free (t);
6573         }
6574
6575       aflags = get_access_flags_from_decl (found);
6576
6577       /* Can't override final. Can't override static. */
6578       if (METHOD_FINAL (found) || METHOD_STATIC (found))
6579         {
6580           /* Static *can* override static */
6581           if (METHOD_STATIC (found) && METHOD_STATIC (method))
6582             continue;
6583           parse_error_context
6584             (method_wfl,
6585              "%s methods can't be overridden. Method %qs is %s in class %qs",
6586              (METHOD_FINAL (found) ? "Final" : "Static"),
6587              lang_printable_name (found, 2),
6588              (METHOD_FINAL (found) ? "final" : "static"),
6589              IDENTIFIER_POINTER
6590                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6591           continue;
6592         }
6593
6594       /* Static method can't override instance method. */
6595       if (METHOD_STATIC (method))
6596         {
6597           parse_error_context
6598             (method_wfl,
6599              "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6600              lang_printable_name (found, 2),
6601              IDENTIFIER_POINTER
6602                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6603           continue;
6604         }
6605
6606       /* - Overriding/hiding public must be public
6607          - Overriding/hiding protected must be protected or public
6608          - If the overridden or hidden method has default (package)
6609            access, then the overriding or hiding method must not be
6610            private; otherwise, a compile-time error occurs.  If
6611            `found' belongs to an interface, things have been already
6612            taken care of.  */
6613       if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6614           && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6615               || (METHOD_PROTECTED (found)
6616                   && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6617               || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6618                   && METHOD_PRIVATE (method))))
6619         {
6620           parse_error_context
6621             (method_wfl,
6622              "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6623              (METHOD_PUBLIC (method) ? "public" :
6624               (METHOD_PRIVATE (method) ? "private" : "protected")),
6625              IDENTIFIER_POINTER (DECL_NAME
6626                                  (TYPE_NAME (DECL_CONTEXT (found)))));
6627           continue;
6628         }
6629
6630       /* Check this method against all the other implementations it
6631          overrides.  Here we only check the class hierarchy; the rest
6632          of the checking is done later.  If this method is just a
6633          Miranda method, we can skip the check.  */
6634       if (! METHOD_INVISIBLE (method))
6635         check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6636     }
6637
6638   /* The above throws clause check only looked at superclasses.  Now
6639      we must also make sure that all methods declared in interfaces
6640      have compatible throws clauses.  FIXME: there are more efficient
6641      ways to organize this checking; we should implement one.  */
6642   check_interface_throws_clauses (class, class);
6643
6644   if (!TYPE_NVIRTUALS (class))
6645     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6646
6647   /* Search for inherited abstract method not yet implemented in this
6648      class.  */
6649   java_check_abstract_method_definitions (class_decl);
6650
6651   if (!saw_constructor)
6652     abort ();
6653 }
6654
6655 /* Check to make sure that all the methods in all the interfaces
6656    implemented by CLASS_DECL are compatible with the concrete
6657    implementations available in CHECK_CLASS_DECL.  */
6658 static void
6659 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6660 {
6661   for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6662     {
6663       int i;
6664
6665       if (! CLASS_LOADED_P (class_decl))
6666         {
6667           if (CLASS_FROM_SOURCE_P (class_decl))
6668             safe_layout_class (class_decl);
6669           else
6670             load_class (class_decl, 1);
6671         }
6672
6673       for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6674         {
6675           tree interface
6676             = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6677           tree iface_method;
6678
6679           for (iface_method = TYPE_METHODS (interface);
6680                iface_method != NULL_TREE;
6681                iface_method = TREE_CHAIN (iface_method))
6682             {
6683               tree sig, method;
6684
6685               /* First look for a concrete method implemented or
6686                  inherited by this class.  No need to search
6687                  interfaces here, since we're already looking through
6688                  all of them.  */
6689               sig = build_java_argument_signature (TREE_TYPE (iface_method));
6690               method
6691                 = lookup_argument_method_generic (check_class_decl,
6692                                                   DECL_NAME (iface_method),
6693                                                   sig, SEARCH_VISIBLE);
6694               /* If we don't find an implementation, that is ok.  Any
6695                  potential errors from that are diagnosed elsewhere.
6696                  Also, multiple inheritance with conflicting throws
6697                  clauses is fine in the absence of a concrete
6698                  implementation.  */
6699               if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6700                   && !METHOD_INVISIBLE (iface_method))
6701                 {
6702                   tree method_wfl = DECL_FUNCTION_WFL (method);
6703                   check_throws_clauses (method, method_wfl, iface_method);
6704                 }
6705             }
6706
6707           /* Now check superinterfaces.  */
6708           check_interface_throws_clauses (check_class_decl, interface);
6709         }
6710     }
6711 }
6712
6713 /* Check throws clauses of a method against the clauses of all the
6714    methods it overrides.  We do this by searching up the class
6715    hierarchy, examining all matching accessible methods.  */
6716 static void
6717 check_concrete_throws_clauses (tree class, tree self_method,
6718                                tree name, tree signature)
6719 {
6720   tree method = lookup_argument_method_generic (class, name, signature,
6721                                                 SEARCH_SUPER | SEARCH_VISIBLE);
6722   while (method != NULL_TREE)
6723     {
6724       if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6725         check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6726                               method);
6727
6728       method = lookup_argument_method_generic (DECL_CONTEXT (method),
6729                                                name, signature,
6730                                                SEARCH_SUPER | SEARCH_VISIBLE);
6731     }
6732 }
6733
6734 /* Generate an error if the `throws' clause of METHOD (if any) is
6735    incompatible with the `throws' clause of FOUND (if any).  */
6736 static void
6737 check_throws_clauses (tree method, tree method_wfl, tree found)
6738 {
6739   tree mthrows;
6740
6741   /* Can't check these things with class loaded from bytecode. FIXME */
6742   if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6743     return;
6744
6745   for (mthrows = DECL_FUNCTION_THROWS (method);
6746        mthrows; mthrows = TREE_CHAIN (mthrows))
6747     {
6748       tree fthrows;
6749
6750       /* We don't verify unchecked expressions */
6751       if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6752         continue;
6753       /* Checked expression must be compatible */
6754       for (fthrows = DECL_FUNCTION_THROWS (found);
6755            fthrows; fthrows = TREE_CHAIN (fthrows))
6756         {
6757           if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6758             break;
6759         }
6760       if (!fthrows)
6761         {
6762           parse_error_context
6763             (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",
6764              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6765              lang_printable_name (found, 2),
6766              IDENTIFIER_POINTER
6767              (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6768         }
6769     }
6770 }
6771
6772 /* Check abstract method of interface INTERFACE */
6773 static void
6774 java_check_abstract_methods (tree interface_decl)
6775 {
6776   int i;
6777   tree method, found;
6778   tree interface = TREE_TYPE (interface_decl);
6779   tree base_binfo;
6780
6781   for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6782     {
6783       /* 2- Check for double definition inside the defining interface */
6784       if (check_method_redefinition (interface, method))
6785         continue;
6786
6787       /* 3- Overriding is OK as far as we preserve the return type.  */
6788       found = lookup_java_interface_method2 (interface, method);
6789       if (found)
6790         {
6791           char *t;
6792           t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6793           parse_error_context
6794             (DECL_FUNCTION_WFL (found),
6795              "Method %qs was defined with return type %qs in class %qs",
6796              lang_printable_name (found, 2), t,
6797              IDENTIFIER_POINTER
6798                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6799           free (t);
6800           continue;
6801         }
6802     }
6803
6804   /* 4- Inherited methods can't differ by their returned types */
6805   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6806     {
6807       tree sub_interface_method, sub_interface;
6808
6809       sub_interface = BINFO_TYPE (base_binfo);
6810       for (sub_interface_method = TYPE_METHODS (sub_interface);
6811            sub_interface_method;
6812            sub_interface_method = TREE_CHAIN (sub_interface_method))
6813         {
6814           found = lookup_java_interface_method2 (interface,
6815                                                  sub_interface_method);
6816           if (found && (found != sub_interface_method))
6817             {
6818               parse_error_context
6819                 (lookup_cl (sub_interface_method),
6820                  "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6821                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6822                  lang_printable_name (found, 2),
6823                  IDENTIFIER_POINTER
6824                    (DECL_NAME (TYPE_NAME
6825                                (DECL_CONTEXT (sub_interface_method)))),
6826                  IDENTIFIER_POINTER
6827                    (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6828             }
6829         }
6830     }
6831 }
6832
6833 /* Lookup methods in interfaces using their name and partial
6834    signature. Return a matching method only if their types differ.  */
6835
6836 static tree
6837 lookup_java_interface_method2 (tree class, tree method_decl)
6838 {
6839   int i;
6840   tree base_binfo;
6841   tree to_return;
6842
6843   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6844     {
6845       if ((BINFO_TYPE (base_binfo) != object_type_node)
6846           && (to_return =
6847               lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6848         return to_return;
6849     }
6850   for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6851     {
6852       to_return = lookup_java_interface_method2
6853         (BINFO_TYPE (base_binfo), method_decl);
6854       if (to_return)
6855         return to_return;
6856     }
6857
6858   return NULL_TREE;
6859 }
6860
6861 /* Lookup method using their name and partial signature. Return a
6862    matching method only if their types differ.  */
6863
6864 static tree
6865 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6866 {
6867   tree method, method_signature, method_name, method_type, name;
6868
6869   method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6870   name = DECL_NAME (method_decl);
6871   method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6872                  EXPR_WFL_NODE (name) : name);
6873   method_type = TREE_TYPE (TREE_TYPE (method_decl));
6874
6875   while (clas != NULL_TREE)
6876     {
6877       for (method = TYPE_METHODS (clas);
6878            method != NULL_TREE;  method = TREE_CHAIN (method))
6879         {
6880           tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6881           tree name = DECL_NAME (method);
6882           if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6883                EXPR_WFL_NODE (name) : name) == method_name
6884               && method_sig == method_signature
6885               && TREE_TYPE (TREE_TYPE (method)) != method_type)
6886             return method;
6887         }
6888       clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6889     }
6890   return NULL_TREE;
6891 }
6892
6893 /* Return the line that matches DECL line number, and try its best to
6894    position the column number. Used during error reports.
6895    FUTURE/FIXME: return source_location instead of node. */
6896
6897 static GTY(()) tree cl_v;
6898 static tree
6899 lookup_cl (tree decl)
6900 {
6901 #ifndef USE_MAPPED_LOCATION
6902   char *line, *found;
6903 #endif
6904
6905   if (!decl)
6906     return NULL_TREE;
6907
6908   if (cl_v == NULL_TREE)
6909     {
6910       cl_v = build_unknown_wfl (NULL_TREE);
6911     }
6912
6913 #ifdef USE_MAPPED_LOCATION
6914   SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6915 #else
6916   EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6917   EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6918
6919   line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6920                             EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6921
6922   found = strstr ((const char *)line,
6923                   (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6924   if (found)
6925     EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6926 #endif
6927
6928   return cl_v;
6929 }
6930
6931 /* Look for a simple name in the single-type import list */
6932
6933 static tree
6934 find_name_in_single_imports (tree name)
6935 {
6936   tree node;
6937
6938   for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6939     if (TREE_VALUE (node) == name)
6940       return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6941
6942   return NULL_TREE;
6943 }
6944
6945 /* Process all single-type import. */
6946
6947 static int
6948 process_imports (void)
6949 {
6950   tree import;
6951   int error_found;
6952
6953   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6954     {
6955       tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6956       char *original_name;
6957
6958       /* Don't load twice something already defined. */
6959       if (IDENTIFIER_CLASS_VALUE (to_be_found))
6960         continue;
6961
6962       original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6963                                IDENTIFIER_LENGTH (to_be_found),
6964                                IDENTIFIER_LENGTH (to_be_found) + 1);
6965
6966       while (1)
6967         {
6968           tree left;
6969
6970           QUALIFIED_P (to_be_found) = 1;
6971           load_class (to_be_found, 0);
6972           error_found =
6973             check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6974
6975           /* We found it, we can bail out */
6976           if (IDENTIFIER_CLASS_VALUE (to_be_found))
6977             {
6978               check_deprecation (TREE_PURPOSE (import),
6979                                  IDENTIFIER_CLASS_VALUE (to_be_found));
6980               break;
6981             }
6982
6983           /* We haven't found it. Maybe we're trying to access an
6984              inner class.  The only way for us to know is to try again
6985              after having dropped a qualifier. If we can't break it further,
6986              we have an error. */
6987           if (split_qualified_name (&left, NULL, to_be_found))
6988             break;
6989
6990           to_be_found = left;
6991         }
6992       if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6993         {
6994           parse_error_context (TREE_PURPOSE (import),
6995                                "Class or interface %qs not found in import",
6996                                original_name);
6997           error_found = 1;
6998         }
6999
7000       free (original_name);
7001       if (error_found)
7002         return 1;
7003     }
7004   return 0;
7005 }
7006
7007 /* Possibly find and mark a class imported by a single-type import
7008    statement.  */
7009
7010 static void
7011 find_in_imports (tree enclosing_type, tree class_type)
7012 {
7013   tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
7014                  ctxp->import_list);
7015   while (import)
7016     {
7017       if (TREE_VALUE (import) == TYPE_NAME (class_type))
7018         {
7019           TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
7020           QUALIFIED_P (TYPE_NAME (class_type)) = 1;
7021           return;
7022         }
7023       import = TREE_CHAIN (import);
7024     }
7025 }
7026
7027 static int
7028 note_possible_classname (const char *name, int len)
7029 {
7030   tree node;
7031   if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
7032     len = len - 5;
7033   else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
7034     len = len - 6;
7035   else
7036     return 0;
7037   node = ident_subst (name, len, "", '/', '.', "");
7038   IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
7039   QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
7040   return 1;
7041 }
7042
7043 /* Read a import directory, gathering potential match for further type
7044    references. Indifferently reads a filesystem or a ZIP archive
7045    directory.  */
7046
7047 static void
7048 read_import_dir (tree wfl)
7049 {
7050   tree package_id = EXPR_WFL_NODE (wfl);
7051   const char *package_name = IDENTIFIER_POINTER (package_id);
7052   int package_length = IDENTIFIER_LENGTH (package_id);
7053   DIR *dirp = NULL;
7054   JCF *saved_jcf = current_jcf;
7055
7056   int found = 0;
7057   int k;
7058   void *entry;
7059   struct buffer filename[1];
7060
7061   if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7062     return;
7063   IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7064
7065   BUFFER_INIT (filename);
7066   buffer_grow (filename, package_length + 100);
7067
7068   for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7069     {
7070       const char *entry_name = jcf_path_name (entry);
7071       int entry_length = strlen (entry_name);
7072       if (jcf_path_is_zipfile (entry))
7073         {
7074           ZipFile *zipf;
7075           buffer_grow (filename, entry_length);
7076           memcpy (filename->data, entry_name, entry_length - 1);
7077           filename->data[entry_length-1] = '\0';
7078           zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7079           if (zipf == NULL)
7080             error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7081           else
7082             {
7083               ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7084               BUFFER_RESET (filename);
7085               for (k = 0; k < package_length; k++)
7086                 {
7087                   char ch = package_name[k];
7088                   *filename->ptr++ = ch == '.' ? '/' : ch;
7089                 }
7090               *filename->ptr++ = '/';
7091
7092               for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
7093                 {
7094                   const char *current_entry = ZIPDIR_FILENAME (zipd);
7095                   int current_entry_len = zipd->filename_length;
7096
7097                   if (current_entry_len >= BUFFER_LENGTH (filename)
7098                       && strncmp ((const char *) filename->data, current_entry,
7099                                   BUFFER_LENGTH (filename)) != 0)
7100                     continue;
7101                   found |= note_possible_classname (current_entry,
7102                                                     current_entry_len);
7103                 }
7104             }
7105         }
7106       else
7107         {
7108           BUFFER_RESET (filename);
7109           buffer_grow (filename, entry_length + package_length + 4);
7110           strcpy ((char *) filename->data, entry_name);
7111           filename->ptr = filename->data + entry_length;
7112           for (k = 0; k < package_length; k++)
7113             {
7114               char ch = package_name[k];
7115               *filename->ptr++ = ch == '.' ? '/' : ch;
7116             }
7117           *filename->ptr = '\0';
7118
7119           dirp = opendir ((const char *) filename->data);
7120           if (dirp == NULL)
7121             continue;
7122           *filename->ptr++ = '/';
7123           for (;;)
7124             {
7125               int len;
7126               const char *d_name;
7127               struct dirent *direntp = readdir (dirp);
7128               if (!direntp)
7129                 break;
7130               d_name = direntp->d_name;
7131               len = strlen (direntp->d_name);
7132               buffer_grow (filename, len+1);
7133               strcpy ((char *) filename->ptr, d_name);
7134               found |= note_possible_classname ((const char *) filename->data + entry_length,
7135                                                 package_length+len+1);
7136             }
7137           if (dirp)
7138             closedir (dirp);
7139         }
7140     }
7141
7142   free (filename->data);
7143
7144   /* Here we should have a unified way of retrieving an entry, to be
7145      indexed. */
7146   if (!found)
7147     {
7148       static int first = 1;
7149       if (first)
7150         {
7151           error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7152           java_error_count++;
7153           first = 0;
7154         }
7155       else
7156         parse_error_context (wfl, "Package %qs not found in import",
7157                              package_name);
7158       current_jcf = saved_jcf;
7159       return;
7160     }
7161   current_jcf = saved_jcf;
7162 }
7163
7164 /* Possibly find a type in the import on demands specified
7165    types. Returns 1 if an error occurred, 0 otherwise. Run through the
7166    entire list, to detected potential double definitions.  */
7167
7168 static int
7169 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7170 {
7171   tree class_type_name = TYPE_NAME (class_type);
7172   tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
7173                   ctxp->import_demand_list);
7174   tree cl = NULL_TREE;
7175   int seen_once = -1;   /* -1 when not set, 1 if seen once, >1 otherwise. */
7176   int to_return = -1;   /* -1 when not set, 0 or 1 otherwise */
7177   tree node;
7178
7179   for (; import; import = TREE_CHAIN (import))
7180     {
7181       location_t saved_location = input_location;
7182       int access_check;
7183       const char *id_name;
7184       tree decl, type_name_copy;
7185
7186       obstack_grow (&temporary_obstack,
7187                     IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7188                     IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7189       obstack_1grow (&temporary_obstack, '.');
7190       obstack_grow0 (&temporary_obstack,
7191                      IDENTIFIER_POINTER (class_type_name),
7192                      IDENTIFIER_LENGTH (class_type_name));
7193       id_name = obstack_finish (&temporary_obstack);
7194
7195       if (! (node = maybe_get_identifier (id_name)))
7196         continue;
7197
7198       /* Setup input_line so that it refers to the line of the import (in
7199          case we parse a class file and encounter errors */
7200 #ifdef USE_MAPPED_LOCATION
7201       input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7202 #else
7203       input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7204 #endif
7205
7206       type_name_copy = TYPE_NAME (class_type);
7207       TYPE_NAME (class_type) = node;
7208       QUALIFIED_P (node) = 1;
7209       decl = IDENTIFIER_CLASS_VALUE (node);
7210       access_check = -1;
7211       /* If there is no DECL set for the class or if the class isn't
7212          loaded and not seen in source yet, then load */
7213       if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7214         {
7215           load_class (node, 0);
7216           decl = IDENTIFIER_CLASS_VALUE (node);
7217         }
7218       if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7219         access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7220                                                false, NULL_TREE);
7221       else
7222         /* 6.6.1: Inner classes are subject to member access rules. */
7223         access_check = 0;
7224
7225       input_location = saved_location;
7226
7227       /* If the loaded class is not accessible or couldn't be loaded,
7228          we restore the original TYPE_NAME and process the next
7229          import. */
7230       if (access_check || !decl)
7231         {
7232           TYPE_NAME (class_type) = type_name_copy;
7233           continue;
7234         }
7235
7236       /* If the loaded class is accessible, we keep a tab on it to
7237          detect and report multiple inclusions. */
7238       if (IS_A_CLASSFILE_NAME (node))
7239         {
7240           if (seen_once < 0)
7241             {
7242               cl = TREE_PURPOSE (import);
7243               seen_once = 1;
7244             }
7245           else if (seen_once >= 0)
7246             {
7247               tree location = (cl ? cl : TREE_PURPOSE (import));
7248               tree package = (cl ? EXPR_WFL_NODE (cl) :
7249                               EXPR_WFL_NODE (TREE_PURPOSE (import)));
7250               seen_once++;
7251               parse_error_context
7252                 (location,
7253                  "Type %qs also potentially defined in package %qs",
7254                  IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7255                  IDENTIFIER_POINTER (package));
7256             }
7257         }
7258       to_return = access_check;
7259     }
7260
7261   if (seen_once == 1)
7262     return to_return;
7263   else
7264     return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7265 }
7266
7267 /* Add package NAME to the list of packages encountered so far. To
7268    speed up class lookup in do_resolve_class, we make sure a
7269    particular package is added only once.  */
7270
7271 static void
7272 register_package (tree name)
7273 {
7274   static htab_t pht;
7275   void **e;
7276
7277   if (pht == NULL)
7278     pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
7279
7280   e = htab_find_slot (pht, name, INSERT);
7281   if (*e == NULL)
7282     {
7283       package_list = chainon (package_list, build_tree_list (name, NULL));
7284       *e = name;
7285     }
7286 }
7287
7288 static tree
7289 resolve_package (tree pkg, tree *next, tree *type_name)
7290 {
7291   tree current;
7292   tree decl = NULL_TREE;
7293   *type_name = NULL_TREE;
7294
7295   /* The trick is to determine when the package name stops and were
7296      the name of something contained in the package starts. Then we
7297      return a fully qualified name of what we want to get. */
7298
7299   *next = EXPR_WFL_QUALIFICATION (pkg);
7300
7301   /* Try to progressively construct a type name */
7302   if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7303     for (current = EXPR_WFL_QUALIFICATION (pkg);
7304          current; current = TREE_CHAIN (current))
7305       {
7306         /* If we don't have what we're expecting, exit now. TYPE_NAME
7307            will be null and the error caught later. */
7308         if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7309           break;
7310         *type_name =
7311           merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7312         if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7313           {
7314             /* resolve_package should be used in a loop, hence we
7315                point at this one to naturally process the next one at
7316                the next iteration. */
7317             *next = current;
7318             break;
7319           }
7320       }
7321   return decl;
7322 }
7323
7324
7325 /* Check accessibility of inner classes according to member access rules.
7326    DECL is the inner class, ENCLOSING_DECL is the class from which the
7327    access is being attempted. */
7328
7329 static void
7330 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7331 {
7332   const char *access;
7333   tree enclosing_decl_type;
7334
7335   /* We don't issue an error message when CL is null. CL can be null
7336      as a result of processing a JDEP crafted by source_start_java_method
7337      for the purpose of patching its parm decl. But the error would
7338      have been already trapped when fixing the method's signature.
7339      DECL can also be NULL in case of earlier errors. */
7340   if (!decl || !cl)
7341     return;
7342
7343   enclosing_decl_type = TREE_TYPE (enclosing_decl);
7344
7345   if (CLASS_PRIVATE (decl))
7346     {
7347       /* Access is permitted only within the body of the top-level
7348          class in which DECL is declared. */
7349       tree top_level = decl;
7350       while (DECL_CONTEXT (top_level))
7351         top_level = DECL_CONTEXT (top_level);
7352       while (DECL_CONTEXT (enclosing_decl))
7353         enclosing_decl = DECL_CONTEXT (enclosing_decl);
7354       if (top_level == enclosing_decl)
7355         return;
7356       access = "private";
7357     }
7358   else if (CLASS_PROTECTED (decl))
7359     {
7360       tree decl_context;
7361       /* Access is permitted from within the same package... */
7362       if (in_same_package (decl, enclosing_decl))
7363         return;
7364
7365       /* ... or from within the body of a subtype of the context in which
7366          DECL is declared. */
7367       decl_context = DECL_CONTEXT (decl);
7368       while (enclosing_decl)
7369         {
7370           if (CLASS_INTERFACE (decl))
7371             {
7372               if (interface_of_p (TREE_TYPE (decl_context),
7373                                   enclosing_decl_type))
7374                 return;
7375             }
7376           else
7377             {
7378               /* Eww. The order of the arguments is different!! */
7379               if (inherits_from_p (enclosing_decl_type,
7380                                    TREE_TYPE (decl_context)))
7381                 return;
7382             }
7383           enclosing_decl = DECL_CONTEXT (enclosing_decl);
7384         }
7385       access = "protected";
7386     }
7387   else if (! CLASS_PUBLIC (decl))
7388     {
7389       /* Access is permitted only from within the same package as DECL. */
7390       if (in_same_package (decl, enclosing_decl))
7391         return;
7392       access = "non-public";
7393     }
7394   else
7395     /* Class is public. */
7396     return;
7397
7398   parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7399                        (CLASS_INTERFACE (decl) ? "interface" : "class"),
7400                        lang_printable_name (decl, 2), access);
7401 }
7402
7403 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7404    foreign package, it must be PUBLIC. Return 0 if no access
7405    violations were found, 1 otherwise. If VERBOSE is true and an error
7406    was found, it is reported and accounted for.  If CL is NULL then 
7407    look it up with THIS_DECL.  */
7408
7409 static int
7410 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7411 {
7412   tree type;
7413
7414   if (!IDENTIFIER_CLASS_VALUE (class_name))
7415     return 0;
7416
7417   if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7418     return 0;
7419
7420   if (!CLASS_PUBLIC (TYPE_NAME (type)))
7421     {
7422       /* Access to a private class within the same package is
7423          allowed. */
7424       tree l, r;
7425       split_qualified_name (&l, &r, class_name);
7426       if (!QUALIFIED_P (class_name) && !ctxp->package)
7427         /* Both in the empty package. */
7428         return 0;
7429       if (l == ctxp->package)
7430         /* Both in the same package. */
7431         return 0;
7432
7433       if (verbose)
7434         parse_error_context
7435           (cl == NULL ? lookup_cl (this_decl): cl,
7436            "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7437            (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7438            IDENTIFIER_POINTER (class_name));
7439       return 1;
7440     }
7441   return 0;
7442 }
7443
7444 /* Local variable declaration. */
7445
7446 static void
7447 declare_local_variables (int modifier, tree type, tree vlist)
7448 {
7449   tree decl, current, saved_type;
7450   tree type_wfl = NULL_TREE;
7451   int must_chain = 0;
7452   int final_p = 0;
7453
7454   /* Push a new block if statements were seen between the last time we
7455      pushed a block and now. Keep a count of blocks to close */
7456   if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7457     {
7458       tree b = enter_block ();
7459       BLOCK_IS_IMPLICIT (b) = 1;
7460     }
7461
7462   if (modifier)
7463     {
7464       size_t i;
7465       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7466         if (1 << i & modifier)
7467           break;
7468       if (modifier == ACC_FINAL)
7469         final_p = 1;
7470       else
7471         {
7472           parse_error_context
7473             (ctxp->modifier_ctx [i],
7474              "Only %<final%> is allowed as a local variables modifier");
7475           return;
7476         }
7477     }
7478
7479   /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7480      hold the TYPE value if a new incomplete has to be created (as
7481      opposed to being found already existing and reused). */
7482   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7483
7484   /* If TYPE is fully resolved and we don't have a reference, make one */
7485   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7486
7487   /* Go through all the declared variables */
7488   for (current = vlist, saved_type = type; current;
7489        current = TREE_CHAIN (current), type = saved_type)
7490     {
7491       tree other, real_type;
7492       tree wfl  = TREE_PURPOSE (current);
7493       tree name = EXPR_WFL_NODE (wfl);
7494       tree init = TREE_VALUE (current);
7495
7496       /* Process NAME, as it may specify extra dimension(s) for it */
7497       type = build_array_from_name (type, type_wfl, name, &name);
7498
7499       /* Variable redefinition check */
7500       if ((other = lookup_name_in_blocks (name)))
7501         {
7502           variable_redefinition_error (wfl, name, TREE_TYPE (other),
7503                                        DECL_SOURCE_LINE (other));
7504           continue;
7505         }
7506
7507       /* Type adjustment. We may have just readjusted TYPE because
7508          the variable specified more dimensions. Make sure we have
7509          a reference if we can and don't have one already. */
7510       PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7511
7512       real_type = GET_REAL_TYPE (type);
7513       /* Never layout this decl. This will be done when its scope
7514          will be entered */
7515       decl = build_decl (VAR_DECL, name, real_type);
7516       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7517       DECL_FINAL (decl) = final_p;
7518       BLOCK_CHAIN_DECL (decl);
7519
7520       /* If doing xreferencing, replace the line number with the WFL
7521          compound value */
7522 #ifdef USE_MAPPED_LOCATION
7523       if (flag_emit_xref)
7524         DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (wfl);
7525 #else
7526       if (flag_emit_xref)
7527         DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7528 #endif
7529
7530       /* Don't try to use an INIT statement when an error was found */
7531       if (init && java_error_count)
7532         init = NULL_TREE;
7533
7534       /* Remember it if this is an initialized-upon-declaration final
7535          variable.  */
7536       if (init && final_p)
7537         {
7538           DECL_LOCAL_FINAL_IUD (decl) = 1;
7539         }
7540
7541       /* Add the initialization function to the current function's code */
7542       if (init)
7543         {
7544           /* Name might have been readjusted */
7545           EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7546           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7547           java_method_add_stmt (current_function_decl,
7548                                 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7549                                                       init));
7550         }
7551
7552       /* Setup dependency the type of the decl */
7553       if (must_chain)
7554         {
7555           jdep *dep;
7556           register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7557           dep = CLASSD_LAST (ctxp->classd_list);
7558           JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7559         }
7560     }
7561   SOURCE_FRONTEND_DEBUG (("Defined locals"));
7562 }
7563
7564 /* Called during parsing. Build decls from argument list.  */
7565
7566 static void
7567 source_start_java_method (tree fndecl)
7568 {
7569   tree tem;
7570   tree parm_decl;
7571   int i;
7572
7573   if (!fndecl)
7574     return;
7575
7576   current_function_decl = fndecl;
7577
7578   /* New scope for the function */
7579   enter_block ();
7580   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7581        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7582     {
7583       tree type = TREE_VALUE (tem);
7584       tree name = TREE_PURPOSE (tem);
7585
7586       /* If type is incomplete. Create an incomplete decl and ask for
7587          the decl to be patched later */
7588       if (INCOMPLETE_TYPE_P (type))
7589         {
7590           jdep *jdep;
7591           tree real_type = GET_REAL_TYPE (type);
7592           parm_decl = build_decl (PARM_DECL, name, real_type);
7593           type = obtain_incomplete_type (type);
7594           register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7595           jdep = CLASSD_LAST (ctxp->classd_list);
7596           JDEP_MISC (jdep) = name;
7597           JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7598         }
7599       else
7600         parm_decl = build_decl (PARM_DECL, name, type);
7601
7602       /* Remember if a local variable was declared final (via its
7603          TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7604       if (ARG_FINAL_P (tem))
7605         {
7606           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7607           DECL_FINAL (parm_decl) = 1;
7608         }
7609
7610       BLOCK_CHAIN_DECL (parm_decl);
7611     }
7612   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7613   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7614     nreverse (tem);
7615   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7616   DECL_MAX_LOCALS (current_function_decl) = i;
7617 }
7618
7619 /* Called during parsing. Creates an artificial method declaration.  */
7620
7621 static tree
7622 create_artificial_method (tree class, int flags, tree type,
7623                           tree name, tree args)
7624 {
7625   tree mdecl;
7626   location_t save_location = input_location;
7627
7628   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7629   mdecl = make_node (FUNCTION_TYPE);
7630   TREE_TYPE (mdecl) = type;
7631   TYPE_ARG_TYPES (mdecl) = args;
7632   /* We used to compute the signature of MDECL here and then use
7633      add_method(), but that failed because our caller might modify
7634      the type of the returned method, which trashes the cache in
7635      get_type_from_signature().  */
7636   mdecl = add_method_1 (class, flags, name, mdecl);
7637   input_location = save_location;
7638   DECL_ARTIFICIAL (mdecl) = 1;
7639   return mdecl;
7640 }
7641
7642 /* Starts the body if an artificial method.  */
7643
7644 static void
7645 start_artificial_method_body (tree mdecl)
7646 {
7647 #ifdef USE_MAPPED_LOCATION
7648   DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7649   DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7650 #else
7651   DECL_SOURCE_LINE (mdecl) = 1;
7652   DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7653 #endif
7654   source_start_java_method (mdecl);
7655   enter_block ();
7656 }
7657
7658 static void
7659 end_artificial_method_body (tree mdecl)
7660 {
7661   /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7662      It has to be evaluated first. (if mdecl is current_function_decl,
7663      we have an undefined behavior if no temporary variable is used.) */
7664   tree b = exit_block ();
7665   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7666   exit_block ();
7667 }
7668
7669 /* Dump a tree of some kind.  This is a convenience wrapper for the
7670    dump_* functions in tree-dump.c.  */
7671 static void
7672 dump_java_tree (enum tree_dump_index phase, tree t)
7673 {
7674   FILE *stream;
7675   int flags;
7676
7677   stream = dump_begin (phase, &flags);
7678   flags |= TDF_SLIM;
7679   if (stream)
7680     {
7681       dump_node (t, flags, stream);
7682       dump_end (phase, stream);
7683     }
7684 }
7685
7686 /* Terminate a function and expand its body.  */
7687
7688 static void
7689 source_end_java_method (void)
7690 {
7691   tree fndecl = current_function_decl;
7692
7693   if (!fndecl)
7694     return;
7695
7696   java_parser_context_save_global ();
7697 #ifdef USE_MAPPED_LOCATION
7698   input_location = ctxp->last_ccb_indent1;
7699 #else
7700   input_line = ctxp->last_ccb_indent1;
7701 #endif
7702
7703   /* Turn function bodies with only a NOP expr null, so they don't get
7704      generated at all and we won't get warnings when using the -W
7705      -Wall flags. */
7706   if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7707     BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7708
7709   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7710       && ! flag_emit_class_files
7711       && ! flag_emit_xref)
7712     finish_method (fndecl);
7713
7714   current_function_decl = NULL_TREE;
7715   java_parser_context_restore_global ();
7716   current_function_decl = NULL_TREE;
7717 }
7718
7719 /* Record EXPR in the current function block. Complements compound
7720    expression second operand if necessary.  */
7721
7722 tree
7723 java_method_add_stmt (tree fndecl, tree expr)
7724 {
7725   if (!GET_CURRENT_BLOCK (fndecl))
7726     return NULL_TREE;
7727   return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7728 }
7729
7730 static tree
7731 add_stmt_to_block (tree b, tree type, tree stmt)
7732 {
7733   tree body = BLOCK_EXPR_BODY (b), c;
7734
7735   if (java_error_count)
7736     return body;
7737
7738   if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7739     return body;
7740
7741   BLOCK_EXPR_BODY (b) = c;
7742   TREE_SIDE_EFFECTS (c) = 1;
7743   return c;
7744 }
7745
7746 /* Lays out the methods for the classes seen so far.  */
7747
7748 void
7749 java_layout_seen_class_methods (void)
7750 {
7751   tree previous_list = all_class_list;
7752   tree end = NULL_TREE;
7753   tree current;
7754
7755   while (1)
7756     {
7757       for (current = previous_list;
7758            current != end; current = TREE_CHAIN (current))
7759         {
7760           tree decl = TREE_VALUE (current);
7761           tree cls = TREE_TYPE (decl);
7762
7763           input_location = DECL_SOURCE_LOCATION (decl);
7764
7765           if (! CLASS_LOADED_P (cls))
7766             load_class (cls, 0);
7767
7768           layout_class_methods (cls);
7769         }
7770
7771       /* Note that new classes might have been added while laying out
7772          methods, changing the value of all_class_list.  */
7773
7774       if (previous_list != all_class_list)
7775         {
7776           end = previous_list;
7777           previous_list = all_class_list;
7778         }
7779       else
7780         break;
7781     }
7782 }
7783
7784 static GTY(()) tree stop_reordering;
7785 void
7786 java_reorder_fields (void)
7787 {
7788   tree current;
7789
7790   for (current = gclass_list; current; current = TREE_CHAIN (current))
7791     {
7792       output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7793
7794       if (current_class == stop_reordering)
7795         break;
7796
7797       /* Reverse the fields, but leave the dummy field in front.
7798          Fields are already ordered for Object and Class */
7799       if (TYPE_FIELDS (current_class) && current_class != object_type_node
7800           && current_class != class_type_node)
7801       {
7802         /* If the dummy field is there, reverse the right fields and
7803            just layout the type for proper fields offset */
7804         if (!DECL_NAME (TYPE_FIELDS (current_class)))
7805           {
7806             tree fields = TYPE_FIELDS (current_class);
7807             TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7808             TYPE_SIZE (current_class) = NULL_TREE;
7809           }
7810         /* We don't have a dummy field, we need to layout the class,
7811            after having reversed the fields */
7812         else
7813           {
7814             TYPE_FIELDS (current_class) =
7815               nreverse (TYPE_FIELDS (current_class));
7816             TYPE_SIZE (current_class) = NULL_TREE;
7817           }
7818       }
7819     }
7820   /* There are cases were gclass_list will be empty. */
7821   if (gclass_list)
7822     stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7823 }
7824
7825 /* Layout the methods of all classes loaded in one way or another.
7826    Check methods of source parsed classes. Then reorder the
7827    fields and layout the classes or the type of all source parsed
7828    classes */
7829
7830 void
7831 java_layout_classes (void)
7832 {
7833   tree current;
7834   int save_error_count = java_error_count;
7835
7836   /* Layout the methods of all classes seen so far */
7837   java_layout_seen_class_methods ();
7838   java_parse_abort_on_error ();
7839   all_class_list = NULL_TREE;
7840
7841   /* Then check the methods of all parsed classes */
7842   for (current = gclass_list; current; current = TREE_CHAIN (current))
7843     if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7844       java_check_methods (TREE_VALUE (current));
7845   java_parse_abort_on_error ();
7846
7847   for (current = gclass_list; current; current = TREE_CHAIN (current))
7848     {
7849       output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7850       layout_class (current_class);
7851
7852       /* Error reported by the caller */
7853       if (java_error_count)
7854         return;
7855     }
7856
7857   /* We might have reloaded classes durign the process of laying out
7858      classes for code generation. We must layout the methods of those
7859      late additions, as constructor checks might use them */
7860   java_layout_seen_class_methods ();
7861   java_parse_abort_on_error ();
7862 }
7863
7864 /* Expand methods in the current set of classes remembered for
7865    generation.  */
7866
7867 static void
7868 java_complete_expand_classes (void)
7869 {
7870   tree current;
7871
7872   do_not_fold = flag_emit_xref;
7873
7874   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7875     if (!INNER_CLASS_DECL_P (current))
7876       java_complete_expand_class (current);
7877 }
7878
7879 /* Expand the methods found in OUTER, starting first by OUTER's inner
7880    classes, if any.  */
7881
7882 static void
7883 java_complete_expand_class (tree outer)
7884 {
7885   tree inner_list;
7886
7887   /* We need to go after all inner classes and start expanding them,
7888      starting with most nested ones. We have to do that because nested
7889      classes might add functions to outer classes */
7890
7891   for (inner_list = DECL_INNER_CLASS_LIST (outer);
7892        inner_list; inner_list = TREE_CHAIN (inner_list))
7893     java_complete_expand_class (TREE_PURPOSE (inner_list));
7894
7895   java_complete_expand_methods (outer);
7896 }
7897
7898 /* Expand methods registered in CLASS_DECL. The general idea is that
7899    we expand regular methods first. This allows us get an estimate on
7900    how outer context local alias fields are really used so we can add
7901    to the constructor just enough code to initialize them properly (it
7902    also lets us generate finit$ correctly.) Then we expand the
7903    constructors and then <clinit>.  */
7904
7905 static void
7906 java_complete_expand_methods (tree class_decl)
7907 {
7908   tree clinit, decl, first_decl;
7909
7910   output_class = current_class = TREE_TYPE (class_decl);
7911
7912   /* Pre-expand <clinit> to figure whether we really need it or
7913      not. If we do need it, we pre-expand the static fields so they're
7914      ready to be used somewhere else. <clinit> will be fully expanded
7915      after we processed the constructors. */
7916   first_decl = TYPE_METHODS (current_class);
7917   clinit = maybe_generate_pre_expand_clinit (current_class);
7918
7919   /* Then generate finit$ (if we need to) because constructors will
7920    try to use it.*/
7921   if (TYPE_FINIT_STMT_LIST (current_class))
7922     java_complete_expand_method (generate_finit (current_class));
7923
7924   /* Then generate instinit$ (if we need to) because constructors will
7925      try to use it. */
7926   if (TYPE_II_STMT_LIST (current_class))
7927     java_complete_expand_method (generate_instinit (current_class));
7928
7929   /* Now do the constructors */
7930   for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7931     {
7932       if (!DECL_CONSTRUCTOR_P (decl))
7933         continue;
7934       java_complete_expand_method (decl);
7935     }
7936
7937   /* First, do the ordinary methods. */
7938   for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7939     {
7940       /* Ctors aren't part of this batch. */
7941       if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7942         continue;
7943
7944       /* Skip abstract or native methods -- but do handle native
7945          methods when generating JNI stubs.  */
7946       if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7947         {
7948           DECL_FUNCTION_BODY (decl) = NULL_TREE;
7949           continue;
7950         }
7951
7952       if (METHOD_NATIVE (decl))
7953         {
7954           tree body;
7955           current_function_decl = decl;
7956           body = build_jni_stub (decl);
7957           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7958         }
7959
7960       java_complete_expand_method (decl);
7961     }
7962
7963   /* If there is indeed a <clinit>, fully expand it now */
7964   if (clinit)
7965     {
7966       /* Prevent the use of `this' inside <clinit> */
7967       ctxp->explicit_constructor_p = 1;
7968       java_complete_expand_method (clinit);
7969       ctxp->explicit_constructor_p = 0;
7970     }
7971
7972   /* We might have generated a class$ that we now want to expand */
7973   if (TYPE_DOT_CLASS (current_class))
7974     java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7975
7976   /* Now verify constructor circularity (stop after the first one we
7977      prove wrong.) */
7978   if (!CLASS_INTERFACE (class_decl))
7979     for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7980       if (DECL_CONSTRUCTOR_P (decl)
7981           && verify_constructor_circularity (decl, decl))
7982         break;
7983 }
7984
7985 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7986    safely used in some other methods/constructors.  */
7987
7988 static tree
7989 maybe_generate_pre_expand_clinit (tree class_type)
7990 {
7991   tree current, mdecl;
7992
7993   if (!TYPE_CLINIT_STMT_LIST (class_type))
7994     return NULL_TREE;
7995
7996   /* Go through all static fields and pre expand them */
7997   for (current = TYPE_FIELDS (class_type); current;
7998        current = TREE_CHAIN (current))
7999     if (FIELD_STATIC (current))
8000       build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
8001
8002   /* Then build the <clinit> method */
8003   mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
8004                                     clinit_identifier_node, end_params_node);
8005   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
8006                        mdecl, NULL_TREE);
8007   start_artificial_method_body (mdecl);
8008
8009   /* We process the list of assignment we produced as the result of
8010      the declaration of initialized static field and add them as
8011      statement to the <clinit> method. */
8012   for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
8013        current = TREE_CHAIN (current))
8014     {
8015       tree stmt = current;
8016       /* We build the assignment expression that will initialize the
8017          field to its value. There are strict rules on static
8018          initializers (8.5). FIXME */
8019       if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
8020         stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
8021       java_method_add_stmt (mdecl, stmt);
8022     }
8023
8024   end_artificial_method_body (mdecl);
8025
8026   /* Now we want to place <clinit> as the last method (because we need
8027      it at least for interface so that it doesn't interfere with the
8028      dispatch table based lookup. */
8029   if (TREE_CHAIN (TYPE_METHODS (class_type)))
8030     {
8031       current = TREE_CHAIN (TYPE_METHODS (class_type));
8032       TYPE_METHODS (class_type) = current;
8033
8034       while (TREE_CHAIN (current))
8035         current = TREE_CHAIN (current);
8036
8037       TREE_CHAIN (current) = mdecl;
8038       TREE_CHAIN (mdecl) = NULL_TREE;
8039     }
8040
8041   return mdecl;
8042 }
8043
8044 /* Analyzes a method body and look for something that isn't a
8045    MODIFY_EXPR with a constant value.  */
8046
8047 static int
8048 analyze_clinit_body (tree this_class, tree bbody)
8049 {
8050   while (bbody)
8051     switch (TREE_CODE (bbody))
8052       {
8053       case BLOCK:
8054         bbody = BLOCK_EXPR_BODY (bbody);
8055         break;
8056
8057       case EXPR_WITH_FILE_LOCATION:
8058         bbody = EXPR_WFL_NODE (bbody);
8059         break;
8060
8061       case COMPOUND_EXPR:
8062         if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
8063           return 1;
8064         bbody = TREE_OPERAND (bbody, 1);
8065         break;
8066
8067       case MODIFY_EXPR:
8068         /* If we're generating to class file and we're dealing with an
8069            array initialization, we return 1 to keep <clinit> */
8070         if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
8071             && flag_emit_class_files)
8072           return 1;
8073
8074         /* There are a few cases where we're required to keep
8075            <clinit>:
8076            - If this is an assignment whose operand is not constant,
8077            - If this is an assignment to a non-initialized field,
8078            - If this field is not a member of the current class.
8079         */
8080         return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8081                 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8082                 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8083
8084       default:
8085         return 1;
8086       }
8087   return 0;
8088 }
8089
8090
8091 /* See whether we could get rid of <clinit>. Criteria are: all static
8092    final fields have constant initial values and the body of <clinit>
8093    is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8094
8095 static int
8096 maybe_yank_clinit (tree mdecl)
8097 {
8098   tree type, current;
8099   tree fbody, bbody;
8100
8101   if (!DECL_CLINIT_P (mdecl))
8102     return 0;
8103
8104   /* If the body isn't empty, then we keep <clinit>. Note that if
8105      we're emitting classfiles, this isn't enough not to rule it
8106      out. */
8107   fbody = DECL_FUNCTION_BODY (mdecl);
8108   bbody = BLOCK_EXPR_BODY (fbody);
8109   if (bbody && bbody != error_mark_node)
8110     bbody = BLOCK_EXPR_BODY (bbody);
8111   else
8112     return 0;
8113   if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8114     return 0;
8115
8116   type = DECL_CONTEXT (mdecl);
8117   current = TYPE_FIELDS (type);
8118
8119   for (current = (current ? TREE_CHAIN (current) : current);
8120        current; current = TREE_CHAIN (current))
8121     {
8122       tree f_init;
8123
8124       /* We're not interested in non-static fields.  */
8125       if (!FIELD_STATIC (current))
8126         continue;
8127
8128       /* Nor in fields without initializers. */
8129       f_init = DECL_INITIAL (current);
8130       if (f_init == NULL_TREE)
8131         continue;
8132
8133       /* Anything that isn't String or a basic type is ruled out -- or
8134          if we know how to deal with it (when doing things natively) we
8135          should generated an empty <clinit> so that SUID are computed
8136          correctly. */
8137       if (! JSTRING_TYPE_P (TREE_TYPE (current))
8138           && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8139         return 0;
8140
8141       if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8142         return 0;
8143     }
8144
8145   /* Now we analyze the method body and look for something that
8146      isn't a MODIFY_EXPR */
8147   if (!IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8148     return 0;
8149
8150   /* Get rid of <clinit> in the class' list of methods */
8151   if (TYPE_METHODS (type) == mdecl)
8152     TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8153   else
8154     for (current = TYPE_METHODS (type); current;
8155          current = TREE_CHAIN (current))
8156       if (TREE_CHAIN (current) == mdecl)
8157         {
8158           TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8159           break;
8160         }
8161
8162   return 1;
8163 }
8164
8165 /* Install the argument from MDECL. Suitable to completion and
8166    expansion of mdecl's body.  */
8167
8168 void
8169 start_complete_expand_method (tree mdecl)
8170 {
8171   tree tem;
8172
8173   pushlevel (1);                /* Prepare for a parameter push */
8174   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8175   DECL_ARGUMENTS (mdecl) = tem;
8176
8177   for (; tem; tem = TREE_CHAIN (tem))
8178     {
8179       /* TREE_CHAIN (tem) will change after pushdecl. */
8180       tree next = TREE_CHAIN (tem);
8181       tree type = TREE_TYPE (tem);
8182       if (targetm.calls.promote_prototypes (type)
8183           && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8184           && INTEGRAL_TYPE_P (type))
8185         type = integer_type_node;
8186       DECL_ARG_TYPE (tem) = type;
8187       layout_decl (tem, 0);
8188       pushdecl (tem);
8189       /* Re-install the next so that the list is kept and the loop
8190          advances. */
8191       TREE_CHAIN (tem) = next;
8192     }
8193   pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8194   input_location = DECL_SOURCE_LOCATION (mdecl);
8195   build_result_decl (mdecl);
8196 }
8197
8198
8199 /* Complete and expand a method.  */
8200
8201 static void
8202 java_complete_expand_method (tree mdecl)
8203 {
8204   tree fbody, block_body, exception_copy;
8205
8206   current_function_decl = mdecl;
8207   /* Fix constructors before expanding them */
8208   if (DECL_CONSTRUCTOR_P (mdecl))
8209     fix_constructors (mdecl);
8210
8211   /* Expand functions that have a body */
8212   if (!DECL_FUNCTION_BODY (mdecl))
8213     return;
8214
8215   fbody = DECL_FUNCTION_BODY (mdecl);
8216   block_body = BLOCK_EXPR_BODY (fbody);
8217   exception_copy = NULL_TREE;
8218
8219   current_function_decl = mdecl;
8220
8221   if (! quiet_flag)
8222     fprintf (stderr, " [%s.",
8223              lang_printable_name (DECL_CONTEXT (mdecl), 0));
8224   announce_function (mdecl);
8225   if (! quiet_flag)
8226     fprintf (stderr, "]");
8227
8228   /* Prepare the function for tree completion */
8229   start_complete_expand_method (mdecl);
8230
8231   /* Install the current this */
8232   current_this = (!METHOD_STATIC (mdecl) ?
8233                   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8234
8235   /* Purge the `throws' list of unchecked exceptions (we save a copy
8236      of the list and re-install it later.) */
8237   exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8238   purge_unchecked_exceptions (mdecl);
8239
8240   /* Install exceptions thrown with `throws' */
8241   PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8242
8243   if (block_body != NULL_TREE)
8244     {
8245       block_body = java_complete_tree (block_body);
8246
8247       /* Before we check initialization, attached all class initialization
8248          variable to the block_body */
8249       htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8250                      attach_init_test_initialization_flags, block_body);
8251
8252       if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8253         {
8254           check_for_initialization (block_body, mdecl);
8255
8256           /* Go through all the flags marking the initialization of
8257              static variables and see whether they're definitively
8258              assigned, in which case the type is remembered as
8259              definitively initialized in MDECL. */
8260           if (STATIC_CLASS_INIT_OPT_P ())
8261             {
8262               /* Always register the context as properly initialized in
8263                  MDECL. This used with caution helps removing extra
8264                  initialization of self. */
8265               if (METHOD_STATIC (mdecl))
8266                 {
8267                   *(htab_find_slot
8268                     (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8269                      DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8270                 }
8271             }
8272         }
8273       ctxp->explicit_constructor_p = 0;
8274     }
8275
8276   BLOCK_EXPR_BODY (fbody) = block_body;
8277
8278   /* If we saw a return but couldn't evaluate it properly, we'll have
8279      an error_mark_node here. */
8280   if (block_body != error_mark_node
8281       && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8282       && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8283       && !flag_emit_xref)
8284     missing_return_error (current_function_decl);
8285
8286   /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8287   maybe_yank_clinit (mdecl);
8288
8289   /* Pop the current level, with special measures if we found errors. */
8290   if (java_error_count)
8291     pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8292   poplevel (1, 0, 1);
8293
8294   /* Pop the exceptions and sanity check */
8295   POP_EXCEPTIONS();
8296   if (currently_caught_type_list)
8297     abort ();
8298
8299   /* Restore the copy of the list of exceptions if emitting xrefs. */
8300   DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8301 }
8302
8303 /* For with each class for which there's code to generate. */
8304
8305 static void
8306 java_expand_method_bodies (tree class)
8307 {
8308   tree decl;
8309   for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8310     {
8311       tree block;
8312
8313       if (! DECL_FUNCTION_BODY (decl))
8314         continue;
8315
8316       current_function_decl = decl;
8317
8318       block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8319
8320       /* Save the function body for gimplify and inlining.  */
8321       DECL_SAVED_TREE (decl) = block;
8322
8323       /* It's time to assign the variable flagging static class
8324          initialization based on which classes invoked static methods
8325          are definitely initializing. This should be flagged. */
8326       if (STATIC_CLASS_INIT_OPT_P ())
8327         {
8328           tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8329           for (; list != NULL_TREE;  list = TREE_CHAIN (list))
8330             {
8331               /* Executed for each statement calling a static function.
8332                  LIST is a TREE_LIST whose PURPOSE is the called function
8333                  and VALUE is a compound whose second operand can be patched
8334                  with static class initialization flag assignments.  */
8335
8336               tree called_method = TREE_PURPOSE (list);
8337               tree compound = TREE_VALUE (list);
8338               tree assignment_compound_list
8339                 = build_tree_list (called_method, NULL);
8340
8341               /* For each class definitely initialized in
8342                  CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8343                  assignment to the class initialization flag. */
8344               htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8345                              emit_test_initialization,
8346                              assignment_compound_list);
8347
8348               if (TREE_VALUE (assignment_compound_list))
8349                 TREE_OPERAND (compound, 1)
8350                   = TREE_VALUE (assignment_compound_list);
8351             }
8352         }
8353
8354       /* Expand the function body.  */
8355       source_end_java_method ();
8356     }
8357 }
8358
8359 \f
8360
8361 /* This section of the code deals with accessing enclosing context
8362    fields either directly by using the relevant access to this$<n> or
8363    by invoking an access method crafted for that purpose.  */
8364
8365 /* Build the necessary access from an inner class to an outer
8366    class. This routine could be optimized to cache previous result
8367    (decl, current_class and returned access).  When an access method
8368    needs to be generated, it always takes the form of a read. It might
8369    be later turned into a write by calling outer_field_access_fix.  */
8370
8371 static tree
8372 build_outer_field_access (tree id, tree decl)
8373 {
8374   tree access = NULL_TREE;
8375   tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8376   tree decl_ctx = DECL_CONTEXT (decl);
8377
8378   /* If the immediate enclosing context of the current class is the
8379      field decl's class or inherits from it; build the access as
8380      `this$<n>.<field>'. Note that we will break the `private' barrier
8381      if we're not emitting bytecodes. */
8382   if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8383       && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8384     {
8385       tree thisn = build_current_thisn (current_class);
8386       access = make_qualified_primary (build_wfl_node (thisn),
8387                                        id, EXPR_WFL_LINECOL (id));
8388     }
8389   /* Otherwise, generate access methods to outer this and access the
8390      field (either using an access method or by direct access.) */
8391   else
8392     {
8393       int lc = EXPR_WFL_LINECOL (id);
8394
8395       /* Now we chain the required number of calls to the access$0 to
8396          get a hold to the enclosing instance we need, and then we
8397          build the field access. */
8398       access = build_access_to_thisn (current_class, decl_ctx, lc);
8399
8400       /* If the field is private and we're generating bytecode, then
8401          we generate an access method */
8402       if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8403         {
8404           tree name = build_outer_field_access_methods (decl);
8405           access = build_outer_field_access_expr (lc, decl_ctx,
8406                                                   name, access, NULL_TREE);
8407         }
8408       /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8409          Once again we break the `private' access rule from a foreign
8410          class. */
8411       else
8412         access = make_qualified_primary (access, id, lc);
8413     }
8414   return resolve_expression_name (access, NULL);
8415 }
8416
8417 /* Return a nonzero value if NODE describes an outer field inner
8418    access.  */
8419
8420 static int
8421 outer_field_access_p (tree type, tree decl)
8422 {
8423   if (!INNER_CLASS_TYPE_P (type)
8424       || TREE_CODE (decl) != FIELD_DECL
8425       || DECL_CONTEXT (decl) == type)
8426     return 0;
8427
8428   /* If the inner class extends the declaration context of the field
8429      we're trying to access, then this isn't an outer field access */
8430   if (inherits_from_p (type, DECL_CONTEXT (decl)))
8431     return 0;
8432
8433   for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8434        type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8435     {
8436       if (type == DECL_CONTEXT (decl))
8437         return 1;
8438
8439       if (!DECL_CONTEXT (TYPE_NAME (type)))
8440         {
8441           /* Before we give up, see whether the field is inherited from
8442              the enclosing context we're considering. */
8443           if (inherits_from_p (type, DECL_CONTEXT (decl)))
8444             return 1;
8445           break;
8446         }
8447     }
8448
8449   return 0;
8450 }
8451
8452 /* Return a nonzero value if NODE represents an outer field inner
8453    access that was been already expanded. As a side effect, it returns
8454    the name of the field being accessed and the argument passed to the
8455    access function, suitable for a regeneration of the access method
8456    call if necessary. */
8457
8458 static int
8459 outer_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8460                                tree *arg)
8461 {
8462   int identified = 0;
8463
8464   if (TREE_CODE (node) != CALL_EXPR)
8465     return 0;
8466
8467   /* Well, gcj generates slightly different tree nodes when compiling
8468      to native or bytecodes. It's the case for function calls. */
8469
8470   if (flag_emit_class_files
8471       && TREE_CODE (node) == CALL_EXPR
8472       && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8473     identified = 1;
8474   else if (!flag_emit_class_files)
8475     {
8476       node = TREE_OPERAND (node, 0);
8477
8478       if (node && TREE_OPERAND (node, 0)
8479           && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8480         {
8481           node = TREE_OPERAND (node, 0);
8482           if (TREE_OPERAND (node, 0)
8483               && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8484               && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8485                   (DECL_NAME (TREE_OPERAND (node, 0)))))
8486             identified = 1;
8487         }
8488     }
8489
8490   if (identified && name && arg_type && arg)
8491     {
8492       tree argument = TREE_OPERAND (node, 1);
8493       *name = DECL_NAME (TREE_OPERAND (node, 0));
8494       *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8495       *arg = TREE_VALUE (argument);
8496     }
8497   return identified;
8498 }
8499
8500 /* Detect in NODE an outer field read access from an inner class and
8501    transform it into a write with RHS as an argument. This function is
8502    called from the java_complete_lhs when an assignment to a LHS can
8503    be identified. */
8504
8505 static tree
8506 outer_field_access_fix (tree wfl, tree node, tree rhs)
8507 {
8508   tree name, arg_type, arg;
8509
8510   if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8511     {
8512       node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8513                                             arg_type, name, arg, rhs);
8514       return java_complete_tree (node);
8515     }
8516   return NULL_TREE;
8517 }
8518
8519 /* Construct the expression that calls an access method:
8520      <type>.access$<n>(<arg1> [, <arg2>]);
8521
8522    ARG2 can be NULL and will be omitted in that case. It will denote a
8523    read access.  */
8524
8525 static tree
8526 build_outer_field_access_expr (int lc, tree type, tree access_method_name,
8527                                tree arg1, tree arg2)
8528 {
8529   tree args, cn, access;
8530
8531   args = arg1 ? arg1 :
8532     build_wfl_node (build_current_thisn (current_class));
8533   args = build_tree_list (NULL_TREE, args);
8534
8535   if (arg2)
8536     args = tree_cons (NULL_TREE, arg2, args);
8537
8538   access = build_method_invocation (build_wfl_node (access_method_name), args);
8539   cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8540   return make_qualified_primary (cn, access, lc);
8541 }
8542
8543 static tree
8544 build_new_access_id (void)
8545 {
8546   static int access_n_counter = 1;
8547   char buffer [128];
8548
8549   sprintf (buffer, "access$%d", access_n_counter++);
8550   return get_identifier (buffer);
8551 }
8552
8553 /* Create the static access functions for the outer field DECL. We define a
8554    read:
8555      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8556        return inst$.field;
8557      }
8558    and a write access:
8559      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8560                                      TREE_TYPE (<field>) value$) {
8561        return inst$.field = value$;
8562      }
8563    We should have a usage flags on the DECL so we can lazily turn the ones
8564    we're using for code generation. FIXME.
8565 */
8566
8567 static tree
8568 build_outer_field_access_methods (tree decl)
8569 {
8570   tree id, args, stmt, mdecl;
8571
8572   if (FIELD_INNER_ACCESS_P (decl))
8573     return FIELD_INNER_ACCESS (decl);
8574
8575   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8576
8577   /* Create the identifier and a function named after it. */
8578   id = build_new_access_id ();
8579
8580   /* The identifier is marked as bearing the name of a generated write
8581      access function for outer field accessed from inner classes. */
8582   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8583
8584   /* Create the read access */
8585   args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8586   TREE_CHAIN (args) = end_params_node;
8587   stmt = make_qualified_primary (build_wfl_node (inst_id),
8588                                  build_wfl_node (DECL_NAME (decl)), 0);
8589   stmt = build_return (0, stmt);
8590   mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8591                                            TREE_TYPE (decl), id, args, stmt);
8592   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8593
8594   /* Create the write access method. No write access for final variable */
8595   if (!FIELD_FINAL (decl))
8596     {
8597       args = build_tree_list (inst_id,
8598                               build_pointer_type (DECL_CONTEXT (decl)));
8599       TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8600       TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8601       stmt = make_qualified_primary (build_wfl_node (inst_id),
8602                                      build_wfl_node (DECL_NAME (decl)), 0);
8603       stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8604                                                 build_wfl_node (wpv_id)));
8605       mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8606                                                TREE_TYPE (decl), id,
8607                                                args, stmt);
8608     }
8609   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8610
8611   /* Return the access name */
8612   return FIELD_INNER_ACCESS (decl) = id;
8613 }
8614
8615 /* Build an field access method NAME.  */
8616
8617 static tree
8618 build_outer_field_access_method (tree class, tree type, tree name,
8619                                  tree args, tree body)
8620 {
8621   tree saved_current_function_decl, mdecl;
8622
8623   /* Create the method */
8624   mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8625   fix_method_argument_names (args, mdecl);
8626   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8627
8628   /* Attach the method body. */
8629   saved_current_function_decl = current_function_decl;
8630   start_artificial_method_body (mdecl);
8631   java_method_add_stmt (mdecl, body);
8632   end_artificial_method_body (mdecl);
8633   current_function_decl = saved_current_function_decl;
8634
8635   return mdecl;
8636 }
8637
8638 \f
8639 /* This section deals with building access function necessary for
8640    certain kinds of method invocation from inner classes.  */
8641
8642 static tree
8643 build_outer_method_access_method (tree decl)
8644 {
8645   tree saved_current_function_decl, mdecl;
8646   tree args = NULL_TREE, call_args = NULL_TREE;
8647   tree carg, id, body, class;
8648   char buffer [80];
8649   int parm_id_count = 0;
8650
8651   /* Test this abort with an access to a private field */
8652   if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8653     abort ();
8654
8655   /* Check the cache first */
8656   if (DECL_FUNCTION_INNER_ACCESS (decl))
8657     return DECL_FUNCTION_INNER_ACCESS (decl);
8658
8659   class = DECL_CONTEXT (decl);
8660
8661   /* Obtain an access identifier and mark it */
8662   id = build_new_access_id ();
8663   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8664
8665   carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8666   /* Create the arguments, as much as the original */
8667   for (; carg && carg != end_params_node;
8668        carg = TREE_CHAIN (carg))
8669     {
8670       sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8671       args = chainon (args, build_tree_list (get_identifier (buffer),
8672                                              TREE_VALUE (carg)));
8673     }
8674   args = chainon (args, end_params_node);
8675
8676   /* Create the method */
8677   mdecl = create_artificial_method (class, ACC_STATIC,
8678                                     TREE_TYPE (TREE_TYPE (decl)), id, args);
8679   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8680   /* There is a potential bug here. We should be able to use
8681      fix_method_argument_names, but then arg names get mixed up and
8682      eventually a constructor will have its this$0 altered and the
8683      outer context won't be assignment properly. The testcase is
8684      stub.java FIXME */
8685   TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8686
8687   /* Attach the method body. */
8688   saved_current_function_decl = current_function_decl;
8689   start_artificial_method_body (mdecl);
8690
8691   /* The actual method invocation uses the same args. When invoking a
8692      static methods that way, we don't want to skip the first
8693      argument. */
8694   carg = args;
8695   if (!METHOD_STATIC (decl))
8696     carg = TREE_CHAIN (carg);
8697   for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8698     call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8699                            call_args);
8700
8701   body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8702                                   call_args);
8703   if (!METHOD_STATIC (decl))
8704     body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8705                                    body, 0);
8706   if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8707     body = build_return (0, body);
8708   java_method_add_stmt (mdecl,body);
8709   end_artificial_method_body (mdecl);
8710   current_function_decl = saved_current_function_decl;
8711
8712   /* Back tag the access function so it know what it accesses */
8713   DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8714
8715   /* Tag the current method so it knows it has an access generated */
8716   return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8717 }
8718
8719 \f
8720 /* This section of the code deals with building expressions to access
8721    the enclosing instance of an inner class. The enclosing instance is
8722    kept in a generated field called this$<n>, with <n> being the
8723    inner class nesting level (starting from 0.)  */
8724
8725 /* Build an access to a given this$<n>, always chaining access call to
8726    others. Access methods to this$<n> are build on the fly if
8727    necessary. This CAN'T be used to solely access this$<n-1> from
8728    this$<n> (which alway yield to special cases and optimization, see
8729    for example build_outer_field_access).  */
8730
8731 static tree
8732 build_access_to_thisn (tree from, tree to, int lc)
8733 {
8734   tree access = NULL_TREE;
8735
8736   while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8737     {
8738       if (!access)
8739         {
8740           access = build_current_thisn (from);
8741           access = build_wfl_node (access);
8742         }
8743       else
8744         {
8745           tree access0_wfl, cn;
8746
8747           maybe_build_thisn_access_method (from);
8748           access0_wfl = build_wfl_node (access0_identifier_node);
8749           cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8750           EXPR_WFL_LINECOL (access0_wfl) = lc;
8751           access = build_tree_list (NULL_TREE, access);
8752           access = build_method_invocation (access0_wfl, access);
8753           access = make_qualified_primary (cn, access, lc);
8754         }
8755
8756       /* If FROM isn't an inner class, that's fine, we've done enough.
8757          What we're looking for can be accessed from there.  */
8758       from = DECL_CONTEXT (TYPE_NAME (from));
8759       if (!from)
8760         break;
8761       from = TREE_TYPE (from);
8762     }
8763   return access;
8764 }
8765
8766 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8767    is returned if nothing needs to be generated. Otherwise, the method
8768    generated and a method decl is returned.
8769
8770    NOTE: These generated methods should be declared in a class file
8771    attribute so that they can't be referred to directly.  */
8772
8773 static tree
8774 maybe_build_thisn_access_method (tree type)
8775 {
8776   tree mdecl, args, stmt, rtype;
8777   tree saved_current_function_decl;
8778
8779   /* If TYPE is a top-level class, no access method is required.
8780      If there already is such an access method, bail out. */
8781   if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8782     return NULL_TREE;
8783
8784   /* We generate the method. The method looks like:
8785      static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8786   */
8787   args = build_tree_list (inst_id, build_pointer_type (type));
8788   TREE_CHAIN (args) = end_params_node;
8789   rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8790   mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8791                                     access0_identifier_node, args);
8792   fix_method_argument_names (args, mdecl);
8793   layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8794   stmt = build_current_thisn (type);
8795   stmt = make_qualified_primary (build_wfl_node (inst_id),
8796                                  build_wfl_node (stmt), 0);
8797   stmt = build_return (0, stmt);
8798
8799   saved_current_function_decl = current_function_decl;
8800   start_artificial_method_body (mdecl);
8801   java_method_add_stmt (mdecl, stmt);
8802   end_artificial_method_body (mdecl);
8803   current_function_decl = saved_current_function_decl;
8804
8805   CLASS_ACCESS0_GENERATED_P (type) = 1;
8806
8807   return mdecl;
8808 }
8809
8810 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8811    the first level of innerclassing. this$1 for the next one, etc...
8812    This function can be invoked with TYPE to NULL, available and then
8813    has to count the parser context.  */
8814
8815 static GTY(()) tree saved_thisn;
8816 static GTY(()) tree saved_type;
8817
8818 static tree
8819 build_current_thisn (tree type)
8820 {
8821   static int saved_i = -1;
8822   static int saved_type_i = 0;
8823   tree decl;
8824   char buffer [24];
8825   int i = 0;
8826
8827   if (type)
8828     {
8829       if (type == saved_type)
8830         i = saved_type_i;
8831       else
8832         {
8833           for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8834                decl; decl = DECL_CONTEXT (decl), i++)
8835             ;
8836
8837           saved_type = type;
8838           saved_type_i = i;
8839         }
8840     }
8841   else
8842     i = list_length (GET_CPC_LIST ())-2;
8843
8844   if (i == saved_i)
8845     return saved_thisn;
8846
8847   sprintf (buffer, "this$%d", i);
8848   saved_i = i;
8849   saved_thisn = get_identifier (buffer);
8850   return saved_thisn;
8851 }
8852
8853 /* Return the assignment to the hidden enclosing context `this$<n>'
8854    by the second incoming parameter to the innerclass constructor. The
8855    form used is `this.this$<n> = this$<n>;'.  */
8856
8857 static tree
8858 build_thisn_assign (void)
8859 {
8860   if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8861     {
8862       tree thisn = build_current_thisn (current_class);
8863       tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8864                                          build_wfl_node (thisn), 0);
8865       tree rhs = build_wfl_node (thisn);
8866 #ifdef USE_MAPPED_LOCATION
8867       SET_EXPR_LOCATION (lhs, input_location);
8868 #else
8869       EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8870 #endif
8871       return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8872     }
8873   return NULL_TREE;
8874 }
8875
8876 \f
8877 /* Building the synthetic `class$' used to implement the `.class' 1.1
8878    extension for non primitive types. This method looks like:
8879
8880     static Class class$(String type) throws NoClassDefFoundError
8881     {
8882       try {return (java.lang.Class.forName (String));}
8883       catch (ClassNotFoundException e) {
8884         throw new NoClassDefFoundError(e.getMessage());}
8885     } */
8886
8887 static GTY(()) tree get_message_wfl;
8888 static GTY(()) tree type_parm_wfl;
8889
8890 static tree
8891 build_dot_class_method (tree class)
8892 {
8893 #define BWF(S) build_wfl_node (get_identifier ((S)))
8894 #ifdef USE_MAPPED_LOCATION
8895 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8896 #else
8897 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8898 #endif
8899   tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8900   tree stmt, throw_stmt;
8901
8902   if (!get_message_wfl)
8903     {
8904       get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8905       type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8906     }
8907
8908   /* Build the arguments */
8909   args = build_tree_list (get_identifier ("type$"),
8910                           build_pointer_type (string_type_node));
8911   TREE_CHAIN (args) = end_params_node;
8912
8913   /* Build the qualified name java.lang.Class.forName */
8914   tmp = MQN (MQN (MQN (BWF ("java"),
8915                        BWF ("lang")), BWF ("Class")), BWF ("forName"));
8916
8917   /* Create the "class$" function */
8918   mdecl = create_artificial_method (class, ACC_STATIC,
8919                                     build_pointer_type (class_type_node),
8920                                     classdollar_identifier_node, args);
8921   qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8922                    BWF ("NoClassDefFoundError"));
8923   DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8924   register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8925   JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8926     &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8927
8928   /* We start by building the try block. We need to build:
8929        return (java.lang.Class.forName (type)); */
8930   stmt = build_method_invocation (tmp,
8931                                   build_tree_list (NULL_TREE, type_parm_wfl));
8932   stmt = build_return (0, stmt);
8933
8934   /* Now onto the catch block. We start by building the expression
8935      throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8936 #ifdef USE_MAPPED_LOCATION
8937   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8938                                     get_message_wfl, UNKNOWN_LOCATION);
8939 #else
8940   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8941                                     get_message_wfl, 0);
8942 #endif
8943   throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8944
8945   /* Build new NoClassDefFoundError (_.getMessage) */
8946   throw_stmt = build_new_invocation
8947     (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8948      build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8949
8950   /* Build the throw, (it's too early to use BUILD_THROW) */
8951   throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8952
8953   /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8954   qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8955                    BWF ("ClassNotFoundException"));
8956   stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8957
8958   fix_method_argument_names (args, mdecl);
8959   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8960   saved_current_function_decl = current_function_decl;
8961   start_artificial_method_body (mdecl);
8962   java_method_add_stmt (mdecl, stmt);
8963   end_artificial_method_body (mdecl);
8964   current_function_decl = saved_current_function_decl;
8965   TYPE_DOT_CLASS (class) = mdecl;
8966
8967   return mdecl;
8968 }
8969
8970 static tree
8971 build_dot_class_method_invocation (tree this_class, tree type)
8972 {
8973   tree dot_class_method = TYPE_DOT_CLASS (this_class);
8974   tree sig_id, s, t;
8975
8976   if (TYPE_ARRAY_P (type))
8977     sig_id = build_java_signature (type);
8978   else
8979     sig_id = DECL_NAME (TYPE_NAME (type));
8980
8981   /* Ensure that the proper name separator is used */
8982   sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8983                                IDENTIFIER_LENGTH (sig_id));
8984
8985   s = build_string (IDENTIFIER_LENGTH (sig_id),
8986                     IDENTIFIER_POINTER (sig_id));
8987   t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
8988                                build_tree_list (NULL_TREE, s));
8989   if (DECL_CONTEXT (dot_class_method) != this_class)
8990     {
8991       tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
8992       t = make_qualified_primary (build_wfl_node (class_name), t, 0);
8993     }
8994   return t;
8995 }
8996
8997 /* This section of the code deals with constructor.  */
8998
8999 /* Craft a body for default constructor. Patch existing constructor
9000    bodies with call to super() and field initialization statements if
9001    necessary.  */
9002
9003 static void
9004 fix_constructors (tree mdecl)
9005 {
9006   tree iii;                     /* Instance Initializer Invocation */
9007   tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9008   tree thisn_assign, compound = NULL_TREE;
9009   tree class_type = DECL_CONTEXT (mdecl);
9010
9011   if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9012     return;
9013   DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9014
9015   if (!*bodyp)
9016     {
9017       /* It is an error for the compiler to generate a default
9018          constructor if the superclass doesn't have a constructor that
9019          takes no argument, or the same args for an anonymous class */
9020       if (verify_constructor_super (mdecl))
9021         {
9022           tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9023           tree save = DECL_NAME (mdecl);
9024           const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9025           DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9026           parse_error_context
9027             (lookup_cl (TYPE_NAME (class_type)),
9028              "No constructor matching %qs found in class %qs",
9029              lang_printable_name (mdecl, 2), n);
9030           DECL_NAME (mdecl) = save;
9031         }
9032
9033       /* The constructor body must be crafted by hand. It's the
9034          constructor we defined when we realize we didn't have the
9035          CLASSNAME() constructor */
9036       start_artificial_method_body (mdecl);
9037
9038       /* Insert an assignment to the this$<n> hidden field, if
9039          necessary */
9040       if ((thisn_assign = build_thisn_assign ()))
9041         java_method_add_stmt (mdecl, thisn_assign);
9042
9043       /* We don't generate a super constructor invocation if we're
9044          compiling java.lang.Object. build_super_invocation takes care
9045          of that. */
9046       java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9047
9048       /* FIXME */
9049       if ((iii = build_instinit_invocation (class_type)))
9050         java_method_add_stmt (mdecl, iii);
9051
9052       end_artificial_method_body (mdecl);
9053     }
9054   /* Search for an explicit constructor invocation */
9055   else
9056     {
9057       int found = 0;
9058       int invokes_this = 0;
9059       tree main_block = BLOCK_EXPR_BODY (*bodyp);
9060
9061       while (*bodyp)
9062         {
9063           tree body = *bodyp;
9064           switch (TREE_CODE (body))
9065             {
9066             case CALL_EXPR:
9067               found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9068               if (CALL_THIS_CONSTRUCTOR_P (body))
9069                 invokes_this = 1;
9070               break;
9071             case COMPOUND_EXPR:
9072             case EXPR_WITH_FILE_LOCATION:
9073               bodyp = &TREE_OPERAND (body, 0);
9074               continue;
9075             case BLOCK:
9076               bodyp = &BLOCK_EXPR_BODY (body);
9077               continue;
9078             default:
9079               break;
9080             }
9081           break;
9082         }
9083
9084       /* Generate the assignment to this$<n>, if necessary */
9085       if ((thisn_assign = build_thisn_assign ()))
9086         compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9087
9088       /* The constructor is missing an invocation of super() */
9089       if (!found)
9090         compound = add_stmt_to_compound (compound, NULL_TREE,
9091                                          build_super_invocation (mdecl));
9092       /* Explicit super() invocation should take place before the
9093          instance initializer blocks. */
9094       else
9095         {
9096           compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9097           *bodyp = build_java_empty_stmt ();
9098         }
9099
9100       DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9101
9102       /* Insert the instance initializer block right after. */
9103       if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9104         compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9105
9106       /* Fix the constructor main block if we're adding extra stmts */
9107       if (compound)
9108         {
9109           compound = add_stmt_to_compound (compound, NULL_TREE,
9110                                            BLOCK_EXPR_BODY (main_block));
9111           BLOCK_EXPR_BODY (main_block) = compound;
9112         }
9113     }
9114 }
9115
9116 /* Browse constructors in the super class, searching for a constructor
9117    that doesn't take any argument. Return 0 if one is found, 1
9118    otherwise.  If the current class is an anonymous inner class, look
9119    for something that has the same signature. */
9120
9121 static int
9122 verify_constructor_super (tree mdecl)
9123 {
9124   tree class = CLASSTYPE_SUPER (current_class);
9125   int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9126   tree sdecl;
9127
9128   if (!class)
9129     return 0;
9130
9131   if (ANONYMOUS_CLASS_P (current_class))
9132     {
9133       tree mdecl_arg_type;
9134       SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9135       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9136         if (DECL_CONSTRUCTOR_P (sdecl))
9137           {
9138             tree m_arg_type;
9139             tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9140             if (super_inner)
9141               arg_type = TREE_CHAIN (arg_type);
9142             for (m_arg_type = mdecl_arg_type;
9143                  (arg_type != end_params_node
9144                   && m_arg_type != end_params_node);
9145                  arg_type = TREE_CHAIN (arg_type),
9146                    m_arg_type = TREE_CHAIN (m_arg_type))
9147               if (!valid_method_invocation_conversion_p
9148                      (TREE_VALUE (arg_type),
9149                       TREE_VALUE (m_arg_type)))
9150                 break;
9151
9152             if (arg_type == end_params_node && m_arg_type == end_params_node)
9153               return 0;
9154           }
9155     }
9156   else
9157     {
9158       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9159         {
9160           tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9161           if (super_inner)
9162             arg = TREE_CHAIN (arg);
9163           if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9164             return 0;
9165         }
9166     }
9167   return 1;
9168 }
9169
9170 /* Generate code for all context remembered for code generation.  */
9171
9172 static GTY(()) tree reversed_class_list;
9173 void
9174 java_expand_classes (void)
9175 {
9176   int save_error_count = 0;
9177   static struct parser_ctxt *cur_ctxp = NULL;
9178   location_t save_location;
9179
9180   java_parse_abort_on_error ();
9181   if (!(ctxp = ctxp_for_generation))
9182     return;
9183   java_layout_classes ();
9184   java_parse_abort_on_error ();
9185   save_location = input_location;
9186
9187   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9188     {
9189       tree current;
9190       for (current = cur_ctxp->class_list; 
9191            current; 
9192            current = TREE_CHAIN (current))
9193         gen_indirect_dispatch_tables (TREE_TYPE (current));
9194     }
9195   
9196   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9197     {
9198       ctxp = cur_ctxp;
9199       input_location = ctxp->file_start_location;
9200       lang_init_source (2);            /* Error msgs have method prototypes */
9201       java_complete_expand_classes (); /* Complete and expand classes */
9202       java_parse_abort_on_error ();
9203     }
9204   input_location = save_location;
9205
9206   /* Find anonymous classes and expand their constructor. This extra pass is
9207      necessary because the constructor itself is only generated when the
9208      method in which it is defined is expanded. */
9209   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9210     {
9211       tree current;
9212       ctxp = cur_ctxp;
9213       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9214         {
9215           output_class = current_class = TREE_TYPE (current);
9216           if (ANONYMOUS_CLASS_P (current_class))
9217             {
9218               tree d;
9219               for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9220                 {
9221                   if (DECL_CONSTRUCTOR_P (d))
9222                     {
9223                       java_complete_expand_method (d);
9224                       break;    /* There is only one constructor. */
9225                     }
9226                 }
9227             }
9228         }
9229     }
9230
9231   /* Expanding the constructors of anonymous classes generates access
9232      methods.  Scan all the methods looking for null DECL_RESULTs --
9233      this will be the case if a method hasn't been expanded.  */
9234   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9235     {
9236       tree current;
9237       ctxp = cur_ctxp;
9238       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9239         {
9240           tree d;
9241           output_class = current_class = TREE_TYPE (current);
9242           for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9243             {
9244               if (DECL_RESULT (d) == NULL_TREE)
9245                 java_complete_expand_method (d);
9246             }
9247         }
9248     }
9249
9250   /* ???  Instead of all this we could iterate around the list of
9251      classes until there were no more un-expanded methods.  It would
9252      take a little longer -- one pass over the whole list of methods
9253      -- but it would be simpler.  Like this:  */
9254 #if 0
9255     {
9256       int something_changed;
9257     
9258       do
9259         {
9260           something_changed = 0;
9261           for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9262             {
9263               tree current;
9264               ctxp = cur_ctxp;
9265               for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9266                 {
9267                   tree d;
9268                   output_class = current_class = TREE_TYPE (current);
9269                   for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9270                     {
9271                       if (DECL_RESULT (d) == NULL_TREE)
9272                         {
9273                           something_changed = 1;
9274                           java_complete_expand_method (d);
9275                         }
9276                     }
9277                 }
9278             }
9279         }
9280       while (something_changed);
9281     }
9282 #endif
9283
9284   /* If we've found error at that stage, don't try to generate
9285      anything, unless we're emitting xrefs or checking the syntax only
9286      (but not using -fsyntax-only for the purpose of generating
9287      bytecode. */
9288   if (java_error_count && !flag_emit_xref
9289       && (!flag_syntax_only && !flag_emit_class_files))
9290     return;
9291
9292   /* Now things are stable, go for generation of the class data. */
9293
9294   /* We pessimistically marked all methods and fields external until
9295      we knew what set of classes we were planning to compile.  Now mark
9296      those that will be generated locally as not external.  */
9297   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9298     {
9299       tree current;
9300       ctxp = cur_ctxp;
9301       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9302         java_mark_class_local (TREE_TYPE (current));
9303     }
9304
9305   /* Compile the classes.  */
9306   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9307     {
9308       tree current;
9309       reversed_class_list = NULL;
9310
9311       ctxp = cur_ctxp;
9312
9313       /* We write out the classes in reverse order.  This ensures that
9314          inner classes are written before their containing classes,
9315          which is important for parallel builds.  Otherwise, the
9316          class file for the outer class may be found, but the class
9317          file for the inner class may not be present.  In that
9318          situation, the compiler cannot fall back to the original
9319          source, having already read the outer class, so we must
9320          prevent that situation.  */
9321       for (current = ctxp->class_list;
9322            current;
9323            current = TREE_CHAIN (current))
9324         reversed_class_list
9325           = tree_cons (NULL_TREE, current, reversed_class_list);
9326
9327       for (current = reversed_class_list;
9328            current;
9329            current = TREE_CHAIN (current))
9330         {
9331           output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9332           if (flag_emit_class_files)
9333             write_classfile (current_class);
9334           if (flag_emit_xref)
9335             expand_xref (current_class);
9336           else if (! flag_syntax_only)
9337             java_expand_method_bodies (current_class);
9338         }
9339     }
9340 }
9341
9342 void
9343 java_finish_classes (void)
9344 {
9345   static struct parser_ctxt *cur_ctxp = NULL;
9346   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9347     {
9348       tree current;
9349       ctxp = cur_ctxp;
9350       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9351         {
9352           output_class = current_class = TREE_TYPE (current);
9353           finish_class ();
9354         }
9355     }
9356 }
9357
9358 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9359    a tree list node containing RIGHT. Fore coming RIGHTs will be
9360    chained to this hook. LOCATION contains the location of the
9361    separating `.' operator.  */
9362
9363 static tree
9364 make_qualified_primary (tree primary, tree right, int location)
9365 {
9366   tree wfl;
9367
9368   if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9369     wfl = build_wfl_wrap (primary, location);
9370   else
9371     {
9372       wfl = primary;
9373       /* If wfl wasn't qualified, we build a first anchor */
9374       if (!EXPR_WFL_QUALIFICATION (wfl))
9375         EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9376     }
9377
9378   /* And chain them */
9379   EXPR_WFL_LINECOL (right) = location;
9380   chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9381   PRIMARY_P (wfl) =  1;
9382   return wfl;
9383 }
9384
9385 /* Simple merge of two name separated by a `.' */
9386
9387 static tree
9388 merge_qualified_name (tree left, tree right)
9389 {
9390   tree node;
9391   if (!left && !right)
9392     return NULL_TREE;
9393
9394   if (!left)
9395     return right;
9396
9397   if (!right)
9398     return left;
9399
9400   obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9401                 IDENTIFIER_LENGTH (left));
9402   obstack_1grow (&temporary_obstack, '.');
9403   obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9404                  IDENTIFIER_LENGTH (right));
9405   node =  get_identifier (obstack_base (&temporary_obstack));
9406   obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9407   QUALIFIED_P (node) = 1;
9408   return node;
9409 }
9410
9411 /* Merge the two parts of a qualified name into LEFT.  Set the
9412    location information of the resulting node to LOCATION, usually
9413    inherited from the location information of the `.' operator. */
9414
9415 static tree
9416 make_qualified_name (tree left, tree right,
9417 #ifdef USE_MAPPED_LOCATION
9418                      source_location location
9419 #else
9420                      int location
9421 #endif
9422                      )
9423 {
9424 #ifdef USE_COMPONENT_REF
9425   tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9426   SET_EXPR_LOCATION (node, location);
9427   return node;
9428 #else
9429   tree left_id = EXPR_WFL_NODE (left);
9430   tree right_id = EXPR_WFL_NODE (right);
9431   tree wfl, merge;
9432
9433   merge = merge_qualified_name (left_id, right_id);
9434
9435   /* Left wasn't qualified and is now qualified */
9436 #ifdef USE_MAPPED_LOCATION
9437   if (!QUALIFIED_P (left_id))
9438     {
9439       tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9440       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9441     }
9442
9443   wfl = build_expr_wfl (right_id, location);
9444 #else
9445   if (!QUALIFIED_P (left_id))
9446     {
9447       tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9448       EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9449       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9450     }
9451
9452   wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9453   EXPR_WFL_LINECOL (wfl) = location;
9454 #endif
9455   chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9456   EXPR_WFL_NODE (left) = merge;
9457   return left;
9458 #endif
9459 }
9460
9461 /* Extract the last identifier component of the qualified in WFL. The
9462    last identifier is removed from the linked list */
9463
9464 static tree
9465 cut_identifier_in_qualified (tree wfl)
9466 {
9467   tree q;
9468   tree previous = NULL_TREE;
9469   for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9470     if (!TREE_CHAIN (q))
9471       {
9472         if (!previous)
9473           /* Operating on a non qualified qualified WFL.  */
9474           abort ();
9475
9476         TREE_CHAIN (previous) = NULL_TREE;
9477         return TREE_PURPOSE (q);
9478       }
9479 }
9480
9481 /* Resolve the expression name NAME. Return its decl.  */
9482
9483 static tree
9484 resolve_expression_name (tree id, tree *orig)
9485 {
9486   tree name = EXPR_WFL_NODE (id);
9487   tree decl;
9488
9489   /* 6.5.5.1: Simple expression names */
9490   if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9491     {
9492       /* 15.13.1: NAME can appear within the scope of a local variable
9493          declaration */
9494       if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9495         return decl;
9496
9497       /* 15.13.1: NAME can appear within a class declaration */
9498       else
9499         {
9500           decl = lookup_field_wrapper (current_class, name);
9501           if (decl)
9502             {
9503               tree access = NULL_TREE;
9504               int fs = FIELD_STATIC (decl);
9505
9506               /* If we're accessing an outer scope local alias, make
9507                  sure we change the name of the field we're going to
9508                  build access to. */
9509               if (FIELD_LOCAL_ALIAS_USED (decl))
9510                 name = DECL_NAME (decl);
9511
9512               check_deprecation (id, decl);
9513
9514               /* Instance variable (8.3.1.1) can't appear within
9515                  static method, static initializer or initializer for
9516                  a static variable. */
9517               if (!fs && METHOD_STATIC (current_function_decl))
9518                 {
9519                   static_ref_err (id, name, current_class);
9520                   return error_mark_node;
9521                 }
9522               /* Instance variables can't appear as an argument of
9523                  an explicit constructor invocation */
9524               if (!fs && ctxp->explicit_constructor_p
9525                   && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9526                 {
9527                   parse_error_context
9528                     (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9529                   return error_mark_node;
9530                 }
9531
9532               /* If we're processing an inner class and we're trying
9533                  to access a field belonging to an outer class, build
9534                  the access to the field */
9535               if (!fs && outer_field_access_p (current_class, decl))
9536                 {
9537                   if (CLASS_STATIC (TYPE_NAME (current_class)))
9538                     {
9539                       static_ref_err (id, DECL_NAME (decl), current_class);
9540                       return error_mark_node;
9541                     }
9542                   access = build_outer_field_access (id, decl);
9543                   if (orig)
9544                     *orig = access;
9545                   return access;
9546                 }
9547
9548               /* Otherwise build what it takes to access the field */
9549               access = build_field_ref ((fs ? NULL_TREE : current_this),
9550                                         DECL_CONTEXT (decl), name);
9551               if (fs)
9552                 access = maybe_build_class_init_for_field (decl, access);
9553               /* We may be asked to save the real field access node */
9554               if (orig)
9555                 *orig = access;
9556               /* Last check: can we access the field? */
9557               if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9558                 {
9559                   not_accessible_field_error (id, decl);
9560                   return error_mark_node;
9561                 }
9562               /* And we return what we got */
9563               return access;
9564             }
9565           /* Fall down to error report on undefined variable */
9566         }
9567     }
9568   /* 6.5.5.2 Qualified Expression Names */
9569   else
9570     {
9571       if (orig)
9572         *orig = NULL_TREE;
9573       qualify_ambiguous_name (id);
9574       /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9575       /* 15.10.2: Accessing Superclass Members using super */
9576       return resolve_field_access (id, orig, NULL);
9577     }
9578
9579   /* We've got an error here */
9580   if (INNER_CLASS_TYPE_P (current_class))
9581     parse_error_context (id,
9582                          "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9583                          IDENTIFIER_POINTER (name),
9584                          IDENTIFIER_POINTER (DECL_NAME
9585                                              (TYPE_NAME (current_class))));
9586   else
9587     parse_error_context (id, "Undefined variable %qs",
9588                          IDENTIFIER_POINTER (name));
9589
9590   return error_mark_node;
9591 }
9592
9593 static void
9594 static_ref_err (tree wfl, tree field_id, tree class_type)
9595 {
9596   parse_error_context
9597     (wfl,
9598      "Can't make a static reference to nonstatic variable %qs in class %qs",
9599      IDENTIFIER_POINTER (field_id),
9600      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9601 }
9602
9603 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9604    We return something suitable to generate the field access. We also
9605    return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
9606    recipient's address can be null. */
9607
9608 static tree
9609 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9610 {
9611   int is_static = 0;
9612   tree field_ref;
9613   tree decl = NULL_TREE, where_found, type_found;
9614
9615   if (resolve_qualified_expression_name (qual_wfl, &decl,
9616                                          &where_found, &type_found))
9617     return error_mark_node;
9618
9619   /* Resolve the LENGTH field of an array here */
9620   if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9621       && type_found && TYPE_ARRAY_P (type_found)
9622       && ! flag_emit_class_files && ! flag_emit_xref)
9623     {
9624       tree length = build_java_array_length_access (where_found);
9625       field_ref = length;
9626
9627       /* In case we're dealing with a static array, we need to
9628          initialize its class before the array length can be fetched.
9629          It's also a good time to create a DECL_RTL for the field if
9630          none already exists, otherwise if the field was declared in a
9631          class found in an external file and hasn't been (and won't
9632          be) accessed for its value, none will be created. */
9633       if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9634         {
9635           build_static_field_ref (where_found);
9636           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9637         }
9638     }
9639   /* We might have been trying to resolve field.method(). In which
9640      case, the resolution is over and decl is the answer */
9641   else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9642     field_ref = decl;
9643   else if (JDECL_P (decl))
9644     {
9645       if (!type_found)
9646         type_found = DECL_CONTEXT (decl);
9647       is_static = FIELD_STATIC (decl);
9648       field_ref = build_field_ref ((is_static && !flag_emit_xref?
9649                                     NULL_TREE : where_found),
9650                                    type_found, DECL_NAME (decl));
9651       if (field_ref == error_mark_node)
9652         return error_mark_node;
9653       if (is_static)
9654         field_ref = maybe_build_class_init_for_field (decl, field_ref);
9655
9656       /* If we're looking at a static field, we may need to generate a
9657          class initialization for it.  This can happen when the access
9658          looks like `field.ref', where `field' is a static field in an
9659          interface we implement.  */
9660       if (!flag_emit_class_files
9661           && !flag_emit_xref
9662           && TREE_CODE (where_found) == VAR_DECL
9663           && FIELD_STATIC (where_found))
9664         {
9665           build_static_field_ref (where_found);
9666           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9667         }
9668     }
9669   else
9670     field_ref = decl;
9671
9672   if (field_decl)
9673     *field_decl = decl;
9674   if (field_type)
9675     *field_type = (QUAL_DECL_TYPE (decl) ?
9676                    QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9677   return field_ref;
9678 }
9679
9680 /* If NODE is an access to a static field, strip out the class
9681    initialization part and return the field decl, otherwise, return
9682    NODE. */
9683
9684 tree
9685 extract_field_decl (tree node)
9686 {
9687   if (TREE_CODE (node) == COMPOUND_EXPR)
9688     {
9689       tree op1 = TREE_OPERAND (node, 1);
9690       if (TREE_CODE (op1) == COMPOUND_EXPR)
9691          {
9692            tree call = TREE_OPERAND (op1, 0);
9693            if (TREE_CODE (call) == CALL_EXPR
9694                && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9695                && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9696                    == soft_initclass_node))
9697              return TREE_OPERAND (op1, 1);
9698          }
9699       else if (JDECL_P (op1))
9700         return op1;
9701     }
9702   return node;
9703 }
9704
9705 /* 6.5.5.2: Qualified Expression Names */
9706
9707 static int
9708 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9709                                    tree *where_found, tree *type_found)
9710 {
9711   int from_type = 0;            /* Field search initiated from a type */
9712   int from_super = 0, from_cast = 0, from_qualified_this = 0;
9713   int previous_call_static = 0;
9714   int is_static;
9715   tree decl = NULL_TREE, type = NULL_TREE, q;
9716   /* For certain for of inner class instantiation */
9717   tree saved_current, saved_this;
9718 #define RESTORE_THIS_AND_CURRENT_CLASS                          \
9719   { current_class = saved_current; current_this = saved_this;}
9720
9721   *type_found = *where_found = NULL_TREE;
9722
9723   for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9724     {
9725       tree qual_wfl = QUAL_WFL (q);
9726       tree ret_decl;            /* for EH checking */
9727 #ifdef USE_MAPPED_LOCATION
9728       source_location location;  /* for EH checking */
9729 #else
9730       int location;             /* for EH checking */
9731 #endif
9732
9733       /* 15.10.1 Field Access Using a Primary */
9734       switch (TREE_CODE (qual_wfl))
9735         {
9736         case CALL_EXPR:
9737         case NEW_CLASS_EXPR:
9738           /* If the access to the function call is a non static field,
9739              build the code to access it. */
9740           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9741             {
9742               decl = maybe_access_field (decl, *where_found,
9743                                          DECL_CONTEXT (decl));
9744               if (decl == error_mark_node)
9745                 return 1;
9746             }
9747
9748           /* And code for the function call */
9749           if (complete_function_arguments (qual_wfl))
9750             return 1;
9751
9752           /* We might have to setup a new current class and a new this
9753              for the search of an inner class, relative to the type of
9754              a expression resolved as `decl'. The current values are
9755              saved and restored shortly after */
9756           saved_current = current_class;
9757           saved_this = current_this;
9758           if (decl
9759               && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9760                   || from_qualified_this))
9761             {
9762               /* If we still have `from_qualified_this', we have the form
9763                  <T>.this.f() and we need to build <T>.this */
9764               if (from_qualified_this)
9765                 {
9766                   decl = build_access_to_thisn (current_class, type, 0);
9767                   decl = java_complete_tree (decl);
9768                   type = TREE_TYPE (TREE_TYPE (decl));
9769                 }
9770               current_class = type;
9771               current_this = decl;
9772               from_qualified_this = 0;
9773             }
9774
9775           if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9776             CALL_USING_SUPER (qual_wfl) = 1;
9777 #ifdef USE_MAPPED_LOCATION
9778           location = (TREE_CODE (qual_wfl) == CALL_EXPR
9779                       ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9780                       : UNKNOWN_LOCATION);
9781 #else
9782           location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9783                       EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9784 #endif
9785           *where_found = patch_method_invocation (qual_wfl, decl, type,
9786                                                   from_super,
9787                                                   &is_static, &ret_decl);
9788           from_super = 0;
9789           if (*where_found == error_mark_node)
9790             {
9791               RESTORE_THIS_AND_CURRENT_CLASS;
9792               return 1;
9793             }
9794           *type_found = type = QUAL_DECL_TYPE (*where_found);
9795
9796           *where_found = force_evaluation_order (*where_found);
9797
9798           /* If we're creating an inner class instance, check for that
9799              an enclosing instance is in scope */
9800           if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9801               && INNER_ENCLOSING_SCOPE_CHECK (type))
9802             {
9803               parse_error_context
9804                 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9805                  lang_printable_name (type, 0),
9806                  (!current_this ? "" :
9807                   "; an explicit one must be provided when creating this inner class"));
9808               RESTORE_THIS_AND_CURRENT_CLASS;
9809               return 1;
9810             }
9811
9812           /* In case we had to change then to resolve a inner class
9813              instantiation using a primary qualified by a `new' */
9814           RESTORE_THIS_AND_CURRENT_CLASS;
9815
9816 #ifdef USE_MAPPED_LOCATION
9817           if (location != UNKNOWN_LOCATION)
9818 #else
9819           if (location)
9820 #endif
9821             {
9822               tree arguments = NULL_TREE;
9823               if (TREE_CODE (qual_wfl) == CALL_EXPR
9824                   && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9825                 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9826               check_thrown_exceptions (location, ret_decl, arguments);
9827             }
9828
9829           /* If the previous call was static and this one is too,
9830              build a compound expression to hold the two (because in
9831              that case, previous function calls aren't transported as
9832              forcoming function's argument. */
9833           if (previous_call_static && is_static)
9834             {
9835               /* We must set CAN_COMPLETE_NORMALLY for the first call
9836                  since it is done nowhere else.  */
9837               CAN_COMPLETE_NORMALLY (decl) = 1;
9838               decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9839                              decl, *where_found);
9840               TREE_SIDE_EFFECTS (decl) = 1;
9841             }
9842           else
9843             {
9844               previous_call_static = is_static;
9845               decl = *where_found;
9846             }
9847           from_type = 0;
9848           continue;
9849
9850         case NEW_ARRAY_EXPR:
9851         case NEW_ANONYMOUS_ARRAY_EXPR:
9852           *where_found = decl = java_complete_tree (qual_wfl);
9853           if (decl == error_mark_node)
9854             return 1;
9855           *type_found = type = QUAL_DECL_TYPE (decl);
9856           continue;
9857
9858         case CONVERT_EXPR:
9859           *where_found = decl = java_complete_tree (qual_wfl);
9860           if (decl == error_mark_node)
9861             return 1;
9862           *type_found = type = QUAL_DECL_TYPE (decl);
9863           from_cast = 1;
9864           continue;
9865
9866         case CONDITIONAL_EXPR:
9867         case STRING_CST:
9868         case MODIFY_EXPR:
9869           *where_found = decl = java_complete_tree (qual_wfl);
9870           if (decl == error_mark_node)
9871             return 1;
9872           *type_found = type = QUAL_DECL_TYPE (decl);
9873           continue;
9874
9875         case ARRAY_REF:
9876           /* If the access to the function call is a non static field,
9877              build the code to access it. */
9878           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9879             {
9880               decl = maybe_access_field (decl, *where_found, type);
9881               if (decl == error_mark_node)
9882                 return 1;
9883             }
9884           /* And code for the array reference expression */
9885           decl = java_complete_tree (qual_wfl);
9886           if (decl == error_mark_node)
9887             return 1;
9888           type = QUAL_DECL_TYPE (decl);
9889           continue;
9890
9891         case PLUS_EXPR:
9892           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9893             return 1;
9894           if ((type = patch_string (decl)))
9895             decl = type;
9896           *where_found = QUAL_RESOLUTION (q) = decl;
9897           *type_found = type = TREE_TYPE (decl);
9898           break;
9899
9900         case CLASS_LITERAL:
9901           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9902             return 1;
9903           *where_found = QUAL_RESOLUTION (q) = decl;
9904           *type_found = type = TREE_TYPE (decl);
9905           break;
9906
9907         default:
9908           /* Fix for -Wall Just go to the next statement. Don't
9909              continue */
9910           break;
9911         }
9912
9913       /* If we fall here, we weren't processing a (static) function call. */
9914       previous_call_static = 0;
9915
9916       /* It can be the keyword THIS */
9917       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9918           && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9919         {
9920           if (!current_this)
9921             {
9922               parse_error_context
9923                 (wfl, "Keyword %<this%> used outside allowed context");
9924               return 1;
9925             }
9926           if (ctxp->explicit_constructor_p
9927               && type == current_class)
9928             {
9929               parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9930               return 1;
9931             }
9932           /* We have to generate code for intermediate access */
9933           if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9934             {
9935               *where_found = decl = current_this;
9936               *type_found = type = QUAL_DECL_TYPE (decl);
9937             }
9938           /* We're trying to access the this from somewhere else. Make sure
9939              it's allowed before doing so. */
9940           else
9941             {
9942               if (!enclosing_context_p (type, current_class))
9943                 {
9944                   char *p  = xstrdup (lang_printable_name (type, 0));
9945                   parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9946                                        p, p,
9947                                        lang_printable_name (current_class, 0));
9948                   free (p);
9949                   return 1;
9950                 }
9951               from_qualified_this = 1;
9952               /* If there's nothing else after that, we need to
9953                  produce something now, otherwise, the section of the
9954                  code that needs to produce <T>.this will generate
9955                  what is necessary. */
9956               if (!TREE_CHAIN (q))
9957                 {
9958                   decl = build_access_to_thisn (current_class, type, 0);
9959                   *where_found = decl = java_complete_tree (decl);
9960                   *type_found = type = TREE_TYPE (decl);
9961                 }
9962             }
9963
9964           from_type = 0;
9965           continue;
9966         }
9967
9968       /* 15.10.2 Accessing Superclass Members using SUPER */
9969       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9970           && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9971         {
9972           tree node;
9973           /* Check on the restricted use of SUPER */
9974           if (METHOD_STATIC (current_function_decl)
9975               || current_class == object_type_node)
9976             {
9977               parse_error_context
9978                 (wfl, "Keyword %<super%> used outside allowed context");
9979               return 1;
9980             }
9981           /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9982           node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9983                              CLASSTYPE_SUPER (current_class),
9984                              build_this (EXPR_WFL_LINECOL (qual_wfl)));
9985           *where_found = decl = java_complete_tree (node);
9986           if (decl == error_mark_node)
9987             return 1;
9988           *type_found = type = QUAL_DECL_TYPE (decl);
9989           from_super = from_type = 1;
9990           continue;
9991         }
9992
9993       /* 15.13.1: Can't search for field name in packages, so we
9994          assume a variable/class name was meant. */
9995       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9996         {
9997           tree name;
9998           if ((decl = resolve_package (wfl, &q, &name)))
9999             {
10000               tree list;
10001               *where_found = decl;
10002
10003               check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10004
10005               /* We want to be absolutely sure that the class is laid
10006                  out. We're going to search something inside it. */
10007               *type_found = type = TREE_TYPE (decl);
10008               layout_class (type);
10009               from_type = 1;
10010
10011               /* Fix them all the way down, if any are left. */
10012               if (q)
10013                 {
10014                   list = TREE_CHAIN (q);
10015                   while (list)
10016                     {
10017                       RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10018                       list = TREE_CHAIN (list);
10019                     }
10020                 }
10021             }
10022           else
10023             {
10024               if (from_super || from_cast)
10025                 parse_error_context
10026                   ((from_cast ? qual_wfl : wfl),
10027                    "No variable %qs defined in class %qs",
10028                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10029                    lang_printable_name (type, 0));
10030               else
10031                 parse_error_context
10032                   (qual_wfl, "Undefined variable or class name: %qs",
10033                    IDENTIFIER_POINTER (name));
10034               return 1;
10035             }
10036         }
10037
10038       /* We have a type name. It's been already resolved when the
10039          expression was qualified. */
10040       else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10041         {
10042           decl = QUAL_RESOLUTION (q);
10043
10044           /* Sneak preview. If next we see a `new', we're facing a
10045              qualification which resulted in a type being selected
10046              instead of a field.  Report the error.  */
10047           if(TREE_CHAIN (q)
10048              && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10049             {
10050               parse_error_context (qual_wfl, "Undefined variable %qs",
10051                                    IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10052               return 1;
10053             }
10054
10055           check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10056           
10057           check_deprecation (qual_wfl, decl);
10058
10059           type = TREE_TYPE (decl);
10060           from_type = 1;
10061         }
10062       /* We resolve an expression name */
10063       else
10064         {
10065           tree field_decl = NULL_TREE;
10066
10067           /* If there exists an early resolution, use it. That occurs
10068              only once and we know that there are more things to
10069              come. Don't do that when processing something after SUPER
10070              (we need more thing to be put in place below */
10071           if (!from_super && QUAL_RESOLUTION (q))
10072             {
10073               decl = QUAL_RESOLUTION (q);
10074               if (!type)
10075                 {
10076                   if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
10077                     {
10078                       if (current_this)
10079                         *where_found = current_this;
10080                       else
10081                         {
10082                           static_ref_err (qual_wfl, DECL_NAME (decl),
10083                                           current_class);
10084                           return 1;
10085                         }
10086                       if (outer_field_access_p (current_class, decl))
10087                         decl = build_outer_field_access (qual_wfl, decl);
10088                     }
10089                   else
10090                     {
10091                       *where_found = TREE_TYPE (decl);
10092                       if (TREE_CODE (*where_found) == POINTER_TYPE)
10093                         *where_found = TREE_TYPE (*where_found);
10094                     }
10095                 }
10096             }
10097
10098           /* Report and error if we're using a numerical literal as a
10099              qualifier. It can only be an INTEGER_CST. */
10100           else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10101             {
10102               parse_error_context
10103                 (wfl, "Can't use type %qs as a qualifier",
10104                  lang_printable_name (TREE_TYPE (qual_wfl), 0));
10105               return 1;
10106             }
10107
10108           /* We have to search for a field, knowing the type of its
10109              container. The flag FROM_TYPE indicates that we resolved
10110              the last member of the expression as a type name, which
10111              means that for the resolution of this field, we'll look
10112              for other errors than if it was resolved as a member of
10113              an other field. */
10114           else
10115             {
10116               int is_static;
10117               tree field_decl_type; /* For layout */
10118
10119               if (!from_type && !JREFERENCE_TYPE_P (type))
10120                 {
10121                   parse_error_context
10122                     (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10123                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10124                      lang_printable_name (type, 0),
10125                      IDENTIFIER_POINTER (DECL_NAME (decl)));
10126                   return 1;
10127                 }
10128
10129               field_decl = lookup_field_wrapper (type,
10130                                                  EXPR_WFL_NODE (qual_wfl));
10131
10132               /* Maybe what we're trying to access to is an inner
10133                  class, only if decl is a TYPE_DECL. */
10134               if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10135                 {
10136                   tree ptr, inner_decl;
10137
10138                   BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10139                   inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10140                   if (inner_decl)
10141                     {
10142                       check_inner_class_access (inner_decl, decl, qual_wfl);
10143                       type = TREE_TYPE (inner_decl);
10144                       decl = inner_decl;
10145                       from_type = 1;
10146                       continue;
10147                     }
10148                 }
10149
10150               if (field_decl == NULL_TREE)
10151                 {
10152                   parse_error_context
10153                     (qual_wfl, "No variable %qs defined in type %qs",
10154                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10155                      GET_TYPE_NAME (type));
10156                   return 1;
10157                 }
10158               if (field_decl == error_mark_node)
10159                 return 1;
10160
10161               /* Layout the type of field_decl, since we may need
10162                  it. Don't do primitive types or loaded classes. The
10163                  situation of non primitive arrays may not handled
10164                  properly here. FIXME */
10165               if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10166                 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10167               else
10168                 field_decl_type = TREE_TYPE (field_decl);
10169               if (!JPRIMITIVE_TYPE_P (field_decl_type)
10170                   && !CLASS_LOADED_P (field_decl_type)
10171                   && !TYPE_ARRAY_P (field_decl_type))
10172                 resolve_and_layout (field_decl_type, NULL_TREE);
10173
10174               /* Check on accessibility here */
10175               if (not_accessible_p (current_class, field_decl,
10176                                     *type_found, from_super))
10177                 return not_accessible_field_error (qual_wfl,field_decl);    
10178               check_deprecation (qual_wfl, field_decl);
10179
10180               /* There are things to check when fields are accessed
10181                  from type. There are no restrictions on a static
10182                  declaration of the field when it is accessed from an
10183                  interface */
10184               is_static = FIELD_STATIC (field_decl);
10185               if (!from_super && from_type
10186                   && !TYPE_INTERFACE_P (type)
10187                   && !is_static
10188                   && (current_function_decl
10189                       && METHOD_STATIC (current_function_decl)))
10190                 {
10191                   static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10192                   return 1;
10193                 }
10194               from_cast = from_super = 0;
10195
10196               /* It's an access from a type but it isn't static, we
10197                  make it relative to `this'. */
10198               if (!is_static && from_type)
10199                 decl = current_this;
10200
10201               /* If we need to generate something to get a proper
10202                  handle on what this field is accessed from, do it
10203                  now. */
10204               if (!is_static)
10205                 {
10206                   decl = maybe_access_field (decl, *where_found, *type_found);
10207                   if (decl == error_mark_node)
10208                     return 1;
10209                 }
10210
10211               /* We want to keep the location were found it, and the type
10212                  we found. */
10213               *where_found = decl;
10214               *type_found = type;
10215
10216               /* Generate the correct expression for field access from
10217                  qualified this */
10218               if (from_qualified_this)
10219                 {
10220                   field_decl = build_outer_field_access (qual_wfl, field_decl);
10221                   from_qualified_this = 0;
10222                 }
10223
10224               /* This is the decl found and eventually the next one to
10225                  search from */
10226               decl = field_decl;
10227             }
10228           from_type = 0;
10229           type = QUAL_DECL_TYPE (decl);
10230
10231           /* Sneak preview. If decl is qualified by a `new', report
10232              the error here to be accurate on the peculiar construct */
10233           if (TREE_CHAIN (q)
10234               && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10235               && !JREFERENCE_TYPE_P (type))
10236             {
10237               parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10238                                    lang_printable_name (type, 0));
10239               return 1;
10240             }
10241         }
10242       /* `q' might have changed due to a after package resolution
10243          re-qualification */
10244       if (!q)
10245         break;
10246     }
10247   *found_decl = decl;
10248   return 0;
10249 }
10250
10251 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10252    can't be accessed from REFERENCE (a record type). If MEMBER
10253    features a protected access, we then use WHERE which, if non null,
10254    holds the type of MEMBER's access that is checked against
10255    6.6.2.1. This function should be used when decl is a field or a
10256    method.  */
10257
10258 static int
10259 not_accessible_p (tree reference, tree member, tree where, int from_super)
10260 {
10261   int access_flag = get_access_flags_from_decl (member);
10262   bool is_static = false;
10263  
10264   if (TREE_CODE (member) == FIELD_DECL ||
10265       TREE_CODE (member) == VAR_DECL)
10266     is_static = FIELD_STATIC (member);
10267   else
10268     is_static = METHOD_STATIC (member);
10269
10270   /* Access always granted for members declared public */
10271   if (access_flag & ACC_PUBLIC)
10272     return 0;
10273
10274   /* Check access on protected members */
10275   if (access_flag & ACC_PROTECTED)
10276     {
10277       /* Access granted if it occurs from within the package
10278          containing the class in which the protected member is
10279          declared */
10280       if (class_in_current_package (DECL_CONTEXT (member)))
10281         return 0;
10282
10283       /* If accessed with the form `super.member', then access is granted */
10284       if (from_super)
10285         return 0;
10286
10287       /* If WHERE is active, access was made through a qualifier. For 
10288          non-static members, access is granted if the type of the qualifier 
10289          is or is a sublass of the type the access is made from (6.6.2.1.)  */
10290       if (where && !is_static)
10291         {
10292           while (reference)
10293             {
10294               if (inherits_from_p (where, reference))
10295                 return 0;
10296               if (INNER_CLASS_TYPE_P (reference))
10297                 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10298               else
10299                 break;
10300             }
10301           return 1;
10302         }
10303
10304       /* Otherwise, access is granted if occurring from within the class
10305          where member is declared, or a subclass of it.  */
10306       while (reference)
10307         {
10308           if (inherits_from_p (reference, DECL_CONTEXT (member)))
10309             return 0;
10310           if (INNER_CLASS_TYPE_P (reference))
10311             reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10312           else
10313             break;
10314         }
10315       return 1;
10316     }
10317
10318   /* Check access on private members. Access is granted only if it
10319      occurs from within the class in which it is declared -- that does
10320      it for innerclasses too. */
10321   if (access_flag & ACC_PRIVATE)
10322     {
10323       if (reference == DECL_CONTEXT (member) ||
10324           common_enclosing_context_p (DECL_CONTEXT (member), reference))
10325         return 0;
10326       return 1;
10327     }
10328
10329   /* Default access is permitted only when occurring from within the
10330      package in which the context (MEMBER) is declared.  */
10331   return !class_in_current_package (DECL_CONTEXT (member));
10332 }
10333
10334 /* Test deprecated decl access.  */
10335 static void
10336 check_deprecation (tree wfl, tree decl)
10337 {
10338   const char *file;
10339   tree elt;
10340
10341   if (! warn_deprecated)
10342     return;
10343
10344   /* We want to look at the element type of arrays here, so we strip
10345      all surrounding array types.  */
10346   if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10347     {
10348       elt = TREE_TYPE (decl);
10349       while (TYPE_ARRAY_P (elt))
10350         elt = TYPE_ARRAY_ELEMENT (elt);
10351       /* We'll end up with a pointer type, so we use TREE_TYPE to go
10352          to the record.  */
10353       decl = TYPE_NAME (TREE_TYPE (elt));
10354     }
10355   file = DECL_SOURCE_FILE (decl);
10356
10357   /* Complain if the field is deprecated and the file it was defined
10358      in isn't compiled at the same time the file which contains its
10359      use is */
10360   if (DECL_DEPRECATED (decl)
10361       && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10362     {
10363       const char *the;
10364       switch (TREE_CODE (decl))
10365         {
10366         case FUNCTION_DECL:
10367           the = "method";
10368           break;
10369         case FIELD_DECL:
10370         case VAR_DECL:
10371           the = "field";
10372           break;
10373         case TYPE_DECL:
10374           parse_warning_context (wfl, "The class %qs has been deprecated",
10375                                  IDENTIFIER_POINTER (DECL_NAME (decl)));
10376           return;
10377         default:
10378           abort ();
10379         }
10380       /* Don't issue a message if the context as been deprecated as a
10381          whole. */
10382       if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10383         parse_warning_context
10384           (wfl, "The %s %qs in class %qs has been deprecated",
10385            the, lang_printable_name (decl, 0),
10386            IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10387     }
10388 }
10389
10390 /* Returns 1 if class was declared in the current package, 0 otherwise */
10391
10392 static GTY(()) tree cicp_cache;
10393 static int
10394 class_in_current_package (tree class)
10395 {
10396   int qualified_flag;
10397   tree left;
10398
10399   if (cicp_cache == class)
10400     return 1;
10401
10402   qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10403
10404   /* If the current package is empty and the name of CLASS is
10405      qualified, class isn't in the current package.  If there is a
10406      current package and the name of the CLASS is not qualified, class
10407      isn't in the current package */
10408   if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10409     return 0;
10410
10411   /* If there is not package and the name of CLASS isn't qualified,
10412      they belong to the same unnamed package */
10413   if (!ctxp->package && !qualified_flag)
10414     return 1;
10415
10416   /* Compare the left part of the name of CLASS with the package name */
10417   split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10418   if (ctxp->package == left)
10419     {
10420       cicp_cache = class;
10421       return 1;
10422     }
10423   return 0;
10424 }
10425
10426 /* This function may generate code to access DECL from WHERE. This is
10427    done only if certain conditions meet.  */
10428
10429 static tree
10430 maybe_access_field (tree decl, tree where, tree type)
10431 {
10432   if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10433       && !FIELD_STATIC (decl))
10434     decl = build_field_ref (where ? where : current_this,
10435                             (type ? type : DECL_CONTEXT (decl)),
10436                             DECL_NAME (decl));
10437   return decl;
10438 }
10439
10440 /* Build a method invocation, by patching PATCH. If non NULL
10441    and according to the situation, PRIMARY and WHERE may be
10442    used. IS_STATIC is set to 1 if the invoked function is static. */
10443
10444 static tree
10445 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10446                          int *is_static, tree *ret_decl)
10447 {
10448   tree wfl = TREE_OPERAND (patch, 0);
10449   tree args = TREE_OPERAND (patch, 1);
10450   tree name = EXPR_WFL_NODE (wfl);
10451   tree list;
10452   int is_static_flag = 0;
10453   int is_super_init = 0;
10454   tree this_arg = NULL_TREE;
10455   int is_array_clone_call = 0;
10456
10457   /* Should be overridden if everything goes well. Otherwise, if
10458      something fails, it should keep this value. It stop the
10459      evaluation of a bogus assignment. See java_complete_tree,
10460      MODIFY_EXPR: for the reasons why we sometimes want to keep on
10461      evaluating an assignment */
10462   TREE_TYPE (patch) = error_mark_node;
10463
10464   /* Since lookup functions are messing with line numbers, save the
10465      context now.  */
10466   java_parser_context_save_global ();
10467
10468   /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10469
10470   /* Resolution of qualified name, excluding constructors */
10471   if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10472     {
10473       tree identifier, identifier_wfl, type, resolved;
10474       /* Extract the last IDENTIFIER of the qualified
10475          expression. This is a wfl and we will use it's location
10476          data during error report. */
10477       identifier_wfl = cut_identifier_in_qualified (wfl);
10478       identifier = EXPR_WFL_NODE (identifier_wfl);
10479
10480       /* Given the context, IDENTIFIER is syntactically qualified
10481          as a MethodName. We need to qualify what's before */
10482       qualify_ambiguous_name (wfl);
10483       resolved = resolve_field_access (wfl, NULL, NULL);
10484
10485       if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10486          && FIELD_FINAL (resolved)
10487          && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10488          && !flag_emit_class_files && !flag_emit_xref)
10489        resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10490
10491       if (resolved == error_mark_node)
10492         PATCH_METHOD_RETURN_ERROR ();
10493
10494       type = GET_SKIP_TYPE (resolved);
10495       resolve_and_layout (type, NULL_TREE);
10496
10497       if (JPRIMITIVE_TYPE_P (type))
10498         {
10499           parse_error_context
10500             (identifier_wfl,
10501              "Can't invoke a method on primitive type %qs",
10502              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10503           PATCH_METHOD_RETURN_ERROR ();
10504         }
10505
10506       list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10507       args = nreverse (args);
10508
10509       /* We're resolving a call from a type */
10510       if (TREE_CODE (resolved) == TYPE_DECL)
10511         {
10512           if (CLASS_INTERFACE (resolved))
10513             {
10514               parse_error_context
10515                 (identifier_wfl,
10516                 "Can't make static reference to method %qs in interface %qs",
10517                  IDENTIFIER_POINTER (identifier),
10518                  IDENTIFIER_POINTER (name));
10519               PATCH_METHOD_RETURN_ERROR ();
10520             }
10521           if (list && !METHOD_STATIC (list))
10522             {
10523               char *fct_name = xstrdup (lang_printable_name (list, 2));
10524               parse_error_context
10525                 (identifier_wfl,
10526                  "Can't make static reference to method %<%s %s%> in class %qs",
10527                  lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10528                  fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10529               free (fct_name);
10530               PATCH_METHOD_RETURN_ERROR ();
10531             }
10532         }
10533       else
10534         this_arg = primary = resolved;
10535
10536       if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10537         is_array_clone_call = 1;
10538
10539       /* IDENTIFIER_WFL will be used to report any problem further */
10540       wfl = identifier_wfl;
10541     }
10542   /* Resolution of simple names, names generated after a primary: or
10543      constructors */
10544   else
10545     {
10546       tree class_to_search = NULL_TREE;
10547       int lc;                   /* Looking for Constructor */
10548
10549       /* We search constructor in their target class */
10550       if (CALL_CONSTRUCTOR_P (patch))
10551         {
10552           if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10553             class_to_search = EXPR_WFL_NODE (wfl);
10554           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10555                    this_identifier_node)
10556             class_to_search = NULL_TREE;
10557           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10558                    super_identifier_node)
10559             {
10560               is_super_init = 1;
10561               if (CLASSTYPE_SUPER (current_class))
10562                 class_to_search =
10563                   DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10564               else
10565                 {
10566                   parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10567                   PATCH_METHOD_RETURN_ERROR ();
10568                 }
10569             }
10570
10571           /* Class to search is NULL if we're searching the current one */
10572           if (class_to_search)
10573             {
10574               class_to_search = resolve_and_layout (class_to_search, wfl);
10575
10576               if (!class_to_search)
10577                 {
10578                   parse_error_context
10579                     (wfl, "Class %qs not found in type declaration",
10580                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10581                   PATCH_METHOD_RETURN_ERROR ();
10582                 }
10583
10584               /* Can't instantiate an abstract class, but we can
10585                  invoke it's constructor. It's use within the `new'
10586                  context is denied here. */
10587               if (CLASS_ABSTRACT (class_to_search)
10588                   && TREE_CODE (patch) == NEW_CLASS_EXPR)
10589                 {
10590                   parse_error_context
10591                     (wfl, "Class %qs is an abstract class. It can't be instantiated",
10592                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10593                   PATCH_METHOD_RETURN_ERROR ();
10594                 }
10595
10596               class_to_search = TREE_TYPE (class_to_search);
10597             }
10598           else
10599             class_to_search = current_class;
10600           lc = 1;
10601         }
10602       /* This is a regular search in the local class, unless an
10603          alternate class is specified. */
10604       else
10605         {
10606           if (where != NULL_TREE)
10607             class_to_search = where;
10608           else if (QUALIFIED_P (name))
10609             class_to_search = current_class;
10610           else
10611             {
10612               class_to_search = current_class;
10613
10614               for (;;)
10615                 {
10616                   if (has_method (class_to_search, name))
10617                     break;
10618                   if (! INNER_CLASS_TYPE_P (class_to_search))
10619                     {
10620                       parse_error_context (wfl,
10621                                            "No method named %qs in scope",
10622                                            IDENTIFIER_POINTER (name));
10623                       PATCH_METHOD_RETURN_ERROR ();
10624                     }
10625                   class_to_search
10626                     = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10627                 }
10628             }
10629           lc = 0;
10630         }
10631
10632       /* NAME is a simple identifier or comes from a primary. Search
10633          in the class whose declaration contain the method being
10634          invoked. */
10635       resolve_and_layout (class_to_search, NULL_TREE);
10636
10637       list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10638       /* Don't continue if no method were found, as the next statement
10639          can't be executed then. */
10640       if (!list)
10641         PATCH_METHOD_RETURN_ERROR ();
10642
10643       if (TYPE_ARRAY_P (class_to_search)
10644           && DECL_NAME (list) == get_identifier ("clone"))
10645         is_array_clone_call = 1;
10646
10647       /* Check for static reference if non static methods */
10648       if (check_for_static_method_reference (wfl, patch, list,
10649                                              class_to_search, primary))
10650         PATCH_METHOD_RETURN_ERROR ();
10651
10652       /* Check for inner classes creation from illegal contexts */
10653       if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10654                  && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10655           && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10656           && !DECL_INIT_P (current_function_decl))
10657         {
10658           parse_error_context
10659             (wfl, "No enclosing instance for inner class %qs is in scope%s",
10660              lang_printable_name (class_to_search, 0),
10661              (!current_this ? "" :
10662               "; an explicit one must be provided when creating this inner class"));
10663           PATCH_METHOD_RETURN_ERROR ();
10664         }
10665
10666       /* Non static methods are called with the current object extra
10667          argument. If patch a `new TYPE()', the argument is the value
10668          returned by the object allocator. If method is resolved as a
10669          primary, use the primary otherwise use the current THIS. */
10670       args = nreverse (args);
10671       if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10672         {
10673           this_arg = primary ? primary : current_this;
10674
10675           /* If we're using an access method, things are different.
10676              There are two family of cases:
10677
10678              1) We're not generating bytecodes:
10679
10680              - LIST is non static. It's invocation is transformed from
10681                x(a1,...,an) into this$<n>.x(a1,....an).
10682              - LIST is static. It's invocation is transformed from
10683                x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10684
10685              2) We're generating bytecodes:
10686
10687              - LIST is non static. It's invocation is transformed from
10688                x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10689              - LIST is static. It's invocation is transformed from
10690                x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10691
10692              Of course, this$<n> can be arbitrarily complex, ranging from
10693              this$0 (the immediate outer context) to
10694              access$0(access$0(...(this$0))).
10695
10696              maybe_use_access_method returns a nonzero value if the
10697              this_arg has to be moved into the (then generated) stub
10698              argument list. In the meantime, the selected function
10699              might have be replaced by a generated stub. */
10700           if (!primary &&
10701               maybe_use_access_method (is_super_init, &list, &this_arg))
10702             {
10703               args = tree_cons (NULL_TREE, this_arg, args);
10704               this_arg = NULL_TREE; /* So it doesn't get chained twice */
10705             }
10706         }
10707     }
10708
10709   /* Merge point of all resolution schemes. If we have nothing, this
10710      is an error, already signaled */
10711   if (!list)
10712     PATCH_METHOD_RETURN_ERROR ();
10713
10714   /* Check accessibility, position the is_static flag, build and
10715      return the call */
10716   if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10717                         (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10718                          NULL_TREE), from_super)
10719       /* Calls to clone() on array types are permitted as a special-case. */
10720       && !is_array_clone_call)
10721     {
10722       const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10723       const char *const access =
10724         accessibility_string (get_access_flags_from_decl (list));
10725       const char *const klass =
10726         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10727       const char *const refklass =
10728         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10729       const char *const what = (DECL_CONSTRUCTOR_P (list)
10730                                 ? "constructor" : "method");
10731       parse_error_context (wfl,
10732                            "Can't access %s %s %<%s.%s%> from %qs",
10733                            access, what, klass, fct_name, refklass);
10734       PATCH_METHOD_RETURN_ERROR ();
10735     }
10736
10737   /* Deprecation check: check whether the method being invoked or the
10738      instance-being-created's type are deprecated.  */
10739   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10740     check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10741   check_deprecation (wfl, list);
10742
10743   /* If invoking a innerclass constructor, there are hidden parameters
10744      to pass */
10745   if (TREE_CODE (patch) == NEW_CLASS_EXPR
10746       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10747     {
10748       /* And make sure we add the accessed local variables to be saved
10749          in field aliases. */
10750       args = build_alias_initializer_parameter_list
10751         (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10752
10753       /* Secretly pass the current_this/primary as a second argument */
10754       if (primary || current_this)
10755         {
10756           tree extra_arg;
10757           tree this_type = (current_this ?
10758                             TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10759           /* Method's (list) enclosing context */
10760           tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10761           /* If we have a primary, use it. */
10762           if (primary)
10763             extra_arg = primary;
10764           /* The current `this' is an inner class but isn't a direct
10765              enclosing context for the inner class we're trying to
10766              create. Build an access to the proper enclosing context
10767              and use it. */
10768           else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10769                    && this_type != TREE_TYPE (mec))
10770             {
10771
10772               extra_arg = build_access_to_thisn (current_class,
10773                                                  TREE_TYPE (mec), 0);
10774               extra_arg = java_complete_tree (extra_arg);
10775             }
10776           /* Otherwise, just use the current `this' as an enclosing
10777              context. */
10778           else
10779             extra_arg = current_this;
10780           args = tree_cons (NULL_TREE, extra_arg, args);
10781         }
10782       else
10783         args = tree_cons (NULL_TREE, integer_zero_node, args);
10784     }
10785
10786   /* This handles the situation where a constructor invocation needs
10787      to have an enclosing context passed as a second parameter (the
10788      constructor is one of an inner class). */
10789   if ((is_super_init ||
10790        (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10791       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10792     {
10793       tree dest = TYPE_NAME (DECL_CONTEXT (list));
10794       tree extra_arg =
10795         build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10796       extra_arg = java_complete_tree (extra_arg);
10797       args = tree_cons (NULL_TREE, extra_arg, args);
10798     }
10799
10800   is_static_flag = METHOD_STATIC (list);
10801   if (! is_static_flag && this_arg != NULL_TREE)
10802     args = tree_cons (NULL_TREE, this_arg, args);
10803
10804   /* In the context of an explicit constructor invocation, we can't
10805      invoke any method relying on `this'. Exceptions are: we're
10806      invoking a static function, primary exists and is not the current
10807      this, we're creating a new object. */
10808   if (ctxp->explicit_constructor_p
10809       && !is_static_flag
10810       && (!primary || primary == current_this)
10811       && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10812     {
10813       parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10814       PATCH_METHOD_RETURN_ERROR ();
10815     }
10816   java_parser_context_restore_global ();
10817   if (is_static)
10818     *is_static = is_static_flag;
10819   /* Sometimes, we want the decl of the selected method. Such as for
10820      EH checking */
10821   if (ret_decl)
10822     *ret_decl = list;
10823   patch = patch_invoke (patch, list, args);
10824
10825   /* Now is a good time to insert the call to finit$ */
10826   if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10827     {
10828       tree finit_parms, finit_call;
10829
10830       /* Prepare to pass hidden parameters to finit$, if any. */
10831       finit_parms = build_alias_initializer_parameter_list
10832         (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10833
10834       finit_call =
10835         build_method_invocation (build_wfl_node (finit_identifier_node),
10836                                  finit_parms);
10837
10838       /* Generate the code used to initialize fields declared with an
10839          initialization statement and build a compound statement along
10840          with the super constructor invocation. */
10841       CAN_COMPLETE_NORMALLY (patch) = 1;
10842       patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10843                       java_complete_tree (finit_call));
10844     }
10845   return patch;
10846 }
10847
10848 /* Check that we're not trying to do a static reference to a method in
10849    non static method. Return 1 if it's the case, 0 otherwise. */
10850
10851 static int
10852 check_for_static_method_reference (tree wfl, tree node, tree method,
10853                                    tree where, tree primary)
10854 {
10855   if (METHOD_STATIC (current_function_decl)
10856       && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10857     {
10858       char *fct_name = xstrdup (lang_printable_name (method, 0));
10859       parse_error_context
10860         (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10861          lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10862          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10863       free (fct_name);
10864       return 1;
10865     }
10866   return 0;
10867 }
10868
10869 /* Fix the invocation of *MDECL if necessary in the case of a
10870    invocation from an inner class. *THIS_ARG might be modified
10871    appropriately and an alternative access to *MDECL might be
10872    returned.  */
10873
10874 static int
10875 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10876 {
10877   tree ctx;
10878   tree md = *mdecl, ta = *this_arg;
10879   int to_return = 0;
10880   int non_static_context = !METHOD_STATIC (md);
10881
10882   if (is_super_init
10883       || DECL_CONTEXT (md) == current_class
10884       || !PURE_INNER_CLASS_TYPE_P (current_class)
10885       || DECL_FINIT_P (md)
10886       || DECL_INSTINIT_P (md))
10887     return 0;
10888
10889   /* If we're calling a method found in an enclosing class, generate
10890      what it takes to retrieve the right this. Don't do that if we're
10891      invoking a static method. Note that if MD's type is unrelated to
10892      CURRENT_CLASS, then the current this can be used. */
10893
10894   if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10895     {
10896       ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10897       if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10898         {
10899           ta = build_current_thisn (current_class);
10900           ta = build_wfl_node (ta);
10901         }
10902       else
10903         {
10904           tree type = ctx;
10905           while (type)
10906             {
10907               maybe_build_thisn_access_method (type);
10908               if (inherits_from_p (type, DECL_CONTEXT (md)))
10909                 {
10910                   ta = build_access_to_thisn (ctx, type, 0);
10911                   break;
10912                 }
10913               type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10914                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10915             }
10916         }
10917       ta = java_complete_tree (ta);
10918     }
10919
10920   /* We might have to use an access method to get to MD. We can
10921      break the method access rule as far as we're not generating
10922      bytecode */
10923   if (METHOD_PRIVATE (md) && flag_emit_class_files)
10924     {
10925       md = build_outer_method_access_method (md);
10926       to_return = 1;
10927     }
10928
10929   *mdecl = md;
10930   *this_arg = ta;
10931
10932   /* Returning a nonzero value indicates we were doing a non static
10933      method invocation that is now a static invocation. It will have
10934      callee displace `this' to insert it in the regular argument
10935      list. */
10936   return (non_static_context && to_return);
10937 }
10938
10939 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10940    mode.  */
10941
10942 static tree
10943 patch_invoke (tree patch, tree method, tree args)
10944 {
10945   tree dtable, func;
10946   tree original_call, t, ta;
10947   tree check = NULL_TREE;
10948
10949   /* Last step for args: convert build-in types. If we're dealing with
10950      a new TYPE() type call, the first argument to the constructor
10951      isn't found in the incoming argument list, but delivered by
10952      `new' */
10953   t = TYPE_ARG_TYPES (TREE_TYPE (method));
10954   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10955     t = TREE_CHAIN (t);
10956   for (ta = args; t != end_params_node && ta;
10957        t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10958     if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10959         TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10960       TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10961
10962   /* Resolve unresolved returned type issues */
10963   t = TREE_TYPE (TREE_TYPE (method));
10964   if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10965     resolve_and_layout (TREE_TYPE (t), NULL);
10966
10967   if (flag_emit_class_files || flag_emit_xref)
10968     func = method;
10969   else
10970     {
10971       switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10972         {
10973         case INVOKE_VIRTUAL:
10974           dtable = invoke_build_dtable (0, args);
10975           func = build_invokevirtual (dtable, method);
10976           break;
10977
10978         case INVOKE_NONVIRTUAL:
10979           /* If the object for the method call is null, we throw an
10980              exception.  We don't do this if the object is the current
10981              method's `this'.  In other cases we just rely on an
10982              optimization pass to eliminate redundant checks.  */
10983           if (TREE_VALUE (args) != current_this)
10984             {
10985               /* We use a save_expr here to make sure we only evaluate
10986                  the new `self' expression once.  */
10987               tree save_arg = save_expr (TREE_VALUE (args));
10988               TREE_VALUE (args) = save_arg;
10989               check = java_check_reference (save_arg, 1);
10990             }
10991           /* Fall through.  */
10992
10993         case INVOKE_SUPER:
10994         case INVOKE_STATIC:
10995           {
10996             tree signature = build_java_signature (TREE_TYPE (method));
10997             func = build_known_method_ref (method, TREE_TYPE (method),
10998                                            DECL_CONTEXT (method),
10999                                            signature, args);
11000           }
11001           break;
11002
11003         case INVOKE_INTERFACE:
11004           dtable = invoke_build_dtable (1, args);
11005           func = build_invokeinterface (dtable, method);
11006           break;
11007
11008         default:
11009           abort ();
11010         }
11011
11012       /* Ensure self_type is initialized, (invokestatic). FIXME */
11013       func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11014     }
11015
11016   TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11017   TREE_OPERAND (patch, 0) = func;
11018   TREE_OPERAND (patch, 1) = args;
11019   patch = check_for_builtin (method, patch);
11020   original_call = patch;
11021
11022   /* We're processing a `new TYPE ()' form. New is called and its
11023      returned value is the first argument to the constructor. We build
11024      a COMPOUND_EXPR and use saved expression so that the overall NEW
11025      expression value is a pointer to a newly created and initialized
11026      class. */
11027   if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11028     {
11029       tree class = DECL_CONTEXT (method);
11030       tree c1, saved_new, new;
11031       tree alloc_node;
11032
11033       if (flag_emit_class_files || flag_emit_xref)
11034         {
11035           TREE_TYPE (patch) = build_pointer_type (class);
11036           return patch;
11037         }
11038       if (!TYPE_SIZE (class))
11039         safe_layout_class (class);
11040       alloc_node =
11041         (class_has_finalize_method (class) ? alloc_object_node
11042                                            : alloc_no_finalizer_node);
11043       new = build3 (CALL_EXPR, promote_type (class),
11044                     build_address_of (alloc_node),
11045                     build_tree_list (NULL_TREE, build_class_ref (class)),
11046                     NULL_TREE);
11047       saved_new = save_expr (new);
11048       c1 = build_tree_list (NULL_TREE, saved_new);
11049       TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11050       TREE_OPERAND (original_call, 1) = c1;
11051       TREE_SET_CODE (original_call, CALL_EXPR);
11052       patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11053     }
11054
11055   /* If CHECK is set, then we are building a check to see if the object
11056      is NULL.  */
11057   if (check != NULL_TREE)
11058     {
11059       /* We have to call force_evaluation_order now because creating a
11060          COMPOUND_EXPR wraps the arg list in a way that makes it
11061          unrecognizable by force_evaluation_order later.  Yuk.  */
11062       patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check, 
11063                       force_evaluation_order (patch));
11064       TREE_SIDE_EFFECTS (patch) = 1;
11065     }
11066
11067   /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11068      put it as the first expression of a COMPOUND_EXPR. The second
11069      expression being an empty statement to be later patched if
11070      necessary. We remember a TREE_LIST (the PURPOSE is the method,
11071      the VALUE is the compound) in a hashtable and return a
11072      COMPOUND_EXPR built so that the result of the evaluation of the
11073      original PATCH node is returned. */
11074   if (STATIC_CLASS_INIT_OPT_P ()
11075       && current_function_decl && METHOD_STATIC (method))
11076     {
11077       tree list;
11078       tree fndecl = current_function_decl;
11079       /* We have to call force_evaluation_order now because creating a
11080          COMPOUND_EXPR wraps the arg list in a way that makes it
11081          unrecognizable by force_evaluation_order later.  Yuk.  */
11082       tree save = force_evaluation_order (patch);
11083       tree type = TREE_TYPE (patch);
11084
11085       patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11086       list = tree_cons (method, patch,
11087                         DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11088
11089       DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11090
11091       patch = build2 (COMPOUND_EXPR, type, patch, save);
11092     }
11093
11094   return patch;
11095 }
11096
11097 static int
11098 invocation_mode (tree method, int super)
11099 {
11100   int access = get_access_flags_from_decl (method);
11101
11102   if (super)
11103     return INVOKE_SUPER;
11104
11105   if (access & ACC_STATIC)
11106     return INVOKE_STATIC;
11107
11108   /* We have to look for a constructor before we handle nonvirtual
11109      calls; otherwise the constructor will look nonvirtual.  */
11110   if (DECL_CONSTRUCTOR_P (method))
11111     return INVOKE_STATIC;
11112
11113   if (access & ACC_PRIVATE)
11114     return INVOKE_NONVIRTUAL;
11115
11116   /* Binary compatibility: just because it's final today, that doesn't
11117      mean it'll be final tomorrow.  */
11118   if (! flag_indirect_dispatch  
11119       || DECL_CONTEXT (method) == object_type_node)
11120     {
11121       if (access & ACC_FINAL)
11122         return INVOKE_NONVIRTUAL;
11123
11124       if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11125         return INVOKE_NONVIRTUAL;
11126     }
11127
11128   if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11129     return INVOKE_INTERFACE;
11130
11131   return INVOKE_VIRTUAL;
11132 }
11133
11134 /* Retrieve a refined list of matching methods. It covers the step
11135    15.11.2 (Compile-Time Step 2) */
11136
11137 static tree
11138 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11139 {
11140   tree atl = end_params_node;           /* Arg Type List */
11141   tree method, signature, list, node;
11142   const char *candidates;               /* Used for error report */
11143   char *dup;
11144
11145   /* Fix the arguments */
11146   for (node = arg_list; node; node = TREE_CHAIN (node))
11147     {
11148       tree current_arg = TREE_TYPE (TREE_VALUE (node));
11149       /* Non primitive type may have to be resolved */
11150       if (!JPRIMITIVE_TYPE_P (current_arg))
11151         resolve_and_layout (current_arg, NULL_TREE);
11152       /* And promoted */
11153       if (TREE_CODE (current_arg) == RECORD_TYPE)
11154         current_arg = promote_type (current_arg);
11155       atl = tree_cons (NULL_TREE, current_arg, atl);
11156     }
11157
11158   /* Presto. If we're dealing with an anonymous class and a
11159      constructor call, generate the right constructor now, since we
11160      know the arguments' types. */
11161
11162   if (lc && ANONYMOUS_CLASS_P (class))
11163     {
11164       tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11165       /* The anonymous class may have already been laid out, so make sure
11166          the new constructor is laid out here.  */
11167       layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11168     }
11169
11170   /* Find all candidates and then refine the list, searching for the
11171      most specific method. */
11172   list = find_applicable_accessible_methods_list (lc, class, name, atl);
11173   list = find_most_specific_methods_list (list);
11174   if (list && !TREE_CHAIN (list))
11175     return TREE_VALUE (list);
11176
11177   /* Issue an error. List candidates if any. Candidates are listed
11178      only if accessible (non accessible methods may end-up here for
11179      the sake of a better error report). */
11180   candidates = NULL;
11181   if (list)
11182     {
11183       tree current;
11184       obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11185       for (current = list; current; current = TREE_CHAIN (current))
11186         {
11187           tree cm = TREE_VALUE (current);
11188           char string [4096];
11189           if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11190             continue;
11191           sprintf
11192             (string, "  '%s' in '%s'%s",
11193              get_printable_method_name (cm),
11194              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11195              (TREE_CHAIN (current) ? "\n" : ""));
11196           obstack_grow (&temporary_obstack, string, strlen (string));
11197         }
11198       obstack_1grow (&temporary_obstack, '\0');
11199       candidates = obstack_finish (&temporary_obstack);
11200     }
11201   /* Issue the error message */
11202   method = make_node (FUNCTION_TYPE);
11203   TYPE_ARG_TYPES (method) = atl;
11204   signature = build_java_argument_signature (method);
11205   dup = xstrdup (lang_printable_name (class, 0));
11206   parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11207                        (lc ? "constructor" : "method"),
11208                        (lc ? dup : IDENTIFIER_POINTER (name)),
11209                        IDENTIFIER_POINTER (signature), dup,
11210                        (candidates ? candidates : ""));
11211   free (dup);
11212   return NULL_TREE;
11213 }
11214
11215 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11216    when we're looking for a constructor. */
11217
11218 static tree
11219 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11220                                          tree arglist)
11221 {
11222   static htab_t searched_classes;
11223   static int search_not_done = 0;
11224   tree list = NULL_TREE, all_list = NULL_TREE;
11225   tree base_binfo;
11226   int i;
11227
11228   /* Check the hash table to determine if this class has been searched
11229      already. */
11230   if (searched_classes)
11231     {
11232       if (htab_find (searched_classes, class) != NULL)
11233         return NULL;
11234     }
11235   else
11236     {
11237       searched_classes = htab_create (10, htab_hash_pointer,
11238                                       htab_eq_pointer, NULL);
11239     }
11240
11241   search_not_done++;
11242   *htab_find_slot (searched_classes, class, INSERT) = class;
11243
11244   if (!CLASS_LOADED_P (class))
11245     {
11246       load_class (class, 1);
11247       safe_layout_class (class);
11248     }
11249
11250   /* Search interfaces */
11251   if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11252       && CLASS_INTERFACE (TYPE_NAME (class)))
11253     {
11254       search_applicable_methods_list (lc, TYPE_METHODS (class),
11255                                       name, arglist, &list, &all_list);
11256       for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11257         {
11258           tree t = BINFO_TYPE (base_binfo);
11259           tree rlist;
11260           
11261           rlist = find_applicable_accessible_methods_list (lc,  t, name,
11262                                                            arglist);
11263           list = chainon (rlist, list);
11264         }
11265     }
11266   /* Search classes */
11267   else
11268     {
11269       search_applicable_methods_list (lc, TYPE_METHODS (class),
11270                                       name, arglist, &list, &all_list);
11271
11272       /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11273          that we only search in class. Note that we should have found
11274          something at this point. */
11275       if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11276         {
11277           lc = 1;
11278           if (!list)
11279             abort ();
11280         }
11281
11282       /* We must search all interfaces of this class */
11283       if (!lc)
11284         {
11285           for (i = 1;
11286                BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11287             {
11288               tree t = BINFO_TYPE (base_binfo);
11289               if (t != object_type_node)
11290                 {
11291                   tree rlist
11292                     = find_applicable_accessible_methods_list (lc, t,
11293                                                                name, arglist);
11294                   list = chainon (rlist, list);
11295                 }
11296             }
11297         }
11298
11299       /* Search superclass */
11300       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11301         {
11302           tree rlist;
11303           class = CLASSTYPE_SUPER (class);
11304           rlist = find_applicable_accessible_methods_list (lc, class,
11305                                                            name, arglist);
11306           list = chainon (rlist, list);
11307         }
11308     }
11309
11310   search_not_done--;
11311
11312   /* We're done. Reset the searched classes list and finally search
11313      java.lang.Object if it wasn't searched already. */
11314   if (!search_not_done)
11315     {
11316       if (!lc
11317           && TYPE_METHODS (object_type_node)
11318           && htab_find (searched_classes, object_type_node) == NULL)
11319         {
11320           search_applicable_methods_list (lc,
11321                                           TYPE_METHODS (object_type_node),
11322                                           name, arglist, &list, &all_list);
11323         }
11324       htab_delete (searched_classes);
11325       searched_classes = NULL;
11326     }
11327
11328   /* Either return the list obtained or all selected (but
11329      inaccessible) methods for better error report. */
11330   return (!list ? all_list : list);
11331 }
11332
11333 /* Effectively search for the appropriate method in method */
11334
11335 static void
11336 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11337                                 tree *list, tree *all_list)
11338 {
11339   for (; method; method = TREE_CHAIN (method))
11340     {
11341       /* When dealing with constructor, stop here, otherwise search
11342          other classes */
11343       if (lc && !DECL_CONSTRUCTOR_P (method))
11344         continue;
11345       else if (!lc && (DECL_CONSTRUCTOR_P (method)
11346                        || (DECL_NAME (method) != name)))
11347         continue;
11348
11349       if (argument_types_convertible (method, arglist))
11350         {
11351           /* Retain accessible methods only */
11352           if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11353                                  method, NULL_TREE, 0))
11354             *list = tree_cons (NULL_TREE, method, *list);
11355           else
11356             /* Also retain all selected method here */
11357             *all_list = tree_cons (NULL_TREE, method, *list);
11358         }
11359     }
11360 }
11361
11362 /* 15.11.2.2 Choose the Most Specific Method */
11363
11364 static tree
11365 find_most_specific_methods_list (tree list)
11366 {
11367   int max = 0;
11368   int abstract, candidates;
11369   tree current, new_list = NULL_TREE;
11370   for (current = list; current; current = TREE_CHAIN (current))
11371     {
11372       tree method;
11373       DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11374
11375       for (method = list; method; method = TREE_CHAIN (method))
11376         {
11377           tree method_v, current_v;
11378           /* Don't test a method against itself */
11379           if (method == current)
11380             continue;
11381
11382           method_v = TREE_VALUE (method);
11383           current_v = TREE_VALUE (current);
11384
11385           /* Compare arguments and location where methods where declared */
11386           if (argument_types_convertible (method_v, current_v))
11387             {
11388               if (valid_method_invocation_conversion_p
11389                   (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11390                   || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11391                       && enclosing_context_p (DECL_CONTEXT (method_v),
11392                                               DECL_CONTEXT (current_v))))
11393                 {
11394                   int v = (DECL_SPECIFIC_COUNT (current_v) +=
11395                     (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11396                   max = (v > max ? v : max);
11397                 }
11398             }
11399         }
11400     }
11401
11402   /* Review the list and select the maximally specific methods */
11403   for (current = list, abstract = -1, candidates = -1;
11404        current; current = TREE_CHAIN (current))
11405     if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11406       {
11407         new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11408         abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11409         candidates++;
11410       }
11411
11412   /* If we have several and they're all abstract, just pick the
11413      closest one. */
11414   if (candidates > 0 && candidates == abstract)
11415     {
11416       /* FIXME: merge the throws clauses.  There is no convenient way
11417          to do this in gcj right now, since ideally we'd like to
11418          introduce a new METHOD_DECL here, but that is really not
11419          possible.  */
11420       new_list = nreverse (new_list);
11421       TREE_CHAIN (new_list) = NULL_TREE;
11422       return new_list;
11423     }
11424
11425   /* We have several (we couldn't find a most specific), all but one
11426      are abstract, we pick the only non abstract one. */
11427   if (candidates > 0 && (candidates == abstract+1))
11428     {
11429       for (current = new_list; current; current = TREE_CHAIN (current))
11430         if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11431           {
11432             TREE_CHAIN (current) = NULL_TREE;
11433             new_list = current;
11434           }
11435     }
11436
11437   /* If we can't find one, lower expectations and try to gather multiple
11438      maximally specific methods */
11439   while (!new_list && max)
11440     {
11441       while (--max > 0)
11442         {
11443           if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11444             new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11445         }
11446     }
11447
11448   return new_list;
11449 }
11450
11451 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11452    converted by method invocation conversion (5.3) to the type of the
11453    corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11454    to change less often than M1. */
11455
11456 static GTY(()) tree m2_arg_value;
11457 static GTY(()) tree m2_arg_cache;
11458
11459 static int
11460 argument_types_convertible (tree m1, tree m2_or_arglist)
11461 {
11462   tree m1_arg, m2_arg;
11463
11464   SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11465
11466   if (m2_arg_value == m2_or_arglist)
11467     m2_arg = m2_arg_cache;
11468   else
11469     {
11470       /* M2_OR_ARGLIST can be a function DECL or a raw list of
11471          argument types */
11472       if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11473         {
11474           m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11475           if (!METHOD_STATIC (m2_or_arglist))
11476             m2_arg = TREE_CHAIN (m2_arg);
11477         }
11478       else
11479         m2_arg = m2_or_arglist;
11480
11481       m2_arg_value = m2_or_arglist;
11482       m2_arg_cache = m2_arg;
11483     }
11484
11485   while (m1_arg != end_params_node && m2_arg != end_params_node)
11486     {
11487       resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11488       if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11489                                                  TREE_VALUE (m2_arg)))
11490         break;
11491       m1_arg = TREE_CHAIN (m1_arg);
11492       m2_arg = TREE_CHAIN (m2_arg);
11493     }
11494   return m1_arg == end_params_node && m2_arg == end_params_node;
11495 }
11496
11497 /* Qualification routines */
11498
11499 /* Given a name x.y.z, look up x locally.  If it's found, save the
11500    decl.  If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11501    so that we later try and load the appropriate classes.  */
11502 static void
11503 qualify_ambiguous_name (tree id)
11504 {
11505   tree name, decl;
11506
11507   /* We inspect the first item of the qualification list.  As a sanity
11508      check, make sure that it is an identfier node.  */
11509   tree qual = EXPR_WFL_QUALIFICATION (id);
11510   tree qual_wfl = QUAL_WFL (qual);
11511
11512   if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11513     return;
11514
11515   name = EXPR_WFL_NODE (qual_wfl);
11516
11517   /* If we don't have an identifier, or we have a 'this' or 'super',
11518      then field access processing is all we need : there is nothing
11519      for us to do.  */
11520   if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11521       name == this_identifier_node ||
11522       name == super_identifier_node)
11523     return;
11524
11525   /* If name appears within the scope of a local variable declaration
11526      or parameter declaration, or is a field within an enclosing
11527      class, then it is an expression name.  Save the decl and let
11528      resolve_field_access do it's work.  */
11529   if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11530       (decl = lookup_field_wrapper (current_class, name)))
11531     {
11532       QUAL_RESOLUTION (qual) = decl;
11533       return;
11534     }
11535
11536   /* If name is a known class name (either declared or imported), mark
11537      us as a type name.  */
11538   if ((decl = resolve_and_layout (name, NULL_TREE)))
11539     {
11540       RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11541       QUAL_RESOLUTION (qual) = decl;
11542     }
11543
11544   /* Check here that NAME isn't declared by more than one
11545      type-import-on-demand declaration of the compilation unit
11546      containing NAME. FIXME */
11547
11548   /* We couldn't find a declaration for the name.  Assume for now that
11549      we have a qualified class name that needs to be loaded from an
11550      external class file.  */
11551   else
11552     RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11553
11554   /* Propagate the qualification across other components of the
11555      qualified name */
11556   for (qual = TREE_CHAIN (qual); qual;
11557        qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11558     {
11559       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11560         RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11561     }
11562
11563   /* Store the global qualification for the ambiguous part of ID back
11564      into ID fields */
11565   if (RESOLVE_TYPE_NAME_P (qual_wfl))
11566     RESOLVE_TYPE_NAME_P (id) = 1;
11567   else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11568     RESOLVE_PACKAGE_NAME_P (id) = 1;
11569 }
11570
11571 /* Patch tree nodes in a function body. When a BLOCK is found, push
11572    local variable decls if present.
11573    Same as java_complete_lhs, but does resolve static finals to values. */
11574
11575 static tree
11576 java_complete_tree (tree node)
11577 {
11578   node = java_complete_lhs (node);
11579   if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11580       && DECL_INITIAL (node) != NULL_TREE
11581       && !flag_emit_xref)
11582     {
11583       tree value = fold_constant_for_init (node, node);
11584       if (value != NULL_TREE)
11585         return value;
11586     }
11587   return node;
11588 }
11589
11590 static tree
11591 java_stabilize_reference (tree node)
11592 {
11593   if (TREE_CODE (node) == COMPOUND_EXPR)
11594     {
11595       tree op0 = TREE_OPERAND (node, 0);
11596       tree op1 = TREE_OPERAND (node, 1);
11597       TREE_OPERAND (node, 0) = save_expr (op0);
11598       TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11599       return node;
11600     }
11601   return stabilize_reference (node);
11602 }
11603
11604 /* Patch tree nodes in a function body. When a BLOCK is found, push
11605    local variable decls if present.
11606    Same as java_complete_tree, but does not resolve static finals to values. */
11607
11608 static tree
11609 java_complete_lhs (tree node)
11610 {
11611   tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11612   int flag;
11613
11614   /* CONVERT_EXPR always has its type set, even though it needs to be
11615      worked out. */
11616   if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11617     return node;
11618
11619   /* The switch block implements cases processing container nodes
11620      first.  Contained nodes are always written back. Leaves come
11621      next and return a value. */
11622   switch (TREE_CODE (node))
11623     {
11624     case BLOCK:
11625
11626       /* 1- Block section.
11627          Set the local values on decl names so we can identify them
11628          faster when they're referenced. At that stage, identifiers
11629          are legal so we don't check for declaration errors. */
11630       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11631         {
11632           DECL_CONTEXT (cn) = current_function_decl;
11633           IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11634         }
11635       if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11636           CAN_COMPLETE_NORMALLY (node) = 1;
11637       else
11638         {
11639           tree stmt = BLOCK_EXPR_BODY (node);
11640           tree *ptr;
11641           int error_seen = 0;
11642           if (TREE_CODE (stmt) == COMPOUND_EXPR)
11643             {
11644               /* Re-order from (((A; B); C); ...; Z) to
11645                  (A; (B; (C ; (...; Z)))).
11646                  This makes it easier to scan the statements left-to-right
11647                  without using recursion (which might overflow the stack
11648                  if the block has many statements. */
11649               for (;;)
11650                 {
11651                   tree left = TREE_OPERAND (stmt, 0);
11652                   if (TREE_CODE (left) != COMPOUND_EXPR)
11653                     break;
11654                   TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11655                   TREE_OPERAND (left, 1) = stmt;
11656                   stmt = left;
11657                 }
11658               BLOCK_EXPR_BODY (node) = stmt;
11659             }
11660
11661           /* Now do the actual complete, without deep recursion for
11662              long blocks. */
11663           ptr = &BLOCK_EXPR_BODY (node);
11664           while (TREE_CODE (*ptr) == COMPOUND_EXPR
11665                  && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11666             {
11667               tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11668               tree *next = &TREE_OPERAND (*ptr, 1);
11669               TREE_OPERAND (*ptr, 0) = cur;
11670               if (IS_EMPTY_STMT (cur))
11671                 {
11672                   /* Optimization;  makes it easier to detect empty bodies.
11673                      Most useful for <clinit> with all-constant initializer. */
11674                   *ptr = *next;
11675                   continue;
11676                 }
11677               if (TREE_CODE (cur) == ERROR_MARK)
11678                 error_seen++;
11679               else if (! CAN_COMPLETE_NORMALLY (cur))
11680                 {
11681                   wfl_op2 = *next;
11682                   for (;;)
11683                     {
11684                       if (TREE_CODE (wfl_op2) == BLOCK)
11685                         wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11686                       else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11687                         wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11688                       else
11689                         break;
11690                     }
11691                   if (TREE_CODE (wfl_op2) != CASE_EXPR
11692                       && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11693                     unreachable_stmt_error (*ptr);
11694                 }
11695               if (TREE_TYPE (*ptr) == NULL_TREE)
11696                 TREE_TYPE (*ptr) = void_type_node;
11697               ptr = next;
11698             }
11699           *ptr = java_complete_tree (*ptr);
11700
11701           if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11702             return error_mark_node;
11703           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11704         }
11705       /* Turn local bindings to null */
11706       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11707         IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11708
11709       TREE_TYPE (node) = void_type_node;
11710       break;
11711
11712       /* 2- They are expressions but ultimately deal with statements */
11713
11714     case THROW_EXPR:
11715       wfl_op1 = TREE_OPERAND (node, 0);
11716       COMPLETE_CHECK_OP_0 (node);
11717       /* 14.19 A throw statement cannot complete normally. */
11718       CAN_COMPLETE_NORMALLY (node) = 0;
11719       return patch_throw_statement (node, wfl_op1);
11720
11721     case SYNCHRONIZED_EXPR:
11722       wfl_op1 = TREE_OPERAND (node, 0);
11723       return patch_synchronized_statement (node, wfl_op1);
11724
11725     case TRY_EXPR:
11726       return patch_try_statement (node);
11727
11728     case TRY_FINALLY_EXPR:
11729       COMPLETE_CHECK_OP_0 (node);
11730       COMPLETE_CHECK_OP_1 (node);
11731       if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11732         /* Reduce try/finally nodes with an empty try block.  */
11733         return TREE_OPERAND (node, 1);
11734       if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11735         /* Likewise for an empty finally block.  */
11736         return TREE_OPERAND (node, 0);
11737       CAN_COMPLETE_NORMALLY (node)
11738         = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11739            && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11740       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11741       return node;
11742
11743     case LABELED_BLOCK_EXPR:
11744       PUSH_LABELED_BLOCK (node);
11745       if (LABELED_BLOCK_BODY (node))
11746         COMPLETE_CHECK_OP_1 (node);
11747       TREE_TYPE (node) = void_type_node;
11748       POP_LABELED_BLOCK ();
11749
11750       if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11751         {
11752           LABELED_BLOCK_BODY (node) = NULL_TREE;
11753           CAN_COMPLETE_NORMALLY (node) = 1;
11754         }
11755       else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11756         CAN_COMPLETE_NORMALLY (node) = 1;
11757       return node;
11758
11759     case EXIT_BLOCK_EXPR:
11760       return patch_bc_statement (node);
11761
11762     case CASE_EXPR:
11763       cn = java_complete_tree (TREE_OPERAND (node, 0));
11764       if (cn == error_mark_node)
11765         return cn;
11766
11767       /* First, the case expression must be constant. Values of final
11768          fields are accepted. */
11769       cn = fold (cn);
11770       if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11771           && JDECL_P (TREE_OPERAND (cn, 1))
11772           && FIELD_FINAL (TREE_OPERAND (cn, 1))
11773           && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11774         {
11775           cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11776                                        TREE_OPERAND (cn, 1));
11777         }
11778       /* Accept final locals too. */
11779       else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn) 
11780                && DECL_INITIAL (cn))
11781         cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11782
11783       if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11784         {
11785           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11786           parse_error_context (node, "Constant expression required");
11787           return error_mark_node;
11788         }
11789
11790       nn = ctxp->current_loop;
11791
11792       /* It must be assignable to the type of the switch expression. */
11793       if (!try_builtin_assignconv (NULL_TREE,
11794                                    TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11795         {
11796           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11797           parse_error_context
11798             (wfl_operator,
11799              "Incompatible type for case. Can't convert %qs to %<int%>",
11800              lang_printable_name (TREE_TYPE (cn), 0));
11801           return error_mark_node;
11802         }
11803
11804       cn = fold (convert (int_type_node, cn));
11805       TREE_CONSTANT_OVERFLOW (cn) = 0;
11806       CAN_COMPLETE_NORMALLY (cn) = 1;
11807
11808       /* Save the label on a list so that we can later check for
11809          duplicates.  */
11810       case_label_list = tree_cons (node, cn, case_label_list);
11811
11812       /* Multiple instance of a case label bearing the same value is
11813          checked later. The case expression is all right so far. */
11814       if (TREE_CODE (cn) == VAR_DECL)
11815         cn = DECL_INITIAL (cn);
11816       TREE_OPERAND (node, 0) = cn;
11817       TREE_TYPE (node) = void_type_node;
11818       CAN_COMPLETE_NORMALLY (node) = 1;
11819       TREE_SIDE_EFFECTS (node) = 1;
11820       break;
11821
11822     case DEFAULT_EXPR:
11823       nn = ctxp->current_loop;
11824       /* Only one default label is allowed per switch statement */
11825       if (SWITCH_HAS_DEFAULT (nn))
11826         {
11827 #ifdef USE_MAPPED_LOCATION
11828           SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11829 #else
11830           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11831 #endif
11832           parse_error_context (wfl_operator,
11833                                "Duplicate case label: %<default%>");
11834           return error_mark_node;
11835         }
11836       else
11837         SWITCH_HAS_DEFAULT (nn) = 1;
11838       TREE_TYPE (node) = void_type_node;
11839       TREE_SIDE_EFFECTS (node) = 1;
11840       CAN_COMPLETE_NORMALLY (node) = 1;
11841       break;
11842
11843     case SWITCH_EXPR:
11844     case LOOP_EXPR:
11845       PUSH_LOOP (node);
11846       /* Check whether the loop was enclosed in a labeled
11847          statement. If not, create one, insert the loop in it and
11848          return the node */
11849       nn = patch_loop_statement (node);
11850
11851       /* Anyways, walk the body of the loop */
11852       if (TREE_CODE (node) == LOOP_EXPR)
11853         TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11854       /* Switch statement: walk the switch expression and the cases */
11855       else
11856         node = patch_switch_statement (node);
11857
11858       if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11859         nn = error_mark_node;
11860       else
11861         {
11862           TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11863           /* If we returned something different, that's because we
11864              inserted a label. Pop the label too. */
11865           if (nn != node)
11866             {
11867               if (CAN_COMPLETE_NORMALLY (node))
11868                 CAN_COMPLETE_NORMALLY (nn) = 1;
11869               POP_LABELED_BLOCK ();
11870             }
11871         }
11872       POP_LOOP ();
11873       return nn;
11874
11875     case EXIT_EXPR:
11876       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11877       return patch_exit_expr (node);
11878
11879     case COND_EXPR:
11880       /* Condition */
11881       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11882       if (TREE_OPERAND (node, 0) == error_mark_node)
11883         return error_mark_node;
11884       /* then-else branches */
11885       TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11886       if (TREE_OPERAND (node, 1) == error_mark_node)
11887         return error_mark_node;
11888       {
11889         /* This is a special case due to build_assertion().  When
11890            assertions are disabled we build a COND_EXPR in which
11891            Operand 1 is the body of the assertion.  If that happens to
11892            be a string concatenation we'll need to patch it here.  */
11893         tree patched = patch_string (TREE_OPERAND (node, 1));
11894         if (patched)
11895           TREE_OPERAND (node, 1) = patched;
11896       }
11897      TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11898       if (TREE_OPERAND (node, 2) == error_mark_node)
11899         return error_mark_node;
11900       return patch_if_else_statement (node);
11901       break;
11902
11903     case CONDITIONAL_EXPR:
11904       /* Condition */
11905       wfl_op1 = TREE_OPERAND (node, 0);
11906       COMPLETE_CHECK_OP_0 (node);
11907       wfl_op2 = TREE_OPERAND (node, 1);
11908       COMPLETE_CHECK_OP_1 (node);
11909       wfl_op3 = TREE_OPERAND (node, 2);
11910       COMPLETE_CHECK_OP_2 (node);
11911       return patch_conditional_expr (node, wfl_op1, wfl_op2);
11912
11913       /* 3- Expression section */
11914     case COMPOUND_EXPR:
11915       wfl_op2 = TREE_OPERAND (node, 1);
11916       TREE_OPERAND (node, 0) = nn =
11917         java_complete_tree (TREE_OPERAND (node, 0));
11918       if (IS_EMPTY_STMT (wfl_op2))
11919         CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11920       else
11921         {
11922           if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11923             {
11924               /* An unreachable condition in a do-while statement
11925                  is *not* (technically) an unreachable statement. */
11926               nn = wfl_op2;
11927               if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11928                 nn = EXPR_WFL_NODE (nn);
11929               /* NN can be NULL_TREE exactly when UPDATE is, in
11930                  finish_for_loop.  */
11931               if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11932                 {
11933                   SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11934                   if (SUPPRESS_UNREACHABLE_ERROR (nn))
11935                     {
11936                       /* Perhaps this warning should have an
11937                          associated flag.  The code being compiled is
11938                          pedantically correct, but useless.  */
11939                       parse_warning_context (wfl_operator,
11940                                              "Unreachable statement");
11941                     }
11942                   else
11943                     parse_error_context (wfl_operator,
11944                                          "Unreachable statement");
11945                 }
11946             }
11947           TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11948           if (TREE_OPERAND (node, 1) == error_mark_node)
11949             return error_mark_node;
11950           /* Even though we might allow the case where the first
11951              operand doesn't return normally, we still should compute
11952              CAN_COMPLETE_NORMALLY correctly.  */
11953           CAN_COMPLETE_NORMALLY (node)
11954             = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11955                && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11956         }
11957       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11958       break;
11959
11960     case RETURN_EXPR:
11961       /* CAN_COMPLETE_NORMALLY (node) = 0; */
11962       return patch_return (node);
11963
11964     case EXPR_WITH_FILE_LOCATION:
11965       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11966           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11967         {
11968           tree wfl = node;
11969           node = resolve_expression_name (node, NULL);
11970           if (node == error_mark_node)
11971             return node;
11972           /* Keep line number information somewhere were it doesn't
11973              disrupt the completion process. */
11974           if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11975             {
11976               EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11977               TREE_OPERAND (node, 1) = wfl;
11978             }
11979           CAN_COMPLETE_NORMALLY (node) = 1;
11980         }
11981       else
11982         {
11983           tree body;
11984           location_t save_location = input_location;
11985 #ifdef USE_MAPPED_LOCATION
11986           input_location = EXPR_LOCATION (node);
11987           if (input_location == UNKNOWN_LOCATION)
11988             input_location = save_location;
11989 #else
11990           input_line = EXPR_WFL_LINENO (node);
11991 #endif
11992           body = java_complete_tree (EXPR_WFL_NODE (node));
11993           input_location = save_location;
11994           EXPR_WFL_NODE (node) = body;
11995           TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11996           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11997           if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
11998             {
11999               /* Makes it easier to constant fold, detect empty bodies. */
12000               return body;
12001             }
12002           if (body == error_mark_node)
12003             {
12004               /* Its important for the evaluation of assignment that
12005                  this mark on the TREE_TYPE is propagated. */
12006               TREE_TYPE (node) = error_mark_node;
12007               return error_mark_node;
12008             }
12009           else
12010             TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12011
12012         }
12013       break;
12014
12015     case NEW_ARRAY_EXPR:
12016       /* Patch all the dimensions */
12017       flag = 0;
12018       for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12019         {
12020           int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12021           tree dim = convert (int_type_node,
12022                               java_complete_tree (TREE_VALUE (cn)));
12023           if (dim == error_mark_node)
12024             {
12025               flag = 1;
12026               continue;
12027             }
12028           else
12029             {
12030               TREE_VALUE (cn) = dim;
12031               /* Setup the location of the current dimension, for
12032                  later error report. */
12033 #ifdef USE_MAPPED_LOCATION
12034               TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12035 #else
12036               TREE_PURPOSE (cn) =
12037                 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12038               EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12039 #endif
12040             }
12041         }
12042       /* They complete the array creation expression, if no errors
12043          were found. */
12044       CAN_COMPLETE_NORMALLY (node) = 1;
12045       return (flag ? error_mark_node
12046               : force_evaluation_order (patch_newarray (node)));
12047
12048     case NEW_ANONYMOUS_ARRAY_EXPR:
12049       /* Create the array type if necessary. */
12050       if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12051         {
12052           tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12053           if (!(type = resolve_type_during_patch (type)))
12054             return error_mark_node;
12055           type = build_array_from_name (type, NULL_TREE,
12056                                         ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12057           ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12058         }
12059       node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12060                                    ANONYMOUS_ARRAY_INITIALIZER (node));
12061       if (node == error_mark_node)
12062         return error_mark_node;
12063       CAN_COMPLETE_NORMALLY (node) = 1;
12064       return node;
12065
12066     case NEW_CLASS_EXPR:
12067     case CALL_EXPR:
12068       /* Complete function's argument(s) first */
12069       if (complete_function_arguments (node))
12070         return error_mark_node;
12071       else
12072         {
12073           tree decl, wfl = TREE_OPERAND (node, 0);
12074           int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12075           int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12076                            super_identifier_node);
12077           tree arguments;
12078 #ifdef USE_MAPPED_LOCATION
12079           source_location location = EXPR_LOCATION (node);
12080 #else
12081           int location = EXPR_WFL_LINECOL (node);
12082 #endif
12083
12084           node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12085                                           from_super, 0, &decl);
12086           if (node == error_mark_node)
12087             return error_mark_node;
12088
12089           if (TREE_CODE (node) == CALL_EXPR
12090               && TREE_OPERAND (node, 1) != NULL_TREE)
12091             arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12092           else
12093             arguments = NULL_TREE;
12094           check_thrown_exceptions (location, decl, arguments);
12095           /* If we call this(...), register signature and positions */
12096           if (in_this)
12097             DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12098               tree_cons (wfl, decl,
12099                          DECL_CONSTRUCTOR_CALLS (current_function_decl));
12100           CAN_COMPLETE_NORMALLY (node) = 1;
12101           return force_evaluation_order (node);
12102         }
12103
12104     case MODIFY_EXPR:
12105       /* Save potential wfls */
12106       wfl_op1 = TREE_OPERAND (node, 0);
12107       TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12108
12109       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12110           && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12111           && DECL_INITIAL (nn) != NULL_TREE)
12112         {
12113           tree value;
12114
12115           value = fold_constant_for_init (nn, nn);
12116
12117           /* When we have a primitype type, or a string and we're not
12118              emitting a class file, we actually don't want to generate
12119              anything for the assignment. */
12120           if (value != NULL_TREE && 
12121               (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) || 
12122                (TREE_TYPE (value) == string_ptr_type_node &&
12123                 ! flag_emit_class_files)))
12124             {
12125               /* Prepare node for patch_assignment */
12126               TREE_OPERAND (node, 1) = value;
12127               /* Call patch assignment to verify the assignment */
12128               if (patch_assignment (node, wfl_op1) == error_mark_node)
12129                 return error_mark_node;
12130               /* Set DECL_INITIAL properly (a conversion might have
12131                  been decided by patch_assignment) and return the
12132                  empty statement. */
12133               else
12134                 {
12135                   tree patched = patch_string (TREE_OPERAND (node, 1));
12136                   if (patched)
12137                     DECL_INITIAL (nn) = patched;
12138                   else
12139                     DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12140                   DECL_FIELD_FINAL_IUD (nn) = 1;
12141                   return build_java_empty_stmt ();
12142                 }
12143             }
12144           if (! flag_emit_class_files)
12145             DECL_INITIAL (nn) = NULL_TREE;
12146         }
12147       wfl_op2 = TREE_OPERAND (node, 1);
12148
12149       if (TREE_OPERAND (node, 0) == error_mark_node)
12150         return error_mark_node;
12151
12152       flag = COMPOUND_ASSIGN_P (wfl_op2);
12153       if (flag)
12154         {
12155           /* This might break when accessing outer field from inner
12156              class. TESTME, FIXME */
12157           tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12158
12159           /* Hand stabilize the lhs on both places */
12160           TREE_OPERAND (node, 0) = lvalue;
12161           TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12162             (flag_emit_class_files ? lvalue : save_expr (lvalue));
12163
12164           /* 15.25.2.a: Left hand is not an array access. FIXME */
12165           /* Now complete the RHS. We write it back later on. */
12166           nn = java_complete_tree (TREE_OPERAND (node, 1));
12167
12168           if ((cn = patch_string (nn)))
12169             nn = cn;
12170
12171           /* The last part of the rewrite for E1 op= E2 is to have
12172              E1 = (T)(E1 op E2), with T being the type of E1. */
12173           nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12174                                                TREE_TYPE (lvalue), nn));
12175
12176           /* If the assignment is compound and has reference type,
12177              then ensure the LHS has type String and nothing else.  */
12178           if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12179               && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12180             parse_error_context (wfl_op2,
12181                                  "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12182                                  lang_printable_name (TREE_TYPE (lvalue), 0));
12183
12184           /* 15.25.2.b: Left hand is an array access. FIXME */
12185         }
12186
12187       /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12188          function to complete this RHS. Note that a NEW_ARRAY_INIT
12189          might have been already fully expanded if created as a result
12190          of processing an anonymous array initializer. We avoid doing
12191          the operation twice by testing whether the node already bears
12192          a type. */
12193       else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12194         nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12195                                    TREE_OPERAND (node, 1));
12196       /* Otherwise we simply complete the RHS */
12197       else
12198         nn = java_complete_tree (TREE_OPERAND (node, 1));
12199
12200       if (nn == error_mark_node)
12201         return error_mark_node;
12202
12203       /* Write back the RHS as we evaluated it. */
12204       TREE_OPERAND (node, 1) = nn;
12205
12206       /* In case we're handling = with a String as a RHS, we need to
12207          produce a String out of the RHS (it might still be a
12208          STRING_CST or a StringBuffer at this stage */
12209       if ((nn = patch_string (TREE_OPERAND (node, 1))))
12210         TREE_OPERAND (node, 1) = nn;
12211
12212       if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12213                                         TREE_OPERAND (node, 1))))
12214         {
12215           /* We return error_mark_node if outer_field_access_fix
12216              detects we write into a final. */
12217           if (nn == error_mark_node)
12218             return error_mark_node;
12219           node = nn;
12220         }
12221       else
12222         {
12223           node = patch_assignment (node, wfl_op1);
12224           if (node == error_mark_node)
12225             return error_mark_node;
12226           /* Reorganize the tree if necessary. */
12227           if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12228                        || JSTRING_P (TREE_TYPE (node))))
12229             node = java_refold (node);
12230         }
12231
12232       /* Seek to set DECL_INITIAL to a proper value, since it might have
12233          undergone a conversion in patch_assignment. We do that only when
12234          it's necessary to have DECL_INITIAL properly set. */
12235       nn = TREE_OPERAND (node, 0);
12236       if (TREE_CODE (nn) == VAR_DECL
12237           && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12238           && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12239           && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12240               || TREE_TYPE (nn) == string_ptr_type_node))
12241         DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12242
12243       CAN_COMPLETE_NORMALLY (node) = 1;
12244       return node;
12245
12246     case MULT_EXPR:
12247     case PLUS_EXPR:
12248     case MINUS_EXPR:
12249     case LSHIFT_EXPR:
12250     case RSHIFT_EXPR:
12251     case URSHIFT_EXPR:
12252     case BIT_AND_EXPR:
12253     case BIT_XOR_EXPR:
12254     case BIT_IOR_EXPR:
12255     case TRUNC_MOD_EXPR:
12256     case TRUNC_DIV_EXPR:
12257     case RDIV_EXPR:
12258     case TRUTH_ANDIF_EXPR:
12259     case TRUTH_ORIF_EXPR:
12260     case EQ_EXPR:
12261     case NE_EXPR:
12262     case GT_EXPR:
12263     case GE_EXPR:
12264     case LT_EXPR:
12265     case LE_EXPR:
12266       /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12267          knows how to handle those cases. */
12268       wfl_op1 = TREE_OPERAND (node, 0);
12269       wfl_op2 = TREE_OPERAND (node, 1);
12270
12271       CAN_COMPLETE_NORMALLY (node) = 1;
12272       /* Don't complete string nodes if dealing with the PLUS operand. */
12273       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12274         {
12275           nn = java_complete_tree (wfl_op1);
12276           if (nn == error_mark_node)
12277             return error_mark_node;
12278
12279           TREE_OPERAND (node, 0) = nn;
12280         }
12281       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12282         {
12283           nn = java_complete_tree (wfl_op2);
12284           if (nn == error_mark_node)
12285             return error_mark_node;
12286
12287           TREE_OPERAND (node, 1) = nn;
12288         }
12289       return patch_binop (node, wfl_op1, wfl_op2);
12290
12291     case INSTANCEOF_EXPR:
12292       wfl_op1 = TREE_OPERAND (node, 0);
12293       COMPLETE_CHECK_OP_0 (node);
12294       if (flag_emit_xref)
12295         {
12296           TREE_TYPE (node) = boolean_type_node;
12297           return node;
12298         }
12299       return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12300
12301     case UNARY_PLUS_EXPR:
12302     case NEGATE_EXPR:
12303     case TRUTH_NOT_EXPR:
12304     case BIT_NOT_EXPR:
12305     case PREDECREMENT_EXPR:
12306     case PREINCREMENT_EXPR:
12307     case POSTDECREMENT_EXPR:
12308     case POSTINCREMENT_EXPR:
12309     case CONVERT_EXPR:
12310       /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12311          how to handle those cases. */
12312       wfl_op1 = TREE_OPERAND (node, 0);
12313       CAN_COMPLETE_NORMALLY (node) = 1;
12314       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12315       if (TREE_OPERAND (node, 0) == error_mark_node)
12316         return error_mark_node;
12317       node = patch_unaryop (node, wfl_op1);
12318       CAN_COMPLETE_NORMALLY (node) = 1;
12319       break;
12320
12321     case ARRAY_REF:
12322       /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12323          how to handle those cases. */
12324       wfl_op1 = TREE_OPERAND (node, 0);
12325       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12326       if (TREE_OPERAND (node, 0) == error_mark_node)
12327         return error_mark_node;
12328       if (!flag_emit_class_files && !flag_emit_xref)
12329         TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12330       /* The same applies to wfl_op2 */
12331       wfl_op2 = TREE_OPERAND (node, 1);
12332       TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12333       if (TREE_OPERAND (node, 1) == error_mark_node)
12334         return error_mark_node;
12335       if (!flag_emit_class_files && !flag_emit_xref)
12336         TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12337       return patch_array_ref (node);
12338
12339     case RECORD_TYPE:
12340       return node;;
12341
12342     case COMPONENT_REF:
12343       /* The first step in the re-write of qualified name handling.  FIXME.
12344          So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12345       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12346       if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12347         {
12348           tree name = TREE_OPERAND (node, 1);
12349           tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12350           if (field == NULL_TREE)
12351             {
12352               error ("missing static field %qs", IDENTIFIER_POINTER (name));
12353               return error_mark_node;
12354             }
12355           if (! FIELD_STATIC (field))
12356             {
12357               error ("not a static field %qs", IDENTIFIER_POINTER (name));
12358               return error_mark_node;
12359             }
12360           return field;
12361         }
12362       else
12363         abort ();
12364       break;
12365
12366     case THIS_EXPR:
12367       /* Can't use THIS in a static environment */
12368       if (!current_this)
12369         {
12370           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12371           parse_error_context (wfl_operator,
12372                                "Keyword %<this%> used outside allowed context");
12373           TREE_TYPE (node) = error_mark_node;
12374           return error_mark_node;
12375         }
12376       if (ctxp->explicit_constructor_p)
12377         {
12378           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12379           parse_error_context
12380             (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12381           TREE_TYPE (node) = error_mark_node;
12382           return error_mark_node;
12383         }
12384       return current_this;
12385
12386     case CLASS_LITERAL:
12387       CAN_COMPLETE_NORMALLY (node) = 1;
12388       node = patch_incomplete_class_ref (node);
12389       if (node == error_mark_node)
12390         return error_mark_node;
12391       break;
12392
12393     default:
12394       CAN_COMPLETE_NORMALLY (node) = 1;
12395       /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12396          and it's time to turn it into the appropriate String object */
12397       if ((nn = patch_string (node)))
12398         node = nn;
12399       else
12400         internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12401     }
12402   return node;
12403 }
12404
12405 /* Complete function call's argument. Return a nonzero value is an
12406    error was found.  */
12407
12408 static int
12409 complete_function_arguments (tree node)
12410 {
12411   int flag = 0;
12412   tree cn;
12413
12414   ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12415   for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12416     {
12417       tree wfl = TREE_VALUE (cn), parm, temp;
12418       parm = java_complete_tree (wfl);
12419
12420       if (parm == error_mark_node)
12421         {
12422           flag = 1;
12423           continue;
12424         }
12425       /* If we have a string literal that we haven't transformed yet or a
12426          crafted string buffer, as a result of the use of the String
12427          `+' operator. Build `parm.toString()' and expand it. */
12428       if ((temp = patch_string (parm)))
12429         parm = temp;
12430
12431       TREE_VALUE (cn) = parm;
12432     }
12433   ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12434   return flag;
12435 }
12436
12437 /* Sometimes (for loops and variable initialized during their
12438    declaration), we want to wrap a statement around a WFL and turn it
12439    debugable.  */
12440
12441 static tree
12442 build_debugable_stmt (int location, tree stmt)
12443 {
12444   if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12445     {
12446 #ifdef USE_MAPPED_LOCATION
12447       stmt = expr_add_location (stmt, location, 1);
12448 #else
12449       stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12450       EXPR_WFL_LINECOL (stmt) = location;
12451       JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12452 #endif
12453     }
12454   return stmt;
12455 }
12456
12457 static tree
12458 build_expr_block (tree body, tree decls)
12459 {
12460   tree node = make_node (BLOCK);
12461   BLOCK_EXPR_DECLS (node) = decls;
12462   BLOCK_EXPR_BODY (node) = body;
12463   if (body)
12464     TREE_TYPE (node) = TREE_TYPE (body);
12465   TREE_SIDE_EFFECTS (node) = 1;
12466   return node;
12467 }
12468
12469 /* Create a new function block and link it appropriately to current
12470    function block chain */
12471
12472 static tree
12473 enter_block (void)
12474 {
12475   tree b = build_expr_block (NULL_TREE, NULL_TREE);
12476
12477   /* Link block B supercontext to the previous block. The current
12478      function DECL is used as supercontext when enter_a_block is called
12479      for the first time for a given function. The current function body
12480      (DECL_FUNCTION_BODY) is set to be block B.  */
12481
12482   tree fndecl = current_function_decl;
12483
12484   if (!fndecl) {
12485     BLOCK_SUPERCONTEXT (b) = current_static_block;
12486     current_static_block = b;
12487   }
12488
12489   else if (!DECL_FUNCTION_BODY (fndecl))
12490     {
12491       BLOCK_SUPERCONTEXT (b) = fndecl;
12492       DECL_FUNCTION_BODY (fndecl) = b;
12493     }
12494   else
12495     {
12496       BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12497       DECL_FUNCTION_BODY (fndecl) = b;
12498     }
12499   return b;
12500 }
12501
12502 /* Exit a block by changing the current function body
12503    (DECL_FUNCTION_BODY) to the current block super context, only if
12504    the block being exited isn't the method's top level one.  */
12505
12506 static tree
12507 exit_block (void)
12508 {
12509   tree b;
12510   if (current_function_decl)
12511     {
12512       b = DECL_FUNCTION_BODY (current_function_decl);
12513       if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12514         DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12515     }
12516   else
12517     {
12518       b = current_static_block;
12519
12520       if (BLOCK_SUPERCONTEXT (b))
12521         current_static_block = BLOCK_SUPERCONTEXT (b);
12522     }
12523   return b;
12524 }
12525
12526 /* Lookup for NAME in the nested function's blocks, all the way up to
12527    the current toplevel one. It complies with Java's local variable
12528    scoping rules.  */
12529
12530 static tree
12531 lookup_name_in_blocks (tree name)
12532 {
12533   tree b = GET_CURRENT_BLOCK (current_function_decl);
12534
12535   while (b != current_function_decl)
12536     {
12537       tree current;
12538
12539       /* Paranoid sanity check. To be removed */
12540       if (TREE_CODE (b) != BLOCK)
12541         abort ();
12542
12543       for (current = BLOCK_EXPR_DECLS (b); current;
12544            current = TREE_CHAIN (current))
12545         if (DECL_NAME (current) == name)
12546           return current;
12547       b = BLOCK_SUPERCONTEXT (b);
12548     }
12549   return NULL_TREE;
12550 }
12551
12552 static void
12553 maybe_absorb_scoping_blocks (void)
12554 {
12555   while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12556     {
12557       tree b = exit_block ();
12558       java_method_add_stmt (current_function_decl, b);
12559       SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12560     }
12561 }
12562
12563 \f
12564 /* This section of the source is reserved to build_* functions that
12565    are building incomplete tree nodes and the patch_* functions that
12566    are completing them.  */
12567
12568 /* Wrap a non WFL node around a WFL.  */
12569
12570 static tree
12571 build_wfl_wrap (tree node, int location)
12572 {
12573   tree wfl, node_to_insert = node;
12574
12575   /* We want to process THIS . xxx symbolically, to keep it consistent
12576      with the way we're processing SUPER. A THIS from a primary as a
12577      different form than a SUPER. Turn THIS into something symbolic */
12578   if (TREE_CODE (node) == THIS_EXPR)
12579     node_to_insert = wfl = build_wfl_node (this_identifier_node);
12580   else
12581 #ifdef USE_MAPPED_LOCATION
12582     wfl = build_unknown_wfl (NULL_TREE);
12583
12584   SET_EXPR_LOCATION (wfl, location);
12585 #else
12586     wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12587
12588   EXPR_WFL_LINECOL (wfl) = location;
12589 #endif
12590   EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12591   return wfl;
12592 }
12593
12594 /* Build a super() constructor invocation. Returns an empty statement if
12595    we're currently dealing with the class java.lang.Object. */
12596
12597 static tree
12598 build_super_invocation (tree mdecl)
12599 {
12600   if (DECL_CONTEXT (mdecl) == object_type_node)
12601     return build_java_empty_stmt ();
12602   else
12603     {
12604       tree super_wfl = build_wfl_node (super_identifier_node);
12605       tree a = NULL_TREE, t;
12606
12607       /* This is called after parsing is done, so the parser context
12608          won't be accurate. Set location info from current_class decl. */
12609       tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12610       EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12611
12612       /* If we're dealing with an anonymous class, pass the arguments
12613          of the crafted constructor along. */
12614       if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12615         {
12616           SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12617           for (; t != end_params_node; t = TREE_CHAIN (t))
12618             a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12619         }
12620       return build_method_invocation (super_wfl, a);
12621     }
12622 }
12623
12624 /* Build a SUPER/THIS qualified method invocation.  */
12625
12626 static tree
12627 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12628                                        int lloc, int rloc)
12629 {
12630   tree invok;
12631   tree wfl =
12632     build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12633   EXPR_WFL_LINECOL (wfl) = lloc;
12634   invok = build_method_invocation (name, args);
12635   return make_qualified_primary (wfl, invok, rloc);
12636 }
12637
12638 /* Build an incomplete CALL_EXPR node. */
12639
12640 static tree
12641 build_method_invocation (tree name, tree args)
12642 {
12643   tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12644   TREE_SIDE_EFFECTS (call) = 1;
12645   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12646   return call;
12647 }
12648
12649 /* Build an incomplete new xxx(...) node. */
12650
12651 static tree
12652 build_new_invocation (tree name, tree args)
12653 {
12654   tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12655   TREE_SIDE_EFFECTS (call) = 1;
12656   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12657   return call;
12658 }
12659
12660 /* Build an incomplete assignment expression. */
12661
12662 static tree
12663 build_assignment (int op, int op_location, tree lhs, tree rhs)
12664 {
12665   tree assignment;
12666   /* Build the corresponding binop if we deal with a Compound
12667      Assignment operator. Mark the binop sub-tree as part of a
12668      Compound Assignment expression */
12669   if (op != ASSIGN_TK)
12670     {
12671       rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12672       COMPOUND_ASSIGN_P (rhs) = 1;
12673     }
12674   assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12675   TREE_SIDE_EFFECTS (assignment) = 1;
12676   EXPR_WFL_LINECOL (assignment) = op_location;
12677   return assignment;
12678 }
12679
12680 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12681    the buffer.  This is used only for string conversion.  */
12682 static char *
12683 string_convert_int_cst (tree node)
12684 {
12685   /* Long.MIN_VALUE is -9223372036854775808, 20 characters.  */
12686   static char buffer[21];
12687
12688   unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12689   unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12690   char *p = buffer + sizeof (buffer);
12691   int neg = 0;
12692
12693   unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12694                                   << (HOST_BITS_PER_WIDE_INT - 1));
12695
12696   *--p = '\0';
12697
12698   /* If negative, note the fact and negate the value.  */
12699   if ((hi & hibit))
12700     {
12701       lo = ~lo;
12702       hi = ~hi;
12703       if (++lo == 0)
12704         ++hi;
12705       neg = 1;
12706     }
12707
12708   /* Divide by 10 until there are no bits left.  */
12709   do
12710     {
12711       unsigned HOST_WIDE_INT acc = 0;
12712       unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12713       unsigned int i;
12714
12715       /* Use long division to compute the result and the remainder.  */
12716       for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12717         {
12718           /* Shift a bit into accumulator.  */
12719           acc <<= 1;
12720           if ((hi & hibit))
12721             acc |= 1;
12722
12723           /* Shift the value.  */
12724           hi <<= 1;
12725           if ((lo & hibit))
12726             hi |= 1;
12727           lo <<= 1;
12728
12729           /* Shift the correct bit into the result.  */
12730           outhi <<= 1;
12731           if ((outlo & hibit))
12732             outhi |= 1;
12733           outlo <<= 1;
12734           if (acc >= 10)
12735             {
12736               acc -= 10;
12737               outlo |= 1;
12738             }
12739         }
12740
12741       /* '0' == 060 in Java, but might not be here (think EBCDIC).  */
12742       *--p = '\060' + acc;
12743
12744       hi = outhi;
12745       lo = outlo;
12746     }
12747   while (hi || lo);
12748
12749   if (neg)
12750     *--p = '\055'; /* '-' == 055 in Java, but might not be here.  */
12751
12752   return p;
12753 }
12754
12755 /* Print an INTEGER_CST node in a static buffer, and return the
12756    buffer.  This is used only for error handling.  */
12757 char *
12758 print_int_node (tree node)
12759 {
12760   static char buffer [80];
12761   if (TREE_CONSTANT_OVERFLOW (node))
12762     sprintf (buffer, "<overflow>");
12763
12764   if (TREE_INT_CST_HIGH (node) == 0)
12765     sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12766              TREE_INT_CST_LOW (node));
12767   else if (TREE_INT_CST_HIGH (node) == -1
12768            && TREE_INT_CST_LOW (node) != 0)
12769     sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12770              -TREE_INT_CST_LOW (node));
12771   else
12772     sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12773              TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12774
12775   return buffer;
12776 }
12777
12778 \f
12779 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12780    context.  */
12781
12782 /* 15.25 Assignment operators. */
12783
12784 static tree
12785 patch_assignment (tree node, tree wfl_op1)
12786 {
12787   tree rhs = TREE_OPERAND (node, 1);
12788   tree lvalue = TREE_OPERAND (node, 0), llvalue;
12789   tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12790   int error_found = 0;
12791   int lvalue_from_array = 0;
12792   int is_return = 0;
12793
12794   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12795
12796   /* Lhs can be a named variable */
12797   if (JDECL_P (lvalue))
12798     {
12799       lhs_type = TREE_TYPE (lvalue);
12800     }
12801   /* Or Lhs can be an array access. */
12802   else if (TREE_CODE (lvalue) == ARRAY_REF)
12803     {
12804       lhs_type = TREE_TYPE (lvalue);
12805       lvalue_from_array = 1;
12806     }
12807   /* Or a field access */
12808   else if (TREE_CODE (lvalue) == COMPONENT_REF)
12809     lhs_type = TREE_TYPE (lvalue);
12810   /* Or a function return slot */
12811   else if (TREE_CODE (lvalue) == RESULT_DECL)
12812     {
12813       /* If the return type is an integral type, then we create the
12814          RESULT_DECL with a promoted type, but we need to do these
12815          checks against the unpromoted type to ensure type safety.  So
12816          here we look at the real type, not the type of the decl we
12817          are modifying.  */
12818       lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12819       is_return = 1;
12820     }
12821   /* Otherwise, we might want to try to write into an optimized static
12822      final, this is an of a different nature, reported further on. */
12823   else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12824            && resolve_expression_name (wfl_op1, &llvalue))
12825     {
12826       lhs_type = TREE_TYPE (lvalue);
12827     }
12828   else
12829     {
12830       parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12831       error_found = 1;
12832     }
12833
12834   rhs_type = TREE_TYPE (rhs);
12835
12836   /* 5.1 Try the assignment conversion for builtin type. */
12837   new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12838
12839   /* 5.2 If it failed, try a reference conversion */
12840   if (!new_rhs)
12841     new_rhs = try_reference_assignconv (lhs_type, rhs);
12842
12843   /* 15.25.2 If we have a compound assignment, convert RHS into the
12844      type of the LHS */
12845   else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12846     new_rhs = convert (lhs_type, rhs);
12847
12848   /* Explicit cast required. This is an error */
12849   if (!new_rhs)
12850     {
12851       char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12852       char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12853       tree wfl;
12854       char operation [32];      /* Max size known */
12855
12856       /* If the assignment is part of a declaration, we use the WFL of
12857          the declared variable to point out the error and call it a
12858          declaration problem. If the assignment is a genuine =
12859          operator, we call is a operator `=' problem, otherwise we
12860          call it an assignment problem. In both of these last cases,
12861          we use the WFL of the operator to indicate the error. */
12862
12863       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12864         {
12865           wfl = wfl_op1;
12866           strcpy (operation, "declaration");
12867         }
12868       else
12869         {
12870           wfl = wfl_operator;
12871           if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12872             strcpy (operation, "assignment");
12873           else if (is_return)
12874             strcpy (operation, "'return'");
12875           else
12876             strcpy (operation, "'='");
12877         }
12878
12879       if (!valid_cast_to_p (rhs_type, lhs_type))
12880         parse_error_context
12881           (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12882            operation, t1, t2);
12883       else
12884         parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12885                              operation, t1, t2);
12886       free (t1); free (t2);
12887       error_found = 1;
12888     }
12889
12890   if (error_found)
12891     return error_mark_node;
12892
12893   /* If we're processing a `return' statement, promote the actual type
12894      to the promoted type.  */
12895   if (is_return)
12896     new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12897
12898   /* 10.10: Array Store Exception runtime check */
12899   if (!flag_emit_class_files
12900       && !flag_emit_xref
12901       && lvalue_from_array
12902       && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12903     {
12904       tree array, store_check, base, index_expr;
12905
12906       /* Save RHS so that it doesn't get re-evaluated by the store check. */
12907       new_rhs = save_expr (new_rhs);
12908
12909       /* Get the INDIRECT_REF. */
12910       array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12911       /* Get the array pointer expr. */
12912       array = TREE_OPERAND (array, 0);
12913       store_check = build_java_arraystore_check (array, new_rhs);
12914
12915       index_expr = TREE_OPERAND (lvalue, 1);
12916
12917       if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12918         {
12919           /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12920              happen before the store check, so prepare to insert the store
12921              check within the second operand of the existing COMPOUND_EXPR. */
12922           base = index_expr;
12923         }
12924       else
12925         base = lvalue;
12926
12927       index_expr = TREE_OPERAND (base, 1);
12928       TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12929                                        store_check, index_expr);
12930     }
12931
12932   /* Final locals can be used as case values in switch
12933      statement. Prepare them for this eventuality. */
12934   if (TREE_CODE (lvalue) == VAR_DECL
12935       && DECL_FINAL (lvalue)
12936       && TREE_CONSTANT (new_rhs)
12937       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12938       && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12939       )
12940     {
12941       TREE_CONSTANT (lvalue) = 1;
12942       TREE_INVARIANT (lvalue) = 1;
12943       DECL_INITIAL (lvalue) = new_rhs;
12944     }
12945
12946   /* Copy the rhs if it's a reference.  */
12947   if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12948     {
12949       switch (TREE_CODE (new_rhs))
12950         {
12951         case ARRAY_REF:
12952         case INDIRECT_REF:
12953         case COMPONENT_REF:
12954           /* Transform a = foo.bar 
12955              into a = ({int tmp; tmp = foo.bar;}).
12956              We need to ensure that if a read from memory fails
12957              because of a NullPointerException, a destination variable
12958              will remain unchanged.  An explicit temporary does what
12959              we need.  
12960
12961              If flag_check_references is set, this is unnecessary
12962              because we'll check each reference before doing any
12963              reads.  If optimize is not set the result will never be
12964              written to a stack slot that contains the LHS.  */
12965           {
12966             tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"), 
12967                                    TREE_TYPE (new_rhs));
12968             tree block = make_node (BLOCK);
12969             tree assignment 
12970               = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12971             DECL_CONTEXT (tmp) = current_function_decl;
12972             TREE_TYPE (block) = TREE_TYPE (new_rhs);
12973             BLOCK_VARS (block) = tmp;
12974             BLOCK_EXPR_BODY (block) = assignment;
12975             TREE_SIDE_EFFECTS (block) = 1;
12976             new_rhs = block;
12977           }
12978           break;
12979         default:
12980           break;
12981         }
12982     }
12983
12984   TREE_OPERAND (node, 0) = lvalue;
12985   TREE_OPERAND (node, 1) = new_rhs;
12986   TREE_TYPE (node) = lhs_type;
12987   return node;
12988 }
12989
12990 /* Check that type SOURCE can be cast into type DEST. If the cast
12991    can't occur at all, return NULL; otherwise, return a possibly
12992    modified rhs.  */
12993
12994 static tree
12995 try_reference_assignconv (tree lhs_type, tree rhs)
12996 {
12997   tree new_rhs = NULL_TREE;
12998   tree rhs_type = TREE_TYPE (rhs);
12999
13000   if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13001     {
13002       /* `null' may be assigned to any reference type */
13003       if (rhs == null_pointer_node)
13004         new_rhs = null_pointer_node;
13005       /* Try the reference assignment conversion */
13006       else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13007         new_rhs = rhs;
13008       /* This is a magic assignment that we process differently */
13009       else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13010         new_rhs = rhs;
13011     }
13012   return new_rhs;
13013 }
13014
13015 /* Check that RHS can be converted into LHS_TYPE by the assignment
13016    conversion (5.2), for the cases of RHS being a builtin type. Return
13017    NULL_TREE if the conversion fails or if because RHS isn't of a
13018    builtin type. Return a converted RHS if the conversion is possible.  */
13019
13020 static tree
13021 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13022 {
13023   tree new_rhs = NULL_TREE;
13024   tree rhs_type = TREE_TYPE (rhs);
13025
13026   /* Handle boolean specially.  */
13027   if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13028       || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13029     {
13030       if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13031           && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13032         new_rhs = rhs;
13033     }
13034
13035   /* 5.1.1 Try Identity Conversion,
13036      5.1.2 Try Widening Primitive Conversion */
13037   else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13038     new_rhs = convert (lhs_type, rhs);
13039
13040   /* Try a narrowing primitive conversion (5.1.3):
13041        - expression is a constant expression of type byte, short, char,
13042          or int, AND
13043        - variable is byte, short or char AND
13044        - The value of the expression is representable in the type of the
13045          variable */
13046   else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13047             || rhs_type == char_type_node || rhs_type == int_type_node)
13048             && TREE_CONSTANT (rhs)
13049            && (lhs_type == byte_type_node || lhs_type == char_type_node
13050                || lhs_type == short_type_node))
13051     {
13052       if (int_fits_type_p (rhs, lhs_type))
13053         new_rhs = convert (lhs_type, rhs);
13054       else if (wfl_op1)         /* Might be called with a NULL */
13055         parse_warning_context
13056           (wfl_op1,
13057            "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13058            print_int_node (rhs), lang_printable_name (lhs_type, 0));
13059       /* Reported a warning that will turn into an error further
13060          down, so we don't return */
13061     }
13062
13063   return new_rhs;
13064 }
13065
13066 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13067    conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
13068    0 is the conversion test fails.  This implements parts the method
13069    invocation conversion (5.3).  */
13070
13071 static int
13072 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13073 {
13074   /* 5.1.1: This is the identity conversion part. */
13075   if (lhs_type == rhs_type)
13076     return 1;
13077
13078   /* Reject non primitive types and boolean conversions.  */
13079   if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13080     return 0;
13081
13082   /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13083      than a char can't be converted into a char. Short can't too, but
13084      the < test below takes care of that */
13085   if (lhs_type == char_type_node && rhs_type == byte_type_node)
13086     return 0;
13087
13088   /* Accept all promoted type here. Note, we can't use <= in the test
13089      below, because we still need to bounce out assignments of short
13090      to char and the likes */
13091   if (lhs_type == int_type_node
13092       && (rhs_type == promoted_byte_type_node
13093           || rhs_type == promoted_short_type_node
13094           || rhs_type == promoted_char_type_node
13095           || rhs_type == promoted_boolean_type_node))
13096     return 1;
13097
13098   /* From here, an integral is widened if its precision is smaller
13099      than the precision of the LHS or if the LHS is a floating point
13100      type, or the RHS is a float and the RHS a double. */
13101   if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13102        && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13103       || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13104       || (rhs_type == float_type_node && lhs_type == double_type_node))
13105     return 1;
13106
13107   return 0;
13108 }
13109
13110 /* Check that something of SOURCE type can be assigned or cast to
13111    something of DEST type at runtime. Return 1 if the operation is
13112    valid, 0 otherwise. If CAST is set to 1, we're treating the case
13113    were SOURCE is cast into DEST, which borrows a lot of the
13114    assignment check. */
13115
13116 static int
13117 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13118 {
13119   /* SOURCE or DEST might be null if not from a declared entity. */
13120   if (!source || !dest)
13121     return 0;
13122   if (JNULLP_TYPE_P (source))
13123     return 1;
13124   if (TREE_CODE (source) == POINTER_TYPE)
13125     source = TREE_TYPE (source);
13126   if (TREE_CODE (dest) == POINTER_TYPE)
13127     dest = TREE_TYPE (dest);
13128
13129   /* If source and dest are being compiled from bytecode, they may need to
13130      be loaded. */
13131   if (CLASS_P (source) && !CLASS_LOADED_P (source))
13132     {
13133       load_class (source, 1);
13134       safe_layout_class (source);
13135     }
13136   if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13137     {
13138       load_class (dest, 1);
13139       safe_layout_class (dest);
13140     }
13141
13142   /* Case where SOURCE is a class type */
13143   if (TYPE_CLASS_P (source))
13144     {
13145       if (TYPE_CLASS_P (dest))
13146         return  (source == dest
13147                  || inherits_from_p (source, dest)
13148                  || (cast && inherits_from_p (dest, source)));
13149       if (TYPE_INTERFACE_P (dest))
13150         {
13151           /* If doing a cast and SOURCE is final, the operation is
13152              always correct a compile time (because even if SOURCE
13153              does not implement DEST, a subclass of SOURCE might). */
13154           if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13155             return 1;
13156           /* Otherwise, SOURCE must implement DEST */
13157           return interface_of_p (dest, source);
13158         }
13159       /* DEST is an array, cast permitted if SOURCE is of Object type */
13160       return (cast && source == object_type_node ? 1 : 0);
13161     }
13162   if (TYPE_INTERFACE_P (source))
13163     {
13164       if (TYPE_CLASS_P (dest))
13165         {
13166           /* If not casting, DEST must be the Object type */
13167           if (!cast)
13168             return dest == object_type_node;
13169           /* We're doing a cast. The cast is always valid is class
13170              DEST is not final, otherwise, DEST must implement SOURCE */
13171           else if (!CLASS_FINAL (TYPE_NAME (dest)))
13172             return 1;
13173           else
13174             return interface_of_p (source, dest);
13175         }
13176       if (TYPE_INTERFACE_P (dest))
13177         {
13178           /* If doing a cast, then if SOURCE and DEST contain method
13179              with the same signature but different return type, then
13180              this is a (compile time) error */
13181           if (cast)
13182             {
13183               tree method_source, method_dest;
13184               tree source_type;
13185               tree source_sig;
13186               tree source_name;
13187               for (method_source = TYPE_METHODS (source); method_source;
13188                    method_source = TREE_CHAIN (method_source))
13189                 {
13190                   source_sig =
13191                     build_java_argument_signature (TREE_TYPE (method_source));
13192                   source_type = TREE_TYPE (TREE_TYPE (method_source));
13193                   source_name = DECL_NAME (method_source);
13194                   for (method_dest = TYPE_METHODS (dest);
13195                        method_dest; method_dest = TREE_CHAIN (method_dest))
13196                     if (source_sig ==
13197                         build_java_argument_signature (TREE_TYPE (method_dest))
13198                         && source_name == DECL_NAME (method_dest)
13199                         && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13200                       return 0;
13201                 }
13202               return 1;
13203             }
13204           else
13205             return source == dest || interface_of_p (dest, source);
13206         }
13207       else
13208         {
13209           /* Array */
13210           return (cast
13211                   && (DECL_NAME (TYPE_NAME (source))
13212                       == java_lang_cloneable_identifier_node
13213                       || (DECL_NAME (TYPE_NAME (source))
13214                           == java_io_serializable_identifier_node)));
13215         }
13216     }
13217   if (TYPE_ARRAY_P (source))
13218     {
13219       if (TYPE_CLASS_P (dest))
13220         return dest == object_type_node;
13221       /* Can't cast an array to an interface unless the interface is
13222          java.lang.Cloneable or java.io.Serializable.  */
13223       if (TYPE_INTERFACE_P (dest))
13224         return (DECL_NAME (TYPE_NAME (dest))
13225                 == java_lang_cloneable_identifier_node
13226                 || (DECL_NAME (TYPE_NAME (dest))
13227                     == java_io_serializable_identifier_node));
13228       else                      /* Arrays */
13229         {
13230           tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13231           tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13232
13233           /* In case of severe errors, they turn out null */
13234           if (!dest_element_type || !source_element_type)
13235             return 0;
13236           if (source_element_type == dest_element_type)
13237             return 1;
13238           return valid_ref_assignconv_cast_p (source_element_type,
13239                                               dest_element_type, cast);
13240         }
13241       return 0;
13242     }
13243   return 0;
13244 }
13245
13246 static int
13247 valid_cast_to_p (tree source, tree dest)
13248 {
13249   if (TREE_CODE (source) == POINTER_TYPE)
13250     source = TREE_TYPE (source);
13251   if (TREE_CODE (dest) == POINTER_TYPE)
13252     dest = TREE_TYPE (dest);
13253
13254   if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13255     return valid_ref_assignconv_cast_p (source, dest, 1);
13256
13257   else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13258     return 1;
13259
13260   else if (TREE_CODE (source) == BOOLEAN_TYPE
13261            && TREE_CODE (dest) == BOOLEAN_TYPE)
13262     return 1;
13263
13264   return 0;
13265 }
13266
13267 static tree
13268 do_unary_numeric_promotion (tree arg)
13269 {
13270   tree type = TREE_TYPE (arg);
13271   if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13272       || TREE_CODE (type) == CHAR_TYPE)
13273     arg = convert (int_type_node, arg);
13274   return arg;
13275 }
13276
13277 /* Return a nonzero value if SOURCE can be converted into DEST using
13278    the method invocation conversion rule (5.3).  */
13279 static int
13280 valid_method_invocation_conversion_p (tree dest, tree source)
13281 {
13282   return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13283            && valid_builtin_assignconv_identity_widening_p (dest, source))
13284           || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13285               && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13286               && valid_ref_assignconv_cast_p (source, dest, 0)));
13287 }
13288
13289 /* Build an incomplete binop expression. */
13290
13291 static tree
13292 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13293 {
13294   tree binop = build2 (op, NULL_TREE, op1, op2);
13295   TREE_SIDE_EFFECTS (binop) = 1;
13296   /* Store the location of the operator, for better error report. The
13297      string of the operator will be rebuild based on the OP value. */
13298   EXPR_WFL_LINECOL (binop) = op_location;
13299   return binop;
13300 }
13301
13302 /* Build the string of the operator retained by NODE. If NODE is part
13303    of a compound expression, add an '=' at the end of the string. This
13304    function is called when an error needs to be reported on an
13305    operator. The string is returned as a pointer to a static character
13306    buffer. */
13307
13308 static char *
13309 operator_string (tree node)
13310 {
13311 #define BUILD_OPERATOR_STRING(S)                                        \
13312   {                                                                     \
13313     sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13314     return buffer;                                                      \
13315   }
13316
13317   static char buffer [10];
13318   switch (TREE_CODE (node))
13319     {
13320     case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13321     case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13322     case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13323     case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13324     case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13325     case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13326     case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13327     case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13328     case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13329     case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13330     case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13331     case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13332     case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13333     case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13334     case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13335     case GT_EXPR: BUILD_OPERATOR_STRING (">");
13336     case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13337     case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13338     case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13339     case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13340     case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13341     case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13342     case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13343     case PREINCREMENT_EXPR:     /* Fall through */
13344     case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13345     case PREDECREMENT_EXPR:     /* Fall through */
13346     case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13347     default:
13348       internal_error ("unregistered operator %s",
13349                       tree_code_name [TREE_CODE (node)]);
13350     }
13351   return NULL;
13352 #undef BUILD_OPERATOR_STRING
13353 }
13354
13355 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13356
13357 static int
13358 java_decl_equiv (tree var_acc1, tree var_acc2)
13359 {
13360   if (JDECL_P (var_acc1))
13361     return (var_acc1 == var_acc2);
13362
13363   return (TREE_CODE (var_acc1) == COMPONENT_REF
13364           && TREE_CODE (var_acc2) == COMPONENT_REF
13365           && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13366              == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13367           && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13368 }
13369
13370 /* Return a nonzero value if CODE is one of the operators that can be
13371    used in conjunction with the `=' operator in a compound assignment.  */
13372
13373 static int
13374 binop_compound_p (enum tree_code code)
13375 {
13376   int i;
13377   for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13378     if (binop_lookup [i] == code)
13379       break;
13380
13381   return i < BINOP_COMPOUND_CANDIDATES;
13382 }
13383
13384 /* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13385
13386 static tree
13387 java_refold (tree t)
13388 {
13389   tree c, b, ns, decl;
13390
13391   if (TREE_CODE (t) != MODIFY_EXPR)
13392     return t;
13393
13394   c = TREE_OPERAND (t, 1);
13395   if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13396          && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13397          && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13398     return t;
13399
13400   /* Now the left branch of the binary operator. */
13401   b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13402   if (! (b && TREE_CODE (b) == NOP_EXPR
13403          && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13404     return t;
13405
13406   ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13407   if (! (ns && TREE_CODE (ns) == NOP_EXPR
13408          && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13409     return t;
13410
13411   decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13412   if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13413       /* It's got to be the an equivalent decl */
13414       && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13415     {
13416       /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13417       TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13418       /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13419       TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13420       /* Change the right part of the BINOP_EXPR */
13421       TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13422     }
13423
13424   return t;
13425 }
13426
13427 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13428    errors but we modify NODE so that it contains the type computed
13429    according to the expression, when it's fixed. Otherwise, we write
13430    error_mark_node as the type. It allows us to further the analysis
13431    of remaining nodes and detects more errors in certain cases.  */
13432
13433 static tree
13434 patch_binop (tree node, tree wfl_op1, tree wfl_op2)
13435 {
13436   tree op1 = TREE_OPERAND (node, 0);
13437   tree op2 = TREE_OPERAND (node, 1);
13438   tree op1_type = TREE_TYPE (op1);
13439   tree op2_type = TREE_TYPE (op2);
13440   tree prom_type = NULL_TREE, cn;
13441   enum tree_code code = TREE_CODE (node);
13442
13443   /* If 1, tell the routine that we have to return error_mark_node
13444      after checking for the initialization of the RHS */
13445   int error_found = 0;
13446
13447   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13448
13449   /* If either op<n>_type are NULL, this might be early signs of an
13450      error situation, unless it's too early to tell (in case we're
13451      handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13452      correctly so the error can be later on reported accurately. */
13453   if (! (code == PLUS_EXPR || code == NE_EXPR
13454          || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13455     {
13456       tree n;
13457       if (! op1_type)
13458         {
13459           n = java_complete_tree (op1);
13460           op1_type = TREE_TYPE (n);
13461         }
13462       if (! op2_type)
13463         {
13464           n = java_complete_tree (op2);
13465           op2_type = TREE_TYPE (n);
13466         }
13467     }
13468
13469   switch (code)
13470     {
13471     /* 15.16 Multiplicative operators */
13472     case MULT_EXPR:             /* 15.16.1 Multiplication Operator * */
13473     case RDIV_EXPR:             /* 15.16.2 Division Operator / */
13474     case TRUNC_DIV_EXPR:        /* 15.16.2 Integral type Division Operator / */
13475     case TRUNC_MOD_EXPR:        /* 15.16.3 Remainder operator % */
13476       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13477         {
13478           if (!JNUMERIC_TYPE_P (op1_type))
13479             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13480           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13481             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13482           TREE_TYPE (node) = error_mark_node;
13483           error_found = 1;
13484           break;
13485         }
13486       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13487
13488       /* Detect integral division by zero */
13489       if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13490           && TREE_CODE (prom_type) == INTEGER_TYPE
13491           && (op2 == integer_zero_node || op2 == long_zero_node ||
13492               (TREE_CODE (op2) == INTEGER_CST &&
13493                ! TREE_INT_CST_LOW (op2)  && ! TREE_INT_CST_HIGH (op2))))
13494         {
13495           parse_warning_context
13496             (wfl_operator,
13497              "Evaluating this expression will result in an arithmetic exception being thrown");
13498           TREE_CONSTANT (node) = 0;
13499           TREE_INVARIANT (node) = 0;
13500         }
13501
13502       /* Change the division operator if necessary */
13503       if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13504         TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13505
13506       /* Before divisions as is disappear, try to simplify and bail if
13507          applicable, otherwise we won't perform even simple
13508          simplifications like (1-1)/3. We can't do that with floating
13509          point number, folds can't handle them at this stage. */
13510       if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13511           && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13512         {
13513           TREE_TYPE (node) = prom_type;
13514           node = fold (node);
13515           if (TREE_CODE (node) != code)
13516             return node;
13517         }
13518
13519       if (TREE_CODE (prom_type) == INTEGER_TYPE
13520           && flag_use_divide_subroutine
13521           && ! flag_emit_class_files
13522           && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13523         return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13524
13525       /* This one is more complicated. FLOATs are processed by a
13526          function call to soft_fmod. Duplicate the value of the
13527          COMPOUND_ASSIGN_P flag. */
13528       if (code == TRUNC_MOD_EXPR)
13529         {
13530           tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13531           COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13532           return mod;
13533         }
13534       break;
13535
13536     /* 15.17 Additive Operators */
13537     case PLUS_EXPR:             /* 15.17.1 String Concatenation Operator + */
13538
13539       /* Operation is valid if either one argument is a string
13540          constant, a String object or a StringBuffer crafted for the
13541          purpose of the a previous usage of the String concatenation
13542          operator */
13543
13544       if (TREE_CODE (op1) == STRING_CST
13545           || TREE_CODE (op2) == STRING_CST
13546           || JSTRING_TYPE_P (op1_type)
13547           || JSTRING_TYPE_P (op2_type)
13548           || IS_CRAFTED_STRING_BUFFER_P (op1)
13549           || IS_CRAFTED_STRING_BUFFER_P (op2))
13550         return build_string_concatenation (op1, op2);
13551
13552     case MINUS_EXPR:            /* 15.17.2 Additive Operators (+ and -) for
13553                                    Numeric Types */
13554       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13555         {
13556           if (!JNUMERIC_TYPE_P (op1_type))
13557             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13558           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13559             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13560           TREE_TYPE (node) = error_mark_node;
13561           error_found = 1;
13562           break;
13563         }
13564       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13565       break;
13566
13567     /* 15.18 Shift Operators */
13568     case LSHIFT_EXPR:
13569     case RSHIFT_EXPR:
13570     case URSHIFT_EXPR:
13571       if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13572         {
13573           if (!JINTEGRAL_TYPE_P (op1_type))
13574             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13575           else
13576             {
13577               if (JNUMERIC_TYPE_P (op2_type))
13578                 parse_error_context (wfl_operator,
13579                                      "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13580                                      operator_string (node),
13581                                      lang_printable_name (op2_type, 0));
13582               else
13583                 parse_error_context (wfl_operator,
13584                                      "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13585                                      operator_string (node),
13586                                      lang_printable_name (op2_type, 0));
13587             }
13588           TREE_TYPE (node) = error_mark_node;
13589           error_found = 1;
13590           break;
13591         }
13592
13593       /* Unary numeric promotion (5.6.1) is performed on each operand
13594          separately */
13595       op1 = do_unary_numeric_promotion (op1);
13596       op2 = do_unary_numeric_promotion (op2);
13597
13598       /* If the right hand side is of type `long', first cast it to
13599          `int'.  */
13600       if (TREE_TYPE (op2) == long_type_node)
13601         op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13602
13603       /* The type of the shift expression is the type of the promoted
13604          type of the left-hand operand */
13605       prom_type = TREE_TYPE (op1);
13606
13607       /* Shift int only up to 0x1f and long up to 0x3f */
13608       if (prom_type == int_type_node)
13609         op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13610                             build_int_cst (NULL_TREE, 0x1f)));
13611       else
13612         op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13613                             build_int_cst (NULL_TREE, 0x3f)));
13614
13615       /* The >>> operator is a >> operating on unsigned quantities */
13616       if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13617         {
13618           tree to_return;
13619           tree utype = java_unsigned_type (prom_type);
13620           op1 = convert (utype, op1);
13621           TREE_SET_CODE (node, RSHIFT_EXPR);
13622           TREE_OPERAND (node, 0) = op1;
13623           TREE_OPERAND (node, 1) = op2;
13624           TREE_TYPE (node) = utype;
13625           to_return = convert (prom_type, node);
13626           /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13627           COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13628           TREE_SIDE_EFFECTS (to_return)
13629             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13630           return to_return;
13631         }
13632       break;
13633
13634       /* 15.19.1 Type Comparison Operator instanceof */
13635     case INSTANCEOF_EXPR:
13636
13637       TREE_TYPE (node) = boolean_type_node;
13638
13639       /* OP1_TYPE might be NULL when OP1 is a string constant.  */
13640       if ((cn = patch_string (op1)))
13641         {
13642           op1 = cn;
13643           op1_type = TREE_TYPE (op1);
13644         }
13645       if (op1_type == NULL_TREE)
13646         abort ();
13647
13648       if (!(op2_type = resolve_type_during_patch (op2)))
13649         return error_mark_node;
13650
13651       /* The first operand must be a reference type or the null type */
13652       if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13653         error_found = 1;        /* Error reported further below */
13654
13655       /* The second operand must be a reference type */
13656       if (!JREFERENCE_TYPE_P (op2_type))
13657         {
13658           SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13659           parse_error_context
13660             (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13661              lang_printable_name (op2_type, 0));
13662           error_found = 1;
13663         }
13664
13665       if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13666         {
13667           /* If the first operand is null, the result is always false */
13668           if (op1 == null_pointer_node)
13669             return boolean_false_node;
13670           else if (flag_emit_class_files)
13671             {
13672               TREE_OPERAND (node, 1) = op2_type;
13673               TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13674               return node;
13675             }
13676           /* Otherwise we have to invoke instance of to figure it out */
13677           else
13678             return build_instanceof (op1, op2_type);
13679         }
13680       /* There is no way the expression operand can be an instance of
13681          the type operand. This is a compile time error. */
13682       else
13683         {
13684           char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13685           SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13686           parse_error_context
13687             (wfl_operator, "Impossible for %qs to be instance of %qs",
13688              t1, lang_printable_name (op2_type, 0));
13689           free (t1);
13690           error_found = 1;
13691         }
13692
13693       break;
13694
13695       /* 15.21 Bitwise and Logical Operators */
13696     case BIT_AND_EXPR:
13697     case BIT_XOR_EXPR:
13698     case BIT_IOR_EXPR:
13699       if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13700         /* Binary numeric promotion is performed on both operand and the
13701            expression retain that type */
13702         prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13703
13704       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13705                && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13706         /* The type of the bitwise operator expression is BOOLEAN */
13707         prom_type = boolean_type_node;
13708       else
13709         {
13710           if (!JINTEGRAL_TYPE_P (op1_type))
13711             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13712           if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13713             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13714           TREE_TYPE (node) = error_mark_node;
13715           error_found = 1;
13716           /* Insert a break here if adding thing before the switch's
13717              break for this case */
13718         }
13719       break;
13720
13721       /* 15.22 Conditional-And Operator */
13722     case TRUTH_ANDIF_EXPR:
13723       /* 15.23 Conditional-Or Operator */
13724     case TRUTH_ORIF_EXPR:
13725       /* Operands must be of BOOLEAN type */
13726       if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13727           TREE_CODE (op2_type) != BOOLEAN_TYPE)
13728         {
13729           if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13730             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13731           if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13732             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13733           TREE_TYPE (node) = boolean_type_node;
13734           error_found = 1;
13735           break;
13736         }
13737       else if (integer_zerop (op1))
13738         {
13739           return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13740         }
13741       else if (integer_onep (op1))
13742         {
13743           return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13744         }
13745       /* The type of the conditional operators is BOOLEAN */
13746       prom_type = boolean_type_node;
13747       break;
13748
13749       /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13750     case LT_EXPR:
13751     case GT_EXPR:
13752     case LE_EXPR:
13753     case GE_EXPR:
13754       /* The type of each of the operands must be a primitive numeric
13755          type */
13756       if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13757         {
13758           if (!JNUMERIC_TYPE_P (op1_type))
13759             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13760           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13761             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13762           TREE_TYPE (node) = boolean_type_node;
13763           error_found = 1;
13764           break;
13765         }
13766       /* Binary numeric promotion is performed on the operands */
13767       binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13768       /* The type of the relation expression is always BOOLEAN */
13769       prom_type = boolean_type_node;
13770       break;
13771
13772       /* 15.20 Equality Operator */
13773     case EQ_EXPR:
13774     case NE_EXPR:
13775       /* It's time for us to patch the strings. */
13776       if ((cn = patch_string (op1)))
13777        {
13778          op1 = cn;
13779          op1_type = TREE_TYPE (op1);
13780        }
13781       if ((cn = patch_string (op2)))
13782        {
13783          op2 = cn;
13784          op2_type = TREE_TYPE (op2);
13785        }
13786
13787       /* 15.20.1 Numerical Equality Operators == and != */
13788       /* Binary numeric promotion is performed on the operands */
13789       if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13790         binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13791
13792       /* 15.20.2 Boolean Equality Operators == and != */
13793       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13794           TREE_CODE (op2_type) == BOOLEAN_TYPE)
13795         ;                       /* Nothing to do here */
13796
13797       /* 15.20.3 Reference Equality Operators == and != */
13798       /* Types have to be either references or the null type. If
13799          they're references, it must be possible to convert either
13800          type to the other by casting conversion. */
13801       else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13802                || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13803                || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13804                || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13805                    && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13806                        || valid_ref_assignconv_cast_p (op2_type,
13807                                                        op1_type, 1))))
13808         ;                       /* Nothing to do here */
13809
13810       /* Else we have an error figure what can't be converted into
13811          what and report the error */
13812       else
13813         {
13814           char *t1;
13815           t1 = xstrdup (lang_printable_name (op1_type, 0));
13816           parse_error_context
13817             (wfl_operator,
13818              "Incompatible type for %qs. Can't convert %qs to %qs",
13819              operator_string (node), t1,
13820              lang_printable_name (op2_type, 0));
13821           free (t1);
13822           TREE_TYPE (node) = boolean_type_node;
13823           error_found = 1;
13824           break;
13825         }
13826       prom_type = boolean_type_node;
13827       break;
13828     default:
13829       abort ();
13830     }
13831
13832   if (error_found)
13833     return error_mark_node;
13834
13835   TREE_OPERAND (node, 0) = op1;
13836   TREE_OPERAND (node, 1) = op2;
13837   TREE_TYPE (node) = prom_type;
13838   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13839
13840   if (flag_emit_xref)
13841     return node;
13842
13843   /* fold does not respect side-effect order as required for Java but not C.
13844    * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13845    * bytecode.
13846    */
13847   if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13848       : ! TREE_SIDE_EFFECTS (node))
13849     node = fold (node);
13850   return node;
13851 }
13852
13853 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13854    zero value, the value of CSTE comes after the valude of STRING */
13855
13856 static tree
13857 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13858 {
13859   const char *old = TREE_STRING_POINTER (cste);
13860   int old_len = TREE_STRING_LENGTH (cste);
13861   int len = old_len + string_len;
13862   char *new = alloca (len+1);
13863
13864   if (after)
13865     {
13866       memcpy (new, string, string_len);
13867       memcpy (&new [string_len], old, old_len);
13868     }
13869   else
13870     {
13871       memcpy (new, old, old_len);
13872       memcpy (&new [old_len], string, string_len);
13873     }
13874   new [len] = '\0';
13875   return build_string (len, new);
13876 }
13877
13878 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13879    new STRING_CST on success, NULL_TREE on failure.  */
13880
13881 static tree
13882 merge_string_cste (tree op1, tree op2, int after)
13883 {
13884   /* Handle two string constants right away.  */
13885   if (TREE_CODE (op2) == STRING_CST)
13886     return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13887                                  TREE_STRING_LENGTH (op2), after);
13888
13889   /* Reasonable integer constant can be treated right away.  */
13890   if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13891     {
13892       static const char *const boolean_true = "true";
13893       static const char *const boolean_false = "false";
13894       static const char *const null_pointer = "null";
13895       char ch[4];
13896       const char *string;
13897
13898       if (op2 == boolean_true_node)
13899         string = boolean_true;
13900       else if (op2 == boolean_false_node)
13901         string = boolean_false;
13902       else if (op2 == null_pointer_node
13903                || (integer_zerop (op2)
13904                    && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13905         /* FIXME: null is not a compile-time constant, so it is only safe to
13906            merge if the overall expression is non-constant. However, this
13907            code always merges without checking the overall expression.  */
13908         string = null_pointer;
13909       else if (TREE_TYPE (op2) == char_type_node)
13910         {
13911           /* Convert the character into UTF-8.  */
13912           unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13913           unsigned char *p = (unsigned char *) ch;
13914           if (0x01 <= c && c <= 0x7f)
13915             *p++ = (unsigned char) c;
13916           else if (c < 0x7ff)
13917             {
13918               *p++ = (unsigned char) (c >> 6 | 0xc0);
13919               *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13920             }
13921           else
13922             {
13923               *p++ = (unsigned char) (c >> 12 | 0xe0);
13924               *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13925               *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13926             }
13927           *p = '\0';
13928
13929           string = ch;
13930         }
13931       else
13932         string = string_convert_int_cst (op2);
13933
13934       return do_merge_string_cste (op1, string, strlen (string), after);
13935     }
13936   return NULL_TREE;
13937 }
13938
13939 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13940    has to be a STRING_CST and the other part must be a STRING_CST or a
13941    INTEGRAL constant. Return a new STRING_CST if the operation
13942    succeed, NULL_TREE otherwise.
13943
13944    If the case we want to optimize for space, we might want to return
13945    NULL_TREE for each invocation of this routine. FIXME */
13946
13947 static tree
13948 string_constant_concatenation (tree op1, tree op2)
13949 {
13950   if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13951     {
13952       tree string, rest;
13953       int invert;
13954
13955       string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13956       rest   = (string == op1 ? op2 : op1);
13957       invert = (string == op1 ? 0 : 1 );
13958
13959       /* Walk REST, only if it looks reasonable */
13960       if (TREE_CODE (rest) != STRING_CST
13961           && !IS_CRAFTED_STRING_BUFFER_P (rest)
13962           && !JSTRING_TYPE_P (TREE_TYPE (rest))
13963           && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13964         {
13965           rest = java_complete_tree (rest);
13966           if (rest == error_mark_node)
13967             return error_mark_node;
13968           rest = fold (rest);
13969         }
13970       return merge_string_cste (string, rest, invert);
13971     }
13972   return NULL_TREE;
13973 }
13974
13975 /* Implement the `+' operator. Does static optimization if possible,
13976    otherwise create (if necessary) and append elements to a
13977    StringBuffer. The StringBuffer will be carried around until it is
13978    used for a function call or an assignment. Then toString() will be
13979    called on it to turn it into a String object. */
13980
13981 static tree
13982 build_string_concatenation (tree op1, tree op2)
13983 {
13984   tree result;
13985   int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13986
13987   if (flag_emit_xref)
13988     return build2 (PLUS_EXPR, string_type_node, op1, op2);
13989
13990   /* Try to do some static optimization */
13991   if ((result = string_constant_concatenation (op1, op2)))
13992     return result;
13993
13994   /* Discard empty strings on either side of the expression */
13995   if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13996     {
13997       op1 = op2;
13998       op2 = NULL_TREE;
13999     }
14000   else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14001     op2 = NULL_TREE;
14002
14003   /* If operands are string constant, turn then into object references */
14004   if (TREE_CODE (op1) == STRING_CST)
14005     op1 = patch_string_cst (op1);
14006   if (op2 && TREE_CODE (op2) == STRING_CST)
14007     op2 = patch_string_cst (op2);
14008
14009   /* If either one of the constant is null and the other non null
14010      operand is a String constant, return it. */
14011   if ((TREE_CODE (op1) == STRING_CST) && !op2)
14012     return op1;
14013
14014   /* If OP1 isn't already a StringBuffer, create and
14015      initialize a new one */
14016   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14017     {
14018       /* Two solutions here:
14019          1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14020          2) OP1 is something else, we call new StringBuffer().append(OP1).  */
14021       if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14022         op1 = BUILD_STRING_BUFFER (op1);
14023       else
14024         {
14025           tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14026           op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14027         }
14028     }
14029
14030   if (op2)
14031     {
14032       /* OP1 is no longer the last node holding a crafted StringBuffer */
14033       IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14034       /* Create a node for `{new...,xxx}.append (op2)' */
14035       if (op2)
14036         op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14037     }
14038
14039   /* Mark the last node holding a crafted StringBuffer */
14040   IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14041
14042   TREE_SIDE_EFFECTS (op1) = side_effects;
14043   return op1;
14044 }
14045
14046 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14047    StringBuffer. If no string were found to be patched, return
14048    NULL. */
14049
14050 static tree
14051 patch_string (tree node)
14052 {
14053   if (node == error_mark_node)
14054     return error_mark_node;
14055   if (TREE_CODE (node) == STRING_CST)
14056     return patch_string_cst (node);
14057   else if (IS_CRAFTED_STRING_BUFFER_P (node))
14058     {
14059       int saved = ctxp->explicit_constructor_p;
14060       tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14061       tree ret;
14062       /* Temporary disable forbid the use of `this'. */
14063       ctxp->explicit_constructor_p = 0;
14064       ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14065       /* String concatenation arguments must be evaluated in order too. */
14066       ret = force_evaluation_order (ret);
14067       /* Restore it at its previous value */
14068       ctxp->explicit_constructor_p = saved;
14069       return ret;
14070     }
14071   return NULL_TREE;
14072 }
14073
14074 /* Build the internal representation of a string constant.  */
14075
14076 static tree
14077 patch_string_cst (tree node)
14078 {
14079   int location;
14080   if (! flag_emit_class_files)
14081     {
14082       node = get_identifier (TREE_STRING_POINTER (node));
14083       location = alloc_name_constant (CONSTANT_String, node);
14084       node = build_ref_from_constant_pool (location);
14085     }
14086   TREE_CONSTANT (node) = 1;
14087   TREE_INVARIANT (node) = 1;
14088
14089   /* ??? Guessing that the class file code can't handle casts.  */
14090   if (! flag_emit_class_files)
14091     node = convert (string_ptr_type_node, node);
14092   else
14093     TREE_TYPE (node) = string_ptr_type_node;
14094
14095   return node;
14096 }
14097
14098 /* Build an incomplete unary operator expression. */
14099
14100 static tree
14101 build_unaryop (int op_token, int op_location, tree op1)
14102 {
14103   enum tree_code op;
14104   tree unaryop;
14105   switch (op_token)
14106     {
14107     case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14108     case MINUS_TK: op = NEGATE_EXPR; break;
14109     case NEG_TK: op = TRUTH_NOT_EXPR; break;
14110     case NOT_TK: op = BIT_NOT_EXPR; break;
14111     default: abort ();
14112     }
14113
14114   unaryop = build1 (op, NULL_TREE, op1);
14115   TREE_SIDE_EFFECTS (unaryop) = 1;
14116   /* Store the location of the operator, for better error report. The
14117      string of the operator will be rebuild based on the OP value. */
14118   EXPR_WFL_LINECOL (unaryop) = op_location;
14119   return unaryop;
14120 }
14121
14122 /* Special case for the ++/-- operators, since they require an extra
14123    argument to build, which is set to NULL and patched
14124    later. IS_POST_P is 1 if the operator, 0 otherwise.  */
14125
14126 static tree
14127 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14128 {
14129   static const enum tree_code lookup [2][2] =
14130     {
14131       { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14132       { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14133     };
14134   tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14135                       NULL_TREE, op1, NULL_TREE);
14136   TREE_SIDE_EFFECTS (node) = 1;
14137   /* Store the location of the operator, for better error report. The
14138      string of the operator will be rebuild based on the OP value. */
14139   EXPR_WFL_LINECOL (node) = op_location;
14140   return node;
14141 }
14142
14143 /* Build an incomplete cast operator, based on the use of the
14144    CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14145    set. java_complete_tree is trained to walk a CONVERT_EXPR even
14146    though its type is already set.  */
14147
14148 static tree
14149 build_cast (int location, tree type, tree exp)
14150 {
14151   tree node = build1 (CONVERT_EXPR, type, exp);
14152   EXPR_WFL_LINECOL (node) = location;
14153   return node;
14154 }
14155
14156 /* Build an incomplete class reference operator.  */
14157 static tree
14158 build_incomplete_class_ref (int location, tree class_name)
14159 {
14160   tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14161   tree class_decl = GET_CPC ();
14162   tree this_class = TREE_TYPE (class_decl);
14163
14164   /* Generate the synthetic static method `class$'.  (Previously we
14165      deferred this, causing different method tables to be emitted
14166      for native code and bytecode.)  */
14167   if (!TYPE_DOT_CLASS (this_class)
14168       && !JPRIMITIVE_TYPE_P (class_name)
14169       && !(TREE_CODE (class_name) == VOID_TYPE))
14170     {
14171       tree cpc_list = GET_CPC_LIST();
14172       tree cpc = cpc_list;
14173       tree target_class;
14174
14175       /* For inner classes, add a 'class$' method to their outermost
14176          context, creating it if necessary.  */
14177       
14178       while (GET_NEXT_ENCLOSING_CPC(cpc))
14179         cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14180       class_decl = TREE_VALUE (cpc);
14181
14182       target_class = TREE_TYPE (class_decl);
14183
14184       if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14185         {
14186           /* For interfaces, adding a static 'class$' method directly 
14187              is illegal.  So create an inner class to contain the new
14188              method.  Empirically this matches the behavior of javac.  */
14189           tree t, inner;
14190           /* We want the generated inner class inside the outermost class. */
14191           GET_CPC_LIST() = cpc;
14192           t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14193           inner = create_anonymous_class (t);
14194           target_class = TREE_TYPE (inner);
14195           end_class_declaration (1);
14196           GET_CPC_LIST() = cpc_list;
14197         }
14198
14199       if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14200         build_dot_class_method (target_class);
14201
14202       if (this_class != target_class)
14203         TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14204     }
14205
14206   EXPR_WFL_LINECOL (node) = location;
14207   return node;
14208 }
14209
14210 /* Complete an incomplete class reference operator.  */
14211 static tree
14212 patch_incomplete_class_ref (tree node)
14213 {
14214   tree type = TREE_OPERAND (node, 0);
14215   tree ref_type;
14216
14217   if (!(ref_type = resolve_type_during_patch (type)))
14218     return error_mark_node;
14219
14220   /* If we're not emitting class files and we know ref_type is a
14221      compiled class, build a direct reference.  */
14222   if ((! flag_emit_class_files && is_compiled_class (ref_type))
14223       || JPRIMITIVE_TYPE_P (ref_type)
14224       || TREE_CODE (ref_type) == VOID_TYPE)
14225     {
14226       tree dot = build_class_ref (ref_type);
14227       /* A class referenced by `foo.class' is initialized.  */
14228       if (!flag_emit_class_files)
14229        dot = build_class_init (ref_type, dot);
14230       return java_complete_tree (dot);
14231     }
14232
14233   /* If we're emitting class files and we have to deal with non
14234      primitive types, we invoke the synthetic static method `class$'.  */
14235   ref_type = build_dot_class_method_invocation (current_class, ref_type);
14236   return java_complete_tree (ref_type);
14237 }
14238
14239 /* 15.14 Unary operators. We return error_mark_node in case of error,
14240    but preserve the type of NODE if the type is fixed.  */
14241
14242 static tree
14243 patch_unaryop (tree node, tree wfl_op)
14244 {
14245   tree op = TREE_OPERAND (node, 0);
14246   tree op_type = TREE_TYPE (op);
14247   tree prom_type = NULL_TREE, value, decl;
14248   int outer_field_flag = 0;
14249   int code = TREE_CODE (node);
14250   int error_found = 0;
14251
14252   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14253
14254   switch (code)
14255     {
14256       /* 15.13.2 Postfix Increment Operator ++ */
14257     case POSTINCREMENT_EXPR:
14258       /* 15.13.3 Postfix Increment Operator -- */
14259     case POSTDECREMENT_EXPR:
14260       /* 15.14.1 Prefix Increment Operator ++ */
14261     case PREINCREMENT_EXPR:
14262       /* 15.14.2 Prefix Decrement Operator -- */
14263     case PREDECREMENT_EXPR:
14264       op = decl = extract_field_decl (op);
14265       outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14266       /* We might be trying to change an outer field accessed using
14267          access method. */
14268       if (outer_field_flag)
14269         {
14270           /* Retrieve the decl of the field we're trying to access. We
14271              do that by first retrieving the function we would call to
14272              access the field. It has been already verified that this
14273              field isn't final */
14274           if (flag_emit_class_files)
14275             decl = TREE_OPERAND (op, 0);
14276           else
14277             decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14278           decl = DECL_FUNCTION_ACCESS_DECL (decl);
14279         }
14280       /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14281       else if (!JDECL_P (decl)
14282           && TREE_CODE (decl) != COMPONENT_REF
14283           && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14284           && TREE_CODE (decl) != INDIRECT_REF
14285           && !(TREE_CODE (decl) == COMPOUND_EXPR
14286                && TREE_OPERAND (decl, 1)
14287                && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14288         {
14289           TREE_TYPE (node) = error_mark_node;
14290           error_found = 1;
14291         }
14292
14293       /* From now on, we know that op if a variable and that it has a
14294          valid wfl. We use wfl_op to locate errors related to the
14295          ++/-- operand. */
14296       if (!JNUMERIC_TYPE_P (op_type))
14297         {
14298           parse_error_context
14299             (wfl_op, "Invalid argument type %qs to %qs",
14300              lang_printable_name (op_type, 0), operator_string (node));
14301           TREE_TYPE (node) = error_mark_node;
14302           error_found = 1;
14303         }
14304       else
14305         {
14306           /* Before the addition, binary numeric promotion is performed on
14307              both operands, if really necessary */
14308           if (JINTEGRAL_TYPE_P (op_type))
14309             {
14310               value = build_int_cst (op_type, 1);
14311               TREE_TYPE (node) = op_type;
14312             }
14313           else
14314             {
14315               value = build_int_cst (NULL_TREE, 1);
14316               TREE_TYPE (node) =
14317                 binary_numeric_promotion (op_type,
14318                                           TREE_TYPE (value), &op, &value);
14319             }
14320
14321           /* We remember we might be accessing an outer field */
14322           if (outer_field_flag)
14323             {
14324               /* We re-generate an access to the field */
14325               value = build2 (PLUS_EXPR, TREE_TYPE (op),
14326                               build_outer_field_access (wfl_op, decl), value);
14327
14328               /* And we patch the original access$() into a write
14329                  with plus_op as a rhs */
14330               return outer_field_access_fix (node, op, value);
14331             }
14332
14333           /* And write back into the node. */
14334           TREE_OPERAND (node, 0) = op;
14335           TREE_OPERAND (node, 1) = value;
14336           /* Convert the overall back into its original type, if
14337              necessary, and return */
14338           if (JINTEGRAL_TYPE_P (op_type))
14339             return fold (node);
14340           else
14341             return fold (convert (op_type, node));
14342         }
14343       break;
14344
14345       /* 15.14.3 Unary Plus Operator + */
14346     case UNARY_PLUS_EXPR:
14347       /* 15.14.4 Unary Minus Operator - */
14348     case NEGATE_EXPR:
14349       if (!JNUMERIC_TYPE_P (op_type))
14350         {
14351           ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14352           TREE_TYPE (node) = error_mark_node;
14353           error_found = 1;
14354         }
14355       /* Unary numeric promotion is performed on operand */
14356       else
14357         {
14358           op = do_unary_numeric_promotion (op);
14359           prom_type = TREE_TYPE (op);
14360           if (code == UNARY_PLUS_EXPR)
14361             return fold (op);
14362         }
14363       break;
14364
14365       /* 15.14.5 Bitwise Complement Operator ~ */
14366     case BIT_NOT_EXPR:
14367       if (!JINTEGRAL_TYPE_P (op_type))
14368         {
14369           ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14370           TREE_TYPE (node) = error_mark_node;
14371           error_found = 1;
14372         }
14373       else
14374         {
14375           op = do_unary_numeric_promotion (op);
14376           prom_type = TREE_TYPE (op);
14377         }
14378       break;
14379
14380       /* 15.14.6 Logical Complement Operator ! */
14381     case TRUTH_NOT_EXPR:
14382       if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14383         {
14384           ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14385           /* But the type is known. We will report an error if further
14386              attempt of a assignment is made with this rhs */
14387           TREE_TYPE (node) = boolean_type_node;
14388           error_found = 1;
14389         }
14390       else
14391         prom_type = boolean_type_node;
14392       break;
14393
14394       /* 15.15 Cast Expression */
14395     case CONVERT_EXPR:
14396       value = patch_cast (node, wfl_operator);
14397       if (value == error_mark_node)
14398         {
14399           /* If this cast is part of an assignment, we tell the code
14400              that deals with it not to complain about a mismatch,
14401              because things have been cast, anyways */
14402           TREE_TYPE (node) = error_mark_node;
14403           error_found = 1;
14404         }
14405       else
14406         {
14407           value = fold (value);
14408           return value;
14409         }
14410       break;
14411     }
14412
14413   if (error_found)
14414     return error_mark_node;
14415
14416   /* There are cases where node has been replaced by something else
14417      and we don't end up returning here: UNARY_PLUS_EXPR,
14418      CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14419   TREE_OPERAND (node, 0) = fold (op);
14420   TREE_TYPE (node) = prom_type;
14421   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14422   return fold (node);
14423 }
14424
14425 /* Generic type resolution that sometimes takes place during node
14426    patching. Returned the resolved type or generate an error
14427    message. Return the resolved type or NULL_TREE.  */
14428
14429 static tree
14430 resolve_type_during_patch (tree type)
14431 {
14432   if (unresolved_type_p (type, NULL))
14433     {
14434       tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14435       if (!type_decl)
14436         {
14437           parse_error_context (type,
14438                                "Class %qs not found in type declaration",
14439                                IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14440           return NULL_TREE;
14441         }
14442
14443       check_deprecation (type, type_decl);
14444
14445       return TREE_TYPE (type_decl);
14446     }
14447   return type;
14448 }
14449
14450 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14451    found. Otherwise NODE or something meant to replace it is returned.  */
14452
14453 static tree
14454 patch_cast (tree node, tree wfl_op)
14455 {
14456   tree op = TREE_OPERAND (node, 0);
14457   tree cast_type = TREE_TYPE (node);
14458   tree patched, op_type;
14459   char *t1;
14460
14461   /* Some string patching might be necessary at this stage */
14462   if ((patched = patch_string (op)))
14463     TREE_OPERAND (node, 0) = op = patched;
14464   op_type = TREE_TYPE (op);
14465
14466   /* First resolve OP_TYPE if unresolved */
14467   if (!(cast_type = resolve_type_during_patch (cast_type)))
14468     return error_mark_node;
14469
14470   /* Check on cast that are proven correct at compile time */
14471   if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14472     {
14473       /* Same type */
14474       if (cast_type == op_type)
14475         return node;
14476
14477       /* A narrowing conversion from a floating-point number to an
14478          integral type requires special handling (5.1.3).  */
14479       if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14480         if (cast_type != long_type_node)
14481           op = convert (integer_type_node, op);
14482
14483       /* Try widening/narrowing conversion.  Potentially, things need
14484          to be worked out in gcc so we implement the extreme cases
14485          correctly.  fold_convert() needs to be fixed.  */
14486       return convert (cast_type, op);
14487     }
14488
14489   /* It's also valid to cast a boolean into a boolean */
14490   if (op_type == boolean_type_node && cast_type == boolean_type_node)
14491     return node;
14492
14493   /* null can be casted to references */
14494   if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14495     return build_null_of_type (cast_type);
14496
14497   /* The remaining legal casts involve conversion between reference
14498      types. Check for their compile time correctness. */
14499   if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14500       && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14501     {
14502       TREE_TYPE (node) = promote_type (cast_type);
14503       /* Now, the case can be determined correct at compile time if
14504          OP_TYPE can be converted into CAST_TYPE by assignment
14505          conversion (5.2) */
14506
14507       if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14508         {
14509           TREE_SET_CODE (node, NOP_EXPR);
14510           return node;
14511         }
14512
14513       if (flag_emit_class_files)
14514         {
14515           TREE_SET_CODE (node, CONVERT_EXPR);
14516           return node;
14517         }
14518
14519       /* The cast requires a run-time check */
14520       return build3 (CALL_EXPR, promote_type (cast_type),
14521                      build_address_of (soft_checkcast_node),
14522                      tree_cons (NULL_TREE, build_class_ref (cast_type),
14523                                 build_tree_list (NULL_TREE, op)),
14524                      NULL_TREE);
14525     }
14526
14527   /* Any other casts are proven incorrect at compile time */
14528   t1 = xstrdup (lang_printable_name (op_type, 0));
14529   parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14530                        t1, lang_printable_name (cast_type, 0));
14531   free (t1);
14532   return error_mark_node;
14533 }
14534
14535 /* Build a null constant and give it the type TYPE.  */
14536
14537 static tree
14538 build_null_of_type (tree type)
14539 {
14540   tree node = build_int_cst (promote_type (type), 0);
14541   return node;
14542 }
14543
14544 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14545    a list of indices. */
14546 static tree
14547 build_array_ref (int location, tree array, tree index)
14548 {
14549   tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14550                       NULL_TREE, NULL_TREE);
14551   EXPR_WFL_LINECOL (node) = location;
14552   return node;
14553 }
14554
14555 /* 15.12 Array Access Expression */
14556
14557 static tree
14558 patch_array_ref (tree node)
14559 {
14560   tree array = TREE_OPERAND (node, 0);
14561   tree array_type  = TREE_TYPE (array);
14562   tree index = TREE_OPERAND (node, 1);
14563   tree index_type = TREE_TYPE (index);
14564   int error_found = 0;
14565
14566   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14567
14568   if (TREE_CODE (array_type) == POINTER_TYPE)
14569     array_type = TREE_TYPE (array_type);
14570
14571   /* The array reference must be an array */
14572   if (!TYPE_ARRAY_P (array_type))
14573     {
14574       parse_error_context
14575         (wfl_operator,
14576          "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14577          lang_printable_name (array_type, 0));
14578       TREE_TYPE (node) = error_mark_node;
14579       error_found = 1;
14580     }
14581
14582   /* The array index undergoes unary numeric promotion. The promoted
14583      type must be int */
14584   index = do_unary_numeric_promotion (index);
14585   if (TREE_TYPE (index) != int_type_node)
14586     {
14587       if (valid_cast_to_p (index_type, int_type_node))
14588         parse_error_context (wfl_operator,
14589    "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14590                              lang_printable_name (index_type, 0));
14591       else
14592         parse_error_context (wfl_operator,
14593           "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14594                              lang_printable_name (index_type, 0));
14595       TREE_TYPE (node) = error_mark_node;
14596       error_found = 1;
14597     }
14598
14599   if (error_found)
14600     return error_mark_node;
14601
14602   array_type = TYPE_ARRAY_ELEMENT (array_type);
14603
14604   if (flag_emit_class_files || flag_emit_xref)
14605     {
14606       TREE_OPERAND (node, 0) = array;
14607       TREE_OPERAND (node, 1) = index;
14608     }
14609   else
14610     node = build_java_arrayaccess (array, array_type, index);
14611   TREE_TYPE (node) = array_type;
14612   return node;
14613 }
14614
14615 /* 15.9 Array Creation Expressions */
14616
14617 static tree
14618 build_newarray_node (tree type, tree dims, int extra_dims)
14619 {
14620   tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14621                       nreverse (dims),
14622                       build_int_cst (NULL_TREE, extra_dims));
14623   return node;
14624 }
14625
14626 static tree
14627 patch_newarray (tree node)
14628 {
14629   tree type = TREE_OPERAND (node, 0);
14630   tree dims = TREE_OPERAND (node, 1);
14631   tree cdim, array_type;
14632   int error_found = 0;
14633   int ndims = 0;
14634   int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14635
14636   /* Dimension types are verified. It's better for the types to be
14637      verified in order. */
14638   for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14639     {
14640       int dim_error = 0;
14641       tree dim = TREE_VALUE (cdim);
14642
14643       /* Dim might have been saved during its evaluation */
14644       dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14645
14646       /* The type of each specified dimension must be an integral type. */
14647       if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14648         dim_error = 1;
14649
14650       /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14651          promoted type must be int. */
14652       else
14653         {
14654           dim = do_unary_numeric_promotion (dim);
14655           if (TREE_TYPE (dim) != int_type_node)
14656             dim_error = 1;
14657         }
14658
14659       /* Report errors on types here */
14660       if (dim_error)
14661         {
14662           parse_error_context
14663             (TREE_PURPOSE (cdim),
14664              "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14665              (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14666               "Explicit cast needed to" : "Can't"),
14667              lang_printable_name (TREE_TYPE (dim), 0));
14668           error_found = 1;
14669         }
14670
14671       TREE_PURPOSE (cdim) = NULL_TREE;
14672     }
14673
14674   /* Resolve array base type if unresolved */
14675   if (!(type = resolve_type_during_patch (type)))
14676     error_found = 1;
14677
14678   if (error_found)
14679     {
14680       /* We don't want further evaluation of this bogus array creation
14681          operation */
14682       TREE_TYPE (node) = error_mark_node;
14683       return error_mark_node;
14684     }
14685
14686   /* Set array_type to the actual (promoted) array type of the result. */
14687   if (TREE_CODE (type) == RECORD_TYPE)
14688     type = build_pointer_type (type);
14689   while (--xdims >= 0)
14690     {
14691       type = promote_type (build_java_array_type (type, -1));
14692     }
14693   dims = nreverse (dims);
14694   array_type = type;
14695   for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14696     {
14697       type = array_type;
14698       array_type
14699         = build_java_array_type (type,
14700                                  TREE_CODE (cdim) == INTEGER_CST
14701                                  ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14702                                  : -1);
14703       array_type = promote_type (array_type);
14704     }
14705   dims = nreverse (dims);
14706
14707   /* The node is transformed into a function call. Things are done
14708      differently according to the number of dimensions. If the number
14709      of dimension is equal to 1, then the nature of the base type
14710      (primitive or not) matters. */
14711   if (ndims == 1)
14712     return build_new_array (type, TREE_VALUE (dims));
14713
14714   /* Can't reuse what's already written in expr.c because it uses the
14715      JVM stack representation. Provide a build_multianewarray. FIXME */
14716   return build3 (CALL_EXPR, array_type,
14717                  build_address_of (soft_multianewarray_node),
14718                  tree_cons (NULL_TREE,
14719                             build_class_ref (TREE_TYPE (array_type)),
14720                             tree_cons (NULL_TREE,
14721                                        build_int_cst (NULL_TREE, ndims),
14722                                        dims)),
14723                  NULL_TREE);
14724 }
14725
14726 /* 10.6 Array initializer.  */
14727
14728 /* Build a wfl for array element that don't have one, so we can
14729    pin-point errors.  */
14730
14731 static tree
14732 maybe_build_array_element_wfl (tree node)
14733 {
14734   if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14735     {
14736       /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14737       return build_expr_wfl (NULL_TREE,
14738 #ifdef USE_MAPPED_LOCATION
14739                              input_location
14740 #else
14741                              ctxp->filename,
14742                              ctxp->lexer->token_start.line,
14743                              ctxp->lexer->token_start.col
14744 #endif
14745                              );
14746     }
14747   else
14748     return NULL_TREE;
14749 }
14750
14751 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14752    identification of initialized arrays easier to detect during walk
14753    and expansion.  */
14754
14755 static tree
14756 build_new_array_init (int location, tree values)
14757 {
14758   tree constructor = build_constructor (NULL_TREE, values);
14759   tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14760   EXPR_WFL_LINECOL (to_return) = location;
14761   return to_return;
14762 }
14763
14764 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14765    occurred.  Otherwise return NODE after having set its type
14766    appropriately.  */
14767
14768 static tree
14769 patch_new_array_init (tree type, tree node)
14770 {
14771   int error_seen = 0;
14772   tree current, element_type;
14773   HOST_WIDE_INT length;
14774   int all_constant = 1;
14775   tree init = TREE_OPERAND (node, 0);
14776
14777   if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14778     {
14779       parse_error_context (node,
14780                            "Invalid array initializer for non-array type %qs",
14781                            lang_printable_name (type, 1));
14782       return error_mark_node;
14783     }
14784   type = TREE_TYPE (type);
14785   element_type = TYPE_ARRAY_ELEMENT (type);
14786
14787   CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14788
14789   for (length = 0, current = CONSTRUCTOR_ELTS (init);
14790        current;  length++, current = TREE_CHAIN (current))
14791     {
14792       tree elt = TREE_VALUE (current);
14793       if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14794         {
14795           error_seen |= array_constructor_check_entry (element_type, current);
14796           elt = TREE_VALUE (current);
14797           /* When compiling to native code, STRING_CST is converted to
14798              INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14799           if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14800             all_constant = 0;
14801         }
14802       else
14803         {
14804           TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14805           TREE_PURPOSE (current) = NULL_TREE;
14806           all_constant = 0;
14807         }
14808       if (elt && TREE_CODE (elt) == TREE_LIST
14809           && TREE_VALUE (elt) == error_mark_node)
14810         error_seen = 1;
14811     }
14812
14813   if (error_seen)
14814     return error_mark_node;
14815
14816   /* Create a new type. We can't reuse the one we have here by
14817      patching its dimension because it originally is of dimension -1
14818      hence reused by gcc. This would prevent triangular arrays. */
14819   type = build_java_array_type (element_type, length);
14820   TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14821   TREE_TYPE (node) = promote_type (type);
14822   TREE_CONSTANT (init) = all_constant;
14823   TREE_INVARIANT (init) = all_constant;
14824   TREE_CONSTANT (node) = all_constant;
14825   TREE_INVARIANT (node) = all_constant;
14826   return node;
14827 }
14828
14829 /* Verify that one entry of the initializer element list can be
14830    assigned to the array base type. Report 1 if an error occurred, 0
14831    otherwise.  */
14832
14833 static int
14834 array_constructor_check_entry (tree type, tree entry)
14835 {
14836   char *array_type_string = NULL;       /* For error reports */
14837   tree value, type_value, new_value, wfl_value, patched;
14838   int error_seen = 0;
14839
14840   new_value = NULL_TREE;
14841   wfl_value = TREE_VALUE (entry);
14842
14843   value = java_complete_tree (TREE_VALUE (entry));
14844   /* patch_string return error_mark_node if arg is error_mark_node */
14845   if ((patched = patch_string (value)))
14846     value = patched;
14847   if (value == error_mark_node)
14848     return 1;
14849
14850   type_value = TREE_TYPE (value);
14851
14852   /* At anytime, try_builtin_assignconv can report a warning on
14853      constant overflow during narrowing. */
14854   SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14855   new_value = try_builtin_assignconv (wfl_operator, type, value);
14856   if (!new_value && (new_value = try_reference_assignconv (type, value)))
14857     type_value = promote_type (type);
14858
14859   /* Check and report errors */
14860   if (!new_value)
14861     {
14862       const char *const msg = (!valid_cast_to_p (type_value, type) ?
14863                    "Can't" : "Explicit cast needed to");
14864       if (!array_type_string)
14865         array_type_string = xstrdup (lang_printable_name (type, 1));
14866       parse_error_context
14867         (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14868          msg, lang_printable_name (type_value, 1), array_type_string);
14869       error_seen = 1;
14870     }
14871
14872   if (new_value)
14873     TREE_VALUE (entry) = new_value;
14874
14875   if (array_type_string)
14876     free (array_type_string);
14877
14878   TREE_PURPOSE (entry) = NULL_TREE;
14879   return error_seen;
14880 }
14881
14882 static tree
14883 build_this (int location)
14884 {
14885   tree node = build_wfl_node (this_identifier_node);
14886   TREE_SET_CODE (node, THIS_EXPR);
14887   EXPR_WFL_LINECOL (node) = location;
14888   return node;
14889 }
14890
14891 /* 14.15 The return statement. It builds a modify expression that
14892    assigns the returned value to the RESULT_DECL that hold the value
14893    to be returned. */
14894
14895 static tree
14896 build_return (int location, tree op)
14897 {
14898   tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14899   EXPR_WFL_LINECOL (node) = location;
14900   node = build_debugable_stmt (location, node);
14901   return node;
14902 }
14903
14904 static tree
14905 patch_return (tree node)
14906 {
14907   tree return_exp = TREE_OPERAND (node, 0);
14908   tree meth = current_function_decl;
14909   tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14910   int error_found = 0;
14911
14912   TREE_TYPE (node) = error_mark_node;
14913   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14914
14915   /* It's invalid to have a return value within a function that is
14916      declared with the keyword void or that is a constructor */
14917   if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14918     error_found = 1;
14919
14920   /* It's invalid to use a return statement in a static block */
14921   if (DECL_CLINIT_P (current_function_decl))
14922     error_found = 1;
14923
14924   /* It's invalid to have a no return value within a function that
14925      isn't declared with the keyword `void' */
14926   if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14927     error_found = 2;
14928
14929   if (DECL_INSTINIT_P (current_function_decl))
14930     error_found = 1;
14931
14932   if (error_found)
14933     {
14934       if (DECL_INSTINIT_P (current_function_decl))
14935         parse_error_context (wfl_operator,
14936                              "%<return%> inside instance initializer");
14937
14938       else if (DECL_CLINIT_P (current_function_decl))
14939         parse_error_context (wfl_operator,
14940                              "%<return%> inside static initializer");
14941
14942       else if (!DECL_CONSTRUCTOR_P (meth))
14943         {
14944           char *t = xstrdup (lang_printable_name (mtype, 0));
14945           parse_error_context (wfl_operator,
14946                                "%<return%> with%s value from %<%s %s%>",
14947                                (error_found == 1 ? "" : "out"),
14948                                t, lang_printable_name (meth, 2));
14949           free (t);
14950         }
14951       else
14952         parse_error_context (wfl_operator,
14953                              "%<return%> with value from constructor %qs",
14954                              lang_printable_name (meth, 2));
14955       return error_mark_node;
14956     }
14957
14958   /* If we have a return_exp, build a modify expression and expand
14959      it. Note: at that point, the assignment is declared valid, but we
14960      may want to carry some more hacks */
14961   if (return_exp)
14962     {
14963       tree exp = java_complete_tree (return_exp);
14964       tree modify, patched;
14965
14966       if ((patched = patch_string (exp)))
14967         exp = patched;
14968
14969       modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14970       EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14971       modify = java_complete_tree (modify);
14972
14973       if (modify != error_mark_node)
14974         {
14975           TREE_SIDE_EFFECTS (modify) = 1;
14976           TREE_OPERAND (node, 0) = modify;
14977         }
14978       else
14979         return error_mark_node;
14980     }
14981   TREE_TYPE (node) = void_type_node;
14982   TREE_SIDE_EFFECTS (node) = 1;
14983   return node;
14984 }
14985
14986 /* 14.8 The if Statement */
14987
14988 static tree
14989 build_if_else_statement (int location, tree expression, tree if_body,
14990                          tree else_body)
14991 {
14992   tree node;
14993   if (!else_body)
14994     else_body = build_java_empty_stmt ();
14995   node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14996   EXPR_WFL_LINECOL (node) = location;
14997   node = build_debugable_stmt (location, node);
14998   return node;
14999 }
15000
15001 static tree
15002 patch_if_else_statement (tree node)
15003 {
15004   tree expression = TREE_OPERAND (node, 0);
15005   int can_complete_normally
15006     = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15007        | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15008
15009   TREE_TYPE (node) = error_mark_node;
15010   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15011
15012   /* The type of expression must be boolean */
15013   if (TREE_TYPE (expression) != boolean_type_node
15014       && TREE_TYPE (expression) != promoted_boolean_type_node)
15015     {
15016       parse_error_context
15017         (wfl_operator,
15018          "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
15019          lang_printable_name (TREE_TYPE (expression), 0));
15020       return error_mark_node;
15021     }
15022
15023   TREE_TYPE (node) = void_type_node;
15024   TREE_SIDE_EFFECTS (node) = 1;
15025   CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15026   return node;
15027 }
15028
15029 /* 14.6 Labeled Statements */
15030
15031 /* Action taken when a labeled statement is parsed. a new
15032    LABELED_BLOCK_EXPR is created. No statement is attached to the
15033    label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
15034
15035 static tree
15036 build_labeled_block (int location, tree label)
15037 {
15038   tree label_name ;
15039   tree label_decl, node;
15040   if (label == NULL_TREE || label == continue_identifier_node)
15041     label_name = label;
15042   else
15043     {
15044       label_name = merge_qualified_name (label_id, label);
15045       /* Issue an error if we try to reuse a label that was previously
15046          declared */
15047       if (IDENTIFIER_LOCAL_VALUE (label_name))
15048         {
15049           EXPR_WFL_LINECOL (wfl_operator) = location;
15050           parse_error_context (wfl_operator,
15051             "Declaration of %qs shadows a previous label declaration",
15052                                IDENTIFIER_POINTER (label));
15053           EXPR_WFL_LINECOL (wfl_operator) =
15054             EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15055           parse_error_context (wfl_operator,
15056             "This is the location of the previous declaration of label %qs",
15057                                IDENTIFIER_POINTER (label));
15058           java_error_count--;
15059         }
15060     }
15061
15062   label_decl = create_label_decl (label_name);
15063   node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15064   EXPR_WFL_LINECOL (node) = location;
15065   TREE_SIDE_EFFECTS (node) = 1;
15066   return node;
15067 }
15068
15069 /* A labeled statement LBE is attached a statement.  */
15070
15071 static tree
15072 finish_labeled_statement (tree lbe, /* Labeled block expr */
15073                           tree statement)
15074 {
15075   /* In anyways, tie the loop to its statement */
15076   LABELED_BLOCK_BODY (lbe) = statement;
15077   pop_labeled_block ();
15078   POP_LABELED_BLOCK ();
15079   return lbe;
15080 }
15081
15082 /* 14.10, 14.11, 14.12 Loop Statements */
15083
15084 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15085    list. */
15086
15087 static tree
15088 build_new_loop (tree loop_body)
15089 {
15090   tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15091   TREE_SIDE_EFFECTS (loop) = 1;
15092   PUSH_LOOP (loop);
15093   return loop;
15094 }
15095
15096 /* Create a loop body according to the following structure:
15097      COMPOUND_EXPR
15098        COMPOUND_EXPR            (loop main body)
15099          EXIT_EXPR              (this order is for while/for loops.
15100          LABELED_BLOCK_EXPR      the order is reversed for do loops)
15101            LABEL_DECL           (a continue occurring here branches at the
15102            BODY                  end of this labeled block)
15103        INCREMENT                (if any)
15104
15105   REVERSED, if nonzero, tells that the loop condition expr comes
15106   after the body, like in the do-while loop.
15107
15108   To obtain a loop, the loop body structure described above is
15109   encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15110
15111    LABELED_BLOCK_EXPR
15112      LABEL_DECL                   (use this label to exit the loop)
15113      LOOP_EXPR
15114        <structure described above> */
15115
15116 static tree
15117 build_loop_body (int location, tree condition, int reversed)
15118 {
15119   tree first, second, body;
15120
15121   condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15122   EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15123   condition = build_debugable_stmt (location, condition);
15124   TREE_SIDE_EFFECTS (condition) = 1;
15125
15126   body = build_labeled_block (0, continue_identifier_node);
15127   first = (reversed ? body : condition);
15128   second = (reversed ? condition : body);
15129   return build2 (COMPOUND_EXPR, NULL_TREE,
15130                  build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15131                  build_java_empty_stmt ());
15132 }
15133
15134 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15135    their order) on the current loop. Unlink the current loop from the
15136    loop list.  */
15137
15138 static tree
15139 finish_loop_body (int location, tree condition, tree body, int reversed)
15140 {
15141   tree to_return = ctxp->current_loop;
15142   tree loop_body = LOOP_EXPR_BODY (to_return);
15143   if (condition)
15144     {
15145       tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15146       /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15147          The real EXIT_EXPR is one operand further. */
15148       EXPR_WFL_LINECOL (cnode) = location;
15149       if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15150         {
15151           cnode = EXPR_WFL_NODE (cnode);
15152           /* This one is for accurate error reports */
15153           EXPR_WFL_LINECOL (cnode) = location;
15154         }
15155       TREE_OPERAND (cnode, 0) = condition;
15156     }
15157   LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15158   POP_LOOP ();
15159   return to_return;
15160 }
15161
15162 /* Tailored version of finish_loop_body for FOR loops, when FOR
15163    loops feature the condition part */
15164
15165 static tree
15166 finish_for_loop (int location, tree condition, tree update, tree body)
15167 {
15168   /* Put the condition and the loop body in place */
15169   tree loop = finish_loop_body (location, condition, body, 0);
15170   /* LOOP is the current loop which has been now popped of the loop
15171      stack.  Mark the update block as reachable and install it.  We do
15172      this because the (current interpretation of the) JLS requires
15173      that the update expression be considered reachable even if the
15174      for loop's body doesn't complete normally.  */
15175   if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15176     {
15177       tree up2 = update;
15178       if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15179         up2 = EXPR_WFL_NODE (up2);
15180       /* It is possible for the update expression to be an
15181          EXPR_WFL_NODE wrapping nothing.  */
15182       if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15183         {
15184           /* Try to detect constraint violations.  These would be
15185              programming errors somewhere.  */
15186           if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15187             abort ();
15188           SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15189         }
15190     }
15191   LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15192   return loop;
15193 }
15194
15195 /* Try to find the loop a block might be related to. This comprises
15196    the case where the LOOP_EXPR is found as the second operand of a
15197    COMPOUND_EXPR, because the loop happens to have an initialization
15198    part, then expressed as the first operand of the COMPOUND_EXPR. If
15199    the search finds something, 1 is returned. Otherwise, 0 is
15200    returned. The search is assumed to start from a
15201    LABELED_BLOCK_EXPR's block.  */
15202
15203 static tree
15204 search_loop (tree statement)
15205 {
15206   if (TREE_CODE (statement) == LOOP_EXPR)
15207     return statement;
15208
15209   if (TREE_CODE (statement) == BLOCK)
15210     statement = BLOCK_SUBBLOCKS (statement);
15211   else
15212     return NULL_TREE;
15213
15214   if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15215     while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15216       statement = TREE_OPERAND (statement, 1);
15217
15218   return (TREE_CODE (statement) == LOOP_EXPR
15219           && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15220 }
15221
15222 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15223    returned otherwise.  */
15224
15225 static int
15226 labeled_block_contains_loop_p (tree block, tree loop)
15227 {
15228   if (!block)
15229     return 0;
15230
15231   if (LABELED_BLOCK_BODY (block) == loop)
15232     return 1;
15233
15234   if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15235     return 1;
15236
15237   return 0;
15238 }
15239
15240 /* If the loop isn't surrounded by a labeled statement, create one and
15241    insert LOOP as its body.  */
15242
15243 static tree
15244 patch_loop_statement (tree loop)
15245 {
15246   tree loop_label;
15247
15248   TREE_TYPE (loop) = void_type_node;
15249   if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15250     return loop;
15251
15252   loop_label = build_labeled_block (0, NULL_TREE);
15253   /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15254      that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15255   LABELED_BLOCK_BODY (loop_label) = loop;
15256   PUSH_LABELED_BLOCK (loop_label);
15257   return loop_label;
15258 }
15259
15260 /* 14.13, 14.14: break and continue Statements */
15261
15262 /* Build a break or a continue statement. a null NAME indicates an
15263    unlabeled break/continue statement.  */
15264
15265 static tree
15266 build_bc_statement (int location, int is_break, tree name)
15267 {
15268   tree break_continue, label_block_expr = NULL_TREE;
15269
15270   if (name)
15271     {
15272       if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15273             (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15274         /* Null means that we don't have a target for this named
15275            break/continue. In this case, we make the target to be the
15276            label name, so that the error can be reported accurately in
15277            patch_bc_statement. */
15278         label_block_expr = EXPR_WFL_NODE (name);
15279     }
15280   /* Unlabeled break/continue will be handled during the
15281      break/continue patch operation */
15282   break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15283
15284   IS_BREAK_STMT_P (break_continue) = is_break;
15285   TREE_SIDE_EFFECTS (break_continue) = 1;
15286   EXPR_WFL_LINECOL (break_continue) = location;
15287   break_continue = build_debugable_stmt (location, break_continue);
15288   return break_continue;
15289 }
15290
15291 /* Verification of a break/continue statement. */
15292
15293 static tree
15294 patch_bc_statement (tree node)
15295 {
15296   tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15297   tree labeled_block = ctxp->current_labeled_block;
15298   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15299
15300   /* Having an identifier here means that the target is unknown. */
15301   if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15302     {
15303       parse_error_context (wfl_operator, "No label definition found for %qs",
15304                            IDENTIFIER_POINTER (bc_label));
15305       return error_mark_node;
15306     }
15307   if (! IS_BREAK_STMT_P (node))
15308     {
15309       /* It's a continue statement. */
15310       for (;; labeled_block = TREE_CHAIN (labeled_block))
15311         {
15312           if (labeled_block == NULL_TREE)
15313             {
15314               if (bc_label == NULL_TREE)
15315                 parse_error_context (wfl_operator,
15316                                      "%<continue%> must be in loop");
15317               else
15318                 parse_error_context
15319                   (wfl_operator, "continue label %qs does not name a loop",
15320                    IDENTIFIER_POINTER (bc_label));
15321               return error_mark_node;
15322             }
15323           if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15324                == continue_identifier_node)
15325               && (bc_label == NULL_TREE
15326                   || TREE_CHAIN (labeled_block) == bc_label))
15327             {
15328               bc_label = labeled_block;
15329               break;
15330             }
15331         }
15332     }
15333   else if (!bc_label)
15334     {
15335       for (;; labeled_block = TREE_CHAIN (labeled_block))
15336         {
15337           if (labeled_block == NULL_TREE)
15338             {
15339               parse_error_context (wfl_operator,
15340                                      "%<break%> must be in loop or switch");
15341               return error_mark_node;
15342             }
15343           target_stmt = LABELED_BLOCK_BODY (labeled_block);
15344           if (TREE_CODE (target_stmt) == SWITCH_EXPR
15345               || search_loop (target_stmt))
15346             {
15347               bc_label = labeled_block;
15348               break;
15349             }
15350         }
15351     }
15352
15353   EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15354   CAN_COMPLETE_NORMALLY (bc_label) = 1;
15355
15356   /* Our break/continue don't return values. */
15357   TREE_TYPE (node) = void_type_node;
15358   /* Encapsulate the break within a compound statement so that it's
15359      expanded all the times by expand_expr (and not clobbered
15360      sometimes, like after a if statement) */
15361   node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15362   TREE_SIDE_EFFECTS (node) = 1;
15363   return node;
15364 }
15365
15366 /* Process the exit expression belonging to a loop. Its type must be
15367    boolean.  */
15368
15369 static tree
15370 patch_exit_expr (tree node)
15371 {
15372   tree expression = TREE_OPERAND (node, 0);
15373   TREE_TYPE (node) = error_mark_node;
15374   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15375
15376   /* The type of expression must be boolean */
15377   if (TREE_TYPE (expression) != boolean_type_node)
15378     {
15379       parse_error_context
15380         (wfl_operator,
15381     "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15382          lang_printable_name (TREE_TYPE (expression), 0));
15383       return error_mark_node;
15384     }
15385   /* Now we know things are allright, invert the condition, fold and
15386      return */
15387   TREE_OPERAND (node, 0) =
15388     fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15389
15390   if (! integer_zerop (TREE_OPERAND (node, 0))
15391       && ctxp->current_loop != NULL_TREE
15392       && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15393     CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15394   if (! integer_onep (TREE_OPERAND (node, 0)))
15395     CAN_COMPLETE_NORMALLY (node) = 1;
15396
15397
15398   TREE_TYPE (node) = void_type_node;
15399   return node;
15400 }
15401
15402 /* 14.9 Switch statement */
15403
15404 static tree
15405 patch_switch_statement (tree node)
15406 {
15407   tree se = TREE_OPERAND (node, 0), se_type;
15408   tree save, iter;
15409
15410   /* Complete the switch expression */
15411   se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15412   se_type = TREE_TYPE (se);
15413   /* The type of the switch expression must be char, byte, short or
15414      int */
15415   if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15416     {
15417       EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15418       parse_error_context (wfl_operator,
15419           "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15420                            lang_printable_name (se_type, 0));
15421       /* This is what java_complete_tree will check */
15422       TREE_OPERAND (node, 0) = error_mark_node;
15423       return error_mark_node;
15424     }
15425
15426   /* Save and restore the outer case label list.  */
15427   save = case_label_list;
15428   case_label_list = NULL_TREE;
15429
15430   TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15431
15432   /* See if we've found a duplicate label.  We can't leave this until
15433      code generation, because in `--syntax-only' and `-C' modes we
15434      don't do ordinary code generation.  */
15435   for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15436     {
15437       HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15438       tree subiter;
15439       for (subiter = TREE_CHAIN (iter);
15440            subiter != NULL_TREE;
15441            subiter = TREE_CHAIN (subiter))
15442         {
15443           HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15444           if (val == subval)
15445             {
15446               EXPR_WFL_LINECOL (wfl_operator)
15447                 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15448               /* The case_label_list is in reverse order, so print the
15449                  outer label first.  */
15450               parse_error_context (wfl_operator, "duplicate case label: %<"
15451                                    HOST_WIDE_INT_PRINT_DEC "%>", subval);
15452               EXPR_WFL_LINECOL (wfl_operator)
15453                 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15454               parse_error_context (wfl_operator, "original label is here");
15455
15456               break;
15457             }
15458         }
15459     }
15460
15461   case_label_list = save;
15462
15463   /* Ready to return */
15464   if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15465     {
15466       TREE_TYPE (node) = error_mark_node;
15467       return error_mark_node;
15468     }
15469   TREE_TYPE (node) = void_type_node;
15470   TREE_SIDE_EFFECTS (node) = 1;
15471   CAN_COMPLETE_NORMALLY (node)
15472     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15473       || ! SWITCH_HAS_DEFAULT (node);
15474   return node;
15475 }
15476
15477 /* Assertions.  */
15478
15479 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15480    might be NULL_TREE.  */
15481 static tree
15482 build_assertion (
15483 #ifdef USE_MAPPED_LOCATION
15484                  source_location location,
15485 #else
15486                  int location,
15487 #endif
15488                  tree condition, tree value)
15489 {
15490   tree node;
15491   tree klass = GET_CPC ();
15492
15493   if (! enable_assertions (klass))
15494     {
15495       condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15496                           boolean_false_node, condition);
15497       if (value == NULL_TREE)
15498         value = build_java_empty_stmt ();
15499       return build_if_else_statement (location, condition,
15500                                       value, NULL_TREE);
15501     }
15502
15503   if (! CLASS_USES_ASSERTIONS (klass))
15504     {
15505       tree field, classdollar, id, call;
15506       tree class_type = TREE_TYPE (klass);
15507
15508       field = add_field (class_type,
15509                          get_identifier ("$assertionsDisabled"),
15510                          boolean_type_node,
15511                          ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15512       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15513       FIELD_SYNTHETIC (field) = 1;
15514
15515       classdollar = build_incomplete_class_ref (location, class_type);
15516
15517       /* Call CLASS.desiredAssertionStatus().  */
15518       id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15519       call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15520       call = make_qualified_primary (classdollar, call, location);
15521       TREE_SIDE_EFFECTS (call) = 1;
15522
15523       /* Invert to obtain !CLASS.desiredAssertionStatus().  This may
15524          seem odd, but we do it to generate code identical to that of
15525          the JDK.  */
15526       call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15527       TREE_SIDE_EFFECTS (call) = 1;
15528       DECL_INITIAL (field) = call;
15529
15530       /* Record the initializer in the initializer statement list.  */
15531       call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15532       TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15533       SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15534       MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15535
15536       CLASS_USES_ASSERTIONS (klass) = 1;
15537     }
15538
15539   if (value != NULL_TREE)
15540     value = tree_cons (NULL_TREE, value, NULL_TREE);
15541
15542   node = build_wfl_node (get_identifier ("java"));
15543   node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15544                               location);
15545   node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15546                               location);
15547
15548   node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15549   TREE_SIDE_EFFECTS (node) = 1;
15550   /* It is too early to use BUILD_THROW.  */
15551   node = build1 (THROW_EXPR, NULL_TREE, node);
15552   TREE_SIDE_EFFECTS (node) = 1;
15553
15554   /* We invert the condition; if we just put NODE as the `else' part
15555      then we generate weird-looking bytecode.  */
15556   condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15557   /* Check $assertionsDisabled.  */
15558   condition
15559     = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15560               build1 (TRUTH_NOT_EXPR, NULL_TREE,
15561                       build_wfl_node (get_identifier ("$assertionsDisabled"))),
15562               condition);
15563   node = build_if_else_statement (location, condition, node, NULL_TREE);
15564   return node;
15565 }
15566
15567 /* 14.18 The try/catch statements */
15568
15569 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15570    catches TYPE and executes CATCH_STMTS.  */
15571
15572 static tree
15573 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15574                             tree catch_stmts)
15575 {
15576   tree try_block, catch_clause_param, catch_block, catch;
15577
15578   /* First build a try block */
15579   try_block = build_expr_block (try_stmts, NULL_TREE);
15580
15581   /* Build a catch block: we need a catch clause parameter */
15582   if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15583     {
15584       tree catch_type = obtain_incomplete_type (type_or_name);
15585       jdep *dep;
15586       catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15587       register_incomplete_type (JDEP_VARIABLE, type_or_name,
15588                                 catch_clause_param, catch_type);
15589       dep = CLASSD_LAST (ctxp->classd_list);
15590       JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15591     }
15592   else
15593     catch_clause_param = build_decl (VAR_DECL, wpv_id,
15594                                      build_pointer_type (type_or_name));
15595
15596   /* And a block */
15597   catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15598
15599   /* Initialize the variable and store in the block */
15600   catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15601                   build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15602   add_stmt_to_block (catch_block, NULL_TREE, catch);
15603
15604   /* Add the catch statements */
15605   add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15606
15607   /* Now we can build a JAVA_CATCH_EXPR */
15608   catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15609
15610   return build_try_statement (location, try_block, catch_block);
15611 }
15612
15613 static tree
15614 build_try_statement (int location, tree try_block, tree catches)
15615 {
15616   tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15617   EXPR_WFL_LINECOL (node) = location;
15618   return node;
15619 }
15620
15621 static tree
15622 build_try_finally_statement (int location, tree try_block, tree finally)
15623 {
15624   tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15625   EXPR_WFL_LINECOL (node) = location;
15626   return node;
15627 }
15628
15629 static tree
15630 patch_try_statement (tree node)
15631 {
15632   int error_found = 0;
15633   tree try = TREE_OPERAND (node, 0);
15634   /* Exception handlers are considered in left to right order */
15635   tree catch = nreverse (TREE_OPERAND (node, 1));
15636   tree current, caught_type_list = NULL_TREE;
15637
15638   /* Check catch clauses, if any. Every time we find an error, we try
15639      to process the next catch clause. We process the catch clause before
15640      the try block so that when processing the try block we can check thrown
15641      exceptions against the caught type list. */
15642   for (current = catch; current; current = TREE_CHAIN (current))
15643     {
15644       tree carg_decl, carg_type;
15645       tree sub_current, catch_block, catch_clause;
15646       int unreachable;
15647
15648       /* At this point, the structure of the catch clause is
15649            JAVA_CATCH_EXPR              (catch node)
15650              BLOCK              (with the decl of the parameter)
15651                COMPOUND_EXPR
15652                  MODIFY_EXPR   (assignment of the catch parameter)
15653                  BLOCK          (catch clause block)
15654        */
15655       catch_clause = TREE_OPERAND (current, 0);
15656       carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15657       carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15658
15659       /* Catch clauses can't have more than one parameter declared,
15660          but it's already enforced by the grammar. Make sure that the
15661          only parameter of the clause statement in of class Throwable
15662          or a subclass of Throwable, but that was done earlier. The
15663          catch clause parameter type has also been resolved. */
15664
15665       /* Just make sure that the catch clause parameter type inherits
15666          from java.lang.Throwable */
15667       if (!inherits_from_p (carg_type, throwable_type_node))
15668         {
15669           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15670           parse_error_context (wfl_operator,
15671                                "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15672                                lang_printable_name (carg_type, 0));
15673           error_found = 1;
15674           continue;
15675         }
15676
15677       /* Partial check for unreachable catch statement: The catch
15678          clause is reachable iff is no earlier catch block A in
15679          the try statement such that the type of the catch
15680          clause's parameter is the same as or a subclass of the
15681          type of A's parameter */
15682       unreachable = 0;
15683       for (sub_current = catch;
15684            sub_current != current; sub_current = TREE_CHAIN (sub_current))
15685         {
15686           tree sub_catch_clause, decl;
15687           sub_catch_clause = TREE_OPERAND (sub_current, 0);
15688           decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15689
15690           if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15691             {
15692               EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15693               parse_error_context
15694                 (wfl_operator,
15695                  "%<catch%> not reached because of the catch clause at line %d",
15696                  EXPR_WFL_LINENO (sub_current));
15697               unreachable = error_found = 1;
15698               break;
15699             }
15700         }
15701       /* Complete the catch clause block */
15702       catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15703       if (catch_block == error_mark_node)
15704         {
15705           error_found = 1;
15706           continue;
15707         }
15708       if (CAN_COMPLETE_NORMALLY (catch_block))
15709         CAN_COMPLETE_NORMALLY (node) = 1;
15710       TREE_OPERAND (current, 0) = catch_block;
15711
15712       if (unreachable)
15713         continue;
15714
15715       /* Things to do here: the exception must be thrown */
15716
15717       /* Link this type to the caught type list */
15718       caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15719     }
15720
15721   PUSH_EXCEPTIONS (caught_type_list);
15722   if ((try = java_complete_tree (try)) == error_mark_node)
15723     error_found = 1;
15724   if (CAN_COMPLETE_NORMALLY (try))
15725     CAN_COMPLETE_NORMALLY (node) = 1;
15726   POP_EXCEPTIONS ();
15727
15728   /* Verification ends here */
15729   if (error_found)
15730     return error_mark_node;
15731
15732   TREE_OPERAND (node, 0) = try;
15733   TREE_OPERAND (node, 1) = catch;
15734   TREE_TYPE (node) = void_type_node;
15735   return node;
15736 }
15737
15738 /* 14.17 The synchronized Statement */
15739
15740 static tree
15741 patch_synchronized_statement (tree node, tree wfl_op1)
15742 {
15743   tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15744   tree block = TREE_OPERAND (node, 1);
15745
15746   tree tmp, enter, exit, expr_decl, assignment;
15747
15748   if (expr == error_mark_node)
15749     {
15750       block = java_complete_tree (block);
15751       return expr;
15752     }
15753
15754   /* We might be trying to synchronize on a STRING_CST */
15755   if ((tmp = patch_string (expr)))
15756     expr = tmp;
15757
15758   /* The TYPE of expr must be a reference type */
15759   if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15760     {
15761       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15762       parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15763                            lang_printable_name (TREE_TYPE (expr), 0));
15764       return error_mark_node;
15765     }
15766
15767   if (flag_emit_xref)
15768     {
15769       TREE_OPERAND (node, 0) = expr;
15770       TREE_OPERAND (node, 1) = java_complete_tree (block);
15771       CAN_COMPLETE_NORMALLY (node) = 1;
15772       return node;
15773     }
15774
15775   /* Generate a try-finally for the synchronized statement, except
15776      that the handler that catches all throw exception calls
15777      _Jv_MonitorExit and then rethrow the exception.
15778      The synchronized statement is then implemented as:
15779      TRY
15780        {
15781          _Jv_MonitorEnter (expression)
15782          synchronized_block
15783          _Jv_MonitorExit (expression)
15784        }
15785      CATCH_ALL
15786        {
15787          e = _Jv_exception_info ();
15788          _Jv_MonitorExit (expression)
15789          Throw (e);
15790        } */
15791
15792   expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15793   BUILD_MONITOR_ENTER (enter, expr_decl);
15794   BUILD_MONITOR_EXIT (exit, expr_decl);
15795   CAN_COMPLETE_NORMALLY (enter) = 1;
15796   CAN_COMPLETE_NORMALLY (exit) = 1;
15797   assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15798   TREE_SIDE_EFFECTS (assignment) = 1;
15799   node = build2 (COMPOUND_EXPR, NULL_TREE,
15800                  build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15801                  build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15802   node = build_expr_block (node, expr_decl);
15803
15804   return java_complete_tree (node);
15805 }
15806
15807 /* 14.16 The throw Statement */
15808
15809 static tree
15810 patch_throw_statement (tree node, tree wfl_op1)
15811 {
15812   tree expr = TREE_OPERAND (node, 0);
15813   tree type = TREE_TYPE (expr);
15814   int unchecked_ok = 0, tryblock_throws_ok = 0;
15815
15816   /* Thrown expression must be assignable to java.lang.Throwable */
15817   if (!try_reference_assignconv (throwable_type_node, expr))
15818     {
15819       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15820       parse_error_context (wfl_operator,
15821     "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15822                            lang_printable_name (type, 0));
15823       /* If the thrown expression was a reference, we further the
15824          compile-time check. */
15825       if (!JREFERENCE_TYPE_P (type))
15826         return error_mark_node;
15827     }
15828
15829   /* At least one of the following must be true */
15830
15831   /* The type of the throw expression is a not checked exception,
15832      i.e. is a unchecked expression. */
15833   unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15834
15835   SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15836   /* An instance can't throw a checked exception unless that exception
15837      is explicitly declared in the `throws' clause of each
15838      constructor. This doesn't apply to anonymous classes, since they
15839      don't have declared constructors. */
15840   if (!unchecked_ok
15841       && DECL_INSTINIT_P (current_function_decl)
15842       && !ANONYMOUS_CLASS_P (current_class))
15843     {
15844       tree current;
15845       for (current = TYPE_METHODS (current_class); current;
15846            current = TREE_CHAIN (current))
15847         if (DECL_CONSTRUCTOR_P (current)
15848             && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15849           {
15850             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)",
15851                                  lang_printable_name (TREE_TYPE (expr), 0));
15852             return error_mark_node;
15853           }
15854     }
15855
15856   /* Throw is contained in a try statement and at least one catch
15857      clause can receive the thrown expression or the current method is
15858      declared to throw such an exception. Or, the throw statement is
15859      contained in a method or constructor declaration and the type of
15860      the Expression is assignable to at least one type listed in the
15861      throws clause the declaration. */
15862   if (!unchecked_ok)
15863     tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15864   if (!(unchecked_ok || tryblock_throws_ok))
15865     {
15866       /* If there is a surrounding try block that has no matching
15867          clatch clause, report it first. A surrounding try block exits
15868          only if there is something after the list of checked
15869          exception thrown by the current function (if any). */
15870       if (IN_TRY_BLOCK_P ())
15871         parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15872                              lang_printable_name (type, 0));
15873       /* If we have no surrounding try statement and the method doesn't have
15874          any throws, report it now. FIXME */
15875
15876       /* We report that the exception can't be throw from a try block
15877          in all circumstances but when the `throw' is inside a static
15878          block. */
15879       else if (!EXCEPTIONS_P (currently_caught_type_list)
15880                && !tryblock_throws_ok)
15881         {
15882           if (DECL_CLINIT_P (current_function_decl))
15883             parse_error_context (wfl_operator,
15884                    "Checked exception %qs can't be thrown in initializer",
15885                                  lang_printable_name (type, 0));
15886           else
15887             parse_error_context (wfl_operator,
15888                    "Checked exception %qs isn't thrown from a %<try%> block",
15889                                  lang_printable_name (type, 0));
15890         }
15891       /* Otherwise, the current method doesn't have the appropriate
15892          throws declaration */
15893       else
15894         parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15895                              lang_printable_name (type, 0));
15896       return error_mark_node;
15897     }
15898
15899   if (! flag_emit_class_files && ! flag_emit_xref)
15900     BUILD_THROW (node, expr);
15901
15902   /* If doing xrefs, keep the location where the `throw' was seen. */
15903   if (flag_emit_xref)
15904     EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15905   return node;
15906 }
15907
15908 /* Check that exception said to be thrown by method DECL can be
15909    effectively caught from where DECL is invoked.  THIS_EXPR is the
15910    expression that computes `this' for the method call.  */
15911 static void
15912 check_thrown_exceptions (
15913 #ifdef USE_MAPPED_LOCATION
15914                          source_location location,
15915 #else
15916
15917                          int location,
15918 #endif
15919                          tree decl, tree this_expr)
15920 {
15921   tree throws;
15922   int is_array_call = 0;
15923
15924   /* Skip check within generated methods, such as access$<n>.  */
15925   if (OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15926     return;
15927
15928   if (this_expr != NULL_TREE
15929       && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15930       && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15931     is_array_call = 1;
15932
15933   /* For all the unchecked exceptions thrown by DECL.  */
15934   for (throws = DECL_FUNCTION_THROWS (decl); throws;
15935        throws = TREE_CHAIN (throws))
15936     if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15937       {
15938         /* Suppress errors about cloning arrays.  */
15939         if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
15940           continue;
15941
15942 #ifdef USE_MAPPED_LOCATION
15943         SET_EXPR_LOCATION (wfl_operator, location);
15944 #else
15945         EXPR_WFL_LINECOL (wfl_operator) = location;
15946 #endif
15947         if (DECL_FINIT_P (current_function_decl))
15948           parse_error_context
15949             (wfl_operator, "Exception %qs can't be thrown in initializer",
15950              lang_printable_name (TREE_VALUE (throws), 0));
15951         else
15952           {
15953             parse_error_context
15954               (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
15955                lang_printable_name (TREE_VALUE (throws), 0),
15956                (DECL_INIT_P (current_function_decl) ?
15957                 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15958                 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15959           }
15960       }
15961 }
15962
15963 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15964    try-catch blocks, OR is listed in the `throws' clause of the
15965    current method.  */
15966
15967 static int
15968 check_thrown_exceptions_do (tree exception)
15969 {
15970   tree list = currently_caught_type_list;
15971   resolve_and_layout (exception, NULL_TREE);
15972   /* First, all the nested try-catch-finally at that stage. The
15973      last element contains `throws' clause exceptions, if any. */
15974   if (IS_UNCHECKED_EXCEPTION_P (exception))
15975     return 1;
15976   while (list)
15977     {
15978       tree caught;
15979       for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15980         if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15981           return 1;
15982       list = TREE_CHAIN (list);
15983     }
15984   return 0;
15985 }
15986
15987 static void
15988 purge_unchecked_exceptions (tree mdecl)
15989 {
15990   tree throws = DECL_FUNCTION_THROWS (mdecl);
15991   tree new = NULL_TREE;
15992
15993   while (throws)
15994     {
15995       tree next = TREE_CHAIN (throws);
15996       if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15997         {
15998           TREE_CHAIN (throws) = new;
15999           new = throws;
16000         }
16001       throws = next;
16002     }
16003   /* List is inverted here, but it doesn't matter */
16004   DECL_FUNCTION_THROWS (mdecl) = new;
16005 }
16006
16007 /* This function goes over all of CLASS_TYPE ctors and checks whether
16008    each of them features at least one unchecked exception in its
16009    `throws' clause. If it's the case, it returns `true', `false'
16010    otherwise.  */
16011
16012 static bool
16013 ctors_unchecked_throws_clause_p (tree class_type)
16014 {
16015   tree current;
16016
16017   for (current = TYPE_METHODS (class_type); current;
16018        current = TREE_CHAIN (current))
16019     {
16020       bool ctu = false; /* Ctor Throws Unchecked */
16021       if (DECL_CONSTRUCTOR_P (current))
16022         {
16023           tree throws;
16024           for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16025                throws = TREE_CHAIN (throws))
16026             if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16027               ctu = true;
16028         }
16029       /* We return false as we found one ctor that is unfit. */
16030       if (!ctu && DECL_CONSTRUCTOR_P (current))
16031         return false;
16032     }
16033   /* All ctors feature at least one unchecked exception in their
16034      `throws' clause. */
16035   return true;
16036 }
16037
16038 /* 15.24 Conditional Operator ?: */
16039
16040 static tree
16041 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16042 {
16043   tree cond = TREE_OPERAND (node, 0);
16044   tree op1 = TREE_OPERAND (node, 1);
16045   tree op2 = TREE_OPERAND (node, 2);
16046   tree resulting_type = NULL_TREE;
16047   tree t1, t2, patched;
16048   int error_found = 0;
16049
16050   /* Operands of ?: might be StringBuffers crafted as a result of a
16051      string concatenation. Obtain a descent operand here.  */
16052   if ((patched = patch_string (op1)))
16053     TREE_OPERAND (node, 1) = op1 = patched;
16054   if ((patched = patch_string (op2)))
16055     TREE_OPERAND (node, 2) = op2 = patched;
16056
16057   t1 = TREE_TYPE (op1);
16058   t2 = TREE_TYPE (op2);
16059
16060   /* The first expression must be a boolean */
16061   if (TREE_TYPE (cond) != boolean_type_node)
16062     {
16063       SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16064       parse_error_context (wfl_operator,
16065                "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
16066                            lang_printable_name (TREE_TYPE (cond), 0));
16067       error_found = 1;
16068     }
16069
16070   /* Second and third can be numeric, boolean (i.e. primitive),
16071      references or null. Anything else results in an error */
16072   if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16073         || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16074             && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16075         || (t1 == boolean_type_node && t2 == boolean_type_node)))
16076     error_found = 1;
16077
16078   /* Determine the type of the conditional expression. Same types are
16079      easy to deal with */
16080   else if (t1 == t2)
16081     resulting_type = t1;
16082
16083   /* There are different rules for numeric types */
16084   else if (JNUMERIC_TYPE_P (t1))
16085     {
16086       /* if byte/short found, the resulting type is short */
16087       if ((t1 == byte_type_node && t2 == short_type_node)
16088           || (t1 == short_type_node && t2 == byte_type_node))
16089         resulting_type = short_type_node;
16090
16091       /* If t1 is a constant int and t2 is of type byte, short or char
16092          and t1's value fits in t2, then the resulting type is t2 */
16093       else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16094           && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16095         resulting_type = t2;
16096
16097       /* If t2 is a constant int and t1 is of type byte, short or char
16098          and t2's value fits in t1, then the resulting type is t1 */
16099       else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16100           && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16101         resulting_type = t1;
16102
16103       /* Otherwise, binary numeric promotion is applied and the
16104          resulting type is the promoted type of operand 1 and 2 */
16105       else
16106         resulting_type = binary_numeric_promotion (t1, t2,
16107                                                    &TREE_OPERAND (node, 1),
16108                                                    &TREE_OPERAND (node, 2));
16109     }
16110
16111   /* Cases of a reference and a null type */
16112   else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16113     resulting_type = t1;
16114
16115   else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16116     resulting_type = t2;
16117
16118   /* Last case: different reference types. If a type can be converted
16119      into the other one by assignment conversion, the latter
16120      determines the type of the expression */
16121   else if ((resulting_type = try_reference_assignconv (t1, op2)))
16122     resulting_type = promote_type (t1);
16123
16124   else if ((resulting_type = try_reference_assignconv (t2, op1)))
16125     resulting_type = promote_type (t2);
16126
16127   /* If we don't have any resulting type, we're in trouble */
16128   if (!resulting_type)
16129     {
16130       char *t = xstrdup (lang_printable_name (t1, 0));
16131       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16132       parse_error_context (wfl_operator,
16133                  "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16134                            t, lang_printable_name (t2, 0));
16135       free (t);
16136       error_found = 1;
16137     }
16138
16139   if (error_found)
16140     {
16141       TREE_TYPE (node) = error_mark_node;
16142       return error_mark_node;
16143     }
16144
16145   TREE_TYPE (node) = resulting_type;
16146   TREE_SET_CODE (node, COND_EXPR);
16147   CAN_COMPLETE_NORMALLY (node) = 1;
16148   return node;
16149 }
16150
16151 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16152
16153 static tree
16154 maybe_build_class_init_for_field (tree decl, tree expr)
16155 {
16156   tree clas = DECL_CONTEXT (decl);
16157   if (flag_emit_class_files || flag_emit_xref)
16158     return expr;
16159
16160   if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16161       && FIELD_FINAL (decl))
16162     {
16163       tree init = DECL_INITIAL (decl);
16164       if (init != NULL_TREE)
16165         init = fold_constant_for_init (init, decl);
16166       if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16167         return expr;
16168     }
16169
16170   return build_class_init (clas, expr);
16171 }
16172
16173 /* Try to constant fold NODE.
16174    If NODE is not a constant expression, return NULL_EXPR.
16175    CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16176
16177 static tree
16178 fold_constant_for_init (tree node, tree context)
16179 {
16180   tree op0, op1, val;
16181   enum tree_code code = TREE_CODE (node);
16182
16183   switch (code)
16184     {
16185     case INTEGER_CST:
16186       if (node == null_pointer_node)
16187         return NULL_TREE;
16188     case STRING_CST:
16189     case REAL_CST:
16190       return node;
16191
16192     case PLUS_EXPR:
16193     case MINUS_EXPR:
16194     case MULT_EXPR:
16195     case TRUNC_MOD_EXPR:
16196     case RDIV_EXPR:
16197     case LSHIFT_EXPR:
16198     case RSHIFT_EXPR:
16199     case URSHIFT_EXPR:
16200     case BIT_AND_EXPR:
16201     case BIT_XOR_EXPR:
16202     case BIT_IOR_EXPR:
16203     case TRUTH_ANDIF_EXPR:
16204     case TRUTH_ORIF_EXPR:
16205     case EQ_EXPR:
16206     case NE_EXPR:
16207     case GT_EXPR:
16208     case GE_EXPR:
16209     case LT_EXPR:
16210     case LE_EXPR:
16211       op0 = TREE_OPERAND (node, 0);
16212       op1 = TREE_OPERAND (node, 1);
16213       val = fold_constant_for_init (op0, context);
16214       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16215         return NULL_TREE;
16216       TREE_OPERAND (node, 0) = val;
16217       val = fold_constant_for_init (op1, context);
16218       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16219         return NULL_TREE;
16220       TREE_OPERAND (node, 1) = val;
16221       return patch_binop (node, op0, op1);
16222
16223     case UNARY_PLUS_EXPR:
16224     case NEGATE_EXPR:
16225     case TRUTH_NOT_EXPR:
16226     case BIT_NOT_EXPR:
16227     case CONVERT_EXPR:
16228       op0 = TREE_OPERAND (node, 0);
16229       val = fold_constant_for_init (op0, context);
16230       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16231         return NULL_TREE;
16232       TREE_OPERAND (node, 0) = val;
16233       val = patch_unaryop (node, op0);
16234       if (! TREE_CONSTANT (val))
16235         return NULL_TREE;
16236       return val;
16237
16238       break;
16239
16240     case COND_EXPR:
16241       val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16242       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16243         return NULL_TREE;
16244       TREE_OPERAND (node, 0) = val;
16245       val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16246       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16247         return NULL_TREE;
16248       TREE_OPERAND (node, 1) = val;
16249       val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16250       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16251         return NULL_TREE;
16252       TREE_OPERAND (node, 2) = val;
16253       return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16254         : TREE_OPERAND (node, 2);
16255
16256     case VAR_DECL:
16257     case FIELD_DECL:
16258       if (! FIELD_FINAL (node)
16259           || DECL_INITIAL (node) == NULL_TREE)
16260         return NULL_TREE;
16261       val = DECL_INITIAL (node);
16262       /* Guard against infinite recursion. */
16263       DECL_INITIAL (node) = NULL_TREE;
16264       val = fold_constant_for_init (val, node);
16265       if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16266         val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16267       DECL_INITIAL (node) = val;
16268       return val;
16269
16270     case EXPR_WITH_FILE_LOCATION:
16271       /* Compare java_complete_tree and resolve_expression_name. */
16272       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16273           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16274         {
16275           tree name = EXPR_WFL_NODE (node);
16276           tree decl;
16277           if (PRIMARY_P (node))
16278             return NULL_TREE;
16279           else if (! QUALIFIED_P (name))
16280             {
16281               decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16282               if (decl == NULL_TREE
16283                   || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16284                 return NULL_TREE;
16285               return fold_constant_for_init (decl, decl);
16286             }
16287           else
16288             {
16289               tree r = NULL_TREE;
16290               /* Install the proper context for the field resolution.  */
16291               tree saved_current_class = current_class;
16292               /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
16293               current_class = DECL_CONTEXT (context);
16294               qualify_ambiguous_name (node);
16295               r = resolve_field_access (node, &decl, NULL);
16296               /* Restore prior context.  */
16297               current_class = saved_current_class;
16298               if (r != error_mark_node && decl != NULL_TREE)
16299                 return fold_constant_for_init (decl, decl);
16300               return NULL_TREE;
16301             }
16302         }
16303       else
16304         {
16305           op0 = TREE_OPERAND (node, 0);
16306           val = fold_constant_for_init (op0, context);
16307           if (val == NULL_TREE || ! TREE_CONSTANT (val))
16308             return NULL_TREE;
16309           TREE_OPERAND (node, 0) = val;
16310           return val;
16311         }
16312
16313 #ifdef USE_COMPONENT_REF
16314     case IDENTIFIER:
16315     case COMPONENT_REF:
16316       ?;
16317 #endif
16318
16319     default:
16320       return NULL_TREE;
16321     }
16322 }
16323
16324 #ifdef USE_COMPONENT_REF
16325 /* Context is 'T' for TypeName, 'P' for PackageName,
16326    'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16327
16328 tree
16329 resolve_simple_name (tree name, int context)
16330 {
16331 }
16332
16333 tree
16334 resolve_qualified_name (tree name, int context)
16335 {
16336 }
16337 #endif
16338
16339 void
16340 init_src_parse (void)
16341 {
16342   /* Sanity check; we've been bit by this before.  */
16343   if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16344     abort ();
16345 }
16346
16347 \f
16348
16349 /* This section deals with the functions that are called when tables
16350    recording class initialization information are traversed.  */
16351
16352 /* This function is called for each class that is known definitely
16353    initialized when a given static method was called. This function
16354    augments a compound expression (INFO) storing all assignment to
16355    initialized static class flags if a flag already existed, otherwise
16356    a new one is created.  */
16357
16358 static int
16359 emit_test_initialization (void **entry_p, void *info)
16360 {
16361   tree l = (tree) info;
16362   tree decl, init;
16363   tree key = (tree) *entry_p;
16364   tree *ite;
16365   htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16366
16367   /* If we haven't found a flag and we're dealing with self registered
16368      with current_function_decl, then don't do anything. Self is
16369      always added as definitely initialized but this information is
16370      valid only if used outside the current function. */
16371   if (current_function_decl == TREE_PURPOSE (l)
16372       && java_treetreehash_find (cf_ht, key) == NULL)
16373     return true;
16374
16375   ite = java_treetreehash_new (cf_ht, key);
16376
16377   /* If we don't have a variable, create one and install it. */
16378   if (*ite == NULL)
16379     {
16380       tree block;
16381
16382       decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16383       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16384       LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16385       DECL_CONTEXT (decl) = current_function_decl;
16386       DECL_INITIAL (decl) = boolean_true_node;
16387       /* Don't emit any symbolic debugging info for this decl.  */
16388       DECL_IGNORED_P (decl) = 1;
16389
16390       /* The trick is to find the right context for it. */
16391       block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16392       TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16393       BLOCK_EXPR_DECLS (block) = decl;
16394       *ite = decl;
16395     }
16396   else
16397     decl = *ite;
16398
16399   /* Now simply augment the compound that holds all the assignments
16400      pertaining to this method invocation. */
16401   init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16402   TREE_SIDE_EFFECTS (init) = 1;
16403   TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16404   TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16405
16406   return true;
16407 }
16408
16409 #ifdef __XGETTEXT__
16410 /* Depending on the version of Bison used to compile this grammar,
16411    it may issue generic diagnostics spelled "syntax error" or
16412    "parse error".  To prevent this from changing the translation
16413    template randomly, we list all the variants of this particular
16414    diagnostic here.  Translators: there is no fine distinction
16415    between diagnostics with "syntax error" in them, and diagnostics
16416    with "parse error" in them.  It's okay to give them both the same
16417    translation.  */
16418 const char d1[] = N_("syntax error");
16419 const char d2[] = N_("parse error");
16420 const char d3[] = N_("syntax error; also virtual memory exhausted");
16421 const char d4[] = N_("parse error; also virtual memory exhausted");
16422 const char d5[] = N_("syntax error: cannot back up");
16423 const char d6[] = N_("parse error: cannot back up");
16424 #endif
16425
16426 #include "gt-java-parse.h"
16427 #include "gtype-java.h"