OSDN Git Service

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