OSDN Git Service

a606d87869e52ced980e267cfa031aa4ae9f91ab
[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, 2006
4    Free Software Foundation, Inc.
5    Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
23
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
27
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
30 language).
31
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
35
36 The following modifications were brought to the original grammar:
37
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
40
41 Note: All the extra rules described above should go away when the
42       empty_statement rule will work.
43
44 statement_nsi: 'nsi' should be read no_short_if.
45
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions.  */
48
49 %{
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include <dirent.h>
55 #include "tree.h"
56 #include "rtl.h"
57 #include "real.h"
58 #include "obstack.h"
59 #include "toplev.h"
60 #include "pretty-print.h"
61 #include "diagnostic.h"
62 #include "flags.h"
63 #include "java-tree.h"
64 #include "jcf.h"
65 #include "lex.h"
66 #include "parse.h"
67 #include "zipfile.h"
68 #include "convert.h"
69 #include "buffer.h"
70 #include "function.h"
71 #include "except.h"
72 #include "ggc.h"
73 #include "debug.h"
74 #include "tree-inline.h"
75 #include "tree-dump.h"
76 #include "cgraph.h"
77 #include "target.h"
78
79 /* Local function prototypes */
80 static char *java_accstring_lookup (int);
81 static const char *accessibility_string (int);
82 static void  classitf_redefinition_error (const char *,tree, tree, tree);
83 static void  variable_redefinition_error (tree, tree, tree, int);
84 static tree  create_class (int, tree, tree, tree);
85 static tree  create_interface (int, tree, tree);
86 static void  end_class_declaration (int);
87 static tree  find_field (tree, tree);
88 static tree lookup_field_wrapper (tree, tree);
89 static int   duplicate_declaration_error_p (tree, tree, tree);
90 static void  register_fields (int, tree, tree);
91 static tree parser_qualified_classname (tree);
92 static int  parser_check_super (tree, tree, tree);
93 static int  parser_check_super_interface (tree, tree, tree);
94 static void check_modifiers_consistency (int);
95 static tree lookup_cl (tree);
96 static tree lookup_java_method2 (tree, tree, int);
97 static tree method_header (int, tree, tree, tree);
98 static void fix_method_argument_names (tree ,tree);
99 static tree method_declarator (tree, tree);
100 static void parse_warning_context (tree cl, const char *gmsgid, ...) ATTRIBUTE_GCC_DIAG(2,3);
101 #ifdef USE_MAPPED_LOCATION
102 static void issue_warning_error_from_context
103   (source_location, const char *gmsgid, va_list *);
104 #else
105 static void issue_warning_error_from_context
106   (tree, const char *gmsgid, va_list *);
107 #endif
108 static void parse_ctor_invocation_error (void);
109 static tree parse_jdk1_1_error (const char *);
110 static void complete_class_report_errors (jdep *);
111 static int process_imports (void);
112 static void read_import_dir (tree);
113 static int find_in_imports_on_demand (tree, tree);
114 static void find_in_imports (tree, tree);
115 static bool inner_class_accessible (tree, tree);
116 static void check_inner_class_access (tree, tree, tree);
117 static int check_pkg_class_access (tree, tree, bool, tree);
118 static tree resolve_package (tree, tree *, tree *);
119 static tree resolve_class (tree, tree, tree, tree);
120 static void declare_local_variables (int, tree, tree);
121 static void dump_java_tree (enum tree_dump_index, tree);
122 static void source_start_java_method (tree);
123 static void source_end_java_method (void);
124 static tree find_name_in_single_imports (tree);
125 static void check_abstract_method_header (tree);
126 static tree lookup_java_interface_method2 (tree, tree);
127 static tree resolve_expression_name (tree, tree *);
128 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
129 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
130 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
131 static tree resolve_and_layout (tree, tree);
132 static tree qualify_and_find (tree, tree, tree);
133 static tree resolve_no_layout (tree, tree);
134 static int invocation_mode (tree, int);
135 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
136 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
137 static tree find_most_specific_methods_list (tree, tree);
138 static int argument_types_convertible (tree, tree);
139 static tree patch_invoke (tree, tree, tree);
140 static int maybe_use_access_method (int, tree *, tree *);
141 static tree lookup_method_invoke (int, tree, tree, tree, tree);
142 static tree register_incomplete_type (int, tree, tree, tree);
143 static tree check_inner_circular_reference (tree, tree);
144 static tree check_circular_reference (tree);
145 static tree obtain_incomplete_type (tree);
146 static tree java_complete_lhs (tree);
147 static tree java_complete_tree (tree);
148 static tree maybe_generate_pre_expand_clinit (tree);
149 static int analyze_clinit_body (tree, tree);
150 static int maybe_yank_clinit (tree);
151 static void start_complete_expand_method (tree);
152 static void java_complete_expand_method (tree);
153 static void java_expand_method_bodies (tree);
154 static int  unresolved_type_p (tree, tree *);
155 static void create_jdep_list (struct parser_ctxt *);
156 static tree build_expr_block (tree, tree);
157 static tree enter_block (void);
158 static tree exit_block (void);
159 static tree lookup_name_in_blocks (tree);
160 static void maybe_absorb_scoping_blocks (void);
161 static tree build_method_invocation (tree, tree);
162 static tree build_new_invocation (tree, tree);
163 static tree build_assignment (int, int, tree, tree);
164 static tree build_binop (enum tree_code, int, tree, tree);
165 static tree patch_assignment (tree, tree);
166 static tree patch_binop (tree, tree, tree, int);
167 static tree build_unaryop (int, int, tree);
168 static tree build_incdec (int, int, tree, int);
169 static tree patch_unaryop (tree, tree);
170 static tree build_cast (int, tree, tree);
171 static tree build_null_of_type (tree);
172 static tree patch_cast (tree, tree);
173 static int valid_ref_assignconv_cast_p (tree, tree, int);
174 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
175 static int valid_cast_to_p (tree, tree);
176 static int valid_method_invocation_conversion_p (tree, tree);
177 static tree try_builtin_assignconv (tree, tree, tree);
178 static tree try_reference_assignconv (tree, tree);
179 static tree build_unresolved_array_type (tree);
180 static int build_type_name_from_array_name (tree, tree *);
181 static tree build_array_from_name (tree, tree, tree, tree *);
182 static tree build_array_ref (int, tree, tree);
183 static tree patch_array_ref (tree);
184 #ifdef USE_MAPPED_LOCATION
185 static tree make_qualified_name (tree, tree, source_location);
186 #else
187 static tree make_qualified_name (tree, tree, int);
188 #endif
189 static tree merge_qualified_name (tree, tree);
190 static tree make_qualified_primary (tree, tree, int);
191 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
192 static void qualify_ambiguous_name (tree);
193 static tree resolve_field_access (tree, tree *, tree *);
194 static tree build_newarray_node (tree, tree, int);
195 static tree patch_newarray (tree);
196 static tree resolve_type_during_patch (tree);
197 static tree build_this (int);
198 static tree build_wfl_wrap (tree, int);
199 static tree build_return (int, tree);
200 static tree patch_return (tree);
201 static tree maybe_access_field (tree, tree, tree);
202 static int complete_function_arguments (tree);
203 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
204 static int not_accessible_p (tree, tree, tree, int);
205 static void check_deprecation (tree, tree);
206 static int class_in_current_package (tree);
207 static tree build_if_else_statement (int, tree, tree, tree);
208 static tree patch_if_else_statement (tree);
209 static tree add_stmt_to_block (tree, tree, tree);
210 static tree patch_exit_expr (tree);
211 static tree build_labeled_block (int, tree);
212 static tree finish_labeled_statement (tree, tree);
213 static tree build_bc_statement (int, int, tree);
214 static tree patch_bc_statement (tree);
215 static tree patch_loop_statement (tree);
216 static tree build_new_loop (tree);
217 static tree build_loop_body (int, tree, int);
218 static tree finish_loop_body (int, tree, tree, int);
219 static tree build_debugable_stmt (int, tree);
220 static tree finish_for_loop (int, tree, tree, tree);
221 static tree patch_switch_statement (tree);
222 static tree string_constant_concatenation (tree, tree);
223 static tree build_string_concatenation (tree, tree);
224 static tree patch_string_cst (tree);
225 static tree patch_string (tree);
226 static tree encapsulate_with_try_catch (int, tree, tree, tree);
227 #ifdef USE_MAPPED_LOCATION
228 static tree build_assertion (source_location, tree, tree);
229 #else
230 static tree build_assertion (int, tree, tree);
231 #endif
232 static tree build_try_statement (int, tree, tree);
233 static tree build_try_finally_statement (int, tree, tree);
234 static tree patch_try_statement (tree);
235 static tree patch_synchronized_statement (tree, tree);
236 static tree patch_throw_statement (tree, tree);
237 static void add_exception_to_throws (tree, tree);
238 #ifdef USE_MAPPED_LOCATION
239 static void check_thrown_exceptions (source_location, tree, tree);
240 #else
241 static void check_thrown_exceptions (int, tree, tree);
242 #endif
243 static int check_thrown_exceptions_do (tree);
244 static bool ctors_unchecked_throws_clause_p (tree);
245 static void check_concrete_throws_clauses (tree, tree, tree, tree);
246 static void check_throws_clauses (tree, tree, tree);
247 static void finish_method_declaration (tree);
248 static tree build_super_invocation (tree);
249 static int verify_constructor_circularity (tree, tree);
250 static char *constructor_circularity_msg (tree, tree);
251 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
252 static const char *get_printable_method_name (tree);
253 static tree patch_conditional_expr (tree, tree, tree);
254 static tree generate_finit (tree);
255 static tree generate_instinit (tree);
256 static tree build_instinit_invocation (tree);
257 static void fix_constructors (tree);
258 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
259 static tree craft_constructor (tree, tree);
260 static tree get_constructor_super (tree);
261 static tree create_artificial_method (tree, int, tree, tree, tree);
262 static void start_artificial_method_body (tree);
263 static void end_artificial_method_body (tree);
264 static int check_method_redefinition (tree, tree);
265 static int check_method_types_complete (tree);
266 static bool hack_is_accessible_p (tree, tree);
267 static void java_check_regular_methods (tree);
268 static void check_interface_throws_clauses (tree, tree);
269 static void java_check_abstract_methods (tree);
270 static void unreachable_stmt_error (tree);
271 static int not_accessible_field_error (tree, tree);
272 static tree find_expr_with_wfl (tree);
273 static void missing_return_error (tree);
274 static tree build_new_array_init (int, tree);
275 static tree patch_new_array_init (tree, tree);
276 static tree maybe_build_array_element_wfl (tree);
277 static int array_constructor_check_entry (tree, constructor_elt *);
278 static const char *purify_type_name (const char *);
279 static tree fold_constant_for_init (tree, tree);
280 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
281 static void static_ref_err (tree, tree, tree);
282 static void parser_add_interface (tree, tree, tree);
283 static void add_superinterfaces (tree, tree);
284 static tree jdep_resolve_class (jdep *);
285 static int note_possible_classname (const char *, int);
286 static void java_complete_expand_classes (void);
287 static void java_complete_expand_class (tree);
288 static void java_complete_expand_methods (tree);
289 static tree cut_identifier_in_qualified (tree);
290 static tree java_stabilize_reference (tree);
291 static tree do_unary_numeric_promotion (tree);
292 static char * operator_string (tree);
293 static tree do_merge_string_cste (tree, const char *, int, int);
294 static tree merge_string_cste (tree, tree, int);
295 static tree java_refold (tree);
296 static int java_decl_equiv (tree, tree);
297 static int binop_compound_p (enum tree_code);
298 static tree search_loop (tree);
299 static int labeled_block_contains_loop_p (tree, tree);
300 static int check_abstract_method_definitions (int, tree, tree);
301 static void java_check_abstract_method_definitions (tree);
302 static void java_debug_context_do (int);
303 static void java_parser_context_push_initialized_field (void);
304 static void java_parser_context_pop_initialized_field (void);
305 static tree reorder_static_initialized (tree);
306 static void java_parser_context_suspend (void);
307 static void java_parser_context_resume (void);
308 static int pop_current_osb (struct parser_ctxt *);
309
310 /* JDK 1.1 work. FIXME */
311
312 static tree maybe_make_nested_class_name (tree);
313 static int make_nested_class_name (tree);
314 static void link_nested_class_to_enclosing (void);
315 static tree resolve_inner_class (tree, tree, tree, tree);
316 static tree find_as_inner_class (tree, tree, tree);
317 static tree find_as_inner_class_do (tree, tree);
318 static int check_inner_class_redefinition (tree, tree);
319
320 static tree build_thisn_assign (void);
321 static tree build_current_thisn (tree);
322 static tree build_access_to_thisn (tree, tree, int);
323 static tree maybe_build_thisn_access_method (tree);
324
325 static tree build_nested_field_access (tree, tree);
326 static tree build_nested_field_access_methods (tree);
327 static tree build_nested_field_access_method (tree, tree, tree, tree, tree);
328 static tree build_nested_field_access_expr (int, tree, tree, tree, tree);
329 static tree build_nested_method_access_method (tree);
330 static tree build_new_access_id (void);
331
332 static int nested_member_access_p (tree, tree);
333 static int nested_field_expanded_access_p (tree, tree *, tree *, tree *);
334 static tree nested_field_access_fix (tree, tree, tree);
335
336 static tree build_incomplete_class_ref (int, tree);
337 static tree patch_incomplete_class_ref (tree);
338 static tree create_anonymous_class (tree);
339 static void patch_anonymous_class (tree, tree, tree);
340 static void add_inner_class_fields (tree, tree);
341
342 static tree build_dot_class_method (tree);
343 static tree build_dot_class_method_invocation (tree, tree);
344 static void create_new_parser_context (int);
345 static tree maybe_build_class_init_for_field (tree, tree);
346
347 static int emit_test_initialization (void **, void *);
348
349 static char *string_convert_int_cst (tree);
350
351 /* Number of error found so far. */
352 int java_error_count;
353 /* Number of warning found so far. */
354 int java_warning_count;
355 /* Cyclic inheritance report, as it can be set by layout_class */
356 const char *cyclic_inheritance_report;
357
358 /* The current parser context */
359 struct parser_ctxt *ctxp;
360
361 /* List of things that were analyzed for which code will be generated */
362 struct parser_ctxt *ctxp_for_generation = NULL;
363 struct parser_ctxt *ctxp_for_generation_last = NULL;
364
365 /* binop_lookup maps token to tree_code. It is used where binary
366    operations are involved and required by the parser. RDIV_EXPR
367    covers both integral/floating point division. The code is changed
368    once the type of both operator is worked out.  */
369
370 static const enum tree_code binop_lookup[19] =
371   {
372     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
373     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
374     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
375     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
376     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
377    };
378 #define BINOP_LOOKUP(VALUE)                                             \
379   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
380
381 /* This is the end index for binary operators that can also be used
382    in compound assignments. */
383 #define BINOP_COMPOUND_CANDIDATES 11
384
385 /* The "$L" identifier we use to create labels.  */
386 static GTY(()) tree label_id;
387
388 /* The "StringBuffer" identifier used for the String `+' operator. */
389 static GTY(()) tree wfl_string_buffer;
390
391 /* The "append" identifier used for String `+' operator.  */
392 static GTY(()) tree wfl_append;
393
394 /* The "toString" identifier used for String `+' operator. */
395 static GTY(()) tree wfl_to_string;
396
397 /* The "java.lang" import qualified name.  */
398 static GTY(()) tree java_lang_id;
399
400 /* The generated `inst$' identifier used for generated enclosing
401    instance/field access functions.  */
402 static GTY(()) tree inst_id;
403
404 /* Context and flag for static blocks */
405 static GTY(()) tree current_static_block;
406
407 /* The generated `write_parm_value$' identifier.  */
408 static GTY(()) tree wpv_id;
409
410 /* Hold THIS for the scope of the current method decl.  */
411 static GTY(()) tree current_this;
412
413 /* Hold a list of catch clauses list. The first element of this list is
414    the list of the catch clauses of the currently analyzed try block. */
415 static GTY(()) tree currently_caught_type_list;
416
417 /* This holds a linked list of all the case labels for the current
418    switch statement.  It is only used when checking to see if there
419    are duplicate labels.  FIXME: probably this should just be attached
420    to the switch itself; then it could be referenced via
421    `ctxp->current_loop'.  */
422 static GTY(()) tree case_label_list;
423
424 /* Anonymous class counter. Will be reset to 1 every time a non
425    anonymous class gets created. */
426 static int anonymous_class_counter = 1;
427
428 static GTY(()) tree src_parse_roots[1];
429
430 /* All classes seen from source code */
431 #define gclass_list src_parse_roots[0]
432
433 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
434    line and point it out.  */
435 /* Should point out the one that don't fit. ASCII/unicode, going
436    backward. FIXME */
437
438 #define check_modifiers(__message, __value, __mask) do {        \
439   if ((__value) & ~(__mask))                                    \
440     {                                                           \
441       size_t i, remainder = (__value) & ~(__mask);              \
442       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)     \
443         if ((1 << i) & remainder)                               \
444           parse_error_context (ctxp->modifier_ctx [i], (__message), \
445                                java_accstring_lookup (1 << i)); \
446     }                                                           \
447 } while (0)
448
449 %}
450
451 %union {
452   tree node;
453   int sub_token;
454   struct {
455     int token;
456 #ifdef USE_MAPPED_LOCATION
457     source_location location;
458 #else
459     int location;
460 #endif
461   } operator;
462   int value;
463 }
464
465 %{
466 #ifdef USE_MAPPED_LOCATION
467 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
468   SET_EXPR_LOCATION(EXPR, (TOKEN).location)
469 #else
470 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
471   (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
472 #endif
473
474 #include "lex.c"
475 %}
476
477 %pure_parser
478
479 /* Things defined here have to match the order of what's in the
480    binop_lookup table.  */
481
482 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
483 %token   LS_TK           SRS_TK          ZRS_TK
484 %token   AND_TK          XOR_TK          OR_TK
485 %token   BOOL_AND_TK BOOL_OR_TK
486 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
487
488 /* This maps to the same binop_lookup entry than the token above */
489
490 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
491 %token   REM_ASSIGN_TK
492 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
493 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
494
495
496 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
497
498 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
499 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
500 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
501 %token   PAD_TK          ABSTRACT_TK        STRICT_TK
502 %token   MODIFIER_TK
503
504 /* Keep those two in order, too */
505 %token   DECR_TK INCR_TK
506
507 /* From now one, things can be in any order */
508
509 %token   DEFAULT_TK      IF_TK              THROW_TK
510 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
511 %token   THROWS_TK       BREAK_TK           IMPORT_TK
512 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
513 %token   VOID_TK         CATCH_TK           INTERFACE_TK
514 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
515 %token   SUPER_TK        WHILE_TK           CLASS_TK
516 %token   SWITCH_TK       CONST_TK           TRY_TK
517 %token   FOR_TK          NEW_TK             CONTINUE_TK
518 %token   GOTO_TK         PACKAGE_TK         THIS_TK
519 %token   ASSERT_TK
520
521 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
522 %token   CHAR_TK         INTEGRAL_TK
523
524 %token   FLOAT_TK        DOUBLE_TK          FP_TK
525
526 %token   ID_TK
527
528 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
529
530 %token   ASSIGN_ANY_TK   ASSIGN_TK
531 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
532
533 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
534 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
535
536 %type    <value>        modifiers MODIFIER_TK final synchronized
537
538 %type    <node>         super ID_TK identifier
539 %type    <node>         name simple_name qualified_name
540 %type    <node>         type_declaration compilation_unit
541                         field_declaration method_declaration extends_interfaces
542                         interfaces interface_type_list
543                         import_declarations package_declaration
544                         type_declarations interface_body
545                         interface_member_declaration constant_declaration
546                         interface_member_declarations interface_type
547                         abstract_method_declaration
548 %type    <node>         class_body_declaration class_member_declaration
549                         static_initializer constructor_declaration block
550 %type    <node>         class_body_declarations constructor_header
551 %type    <node>         class_or_interface_type class_type class_type_list
552                         constructor_declarator explicit_constructor_invocation
553 %type    <node>         dim_expr dim_exprs this_or_super throws
554
555 %type    <node>         variable_declarator_id variable_declarator
556                         variable_declarators variable_initializer
557                         variable_initializers constructor_body
558                         array_initializer
559
560 %type    <node>         class_body block_end constructor_block_end
561 %type    <node>         statement statement_without_trailing_substatement
562                         labeled_statement if_then_statement label_decl
563                         if_then_else_statement while_statement for_statement
564                         statement_nsi labeled_statement_nsi do_statement
565                         if_then_else_statement_nsi while_statement_nsi
566                         for_statement_nsi statement_expression_list for_init
567                         for_update statement_expression expression_statement
568                         primary_no_new_array expression primary array_type
569                         array_creation_initialized array_creation_uninitialized
570                         class_instance_creation_expression field_access
571                         method_invocation array_access something_dot_new
572                         argument_list postfix_expression while_expression
573                         post_increment_expression post_decrement_expression
574                         unary_expression_not_plus_minus unary_expression
575                         pre_increment_expression pre_decrement_expression
576                         cast_expression
577                         multiplicative_expression additive_expression
578                         shift_expression relational_expression
579                         equality_expression and_expression
580                         exclusive_or_expression inclusive_or_expression
581                         conditional_and_expression conditional_or_expression
582                         conditional_expression assignment_expression
583                         left_hand_side assignment for_header for_begin
584                         constant_expression do_statement_begin empty_statement
585                         switch_statement synchronized_statement throw_statement
586                         try_statement assert_statement
587                         switch_expression switch_block
588                         catches catch_clause catch_clause_parameter finally
589                         anonymous_class_creation trap_overflow_corner_case
590 %type    <node>         return_statement break_statement continue_statement
591
592 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK
593 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
594 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
595 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
596 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
597 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
598 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
599 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
600 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
601 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
602 %type    <operator>     THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
603 %type    <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
604 %type    <operator>     NEW_TK ASSERT_TK
605
606 %type    <node>         method_body
607
608 %type    <node>         literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
609                         STRING_LIT_TK NULL_TK VOID_TK
610
611 %type    <node>         IF_TK WHILE_TK FOR_TK
612
613 %type    <node>         formal_parameter_list formal_parameter
614                         method_declarator method_header
615
616 %type    <node>         primitive_type reference_type type
617                         BOOLEAN_TK INTEGRAL_TK FP_TK
618
619 /* Added or modified JDK 1.1 rule types  */
620 %type    <node>         type_literals
621
622 %%
623 /* 19.2 Production from 2.3: The Syntactic Grammar  */
624 goal:  compilation_unit
625                 {}
626 ;
627
628 /* 19.3 Productions from 3: Lexical structure  */
629 literal:
630         INT_LIT_TK
631 |       FP_LIT_TK
632 |       BOOL_LIT_TK
633 |       CHAR_LIT_TK
634 |       STRING_LIT_TK
635 |       NULL_TK
636 ;
637
638 /* 19.4 Productions from 4: Types, Values and Variables  */
639 type:
640         primitive_type
641 |       reference_type
642 ;
643
644 primitive_type:
645         INTEGRAL_TK
646 |       FP_TK
647 |       BOOLEAN_TK
648 ;
649
650 reference_type:
651         class_or_interface_type
652 |       array_type
653 ;
654
655 class_or_interface_type:
656         name
657 ;
658
659 class_type:
660         class_or_interface_type /* Default rule */
661 ;
662
663 interface_type:
664          class_or_interface_type
665 ;
666
667 array_type:
668         primitive_type dims
669                 {
670                   int osb = pop_current_osb (ctxp);
671                   tree t = build_java_array_type (($1), -1);
672                   while (--osb)
673                     t = build_unresolved_array_type (t);
674                   $$ = t;
675                 }
676 |       name dims
677                 {
678                   int osb = pop_current_osb (ctxp);
679                   tree t = $1;
680                   while (osb--)
681                     t = build_unresolved_array_type (t);
682                   $$ = t;
683                 }
684 ;
685
686 /* 19.5 Productions from 6: Names  */
687 name:
688         simple_name             /* Default rule */
689 |       qualified_name          /* Default rule */
690 ;
691
692 simple_name:
693         identifier              /* Default rule */
694 ;
695
696 qualified_name:
697         name DOT_TK identifier
698                 { $$ = make_qualified_name ($1, $3, $2.location); }
699 ;
700
701 identifier:
702         ID_TK
703 ;
704
705 /* 19.6: Production from 7: Packages  */
706 compilation_unit:
707                 {$$ = NULL;}
708 |       package_declaration
709 |       import_declarations
710 |       type_declarations
711 |       package_declaration import_declarations
712 |       package_declaration type_declarations
713 |       import_declarations type_declarations
714 |       package_declaration import_declarations type_declarations
715 ;
716
717 import_declarations:
718         import_declaration
719                 {
720                   $$ = NULL;
721                 }
722 |       import_declarations import_declaration
723                 {
724                   $$ = NULL;
725                 }
726 ;
727
728 type_declarations:
729         type_declaration
730 |       type_declarations type_declaration
731 ;
732
733 package_declaration:
734         PACKAGE_TK name SC_TK
735                 {
736                   ctxp->package = EXPR_WFL_NODE ($2);
737                 }
738 |       PACKAGE_TK error
739                 {yyerror ("Missing name"); RECOVER;}
740 |       PACKAGE_TK name error
741                 {yyerror ("';' expected"); RECOVER;}
742 ;
743
744 import_declaration:
745         single_type_import_declaration
746 |       type_import_on_demand_declaration
747 ;
748
749 single_type_import_declaration:
750         IMPORT_TK name SC_TK
751                 {
752                   tree name = EXPR_WFL_NODE ($2), last_name;
753                   int   i = IDENTIFIER_LENGTH (name)-1;
754                   const char *last = &IDENTIFIER_POINTER (name)[i];
755                   while (last != IDENTIFIER_POINTER (name))
756                     {
757                       if (last [0] == '.')
758                         break;
759                       last--;
760                     }
761                   last_name = get_identifier (++last);
762                   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
763                     {
764                       tree err = find_name_in_single_imports (last_name);
765                       if (err && err != name)
766                         parse_error_context
767                           ($2, "Ambiguous class: %qs and %qs",
768                            IDENTIFIER_POINTER (name),
769                            IDENTIFIER_POINTER (err));
770                       else
771                         REGISTER_IMPORT ($2, last_name);
772                     }
773                   else
774                     REGISTER_IMPORT ($2, last_name);
775                 }
776 |       IMPORT_TK error
777                 {yyerror ("Missing name"); RECOVER;}
778 |       IMPORT_TK name error
779                 {yyerror ("';' expected"); RECOVER;}
780 ;
781
782 type_import_on_demand_declaration:
783         IMPORT_TK name DOT_TK MULT_TK SC_TK
784                 {
785                   tree name = EXPR_WFL_NODE ($2);
786                   tree it;
787                   /* Search for duplicates. */
788                   for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
789                     if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
790                       break;
791                   /* Don't import the same thing more than once, just ignore
792                      duplicates (7.5.2) */
793                   if (! it)
794                     {
795                       read_import_dir ($2);
796                       ctxp->import_demand_list =
797                         chainon (ctxp->import_demand_list,
798                                  build_tree_list ($2, NULL_TREE));
799                     }
800                 }
801 |       IMPORT_TK name DOT_TK error
802                 {yyerror ("'*' expected"); RECOVER;}
803 |       IMPORT_TK name DOT_TK MULT_TK error
804                 {yyerror ("';' expected"); RECOVER;}
805 ;
806
807 type_declaration:
808         class_declaration
809                 { end_class_declaration (0); }
810 |       interface_declaration
811                 { end_class_declaration (0); }
812 |       empty_statement
813 |       error
814                 {
815                   YYERROR_NOW;
816                   yyerror ("Class or interface declaration expected");
817                 }
818 ;
819
820 /* 19.7 Shortened from the original:
821    modifiers: modifier | modifiers modifier
822    modifier: any of public...  */
823 modifiers:
824         MODIFIER_TK
825                 {
826                   $$ = (1 << $1);
827                 }
828 |       modifiers MODIFIER_TK
829                 {
830                   int acc = (1 << $2);
831                   if ($$ & acc)
832                     parse_error_context
833                       (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
834                        java_accstring_lookup (acc));
835                   else
836                     {
837                       $$ |= acc;
838                     }
839                 }
840 ;
841
842 /* 19.8.1 Production from $8.1: Class Declaration */
843 class_declaration:
844         modifiers CLASS_TK identifier super interfaces
845                 { create_class ($1, $3, $4, $5); }
846         class_body
847                 {;}
848 |       CLASS_TK identifier super interfaces
849                 { create_class (0, $2, $3, $4); }
850         class_body
851                 {;}
852 |       modifiers CLASS_TK error
853                 { yyerror ("Missing class name"); RECOVER; }
854 |       CLASS_TK error
855                 { yyerror ("Missing class name"); RECOVER; }
856 |       CLASS_TK identifier error
857                 {
858                   if (!ctxp->class_err) yyerror ("'{' expected");
859                   DRECOVER(class1);
860                 }
861 |       modifiers CLASS_TK identifier error
862                 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
863 ;
864
865 super:
866                 { $$ = NULL; }
867 |       EXTENDS_TK class_type
868                 { $$ = $2; }
869 |       EXTENDS_TK class_type error
870                 {yyerror ("'{' expected"); ctxp->class_err=1;}
871 |       EXTENDS_TK error
872                 {yyerror ("Missing super class name"); ctxp->class_err=1;}
873 ;
874
875 interfaces:
876                 { $$ = NULL_TREE; }
877 |       IMPLEMENTS_TK interface_type_list
878                 { $$ = $2; }
879 |       IMPLEMENTS_TK error
880                 {
881                   ctxp->class_err=1;
882                   yyerror ("Missing interface name");
883                 }
884 ;
885
886 interface_type_list:
887         interface_type
888                 {
889                   ctxp->interface_number = 1;
890                   $$ = build_tree_list ($1, NULL_TREE);
891                 }
892 |       interface_type_list C_TK interface_type
893                 {
894                   ctxp->interface_number++;
895                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
896                 }
897 |       interface_type_list C_TK error
898                 {yyerror ("Missing interface name"); RECOVER;}
899 ;
900
901 class_body:
902         OCB_TK CCB_TK
903                 {
904                   $$ = GET_CPC ();
905                 }
906 |       OCB_TK class_body_declarations CCB_TK
907                 {
908                   $$ = GET_CPC ();
909                 }
910 ;
911
912 class_body_declarations:
913         class_body_declaration
914 |       class_body_declarations class_body_declaration
915 ;
916
917 class_body_declaration:
918         class_member_declaration
919 |       static_initializer
920 |       constructor_declaration
921 |       block                   /* Added, JDK1.1, instance initializer */
922                 {
923                   if (!IS_EMPTY_STMT ($1))
924                     {
925                       TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
926                       SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
927                     }
928                 }
929 ;
930
931 class_member_declaration:
932         field_declaration
933 |       method_declaration
934 |       class_declaration       /* Added, JDK1.1 inner classes */
935                 { end_class_declaration (1); }
936 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
937                 { end_class_declaration (1); }
938 |       empty_statement
939 ;
940
941 /* 19.8.2 Productions from 8.3: Field Declarations  */
942 field_declaration:
943         type variable_declarators SC_TK
944                 { register_fields (0, $1, $2); }
945 |       modifiers type variable_declarators SC_TK
946                 {
947                   check_modifiers
948                     ("Illegal modifier %qs for field declaration",
949                      $1, FIELD_MODIFIERS);
950                   check_modifiers_consistency ($1);
951                   register_fields ($1, $2, $3);
952                 }
953 ;
954
955 variable_declarators:
956         /* Should we use build_decl_list () instead ? FIXME */
957         variable_declarator     /* Default rule */
958 |       variable_declarators C_TK variable_declarator
959                 { $$ = chainon ($1, $3); }
960 |       variable_declarators C_TK error
961                 {yyerror ("Missing term"); RECOVER;}
962 ;
963
964 variable_declarator:
965         variable_declarator_id
966                 { $$ = build_tree_list ($1, NULL_TREE); }
967 |       variable_declarator_id ASSIGN_TK variable_initializer
968                 {
969                   if (java_error_count)
970                     $3 = NULL_TREE;
971                   $$ = build_tree_list
972                     ($1, build_assignment ($2.token, $2.location, $1, $3));
973                 }
974 |       variable_declarator_id ASSIGN_TK error
975                 {
976                   yyerror ("Missing variable initializer");
977                   $$ = build_tree_list ($1, NULL_TREE);
978                   RECOVER;
979                 }
980 |       variable_declarator_id ASSIGN_TK variable_initializer error
981                 {
982                   yyerror ("';' expected");
983                   $$ = build_tree_list ($1, NULL_TREE);
984                   RECOVER;
985                 }
986 ;
987
988 variable_declarator_id:
989         identifier
990 |       variable_declarator_id OSB_TK CSB_TK
991                 { $$ = build_unresolved_array_type ($1); }
992 |       identifier error
993                 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
994 |       variable_declarator_id OSB_TK error
995                 {
996                   yyerror ("']' expected");
997                   DRECOVER(vdi);
998                 }
999 |       variable_declarator_id CSB_TK error
1000                 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1001 ;
1002
1003 variable_initializer:
1004         expression
1005 |       array_initializer
1006 ;
1007
1008 /* 19.8.3 Productions from 8.4: Method Declarations  */
1009 method_declaration:
1010         method_header
1011                 {
1012                   current_function_decl = $1;
1013                   if (current_function_decl
1014                       && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1015                     source_start_java_method (current_function_decl);
1016                   else
1017                     current_function_decl = NULL_TREE;
1018                 }
1019         method_body
1020                 { finish_method_declaration ($3); }
1021 |       method_header error
1022                 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1023 ;
1024
1025 method_header:
1026         type method_declarator throws
1027                 { $$ = method_header (0, $1, $2, $3); }
1028 |       VOID_TK method_declarator throws
1029                 { $$ = method_header (0, void_type_node, $2, $3); }
1030 |       modifiers type method_declarator throws
1031                 { $$ = method_header ($1, $2, $3, $4); }
1032 |       modifiers VOID_TK method_declarator throws
1033                 { $$ = method_header ($1, void_type_node, $3, $4); }
1034 |       type error
1035                 {
1036                   yyerror ("Invalid method declaration, method name required");
1037                   $$ = NULL_TREE;
1038                   RECOVER;
1039                 }
1040 |       modifiers type error
1041                 {
1042                   yyerror ("Identifier expected");
1043                   $$ = NULL_TREE;
1044                   RECOVER;
1045                 }
1046 |       VOID_TK error
1047                 {
1048                   yyerror ("Identifier expected");
1049                   $$ = NULL_TREE;
1050                   RECOVER;
1051                 }
1052 |       modifiers VOID_TK error
1053                 {
1054                   yyerror ("Identifier expected");
1055                   $$ = NULL_TREE;
1056                   RECOVER;
1057                 }
1058 |       modifiers error
1059                 {
1060                   yyerror ("Invalid method declaration, return type required");
1061                   $$ = NULL_TREE;
1062                   RECOVER;
1063                 }
1064 ;
1065
1066 method_declarator:
1067         identifier OP_TK CP_TK
1068                 {
1069                   ctxp->formal_parameter_number = 0;
1070                   $$ = method_declarator ($1, NULL_TREE);
1071                 }
1072 |       identifier OP_TK formal_parameter_list CP_TK
1073                 { $$ = method_declarator ($1, $3); }
1074 |       method_declarator OSB_TK CSB_TK
1075                 {
1076                   SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1077                   TREE_PURPOSE ($1) =
1078                     build_unresolved_array_type (TREE_PURPOSE ($1));
1079                   parse_warning_context
1080                     (wfl_operator,
1081                      "Discouraged form of returned type specification");
1082                 }
1083 |       identifier OP_TK error
1084                 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1085 |       method_declarator OSB_TK error
1086                 {yyerror ("']' expected"); RECOVER;}
1087 ;
1088
1089 formal_parameter_list:
1090         formal_parameter
1091                 {
1092                   ctxp->formal_parameter_number = 1;
1093                 }
1094 |       formal_parameter_list C_TK formal_parameter
1095                 {
1096                   ctxp->formal_parameter_number += 1;
1097                   $$ = chainon ($1, $3);
1098                 }
1099 |       formal_parameter_list C_TK error
1100                 { yyerror ("Missing formal parameter term"); RECOVER; }
1101 ;
1102
1103 formal_parameter:
1104         type variable_declarator_id
1105                 {
1106                   $$ = build_tree_list ($2, $1);
1107                 }
1108 |       final type variable_declarator_id /* Added, JDK1.1 final parms */
1109                 {
1110                   $$ = build_tree_list ($3, $2);
1111                   ARG_FINAL_P ($$) = 1;
1112                 }
1113 |       type error
1114                 {
1115                   yyerror ("Missing identifier"); RECOVER;
1116                   $$ = NULL_TREE;
1117                 }
1118 |       final type error
1119                 {
1120                   yyerror ("Missing identifier"); RECOVER;
1121                   $$ = NULL_TREE;
1122                 }
1123 ;
1124
1125 final:
1126         modifiers
1127                 {
1128                   check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1129                                    $1, ACC_FINAL);
1130                   if ($1 != ACC_FINAL)
1131                     MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1132                 }
1133 ;
1134
1135 throws:
1136                 { $$ = NULL_TREE; }
1137 |       THROWS_TK class_type_list
1138                 { $$ = $2; }
1139 |       THROWS_TK error
1140                 {yyerror ("Missing class type term"); RECOVER;}
1141 ;
1142
1143 class_type_list:
1144         class_type
1145                 { $$ = build_tree_list ($1, $1); }
1146 |       class_type_list C_TK class_type
1147                 { $$ = tree_cons ($3, $3, $1); }
1148 |       class_type_list C_TK error
1149                 {yyerror ("Missing class type term"); RECOVER;}
1150 ;
1151
1152 method_body:
1153         block
1154 |       SC_TK { $$ = NULL_TREE; }
1155 ;
1156
1157 /* 19.8.4 Productions from 8.5: Static Initializers  */
1158 static_initializer:
1159         static block
1160                 {
1161                   TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1162                   SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1163                   current_static_block = NULL_TREE;
1164                 }
1165 ;
1166
1167 static:                         /* Test lval.sub_token here */
1168         modifiers
1169                 {
1170                   check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1171                   /* Can't have a static initializer in an innerclass */
1172                   if ($1 | ACC_STATIC &&
1173                       GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1174                     parse_error_context
1175                       (MODIFIER_WFL (STATIC_TK),
1176                        "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1177                        IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1178                   SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1179                 }
1180 ;
1181
1182 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1183 constructor_declaration:
1184         constructor_header
1185                 {
1186                   current_function_decl = $1;
1187                   source_start_java_method (current_function_decl);
1188                 }
1189         constructor_body
1190                 { finish_method_declaration ($3); }
1191 ;
1192
1193 constructor_header:
1194         constructor_declarator throws
1195                 { $$ = method_header (0, NULL_TREE, $1, $2); }
1196 |       modifiers constructor_declarator throws
1197                 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1198 ;
1199
1200 constructor_declarator:
1201         simple_name OP_TK CP_TK
1202                 {
1203                   ctxp->formal_parameter_number = 0;
1204                   $$ = method_declarator ($1, NULL_TREE);
1205                 }
1206 |       simple_name OP_TK formal_parameter_list CP_TK
1207                 { $$ = method_declarator ($1, $3); }
1208 ;
1209
1210 constructor_body:
1211         /* Unlike regular method, we always need a complete (empty)
1212            body so we can safely perform all the required code
1213            addition (super invocation and field initialization) */
1214         block_begin constructor_block_end
1215                 {
1216                   BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1217                   $$ = $2;
1218                 }
1219 |       block_begin explicit_constructor_invocation constructor_block_end
1220                 { $$ = $3; }
1221 |       block_begin block_statements constructor_block_end
1222                 { $$ = $3; }
1223 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1224                 { $$ = $4; }
1225 ;
1226
1227 constructor_block_end:
1228         block_end
1229 ;
1230
1231 /* Error recovery for that rule moved down expression_statement: rule.  */
1232 explicit_constructor_invocation:
1233         this_or_super OP_TK CP_TK SC_TK
1234                 {
1235                   $$ = build_method_invocation ($1, NULL_TREE);
1236                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1237                   $$ = java_method_add_stmt (current_function_decl, $$);
1238                 }
1239 |       this_or_super OP_TK argument_list CP_TK SC_TK
1240                 {
1241                   $$ = build_method_invocation ($1, $3);
1242                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1243                   $$ = java_method_add_stmt (current_function_decl, $$);
1244                 }
1245         /* Added, JDK1.1 inner classes. Modified because the rule
1246            'primary' couldn't work.  */
1247 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1248                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1249 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1250                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1251 ;
1252
1253 this_or_super:                  /* Added, simplifies error diagnostics */
1254         THIS_TK
1255                 {
1256                   tree wfl = build_wfl_node (this_identifier_node);
1257                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1258                   $$ = wfl;
1259                 }
1260 |       SUPER_TK
1261                 {
1262                   tree wfl = build_wfl_node (super_identifier_node);
1263                   SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1264                   $$ = wfl;
1265                 }
1266 ;
1267
1268 /* 19.9 Productions from 9: Interfaces  */
1269 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1270 interface_declaration:
1271         INTERFACE_TK identifier
1272                 { create_interface (0, $2, NULL_TREE); }
1273         interface_body
1274                 { ; }
1275 |       modifiers INTERFACE_TK identifier
1276                 { create_interface ($1, $3, NULL_TREE); }
1277         interface_body
1278                 { ; }
1279 |       INTERFACE_TK identifier extends_interfaces
1280                 { create_interface (0, $2, $3); }
1281         interface_body
1282                 { ; }
1283 |       modifiers INTERFACE_TK identifier extends_interfaces
1284                 { create_interface ($1, $3, $4); }
1285         interface_body
1286                 { ; }
1287 |       INTERFACE_TK identifier error
1288                 { yyerror ("'{' expected"); RECOVER; }
1289 |       modifiers INTERFACE_TK identifier error
1290                 { yyerror ("'{' expected"); RECOVER; }
1291 ;
1292
1293 extends_interfaces:
1294         EXTENDS_TK interface_type
1295                 {
1296                   ctxp->interface_number = 1;
1297                   $$ = build_tree_list ($2, NULL_TREE);
1298                 }
1299 |       extends_interfaces C_TK interface_type
1300                 {
1301                   ctxp->interface_number++;
1302                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1303                 }
1304 |       EXTENDS_TK error
1305                 {yyerror ("Invalid interface type"); RECOVER;}
1306 |       extends_interfaces C_TK error
1307                 {yyerror ("Missing term"); RECOVER;}
1308 ;
1309
1310 interface_body:
1311         OCB_TK CCB_TK
1312                 { $$ = NULL_TREE; }
1313 |       OCB_TK interface_member_declarations CCB_TK
1314                 { $$ = NULL_TREE; }
1315 ;
1316
1317 interface_member_declarations:
1318         interface_member_declaration
1319 |       interface_member_declarations interface_member_declaration
1320 ;
1321
1322 interface_member_declaration:
1323         constant_declaration
1324 |       abstract_method_declaration
1325 |       class_declaration       /* Added, JDK1.1 inner classes */
1326                 { end_class_declaration (1); }
1327 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
1328                 { end_class_declaration (1); }
1329 |       empty_statement
1330 ;
1331
1332 constant_declaration:
1333         field_declaration
1334 ;
1335
1336 abstract_method_declaration:
1337         method_header SC_TK
1338                 {
1339                   check_abstract_method_header ($1);
1340                   current_function_decl = NULL_TREE; /* FIXME ? */
1341                 }
1342 |       method_header error
1343                 {yyerror ("';' expected"); RECOVER;}
1344 ;
1345
1346 /* 19.10 Productions from 10: Arrays  */
1347 array_initializer:
1348         OCB_TK CCB_TK
1349                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1350 |       OCB_TK C_TK CCB_TK
1351                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1352 |       OCB_TK variable_initializers CCB_TK
1353                 { $$ = build_new_array_init ($1.location, $2); }
1354 |       OCB_TK variable_initializers C_TK CCB_TK
1355                 { $$ = build_new_array_init ($1.location, $2); }
1356 ;
1357
1358 variable_initializers:
1359         variable_initializer
1360                 {
1361                   $$ = tree_cons (maybe_build_array_element_wfl ($1),
1362                                   $1, NULL_TREE);
1363                 }
1364 |       variable_initializers C_TK variable_initializer
1365                 {
1366                   $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1367                 }
1368 |       variable_initializers C_TK error
1369                 {yyerror ("Missing term"); RECOVER;}
1370 ;
1371
1372 /* 19.11 Production from 14: Blocks and Statements  */
1373 block:
1374         block_begin block_end
1375                 { $$ = $2; }
1376 |       block_begin block_statements block_end
1377                 { $$ = $3; }
1378 ;
1379
1380 block_begin:
1381         OCB_TK
1382                 { enter_block (); }
1383 ;
1384
1385 block_end:
1386         CCB_TK
1387                 {
1388                   maybe_absorb_scoping_blocks ();
1389                   $$ = exit_block ();
1390                   if (!BLOCK_SUBBLOCKS ($$))
1391                     BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1392                 }
1393 ;
1394
1395 block_statements:
1396         block_statement
1397 |       block_statements block_statement
1398 ;
1399
1400 block_statement:
1401         local_variable_declaration_statement
1402 |       statement
1403                 { java_method_add_stmt (current_function_decl, $1); }
1404 |       class_declaration       /* Added, JDK1.1 local classes */
1405                 {
1406                   LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1407                   end_class_declaration (1);
1408                 }
1409 ;
1410
1411 local_variable_declaration_statement:
1412         local_variable_declaration SC_TK /* Can't catch missing ';' here */
1413 ;
1414
1415 local_variable_declaration:
1416         type variable_declarators
1417                 { declare_local_variables (0, $1, $2); }
1418 |       final type variable_declarators /* Added, JDK1.1 final locals */
1419                 { declare_local_variables ($1, $2, $3); }
1420 ;
1421
1422 statement:
1423         statement_without_trailing_substatement
1424 |       labeled_statement
1425 |       if_then_statement
1426 |       if_then_else_statement
1427 |       while_statement
1428 |       for_statement
1429                 { $$ = exit_block (); }
1430 ;
1431
1432 statement_nsi:
1433         statement_without_trailing_substatement
1434 |       labeled_statement_nsi
1435 |       if_then_else_statement_nsi
1436 |       while_statement_nsi
1437 |       for_statement_nsi
1438                 { $$ = exit_block (); }
1439 ;
1440
1441 statement_without_trailing_substatement:
1442         block
1443 |       empty_statement
1444 |       expression_statement
1445 |       switch_statement
1446 |       do_statement
1447 |       break_statement
1448 |       continue_statement
1449 |       return_statement
1450 |       synchronized_statement
1451 |       throw_statement
1452 |       try_statement
1453 |       assert_statement
1454 ;
1455
1456 empty_statement:
1457         SC_TK
1458                 {
1459                   if (flag_extraneous_semicolon
1460                       && ! current_static_block
1461                       && (! current_function_decl ||
1462                           /* Verify we're not in a inner class declaration */
1463                           (GET_CPC () != TYPE_NAME
1464                            (DECL_CONTEXT (current_function_decl)))))
1465
1466                     {
1467 #ifdef USE_MAPPED_LOCATION
1468                       SET_EXPR_LOCATION (wfl_operator, input_location);
1469 #else
1470                       EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1471 #endif
1472                       parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1473                     }
1474                   $$ = build_java_empty_stmt ();
1475                 }
1476 ;
1477
1478 label_decl:
1479         identifier REL_CL_TK
1480                 {
1481                   $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1482                                             EXPR_WFL_NODE ($1));
1483                   pushlevel (2);
1484                   push_labeled_block ($$);
1485                   PUSH_LABELED_BLOCK ($$);
1486                 }
1487 ;
1488
1489 labeled_statement:
1490         label_decl statement
1491                 { $$ = finish_labeled_statement ($1, $2); }
1492 |       identifier error
1493                 {yyerror ("':' expected"); RECOVER;}
1494 ;
1495
1496 labeled_statement_nsi:
1497         label_decl statement_nsi
1498                 { $$ = finish_labeled_statement ($1, $2); }
1499 ;
1500
1501 /* We concentrate here a bunch of error handling rules that we couldn't write
1502    earlier, because expression_statement catches a missing ';'.  */
1503 expression_statement:
1504         statement_expression SC_TK
1505                 {
1506                   /* We have a statement. Generate a WFL around it so
1507                      we can debug it */
1508 #ifdef USE_MAPPED_LOCATION
1509                   $$ = expr_add_location ($1, input_location, 1);
1510 #else
1511                   $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1512                   JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1513 #endif
1514                   /* We know we have a statement, so set the debug
1515                      info to be eventually generate here. */
1516                 }
1517 |       error SC_TK
1518                 {
1519                   YYNOT_TWICE yyerror ("Invalid expression statement");
1520                   DRECOVER (expr_stmt);
1521                 }
1522 |       error OCB_TK
1523                 {
1524                   YYNOT_TWICE yyerror ("Invalid expression statement");
1525                   DRECOVER (expr_stmt);
1526                 }
1527 |       error CCB_TK
1528                 {
1529                   YYNOT_TWICE yyerror ("Invalid expression statement");
1530                   DRECOVER (expr_stmt);
1531                 }
1532 |       this_or_super OP_TK error
1533                 {yyerror ("')' expected"); RECOVER;}
1534 |       this_or_super OP_TK CP_TK error
1535                 {
1536                   parse_ctor_invocation_error ();
1537                   RECOVER;
1538                 }
1539 |       this_or_super OP_TK argument_list error
1540                 {yyerror ("')' expected"); RECOVER;}
1541 |       this_or_super OP_TK argument_list CP_TK error
1542                 {
1543                   parse_ctor_invocation_error ();
1544                   RECOVER;
1545                 }
1546 |       name DOT_TK SUPER_TK error
1547                 {yyerror ("'(' expected"); RECOVER;}
1548 |       name DOT_TK SUPER_TK OP_TK error
1549                 {yyerror ("')' expected"); RECOVER;}
1550 |       name DOT_TK SUPER_TK OP_TK argument_list error
1551                 {yyerror ("')' expected"); RECOVER;}
1552 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1553                 {yyerror ("';' expected"); RECOVER;}
1554 |       name DOT_TK SUPER_TK OP_TK CP_TK error
1555                 {yyerror ("';' expected"); RECOVER;}
1556 ;
1557
1558 statement_expression:
1559         assignment
1560 |       pre_increment_expression
1561 |       pre_decrement_expression
1562 |       post_increment_expression
1563 |       post_decrement_expression
1564 |       method_invocation
1565 |       class_instance_creation_expression
1566 ;
1567
1568 if_then_statement:
1569         IF_TK OP_TK expression CP_TK statement
1570                 {
1571                   $$ = build_if_else_statement ($2.location, $3,
1572                                                 $5, NULL_TREE);
1573                 }
1574 |       IF_TK error
1575                 {yyerror ("'(' expected"); RECOVER;}
1576 |       IF_TK OP_TK error
1577                 {yyerror ("Missing term"); RECOVER;}
1578 |       IF_TK OP_TK expression error
1579                 {yyerror ("')' expected"); RECOVER;}
1580 ;
1581
1582 if_then_else_statement:
1583         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1584                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1585 ;
1586
1587 if_then_else_statement_nsi:
1588         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1589                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1590 ;
1591
1592 switch_statement:
1593         switch_expression
1594                 {
1595                   enter_block ();
1596                 }
1597         switch_block
1598                 {
1599                   /* Make into "proper list" of COMPOUND_EXPRs.
1600                      I.e. make the last statement also have its own
1601                      COMPOUND_EXPR. */
1602                   maybe_absorb_scoping_blocks ();
1603                   TREE_OPERAND ($1, 1) = exit_block ();
1604                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1605                 }
1606 ;
1607
1608 switch_expression:
1609         SWITCH_TK OP_TK expression CP_TK
1610                 {
1611                   $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1612                                NULL_TREE, NULL_TREE);
1613                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1614                 }
1615 |       SWITCH_TK error
1616                 {yyerror ("'(' expected"); RECOVER;}
1617 |       SWITCH_TK OP_TK error
1618                 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1619 |       SWITCH_TK OP_TK expression CP_TK error
1620                 {yyerror ("'{' expected"); RECOVER;}
1621 ;
1622
1623 /* Default assignment is there to avoid type node on switch_block
1624    node. */
1625
1626 switch_block:
1627         OCB_TK CCB_TK
1628                 { $$ = NULL_TREE; }
1629 |       OCB_TK switch_labels CCB_TK
1630                 { $$ = NULL_TREE; }
1631 |       OCB_TK switch_block_statement_groups CCB_TK
1632                 { $$ = NULL_TREE; }
1633 |       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1634                 { $$ = NULL_TREE; }
1635 ;
1636
1637 switch_block_statement_groups:
1638         switch_block_statement_group
1639 |       switch_block_statement_groups switch_block_statement_group
1640 ;
1641
1642 switch_block_statement_group:
1643         switch_labels block_statements
1644 ;
1645
1646 switch_labels:
1647         switch_label
1648 |       switch_labels switch_label
1649 ;
1650
1651 switch_label:
1652         CASE_TK constant_expression REL_CL_TK
1653                 {
1654                   tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1655                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1656                   java_method_add_stmt (current_function_decl, lab);
1657                 }
1658 |       DEFAULT_TK REL_CL_TK
1659                 {
1660                   tree lab = make_node (DEFAULT_EXPR);
1661                   SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1662                   java_method_add_stmt (current_function_decl, lab);
1663                 }
1664 |       CASE_TK error
1665                 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1666 |       CASE_TK constant_expression error
1667                 {yyerror ("':' expected"); RECOVER;}
1668 |       DEFAULT_TK error
1669                 {yyerror ("':' expected"); RECOVER;}
1670 ;
1671
1672 while_expression:
1673         WHILE_TK OP_TK expression CP_TK
1674                 {
1675                   tree body = build_loop_body ($2.location, $3, 0);
1676                   $$ = build_new_loop (body);
1677                 }
1678 ;
1679
1680 while_statement:
1681         while_expression statement
1682                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1683 |       WHILE_TK error
1684                 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1685 |       WHILE_TK OP_TK error
1686                 {yyerror ("Missing term and ')' expected"); RECOVER;}
1687 |       WHILE_TK OP_TK expression error
1688                 {yyerror ("')' expected"); RECOVER;}
1689 ;
1690
1691 while_statement_nsi:
1692         while_expression statement_nsi
1693                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1694 ;
1695
1696 do_statement_begin:
1697         DO_TK
1698                 {
1699                   tree body = build_loop_body (0, NULL_TREE, 1);
1700                   $$ = build_new_loop (body);
1701                 }
1702         /* Need error handing here. FIXME */
1703 ;
1704
1705 do_statement:
1706         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1707                 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1708 ;
1709
1710 for_statement:
1711         for_begin SC_TK expression SC_TK for_update CP_TK statement
1712                 {
1713                   if (CONSTANT_CLASS_P ($3))
1714                     $3 = build_wfl_node ($3);
1715                   $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1716                 }
1717 |       for_begin SC_TK SC_TK for_update CP_TK statement
1718                 {
1719                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1720                   /* We have not condition, so we get rid of the EXIT_EXPR */
1721                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1722                     build_java_empty_stmt ();
1723                 }
1724 |       for_begin SC_TK error
1725                 {yyerror ("Invalid control expression"); RECOVER;}
1726 |       for_begin SC_TK expression SC_TK error
1727                 {yyerror ("Invalid update expression"); RECOVER;}
1728 |       for_begin SC_TK SC_TK error
1729                 {yyerror ("Invalid update expression"); RECOVER;}
1730 ;
1731
1732 for_statement_nsi:
1733         for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1734                 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1735 |       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1736                 {
1737                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1738                   /* We have not condition, so we get rid of the EXIT_EXPR */
1739                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1740                     build_java_empty_stmt ();
1741                 }
1742 ;
1743
1744 for_header:
1745         FOR_TK OP_TK
1746                 {
1747                   /* This scope defined for local variable that may be
1748                      defined within the scope of the for loop */
1749                   enter_block ();
1750                 }
1751 |       FOR_TK error
1752                 {yyerror ("'(' expected"); DRECOVER(for_1);}
1753 |       FOR_TK OP_TK error
1754                 {yyerror ("Invalid init statement"); RECOVER;}
1755 ;
1756
1757 for_begin:
1758         for_header for_init
1759                 {
1760                   /* We now declare the loop body. The loop is
1761                      declared as a for loop. */
1762                   tree body = build_loop_body (0, NULL_TREE, 0);
1763                   $$ =  build_new_loop (body);
1764                   FOR_LOOP_P ($$) = 1;
1765                   /* The loop is added to the current block the for
1766                      statement is defined within */
1767                   java_method_add_stmt (current_function_decl, $$);
1768                 }
1769 ;
1770 for_init:                       /* Can be empty */
1771                 { $$ = build_java_empty_stmt (); }
1772 |       statement_expression_list
1773                 {
1774                   /* Init statement recorded within the previously
1775                      defined block scope */
1776                   $$ = java_method_add_stmt (current_function_decl, $1);
1777                 }
1778 |       local_variable_declaration
1779                 {
1780                   /* Local variable are recorded within the previously
1781                      defined block scope */
1782                   $$ = NULL_TREE;
1783                 }
1784 |       statement_expression_list error
1785                 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1786 ;
1787
1788 for_update:                     /* Can be empty */
1789                 {$$ = build_java_empty_stmt ();}
1790 |       statement_expression_list
1791                 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1792 ;
1793
1794 statement_expression_list:
1795         statement_expression
1796                 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1797 |       statement_expression_list C_TK statement_expression
1798                 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1799 |       statement_expression_list C_TK error
1800                 {yyerror ("Missing term"); RECOVER;}
1801 ;
1802
1803 break_statement:
1804         BREAK_TK SC_TK
1805                 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1806 |       BREAK_TK identifier SC_TK
1807                 { $$ = build_bc_statement ($1.location, 1, $2); }
1808 |       BREAK_TK error
1809                 {yyerror ("Missing term"); RECOVER;}
1810 |       BREAK_TK identifier error
1811                 {yyerror ("';' expected"); RECOVER;}
1812 ;
1813
1814 continue_statement:
1815         CONTINUE_TK SC_TK
1816                 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1817 |       CONTINUE_TK identifier SC_TK
1818                 { $$ = build_bc_statement ($1.location, 0, $2); }
1819 |       CONTINUE_TK error
1820                 {yyerror ("Missing term"); RECOVER;}
1821 |       CONTINUE_TK identifier error
1822                 {yyerror ("';' expected"); RECOVER;}
1823 ;
1824
1825 return_statement:
1826         RETURN_TK SC_TK
1827                 { $$ = build_return ($1.location, NULL_TREE); }
1828 |       RETURN_TK expression SC_TK
1829                 { $$ = build_return ($1.location, $2); }
1830 |       RETURN_TK error
1831                 {yyerror ("Missing term"); RECOVER;}
1832 |       RETURN_TK expression error
1833                 {yyerror ("';' expected"); RECOVER;}
1834 ;
1835
1836 throw_statement:
1837         THROW_TK expression SC_TK
1838                 {
1839                   $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1840                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1841                 }
1842 |       THROW_TK error
1843                 {yyerror ("Missing term"); RECOVER;}
1844 |       THROW_TK expression error
1845                 {yyerror ("';' expected"); RECOVER;}
1846 ;
1847
1848 assert_statement:
1849         ASSERT_TK expression REL_CL_TK expression SC_TK
1850                 {
1851                   $$ = build_assertion ($1.location, $2, $4);
1852                 }
1853 |       ASSERT_TK expression SC_TK
1854                 {
1855                   $$ = build_assertion ($1.location, $2, NULL_TREE);
1856                 }
1857 |       ASSERT_TK error
1858                 {yyerror ("Missing term"); RECOVER;}
1859 |       ASSERT_TK expression error
1860                 {yyerror ("';' expected"); RECOVER;}
1861 ;
1862
1863 synchronized_statement:
1864         synchronized OP_TK expression CP_TK block
1865                 {
1866                   $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1867                   EXPR_WFL_LINECOL ($$) =
1868                     EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1869                 }
1870 |       synchronized OP_TK expression CP_TK error
1871                 {yyerror ("'{' expected"); RECOVER;}
1872 |       synchronized error
1873                 {yyerror ("'(' expected"); RECOVER;}
1874 |       synchronized OP_TK error CP_TK
1875                 {yyerror ("Missing term"); RECOVER;}
1876 |       synchronized OP_TK error
1877                 {yyerror ("Missing term"); RECOVER;}
1878 ;
1879
1880 synchronized:
1881         modifiers
1882                 {
1883                   check_modifiers (
1884              "Illegal modifier %qs. Only %<synchronized%> was expected here",
1885                                    $1, ACC_SYNCHRONIZED);
1886                   if ($1 != ACC_SYNCHRONIZED)
1887                     MODIFIER_WFL (SYNCHRONIZED_TK) =
1888                       build_wfl_node (NULL_TREE);
1889                 }
1890 ;
1891
1892 try_statement:
1893         TRY_TK block catches
1894                 { $$ = build_try_statement ($1.location, $2, $3); }
1895 |       TRY_TK block finally
1896                 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1897 |       TRY_TK block catches finally
1898                 { $$ = build_try_finally_statement
1899                     ($1.location, build_try_statement ($1.location,
1900                                                        $2, $3), $4);
1901                 }
1902 |       TRY_TK error
1903                 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1904 ;
1905
1906 catches:
1907         catch_clause
1908 |       catches catch_clause
1909                 {
1910                   TREE_CHAIN ($2) = $1;
1911                   $$ = $2;
1912                 }
1913 ;
1914
1915 catch_clause:
1916         catch_clause_parameter block
1917                 {
1918                   java_method_add_stmt (current_function_decl, $2);
1919                   exit_block ();
1920                   $$ = $1;
1921                 }
1922 ;
1923
1924 catch_clause_parameter:
1925         CATCH_TK OP_TK formal_parameter CP_TK
1926                 {
1927                   /* We add a block to define a scope for
1928                      formal_parameter (CCBP). The formal parameter is
1929                      declared initialized by the appropriate function
1930                      call */
1931                   tree ccpb;
1932                   tree init;
1933                   if ($3)
1934                     {
1935                       ccpb = enter_block ();
1936                       init = build_assignment
1937                         (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1938                          build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1939                       declare_local_variables (0, TREE_VALUE ($3),
1940                                                build_tree_list 
1941                                                (TREE_PURPOSE ($3), init));
1942                       $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1943                       SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1944                     }
1945                   else
1946                     {
1947                       $$ = error_mark_node;
1948                     }
1949                 }
1950 |       CATCH_TK error
1951                 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1952 |       CATCH_TK OP_TK error
1953                 {
1954                   yyerror ("Missing term or ')' expected");
1955                   RECOVER; $$ = NULL_TREE;
1956                 }
1957 |       CATCH_TK OP_TK error CP_TK /* That's for () */
1958                 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1959 ;
1960
1961 finally:
1962         FINALLY_TK block
1963                 { $$ = $2; }
1964 |       FINALLY_TK error
1965                 {yyerror ("'{' expected"); RECOVER; }
1966 ;
1967
1968 /* 19.12 Production from 15: Expressions  */
1969 primary:
1970         primary_no_new_array
1971 |       array_creation_uninitialized
1972 |       array_creation_initialized
1973 ;
1974
1975 primary_no_new_array:
1976         literal
1977 |       THIS_TK
1978                 { $$ = build_this ($1.location); }
1979 |       OP_TK expression CP_TK
1980                 {$$ = $2;}
1981 |       class_instance_creation_expression
1982 |       field_access
1983 |       method_invocation
1984 |       array_access
1985 |       type_literals
1986         /* Added, JDK1.1 inner classes. Documentation is wrong
1987            referring to a 'ClassName' (class_name) rule that doesn't
1988            exist. Used name: instead.  */
1989 |       name DOT_TK THIS_TK
1990                 {
1991                   tree wfl = build_wfl_node (this_identifier_node);
1992                   $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1993                 }
1994 |       OP_TK expression error
1995                 {yyerror ("')' expected"); RECOVER;}
1996 |       name DOT_TK error
1997                 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1998 |       primitive_type DOT_TK error
1999                 {yyerror ("'class' expected" ); RECOVER;}
2000 |       VOID_TK DOT_TK error
2001                 {yyerror ("'class' expected" ); RECOVER;}
2002 ;
2003
2004 type_literals:
2005         name DOT_TK CLASS_TK
2006                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2007 |       array_type DOT_TK CLASS_TK
2008                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2009 |       primitive_type DOT_TK CLASS_TK
2010                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2011 |       VOID_TK DOT_TK CLASS_TK
2012                 {
2013                    $$ = build_incomplete_class_ref ($2.location,
2014                                                    void_type_node);
2015                 }
2016 ;
2017
2018 class_instance_creation_expression:
2019         NEW_TK class_type OP_TK argument_list CP_TK
2020                 { $$ = build_new_invocation ($2, $4); }
2021 |       NEW_TK class_type OP_TK CP_TK
2022                 { $$ = build_new_invocation ($2, NULL_TREE); }
2023 |       anonymous_class_creation
2024         /* Added, JDK1.1 inner classes, modified to use name or
2025            primary instead of primary solely which couldn't work in
2026            all situations.  */
2027 |       something_dot_new identifier OP_TK CP_TK
2028                 {
2029                   tree ctor = build_new_invocation ($2, NULL_TREE);
2030                   $$ = make_qualified_primary ($1, ctor,
2031                                                EXPR_WFL_LINECOL ($1));
2032                 }
2033 |       something_dot_new identifier OP_TK CP_TK class_body
2034 |       something_dot_new identifier OP_TK argument_list CP_TK
2035                 {
2036                   tree ctor = build_new_invocation ($2, $4);
2037                   $$ = make_qualified_primary ($1, ctor,
2038                                                EXPR_WFL_LINECOL ($1));
2039                 }
2040 |       something_dot_new identifier OP_TK argument_list CP_TK class_body
2041 |       NEW_TK error SC_TK
2042                 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2043 |       NEW_TK class_type error
2044                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2045 |       NEW_TK class_type OP_TK error
2046                 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2047 |       NEW_TK class_type OP_TK argument_list error
2048                 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2049 |       something_dot_new error
2050                 {
2051                   $$ = NULL_TREE;
2052                   YYERROR_NOW;
2053                   yyerror ("Identifier expected");
2054                   RECOVER;
2055                 }
2056 |       something_dot_new identifier error
2057                 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2058 ;
2059
2060 /* Created after JDK1.1 rules originally added to
2061    class_instance_creation_expression, but modified to use
2062    'class_type' instead of 'TypeName' (type_name) which is mentioned
2063    in the documentation but doesn't exist. */
2064
2065 anonymous_class_creation:
2066         NEW_TK class_type OP_TK argument_list CP_TK
2067                 { create_anonymous_class ($2); }
2068         class_body
2069                 {
2070                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2071                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2072
2073                   end_class_declaration (1);
2074
2075                   /* Now we can craft the new expression */
2076                   $$ = build_new_invocation (id, $4);
2077
2078                   /* Note that we can't possibly be here if
2079                      `class_type' is an interface (in which case the
2080                      anonymous class extends Object and implements
2081                      `class_type', hence its constructor can't have
2082                      arguments.) */
2083
2084                   /* Otherwise, the innerclass must feature a
2085                      constructor matching `argument_list'. Anonymous
2086                      classes are a bit special: it's impossible to
2087                      define constructor for them, hence constructors
2088                      must be generated following the hints provided by
2089                      the `new' expression. Whether a super constructor
2090                      of that nature exists or not is to be verified
2091                      later on in get_constructor_super.
2092
2093                      It's during the expansion of a `new' statement
2094                      referring to an anonymous class that a ctor will
2095                      be generated for the anonymous class, with the
2096                      right arguments. */
2097
2098                 }
2099 |       NEW_TK class_type OP_TK CP_TK
2100                 { create_anonymous_class ($2); }
2101         class_body
2102                 {
2103                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2104                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2105
2106                   end_class_declaration (1);
2107
2108                   /* Now we can craft the new expression. The
2109                      statement doesn't need to be remember so that a
2110                      constructor can be generated, since its signature
2111                      is already known. */
2112                   $$ = build_new_invocation (id, NULL_TREE);
2113                 }
2114 ;
2115
2116 something_dot_new:              /* Added, not part of the specs. */
2117         name DOT_TK NEW_TK
2118                 { $$ = $1; }
2119 |       primary DOT_TK NEW_TK
2120                 { $$ = $1; }
2121 ;
2122
2123 argument_list:
2124         expression
2125                 {
2126                   $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2127                   ctxp->formal_parameter_number = 1;
2128                 }
2129 |       argument_list C_TK expression
2130                 {
2131                   ctxp->formal_parameter_number += 1;
2132                   $$ = tree_cons (NULL_TREE, $3, $1);
2133                 }
2134 |       argument_list C_TK error
2135                 {yyerror ("Missing term"); RECOVER;}
2136 ;
2137
2138 array_creation_uninitialized:
2139         NEW_TK primitive_type dim_exprs
2140                 { $$ = build_newarray_node ($2, $3, 0); }
2141 |       NEW_TK class_or_interface_type dim_exprs
2142                 { $$ = build_newarray_node ($2, $3, 0); }
2143 |       NEW_TK primitive_type dim_exprs dims
2144                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2145 |       NEW_TK class_or_interface_type dim_exprs dims
2146                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2147 |       NEW_TK error CSB_TK
2148                 {yyerror ("'[' expected"); DRECOVER ("]");}
2149 |       NEW_TK error OSB_TK
2150                 {yyerror ("']' expected"); RECOVER;}
2151 ;
2152
2153 array_creation_initialized:
2154         /* Added, JDK1.1 anonymous array. Initial documentation rule
2155            modified */
2156         NEW_TK class_or_interface_type dims array_initializer
2157                 {
2158                   char *sig;
2159                   int osb = pop_current_osb (ctxp);
2160                   while (osb--)
2161                     obstack_grow (&temporary_obstack, "[]", 2);
2162                   obstack_1grow (&temporary_obstack, '\0');
2163                   sig = obstack_finish (&temporary_obstack);
2164                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2165                                $2, get_identifier (sig), $4);
2166                 }
2167 |       NEW_TK primitive_type dims array_initializer
2168                 {
2169                   int osb = pop_current_osb (ctxp);
2170                   tree type = $2;
2171                   while (osb--)
2172                     type = build_java_array_type (type, -1);
2173                   $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2174                                build_pointer_type (type), NULL_TREE, $4);
2175                 }
2176 |       NEW_TK error CSB_TK
2177                 {yyerror ("'[' expected"); DRECOVER ("]");}
2178 |       NEW_TK error OSB_TK
2179                 {yyerror ("']' expected"); RECOVER;}
2180 ;
2181
2182 dim_exprs:
2183         dim_expr
2184                 { $$ = build_tree_list (NULL_TREE, $1); }
2185 |       dim_exprs dim_expr
2186                 { $$ = tree_cons (NULL_TREE, $2, $$); }
2187 ;
2188
2189 dim_expr:
2190         OSB_TK expression CSB_TK
2191                 {
2192                   if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2193                     {
2194                       $2 = build_wfl_node ($2);
2195                       TREE_TYPE ($2) = NULL_TREE;
2196                     }
2197                   EXPR_WFL_LINECOL ($2) = $1.location;
2198                   $$ = $2;
2199                 }
2200 |       OSB_TK expression error
2201                 {yyerror ("']' expected"); RECOVER;}
2202 |       OSB_TK error
2203                 {
2204                   yyerror ("Missing term");
2205                   yyerror ("']' expected");
2206                   RECOVER;
2207                 }
2208 ;
2209
2210 dims:
2211         OSB_TK CSB_TK
2212                 {
2213                   int allocate = 0;
2214                   /* If not initialized, allocate memory for the osb
2215                      numbers stack */
2216                   if (!ctxp->osb_limit)
2217                     {
2218                       allocate = ctxp->osb_limit = 32;
2219                       ctxp->osb_depth = -1;
2220                     }
2221                   /* If capacity overflown, reallocate a bigger chunk */
2222                   else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2223                     allocate = ctxp->osb_limit << 1;
2224
2225                   if (allocate)
2226                     {
2227                       allocate *= sizeof (int);
2228                       if (ctxp->osb_number)
2229                         ctxp->osb_number = xrealloc (ctxp->osb_number,
2230                                                      allocate);
2231                       else
2232                         ctxp->osb_number = xmalloc (allocate);
2233                     }
2234                   ctxp->osb_depth++;
2235                   CURRENT_OSB (ctxp) = 1;
2236                 }
2237 |       dims OSB_TK CSB_TK
2238                 { CURRENT_OSB (ctxp)++; }
2239 |       dims OSB_TK error
2240                 { yyerror ("']' expected"); RECOVER;}
2241 ;
2242
2243 field_access:
2244         primary DOT_TK identifier
2245                 { $$ = make_qualified_primary ($1, $3, $2.location); }
2246                 /*  FIXME - REWRITE TO:
2247                 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2248 |       SUPER_TK DOT_TK identifier
2249                 {
2250                   tree super_wfl = build_wfl_node (super_identifier_node);
2251                   SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2252                   $$ = make_qualified_name (super_wfl, $3, $2.location);
2253                 }
2254 |       SUPER_TK error
2255                 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2256 ;
2257
2258 method_invocation:
2259         name OP_TK CP_TK
2260                 { $$ = build_method_invocation ($1, NULL_TREE); }
2261 |       name OP_TK argument_list CP_TK
2262                 { $$ = build_method_invocation ($1, $3); }
2263 |       primary DOT_TK identifier OP_TK CP_TK
2264                 {
2265                   if (TREE_CODE ($1) == THIS_EXPR)
2266                     $$ = build_this_super_qualified_invocation
2267                       (1, $3, NULL_TREE, 0, $2.location);
2268                   else
2269                     {
2270                       tree invok = build_method_invocation ($3, NULL_TREE);
2271                       $$ = make_qualified_primary ($1, invok, $2.location);
2272                     }
2273                 }
2274 |       primary DOT_TK identifier OP_TK argument_list CP_TK
2275                 {
2276                   if (TREE_CODE ($1) == THIS_EXPR)
2277                     $$ = build_this_super_qualified_invocation
2278                       (1, $3, $5, 0, $2.location);
2279                   else
2280                     {
2281                       tree invok = build_method_invocation ($3, $5);
2282                       $$ = make_qualified_primary ($1, invok, $2.location);
2283                     }
2284                 }
2285 |       SUPER_TK DOT_TK identifier OP_TK CP_TK
2286                 {
2287                   $$ = build_this_super_qualified_invocation
2288                     (0, $3, NULL_TREE, $1.location, $2.location);
2289                 }
2290 |       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2291                 {
2292                   $$ = build_this_super_qualified_invocation
2293                     (0, $3, $5, $1.location, $2.location);
2294                 }
2295         /* Screws up thing. I let it here until I'm convinced it can
2296            be removed. FIXME
2297 |       primary DOT_TK error
2298                 {yyerror ("'(' expected"); DRECOVER(bad);} */
2299 |       SUPER_TK DOT_TK error CP_TK
2300                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2301 |       SUPER_TK DOT_TK error DOT_TK
2302                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2303 ;
2304
2305 array_access:
2306         name OSB_TK expression CSB_TK
2307                 { $$ = build_array_ref ($2.location, $1, $3); }
2308 |       primary_no_new_array OSB_TK expression CSB_TK
2309                 { $$ = build_array_ref ($2.location, $1, $3); }
2310 |       array_creation_initialized OSB_TK expression CSB_TK
2311                 { $$ = build_array_ref ($2.location, $1, $3); }
2312 |       name OSB_TK error
2313                 {
2314                   yyerror ("Missing term and ']' expected");
2315                   DRECOVER(array_access);
2316                 }
2317 |       name OSB_TK expression error
2318                 {
2319                   yyerror ("']' expected");
2320                   DRECOVER(array_access);
2321                 }
2322 |       primary_no_new_array OSB_TK error
2323                 {
2324                   yyerror ("Missing term and ']' expected");
2325                   DRECOVER(array_access);
2326                 }
2327 |       primary_no_new_array OSB_TK expression error
2328                 {
2329                   yyerror ("']' expected");
2330                   DRECOVER(array_access);
2331                 }
2332 |       array_creation_initialized OSB_TK error
2333                 {
2334                   yyerror ("Missing term and ']' expected");
2335                   DRECOVER(array_access);
2336                 }
2337 |       array_creation_initialized OSB_TK expression error
2338                 {
2339                   yyerror ("']' expected");
2340                   DRECOVER(array_access);
2341                 }
2342 ;
2343
2344 postfix_expression:
2345         primary
2346 |       name
2347 |       post_increment_expression
2348 |       post_decrement_expression
2349 ;
2350
2351 post_increment_expression:
2352         postfix_expression INCR_TK
2353                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2354 ;
2355
2356 post_decrement_expression:
2357         postfix_expression DECR_TK
2358                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2359 ;
2360
2361 trap_overflow_corner_case:
2362         pre_increment_expression
2363 |       pre_decrement_expression
2364 |       PLUS_TK unary_expression
2365                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2366 |       unary_expression_not_plus_minus
2367 |       PLUS_TK error
2368                 {yyerror ("Missing term"); RECOVER}
2369 ;
2370
2371 unary_expression:
2372         trap_overflow_corner_case
2373                 {
2374                   if ($1)
2375                     error_if_numeric_overflow ($1);
2376                   $$ = $1;
2377                 }
2378 |       MINUS_TK trap_overflow_corner_case
2379                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2380 |       MINUS_TK error
2381                 {yyerror ("Missing term"); RECOVER}
2382 ;
2383
2384 pre_increment_expression:
2385         INCR_TK unary_expression
2386                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2387 |       INCR_TK error
2388                 {yyerror ("Missing term"); RECOVER}
2389 ;
2390
2391 pre_decrement_expression:
2392         DECR_TK unary_expression
2393                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2394 |       DECR_TK error
2395                 {yyerror ("Missing term"); RECOVER}
2396 ;
2397
2398 unary_expression_not_plus_minus:
2399         postfix_expression
2400 |       NOT_TK unary_expression
2401                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2402 |       NEG_TK unary_expression
2403                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2404 |       cast_expression
2405 |       NOT_TK error
2406                 {yyerror ("Missing term"); RECOVER}
2407 |       NEG_TK error
2408                 {yyerror ("Missing term"); RECOVER}
2409 ;
2410
2411 cast_expression:                /* Error handling here is potentially weak */
2412         OP_TK primitive_type dims CP_TK unary_expression
2413                 {
2414                   tree type = $2;
2415                   int osb = pop_current_osb (ctxp);
2416                   while (osb--)
2417                     type = build_java_array_type (type, -1);
2418                   $$ = build_cast ($1.location, type, $5);
2419                 }
2420 |       OP_TK primitive_type CP_TK unary_expression
2421                 { $$ = build_cast ($1.location, $2, $4); }
2422 |       OP_TK expression CP_TK unary_expression_not_plus_minus
2423                 { $$ = build_cast ($1.location, $2, $4); }
2424 |       OP_TK name dims CP_TK unary_expression_not_plus_minus
2425                 {
2426                   const char *ptr;
2427                   int osb = pop_current_osb (ctxp);
2428                   obstack_grow (&temporary_obstack,
2429                                 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2430                                 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2431                   while (osb--)
2432                     obstack_grow (&temporary_obstack, "[]", 2);
2433                   obstack_1grow (&temporary_obstack, '\0');
2434                   ptr = obstack_finish (&temporary_obstack);
2435                   EXPR_WFL_NODE ($2) = get_identifier (ptr);
2436                   $$ = build_cast ($1.location, $2, $5);
2437                 }
2438 |       OP_TK primitive_type OSB_TK error
2439                 {yyerror ("']' expected, invalid type expression");}
2440 |       OP_TK error
2441                 {
2442                   YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2443                   RECOVER;
2444                 }
2445 |       OP_TK primitive_type dims CP_TK error
2446                 {yyerror ("Missing term"); RECOVER;}
2447 |       OP_TK primitive_type CP_TK error
2448                 {yyerror ("Missing term"); RECOVER;}
2449 |       OP_TK name dims CP_TK error
2450                 {yyerror ("Missing term"); RECOVER;}
2451 ;
2452
2453 multiplicative_expression:
2454         unary_expression
2455 |       multiplicative_expression MULT_TK unary_expression
2456                 {
2457                   $$ = build_binop (BINOP_LOOKUP ($2.token),
2458                                     $2.location, $1, $3);
2459                 }
2460 |       multiplicative_expression DIV_TK unary_expression
2461                 {
2462                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2463                                     $1, $3);
2464                 }
2465 |       multiplicative_expression REM_TK unary_expression
2466                 {
2467                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2468                                     $1, $3);
2469                 }
2470 |       multiplicative_expression MULT_TK error
2471                 {yyerror ("Missing term"); RECOVER;}
2472 |       multiplicative_expression DIV_TK error
2473                 {yyerror ("Missing term"); RECOVER;}
2474 |       multiplicative_expression REM_TK error
2475                 {yyerror ("Missing term"); RECOVER;}
2476 ;
2477
2478 additive_expression:
2479         multiplicative_expression
2480 |       additive_expression PLUS_TK multiplicative_expression
2481                 {
2482                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2483                                     $1, $3);
2484                 }
2485 |       additive_expression MINUS_TK multiplicative_expression
2486                 {
2487                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2488                                     $1, $3);
2489                 }
2490 |       additive_expression PLUS_TK error
2491                 {yyerror ("Missing term"); RECOVER;}
2492 |       additive_expression MINUS_TK error
2493                 {yyerror ("Missing term"); RECOVER;}
2494 ;
2495
2496 shift_expression:
2497         additive_expression
2498 |       shift_expression LS_TK additive_expression
2499                 {
2500                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2501                                     $1, $3);
2502                 }
2503 |       shift_expression SRS_TK additive_expression
2504                 {
2505                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2506                                     $1, $3);
2507                 }
2508 |       shift_expression ZRS_TK additive_expression
2509                 {
2510                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2511                                     $1, $3);
2512                 }
2513 |       shift_expression LS_TK error
2514                 {yyerror ("Missing term"); RECOVER;}
2515 |       shift_expression SRS_TK error
2516                 {yyerror ("Missing term"); RECOVER;}
2517 |       shift_expression ZRS_TK error
2518                 {yyerror ("Missing term"); RECOVER;}
2519 ;
2520
2521 relational_expression:
2522         shift_expression
2523 |       relational_expression LT_TK shift_expression
2524                 {
2525                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2526                                     $1, $3);
2527                 }
2528 |       relational_expression GT_TK shift_expression
2529                 {
2530                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2531                                     $1, $3);
2532                 }
2533 |       relational_expression LTE_TK shift_expression
2534                 {
2535                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2536                                     $1, $3);
2537                 }
2538 |       relational_expression GTE_TK shift_expression
2539                 {
2540                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2541                                     $1, $3);
2542                 }
2543 |       relational_expression INSTANCEOF_TK reference_type
2544                 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2545 |       relational_expression LT_TK error
2546                 {yyerror ("Missing term"); RECOVER;}
2547 |       relational_expression GT_TK error
2548                 {yyerror ("Missing term"); RECOVER;}
2549 |       relational_expression LTE_TK error
2550                 {yyerror ("Missing term"); RECOVER;}
2551 |       relational_expression GTE_TK error
2552                 {yyerror ("Missing term"); RECOVER;}
2553 |       relational_expression INSTANCEOF_TK error
2554                 {yyerror ("Invalid reference type"); RECOVER;}
2555 ;
2556
2557 equality_expression:
2558         relational_expression
2559 |       equality_expression EQ_TK relational_expression
2560                 {
2561                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2562                                     $1, $3);
2563                 }
2564 |       equality_expression NEQ_TK relational_expression
2565                 {
2566                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2567                                     $1, $3);
2568                 }
2569 |       equality_expression EQ_TK error
2570                 {yyerror ("Missing term"); RECOVER;}
2571 |       equality_expression NEQ_TK error
2572                 {yyerror ("Missing term"); RECOVER;}
2573 ;
2574
2575 and_expression:
2576         equality_expression
2577 |       and_expression AND_TK equality_expression
2578                 {
2579                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2580                                     $1, $3);
2581                 }
2582 |       and_expression AND_TK error
2583                 {yyerror ("Missing term"); RECOVER;}
2584 ;
2585
2586 exclusive_or_expression:
2587         and_expression
2588 |       exclusive_or_expression XOR_TK and_expression
2589                 {
2590                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2591                                     $1, $3);
2592                 }
2593 |       exclusive_or_expression XOR_TK error
2594                 {yyerror ("Missing term"); RECOVER;}
2595 ;
2596
2597 inclusive_or_expression:
2598         exclusive_or_expression
2599 |       inclusive_or_expression OR_TK exclusive_or_expression
2600                 {
2601                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2602                                     $1, $3);
2603                 }
2604 |       inclusive_or_expression OR_TK error
2605                 {yyerror ("Missing term"); RECOVER;}
2606 ;
2607
2608 conditional_and_expression:
2609         inclusive_or_expression
2610 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2611                 {
2612                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2613                                     $1, $3);
2614                 }
2615 |       conditional_and_expression BOOL_AND_TK error
2616                 {yyerror ("Missing term"); RECOVER;}
2617 ;
2618
2619 conditional_or_expression:
2620         conditional_and_expression
2621 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
2622                 {
2623                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2624                                     $1, $3);
2625                 }
2626 |       conditional_or_expression BOOL_OR_TK error
2627                 {yyerror ("Missing term"); RECOVER;}
2628 ;
2629
2630 conditional_expression:         /* Error handling here is weak */
2631         conditional_or_expression
2632 |       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2633                 {
2634                   $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2635                   SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2636                 }
2637 |       conditional_or_expression REL_QM_TK REL_CL_TK error
2638                 {
2639                   YYERROR_NOW;
2640                   yyerror ("Missing term");
2641                   DRECOVER (1);
2642                 }
2643 |       conditional_or_expression REL_QM_TK error
2644                 {yyerror ("Missing term"); DRECOVER (2);}
2645 |       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2646                 {yyerror ("Missing term"); DRECOVER (3);}
2647 ;
2648
2649 assignment_expression:
2650         conditional_expression
2651 |       assignment
2652 ;
2653
2654 assignment:
2655         left_hand_side assignment_operator assignment_expression
2656                 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2657 |       left_hand_side assignment_operator error
2658                 {
2659                   YYNOT_TWICE yyerror ("Missing term");
2660                   DRECOVER (assign);
2661                 }
2662 ;
2663
2664 left_hand_side:
2665         name
2666 |       field_access
2667 |       array_access
2668 ;
2669
2670 assignment_operator:
2671         ASSIGN_ANY_TK
2672 |       ASSIGN_TK
2673 ;
2674
2675 expression:
2676         assignment_expression
2677 ;
2678
2679 constant_expression:
2680         expression
2681 ;
2682
2683 %%
2684
2685 /* Helper function to retrieve an OSB count. Should be used when the
2686    `dims:' rule is being used.  */
2687
2688 static int
2689 pop_current_osb (struct parser_ctxt *ctxp)
2690 {
2691   int to_return;
2692
2693   if (ctxp->osb_depth < 0)
2694     abort ();
2695
2696   to_return = CURRENT_OSB (ctxp);
2697   ctxp->osb_depth--;
2698
2699   return to_return;
2700 }
2701
2702 \f
2703
2704 /* This section of the code deal with save/restoring parser contexts.
2705    Add mode documentation here. FIXME */
2706
2707 /* Helper function. Create a new parser context. With
2708    COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2709    context is copied, otherwise, the new context is zeroed. The newly
2710    created context becomes the current one.  */
2711
2712 static void
2713 create_new_parser_context (int copy_from_previous)
2714 {
2715   struct parser_ctxt *new;
2716
2717   new = ggc_alloc (sizeof (struct parser_ctxt));
2718   if (copy_from_previous)
2719     {
2720       memcpy (new, ctxp, sizeof (struct parser_ctxt));
2721       /* This flag, indicating the context saves global values,
2722          should only be set by java_parser_context_save_global.  */
2723       new->saved_data_ctx = 0;
2724     }
2725   else
2726     memset (new, 0, sizeof (struct parser_ctxt));
2727
2728   new->next = ctxp;
2729   ctxp = new;
2730 }
2731
2732 /* Create a new parser context and make it the current one. */
2733
2734 void
2735 java_push_parser_context (void)
2736 {
2737   create_new_parser_context (0);
2738 }
2739
2740 void
2741 java_pop_parser_context (int generate)
2742 {
2743   tree current;
2744   struct parser_ctxt *next;
2745
2746   if (!ctxp)
2747     return;
2748
2749   next = ctxp->next;
2750   if (next)
2751     {
2752       input_location = ctxp->save_location;
2753       current_class = ctxp->class_type;
2754     }
2755
2756   /* If the old and new lexers differ, then free the old one.  */
2757   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2758     java_destroy_lexer (ctxp->lexer);
2759
2760   /* Set the single import class file flag to 0 for the current list
2761      of imported things */
2762   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2763     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2764
2765   /* If we pushed a context to parse a class intended to be generated,
2766      we keep it so we can remember the class. What we could actually
2767      do is to just update a list of class names.  */
2768   if (generate)
2769     {
2770       if (ctxp_for_generation_last == NULL)
2771         ctxp_for_generation = ctxp;
2772       else
2773         ctxp_for_generation_last->next = ctxp;
2774       ctxp->next = NULL;
2775       ctxp_for_generation_last = ctxp;
2776     }
2777
2778   /* And restore those of the previous context */
2779   if ((ctxp = next))            /* Assignment is really meant here */
2780     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2781       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2782 }
2783
2784 /* Create a parser context for the use of saving some global
2785    variables.  */
2786
2787 void
2788 java_parser_context_save_global (void)
2789 {
2790   if (!ctxp)
2791     {
2792       java_push_parser_context ();
2793       ctxp->saved_data_ctx = 1;
2794     }
2795
2796   /* If this context already stores data, create a new one suitable
2797      for data storage. */
2798   else if (ctxp->saved_data)
2799     {
2800       create_new_parser_context (1);
2801       ctxp->saved_data_ctx = 1;
2802     }
2803
2804   ctxp->save_location = input_location;
2805   ctxp->class_type = current_class;
2806   ctxp->function_decl = current_function_decl;
2807   ctxp->saved_data = 1;
2808 }
2809
2810 /* Restore some global variables from the previous context. Make the
2811    previous context the current one.  */
2812
2813 void
2814 java_parser_context_restore_global (void)
2815 {
2816   input_location = ctxp->save_location;
2817   current_class = ctxp->class_type;
2818   if (wfl_operator)
2819 #ifdef USE_MAPPED_LOCATION
2820     SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2821 #else
2822     EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2823 #endif
2824   current_function_decl = ctxp->function_decl;
2825   ctxp->saved_data = 0;
2826   if (ctxp->saved_data_ctx)
2827     java_pop_parser_context (0);
2828 }
2829
2830 /* Suspend vital data for the current class/function being parsed so
2831    that an other class can be parsed. Used to let local/anonymous
2832    classes be parsed.  */
2833
2834 static void
2835 java_parser_context_suspend (void)
2836 {
2837   /* This makes debugging through java_debug_context easier */
2838   static const char *const name = "<inner buffer context>";
2839
2840   /* Duplicate the previous context, use it to save the globals we're
2841      interested in */
2842   create_new_parser_context (1);
2843   ctxp->function_decl = current_function_decl;
2844   ctxp->class_type = current_class;
2845
2846   /* Then create a new context which inherits all data from the
2847      previous one. This will be the new current context  */
2848   create_new_parser_context (1);
2849
2850   /* Help debugging */
2851   ctxp->next->filename = name;
2852 }
2853
2854 /* Resume vital data for the current class/function being parsed so
2855    that an other class can be parsed. Used to let local/anonymous
2856    classes be parsed.  The trick is the data storing file position
2857    informations must be restored to their current value, so parsing
2858    can resume as if no context was ever saved. */
2859
2860 static void
2861 java_parser_context_resume (void)
2862 {
2863   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2864   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2865   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2866
2867   /* We need to inherit the list of classes to complete/generate */
2868   restored->classd_list = old->classd_list;
2869   restored->class_list = old->class_list;
2870
2871   /* Restore the current class and function from the saver */
2872   current_class = saver->class_type;
2873   current_function_decl = saver->function_decl;
2874
2875   /* Retrieve the restored context */
2876   ctxp = restored;
2877
2878   /* Re-installed the data for the parsing to carry on */
2879   memcpy (&ctxp->marker_begining, &old->marker_begining,
2880           (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2881 }
2882
2883 /* Add a new anchor node to which all statement(s) initializing static
2884    and non static initialized upon declaration field(s) will be
2885    linked.  */
2886
2887 static void
2888 java_parser_context_push_initialized_field (void)
2889 {
2890   tree node;
2891
2892   node = build_tree_list (NULL_TREE, NULL_TREE);
2893   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2894   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2895
2896   node = build_tree_list (NULL_TREE, NULL_TREE);
2897   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2898   CPC_INITIALIZER_LIST (ctxp) = node;
2899
2900   node = build_tree_list (NULL_TREE, NULL_TREE);
2901   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2902   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2903 }
2904
2905 /* Pop the lists of initialized field. If this lists aren't empty,
2906    remember them so we can use it to create and populate the finit$
2907    or <clinit> functions. */
2908
2909 static void
2910 java_parser_context_pop_initialized_field (void)
2911 {
2912   tree stmts;
2913   tree class_type = TREE_TYPE (GET_CPC ());
2914
2915   if (CPC_INITIALIZER_LIST (ctxp))
2916     {
2917       stmts = CPC_INITIALIZER_STMT (ctxp);
2918       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2919       if (stmts && !java_error_count)
2920         TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2921     }
2922
2923   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2924     {
2925       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2926       CPC_STATIC_INITIALIZER_LIST (ctxp) =
2927         TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2928       /* Keep initialization in order to enforce 8.5 */
2929       if (stmts && !java_error_count)
2930         TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2931     }
2932
2933   /* JDK 1.1 instance initializers */
2934   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2935     {
2936       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2937       CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2938         TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2939       if (stmts && !java_error_count)
2940         TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2941     }
2942 }
2943
2944 static tree
2945 reorder_static_initialized (tree list)
2946 {
2947   /* We have to keep things in order. The alias initializer have to
2948      come first, then the initialized regular field, in reverse to
2949      keep them in lexical order. */
2950   tree marker, previous = NULL_TREE;
2951   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2952     if (TREE_CODE (marker) == TREE_LIST
2953         && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2954       break;
2955
2956   /* No static initialized, the list is fine as is */
2957   if (!previous)
2958     list = TREE_CHAIN (marker);
2959
2960   /* No marker? reverse the whole list */
2961   else if (!marker)
2962     list = nreverse (list);
2963
2964   /* Otherwise, reverse what's after the marker and the new reordered
2965      sublist will replace the marker. */
2966   else
2967     {
2968       TREE_CHAIN (previous) = NULL_TREE;
2969       list = nreverse (list);
2970       list = chainon (TREE_CHAIN (marker), list);
2971     }
2972   return list;
2973 }
2974
2975 /* Helper functions to dump the parser context stack.  */
2976
2977 #define TAB_CONTEXT(C) \
2978   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2979
2980 static void
2981 java_debug_context_do (int tab)
2982 {
2983   struct parser_ctxt *copy = ctxp;
2984   while (copy)
2985     {
2986       TAB_CONTEXT (tab);
2987       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2988       TAB_CONTEXT (tab);
2989       fprintf (stderr, "filename: %s\n", copy->filename);
2990       TAB_CONTEXT (tab);
2991       fprintf (stderr, "package: %s\n",
2992                (copy->package ?
2993                 IDENTIFIER_POINTER (copy->package) : "<none>"));
2994       TAB_CONTEXT (tab);
2995       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2996       TAB_CONTEXT (tab);
2997       fprintf (stderr, "saved data: %d\n", copy->saved_data);
2998       copy = copy->next;
2999       tab += 2;
3000     }
3001 }
3002
3003 /* Dump the stacked up parser contexts. Intended to be called from a
3004    debugger.  */
3005
3006 void
3007 java_debug_context (void)
3008 {
3009   java_debug_context_do (0);
3010 }
3011
3012 \f
3013
3014 /* Flag for the error report routine to issue the error the first time
3015    it's called (overriding the default behavior which is to drop the
3016    first invocation and honor the second one, taking advantage of a
3017    richer context.  */
3018 static int force_error = 0;
3019
3020 /* Reporting an constructor invocation error.  */
3021 static void
3022 parse_ctor_invocation_error (void)
3023 {
3024   if (DECL_CONSTRUCTOR_P (current_function_decl))
3025     yyerror ("Constructor invocation must be first thing in a constructor");
3026   else
3027     yyerror ("Only constructors can invoke constructors");
3028 }
3029
3030 /* Reporting JDK1.1 features not implemented.  */
3031
3032 static tree
3033 parse_jdk1_1_error (const char *msg)
3034 {
3035   sorry (": %qs JDK1.1(TM) feature", msg);
3036   java_error_count++;
3037   return build_java_empty_stmt ();
3038 }
3039
3040 static int do_warning = 0;
3041
3042 void
3043 yyerror (const char *msgid)
3044 {
3045 #ifdef USE_MAPPED_LOCATION
3046   static source_location elc;
3047   expanded_location xloc = expand_location (input_location);
3048   int current_line = xloc.line;
3049 #else
3050   static java_lc elc;
3051   int save_lineno;
3052   int current_line = input_line;
3053 #endif
3054   static int prev_lineno;
3055   static const char *prev_msg;
3056
3057   char *remainder, *code_from_source;
3058
3059   if (!force_error && prev_lineno == current_line)
3060     return;
3061 #ifndef USE_MAPPED_LOCATION
3062   current_line = ctxp->lexer->token_start.line;
3063 #endif
3064
3065   /* Save current error location but report latter, when the context is
3066      richer.  */
3067   if (ctxp->java_error_flag == 0)
3068     {
3069       ctxp->java_error_flag = 1;
3070 #ifdef USE_MAPPED_LOCATION
3071       elc = input_location;
3072 #else
3073       elc = ctxp->lexer->token_start;
3074 #endif
3075       /* Do something to use the previous line if we're reaching the
3076          end of the file... */
3077 #ifdef VERBOSE_SKELETON
3078       printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3079 #endif
3080       return;
3081     }
3082
3083   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3084   if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3085     return;
3086
3087   ctxp->java_error_flag = 0;
3088   if (do_warning)
3089     java_warning_count++;
3090   else
3091     java_error_count++;
3092
3093 #if 0 /* FIXME */
3094   if (elc.col == 0 && msgid && msgid[1] == ';')
3095     elc = ctxp->prev_line_end;
3096 #endif
3097
3098   prev_msg = msgid;
3099
3100 #ifdef USE_MAPPED_LOCATION
3101   prev_lineno = current_line;
3102   code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3103 #else
3104   save_lineno = input_line;
3105   prev_lineno = input_line = current_line;
3106   code_from_source = java_get_line_col (input_filename, current_line,
3107                                         ctxp->lexer->token_start.col);
3108 #endif
3109
3110
3111   obstack_grow0 (&temporary_obstack,
3112                  code_from_source, strlen (code_from_source));
3113   remainder = obstack_finish (&temporary_obstack);
3114   if (do_warning)
3115     warning (0, "%s.\n%s", msgid, remainder);
3116   else
3117     error ("%s.\n%s", msgid, remainder);
3118
3119   /* This allow us to cheaply avoid an extra 'Invalid expression
3120      statement' error report when errors have been already reported on
3121      the same line. This occurs when we report an error but don't have
3122      a synchronization point other than ';', which
3123      expression_statement is the only one to take care of.  */
3124 #ifndef USE_MAPPED_LOCATION
3125   input_line = save_lineno;
3126 #endif
3127   ctxp->prevent_ese = input_line;
3128 }
3129
3130 static void
3131 issue_warning_error_from_context (
3132 #ifdef USE_MAPPED_LOCATION
3133                                   source_location cl,
3134 #else
3135                                   tree cl,
3136 #endif
3137                                   const char *gmsgid, va_list *ap)
3138 {
3139 #ifdef USE_MAPPED_LOCATION
3140   source_location saved_location = input_location;
3141   expanded_location xloc = expand_location (cl);
3142 #else
3143   java_lc save_lc = ctxp->lexer->token_start;
3144   const char *saved = ctxp->filename, *saved_input_filename;
3145 #endif
3146   char buffer [4096];
3147   text_info text;
3148
3149   text.err_no = errno;
3150   text.args_ptr = ap;
3151   text.format_spec = gmsgid;
3152   pp_format (global_dc->printer, &text);
3153   pp_output_formatted_text (global_dc->printer);
3154   strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3155   buffer[sizeof (buffer) - 1] = '\0';
3156   pp_clear_output_area (global_dc->printer);
3157
3158   force_error = 1;
3159
3160 #ifdef USE_MAPPED_LOCATION
3161   if (xloc.file != NULL)
3162     {
3163       ctxp->filename = xloc.file;
3164       input_location = cl;
3165     }
3166 #else
3167   ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3168   ctxp->lexer->token_start.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3169                                    : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3170                                    : EXPR_WFL_COLNO (cl));
3171
3172   /* We have a CL, that's a good reason for using it if it contains data */
3173   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3174     ctxp->filename = EXPR_WFL_FILENAME (cl);
3175   saved_input_filename = input_filename;
3176   input_filename = ctxp->filename;
3177 #endif
3178   java_error (NULL);
3179   java_error (buffer);
3180 #ifdef USE_MAPPED_LOCATION
3181   input_location = saved_location;
3182 #else
3183   ctxp->filename = saved;
3184   input_filename = saved_input_filename;
3185   ctxp->lexer->token_start = save_lc;
3186 #endif
3187   force_error = 0;
3188 }
3189
3190 /* Issue an error message at a current source line CL.
3191    FUTURE/FIXME:  change cl to be a source_location. */
3192
3193 void
3194 parse_error_context (tree cl, const char *gmsgid, ...)
3195 {
3196   va_list ap;
3197   va_start (ap, gmsgid);
3198 #ifdef USE_MAPPED_LOCATION
3199   issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3200 #else
3201   issue_warning_error_from_context (cl, gmsgid, &ap);
3202 #endif
3203   va_end (ap);
3204 }
3205
3206 /* Issue a warning at a current source line CL.
3207    FUTURE/FIXME:  change cl to be a source_location. */
3208
3209 static void
3210 parse_warning_context (tree cl, const char *gmsgid, ...)
3211 {
3212   va_list ap;
3213   va_start (ap, gmsgid);
3214
3215   do_warning = 1;
3216 #ifdef USE_MAPPED_LOCATION
3217   issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3218 #else
3219   issue_warning_error_from_context (cl, gmsgid, &ap);
3220 #endif
3221   do_warning = 0;
3222   va_end (ap);
3223 }
3224
3225 static tree
3226 find_expr_with_wfl (tree node)
3227 {
3228   while (node)
3229     {
3230       enum tree_code_class code;
3231       tree to_return;
3232
3233       switch (TREE_CODE (node))
3234         {
3235         case BLOCK:
3236           node = BLOCK_EXPR_BODY (node);
3237           continue;
3238
3239         case COMPOUND_EXPR:
3240           to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3241           if (to_return)
3242             return to_return;
3243           node = TREE_OPERAND (node, 1);
3244           continue;
3245
3246         case LOOP_EXPR:
3247           node = TREE_OPERAND (node, 0);
3248           continue;
3249
3250         case LABELED_BLOCK_EXPR:
3251           node = LABELED_BLOCK_BODY (node);
3252           continue;
3253
3254         default:
3255           code = TREE_CODE_CLASS (TREE_CODE (node));
3256           if (((code == tcc_unary) || (code == tcc_binary)
3257                || (code == tcc_expression))
3258               && EXPR_WFL_LINECOL (node))
3259             return node;
3260           return NULL_TREE;
3261         }
3262     }
3263   return NULL_TREE;
3264 }
3265
3266 /* Issue a missing return statement error. Uses METHOD to figure the
3267    last line of the method the error occurs in.  */
3268
3269 static void
3270 missing_return_error (tree method)
3271 {
3272 #ifdef USE_MAPPED_LOCATION
3273   SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3274 #else
3275   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3276 #endif
3277   parse_error_context (wfl_operator, "Missing return statement");
3278 }
3279
3280 /* Issue an unreachable statement error. From NODE, find the next
3281    statement to report appropriately.  */
3282 static void
3283 unreachable_stmt_error (tree node)
3284 {
3285   /* Browse node to find the next expression node that has a WFL. Use
3286      the location to report the error */
3287   if (TREE_CODE (node) == COMPOUND_EXPR)
3288     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3289   else
3290     node = find_expr_with_wfl (node);
3291
3292   if (node)
3293     {
3294 #ifdef USE_MAPPED_LOCATION
3295       SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3296 #else
3297       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3298 #endif
3299       parse_error_context (wfl_operator, "Unreachable statement");
3300     }
3301   else
3302     abort ();
3303 }
3304
3305 static int
3306 not_accessible_field_error (tree wfl, tree decl)
3307 {
3308   parse_error_context 
3309     (wfl, "Can't access %s field %<%s.%s%> from %qs",
3310      accessibility_string (get_access_flags_from_decl (decl)),
3311      GET_TYPE_NAME (DECL_CONTEXT (decl)),
3312      IDENTIFIER_POINTER (DECL_NAME (decl)),
3313      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3314   return 1;
3315 }
3316
3317 int
3318 java_report_errors (void)
3319 {
3320   if (java_error_count)
3321     fprintf (stderr, "%d error%s",
3322              java_error_count, (java_error_count == 1 ? "" : "s"));
3323   if (java_warning_count)
3324     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3325              java_warning_count, (java_warning_count == 1 ? "" : "s"));
3326   if (java_error_count || java_warning_count)
3327     putc ('\n', stderr);
3328   return java_error_count;
3329 }
3330
3331 static char *
3332 java_accstring_lookup (int flags)
3333 {
3334   static char buffer [80];
3335 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3336
3337   /* Access modifier looked-up first for easier report on forbidden
3338      access. */
3339   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3340   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3341   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3342   if (flags & ACC_STATIC) COPY_RETURN ("static");
3343   if (flags & ACC_FINAL) COPY_RETURN ("final");
3344   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3345   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3346   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3347   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3348   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3349   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3350
3351   buffer [0] = '\0';
3352   return buffer;
3353 #undef COPY_RETURN
3354 }
3355
3356 /* Returns a string denoting the accessibility of a class or a member as
3357    indicated by FLAGS.  We need a separate function from
3358    java_accstring_lookup, as the latter can return spurious "static", etc.
3359    if package-private access is defined (in which case none of the
3360    relevant access control bits in FLAGS is set).  */
3361
3362 static const char *
3363 accessibility_string (int flags)
3364 {
3365   if (flags & ACC_PRIVATE) return "private";
3366   if (flags & ACC_PROTECTED) return "protected";
3367   if (flags & ACC_PUBLIC) return "public";
3368
3369   return "package-private";
3370 }
3371
3372 /* Issuing error messages upon redefinition of classes, interfaces or
3373    variables. */
3374
3375 static void
3376 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3377 {
3378   parse_error_context (cl, "%s %qs already defined in %s:%d",
3379                        context, IDENTIFIER_POINTER (id),
3380                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3381   /* Here we should point out where its redefined. It's a unicode. FIXME */
3382 }
3383
3384 static void
3385 variable_redefinition_error (tree context, tree name, tree type, int line)
3386 {
3387   const char *type_name;
3388
3389   /* Figure a proper name for type. We might haven't resolved it */
3390   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3391     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3392   else
3393     type_name = lang_printable_name (type, 0);
3394
3395   parse_error_context (context,
3396                        "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3397                        IDENTIFIER_POINTER (name),
3398                        type_name, IDENTIFIER_POINTER (name), line);
3399 }
3400
3401 /* If ANAME is terminated with `[]', it indicates an array. This
3402    function returns the number of `[]' found and if this number is
3403    greater than zero, it extracts the array type name and places it in
3404    the node pointed to by TRIMMED unless TRIMMED is null.  */
3405
3406 static int
3407 build_type_name_from_array_name (tree aname, tree *trimmed)
3408 {
3409   const char *name = IDENTIFIER_POINTER (aname);
3410   int len = IDENTIFIER_LENGTH (aname);
3411   int array_dims;
3412
3413   STRING_STRIP_BRACKETS (name, len, array_dims);
3414
3415   if (array_dims && trimmed)
3416     *trimmed = get_identifier_with_length (name, len);
3417
3418   return array_dims;
3419 }
3420
3421 static tree
3422 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3423 {
3424   int more_dims = 0;
3425
3426   /* Eventually get more dims */
3427   more_dims = build_type_name_from_array_name (name, &name);
3428
3429   /* If we have, then craft a new type for this variable */
3430   if (more_dims)
3431     {
3432       tree save = type;
3433
3434       /* If we have a pointer, use its type */
3435       if (TREE_CODE (type) == POINTER_TYPE)
3436         type = TREE_TYPE (type);
3437
3438       /* Building the first dimension of a primitive type uses this
3439          function */
3440       if (JPRIMITIVE_TYPE_P (type))
3441         {
3442           type = build_java_array_type (type, -1);
3443           more_dims--;
3444         }
3445       /* Otherwise, if we have a WFL for this type, use it (the type
3446          is already an array on an unresolved type, and we just keep
3447          on adding dimensions) */
3448       else if (type_wfl)
3449         {
3450           type = type_wfl;
3451           more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3452                                                         NULL);
3453         }
3454
3455       /* Add all the dimensions */
3456       while (more_dims--)
3457         type = build_unresolved_array_type (type);
3458
3459       /* The type may have been incomplete in the first place */
3460       if (type_wfl)
3461         type = obtain_incomplete_type (type);
3462     }
3463
3464   if (ret_name)
3465     *ret_name = name;
3466   return type;
3467 }
3468
3469 /* Build something that the type identifier resolver will identify as
3470    being an array to an unresolved type. TYPE_WFL is a WFL on a
3471    identifier. */
3472
3473 static tree
3474 build_unresolved_array_type (tree type_or_wfl)
3475 {
3476   const char *ptr;
3477   tree wfl;
3478
3479   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3480      just create a array type */
3481   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3482     return build_java_array_type (type_or_wfl, -1);
3483
3484   obstack_grow (&temporary_obstack,
3485                  IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3486                  IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3487   obstack_grow0 (&temporary_obstack, "[]", 2);
3488   ptr = obstack_finish (&temporary_obstack);
3489 #ifdef USE_MAPPED_LOCATION
3490   wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3491 #else
3492   wfl = build_expr_wfl (get_identifier (ptr),
3493                         EXPR_WFL_FILENAME (type_or_wfl),
3494                         EXPR_WFL_LINENO (type_or_wfl),
3495                         EXPR_WFL_COLNO (type_or_wfl));
3496 #endif
3497   /* Re-install the existing qualifications so that the type can be
3498      resolved properly. */
3499   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3500   return wfl;
3501 }
3502
3503 static void
3504 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3505 {
3506   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3507     parse_error_context (wfl, "Interface %qs repeated",
3508                          IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3509 }
3510
3511 /* Bulk of common class/interface checks. Return 1 if an error was
3512    encountered. TAG is 0 for a class, 1 for an interface.  */
3513
3514 static int
3515 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3516                                 tree qualified_name, tree decl, tree cl)
3517 {
3518   tree node;
3519   int sca = 0;                  /* Static class allowed */
3520   int icaf = 0;                 /* Inner class allowed flags */
3521   int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3522
3523   if (!quiet_flag)
3524     fprintf (stderr, " %s%s %s",
3525              (CPC_INNER_P () ? "inner" : ""),
3526              (is_interface ? "interface" : "class"),
3527              IDENTIFIER_POINTER (qualified_name));
3528
3529   /* Scope of an interface/class type name:
3530        - Can't be imported by a single type import
3531        - Can't already exists in the package */
3532   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3533       && (node = find_name_in_single_imports (raw_name))
3534       && !CPC_INNER_P ())
3535     {
3536       parse_error_context
3537         (cl, "%s name %qs clashes with imported type %qs",
3538          (is_interface ? "Interface" : "Class"),
3539          IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3540       return 1;
3541     }
3542   if (decl && CLASS_COMPLETE_P (decl))
3543     {
3544       classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3545                                    qualified_name, decl, cl);
3546       return 1;
3547     }
3548
3549   if (check_inner_class_redefinition (raw_name, cl))
3550     return 1;
3551
3552   /* If public, file name should match class/interface name, except
3553      when dealing with an inner class */
3554   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3555     {
3556       const char *fname = input_filename;
3557       const char *f;
3558
3559       for (f = fname + strlen (fname);
3560            f != fname && ! IS_DIR_SEPARATOR (*f);
3561            f--)
3562         ;
3563       if (IS_DIR_SEPARATOR (*f))
3564         f++;
3565       if (strncmp (IDENTIFIER_POINTER (raw_name),
3566                    f , IDENTIFIER_LENGTH (raw_name)) ||
3567           f [IDENTIFIER_LENGTH (raw_name)] != '.')
3568         parse_error_context
3569           (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3570                              (is_interface ? "interface" : "class"),
3571                              IDENTIFIER_POINTER (qualified_name),
3572                              IDENTIFIER_POINTER (raw_name));
3573     }
3574
3575   /* Static classes can be declared only in top level classes. Note:
3576      once static, a inner class is a top level class. */
3577   if (flags & ACC_STATIC)
3578     {
3579       /* Catch the specific error of declaring an class inner class
3580          with no toplevel enclosing class. Prevent check_modifiers from
3581          complaining a second time */
3582       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3583         {
3584           parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3585                                IDENTIFIER_POINTER (qualified_name));
3586           sca = ACC_STATIC;
3587         }
3588       /* Else, in the context of a top-level class declaration, let
3589          `check_modifiers' do its job, otherwise, give it a go */
3590       else
3591         sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3592     }
3593
3594   /* Inner classes can be declared private or protected
3595      within their enclosing classes. */
3596   if (CPC_INNER_P ())
3597     {
3598       /* A class which is local to a block can't be public, private,
3599          protected or static. But it is created final, so allow this
3600          one. */
3601       if (current_function_decl)
3602         icaf = sca = uaaf = ACC_FINAL;
3603       else
3604         {
3605           check_modifiers_consistency (flags);
3606           icaf = ACC_PROTECTED;
3607           if (! CLASS_INTERFACE (GET_CPC ()))
3608             icaf |= ACC_PRIVATE;
3609         }
3610     }
3611
3612   if (is_interface)
3613     {
3614       if (CPC_INNER_P ())
3615         uaaf = INTERFACE_INNER_MODIFIERS;
3616       else
3617         uaaf = INTERFACE_MODIFIERS;
3618
3619       check_modifiers ("Illegal modifier %qs for interface declaration",
3620                        flags, uaaf);
3621     }
3622   else
3623     check_modifiers ((current_function_decl ?
3624                       "Illegal modifier %qs for local class declaration" :
3625                       "Illegal modifier %qs for class declaration"),
3626                      flags, uaaf|sca|icaf);
3627   return 0;
3628 }
3629
3630 /* Construct a nested class name.  If the final component starts with
3631    a digit, return true.  Otherwise return false.  */
3632 static int
3633 make_nested_class_name (tree cpc_list)
3634 {
3635   tree name;
3636
3637   if (!cpc_list)
3638     return 0;
3639
3640   make_nested_class_name (TREE_CHAIN (cpc_list));
3641
3642   /* Pick the qualified name when dealing with the first upmost
3643      enclosing class */
3644   name = (TREE_CHAIN (cpc_list)
3645           ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3646   obstack_grow (&temporary_obstack,
3647                 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3648   obstack_1grow (&temporary_obstack, '$');
3649
3650   return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3651 }
3652
3653 /* Can't redefine a class already defined in an earlier scope. */
3654
3655 static int
3656 check_inner_class_redefinition (tree raw_name, tree cl)
3657 {
3658   tree scope_list;
3659
3660   for (scope_list = GET_CPC_LIST (); scope_list;
3661        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3662     if (raw_name == GET_CPC_UN_NODE (scope_list))
3663       {
3664         parse_error_context
3665           (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",
3666            IDENTIFIER_POINTER (raw_name));
3667         return 1;
3668       }
3669   return 0;
3670 }
3671
3672 /* Tries to find a decl for CLASS_TYPE within ENCLOSING.  May return an
3673    invisible/non-accessible matching decl when an accessible one could not be 
3674    found, in order to give a better error message when accessibility is 
3675    checked later.  */
3676
3677 static tree
3678 resolve_inner_class (tree context, tree cl, tree enclosing, tree class_type)
3679 {
3680   tree local_super = NULL_TREE;
3681   tree candidate = NULL_TREE;
3682
3683   /* This hash table is used to register the classes we're going
3684      through when searching the current class as an inner class, in
3685      order to detect circular references.  */
3686   htab_t circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
3687                                          NULL);
3688
3689   while (enclosing)
3690     {
3691       tree decl;
3692
3693       *htab_find_slot (circularity_hash, enclosing, INSERT) = enclosing;
3694
3695       if ((decl = find_as_inner_class (enclosing, class_type, cl)))
3696         {
3697           if (inner_class_accessible (decl, context))
3698             {
3699               candidate = decl;
3700               break;
3701             }
3702           else
3703             if (candidate == NULL_TREE)
3704               candidate = decl;
3705         }       
3706
3707       /* Now go to the upper classes, bail out if necessary.  We will
3708          analyze the returned SUPER and act accordingly (see
3709          do_resolve_class).  */
3710       if (JPRIMITIVE_TYPE_P (TREE_TYPE (enclosing))
3711           || TREE_TYPE (enclosing) == void_type_node)
3712         {
3713           parse_error_context (cl, "Qualifier must be a reference");
3714           enclosing = NULL_TREE;
3715           break;
3716         }
3717       local_super = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
3718       if (!local_super || local_super == object_type_node)
3719         break;
3720
3721       if (TREE_CODE (local_super) == POINTER_TYPE)
3722         local_super = do_resolve_class (NULL, NULL, local_super, NULL, NULL);
3723       else
3724         local_super = TYPE_NAME (local_super);
3725
3726       /* We may not have checked for circular inheritance yet, so do so
3727          here to prevent an infinite loop. */
3728       if (htab_find (circularity_hash, local_super) != NULL)
3729         {
3730           if (!cl)
3731             cl = lookup_cl (enclosing);
3732
3733           parse_error_context
3734             (cl, "Cyclic inheritance involving %s",
3735              IDENTIFIER_POINTER (DECL_NAME (enclosing)));
3736           enclosing = NULL_TREE;
3737         }
3738       else
3739         enclosing = local_super;
3740     }
3741
3742   htab_delete (circularity_hash);
3743
3744   /* We failed, but we might have found a matching class that wasn't 
3745      accessible.  Return that to get a better error message.  */
3746   return candidate;
3747 }
3748
3749 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3750    qualified. */
3751
3752 static tree
3753 find_as_inner_class (tree enclosing, tree name, tree cl)
3754 {
3755   tree qual, to_return;
3756   if (!enclosing)
3757     return NULL_TREE;
3758
3759   name = TYPE_NAME (name);
3760
3761   /* First search: within the scope of `enclosing', search for name */
3762   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3763     qual = EXPR_WFL_QUALIFICATION (cl);
3764   else if (cl)
3765     qual = build_tree_list (cl, NULL_TREE);
3766   else
3767     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3768
3769   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3770     return to_return;
3771
3772   /* We're dealing with a qualified name. Try to resolve thing until
3773      we get something that is an enclosing class. */
3774   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3775     {
3776       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3777
3778       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3779            qual = TREE_CHAIN (qual))
3780         {
3781           acc = merge_qualified_name (acc,
3782                                       EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3783           BUILD_PTR_FROM_NAME (ptr, acc);
3784           decl = do_resolve_class (NULL_TREE, NULL_TREE, ptr, NULL_TREE, cl);
3785         }
3786
3787       /* A NULL qual and a decl means that the search ended
3788          successfully?!? We have to do something then. FIXME */
3789
3790       if (decl)
3791         enclosing = decl;
3792       else
3793         qual = EXPR_WFL_QUALIFICATION (cl);
3794     }
3795   /* Otherwise, create a qual for the other part of the resolution. */
3796   else
3797     qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3798
3799   return find_as_inner_class_do (qual, enclosing);
3800 }
3801
3802 /* We go inside the list of sub classes and try to find a way
3803    through. */
3804
3805 static tree
3806 find_as_inner_class_do (tree qual, tree enclosing)
3807 {
3808   if (!qual)
3809     return NULL_TREE;
3810
3811   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3812     {
3813       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3814       tree next_enclosing = NULL_TREE;
3815       tree inner_list;
3816
3817       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3818            inner_list; inner_list = TREE_CHAIN (inner_list))
3819         {
3820           if (TREE_VALUE (inner_list) == name_to_match)
3821             {
3822               next_enclosing = TREE_PURPOSE (inner_list);
3823               break;
3824             }
3825         }
3826       enclosing = next_enclosing;
3827     }
3828
3829   return (!qual && enclosing ? enclosing : NULL_TREE);
3830 }
3831
3832 static void
3833 link_nested_class_to_enclosing (void)
3834 {
3835   if (GET_ENCLOSING_CPC ())
3836     {
3837       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3838       DECL_INNER_CLASS_LIST (enclosing) =
3839         tree_cons (GET_CPC (), GET_CPC_UN (),
3840                    DECL_INNER_CLASS_LIST (enclosing));
3841     }
3842 }
3843
3844 static tree
3845 maybe_make_nested_class_name (tree name)
3846 {
3847   tree id = NULL_TREE;
3848
3849   if (CPC_INNER_P ())
3850     {
3851       /* If we're in a function, we must append a number to create the
3852          nested class name.  However, we don't do this if the class we
3853          are constructing is anonymous, because in that case we'll
3854          already have a number as the class name.  */
3855       if (! make_nested_class_name (GET_CPC_LIST ())
3856           && current_function_decl != NULL_TREE
3857           && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3858         {
3859           char buf[10];
3860           sprintf (buf, "%d", anonymous_class_counter);
3861           ++anonymous_class_counter;
3862           obstack_grow (&temporary_obstack, buf, strlen (buf));
3863           obstack_1grow (&temporary_obstack, '$');
3864         }
3865       obstack_grow0 (&temporary_obstack,
3866                      IDENTIFIER_POINTER (name),
3867                      IDENTIFIER_LENGTH (name));
3868       id = get_identifier (obstack_finish (&temporary_obstack));
3869       if (ctxp->package)
3870         QUALIFIED_P (id) = 1;
3871     }
3872   return id;
3873 }
3874
3875 /* If DECL is NULL, create and push a new DECL, record the current
3876    line CL and do other maintenance things.  */
3877
3878 static tree
3879 maybe_create_class_interface_decl (tree decl, tree raw_name,
3880                                    tree qualified_name, tree cl)
3881 {
3882   if (!decl)
3883     decl = push_class (make_class (), qualified_name);
3884
3885   /* Take care of the file and line business */
3886 #ifdef USE_MAPPED_LOCATION
3887   DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3888 #else
3889   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3890   DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3891 #endif
3892   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3893   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3894 #ifdef USE_MAPPED_LOCATION
3895   {
3896     tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3897     CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3898       tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3899   }
3900 #else
3901   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3902     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3903 #endif
3904
3905   PUSH_CPC (decl, raw_name);
3906   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3907
3908   /* Link the declaration to the already seen ones */
3909   TREE_CHAIN (decl) = ctxp->class_list;
3910   ctxp->class_list = decl;
3911
3912   /* Create a new nodes in the global lists */
3913   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3914   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3915
3916   /* Install a new dependency list element */
3917   create_jdep_list (ctxp);
3918
3919   /* We keep the compilation unit imports in the class so that
3920      they can be used later to resolve type dependencies that
3921      aren't necessary to solve now. */
3922   TYPE_IMPORT_LIST (TREE_TYPE (decl)) = ctxp->import_list;
3923   TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (decl)) = ctxp->import_demand_list;
3924
3925   TYPE_PACKAGE (TREE_TYPE (decl)) = ctxp->package;
3926
3927   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3928                           IDENTIFIER_POINTER (qualified_name)));
3929   return decl;
3930 }
3931
3932 static void
3933 add_superinterfaces (tree decl, tree interface_list)
3934 {
3935   tree node;
3936   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3937      takes care of ensuring that:
3938        - This is an accessible interface type,
3939        - Circularity detection.
3940    parser_add_interface is then called. If present but not defined,
3941    the check operation is delayed until the super interface gets
3942    defined.  */
3943   for (node = interface_list; node; node = TREE_CHAIN (node))
3944     {
3945       tree current = TREE_PURPOSE (node);
3946       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3947       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3948         {
3949           if (!parser_check_super_interface (idecl, decl, current))
3950             parser_add_interface (decl, idecl, current);
3951         }
3952       else
3953         register_incomplete_type (JDEP_INTERFACE,
3954                                   current, decl, NULL_TREE);
3955     }
3956 }
3957
3958 /* Create an interface in pass1 and return its decl. Return the
3959    interface's decl in pass 2.  */
3960
3961 static tree
3962 create_interface (int flags, tree id, tree super)
3963 {
3964   tree raw_name = EXPR_WFL_NODE (id);
3965   tree q_name = parser_qualified_classname (raw_name);
3966   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3967
3968   /* Certain syntax errors are making SUPER be like ID. Avoid this
3969      case. */
3970   if (ctxp->class_err && id == super)
3971     super = NULL;
3972
3973   EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3974
3975   /* Basic checks: scope, redefinition, modifiers */
3976   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3977     {
3978       PUSH_ERROR ();
3979       return NULL_TREE;
3980     }
3981
3982   /* Suspend the current parsing context if we're parsing an inner
3983      interface */
3984   if (CPC_INNER_P ())
3985     {
3986       java_parser_context_suspend ();
3987       /* Interface members are public. */
3988       if (CLASS_INTERFACE (GET_CPC ()))
3989         flags |= ACC_PUBLIC;
3990     }
3991
3992   /* Push a new context for (static) initialized upon declaration fields */
3993   java_parser_context_push_initialized_field ();
3994